paprflare-sdk 0.0.16 → 0.0.17

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.mts CHANGED
@@ -1394,6 +1394,7 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1394
1394
  private currentWPM;
1395
1395
  private thinkingStartTime?;
1396
1396
  private activeTools;
1397
+ private currentToolCall;
1397
1398
  private cursorInterval?;
1398
1399
  private typingQueue;
1399
1400
  private isTyping;
@@ -1419,19 +1420,43 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1419
1420
  */
1420
1421
  start(): void;
1421
1422
  /**
1422
- * Write text content with enhanced tracking
1423
+ * Write text content (method used by providers)
1424
+ */
1425
+ writeText(text: string): void;
1426
+ /**
1427
+ * Legacy write method (for backward compatibility)
1423
1428
  */
1424
1429
  write(text: string): void;
1430
+ /**
1431
+ * Start a tool call
1432
+ */
1433
+ startToolCall(name: string, id: string): void;
1434
+ /**
1435
+ * Write tool call delta (incremental arguments)
1436
+ */
1437
+ writeToolCallDelta(delta: string): void;
1438
+ /**
1439
+ * End a tool call with final arguments
1440
+ */
1441
+ endToolCall(args: Record<string, unknown>): void;
1442
+ /**
1443
+ * Write usage information
1444
+ */
1445
+ writeUsage(tokens: {
1446
+ promptTokens: number;
1447
+ completionTokens: number;
1448
+ totalTokens: number;
1449
+ }): void;
1425
1450
  /**
1426
1451
  * Set thinking status
1427
1452
  */
1428
1453
  setThinking(isThinking: boolean, message?: string): void;
1429
1454
  /**
1430
- * Start tool execution
1455
+ * Legacy startTool method (for backward compatibility)
1431
1456
  */
1432
1457
  startTool(id: string, name: string, input: any): void;
1433
1458
  /**
1434
- * Complete tool execution
1459
+ * Legacy completeTool method (for backward compatibility)
1435
1460
  */
1436
1461
  completeTool(toolId: string, content: any, success?: boolean, error?: string): void;
1437
1462
  /**
@@ -1447,16 +1472,16 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1447
1472
  */
1448
1473
  error(message: string): void;
1449
1474
  /**
1450
- * Flush all currently buffered events as an async generator.
1451
- * Use as: yield* writer.flush()
1452
- */
1475
+ * Flush all currently buffered events as an async generator.
1476
+ * CRITICAL: This clears the events array to prevent duplication
1477
+ */
1453
1478
  flush(): AsyncGenerator<StreamEvent>;
1454
1479
  /**
1455
1480
  * Mark stream as done
1456
1481
  */
1457
1482
  done(finalMessage?: any): void;
1458
1483
  /**
1459
- * Write usage info
1484
+ * Legacy usage method (for backward compatibility)
1460
1485
  */
