vovk 3.0.0-draft.99 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/README.md +22 -13
  2. package/bin/index.mjs +10 -0
  3. package/dist/client/createRPC.d.ts +13 -3
  4. package/dist/client/createRPC.js +112 -50
  5. package/dist/client/defaultHandler.d.ts +5 -1
  6. package/dist/client/defaultHandler.js +12 -9
  7. package/dist/client/defaultStreamHandler.d.ts +16 -4
  8. package/dist/client/defaultStreamHandler.js +259 -62
  9. package/dist/client/fetcher.d.ts +41 -3
  10. package/dist/client/fetcher.js +125 -60
  11. package/dist/client/progressive.d.ts +15 -0
  12. package/dist/client/progressive.js +56 -0
  13. package/dist/{utils → client}/serializeQuery.d.ts +2 -2
  14. package/dist/{utils → client}/serializeQuery.js +1 -4
  15. package/dist/core/HttpException.d.ts +16 -0
  16. package/dist/core/HttpException.js +26 -0
  17. package/dist/core/JSONLinesResponder.d.ts +42 -0
  18. package/dist/core/JSONLinesResponder.js +92 -0
  19. package/dist/core/controllersToStaticParams.d.ts +13 -0
  20. package/dist/core/controllersToStaticParams.js +36 -0
  21. package/dist/core/createDecorator.d.ts +12 -0
  22. package/dist/{createDecorator.js → core/createDecorator.js} +18 -12
  23. package/dist/core/decorators.d.ts +59 -0
  24. package/dist/core/decorators.js +132 -0
  25. package/dist/core/getSchema.d.ts +21 -0
  26. package/dist/core/getSchema.js +31 -0
  27. package/dist/core/initSegment.d.ts +33 -0
  28. package/dist/core/initSegment.js +35 -0
  29. package/dist/core/multitenant.d.ts +33 -0
  30. package/dist/core/multitenant.js +132 -0
  31. package/dist/core/resolveGeneratorConfigValues.d.ts +19 -0
  32. package/dist/core/resolveGeneratorConfigValues.js +59 -0
  33. package/dist/{utils → core}/setHandlerSchema.d.ts +2 -2
  34. package/dist/{utils → core}/setHandlerSchema.js +1 -4
  35. package/dist/core/toDownloadResponse.d.ts +11 -0
  36. package/dist/core/toDownloadResponse.js +25 -0
  37. package/dist/core/vovkApp.d.ts +36 -0
  38. package/dist/core/vovkApp.js +316 -0
  39. package/dist/index.d.ts +25 -59
  40. package/dist/index.js +23 -23
  41. package/dist/internal.d.ts +17 -0
  42. package/dist/internal.js +10 -0
  43. package/dist/openapi/error.d.ts +2 -0
  44. package/dist/openapi/error.js +97 -0
  45. package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.d.ts +3 -0
  46. package/dist/openapi/openAPIToVovkSchema/applyComponentsSchemas.js +65 -0
  47. package/dist/openapi/openAPIToVovkSchema/index.d.ts +5 -0
  48. package/dist/openapi/openAPIToVovkSchema/index.js +153 -0
  49. package/dist/openapi/openAPIToVovkSchema/inlineRefs.d.ts +9 -0
  50. package/dist/openapi/openAPIToVovkSchema/inlineRefs.js +99 -0
  51. package/dist/openapi/operation.d.ts +10 -0
  52. package/dist/openapi/operation.js +19 -0
  53. package/dist/openapi/tool.d.ts +2 -0
  54. package/dist/openapi/tool.js +12 -0
  55. package/dist/openapi/vovkSchemaToOpenAPI.d.ts +21 -0
  56. package/dist/openapi/vovkSchemaToOpenAPI.js +250 -0
  57. package/dist/req/bufferBody.d.ts +1 -0
  58. package/dist/req/bufferBody.js +30 -0
  59. package/dist/req/parseBody.d.ts +4 -0
  60. package/dist/req/parseBody.js +49 -0
  61. package/dist/req/parseForm.d.ts +1 -0
  62. package/dist/req/parseForm.js +24 -0
  63. package/dist/{utils → req}/parseQuery.d.ts +1 -2
  64. package/dist/{utils → req}/parseQuery.js +2 -5
  65. package/dist/req/reqMeta.d.ts +2 -0
  66. package/dist/{utils → req}/reqMeta.js +1 -4
  67. package/dist/req/reqQuery.d.ts +2 -0
  68. package/dist/req/reqQuery.js +4 -0
  69. package/dist/req/validateContentType.d.ts +1 -0
  70. package/dist/req/validateContentType.js +32 -0
  71. package/dist/samples/createCodeSamples.d.ts +20 -0
  72. package/dist/samples/createCodeSamples.js +293 -0
  73. package/dist/samples/objectToCode.d.ts +8 -0
  74. package/dist/samples/objectToCode.js +38 -0
  75. package/dist/samples/schemaToCode.d.ts +11 -0
  76. package/dist/samples/schemaToCode.js +264 -0
  77. package/dist/samples/schemaToObject.d.ts +2 -0
  78. package/dist/samples/schemaToObject.js +164 -0
  79. package/dist/samples/schemaToTsType.d.ts +2 -0
  80. package/dist/samples/schemaToTsType.js +114 -0
  81. package/dist/tools/ToModelOutput.d.ts +8 -0
  82. package/dist/tools/ToModelOutput.js +10 -0
  83. package/dist/tools/createTool.d.ts +126 -0
  84. package/dist/tools/createTool.js +6 -0
  85. package/dist/tools/createToolFactory.d.ts +135 -0
  86. package/dist/tools/createToolFactory.js +61 -0
  87. package/dist/tools/deriveTools.d.ts +46 -0
  88. package/dist/tools/deriveTools.js +134 -0
  89. package/dist/tools/toModelOutputDefault.d.ts +7 -0
  90. package/dist/tools/toModelOutputDefault.js +7 -0
  91. package/dist/tools/toModelOutputMCP.d.ts +30 -0
  92. package/dist/tools/toModelOutputMCP.js +54 -0
  93. package/dist/tsconfig.tsbuildinfo +1 -0
  94. package/dist/types/client.d.ts +140 -0
  95. package/dist/types/client.js +1 -0
  96. package/dist/types/config.d.ts +151 -0
  97. package/dist/types/config.js +1 -0
  98. package/dist/types/core.d.ts +115 -0
  99. package/dist/types/core.js +1 -0
  100. package/dist/types/enums.d.ts +75 -0
  101. package/dist/{types.js → types/enums.js} +21 -9
  102. package/dist/types/inference.d.ts +117 -0
  103. package/dist/types/inference.js +1 -0
  104. package/dist/types/json-schema.d.ts +51 -0
  105. package/dist/types/json-schema.js +1 -0
  106. package/dist/types/operation.d.ts +5 -0
  107. package/dist/types/operation.js +1 -0
  108. package/dist/types/package.d.ts +544 -0
  109. package/dist/types/package.js +5 -0
  110. package/dist/types/request.d.ts +48 -0
  111. package/dist/types/request.js +1 -0
  112. package/dist/types/standard-schema.d.ts +117 -0
  113. package/dist/types/standard-schema.js +6 -0
  114. package/dist/types/tools.d.ts +43 -0
  115. package/dist/types/tools.js +1 -0
  116. package/dist/types/utils.d.ts +9 -0
  117. package/dist/types/utils.js +1 -0
  118. package/dist/types/validation.d.ts +48 -0
  119. package/dist/types/validation.js +1 -0
  120. package/dist/utils/camelCase.d.ts +6 -0
  121. package/dist/utils/camelCase.js +34 -0
  122. package/dist/utils/deepExtend.d.ts +53 -0
  123. package/dist/utils/deepExtend.js +128 -0
  124. package/dist/utils/fileNameToDisposition.d.ts +1 -0
  125. package/dist/utils/fileNameToDisposition.js +3 -0
  126. package/dist/utils/shim.d.ts +1 -0
  127. package/dist/utils/shim.js +1 -1
  128. package/dist/utils/toKebabCase.d.ts +1 -0
  129. package/dist/utils/toKebabCase.js +5 -0
  130. package/dist/utils/trimPath.d.ts +1 -0
  131. package/dist/utils/trimPath.js +1 -0
  132. package/dist/utils/upperFirst.d.ts +1 -0
  133. package/dist/utils/upperFirst.js +3 -0
  134. package/dist/validation/createStandardValidation.d.ts +268 -0
  135. package/dist/validation/createStandardValidation.js +45 -0
  136. package/dist/validation/createValidateOnClient.d.ts +14 -0
  137. package/dist/validation/createValidateOnClient.js +23 -0
  138. package/dist/validation/procedure.d.ts +261 -0
  139. package/dist/validation/procedure.js +8 -0
  140. package/dist/validation/withValidationLibrary.d.ts +119 -0
  141. package/dist/validation/withValidationLibrary.js +174 -0
  142. package/package.json +44 -10
  143. package/dist/HttpException.d.ts +0 -7
  144. package/dist/HttpException.js +0 -15
  145. package/dist/StreamJSONResponse.d.ts +0 -14
  146. package/dist/StreamJSONResponse.js +0 -57
  147. package/dist/VovkApp.d.ts +0 -29
  148. package/dist/VovkApp.js +0 -188
  149. package/dist/client/index.d.ts +0 -3
  150. package/dist/client/index.js +0 -7
  151. package/dist/client/types.d.ts +0 -104
  152. package/dist/client/types.js +0 -2
  153. package/dist/createDecorator.d.ts +0 -6
  154. package/dist/createVovkApp.d.ts +0 -62
  155. package/dist/createVovkApp.js +0 -118
  156. package/dist/types.d.ts +0 -220
  157. package/dist/utils/generateStaticAPI.d.ts +0 -4
  158. package/dist/utils/generateStaticAPI.js +0 -18
  159. package/dist/utils/getSchema.d.ts +0 -20
  160. package/dist/utils/getSchema.js +0 -33
  161. package/dist/utils/reqForm.d.ts +0 -2
  162. package/dist/utils/reqForm.js +0 -13
  163. package/dist/utils/reqMeta.d.ts +0 -2
  164. package/dist/utils/reqQuery.d.ts +0 -2
  165. package/dist/utils/reqQuery.js +0 -10
  166. package/dist/utils/withValidation.d.ts +0 -20
  167. package/dist/utils/withValidation.js +0 -72
