@threaded/ai 1.0.30 → 1.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.
Files changed (112) hide show
  1. package/README.md +240 -0
  2. package/dist/approval.d.ts +18 -0
  3. package/dist/approval.d.ts.map +1 -0
  4. package/dist/approval.js +35 -0
  5. package/dist/approval.js.map +1 -0
  6. package/dist/composition/compose.d.ts +3 -0
  7. package/dist/composition/compose.d.ts.map +1 -0
  8. package/dist/composition/compose.js +38 -0
  9. package/dist/composition/compose.js.map +1 -0
  10. package/dist/composition/model.d.ts +9 -0
  11. package/dist/composition/model.d.ts.map +1 -0
  12. package/dist/composition/model.js +192 -0
  13. package/dist/composition/model.js.map +1 -0
  14. package/dist/composition/retry.d.ts +6 -0
  15. package/dist/composition/retry.d.ts.map +1 -0
  16. package/dist/composition/retry.js +18 -0
  17. package/dist/composition/retry.js.map +1 -0
  18. package/dist/composition/scope.d.ts +3 -0
  19. package/dist/composition/scope.d.ts.map +1 -0
  20. package/dist/composition/scope.js +83 -0
  21. package/dist/composition/scope.js.map +1 -0
  22. package/dist/composition/tap.d.ts +3 -0
  23. package/dist/composition/tap.d.ts.map +1 -0
  24. package/dist/composition/tap.js +7 -0
  25. package/dist/composition/tap.js.map +1 -0
  26. package/dist/composition/when.d.ts +3 -0
  27. package/dist/composition/when.d.ts.map +1 -0
  28. package/dist/composition/when.js +9 -0
  29. package/dist/composition/when.js.map +1 -0
  30. package/dist/embed.d.ts +16 -0
  31. package/dist/embed.d.ts.map +1 -0
  32. package/dist/embed.js +72 -0
  33. package/dist/embed.js.map +1 -0
  34. package/dist/examples.d.ts +2 -0
  35. package/dist/examples.d.ts.map +1 -0
  36. package/dist/examples.js +6 -0
  37. package/dist/examples.js.map +1 -0
  38. package/dist/helpers.d.ts +17 -0
  39. package/dist/helpers.d.ts.map +1 -0
  40. package/dist/helpers.js +104 -0
  41. package/dist/helpers.js.map +1 -0
  42. package/dist/image-model-schema.d.ts +19 -0
  43. package/dist/image-model-schema.d.ts.map +1 -0
  44. package/dist/image-model-schema.js +103 -0
  45. package/dist/image-model-schema.js.map +1 -0
  46. package/dist/image.d.ts +3 -0
  47. package/dist/image.d.ts.map +1 -0
  48. package/dist/image.js +120 -0
  49. package/dist/image.js.map +1 -0
  50. package/dist/index.d.ts +18 -352
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +17 -2073
  53. package/dist/index.js.map +1 -1
  54. package/dist/mcp.d.ts +3 -0
  55. package/dist/mcp.d.ts.map +1 -0
  56. package/dist/mcp.js +29 -0
  57. package/dist/mcp.js.map +1 -0
  58. package/dist/providers/anthropic.d.ts +3 -0
  59. package/dist/providers/anthropic.d.ts.map +1 -0
  60. package/dist/providers/anthropic.js +226 -0
  61. package/dist/providers/anthropic.js.map +1 -0
  62. package/dist/providers/google.d.ts +3 -0
  63. package/dist/providers/google.d.ts.map +1 -0
  64. package/dist/providers/google.js +244 -0
  65. package/dist/providers/google.js.map +1 -0
  66. package/dist/providers/huggingface.d.ts +3 -0
  67. package/dist/providers/huggingface.d.ts.map +1 -0
  68. package/dist/providers/huggingface.js +59 -0
  69. package/dist/providers/huggingface.js.map +1 -0
  70. package/dist/providers/index.d.ts +3 -0
  71. package/dist/providers/index.d.ts.map +1 -0
  72. package/dist/providers/index.js +29 -0
  73. package/dist/providers/index.js.map +1 -0
  74. package/dist/providers/local.d.ts +3 -0
  75. package/dist/providers/local.d.ts.map +1 -0
  76. package/dist/providers/local.js +152 -0
  77. package/dist/providers/local.js.map +1 -0
  78. package/dist/providers/openai.d.ts +3 -0
  79. package/dist/providers/openai.d.ts.map +1 -0
  80. package/dist/providers/openai.js +165 -0
  81. package/dist/providers/openai.js.map +1 -0
  82. package/dist/providers/xai.d.ts +3 -0
  83. package/dist/providers/xai.d.ts.map +1 -0
  84. package/dist/providers/xai.js +161 -0
  85. package/dist/providers/xai.js.map +1 -0
  86. package/dist/schema.d.ts +7 -0
  87. package/dist/schema.d.ts.map +1 -0
  88. package/dist/schema.js +44 -0
  89. package/dist/schema.js.map +1 -0
  90. package/dist/thread.d.ts +25 -0
  91. package/dist/thread.d.ts.map +1 -0
  92. package/dist/thread.js +87 -0
  93. package/dist/thread.js.map +1 -0
  94. package/dist/types.d.ts +193 -0
  95. package/dist/types.d.ts.map +1 -0
  96. package/dist/types.js +8 -0
  97. package/dist/types.js.map +1 -0
  98. package/dist/utils/rateLimited.d.ts +27 -0
  99. package/dist/utils/rateLimited.d.ts.map +1 -0
  100. package/dist/utils/rateLimited.js +74 -0
  101. package/dist/utils/rateLimited.js.map +1 -0
  102. package/dist/utils.d.ts +8 -0
  103. package/dist/utils.d.ts.map +1 -0
  104. package/dist/utils.js +78 -0
  105. package/dist/utils.js.map +1 -0
  106. package/package.json +34 -12
  107. package/.claude/settings.local.json +0 -15
  108. package/.lore +0 -65
  109. package/dist/index.cjs +0 -2149
  110. package/dist/index.cjs.map +0 -1
  111. package/dist/index.d.cts +0 -352
  112. package/tsconfig.json +0 -29
