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.
- package/CHANGELOG.md +27 -0
- package/LICENSE +21 -0
- package/README.md +641 -0
- package/dist/adapters/agent-protocol.d.ts +149 -0
- package/dist/adapters/agent-protocol.d.ts.map +1 -0
- package/dist/adapters/agent-protocol.js +133 -0
- package/dist/adapters/agent-protocol.js.map +1 -0
- package/dist/adapters/index.d.ts +39 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +42 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/inngest.d.ts +234 -0
- package/dist/adapters/inngest.d.ts.map +1 -0
- package/dist/adapters/inngest.js +276 -0
- package/dist/adapters/inngest.js.map +1 -0
- package/dist/adapters/mastra.d.ts +201 -0
- package/dist/adapters/mastra.d.ts.map +1 -0
- package/dist/adapters/mastra.js +250 -0
- package/dist/adapters/mastra.js.map +1 -0
- package/dist/adapters/types.d.ts +42 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +6 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/adapters/vercel-ai.d.ts +176 -0
- package/dist/adapters/vercel-ai.d.ts.map +1 -0
- package/dist/adapters/vercel-ai.js +244 -0
- package/dist/adapters/vercel-ai.js.map +1 -0
- package/dist/catalog/index.d.ts +177 -0
- package/dist/catalog/index.d.ts.map +1 -0
- package/dist/catalog/index.js +244 -0
- package/dist/catalog/index.js.map +1 -0
- package/dist/graph/agent.d.ts +214 -0
- package/dist/graph/agent.d.ts.map +1 -0
- package/dist/graph/agent.js +196 -0
- package/dist/graph/agent.js.map +1 -0
- package/dist/graph/index.d.ts +5 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +4 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/nodes.d.ts +100 -0
- package/dist/graph/nodes.d.ts.map +1 -0
- package/dist/graph/nodes.js +190 -0
- package/dist/graph/nodes.js.map +1 -0
- package/dist/graph/search-tool.d.ts +34 -0
- package/dist/graph/search-tool.d.ts.map +1 -0
- package/dist/graph/search-tool.js +54 -0
- package/dist/graph/search-tool.js.map +1 -0
- package/dist/graph/state.d.ts +26 -0
- package/dist/graph/state.d.ts.map +1 -0
- package/dist/graph/state.js +29 -0
- package/dist/graph/state.js.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/loader/index.d.ts +172 -0
- package/dist/loader/index.d.ts.map +1 -0
- package/dist/loader/index.js +179 -0
- package/dist/loader/index.js.map +1 -0
- package/dist/loader/loader.d.ts +114 -0
- package/dist/loader/loader.d.ts.map +1 -0
- package/dist/loader/loader.js +185 -0
- package/dist/loader/loader.js.map +1 -0
- package/dist/search/cache.d.ts +76 -0
- package/dist/search/cache.d.ts.map +1 -0
- package/dist/search/cache.js +135 -0
- package/dist/search/cache.js.map +1 -0
- package/dist/search/index.d.ts +63 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +122 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/normalize.d.ts +104 -0
- package/dist/search/normalize.d.ts.map +1 -0
- package/dist/search/normalize.js +211 -0
- package/dist/search/normalize.js.map +1 -0
- package/dist/search/orama.d.ts +256 -0
- package/dist/search/orama.d.ts.map +1 -0
- package/dist/search/orama.js +511 -0
- package/dist/search/orama.js.map +1 -0
- package/dist/search/types.d.ts +96 -0
- package/dist/search/types.d.ts.map +1 -0
- package/dist/search/types.js +8 -0
- package/dist/search/types.js.map +1 -0
- package/dist/sources/dynamic.d.ts +200 -0
- package/dist/sources/dynamic.d.ts.map +1 -0
- package/dist/sources/dynamic.js +194 -0
- package/dist/sources/dynamic.js.map +1 -0
- package/dist/sources/index.d.ts +11 -0
- package/dist/sources/index.d.ts.map +1 -0
- package/dist/sources/index.js +14 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/sources/local.d.ts +128 -0
- package/dist/sources/local.d.ts.map +1 -0
- package/dist/sources/local.js +155 -0
- package/dist/sources/local.js.map +1 -0
- package/dist/sources/mcp.d.ts +438 -0
- package/dist/sources/mcp.d.ts.map +1 -0
- package/dist/sources/mcp.js +438 -0
- package/dist/sources/mcp.js.map +1 -0
- package/dist/sources/types.d.ts +16 -0
- package/dist/sources/types.d.ts.map +1 -0
- package/dist/sources/types.js +7 -0
- package/dist/sources/types.js.map +1 -0
- package/dist/sources/with-metadata.d.ts +7 -0
- package/dist/sources/with-metadata.d.ts.map +1 -0
- package/dist/sources/with-metadata.js +7 -0
- package/dist/sources/with-metadata.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types.d.ts +700 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +97 -0
- package/dist/types.js.map +1 -0
- package/package.json +118 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamicSource module.
|
|
3
|
+
*
|
|
4
|
+
* Provides a ToolSource implementation for lazy-loading tools that
|
|
5
|
+
* aren't stored in memory.
|
|
6
|
+
*
|
|
7
|
+
* @module sources/dynamic
|
|
8
|
+
*/
|
|
9
|
+
import type { StructuredTool } from "@langchain/core/tools";
|
|
10
|
+
import type { ToolSource, ToolMetadata } from "../types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Configuration options for DynamicSource.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const options: DynamicSourceOptions = {
|
|
17
|
+
* metadata: [
|
|
18
|
+
* { id: 'plugin1', name: 'plugin1', description: '...', source: 'dynamic', sourceId: 'dynamic' },
|
|
19
|
+
* { id: 'plugin2', name: 'plugin2', description: '...', source: 'dynamic', sourceId: 'dynamic' },
|
|
20
|
+
* ],
|
|
21
|
+
* loader: async (id) => {
|
|
22
|
+
* const module = await import(`./plugins/${id}.js`);
|
|
23
|
+
* return module.default;
|
|
24
|
+
* },
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export interface DynamicSourceOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Pre-known tool metadata.
|
|
31
|
+
*
|
|
32
|
+
* This metadata is used for indexing and search. The actual tool
|
|
33
|
+
* implementations are loaded on-demand via the loader function.
|
|
34
|
+
*/
|
|
35
|
+
metadata: ToolMetadata[];
|
|
36
|
+
/**
|
|
37
|
+
* Function to load a tool by ID.
|
|
38
|
+
*
|
|
39
|
+
* Called when the tool is needed for execution. Should return
|
|
40
|
+
* a StructuredTool instance or throw if loading fails.
|
|
41
|
+
*
|
|
42
|
+
* @param id - The bare tool name (without 'dynamic:' prefix)
|
|
43
|
+
* @returns Promise resolving to the loaded tool
|
|
44
|
+
*/
|
|
45
|
+
loader: (id: string) => Promise<StructuredTool>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* DynamicSource provides lazy-loaded tools.
|
|
49
|
+
*
|
|
50
|
+
* Metadata is provided upfront for indexing and search, but actual
|
|
51
|
+
* tool implementations are loaded on-demand via a loader function.
|
|
52
|
+
*
|
|
53
|
+
* This is useful for:
|
|
54
|
+
* - Large tool collections where loading all tools upfront is expensive
|
|
55
|
+
* - Tools that require dynamic initialization or configuration
|
|
56
|
+
* - Plugin systems where tools are loaded from external sources
|
|
57
|
+
* - Code-split modules that shouldn't be loaded until needed
|
|
58
|
+
*
|
|
59
|
+
* @example Basic usage
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { DynamicSource } from '@repo/bigtool-ts';
|
|
62
|
+
*
|
|
63
|
+
* const source = new DynamicSource({
|
|
64
|
+
* metadata: [
|
|
65
|
+
* {
|
|
66
|
+
* id: 'heavy-tool',
|
|
67
|
+
* name: 'heavy-tool',
|
|
68
|
+
* description: 'A tool that takes a long time to load',
|
|
69
|
+
* source: 'dynamic',
|
|
70
|
+
* sourceId: 'dynamic',
|
|
71
|
+
* },
|
|
72
|
+
* ],
|
|
73
|
+
* loader: async (id) => {
|
|
74
|
+
* // Only loaded when actually needed
|
|
75
|
+
* const { createHeavyTool } = await import('./heavy-tool.js');
|
|
76
|
+
* return createHeavyTool();
|
|
77
|
+
* },
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* await catalog.register(source);
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Plugin system
|
|
84
|
+
* ```typescript
|
|
85
|
+
* // Load plugin manifest
|
|
86
|
+
* const plugins = await loadPluginManifest();
|
|
87
|
+
*
|
|
88
|
+
* const source = new DynamicSource({
|
|
89
|
+
* metadata: plugins.map(p => ({
|
|
90
|
+
* id: p.name,
|
|
91
|
+
* name: p.name,
|
|
92
|
+
* description: p.description,
|
|
93
|
+
* categories: p.categories,
|
|
94
|
+
* source: 'dynamic',
|
|
95
|
+
* sourceId: 'dynamic',
|
|
96
|
+
* })),
|
|
97
|
+
* loader: async (name) => {
|
|
98
|
+
* return await loadPlugin(name);
|
|
99
|
+
* },
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare class DynamicSource implements ToolSource {
|
|
104
|
+
/**
|
|
105
|
+
* Unique identifier for this source.
|
|
106
|
+
*
|
|
107
|
+
* Always 'dynamic' for DynamicSource instances.
|
|
108
|
+
*/
|
|
109
|
+
readonly id = "dynamic";
|
|
110
|
+
/** @internal Cached metadata */
|
|
111
|
+
private metadata;
|
|
112
|
+
/** @internal Loader function */
|
|
113
|
+
private loader;
|
|
114
|
+
/** @internal Map for fast metadata lookup */
|
|
115
|
+
private metadataMap;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a new DynamicSource.
|
|
118
|
+
*
|
|
119
|
+
* @param config - Configuration with metadata and loader function
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const source = new DynamicSource({
|
|
124
|
+
* metadata: toolMetadata,
|
|
125
|
+
* loader: async (id) => loadTool(id),
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
constructor(config: DynamicSourceOptions);
|
|
130
|
+
/**
|
|
131
|
+
* Get metadata for all tools.
|
|
132
|
+
*
|
|
133
|
+
* Returns the pre-configured metadata immediately without any
|
|
134
|
+
* network or async operations. This makes indexing fast.
|
|
135
|
+
*
|
|
136
|
+
* @returns Promise resolving to array of tool metadata
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const metadata = await source.getMetadata();
|
|
141
|
+
* console.log(`${metadata.length} dynamic tools available`);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
getMetadata(): Promise<ToolMetadata[]>;
|
|
145
|
+
/**
|
|
146
|
+
* Get a tool by its ID.
|
|
147
|
+
*
|
|
148
|
+
* Delegates to the loader function provided at construction.
|
|
149
|
+
* Returns null if the tool ID is not in the metadata.
|
|
150
|
+
*
|
|
151
|
+
* @param id - Tool ID ('dynamic:name' or bare 'name')
|
|
152
|
+
* @returns Promise resolving to the loaded tool, or null if not found
|
|
153
|
+
* @throws DynamicSourceError if the loader function fails
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const tool = await source.getTool('my-plugin');
|
|
158
|
+
* if (tool) {
|
|
159
|
+
* const result = await tool.invoke(input);
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
getTool(id: string): Promise<StructuredTool | null>;
|
|
164
|
+
/**
|
|
165
|
+
* Extract the tool name from an ID.
|
|
166
|
+
*
|
|
167
|
+
* @internal
|
|
168
|
+
* @param id - Full or bare tool ID
|
|
169
|
+
* @returns Bare tool name
|
|
170
|
+
*/
|
|
171
|
+
private extractName;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Error thrown when dynamic tool loading fails.
|
|
175
|
+
*
|
|
176
|
+
* This error wraps any error thrown by the loader function,
|
|
177
|
+
* adding context about which tool failed to load.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* try {
|
|
182
|
+
* const tool = await source.getTool('broken-plugin');
|
|
183
|
+
* } catch (error) {
|
|
184
|
+
* if (error instanceof DynamicSourceError) {
|
|
185
|
+
* console.error('Plugin failed to load:', error.message);
|
|
186
|
+
* console.error('Original error:', error.cause);
|
|
187
|
+
* }
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export declare class DynamicSourceError extends Error {
|
|
192
|
+
/**
|
|
193
|
+
* Creates a new DynamicSourceError.
|
|
194
|
+
*
|
|
195
|
+
* @param message - Error message with context
|
|
196
|
+
* @param options - Error options (e.g., cause)
|
|
197
|
+
*/
|
|
198
|
+
constructor(message: string, options?: ErrorOptions);
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=dynamic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dynamic.d.ts","sourceRoot":"","sources":["../../src/sources/dynamic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,QAAQ,EAAE,YAAY,EAAE,CAAC;IAEzB;;;;;;;;OAQG;IACH,MAAM,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;CACjD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,qBAAa,aAAc,YAAW,UAAU;IAC9C;;;;OAIG;IACH,QAAQ,CAAC,EAAE,aAAa;IAExB,gCAAgC;IAChC,OAAO,CAAC,QAAQ,CAAiB;IAEjC,gCAAgC;IAChC,OAAO,CAAC,MAAM,CAA0C;IAExD,6CAA6C;IAC7C,OAAO,CAAC,WAAW,CAA4B;IAE/C;;;;;;;;;;;;OAYG;gBACS,MAAM,EAAE,oBAAoB;IAiBxC;;;;;;;;;;;;;OAaG;IACG,WAAW,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;IAI5C;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;IAoBzD;;;;;;OAMG;IACH,OAAO,CAAC,WAAW;CAGpB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C;;;;;OAKG;gBACS,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD"}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamicSource module.
|
|
3
|
+
*
|
|
4
|
+
* Provides a ToolSource implementation for lazy-loading tools that
|
|
5
|
+
* aren't stored in memory.
|
|
6
|
+
*
|
|
7
|
+
* @module sources/dynamic
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* DynamicSource provides lazy-loaded tools.
|
|
11
|
+
*
|
|
12
|
+
* Metadata is provided upfront for indexing and search, but actual
|
|
13
|
+
* tool implementations are loaded on-demand via a loader function.
|
|
14
|
+
*
|
|
15
|
+
* This is useful for:
|
|
16
|
+
* - Large tool collections where loading all tools upfront is expensive
|
|
17
|
+
* - Tools that require dynamic initialization or configuration
|
|
18
|
+
* - Plugin systems where tools are loaded from external sources
|
|
19
|
+
* - Code-split modules that shouldn't be loaded until needed
|
|
20
|
+
*
|
|
21
|
+
* @example Basic usage
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { DynamicSource } from '@repo/bigtool-ts';
|
|
24
|
+
*
|
|
25
|
+
* const source = new DynamicSource({
|
|
26
|
+
* metadata: [
|
|
27
|
+
* {
|
|
28
|
+
* id: 'heavy-tool',
|
|
29
|
+
* name: 'heavy-tool',
|
|
30
|
+
* description: 'A tool that takes a long time to load',
|
|
31
|
+
* source: 'dynamic',
|
|
32
|
+
* sourceId: 'dynamic',
|
|
33
|
+
* },
|
|
34
|
+
* ],
|
|
35
|
+
* loader: async (id) => {
|
|
36
|
+
* // Only loaded when actually needed
|
|
37
|
+
* const { createHeavyTool } = await import('./heavy-tool.js');
|
|
38
|
+
* return createHeavyTool();
|
|
39
|
+
* },
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* await catalog.register(source);
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example Plugin system
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // Load plugin manifest
|
|
48
|
+
* const plugins = await loadPluginManifest();
|
|
49
|
+
*
|
|
50
|
+
* const source = new DynamicSource({
|
|
51
|
+
* metadata: plugins.map(p => ({
|
|
52
|
+
* id: p.name,
|
|
53
|
+
* name: p.name,
|
|
54
|
+
* description: p.description,
|
|
55
|
+
* categories: p.categories,
|
|
56
|
+
* source: 'dynamic',
|
|
57
|
+
* sourceId: 'dynamic',
|
|
58
|
+
* })),
|
|
59
|
+
* loader: async (name) => {
|
|
60
|
+
* return await loadPlugin(name);
|
|
61
|
+
* },
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export class DynamicSource {
|
|
66
|
+
/**
|
|
67
|
+
* Unique identifier for this source.
|
|
68
|
+
*
|
|
69
|
+
* Always 'dynamic' for DynamicSource instances.
|
|
70
|
+
*/
|
|
71
|
+
id = "dynamic";
|
|
72
|
+
/** @internal Cached metadata */
|
|
73
|
+
metadata;
|
|
74
|
+
/** @internal Loader function */
|
|
75
|
+
loader;
|
|
76
|
+
/** @internal Map for fast metadata lookup */
|
|
77
|
+
metadataMap;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a new DynamicSource.
|
|
80
|
+
*
|
|
81
|
+
* @param config - Configuration with metadata and loader function
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const source = new DynamicSource({
|
|
86
|
+
* metadata: toolMetadata,
|
|
87
|
+
* loader: async (id) => loadTool(id),
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
constructor(config) {
|
|
92
|
+
// Normalize metadata to ensure correct ID format and source
|
|
93
|
+
this.metadata = config.metadata.map((m) => ({
|
|
94
|
+
...m,
|
|
95
|
+
id: m.id.startsWith("dynamic:") ? m.id : `dynamic:${m.id}`,
|
|
96
|
+
source: "dynamic",
|
|
97
|
+
sourceId: this.id,
|
|
98
|
+
}));
|
|
99
|
+
// Build lookup map for efficient access
|
|
100
|
+
this.metadataMap = new Map(this.metadata.map((m) => [this.extractName(m.id), m]));
|
|
101
|
+
this.loader = config.loader;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get metadata for all tools.
|
|
105
|
+
*
|
|
106
|
+
* Returns the pre-configured metadata immediately without any
|
|
107
|
+
* network or async operations. This makes indexing fast.
|
|
108
|
+
*
|
|
109
|
+
* @returns Promise resolving to array of tool metadata
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const metadata = await source.getMetadata();
|
|
114
|
+
* console.log(`${metadata.length} dynamic tools available`);
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
async getMetadata() {
|
|
118
|
+
return this.metadata;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get a tool by its ID.
|
|
122
|
+
*
|
|
123
|
+
* Delegates to the loader function provided at construction.
|
|
124
|
+
* Returns null if the tool ID is not in the metadata.
|
|
125
|
+
*
|
|
126
|
+
* @param id - Tool ID ('dynamic:name' or bare 'name')
|
|
127
|
+
* @returns Promise resolving to the loaded tool, or null if not found
|
|
128
|
+
* @throws DynamicSourceError if the loader function fails
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* const tool = await source.getTool('my-plugin');
|
|
133
|
+
* if (tool) {
|
|
134
|
+
* const result = await tool.invoke(input);
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
async getTool(id) {
|
|
139
|
+
const name = this.extractName(id);
|
|
140
|
+
// Check if tool exists in our metadata
|
|
141
|
+
if (!this.metadataMap.has(name)) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
return await this.loader(name);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// Re-throw with context
|
|
149
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
150
|
+
throw new DynamicSourceError(`Failed to load dynamic tool "${name}": ${message}`, { cause: error });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Extract the tool name from an ID.
|
|
155
|
+
*
|
|
156
|
+
* @internal
|
|
157
|
+
* @param id - Full or bare tool ID
|
|
158
|
+
* @returns Bare tool name
|
|
159
|
+
*/
|
|
160
|
+
extractName(id) {
|
|
161
|
+
return id.startsWith("dynamic:") ? id.slice(8) : id;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Error thrown when dynamic tool loading fails.
|
|
166
|
+
*
|
|
167
|
+
* This error wraps any error thrown by the loader function,
|
|
168
|
+
* adding context about which tool failed to load.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* try {
|
|
173
|
+
* const tool = await source.getTool('broken-plugin');
|
|
174
|
+
* } catch (error) {
|
|
175
|
+
* if (error instanceof DynamicSourceError) {
|
|
176
|
+
* console.error('Plugin failed to load:', error.message);
|
|
177
|
+
* console.error('Original error:', error.cause);
|
|
178
|
+
* }
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export class DynamicSourceError extends Error {
|
|
183
|
+
/**
|
|
184
|
+
* Creates a new DynamicSourceError.
|
|
185
|
+
*
|
|
186
|
+
* @param message - Error message with context
|
|
187
|
+
* @param options - Error options (e.g., cause)
|
|
188
|
+
*/
|
|
189
|
+
constructor(message, options) {
|
|
190
|
+
super(message, options);
|
|
191
|
+
this.name = "DynamicSourceError";
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=dynamic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dynamic.js","sourceRoot":"","sources":["../../src/sources/dynamic.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2CH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,MAAM,OAAO,aAAa;IACxB;;;;OAIG;IACM,EAAE,GAAG,SAAS,CAAC;IAExB,gCAAgC;IACxB,QAAQ,CAAiB;IAEjC,gCAAgC;IACxB,MAAM,CAA0C;IAExD,6CAA6C;IACrC,WAAW,CAA4B;IAE/C;;;;;;;;;;;;OAYG;IACH,YAAY,MAA4B;QACtC,4DAA4D;QAC5D,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC1C,GAAG,CAAC;YACJ,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE;YAC1D,MAAM,EAAE,SAAkB;YAC1B,QAAQ,EAAE,IAAI,CAAC,EAAE;SAClB,CAAC,CAAC,CAAC;QAEJ,wCAAwC;QACxC,IAAI,CAAC,WAAW,GAAG,IAAI,GAAG,CACxB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CACtD,CAAC;QAEF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,KAAK,CAAC,WAAW;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,KAAK,CAAC,OAAO,CAAC,EAAU;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;QAElC,uCAAuC;QACvC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,wBAAwB;YACxB,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACvE,MAAM,IAAI,kBAAkB,CAC1B,gCAAgC,IAAI,MAAM,OAAO,EAAE,EACnD,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACK,WAAW,CAAC,EAAU;QAC5B,OAAO,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACtD,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C;;;;;OAKG;IACH,YAAY,OAAe,EAAE,OAAsB;QACjD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Sources - Different ways to provide tools to bigtool-ts.
|
|
3
|
+
*
|
|
4
|
+
* @module sources
|
|
5
|
+
*/
|
|
6
|
+
export { LocalSource } from './local.js';
|
|
7
|
+
export { MCPSource, createMCPSource, MCPSourceError, MCPToolError, type MCPSourceOptions, type MCPClient, type MCPServerConfig, type MCPStdioConfig, type MCPSSEConfig, type MCPWebSocketConfig, } from './mcp.js';
|
|
8
|
+
export { DynamicSource, DynamicSourceError, type DynamicSourceOptions, } from './dynamic.js';
|
|
9
|
+
export { withMetadata } from './with-metadata.js';
|
|
10
|
+
export type { ToolSource, ToolMetadata, ToolEnhancement, EnhancedTool, } from './types.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sources/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EACL,SAAS,EACT,eAAe,EACf,cAAc,EACd,YAAY,EACZ,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,KAAK,oBAAoB,GAC1B,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD,YAAY,EACV,UAAU,EACV,YAAY,EACZ,eAAe,EACf,YAAY,GACb,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Sources - Different ways to provide tools to bigtool-ts.
|
|
3
|
+
*
|
|
4
|
+
* @module sources
|
|
5
|
+
*/
|
|
6
|
+
// LocalSource - Wraps in-memory StructuredTool instances
|
|
7
|
+
export { LocalSource } from './local.js';
|
|
8
|
+
// MCPSource - Connects to MCP servers
|
|
9
|
+
export { MCPSource, createMCPSource, MCPSourceError, MCPToolError, } from './mcp.js';
|
|
10
|
+
// DynamicSource - Lazy-loads tools on demand
|
|
11
|
+
export { DynamicSource, DynamicSourceError, } from './dynamic.js';
|
|
12
|
+
// Re-export withMetadata utility for convenience
|
|
13
|
+
export { withMetadata } from './with-metadata.js';
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sources/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,yDAAyD;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,sCAAsC;AACtC,OAAO,EACL,SAAS,EACT,eAAe,EACf,cAAc,EACd,YAAY,GAOb,MAAM,UAAU,CAAC;AAElB,6CAA6C;AAC7C,OAAO,EACL,aAAa,EACb,kBAAkB,GAEnB,MAAM,cAAc,CAAC;AAEtB,iDAAiD;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalSource module.
|
|
3
|
+
*
|
|
4
|
+
* Provides a ToolSource implementation for in-memory StructuredTool instances.
|
|
5
|
+
*
|
|
6
|
+
* @module sources/local
|
|
7
|
+
*/
|
|
8
|
+
import type { StructuredTool } from '@langchain/core/tools';
|
|
9
|
+
import type { ToolSource, ToolMetadata } from '../types.js';
|
|
10
|
+
/**
|
|
11
|
+
* A tool source backed by local StructuredTool instances.
|
|
12
|
+
*
|
|
13
|
+
* LocalSource is the simplest way to add tools to the discovery system.
|
|
14
|
+
* It wraps an array of StructuredTool instances and exposes them through
|
|
15
|
+
* the ToolSource interface.
|
|
16
|
+
*
|
|
17
|
+
* Tools are stored in memory and returned immediately when requested.
|
|
18
|
+
* Use {@link withMetadata} to add categories and keywords for better
|
|
19
|
+
* search matching.
|
|
20
|
+
*
|
|
21
|
+
* @example Basic usage
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { LocalSource } from '@repo/bigtool-ts';
|
|
24
|
+
*
|
|
25
|
+
* const source = new LocalSource([
|
|
26
|
+
* calculatorTool,
|
|
27
|
+
* weatherTool,
|
|
28
|
+
* searchTool,
|
|
29
|
+
* ]);
|
|
30
|
+
*
|
|
31
|
+
* // Register with catalog
|
|
32
|
+
* await catalog.register(source);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example With custom ID
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const utilitySource = new LocalSource(utilityTools, 'utilities');
|
|
38
|
+
* const aiSource = new LocalSource(aiTools, 'ai');
|
|
39
|
+
*
|
|
40
|
+
* // Tools will be namespaced: 'utilities:calculator', 'ai:chat'
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @example With enhanced metadata
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { LocalSource, withMetadata } from '@repo/bigtool-ts';
|
|
46
|
+
*
|
|
47
|
+
* const tools = [
|
|
48
|
+
* withMetadata(createPRTool, {
|
|
49
|
+
* categories: ['github', 'git'],
|
|
50
|
+
* keywords: ['PR', 'pull request', 'merge'],
|
|
51
|
+
* }),
|
|
52
|
+
* withMetadata(reviewPRTool, {
|
|
53
|
+
* categories: ['github', 'git'],
|
|
54
|
+
* keywords: ['review', 'approve', 'comment'],
|
|
55
|
+
* }),
|
|
56
|
+
* ];
|
|
57
|
+
*
|
|
58
|
+
* const source = new LocalSource(tools, 'github');
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare class LocalSource implements ToolSource {
|
|
62
|
+
/**
|
|
63
|
+
* Unique identifier for this source.
|
|
64
|
+
*
|
|
65
|
+
* Used to namespace tool IDs (e.g., 'local:calculator').
|
|
66
|
+
*/
|
|
67
|
+
readonly id: string;
|
|
68
|
+
/** @internal Map of tool name to StructuredTool */
|
|
69
|
+
private tools;
|
|
70
|
+
/** @internal Cached metadata for all tools */
|
|
71
|
+
private metadata;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new LocalSource.
|
|
74
|
+
*
|
|
75
|
+
* @param tools - Array of StructuredTool instances to expose
|
|
76
|
+
* @param id - Optional source identifier (default: 'local')
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const source = new LocalSource([tool1, tool2]);
|
|
81
|
+
* const namedSource = new LocalSource([tool1, tool2], 'my-tools');
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
constructor(tools: StructuredTool[], id?: string);
|
|
85
|
+
/**
|
|
86
|
+
* Extracts metadata from a StructuredTool.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
* @param tool - The tool to extract metadata from
|
|
90
|
+
* @returns Tool metadata for indexing
|
|
91
|
+
*/
|
|
92
|
+
private extractMetadata;
|
|
93
|
+
/**
|
|
94
|
+
* Get metadata for all tools in this source.
|
|
95
|
+
*
|
|
96
|
+
* Returns cached metadata extracted during construction.
|
|
97
|
+
* This is synchronous internally but returns a Promise for
|
|
98
|
+
* interface compatibility.
|
|
99
|
+
*
|
|
100
|
+
* @returns Promise resolving to array of tool metadata
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const metadata = await source.getMetadata();
|
|
105
|
+
* console.log(`Found ${metadata.length} tools`);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
getMetadata(): Promise<ToolMetadata[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Get a tool by its ID.
|
|
111
|
+
*
|
|
112
|
+
* Accepts both prefixed IDs (e.g., 'local:calculator') and bare
|
|
113
|
+
* tool names (e.g., 'calculator'). Returns the tool immediately
|
|
114
|
+
* from memory.
|
|
115
|
+
*
|
|
116
|
+
* @param id - Tool ID or bare name
|
|
117
|
+
* @returns Promise resolving to the tool, or null if not found
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const tool = await source.getTool('local:calculator');
|
|
122
|
+
* // or
|
|
123
|
+
* const tool = await source.getTool('calculator');
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
getTool(id: string): Promise<StructuredTool | null>;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/sources/local.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAmB,MAAM,aAAa,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,qBAAa,WAAY,YAAW,UAAU;IAC5C;;;;OAIG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB,mDAAmD;IACnD,OAAO,CAAC,KAAK,CAA8B;IAE3C,8CAA8C;IAC9C,OAAO,CAAC,QAAQ,CAAiB;IAEjC;;;;;;;;;;;OAWG;gBACS,KAAK,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC,EAAE,MAAM;IAWhD;;;;;;OAMG;IACH,OAAO,CAAC,eAAe;IAwBvB;;;;;;;;;;;;;;OAcG;IACG,WAAW,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;IAI5C;;;;;;;;;;;;;;;;OAgBG;IACG,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;CAK1D"}
|