@vertesia/tools-sdk 0.24.0-dev.202601221707

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.
Files changed (144) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +122 -0
  3. package/lib/cjs/InteractionCollection.js +164 -0
  4. package/lib/cjs/InteractionCollection.js.map +1 -0
  5. package/lib/cjs/SkillCollection.js +376 -0
  6. package/lib/cjs/SkillCollection.js.map +1 -0
  7. package/lib/cjs/ToolCollection.js +228 -0
  8. package/lib/cjs/ToolCollection.js.map +1 -0
  9. package/lib/cjs/ToolRegistry.js +111 -0
  10. package/lib/cjs/ToolRegistry.js.map +1 -0
  11. package/lib/cjs/auth.js +104 -0
  12. package/lib/cjs/auth.js.map +1 -0
  13. package/lib/cjs/build/validate.js +7 -0
  14. package/lib/cjs/build/validate.js.map +1 -0
  15. package/lib/cjs/copy-assets.js +84 -0
  16. package/lib/cjs/copy-assets.js.map +1 -0
  17. package/lib/cjs/index.js +31 -0
  18. package/lib/cjs/index.js.map +1 -0
  19. package/lib/cjs/package.json +3 -0
  20. package/lib/cjs/server/interactions.js +66 -0
  21. package/lib/cjs/server/interactions.js.map +1 -0
  22. package/lib/cjs/server/mcp.js +45 -0
  23. package/lib/cjs/server/mcp.js.map +1 -0
  24. package/lib/cjs/server/site.js +30 -0
  25. package/lib/cjs/server/site.js.map +1 -0
  26. package/lib/cjs/server/skills.js +114 -0
  27. package/lib/cjs/server/skills.js.map +1 -0
  28. package/lib/cjs/server/tools.js +104 -0
  29. package/lib/cjs/server/tools.js.map +1 -0
  30. package/lib/cjs/server/types.js +3 -0
  31. package/lib/cjs/server/types.js.map +1 -0
  32. package/lib/cjs/server/widgets.js +27 -0
  33. package/lib/cjs/server/widgets.js.map +1 -0
  34. package/lib/cjs/server.js +132 -0
  35. package/lib/cjs/server.js.map +1 -0
  36. package/lib/cjs/site/styles.js +621 -0
  37. package/lib/cjs/site/styles.js.map +1 -0
  38. package/lib/cjs/site/templates.js +968 -0
  39. package/lib/cjs/site/templates.js.map +1 -0
  40. package/lib/cjs/types.js +3 -0
  41. package/lib/cjs/types.js.map +1 -0
  42. package/lib/cjs/utils.js +31 -0
  43. package/lib/cjs/utils.js.map +1 -0
  44. package/lib/esm/InteractionCollection.js +125 -0
  45. package/lib/esm/InteractionCollection.js.map +1 -0
  46. package/lib/esm/SkillCollection.js +369 -0
  47. package/lib/esm/SkillCollection.js.map +1 -0
  48. package/lib/esm/ToolCollection.js +190 -0
  49. package/lib/esm/ToolCollection.js.map +1 -0
  50. package/lib/esm/ToolRegistry.js +106 -0
  51. package/lib/esm/ToolRegistry.js.map +1 -0
  52. package/lib/esm/auth.js +97 -0
  53. package/lib/esm/auth.js.map +1 -0
  54. package/lib/esm/build/validate.js +4 -0
  55. package/lib/esm/build/validate.js.map +1 -0
  56. package/lib/esm/copy-assets.js +81 -0
  57. package/lib/esm/copy-assets.js.map +1 -0
  58. package/lib/esm/index.js +11 -0
  59. package/lib/esm/index.js.map +1 -0
  60. package/lib/esm/server/interactions.js +63 -0
  61. package/lib/esm/server/interactions.js.map +1 -0
  62. package/lib/esm/server/mcp.js +42 -0
  63. package/lib/esm/server/mcp.js.map +1 -0
  64. package/lib/esm/server/site.js +27 -0
  65. package/lib/esm/server/site.js.map +1 -0
  66. package/lib/esm/server/skills.js +111 -0
  67. package/lib/esm/server/skills.js.map +1 -0
  68. package/lib/esm/server/tools.js +101 -0
  69. package/lib/esm/server/tools.js.map +1 -0
  70. package/lib/esm/server/types.js +2 -0
  71. package/lib/esm/server/types.js.map +1 -0
  72. package/lib/esm/server/widgets.js +24 -0
  73. package/lib/esm/server/widgets.js.map +1 -0
  74. package/lib/esm/server.js +128 -0
  75. package/lib/esm/server.js.map +1 -0
  76. package/lib/esm/site/styles.js +618 -0
  77. package/lib/esm/site/styles.js.map +1 -0
  78. package/lib/esm/site/templates.js +956 -0
  79. package/lib/esm/site/templates.js.map +1 -0
  80. package/lib/esm/types.js +2 -0
  81. package/lib/esm/types.js.map +1 -0
  82. package/lib/esm/utils.js +26 -0
  83. package/lib/esm/utils.js.map +1 -0
  84. package/lib/types/InteractionCollection.d.ts +48 -0
  85. package/lib/types/InteractionCollection.d.ts.map +1 -0
  86. package/lib/types/SkillCollection.d.ts +118 -0
  87. package/lib/types/SkillCollection.d.ts.map +1 -0
  88. package/lib/types/ToolCollection.d.ts +72 -0
  89. package/lib/types/ToolCollection.d.ts.map +1 -0
  90. package/lib/types/ToolRegistry.d.ts +41 -0
  91. package/lib/types/ToolRegistry.d.ts.map +1 -0
  92. package/lib/types/auth.d.ts +32 -0
  93. package/lib/types/auth.d.ts.map +1 -0
  94. package/lib/types/build/validate.d.ts +2 -0
  95. package/lib/types/build/validate.d.ts.map +1 -0
  96. package/lib/types/copy-assets.d.ts +14 -0
  97. package/lib/types/copy-assets.d.ts.map +1 -0
  98. package/lib/types/index.d.ts +11 -0
  99. package/lib/types/index.d.ts.map +1 -0
  100. package/lib/types/server/interactions.d.ts +4 -0
  101. package/lib/types/server/interactions.d.ts.map +1 -0
  102. package/lib/types/server/mcp.d.ts +4 -0
  103. package/lib/types/server/mcp.d.ts.map +1 -0
  104. package/lib/types/server/site.d.ts +4 -0
  105. package/lib/types/server/site.d.ts.map +1 -0
  106. package/lib/types/server/skills.d.ts +4 -0
  107. package/lib/types/server/skills.d.ts.map +1 -0
  108. package/lib/types/server/tools.d.ts +4 -0
  109. package/lib/types/server/tools.d.ts.map +1 -0
  110. package/lib/types/server/types.d.ts +62 -0
  111. package/lib/types/server/types.d.ts.map +1 -0
  112. package/lib/types/server/widgets.d.ts +9 -0
  113. package/lib/types/server/widgets.d.ts.map +1 -0
  114. package/lib/types/server.d.ts +27 -0
  115. package/lib/types/server.d.ts.map +1 -0
  116. package/lib/types/site/styles.d.ts +5 -0
  117. package/lib/types/site/styles.d.ts.map +1 -0
  118. package/lib/types/site/templates.d.ts +54 -0
  119. package/lib/types/site/templates.d.ts.map +1 -0
  120. package/lib/types/types.d.ts +280 -0
  121. package/lib/types/types.d.ts.map +1 -0
  122. package/lib/types/utils.d.ts +4 -0
  123. package/lib/types/utils.d.ts.map +1 -0
  124. package/package.json +58 -0
  125. package/src/InteractionCollection.ts +143 -0
  126. package/src/SkillCollection.ts +461 -0
  127. package/src/ToolCollection.ts +223 -0
  128. package/src/ToolRegistry.ts +135 -0
  129. package/src/auth.ts +123 -0
  130. package/src/build/validate.ts +3 -0
  131. package/src/copy-assets.ts +104 -0
  132. package/src/index.ts +12 -0
  133. package/src/server/interactions.ts +79 -0
  134. package/src/server/mcp.ts +51 -0
  135. package/src/server/site.ts +46 -0
  136. package/src/server/skills.ts +133 -0
  137. package/src/server/tools.ts +128 -0
  138. package/src/server/types.ts +65 -0
  139. package/src/server/widgets.ts +38 -0
  140. package/src/server.ts +160 -0
  141. package/src/site/styles.ts +617 -0
  142. package/src/site/templates.ts +994 -0
  143. package/src/types.ts +303 -0
  144. package/src/utils.ts +23 -0
