@dsrcjs/ai-widget 1.0.7 → 1.0.9

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.
@@ -1,8 +0,0 @@
1
- import{S as u,b as I,a as v,r as A,o as E,c as M,t as O}from"./index-ve2JSWl1.js";const S=()=>({parts:[]}),w=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),i=e=>typeof e=="string";function N(e,t){w(t)&&(e.metadata={...e.metadata,...t})}function b(e,t){for(const r of e.parts)if(r.type==="text"&&r.id===t)return r;const n={type:"text",id:t,text:""};return e.parts.push(n),n}function T(e,t,n){if(n.transient===!0)return;const r=i(n.id)?n.id:void 0,s=r==null?void 0:e.parts.find(a=>a.type===t&&a.id===r);s!=null&&s.type!=="text"?s.data=n.data:e.parts.push({type:t,...r!=null?{id:r}:{},data:n.data})}function D(e,t){if(!w(t)||!i(t.type))throw new u("stream_error");switch(t.type){case"start":case"finish":case"message-metadata":N(e,t.messageMetadata);return;case"text-start":i(t.id)&&b(e,t.id);return;case"text-delta":i(t.id)&&i(t.delta)&&(b(e,t.id).text+=t.delta);return;case"data-step":case"data-attachment":case"data-block":T(e,t.type,t);return;case"error":throw new u(i(t.errorText)&&t.errorText!==""?t.errorText:"stream_error");default:return}}const _="[DONE]";function g(){const e=new Error("The operation was aborted.");return e.name="AbortError",e}function j(e){let t;for(const n of e.split(`
2
- `)){if(n.startsWith(":"))continue;const r=n.indexOf(":");if((r===-1?n:n.slice(0,r))!=="data")continue;let s=r===-1?"":n.slice(r+1);s.startsWith(" ")&&(s=s.slice(1)),(t??(t=[])).push(s)}return t?.join(`
3
- `)}async function*k(e,t){const n=e.getReader(),r=()=>{n.cancel().catch(()=>{})};t?.addEventListener("abort",r,{once:!0});const s=new TextDecoder;let a="";try{if(t?.aborted)throw g();for(;;){const{done:d,value:c}=await n.read();if(t?.aborted)throw g();if(d)return;a+=s.decode(c,{stream:!0});const o=a.endsWith("\r")?"\r":"";a=a.slice(0,a.length-o.length).replace(/\r\n?/g,`
4
- `);for(let l=a.indexOf(`
5
-
6
- `);l!==-1;l=a.indexOf(`
7
-
8
- `)){const f=j(a.slice(0,l));if(a=a.slice(l+2),f===_)return;f!==void 0&&(yield f)}a+=o}}finally{t?.removeEventListener("abort",r),r()}}const p=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m=e=>typeof e=="string"&&e!=="";function y(e){return m(e)?e:typeof e=="number"&&Number.isInteger(e)?String(e):void 0}function C(e){if(!p(e))return;const t=y(e.threadId),n=y(e.userMessageId),r=y(e.assistantMessageId);if(!(t==null||n==null||r==null||typeof e.mode!="string"))return{threadId:t,userMessageId:n,assistantMessageId:r,mode:e.mode}}function W(e,t){if(!p(t)||!m(t.node)||t.status!=="running"&&t.status!=="done"||typeof t.attempt!="number"||!Number.isInteger(t.attempt)||t.attempt<=0)return;const n={id:e,node:t.node,status:t.status,attempt:t.attempt},{durationMs:r}=t;if(r!==void 0){if(typeof r!="number"||r<0)return;n.durationMs=r}const{label:s,total:a}=t;return typeof s=="string"&&(n.label=s),(a===null||typeof a=="number"&&Number.isFinite(a))&&(n.total=a),n}function L(e){if(!p(e)||!m(e.url))return;const{name:t}=e;if(!(t!=null&&typeof t!="string"))return{name:t?.trim()?t:R(e.url),url:e.url}}function R(e){const t=e.split("?")[0]??e,n=t.slice(t.lastIndexOf("/")+1);try{return decodeURIComponent(n)}catch{return n}}function x(e){const t={text:"",steps:[],attachments:[],blocks:[],metadata:C(e.metadata)};for(const n of e.parts)if(n.type==="text")t.text+=n.text;else if(n.type==="data-step"){const r=n.id!=null?W(n.id,n.data):void 0;r!=null&&t.steps.push(r)}else if(n.type==="data-block"){const r=O(n.data);r!=null&&t.blocks.push(r)}else{const r=L(n.data);r!=null&&t.attachments.push(r)}return t}async function h(e,t,n){const r=await I(e),s=S();let a=!1,d=0;const c=()=>{d=Date.now(),a=!1,t(x(s))};try{for await(const o of k(r,n))D(s,U(o)),a=!0,Date.now()-d>=v&&c()}catch(o){throw a&&c(),o instanceof Error&&o.name==="AbortError"||o instanceof u?o:new u(o instanceof Error?o.message:"stream_error")}a&&c()}function U(e){try{return JSON.parse(e)}catch{throw new u("stream_error")}}async function F({transport:e,request:t,threadId:n,signal:r,onSnapshot:s}){const a=await E(e,M(t,n),r);await h(a,s,r)}async function J(e,t,n,r){const s=await A(e,t,n);await h(s,r,n)}export{h as readUiMessageStream,J as resumeChatAnswer,F as streamChatAnswer,x as toSnapshot};