Skip to content

Commit

Permalink
Add agent & group api
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Nov 21, 2023
1 parent bb98f50 commit edfb0fa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export async function initialize(api) {

app.register(mercurius, {
schema,
context: (request, reply) => {
context: () => {
return { api, pubSub: pubSub }
},
subscription: {
onConnect: (connectionParams, webSocket, context) => {
onConnect: () => {
return { api, pubSub: pubSub }
},
},
Expand All @@ -57,8 +57,32 @@ export async function initialize(api) {
})

api.comms.on('all', (msg) => {
api.log.trace(
'Received message from system, sending to GraphQL subs',
msg.toObject()
)
pubSub.publish('MESSAGE_SENT', {
messageSent: msg.toObject(),
})
})

api.on('groupCreated', (group) => {
api.log.debug(
'Received groupCreated event from system, sending to GraphQL subs',
group
)
pubSub.publish('GROUP_CREATED', {
groupCreated: group,
})
})

api.on('agentCreated', (agent) => {
api.log.debug(
'Received agentCreated event from system, sending to GraphQL subs',
agent
)
pubSub.publish('AGENT_CREATED', {
agentCreated: agent,
})
})
}
54 changes: 48 additions & 6 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,45 @@ export const typeDefs = gql`
messages: [Message]
}
type Agent {
name: String
driver: Driver
name: String!
config: AgentConfig!
driver: Driver!
}
type AgentConfig {
type: String!
description: String
creator: Boolean
isolate: Boolean
driver: DriverConfig!
}
type DriverConfig {
type: String!
}
type Driver {
type: String
type: String!
}
type Group {
name: String
name: String!
}
type Message {
id: Float
type: String
source: String
target: String
target: String!
timestamp: String
content: String
content: String!
}
type Mutation {
sendMessage(message: String!, target: String!): Message
createGroup(name: String!): String
createAgent(name: String!, driver: String!): Agent
}
type Subscription {
messageSent: Message
groupCreated: String
agentCreated: Agent
}
`

Expand Down Expand Up @@ -86,12 +101,39 @@ export const resolvers = {
context.api.comms.emit(msg)
return msg
},
createGroup: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to create Group', args)
context.api.createGroup(args.name)
return args.name
},
createAgent: (parent, args, context) => {
context.api.log.trace('GraphQL Received Request to create Agent', args)

return context.api.createAgent(args.name, {
description: 'Created by GraphQL',
creator: false,
isolate: false,
driver: {
type: args.driver,
},
})
},
},
Subscription: {
messageSent: {
subscribe: (_, __, { pubSub }) => {
return pubSub.asyncIterator(['MESSAGE_SENT'])
},
},
groupCreated: {
subscribe: (_, __, { pubSub }) => {
return pubSub.asyncIterator(['GROUP_CREATED'])
},
},
agentCreated: {
subscribe: (_, __, { pubSub }) => {
return pubSub.asyncIterator(['AGENT_CREATED'])
},
},
},
}

0 comments on commit edfb0fa

Please sign in to comment.