package/dist/index.d.cts DELETED
@@ -1,352 +0,0 @@
1
- import { Client } from '@modelcontextprotocol/sdk/client/index';
2
-
3
- interface Message {
4
- role: "system" | "user" | "assistant" | "tool";
5
- content: string;
6
- tool_call_id?: string;
7
- }
8
- interface ToolCall {
9
- id: string;
10
- function: {
11
- name: string;
12
- arguments: string;
13
- };
14
- }
15
- interface ToolCallResult {
16
- name: string;
17
- inputs: any;
18
- results: any;
19
- }
20
- interface ToolDefinition {
21
- type: "function";
22
- function: {
23
- name: string;
24
- description: string;
25
- parameters: {
26
- type: string;
27
- properties: Record<string, any>;
28
- required?: string[];
29
- };
30
- };
31
- }
32
- interface ToolConfig {
33
- name: string;
34
- description: string;
35
- schema: Record<string, SchemaProperty> | StandardSchema;
36
- execute: (args: any) => Promise<any> | any;
37
- _maxCalls?: number;
38
- }
39
- interface SchemaProperty {
40
- type: "string" | "number" | "boolean" | "array" | "object";
41
- description?: string;
42
- enum?: string[];
43
- optional?: boolean;
44
- items?: SchemaProperty;
45
- properties?: Record<string, SchemaProperty>;
46
- }
47
- interface ToolExecutionConfig {
48
- /** require user approval before executing tools */
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
- */
65
- approvalCallback?: (call: ToolCall) => boolean | Promise<boolean>;
66
- /** execute tools in parallel instead of sequentially */
67
- parallel?: boolean;
68
- /** number of times to retry failed tool executions */
69
- retryCount?: number;
70
- /** identifier for approval requests, useful for managing multiple approval flows */
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;
74
- }
75
- type StreamEvent = {
76
- type: 'content';
77
- content: string;
78
- } | {
79
- type: 'tool_calls_ready';
80
- calls: ToolCall[];
81
- } | {
82
- type: 'tool_executing';
83
- call: ToolCall;
84
- } | {
85
- type: 'tool_complete';
86
- call: ToolCall;
87
- result: any;
88
- } | {
89
- type: 'tool_error';
90
- call: ToolCall;
91
- error: string;
92
- } | {
93
- type: 'approval_requested';
94
- call: ToolCall;
95
- requestId: string;
96
- } | {
97
- type: 'usage';
98
- usage: TokenUsage;
99
- };
100
- interface ConversationContext {
101
- history: Message[];
102
- lastRequest?: Message;
103
- lastResponse?: Message & {
104
- tool_calls?: ToolCall[];
105
- };
106
- tools?: ToolDefinition[];
107
- toolExecutors?: Record<string, Function>;
108
- stream?: (event: StreamEvent) => void;
109
- stopReason?: string;
110
- toolCallCounts?: Record<string, number>;
111
- toolLimits?: Record<string, number>;
112
- toolConfig?: ToolExecutionConfig;
113
- abortSignal?: AbortSignal;
114
- usage?: TokenUsage;
115
- }
116
- declare enum Inherit {
117
- Nothing = 0,
118
- Conversation = 1,
119
- Tools = 2,
120
- All = 3
121
- }
122
- interface ScopeConfig {
123
- inherit?: number;
124
- tools?: ToolConfig[];
125
- toolConfig?: ToolExecutionConfig;
126
- system?: string;
127
- silent?: boolean;
128
- until?: (ctx: ConversationContext) => boolean;
129
- stream?: (event: StreamEvent) => void;
130
- }
131
- type StepFunction = (ctx: ConversationContext) => Promise<ConversationContext>;
132
- type ComposedFunction = (ctxOrMessage: ConversationContext | string) => Promise<ConversationContext>;
133
- interface JsonSchema {
134
- name: string;
135
- schema: Record<string, any>;
136
- }
137
- interface StandardSchema {
138
- "~standard": any;
139
- [key: string]: any;
140
- }
141
- interface ProviderConfig {
142
- model: string;
143
- instructions?: string;
144
- schema?: JsonSchema;
145
- apiKey?: string;
146
- baseUrl?: string;
147
- }
148
- interface ParsedModel {
149
- provider: string;
150
- model: string;
151
- }
152
- interface ApiKeys {
153
- openai?: string;
154
- anthropic?: string;
155
- google?: string;
156
- [provider: string]: string | undefined;
157
- }
158
- interface ThreadStore {
159
- get(threadId: string): Promise<Message[]>;
160
- set(threadId: string, messages: Message[]): Promise<void>;
161
- }
162
- interface Thread {
163
- id: string;
164
- store: ThreadStore;
165
- generate(step: StepFunction): Promise<ConversationContext>;
166
- message(content: string, workflow?: StepFunction, options?: {
167
- abortSignal?: AbortSignal;
168
- }): Promise<ConversationContext>;
169
- }
170
- interface RetryOptions {
171
- times?: number;
172
- }
173
- interface ImageConfig {
174
- n?: number;
175
- size?: string;
176
- quality?: "standard" | "hd" | "low" | "medium" | "high" | "auto";
177
- style?: "vivid" | "natural";
178
- responseFormat?: "url" | "b64_json";
179
- aspectRatio?: string;
180
- outputFormat?: "png" | "jpeg" | "webp";
181
- outputCompression?: number;
182
- background?: "transparent" | "opaque" | "auto";
183
- moderation?: "auto" | "low";
184
- imageSize?: "1K" | "2K";
185
- }
186
- interface ImageResult {
187
- data: string;
188
- revisedPrompt?: string;
189
- }
190
- interface TokenUsage {
191
- promptTokens: number;
192
- completionTokens: number;
193
- totalTokens: number;
194
- }
195
-
196
- declare const createMCPTools: (client: Client) => Promise<ToolConfig[]>;
197
-
198
- declare const toolConfigToToolDefinition: (tool: ToolConfig) => ToolDefinition;
199
- declare const parseModelName: (model: string) => ParsedModel;
200
- declare const setKeys: (keys: ApiKeys) => void;
201
- declare const getKey: (provider: string) => string;
202
- declare const maxCalls: (toolConfig: ToolConfig, maxCalls: number) => ToolConfig;
203
- declare const addUsage: (existing: TokenUsage | undefined, promptTokens: number, completionTokens: number, totalTokens: number) => TokenUsage;
204
-
205
- /**
206
- * generates embeddings for text using openai or huggingface models
207
- *
208
- * openai models use the prefix "openai/" (e.g., "openai/text-embedding-3-small")
209
- * all other models use huggingface transformers
210
- *
211
- * accepts a single string or an array of strings for batch embedding
212
- *
213
- * @example
214
- * const vector = await embed("openai/text-embedding-3-small", "hello world");
215
- * const vectors = await embed("openai/text-embedding-3-small", ["hello", "world"]);
216
- */
217
- declare const embed: (model: string, text: string | string[], config?: {
218
- dimensions?: number;
219
- }) => Promise<number[] | number[][]>;
220
-
221
- declare const generateImage: (model: string, prompt: string, config?: ImageConfig) => Promise<ImageResult>;
222
-
223
- interface ConfigOption {
224
- values: string[];
225
- default: string;
226
- description: string;
227
- }
228
- interface ModelConfig {
229
- [option: string]: ConfigOption;
230
- }
231
- interface ProviderModels {
232
- [model: string]: ModelConfig;
233
- }
234
- interface ImageModelSchema {
235
- [provider: string]: ProviderModels;
236
- }
237
- declare const IMAGE_MODEL_SCHEMA: ImageModelSchema;
238
- declare const IMAGE_EDIT_MODEL_SCHEMA: ImageModelSchema;
239
- declare function getModelConfig(provider: string, model: string): ModelConfig | null;
240
- declare function getDefaultConfig(provider: string, model: string): Record<string, string>;
241
-
242
- /**
243
- * @example
244
- * // in-memory (default)
245
- * const thread = getOrCreateThread('user-123');
246
- *
247
- * @example
248
- * // sqlite
249
- * const thread = getOrCreateThread('user-123', {
250
- * async get(id) {
251
- * const row = await db.get('SELECT messages FROM threads WHERE id = ?', id);
252
- * return row ? JSON.parse(row.messages) : [];
253
- * },
254
- * async set(id, messages) {
255
- * await db.run(
256
- * 'INSERT OR REPLACE INTO threads (id, messages, updated_at) VALUES (?, ?, ?)',
257
- * id,
258
- * JSON.stringify(messages),
259
- * Date.now()
260
- * );
261
- * }
262
- * });
263
- */
264
- declare const getOrCreateThread: (id: string, store?: ThreadStore) => Thread;
265
-
266
- declare const isStandardSchema: (schema: any) => schema is StandardSchema;
267
- declare const convertStandardSchemaToJsonSchema: (standardSchema: StandardSchema, name?: string) => JsonSchema;
268
- declare const convertMCPSchemaToToolSchema: (mcpSchema: any) => Record<string, SchemaProperty>;
269
- declare function normalizeSchema(schema: JsonSchema | StandardSchema, name?: string): JsonSchema;
270
- declare const convertStandardSchemaToSchemaProperties: (standardSchema: StandardSchema) => Record<string, SchemaProperty>;
271
-
272
- /**
273
- * scope({ until: noToolsCalled() })
274
- */
275
- declare const noToolsCalled: () => (ctx: ConversationContext) => boolean;
276
- declare const everyNMessages: (n: number, step: StepFunction) => StepFunction;
277
- declare const everyNTokens: (n: number, step: StepFunction) => StepFunction;
278
- declare const appendToLastRequest: (content: string) => StepFunction;
279
- /**
280
- * toolNotUsedInNTurns({ toolName: "search_web", times: 10 }, appendToLastRequest("consider using web search..."))
281
- */
282
- declare const toolNotUsedInNTurns: ({ toolName, times }: {
283
- toolName: string;
284
- times: number;
285
- }, step: StepFunction) => StepFunction;
286
- declare const toolWasCalled: (name: string) => (ctx: ConversationContext) => boolean;
287
-
288
- interface ApprovalRequest {
289
- id: string;
290
- toolCall: ToolCall;
291
- approvalId?: string;
292
- }
293
- interface ApprovalResponse {
294
- id: string;
295
- approved: boolean;
296
- reason?: string;
297
- }
298
- declare const generateApprovalToken: () => string;
299
- declare const requestApproval: (toolCall: ToolCall, approvalId?: string) => Promise<ApprovalResponse>;
300
- declare const resolveApproval: (response: ApprovalResponse) => boolean;
301
- declare const onApprovalRequested: (listener: (request: ApprovalRequest) => void) => void;
302
- declare const onApprovalResolved: (listener: (response: ApprovalResponse) => void) => void;
303
- declare const removeApprovalListener: (event: "approvalRequested" | "approvalResolved", listener: (...args: any[]) => void) => void;
304
-
305
- declare const tap: (fn: (ctx: ConversationContext) => Promise<void> | void) => StepFunction;
306
-
307
- declare const when: (condition: (ctx: ConversationContext) => boolean, action: StepFunction) => StepFunction;
308
-
309
- declare const model: ({ model, schema, system, apiKey, baseUrl, }?: {
310
- model?: string;
311
- schema?: JsonSchema | StandardSchema;
312
- system?: string | ((ctx: ConversationContext) => string);
313
- apiKey?: string;
314
- baseUrl?: string;
315
- }) => ComposedFunction;
316
-
317
- /**
318
- * scope({}, retry({ times: 2 }, model(...)))
319
- */
320
- declare const retry: ({ times }: RetryOptions | undefined, step: StepFunction) => StepFunction;
321
-
322
- declare const scope: (config: ScopeConfig, ...steps: StepFunction[]) => StepFunction;
323
-
324
- type RateLimitConfig = {
325
- rps: number;
326
- burst: number;
327
- concurrency: number;
328
- };
329
- /**
330
- * creates a rate limiter that wraps async functions with burst, rate, and concurrency controls
331
- *
332
- * @param config - rate limit configuration
333
- * @param config.rps - maximum requests per second
334
- * @param config.burst - maximum burst size (initial token bucket capacity)
335
- * @param config.concurrency - maximum concurrent in-flight requests
336
- *
337
- * @example
338
- * const limiter = rateLimited({ rps: 10, burst: 20, concurrency: 5 });
339
- *
340
- * const workflow = limiter(
341
- * compose(
342
- * scope({ tools: [searchTool] }, model())
343
- * )
344
- * );
345
- *
346
- * await workflow("hello");
347
- */
348
- declare const rateLimited: (config: RateLimitConfig) => <T extends (...args: any[]) => Promise<any>>(fn: T) => T;
349
-
350
- declare const compose: (...steps: StepFunction[]) => ComposedFunction;
351
-
352
- export { type ApiKeys, type ApprovalRequest, type ApprovalResponse, type ComposedFunction, type ConfigOption, type ConversationContext, IMAGE_EDIT_MODEL_SCHEMA, IMAGE_MODEL_SCHEMA, type ImageConfig, type ImageModelSchema, type ImageResult, Inherit, type JsonSchema, type Message, type ModelConfig, type ParsedModel, type ProviderConfig, type ProviderModels, type RetryOptions, type SchemaProperty, type ScopeConfig, type StandardSchema, type StepFunction, type StreamEvent, type Thread, type ThreadStore, type TokenUsage, type ToolCall, type ToolCallResult, type ToolConfig, type ToolDefinition, type ToolExecutionConfig, addUsage, appendToLastRequest, compose, convertMCPSchemaToToolSchema, convertStandardSchemaToJsonSchema, convertStandardSchemaToSchemaProperties, createMCPTools, embed, everyNMessages, everyNTokens, generateApprovalToken, generateImage, getDefaultConfig, getKey, getModelConfig, getOrCreateThread, isStandardSchema, maxCalls, model, noToolsCalled, normalizeSchema, onApprovalRequested, onApprovalResolved, parseModelName, rateLimited, removeApprovalListener, requestApproval, resolveApproval, retry, scope, setKeys, tap, toolConfigToToolDefinition, toolNotUsedInNTurns, toolWasCalled, when };
package/tsconfig.json DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "lib": [
5
- "ES2022"
6
- ],
7
- "module": "ESNext",
8
- "moduleResolution": "node",
9
- "types": [
10
- "node"
11
- ],
12
- "strict": true,
13
- "esModuleInterop": true,
14
- "allowSyntheticDefaultImports": true,
15
- "skipLibCheck": true,
16
- "forceConsistentCasingInFileNames": true,
17
- "declaration": true,
18
- "declarationMap": true,
19
- "outDir": "./dist",
20
- "rootDir": "./src"
21
- },
22
- "include": [
23
- "src/**/*",
24
- ],
25
- "exclude": [
26
- "node_modules",
27
- "dist",
28
- ]
29
- }