Skip to content

Commit

Permalink
Clean up & first implementation running with OpenAI
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Dec 5, 2023
1 parent fd04ad6 commit 717b5b6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 91 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function initialize(api) {
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const schemaPath = path.join(__dirname, 'schema.graphql')
const typeDefs = gql(fs.readFileSync(schemaPath, 'utf8'))
const typeDefs = gql(await fs.promises.readFile(schemaPath, 'utf8'))

const app = Fastify()
app.register(cors)
Expand All @@ -67,8 +67,8 @@ export async function initialize(api) {
// Start the server
app.listen({port: config.port || 4000}, (err, address) => {
if (err) {
api.log.error(err)
throw new Error(err)
api.log.error(err.message)
throw new Error(err.message)
}
api.log.info(`GraphQL endpoint available at ${address}`)
})
Expand Down
171 changes: 83 additions & 88 deletions resolvers.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,97 @@
const Query = {
agents: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Agents', args)
const response = []
const agentMap = context.api.agents.all()
for (let agentName in agentMap) {
const agent = agentMap[agentName]
response.push(agent)
}
return response
},
drivers: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Drivers', args)
const response = new Set()
const agentMap = context.api.agents.all()
for (let agentName in agentMap) {
const agent = agentMap[agentName]
response.add(agent.driver)
}
return response.values()
},
groups: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Groups', args)
const response = new Set()
const groups = context.api.groups.map()
for (const groupName in groups) {
response.add({
name: groupName,
members: groups[groupName]
})
}
return response.values()
},
history: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for History', args)
if (!args.target) return context.api.comms.history.all()
const sourceHistory = context.api.comms.history.bySource(args.source)
const targetHistory = context.api.comms.history.byTarget(args.target)
return [...sourceHistory, ...targetHistory]
.sort((a, b) => a.timestamp - b.timestamp)
.map((msg) => msg.toObject())
agents: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Agents', args)
const response = []
const agentMap = context.api.agents.all()
for (let agentName in agentMap) {
const agent = agentMap[agentName]
response.push(agent)
}
return response
},
drivers: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Drivers', args)
const driverNames = context.api.agents.availableDrivers()
return driverNames.map(name => ({type: name}))
},
groups: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for Groups', args)
const response = []
const groups = context.api.groups.map()
for (const groupName in groups) {
response.push({
name: groupName,
members: groups[groupName]
})
}
return response
},
history: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request for History', args)
if (!args.target) return context.api.comms.history.all()
const sourceHistory = context.api.comms.history.bySource(args.source)
const targetHistory = context.api.comms.history.byTarget(args.target)
return [...sourceHistory, ...targetHistory]
.sort((a, b) => a.timestamp - b.timestamp)
.map((msg) => msg.toObject())
}
}

const Mutation = {
sendMessage: (parent, args, context) => {
context.api.log.trace('GraphQL Received Message:', args)
const msg = context.api.comms.createMessage(
args.target,
args.source || 'user',
args.message
)
context.api.comms.emit(msg)
return msg.toObject()
},
createGroup: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to add Group', args)
context.api.createGroup(args.name)
return args.name
},
createAgent: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to add Agent', args)
sendMessage: (parent, args, context) => {
context.api.log.trace('GraphQL Received Message:', args)
const msg = context.api.comms.createMessage(
args.target,
args.source || 'user',
args.message
)
context.api.comms.emit(msg)
return msg.toObject()
},
createGroup: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to add Group', args)
context.api.createGroup(args.name)
return args.name
},
createAgent: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to add Agent', args)

return context.api.createAgent(args.name, {
description: 'Created by GraphQL',
creator: false,
isolate: false,
driver: {
type: args.driver
}
})
}
return context.api.createAgent(args.name, {
description: 'Created by GraphQL',
creator: false,
isolate: false,
driver: {
type: args.driver
}
})
}
}

const Subscription = {
messageCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['MESSAGE_SENT'])
}
},
groupCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_CREATED'])
}
},
groupUpdated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_UPDATED'])
}
},
agentCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['AGENT_CREATED'])
}
messageCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['MESSAGE_SENT'])
}
},
groupCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_CREATED'])
}
},
groupUpdated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_UPDATED'])
}
},
agentCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['AGENT_CREATED'])
}
}
}

export default {
Query,
Mutation,
Subscription
Query,
Mutation,
Subscription
}

0 comments on commit 717b5b6

Please sign in to comment.