@langchain/openai 0.6.16 → 1.0.0-alpha.2
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/README.md +8 -8
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/azure/chat_models.cjs +661 -808
- package/dist/azure/chat_models.cjs.map +1 -0
- package/dist/azure/chat_models.d.cts +449 -0
- package/dist/azure/chat_models.d.cts.map +1 -0
- package/dist/azure/chat_models.d.ts +32 -28
- package/dist/azure/chat_models.d.ts.map +1 -0
- package/dist/azure/chat_models.js +660 -804
- package/dist/azure/chat_models.js.map +1 -0
- package/dist/azure/embeddings.cjs +81 -130
- package/dist/azure/embeddings.cjs.map +1 -0
- package/dist/azure/embeddings.d.cts +27 -0
- package/dist/azure/embeddings.d.cts.map +1 -0
- package/dist/azure/embeddings.d.ts +25 -20
- package/dist/azure/embeddings.d.ts.map +1 -0
- package/dist/azure/embeddings.js +80 -126
- package/dist/azure/embeddings.js.map +1 -0
- package/dist/azure/llms.cjs +109 -168
- package/dist/azure/llms.cjs.map +1 -0
- package/dist/azure/llms.d.cts +33 -0
- package/dist/azure/llms.d.cts.map +1 -0
- package/dist/azure/llms.d.ts +31 -25
- package/dist/azure/llms.d.ts.map +1 -0
- package/dist/azure/llms.js +108 -164
- package/dist/azure/llms.js.map +1 -0
- package/dist/chat_models.cjs +1959 -2789
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +931 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +367 -351
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +1954 -2779
- package/dist/chat_models.js.map +1 -0
- package/dist/embeddings.cjs +140 -206
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +120 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +93 -86
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +138 -201
- package/dist/embeddings.js.map +1 -0
- package/dist/index.cjs +47 -34
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -13
- package/dist/index.js +17 -13
- package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
- package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
- package/dist/llms.cjs +312 -468
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +123 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +93 -87
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +309 -462
- package/dist/llms.js.map +1 -0
- package/dist/tools/custom.cjs +25 -26
- package/dist/tools/custom.cjs.map +1 -0
- package/dist/tools/custom.d.cts +10 -0
- package/dist/tools/custom.d.cts.map +1 -0
- package/dist/tools/custom.d.ts +8 -3
- package/dist/tools/custom.d.ts.map +1 -0
- package/dist/tools/custom.js +24 -23
- package/dist/tools/custom.js.map +1 -0
- package/dist/tools/dalle.cjs +104 -194
- package/dist/tools/dalle.cjs.map +1 -0
- package/dist/tools/dalle.d.cts +123 -0
- package/dist/tools/dalle.d.cts.map +1 -0
- package/dist/tools/dalle.d.ts +111 -104
- package/dist/tools/dalle.d.ts.map +1 -0
- package/dist/tools/dalle.js +102 -189
- package/dist/tools/dalle.js.map +1 -0
- package/dist/tools/index.cjs +1 -17
- package/dist/tools/index.js +1 -1
- package/dist/types.d.cts +263 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +244 -246
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/azure.cjs +86 -47
- package/dist/utils/azure.cjs.map +1 -0
- package/dist/utils/azure.d.cts +76 -0
- package/dist/utils/azure.d.cts.map +1 -0
- package/dist/utils/azure.d.ts +51 -9
- package/dist/utils/azure.d.ts.map +1 -0
- package/dist/utils/azure.js +85 -45
- package/dist/utils/azure.js.map +1 -0
- package/dist/utils/client.cjs +25 -0
- package/dist/utils/client.cjs.map +1 -0
- package/dist/utils/client.d.cts +5 -0
- package/dist/utils/client.d.cts.map +1 -0
- package/dist/utils/client.d.ts +5 -0
- package/dist/utils/client.d.ts.map +1 -0
- package/dist/utils/client.js +24 -0
- package/dist/utils/client.js.map +1 -0
- package/dist/utils/errors.cjs +9 -8
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/errors.js +9 -6
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/message_inputs.cjs +139 -0
- package/dist/utils/message_inputs.cjs.map +1 -0
- package/dist/utils/message_inputs.d.cts +9 -0
- package/dist/utils/message_inputs.d.cts.map +1 -0
- package/dist/utils/message_inputs.d.ts +9 -0
- package/dist/utils/message_inputs.d.ts.map +1 -0
- package/dist/utils/message_inputs.js +137 -0
- package/dist/utils/message_inputs.js.map +1 -0
- package/dist/utils/misc.cjs +32 -0
- package/dist/utils/misc.cjs.map +1 -0
- package/dist/utils/misc.d.cts +9 -0
- package/dist/utils/misc.d.cts.map +1 -0
- package/dist/utils/misc.d.ts +9 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +29 -0
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/output.cjs +106 -45
- package/dist/utils/output.cjs.map +1 -0
- package/dist/utils/output.js +104 -43
- package/dist/utils/output.js.map +1 -0
- package/dist/utils/prompts.cjs +39 -38
- package/dist/utils/prompts.cjs.map +1 -0
- package/dist/utils/prompts.d.cts +42 -0
- package/dist/utils/prompts.d.cts.map +1 -0
- package/dist/utils/prompts.d.ts +10 -4
- package/dist/utils/prompts.d.ts.map +1 -0
- package/dist/utils/prompts.js +40 -36
- package/dist/utils/prompts.js.map +1 -0
- package/dist/utils/standard.cjs +315 -0
- package/dist/utils/standard.cjs.map +1 -0
- package/dist/utils/standard.js +313 -0
- package/dist/utils/standard.js.map +1 -0
- package/dist/utils/tools.cjs +150 -127
- package/dist/utils/tools.cjs.map +1 -0
- package/dist/utils/tools.d.cts +13 -0
- package/dist/utils/tools.d.cts.map +1 -0
- package/dist/utils/tools.d.ts +12 -42
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +146 -123
- package/dist/utils/tools.js.map +1 -0
- package/package.json +51 -60
- package/dist/tools/index.d.ts +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -1
- package/dist/utils/errors.d.ts +0 -2
- package/dist/utils/headers.cjs +0 -39
- package/dist/utils/headers.d.ts +0 -8
- package/dist/utils/headers.js +0 -35
- package/dist/utils/openai-format-fndef.cjs +0 -80
- package/dist/utils/openai-format-fndef.d.ts +0 -44
- package/dist/utils/openai-format-fndef.js +0 -77
- package/dist/utils/openai.cjs +0 -127
- package/dist/utils/openai.d.ts +0 -15
- package/dist/utils/openai.js +0 -119
- package/dist/utils/output.d.ts +0 -11
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/utils/tools.js
CHANGED
|
@@ -1,133 +1,156 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isInteropZodSchema } from "@langchain/core/utils/types";
|
|
2
|
+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
3
|
+
import { convertToOpenAITool, isLangChainTool } from "@langchain/core/utils/function_calling";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/tools.ts
|
|
3
6
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
tool
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
toolDef = tool;
|
|
23
|
-
}
|
|
24
|
-
if (toolDef.type === "function" && fields?.strict !== undefined) {
|
|
25
|
-
toolDef.function.strict = fields.strict;
|
|
26
|
-
}
|
|
27
|
-
return toolDef;
|
|
7
|
+
* Formats a tool in either OpenAI format, or LangChain structured tool format
|
|
8
|
+
* into an OpenAI tool format. If the tool is already in OpenAI format, return without
|
|
9
|
+
* any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format
|
|
10
|
+
* using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters
|
|
11
|
+
* returned from the `zodFunction` util are not defined.
|
|
12
|
+
*
|
|
13
|
+
* @param {BindToolsInput} tool The tool to convert to an OpenAI tool.
|
|
14
|
+
* @param {Object} [fields] Additional fields to add to the OpenAI tool.
|
|
15
|
+
* @returns {ToolDefinition} The inputted tool in OpenAI tool format.
|
|
16
|
+
*/
|
|
17
|
+
function _convertToOpenAITool(tool, fields) {
|
|
18
|
+
let toolDef;
|
|
19
|
+
if (isLangChainTool(tool)) toolDef = convertToOpenAITool(tool);
|
|
20
|
+
else toolDef = tool;
|
|
21
|
+
if (fields?.strict !== void 0) toolDef.function.strict = fields.strict;
|
|
22
|
+
return toolDef;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
function isAnyOfProp(prop) {
|
|
25
|
+
return prop.anyOf !== void 0 && Array.isArray(prop.anyOf);
|
|
31
26
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
function formatFunctionDefinitions(functions) {
|
|
28
|
+
const lines = ["namespace functions {", ""];
|
|
29
|
+
for (const f of functions) {
|
|
30
|
+
if (f.description) lines.push(`// ${f.description}`);
|
|
31
|
+
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
32
|
+
lines.push(`type ${f.name} = (_: {`);
|
|
33
|
+
lines.push(formatObjectProperties(f.parameters, 0));
|
|
34
|
+
lines.push("}) => any;");
|
|
35
|
+
} else lines.push(`type ${f.name} = () => any;`);
|
|
36
|
+
lines.push("");
|
|
37
|
+
}
|
|
38
|
+
lines.push("} // namespace functions");
|
|
39
|
+
return lines.join("\n");
|
|
37
40
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
tool.metadata.customTool !== null);
|
|
41
|
+
function formatObjectProperties(obj, indent) {
|
|
42
|
+
const lines = [];
|
|
43
|
+
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
44
|
+
if (param.description && indent < 2) lines.push(`// ${param.description}`);
|
|
45
|
+
if (obj.required?.includes(name)) lines.push(`${name}: ${formatType(param, indent)},`);
|
|
46
|
+
else lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
47
|
+
}
|
|
48
|
+
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
function formatType(param, indent) {
|
|
51
|
+
if (isAnyOfProp(param)) return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
52
|
+
switch (param.type) {
|
|
53
|
+
case "string":
|
|
54
|
+
if (param.enum) return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
55
|
+
return "string";
|
|
56
|
+
case "number":
|
|
57
|
+
if (param.enum) return param.enum.map((v) => `${v}`).join(" | ");
|
|
58
|
+
return "number";
|
|
59
|
+
case "integer":
|
|
60
|
+
if (param.enum) return param.enum.map((v) => `${v}`).join(" | ");
|
|
61
|
+
return "number";
|
|
62
|
+
case "boolean": return "boolean";
|
|
63
|
+
case "null": return "null";
|
|
64
|
+
case "object": return [
|
|
65
|
+
"{",
|
|
66
|
+
formatObjectProperties(param, indent + 2),
|
|
67
|
+
"}"
|
|
68
|
+
].join("\n");
|
|
69
|
+
case "array":
|
|
70
|
+
if (param.items) return `${formatType(param.items, indent)}[]`;
|
|
71
|
+
return "any[]";
|
|
72
|
+
default: return "";
|
|
73
|
+
}
|
|
54
74
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
id: rawToolCall.call_id,
|
|
66
|
-
name: rawToolCall.name,
|
|
67
|
-
isCustomTool: true,
|
|
68
|
-
args: {
|
|
69
|
-
input: rawToolCall.input,
|
|
70
|
-
},
|
|
71
|
-
};
|
|
75
|
+
function formatToOpenAIToolChoice(toolChoice) {
|
|
76
|
+
if (!toolChoice) return void 0;
|
|
77
|
+
else if (toolChoice === "any" || toolChoice === "required") return "required";
|
|
78
|
+
else if (toolChoice === "auto") return "auto";
|
|
79
|
+
else if (toolChoice === "none") return "none";
|
|
80
|
+
else if (typeof toolChoice === "string") return {
|
|
81
|
+
type: "function",
|
|
82
|
+
function: { name: toolChoice }
|
|
83
|
+
};
|
|
84
|
+
else return toolChoice;
|
|
72
85
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"isCustomTool" in toolCall &&
|
|
76
|
-
toolCall.isCustomTool === true);
|
|
86
|
+
function isBuiltInTool(tool) {
|
|
87
|
+
return "type" in tool && tool.type !== "function";
|
|
77
88
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (!tool.custom.format) {
|
|
81
|
-
return undefined;
|
|
82
|
-
}
|
|
83
|
-
if (tool.custom.format.type === "grammar") {
|
|
84
|
-
return {
|
|
85
|
-
type: "grammar",
|
|
86
|
-
definition: tool.custom.format.grammar.definition,
|
|
87
|
-
syntax: tool.custom.format.grammar.syntax,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
if (tool.custom.format.type === "text") {
|
|
91
|
-
return {
|
|
92
|
-
type: "text",
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
return undefined;
|
|
96
|
-
};
|
|
97
|
-
return {
|
|
98
|
-
type: "custom",
|
|
99
|
-
name: tool.custom.name,
|
|
100
|
-
description: tool.custom.description,
|
|
101
|
-
format: getFormat(),
|
|
102
|
-
};
|
|
89
|
+
function isBuiltInToolChoice(tool_choice) {
|
|
90
|
+
return tool_choice != null && typeof tool_choice === "object" && "type" in tool_choice && tool_choice.type !== "function";
|
|
103
91
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (!tool.format) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
if (tool.format.type === "grammar") {
|
|
110
|
-
return {
|
|
111
|
-
type: "grammar",
|
|
112
|
-
grammar: {
|
|
113
|
-
definition: tool.format.definition,
|
|
114
|
-
syntax: tool.format.syntax,
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
if (tool.format.type === "text") {
|
|
119
|
-
return {
|
|
120
|
-
type: "text",
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
return undefined;
|
|
124
|
-
};
|
|
125
|
-
return {
|
|
126
|
-
type: "custom",
|
|
127
|
-
custom: {
|
|
128
|
-
name: tool.name,
|
|
129
|
-
description: tool.description,
|
|
130
|
-
format: getFormat(),
|
|
131
|
-
},
|
|
132
|
-
};
|
|
92
|
+
function isCustomTool(tool) {
|
|
93
|
+
return typeof tool === "object" && tool !== null && "metadata" in tool && typeof tool.metadata === "object" && tool.metadata !== null && "customTool" in tool.metadata && typeof tool.metadata.customTool === "object" && tool.metadata.customTool !== null;
|
|
133
94
|
}
|
|
95
|
+
function isOpenAICustomTool(tool) {
|
|
96
|
+
return "type" in tool && tool.type === "custom" && "custom" in tool && typeof tool.custom === "object" && tool.custom !== null;
|
|
97
|
+
}
|
|
98
|
+
function parseCustomToolCall(rawToolCall) {
|
|
99
|
+
if (rawToolCall.type !== "custom_tool_call") return void 0;
|
|
100
|
+
return {
|
|
101
|
+
...rawToolCall,
|
|
102
|
+
type: "tool_call",
|
|
103
|
+
call_id: rawToolCall.id,
|
|
104
|
+
id: rawToolCall.call_id,
|
|
105
|
+
name: rawToolCall.name,
|
|
106
|
+
isCustomTool: true,
|
|
107
|
+
args: { input: rawToolCall.input }
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function isCustomToolCall(toolCall) {
|
|
111
|
+
return toolCall.type === "tool_call" && "isCustomTool" in toolCall && toolCall.isCustomTool === true;
|
|
112
|
+
}
|
|
113
|
+
function convertCompletionsCustomTool(tool) {
|
|
114
|
+
const getFormat = () => {
|
|
115
|
+
if (!tool.custom.format) return void 0;
|
|
116
|
+
if (tool.custom.format.type === "grammar") return {
|
|
117
|
+
type: "grammar",
|
|
118
|
+
definition: tool.custom.format.grammar.definition,
|
|
119
|
+
syntax: tool.custom.format.grammar.syntax
|
|
120
|
+
};
|
|
121
|
+
if (tool.custom.format.type === "text") return { type: "text" };
|
|
122
|
+
return void 0;
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
type: "custom",
|
|
126
|
+
name: tool.custom.name,
|
|
127
|
+
description: tool.custom.description,
|
|
128
|
+
format: getFormat()
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function convertResponsesCustomTool(tool) {
|
|
132
|
+
const getFormat = () => {
|
|
133
|
+
if (!tool.format) return void 0;
|
|
134
|
+
if (tool.format.type === "grammar") return {
|
|
135
|
+
type: "grammar",
|
|
136
|
+
grammar: {
|
|
137
|
+
definition: tool.format.definition,
|
|
138
|
+
syntax: tool.format.syntax
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
if (tool.format.type === "text") return { type: "text" };
|
|
142
|
+
return void 0;
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
type: "custom",
|
|
146
|
+
custom: {
|
|
147
|
+
name: tool.name,
|
|
148
|
+
description: tool.description,
|
|
149
|
+
format: getFormat()
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
//#endregion
|
|
155
|
+
export { _convertToOpenAITool, convertCompletionsCustomTool, convertResponsesCustomTool, formatFunctionDefinitions, formatToOpenAIToolChoice, isBuiltInTool, isBuiltInToolChoice, isCustomTool, isCustomToolCall, isOpenAICustomTool, parseCustomToolCall };
|
|
156
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","names":["tool: BindToolsInput","fields?: {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }","toolDef: OpenAIClient.ChatCompletionTool | undefined","formatToOpenAITool","prop: Prop","functions: FunctionDef[]","obj: ObjectProp","indent: number","lines: string[]","param: Prop","toolChoice?: OpenAIToolChoice","tool: ChatOpenAIToolType","tool_choice: OpenAIToolChoice | ResponsesToolChoice | undefined","tool: unknown","rawToolCall: Record<string, any>","toolCall: ToolCall","tool: OpenAIClient.Chat.ChatCompletionCustomTool","tool: OpenAIClient.Responses.CustomTool"],"sources":["../../src/utils/tools.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\n\nimport { ToolDefinition } from \"@langchain/core/language_models/base\";\nimport { BindToolsInput } from \"@langchain/core/language_models/chat_models\";\nimport {\n convertToOpenAITool as formatToOpenAITool,\n isLangChainTool,\n} from \"@langchain/core/utils/function_calling\";\nimport { DynamicTool, StructuredToolInterface } from \"@langchain/core/tools\";\nimport { isInteropZodSchema } from \"@langchain/core/utils/types\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport { ToolCall } from \"@langchain/core/messages/tool\";\n\n/**\n * Formats a tool in either OpenAI format, or LangChain structured tool format\n * into an OpenAI tool format. If the tool is already in OpenAI format, return without\n * any changes. If it is in LangChain structured tool format, convert it to OpenAI tool format\n * using OpenAI's `zodFunction` util, falling back to `convertToOpenAIFunction` if the parameters\n * returned from the `zodFunction` util are not defined.\n *\n * @param {BindToolsInput} tool The tool to convert to an OpenAI tool.\n * @param {Object} [fields] Additional fields to add to the OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function _convertToOpenAITool(\n tool: BindToolsInput,\n fields?: {\n /**\n * If `true`, model output is guaranteed to exactly match the JSON Schema\n * provided in the function definition.\n */\n strict?: boolean;\n }\n): OpenAIClient.ChatCompletionTool {\n let toolDef: OpenAIClient.ChatCompletionTool | undefined;\n\n if (isLangChainTool(tool)) {\n toolDef = formatToOpenAITool(tool);\n } else {\n toolDef = tool as ToolDefinition;\n }\n\n if (fields?.strict !== undefined) {\n toolDef.function.strict = fields.strict;\n }\n\n return toolDef;\n}\n\ntype OpenAIFunction = OpenAIClient.Chat.ChatCompletionCreateParams.Function;\n\n// Types representing the OpenAI function definitions. While the OpenAI client library\n// does have types for function definitions, the properties are just Record<string, unknown>,\n// which isn't very useful for type checking this formatting code.\nexport interface FunctionDef extends Omit<OpenAIFunction, \"parameters\"> {\n name: string;\n description?: string;\n parameters: ObjectProp;\n}\n\ninterface ObjectProp {\n type: \"object\";\n properties?: {\n [key: string]: Prop;\n };\n required?: string[];\n}\n\ninterface AnyOfProp {\n anyOf: Prop[];\n}\n\ntype Prop = {\n description?: string;\n} & (\n | AnyOfProp\n | ObjectProp\n | {\n type: \"string\";\n enum?: string[];\n }\n | {\n type: \"number\" | \"integer\";\n minimum?: number;\n maximum?: number;\n enum?: number[];\n }\n | { type: \"boolean\" }\n | { type: \"null\" }\n | {\n type: \"array\";\n items?: Prop;\n }\n);\n\nfunction isAnyOfProp(prop: Prop): prop is AnyOfProp {\n return (\n (prop as AnyOfProp).anyOf !== undefined &&\n Array.isArray((prop as AnyOfProp).anyOf)\n );\n}\n\n// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.\n// This function converts the JSON schemas into TypeScript definitions.\nexport function formatFunctionDefinitions(functions: FunctionDef[]) {\n const lines = [\"namespace functions {\", \"\"];\n for (const f of functions) {\n if (f.description) {\n lines.push(`// ${f.description}`);\n }\n if (Object.keys(f.parameters.properties ?? {}).length > 0) {\n lines.push(`type ${f.name} = (_: {`);\n lines.push(formatObjectProperties(f.parameters, 0));\n lines.push(\"}) => any;\");\n } else {\n lines.push(`type ${f.name} = () => any;`);\n }\n lines.push(\"\");\n }\n lines.push(\"} // namespace functions\");\n return lines.join(\"\\n\");\n}\n\n// Format just the properties of an object (not including the surrounding braces)\nfunction formatObjectProperties(obj: ObjectProp, indent: number): string {\n const lines: string[] = [];\n for (const [name, param] of Object.entries(obj.properties ?? {})) {\n if (param.description && indent < 2) {\n lines.push(`// ${param.description}`);\n }\n if (obj.required?.includes(name)) {\n lines.push(`${name}: ${formatType(param, indent)},`);\n } else {\n lines.push(`${name}?: ${formatType(param, indent)},`);\n }\n }\n return lines.map((line) => \" \".repeat(indent) + line).join(\"\\n\");\n}\n\n// Format a single property type\nfunction formatType(param: Prop, indent: number): string {\n if (isAnyOfProp(param)) {\n return param.anyOf.map((v) => formatType(v, indent)).join(\" | \");\n }\n switch (param.type) {\n case \"string\":\n if (param.enum) {\n return param.enum.map((v) => `\"${v}\"`).join(\" | \");\n }\n return \"string\";\n case \"number\":\n if (param.enum) {\n return param.enum.map((v) => `${v}`).join(\" | \");\n }\n return \"number\";\n case \"integer\":\n if (param.enum) {\n return param.enum.map((v) => `${v}`).join(\" | \");\n }\n return \"number\";\n case \"boolean\":\n return \"boolean\";\n case \"null\":\n return \"null\";\n case \"object\":\n return [\"{\", formatObjectProperties(param, indent + 2), \"}\"].join(\"\\n\");\n case \"array\":\n if (param.items) {\n return `${formatType(param.items, indent)}[]`;\n }\n return \"any[]\";\n default:\n return \"\";\n }\n}\n\nexport function formatToOpenAIAssistantTool(\n tool: StructuredToolInterface\n): ToolDefinition {\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: isInteropZodSchema(tool.schema)\n ? toJsonSchema(tool.schema)\n : tool.schema,\n },\n };\n}\n\nexport type OpenAIToolChoice =\n | OpenAIClient.ChatCompletionToolChoiceOption\n | \"any\"\n | string;\n\nexport type ResponsesToolChoice = NonNullable<\n OpenAIClient.Responses.ResponseCreateParams[\"tool_choice\"]\n>;\n\nexport type ChatOpenAIToolType =\n | BindToolsInput\n | OpenAIClient.Chat.ChatCompletionTool\n | ResponsesTool;\n\nexport type ResponsesTool = NonNullable<\n OpenAIClient.Responses.ResponseCreateParams[\"tools\"]\n>[number];\n\nexport function formatToOpenAIToolChoice(\n toolChoice?: OpenAIToolChoice\n): OpenAIClient.ChatCompletionToolChoiceOption | undefined {\n if (!toolChoice) {\n return undefined;\n } else if (toolChoice === \"any\" || toolChoice === \"required\") {\n return \"required\";\n } else if (toolChoice === \"auto\") {\n return \"auto\";\n } else if (toolChoice === \"none\") {\n return \"none\";\n } else if (typeof toolChoice === \"string\") {\n return {\n type: \"function\",\n function: {\n name: toolChoice,\n },\n };\n } else {\n return toolChoice;\n }\n}\n\nexport function isBuiltInTool(tool: ChatOpenAIToolType): tool is ResponsesTool {\n return \"type\" in tool && tool.type !== \"function\";\n}\n\nexport function isBuiltInToolChoice(\n tool_choice: OpenAIToolChoice | ResponsesToolChoice | undefined\n): tool_choice is ResponsesToolChoice {\n return (\n tool_choice != null &&\n typeof tool_choice === \"object\" &&\n \"type\" in tool_choice &&\n tool_choice.type !== \"function\"\n );\n}\n\nexport type CustomToolCall = ToolCall & {\n call_id: string;\n isCustomTool: true;\n};\n\ntype LangchainCustomTool = DynamicTool<string> & {\n metadata: {\n customTool: OpenAIClient.Responses.CustomTool;\n };\n};\n\nexport function isCustomTool(tool: unknown): tool is LangchainCustomTool {\n return (\n typeof tool === \"object\" &&\n tool !== null &&\n \"metadata\" in tool &&\n typeof tool.metadata === \"object\" &&\n tool.metadata !== null &&\n \"customTool\" in tool.metadata &&\n typeof tool.metadata.customTool === \"object\" &&\n tool.metadata.customTool !== null\n );\n}\n\nexport function isOpenAICustomTool(\n tool: ChatOpenAIToolType\n): tool is OpenAIClient.Chat.ChatCompletionCustomTool {\n return (\n \"type\" in tool &&\n tool.type === \"custom\" &&\n \"custom\" in tool &&\n typeof tool.custom === \"object\" &&\n tool.custom !== null\n );\n}\n\nexport function parseCustomToolCall(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n rawToolCall: Record<string, any>\n): CustomToolCall | undefined {\n if (rawToolCall.type !== \"custom_tool_call\") {\n return undefined;\n }\n return {\n ...rawToolCall,\n type: \"tool_call\",\n call_id: rawToolCall.id,\n id: rawToolCall.call_id,\n name: rawToolCall.name,\n isCustomTool: true,\n args: {\n input: rawToolCall.input,\n },\n };\n}\n\nexport function isCustomToolCall(\n toolCall: ToolCall\n): toolCall is CustomToolCall {\n return (\n toolCall.type === \"tool_call\" &&\n \"isCustomTool\" in toolCall &&\n toolCall.isCustomTool === true\n );\n}\n\nexport function convertCompletionsCustomTool(\n tool: OpenAIClient.Chat.ChatCompletionCustomTool\n): OpenAIClient.Responses.CustomTool {\n const getFormat = () => {\n if (!tool.custom.format) {\n return undefined;\n }\n if (tool.custom.format.type === \"grammar\") {\n return {\n type: \"grammar\" as const,\n definition: tool.custom.format.grammar.definition,\n syntax: tool.custom.format.grammar.syntax,\n };\n }\n if (tool.custom.format.type === \"text\") {\n return {\n type: \"text\" as const,\n };\n }\n return undefined;\n };\n return {\n type: \"custom\",\n name: tool.custom.name,\n description: tool.custom.description,\n format: getFormat(),\n };\n}\n\nexport function convertResponsesCustomTool(\n tool: OpenAIClient.Responses.CustomTool\n): OpenAIClient.Chat.ChatCompletionCustomTool {\n const getFormat = () => {\n if (!tool.format) {\n return undefined;\n }\n if (tool.format.type === \"grammar\") {\n return {\n type: \"grammar\" as const,\n grammar: {\n definition: tool.format.definition,\n syntax: tool.format.syntax,\n },\n };\n }\n if (tool.format.type === \"text\") {\n return {\n type: \"text\" as const,\n };\n }\n return undefined;\n };\n return {\n type: \"custom\",\n custom: {\n name: tool.name,\n description: tool.description,\n format: getFormat(),\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAwBA,SAAgB,qBACdA,MACAC,QAOiC;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GACN,SAAS;IACP,YAAY,KAAK,OAAO;IACxB,QAAQ,KAAK,OAAO;GACrB;EACF;AAEH,MAAI,KAAK,OAAO,SAAS,OACvB,QAAO,EACL,MAAM,OACP;AAEH,SAAO;CACR;AACD,QAAO;EACL,MAAM;EACN,QAAQ;GACN,MAAM,KAAK;GACX,aAAa,KAAK;GAClB,QAAQ,WAAW;EACpB;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,70 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/openai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "OpenAI integrations for LangChain.js",
|
|
5
|
+
"author": "LangChain",
|
|
6
|
+
"license": "MIT",
|
|
5
7
|
"type": "module",
|
|
6
8
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
9
|
+
"node": ">=20"
|
|
8
10
|
},
|
|
9
|
-
"main": "./index.js",
|
|
10
|
-
"types": "./index.d.ts",
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
13
|
"url": "git@github.com:langchain-ai/langchainjs.git"
|
|
14
14
|
},
|
|
15
|
-
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-openai/",
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "yarn turbo:command build:internal --filter=@langchain/openai",
|
|
18
|
-
"build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
|
|
19
|
-
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
|
|
20
|
-
"lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
21
|
-
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
22
|
-
"lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
|
|
23
|
-
"clean": "rm -rf .turbo dist/",
|
|
24
|
-
"prepack": "yarn build",
|
|
25
|
-
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
26
|
-
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
|
|
27
|
-
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
|
|
28
|
-
"test:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
|
|
29
|
-
"test:standard:unit": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.test.ts --testTimeout 100000 --maxWorkers=50%",
|
|
30
|
-
"test:standard:int": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.standard\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
|
|
31
|
-
"test:standard": "yarn test:standard:unit && yarn test:standard:int",
|
|
32
|
-
"format": "prettier --config .prettierrc --write \"src\"",
|
|
33
|
-
"format:check": "prettier --config .prettierrc --check \"src\""
|
|
34
|
-
},
|
|
35
|
-
"author": "LangChain",
|
|
36
|
-
"license": "MIT",
|
|
15
|
+
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-openai/",
|
|
37
16
|
"dependencies": {
|
|
38
17
|
"js-tiktoken": "^1.0.12",
|
|
39
|
-
"openai": "5.
|
|
40
|
-
"zod": "^3.25.
|
|
18
|
+
"openai": "^5.3.0",
|
|
19
|
+
"zod": "^3.25.76 || ^4"
|
|
41
20
|
},
|
|
42
21
|
"peerDependencies": {
|
|
43
|
-
"@langchain/core": "
|
|
22
|
+
"@langchain/core": "^1.0.0-alpha.6"
|
|
44
23
|
},
|
|
45
24
|
"devDependencies": {
|
|
46
25
|
"@azure/identity": "^4.2.1",
|
|
47
|
-
"@
|
|
48
|
-
"@
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"@swc/core": "^1.3.90",
|
|
52
|
-
"@swc/jest": "^0.2.29",
|
|
26
|
+
"@cfworker/json-schema": "^4.1.1",
|
|
27
|
+
"@tsconfig/recommended": "^1.0.10",
|
|
28
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
29
|
+
"dotenv": "^17.2.1",
|
|
53
30
|
"dpdm": "^3.14.0",
|
|
54
|
-
"eslint": "^
|
|
55
|
-
"eslint-config-airbnb-base": "^15.0.0",
|
|
56
|
-
"eslint-config-prettier": "^8.6.0",
|
|
57
|
-
"eslint-plugin-import": "^2.31.0",
|
|
58
|
-
"eslint-plugin-jest": "^27.6.0",
|
|
59
|
-
"eslint-plugin-no-instanceof": "^1.0.1",
|
|
60
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
61
|
-
"jest": "^29.5.0",
|
|
62
|
-
"jest-environment-node": "^29.6.4",
|
|
31
|
+
"eslint": "^9.34.0",
|
|
63
32
|
"prettier": "^2.8.3",
|
|
64
|
-
"release-it": "^
|
|
33
|
+
"release-it": "^19.0.4",
|
|
34
|
+
"release-it-pnpm": "^4.6.6",
|
|
65
35
|
"rimraf": "^5.0.1",
|
|
66
|
-
"
|
|
67
|
-
"
|
|
36
|
+
"typescript": "~5.8.3",
|
|
37
|
+
"uuid": "^11.1.0",
|
|
38
|
+
"vitest": "^3.2.4",
|
|
39
|
+
"zod-to-json-schema": "^3.24.6",
|
|
40
|
+
"@langchain/standard-tests": "0.0.0",
|
|
41
|
+
"@langchain/eslint": "0.1.0",
|
|
42
|
+
"@langchain/core": "1.0.0-alpha.6"
|
|
68
43
|
},
|
|
69
44
|
"publishConfig": {
|
|
70
45
|
"access": "public"
|
|
@@ -83,23 +58,39 @@
|
|
|
83
58
|
"embeddings",
|
|
84
59
|
"vectorstores"
|
|
85
60
|
],
|
|
61
|
+
"main": "./dist/index.js",
|
|
62
|
+
"types": "./dist/index.d.ts",
|
|
86
63
|
"exports": {
|
|
87
64
|
".": {
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
|
|
65
|
+
"import": {
|
|
66
|
+
"types": "./dist/index.d.ts",
|
|
67
|
+
"default": "./dist/index.js"
|
|
68
|
+
},
|
|
69
|
+
"require": {
|
|
70
|
+
"types": "./dist/index.d.cts",
|
|
71
|
+
"default": "./dist/index.cjs"
|
|
92
72
|
},
|
|
93
|
-
"
|
|
94
|
-
"require": "./index.cjs"
|
|
73
|
+
"input": "./src/index.ts"
|
|
95
74
|
},
|
|
96
75
|
"./package.json": "./package.json"
|
|
97
76
|
},
|
|
98
77
|
"files": [
|
|
99
|
-
"dist/"
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
|
|
105
|
-
|
|
78
|
+
"dist/"
|
|
79
|
+
],
|
|
80
|
+
"scripts": {
|
|
81
|
+
"build": "pnpm --filter @langchain/build compile @langchain/openai",
|
|
82
|
+
"lint:eslint": "eslint --cache src/",
|
|
83
|
+
"lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
84
|
+
"lint": "pnpm lint:eslint && pnpm lint:dpdm",
|
|
85
|
+
"lint:fix": "pnpm lint:eslint --fix && pnpm lint:dpdm",
|
|
86
|
+
"clean": "rm -rf .turbo dist/",
|
|
87
|
+
"test": "vitest run",
|
|
88
|
+
"test:watch": "vitest",
|
|
89
|
+
"test:int": "vitest run --mode int",
|
|
90
|
+
"test:standard:unit": "vitest run --mode standard-unit",
|
|
91
|
+
"test:standard:int": "vitest run --mode standard-int",
|
|
92
|
+
"test:standard": "pnpm test:standard:unit && pnpm test:standard:int",
|
|
93
|
+
"format": "prettier --config .prettierrc --write \"src\"",
|
|
94
|
+
"format:check": "prettier --config .prettierrc --check \"src\""
|
|
95
|
+
}
|
|
96
|
+
}
|
package/dist/tools/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./dalle.js";
|
package/dist/types.cjs
DELETED
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/utils/errors.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export type LangChainErrorCodes = "INVALID_PROMPT_INPUT" | "INVALID_TOOL_RESULTS" | "MESSAGE_COERCION_FAILURE" | "MODEL_AUTHENTICATION" | "MODEL_NOT_FOUND" | "MODEL_RATE_LIMIT" | "OUTPUT_PARSING_FAILURE";
|
|
2
|
-
export declare function addLangChainErrorFields(error: any, lc_error_code: LangChainErrorCodes): any;
|
package/dist/utils/headers.cjs
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isHeaders = isHeaders;
|
|
4
|
-
exports.normalizeHeaders = normalizeHeaders;
|
|
5
|
-
const iife = (fn) => fn();
|
|
6
|
-
function isHeaders(headers) {
|
|
7
|
-
return (typeof Headers !== "undefined" &&
|
|
8
|
-
headers !== null &&
|
|
9
|
-
typeof headers === "object" &&
|
|
10
|
-
Object.prototype.toString.call(headers) === "[object Headers]");
|
|
11
|
-
}
|
|
12
|
-
function normalizeHeaders(headers) {
|
|
13
|
-
const output = iife(() => {
|
|
14
|
-
// If headers is a Headers instance
|
|
15
|
-
if (isHeaders(headers)) {
|
|
16
|
-
return headers;
|
|
17
|
-
}
|
|
18
|
-
// If headers is an array of [key, value] pairs
|
|
19
|
-
else if (Array.isArray(headers)) {
|
|
20
|
-
return new Headers(headers);
|
|
21
|
-
}
|
|
22
|
-
// If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
|
|
23
|
-
else if (typeof headers === "object" &&
|
|
24
|
-
headers !== null &&
|
|
25
|
-
"values" in headers &&
|
|
26
|
-
isHeaders(headers.values)) {
|
|
27
|
-
return headers.values;
|
|
28
|
-
}
|
|
29
|
-
// If headers is a plain object
|
|
30
|
-
else if (typeof headers === "object" && headers !== null) {
|
|
31
|
-
const entries = Object.entries(headers)
|
|
32
|
-
.filter(([, v]) => typeof v === "string")
|
|
33
|
-
.map(([k, v]) => [k, v]);
|
|
34
|
-
return new Headers(entries);
|
|
35
|
-
}
|
|
36
|
-
return new Headers();
|
|
37
|
-
});
|
|
38
|
-
return Object.fromEntries(output.entries());
|
|
39
|
-
}
|
package/dist/utils/headers.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
type HeaderValue = string | undefined | null;
|
|
2
|
-
export type HeadersLike = Headers | readonly HeaderValue[][] | Record<string, HeaderValue | readonly HeaderValue[]> | undefined | null | {
|
|
3
|
-
values: Headers;
|
|
4
|
-
[key: string]: unknown;
|
|
5
|
-
};
|
|
6
|
-
export declare function isHeaders(headers: unknown): headers is Headers;
|
|
7
|
-
export declare function normalizeHeaders(headers: HeadersLike): Record<string, HeaderValue | readonly HeaderValue[]>;
|
|
8
|
-
export {};
|
package/dist/utils/headers.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
const iife = (fn) => fn();
|
|
2
|
-
export function isHeaders(headers) {
|
|
3
|
-
return (typeof Headers !== "undefined" &&
|
|
4
|
-
headers !== null &&
|
|
5
|
-
typeof headers === "object" &&
|
|
6
|
-
Object.prototype.toString.call(headers) === "[object Headers]");
|
|
7
|
-
}
|
|
8
|
-
export function normalizeHeaders(headers) {
|
|
9
|
-
const output = iife(() => {
|
|
10
|
-
// If headers is a Headers instance
|
|
11
|
-
if (isHeaders(headers)) {
|
|
12
|
-
return headers;
|
|
13
|
-
}
|
|
14
|
-
// If headers is an array of [key, value] pairs
|
|
15
|
-
else if (Array.isArray(headers)) {
|
|
16
|
-
return new Headers(headers);
|
|
17
|
-
}
|
|
18
|
-
// If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
|
|
19
|
-
else if (typeof headers === "object" &&
|
|
20
|
-
headers !== null &&
|
|
21
|
-
"values" in headers &&
|
|
22
|
-
isHeaders(headers.values)) {
|
|
23
|
-
return headers.values;
|
|
24
|
-
}
|
|
25
|
-
// If headers is a plain object
|
|
26
|
-
else if (typeof headers === "object" && headers !== null) {
|
|
27
|
-
const entries = Object.entries(headers)
|
|
28
|
-
.filter(([, v]) => typeof v === "string")
|
|
29
|
-
.map(([k, v]) => [k, v]);
|
|
30
|
-
return new Headers(entries);
|
|
31
|
-
}
|
|
32
|
-
return new Headers();
|
|
33
|
-
});
|
|
34
|
-
return Object.fromEntries(output.entries());
|
|
35
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatFunctionDefinitions = formatFunctionDefinitions;
|
|
4
|
-
function isAnyOfProp(prop) {
|
|
5
|
-
return (prop.anyOf !== undefined &&
|
|
6
|
-
Array.isArray(prop.anyOf));
|
|
7
|
-
}
|
|
8
|
-
// When OpenAI use functions in the prompt, they format them as TypeScript definitions rather than OpenAPI JSON schemas.
|
|
9
|
-
// This function converts the JSON schemas into TypeScript definitions.
|
|
10
|
-
function formatFunctionDefinitions(functions) {
|
|
11
|
-
const lines = ["namespace functions {", ""];
|
|
12
|
-
for (const f of functions) {
|
|
13
|
-
if (f.description) {
|
|
14
|
-
lines.push(`// ${f.description}`);
|
|
15
|
-
}
|
|
16
|
-
if (Object.keys(f.parameters.properties ?? {}).length > 0) {
|
|
17
|
-
lines.push(`type ${f.name} = (_: {`);
|
|
18
|
-
lines.push(formatObjectProperties(f.parameters, 0));
|
|
19
|
-
lines.push("}) => any;");
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
lines.push(`type ${f.name} = () => any;`);
|
|
23
|
-
}
|
|
24
|
-
lines.push("");
|
|
25
|
-
}
|
|
26
|
-
lines.push("} // namespace functions");
|
|
27
|
-
return lines.join("\n");
|
|
28
|
-
}
|
|
29
|
-
// Format just the properties of an object (not including the surrounding braces)
|
|
30
|
-
function formatObjectProperties(obj, indent) {
|
|
31
|
-
const lines = [];
|
|
32
|
-
for (const [name, param] of Object.entries(obj.properties ?? {})) {
|
|
33
|
-
if (param.description && indent < 2) {
|
|
34
|
-
lines.push(`// ${param.description}`);
|
|
35
|
-
}
|
|
36
|
-
if (obj.required?.includes(name)) {
|
|
37
|
-
lines.push(`${name}: ${formatType(param, indent)},`);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
lines.push(`${name}?: ${formatType(param, indent)},`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return lines.map((line) => " ".repeat(indent) + line).join("\n");
|
|
44
|
-
}
|
|
45
|
-
// Format a single property type
|
|
46
|
-
function formatType(param, indent) {
|
|
47
|
-
if (isAnyOfProp(param)) {
|
|
48
|
-
return param.anyOf.map((v) => formatType(v, indent)).join(" | ");
|
|
49
|
-
}
|
|
50
|
-
switch (param.type) {
|
|
51
|
-
case "string":
|
|
52
|
-
if (param.enum) {
|
|
53
|
-
return param.enum.map((v) => `"${v}"`).join(" | ");
|
|
54
|
-
}
|
|
55
|
-
return "string";
|
|
56
|
-
case "number":
|
|
57
|
-
if (param.enum) {
|
|
58
|
-
return param.enum.map((v) => `${v}`).join(" | ");
|
|
59
|
-
}
|
|
60
|
-
return "number";
|
|
61
|
-
case "integer":
|
|
62
|
-
if (param.enum) {
|
|
63
|
-
return param.enum.map((v) => `${v}`).join(" | ");
|
|
64
|
-
}
|
|
65
|
-
return "number";
|
|
66
|
-
case "boolean":
|
|
67
|
-
return "boolean";
|
|
68
|
-
case "null":
|
|
69
|
-
return "null";
|
|
70
|
-
case "object":
|
|
71
|
-
return ["{", formatObjectProperties(param, indent + 2), "}"].join("\n");
|
|
72
|
-
case "array":
|
|
73
|
-
if (param.items) {
|
|
74
|
-
return `${formatType(param.items, indent)}[]`;
|
|
75
|
-
}
|
|
76
|
-
return "any[]";
|
|
77
|
-
default:
|
|
78
|
-
return "";
|
|
79
|
-
}
|
|
80
|
-
}
|