sis-tools 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/dist/index.cjs +1531 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +869 -0
- package/dist/index.d.ts +869 -0
- package/dist/index.js +1476 -0
- package/dist/index.js.map +1 -0
- package/package.json +86 -0
- package/src/embeddings/base.ts +47 -0
- package/src/embeddings/cohere.ts +79 -0
- package/src/embeddings/google.ts +67 -0
- package/src/embeddings/index.ts +43 -0
- package/src/embeddings/openai.ts +87 -0
- package/src/formatters.ts +249 -0
- package/src/hooks.ts +341 -0
- package/src/index.ts +104 -0
- package/src/optional-peer-deps.d.ts +17 -0
- package/src/scoring.ts +198 -0
- package/src/sis.ts +572 -0
- package/src/store.ts +134 -0
- package/src/types.ts +136 -0
- package/src/validators.ts +484 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,869 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for SIS
|
|
3
|
+
*/
|
|
4
|
+
interface ToolParameter {
|
|
5
|
+
type: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
required?: boolean;
|
|
8
|
+
default?: unknown;
|
|
9
|
+
}
|
|
10
|
+
interface ToolParameters {
|
|
11
|
+
[key: string]: ToolParameter;
|
|
12
|
+
}
|
|
13
|
+
interface ToolExample {
|
|
14
|
+
query: string;
|
|
15
|
+
input?: Record<string, unknown>;
|
|
16
|
+
output?: unknown;
|
|
17
|
+
}
|
|
18
|
+
interface ToolMetadata {
|
|
19
|
+
version?: string;
|
|
20
|
+
author?: string;
|
|
21
|
+
tags?: string[];
|
|
22
|
+
priority?: number;
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
interface Tool {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
parameters: ToolParameters;
|
|
29
|
+
returns?: Record<string, unknown>;
|
|
30
|
+
semanticHints: string[];
|
|
31
|
+
examples: ToolExample[];
|
|
32
|
+
handler?: ToolHandler;
|
|
33
|
+
metadata: ToolMetadata;
|
|
34
|
+
embedding?: number[];
|
|
35
|
+
}
|
|
36
|
+
type ToolHandler = (params: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
37
|
+
interface ToolMatch {
|
|
38
|
+
tool: Tool;
|
|
39
|
+
score: number;
|
|
40
|
+
}
|
|
41
|
+
interface ResolvedTool {
|
|
42
|
+
name: string;
|
|
43
|
+
schema: ToolSchema;
|
|
44
|
+
score: number;
|
|
45
|
+
handler?: ToolHandler;
|
|
46
|
+
}
|
|
47
|
+
interface ToolSchema {
|
|
48
|
+
name: string;
|
|
49
|
+
description: string;
|
|
50
|
+
parameters: {
|
|
51
|
+
type: "object";
|
|
52
|
+
properties: Record<string, unknown>;
|
|
53
|
+
required: string[];
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
interface OpenAIToolSchema {
|
|
57
|
+
type: "function";
|
|
58
|
+
function: ToolSchema;
|
|
59
|
+
}
|
|
60
|
+
interface AnthropicToolSchema {
|
|
61
|
+
name: string;
|
|
62
|
+
description: string;
|
|
63
|
+
input_schema: {
|
|
64
|
+
type: "object";
|
|
65
|
+
properties: Record<string, unknown>;
|
|
66
|
+
required: string[];
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Convert a Tool to its JSON schema representation
|
|
71
|
+
*/
|
|
72
|
+
declare function toSchema(tool: Tool): ToolSchema;
|
|
73
|
+
/**
|
|
74
|
+
* Convert a Tool to OpenAI function calling format
|
|
75
|
+
*/
|
|
76
|
+
declare function toOpenAISchema(tool: Tool): OpenAIToolSchema;
|
|
77
|
+
/**
|
|
78
|
+
* Convert a Tool to Anthropic tool use format
|
|
79
|
+
*/
|
|
80
|
+
declare function toAnthropicSchema(tool: Tool): AnthropicToolSchema;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Base embedding provider interface
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
interface EmbeddingProvider {
|
|
87
|
+
/**
|
|
88
|
+
* Generate embedding for a single text
|
|
89
|
+
*/
|
|
90
|
+
embed(text: string): Promise<number[]>;
|
|
91
|
+
/**
|
|
92
|
+
* Generate embeddings for multiple texts
|
|
93
|
+
*/
|
|
94
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
95
|
+
/**
|
|
96
|
+
* The dimensionality of embeddings
|
|
97
|
+
*/
|
|
98
|
+
readonly dimensions: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* OpenAI embedding provider
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
interface OpenAIEmbeddingsOptions {
|
|
106
|
+
model?: string;
|
|
107
|
+
apiKey?: string;
|
|
108
|
+
dimensions?: number;
|
|
109
|
+
}
|
|
110
|
+
declare class OpenAIEmbeddings implements EmbeddingProvider {
|
|
111
|
+
private client;
|
|
112
|
+
private model;
|
|
113
|
+
private apiKey?;
|
|
114
|
+
readonly dimensions: number;
|
|
115
|
+
constructor(options?: OpenAIEmbeddingsOptions);
|
|
116
|
+
private ensureClient;
|
|
117
|
+
embed(text: string): Promise<number[]>;
|
|
118
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Cohere embedding provider
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
interface CohereEmbeddingsOptions {
|
|
126
|
+
model?: string;
|
|
127
|
+
apiKey?: string;
|
|
128
|
+
inputType?: "search_query" | "search_document" | "classification" | "clustering";
|
|
129
|
+
}
|
|
130
|
+
declare class CohereEmbeddings implements EmbeddingProvider {
|
|
131
|
+
private client;
|
|
132
|
+
private model;
|
|
133
|
+
private inputType;
|
|
134
|
+
private apiKey?;
|
|
135
|
+
readonly dimensions: number;
|
|
136
|
+
constructor(options?: CohereEmbeddingsOptions);
|
|
137
|
+
private ensureClient;
|
|
138
|
+
embed(text: string): Promise<number[]>;
|
|
139
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Google embedding provider
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
interface GoogleEmbeddingsOptions {
|
|
147
|
+
model?: string;
|
|
148
|
+
apiKey?: string;
|
|
149
|
+
}
|
|
150
|
+
declare class GoogleEmbeddings implements EmbeddingProvider {
|
|
151
|
+
private client;
|
|
152
|
+
private model;
|
|
153
|
+
private apiKey?;
|
|
154
|
+
readonly dimensions: number;
|
|
155
|
+
constructor(options?: GoogleEmbeddingsOptions);
|
|
156
|
+
private ensureClient;
|
|
157
|
+
embed(text: string): Promise<number[]>;
|
|
158
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Embedding providers for SIS
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
type ProviderName = "openai" | "cohere" | "google";
|
|
166
|
+
interface ProviderOptions {
|
|
167
|
+
model?: string;
|
|
168
|
+
apiKey?: string;
|
|
169
|
+
dimensions?: number;
|
|
170
|
+
[key: string]: unknown;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Factory to get an embedding provider by name
|
|
174
|
+
*/
|
|
175
|
+
declare function getProvider(name: ProviderName, options?: ProviderOptions): EmbeddingProvider;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Similarity and scoring functions for semantic search
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Interface for custom similarity functions
|
|
183
|
+
*/
|
|
184
|
+
interface SimilarityFunction {
|
|
185
|
+
/**
|
|
186
|
+
* Compute similarity score between query and tool embeddings
|
|
187
|
+
* @param queryEmbedding - The query vector
|
|
188
|
+
* @param toolEmbedding - The tool's embedding vector
|
|
189
|
+
* @returns Similarity score, typically in range [0, 1]
|
|
190
|
+
*/
|
|
191
|
+
compute(queryEmbedding: number[], toolEmbedding: number[]): number;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Interface for custom scoring/ranking functions
|
|
195
|
+
*/
|
|
196
|
+
interface ScoringFunction {
|
|
197
|
+
/**
|
|
198
|
+
* Apply custom scoring logic to similarity score
|
|
199
|
+
* @param similarity - Raw similarity score from SimilarityFunction
|
|
200
|
+
* @param tool - The tool being scored (access metadata here)
|
|
201
|
+
* @returns Final score for ranking. Higher is better.
|
|
202
|
+
*/
|
|
203
|
+
score(similarity: number, tool: Tool): number;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Default cosine similarity implementation
|
|
207
|
+
*/
|
|
208
|
+
declare class CosineSimilarity implements SimilarityFunction {
|
|
209
|
+
compute(a: number[], b: number[]): number;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Euclidean distance converted to similarity score
|
|
213
|
+
* Uses: similarity = 1 / (1 + distance)
|
|
214
|
+
*/
|
|
215
|
+
declare class EuclideanSimilarity implements SimilarityFunction {
|
|
216
|
+
compute(a: number[], b: number[]): number;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Dot product similarity (unnormalized cosine)
|
|
220
|
+
*/
|
|
221
|
+
declare class DotProductSimilarity implements SimilarityFunction {
|
|
222
|
+
compute(a: number[], b: number[]): number;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Default priority-based scoring
|
|
226
|
+
* Multiplies similarity by tool's priority metadata
|
|
227
|
+
*/
|
|
228
|
+
declare class PriorityScoring implements ScoringFunction {
|
|
229
|
+
private maxScore;
|
|
230
|
+
constructor(maxScore?: number);
|
|
231
|
+
score(similarity: number, tool: Tool): number;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Weighted scoring combining multiple factors
|
|
235
|
+
*/
|
|
236
|
+
declare class WeightedScoring implements ScoringFunction {
|
|
237
|
+
private similarityWeight;
|
|
238
|
+
private priorityWeight;
|
|
239
|
+
private maxScore;
|
|
240
|
+
constructor(similarityWeight?: number, priorityWeight?: number, maxScore?: number);
|
|
241
|
+
score(similarity: number, tool: Tool): number;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Scoring that boosts tools matching specified tags
|
|
245
|
+
*/
|
|
246
|
+
declare class TagBoostScoring implements ScoringFunction {
|
|
247
|
+
private boostTags;
|
|
248
|
+
private boostFactor;
|
|
249
|
+
private maxScore;
|
|
250
|
+
constructor(boostTags?: string[], boostFactor?: number, maxScore?: number);
|
|
251
|
+
score(similarity: number, tool: Tool): number;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Combine multiple scoring functions in sequence
|
|
255
|
+
*/
|
|
256
|
+
declare class CompositeScoring implements ScoringFunction {
|
|
257
|
+
private scorers;
|
|
258
|
+
constructor(scorers: ScoringFunction[]);
|
|
259
|
+
score(similarity: number, tool: Tool): number;
|
|
260
|
+
}
|
|
261
|
+
declare const DEFAULT_SIMILARITY: CosineSimilarity;
|
|
262
|
+
declare const DEFAULT_SCORING: PriorityScoring;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Tool formatters for different LLM providers
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Interface for custom tool formatters
|
|
270
|
+
*/
|
|
271
|
+
interface ToolFormatter {
|
|
272
|
+
/** Format name used to identify this formatter */
|
|
273
|
+
readonly name: string;
|
|
274
|
+
/** Format a resolved tool for LLM consumption */
|
|
275
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
276
|
+
/** Format multiple tools (override for batch optimizations) */
|
|
277
|
+
formatBatch?(tools: ResolvedTool[]): Record<string, unknown>[];
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Returns the tool schema as-is
|
|
281
|
+
*/
|
|
282
|
+
declare class RawFormatter implements ToolFormatter {
|
|
283
|
+
readonly name = "raw";
|
|
284
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Formatter for OpenAI function calling format
|
|
288
|
+
*/
|
|
289
|
+
declare class OpenAIFormatter implements ToolFormatter {
|
|
290
|
+
readonly name = "openai";
|
|
291
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Formatter for Anthropic tool use format
|
|
295
|
+
*/
|
|
296
|
+
declare class AnthropicFormatter implements ToolFormatter {
|
|
297
|
+
readonly name = "anthropic";
|
|
298
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Formatter for Google Gemini function calling format
|
|
302
|
+
*/
|
|
303
|
+
declare class GeminiFormatter implements ToolFormatter {
|
|
304
|
+
readonly name = "gemini";
|
|
305
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Formatter for Mistral function calling format (OpenAI-compatible)
|
|
309
|
+
*/
|
|
310
|
+
declare class MistralFormatter implements ToolFormatter {
|
|
311
|
+
readonly name = "mistral";
|
|
312
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Formatter for Llama/Meta function calling format
|
|
316
|
+
*/
|
|
317
|
+
declare class LlamaFormatter implements ToolFormatter {
|
|
318
|
+
readonly name = "llama";
|
|
319
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Formatter for Cohere Command R format
|
|
323
|
+
*/
|
|
324
|
+
declare class CohereFormatter implements ToolFormatter {
|
|
325
|
+
readonly name = "cohere";
|
|
326
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Minimal formatter with just name and description
|
|
330
|
+
*/
|
|
331
|
+
declare class MinimalFormatter implements ToolFormatter {
|
|
332
|
+
readonly name = "minimal";
|
|
333
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Verbose formatter with all available information
|
|
337
|
+
*/
|
|
338
|
+
declare class VerboseFormatter implements ToolFormatter {
|
|
339
|
+
readonly name = "verbose";
|
|
340
|
+
format(tool: ResolvedTool): Record<string, unknown>;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Register a custom formatter
|
|
344
|
+
*/
|
|
345
|
+
declare function registerFormatter(formatter: ToolFormatter): void;
|
|
346
|
+
/**
|
|
347
|
+
* Unregister a formatter by name
|
|
348
|
+
*/
|
|
349
|
+
declare function unregisterFormatter(name: string): boolean;
|
|
350
|
+
/**
|
|
351
|
+
* Get a formatter by name
|
|
352
|
+
*/
|
|
353
|
+
declare function getFormatter(name: string): ToolFormatter;
|
|
354
|
+
/**
|
|
355
|
+
* List all registered formatter names
|
|
356
|
+
*/
|
|
357
|
+
declare function listFormatters(): string[];
|
|
358
|
+
/**
|
|
359
|
+
* Check if a formatter is registered
|
|
360
|
+
*/
|
|
361
|
+
declare function hasFormatter(name: string): boolean;
|
|
362
|
+
/**
|
|
363
|
+
* Format tools using a formatter (by name or instance)
|
|
364
|
+
*/
|
|
365
|
+
declare function formatTools(tools: ResolvedTool[], formatter: string | ToolFormatter): Record<string, unknown>[];
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Middleware/hooks system for SIS
|
|
369
|
+
*/
|
|
370
|
+
/**
|
|
371
|
+
* Types of hooks available in the SIS lifecycle
|
|
372
|
+
*/
|
|
373
|
+
declare enum HookType {
|
|
374
|
+
PRE_REGISTER = "pre_register",
|
|
375
|
+
POST_REGISTER = "post_register",
|
|
376
|
+
PRE_EMBED = "pre_embed",
|
|
377
|
+
POST_EMBED = "post_embed",
|
|
378
|
+
PRE_RESOLVE = "pre_resolve",
|
|
379
|
+
POST_RESOLVE = "post_resolve",
|
|
380
|
+
PRE_SEARCH = "pre_search",
|
|
381
|
+
POST_SEARCH = "post_search",
|
|
382
|
+
PRE_EXECUTE = "pre_execute",
|
|
383
|
+
POST_EXECUTE = "post_execute"
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Context passed to hooks
|
|
387
|
+
*/
|
|
388
|
+
interface HookContext {
|
|
389
|
+
hookType: HookType;
|
|
390
|
+
data: Record<string, unknown>;
|
|
391
|
+
cancelled: boolean;
|
|
392
|
+
error: Error | null;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Create a new hook context
|
|
396
|
+
*/
|
|
397
|
+
declare function createContext(hookType: HookType, data?: Record<string, unknown>): HookContext;
|
|
398
|
+
/**
|
|
399
|
+
* Cancel further processing
|
|
400
|
+
*/
|
|
401
|
+
declare function cancelContext(context: HookContext, reason?: string): void;
|
|
402
|
+
/**
|
|
403
|
+
* Set an error on the context
|
|
404
|
+
*/
|
|
405
|
+
declare function setContextError(context: HookContext, error: Error): void;
|
|
406
|
+
/**
|
|
407
|
+
* Interface for hooks
|
|
408
|
+
*/
|
|
409
|
+
interface Hook {
|
|
410
|
+
/** The hook type this hook handles */
|
|
411
|
+
hookType: HookType;
|
|
412
|
+
/** Hook priority. Higher priority hooks run first. */
|
|
413
|
+
priority?: number;
|
|
414
|
+
/** Execute the hook */
|
|
415
|
+
execute(context: HookContext): Promise<HookContext> | HookContext;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Hook function type
|
|
419
|
+
*/
|
|
420
|
+
type HookFunction = (context: HookContext) => Promise<HookContext> | HookContext;
|
|
421
|
+
/**
|
|
422
|
+
* Create a hook from a function
|
|
423
|
+
*/
|
|
424
|
+
declare function createHook(hookType: HookType, fn: HookFunction, priority?: number): Hook;
|
|
425
|
+
/**
|
|
426
|
+
* Registry for managing hooks
|
|
427
|
+
*/
|
|
428
|
+
declare class HookRegistry {
|
|
429
|
+
private hooks;
|
|
430
|
+
constructor();
|
|
431
|
+
/**
|
|
432
|
+
* Register a hook
|
|
433
|
+
*/
|
|
434
|
+
register(hook: Hook): void;
|
|
435
|
+
/**
|
|
436
|
+
* Register a function as a hook
|
|
437
|
+
*/
|
|
438
|
+
on(hookType: HookType, fn: HookFunction, priority?: number): Hook;
|
|
439
|
+
/**
|
|
440
|
+
* Unregister a hook
|
|
441
|
+
*/
|
|
442
|
+
unregister(hook: Hook): boolean;
|
|
443
|
+
/**
|
|
444
|
+
* Clear hooks
|
|
445
|
+
*/
|
|
446
|
+
clear(hookType?: HookType): void;
|
|
447
|
+
/**
|
|
448
|
+
* Get all hooks for a type
|
|
449
|
+
*/
|
|
450
|
+
getHooks(hookType: HookType): Hook[];
|
|
451
|
+
/**
|
|
452
|
+
* Check if any hooks are registered for a type
|
|
453
|
+
*/
|
|
454
|
+
hasHooks(hookType: HookType): boolean;
|
|
455
|
+
/**
|
|
456
|
+
* Run all hooks of a given type
|
|
457
|
+
*/
|
|
458
|
+
run(hookType: HookType, context: HookContext): Promise<HookContext>;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Hook that logs hook execution
|
|
462
|
+
*/
|
|
463
|
+
declare class LoggingHook implements Hook {
|
|
464
|
+
hookType: HookType;
|
|
465
|
+
priority: number;
|
|
466
|
+
private logger;
|
|
467
|
+
constructor(hookType: HookType, logger?: (message: string) => void);
|
|
468
|
+
execute(context: HookContext): HookContext;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Hook that records execution time
|
|
472
|
+
*/
|
|
473
|
+
declare class TimingHook implements Hook {
|
|
474
|
+
hookType: HookType;
|
|
475
|
+
priority: number;
|
|
476
|
+
private preHook;
|
|
477
|
+
private postHook;
|
|
478
|
+
private timingKey;
|
|
479
|
+
constructor(preHook: HookType, postHook: HookType, timingKey?: string);
|
|
480
|
+
execute(context: HookContext): HookContext;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Hook that collects metrics
|
|
484
|
+
*/
|
|
485
|
+
declare class MetricsHook implements Hook {
|
|
486
|
+
hookType: HookType;
|
|
487
|
+
priority: number;
|
|
488
|
+
count: number;
|
|
489
|
+
errors: number;
|
|
490
|
+
constructor(hookType: HookType);
|
|
491
|
+
execute(context: HookContext): HookContext;
|
|
492
|
+
reset(): void;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Hook that caches resolution results
|
|
496
|
+
*/
|
|
497
|
+
declare class CachingHook implements Hook {
|
|
498
|
+
hookType: HookType;
|
|
499
|
+
priority: number;
|
|
500
|
+
private cache;
|
|
501
|
+
private maxSize;
|
|
502
|
+
constructor(maxSize?: number);
|
|
503
|
+
execute(context: HookContext): HookContext;
|
|
504
|
+
clearCache(): void;
|
|
505
|
+
get cacheSize(): number;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Validation system for SIS tools and parameters
|
|
510
|
+
*/
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Result of a validation operation
|
|
514
|
+
*/
|
|
515
|
+
interface ValidationResult {
|
|
516
|
+
valid: boolean;
|
|
517
|
+
errors: string[];
|
|
518
|
+
warnings: string[];
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Create a validation result
|
|
522
|
+
*/
|
|
523
|
+
declare function createValidationResult(valid: boolean, errors?: string[], warnings?: string[]): ValidationResult;
|
|
524
|
+
/**
|
|
525
|
+
* Merge two validation results
|
|
526
|
+
*/
|
|
527
|
+
declare function mergeValidationResults(a: ValidationResult, b: ValidationResult): ValidationResult;
|
|
528
|
+
/**
|
|
529
|
+
* Interface for validators
|
|
530
|
+
*/
|
|
531
|
+
interface Validator<T = unknown> {
|
|
532
|
+
validate(data: T): ValidationResult;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Validation error
|
|
536
|
+
*/
|
|
537
|
+
declare class ValidationError extends Error {
|
|
538
|
+
result: ValidationResult;
|
|
539
|
+
constructor(result: ValidationResult);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Options for ToolSchemaValidator
|
|
543
|
+
*/
|
|
544
|
+
interface ToolSchemaValidatorOptions {
|
|
545
|
+
requireDescription?: boolean;
|
|
546
|
+
requireParameters?: boolean;
|
|
547
|
+
minDescriptionLength?: number;
|
|
548
|
+
maxDescriptionLength?: number;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Validates tool definitions
|
|
552
|
+
*/
|
|
553
|
+
declare class ToolSchemaValidator implements Validator<Tool> {
|
|
554
|
+
private options;
|
|
555
|
+
constructor(options?: ToolSchemaValidatorOptions);
|
|
556
|
+
validate(tool: Tool): ValidationResult;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Options for ParameterValidator
|
|
560
|
+
*/
|
|
561
|
+
interface ParameterValidatorOptions {
|
|
562
|
+
strict?: boolean;
|
|
563
|
+
allowExtra?: boolean;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Validates execution parameters against tool schema
|
|
567
|
+
*/
|
|
568
|
+
declare class ParameterValidator implements Validator<{
|
|
569
|
+
tool: Tool;
|
|
570
|
+
params: Record<string, unknown>;
|
|
571
|
+
}> {
|
|
572
|
+
private options;
|
|
573
|
+
constructor(options?: ParameterValidatorOptions);
|
|
574
|
+
validate(data: {
|
|
575
|
+
tool: Tool;
|
|
576
|
+
params: Record<string, unknown>;
|
|
577
|
+
}): ValidationResult;
|
|
578
|
+
private checkType;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Options for ResultValidator
|
|
582
|
+
*/
|
|
583
|
+
interface ResultValidatorOptions {
|
|
584
|
+
strict?: boolean;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Validates tool execution results
|
|
588
|
+
*/
|
|
589
|
+
declare class ResultValidator implements Validator<{
|
|
590
|
+
tool: Tool;
|
|
591
|
+
result: unknown;
|
|
592
|
+
}> {
|
|
593
|
+
private options;
|
|
594
|
+
constructor(options?: ResultValidatorOptions);
|
|
595
|
+
validate(data: {
|
|
596
|
+
tool: Tool;
|
|
597
|
+
result: unknown;
|
|
598
|
+
}): ValidationResult;
|
|
599
|
+
private checkReturnType;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Options for EmbeddingValidator
|
|
603
|
+
*/
|
|
604
|
+
interface EmbeddingValidatorOptions {
|
|
605
|
+
expectedDimensions?: number;
|
|
606
|
+
checkNormalization?: boolean;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Validates embedding vectors
|
|
610
|
+
*/
|
|
611
|
+
declare class EmbeddingValidator implements Validator<number[]> {
|
|
612
|
+
private options;
|
|
613
|
+
constructor(options?: EmbeddingValidatorOptions);
|
|
614
|
+
validate(embedding: number[]): ValidationResult;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Combines multiple validators
|
|
618
|
+
*/
|
|
619
|
+
declare class CompositeValidator<T> implements Validator<T> {
|
|
620
|
+
private validators;
|
|
621
|
+
constructor(validators: Validator<T>[]);
|
|
622
|
+
validate(data: T): ValidationResult;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Registry for managing validators
|
|
626
|
+
*/
|
|
627
|
+
declare class ValidatorRegistry {
|
|
628
|
+
schemaValidators: ToolSchemaValidator[];
|
|
629
|
+
parameterValidators: ParameterValidator[];
|
|
630
|
+
resultValidators: ResultValidator[];
|
|
631
|
+
embeddingValidators: EmbeddingValidator[];
|
|
632
|
+
addSchemaValidator(validator: ToolSchemaValidator): void;
|
|
633
|
+
addParameterValidator(validator: ParameterValidator): void;
|
|
634
|
+
addResultValidator(validator: ResultValidator): void;
|
|
635
|
+
addEmbeddingValidator(validator: EmbeddingValidator): void;
|
|
636
|
+
validateTool(tool: Tool): ValidationResult;
|
|
637
|
+
validateParams(tool: Tool, params: Record<string, unknown>): ValidationResult;
|
|
638
|
+
validateResult(tool: Tool, resultValue: unknown): ValidationResult;
|
|
639
|
+
validateEmbedding(embedding: number[]): ValidationResult;
|
|
640
|
+
clear(): void;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Create a registry with strict validators
|
|
644
|
+
*/
|
|
645
|
+
declare function createStrictValidator(): ValidatorRegistry;
|
|
646
|
+
/**
|
|
647
|
+
* Create a registry with lenient validators
|
|
648
|
+
*/
|
|
649
|
+
declare function createLenientValidator(): ValidatorRegistry;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Core SIS implementation
|
|
653
|
+
*/
|
|
654
|
+
|
|
655
|
+
interface SISOptions {
|
|
656
|
+
embeddingProvider?: ProviderName | EmbeddingProvider;
|
|
657
|
+
defaultTopK?: number;
|
|
658
|
+
defaultThreshold?: number;
|
|
659
|
+
providerOptions?: ProviderOptions;
|
|
660
|
+
remoteUrl?: string;
|
|
661
|
+
projectId?: string;
|
|
662
|
+
similarity?: SimilarityFunction;
|
|
663
|
+
scoring?: ScoringFunction;
|
|
664
|
+
validators?: ValidatorRegistry;
|
|
665
|
+
validateOnRegister?: boolean;
|
|
666
|
+
validateOnExecute?: boolean;
|
|
667
|
+
}
|
|
668
|
+
interface RegisterOptions {
|
|
669
|
+
name: string;
|
|
670
|
+
description: string;
|
|
671
|
+
parameters?: ToolParameters;
|
|
672
|
+
handler?: ToolHandler;
|
|
673
|
+
semanticHints?: string[];
|
|
674
|
+
examples?: ToolExample[];
|
|
675
|
+
metadata?: ToolMetadata;
|
|
676
|
+
}
|
|
677
|
+
interface StoreOptions extends RegisterOptions {
|
|
678
|
+
embedding: number[];
|
|
679
|
+
}
|
|
680
|
+
type ResolveFormat = "raw" | "openai" | "anthropic" | string;
|
|
681
|
+
interface ResolveOptions {
|
|
682
|
+
topK?: number;
|
|
683
|
+
threshold?: number;
|
|
684
|
+
format?: ResolveFormat | ToolFormatter;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Semantic Integration System - Intelligent tool resolution
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* const sis = new SIS({ embeddingProvider: 'openai' });
|
|
691
|
+
*
|
|
692
|
+
* sis.register({
|
|
693
|
+
* name: 'web_search',
|
|
694
|
+
* description: 'Search the web for information',
|
|
695
|
+
* parameters: { query: { type: 'string' } },
|
|
696
|
+
* handler: async ({ query }) => searchApi(query)
|
|
697
|
+
* });
|
|
698
|
+
*
|
|
699
|
+
* await sis.initialize();
|
|
700
|
+
* const tools = await sis.resolve("what's the weather?");
|
|
701
|
+
*
|
|
702
|
+
* // With custom formatter
|
|
703
|
+
* const geminiTools = await sis.resolve("query", { format: "gemini" });
|
|
704
|
+
*/
|
|
705
|
+
declare class SIS {
|
|
706
|
+
private _embeddings;
|
|
707
|
+
private _vectorStore;
|
|
708
|
+
private _pendingTools;
|
|
709
|
+
private _defaultTopK;
|
|
710
|
+
private _defaultThreshold;
|
|
711
|
+
private _initialized;
|
|
712
|
+
private _remoteUrl?;
|
|
713
|
+
private _projectId?;
|
|
714
|
+
private _similarity;
|
|
715
|
+
private _scoring;
|
|
716
|
+
private _hooks;
|
|
717
|
+
private _validators?;
|
|
718
|
+
private _validateOnRegister;
|
|
719
|
+
private _validateOnExecute;
|
|
720
|
+
constructor(options?: SISOptions);
|
|
721
|
+
/** Get the hook registry for registering middleware */
|
|
722
|
+
get hooks(): HookRegistry;
|
|
723
|
+
/** Get the validator registry */
|
|
724
|
+
get validators(): ValidatorRegistry | undefined;
|
|
725
|
+
/** Get the current similarity function */
|
|
726
|
+
get similarity(): SimilarityFunction;
|
|
727
|
+
/** Set a new similarity function */
|
|
728
|
+
set similarity(fn: SimilarityFunction);
|
|
729
|
+
/** Get the current scoring function */
|
|
730
|
+
get scoring(): ScoringFunction;
|
|
731
|
+
/** Set a new scoring function */
|
|
732
|
+
set scoring(fn: ScoringFunction);
|
|
733
|
+
/** Register a hook */
|
|
734
|
+
registerHook(hook: Hook): void;
|
|
735
|
+
/** Unregister a hook */
|
|
736
|
+
unregisterHook(hook: Hook): boolean;
|
|
737
|
+
/**
|
|
738
|
+
* Register a tool programmatically
|
|
739
|
+
*/
|
|
740
|
+
register(options: RegisterOptions): Tool;
|
|
741
|
+
/**
|
|
742
|
+
* Store (upsert) a tool with a precomputed embedding.
|
|
743
|
+
*
|
|
744
|
+
* This bypasses the embedding provider, allowing custom embedding workflows.
|
|
745
|
+
*/
|
|
746
|
+
store(options: StoreOptions): Tool;
|
|
747
|
+
/**
|
|
748
|
+
* Initialize embeddings for all pending tools
|
|
749
|
+
*/
|
|
750
|
+
initialize(): Promise<void>;
|
|
751
|
+
/**
|
|
752
|
+
* Resolve tools for a query (raw format)
|
|
753
|
+
*/
|
|
754
|
+
resolve(query: string, options?: {
|
|
755
|
+
topK?: number;
|
|
756
|
+
threshold?: number;
|
|
757
|
+
format?: "raw";
|
|
758
|
+
}): Promise<ResolvedTool[]>;
|
|
759
|
+
/**
|
|
760
|
+
* Resolve tools for a query (OpenAI format)
|
|
761
|
+
*/
|
|
762
|
+
resolve(query: string, options: {
|
|
763
|
+
topK?: number;
|
|
764
|
+
threshold?: number;
|
|
765
|
+
format: "openai";
|
|
766
|
+
}): Promise<OpenAIToolSchema[]>;
|
|
767
|
+
/**
|
|
768
|
+
* Resolve tools for a query (Anthropic format)
|
|
769
|
+
*/
|
|
770
|
+
resolve(query: string, options: {
|
|
771
|
+
topK?: number;
|
|
772
|
+
threshold?: number;
|
|
773
|
+
format: "anthropic";
|
|
774
|
+
}): Promise<AnthropicToolSchema[]>;
|
|
775
|
+
/**
|
|
776
|
+
* Resolve tools for a query (custom format)
|
|
777
|
+
*/
|
|
778
|
+
resolve(query: string, options: {
|
|
779
|
+
topK?: number;
|
|
780
|
+
threshold?: number;
|
|
781
|
+
format: string | ToolFormatter;
|
|
782
|
+
}): Promise<Record<string, unknown>[]>;
|
|
783
|
+
/**
|
|
784
|
+
* Format results based on format option
|
|
785
|
+
*/
|
|
786
|
+
private formatResults;
|
|
787
|
+
/**
|
|
788
|
+
* Resolve the single best matching tool
|
|
789
|
+
*/
|
|
790
|
+
resolveOne(query: string, threshold?: number): Promise<ResolvedTool | null>;
|
|
791
|
+
/**
|
|
792
|
+
* Get a registered tool by name
|
|
793
|
+
*/
|
|
794
|
+
getTool(name: string): Tool | undefined;
|
|
795
|
+
/**
|
|
796
|
+
* List all registered tool names
|
|
797
|
+
*/
|
|
798
|
+
listTools(): string[];
|
|
799
|
+
/**
|
|
800
|
+
* Number of registered tools
|
|
801
|
+
*/
|
|
802
|
+
get toolCount(): number;
|
|
803
|
+
/**
|
|
804
|
+
* Execute a tool by name
|
|
805
|
+
*/
|
|
806
|
+
execute(toolName: string, params: Record<string, unknown>): Promise<unknown>;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/**
|
|
810
|
+
* Vector store for tool embeddings
|
|
811
|
+
*/
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* In-memory vector store for tool embeddings.
|
|
815
|
+
*
|
|
816
|
+
* Uses cosine similarity by default for matching. Supports custom
|
|
817
|
+
* similarity and scoring functions for advanced use cases.
|
|
818
|
+
*
|
|
819
|
+
* For production use with many tools, consider using an external
|
|
820
|
+
* store like Pinecone, Chroma, or Qdrant.
|
|
821
|
+
*/
|
|
822
|
+
declare class VectorStore {
|
|
823
|
+
private tools;
|
|
824
|
+
private embeddings;
|
|
825
|
+
/**
|
|
826
|
+
* Add a tool with its embedding to the store
|
|
827
|
+
*/
|
|
828
|
+
add(tool: Tool, embedding: number[]): void;
|
|
829
|
+
/**
|
|
830
|
+
* Add multiple tools with embeddings
|
|
831
|
+
*/
|
|
832
|
+
addBatch(tools: Tool[], embeddings: number[][]): void;
|
|
833
|
+
/**
|
|
834
|
+
* Search for similar tools
|
|
835
|
+
*
|
|
836
|
+
* @param queryEmbedding - The query embedding vector
|
|
837
|
+
* @param topK - Maximum number of results
|
|
838
|
+
* @param threshold - Minimum score to include
|
|
839
|
+
* @param similarityFn - Custom similarity function (defaults to cosine)
|
|
840
|
+
* @param scoringFn - Custom scoring function (defaults to priority scoring)
|
|
841
|
+
*/
|
|
842
|
+
search(queryEmbedding: number[], topK?: number, threshold?: number, similarityFn?: SimilarityFunction, scoringFn?: ScoringFunction): ToolMatch[];
|
|
843
|
+
/**
|
|
844
|
+
* Remove a tool by name
|
|
845
|
+
*/
|
|
846
|
+
remove(toolName: string): boolean;
|
|
847
|
+
/**
|
|
848
|
+
* Remove all tools from the store
|
|
849
|
+
*/
|
|
850
|
+
clear(): void;
|
|
851
|
+
/**
|
|
852
|
+
* Get a tool by name
|
|
853
|
+
*/
|
|
854
|
+
get(toolName: string): Tool | undefined;
|
|
855
|
+
/**
|
|
856
|
+
* Number of tools in the store
|
|
857
|
+
*/
|
|
858
|
+
get size(): number;
|
|
859
|
+
/**
|
|
860
|
+
* Check if a tool exists by name
|
|
861
|
+
*/
|
|
862
|
+
has(toolName: string): boolean;
|
|
863
|
+
/**
|
|
864
|
+
* Get all tools
|
|
865
|
+
*/
|
|
866
|
+
getAll(): Tool[];
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
export { AnthropicFormatter, type AnthropicToolSchema, CachingHook, CohereEmbeddings, CohereFormatter, CompositeScoring, CompositeValidator, CosineSimilarity, DEFAULT_SCORING, DEFAULT_SIMILARITY, DotProductSimilarity, type EmbeddingProvider, EmbeddingValidator, type EmbeddingValidatorOptions, EuclideanSimilarity, GeminiFormatter, GoogleEmbeddings, type Hook, type HookContext, type HookFunction, HookRegistry, HookType, LlamaFormatter, LoggingHook, MetricsHook, MinimalFormatter, MistralFormatter, OpenAIEmbeddings, OpenAIFormatter, type OpenAIToolSchema, ParameterValidator, type ParameterValidatorOptions, PriorityScoring, RawFormatter, type RegisterOptions, type ResolveFormat, type ResolveOptions, type ResolvedTool, ResultValidator, type ResultValidatorOptions, SIS, type SISOptions, type ScoringFunction, type SimilarityFunction, type StoreOptions, TagBoostScoring, TimingHook, type Tool, type ToolExample, type ToolFormatter, type ToolHandler, type ToolMatch, type ToolMetadata, type ToolParameter, type ToolParameters, type ToolSchema, ToolSchemaValidator, type ToolSchemaValidatorOptions, ValidationError, type ValidationResult, type Validator, ValidatorRegistry, VectorStore, VerboseFormatter, WeightedScoring, cancelContext, createContext, createHook, createLenientValidator, createStrictValidator, createValidationResult, formatTools, getFormatter, getProvider, hasFormatter, listFormatters, mergeValidationResults, registerFormatter, setContextError, toAnthropicSchema, toOpenAISchema, toSchema, unregisterFormatter };
|