@onereach/step-create-chat-completion 0.0.7-rc.66 → 0.0.7-rc.67
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.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/schemas/data-in.ts","../src/threads/base/types.ts","../src/threads/base/base.ts","../src/utils/thread-utilities.ts","../src/threads/base/child-types.ts","../src/threads/cancel/constants.ts","../src/threads/cancel/types.ts","../src/threads/main/types.ts","../src/threads/cancel/cancel-context.ts","../src/threads/request/constants.ts","../src/threads/request/types.ts","../src/threads/tools/context/constants.ts","../src/threads/tools/context/types.ts","../src/threads/content/constants.ts","../src/threads/content/types.ts","../src/threads/content/abstract-content-context.ts","../src/threads/content/simple/constants.ts","../src/threads/content/simple/types.ts","../src/threads/content/simple/simple-content-context.ts","../src/threads/content/stream/worker/constants.ts","../src/threads/content/stream/worker/types.ts","../src/threads/content/stream/context/constants.ts","../src/threads/content/stream/context/types.ts","../src/threads/content/stream/context/stream-content-context.ts","../src/threads/content/stream/worker/stream-content-context-worker.ts","../src/threads/history/types.ts","../src/threads/history/history-context.ts","../src/threads/request/request-context.ts","../src/threads/tools/worker/constants.ts","../src/threads/tools/worker/types.ts","../src/threads/tools/context/tools-context.ts","../src/threads/tools/worker/tools-context-worker.ts","../src/threads/main/step.ts","../src/index.ts"],"mappings":";;;;;;;;cA4Ea,YAAA,EAAY,CAAA,CAAA,aAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAoKb,MAAA,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AAsB,MAAA,EAAQ,OAAA,CAAQ,CAAA,IAAK,OAAA,CAAQ,oBAAA;EAAA,OAWzE,WAAA,CAAY,MAAA,EAAQ,OAAA,GAAU,SAAA;EAAA,QAMpC,MAAA;EAAA,QAIA,QAAA;EAAA,QAIM,UAAA;EAAA,
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/schemas/data-in.ts","../src/threads/base/types.ts","../src/threads/base/base.ts","../src/utils/thread-utilities.ts","../src/threads/base/child-types.ts","../src/threads/cancel/constants.ts","../src/threads/cancel/types.ts","../src/threads/main/types.ts","../src/threads/cancel/cancel-context.ts","../src/threads/request/constants.ts","../src/threads/request/types.ts","../src/threads/tools/context/constants.ts","../src/threads/tools/context/types.ts","../src/threads/content/constants.ts","../src/threads/content/types.ts","../src/threads/content/abstract-content-context.ts","../src/threads/content/simple/constants.ts","../src/threads/content/simple/types.ts","../src/threads/content/simple/simple-content-context.ts","../src/threads/content/stream/worker/constants.ts","../src/threads/content/stream/worker/types.ts","../src/threads/content/stream/context/constants.ts","../src/threads/content/stream/context/types.ts","../src/threads/content/stream/context/stream-content-context.ts","../src/threads/content/stream/worker/stream-content-context-worker.ts","../src/threads/history/types.ts","../src/threads/history/history-context.ts","../src/threads/request/request-context.ts","../src/threads/tools/worker/constants.ts","../src/threads/tools/worker/types.ts","../src/threads/tools/context/tools-context.ts","../src/threads/tools/worker/tools-context-worker.ts","../src/threads/main/step.ts","../src/index.ts"],"mappings":";;;;;;;;cA4Ea,YAAA,EAAY,CAAA,CAAA,aAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAoKb,MAAA,GAAS,CAAA,CAAE,KAAK,QAAQ,YAAA;;;UC5OnB,UAAA,SAAmB,MAAM;EACx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AA,CAAa,wBAAA,EAA0B,MAAA,CAAO,GAAA;;;;YAmCpE,qBAAA,CAAsB,QAAA,EAAU,uCAAA;EAAA,UAShC,iBAAA,CAAkB,UAAA,UAAoB,MAAA;EAAA,UAatC,wBAAA;EAAA,cAgBI,QAAA,IAAY,gCAAA;AAAA;;;cC7Kf,kBAAA,SAA2B,QAAA,CAAS,wBAAA;EAAA,gBACxB,KAAA;EAAA,OAEH,KAAA,CAClB,MAAA,EAAQ,OAAA,CAAQ,kBAAA,GAChB,QAAA,EAAU,uCAAA,GACT,OAAA;EAAA,OAqBW,WAAA,CAAY,QAAA,EAAU,uCAAA,GAAwC,SAAA;E/BuM3E;;;E+B7LM,OAAA;EAAA,UAYG,WAAA;AAAA;;;cCtCC,wBAAA,SAAiC,QAAA,CAAS,oBAAA;EAAA,QACvC,aAAA;EhCoDH;;;;;;;;;EAAA,QgCMG,SAAA;EAAA,QAYN,UAAA;EAAA,QAMA,sBAAA;EAAA,YAWI,cAAA;EAML,OAAA;EAcM,UAAA,IAAc,OAAA;EAiBd,mBAAA,CAAoB,KAAA,EAAO,YAAA,CAAa,oBAAA,EAAsB,MAAA,CAAO,GAAA,IAAO,OAAA;EAoD5E,mBAAA,CAAoB,KAAA,EAAO,YAAA,CAAa,oBAAA,EAAsB,MAAA,CAAO,GAAA,IAAO,OAAA;EAuB5E,QAAA,IAAY,OAAA;AAAA;;;cChNd,MAAA"}
|
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{SpanKind as d,SpanStatusCode as ee,context as f,metrics as p,propagation as te,trace as m}from"@opentelemetry/api";import{SeverityNumber as ne,logs as h}from"@opentelemetry/api-logs";import g from"openai";import re from"timestring";import{ATTR_GEN_AI_INPUT_MESSAGES as ie,ATTR_GEN_AI_OPERATION_NAME as ae,ATTR_GEN_AI_OUTPUT_MESSAGES as oe,ATTR_GEN_AI_OUTPUT_TYPE as se,ATTR_GEN_AI_PROVIDER_NAME as ce,ATTR_GEN_AI_REQUEST_CHOICE_COUNT as le,ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY as ue,ATTR_GEN_AI_REQUEST_MAX_TOKENS as de,ATTR_GEN_AI_REQUEST_MODEL as fe,ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY as pe,ATTR_GEN_AI_REQUEST_SEED as me,ATTR_GEN_AI_REQUEST_STOP_SEQUENCES as he,ATTR_GEN_AI_REQUEST_STREAM as ge,ATTR_GEN_AI_REQUEST_TEMPERATURE as _e,ATTR_GEN_AI_REQUEST_TOP_P as ve,ATTR_GEN_AI_RESPONSE_FINISH_REASONS as ye,ATTR_GEN_AI_RESPONSE_ID as be,ATTR_GEN_AI_RESPONSE_MODEL as _,ATTR_GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK as xe,ATTR_GEN_AI_TOKEN_TYPE as Se,ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS as Ce,ATTR_GEN_AI_USAGE_INPUT_TOKENS as we,ATTR_GEN_AI_USAGE_OUTPUT_TOKENS as Te,ATTR_GEN_AI_USAGE_REASONING_OUTPUT_TOKENS as Ee,ATTR_SERVER_ADDRESS as De,ATTR_SERVER_PORT as Oe,EVENT_GEN_AI_CLIENT_INFERENCE_OPERATION_DETAILS as ke,GEN_AI_OPERATION_NAME_VALUE_CHAT as Ae,GEN_AI_OUTPUT_TYPE_VALUE_JSON as je,GEN_AI_OUTPUT_TYPE_VALUE_TEXT as Me,GEN_AI_TOKEN_TYPE_VALUE_INPUT as Ne,GEN_AI_TOKEN_TYPE_VALUE_OUTPUT as Pe,METRIC_GEN_AI_CLIENT_OPERATION_DURATION as Fe,METRIC_GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK as Ie,METRIC_GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK as Le,METRIC_GEN_AI_CLIENT_TOKEN_USAGE as Re}from"@opentelemetry/semantic-conventions/incubating";import{readFile as ze}from"node:fs/promises";const v={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 y=class extends a{},Be=class extends a{};i.config(i.locales.en());const Ve=[y,r];function b(e,t={}){let{preserveErrors:n=Ve,unknownMessage:a=`Unknown error`,unknownCode:o=v.UNKNOWN}=t;if(n?.some(t=>e instanceof t))return e;if(e instanceof i.core.$ZodError)return new y(i.prettifyError(e),e,{code:v.VALIDATION});if(typeof e==`string`)return new y(e,{code:v.UNKNOWN});if(e instanceof Error){if(e.message.includes(`Request timed out`))return new r(`Timeout`,e,{code:v.TIMEOUT});let t=e?.response?.data?.error?.message;return t==null?new y(a,e,{code:o}):new y(t,e,{code:v.SERVER_ERROR})}return new y(a,{code:o,data:e})}function He(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 x=He((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=>S.call(this,e,t,n)):r}catch(t){return S.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function S(e,t,r){let i=r.toString();this.log.DEBUG?.(`Error in method '${i}'`,t);let a=e.useErrorFilter?b(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 Ue({thread:e,...t}){let n=w({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 We({thread:e,...t}){let n=w({thread:e,...t});return e.process.cache[n]}function C({thread:e,...t}){let n=w({thread:e,...t});delete e.process.cache[n]}function w({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function T(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 Ge(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function Ke(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 E(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await qe(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function qe(e,t,n={}){let r=Ke(e).map(async n=>await E(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 D=He((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),b(e)}):t}catch(e){let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),b(e)}}},{});i.config(i.locales.en());const Je=i.object({captureMessageContentInTraces:i.optional(i.boolean()),captureMessageContentInLogs:i.optional(i.boolean()),carrier:i.optional(i.record(i.string(),i.any()))});i.config(i.locales.en());const O=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),Ye=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:O}),i.object({role:i.literal(`user`),content:O,name:i.optional(O.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(O),tool_calls:i.optional(i.array(i.object({id:O.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:O.check(i.minLength(1)),arguments:O.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:O,name:i.optional(O.check(i.minLength(1),i.maxLength(64))),tool_call_id:O.check(i.minLength(1))})]),Xe=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)))}))}))}),Ze=i.object({provider:O.check(i.minLength(1)),model:O.check(i.minLength(1)),accountKey:k(O),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(O),userMessage:i.optional(O),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(O),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)),chunkMode:i.optional(i.enum([`length`,`phrase`])),minPhraseLength:k(i.coerce.number().check(i.overwrite(e=>e===0?20:e),i.int(),i.gte(1),i.lte(1024)))})),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(Ye)),historyCode:i.optional(i.array(Ye)),functionsList:i.array(i.object({name:O.check(i.minLength(1),i.maxLength(64)),description:i.optional(O.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(Xe,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()})),otel:i.optional(Je),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:O.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 Ze.parse(e)}};j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],M.prototype,`resolveDataIn`,null);const N=`llm_cmp`,P={cancel:`st_cancel`},Qe={cancel:`cnl_cancel`};var F=class e extends M{static class=`cncl_ctrl`;static async start(e){await T(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await E(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local({name:Qe.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=We({thread:this.thread});n?n.abort(new Be(`Request cancelled`)):this.process.enqueueAndRun({name:P.cancel,thread:e})}};j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`initialize`,null),j([x,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([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onAdjustOnCancel`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onPrepareHistory`,null),j([x,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}})}},$e=class extends z{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),C({thread:this.thread})}},et=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);C({thread:this.thread})}};function tt({thread:e,isStreaming:t}){return t?new et(e):new $e(e)}var nt=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var rt=class extends nt{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 y(`Chat completion request failed`,e,{code:v.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{accountKey:e}=this.data;return new g({baseURL:this.openAiBaseUrl,apiKey:`_fake_key_`,defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get([`reporting`,`sessionId`]),...e?{"x-or-account-token-name":e}:void 0},timeout:this.timeoutMs})}get openAiBaseUrl(){return`${this.config.env.OPENAI_API_URL}/${this.data.provider}`}get stopSequences(){return(this.data.stop??[]).map(e=>e.token).filter(e=>e.length>0)}get timeoutMs(){let e=re(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`,[])],rt.prototype,`timeoutMs`,null);function it(e,t,n){return{[ae]:Ae,[ce]:t,[fe]:e.model,...ct(n)}}function at(e){let t={[ge]:!!e.stream,[se]:e.response_format==null||e.response_format.type===`text`?Me:je};return e.n!=null&&e.n>1&&(t[le]=e.n),e.seed!=null&&(t[me]=e.seed),e.frequency_penalty!=null&&(t[ue]=e.frequency_penalty),e.max_completion_tokens==null?e.max_tokens!=null&&(t[de]=e.max_tokens):t[de]=e.max_completion_tokens,e.presence_penalty!=null&&(t[pe]=e.presence_penalty),e.stop!=null&&(t[he]=Array.isArray(e.stop)?e.stop:[e.stop]),e.temperature!=null&&(t[_e]=e.temperature),e.top_p!=null&&(t[ve]=e.top_p),t}function B(e){return{[ye]:e.choices.map(e=>e.finish_reason).filter(e=>e!=null),[be]:e.id,[_]:e.model,...e.usage&&{[we]:e.usage.prompt_tokens,[Te]:e.usage.completion_tokens},...e.usage?.prompt_tokens_details&&{[Ce]:e.usage.prompt_tokens_details.cached_tokens},...e.usage?.completion_tokens_details&&{[Ee]:e.usage.completion_tokens_details.reasoning_tokens}}}function ot(e,t){return{[ie]:e.messages.map(e=>({...e,role:e.role,parts:Array.isArray(e.content)?e.content.map(e=>{switch(e.type){case`text`:return{type:`text`,content:e.text};case`input_audio`:return{type:`blob`,modality:`audio`,mime_type:`audio/${e.input_audio.format}`,content:e.input_audio.data};case`file`:return{type:`file`,modality:`document`,file_id:e.file.file_id};case`image_url`:try{return new URL(e.image_url.url),{type:`uri`,modality:`image`,uri:e.image_url.url}}catch{return{type:`blob`,modality:`image`,content:e.image_url.url}}case`refusal`:return{type:`refusal`,content:e.refusal};default:return e}}):[{type:`text`,content:e.content}]})),[oe]:t.choices.map(e=>({role:e.message.role,finish_reason:e.finish_reason===`tool_calls`||e.finish_reason===`function_call`?`tool_call`:e.finish_reason,parts:[...e.message.content?[{type:`text`,content:e.message.content}]:[],(e.message.tool_calls??[]).map(e=>e.type===`function`?{type:`tool_call`,id:e.id,name:e.function.name,arguments:e.function.arguments}:e)]}))}}const st={"https:":443,"http:":80};function ct(e){if(!e)return;let t;try{t=new URL(e)}catch{return}return{[De]:t.hostname,[Oe]:t.port?Number(t.port):st[t.protocol]}}function lt({logger:e,spanAttributes:t,config:n,request:r,context:i}){return function(a){e.emit({context:i,eventName:ke,severityNumber:ne.INFO,attributes:{...t,...B(a),...n?.captureMessageContentInLogs&&ot(r,a)}})}}function ut(e){return e.createHistogram(Fe,{description:`GenAI operation duration`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function dt({meter:e,attributes:t,context:n,startTime:r=Date.now()}){let i=ut(e);return function(e,a){i.record(a??(Date.now()-r)/1e3,{...t,[_]:e??`unknown`},n)}}function ft(e){return e.createHistogram(Ie,{description:`GenAI operation time per output chunk`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function pt({meter:e,attributes:t,context:n}){return function(r,i){ft(e).record(r,{...t,[_]:i??`unknown`},n)}}function mt(e){return e.createHistogram(Le,{description:`GenAI operation time to first chunk`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function ht({meter:e,attributes:t,context:n}){return function(r,i){mt(e).record(r,{...t,[_]:i??`unknown`},n)}}function gt(e){return e.createHistogram(Re,{description:`Measures number of input and output tokens used`,unit:`{token}`,advice:{explicitBucketBoundaries:[1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864]}})}function _t({meter:e,context:t,attributes:n}){let r=gt(e);return function(e,i){e?.prompt_tokens!=null&&r.record(e.prompt_tokens,{...n,[_]:i??`unknown`,[Se]:Ne},t),e?.completion_tokens!=null&&r.record(e.completion_tokens,{...n,[_]:i??`unknown`,[Se]:Pe},t)}}var vt=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{}}};function yt(e){if(!(!e||Object.keys(e).length===0))return te.extract(f.active(),e)}async function*bt(e,t,n,r,i,a,o){let s=[],c={},l,u,d=0,p=Date.now();try{let te=await e,ne=m.setSpan(f.active(),t),h=f.bind(ne,te),g={},re=new vt(g);for await(let e of h){re.reduceChunk(e);let t=e.choices.map(e=>e?.finish_reason).filter(e=>e!=null);if(s.push(...t),Object.assign(c,B(e)),e.model&&(l??=e.model),e.usage&&(u??=e.usage),d===0){let e=(Date.now()-p)/1e3;a(e,l),c[xe]=e}else o((Date.now()-p)/1e3,l);d+=1,p=Date.now(),yield e}Object.assign(c,{[ye]:s}),t.setAttributes(c),t.setStatus({code:ee.OK}),n(g),r(l),i(u,l)}catch(e){let n;throw n=e instanceof Error?e:Error(typeof e==`string`?e:String(e)),t.recordException(n),t.setStatus({code:ee.ERROR,message:n.message}),e}finally{t.end()}}async function xt(e,t,n,r,i){try{let a=await e,o=B(a);return t.setAttributes(o),n(a),r(a.model),i(a.usage,a.model),a}finally{t.end()}}function St({tracer:e,request:t,provider:n,openAiBaseURL:r,config:i}){let a=it(t,n,r),o={...a,...at(t)},s=e.startSpan(`${o[ae]} ${o[fe]}`,{kind:d.CLIENT,attributes:o},yt(i?.carrier));return{span:s,spanContext:m.setSpan(f.active(),s),commonAttributes:a,spanAttributes:o}}var V=class extends rt{get tracer(){return m.getTracer(`create-llm-chat-completion-step`)}get meter(){return p.getMeter(`create-llm-chat-completion-step`)}get logger(){return h.getLogger(`create-llm-chat-completion-step`)}async doChatCompletionRequest(...e){let t=e[0],n,r=this.data.otel??{};try{n=St({tracer:this.tracer,request:t,provider:this.data.provider,openAiBaseURL:this.openAiBaseUrl,config:r})}catch(t){return this.log.WARN?.(`Unexpected error starting OpenTelemetry span:`,t),super.doChatCompletionRequest(...e)}let{span:i,spanContext:a,commonAttributes:o,spanAttributes:s}=n;this.log.DEBUG?.(`OpenTelemetry span started`,{spanAttributes:s});let c=lt({logger:this.logger,context:a,spanAttributes:s,config:r,request:t}),l=dt({meter:this.meter,attributes:o,context:a}),u=_t({meter:this.meter,attributes:o,context:a}),d=super.doChatCompletionRequest(...e);return t.stream?bt(d,i,c,l,u,ht({meter:this.meter,attributes:o,context:a}),pt({meter:this.meter,attributes:o,context:a})):xt(d,i,c,l,u)}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`tracer`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`meter`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`logger`,null);const Ct=[`requestInit`];var H=class extends M{static class=`rqst_ctx`;static async start(e){return await T(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 V(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return tt({thread:this.thread,isStreaming:e})}async runStep(){l(Ct);let e=Ue({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{C({thread:this.thread})}}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],H.prototype,`openAiService`,null),j([c({tags:Ct}),A(`design:type`,Object),A(`design:paramtypes`,[])],H.prototype,`responseHandler`,null),j([x,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 T(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([D,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 T(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 y(`Only function tools are supported at the moment`,{code:v.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 y(`${U.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${U.name} thread ended with an error`,unknownCode:v.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 y(`Unknown tool call with id '${e}'`,{code:v.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([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onCallTool`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`onLastToolCallReceived`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onWorkerThreadEnd`,null);const wt=[`toolsInit`];var K=class extends M{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(wt),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 y(`${H.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${H.name} thread ended with an error`,unknownCode:v.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return this.cancel();if(t.status!==`ok`)throw new y(`Unexpected end of the ${H.name} thread`,{code:v.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 y(`${G.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${G.name} thread ended with an error`,unknownCode:v.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return;let{status:n,messages:r}=t;if(n!==`ok`||!Array.isArray(r))throw new y(`Unexpected end of the ${G.name} thread`,{code:v.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 y(`Missing chat completion content to return value`,{code:v.STEP_LOGIC_ISSUE});this.end({status:`ok`,chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Object)],K.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],K.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],K.prototype,`onRequestContextEnd`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],K.prototype,`onToolsContextEnd`,null),j([c({tags:wt}),A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],K.prototype,`initToolsContextThread`,null);var q=class extends K{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([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],q.prototype,`onHandleContent`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],q.prototype,`onAllContentReceived`,null);const Tt=new Set([`mr`,`mrs`,`ms`,`dr`,`prof`,`sr`,`jr`,`vs`,`etc`,`eg`,`ie`,`us`,`uk`,`st`,`approx`,`dept`,`est`]),Et=/[.!?]["')\]]*(?=\s|$)/g,Dt=/[;:\u{2014}\u{2013}]["')\]]*(?=\s|$)|(?:\r?\n)/gu,Ot=/,["')\]]*(?=\s|$)/g;function kt(e,t){let{maxLength:n,minLength:r}=t;if(n<1)throw Error(`maxLength must be >= 1`);let i=Math.max(1,Math.min(r,n)),a=Math.min(n,Math.max(i*2,i)),o=[],s=0;for(;s<e.length;){let t=e.slice(s),r=J(t,Et,i,jt);if(r!=null){o.push(t.slice(0,r)),s+=r;continue}let c=J(t,Dt,i);if(c!=null){o.push(t.slice(0,c)),s+=c;continue}let l=J(t,Ot,a);if(l!=null){o.push(t.slice(0,l)),s+=l;continue}if(t.length>=n){let e=At(t,n),r=e??n;if(e!=null)for(;r>0&&/\s/.test(t[r-1]);)--r;r<1&&(r=n),o.push(t.slice(0,r)),s+=r;continue}break}return{phrases:o.filter(e=>e.length>0),remainder:e.slice(s)}}function At(e,t){let n=Math.min(e.length,t);for(let t=n-1;t>=1;--t)if(/\s/.test(e[t]))return t+1}function J(e,t,n,r){let i=new RegExp(t.source,t.flags),a;for(;(a=i.exec(e))!=null;){let t=a.index+a[0].length;if(!(t<n)&&!(r!=null&&!r(a,e)))return t}}function jt(e,t){if(e[0]?.at(0)!==`.`)return!0;let n=t[e.index-1],r=t[e.index+1];if(n!=null&&/\d/.test(n)&&r!=null&&/\d/.test(r))return!1;let i=t.slice(0,e.index).match(/((?:[A-Za-z]\.)+[A-Za-z]|[A-Za-z]+)$/);if(i==null)return!0;let a=i[1].toLowerCase().replaceAll(`.`,``);return!(Tt.has(a)||/^[A-Za-z]$/.test(i[1]))}i.config(i.locales.en());const Mt=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())}))}))})})}),Nt={chunk:`chunk`};var Y=class extends M{static class=`str_wrk`;static async start(e,t){await T(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return Ge(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 y(`Stream worker thread state must contain an event to process`,{code:v.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.(`Executing stream worker thread`,e),this.exitStep(Nt.chunk,{chunk:e})}};j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],Y.prototype,`runStep`,null);const X=[`streamInit`];var Z=class e extends K{static class=`cnt_cxt_str`;static getInitialState(){return{class:this.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1,holdback:``}}onToolCall(e){this.log.DEBUG?.(`Tool call received`,e),this.callFunction(e)}scheduleNextContentEvent(){this.promoteDebouncedHoldback();let e=this.state.events[0];e==null||!this.isEventReady(e)||`lastChunkReceived`in e||this.tryRunWorker()}addContentToQueue(e){if(this.isPhraseChunkMode){this.addPhraseContentToQueue(e);return}this.addLengthContentToQueue(e)}addLengthContentToQueue(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.enqueueChunk(e.slice(0,this.maxChunkContentLength)),e=e.slice(this.maxChunkContentLength)}addPhraseContentToQueue(e){let t=this.state.events.at(-1);if(t!=null&&`lastChunkReceived`in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}this.promoteDebouncedHoldback(),this.state.holdback=`${this.state.holdback}${e}`,this.state.holdbackTs??=Date.now();let{phrases:n,remainder:r}=kt(this.state.holdback,{maxLength:this.maxChunkContentLength,minLength:this.minPhraseLength});for(let e of n)this.enqueueChunk(e);this.state.holdback=r,r||(this.state.holdbackTs=void 0)}promoteDebouncedHoldback(){this.isPhraseChunkMode&&(!this.state.holdback||this.state.holdbackTs==null||Date.now()-this.state.holdbackTs<this.debounceDuration||this.flushHoldback())}flushHoldback(){let e=this.state.holdback;if(!e)return;this.state.holdback=``,this.state.holdbackTs=void 0;let{phrases:t,remainder:n}=kt(e,{maxLength:this.maxChunkContentLength,minLength:1});for(let e of t)this.enqueueChunk(e);for(e=n;e;){let t=e.length>this.maxChunkContentLength?At(e,this.maxChunkContentLength)??this.maxChunkContentLength:e.length;if(t<e.length){let n=t;for(;n>0&&/\s/.test(e[n-1]);)--n;n>=1&&(t=n)}this.enqueueChunk(e.slice(0,t)),e=e.slice(t)}}enqueueChunk(e){if(!e)return;this.state.index+=1;let t={content:e,index:this.state.index,ts:Date.now()};this.log.DEBUG?.(`Add new event into the queue`,t),this.state.events.push(t)}tryRunWorker(){if(this.promoteDebouncedHoldback(),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;if(this.isPhraseChunkMode)return!0;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 y(`Missing completion message in chat completion result`,{code:v.STEP_LOGIC_ISSUE});this.complete()}}onAllContentReceived(){if(this.log.DEBUG?.(`All LLM response chunks were received. No more response chunks expected`),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.flushHoldback(),this.state.events.length>0){let e=this.state.events.at(-1);e.last=!0}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}get isPhraseChunkMode(){return this.data.streamSettings?.chunkMode===`phrase`}get minPhraseLength(){return this.data.streamSettings?.minPhraseLength??20}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 b(t,{unknownMessage:`${Y.name} thread ended with an error`});t?.status!==`cancel`&&this.tryRunWorker()}onHandleContent(e){let t=Mt.parse(e).params.content;new vt(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([x,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([c({tags:X}),A(`design:type`,Boolean),A(`design:paramtypes`,[])],Z.prototype,`isPhraseChunkMode`,null),j([c({tags:X}),A(`design:type`,Number),A(`design:paramtypes`,[])],Z.prototype,`minPhraseLength`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],Z.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onWorkerThreadEnd`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onHandleContent`,null);const Pt=e(import.meta.url);async function Ft(e){if(typeof e!=`string`||!/^\d+\.\d+\.\d+$/.test(e))throw Error(`Invalid minimum version format: ${e}`);let t=Pt.resolve(`@onereach/flow-sdk/package.json`);if(!t)throw Error(`Could not find package.json for @onereach/flow-sdk`);let n=await ze(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:q)}async start(e){let t=this.Constructor.getInitialState();await T(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 E(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 y(`Missing chat completion result, but request is marked as processed`,{code:v.STEP_LOGIC_ISSUE});let i=n.choices?.[0]?.message;if(i==null)throw new y(`Missing completion message in chat completion result`,{code:v.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 y(`Reached limit of tokens for LLM completion response`,{code:v.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([qe(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 y(`Merge-field type has to be "Thread" if you are using functions`,{code:v.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 Ft(`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 y(`ContentContext thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`ContentContext thread ended with an error`,unknownCode:v.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 y(`Unexpected end of the ContentContext thread`,{code:v.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 b(t,{unknownMessage:`${L.name} thread ended with an error`,unknownCode:v.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([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],$.prototype,`runStep`,null),j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`initialize`,null),j([x({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onContentContextEnd`,null),j([x({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onHistoryContextEnd`,null),j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const It={[L.class]:L,[H.class]:H,[G.class]:G,[U.class]:U,[q.class]:q,[Z.class]:Z,[Y.class]:Y,[F.class]:F};export{It 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{SpanKind as d,SpanStatusCode as ee,context as f,metrics as p,propagation as te,trace as m}from"@opentelemetry/api";import{SeverityNumber as ne,logs as h}from"@opentelemetry/api-logs";import g from"openai";import re from"timestring";import{ATTR_GEN_AI_INPUT_MESSAGES as ie,ATTR_GEN_AI_OPERATION_NAME as ae,ATTR_GEN_AI_OUTPUT_MESSAGES as oe,ATTR_GEN_AI_OUTPUT_TYPE as se,ATTR_GEN_AI_PROVIDER_NAME as ce,ATTR_GEN_AI_REQUEST_CHOICE_COUNT as le,ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY as ue,ATTR_GEN_AI_REQUEST_MAX_TOKENS as de,ATTR_GEN_AI_REQUEST_MODEL as fe,ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY as pe,ATTR_GEN_AI_REQUEST_SEED as me,ATTR_GEN_AI_REQUEST_STOP_SEQUENCES as he,ATTR_GEN_AI_REQUEST_STREAM as ge,ATTR_GEN_AI_REQUEST_TEMPERATURE as _e,ATTR_GEN_AI_REQUEST_TOP_P as ve,ATTR_GEN_AI_RESPONSE_FINISH_REASONS as ye,ATTR_GEN_AI_RESPONSE_ID as be,ATTR_GEN_AI_RESPONSE_MODEL as _,ATTR_GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK as xe,ATTR_GEN_AI_TOKEN_TYPE as Se,ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS as Ce,ATTR_GEN_AI_USAGE_INPUT_TOKENS as we,ATTR_GEN_AI_USAGE_OUTPUT_TOKENS as Te,ATTR_GEN_AI_USAGE_REASONING_OUTPUT_TOKENS as Ee,ATTR_SERVER_ADDRESS as De,ATTR_SERVER_PORT as Oe,EVENT_GEN_AI_CLIENT_INFERENCE_OPERATION_DETAILS as ke,GEN_AI_OPERATION_NAME_VALUE_CHAT as Ae,GEN_AI_OUTPUT_TYPE_VALUE_JSON as je,GEN_AI_OUTPUT_TYPE_VALUE_TEXT as Me,GEN_AI_TOKEN_TYPE_VALUE_INPUT as Ne,GEN_AI_TOKEN_TYPE_VALUE_OUTPUT as Pe,METRIC_GEN_AI_CLIENT_OPERATION_DURATION as Fe,METRIC_GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK as Ie,METRIC_GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK as Le,METRIC_GEN_AI_CLIENT_TOKEN_USAGE as Re}from"@opentelemetry/semantic-conventions/incubating";import{readFile as ze}from"node:fs/promises";const v={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 y=class extends a{},Be=class extends a{};i.config(i.locales.en());const Ve=[y,r];function b(e,t={}){let{preserveErrors:n=Ve,unknownMessage:a=`Unknown error`,unknownCode:o=v.UNKNOWN}=t;if(n?.some(t=>e instanceof t))return e;if(e instanceof i.core.$ZodError)return new y(i.prettifyError(e),e,{code:v.VALIDATION});if(typeof e==`string`)return new y(e,{code:v.UNKNOWN});if(e instanceof Error){if(e.message.includes(`Request timed out`))return new r(`Timeout`,e,{code:v.TIMEOUT});let t=e?.response?.data?.error?.message;return t==null?new y(a,e,{code:o}):new y(t,e,{code:v.SERVER_ERROR})}return new y(a,{code:o,data:e})}function He(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 x=He((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=>S.call(this,e,t,n)):r}catch(t){return S.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function S(e,t,r){let i=r.toString();this.log.DEBUG?.(`Error in method '${i}'`,t);let a=e.useErrorFilter?b(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 Ue({thread:e,...t}){let n=w({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 We({thread:e,...t}){let n=w({thread:e,...t});return e.process.cache[n]}function C({thread:e,...t}){let n=w({thread:e,...t});delete e.process.cache[n]}function w({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function T(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 Ge(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function Ke(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 E(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await qe(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function qe(e,t,n={}){let r=Ke(e).map(async n=>await E(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 D=He((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),b(e)}):t}catch(e){let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),b(e)}}},{});i.config(i.locales.en());const Je=i.object({captureMessageContentInTraces:i.optional(i.boolean()),captureMessageContentInLogs:i.optional(i.boolean()),carrier:i.optional(i.record(i.string(),i.any()))});i.config(i.locales.en());const O=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),Ye=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:O}),i.object({role:i.literal(`user`),content:O,name:i.optional(O.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(O),tool_calls:i.optional(i.array(i.object({id:O.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:O.check(i.minLength(1)),arguments:O.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:O,name:i.optional(O.check(i.minLength(1),i.maxLength(64))),tool_call_id:O.check(i.minLength(1))})]),Xe=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)))}))}))}),Ze=i.object({provider:O.check(i.minLength(1)),model:O.check(i.minLength(1)),accountKey:k(O),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(O),userMessage:i.optional(O),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(O),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)),chunkMode:i.optional(i.enum([`length`,`phrase`])),minPhraseLength:k(i.coerce.number().check(i.overwrite(e=>e===0?20:e),i.int(),i.gte(1),i.lte(1024)))})),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(Ye)),historyCode:i.optional(i.array(Ye)),functionsList:i.array(i.object({name:O.check(i.minLength(1),i.maxLength(64)),description:i.optional(O.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(Xe,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()})),otel:i.optional(Je),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:O.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 Ze.parse(e)}};j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],M.prototype,`resolveDataIn`,null);const N=`llm_cmp`,P={cancel:`st_cancel`},Qe={cancel:`cnl_cancel`};var F=class e extends M{static class=`cncl_ctrl`;static async start(e){await T(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await E(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local({name:Qe.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=We({thread:this.thread});n?n.abort(new Be(`Request cancelled`)):this.process.enqueueAndRun({name:P.cancel,thread:e})}};j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],F.prototype,`initialize`,null),j([x,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([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onAdjustOnCancel`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],L.prototype,`onPrepareHistory`,null),j([x,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}})}},$e=class extends z{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),C({thread:this.thread})}},et=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);C({thread:this.thread})}};function tt({thread:e,isStreaming:t}){return t?new et(e):new $e(e)}var nt=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var rt=class extends nt{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 y(`Chat completion request failed`,e,{code:v.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{accountKey:e}=this.data;return new g({baseURL:this.openAiBaseUrl,apiKey:`_fake_key_`,defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get([`reporting`,`sessionId`]),...e?{"x-or-account-token-name":e}:void 0},timeout:this.timeoutMs})}get openAiBaseUrl(){return`${this.config.env.OPENAI_API_URL}/${this.data.provider}`}get stopSequences(){return(this.data.stop??[]).map(e=>e.token).filter(e=>e.length>0)}get timeoutMs(){let e=re(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`,[])],rt.prototype,`timeoutMs`,null);function it(e,t,n){return{[ae]:Ae,[ce]:t,[fe]:e.model,...ct(n)}}function at(e){let t={[ge]:!!e.stream,[se]:e.response_format==null||e.response_format.type===`text`?Me:je};return e.n!=null&&e.n>1&&(t[le]=e.n),e.seed!=null&&(t[me]=e.seed),e.frequency_penalty!=null&&(t[ue]=e.frequency_penalty),e.max_completion_tokens==null?e.max_tokens!=null&&(t[de]=e.max_tokens):t[de]=e.max_completion_tokens,e.presence_penalty!=null&&(t[pe]=e.presence_penalty),e.stop!=null&&(t[he]=Array.isArray(e.stop)?e.stop:[e.stop]),e.temperature!=null&&(t[_e]=e.temperature),e.top_p!=null&&(t[ve]=e.top_p),t}function B(e){return{[ye]:e.choices.map(e=>e.finish_reason).filter(e=>e!=null),[be]:e.id,[_]:e.model,...e.usage&&{[we]:e.usage.prompt_tokens,[Te]:e.usage.completion_tokens},...e.usage?.prompt_tokens_details&&{[Ce]:e.usage.prompt_tokens_details.cached_tokens},...e.usage?.completion_tokens_details&&{[Ee]:e.usage.completion_tokens_details.reasoning_tokens}}}function ot(e,t){return{[ie]:e.messages.map(e=>({...e,role:e.role,parts:Array.isArray(e.content)?e.content.map(e=>{switch(e.type){case`text`:return{type:`text`,content:e.text};case`input_audio`:return{type:`blob`,modality:`audio`,mime_type:`audio/${e.input_audio.format}`,content:e.input_audio.data};case`file`:return{type:`file`,modality:`document`,file_id:e.file.file_id};case`image_url`:try{return new URL(e.image_url.url),{type:`uri`,modality:`image`,uri:e.image_url.url}}catch{return{type:`blob`,modality:`image`,content:e.image_url.url}}case`refusal`:return{type:`refusal`,content:e.refusal};default:return e}}):[{type:`text`,content:e.content}]})),[oe]:t.choices.map(e=>({role:e.message.role,finish_reason:e.finish_reason===`tool_calls`||e.finish_reason===`function_call`?`tool_call`:e.finish_reason,parts:[...e.message.content?[{type:`text`,content:e.message.content}]:[],(e.message.tool_calls??[]).map(e=>e.type===`function`?{type:`tool_call`,id:e.id,name:e.function.name,arguments:e.function.arguments}:e)]}))}}const st={"https:":443,"http:":80};function ct(e){if(!e)return;let t;try{t=new URL(e)}catch{return}return{[De]:t.hostname,[Oe]:t.port?Number(t.port):st[t.protocol]}}function lt({logger:e,spanAttributes:t,config:n,request:r,context:i}){return function(a){e.emit({context:i,eventName:ke,severityNumber:ne.INFO,attributes:{...t,...B(a),...n?.captureMessageContentInLogs&&ot(r,a)}})}}function ut(e){return e.createHistogram(Fe,{description:`GenAI operation duration`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function dt({meter:e,attributes:t,context:n,startTime:r=Date.now()}){let i=ut(e);return function(e,a){i.record(a??(Date.now()-r)/1e3,{...t,[_]:e??`unknown`},n)}}function ft(e){return e.createHistogram(Ie,{description:`GenAI operation time per output chunk`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function pt({meter:e,attributes:t,context:n}){return function(r,i){ft(e).record(r,{...t,[_]:i??`unknown`},n)}}function mt(e){return e.createHistogram(Le,{description:`GenAI operation time to first chunk`,unit:`s`,advice:{explicitBucketBoundaries:[.01,.02,.04,.08,.16,.32,.64,1.28,2.56,5.12,10.24,20.48,40.96,81.92]}})}function ht({meter:e,attributes:t,context:n}){return function(r,i){mt(e).record(r,{...t,[_]:i??`unknown`},n)}}function gt(e){return e.createHistogram(Re,{description:`Measures number of input and output tokens used`,unit:`{token}`,advice:{explicitBucketBoundaries:[1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864]}})}function _t({meter:e,context:t,attributes:n}){let r=gt(e);return function(e,i){e?.prompt_tokens!=null&&r.record(e.prompt_tokens,{...n,[_]:i??`unknown`,[Se]:Ne},t),e?.completion_tokens!=null&&r.record(e.completion_tokens,{...n,[_]:i??`unknown`,[Se]:Pe},t)}}var vt=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{}}};function yt(e){if(!(!e||Object.keys(e).length===0))return te.extract(f.active(),e)}async function*bt(e,t,n,r,i,a,o){let s=[],c={},l,u,d=0,p=Date.now();try{let te=await e,ne=m.setSpan(f.active(),t),h=f.bind(ne,te),g={},re=new vt(g);for await(let e of h){re.reduceChunk(e);let t=e.choices.map(e=>e?.finish_reason).filter(e=>e!=null);if(s.push(...t),Object.assign(c,B(e)),e.model&&(l??=e.model),e.usage&&(u??=e.usage),d===0){let e=(Date.now()-p)/1e3;a(e,l),c[xe]=e}else o((Date.now()-p)/1e3,l);d+=1,p=Date.now(),yield e}Object.assign(c,{[ye]:s}),t.setAttributes(c),t.setStatus({code:ee.OK}),n(g),r(l),i(u,l)}catch(e){let n;throw n=e instanceof Error?e:Error(typeof e==`string`?e:String(e)),t.recordException(n),t.setStatus({code:ee.ERROR,message:n.message}),e}finally{t.end()}}async function xt(e,t,n,r,i){try{let a=await e,o=B(a);return t.setAttributes(o),n(a),r(a.model),i(a.usage,a.model),a}finally{t.end()}}function St({tracer:e,request:t,provider:n,openAiBaseURL:r,config:i}){let a=it(t,n,r),o={...a,...at(t)},s=e.startSpan(`${o[ae]} ${o[fe]}`,{kind:d.CLIENT,attributes:o},yt(i?.carrier));return{span:s,spanContext:m.setSpan(f.active(),s),commonAttributes:a,spanAttributes:o}}var V=class extends rt{get tracer(){return m.getTracer(`create-llm-chat-completion-step`)}get meter(){return p.getMeter(`create-llm-chat-completion-step`)}get logger(){return h.getLogger(`create-llm-chat-completion-step`)}async doChatCompletionRequest(...e){let t=e[0],n,r=this.data.otel??{};try{n=St({tracer:this.tracer,request:t,provider:this.data.provider,openAiBaseURL:this.openAiBaseUrl,config:r})}catch(t){return this.log.WARN?.(`Unexpected error starting OpenTelemetry span:`,t),super.doChatCompletionRequest(...e)}let{span:i,spanContext:a,commonAttributes:o,spanAttributes:s}=n;this.log.DEBUG?.(`OpenTelemetry span started`,{spanAttributes:s});let c=lt({logger:this.logger,context:a,spanAttributes:s,config:r,request:t}),l=dt({meter:this.meter,attributes:o,context:a}),u=_t({meter:this.meter,attributes:o,context:a}),d=super.doChatCompletionRequest(...e);return t.stream?bt(d,i,c,l,u,ht({meter:this.meter,attributes:o,context:a}),pt({meter:this.meter,attributes:o,context:a})):xt(d,i,c,l,u)}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`tracer`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`meter`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`logger`,null);const Ct=[`requestInit`];var H=class extends M{static class=`rqst_ctx`;static async start(e){return await T(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:`developer`,content:await this.data.stateSettings?.stateFunction()};if(e.at(0)?.role===`system`)t.splice(1,0,n);else 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 V(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return tt({thread:this.thread,isStreaming:e})}async runStep(){l(Ct);let e=Ue({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{C({thread:this.thread})}}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],H.prototype,`openAiService`,null),j([c({tags:Ct}),A(`design:type`,Object),A(`design:paramtypes`,[])],H.prototype,`responseHandler`,null),j([x,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 T(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([D,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 T(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 y(`Only function tools are supported at the moment`,{code:v.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 y(`${U.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${U.name} thread ended with an error`,unknownCode:v.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 y(`Unknown tool call with id '${e}'`,{code:v.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([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onCallTool`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],G.prototype,`onLastToolCallReceived`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],G.prototype,`onWorkerThreadEnd`,null);const wt=[`toolsInit`];var K=class extends M{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(wt),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 y(`${H.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${H.name} thread ended with an error`,unknownCode:v.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return this.cancel();if(t.status!==`ok`)throw new y(`Unexpected end of the ${H.name} thread`,{code:v.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 y(`${G.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${G.name} thread ended with an error`,unknownCode:v.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return;let{status:n,messages:r}=t;if(n!==`ok`||!Array.isArray(r))throw new y(`Unexpected end of the ${G.name} thread`,{code:v.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 y(`Missing chat completion content to return value`,{code:v.STEP_LOGIC_ISSUE});this.end({status:`ok`,chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Object)],K.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],K.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],K.prototype,`onRequestContextEnd`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],K.prototype,`onToolsContextEnd`,null),j([c({tags:wt}),A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],K.prototype,`initToolsContextThread`,null);var q=class extends K{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([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],q.prototype,`onHandleContent`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],q.prototype,`onAllContentReceived`,null);const Tt=new Set([`mr`,`mrs`,`ms`,`dr`,`prof`,`sr`,`jr`,`vs`,`etc`,`eg`,`ie`,`us`,`uk`,`st`,`approx`,`dept`,`est`]),Et=/[.!?]["')\]]*(?=\s|$)/g,Dt=/[;:\u{2014}\u{2013}]["')\]]*(?=\s|$)|(?:\r?\n)/gu,Ot=/,["')\]]*(?=\s|$)/g;function kt(e,t){let{maxLength:n,minLength:r}=t;if(n<1)throw Error(`maxLength must be >= 1`);let i=Math.max(1,Math.min(r,n)),a=Math.min(n,Math.max(i*2,i)),o=[],s=0;for(;s<e.length;){let t=e.slice(s),r=J(t,Et,i,jt);if(r!=null){o.push(t.slice(0,r)),s+=r;continue}let c=J(t,Dt,i);if(c!=null){o.push(t.slice(0,c)),s+=c;continue}let l=J(t,Ot,a);if(l!=null){o.push(t.slice(0,l)),s+=l;continue}if(t.length>=n){let e=At(t,n),r=e??n;if(e!=null)for(;r>0&&/\s/.test(t[r-1]);)--r;r<1&&(r=n),o.push(t.slice(0,r)),s+=r;continue}break}return{phrases:o.filter(e=>e.length>0),remainder:e.slice(s)}}function At(e,t){let n=Math.min(e.length,t);for(let t=n-1;t>=1;--t)if(/\s/.test(e[t]))return t+1}function J(e,t,n,r){let i=new RegExp(t.source,t.flags),a;for(;(a=i.exec(e))!=null;){let t=a.index+a[0].length;if(!(t<n)&&!(r!=null&&!r(a,e)))return t}}function jt(e,t){if(e[0]?.at(0)!==`.`)return!0;let n=t[e.index-1],r=t[e.index+1];if(n!=null&&/\d/.test(n)&&r!=null&&/\d/.test(r))return!1;let i=t.slice(0,e.index).match(/((?:[A-Za-z]\.)+[A-Za-z]|[A-Za-z]+)$/);if(i==null)return!0;let a=i[1].toLowerCase().replaceAll(`.`,``);return!(Tt.has(a)||/^[A-Za-z]$/.test(i[1]))}i.config(i.locales.en());const Mt=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())}))}))})})}),Nt={chunk:`chunk`};var Y=class extends M{static class=`str_wrk`;static async start(e,t){await T(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return Ge(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 y(`Stream worker thread state must contain an event to process`,{code:v.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.(`Executing stream worker thread`,e),this.exitStep(Nt.chunk,{chunk:e})}};j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],Y.prototype,`runStep`,null);const X=[`streamInit`];var Z=class e extends K{static class=`cnt_cxt_str`;static getInitialState(){return{class:this.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1,holdback:``}}onToolCall(e){this.log.DEBUG?.(`Tool call received`,e),this.callFunction(e)}scheduleNextContentEvent(){this.promoteDebouncedHoldback();let e=this.state.events[0];e==null||!this.isEventReady(e)||`lastChunkReceived`in e||this.tryRunWorker()}addContentToQueue(e){if(this.isPhraseChunkMode){this.addPhraseContentToQueue(e);return}this.addLengthContentToQueue(e)}addLengthContentToQueue(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.enqueueChunk(e.slice(0,this.maxChunkContentLength)),e=e.slice(this.maxChunkContentLength)}addPhraseContentToQueue(e){let t=this.state.events.at(-1);if(t!=null&&`lastChunkReceived`in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}this.promoteDebouncedHoldback(),this.state.holdback=`${this.state.holdback}${e}`,this.state.holdbackTs??=Date.now();let{phrases:n,remainder:r}=kt(this.state.holdback,{maxLength:this.maxChunkContentLength,minLength:this.minPhraseLength});for(let e of n)this.enqueueChunk(e);this.state.holdback=r,r||(this.state.holdbackTs=void 0)}promoteDebouncedHoldback(){this.isPhraseChunkMode&&(!this.state.holdback||this.state.holdbackTs==null||Date.now()-this.state.holdbackTs<this.debounceDuration||this.flushHoldback())}flushHoldback(){let e=this.state.holdback;if(!e)return;this.state.holdback=``,this.state.holdbackTs=void 0;let{phrases:t,remainder:n}=kt(e,{maxLength:this.maxChunkContentLength,minLength:1});for(let e of t)this.enqueueChunk(e);for(e=n;e;){let t=e.length>this.maxChunkContentLength?At(e,this.maxChunkContentLength)??this.maxChunkContentLength:e.length;if(t<e.length){let n=t;for(;n>0&&/\s/.test(e[n-1]);)--n;n>=1&&(t=n)}this.enqueueChunk(e.slice(0,t)),e=e.slice(t)}}enqueueChunk(e){if(!e)return;this.state.index+=1;let t={content:e,index:this.state.index,ts:Date.now()};this.log.DEBUG?.(`Add new event into the queue`,t),this.state.events.push(t)}tryRunWorker(){if(this.promoteDebouncedHoldback(),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;if(this.isPhraseChunkMode)return!0;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 y(`Missing completion message in chat completion result`,{code:v.STEP_LOGIC_ISSUE});this.complete()}}onAllContentReceived(){if(this.log.DEBUG?.(`All LLM response chunks were received. No more response chunks expected`),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.flushHoldback(),this.state.events.length>0){let e=this.state.events.at(-1);e.last=!0}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}get isPhraseChunkMode(){return this.data.streamSettings?.chunkMode===`phrase`}get minPhraseLength(){return this.data.streamSettings?.minPhraseLength??20}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 b(t,{unknownMessage:`${Y.name} thread ended with an error`});t?.status!==`cancel`&&this.tryRunWorker()}onHandleContent(e){let t=Mt.parse(e).params.content;new vt(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([x,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([c({tags:X}),A(`design:type`,Boolean),A(`design:paramtypes`,[])],Z.prototype,`isPhraseChunkMode`,null),j([c({tags:X}),A(`design:type`,Number),A(`design:paramtypes`,[])],Z.prototype,`minPhraseLength`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],Z.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onWorkerThreadEnd`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],Z.prototype,`onHandleContent`,null);const Pt=e(import.meta.url);async function Ft(e){if(typeof e!=`string`||!/^\d+\.\d+\.\d+$/.test(e))throw Error(`Invalid minimum version format: ${e}`);let t=Pt.resolve(`@onereach/flow-sdk/package.json`);if(!t)throw Error(`Could not find package.json for @onereach/flow-sdk`);let n=await ze(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:q)}async start(e){let t=this.Constructor.getInitialState();await T(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 E(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 y(`Missing chat completion result, but request is marked as processed`,{code:v.STEP_LOGIC_ISSUE});let i=n.choices?.[0]?.message;if(i==null)throw new y(`Missing completion message in chat completion result`,{code:v.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 y(`Reached limit of tokens for LLM completion response`,{code:v.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([qe(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 y(`Merge-field type has to be "Thread" if you are using functions`,{code:v.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 Ft(`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 y(`ContentContext thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`ContentContext thread ended with an error`,unknownCode:v.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 y(`Unexpected end of the ContentContext thread`,{code:v.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 b(t,{unknownMessage:`${L.name} thread ended with an error`,unknownCode:v.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([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],$.prototype,`runStep`,null),j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`initialize`,null),j([x({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onContentContextEnd`,null),j([x({allowHandleError:!0}),A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],$.prototype,`onHistoryContextEnd`,null),j([D,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const It={[L.class]:L,[H.class]:H,[G.class]:G,[U.class]:U,[q.class]:q,[Z.class]:Z,[Y.class]:Y,[F.class]:F};export{It as states,$ as step};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|