bigtool-ts 0.1.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.
Files changed (116) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE +21 -0
  3. package/README.md +641 -0
  4. package/dist/adapters/agent-protocol.d.ts +149 -0
  5. package/dist/adapters/agent-protocol.d.ts.map +1 -0
  6. package/dist/adapters/agent-protocol.js +133 -0
  7. package/dist/adapters/agent-protocol.js.map +1 -0
  8. package/dist/adapters/index.d.ts +39 -0
  9. package/dist/adapters/index.d.ts.map +1 -0
  10. package/dist/adapters/index.js +42 -0
  11. package/dist/adapters/index.js.map +1 -0
  12. package/dist/adapters/inngest.d.ts +234 -0
  13. package/dist/adapters/inngest.d.ts.map +1 -0
  14. package/dist/adapters/inngest.js +276 -0
  15. package/dist/adapters/inngest.js.map +1 -0
  16. package/dist/adapters/mastra.d.ts +201 -0
  17. package/dist/adapters/mastra.d.ts.map +1 -0
  18. package/dist/adapters/mastra.js +250 -0
  19. package/dist/adapters/mastra.js.map +1 -0
  20. package/dist/adapters/types.d.ts +42 -0
  21. package/dist/adapters/types.d.ts.map +1 -0
  22. package/dist/adapters/types.js +6 -0
  23. package/dist/adapters/types.js.map +1 -0
  24. package/dist/adapters/vercel-ai.d.ts +176 -0
  25. package/dist/adapters/vercel-ai.d.ts.map +1 -0
  26. package/dist/adapters/vercel-ai.js +244 -0
  27. package/dist/adapters/vercel-ai.js.map +1 -0
  28. package/dist/catalog/index.d.ts +177 -0
  29. package/dist/catalog/index.d.ts.map +1 -0
  30. package/dist/catalog/index.js +244 -0
  31. package/dist/catalog/index.js.map +1 -0
  32. package/dist/graph/agent.d.ts +214 -0
  33. package/dist/graph/agent.d.ts.map +1 -0
  34. package/dist/graph/agent.js +196 -0
  35. package/dist/graph/agent.js.map +1 -0
  36. package/dist/graph/index.d.ts +5 -0
  37. package/dist/graph/index.d.ts.map +1 -0
  38. package/dist/graph/index.js +4 -0
  39. package/dist/graph/index.js.map +1 -0
  40. package/dist/graph/nodes.d.ts +100 -0
  41. package/dist/graph/nodes.d.ts.map +1 -0
  42. package/dist/graph/nodes.js +190 -0
  43. package/dist/graph/nodes.js.map +1 -0
  44. package/dist/graph/search-tool.d.ts +34 -0
  45. package/dist/graph/search-tool.d.ts.map +1 -0
  46. package/dist/graph/search-tool.js +54 -0
  47. package/dist/graph/search-tool.js.map +1 -0
  48. package/dist/graph/state.d.ts +26 -0
  49. package/dist/graph/state.d.ts.map +1 -0
  50. package/dist/graph/state.js +29 -0
  51. package/dist/graph/state.js.map +1 -0
  52. package/dist/index.d.ts +69 -0
  53. package/dist/index.d.ts.map +1 -0
  54. package/dist/index.js +85 -0
  55. package/dist/index.js.map +1 -0
  56. package/dist/loader/index.d.ts +172 -0
  57. package/dist/loader/index.d.ts.map +1 -0
  58. package/dist/loader/index.js +179 -0
  59. package/dist/loader/index.js.map +1 -0
  60. package/dist/loader/loader.d.ts +114 -0
  61. package/dist/loader/loader.d.ts.map +1 -0
  62. package/dist/loader/loader.js +185 -0
  63. package/dist/loader/loader.js.map +1 -0
  64. package/dist/search/cache.d.ts +76 -0
  65. package/dist/search/cache.d.ts.map +1 -0
  66. package/dist/search/cache.js +135 -0
  67. package/dist/search/cache.js.map +1 -0
  68. package/dist/search/index.d.ts +63 -0
  69. package/dist/search/index.d.ts.map +1 -0
  70. package/dist/search/index.js +122 -0
  71. package/dist/search/index.js.map +1 -0
  72. package/dist/search/normalize.d.ts +104 -0
  73. package/dist/search/normalize.d.ts.map +1 -0
  74. package/dist/search/normalize.js +211 -0
  75. package/dist/search/normalize.js.map +1 -0
  76. package/dist/search/orama.d.ts +256 -0
  77. package/dist/search/orama.d.ts.map +1 -0
  78. package/dist/search/orama.js +511 -0
  79. package/dist/search/orama.js.map +1 -0
  80. package/dist/search/types.d.ts +96 -0
  81. package/dist/search/types.d.ts.map +1 -0
  82. package/dist/search/types.js +8 -0
  83. package/dist/search/types.js.map +1 -0
  84. package/dist/sources/dynamic.d.ts +200 -0
  85. package/dist/sources/dynamic.d.ts.map +1 -0
  86. package/dist/sources/dynamic.js +194 -0
  87. package/dist/sources/dynamic.js.map +1 -0
  88. package/dist/sources/index.d.ts +11 -0
  89. package/dist/sources/index.d.ts.map +1 -0
  90. package/dist/sources/index.js +14 -0
  91. package/dist/sources/index.js.map +1 -0
  92. package/dist/sources/local.d.ts +128 -0
  93. package/dist/sources/local.d.ts.map +1 -0
  94. package/dist/sources/local.js +155 -0
  95. package/dist/sources/local.js.map +1 -0
  96. package/dist/sources/mcp.d.ts +438 -0
  97. package/dist/sources/mcp.d.ts.map +1 -0
  98. package/dist/sources/mcp.js +438 -0
  99. package/dist/sources/mcp.js.map +1 -0
  100. package/dist/sources/types.d.ts +16 -0
  101. package/dist/sources/types.d.ts.map +1 -0
  102. package/dist/sources/types.js +7 -0
  103. package/dist/sources/types.js.map +1 -0
  104. package/dist/sources/with-metadata.d.ts +7 -0
  105. package/dist/sources/with-metadata.d.ts.map +1 -0
  106. package/dist/sources/with-metadata.js +7 -0
  107. package/dist/sources/with-metadata.js.map +1 -0
  108. package/dist/types/index.d.ts +7 -0
  109. package/dist/types/index.d.ts.map +1 -0
  110. package/dist/types/index.js +8 -0
  111. package/dist/types/index.js.map +1 -0
  112. package/dist/types.d.ts +700 -0
  113. package/dist/types.d.ts.map +1 -0
  114. package/dist/types.js +97 -0
  115. package/dist/types.js.map +1 -0
  116. package/package.json +118 -0
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Vercel AI SDK adapter for bigtool-ts.
3
+ *
4
+ * Converts bigtool-ts tools to Vercel AI SDK format for use with
5
+ * generateText(), streamText(), and other SDK functions.
6
+ *
7
+ * @module adapters/vercel-ai
8
+ */
9
+ import { z } from 'zod';
10
+ // ═══════════════════════════════════════════════════════════════════
11
+ // SEARCH RESULT SCHEMA
12
+ // ═══════════════════════════════════════════════════════════════════
13
+ const searchInputSchema = z.object({
14
+ query: z.string().describe('Natural language search query'),
15
+ });
16
+ const searchResultSchema = z.object({
17
+ name: z.string(),
18
+ description: z.string(),
19
+ score: z.number(),
20
+ });
21
+ const searchOutputSchema = z.array(searchResultSchema);
22
+ // ═══════════════════════════════════════════════════════════════════
23
+ // ADAPTER IMPLEMENTATION
24
+ // ═══════════════════════════════════════════════════════════════════
25
+ /**
26
+ * Vercel AI SDK adapter for bigtool-ts.
27
+ *
28
+ * Converts bigtool-ts tools to Vercel AI SDK format. Tools are lazily
29
+ * loaded when executed, and the adapter handles abort signal propagation.
30
+ *
31
+ * @example Basic usage with streamText
32
+ * ```typescript
33
+ * import { streamText } from 'ai';
34
+ * import { openai } from '@ai-sdk/openai';
35
+ * import { createVercelAdapter } from '@repo/bigtool-ts/adapters';
36
+ *
37
+ * const adapter = createVercelAdapter({ catalog, loader, searchIndex });
38
+ *
39
+ * const result = await streamText({
40
+ * model: openai('gpt-4o'),
41
+ * tools: {
42
+ * search_tools: adapter.createSearchTool(),
43
+ * ...await adapter.getToolsAsRecord(['github:create_pr']),
44
+ * },
45
+ * prompt: 'Create a PR with title "Fix bug"',
46
+ * });
47
+ * ```
48
+ *
49
+ * @example With search tool for dynamic discovery
50
+ * ```typescript
51
+ * const result = await generateText({
52
+ * model: openai('gpt-4o'),
53
+ * tools: {
54
+ * search_tools: adapter.createSearchTool({ limit: 10 }),
55
+ * },
56
+ * prompt: 'What tools are available for GitHub?',
57
+ * });
58
+ * ```
59
+ */
60
+ export class VercelAIAdapter {
61
+ catalog;
62
+ loader;
63
+ searchIndex;
64
+ constructor(config) {
65
+ this.catalog = config.catalog;
66
+ this.loader = config.loader;
67
+ this.searchIndex = config.searchIndex;
68
+ }
69
+ /**
70
+ * Convert bigtool-ts metadata to a Vercel AI SDK tool.
71
+ *
72
+ * The returned tool lazily loads the implementation on first execute.
73
+ * Abort signals are checked before loading to fail fast on cancellation.
74
+ *
75
+ * @param metadata - Tool metadata from the catalog
76
+ * @returns Vercel AI SDK compatible tool
77
+ */
78
+ toFrameworkTool(metadata) {
79
+ const loader = this.loader;
80
+ return {
81
+ description: metadata.description,
82
+ inputSchema: createJsonSchema(metadata.parameters),
83
+ execute: async (args, options) => {
84
+ // Fail fast on abort before expensive loader call
85
+ options.abortSignal?.throwIfAborted();
86
+ const tool = await loader.load(metadata.id);
87
+ return tool.invoke(args);
88
+ },
89
+ };
90
+ }
91
+ /**
92
+ * Get tools by IDs as an array.
93
+ *
94
+ * Missing tools are silently skipped. Use this when you need an array
95
+ * of tools and don't care about the tool names.
96
+ *
97
+ * @param toolIds - Array of tool IDs to load
98
+ * @returns Array of Vercel AI SDK tools
99
+ */
100
+ async getTools(toolIds) {
101
+ const tools = [];
102
+ for (const id of toolIds) {
103
+ const metadata = this.catalog.getMetadata(id);
104
+ if (metadata === null) {
105
+ continue;
106
+ }
107
+ tools.push(this.toFrameworkTool(metadata));
108
+ }
109
+ return tools;
110
+ }
111
+ /**
112
+ * Get tools by IDs as a record (ToolSet).
113
+ *
114
+ * The record keys are the tool names from metadata. Missing tools are
115
+ * silently skipped. Use this for passing to generateText/streamText.
116
+ *
117
+ * @param toolIds - Array of tool IDs to load
118
+ * @returns Record of tools keyed by name
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const tools = await adapter.getToolsAsRecord([
123
+ * 'github:create_pr',
124
+ * 'slack:send_message',
125
+ * ]);
126
+ *
127
+ * const result = await streamText({
128
+ * model: openai('gpt-4o'),
129
+ * tools,
130
+ * prompt: 'Create a PR and notify the team',
131
+ * });
132
+ * ```
133
+ */
134
+ async getToolsAsRecord(toolIds) {
135
+ const tools = {};
136
+ for (const id of toolIds) {
137
+ const metadata = this.catalog.getMetadata(id);
138
+ if (metadata === null) {
139
+ continue;
140
+ }
141
+ tools[metadata.name] = this.toFrameworkTool(metadata);
142
+ }
143
+ return tools;
144
+ }
145
+ /**
146
+ * Create a search tool for discovering available tools.
147
+ *
148
+ * The search tool enables dynamic tool discovery in agentic workflows.
149
+ * The LLM can use this tool to find relevant tools before calling them.
150
+ *
151
+ * @param options - Search configuration options
152
+ * @returns Vercel AI SDK tool for searching the catalog
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * const searchTool = adapter.createSearchTool({
157
+ * limit: 5,
158
+ * threshold: 0.3,
159
+ * categories: ['github'],
160
+ * });
161
+ *
162
+ * const result = await generateText({
163
+ * model: openai('gpt-4o'),
164
+ * tools: { search_tools: searchTool },
165
+ * prompt: 'Find tools for creating pull requests',
166
+ * });
167
+ * ```
168
+ */
169
+ createSearchTool(options = {}) {
170
+ const limit = options.limit ?? 5;
171
+ const threshold = options.threshold ?? 0;
172
+ const categories = options.categories;
173
+ const searchIndex = this.searchIndex;
174
+ const catalog = this.catalog;
175
+ return {
176
+ description: 'Search for available tools by query. Use this to discover what tools are available before calling them.',
177
+ inputSchema: searchInputSchema,
178
+ outputSchema: searchOutputSchema,
179
+ execute: async (input) => {
180
+ // Input is validated by the framework via inputSchema before execute is called
181
+ const { query } = input;
182
+ const results = await searchIndex.search(query, {
183
+ limit,
184
+ threshold,
185
+ categories,
186
+ });
187
+ return results.map((result) => {
188
+ const meta = catalog.getMetadata(result.toolId);
189
+ return {
190
+ name: meta?.name ?? result.toolId,
191
+ description: meta?.description ?? '',
192
+ score: result.score,
193
+ };
194
+ });
195
+ },
196
+ };
197
+ }
198
+ }
199
+ // ═══════════════════════════════════════════════════════════════════
200
+ // FACTORY FUNCTION
201
+ // ═══════════════════════════════════════════════════════════════════
202
+ /**
203
+ * Create a Vercel AI SDK adapter.
204
+ *
205
+ * Factory function for creating a VercelAIAdapter instance.
206
+ * Use this as the primary entry point for creating adapters.
207
+ *
208
+ * @param config - Adapter configuration with catalog, loader, and searchIndex
209
+ * @returns Configured VercelAIAdapter instance
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * import { createVercelAdapter } from '@repo/bigtool-ts/adapters';
214
+ *
215
+ * const adapter = createVercelAdapter({
216
+ * catalog: new DefaultToolCatalog(),
217
+ * loader: new DefaultToolLoader(catalog),
218
+ * searchIndex: new OramaSearch(),
219
+ * });
220
+ * ```
221
+ */
222
+ export function createVercelAdapter(config) {
223
+ return new VercelAIAdapter(config);
224
+ }
225
+ // ═══════════════════════════════════════════════════════════════════
226
+ // HELPERS
227
+ // ═══════════════════════════════════════════════════════════════════
228
+ /**
229
+ * Create a JSON schema wrapper for Vercel AI SDK.
230
+ *
231
+ * The Vercel AI SDK accepts FlexibleSchema which includes raw JSON Schema.
232
+ * This function wraps the parameters to match the expected format.
233
+ *
234
+ * @param parameters - JSON Schema parameters from tool metadata
235
+ * @returns Wrapped schema for Vercel AI SDK
236
+ */
237
+ function createJsonSchema(parameters) {
238
+ // Default to empty object schema if no parameters
239
+ const schema = parameters ?? { type: 'object', properties: {} };
240
+ // Return in a format compatible with Vercel AI SDK's jsonSchema() helper
241
+ // The SDK accepts raw JSON Schema objects directly
242
+ return schema;
243
+ }
244
+ //# sourceMappingURL=vercel-ai.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vercel-ai.js","sourceRoot":"","sources":["../../src/adapters/vercel-ai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA+CxB,sEAAsE;AACtE,uBAAuB;AACvB,sEAAsE;AAEtE,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC;IACjC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,+BAA+B,CAAC;CAC5D,CAAC,CAAC;AAEH,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IAClC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;IACvB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;CAClB,CAAC,CAAC;AAEH,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAKvD,sEAAsE;AACtE,yBAAyB;AACzB,sEAAsE;AAEtE;;;;;;;;;;;;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,MAAM,CAAC,MAAM,CAAC,CAAC;oBAChD,OAAO;wBACL,IAAI,EAAE,IAAI,EAAE,IAAI,IAAI,MAAM,CAAC,MAAM;wBACjC,WAAW,EAAE,IAAI,EAAE,WAAW,IAAI,EAAE;wBACpC,KAAK,EAAE,MAAM,CAAC,KAAK;qBACpB,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AAED,sEAAsE;AACtE,mBAAmB;AACnB,sEAAsE;AAEtE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAqB;IACvD,OAAO,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC;AAED,sEAAsE;AACtE,UAAU;AACV,sEAAsE;AAEtE;;;;;;;;GAQG;AACH,SAAS,gBAAgB,CACvB,UAA+C;IAE/C,kDAAkD;IAClD,MAAM,MAAM,GAAG,UAAU,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;IAEhE,yEAAyE;IACzE,mDAAmD;IACnD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * ToolCatalog module.
3
+ *
4
+ * Provides the default implementation of the ToolCatalog interface,
5
+ * aggregating tools from multiple sources into a unified registry.
6
+ *
7
+ * @module catalog
8
+ */
9
+ import type { ToolCatalog, ToolSource, ToolMetadata, ToolsChangedEvent } from '../types.js';
10
+ import { type EventEmitter } from '../types.js';
11
+ /**
12
+ * Default implementation of ToolCatalog.
13
+ *
14
+ * Maintains a registry of tools from multiple sources and provides
15
+ * a unified view of all available tool metadata. The catalog is the
16
+ * central source of truth for what tools exist in the system.
17
+ *
18
+ * Key responsibilities:
19
+ * - Registering and unregistering tool sources
20
+ * - Aggregating metadata from all sources
21
+ * - Emitting change events when tools are added or removed
22
+ * - Providing source references for tool loading
23
+ *
24
+ * @example Basic usage
25
+ * ```typescript
26
+ * import { DefaultToolCatalog, LocalSource, MCPSource } from '@repo/bigtool-ts';
27
+ *
28
+ * const catalog = new DefaultToolCatalog();
29
+ *
30
+ * // Register sources
31
+ * await catalog.register(new LocalSource(myTools));
32
+ * await catalog.register(new MCPSource(mcpClient));
33
+ *
34
+ * // Query the catalog
35
+ * const allTools = catalog.getAllMetadata();
36
+ * console.log(`Total tools: ${allTools.length}`);
37
+ *
38
+ * // Get specific tool metadata
39
+ * const tool = catalog.getMetadata('local:calculator');
40
+ * ```
41
+ *
42
+ * @example Listening for changes
43
+ * ```typescript
44
+ * const catalog = new DefaultToolCatalog();
45
+ *
46
+ * catalog.onToolsChanged.subscribe(async (event) => {
47
+ * if (event.added.length > 0) {
48
+ * console.log('New tools:', event.added);
49
+ * await searchIndex.reindex();
50
+ * }
51
+ * if (event.removed.length > 0) {
52
+ * console.log('Removed tools:', event.removed);
53
+ * }
54
+ * });
55
+ *
56
+ * await catalog.register(new LocalSource(tools));
57
+ * ```
58
+ */
59
+ export declare class DefaultToolCatalog implements ToolCatalog {
60
+ /** @internal Map of source ID to source */
61
+ private sources;
62
+ /** @internal Map of source ID to its metadata */
63
+ private metadataBySource;
64
+ /** @internal Map of tool ID to metadata for fast lookup */
65
+ private metadataById;
66
+ /**
67
+ * Event emitter for tool changes.
68
+ *
69
+ * Subscribe to be notified when tools are added or removed
70
+ * from the catalog.
71
+ */
72
+ readonly onToolsChanged: EventEmitter<ToolsChangedEvent>;
73
+ /**
74
+ * Creates a new DefaultToolCatalog.
75
+ *
76
+ * The catalog starts empty. Call register() to add tool sources.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const catalog = new DefaultToolCatalog();
81
+ * ```
82
+ */
83
+ constructor();
84
+ /**
85
+ * Register a new tool source.
86
+ *
87
+ * Fetches metadata from the source and adds all tools to the catalog.
88
+ * Subscribes to the source's onRefresh event if available.
89
+ * Emits a ToolsChangedEvent with the added tool IDs.
90
+ *
91
+ * @param source - The tool source to register
92
+ * @throws Error if a source with the same ID is already registered
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * await catalog.register(new LocalSource(tools, 'my-tools'));
97
+ * ```
98
+ */
99
+ register(source: ToolSource): Promise<void>;
100
+ /**
101
+ * Unregister a tool source by ID.
102
+ *
103
+ * Removes all tools from that source and emits a ToolsChangedEvent.
104
+ * No-op if the source ID doesn't exist.
105
+ *
106
+ * @param sourceId - The source ID to unregister
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * // Remove a source
111
+ * catalog.unregister('mcp:github');
112
+ *
113
+ * // Safe to call even if not registered
114
+ * catalog.unregister('nonexistent'); // No-op
115
+ * ```
116
+ */
117
+ unregister(sourceId: string): void;
118
+ /**
119
+ * Get all tool metadata in the catalog.
120
+ *
121
+ * Returns a snapshot of all currently registered tools from
122
+ * all sources.
123
+ *
124
+ * @returns Array of all tool metadata
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const allTools = catalog.getAllMetadata();
129
+ * console.log(`Catalog has ${allTools.length} tools`);
130
+ *
131
+ * // Use for search indexing
132
+ * await searchIndex.index(allTools);
133
+ * ```
134
+ */
135
+ getAllMetadata(): ToolMetadata[];
136
+ /**
137
+ * Get metadata for a specific tool.
138
+ *
139
+ * @param id - The tool ID to look up
140
+ * @returns Tool metadata, or null if not found
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * const meta = catalog.getMetadata('local:calculator');
145
+ * if (meta) {
146
+ * console.log(`Found: ${meta.name} - ${meta.description}`);
147
+ * }
148
+ * ```
149
+ */
150
+ getMetadata(id: string): ToolMetadata | null;
151
+ /**
152
+ * Get the source for a tool by source ID.
153
+ *
154
+ * Used by the ToolLoader to retrieve tool implementations.
155
+ *
156
+ * @param sourceId - The source ID to look up
157
+ * @returns The tool source, or null if not found
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const source = catalog.getSource('mcp:github');
162
+ * if (source) {
163
+ * const tool = await source.getTool('create_pr');
164
+ * }
165
+ * ```
166
+ */
167
+ getSource(sourceId: string): ToolSource | null;
168
+ /**
169
+ * Handle a source refresh event.
170
+ *
171
+ * @internal
172
+ * @param sourceId - The source that refreshed
173
+ * @param newMetadata - The new metadata from the source
174
+ */
175
+ private handleSourceRefresh;
176
+ }
177
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/catalog/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,YAAY,EACZ,iBAAiB,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,qBAAa,kBAAmB,YAAW,WAAW;IACpD,2CAA2C;IAC3C,OAAO,CAAC,OAAO,CAAsC;IAErD,iDAAiD;IACjD,OAAO,CAAC,gBAAgB,CAA0C;IAElE,2DAA2D;IAC3D,OAAO,CAAC,YAAY,CAAwC;IAE5D;;;;;OAKG;IACH,QAAQ,CAAC,cAAc,EAAE,YAAY,CAAC,iBAAiB,CAAC,CAAC;IAEzD;;;;;;;;;OASG;;IAKH;;;;;;;;;;;;;;OAcG;IACG,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IA4BjD;;;;;;;;;;;;;;;;OAgBG;IACH,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAmBlC;;;;;;;;;;;;;;;;OAgBG;IACH,cAAc,IAAI,YAAY,EAAE;IAIhC;;;;;;;;;;;;;OAaG;IACH,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IAI5C;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAI9C;;;;;;OAMG;IACH,OAAO,CAAC,mBAAmB;CA+B5B"}
@@ -0,0 +1,244 @@
1
+ /**
2
+ * ToolCatalog module.
3
+ *
4
+ * Provides the default implementation of the ToolCatalog interface,
5
+ * aggregating tools from multiple sources into a unified registry.
6
+ *
7
+ * @module catalog
8
+ */
9
+ import { createEventEmitter } from '../types.js';
10
+ /**
11
+ * Default implementation of ToolCatalog.
12
+ *
13
+ * Maintains a registry of tools from multiple sources and provides
14
+ * a unified view of all available tool metadata. The catalog is the
15
+ * central source of truth for what tools exist in the system.
16
+ *
17
+ * Key responsibilities:
18
+ * - Registering and unregistering tool sources
19
+ * - Aggregating metadata from all sources
20
+ * - Emitting change events when tools are added or removed
21
+ * - Providing source references for tool loading
22
+ *
23
+ * @example Basic usage
24
+ * ```typescript
25
+ * import { DefaultToolCatalog, LocalSource, MCPSource } from '@repo/bigtool-ts';
26
+ *
27
+ * const catalog = new DefaultToolCatalog();
28
+ *
29
+ * // Register sources
30
+ * await catalog.register(new LocalSource(myTools));
31
+ * await catalog.register(new MCPSource(mcpClient));
32
+ *
33
+ * // Query the catalog
34
+ * const allTools = catalog.getAllMetadata();
35
+ * console.log(`Total tools: ${allTools.length}`);
36
+ *
37
+ * // Get specific tool metadata
38
+ * const tool = catalog.getMetadata('local:calculator');
39
+ * ```
40
+ *
41
+ * @example Listening for changes
42
+ * ```typescript
43
+ * const catalog = new DefaultToolCatalog();
44
+ *
45
+ * catalog.onToolsChanged.subscribe(async (event) => {
46
+ * if (event.added.length > 0) {
47
+ * console.log('New tools:', event.added);
48
+ * await searchIndex.reindex();
49
+ * }
50
+ * if (event.removed.length > 0) {
51
+ * console.log('Removed tools:', event.removed);
52
+ * }
53
+ * });
54
+ *
55
+ * await catalog.register(new LocalSource(tools));
56
+ * ```
57
+ */
58
+ export class DefaultToolCatalog {
59
+ /** @internal Map of source ID to source */
60
+ sources = new Map();
61
+ /** @internal Map of source ID to its metadata */
62
+ metadataBySource = new Map();
63
+ /** @internal Map of tool ID to metadata for fast lookup */
64
+ metadataById = new Map();
65
+ /**
66
+ * Event emitter for tool changes.
67
+ *
68
+ * Subscribe to be notified when tools are added or removed
69
+ * from the catalog.
70
+ */
71
+ onToolsChanged;
72
+ /**
73
+ * Creates a new DefaultToolCatalog.
74
+ *
75
+ * The catalog starts empty. Call register() to add tool sources.
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const catalog = new DefaultToolCatalog();
80
+ * ```
81
+ */
82
+ constructor() {
83
+ this.onToolsChanged = createEventEmitter();
84
+ }
85
+ /**
86
+ * Register a new tool source.
87
+ *
88
+ * Fetches metadata from the source and adds all tools to the catalog.
89
+ * Subscribes to the source's onRefresh event if available.
90
+ * Emits a ToolsChangedEvent with the added tool IDs.
91
+ *
92
+ * @param source - The tool source to register
93
+ * @throws Error if a source with the same ID is already registered
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * await catalog.register(new LocalSource(tools, 'my-tools'));
98
+ * ```
99
+ */
100
+ async register(source) {
101
+ if (this.sources.has(source.id)) {
102
+ throw new Error(`ToolCatalog: Source with id '${source.id}' already registered`);
103
+ }
104
+ this.sources.set(source.id, source);
105
+ // Fetch and store metadata
106
+ const metadata = await source.getMetadata();
107
+ this.metadataBySource.set(source.id, metadata);
108
+ const added = [];
109
+ for (const tool of metadata) {
110
+ this.metadataById.set(tool.id, tool);
111
+ added.push(tool.id);
112
+ }
113
+ // Subscribe to refresh events if available
114
+ if (source.onRefresh) {
115
+ source.onRefresh.on((newMetadata) => {
116
+ this.handleSourceRefresh(source.id, newMetadata);
117
+ });
118
+ }
119
+ // Emit change event
120
+ this.onToolsChanged.emit({ added, removed: [] });
121
+ }
122
+ /**
123
+ * Unregister a tool source by ID.
124
+ *
125
+ * Removes all tools from that source and emits a ToolsChangedEvent.
126
+ * No-op if the source ID doesn't exist.
127
+ *
128
+ * @param sourceId - The source ID to unregister
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // Remove a source
133
+ * catalog.unregister('mcp:github');
134
+ *
135
+ * // Safe to call even if not registered
136
+ * catalog.unregister('nonexistent'); // No-op
137
+ * ```
138
+ */
139
+ unregister(sourceId) {
140
+ const metadata = this.metadataBySource.get(sourceId);
141
+ if (!metadata) {
142
+ return;
143
+ }
144
+ const removed = [];
145
+ for (const tool of metadata) {
146
+ this.metadataById.delete(tool.id);
147
+ removed.push(tool.id);
148
+ }
149
+ this.sources.delete(sourceId);
150
+ this.metadataBySource.delete(sourceId);
151
+ // Emit change event
152
+ this.onToolsChanged.emit({ added: [], removed });
153
+ }
154
+ /**
155
+ * Get all tool metadata in the catalog.
156
+ *
157
+ * Returns a snapshot of all currently registered tools from
158
+ * all sources.
159
+ *
160
+ * @returns Array of all tool metadata
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * const allTools = catalog.getAllMetadata();
165
+ * console.log(`Catalog has ${allTools.length} tools`);
166
+ *
167
+ * // Use for search indexing
168
+ * await searchIndex.index(allTools);
169
+ * ```
170
+ */
171
+ getAllMetadata() {
172
+ return Array.from(this.metadataById.values());
173
+ }
174
+ /**
175
+ * Get metadata for a specific tool.
176
+ *
177
+ * @param id - The tool ID to look up
178
+ * @returns Tool metadata, or null if not found
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * const meta = catalog.getMetadata('local:calculator');
183
+ * if (meta) {
184
+ * console.log(`Found: ${meta.name} - ${meta.description}`);
185
+ * }
186
+ * ```
187
+ */
188
+ getMetadata(id) {
189
+ return this.metadataById.get(id) ?? null;
190
+ }
191
+ /**
192
+ * Get the source for a tool by source ID.
193
+ *
194
+ * Used by the ToolLoader to retrieve tool implementations.
195
+ *
196
+ * @param sourceId - The source ID to look up
197
+ * @returns The tool source, or null if not found
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * const source = catalog.getSource('mcp:github');
202
+ * if (source) {
203
+ * const tool = await source.getTool('create_pr');
204
+ * }
205
+ * ```
206
+ */
207
+ getSource(sourceId) {
208
+ return this.sources.get(sourceId) ?? null;
209
+ }
210
+ /**
211
+ * Handle a source refresh event.
212
+ *
213
+ * @internal
214
+ * @param sourceId - The source that refreshed
215
+ * @param newMetadata - The new metadata from the source
216
+ */
217
+ handleSourceRefresh(sourceId, newMetadata) {
218
+ const oldMetadata = this.metadataBySource.get(sourceId) ?? [];
219
+ const oldIds = new Set(oldMetadata.map(m => m.id));
220
+ const newIds = new Set(newMetadata.map(m => m.id));
221
+ const added = [];
222
+ const removed = [];
223
+ // Find removed tools
224
+ for (const id of oldIds) {
225
+ if (!newIds.has(id)) {
226
+ this.metadataById.delete(id);
227
+ removed.push(id);
228
+ }
229
+ }
230
+ // Find added/updated tools
231
+ for (const tool of newMetadata) {
232
+ if (!oldIds.has(tool.id)) {
233
+ added.push(tool.id);
234
+ }
235
+ this.metadataById.set(tool.id, tool);
236
+ }
237
+ this.metadataBySource.set(sourceId, newMetadata);
238
+ // Emit change event if anything changed
239
+ if (added.length > 0 || removed.length > 0) {
240
+ this.onToolsChanged.emit({ added, removed });
241
+ }
242
+ }
243
+ }
244
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/catalog/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH,OAAO,EAAE,kBAAkB,EAAqB,MAAM,aAAa,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,OAAO,kBAAkB;IAC7B,2CAA2C;IACnC,OAAO,GAA4B,IAAI,GAAG,EAAE,CAAC;IAErD,iDAAiD;IACzC,gBAAgB,GAAgC,IAAI,GAAG,EAAE,CAAC;IAElE,2DAA2D;IACnD,YAAY,GAA8B,IAAI,GAAG,EAAE,CAAC;IAE5D;;;;;OAKG;IACM,cAAc,CAAkC;IAEzD;;;;;;;;;OASG;IACH;QACE,IAAI,CAAC,cAAc,GAAG,kBAAkB,EAAE,CAAC;IAC7C,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,QAAQ,CAAC,MAAkB;QAC/B,IAAI,IAAI,CAAC,OA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