kizunajs 2.0.0-beta.2 → 2.0.0-beta.21
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 +3 -2
- package/dist/{adapter-CZW-unqX.mjs → adapter-BSqNrxhq.mjs} +522 -104
- package/dist/adapter-C3WYdSfx.d.cts +2754 -0
- package/dist/{adapter-DDR_yo9L.cjs → adapter-ClaFZoab.cjs} +638 -196
- package/dist/adapter-D4bTPElA.d.mts +2754 -0
- package/dist/adapter.cjs +37 -29
- package/dist/adapter.d.cts +3 -5
- package/dist/adapter.d.mts +3 -5
- package/dist/adapter.mjs +5 -5
- package/dist/{error-response-C8Ragjcd.cjs → error-response-C9ymqSU4.cjs} +25 -5
- package/dist/{error-response-C839d8By.mjs → error-response-CNQaKVYd.mjs} +20 -6
- package/dist/{security-scheme-BrszwGD5.d.cts → error-response-NoOGdh8H.d.mts} +151 -24
- package/dist/{security-scheme-D3nb4XKj.d.mts → error-response-YbkRMKno.d.cts} +151 -24
- package/dist/generator.cjs +190 -95
- package/dist/generator.d.cts +189 -28
- package/dist/generator.d.mts +189 -28
- package/dist/generator.mjs +151 -60
- package/dist/index.cjs +159 -84
- package/dist/index.d.cts +193 -253
- package/dist/index.d.mts +193 -253
- package/dist/index.mjs +112 -38
- package/dist/jobs-entry.cjs +12 -11
- package/dist/jobs-entry.d.cts +1 -1
- package/dist/jobs-entry.d.mts +1 -1
- package/dist/jobs-entry.mjs +3 -2
- package/dist/plugin-Dw-yCjTY.cjs +233 -0
- package/dist/{cache-CLN5y0Xp.mjs → plugin-server-BztlE1fz.mjs} +188 -49
- package/dist/{cache-BgohRj0i.cjs → plugin-server-kUsK7thV.cjs} +238 -93
- package/dist/plugin-xxCxibmu.mjs +138 -0
- package/dist/plugin.cjs +8 -72
- package/dist/plugin.d.cts +3 -3
- package/dist/plugin.d.mts +3 -3
- package/dist/plugin.mjs +2 -68
- package/dist/{protected-resource-metadata-CA0v5cjg.d.mts → protected-resource-metadata-B9hMFQ9N.d.cts} +1 -1
- package/dist/{protected-resource-metadata-Cprrv0mt.d.cts → protected-resource-metadata-CfBVsqIk.d.mts} +1 -1
- package/dist/{schemas-C56no6bv.cjs → schemas-DNPVDvkd.cjs} +28 -1
- package/dist/{schemas-CJQcc97N.mjs → schemas-HW2FDsvz.mjs} +23 -2
- package/dist/schemas.cjs +3 -2
- package/dist/schemas.d.cts +24 -3
- package/dist/schemas.d.mts +24 -3
- package/dist/schemas.mjs +3 -3
- package/package.json +1 -1
- package/src/adapter.ts +245 -29
- package/src/api-definition.ts +12 -16
- package/src/api.ts +7 -3
- package/src/brand.ts +29 -0
- package/src/config.ts +51 -10
- package/src/configured.ts +13 -6
- package/src/define-config.ts +118 -39
- package/src/generator.ts +363 -46
- package/src/group-routes.ts +106 -0
- package/src/groups.ts +140 -0
- package/src/handler-pipeline.ts +78 -19
- package/src/index.ts +15 -6
- package/src/job-runner.ts +5 -0
- package/src/job-transport.ts +3 -3
- package/src/job.ts +8 -6
- package/src/jobs-dev-runner.ts +6 -0
- package/src/jobs.ts +61 -8
- package/src/kizuna.ts +56 -33
- package/src/plugin-server.ts +129 -19
- package/src/plugin.ts +269 -127
- package/src/routes.ts +80 -32
- package/src/schemas.ts +1 -0
- package/src/tool-answer.ts +21 -0
- package/src/tool-definitions.ts +166 -0
- package/src/tool-events.ts +90 -18
- package/src/tool-records.ts +11 -3
- package/src/tool-runner.ts +298 -0
- package/src/types.ts +90 -24
- package/src/zod-internals.ts +22 -5
- package/src/zod-meta.ts +7 -2
- package/dist/adapter-BGfMqEkM.d.cts +0 -563
- package/dist/adapter-Ci3cfcoT.d.mts +0 -563
- package/dist/api-definition-BHnh15zD.d.mts +0 -803
- package/dist/api-definition-DQj-ewHy.d.cts +0 -803
- package/dist/error-response-BBGSdgYw.d.mts +0 -67
- package/dist/error-response-Do38sr-9.d.cts +0 -67
- package/dist/plugin-Cz0kn6Lk.d.mts +0 -830
- package/dist/plugin-DhKvx_T0.d.cts +0 -830
- package/src/tags.ts +0 -67
|
@@ -1,41 +1,128 @@
|
|
|
1
|
-
const
|
|
2
|
-
const require_error_response = require("./error-response-
|
|
3
|
-
const
|
|
4
|
-
const require_schemas = require("./schemas-
|
|
1
|
+
const require_plugin = require("./plugin-Dw-yCjTY.cjs");
|
|
2
|
+
const require_error_response = require("./error-response-C9ymqSU4.cjs");
|
|
3
|
+
const require_plugin_server = require("./plugin-server-kUsK7thV.cjs");
|
|
4
|
+
const require_schemas = require("./schemas-DNPVDvkd.cjs");
|
|
5
5
|
let zod = require("zod");
|
|
6
|
-
//#region src/
|
|
7
|
-
const isTagSet = (value) => typeof value === "object" && value !== null && "__brand" in value && value.__brand === "TagSet";
|
|
6
|
+
//#region src/tool-definitions.ts
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
* What a route says about publishing itself, or `undefined` when it says
|
|
9
|
+
* nothing.
|
|
10
|
+
*/
|
|
11
|
+
const toolOptionsOf = (route) => {
|
|
12
|
+
if (route.tool === void 0 || route.tool === false) return void 0;
|
|
13
|
+
return route.tool === true ? {} : route.tool;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Why a route cannot run as a tool, or `undefined` when it can.
|
|
17
|
+
*/
|
|
18
|
+
const toolRefusal = (route) => {
|
|
19
|
+
if (route.hidden === true) return "it is hidden, and a hidden route never publishes";
|
|
20
|
+
if (route.contentType !== void 0 && route.contentType !== "application/json") return `it takes a ${route.contentType} body, and tool input is JSON`;
|
|
21
|
+
if (require_plugin_server.routeStreams(route)) return "it streams, and a tool result is one value";
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The routes a model may call: the ones that declared `tool`, less the ones
|
|
25
|
+
* whose shape a tool call cannot carry.
|
|
26
|
+
*/
|
|
27
|
+
const selectToolRoutes = (routes) => routes.filter(({ route }) => toolOptionsOf(route) !== void 0 && toolRefusal(route) === void 0);
|
|
28
|
+
const describeRoles = (route) => route.roles !== void 0 && route.roles.length > 0 ? `Roles: ${route.roles.join(", ")}` : void 0;
|
|
29
|
+
const describeRequires = (route) => {
|
|
30
|
+
const requires = route.requires;
|
|
31
|
+
if (requires === void 0) return void 0;
|
|
32
|
+
const names = Object.entries(requires).flatMap(([resource, verbs]) => verbs.map((verb) => `${resource}:${verb}`));
|
|
33
|
+
return names.length > 0 ? `Permissions: ${names.join(", ")}` : void 0;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* What a model reads before it calls the route.
|
|
21
37
|
*/
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
38
|
+
const describeTool = (route) => {
|
|
39
|
+
const parts = [];
|
|
40
|
+
const declared = toolOptionsOf(route)?.description;
|
|
41
|
+
if (declared) parts.push(declared);
|
|
42
|
+
else if (route.summary) parts.push(route.summary);
|
|
43
|
+
if (route.description) parts.push(route.description);
|
|
44
|
+
if (parts.length === 0) parts.push(`${route.method} ${route.path}`);
|
|
45
|
+
parts.push(`\nHTTP: ${route.method} ${route.path}`);
|
|
46
|
+
const requirements = require_schemas.resolveSecurityRequirements(route);
|
|
47
|
+
if (requirements.length > 0) parts.push(`Requires: ${requirements.map(({ scheme }) => scheme).join(", ")}`);
|
|
48
|
+
const roles = describeRoles(route);
|
|
49
|
+
if (roles !== void 0) parts.push(roles);
|
|
50
|
+
const permissions = describeRequires(route);
|
|
51
|
+
if (permissions !== void 0) parts.push(permissions);
|
|
52
|
+
return parts.join("\n");
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* The `{ params, query, body }` a model sends to call the route.
|
|
56
|
+
*/
|
|
57
|
+
const buildToolInputSchema = (route) => {
|
|
58
|
+
const shape = {};
|
|
59
|
+
let hasParams = false;
|
|
60
|
+
let hasQuery = false;
|
|
61
|
+
let hasBody = false;
|
|
62
|
+
const paramNames = require_plugin_server.parsePath(route.path).paramNames;
|
|
63
|
+
if (paramNames.length > 0) {
|
|
64
|
+
hasParams = true;
|
|
65
|
+
const paramShape = {};
|
|
66
|
+
const explicitShape = route.pathParams ? require_error_response.readObjectShape(route.pathParams) : void 0;
|
|
67
|
+
for (const name of paramNames) paramShape[name] = explicitShape?.[name] ?? zod.z.string();
|
|
68
|
+
shape["params"] = zod.z.object(paramShape);
|
|
69
|
+
}
|
|
70
|
+
if (route.query) {
|
|
71
|
+
hasQuery = true;
|
|
72
|
+
shape["query"] = route.query.safeParse({}).success ? route.query.optional() : route.query;
|
|
73
|
+
}
|
|
74
|
+
if (route.body && !require_error_response.isVoidSchema(route.body)) {
|
|
75
|
+
hasBody = true;
|
|
76
|
+
shape["body"] = route.body.safeParse(void 0).success ? route.body.optional() : route.body;
|
|
77
|
+
}
|
|
25
78
|
return {
|
|
26
|
-
|
|
27
|
-
|
|
79
|
+
shape: Object.keys(shape).length === 0 ? void 0 : shape,
|
|
80
|
+
hasParams,
|
|
81
|
+
hasQuery,
|
|
82
|
+
hasBody
|
|
28
83
|
};
|
|
29
84
|
};
|
|
85
|
+
const toJsonSchema = (route) => {
|
|
86
|
+
const { shape } = buildToolInputSchema(route);
|
|
87
|
+
const { $schema: _, ...schema } = zod.z.toJSONSchema(zod.z.object(shape ?? {}), {
|
|
88
|
+
io: "input",
|
|
89
|
+
unrepresentable: "any",
|
|
90
|
+
override: ({ jsonSchema }) => {
|
|
91
|
+
delete jsonSchema.brand;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return schema;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* One {@link ToolDefinition} per route.
|
|
98
|
+
*/
|
|
99
|
+
const buildToolDefinitions = (routes) => {
|
|
100
|
+
const entries = require_plugin_server.flattenRoutes(routes);
|
|
101
|
+
const names = require_plugin_server.deriveToolNames(entries.map(({ routeKey }) => ({
|
|
102
|
+
key: routeKey,
|
|
103
|
+
origin: "route"
|
|
104
|
+
})));
|
|
105
|
+
return entries.map(({ routeKey, route }) => ({
|
|
106
|
+
name: names.get(routeKey),
|
|
107
|
+
key: routeKey,
|
|
108
|
+
description: describeTool(route),
|
|
109
|
+
inputSchema: toJsonSchema(route)
|
|
110
|
+
}));
|
|
111
|
+
};
|
|
30
112
|
//#endregion
|
|
31
113
|
//#region src/tool-events.ts
|
|
32
114
|
/**
|
|
115
|
+
* Where a streamed response keeps the routes it named under `tools`.
|
|
116
|
+
*/
|
|
117
|
+
const STREAM_TOOLS = Symbol.for("kizuna.stream.tools");
|
|
118
|
+
const streamToolsOf = (response) => response !== null && typeof response === "object" ? response[STREAM_TOOLS] : void 0;
|
|
119
|
+
/**
|
|
33
120
|
* The `{ params, query, body }` a route takes as a tool, as schemas, so the
|
|
34
121
|
* stream events and the MCP endpoint agree on one shape.
|
|
35
122
|
*/
|
|
36
123
|
const toolInputShape = (route) => {
|
|
37
124
|
const shape = {};
|
|
38
|
-
const paramNames =
|
|
125
|
+
const paramNames = require_plugin_server.parsePath(route.path).paramNames;
|
|
39
126
|
if (paramNames.length > 0) {
|
|
40
127
|
const declared = route.pathParams ? require_error_response.readObjectShape(route.pathParams) : void 0;
|
|
41
128
|
shape["params"] = zod.z.object(Object.fromEntries(paramNames.map((name) => [name, declared?.[name] ?? zod.z.string()])));
|
|
@@ -45,13 +132,30 @@ const toolInputShape = (route) => {
|
|
|
45
132
|
return shape;
|
|
46
133
|
};
|
|
47
134
|
/**
|
|
48
|
-
*
|
|
135
|
+
* The body of each `2xx` the route declares.
|
|
49
136
|
*/
|
|
50
137
|
const toolOutputSchema = (route) => {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
138
|
+
const bodies = [];
|
|
139
|
+
let someSuccessHasNoBody = false;
|
|
140
|
+
for (const status of Object.keys(route.responses).map(Number).sort((left, right) => left - right)) {
|
|
141
|
+
if (!require_plugin_server.isSuccessStatus(status)) continue;
|
|
142
|
+
const response = route.responses[status];
|
|
143
|
+
if (response === void 0) continue;
|
|
144
|
+
const contentType = require_plugin_server.resolveResponseContentType(response);
|
|
145
|
+
if (require_plugin_server.isStreamResponse(response) || contentType !== void 0 && !require_plugin_server.isJsonMediaType(contentType)) {
|
|
146
|
+
someSuccessHasNoBody = true;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const body = require_plugin_server.resolveResponseBody(response);
|
|
150
|
+
if (body === void 0 || require_error_response.isVoidSchema(body)) {
|
|
151
|
+
someSuccessHasNoBody = true;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
bodies.push(body);
|
|
155
|
+
}
|
|
156
|
+
if (bodies.length === 0) return void 0;
|
|
157
|
+
const output = bodies.length === 1 ? bodies[0] : zod.z.union(bodies);
|
|
158
|
+
return someSuccessHasNoBody ? output.optional() : output;
|
|
55
159
|
};
|
|
56
160
|
/**
|
|
57
161
|
* Server-sent events for the routes a model may call, to spread into a route's
|
|
@@ -69,7 +173,7 @@ const toolOutputSchema = (route) => {
|
|
|
69
173
|
* ```
|
|
70
174
|
*/
|
|
71
175
|
const toolEvents = (tools) => {
|
|
72
|
-
const entries =
|
|
176
|
+
const entries = require_plugin_server.flattenRoutes(tools);
|
|
73
177
|
if (entries.length === 0) throw new Error("`toolEvents` was given no routes. Name at least one route a model may call, or drop the spread.");
|
|
74
178
|
const names = entries.map(({ routeKey }) => routeKey);
|
|
75
179
|
const identifier = { id: zod.z.string() };
|
|
@@ -78,6 +182,7 @@ const toolEvents = (tools) => {
|
|
|
78
182
|
return zod.z.object({
|
|
79
183
|
...identifier,
|
|
80
184
|
name: zod.z.literal(routeKey),
|
|
185
|
+
needsApproval: zod.z.literal(true).optional(),
|
|
81
186
|
...Object.keys(input).length > 0 ? { input: zod.z.object(input) } : {}
|
|
82
187
|
});
|
|
83
188
|
});
|
|
@@ -106,12 +211,25 @@ const toolEvents = (tools) => {
|
|
|
106
211
|
* `k.routes` before a route is validated.
|
|
107
212
|
*/
|
|
108
213
|
const expandStreamTools = (route, routeKey) => {
|
|
214
|
+
let namedAt;
|
|
109
215
|
for (const [status, response] of Object.entries(route.responses)) {
|
|
110
|
-
if (!
|
|
216
|
+
if (!require_plugin_server.isStreamResponse(response)) continue;
|
|
111
217
|
const { tools } = response;
|
|
112
218
|
if (tools === void 0) continue;
|
|
113
219
|
const where = `Route "${routeKey}" declares tools on status ${status}`;
|
|
114
|
-
if (
|
|
220
|
+
if (namedAt !== void 0) throw new Error(`${where} and on status ${namedAt}. A handler runs one set of tools, so name them on one response.`);
|
|
221
|
+
namedAt = status;
|
|
222
|
+
if (require_plugin_server.isZodSchema(response.stream)) throw new Error(`${where} beside a single stream schema. Tool events are named, so name the other events too.`);
|
|
223
|
+
const toolEntries = require_plugin_server.flattenRoutes(tools);
|
|
224
|
+
require_plugin_server.deriveToolNames(toolEntries.map(({ routeKey: toolKey }) => ({
|
|
225
|
+
key: toolKey,
|
|
226
|
+
origin: "route"
|
|
227
|
+
})));
|
|
228
|
+
for (const { routeKey: toolKey, route: toolRoute } of toolEntries) {
|
|
229
|
+
if (toolOptionsOf(toolRoute) === void 0) throw new Error(`${where}, naming "${toolKey}", which does not declare \`tool\`. Declare it on that route.`);
|
|
230
|
+
const refusal = toolRefusal(toolRoute);
|
|
231
|
+
if (refusal !== void 0) throw new Error(`${where}, naming "${toolKey}", which cannot run as a tool: ${refusal}.`);
|
|
232
|
+
}
|
|
115
233
|
const events = toolEvents(tools);
|
|
116
234
|
const named = response.stream;
|
|
117
235
|
for (const name of Object.keys(events)) if (name in named) throw new Error(`${where}, but its stream already names an event "${name}". Rename that event.`);
|
|
@@ -120,6 +238,7 @@ const expandStreamTools = (route, routeKey) => {
|
|
|
120
238
|
...named,
|
|
121
239
|
...events
|
|
122
240
|
};
|
|
241
|
+
mutable[STREAM_TOOLS] = tools;
|
|
123
242
|
delete mutable.tools;
|
|
124
243
|
}
|
|
125
244
|
};
|
|
@@ -131,8 +250,8 @@ const isEmptyObjectSchema = (schema) => {
|
|
|
131
250
|
return typeof candidate.shape === "object" && candidate.shape !== null && Object.keys(candidate.shape).length === 0;
|
|
132
251
|
};
|
|
133
252
|
const responseSchemas = (response) => {
|
|
134
|
-
if (
|
|
135
|
-
if (
|
|
253
|
+
if (require_plugin_server.isZodSchema(response)) return [response];
|
|
254
|
+
if (require_plugin_server.isStreamResponse(response)) return require_plugin_server.streamSchemas(response.stream);
|
|
136
255
|
return [response.body];
|
|
137
256
|
};
|
|
138
257
|
/**
|
|
@@ -164,7 +283,7 @@ const assertPathParamsMatchPath = (route, routeKey) => {
|
|
|
164
283
|
const shape = route.pathParams ? require_error_response.readObjectShape(route.pathParams) : void 0;
|
|
165
284
|
if (!shape) return;
|
|
166
285
|
const declared = Object.keys(shape);
|
|
167
|
-
const { paramNames } =
|
|
286
|
+
const { paramNames } = require_plugin_server.parsePath(route.path);
|
|
168
287
|
const unmatched = declared.filter((name) => !paramNames.includes(name));
|
|
169
288
|
const undeclared = paramNames.filter((name) => !declared.includes(name));
|
|
170
289
|
if (unmatched.length === 0 && undeclared.length === 0) return;
|
|
@@ -204,40 +323,136 @@ const assertToolDescribed = (route, routeKey) => {
|
|
|
204
323
|
const validateRoutes = (routes, prefix) => {
|
|
205
324
|
for (const [key, value] of Object.entries(routes)) {
|
|
206
325
|
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
207
|
-
if (
|
|
326
|
+
if (require_plugin_server.isRouteDefinition(value)) {
|
|
208
327
|
if (isEmptyObjectSchema(value.body)) throw new Error(`Route "${fullKey}" has an empty body schema (z.object({})). Use z.void() or omit the body field.`);
|
|
209
328
|
expandStreamTools(value, fullKey);
|
|
210
329
|
assertPathParamsMatchPath(value, fullKey);
|
|
211
330
|
assertPathParamsAreScalar(value, fullKey);
|
|
212
331
|
assertNoCoercion(value, fullKey);
|
|
213
|
-
|
|
332
|
+
require_plugin_server.assertValidStreams(value, fullKey);
|
|
214
333
|
assertToolDescribed(value, fullKey);
|
|
215
|
-
|
|
334
|
+
require_plugin_server.resolveCoercionPlans(value);
|
|
216
335
|
} else if (value && typeof value === "object") validateRoutes(value, fullKey);
|
|
217
336
|
}
|
|
218
337
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
338
|
+
const isDeclared = (value) => typeof value === "object" && value !== null && require_plugin.ROUTES_GROUP in value;
|
|
339
|
+
const groupOf = (value) => isDeclared(value) ? value[require_plugin.ROUTES_GROUP] : void 0;
|
|
340
|
+
/**
|
|
341
|
+
* Throw on a grouped tree nested inside another. It belongs in `defineConfig`'s
|
|
342
|
+
* list.
|
|
343
|
+
*/
|
|
344
|
+
const assertNoGroupInside = (routes, prefix) => {
|
|
345
|
+
for (const [key, value] of Object.entries(routes)) {
|
|
346
|
+
if (require_plugin_server.isRouteDefinition(value) || !value || typeof value !== "object") continue;
|
|
347
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
348
|
+
const group = groupOf(value);
|
|
349
|
+
if (group !== void 0 && group !== "") throw new Error(`"${fullKey}" was declared into the group ${group}, so its key comes from there. List it in defineConfig's \`routes\` instead of nesting it.`);
|
|
350
|
+
assertNoGroupInside(value, fullKey);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
function groupRoutes(first, second) {
|
|
354
|
+
const routes = typeof first === "string" ? second : first;
|
|
355
|
+
assertNoGroupInside(routes);
|
|
356
|
+
routes[require_plugin.ROUTES_GROUP] = typeof first === "string" ? first : "";
|
|
357
|
+
validateRoutes(routes);
|
|
358
|
+
return routes;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Merge `source` into `target`, throwing when both hold the same key.
|
|
362
|
+
*/
|
|
363
|
+
const mergeRoutes = (target, source, path) => {
|
|
364
|
+
for (const [key, value] of Object.entries(source)) {
|
|
365
|
+
const fullKey = path === "" ? key : `${path}.${key}`;
|
|
366
|
+
const existing = target[key];
|
|
367
|
+
if (existing === void 0) {
|
|
368
|
+
target[key] = value;
|
|
369
|
+
continue;
|
|
226
370
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
371
|
+
if (require_plugin_server.isRouteDefinition(existing) || require_plugin_server.isRouteDefinition(value)) throw new Error(`Two routes claim the key "${fullKey}". Rename one, or declare it into another group.`);
|
|
372
|
+
const merged = { ...existing };
|
|
373
|
+
mergeRoutes(merged, value, fullKey);
|
|
374
|
+
target[key] = merged;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
/**
|
|
378
|
+
* The route tree a list of declared trees makes, each at its group's path.
|
|
379
|
+
*/
|
|
380
|
+
const assembleRoutes = (declared) => {
|
|
381
|
+
const root = {};
|
|
382
|
+
for (const entry of declared) {
|
|
383
|
+
const routes = isDeclared(entry) ? entry : groupRoutes(entry);
|
|
384
|
+
const group = routes[require_plugin.ROUTES_GROUP];
|
|
385
|
+
let node = root;
|
|
386
|
+
let path = "";
|
|
387
|
+
for (const segment of group === "" ? [] : group.split(".")) {
|
|
388
|
+
path = path === "" ? segment : `${path}.${segment}`;
|
|
389
|
+
const existing = node[segment];
|
|
390
|
+
if (require_plugin_server.isRouteDefinition(existing)) throw new Error(`The route "${path}" sits where the group ${group} needs its key. Rename the route, or the group.`);
|
|
391
|
+
if (existing === void 0) node[segment] = {};
|
|
392
|
+
node = node[segment];
|
|
393
|
+
}
|
|
394
|
+
if (group !== "") node[require_plugin.ROUTES_GROUP] = group;
|
|
395
|
+
mergeRoutes(node, routes, path);
|
|
230
396
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
397
|
+
return root;
|
|
398
|
+
};
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/groups.ts
|
|
401
|
+
const isGroupSet = (value) => typeof value === "object" && value !== null && "__brand" in value && value.__brand === "GroupSet";
|
|
402
|
+
const collect = (declared, ancestors, into) => {
|
|
403
|
+
for (const [key, value] of Object.entries(declared)) {
|
|
404
|
+
if (key.includes(".")) throw new Error(`The group "${key}" has a dot in its key. Nest it under \`groups\` instead.`);
|
|
405
|
+
const options = typeof value === "string" ? { title: value } : value;
|
|
406
|
+
const parent = ancestors[ancestors.length - 1];
|
|
407
|
+
const path = parent === void 0 ? key : `${parent}.${key}`;
|
|
408
|
+
const lineage = [...ancestors, path];
|
|
409
|
+
into.set(path, {
|
|
410
|
+
path,
|
|
411
|
+
title: options.title,
|
|
412
|
+
...options.description === void 0 ? {} : { description: options.description },
|
|
413
|
+
...options.externalDocs === void 0 ? {} : { externalDocs: options.externalDocs },
|
|
414
|
+
lineage
|
|
415
|
+
});
|
|
416
|
+
if (options.groups) collect(options.groups, lineage, into);
|
|
236
417
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
418
|
+
};
|
|
419
|
+
/**
|
|
420
|
+
* Throw on two siblings with one title.
|
|
421
|
+
*/
|
|
422
|
+
const assertUniqueTitles = (groups) => {
|
|
423
|
+
const owners = /* @__PURE__ */ new Map();
|
|
424
|
+
for (const group of groups.values()) {
|
|
425
|
+
const sibling = `${group.lineage[group.lineage.length - 2] ?? ""}\u0000${group.title}`;
|
|
426
|
+
const owner = owners.get(sibling);
|
|
427
|
+
if (owner !== void 0) throw new Error(`The groups "${owner}" and "${group.path}" share the title "${group.title}". Give each group its own title.`);
|
|
428
|
+
owners.set(sibling, group.path);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
/**
|
|
432
|
+
* Declare the groups routes are filed under. A string is shorthand for
|
|
433
|
+
* `{ title }`.
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* const groups = k.groups({
|
|
437
|
+
* workspace: {
|
|
438
|
+
* title: 'Workspace',
|
|
439
|
+
* groups: {
|
|
440
|
+
* members: 'Members',
|
|
441
|
+
* },
|
|
442
|
+
* },
|
|
443
|
+
* health: 'Health',
|
|
444
|
+
* });
|
|
445
|
+
*/
|
|
446
|
+
const createGroups = (declared) => {
|
|
447
|
+
const groups = /* @__PURE__ */ new Map();
|
|
448
|
+
collect(declared, [], groups);
|
|
449
|
+
assertUniqueTitles(groups);
|
|
450
|
+
return {
|
|
451
|
+
__brand: "GroupSet",
|
|
452
|
+
declared,
|
|
453
|
+
groups
|
|
454
|
+
};
|
|
455
|
+
};
|
|
241
456
|
//#endregion
|
|
242
457
|
//#region src/permissions.ts
|
|
243
458
|
const isPermissions = (value) => typeof value === "object" && value !== null && "__brand" in value && value.__brand === "Permissions";
|
|
@@ -400,13 +615,13 @@ const problemFromBody = (status, body) => {
|
|
|
400
615
|
*/
|
|
401
616
|
const dueJobs = (contract, options) => {
|
|
402
617
|
const schedules = {};
|
|
403
|
-
for (const { jobKey, job } of
|
|
618
|
+
for (const { jobKey, job } of require_plugin_server.flattenJobs(contract.jobs ?? {})) {
|
|
404
619
|
if (options?.only && !options.only.includes(jobKey)) continue;
|
|
405
620
|
if (options?.exclude?.includes(jobKey)) continue;
|
|
406
621
|
if (job.schedule === void 0) continue;
|
|
407
622
|
schedules[jobKey] = job.schedule;
|
|
408
623
|
}
|
|
409
|
-
return
|
|
624
|
+
return require_plugin_server.dueSchedules(schedules, {
|
|
410
625
|
at: options?.at,
|
|
411
626
|
windowMs: options?.windowMs
|
|
412
627
|
});
|
|
@@ -557,7 +772,7 @@ const handlerAt = (handlers, jobKey) => {
|
|
|
557
772
|
const createJobRunner = (source, handlers, options) => {
|
|
558
773
|
const jobs = (source && "jobs" in source ? source.jobs ?? {} : source) ?? {};
|
|
559
774
|
const jobFor = (jobKey) => {
|
|
560
|
-
const job =
|
|
775
|
+
const job = require_plugin_server.jobAt(jobs, jobKey);
|
|
561
776
|
if (!job) throw new Error(`No job named "${jobKey}" on this api.`);
|
|
562
777
|
return job;
|
|
563
778
|
};
|
|
@@ -572,12 +787,13 @@ const createJobRunner = (source, handlers, options) => {
|
|
|
572
787
|
const job = jobFor(jobKey);
|
|
573
788
|
const handler = handlerAt(handlers, jobKey);
|
|
574
789
|
if (typeof handler !== "function") throw new Error(`No handler was bound for job "${jobKey}".`);
|
|
575
|
-
return
|
|
790
|
+
return require_plugin_server.toJobResponse(handler({
|
|
576
791
|
input: validateInput(job, jobKey, input),
|
|
577
792
|
throwError: (response) => {
|
|
578
793
|
throw new ResponseError(response);
|
|
579
794
|
},
|
|
580
|
-
jobs: tree
|
|
795
|
+
jobs: tree,
|
|
796
|
+
plugins: options?.plugins ?? {}
|
|
581
797
|
}));
|
|
582
798
|
};
|
|
583
799
|
const runDetached = (jobKey, input, runAt) => {
|
|
@@ -629,7 +845,7 @@ const createJobRunner = (source, handlers, options) => {
|
|
|
629
845
|
const result = {};
|
|
630
846
|
for (const [name, node] of Object.entries(nodes)) {
|
|
631
847
|
const jobKey = prefix ? `${prefix}.${name}` : name;
|
|
632
|
-
if (
|
|
848
|
+
if (require_plugin_server.isCompiledJob(node)) result[name] = {
|
|
633
849
|
run: (input) => invoke(jobKey, input),
|
|
634
850
|
queue: (message) => queue(jobKey, message)
|
|
635
851
|
};
|
|
@@ -657,7 +873,7 @@ var JobDispatchError = class extends Error {
|
|
|
657
873
|
*
|
|
658
874
|
* @example
|
|
659
875
|
* export const pgBoss = (boss: PgBoss) =>
|
|
660
|
-
*
|
|
876
|
+
* defineJobTransport({
|
|
661
877
|
* name: 'pg-boss',
|
|
662
878
|
* supports: {
|
|
663
879
|
* retry: true,
|
|
@@ -686,8 +902,8 @@ var JobDispatchError = class extends Error {
|
|
|
686
902
|
* },
|
|
687
903
|
* });
|
|
688
904
|
*/
|
|
689
|
-
const
|
|
690
|
-
if (!definition.name) throw new Error("
|
|
905
|
+
const defineJobTransport = (definition) => {
|
|
906
|
+
if (!definition.name) throw new Error("defineJobTransport needs a `name`.");
|
|
691
907
|
return {
|
|
692
908
|
...definition,
|
|
693
909
|
supports: definition.supports ?? {},
|
|
@@ -778,7 +994,7 @@ const requiresDenial = (input) => {
|
|
|
778
994
|
//#region src/route-matcher.ts
|
|
779
995
|
const REGEX_ESCAPE = /[.+?^${}()|[\]\\]/g;
|
|
780
996
|
const compileRoute = (routeKey, route) => {
|
|
781
|
-
const { segments, paramNames } =
|
|
997
|
+
const { segments, paramNames } = require_plugin_server.parsePath(route.path);
|
|
782
998
|
let body = "";
|
|
783
999
|
for (const segment of segments) if (segment.kind === "literal") body += segment.value.replace(REGEX_ESCAPE, "\\$&");
|
|
784
1000
|
else body += "([^/]+)";
|
|
@@ -794,7 +1010,7 @@ const cache = /* @__PURE__ */ new WeakMap();
|
|
|
794
1010
|
const getCompiled = (routes) => {
|
|
795
1011
|
const existing = cache.get(routes);
|
|
796
1012
|
if (existing) return existing;
|
|
797
|
-
const fresh =
|
|
1013
|
+
const fresh = require_plugin_server.flattenRoutes(routes).map(({ routeKey, route }) => compileRoute(routeKey, route));
|
|
798
1014
|
fresh.sort((a, b) => {
|
|
799
1015
|
const limit = Math.min(a.segments.length, b.segments.length);
|
|
800
1016
|
for (let index = 0; index < limit; index++) {
|
|
@@ -817,7 +1033,7 @@ const stripBasePath = (pathname, basePath) => {
|
|
|
817
1033
|
const sortFlattenedRoutes = (routes) => {
|
|
818
1034
|
const parsed = routes.map((entry) => ({
|
|
819
1035
|
entry,
|
|
820
|
-
...
|
|
1036
|
+
...require_plugin_server.parsePath(entry.route.path)
|
|
821
1037
|
}));
|
|
822
1038
|
parsed.sort((a, b) => {
|
|
823
1039
|
const limit = Math.min(a.segments.length, b.segments.length);
|
|
@@ -876,7 +1092,7 @@ const matchRoute = (method, pathname, routes, basePath) => {
|
|
|
876
1092
|
//#endregion
|
|
877
1093
|
//#region src/path-claims.ts
|
|
878
1094
|
const signatureOf = ({ method, path }) => {
|
|
879
|
-
const { segments } =
|
|
1095
|
+
const { segments } = require_plugin_server.parsePath(path);
|
|
880
1096
|
return `${method}:${segments.map((segment) => segment.kind === "param" ? ":*" : segment.value).join("")}`;
|
|
881
1097
|
};
|
|
882
1098
|
/**
|
|
@@ -891,7 +1107,7 @@ const assertNoPathCollisions = (claims) => {
|
|
|
891
1107
|
claimed.set(signature, claim);
|
|
892
1108
|
}
|
|
893
1109
|
};
|
|
894
|
-
const routeClaims = (routes, kind = "Route") => routes === void 0 ? [] :
|
|
1110
|
+
const routeClaims = (routes, kind = "Route") => routes === void 0 ? [] : require_plugin_server.flattenRoutes(routes).map(({ routeKey, route }) => ({
|
|
895
1111
|
kind,
|
|
896
1112
|
key: routeKey,
|
|
897
1113
|
method: route.method,
|
|
@@ -950,46 +1166,132 @@ const rawResponse = (response) => ({
|
|
|
950
1166
|
});
|
|
951
1167
|
const isRawResponse = (value) => typeof value === "object" && value !== null && RAW_RESPONSE in value;
|
|
952
1168
|
//#endregion
|
|
953
|
-
//#region src/
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
const
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1169
|
+
//#region src/tool-runner.ts
|
|
1170
|
+
const DECLINED = "Declined, so nothing ran.";
|
|
1171
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1172
|
+
const detailOf = (status, body) => isRecord(body) && typeof body["detail"] === "string" ? body["detail"] : `The route answered ${status}.`;
|
|
1173
|
+
const callInput = (route, parsed) => {
|
|
1174
|
+
const input = {};
|
|
1175
|
+
if (require_plugin_server.parsePath(route.path).paramNames.length > 0) input["params"] = parsed.params;
|
|
1176
|
+
if (route.query) input["query"] = parsed.query;
|
|
1177
|
+
if (route.body && !require_error_response.isVoidSchema(route.body)) input["body"] = parsed.body;
|
|
1178
|
+
return Object.keys(input).length > 0 ? input : void 0;
|
|
1179
|
+
};
|
|
1180
|
+
const definitionsCache = /* @__PURE__ */ new WeakMap();
|
|
1181
|
+
const definitionsFor = (tools) => {
|
|
1182
|
+
const cached = definitionsCache.get(tools);
|
|
1183
|
+
if (cached) return cached;
|
|
1184
|
+
const built = buildToolDefinitions(tools);
|
|
1185
|
+
definitionsCache.set(tools, built);
|
|
1186
|
+
return built;
|
|
1187
|
+
};
|
|
1188
|
+
/**
|
|
1189
|
+
* Bind a response's tools to the request streaming it.
|
|
1190
|
+
*/
|
|
1191
|
+
const createStreamTools = (tools, pipeline) => {
|
|
1192
|
+
const definitions = definitionsFor(tools);
|
|
1193
|
+
const entries = new Map(require_plugin_server.flattenRoutes(tools).map((entry) => [entry.routeKey, entry]));
|
|
1194
|
+
const keysByName = new Map(definitions.map((definition) => [definition.name, definition.key]));
|
|
1195
|
+
const namesByKey = new Map(definitions.map((definition) => [definition.key, definition.name]));
|
|
1196
|
+
const answered = (call, status, body) => {
|
|
1197
|
+
const content = JSON.stringify({
|
|
1198
|
+
status,
|
|
1199
|
+
body
|
|
1200
|
+
});
|
|
1201
|
+
return status < 400 ? {
|
|
1202
|
+
call,
|
|
1203
|
+
state: "done",
|
|
1204
|
+
status,
|
|
1205
|
+
body,
|
|
1206
|
+
content,
|
|
1207
|
+
isError: false
|
|
1208
|
+
} : {
|
|
1209
|
+
call,
|
|
1210
|
+
state: "failed",
|
|
1211
|
+
status,
|
|
1212
|
+
body,
|
|
1213
|
+
content,
|
|
1214
|
+
isError: true
|
|
1215
|
+
};
|
|
1216
|
+
};
|
|
1217
|
+
async function* run(call, options = {}) {
|
|
1218
|
+
const key = keysByName.get(call.name) ?? call.name;
|
|
1219
|
+
const entry = entries.get(key);
|
|
1220
|
+
const modelCall = {
|
|
1221
|
+
id: call.id,
|
|
1222
|
+
name: namesByKey.get(key) ?? call.name,
|
|
1223
|
+
input: call.input ?? {}
|
|
1224
|
+
};
|
|
1225
|
+
if (entry === void 0) return answered(modelCall, 404, problemDetails(404, `No tool named "${call.name}". Call one of: ${[...keysByName.keys()].join(", ")}.`));
|
|
1226
|
+
const failed = (status, body) => ({
|
|
1227
|
+
event: "tool_error",
|
|
1228
|
+
data: {
|
|
1229
|
+
id: call.id,
|
|
1230
|
+
name: key,
|
|
1231
|
+
message: detailOf(status, body)
|
|
1232
|
+
}
|
|
1233
|
+
});
|
|
1234
|
+
const input = isRecord(call.input) ? call.input : {};
|
|
1235
|
+
const params = Object.fromEntries(Object.entries(isRecord(input["params"]) ? input["params"] : {}).map(([name, value]) => [name, String(value)]));
|
|
1236
|
+
const validation = require_plugin_server.validateRequest(entry.route, {
|
|
1237
|
+
params,
|
|
1238
|
+
query: input["query"] ?? {},
|
|
1239
|
+
body: input["body"],
|
|
1240
|
+
headers: pipeline.headers
|
|
1241
|
+
});
|
|
1242
|
+
if (!validation.ok) {
|
|
1243
|
+
const rendered = pipeline.render(validation.error);
|
|
1244
|
+
yield failed(rendered.status, rendered.body);
|
|
1245
|
+
return answered(modelCall, rendered.status, rendered.body);
|
|
989
1246
|
}
|
|
990
|
-
|
|
1247
|
+
const validated = callInput(entry.route, validation.parsed);
|
|
1248
|
+
const needsApproval = toolOptionsOf(entry.route)?.needsApproval === true;
|
|
1249
|
+
const waiting = needsApproval && options.approved === void 0;
|
|
1250
|
+
yield {
|
|
1251
|
+
event: "tool_call",
|
|
1252
|
+
data: {
|
|
1253
|
+
id: call.id,
|
|
1254
|
+
name: key,
|
|
1255
|
+
...waiting ? { needsApproval: true } : {},
|
|
1256
|
+
...validated === void 0 ? {} : { input: validated }
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
if (waiting) return {
|
|
1260
|
+
call: modelCall,
|
|
1261
|
+
state: "needs-approval"
|
|
1262
|
+
};
|
|
1263
|
+
if (needsApproval && options.approved === false) {
|
|
1264
|
+
yield {
|
|
1265
|
+
event: "tool_error",
|
|
1266
|
+
data: {
|
|
1267
|
+
id: call.id,
|
|
1268
|
+
name: key,
|
|
1269
|
+
message: DECLINED
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
return {
|
|
1273
|
+
call: modelCall,
|
|
1274
|
+
state: "declined",
|
|
1275
|
+
content: DECLINED,
|
|
1276
|
+
isError: true
|
|
1277
|
+
};
|
|
1278
|
+
}
|
|
1279
|
+
const { status, body } = await pipeline.invoke(entry, validation.parsed, params);
|
|
1280
|
+
if (status >= 400) yield failed(status, body);
|
|
1281
|
+
else yield {
|
|
1282
|
+
event: "tool_result",
|
|
1283
|
+
data: {
|
|
1284
|
+
id: call.id,
|
|
1285
|
+
name: key,
|
|
1286
|
+
...body === void 0 ? {} : { output: body }
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
return answered(modelCall, status, body);
|
|
991
1290
|
}
|
|
992
|
-
return
|
|
1291
|
+
return {
|
|
1292
|
+
definitions,
|
|
1293
|
+
run
|
|
1294
|
+
};
|
|
993
1295
|
};
|
|
994
1296
|
//#endregion
|
|
995
1297
|
//#region src/adapter.ts
|
|
@@ -1067,7 +1369,10 @@ const HANDLER_ARG_KEYS = [
|
|
|
1067
1369
|
"throwError",
|
|
1068
1370
|
"auth",
|
|
1069
1371
|
"requestContext",
|
|
1070
|
-
"plugins"
|
|
1372
|
+
"plugins",
|
|
1373
|
+
"tools",
|
|
1374
|
+
"rawBody",
|
|
1375
|
+
"lastEventId"
|
|
1071
1376
|
];
|
|
1072
1377
|
/**
|
|
1073
1378
|
* The adapter's own context, with kizuna's arguments removed.
|
|
@@ -1077,12 +1382,31 @@ const assertNoDuplicateRoutes = (routes, pluginRoutes = {}) => {
|
|
|
1077
1382
|
assertNoPathCollisions([...routeClaims(routes), ...routeClaims(pluginRoutes, "Plugin route")]);
|
|
1078
1383
|
};
|
|
1079
1384
|
/**
|
|
1385
|
+
* Each plugin's router, built from the handlers its routes carry, and its
|
|
1386
|
+
* exports. A plugin route without a handler throws, since nothing would answer
|
|
1387
|
+
* it.
|
|
1388
|
+
*/
|
|
1389
|
+
const pluginServers = (plugins) => {
|
|
1390
|
+
const servers = {};
|
|
1391
|
+
for (const [slug, plugin] of Object.entries(plugins ?? {})) {
|
|
1392
|
+
for (const [routeKey, route] of Object.entries(plugin.routes)) {
|
|
1393
|
+
if (route[require_plugin.HANDLER] !== void 0) continue;
|
|
1394
|
+
throw new Error(`Plugin '${slug}' declares the route '${routeKey}' without a handler. Declare it with \`route(...).handler(...)\`.`);
|
|
1395
|
+
}
|
|
1396
|
+
servers[slug] = {
|
|
1397
|
+
router: routerFromRoutes(plugin.routes),
|
|
1398
|
+
exports: plugin.exports
|
|
1399
|
+
};
|
|
1400
|
+
}
|
|
1401
|
+
return servers;
|
|
1402
|
+
};
|
|
1403
|
+
/**
|
|
1080
1404
|
* Brand an api's routes with the parts that serve them, for the adapter to read back when mounting.
|
|
1081
1405
|
*/
|
|
1082
1406
|
const assembleApi = (contract, parts) => {
|
|
1083
1407
|
const pluginRoutes = require_plugin.pluginRouteTree(contract.plugins);
|
|
1084
1408
|
assertNoDuplicateRoutes(contract.routes, pluginRoutes);
|
|
1085
|
-
for (const { route } of [...
|
|
1409
|
+
for (const { route } of [...require_plugin_server.flattenRoutes(contract.routes), ...require_plugin_server.flattenRoutes(pluginRoutes)]) require_plugin_server.resolveCoercionPlans(route);
|
|
1086
1410
|
const api = {
|
|
1087
1411
|
routes: contract.routes,
|
|
1088
1412
|
[API_META]: true,
|
|
@@ -1094,17 +1418,17 @@ const assembleApi = (contract, parts) => {
|
|
|
1094
1418
|
[require_plugin.PLUGIN_ROUTES_META_KEY]: pluginRoutes,
|
|
1095
1419
|
[CONTRACT_META]: contract
|
|
1096
1420
|
};
|
|
1097
|
-
api[require_plugin.PLUGIN_SERVERS_META_KEY] =
|
|
1421
|
+
api[require_plugin.PLUGIN_SERVERS_META_KEY] = pluginServers(contract.plugins);
|
|
1098
1422
|
return api;
|
|
1099
1423
|
};
|
|
1100
1424
|
/**
|
|
1101
1425
|
* The endpoints jobs are served on, in the {@link Routes} shape the request
|
|
1102
|
-
* pipeline takes.
|
|
1426
|
+
* pipeline takes. None when the jobs run in process.
|
|
1103
1427
|
*/
|
|
1104
|
-
const jobRoutes = (meta) =>
|
|
1428
|
+
const jobRoutes = (meta) => meta.config?.mode === "in-process" ? {} : {
|
|
1105
1429
|
[DISPATCH_ROUTE_KEY]: dispatchRoute(meta),
|
|
1106
1430
|
[RUN_ROUTE_KEY]: runRoute(meta)
|
|
1107
|
-
}
|
|
1431
|
+
};
|
|
1108
1432
|
/**
|
|
1109
1433
|
* Their handlers, in the {@link Router} shape the request pipeline takes.
|
|
1110
1434
|
*/
|
|
@@ -1117,7 +1441,7 @@ const jobRouter = (meta) => ({
|
|
|
1117
1441
|
*/
|
|
1118
1442
|
const warnUnsupportedJobOptions = (jobs, transport, logger = console) => {
|
|
1119
1443
|
if (!jobs) return;
|
|
1120
|
-
const retrying =
|
|
1444
|
+
const retrying = require_plugin_server.flattenJobs(jobs).filter(({ job }) => job.definition.retry !== void 0).map(({ jobKey }) => jobKey);
|
|
1121
1445
|
if (retrying.length === 0) return;
|
|
1122
1446
|
const named = retrying.map((jobKey) => `"${jobKey}"`).join(", ");
|
|
1123
1447
|
if (!transport) {
|
|
@@ -1134,7 +1458,7 @@ const DISPATCH_ROUTE_KEY = "kizuna:dispatch";
|
|
|
1134
1458
|
* The endpoint a scheduler ticks to run whichever jobs are due.
|
|
1135
1459
|
*/
|
|
1136
1460
|
const dispatchRoute = (meta) => {
|
|
1137
|
-
const identity =
|
|
1461
|
+
const identity = require_plugin_server.flattenJobs(meta.jobs).find(({ job }) => job.identity !== void 0)?.job.identity;
|
|
1138
1462
|
return {
|
|
1139
1463
|
method: meta.config?.method ?? "POST",
|
|
1140
1464
|
path: `${meta.config?.path ?? "/jobs"}/dispatch`,
|
|
@@ -1152,7 +1476,7 @@ const dispatchRoute = (meta) => {
|
|
|
1152
1476
|
const dispatchHandler = (meta) => {
|
|
1153
1477
|
const runner = createJobRunner(meta.jobs, meta.handlers, meta);
|
|
1154
1478
|
const thunks = {};
|
|
1155
|
-
for (const { jobKey } of
|
|
1479
|
+
for (const { jobKey } of require_plugin_server.flattenJobs(meta.jobs)) {
|
|
1156
1480
|
const jobFn = jobFnAt(runner, jobKey);
|
|
1157
1481
|
if (!jobFn) continue;
|
|
1158
1482
|
thunks[jobKey] = () => jobFn.run();
|
|
@@ -1185,7 +1509,7 @@ const RUN_ROUTE_KEY = "kizuna:run";
|
|
|
1185
1509
|
* The endpoint a queue delivers one job to, naming it in the body.
|
|
1186
1510
|
*/
|
|
1187
1511
|
const runRoute = (meta) => {
|
|
1188
|
-
const identity =
|
|
1512
|
+
const identity = require_plugin_server.flattenJobs(meta.jobs).find(({ job }) => job.identity !== void 0)?.job.identity;
|
|
1189
1513
|
return {
|
|
1190
1514
|
method: "POST",
|
|
1191
1515
|
path: `${meta.config?.path ?? "/jobs"}/run`,
|
|
@@ -1234,7 +1558,7 @@ const jobRunnerFrom = (meta) => meta ? createJobRunner(meta.jobs, meta.handlers,
|
|
|
1234
1558
|
const boundJobKeys = (meta) => {
|
|
1235
1559
|
const bound = /* @__PURE__ */ new Set();
|
|
1236
1560
|
if (!meta) return bound;
|
|
1237
|
-
for (const { jobKey } of
|
|
1561
|
+
for (const { jobKey } of require_plugin_server.flattenJobs(meta.jobs)) {
|
|
1238
1562
|
let handler = meta.handlers;
|
|
1239
1563
|
for (const segment of jobKey.split(".")) {
|
|
1240
1564
|
if (!handler || typeof handler !== "object") break;
|
|
@@ -1249,6 +1573,7 @@ const boundJobKeys = (meta) => {
|
|
|
1249
1573
|
* header, `undefined` when absent. For guards and request context resolvers on
|
|
1250
1574
|
* adapters that expose raw header records.
|
|
1251
1575
|
*/
|
|
1576
|
+
const streamsEvents = (route) => Object.values(route.responses).some((response) => require_plugin_server.isStreamResponse(response) && require_plugin_server.streamContentType(response) === "text/event-stream");
|
|
1252
1577
|
const getHeaderValue = (value) => {
|
|
1253
1578
|
if (typeof value === "string") return value;
|
|
1254
1579
|
if (Array.isArray(value) && typeof value[0] === "string") return value[0];
|
|
@@ -1270,7 +1595,7 @@ const parseCookies = (cookieHeader) => {
|
|
|
1270
1595
|
*/
|
|
1271
1596
|
const assertDeclaredBody = (route, routeKey, status, body) => {
|
|
1272
1597
|
const responseSpec = route.responses[status];
|
|
1273
|
-
if (responseSpec === void 0 ||
|
|
1598
|
+
if (responseSpec === void 0 || require_plugin_server.isStreamResponse(responseSpec)) return;
|
|
1274
1599
|
const bodySchema = "safeParse" in responseSpec ? responseSpec : responseSpec.body;
|
|
1275
1600
|
const wire = status >= 400 && body !== null && typeof body === "object" ? problemFromBody(status, body) : body;
|
|
1276
1601
|
const parsed = bodySchema.safeParse(wire);
|
|
@@ -1282,7 +1607,7 @@ const assertDeclaredBody = (route, routeKey, status, body) => {
|
|
|
1282
1607
|
*/
|
|
1283
1608
|
const requiresDenialBody = (route, detail) => {
|
|
1284
1609
|
const declared = route.responses[403];
|
|
1285
|
-
const filled = (declared === void 0 ||
|
|
1610
|
+
const filled = (declared === void 0 || require_plugin_server.isStreamResponse(declared) ? void 0 : "safeParse" in declared ? declared : declared.body)?.safeParse(problemDetails(403, detail));
|
|
1286
1611
|
return filled?.success ? filled.data : { detail };
|
|
1287
1612
|
};
|
|
1288
1613
|
/**
|
|
@@ -1338,8 +1663,8 @@ const extractCredential = (scheme, request) => {
|
|
|
1338
1663
|
const routerFromRoutes = (routes) => {
|
|
1339
1664
|
const router = {};
|
|
1340
1665
|
for (const [key, value] of Object.entries(routes)) {
|
|
1341
|
-
if (
|
|
1342
|
-
if (value[
|
|
1666
|
+
if (require_plugin_server.isRouteDefinition(value)) {
|
|
1667
|
+
if (value[require_plugin.HANDLER]) router[key] = value[require_plugin.HANDLER];
|
|
1343
1668
|
continue;
|
|
1344
1669
|
}
|
|
1345
1670
|
if (value && typeof value === "object") router[key] = routerFromRoutes(value);
|
|
@@ -1389,12 +1714,12 @@ const mediaTypeEssence = (value) => (value.split(";")[0] ?? "").trim().toLowerCa
|
|
|
1389
1714
|
const producibleMediaTypes = (route) => {
|
|
1390
1715
|
const types = /* @__PURE__ */ new Set();
|
|
1391
1716
|
for (const [statusKey, response] of Object.entries(route.responses)) {
|
|
1392
|
-
if (!
|
|
1393
|
-
const declared =
|
|
1717
|
+
if (!require_plugin_server.isSuccessStatus(Number(statusKey))) continue;
|
|
1718
|
+
const declared = require_plugin_server.resolveResponseContentType(response);
|
|
1394
1719
|
if (declared !== void 0) types.add(mediaTypeEssence(declared));
|
|
1395
|
-
else if (
|
|
1720
|
+
else if (require_plugin_server.isStreamResponse(response)) types.add(require_plugin_server.EVENT_STREAM_MEDIA_TYPE);
|
|
1396
1721
|
else {
|
|
1397
|
-
const body =
|
|
1722
|
+
const body = require_plugin_server.resolveResponseBody(response);
|
|
1398
1723
|
types.add(body !== void 0 && require_error_response.isBinarySchema(body) ? "application/octet-stream" : "application/json");
|
|
1399
1724
|
}
|
|
1400
1725
|
}
|
|
@@ -1427,7 +1752,7 @@ const withDeprecationHeaders = (result, route) => {
|
|
|
1427
1752
|
case "not-found":
|
|
1428
1753
|
case "method-not-allowed": return result;
|
|
1429
1754
|
default: {
|
|
1430
|
-
const announced =
|
|
1755
|
+
const announced = require_plugin_server.deprecationHeaders(route);
|
|
1431
1756
|
if (Object.keys(announced).length === 0) return result;
|
|
1432
1757
|
return {
|
|
1433
1758
|
...result,
|
|
@@ -1471,7 +1796,7 @@ const withCacheHeaders = (result, route) => {
|
|
|
1471
1796
|
if (result.kind === "raw-response" || result.kind === "not-found" || result.kind === "method-not-allowed") return result;
|
|
1472
1797
|
const status = statusOf(result);
|
|
1473
1798
|
if (status === void 0) return result;
|
|
1474
|
-
const policy =
|
|
1799
|
+
const policy = require_plugin_server.cacheHeaders(require_plugin_server.resolveResponseCache(route.responses[status]));
|
|
1475
1800
|
if (Object.keys(policy).length === 0) return result;
|
|
1476
1801
|
return {
|
|
1477
1802
|
...result,
|
|
@@ -1488,9 +1813,9 @@ const withCacheHeaders = (result, route) => {
|
|
|
1488
1813
|
*/
|
|
1489
1814
|
const responseBytes = (body, response) => {
|
|
1490
1815
|
if (typeof body === "string" || body instanceof Uint8Array) return body;
|
|
1491
|
-
const bodySchema = response !== void 0 ?
|
|
1816
|
+
const bodySchema = response !== void 0 ? require_plugin_server.resolveResponseBody(response) : void 0;
|
|
1492
1817
|
const isBinary = bodySchema !== void 0 && require_error_response.isBinarySchema(bodySchema);
|
|
1493
|
-
return
|
|
1818
|
+
return require_plugin_server.isJsonMediaType(require_plugin_server.resolveResponseContentType(response) ?? (isBinary ? "application/octet-stream" : "application/json")) ? JSON.stringify(body) : void 0;
|
|
1494
1819
|
};
|
|
1495
1820
|
/**
|
|
1496
1821
|
* Send the `ETag` a response declares, and turn the request into a
|
|
@@ -1501,7 +1826,7 @@ const responseBytes = (body, response) => {
|
|
|
1501
1826
|
const withEtag = async (result, route, request) => {
|
|
1502
1827
|
if (result.kind !== "success" || result.status >= 300 || result.body === void 0) return result;
|
|
1503
1828
|
const response = route.responses[result.status];
|
|
1504
|
-
if (!
|
|
1829
|
+
if (!require_plugin_server.resolveResponseEtag(response)) return result;
|
|
1505
1830
|
const bytes = responseBytes(result.body, response);
|
|
1506
1831
|
if (bytes === void 0) return result;
|
|
1507
1832
|
const etag = await computeEtag(bytes);
|
|
@@ -1540,6 +1865,20 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1540
1865
|
headers: request.headers,
|
|
1541
1866
|
body: void 0
|
|
1542
1867
|
};
|
|
1868
|
+
let rawBody;
|
|
1869
|
+
if (route.rawBody === true) {
|
|
1870
|
+
rawBody = request.readRawBody === void 0 ? void 0 : await request.readRawBody(route);
|
|
1871
|
+
if (rawBody === void 0) {
|
|
1872
|
+
const error = /* @__PURE__ */ new Error(`[kizuna] ${routeKey} needs the raw body, but it was parsed before kizuna saw it. On Express, mount kizuna before express.json(), or pass \`verify: keepRawBody\` to it.`);
|
|
1873
|
+
console.error(error.message);
|
|
1874
|
+
return {
|
|
1875
|
+
kind: "handler-error",
|
|
1876
|
+
routeKey,
|
|
1877
|
+
route,
|
|
1878
|
+
error
|
|
1879
|
+
};
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1543
1882
|
const acceptHeader = raw.headers["accept"];
|
|
1544
1883
|
if (!isAcceptable(acceptHeader, route)) return { kind: "not-acceptable" };
|
|
1545
1884
|
if (route.body && !require_error_response.isVoidSchema(route.body)) {
|
|
@@ -1554,7 +1893,7 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1554
1893
|
received
|
|
1555
1894
|
};
|
|
1556
1895
|
try {
|
|
1557
|
-
raw.body = await request.readBody(route);
|
|
1896
|
+
raw.body = rawBody !== void 0 ? parseBodyText(rawBody, route) : await request.readBody(route);
|
|
1558
1897
|
} catch {
|
|
1559
1898
|
return {
|
|
1560
1899
|
kind: "invalid-body",
|
|
@@ -1563,9 +1902,9 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1563
1902
|
}
|
|
1564
1903
|
}
|
|
1565
1904
|
}
|
|
1566
|
-
const validation =
|
|
1905
|
+
const validation = require_plugin_server.validateRequest(route, raw);
|
|
1567
1906
|
if (!validation.ok) {
|
|
1568
|
-
const formatted =
|
|
1907
|
+
const formatted = require_plugin_server.formatValidationError(validation.error);
|
|
1569
1908
|
return {
|
|
1570
1909
|
kind: "validation-failed",
|
|
1571
1910
|
stage: validation.error.stage,
|
|
@@ -1573,18 +1912,85 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1573
1912
|
issues: formatted.issues
|
|
1574
1913
|
};
|
|
1575
1914
|
}
|
|
1576
|
-
|
|
1915
|
+
return invokeRoute({
|
|
1916
|
+
resolved,
|
|
1917
|
+
parsed: validation.parsed,
|
|
1918
|
+
handler: resolveHandler(router, routeKey),
|
|
1919
|
+
request,
|
|
1920
|
+
routes,
|
|
1921
|
+
definition,
|
|
1922
|
+
handlerContext: () => definition.buildHandlerContext(request, responseContext),
|
|
1923
|
+
guards,
|
|
1924
|
+
schemes,
|
|
1925
|
+
contextResolvers,
|
|
1926
|
+
pluginExports,
|
|
1927
|
+
jobRunner,
|
|
1928
|
+
responseValidation,
|
|
1929
|
+
rawBody
|
|
1930
|
+
});
|
|
1931
|
+
};
|
|
1932
|
+
const streamToolsOfRoute = (route) => {
|
|
1933
|
+
for (const response of Object.values(route.responses)) {
|
|
1934
|
+
const tools = streamToolsOf(response);
|
|
1935
|
+
if (tools !== void 0) return tools;
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
/**
|
|
1939
|
+
* What a tool call reports: the status and body HTTP would have sent.
|
|
1940
|
+
*/
|
|
1941
|
+
const toolAnswer = (result) => {
|
|
1942
|
+
if (result.kind === "raw-response") return {
|
|
1943
|
+
status: 500,
|
|
1944
|
+
body: problemDetails(500, "This route answers with a raw response, and a tool answers with a JSON body.")
|
|
1945
|
+
};
|
|
1946
|
+
if (result.kind === "handler-error") console.error(`[kizuna] tool "${result.routeKey}" threw:`, result.error);
|
|
1947
|
+
const rendered = renderJsonResult(result);
|
|
1948
|
+
return {
|
|
1949
|
+
status: rendered.status,
|
|
1950
|
+
body: rendered.body
|
|
1951
|
+
};
|
|
1952
|
+
};
|
|
1953
|
+
/**
|
|
1954
|
+
* Each call runs its route as a request of its own, from the same caller.
|
|
1955
|
+
*/
|
|
1956
|
+
const streamToolsFor = (tools, invocation, handlerContext) => createStreamTools(tools, {
|
|
1957
|
+
headers: invocation.request.headers,
|
|
1958
|
+
invoke: async (entry, parsed, params) => toolAnswer(await invokeRoute({
|
|
1959
|
+
...invocation,
|
|
1960
|
+
resolved: {
|
|
1961
|
+
routeKey: entry.routeKey,
|
|
1962
|
+
route: entry.route,
|
|
1963
|
+
params
|
|
1964
|
+
},
|
|
1965
|
+
parsed,
|
|
1966
|
+
handler: entry.route[require_plugin.HANDLER],
|
|
1967
|
+
handlerContext: () => handlerContext
|
|
1968
|
+
})),
|
|
1969
|
+
render: (failure) => {
|
|
1970
|
+
const formatted = require_plugin_server.formatValidationError(failure);
|
|
1971
|
+
return toolAnswer({
|
|
1972
|
+
kind: "validation-failed",
|
|
1973
|
+
stage: failure.stage,
|
|
1974
|
+
detail: formatted.detail,
|
|
1975
|
+
issues: formatted.issues
|
|
1976
|
+
});
|
|
1977
|
+
}
|
|
1978
|
+
});
|
|
1979
|
+
const invokeRoute = async (invocation) => {
|
|
1980
|
+
const { resolved, parsed, request, routes, definition, guards, schemes, contextResolvers, pluginExports, jobRunner, responseValidation } = invocation;
|
|
1981
|
+
const { routeKey, route, params } = resolved;
|
|
1982
|
+
const handler = invocation.handler;
|
|
1577
1983
|
if (typeof handler !== "function") return {
|
|
1578
1984
|
kind: "no-handler",
|
|
1579
1985
|
routeKey
|
|
1580
1986
|
};
|
|
1581
1987
|
try {
|
|
1582
|
-
const handlerContext = await
|
|
1988
|
+
const handlerContext = await invocation.handlerContext();
|
|
1583
1989
|
const requestContext = {};
|
|
1584
1990
|
if (contextResolvers) for (const [name, resolver] of Object.entries(contextResolvers)) requestContext[name] = await resolver({
|
|
1585
1991
|
...handlerContext,
|
|
1586
1992
|
params,
|
|
1587
|
-
headers:
|
|
1993
|
+
headers: request.headers
|
|
1588
1994
|
});
|
|
1589
1995
|
const hasRequestContext = Object.keys(requestContext).length > 0;
|
|
1590
1996
|
const securityContext = {};
|
|
@@ -1636,13 +2042,17 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1636
2042
|
const throwError = (response) => {
|
|
1637
2043
|
throw new ResponseError(response);
|
|
1638
2044
|
};
|
|
2045
|
+
const tools = streamToolsOfRoute(route);
|
|
1639
2046
|
const handlerResult = await handler({
|
|
1640
|
-
params:
|
|
1641
|
-
query:
|
|
1642
|
-
body:
|
|
1643
|
-
headers:
|
|
2047
|
+
params: parsed.params,
|
|
2048
|
+
query: parsed.query,
|
|
2049
|
+
body: parsed.body,
|
|
2050
|
+
headers: parsed.headers,
|
|
2051
|
+
...route.rawBody === true ? { rawBody: invocation.rawBody } : {},
|
|
2052
|
+
...streamsEvents(route) ? { lastEventId: getHeaderValue(invocation.request.headers["last-event-id"]) } : {},
|
|
1644
2053
|
throwError,
|
|
1645
2054
|
...handlerContext,
|
|
2055
|
+
...tools ? { tools: streamToolsFor(tools, invocation, handlerContext) } : {},
|
|
1646
2056
|
...jobRunner ? { jobs: jobRunner } : {},
|
|
1647
2057
|
...hasRequestContext ? { requestContext } : {},
|
|
1648
2058
|
...Object.keys(securityContext).length > 0 ? { auth: securityContext } : {},
|
|
@@ -1654,7 +2064,7 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1654
2064
|
};
|
|
1655
2065
|
if (responseValidation) assertDeclaredBody(route, routeKey, handlerResult.status, handlerResult.body);
|
|
1656
2066
|
const successHeaders = route.method === "OPTIONS" ? {
|
|
1657
|
-
allow:
|
|
2067
|
+
allow: require_plugin_server.allowedMethodsForPath(routes, route.path).join(", "),
|
|
1658
2068
|
...handlerResult.headers ?? {}
|
|
1659
2069
|
} : handlerResult.headers;
|
|
1660
2070
|
return {
|
|
@@ -1688,13 +2098,13 @@ const routedPipeline = async (resolved, request, routes, router, definition, res
|
|
|
1688
2098
|
};
|
|
1689
2099
|
}
|
|
1690
2100
|
};
|
|
1691
|
-
const
|
|
2101
|
+
const defineAdapter = (definition) => ({
|
|
1692
2102
|
handle: async ({ routes, router, request, responseContext, guards, schemes, requestContext, pluginExports, jobs, basePath, responseValidation }) => {
|
|
1693
2103
|
const result = await runPipeline(request, routes, router, definition, responseContext, guards, schemes, requestContext, pluginExports, jobs, basePath, responseValidation);
|
|
1694
2104
|
return definition.respond(result, responseContext);
|
|
1695
2105
|
},
|
|
1696
2106
|
eachRoute: function* (routes, router) {
|
|
1697
|
-
const sorted = sortFlattenedRoutes(
|
|
2107
|
+
const sorted = sortFlattenedRoutes(require_plugin_server.flattenRoutes(routes));
|
|
1698
2108
|
let mounted = 0;
|
|
1699
2109
|
for (const { routeKey, route } of sorted) {
|
|
1700
2110
|
const handler = resolveHandler(router, routeKey);
|
|
@@ -1768,17 +2178,17 @@ const renderResult = (result, formatError, request) => {
|
|
|
1768
2178
|
return renderError(result.status, problem.detail, problem, result.headers);
|
|
1769
2179
|
}
|
|
1770
2180
|
const responseSpec = result.route.responses[result.status];
|
|
1771
|
-
if (responseSpec !== void 0 &&
|
|
2181
|
+
if (responseSpec !== void 0 && require_plugin_server.isStreamResponse(responseSpec)) {
|
|
1772
2182
|
const streamResult = result;
|
|
1773
2183
|
return {
|
|
1774
2184
|
status: result.status,
|
|
1775
2185
|
headers: {
|
|
1776
|
-
"content-type":
|
|
2186
|
+
"content-type": require_plugin_server.streamContentType(responseSpec),
|
|
1777
2187
|
"cache-control": "no-store",
|
|
1778
2188
|
...result.headers ?? {}
|
|
1779
2189
|
},
|
|
1780
2190
|
body: void 0,
|
|
1781
|
-
stream: (options) =>
|
|
2191
|
+
stream: (options) => require_plugin_server.encodeStreamBody(streamResult, options)
|
|
1782
2192
|
};
|
|
1783
2193
|
}
|
|
1784
2194
|
if (result.body === void 0) return {
|
|
@@ -1786,10 +2196,10 @@ const renderResult = (result, formatError, request) => {
|
|
|
1786
2196
|
headers: { ...result.headers ?? {} },
|
|
1787
2197
|
body: result.body
|
|
1788
2198
|
};
|
|
1789
|
-
const bodySchema = responseSpec !== void 0 ?
|
|
2199
|
+
const bodySchema = responseSpec !== void 0 ? require_plugin_server.resolveResponseBody(responseSpec) : void 0;
|
|
1790
2200
|
const isBinary = bodySchema !== void 0 && require_error_response.isBinarySchema(bodySchema);
|
|
1791
|
-
const contentType =
|
|
1792
|
-
const raw = isBinary || !
|
|
2201
|
+
const contentType = require_plugin_server.resolveResponseContentType(responseSpec) ?? (isBinary ? "application/octet-stream" : "application/json");
|
|
2202
|
+
const raw = isBinary || !require_plugin_server.isJsonMediaType(contentType);
|
|
1793
2203
|
if (raw && typeof result.body !== "string" && !(result.body instanceof Uint8Array)) throw new Error(`${result.routeKey} (status ${result.status}) is declared with content type "${contentType}", so its body must be a string or Uint8Array, but the handler returned ${describeBodyType(result.body)}.`);
|
|
1794
2204
|
return {
|
|
1795
2205
|
status: result.status,
|
|
@@ -1827,6 +2237,14 @@ const formDataToObject = (form) => {
|
|
|
1827
2237
|
return result;
|
|
1828
2238
|
};
|
|
1829
2239
|
/**
|
|
2240
|
+
* Parse a body from its text, the way {@link parseFetchBody} would, for a route
|
|
2241
|
+
* whose text core already read.
|
|
2242
|
+
*/
|
|
2243
|
+
const parseBodyText = (text, route) => {
|
|
2244
|
+
if (route.contentType === "application/x-www-form-urlencoded") return Object.fromEntries(new URLSearchParams(text));
|
|
2245
|
+
return text.length > 0 ? JSON.parse(text) : void 0;
|
|
2246
|
+
};
|
|
2247
|
+
/**
|
|
1830
2248
|
* Content-type-aware body parser for Web Fetch `Request`.
|
|
1831
2249
|
*
|
|
1832
2250
|
* Reusable across any Fetch-based adapter.
|
|
@@ -1964,6 +2382,12 @@ Object.defineProperty(exports, "assembleApi", {
|
|
|
1964
2382
|
return assembleApi;
|
|
1965
2383
|
}
|
|
1966
2384
|
});
|
|
2385
|
+
Object.defineProperty(exports, "assembleRoutes", {
|
|
2386
|
+
enumerable: true,
|
|
2387
|
+
get: function() {
|
|
2388
|
+
return assembleRoutes;
|
|
2389
|
+
}
|
|
2390
|
+
});
|
|
1967
2391
|
Object.defineProperty(exports, "assertNoPathCollisions", {
|
|
1968
2392
|
enumerable: true,
|
|
1969
2393
|
get: function() {
|
|
@@ -1982,28 +2406,34 @@ Object.defineProperty(exports, "boundJobKeys", {
|
|
|
1982
2406
|
return boundJobKeys;
|
|
1983
2407
|
}
|
|
1984
2408
|
});
|
|
1985
|
-
Object.defineProperty(exports, "
|
|
2409
|
+
Object.defineProperty(exports, "buildToolDefinitions", {
|
|
1986
2410
|
enumerable: true,
|
|
1987
2411
|
get: function() {
|
|
1988
|
-
return
|
|
2412
|
+
return buildToolDefinitions;
|
|
1989
2413
|
}
|
|
1990
2414
|
});
|
|
1991
|
-
Object.defineProperty(exports, "
|
|
2415
|
+
Object.defineProperty(exports, "buildToolInputSchema", {
|
|
1992
2416
|
enumerable: true,
|
|
1993
2417
|
get: function() {
|
|
1994
|
-
return
|
|
2418
|
+
return buildToolInputSchema;
|
|
1995
2419
|
}
|
|
1996
2420
|
});
|
|
1997
|
-
Object.defineProperty(exports, "
|
|
2421
|
+
Object.defineProperty(exports, "contractOf", {
|
|
1998
2422
|
enumerable: true,
|
|
1999
2423
|
get: function() {
|
|
2000
|
-
return
|
|
2424
|
+
return contractOf;
|
|
2425
|
+
}
|
|
2426
|
+
});
|
|
2427
|
+
Object.defineProperty(exports, "createGroups", {
|
|
2428
|
+
enumerable: true,
|
|
2429
|
+
get: function() {
|
|
2430
|
+
return createGroups;
|
|
2001
2431
|
}
|
|
2002
2432
|
});
|
|
2003
|
-
Object.defineProperty(exports, "
|
|
2433
|
+
Object.defineProperty(exports, "createJobRunner", {
|
|
2004
2434
|
enumerable: true,
|
|
2005
2435
|
get: function() {
|
|
2006
|
-
return
|
|
2436
|
+
return createJobRunner;
|
|
2007
2437
|
}
|
|
2008
2438
|
});
|
|
2009
2439
|
Object.defineProperty(exports, "createPermissions", {
|
|
@@ -2018,10 +2448,22 @@ Object.defineProperty(exports, "createRoles", {
|
|
|
2018
2448
|
return createRoles;
|
|
2019
2449
|
}
|
|
2020
2450
|
});
|
|
2021
|
-
Object.defineProperty(exports, "
|
|
2451
|
+
Object.defineProperty(exports, "defineAdapter", {
|
|
2452
|
+
enumerable: true,
|
|
2453
|
+
get: function() {
|
|
2454
|
+
return defineAdapter;
|
|
2455
|
+
}
|
|
2456
|
+
});
|
|
2457
|
+
Object.defineProperty(exports, "defineJobTransport", {
|
|
2458
|
+
enumerable: true,
|
|
2459
|
+
get: function() {
|
|
2460
|
+
return defineJobTransport;
|
|
2461
|
+
}
|
|
2462
|
+
});
|
|
2463
|
+
Object.defineProperty(exports, "describeTool", {
|
|
2022
2464
|
enumerable: true,
|
|
2023
2465
|
get: function() {
|
|
2024
|
-
return
|
|
2466
|
+
return describeTool;
|
|
2025
2467
|
}
|
|
2026
2468
|
});
|
|
2027
2469
|
Object.defineProperty(exports, "dispatchDueJobs", {
|
|
@@ -2084,6 +2526,12 @@ Object.defineProperty(exports, "getStatusText", {
|
|
|
2084
2526
|
return getStatusText;
|
|
2085
2527
|
}
|
|
2086
2528
|
});
|
|
2529
|
+
Object.defineProperty(exports, "groupRoutes", {
|
|
2530
|
+
enumerable: true,
|
|
2531
|
+
get: function() {
|
|
2532
|
+
return groupRoutes;
|
|
2533
|
+
}
|
|
2534
|
+
});
|
|
2087
2535
|
Object.defineProperty(exports, "guardDenyFor", {
|
|
2088
2536
|
enumerable: true,
|
|
2089
2537
|
get: function() {
|
|
@@ -2102,6 +2550,12 @@ Object.defineProperty(exports, "insufficientScope", {
|
|
|
2102
2550
|
return insufficientScope;
|
|
2103
2551
|
}
|
|
2104
2552
|
});
|
|
2553
|
+
Object.defineProperty(exports, "isGroupSet", {
|
|
2554
|
+
enumerable: true,
|
|
2555
|
+
get: function() {
|
|
2556
|
+
return isGroupSet;
|
|
2557
|
+
}
|
|
2558
|
+
});
|
|
2105
2559
|
Object.defineProperty(exports, "isGuardDenial", {
|
|
2106
2560
|
enumerable: true,
|
|
2107
2561
|
get: function() {
|
|
@@ -2126,12 +2580,6 @@ Object.defineProperty(exports, "isRoles", {
|
|
|
2126
2580
|
return isRoles;
|
|
2127
2581
|
}
|
|
2128
2582
|
});
|
|
2129
|
-
Object.defineProperty(exports, "isTagSet", {
|
|
2130
|
-
enumerable: true,
|
|
2131
|
-
get: function() {
|
|
2132
|
-
return isTagSet;
|
|
2133
|
-
}
|
|
2134
|
-
});
|
|
2135
2583
|
Object.defineProperty(exports, "jobFnAt", {
|
|
2136
2584
|
enumerable: true,
|
|
2137
2585
|
get: function() {
|
|
@@ -2162,34 +2610,22 @@ Object.defineProperty(exports, "matchRoute", {
|
|
|
2162
2610
|
return matchRoute;
|
|
2163
2611
|
}
|
|
2164
2612
|
});
|
|
2165
|
-
Object.defineProperty(exports, "
|
|
2166
|
-
enumerable: true,
|
|
2167
|
-
get: function() {
|
|
2168
|
-
return parseFetchBody;
|
|
2169
|
-
}
|
|
2170
|
-
});
|
|
2171
|
-
Object.defineProperty(exports, "permissionNames", {
|
|
2172
|
-
enumerable: true,
|
|
2173
|
-
get: function() {
|
|
2174
|
-
return permissionNames;
|
|
2175
|
-
}
|
|
2176
|
-
});
|
|
2177
|
-
Object.defineProperty(exports, "pluginExportsOf", {
|
|
2613
|
+
Object.defineProperty(exports, "parseBodyText", {
|
|
2178
2614
|
enumerable: true,
|
|
2179
2615
|
get: function() {
|
|
2180
|
-
return
|
|
2616
|
+
return parseBodyText;
|
|
2181
2617
|
}
|
|
2182
2618
|
});
|
|
2183
|
-
Object.defineProperty(exports, "
|
|
2619
|
+
Object.defineProperty(exports, "parseFetchBody", {
|
|
2184
2620
|
enumerable: true,
|
|
2185
2621
|
get: function() {
|
|
2186
|
-
return
|
|
2622
|
+
return parseFetchBody;
|
|
2187
2623
|
}
|
|
2188
2624
|
});
|
|
2189
|
-
Object.defineProperty(exports, "
|
|
2625
|
+
Object.defineProperty(exports, "permissionNames", {
|
|
2190
2626
|
enumerable: true,
|
|
2191
2627
|
get: function() {
|
|
2192
|
-
return
|
|
2628
|
+
return permissionNames;
|
|
2193
2629
|
}
|
|
2194
2630
|
});
|
|
2195
2631
|
Object.defineProperty(exports, "problemDetails", {
|
|
@@ -2222,12 +2658,6 @@ Object.defineProperty(exports, "requiresDenial", {
|
|
|
2222
2658
|
return requiresDenial;
|
|
2223
2659
|
}
|
|
2224
2660
|
});
|
|
2225
|
-
Object.defineProperty(exports, "resolvePluginServers", {
|
|
2226
|
-
enumerable: true,
|
|
2227
|
-
get: function() {
|
|
2228
|
-
return resolvePluginServers;
|
|
2229
|
-
}
|
|
2230
|
-
});
|
|
2231
2661
|
Object.defineProperty(exports, "routeClaims", {
|
|
2232
2662
|
enumerable: true,
|
|
2233
2663
|
get: function() {
|
|
@@ -2252,16 +2682,16 @@ Object.defineProperty(exports, "runRoute", {
|
|
|
2252
2682
|
return runRoute;
|
|
2253
2683
|
}
|
|
2254
2684
|
});
|
|
2255
|
-
Object.defineProperty(exports, "
|
|
2685
|
+
Object.defineProperty(exports, "selectToolRoutes", {
|
|
2256
2686
|
enumerable: true,
|
|
2257
2687
|
get: function() {
|
|
2258
|
-
return
|
|
2688
|
+
return selectToolRoutes;
|
|
2259
2689
|
}
|
|
2260
2690
|
});
|
|
2261
|
-
Object.defineProperty(exports, "
|
|
2691
|
+
Object.defineProperty(exports, "sortFlattenedRoutes", {
|
|
2262
2692
|
enumerable: true,
|
|
2263
2693
|
get: function() {
|
|
2264
|
-
return
|
|
2694
|
+
return sortFlattenedRoutes;
|
|
2265
2695
|
}
|
|
2266
2696
|
});
|
|
2267
2697
|
Object.defineProperty(exports, "toolEvents", {
|
|
@@ -2270,6 +2700,18 @@ Object.defineProperty(exports, "toolEvents", {
|
|
|
2270
2700
|
return toolEvents;
|
|
2271
2701
|
}
|
|
2272
2702
|
});
|
|
2703
|
+
Object.defineProperty(exports, "toolOptionsOf", {
|
|
2704
|
+
enumerable: true,
|
|
2705
|
+
get: function() {
|
|
2706
|
+
return toolOptionsOf;
|
|
2707
|
+
}
|
|
2708
|
+
});
|
|
2709
|
+
Object.defineProperty(exports, "toolRefusal", {
|
|
2710
|
+
enumerable: true,
|
|
2711
|
+
get: function() {
|
|
2712
|
+
return toolRefusal;
|
|
2713
|
+
}
|
|
2714
|
+
});
|
|
2273
2715
|
Object.defineProperty(exports, "warnUnsupportedJobOptions", {
|
|
2274
2716
|
enumerable: true,
|
|
2275
2717
|
get: function() {
|