@typia/vercel 12.1.0-dev.20260325 → 13.0.0-dev.20260426

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/src/index.ts CHANGED
@@ -1,155 +1,155 @@
1
- import {
2
- IHttpLlmController,
3
- ILlmController,
4
- ILlmSchema,
5
- } from "@typia/interface";
6
- import type { Schema, Tool } from "ai";
7
-
8
- import { VercelParameterConverter } from "./internal/VercelParameterConverter";
9
- import { VercelToolsRegistrar } from "./internal/VercelToolsRegistrar";
10
-
11
- /**
12
- * Convert typia controllers to Vercel AI SDK tools.
13
- *
14
- * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
15
- * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
16
- * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
17
- *
18
- * Every tool call is validated by typia. If the LLM provides invalid arguments,
19
- * returns a validation error formatted by {@link LlmJson.stringify} so the LLM
20
- * can auto-correct in the next turn.
21
- *
22
- * ## Example with OpenAI
23
- *
24
- * ```typescript
25
- * import { generateText } from "ai";
26
- * import { openai } from "@ai-sdk/openai";
27
- * import typia from "typia";
28
- * import { toVercelTools } from "@typia/vercel";
29
- *
30
- * class Calculator {
31
- * /&#42;&#42; Add two numbers together. &#42;/
32
- * add(props: { a: number; b: number }): { value: number } {
33
- * return { value: props.a + props.b };
34
- * }
35
- * }
36
- *
37
- * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
38
- * const tools = toVercelTools({ controllers: [controller] });
39
- *
40
- * const result = await generateText({
41
- * model: openai("gpt-4o"),
42
- * tools,
43
- * prompt: "What is 15 + 27?",
44
- * });
45
- * ```
46
- *
47
- * ## Example with Anthropic
48
- *
49
- * ```typescript
50
- * import { anthropic } from "@ai-sdk/anthropic";
51
- * import { toVercelTools } from "@typia/vercel";
52
- * import { generateText } from "ai";
53
- *
54
- * const result = await generateText({
55
- * model: anthropic("claude-sonnet-4-20250514"),
56
- * tools: toVercelTools({ controllers: [controller] }),
57
- * prompt: "Calculate 100 * 50",
58
- * });
59
- * ```
60
- *
61
- * ## Example with HTTP Controller (OpenAPI)
62
- *
63
- * ```typescript
64
- * import { openai } from "@ai-sdk/openai";
65
- * import { HttpLlm } from "@typia/utils";
66
- * import { toVercelTools } from "@typia/vercel";
67
- * import { generateText } from "ai";
68
- *
69
- * const controller = HttpLlm.controller({
70
- * name: "shopping",
71
- * document: await fetch("https://api.example.com/swagger.json").then(
72
- * (r) => r.json(),
73
- * ),
74
- * connection: { host: "https://api.example.com" },
75
- * });
76
- *
77
- * const result = await generateText({
78
- * model: openai("gpt-4o"),
79
- * tools: toVercelTools({ controllers: [controller] }),
80
- * prompt: "Search for laptops under $1000",
81
- * });
82
- * ```
83
- *
84
- * @author Jeongho Nam - https://github.com/samchon
85
- * @param props Conversion properties
86
- * @returns Record of Vercel AI SDK Tools keyed by tool name
87
- */
88
- export function toVercelTools(props: {
89
- /**
90
- * List of controllers to convert to Vercel tools.
91
- *
92
- * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
93
- * methods of the class to tools
94
- * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
95
- * operations from OpenAPI document to tools
96
- */
97
- controllers: Array<ILlmController | IHttpLlmController>;
98
-
99
- /**
100
- * Whether to prefix tool names with controller name.
101
- *
102
- * If `true`, tool names are formatted as `{controller}_{function}`. If
103
- * `false`, only the function name is used (may cause conflicts).
104
- *
105
- * @default false
106
- */
107
- prefix?: boolean | undefined;
108
- }): Record<string, Tool> {
109
- return VercelToolsRegistrar.convert(props);
110
- }
111
-
112
- /**
113
- * Convert LLM parameters schema to Vercel AI SDK schema format.
114
- *
115
- * Transforms {@link ILlmSchema.IParameters} into Vercel AI SDK's `Schema` type
116
- * for use with `generateObject()`. Use with `typia.llm.structuredOutput<T>()`
117
- * or `typia.llm.parameters<T>()`.
118
- *
119
- * ## Example
120
- *
121
- * ```typescript
122
- * import { openai } from "@ai-sdk/openai";
123
- * import { toVercelSchema } from "@typia/vercel";
124
- * import { generateObject } from "ai";
125
- * import typia from "typia";
126
- *
127
- * interface IMember {
128
- * name: string;
129
- * age: number;
130
- * }
131
- *
132
- * const output = typia.llm.structuredOutput<IMember>();
133
- * const schema = toVercelSchema(output.parameters);
134
- *
135
- * const { object } = await generateObject({
136
- * model: openai("gpt-4o"),
137
- * schema,
138
- * prompt: "Generate a member named John who is 30 years old",
139
- * });
140
- *
141
- * const coerced = output.coerce(object);
142
- * const result = output.validate(coerced);
143
- * ```
144
- *
145
- * @author Jeongho Nam - https://github.com/samchon
146
- * @param parameters LLM parameters schema from
147
- * `typia.llm.structuredOutput<T>().parameters` or
148
- * `typia.llm.parameters<T>()`
149
- * @returns Vercel AI SDK Schema for `generateObject()`
150
- */
151
- export function toVercelSchema(
152
- parameters: ILlmSchema.IParameters,
153
- ): Schema<object> {
154
- return VercelParameterConverter.convert(parameters);
155
- }
1
+ import {
2
+ IHttpLlmController,
3
+ ILlmController,
4
+ ILlmSchema,
5
+ } from "@typia/interface";
6
+ import type { Schema, Tool } from "ai";
7
+
8
+ import { VercelParameterConverter } from "./internal/VercelParameterConverter";
9
+ import { VercelToolsRegistrar } from "./internal/VercelToolsRegistrar";
10
+
11
+ /**
12
+ * Convert typia controllers to Vercel AI SDK tools.
13
+ *
14
+ * Transforms TypeScript class methods via `typia.llm.controller<Class>()` or
15
+ * OpenAPI operations via `HttpLlm.controller()` into Vercel AI SDK tools that
16
+ * can be used with any LLM provider (OpenAI, Anthropic, Google, etc.).
17
+ *
18
+ * Every tool call is validated by typia. If the LLM provides invalid arguments,
19
+ * returns a validation error formatted by {@link LlmJson.stringify} so the LLM
20
+ * can auto-correct in the next turn.
21
+ *
22
+ * ## Example with OpenAI
23
+ *
24
+ * ```typescript
25
+ * import { generateText } from "ai";
26
+ * import { openai } from "@ai-sdk/openai";
27
+ * import typia from "typia";
28
+ * import { toVercelTools } from "@typia/vercel";
29
+ *
30
+ * class Calculator {
31
+ * /&#42;&#42; Add two numbers together. &#42;/
32
+ * add(props: { a: number; b: number }): { value: number } {
33
+ * return { value: props.a + props.b };
34
+ * }
35
+ * }
36
+ *
37
+ * const controller = typia.llm.controller<Calculator>("calc", new Calculator());
38
+ * const tools = toVercelTools({ controllers: [controller] });
39
+ *
40
+ * const result = await generateText({
41
+ * model: openai("gpt-4o"),
42
+ * tools,
43
+ * prompt: "What is 15 + 27?",
44
+ * });
45
+ * ```
46
+ *
47
+ * ## Example with Anthropic
48
+ *
49
+ * ```typescript
50
+ * import { anthropic } from "@ai-sdk/anthropic";
51
+ * import { toVercelTools } from "@typia/vercel";
52
+ * import { generateText } from "ai";
53
+ *
54
+ * const result = await generateText({
55
+ * model: anthropic("claude-sonnet-4-20250514"),
56
+ * tools: toVercelTools({ controllers: [controller] }),
57
+ * prompt: "Calculate 100 * 50",
58
+ * });
59
+ * ```
60
+ *
61
+ * ## Example with HTTP Controller (OpenAPI)
62
+ *
63
+ * ```typescript
64
+ * import { openai } from "@ai-sdk/openai";
65
+ * import { HttpLlm } from "@typia/utils";
66
+ * import { toVercelTools } from "@typia/vercel";
67
+ * import { generateText } from "ai";
68
+ *
69
+ * const controller = HttpLlm.controller({
70
+ * name: "shopping",
71
+ * document: await fetch("https://api.example.com/swagger.json").then(
72
+ * (r) => r.json(),
73
+ * ),
74
+ * connection: { host: "https://api.example.com" },
75
+ * });
76
+ *
77
+ * const result = await generateText({
78
+ * model: openai("gpt-4o"),
79
+ * tools: toVercelTools({ controllers: [controller] }),
80
+ * prompt: "Search for laptops under $1000",
81
+ * });
82
+ * ```
83
+ *
84
+ * @author Jeongho Nam - https://github.com/samchon
85
+ * @param props Conversion properties
86
+ * @returns Record of Vercel AI SDK Tools keyed by tool name
87
+ */
88
+ export function toVercelTools(props: {
89
+ /**
90
+ * List of controllers to convert to Vercel tools.
91
+ *
92
+ * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
93
+ * methods of the class to tools
94
+ * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
95
+ * operations from OpenAPI document to tools
96
+ */
97
+ controllers: Array<ILlmController | IHttpLlmController>;
98
+
99
+ /**
100
+ * Whether to prefix tool names with controller name.
101
+ *
102
+ * If `true`, tool names are formatted as `{controller}_{function}`. If
103
+ * `false`, only the function name is used (may cause conflicts).
104
+ *
105
+ * @default false
106
+ */
107
+ prefix?: boolean | undefined;
108
+ }): Record<string, Tool> {
109
+ return VercelToolsRegistrar.convert(props);
110
+ }
111
+
112
+ /**
113
+ * Convert LLM parameters schema to Vercel AI SDK schema format.
114
+ *
115
+ * Transforms {@link ILlmSchema.IParameters} into Vercel AI SDK's `Schema` type
116
+ * for use with `generateObject()`. Use with `typia.llm.structuredOutput<T>()`
117
+ * or `typia.llm.parameters<T>()`.
118
+ *
119
+ * ## Example
120
+ *
121
+ * ```typescript
122
+ * import { openai } from "@ai-sdk/openai";
123
+ * import { toVercelSchema } from "@typia/vercel";
124
+ * import { generateObject } from "ai";
125
+ * import typia from "typia";
126
+ *
127
+ * interface IMember {
128
+ * name: string;
129
+ * age: number;
130
+ * }
131
+ *
132
+ * const output = typia.llm.structuredOutput<IMember>();
133
+ * const schema = toVercelSchema(output.parameters);
134
+ *
135
+ * const { object } = await generateObject({
136
+ * model: openai("gpt-4o"),
137
+ * schema,
138
+ * prompt: "Generate a member named John who is 30 years old",
139
+ * });
140
+ *
141
+ * const coerced = output.coerce(object);
142
+ * const result = output.validate(coerced);
143
+ * ```
144
+ *
145
+ * @author Jeongho Nam - https://github.com/samchon
146
+ * @param parameters LLM parameters schema from
147
+ * `typia.llm.structuredOutput<T>().parameters` or
148
+ * `typia.llm.parameters<T>()`
149
+ * @returns Vercel AI SDK Schema for `generateObject()`
150
+ */
151
+ export function toVercelSchema(
152
+ parameters: ILlmSchema.IParameters,
153
+ ): Schema<object> {
154
+ return VercelParameterConverter.convert(parameters);
155
+ }
@@ -1,8 +1,8 @@
1
- import { ILlmSchema } from "@typia/interface";
2
- import { Schema, jsonSchema } from "ai";
3
- import { JSONSchema7 } from "json-schema";
4
-
5
- export namespace VercelParameterConverter {
6
- export const convert = (parameters: ILlmSchema.IParameters): Schema<object> =>
7
- jsonSchema<object>(parameters as JSONSchema7);
8
- }
1
+ import { ILlmSchema } from "@typia/interface";
2
+ import { Schema, jsonSchema } from "ai";
3
+ import { JSONSchema7 } from "json-schema";
4
+
5
+ export namespace VercelParameterConverter {
6
+ export const convert = (parameters: ILlmSchema.IParameters): Schema<object> =>
7
+ jsonSchema<object>(parameters as JSONSchema7);
8
+ }
@@ -1,166 +1,166 @@
1
- import {
2
- IHttpLlmController,
3
- IHttpLlmFunction,
4
- ILlmController,
5
- ILlmFunction,
6
- IValidation,
7
- } from "@typia/interface";
8
- import { HttpLlm, LlmJson } from "@typia/utils";
9
- import { Tool, tool } from "ai";
10
-
11
- import { VercelParameterConverter } from "./VercelParameterConverter";
12
-
13
- export namespace VercelToolsRegistrar {
14
- /**
15
- * Convert typia controllers to Vercel AI SDK tools.
16
- *
17
- * @param props Conversion properties
18
- * @returns Record of Vercel AI SDK Tools
19
- */
20
- export const convert = (props: {
21
- controllers: Array<ILlmController | IHttpLlmController>;
22
- prefix?: boolean | undefined;
23
- }): Record<string, Tool> => {
24
- const prefix: boolean = props.prefix ?? false;
25
- const tools: Record<string, Tool> = {};
26
-
27
- // check duplicate tool names
28
- if (prefix === false && props.controllers.length >= 2) {
29
- const names: Map<string, string> = new Map();
30
- const duplicates: string[] = [];
31
- for (const controller of props.controllers) {
32
- for (const func of controller.application.functions) {
33
- const existing: string | undefined = names.get(func.name);
34
- if (existing !== undefined)
35
- duplicates.push(
36
- `"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
37
- );
38
- else names.set(func.name, controller.name);
39
- }
40
- }
41
- if (duplicates.length > 0)
42
- throw new Error(
43
- `Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
44
- );
45
- }
46
-
47
- // convert controllers to tools
48
- for (const controller of props.controllers) {
49
- if (controller.protocol === "class") {
50
- registerClassController({ tools, controller, prefix });
51
- } else {
52
- registerHttpController({ tools, controller, prefix });
53
- }
54
- }
55
-
56
- return tools;
57
- };
58
-
59
- const registerClassController = (props: {
60
- tools: Record<string, Tool>;
61
- controller: ILlmController;
62
- prefix: boolean;
63
- }): void => {
64
- const { tools, controller, prefix } = props;
65
- const execute: Record<string, unknown> = controller.execute;
66
-
67
- for (const func of controller.application.functions) {
68
- const toolName: string = prefix
69
- ? `${controller.name}_${func.name}`
70
- : func.name;
71
-
72
- const method: unknown = execute[func.name];
73
- if (typeof method !== "function") {
74
- throw new Error(
75
- `Method "${func.name}" not found on controller "${controller.name}"`,
76
- );
77
- }
78
-
79
- tools[toolName] = createTool({
80
- name: toolName,
81
- func,
82
- execute: async (args: unknown) => method.call(execute, args),
83
- });
84
- }
85
- };
86
-
87
- const registerHttpController = (props: {
88
- tools: Record<string, Tool>;
89
- controller: IHttpLlmController;
90
- prefix: boolean;
91
- }): void => {
92
- const { tools, controller, prefix } = props;
93
- const application = controller.application;
94
- const connection = controller.connection;
95
-
96
- for (const func of application.functions) {
97
- const toolName: string = prefix
98
- ? `${controller.name}_${func.name}`
99
- : func.name;
100
-
101
- tools[toolName] = createTool({
102
- name: toolName,
103
- func,
104
- execute: async (args: unknown) => {
105
- if (controller.execute !== undefined) {
106
- const response = await controller.execute({
107
- connection,
108
- application,
109
- function: func,
110
- arguments: args as object,
111
- });
112
- return response.body;
113
- }
114
- return HttpLlm.execute({
115
- application,
116
- function: func,
117
- connection,
118
- input: args as object,
119
- });
120
- },
121
- });
122
- }
123
- };
124
-
125
- const createTool = (props: {
126
- name: string;
127
- func: ILlmFunction | IHttpLlmFunction;
128
- execute: (args: unknown) => Promise<unknown>;
129
- }): Tool => {
130
- const { name, func, execute } = props;
131
-
132
- return tool({
133
- description: func.description ?? "",
134
- inputSchema: VercelParameterConverter.convert(func.parameters),
135
- execute: async (args: unknown): Promise<ITryResult> => {
136
- const coerced: unknown = LlmJson.coerce(args, func.parameters);
137
- const validation: IValidation<unknown> = func.validate(coerced);
138
- if (!validation.success)
139
- return {
140
- success: false,
141
- error:
142
- `Type errors in "${name}" arguments:\n\n` +
143
- `\`\`\`json\n${LlmJson.stringify(validation)}\n\`\`\``,
144
- } satisfies ITryResult;
145
- try {
146
- const result: unknown = await execute(validation.data);
147
- return result === undefined
148
- ? ({ success: true } satisfies ITryResult)
149
- : ({ success: true, data: result } satisfies ITryResult);
150
- } catch (error) {
151
- return {
152
- success: false,
153
- error:
154
- error instanceof Error
155
- ? `${error.name}: ${error.message}`
156
- : String(error),
157
- } satisfies ITryResult;
158
- }
159
- },
160
- });
161
- };
162
- }
163
-
164
- type ITryResult =
165
- | { success: true; data?: unknown | undefined }
166
- | { success: false; error: string };
1
+ import {
2
+ IHttpLlmController,
3
+ IHttpLlmFunction,
4
+ ILlmController,
5
+ ILlmFunction,
6
+ IValidation,
7
+ } from "@typia/interface";
8
+ import { HttpLlm, LlmJson } from "@typia/utils";
9
+ import { Tool, tool } from "ai";
10
+
11
+ import { VercelParameterConverter } from "./VercelParameterConverter";
12
+
13
+ export namespace VercelToolsRegistrar {
14
+ /**
15
+ * Convert typia controllers to Vercel AI SDK tools.
16
+ *
17
+ * @param props Conversion properties
18
+ * @returns Record of Vercel AI SDK Tools
19
+ */
20
+ export const convert = (props: {
21
+ controllers: Array<ILlmController | IHttpLlmController>;
22
+ prefix?: boolean | undefined;
23
+ }): Record<string, Tool> => {
24
+ const prefix: boolean = props.prefix ?? false;
25
+ const tools: Record<string, Tool> = {};
26
+
27
+ // check duplicate tool names
28
+ if (prefix === false && props.controllers.length >= 2) {
29
+ const names: Map<string, string> = new Map();
30
+ const duplicates: string[] = [];
31
+ for (const controller of props.controllers) {
32
+ for (const func of controller.application.functions) {
33
+ const existing: string | undefined = names.get(func.name);
34
+ if (existing !== undefined)
35
+ duplicates.push(
36
+ `"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
37
+ );
38
+ else names.set(func.name, controller.name);
39
+ }
40
+ }
41
+ if (duplicates.length > 0)
42
+ throw new Error(
43
+ `Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
44
+ );
45
+ }
46
+
47
+ // convert controllers to tools
48
+ for (const controller of props.controllers) {
49
+ if (controller.protocol === "class") {
50
+ registerClassController({ tools, controller, prefix });
51
+ } else {
52
+ registerHttpController({ tools, controller, prefix });
53
+ }
54
+ }
55
+
56
+ return tools;
57
+ };
58
+
59
+ const registerClassController = (props: {
60
+ tools: Record<string, Tool>;
61
+ controller: ILlmController;
62
+ prefix: boolean;
63
+ }): void => {
64
+ const { tools, controller, prefix } = props;
65
+ const execute: Record<string, unknown> = controller.execute;
66
+
67
+ for (const func of controller.application.functions) {
68
+ const toolName: string = prefix
69
+ ? `${controller.name}_${func.name}`
70
+ : func.name;
71
+
72
+ const method: unknown = execute[func.name];
73
+ if (typeof method !== "function") {
74
+ throw new Error(
75
+ `Method "${func.name}" not found on controller "${controller.name}"`,
76
+ );
77
+ }
78
+
79
+ tools[toolName] = createTool({
80
+ name: toolName,
81
+ func,
82
+ execute: async (args: unknown) => method.call(execute, args),
83
+ });
84
+ }
85
+ };
86
+
87
+ const registerHttpController = (props: {
88
+ tools: Record<string, Tool>;
89
+ controller: IHttpLlmController;
90
+ prefix: boolean;
91
+ }): void => {
92
+ const { tools, controller, prefix } = props;
93
+ const application = controller.application;
94
+ const connection = controller.connection;
95
+
96
+ for (const func of application.functions) {
97
+ const toolName: string = prefix
98
+ ? `${controller.name}_${func.name}`
99
+ : func.name;
100
+
101
+ tools[toolName] = createTool({
102
+ name: toolName,
103
+ func,
104
+ execute: async (args: unknown) => {
105
+ if (controller.execute !== undefined) {
106
+ const response = await controller.execute({
107
+ connection,
108
+ application,
109
+ function: func,
110
+ arguments: args as object,
111
+ });
112
+ return response.body;
113
+ }
114
+ return HttpLlm.execute({
115
+ application,
116
+ function: func,
117
+ connection,
118
+ input: args as object,
119
+ });
120
+ },
121
+ });
122
+ }
123
+ };
124
+
125
+ const createTool = (props: {
126
+ name: string;
127
+ func: ILlmFunction | IHttpLlmFunction;
128
+ execute: (args: unknown) => Promise<unknown>;
129
+ }): Tool => {
130
+ const { name, func, execute } = props;
131
+
132
+ return tool({
133
+ description: func.description ?? "",
134
+ inputSchema: VercelParameterConverter.convert(func.parameters),
135
+ execute: async (args: unknown): Promise<ITryResult> => {
136
+ const coerced: unknown = LlmJson.coerce(args, func.parameters);
137
+ const validation: IValidation<unknown> = func.validate(coerced);
138
+ if (!validation.success)
139
+ return {
140
+ success: false,
141
+ error:
142
+ `Type errors in "${name}" arguments:\n\n` +
143
+ `\`\`\`json\n${LlmJson.stringify(validation)}\n\`\`\``,
144
+ } satisfies ITryResult;
145
+ try {
146
+ const result: unknown = await execute(validation.data);
147
+ return result === undefined
148
+ ? ({ success: true } satisfies ITryResult)
149
+ : ({ success: true, data: result } satisfies ITryResult);
150
+ } catch (error) {
151
+ return {
152
+ success: false,
153
+ error:
154
+ error instanceof Error
155
+ ? `${error.name}: ${error.message}`
156
+ : String(error),
157
+ } satisfies ITryResult;
158
+ }
159
+ },
160
+ });
161
+ };
162
+ }
163
+
164
+ type ITryResult =
165
+ | { success: true; data?: unknown | undefined }
166
+ | { success: false; error: string };