@onereach/step-create-chat-completion 0.0.7-rc.59 → 0.0.7-rc.61

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.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{createRequire as e}from"node:module";import{install as t}from"source-map-support";import{ACTION as n}from"@onereach/flow-sdk/types/index.js";import r from"@onereach/flow-sdk/errors/timeout.js";import*as i from"zod/mini";import a from"@onereach/flow-sdk/errors/base.js";import o from"@onereach/flow-sdk/step.js";import{setImmediate as s}from"node:timers/promises";import{Memoize as c,clear as l}from"typescript-memoize";import{BasicThreadService as u}from"@onereach/flow-sdk/services/basic.js";import d from"openai";import ee from"timestring";import{readFile as te}from"node:fs/promises";const f={UNKNOWN:`UNKNOWN`,STEP_LOGIC_ISSUE:`STEP_LOGIC_ISSUE`,AUTH:`AUTH`,TIMEOUT:`TIMEOUT`,VALIDATION:`VALIDATION`,INVALID_REQUEST:`INVALID_REQUEST`,RATE_LIMITED:`RATE_LIMITED`,LLM_FINISH_LENGTH:`LLM_FINISH_LENGTH`,SERVER_ERROR:`SERVER_ERROR`,UNSUPPORTED:`UNSUPPORTED`};var p=class extends a{},ne=class extends a{};i.config(i.locales.en());const re=[p,r];function m(e,t={}){let{preserveErrors:n=re,unknownMessage:a=`Unknown error`,unknownCode:o=f.UNKNOWN}=t;if(n?.some(t=>e instanceof t))return e;if(e instanceof i.core.$ZodError)return new p(i.prettifyError(e),e,{code:f.VALIDATION});if(typeof e==`string`)return new p(e,{code:f.UNKNOWN});if(e instanceof Error){if(e.message.includes(`Request timed out`))return new r(`Timeout`,e,{code:f.TIMEOUT});let t=e?.response?.data?.error?.message;return t==null?new p(a,e,{code:o}):new p(t,e,{code:f.SERVER_ERROR})}return new p(a,{code:o,data:e})}function h(e,t){function n(t,n,r,i){let a=e(t,n,r,i);if(typeof a==`function`){let e=i.value,t=a;if(e?.name!=null)try{Object.defineProperty(t,`name`,{value:e.name})}catch{}return i.value=t,i}return a??i}return function(e,r,i){if(typeof e==`object`&&e&&(typeof r==`string`||typeof r==`symbol`)&&typeof i==`object`&&i)return n(t,e,r,i);{let r=e??t;return(e,t,i)=>n(r,e,t,i)}}}const g=h((e,t,n,r)=>{let i=r.value;if(!(!i||typeof i!=`function`))return function(...t){try{let r=i.apply(this,t);return r instanceof Promise?r.catch(t=>_.call(this,e,t,n)):r}catch(t){return _.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function _(e,t,r){let i=r.toString();this.log.DEBUG?.(`Error in method '${i}'`,t);let a=e.useErrorFilter?m(t):t;if(a instanceof Error)if(e.allowHandleError){this.thread.enqueue({name:n.error,error:a});return}else{this.end(a);return}throw a}function ie({thread:e,...t}){let n=b({thread:e,...t}),r=e.process;if(r.cache[n]!=null)throw Error(`AbortController for key '${n}' already exists`);return r.cache[n]=new AbortController}function v({thread:e,...t}){let n=b({thread:e,...t});return e.process.cache[n]}function y({thread:e,...t}){let n=b({thread:e,...t});delete e.process.cache[n]}function b({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function x(e,{id:t,state:r,local:i,...a},{force:o}={}){let s=e.process.getThread(t);if(e.log.DEBUG?.(`runThread: thread with id '${t}'`,a),s==null)return await e.thread.runThread({...a,id:t,state:r,local:{...i,parent:e.thread.id,active:!0}});if(!o&&!s.ended&&s.local.active)throw Error(`Thread with id '${t}' has not ended yet, cannot start it again`);if(r==null)throw Error(`Thread with id '${t}' must have a state to run`);return s.local.active=!0,await s.enqueueAndRun({name:n.goto,state:{...r,active:!0}})}function ae(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function oe(e,t=e.id){let n=[];for(let r in e.process.threads){let i=e.process.threads[r];i?.local.parent===t&&n.push(i)}return n}async function S(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await C(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function C(e,t,n={}){let r=oe(e).map(async n=>await S(e,n.id,{stopChildThreads:!0,result:t})),i=await Promise.allSettled(r);if(n.throw??!0){let e=i.find(e=>e.status===`rejected`);if(e)throw e.reason}return i}const w=h((e,t,n,r)=>{let i=r.value;if(!(!i||typeof i!=`function`))return function(...e){try{let t=i.apply(this,e);return t instanceof Promise?t.catch(e=>{let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),m(e)}):t}catch(e){let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),m(e)}}},{});i.config(i.locales.en());const T=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),E=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:T}),i.object({role:i.literal(`user`),content:T,name:i.optional(T.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(T),tool_calls:i.optional(i.array(i.object({id:T.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:T.check(i.minLength(1)),arguments:T.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:T,name:i.optional(T.check(i.minLength(1),i.maxLength(64))),tool_call_id:T.check(i.minLength(1))})]),se=i.object({type:i.literal(`object`),properties:i.record(i.string().check(i.minLength(1),i.maxLength(64)),i.object({type:i.enum([`string`,`number`,`boolean`,`array`,`object`]),description:i.optional(i.string().check(i.maxLength(1e3))),items:i.optional(i.object({type:i.enum([`string`,`number`,`boolean`,`object`]),description:i.optional(i.string().check(i.maxLength(1e3)))}))}))}),ce=i.object({provider:T.check(i.minLength(1)),model:T.check(i.minLength(1)),accountKey:D(T),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(T),userMessage:i.optional(T),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(T),temperature:D(i.coerce.number().check(i.gte(0),i.lte(2))),topP:D(i.coerce.number().check(i.gte(0),i.lte(1))),maxTokens:D(i.coerce.number().check(i.int(),i.gte(1))),presencePenalty:D(i.coerce.number().check(i.gte(-2),i.lte(2))),frequencyPenalty:D(i.coerce.number().check(i.gte(-2),i.lte(2))),n:D(i.coerce.number().check(i.int(),i.gte(1),i.lte(128))),stop:i.optional(i.array(i.object({token:i.string().check(i.minLength(1),i.maxLength(64))})).check(i.maxLength(4))),reasoningEffort:D(i.enum([`none`,`minimal`,`low`,`medium`,`high`,`xhigh`])),streamResponse:i._default(i.boolean(),!1),streamSettings:i.optional(i.object({maxBufferLength:i.coerce.number().check(i.overwrite(e=>e===0?50:e),i.int(),i.gte(1),i.lte(32768)),debounceDuration:i.coerce.number().check(i.int(),i.gte(0),i.lte(1e4))})),customHistory:i._default(i.boolean(),!1),historyLength:D(i.coerce.number().check(i.int(),i.gte(1),i.lte(100))),historyMode:i.optional(i.enum([`mergefield`,`codeHistory`])),historyMergefield:i.optional(i.array(E)),historyCode:i.optional(i.array(E)),functionsList:i.array(i.object({name:T.check(i.minLength(1),i.maxLength(64)),description:i.optional(T.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(se,i.transform(e=>(e.required=Object.keys(e.properties),e.additionalProperties=!1,e)))),strict:i.boolean()})),useState:i.optional(i.boolean()),stateSettings:i.optional(i.object({stateFunction:i.function()})),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:T.check(i.trim(),i.minLength(1))}).check(i.refine(e=>!e.streamResponse||e.provider===`openai`,{error:`Streaming is only supported for OpenAI provider`}),i.refine(e=>!e.streamResponse||e.streamSettings!==void 0,{error:`Missing "Stream settings" for response streaming mode`}),i.refine(e=>e.typeOfStep!==`chat`||e.customHistory||!e.switchAssistantsMessage||e.assistantsFirstMessage!==void 0,{error:`Missing "Assistant's first message" value`}),i.refine(e=>e.customHistory||e.userMessage!==void 0,{error:`Missing "User message" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==void 0,{error:`Missing "History mode" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield!==void 0,{error:`Missing "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode!==void 0,{error:`Missing "History" data as code`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via code`}),i.refine(e=>!e.useState||e.stateSettings!=null,{error:`Missing state settings`}));function D(e){return i.optional(i.pipe(i.union([i.undefined(),i.literal(``),e]),i.transform(e=>e===``?void 0:e)))}function O(e,t){if(typeof Reflect==`object`&&typeof Reflect.metadata==`function`)return Reflect.metadata(e,t)}function k(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var A=class extends o{async resolveDataIn(){let e=await super.resolveDataIn();return ce.parse(e)}};k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],A.prototype,`resolveDataIn`,null);const j=`llm_cmp`,M={cancel:`st_cancel`},le={cancel:`cnl_cancel`};var N=class e extends A{static class=`cncl_ctrl`;static async start(e){await x(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await S(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${j}_${this.class}_${e}`}runStep(){this.triggers.local({name:le.cancel},this.onCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onCancel(){this.log.DEBUG?.(`Canceling LLM request`);let e=this.local.parent,t=this.process.getThread(e);if(!t||t.ended)return this.end();let n=v({thread:this.thread});n?n.abort(new ne(`Request cancelled`)):this.process.enqueueAndRun({name:M.cancel,thread:e})}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],N.prototype,`runStep`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],N.prototype,`initialize`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],N.prototype,`onCancel`,null);const P={appendToHistory:`hst_append`,prepareHistory:`hst_prepare`,adjustOnCancel:`hst_cncl`};var F=class e extends A{static class=`hst_ctrl`;static async start(e){let t=this.getThreadId(e.step.id);e.process.getThread(t)??await e.process.runThread({id:t,background:!0,state:{step:e.step.id,class:this.class,messages:[]}})}static async prepareHistory(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:P.prepareHistory})}static async appendToHistory(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:P.appendToHistory,params:{message:t}})}static async adjustOnCancel(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:P.adjustOnCancel})}static getHistory(e){return e.process.getSafeThread(this.getThreadId(e.step.id)).state.messages}static getThreadId(e){return`${j}_${this.class}_${e}`}runStep(){this.triggers.local(P.prepareHistory,this.onPrepareHistory).local(P.appendToHistory,this.onAppendToHistory).local(P.adjustOnCancel,this.onAdjustOnCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onAdjustOnCancel(){let e;for(;(e=this.state.messages.at(-1))&&!([`system`,`user`].includes(e.role)||e.role===`assistant`&&e.tool_calls==null);){let e=this.state.messages.pop();this.log.DEBUG?.(`Removed message from history`,e)}}onPrepareHistory(){this.log.DEBUG?.(`Preparing history`);let{customHistory:e,historyLength:t,systemMessage:n,typeOfStep:r,userMessage:i,historyMode:a,historyMergefield:o,historyCode:s,assistantsFirstMessage:c}=this.data;if(r===`completion`){this.state.messages=[{role:`user`,content:i}];return}if(e){this.state.messages=a===`mergefield`?o:s;return}let l=this.state.messages;if(l.length===0&&c!=null&&c.length>0&&l.push({role:`assistant`,content:c}),l.at(-1)?.role!==`tool`&&l.push({role:`user`,content:i}),!l.some(e=>e.role===`tool`)&&t!=null&&t>0&&(l=l.slice(-1*t)),n!=null&&n.length>0){let e={role:`system`,content:n};l[0]?.role===`system`?l[0]=e:l.unshift(e)}this.state.messages=l}onAppendToHistory(e){this.log.DEBUG?.(`Appending message to history`,e.params);let{message:t}=e.params,n=Array.isArray(t)?t:[t];this.state.messages.push(...n)}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],F.prototype,`runStep`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],F.prototype,`initialize`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],F.prototype,`onAdjustOnCancel`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],F.prototype,`onPrepareHistory`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,void 0)],F.prototype,`onAppendToHistory`,null);const I={handleContent:`hnd_cnt`};var L=class extends u{async handleContentByParentThread(e){await s(),await this.thread.process.enqueueAndRun({thread:this.thread.local.parent,name:I.handleContent,params:{content:e}})}},ue=class extends L{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),y({thread:this.thread})}},de=class extends L{async handle(e){this.log.DEBUG?.(`Start processing stream of chunks from LLM`);for await(let t of e)await this.handleContentByParentThread(t);y({thread:this.thread})}};function fe({thread:e,isStreaming:t}){return t?new de(e):new ue(e)}var pe=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var R=class extends pe{async makeOpenAiRequest(e,t){let{model:n,temperature:r,topP:i,maxTokens:a,frequencyPenalty:o,presencePenalty:s,n:c,streamResponse:l,reasoningEffort:u}=this.data,d={messages:structuredClone(e),model:n,temperature:r,top_p:i,max_tokens:a,frequency_penalty:o,presence_penalty:s,stop:this.stopSequences,n:c,reasoning_effort:u,stream:l,tools:this.tools};this.log.DEBUG?.(`LLM request`,d);try{let e=await this.openAiClient.chat.completions.create(d,{signal:t});return l||this.log.DEBUG?.(`LLM response`,e),e}catch(e){throw e instanceof Error?new p(`Chat completion request failed`,e,{code:f.SERVER_ERROR,data:d}):e}}get tools(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0))return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:!e.parameters||Object.keys(e.parameters).length===0?void 0:e.parameters,strict:e.strict}}))}get openAiClient(){let{provider:e,accountKey:t}=this.data,n={baseURL:`${this.config.env.OPENAI_API_URL}/${e}`,apiKey:`_fake_key_`,defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get([`reporting`,`sessionId`]),...t?{"x-or-account-token-name":t}:void 0},timeout:this.timeoutMs};return this.log.DEBUG?.(`OpenAI client options`,n),new d(n)}get stopSequences(){return(this.data.stop??[]).map(e=>e.token).filter(e=>e.length>0)}get timeoutMs(){let e=ee(this.data.timeoutDuration,`ms`);return i.number().check(i.int(),i.gte(1e3),i.lte(6e5)).parse(e)}};k([c(),O(`design:type`,Number),O(`design:paramtypes`,[])],R.prototype,`timeoutMs`,null);var z;const B=[`requestInit`];var V=class extends A{static class=`rqst_ctx`;static async start(e){return await x(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0})}static getThreadId(e){return`${j}_${this.class}_${e}`}async runStep(){l(B);let e=ie({thread:this.thread});try{let t=await this.openAiService.makeOpenAiRequest(await this.getHistory(),e.signal);await this.responseHandler.handle(t),this.complete()}catch(t){if(e?.signal.aborted)return this.cancel();throw t}finally{y({thread:this.thread})}}cancel(){this.end({status:`cancel`})}complete(){this.end({status:`ok`})}async getHistory(){let e=F.getHistory(this.thread);if(!this.data.useState)return e;let t=[...e],n={role:`assistant`,content:await this.data.stateSettings?.stateFunction()};return e.at(-1)?.role===`user`?t.splice(-1,0,n):t.push(n),t}get openAiService(){return new R(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return fe({thread:this.thread,isStreaming:e})}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],V.prototype,`runStep`,null),k([c(),O(`design:type`,typeof(z=R!==void 0&&R)==`function`?z:Object),O(`design:paramtypes`,[])],V.prototype,`openAiService`,null),k([c({tags:B}),O(`design:type`,Object),O(`design:paramtypes`,[])],V.prototype,`responseHandler`,null);const H={callTool:`tl_run_call`,lastToolCallReceived:`tl_rsp_end`};var U=class extends A{static class=`tl_wrk`;static async start(e,t){let n=JSON.parse(t.function?.arguments);await x(e,{id:this.getThreadId(t),state:{step:e.step.id,class:this.class},local:{toolCall:t,parsedArguments:n,parent:e.id,active:!0}})}static getThreadId(e){return`${j}_${this.class}_${e.id}`}runStep(){let{toolCall:e,parsedArguments:t}=this.local;this.log.DEBUG?.(`Initializing tool worker thread`,e),this.exitStep(this.getExitName(),{function:{name:e.function.name,arguments:t}})}getExitName(){return`function`}};k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],U.prototype,`runStep`,null);var W=class e extends A{static class=`tl_ctrl`;static async start(e){await x(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,tasks:{},expectMoreTasks:!0},background:!0})}static async callTool(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:H.callTool,params:{toolCall:t}})}static async lastToolCallReceived(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:H.lastToolCallReceived})}static getThreadId(e){return`${j}_${this.class}_${e}`}runStep(){this.triggers.local(H.callTool,this.onCallTool).local(H.lastToolCallReceived,this.onLastToolCallReceived).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onCallTool(e){let{toolCall:t}=e.params;if(t.type!==`function`)throw new p(`Only function tools are supported at the moment`,{code:f.UNSUPPORTED});this.state.tasks[t.id]={call:t,status:`pending`},this.startToolWorkerThread(t)}onLastToolCallReceived(){this.log.DEBUG?.(`No more tool calls expected`,this.state.tasks),this.state.expectMoreTasks=!1,this.tryEndToolsContextThread()}onWorkerThreadEnd(e){this.log.DEBUG?.(`Tool caller worker thread has ended`,e);let{result:t}=e.params.thread;if(t==null)throw new p(`${U.name} thread ended unexpectedly`,{code:f.STEP_LOGIC_ISSUE});if(t instanceof Error)throw m(t,{unknownMessage:`${U.name} thread ended with an error`,unknownCode:f.STEP_LOGIC_ISSUE});t.status!==`cancel`&&(this.log.DEBUG?.(`Tool worker thread completed successfully`,{toolCallId:t.toolCallId,result:t.data}),this.setToolCallResult(t.toolCallId,t.data),this.tryEndToolsContextThread())}startToolWorkerThread(e){this.triggers.hook({name:n.end,thread:U.getThreadId(e)},this.onWorkerThreadEnd),U.start(this.thread,e)}setToolCallResult(e,t){let n=this.state.tasks[e];if(n==null)throw new p(`Unknown tool call with id '${e}'`,{code:f.STEP_LOGIC_ISSUE});n.status=`completed`,n.result=t}tryEndToolsContextThread(){this.state.expectMoreTasks||Object.values(this.state.tasks).some(e=>e.status===`pending`)||(this.log.DEBUG?.(`All tool calls completed or failed`,this.state.tasks),this.end({status:`ok`,messages:this.messages}))}get messages(){return Object.values(this.state.tasks).map(e=>({role:`tool`,tool_call_id:e.call.id,content:JSON.stringify(e.result)}))}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],W.prototype,`runStep`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],W.prototype,`initialize`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,void 0)],W.prototype,`onCallTool`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],W.prototype,`onLastToolCallReceived`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,void 0)],W.prototype,`onWorkerThreadEnd`,null);const G=[`toolsInit`];var K=class extends A{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(G),this.triggers.local(I.handleContent,this.onHandleContent).hook({name:n.end,thread:V.getThreadId(this.thread.step.id)},this.onRequestContextEnd).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`),V.start(this.thread)}async onRequestContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${V.name} thread ended with result`,t),t==null)throw new p(`${V.name} thread ended unexpectedly`,{code:f.STEP_LOGIC_ISSUE});if(t instanceof Error)throw m(t,{unknownMessage:`${V.name} thread ended with an error`,unknownCode:f.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return this.cancel();if(t.status!==`ok`)throw new p(`Unexpected end of the ${V.name} thread`,{code:f.STEP_LOGIC_ISSUE,data:e.params.result});this.state.requestComplete=!0,await this.onAllContentReceived(),this.state.toolsReady!=null&&(await s(),await W.lastToolCallReceived(this.thread)),await this.tryToComplete()}async onToolsContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${W.name} thread ended`,t),t==null)throw new p(`${W.name} thread ended unexpectedly`,{code:f.STEP_LOGIC_ISSUE});if(t instanceof Error)throw m(t,{unknownMessage:`${W.name} thread ended with an error`,unknownCode:f.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return;let{status:n,messages:r}=t;if(n!==`ok`||!Array.isArray(r))throw new p(`Unexpected end of the ${W.name} thread`,{code:f.STEP_LOGIC_ISSUE,data:e.params.result});this.state.toolsResults=r,this.state.toolsReady=!0,await this.tryToComplete()}async callFunction(e){this.state.toolsReady=!1,await this.initToolsContextThread(),await W.callTool(this.thread,e)}handleMultipleChoices(){let{chatCompletion:e}=this.state,t=e?.choices??[];t?.length>1&&this.log.WARN?.(`Multiple choices received from LLM, but only the first one will be processed`,{choices:t.length})}async initToolsContextThread(){this.triggers.hook({name:n.end,thread:W.getThreadId(this.thread.step.id)},this.onToolsContextEnd),await W.start(this.thread)}cancel(){this.end({status:`cancel`})}complete(){let{chatCompletion:e}=this.state;if(e==null)throw new p(`Missing chat completion content to return value`,{code:f.STEP_LOGIC_ISSUE});this.end({status:`ok`,chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Object)],K.prototype,`runStep`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],K.prototype,`initialize`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,Promise)],K.prototype,`onRequestContextEnd`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,Promise)],K.prototype,`onToolsContextEnd`,null),k([c({tags:G}),O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],K.prototype,`initToolsContextThread`,null);var q=class extends K{static class=`cnt_cxt_smp`;static getInitialState(){return{requestComplete:!1}}async onHandleContent(e){let{content:t}=e.params;this.state.chatCompletion=t;let n=t.choices[0]?.message.tool_calls;if(n!=null)for(let e of n)await this.callFunction(e)}onAllContentReceived(){this.handleMultipleChoices()}tryToComplete(){let{requestComplete:e,toolsReady:t}=this.state;!e||t===!1||this.complete()}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,Promise)],q.prototype,`onHandleContent`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],q.prototype,`onAllContentReceived`,null);const me={chunk:`chunk`};var J=class extends A{static class=`str_wrk`;static async start(e,t){await x(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return ae(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${j}_${this.class}_${e}`}runStep(){if(!(`event`in this.state)||this.state.event==null)throw new p(`Stream worker thread state must contain an event to process`,{code:f.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.(`Executing stream worker thread`,e),this.exitStep(me.chunk,{chunk:e})}};k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],J.prototype,`runStep`,null),i.config(i.locales.en());const he=i.looseObject({name:i.string(),params:i.looseObject({content:i.looseObject({id:i.string(),object:i.literal(`chat.completion.chunk`),created:i.number(),model:i.string(),service_tier:i.nullish(i.enum([`default`,`auto`,`flex`,`scale`,`priority`])),choices:i.array(i.looseObject({index:i.number().check(i.int(),i.gte(0)),delta:i.looseObject({role:i.optional(i.enum([`developer`,`system`,`user`,`assistant`,`tool`])),content:i.nullish(i.string()),refusal:i.nullish(i.string()),tool_calls:i.optional(i.array(i.looseObject({index:i.number().check(i.int(),i.gte(0)),id:i.optional(i.string()),type:i.optional(i.literal(`function`)),function:i.looseObject({name:i.optional(i.string()),arguments:i.optional(i.string())})})))}),logprobs:i.nullish(i.any()),finish_reason:i.nullable(i.enum([`stop`,`length`,`tool_calls`,`content_filter`]))})),logprobs:i.nullish(i.looseObject({content:i.nullable(i.array(i.looseObject({}))),refusal:i.nullable(i.array(i.looseObject({})))})),usage:i.nullish(i.looseObject({prompt_tokens:i.number(),completion_tokens:i.number(),total_tokens:i.number(),completion_tokens_details:i.optional(i.looseObject({accepted_prediction_tokens:i.optional(i.number()),audio_tokens:i.optional(i.number()),reasoning_tokens:i.optional(i.number()),rejected_prediction_tokens:i.optional(i.number())})),prompt_tokens_details:i.optional(i.looseObject({audio_tokens:i.optional(i.number()),cached_tokens:i.optional(i.number())}))}))})})});var ge=class{_result;_onToolCall;constructor(e,t){this._result=e,this._onToolCall=t}reduceChunk(e){if(Object.keys(this._result).length===0){let{choices:t,...n}=e;Object.assign(this._result,n),this._result.object!=null&&(this._result.object=`chat.completion`)}e.usage&&(this._result.usage=e.usage);for(let t of e.choices){let e=this._result.choices?.[t.index];if(!e){let{delta:n,...r}=t;e=r,this._result.choices??=[],this._result.choices[t.index]=e}if(t.finish_reason!=null&&(e.finish_reason=t.finish_reason),t.logprobs!=null&&(e.logprobs=t.logprobs),t.delta?.content!==void 0){let{content:n,...r}=t.delta;e.message??=r,n===null?e.message.content??=null:(e.message.content??=``,e.message.content+=n)}if(t.delta?.tool_calls!=null){let{tool_calls:n,...r}=t.delta;e.message??=r,e.message.tool_calls??=[];for(let n of t.delta.tool_calls){let r=n.index;if(e.message.tool_calls[r]==null)e.message.tool_calls[r]=structuredClone(n);else{let t=e.message.tool_calls[r];if(!(`function`in t)||t.function==null)continue;t.function.arguments+=n.function?.arguments??``}t.index===0&&this.tryParseToolCall(e.message.tool_calls[r])}}}}tryParseToolCall(e){try{if(!(`function`in e)||e.function==null)return;let t=JSON.parse(e.function.arguments);this._onToolCall(e,t)}catch{}}};const Y=[`streamInit`];var X=class e extends K{static class=`cnt_cxt_str`;static getInitialState(){return{class:e.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1}}async runStep(){await super.runStep(),l(Y),this.triggers.hook({name:n.end,thread:J.getThreadId(this.thread.step.id)},this.onWorkerThreadEnd)}onWorkerThreadEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${J.name} thread finished processing chunk`,t),t instanceof Error)throw m(t,{unknownMessage:`${J.name} thread ended with an error`});t?.status!==`cancel`&&this.tryRunWorker()}onHandleContent(e){let t=he.parse(e).params.content;new ge(this.state.partialChatCompletion,this.onToolCall.bind(this)).reduceChunk(t);for(let e of t.choices){if(e.index>0)continue;let t=e.delta?.content;t!=null&&t.length>0&&(this.addContentToQueue(t),this.scheduleNextContentEvent())}}onAllContentReceived(){this.log.DEBUG?.(`All LLM response chunks were received. No more response chunks expected`),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.state.events.push({lastChunkReceived:!0}),this.state.lastChunkReceived=!0,this.tryRunWorker()}onToolCall(e){this.log.DEBUG?.(`Tool call received`,e),this.callFunction(e)}scheduleNextContentEvent(){let e=this.state.events[0];e==null||!this.isEventReady(e)||`lastChunkReceived`in e||this.tryRunWorker()}addContentToQueue(e){let t=this.state.events.at(-1);if(t!=null){if(`lastChunkReceived`in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}if(t.content.length<this.maxChunkContentLength){let n=`${t.content}${e}`;t.content=n.slice(0,this.maxChunkContentLength),e=n.slice(this.maxChunkContentLength)}}for(;e;){this.state.index+=1;let t={content:e.slice(0,this.maxChunkContentLength),index:this.state.index,ts:Date.now()};this.log.DEBUG?.(`Add new event into the queue`,t),this.state.events.push(t),e=e.slice(this.maxChunkContentLength)}}tryRunWorker(){if(J.isBusy(this.thread)){this.log.DEBUG?.(`Skip event processing. Worker thread is busy`);return}let t=this.getEventToProcess();if(t!=null){if(`lastChunkReceived`in t){this.log.DEBUG?.(`Stopping ${e.name} thread. All response content chunks were processed and no new chunks are coming`),this.state.workerComplete=!0,this.tryToComplete();return}this.log.DEBUG?.(`Notify stream worker thread about response chunk to process`,t),J.start(this.thread,t)}}isEventReady(e){if(e==null)return!1;if(`lastChunkReceived`in e)return!0;if(e.content==null)return!1;let t=Date.now()-e.ts;return this.state.lastChunkReceived||t>=this.debounceDuration||e.content.length>=this.maxChunkContentLength}getEventToProcess(){let e=this.state.events[0];if(e!=null&&this.isEventReady(e))return`lastChunkReceived`in e||this.state.events.shift(),e}tryToComplete(){let{chatCompletion:e,toolsReady:t,requestComplete:n,workerComplete:r}=this.state;if(!(!r||!n||t===!1||e==null)){if(e.choices?.[0]?.message==null)throw new p(`Missing completion message in chat completion result`,{code:f.STEP_LOGIC_ISSUE});this.complete()}}get maxChunkContentLength(){return this.data.streamSettings?.maxBufferLength??100}get debounceDuration(){return this.data.streamSettings?.debounceDuration??200}};k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],X.prototype,`runStep`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,void 0)],X.prototype,`onWorkerThreadEnd`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,void 0)],X.prototype,`onHandleContent`,null),k([g,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],X.prototype,`onAllContentReceived`,null),k([c({tags:Y}),O(`design:type`,Number),O(`design:paramtypes`,[])],X.prototype,`maxChunkContentLength`,null),k([c({tags:Y}),O(`design:type`,Number),O(`design:paramtypes`,[])],X.prototype,`debounceDuration`,null);var Z,Q=class e{static factory(t){return new e(t?X:q)}Constructor;constructor(e){this.Constructor=e}async start(e){let t=this.Constructor.getInitialState();await x(e,{id:this.getThreadId(e.step.id),state:{...t,requestComplete:!1,step:e.step.id,class:this.Constructor.class},background:!0})}async cancel(e){await S(e,this.getThreadId(e.step.id),{result:{status:`cancel`},stopChildThreads:!0})}getThreadId(e){return`${j}_${this.Constructor.class}_${e}`}};k([c(),O(`design:type`,Function),O(`design:paramtypes`,[Boolean]),O(`design:returntype`,typeof(Z=Q!==void 0&&Q)==`function`?Z:Object)],Q,`factory`,null);const _e=e(import.meta.url);async function ve(e){if(typeof e!=`string`||!/^\d+\.\d+\.\d+$/.test(e))throw Error(`Invalid minimum version format: ${e}`);let t=_e.resolve(`@onereach/flow-sdk/package.json`);if(!t)throw Error(`Could not find package.json for @onereach/flow-sdk`);let n=await te(t,`utf8`),{version:r}=JSON.parse(n),{groups:i}=/^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/.exec(r)||{};if(!i)throw Error(`Invalid Flow SDK version format: ${r}`);let a=Number.parseInt(i.major??`0`,10),o=Number.parseInt(i.minor??`0`,10),s=Number.parseInt(i.patch??`0`,10);if(a==null||o==null||s==null||Number.isNaN(a)||Number.isNaN(o)||Number.isNaN(s))throw Error(`Invalid version format: ${r}`);let[c,l,u]=e.split(`.`).map(e=>Number.parseInt(e,10));if(c==null||l==null||u==null||Number.isNaN(c)||Number.isNaN(l)||Number.isNaN(u))throw Error(`Invalid minimum version format: ${e}`);if(a<c||a===c&&o<l||a===c&&o===l&&s<u)throw Error(`Flow SDK version '${r}' is lower than required minimum version '${e}' for the step`)}var $=class extends A{runStep(){this.triggers.hook({name:n.end,thread:this.contentContext.getThreadId(this.thread.step.id)},this.onContentContextEnd),this.triggers.hook({name:n.end,thread:F.getThreadId(this.thread.step.id)},this.onHistoryContextEnd),this.triggers.local(M.cancel,this.onCancel),this.triggers.otherwise(this.initialize)}async initialize(){await ve(`8.0.72`),this.validateMergeFieldType(),this.resetState(),this.state.cancelled=!1,await Promise.all([F.start(this.thread),N.start(this.thread)]),await F.prepareHistory(this.thread),await this.contentContext.start(this.thread)}async onContentContextEnd(e){try{let{result:t}=e.params.thread;if(this.log.DEBUG?.(`ContentContext thread ended with result`,t),t==null)throw new p(`ContentContext thread ended unexpectedly`,{code:f.STEP_LOGIC_ISSUE});if(t instanceof Error)throw m(t,{unknownMessage:`ContentContext thread ended with an error`,unknownCode:f.STEP_LOGIC_ISSUE});if(t.status===`cancel`){await this.resetStep();return}let{status:n,chatCompletion:r,toolsResults:i}=t;if(n!==`ok`||r==null||i==null)throw new p(`Unexpected end of the ContentContext thread`,{code:f.STEP_LOGIC_ISSUE,data:e.params.result});this.log.DEBUG?.(`Create Chat Completion result`,r),this.state.contentReady=!0,this.state.chatCompletion=r,this.state.toolsResults=i,await this.tryToExitStep()}catch(e){throw await this.resetStep().catch(()=>{}),e}}async onHistoryContextEnd(e){try{let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${F.name} thread ended with result`,t),t instanceof Error)throw m(t,{unknownMessage:`${F.name} thread ended with an error`,unknownCode:f.STEP_LOGIC_ISSUE})}catch(e){throw await N.stop(this.thread).catch(()=>{}),e}}async onCancel(){await this.resetStep()}async tryToExitStep(){let{cancelled:e,contentReady:t,chatCompletion:n,toolsResults:r}=this.state;if(e){this.resetState();return}if(!t)return;if(n==null)throw new p(`Missing chat completion result, but request is marked as processed`,{code:f.STEP_LOGIC_ISSUE});let i=n.choices?.[0]?.message;if(i==null)throw new p(`Missing completion message in chat completion result`,{code:f.STEP_LOGIC_ISSUE});await Promise.all([F.appendToHistory(this.thread,[i,...r]),N.stop(this.thread)]);let a=n.choices[0]?.finish_reason;if(a===`length`)throw new p(`Reached limit of tokens for LLM completion response`,{code:f.LLM_FINISH_LENGTH});if(a===`tool_calls`){this.thread.jumpTo(this.currentStepId);return}this.log.DEBUG?.(`Exiting step with collected data`,n),this.exitStep(`next`,n)}async resetStep(){this.resetState(),this.state.cancelled=!0;let e=(await Promise.allSettled([C(this.thread,{status:`cancel`}),F.adjustOnCancel(this.thread)])).find(e=>e.status===`rejected`);if(e)throw e.reason}resetState(){this.state.contentReady=!1,this.state.chatCompletion=void 0,this.state.toolsResults=[]}validateMergeFieldType(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0)&&(this.dataOut==null||this.getMergeField(this.dataOut).type!==`thread`))throw new p(`Merge-field type has to be "Thread" if you are using functions`,{code:f.VALIDATION})}get contentContext(){let e=this.data.streamResponse;return Q.factory(e)}};k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,void 0)],$.prototype,`runStep`,null),k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],$.prototype,`initialize`,null),k([g({allowHandleError:!0}),O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,Promise)],$.prototype,`onContentContextEnd`,null),k([g({allowHandleError:!0}),O(`design:type`,Function),O(`design:paramtypes`,[Object]),O(`design:returntype`,Promise)],$.prototype,`onHistoryContextEnd`,null),k([w,O(`design:type`,Function),O(`design:paramtypes`,[]),O(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const ye={[F.class]:F,[V.class]:V,[W.class]:W,[U.class]:U,[q.class]:q,[X.class]:X,[J.class]:J,[N.class]:N};export{ye as states,$ as step};
1
+ import{createRequire as e}from"node:module";import{install as t}from"source-map-support";import{ACTION as n}from"@onereach/flow-sdk/types/index.js";import r from"@onereach/flow-sdk/errors/timeout.js";import*as i from"zod/mini";import a from"@onereach/flow-sdk/errors/base.js";import o from"@onereach/flow-sdk/step.js";import{setImmediate as s}from"node:timers/promises";import{Memoize as c,clear as l}from"typescript-memoize";import{BasicThreadService as u}from"@onereach/flow-sdk/services/basic.js";import d from"openai";import f from"timestring";import{readFile as ee}from"node:fs/promises";const p={UNKNOWN:`UNKNOWN`,STEP_LOGIC_ISSUE:`STEP_LOGIC_ISSUE`,AUTH:`AUTH`,TIMEOUT:`TIMEOUT`,VALIDATION:`VALIDATION`,INVALID_REQUEST:`INVALID_REQUEST`,RATE_LIMITED:`RATE_LIMITED`,LLM_FINISH_LENGTH:`LLM_FINISH_LENGTH`,SERVER_ERROR:`SERVER_ERROR`,UNSUPPORTED:`UNSUPPORTED`};var m=class extends a{},te=class extends a{};i.config(i.locales.en());const ne=[m,r];function h(e,t={}){let{preserveErrors:n=ne,unknownMessage:a=`Unknown error`,unknownCode:o=p.UNKNOWN}=t;if(n?.some(t=>e instanceof t))return e;if(e instanceof i.core.$ZodError)return new m(i.prettifyError(e),e,{code:p.VALIDATION});if(typeof e==`string`)return new m(e,{code:p.UNKNOWN});if(e instanceof Error){if(e.message.includes(`Request timed out`))return new r(`Timeout`,e,{code:p.TIMEOUT});let t=e?.response?.data?.error?.message;return t==null?new m(a,e,{code:o}):new m(t,e,{code:p.SERVER_ERROR})}return new m(a,{code:o,data:e})}function g(e,t){function n(t,n,r,i){let a=e(t,n,r,i);if(typeof a==`function`){let e=i.value,t=a;if(e?.name!=null)try{Object.defineProperty(t,"name",{value:e.name})}catch{}return i.value=t,i}return a??i}return function(e,r,i){if(typeof e==`object`&&e&&(typeof r==`string`||typeof r==`symbol`)&&typeof i==`object`&&i)return n(t,e,r,i);let a=e??t;return(e,t,r)=>n(a,e,t,r)}}const _=g((e,t,n,r)=>{let i=r.value;if(!(!i||typeof i!=`function`))return function(...t){try{let r=i.apply(this,t);return r instanceof Promise?r.catch(t=>v.call(this,e,t,n)):r}catch(t){return v.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function v(e,t,r){let i=r.toString();this.log.DEBUG?.(`Error in method '${i}'`,t);let a=e.useErrorFilter?h(t):t;if(a instanceof Error){if(e.allowHandleError){this.thread.enqueue({name:n.error,error:a});return}this.end(a);return}throw a}function re({thread:e,...t}){let n=x({thread:e,...t}),r=e.process;if(r.cache[n]!=null)throw Error(`AbortController for key '${n}' already exists`);return r.cache[n]=new AbortController}function y({thread:e,...t}){let n=x({thread:e,...t});return e.process.cache[n]}function b({thread:e,...t}){let n=x({thread:e,...t});delete e.process.cache[n]}function x({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function S(e,{id:t,state:r,local:i,...a},{force:o}={}){let s=e.process.getThread(t);if(e.log.DEBUG?.(`runThread: thread with id '${t}'`,a),s==null)return await e.thread.runThread({...a,id:t,state:r,local:{...i,parent:e.thread.id,active:!0}});if(!o&&!s.ended&&s.local.active)throw Error(`Thread with id '${t}' has not ended yet, cannot start it again`);if(r==null)throw Error(`Thread with id '${t}' must have a state to run`);return s.local.active=!0,await s.enqueueAndRun({name:n.goto,state:{...r,active:!0}})}function C(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function ie(e,t=e.id){let n=[];for(let r in e.process.threads){let i=e.process.threads[r];i?.local.parent===t&&n.push(i)}return n}async function w(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await T(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function T(e,t,n={}){let r=ie(e).map(async n=>await w(e,n.id,{stopChildThreads:!0,result:t})),i=await Promise.allSettled(r);if(n.throw??!0){let e=i.find(e=>e.status===`rejected`);if(e)throw e.reason}return i}const E=g((e,t,n,r)=>{let i=r.value;if(!(!i||typeof i!=`function`))return function(...e){try{let t=i.apply(this,e);return t instanceof Promise?t.catch(e=>{let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),h(e)}):t}catch(e){let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),h(e)}}},{});i.config(i.locales.en());const D=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),O=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:D}),i.object({role:i.literal(`user`),content:D,name:i.optional(D.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(D),tool_calls:i.optional(i.array(i.object({id:D.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:D.check(i.minLength(1)),arguments:D.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:D,name:i.optional(D.check(i.minLength(1),i.maxLength(64))),tool_call_id:D.check(i.minLength(1))})]),ae=i.object({type:i.literal(`object`),properties:i.record(i.string().check(i.minLength(1),i.maxLength(64)),i.object({type:i.enum([`string`,`number`,`boolean`,`array`,`object`]),description:i.optional(i.string().check(i.maxLength(1e3))),items:i.optional(i.object({type:i.enum([`string`,`number`,`boolean`,`object`]),description:i.optional(i.string().check(i.maxLength(1e3)))}))}))}),oe=i.object({provider:D.check(i.minLength(1)),model:D.check(i.minLength(1)),accountKey:k(D),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(D),userMessage:i.optional(D),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(D),temperature:k(i.coerce.number().check(i.gte(0),i.lte(2))),topP:k(i.coerce.number().check(i.gte(0),i.lte(1))),maxTokens:k(i.coerce.number().check(i.int(),i.gte(1))),presencePenalty:k(i.coerce.number().check(i.gte(-2),i.lte(2))),frequencyPenalty:k(i.coerce.number().check(i.gte(-2),i.lte(2))),n:k(i.coerce.number().check(i.int(),i.gte(1),i.lte(128))),stop:i.optional(i.array(i.object({token:i.string().check(i.minLength(1),i.maxLength(64))})).check(i.maxLength(4))),reasoningEffort:k(i.enum([`none`,`minimal`,`low`,`medium`,`high`,`xhigh`])),streamResponse:i._default(i.boolean(),!1),streamSettings:i.optional(i.object({maxBufferLength:i.coerce.number().check(i.overwrite(e=>e===0?50:e),i.int(),i.gte(1),i.lte(32768)),debounceDuration:i.coerce.number().check(i.int(),i.gte(0),i.lte(1e4))})),customHistory:i._default(i.boolean(),!1),historyLength:k(i.coerce.number().check(i.int(),i.gte(1),i.lte(100))),historyMode:i.optional(i.enum([`mergefield`,`codeHistory`])),historyMergefield:i.optional(i.array(O)),historyCode:i.optional(i.array(O)),functionsList:i.array(i.object({name:D.check(i.minLength(1),i.maxLength(64)),description:i.optional(D.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(ae,i.transform(e=>(e.required=Object.keys(e.properties),e.additionalProperties=!1,e)))),strict:i.boolean()})),useState:i.optional(i.boolean()),stateSettings:i.optional(i.object({stateFunction:i.function()})),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:D.check(i.trim(),i.minLength(1))}).check(i.refine(e=>!e.streamResponse||e.provider===`openai`,{error:`Streaming is only supported for OpenAI provider`}),i.refine(e=>!e.streamResponse||e.streamSettings!==void 0,{error:`Missing "Stream settings" for response streaming mode`}),i.refine(e=>e.typeOfStep!==`chat`||e.customHistory||!e.switchAssistantsMessage||e.assistantsFirstMessage!==void 0,{error:`Missing "Assistant's first message" value`}),i.refine(e=>e.customHistory||e.userMessage!==void 0,{error:`Missing "User message" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==void 0,{error:`Missing "History mode" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield!==void 0,{error:`Missing "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode!==void 0,{error:`Missing "History" data as code`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via code`}),i.refine(e=>!e.useState||e.stateSettings!=null,{error:`Missing state settings`}));function k(e){return i.optional(i.pipe(i.union([i.undefined(),i.literal(``),e]),i.transform(e=>e===``?void 0:e)))}function A(e,t){if(typeof Reflect==`object`&&typeof Reflect.metadata==`function`)return Reflect.metadata(e,t)}function j(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var M=class extends o{async resolveDataIn(){let e=await super.resolveDataIn();return oe.parse(e)}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],M.prototype,`resolveDataIn`,null);const N=`llm_cmp`,P={cancel:`st_cancel`},se={cancel:`cnl_cancel`};var F=class e extends M{static class=`cncl_ctrl`;static async start(e){await S(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await w(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local({name:se.cancel},this.onCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onCancel(){this.log.DEBUG?.(`Canceling LLM request`);let e=this.local.parent,t=this.process.getThread(e);if(!t||t.ended)return this.end();let n=y({thread:this.thread});n?n.abort(new te(`Request cancelled`)):this.process.enqueueAndRun({name:P.cancel,thread:e})}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`runStep`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`initialize`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`onCancel`,null);const I={appendToHistory:`hst_append`,prepareHistory:`hst_prepare`,adjustOnCancel:`hst_cncl`};var L=class e extends M{static class=`hst_ctrl`;static async start(e){let t=this.getThreadId(e.step.id);e.process.getThread(t)??await e.process.runThread({id:t,background:!0,state:{step:e.step.id,class:this.class,messages:[]}})}static async prepareHistory(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:I.prepareHistory})}static async appendToHistory(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:I.appendToHistory,params:{message:t}})}static async adjustOnCancel(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:I.adjustOnCancel})}static getHistory(e){return e.process.getSafeThread(this.getThreadId(e.step.id)).state.messages}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local(I.prepareHistory,this.onPrepareHistory).local(I.appendToHistory,this.onAppendToHistory).local(I.adjustOnCancel,this.onAdjustOnCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onAdjustOnCancel(){let e;for(;(e=this.state.messages.at(-1))&&!([`system`,`user`].includes(e.role)||e.role===`assistant`&&e.tool_calls==null);){let e=this.state.messages.pop();this.log.DEBUG?.(`Removed message from history`,e)}}onPrepareHistory(){this.log.DEBUG?.(`Preparing history`);let{customHistory:e,historyLength:t,systemMessage:n,typeOfStep:r,userMessage:i,historyMode:a,historyMergefield:o,historyCode:s,assistantsFirstMessage:c}=this.data;if(r===`completion`){this.state.messages=[{role:`user`,content:i}];return}if(e){this.state.messages=a===`mergefield`?o:s;return}let l=this.state.messages;if(l.length===0&&c!=null&&c.length>0&&l.push({role:`assistant`,content:c}),l.at(-1)?.role!==`tool`&&l.push({role:`user`,content:i}),!l.some(e=>e.role===`tool`)&&t!=null&&t>0&&(l=l.slice(-1*t)),n!=null&&n.length>0){let e={role:`system`,content:n};l[0]?.role===`system`?l[0]=e:l.unshift(e)}this.state.messages=l}onAppendToHistory(e){this.log.DEBUG?.(`Appending message to history`,e.params);let{message:t}=e.params,n=Array.isArray(t)?t:[t];this.state.messages.push(...n)}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`runStep`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`initialize`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onAdjustOnCancel`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onPrepareHistory`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],L.prototype,`onAppendToHistory`,null);const R={handleContent:`hnd_cnt`};var z=class extends u{async handleContentByParentThread(e){await s(),await this.thread.process.enqueueAndRun({thread:this.thread.local.parent,name:R.handleContent,params:{content:e}})}},ce=class extends z{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),b({thread:this.thread})}},le=class extends z{async handle(e){this.log.DEBUG?.(`Start processing stream of chunks from LLM`);for await(let t of e)await this.handleContentByParentThread(t);b({thread:this.thread})}};function ue({thread:e,isStreaming:t}){return t?new le(e):new ce(e)}var de=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var B=class extends de{async makeChatCompletionRequest(e,t){let{model:n,temperature:r,topP:i,maxTokens:a,frequencyPenalty:o,presencePenalty:s,n:c,streamResponse:l,reasoningEffort:u}=this.data,d={messages:structuredClone(e),model:n,temperature:r,top_p:i,max_tokens:a,frequency_penalty:o,presence_penalty:s,stop:this.stopSequences,n:c,reasoning_effort:u,stream:l,tools:this.tools};this.log.DEBUG?.(`LLM request`,d);try{let e=await this.doChatCompletionRequest(d,{signal:t});return l||this.log.DEBUG?.(`LLM response`,e),e}catch(e){throw e instanceof Error?new m(`Chat completion request failed`,e,{code:p.SERVER_ERROR,data:d}):e}}async doChatCompletionRequest(e,t){return await this.openAiClient.chat.completions.create(e,t)}get tools(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0))return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:!e.parameters||Object.keys(e.parameters).length===0?void 0:e.parameters,strict:e.strict}}))}get openAiClient(){let{provider:e,accountKey:t}=this.data;return new d({baseURL:`${this.config.env.OPENAI_API_URL}/${e}`,apiKey:`_fake_key_`,defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get([`reporting`,`sessionId`]),...t?{"x-or-account-token-name":t}:void 0},timeout:this.timeoutMs})}get stopSequences(){return(this.data.stop??[]).map(e=>e.token).filter(e=>e.length>0)}get timeoutMs(){let e=f(this.data.timeoutDuration,`ms`);return i.number().check(i.int(),i.gte(1e3),i.lte(6e5)).parse(e)}};j([c(),A(`design:type`,Number),A(`design:paramtypes`,[])],B.prototype,`timeoutMs`,null);const V=[`requestInit`];var H=class extends M{static class=`rqst_ctx`;static async start(e){return await S(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0})}static getThreadId(e){return`${N}_${this.class}_${e}`}cancel(){this.end({status:`cancel`})}complete(){this.end({status:`ok`})}async getHistory(){let e=L.getHistory(this.thread);if(!this.data.useState)return e;let t=[...e],n={role:`assistant`,content:await this.data.stateSettings?.stateFunction()};if(e.at(-1)?.role===`user`){let e=Math.max(t.length-1,0);t.splice(e,0,n)}else t.push(n);return t}get openAiService(){return new B(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return ue({thread:this.thread,isStreaming:e})}async runStep(){l(V);let e=re({thread:this.thread});try{let t=await this.openAiService.makeChatCompletionRequest(await this.getHistory(),e.signal);await this.responseHandler.handle(t),this.complete()}catch(t){if(e?.signal.aborted)return this.cancel();throw t}finally{b({thread:this.thread})}}};j([c(),A(`design:type`,B===void 0?Object:B),A(`design:paramtypes`,[])],H.prototype,`openAiService`,null),j([c({tags:V}),A(`design:type`,Object),A(`design:paramtypes`,[])],H.prototype,`responseHandler`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],H.prototype,`runStep`,null);var U=class extends M{static class=`tl_wrk`;static async start(e,t){let n=JSON.parse(t.function?.arguments);await S(e,{id:this.getThreadId(t),state:{step:e.step.id,class:this.class},local:{toolCall:t,parsedArguments:n,parent:e.id,active:!0}})}static getThreadId(e){return`${N}_${this.class}_${e.id}`}runStep(){let{toolCall:e,parsedArguments:t}=this.local;this.log.DEBUG?.(`Initializing tool worker thread`,e),this.exitStep(this.getExitName(),{function:{name:e.function.name,arguments:t}})}getExitName(){return`function`}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],U.prototype,`runStep`,null);const W={callTool:`tl_run_call`,lastToolCallReceived:`tl_rsp_end`};var G=class e extends M{static class=`tl_ctrl`;static async start(e){await S(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,tasks:{},expectMoreTasks:!0},background:!0})}static async callTool(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:W.callTool,params:{toolCall:t}})}static async lastToolCallReceived(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:W.lastToolCallReceived})}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local(W.callTool,this.onCallTool).local(W.lastToolCallReceived,this.onLastToolCallReceived).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${e.name} thread`)}onCallTool(e){let{toolCall:t}=e.params;if(t.type!==`function`)throw new m(`Only function tools are supported at the moment`,{code:p.UNSUPPORTED});this.state.tasks[t.id]={call:t,status:`pending`},this.startToolWorkerThread(t)}onLastToolCallReceived(){this.log.DEBUG?.(`No more tool calls expected`,this.state.tasks),this.state.expectMoreTasks=!1,this.tryEndToolsContextThread()}onWorkerThreadEnd(e){this.log.DEBUG?.(`Tool caller worker thread has ended`,e);let{result:t}=e.params.thread;if(t==null)throw new m(`${U.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw h(t,{unknownMessage:`${U.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});t.status!==`cancel`&&(this.log.DEBUG?.(`Tool worker thread completed successfully`,{toolCallId:t.toolCallId,result:t.data}),this.setToolCallResult(t.toolCallId,t.data),this.tryEndToolsContextThread())}startToolWorkerThread(e){this.triggers.hook({name:n.end,thread:U.getThreadId(e)},this.onWorkerThreadEnd),U.start(this.thread,e)}setToolCallResult(e,t){let n=this.state.tasks[e];if(n==null)throw new m(`Unknown tool call with id '${e}'`,{code:p.STEP_LOGIC_ISSUE});n.status=`completed`,n.result=t}tryEndToolsContextThread(){this.state.expectMoreTasks||Object.values(this.state.tasks).some(e=>e.status===`pending`)||(this.log.DEBUG?.(`All tool calls completed or failed`,this.state.tasks),this.end({status:`ok`,messages:this.messages}))}get messages(){return Object.values(this.state.tasks).map(e=>({role:`tool`,tool_call_id:e.call.id,content:JSON.stringify(e.result)}))}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`runStep`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`initialize`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onCallTool`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`onLastToolCallReceived`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onWorkerThreadEnd`,null);const K=[`toolsInit`];var q=class extends M{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(K),this.triggers.local(R.handleContent,this.onHandleContent).hook({name:n.end,thread:H.getThreadId(this.thread.step.id)},this.onRequestContextEnd).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`),H.start(this.thread)}async onRequestContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${H.name} thread ended with result`,t),t==null)throw new m(`${H.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw h(t,{unknownMessage:`${H.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return this.cancel();if(t.status!==`ok`)throw new m(`Unexpected end of the ${H.name} thread`,{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.state.requestComplete=!0,await this.onAllContentReceived(),this.state.toolsReady!=null&&(await s(),await G.lastToolCallReceived(this.thread)),await this.tryToComplete()}async onToolsContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${G.name} thread ended`,t),t==null)throw new m(`${G.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw h(t,{unknownMessage:`${G.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return;let{status:n,messages:r}=t;if(n!==`ok`||!Array.isArray(r))throw new m(`Unexpected end of the ${G.name} thread`,{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.state.toolsResults=r,this.state.toolsReady=!0,await this.tryToComplete()}async callFunction(e){this.state.toolsReady=!1,await this.initToolsContextThread(),await G.callTool(this.thread,e)}handleMultipleChoices(){let{chatCompletion:e}=this.state,t=e?.choices??[];t?.length>1&&this.log.WARN?.(`Multiple choices received from LLM, but only the first one will be processed`,{choices:t.length})}async initToolsContextThread(){this.triggers.hook({name:n.end,thread:G.getThreadId(this.thread.step.id)},this.onToolsContextEnd),await G.start(this.thread)}cancel(){this.end({status:`cancel`})}complete(){let{chatCompletion:e}=this.state;if(e==null)throw new m(`Missing chat completion content to return value`,{code:p.STEP_LOGIC_ISSUE});this.end({status:`ok`,chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Object)],q.prototype,`runStep`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],q.prototype,`initialize`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],q.prototype,`onRequestContextEnd`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],q.prototype,`onToolsContextEnd`,null),j([c({tags:K}),A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],q.prototype,`initToolsContextThread`,null);var J=class extends q{static class=`cnt_cxt_smp`;static getInitialState(){return{class:this.class,requestComplete:!1}}async onHandleContent(e){let{content:t}=e.params;this.state.chatCompletion=t;let n=t.choices[0]?.message.tool_calls;if(n!=null)for(let e of n)await this.callFunction(e)}onAllContentReceived(){this.handleMultipleChoices()}tryToComplete(){let{requestComplete:e,toolsReady:t}=this.state;!e||t===!1||this.complete()}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],J.prototype,`onHandleContent`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],J.prototype,`onAllContentReceived`,null);var fe=class{result;onToolCall;constructor(e,t){this.result=e,this.onToolCall=t}reduceChunk(e){if(Object.keys(this.result).length===0){let{choices:t,...n}=e;Object.assign(this.result,n),this.result.object!=null&&(this.result.object=`chat.completion`)}e.usage&&(this.result.usage=e.usage);for(let t of e.choices)this.reduceChunkChoice(t)}reduceChunkChoice(e){let t=this.result.choices?.[e.index];if(!t){let{delta:n,...r}=e;t=r,this.result.choices??=[],this.result.choices[e.index]=t}if(e.finish_reason!=null&&(t.finish_reason=e.finish_reason),e.logprobs!=null&&(t.logprobs=e.logprobs),e.delta?.content!==void 0){let{content:n,...r}=e.delta;t.message??=r,n===null?t.message.content??=null:(t.message.content??=``,t.message.content+=n)}if(e.delta?.tool_calls!=null){let{tool_calls:n,...r}=e.delta;t.message??=r,t.message.tool_calls??=[];for(let r of n){let n=r.index;if(t.message.tool_calls[n]==null)t.message.tool_calls[n]=structuredClone(r);else{let e=t.message.tool_calls[n];if(!(`function`in e)||e.function==null)continue;e.function.arguments+=r.function?.arguments??``}e.index===0&&this.onToolCall!=null&&this.tryParseToolCall(t.message.tool_calls[n])}}}tryParseToolCall(e){try{if(!(`function`in e)||e.function==null)return;let t=JSON.parse(e.function.arguments);this.onToolCall!=null&&this.onToolCall(e,t)}catch{}}};i.config(i.locales.en());const pe=i.looseObject({name:i.string(),params:i.looseObject({content:i.looseObject({id:i.string(),object:i.literal(`chat.completion.chunk`),created:i.number(),model:i.string(),service_tier:i.nullish(i.enum([`default`,`auto`,`flex`,`scale`,`priority`])),choices:i.array(i.looseObject({index:i.number().check(i.int(),i.gte(0)),delta:i.looseObject({role:i.optional(i.enum([`developer`,`system`,`user`,`assistant`,`tool`])),content:i.nullish(i.string()),refusal:i.nullish(i.string()),tool_calls:i.optional(i.array(i.looseObject({index:i.number().check(i.int(),i.gte(0)),id:i.optional(i.string()),type:i.optional(i.literal(`function`)),function:i.looseObject({name:i.optional(i.string()),arguments:i.optional(i.string())})})))}),logprobs:i.nullish(i.any()),finish_reason:i.nullable(i.enum([`stop`,`length`,`tool_calls`,`content_filter`]))})),logprobs:i.nullish(i.looseObject({content:i.nullable(i.array(i.looseObject({}))),refusal:i.nullable(i.array(i.looseObject({})))})),usage:i.nullish(i.looseObject({prompt_tokens:i.number(),completion_tokens:i.number(),total_tokens:i.number(),completion_tokens_details:i.optional(i.looseObject({accepted_prediction_tokens:i.optional(i.number()),audio_tokens:i.optional(i.number()),reasoning_tokens:i.optional(i.number()),rejected_prediction_tokens:i.optional(i.number())})),prompt_tokens_details:i.optional(i.looseObject({audio_tokens:i.optional(i.number()),cached_tokens:i.optional(i.number())}))}))})})}),me={chunk:`chunk`};var Y=class extends M{static class=`str_wrk`;static async start(e,t){await S(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return C(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){if(!(`event`in this.state)||this.state.event==null)throw new m(`Stream worker thread state must contain an event to process`,{code:p.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.(`Executing stream worker thread`,e),this.exitStep(me.chunk,{chunk:e})}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],Y.prototype,`runStep`,null);const X=[`streamInit`];var Z=class e extends q{static class=`cnt_cxt_str`;static getInitialState(){return{class:this.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1}}onToolCall(e){this.log.DEBUG?.(`Tool call received`,e),this.callFunction(e)}scheduleNextContentEvent(){let e=this.state.events[0];e==null||!this.isEventReady(e)||`lastChunkReceived`in e||this.tryRunWorker()}addContentToQueue(e){let t=this.state.events.at(-1);if(t!=null){if(`lastChunkReceived`in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}if(t.content.length<this.maxChunkContentLength){let n=`${t.content}${e}`;t.content=n.slice(0,this.maxChunkContentLength),e=n.slice(this.maxChunkContentLength)}}for(;e;){this.state.index+=1;let t={content:e.slice(0,this.maxChunkContentLength),index:this.state.index,ts:Date.now()};this.log.DEBUG?.(`Add new event into the queue`,t),this.state.events.push(t),e=e.slice(this.maxChunkContentLength)}}tryRunWorker(){if(Y.isBusy(this.thread)){this.log.DEBUG?.(`Skip event processing. Worker thread is busy`);return}let t=this.getEventToProcess();if(t!=null){if(`lastChunkReceived`in t){this.log.DEBUG?.(`Stopping ${e.name} thread. All response content chunks were processed and no new chunks are coming`),this.state.workerComplete=!0,this.tryToComplete();return}this.log.DEBUG?.(`Notify stream worker thread about response chunk to process`,t),Y.start(this.thread,t)}}isEventReady(e){if(e==null)return!1;if(`lastChunkReceived`in e)return!0;if(e.content==null)return!1;let t=Date.now()-e.ts;return this.state.lastChunkReceived||t>=this.debounceDuration||e.content.length>=this.maxChunkContentLength}getEventToProcess(){let e=this.state.events[0];if(e!=null&&this.isEventReady(e))return`lastChunkReceived`in e||this.state.events.shift(),e}tryToComplete(){let{chatCompletion:e,toolsReady:t,requestComplete:n,workerComplete:r}=this.state;if(!(!r||!n||t===!1||e==null)){if(e.choices?.[0]?.message==null)throw new m(`Missing completion message in chat completion result`,{code:p.STEP_LOGIC_ISSUE});this.complete()}}onAllContentReceived(){this.log.DEBUG?.(`All LLM response chunks were received. No more response chunks expected`),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.state.events.push({lastChunkReceived:!0}),this.state.lastChunkReceived=!0,this.tryRunWorker()}get maxChunkContentLength(){return this.data.streamSettings?.maxBufferLength??100}get debounceDuration(){return this.data.streamSettings?.debounceDuration??200}async runStep(){await super.runStep(),l(X),this.triggers.hook({name:n.end,thread:Y.getThreadId(this.thread.step.id)},this.onWorkerThreadEnd)}onWorkerThreadEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${Y.name} thread finished processing chunk`,t),t instanceof Error)throw h(t,{unknownMessage:`${Y.name} thread ended with an error`});t?.status!==`cancel`&&this.tryRunWorker()}onHandleContent(e){let t=pe.parse(e).params.content;new fe(this.state.partialChatCompletion,this.onToolCall.bind(this)).reduceChunk(t);for(let e of t.choices){if(e.index>0)continue;let t=e.delta?.content;t!=null&&t.length>0&&(this.addContentToQueue(t),this.scheduleNextContentEvent())}}};j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],Z.prototype,`onAllContentReceived`,null),j([c({tags:X}),A(`design:type`,Number),A(`design:paramtypes`,[])],Z.prototype,`maxChunkContentLength`,null),j([c({tags:X}),A(`design:type`,Number),A(`design:paramtypes`,[])],Z.prototype,`debounceDuration`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],Z.prototype,`runStep`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onWorkerThreadEnd`,null),j([_,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onHandleContent`,null);const he=e(import.meta.url);async function ge(e){if(typeof e!=`string`||!/^\d+\.\d+\.\d+$/.test(e))throw Error(`Invalid minimum version format: ${e}`);let t=he.resolve(`@onereach/flow-sdk/package.json`);if(!t)throw Error(`Could not find package.json for @onereach/flow-sdk`);let n=await ee(t,`utf8`),{version:r}=JSON.parse(n),{groups:i}=/^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/.exec(r)||{};if(!i)throw Error(`Invalid Flow SDK version format: ${r}`);let a=Math.trunc(Number(i.major??`0`)),o=Math.trunc(Number(i.minor??`0`)),s=Math.trunc(Number(i.patch??`0`));if(a==null||o==null||s==null||Number.isNaN(a)||Number.isNaN(o)||Number.isNaN(s))throw Error(`Invalid version format: ${r}`);let[c,l,u]=e.split(`.`).map(e=>Math.trunc(Number(e)));if(c==null||l==null||u==null||Number.isNaN(c)||Number.isNaN(l)||Number.isNaN(u))throw Error(`Invalid minimum version format: ${e}`);if(a<c||a===c&&o<l||a===c&&o===l&&s<u)throw Error(`Flow SDK version '${r}' is lower than required minimum version '${e}' for the step`)}var Q=class e{Constructor;constructor(e){this.Constructor=e}static factory(t){return new e(t?Z:J)}async start(e){let t=this.Constructor.getInitialState();await S(e,{id:this.getThreadId(e.step.id),state:{...t,requestComplete:!1,step:e.step.id,class:this.Constructor.class},background:!0})}async cancel(e){await w(e,this.getThreadId(e.step.id),{result:{status:`cancel`},stopChildThreads:!0})}getThreadId(e){return`${N}_${this.Constructor.class}_${e}`}};j([c(),A(`design:type`,Function),A(`design:paramtypes`,[Boolean]),A(`design:returntype`,Q===void 0?Object:Q)],Q,`factory`,null);var $=class extends M{async tryToExitStep(){let{cancelled:e,contentReady:t,chatCompletion:n,toolsResults:r}=this.state;if(e){this.resetState();return}if(!t)return;if(n==null)throw new m(`Missing chat completion result, but request is marked as processed`,{code:p.STEP_LOGIC_ISSUE});let i=n.choices?.[0]?.message;if(i==null)throw new m(`Missing completion message in chat completion result`,{code:p.STEP_LOGIC_ISSUE});await Promise.all([L.appendToHistory(this.thread,[i,...r]),F.stop(this.thread)]);let a=n.choices[0]?.finish_reason;if(a===`length`)throw new m(`Reached limit of tokens for LLM completion response`,{code:p.LLM_FINISH_LENGTH});if(a===`tool_calls`){this.thread.jumpTo(this.currentStepId);return}this.log.DEBUG?.(`Exiting step with collected data`,n),this.exitStep(`next`,n)}async resetStep(){this.resetState(),this.state.cancelled=!0;let e=(await Promise.allSettled([T(this.thread,{status:`cancel`}),L.adjustOnCancel(this.thread)])).find(e=>e.status===`rejected`);if(e)throw e.reason}resetState(){this.state.contentReady=!1,this.state.chatCompletion=void 0,this.state.toolsResults=[]}validateMergeFieldType(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0)&&(this.dataOut==null||this.getMergeField(this.dataOut).type!==`thread`))throw new m(`Merge-field type has to be "Thread" if you are using functions`,{code:p.VALIDATION})}get contentContext(){let e=this.data.streamResponse;return Q.factory(e)}runStep(){this.triggers.hook({name:n.end,thread:this.contentContext.getThreadId(this.thread.step.id)},this.onContentContextEnd),this.triggers.hook({name:n.end,thread:L.getThreadId(this.thread.step.id)},this.onHistoryContextEnd),this.triggers.local(P.cancel,this.onCancel),this.triggers.otherwise(this.initialize)}async initialize(){await ge(`8.0.72`),this.validateMergeFieldType(),this.resetState(),this.state.cancelled=!1,await Promise.all([L.start(this.thread),F.start(this.thread)]),await L.prepareHistory(this.thread),await this.contentContext.start(this.thread)}async onContentContextEnd(e){try{let{result:t}=e.params.thread;if(this.log.DEBUG?.(`ContentContext thread ended with result`,t),t==null)throw new m(`ContentContext thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw h(t,{unknownMessage:`ContentContext thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});if(t.status===`cancel`){await this.resetStep();return}let{status:n,chatCompletion:r,toolsResults:i}=t;if(n!==`ok`||r==null||i==null)throw new m(`Unexpected end of the ContentContext thread`,{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.log.DEBUG?.(`Create Chat Completion result`,r),this.state.contentReady=!0,this.state.chatCompletion=r,this.state.toolsResults=i,await this.tryToExitStep()}catch(e){try{await this.resetStep()}catch(e){this.log.WARN?.(`Failed to reset step`,e)}throw e}}async onHistoryContextEnd(e){try{let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${L.name} thread ended with result`,t),t instanceof Error)throw h(t,{unknownMessage:`${L.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE})}catch(e){try{await F.stop(this.thread)}catch(e){this.log.WARN?.(`Failed to stop CancelContext thread`,e)}throw e}}async onCancel(){await this.resetStep()}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],$.prototype,`runStep`,null),j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`initialize`,null),j([_({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onContentContextEnd`,null),j([_({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onHistoryContextEnd`,null),j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const _e={[L.class]:L,[H.class]:H,[G.class]:G,[U.class]:U,[J.class]:J,[Z.class]:Z,[Y.class]:Y,[F.class]:F};export{_e as states,$ as step};
2
2
  //# sourceMappingURL=index.mjs.map