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
package/dist/generator.mjs
CHANGED
|
@@ -1,80 +1,171 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
//#region src/
|
|
1
|
+
import { C as readMetaExamples, D as unwrapOptionalWrappers, S as readMetaDescription, T as readObjectShape, _ as readDiscriminatedUnion, b as readMeta, c as isBinarySchema, d as isIntegerSchema, f as isObjectSchema, g as readDeprecation, h as readDefType, i as FILE_PROBE, l as isDiscriminatedUnionSchema, m as readDef, p as isVoidSchema, s as globalRegistrySchemas, u as isFileSchema, v as readDiscriminatorLiteral, w as readMetaId, x as readMetaBrand, y as readDiscriminatorStringLiteral } from "./error-response-CNQaKVYd.mjs";
|
|
2
|
+
import { $ as resolveResponseStream, A as toToolName, B as streamStatuses, F as routeStreams, G as isZodSchema, H as isJsonMediaType, I as soleStreamResponse, J as resolveResponseBody, K as localTypeName, L as streamContentType, P as isNamedStream, Q as resolveResponseHeaders, R as streamMode, U as isStreamResponse, V as isHintPrefix, W as isSuccessStatus, X as resolveResponseContentType, Y as resolveResponseCache, Z as resolveResponseEtag, _ as jobAt, a as pluginRoutesOf, at as toPascalCase, c as cacheHeaders, dt as isRouteDefinition, et as sanitizeFieldName, h as isCompiledJob, it as toCamelCase, j as EVENT_STREAM_MEDIA_TYPE, k as deriveToolNames, lt as flattenRoutes, m as flattenJobs, n as describeIssue, nt as shortTypeName, q as mergeHeaderFields, rt as statusToCamelCase, st as parsePath, tt as sanitizeIdentifier, u as deprecationHeaders, z as streamSchemas } from "./plugin-server-BztlE1fz.mjs";
|
|
3
|
+
//#region src/generator.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @see https://modelcontextprotocol.io/specification/latest/server/tools
|
|
5
|
+
* Every route the api serves, the app's and the plugins' under their slugs,
|
|
6
|
+
* each with the plugin it came from.
|
|
8
7
|
*/
|
|
9
|
-
const
|
|
8
|
+
const servedRoutes = (contract) => [...flattenRoutes(contract.routes).map((entry) => ({
|
|
9
|
+
...entry,
|
|
10
|
+
plugin: void 0
|
|
11
|
+
})), ...Object.entries(pluginRoutesOf(contract)).flatMap(([slug, routes]) => flattenRoutes(routes, slug).map((entry) => ({
|
|
12
|
+
...entry,
|
|
13
|
+
plugin: { slug }
|
|
14
|
+
})))];
|
|
15
|
+
const withoutHidden = (routes) => {
|
|
16
|
+
const listed = {};
|
|
17
|
+
for (const symbol of Object.getOwnPropertySymbols(routes)) listed[symbol] = routes[symbol];
|
|
18
|
+
for (const [key, value] of Object.entries(routes)) {
|
|
19
|
+
if (isRouteDefinition(value)) {
|
|
20
|
+
if (value.hidden !== true) listed[key] = value;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (value && typeof value === "object") {
|
|
24
|
+
const group = withoutHidden(value);
|
|
25
|
+
if (Object.keys(group).length > 0) listed[key] = group;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return listed;
|
|
29
|
+
};
|
|
10
30
|
/**
|
|
11
|
-
* The
|
|
12
|
-
*
|
|
13
|
-
* reject the whole tool list over one name outside it, so the dot does not
|
|
14
|
-
* survive here.
|
|
31
|
+
* The route tree the generated clients hold: the app's routes, less every
|
|
32
|
+
* `hidden` one.
|
|
15
33
|
*/
|
|
16
|
-
const
|
|
17
|
-
const
|
|
34
|
+
const listedRoutes = (contract) => withoutHidden(contract.routes);
|
|
35
|
+
const isSchema = (value) => typeof value === "object" && value !== null && "_zod" in value && typeof value._zod?.def?.type === "string";
|
|
18
36
|
/**
|
|
19
|
-
* `
|
|
20
|
-
* and
|
|
37
|
+
* Every `Kizuna.model` schema reachable from a value, found by walking its
|
|
38
|
+
* schemas and whatever plain objects and lists hold them.
|
|
21
39
|
*/
|
|
22
|
-
const
|
|
23
|
-
|
|
40
|
+
const reachModels = (value, models, found, seen) => {
|
|
41
|
+
if (typeof value !== "object" || value === null || seen.has(value)) return;
|
|
42
|
+
seen.add(value);
|
|
43
|
+
if (isSchema(value)) {
|
|
44
|
+
if (models.has(value)) found.add(value);
|
|
45
|
+
const def = value._zod.def;
|
|
46
|
+
for (const [key, child] of Object.entries(def)) {
|
|
47
|
+
if (key === "checks") continue;
|
|
48
|
+
if (key === "getter" && typeof child === "function") {
|
|
49
|
+
reachModels(child(), models, found, seen);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
reachModels(child, models, found, seen);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
for (const child of Array.isArray(value) ? value : Object.values(value)) reachModels(child, models, found, seen);
|
|
57
|
+
};
|
|
24
58
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
59
|
+
* The named models the given routes use, sorted by name, each with the plugin
|
|
60
|
+
* that brought it when none of the app's own routes use it.
|
|
27
61
|
*/
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
if (claimant !== void 0) throw new Error(`${sentenceLabel(claimant.origin)} "${claimant.key}" and ${origin} "${key}" both become the tool name "${name}". Rename one of them.`);
|
|
38
|
-
claimedBy.set(name, entry);
|
|
39
|
-
names.set(key, name);
|
|
62
|
+
const modelsOf = (entries) => {
|
|
63
|
+
const models = /* @__PURE__ */ new Map();
|
|
64
|
+
for (const [id, schema] of globalRegistrySchemas()) models.set(schema, id);
|
|
65
|
+
const owners = /* @__PURE__ */ new Map();
|
|
66
|
+
const appFirst = [...entries].sort((left, right) => Number(left.plugin !== void 0) - Number(right.plugin !== void 0));
|
|
67
|
+
for (const { route, plugin } of appFirst) {
|
|
68
|
+
const found = /* @__PURE__ */ new Set();
|
|
69
|
+
reachModels(route, models, found, /* @__PURE__ */ new Set());
|
|
70
|
+
for (const schema of found) if (!owners.has(schema)) owners.set(schema, plugin);
|
|
40
71
|
}
|
|
41
|
-
return
|
|
72
|
+
return [...owners].map(([schema, plugin]) => ({
|
|
73
|
+
name: models.get(schema),
|
|
74
|
+
schema,
|
|
75
|
+
description: readMetaDescription(schema),
|
|
76
|
+
plugin
|
|
77
|
+
})).sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0);
|
|
42
78
|
};
|
|
43
|
-
|
|
44
|
-
//#region src/generator.ts
|
|
79
|
+
const deprecationMessageOf = (route) => typeof route.deprecated === "string" ? route.deprecated : typeof route.deprecated === "object" ? route.deprecated.message : void 0;
|
|
45
80
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* Centralises routes walking and deprecation resolution so generator authors
|
|
49
|
-
* only need to implement `processRoute` and `finalize`. Field-level deprecation
|
|
50
|
-
* lives on the schemas themselves, read with `readDeprecation`.
|
|
81
|
+
* Run a walk over an api: its models, then every route it serves, the app's
|
|
82
|
+
* and each plugin's under its slug. Returns what `finalize` returns.
|
|
51
83
|
*
|
|
52
|
-
*
|
|
53
|
-
* const
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* processRoute({ route, deprecated }) {
|
|
57
|
-
* routeList.push(`${route.method} ${route.path}${deprecated ? ' (deprecated)' : ''}`);
|
|
58
|
-
* },
|
|
59
|
-
* finalize() {
|
|
60
|
-
* return routeList;
|
|
61
|
-
* },
|
|
62
|
-
* };
|
|
84
|
+
* @example
|
|
85
|
+
* const routeList = walkApi(api, {
|
|
86
|
+
* processRoute: ({ route }) => lines.push(`${route.method} ${route.path}`),
|
|
87
|
+
* finalize: () => lines.join('\n'),
|
|
63
88
|
* });
|
|
64
|
-
*
|
|
65
|
-
* const list = generateRouteList(kizuna.api, {});
|
|
66
|
-
* ```
|
|
67
89
|
*/
|
|
68
|
-
const
|
|
69
|
-
const {
|
|
70
|
-
|
|
90
|
+
const walkApi = (api, walk, options = {}) => {
|
|
91
|
+
const entries = servedRoutes(api).filter(({ route }) => options.skipHidden !== true || route.hidden !== true);
|
|
92
|
+
if (walk.processModel !== void 0) for (const model of modelsOf(entries)) walk.processModel(model);
|
|
93
|
+
for (const { routeKey, route, routeGroups, plugin } of entries) walk.processRoute?.({
|
|
71
94
|
routeKey,
|
|
72
95
|
route,
|
|
73
|
-
|
|
96
|
+
routeGroups,
|
|
97
|
+
hidden: route.hidden === true,
|
|
98
|
+
plugin,
|
|
74
99
|
deprecated: route.deprecated !== void 0 && route.deprecated !== false,
|
|
75
|
-
deprecationMessage:
|
|
100
|
+
deprecationMessage: deprecationMessageOf(route)
|
|
76
101
|
});
|
|
77
|
-
return finalize();
|
|
102
|
+
return walk.finalize();
|
|
103
|
+
};
|
|
104
|
+
const parseOptions = (schema, input, name) => {
|
|
105
|
+
if (schema === void 0) return input;
|
|
106
|
+
const parsed = schema.safeParse(input);
|
|
107
|
+
if (!parsed.success) throw new Error(`[kizuna] ${name} has invalid options: ${parsed.error.issues.map(describeIssue).join("; ")}`);
|
|
108
|
+
return parsed.data;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* Declare a client for a language: the options an app passes it, and the walk
|
|
112
|
+
* that returns its file. Returns the function an app names under `clients`.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* export const dartClient = defineClient({
|
|
116
|
+
* target: 'dart',
|
|
117
|
+
* options: z.object({
|
|
118
|
+
* library: z.string().default('api'),
|
|
119
|
+
* }),
|
|
120
|
+
* generate: ({ options }) => {
|
|
121
|
+
* const writer = new DartWriter(options.library);
|
|
122
|
+
*
|
|
123
|
+
* return {
|
|
124
|
+
* processModel: (model) => writer.model(model),
|
|
125
|
+
* processRoute: (context) => writer.method(context),
|
|
126
|
+
* finalize: () => writer.render(),
|
|
127
|
+
* };
|
|
128
|
+
* },
|
|
129
|
+
* });
|
|
130
|
+
*/
|
|
131
|
+
const defineClient = (definition) => (input) => {
|
|
132
|
+
const { output, ...rest } = input;
|
|
133
|
+
const options = parseOptions(definition.options, rest, `Client '${definition.target}'`);
|
|
134
|
+
return {
|
|
135
|
+
target: definition.target,
|
|
136
|
+
output,
|
|
137
|
+
render: (api) => walkApi(api, definition.generate({
|
|
138
|
+
options,
|
|
139
|
+
api
|
|
140
|
+
}), { skipHidden: true })
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Declare a generator for a file that isn't a client, like a document or a
|
|
145
|
+
* manifest. A plugin installs it by returning it from `setup`.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* export const gatewayManifest = defineGenerator({
|
|
149
|
+
* generate: () => {
|
|
150
|
+
* const entries: string[] = [];
|
|
151
|
+
*
|
|
152
|
+
* return {
|
|
153
|
+
* processRoute: ({ route }) => entries.push(`${route.method} ${route.path}`),
|
|
154
|
+
* finalize: () => entries.join('\n'),
|
|
155
|
+
* };
|
|
156
|
+
* },
|
|
157
|
+
* });
|
|
158
|
+
*/
|
|
159
|
+
const defineGenerator = (definition) => (input) => {
|
|
160
|
+
const { output, ...rest } = input;
|
|
161
|
+
const options = parseOptions(definition.options, rest, `The generator for '${output}'`);
|
|
162
|
+
return {
|
|
163
|
+
output,
|
|
164
|
+
render: (api) => walkApi(api, definition.generate({
|
|
165
|
+
options,
|
|
166
|
+
api
|
|
167
|
+
}))
|
|
168
|
+
};
|
|
78
169
|
};
|
|
79
170
|
//#endregion
|
|
80
|
-
export { EVENT_STREAM_MEDIA_TYPE, FILE_PROBE, cacheHeaders,
|
|
171
|
+
export { EVENT_STREAM_MEDIA_TYPE, FILE_PROBE, cacheHeaders, defineClient, defineGenerator, deprecationHeaders, deriveToolNames, flattenJobs, globalRegistrySchemas, isBinarySchema, isCompiledJob, isDiscriminatedUnionSchema, isFileSchema, isHintPrefix, isIntegerSchema, isJsonMediaType, isNamedStream, isObjectSchema, isStreamResponse, isSuccessStatus, isVoidSchema, isZodSchema, jobAt, listedRoutes, localTypeName, mergeHeaderFields, parsePath, readDef, readDefType, readDeprecation, readDiscriminatedUnion, readDiscriminatorLiteral, readDiscriminatorStringLiteral, readMeta, readMetaBrand, readMetaDescription, readMetaExamples, readMetaId, readObjectShape, resolveResponseBody, resolveResponseCache, resolveResponseContentType, resolveResponseEtag, resolveResponseHeaders, resolveResponseStream, routeStreams, sanitizeFieldName, sanitizeIdentifier, shortTypeName, soleStreamResponse, statusToCamelCase, streamContentType, streamMode, streamSchemas, streamStatuses, toCamelCase, toPascalCase, toToolName, unwrapOptionalWrappers, walkApi };
|