@ag-ui/client 0.0.30 → 0.0.32
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 +187 -76
- package/dist/index.d.ts +187 -76
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
1
|
+
var ee=Object.defineProperty,te=Object.defineProperties;var ne=Object.getOwnPropertyDescriptors;var q=Object.getOwnPropertySymbols;var ae=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var V=(p,o,t)=>o in p?ee(p,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):p[o]=t,I=(p,o)=>{for(var t in o||(o={}))ae.call(o,t)&&V(p,t,o[t]);if(q)for(var t of q(o))se.call(o,t)&&V(p,t,o[t]);return p},G=(p,o)=>te(p,ne(o));import{EventType as y}from"@ag-ui/core";import{mergeAll as oe,defaultIfEmpty as re,concatMap as ie}from"rxjs/operators";import{of as le,EMPTY as ge}from"rxjs";var R=p=>{if(typeof structuredClone=="function")return structuredClone(p);try{return JSON.parse(JSON.stringify(p))}catch(o){return I({},p)}};import{applyPatch as Ee}from"fast-json-patch";async function C(p,o,t,n){let e=o,s=t,i;for(let r of p)try{let l=await n(r,R(e),R(s));if(l===void 0)continue;if(l.messages!==void 0&&(e=l.messages),l.state!==void 0&&(s=l.state),i=l.stopPropagation,i===!0)break}catch(l){process.env.NODE_ENV==="test"||process.env.JEST_WORKER_ID!==void 0||console.error("Subscriber error:",l);continue}return I(I(I({},JSON.stringify(e)!==JSON.stringify(o)?{messages:e}:{}),JSON.stringify(s)!==JSON.stringify(t)?{state:s}:{}),i!==void 0?{stopPropagation:i}:{})}import ce from"untruncate-json";var K=(p,o,t,n)=>{let e=R(p.messages),s=R(p.state),i={},r=c=>{c.messages!==void 0&&(e=c.messages,i.messages=c.messages),c.state!==void 0&&(s=c.state,i.state=c.state)},l=()=>{let c=R(i);return i={},c.messages!==void 0||c.state!==void 0?le(c):ge};return o.pipe(ie(async c=>{var T;let d=await C(n,e,s,(u,a,E)=>{var S;return(S=u.onEvent)==null?void 0:S.call(u,{event:c,agent:t,input:p,messages:a,state:E})});if(r(d),d.stopPropagation===!0)return l();switch(c.type){case y.TEXT_MESSAGE_START:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onTextMessageStartEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{messageId:a,role:E}=c,S={id:a,role:E,content:""};e.push(S),r({messages:e})}return l()}case y.TEXT_MESSAGE_CONTENT:{let u=await C(n,e,s,(a,E,S)=>{var g,N;return(N=a.onTextMessageContentEvent)==null?void 0:N.call(a,{event:c,messages:E,state:S,agent:t,input:p,textMessageBuffer:(g=E[E.length-1].content)!=null?g:""})});if(r(u),u.stopPropagation!==!0){let{delta:a}=c,E=e[e.length-1];E.content=E.content+a,r({messages:e})}return l()}case y.TEXT_MESSAGE_END:{let u=await C(n,e,s,(a,E,S)=>{var g,N;return(N=a.onTextMessageEndEvent)==null?void 0:N.call(a,{event:c,messages:E,state:S,agent:t,input:p,textMessageBuffer:(g=E[E.length-1].content)!=null?g:""})});return r(u),await Promise.all(n.map(a=>{var E;(E=a.onNewMessage)==null||E.call(a,{message:e[e.length-1],messages:e,state:s,agent:t,input:p})})),l()}case y.TOOL_CALL_START:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onToolCallStartEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{toolCallId:a,toolCallName:E,parentMessageId:S}=c,g;S&&e.length>0&&e[e.length-1].id===S?g=e[e.length-1]:(g={id:S||a,role:"assistant",toolCalls:[]},e.push(g)),(T=g.toolCalls)!=null||(g.toolCalls=[]),g.toolCalls.push({id:a,type:"function",function:{name:E,arguments:""}}),r({messages:e})}return l()}case y.TOOL_CALL_ARGS:{let u=await C(n,e,s,(a,E,S)=>{var P,D,H;let g=(D=(P=E[E.length-1])==null?void 0:P.toolCalls)!=null?D:[],N=g.length>0?g[g.length-1].function.arguments:"",w=g.length>0?g[g.length-1].function.name:"",O={};try{O=ce(N)}catch(b){}return(H=a.onToolCallArgsEvent)==null?void 0:H.call(a,{event:c,messages:E,state:S,agent:t,input:p,toolCallBuffer:N,toolCallName:w,partialToolCallArgs:O})});if(r(u),u.stopPropagation!==!0){let{delta:a}=c,E=e[e.length-1],S=E.toolCalls[E.toolCalls.length-1];S.function.arguments+=a,r({messages:e})}return l()}case y.TOOL_CALL_END:{let u=await C(n,e,s,(a,E,S)=>{var P,D,H;let g=(D=(P=E[E.length-1])==null?void 0:P.toolCalls)!=null?D:[],N=g.length>0?g[g.length-1].function.arguments:"",w=g.length>0?g[g.length-1].function.name:"",O={};try{O=JSON.parse(N)}catch(b){}return(H=a.onToolCallEndEvent)==null?void 0:H.call(a,{event:c,messages:E,state:S,agent:t,input:p,toolCallName:w,toolCallArgs:O})});return r(u),await Promise.all(n.map(a=>{var E;(E=a.onNewToolCall)==null||E.call(a,{toolCall:e[e.length-1].toolCalls[e[e.length-1].toolCalls.length-1],messages:e,state:s,agent:t,input:p})})),l()}case y.TOOL_CALL_RESULT:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onToolCallResultEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{messageId:a,toolCallId:E,content:S,role:g}=c,N={id:a,toolCallId:E,role:g||"tool",content:S};e.push(N),await Promise.all(n.map(w=>{var O;(O=w.onNewMessage)==null||O.call(w,{message:N,messages:e,state:s,agent:t,input:p})})),r({messages:e})}return l()}case y.STATE_SNAPSHOT:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onStateSnapshotEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{snapshot:a}=c;s=a,r({state:s})}return l()}case y.STATE_DELTA:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onStateDeltaEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{delta:a}=c;try{s=Ee(s,a,!0,!1).newDocument,r({state:s})}catch(E){let S=E instanceof Error?E.message:String(E);console.warn(`Failed to apply state patch:
|
|
2
2
|
Current state: ${JSON.stringify(s,null,2)}
|
|
3
|
-
Patch operations: ${JSON.stringify(
|
|
4
|
-
Error: ${
|
|
5
|
-
`),r=[];for(let
|
|
6
|
-
`),C=JSON.parse(i);n.next(C)}catch(i){n.error(i)}}return n.asObservable()};import{Subject as ie}from"rxjs";import*as J from"@ag-ui/proto";var P=g=>{let n=new ie,t=new Uint8Array(0);g.subscribe({next:s=>{if(s.type!=="headers"&&s.type==="data"&&s.data){let l=new Uint8Array(t.length+s.data.length);l.set(t,0),l.set(s.data,t.length),t=l,e()}},error:s=>n.error(s),complete:()=>{if(t.length>0)try{e()}catch(s){console.warn("Incomplete or invalid protocol buffer data at stream end")}n.complete()}});function e(){for(;t.length>=4;){let u=4+new DataView(t.buffer,t.byteOffset,4).getUint32(0,!1);if(t.length<u)break;try{let r=t.slice(4,u),i=J.decode(r);n.next(i),t=t.slice(u)}catch(r){let i=r instanceof Error?r.message:String(r);n.error(new Error(`Failed to decode protocol buffer message: ${i}`));return}}}return n.asObservable()};import*as z from"@ag-ui/proto";var U=g=>{let n=new ce,t=new ue,e=!1;return g.subscribe({next:s=>{t.next(s),s.type==="headers"&&!e?(e=!0,s.headers.get("content-type")===z.AGUI_MEDIA_TYPE?P(t).subscribe({next:u=>n.next(u),error:u=>n.error(u),complete:()=>n.complete()}):G(t).subscribe({next:u=>{try{let r=le.parse(u);n.next(r)}catch(r){n.error(r)}},error:u=>n.error(u),complete:()=>n.complete()})):e||n.error(new Error("No headers event received before data events"))},error:s=>{t.error(s),n.error(s)},complete:()=>{t.complete()}}),n.asObservable()};import{mergeMap as _e}from"rxjs/operators";import{applyPatch as Ce}from"fast-json-patch";import{EventType as v}from"@ag-ui/core";import{z as o}from"zod";var A=o.enum(["TextMessageStart","TextMessageContent","TextMessageEnd","ActionExecutionStart","ActionExecutionArgs","ActionExecutionEnd","ActionExecutionResult","AgentStateMessage","MetaEvent","RunStarted","RunFinished","RunError","NodeStarted","NodeFinished"]),Ee=o.enum(["LangGraphInterruptEvent","PredictState","Exit"]),ge=o.object({type:o.literal(A.enum.TextMessageStart),messageId:o.string(),parentMessageId:o.string().optional()}),pe=o.object({type:o.literal(A.enum.TextMessageContent),messageId:o.string(),content:o.string()}),Te=o.object({type:o.literal(A.enum.TextMessageEnd),messageId:o.string()}),de=o.object({type:o.literal(A.enum.ActionExecutionStart),actionExecutionId:o.string(),actionName:o.string(),parentMessageId:o.string().optional()}),fe=o.object({type:o.literal(A.enum.ActionExecutionArgs),actionExecutionId:o.string(),args:o.string()}),Se=o.object({type:o.literal(A.enum.ActionExecutionEnd),actionExecutionId:o.string()}),me=o.object({type:o.literal(A.enum.ActionExecutionResult),actionName:o.string(),actionExecutionId:o.string(),result:o.string()}),Ae=o.object({type:o.literal(A.enum.AgentStateMessage),threadId:o.string(),agentName:o.string(),nodeName:o.string(),runId:o.string(),active:o.boolean(),role:o.string(),state:o.string(),running:o.boolean()}),ye=o.object({type:o.literal(A.enum.MetaEvent),name:Ee,value:o.any()}),Ut=o.discriminatedUnion("type",[ge,pe,Te,de,fe,Se,me,Ae,ye]),Xt=o.object({id:o.string(),role:o.string(),content:o.string(),parentMessageId:o.string().optional()}),kt=o.object({id:o.string(),name:o.string(),arguments:o.any(),parentMessageId:o.string().optional()}),Kt=o.object({id:o.string(),result:o.any(),actionExecutionId:o.string(),actionName:o.string()});import ve from"untruncate-json";var X=(g,n,t)=>e=>{let s={},l=!0,u=!0,r="",i=null,C=null,c=[],p={},E=T=>{typeof T=="object"&&T!==null&&("messages"in T&&delete T.messages,s=T)};return e.pipe(_e(T=>{switch(T.type){case v.TEXT_MESSAGE_START:{let a=T;return[{type:A.enum.TextMessageStart,messageId:a.messageId}]}case v.TEXT_MESSAGE_CONTENT:{let a=T;return[{type:A.enum.TextMessageContent,messageId:a.messageId,content:a.delta}]}case v.TEXT_MESSAGE_END:{let a=T;return[{type:A.enum.TextMessageEnd,messageId:a.messageId}]}case v.TOOL_CALL_START:{let a=T;return c.push({id:a.toolCallId,type:"function",function:{name:a.toolCallName,arguments:""}}),u=!0,p[a.toolCallId]=a.toolCallName,[{type:A.enum.ActionExecutionStart,actionExecutionId:a.toolCallId,actionName:a.toolCallName,parentMessageId:a.parentMessageId}]}case v.TOOL_CALL_ARGS:{let a=T,N=c[c.length-1];N.function.arguments+=a.delta;let w=!1;if(C){let h=C.find(x=>x.tool==N.function.name);if(h)try{let x=JSON.parse(ve(N.function.arguments));h.tool_argument&&h.tool_argument in x?(E(M(R({},s),{[h.state_key]:x[h.tool_argument]})),w=!0):h.tool_argument||(E(M(R({},s),{[h.state_key]:x})),w=!0)}catch(x){}}return[{type:A.enum.ActionExecutionArgs,actionExecutionId:a.toolCallId,args:a.delta},...w?[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(s),active:u}]:[]]}case v.TOOL_CALL_END:{let a=T;return[{type:A.enum.ActionExecutionEnd,actionExecutionId:a.toolCallId}]}case v.TOOL_CALL_RESULT:{let a=T;return[{type:A.enum.ActionExecutionResult,actionExecutionId:a.toolCallId,result:a.content,actionName:p[a.toolCallId]||"unknown"}]}case v.RAW:return[];case v.CUSTOM:{let a=T;switch(a.name){case"Exit":l=!1;break;case"PredictState":C=a.value;break}return[{type:A.enum.MetaEvent,name:a.name,value:a.value}]}case v.STATE_SNAPSHOT:return E(T.snapshot),[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(s),active:u}];case v.STATE_DELTA:{let N=Ce(s,T.delta,!0,!1);return N?(E(N.newDocument),[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(s),active:u}]):[]}case v.MESSAGES_SNAPSHOT:return i=T.messages,[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(R(R({},s),i?{messages:i}:{})),active:!0}];case v.RUN_STARTED:return[];case v.RUN_FINISHED:return i&&(s.messages=i),[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(R(R({},s),i?{messages:Ne(i)}:{})),active:!1}];case v.RUN_ERROR:return console.error("Run error",T),[];case v.STEP_STARTED:return r=T.stepName,c=[],C=null,[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(s),active:!0}];case v.STEP_FINISHED:return c=[],C=null,[{type:A.enum.AgentStateMessage,threadId:g,agentName:t,nodeName:r,runId:n,running:l,role:"assistant",state:JSON.stringify(s),active:!1}];default:return[]}}))};function Ne(g){var t;let n=[];for(let e of g)if(e.role==="assistant"||e.role==="user"||e.role==="system"){if(e.content){let s={id:e.id,role:e.role,content:e.content};n.push(s)}if(e.role==="assistant"&&e.toolCalls&&e.toolCalls.length>0)for(let s of e.toolCalls){let l={id:s.id,name:s.function.name,arguments:JSON.parse(s.function.arguments),parentMessageId:e.id};n.push(l)}}else if(e.role==="tool"){let s="unknown";for(let u of g)if(u.role==="assistant"&&((t=u.toolCalls)!=null&&t.length)){for(let r of u.toolCalls)if(r.id===e.toolCallId){s=r.function.name;break}}let l={id:e.id,result:e.content,actionExecutionId:e.toolCallId,actionName:s};n.push(l)}return n}import{v4 as b}from"uuid";import{catchError as Me,map as he,tap as xe}from"rxjs/operators";import{finalize as Ie}from"rxjs/operators";import{throwError as Oe,pipe as be}from"rxjs";import{lastValueFrom as we,of as He}from"rxjs";import{mergeMap as Le,finalize as Re}from"rxjs";import{EventType as d}from"@ag-ui/core";var k=g=>n=>{let t,e,s,l=()=>{if(!t||s!=="text")throw new Error("No text message to close");let i={type:d.TEXT_MESSAGE_END,messageId:t.messageId};return s=void 0,t=void 0,g&&console.debug("[TRANSFORM]: TEXT_MESSAGE_END",JSON.stringify(i)),i},u=()=>{if(!e||s!=="tool")throw new Error("No tool call to close");let i={type:d.TOOL_CALL_END,toolCallId:e.toolCallId};return s=void 0,e=void 0,g&&console.debug("[TRANSFORM]: TOOL_CALL_END",JSON.stringify(i)),i},r=()=>s==="text"?[l()]:s==="tool"?[u()]:[];return n.pipe(Le(i=>{switch(i.type){case d.TEXT_MESSAGE_START:case d.TEXT_MESSAGE_CONTENT:case d.TEXT_MESSAGE_END:case d.TOOL_CALL_START:case d.TOOL_CALL_ARGS:case d.TOOL_CALL_END:case d.TOOL_CALL_RESULT:case d.STATE_SNAPSHOT:case d.STATE_DELTA:case d.MESSAGES_SNAPSHOT:case d.CUSTOM:case d.RUN_STARTED:case d.RUN_FINISHED:case d.RUN_ERROR:case d.STEP_STARTED:case d.STEP_FINISHED:case d.THINKING_START:case d.THINKING_END:case d.THINKING_TEXT_MESSAGE_START:case d.THINKING_TEXT_MESSAGE_CONTENT:case d.THINKING_TEXT_MESSAGE_END:return[...r(),i];case d.RAW:return[i];case d.TEXT_MESSAGE_CHUNK:let c=i,p=[];if((s!=="text"||c.messageId!==void 0&&c.messageId!==(t==null?void 0:t.messageId))&&p.push(...r()),s!=="text"){if(c.messageId===void 0)throw new Error("First TEXT_MESSAGE_CHUNK must have a messageId");t={messageId:c.messageId},s="text";let a={type:d.TEXT_MESSAGE_START,messageId:c.messageId,role:"assistant"};p.push(a),g&&console.debug("[TRANSFORM]: TEXT_MESSAGE_START",JSON.stringify(a))}if(c.delta!==void 0){let a={type:d.TEXT_MESSAGE_CONTENT,messageId:t.messageId,delta:c.delta};p.push(a),g&&console.debug("[TRANSFORM]: TEXT_MESSAGE_CONTENT",JSON.stringify(a))}return p;case d.TOOL_CALL_CHUNK:let E=i,T=[];if((s!=="tool"||E.toolCallId!==void 0&&E.toolCallId!==(e==null?void 0:e.toolCallId))&&T.push(...r()),s!=="tool"){if(E.toolCallId===void 0)throw new Error("First TOOL_CALL_CHUNK must have a toolCallId");if(E.toolCallName===void 0)throw new Error("First TOOL_CALL_CHUNK must have a toolCallName");e={toolCallId:E.toolCallId,toolCallName:E.toolCallName,parentMessageId:E.parentMessageId},s="tool";let a={type:d.TOOL_CALL_START,toolCallId:E.toolCallId,toolCallName:E.toolCallName,parentMessageId:E.parentMessageId};T.push(a),g&&console.debug("[TRANSFORM]: TOOL_CALL_START",JSON.stringify(a))}if(E.delta!==void 0){let a={type:d.TOOL_CALL_ARGS,toolCallId:e.toolCallId,delta:E.delta};T.push(a),g&&console.debug("[TRANSFORM]: TOOL_CALL_ARGS",JSON.stringify(a))}return T}let C=i.type}),Re(()=>r()))};var I=class{constructor({agentId:n,description:t,threadId:e,initialMessages:s,initialState:l,debug:u}={}){this.debug=!1;this.agentId=n,this.description=t!=null?t:"",this.threadId=e!=null?e:b(),this.messages=L(s!=null?s:[]),this.state=L(l!=null?l:{}),this.debug=u!=null?u:!1}async runAgent(n){var s;this.agentId=(s=this.agentId)!=null?s:b();let t=this.prepareRunAgentInput(n),e=be(()=>this.run(t),k(this.debug),O(this.debug),l=>this.apply(t,l),l=>this.processApplyEvents(t,l),Me(l=>(this.onError(l),Oe(()=>l))),Ie(()=>{this.onFinalize()}));return we(e(He(null))).then(()=>{})}abortRun(){}apply(...n){return H(...n)}processApplyEvents(n,t){return t.pipe(xe(e=>{e.messages&&(this.messages=e.messages),e.state&&(this.state=e.state)}))}prepareRunAgentInput(n){var t,e,s;return{threadId:this.threadId,runId:(n==null?void 0:n.runId)||b(),tools:L((t=n==null?void 0:n.tools)!=null?t:[]),context:L((e=n==null?void 0:n.context)!=null?e:[]),forwardedProps:L((s=n==null?void 0:n.forwardedProps)!=null?s:{}),state:L(this.state),messages:L(this.messages)}}onError(n){console.error("Agent execution failed:",n)}onFinalize(){}clone(){let n=Object.create(Object.getPrototypeOf(this));for(let t of Object.getOwnPropertyNames(this)){let e=this[t];typeof e!="function"&&(n[t]=L(e))}return n}legacy_to_be_removed_runAgentBridged(n){var e;this.agentId=(e=this.agentId)!=null?e:b();let t=this.prepareRunAgentInput(n);return this.run(t).pipe(k(this.debug),O(this.debug),X(this.threadId,t.runId,this.agentId),s=>s.pipe(he(l=>(this.debug&&console.debug("[LEGACY]:",JSON.stringify(l)),l))))}};var K=class extends I{constructor(t){var e;super(t);this.abortController=new AbortController;this.url=t.url,this.headers=L((e=t.headers)!=null?e:{})}requestInit(t){return{method:"POST",headers:M(R({},this.headers),{"Content-Type":"application/json",Accept:"text/event-stream"}),body:JSON.stringify(t),signal:this.abortController.signal}}runAgent(t){var e;return this.abortController=(e=t==null?void 0:t.abortController)!=null?e:new AbortController,super.runAgent(t)}abortRun(){this.abortController.abort(),super.abortRun()}run(t){let e=D(this.url,this.requestInit(t));return U(e)}};export*from"@ag-ui/core";export{I as AbstractAgent,K as HttpAgent,X as convertToLegacyEvents,H as defaultApplyEvents,P as parseProtoStream,G as parseSSEStream,D as runHttpRequest,U as transformHttpEventStream,O as verifyEvents};
|
|
3
|
+
Patch operations: ${JSON.stringify(a,null,2)}
|
|
4
|
+
Error: ${S}`)}}return l()}case y.MESSAGES_SNAPSHOT:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onMessagesSnapshotEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});if(r(u),u.stopPropagation!==!0){let{messages:a}=c;e=a,r({messages:e})}return l()}case y.RAW:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onRawEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.CUSTOM:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onCustomEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.RUN_STARTED:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onRunStartedEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.RUN_FINISHED:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onRunFinishedEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p,result:c.result})});return r(u),l()}case y.RUN_ERROR:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onRunErrorEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.STEP_STARTED:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onStepStartedEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.STEP_FINISHED:{let u=await C(n,e,s,(a,E,S)=>{var g;return(g=a.onStepFinishedEvent)==null?void 0:g.call(a,{event:c,messages:E,state:S,agent:t,input:p})});return r(u),l()}case y.TEXT_MESSAGE_CHUNK:throw new Error("TEXT_MESSAGE_CHUNK must be tranformed before being applied");case y.TOOL_CALL_CHUNK:throw new Error("TOOL_CALL_CHUNK must be tranformed before being applied");case y.THINKING_START:return l();case y.THINKING_END:return l();case y.THINKING_TEXT_MESSAGE_START:return l();case y.THINKING_TEXT_MESSAGE_CONTENT:return l();case y.THINKING_TEXT_MESSAGE_END:return l()}let _=c.type;return l()}),oe(),n.length>0?re({}):c=>c)};import{EventType as A,AGUIError as v}from"@ag-ui/core";import{throwError as h,of as x}from"rxjs";import{mergeMap as ue}from"rxjs/operators";var X=p=>o=>{let t,n,e=!1,s=!1,i=!1,r=new Map,l=!1,c=!1;return o.pipe(ue(d=>{let _=d.type;if(p&&console.debug("[VERIFY]:",JSON.stringify(d)),s)return h(()=>new v(`Cannot send event type '${_}': The run has already errored with 'RUN_ERROR'. No further events can be sent.`));if(e&&_!==A.RUN_ERROR)return h(()=>new v(`Cannot send event type '${_}': The run has already finished with 'RUN_FINISHED'. Start a new run with 'RUN_STARTED'.`));if(t!==void 0&&![A.TEXT_MESSAGE_CONTENT,A.TEXT_MESSAGE_END,A.RAW].includes(_))return h(()=>new v(`Cannot send event type '${_}' after 'TEXT_MESSAGE_START': Send 'TEXT_MESSAGE_END' first.`));if(n!==void 0&&![A.TOOL_CALL_ARGS,A.TOOL_CALL_END,A.RAW].includes(_))return _===A.TOOL_CALL_START?h(()=>new v("Cannot send 'TOOL_CALL_START' event: A tool call is already in progress. Complete it with 'TOOL_CALL_END' first.")):h(()=>new v(`Cannot send event type '${_}' after 'TOOL_CALL_START': Send 'TOOL_CALL_END' first.`));if(i){if(_===A.RUN_STARTED)return h(()=>new v("Cannot send multiple 'RUN_STARTED' events: A 'RUN_STARTED' event was already sent. Each run must have exactly one 'RUN_STARTED' event at the beginning."))}else if(i=!0,_!==A.RUN_STARTED&&_!==A.RUN_ERROR)return h(()=>new v("First event must be 'RUN_STARTED'"));switch(_){case A.TEXT_MESSAGE_START:return t!==void 0?h(()=>new v("Cannot send 'TEXT_MESSAGE_START' event: A text message is already in progress. Complete it with 'TEXT_MESSAGE_END' first.")):(t=d.messageId,x(d));case A.TEXT_MESSAGE_CONTENT:return t===void 0?h(()=>new v("Cannot send 'TEXT_MESSAGE_CONTENT' event: No active text message found. Start a text message with 'TEXT_MESSAGE_START' first.")):d.messageId!==t?h(()=>new v(`Cannot send 'TEXT_MESSAGE_CONTENT' event: Message ID mismatch. The ID '${d.messageId}' doesn't match the active message ID '${t}'.`)):x(d);case A.TEXT_MESSAGE_END:return t===void 0?h(()=>new v("Cannot send 'TEXT_MESSAGE_END' event: No active text message found. A 'TEXT_MESSAGE_START' event must be sent first.")):d.messageId!==t?h(()=>new v(`Cannot send 'TEXT_MESSAGE_END' event: Message ID mismatch. The ID '${d.messageId}' doesn't match the active message ID '${t}'.`)):(t=void 0,x(d));case A.TOOL_CALL_START:return n!==void 0?h(()=>new v("Cannot send 'TOOL_CALL_START' event: A tool call is already in progress. Complete it with 'TOOL_CALL_END' first.")):(n=d.toolCallId,x(d));case A.TOOL_CALL_ARGS:return n===void 0?h(()=>new v("Cannot send 'TOOL_CALL_ARGS' event: No active tool call found. Start a tool call with 'TOOL_CALL_START' first.")):d.toolCallId!==n?h(()=>new v(`Cannot send 'TOOL_CALL_ARGS' event: Tool call ID mismatch. The ID '${d.toolCallId}' doesn't match the active tool call ID '${n}'.`)):x(d);case A.TOOL_CALL_END:return n===void 0?h(()=>new v("Cannot send 'TOOL_CALL_END' event: No active tool call found. A 'TOOL_CALL_START' event must be sent first.")):d.toolCallId!==n?h(()=>new v(`Cannot send 'TOOL_CALL_END' event: Tool call ID mismatch. The ID '${d.toolCallId}' doesn't match the active tool call ID '${n}'.`)):(n=void 0,x(d));case A.STEP_STARTED:{let T=d.stepName;return r.has(T)?h(()=>new v(`Step "${T}" is already active for 'STEP_STARTED'`)):(r.set(T,!0),x(d))}case A.STEP_FINISHED:{let T=d.stepName;return r.has(T)?(r.delete(T),x(d)):h(()=>new v(`Cannot send 'STEP_FINISHED' for step "${T}" that was not started`))}case A.RUN_STARTED:return x(d);case A.RUN_FINISHED:{if(r.size>0){let T=Array.from(r.keys()).join(", ");return h(()=>new v(`Cannot send 'RUN_FINISHED' while steps are still active: ${T}`))}return e=!0,x(d)}case A.RUN_ERROR:return s=!0,x(d);case A.CUSTOM:return x(d);case A.THINKING_TEXT_MESSAGE_START:return l?c?h(()=>new v("Cannot send 'THINKING_TEXT_MESSAGE_START' event: A thinking message is already in progress. Complete it with 'THINKING_TEXT_MESSAGE_END' first.")):(c=!0,x(d)):h(()=>new v("Cannot send 'THINKING_TEXT_MESSAGE_START' event: A thinking step is not in progress. Create one with 'THINKING_START' first."));case A.THINKING_TEXT_MESSAGE_CONTENT:return c?x(d):h(()=>new v("Cannot send 'THINKING_TEXT_MESSAGE_CONTENT' event: No active thinking message found. Start a message with 'THINKING_TEXT_MESSAGE_START' first."));case A.THINKING_TEXT_MESSAGE_END:return c?(c=!1,x(d)):h(()=>new v("Cannot send 'THINKING_TEXT_MESSAGE_END' event: No active thinking message found. A 'THINKING_TEXT_MESSAGE_START' event must be sent first."));case A.THINKING_START:return l?h(()=>new v("Cannot send 'THINKING_START' event: A thinking step is already in progress. End it with 'THINKING_END' first.")):(l=!0,x(d));case A.THINKING_END:return l?(l=!1,x(d)):h(()=>new v("Cannot send 'THINKING_END' event: No active thinking step found. A 'THINKING_START' event must be sent first."));default:return x(d)}}))};import{EventSchemas as ve}from"@ag-ui/core";import{Subject as he,ReplaySubject as Me}from"rxjs";import{Observable as pe,from as me,defer as Se,throwError as de}from"rxjs";import{switchMap as Te}from"rxjs/operators";var k=(p,o)=>Se(()=>me(fetch(p,o))).pipe(Te(t=>{var s;let n={type:"headers",status:t.status,headers:t.headers},e=(s=t.body)==null?void 0:s.getReader();return e?new pe(i=>(i.next(n),(async()=>{try{for(;;){let{done:r,value:l}=await e.read();if(r)break;let c={type:"data",data:l};i.next(c)}i.complete()}catch(r){i.error(r)}})(),()=>{e.cancel()})):de(()=>new Error("Failed to getReader() from response"))}));import{Subject as fe}from"rxjs";var B=p=>{let o=new fe,t=new TextDecoder("utf-8",{fatal:!1}),n="";p.subscribe({next:s=>{if(s.type!=="headers"&&s.type==="data"&&s.data){let i=t.decode(s.data,{stream:!0});n+=i;let r=n.split(/\n\n/);n=r.pop()||"";for(let l of r)e(l)}},error:s=>o.error(s),complete:()=>{n&&(n+=t.decode(),e(n)),o.complete()}});function e(s){let i=s.split(`
|
|
5
|
+
`),r=[];for(let l of i)l.startsWith("data: ")&&r.push(l.slice(6));if(r.length>0)try{let l=r.join(`
|
|
6
|
+
`),c=JSON.parse(l);o.next(c)}catch(l){o.error(l)}}return o.asObservable()};import{Subject as Ae}from"rxjs";import*as Y from"@ag-ui/proto";var J=p=>{let o=new Ae,t=new Uint8Array(0);p.subscribe({next:e=>{if(e.type!=="headers"&&e.type==="data"&&e.data){let s=new Uint8Array(t.length+e.data.length);s.set(t,0),s.set(e.data,t.length),t=s,n()}},error:e=>o.error(e),complete:()=>{if(t.length>0)try{n()}catch(e){console.warn("Incomplete or invalid protocol buffer data at stream end")}o.complete()}});function n(){for(;t.length>=4;){let i=4+new DataView(t.buffer,t.byteOffset,4).getUint32(0,!1);if(t.length<i)break;try{let r=t.slice(4,i),l=Y.decode(r);o.next(l),t=t.slice(i)}catch(r){let l=r instanceof Error?r.message:String(r);o.error(new Error(`Failed to decode protocol buffer message: ${l}`));return}}}return o.asObservable()};import*as Q from"@ag-ui/proto";var z=p=>{let o=new he,t=new Me,n=!1;return p.subscribe({next:e=>{t.next(e),e.type==="headers"&&!n?(n=!0,e.headers.get("content-type")===Q.AGUI_MEDIA_TYPE?J(t).subscribe({next:i=>o.next(i),error:i=>o.error(i),complete:()=>o.complete()}):B(t).subscribe({next:i=>{try{let r=ve.parse(i);o.next(r)}catch(r){o.error(r)}},error:i=>o.error(i),complete:()=>o.complete()})):n||o.error(new Error("No headers event received before data events"))},error:e=>{t.error(e),o.error(e)},complete:()=>{t.complete()}}),o.asObservable()};import{mergeMap as Pe}from"rxjs/operators";import{applyPatch as De}from"fast-json-patch";import{EventType as L}from"@ag-ui/core";import{z as m}from"zod";var M=m.enum(["TextMessageStart","TextMessageContent","TextMessageEnd","ActionExecutionStart","ActionExecutionArgs","ActionExecutionEnd","ActionExecutionResult","AgentStateMessage","MetaEvent","RunStarted","RunFinished","RunError","NodeStarted","NodeFinished"]),ye=m.enum(["LangGraphInterruptEvent","PredictState","Exit"]),Ce=m.object({type:m.literal(M.enum.TextMessageStart),messageId:m.string(),parentMessageId:m.string().optional()}),_e=m.object({type:m.literal(M.enum.TextMessageContent),messageId:m.string(),content:m.string()}),Re=m.object({type:m.literal(M.enum.TextMessageEnd),messageId:m.string()}),Ne=m.object({type:m.literal(M.enum.ActionExecutionStart),actionExecutionId:m.string(),actionName:m.string(),parentMessageId:m.string().optional()}),xe=m.object({type:m.literal(M.enum.ActionExecutionArgs),actionExecutionId:m.string(),args:m.string()}),Le=m.object({type:m.literal(M.enum.ActionExecutionEnd),actionExecutionId:m.string()}),Ie=m.object({type:m.literal(M.enum.ActionExecutionResult),actionName:m.string(),actionExecutionId:m.string(),result:m.string()}),Oe=m.object({type:m.literal(M.enum.AgentStateMessage),threadId:m.string(),agentName:m.string(),nodeName:m.string(),runId:m.string(),active:m.boolean(),role:m.string(),state:m.string(),running:m.boolean()}),we=m.object({type:m.literal(M.enum.MetaEvent),name:ye,value:m.any()}),cn=m.discriminatedUnion("type",[Ce,_e,Re,Ne,xe,Le,Ie,Oe,we]),un=m.object({id:m.string(),role:m.string(),content:m.string(),parentMessageId:m.string().optional()}),pn=m.object({id:m.string(),name:m.string(),arguments:m.any(),parentMessageId:m.string().optional()}),mn=m.object({id:m.string(),result:m.any(),actionExecutionId:m.string(),actionName:m.string()});import He from"untruncate-json";var j=(p,o,t)=>n=>{let e={},s=!0,i=!0,r="",l=null,c=null,d=[],_={},T=u=>{typeof u=="object"&&u!==null&&("messages"in u&&delete u.messages,e=u)};return n.pipe(Pe(u=>{switch(u.type){case L.TEXT_MESSAGE_START:{let a=u;return[{type:M.enum.TextMessageStart,messageId:a.messageId}]}case L.TEXT_MESSAGE_CONTENT:{let a=u;return[{type:M.enum.TextMessageContent,messageId:a.messageId,content:a.delta}]}case L.TEXT_MESSAGE_END:{let a=u;return[{type:M.enum.TextMessageEnd,messageId:a.messageId}]}case L.TOOL_CALL_START:{let a=u;return d.push({id:a.toolCallId,type:"function",function:{name:a.toolCallName,arguments:""}}),i=!0,_[a.toolCallId]=a.toolCallName,[{type:M.enum.ActionExecutionStart,actionExecutionId:a.toolCallId,actionName:a.toolCallName,parentMessageId:a.parentMessageId}]}case L.TOOL_CALL_ARGS:{let a=u,E=d[d.length-1];E.function.arguments+=a.delta;let S=!1;if(c){let g=c.find(N=>N.tool==E.function.name);if(g)try{let N=JSON.parse(He(E.function.arguments));g.tool_argument&&g.tool_argument in N?(T(G(I({},e),{[g.state_key]:N[g.tool_argument]})),S=!0):g.tool_argument||(T(G(I({},e),{[g.state_key]:N})),S=!0)}catch(N){}}return[{type:M.enum.ActionExecutionArgs,actionExecutionId:a.toolCallId,args:a.delta},...S?[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(e),active:i}]:[]]}case L.TOOL_CALL_END:{let a=u;return[{type:M.enum.ActionExecutionEnd,actionExecutionId:a.toolCallId}]}case L.TOOL_CALL_RESULT:{let a=u;return[{type:M.enum.ActionExecutionResult,actionExecutionId:a.toolCallId,result:a.content,actionName:_[a.toolCallId]||"unknown"}]}case L.RAW:return[];case L.CUSTOM:{let a=u;switch(a.name){case"Exit":s=!1;break;case"PredictState":c=a.value;break}return[{type:M.enum.MetaEvent,name:a.name,value:a.value}]}case L.STATE_SNAPSHOT:return T(u.snapshot),[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(e),active:i}];case L.STATE_DELTA:{let E=De(e,u.delta,!0,!1);return E?(T(E.newDocument),[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(e),active:i}]):[]}case L.MESSAGES_SNAPSHOT:return l=u.messages,[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(I(I({},e),l?{messages:l}:{})),active:!0}];case L.RUN_STARTED:return[];case L.RUN_FINISHED:return l&&(e.messages=l),[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(I(I({},e),l?{messages:Ge(l)}:{})),active:!1}];case L.RUN_ERROR:return console.error("Run error",u),[];case L.STEP_STARTED:return r=u.stepName,d=[],c=null,[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(e),active:!0}];case L.STEP_FINISHED:return d=[],c=null,[{type:M.enum.AgentStateMessage,threadId:p,agentName:t,nodeName:r,runId:o,running:s,role:"assistant",state:JSON.stringify(e),active:!1}];default:return[]}}))};function Ge(p){var t;let o=[];for(let n of p)if(n.role==="assistant"||n.role==="user"||n.role==="system"){if(n.content){let e={id:n.id,role:n.role,content:n.content};o.push(e)}if(n.role==="assistant"&&n.toolCalls&&n.toolCalls.length>0)for(let e of n.toolCalls){let s={id:e.id,name:e.function.name,arguments:JSON.parse(e.function.arguments),parentMessageId:n.id};o.push(s)}}else if(n.role==="tool"){let e="unknown";for(let i of p)if(i.role==="assistant"&&((t=i.toolCalls)!=null&&t.length)){for(let r of i.toolCalls)if(r.id===n.toolCallId){e=r.function.name;break}}let s={id:n.id,result:n.content,actionExecutionId:n.toolCallId,actionName:e};o.push(s)}return o}import{v4 as U}from"uuid";import{catchError as Ue,map as Z,tap as Ke}from"rxjs/operators";import{finalize as ke}from"rxjs/operators";import{pipe as Be,from as Je,of as ze}from"rxjs";import{lastValueFrom as je}from"rxjs";import{mergeMap as Fe,finalize as Xe}from"rxjs";import{EventType as f}from"@ag-ui/core";var $=p=>o=>{let t,n,e,s=()=>{if(!t||e!=="text")throw new Error("No text message to close");let l={type:f.TEXT_MESSAGE_END,messageId:t.messageId};return e=void 0,t=void 0,p&&console.debug("[TRANSFORM]: TEXT_MESSAGE_END",JSON.stringify(l)),l},i=()=>{if(!n||e!=="tool")throw new Error("No tool call to close");let l={type:f.TOOL_CALL_END,toolCallId:n.toolCallId};return e=void 0,n=void 0,p&&console.debug("[TRANSFORM]: TOOL_CALL_END",JSON.stringify(l)),l},r=()=>e==="text"?[s()]:e==="tool"?[i()]:[];return o.pipe(Fe(l=>{switch(l.type){case f.TEXT_MESSAGE_START:case f.TEXT_MESSAGE_CONTENT:case f.TEXT_MESSAGE_END:case f.TOOL_CALL_START:case f.TOOL_CALL_ARGS:case f.TOOL_CALL_END:case f.TOOL_CALL_RESULT:case f.STATE_SNAPSHOT:case f.STATE_DELTA:case f.MESSAGES_SNAPSHOT:case f.CUSTOM:case f.RUN_STARTED:case f.RUN_FINISHED:case f.RUN_ERROR:case f.STEP_STARTED:case f.STEP_FINISHED:case f.THINKING_START:case f.THINKING_END:case f.THINKING_TEXT_MESSAGE_START:case f.THINKING_TEXT_MESSAGE_CONTENT:case f.THINKING_TEXT_MESSAGE_END:return[...r(),l];case f.RAW:return[l];case f.TEXT_MESSAGE_CHUNK:let d=l,_=[];if((e!=="text"||d.messageId!==void 0&&d.messageId!==(t==null?void 0:t.messageId))&&_.push(...r()),e!=="text"){if(d.messageId===void 0)throw new Error("First TEXT_MESSAGE_CHUNK must have a messageId");t={messageId:d.messageId},e="text";let a={type:f.TEXT_MESSAGE_START,messageId:d.messageId,role:"assistant"};_.push(a),p&&console.debug("[TRANSFORM]: TEXT_MESSAGE_START",JSON.stringify(a))}if(d.delta!==void 0){let a={type:f.TEXT_MESSAGE_CONTENT,messageId:t.messageId,delta:d.delta};_.push(a),p&&console.debug("[TRANSFORM]: TEXT_MESSAGE_CONTENT",JSON.stringify(a))}return _;case f.TOOL_CALL_CHUNK:let T=l,u=[];if((e!=="tool"||T.toolCallId!==void 0&&T.toolCallId!==(n==null?void 0:n.toolCallId))&&u.push(...r()),e!=="tool"){if(T.toolCallId===void 0)throw new Error("First TOOL_CALL_CHUNK must have a toolCallId");if(T.toolCallName===void 0)throw new Error("First TOOL_CALL_CHUNK must have a toolCallName");n={toolCallId:T.toolCallId,toolCallName:T.toolCallName,parentMessageId:T.parentMessageId},e="tool";let a={type:f.TOOL_CALL_START,toolCallId:T.toolCallId,toolCallName:T.toolCallName,parentMessageId:T.parentMessageId};u.push(a),p&&console.debug("[TRANSFORM]: TOOL_CALL_START",JSON.stringify(a))}if(T.delta!==void 0){let a={type:f.TOOL_CALL_ARGS,toolCallId:n.toolCallId,delta:T.delta};u.push(a),p&&console.debug("[TRANSFORM]: TOOL_CALL_ARGS",JSON.stringify(a))}return u}let c=l.type}),Xe(()=>r()))};var F=class{constructor({agentId:o,description:t,threadId:n,initialMessages:e,initialState:s,debug:i}={}){this.debug=!1;this.subscribers=[];this.agentId=o,this.description=t!=null?t:"",this.threadId=n!=null?n:U(),this.messages=R(e!=null?e:[]),this.state=R(s!=null?s:{}),this.debug=i!=null?i:!1}subscribe(o){return this.subscribers.push(o),{unsubscribe:()=>{this.subscribers=this.subscribers.filter(t=>t!==o)}}}async runAgent(o,t){var l;this.agentId=(l=this.agentId)!=null?l:U();let n=this.prepareRunAgentInput(o),e,s=new Set(this.messages.map(c=>c.id)),i=[{onRunFinishedEvent:c=>{e=c.result}},...this.subscribers,t!=null?t:{}];await this.onInitialize(n,i);let r=Be(()=>this.run(n),$(this.debug),X(this.debug),c=>this.apply(n,c,i),c=>this.processApplyEvents(n,c,i),Ue(c=>this.onError(n,c,i)),ke(()=>{this.onFinalize(n,i)}));return je(r(ze(null))).then(()=>{let c=R(this.messages).filter(d=>!s.has(d.id));return{result:e,newMessages:c}})}abortRun(){}apply(o,t,n){return K(o,t,this,n)}processApplyEvents(o,t,n){return t.pipe(Ke(e=>{e.messages&&(this.messages=e.messages,n.forEach(s=>{var i;(i=s.onMessagesChanged)==null||i.call(s,{messages:this.messages,state:this.state,agent:this,input:o})})),e.state&&(this.state=e.state,n.forEach(s=>{var i;(i=s.onStateChanged)==null||i.call(s,{state:this.state,messages:this.messages,agent:this,input:o})}))}))}prepareRunAgentInput(o){var t,n,e;return{threadId:this.threadId,runId:(o==null?void 0:o.runId)||U(),tools:R((t=o==null?void 0:o.tools)!=null?t:[]),context:R((n=o==null?void 0:o.context)!=null?n:[]),forwardedProps:R((e=o==null?void 0:o.forwardedProps)!=null?e:{}),state:R(this.state),messages:R(this.messages)}}async onInitialize(o,t){let n=await C(t,this.messages,this.state,(e,s,i)=>{var r;return(r=e.onRunInitialized)==null?void 0:r.call(e,{messages:s,state:i,agent:this,input:o})});(n.messages!==void 0||n.state!==void 0)&&(n.messages&&(this.messages=n.messages,o.messages=n.messages,t.forEach(e=>{var s;(s=e.onMessagesChanged)==null||s.call(e,{messages:this.messages,state:this.state,agent:this,input:o})})),n.state&&(this.state=n.state,o.state=n.state,t.forEach(e=>{var s;(s=e.onStateChanged)==null||s.call(e,{state:this.state,messages:this.messages,agent:this,input:o})})))}onError(o,t,n){return Je(C(n,this.messages,this.state,(e,s,i)=>{var r;return(r=e.onRunFailed)==null?void 0:r.call(e,{error:t,messages:s,state:i,agent:this,input:o})})).pipe(Z(e=>{let s=e;if((s.messages!==void 0||s.state!==void 0)&&(s.messages!==void 0&&(this.messages=s.messages,n.forEach(i=>{var r;(r=i.onMessagesChanged)==null||r.call(i,{messages:this.messages,state:this.state,agent:this,input:o})})),s.state!==void 0&&(this.state=s.state,n.forEach(i=>{var r;(r=i.onStateChanged)==null||r.call(i,{state:this.state,messages:this.messages,agent:this,input:o})}))),s.stopPropagation!==!0)throw console.error("Agent execution failed:",t),t;return{}}))}async onFinalize(o,t){let n=await C(t,this.messages,this.state,(e,s,i)=>{var r;return(r=e.onRunFinalized)==null?void 0:r.call(e,{messages:s,state:i,agent:this,input:o})});(n.messages!==void 0||n.state!==void 0)&&(n.messages!==void 0&&(this.messages=n.messages,t.forEach(e=>{var s;(s=e.onMessagesChanged)==null||s.call(e,{messages:this.messages,state:this.state,agent:this,input:o})})),n.state!==void 0&&(this.state=n.state,t.forEach(e=>{var s;(s=e.onStateChanged)==null||s.call(e,{state:this.state,messages:this.messages,agent:this,input:o})})))}clone(){let o=Object.create(Object.getPrototypeOf(this));for(let t of Object.getOwnPropertyNames(this)){let n=this[t];typeof n!="function"&&(o[t]=R(n))}return o}addMessage(o){this.messages.push(o),(async()=>{var t,n,e;for(let s of this.subscribers)await((t=s.onNewMessage)==null?void 0:t.call(s,{message:o,messages:this.messages,state:this.state,agent:this}));if(o.role==="assistant"&&o.toolCalls)for(let s of o.toolCalls)for(let i of this.subscribers)await((n=i.onNewToolCall)==null?void 0:n.call(i,{toolCall:s,messages:this.messages,state:this.state,agent:this}));for(let s of this.subscribers)await((e=s.onMessagesChanged)==null?void 0:e.call(s,{messages:this.messages,state:this.state,agent:this}))})()}addMessages(o){this.messages.push(...o),(async()=>{var t,n,e;for(let s of o){for(let i of this.subscribers)await((t=i.onNewMessage)==null?void 0:t.call(i,{message:s,messages:this.messages,state:this.state,agent:this}));if(s.role==="assistant"&&s.toolCalls)for(let i of s.toolCalls)for(let r of this.subscribers)await((n=r.onNewToolCall)==null?void 0:n.call(r,{toolCall:i,messages:this.messages,state:this.state,agent:this}))}for(let s of this.subscribers)await((e=s.onMessagesChanged)==null?void 0:e.call(s,{messages:this.messages,state:this.state,agent:this}))})()}setMessages(o){this.messages=R(o),(async()=>{var t;for(let n of this.subscribers)await((t=n.onMessagesChanged)==null?void 0:t.call(n,{messages:this.messages,state:this.state,agent:this}))})()}setState(o){this.state=R(o),(async()=>{var t;for(let n of this.subscribers)await((t=n.onStateChanged)==null?void 0:t.call(n,{messages:this.messages,state:this.state,agent:this}))})()}legacy_to_be_removed_runAgentBridged(o){var n;this.agentId=(n=this.agentId)!=null?n:U();let t=this.prepareRunAgentInput(o);return this.run(t).pipe($(this.debug),X(this.debug),j(this.threadId,t.runId,this.agentId),e=>e.pipe(Z(s=>(this.debug&&console.debug("[LEGACY]:",JSON.stringify(s)),s))))}};var W=class extends F{constructor(t){var n;super(t);this.abortController=new AbortController;this.url=t.url,this.headers=R((n=t.headers)!=null?n:{})}requestInit(t){return{method:"POST",headers:G(I({},this.headers),{"Content-Type":"application/json",Accept:"text/event-stream"}),body:JSON.stringify(t),signal:this.abortController.signal}}runAgent(t){var n;return this.abortController=(n=t==null?void 0:t.abortController)!=null?n:new AbortController,super.runAgent(t)}abortRun(){this.abortController.abort(),super.abortRun()}run(t){let n=k(this.url,this.requestInit(t));return z(n)}};export*from"@ag-ui/core";export{F as AbstractAgent,W as HttpAgent,j as convertToLegacyEvents,K as defaultApplyEvents,J as parseProtoStream,B as parseSSEStream,k as runHttpRequest,z as transformHttpEventStream,X as verifyEvents};
|
|
7
7
|
//# sourceMappingURL=index.mjs.map
|