-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiTool.ts
More file actions
66 lines (56 loc) · 1.89 KB
/
Copy pathapiTool.ts
File metadata and controls
66 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { tool } from "langchain";
import type { ApiBasedTool } from "./apiBasedTools.js";
const emptyToolSchema = {
type: "object",
properties: {},
additionalProperties: true,
} as const;
function normalizeToolInputSchema(inputSchema: unknown) {
if (!inputSchema || typeof inputSchema !== "object" || Array.isArray(inputSchema)) {
return emptyToolSchema;
}
const schema = JSON.parse(JSON.stringify(inputSchema)) as Record<string, unknown>;
if (schema.type !== "object") {
return emptyToolSchema;
}
const notes: string[] = [];
if ("if" in schema || "then" in schema || "else" in schema || "allOf" in schema) {
delete schema.if;
delete schema.then;
delete schema.else;
delete schema.allOf;
notes.push("Runtime applies additional conditional validation rules.");
}
if ("oneOf" in schema || "anyOf" in schema || "not" in schema || "enum" in schema) {
delete schema.oneOf;
delete schema.anyOf;
delete schema.not;
delete schema.enum;
notes.push("Top-level composite validation rules are omitted for tool compatibility.");
}
if (notes.length > 0) {
schema.description = typeof schema.description === "string"
? `${schema.description}\n\n${notes.join(" ")}`
: notes.join(" ");
}
return schema;
}
export function createApiTool(toolName: string, apiBasedTool: ApiBasedTool) {
return tool(
async (input, runtime) => {
const normalizedInput = (input ?? {}) as Record<string, unknown>;
return apiBasedTool.call({
adminUser: runtime.context.adminUser,
abortSignal: runtime.context.abortSignal,
inputs: normalizedInput,
userTimeZone: runtime.context.userTimeZone,
});
},
{
name: toolName,
description: apiBasedTool.description ?? `${toolName} tool`,
schema: normalizeToolInputSchema(apiBasedTool.input_schema),
verboseParsingErrors: true,
},
);
}