@zmdb/ai 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +10 -0
  2. package/README.md +56 -0
  3. package/dist/chat/index.d.ts +81 -0
  4. package/dist/chat/index.d.ts.map +1 -0
  5. package/dist/chat/index.js +86 -0
  6. package/dist/chat/index.js.map +1 -0
  7. package/dist/compiler.d.ts +3 -0
  8. package/dist/compiler.d.ts.map +1 -0
  9. package/dist/compiler.js +2 -0
  10. package/dist/compiler.js.map +1 -0
  11. package/dist/http/caller.d.ts +3 -0
  12. package/dist/http/caller.d.ts.map +1 -0
  13. package/dist/http/caller.js +128 -0
  14. package/dist/http/caller.js.map +1 -0
  15. package/dist/http/generate.d.ts +7 -0
  16. package/dist/http/generate.d.ts.map +1 -0
  17. package/dist/http/generate.js +205 -0
  18. package/dist/http/generate.js.map +1 -0
  19. package/dist/http/index.d.ts +5 -0
  20. package/dist/http/index.d.ts.map +1 -0
  21. package/dist/http/index.js +6 -0
  22. package/dist/http/index.js.map +1 -0
  23. package/dist/http/parse.d.ts +11 -0
  24. package/dist/http/parse.d.ts.map +1 -0
  25. package/dist/http/parse.js +398 -0
  26. package/dist/http/parse.js.map +1 -0
  27. package/dist/http/types.d.ts +59 -0
  28. package/dist/http/types.d.ts.map +1 -0
  29. package/dist/http/types.js +20 -0
  30. package/dist/http/types.js.map +1 -0
  31. package/dist/index.d.ts +12 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +33 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/providers.d.ts +85 -0
  36. package/dist/providers.d.ts.map +1 -0
  37. package/dist/providers.js +252 -0
  38. package/dist/providers.js.map +1 -0
  39. package/dist/tool-runtime.d.ts +36 -0
  40. package/dist/tool-runtime.d.ts.map +1 -0
  41. package/dist/tool-runtime.js +55 -0
  42. package/dist/tool-runtime.js.map +1 -0
  43. package/package.json +66 -0
  44. package/src/chat/index.ts +202 -0
  45. package/src/compiler.ts +2 -0
  46. package/src/http/caller.ts +132 -0
  47. package/src/http/generate.ts +218 -0
  48. package/src/http/index.ts +16 -0
  49. package/src/http/parse.ts +666 -0
  50. package/src/http/types.ts +83 -0
  51. package/src/index.ts +40 -0
  52. package/src/providers.ts +443 -0
  53. package/src/tool-runtime.ts +91 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,OAAO,EAAmC,MAAM,gBAAgB,CAAC;AAE1E,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,MAA0B,EAAE,IAAkB;IACzF,OAAO,OAAO,CAAC,aAAa,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AACpD,CAAC;AAQD,MAAM,UAAU,YAAY,CAAc,IAAY,EAAE,MAA0B;IAChF,+DAA+D;IAC/D,MAAM,QAAQ,GAAG,IAAI;SAClB,IAAI,EAAE;SACN,OAAO,CAAC,mBAAmB,EAAE,EAAE,CAAC;SAChC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;SACtB,IAAI,EAAE,CAAC;IACV,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAChC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,cAAc,CAAC,EAAE,CAAC;IAC3F,CAAC;IACD,6EAA6E;IAC7E,sEAAsE;IACtE,8EAA8E;IAC9E,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAW,EAAE,CAAC;IACzD,IAAI,CAAC;QACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;IACjD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,iBAAiB,CAAC,EAAE,CAAC;IAC9F,CAAC;AACH,CAAC;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -0,0 +1,85 @@