@@ -0,0 +1,461 @@
1
+ import { ToolDefinition } from "@llumiverse/common";
2
+ import { existsSync, readdirSync, readFileSync, statSync } from "fs";
3
+ import { Context } from "hono";
4
+ import { HTTPException } from "hono/http-exception";
5
+ import { join } from "path";
6
+ import { ToolContext } from "./server/types.js";
7
+ import type {
8
+ CollectionProperties,
9
+ ICollection,
10
+ SkillContentType,
11
+ SkillDefinition,
12
+ SkillExecutionResult,
13
+ ToolCollectionDefinition,
14
+ ToolDefinitionWithDefault,
15
+ ToolExecutionPayload,
16
+ ToolExecutionResult,
17
+ } from "./types.js";
18
+ import { kebabCaseToTitle } from "./utils.js";
19
+
20
+ export interface SkillCollectionProperties extends CollectionProperties {
21
+ /**
22
+ * The skills in this collection
23
+ */
24
+ skills: SkillDefinition[];
25
+ }
26
+
27
+ /**
28
+ * Implements a skills collection endpoint.
29
+ * Skills provide contextual instructions to agents.
30
+ * They can be static (markdown) or dynamic (JST templates).
31
+ */
32
+ export class SkillCollection implements ICollection<SkillDefinition> {
33
+ /**
34
+ * A kebab case collection name
35
+ */
36
+ name: string;
37
+ /**
38
+ * Optional title for UI display
39
+ */
40
+ title?: string;
41
+ /**
42
+ * Optional icon for UI display
43
+ */
44
+ icon?: string;
45
+ /**
46
+ * A short description
47
+ */
48
+ description?: string;
49
+ /**
50
+ * The skills in this collection
51
+ */
52
+ private skills: Map<string, SkillDefinition>;
53
+
54
+ constructor({ name, title, icon, description, skills }: SkillCollectionProperties) {
55
+ this.name = name;
56
+ this.title = title || kebabCaseToTitle(name);
57
+ this.icon = icon;
58
+ this.description = description;
59
+ this.skills = new Map(skills.map(s => [s.name, s]));
60
+ }
61
+
62
+ [Symbol.iterator](): Iterator<SkillDefinition> {
63
+ return this.skills.values();
64
+ }
65
+
66
+ map<U>(callback: (skill: SkillDefinition, index: number) => U): U[] {
67
+ return Array.from(this.skills.values()).map(callback);
68
+ }
69
+
70
+ /**
71
+ * Get a skill by name
72
+ */
73
+ getSkill(name: string): SkillDefinition | undefined {
74
+ return this.skills.get(name);
75
+ }
76
+
77
+ /**
78
+ * Get all skill definitions
79
+ */
80
+ getSkillDefinitions(): SkillDefinition[] {
81
+ return Array.from(this.skills.values());
82
+ }
83
+
84
+ /**
85
+ * Get skills exposed as tool definitions.
86
+ * This allows skills to appear alongside regular tools.
87
+ * When called, they return rendered instructions.
88
+ * Includes related_tools for dynamic tool discovery.
89
+ */
90
+ getToolDefinitions(): ToolDefinitionWithDefault[] {
91
+ const defaultSchema: ToolDefinition['input_schema'] = {
92
+ type: 'object',
93
+ properties: {
94
+ context: {
95
+ type: "string",
96
+ description: "Additional context or specific requirements for this task"
97
+ }
98
+ }
99
+ };
100
+
101
+ return Array.from(this.skills.values()).map(skill => {
102
+ // Build description with related tools info if available
103
+ let description = `[Skill] ${skill.description}. Returns contextual instructions for this task.`;
104
+ if (skill.related_tools && skill.related_tools.length > 0) {
105
+ description += ` Unlocks tools: ${skill.related_tools.join(', ')}.`;
106
+ }
107
+
108
+ return {
109
+ name: `learn_${skill.name}`,
110
+ description,
111
+ input_schema: skill.input_schema || defaultSchema,
112
+ related_tools: skill.related_tools,
113
+ };
114
+ });
115
+ }
116
+
117
+ /**
118
+ * Get as a tool collection definition (for listing alongside tools)
119
+ */
120
+ getAsToolCollection(baseUrl: string): ToolCollectionDefinition {
121
+ return {
122
+ title: this.title || this.name,
123
+ description: this.description || `Skills: ${this.name}`,
124
+ src: `${baseUrl}/api/skills/${this.name}`,
125
+ tools: this.getToolDefinitions()
126
+ };
127
+ }
128
+
129
+ getWidgets(): {
130
+ name: string;
131
+ skill: string;
132
+ }[] {
133
+ const out: {
134
+ name: string;
135
+ skill: string;
136
+ }[] = [];
137
+ for (const skill of this.skills.values()) {
138
+ if (skill.widgets) {
139
+ for (const widget of skill.widgets) {
140
+ out.push({
141
+ name: widget,
142
+ skill: skill.name,
143
+ });
144
+ }
145
+ }
146
+ }
147
+ return Array.from(out);
148
+ }
149
+
150
+ /**
151
+ * Execute a skill - accepts standard tool execution payload.
152
+ * Returns rendered instructions in tool result format.
153
+ *
154
+ * @param ctx - Hono context
155
+ * @param preParsedPayload - Optional pre-parsed payload (used when routing from root endpoint)
156
+ */
157
+ async execute(ctx: Context, preParsedPayload?: ToolExecutionPayload<Record<string, any>>): Promise<Response> {
158
+ const toolCtx = ctx as ToolContext;
159
+ let payload: ToolExecutionPayload<Record<string, any>> | undefined = preParsedPayload;
160
+ try {
161
+ if (!payload) {
162
+ // Check if body was already parsed and validated by middleware
163
+ if (toolCtx.payload) {
164
+ payload = toolCtx.payload;
165
+ } else {
166
+ throw new HTTPException(400, {
167
+ message: 'Invalid or missing skill execution payload. Expected { tool_use: { id, tool_name, tool_input? }, metadata? }'
168
+ });
169
+ }
170
+ }
171
+
172
+ const toolName = payload.tool_use.tool_name;
173
+
174
+ // Extract skill name from tool name (remove "learn_" prefix if present)
175
+ const skillName = toolName.startsWith('learn_')
176
+ ? toolName.replace('learn_', '')
177
+ : toolName;
178
+
179
+ const skill = this.skills.get(skillName);
180
+
181
+ if (!skill) {
182
+ console.warn("[SkillCollection] Skill not found", {
183
+ collection: this.name,
184
+ requestedSkill: skillName,
185
+ toolName,
186
+ availableSkills: Array.from(this.skills.keys()),
187
+ });
188
+ throw new HTTPException(404, {
189
+ message: `Skill not found: ${skillName}`
190
+ });
191
+ }
192
+
193
+ const data = payload.tool_use.tool_input || {};
194
+ const result = await this.renderSkill(skill, data);
195
+
196
+ // TODO: If skill.execution is set, run via Daytona
197
+
198
+ // Return in tool result format
199
+ return ctx.json({
200
+ tool_use_id: payload.tool_use.id,
201
+ is_error: false,
202
+ content: result.instructions,
203
+ meta: {
204
+ skill_name: skill.name,
205
+ content_type: skill.content_type,
206
+ execution: skill.execution,
207
+ }
208
+ } satisfies ToolExecutionResult & { tool_use_id: string });
209
+ } catch (err: any) {
210
+ const status = err.status || 500;
211
+ const toolName = payload?.tool_use?.tool_name;
212
+ const toolUseId = payload?.tool_use?.id;
213
+
214
+ if (status >= 500) {
215
+ console.error("[SkillCollection] Skill execution failed", {
216
+ collection: this.name,
217
+ skill: toolName,
218
+ toolUseId,
219
+ error: err.message,
220
+ status,
221
+ toolInput: payload?.tool_use?.tool_input,
222
+ stack: err.stack,
223
+ });
224
+ }
225
+
226
+ return ctx.json({
227
+ tool_use_id: toolUseId || "unknown",
228
+ is_error: true,
229
+ content: err.message || "Error executing skill",
230
+ }, status);
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Render skill instructions (static or dynamic)
236
+ */
237
+ private async renderSkill(
238
+ skill: SkillDefinition,
239
+ _data: Record<string, unknown>
240
+ ): Promise<SkillExecutionResult> {
241
+ const instructions = skill.instructions;
242
+
243
+ if (skill.content_type === 'jst') {
244
+ // JST templates are not currently supported
245
+ throw new HTTPException(501, {
246
+ message: `JST templates are not currently supported. Use 'md' content type instead.`
247
+ });
248
+ }
249
+
250
+ return {
251
+ name: skill.name,
252
+ instructions,
253
+ };
254
+ }
255
+ }
256
+
257
+ // ================== Skill Parser ==================
258
+
259
+ interface SkillFrontmatter {
260
+ name: string;
261
+ title?: string;
262
+ description: string;
263
+ keywords?: string[];
264
+ tools?: string[];
265
+ data_patterns?: string[];
266
+ language?: string;
267
+ packages?: string[];
268
+ system_packages?: string[];
269
+ widgets?: string[];
270
+ scripts?: string[];
271
+ }
272
+
273
+ /**
274
+ * Parse a SKILL.md or SKILL.jst file content into a SkillDefinition.
275
+ *
276
+ * Format:
277
+ * ```
278
+ * ---
279
+ * name: skill-name
280
+ * description: Short description
281
+ * keywords: [keyword1, keyword2]
282
+ * tools: [tool1, tool2]
283
+ * language: python
284
+ * packages: [numpy, pandas]
285
+ * ---
286
+ *
287
+ * # Instructions
288
+ *
289
+ * Your markdown/jst content here...
290
+ * ```
291
+ */
292
+ export function parseSkillFile(
293
+ content: string,
294
+ contentType: SkillContentType
295
+ ): SkillDefinition {
296
+ // Parse YAML frontmatter
297
+ const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
298
+
299
+ if (!frontmatterMatch) {
300
+ throw new Error("Invalid skill file: missing YAML frontmatter");
301
+ }
302
+
303
+ const [, yamlContent, body] = frontmatterMatch;
304
+ const frontmatter = parseYamlFrontmatter(yamlContent);
305
+ const instructions = body.trim();
306
+
307
+ if (!frontmatter.name) {
308
+ throw new Error("Skill file missing required 'name' field");
309
+ }
310
+ if (!frontmatter.description) {
311
+ throw new Error("Skill file missing required 'description' field");
312
+ }
313
+
314
+ const skill: SkillDefinition = {
315
+ name: frontmatter.name,
316
+ title: frontmatter.title,
317
+ description: frontmatter.description,
318
+ instructions,
319
+ content_type: contentType,
320
+ widgets: frontmatter.widgets || undefined,
321
+ scripts: frontmatter.scripts || undefined,
322
+ };
323
+
324
+ // Build context triggers
325
+ if (frontmatter.keywords || frontmatter.tools || frontmatter.data_patterns) {
326
+ skill.context_triggers = {
327
+ keywords: frontmatter.keywords,
328
+ tool_names: frontmatter.tools,
329
+ data_patterns: frontmatter.data_patterns,
330
+ };
331
+ }
332
+
333
+ // Build execution config
334
+ if (frontmatter.language) {
335
+ skill.execution = {
336
+ language: frontmatter.language,
337
+ packages: frontmatter.packages,
338
+ system_packages: frontmatter.system_packages,
339
+ };
340
+
341
+ // Extract code template from instructions if present
342
+ const codeBlockMatch = instructions.match(/```(?:python|javascript|typescript|js|ts|py)\n([\s\S]*?)```/);
343
+ if (codeBlockMatch) {
344
+ skill.execution.template = codeBlockMatch[1].trim();
345
+ }
346
+ }
347
+
348
+ // Related tools from frontmatter
349
+ if (frontmatter.tools) {
350
+ skill.related_tools = frontmatter.tools;
351
+ }
352
+
353
+ return skill;
354
+ }
355
+
356
+ /**
357
+ * Simple YAML frontmatter parser (handles basic key: value and arrays)
358
+ */
359
+ function parseYamlFrontmatter(yaml: string): SkillFrontmatter {
360
+ const result: Record<string, any> = {};
361
+ const lines = yaml.split('\n');
362
+
363
+ for (const line of lines) {
364
+ const trimmed = line.trim();
365
+ if (!trimmed || trimmed.startsWith('#')) continue;
366
+
367
+ const colonIndex = trimmed.indexOf(':');
368
+ if (colonIndex === -1) continue;
369
+
370
+ const key = trimmed.slice(0, colonIndex).trim();
371
+ let value = trimmed.slice(colonIndex + 1).trim();
372
+
373
+ // Handle array syntax: [item1, item2]
374
+ if (value.startsWith('[') && value.endsWith(']')) {
375
+ const items = value.slice(1, -1).split(',').map(s => s.trim());
376
+ result[key] = items.filter(s => s.length > 0);
377
+ } else {
378
+ // Remove quotes if present
379
+ if ((value.startsWith('"') && value.endsWith('"')) ||
380
+ (value.startsWith("'") && value.endsWith("'"))) {
381
+ value = value.slice(1, -1);
382
+ }
383
+ result[key] = value;
384
+ }
385
+ }
386
+
387
+ return result as SkillFrontmatter;
388
+ }
389
+
390
+ /**
391
+ * Helper to load skill from file path (for Node.js usage)
392
+ */
393
+ export async function loadSkillFromFile(
394
+ filePath: string,
395
+ fs: { readFile: (path: string, encoding: string) => Promise<string> }
396
+ ): Promise<SkillDefinition> {
397
+ const content = await fs.readFile(filePath, 'utf-8');
398
+ const contentType: SkillContentType = filePath.endsWith('.jst') ? 'jst' : 'md';
399
+ return parseSkillFile(content, contentType);
400
+ }
401
+
402
+ /**
403
+ * Load all skills from a directory.
404
+ * Scans for subdirectories containing SKILL.md files.
405
+ *
406
+ * Directory structure:
407
+ * ```
408
+ * skills/
409
+ * nagare/
410
+ * fund-onboarding/
411
+ * SKILL.md
412
+ * monte-carlo/
413
+ * SKILL.md
414
+ * ```
415
+ *
416
+ * @param dirPath - Path to the skills collection directory
417
+ * @returns Array of parsed skill definitions
418
+ */
419
+ export function loadSkillsFromDirectory(dirPath: string): SkillDefinition[] {
420
+ const skills: SkillDefinition[] = [];
421
+
422
+ let entries: string[];
423
+ try {
424
+ entries = readdirSync(dirPath);
425
+ } catch {
426
+ console.warn(`Could not read skills directory: ${dirPath}`);
427
+ return skills;
428
+ }
429
+
430
+ for (const entry of entries) {
431
+ const entryPath = join(dirPath, entry);
432
+
433
+ try {
434
+ const stat = statSync(entryPath);
435
+ if (!stat.isDirectory()) continue;
436
+
437
+ // Look for SKILL.md or SKILL.jst
438
+ const mdPath = join(entryPath, "SKILL.md");
439
+ const jstPath = join(entryPath, "SKILL.jst");
440
+
441
+ let content: string | undefined;
442
+ let contentType: SkillContentType = 'md';
443
+
444
+ if (existsSync(mdPath)) {
445
+ content = readFileSync(mdPath, "utf-8");
446
+ contentType = 'md';
447
+ } else if (existsSync(jstPath)) {
448
+ content = readFileSync(jstPath, "utf-8");
449
+ contentType = 'jst';
450
+ }
451
+
452
+ if (content) {
453
+ skills.push(parseSkillFile(content, contentType));
454
+ }
455
+ } catch (err) {
456
+ console.warn(`Error loading skill from ${entryPath}:`, err);
457
+ }
458
+ }
459
+
460
+ return skills;
461
+ }
@@ -0,0 +1,223 @@
1
+ import { readdirSync, statSync, existsSync } from "fs";
2
+ import { join } from "path";
3
+ import { pathToFileURL } from "url";
4
+ import { Context } from "hono";
5
+ import { HTTPException } from "hono/http-exception";
6
+ import { authorize } from "./auth.js";
7
+ import { ToolFilterOptions, ToolRegistry } from "./ToolRegistry.js";
8
+ import { ToolContext } from "./server/types.js";
9
+ import type { CollectionProperties, ICollection, Tool, ToolDefinitionWithDefault, ToolExecutionPayload, ToolExecutionResponse, ToolExecutionResponseError } from "./types.js";
10
+ import { kebabCaseToTitle } from "./utils.js";
11
+
12
+ export interface ToolCollectionProperties extends CollectionProperties {
13
+ /**
14
+ * The tools
15
+ */
16
+ tools: Tool<any>[];
17
+ }
18
+
19
+ /**
20
+ * Implements a tools collection endpoint
21
+ */
22
+ export class ToolCollection implements ICollection<Tool<any>> {
23
+
24
+ /**
25
+ * A kebab case collection name. Must only contains alphanumeric and dash characters,
26
+ * The name can be used to generate the path where the collection is exposed.
27
+ * Example: my-collection
28
+ */
29
+ name: string;
30
+ /**
31
+ * Optional title for UI display.
32
+ * If not provided the title will be generated form the kebab case name by replacing - with spaces and upper casing first letter in words.
33
+ */
34
+ title?: string;
35
+ /**
36
+ * Optional icon for UI display
37
+ */
38
+ icon?: string;
39
+ /**
40
+ * A short description
41
+ */
42
+ description?: string;
43
+ /**
44
+ * The tool registry
45
+ */
46
+ tools: ToolRegistry;
47
+
48
+ constructor({
49
+ name, title, icon, description, tools
50
+ }: ToolCollectionProperties) {
51
+ this.name = name;
52
+ this.title = title || kebabCaseToTitle(name);
53
+ this.icon = icon;
54
+ this.description = description;
55
+ this.tools = new ToolRegistry(tools);
56
+ }
57
+
58
+ [Symbol.iterator](): Iterator<Tool<any>> {
59
+ let index = 0;
60
+ const tools = this.tools.getTools();
61
+
62
+ return {
63
+ next(): IteratorResult<Tool<any>> {
64
+ if (index < tools.length) {
65
+ return { value: tools[index++], done: false };
66
+ } else {
67
+ return { done: true, value: undefined };
68
+ }
69
+ }
70
+ };
71
+ }
72
+
73
+ map<U>(callback: (tool: Tool<any>, index: number) => U): U[] {
74
+ return this.tools.getTools().map(callback);
75
+ }
76
+
77
+ async execute(ctx: Context, preParsedPayload?: ToolExecutionPayload<any>): Promise<Response> {
78
+ let payload: ToolExecutionPayload<any> | undefined = preParsedPayload;
79
+ try {
80
+ if (!payload) {
81
+ payload = await readPayload(ctx);
82
+ }
83
+ const toolName = payload.tool_use?.tool_name;
84
+ const toolUseId = payload.tool_use?.id;
85
+ const endpointOverrides = payload.metadata?.endpoints;
86
+
87
+ const runId = payload.metadata?.run_id;
88
+
89
+ console.log(`[ToolCollection] Tool call received: ${toolName}`, {
90
+ collection: this.name,
91
+ toolUseId,
92
+ runId,
93
+ hasEndpointOverrides: !!endpointOverrides,
94
+ });
95
+
96
+ const session = await authorize(ctx, endpointOverrides, { toolName, toolUseId, runId });
97
+ const r = await this.tools.runTool(payload, session);
98
+ return ctx.json({
99
+ ...r,
100
+ tool_use_id: payload.tool_use.id
101
+ } satisfies ToolExecutionResponse);
102
+ } catch (err: any) { // HTTPException ?
103
+ const status = err.status || 500;
104
+ const toolName = payload?.tool_use?.tool_name;
105
+ const toolUseId = payload?.tool_use?.id;
106
+
107
+ console.error("[ToolCollection] Tool execution failed", {
108
+ collection: this.name,
109
+ tool: toolName,
110
+ toolUseId,
111
+ error: err.message,
112
+ status,
113
+ stack: err.stack,
114
+ });
115
+
116
+ return ctx.json({
117
+ tool_use_id: toolUseId || "undefined",
118
+ error: err.message || "Error executing tool",
119
+ status
120
+ } satisfies ToolExecutionResponseError, status)
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Get tool definitions with optional filtering.
126
+ * @param options - Filtering options for default/unlocked tools
127
+ * @returns Filtered tool definitions
128
+ */
129
+ getToolDefinitions(options?: ToolFilterOptions): ToolDefinitionWithDefault[] {
130
+ return this.tools.getDefinitions(options);
131
+ }
132
+
133
+ /**
134
+ * Get tools that are in reserve (default: false and not unlocked).
135
+ * @param unlockedTools - List of tool names that are unlocked
136
+ * @returns Tool definitions for reserve tools
137
+ */
138
+ getReserveTools(unlockedTools: string[] = []): ToolDefinitionWithDefault[] {
139
+ return this.tools.getReserveTools(unlockedTools);
140
+ }
141
+
142
+ }
143
+
144
+
145
+ function readPayload(ctx: Context): ToolExecutionPayload<any> {
146
+ const toolCtx = ctx as ToolContext;
147
+
148
+ // Check if body was already parsed and validated by middleware
149
+ if (toolCtx.payload) {
150
+ return toolCtx.payload;
151
+ }
152
+
153
+ // If no payload, middleware couldn't parse/validate - return error
154
+ throw new HTTPException(400, {
155
+ message: 'Invalid or missing tool execution payload. Expected { tool_use: { id, tool_name, tool_input? }, metadata? }'
156
+ });
157
+ }
158
+
159
+ /**
160
+ * Load all tools from a directory.
161
+ * Scans for .js files and imports tools that match naming convention.
162
+ *
163
+ * Directory structure:
164
+ * ```
165
+ * collection/
166
+ * tools/
167
+ * SearchFundsTool.js # exports SearchFundsTool
168
+ * GetFundDetailsTool.js # exports GetFundDetailsTool
169
+ * ```
170
+ *
171
+ * Naming convention: File should export a Tool with name matching *Tool pattern.
172
+ *
173
+ * @param toolsDir - Path to the tools directory (e.g., /path/to/collection/tools)
174
+ * @returns Promise resolving to array of Tool objects
175
+ */
176
+ export async function loadToolsFromDirectory(toolsDir: string): Promise<Tool<any>[]> {
177
+ const tools: Tool<any>[] = [];
178
+
179
+ if (!existsSync(toolsDir)) {
180
+ console.warn(`Tools directory not found: ${toolsDir}`);
181
+ return tools;
182
+ }
183
+
184
+ let entries: string[];
185
+ try {
186
+ entries = readdirSync(toolsDir);
187
+ } catch {
188
+ console.warn(`Could not read tools directory: ${toolsDir}`);
189
+ return tools;
190
+ }
191
+
192
+ for (const entry of entries) {
193
+ // Only process .js and .ts files that end with Tool
194
+ if (!entry.endsWith('Tool.js') && !entry.endsWith('Tool.ts')) continue;
195
+ if (entry.endsWith('.d.ts')) continue;
196
+
197
+ const entryPath = join(toolsDir, entry);
198
+
199
+ try {
200
+ const stat = statSync(entryPath);
201
+ if (!stat.isFile()) continue;
202
+
203
+ // Dynamic import - need file:// URL for ESM
204
+ const fileUrl = pathToFileURL(entryPath).href;
205
+ const module = await import(fileUrl);
206
+
207
+ // Find exported Tool (named export matching filename or any Tool export)
208
+ const baseName = entry.replace(/\.(js|ts)$/, '');
209
+ const tool = module[baseName] || module.default;
210
+
211
+ if (tool && typeof tool.name === 'string' && typeof tool.run === 'function') {
212
+ tools.push(tool);
213
+ } else {
214
+ console.warn(`No valid Tool export found in ${entry}`);
215
+ }
216
+ } catch (err) {
217
+ console.warn(`Error loading tool from ${entry}:`, err);
218
+ }
219
+ }
220
+
221
+ return tools;
222
+ }
223
+