@llmrtc/llmrtc-core 1.0.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/LICENSE +201 -0
- package/README.md +25 -0
- package/dist/hooks.d.ts +345 -0
- package/dist/hooks.js +52 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logging-hooks.d.ts +120 -0
- package/dist/logging-hooks.js +280 -0
- package/dist/logging-hooks.js.map +1 -0
- package/dist/metrics.d.ts +243 -0
- package/dist/metrics.js +241 -0
- package/dist/metrics.js.map +1 -0
- package/dist/orchestrator.d.ts +73 -0
- package/dist/orchestrator.js +450 -0
- package/dist/orchestrator.js.map +1 -0
- package/dist/playbook-engine.d.ts +140 -0
- package/dist/playbook-engine.js +390 -0
- package/dist/playbook-engine.js.map +1 -0
- package/dist/playbook-orchestrator.d.ts +193 -0
- package/dist/playbook-orchestrator.js +687 -0
- package/dist/playbook-orchestrator.js.map +1 -0
- package/dist/playbook.d.ts +220 -0
- package/dist/playbook.js +92 -0
- package/dist/playbook.js.map +1 -0
- package/dist/protocol.d.ts +327 -0
- package/dist/protocol.js +149 -0
- package/dist/protocol.js.map +1 -0
- package/dist/tool-executor.d.ts +64 -0
- package/dist/tool-executor.js +240 -0
- package/dist/tool-executor.js.map +1 -0
- package/dist/tools.d.ts +183 -0
- package/dist/tools.js +157 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +40 -0
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @llmrtc/llmrtc Wire Protocol v1
|
|
3
|
+
*
|
|
4
|
+
* This file defines all JSON message types exchanged between
|
|
5
|
+
* the web-client and backend over WebSocket and WebRTC data channel.
|
|
6
|
+
*
|
|
7
|
+
* See PROTOCOL.md in the repository root for full documentation.
|
|
8
|
+
*/
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// Protocol Version
|
|
11
|
+
// =============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* Current protocol version.
|
|
14
|
+
* Exchanged during handshake in the 'ready' message.
|
|
15
|
+
*/
|
|
16
|
+
export const PROTOCOL_VERSION = 1;
|
|
17
|
+
// =============================================================================
|
|
18
|
+
// Type Guards
|
|
19
|
+
// =============================================================================
|
|
20
|
+
/** Client message type literals */
|
|
21
|
+
const CLIENT_MESSAGE_TYPES = new Set([
|
|
22
|
+
'ping',
|
|
23
|
+
'offer',
|
|
24
|
+
'reconnect',
|
|
25
|
+
'audio',
|
|
26
|
+
'attachments'
|
|
27
|
+
]);
|
|
28
|
+
/** Server message type literals */
|
|
29
|
+
const SERVER_MESSAGE_TYPES = new Set([
|
|
30
|
+
'ready',
|
|
31
|
+
'pong',
|
|
32
|
+
'signal',
|
|
33
|
+
'reconnect-ack',
|
|
34
|
+
'transcript',
|
|
35
|
+
'llm-chunk',
|
|
36
|
+
'llm',
|
|
37
|
+
'tts-start',
|
|
38
|
+
'tts-chunk',
|
|
39
|
+
'tts',
|
|
40
|
+
'tts-complete',
|
|
41
|
+
'tts-cancelled',
|
|
42
|
+
'speech-start',
|
|
43
|
+
'speech-end',
|
|
44
|
+
'tool-call-start',
|
|
45
|
+
'tool-call-end',
|
|
46
|
+
'stage-change',
|
|
47
|
+
'error'
|
|
48
|
+
]);
|
|
49
|
+
/**
|
|
50
|
+
* Check if a message is a client-to-server message
|
|
51
|
+
*/
|
|
52
|
+
export function isClientMessage(msg) {
|
|
53
|
+
return CLIENT_MESSAGE_TYPES.has(msg.type);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if a message is a server-to-client message
|
|
57
|
+
*/
|
|
58
|
+
export function isServerMessage(msg) {
|
|
59
|
+
return SERVER_MESSAGE_TYPES.has(msg.type);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Check if a message is an error message
|
|
63
|
+
*/
|
|
64
|
+
export function isErrorMessage(msg) {
|
|
65
|
+
return msg.type === 'error';
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse and validate a JSON message
|
|
69
|
+
* Returns null if parsing fails or message type is unknown
|
|
70
|
+
*/
|
|
71
|
+
export function parseMessage(json) {
|
|
72
|
+
try {
|
|
73
|
+
const msg = JSON.parse(json);
|
|
74
|
+
if (typeof msg !== 'object' || msg === null || typeof msg.type !== 'string') {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (!CLIENT_MESSAGE_TYPES.has(msg.type) && !SERVER_MESSAGE_TYPES.has(msg.type)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return msg;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// =============================================================================
|
|
87
|
+
// Message Constructors (for type-safe message creation)
|
|
88
|
+
// =============================================================================
|
|
89
|
+
/**
|
|
90
|
+
* Create a ready message
|
|
91
|
+
*/
|
|
92
|
+
export function createReadyMessage(id, iceServers) {
|
|
93
|
+
return { type: 'ready', id, protocolVersion: PROTOCOL_VERSION, iceServers };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create an error message
|
|
97
|
+
*/
|
|
98
|
+
export function createErrorMessage(code, message) {
|
|
99
|
+
return { type: 'error', code, message };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Create a transcript message
|
|
103
|
+
*/
|
|
104
|
+
export function createTranscriptMessage(text, isFinal) {
|
|
105
|
+
return { type: 'transcript', text, isFinal };
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Create an LLM chunk message
|
|
109
|
+
*/
|
|
110
|
+
export function createLLMChunkMessage(content, done) {
|
|
111
|
+
return { type: 'llm-chunk', content, done };
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Create an LLM message
|
|
115
|
+
*/
|
|
116
|
+
export function createLLMMessage(text) {
|
|
117
|
+
return { type: 'llm', text };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Create a TTS chunk message
|
|
121
|
+
*/
|
|
122
|
+
export function createTTSChunkMessage(data, format, sampleRate) {
|
|
123
|
+
return { type: 'tts-chunk', data, format, sampleRate };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create a TTS message
|
|
127
|
+
*/
|
|
128
|
+
export function createTTSMessage(data, format) {
|
|
129
|
+
return { type: 'tts', data, format };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Create a tool call start message
|
|
133
|
+
*/
|
|
134
|
+
export function createToolCallStartMessage(name, callId, args) {
|
|
135
|
+
return { type: 'tool-call-start', name, callId, arguments: args };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create a tool call end message
|
|
139
|
+
*/
|
|
140
|
+
export function createToolCallEndMessage(callId, durationMs, result, error) {
|
|
141
|
+
return { type: 'tool-call-end', callId, result, error, durationMs };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create a stage change message
|
|
145
|
+
*/
|
|
146
|
+
export function createStageChangeMessage(from, to, reason) {
|
|
147
|
+
return { type: 'stage-change', from, to, reason };
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,gFAAgF;AAChF,mBAAmB;AACnB,gFAAgF;AAEhF;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAoWlC,gFAAgF;AAChF,cAAc;AACd,gFAAgF;AAEhF,mCAAmC;AACnC,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAC;IACnC,MAAM;IACN,OAAO;IACP,WAAW;IACX,OAAO;IACP,aA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MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;AACpE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,MAAc,EACd,UAAkB,EAClB,MAAgB,EAChB,KAAc;IAEd,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC;AACtE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,IAAY,EACZ,EAAU,EACV,MAAc;IAEd,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Executor
|
|
3
|
+
*
|
|
4
|
+
* Handles execution of tool calls from LLM responses.
|
|
5
|
+
* Supports sequential and parallel execution policies.
|
|
6
|
+
*/
|
|
7
|
+
import { Tool, ToolCallRequest, ToolCallResult, ToolExecutionContext, ToolRegistry } from './tools.js';
|
|
8
|
+
/**
|
|
9
|
+
* Options for tool execution
|
|
10
|
+
*/
|
|
11
|
+
export interface ToolExecutorOptions {
|
|
12
|
+
/** Default execution policy when not specified on tool (default: 'parallel') */
|
|
13
|
+
defaultPolicy?: 'sequential' | 'parallel';
|
|
14
|
+
/** Maximum concurrent tool executions (default: 10) */
|
|
15
|
+
maxConcurrency?: number;
|
|
16
|
+
/** Timeout per tool execution in ms (default: 30000) */
|
|
17
|
+
timeout?: number;
|
|
18
|
+
/** Validate tool arguments against schema before execution (default: true) */
|
|
19
|
+
validateArguments?: boolean;
|
|
20
|
+
/** Handler called when a tool execution starts */
|
|
21
|
+
onToolStart?: (toolName: string, callId: string, args: Record<string, unknown>) => void;
|
|
22
|
+
/** Handler called when a tool execution completes */
|
|
23
|
+
onToolEnd?: (result: ToolCallResult) => void;
|
|
24
|
+
/** Handler called when a tool execution fails */
|
|
25
|
+
onToolError?: (toolName: string, callId: string, error: Error) => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Executes tool calls from LLM responses
|
|
29
|
+
* Handles both sequential and parallel execution based on tool policies
|
|
30
|
+
*/
|
|
31
|
+
export declare class ToolExecutor {
|
|
32
|
+
private registry;
|
|
33
|
+
private options;
|
|
34
|
+
constructor(registry: ToolRegistry, options?: ToolExecutorOptions);
|
|
35
|
+
/**
|
|
36
|
+
* Execute a batch of tool calls
|
|
37
|
+
* Respects individual tool execution policies
|
|
38
|
+
*/
|
|
39
|
+
execute(calls: ToolCallRequest[], context: Omit<ToolExecutionContext, 'callId'>): Promise<ToolCallResult[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Execute a single tool call
|
|
42
|
+
*/
|
|
43
|
+
executeSingle(call: ToolCallRequest, context: Omit<ToolExecutionContext, 'callId'>): Promise<ToolCallResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Execute multiple tools in parallel with concurrency limit
|
|
46
|
+
*/
|
|
47
|
+
private executeParallel;
|
|
48
|
+
/**
|
|
49
|
+
* Execute a tool handler with timeout
|
|
50
|
+
*/
|
|
51
|
+
private executeWithTimeout;
|
|
52
|
+
/**
|
|
53
|
+
* Combine multiple abort signals into one
|
|
54
|
+
*/
|
|
55
|
+
private combineAbortSignals;
|
|
56
|
+
/**
|
|
57
|
+
* Group tool calls by their execution policy
|
|
58
|
+
*/
|
|
59
|
+
private groupByPolicy;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create a tool executor with a new registry
|
|
63
|
+
*/
|
|
64
|
+
export declare function createToolExecutor(tools: Tool<unknown, unknown>[], options?: ToolExecutorOptions): ToolExecutor;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Executor
|
|
3
|
+
*
|
|
4
|
+
* Handles execution of tool calls from LLM responses.
|
|
5
|
+
* Supports sequential and parallel execution policies.
|
|
6
|
+
*/
|
|
7
|
+
import { ToolRegistry, validateToolArguments, } from './tools.js';
|
|
8
|
+
// =============================================================================
|
|
9
|
+
// Tool Executor
|
|
10
|
+
// =============================================================================
|
|
11
|
+
/**
|
|
12
|
+
* Executes tool calls from LLM responses
|
|
13
|
+
* Handles both sequential and parallel execution based on tool policies
|
|
14
|
+
*/
|
|
15
|
+
export class ToolExecutor {
|
|
16
|
+
constructor(registry, options = {}) {
|
|
17
|
+
Object.defineProperty(this, "registry", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "options", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
this.registry = registry;
|
|
30
|
+
this.options = {
|
|
31
|
+
defaultPolicy: options.defaultPolicy ?? 'parallel',
|
|
32
|
+
maxConcurrency: options.maxConcurrency ?? 10,
|
|
33
|
+
timeout: options.timeout ?? 30000,
|
|
34
|
+
validateArguments: options.validateArguments ?? true,
|
|
35
|
+
onToolStart: options.onToolStart,
|
|
36
|
+
onToolEnd: options.onToolEnd,
|
|
37
|
+
onToolError: options.onToolError,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Execute a batch of tool calls
|
|
42
|
+
* Respects individual tool execution policies
|
|
43
|
+
*/
|
|
44
|
+
async execute(calls, context) {
|
|
45
|
+
if (calls.length === 0) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
// Group calls by execution policy
|
|
49
|
+
const { sequential, parallel } = this.groupByPolicy(calls);
|
|
50
|
+
const results = [];
|
|
51
|
+
// Execute sequential tools first (in order)
|
|
52
|
+
for (const call of sequential) {
|
|
53
|
+
const result = await this.executeSingle(call, context);
|
|
54
|
+
results.push(result);
|
|
55
|
+
// Stop if aborted
|
|
56
|
+
if (context.abortSignal?.aborted) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Execute parallel tools concurrently (with concurrency limit)
|
|
61
|
+
if (parallel.length > 0 && !context.abortSignal?.aborted) {
|
|
62
|
+
const parallelResults = await this.executeParallel(parallel, context);
|
|
63
|
+
results.push(...parallelResults);
|
|
64
|
+
}
|
|
65
|
+
return results;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Execute a single tool call
|
|
69
|
+
*/
|
|
70
|
+
async executeSingle(call, context) {
|
|
71
|
+
const startTime = Date.now();
|
|
72
|
+
const fullContext = { ...context, callId: call.callId };
|
|
73
|
+
// Find the tool
|
|
74
|
+
const tool = this.registry.get(call.name);
|
|
75
|
+
if (!tool) {
|
|
76
|
+
const result = {
|
|
77
|
+
toolName: call.name,
|
|
78
|
+
callId: call.callId,
|
|
79
|
+
result: null,
|
|
80
|
+
durationMs: Date.now() - startTime,
|
|
81
|
+
success: false,
|
|
82
|
+
error: `Tool '${call.name}' not found`,
|
|
83
|
+
};
|
|
84
|
+
this.options.onToolError?.(call.name, call.callId, new Error(result.error));
|
|
85
|
+
this.options.onToolEnd?.(result);
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
// Validate arguments if enabled
|
|
89
|
+
if (this.options.validateArguments) {
|
|
90
|
+
const validation = validateToolArguments(tool.definition, call.arguments);
|
|
91
|
+
if (!validation.valid) {
|
|
92
|
+
const errorMsg = `Invalid arguments: ${validation.errors.join(', ')}`;
|
|
93
|
+
const result = {
|
|
94
|
+
toolName: call.name,
|
|
95
|
+
callId: call.callId,
|
|
96
|
+
result: null,
|
|
97
|
+
durationMs: Date.now() - startTime,
|
|
98
|
+
success: false,
|
|
99
|
+
error: errorMsg,
|
|
100
|
+
};
|
|
101
|
+
this.options.onToolError?.(call.name, call.callId, new Error(errorMsg));
|
|
102
|
+
this.options.onToolEnd?.(result);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Notify start
|
|
107
|
+
this.options.onToolStart?.(call.name, call.callId, call.arguments);
|
|
108
|
+
try {
|
|
109
|
+
// Execute with timeout
|
|
110
|
+
const result = await this.executeWithTimeout(tool, call.arguments, fullContext);
|
|
111
|
+
const toolResult = {
|
|
112
|
+
toolName: call.name,
|
|
113
|
+
callId: call.callId,
|
|
114
|
+
result,
|
|
115
|
+
durationMs: Date.now() - startTime,
|
|
116
|
+
success: true,
|
|
117
|
+
};
|
|
118
|
+
this.options.onToolEnd?.(toolResult);
|
|
119
|
+
return toolResult;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
123
|
+
const toolResult = {
|
|
124
|
+
toolName: call.name,
|
|
125
|
+
callId: call.callId,
|
|
126
|
+
result: null,
|
|
127
|
+
durationMs: Date.now() - startTime,
|
|
128
|
+
success: false,
|
|
129
|
+
error: errorMessage,
|
|
130
|
+
};
|
|
131
|
+
this.options.onToolError?.(call.name, call.callId, error instanceof Error ? error : new Error(errorMessage));
|
|
132
|
+
this.options.onToolEnd?.(toolResult);
|
|
133
|
+
return toolResult;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Execute multiple tools in parallel with concurrency limit
|
|
138
|
+
*/
|
|
139
|
+
async executeParallel(calls, context) {
|
|
140
|
+
const results = [];
|
|
141
|
+
const executing = [];
|
|
142
|
+
const queue = [...calls];
|
|
143
|
+
while (queue.length > 0 || executing.length > 0) {
|
|
144
|
+
// Check for abort
|
|
145
|
+
if (context.abortSignal?.aborted) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
// Start new executions up to concurrency limit
|
|
149
|
+
while (executing.length < this.options.maxConcurrency && queue.length > 0) {
|
|
150
|
+
const call = queue.shift();
|
|
151
|
+
const promise = this.executeSingle(call, context).then(result => {
|
|
152
|
+
results.push(result);
|
|
153
|
+
const index = executing.indexOf(promise);
|
|
154
|
+
if (index > -1) {
|
|
155
|
+
executing.splice(index, 1);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
executing.push(promise);
|
|
159
|
+
}
|
|
160
|
+
// Wait for at least one to complete
|
|
161
|
+
if (executing.length > 0) {
|
|
162
|
+
await Promise.race(executing);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return results;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Execute a tool handler with timeout
|
|
169
|
+
*/
|
|
170
|
+
async executeWithTimeout(tool, args, context) {
|
|
171
|
+
const timeoutMs = this.options.timeout;
|
|
172
|
+
// Create abort controller for timeout
|
|
173
|
+
const timeoutController = new AbortController();
|
|
174
|
+
const timeoutId = setTimeout(() => timeoutController.abort(), timeoutMs);
|
|
175
|
+
// Combine with external abort signal
|
|
176
|
+
const combinedSignal = context.abortSignal
|
|
177
|
+
? this.combineAbortSignals(context.abortSignal, timeoutController.signal)
|
|
178
|
+
: timeoutController.signal;
|
|
179
|
+
try {
|
|
180
|
+
const result = await Promise.race([
|
|
181
|
+
tool.handler(args, { ...context, abortSignal: combinedSignal }),
|
|
182
|
+
new Promise((_, reject) => {
|
|
183
|
+
combinedSignal.addEventListener('abort', () => {
|
|
184
|
+
if (timeoutController.signal.aborted) {
|
|
185
|
+
reject(new Error(`Tool execution timed out after ${timeoutMs}ms`));
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
reject(new Error('Tool execution aborted'));
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}),
|
|
192
|
+
]);
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
clearTimeout(timeoutId);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Combine multiple abort signals into one
|
|
201
|
+
*/
|
|
202
|
+
combineAbortSignals(...signals) {
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
for (const signal of signals) {
|
|
205
|
+
if (signal.aborted) {
|
|
206
|
+
controller.abort();
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
signal.addEventListener('abort', () => controller.abort(), { once: true });
|
|
210
|
+
}
|
|
211
|
+
return controller.signal;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Group tool calls by their execution policy
|
|
215
|
+
*/
|
|
216
|
+
groupByPolicy(calls) {
|
|
217
|
+
const sequential = [];
|
|
218
|
+
const parallel = [];
|
|
219
|
+
for (const call of calls) {
|
|
220
|
+
const tool = this.registry.get(call.name);
|
|
221
|
+
const policy = tool?.definition.executionPolicy ?? this.options.defaultPolicy;
|
|
222
|
+
if (policy === 'sequential') {
|
|
223
|
+
sequential.push(call);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
parallel.push(call);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return { sequential, parallel };
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Create a tool executor with a new registry
|
|
234
|
+
*/
|
|
235
|
+
export function createToolExecutor(tools, options) {
|
|
236
|
+
const registry = new ToolRegistry();
|
|
237
|
+
registry.registerAll(tools);
|
|
238
|
+
return new ToolExecutor(registry, options);
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=tool-executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-executor.js","sourceRoot":"","sources":["../src/tool-executor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAKL,YAAY,EACZ,qBAAqB,GACtB,MAAM,YAAY,CAAC;AA0BpB,gFAAgF;AAChF,gBAAgB;AAChB,gFAAgF;AAEhF;;;GAGG;AACH,MAAM,OAAO,YAAY;IAKvB,YAAY,QAAsB,EAAE,UAA+B,EAAE;QAJ7D;;;;;WAAuB;QACvB;;;;;WACiE;QAGvE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG;YACb,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,UAAU;Y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AAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACxB,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;QAED,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;IAClC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAA+B,EAC/B,OAA6B;IAE7B,MAAM,QAAQ,GAAG,IAAI,YAAY,EAAE,CAAC;IACpC,QAAQ,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAC5B,OAAO,IAAI,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC"}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Types and Registry for LLM Tool Calling
|
|
3
|
+
*
|
|
4
|
+
* Provides provider-agnostic tool definitions that can be adapted
|
|
5
|
+
* to any LLM provider's tool calling format.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* JSON Schema property definition for tool parameters
|
|
9
|
+
*/
|
|
10
|
+
export interface JSONSchemaProperty {
|
|
11
|
+
type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';
|
|
12
|
+
description?: string;
|
|
13
|
+
enum?: (string | number | boolean | null)[];
|
|
14
|
+
default?: unknown;
|
|
15
|
+
items?: JSONSchemaProperty;
|
|
16
|
+
properties?: Record<string, JSONSchemaProperty>;
|
|
17
|
+
required?: string[];
|
|
18
|
+
minimum?: number;
|
|
19
|
+
maximum?: number;
|
|
20
|
+
minLength?: number;
|
|
21
|
+
maxLength?: number;
|
|
22
|
+
pattern?: string;
|
|
23
|
+
format?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* JSON Schema definition for tool parameters
|
|
27
|
+
* Follows JSON Schema draft-07 subset supported by most LLM providers
|
|
28
|
+
*/
|
|
29
|
+
export interface ToolParameterSchema {
|
|
30
|
+
type: 'object';
|
|
31
|
+
properties: Record<string, JSONSchemaProperty>;
|
|
32
|
+
required?: string[];
|
|
33
|
+
additionalProperties?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Provider-agnostic tool definition
|
|
37
|
+
* Can be converted to any LLM provider's format
|
|
38
|
+
*/
|
|
39
|
+
export interface ToolDefinition {
|
|
40
|
+
/** Unique name for the tool (used in function calls) */
|
|
41
|
+
name: string;
|
|
42
|
+
/** Human-readable description for the LLM */
|
|
43
|
+
description: string;
|
|
44
|
+
/** JSON Schema describing the parameters */
|
|
45
|
+
parameters: ToolParameterSchema;
|
|
46
|
+
/** Execution policy when multiple calls to this tool occur (default: 'parallel') */
|
|
47
|
+
executionPolicy?: 'sequential' | 'parallel';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Context provided to tool handlers during execution
|
|
51
|
+
*/
|
|
52
|
+
export interface ToolExecutionContext {
|
|
53
|
+
/** Unique identifier for this tool call */
|
|
54
|
+
callId: string;
|
|
55
|
+
/** Current session ID */
|
|
56
|
+
sessionId?: string;
|
|
57
|
+
/** Current turn ID */
|
|
58
|
+
turnId?: string;
|
|
59
|
+
/** Signal to check if execution should be aborted */
|
|
60
|
+
abortSignal?: AbortSignal;
|
|
61
|
+
/** Additional metadata from the orchestrator */
|
|
62
|
+
metadata?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Function signature for tool handlers
|
|
66
|
+
* @template TParams - Type of the parsed parameters
|
|
67
|
+
* @template TResult - Type of the result
|
|
68
|
+
*/
|
|
69
|
+
export type ToolHandler<TParams = Record<string, unknown>, TResult = unknown> = (params: TParams, context: ToolExecutionContext) => Promise<TResult>;
|
|
70
|
+
/**
|
|
71
|
+
* Complete tool with definition and handler
|
|
72
|
+
* @template TParams - Type of the parsed parameters
|
|
73
|
+
* @template TResult - Type of the result
|
|
74
|
+
*/
|
|
75
|
+
export interface Tool<TParams = Record<string, unknown>, TResult = unknown> {
|
|
76
|
+
definition: ToolDefinition;
|
|
77
|
+
handler: ToolHandler<TParams, TResult>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* A tool call request from the LLM
|
|
81
|
+
* Normalized from provider-specific formats
|
|
82
|
+
*/
|
|
83
|
+
export interface ToolCallRequest {
|
|
84
|
+
/** Provider-specific call ID for correlating results */
|
|
85
|
+
callId: string;
|
|
86
|
+
/** Name of the tool to invoke */
|
|
87
|
+
name: string;
|
|
88
|
+
/** Parsed arguments from the LLM */
|
|
89
|
+
arguments: Record<string, unknown>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Result of a tool execution
|
|
93
|
+
*/
|
|
94
|
+
export interface ToolCallResult {
|
|
95
|
+
/** Name of the tool that was called */
|
|
96
|
+
toolName: string;
|
|
97
|
+
/** Call ID for correlation with request */
|
|
98
|
+
callId: string;
|
|
99
|
+
/** Result data (will be serialized to JSON for LLM) */
|
|
100
|
+
result: unknown;
|
|
101
|
+
/** Execution duration in milliseconds */
|
|
102
|
+
durationMs: number;
|
|
103
|
+
/** Whether execution succeeded */
|
|
104
|
+
success: boolean;
|
|
105
|
+
/** Error message if execution failed */
|
|
106
|
+
error?: string;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Tool choice options for LLM requests
|
|
110
|
+
* - 'auto': LLM decides whether to use tools
|
|
111
|
+
* - 'none': Disable tool use
|
|
112
|
+
* - 'required': LLM must use at least one tool
|
|
113
|
+
* - { name: string }: Force use of a specific tool
|
|
114
|
+
*/
|
|
115
|
+
export type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
116
|
+
name: string;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Registry for managing tool definitions and handlers
|
|
120
|
+
* Provides lookup by name and bulk operations
|
|
121
|
+
*/
|
|
122
|
+
export declare class ToolRegistry {
|
|
123
|
+
private tools;
|
|
124
|
+
/**
|
|
125
|
+
* Register a tool with its handler
|
|
126
|
+
* @throws Error if a tool with the same name is already registered
|
|
127
|
+
*/
|
|
128
|
+
register<TParams = Record<string, unknown>, TResult = unknown>(tool: Tool<TParams, TResult>): this;
|
|
129
|
+
/**
|
|
130
|
+
* Register multiple tools at once
|
|
131
|
+
* @throws Error if any tool name conflicts
|
|
132
|
+
*/
|
|
133
|
+
registerAll(tools: Tool<unknown, unknown>[]): this;
|
|
134
|
+
/**
|
|
135
|
+
* Unregister a tool by name
|
|
136
|
+
* @returns true if the tool was found and removed
|
|
137
|
+
*/
|
|
138
|
+
unregister(name: string): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Get a tool by name
|
|
141
|
+
* @returns The tool or undefined if not found
|
|
142
|
+
*/
|
|
143
|
+
get<TParams = Record<string, unknown>, TResult = unknown>(name: string): Tool<TParams, TResult> | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* Check if a tool is registered
|
|
146
|
+
*/
|
|
147
|
+
has(name: string): boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Get all registered tool names
|
|
150
|
+
*/
|
|
151
|
+
names(): string[];
|
|
152
|
+
/**
|
|
153
|
+
* Get tool definitions for a subset of tools
|
|
154
|
+
* Useful for providing stage-specific tool lists
|
|
155
|
+
* @param names - Optional filter; if omitted, returns all definitions
|
|
156
|
+
*/
|
|
157
|
+
getDefinitions(names?: string[]): ToolDefinition[];
|
|
158
|
+
/**
|
|
159
|
+
* Get all registered tools
|
|
160
|
+
*/
|
|
161
|
+
getAll(): Tool<unknown, unknown>[];
|
|
162
|
+
/**
|
|
163
|
+
* Clear all registered tools
|
|
164
|
+
*/
|
|
165
|
+
clear(): void;
|
|
166
|
+
/**
|
|
167
|
+
* Number of registered tools
|
|
168
|
+
*/
|
|
169
|
+
get size(): number;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a tool definition with a handler
|
|
173
|
+
* Type-safe helper for defining tools
|
|
174
|
+
*/
|
|
175
|
+
export declare function defineTool<TParams = Record<string, unknown>, TResult = unknown>(definition: ToolDefinition, handler: ToolHandler<TParams, TResult>): Tool<TParams, TResult>;
|
|
176
|
+
/**
|
|
177
|
+
* Validate that arguments match a tool's parameter schema
|
|
178
|
+
* Basic validation - providers typically do this, but useful for testing
|
|
179
|
+
*/
|
|
180
|
+
export declare function validateToolArguments(definition: ToolDefinition, args: Record<string, unknown>): {
|
|
181
|
+
valid: boolean;
|
|
182
|
+
errors: string[];
|
|
183
|
+
};
|