@typia/vercel 12.0.0-dev.20260316 → 12.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +111 -111
- package/package.json +3 -3
- package/src/index.ts +155 -155
- package/src/internal/VercelParameterConverter.ts +8 -8
- package/src/internal/VercelToolsRegistrar.ts +166 -166
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
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
CHANGED
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
# `@typia/vercel`
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
[](https://github.com/samchon/typia/blob/master/LICENSE)
|
|
6
|
-
[](https://www.npmjs.com/package/typia)
|
|
7
|
-
[](https://www.npmjs.com/package/typia)
|
|
8
|
-
[](https://github.com/samchon/typia/actions?query=workflow%3Atest)
|
|
9
|
-
[](https://typia.io/docs/)
|
|
10
|
-
[](https://gurubase.io/g/typia)
|
|
11
|
-
[](https://discord.gg/E94XhzrUCZ)
|
|
12
|
-
|
|
13
|
-
[Vercel AI SDK](https://github.com/vercel/ai) integration for [`typia`](https://github.com/samchon/typia).
|
|
14
|
-
|
|
15
|
-
Converts typia controllers to Vercel AI SDK tools compatible with OpenAI, Anthropic, Google, and other LLM providers.
|
|
16
|
-
|
|
17
|
-
## Setup
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
npm install @typia/vercel ai
|
|
21
|
-
npm install typia
|
|
22
|
-
npx typia setup
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Usage
|
|
26
|
-
|
|
27
|
-
### From TypeScript class
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
import { openai } from "@ai-sdk/openai";
|
|
31
|
-
import { toVercelTools } from "@typia/vercel";
|
|
32
|
-
import { generateText, GenerateTextResult, Tool } from "ai";
|
|
33
|
-
import typia from "typia";
|
|
34
|
-
|
|
35
|
-
const tools: Record<string, Tool> = toVercelTools({
|
|
36
|
-
controllers: [
|
|
37
|
-
typia.llm.controller<Calculator>("Calculator", new Calculator()),
|
|
38
|
-
],
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const result: GenerateTextResult = await generateText({
|
|
42
|
-
model: openai("gpt-4o"),
|
|
43
|
-
prompt: "What is 10 + 5?",
|
|
44
|
-
tools,
|
|
45
|
-
});
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### From OpenAPI document
|
|
49
|
-
|
|
50
|
-
```typescript
|
|
51
|
-
import { toVercelTools } from "@typia/vercel";
|
|
52
|
-
import { HttpLlm } from "@typia/utils";
|
|
53
|
-
import { Tool } from "ai";
|
|
54
|
-
|
|
55
|
-
const tools: Record<string, Tool> = toVercelTools({
|
|
56
|
-
controllers: [
|
|
57
|
-
HttpLlm.controller({
|
|
58
|
-
name: "petStore",
|
|
59
|
-
document: yourOpenApiDocument,
|
|
60
|
-
connection: { host: "https://api.example.com" },
|
|
61
|
-
}),
|
|
62
|
-
],
|
|
63
|
-
});
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Structured Output
|
|
67
|
-
|
|
68
|
-
Use `typia.llm.parameters<T>()` with Vercel's `jsonSchema()` to generate structured output with validation:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
import { openai } from "@ai-sdk/openai";
|
|
72
|
-
import { generateObject, jsonSchema } from "ai";
|
|
73
|
-
import { dedent, LlmJson } from "@typia/utils";
|
|
74
|
-
import typia, { tags } from "typia";
|
|
75
|
-
|
|
76
|
-
interface IMember {
|
|
77
|
-
email: string & tags.Format<"email">;
|
|
78
|
-
name: string;
|
|
79
|
-
age: number & tags.Minimum<0> & tags.Maximum<100>;
|
|
80
|
-
hobbies: string[];
|
|
81
|
-
joined_at: string & tags.Format<"date">;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const { object } = await generateObject({
|
|
85
|
-
model: openai("gpt-4o"),
|
|
86
|
-
schema: jsonSchema<IMember>(typia.llm.parameters<IMember>(), {
|
|
87
|
-
validate: (value) => {
|
|
88
|
-
const result = typia.validate<IMember>(value);
|
|
89
|
-
if (result.success) return { success: true, value: result.data };
|
|
90
|
-
return {
|
|
91
|
-
success: false,
|
|
92
|
-
error: new Error(LlmJson.stringify(result)),
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
}),
|
|
96
|
-
prompt: dedent`
|
|
97
|
-
I am a new member of the community.
|
|
98
|
-
|
|
99
|
-
My name is John Doe, and I am 25 years old.
|
|
100
|
-
I like playing basketball and reading books,
|
|
101
|
-
and joined to this community at 2022-01-01.
|
|
102
|
-
`,
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Features
|
|
107
|
-
|
|
108
|
-
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
109
|
-
- Automatic argument validation with LLM-friendly error feedback
|
|
110
|
-
- Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
|
|
111
|
-
- Works with any LLM provider supported by Vercel AI SDK
|
|
1
|
+
# `@typia/vercel`
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://github.com/samchon/typia/blob/master/LICENSE)
|
|
6
|
+
[](https://www.npmjs.com/package/typia)
|
|
7
|
+
[](https://www.npmjs.com/package/typia)
|
|
8
|
+
[](https://github.com/samchon/typia/actions?query=workflow%3Atest)
|
|
9
|
+
[](https://typia.io/docs/)
|
|
10
|
+
[](https://gurubase.io/g/typia)
|
|
11
|
+
[](https://discord.gg/E94XhzrUCZ)
|
|
12
|
+
|
|
13
|
+
[Vercel AI SDK](https://github.com/vercel/ai) integration for [`typia`](https://github.com/samchon/typia).
|
|
14
|
+
|
|
15
|
+
Converts typia controllers to Vercel AI SDK tools compatible with OpenAI, Anthropic, Google, and other LLM providers.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @typia/vercel ai
|
|
21
|
+
npm install typia
|
|
22
|
+
npx typia setup
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### From TypeScript class
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { openai } from "@ai-sdk/openai";
|
|
31
|
+
import { toVercelTools } from "@typia/vercel";
|
|
32
|
+
import { generateText, GenerateTextResult, Tool } from "ai";
|
|
33
|
+
import typia from "typia";
|
|
34
|
+
|
|
35
|
+
const tools: Record<string, Tool> = toVercelTools({
|
|
36
|
+
controllers: [
|
|
37
|
+
typia.llm.controller<Calculator>("Calculator", new Calculator()),
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result: GenerateTextResult = await generateText({
|
|
42
|
+
model: openai("gpt-4o"),
|
|
43
|
+
prompt: "What is 10 + 5?",
|
|
44
|
+
tools,
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### From OpenAPI document
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { toVercelTools } from "@typia/vercel";
|
|
52
|
+
import { HttpLlm } from "@typia/utils";
|
|
53
|
+
import { Tool } from "ai";
|
|
54
|
+
|
|
55
|
+
const tools: Record<string, Tool> = toVercelTools({
|
|
56
|
+
controllers: [
|
|
57
|
+
HttpLlm.controller({
|
|
58
|
+
name: "petStore",
|
|
59
|
+
document: yourOpenApiDocument,
|
|
60
|
+
connection: { host: "https://api.example.com" },
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Structured Output
|
|
67
|
+
|
|
68
|
+
Use `typia.llm.parameters<T>()` with Vercel's `jsonSchema()` to generate structured output with validation:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { openai } from "@ai-sdk/openai";
|
|
72
|
+
import { generateObject, jsonSchema } from "ai";
|
|
73
|
+
import { dedent, LlmJson } from "@typia/utils";
|
|
74
|
+
import typia, { tags } from "typia";
|
|
75
|
+
|
|
76
|
+
interface IMember {
|
|
77
|
+
email: string & tags.Format<"email">;
|
|
78
|
+
name: string;
|
|
79
|
+
age: number & tags.Minimum<0> & tags.Maximum<100>;
|
|
80
|
+
hobbies: string[];
|
|
81
|
+
joined_at: string & tags.Format<"date">;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const { object } = await generateObject({
|
|
85
|
+
model: openai("gpt-4o"),
|
|
86
|
+
schema: jsonSchema<IMember>(typia.llm.parameters<IMember>(), {
|
|
87
|
+
validate: (value) => {
|
|
88
|
+
const result = typia.validate<IMember>(value);
|
|
89
|
+
if (result.success) return { success: true, value: result.data };
|
|
90
|
+
return {
|
|
91
|
+
success: false,
|
|
92
|
+
error: new Error(LlmJson.stringify(result)),
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
prompt: dedent`
|
|
97
|
+
I am a new member of the community.
|
|
98
|
+
|
|
99
|
+
My name is John Doe, and I am 25 years old.
|
|
100
|
+
I like playing basketball and reading books,
|
|
101
|
+
and joined to this community at 2022-01-01.
|
|
102
|
+
`,
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Features
|
|
107
|
+
|
|
108
|
+
- No manual schema definition — generates everything from TypeScript types or OpenAPI
|
|
109
|
+
- Automatic argument validation with LLM-friendly error feedback
|
|
110
|
+
- Supports both class-based (`typia.llm.controller`) and HTTP-based (`HttpLlm.controller`) controllers
|
|
111
|
+
- Works with any LLM provider supported by Vercel AI SDK
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typia/vercel",
|
|
3
|
-
"version": "12.0.0
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "Vercel AI SDK integration for typia",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://typia.io",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@typia/
|
|
26
|
-
"@typia/
|
|
25
|
+
"@typia/interface": "^12.0.0",
|
|
26
|
+
"@typia/utils": "^12.0.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"ai": ">=6.0.0"
|
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
|
-
* /** Add two numbers together. */
|
|
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
|
+
* /** Add two numbers together. */
|
|
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 };
|