@yak-io/javascript 0.1.0 → 0.2.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/dist/client.d.ts +15 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +5 -1
- package/dist/types/tools.d.ts +21 -0
- package/dist/types/tools.d.ts.map +1 -1
- package/package.json +19 -5
- package/dist/schema-parser.d.ts +0 -21
- package/dist/schema-parser.d.ts.map +0 -1
- package/dist/schema-parser.js +0 -341
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ChatConfig } from "./types/config.js";
|
|
2
2
|
import type { Theme } from "./types/messaging.js";
|
|
3
|
-
import type { ToolCallHandler, GraphQLSchemaHandler, RESTSchemaHandler } from "./types/tools.js";
|
|
3
|
+
import type { ToolCallHandler, ToolCallEventListener, GraphQLSchemaHandler, RESTSchemaHandler } from "./types/tools.js";
|
|
4
4
|
declare global {
|
|
5
5
|
interface Window {
|
|
6
6
|
__YAK_INTERNAL_DEV__?: boolean;
|
|
@@ -84,6 +84,20 @@ export interface YakClientConfig {
|
|
|
84
84
|
onRedirect?: (path: string) => void;
|
|
85
85
|
onClose?: () => void;
|
|
86
86
|
onReady?: () => void;
|
|
87
|
+
/**
|
|
88
|
+
* Called after every tool call completes (success or failure).
|
|
89
|
+
* Useful for page-level cache invalidation based on which tools were called.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* onToolCallComplete: (event) => {
|
|
94
|
+
* if (event.ok && event.name.startsWith("order.")) {
|
|
95
|
+
* queryClient.invalidateQueries({ queryKey: ["orders"] });
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
onToolCallComplete?: ToolCallEventListener;
|
|
87
101
|
/** Chat configuration options */
|
|
88
102
|
options?: {
|
|
89
103
|
/** Disable the restart session button in the header */
|
package/dist/client.d.ts.map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AO,IAAI,OAAO;IAIlB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOrC,aAAa,CAAC,MAAM,EAAE,OAAO;IAa7B,KAAK;IAOL,OAAO;IAQd,OAAO,CAAC,cAAc;IA+BtB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc,CAGrB;IAED,OAAO,CAAC,aAAa,CA+GpB;IAED,OAAO,CAAC,kBAAkB;IA2B1B,OAAO,CAAC,eAAe;YAoBT,cAAc;YA2Bd,uBAAuB;YAuBvB,oBAAoB;IAuBlC,OAAO,CAAC,sBAAsB;IAuB9B;;;OAGG;IACH,OAAO,CAAC,cAAc;IAatB,OAAO,CAAC,mBAAmB;IAU3B;;;OAGG;IACH,OAAO,CAAC,iBAAiB;CAsB1B"}
|
package/dist/client.js
CHANGED
|
@@ -381,16 +381,20 @@ export class YakClient {
|
|
|
381
381
|
if (!this.config.onToolCall) {
|
|
382
382
|
logger.error("Tool call received but no onToolCall handler configured");
|
|
383
383
|
this.sendToolResultToIframe(id, false, undefined, "No tool call handler configured");
|
|
384
|
+
this.config.onToolCallComplete?.({ name, args, ok: false, error: "No tool call handler configured" });
|
|
384
385
|
return;
|
|
385
386
|
}
|
|
386
387
|
try {
|
|
387
388
|
const result = await this.config.onToolCall(name, args);
|
|
388
389
|
logger.debug(`Tool call succeeded: ${name}`, { id });
|
|
389
390
|
this.sendToolResultToIframe(id, true, result);
|
|
391
|
+
this.config.onToolCallComplete?.({ name, args, ok: true, result });
|
|
390
392
|
}
|
|
391
393
|
catch (error) {
|
|
394
|
+
const errorMessage = this.extractErrorMessage(error);
|
|
392
395
|
logger.error(`Tool call failed: ${name}`, { id, error });
|
|
393
|
-
this.sendToolResultToIframe(id, false, undefined,
|
|
396
|
+
this.sendToolResultToIframe(id, false, undefined, errorMessage);
|
|
397
|
+
this.config.onToolCallComplete?.({ name, args, ok: false, error: errorMessage });
|
|
394
398
|
}
|
|
395
399
|
}
|
|
396
400
|
async handleGraphQLSchemaCall(id, schemaName, request) {
|
package/dist/types/tools.d.ts
CHANGED
|
@@ -134,4 +134,25 @@ export type ToolExecutor = (name: string, args: unknown, req: Request) => Promis
|
|
|
134
134
|
* @throws Error if the tool call fails
|
|
135
135
|
*/
|
|
136
136
|
export type ToolCallHandler = (name: string, args: unknown) => Promise<unknown>;
|
|
137
|
+
/**
|
|
138
|
+
* Event emitted after a tool call completes (success or failure).
|
|
139
|
+
* Useful for page-level cache invalidation.
|
|
140
|
+
*/
|
|
141
|
+
export type ToolCallEvent = {
|
|
142
|
+
/** The tool name that was called */
|
|
143
|
+
name: string;
|
|
144
|
+
/** The arguments passed to the tool */
|
|
145
|
+
args: unknown;
|
|
146
|
+
/** Whether the call succeeded */
|
|
147
|
+
ok: boolean;
|
|
148
|
+
/** The result (if ok is true) */
|
|
149
|
+
result?: unknown;
|
|
150
|
+
/** The error message (if ok is false) */
|
|
151
|
+
error?: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Listener for tool call completion events.
|
|
155
|
+
* Called after every tool call completes, regardless of success/failure.
|
|
156
|
+
*/
|
|
157
|
+
export type ToolCallEventListener = (event: ToolCallEvent) => void;
|
|
137
158
|
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/types/tools.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,SAAS,CAAC;IAChB,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,S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AE,MAAM,EAAE,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhF;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,uCAAuC;IACvC,IAAI,EAAE,OAAO,CAAC;IACd,iCAAiC;IACjC,EAAE,EAAE,OAAO,CAAC;IACZ,iCAAiC;IACjC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yCAAyC;IACzC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yak-io/javascript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core JavaScript SDK for embedding yak chatbot",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
"directory": "packages/javascript"
|
|
12
12
|
},
|
|
13
13
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
15
|
-
"provenance": false
|
|
14
|
+
"access": "public"
|
|
16
15
|
},
|
|
17
16
|
"keywords": [
|
|
18
17
|
"yak",
|
|
@@ -30,17 +29,32 @@
|
|
|
30
29
|
"LICENSE"
|
|
31
30
|
],
|
|
32
31
|
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
33
35
|
"exports": {
|
|
34
36
|
".": {
|
|
35
37
|
"types": "./dist/index.d.ts",
|
|
36
|
-
"import": "./dist/index.js"
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"default": "./dist/index.js"
|
|
37
40
|
},
|
|
38
41
|
"./server": {
|
|
39
42
|
"types": "./dist/index.server.d.ts",
|
|
40
|
-
"import": "./dist/index.server.js"
|
|
43
|
+
"import": "./dist/index.server.js",
|
|
44
|
+
"default": "./dist/index.server.js"
|
|
41
45
|
},
|
|
42
46
|
"./package.json": "./package.json"
|
|
43
47
|
},
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
".": [
|
|
51
|
+
"./dist/index.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"server": [
|
|
54
|
+
"./dist/index.server.d.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
44
58
|
"devDependencies": {
|
|
45
59
|
"@types/node": "^24.10.2",
|
|
46
60
|
"typescript": "^5.3.0",
|
package/dist/schema-parser.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client-side Schema Parser
|
|
3
|
-
*
|
|
4
|
-
* Parses GraphQL schemas and OpenAPI specs into ToolDefinitions.
|
|
5
|
-
* This is a simplified version suitable for browser environments.
|
|
6
|
-
*/
|
|
7
|
-
import type { ToolDefinition, SchemaSource, ToolManifest } from "./types/tools.js";
|
|
8
|
-
/**
|
|
9
|
-
* Parses schema sources into tool definitions
|
|
10
|
-
*/
|
|
11
|
-
export declare function parseSchemaSources(sources: SchemaSource[]): ToolDefinition[];
|
|
12
|
-
/**
|
|
13
|
-
* Expands a tool manifest by parsing any schema sources into tool definitions.
|
|
14
|
-
* Use this to pre-process the manifest before passing to YakProvider.
|
|
15
|
-
*/
|
|
16
|
-
export declare function expandToolManifest(manifest: ToolManifest): ToolManifest;
|
|
17
|
-
/**
|
|
18
|
-
* Builds an executable request from a tool definition and provided arguments.
|
|
19
|
-
*/
|
|
20
|
-
export declare function buildExecutableRequest(tool: ToolDefinition, args: Record<string, unknown>): import("./types/tools.js").ExecutableRequest | undefined;
|
|
21
|
-
//# sourceMappingURL=schema-parser.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schema-parser.d.ts","sourceRoot":"","sources":["../src/schema-parser.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,cAAc,EAGd,YAAY,EAEZ,YAAY,EACb,MAAM,kBAAkB,CAAC;AAyV1B;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,cAAc,EAAE,CAgB5E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,YAAY,GAAG,YAAY,CAavE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,OAAO,kBAAkB,EAAE,iBAAiB,GAAG,SAAS,CAuD1D"}
|
package/dist/schema-parser.js
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client-side Schema Parser
|
|
3
|
-
*
|
|
4
|
-
* Parses GraphQL schemas and OpenAPI specs into ToolDefinitions.
|
|
5
|
-
* This is a simplified version suitable for browser environments.
|
|
6
|
-
*/
|
|
7
|
-
function graphqlTypeToJsonSchema(type) {
|
|
8
|
-
const baseType = type.replace(/!/g, "").replace(/\[|\]/g, "").trim();
|
|
9
|
-
const isArray = type.includes("[");
|
|
10
|
-
let schema;
|
|
11
|
-
switch (baseType.toLowerCase()) {
|
|
12
|
-
case "string":
|
|
13
|
-
case "id":
|
|
14
|
-
schema = { type: "string" };
|
|
15
|
-
break;
|
|
16
|
-
case "int":
|
|
17
|
-
schema = { type: "integer" };
|
|
18
|
-
break;
|
|
19
|
-
case "float":
|
|
20
|
-
schema = { type: "number" };
|
|
21
|
-
break;
|
|
22
|
-
case "boolean":
|
|
23
|
-
schema = { type: "boolean" };
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
schema = { type: "object" };
|
|
27
|
-
}
|
|
28
|
-
if (isArray) {
|
|
29
|
-
schema = { type: "array", items: schema };
|
|
30
|
-
}
|
|
31
|
-
return schema;
|
|
32
|
-
}
|
|
33
|
-
function parseGraphQLSDL(sdl) {
|
|
34
|
-
const result = { queries: [], mutations: [] };
|
|
35
|
-
const fieldPattern = /(\w+)\s*(?:\(([^)]*)\))?\s*:\s*([^\n}]+)/g;
|
|
36
|
-
const argPattern = /(\w+)\s*:\s*([^,)]+)/g;
|
|
37
|
-
const queryMatch = sdl.match(/type\s+Query\s*\{([^}]+)\}/s);
|
|
38
|
-
if (queryMatch?.[1]) {
|
|
39
|
-
const queryBody = queryMatch[1];
|
|
40
|
-
let fieldMatch = null;
|
|
41
|
-
fieldPattern.lastIndex = 0;
|
|
42
|
-
while ((fieldMatch = fieldPattern.exec(queryBody)) !== null) {
|
|
43
|
-
const name = fieldMatch[1];
|
|
44
|
-
const argsStr = fieldMatch[2];
|
|
45
|
-
const returnType = fieldMatch[3];
|
|
46
|
-
if (!name || !returnType)
|
|
47
|
-
continue;
|
|
48
|
-
const args = [];
|
|
49
|
-
if (argsStr) {
|
|
50
|
-
let argMatch = null;
|
|
51
|
-
argPattern.lastIndex = 0;
|
|
52
|
-
while ((argMatch = argPattern.exec(argsStr)) !== null) {
|
|
53
|
-
const argName = argMatch[1];
|
|
54
|
-
const argType = argMatch[2];
|
|
55
|
-
if (!argName || !argType)
|
|
56
|
-
continue;
|
|
57
|
-
args.push({
|
|
58
|
-
name: argName,
|
|
59
|
-
type: argType.trim(),
|
|
60
|
-
required: argType.includes("!"),
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
result.queries.push({ name, type: returnType.trim(), args });
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const mutationMatch = sdl.match(/type\s+Mutation\s*\{([^}]+)\}/s);
|
|
68
|
-
if (mutationMatch?.[1]) {
|
|
69
|
-
const mutationBody = mutationMatch[1];
|
|
70
|
-
let fieldMatch = null;
|
|
71
|
-
fieldPattern.lastIndex = 0;
|
|
72
|
-
while ((fieldMatch = fieldPattern.exec(mutationBody)) !== null) {
|
|
73
|
-
const name = fieldMatch[1];
|
|
74
|
-
const argsStr = fieldMatch[2];
|
|
75
|
-
const returnType = fieldMatch[3];
|
|
76
|
-
if (!name || !returnType)
|
|
77
|
-
continue;
|
|
78
|
-
const args = [];
|
|
79
|
-
if (argsStr) {
|
|
80
|
-
let argMatch = null;
|
|
81
|
-
argPattern.lastIndex = 0;
|
|
82
|
-
while ((argMatch = argPattern.exec(argsStr)) !== null) {
|
|
83
|
-
const argName = argMatch[1];
|
|
84
|
-
const argType = argMatch[2];
|
|
85
|
-
if (!argName || !argType)
|
|
86
|
-
continue;
|
|
87
|
-
args.push({
|
|
88
|
-
name: argName,
|
|
89
|
-
type: argType.trim(),
|
|
90
|
-
required: argType.includes("!"),
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
result.mutations.push({ name, type: returnType.trim(), args });
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
98
|
-
}
|
|
99
|
-
function generateGraphQLOperation(field, operationType) {
|
|
100
|
-
const args = field.args.map((arg) => `$${arg.name}: ${arg.type}`).join(", ");
|
|
101
|
-
const variables = field.args.map((arg) => `${arg.name}: $${arg.name}`).join(", ");
|
|
102
|
-
const argsDecl = args ? `(${args})` : "";
|
|
103
|
-
const varsUsage = variables ? `(${variables})` : "";
|
|
104
|
-
return `${operationType}${argsDecl} { ${field.name}${varsUsage} }`;
|
|
105
|
-
}
|
|
106
|
-
function parseGraphQLSchema(source) {
|
|
107
|
-
const tools = [];
|
|
108
|
-
const parsed = parseGraphQLSDL(source.schema);
|
|
109
|
-
const includeSet = source.include ? new Set(source.include) : null;
|
|
110
|
-
const excludeSet = source.exclude ? new Set(source.exclude) : null;
|
|
111
|
-
const opTypes = source.operationTypes ?? ["query", "mutation"];
|
|
112
|
-
const processField = (field, operationType) => {
|
|
113
|
-
if (includeSet && !includeSet.has(field.name))
|
|
114
|
-
return;
|
|
115
|
-
if (excludeSet && excludeSet.has(field.name))
|
|
116
|
-
return;
|
|
117
|
-
const properties = {};
|
|
118
|
-
const required = [];
|
|
119
|
-
for (const arg of field.args) {
|
|
120
|
-
properties[arg.name] = {
|
|
121
|
-
...graphqlTypeToJsonSchema(arg.type),
|
|
122
|
-
description: arg.description,
|
|
123
|
-
};
|
|
124
|
-
if (arg.required) {
|
|
125
|
-
required.push(arg.name);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
const inputSchema = {
|
|
129
|
-
type: "object",
|
|
130
|
-
properties,
|
|
131
|
-
required: required.length > 0 ? required : undefined,
|
|
132
|
-
additionalProperties: false,
|
|
133
|
-
};
|
|
134
|
-
const operation = generateGraphQLOperation(field, operationType);
|
|
135
|
-
tools.push({
|
|
136
|
-
name: `graphql.${field.name}`,
|
|
137
|
-
displayName: field.name,
|
|
138
|
-
description: field.description ?? `${operationType === "query" ? "Query" : "Mutate"}: ${field.name}`,
|
|
139
|
-
inputSchema,
|
|
140
|
-
requestTemplate: {
|
|
141
|
-
type: "graphql",
|
|
142
|
-
endpoint: source.endpoint,
|
|
143
|
-
operation,
|
|
144
|
-
},
|
|
145
|
-
source: `graphql:${field.name}`,
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
if (opTypes.includes("query")) {
|
|
149
|
-
for (const query of parsed.queries) {
|
|
150
|
-
processField(query, "query");
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (opTypes.includes("mutation")) {
|
|
154
|
-
for (const mutation of parsed.mutations) {
|
|
155
|
-
processField(mutation, "mutation");
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return tools;
|
|
159
|
-
}
|
|
160
|
-
function parseOpenAPISpec(spec) {
|
|
161
|
-
if (typeof spec === "string") {
|
|
162
|
-
try {
|
|
163
|
-
return JSON.parse(spec);
|
|
164
|
-
}
|
|
165
|
-
catch {
|
|
166
|
-
console.warn("[yak] OpenAPI spec must be valid JSON");
|
|
167
|
-
return { paths: {} };
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return spec;
|
|
171
|
-
}
|
|
172
|
-
function parseOpenAPISchema(source) {
|
|
173
|
-
const tools = [];
|
|
174
|
-
const spec = parseOpenAPISpec(source.spec);
|
|
175
|
-
const baseUrl = source.baseUrl ?? spec.servers?.[0]?.url ?? "";
|
|
176
|
-
const includeSet = source.include ? new Set(source.include) : null;
|
|
177
|
-
const excludeSet = source.exclude ? new Set(source.exclude) : null;
|
|
178
|
-
const methods = source.methods ?? ["get", "post", "put", "patch", "delete"];
|
|
179
|
-
if (!spec.paths)
|
|
180
|
-
return tools;
|
|
181
|
-
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
182
|
-
for (const method of methods) {
|
|
183
|
-
const operation = pathItem[method];
|
|
184
|
-
if (!operation)
|
|
185
|
-
continue;
|
|
186
|
-
const operationId = operation.operationId ?? `${method}_${path.replace(/[^a-zA-Z0-9]/g, "_")}`;
|
|
187
|
-
if (includeSet && !includeSet.has(operationId))
|
|
188
|
-
continue;
|
|
189
|
-
if (excludeSet && excludeSet.has(operationId))
|
|
190
|
-
continue;
|
|
191
|
-
const properties = {};
|
|
192
|
-
const required = [];
|
|
193
|
-
const pathParams = {};
|
|
194
|
-
const queryParams = {};
|
|
195
|
-
if (operation.parameters) {
|
|
196
|
-
for (const param of operation.parameters) {
|
|
197
|
-
properties[param.name] = {
|
|
198
|
-
...(param.schema ?? { type: "string" }),
|
|
199
|
-
description: param.description,
|
|
200
|
-
};
|
|
201
|
-
if (param.required) {
|
|
202
|
-
required.push(param.name);
|
|
203
|
-
}
|
|
204
|
-
if (param.in === "path") {
|
|
205
|
-
pathParams[param.name] = param.name;
|
|
206
|
-
}
|
|
207
|
-
else if (param.in === "query") {
|
|
208
|
-
queryParams[param.name] = param.name;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
let bodyParams;
|
|
213
|
-
const bodySchema = operation.requestBody?.content?.["application/json"]?.schema;
|
|
214
|
-
if (bodySchema?.properties) {
|
|
215
|
-
bodyParams = {};
|
|
216
|
-
for (const [propName, propSchema] of Object.entries(bodySchema.properties)) {
|
|
217
|
-
properties[propName] = propSchema;
|
|
218
|
-
bodyParams[propName] = propName;
|
|
219
|
-
}
|
|
220
|
-
if (bodySchema.required) {
|
|
221
|
-
required.push(...bodySchema.required);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
const inputSchema = {
|
|
225
|
-
type: "object",
|
|
226
|
-
properties,
|
|
227
|
-
required: required.length > 0 ? required : undefined,
|
|
228
|
-
additionalProperties: false,
|
|
229
|
-
};
|
|
230
|
-
tools.push({
|
|
231
|
-
name: `api.${operationId}`,
|
|
232
|
-
displayName: operation.summary ?? operationId,
|
|
233
|
-
description: operation.description ?? operation.summary ?? `${method.toUpperCase()} ${path}`,
|
|
234
|
-
inputSchema,
|
|
235
|
-
requestTemplate: {
|
|
236
|
-
type: "rest",
|
|
237
|
-
url: `${baseUrl}${path}`,
|
|
238
|
-
method: method.toUpperCase(),
|
|
239
|
-
...(Object.keys(pathParams).length > 0 && { pathParams }),
|
|
240
|
-
...(Object.keys(queryParams).length > 0 && { queryParams }),
|
|
241
|
-
...(bodyParams && { bodyParams }),
|
|
242
|
-
},
|
|
243
|
-
source: `openapi:${operationId}`,
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return tools;
|
|
248
|
-
}
|
|
249
|
-
// =============================================================================
|
|
250
|
-
// Public API
|
|
251
|
-
// =============================================================================
|
|
252
|
-
/**
|
|
253
|
-
* Parses schema sources into tool definitions
|
|
254
|
-
*/
|
|
255
|
-
export function parseSchemaSources(sources) {
|
|
256
|
-
const tools = [];
|
|
257
|
-
for (const source of sources) {
|
|
258
|
-
try {
|
|
259
|
-
if (source.type === "graphql") {
|
|
260
|
-
tools.push(...parseGraphQLSchema(source));
|
|
261
|
-
}
|
|
262
|
-
else if (source.type === "openapi") {
|
|
263
|
-
tools.push(...parseOpenAPISchema(source));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
catch (error) {
|
|
267
|
-
console.error(`[yak] Failed to parse ${source.type} schema:`, error);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return tools;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Expands a tool manifest by parsing any schema sources into tool definitions.
|
|
274
|
-
* Use this to pre-process the manifest before passing to YakProvider.
|
|
275
|
-
*/
|
|
276
|
-
export function expandToolManifest(manifest) {
|
|
277
|
-
if (!manifest.schemaSources?.length) {
|
|
278
|
-
return manifest;
|
|
279
|
-
}
|
|
280
|
-
const expandedTools = parseSchemaSources(manifest.schemaSources);
|
|
281
|
-
return {
|
|
282
|
-
...manifest,
|
|
283
|
-
tools: [...manifest.tools, ...expandedTools],
|
|
284
|
-
// Clear schemaSources since they've been expanded
|
|
285
|
-
schemaSources: undefined,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Builds an executable request from a tool definition and provided arguments.
|
|
290
|
-
*/
|
|
291
|
-
export function buildExecutableRequest(tool, args) {
|
|
292
|
-
const template = tool.requestTemplate;
|
|
293
|
-
if (!template)
|
|
294
|
-
return undefined;
|
|
295
|
-
if (template.type === "graphql") {
|
|
296
|
-
return {
|
|
297
|
-
type: "graphql",
|
|
298
|
-
endpoint: template.endpoint,
|
|
299
|
-
query: template.operation,
|
|
300
|
-
variables: args,
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
// REST request
|
|
304
|
-
let url = template.url;
|
|
305
|
-
if (template.pathParams) {
|
|
306
|
-
for (const [param, argName] of Object.entries(template.pathParams)) {
|
|
307
|
-
const value = args[argName];
|
|
308
|
-
if (value !== undefined) {
|
|
309
|
-
url = url.replace(`{${param}}`, String(value));
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
if (template.queryParams) {
|
|
314
|
-
const queryEntries = [];
|
|
315
|
-
for (const [, argName] of Object.entries(template.queryParams)) {
|
|
316
|
-
const value = args[argName];
|
|
317
|
-
if (value !== undefined) {
|
|
318
|
-
queryEntries.push(`${encodeURIComponent(argName)}=${encodeURIComponent(String(value))}`);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (queryEntries.length > 0) {
|
|
322
|
-
url += (url.includes("?") ? "&" : "?") + queryEntries.join("&");
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
let body;
|
|
326
|
-
if (template.bodyParams) {
|
|
327
|
-
body = {};
|
|
328
|
-
for (const [, argName] of Object.entries(template.bodyParams)) {
|
|
329
|
-
if (args[argName] !== undefined) {
|
|
330
|
-
body[argName] = args[argName];
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return {
|
|
335
|
-
type: "rest",
|
|
336
|
-
url,
|
|
337
|
-
method: template.method,
|
|
338
|
-
headers: { "Content-Type": "application/json" },
|
|
339
|
-
body: body && Object.keys(body).length > 0 ? body : undefined,
|
|
340
|
-
};
|
|
341
|
-
}
|