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/src/index.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SIS - Semantic Integration System
|
|
3
|
+
*
|
|
4
|
+
* Intelligent tool resolution for LLMs using semantic embeddings.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Core
|
|
8
|
+
export { SIS } from "./sis";
|
|
9
|
+
export type { SISOptions, RegisterOptions, StoreOptions, ResolveFormat, ResolveOptions } from "./sis";
|
|
10
|
+
|
|
11
|
+
// Types
|
|
12
|
+
export type {
|
|
13
|
+
Tool,
|
|
14
|
+
ToolHandler,
|
|
15
|
+
ToolParameter,
|
|
16
|
+
ToolParameters,
|
|
17
|
+
ToolExample,
|
|
18
|
+
ToolMetadata,
|
|
19
|
+
ToolMatch,
|
|
20
|
+
ResolvedTool,
|
|
21
|
+
ToolSchema,
|
|
22
|
+
OpenAIToolSchema,
|
|
23
|
+
AnthropicToolSchema,
|
|
24
|
+
} from "./types";
|
|
25
|
+
export { toSchema, toOpenAISchema, toAnthropicSchema } from "./types";
|
|
26
|
+
|
|
27
|
+
// Embeddings
|
|
28
|
+
export type { EmbeddingProvider } from "./embeddings";
|
|
29
|
+
export { OpenAIEmbeddings, CohereEmbeddings, GoogleEmbeddings, getProvider } from "./embeddings";
|
|
30
|
+
|
|
31
|
+
// Store
|
|
32
|
+
export { VectorStore } from "./store";
|
|
33
|
+
|
|
34
|
+
// Scoring
|
|
35
|
+
export type { SimilarityFunction, ScoringFunction } from "./scoring";
|
|
36
|
+
export {
|
|
37
|
+
CosineSimilarity,
|
|
38
|
+
EuclideanSimilarity,
|
|
39
|
+
DotProductSimilarity,
|
|
40
|
+
PriorityScoring,
|
|
41
|
+
WeightedScoring,
|
|
42
|
+
TagBoostScoring,
|
|
43
|
+
CompositeScoring,
|
|
44
|
+
DEFAULT_SIMILARITY,
|
|
45
|
+
DEFAULT_SCORING,
|
|
46
|
+
} from "./scoring";
|
|
47
|
+
|
|
48
|
+
// Formatters
|
|
49
|
+
export type { ToolFormatter } from "./formatters";
|
|
50
|
+
export {
|
|
51
|
+
RawFormatter,
|
|
52
|
+
OpenAIFormatter,
|
|
53
|
+
AnthropicFormatter,
|
|
54
|
+
GeminiFormatter,
|
|
55
|
+
MistralFormatter,
|
|
56
|
+
LlamaFormatter,
|
|
57
|
+
CohereFormatter,
|
|
58
|
+
MinimalFormatter,
|
|
59
|
+
VerboseFormatter,
|
|
60
|
+
registerFormatter,
|
|
61
|
+
unregisterFormatter,
|
|
62
|
+
getFormatter,
|
|
63
|
+
listFormatters,
|
|
64
|
+
hasFormatter,
|
|
65
|
+
formatTools,
|
|
66
|
+
} from "./formatters";
|
|
67
|
+
|
|
68
|
+
// Hooks
|
|
69
|
+
export type { Hook, HookContext, HookFunction } from "./hooks";
|
|
70
|
+
export {
|
|
71
|
+
HookType,
|
|
72
|
+
HookRegistry,
|
|
73
|
+
createHook,
|
|
74
|
+
createContext,
|
|
75
|
+
cancelContext,
|
|
76
|
+
setContextError,
|
|
77
|
+
LoggingHook,
|
|
78
|
+
TimingHook,
|
|
79
|
+
MetricsHook,
|
|
80
|
+
CachingHook,
|
|
81
|
+
} from "./hooks";
|
|
82
|
+
|
|
83
|
+
// Validators
|
|
84
|
+
export type {
|
|
85
|
+
Validator,
|
|
86
|
+
ValidationResult,
|
|
87
|
+
ToolSchemaValidatorOptions,
|
|
88
|
+
ParameterValidatorOptions,
|
|
89
|
+
ResultValidatorOptions,
|
|
90
|
+
EmbeddingValidatorOptions,
|
|
91
|
+
} from "./validators";
|
|
92
|
+
export {
|
|
93
|
+
ValidationError,
|
|
94
|
+
ToolSchemaValidator,
|
|
95
|
+
ParameterValidator,
|
|
96
|
+
ResultValidator,
|
|
97
|
+
EmbeddingValidator,
|
|
98
|
+
CompositeValidator,
|
|
99
|
+
ValidatorRegistry,
|
|
100
|
+
createValidationResult,
|
|
101
|
+
mergeValidationResults,
|
|
102
|
+
createStrictValidator,
|
|
103
|
+
createLenientValidator,
|
|
104
|
+
} from "./validators";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare module "openai" {
|
|
2
|
+
export const OpenAI: any;
|
|
3
|
+
const _default: any;
|
|
4
|
+
export default _default;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare module "cohere-ai" {
|
|
8
|
+
export const CohereClient: any;
|
|
9
|
+
const _default: any;
|
|
10
|
+
export default _default;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare module "@google/generative-ai" {
|
|
14
|
+
export const GoogleGenerativeAI: any;
|
|
15
|
+
const _default: any;
|
|
16
|
+
export default _default;
|
|
17
|
+
}
|
package/src/scoring.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Similarity and scoring functions for semantic search
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Tool } from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Interface for custom similarity functions
|
|
9
|
+
*/
|
|
10
|
+
export interface SimilarityFunction {
|
|
11
|
+
/**
|
|
12
|
+
* Compute similarity score between query and tool embeddings
|
|
13
|
+
* @param queryEmbedding - The query vector
|
|
14
|
+
* @param toolEmbedding - The tool's embedding vector
|
|
15
|
+
* @returns Similarity score, typically in range [0, 1]
|
|
16
|
+
*/
|
|
17
|
+
compute(queryEmbedding: number[], toolEmbedding: number[]): number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Interface for custom scoring/ranking functions
|
|
22
|
+
*/
|
|
23
|
+
export interface ScoringFunction {
|
|
24
|
+
/**
|
|
25
|
+
* Apply custom scoring logic to similarity score
|
|
26
|
+
* @param similarity - Raw similarity score from SimilarityFunction
|
|
27
|
+
* @param tool - The tool being scored (access metadata here)
|
|
28
|
+
* @returns Final score for ranking. Higher is better.
|
|
29
|
+
*/
|
|
30
|
+
score(similarity: number, tool: Tool): number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Default cosine similarity implementation
|
|
35
|
+
*/
|
|
36
|
+
export class CosineSimilarity implements SimilarityFunction {
|
|
37
|
+
compute(a: number[], b: number[]): number {
|
|
38
|
+
if (a.length !== b.length) {
|
|
39
|
+
throw new Error(`Vector dimensions must match: ${a.length} !== ${b.length}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let dotProduct = 0;
|
|
43
|
+
let normA = 0;
|
|
44
|
+
let normB = 0;
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < a.length; i++) {
|
|
47
|
+
dotProduct += a[i] * b[i];
|
|
48
|
+
normA += a[i] * a[i];
|
|
49
|
+
normB += b[i] * b[i];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
normA = Math.sqrt(normA);
|
|
53
|
+
normB = Math.sqrt(normB);
|
|
54
|
+
|
|
55
|
+
if (normA === 0 || normB === 0) {
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return dotProduct / (normA * normB);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Euclidean distance converted to similarity score
|
|
65
|
+
* Uses: similarity = 1 / (1 + distance)
|
|
66
|
+
*/
|
|
67
|
+
export class EuclideanSimilarity implements SimilarityFunction {
|
|
68
|
+
compute(a: number[], b: number[]): number {
|
|
69
|
+
if (a.length !== b.length) {
|
|
70
|
+
throw new Error(`Vector dimensions must match: ${a.length} !== ${b.length}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let sumSquares = 0;
|
|
74
|
+
for (let i = 0; i < a.length; i++) {
|
|
75
|
+
const diff = a[i] - b[i];
|
|
76
|
+
sumSquares += diff * diff;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const distance = Math.sqrt(sumSquares);
|
|
80
|
+
return 1.0 / (1.0 + distance);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Dot product similarity (unnormalized cosine)
|
|
86
|
+
*/
|
|
87
|
+
export class DotProductSimilarity implements SimilarityFunction {
|
|
88
|
+
compute(a: number[], b: number[]): number {
|
|
89
|
+
if (a.length !== b.length) {
|
|
90
|
+
throw new Error(`Vector dimensions must match: ${a.length} !== ${b.length}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let dotProduct = 0;
|
|
94
|
+
for (let i = 0; i < a.length; i++) {
|
|
95
|
+
dotProduct += a[i] * b[i];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return dotProduct;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Default priority-based scoring
|
|
104
|
+
* Multiplies similarity by tool's priority metadata
|
|
105
|
+
*/
|
|
106
|
+
export class PriorityScoring implements ScoringFunction {
|
|
107
|
+
private maxScore: number;
|
|
108
|
+
|
|
109
|
+
constructor(maxScore: number = 1.0) {
|
|
110
|
+
this.maxScore = maxScore;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
score(similarity: number, tool: Tool): number {
|
|
114
|
+
const priority = (tool.metadata.priority as number) ?? 1.0;
|
|
115
|
+
const boosted = similarity * priority;
|
|
116
|
+
return Math.min(boosted, this.maxScore);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Weighted scoring combining multiple factors
|
|
122
|
+
*/
|
|
123
|
+
export class WeightedScoring implements ScoringFunction {
|
|
124
|
+
private similarityWeight: number;
|
|
125
|
+
private priorityWeight: number;
|
|
126
|
+
private maxScore: number;
|
|
127
|
+
|
|
128
|
+
constructor(
|
|
129
|
+
similarityWeight: number = 0.8,
|
|
130
|
+
priorityWeight: number = 0.2,
|
|
131
|
+
maxScore: number = 1.0
|
|
132
|
+
) {
|
|
133
|
+
this.similarityWeight = similarityWeight;
|
|
134
|
+
this.priorityWeight = priorityWeight;
|
|
135
|
+
this.maxScore = maxScore;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
score(similarity: number, tool: Tool): number {
|
|
139
|
+
const priority = (tool.metadata.priority as number) ?? 1.0;
|
|
140
|
+
const normalizedPriority = Math.min(priority / 2.0, 1.0);
|
|
141
|
+
|
|
142
|
+
const weighted =
|
|
143
|
+
this.similarityWeight * similarity +
|
|
144
|
+
this.priorityWeight * normalizedPriority;
|
|
145
|
+
|
|
146
|
+
return Math.min(weighted, this.maxScore);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Scoring that boosts tools matching specified tags
|
|
152
|
+
*/
|
|
153
|
+
export class TagBoostScoring implements ScoringFunction {
|
|
154
|
+
private boostTags: Set<string>;
|
|
155
|
+
private boostFactor: number;
|
|
156
|
+
private maxScore: number;
|
|
157
|
+
|
|
158
|
+
constructor(
|
|
159
|
+
boostTags: string[] = [],
|
|
160
|
+
boostFactor: number = 1.5,
|
|
161
|
+
maxScore: number = 1.0
|
|
162
|
+
) {
|
|
163
|
+
this.boostTags = new Set(boostTags);
|
|
164
|
+
this.boostFactor = boostFactor;
|
|
165
|
+
this.maxScore = maxScore;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
score(similarity: number, tool: Tool): number {
|
|
169
|
+
const toolTags = new Set(tool.metadata.tags as string[] ?? []);
|
|
170
|
+
const hasMatchingTag = [...toolTags].some(tag => this.boostTags.has(tag));
|
|
171
|
+
|
|
172
|
+
const boosted = hasMatchingTag ? similarity * this.boostFactor : similarity;
|
|
173
|
+
return Math.min(boosted, this.maxScore);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Combine multiple scoring functions in sequence
|
|
179
|
+
*/
|
|
180
|
+
export class CompositeScoring implements ScoringFunction {
|
|
181
|
+
private scorers: ScoringFunction[];
|
|
182
|
+
|
|
183
|
+
constructor(scorers: ScoringFunction[]) {
|
|
184
|
+
this.scorers = scorers;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
score(similarity: number, tool: Tool): number {
|
|
188
|
+
let currentScore = similarity;
|
|
189
|
+
for (const scorer of this.scorers) {
|
|
190
|
+
currentScore = scorer.score(currentScore, tool);
|
|
191
|
+
}
|
|
192
|
+
return currentScore;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Default instances for convenience
|
|
197
|
+
export const DEFAULT_SIMILARITY = new CosineSimilarity();
|
|
198
|
+
export const DEFAULT_SCORING = new PriorityScoring();
|