@typia/vercel 12.0.0-dev.20260225

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Jeongho Nam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # `@typia/vercel`
2
+
3
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/typia/blob/master/LICENSE)
4
+ [![NPM Version](https://img.shields.io/npm/v/typia.svg)](https://www.npmjs.com/package/typia)
5
+ [![NPM Downloads](https://img.shields.io/npm/dm/typia.svg)](https://www.npmjs.com/package/typia)
6
+
7
+ [Vercel AI SDK](https://github.com/vercel/ai) integration for [`typia`](https://github.com/samchon/typia).
8
+
9
+ Converts typia controllers to Vercel AI SDK tools compatible with OpenAI, Anthropic, Google, and other LLM providers.
10
+
11
+ ## Setup
12
+
13
+ ```bash
14
+ npm install @typia/vercel ai
15
+ npm install typia
16
+ npx typia setup
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### From TypeScript class
22
+
23
+ ```typescript
24
+ import { openai } from "@ai-sdk/openai";
25
+ import { toVercelTools } from "@typia/vercel";
26
+ import { generateText, GenerateTextResult, Tool } from "ai";
27
+ import typia from "typia";
28
+
29
+ const tools: Record<string, Tool> = toVercelTools({
30
+ controllers: [
31
+ typia.llm.controller<Calculator>("Calculator", new Calculator()),
32
+ ],
33
+ });
34
+
35
+ const result: GenerateTextResult = await generateText({
36
+ model: openai("gpt-4o"),
37
+ prompt: "What is 10 + 5?",
38
+ tools,
39
+ });
40
+ ```
41
+
42
+ ### From OpenAPI document
43
+
44
+ ```typescript
45
+ import { toVercelTools } from "@typia/vercel";
46
+ import { HttpLlm } from "@typia/utils";
47
+ import { Tool } from "ai";
48
+
49
+ const tools: Record<string, Tool> = toVercelTools({
50
+ controllers: [
51
+ HttpLlm.controller({
52
+ name: "petStore",
53
+ document: yourOpenApiDocument,
54
+ connection: { host: "https://api.example.com" },
55
+ }),
56
+ ],
57
+ });
58
+ ```
59
+
60
+ ## Features
61
+
62
+ - No manual schema definition — generates everything from TypeScript types or OpenAPI
63
+ - Automatic argument validation with LLM-friendly error feedback
64
+ - Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
65
+ - Works with any LLM provider supported by Vercel AI SDK
package/lib/index.d.ts ADDED
@@ -0,0 +1,97 @@
1
+ import type { Tool } from "ai";
2
+ import { IHttpLlmController, ILlmController } from "@typia/interface";
3
+ /**
4
+ * Convert typia controllers to Vercel AI SDK tools.
5
+ *
6
+ * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
7
+ * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
8
+ * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
9
+ *
10
+ * Every tool call is validated by typia. If the LLM provides invalid arguments,
11
+ * returns a validation error formatted by {@link stringifyValidationFailure}
12
+ * so the LLM can auto-correct in the next turn.
13
+ *
14
+ * ## Example with OpenAI
15
+ *
16
+ * ```typescript
17
+ * import { generateText } from "ai";
18
+ * import { openai } from "@ai-sdk/openai";
19
+ * import typia from "typia";
20
+ * import { toVercelTools } from "@typia/vercel";
21
+ *
22
+ * class Calculator {
23
+ * /&#42;&#42; Add two numbers together. &#42;/
24
+ * add(props: { a: number; b: number }): number {
25
+ * return props.a + props.b;
26
+ * }
27
+ * }
28
+ *
29
+ * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
30
+ * const tools = toVercelTools({ controllers: [controller] });
31
+ *
32
+ * const result = await generateText({
33
+ * model: openai("gpt-4o"),
34
+ * tools,
35
+ * prompt: "What is 15 + 27?",
36
+ * });
37
+ * ```
38
+ *
39
+ * ## Example with Anthropic
40
+ *
41
+ * ```typescript
42
+ * import { generateText } from "ai";
43
+ * import { anthropic } from "@ai-sdk/anthropic";
44
+ * import { toVercelTools } from "@typia/vercel";
45
+ *
46
+ * const result = await generateText({
47
+ * model: anthropic("claude-sonnet-4-20250514"),
48
+ * tools: toVercelTools({ controllers: [controller] }),
49
+ * prompt: "Calculate 100 * 50",
50
+ * });
51
+ * ```
52
+ *
53
+ * ## Example with HTTP Controller (OpenAPI)
54
+ *
55
+ * ```typescript
56
+ * import { generateText } from "ai";
57
+ * import { openai } from "@ai-sdk/openai";
58
+ * import { HttpLlm } from "@typia/utils";
59
+ * import { toVercelTools } from "@typia/vercel";
60
+ *
61
+ * const controller = HttpLlm.controller({
62
+ * name: "shopping",
63
+ * document: await fetch("https://api.example.com/swagger.json").then(r => r.json()),
64
+ * connection: { host: "https://api.example.com" },
65
+ * });
66
+ *
67
+ * const result = await generateText({
68
+ * model: openai("gpt-4o"),
69
+ * tools: toVercelTools({ controllers: [controller] }),
70
+ * prompt: "Search for laptops under $1000",
71
+ * });
72
+ * ```
73
+ *
74
+ * @param props Conversion properties
75
+ * @returns Record of Vercel AI SDK Tools keyed by tool name
76
+ * @author Jeongho Nam - https://github.com/samchon
77
+ */
78
+ export declare function toVercelTools(props: {
79
+ /**
80
+ * List of controllers to convert to Vercel tools.
81
+ *
82
+ * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
83
+ * methods of the class to tools
84
+ * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
85
+ * operations from OpenAPI document to tools
86
+ */
87
+ controllers: Array<ILlmController | IHttpLlmController>;
88
+ /**
89
+ * Whether to prefix tool names with controller name.
90
+ *
91
+ * If `true`, tool names are formatted as `{controller}_{function}`.
92
+ * If `false`, only the function name is used (may cause conflicts).
93
+ *
94
+ * @default true
95
+ */
96
+ prefix?: boolean | undefined;
97
+ }): Record<string, Tool>;
package/lib/index.js ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toVercelTools = toVercelTools;
4
+ const VercelToolsRegistrar_1 = require("./internal/VercelToolsRegistrar");
5
+ /**
6
+ * Convert typia controllers to Vercel AI SDK tools.
7
+ *
8
+ * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
9
+ * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
10
+ * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
11
+ *
12
+ * Every tool call is validated by typia. If the LLM provides invalid arguments,
13
+ * returns a validation error formatted by {@link stringifyValidationFailure}
14
+ * so the LLM can auto-correct in the next turn.
15
+ *
16
+ * ## Example with OpenAI
17
+ *
18
+ * ```typescript
19
+ * import { generateText } from "ai";
20
+ * import { openai } from "@ai-sdk/openai";
21
+ * import typia from "typia";
22
+ * import { toVercelTools } from "@typia/vercel";
23
+ *
24
+ * class Calculator {
25
+ * /&#42;&#42; Add two numbers together. &#42;/
26
+ * add(props: { a: number; b: number }): number {
27
+ * return props.a + props.b;
28
+ * }
29
+ * }
30
+ *
31
+ * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
32
+ * const tools = toVercelTools({ controllers: [controller] });
33
+ *
34
+ * const result = await generateText({
35
+ * model: openai("gpt-4o"),
36
+ * tools,
37
+ * prompt: "What is 15 + 27?",
38
+ * });
39
+ * ```
40
+ *
41
+ * ## Example with Anthropic
42
+ *
43
+ * ```typescript
44
+ * import { generateText } from "ai";
45
+ * import { anthropic } from "@ai-sdk/anthropic";
46
+ * import { toVercelTools } from "@typia/vercel";
47
+ *
48
+ * const result = await generateText({
49
+ * model: anthropic("claude-sonnet-4-20250514"),
50
+ * tools: toVercelTools({ controllers: [controller] }),
51
+ * prompt: "Calculate 100 * 50",
52
+ * });
53
+ * ```
54
+ *
55
+ * ## Example with HTTP Controller (OpenAPI)
56
+ *
57
+ * ```typescript
58
+ * import { generateText } from "ai";
59
+ * import { openai } from "@ai-sdk/openai";
60
+ * import { HttpLlm } from "@typia/utils";
61
+ * import { toVercelTools } from "@typia/vercel";
62
+ *
63
+ * const controller = HttpLlm.controller({
64
+ * name: "shopping",
65
+ * document: await fetch("https://api.example.com/swagger.json").then(r => r.json()),
66
+ * connection: { host: "https://api.example.com" },
67
+ * });
68
+ *
69
+ * const result = await generateText({
70
+ * model: openai("gpt-4o"),
71
+ * tools: toVercelTools({ controllers: [controller] }),
72
+ * prompt: "Search for laptops under $1000",
73
+ * });
74
+ * ```
75
+ *
76
+ * @param props Conversion properties
77
+ * @returns Record of Vercel AI SDK Tools keyed by tool name
78
+ * @author Jeongho Nam - https://github.com/samchon
79
+ */
80
+ function toVercelTools(props) {
81
+ return VercelToolsRegistrar_1.VercelToolsRegistrar.convert(props);
82
+ }
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAgFA,sCAsBC;AAnGD,0EAAuE;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0EG;AACH,SAAgB,aAAa,CAAC,KAoB7B;IACC,OAAO,2CAAoB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7C,CAAC"}
package/lib/index.mjs ADDED
@@ -0,0 +1,83 @@
1
+ import { VercelToolsRegistrar } from './internal/VercelToolsRegistrar.mjs';
2
+
3
+ /**
4
+ * Convert typia controllers to Vercel AI SDK tools.
5
+ *
6
+ * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
7
+ * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
8
+ * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
9
+ *
10
+ * Every tool call is validated by typia. If the LLM provides invalid arguments,
11
+ * returns a validation error formatted by {@link stringifyValidationFailure}
12
+ * so the LLM can auto-correct in the next turn.
13
+ *
14
+ * ## Example with OpenAI
15
+ *
16
+ * ```typescript
17
+ * import { generateText } from "ai";
18
+ * import { openai } from "@ai-sdk/openai";
19
+ * import typia from "typia";
20
+ * import { toVercelTools } from "@typia/vercel";
21
+ *
22
+ * class Calculator {
23
+ * /&#42;&#42; Add two numbers together. &#42;/
24
+ * add(props: { a: number; b: number }): number {
25
+ * return props.a + props.b;
26
+ * }
27
+ * }
28
+ *
29
+ * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
30
+ * const tools = toVercelTools({ controllers: [controller] });
31
+ *
32
+ * const result = await generateText({
33
+ * model: openai("gpt-4o"),
34
+ * tools,
35
+ * prompt: "What is 15 + 27?",
36
+ * });
37
+ * ```
38
+ *
39
+ * ## Example with Anthropic
40
+ *
41
+ * ```typescript
42
+ * import { generateText } from "ai";
43
+ * import { anthropic } from "@ai-sdk/anthropic";
44
+ * import { toVercelTools } from "@typia/vercel";
45
+ *
46
+ * const result = await generateText({
47
+ * model: anthropic("claude-sonnet-4-20250514"),
48
+ * tools: toVercelTools({ controllers: [controller] }),
49
+ * prompt: "Calculate 100 * 50",
50
+ * });
51
+ * ```
52
+ *
53
+ * ## Example with HTTP Controller (OpenAPI)
54
+ *
55
+ * ```typescript
56
+ * import { generateText } from "ai";
57
+ * import { openai } from "@ai-sdk/openai";
58
+ * import { HttpLlm } from "@typia/utils";
59
+ * import { toVercelTools } from "@typia/vercel";
60
+ *
61
+ * const controller = HttpLlm.controller({
62
+ * name: "shopping",
63
+ * document: await fetch("https://api.example.com/swagger.json").then(r => r.json()),
64
+ * connection: { host: "https://api.example.com" },
65
+ * });
66
+ *
67
+ * const result = await generateText({
68
+ * model: openai("gpt-4o"),
69
+ * tools: toVercelTools({ controllers: [controller] }),
70
+ * prompt: "Search for laptops under $1000",
71
+ * });
72
+ * ```
73
+ *
74
+ * @param props Conversion properties
75
+ * @returns Record of Vercel AI SDK Tools keyed by tool name
76
+ * @author Jeongho Nam - https://github.com/samchon
77
+ */
78
+ function toVercelTools(props) {
79
+ return VercelToolsRegistrar.convert(props);
80
+ }
81
+
82
+ export { toVercelTools };
83
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0EG;AACG,SAAU,aAAa,CAAC,KAoB7B,EAAA;AACC,IAAA,OAAO,oBAAoB,CAAC,OAAO,CAAC,KAAK,CAAC;AAC5C;;;;"}
@@ -0,0 +1,14 @@
1
+ import type { Tool } from "ai";
2
+ import { IHttpLlmController, ILlmController } from "@typia/interface";
3
+ export declare namespace VercelToolsRegistrar {
4
+ /**
5
+ * Convert typia controllers to Vercel AI SDK tools.
6
+ *
7
+ * @param props Conversion properties
8
+ * @returns Record of Vercel AI SDK Tools
9
+ */
10
+ const convert: (props: {
11
+ controllers: Array<ILlmController | IHttpLlmController>;
12
+ prefix?: boolean | undefined;
13
+ }) => Record<string, Tool>;
14
+ }
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.VercelToolsRegistrar = void 0;
13
+ const ai_1 = require("ai");
14
+ const utils_1 = require("@typia/utils");
15
+ var VercelToolsRegistrar;
16
+ (function (VercelToolsRegistrar) {
17
+ /**
18
+ * Convert typia controllers to Vercel AI SDK tools.
19
+ *
20
+ * @param props Conversion properties
21
+ * @returns Record of Vercel AI SDK Tools
22
+ */
23
+ VercelToolsRegistrar.convert = (props) => {
24
+ var _a;
25
+ const tools = {};
26
+ const prefix = (_a = props.prefix) !== null && _a !== void 0 ? _a : true;
27
+ for (const controller of props.controllers) {
28
+ if (controller.protocol === "class") {
29
+ registerClassController({
30
+ tools,
31
+ controller,
32
+ prefix,
33
+ });
34
+ }
35
+ else {
36
+ registerHttpController({
37
+ tools,
38
+ controller,
39
+ prefix,
40
+ });
41
+ }
42
+ }
43
+ return tools;
44
+ };
45
+ const registerClassController = (props) => {
46
+ const { tools, controller, prefix } = props;
47
+ const execute = controller.execute;
48
+ for (const func of controller.application.functions) {
49
+ const toolName = prefix
50
+ ? `${controller.name}_${func.name}`
51
+ : func.name;
52
+ if (tools[toolName] !== undefined) {
53
+ throw new Error(`Duplicate tool name "${toolName}" from controller "${controller.name}"`);
54
+ }
55
+ const method = execute[func.name];
56
+ if (typeof method !== "function") {
57
+ throw new Error(`Method "${func.name}" not found on controller "${controller.name}"`);
58
+ }
59
+ tools[toolName] = createTool({
60
+ func,
61
+ execute: (args) => __awaiter(this, void 0, void 0, function* () { return method.call(execute, args); }),
62
+ });
63
+ }
64
+ };
65
+ const registerHttpController = (props) => {
66
+ const { tools, controller, prefix } = props;
67
+ const application = controller.application;
68
+ const connection = controller.connection;
69
+ for (const func of application.functions) {
70
+ const toolName = prefix
71
+ ? `${controller.name}_${func.name}`
72
+ : func.name;
73
+ if (tools[toolName] !== undefined) {
74
+ throw new Error(`Duplicate tool name "${toolName}" from controller "${controller.name}"`);
75
+ }
76
+ tools[toolName] = createTool({
77
+ func,
78
+ execute: (args) => __awaiter(this, void 0, void 0, function* () {
79
+ if (controller.execute !== undefined) {
80
+ const response = yield controller.execute({
81
+ connection,
82
+ application,
83
+ function: func,
84
+ arguments: args,
85
+ });
86
+ return response.body;
87
+ }
88
+ return utils_1.HttpLlm.execute({
89
+ application,
90
+ function: func,
91
+ connection,
92
+ input: args,
93
+ });
94
+ }),
95
+ });
96
+ }
97
+ };
98
+ const createTool = (props) => {
99
+ const { func, execute } = props;
100
+ return (0, ai_1.tool)({
101
+ description: func.description,
102
+ // Convert ILlmSchema.IParameters to Vercel jsonSchema
103
+ parameters: (0, ai_1.jsonSchema)(convertParameters(func.parameters)),
104
+ execute: (args) => __awaiter(this, void 0, void 0, function* () {
105
+ // Validate using typia's built-in validator
106
+ const validation = func.validate(args);
107
+ if (!validation.success) {
108
+ // Return validation error in LLM-friendly format
109
+ return {
110
+ error: true,
111
+ message: (0, utils_1.stringifyValidationFailure)(validation),
112
+ };
113
+ }
114
+ try {
115
+ const result = yield execute(validation.data);
116
+ return result === undefined ? { success: true } : result;
117
+ }
118
+ catch (error) {
119
+ return {
120
+ error: true,
121
+ message: error instanceof Error
122
+ ? `${error.name}: ${error.message}`
123
+ : String(error),
124
+ };
125
+ }
126
+ }),
127
+ });
128
+ };
129
+ const convertParameters = (params) => {
130
+ const schema = {
131
+ type: "object",
132
+ properties: params.properties,
133
+ required: params.required,
134
+ additionalProperties: params.additionalProperties,
135
+ };
136
+ // Add $defs if present
137
+ if (Object.keys(params.$defs).length > 0) {
138
+ schema.$defs = params.$defs;
139
+ }
140
+ return schema;
141
+ };
142
+ })(VercelToolsRegistrar || (exports.VercelToolsRegistrar = VercelToolsRegistrar = {}));
143
+ //# sourceMappingURL=VercelToolsRegistrar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VercelToolsRegistrar.js","sourceRoot":"","sources":["../../src/internal/VercelToolsRegistrar.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2BAAsC;AAWtC,wCAAmE;AAEnE,IAAiB,oBAAoB,CAqKpC;AArKD,WAAiB,oBAAoB;IACnC;;;;;OAKG;IACU,4BAAO,GAAG,CAAC,KAGvB,EAAwB,EAAE;;QACzB,MAAM,KAAK,GAAyB,EAAE,CAAC;QACvC,MAAM,MAAM,GAAY,MAAA,KAAK,CAAC,MAAM,mCAAI,IAAI,CAAC;QAE7C,KAAK,MAAM,UAAU,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC;YAC3C,IAAI,UAAU,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;gBACpC,uBAAuB,CAAC;oBACtB,KAAK;oBACL,UAAU;oBACV,MAAM;iBACP,CAAC,CAAC;YACL,CAAC;iBAAM,CAAC;gBACN,sBAAsB,CAAC;oBACrB,KAAK;oBACL,UAAU;oBACV,MAAM;iBACP,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IAEF,MAAM,uBAAuB,GAAG,CAAC,KAIhC,EAAQ,EAAE;QACT,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;QAC5C,MAAM,OAAO,GAA4B,UAAU,CAAC,OAAO,CAAC;QAE5D,KAAK,MAAM,IAAI,IAAI,UAAU,CAAC,WAAW,CAAC,SAAS,EAAE,CAAC;YACpD,MAAM,QAAQ,GAAW,MAAM;gBAC7B,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAA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AAC,IAAI,CAAC,CAAC;oBACvD,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;gBAC3D,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO;wBACL,KAAK,EAAE,IAAI;wBACX,OAAO,EACL,KAAK,YAAY,KAAK;4BACpB,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE;4BACnC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;qBACpB,CAAC;gBACJ,CAAC;YACH,CAAC,CAAA;SACF,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,MAAM,iBAAiB,GAAG,CAAC,MAA8B,EAAe,EAAE;QACxE,MAAM,MAAM,GAAgB;YAC1B,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,MAAM,CAAC,UAAuC;YAC1D,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,oBAAoB,EAAE,MAAM,CAAC,oBAAoB;SAClD,CAAC;QAEF,uBAAuB;QACvB,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzC,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,KAA6B,CAAC;QACtD,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,EArKgB,oBAAoB,oCAApB,oBAAoB,QAqKpC"}
@@ -0,0 +1,133 @@
1
+ import { tool, jsonSchema } from 'ai';
2
+ import { stringifyValidationFailure, HttpLlm } from '@typia/utils';
3
+
4
+ var VercelToolsRegistrar;
5
+ (function (VercelToolsRegistrar) {
6
+ /**
7
+ * Convert typia controllers to Vercel AI SDK tools.
8
+ *
9
+ * @param props Conversion properties
10
+ * @returns Record of Vercel AI SDK Tools
11
+ */
12
+ VercelToolsRegistrar.convert = (props) => {
13
+ const tools = {};
14
+ const prefix = props.prefix ?? true;
15
+ for (const controller of props.controllers) {
16
+ if (controller.protocol === "class") {
17
+ registerClassController({
18
+ tools,
19
+ controller,
20
+ prefix,
21
+ });
22
+ }
23
+ else {
24
+ registerHttpController({
25
+ tools,
26
+ controller,
27
+ prefix,
28
+ });
29
+ }
30
+ }
31
+ return tools;
32
+ };
33
+ const registerClassController = (props) => {
34
+ const { tools, controller, prefix } = props;
35
+ const execute = controller.execute;
36
+ for (const func of controller.application.functions) {
37
+ const toolName = prefix
38
+ ? `${controller.name}_${func.name}`
39
+ : func.name;
40
+ if (tools[toolName] !== undefined) {
41
+ throw new Error(`Duplicate tool name "${toolName}" from controller "${controller.name}"`);
42
+ }
43
+ const method = execute[func.name];
44
+ if (typeof method !== "function") {
45
+ throw new Error(`Method "${func.name}" not found on controller "${controller.name}"`);
46
+ }
47
+ tools[toolName] = createTool({
48
+ func,
49
+ execute: async (args) => method.call(execute, args),
50
+ });
51
+ }
52
+ };
53
+ const registerHttpController = (props) => {
54
+ const { tools, controller, prefix } = props;
55
+ const application = controller.application;
56
+ const connection = controller.connection;
57
+ for (const func of application.functions) {
58
+ const toolName = prefix
59
+ ? `${controller.name}_${func.name}`
60
+ : func.name;
61
+ if (tools[toolName] !== undefined) {
62
+ throw new Error(`Duplicate tool name "${toolName}" from controller "${controller.name}"`);
63
+ }
64
+ tools[toolName] = createTool({
65
+ func,
66
+ execute: async (args) => {
67
+ if (controller.execute !== undefined) {
68
+ const response = await controller.execute({
69
+ connection,
70
+ application,
71
+ function: func,
72
+ arguments: args,
73
+ });
74
+ return response.body;
75
+ }
76
+ return HttpLlm.execute({
77
+ application,
78
+ function: func,
79
+ connection,
80
+ input: args,
81
+ });
82
+ },
83
+ });
84
+ }
85
+ };
86
+ const createTool = (props) => {
87
+ const { func, execute } = props;
88
+ return tool({
89
+ description: func.description,
90
+ // Convert ILlmSchema.IParameters to Vercel jsonSchema
91
+ parameters: jsonSchema(convertParameters(func.parameters)),
92
+ execute: async (args) => {
93
+ // Validate using typia's built-in validator
94
+ const validation = func.validate(args);
95
+ if (!validation.success) {
96
+ // Return validation error in LLM-friendly format
97
+ return {
98
+ error: true,
99
+ message: stringifyValidationFailure(validation),
100
+ };
101
+ }
102
+ try {
103
+ const result = await execute(validation.data);
104
+ return result === undefined ? { success: true } : result;
105
+ }
106
+ catch (error) {
107
+ return {
108
+ error: true,
109
+ message: error instanceof Error
110
+ ? `${error.name}: ${error.message}`
111
+ : String(error),
112
+ };
113
+ }
114
+ },
115
+ });
116
+ };
117
+ const convertParameters = (params) => {
118
+ const schema = {
119
+ type: "object",
120
+ properties: params.properties,
121
+ required: params.required,
122
+ additionalProperties: params.additionalProperties,
123
+ };
124
+ // Add $defs if present
125
+ if (Object.keys(params.$defs).length > 0) {
126
+ schema.$defs = params.$defs;
127
+ }
128
+ return schema;
129
+ };
130
+ })(VercelToolsRegistrar || (VercelToolsRegistrar = {}));
131
+
132
+ export { VercelToolsRegistrar };
133
+ //# sourceMappingURL=VercelToolsRegistrar.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VercelToolsRegistrar.mjs","sources":["../../src/internal/VercelToolsRegistrar.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAaM,IAAW;AAAjB,CAAA,UAAiB,oBAAoB,EAAA;AACnC;;;;;AAKG;AACU,IAAA,oBAAA,CAAA,OAAO,GAAG,CAAC,KAGvB,KAA0B;QACzB,MAAM,KAAK,GAAyB,EAAE;AACtC,QAAA,MAAM,MAAM,GAAY,KAAK,CAAC,MAAM,IAAI,IAAI;AAE5C,QAAA,KAAK,MAAM,UAAU,IAAI,KAAK,CAAC,WAAW,EAAE;AAC1C,YAAA,IAAI,UAAU,CAAC,QAAQ,KAAK,OAAO,EAAE;AACnC,gBAAA,uBAAuB,CAAC;oBACtB,KAAK;oBACL,UAAU;oBACV,MAAM;AACP,iBAAA,CAAC;YACJ;iBAAO;AACL,gBAAA,sBAAsB,CAAC;oBACrB,KAAK;oBACL,UAAU;oB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A,CAAA,CAAA;;;;"}
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@typia/vercel",
3
+ "version": "12.0.0-dev.20260225",
4
+ "description": "Vercel AI SDK integration for typia",
5
+ "main": "lib/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./lib/index.d.ts",
9
+ "import": "./lib/index.mjs",
10
+ "default": "./lib/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/samchon/typia"
17
+ },
18
+ "author": "Jeongho Nam",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/samchon/typia/issues"
22
+ },
23
+ "homepage": "https://typia.io",
24
+ "dependencies": {
25
+ "@typia/interface": "^12.0.0-dev.20260225",
26
+ "@typia/utils": "^12.0.0-dev.20260225"
27
+ },
28
+ "peerDependencies": {
29
+ "ai": ">=4.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "@rollup/plugin-commonjs": "^29.0.0",
33
+ "@rollup/plugin-node-resolve": "^16.0.3",
34
+ "@rollup/plugin-typescript": "^12.3.0",
35
+ "@types/json-schema": "^7.0.15",
36
+ "ai": "^4.3.16",
37
+ "rimraf": "^6.1.2",
38
+ "rollup": "^4.56.0",
39
+ "rollup-plugin-auto-external": "^2.0.0",
40
+ "rollup-plugin-node-externals": "^8.1.2",
41
+ "tinyglobby": "^0.2.12",
42
+ "typescript": "~5.9.3"
43
+ },
44
+ "sideEffects": false,
45
+ "files": [
46
+ "LICENSE",
47
+ "README.md",
48
+ "package.json",
49
+ "lib",
50
+ "src"
51
+ ],
52
+ "keywords": [
53
+ "vercel",
54
+ "ai-sdk",
55
+ "typia",
56
+ "llm",
57
+ "llm-function-calling",
58
+ "ai",
59
+ "tool-calling",
60
+ "openai",
61
+ "anthropic",
62
+ "claude",
63
+ "chatgpt",
64
+ "gemini",
65
+ "validation",
66
+ "json-schema",
67
+ "typescript"
68
+ ],
69
+ "publishConfig": {
70
+ "access": "public"
71
+ },
72
+ "scripts": {
73
+ "build": "rimraf lib && tsc && rollup -c",
74
+ "dev": "rimraf lib && tsc --watch"
75
+ },
76
+ "module": "lib/index.mjs",
77
+ "types": "lib/index.d.ts"
78
+ }
package/src/index.ts ADDED
@@ -0,0 +1,103 @@
1
+ import type { Tool } from "ai";
2
+ import { IHttpLlmController, ILlmController } from "@typia/interface";
3
+
4
+ import { VercelToolsRegistrar } from "./internal/VercelToolsRegistrar";
5
+
6
+ /**
7
+ * Convert typia controllers to Vercel AI SDK tools.
8
+ *
9
+ * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
10
+ * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
11
+ * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
12
+ *
13
+ * Every tool call is validated by typia. If the LLM provides invalid arguments,
14
+ * returns a validation error formatted by {@link stringifyValidationFailure}
15
+ * so the LLM can auto-correct in the next turn.
16
+ *
17
+ * ## Example with OpenAI
18
+ *
19
+ * ```typescript
20
+ * import { generateText } from "ai";
21
+ * import { openai } from "@ai-sdk/openai";
22
+ * import typia from "typia";
23
+ * import { toVercelTools } from "@typia/vercel";
24
+ *
25
+ * class Calculator {
26
+ * /&#42;&#42; Add two numbers together. &#42;/
27
+ * add(props: { a: number; b: number }): number {
28
+ * return props.a + props.b;
29
+ * }
30
+ * }
31
+ *
32
+ * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
33
+ * const tools = toVercelTools({ controllers: [controller] });
34
+ *
35
+ * const result = await generateText({
36
+ * model: openai("gpt-4o"),
37
+ * tools,
38
+ * prompt: "What is 15 + 27?",
39
+ * });
40
+ * ```
41
+ *
42
+ * ## Example with Anthropic
43
+ *
44
+ * ```typescript
45
+ * import { generateText } from "ai";
46
+ * import { anthropic } from "@ai-sdk/anthropic";
47
+ * import { toVercelTools } from "@typia/vercel";
48
+ *
49
+ * const result = await generateText({
50
+ * model: anthropic("claude-sonnet-4-20250514"),
51
+ * tools: toVercelTools({ controllers: [controller] }),
52
+ * prompt: "Calculate 100 * 50",
53
+ * });
54
+ * ```
55
+ *
56
+ * ## Example with HTTP Controller (OpenAPI)
57
+ *
58
+ * ```typescript
59
+ * import { generateText } from "ai";
60
+ * import { openai } from "@ai-sdk/openai";
61
+ * import { HttpLlm } from "@typia/utils";
62
+ * import { toVercelTools } from "@typia/vercel";
63
+ *
64
+ * const controller = HttpLlm.controller({
65
+ * name: "shopping",
66
+ * document: await fetch("https://api.example.com/swagger.json").then(r => r.json()),
67
+ * connection: { host: "https://api.example.com" },
68
+ * });
69
+ *
70
+ * const result = await generateText({
71
+ * model: openai("gpt-4o"),
72
+ * tools: toVercelTools({ controllers: [controller] }),
73
+ * prompt: "Search for laptops under $1000",
74
+ * });
75
+ * ```
76
+ *
77
+ * @param props Conversion properties
78
+ * @returns Record of Vercel AI SDK Tools keyed by tool name
79
+ * @author Jeongho Nam - https://github.com/samchon
80
+ */
81
+ export function toVercelTools(props: {
82
+ /**
83
+ * List of controllers to convert to Vercel tools.
84
+ *
85
+ * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
86
+ * methods of the class to tools
87
+ * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
88
+ * operations from OpenAPI document to tools
89
+ */
90
+ controllers: Array<ILlmController | IHttpLlmController>;
91
+
92
+ /**
93
+ * Whether to prefix tool names with controller name.
94
+ *
95
+ * If `true`, tool names are formatted as `{controller}_{function}`.
96
+ * If `false`, only the function name is used (may cause conflicts).
97
+ *
98
+ * @default true
99
+ */
100
+ prefix?: boolean | undefined;
101
+ }): Record<string, Tool> {
102
+ return VercelToolsRegistrar.convert(props);
103
+ }
@@ -0,0 +1,179 @@
1
+ import { jsonSchema, tool } from "ai";
2
+ import type { Tool } from "ai";
3
+ import type { JSONSchema7 } from "json-schema";
4
+ import {
5
+ IHttpLlmController,
6
+ IHttpLlmFunction,
7
+ ILlmController,
8
+ ILlmFunction,
9
+ ILlmSchema,
10
+ IValidation,
11
+ } from "@typia/interface";
12
+ import { HttpLlm, stringifyValidationFailure } from "@typia/utils";
13
+
14
+ export namespace VercelToolsRegistrar {
15
+ /**
16
+ * Convert typia controllers to Vercel AI SDK tools.
17
+ *
18
+ * @param props Conversion properties
19
+ * @returns Record of Vercel AI SDK Tools
20
+ */
21
+ export const convert = (props: {
22
+ controllers: Array<ILlmController | IHttpLlmController>;
23
+ prefix?: boolean | undefined;
24
+ }): Record<string, Tool> => {
25
+ const tools: Record<string, Tool> = {};
26
+ const prefix: boolean = props.prefix ?? true;
27
+
28
+ for (const controller of props.controllers) {
29
+ if (controller.protocol === "class") {
30
+ registerClassController({
31
+ tools,
32
+ controller,
33
+ prefix,
34
+ });
35
+ } else {
36
+ registerHttpController({
37
+ tools,
38
+ controller,
39
+ prefix,
40
+ });
41
+ }
42
+ }
43
+
44
+ return tools;
45
+ };
46
+
47
+ const registerClassController = (props: {
48
+ tools: Record<string, Tool>;
49
+ controller: ILlmController;
50
+ prefix: boolean;
51
+ }): void => {
52
+ const { tools, controller, prefix } = props;
53
+ const execute: Record<string, unknown> = controller.execute;
54
+
55
+ for (const func of controller.application.functions) {
56
+ const toolName: string = prefix
57
+ ? `${controller.name}_${func.name}`
58
+ : func.name;
59
+
60
+ if (tools[toolName] !== undefined) {
61
+ throw new Error(
62
+ `Duplicate tool name "${toolName}" from controller "${controller.name}"`,
63
+ );
64
+ }
65
+
66
+ const method: unknown = execute[func.name];
67
+ if (typeof method !== "function") {
68
+ throw new Error(
69
+ `Method "${func.name}" not found on controller "${controller.name}"`,
70
+ );
71
+ }
72
+
73
+ tools[toolName] = createTool({
74
+ func,
75
+ execute: async (args: unknown) => method.call(execute, args),
76
+ });
77
+ }
78
+ };
79
+
80
+ const registerHttpController = (props: {
81
+ tools: Record<string, Tool>;
82
+ controller: IHttpLlmController;
83
+ prefix: boolean;
84
+ }): void => {
85
+ const { tools, controller, prefix } = props;
86
+ const application = controller.application;
87
+ const connection = controller.connection;
88
+
89
+ for (const func of application.functions) {
90
+ const toolName: string = prefix
91
+ ? `${controller.name}_${func.name}`
92
+ : func.name;
93
+
94
+ if (tools[toolName] !== undefined) {
95
+ throw new Error(
96
+ `Duplicate tool name "${toolName}" from controller "${controller.name}"`,
97
+ );
98
+ }
99
+
100
+ tools[toolName] = createTool({
101
+ func,
102
+ execute: async (args: unknown) => {
103
+ if (controller.execute !== undefined) {
104
+ const response = await controller.execute({
105
+ connection,
106
+ application,
107
+ function: func,
108
+ arguments: args as object,
109
+ });
110
+ return response.body;
111
+ }
112
+ return HttpLlm.execute({
113
+ application,
114
+ function: func,
115
+ connection,
116
+ input: args as object,
117
+ });
118
+ },
119
+ });
120
+ }
121
+ };
122
+
123
+ const createTool = (props: {
124
+ func: ILlmFunction | IHttpLlmFunction;
125
+ execute: (args: unknown) => Promise<unknown>;
126
+ }): Tool => {
127
+ const { func, execute } = props;
128
+
129
+ return tool({
130
+ description: func.description,
131
+
132
+ // Convert ILlmSchema.IParameters to Vercel jsonSchema
133
+ parameters: jsonSchema<object>(
134
+ convertParameters(func.parameters) as JSONSchema7,
135
+ ),
136
+
137
+ execute: async (args: object) => {
138
+ // Validate using typia's built-in validator
139
+ const validation: IValidation<unknown> = func.validate(args);
140
+ if (!validation.success) {
141
+ // Return validation error in LLM-friendly format
142
+ return {
143
+ error: true,
144
+ message: stringifyValidationFailure(validation),
145
+ };
146
+ }
147
+
148
+ try {
149
+ const result: unknown = await execute(validation.data);
150
+ return result === undefined ? { success: true } : result;
151
+ } catch (error) {
152
+ return {
153
+ error: true,
154
+ message:
155
+ error instanceof Error
156
+ ? `${error.name}: ${error.message}`
157
+ : String(error),
158
+ };
159
+ }
160
+ },
161
+ });
162
+ };
163
+
164
+ const convertParameters = (params: ILlmSchema.IParameters): JSONSchema7 => {
165
+ const schema: JSONSchema7 = {
166
+ type: "object",
167
+ properties: params.properties as JSONSchema7["properties"],
168
+ required: params.required,
169
+ additionalProperties: params.additionalProperties,
170
+ };
171
+
172
+ // Add $defs if present
173
+ if (Object.keys(params.$defs).length > 0) {
174
+ schema.$defs = params.$defs as JSONSchema7["$defs"];
175
+ }
176
+
177
+ return schema;
178
+ };
179
+ }