vovk 3.5.1 → 3.7.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 (113) hide show
  1. package/dist/client/create-rpc.d.ts +13 -0
  2. package/dist/client/create-rpc.js +147 -0
  3. package/dist/client/default-handler.d.ts +6 -0
  4. package/dist/client/default-handler.js +25 -0
  5. package/dist/client/default-stream-handler.d.ts +16 -0
  6. package/dist/client/default-stream-handler.js +282 -0
  7. package/dist/client/fetcher.d.ts +1 -1
  8. package/dist/client/fetcher.js +2 -2
  9. package/dist/client/serialize-query.d.ts +13 -0
  10. package/dist/client/serialize-query.js +62 -0
  11. package/dist/core/apply-decorator-adapter.d.ts +7 -0
  12. package/dist/core/apply-decorator-adapter.js +50 -0
  13. package/dist/core/controllers-to-static-params.d.ts +13 -0
  14. package/dist/core/controllers-to-static-params.js +32 -0
  15. package/dist/core/create-decorator.d.ts +12 -0
  16. package/dist/core/create-decorator.js +52 -0
  17. package/dist/core/decorators.js +4 -4
  18. package/dist/core/get-schema.d.ts +21 -0
  19. package/dist/core/get-schema.js +31 -0
  20. package/dist/core/http-exception.d.ts +16 -0
  21. package/dist/core/http-exception.js +26 -0
  22. package/dist/core/init-segment.d.ts +33 -0
  23. package/dist/core/init-segment.js +62 -0
  24. package/dist/core/json-lines-responder.d.ts +42 -0
  25. package/dist/core/json-lines-responder.js +94 -0
  26. package/dist/core/resolve-generator-config-values.d.ts +19 -0
  27. package/dist/core/resolve-generator-config-values.js +59 -0
  28. package/dist/core/set-handler-schema.d.ts +4 -0
  29. package/dist/core/set-handler-schema.js +12 -0
  30. package/dist/core/to-download-response.d.ts +11 -0
  31. package/dist/core/to-download-response.js +25 -0
  32. package/dist/core/vovk-app.d.ts +36 -0
  33. package/dist/core/vovk-app.js +318 -0
  34. package/dist/index.d.ts +10 -10
  35. package/dist/index.js +10 -10
  36. package/dist/internal.d.ts +10 -10
  37. package/dist/internal.js +9 -9
  38. package/dist/openapi/error.js +1 -1
  39. package/dist/openapi/openapi-to-vovk-schema/apply-components-schemas.d.ts +23 -0
  40. package/dist/openapi/openapi-to-vovk-schema/apply-components-schemas.js +90 -0
  41. package/dist/openapi/openapi-to-vovk-schema/index.d.ts +5 -0
  42. package/dist/openapi/openapi-to-vovk-schema/index.js +179 -0
  43. package/dist/openapi/openapi-to-vovk-schema/inline-refs.d.ts +9 -0
  44. package/dist/openapi/openapi-to-vovk-schema/inline-refs.js +99 -0
  45. package/dist/openapi/openapi-to-vovk-schema/prune-components-schemas.d.ts +7 -0
  46. package/dist/openapi/openapi-to-vovk-schema/prune-components-schemas.js +51 -0
  47. package/dist/openapi/operation.js +1 -1
  48. package/dist/openapi/tool.js +1 -1
  49. package/dist/openapi/vovk-schema-to-openapi.d.ts +21 -0
  50. package/dist/openapi/vovk-schema-to-openapi.js +250 -0
  51. package/dist/req/buffer-body.d.ts +1 -0
  52. package/dist/req/buffer-body.js +30 -0
  53. package/dist/req/parse-body.d.ts +4 -0
  54. package/dist/req/parse-body.js +49 -0
  55. package/dist/req/parse-form.d.ts +1 -0
  56. package/dist/req/parse-form.js +24 -0
  57. package/dist/req/parse-query.d.ts +24 -0
  58. package/dist/req/parse-query.js +156 -0
  59. package/dist/req/req-meta.d.ts +2 -0
  60. package/dist/req/req-meta.js +10 -0
  61. package/dist/req/req-query.d.ts +2 -0
  62. package/dist/req/req-query.js +4 -0
  63. package/dist/req/validate-content-type.d.ts +1 -0
  64. package/dist/req/validate-content-type.js +32 -0
  65. package/dist/samples/create-code-samples.d.ts +20 -0
  66. package/dist/samples/create-code-samples.js +293 -0
  67. package/dist/samples/object-to-code.d.ts +8 -0
  68. package/dist/samples/object-to-code.js +38 -0
  69. package/dist/samples/schema-to-code.d.ts +11 -0
  70. package/dist/samples/schema-to-code.js +264 -0
  71. package/dist/samples/schema-to-object.d.ts +2 -0
  72. package/dist/samples/schema-to-object.js +164 -0
  73. package/dist/samples/schema-to-ts-type.d.ts +2 -0
  74. package/dist/samples/schema-to-ts-type.js +114 -0
  75. package/dist/tools/create-tool-factory.d.ts +135 -0
  76. package/dist/tools/create-tool-factory.js +62 -0
  77. package/dist/tools/create-tool.d.ts +126 -0
  78. package/dist/tools/create-tool.js +6 -0
  79. package/dist/tools/derive-tools.d.ts +46 -0
  80. package/dist/tools/derive-tools.js +131 -0
  81. package/dist/tools/to-model-output-default.d.ts +7 -0
  82. package/dist/tools/to-model-output-default.js +7 -0
  83. package/dist/tools/to-model-output-mcp.d.ts +30 -0
  84. package/dist/tools/to-model-output-mcp.js +54 -0
  85. package/dist/tools/to-model-output.d.ts +8 -0
  86. package/dist/tools/to-model-output.js +10 -0
  87. package/dist/types/client.d.ts +3 -3
  88. package/dist/types/core.d.ts +1 -1
  89. package/dist/types/inference.d.ts +1 -1
  90. package/dist/types/validation.d.ts +1 -1
  91. package/dist/utils/camel-case.d.ts +6 -0
  92. package/dist/utils/camel-case.js +34 -0
  93. package/dist/utils/deep-extend.d.ts +54 -0
  94. package/dist/utils/deep-extend.js +127 -0
  95. package/dist/utils/file-name-to-disposition.d.ts +1 -0
  96. package/dist/utils/file-name-to-disposition.js +3 -0
  97. package/dist/utils/to-kebab-case.d.ts +1 -0
  98. package/dist/utils/to-kebab-case.js +5 -0
  99. package/dist/utils/trim-path.d.ts +1 -0
  100. package/dist/utils/trim-path.js +1 -0
  101. package/dist/utils/upper-first.d.ts +1 -0
  102. package/dist/utils/upper-first.js +3 -0
  103. package/dist/validation/create-standard-validation.d.ts +268 -0
  104. package/dist/validation/create-standard-validation.js +45 -0
  105. package/dist/validation/create-validate-on-client.d.ts +14 -0
  106. package/dist/validation/create-validate-on-client.js +23 -0
  107. package/dist/validation/procedure.d.ts +24 -24
  108. package/dist/validation/procedure.js +1 -1
  109. package/dist/validation/validation-schemas-object-to-single-validation-schema.d.ts +17 -0
  110. package/dist/validation/validation-schemas-object-to-single-validation-schema.js +92 -0
  111. package/dist/validation/with-validation-library.d.ts +119 -0
  112. package/dist/validation/with-validation-library.js +184 -0
  113. package/package.json +13 -5
