-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolvers.js
125 lines (122 loc) · 3.32 KB
/
resolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 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)
let messages
if (!args.source && !args.target) {
messages = context.api.comms.history.all()
} else {
const sourceHistory = context.api.comms.history.bySource(args.source)
const targetHistory = context.api.comms.history.byTarget(args.target)
messages = [...sourceHistory, ...targetHistory]
}
return messages
.sort((a, b) => a.timestamp - b.timestamp)
.map((msg) => msg.toObject())
},
skills: (parent, args, context) => {
const response = []
for (const skill of context.api.skills.list()) {
response.push({
name: skill
})
}
return response
}
}
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)
return context.api.createAgent(args.name, {
description: args.description,
driver: {
type: args.driver
},
instructions: args.instructions,
skills: args.skills
})
}
}
const Subscription = {
messageCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['MESSAGE_SENT'])
}
},
messageUpdated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['MESSAGE_UPDATED'])
}
},
groupCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_CREATED'])
}
},
groupUpdated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['GROUP_UPDATED'])
}
},
agentCreated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['AGENT_CREATED'])
}
},
agentUpdated: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['AGENT_UPDATED'])
}
},
skillStatus: {
subscribe: (_, __, {pubSub}) => {
return pubSub.asyncIterator(['SKILL_STATUS'])
}
}
}
export default {
Query,
Mutation,
Subscription
}