binario 0.1.0 → 0.2.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.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  export { z } from 'zod';
3
+ import * as react from 'react';
3
4
 
4
5
  type Provider = 'openai' | 'anthropic' | 'google' | 'mistral' | 'cohere' | 'cloudflare' | 'lovable';
5
6
  type CloudflareModel = '@cf/meta/llama-3.2-1b-instruct' | '@cf/meta/llama-3.2-3b-instruct' | '@cf/meta/llama-3.1-8b-instruct-fp8-fast' | '@cf/meta/llama-3.2-11b-vision-instruct' | '@cf/mistralai/mistral-small-3.1-24b-instruct' | '@cf/qwen/qwen3-30b-a3b-fp8' | '@cf/meta/llama-3.3-70b-instruct-fp8-fast' | '@cf/meta/llama-3.1-70b-instruct' | '@cf/meta/llama-4-scout-17b-16e-instruct' | '@hf/nousresearch/hermes-2-pro-mistral-7b' | '@cf/ibm/granite-4.0-h-micro' | '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b' | '@cf/qwen/qwq-32b' | string;
@@ -1054,6 +1055,81 @@ interface UseBinarioAgentReturn<TContext> {
1054
1055
  }
1055
1056
  declare function useBinarioAgent<TContext = unknown, TDeps = unknown>(agent: Agent<TContext, TDeps>, options?: UseBinarioAgentOptions): UseBinarioAgentReturn<TContext>;
1056
1057
 