@@ -0,0 +1,135 @@
1
+ import type { VovkValidationType } from '../types/core.js';
2
+ import type { VovkTool } from '../types/tools.js';
3
+ import type { DefaultModelOutput, ToModelOutputDefaultFn } from './to-model-output-default.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: VovkTool<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
19
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
27
+ toModelOutput: TToModelOutput;
28
+ }): VovkTool<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
29
+ <TInput, TOutput>(options: {
30
+ name: string;
31
+ title?: string;
32
+ description: string;
33
+ onExecute?: ((result: unknown, tool: VovkTool<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
34
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
42
+ toModelOutput?: undefined;
43
+ }): VovkTool<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: VovkTool<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
49
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
57
+ toModelOutput: TToModelOutput;
58
+ }): VovkTool<TInput, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
59
+ <TInput, TOutput>(options: {
60
+ name: string;
61
+ title?: string;
62
+ description: string;
63
+ onExecute?: ((result: unknown, tool: VovkTool<TInput, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
64
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
72
+ toModelOutput?: undefined;
73
+ }): VovkTool<TInput, TOutput, DefaultModelOutput<TOutput>>;
74
+ <TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
75
+ name: string;
76
+ title?: string;
77
+ description: string;
78
+ onExecute?: ((result: unknown, tool: VovkTool<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
79
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
87
+ toModelOutput: TToModelOutput;
88
+ }): VovkTool<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
89
+ <TOutput>(options: {
90
+ name: string;
91
+ title?: string;
92
+ description: string;
93
+ onExecute?: ((result: unknown, tool: VovkTool<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
94
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
102
+ toModelOutput?: undefined;
103
+ }): VovkTool<null, TOutput, DefaultModelOutput<TOutput>>;
104
+ <TOutput, TToModelOutput extends AnyToModelOutputFn>(options: {
105
+ name: string;
106
+ title?: string;
107
+ description: string;
108
+ onExecute?: ((result: unknown, tool: VovkTool<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>) => void) | undefined;
109
+ onError?: ((error: Error, tool: VovkTool<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?: KnownAny) => TOutput | Promise<TOutput>;
117
+ toModelOutput: TToModelOutput;
118
+ }): VovkTool<null, TOutput, InferFormattedOutput<TToModelOutput, TOutput>>;
119
+ <TOutput>(options: {
120
+ name: string;
121
+ title?: string;
122
+ description: string;
123
+ onExecute?: ((result: unknown, tool: VovkTool<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
124
+ onError?: ((error: Error, tool: VovkTool<null, TOutput, DefaultModelOutput<TOutput>>) => void) | undefined;
125
+ target?: CombinedSpec.Target;
126
+ } & {
127
+ inputSchema?: undefined;
128
+ } & {
129
+ outputSchema?: undefined;
130
+ } & {
131
+ execute: (input: null, processingMeta?: KnownAny) => TOutput | Promise<TOutput>;
132
+ toModelOutput?: undefined;
133
+ }): VovkTool<null, TOutput, DefaultModelOutput<TOutput>>;
134
+ };
135
+ export {};
@@ -0,0 +1,62 @@
1
+ import { ToModelOutput } from './to-model-output.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
+ // biome-ignore lint/suspicious/noAssignInExpressions: TODO
13
+ return (parameters ??= inputSchema ? toJSONSchema(inputSchema, { validationType: 'tool-input', target }) : {});
14
+ },
15
+ inputSchema: inputSchema,
16
+ outputSchema: outputSchema,
17
+ inputSchemas: undefined,
18
+ async execute(input, processingMeta) {
19
+ let result;
20
+ try {
21
+ let validatedInput;
22
+ if (inputSchema) {
23
+ const validatedInputResult = await inputSchema['~standard'].validate(input);
24
+ if (validatedInputResult.issues?.length) {
25
+ throw new Error(`Input validation failed. ${validatedInputResult.issues
26
+ .map(({ message, path }) => `${message}${path ? ` at ${path.join('.')}` : ''}`)
27
+ .join(', ')}`);
28
+ }
29
+ validatedInput = validatedInputResult.value;
30
+ }
31
+ else {
32
+ validatedInput = null;
33
+ }
34
+ result = await execute(validatedInput, processingMeta);
35
+ if (outputSchema) {
36
+ const validatedOutputResult = await outputSchema['~standard'].validate(result);
37
+ if (validatedOutputResult.issues?.length) {
38
+ throw new Error(`Output validation failed. ${validatedOutputResult.issues
39
+ .map(({ message, path }) => `${message}${path ? ` at ${path.join('.')}` : ''}`)
40
+ .join(', ')}`);
41
+ }
42
+ result = validatedOutputResult.value;
43
+ }
44
+ onExecute?.(result, tool);
45
+ }
46
+ catch (e) {
47
+ onError?.(e, tool);
48
+ result = e;
49
+ }
50
+ if (result instanceof Error) {
51
+ result.toJSON = () => ({
52
+ isError: true,
53
+ message: result.message,
54
+ });
55
+ }
56
+ return toModelOutput(result, tool, null);
57
+ },
58
+ };
59
+ return tool;
60
+ }
61
+ return createTool;
62
+ }
@@ -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("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
19
+ toModelOutput: TToModelOutput;
20
+ }): import("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.js").DefaultModelOutput<TOutput>>) => void) | undefined;
26
+ onError?: ((error: Error, tool: import("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
34
+ toModelOutput?: undefined;
35
+ }): import("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.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("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
49
+ toModelOutput: TToModelOutput;
50
+ }): import("../index.js").VovkTool<TInput, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.js").DefaultModelOutput<TOutput>>) => void) | undefined;
56
+ onError?: ((error: Error, tool: import("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
64
+ toModelOutput?: undefined;
65
+ }): import("../index.js").VovkTool<TInput, TOutput, import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
79
+ toModelOutput: TToModelOutput;
80
+ }): import("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.js").DefaultModelOutput<TOutput>>) => void) | undefined;
86
+ onError?: ((error: Error, tool: import("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
94
+ toModelOutput?: undefined;
95
+ }): import("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
109
+ toModelOutput: TToModelOutput;
110
+ }): import("../index.js").VovkTool<null, TOutput, TToModelOutput extends import("./to-model-output-default.js").ToModelOutputDefaultFn ? import("./to-model-output-default.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("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.js").DefaultModelOutput<TOutput>>) => void) | undefined;
116
+ onError?: ((error: Error, tool: import("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.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?: import("../types/utils.js").KnownAny) => TOutput | Promise<TOutput>;
124
+ toModelOutput?: undefined;
125
+ }): import("../index.js").VovkTool<null, TOutput, import("./to-model-output-default.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,46 @@
1
+ import type { DefaultModelOutput } from './to-model-output-default.js';
2
+ import type { VovkRequest } from '../types/request.js';
3
+ import type { VovkTool, ToModelOutputFn } from '../types/tools.js';
4
+ type DerivedToolInput = {
5
+ body?: unknown;
6
+ query?: unknown;
7
+ params?: unknown;
8
+ };
9
+ type DeriveToolsBaseOptions<TOutput = unknown, TFormattedOutput = unknown> = {
10
+ modules: Record<string, object>;
11
+ meta?: Record<string, unknown>;
12
+ onExecute?: (result: unknown, tool: VovkTool<DerivedToolInput, TOutput, TFormattedOutput>, req: Pick<VovkRequest, 'vovk'> | null) => void;
13
+ onError?: (error: Error, tool: VovkTool<DerivedToolInput, TOutput, TFormattedOutput>, req: Pick<VovkRequest, 'vovk'> | null) => void;
14
+ };
15
+ type DeriveToolsResult<TOutput, TFormattedOutput> = {
16
+ tools: VovkTool<DerivedToolInput, TOutput, TFormattedOutput>[];
17
+ toolsByName: Record<string, VovkTool<DerivedToolInput, TOutput, TFormattedOutput>>;
18
+ };
19
+ /**
20
+ * Derives AI tools from controllers and RPC modules.
21
+ * @see https://vovk.dev/tools
22
+ * @example
23
+ * ```ts
24
+ * import { deriveTools, ToModelOutput } from 'vovk';
25
+ * import { UserRPC } from 'vovk-client';
26
+ *
27
+ * // Derive AI tools from the UserRPC module
28
+ * const { tools, toolsByName } = deriveTools({
29
+ * modules: { UserRPC },
30
+ * toModelOutput: ToModelOutput.MCP,
31
+ * onExecute: (result, tool) => {
32
+ * console.log(`Tool ${tool.name} executed successfully.`);
33
+ * },
34
+ * onError: (error, tool) => {
35
+ * console.error(`Tool ${tool.name} execution failed:`, error);
36
+ * },
37
+ * });
38
+ * ```
39
+ */
40
+ export declare function deriveTools<TOutput = unknown, TFormattedOutput = DefaultModelOutput<TOutput>>(options: DeriveToolsBaseOptions & {
41
+ toModelOutput?: never;
42
+ }): DeriveToolsResult<TOutput, TFormattedOutput>;
43
+ export declare function deriveTools<TOutput = unknown, TFormattedOutput = unknown>(options: DeriveToolsBaseOptions & {
44
+ toModelOutput: ToModelOutputFn<unknown, TOutput, TFormattedOutput>;
45
+ }): DeriveToolsResult<TOutput, TFormattedOutput>;
46
+ export {};
@@ -0,0 +1,131 @@
1
+ import { ToModelOutput } from './to-model-output.js';
2
+ import { validationSchemasObjectToSingleValidationSchema } from '../validation/validation-schemas-object-to-single-validation-schema.js';
3
+ async function caller({ handler, handlerName, body, query, params, meta, toModelOutput }, tool) {
4
+ if (!handler.isRPC && !handler.fn) {
5
+ throw new Error('Handler is not a valid RPC or controller method');
6
+ }
7
+ try {
8
+ let result;
9
+ let req = null;
10
+ if (handler.isRPC) {
11
+ result = await handler({
12
+ handler,
13
+ body,
14
+ query,
15
+ params,
16
+ meta,
17
+ });
18
+ }
19
+ else if (handler.fn) {
20
+ [result, req] = await handler.fn({
21
+ body,
22
+ query,
23
+ params,
24
+ meta,
25
+ transform: (result, req) => [result, req],
26
+ });
27
+ }
28
+ else {
29
+ throw new Error(`Unable to call handler "${handlerName}". It's neither RPC nor controller method with "fn" interface.`);
30
+ }
31
+ return [await toModelOutput(result, tool, req), req];
32
+ }
33
+ catch (e) {
34
+ return [await toModelOutput(e, tool, null), null];
35
+ }
36
+ }
37
+ const makeTool = ({ moduleName, handlerName, module, meta, toModelOutput, onExecute, onError, }) => {
38
+ if (!module) {
39
+ throw new Error(`Module "${moduleName}" not found.`);
40
+ }
41
+ const handler = module[handlerName];
42
+ if (!handler) {
43
+ throw new Error(`Handler "${handlerName}" not found in module "${moduleName}".`);
44
+ }
45
+ const { schema, definition } = handler;
46
+ const inputSchemas = Object.fromEntries(['body', 'query', 'params'].map((key) => [key, definition?.[key]]).filter(([, value]) => Boolean(value)));
47
+ const inputSchema = Object.keys(inputSchemas).length > 0 ? validationSchemasObjectToSingleValidationSchema(inputSchemas) : undefined;
48
+ if (!schema?.operationObject) {
49
+ throw new Error(`Handler "${handlerName}" in module "${moduleName}" does not have a valid schema.`);
50
+ }
51
+ const execute = async (input) => {
52
+ const { body, query, params } = input;
53
+ const callerInput = {
54
+ schema,
55
+ inputSchemas,
56
+ handler,
57
+ body,
58
+ query,
59
+ params,
60
+ meta,
61
+ handlerName,
62
+ moduleName,
63
+ toModelOutput,
64
+ };
65
+ const [result, req] = await caller(callerInput, tool);
66
+ if (result instanceof Error) {
67
+ onError(result, tool, req);
68
+ }
69
+ else {
70
+ onExecute(result, tool, req);
71
+ }
72
+ return result;
73
+ };
74
+ const parametersProperties = {
75
+ ...(schema?.validation?.body
76
+ ? {
77
+ body: schema.validation.body,
78
+ }
79
+ : {}),
80
+ ...(schema?.validation?.query
81
+ ? {
82
+ query: schema.validation.query,
83
+ }
84
+ : {}),
85
+ ...(schema?.validation?.params
86
+ ? {
87
+ params: schema.validation.params,
88
+ }
89
+ : {}),
90
+ };
91
+ const tool = {
92
+ type: 'function',
93
+ execute,
94
+ name: schema.operationObject?.['x-tool']?.name ?? `${moduleName}_${handlerName}`,
95
+ inputSchema: inputSchema,
96
+ outputSchema: definition?.output,
97
+ title: schema.operationObject?.['x-tool']?.title ?? schema.operationObject?.summary,
98
+ description: schema.operationObject?.['x-tool']?.description ??
99
+ ([schema.operationObject?.summary ?? '', schema.operationObject?.description ?? ''].filter(Boolean).join('\n') ||
100
+ handlerName),
101
+ parameters: {
102
+ type: 'object',
103
+ properties: parametersProperties,
104
+ required: Object.keys(parametersProperties),
105
+ additionalProperties: false,
106
+ },
107
+ inputSchemas,
108
+ };
109
+ return tool;
110
+ };
111
+ export function deriveTools(options) {
112
+ const { modules, meta, toModelOutput = ToModelOutput.DEFAULT, onExecute = (result) => result, onError = () => { }, } = options;
113
+ const tools = Object.entries(modules ?? {}).flatMap(([moduleName, module]) => {
114
+ return Object.entries(module ?? {})
115
+ .filter(([, handler]) => handler?.schema?.operationObject && !handler?.schema?.operationObject?.['x-tool']?.hidden)
116
+ .map(([handlerName]) => makeTool({
117
+ moduleName,
118
+ handlerName,
119
+ module,
120
+ meta,
121
+ toModelOutput,
122
+ onExecute,
123
+ onError,
124
+ }));
125
+ });
126
+ const toolsByName = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
127
+ return {
128
+ tools,
129
+ toolsByName,
130
+ };
131
+ }
@@ -0,0 +1,7 @@
1
+ import type { VovkRequest } from '../types/request.js';
2
+ import type { VovkTool } from '../types/tools.js';
3
+ export type DefaultModelOutput<T> = T | {
4
+ error: string;
5
+ };
6
+ export type ToModelOutputDefaultFn = <TInput, TOutput>(result: TOutput | Error, tool: VovkTool<TInput, TOutput, unknown>, req: Pick<VovkRequest, 'vovk'> | null) => DefaultModelOutput<TOutput>;
7
+ export declare const toModelOutputDefault: ToModelOutputDefaultFn;
@@ -0,0 +1,7 @@
1
+ export const toModelOutputDefault = (result,
2
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
3
+ _tool,
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
+ _req) => {
6
+ return result instanceof Error ? { error: result.message } : result;
7
+ };
@@ -0,0 +1,30 @@
1
+ import type { VovkRequest } from '../types/request.js';
2
+ import type { VovkTool } from '../types/tools.js';
3
+ export type MCPModelOutput = {
4
+ content: [
5
+ {
6
+ type: 'audio';
7
+ mimeType: string;
8
+ data: string;
9
+ } | {
10
+ type: 'image';
11
+ mimeType: string;
12
+ data: string;
13
+ } | {
14
+ type: 'text';
15
+ text: string;
16
+ }
17
+ ];
18
+ annotations?: {
19
+ audience?: ('user' | 'assistant')[];
20
+ priority?: number;
21
+ lastModified?: string;
22
+ };
23
+ structuredContent?: {
24
+ [key: string]: unknown;
25
+ };
26
+ isError?: boolean;
27
+ };
28
+ type ToModelOutputMCPFn = <TOutput>(result: TOutput | Error, tool: VovkTool, req: Pick<VovkRequest, 'vovk'> | null) => Promise<MCPModelOutput>;
29
+ export declare const toModelOutputMCP: ToModelOutputMCPFn;
30
+ export {};
@@ -0,0 +1,54 @@
1
+ import { reqMeta } from '../req/req-meta.js';
2
+ // converts array to object with "items" key for MCP structured content
3
+ const structuredContentToObject = (data) => {
4
+ if (Array.isArray(data)) {
5
+ return { items: data };
6
+ }
7
+ return data;
8
+ };
9
+ const toBase64 = (buf) => typeof Buffer !== 'undefined'
10
+ ? Buffer.from(buf).toString('base64')
11
+ : btoa([...new Uint8Array(buf)].map((b) => String.fromCharCode(b)).join(''));
12
+ async function responseToMCP(res) {
13
+ const mimeType = res.headers.get('Content-Type')?.split(';')[0].trim() || '';
14
+ if (mimeType.startsWith('audio/')) {
15
+ return { content: [{ type: 'audio', mimeType, data: toBase64(await res.arrayBuffer()) }] };
16
+ }
17
+ if (mimeType.startsWith('image/')) {
18
+ return { content: [{ type: 'image', mimeType, data: toBase64(await res.arrayBuffer()) }] };
19
+ }
20
+ if (mimeType === 'application/json') {
21
+ const structuredContent = await res.json();
22
+ return {
23
+ content: [{ type: 'text', text: JSON.stringify(structuredContent) }],
24
+ structuredContent: structuredContentToObject(structuredContent),
25
+ };
26
+ }
27
+ if (mimeType.startsWith('text/') || /xml|javascript|yaml/.test(mimeType)) {
28
+ return { content: [{ type: 'text', text: await res.text() }] };
29
+ }
30
+ return {
31
+ content: [{ type: 'text', text: `Unsupported response content type ${mimeType}` }],
32
+ isError: true,
33
+ };
34
+ }
35
+ export const toModelOutputMCP = async (result, _tool, req) => {
36
+ const mcpOutputMeta = req ? reqMeta(req).mcpOutput : null;
37
+ if (result instanceof Response) {
38
+ return { ...(await responseToMCP(result)), ...(mcpOutputMeta || {}) };
39
+ }
40
+ const isError = result instanceof Error;
41
+ return {
42
+ content: [
43
+ {
44
+ type: 'text',
45
+ text: isError ? result.message : JSON.stringify(result),
46
+ },
47
+ ],
48
+ ...(isError ? { isError: true } : {}),
49
+ ...(!isError && typeof result === 'object' && result !== null
50
+ ? { structuredContent: structuredContentToObject(result) }
51
+ : {}),
52
+ ...(mcpOutputMeta || {}),
53
+ };
54
+ };
@@ -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("./to-model-output-default.js").ToModelOutputDefaultFn;
7
+ readonly MCP: <TOutput>(result: TOutput | Error, tool: import("../index.js").VovkTool, req: Pick<import("../index.js").VovkRequest, "vovk"> | null) => Promise<import("./to-model-output-mcp.js").MCPModelOutput>;
8
+ };
@@ -0,0 +1,10 @@
1
+ import { toModelOutputDefault } from './to-model-output-default.js';
2
+ import { toModelOutputMCP } from './to-model-output-mcp.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
+ };
@@ -1,7 +1,7 @@
1
1
  import type { NextResponse } from 'next/server.js';
2
- import type { JSONLinesResponder } from '../core/JSONLinesResponder.js';
3
- import type { defaultStreamHandler } from '../client/defaultStreamHandler.js';
4
- import type { defaultHandler } from '../client/defaultHandler.js';
2
+ import type { JSONLinesResponder } from '../core/json-lines-responder.js';
3
+ import type { defaultStreamHandler } from '../client/default-stream-handler.js';
4
+ import type { defaultHandler } from '../client/default-handler.js';
5
5
  import type { VovkRequest } from './request.js';
6
6
  import type { ControllerStaticMethod, VovkControllerSchema, VovkHandlerSchema, VovkSchema, VovkSegmentSchema } from './core.js';
7
7
  import type { IsEmptyObject, KnownAny, Prettify } from './utils.js';