@cogitator-ai/koa 0.8.6 → 0.9.0
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 +93 -34
- package/dist/app.d.ts +7 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +31 -14
- package/dist/app.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware/auth.d.ts +6 -0
- package/dist/middleware/auth.d.ts.map +1 -1
- package/dist/middleware/auth.js +17 -2
- package/dist/middleware/auth.js.map +1 -1
- package/dist/middleware/body-parser.d.ts.map +1 -1
- package/dist/middleware/body-parser.js +3 -4
- package/dist/middleware/body-parser.js.map +1 -1
- package/dist/middleware/context.d.ts +5 -0
- package/dist/middleware/context.d.ts.map +1 -1
- package/dist/middleware/context.js +10 -1
- package/dist/middleware/context.js.map +1 -1
- package/dist/middleware/error-handler.d.ts.map +1 -1
- package/dist/middleware/error-handler.js +16 -36
- package/dist/middleware/error-handler.js.map +1 -1
- package/dist/middleware/index.d.ts +1 -1
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +1 -1
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/unmatched.d.ts +20 -0
- package/dist/middleware/unmatched.d.ts.map +1 -0
- package/dist/middleware/unmatched.js +83 -0
- package/dist/middleware/unmatched.js.map +1 -0
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/agents.js +33 -22
- package/dist/routes/agents.js.map +1 -1
- package/dist/routes/swarms.d.ts.map +1 -1
- package/dist/routes/swarms.js +32 -26
- package/dist/routes/swarms.js.map +1 -1
- package/dist/routes/threads.d.ts.map +1 -1
- package/dist/routes/threads.js +25 -26
- package/dist/routes/threads.js.map +1 -1
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/workflows.js +16 -25
- package/dist/routes/workflows.js.map +1 -1
- package/dist/types.d.ts +66 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/errors.d.ts +14 -6
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +29 -12
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/request.d.ts +10 -0
- package/dist/utils/request.d.ts.map +1 -1
- package/dist/utils/request.js +14 -0
- package/dist/utils/request.js.map +1 -1
- package/dist/websocket/handler.d.ts +6 -0
- package/dist/websocket/handler.d.ts.map +1 -1
- package/dist/websocket/handler.js +48 -18
- package/dist/websocket/handler.js.map +1 -1
- package/package.json +8 -8
package/dist/routes/workflows.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import Router from '@koa/router';
|
|
2
2
|
import { KoaStreamWriter, setupSSEHeaders } from '../streaming/index.js';
|
|
3
|
-
import { generateId, parseWorkflowRunRequest } from '@cogitator-ai/server-shared';
|
|
3
|
+
import { generateId, parseWorkflowRunRequest, workflowOptionsWithin, } from '@cogitator-ai/server-shared';
|
|
4
4
|
import { getOwn } from '../utils/lookup.js';
|
|
5
|
-
import { isModuleNotFoundError,
|
|
5
|
+
import { describeError, invalidInput, isModuleNotFoundError, notFound, notInstalled, respondWithError, } from '../utils/errors.js';
|
|
6
6
|
import { getRequestBody, onClientDisconnect } from '../utils/request.js';
|
|
7
7
|
import { toWorkflowRunResponse } from '../utils/results.js';
|
|
8
8
|
export function createWorkflowRoutes() {
|
|
@@ -22,14 +22,12 @@ export function createWorkflowRoutes() {
|
|
|
22
22
|
const { name } = ctx.params;
|
|
23
23
|
const workflow = getOwn(workflows, name);
|
|
24
24
|
if (!workflow) {
|
|
25
|
-
ctx
|
|
26
|
-
ctx.body = { error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' } };
|
|
25
|
+
notFound(ctx, `Workflow '${name}' not found`);
|
|
27
26
|
return;
|
|
28
27
|
}
|
|
29
28
|
const parsed = parseWorkflowRunRequest(getRequestBody(ctx));
|
|
30
29
|
if (!parsed.ok) {
|
|
31
|
-
ctx.
|
|
32
|
-
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
30
|
+
invalidInput(ctx, parsed.message);
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
33
|
const abortController = new AbortController();
|
|
@@ -38,15 +36,13 @@ export function createWorkflowRoutes() {
|
|
|
38
36
|
const { WorkflowExecutor } = await import('@cogitator-ai/workflows');
|
|
39
37
|
const executor = new WorkflowExecutor(runtime);
|
|
40
38
|
const result = await executor.execute(workflow, parsed.value.input, {
|
|
41
|
-
...parsed.value.options,
|
|
39
|
+
...workflowOptionsWithin(parsed.value.options ?? {}, ctx.state.cogitator.limits),
|
|
42
40
|
signal: abortController.signal,
|
|
43
41
|
});
|
|
44
42
|
if (abortController.signal.aborted)
|
|
45
43
|
return;
|
|
46
44
|
if (result.error) {
|
|
47
|
-
|
|
48
|
-
ctx.status = status;
|
|
49
|
-
ctx.body = body;
|
|
45
|
+
respondWithError(ctx, result.error, 'Workflow run error');
|
|
50
46
|
return;
|
|
51
47
|
}
|
|
52
48
|
ctx.body = toWorkflowRunResponse(result);
|
|
@@ -55,13 +51,10 @@ export function createWorkflowRoutes() {
|
|
|
55
51
|
if (abortController.signal.aborted)
|
|
56
52
|
return;
|
|
57
53
|
if (isModuleNotFoundError(error)) {
|
|
58
|
-
ctx
|
|
59
|
-
ctx.body = { error: { message: 'Workflows package not installed', code: 'UNIMPLEMENTED' } };
|
|
54
|
+
notInstalled(ctx, 'Workflows');
|
|
60
55
|
return;
|
|
61
56
|
}
|
|
62
|
-
|
|
63
|
-
ctx.status = status;
|
|
64
|
-
ctx.body = body;
|
|
57
|
+
respondWithError(ctx, error, 'Workflow run error');
|
|
65
58
|
}
|
|
66
59
|
});
|
|
67
60
|
router.post('/workflows/:name/stream', async (ctx) => {
|
|
@@ -69,14 +62,12 @@ export function createWorkflowRoutes() {
|
|
|
69
62
|
const { name } = ctx.params;
|
|
70
63
|
const workflow = getOwn(workflows, name);
|
|
71
64
|
if (!workflow) {
|
|
72
|
-
ctx
|
|
73
|
-
ctx.body = { error: { message: `Workflow '${name}' not found`, code: 'NOT_FOUND' } };
|
|
65
|
+
notFound(ctx, `Workflow '${name}' not found`);
|
|
74
66
|
return;
|
|
75
67
|
}
|
|
76
68
|
const parsed = parseWorkflowRunRequest(getRequestBody(ctx));
|
|
77
69
|
if (!parsed.ok) {
|
|
78
|
-
ctx.
|
|
79
|
-
ctx.body = { error: { message: parsed.message, code: 'INVALID_INPUT' } };
|
|
70
|
+
invalidInput(ctx, parsed.message);
|
|
80
71
|
return;
|
|
81
72
|
}
|
|
82
73
|
setupSSEHeaders(ctx);
|
|
@@ -94,7 +85,7 @@ export function createWorkflowRoutes() {
|
|
|
94
85
|
const executor = new WorkflowExecutor(runtime);
|
|
95
86
|
writer.start(messageId);
|
|
96
87
|
const result = await executor.execute(workflow, parsed.value.input, {
|
|
97
|
-
...parsed.value.options,
|
|
88
|
+
...workflowOptionsWithin(parsed.value.options ?? {}, ctx.state.cogitator.limits),
|
|
98
89
|
signal: abortController.signal,
|
|
99
90
|
onNodeStart: (node) => {
|
|
100
91
|
writer.workflowEvent('node_started', { nodeName: node, timestamp: Date.now() });
|
|
@@ -105,7 +96,7 @@ export function createWorkflowRoutes() {
|
|
|
105
96
|
onNodeError: (node, error) => {
|
|
106
97
|
writer.workflowEvent('node_error', {
|
|
107
98
|
nodeName: node,
|
|
108
|
-
error:
|
|
99
|
+
error: describeError(error, `Workflow node ${node} error`).message,
|
|
109
100
|
});
|
|
110
101
|
},
|
|
111
102
|
onNodeProgress: (node, progress) => {
|
|
@@ -115,8 +106,8 @@ export function createWorkflowRoutes() {
|
|
|
115
106
|
if (abortController.signal.aborted)
|
|
116
107
|
return;
|
|
117
108
|
if (result.error) {
|
|
118
|
-
const
|
|
119
|
-
writer.error(
|
|
109
|
+
const failure = describeError(result.error, 'Workflow stream error');
|
|
110
|
+
writer.error(failure.message, failure.code);
|
|
120
111
|
return;
|
|
121
112
|
}
|
|
122
113
|
writer.workflowEvent('workflow_completed', {
|
|
@@ -132,8 +123,8 @@ export function createWorkflowRoutes() {
|
|
|
132
123
|
writer.error('Workflows package not installed', 'UNIMPLEMENTED');
|
|
133
124
|
}
|
|
134
125
|
else {
|
|
135
|
-
const
|
|
136
|
-
writer.error(
|
|
126
|
+
const failure = describeError(error, 'Workflow stream error');
|
|
127
|
+
writer.error(failure.message, failure.code);
|
|
137
128
|
}
|
|
138
129
|
}
|
|
139
130
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workflows.js","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,
|
|
1
|
+
{"version":3,"file":"workflows.js","sourceRoot":"","sources":["../../src/routes/workflows.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACzE,OAAO,EACL,UAAU,EACV,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EACL,aAAa,EACb,YAAY,EACZ,qBAAqB,EACrB,QAAQ,EACR,YAAY,EACZ,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE5D,MAAM,UAAU,oBAAoB;IAClC,MAAM,MAAM,GAAG,IAAI,MAAM,EAAkB,CAAC;IAE5C,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,GAAG,EAAE,EAAE;QAC/B,MAAM,EAAE,SAAS,EAAE,GAAG,GAAG,CAA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AQ,EAAE,MAAM,CAAC,QAAQ;aAC1B,CAAC,CAAC;YACH,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,eAAe,CAAC,MAAM,CAAC,OAAO;gBAAE,OAAO;YAC3C,IAAI,qBAAqB,CAAC,KAAK,CAAC,EAAE,CAAC;gBACjC,MAAM,CAAC,KAAK,CAAC,iCAAiC,EAAE,eAAe,CAAC,CAAC;YACnE,CAAC;iBAAM,CAAC;gBACN,MAAM,OAAO,GAAG,aAAa,CAAC,KAAK,EAAE,uBAAuB,CAAC,CAAC;gBAC9D,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;YAC9C,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,KAAK,EAAE,CAAC;QACjB,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'http';
|
|
2
2
|
import type { Context } from 'koa';
|
|
3
3
|
import type { Cogitator, Agent } from '@cogitator-ai/core';
|
|
4
|
-
import type { AgentRunResponse, ContextPolicy, PendingApproval, ReadinessCheckStatus, ReadinessResponse, ThreadMessageRole, WorkflowRunRequestBody } from '@cogitator-ai/server-shared';
|
|
4
|
+
import type { AgentRunResponse, AnonymousPolicy, AuthorizeThread, ContextPolicy, PendingApproval, ReadinessCheckStatus, ReadinessResponse, RunLimits, ThreadMessageRole, WorkflowRunRequestBody } from '@cogitator-ai/server-shared';
|
|
5
5
|
import type { Message, ToolCall, ToolResult, RunResult, Workflow, WorkflowResult, WorkflowState, SwarmConfig, SwarmResult, SwarmRunOptions, StreamingWorkflowEvent, SwarmEvent, SwarmMessage, ToolApprovalDecision } from '@cogitator-ai/types';
|
|
6
|
-
export type { Message, ToolCall, ToolResult, RunResult, Workflow, WorkflowResult, WorkflowState, SwarmConfig, SwarmResult, SwarmRunOptions, StreamingWorkflowEvent, SwarmEvent, SwarmMessage, ToolApprovalDecision, PendingApproval, ReadinessResponse, ReadinessCheckStatus, };
|
|
6
|
+
export type { Message, ToolCall, ToolResult, RunResult, Workflow, WorkflowResult, WorkflowState, SwarmConfig, SwarmResult, SwarmRunOptions, StreamingWorkflowEvent, SwarmEvent, SwarmMessage, ToolApprovalDecision, PendingApproval, ReadinessResponse, ReadinessCheckStatus, RunLimits, AnonymousPolicy, AuthorizeThread, };
|
|
7
7
|
export interface AuthContext {
|
|
8
8
|
userId?: string;
|
|
9
9
|
roles?: string[];
|
|
@@ -12,12 +12,27 @@ export interface AuthContext {
|
|
|
12
12
|
}
|
|
13
13
|
export type AuthFunction = (ctx: Context) => Promise<AuthContext | undefined> | AuthContext | undefined;
|
|
14
14
|
export type WebSocketAuthFunction = (req: IncomingMessage) => Promise<AuthContext | undefined> | AuthContext | undefined;
|
|
15
|
+
/** Builds the `deps` an agent run of a request passes to its tools (`RunOptions.deps`) */
|
|
16
|
+
export type DepsFunction = (ctx: Context) => unknown | Promise<unknown>;
|
|
17
|
+
/** Builds the `deps` an agent run on a WebSocket connection passes to its tools */
|
|
18
|
+
export type WebSocketDepsFunction = (req: IncomingMessage) => unknown | Promise<unknown>;
|
|
15
19
|
export interface WebSocketConfig {
|
|
16
20
|
path?: string;
|
|
17
21
|
pingInterval?: number;
|
|
18
22
|
pingTimeout?: number;
|
|
19
23
|
maxPayloadSize?: number;
|
|
20
24
|
auth?: WebSocketAuthFunction;
|
|
25
|
+
/**
|
|
26
|
+
* Whether a connection without a `userId` from `auth` may open: `'allow'` lets it in,
|
|
27
|
+
* `'deny'` refuses the upgrade with `401`. Default: the `anonymous` of the route context,
|
|
28
|
+
* else `'allow'`
|
|
29
|
+
*/
|
|
30
|
+
anonymous?: AnonymousPolicy;
|
|
31
|
+
/**
|
|
32
|
+
* Builds the `deps` of the agent runs and resumes on a connection, from its upgrade request,
|
|
33
|
+
* called for each run. Deps never come from a message.
|
|
34
|
+
*/
|
|
35
|
+
deps?: WebSocketDepsFunction;
|
|
21
36
|
}
|
|
22
37
|
export type { SwaggerConfig } from '@cogitator-ai/server-shared';
|
|
23
38
|
import type { SwaggerConfig } from '@cogitator-ai/server-shared';
|
|
@@ -47,6 +62,34 @@ export interface CogitatorAppOptions {
|
|
|
47
62
|
* `assistant`. A `system` message is read by the model as operator instructions.
|
|
48
63
|
*/
|
|
49
64
|
threadMessageRoles?: readonly ThreadMessageRole[];
|
|
65
|
+
/**
|
|
66
|
+
* Caps on what a request may ask a run for. A swarm run times out after at most
|
|
67
|
+
* `swarmTimeout` ms and a workflow run takes at most `workflowMaxIterations` iterations and
|
|
68
|
+
* `workflowMaxConcurrency` nodes at once, over HTTP and SSE (WebSocket runs take the
|
|
69
|
+
* `limits` of the `RouteContext` given to `setupWebSocket`). A request that asks for more is
|
|
70
|
+
* held to the cap, one that asks for nothing gets it. Each limit must be a positive integer,
|
|
71
|
+
* or creating the router throws.
|
|
72
|
+
*/
|
|
73
|
+
limits?: RunLimits;
|
|
74
|
+
/**
|
|
75
|
+
* Whether a caller without a `userId` may use the API: `'allow'` (the default) lets them
|
|
76
|
+
* in, `'deny'` answers `401 UNAUTHORIZED`. `/health`, `/ready`, `/openapi.json` and `/docs`
|
|
77
|
+
* stay open either way.
|
|
78
|
+
*/
|
|
79
|
+
anonymous?: AnonymousPolicy;
|
|
80
|
+
/**
|
|
81
|
+
* Decides whether a caller may touch a thread, on top of the ownership check of core:
|
|
82
|
+
* called with what `auth` returned and the thread a request names, on agent runs, streams
|
|
83
|
+
* and resumes, swarm runs and the thread routes. `false` answers `403 THREAD_FORBIDDEN` and
|
|
84
|
+
* starts no run.
|
|
85
|
+
*/
|
|
86
|
+
authorizeThread?: AuthorizeThread<AuthContext>;
|
|
87
|
+
/**
|
|
88
|
+
* Builds the `deps` of the agent runs and resumes of a request, which tools get as
|
|
89
|
+
* `context.deps` and the model never sees: a database handle, a tenant, a client. Deps
|
|
90
|
+
* never come from the request body. WebSocket runs use `WebSocketConfig.deps`.
|
|
91
|
+
*/
|
|
92
|
+
deps?: DepsFunction;
|
|
50
93
|
}
|
|
51
94
|
export interface CogitatorState {
|
|
52
95
|
cogitator: RouteContext;
|
|
@@ -65,6 +108,25 @@ export interface RouteContext {
|
|
|
65
108
|
acceptContext?: ContextPolicy;
|
|
66
109
|
/** Roles clients may add to threads. Default: `user` and `assistant` */
|
|
67
110
|
threadMessageRoles?: readonly ThreadMessageRole[];
|
|
111
|
+
/** Caps on what a request may ask a run for, see `CogitatorAppOptions.limits`. Default: none */
|
|
112
|
+
limits?: RunLimits;
|
|
113
|
+
/**
|
|
114
|
+
* Who may touch a thread, see `CogitatorAppOptions.authorizeThread`. Default: every caller
|
|
115
|
+
* may touch the threads core lets them
|
|
116
|
+
*/
|
|
117
|
+
authorizeThread?: AuthorizeThread<AuthContext>;
|
|
118
|
+
/**
|
|
119
|
+
* Whether a caller without a `userId` may use the API, see `CogitatorAppOptions.anonymous`.
|
|
120
|
+
* `setupWebSocket` applies it to the handshake when `WebSocketConfig.anonymous` is not set.
|
|
121
|
+
* Default: `'allow'`
|
|
122
|
+
*/
|
|
123
|
+
anonymous?: AnonymousPolicy;
|
|
124
|
+
/**
|
|
125
|
+
* Builds the `deps` of HTTP agent runs, see `CogitatorAppOptions.deps`. It gets the Koa
|
|
126
|
+
* `Context` of a request, which a WebSocket upgrade does not have, so `setupWebSocket` does
|
|
127
|
+
* not use it: WebSocket runs take `WebSocketConfig.deps`. Default: none
|
|
128
|
+
*/
|
|
129
|
+
deps?: DepsFunction;
|
|
68
130
|
}
|
|
69
131
|
export interface AgentListResponse {
|
|
70
132
|
agents: Array<{
|
|
@@ -90,6 +152,8 @@ export interface ThreadResponse {
|
|
|
90
152
|
metadata?: Record<string, unknown>;
|
|
91
153
|
createdAt: number;
|
|
92
154
|
updatedAt: number;
|
|
155
|
+
/** Pass as `cursor` to read the page before this one, absent at the thread's start */
|
|
156
|
+
nextCursor?: string;
|
|
93
157
|
}
|
|
94
158
|
export interface AddMessageRequest {
|
|
95
159
|
role: 'user' | 'assistant' | 'system';
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EACV,gBAAgB,EAChB,aAAa,EACb,eAAe,EACf,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,sBAAsB,EACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EACV,OAAO,EACP,QAAQ,EACR,UAAU,EACV,SAAS,EACT,QAAQ,EACR,cAAc,EACd,aAAa,EACb,WAAW,EACX,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,UAAU,EACV,YAAY,EACZ,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,OAAO,EACP,QAAQ,EACR,UAAU,EACV,SAAS,EACT,QAAQ,EACR,cAAc,EACd,aAAa,EACb,WAAW,EACX,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,UAAU,EACV,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,iBAAiB,EACjB,oBAAoB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAA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|
2
|
-
|
|
3
|
-
status: number;
|
|
4
|
-
body: ErrorResponse;
|
|
5
|
-
}
|
|
1
|
+
import type { Context } from 'koa';
|
|
2
|
+
import { type HttpErrorDescription } from '@cogitator-ai/server-shared';
|
|
6
3
|
export declare function isModuleNotFoundError(error: unknown): boolean;
|
|
7
|
-
|
|
4
|
+
/**
|
|
5
|
+
* How the server answers `error`: {@link describeHttpError}, or {@link INTERNAL_ERROR} for an
|
|
6
|
+
* error the client is not told about, which is logged under `label`
|
|
7
|
+
*/
|
|
8
|
+
export declare function describeError(error: unknown, label: string): HttpErrorDescription;
|
|
9
|
+
/** Answers an error with the error envelope and its `Retry-After` */
|
|
10
|
+
export declare function sendError(ctx: Context, error: HttpErrorDescription): void;
|
|
11
|
+
/** Answers `error` as {@link describeError} describes it */
|
|
12
|
+
export declare function respondWithError(ctx: Context, error: unknown, label: string): void;
|
|
13
|
+
export declare function invalidInput(ctx: Context, message: string): void;
|
|
14
|
+
export declare function notFound(ctx: Context, message: string): void;
|
|
15
|
+
export declare function notInstalled(ctx: Context, label: 'Swarms' | 'Workflows'): void;
|
|
8
16
|
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACnC,OAAO,EAKL,KAAK,oBAAoB,EAC1B,MAAM,6BAA6B,CAAC;AAErC,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAI7D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,oBAAoB,CAKjF;AAED,qEAAqE;AACrE,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,oBAAoB,GAAG,IAAI,CAIzE;AAED,4DAA4D;AAC5D,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAElF;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAEhE;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAE5D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,GAAG,WAAW,GAAG,IAAI,CAE9E"}
|
package/dist/utils/errors.js
CHANGED
|
@@ -1,21 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INTERNAL_ERROR, describeHttpError, errorEnvelope, errorHeaders, } from '@cogitator-ai/server-shared';
|
|
2
2
|
export function isModuleNotFoundError(error) {
|
|
3
3
|
if (typeof error !== 'object' || error === null)
|
|
4
4
|
return false;
|
|
5
5
|
const code = error.code;
|
|
6
6
|
return code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND';
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
/**
|
|
9
|
+
* How the server answers `error`: {@link describeHttpError}, or {@link INTERNAL_ERROR} for an
|
|
10
|
+
* error the client is not told about, which is logged under `label`
|
|
11
|
+
*/
|
|
12
|
+
export function describeError(error, label) {
|
|
13
|
+
const described = describeHttpError(error);
|
|
14
|
+
if (described)
|
|
15
|
+
return described;
|
|
15
16
|
console.error(`[CogitatorKoa] ${label}:`, error);
|
|
16
|
-
return
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
return INTERNAL_ERROR;
|
|
18
|
+
}
|
|
19
|
+
/** Answers an error with the error envelope and its `Retry-After` */
|
|
20
|
+
export function sendError(ctx, error) {
|
|
21
|
+
ctx.status = error.status;
|
|
22
|
+
ctx.set(errorHeaders(error));
|
|
23
|
+
ctx.body = errorEnvelope(error);
|
|
24
|
+
}
|
|
25
|
+
/** Answers `error` as {@link describeError} describes it */
|
|
26
|
+
export function respondWithError(ctx, error, label) {
|
|
27
|
+
sendError(ctx, describeError(error, label));
|
|
28
|
+
}
|
|
29
|
+
export function invalidInput(ctx, message) {
|
|
30
|
+
sendError(ctx, { status: 400, code: 'INVALID_INPUT', message });
|
|
31
|
+
}
|
|
32
|
+
export function notFound(ctx, message) {
|
|
33
|
+
sendError(ctx, { status: 404, code: 'NOT_FOUND', message });
|
|
34
|
+
}
|
|
35
|
+
export function notInstalled(ctx, label) {
|
|
36
|
+
sendError(ctx, { status: 501, code: 'UNIMPLEMENTED', message: `${label} package not installed` });
|
|
20
37
|
}
|
|
21
38
|
//# sourceMappingURL=errors.js.map
|
package/dist/utils/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,aAAa,EACb,YAAY,GAEb,MAAM,6BAA6B,CAAC;AAErC,MAAM,UAAU,qBAAqB,CAAC,KAAc;IAClD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,IAAI,GAAI,KAA+B,CAAC,IAAI,CAAC;IACnD,OAAO,IAAI,KAAK,kBAAkB,IAAI,IAAI,KAAK,sBAAsB,CAAC;AACxE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc,EAAE,KAAa;IACzD,MAAM,SAAS,GAAG,iBAA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+
import { type HttpErrorDescription } from '@cogitator-ai/server-shared';
|
|
2
3
|
export declare function getRequestBody(ctx: Context): unknown;
|
|
3
4
|
export declare function onClientDisconnect(ctx: Context, listener: () => void): void;
|
|
5
|
+
/**
|
|
6
|
+
* The refusal of a request that names a thread the router's `authorizeThread` keeps the
|
|
7
|
+
* caller from, none when it may go on
|
|
8
|
+
*/
|
|
9
|
+
export declare function refuseThread(ctx: Context, threadId: string | undefined): Promise<HttpErrorDescription | undefined>;
|
|
10
|
+
/** The `deps` a run of this request gets from the router's `deps` hook, none without it */
|
|
11
|
+
export declare function runDeps(ctx: Context): Promise<{
|
|
12
|
+
deps?: unknown;
|
|
13
|
+
}>;
|
|
4
14
|
//# sourceMappingURL=request.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/utils/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/utils/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACnC,OAAO,EAAyB,KAAK,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAG/F,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAEpD;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI,CAK3E;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAG3C;AAED,2FAA2F;AAC3F,wBAAsB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC;IAAE,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAGvE"}
|
package/dist/utils/request.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { authorizeThreadAccess } from '@cogitator-ai/server-shared';
|
|
1
2
|
export function getRequestBody(ctx) {
|
|
2
3
|
return ctx.request.body;
|
|
3
4
|
}
|
|
@@ -8,4 +9,17 @@ export function onClientDisconnect(ctx, listener) {
|
|
|
8
9
|
listener();
|
|
9
10
|
});
|
|
10
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* The refusal of a request that names a thread the router's `authorizeThread` keeps the
|
|
14
|
+
* caller from, none when it may go on
|
|
15
|
+
*/
|
|
16
|
+
export function refuseThread(ctx, threadId) {
|
|
17
|
+
const state = ctx.state;
|
|
18
|
+
return authorizeThreadAccess(state.cogitator.authorizeThread, state.auth, threadId);
|
|
19
|
+
}
|
|
20
|
+
/** The `deps` a run of this request gets from the router's `deps` hook, none without it */
|
|
21
|
+
export async function runDeps(ctx) {
|
|
22
|
+
const deps = ctx.state.cogitator.deps;
|
|
23
|
+
return deps ? { deps: await deps(ctx) } : {};
|
|
24
|
+
}
|
|
11
25
|
//# sourceMappingURL=request.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/utils/request.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/utils/request.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAA6B,MAAM,6BAA6B,CAAC;AAG/F,MAAM,UAAU,cAAc,CAAC,GAAY;IACzC,OAAQ,GAAG,CAAC,OAAmD,CAAC,IAAI,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,GAAY,EAAE,QAAoB;IACnE,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;IACpB,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;QACrB,IAAI,CAAC,GAAG,CAAC,gBAAgB;YAAE,QAAQ,EAAE,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC1B,GAAY,EACZ,QAA4B;IAE5B,MAAM,KAAK,GAAG,GAAG,CAAC,KAAuB,CAAC;IAC1C,OAAO,qBAAqB,CAAC,KAAK,CAAC,SAAS,CAAC,eAAe,EAAE,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AACtF,CAAC;AAED,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,GAAY;IACxC,MAAM,IAAI,GAAI,GAAG,CAAC,KAAwB,CAAC,SAAS,CAAC,IAAI,CAAC;IAC1D,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/C,CAAC"}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { type Server as HttpServer } from 'http';
|
|
2
2
|
import type { RouteContext, WebSocketConfig } from '../types.js';
|
|
3
3
|
type WebSocketServerType = import('ws').WebSocketServer;
|
|
4
|
+
/**
|
|
5
|
+
* Serves WebSocket runs on `server`, with the agents, workflows, swarms, `limits` and
|
|
6
|
+
* `authorizeThread` of `ctx`, and its `anonymous` policy when `config` sets none.
|
|
7
|
+
*
|
|
8
|
+
* @throws Error when `ctx.limits` holds a limit that is not a positive integer
|
|
9
|
+
*/
|
|
4
10
|
export declare function setupWebSocket(server: HttpServer, ctx: RouteContext, config?: WebSocketConfig): Promise<WebSocketServerType | null>;
|
|
5
11
|
export {};
|
|
6
12
|
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/websocket/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,MAAM,IAAI,UAAU,EAAE,MAAM,MAAM,CAAC;AAErF,OAAO,KAAK,EAEV,YAAY,EAEZ,eAAe,EAIhB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/websocket/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,MAAM,IAAI,UAAU,EAAE,MAAM,MAAM,CAAC;AAErF,OAAO,KAAK,EAEV,YAAY,EAEZ,eAAe,EAIhB,MAAM,aAAa,CAAC;AAqBrB,KAAK,mBAAmB,GAAG,OAAO,IAAI,EAAE,eAAe,CAAC;AAgCxD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,UAAU,EAClB,GAAG,EAAE,YAAY,EACjB,MAAM,GAAE,eAAoB,GAC3B,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAwDrC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { STATUS_CODES } from 'http';
|
|
2
|
-
import { isJsonObject, parseResumeRequest, parseRunRequest, toAgentRunResponse, toAgentToolCall, withSwarm, } from '@cogitator-ai/server-shared';
|
|
2
|
+
import { admitCaller, authorizeThreadAccess, checkRunLimits, isJsonObject, parseResumeRequest, parseRunRequest, swarmTimeoutWithin, toAgentRunResponse, toAgentToolCall, withSwarm, workflowOptionsWithin, } from '@cogitator-ai/server-shared';
|
|
3
3
|
import { getOwn } from '../utils/lookup.js';
|
|
4
|
-
import {
|
|
4
|
+
import { describeError, isModuleNotFoundError } from '../utils/errors.js';
|
|
5
5
|
import { toSwarmRunResponse, toWorkflowRunResponse } from '../utils/results.js';
|
|
6
6
|
const WS_OPEN = 1;
|
|
7
7
|
const DEFAULT_PATH = '/ws';
|
|
@@ -13,7 +13,14 @@ function isRunType(value) {
|
|
|
13
13
|
}
|
|
14
14
|
class ClientFacingError extends Error {
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Serves WebSocket runs on `server`, with the agents, workflows, swarms, `limits` and
|
|
18
|
+
* `authorizeThread` of `ctx`, and its `anonymous` policy when `config` sets none.
|
|
19
|
+
*
|
|
20
|
+
* @throws Error when `ctx.limits` holds a limit that is not a positive integer
|
|
21
|
+
*/
|
|
16
22
|
export async function setupWebSocket(server, ctx, config = {}) {
|
|
23
|
+
checkRunLimits(ctx.limits);
|
|
17
24
|
let wsModule;
|
|
18
25
|
try {
|
|
19
26
|
wsModule = await import('ws');
|
|
@@ -27,6 +34,7 @@ export async function setupWebSocket(server, ctx, config = {}) {
|
|
|
27
34
|
const path = config.path ?? DEFAULT_PATH;
|
|
28
35
|
const pingInterval = config.pingInterval ?? DEFAULT_PING_INTERVAL;
|
|
29
36
|
const pingTimeout = config.pingTimeout ?? pingInterval;
|
|
37
|
+
const anonymous = config.anonymous ?? ctx.anonymous;
|
|
30
38
|
const wss = new wsModule.WebSocketServer({
|
|
31
39
|
noServer: true,
|
|
32
40
|
maxPayload: config.maxPayloadSize ?? DEFAULT_MAX_PAYLOAD,
|
|
@@ -40,7 +48,7 @@ export async function setupWebSocket(server, ctx, config = {}) {
|
|
|
40
48
|
}
|
|
41
49
|
const onSocketError = () => socket.destroy();
|
|
42
50
|
socket.on('error', onSocketError);
|
|
43
|
-
void authorize(config.auth, req).then((outcome) => {
|
|
51
|
+
void authorize(config.auth, anonymous, req).then((outcome) => {
|
|
44
52
|
if (!outcome.ok) {
|
|
45
53
|
rejectUpgrade(socket, outcome.status);
|
|
46
54
|
return;
|
|
@@ -59,7 +67,9 @@ export async function setupWebSocket(server, ctx, config = {}) {
|
|
|
59
67
|
server.off('upgrade', onUpgrade);
|
|
60
68
|
});
|
|
61
69
|
wss.on('connection', (ws, req) => {
|
|
62
|
-
|
|
70
|
+
const deps = config.deps;
|
|
71
|
+
const connection = { auth: authResults.get(req), ...(deps && { deps: () => deps(req) }) };
|
|
72
|
+
handleConnection(ws, ctx, connection, pingInterval, pingTimeout);
|
|
63
73
|
});
|
|
64
74
|
return wss;
|
|
65
75
|
}
|
|
@@ -71,11 +81,11 @@ function getPathname(req) {
|
|
|
71
81
|
return '';
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
|
-
async function authorize(auth, req) {
|
|
75
|
-
if (!auth)
|
|
76
|
-
return { ok: true, auth: undefined };
|
|
84
|
+
async function authorize(auth, anonymous, req) {
|
|
77
85
|
try {
|
|
78
|
-
|
|
86
|
+
const caller = auth ? await auth(req) : undefined;
|
|
87
|
+
const refusal = admitCaller(caller?.userId, anonymous);
|
|
88
|
+
return refusal ? { ok: false, status: refusal.status } : { ok: true, auth: caller };
|
|
79
89
|
}
|
|
80
90
|
catch (error) {
|
|
81
91
|
const status = error?.status;
|
|
@@ -97,8 +107,9 @@ function rejectUpgrade(socket, status) {
|
|
|
97
107
|
}
|
|
98
108
|
socket.destroy();
|
|
99
109
|
}
|
|
100
|
-
function handleConnection(ws, ctx,
|
|
101
|
-
const
|
|
110
|
+
function handleConnection(ws, ctx, connection, pingInterval, pingTimeout) {
|
|
111
|
+
const { auth, deps } = connection;
|
|
112
|
+
const state = { ...(auth && { auth }), ...(deps && { deps }) };
|
|
102
113
|
let pongTimer;
|
|
103
114
|
const heartbeat = setInterval(() => {
|
|
104
115
|
if (pongTimer)
|
|
@@ -206,7 +217,10 @@ async function handleRun(ws, id, rawPayload, ctx, state) {
|
|
|
206
217
|
sendResponse(ws, { type: 'error', id, error: payload });
|
|
207
218
|
return;
|
|
208
219
|
}
|
|
209
|
-
await runOverSocket(ws, id, state, 'WebSocket run error', (signal, emit) =>
|
|
220
|
+
await runOverSocket(ws, id, state, 'WebSocket run error', async (signal, emit) => {
|
|
221
|
+
await authorizeThread(ctx, state, payload.threadId);
|
|
222
|
+
return executeRun(payload, ctx, state, signal, emit);
|
|
223
|
+
});
|
|
210
224
|
}
|
|
211
225
|
async function handleResume(ws, id, rawPayload, ctx, state) {
|
|
212
226
|
const payload = parseResumePayload(rawPayload);
|
|
@@ -214,7 +228,10 @@ async function handleResume(ws, id, rawPayload, ctx, state) {
|
|
|
214
228
|
sendResponse(ws, { type: 'error', id, error: payload });
|
|
215
229
|
return;
|
|
216
230
|
}
|
|
217
|
-
await runOverSocket(ws, id, state, 'WebSocket resume error', (signal, emit) =>
|
|
231
|
+
await runOverSocket(ws, id, state, 'WebSocket resume error', async (signal, emit) => {
|
|
232
|
+
await authorizeThread(ctx, state, payload.threadId);
|
|
233
|
+
return executeResume(payload, ctx, state, signal, emit);
|
|
234
|
+
});
|
|
218
235
|
}
|
|
219
236
|
async function runOverSocket(ws, id, state, label, execute) {
|
|
220
237
|
if (state.abortController) {
|
|
@@ -243,8 +260,7 @@ async function runOverSocket(ws, id, state, label, execute) {
|
|
|
243
260
|
sendResponse(ws, { type: 'error', id, error: error.message });
|
|
244
261
|
}
|
|
245
262
|
else {
|
|
246
|
-
|
|
247
|
-
sendResponse(ws, { type: 'error', id, error: body.error.message });
|
|
263
|
+
sendResponse(ws, { type: 'error', id, error: describeError(error, label).message });
|
|
248
264
|
}
|
|
249
265
|
}
|
|
250
266
|
finally {
|
|
@@ -253,6 +269,16 @@ async function runOverSocket(ws, id, state, label, execute) {
|
|
|
253
269
|
}
|
|
254
270
|
}
|
|
255
271
|
}
|
|
272
|
+
/** Refuses a run on a thread the `authorizeThread` of `ctx` keeps the connection's caller from */
|
|
273
|
+
async function authorizeThread(ctx, state, threadId) {
|
|
274
|
+
const refusal = await authorizeThreadAccess(ctx.authorizeThread, state.auth, threadId);
|
|
275
|
+
if (refusal)
|
|
276
|
+
throw new ClientFacingError(refusal.message);
|
|
277
|
+
}
|
|
278
|
+
/** The `deps` an agent run on the connection gets, none without `WebSocketConfig.deps` */
|
|
279
|
+
async function runDeps(state) {
|
|
280
|
+
return state.deps ? { deps: await state.deps() } : {};
|
|
281
|
+
}
|
|
256
282
|
function agentStreamCallbacks(emit) {
|
|
257
283
|
return {
|
|
258
284
|
onToken: (token) => emit({ type: 'token', delta: token }),
|
|
@@ -261,12 +287,13 @@ function agentStreamCallbacks(emit) {
|
|
|
261
287
|
onToolResult: (toolResult) => emit({ type: 'tool-result', ...toolResult }),
|
|
262
288
|
};
|
|
263
289
|
}
|
|
264
|
-
async function executeResume(payload, ctx,
|
|
290
|
+
async function executeResume(payload, ctx, state, signal, emit) {
|
|
265
291
|
const agent = getOwn(ctx.agents, payload.name);
|
|
266
292
|
if (!agent)
|
|
267
293
|
throw new ClientFacingError(`Agent '${payload.name}' not found`);
|
|
268
294
|
const result = await ctx.runtime.resume(agent, payload.threadId, {
|
|
269
|
-
userId,
|
|
295
|
+
userId: state.auth?.userId,
|
|
296
|
+
...(await runDeps(state)),
|
|
270
297
|
decisions: payload.decisions,
|
|
271
298
|
defaultDecision: payload.defaultDecision,
|
|
272
299
|
stream: true,
|
|
@@ -275,7 +302,8 @@ async function executeResume(payload, ctx, userId, signal, emit) {
|
|
|
275
302
|
});
|
|
276
303
|
return toAgentRunResponse(result);
|
|
277
304
|
}
|
|
278
|
-
async function executeRun(payload, ctx,
|
|
305
|
+
async function executeRun(payload, ctx, state, signal, emit) {
|
|
306
|
+
const userId = state.auth?.userId;
|
|
279
307
|
switch (payload.type) {
|
|
280
308
|
case 'agent': {
|
|
281
309
|
const agent = getOwn(ctx.agents, payload.name);
|
|
@@ -286,6 +314,7 @@ async function executeRun(payload, ctx, userId, signal, emit) {
|
|
|
286
314
|
context: payload.context,
|
|
287
315
|
threadId: payload.threadId,
|
|
288
316
|
userId,
|
|
317
|
+
...(await runDeps(state)),
|
|
289
318
|
stream: true,
|
|
290
319
|
signal,
|
|
291
320
|
...agentStreamCallbacks(emit),
|
|
@@ -298,7 +327,7 @@ async function executeRun(payload, ctx, userId, signal, emit) {
|
|
|
298
327
|
throw new ClientFacingError(`Workflow '${payload.name}' not found`);
|
|
299
328
|
const { WorkflowExecutor } = await importOptional(() => import('@cogitator-ai/workflows'), 'Workflows');
|
|
300
329
|
const executor = new WorkflowExecutor(ctx.runtime);
|
|
301
|
-
const result = await executor.execute(workflow, { input: payload.input }, { signal });
|
|
330
|
+
const result = await executor.execute(workflow, { input: payload.input }, { ...workflowOptionsWithin({}, ctx.limits), signal });
|
|
302
331
|
if (result.error)
|
|
303
332
|
throw result.error;
|
|
304
333
|
return toWorkflowRunResponse(result);
|
|
@@ -316,6 +345,7 @@ async function executeRun(payload, ctx, userId, signal, emit) {
|
|
|
316
345
|
input: payload.input,
|
|
317
346
|
context: payload.context,
|
|
318
347
|
threadId: payload.threadId,
|
|
348
|
+
timeout: swarmTimeoutWithin(undefined, ctx.limits),
|
|
319
349
|
userId,
|
|
320
350
|
});
|
|
321
351
|
return toSwarmRunResponse(swarm, result);
|