binario 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.
@@ -0,0 +1,555 @@
1
+ import { z } from 'zod';
2
+
3
+ type Provider = 'openai' | 'anthropic' | 'google' | 'mistral' | 'cohere' | 'cloudflare' | 'lovable';
4
+ interface Message {
5
+ role: 'system' | 'user' | 'assistant' | 'tool';
6
+ content: string;
7
+ name?: string;
8
+ tool_calls?: ToolCall[];
9
+ tool_call_id?: string;
10
+ images?: string[];
11
+ }
12
+ interface ToolCall {
13
+ id: string;
14
+ type: 'function';
15
+ function: {
16
+ name: string;
17
+ arguments: string;
18
+ };
19
+ }
20
+ interface Tool {
21
+ type: 'function';
22
+ function: {
23
+ name: string;
24
+ description: string;
25
+ parameters: Record<string, unknown>;
26
+ };
27
+ }
28
+ interface StreamCallbacks {
29
+ onToken?: (token: string) => void;
30
+ onToolCall?: (toolCall: ToolCall) => void;
31
+ onComplete?: (response: ChatResponse) => void;
32
+ onError?: (error: Error) => void;
33
+ }
34
+ interface ChatOptions {
35
+ provider?: Provider;
36
+ model?: string;
37
+ temperature?: number;
38
+ maxTokens?: number;
39
+ topP?: number;
40
+ frequencyPenalty?: number;
41
+ presencePenalty?: number;
42
+ stop?: string[];
43
+ tools?: Tool[];
44
+ toolChoice?: 'auto' | 'none' | {
45
+ type: 'function';
46
+ function: {
47
+ name: string;
48
+ };
49
+ };
50
+ stream?: boolean;
51
+ timeout?: number;
52
+ retries?: number;
53
+ cache?: boolean;
54
+ cacheKey?: string;
55
+ cacheTTL?: number;
56
+ outputSchema?: z.ZodType<unknown>;
57
+ }
58
+ interface ChatResponse {
59
+ id: string;
60
+ provider: Provider;
61
+ model: string;
62
+ content: string;
63
+ toolCalls?: ToolCall[];
64
+ usage: {
65
+ promptTokens: number;
66
+ completionTokens: number;
67
+ totalTokens: number;
68
+ };
69
+ finishReason: 'stop' | 'length' | 'tool_calls' | 'content_filter';
70
+ latency: number;
71
+ cached: boolean;
72
+ data?: unknown;
73
+ }
74
+ interface ProviderConfig {
75
+ apiKey?: string;
76
+ baseUrl?: string;
77
+ defaultModel?: string;
78
+ headers?: Record<string, string>;
79
+ binding?: unknown;
80
+ accountId?: string;
81
+ }
82
+ interface BinarioConfig {
83
+ providers: Partial<Record<Provider, ProviderConfig>>;
84
+ defaultProvider?: Provider;
85
+ cache?: {
86
+ enabled: boolean;
87
+ ttl?: number;
88
+ maxSize?: number;
89
+ };
90
+ retry?: {
91
+ maxRetries: number;
92
+ backoff: 'linear' | 'exponential';
93
+ initialDelay: number;
94
+ };
95
+ timeout?: number;
96
+ debug?: boolean;
97
+ }
98
+ interface AgentTool<TContext = unknown, TArgs = unknown, TResult = unknown> {
99
+ name: string;
100
+ description: string;
101
+ parameters: z.ZodType<TArgs>;
102
+ execute: (args: TArgs, context: TContext) => Promise<TResult> | TResult;
103
+ }
104
+ interface AgentConfig<TContext = unknown, TDeps = unknown> {
105
+ name?: string;
106
+ model?: string;
107
+ provider?: Provider;
108
+ systemPrompt?: string | ((context: TContext) => string);
109
+ tools?: AgentTool<TContext>[];
110
+ maxIterations?: number;
111
+ dependencies?: TDeps;
112
+ outputSchema?: z.ZodType<unknown>;
113
+ }
114
+ interface AgentRunOptions {
115
+ maxIterations?: number;
116
+ maxSteps?: number;
117
+ onToolCall?: (tool: string, args: unknown, result: unknown) => void;
118
+ onThinking?: (content: string) => void;
119
+ onStep?: (step: {
120
+ type: string;
121
+ content: string;
122
+ }) => void;
123
+ signal?: AbortSignal;
124
+ }
125
+ interface AgentResult<T = string> {
126
+ output: T;
127
+ messages: Message[];
128
+ toolCalls: Array<{
129
+ tool: string;
130
+ args: unknown;
131
+ result: unknown;
132
+ }>;
133
+ iterations: number;
134
+ usage: {
135
+ promptTokens: number;
136
+ completionTokens: number;
137
+ totalTokens: number;
138
+ };
139
+ }
140
+
141
+ declare class BinarioAI {
142
+ private config;
143
+ private cache;
144
+ constructor(config: BinarioConfig);
145
+ private getProvider;
146
+ private getProviderConfig;
147
+ private retryWithBackoff;
148
+ private formatMessagesForProvider;
149
+ private parseProviderResponse;
150
+ chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse>;
151
+ private handleStructuredOutput;
152
+ streamChat(messages: Message[], options?: ChatOptions, callbacks?: StreamCallbacks): AsyncGenerator<string, ChatResponse, unknown>;
153
+ clearCache(): void;
154
+ }
155
+
156
+ /**
157
+ * Agent class for multi-step reasoning with tools
158
+ *
159
+ * @example
160
+ * const agent = createAgent({
161
+ * model: 'cloudflare:llama-3.3-70b',
162
+ * systemPrompt: 'You are a helpful assistant',
163
+ * tools: [searchTool, calculatorTool],
164
+ * });
165
+ *
166
+ * const result = await agent.run('What is the weather in Tokyo?', {
167
+ * onToolCall: (tool, args, result) => console.log(`Called ${tool}`)
168
+ * });
169
+ */
170
+ declare class Agent<TContext = unknown, TDeps = unknown> {
171
+ private binario;
172
+ private config;
173
+ private context;
174
+ constructor(binario: BinarioAI, config: AgentConfig<TContext, TDeps>, context?: TContext);
175
+ /**
176
+ * Set the agent context (for dependency injection)
177
+ */
178
+ withContext(context: TContext): Agent<TContext, TDeps>;
179
+ /**
180
+ * Get the system prompt (can be dynamic based on context)
181
+ */
182
+ private getSystemPrompt;
183
+ /**
184
+ * Convert tools to LLM-compatible format
185
+ */
186
+ private getToolsForLLM;
187
+ /**
188
+ * Execute a tool call
189
+ */
190
+ private executeTool;
191
+ /**
192
+ * Run the agent with a user message
193
+ */
194
+ run(input: string, options?: AgentRunOptions): Promise<AgentResult<string>>;
195
+ /**
196
+ * Run the agent with structured output
197
+ */
198
+ runStructured<T>(input: string, schema: z.ZodType<T>, options?: AgentRunOptions): Promise<AgentResult<T>>;
199
+ /**
200
+ * Stream the agent's response
201
+ */
202
+ stream(input: string, options?: AgentRunOptions): AsyncGenerator<{
203
+ type: 'token' | 'tool_call' | 'complete';
204
+ content: string;
205
+ data?: unknown;
206
+ }>;
207
+ }
208
+
209
+ /** Stored message with metadata */
210
+ interface StoredMessage {
211
+ id: string;
212
+ message: Message;
213
+ timestamp: number;
214
+ conversationId: string;
215
+ tokenCount?: number;
216
+ embedding?: number[];
217
+ metadata?: Record<string, unknown>;
218
+ }
219
+ /** Conversation context for LLM */
220
+ interface ConversationContext {
221
+ messages: Message[];
222
+ summary?: string;
223
+ tokenCount: number;
224
+ messageCount: number;
225
+ }
226
+ /** Memory store interface for persistence */
227
+ interface MemoryStore {
228
+ /** Get all messages for a conversation */
229
+ getMessages(conversationId: string): Promise<StoredMessage[]>;
230
+ /** Add a message to storage */
231
+ addMessage(message: StoredMessage): Promise<void>;
232
+ /** Update an existing message */
233
+ updateMessage(id: string, updates: Partial<StoredMessage>): Promise<void>;
234
+ /** Delete a message */
235
+ deleteMessage(id: string): Promise<void>;
236
+ /** Clear all messages for a conversation */
237
+ clear(conversationId: string): Promise<void>;
238
+ /** Get conversation metadata (e.g., summary) */
239
+ getMetadata(conversationId: string): Promise<Record<string, unknown> | null>;
240
+ /** Set conversation metadata */
241
+ setMetadata(conversationId: string, metadata: Record<string, unknown>): Promise<void>;
242
+ }
243
+ /** Base memory interface */
244
+ interface Memory {
245
+ /** Memory type identifier */
246
+ readonly type: 'buffer' | 'summary' | 'summary-buffer' | 'vector';
247
+ /** Add a message to memory */
248
+ add(message: Message): Promise<void>;
249
+ /** Add multiple messages at once */
250
+ addMany(messages: Message[]): Promise<void>;
251
+ /** Get messages formatted for LLM context */
252
+ getMessages(): Promise<Message[]>;
253
+ /** Get full context including summary and token info */
254
+ getContext(): Promise<ConversationContext>;
255
+ /** Get context window limited by tokens */
256
+ getContextWindow(maxTokens: number): Promise<ConversationContext>;
257
+ /** Clear all memory */
258
+ clear(): Promise<void>;
259
+ /** Get current message count */
260
+ getMessageCount(): Promise<number>;
261
+ /** Get estimated token count */
262
+ getTokenCount(): Promise<number>;
263
+ }
264
+
265
+ /** Summarizer function type */
266
+ type SummarizerFn = (messages: Message[], prompt: string) => Promise<string>;
267
+
268
+ /** Embedding result for a single text */
269
+ interface EmbeddingResult {
270
+ /** The input text */
271
+ text: string;
272
+ /** The embedding vector */
273
+ embedding: number[];
274
+ /** Token count used */
275
+ tokenCount?: number;
276
+ }
277
+ /** Batch embedding result */
278
+ interface BatchEmbeddingResult {
279
+ /** Array of embedding results */
280
+ embeddings: EmbeddingResult[];
281
+ /** Model used */
282
+ model: string;
283
+ /** Total tokens used */
284
+ usage: {
285
+ promptTokens: number;
286
+ totalTokens: number;
287
+ };
288
+ }
289
+ /** Embeddings provider interface */
290
+ interface EmbeddingsProvider {
291
+ /** Provider name */
292
+ readonly name: string;
293
+ /** Embed a single text */
294
+ embed(text: string): Promise<EmbeddingResult>;
295
+ /** Embed multiple texts */
296
+ embedMany(texts: string[]): Promise<BatchEmbeddingResult>;
297
+ }
298
+
299
+ interface UseBinarioChatOptions extends ChatOptions {
300
+ initialMessages?: Message[];
301
+ onFinish?: (response: ChatResponse) => void;
302
+ onError?: (error: Error) => void;
303
+ }
304
+ interface UseBinarioChatReturn {
305
+ messages: Message[];
306
+ input: string;
307
+ setInput: (input: string) => void;
308
+ isLoading: boolean;
309
+ error: Error | null;
310
+ append: (message: Message) => Promise<void>;
311
+ reload: () => Promise<void>;
312
+ stop: () => void;
313
+ setMessages: (messages: Message[]) => void;
314
+ lastResponse: ChatResponse | null;
315
+ }
316
+ declare function useBinarioChat(binario: BinarioAI, options?: UseBinarioChatOptions): UseBinarioChatReturn;
317
+ interface UseBinarioStreamOptions extends ChatOptions {
318
+ initialMessages?: Message[];
319
+ onToken?: (token: string) => void;
320
+ onFinish?: (response: ChatResponse) => void;
321
+ onError?: (error: Error) => void;
322
+ }
323
+ interface UseBinarioStreamReturn {
324
+ messages: Message[];
325
+ input: string;
326
+ setInput: (input: string) => void;
327
+ isStreaming: boolean;
328
+ error: Error | null;
329
+ streamingContent: string;
330
+ send: (content: string) => Promise<void>;
331
+ stop: () => void;
332
+ setMessages: (messages: Message[]) => void;
333
+ }
334
+ declare function useBinarioStream(binario: BinarioAI, options?: UseBinarioStreamOptions): UseBinarioStreamReturn;
335
+ interface UseBinarioCompletionOptions extends ChatOptions {
336
+ onFinish?: (response: ChatResponse) => void;
337
+ onError?: (error: Error) => void;
338
+ }
339
+ declare function useBinarioCompletion(binario: BinarioAI, options?: UseBinarioCompletionOptions): {
340
+ completion: string;
341
+ isLoading: boolean;
342
+ error: Error | null;
343
+ complete: (prompt: string, systemPrompt?: string) => Promise<string | null>;
344
+ };
345
+ interface UseBinarioAgentOptions {
346
+ onStep?: (step: {
347
+ type: string;
348
+ content: string;
349
+ }) => void;
350
+ onToolCall?: (tool: {
351
+ name: string;
352
+ args: unknown;
353
+ }) => void;
354
+ onError?: (error: Error) => void;
355
+ maxSteps?: number;
356
+ }
357
+ interface UseBinarioAgentReturn<TContext> {
358
+ output: string;
359
+ isRunning: boolean;
360
+ error: Error | null;
361
+ steps: Array<{
362
+ type: string;
363
+ content: string;
364
+ }>;
365
+ run: (input: string, context?: TContext) => Promise<string | null>;
366
+ runStructured: <T>(input: string, schema: z.ZodType<T>, context?: TContext) => Promise<T | null>;
367
+ stop: () => void;
368
+ }
369
+ declare function useBinarioAgent<TContext = unknown, TDeps = unknown>(agent: Agent<TContext, TDeps>, options?: UseBinarioAgentOptions): UseBinarioAgentReturn<TContext>;
370
+ interface UseBinarioStructuredOptions<T> extends ChatOptions {
371
+ schema: z.ZodType<T>;
372
+ onFinish?: (data: T) => void;
373
+ onError?: (error: Error) => void;
374
+ }
375
+ interface UseBinarioStructuredReturn<T> {
376
+ data: T | null;
377
+ isLoading: boolean;
378
+ error: Error | null;
379
+ generate: (prompt: string, systemPrompt?: string) => Promise<T | null>;
380
+ }
381
+ declare function useBinarioStructured<T>(binario: BinarioAI, options: UseBinarioStructuredOptions<T>): UseBinarioStructuredReturn<T>;
382
+ interface HookTool {
383
+ name: string;
384
+ description: string;
385
+ parameters: z.ZodType<unknown>;
386
+ execute: (args: unknown) => Promise<unknown> | unknown;
387
+ }
388
+ interface UseBinarioToolsOptions {
389
+ tools: HookTool[];
390
+ provider?: string;
391
+ model?: string;
392
+ onToolCall?: (name: string, args: unknown) => void;
393
+ onToolResult?: (name: string, result: unknown) => void;
394
+ onError?: (error: Error) => void;
395
+ }
396
+ interface UseBinarioToolsReturn {
397
+ messages: Message[];
398
+ isProcessing: boolean;
399
+ error: Error | null;
400
+ send: (content: string) => Promise<void>;
401
+ setMessages: (messages: Message[]) => void;
402
+ }
403
+ declare function useBinarioTools(binario: BinarioAI, options: UseBinarioToolsOptions): UseBinarioToolsReturn;
404
+
405
+ type MemoryType = 'buffer' | 'summary' | 'summary-buffer' | 'vector';
406
+ interface UseBinarioMemoryOptions {
407
+ /** Memory type to use */
408
+ type?: MemoryType;
409
+ /** Conversation ID (auto-generated if not provided) */
410
+ conversationId?: string;
411
+ /** Storage backend: 'memory' | 'localStorage' | custom MemoryStore */
412
+ store?: 'memory' | 'localStorage' | MemoryStore;
413
+ /** LocalStorage prefix (only for localStorage store) */
414
+ storagePrefix?: string;
415
+ /** Max messages to keep (for buffer/summary-buffer) */
416
+ maxMessages?: number;
417
+ /** Max tokens to keep */
418
+ maxTokens?: number;
419
+ /** Buffer size for summary-buffer memory */
420
+ bufferSize?: number;
421
+ /** Summarizer function (required for summary/summary-buffer) */
422
+ summarizer?: SummarizerFn;
423
+ /** Embeddings provider (required for vector memory) */
424
+ embeddings?: EmbeddingsProvider;
425
+ /** Top K results for vector search */
426
+ topK?: number;
427
+ /** Min similarity score for vector search */
428
+ minScore?: number;
429
+ }
430
+ interface UseBinarioMemoryReturn {
431
+ /** The memory instance */
432
+ memory: Memory;
433
+ /** Current messages in memory */
434
+ messages: Message[];
435
+ /** Current context (messages + summary + token count) */
436
+ context: ConversationContext | null;
437
+ /** Whether memory is loading */
438
+ isLoading: boolean;
439
+ /** Add a message to memory */
440
+ addMessage: (message: Message) => Promise<void>;
441
+ /** Add multiple messages */
442
+ addMessages: (messages: Message[]) => Promise<void>;
443
+ /** Clear all messages */
444
+ clear: () => Promise<void>;
445
+ /** Refresh messages from memory */
446
+ refresh: () => Promise<void>;
447
+ /** Get context window with token limit */
448
+ getContextWindow: (maxTokens: number) => Promise<ConversationContext>;
449
+ /** Current conversation ID */
450
+ conversationId: string;
451
+ /** Switch to a different conversation */
452
+ switchConversation: (id: string) => void;
453
+ }
454
+ declare function useBinarioMemory(options?: UseBinarioMemoryOptions): UseBinarioMemoryReturn;
455
+ interface UseBinarioChatWithMemoryOptions extends UseBinarioChatOptions {
456
+ /** Memory options */
457
+ memory?: UseBinarioMemoryOptions;
458
+ /** Auto-save messages to memory */
459
+ autoSave?: boolean;
460
+ /** Context window size in tokens */
461
+ contextWindowSize?: number;
462
+ }
463
+ interface UseBinarioChatWithMemoryReturn extends UseBinarioChatReturn {
464
+ /** Memory state and controls */
465
+ memory: UseBinarioMemoryReturn;
466
+ /** Load conversation from memory */
467
+ loadConversation: (conversationId?: string) => Promise<void>;
468
+ /** Save current conversation to memory */
469
+ saveConversation: () => Promise<void>;
470
+ }
471
+ declare function useBinarioChatWithMemory(binario: BinarioAI, options?: UseBinarioChatWithMemoryOptions): UseBinarioChatWithMemoryReturn;
472
+
473
+ type EmbeddingsProviderType = 'cloudflare' | 'custom';
474
+ interface UseBinarioEmbedOptions {
475
+ /** Embeddings provider type */
476
+ provider?: EmbeddingsProviderType;
477
+ /** Custom embeddings provider instance */
478
+ customProvider?: EmbeddingsProvider;
479
+ /** Model to use (provider-specific) */
480
+ model?: string;
481
+ /** Cloudflare account ID (for REST API) */
482
+ accountId?: string;
483
+ /** Cloudflare API key (for REST API) */
484
+ apiKey?: string;
485
+ /** Cache embeddings in memory */
486
+ cache?: boolean;
487
+ /** Callback when embedding starts */
488
+ onStart?: () => void;
489
+ /** Callback when embedding completes */
490
+ onComplete?: (result: EmbeddingResult | BatchEmbeddingResult) => void;
491
+ /** Callback on error */
492
+ onError?: (error: Error) => void;
493
+ }
494
+ interface UseBinarioEmbedReturn {
495
+ /** Generate embedding for a single text */
496
+ embed: (text: string) => Promise<EmbeddingResult | null>;
497
+ /** Generate embeddings for multiple texts */
498
+ embedMany: (texts: string[]) => Promise<BatchEmbeddingResult | null>;
499
+ /** Calculate similarity between two texts */
500
+ similarity: (text1: string, text2: string) => Promise<number | null>;
501
+ /** Find most similar texts from a list */
502
+ findSimilar: (query: string, candidates: string[], topK?: number) => Promise<Array<{
503
+ text: string;
504
+ score: number;
505
+ }> | null>;
506
+ /** Current embedding result */
507
+ result: EmbeddingResult | null;
508
+ /** Batch embedding results */
509
+ batchResult: BatchEmbeddingResult | null;
510
+ /** Whether embedding is in progress */
511
+ isLoading: boolean;
512
+ /** Error if any */
513
+ error: Error | null;
514
+ /** Clear cached embeddings */
515
+ clearCache: () => void;
516
+ /** Get embedding from cache */
517
+ getCached: (text: string) => number[] | null;
518
+ }
519
+ declare function useBinarioEmbed(options?: UseBinarioEmbedOptions): UseBinarioEmbedReturn;
520
+ interface UseBinarioSemanticSearchOptions extends UseBinarioEmbedOptions {
521
+ /** Minimum similarity score to include in results */
522
+ minScore?: number;
523
+ /** Maximum number of results to return */
524
+ maxResults?: number;
525
+ }
526
+ interface SearchDocument {
527
+ id: string;
528
+ text: string;
529
+ metadata?: Record<string, unknown>;
530
+ }
531
+ interface SearchResult {
532
+ document: SearchDocument;
533
+ score: number;
534
+ }
535
+ interface UseBinarioSemanticSearchReturn {
536
+ /** Add documents to the search index */
537
+ addDocuments: (documents: SearchDocument[]) => Promise<void>;
538
+ /** Search for similar documents */
539
+ search: (query: string) => Promise<SearchResult[]>;
540
+ /** Remove documents by ID */
541
+ removeDocuments: (ids: string[]) => void;
542
+ /** Clear all documents */
543
+ clear: () => void;
544
+ /** Number of indexed documents */
545
+ documentCount: number;
546
+ /** Whether indexing is in progress */
547
+ isIndexing: boolean;
548
+ /** Whether search is in progress */
549
+ isSearching: boolean;
550
+ /** Error if any */
551
+ error: Error | null;
552
+ }
553
+ declare function useBinarioSemanticSearch(options?: UseBinarioSemanticSearchOptions): UseBinarioSemanticSearchReturn;
554
+
555
+ export { type EmbeddingsProviderType, type HookTool, type MemoryType, type SearchDocument, type SearchResult, type UseBinarioAgentOptions, type UseBinarioAgentReturn, type UseBinarioChatOptions, type UseBinarioChatReturn, type UseBinarioChatWithMemoryOptions, type UseBinarioChatWithMemoryReturn, type UseBinarioCompletionOptions, type UseBinarioEmbedOptions, type UseBinarioEmbedReturn, type UseBinarioMemoryOptions, type UseBinarioMemoryReturn, type UseBinarioSemanticSearchOptions, type UseBinarioSemanticSearchReturn, type UseBinarioStreamOptions, type UseBinarioStreamReturn, type UseBinarioStructuredOptions, type UseBinarioStructuredReturn, type UseBinarioToolsOptions, type UseBinarioToolsReturn, useBinarioAgent, useBinarioChat, useBinarioChatWithMemory, useBinarioCompletion, useBinarioEmbed, useBinarioMemory, useBinarioSemanticSearch, useBinarioStream, useBinarioStructured, useBinarioTools };