@yak-io/javascript 0.10.1 → 0.11.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.
@@ -0,0 +1,339 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.server.ts
21
+ var index_server_exports = {};
22
+ __export(index_server_exports, {
23
+ createYakConfigHandler: () => createYakConfigHandler,
24
+ createYakHandler: () => createYakHandler,
25
+ createYakToolsHandler: () => createYakToolsHandler,
26
+ normalizeRouteSources: () => normalizeRouteSources,
27
+ normalizeToolSources: () => normalizeToolSources
28
+ });
29
+ module.exports = __toCommonJS(index_server_exports);
30
+
31
+ // src/server/sources.ts
32
+ function isRouteSource(value) {
33
+ return Boolean(
34
+ value && typeof value === "object" && "getRoutes" in value && typeof value.getRoutes === "function"
35
+ );
36
+ }
37
+ function isRouteInfoArray(value) {
38
+ return Array.isArray(value) && value.every((route) => route && typeof route.path === "string");
39
+ }
40
+ function normalizeRouteSourceItem(item, index) {
41
+ if (isRouteSource(item)) {
42
+ return {
43
+ id: item.id ?? `route-source-${index}`,
44
+ getRoutes: item.getRoutes
45
+ };
46
+ }
47
+ if (typeof item === "function") {
48
+ return {
49
+ id: `route-fn-${index}`,
50
+ getRoutes: item
51
+ };
52
+ }
53
+ if (isRouteInfoArray(item)) {
54
+ return {
55
+ id: `route-static-${index}`,
56
+ getRoutes: async () => item
57
+ };
58
+ }
59
+ throw new Error("Unsupported route source input");
60
+ }
61
+ function normalizeRouteSources(input) {
62
+ if (Array.isArray(input)) {
63
+ if (input.length === 0) {
64
+ return [];
65
+ }
66
+ if (isRouteInfoArray(input)) {
67
+ return [normalizeRouteSourceItem(input, 0)];
68
+ }
69
+ return input.map(
70
+ (item, index) => normalizeRouteSourceItem(item, index)
71
+ );
72
+ }
73
+ return [normalizeRouteSourceItem(input, 0)];
74
+ }
75
+ function isToolSource(value) {
76
+ return Boolean(
77
+ value && typeof value === "object" && "getTools" in value && typeof value.getTools === "function"
78
+ );
79
+ }
80
+ function isToolManifest(value) {
81
+ return Boolean(value && typeof value === "object" && "tools" in value);
82
+ }
83
+ function isToolDefinitionArray(value) {
84
+ return Array.isArray(value) && value.every((tool) => tool && typeof tool.name === "string");
85
+ }
86
+ function toToolDefinitions(result) {
87
+ if (isToolDefinitionArray(result)) {
88
+ return result;
89
+ }
90
+ if (isToolManifest(result)) {
91
+ return result.tools;
92
+ }
93
+ throw new Error("Tool source must resolve to ToolDefinition[] or ToolManifest");
94
+ }
95
+ function normalizeToolSourceItem(item, index) {
96
+ if (isToolSource(item)) {
97
+ return {
98
+ id: item.id ?? `tool-source-${index}`,
99
+ getTools: async () => {
100
+ const result = await item.getTools();
101
+ return toToolDefinitions(result);
102
+ },
103
+ executeTool: item.executeTool
104
+ };
105
+ }
106
+ if (typeof item === "function") {
107
+ return {
108
+ id: `tool-fn-${index}`,
109
+ getTools: async () => {
110
+ const result = await item();
111
+ return toToolDefinitions(result);
112
+ }
113
+ };
114
+ }
115
+ if (isToolDefinitionArray(item)) {
116
+ return {
117
+ id: `tool-static-${index}`,
118
+ getTools: async () => item
119
+ };
120
+ }
121
+ if (isToolManifest(item)) {
122
+ return {
123
+ id: `tool-manifest-${index}`,
124
+ getTools: async () => item.tools
125
+ };
126
+ }
127
+ if (item && typeof item === "object" && "getTools" in item && typeof item.getTools === "function") {
128
+ return {
129
+ id: item.id ?? `tool-wrapper-${index}`,
130
+ getTools: async () => {
131
+ const result = await item.getTools();
132
+ return toToolDefinitions(result);
133
+ },
134
+ executeTool: item.executeTool
135
+ };
136
+ }
137
+ throw new Error("Unsupported tool source input");
138
+ }
139
+ function normalizeToolSources(input) {
140
+ if (!input) {
141
+ return [];
142
+ }
143
+ if (Array.isArray(input)) {
144
+ if (input.length === 0) {
145
+ return [];
146
+ }
147
+ if (isToolDefinitionArray(input)) {
148
+ return [normalizeToolSourceItem(input, 0)];
149
+ }
150
+ return input.map(
151
+ (item, index) => normalizeToolSourceItem(item, index)
152
+ );
153
+ }
154
+ return [normalizeToolSourceItem(input, 0)];
155
+ }
156
+
157
+ // src/server/createYakHandler.ts
158
+ function createYakHandler(config) {
159
+ const routeSources = normalizeRouteSources(config.routes);
160
+ const toolSources = normalizeToolSources(config.tools);
161
+ const GET = async function handleConfig(_req) {
162
+ const chatConfig = {
163
+ routes: await buildRouteManifest(routeSources)
164
+ };
165
+ if (toolSources.length > 0) {
166
+ const { manifest } = await buildToolRegistry(toolSources);
167
+ chatConfig.tools = manifest;
168
+ }
169
+ return jsonResponse(chatConfig, 200, {
170
+ "Cache-Control": "no-store"
171
+ });
172
+ };
173
+ const POST = async function handleToolCall(req) {
174
+ if (toolSources.length === 0) {
175
+ return errorResponse("Tool execution is not configured", 501);
176
+ }
177
+ let payload;
178
+ try {
179
+ payload = await req.json();
180
+ } catch {
181
+ return errorResponse("Invalid JSON payload", 400);
182
+ }
183
+ if (!isHostToolCallPayload(payload)) {
184
+ return errorResponse("Invalid tool call payload", 400);
185
+ }
186
+ try {
187
+ const { lookup } = await buildToolRegistry(toolSources);
188
+ const owner = lookup.get(payload.name);
189
+ if (!owner) {
190
+ return createToolErrorResponse(`Tool '${payload.name}' is not registered`);
191
+ }
192
+ if (!owner.executeTool) {
193
+ return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);
194
+ }
195
+ const result = await owner.executeTool(payload.name, payload.args, req);
196
+ const successResult = {
197
+ ok: true,
198
+ result
199
+ };
200
+ return jsonResponse(successResult);
201
+ } catch (error) {
202
+ return createToolErrorResponse(extractErrorMessage(error));
203
+ }
204
+ };
205
+ return { GET, POST };
206
+ }
207
+ function createYakConfigHandler(config) {
208
+ const routeSources = normalizeRouteSources(config.routes);
209
+ const toolSources = normalizeToolSources(config.tools);
210
+ return async function handleConfig(_req) {
211
+ const chatConfig = {
212
+ routes: await buildRouteManifest(routeSources)
213
+ };
214
+ if (toolSources.length > 0) {
215
+ const { manifest } = await buildToolRegistry(toolSources);
216
+ chatConfig.tools = manifest;
217
+ }
218
+ return jsonResponse(chatConfig, 200, {
219
+ "Cache-Control": "no-store"
220
+ });
221
+ };
222
+ }
223
+ function createYakToolsHandler(config) {
224
+ const toolSources = normalizeToolSources(config.tools);
225
+ if (toolSources.length === 0) {
226
+ throw new Error("createYakToolsHandler requires at least one tool source");
227
+ }
228
+ return async function handleTools(req) {
229
+ let payload;
230
+ try {
231
+ payload = await req.json();
232
+ } catch {
233
+ return errorResponse("Invalid JSON payload", 400);
234
+ }
235
+ if (!isHostToolCallPayload(payload)) {
236
+ return errorResponse("Invalid tool call payload", 400);
237
+ }
238
+ try {
239
+ const { lookup } = await buildToolRegistry(toolSources);
240
+ const owner = lookup.get(payload.name);
241
+ if (!owner) {
242
+ return createToolErrorResponse(`Tool '${payload.name}' is not registered`);
243
+ }
244
+ if (!owner.executeTool) {
245
+ return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);
246
+ }
247
+ const result = await owner.executeTool(payload.name, payload.args, req);
248
+ const successResult = {
249
+ ok: true,
250
+ result
251
+ };
252
+ return jsonResponse(successResult);
253
+ } catch (error) {
254
+ return createToolErrorResponse(extractErrorMessage(error));
255
+ }
256
+ };
257
+ }
258
+ async function buildRouteManifest(routeSources) {
259
+ const entries = await Promise.all(
260
+ routeSources.map(async (source) => ({
261
+ id: source.id ?? "routes",
262
+ routes: await source.getRoutes()
263
+ }))
264
+ );
265
+ const merged = [];
266
+ const seen = /* @__PURE__ */ new Set();
267
+ for (const entry of entries) {
268
+ for (const route of entry.routes) {
269
+ const key = route.path;
270
+ if (!seen.has(key)) {
271
+ seen.add(key);
272
+ merged.push(route);
273
+ }
274
+ }
275
+ }
276
+ merged.sort((a, b) => a.path.localeCompare(b.path));
277
+ return {
278
+ routes: merged,
279
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
280
+ sources: entries.map((entry) => ({ id: entry.id, count: entry.routes.length }))
281
+ };
282
+ }
283
+ async function buildToolRegistry(toolSources) {
284
+ const entries = await Promise.all(
285
+ toolSources.map(async (source) => ({
286
+ id: source.id ?? "tools",
287
+ source,
288
+ tools: await source.getTools()
289
+ }))
290
+ );
291
+ const manifestTools = [];
292
+ const lookup = /* @__PURE__ */ new Map();
293
+ for (const entry of entries) {
294
+ for (const tool of entry.tools) {
295
+ manifestTools.push(tool);
296
+ lookup.set(tool.name, entry.source);
297
+ }
298
+ }
299
+ return {
300
+ manifest: {
301
+ tools: manifestTools,
302
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
303
+ sources: entries.map((entry) => ({ id: entry.id, count: entry.tools.length }))
304
+ },
305
+ lookup
306
+ };
307
+ }
308
+ function isHostToolCallPayload(payload) {
309
+ return typeof payload === "object" && payload !== null && typeof payload.name === "string" && "args" in payload;
310
+ }
311
+ function extractErrorMessage(error) {
312
+ if (error instanceof Error) {
313
+ return error.message;
314
+ }
315
+ if (typeof error === "string") {
316
+ return error;
317
+ }
318
+ return "An unknown error occurred";
319
+ }
320
+ function jsonResponse(body, status = 200, headers = {}) {
321
+ return new Response(JSON.stringify(body), {
322
+ status,
323
+ headers: {
324
+ "Content-Type": "application/json",
325
+ ...headers
326
+ }
327
+ });
328
+ }
329
+ function errorResponse(message, status) {
330
+ return jsonResponse({ error: message }, status);
331
+ }
332
+ function createToolErrorResponse(error) {
333
+ const errorResult = {
334
+ ok: false,
335
+ error
336
+ };
337
+ return jsonResponse(errorResult);
338
+ }
339
+ //# sourceMappingURL=index.server.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.server.ts", "../src/server/sources.ts", "../src/server/createYakHandler.ts"],
4
+ "sourcesContent": ["export * from \"./server/index.js\";\n", "import type { RouteInfo } from \"../types/routes.js\";\nimport type { ToolDefinition, ToolExecutor, ToolManifest } from \"../types/tools.js\";\n\nexport type RouteSource = {\n id?: string;\n getRoutes: () => Promise<RouteInfo[]>;\n};\n\ntype RouteSourceFn = () => Promise<RouteInfo[]>;\n\ntype RouteSourceInputItem = RouteSource | RouteSourceFn | RouteInfo[];\n\nexport type RouteSourceInput = RouteSourceInputItem | RouteSourceInputItem[];\n\nexport type ToolSource = {\n id?: string;\n getTools: () => Promise<ToolDefinition[]>;\n executeTool?: ToolExecutor;\n};\n\ntype ToolSourceFn = () => Promise<ToolDefinition[] | ToolManifest>;\n\ntype ToolSourceInputItem =\n | ToolSource\n | ToolSourceFn\n | ToolDefinition[]\n | ToolManifest\n | {\n getTools: ToolSourceFn;\n executeTool?: ToolExecutor;\n };\n\nexport type ToolSourceInput = ToolSourceInputItem | ToolSourceInputItem[];\n\nfunction isRouteSource(value: unknown): value is RouteSource {\n return Boolean(\n value &&\n typeof value === \"object\" &&\n \"getRoutes\" in value &&\n typeof (value as RouteSource).getRoutes === \"function\"\n );\n}\n\nfunction isRouteInfoArray(value: unknown): value is RouteInfo[] {\n return Array.isArray(value) && value.every((route) => route && typeof route.path === \"string\");\n}\n\nfunction normalizeRouteSourceItem(item: RouteSourceInputItem, index: number): RouteSource {\n if (isRouteSource(item)) {\n return {\n id: item.id ?? `route-source-${index}`,\n getRoutes: item.getRoutes,\n };\n }\n\n if (typeof item === \"function\") {\n return {\n id: `route-fn-${index}`,\n getRoutes: item,\n };\n }\n\n if (isRouteInfoArray(item)) {\n return {\n id: `route-static-${index}`,\n getRoutes: async () => item,\n };\n }\n\n throw new Error(\"Unsupported route source input\");\n}\n\nexport function normalizeRouteSources(input: RouteSourceInput): RouteSource[] {\n if (Array.isArray(input)) {\n if (input.length === 0) {\n return [];\n }\n if (isRouteInfoArray(input)) {\n return [normalizeRouteSourceItem(input, 0)];\n }\n return (input as RouteSourceInputItem[]).map((item, index) =>\n normalizeRouteSourceItem(item, index)\n );\n }\n\n return [normalizeRouteSourceItem(input, 0)];\n}\n\nfunction isToolSource(value: unknown): value is ToolSource {\n return Boolean(\n value &&\n typeof value === \"object\" &&\n \"getTools\" in value &&\n typeof (value as ToolSource).getTools === \"function\"\n );\n}\n\nfunction isToolManifest(value: unknown): value is ToolManifest {\n return Boolean(value && typeof value === \"object\" && \"tools\" in value);\n}\n\nfunction isToolDefinitionArray(value: unknown): value is ToolDefinition[] {\n return Array.isArray(value) && value.every((tool) => tool && typeof tool.name === \"string\");\n}\n\nfunction toToolDefinitions(result: unknown): ToolDefinition[] {\n if (isToolDefinitionArray(result)) {\n return result;\n }\n if (isToolManifest(result)) {\n return result.tools;\n }\n throw new Error(\"Tool source must resolve to ToolDefinition[] or ToolManifest\");\n}\n\nfunction normalizeToolSourceItem(item: ToolSourceInputItem, index: number): ToolSource {\n if (isToolSource(item)) {\n return {\n id: item.id ?? `tool-source-${index}`,\n getTools: async () => {\n const result = await item.getTools();\n return toToolDefinitions(result);\n },\n executeTool: item.executeTool,\n };\n }\n\n if (typeof item === \"function\") {\n return {\n id: `tool-fn-${index}`,\n getTools: async () => {\n const result = await item();\n return toToolDefinitions(result);\n },\n };\n }\n\n if (isToolDefinitionArray(item)) {\n return {\n id: `tool-static-${index}`,\n getTools: async () => item,\n };\n }\n\n if (isToolManifest(item)) {\n return {\n id: `tool-manifest-${index}`,\n getTools: async () => item.tools,\n };\n }\n\n if (\n item &&\n typeof item === \"object\" &&\n \"getTools\" in item &&\n typeof item.getTools === \"function\"\n ) {\n return {\n id: (item as { id?: string }).id ?? `tool-wrapper-${index}`,\n getTools: async () => {\n const result = await item.getTools();\n return toToolDefinitions(result);\n },\n executeTool: item.executeTool,\n };\n }\n\n throw new Error(\"Unsupported tool source input\");\n}\n\nexport function normalizeToolSources(input?: ToolSourceInput): ToolSource[] {\n if (!input) {\n return [];\n }\n if (Array.isArray(input)) {\n if (input.length === 0) {\n return [];\n }\n if (isToolDefinitionArray(input)) {\n return [normalizeToolSourceItem(input, 0)];\n }\n return (input as ToolSourceInputItem[]).map((item, index) =>\n normalizeToolSourceItem(item, index)\n );\n }\n\n return [normalizeToolSourceItem(input, 0)];\n}\n", "import type { ChatConfig } from \"../types/config.js\";\nimport type { RouteInfo, RouteManifest } from \"../types/routes.js\";\nimport type { ToolCallResult, ToolDefinition, ToolManifest } from \"../types/tools.js\";\nimport type { RouteSource, RouteSourceInput, ToolSource, ToolSourceInput } from \"./sources.js\";\nimport { normalizeRouteSources, normalizeToolSources } from \"./sources.js\";\n\nexport type YakHandlerConfig = {\n routes: RouteSourceInput;\n tools?: ToolSourceInput;\n};\n\nexport function createYakHandler(config: YakHandlerConfig) {\n const routeSources = normalizeRouteSources(config.routes);\n const toolSources = normalizeToolSources(config.tools);\n\n const GET = async function handleConfig(_req: Request): Promise<Response> {\n const chatConfig: ChatConfig = {\n routes: await buildRouteManifest(routeSources),\n };\n\n if (toolSources.length > 0) {\n const { manifest } = await buildToolRegistry(toolSources);\n chatConfig.tools = manifest;\n }\n\n return jsonResponse(chatConfig, 200, {\n \"Cache-Control\": \"no-store\",\n });\n };\n\n const POST = async function handleToolCall(req: Request): Promise<Response> {\n if (toolSources.length === 0) {\n return errorResponse(\"Tool execution is not configured\", 501);\n }\n\n let payload: unknown;\n\n try {\n payload = await req.json();\n } catch {\n return errorResponse(\"Invalid JSON payload\", 400);\n }\n\n if (!isHostToolCallPayload(payload)) {\n return errorResponse(\"Invalid tool call payload\", 400);\n }\n\n try {\n const { lookup } = await buildToolRegistry(toolSources);\n const owner = lookup.get(payload.name);\n\n if (!owner) {\n return createToolErrorResponse(`Tool '${payload.name}' is not registered`);\n }\n\n if (!owner.executeTool) {\n return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);\n }\n\n const result = await owner.executeTool(payload.name, payload.args, req);\n\n const successResult: ToolCallResult = {\n ok: true,\n result,\n };\n\n return jsonResponse(successResult);\n } catch (error) {\n return createToolErrorResponse(extractErrorMessage(error));\n }\n };\n\n return { GET, POST };\n}\n\nexport type YakConfigHandlerConfig = {\n routes: RouteSourceInput;\n tools?: ToolSourceInput;\n};\n\nexport function createYakConfigHandler(config: YakConfigHandlerConfig) {\n const routeSources = normalizeRouteSources(config.routes);\n const toolSources = normalizeToolSources(config.tools);\n\n return async function handleConfig(_req: Request): Promise<Response> {\n const chatConfig: ChatConfig = {\n routes: await buildRouteManifest(routeSources),\n };\n\n if (toolSources.length > 0) {\n const { manifest } = await buildToolRegistry(toolSources);\n chatConfig.tools = manifest;\n }\n\n return jsonResponse(chatConfig, 200, {\n \"Cache-Control\": \"no-store\",\n });\n };\n}\n\nexport type YakToolsHandlerConfig = {\n tools: ToolSourceInput;\n};\n\nexport function createYakToolsHandler(config: YakToolsHandlerConfig) {\n const toolSources = normalizeToolSources(config.tools);\n\n if (toolSources.length === 0) {\n throw new Error(\"createYakToolsHandler requires at least one tool source\");\n }\n\n return async function handleTools(req: Request): Promise<Response> {\n let payload: unknown;\n\n try {\n payload = await req.json();\n } catch {\n return errorResponse(\"Invalid JSON payload\", 400);\n }\n\n if (!isHostToolCallPayload(payload)) {\n return errorResponse(\"Invalid tool call payload\", 400);\n }\n\n try {\n const { lookup } = await buildToolRegistry(toolSources);\n const owner = lookup.get(payload.name);\n\n if (!owner) {\n return createToolErrorResponse(`Tool '${payload.name}' is not registered`);\n }\n\n if (!owner.executeTool) {\n return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);\n }\n\n const result = await owner.executeTool(payload.name, payload.args, req);\n const successResult: ToolCallResult = {\n ok: true,\n result,\n };\n\n return jsonResponse(successResult);\n } catch (error) {\n return createToolErrorResponse(extractErrorMessage(error));\n }\n };\n}\n\nasync function buildRouteManifest(routeSources: RouteSource[]): Promise<RouteManifest> {\n const entries = await Promise.all(\n routeSources.map(async (source) => ({\n id: source.id ?? \"routes\",\n routes: await source.getRoutes(),\n }))\n );\n\n const merged: RouteInfo[] = [];\n const seen = new Set<string>();\n\n for (const entry of entries) {\n for (const route of entry.routes) {\n const key = route.path;\n if (!seen.has(key)) {\n seen.add(key);\n merged.push(route);\n }\n }\n }\n\n merged.sort((a, b) => a.path.localeCompare(b.path));\n\n return {\n routes: merged,\n generated_at: new Date().toISOString(),\n sources: entries.map((entry) => ({ id: entry.id, count: entry.routes.length })),\n };\n}\n\ntype ToolRegistry = {\n manifest: ToolManifest;\n lookup: Map<string, ToolSource>;\n};\n\nasync function buildToolRegistry(toolSources: ToolSource[]): Promise<ToolRegistry> {\n const entries = await Promise.all(\n toolSources.map(async (source) => ({\n id: source.id ?? \"tools\",\n source,\n tools: await source.getTools(),\n }))\n );\n\n const manifestTools: ToolDefinition[] = [];\n const lookup = new Map<string, ToolSource>();\n\n for (const entry of entries) {\n for (const tool of entry.tools) {\n manifestTools.push(tool);\n lookup.set(tool.name, entry.source);\n }\n }\n\n return {\n manifest: {\n tools: manifestTools,\n generated_at: new Date().toISOString(),\n sources: entries.map((entry) => ({ id: entry.id, count: entry.tools.length })),\n },\n lookup,\n };\n}\n\n/**\n * Payload received from the host (id is optional since YakClient handles correlation)\n */\ntype HostToolCallPayload = {\n name: string;\n args: unknown;\n};\n\nfunction isHostToolCallPayload(payload: unknown): payload is HostToolCallPayload {\n return (\n typeof payload === \"object\" &&\n payload !== null &&\n typeof (payload as HostToolCallPayload).name === \"string\" &&\n \"args\" in payload\n );\n}\n\nfunction extractErrorMessage(error: unknown): string {\n if (error instanceof Error) {\n return error.message;\n }\n if (typeof error === \"string\") {\n return error;\n }\n return \"An unknown error occurred\";\n}\n\nfunction jsonResponse(body: unknown, status = 200, headers: Record<string, string> = {}): Response {\n return new Response(JSON.stringify(body), {\n status,\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n },\n });\n}\n\nfunction errorResponse(message: string, status: number): Response {\n return jsonResponse({ error: message }, status);\n}\n\nfunction createToolErrorResponse(error: string): Response {\n const errorResult: ToolCallResult = {\n ok: false,\n error,\n };\n\n return jsonResponse(errorResult);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkCA,SAAS,cAAc,OAAsC;AAC3D,SAAO;AAAA,IACL,SACE,OAAO,UAAU,YACjB,eAAe,SACf,OAAQ,MAAsB,cAAc;AAAA,EAChD;AACF;AAEA,SAAS,iBAAiB,OAAsC;AAC9D,SAAO,MAAM,QAAQ,KAAK,KAAK,MAAM,MAAM,CAAC,UAAU,SAAS,OAAO,MAAM,SAAS,QAAQ;AAC/F;AAEA,SAAS,yBAAyB,MAA4B,OAA4B;AACxF,MAAI,cAAc,IAAI,GAAG;AACvB,WAAO;AAAA,MACL,IAAI,KAAK,MAAM,gBAAgB,KAAK;AAAA,MACpC,WAAW,KAAK;AAAA,IAClB;AAAA,EACF;AAEA,MAAI,OAAO,SAAS,YAAY;AAC9B,WAAO;AAAA,MACL,IAAI,YAAY,KAAK;AAAA,MACrB,WAAW;AAAA,IACb;AAAA,EACF;AAEA,MAAI,iBAAiB,IAAI,GAAG;AAC1B,WAAO;AAAA,MACL,IAAI,gBAAgB,KAAK;AAAA,MACzB,WAAW,YAAY;AAAA,IACzB;AAAA,EACF;AAEA,QAAM,IAAI,MAAM,gCAAgC;AAClD;AAEO,SAAS,sBAAsB,OAAwC;AAC5E,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,CAAC;AAAA,IACV;AACA,QAAI,iBAAiB,KAAK,GAAG;AAC3B,aAAO,CAAC,yBAAyB,OAAO,CAAC,CAAC;AAAA,IAC5C;AACA,WAAQ,MA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MAAI,iBAAiB,OAAO;AAC1B,WAAO,MAAM;AAAA,EACf;AACA,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAEA,SAAS,aAAa,MAAe,SAAS,KAAK,UAAkC,CAAC,GAAa;AACjG,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACxC;AAAA,IACA,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,GAAG;AAAA,IACL;AAAA,EACF,CAAC;AACH;AAEA,SAAS,cAAc,SAAiB,QAA0B;AAChE,SAAO,aAAa,EAAE,OAAO,QAAQ,GAAG,MAAM;AAChD;AAEA,SAAS,wBAAwB,OAAyB;AACxD,QAAM,cAA8B;AAAA,IAClC,IAAI;AAAA,IACJ;AAAA,EACF;AAEA,SAAO,aAAa,WAAW;AACjC;",
6
+ "names": []
7
+ }
@@ -1 +1,316 @@
1
- export * from "./server/index.js";
1
+ // src/server/sources.ts
2
+ function isRouteSource(value) {
3
+ return Boolean(
4
+ value && typeof value === "object" && "getRoutes" in value && typeof value.getRoutes === "function"
5
+ );
6
+ }
7
+ function isRouteInfoArray(value) {
8
+ return Array.isArray(value) && value.every((route) => route && typeof route.path === "string");
9
+ }
10
+ function normalizeRouteSourceItem(item, index) {
11
+ if (isRouteSource(item)) {
12
+ return {
13
+ id: item.id ?? `route-source-${index}`,
14
+ getRoutes: item.getRoutes
15
+ };
16
+ }
17
+ if (typeof item === "function") {
18
+ return {
19
+ id: `route-fn-${index}`,
20
+ getRoutes: item
21
+ };
22
+ }
23
+ if (isRouteInfoArray(item)) {
24
+ return {
25
+ id: `route-static-${index}`,
26
+ getRoutes: async () => item
27
+ };
28
+ }
29
+ throw new Error("Unsupported route source input");
30
+ }
31
+ function normalizeRouteSources(input) {
32
+ if (Array.isArray(input)) {
33
+ if (input.length === 0) {
34
+ return [];
35
+ }
36
+ if (isRouteInfoArray(input)) {
37
+ return [normalizeRouteSourceItem(input, 0)];
38
+ }
39
+ return input.map(
40
+ (item, index) => normalizeRouteSourceItem(item, index)
41
+ );
42
+ }
43
+ return [normalizeRouteSourceItem(input, 0)];
44
+ }
45
+ function isToolSource(value) {
46
+ return Boolean(
47
+ value && typeof value === "object" && "getTools" in value && typeof value.getTools === "function"
48
+ );
49
+ }
50
+ function isToolManifest(value) {
51
+ return Boolean(value && typeof value === "object" && "tools" in value);
52
+ }
53
+ function isToolDefinitionArray(value) {
54
+ return Array.isArray(value) && value.every((tool) => tool && typeof tool.name === "string");
55
+ }
56
+ function toToolDefinitions(result) {
57
+ if (isToolDefinitionArray(result)) {
58
+ return result;
59
+ }
60
+ if (isToolManifest(result)) {
61
+ return result.tools;
62
+ }
63
+ throw new Error("Tool source must resolve to ToolDefinition[] or ToolManifest");
64
+ }
65
+ function normalizeToolSourceItem(item, index) {
66
+ if (isToolSource(item)) {
67
+ return {
68
+ id: item.id ?? `tool-source-${index}`,
69
+ getTools: async () => {
70
+ const result = await item.getTools();
71
+ return toToolDefinitions(result);
72
+ },
73
+ executeTool: item.executeTool
74
+ };
75
+ }
76
+ if (typeof item === "function") {
77
+ return {
78
+ id: `tool-fn-${index}`,
79
+ getTools: async () => {
80
+ const result = await item();
81
+ return toToolDefinitions(result);
82
+ }
83
+ };
84
+ }
85
+ if (isToolDefinitionArray(item)) {
86
+ return {
87
+ id: `tool-static-${index}`,
88
+ getTools: async () => item
89
+ };
90
+ }
91
+ if (isToolManifest(item)) {
92
+ return {
93
+ id: `tool-manifest-${index}`,
94
+ getTools: async () => item.tools
95
+ };
96
+ }
97
+ if (item && typeof item === "object" && "getTools" in item && typeof item.getTools === "function") {
98
+ return {
99
+ id: item.id ?? `tool-wrapper-${index}`,
100
+ getTools: async () => {
101
+ const result = await item.getTools();
102
+ return toToolDefinitions(result);
103
+ },
104
+ executeTool: item.executeTool
105
+ };
106
+ }
107
+ throw new Error("Unsupported tool source input");
108
+ }
109
+ function normalizeToolSources(input) {
110
+ if (!input) {
111
+ return [];
112
+ }
113
+ if (Array.isArray(input)) {
114
+ if (input.length === 0) {
115
+ return [];
116
+ }
117
+ if (isToolDefinitionArray(input)) {
118
+ return [normalizeToolSourceItem(input, 0)];
119
+ }
120
+ return input.map(
121
+ (item, index) => normalizeToolSourceItem(item, index)
122
+ );
123
+ }
124
+ return [normalizeToolSourceItem(input, 0)];
125
+ }
126
+
127
+ // src/server/createYakHandler.ts
128
+ function createYakHandler(config) {
129
+ const routeSources = normalizeRouteSources(config.routes);
130
+ const toolSources = normalizeToolSources(config.tools);
131
+ const GET = async function handleConfig(_req) {
132
+ const chatConfig = {
133
+ routes: await buildRouteManifest(routeSources)
134
+ };
135
+ if (toolSources.length > 0) {
136
+ const { manifest } = await buildToolRegistry(toolSources);
137
+ chatConfig.tools = manifest;
138
+ }
139
+ return jsonResponse(chatConfig, 200, {
140
+ "Cache-Control": "no-store"
141
+ });
142
+ };
143
+ const POST = async function handleToolCall(req) {
144
+ if (toolSources.length === 0) {
145
+ return errorResponse("Tool execution is not configured", 501);
146
+ }
147
+ let payload;
148
+ try {
149
+ payload = await req.json();
150
+ } catch {
151
+ return errorResponse("Invalid JSON payload", 400);
152
+ }
153
+ if (!isHostToolCallPayload(payload)) {
154
+ return errorResponse("Invalid tool call payload", 400);
155
+ }
156
+ try {
157
+ const { lookup } = await buildToolRegistry(toolSources);
158
+ const owner = lookup.get(payload.name);
159
+ if (!owner) {
160
+ return createToolErrorResponse(`Tool '${payload.name}' is not registered`);
161
+ }
162
+ if (!owner.executeTool) {
163
+ return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);
164
+ }
165
+ const result = await owner.executeTool(payload.name, payload.args, req);
166
+ const successResult = {
167
+ ok: true,
168
+ result
169
+ };
170
+ return jsonResponse(successResult);
171
+ } catch (error) {
172
+ return createToolErrorResponse(extractErrorMessage(error));
173
+ }
174
+ };
175
+ return { GET, POST };
176
+ }
177
+ function createYakConfigHandler(config) {
178
+ const routeSources = normalizeRouteSources(config.routes);
179
+ const toolSources = normalizeToolSources(config.tools);
180
+ return async function handleConfig(_req) {
181
+ const chatConfig = {
182
+ routes: await buildRouteManifest(routeSources)
183
+ };
184
+ if (toolSources.length > 0) {
185
+ const { manifest } = await buildToolRegistry(toolSources);
186
+ chatConfig.tools = manifest;
187
+ }
188
+ return jsonResponse(chatConfig, 200, {
189
+ "Cache-Control": "no-store"
190
+ });
191
+ };
192
+ }
193
+ function createYakToolsHandler(config) {
194
+ const toolSources = normalizeToolSources(config.tools);
195
+ if (toolSources.length === 0) {
196
+ throw new Error("createYakToolsHandler requires at least one tool source");
197
+ }
198
+ return async function handleTools(req) {
199
+ let payload;
200
+ try {
201
+ payload = await req.json();
202
+ } catch {
203
+ return errorResponse("Invalid JSON payload", 400);
204
+ }
205
+ if (!isHostToolCallPayload(payload)) {
206
+ return errorResponse("Invalid tool call payload", 400);
207
+ }
208
+ try {
209
+ const { lookup } = await buildToolRegistry(toolSources);
210
+ const owner = lookup.get(payload.name);
211
+ if (!owner) {
212
+ return createToolErrorResponse(`Tool '${payload.name}' is not registered`);
213
+ }
214
+ if (!owner.executeTool) {
215
+ return createToolErrorResponse(`Tool '${payload.name}' does not expose an executor`);
216
+ }
217
+ const result = await owner.executeTool(payload.name, payload.args, req);
218
+ const successResult = {
219
+ ok: true,
220
+ result
221
+ };
222
+ return jsonResponse(successResult);
223
+ } catch (error) {
224
+ return createToolErrorResponse(extractErrorMessage(error));
225
+ }
226
+ };
227
+ }
228
+ async function buildRouteManifest(routeSources) {
229
+ const entries = await Promise.all(
230
+ routeSources.map(async (source) => ({
231
+ id: source.id ?? "routes",
232
+ routes: await source.getRoutes()
233
+ }))
234
+ );
235
+ const merged = [];
236
+ const seen = /* @__PURE__ */ new Set();
237
+ for (const entry of entries) {
238
+ for (const route of entry.routes) {
239
+ const key = route.path;
240
+ if (!seen.has(key)) {
241
+ seen.add(key);
242
+ merged.push(route);
243
+ }
244
+ }
245
+ }
246
+ merged.sort((a, b) => a.path.localeCompare(b.path));
247
+ return {
248
+ routes: merged,
249
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
250
+ sources: entries.map((entry) => ({ id: entry.id, count: entry.routes.length }))
251
+ };
252
+ }
253
+ async function buildToolRegistry(toolSources) {
254
+ const entries = await Promise.all(
255
+ toolSources.map(async (source) => ({
256
+ id: source.id ?? "tools",
257
+ source,
258
+ tools: await source.getTools()
259
+ }))
260
+ );
261
+ const manifestTools = [];
262
+ const lookup = /* @__PURE__ */ new Map();
263
+ for (const entry of entries) {
264
+ for (const tool of entry.tools) {
265
+ manifestTools.push(tool);
266
+ lookup.set(tool.name, entry.source);
267
+ }
268
+ }
269
+ return {
270
+ manifest: {
271
+ tools: manifestTools,
272
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
273
+ sources: entries.map((entry) => ({ id: entry.id, count: entry.tools.length }))
274
+ },
275
+ lookup
276
+ };
277
+ }
278
+ function isHostToolCallPayload(payload) {
279
+ return typeof payload === "object" && payload !== null && typeof payload.name === "string" && "args" in payload;
280
+ }
281
+ function extractErrorMessage(error) {
282
+ if (error instanceof Error) {
283
+ return error.message;
284
+ }
285
+ if (typeof error === "string") {
286
+ return error;
287
+ }
288
+ return "An unknown error occurred";
289
+ }
290
+ function jsonResponse(body, status = 200, headers = {}) {
291
+ return new Response(JSON.stringify(body), {
292
+ status,
293
+ headers: {
294
+ "Content-Type": "application/json",
295
+ ...headers
296
+ }
297
+ });
298
+ }
299
+ function errorResponse(message, status) {
300
+ return jsonResponse({ error: message }, status);
301
+ }
302
+ function createToolErrorResponse(error) {
303
+ const errorResult = {
304
+ ok: false,
305
+ error
306
+ };
307
+ return jsonResponse(errorResult);
308
+ }
309
+ export {
310
+ createYakConfigHandler,
311
+ createYakHandler,
312
+ createYakToolsHandler,
313
+ normalizeRouteSources,
314
+ normalizeToolSources
315
+ };
316
+ //# sourceMappingURL=index.server.js.map