@typia/langchain 13.0.0-dev.20260619.1 → 13.0.0-dev.20260626.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typia/langchain",
3
- "version": "13.0.0-dev.20260619.1",
3
+ "version": "13.0.0-dev.20260626.1",
4
4
  "description": "LangChain.js 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/interface": "^13.0.0-dev.20260619.1",
26
- "@typia/utils": "^13.0.0-dev.20260619.1"
25
+ "@typia/utils": "^13.0.0-dev.20260626.1",
26
+ "@typia/interface": "^13.0.0-dev.20260626.1"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@langchain/core": ">=1.0.0"
@@ -38,7 +38,7 @@
38
38
  "rollup": "^4.56.0",
39
39
  "rollup-plugin-node-externals": "^8.1.2",
40
40
  "tinyglobby": "^0.2.12",
41
- "ttsc": "^0.16.0"
41
+ "ttsc": "^0.16.3"
42
42
  },
43
43
  "sideEffects": false,
44
44
  "files": [
package/src/index.ts CHANGED
@@ -1,64 +1,64 @@
1
- import type { DynamicStructuredTool } from "@langchain/core/tools";
2
- import { IHttpLlmController, ILlmController } from "@typia/interface";
3
-
4
- import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
5
-
6
- /**
7
- * Convert typia controllers to LangChain tools.
8
- *
9
- * Converts TypeScript class methods via `typia.llm.controller<Class>()` or
10
- * OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
11
- *
12
- * Every tool call is validated by typia. If LLM provides invalid arguments,
13
- * returns validation error formatted by {@link LlmJson.stringify} so that LLM
14
- * can correct them automatically.
15
- *
16
- * @example
17
- * ```typescript
18
- * import { initChatModel } from "langchain/chat_models/universal";
19
- * import typia from "typia";
20
- * import { toLangChainTools } from "@typia/langchain";
21
- *
22
- * class Calculator {
23
- * add(input: { a: number; b: number }): { value: number } {
24
- * return { value: input.a + input.b };
25
- * }
26
- * }
27
- *
28
- * const tools = toLangChainTools({
29
- * controllers: [
30
- * typia.llm.controller<Calculator>("calculator", new Calculator()),
31
- * ],
32
- * });
33
- *
34
- * const llm = await initChatModel();
35
- * const modelWithTools = llm.bindTools(tools);
36
- * const result = await modelWithTools.invoke("What is 10 + 5?");
37
- * ```;
38
- *
39
- * @param props Conversion properties
40
- * @returns Array of LangChain DynamicStructuredTool
41
- */
42
- export function toLangChainTools(props: {
43
- /**
44
- * List of controllers to convert to LangChain tools.
45
- *
46
- * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
47
- * methods of the class to tools
48
- * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
49
- * operations from OpenAPI document to tools
50
- */
51
- controllers: Array<ILlmController | IHttpLlmController>;
52
-
53
- /**
54
- * Whether to add controller name as prefix to tool names.
55
- *
56
- * If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
57
- * tool names are just `{methodName}`.
58
- *
59
- * @default false
60
- */
61
- prefix?: boolean | undefined;
62
- }): DynamicStructuredTool[] {
63
- return LangChainToolsRegistrar.convert(props);
64
- }
1
+ import type { DynamicStructuredTool } from "@langchain/core/tools";
2
+ import { IHttpLlmController, ILlmController } from "@typia/interface";
3
+
4
+ import { LangChainToolsRegistrar } from "./internal/LangChainToolsRegistrar";
5
+
6
+ /**
7
+ * Convert typia controllers to LangChain tools.
8
+ *
9
+ * Converts TypeScript class methods via `typia.llm.controller<Class>()` or
10
+ * OpenAPI operations via `HttpLlm.controller()` to LangChain tools.
11
+ *
12
+ * Every tool call is validated by typia. If LLM provides invalid arguments,
13
+ * returns validation error formatted by {@link LlmJson.stringify} so that LLM
14
+ * can correct them automatically.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { initChatModel } from "langchain/chat_models/universal";
19
+ * import typia from "typia";
20
+ * import { toLangChainTools } from "@typia/langchain";
21
+ *
22
+ * class Calculator {
23
+ * add(input: { a: number; b: number }): { value: number } {
24
+ * return { value: input.a + input.b };
25
+ * }
26
+ * }
27
+ *
28
+ * const tools = toLangChainTools({
29
+ * controllers: [
30
+ * typia.llm.controller<Calculator>("calculator", new Calculator()),
31
+ * ],
32
+ * });
33
+ *
34
+ * const llm = await initChatModel();
35
+ * const modelWithTools = llm.bindTools(tools);
36
+ * const result = await modelWithTools.invoke("What is 10 + 5?");
37
+ * ```;
38
+ *
39
+ * @param props Conversion properties
40
+ * @returns Array of LangChain DynamicStructuredTool
41
+ */
42
+ export function toLangChainTools(props: {
43
+ /**
44
+ * List of controllers to convert to LangChain tools.
45
+ *
46
+ * - {@link ILlmController}: from `typia.llm.controller<Class>()`, converts all
47
+ * methods of the class to tools
48
+ * - {@link IHttpLlmController}: from `HttpLlm.controller()`, converts all
49
+ * operations from OpenAPI document to tools
50
+ */
51
+ controllers: Array<ILlmController | IHttpLlmController>;
52
+
53
+ /**
54
+ * Whether to add controller name as prefix to tool names.
55
+ *
56
+ * If `true`, tool names become `{controllerName}_{methodName}`. If `false`,
57
+ * tool names are just `{methodName}`.
58
+ *
59
+ * @default false
60
+ */
61
+ prefix?: boolean | undefined;
62
+ }): DynamicStructuredTool[] {
63
+ return LangChainToolsRegistrar.convert(props);
64
+ }
@@ -1,149 +1,149 @@
1
- import {
2
- DynamicStructuredTool,
3
- ToolInputParsingException,
4
- } from "@langchain/core/tools";
5
- import {
6
- IHttpLlmController,
7
- IHttpLlmFunction,
8
- ILlmController,
9
- ILlmFunction,
10
- IValidation,
11
- } from "@typia/interface";
12
- import { HttpLlm, LlmJson } from "@typia/utils";
13
-
14
- export namespace LangChainToolsRegistrar {
15
- export const convert = (props: {
16
- controllers: Array<ILlmController | IHttpLlmController>;
17
- prefix?: boolean | undefined;
18
- }): DynamicStructuredTool[] => {
19
- const prefix: boolean = props.prefix ?? false;
20
- const tools: DynamicStructuredTool[] = [];
21
-
22
- // check duplicate tool names
23
- if (prefix === false && props.controllers.length >= 2) {
24
- const names: Map<string, string> = new Map();
25
- const duplicates: string[] = [];
26
- for (const controller of props.controllers) {
27
- for (const func of controller.application.functions) {
28
- const existing: string | undefined = names.get(func.name);
29
- if (existing !== undefined)
30
- duplicates.push(
31
- `"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
32
- );
33
- else names.set(func.name, controller.name);
34
- }
35
- }
36
- if (duplicates.length > 0)
37
- throw new Error(
38
- `Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
39
- );
40
- }
41
-
42
- // convert controllers to tools
43
- for (const controller of props.controllers) {
44
- if (controller.protocol === "class") {
45
- convertClassController(tools, controller, prefix);
46
- } else {
47
- convertHttpController(tools, controller, prefix);
48
- }
49
- }
50
-
51
- return tools;
52
- };
53
-
54
- const convertClassController = (
55
- tools: DynamicStructuredTool[],
56
- controller: ILlmController,
57
- prefix: boolean,
58
- ): void => {
59
- const execute: Record<string, unknown> = controller.execute;
60
-
61
- for (const func of controller.application.functions) {
62
- const toolName: string = prefix
63
- ? `${controller.name}_${func.name}`
64
- : func.name;
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.push(
74
- createTool({
75
- name: toolName,
76
- function: func,
77
- execute: async (args: unknown) => method.call(execute, args),
78
- }),
79
- );
80
- }
81
- };
82
-
83
- const convertHttpController = (
84
- tools: DynamicStructuredTool[],
85
- controller: IHttpLlmController,
86
- prefix: boolean,
87
- ): void => {
88
- const application = controller.application;
89
- const connection = controller.connection;
90
-
91
- for (const func of application.functions) {
92
- const toolName: string = prefix
93
- ? `${controller.name}_${func.name}`
94
- : func.name;
95
-
96
- tools.push(
97
- createTool({
98
- name: toolName,
99
- function: func,
100
- execute: async (args: unknown) => {
101
- if (controller.execute !== undefined) {
102
- const response = await controller.execute({
103
- connection,
104
- application,
105
- function: func,
106
- arguments: args as object,
107
- });
108
- return response.body;
109
- }
110
- return HttpLlm.execute({
111
- application,
112
- function: func,
113
- connection,
114
- input: args as object,
115
- });
116
- },
117
- }),
118
- );
119
- }
120
- };
121
-
122
- const createTool = (entry: {
123
- name: string;
124
- function: ILlmFunction | IHttpLlmFunction;
125
- execute: (args: unknown) => Promise<unknown>;
126
- }): DynamicStructuredTool =>
127
- new DynamicStructuredTool<any>({
128
- name: entry.name,
129
- description: entry.function.description ?? "",
130
- schema: entry.function.parameters,
131
- func: async (args: unknown): Promise<unknown> => {
132
- const coerced: unknown = LlmJson.coerce(
133
- args,
134
- entry.function.parameters,
135
- );
136
- const valid: IValidation<unknown> = entry.function.validate(coerced);
137
- if (valid.success === false)
138
- throw new ToolInputParsingException(
139
- `Type errors in "${entry.name}" arguments:\n\n` +
140
- `\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``,
141
- JSON.stringify(coerced),
142
- );
143
- const result: unknown = await entry.execute(valid.data);
144
- return result === undefined
145
- ? { success: true }
146
- : { success: true, data: result };
147
- },
148
- });
149
- }
1
+ import {
2
+ DynamicStructuredTool,
3
+ ToolInputParsingException,
4
+ } from "@langchain/core/tools";
5
+ import {
6
+ IHttpLlmController,
7
+ IHttpLlmFunction,
8
+ ILlmController,
9
+ ILlmFunction,
10
+ IValidation,
11
+ } from "@typia/interface";
12
+ import { HttpLlm, LlmJson } from "@typia/utils";
13
+
14
+ export namespace LangChainToolsRegistrar {
15
+ export const convert = (props: {
16
+ controllers: Array<ILlmController | IHttpLlmController>;
17
+ prefix?: boolean | undefined;
18
+ }): DynamicStructuredTool[] => {
19
+ const prefix: boolean = props.prefix ?? false;
20
+ const tools: DynamicStructuredTool[] = [];
21
+
22
+ // check duplicate tool names
23
+ if (prefix === false && props.controllers.length >= 2) {
24
+ const names: Map<string, string> = new Map();
25
+ const duplicates: string[] = [];
26
+ for (const controller of props.controllers) {
27
+ for (const func of controller.application.functions) {
28
+ const existing: string | undefined = names.get(func.name);
29
+ if (existing !== undefined)
30
+ duplicates.push(
31
+ `"${func.name}" in "${controller.name}" (conflicts with "${existing}")`,
32
+ );
33
+ else names.set(func.name, controller.name);
34
+ }
35
+ }
36
+ if (duplicates.length > 0)
37
+ throw new Error(
38
+ `Duplicate tool names found:\n - ${duplicates.join("\n - ")}`,
39
+ );
40
+ }
41
+
42
+ // convert controllers to tools
43
+ for (const controller of props.controllers) {
44
+ if (controller.protocol === "class") {
45
+ convertClassController(tools, controller, prefix);
46
+ } else {
47
+ convertHttpController(tools, controller, prefix);
48
+ }
49
+ }
50
+
51
+ return tools;
52
+ };
53
+
54
+ const convertClassController = (
55
+ tools: DynamicStructuredTool[],
56
+ controller: ILlmController,
57
+ prefix: boolean,
58
+ ): void => {
59
+ const execute: Record<string, unknown> = controller.execute;
60
+
61
+ for (const func of controller.application.functions) {
62
+ const toolName: string = prefix
63
+ ? `${controller.name}_${func.name}`
64
+ : func.name;
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.push(
74
+ createTool({
75
+ name: toolName,
76
+ function: func,
77
+ execute: async (args: unknown) => method.call(execute, args),
78
+ }),
79
+ );
80
+ }
81
+ };
82
+
83
+ const convertHttpController = (
84
+ tools: DynamicStructuredTool[],
85
+ controller: IHttpLlmController,
86
+ prefix: boolean,
87
+ ): void => {
88
+ const application = controller.application;
89
+ const connection = controller.connection;
90
+
91
+ for (const func of application.functions) {
92
+ const toolName: string = prefix
93
+ ? `${controller.name}_${func.name}`
94
+ : func.name;
95
+
96
+ tools.push(
97
+ createTool({
98
+ name: toolName,
99
+ function: func,
100
+ execute: async (args: unknown) => {
101
+ if (controller.execute !== undefined) {
102
+ const response = await controller.execute({
103
+ connection,
104
+ application,
105
+ function: func,
106
+ arguments: args as object,
107
+ });
108
+ return response.body;
109
+ }
110
+ return HttpLlm.execute({
111
+ application,
112
+ function: func,
113
+ connection,
114
+ input: args as object,
115
+ });
116
+ },
117
+ }),
118
+ );
119
+ }
120
+ };
121
+
122
+ const createTool = (entry: {
123
+ name: string;
124
+ function: ILlmFunction | IHttpLlmFunction;
125
+ execute: (args: unknown) => Promise<unknown>;
126
+ }): DynamicStructuredTool =>
127
+ new DynamicStructuredTool<any>({
128
+ name: entry.name,
129
+ description: entry.function.description ?? "",
130
+ schema: entry.function.parameters,
131
+ func: async (args: unknown): Promise<unknown> => {
132
+ const coerced: unknown = LlmJson.coerce(
133
+ args,
134
+ entry.function.parameters,
135
+ );
136
+ const valid: IValidation<unknown> = entry.function.validate(coerced);
137
+ if (valid.success === false)
138
+ throw new ToolInputParsingException(
139
+ `Type errors in "${entry.name}" arguments:\n\n` +
140
+ `\`\`\`json\n${LlmJson.stringify(valid)}\n\`\`\``,
141
+ JSON.stringify(coerced),
142
+ );
143
+ const result: unknown = await entry.execute(valid.data);
144
+ return result === undefined
145
+ ? { success: true }
146
+ : { success: true, data: result };
147
+ },
148
+ });
149
+ }