1
+ import { type CoreSchema } from '@zmdb/schema';
2
+ import { type JsonSchemaObject, type ShapeIR } from '@zmdb/schema/ir';
3
+ import { ToolSpecRefusalError, type ToolProvider, type ToolSpecRefusal } from './http/types.js';
4
+ export type { ToolProvider, ToolSpecRefusal };
5
+ export { ToolSpecRefusalError };
6
+ export interface ToolOptions {
7
+ readonly description?: string;
8
+ }
9
+ export type ToolSchema = CoreSchema<string>;
10
+ export interface ToolSpec {
11
+ readonly name: string;
12
+ readonly description?: string;
13
+ readonly parameters: JsonSchemaObject;
14
+ }
15
+ export interface StrictJsonSchemaObject extends JsonSchemaObject {
16
+ readonly additionalProperties: false;
17
+ }
18
+ export interface GeminiSchemaObject extends JsonSchemaObject {
19
+ }
20
+ export interface ToolSpecFor {
21
+ readonly openai: {
22
+ readonly type: 'function';
23
+ readonly function: {
24
+ readonly name: string;
25
+ readonly description?: string;
26
+ readonly parameters: JsonSchemaObject;
27
+ };
28
+ };
29
+ readonly 'openai-strict': {
30
+ readonly type: 'function';
31
+ readonly function: {
32
+ readonly name: string;
33
+ readonly description?: string;
34
+ readonly strict: true;
35
+ readonly parameters: StrictJsonSchemaObject;
36
+ };
37
+ };
38
+ readonly anthropic: {
39
+ readonly name: string;
40
+ readonly description?: string;
41
+ readonly input_schema: JsonSchemaObject;
42
+ };
43
+ readonly gemini: {
44
+ readonly name: string;
45
+ readonly description?: string;
46
+ readonly parameters: GeminiSchemaObject;
47
+ };
48
+ readonly 'json-schema': ToolSpec;
49
+ }
50
+ export interface ToolDialect {
51
+ readonly allowedKeywords: ReadonlySet<string>;
52
+ readonly maxProperties?: number;
53
+ readonly source: string;
54
+ readonly verifiedOn: '2026-09-04';
55
+ }
56
+ /**
57
+ * Provider constraints are data rather than branches scattered through the emitter.
58
+ *
59
+ * The cap is deliberately below every provider's moving request-size ceiling. It is a
60
+ * build-time guard against producing a tool definition large enough to be rejected after
61
+ * deployment, not a promise that a provider will accept every document below it.
62
+ */
63
+ export declare const TOOL_DIALECTS: Readonly<Record<ToolProvider, ToolDialect>>;
64
+ type AnyToolSpec = ToolSpecFor[ToolProvider];
65
+ /**
66
+ * The provider's parameter document, directly from the declaration IR.
67
+ *
68
+ * This is exported for the AOT emitter. Applications should call {@link toolFor}; exposing
69
+ * the pure step keeps runtime and build-time output byte-identical without a second walker.
70
+ */
71
+ export declare function toolSchemaForProvider(provider: ToolProvider, shape: ShapeIR): JsonSchemaObject;
72
+ export declare function frameTool(provider: ToolProvider, name: string, parameters: JsonSchemaObject, options?: ToolOptions): AnyToolSpec;
73
+ export declare function toolFor<_T>(provider: 'openai', name: string, options?: ToolOptions): ToolSpecFor['openai'];
74
+ export declare function toolFor<_T>(provider: 'openai-strict', name: string, options?: ToolOptions): ToolSpecFor['openai-strict'];
75
+ export declare function toolFor<_T>(provider: 'anthropic', name: string, options?: ToolOptions): ToolSpecFor['anthropic'];
76
+ export declare function toolFor<_T>(provider: 'gemini', name: string, options?: ToolOptions): ToolSpecFor['gemini'];
77
+ export declare function toolFor<_T>(provider: 'json-schema', name: string, options?: ToolOptions): ToolSpecFor['json-schema'];
78
+ export declare function toolFor<_T, P extends ToolProvider>(provider: P, name: string, options?: ToolOptions): ToolSpecFor[P];
79
+ export declare function toolFor(provider: 'openai', name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor['openai'];
80
+ export declare function toolFor(provider: 'openai-strict', name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor['openai-strict'];
81
+ export declare function toolFor(provider: 'anthropic', name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor['anthropic'];
82
+ export declare function toolFor(provider: 'gemini', name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor['gemini'];
83
+ export declare function toolFor(provider: 'json-schema', name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor['json-schema'];
84
+ export declare function toolFor<P extends ToolProvider>(provider: P, name: string, schema: ToolSchema, options?: ToolOptions): ToolSpecFor[P];
85
+ //# sourceMappingURL=providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAIL,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACb,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,oBAAoB,EAAE,KAAK,YAAY,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEhG,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;AAE5C,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,EAAE,gBAAgB,CAAC;CACvC;AAED,MAAM,WAAW,sBAAuB,SAAQ,gBAAgB;IAC9D,QAAQ,CAAC,oBAAoB,EAAE,KAAK,CAAC;CACtC;AAED,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;CAAG;AAE/D,MA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efusalError } from './http/types.js';
4
+ export { ToolSpecRefusalError };
5
+ const COMMON_KEYWORDS = [
6
+ 'type',
7
+ 'format',
8
+ 'enum',
9
+ 'const',
10
+ 'minimum',
11
+ 'maximum',
12
+ 'minLength',
13
+ 'maxLength',
14
+ 'pattern',
15
+ 'items',
16
+ 'minItems',
17
+ 'maxItems',
18
+ 'properties',
19
+ 'required',
20
+ 'anyOf',
21
+ 'additionalProperties',
22
+ ];
23
+ /**
24
+ * Provider constraints are data rather than branches scattered through the emitter.
25
+ *
26
+ * The cap is deliberately below every provider's moving request-size ceiling. It is a
27
+ * build-time guard against producing a tool definition large enough to be rejected after
28
+ * deployment, not a promise that a provider will accept every document below it.
29
+ */
30
+ export const TOOL_DIALECTS = {
31
+ openai: {
32
+ allowedKeywords: new Set(COMMON_KEYWORDS),
33
+ maxProperties: 1_024,
34
+ source: 'https://platform.openai.com/docs/guides/structured-outputs',
35
+ verifiedOn: '2026-09-04',
36
+ },
37
+ 'openai-strict': {
38
+ allowedKeywords: new Set(COMMON_KEYWORDS),
39
+ maxProperties: 1_024,
40
+ source: 'https://platform.openai.com/docs/guides/structured-outputs',
41
+ verifiedOn: '2026-09-04',
42
+ },
43
+ anthropic: {
44
+ allowedKeywords: new Set(COMMON_KEYWORDS),
45
+ maxProperties: 1_024,
46
+ source: 'https://platform.claude.com/docs/en/agents-and-tools/tool-use/implement-tool-use',
47
+ verifiedOn: '2026-09-04',
48
+ },
49
+ gemini: {
50
+ allowedKeywords: new Set([...COMMON_KEYWORDS, 'nullable']),
51
+ maxProperties: 1_024,
52
+ source: 'https://ai.google.dev/api/caching#Schema',
53
+ verifiedOn: '2026-09-04',
54
+ },
55
+ 'json-schema': {
56
+ allowedKeywords: new Set(COMMON_KEYWORDS),
57
+ source: 'https://json-schema.org/draft/2020-12/json-schema-core.html',
58
+ verifiedOn: '2026-09-04',
59
+ },
60
+ };
61
+ function isRecord(value) {
62
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
63
+ }
64
+ function isToolSchema(value) {
65
+ if (!isRecord(value))
66
+ return false;
67
+ const ir = value['ir'];
68
+ return isRecord(ir) && Array.isArray(ir['columns']);
69
+ }
70
+ function descriptionPart(description) {
71
+ return description ? { description } : {};
72
+ }
73
+ function refusal(provider, path, construct, reason, suggestion) {
74
+ throw new ToolSpecRefusalError({ provider, path, construct, reason, suggestion });
75
+ }
76
+ function joinPath(parent, child) {
77
+ return parent.length === 0 ? child : `${parent}.${child}`;
78
+ }
79
+ function nullableType(value) {
80
+ if (typeof value === 'string')
81
+ return value === 'null' ? value : [value, 'null'];
82
+ if (!Array.isArray(value))
83
+ return value;
84
+ return value.includes('null') ? [...value] : [...value, 'null'];
85
+ }
86
+ function requiredNames(value) {
87
+ const required = value['required'];
88
+ if (!Array.isArray(required))
89
+ return new Set();
90
+ return new Set(required.filter(name => typeof name === 'string'));
91
+ }
92
+ function propertiesOf(value) {
93
+ const properties = value['properties'];
94
+ return isRecord(properties) ? properties : undefined;
95
+ }
96
+ function translateNode(provider, value, path, optional) {
97
+ if (Object.keys(value).length === 0) {
98
+ refusal(provider, path, 'untyped json', 'the provider requires a type for every tool property, but this column emits no type', 'declare the payload with WireAs<W>, or omit the column from the tool');
99
+ }
100
+ const dialect = TOOL_DIALECTS[provider];
101
+ for (const keyword of Object.keys(value)) {
102
+ if (!dialect.allowedKeywords.has(keyword)) {
103
+ refusal(provider, path, `unsupported keyword ${keyword}`, `${provider} cannot express the emitted ${keyword} keyword without changing its meaning`, 'declare a provider-compatible wire shape, or omit the column from the tool');
104
+ }
105
+ }
106
+ const result = {};
107
+ const nested = propertiesOf(value);
108
+ const nestedRequired = requiredNames(value);
109
+ for (const [keyword, raw] of Object.entries(value)) {
110
+ if (provider === 'openai-strict' && keyword === 'format' && raw === 'int64')
111
+ continue;
112
+ if (keyword === 'properties' && nested !== undefined) {
113
+ const properties = {};
114
+ for (const name of Object.keys(nested).toSorted()) {
115
+ const child = nested[name];
116
+ if (!isRecord(child)) {
117
+ refusal(provider, joinPath(path, name), 'non-object property schema', 'the emitted property schema is not an object', 'declare the property with a JSON-Schema-compatible wire type');
118
+ }
119
+ properties[name] = translateNode(provider, child, joinPath(path, name), !nestedRequired.has(name));
120
+ }
121
+ result[keyword] = properties;
122
+ continue;
123
+ }
124
+ if (keyword === 'items' && isRecord(raw)) {
125
+ result[keyword] = translateNode(provider, raw, `${path}[]`, false);
126
+ continue;
127
+ }
128
+ if (keyword === 'anyOf' && Array.isArray(raw)) {
129
+ result[keyword] = raw.map((member, index) => {
130
+ if (!isRecord(member)) {
131
+ refusal(provider, `${path}|${String(index)}`, 'non-object union member', 'the emitted union member is not a schema object', 'declare every union member with a provider-compatible wire type');
132
+ }
133
+ return translateNode(provider, member, `${path}|${String(index)}`, false);
134
+ });
135
+ continue;
136
+ }
137
+ if (keyword === 'required' && nested !== undefined && provider === 'openai-strict') {
138
+ result[keyword] = Object.keys(nested).toSorted();
139
+ continue;
140
+ }
141
+ if (keyword === 'type' && provider === 'gemini' && Array.isArray(raw)) {
142
+ const nonNull = raw.filter(item => item !== 'null');
143
+ if (nonNull.length !== 1 || !raw.includes('null')) {
144
+ refusal(provider, path, 'type union', 'Gemini cannot express this type array without changing the accepted values', 'declare a single nullable wire type, or omit the column from the tool');
145
+ }
146
+ result[keyword] = nonNull[0];
147
+ result['nullable'] = true;
148
+ continue;
149
+ }
150
+ result[keyword] = raw;
151
+ }
152
+ if (provider === 'openai-strict') {
153
+ if (optional)
154
+ result['type'] = nullableType(result['type']);
155
+ if (nested !== undefined)
156
+ result['additionalProperties'] = false;
157
+ }
158
+ return result;
159
+ }
160
+ function visibleShape(shape) {
161
+ return shape
162
+ .filter(entry => !entry.column.sensitive)
163
+ .toSorted((left, right) => left.column.name.localeCompare(right.column.name));
164
+ }
165
+ function propertyCount(value) {
166
+ if (Array.isArray(value))
167
+ return value.reduce((total, item) => total + propertyCount(item), 0);
168
+ if (!isRecord(value))
169
+ return 0;
170
+ const properties = propertiesOf(value);
171
+ let count = properties === undefined ? 0 : Object.keys(properties).length;
172
+ for (const item of Object.values(value))
173
+ count += propertyCount(item);
174
+ return count;
175
+ }
176
+ function enforceShape(provider, shape, document) {
177
+ if (shape.length === 0 && provider !== 'json-schema') {
178
+ refusal(provider, '', 'empty create schema', 'the create variant has no visible properties', 'drop the tool, or unmark a Sensitive column that the model is allowed to supply');
179
+ }
180
+ const maximum = TOOL_DIALECTS[provider].maxProperties;
181
+ const count = propertyCount(document);
182
+ if (maximum !== undefined && count > maximum) {
183
+ refusal(provider, '', `property limit ${String(maximum)}`, `the tool contains ${String(count)} properties, above the provider cap of ${String(maximum)}`, 'split the operation into smaller tools');
184
+ }
185
+ }
186
+ /**
187
+ * The provider's parameter document, directly from the declaration IR.
188
+ *
189
+ * This is exported for the AOT emitter. Applications should call {@link toolFor}; exposing
190
+ * the pure step keeps runtime and build-time output byte-identical without a second walker.
191
+ */
192
+ export function toolSchemaForProvider(provider, shape) {
193
+ const visible = visibleShape(shape);
194
+ const generic = jsonSchemaFromShape(visible);
195
+ enforceShape(provider, visible, generic);
196
+ if (provider !== 'openai-strict' && provider !== 'gemini')
197
+ return generic;
198
+ const properties = {};
199
+ for (const { column, optional } of visible) {
200
+ properties[column.name] = translateNode(provider, jsonSchemaForColumn(column), column.name, optional);
201
+ }
202
+ if (provider === 'openai-strict') {
203
+ const strict = {
204
+ type: 'object',
205
+ properties,
206
+ required: visible.map(entry => entry.column.name),
207
+ additionalProperties: false,
208
+ };
209
+ return strict;
210
+ }
211
+ return {
212
+ type: 'object',
213
+ properties,
214
+ required: generic.required,
215
+ };
216
+ }
217
+ export function frameTool(provider, name, parameters, options = {}) {
218
+ const described = descriptionPart(options.description);
219
+ switch (provider) {
220
+ case 'openai':
221
+ return { type: 'function', function: { name, ...described, parameters } };
222
+ case 'openai-strict':
223
+ return {
224
+ type: 'function',
225
+ function: {
226
+ name,
227
+ ...described,
228
+ strict: true,
229
+ parameters: {
230
+ ...parameters,
231
+ additionalProperties: false,
232
+ },
233
+ },
234
+ };
235
+ case 'anthropic':
236
+ return { name, ...described, input_schema: parameters };
237
+ case 'gemini':
238
+ return { name, ...described, parameters };
239
+ case 'json-schema':
240
+ return { name, ...described, parameters };
241
+ }
242
+ }
243
+ export function toolFor(provider, name, schemaOrOptions, options = {}) {
244
+ if (!isToolSchema(schemaOrOptions)) {
245
+ throw new Error('toolFor<T>() was not replaced at build time. It is compiled away by @zmdb/compiler ' +
246
+ '(the unplugin, Metro adapter, or project compiler), which did not run over this file — a type argument cannot ' +
247
+ 'be read at runtime, so there is nothing to fall back to.');
248
+ }
249
+ const parameters = toolSchemaForProvider(provider, shapeOfVariant(schemaOrOptions.ir, 'create'));
250
+ return frameTool(provider, name, parameters, options);
251
+ }
252
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,MAAM,cAAc,CAAC;AAC/C,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,GAGf,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,oBAAoB,EAA2C,MAAM,iBAAiB,CAAC;AAGhG,OAAO,EAAE,oBAAoB,EAAE,CAAC;AA0DhC,MAAM,eAAe,GAAG;IACtB,MAAM;IACN,QAAQ;IACR,MAAM;IACN,OAAO;IACP,SAAS;IACT,SAAS;IACT,WAAW;IACX,WAAW;IACX,SAAS;IACT,OAAO;IACP,UAAU;IACV,UAAU;IACV,YAAY;IACZ,UAAU;IACV,OAAO;IACP,sBAAsB;CACd,CAAC;AAEX;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,aAAa,GAAgD;IACxE,MAAM,EAAE;QACN,eAAe,EAAE,IAAI,GAAG,CAAS,eAAe,CAAC;QACjD,aAAa,EAAE,KAAK;QACpB,MAAM,EAAE,4DAA4D;QACpE,UAAU,EAAE,YAAY;KACzB;IACD,eAAe,EAAE;QACf,eAAe,EAAE,IAAI,GAAG,CAAS,eA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iseLike<unknown>;
4
+ }
5
+ export type ToolInvocation = {
6
+ readonly kind: 'success';
7
+ readonly content: string;
8
+ } | {
9
+ readonly kind: 'validation-error';
10
+ readonly error: unknown;
11
+ readonly content?: string;
12
+ } | {
13
+ readonly kind: 'handler-error';
14
+ readonly error: unknown;
15
+ };
16
+ export interface ToolAdapterOptions<T, Output = unknown> {
17
+ readonly description: string;
18
+ /**
19
+ * Validate the model-shaped value and return the decoded application value.
20
+ *
21
+ * This function belongs at the call site so an AOT validator can be inlined
22
+ * there. Custom wire codecs can decode in the same function before the
23
+ * handler receives the value.
24
+ */
25
+ readonly validate: (value: unknown) => T;
26
+ readonly execute: (input: T) => Output | PromiseLike<Output>;
27
+ }
28
+ export declare const serialiseToolResult: (result: unknown) => string;
29
+ export declare function invokeTool<T>(entry: InvocableTool<T>, args: unknown, identity?: unknown): Promise<ToolInvocation>;
30
+ /**
31
+ * A malformed model call is returned to the model so it can correct the next
32
+ * turn. Errors without a valid issue list, including handler failures, are
33
+ * application failures and remain thrown.
34
+ */
35
+ export declare function executeToolAdapter<T, Output>(name: string, value: unknown, options: ToolAdapterOptions<T, Output>): Promise<Awaited<Output> | string>;
36
+ //# sourceMappingURL=tool-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-runtime.d.ts","sourceRoot":"","sources":["../src/tool-runtime.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,CAAC,CAAC;IACxC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;CACpF;AAED,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GACzF;IAAE,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEhE,MAAM,WAAW,kBAAkB,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO;IACrD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;CAC9D;AAED,eAAO,MAAM,mBAAmB,WAAY,OAAO,KAAG,MAIrD,CAAC;AAcF,wBAAsB,UAAU,CAAC,CAAC,EAChC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EACvB,IAAI,EAAE,OAAO,EACb,QAAQ,CAAC,EAAE,OAAO,GACjB,OAAO,CAAC,cAAc,CAAC,CAczB;AAUD;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAChD,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,kBAAkB,CAAC,CAAC,EAAE,MAAM,CAAC,GACrC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAUnC"}
@@ -0,0 +1,55 @@
1
+ import { validationIssuesOf } from '@zmdb/validator';
2
+ export const serialiseToolResult = (result) => {
3
+ if (typeof result === 'string')
4
+ return result;
5
+ const serialised = JSON.stringify(result);
6
+ return serialised ?? 'undefined';
7
+ };
8
+ const validationErrorContent = (error) => {
9
+ const issues = validationIssuesOf(error);
10
+ if (issues === undefined)
11
+ return undefined;
12
+ return JSON.stringify(issues.map(issue => issue.expected === undefined
13
+ ? { path: issue.path, message: issue.message }
14
+ : { path: issue.path, message: issue.message, expected: issue.expected }));
15
+ };
16
+ export async function invokeTool(entry, args, identity) {
17
+ let input;
18
+ try {
19
+ input = entry.validate(args);
20
+ }
21
+ catch (error) {
22
+ const content = validationErrorContent(error);
23
+ return content === undefined ? { kind: 'validation-error', error } : { kind: 'validation-error', error, content };
24
+ }
25
+ try {
26
+ return { kind: 'success', content: serialiseToolResult(await entry.handler(input, identity)) };
27
+ }
28
+ catch (error) {
29
+ return { kind: 'handler-error', error };
30
+ }
31
+ }
32
+ const validationLine = (issue) => issue.expected === undefined ? `${issue.path}: ${issue.message}` : `${issue.path}: expected ${issue.expected}`;
33
+ const validationFailure = (name, issues) => {
34
+ const details = issues.length === 0 ? 'validation failed without details' : issues.map(validationLine).join('\n');
35
+ return `Tool "${name}" rejected its arguments:\n${details}`;
36
+ };
37
+ /**
38
+ * A malformed model call is returned to the model so it can correct the next
39
+ * turn. Errors without a valid issue list, including handler failures, are
40
+ * application failures and remain thrown.
41
+ */
42
+ export async function executeToolAdapter(name, value, options) {
43
+ let input;
44
+ try {
45
+ input = options.validate(value);
46
+ }
47
+ catch (error) {
48
+ const issues = validationIssuesOf(error);
49
+ if (issues === undefined)
50
+ throw error;
51
+ return validationFailure(name, issues);
52
+ }
53
+ return await options.execute(input);
54
+ }
55
+ //# sourceMappingURL=tool-runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-runtime.js","sourceRoot":"","sources":["../src/tool-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAwB,MAAM,iBAAiB,CAAC;AAyB3E,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,MAAe,EAAU,EAAE;IAC7D,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC;IAC9C,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS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
+ "version": "1.0.0-beta.1",
4
+ "description": "Provider-neutral AI tool documents, bounded chat orchestration, shared tool invocation, and OpenAPI-derived tools for zmdb.",
5
+ "keywords": [
6
+ "ai",
7
+ "function-calling",
8
+ "json-schema",
9
+ "llm",
10
+ "openapi",
11
+ "typescript",
12
+ "zmdb"
13
+ ],
14
+ "homepage": "https://github.com/ambasta/zmdb#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/ambasta/zmdb/issues"
17
+ },
18
+ "license": "GPL-3.0-or-later",
19
+ "author": "zmdb contributors",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/ambasta/zmdb.git",
23
+ "directory": "packages/ai"
24
+ },
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./chat": {
33
+ "types": "./dist/chat/index.d.ts",
34
+ "import": "./dist/chat/index.js"
35
+ },
36
+ "./compiler": {
37
+ "types": "./dist/compiler.d.ts",
38
+ "import": "./dist/compiler.js"
39
+ },
40
+ "./http": {
41
+ "types": "./dist/http/index.d.ts",
42
+ "import": "./dist/http/index.js"
43
+ },
44
+ "./tool-runtime": {
45
+ "types": "./dist/tool-runtime.d.ts",
46
+ "import": "./dist/tool-runtime.js"
47
+ }
48
+ },
49
+ "publishConfig": {
50
+ "access": "public",
51
+ "tag": "beta"
52
+ },
53
+ "scripts": {
54
+ "build": "node ../../scripts/build-package.mjs",
55
+ "test": "vitest run"
56
+ },
57
+ "peerDependencies": {
58
+ "@zmdb/schema": "1.0.0-beta.1",
59
+ "@zmdb/validator": "1.0.0-beta.1"
60
+ },
61
+ "engines": {
62
+ "node": ">=26"
63
+ },
64
+ "main": "./dist/index.js",
65
+ "types": "./dist/index.d.ts"
66
+ }