@onereach/step-create-chat-completion 0.0.7-rc.63 → 0.0.7-rc.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -39,6 +39,11 @@ declare const dataInSchema: z.ZodMiniObject<{
39
39
  streamSettings: z.ZodMiniOptional<z.ZodMiniObject<{
40
40
  maxBufferLength: z.ZodMiniNumber<unknown>;
41
41
  debounceDuration: z.ZodMiniNumber<unknown>;
42
+ chunkMode: z.ZodMiniOptional<z.ZodMiniEnum<{
43
+ length: "length";
44
+ phrase: "phrase";
45
+ }>>;
46
+ minPhraseLength: z.ZodMiniOptional<z.ZodMiniNumber<unknown>>;
42
47
  }, z.core.$strip>>;
43
48
  customHistory: z.ZodMiniDefault<z.ZodMiniBoolean<boolean>>;
44
49
  historyLength: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
@@ -215,6 +220,8 @@ declare class BaseStep<T extends BaseConfig> extends Step<T> {
215
220
  streamSettings?: {
216
221
  maxBufferLength: number;
217
222
  debounceDuration: number;
223
+ chunkMode?: "length" | "phrase" | undefined;
224
+ minPhraseLength?: number | undefined;
218
225
  } | undefined;
219
226
  historyLength?: number | undefined;
220
227
  historyMode?: "mergefield" | "codeHistory" | undefined;
@@ -477,17 +484,18 @@ declare class SimpleContentContext extends AbstractContentContext<SimpleContentC
477
484
  declare const streamContentContextWorkerClassId = "str_wrk";
478
485
  //#endregion
479
486
  //#region src/threads/content/stream/worker/types.d.ts
480
- type ContentChunk = {
487
+ type ContentChunkEvent = {
481
488
  content: string;
482
489
  index: number;
483
- ts: number;
490
+ ts: number; /** marker for last chunk in the stream */
491
+ last?: boolean;
484
492
  };
485
493
  type StreamContentContextWorkerOut = {
486
- chunk: ContentChunk;
494
+ chunk: ContentChunkEvent;
487
495
  };
488
496
  type StreamContentContextWorkerState = {
489
497
  class: typeof streamContentContextWorkerClassId;
490
- event: ContentChunk;
498
+ event: ContentChunkEvent;
491
499
  };
492
500
  type StreamContentContextWorkerResult = undefined | RESULT<BaseChildConfig>;
493
501
  interface StreamContentContextWorkerConfig extends BaseChildConfig<StreamContentContextWorkerResult> {
@@ -503,11 +511,6 @@ type StreamContentContextHandleContentEvent = IEvent<{
503
511
  content: ChatCompletionChunk;
504
512
  }, ContentContentEventName['handleContent']>;
505
513
  type StreamContentContextEvent = StreamContentContextHandleContentEvent;
506
- type ContentChunkEvent = {
507
- content: string;
508
- index: number;
509
- ts: number;
510
- };
511
514
  type ContentEndEvent = {
512
515
  lastChunkReceived: true;
513
516
  };
@@ -519,6 +522,12 @@ type StreamContentContextState = ContentContextState & {
519
522
  index: number; /** marker for end of response stream */
520
523
  lastChunkReceived: boolean; /** When `true` means worker finished the processing the queue */
521
524
  workerComplete: boolean;
525
+ /**
526
+ * Incomplete text waiting for a phrase boundary (phrase chunk mode only).
527
+ * Promoted to `events` on punctuation, max length, debounce, or end of stream.
528
+ */
529
+ holdback: string; /** Timestamp when the current holdback started accumulating */
530
+ holdbackTs?: number;
522
531
  };
523
532
  interface StreamContentContextConfig extends ContentContextConfig {
524
533
  events: StreamContentContextEvent;
@@ -538,10 +547,22 @@ declare class StreamContentContext extends AbstractContentContext<StreamContentC
538
547
  private scheduleNextContentEvent;
539
548
  /**
540
549
  * Compacts the content into the controller thread's events queue.
541
- * If the content is too long, it splits it into multiple events.
542
- * If the last event is not full, it extends it with the new content.
550
+ * Length mode splits by `maxChunkContentLength`.
551
+ * Phrase mode holds text until TTS-friendly punctuation boundaries.
543
552
  */
544
553
  private addContentToQueue;
554
+ private addLengthContentToQueue;
555
+ private addPhraseContentToQueue;
556
+ /**
557
+ * Promotes aged holdback text into the event queue when debounce has elapsed.
558
+ */
559
+ private promoteDebouncedHoldback;
560
+ /**
561
+ * Flushes any remaining holdback into the queue (end of stream or debounce).
562
+ * Still respects max length, preferring whitespace cuts.
563
+ */
564
+ private flushHoldback;
565
+ private enqueueChunk;
545
566
  /**
546
567
  * Processes the next event in the controller thread's queue.
547
568
  * If the worker thread is busy, it skips processing.
@@ -558,6 +579,8 @@ declare class StreamContentContext extends AbstractContentContext<StreamContentC
558
579
  protected onAllContentReceived(): void;
559
580
  protected get maxChunkContentLength(): number;
560
581
  protected get debounceDuration(): number;
582
+ protected get isPhraseChunkMode(): boolean;
583
+ protected get minPhraseLength(): number;
561
584
  runStep(): Promise<void>;
562
585
  /**
563
586
  * Handler of the worker thread end event.
@@ -572,7 +595,7 @@ declare class StreamContentContext extends AbstractContentContext<StreamContentC
572
595
  //#region src/threads/content/stream/worker/stream-content-context-worker.d.ts
573
596
  declare class StreamContentContextWorker extends BaseStep<StreamContentContextWorkerConfig> {
574
597
  static readonly class = "str_wrk";
575
- static start(thread: IThread<StreamContentContextConfig>, event: ContentChunk): Promise<void>;
598
+ static start(thread: IThread<StreamContentContextConfig>, event: ContentChunkEvent): Promise<void>;
576
599
  static isBusy(thread: IThread<StreamContentContextConfig>): boolean;
577
600
  static getThreadId(stepId: IStepId): IThreadId;
578
601
  runStep(): void;
@@ -732,5 +755,5 @@ declare const states: {
732
755
  cncl_ctrl: typeof CancelContext;
733
756
  };
734
757
  //#endregion
735
- export { type CallToolParameters, type CancelContentEventName, type CancelContextConfig, type CancelContextEvents, type CancelContextState, type ChatCompletionCancelEvent, type ChatCompletionConfig, type ChatCompletionEvent, type ChatCompletionOut, type ChatCompletionState, type ContentChunk, type ContentChunkEvent, type ContentContentEventName, type ContentContextConfig, type ContentContextEvent, type ContentContextHandleContentEvent, type ContentContextOkResult, type ContentContextResult, type ContentContextState, type ContentEndEvent, type ContentEvent, type HistoryContextAppendParameters, type HistoryContextConfig, type HistoryContextEvent, type HistoryContextEventAdjustOnCancel, type HistoryContextEventAppendToHistory, type HistoryContextEventName, type HistoryContextEventPrepareHistory, type HistoryContextState, type ParentEventName, type RequestContextConfig, type RequestContextResult, type RequestContextResultOk, type RequestContextState, type SimpleContentContextConfig, type SimpleContentContextEvent, type SimpleContentContextHandleContentEvent, type SimpleContentContextState, type StreamContentContextConfig, type StreamContentContextEvent, type StreamContentContextHandleContentEvent, type StreamContentContextState, type StreamContentContextWorkerConfig, type StreamContentContextWorkerOut, type StreamContentContextWorkerResult, type StreamContentContextWorkerState, type TasksMap, type ThreadWorkerResult, type ThreadWorkerResultOk, type ToolCallTask, type ToolContextEventName, type ToolsContextConfig, type ToolsContextEvent, type ToolsContextEventCallTool, type ToolsContextEventLastToolReceived, type ToolsContextParsedArguments, type ToolsContextResult, type ToolsContextResultOk, type ToolsContextState, type ToolsContextWorkerConfig, type ToolsContextWorkerLocal, type ToolsContextWorkerMergeField, type ToolsContextWorkerOut, type ToolsContextWorkerState, type historyContextClassId, type historyContextEvents, states, CreateChatCompletionStep as step };
758
+ export { type CallToolParameters, type CancelContentEventName, type CancelContextConfig, type CancelContextEvents, type CancelContextState, type ChatCompletionCancelEvent, type ChatCompletionConfig, type ChatCompletionEvent, type ChatCompletionOut, type ChatCompletionState, type ContentChunkEvent, type ContentContentEventName, type ContentContextConfig, type ContentContextEvent, type ContentContextHandleContentEvent, type ContentContextOkResult, type ContentContextResult, type ContentContextState, type ContentEndEvent, type ContentEvent, type HistoryContextAppendParameters, type HistoryContextConfig, type HistoryContextEvent, type HistoryContextEventAdjustOnCancel, type HistoryContextEventAppendToHistory, type HistoryContextEventName, type HistoryContextEventPrepareHistory, type HistoryContextState, type ParentEventName, type RequestContextConfig, type RequestContextResult, type RequestContextResultOk, type RequestContextState, type SimpleContentContextConfig, type SimpleContentContextEvent, type SimpleContentContextHandleContentEvent, type SimpleContentContextState, type StreamContentContextConfig, type StreamContentContextEvent, type StreamContentContextHandleContentEvent, type StreamContentContextState, type StreamContentContextWorkerConfig, type StreamContentContextWorkerOut, type StreamContentContextWorkerResult, type StreamContentContextWorkerState, type TasksMap, type ThreadWorkerResult, type ThreadWorkerResultOk, type ToolCallTask, type ToolContextEventName, type ToolsContextConfig, type ToolsContextEvent, type ToolsContextEventCallTool, type ToolsContextEventLastToolReceived, type ToolsContextParsedArguments, type ToolsContextResult, type ToolsContextResultOk, type ToolsContextState, type ToolsContextWorkerConfig, type ToolsContextWorkerLocal, type ToolsContextWorkerMergeField, type ToolsContextWorkerOut, type ToolsContextWorkerState, type historyContextClassId, type historyContextEvents, states, CreateChatCompletionStep as step };
736
759
  //# sourceMappingURL=index.d.mts.map
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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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/B4L3E;;;E+BlLM,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"}
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;EACxC,MAAA,EAAQ,MAAA;AAAA;;;cCIG,QAAA,WAAmB,UAAA,UAAoB,IAAA,CAAK,CAAA;EAE1C,aAAA,IAAa,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KC8FhB,kBAAA;EACV,MAAA;EACA,MAAA,EAAQ,SAAS;AAAA;;;KCrGP,qBAAA;EACV,MAAM;AAAA;AAAA,KAEI,eAAA,GAAkB,qBAAA,GAAwB,KAAK;AAAA,KAE/C,cAAA,GAAiB,kBAAkB;AAAA,UAE9B,eAAA,iBACC,MAAA,CAAO,MAAA,IAAU,eAAA,iBAClB,KAAA,CAAM,MAAA,IAAU,cAAA,UACvB,UAAA;EACR,MAAA,EAAQ,OAAA;EACR,KAAA,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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 m,trace as h}from"@opentelemetry/api";import{SeverityNumber as g,logs as _}from"@opentelemetry/api-logs";import v from"openai";import te from"timestring";import{ATTR_GEN_AI_INPUT_MESSAGES as ne,ATTR_GEN_AI_OPERATION_NAME as re,ATTR_GEN_AI_OUTPUT_MESSAGES as ie,ATTR_GEN_AI_OUTPUT_TYPE as ae,ATTR_GEN_AI_PROVIDER_NAME as oe,ATTR_GEN_AI_REQUEST_CHOICE_COUNT as se,ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY as ce,ATTR_GEN_AI_REQUEST_MAX_TOKENS as le,ATTR_GEN_AI_REQUEST_MODEL as ue,ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY as de,ATTR_GEN_AI_REQUEST_SEED as fe,ATTR_GEN_AI_REQUEST_STOP_SEQUENCES as pe,ATTR_GEN_AI_REQUEST_STREAM as me,ATTR_GEN_AI_REQUEST_TEMPERATURE as he,ATTR_GEN_AI_REQUEST_TOP_P as ge,ATTR_GEN_AI_RESPONSE_FINISH_REASONS as _e,ATTR_GEN_AI_RESPONSE_ID as ve,ATTR_GEN_AI_RESPONSE_MODEL as y,ATTR_GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK as ye,ATTR_GEN_AI_TOKEN_TYPE as b,ATTR_GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS as be,ATTR_GEN_AI_USAGE_INPUT_TOKENS as xe,ATTR_GEN_AI_USAGE_OUTPUT_TOKENS as Se,ATTR_GEN_AI_USAGE_REASONING_OUTPUT_TOKENS as Ce,ATTR_SERVER_ADDRESS as we,ATTR_SERVER_PORT as Te,EVENT_GEN_AI_CLIENT_INFERENCE_OPERATION_DETAILS as Ee,GEN_AI_OPERATION_NAME_VALUE_CHAT as De,GEN_AI_OUTPUT_TYPE_VALUE_JSON as Oe,GEN_AI_OUTPUT_TYPE_VALUE_TEXT as ke,GEN_AI_TOKEN_TYPE_VALUE_INPUT as Ae,GEN_AI_TOKEN_TYPE_VALUE_OUTPUT as je,METRIC_GEN_AI_CLIENT_OPERATION_DURATION as Me,METRIC_GEN_AI_CLIENT_OPERATION_TIME_PER_OUTPUT_CHUNK as Ne,METRIC_GEN_AI_CLIENT_OPERATION_TIME_TO_FIRST_CHUNK as Pe,METRIC_GEN_AI_CLIENT_TOKEN_USAGE as Fe}from"@opentelemetry/semantic-conventions/incubating";import{readFile as Ie}from"node:fs/promises";const x={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 S=class extends a{},Le=class extends a{};i.config(i.locales.en());const Re=[S,r];function C(e,t={}){let{preserveErrors:n=Re,unknownMessage:a=`Unknown error`,unknownCode:o=x.UNKNOWN}=t;if(n?.some(t=>e instanceof t))return e;if(e instanceof i.core.$ZodError)return new S(i.prettifyError(e),e,{code:x.VALIDATION});if(typeof e==`string`)return new S(e,{code:x.UNKNOWN});if(e instanceof Error){if(e.message.includes(`Request timed out`))return new r(`Timeout`,e,{code:x.TIMEOUT});let t=e?.response?.data?.error?.message;return t==null?new S(a,e,{code:o}):new S(t,e,{code:x.SERVER_ERROR})}return new S(a,{code:o,data:e})}function w(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 T=w((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=>E.call(this,e,t,n)):r}catch(t){return E.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function E(e,t,r){let i=r.toString();this.log.DEBUG?.(`Error in method '${i}'`,t);let a=e.useErrorFilter?C(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 ze({thread:e,...t}){let n=O({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 Be({thread:e,...t}){let n=O({thread:e,...t});return e.process.cache[n]}function D({thread:e,...t}){let n=O({thread:e,...t});delete e.process.cache[n]}function O({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function k(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 Ve(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function He(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 A(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await Ue(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function Ue(e,t,n={}){let r=He(e).map(async n=>await A(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 j=w((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),C(e)}):t}catch(e){let t=n.toString();throw this.log.DEBUG?.(`Error in method '${t}'`,e),C(e)}}},{});i.config(i.locales.en());const We=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 M=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),Ge=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:M}),i.object({role:i.literal(`user`),content:M,name:i.optional(M.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(M),tool_calls:i.optional(i.array(i.object({id:M.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:M.check(i.minLength(1)),arguments:M.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:M,name:i.optional(M.check(i.minLength(1),i.maxLength(64))),tool_call_id:M.check(i.minLength(1))})]),Ke=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)))}))}))}),qe=i.object({provider:M.check(i.minLength(1)),model:M.check(i.minLength(1)),accountKey:N(M),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(M),userMessage:i.optional(M),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(M),temperature:N(i.coerce.number().check(i.gte(0),i.lte(2))),topP:N(i.coerce.number().check(i.gte(0),i.lte(1))),maxTokens:N(i.coerce.number().check(i.int(),i.gte(1))),presencePenalty:N(i.coerce.number().check(i.gte(-2),i.lte(2))),frequencyPenalty:N(i.coerce.number().check(i.gte(-2),i.lte(2))),n:N(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:N(i.enum([`none`,`minimal`,`low`,`medium`,`high`,`xhigh`])),streamResponse:i._default(i.boolean(),!1),streamSettings:i.optional(i.object({maxBufferLength:i.coerce.number().check(i.overwrite(e=>e===0?50:e),i.int(),i.gte(1),i.lte(32768)),debounceDuration:i.coerce.number().check(i.int(),i.gte(0),i.lte(1e4))})),customHistory:i._default(i.boolean(),!1),historyLength:N(i.coerce.number().check(i.int(),i.gte(1),i.lte(100))),historyMode:i.optional(i.enum([`mergefield`,`codeHistory`])),historyMergefield:i.optional(i.array(Ge)),historyCode:i.optional(i.array(Ge)),functionsList:i.array(i.object({name:M.check(i.minLength(1),i.maxLength(64)),description:i.optional(M.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(Ke,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(We),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:M.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 N(e){return i.optional(i.pipe(i.union([i.undefined(),i.literal(``),e]),i.transform(e=>e===``?void 0:e)))}function P(e,t){if(typeof Reflect==`object`&&typeof Reflect.metadata==`function`)return Reflect.metadata(e,t)}function F(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 I=class extends o{async resolveDataIn(){let e=await super.resolveDataIn();return qe.parse(e)}};F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],I.prototype,`resolveDataIn`,null);const L=`llm_cmp`,Je={cancel:`st_cancel`},Ye={cancel:`cnl_cancel`};var R=class e extends I{static class=`cncl_ctrl`;static async start(e){await k(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await A(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${L}_${this.class}_${e}`}runStep(){this.triggers.local({name:Ye.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=Be({thread:this.thread});n?n.abort(new Le(`Request cancelled`)):this.process.enqueueAndRun({name:Je.cancel,thread:e})}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],R.prototype,`runStep`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],R.prototype,`initialize`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],R.prototype,`onCancel`,null);const z={appendToHistory:`hst_append`,prepareHistory:`hst_prepare`,adjustOnCancel:`hst_cncl`};var B=class e extends I{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:z.prepareHistory})}static async appendToHistory(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:z.appendToHistory,params:{message:t}})}static async adjustOnCancel(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:z.adjustOnCancel})}static getHistory(e){return e.process.getSafeThread(this.getThreadId(e.step.id)).state.messages}static getThreadId(e){return`${L}_${this.class}_${e}`}runStep(){this.triggers.local(z.prepareHistory,this.onPrepareHistory).local(z.appendToHistory,this.onAppendToHistory).local(z.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)}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],B.prototype,`runStep`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],B.prototype,`initialize`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],B.prototype,`onAdjustOnCancel`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],B.prototype,`onPrepareHistory`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,void 0)],B.prototype,`onAppendToHistory`,null);const Xe={handleContent:`hnd_cnt`};var Ze=class extends u{async handleContentByParentThread(e){await s(),await this.thread.process.enqueueAndRun({thread:this.thread.local.parent,name:Xe.handleContent,params:{content:e}})}},Qe=class extends Ze{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),D({thread:this.thread})}},$e=class extends Ze{async handle(e){this.log.DEBUG?.(`Start processing stream of chunks from LLM`);for await(let t of e)await this.handleContentByParentThread(t);D({thread:this.thread})}};function et({thread:e,isStreaming:t}){return t?new $e(e):new Qe(e)}var tt=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var nt=class extends tt{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 S(`Chat completion request failed`,e,{code:x.SERVER_ERROR,data:d}):e}}async doChatCompletionRequest(e,t){return await this.openAiClient.chat.completions.create(e,t)}get tools(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0))return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:!e.parameters||Object.keys(e.parameters).length===0?void 0:e.parameters,strict:e.strict}}))}get openAiClient(){let{provider:e,accountKey:t}=this.data;return new v({baseURL:`${this.config.env.OPENAI_API_URL}/${e}`,apiKey:`_fake_key_`,defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get([`reporting`,`sessionId`]),...t?{"x-or-account-token-name":t}:void 0},timeout:this.timeoutMs})}get stopSequences(){return(this.data.stop??[]).map(e=>e.token).filter(e=>e.length>0)}get timeoutMs(){let e=te(this.data.timeoutDuration,`ms`);return i.number().check(i.int(),i.gte(1e3),i.lte(6e5)).parse(e)}};F([c(),P(`design:type`,Number),P(`design:paramtypes`,[])],nt.prototype,`timeoutMs`,null);function rt(e,t,n){return{[re]:De,[oe]:t,[ue]:e.model,...st(n)}}function it(e){let t={[me]:!!e.stream,[ae]:e.response_format==null||e.response_format.type===`text`?ke:Oe};return e.n!=null&&e.n>1&&(t[se]=e.n),e.seed!=null&&(t[fe]=e.seed),e.frequency_penalty!=null&&(t[ce]=e.frequency_penalty),e.max_completion_tokens==null?e.max_tokens!=null&&(t[le]=e.max_tokens):t[le]=e.max_completion_tokens,e.presence_penalty!=null&&(t[de]=e.presence_penalty),e.stop!=null&&(t[pe]=Array.isArray(e.stop)?e.stop:[e.stop]),e.temperature!=null&&(t[he]=e.temperature),e.top_p!=null&&(t[ge]=e.top_p),t}function V(e){return{[_e]:e.choices.map(e=>e.finish_reason).filter(e=>e!=null),[ve]:e.id,[y]:e.model,...e.usage&&{[xe]:e.usage.prompt_tokens,[Se]:e.usage.completion_tokens},...e.usage?.prompt_tokens_details&&{[be]:e.usage.prompt_tokens_details.cached_tokens},...e.usage?.completion_tokens_details&&{[Ce]:e.usage.completion_tokens_details.reasoning_tokens}}}function at(e,t){return{[ne]: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}]})),[ie]: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 ot={"https:":443,"http:":80};function st(e){if(!e)return;let t;try{t=new URL(e)}catch{return}return{[we]:t.hostname,[Te]:t.port?Number(t.port):ot[t.protocol]}}function ct({logger:e,spanAttributes:t,config:n,request:r,context:i}){return function(a){e.emit({context:i,eventName:Ee,severityNumber:g.INFO,attributes:{...t,...V(a),...n?.captureMessageContentInLogs&&at(r,a)}})}}function lt(e){return e.createHistogram(Me,{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 ut({meter:e,attributes:t,context:n,startTime:r=Date.now()}){let i=lt(e);return function(e,a){i.record(a??(Date.now()-r)/1e3,{...t,[y]:e??`unknown`},n)}}function dt(e){return e.createHistogram(Ne,{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 ft({meter:e,attributes:t,context:n}){return function(r,i){dt(e).record(r,{...t,[y]:i??`unknown`},n)}}function pt(e){return e.createHistogram(Pe,{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 mt({meter:e,attributes:t,context:n}){return function(r,i){pt(e).record(r,{...t,[y]:i??`unknown`},n)}}function ht(e){return e.createHistogram(Fe,{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 gt({meter:e,context:t,attributes:n}){let r=ht(e);return function(e,i){e?.prompt_tokens!=null&&r.record(e.prompt_tokens,{...n,[y]:i??`unknown`,[b]:Ae},t),e?.completion_tokens!=null&&r.record(e.completion_tokens,{...n,[y]:i??`unknown`,[b]:je},t)}}var _t=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 vt(e){if(!(!e||Object.keys(e).length===0))return m.extract(f.active(),e)}async function*yt(e,t,n,r,i,a,o){let s=[],c={},l,u,d=0,p=Date.now();try{let m=await e,g=h.setSpan(f.active(),t),_=f.bind(g,m),v={},te=new _t(v);for await(let e of _){te.reduceChunk(e);let t=e.choices.map(e=>e?.finish_reason).filter(e=>e!=null);if(s.push(...t),Object.assign(c,V(e)),e.model&&(l??=e.model),e.usage&&(u??=e.usage),d===0){let e=(Date.now()-p)/1e3;a(e,l),c[ye]=e}else o((Date.now()-p)/1e3,l);d+=1,p=Date.now(),yield e}Object.assign(c,{[_e]:s}),t.setAttributes(c),t.setStatus({code:ee.OK}),n(v),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 bt(e,t,n,r,i){try{let a=await e,o=V(a);return t.setAttributes(o),n(a),r(a.model),i(a.usage,a.model),a}finally{t.end()}}function xt({tracer:e,request:t,provider:n,openAiBaseURL:r,config:i}){let a=rt(t,n,r),o={...a,...it(t)},s=e.startSpan(`${o[re]} ${o[ue]}`,{kind:d.CLIENT,attributes:o},vt(i?.carrier));return{span:s,spanContext:h.setSpan(f.active(),s),commonAttributes:a,spanAttributes:o}}var H=class extends nt{get tracer(){return h.getTracer(`create-llm-chat-completion-step`)}get meter(){return p.getMeter(`create-llm-chat-completion-step`)}get logger(){return _.getLogger(`create-llm-chat-completion-step`)}async doChatCompletionRequest(...e){let t=e[0],n,r=this.data.otel??{};try{n=xt({tracer:this.tracer,request:t,provider:this.data.provider,openAiBaseURL:this.openAiClient.baseURL,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=ct({logger:this.logger,context:a,spanAttributes:s,config:r,request:t}),l=ut({meter:this.meter,attributes:o,context:a}),u=gt({meter:this.meter,attributes:o,context:a}),d=super.doChatCompletionRequest(...e);return t.stream?yt(d,i,c,l,u,mt({meter:this.meter,attributes:o,context:a}),ft({meter:this.meter,attributes:o,context:a})):bt(d,i,c,l,u)}};F([c(),P(`design:type`,Object),P(`design:paramtypes`,[])],H.prototype,`tracer`,null),F([c(),P(`design:type`,Object),P(`design:paramtypes`,[])],H.prototype,`meter`,null),F([c(),P(`design:type`,Object),P(`design:paramtypes`,[])],H.prototype,`logger`,null);const St=[`requestInit`];var U=class extends I{static class=`rqst_ctx`;static async start(e){return await k(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0})}static getThreadId(e){return`${L}_${this.class}_${e}`}cancel(){this.end({status:`cancel`})}complete(){this.end({status:`ok`})}async getHistory(){let e=B.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 H(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return et({thread:this.thread,isStreaming:e})}async runStep(){l(St);let e=ze({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{D({thread:this.thread})}}};F([c(),P(`design:type`,Object),P(`design:paramtypes`,[])],U.prototype,`openAiService`,null),F([c({tags:St}),P(`design:type`,Object),P(`design:paramtypes`,[])],U.prototype,`responseHandler`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],U.prototype,`runStep`,null);var W=class extends I{static class=`tl_wrk`;static async start(e,t){let n=JSON.parse(t.function?.arguments);await k(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`${L}_${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`}};F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],W.prototype,`runStep`,null);const G={callTool:`tl_run_call`,lastToolCallReceived:`tl_rsp_end`};var K=class e extends I{static class=`tl_ctrl`;static async start(e){await k(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:G.callTool,params:{toolCall:t}})}static async lastToolCallReceived(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:G.lastToolCallReceived})}static getThreadId(e){return`${L}_${this.class}_${e}`}runStep(){this.triggers.local(G.callTool,this.onCallTool).local(G.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 S(`Only function tools are supported at the moment`,{code:x.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 S(`${W.name} thread ended unexpectedly`,{code:x.STEP_LOGIC_ISSUE});if(t instanceof Error)throw C(t,{unknownMessage:`${W.name} thread ended with an error`,unknownCode:x.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:W.getThreadId(e)},this.onWorkerThreadEnd),W.start(this.thread,e)}setToolCallResult(e,t){let n=this.state.tasks[e];if(n==null)throw new S(`Unknown tool call with id '${e}'`,{code:x.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)}))}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],K.prototype,`runStep`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],K.prototype,`initialize`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,void 0)],K.prototype,`onCallTool`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],K.prototype,`onLastToolCallReceived`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,void 0)],K.prototype,`onWorkerThreadEnd`,null);const Ct=[`toolsInit`];var q=class extends I{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(Ct),this.triggers.local(Xe.handleContent,this.onHandleContent).hook({name:n.end,thread:U.getThreadId(this.thread.step.id)},this.onRequestContextEnd).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`),U.start(this.thread)}async onRequestContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${U.name} thread ended with result`,t),t==null)throw new S(`${U.name} thread ended unexpectedly`,{code:x.STEP_LOGIC_ISSUE});if(t instanceof Error)throw C(t,{unknownMessage:`${U.name} thread ended with an error`,unknownCode:x.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return this.cancel();if(t.status!==`ok`)throw new S(`Unexpected end of the ${U.name} thread`,{code:x.STEP_LOGIC_ISSUE,data:e.params.result});this.state.requestComplete=!0,await this.onAllContentReceived(),this.state.toolsReady!=null&&(await s(),await K.lastToolCallReceived(this.thread)),await this.tryToComplete()}async onToolsContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${K.name} thread ended`,t),t==null)throw new S(`${K.name} thread ended unexpectedly`,{code:x.STEP_LOGIC_ISSUE});if(t instanceof Error)throw C(t,{unknownMessage:`${K.name} thread ended with an error`,unknownCode:x.STEP_LOGIC_ISSUE});if(t.status===`cancel`)return;let{status:n,messages:r}=t;if(n!==`ok`||!Array.isArray(r))throw new S(`Unexpected end of the ${K.name} thread`,{code:x.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 K.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:K.getThreadId(this.thread.step.id)},this.onToolsContextEnd),await K.start(this.thread)}cancel(){this.end({status:`cancel`})}complete(){let{chatCompletion:e}=this.state;if(e==null)throw new S(`Missing chat completion content to return value`,{code:x.STEP_LOGIC_ISSUE});this.end({status:`ok`,chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Object)],q.prototype,`runStep`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],q.prototype,`initialize`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,Promise)],q.prototype,`onRequestContextEnd`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,Promise)],q.prototype,`onToolsContextEnd`,null),F([c({tags:Ct}),P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],q.prototype,`initToolsContextThread`,null);var J=class extends q{static class=`cnt_cxt_smp`;static getInitialState(){return{class:this.class,requestComplete:!1}}async onHandleContent(e){let{content:t}=e.params;this.state.chatCompletion=t;let n=t.choices[0]?.message.tool_calls;if(n!=null)for(let e of n)await this.callFunction(e)}onAllContentReceived(){this.handleMultipleChoices()}tryToComplete(){let{requestComplete:e,toolsReady:t}=this.state;!e||t===!1||this.complete()}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,Promise)],J.prototype,`onHandleContent`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],J.prototype,`onAllContentReceived`,null),i.config(i.locales.en());const wt=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())}))}))})})}),Tt={chunk:`chunk`};var Y=class extends I{static class=`str_wrk`;static async start(e,t){await k(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return Ve(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${L}_${this.class}_${e}`}runStep(){if(!(`event`in this.state)||this.state.event==null)throw new S(`Stream worker thread state must contain an event to process`,{code:x.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.(`Executing stream worker thread`,e),this.exitStep(Tt.chunk,{chunk:e})}};F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],Y.prototype,`runStep`,null);const X=[`streamInit`];var Z=class e extends q{static class=`cnt_cxt_str`;static getInitialState(){return{class:this.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1}}onToolCall(e){this.log.DEBUG?.(`Tool call received`,e),this.callFunction(e)}scheduleNextContentEvent(){let e=this.state.events[0];e==null||!this.isEventReady(e)||`lastChunkReceived`in e||this.tryRunWorker()}addContentToQueue(e){let t=this.state.events.at(-1);if(t!=null){if(`lastChunkReceived`in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}if(t.content.length<this.maxChunkContentLength){let n=`${t.content}${e}`;t.content=n.slice(0,this.maxChunkContentLength),e=n.slice(this.maxChunkContentLength)}}for(;e;){this.state.index+=1;let t={content:e.slice(0,this.maxChunkContentLength),index:this.state.index,ts:Date.now()};this.log.DEBUG?.(`Add new event into the queue`,t),this.state.events.push(t),e=e.slice(this.maxChunkContentLength)}}tryRunWorker(){if(Y.isBusy(this.thread)){this.log.DEBUG?.(`Skip event processing. Worker thread is busy`);return}let t=this.getEventToProcess();if(t!=null){if(`lastChunkReceived`in t){this.log.DEBUG?.(`Stopping ${e.name} thread. All response content chunks were processed and no new chunks are coming`),this.state.workerComplete=!0,this.tryToComplete();return}this.log.DEBUG?.(`Notify stream worker thread about response chunk to process`,t),Y.start(this.thread,t)}}isEventReady(e){if(e==null)return!1;if(`lastChunkReceived`in e)return!0;if(e.content==null)return!1;let t=Date.now()-e.ts;return this.state.lastChunkReceived||t>=this.debounceDuration||e.content.length>=this.maxChunkContentLength}getEventToProcess(){let e=this.state.events[0];if(e!=null&&this.isEventReady(e))return`lastChunkReceived`in e||this.state.events.shift(),e}tryToComplete(){let{chatCompletion:e,toolsReady:t,requestComplete:n,workerComplete:r}=this.state;if(!(!r||!n||t===!1||e==null)){if(e.choices?.[0]?.message==null)throw new S(`Missing completion message in chat completion result`,{code:x.STEP_LOGIC_ISSUE});this.complete()}}onAllContentReceived(){this.log.DEBUG?.(`All LLM response chunks were received. No more response chunks expected`),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.state.events.push({lastChunkReceived:!0}),this.state.lastChunkReceived=!0,this.tryRunWorker()}get maxChunkContentLength(){return this.data.streamSettings?.maxBufferLength??100}get debounceDuration(){return this.data.streamSettings?.debounceDuration??200}async runStep(){await super.runStep(),l(X),this.triggers.hook({name:n.end,thread:Y.getThreadId(this.thread.step.id)},this.onWorkerThreadEnd)}onWorkerThreadEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${Y.name} thread finished processing chunk`,t),t instanceof Error)throw C(t,{unknownMessage:`${Y.name} thread ended with an error`});t?.status!==`cancel`&&this.tryRunWorker()}onHandleContent(e){let t=wt.parse(e).params.content;new _t(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())}}};F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],Z.prototype,`onAllContentReceived`,null),F([c({tags:X}),P(`design:type`,Number),P(`design:paramtypes`,[])],Z.prototype,`maxChunkContentLength`,null),F([c({tags:X}),P(`design:type`,Number),P(`design:paramtypes`,[])],Z.prototype,`debounceDuration`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],Z.prototype,`runStep`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,void 0)],Z.prototype,`onWorkerThreadEnd`,null),F([T,P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,void 0)],Z.prototype,`onHandleContent`,null);const Et=e(import.meta.url);async function Dt(e){if(typeof e!=`string`||!/^\d+\.\d+\.\d+$/.test(e))throw Error(`Invalid minimum version format: ${e}`);let t=Et.resolve(`@onereach/flow-sdk/package.json`);if(!t)throw Error(`Could not find package.json for @onereach/flow-sdk`);let n=await Ie(t,`utf8`),{version:r}=JSON.parse(n),{groups:i}=/^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/.exec(r)||{};if(!i)throw Error(`Invalid Flow SDK version format: ${r}`);let a=Math.trunc(Number(i.major??`0`)),o=Math.trunc(Number(i.minor??`0`)),s=Math.trunc(Number(i.patch??`0`));if(a==null||o==null||s==null||Number.isNaN(a)||Number.isNaN(o)||Number.isNaN(s))throw Error(`Invalid version format: ${r}`);let[c,l,u]=e.split(`.`).map(e=>Math.trunc(Number(e)));if(c==null||l==null||u==null||Number.isNaN(c)||Number.isNaN(l)||Number.isNaN(u))throw Error(`Invalid minimum version format: ${e}`);if(a<c||a===c&&o<l||a===c&&o===l&&s<u)throw Error(`Flow SDK version '${r}' is lower than required minimum version '${e}' for the step`)}var Q=class e{Constructor;constructor(e){this.Constructor=e}static factory(t){return new e(t?Z:J)}async start(e){let t=this.Constructor.getInitialState();await k(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 A(e,this.getThreadId(e.step.id),{result:{status:`cancel`},stopChildThreads:!0})}getThreadId(e){return`${L}_${this.Constructor.class}_${e}`}};F([c(),P(`design:type`,Function),P(`design:paramtypes`,[Boolean]),P(`design:returntype`,Q===void 0?Object:Q)],Q,`factory`,null);var $=class extends I{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 S(`Missing chat completion result, but request is marked as processed`,{code:x.STEP_LOGIC_ISSUE});let i=n.choices?.[0]?.message;if(i==null)throw new S(`Missing completion message in chat completion result`,{code:x.STEP_LOGIC_ISSUE});await Promise.all([B.appendToHistory(this.thread,[i,...r]),R.stop(this.thread)]);let a=n.choices[0]?.finish_reason;if(a===`length`)throw new S(`Reached limit of tokens for LLM completion response`,{code:x.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([Ue(this.thread,{status:`cancel`}),B.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 S(`Merge-field type has to be "Thread" if you are using functions`,{code:x.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:B.getThreadId(this.thread.step.id)},this.onHistoryContextEnd),this.triggers.local(Je.cancel,this.onCancel),this.triggers.otherwise(this.initialize)}async initialize(){await Dt(`8.0.72`),this.validateMergeFieldType(),this.resetState(),this.state.cancelled=!1,await Promise.all([B.start(this.thread),R.start(this.thread)]),await B.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 S(`ContentContext thread ended unexpectedly`,{code:x.STEP_LOGIC_ISSUE});if(t instanceof Error)throw C(t,{unknownMessage:`ContentContext thread ended with an error`,unknownCode:x.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 S(`Unexpected end of the ContentContext thread`,{code:x.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?.(`${B.name} thread ended with result`,t),t instanceof Error)throw C(t,{unknownMessage:`${B.name} thread ended with an error`,unknownCode:x.STEP_LOGIC_ISSUE})}catch(e){try{await R.stop(this.thread)}catch(e){this.log.WARN?.(`Failed to stop CancelContext thread`,e)}throw e}}async onCancel(){await this.resetStep()}};F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,void 0)],$.prototype,`runStep`,null),F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],$.prototype,`initialize`,null),F([T({allowHandleError:!0}),P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,Promise)],$.prototype,`onContentContextEnd`,null),F([T({allowHandleError:!0}),P(`design:type`,Function),P(`design:paramtypes`,[Object]),P(`design:returntype`,Promise)],$.prototype,`onHistoryContextEnd`,null),F([j,P(`design:type`,Function),P(`design:paramtypes`,[]),P(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const Ot={[B.class]:B,[U.class]:U,[K.class]:K,[W.class]:W,[J.class]:J,[Z.class]:Z,[Y.class]:Y,[R.class]:R};export{Ot 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 re}from"@opentelemetry/api-logs";import h from"openai";import ie from"timestring";import{ATTR_GEN_AI_INPUT_MESSAGES as ae,ATTR_GEN_AI_OPERATION_NAME as oe,ATTR_GEN_AI_OUTPUT_MESSAGES as se,ATTR_GEN_AI_OUTPUT_TYPE as ce,ATTR_GEN_AI_PROVIDER_NAME as le,ATTR_GEN_AI_REQUEST_CHOICE_COUNT as ue,ATTR_GEN_AI_REQUEST_FREQUENCY_PENALTY as de,ATTR_GEN_AI_REQUEST_MAX_TOKENS as fe,ATTR_GEN_AI_REQUEST_MODEL as pe,ATTR_GEN_AI_REQUEST_PRESENCE_PENALTY as me,ATTR_GEN_AI_REQUEST_SEED as he,ATTR_GEN_AI_REQUEST_STOP_SEQUENCES as ge,ATTR_GEN_AI_REQUEST_STREAM as _e,ATTR_GEN_AI_REQUEST_TEMPERATURE as ve,ATTR_GEN_AI_REQUEST_TOP_P as ye,ATTR_GEN_AI_RESPONSE_FINISH_REASONS as g,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=>Ue.call(this,e,t,n)):r}catch(t){return Ue.call(this,e,t,n)}}},{allowHandleError:!1,useErrorFilter:!0});function Ue(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 We({thread:e,...t}){let n=C({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 Ge({thread:e,...t}){let n=C({thread:e,...t});return e.process.cache[n]}function S({thread:e,...t}){let n=C({thread:e,...t});delete e.process.cache[n]}function C({thread:e,prefix:t,stepId:n=e.currentStepId}){return`${t?`${t}:`:``}abrt_ctrl:${n}`}async function w(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 Ke(e,t){let n=e.process.getThread(t);return!!(n!=null&&n.local.active&&!n.ended)}function qe(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 T(e,t,{result:r,stopChildThreads:i}={}){let a=e.process.getThread(t);e.log.DEBUG?.(`stopThread: thread with id '${t}'`),a&&(i&&await Je(a,r),a.local.active&&(a.local.active=!1,!a.ended&&await a.enqueueAndRun({name:n.ending,result:r})))}async function Je(e,t,n={}){let r=qe(e).map(async n=>await T(e,n.id,{stopChildThreads:!0,result:t})),i=await Promise.allSettled(r);if(n.throw??!0){let e=i.find(e=>e.status===`rejected`);if(e)throw e.reason}return i}const E=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 Ye=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 D=i.string().check(i.trim(),i.refine(e=>![`undefined`,`null`].includes(e),{error:`Unexpected undefined or null merge-field value`})),O=i.discriminatedUnion(`role`,[i.object({role:i.literal(`system`),content:D}),i.object({role:i.literal(`user`),content:D,name:i.optional(D.check(i.minLength(1),i.maxLength(64)))}),i.object({role:i.literal(`assistant`),content:i.nullable(D),tool_calls:i.optional(i.array(i.object({id:D.check(i.minLength(1)),type:i.literal(`function`),function:i.object({name:D.check(i.minLength(1)),arguments:D.check(i.minLength(1))})})))}),i.object({role:i.literal(`tool`),content:D,name:i.optional(D.check(i.minLength(1),i.maxLength(64))),tool_call_id:D.check(i.minLength(1))})]),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:D.check(i.minLength(1)),model:D.check(i.minLength(1)),accountKey:k(D),typeOfStep:i.enum([`chat`,`completion`]),systemMessage:i.optional(D),userMessage:i.optional(D),switchAssistantsMessage:i._default(i.boolean(),!1),assistantsFirstMessage:i.optional(D),temperature:k(i.coerce.number().check(i.gte(0),i.lte(2))),topP:k(i.coerce.number().check(i.gte(0),i.lte(1))),maxTokens:k(i.coerce.number().check(i.int(),i.gte(1))),presencePenalty:k(i.coerce.number().check(i.gte(-2),i.lte(2))),frequencyPenalty:k(i.coerce.number().check(i.gte(-2),i.lte(2))),n:k(i.coerce.number().check(i.int(),i.gte(1),i.lte(128))),stop:i.optional(i.array(i.object({token:i.string().check(i.minLength(1),i.maxLength(64))})).check(i.maxLength(4))),reasoningEffort:k(i.enum([`none`,`minimal`,`low`,`medium`,`high`,`xhigh`])),streamResponse:i._default(i.boolean(),!1),streamSettings:i.optional(i.object({maxBufferLength:i.coerce.number().check(i.overwrite(e=>e===0?50:e),i.int(),i.gte(1),i.lte(32768)),debounceDuration:i.coerce.number().check(i.int(),i.gte(0),i.lte(1e4)),chunkMode:i.optional(i.enum([`length`,`phrase`])),minPhraseLength:i.optional(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(O)),historyCode:i.optional(i.array(O)),functionsList:i.array(i.object({name:D.check(i.minLength(1),i.maxLength(64)),description:i.optional(D.check(i.maxLength(1e3))),parameters:i.optional(i.pipe(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(Ye),processError:i.boolean(),processTimeout:i.boolean(),timeoutDuration:D.check(i.trim(),i.minLength(1))}).check(i.refine(e=>!e.streamResponse||e.provider===`openai`,{error:`Streaming is only supported for OpenAI provider`}),i.refine(e=>!e.streamResponse||e.streamSettings!==void 0,{error:`Missing "Stream settings" for response streaming mode`}),i.refine(e=>e.typeOfStep!==`chat`||e.customHistory||!e.switchAssistantsMessage||e.assistantsFirstMessage!==void 0,{error:`Missing "Assistant's first message" value`}),i.refine(e=>e.customHistory||e.userMessage!==void 0,{error:`Missing "User message" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==void 0,{error:`Missing "History mode" value`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield!==void 0,{error:`Missing "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`mergefield`||e.historyMergefield?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via merge-field`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode!==void 0,{error:`Missing "History" data as code`}),i.refine(e=>e.typeOfStep!==`chat`||!e.customHistory||e.historyMode!==`codeHistory`||e.historyCode?.find?.(e=>e.role===`user`),{error:`Missing at least one message with role "user" message in "History" data via code`}),i.refine(e=>!e.useState||e.stateSettings!=null,{error:`Missing state settings`}));function k(e){return i.optional(i.pipe(i.union([i.undefined(),i.literal(``),e]),i.transform(e=>e===``?void 0:e)))}function A(e,t){if(typeof Reflect==`object`&&typeof Reflect.metadata==`function`)return Reflect.metadata(e,t)}function j(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var M=class extends o{async resolveDataIn(){let e=await super.resolveDataIn();return Ze.parse(e)}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],M.prototype,`resolveDataIn`,null);const N=`llm_cmp`,Qe={cancel:`st_cancel`},$e={cancel:`cnl_cancel`};var P=class e extends M{static class=`cncl_ctrl`;static async start(e){await w(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class},background:!0},{force:!0})}static async stop(e){await T(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local({name:$e.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=Ge({thread:this.thread});n?n.abort(new Be(`Request cancelled`)):this.process.enqueueAndRun({name:Qe.cancel,thread:e})}};j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],P.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],P.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],P.prototype,`onCancel`,null);const F={appendToHistory:`hst_append`,prepareHistory:`hst_prepare`,adjustOnCancel:`hst_cncl`};var I=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:F.prepareHistory})}static async appendToHistory(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:F.appendToHistory,params:{message:t}})}static async adjustOnCancel(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:F.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(F.prepareHistory,this.onPrepareHistory).local(F.appendToHistory,this.onAppendToHistory).local(F.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)],I.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],I.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],I.prototype,`onAdjustOnCancel`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],I.prototype,`onPrepareHistory`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],I.prototype,`onAppendToHistory`,null);const et={handleContent:`hnd_cnt`};var tt=class extends u{async handleContentByParentThread(e){await s(),await this.thread.process.enqueueAndRun({thread:this.thread.local.parent,name:et.handleContent,params:{content:e}})}},nt=class extends tt{async handle(e){this.log.DEBUG?.(`Start processing response from LLM`),await this.handleContentByParentThread(e),S({thread:this.thread})}},rt=class extends tt{async handle(e){this.log.DEBUG?.(`Start processing stream of chunks from LLM`);for await(let t of e)await this.handleContentByParentThread(t);S({thread:this.thread})}};function it({thread:e,isStreaming:t}){return t?new rt(e):new nt(e)}var at=class extends u{data;constructor(e,t){super(e),this.data=t}};i.config(i.locales.en());var L=class extends at{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 h({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=ie(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`,[])],L.prototype,`timeoutMs`,null);function ot(e,t,n){return{[oe]:Ae,[le]:t,[pe]:e.model,...ut(n)}}function st(e){let t={[_e]:!!e.stream,[ce]:e.response_format==null||e.response_format.type===`text`?Me:je};return e.n!=null&&e.n>1&&(t[ue]=e.n),e.seed!=null&&(t[he]=e.seed),e.frequency_penalty!=null&&(t[de]=e.frequency_penalty),e.max_completion_tokens==null?e.max_tokens!=null&&(t[fe]=e.max_tokens):t[fe]=e.max_completion_tokens,e.presence_penalty!=null&&(t[me]=e.presence_penalty),e.stop!=null&&(t[ge]=Array.isArray(e.stop)?e.stop:[e.stop]),e.temperature!=null&&(t[ve]=e.temperature),e.top_p!=null&&(t[ye]=e.top_p),t}function R(e){return{[g]: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 ct(e,t){return{[ae]: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}]})),[se]: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 lt={"https:":443,"http:":80};function ut(e){if(!e)return;let t;try{t=new URL(e)}catch{return}return{[De]:t.hostname,[Oe]:t.port?Number(t.port):lt[t.protocol]}}function dt({logger:e,spanAttributes:t,config:n,request:r,context:i}){return function(a){e.emit({context:i,eventName:ke,severityNumber:ne.INFO,attributes:{...t,...R(a),...n?.captureMessageContentInLogs&&ct(r,a)}})}}function ft(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 pt({meter:e,attributes:t,context:n,startTime:r=Date.now()}){let i=ft(e);return function(e,a){i.record(a??(Date.now()-r)/1e3,{...t,[_]:e??`unknown`},n)}}function mt(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 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(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 _t({meter:e,attributes:t,context:n}){return function(r,i){gt(e).record(r,{...t,[_]:i??`unknown`},n)}}function vt(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 yt({meter:e,context:t,attributes:n}){let r=vt(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 z=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 bt(e){if(!(!e||Object.keys(e).length===0))return te.extract(f.active(),e)}async function*xt(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),re=f.bind(ne,te),h={},ie=new z(h);for await(let e of re){ie.reduceChunk(e);let t=e.choices.map(e=>e?.finish_reason).filter(e=>e!=null);if(s.push(...t),Object.assign(c,R(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,{[g]:s}),t.setAttributes(c),t.setStatus({code:ee.OK}),n(h),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 St(e,t,n,r,i){try{let a=await e,o=R(a);return t.setAttributes(o),n(a),r(a.model),i(a.usage,a.model),a}finally{t.end()}}function Ct({tracer:e,request:t,provider:n,openAiBaseURL:r,config:i}){let a=ot(t,n,r),o={...a,...st(t)},s=e.startSpan(`${o[oe]} ${o[pe]}`,{kind:d.CLIENT,attributes:o},bt(i?.carrier));return{span:s,spanContext:m.setSpan(f.active(),s),commonAttributes:a,spanAttributes:o}}var B=class extends L{get tracer(){return m.getTracer(`create-llm-chat-completion-step`)}get meter(){return p.getMeter(`create-llm-chat-completion-step`)}get logger(){return re.getLogger(`create-llm-chat-completion-step`)}async doChatCompletionRequest(...e){let t=e[0],n,r=this.data.otel??{};try{n=Ct({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=dt({logger:this.logger,context:a,spanAttributes:s,config:r,request:t}),l=pt({meter:this.meter,attributes:o,context:a}),u=yt({meter:this.meter,attributes:o,context:a}),d=super.doChatCompletionRequest(...e);return t.stream?xt(d,i,c,l,u,_t({meter:this.meter,attributes:o,context:a}),ht({meter:this.meter,attributes:o,context:a})):St(d,i,c,l,u)}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],B.prototype,`tracer`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],B.prototype,`meter`,null),j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],B.prototype,`logger`,null);const wt=[`requestInit`];var V=class extends M{static class=`rqst_ctx`;static async start(e){return await w(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=I.getHistory(this.thread);if(!this.data.useState)return e;let t=[...e],n={role:`assistant`,content:await this.data.stateSettings?.stateFunction()};if(e.at(-1)?.role===`user`){let e=Math.max(t.length-1,0);t.splice(e,0,n)}else t.push(n);return t}get openAiService(){return new B(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return it({thread:this.thread,isStreaming:e})}async runStep(){l(wt);let e=We({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{S({thread:this.thread})}}};j([c(),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`openAiService`,null),j([c({tags:wt}),A(`design:type`,Object),A(`design:paramtypes`,[])],V.prototype,`responseHandler`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],V.prototype,`runStep`,null);var H=class extends M{static class=`tl_wrk`;static async start(e,t){let n=JSON.parse(t.function?.arguments);await w(e,{id:this.getThreadId(t),state:{step:e.step.id,class:this.class},local:{toolCall:t,parsedArguments:n,parent:e.id,active:!0}})}static getThreadId(e){return`${N}_${this.class}_${e.id}`}runStep(){let{toolCall:e,parsedArguments:t}=this.local;this.log.DEBUG?.(`Initializing tool worker thread`,e),this.exitStep(this.getExitName(),{function:{name:e.function.name,arguments:t}})}getExitName(){return`function`}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],H.prototype,`runStep`,null);const U={callTool:`tl_run_call`,lastToolCallReceived:`tl_rsp_end`};var W=class e extends M{static class=`tl_ctrl`;static async start(e){await w(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:U.callTool,params:{toolCall:t}})}static async lastToolCallReceived(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:U.lastToolCallReceived})}static getThreadId(e){return`${N}_${this.class}_${e}`}runStep(){this.triggers.local(U.callTool,this.onCallTool).local(U.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(`${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});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:H.getThreadId(e)},this.onWorkerThreadEnd),H.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)],W.prototype,`runStep`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],W.prototype,`initialize`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],W.prototype,`onCallTool`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],W.prototype,`onLastToolCallReceived`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,void 0)],W.prototype,`onWorkerThreadEnd`,null);const Tt=[`toolsInit`];var G=class extends M{static get class(){throw Error(`Define "class" static field`)}static getInitialState(){throw Error(`Define "getInitialState" static method`)}runStep(){l(Tt),this.triggers.local(et.handleContent,this.onHandleContent).hook({name:n.end,thread:V.getThreadId(this.thread.step.id)},this.onRequestContextEnd).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`),V.start(this.thread)}async onRequestContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${V.name} thread ended with result`,t),t==null)throw new y(`${V.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${V.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 ${V.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 W.lastToolCallReceived(this.thread)),await this.tryToComplete()}async onToolsContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${W.name} thread ended`,t),t==null)throw new y(`${W.name} thread ended unexpectedly`,{code:v.STEP_LOGIC_ISSUE});if(t instanceof Error)throw b(t,{unknownMessage:`${W.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 ${W.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 W.callTool(this.thread,e)}handleMultipleChoices(){let{chatCompletion:e}=this.state,t=e?.choices??[];t?.length>1&&this.log.WARN?.(`Multiple choices received from LLM, but only the first one will be processed`,{choices:t.length})}async initToolsContextThread(){this.triggers.hook({name:n.end,thread:W.getThreadId(this.thread.step.id)},this.onToolsContextEnd),await W.start(this.thread)}cancel(){this.end({status:`cancel`})}complete(){let{chatCompletion:e}=this.state;if(e==null)throw new 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)],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`,Promise)],G.prototype,`onRequestContextEnd`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[Object]),A(`design:returntype`,Promise)],G.prototype,`onToolsContextEnd`,null),j([c({tags:Tt}),A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],G.prototype,`initToolsContextThread`,null);var K=class extends G{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)],K.prototype,`onHandleContent`,null),j([x,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],K.prototype,`onAllContentReceived`,null);const Et=new Set([`mr`,`mrs`,`ms`,`dr`,`prof`,`sr`,`jr`,`vs`,`etc`,`eg`,`ie`,`us`,`uk`,`st`,`approx`,`dept`,`est`]),Dt=/[.!?]["')\]]*(?=\s|$)/g,Ot=/[;:\u{2014}\u{2013}]["')\]]*(?=\s|$)|(?:\r?\n)/gu,kt=/,["')\]]*(?=\s|$)/g;function At(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,Dt,i,jt);if(r!=null){o.push(t.slice(0,r)),s+=r;continue}let c=J(t,Ot,i);if(c!=null){o.push(t.slice(0,c)),s+=c;continue}let l=J(t,kt,a);if(l!=null){o.push(t.slice(0,l)),s+=l;continue}if(t.length>=n){let e=q(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 q(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!(Et.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 w(e,{id:this.getThreadId(e.step.id),state:{step:e.step.id,class:this.class,event:t}})}static isBusy(e){return Ke(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([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],Y.prototype,`runStep`,null);const X=[`streamInit`];var Z=class e extends G{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}=At(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}=At(e,{maxLength:this.maxChunkContentLength,minLength:1});for(let e of t)this.enqueueChunk(e);for(e=n;e;){let t=e.length>this.maxChunkContentLength?q(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 z(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:K)}async start(e){let t=this.Constructor.getInitialState();await w(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 T(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([I.appendToHistory(this.thread,[i,...r]),P.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([Je(this.thread,{status:`cancel`}),I.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:I.getThreadId(this.thread.step.id)},this.onHistoryContextEnd),this.triggers.local(Qe.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([I.start(this.thread),P.start(this.thread)]),await I.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?.(`${I.name} thread ended with result`,t),t instanceof Error)throw b(t,{unknownMessage:`${I.name} thread ended with an error`,unknownCode:v.STEP_LOGIC_ISSUE})}catch(e){try{await P.stop(this.thread)}catch(e){this.log.WARN?.(`Failed to stop CancelContext thread`,e)}throw e}}async onCancel(){await this.resetStep()}};j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,void 0)],$.prototype,`runStep`,null),j([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`initialize`,null),j([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([E,A(`design:type`,Function),A(`design:paramtypes`,[]),A(`design:returntype`,Promise)],$.prototype,`onCancel`,null),t();const It={[I.class]:I,[V.class]:V,[W.class]:W,[H.class]:H,[K.class]:K,[Z.class]:Z,[Y.class]:Y,[P.class]:P};export{It as states,$ as step};
2
2
  //# sourceMappingURL=index.mjs.map