1058
+ interface BinarioProviderProps {
1059
+ apiKey: string;
1060
+ baseUrl?: string;
1061
+ children: React.ReactNode;
1062
+ }
1063
+ declare function BinarioProvider({ apiKey, baseUrl, children }: BinarioProviderProps): react.ReactNode;
1064
+ declare function useBinarioClient(): Binario;
1065
+ interface UseChatOptions {
1066
+ model?: string;
1067
+ temperature?: number;
1068
+ maxTokens?: number;
1069
+ systemPrompt?: string;
1070
+ initialMessages?: Message[];
1071
+ onFinish?: (response: any) => void;
1072
+ onError?: (error: Error) => void;
1073
+ }
1074
+ interface UseChatReturn {
1075
+ messages: Message[];
1076
+ input: string;
1077
+ setInput: (input: string) => void;
1078
+ isLoading: boolean;
1079
+ error: Error | null;
1080
+ send: (content?: string) => Promise<void>;
1081
+ setMessages: (messages: Message[]) => void;
1082
+ stop: () => void;
1083
+ }
1084
+ declare function useChat(client: Binario, options?: UseChatOptions): UseChatReturn;
1085
+ interface UseStreamOptions extends UseChatOptions {
1086
+ onToken?: (token: string) => void;
1087
+ }
1088
+ interface UseStreamReturn {
1089
+ messages: Message[];
1090
+ input: string;
1091
+ setInput: (input: string) => void;
1092
+ isStreaming: boolean;
1093
+ error: Error | null;
1094
+ streamingContent: string;
1095
+ send: (content?: string) => Promise<void>;
1096
+ setMessages: (messages: Message[]) => void;
1097
+ stop: () => void;
1098
+ }
1099
+ declare function useStream(client: Binario, options?: UseStreamOptions): UseStreamReturn;
1100
+ interface UseAgentOptions {
1101
+ name?: string;
1102
+ systemPrompt?: string;
1103
+ tools: AgentOptions['tools'];
1104
+ maxIterations?: number;
1105
+ model?: string;
1106
+ onToolCall?: (tool: string, args: unknown) => void;
1107
+ onError?: (error: Error) => void;
1108
+ }
1109
+ interface UseAgentReturn {
1110
+ output: string;
1111
+ isRunning: boolean;
1112
+ error: Error | null;
1113
+ toolCalls: Array<{
1114
+ tool: string;
1115
+ args: unknown;
1116
+ }>;
1117
+ run: (message: string) => Promise<string | null>;
1118
+ stop: () => void;
1119
+ }
1120
+ declare function useAgent(client: Binario, options: UseAgentOptions): UseAgentReturn;
1121
+ interface UseUsageReturn {
1122
+ usage: {
1123
+ requestsUsed: number;
1124
+ tokensUsed: number;
1125
+ plan: string;
1126
+ } | null;
1127
+ isLoading: boolean;
1128
+ error: Error | null;
1129
+ refresh: () => Promise<void>;
1130
+ }
1131
+ declare function useUsage(client: Binario): UseUsageReturn;
1132
+
1057
1133
  interface RequestStartEvent {
1058
1134
  messages: Message[];
1059
1135
  model: string;
@@ -1101,4 +1177,4 @@ interface UsageTracker {
1101
1177
  /** Create a simple in-memory usage tracker */
1102
1178
  declare function createUsageTracker(): UsageTracker;
1103
1179
 
1104
- export { Agent, type AgentConfig, type AgentOptions, type AgentResult, type AgentRunOptions, type AgentTool, type BatchEmbeddingResult, Binario, BinarioAI, BinarioAgent, type BinarioConfig, type BinarioOptions, BinarioPaymentError, BinarioRateLimitError, BufferMemory, type BufferMemoryOptions, CLOUDFLARE_EMBEDDING_MODELS, type ChatOptions$1 as ChatOptions, type ChatResponse, type ChatOptions as ClientChatOptions, type CloudflareAIBinding, type CloudflareEmbeddingModel, CloudflareEmbeddings, type CloudflareEmbeddingsConfig, CloudflareKVStore, type CloudflareModel, type ConversationContext, type CreateMemoryOptions, DEFAULT_SUMMARY_PROMPT, type EmbeddingOptions, type EmbeddingResponse, type EmbeddingResult, type EmbeddingsConfig, type EmbeddingsProvider, InMemoryStore, LocalStorageStore, type Memory, type MemoryOptions, type MemoryStore, type Message, type ObservabilityHooks, type Provider, type ProviderConfig, type RequestEndEvent, type RequestStartEvent, type SearchResult, type SearchableMemory, type StoredMessage, type StreamCallbacks, type StreamOptions, type StructuredOutputSchema, type SummarizableMemory, type SummarizerFn, SummaryBufferMemory, type SummaryBufferMemoryOptions, SummaryMemory, type SummaryMemoryOptions, type Tool, type ToolCall, type TrackRequestParams, type UsageInfo, type UsageTracker, type UseBinarioAgentOptions, type UseBinarioAgentReturn, VectorMemory, type VectorMemoryOptions, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useBinarioAgent, zodToJsonSchema };
1180
+ export { Agent, type AgentConfig, type AgentOptions, type AgentResult, type AgentRunOptions, type AgentTool, type BatchEmbeddingResult, Binario, BinarioAI, BinarioAgent, type BinarioConfig, type BinarioOptions, BinarioPaymentError, BinarioProvider, BinarioRateLimitError, BufferMemory, type BufferMemoryOptions, CLOUDFLARE_EMBEDDING_MODELS, type ChatOptions$1 as ChatOptions, type ChatResponse, type ChatOptions as ClientChatOptions, type CloudflareAIBinding, type CloudflareEmbeddingModel, CloudflareEmbeddings, type CloudflareEmbeddingsConfig, CloudflareKVStore, type CloudflareModel, type ConversationContext, type CreateMemoryOptions, DEFAULT_SUMMARY_PROMPT, type EmbeddingOptions, type EmbeddingResponse, type EmbeddingResult, type EmbeddingsConfig, type EmbeddingsProvider, InMemoryStore, LocalStorageStore, type Memory, type MemoryOptions, type MemoryStore, type Message, type ObservabilityHooks, type Provider, type ProviderConfig, type RequestEndEvent, type RequestStartEvent, type SearchResult, type SearchableMemory, type StoredMessage, type StreamCallbacks, type StreamOptions, type StructuredOutputSchema, type SummarizableMemory, type SummarizerFn, SummaryBufferMemory, type SummaryBufferMemoryOptions, SummaryMemory, type SummaryMemoryOptions, type Tool, type ToolCall, type TrackRequestParams, type UsageInfo, type UsageTracker, type UseAgentOptions, type UseAgentReturn, type UseBinarioAgentOptions, type UseBinarioAgentReturn, type UseChatOptions, type UseChatReturn, type UseStreamOptions, type UseStreamReturn, type UseUsageReturn, VectorMemory, type VectorMemoryOptions, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useAgent, useBinarioAgent, useBinarioClient, useChat, useStream, useUsage, zodToJsonSchema };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  export { z } from 'zod';
3
+ import * as react from 'react';
3
4
 
4
5
  type Provider = 'openai' | 'anthropic' | 'google' | 'mistral' | 'cohere' | 'cloudflare' | 'lovable';
5
6
  type CloudflareModel = '@cf/meta/llama-3.2-1b-instruct' | '@cf/meta/llama-3.2-3b-instruct' | '@cf/meta/llama-3.1-8b-instruct-fp8-fast' | '@cf/meta/llama-3.2-11b-vision-instruct' | '@cf/mistralai/mistral-small-3.1-24b-instruct' | '@cf/qwen/qwen3-30b-a3b-fp8' | '@cf/meta/llama-3.3-70b-instruct-fp8-fast' | '@cf/meta/llama-3.1-70b-instruct' | '@cf/meta/llama-4-scout-17b-16e-instruct' | '@hf/nousresearch/hermes-2-pro-mistral-7b' | '@cf/ibm/granite-4.0-h-micro' | '@cf/deepseek-ai/deepseek-r1-distill-qwen-32b' | '@cf/qwen/qwq-32b' | string;
@@ -1054,6 +1055,81 @@ interface UseBinarioAgentReturn<TContext> {
1054
1055
  }
1055
1056
  declare function useBinarioAgent<TContext = unknown, TDeps = unknown>(agent: Agent<TContext, TDeps>, options?: UseBinarioAgentOptions): UseBinarioAgentReturn<TContext>;
1056
1057
 
1058
+ interface BinarioProviderProps {
1059
+ apiKey: string;
1060
+ baseUrl?: string;
1061
+ children: React.ReactNode;
1062
+ }
1063
+ declare function BinarioProvider({ apiKey, baseUrl, children }: BinarioProviderProps): react.ReactNode;
1064
+ declare function useBinarioClient(): Binario;
1065
+ interface UseChatOptions {
1066
+ model?: string;
1067
+ temperature?: number;
1068
+ maxTokens?: number;
1069
+ systemPrompt?: string;
1070
+ initialMessages?: Message[];
1071
+ onFinish?: (response: any) => void;
1072
+ onError?: (error: Error) => void;
1073
+ }
1074
+ interface UseChatReturn {
1075
+ messages: Message[];
1076
+ input: string;
1077
+ setInput: (input: string) => void;
1078
+ isLoading: boolean;
1079
+ error: Error | null;
1080
+ send: (content?: string) => Promise<void>;
1081
+ setMessages: (messages: Message[]) => void;
1082
+ stop: () => void;
1083
+ }
1084
+ declare function useChat(client: Binario, options?: UseChatOptions): UseChatReturn;
1085
+ interface UseStreamOptions extends UseChatOptions {
1086
+ onToken?: (token: string) => void;
1087
+ }
1088
+ interface UseStreamReturn {
1089
+ messages: Message[];
1090
+ input: string;
1091
+ setInput: (input: string) => void;
1092
+ isStreaming: boolean;
1093
+ error: Error | null;
1094
+ streamingContent: string;
1095
+ send: (content?: string) => Promise<void>;
1096
+ setMessages: (messages: Message[]) => void;
1097
+ stop: () => void;
1098
+ }
1099
+ declare function useStream(client: Binario, options?: UseStreamOptions): UseStreamReturn;
1100
+ interface UseAgentOptions {
1101
+ name?: string;
1102
+ systemPrompt?: string;
1103
+ tools: AgentOptions['tools'];
1104
+ maxIterations?: number;
1105
+ model?: string;
1106
+ onToolCall?: (tool: string, args: unknown) => void;
1107
+ onError?: (error: Error) => void;
1108
+ }
1109
+ interface UseAgentReturn {
1110
+ output: string;
1111
+ isRunning: boolean;
1112
+ error: Error | null;
1113
+ toolCalls: Array<{
1114
+ tool: string;
1115
+ args: unknown;
1116
+ }>;
1117
+ run: (message: string) => Promise<string | null>;
1118
+ stop: () => void;
1119
+ }
1120
+ declare function useAgent(client: Binario, options: UseAgentOptions): UseAgentReturn;
1121
+ interface UseUsageReturn {
1122
+ usage: {
1123
+ requestsUsed: number;
1124
+ tokensUsed: number;
1125
+ plan: string;
1126
+ } | null;
1127
+ isLoading: boolean;
1128
+ error: Error | null;
1129
+ refresh: () => Promise<void>;
1130
+ }
1131
+ declare function useUsage(client: Binario): UseUsageReturn;
1132
+
1057
1133
  interface RequestStartEvent {
1058
1134
  messages: Message[];
1059
1135
  model: string;
@@ -1101,4 +1177,4 @@ interface UsageTracker {
1101
1177
  /** Create a simple in-memory usage tracker */
1102
1178
  declare function createUsageTracker(): UsageTracker;
1103
1179
 
1104
- export { Agent, type AgentConfig, type AgentOptions, type AgentResult, type AgentRunOptions, type AgentTool, type BatchEmbeddingResult, Binario, BinarioAI, BinarioAgent, type BinarioConfig, type BinarioOptions, BinarioPaymentError, BinarioRateLimitError, BufferMemory, type BufferMemoryOptions, CLOUDFLARE_EMBEDDING_MODELS, type ChatOptions$1 as ChatOptions, type ChatResponse, type ChatOptions as ClientChatOptions, type CloudflareAIBinding, type CloudflareEmbeddingModel, CloudflareEmbeddings, type CloudflareEmbeddingsConfig, CloudflareKVStore, type CloudflareModel, type ConversationContext, type CreateMemoryOptions, DEFAULT_SUMMARY_PROMPT, type EmbeddingOptions, type EmbeddingResponse, type EmbeddingResult, type EmbeddingsConfig, type EmbeddingsProvider, InMemoryStore, LocalStorageStore, type Memory, type MemoryOptions, type MemoryStore, type Message, type ObservabilityHooks, type Provider, type ProviderConfig, type RequestEndEvent, type RequestStartEvent, type SearchResult, type SearchableMemory, type StoredMessage, type StreamCallbacks, type StreamOptions, type StructuredOutputSchema, type SummarizableMemory, type SummarizerFn, SummaryBufferMemory, type SummaryBufferMemoryOptions, SummaryMemory, type SummaryMemoryOptions, type Tool, type ToolCall, type TrackRequestParams, type UsageInfo, type UsageTracker, type UseBinarioAgentOptions, type UseBinarioAgentReturn, VectorMemory, type VectorMemoryOptions, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useBinarioAgent, zodToJsonSchema };
1180
+ export { Agent, type AgentConfig, type AgentOptions, type AgentResult, type AgentRunOptions, type AgentTool, type BatchEmbeddingResult, Binario, BinarioAI, BinarioAgent, type BinarioConfig, type BinarioOptions, BinarioPaymentError, BinarioProvider, BinarioRateLimitError, BufferMemory, type BufferMemoryOptions, CLOUDFLARE_EMBEDDING_MODELS, type ChatOptions$1 as ChatOptions, type ChatResponse, type ChatOptions as ClientChatOptions, type CloudflareAIBinding, type CloudflareEmbeddingModel, CloudflareEmbeddings, type CloudflareEmbeddingsConfig, CloudflareKVStore, type CloudflareModel, type ConversationContext, type CreateMemoryOptions, DEFAULT_SUMMARY_PROMPT, type EmbeddingOptions, type EmbeddingResponse, type EmbeddingResult, type EmbeddingsConfig, type EmbeddingsProvider, InMemoryStore, LocalStorageStore, type Memory, type MemoryOptions, type MemoryStore, type Message, type ObservabilityHooks, type Provider, type ProviderConfig, type RequestEndEvent, type RequestStartEvent, type SearchResult, type SearchableMemory, type StoredMessage, type StreamCallbacks, type StreamOptions, type StructuredOutputSchema, type SummarizableMemory, type SummarizerFn, SummaryBufferMemory, type SummaryBufferMemoryOptions, SummaryMemory, type SummaryMemoryOptions, type Tool, type ToolCall, type TrackRequestParams, type UsageInfo, type UsageTracker, type UseAgentOptions, type UseAgentReturn, type UseBinarioAgentOptions, type UseBinarioAgentReturn, type UseChatOptions, type UseChatReturn, type UseStreamOptions, type UseStreamReturn, type UseUsageReturn, VectorMemory, type VectorMemoryOptions, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useAgent, useBinarioAgent, useBinarioClient, useChat, useStream, useUsage, zodToJsonSchema };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  export { z } from 'zod';
3
- import { useState, useRef, useCallback } from 'react';
3
+ import { createContext as createContext$1, useState, useRef, useCallback, useContext, useEffect } from 'react';
4
4
 
5
5
  // src/client.ts
6
6
  var BINARIO_API_URL = "https://binario-api.databin81.workers.dev";
@@ -2389,6 +2389,187 @@ function useBinarioAgent(agent, options = {}) {
2389
2389
  stop
2390
2390
  };
2391
2391
  }
2392
+ var BinarioContext = createContext$1(null);
2393
+ function BinarioProvider({ apiKey, baseUrl, children }) {
2394
+ const clientRef = useRef(null);
2395
+ if (!clientRef.current || clientRef.current.apiKey !== apiKey) {
2396
+ clientRef.current = new Binario(apiKey, { baseUrl });
2397
+ }
2398
+ return (
2399
+ // @ts-ignore - React.createElement for compatibility
2400
+ BinarioContext.Provider({ value: { client: clientRef.current } }, children)
2401
+ );
2402
+ }
2403
+ function useBinarioClient() {
2404
+ const context = useContext(BinarioContext);
2405
+ if (!context) {
2406
+ throw new Error("useBinarioClient must be used within a BinarioProvider");
2407
+ }
2408
+ return context.client;
2409
+ }
2410
+ function useChat(client, options = {}) {
2411
+ const [messages, setMessages] = useState(options.initialMessages || []);
2412
+ const [input, setInput] = useState("");
2413
+ const [isLoading, setIsLoading] = useState(false);
2414
+ const [error, setError] = useState(null);
2415
+ const abortRef = useRef(false);
2416
+ const stop = useCallback(() => {
2417
+ abortRef.current = true;
2418
+ setIsLoading(false);
2419
+ }, []);
2420
+ const send = useCallback(async (content) => {
2421
+ const messageContent = content || input;
2422
+ if (!messageContent.trim()) return;
2423
+ setIsLoading(true);
2424
+ setError(null);
2425
+ abortRef.current = false;
2426
+ const userMessage = { role: "user", content: messageContent };
2427
+ const newMessages = [...messages, userMessage];
2428
+ setMessages(newMessages);
2429
+ setInput("");
2430
+ try {
2431
+ const response = await client.chat(newMessages, {
2432
+ model: options.model,
2433
+ temperature: options.temperature,
2434
+ maxTokens: options.maxTokens,
2435
+ systemPrompt: options.systemPrompt
2436
+ });
2437
+ if (abortRef.current) return;
2438
+ const assistantMessage = {
2439
+ role: "assistant",
2440
+ content: response.content || response.choices?.[0]?.message?.content || ""
2441
+ };
2442
+ setMessages([...newMessages, assistantMessage]);
2443
+ options.onFinish?.(response);
2444
+ } catch (err) {
2445
+ const e = err instanceof Error ? err : new Error(String(err));
2446
+ setError(e);
2447
+ options.onError?.(e);
2448
+ } finally {
2449
+ setIsLoading(false);
2450
+ }
2451
+ }, [messages, input, client, options]);
2452
+ return { messages, input, setInput, isLoading, error, send, setMessages, stop };
2453
+ }
2454
+ function useStream(client, options = {}) {
2455
+ const [messages, setMessages] = useState(options.initialMessages || []);
2456
+ const [input, setInput] = useState("");
2457
+ const [isStreaming, setIsStreaming] = useState(false);
2458
+ const [error, setError] = useState(null);
2459
+ const [streamingContent, setStreamingContent] = useState("");
2460
+ const abortRef = useRef(false);
2461
+ const stop = useCallback(() => {
2462
+ abortRef.current = true;
2463
+ setIsStreaming(false);
2464
+ }, []);
2465
+ const send = useCallback(async (content) => {
2466
+ const messageContent = content || input;
2467
+ if (!messageContent.trim()) return;
2468
+ setIsStreaming(true);
2469
+ setError(null);
2470
+ setStreamingContent("");
2471
+ abortRef.current = false;
2472
+ const userMessage = { role: "user", content: messageContent };
2473
+ const newMessages = [...messages, userMessage];
2474
+ setMessages(newMessages);
2475
+ setInput("");
2476
+ let fullContent = "";
2477
+ try {
2478
+ for await (const token of client.stream(newMessages, {
2479
+ model: options.model,
2480
+ temperature: options.temperature,
2481
+ maxTokens: options.maxTokens,
2482
+ systemPrompt: options.systemPrompt
2483
+ })) {
2484
+ if (abortRef.current) break;
2485
+ fullContent += token;
2486
+ setStreamingContent(fullContent);
2487
+ options.onToken?.(token);
2488
+ }
2489
+ if (!abortRef.current) {
2490
+ const assistantMessage = { role: "assistant", content: fullContent };
2491
+ setMessages([...newMessages, assistantMessage]);
2492
+ setStreamingContent("");
2493
+ options.onFinish?.({ content: fullContent });
2494
+ }
2495
+ } catch (err) {
2496
+ const e = err instanceof Error ? err : new Error(String(err));
2497
+ setError(e);
2498
+ options.onError?.(e);
2499
+ } finally {
2500
+ setIsStreaming(false);
2501
+ }
2502
+ }, [messages, input, client, options]);
2503
+ return { messages, input, setInput, isStreaming, error, streamingContent, send, setMessages, stop };
2504
+ }
2505
+ function useAgent(client, options) {
2506
+ const [output, setOutput] = useState("");
2507
+ const [isRunning, setIsRunning] = useState(false);
2508
+ const [error, setError] = useState(null);
2509
+ const [toolCalls, setToolCalls] = useState([]);
2510
+ const abortRef = useRef(false);
2511
+ const stop = useCallback(() => {
2512
+ abortRef.current = true;
2513
+ setIsRunning(false);
2514
+ }, []);
2515
+ const run = useCallback(async (message) => {
2516
+ setIsRunning(true);
2517
+ setError(null);
2518
+ setOutput("");
2519
+ setToolCalls([]);
2520
+ abortRef.current = false;
2521
+ try {
2522
+ const agent = client.agent({
2523
+ name: options.name,
2524
+ systemPrompt: options.systemPrompt,
2525
+ tools: options.tools,
2526
+ maxIterations: options.maxIterations,
2527
+ model: options.model
2528
+ });
2529
+ const result = await agent.run(message);
2530
+ if (abortRef.current) return null;
2531
+ setOutput(result.output || "");
2532
+ if (result.toolCalls) {
2533
+ const calls = result.toolCalls.map((tc) => ({
2534
+ tool: tc.tool || tc.name,
2535
+ args: tc.args || tc.arguments
2536
+ }));
2537
+ setToolCalls(calls);
2538
+ calls.forEach((tc) => options.onToolCall?.(tc.tool, tc.args));
2539
+ }
2540
+ return result.output || "";
2541
+ } catch (err) {
2542
+ const e = err instanceof Error ? err : new Error(String(err));
2543
+ setError(e);
2544
+ options.onError?.(e);
2545
+ return null;
2546
+ } finally {
2547
+ setIsRunning(false);
2548
+ }
2549
+ }, [client, options]);
2550
+ return { output, isRunning, error, toolCalls, run, stop };
2551
+ }
2552
+ function useUsage(client) {
2553
+ const [usage, setUsage] = useState(null);
2554
+ const [isLoading, setIsLoading] = useState(false);
2555
+ const [error, setError] = useState(null);
2556
+ const refresh = useCallback(async () => {
2557
+ setIsLoading(true);
2558
+ setError(null);
2559
+ try {
2560
+ const data = await client.getUsage();
2561
+ setUsage(data);
2562
+ } catch (err) {
2563
+ setError(err instanceof Error ? err : new Error(String(err)));
2564
+ } finally {
2565
+ setIsLoading(false);
2566
+ }
2567
+ }, [client]);
2568
+ useEffect(() => {
2569
+ refresh();
2570
+ }, [refresh]);
2571
+ return { usage, isLoading, error, refresh };
2572
+ }
2392
2573
 
2393
2574
  // src/observability.ts
2394
2575
  var consoleHooks = {
@@ -2422,6 +2603,6 @@ function createUsageTracker() {
2422
2603
  };
2423
2604
  }
2424
2605
 
2425
- export { Agent, Binario, BinarioAI, BinarioAgent, BinarioPaymentError, BinarioRateLimitError, BufferMemory, CLOUDFLARE_EMBEDDING_MODELS, CloudflareEmbeddings, CloudflareKVStore, DEFAULT_SUMMARY_PROMPT, InMemoryStore, LocalStorageStore, SummaryBufferMemory, SummaryMemory, VectorMemory, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useBinarioAgent, zodToJsonSchema };
2606
+ export { Agent, Binario, BinarioAI, BinarioAgent, BinarioPaymentError, BinarioProvider, BinarioRateLimitError, BufferMemory, CLOUDFLARE_EMBEDDING_MODELS, CloudflareEmbeddings, CloudflareKVStore, DEFAULT_SUMMARY_PROMPT, InMemoryStore, LocalStorageStore, SummaryBufferMemory, SummaryMemory, VectorMemory, consoleHooks, cosineSimilarity, countMessageTokens, countMessagesTokens, countTokens, createAgent, createBinario, createBinarioClient, createBufferMemory, createCloudflareEmbeddings, createCloudflareKVStore, createInMemoryStore, createLocalStorageStore, createSchema, createStoredMessage, createSummaryBufferMemory, createSummaryMemory, createTool, createUsageTracker, createVectorMemory, defineTool, formatContextWithSummary, formatConversationForSummary, generateId, parseStructuredOutput, truncateMessages, truncateMessagesByCount, useAgent, useBinarioAgent, useBinarioClient, useChat, useStream, useUsage, zodToJsonSchema };
2426
2607
  //# sourceMappingURL=index.js.map
2427
2608
  //# sourceMappingURL=index.js.map