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.cjs CHANGED
@@ -2390,6 +2390,187 @@ function useBinarioAgent(agent, options = {}) {
2390
2390
  stop
2391
2391
  };
2392
2392
  }
2393
+ var BinarioContext = react.createContext(null);
2394
+ function BinarioProvider({ apiKey, baseUrl, children }) {
2395
+ const clientRef = react.useRef(null);
2396
+ if (!clientRef.current || clientRef.current.apiKey !== apiKey) {
2397
+ clientRef.current = new Binario(apiKey, { baseUrl });
2398
+ }
2399
+ return (
2400
+ // @ts-ignore - React.createElement for compatibility
2401
+ BinarioContext.Provider({ value: { client: clientRef.current } }, children)
2402
+ );
2403
+ }
2404
+ function useBinarioClient() {
2405
+ const context = react.useContext(BinarioContext);
2406
+ if (!context) {
2407
+ throw new Error("useBinarioClient must be used within a BinarioProvider");
2408
+ }
2409
+ return context.client;
2410
+ }
2411
+ function useChat(client, options = {}) {
2412
+ const [messages, setMessages] = react.useState(options.initialMessages || []);
2413
+ const [input, setInput] = react.useState("");
2414
+ const [isLoading, setIsLoading] = react.useState(false);
2415
+ const [error, setError] = react.useState(null);
2416
+ const abortRef = react.useRef(false);
2417
+ const stop = react.useCallback(() => {
2418
+ abortRef.current = true;
2419
+ setIsLoading(false);
2420
+ }, []);
2421
+ const send = react.useCallback(async (content) => {
2422
+ const messageContent = content || input;
2423
+ if (!messageContent.trim()) return;
2424
+ setIsLoading(true);
2425
+ setError(null);
2426
+ abortRef.current = false;
2427
+ const userMessage = { role: "user", content: messageContent };
2428
+ const newMessages = [...messages, userMessage];
2429
+ setMessages(newMessages);
2430
+ setInput("");
2431
+ try {
2432
+ const response = await client.chat(newMessages, {
2433
+ model: options.model,
2434
+ temperature: options.temperature,
2435
+ maxTokens: options.maxTokens,
2436
+ systemPrompt: options.systemPrompt
2437
+ });
2438
+ if (abortRef.current) return;
2439
+ const assistantMessage = {
2440
+ role: "assistant",
2441
+ content: response.content || response.choices?.[0]?.message?.content || ""
2442
+ };
2443
+ setMessages([...newMessages, assistantMessage]);
2444
+ options.onFinish?.(response);
2445
+ } catch (err) {
2446
+ const e = err instanceof Error ? err : new Error(String(err));
2447
+ setError(e);
2448
+ options.onError?.(e);
2449
+ } finally {
2450
+ setIsLoading(false);
2451
+ }
2452
+ }, [messages, input, client, options]);
2453
+ return { messages, input, setInput, isLoading, error, send, setMessages, stop };
2454
+ }
2455
+ function useStream(client, options = {}) {
2456
+ const [messages, setMessages] = react.useState(options.initialMessages || []);
2457
+ const [input, setInput] = react.useState("");
2458
+ const [isStreaming, setIsStreaming] = react.useState(false);
2459
+ const [error, setError] = react.useState(null);
2460
+ const [streamingContent, setStreamingContent] = react.useState("");
2461
+ const abortRef = react.useRef(false);
2462
+ const stop = react.useCallback(() => {
2463
+ abortRef.current = true;
2464
+ setIsStreaming(false);
2465
+ }, []);
2466
+ const send = react.useCallback(async (content) => {
2467
+ const messageContent = content || input;
2468
+ if (!messageContent.trim()) return;
2469
+ setIsStreaming(true);
2470
+ setError(null);
2471
+ setStreamingContent("");
2472
+ abortRef.current = false;
2473
+ const userMessage = { role: "user", content: messageContent };
2474
+ const newMessages = [...messages, userMessage];
2475
+ setMessages(newMessages);
2476
+ setInput("");
2477
+ let fullContent = "";
2478
+ try {
2479
+ for await (const token of client.stream(newMessages, {
2480
+ model: options.model,
2481
+ temperature: options.temperature,
2482
+ maxTokens: options.maxTokens,
2483
+ systemPrompt: options.systemPrompt
2484
+ })) {
2485
+ if (abortRef.current) break;
2486
+ fullContent += token;
2487
+ setStreamingContent(fullContent);
2488
+ options.onToken?.(token);
2489
+ }
2490
+ if (!abortRef.current) {
2491
+ const assistantMessage = { role: "assistant", content: fullContent };
2492
+ setMessages([...newMessages, assistantMessage]);
2493
+ setStreamingContent("");
2494
+ options.onFinish?.({ content: fullContent });
2495
+ }
2496
+ } catch (err) {
2497
+ const e = err instanceof Error ? err : new Error(String(err));
2498
+ setError(e);
2499
+ options.onError?.(e);
2500
+ } finally {
2501
+ setIsStreaming(false);
2502
+ }
2503
+ }, [messages, input, client, options]);
2504
+ return { messages, input, setInput, isStreaming, error, streamingContent, send, setMessages, stop };
2505
+ }
2506
+ function useAgent(client, options) {
2507
+ const [output, setOutput] = react.useState("");
2508
+ const [isRunning, setIsRunning] = react.useState(false);
2509
+ const [error, setError] = react.useState(null);
2510
+ const [toolCalls, setToolCalls] = react.useState([]);
2511
+ const abortRef = react.useRef(false);
2512
+ const stop = react.useCallback(() => {
2513
+ abortRef.current = true;
2514
+ setIsRunning(false);
2515
+ }, []);
2516
+ const run = react.useCallback(async (message) => {
2517
+ setIsRunning(true);
2518
+ setError(null);
2519
+ setOutput("");
2520
+ setToolCalls([]);
2521
+ abortRef.current = false;
2522
+ try {
2523
+ const agent = client.agent({
2524
+ name: options.name,
2525
+ systemPrompt: options.systemPrompt,
2526
+ tools: options.tools,
2527
+ maxIterations: options.maxIterations,
2528
+ model: options.model
2529
+ });
2530
+ const result = await agent.run(message);
2531
+ if (abortRef.current) return null;
2532
+ setOutput(result.output || "");
2533
+ if (result.toolCalls) {
2534
+ const calls = result.toolCalls.map((tc) => ({
2535
+ tool: tc.tool || tc.name,
2536
+ args: tc.args || tc.arguments
2537
+ }));
2538
+ setToolCalls(calls);
2539
+ calls.forEach((tc) => options.onToolCall?.(tc.tool, tc.args));
2540
+ }
2541
+ return result.output || "";
2542
+ } catch (err) {
2543
+ const e = err instanceof Error ? err : new Error(String(err));
2544
+ setError(e);
2545
+ options.onError?.(e);
2546
+ return null;
2547
+ } finally {
2548
+ setIsRunning(false);
2549
+ }
2550
+ }, [client, options]);
2551
+ return { output, isRunning, error, toolCalls, run, stop };
2552
+ }
2553
+ function useUsage(client) {
2554
+ const [usage, setUsage] = react.useState(null);
2555
+ const [isLoading, setIsLoading] = react.useState(false);
2556
+ const [error, setError] = react.useState(null);
2557
+ const refresh = react.useCallback(async () => {
2558
+ setIsLoading(true);
2559
+ setError(null);
2560
+ try {
2561
+ const data = await client.getUsage();
2562
+ setUsage(data);
2563
+ } catch (err) {
2564
+ setError(err instanceof Error ? err : new Error(String(err)));
2565
+ } finally {
2566
+ setIsLoading(false);
2567
+ }
2568
+ }, [client]);
2569
+ react.useEffect(() => {
2570
+ refresh();
2571
+ }, [refresh]);
2572
+ return { usage, isLoading, error, refresh };
2573
+ }
2393
2574
 