@@ -0,0 +1,164 @@
1
+ export function schemaToObject(schema, rootSchema) {
2
+ if (!schema || typeof schema !== 'object')
3
+ return null;
4
+ // Use the input schema as the root if not provided
5
+ rootSchema = rootSchema || schema;
6
+ // If there's an example, use it
7
+ if (schema.example !== undefined) {
8
+ return schema.example;
9
+ }
10
+ // If there are examples, use one of them
11
+ if (schema.examples && schema.examples.length > 0) {
12
+ return schema.examples[0];
13
+ }
14
+ // Handle const if present
15
+ if (schema.const !== undefined) {
16
+ return schema.const;
17
+ }
18
+ // Handle $ref if present
19
+ if (schema.$ref) {
20
+ return handleRef(schema.$ref, rootSchema);
21
+ }
22
+ // Handle enum if present
23
+ if (schema.enum && schema.enum.length > 0) {
24
+ return schema.enum[0];
25
+ }
26
+ // Handle oneOf, anyOf, allOf
27
+ if (schema.oneOf && schema.oneOf.length > 0) {
28
+ return schemaToObject(schema.oneOf[0], rootSchema);
29
+ }
30
+ if (schema.anyOf && schema.anyOf.length > 0) {
31
+ return schemaToObject(schema.anyOf[0], rootSchema);
32
+ }
33
+ if (schema.allOf && schema.allOf.length > 0) {
34
+ // Merge all schemas in allOf
35
+ const mergedSchema = schema.allOf.reduce((acc, s) => ({ ...acc, ...s }), {});
36
+ return schemaToObject(mergedSchema, rootSchema);
37
+ }
38
+ // Handle different types
39
+ if (schema.type) {
40
+ switch (schema.type) {
41
+ case 'string':
42
+ return handleString(schema);
43
+ case 'number':
44
+ case 'integer':
45
+ return handleNumber(schema);
46
+ case 'boolean':
47
+ return handleBoolean();
48
+ case 'object':
49
+ return handleObject(schema, rootSchema);
50
+ case 'array':
51
+ return handleArray(schema, rootSchema);
52
+ case 'null':
53
+ return null;
54
+ default:
55
+ return null;
56
+ }
57
+ }
58
+ // If type is not specified but properties are, treat it as an object
59
+ if (schema.properties) {
60
+ return handleObject(schema, rootSchema);
61
+ }
62
+ // Default fallback
63
+ return null;
64
+ }
65
+ function handleRef(ref, rootSchema) {
66
+ // Parse the reference path
67
+ const path = ref.split('/').slice(1); // Remove the initial '#'
68
+ // Navigate through the schema to find the referenced definition
69
+ let current = rootSchema;
70
+ for (const segment of path) {
71
+ current = current[segment];
72
+ if (current === undefined) {
73
+ return null; // Reference not found
74
+ }
75
+ }
76
+ // Process the referenced schema
77
+ return schemaToObject(current, rootSchema);
78
+ }
79
+ function handleString(schema) {
80
+ if (schema.format) {
81
+ switch (schema.format) {
82
+ case 'email':
83
+ case 'idn-email':
84
+ return 'user@example.com';
85
+ case 'uri':
86
+ case 'url':
87
+ case 'iri':
88
+ return 'https://example.com';
89
+ case 'date':
90
+ return '2023-01-01';
91
+ case 'date-time':
92
+ return '2023-01-01T00:00:00Z';
93
+ case 'time':
94
+ return '12:00:00Z';
95
+ case 'duration':
96
+ return 'PT1H';
97
+ case 'uuid':
98
+ return '00000000-0000-0000-0000-000000000000';
99
+ case 'regex':
100
+ return '^[a-zA-Z0-9]+$';
101
+ case 'relative-json-pointer':
102
+ return '/some/relative/path';
103
+ case 'color':
104
+ return '#000000';
105
+ case 'hostname':
106
+ return 'example.com';
107
+ case 'zipcode':
108
+ return '12345';
109
+ case 'phone':
110
+ return '+123-456-7890';
111
+ case 'password':
112
+ return '******';
113
+ default:
114
+ return 'string';
115
+ }
116
+ }
117
+ if (schema.pattern) {
118
+ // For simplicity, return a basic string for patterns
119
+ return 'pattern-string';
120
+ }
121
+ return 'string';
122
+ }
123
+ function handleNumber(schema) {
124
+ if (schema.minimum !== undefined && schema.maximum !== undefined) {
125
+ return schema.minimum;
126
+ }
127
+ else if (schema.minimum !== undefined) {
128
+ return schema.minimum;
129
+ }
130
+ else if (schema.maximum !== undefined) {
131
+ return schema.maximum;
132
+ }
133
+ return 0;
134
+ }
135
+ function handleBoolean() {
136
+ return true;
137
+ }
138
+ function handleObject(schema, rootSchema) {
139
+ const result = {};
140
+ if (schema.properties) {
141
+ const required = schema.required || [];
142
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
143
+ // Only include required properties or as a basic example
144
+ if (required.includes(key) || required.length === 0) {
145
+ result[key] = schemaToObject(propSchema, rootSchema);
146
+ }
147
+ }
148
+ }
149
+ // Handle additionalProperties
150
+ if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
151
+ result['additionalProp'] = schemaToObject(schema.additionalProperties, rootSchema);
152
+ }
153
+ return result;
154
+ }
155
+ function handleArray(schema, rootSchema) {
156
+ if (schema.items && typeof schema.items === 'object') {
157
+ const itemSchema = schema.items;
158
+ const minItems = schema.minItems || 1;
159
+ // Create minimum number of items (capped at a reasonable max for examples)
160
+ const numItems = Math.min(minItems, 3);
161
+ return Array.from({ length: numItems }, () => schemaToObject(itemSchema, rootSchema));
162
+ }
163
+ return [];
164
+ }
@@ -0,0 +1,2 @@
1
+ import { VovkJSONSchemaBase } from 'vovk';
2
+ export declare function schemaToTsType(jsonSchema: VovkJSONSchemaBase | boolean): string;
@@ -0,0 +1,114 @@
1
+ export function schemaToTsType(jsonSchema) {
2
+ if (jsonSchema === true)
3
+ return 'unknown';
4
+ if (jsonSchema === false)
5
+ return 'never';
6
+ if (jsonSchema === null || jsonSchema === undefined)
7
+ return 'unknown';
8
+ if (typeof jsonSchema !== 'object')
9
+ return 'unknown';
10
+ // Handle const
11
+ if ('const' in jsonSchema) {
12
+ return JSON.stringify(jsonSchema.const);
13
+ }
14
+ // Handle enum
15
+ if (jsonSchema.enum) {
16
+ return jsonSchema.enum.map((v) => JSON.stringify(v)).join(' | ') || 'never';
17
+ }
18
+ // Handle allOf (intersection)
19
+ if (jsonSchema.allOf) {
20
+ const parts = jsonSchema.allOf.map((s) => schemaToTsType(s));
21
+ return parts.length ? `(${parts.join(' & ')})` : 'unknown';
22
+ }
23
+ // Handle anyOf (union)
24
+ if (jsonSchema.anyOf) {
25
+ const parts = jsonSchema.anyOf.map((s) => schemaToTsType(s));
26
+ return parts.length ? `(${parts.join(' | ')})` : 'never';
27
+ }
28
+ // Handle oneOf (union)
29
+ if (jsonSchema.oneOf) {
30
+ const parts = jsonSchema.oneOf.map((s) => schemaToTsType(s));
31
+ return parts.length ? `(${parts.join(' | ')})` : 'never';
32
+ }
33
+ // Handle not (negate - approximate as unknown)
34
+ if (jsonSchema.not) {
35
+ return 'unknown';
36
+ }
37
+ // Handle type as array (union of types)
38
+ if (Array.isArray(jsonSchema.type)) {
39
+ const types = jsonSchema.type.map((t) => schemaToTsType({ ...jsonSchema, type: t }));
40
+ return types.length ? `(${types.join(' | ')})` : 'unknown';
41
+ }
42
+ const type = jsonSchema.type;
43
+ // Primitives
44
+ if (type === 'string')
45
+ return 'string';
46
+ if (type === 'number' || type === 'integer')
47
+ return 'number';
48
+ if (type === 'boolean')
49
+ return 'boolean';
50
+ if (type === 'null')
51
+ return 'null';
52
+ // Object
53
+ if (type === 'object' || jsonSchema.properties || jsonSchema.additionalProperties !== undefined) {
54
+ const props = jsonSchema.properties || {};
55
+ const required = jsonSchema.required || [];
56
+ const propEntries = Object.entries(props);
57
+ const propStrings = propEntries.map(([key, value]) => {
58
+ const isRequired = required.includes(key);
59
+ const safeName = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
60
+ return `${safeName}${isRequired ? '' : '?'}: ${schemaToTsType(value)}`;
61
+ });
62
+ // Handle additionalProperties
63
+ let additionalType = null;
64
+ if (jsonSchema.additionalProperties === true) {
65
+ additionalType = 'unknown';
66
+ }
67
+ else if (jsonSchema.additionalProperties && typeof jsonSchema.additionalProperties === 'object') {
68
+ additionalType = schemaToTsType(jsonSchema.additionalProperties);
69
+ }
70
+ if (propStrings.length === 0 && additionalType) {
71
+ return `{ [key: string]: ${additionalType} }`;
72
+ }
73
+ if (propStrings.length === 0 && !additionalType) {
74
+ return jsonSchema.additionalProperties === false ? '{}' : '{ [key: string]: unknown }';
75
+ }
76
+ let result = `{ ${propStrings.join('; ')} }`;
77
+ if (additionalType) {
78
+ result = `(${result} & { [key: string]: ${additionalType} })`;
79
+ }
80
+ return result;
81
+ }
82
+ // Array
83
+ if (type === 'array' || jsonSchema.items || jsonSchema.prefixItems) {
84
+ // Tuple (prefixItems)
85
+ if (jsonSchema.prefixItems) {
86
+ const tupleTypes = jsonSchema.prefixItems.map((s) => schemaToTsType(s));
87
+ if (jsonSchema.items === false) {
88
+ return `[${tupleTypes.join(', ')}]`;
89
+ }
90
+ const restType = jsonSchema.items ? schemaToTsType(jsonSchema.items) : 'unknown';
91
+ return `[${tupleTypes.join(', ')}, ...${restType}[]]`;
92
+ }
93
+ // Regular array
94
+ if (jsonSchema.items) {
95
+ if (Array.isArray(jsonSchema.items)) {
96
+ // Legacy tuple syntax
97
+ const tupleTypes = jsonSchema.items.map((s) => schemaToTsType(s));
98
+ if (jsonSchema.additionalItems === false) {
99
+ return `[${tupleTypes.join(', ')}]`;
100
+ }
101
+ const restType = jsonSchema.additionalItems ? schemaToTsType(jsonSchema.additionalItems) : 'unknown';
102
+ return `[${tupleTypes.join(', ')}, ...${restType}[]]`;
103
+ }
104
+ return `${schemaToTsType(jsonSchema.items)}[]`;
105
+ }
106
+ return 'unknown[]';
107
+ }
108
+ // No type specified - try to infer from structure
109
+ if (jsonSchema.properties) {
110
+ return schemaToTsType({ ...jsonSchema, type: 'object' });
111
+ }
112
+ // Fallback for empty or unknown schema
113
+ return 'unknown';
114
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Collection of functions to convert model outputs into different formats.
3
+ * @see https://vovk.dev/tools
4
+ */
5
+ export declare const ToModelOutput: {
6
+ readonly DEFAULT: import("./toModelOutputDefault.js").ToModelOutputDefaultFn;
7
+ readonly MCP: <TOutput>(result: TOutput | Error, tool: import("../index.js").VovkTool, req: Pick<import("../index.js").VovkRequest, "vovk"> | null) => Promise<import("./toModelOutputMCP.js").MCPModelOutput>;
8
+ };
@@ -0,0 +1,10 @@
1
+ import { toModelOutputDefault } from './toModelOutputDefault.js';
2
+ import { toModelOutputMCP } from './toModelOutputMCP.js';
3
+ /**
4
+ * Collection of functions to convert model outputs into different formats.
5
+ * @see https://vovk.dev/tools
6
+ */
7
+ export const ToModelOutput = {
8
+ DEFAULT: toModelOutputDefault,
9
+ MCP: toModelOutputMCP,
10
+ };
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Function to create a standalone AI tool.
3
+ * @see https://vovk.dev/tools
4
+ */
5
+ export declare const createTool: {
6
+ <TInput, TOutput, TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => import("../types/utils.js").KnownAny>(options: {
7
+ name: string;
8
+ title?: string;
9
+ description: string;
10
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
11
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
12
+ target?: import("../types/validation.js").CombinedSpec.Target;
13
+ } & {
14
+ inputSchema: import("../types/validation.js").CombinedSpec<TInput, TInput>;
15
+ } & {
16
+ outputSchema: import("../types/validation.js").CombinedSpec<TOutput, TOutput>;
17
+ } & {
18
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
19
+ toModelOutput: TToModelOutput;
20
+ }): import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>;
21
+ <TInput, TOutput>(options: {
22
+ name: string;
23
+ title?: string;
24
+ description: string;
25
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
26
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
27
+ target?: import("../types/validation.js").CombinedSpec.Target;
28
+ } & {
29
+ inputSchema: import("../types/validation.js").CombinedSpec<TInput, TInput>;
30
+ } & {
31
+ outputSchema: import("../types/validation.js").CombinedSpec<TOutput, TOutput>;
32
+ } & {
33
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
34
+ toModelOutput?: undefined;
35
+ }): import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>;
36
+ <TInput, TOutput, TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => import("../types/utils.js").KnownAny>(options: {
37
+ name: string;
38
+ title?: string;
39
+ description: string;
40
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
41
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
42
+ target?: import("../types/validation.js").CombinedSpec.Target;
43
+ } & {
44
+ inputSchema: import("../types/validation.js").CombinedSpec<TInput, TInput>;
45
+ } & {
46
+ outputSchema?: undefined;
47
+ } & {
48
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
49
+ toModelOutput: TToModelOutput;
50
+ }): import("../internal.js").VovkToolNonDerived<TInput, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>;
51
+ <TInput, TOutput>(options: {
52
+ name: string;
53
+ title?: string;
54
+ description: string;
55
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
56
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
57
+ target?: import("../types/validation.js").CombinedSpec.Target;
58
+ } & {
59
+ inputSchema: import("../types/validation.js").CombinedSpec<TInput, TInput>;
60
+ } & {
61
+ outputSchema?: undefined;
62
+ } & {
63
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
64
+ toModelOutput?: undefined;
65
+ }): import("../internal.js").VovkToolNonDerived<TInput, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>;
66
+ <TOutput, TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => import("../types/utils.js").KnownAny>(options: {
67
+ name: string;
68
+ title?: string;
69
+ description: string;
70
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
71
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
72
+ target?: import("../types/validation.js").CombinedSpec.Target;
73
+ } & {
74
+ inputSchema?: undefined;
75
+ } & {
76
+ outputSchema: import("../types/validation.js").CombinedSpec<TOutput, TOutput>;
77
+ } & {
78
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
79
+ toModelOutput: TToModelOutput;
80
+ }): import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>;
81
+ <TOutput>(options: {
82
+ name: string;
83
+ title?: string;
84
+ description: string;
85
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
86
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
87
+ target?: import("../types/validation.js").CombinedSpec.Target;
88
+ } & {
89
+ inputSchema?: undefined;
90
+ } & {
91
+ outputSchema: import("../types/validation.js").CombinedSpec<TOutput, TOutput>;
92
+ } & {
93
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
94
+ toModelOutput?: undefined;
95
+ }): import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>;
96
+ <TOutput, TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => import("../types/utils.js").KnownAny>(options: {
97
+ name: string;
98
+ title?: string;
99
+ description: string;
100
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
101
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>) => void) | undefined;
102
+ target?: import("../types/validation.js").CombinedSpec.Target;
103
+ } & {
104
+ inputSchema?: undefined;
105
+ } & {
106
+ outputSchema?: undefined;
107
+ } & {
108
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
109
+ toModelOutput: TToModelOutput;
110
+ }): import("../internal.js").VovkToolNonDerived<null, TOutput, TToModelOutput extends import("./toModelOutputDefault.js").ToModelOutputDefaultFn ? import("./toModelOutputDefault.js").DefaultModelOutput<TOutput> : TToModelOutput extends (...args: import("../types/utils.js").KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown>;
111
+ <TOutput>(options: {
112
+ name: string;
113
+ title?: string;
114
+ description: string;
115
+ onExecute?: ((result: unknown, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
116
+ onError?: ((error: Error, tool: import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>) => void) | undefined;
117
+ target?: import("../types/validation.js").CombinedSpec.Target;
118
+ } & {
119
+ inputSchema?: undefined;
120
+ } & {
121
+ outputSchema?: undefined;
122
+ } & {
123
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
124
+ toModelOutput?: undefined;
125
+ }): import("../internal.js").VovkToolNonDerived<null, TOutput, import("./toModelOutputDefault.js").DefaultModelOutput<TOutput>>;
126
+ };
@@ -0,0 +1,6 @@
1
+ import { procedure } from '../validation/procedure.js';
2
+ /**
3
+ * Function to create a standalone AI tool.
4
+ * @see https://vovk.dev/tools
5
+ */
6
+ export const createTool = procedure.createTool;
@@ -0,0 +1,135 @@
1
+ import type { VovkValidationType } from '../types/core.js';
2
+ import type { VovkToolNonDerived } from '../types/tools.js';
3
+ import type { DefaultModelOutput, ToModelOutputDefaultFn } from './toModelOutputDefault.js';
4
+ import type { CombinedSpec } from '../types/validation.js';
5
+ import type { KnownAny } from '../types/utils.js';
6
+ type InferFormattedOutput<TFn, TOutput> = TFn extends ToModelOutputDefaultFn ? DefaultModelOutput<TOutput> : TFn extends (...args: KnownAny[]) => infer R ? R extends Promise<infer U> ? U : R : unknown;
7
+ type AnyToModelOutputFn = (...args: KnownAny[]) => KnownAny;
8
+ export declare function createToolFactory({ toJSONSchema, }: {
9
+ toJSONSchema: (model: KnownAny, meta: {
10
+ validationType: VovkValidationType;
11
+ target: CombinedSpec.Target | undefined;
12
+ }) => KnownAny;
13
+ }): {
14
+ <TInput, TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
15
+ name: string;
16
+ title?: string;
17
+ description: string;
18
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
19
+ onError?: ((error: Error, tool: VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
20
+ target?: CombinedSpec.Target;
21
+ } & {
22
+ inputSchema: CombinedSpec<TInput, TInput>;
23
+ } & {
24
+ outputSchema: CombinedSpec<TOutput, TOutput>;
25
+ } & {
26
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
27
+ toModelOutput: TToModelOutput;
28
+ }): VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
29
+ <TInput, TOutput>(options: {
30
+ name: string;
31
+ title?: string;
32
+ description: string;
33
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
34
+ onError?: ((error: Error, tool: VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
35
+ target?: CombinedSpec.Target;
36
+ } & {
37
+ inputSchema: CombinedSpec<TInput, TInput>;
38
+ } & {
39
+ outputSchema: CombinedSpec<TOutput, TOutput>;
40
+ } & {
41
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
42
+ toModelOutput?: undefined;
43
+ }): VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>;
44
+ <TInput, TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
45
+ name: string;
46
+ title?: string;
47
+ description: string;
48
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
49
+ onError?: ((error: Error, tool: VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
50
+ target?: CombinedSpec.Target;
51
+ } & {
52
+ inputSchema: CombinedSpec<TInput, TInput>;
53
+ } & {
54
+ outputSchema?: undefined;
55
+ } & {
56
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
57
+ toModelOutput: TToModelOutput;
58
+ }): VovkToolNonDerived<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
59
+ <TInput, TOutput>(options: {
60
+ name: string;
61
+ title?: string;
62
+ description: string;
63
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
64
+ onError?: ((error: Error, tool: VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
65
+ target?: CombinedSpec.Target;
66
+ } & {
67
+ inputSchema: CombinedSpec<TInput, TInput>;
68
+ } & {
69
+ outputSchema?: undefined;
70
+ } & {
71
+ execute: (input: TInput, processingMeta?: unknown) => TOutput | Promise<TOutput>;
72
+ toModelOutput?: undefined;
73
+ }): VovkToolNonDerived<TInput, TOutput, DefaultModelOutput<TOutput>>;
74
+ <TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
75
+ name: string;
76
+ title?: string;
77
+ description: string;
78
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
79
+ onError?: ((error: Error, tool: VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
80
+ target?: CombinedSpec.Target;
81
+ } & {
82
+ inputSchema?: undefined;
83
+ } & {
84
+ outputSchema: CombinedSpec<TOutput, TOutput>;
85
+ } & {
86
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
87
+ toModelOutput: TToModelOutput;
88
+ }): VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
89
+ <TOutput>(options: {
90
+ name: string;
91
+ title?: string;
92
+ description: string;
93
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
94
+ onError?: ((error: Error, tool: VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
95
+ target?: CombinedSpec.Target;
96
+ } & {
97
+ inputSchema?: undefined;
98
+ } & {
99
+ outputSchema: CombinedSpec<TOutput, TOutput>;
100
+ } & {
101
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
102
+ toModelOutput?: undefined;
103
+ }): VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>;
104
+ <TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
105
+ name: string;
106
+ title?: string;
107
+ description: string;
108
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
109
+ onError?: ((error: Error, tool: VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
110
+ target?: CombinedSpec.Target;
111
+ } & {
112
+ inputSchema?: undefined;
113
+ } & {
114
+ outputSchema?: undefined;
115
+ } & {
116
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
117
+ toModelOutput: TToModelOutput;
118
+ }): VovkToolNonDerived<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
119
+ <TOutput>(options: {
120
+ name: string;
121
+ title?: string;
122
+ description: string;
123
+ onExecute?: ((result: unknown, tool: VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
124
+ onError?: ((error: Error, tool: VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
125
+ target?: CombinedSpec.Target;
126
+ } & {
127
+ inputSchema?: undefined;
128
+ } & {
129
+ outputSchema?: undefined;
130
+ } & {
131
+ execute: (input: null, processingMeta?: unknown) => TOutput | Promise<TOutput>;
132
+ toModelOutput?: undefined;
133
+ }): VovkToolNonDerived<null, TOutput, DefaultModelOutput<TOutput>>;
134
+ };
135
+ export {};
@@ -0,0 +1,61 @@
1
+ import { ToModelOutput } from './ToModelOutput.js';
2
+ export function createToolFactory({ toJSONSchema, }) {
3
+ // Implementation
4
+ function createTool({ name, title, description, toModelOutput = ToModelOutput.DEFAULT, onExecute, onError, inputSchema, outputSchema, execute, target, }) {
5
+ let parameters;
6
+ const tool = {
7
+ type: 'function',
8
+ name,
9
+ title,
10
+ description,
11
+ get parameters() {
12
+ return (parameters ??= inputSchema ? toJSONSchema(inputSchema, { validationType: 'tool-input', target }) : {});
13
+ },
14
+ inputSchema: inputSchema,
15
+ outputSchema: outputSchema,
16
+ inputSchemas: undefined,
17
+ async execute(input, processingMeta) {
18
+ let result;
19
+ try {
20
+ let validatedInput;
21
+ if (inputSchema) {
22
+ const validatedInputResult = await inputSchema['~standard'].validate(input);
23
+ if (validatedInputResult.issues?.length) {
24
+ throw new Error(`Input validation failed. ${validatedInputResult.issues
25
+ .map(({ message, path }) => `${message}${path ? ` at ${path.join('.')}` : ''}`)
26
+ .join(', ')}`);
27
+ }
28
+ validatedInput = validatedInputResult.value;
29
+ }
30
+ else {
31
+ validatedInput = null;
32
+ }
33
+ result = await execute(validatedInput, processingMeta);
34
+ if (outputSchema) {
35
+ const validatedOutputResult = await outputSchema['~standard'].validate(result);
36
+ if (validatedOutputResult.issues?.length) {
37
+ throw new Error(`Output validation failed. ${validatedOutputResult.issues
38
+ .map(({ message, path }) => `${message}${path ? ` at ${path.join('.')}` : ''}`)
39
+ .join(', ')}`);
40
+ }
41
+ result = validatedOutputResult.value;
42
+ }
43
+ onExecute?.(result, tool);
44
+ }
45
+ catch (e) {
46
+ onError?.(e, tool);
47
+ result = e;
48
+ }
49
+ if (result instanceof Error) {
50
+ result.toJSON = () => ({
51
+ isError: true,
52
+ message: result.message,
53
+ });
54
+ }
55
+ return toModelOutput(result, tool, null);
56
+ },
57
+ };
58
+ return tool;
59
+ }
60
+ return createTool;
61
+ }