-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (124 loc) · 3.63 KB
/
index.js
File metadata and controls
142 lines (124 loc) · 3.63 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* This is the entry point for the GraphQL API plugin. Here we specify the GraphQL schema and resolvers.
*/
import fs from 'fs'
import path from 'path'
import {fileURLToPath} from 'url'
import Fastify from 'fastify'
import cors from '@fastify/cors'
import mercurius from 'mercurius'
import {makeExecutableSchema} from '@graphql-tools/schema'
import {PubSub} from 'graphql-subscriptions'
import {gql} from 'graphql-tag'
import resolvers from './resolvers.js'
/**
* @typedef {Object} GraphQLConfig
* @property {boolean} [disabled=false] Whether the GraphQL API is disabled.
* @property {number} [port=4000] The port to listen on.
*/
/**
* @typedef {Object} GraphQLConfig
* @property {boolean} [disabled=false] Whether the GraphQL API is disabled.
* @property {number} [port=4000] The port to listen on.
*/
/**
* @param {API} api
* @param {GraphQLConfig} api.config.graphql The configuration object for the GraphQL API.
*/
export async function initialize(api) {
const config = api.config.graphql ?? {}
const pubSub = new PubSub()
if (config.disabled) {
api.log.info('GraphQL API is disabled')
return
}
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const schemaPath = path.join(__dirname, 'schema.graphql')
const typeDefs = gql(await fs.promises.readFile(schemaPath, 'utf8'))
const app = Fastify()
app.register(cors)
const schema = makeExecutableSchema({
typeDefs,
resolvers
})
app.register(mercurius, {
schema,
context: () => {
return { api, pubSub: pubSub }
},
subscription: {
onConnect: () => {
return { api, pubSub: pubSub }
}
}
})
// Start the server
app.listen({port: config.port || 4000}, (err, address) => {
if (err) {
api.log.error(err.message)
throw new Error(err.message)
}
api.log.info(`GraphQL endpoint available at ${address}`)
})
api.comms.on('all', (msg) => {
api.log.trace(
'Received messageInput from system, sending to GraphQL subs',
msg.toObject()
)
pubSub.publish('MESSAGE_SENT', {
messageCreated: msg.toObject()
})
})
api.on('messageUpdated', (msg) => {
api.log.trace(
'Received messageUpdated from system, sending to GraphQL subs',
msg.toObject()
)
pubSub.publish('MESSAGE_UPDATED', {
messageUpdated: msg.toObject()
})
})
api.groups.on('created', (name, members) => {
api.log.trace(
'Received groupCreated event from system, sending to GraphQL subs',
name
)
pubSub.publish('GROUP_CREATED', {
groupCreated: { name, members }
})
})
api.groups.on('updated', (name, members) => {
api.log.trace(
'Received groupUpdated event from system, sending to GraphQL subs',
name
)
pubSub.publish('GROUP_UPDATED', {
groupUpdated: { name, members }
})
})
api.on('agentCreated', (agent) => {
api.log.trace(
'Received agentCreated event from system, sending to GraphQL subs',
agent
)
pubSub.publish('AGENT_CREATED', {
agentCreated: agent
})
})
api.on('agentUpdated', (agent) => {
api.log.trace(
'Received agentUpdated event from system, sending to GraphQL subs',
agent
)
pubSub.publish('AGENT_UPDATED', {
agentUpdated: agent
})
})
api.skills.any(['skillStarted', 'skillCompleted', 'skillNotFound', 'skillError'], (event, ...args) => {
api.log.trace('Received a skill status event from system, sending to GraphQL subs', event, args)
pubSub.publish('SKILL_STATUS', {
skillStatus: { status:event, agent:args[0], skill:args[1], data: JSON.stringify(args[2]) }
})
})
}