@threaded/ai 1.0.1 → 1.0.3
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 +285 -83
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -11
- package/dist/index.d.ts +92 -11
- package/dist/index.js +282 -90
- package/dist/index.js.map +1 -1
- package/package.json +6 -11
package/dist/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ interface ToolDefinition {
|
|
|
32
32
|
interface ToolConfig {
|
|
33
33
|
name: string;
|
|
34
34
|
description: string;
|
|
35
|
-
schema: Record<string, SchemaProperty
|
|
35
|
+
schema: Record<string, SchemaProperty> | StandardSchema;
|
|
36
36
|
execute: (args: any) => Promise<any> | any;
|
|
37
37
|
_maxCalls?: number;
|
|
38
38
|
}
|
|
@@ -45,11 +45,32 @@ interface SchemaProperty {
|
|
|
45
45
|
properties?: Record<string, SchemaProperty>;
|
|
46
46
|
}
|
|
47
47
|
interface ToolExecutionConfig {
|
|
48
|
+
/** require user approval before executing tools */
|
|
48
49
|
requireApproval?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* custom callback to handle tool approval, return true to approve
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // simple callback
|
|
55
|
+
* approvalCallback: (call) => call.function.name !== 'dangerousTool'
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // event-driven (SSE): server sends approval request, waits for client POST
|
|
59
|
+
* approvalCallback: (call) => new Promise((resolve) => {
|
|
60
|
+
* pendingApprovals.set(call.id, resolve);
|
|
61
|
+
* res.write(`data: ${JSON.stringify({ type: 'approval_needed', call })}\n\n`);
|
|
62
|
+
* })
|
|
63
|
+
* // then: app.post('/approve/:id', (req) => pendingApprovals.get(id)(req.body.approved))
|
|
64
|
+
*/
|
|
49
65
|
approvalCallback?: (call: ToolCall) => boolean | Promise<boolean>;
|
|
66
|
+
/** execute tools in parallel instead of sequentially */
|
|
50
67
|
parallel?: boolean;
|
|
68
|
+
/** number of times to retry failed tool executions */
|
|
51
69
|
retryCount?: number;
|
|
70
|
+
/** identifier for approval requests, useful for managing multiple approval flows */
|
|
52
71
|
approvalId?: string;
|
|
72
|
+
/** execute tools immediately upon approval instead of waiting for all approvals (default: false, only applies when requireApproval is true) */
|
|
73
|
+
executeOnApproval?: boolean;
|
|
53
74
|
}
|
|
54
75
|
type StreamEvent = {
|
|
55
76
|
type: 'content';
|
|
@@ -100,20 +121,17 @@ interface ScopeConfig {
|
|
|
100
121
|
system?: string;
|
|
101
122
|
silent?: boolean;
|
|
102
123
|
until?: (ctx: ConversationContext) => boolean;
|
|
124
|
+
stream?: (event: StreamEvent) => void;
|
|
103
125
|
}
|
|
104
126
|
type StepFunction = (ctx: ConversationContext) => Promise<ConversationContext>;
|
|
105
|
-
type ComposedFunction = (ctxOrMessage
|
|
127
|
+
type ComposedFunction = (ctxOrMessage: ConversationContext | string) => Promise<ConversationContext>;
|
|
106
128
|
interface JsonSchema {
|
|
107
129
|
name: string;
|
|
108
|
-
schema:
|
|
109
|
-
type: string;
|
|
110
|
-
properties: Record<string, any>;
|
|
111
|
-
required?: string[];
|
|
112
|
-
additionalProperties?: boolean;
|
|
113
|
-
};
|
|
130
|
+
schema: Record<string, any>;
|
|
114
131
|
}
|
|
115
132
|
interface StandardSchema {
|
|
116
133
|
"~standard": any;
|
|
134
|
+
[key: string]: any;
|
|
117
135
|
}
|
|
118
136
|
interface ProviderConfig {
|
|
119
137
|
model: string;
|
|
@@ -152,12 +170,49 @@ declare const setKeys: (keys: ApiKeys) => void;
|
|
|
152
170
|
declare const getKey: (provider: string) => string;
|
|
153
171
|
declare const maxCalls: (toolConfig: ToolConfig, maxCalls: number) => ToolConfig;
|
|
154
172
|
|
|
173
|
+
/**
|
|
174
|
+
* generates embeddings for text using openai or huggingface models
|
|
175
|
+
*
|
|
176
|
+
* openai models use the prefix "openai/" (e.g., "openai/text-embedding-3-small")
|
|
177
|
+
* all other models use huggingface transformers
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* const vector = await embed("openai/text-embedding-3-small", "hello world");
|
|
181
|
+
* const vector2 = await embed("Xenova/all-MiniLM-L6-v2", "hello world");
|
|
182
|
+
*/
|
|
183
|
+
declare const embed: (model: string, text: string, config?: {
|
|
184
|
+
dimensions?: number;
|
|
185
|
+
}) => Promise<number[]>;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @example
|
|
189
|
+
* // in-memory (default)
|
|
190
|
+
* const thread = getOrCreateThread('user-123');
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* // sqlite
|
|
194
|
+
* const thread = getOrCreateThread('user-123', {
|
|
195
|
+
* async get(id) {
|
|
196
|
+
* const row = await db.get('SELECT messages FROM threads WHERE id = ?', id);
|
|
197
|
+
* return row ? JSON.parse(row.messages) : [];
|
|
198
|
+
* },
|
|
199
|
+
* async set(id, messages) {
|
|
200
|
+
* await db.run(
|
|
201
|
+
* 'INSERT OR REPLACE INTO threads (id, messages, updated_at) VALUES (?, ?, ?)',
|
|
202
|
+
* id,
|
|
203
|
+
* JSON.stringify(messages),
|
|
204
|
+
* Date.now()
|
|
205
|
+
* );
|
|
206
|
+
* }
|
|
207
|
+
* });
|
|
208
|
+
*/
|
|
155
209
|
declare const getOrCreateThread: (id: string, store?: ThreadStore) => Thread;
|
|
156
210
|
|
|
157
211
|
declare const isStandardSchema: (schema: any) => schema is StandardSchema;
|
|
158
212
|
declare const convertStandardSchemaToJsonSchema: (standardSchema: StandardSchema, name?: string) => JsonSchema;
|
|
159
213
|
declare const convertMCPSchemaToToolSchema: (mcpSchema: any) => Record<string, SchemaProperty>;
|
|
160
214
|
declare function normalizeSchema(schema: JsonSchema | StandardSchema, name?: string): JsonSchema;
|
|
215
|
+
declare const convertStandardSchemaToSchemaProperties: (standardSchema: StandardSchema) => Record<string, SchemaProperty>;
|
|
161
216
|
|
|
162
217
|
/**
|
|
163
218
|
* scope({ until: noToolsCalled() })
|
|
@@ -198,8 +253,8 @@ declare const when: (condition: (ctx: ConversationContext) => boolean, action: S
|
|
|
198
253
|
|
|
199
254
|
declare const model: ({ model, schema, }?: {
|
|
200
255
|
model?: string;
|
|
201
|
-
schema?:
|
|
202
|
-
}) =>
|
|
256
|
+
schema?: JsonSchema | StandardSchema;
|
|
257
|
+
}) => ComposedFunction;
|
|
203
258
|
|
|
204
259
|
/**
|
|
205
260
|
* scope({}, retry({ times: 2 }, model(...)))
|
|
@@ -208,6 +263,32 @@ declare const retry: ({ times }: RetryOptions | undefined, step: StepFunction) =
|
|
|
208
263
|
|
|
209
264
|
declare const scope: (config: ScopeConfig, ...steps: StepFunction[]) => StepFunction;
|
|
210
265
|
|
|
266
|
+
type RateLimitConfig = {
|
|
267
|
+
rps: number;
|
|
268
|
+
burst: number;
|
|
269
|
+
concurrency: number;
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* creates a rate limiter that wraps async functions with burst, rate, and concurrency controls
|
|
273
|
+
*
|
|
274
|
+
* @param config - rate limit configuration
|
|
275
|
+
* @param config.rps - maximum requests per second
|
|
276
|
+
* @param config.burst - maximum burst size (initial token bucket capacity)
|
|
277
|
+
* @param config.concurrency - maximum concurrent in-flight requests
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* const limiter = rateLimited({ rps: 10, burst: 20, concurrency: 5 });
|
|
281
|
+
*
|
|
282
|
+
* const workflow = limiter(
|
|
283
|
+
* compose(
|
|
284
|
+
* scope({ tools: [searchTool] }, model())
|
|
285
|
+
* )
|
|
286
|
+
* );
|
|
287
|
+
*
|
|
288
|
+
* await workflow("hello");
|
|
289
|
+
*/
|
|
290
|
+
declare const rateLimited: (config: RateLimitConfig) => <T extends (...args: any[]) => Promise<any>>(fn: T) => T;
|
|
291
|
+
|
|
211
292
|
declare const compose: (...steps: StepFunction[]) => ComposedFunction;
|
|
212
293
|
|
|
213
|
-
export { type ApiKeys, type ApprovalRequest, type ApprovalResponse, type ComposedFunction, type ConversationContext, Inherit, type JsonSchema, type Message, type ParsedModel, type ProviderConfig, type RetryOptions, type SchemaProperty, type ScopeConfig, type StandardSchema, type StepFunction, type StreamEvent, type Thread, type ThreadStore, type ToolCall, type ToolCallResult, type ToolConfig, type ToolDefinition, type ToolExecutionConfig, appendToLastRequest, compose, convertMCPSchemaToToolSchema, convertStandardSchemaToJsonSchema, createMCPTools, everyNMessages, everyNTokens, generateApprovalToken, getKey, getOrCreateThread, isStandardSchema, maxCalls, model, noToolsCalled, normalizeSchema, onApprovalRequested, onApprovalResolved, parseModelName, removeApprovalListener, requestApproval, resolveApproval, retry, scope, setKeys, tap, toolConfigToToolDefinition, toolNotUsedInNTurns, toolWasCalled, when };
|
|
294
|
+
export { type ApiKeys, type ApprovalRequest, type ApprovalResponse, type ComposedFunction, type ConversationContext, Inherit, type JsonSchema, type Message, type ParsedModel, type ProviderConfig, type RetryOptions, type SchemaProperty, type ScopeConfig, type StandardSchema, type StepFunction, type StreamEvent, type Thread, type ThreadStore, type ToolCall, type ToolCallResult, type ToolConfig, type ToolDefinition, type ToolExecutionConfig, appendToLastRequest, compose, convertMCPSchemaToToolSchema, convertStandardSchemaToJsonSchema, convertStandardSchemaToSchemaProperties, createMCPTools, embed, everyNMessages, everyNTokens, generateApprovalToken, getKey, getOrCreateThread, isStandardSchema, maxCalls, model, noToolsCalled, normalizeSchema, onApprovalRequested, onApprovalResolved, parseModelName, rateLimited, removeApprovalListener, requestApproval, resolveApproval, retry, scope, setKeys, tap, toolConfigToToolDefinition, toolNotUsedInNTurns, toolWasCalled, when };
|