@cogitator-ai/fastify 0.1.15 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -38
- package/dist/hooks/error-handler.d.ts.map +1 -1
- package/dist/hooks/error-handler.js +29 -0
- package/dist/hooks/error-handler.js.map +1 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +22 -17
- package/dist/plugin.js.map +1 -1
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/agents.js +57 -61
- package/dist/routes/agents.js.map +1 -1
- package/dist/routes/swarms.d.ts.map +1 -1
- package/dist/routes/swarms.js +49 -72
- package/dist/routes/swarms.js.map +1 -1
- package/dist/routes/threads.d.ts.map +1 -1
- package/dist/routes/threads.js +11 -40
- package/dist/routes/threads.js.map +1 -1
- package/dist/routes/utils.d.ts +12 -0
- package/dist/routes/utils.d.ts.map +1 -0
- package/dist/routes/utils.js +32 -0
- package/dist/routes/utils.js.map +1 -0
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/workflows.js +57 -67
- package/dist/routes/workflows.js.map +1 -1
- package/dist/streaming/fastify-stream-writer.d.ts +2 -0
- package/dist/streaming/fastify-stream-writer.d.ts.map +1 -1
- package/dist/streaming/fastify-stream-writer.js +26 -3
- package/dist/streaming/fastify-stream-writer.js.map +1 -1
- package/dist/types.d.ts +11 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +8 -6
- package/dist/types.js.map +1 -1
- package/dist/websocket/handler.d.ts.map +1 -1
- package/dist/websocket/handler.js +171 -95
- package/dist/websocket/handler.js.map +1 -1
- package/package.json +9 -9
package/dist/routes/swarms.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { SwarmRunRequestSchema } from '../types.js';
|
|
2
2
|
import { FastifyStreamWriter, generateId } from '../streaming/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { isModuleNotFound, onClientDisconnect, resolveError, sendError, sendRouteError, } from './utils.js';
|
|
4
|
+
const paramsSchema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: { name: { type: 'string' } },
|
|
7
|
+
required: ['name'],
|
|
8
|
+
};
|
|
9
|
+
const SWARMS_MISSING = 'Swarms package not installed';
|
|
4
10
|
export const swarmRoutes = async (fastify) => {
|
|
11
|
+
const findSwarm = (name) => Object.hasOwn(fastify.cogitator.swarms, name) ? fastify.cogitator.swarms[name] : undefined;
|
|
5
12
|
fastify.get('/swarms', async () => {
|
|
6
13
|
const swarmList = Object.entries(fastify.cogitator.swarms).map(([name, config]) => {
|
|
7
14
|
const agents = [];
|
|
@@ -22,28 +29,24 @@ export const swarmRoutes = async (fastify) => {
|
|
|
22
29
|
const response = { swarms: swarmList };
|
|
23
30
|
return response;
|
|
24
31
|
});
|
|
25
|
-
fastify.post('/swarms/:name/run', {
|
|
26
|
-
schema: {
|
|
27
|
-
params: {
|
|
28
|
-
type: 'object',
|
|
29
|
-
properties: { name: { type: 'string' } },
|
|
30
|
-
required: ['name'],
|
|
31
|
-
},
|
|
32
|
-
body: SwarmRunRequestSchema,
|
|
33
|
-
},
|
|
34
|
-
}, async (request, reply) => {
|
|
32
|
+
fastify.post('/swarms/:name/run', { schema: { params: paramsSchema, body: SwarmRunRequestSchema } }, async (request, reply) => {
|
|
35
33
|
const { name } = request.params;
|
|
36
|
-
const swarmConfig =
|
|
37
|
-
? fastify.cogitator.swarms[name]
|
|
38
|
-
: undefined;
|
|
34
|
+
const swarmConfig = findSwarm(name);
|
|
39
35
|
if (!swarmConfig) {
|
|
40
|
-
return reply
|
|
41
|
-
error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' },
|
|
42
|
-
});
|
|
36
|
+
return sendError(reply, 404, `Swarm '${name}' not found`, 'NOT_FOUND');
|
|
43
37
|
}
|
|
38
|
+
let disconnected = false;
|
|
39
|
+
let abortSwarm;
|
|
40
|
+
onClientDisconnect(reply, () => {
|
|
41
|
+
disconnected = true;
|
|
42
|
+
abortSwarm?.();
|
|
43
|
+
});
|
|
44
44
|
try {
|
|
45
45
|
const { Swarm } = await import('@cogitator-ai/swarms');
|
|
46
46
|
const swarm = new Swarm(fastify.cogitator.runtime, swarmConfig);
|
|
47
|
+
abortSwarm = () => swarm.abort();
|
|
48
|
+
if (disconnected)
|
|
49
|
+
swarm.abort();
|
|
47
50
|
const result = await swarm.run({
|
|
48
51
|
input: request.body.input,
|
|
49
52
|
context: request.body.context,
|
|
@@ -73,47 +76,33 @@ export const swarmRoutes = async (fastify) => {
|
|
|
73
76
|
return response;
|
|
74
77
|
}
|
|
75
78
|
catch (error) {
|
|
76
|
-
if (error
|
|
77
|
-
return reply
|
|
78
|
-
error: { message: 'Swarms package not installed', code: 'UNIMPLEMENTED' },
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
if (CogitatorError.isCogitatorError(error)) {
|
|
82
|
-
return reply.status(500).send({ error: { message: error.message, code: error.code } });
|
|
79
|
+
if (isModuleNotFound(error)) {
|
|
80
|
+
return sendError(reply, 501, SWARMS_MISSING, 'UNIMPLEMENTED');
|
|
83
81
|
}
|
|
84
|
-
request
|
|
85
|
-
return reply
|
|
86
|
-
.status(500)
|
|
87
|
-
.send({ error: { message: 'Internal server error', code: 'INTERNAL' } });
|
|
82
|
+
return sendRouteError(request, reply, error, 'swarm run error');
|
|
88
83
|
}
|
|
89
84
|
});
|
|
90
|
-
fastify.post('/swarms/:name/stream', {
|
|
91
|
-
schema: {
|
|
92
|
-
params: {
|
|
93
|
-
type: 'object',
|
|
94
|
-
properties: { name: { type: 'string' } },
|
|
95
|
-
required: ['name'],
|
|
96
|
-
},
|
|
97
|
-
body: SwarmRunRequestSchema,
|
|
98
|
-
},
|
|
99
|
-
}, async (request, reply) => {
|
|
85
|
+
fastify.post('/swarms/:name/stream', { schema: { params: paramsSchema, body: SwarmRunRequestSchema } }, async (request, reply) => {
|
|
100
86
|
const { name } = request.params;
|
|
101
|
-
const swarmConfig =
|
|
102
|
-
? fastify.cogitator.swarms[name]
|
|
103
|
-
: undefined;
|
|
87
|
+
const swarmConfig = findSwarm(name);
|
|
104
88
|
if (!swarmConfig) {
|
|
105
|
-
return reply
|
|
106
|
-
error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' },
|
|
107
|
-
});
|
|
89
|
+
return sendError(reply, 404, `Swarm '${name}' not found`, 'NOT_FOUND');
|
|
108
90
|
}
|
|
109
91
|
const writer = new FastifyStreamWriter(reply);
|
|
110
92
|
const messageId = generateId('swarm');
|
|
111
|
-
|
|
93
|
+
let disconnected = false;
|
|
94
|
+
let abortSwarm;
|
|
95
|
+
onClientDisconnect(reply, () => {
|
|
96
|
+
disconnected = true;
|
|
97
|
+
abortSwarm?.();
|
|
112
98
|
writer.close();
|
|
113
99
|
});
|
|
114
100
|
try {
|
|
115
101
|
const { Swarm } = await import('@cogitator-ai/swarms');
|
|
116
102
|
const swarm = new Swarm(fastify.cogitator.runtime, swarmConfig);
|
|
103
|
+
abortSwarm = () => swarm.abort();
|
|
104
|
+
if (disconnected)
|
|
105
|
+
swarm.abort();
|
|
117
106
|
writer.start(messageId);
|
|
118
107
|
const result = await swarm.run({
|
|
119
108
|
input: request.body.input,
|
|
@@ -144,48 +133,36 @@ export const swarmRoutes = async (fastify) => {
|
|
|
144
133
|
writer.swarmEvent('swarm_completed', {
|
|
145
134
|
swarmId: swarm.id,
|
|
146
135
|
output: result.output,
|
|
147
|
-
usage:
|
|
136
|
+
usage: {
|
|
137
|
+
totalTokens: resourceUsage.totalTokens,
|
|
138
|
+
totalCost: resourceUsage.totalCost,
|
|
139
|
+
elapsedTime: resourceUsage.elapsedTime,
|
|
140
|
+
},
|
|
148
141
|
});
|
|
149
142
|
writer.finish(messageId);
|
|
150
143
|
}
|
|
151
144
|
catch (error) {
|
|
152
|
-
if (error
|
|
153
|
-
|
|
145
|
+
if (isModuleNotFound(error)) {
|
|
146
|
+
return sendError(reply, 501, SWARMS_MISSING, 'UNIMPLEMENTED');
|
|
154
147
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
else {
|
|
159
|
-
request.log.error({ err: error }, 'swarm stream error');
|
|
160
|
-
writer.error('Internal server error', 'INTERNAL');
|
|
148
|
+
if (!disconnected) {
|
|
149
|
+
const resolved = resolveError(request, error, 'swarm stream error');
|
|
150
|
+
writer.error(resolved.message, resolved.code);
|
|
161
151
|
}
|
|
162
152
|
}
|
|
163
153
|
finally {
|
|
164
154
|
writer.close();
|
|
165
155
|
}
|
|
156
|
+
return reply;
|
|
166
157
|
});
|
|
167
|
-
fastify.get('/swarms/:name/blackboard', {
|
|
168
|
-
schema: {
|
|
169
|
-
params: {
|
|
170
|
-
type: 'object',
|
|
171
|
-
properties: { name: { type: 'string' } },
|
|
172
|
-
required: ['name'],
|
|
173
|
-
},
|
|
174
|
-
},
|
|
175
|
-
}, async (request, reply) => {
|
|
158
|
+
fastify.get('/swarms/:name/blackboard', { schema: { params: paramsSchema } }, async (request, reply) => {
|
|
176
159
|
const { name } = request.params;
|
|
177
|
-
const swarmConfig =
|
|
178
|
-
? fastify.cogitator.swarms[name]
|
|
179
|
-
: undefined;
|
|
160
|
+
const swarmConfig = findSwarm(name);
|
|
180
161
|
if (!swarmConfig) {
|
|
181
|
-
return reply
|
|
182
|
-
error: { message: `Swarm '${name}' not found`, code: 'NOT_FOUND' },
|
|
183
|
-
});
|
|
162
|
+
return sendError(reply, 404, `Swarm '${name}' not found`, 'NOT_FOUND');
|
|
184
163
|
}
|
|
185
164
|
if (!swarmConfig.blackboard?.enabled) {
|
|
186
|
-
return reply
|
|
187
|
-
error: { message: 'Blackboard not enabled for this swarm', code: 'INVALID_INPUT' },
|
|
188
|
-
});
|
|
165
|
+
return sendError(reply, 400, 'Blackboard not enabled for this swarm', 'INVALID_INPUT');
|
|
189
166
|
}
|
|
190
167
|
const response = {
|
|
191
168
|
sections: swarmConfig.blackboard.sections || {},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swarms.js","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"swarms.js","sourceRoot":"","sources":["../../src/routes/swarms.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAExE,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,SAAS,EACT,cAAc,GACf,MAAM,YAAY,CAAC;AAMpB,MAAM,YAAY,GAAG;IACnB,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;IACxC,QAAQ,EAAE,CAAC,MAAM,CAAC;CACV,CAAC;AAEX,MAAM,cAAc,GAAG,8BAA8B,CAAC;AAEtD,MAAM,CAAC,MAAM,WAAW,GAAuB,KAAK,EAAE,OAAO,EAAE,EAAE;IAC/D,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,EAAE,CACjC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAE7F,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;QAChC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE;YAChF,MAAM,MAAM,GAAa,EAAE,CAAC;YAC5B,IAAI,MAAM,CAAC,UAAU;g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EAAE,YAAY,EAAE,EAAE,EACpC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QACvB,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;QAChC,MAAM,WAAW,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;QAEpC,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,UAAU,IAAI,aAAa,EAAE,WAAW,CAAC,CAAC;QACzE,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,CAAC;YACrC,OAAO,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,uCAAuC,EAAE,eAAe,CAAC,CAAC;QACzF,CAAC;QAED,MAAM,QAAQ,GAAuB;YACnC,QAAQ,EAAE,WAAW,CAAC,UAAU,CAAC,QAAQ,IAAI,EAAE;SAChD,CAAC;QAEF,OAAO,QAAQ,CAAC;IAClB,CAAC,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"threads.d.ts","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AASlD,eAAO,MAAM,YAAY,EAAE,
|
|
1
|
+
{"version":3,"file":"threads.d.ts","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AASlD,eAAO,MAAM,YAAY,EAAE,kBAqH1B,CAAC"}
|
package/dist/routes/threads.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AddMessageRequestSchema } from '../types.js';
|
|
2
|
-
import {
|
|
2
|
+
import { sendError, sendRouteError } from './utils.js';
|
|
3
3
|
export const threadRoutes = async (fastify) => {
|
|
4
4
|
const getMemory = () => fastify.cogitator.runtime.memory;
|
|
5
5
|
fastify.get('/threads/:id', {
|
|
@@ -13,17 +13,13 @@ export const threadRoutes = async (fastify) => {
|
|
|
13
13
|
}, async (request, reply) => {
|
|
14
14
|
const memory = getMemory();
|
|
15
15
|
if (!memory) {
|
|
16
|
-
return reply
|
|
17
|
-
error: { message: 'Memory not configured', code: 'UNAVAILABLE' },
|
|
18
|
-
});
|
|
16
|
+
return sendError(reply, 503, 'Memory not configured', 'UNAVAILABLE');
|
|
19
17
|
}
|
|
20
18
|
const { id } = request.params;
|
|
21
19
|
try {
|
|
22
20
|
const result = await memory.getEntries({ threadId: id });
|
|
23
21
|
if (!result.success) {
|
|
24
|
-
return reply.
|
|
25
|
-
error: { message: result.error ?? 'Unknown error', code: 'INTERNAL' },
|
|
26
|
-
});
|
|
22
|
+
return sendError(reply, 500, result.error ?? 'Unknown error', 'INTERNAL');
|
|
27
23
|
}
|
|
28
24
|
const entries = result.data;
|
|
29
25
|
const messages = entries.map((entry) => entry.message);
|
|
@@ -37,13 +33,7 @@ export const threadRoutes = async (fastify) => {
|
|
|
37
33
|
return response;
|
|
38
34
|
}
|
|
39
35
|
catch (error) {
|
|
40
|
-
|
|
41
|
-
return reply.status(500).send({ error: { message: error.message, code: error.code } });
|
|
42
|
-
}
|
|
43
|
-
request.log.error({ err: error }, 'thread get error');
|
|
44
|
-
return reply
|
|
45
|
-
.status(500)
|
|
46
|
-
.send({ error: { message: 'Internal server error', code: 'INTERNAL' } });
|
|
36
|
+
return sendRouteError(request, reply, error, 'thread get error');
|
|
47
37
|
}
|
|
48
38
|
});
|
|
49
39
|
fastify.post('/threads/:id/messages', {
|
|
@@ -58,9 +48,7 @@ export const threadRoutes = async (fastify) => {
|
|
|
58
48
|
}, async (request, reply) => {
|
|
59
49
|
const memory = getMemory();
|
|
60
50
|
if (!memory) {
|
|
61
|
-
return reply
|
|
62
|
-
error: { message: 'Memory not configured', code: 'UNAVAILABLE' },
|
|
63
|
-
});
|
|
51
|
+
return sendError(reply, 503, 'Memory not configured', 'UNAVAILABLE');
|
|
64
52
|
}
|
|
65
53
|
const { id } = request.params;
|
|
66
54
|
const body = request.body;
|
|
@@ -72,22 +60,15 @@ export const threadRoutes = async (fastify) => {
|
|
|
72
60
|
content: body.content,
|
|
73
61
|
},
|
|
74
62
|
tokenCount: 0,
|
|
63
|
+
metadata: body.metadata,
|
|
75
64
|
});
|
|
76
65
|
if (!result.success) {
|
|
77
|
-
return reply.
|
|
78
|
-
error: { message: result.error ?? 'Unknown error', code: 'INTERNAL' },
|
|
79
|
-
});
|
|
66
|
+
return sendError(reply, 500, result.error ?? 'Unknown error', 'INTERNAL');
|
|
80
67
|
}
|
|
81
68
|
return reply.status(201).send({ success: true });
|
|
82
69
|
}
|
|
83
70
|
catch (error) {
|
|
84
|
-
|
|
85
|
-
return reply.status(500).send({ error: { message: error.message, code: error.code } });
|
|
86
|
-
}
|
|
87
|
-
request.log.error({ err: error }, 'thread add message error');
|
|
88
|
-
return reply
|
|
89
|
-
.status(500)
|
|
90
|
-
.send({ error: { message: 'Internal server error', code: 'INTERNAL' } });
|
|
71
|
+
return sendRouteError(request, reply, error, 'thread add message error');
|
|
91
72
|
}
|
|
92
73
|
});
|
|
93
74
|
fastify.delete('/threads/:id', {
|
|
@@ -101,28 +82,18 @@ export const threadRoutes = async (fastify) => {
|
|
|
101
82
|
}, async (request, reply) => {
|
|
102
83
|
const memory = getMemory();
|
|
103
84
|
if (!memory) {
|
|
104
|
-
return reply
|
|
105
|
-
error: { message: 'Memory not configured', code: 'UNAVAILABLE' },
|
|
106
|
-
});
|
|
85
|
+
return sendError(reply, 503, 'Memory not configured', 'UNAVAILABLE');
|
|
107
86
|
}
|
|
108
87
|
const { id } = request.params;
|
|
109
88
|
try {
|
|
110
89
|
const result = await memory.clearThread(id);
|
|
111
90
|
if (!result.success) {
|
|
112
|
-
return reply.
|
|
113
|
-
error: { message: result.error ?? 'Unknown error', code: 'INTERNAL' },
|
|
114
|
-
});
|
|
91
|
+
return sendError(reply, 500, result.error ?? 'Unknown error', 'INTERNAL');
|
|
115
92
|
}
|
|
116
93
|
return reply.status(204).send();
|
|
117
94
|
}
|
|
118
95
|
catch (error) {
|
|
119
|
-
|
|
120
|
-
return reply.status(500).send({ error: { message: error.message, code: error.code } });
|
|
121
|
-
}
|
|
122
|
-
request.log.error({ err: error }, 'thread delete error');
|
|
123
|
-
return reply
|
|
124
|
-
.status(500)
|
|
125
|
-
.send({ error: { message: 'Internal server error', code: 'INTERNAL' } });
|
|
96
|
+
return sendRouteError(request, reply, error, 'thread delete error');
|
|
126
97
|
}
|
|
127
98
|
});
|
|
128
99
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"threads.js","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"threads.js","sourceRoot":"","sources":["../../src/routes/threads.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAMvD,MAAM,CAAC,MAAM,YAAY,GAAuB,KAAK,EAAE,OAAO,EAAE,EAAE;IAChE,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC;IAEzD,OAAO,CAAC,GAAG,CACT,cAAc,EACd;QACE,MAAM,EAAE;YACN,MAAM,EAAE;gBACN,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;gBACtC,QAAQ,EAAE,CAAC,IAAI,CAAC;aACjB;SACF;KACF,EACD,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QACvB,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;Q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KAAK,EAAE,0BAA0B,CAAC,CAAC;QAC3E,CAAC;IACH,CAAC,CACF,CAAC;IAEF,OAAO,CAAC,MAAM,CACZ,cAAc,EACd;QACE,MAAM,EAAE;YACN,MAAM,EAAE;gBACN,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;gBACtC,QAAQ,EAAE,CAAC,IAAI,CAAC;aACjB;SACF;KACF,EACD,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QACvB,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;QAC3B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,uBAAuB,EAAE,aAAa,CAAC,CAAC;QACvE,CAAC;QAED,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;QAE9B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;YAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpB,OAAO,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,IAAI,eAAe,EAAE,UAAU,CAAC,CAAC;YAC5E,CAAC;YACD,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,qBAAqB,CAAC,CAAC;QACtE,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
2
|
+
export declare function sendError(reply: FastifyReply, status: number, message: string, code: string): FastifyReply;
|
|
3
|
+
export declare function isModuleNotFound(error: unknown): boolean;
|
|
4
|
+
export interface ResolvedError {
|
|
5
|
+
status: number;
|
|
6
|
+
message: string;
|
|
7
|
+
code: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function resolveError(request: FastifyRequest, error: unknown, label: string): ResolvedError;
|
|
10
|
+
export declare function sendRouteError(request: FastifyRequest, reply: FastifyReply, error: unknown, label: string): FastifyReply;
|
|
11
|
+
export declare function onClientDisconnect(reply: FastifyReply, handler: () => void): void;
|
|
12
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/routes/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG5D,wBAAgB,SAAS,CACvB,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,GACX,YAAY,CAEd;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOxD;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wBAAgB,YAAY,CAC1B,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,MAAM,GACZ,aAAa,CAUf;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,cAAc,EACvB,KAAK,EAAE,YAAY,EACnB,KAAK,EAAE,OAAO,EACd,KAAK,EAAE,MAAM,GACZ,YAAY,CAGd;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,IAAI,CAIjF"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CogitatorError, ERROR_STATUS_CODES } from '@cogitator-ai/types';
|
|
2
|
+
export function sendError(reply, status, message, code) {
|
|
3
|
+
return reply.status(status).send({ error: { message, code } });
|
|
4
|
+
}
|
|
5
|
+
export function isModuleNotFound(error) {
|
|
6
|
+
return (typeof error === 'object' &&
|
|
7
|
+
error !== null &&
|
|
8
|
+
'code' in error &&
|
|
9
|
+
(error.code === 'ERR_MODULE_NOT_FOUND' || error.code === 'MODULE_NOT_FOUND'));
|
|
10
|
+
}
|
|
11
|
+
export function resolveError(request, error, label) {
|
|
12
|
+
if (CogitatorError.isCogitatorError(error)) {
|
|
13
|
+
return {
|
|
14
|
+
status: ERROR_STATUS_CODES[error.code] || 500,
|
|
15
|
+
message: error.message,
|
|
16
|
+
code: error.code,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
request.log.error({ err: error }, label);
|
|
20
|
+
return { status: 500, message: 'Internal server error', code: 'INTERNAL' };
|
|
21
|
+
}
|
|
22
|
+
export function sendRouteError(request, reply, error, label) {
|
|
23
|
+
const resolved = resolveError(request, error, label);
|
|
24
|
+
return sendError(reply, resolved.status, resolved.message, resolved.code);
|
|
25
|
+
}
|
|
26
|
+
export function onClientDisconnect(reply, handler) {
|
|
27
|
+
reply.raw.on('close', () => {
|
|
28
|
+
if (!reply.raw.writableEnded)
|
|
29
|
+
handler();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/routes/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzE,MAAM,UAAU,SAAS,CACvB,KAAmB,EACnB,MAAc,EACd,OAAe,EACf,IAAY;IAEZ,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;AACjE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AC1C,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workflows.d.ts","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"workflows.d.ts","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAiClD,eAAO,MAAM,cAAc,EAAE,kBAsI5B,CAAC"}
|
package/dist/routes/workflows.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
import { WorkflowRunRequestSchema } from '../types.js';
|
|
2
2
|
import { FastifyStreamWriter, generateId } from '../streaming/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { isModuleNotFound, onClientDisconnect, resolveError, sendError, sendRouteError, } from './utils.js';
|
|
4
|
+
const paramsSchema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: { name: { type: 'string' } },
|
|
7
|
+
required: ['name'],
|
|
8
|
+
};
|
|
9
|
+
const WORKFLOWS_MISSING = 'Workflows package not installed';
|
|
10
|
+
function executorOptions(body) {
|
|
11
|
+
const options = body?.options ?? {};
|
|
12
|
+
return {
|
|
13
|
+
maxConcurrency: options.maxConcurrency,
|
|
14
|
+
maxIterations: options.maxIterations,
|
|
15
|
+
checkpoint: options.checkpoint,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
4
18
|
export const workflowRoutes = async (fastify) => {
|
|
19
|
+
const findWorkflow = (name) => Object.hasOwn(fastify.cogitator.workflows, name)
|
|
20
|
+
? fastify.cogitator.workflows[name]
|
|
21
|
+
: undefined;
|
|
5
22
|
fastify.get('/workflows', async () => {
|
|
6
23
|
const workflowList = Object.entries(fastify.cogitator.workflows).map(([name, workflow]) => ({
|
|
7
24
|
name,
|
|
@@ -11,90 +28,60 @@ export const workflowRoutes = async (fastify) => {
|
|
|
11
28
|
const response = { workflows: workflowList };
|
|
12
29
|
return response;
|
|
13
30
|
});
|
|
14
|
-
fastify.post('/workflows/:name/run', {
|
|
15
|
-
schema: {
|
|
16
|
-
params: {
|
|
17
|
-
type: 'object',
|
|
18
|
-
properties: { name: { type: 'string' } },
|
|
19
|
-
required: ['name'],
|
|
20
|
-
},
|
|
21
|
-
body: WorkflowRunRequestSchema,
|
|
22
|
-
},
|
|
23
|
-
}, async (request, reply) => {
|
|
31
|
+
fastify.post('/workflows/:name/run', { schema: { params: paramsSchema, body: WorkflowRunRequestSchema } }, async (request, reply) => {
|
|
24
32
|
const { name } = request.params;
|
|
25
|
-
const workflow =
|
|
26
|
-
? fastify.cogitator.workflows[name]
|
|
27
|
-
: undefined;
|
|
33
|
+
const workflow = findWorkflow(name);
|
|
28
34
|
if (!workflow) {
|
|
29
|
-
return reply
|
|
30
|
-
error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' },
|
|
31
|
-
});
|
|
35
|
+
return sendError(reply, 404, `Workflow '${name}' not found`, 'NOT_FOUND');
|
|
32
36
|
}
|
|
37
|
+
const abortController = new AbortController();
|
|
38
|
+
onClientDisconnect(reply, () => abortController.abort());
|
|
33
39
|
try {
|
|
34
40
|
const { WorkflowExecutor } = await import('@cogitator-ai/workflows');
|
|
35
41
|
const executor = new WorkflowExecutor(fastify.cogitator.runtime);
|
|
36
|
-
const result = await executor.execute(workflow, request.body?.input,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
const result = await executor.execute(workflow, request.body?.input, {
|
|
43
|
+
...executorOptions(request.body),
|
|
44
|
+
signal: abortController.signal,
|
|
45
|
+
});
|
|
46
|
+
if (result.error) {
|
|
47
|
+
return sendError(reply, 500, `Workflow failed: ${result.error.message}`, 'WORKFLOW_FAILED');
|
|
40
48
|
}
|
|
41
49
|
const response = {
|
|
42
50
|
workflowId: result.workflowId,
|
|
43
51
|
workflowName: result.workflowName,
|
|
44
52
|
state: result.state,
|
|
45
53
|
duration: result.duration,
|
|
46
|
-
nodeResults,
|
|
54
|
+
nodeResults: Object.fromEntries(result.nodeResults),
|
|
47
55
|
};
|
|
48
56
|
return response;
|
|
49
57
|
}
|
|
50
58
|
catch (error) {
|
|
51
|
-
if (error
|
|
52
|
-
return reply
|
|
53
|
-
error: { message: 'Workflows package not installed', code: 'UNIMPLEMENTED' },
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
if (CogitatorError.isCogitatorError(error)) {
|
|
57
|
-
return reply.status(500).send({ error: { message: error.message, code: error.code } });
|
|
59
|
+
if (isModuleNotFound(error)) {
|
|
60
|
+
return sendError(reply, 501, WORKFLOWS_MISSING, 'UNIMPLEMENTED');
|
|
58
61
|
}
|
|
59
|
-
request
|
|
60
|
-
return reply
|
|
61
|
-
.status(500)
|
|
62
|
-
.send({ error: { message: 'Internal server error', code: 'INTERNAL' } });
|
|
62
|
+
return sendRouteError(request, reply, error, 'workflow run error');
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
-
fastify.post('/workflows/:name/stream', {
|
|
66
|
-
schema: {
|
|
67
|
-
params: {
|
|
68
|
-
type: 'object',
|
|
69
|
-
properties: { name: { type: 'string' } },
|
|
70
|
-
required: ['name'],
|
|
71
|
-
},
|
|
72
|
-
body: WorkflowRunRequestSchema,
|
|
73
|
-
},
|
|
74
|
-
}, async (request, reply) => {
|
|
65
|
+
fastify.post('/workflows/:name/stream', { schema: { params: paramsSchema, body: WorkflowRunRequestSchema } }, async (request, reply) => {
|
|
75
66
|
const { name } = request.params;
|
|
76
|
-
const workflow =
|
|
77
|
-
? fastify.cogitator.workflows[name]
|
|
78
|
-
: undefined;
|
|
67
|
+
const workflow = findWorkflow(name);
|
|
79
68
|
if (!workflow) {
|
|
80
|
-
return reply
|
|
81
|
-
error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' },
|
|
82
|
-
});
|
|
69
|
+
return sendError(reply, 404, `Workflow '${name}' not found`, 'NOT_FOUND');
|
|
83
70
|
}
|
|
84
71
|
const writer = new FastifyStreamWriter(reply);
|
|
85
72
|
const messageId = generateId('wf');
|
|
86
|
-
|
|
73
|
+
const abortController = new AbortController();
|
|
74
|
+
onClientDisconnect(reply, () => {
|
|
75
|
+
abortController.abort();
|
|
87
76
|
writer.close();
|
|
88
77
|
});
|
|
89
78
|
try {
|
|
90
79
|
const { WorkflowExecutor } = await import('@cogitator-ai/workflows');
|
|
91
80
|
const executor = new WorkflowExecutor(fastify.cogitator.runtime);
|
|
92
81
|
writer.start(messageId);
|
|
93
|
-
const userOptions = request.body?.options ?? {};
|
|
94
82
|
const result = await executor.execute(workflow, request.body?.input, {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
checkpoint: userOptions.checkpoint,
|
|
83
|
+
...executorOptions(request.body),
|
|
84
|
+
signal: abortController.signal,
|
|
98
85
|
onNodeStart: (node) => {
|
|
99
86
|
writer.workflowEvent('node_started', { nodeName: node, timestamp: Date.now() });
|
|
100
87
|
},
|
|
@@ -108,27 +95,30 @@ export const workflowRoutes = async (fastify) => {
|
|
|
108
95
|
writer.workflowEvent('node_progress', { nodeName: node, progress });
|
|
109
96
|
},
|
|
110
97
|
});
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
98
|
+
if (result.error) {
|
|
99
|
+
writer.error(`Workflow failed: ${result.error.message}`, 'WORKFLOW_FAILED');
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
writer.workflowEvent('workflow_completed', {
|
|
103
|
+
workflowId: result.workflowId,
|
|
104
|
+
duration: result.duration,
|
|
105
|
+
});
|
|
106
|
+
writer.finish(messageId);
|
|
107
|
+
}
|
|
116
108
|
}
|
|
117
109
|
catch (error) {
|
|
118
|
-
if (error
|
|
119
|
-
|
|
110
|
+
if (isModuleNotFound(error)) {
|
|
111
|
+
return sendError(reply, 501, WORKFLOWS_MISSING, 'UNIMPLEMENTED');
|
|
120
112
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
else {
|
|
125
|
-
request.log.error({ err: error }, 'workflow stream error');
|
|
126
|
-
writer.error('Internal server error', 'INTERNAL');
|
|
113
|
+
if (!abortController.signal.aborted) {
|
|
114
|
+
const resolved = resolveError(request, error, 'workflow stream error');
|
|
115
|
+
writer.error(resolved.message, resolved.code);
|
|
127
116
|
}
|
|
128
117
|
}
|
|
129
118
|
finally {
|
|
130
119
|
writer.close();
|
|
131
120
|
}
|
|
121
|
+
return reply;
|
|
132
122
|
});
|
|
133
123
|
};
|
|
134
124
|
//# sourceMappingURL=workflows.js.map
|