Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiTool.ts
More file actions
Latest commit
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";
importtype{ApiBasedTool}from"./apiBasedTools.js";
constemptyToolSchema={
type: "object",
properties: {},
additionalProperties: true,
}asconst;
functionnormalizeToolInputSchema(inputSchema: unknown){
if(!inputSchema||typeofinputSchema!=="object"||Array.isArray(inputSchema)){
returnemptyToolSchema;
}
constschema=JSON.parse(JSON.stringify(inputSchema))asRecord<string,unknown>;
if(schema.type!=="object"){
returnemptyToolSchema;
}
constnotes: string[]=[];
if("if"inschema||"then"inschema||"else"inschema||"allOf"inschema){
deleteschema.if;
deleteschema.then;
deleteschema.else;
deleteschema.allOf;
notes.push("Runtime applies additional conditional validation rules.");
}
if("oneOf"inschema||"anyOf"inschema||"not"inschema||"enum"inschema){
deleteschema.oneOf;
deleteschema.anyOf;
deleteschema.not;
deleteschema.enum;
notes.push("Top-level composite validation rules are omitted for tool compatibility.");
}
if(notes.length>0){
schema.description=typeofschema.description==="string"
? `${schema.description}\n\n${notes.join(" ")}`
: notes.join(" ");
}
returnschema;
}
exportfunctioncreateApiTool(toolName: string,apiBasedTool: ApiBasedTool){
returntool(
async(input,runtime)=>{
constnormalizedInput=(input??{})asRecord<string,unknown>;
returnapiBasedTool.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,
},
);
}