1461
1486
  usage(tokens: {
1462
1487
  prompt: number;
@@ -1524,7 +1549,7 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1524
1549
  */
1525
1550
  getActiveTools(): ToolUseData[];
1526
1551
  /**
1527
- * Convert to async generator
1552
+ * Convert to async generator (legacy method)
1528
1553
  */
1529
1554
  toGenerator(): AsyncGenerator<StreamEvent>;
1530
1555
  /**
package/dist/index.d.ts CHANGED
@@ -1394,6 +1394,7 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1394
1394
  private currentWPM;
1395
1395
  private thinkingStartTime?;
1396
1396
  private activeTools;
1397
+ private currentToolCall;
1397
1398
  private cursorInterval?;
1398
1399
  private typingQueue;
1399
1400
  private isTyping;
@@ -1419,19 +1420,43 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1419
1420
  */
1420
1421
  start(): void;
1421
1422
  /**
1422
- * Write text content with enhanced tracking
1423
+ * Write text content (method used by providers)
1424
+ */
1425
+ writeText(text: string): void;
1426
+ /**
1427
+ * Legacy write method (for backward compatibility)
1423
1428
  */
1424
1429
  write(text: string): void;
1430
+ /**
1431
+ * Start a tool call
1432
+ */
1433
+ startToolCall(name: string, id: string): void;
1434
+ /**
1435
+ * Write tool call delta (incremental arguments)
1436
+ */
1437
+ writeToolCallDelta(delta: string): void;
1438
+ /**
1439
+ * End a tool call with final arguments
1440
+ */
1441
+ endToolCall(args: Record<string, unknown>): void;
1442
+ /**
1443
+ * Write usage information
1444
+ */
1445
+ writeUsage(tokens: {
1446
+ promptTokens: number;
1447
+ completionTokens: number;
1448
+ totalTokens: number;
1449
+ }): void;
1425
1450
  /**
1426
1451
  * Set thinking status
1427
1452
  */
1428
1453
  setThinking(isThinking: boolean, message?: string): void;
1429
1454
  /**
1430
- * Start tool execution
1455
+ * Legacy startTool method (for backward compatibility)
1431
1456
  */
1432
1457
  startTool(id: string, name: string, input: any): void;
1433
1458
  /**
1434
- * Complete tool execution
1459
+ * Legacy completeTool method (for backward compatibility)
1435
1460
  */
1436
1461
  completeTool(toolId: string, content: any, success?: boolean, error?: string): void;
1437
1462
  /**
@@ -1447,16 +1472,16 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1447
1472
  */
1448
1473
  error(message: string): void;
1449
1474
  /**
1450
- * Flush all currently buffered events as an async generator.
1451
- * Use as: yield* writer.flush()
1452
- */
1475
+ * Flush all currently buffered events as an async generator.
1476
+ * CRITICAL: This clears the events array to prevent duplication
1477
+ */
1453
1478
  flush(): AsyncGenerator<StreamEvent>;
1454
1479
  /**
1455
1480
  * Mark stream as done
1456
1481
  */
1457
1482
  done(finalMessage?: any): void;
1458
1483
  /**
1459
- * Write usage info
1484
+ * Legacy usage method (for backward compatibility)
1460
1485
  */
1461
1486
  usage(tokens: {
1462
1487
  prompt: number;
@@ -1524,7 +1549,7 @@ declare class StreamWriter extends EventEmitter$1<StreamWriterEvents> {
1524
1549
  */
1525
1550
  getActiveTools(): ToolUseData[];
1526
1551
  /**
1527
- * Convert to async generator
1552
+ * Convert to async generator (legacy method)
1528
1553
  */
1529
1554
  toGenerator(): AsyncGenerator<StreamEvent>;
1530
1555
  /**
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- 'use strict';var de=require('ioredis'),lruCache=require('lru-cache'),nanoid=require('nanoid'),oe=require('fast-json-stable-stringify'),me=require('eventemitter3'),eventsourceParser=require('eventsource-parser');require('zod');var he=require('p-queue'),asyncMutex=require('async-mutex'),drizzleOrm=require('drizzle-orm'),ae=require('@anthropic-ai/sdk'),generativeAi=require('@google/generative-ai'),re=require('openai');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var de__default=/*#__PURE__*/_interopDefault(de);var oe__default=/*#__PURE__*/_interopDefault(oe);var me__default=/*#__PURE__*/_interopDefault(me);var he__default=/*#__PURE__*/_interopDefault(he);var ae__default=/*#__PURE__*/_interopDefault(ae);var re__default=/*#__PURE__*/_interopDefault(re);function C(o){let e=nanoid.nanoid();return o?`${o}_${e}`:e}function q(o){return oe__default.default(o)}function $(o){return o.map(e=>({...e,id:e.id||C("msg"),timestamp:e.timestamp||Date.now()}))}async function F(o,e){let t,s=e.initialDelay;for(let a=1;a<=e.maxAttempts;a++)try{return await o()}catch(r){if(t=r,a===e.maxAttempts)throw t;e.onRetry&&e.onRetry(a,t),await ie(Math.min(s,e.maxDelay)),s*=e.backoffMultiplier;}throw t}function ie(o){return new Promise(e=>setTimeout(e,o))}function we(o,e){let t=[];for(let s=0;s<o.length;s+=e)t.push(o.slice(s,s+e));return t}function Ce(o,e){let t;return (...s)=>{clearTimeout(t),t=setTimeout(()=>o(...s),e);}}function Te(o,e){let t;return (...s)=>{t||(o(...s),t=true,setTimeout(()=>t=false,e));}}function be(o){try{return JSON.parse(o)}catch{return null}}function S(o){return typeof o=="object"&&o!==null&&o.constructor===Object}function G(o,...e){if(!e.length)return o;let t=e.shift();if(S(o)&&S(t))for(let s in t)S(t[s])?(o[s]||Object.assign(o,{[s]:{}}),G(o[s],t[s])):Object.assign(o,{[s]:t[s]});return G(o,...e)}async function Me(o){let e=[];for await(let t of o)e.push(t);return e}async function Pe(o){let e=performance.now(),t=await o(),s=performance.now()-e;return {result:t,duration:s}}var f=class extends me.EventEmitter{events=[];closed=false;fullText="";autoOutput;animation;status="idle";startTime=0;lastChunkTime=0;chunkIndex=0;paused=false;pauseStartTime=0;totalPauseDuration=0;progressInterval;speedCalculationWindow=[];currentWPM=0;thinkingStartTime;activeTools=new Map;cursorInterval;typingQueue=[];isTyping=false;constructor(e={}){super(),this.autoOutput={enabled:e.enabled!==false,showUsage:e.showUsage!==false,showFinalMessage:e.showFinalMessage!==false,showProgress:e.showProgress!==false,progressInterval:e.progressInterval||500,showThinking:e.showThinking!==false,showTools:e.showTools!==false,showSpeed:e.showSpeed||false,animation:e.animation||{},onText:e.onText||this.defaultTextHandler,onError:e.onError||this.defaultErrorHandler,onUsage:e.onUsage||this.defaultUsageHandler,onDone:e.onDone||this.defaultDoneHandler,onProgress:e.onProgress||this.defaultProgressHandler,onThinking:e.onThinking||this.defaultThinkingHandler,onToolUse:e.onToolUse||this.defaultToolUseHandler,onToolResult:e.onToolResult||this.defaultToolResultHandler,onStatusChange:e.onStatusChange||this.defaultStatusChangeHandler},this.animation={typing:e.animation?.typing||false,typingSpeed:e.animation?.typingSpeed||50,cursor:e.animation?.cursor||false,cursorChar:e.animation?.cursorChar||"\u258A",cursorBlinkInterval:e.animation?.cursorBlinkInterval||530,smoothScroll:e.animation?.smoothScroll||false,highlightWords:e.animation?.highlightWords||false,highlightDuration:e.animation?.highlightDuration||200},this.autoOutput.enabled&&this.setupAutoListeners(),this.animation.cursor&&this.startCursorAnimation();}defaultTextHandler=e=>{this.animation.typing?this.queueTyping(e):process.stdout.write(e);};defaultErrorHandler=e=>{console.error(`
1
+ 'use strict';var de=require('ioredis'),lruCache=require('lru-cache'),nanoid=require('nanoid'),oe=require('fast-json-stable-stringify'),me=require('eventemitter3'),eventsourceParser=require('eventsource-parser');require('zod');var he=require('p-queue'),asyncMutex=require('async-mutex'),drizzleOrm=require('drizzle-orm'),ae=require('@anthropic-ai/sdk'),generativeAi=require('@google/generative-ai'),re=require('openai');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var de__default=/*#__PURE__*/_interopDefault(de);var oe__default=/*#__PURE__*/_interopDefault(oe);var me__default=/*#__PURE__*/_interopDefault(me);var he__default=/*#__PURE__*/_interopDefault(he);var ae__default=/*#__PURE__*/_interopDefault(ae);var re__default=/*#__PURE__*/_interopDefault(re);function C(o){let e=nanoid.nanoid();return o?`${o}_${e}`:e}function q(o){return oe__default.default(o)}function $(o){return o.map(e=>({...e,id:e.id||C("msg"),timestamp:e.timestamp||Date.now()}))}async function F(o,e){let t,s=e.initialDelay;for(let a=1;a<=e.maxAttempts;a++)try{return await o()}catch(r){if(t=r,a===e.maxAttempts)throw t;e.onRetry&&e.onRetry(a,t),await ie(Math.min(s,e.maxDelay)),s*=e.backoffMultiplier;}throw t}function ie(o){return new Promise(e=>setTimeout(e,o))}function we(o,e){let t=[];for(let s=0;s<o.length;s+=e)t.push(o.slice(s,s+e));return t}function Ce(o,e){let t;return (...s)=>{clearTimeout(t),t=setTimeout(()=>o(...s),e);}}function Te(o,e){let t;return (...s)=>{t||(o(...s),t=true,setTimeout(()=>t=false,e));}}function be(o){try{return JSON.parse(o)}catch{return null}}function S(o){return typeof o=="object"&&o!==null&&o.constructor===Object}function G(o,...e){if(!e.length)return o;let t=e.shift();if(S(o)&&S(t))for(let s in t)S(t[s])?(o[s]||Object.assign(o,{[s]:{}}),G(o[s],t[s])):Object.assign(o,{[s]:t[s]});return G(o,...e)}async function Me(o){let e=[];for await(let t of o)e.push(t);return e}async function Pe(o){let e=performance.now(),t=await o(),s=performance.now()-e;return {result:t,duration:s}}var f=class extends me.EventEmitter{events=[];closed=false;fullText="";autoOutput;animation;status="idle";startTime=0;lastChunkTime=0;chunkIndex=0;paused=false;pauseStartTime=0;totalPauseDuration=0;progressInterval;speedCalculationWindow=[];currentWPM=0;thinkingStartTime;activeTools=new Map;currentToolCall=null;cursorInterval;typingQueue=[];isTyping=false;constructor(e={}){super(),this.autoOutput={enabled:e.enabled!==false,showUsage:e.showUsage!==false,showFinalMessage:e.showFinalMessage!==false,showProgress:e.showProgress!==false,progressInterval:e.progressInterval||500,showThinking:e.showThinking!==false,showTools:e.showTools!==false,showSpeed:e.showSpeed||false,animation:e.animation||{},onText:e.onText||this.defaultTextHandler,onError:e.onError||this.defaultErrorHandler,onUsage:e.onUsage||this.defaultUsageHandler,onDone:e.onDone||this.defaultDoneHandler,onProgress:e.onProgress||this.defaultProgressHandler,onThinking:e.onThinking||this.defaultThinkingHandler,onToolUse:e.onToolUse||this.defaultToolUseHandler,onToolResult:e.onToolResult||this.defaultToolResultHandler,onStatusChange:e.onStatusChange||this.defaultStatusChangeHandler},this.animation={typing:e.animation?.typing||false,typingSpeed:e.animation?.typingSpeed||50,cursor:e.animation?.cursor||false,cursorChar:e.animation?.cursorChar||"\u258A",cursorBlinkInterval:e.animation?.cursorBlinkInterval||530,smoothScroll:e.animation?.smoothScroll||false,highlightWords:e.animation?.highlightWords||false,highlightDuration:e.animation?.highlightDuration||200},this.autoOutput.enabled&&this.setupAutoListeners(),this.animation.cursor&&this.startCursorAnimation();}defaultTextHandler=e=>{this.animation.typing?this.queueTyping(e):process.stdout.write(e);};defaultErrorHandler=e=>{console.error(`
2
2
  \u274C Error:`,e);};defaultUsageHandler=e=>{this.autoOutput.showUsage&&console.log(`
3
3
 
4
4
  \u{1F4CA} Tokens: ${e.prompt} prompt + ${e.completion} completion = ${e.total} total`);};defaultDoneHandler=e=>{if(this.animation.cursor&&this.stopCursorAnimation(),this.autoOutput.showFinalMessage){let t=Date.now()-this.startTime;console.log(`
5
5
 
6
6
  \u2705 Stream completed in ${(t/1e3).toFixed(2)}s`),this.autoOutput.showSpeed&&this.currentWPM>0&&console.log(`\u26A1 Average speed: ${this.currentWPM.toFixed(0)} WPM`);}};defaultProgressHandler=e=>{if(!this.autoOutput.showProgress)return;let t=this.createProgressBar(e.percentage),s=(e.elapsed/1e3).toFixed(1);process.stdout.write(`\r\u{1F4DD} Progress: ${t} ${e.percentage.toFixed(0)}% | ${e.words} words | ${s}s`);};defaultThinkingHandler=e=>{this.autoOutput.showThinking&&(e.isThinking?process.stdout.write(`
7
7
  \u{1F4AD} ${e.message||"Thinking"}...`):e.duration&&console.log(` (${(e.duration/1e3).toFixed(1)}s)`));};defaultToolUseHandler=e=>{if(!this.autoOutput.showTools)return;console.log(`
8
- ${{starting:"\u{1F527}",running:"\u2699\uFE0F",completed:"\u2713",failed:"\u2717"}[e.status]} Tool: ${e.name} [${e.status}]`);};defaultToolResultHandler=e=>{if(!this.autoOutput.showTools)return;let t=e.success?"\u2705":"\u274C";console.log(`${t} Tool completed in ${(e.duration/1e3).toFixed(2)}s`);};defaultStatusChangeHandler=e=>{};setupAutoListeners(){this.on("write",this.autoOutput.onText),this.on("error",this.autoOutput.onError),this.on("usage",this.autoOutput.onUsage),this.on("done",this.autoOutput.onDone),this.on("progress",this.autoOutput.onProgress),this.on("thinking",this.autoOutput.onThinking),this.on("toolUse",this.autoOutput.onToolUse),this.on("toolResult",this.autoOutput.onToolResult),this.on("statusChange",this.autoOutput.onStatusChange);}start(){this.startTime===0&&(this.startTime=Date.now(),this.lastChunkTime=this.startTime,this.updateStatus("starting"),this.emit("start"),this.autoOutput.showProgress&&this.startProgressTracking(),setTimeout(()=>this.updateStatus("streaming"),50));}write(e){if(this.closed||this.paused)return;this.startTime===0&&this.start(),this.fullText+=e;let t=Date.now(),s=t-this.lastChunkTime;this.lastChunkTime=t;let a={text:e,index:this.chunkIndex++,timestamp:t,deltaTime:s};this.events.push({type:"text-delta",delta:e}),this.emit("chunk",a),this.emit("write",e),this.updateSpeed(e,s);}setThinking(e,t){if(e)this.thinkingStartTime=Date.now(),this.updateStatus("thinking");else if(this.thinkingStartTime){let s=Date.now()-this.thinkingStartTime;this.emit("thinking",{isThinking:false,duration:s}),this.thinkingStartTime=void 0,this.updateStatus("streaming");return}this.emit("thinking",{isThinking:e,message:t});}startTool(e,t,s){let a={id:e,name:t,input:s,status:"starting",timestamp:Date.now()};this.activeTools.set(e,a),this.updateStatus("tool_executing"),this.emit("toolUse",a),setTimeout(()=>{let r=this.activeTools.get(e);r&&(r.status="running",this.emit("toolUse",r));},100);}completeTool(e,t,s=true,a){let r=this.activeTools.get(e);if(!r)return;let n=Date.now()-r.timestamp;r.status=s?"completed":"failed",this.emit("toolUse",r),this.emit("toolResult",{toolId:e,content:t,success:s,error:a,duration:n,timestamp:Date.now()}),this.activeTools.delete(e),this.activeTools.size===0&&this.updateStatus("streaming");}pause(){this.paused||this.closed||(this.paused=true,this.pauseStartTime=Date.now(),this.updateStatus("paused"),this.emit("pause"),this.progressInterval&&clearInterval(this.progressInterval));}resume(){!this.paused||this.closed||(this.totalPauseDuration+=Date.now()-this.pauseStartTime,this.paused=false,this.updateStatus("resuming"),this.emit("resume"),setTimeout(()=>this.updateStatus("streaming"),50),this.autoOutput.showProgress&&this.startProgressTracking());}error(e){this.closed||(this.events.push({type:"error",error:e}),this.updateStatus("error"),this.emit("error",e));}async*flush(){let e=[...this.events];this.events=[];for(let t of e)yield t;}done(e){if(this.closed)return;this.updateStatus("completing");let t=e||{id:`msg_${Date.now()}`,role:"assistant",content:this.fullText,timestamp:Date.now()};this.events.push({type:"done",finalMessage:t}),this.closed=true,this.progressInterval&&clearInterval(this.progressInterval),this.updateStatus("done"),this.emit("done",t);}usage(e){this.closed||(this.events.push({type:"usage",usage:{promptTokens:e.prompt,completionTokens:e.completion,totalTokens:e.total}}),this.emit("usage",e));}metadata(e,t){let s={type:e,data:t,timestamp:Date.now()};this.emit("metadata",s);}updateStatus(e){this.status=e,this.emit("statusChange",e);}getStatus(){return this.status}startProgressTracking(){this.progressInterval&&clearInterval(this.progressInterval),this.progressInterval=setInterval(()=>{if(this.paused||this.closed)return;let e=Date.now()-this.startTime-this.totalPauseDuration,t=this.countWords(this.fullText),s=this.fullText.length,a=t>0?Math.max(t*1.5,100):100,r=Math.min(t/a*100,95),n={characters:s,words:t,percentage:r,elapsed:e};this.emit("progress",n);},this.autoOutput.progressInterval);}updateSpeed(e,t){if(!this.autoOutput.showSpeed||t===0)return;let a=this.countWords(e)/(t/1e3)*60;this.speedCalculationWindow.push(a),this.speedCalculationWindow.length>10&&this.speedCalculationWindow.shift(),this.currentWPM=this.speedCalculationWindow.reduce((r,n)=>r+n,0)/this.speedCalculationWindow.length,this.emit("speed",this.currentWPM);}queueTyping(e){this.typingQueue.push(...e.split("")),this.isTyping||this.processTypingQueue();}async processTypingQueue(){for(this.isTyping=true;this.typingQueue.length>0;){let e=this.typingQueue.shift();e&&(process.stdout.write(e),await this.sleep(1e3/this.animation.typingSpeed));}this.isTyping=false;}startCursorAnimation(){let e=true;this.cursorInterval=setInterval(()=>{this.closed||this.isTyping||(e?process.stdout.write(this.animation.cursorChar):process.stdout.write("\b \b"),e=!e);},this.animation.cursorBlinkInterval);}stopCursorAnimation(){this.cursorInterval&&(clearInterval(this.cursorInterval),process.stdout.write("\b \b"));}countWords(e){return e.trim().split(/\s+/).filter(Boolean).length}createProgressBar(e,t=20){let s=Math.round(e/100*t),a=t-s;return "\u2588".repeat(s)+"\u2591".repeat(a)}sleep(e){return new Promise(t=>setTimeout(t,e))}getSpeed(){return this.currentWPM}getProgress(){let e=this.closed?Date.now()-this.startTime-this.totalPauseDuration:Date.now()-this.startTime-this.totalPauseDuration,t=this.countWords(this.fullText),s=this.fullText.length,a=t>0?Math.max(t*1.5,100):100,r=Math.min(t/a*100,100);return {characters:s,words:t,percentage:r,elapsed:e}}isClosed(){return this.closed}isPaused(){return this.paused}getText(){return this.fullText}getActiveTools(){return Array.from(this.activeTools.values())}async*toGenerator(){for(let e of this.events)yield e;this.events=[];}getEvents(){return [...this.events]}reset(){this.progressInterval&&clearInterval(this.progressInterval),this.cursorInterval&&clearInterval(this.cursorInterval),this.events=[],this.fullText="",this.closed=false,this.status="idle",this.startTime=0,this.lastChunkTime=0,this.chunkIndex=0,this.paused=false,this.pauseStartTime=0,this.totalPauseDuration=0,this.speedCalculationWindow=[],this.currentWPM=0,this.thinkingStartTime=void 0,this.activeTools.clear(),this.typingQueue=[],this.isTyping=false,this.removeAllListeners(),this.autoOutput.enabled&&this.setupAutoListeners(),this.animation.cursor&&this.startCursorAnimation();}disableAutoOutput(){this.autoOutput.enabled=false,this.removeAllListeners();}enableAutoOutput(){this.autoOutput.enabled=true,this.setupAutoListeners();}};function ke(o){return new f(o)}async function*Ee(o){let e=o.getReader(),t=new TextDecoder,s="",a=eventsourceParser.createParser({onEvent:r=>{s=r.data;},onRetry:r=>{}});try{for(;;){let{done:r,value:n}=await e.read();if(r)break;let l=t.decode(n,{stream:!0});if(a.feed(l),s)try{let i=JSON.parse(s);i.type&&(yield i),s="";}catch{continue}}a.reset({consume:!0});}finally{e.releaseLock();}}function k(o){let e=new TextEncoder;return new ReadableStream({async start(t){try{for await(let s of o){let a=`data: ${JSON.stringify(s)}
8
+ ${{starting:"\u{1F527}",running:"\u2699\uFE0F",completed:"\u2713",failed:"\u2717"}[e.status]} Tool: ${e.name} [${e.status}]`);};defaultToolResultHandler=e=>{if(!this.autoOutput.showTools)return;let t=e.success?"\u2705":"\u274C";console.log(`${t} Tool completed in ${(e.duration/1e3).toFixed(2)}s`);};defaultStatusChangeHandler=e=>{};setupAutoListeners(){this.on("write",this.autoOutput.onText),this.on("error",this.autoOutput.onError),this.on("usage",this.autoOutput.onUsage),this.on("done",this.autoOutput.onDone),this.on("progress",this.autoOutput.onProgress),this.on("thinking",this.autoOutput.onThinking),this.on("toolUse",this.autoOutput.onToolUse),this.on("toolResult",this.autoOutput.onToolResult),this.on("statusChange",this.autoOutput.onStatusChange);}start(){this.startTime===0&&(this.startTime=Date.now(),this.lastChunkTime=this.startTime,this.updateStatus("starting"),this.emit("start"),this.autoOutput.showProgress&&this.startProgressTracking(),setTimeout(()=>this.updateStatus("streaming"),50));}writeText(e){if(this.closed||this.paused)return;this.startTime===0&&this.start(),this.fullText+=e;let t=Date.now(),s=t-this.lastChunkTime;this.lastChunkTime=t;let a={text:e,index:this.chunkIndex++,timestamp:t,deltaTime:s};this.events.push({type:"text-delta",delta:e}),this.emit("chunk",a),this.emit("write",e),this.updateSpeed(e,s);}write(e){this.writeText(e);}startToolCall(e,t){this.currentToolCall={id:t,name:e,argumentsBuffer:""};let s={id:t,name:e,input:{},status:"starting",timestamp:Date.now()};this.activeTools.set(t,s),this.updateStatus("tool_executing"),this.emit("toolUse",s),setTimeout(()=>{let a=this.activeTools.get(t);a&&(a.status="running",this.emit("toolUse",a));},100);}writeToolCallDelta(e){this.currentToolCall&&(this.currentToolCall.argumentsBuffer+=e);}endToolCall(e){if(!this.currentToolCall)return;let t=this.activeTools.get(this.currentToolCall.id);if(t){t.input=e,t.status="completed",this.emit("toolUse",t);let s=Date.now()-t.timestamp;this.emit("toolResult",{toolId:this.currentToolCall.id,content:e,success:true,duration:s,timestamp:Date.now()}),this.activeTools.delete(this.currentToolCall.id);}this.currentToolCall=null,this.activeTools.size===0&&this.updateStatus("streaming");}writeUsage(e){this.closed||(this.events.push({type:"usage",usage:{promptTokens:e.promptTokens,completionTokens:e.completionTokens,totalTokens:e.totalTokens}}),this.emit("usage",{prompt:e.promptTokens,completion:e.completionTokens,total:e.totalTokens}));}setThinking(e,t){if(e)this.thinkingStartTime=Date.now(),this.updateStatus("thinking");else if(this.thinkingStartTime){let s=Date.now()-this.thinkingStartTime;this.emit("thinking",{isThinking:false,duration:s}),this.thinkingStartTime=void 0,this.updateStatus("streaming");return}this.emit("thinking",{isThinking:e,message:t});}startTool(e,t,s){this.startToolCall(t,e),s&&this.endToolCall(s);}completeTool(e,t,s=true,a){let r=this.activeTools.get(e);if(!r)return;let n=Date.now()-r.timestamp;r.status=s?"completed":"failed",this.emit("toolUse",r),this.emit("toolResult",{toolId:e,content:t,success:s,error:a,duration:n,timestamp:Date.now()}),this.activeTools.delete(e),this.activeTools.size===0&&this.updateStatus("streaming");}pause(){this.paused||this.closed||(this.paused=true,this.pauseStartTime=Date.now(),this.updateStatus("paused"),this.emit("pause"),this.progressInterval&&clearInterval(this.progressInterval));}resume(){!this.paused||this.closed||(this.totalPauseDuration+=Date.now()-this.pauseStartTime,this.paused=false,this.updateStatus("resuming"),this.emit("resume"),setTimeout(()=>this.updateStatus("streaming"),50),this.autoOutput.showProgress&&this.startProgressTracking());}error(e){this.closed||(this.events.push({type:"error",error:e}),this.updateStatus("error"),this.emit("error",e));}async*flush(){let e=[...this.events];this.events=[];for(let t of e)yield t;}done(e){if(this.closed)return;this.updateStatus("completing");let t=e||{id:`msg_${Date.now()}`,role:"assistant",content:this.fullText,timestamp:Date.now()};this.events.push({type:"done",finalMessage:t}),this.closed=true,this.progressInterval&&clearInterval(this.progressInterval),this.updateStatus("done"),this.emit("done",t);}usage(e){this.writeUsage({promptTokens:e.prompt,completionTokens:e.completion,totalTokens:e.total});}metadata(e,t){let s={type:e,data:t,timestamp:Date.now()};this.emit("metadata",s);}updateStatus(e){this.status=e,this.emit("statusChange",e);}getStatus(){return this.status}startProgressTracking(){this.progressInterval&&clearInterval(this.progressInterval),this.progressInterval=setInterval(()=>{if(this.paused||this.closed)return;let e=Date.now()-this.startTime-this.totalPauseDuration,t=this.countWords(this.fullText),s=this.fullText.length,a=t>0?Math.max(t*1.5,100):100,r=Math.min(t/a*100,95),n={characters:s,words:t,percentage:r,elapsed:e};this.emit("progress",n);},this.autoOutput.progressInterval);}updateSpeed(e,t){if(!this.autoOutput.showSpeed||t===0)return;let a=this.countWords(e)/(t/1e3)*60;this.speedCalculationWindow.push(a),this.speedCalculationWindow.length>10&&this.speedCalculationWindow.shift(),this.currentWPM=this.speedCalculationWindow.reduce((r,n)=>r+n,0)/this.speedCalculationWindow.length,this.emit("speed",this.currentWPM);}queueTyping(e){this.typingQueue.push(...e.split("")),this.isTyping||this.processTypingQueue();}async processTypingQueue(){for(this.isTyping=true;this.typingQueue.length>0;){let e=this.typingQueue.shift();e&&(process.stdout.write(e),await this.sleep(1e3/this.animation.typingSpeed));}this.isTyping=false;}startCursorAnimation(){let e=true;this.cursorInterval=setInterval(()=>{this.closed||this.isTyping||(e?process.stdout.write(this.animation.cursorChar):process.stdout.write("\b \b"),e=!e);},this.animation.cursorBlinkInterval);}stopCursorAnimation(){this.cursorInterval&&(clearInterval(this.cursorInterval),process.stdout.write("\b \b"));}countWords(e){return e.trim().split(/\s+/).filter(Boolean).length}createProgressBar(e,t=20){let s=Math.round(e/100*t),a=t-s;return "\u2588".repeat(s)+"\u2591".repeat(a)}sleep(e){return new Promise(t=>setTimeout(t,e))}getSpeed(){return this.currentWPM}getProgress(){let e=this.closed?Date.now()-this.startTime-this.totalPauseDuration:Date.now()-this.startTime-this.totalPauseDuration,t=this.countWords(this.fullText),s=this.fullText.length,a=t>0?Math.max(t*1.5,100):100,r=Math.min(t/a*100,100);return {characters:s,words:t,percentage:r,elapsed:e}}isClosed(){return this.closed}isPaused(){return this.paused}getText(){return this.fullText}getActiveTools(){return Array.from(this.activeTools.values())}async*toGenerator(){yield*this.flush();}getEvents(){return [...this.events]}reset(){this.progressInterval&&clearInterval(this.progressInterval),this.cursorInterval&&clearInterval(this.cursorInterval),this.events=[],this.fullText="",this.closed=false,this.status="idle",this.startTime=0,this.lastChunkTime=0,this.chunkIndex=0,this.paused=false,this.pauseStartTime=0,this.totalPauseDuration=0,this.speedCalculationWindow=[],this.currentWPM=0,this.thinkingStartTime=void 0,this.activeTools.clear(),this.currentToolCall=null,this.typingQueue=[],this.isTyping=false,this.removeAllListeners(),this.autoOutput.enabled&&this.setupAutoListeners(),this.animation.cursor&&this.startCursorAnimation();}disableAutoOutput(){this.autoOutput.enabled=false,this.removeAllListeners();}enableAutoOutput(){this.autoOutput.enabled=true,this.setupAutoListeners();}};function ke(o){return new f(o)}async function*Ee(o){let e=o.getReader(),t=new TextDecoder,s="",a=eventsourceParser.createParser({onEvent:r=>{s=r.data;},onRetry:r=>{}});try{for(;;){let{done:r,value:n}=await e.read();if(r)break;let l=t.decode(n,{stream:!0});if(a.feed(l),s)try{let i=JSON.parse(s);i.type&&(yield i),s="";}catch{continue}}a.reset({consume:!0});}finally{e.releaseLock();}}function k(o){let e=new TextEncoder;return new ReadableStream({async start(t){try{for await(let s of o){let a=`data: ${JSON.stringify(s)}
9
9
 
10
- `;t.enqueue(e.encode(a));}t.close();}catch(s){t.error(s);}}})}async function J(o,e){let t=k(o),s={"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"};if(e.node&&e.node.res){let a=e.node.res;Object.entries(s).forEach(([l,i])=>{a.setHeader(l,i);});let r=t.getReader(),n=new TextDecoder;try{for(;;){let{done:l,value:i}=await r.read();if(l)break;let c=n.decode(i,{stream:!0});a.write(c);}}finally{a.end();}}else {if(e instanceof Response||typeof e.body<"u")return;{Object.entries(s).forEach(([n,l])=>{e.setHeader(n,l);});let a=t.getReader(),r=new TextDecoder;try{for(;;){let{done:n,value:l}=await a.read();if(n)break;let i=r.decode(l,{stream:!0});e.write(i);}}finally{e.end();}}}}async function*W(...o){let e=o.map(async s=>{let a=[];for await(let r of s)a.push(r);return a}),t=await Promise.all(e);for(let s of t)for(let a of s)yield a;}async function*Oe(o,e){for await(let t of o){let s=e(t);s!==null&&(yield s);}}async function*De(o,e){for await(let t of o)e(t)&&(yield t);}async function A(o){let e=[];for await(let t of o)e.push(t);return e}async function*Ie(o,e){let t=0;for await(let s of o){if(t>=e)break;yield s,t++;}}async function*Ue(o,e,t){let s=[],a=null,r=async()=>{if(s.length>0){let n=[...s];return s=[],n}return null};try{for await(let l of o)if(s.push(l),a&&(clearTimeout(a),a=null),s.length>=e){let i=await r();i&&(yield i);}else t&&(a=setTimeout(async()=>{let i=await r();},t));let n=await r();n&&(yield n);}finally{a&&clearTimeout(a);}}var p=class extends Error{constructor(t,s,a,r){super(t);this.code=s;this.statusCode=a;this.details=r;this.name="PaprFlareError";}},j=class extends p{constructor(t,s){super(t,"RATE_LIMIT_ERROR",429);this.retryAfter=s;}},H=class extends p{constructor(e,t){super(e,"VALIDATION_ERROR",400,t);}},v=class extends p{constructor(t,s,a){super(t,"PROVIDER_ERROR",a);this.provider=s;}};async function*b(o){let e=`msg_${Date.now()}`,t="",s={id:e,role:"assistant",content:"",timestamp:Date.now(),isComplete:false};yield {type:"message-start",messageId:e};try{for await(let a of o)switch(a.type){case "text-delta":t+=a.delta,s.content=t,yield {type:"text-delta",delta:a.delta};break;case "tool-call-start":s.toolCalls||(s.toolCalls=[]),s.toolCalls.push(a.toolCall),yield {type:"tool-call-start",toolCallId:a.toolCall.id,toolName:a.toolCall.name};break;case "tool-call-end":yield {type:"tool-call-complete",toolCallId:a.toolCall.id,arguments:a.toolCall.arguments};break;case "tool-result":s.toolResults||(s.toolResults=[]),s.toolResults.push(a.result),yield {type:"tool-result",toolCallId:a.result.toolCallId,result:a.result.result,success:!a.result.error};break;case "usage":s.metadata||(s.metadata={}),s.metadata.usage=a.usage,yield {type:"usage",tokens:{prompt:a.usage.promptTokens,completion:a.usage.completionTokens,total:a.usage.totalTokens}};break;case "metadata":s.metadata||(s.metadata={}),s.metadata={...s.metadata,...a.metadata};break;case "artifact-start":s.metadata||(s.metadata={}),s.metadata.artifacts||(s.metadata.artifacts=[]),s.metadata.artifacts.push({...a.artifact,content:""});break;case "artifact-delta":if(s.metadata?.artifacts){let n=s.metadata.artifacts[s.metadata.artifacts.length-1];n&&(n.content+=a.delta);}break;case "artifact-end":if(s.metadata?.artifacts){let n=s.metadata.artifacts[s.metadata.artifacts.length-1];n&&Object.assign(n,a.artifact);}break;case "done":s.isComplete=!0,s.content=t;let r={...s,...a.finalMessage};yield {type:"text-complete",fullText:t},yield {type:"message-complete",message:r};break;case "error":yield {type:"error",error:a.error};break}}catch(a){yield {type:"error",error:a.message||"Stream error"};}}async function B(o,e){let t=b(o),s=null;for await(let a of t)switch(a.type){case "message-start":await e.onStart?.(a.messageId);break;case "text-delta":await e.onText?.(a.delta);break;case "message-complete":s=a.message,await e.onComplete?.(a.message);break;case "error":throw await e.onError?.(a.error),new Error(a.error);case "tool-call-complete":await e.onToolCall?.(a.toolCallId,a.arguments);break;case "tool-result":await e.onToolResult?.({toolId:a.toolCallId,content:a.result,success:a.success,duration:0,timestamp:Date.now()});break;case "usage":await e.onUsage?.(a.tokens);break;case "progress":await e.onProgress?.(a.progress);break;case "thinking":await e.onThinking?.(a.status);break;case "status-change":await e.onStatusChange?.(a.status);break}if(!s)throw new Error("Stream completed without final message");return s}async function K(o){let e="";for await(let t of o)t.type==="text-delta"&&(e+=t.delta);return e}async function*Ge(o){let e=new f({enabled:false,showProgress:false,showThinking:false,showTools:false,showSpeed:false,showUsage:false,showFinalMessage:false,...o.writerOptions}),s=null;o.onProgress&&e.on("progress",o.onProgress),o.onThinking&&e.on("thinking",o.onThinking),o.onToolUse&&e.on("toolUse",o.onToolUse),o.onToolResult&&e.on("toolResult",o.onToolResult),o.onStatusChange&&e.on("statusChange",o.onStatusChange),o.onSpeed&&e.on("speed",o.onSpeed);let a=async n=>{for await(let l of n)switch(l.type){case "text-delta":e.write(l.delta);break;case "tool-call-start":e.startTool(l.toolCall.id,l.toolCall.name,l.toolCall.arguments);break;case "tool-call-end":e.getActiveTools().find(c=>c.id===l.toolCall.id)&&e.completeTool(l.toolCall.id,l.toolCall.arguments,true);break;case "tool-result":e.completeTool(l.result.toolCallId,l.result.result,!l.result.error,l.result.error);break;case "usage":e.usage({prompt:l.usage.promptTokens,completion:l.usage.completionTokens,total:l.usage.totalTokens});break;case "metadata":e.metadata("custom",l.metadata);break;case "artifact-start":e.metadata("custom",{artifactStart:l.artifact});break;case "artifact-delta":e.metadata("custom",{artifactDelta:l.delta});break;case "artifact-end":e.metadata("custom",{artifactEnd:l.artifact});break;case "error":e.error(l.error);break;case "done":e.done(l.finalMessage);break}},r=(async()=>{try{e.start(),await o.execute({writer:e,merge:a}),e.isClosed()||e.done();}catch(n){let l=o.onError?.(n)||n.message;e.error(l),e.done();}})();try{for await(let n of b(e.toGenerator()))yield n,n.type==="message-complete"&&(s=n.message);await r,o.onFinish&&s&&await o.onFinish({message:s});}catch(n){throw n}}async function z(o,e={}){let{showUsage:t=true,showProgress:s=false,showThinking:a=false,showTools:r=false,showSpeed:n=false,colors:l=true,animation:i}=e,c=new f({enabled:true,showUsage:t,showProgress:s,showThinking:a,showTools:r,showSpeed:n,showFinalMessage:false,animation:i});for await(let d of o)switch(d.type){case "text-delta":c.write(d.delta);break;case "tool-call-start":c.startTool(d.toolCall.id,d.toolCall.name,d.toolCall.arguments);break;case "tool-call-end":c.getActiveTools().find(h=>h.id===d.toolCall.id)&&c.completeTool(d.toolCall.id,d.toolCall.arguments,true);break;case "tool-result":c.completeTool(d.result.toolCallId,d.result.result,!d.result.error);break;case "usage":c.usage({prompt:d.usage.promptTokens,completion:d.usage.completionTokens,total:d.usage.totalTokens});break;case "error":c.error(d.error);break;case "done":c.done(d.finalMessage);break}console.log(`
10
+ `;t.enqueue(e.encode(a));}t.close();}catch(s){t.error(s);}}})}async function J(o,e){let t=k(o),s={"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"};if(e.node&&e.node.res){let a=e.node.res;Object.entries(s).forEach(([l,i])=>{a.setHeader(l,i);});let r=t.getReader(),n=new TextDecoder;try{for(;;){let{done:l,value:i}=await r.read();if(l)break;let c=n.decode(i,{stream:!0});a.write(c);}}finally{a.end();}}else {if(e instanceof Response||typeof e.body<"u")return;{Object.entries(s).forEach(([n,l])=>{e.setHeader(n,l);});let a=t.getReader(),r=new TextDecoder;try{for(;;){let{done:n,value:l}=await a.read();if(n)break;let i=r.decode(l,{stream:!0});e.write(i);}}finally{e.end();}}}}async function*W(...o){let e=o.map(async s=>{let a=[];for await(let r of s)a.push(r);return a}),t=await Promise.all(e);for(let s of t)for(let a of s)yield a;}async function*De(o,e){for await(let t of o){let s=e(t);s!==null&&(yield s);}}async function*Oe(o,e){for await(let t of o)e(t)&&(yield t);}async function A(o){let e=[];for await(let t of o)e.push(t);return e}async function*Ie(o,e){let t=0;for await(let s of o){if(t>=e)break;yield s,t++;}}async function*Ue(o,e,t){let s=[],a=null,r=async()=>{if(s.length>0){let n=[...s];return s=[],n}return null};try{for await(let l of o)if(s.push(l),a&&(clearTimeout(a),a=null),s.length>=e){let i=await r();i&&(yield i);}else t&&(a=setTimeout(async()=>{let i=await r();},t));let n=await r();n&&(yield n);}finally{a&&clearTimeout(a);}}var p=class extends Error{constructor(t,s,a,r){super(t);this.code=s;this.statusCode=a;this.details=r;this.name="PaprFlareError";}},j=class extends p{constructor(t,s){super(t,"RATE_LIMIT_ERROR",429);this.retryAfter=s;}},B=class extends p{constructor(e,t){super(e,"VALIDATION_ERROR",400,t);}},v=class extends p{constructor(t,s,a){super(t,"PROVIDER_ERROR",a);this.provider=s;}};async function*b(o){let e=`msg_${Date.now()}`,t="",s={id:e,role:"assistant",content:"",timestamp:Date.now(),isComplete:false};yield {type:"message-start",messageId:e};try{for await(let a of o)switch(a.type){case "text-delta":t+=a.delta,s.content=t,yield {type:"text-delta",delta:a.delta};break;case "tool-call-start":s.toolCalls||(s.toolCalls=[]),s.toolCalls.push(a.toolCall),yield {type:"tool-call-start",toolCallId:a.toolCall.id,toolName:a.toolCall.name};break;case "tool-call-end":yield {type:"tool-call-complete",toolCallId:a.toolCall.id,arguments:a.toolCall.arguments};break;case "tool-result":s.toolResults||(s.toolResults=[]),s.toolResults.push(a.result),yield {type:"tool-result",toolCallId:a.result.toolCallId,result:a.result.result,success:!a.result.error};break;case "usage":s.metadata||(s.metadata={}),s.metadata.usage=a.usage,yield {type:"usage",tokens:{prompt:a.usage.promptTokens,completion:a.usage.completionTokens,total:a.usage.totalTokens}};break;case "metadata":s.metadata||(s.metadata={}),s.metadata={...s.metadata,...a.metadata};break;case "artifact-start":s.metadata||(s.metadata={}),s.metadata.artifacts||(s.metadata.artifacts=[]),s.metadata.artifacts.push({...a.artifact,content:""});break;case "artifact-delta":if(s.metadata?.artifacts){let n=s.metadata.artifacts[s.metadata.artifacts.length-1];n&&(n.content+=a.delta);}break;case "artifact-end":if(s.metadata?.artifacts){let n=s.metadata.artifacts[s.metadata.artifacts.length-1];n&&Object.assign(n,a.artifact);}break;case "done":s.isComplete=!0,s.content=t;let r={...s,...a.finalMessage};yield {type:"text-complete",fullText:t},yield {type:"message-complete",message:r};break;case "error":yield {type:"error",error:a.error};break}}catch(a){yield {type:"error",error:a.message||"Stream error"};}}async function H(o,e){let t=b(o),s=null;for await(let a of t)switch(a.type){case "message-start":await e.onStart?.(a.messageId);break;case "text-delta":await e.onText?.(a.delta);break;case "message-complete":s=a.message,await e.onComplete?.(a.message);break;case "error":throw await e.onError?.(a.error),new Error(a.error);case "tool-call-complete":await e.onToolCall?.(a.toolCallId,a.arguments);break;case "tool-result":await e.onToolResult?.({toolId:a.toolCallId,content:a.result,success:a.success,duration:0,timestamp:Date.now()});break;case "usage":await e.onUsage?.(a.tokens);break;case "progress":await e.onProgress?.(a.progress);break;case "thinking":await e.onThinking?.(a.status);break;case "status-change":await e.onStatusChange?.(a.status);break}if(!s)throw new Error("Stream completed without final message");return s}async function K(o){let e="";for await(let t of o)t.type==="text-delta"&&(e+=t.delta);return e}async function*Ge(o){let e=new f({enabled:false,showProgress:false,showThinking:false,showTools:false,showSpeed:false,showUsage:false,showFinalMessage:false,...o.writerOptions}),s=null;o.onProgress&&e.on("progress",o.onProgress),o.onThinking&&e.on("thinking",o.onThinking),o.onToolUse&&e.on("toolUse",o.onToolUse),o.onToolResult&&e.on("toolResult",o.onToolResult),o.onStatusChange&&e.on("statusChange",o.onStatusChange),o.onSpeed&&e.on("speed",o.onSpeed);let a=async n=>{for await(let l of n)switch(l.type){case "text-delta":e.write(l.delta);break;case "tool-call-start":e.startTool(l.toolCall.id,l.toolCall.name,l.toolCall.arguments);break;case "tool-call-end":e.getActiveTools().find(c=>c.id===l.toolCall.id)&&e.completeTool(l.toolCall.id,l.toolCall.arguments,true);break;case "tool-result":e.completeTool(l.result.toolCallId,l.result.result,!l.result.error,l.result.error);break;case "usage":e.usage({prompt:l.usage.promptTokens,completion:l.usage.completionTokens,total:l.usage.totalTokens});break;case "metadata":e.metadata("custom",l.metadata);break;case "artifact-start":e.metadata("custom",{artifactStart:l.artifact});break;case "artifact-delta":e.metadata("custom",{artifactDelta:l.delta});break;case "artifact-end":e.metadata("custom",{artifactEnd:l.artifact});break;case "error":e.error(l.error);break;case "done":e.done(l.finalMessage);break}},r=(async()=>{try{e.start(),await o.execute({writer:e,merge:a}),e.isClosed()||e.done();}catch(n){let l=o.onError?.(n)||n.message;e.error(l),e.done();}})();try{for await(let n of b(e.toGenerator()))yield n,n.type==="message-complete"&&(s=n.message);await r,o.onFinish&&s&&await o.onFinish({message:s});}catch(n){throw n}}async function z(o,e={}){let{showUsage:t=true,showProgress:s=false,showThinking:a=false,showTools:r=false,showSpeed:n=false,colors:l=true,animation:i}=e,c=new f({enabled:true,showUsage:t,showProgress:s,showThinking:a,showTools:r,showSpeed:n,showFinalMessage:false,animation:i});for await(let d of o)switch(d.type){case "text-delta":c.write(d.delta);break;case "tool-call-start":c.startTool(d.toolCall.id,d.toolCall.name,d.toolCall.arguments);break;case "tool-call-end":c.getActiveTools().find(h=>h.id===d.toolCall.id)&&c.completeTool(d.toolCall.id,d.toolCall.arguments,true);break;case "tool-result":c.completeTool(d.result.toolCallId,d.result.result,!d.result.error);break;case "usage":c.usage({prompt:d.usage.promptTokens,completion:d.usage.completionTokens,total:d.usage.totalTokens});break;case "error":c.error(d.error);break;case "done":c.done(d.finalMessage);break}console.log(`
11
11
  `);}async function V(o,e){let t=b(o);if(!e)return new Response(new ReadableStream({async start(s){let a=new TextEncoder;try{for await(let r of t){let n=`data: ${JSON.stringify(r)}
12
12
 
13
13
  `;s.enqueue(a.encode(n));}s.enqueue(a.encode(`data: [DONE]
@@ -38,7 +38,7 @@ Please ensure:
38
38
 
39
39
  Parse error: ${g.message}`,"VALIDATION_ERROR",400,g)}}extractJSON(e){let t=e.replace(/```json\s*/gi,"").replace(/```\s*/g,""),s=t.match(/\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\}/),a=t.match(/\[(?:[^\[\]]|\[(?:[^\[\]]|\[[^\[\]]*\])*\])*\]/);if(s)return s[0].trim();if(a)return a[0].trim();let r=t.split(`
40
40
  `),n=[],l=false,i=0;for(let c of r){let d=c.trim();if(d.startsWith("{")||d.startsWith("[")){if(l=true,n=[c],i=(c.match(/\{/g)||[]).length-(c.match(/\}/g)||[]).length,i+=(c.match(/\[/g)||[]).length-(c.match(/\]/g)||[]).length,i===0)return c.trim();continue}if(l&&(n.push(c),i+=(c.match(/\{/g)||[]).length-(c.match(/\}/g)||[]).length,i+=(c.match(/\[/g)||[]).length-(c.match(/\]/g)||[]).length,i===0))return n.join(`
41
- `).trim()}return t.trim()}createStreamingResponse(e){return {stream:e,toReadableStream:()=>k(e),pipeToResponse:async t=>{await J(e,t);},mergeStreams:(...t)=>W(e,...t),toUIStream:()=>b(e),process:t=>B(e,t),toText:()=>K(e),toConsole:t=>z(e,t),toResponse:t=>V(e,t)}}async*withFinally(e,t){try{yield*e;}finally{await t();}}async*parseCloudStream(e){let t=e.getReader(),s=new TextDecoder;try{for(;;){let{done:a,value:r}=await t.read();if(a)break;let l=s.decode(r,{stream:!0}).split(`
42
- `);for(let i of l)if(i.startsWith("data: ")){let c=i.slice(6);if(c==="[DONE]")break;try{yield JSON.parse(c);}catch(d){console.error("Failed to parse event:",d);}}}}finally{t.releaseLock();}}async*cacheStream(e,t){for await(let s of e)t.push(s),yield s;}async*eventsToGenerator(e){for(let t of e)yield t;}eventsToResponse(e){let t=e.find(a=>a.type==="done"),s=e.find(a=>a.type==="usage");if(!t||t.type!=="done")throw new p("No completion event found in response","INVALID_RESPONSE");return {message:t.finalMessage,usage:s&&s.type==="usage"?s.usage:void 0}}validateConfig(e){if(e.temperature!==void 0&&(e.temperature<0||e.temperature>2))throw new p("Temperature must be between 0 and 2","VALIDATION_ERROR",400);if(e.maxTokens!==void 0&&e.maxTokens<1)throw new p("Max tokens must be greater than 0","VALIDATION_ERROR",400);if(e.output?.schema&&e.stream)throw new p("Structured output requires stream: false","VALIDATION_ERROR",400)}get database(){if(this.mode==="cloud")throw new p("Database is not available in cloud mode","INVALID_MODE");return this.databaseManager}get threads(){if(this.mode==="cloud")throw new p("Threads are not available in cloud mode","INVALID_MODE");if(!this.threadManager){if(!this.providerCallback)throw new p("Provider callback required for thread management","INVALID_CONFIG");let e=new o({mode:"self-managed",provider:this.providerCallback});this.threadManager=new T(e);}return this.threadManager}getQueueStats(){return {size:this.queue.size,pending:this.queue.pending,concurrency:this.queue.concurrency}}async clearCache(){this.cacheManager&&await this.cacheManager.clearAll();}};function It(o){return {schema:o,mode:"json"}}var U=class{client;defaultModel;constructor(e){this.client=new ae__default.default({apiKey:e.apiKey,baseURL:e.baseURL,timeout:e.timeout||3e4,maxRetries:e.maxRetries||3,defaultHeaders:e.headers}),this.defaultModel=e.model||"claude-3-5-sonnet-20241022";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=e.find(d=>d.role==="system"),r=e.filter(d=>d.role!=="system"),n=this.client.messages.stream({model:t.model||this.defaultModel,messages:this.formatMessages(r),max_tokens:t.maxTokens||4096,temperature:t.temperature,top_p:t.topP,stop_sequences:t.stop,...a&&{system:a.content},...t.tools&&{tools:this.formatTools(t.tools)}}),l=null,i="";for await(let d of n){if(d.type==="content_block_start"&&d.content_block?.type==="tool_use"&&(l=d.content_block,i="",s.startTool(l.id,l.name,{})),d.type==="content_block_delta"&&(d.delta?.type==="text_delta"&&s.write(d.delta.text),d.delta?.type==="input_json_delta"&&(i+=d.delta.partial_json)),d.type==="content_block_stop"&&l){try{let u=JSON.parse(l.input?JSON.stringify(l.input):i||"{}");s.completeTool(l.id,u,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}l=null,i="";}yield*s.flush();}let c=await n.finalMessage();c.usage&&s.usage({prompt:c.usage.input_tokens,completion:c.usage.output_tokens,total:c.usage.input_tokens+c.usage.output_tokens}),s.done(),yield*s.flush();}catch(a){throw a instanceof ae__default.default.APIError?new v(a.message,"anthropic",a.status):new v(`Anthropic provider error: ${a.message}`,"anthropic")}}formatMessages(e){return e.map(t=>{let s=[];if(t.content&&s.push({type:"text",text:t.content}),t.toolCalls)for(let a of t.toolCalls)s.push({type:"tool_use",id:a.id,name:a.name,input:a.arguments});if(t.toolResults)for(let a of t.toolResults)s.push({type:"tool_result",tool_use_id:a.toolCallId,content:JSON.stringify(a.result),...a.error&&{is_error:true}});return {role:t.role==="assistant"?"assistant":"user",content:s.length===1&&s[0].type==="text"?s[0].text:s}})}formatTools(e){return e?e.map(t=>({name:t.name,description:t.description,input_schema:{type:"object",properties:{},required:[]}})):[]}};function qt(o){return new U(o).createCallback()}var N=class{client;defaultModel;constructor(e){this.client=new generativeAi.GoogleGenerativeAI(e.apiKey),this.defaultModel=e.model||"gemini-1.5-flash";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=e.find(h=>h.role==="system"),r=this.client.getGenerativeModel({model:t.model||this.defaultModel,...a&&{systemInstruction:a.content},generationConfig:{temperature:t.temperature,maxOutputTokens:t.maxTokens,topP:t.topP,stopSequences:t.stop}}),n=this.formatMessages(e);if(n.length===0)throw new v("No messages to send","google");let l=n[n.length-1],i=n.slice(0,-1),d=await r.startChat({history:i}).sendMessageStream(l.parts);for await(let h of d.stream){let g=h.text();g&&(s.write(g),yield*s.flush());let w=h.functionCalls();if(w)for(let y of w)s.startTool(y.name,y.name,y.args||{}),s.completeTool(y.name,y.args,!0),yield*s.flush();}let u=await d.response;u.usageMetadata&&s.usage({prompt:u.usageMetadata.promptTokenCount??0,completion:u.usageMetadata.candidatesTokenCount??(u.usageMetadata.totalTokenCount??0)-(u.usageMetadata.promptTokenCount??0),total:u.usageMetadata.totalTokenCount??0}),s.done(),yield*s.flush();}catch(a){throw new v(`Google provider error: ${a.message}`,"google")}}formatMessages(e){return e.filter(t=>t.role!=="system").map(t=>({role:t.role==="assistant"?"model":"user",parts:[{text:t.content}]}))}formatTools(e){return e?e.map(t=>({name:t.name,description:t.description,parameters:{type:"object",properties:{},required:[]}})):[]}};function Ht(o){return new N(o).createCallback()}var _=class{client;defaultModel;constructor(e){this.client=new re__default.default({apiKey:e.apiKey,baseURL:e.baseURL,organization:e.organization,timeout:e.timeout||3e4,maxRetries:e.maxRetries||3,defaultHeaders:e.headers}),this.defaultModel=e.model||"gpt-4-turbo-preview";}createCallback(){return (e,t)=>this.streamChat(e,t)}async getFinalChatCompletion(e){let t=[...e].reverse().find(s=>s.usage!=null);return t?.usage?{usage:t.usage}:null}async*streamChat(e,t){let s=new f;try{let a=await this.client.chat.completions.create({model:t.model||this.defaultModel,messages:this.formatMessages(e),temperature:t.temperature,max_tokens:t.maxTokens,top_p:t.topP,frequency_penalty:t.frequencyPenalty,presence_penalty:t.presencePenalty,stop:t.stop,stream:!0,stream_options:{include_usage:!0},...t.tools&&{tools:this.formatTools(t.tools),tool_choice:t.toolChoice}}),r=null,n="",l=null,i=[];for await(let d of a){i.push(d);let u=d.choices[0]?.delta;if(u){if(u.content&&s.write(u.content),u.tool_calls)for(let h of u.tool_calls){let g=h.index;if(h.id){if(l&&n){try{let w=JSON.parse(n);s.completeTool(l.id,w,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}n="";}r=g,l=h,s.startTool(h.id,h.function?.name||"",{});}h.function?.arguments&&(n+=h.function.arguments);}if(d.choices[0]?.finish_reason&&l&&n)try{let h=JSON.parse(n);s.completeTool(l.id,h,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}yield*s.flush();}}let c=await this.getFinalChatCompletion(i);c?.usage&&s.usage({prompt:c.usage.prompt_tokens,completion:c.usage.completion_tokens,total:c.usage.total_tokens}),s.done(),yield*s.flush();}catch(a){throw a instanceof re__default.default.APIError?new v(a.message,"openai",a.status):new v(`OpenAI provider error: ${a.message}`,"openai")}}formatMessages(e){return e.map(t=>({role:t.role,content:t.content,...t.toolCalls&&{tool_calls:t.toolCalls.map(s=>({id:s.id,type:"function",function:{name:s.name,arguments:JSON.stringify(s.arguments)}}))},...t.toolResults&&t.role==="tool"&&{tool_call_id:t.toolResults[0]?.toolCallId}}))}formatTools(e){return e?e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:{type:"object",properties:{},required:[]}}})):[]}};function Yt(o){return new _(o).createCallback()}var L=class{config;baseURL;constructor(e){this.config=e,this.baseURL=e.baseURL||"https://api.sarvam.ai/v1";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=await fetch(`${this.baseURL}/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},body:JSON.stringify({model:t.model||this.config.model||"sarvam-m",messages:this.formatMessages(e),temperature:t.temperature,max_tokens:t.maxTokens,stream:!0})});if(!a.ok){let r=await a.json().catch(()=>({}));throw new v(r.error?.message||"Sarvam AI API error","sarvam",a.status)}yield*this.parseStream(a.body,s);}catch(a){throw a instanceof v?a:new v(`Sarvam provider error: ${a}`,"sarvam")}}async*parseStream(e,t){let s=e.getReader(),a=new TextDecoder,r="";try{for(;;){let{done:n,value:l}=await s.read();if(n)break;r+=a.decode(l,{stream:!0});let i=r.split(`
43
- `);r=i.pop()||"";for(let c of i){if(!c.trim()||!c.startsWith("data: "))continue;let d=c.slice(6);if(d==="[DONE]"){t.done(),yield*t.flush();return}try{let u=JSON.parse(d);if(u.choices?.[0]){let h=u.choices[0].delta;if(h?.content&&t.write(h.content),u.choices[0].finish_reason){t.done(),yield*t.flush();return}}u.usage&&t.usage({prompt:u.usage.prompt_tokens||0,completion:u.usage.completion_tokens||0,total:u.usage.total_tokens||0}),yield*t.flush();}catch(u){console.error("Failed to parse Sarvam chunk:",u);}}}}finally{s.releaseLock();}}formatMessages(e){return e.map(t=>({role:t.role,content:t.content}))}};function ss(o){return new L(o).createCallback()}exports.AnthropicProvider=U;exports.CacheManager=P;exports.DatabaseManager=x;exports.DrizzleAdapter=Z;exports.GoogleProvider=N;exports.MemoryAdapter=X;exports.MemoryCache=E;exports.OpenAIProvider=_;exports.PaprFlare=se;exports.PaprFlareError=p;exports.PrismaAdapter=ee;exports.ProviderError=v;exports.RateLimitError=j;exports.RedisCache=R;exports.SarvamProvider=L;exports.StreamWriter=f;exports.ThreadManager=T;exports.ThreadPool=te;exports.ValidationError=H;exports.batchStream=Ue;exports.chunk=we;exports.collectStream=A;exports.collectText=K;exports.createAnthropicProvider=qt;exports.createCacheKey=q;exports.createGoogleProvider=Ht;exports.createOpenAIProvider=Yt;exports.createSarvamProvider=ss;exports.createStreamWriter=ke;exports.createStreamableValue=qe;exports.createUIMessageStream=Ge;exports.debounce=Ce;exports.deepMerge=G;exports.filterStream=De;exports.formatMessages=$;exports.generateId=C;exports.isPlainObject=S;exports.measureTime=Pe;exports.mergeStreams=W;exports.parseSSEStream=Ee;exports.pipeToResponse=J;exports.processStream=B;exports.retry=F;exports.safeJsonParse=be;exports.sleep=ie;exports.streamToArray=Me;exports.streamToConsole=z;exports.structuredOutput=It;exports.takeStream=Ie;exports.throttle=Te;exports.toHTTPResponse=V;exports.toReadableStream=k;exports.toUIStream=b;exports.transformStream=Oe;//# sourceMappingURL=index.js.map
41
+ `).trim()}return t.trim()}createStreamingResponse(e){return {stream:e,toReadableStream:()=>k(e),pipeToResponse:async t=>{await J(e,t);},mergeStreams:(...t)=>W(e,...t),toUIStream:()=>b(e),process:t=>H(e,t),toText:()=>K(e),toConsole:t=>z(e,t),toResponse:t=>V(e,t)}}async*withFinally(e,t){try{yield*e;}finally{await t();}}async*parseCloudStream(e){let t=e.getReader(),s=new TextDecoder;try{for(;;){let{done:a,value:r}=await t.read();if(a)break;let l=s.decode(r,{stream:!0}).split(`
42
+ `);for(let i of l)if(i.startsWith("data: ")){let c=i.slice(6);if(c==="[DONE]")break;try{yield JSON.parse(c);}catch(d){console.error("Failed to parse event:",d);}}}}finally{t.releaseLock();}}async*cacheStream(e,t){for await(let s of e)t.push(s),yield s;}async*eventsToGenerator(e){for(let t of e)yield t;}eventsToResponse(e){let t=e.find(a=>a.type==="done"),s=e.find(a=>a.type==="usage");if(!t||t.type!=="done")throw new p("No completion event found in response","INVALID_RESPONSE");return {message:t.finalMessage,usage:s&&s.type==="usage"?s.usage:void 0}}validateConfig(e){if(e.temperature!==void 0&&(e.temperature<0||e.temperature>2))throw new p("Temperature must be between 0 and 2","VALIDATION_ERROR",400);if(e.maxTokens!==void 0&&e.maxTokens<1)throw new p("Max tokens must be greater than 0","VALIDATION_ERROR",400);if(e.output?.schema&&e.stream)throw new p("Structured output requires stream: false","VALIDATION_ERROR",400)}get database(){if(this.mode==="cloud")throw new p("Database is not available in cloud mode","INVALID_MODE");return this.databaseManager}get threads(){if(this.mode==="cloud")throw new p("Threads are not available in cloud mode","INVALID_MODE");if(!this.threadManager){if(!this.providerCallback)throw new p("Provider callback required for thread management","INVALID_CONFIG");let e=new o({mode:"self-managed",provider:this.providerCallback});this.threadManager=new T(e);}return this.threadManager}getQueueStats(){return {size:this.queue.size,pending:this.queue.pending,concurrency:this.queue.concurrency}}async clearCache(){this.cacheManager&&await this.cacheManager.clearAll();}};function It(o){return {schema:o,mode:"json"}}var U=class{client;defaultModel;constructor(e){this.client=new ae__default.default({apiKey:e.apiKey,baseURL:e.baseURL,timeout:e.timeout||3e4,maxRetries:e.maxRetries||3,defaultHeaders:e.headers}),this.defaultModel=e.model||"claude-3-5-sonnet-20241022";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=e.find(d=>d.role==="system"),r=e.filter(d=>d.role!=="system"),n=this.client.messages.stream({model:t.model||this.defaultModel,messages:this.formatMessages(r),max_tokens:t.maxTokens||4096,temperature:t.temperature,top_p:t.topP,stop_sequences:t.stop,...a&&{system:a.content},...t.tools&&{tools:this.formatTools(t.tools)}}),l=null,i="";for await(let d of n){if(d.type==="content_block_start"&&d.content_block?.type==="tool_use"&&(l=d.content_block,i="",s.startTool(l.id,l.name,{})),d.type==="content_block_delta"&&(d.delta?.type==="text_delta"&&s.write(d.delta.text),d.delta?.type==="input_json_delta"&&(i+=d.delta.partial_json)),d.type==="content_block_stop"&&l){try{let u=JSON.parse(l.input?JSON.stringify(l.input):i||"{}");s.completeTool(l.id,u,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}l=null,i="";}yield*s.flush();}let c=await n.finalMessage();c.usage&&s.usage({prompt:c.usage.input_tokens,completion:c.usage.output_tokens,total:c.usage.input_tokens+c.usage.output_tokens}),s.done(),yield*s.flush();}catch(a){throw a instanceof ae__default.default.APIError?new v(a.message,"anthropic",a.status):new v(`Anthropic provider error: ${a.message}`,"anthropic")}}formatMessages(e){return e.map(t=>{let s=[];if(t.content&&s.push({type:"text",text:t.content}),t.toolCalls)for(let a of t.toolCalls)s.push({type:"tool_use",id:a.id,name:a.name,input:a.arguments});if(t.toolResults)for(let a of t.toolResults)s.push({type:"tool_result",tool_use_id:a.toolCallId,content:JSON.stringify(a.result),...a.error&&{is_error:true}});return {role:t.role==="assistant"?"assistant":"user",content:s.length===1&&s[0].type==="text"?s[0].text:s}})}formatTools(e){return e?e.map(t=>({name:t.name,description:t.description,input_schema:{type:"object",properties:{},required:[]}})):[]}};function qt(o){return new U(o).createCallback()}var N=class{client;defaultModel;constructor(e){this.client=new generativeAi.GoogleGenerativeAI(e.apiKey),this.defaultModel=e.model||"gemini-1.5-flash";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=e.find(h=>h.role==="system"),r=this.client.getGenerativeModel({model:t.model||this.defaultModel,...a&&{systemInstruction:a.content},generationConfig:{temperature:t.temperature,maxOutputTokens:t.maxTokens,topP:t.topP,stopSequences:t.stop}}),n=this.formatMessages(e);if(n.length===0)throw new v("No messages to send","google");let l=n[n.length-1],i=n.slice(0,-1),d=await r.startChat({history:i}).sendMessageStream(l.parts);for await(let h of d.stream){let g=h.text();g&&(s.write(g),yield*s.flush());let w=h.functionCalls();if(w)for(let y of w)s.startTool(y.name,y.name,y.args||{}),s.completeTool(y.name,y.args,!0),yield*s.flush();}let u=await d.response;u.usageMetadata&&s.usage({prompt:u.usageMetadata.promptTokenCount??0,completion:u.usageMetadata.candidatesTokenCount??(u.usageMetadata.totalTokenCount??0)-(u.usageMetadata.promptTokenCount??0),total:u.usageMetadata.totalTokenCount??0}),s.done(),yield*s.flush();}catch(a){throw new v(`Google provider error: ${a.message}`,"google")}}formatMessages(e){return e.filter(t=>t.role!=="system").map(t=>({role:t.role==="assistant"?"model":"user",parts:[{text:t.content}]}))}formatTools(e){return e?e.map(t=>({name:t.name,description:t.description,parameters:{type:"object",properties:{},required:[]}})):[]}};function Bt(o){return new N(o).createCallback()}var _=class{client;defaultModel;constructor(e){this.client=new re__default.default({apiKey:e.apiKey,baseURL:e.baseURL,organization:e.organization,timeout:e.timeout||3e4,maxRetries:e.maxRetries||3,defaultHeaders:e.headers}),this.defaultModel=e.model||"gpt-4-turbo-preview";}createCallback(){return (e,t)=>this.streamChat(e,t)}async getFinalChatCompletion(e){let t=[...e].reverse().find(s=>s.usage!=null);return t?.usage?{usage:t.usage}:null}async*streamChat(e,t){let s=new f;try{let a=await this.client.chat.completions.create({model:t.model||this.defaultModel,messages:this.formatMessages(e),temperature:t.temperature,max_tokens:t.maxTokens,top_p:t.topP,frequency_penalty:t.frequencyPenalty,presence_penalty:t.presencePenalty,stop:t.stop,stream:!0,stream_options:{include_usage:!0},...t.tools&&{tools:this.formatTools(t.tools),tool_choice:t.toolChoice}}),r=null,n="",l=null,i=[];for await(let d of a){i.push(d);let u=d.choices[0]?.delta;if(u){if(u.content&&s.write(u.content),u.tool_calls)for(let h of u.tool_calls){let g=h.index;if(h.id){if(l&&n){try{let w=JSON.parse(n);s.completeTool(l.id,w,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}n="";}r=g,l=h,s.startTool(h.id,h.function?.name||"",{});}h.function?.arguments&&(n+=h.function.arguments);}if(d.choices[0]?.finish_reason&&l&&n)try{let h=JSON.parse(n);s.completeTool(l.id,h,!0);}catch{s.completeTool(l.id,{},!1,"Invalid JSON");}yield*s.flush();}}let c=await this.getFinalChatCompletion(i);c?.usage&&s.usage({prompt:c.usage.prompt_tokens,completion:c.usage.completion_tokens,total:c.usage.total_tokens}),s.done(),yield*s.flush();}catch(a){throw a instanceof re__default.default.APIError?new v(a.message,"openai",a.status):new v(`OpenAI provider error: ${a.message}`,"openai")}}formatMessages(e){return e.map(t=>({role:t.role,content:t.content,...t.toolCalls&&{tool_calls:t.toolCalls.map(s=>({id:s.id,type:"function",function:{name:s.name,arguments:JSON.stringify(s.arguments)}}))},...t.toolResults&&t.role==="tool"&&{tool_call_id:t.toolResults[0]?.toolCallId}}))}formatTools(e){return e?e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:{type:"object",properties:{},required:[]}}})):[]}};function Yt(o){return new _(o).createCallback()}var L=class{config;baseURL;constructor(e){this.config=e,this.baseURL=e.baseURL||"https://api.sarvam.ai/v1";}createCallback(){return (e,t)=>this.streamChat(e,t)}async*streamChat(e,t){let s=new f;try{let a=await fetch(`${this.baseURL}/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},body:JSON.stringify({model:t.model||this.config.model||"sarvam-m",messages:this.formatMessages(e),temperature:t.temperature,max_tokens:t.maxTokens,stream:!0})});if(!a.ok){let r=await a.json().catch(()=>({}));throw new v(r.error?.message||"Sarvam AI API error","sarvam",a.status)}yield*this.parseStream(a.body,s);}catch(a){throw a instanceof v?a:new v(`Sarvam provider error: ${a}`,"sarvam")}}async*parseStream(e,t){let s=e.getReader(),a=new TextDecoder,r="";try{for(;;){let{done:n,value:l}=await s.read();if(n)break;r+=a.decode(l,{stream:!0});let i=r.split(`
43
+ `);r=i.pop()||"";for(let c of i){if(!c.trim()||!c.startsWith("data: "))continue;let d=c.slice(6);if(d==="[DONE]"){t.done(),yield*t.flush();return}try{let u=JSON.parse(d);if(u.choices?.[0]){let h=u.choices[0].delta;if(h?.content&&t.write(h.content),u.choices[0].finish_reason){t.done(),yield*t.flush();return}}u.usage&&t.usage({prompt:u.usage.prompt_tokens||0,completion:u.usage.completion_tokens||0,total:u.usage.total_tokens||0}),yield*t.flush();}catch(u){console.error("Failed to parse Sarvam chunk:",u);}}}}finally{s.releaseLock();}}formatMessages(e){return e.map(t=>({role:t.role,content:t.content}))}};function ss(o){return new L(o).createCallback()}exports.AnthropicProvider=U;exports.CacheManager=P;exports.DatabaseManager=x;exports.DrizzleAdapter=Z;exports.GoogleProvider=N;exports.MemoryAdapter=X;exports.MemoryCache=E;exports.OpenAIProvider=_;exports.PaprFlare=se;exports.PaprFlareError=p;exports.PrismaAdapter=ee;exports.ProviderError=v;exports.RateLimitError=j;exports.RedisCache=R;exports.SarvamProvider=L;exports.StreamWriter=f;exports.ThreadManager=T;exports.ThreadPool=te;exports.ValidationError=B;exports.batchStream=Ue;exports.chunk=we;exports.collectStream=A;exports.collectText=K;exports.createAnthropicProvider=qt;exports.createCacheKey=q;exports.createGoogleProvider=Bt;exports.createOpenAIProvider=Yt;exports.createSarvamProvider=ss;exports.createStreamWriter=ke;exports.createStreamableValue=qe;exports.createUIMessageStream=Ge;exports.debounce=Ce;exports.deepMerge=G;exports.filterStream=Oe;exports.formatMessages=$;exports.generateId=C;exports.isPlainObject=S;exports.measureTime=Pe;exports.mergeStreams=W;exports.parseSSEStream=Ee;exports.pipeToResponse=J;exports.processStream=H;exports.retry=F;exports.safeJsonParse=be;exports.sleep=ie;exports.streamToArray=Me;exports.streamToConsole=z;exports.structuredOutput=It;exports.takeStream=Ie;exports.throttle=Te;exports.toHTTPResponse=V;exports.toReadableStream=k;exports.toUIStream=b;exports.transformStream=De;//# sourceMappingURL=index.js.map
44
44
  //# sourceMappingURL=index.js.map