2394
2575
  // src/observability.ts
2395
2576
  var consoleHooks = {
@@ -2432,6 +2613,7 @@ exports.Binario = Binario;
2432
2613
  exports.BinarioAI = BinarioAI;
2433
2614
  exports.BinarioAgent = BinarioAgent;
2434
2615
  exports.BinarioPaymentError = BinarioPaymentError;
2616
+ exports.BinarioProvider = BinarioProvider;
2435
2617
  exports.BinarioRateLimitError = BinarioRateLimitError;
2436
2618
  exports.BufferMemory = BufferMemory;
2437
2619
  exports.CLOUDFLARE_EMBEDDING_MODELS = CLOUDFLARE_EMBEDDING_MODELS;
@@ -2470,7 +2652,12 @@ exports.generateId = generateId;
2470
2652
  exports.parseStructuredOutput = parseStructuredOutput;
2471
2653
  exports.truncateMessages = truncateMessages;
2472
2654
  exports.truncateMessagesByCount = truncateMessagesByCount;
2655
+ exports.useAgent = useAgent;
2473
2656
  exports.useBinarioAgent = useBinarioAgent;
2657
+ exports.useBinarioClient = useBinarioClient;
2658
+ exports.useChat = useChat;
2659
+ exports.useStream = useStream;
2660
+ exports.useUsage = useUsage;
2474
2661
  exports.zodToJsonSchema = zodToJsonSchema;
2475
2662
  //# sourceMappingURL=index.cjs.map
2476
2663
  //# sourceMappingURL=index.cjs.map