@onereach/step-create-chat-completion 0.0.7-rc.46 → 0.0.7-rc.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,2268 +1,2 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/index.ts
5
- import { install } from "source-map-support";
6
-
7
- // src/decorators/background-thread-error-filter.ts
8
- import { ACTION } from "@onereach/flow-sdk/types/index.js";
9
-
10
- // src/errors/error-filter.ts
11
- import TimeoutError from "@onereach/flow-sdk/errors/timeout.js";
12
- import { z } from "zod/v4-mini";
13
-
14
- // src/errors/step-error.ts
15
- import BaseError from "@onereach/flow-sdk/errors/base.js";
16
- var ErrorCode = {
17
- UNKNOWN: "UNKNOWN",
18
- STEP_LOGIC_ISSUE: "STEP_LOGIC_ISSUE",
19
- AUTH: "AUTH",
20
- TIMEOUT: "TIMEOUT",
21
- VALIDATION: "VALIDATION",
22
- INVALID_REQUEST: "INVALID_REQUEST",
23
- RATE_LIMITED: "RATE_LIMITED",
24
- LLM_FINISH_LENGTH: "LLM_FINISH_LENGTH",
25
- SERVER_ERROR: "SERVER_ERROR",
26
- UNSUPPORTED: "UNSUPPORTED"
27
- };
28
- var CreateChatCompletionStepError = class extends BaseError {
29
- static {
30
- __name(this, "CreateChatCompletionStepError");
31
- }
32
- };
33
- var AbortRequestError = class extends BaseError {
34
- static {
35
- __name(this, "AbortRequestError");
36
- }
37
- };
38
-
39
- // src/errors/error-filter.ts
40
- var defaultPreserveErrors = [
41
- CreateChatCompletionStepError,
42
- TimeoutError
43
- ];
44
- function errorFilter(error, options = {}) {
45
- const { preserveErrors = defaultPreserveErrors, unknownMessage = "Unknown error", unknownCode = ErrorCode.UNKNOWN } = options;
46
- if (preserveErrors?.some((ErrorClass) => error instanceof ErrorClass)) {
47
- return error;
48
- }
49
- if (error instanceof z.core.$ZodError) {
50
- return new CreateChatCompletionStepError(z.prettifyError(error), error, {
51
- code: ErrorCode.VALIDATION
52
- });
53
- }
54
- if (typeof error === "string") {
55
- return new CreateChatCompletionStepError(error, {
56
- code: ErrorCode.UNKNOWN
57
- });
58
- }
59
- if (error instanceof Error) {
60
- if (error.message.includes("Request timed out")) {
61
- return new TimeoutError("Timeout", error, {
62
- code: ErrorCode.TIMEOUT
63
- });
64
- }
65
- const responseErrorMessage = error?.response?.data?.error?.message;
66
- if (responseErrorMessage != void 0) {
67
- return new CreateChatCompletionStepError(responseErrorMessage, error, {
68
- code: ErrorCode.SERVER_ERROR
69
- });
70
- }
71
- return new CreateChatCompletionStepError(unknownMessage, error, {
72
- code: unknownCode
73
- });
74
- }
75
- return new CreateChatCompletionStepError(unknownMessage, {
76
- code: unknownCode,
77
- data: error
78
- });
79
- }
80
- __name(errorFilter, "errorFilter");
81
-
82
- // src/utils/create-method-decorator.ts
83
- function createMethodDecoratorWithOptionalArguments(apply, defaultOptions3) {
84
- function runApply(options, target, propertyKey, descriptor) {
85
- const maybe = apply(options, target, propertyKey, descriptor);
86
- if (typeof maybe === "function") {
87
- const originalMethod = descriptor.value;
88
- const wrappedMethod = maybe;
89
- if (originalMethod?.name != void 0) {
90
- try {
91
- Object.defineProperty(wrappedMethod, "name", {
92
- value: originalMethod.name
93
- });
94
- } catch {
95
- }
96
- }
97
- descriptor.value = wrappedMethod;
98
- return descriptor;
99
- }
100
- return maybe ?? descriptor;
101
- }
102
- __name(runApply, "runApply");
103
- return /* @__PURE__ */ __name(function decoratorOrFactory(argument0, argument1, argument2) {
104
- if (argument0 !== null && typeof argument0 === "object" && (typeof argument1 === "string" || typeof argument1 === "symbol") && argument2 !== null && typeof argument2 === "object") {
105
- return runApply(defaultOptions3, argument0, argument1, argument2);
106
- } else {
107
- const options = argument0 ?? defaultOptions3;
108
- return (target, propertyKey, descriptor) => {
109
- return runApply(options, target, propertyKey, descriptor);
110
- };
111
- }
112
- }, "decoratorOrFactory");
113
- }
114
- __name(createMethodDecoratorWithOptionalArguments, "createMethodDecoratorWithOptionalArguments");
115
-
116
- // src/decorators/background-thread-error-filter.ts
117
- var defaultOptions = {
118
- allowHandleError: false,
119
- useErrorFilter: true
120
- };
121
- var BackgroundThreadErrorFilter = createMethodDecoratorWithOptionalArguments((options, _target, propertyKey, descriptor) => {
122
- const originalMethod = descriptor.value;
123
- if (!originalMethod || typeof originalMethod !== "function") return;
124
- return function(...arguments_) {
125
- try {
126
- const result = originalMethod.apply(this, arguments_);
127
- if (result instanceof Promise) {
128
- return result.catch((error) => errorHandler.call(this, options, error, propertyKey));
129
- }
130
- return result;
131
- } catch (error) {
132
- return errorHandler.call(this, options, error, propertyKey);
133
- }
134
- };
135
- }, defaultOptions);
136
- function errorHandler(options, error, propertyKey) {
137
- const methodName = propertyKey.toString();
138
- this.log.DEBUG?.(`Error in method '${methodName}'`, error);
139
- const filteredError = options.useErrorFilter ? errorFilter(error) : error;
140
- if (filteredError instanceof Error) {
141
- if (options.allowHandleError) {
142
- this.thread.enqueue({
143
- name: ACTION.error,
144
- error: filteredError
145
- });
146
- return;
147
- } else {
148
- this.end(filteredError);
149
- return;
150
- }
151
- }
152
- throw filteredError;
153
- }
154
- __name(errorHandler, "errorHandler");
155
-
156
- // src/utils/process-abort-controller.ts
157
- function createAbortController({ thread, ...options }) {
158
- const cacheKey = getAbortControllerKey({
159
- thread,
160
- ...options
161
- });
162
- const process = thread.process;
163
- if (process.cache[cacheKey] != void 0) throw new Error(`AbortController for key '${cacheKey}' already exists`);
164
- return process.cache[cacheKey] = new AbortController();
165
- }
166
- __name(createAbortController, "createAbortController");
167
- function getAbortController({ thread, ...options }) {
168
- const cacheKey = getAbortControllerKey({
169
- thread,
170
- ...options
171
- });
172
- return thread.process.cache[cacheKey];
173
- }
174
- __name(getAbortController, "getAbortController");
175
- function deleteAbortController({ thread, ...options }) {
176
- const cacheKey = getAbortControllerKey({
177
- thread,
178
- ...options
179
- });
180
- delete thread.process.cache[cacheKey];
181
- }
182
- __name(deleteAbortController, "deleteAbortController");
183
- function getAbortControllerKey({ thread, prefix, stepId = thread.currentStepId }) {
184
- const normalizedPrefix = prefix ? `${prefix}:` : "";
185
- return `${normalizedPrefix}abrt_ctrl:${stepId}`;
186
- }
187
- __name(getAbortControllerKey, "getAbortControllerKey");
188
-
189
- // src/utils/thread-utilities.ts
190
- import { ACTION as ACTION2 } from "@onereach/flow-sdk/types/index.js";
191
- async function runChildThread(thread, { id, state, local, ...options }, { force } = {}) {
192
- const threadToRun = thread.process.getThread(id);
193
- thread.log.DEBUG?.(`runThread: thread with id '${id}'`, options);
194
- if (threadToRun == void 0) {
195
- return await thread.thread.runThread({
196
- ...options,
197
- id,
198
- state,
199
- local: {
200
- ...local,
201
- parent: thread.thread.id,
202
- active: true
203
- }
204
- });
205
- } else {
206
- if (!force && !threadToRun.ended && threadToRun.local.active) throw new Error(`Thread with id '${id}' has not ended yet, cannot start it again`);
207
- if (state == void 0) throw new Error(`Thread with id '${id}' must have a state to run`);
208
- threadToRun.local.active = true;
209
- return await threadToRun.enqueueAndRun({
210
- name: ACTION2.goto,
211
- state: {
212
- ...state,
213
- active: true
214
- }
215
- });
216
- }
217
- }
218
- __name(runChildThread, "runChildThread");
219
- function isThreadBusy(thread, threadId) {
220
- const threadToCheck = thread.process.getThread(threadId);
221
- return Boolean(threadToCheck != void 0 && threadToCheck.local.active && !threadToCheck.ended);
222
- }
223
- __name(isThreadBusy, "isThreadBusy");
224
- function listChildThreads(thread, parentThreadId = thread.id) {
225
- const childThreads = [];
226
- for (const threadId in thread.process.threads) {
227
- const item = thread.process.threads[threadId];
228
- if (item?.local.parent === parentThreadId) childThreads.push(item);
229
- }
230
- return childThreads;
231
- }
232
- __name(listChildThreads, "listChildThreads");
233
- async function stopThread(thread, id, { result, stopChildThreads } = {}) {
234
- const threadToStop = thread.process.getThread(id);
235
- thread.log.DEBUG?.(`stopThread: thread with id '${id}'`);
236
- if (!threadToStop) return;
237
- if (stopChildThreads) await endChildThreads(threadToStop, result);
238
- if (!threadToStop.local.active) return;
239
- threadToStop.local.active = false;
240
- if (threadToStop.ended) return;
241
- await threadToStop.enqueueAndRun({
242
- name: ACTION2.ending,
243
- result
244
- });
245
- }
246
- __name(stopThread, "stopThread");
247
- async function endChildThreads(thread, result, options = {}) {
248
- const childThreads = listChildThreads(thread);
249
- const stopTasks = childThreads.map(async (child) => await stopThread(thread, child.id, {
250
- stopChildThreads: true,
251
- result
252
- }));
253
- const stopResults = await Promise.allSettled(stopTasks);
254
- const throwError = options.throw ?? true;
255
- if (throwError) {
256
- const failedTask = stopResults.find((task) => task.status === "rejected");
257
- if (failedTask) throw failedTask.reason;
258
- }
259
- return stopResults;
260
- }
261
- __name(endChildThreads, "endChildThreads");
262
-
263
- // src/threads/base.ts
264
- import Step from "@onereach/flow-sdk/step.js";
265
-
266
- // src/decorators/error-filter.ts
267
- var defaultOptions2 = {};
268
- var ErrorFilter = createMethodDecoratorWithOptionalArguments((_options, _target, propertyKey, descriptor) => {
269
- const originalMethod = descriptor.value;
270
- if (!originalMethod || typeof originalMethod !== "function") return;
271
- return function(...arguments_) {
272
- try {
273
- const result = originalMethod.apply(this, arguments_);
274
- if (result instanceof Promise) {
275
- return result.catch((error) => {
276
- const methodName = propertyKey.toString();
277
- this.log.DEBUG?.(`Error in method '${methodName}'`, error);
278
- throw errorFilter(error);
279
- });
280
- }
281
- return result;
282
- } catch (error) {
283
- const methodName = propertyKey.toString();
284
- this.log.DEBUG?.(`Error in method '${methodName}'`, error);
285
- throw errorFilter(error);
286
- }
287
- };
288
- }, defaultOptions2);
289
-
290
- // src/schemas/data-in.ts
291
- import { en } from "zod/v4/locales";
292
- import { z as z2 } from "zod/v4-mini";
293
- z2.config(en());
294
- var stringNoUndefinedSchema = z2.string().check(z2.trim(), z2.refine((value) => ![
295
- "undefined",
296
- "null"
297
- ].includes(value), {
298
- error: "Unexpected undefined or null merge-field value"
299
- }));
300
- var messageSchema = z2.discriminatedUnion("role", [
301
- z2.object({
302
- role: z2.literal("system"),
303
- content: stringNoUndefinedSchema
304
- }),
305
- z2.object({
306
- role: z2.literal("user"),
307
- content: stringNoUndefinedSchema,
308
- name: z2.optional(stringNoUndefinedSchema.check(z2.minLength(1), z2.maxLength(64)))
309
- }),
310
- z2.object({
311
- role: z2.literal("assistant"),
312
- content: z2.nullable(stringNoUndefinedSchema),
313
- // TODO: add support for annotations and refusal
314
- // params to support in the future
315
- // refusal: z.nullable(z.string().check(z.trim(), z.minLength(1))),
316
- // annotations: z.optional(z.array(z.any())), // TODO: replace z.any() with more details
317
- tool_calls: z2.optional(z2.array(z2.object({
318
- id: stringNoUndefinedSchema.check(z2.minLength(1)),
319
- type: z2.literal("function"),
320
- function: z2.object({
321
- name: stringNoUndefinedSchema.check(z2.minLength(1)),
322
- arguments: stringNoUndefinedSchema.check(z2.minLength(1))
323
- })
324
- })))
325
- }),
326
- z2.object({
327
- role: z2.literal("tool"),
328
- content: stringNoUndefinedSchema,
329
- name: z2.optional(stringNoUndefinedSchema.check(z2.minLength(1), z2.maxLength(64))),
330
- tool_call_id: stringNoUndefinedSchema.check(z2.minLength(1))
331
- })
332
- ]);
333
- var functionParametersBaseSchema = z2.object({
334
- type: z2.literal("object"),
335
- properties: z2.record(z2.string().check(z2.minLength(1), z2.maxLength(64)), z2.object({
336
- type: z2.enum([
337
- "string",
338
- "number",
339
- "boolean",
340
- "array",
341
- "object"
342
- ]),
343
- description: z2.optional(z2.string().check(z2.maxLength(1e3))),
344
- items: z2.optional(z2.object({
345
- type: z2.enum([
346
- "string",
347
- "number",
348
- "boolean",
349
- "object"
350
- ]),
351
- description: z2.optional(z2.string().check(z2.maxLength(1e3)))
352
- }))
353
- }))
354
- });
355
- var dataInSchema = z2.object({
356
- provider: stringNoUndefinedSchema.check(z2.minLength(1)),
357
- model: stringNoUndefinedSchema.check(z2.minLength(1)),
358
- accountKey: optionalStringValue(stringNoUndefinedSchema),
359
- typeOfStep: z2.enum([
360
- "chat",
361
- "completion"
362
- ]),
363
- systemMessage: z2.optional(stringNoUndefinedSchema),
364
- userMessage: z2.optional(stringNoUndefinedSchema),
365
- switchAssistantsMessage: z2._default(z2.boolean(), false),
366
- assistantsFirstMessage: z2.optional(stringNoUndefinedSchema),
367
- // llm generation
368
- temperature: optionalStringValue(z2.coerce.number().check(z2.gte(0), z2.lte(2))),
369
- topP: optionalStringValue(z2.coerce.number().check(z2.gte(0), z2.lte(1))),
370
- maxTokens: optionalStringValue(z2.coerce.number().check(z2.int(), z2.gte(1))),
371
- presencePenalty: optionalStringValue(z2.coerce.number().check(z2.gte(-2), z2.lte(2))),
372
- frequencyPenalty: optionalStringValue(z2.coerce.number().check(z2.gte(-2), z2.lte(2))),
373
- n: optionalStringValue(z2.coerce.number().check(z2.int(), z2.gte(1), z2.lte(128))),
374
- stop: z2.optional(z2.array(z2.object({
375
- token: z2.string().check(z2.minLength(1), z2.maxLength(64))
376
- })).check(z2.maxLength(4))),
377
- // streaming
378
- streamResponse: z2._default(z2.boolean(), false),
379
- streamSettings: z2.optional(z2.object({
380
- maxBufferLength: z2.coerce.number().check(z2.overwrite((value) => value === 0 ? 50 : value), z2.int(), z2.gte(1), z2.lte(32768)),
381
- debounceDuration: z2.coerce.number().check(z2.int(), z2.gte(0), z2.lte(1e4))
382
- })),
383
- // history
384
- customHistory: z2._default(z2.boolean(), false),
385
- historyLength: optionalStringValue(z2.coerce.number().check(z2.int(), z2.gte(1), z2.lte(100))),
386
- historyMode: z2.optional(z2.enum([
387
- "mergefield",
388
- "codeHistory"
389
- ])),
390
- historyMergefield: z2.optional(z2.array(messageSchema)),
391
- historyCode: z2.optional(z2.array(messageSchema)),
392
- // function calls
393
- functionsList: z2.array(z2.object({
394
- name: stringNoUndefinedSchema.check(z2.minLength(1), z2.maxLength(64)),
395
- description: z2.optional(stringNoUndefinedSchema.check(z2.maxLength(1e3))),
396
- parameters: z2.optional(z2.pipe(functionParametersBaseSchema, z2.transform((object) => {
397
- object.required = Object.keys(object.properties);
398
- object.additionalProperties = false;
399
- return object;
400
- }))),
401
- strict: z2.boolean()
402
- })),
403
- processError: z2.boolean(),
404
- processTimeout: z2.boolean(),
405
- timeoutDuration: stringNoUndefinedSchema.check(z2.trim(), z2.minLength(1))
406
- }).check(z2.refine((data) => !data.streamResponse || data.provider === "openai", {
407
- error: "Streaming is only supported for OpenAI provider"
408
- }), z2.refine((data) => !data.streamResponse || data.streamSettings !== void 0, {
409
- error: 'Missing "Stream settings" for response streaming mode'
410
- }), z2.refine((data) => data.typeOfStep !== "chat" || data.customHistory || !data.switchAssistantsMessage || data.assistantsFirstMessage !== void 0, {
411
- error: `Missing "Assistant's first message" value`
412
- }), z2.refine((data) => data.customHistory || data.userMessage !== void 0, {
413
- error: 'Missing "User message" value'
414
- }), z2.refine((data) => data.typeOfStep !== "chat" || !data.customHistory || data.historyMode !== void 0, {
415
- error: 'Missing "History mode" value'
416
- }), z2.refine((data) => data.typeOfStep !== "chat" || !data.customHistory || data.historyMode !== "mergefield" || data.historyMergefield !== void 0, {
417
- error: 'Missing "History" data via merge-field'
418
- }), z2.refine((data) => data.typeOfStep !== "chat" || !data.customHistory || data.historyMode !== "mergefield" || data.historyMergefield?.find?.((message) => message.role === "user"), {
419
- error: 'Missing at least one message with role "user" message in "History" data via merge-field'
420
- }), z2.refine((data) => data.typeOfStep !== "chat" || !data.customHistory || data.historyMode !== "codeHistory" || data.historyCode !== void 0, {
421
- error: 'Missing "History" data as code'
422
- }), z2.refine((data) => data.typeOfStep !== "chat" || !data.customHistory || data.historyMode !== "codeHistory" || data.historyCode?.find?.((message) => message.role === "user"), {
423
- error: 'Missing at least one message with role "user" message in "History" data via code'
424
- }));
425
- function optionalStringValue(schema) {
426
- return z2.pipe(z2.union([
427
- z2.undefined(),
428
- z2.literal(""),
429
- schema
430
- ]), z2.transform((value) => value === "" ? void 0 : value));
431
- }
432
- __name(optionalStringValue, "optionalStringValue");
433
-
434
- // src/threads/base.ts
435
- function _ts_decorate(decorators, target, key, desc) {
436
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
437
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
438
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
439
- return c > 3 && r && Object.defineProperty(target, key, r), r;
440
- }
441
- __name(_ts_decorate, "_ts_decorate");
442
- function _ts_metadata(k, v) {
443
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
444
- }
445
- __name(_ts_metadata, "_ts_metadata");
446
- var BaseStep = class extends Step {
447
- static {
448
- __name(this, "BaseStep");
449
- }
450
- async resolveDataIn() {
451
- const dataIn = await super.resolveDataIn();
452
- this.log.DEBUG?.("data in before validation", dataIn);
453
- return dataInSchema.parse(dataIn);
454
- }
455
- };
456
- _ts_decorate([
457
- ErrorFilter,
458
- _ts_metadata("design:type", Function),
459
- _ts_metadata("design:paramtypes", []),
460
- _ts_metadata("design:returntype", Promise)
461
- ], BaseStep.prototype, "resolveDataIn", null);
462
- var baseClassIdPrefix = "llm_cmp";
463
-
464
- // src/threads/cancel/cancel-context-types.ts
465
- var cancelContextClassId = "cncl_ctrl";
466
- var parentEvents = {
467
- cancel: "st_cancel"
468
- };
469
- var cancelContentEvents = {
470
- cancel: "cnl_cancel"
471
- };
472
-
473
- // src/threads/cancel/cancel-context.ts
474
- function _ts_decorate2(decorators, target, key, desc) {
475
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
476
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
477
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
478
- return c > 3 && r && Object.defineProperty(target, key, r), r;
479
- }
480
- __name(_ts_decorate2, "_ts_decorate");
481
- function _ts_metadata2(k, v) {
482
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
483
- }
484
- __name(_ts_metadata2, "_ts_metadata");
485
- var CancelContext = class _CancelContext extends BaseStep {
486
- static {
487
- __name(this, "CancelContext");
488
- }
489
- static class = cancelContextClassId;
490
- static async start(thread) {
491
- await runChildThread(thread, {
492
- id: this.getThreadId(thread.step.id),
493
- state: {
494
- step: thread.step.id,
495
- class: this.class
496
- },
497
- background: true
498
- }, {
499
- force: true
500
- });
501
- }
502
- static async stop(thread) {
503
- await stopThread(thread, this.getThreadId(thread.step.id));
504
- }
505
- static getThreadId(stepId) {
506
- return `${baseClassIdPrefix}_${this.class}_${stepId}`;
507
- }
508
- // ---------------------------------------
509
- runStep() {
510
- this.triggers.local({
511
- name: cancelContentEvents.cancel
512
- }, this.onCancel).otherwise(this.initialize);
513
- }
514
- initialize() {
515
- this.log.DEBUG?.(`Initializing ${_CancelContext.name} thread`);
516
- }
517
- onCancel() {
518
- this.log.DEBUG?.("Canceling LLM request");
519
- const parentThreadId = this.local.parent;
520
- const parentThread = this.process.getThread(parentThreadId);
521
- if (!parentThread || parentThread.ended) return this.end();
522
- const abortController = getAbortController({
523
- thread: this.thread
524
- });
525
- if (abortController) {
526
- abortController.abort(new AbortRequestError("Request cancelled"));
527
- } else {
528
- void this.process.enqueueAndRun({
529
- name: parentEvents.cancel,
530
- thread: parentThreadId
531
- });
532
- }
533
- }
534
- };
535
- _ts_decorate2([
536
- BackgroundThreadErrorFilter,
537
- _ts_metadata2("design:type", Function),
538
- _ts_metadata2("design:paramtypes", []),
539
- _ts_metadata2("design:returntype", void 0)
540
- ], CancelContext.prototype, "runStep", null);
541
- _ts_decorate2([
542
- BackgroundThreadErrorFilter,
543
- _ts_metadata2("design:type", Function),
544
- _ts_metadata2("design:paramtypes", []),
545
- _ts_metadata2("design:returntype", void 0)
546
- ], CancelContext.prototype, "initialize", null);
547
- _ts_decorate2([
548
- BackgroundThreadErrorFilter,
549
- _ts_metadata2("design:type", Function),
550
- _ts_metadata2("design:paramtypes", []),
551
- _ts_metadata2("design:returntype", void 0)
552
- ], CancelContext.prototype, "onCancel", null);
553
-
554
- // src/threads/content/abstract-content-context.ts
555
- import { ACTION as ACTION4 } from "@onereach/flow-sdk/types/index.js";
556
- import { setImmediate as setImmediate2 } from "timers/promises";
557
- import { clear as clear2, Memoize as Memoize3 } from "typescript-memoize";
558
-
559
- // src/threads/request/request-context.ts
560
- import { clear, Memoize as Memoize2 } from "typescript-memoize";
561
-
562
- // src/threads/history/history-context-types.ts
563
- var historyContextClassId = "hst_ctrl";
564
- var historyContextEvents = {
565
- appendToHistory: "hst_append",
566
- prepareHistory: "hst_prepare",
567
- adjustOnCancel: "hst_cncl"
568
- };
569
-
570
- // src/threads/history/history-context.ts
571
- function _ts_decorate3(decorators, target, key, desc) {
572
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
573
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
574
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
575
- return c > 3 && r && Object.defineProperty(target, key, r), r;
576
- }
577
- __name(_ts_decorate3, "_ts_decorate");
578
- function _ts_metadata3(k, v) {
579
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
580
- }
581
- __name(_ts_metadata3, "_ts_metadata");
582
- var HistoryContext = class _HistoryContext extends BaseStep {
583
- static {
584
- __name(this, "HistoryContext");
585
- }
586
- static class = historyContextClassId;
587
- static async start(thread) {
588
- const historyThreadId = this.getThreadId(thread.step.id);
589
- const historyThread = thread.process.getThread(historyThreadId);
590
- if (historyThread != void 0) return;
591
- await thread.process.runThread({
592
- id: historyThreadId,
593
- background: true,
594
- state: {
595
- step: thread.step.id,
596
- class: this.class,
597
- messages: []
598
- }
599
- });
600
- }
601
- static async prepareHistory(thread) {
602
- await thread.process.enqueueAndRun({
603
- thread: this.getThreadId(thread.step.id),
604
- name: historyContextEvents.prepareHistory
605
- });
606
- }
607
- static async appendToHistory(thread, message) {
608
- await thread.process.enqueueAndRun({
609
- thread: this.getThreadId(thread.step.id),
610
- name: historyContextEvents.appendToHistory,
611
- params: {
612
- message
613
- }
614
- });
615
- }
616
- static async adjustOnCancel(thread) {
617
- await thread.process.enqueueAndRun({
618
- thread: this.getThreadId(thread.step.id),
619
- name: historyContextEvents.adjustOnCancel
620
- });
621
- }
622
- static getHistory(thread) {
623
- return thread.process.getSafeThread(this.getThreadId(thread.step.id)).state.messages;
624
- }
625
- static getThreadId(stepId) {
626
- return `${baseClassIdPrefix}_${this.class}_${stepId}`;
627
- }
628
- // ---------------------------------------
629
- runStep() {
630
- this.triggers.local(historyContextEvents.prepareHistory, this.onPrepareHistory).local(historyContextEvents.appendToHistory, this.onAppendToHistory).local(historyContextEvents.adjustOnCancel, this.onAdjustOnCancel).otherwise(this.initialize);
631
- }
632
- initialize() {
633
- this.log.DEBUG?.(`Initializing ${_HistoryContext.name} thread`);
634
- }
635
- onAdjustOnCancel() {
636
- let message;
637
- while (message = this.state.messages.at(-1)) {
638
- if ([
639
- "system",
640
- "user"
641
- ].includes(message.role)) break;
642
- if (message.role === "assistant" && message.tool_calls == void 0) break;
643
- const removedMessage = this.state.messages.pop();
644
- this.log.DEBUG?.("Removed message from history", removedMessage);
645
- }
646
- }
647
- onPrepareHistory() {
648
- this.log.DEBUG?.("Preparing history");
649
- const { customHistory, historyLength, systemMessage, typeOfStep, userMessage, historyMode, historyMergefield, historyCode, assistantsFirstMessage } = this.data;
650
- if (typeOfStep === "completion") {
651
- this.state.messages = [
652
- {
653
- role: "user",
654
- content: userMessage
655
- }
656
- ];
657
- return;
658
- }
659
- if (customHistory) {
660
- this.state.messages = historyMode === "mergefield" ? historyMergefield : historyCode;
661
- return;
662
- }
663
- let messages = this.state.messages;
664
- if (messages.length === 0 && assistantsFirstMessage != void 0 && assistantsFirstMessage.length > 0) {
665
- messages.push({
666
- role: "assistant",
667
- content: assistantsFirstMessage
668
- });
669
- }
670
- if (messages.at(-1)?.role !== "tool") {
671
- messages.push({
672
- role: "user",
673
- content: userMessage
674
- });
675
- }
676
- const hasToolCalls = messages.some((message) => message.role === "tool");
677
- if (!hasToolCalls && historyLength != void 0 && historyLength > 0) {
678
- messages = messages.slice(-1 * historyLength);
679
- }
680
- if (systemMessage != void 0 && systemMessage.length > 0) {
681
- const systemMessageRecord = {
682
- role: "system",
683
- content: systemMessage
684
- };
685
- if (messages[0]?.role === "system") {
686
- messages[0] = systemMessageRecord;
687
- } else {
688
- messages.unshift(systemMessageRecord);
689
- }
690
- }
691
- this.state.messages = messages;
692
- }
693
- onAppendToHistory(event) {
694
- this.log.DEBUG?.("Appending message to history", event.params);
695
- const { message } = event.params;
696
- const messages = Array.isArray(message) ? message : [
697
- message
698
- ];
699
- this.state.messages.push(...messages);
700
- }
701
- };
702
- _ts_decorate3([
703
- BackgroundThreadErrorFilter,
704
- _ts_metadata3("design:type", Function),
705
- _ts_metadata3("design:paramtypes", []),
706
- _ts_metadata3("design:returntype", void 0)
707
- ], HistoryContext.prototype, "runStep", null);
708
- _ts_decorate3([
709
- BackgroundThreadErrorFilter,
710
- _ts_metadata3("design:type", Function),
711
- _ts_metadata3("design:paramtypes", []),
712
- _ts_metadata3("design:returntype", void 0)
713
- ], HistoryContext.prototype, "initialize", null);
714
- _ts_decorate3([
715
- BackgroundThreadErrorFilter,
716
- _ts_metadata3("design:type", Function),
717
- _ts_metadata3("design:paramtypes", []),
718
- _ts_metadata3("design:returntype", void 0)
719
- ], HistoryContext.prototype, "onAdjustOnCancel", null);
720
- _ts_decorate3([
721
- BackgroundThreadErrorFilter,
722
- _ts_metadata3("design:type", Function),
723
- _ts_metadata3("design:paramtypes", []),
724
- _ts_metadata3("design:returntype", void 0)
725
- ], HistoryContext.prototype, "onPrepareHistory", null);
726
- _ts_decorate3([
727
- BackgroundThreadErrorFilter,
728
- _ts_metadata3("design:type", Function),
729
- _ts_metadata3("design:paramtypes", [
730
- typeof HistoryContextEventAppendToHistory === "undefined" ? Object : HistoryContextEventAppendToHistory
731
- ]),
732
- _ts_metadata3("design:returntype", void 0)
733
- ], HistoryContext.prototype, "onAppendToHistory", null);
734
-
735
- // src/threads/request/response-handlers/abstract-response-handler.ts
736
- import { BasicThreadService } from "@onereach/flow-sdk/services/basic.js";
737
- import { setImmediate } from "timers/promises";
738
-
739
- // src/threads/content/abstract-content-context-types.ts
740
- var contentContextEvents = {
741
- handleContent: "hnd_cnt"
742
- };
743
-
744
- // src/threads/request/response-handlers/abstract-response-handler.ts
745
- var AbstractResponseHandler2 = class extends BasicThreadService {
746
- static {
747
- __name(this, "AbstractResponseHandler");
748
- }
749
- async handleContentByParentThread(content) {
750
- await setImmediate();
751
- await this.thread.process.enqueueAndRun({
752
- thread: this.thread.local.parent,
753
- name: contentContextEvents.handleContent,
754
- params: {
755
- content
756
- }
757
- });
758
- }
759
- };
760
-
761
- // src/threads/request/response-handlers/simple-response-handler.ts
762
- var SimpleResponseHandler = class extends AbstractResponseHandler2 {
763
- static {
764
- __name(this, "SimpleResponseHandler");
765
- }
766
- async handle(response) {
767
- this.log.DEBUG?.("Start processing response from LLM");
768
- await this.handleContentByParentThread(response);
769
- deleteAbortController({
770
- thread: this.thread
771
- });
772
- }
773
- };
774
-
775
- // src/threads/request/response-handlers/stream-response-handler.ts
776
- var StreamResponseHandler = class extends AbstractResponseHandler2 {
777
- static {
778
- __name(this, "StreamResponseHandler");
779
- }
780
- async handle(response) {
781
- this.log.DEBUG?.("Start processing stream of chunks from LLM");
782
- for await (const chunk of response) {
783
- await this.handleContentByParentThread(chunk);
784
- }
785
- deleteAbortController({
786
- thread: this.thread
787
- });
788
- }
789
- };
790
-
791
- // src/threads/request/factories/response-handler.ts
792
- function responseHandlerFactory({ thread, isStreaming }) {
793
- if (isStreaming) return new StreamResponseHandler(thread);
794
- return new SimpleResponseHandler(thread);
795
- }
796
- __name(responseHandlerFactory, "responseHandlerFactory");
797
-
798
- // src/threads/request/request-context-types.ts
799
- var requestContextClassId = "rqst_ctx";
800
-
801
- // src/threads/request/services/openai-request.ts
802
- import OpenAI from "openai";
803
- import timestring from "timestring";
804
- import { Memoize } from "typescript-memoize";
805
- import { z as z3 } from "zod/v4-mini";
806
-
807
- // src/services/thread-service-with-data.ts
808
- import { BasicThreadService as BasicThreadService2 } from "@onereach/flow-sdk/services/basic.js";
809
- var ThreadServiceWithData = class extends BasicThreadService2 {
810
- static {
811
- __name(this, "ThreadServiceWithData");
812
- }
813
- data;
814
- constructor(thread, data) {
815
- super(thread);
816
- this.data = data;
817
- }
818
- };
819
-
820
- // src/threads/request/services/openai-request.ts
821
- function _ts_decorate4(decorators, target, key, desc) {
822
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
823
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
824
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
825
- return c > 3 && r && Object.defineProperty(target, key, r), r;
826
- }
827
- __name(_ts_decorate4, "_ts_decorate");
828
- function _ts_metadata4(k, v) {
829
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
830
- }
831
- __name(_ts_metadata4, "_ts_metadata");
832
- var OpenAIRequestService = class extends ThreadServiceWithData {
833
- static {
834
- __name(this, "OpenAIRequestService");
835
- }
836
- /**
837
- * Makes a request to the OpenAI API to create a chat completion.
838
- */
839
- async makeOpenAiRequest(messages, signal) {
840
- const { model, temperature, topP, maxTokens, frequencyPenalty, presencePenalty, n, streamResponse } = this.data;
841
- const request = {
842
- messages: structuredClone(messages),
843
- model,
844
- temperature,
845
- top_p: topP,
846
- max_tokens: maxTokens,
847
- frequency_penalty: frequencyPenalty,
848
- presence_penalty: presencePenalty,
849
- stop: this.stopSequences,
850
- n,
851
- stream: streamResponse,
852
- tools: this.tools
853
- };
854
- this.log.DEBUG?.("LLM request", request);
855
- try {
856
- const response = await this.openAiClient.chat.completions.create(request, {
857
- signal
858
- });
859
- if (!streamResponse) this.log.DEBUG?.("LLM response", response);
860
- return response;
861
- } catch (error) {
862
- if (error instanceof Error === false) throw error;
863
- throw new CreateChatCompletionStepError("Chat completion request failed", error, {
864
- code: ErrorCode.SERVER_ERROR,
865
- data: request
866
- });
867
- }
868
- }
869
- get tools() {
870
- const { functionsList } = this.data;
871
- if (!Array.isArray(functionsList) || functionsList.length === 0) return void 0;
872
- return functionsList.map((tool) => ({
873
- type: "function",
874
- function: {
875
- name: tool.name,
876
- description: tool.description,
877
- parameters: !tool.parameters || Object.keys(tool.parameters).length === 0 ? void 0 : tool.parameters,
878
- strict: tool.strict
879
- }
880
- }));
881
- }
882
- /**
883
- * Returns the OpenAI client instance configured with the step's data.
884
- */
885
- get openAiClient() {
886
- const { provider, accountKey } = this.data;
887
- const clientOptions = {
888
- baseURL: `${this.config.env.OPENAI_API_URL}/${provider}`,
889
- apiKey: "",
890
- defaultHeaders: {
891
- Authorization: this.config.authorization,
892
- "x-or-session-id": this.session.get([
893
- "reporting",
894
- "sessionId"
895
- ]),
896
- ...accountKey ? {
897
- "x-or-account-token-name": accountKey
898
- } : void 0
899
- },
900
- timeout: this.timeoutMs
901
- };
902
- this.log.DEBUG?.("OpenAI client options", clientOptions);
903
- return new OpenAI(clientOptions);
904
- }
905
- get stopSequences() {
906
- const list = this.data.stop ?? [];
907
- return list.map((item) => item.token).filter((token) => token.length > 0);
908
- }
909
- /**
910
- * Returns the timeout duration in milliseconds.
911
- */
912
- get timeoutMs() {
913
- const value = timestring(this.data.timeoutDuration, "ms");
914
- return z3.number().check(z3.int(), z3.gte(1e3), z3.lte(6e5)).parse(value);
915
- }
916
- };
917
- _ts_decorate4([
918
- Memoize(),
919
- _ts_metadata4("design:type", Number),
920
- _ts_metadata4("design:paramtypes", [])
921
- ], OpenAIRequestService.prototype, "timeoutMs", null);
922
-
923
- // src/threads/request/request-context.ts
924
- function _ts_decorate5(decorators, target, key, desc) {
925
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
926
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
927
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
928
- return c > 3 && r && Object.defineProperty(target, key, r), r;
929
- }
930
- __name(_ts_decorate5, "_ts_decorate");
931
- function _ts_metadata5(k, v) {
932
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
933
- }
934
- __name(_ts_metadata5, "_ts_metadata");
935
- var memoizeTag = [
936
- "requestInit"
937
- ];
938
- var RequestContext = class extends BaseStep {
939
- static {
940
- __name(this, "RequestContext");
941
- }
942
- static class = requestContextClassId;
943
- static async start(thread) {
944
- return await runChildThread(thread, {
945
- id: this.getThreadId(thread.step.id),
946
- state: {
947
- step: thread.step.id,
948
- class: this.class
949
- },
950
- background: true
951
- });
952
- }
953
- static getThreadId(stepId) {
954
- return `${baseClassIdPrefix}_${this.class}_${stepId}`;
955
- }
956
- // ---------------------------------------
957
- async runStep() {
958
- clear(memoizeTag);
959
- const abortController = createAbortController({
960
- thread: this.thread
961
- });
962
- try {
963
- const response = await this.openAiService.makeOpenAiRequest(this.history, abortController.signal);
964
- await this.responseHandler.handle(response);
965
- this.complete();
966
- } catch (error) {
967
- if (abortController?.signal.aborted) return this.cancel();
968
- throw error;
969
- } finally {
970
- deleteAbortController({
971
- thread: this.thread
972
- });
973
- }
974
- }
975
- cancel() {
976
- this.end({
977
- status: "cancel"
978
- });
979
- }
980
- complete() {
981
- this.end({
982
- status: "ok"
983
- });
984
- }
985
- get history() {
986
- return HistoryContext.getHistory(this.thread);
987
- }
988
- get openAiService() {
989
- return new OpenAIRequestService(this.thread, this.data);
990
- }
991
- get responseHandler() {
992
- const isStreaming = this.data.streamResponse;
993
- return responseHandlerFactory({
994
- thread: this.thread,
995
- isStreaming
996
- });
997
- }
998
- };
999
- _ts_decorate5([
1000
- BackgroundThreadErrorFilter,
1001
- _ts_metadata5("design:type", Function),
1002
- _ts_metadata5("design:paramtypes", []),
1003
- _ts_metadata5("design:returntype", Promise)
1004
- ], RequestContext.prototype, "runStep", null);
1005
- _ts_decorate5([
1006
- Memoize2(),
1007
- _ts_metadata5("design:type", typeof OpenAIRequestService === "undefined" ? Object : OpenAIRequestService),
1008
- _ts_metadata5("design:paramtypes", [])
1009
- ], RequestContext.prototype, "openAiService", null);
1010
- _ts_decorate5([
1011
- Memoize2({
1012
- tags: memoizeTag
1013
- }),
1014
- _ts_metadata5("design:type", typeof AbstractResponseHandler === "undefined" ? Object : AbstractResponseHandler),
1015
- _ts_metadata5("design:paramtypes", [])
1016
- ], RequestContext.prototype, "responseHandler", null);
1017
-
1018
- // src/threads/tools/tools-context.ts
1019
- import { ACTION as ACTION3 } from "@onereach/flow-sdk/types/index.js";
1020
-
1021
- // src/threads/tools/tools-context-types.ts
1022
- var toolsContextClassId = "tl_ctrl";
1023
- var toolContentEvents = {
1024
- callTool: "tl_run_call",
1025
- lastToolCallReceived: "tl_rsp_end"
1026
- };
1027
-
1028
- // src/threads/tools/tools-context-worker-types.ts
1029
- var toolsContextWorkerClassId = "tl_wrk";
1030
-
1031
- // src/threads/tools/tools-context-worker.ts
1032
- function _ts_decorate6(decorators, target, key, desc) {
1033
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1034
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1035
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1036
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1037
- }
1038
- __name(_ts_decorate6, "_ts_decorate");
1039
- function _ts_metadata6(k, v) {
1040
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1041
- }
1042
- __name(_ts_metadata6, "_ts_metadata");
1043
- var ToolsContextWorker = class extends BaseStep {
1044
- static {
1045
- __name(this, "ToolsContextWorker");
1046
- }
1047
- static class = toolsContextWorkerClassId;
1048
- static async start(thread, toolCall) {
1049
- const parsedArguments = JSON.parse(toolCall.function?.arguments);
1050
- await runChildThread(thread, {
1051
- id: this.getThreadId(toolCall),
1052
- state: {
1053
- step: thread.step.id,
1054
- class: this.class
1055
- },
1056
- local: {
1057
- toolCall,
1058
- parsedArguments,
1059
- // TODO: figure out how to remove these params without breaking typing
1060
- parent: thread.id,
1061
- active: true
1062
- }
1063
- });
1064
- }
1065
- static getThreadId(toolCall) {
1066
- return `${baseClassIdPrefix}_${this.class}_${toolCall.id}`;
1067
- }
1068
- // -----------------------------------------------------------------------------------
1069
- /**
1070
- * Initializes the tool worker thread to handle processing of repackaged content chunks.
1071
- */
1072
- runStep() {
1073
- const { toolCall, parsedArguments } = this.local;
1074
- this.log.DEBUG?.("Initializing tool worker thread", toolCall);
1075
- this.exitStep(this.getExitName(), {
1076
- function: {
1077
- name: toolCall.function.name,
1078
- arguments: parsedArguments
1079
- }
1080
- });
1081
- }
1082
- getExitName() {
1083
- return "function";
1084
- }
1085
- };
1086
- _ts_decorate6([
1087
- ErrorFilter,
1088
- _ts_metadata6("design:type", Function),
1089
- _ts_metadata6("design:paramtypes", []),
1090
- _ts_metadata6("design:returntype", void 0)
1091
- ], ToolsContextWorker.prototype, "runStep", null);
1092
-
1093
- // src/threads/tools/tools-context.ts
1094
- function _ts_decorate7(decorators, target, key, desc) {
1095
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1096
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1097
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1098
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1099
- }
1100
- __name(_ts_decorate7, "_ts_decorate");
1101
- function _ts_metadata7(k, v) {
1102
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1103
- }
1104
- __name(_ts_metadata7, "_ts_metadata");
1105
- var ToolsContext = class _ToolsContext extends BaseStep {
1106
- static {
1107
- __name(this, "ToolsContext");
1108
- }
1109
- static class = toolsContextClassId;
1110
- static async start(thread) {
1111
- const threadId = this.getThreadId(thread.step.id);
1112
- await runChildThread(thread, {
1113
- id: threadId,
1114
- state: {
1115
- step: thread.step.id,
1116
- class: this.class,
1117
- /** tool calls from LLM response */
1118
- tasks: {},
1119
- /** flag to indicate if there are more tasks to process */
1120
- expectMoreTasks: true
1121
- },
1122
- background: true
1123
- });
1124
- }
1125
- static async callTool(thread, toolCall) {
1126
- await thread.process.enqueueAndRun({
1127
- thread: this.getThreadId(thread.step.id),
1128
- name: toolContentEvents.callTool,
1129
- params: {
1130
- toolCall
1131
- }
1132
- });
1133
- }
1134
- /**
1135
- * Notify tools context thread that no more tool calls are expected
1136
- */
1137
- static async lastToolCallReceived(thread) {
1138
- await thread.process.enqueueAndRun({
1139
- thread: this.getThreadId(thread.step.id),
1140
- name: toolContentEvents.lastToolCallReceived
1141
- });
1142
- }
1143
- static getThreadId(stepId) {
1144
- return `${baseClassIdPrefix}_${this.class}_${stepId}`;
1145
- }
1146
- // ---------------------------------------
1147
- runStep() {
1148
- this.triggers.local(toolContentEvents.callTool, this.onCallTool).local(toolContentEvents.lastToolCallReceived, this.onLastToolCallReceived).otherwise(this.initialize);
1149
- }
1150
- initialize() {
1151
- this.log.DEBUG?.(`Initializing ${_ToolsContext.name} thread`);
1152
- }
1153
- onCallTool(event) {
1154
- const { toolCall } = event.params;
1155
- if (toolCall.type !== "function") {
1156
- throw new CreateChatCompletionStepError("Only function tools are supported at the moment", {
1157
- code: ErrorCode.UNSUPPORTED
1158
- });
1159
- }
1160
- this.state.tasks[toolCall.id] = {
1161
- call: toolCall,
1162
- status: "pending"
1163
- };
1164
- this.startToolWorkerThread(toolCall);
1165
- }
1166
- onLastToolCallReceived() {
1167
- this.log.DEBUG?.("No more tool calls expected", this.state.tasks);
1168
- this.state.expectMoreTasks = false;
1169
- this.tryEndToolsContextThread();
1170
- }
1171
- onWorkerThreadEnd(event) {
1172
- this.log.DEBUG?.("Tool caller worker thread has ended", event);
1173
- const { result } = event.params.thread;
1174
- if (result == void 0) {
1175
- throw new CreateChatCompletionStepError(`${ToolsContextWorker.name} thread ended unexpectedly`, {
1176
- code: ErrorCode.STEP_LOGIC_ISSUE
1177
- });
1178
- }
1179
- if (result instanceof Error) {
1180
- const resultError = errorFilter(result, {
1181
- unknownMessage: `${ToolsContextWorker.name} thread ended with an error`,
1182
- unknownCode: ErrorCode.STEP_LOGIC_ISSUE
1183
- });
1184
- throw resultError;
1185
- }
1186
- if (result.status === "cancel") return;
1187
- this.log.DEBUG?.(`Tool worker thread completed successfully`, {
1188
- toolCallId: result.toolCallId,
1189
- result: result.data
1190
- });
1191
- this.setToolCallResult(result.toolCallId, result.data);
1192
- this.tryEndToolsContextThread();
1193
- }
1194
- /**
1195
- * Starts the tool worker thread to process the tool call.
1196
- */
1197
- startToolWorkerThread(toolCall) {
1198
- this.triggers.hook({
1199
- name: ACTION3.end,
1200
- thread: ToolsContextWorker.getThreadId(toolCall)
1201
- }, this.onWorkerThreadEnd);
1202
- void ToolsContextWorker.start(this.thread, toolCall);
1203
- }
1204
- setToolCallResult(toolCallId, result) {
1205
- const toolCall = this.state.tasks[toolCallId];
1206
- if (toolCall == void 0) {
1207
- throw new CreateChatCompletionStepError(`Unknown tool call with id '${toolCallId}'`, {
1208
- code: ErrorCode.STEP_LOGIC_ISSUE
1209
- });
1210
- }
1211
- toolCall.status = "completed";
1212
- toolCall.result = result;
1213
- }
1214
- tryEndToolsContextThread() {
1215
- if (this.state.expectMoreTasks) return;
1216
- const hasActiveTasks = Object.values(this.state.tasks).some((task) => task.status === "pending");
1217
- if (hasActiveTasks) return;
1218
- this.log.DEBUG?.("All tool calls completed or failed", this.state.tasks);
1219
- this.end({
1220
- status: "ok",
1221
- messages: this.messages
1222
- });
1223
- }
1224
- get messages() {
1225
- return Object.values(this.state.tasks).map((task) => ({
1226
- role: "tool",
1227
- tool_call_id: task.call.id,
1228
- content: JSON.stringify(task.result)
1229
- }));
1230
- }
1231
- };
1232
- _ts_decorate7([
1233
- BackgroundThreadErrorFilter,
1234
- _ts_metadata7("design:type", Function),
1235
- _ts_metadata7("design:paramtypes", []),
1236
- _ts_metadata7("design:returntype", void 0)
1237
- ], ToolsContext.prototype, "runStep", null);
1238
- _ts_decorate7([
1239
- BackgroundThreadErrorFilter,
1240
- _ts_metadata7("design:type", Function),
1241
- _ts_metadata7("design:paramtypes", []),
1242
- _ts_metadata7("design:returntype", void 0)
1243
- ], ToolsContext.prototype, "initialize", null);
1244
- _ts_decorate7([
1245
- BackgroundThreadErrorFilter,
1246
- _ts_metadata7("design:type", Function),
1247
- _ts_metadata7("design:paramtypes", [
1248
- typeof ToolsContextEventCallTool === "undefined" ? Object : ToolsContextEventCallTool
1249
- ]),
1250
- _ts_metadata7("design:returntype", void 0)
1251
- ], ToolsContext.prototype, "onCallTool", null);
1252
- _ts_decorate7([
1253
- BackgroundThreadErrorFilter,
1254
- _ts_metadata7("design:type", Function),
1255
- _ts_metadata7("design:paramtypes", []),
1256
- _ts_metadata7("design:returntype", void 0)
1257
- ], ToolsContext.prototype, "onLastToolCallReceived", null);
1258
- _ts_decorate7([
1259
- BackgroundThreadErrorFilter,
1260
- _ts_metadata7("design:type", Function),
1261
- _ts_metadata7("design:paramtypes", [
1262
- typeof IActionEvent === "undefined" ? Object : IActionEvent
1263
- ]),
1264
- _ts_metadata7("design:returntype", void 0)
1265
- ], ToolsContext.prototype, "onWorkerThreadEnd", null);
1266
-
1267
- // src/threads/content/abstract-content-context.ts
1268
- function _ts_decorate8(decorators, target, key, desc) {
1269
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1270
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1271
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1272
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1273
- }
1274
- __name(_ts_decorate8, "_ts_decorate");
1275
- function _ts_metadata8(k, v) {
1276
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1277
- }
1278
- __name(_ts_metadata8, "_ts_metadata");
1279
- var memoizeTag2 = [
1280
- "toolsInit"
1281
- ];
1282
- var AbstractContentContext = class extends BaseStep {
1283
- static {
1284
- __name(this, "AbstractContentContext");
1285
- }
1286
- static get class() {
1287
- throw new Error('Define "class" static field');
1288
- }
1289
- static getInitialState() {
1290
- throw new Error('Define "getInitialState" static method');
1291
- }
1292
- // ----------------------------------------------
1293
- runStep() {
1294
- clear2(memoizeTag2);
1295
- this.triggers.local(contentContextEvents.handleContent, this.onHandleContent).hook({
1296
- name: ACTION4.end,
1297
- thread: RequestContext.getThreadId(this.thread.step.id)
1298
- }, this.onRequestContextEnd).otherwise(this.initialize);
1299
- }
1300
- initialize() {
1301
- this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`);
1302
- void RequestContext.start(this.thread);
1303
- }
1304
- async onRequestContextEnd(event) {
1305
- const { result } = event.params.thread;
1306
- this.log.DEBUG?.(`${RequestContext.name} thread ended with result`, result);
1307
- if (result == void 0) {
1308
- throw new CreateChatCompletionStepError(`${RequestContext.name} thread ended unexpectedly`, {
1309
- code: ErrorCode.STEP_LOGIC_ISSUE
1310
- });
1311
- }
1312
- if (result instanceof Error) {
1313
- const resultError = errorFilter(result, {
1314
- unknownMessage: `${RequestContext.name} thread ended with an error`,
1315
- unknownCode: ErrorCode.STEP_LOGIC_ISSUE
1316
- });
1317
- throw resultError;
1318
- }
1319
- if (result.status === "cancel") return this.cancel();
1320
- if (result.status !== "ok") {
1321
- throw new CreateChatCompletionStepError(`Unexpected end of the ${RequestContext.name} thread`, {
1322
- code: ErrorCode.STEP_LOGIC_ISSUE,
1323
- data: event.params.result
1324
- });
1325
- }
1326
- this.state.requestComplete = true;
1327
- await this.onAllContentReceived();
1328
- if (this.state.toolsReady != void 0) {
1329
- await setImmediate2();
1330
- await ToolsContext.lastToolCallReceived(this.thread);
1331
- }
1332
- await this.tryToComplete();
1333
- }
1334
- async onToolsContextEnd(event) {
1335
- const { result } = event.params.thread;
1336
- this.log.DEBUG?.(`${ToolsContext.name} thread ended`, result);
1337
- if (result == void 0) {
1338
- throw new CreateChatCompletionStepError(`${ToolsContext.name} thread ended unexpectedly`, {
1339
- code: ErrorCode.STEP_LOGIC_ISSUE
1340
- });
1341
- }
1342
- if (result instanceof Error) {
1343
- const resultError = errorFilter(result, {
1344
- unknownMessage: `${ToolsContext.name} thread ended with an error`,
1345
- unknownCode: ErrorCode.STEP_LOGIC_ISSUE
1346
- });
1347
- throw resultError;
1348
- }
1349
- if (result.status === "cancel") return;
1350
- const { status, messages } = result;
1351
- if (status !== "ok" || !Array.isArray(messages)) {
1352
- throw new CreateChatCompletionStepError(`Unexpected end of the ${ToolsContext.name} thread`, {
1353
- code: ErrorCode.STEP_LOGIC_ISSUE,
1354
- data: event.params.result
1355
- });
1356
- }
1357
- this.state.toolsResults = messages;
1358
- this.state.toolsReady = true;
1359
- await this.tryToComplete();
1360
- }
1361
- async callFunction(toolCall) {
1362
- this.state.toolsReady = false;
1363
- await this.initToolsContextThread();
1364
- await ToolsContext.callTool(this.thread, toolCall);
1365
- }
1366
- handleMultipleChoices() {
1367
- const { chatCompletion } = this.state;
1368
- const choices = chatCompletion?.choices ?? [];
1369
- if (choices?.length > 1) {
1370
- this.log.WARN?.("Multiple choices received from LLM, but only the first one will be processed", {
1371
- choices: choices.length
1372
- });
1373
- }
1374
- }
1375
- async initToolsContextThread() {
1376
- this.triggers.hook({
1377
- name: ACTION4.end,
1378
- thread: ToolsContext.getThreadId(this.thread.step.id)
1379
- }, this.onToolsContextEnd);
1380
- await ToolsContext.start(this.thread);
1381
- }
1382
- cancel() {
1383
- this.end({
1384
- status: "cancel"
1385
- });
1386
- }
1387
- complete() {
1388
- const { chatCompletion } = this.state;
1389
- if (chatCompletion == void 0) {
1390
- throw new CreateChatCompletionStepError("Missing chat completion content to return value", {
1391
- code: ErrorCode.STEP_LOGIC_ISSUE
1392
- });
1393
- }
1394
- this.end({
1395
- status: "ok",
1396
- chatCompletion,
1397
- toolsResults: this.state.toolsResults ?? []
1398
- });
1399
- }
1400
- };
1401
- _ts_decorate8([
1402
- BackgroundThreadErrorFilter,
1403
- _ts_metadata8("design:type", Function),
1404
- _ts_metadata8("design:paramtypes", []),
1405
- _ts_metadata8("design:returntype", Object)
1406
- ], AbstractContentContext.prototype, "runStep", null);
1407
- _ts_decorate8([
1408
- BackgroundThreadErrorFilter,
1409
- _ts_metadata8("design:type", Function),
1410
- _ts_metadata8("design:paramtypes", []),
1411
- _ts_metadata8("design:returntype", void 0)
1412
- ], AbstractContentContext.prototype, "initialize", null);
1413
- _ts_decorate8([
1414
- BackgroundThreadErrorFilter,
1415
- _ts_metadata8("design:type", Function),
1416
- _ts_metadata8("design:paramtypes", [
1417
- typeof IActionEvent === "undefined" ? Object : IActionEvent
1418
- ]),
1419
- _ts_metadata8("design:returntype", Promise)
1420
- ], AbstractContentContext.prototype, "onRequestContextEnd", null);
1421
- _ts_decorate8([
1422
- BackgroundThreadErrorFilter,
1423
- _ts_metadata8("design:type", Function),
1424
- _ts_metadata8("design:paramtypes", [
1425
- typeof IActionEvent === "undefined" ? Object : IActionEvent
1426
- ]),
1427
- _ts_metadata8("design:returntype", Promise)
1428
- ], AbstractContentContext.prototype, "onToolsContextEnd", null);
1429
- _ts_decorate8([
1430
- Memoize3({
1431
- tags: memoizeTag2
1432
- }),
1433
- _ts_metadata8("design:type", Function),
1434
- _ts_metadata8("design:paramtypes", []),
1435
- _ts_metadata8("design:returntype", Promise)
1436
- ], AbstractContentContext.prototype, "initToolsContextThread", null);
1437
-
1438
- // src/threads/content/simple/simple-content-context.ts
1439
- function _ts_decorate9(decorators, target, key, desc) {
1440
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1441
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1442
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1443
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1444
- }
1445
- __name(_ts_decorate9, "_ts_decorate");
1446
- function _ts_metadata9(k, v) {
1447
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1448
- }
1449
- __name(_ts_metadata9, "_ts_metadata");
1450
- var SimpleContentContext = class extends AbstractContentContext {
1451
- static {
1452
- __name(this, "SimpleContentContext");
1453
- }
1454
- static class = "cnt_cxt_smp";
1455
- static getInitialState() {
1456
- return {
1457
- requestComplete: false
1458
- };
1459
- }
1460
- // ----------------------------------------------
1461
- async onHandleContent(event) {
1462
- const { content } = event.params;
1463
- this.state.chatCompletion = content;
1464
- const toolCalls = content.choices[0]?.message.tool_calls;
1465
- if (toolCalls != void 0) {
1466
- for (const toolCall of toolCalls) {
1467
- await this.callFunction(toolCall);
1468
- }
1469
- }
1470
- }
1471
- onAllContentReceived() {
1472
- this.handleMultipleChoices();
1473
- }
1474
- tryToComplete() {
1475
- const { requestComplete, toolsReady } = this.state;
1476
- if (!requestComplete || toolsReady === false) return;
1477
- this.complete();
1478
- }
1479
- };
1480
- _ts_decorate9([
1481
- BackgroundThreadErrorFilter,
1482
- _ts_metadata9("design:type", Function),
1483
- _ts_metadata9("design:paramtypes", [
1484
- typeof SimpleContentContextHandleContentEvent === "undefined" ? Object : SimpleContentContextHandleContentEvent
1485
- ]),
1486
- _ts_metadata9("design:returntype", Promise)
1487
- ], SimpleContentContext.prototype, "onHandleContent", null);
1488
- _ts_decorate9([
1489
- BackgroundThreadErrorFilter,
1490
- _ts_metadata9("design:type", Function),
1491
- _ts_metadata9("design:paramtypes", []),
1492
- _ts_metadata9("design:returntype", void 0)
1493
- ], SimpleContentContext.prototype, "onAllContentReceived", null);
1494
-
1495
- // src/threads/content/stream/stream-content-context-worker-types.ts
1496
- var streamContentContextWorkerClassId = "str_wrk";
1497
- var streamWorkerExits = {
1498
- chunk: "chunk"
1499
- };
1500
-
1501
- // src/threads/content/stream/stream-content-context-worker.ts
1502
- function _ts_decorate10(decorators, target, key, desc) {
1503
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1504
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1505
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1506
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1507
- }
1508
- __name(_ts_decorate10, "_ts_decorate");
1509
- function _ts_metadata10(k, v) {
1510
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1511
- }
1512
- __name(_ts_metadata10, "_ts_metadata");
1513
- var StreamContentContextWorker = class extends BaseStep {
1514
- static {
1515
- __name(this, "StreamContentContextWorker");
1516
- }
1517
- static class = streamContentContextWorkerClassId;
1518
- static async start(thread, event) {
1519
- await runChildThread(thread, {
1520
- id: this.getThreadId(thread.step.id),
1521
- state: {
1522
- step: thread.step.id,
1523
- class: this.class,
1524
- event
1525
- }
1526
- });
1527
- }
1528
- static isBusy(thread) {
1529
- return isThreadBusy(thread, this.getThreadId(thread.step.id));
1530
- }
1531
- static getThreadId(stepId) {
1532
- return `${baseClassIdPrefix}_${this.class}_${stepId}`;
1533
- }
1534
- // ----------------------------------------------
1535
- runStep() {
1536
- if ("event" in this.state === false || this.state.event == void 0) {
1537
- throw new CreateChatCompletionStepError("Stream worker thread state must contain an event to process", {
1538
- code: ErrorCode.STEP_LOGIC_ISSUE
1539
- });
1540
- }
1541
- const chunk = this.state.event;
1542
- this.log.DEBUG?.("Executing stream worker thread", chunk);
1543
- this.exitStep(streamWorkerExits.chunk, {
1544
- chunk
1545
- });
1546
- }
1547
- };
1548
- _ts_decorate10([
1549
- ErrorFilter,
1550
- _ts_metadata10("design:type", Function),
1551
- _ts_metadata10("design:paramtypes", []),
1552
- _ts_metadata10("design:returntype", void 0)
1553
- ], StreamContentContextWorker.prototype, "runStep", null);
1554
-
1555
- // src/threads/content/stream/stream-content-context.ts
1556
- import { ACTION as ACTION5 } from "@onereach/flow-sdk/types/index.js";
1557
- import { clear as clear3, Memoize as Memoize4 } from "typescript-memoize";
1558
-
1559
- // src/threads/content/stream/schemas/stream-chunk.ts
1560
- import { z as z4 } from "zod/v4-mini";
1561
- var streamChunkEventSchema = z4.object({
1562
- name: z4.string(),
1563
- params: z4.object({
1564
- content: z4.object({
1565
- id: z4.string(),
1566
- object: z4.literal("chat.completion.chunk"),
1567
- created: z4.number(),
1568
- model: z4.string(),
1569
- service_tier: z4.optional(z4.union([
1570
- z4.literal("default"),
1571
- z4.literal("auto"),
1572
- z4.literal("flex"),
1573
- z4.literal("scale"),
1574
- z4.literal("priority"),
1575
- z4.null()
1576
- ])),
1577
- system_fingerprint: z4.optional(z4.string()),
1578
- choices: z4.array(z4.object({
1579
- index: z4.number(),
1580
- delta: z4.object({
1581
- role: z4.optional(z4.enum([
1582
- "developer",
1583
- "system",
1584
- "user",
1585
- "assistant",
1586
- "tool"
1587
- ])),
1588
- content: z4.nullish(z4.string()),
1589
- refusal: z4.nullish(z4.string()),
1590
- tool_calls: z4.optional(z4.array(z4.object({
1591
- index: z4.number().check(z4.int(), z4.gte(0)),
1592
- id: z4.optional(z4.string()),
1593
- type: z4.optional(z4.literal("function")),
1594
- function: z4.object({
1595
- name: z4.optional(z4.string()),
1596
- arguments: z4.optional(z4.string())
1597
- })
1598
- })))
1599
- }),
1600
- logprobs: z4.optional(z4.any()),
1601
- finish_reason: z4.union([
1602
- z4.literal("stop"),
1603
- z4.literal("length"),
1604
- z4.literal("tool_calls"),
1605
- z4.literal("content_filter"),
1606
- z4.null()
1607
- ])
1608
- })),
1609
- logprobs: z4.nullish(z4.object({
1610
- content: z4.nullable(z4.array(z4.looseObject({}))),
1611
- refusal: z4.nullable(z4.array(z4.looseObject({})))
1612
- })),
1613
- usage: z4.nullish(z4.object({
1614
- prompt_tokens: z4.number(),
1615
- completion_tokens: z4.number(),
1616
- total_tokens: z4.number(),
1617
- completion_tokens_details: z4.optional(z4.object({
1618
- accepted_prediction_tokens: z4.optional(z4.number()),
1619
- audio_tokens: z4.optional(z4.number()),
1620
- reasoning_tokens: z4.optional(z4.number()),
1621
- rejected_prediction_tokens: z4.optional(z4.number())
1622
- })),
1623
- prompt_tokens_details: z4.optional(z4.object({
1624
- audio_tokens: z4.optional(z4.number()),
1625
- cached_tokens: z4.optional(z4.number())
1626
- }))
1627
- }))
1628
- })
1629
- })
1630
- });
1631
-
1632
- // src/threads/content/stream/services/stream-reducer.ts
1633
- var StreamReducerService = class {
1634
- static {
1635
- __name(this, "StreamReducerService");
1636
- }
1637
- _result;
1638
- _onToolCall;
1639
- constructor(memo, onToolCall) {
1640
- this._result = memo;
1641
- this._onToolCall = onToolCall;
1642
- }
1643
- reduceChunk(chunk) {
1644
- if (Object.keys(this._result).length === 0) {
1645
- const { choices, ...rest } = chunk;
1646
- Object.assign(this._result, rest);
1647
- if (this._result.object != void 0) {
1648
- this._result.object = "chat.completion";
1649
- }
1650
- }
1651
- if (chunk.usage) {
1652
- this._result.usage = chunk.usage;
1653
- }
1654
- for (const choice of chunk.choices) {
1655
- let existing = this._result.choices?.[choice.index];
1656
- if (!existing) {
1657
- const { delta, ...rest } = choice;
1658
- existing = rest;
1659
- this._result.choices ??= [];
1660
- this._result.choices[choice.index] = existing;
1661
- }
1662
- if (choice.finish_reason != void 0) {
1663
- existing.finish_reason = choice.finish_reason;
1664
- }
1665
- if (choice.logprobs != void 0) {
1666
- existing.logprobs = choice.logprobs;
1667
- }
1668
- if (choice.delta?.content !== void 0) {
1669
- const { content, ...delta } = choice.delta;
1670
- existing.message ??= delta;
1671
- if (content === null) {
1672
- existing.message.content ??= null;
1673
- } else {
1674
- existing.message.content ??= "";
1675
- existing.message.content += content;
1676
- }
1677
- }
1678
- if (choice.delta?.tool_calls != void 0) {
1679
- const { tool_calls, ...delta } = choice.delta;
1680
- existing.message ??= delta;
1681
- existing.message.tool_calls ??= [];
1682
- for (const deltaToolCall of choice.delta.tool_calls) {
1683
- const index = deltaToolCall.index;
1684
- if (existing.message.tool_calls[index] == void 0) {
1685
- existing.message.tool_calls[index] = structuredClone(deltaToolCall);
1686
- } else {
1687
- const toolCall = existing.message.tool_calls[index];
1688
- if (!("function" in toolCall) || toolCall.function == void 0) continue;
1689
- toolCall.function.arguments += deltaToolCall.function?.arguments ?? "";
1690
- }
1691
- if (choice.index === 0) {
1692
- this.tryParseToolCall(existing.message.tool_calls[index]);
1693
- }
1694
- }
1695
- }
1696
- }
1697
- }
1698
- tryParseToolCall(toolCall) {
1699
- try {
1700
- if (!("function" in toolCall) || toolCall.function == void 0) return;
1701
- const parsedArguments = JSON.parse(toolCall.function.arguments);
1702
- this._onToolCall(toolCall, parsedArguments);
1703
- } catch {
1704
- }
1705
- }
1706
- };
1707
-
1708
- // src/threads/content/stream/stream-content-context-types.ts
1709
- var streamContentContextClassId = "cnt_cxt_str";
1710
-
1711
- // src/threads/content/stream/stream-content-context.ts
1712
- function _ts_decorate11(decorators, target, key, desc) {
1713
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1714
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1715
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1716
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1717
- }
1718
- __name(_ts_decorate11, "_ts_decorate");
1719
- function _ts_metadata11(k, v) {
1720
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1721
- }
1722
- __name(_ts_metadata11, "_ts_metadata");
1723
- var memoizeTag3 = [
1724
- "streamInit"
1725
- ];
1726
- var StreamContentContext = class _StreamContentContext extends AbstractContentContext {
1727
- static {
1728
- __name(this, "StreamContentContext");
1729
- }
1730
- static class = streamContentContextClassId;
1731
- static getInitialState() {
1732
- return {
1733
- class: _StreamContentContext.class,
1734
- requestComplete: false,
1735
- workerComplete: false,
1736
- partialChatCompletion: {},
1737
- events: [],
1738
- lastChunkReceived: false,
1739
- index: -1
1740
- };
1741
- }
1742
- // ---------------------------------------
1743
- async runStep() {
1744
- await super.runStep();
1745
- clear3(memoizeTag3);
1746
- this.triggers.hook({
1747
- name: ACTION5.end,
1748
- thread: StreamContentContextWorker.getThreadId(this.thread.step.id)
1749
- }, this.onWorkerThreadEnd);
1750
- }
1751
- /**
1752
- * Handler of the worker thread end event.
1753
- */
1754
- onWorkerThreadEnd(event) {
1755
- const { result } = event.params.thread;
1756
- this.log.DEBUG?.(`${StreamContentContextWorker.name} thread finished processing chunk`, result);
1757
- if (result instanceof Error) {
1758
- const resultError = errorFilter(result, {
1759
- unknownMessage: `${StreamContentContextWorker.name} thread ended with an error`
1760
- });
1761
- throw resultError;
1762
- }
1763
- if (result?.status === "cancel") return;
1764
- this.tryRunWorker();
1765
- }
1766
- /**
1767
- * Handles a single chunk of the streaming response from the OpenAI API.
1768
- */
1769
- onHandleContent(event) {
1770
- const parsedEvent = streamChunkEventSchema.parse(event);
1771
- const chunk = parsedEvent.params.content;
1772
- const streamReducer = new StreamReducerService(this.state.partialChatCompletion, this.onToolCall.bind(this));
1773
- streamReducer.reduceChunk(chunk);
1774
- for (const chunkChoice of chunk.choices) {
1775
- this.log.DEBUG?.("LLM stream response chunk", chunkChoice);
1776
- if (chunkChoice.index > 0) continue;
1777
- const content = chunkChoice.delta?.content;
1778
- if (content != void 0 && content.length > 0) {
1779
- this.addContentToQueue(content);
1780
- this.scheduleNextContentEvent();
1781
- }
1782
- }
1783
- }
1784
- /**
1785
- * Handles the notification that all response chunks were received from the OpenAI API.
1786
- */
1787
- onAllContentReceived() {
1788
- this.log.DEBUG?.("All LLM response chunks were received. No more response chunks expected");
1789
- this.state.chatCompletion = this.state.partialChatCompletion;
1790
- this.handleMultipleChoices();
1791
- this.state.events.push({
1792
- lastChunkReceived: true
1793
- });
1794
- this.state.lastChunkReceived = true;
1795
- this.tryRunWorker();
1796
- }
1797
- onToolCall(toolCall) {
1798
- this.log.DEBUG?.("Tool call received", toolCall);
1799
- void this.callFunction(toolCall);
1800
- }
1801
- /**
1802
- * Tries to flush the queue of response chunks to the controller thread.
1803
- * If the first event in the queue is not full, it waits for a debounce duration before flushing.
1804
- * If the first event is full, it flushes it immediately.
1805
- */
1806
- scheduleNextContentEvent() {
1807
- const firstEvent = this.state.events[0];
1808
- if (firstEvent == void 0 || !this.isEventReady(firstEvent) || "lastChunkReceived" in firstEvent) return;
1809
- this.tryRunWorker();
1810
- }
1811
- /**
1812
- * Compacts the content into the controller thread's events queue.
1813
- * If the content is too long, it splits it into multiple events.
1814
- * If the last event is not full, it extends it with the new content.
1815
- */
1816
- addContentToQueue(content) {
1817
- const lastEvent = this.state.events.at(-1);
1818
- if (lastEvent != void 0) {
1819
- if ("lastChunkReceived" in lastEvent) {
1820
- this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");
1821
- return;
1822
- }
1823
- if (lastEvent.content.length < this.maxChunkContentLength) {
1824
- const mergedChunk = `${lastEvent.content}${content}`;
1825
- lastEvent.content = mergedChunk.slice(0, this.maxChunkContentLength);
1826
- this.log.DEBUG?.("Compacting response chunk into last queue event", lastEvent);
1827
- content = mergedChunk.slice(this.maxChunkContentLength);
1828
- }
1829
- }
1830
- while (content) {
1831
- this.state.index += 1;
1832
- const event = {
1833
- // TODO: might be an issue with streaming to TTS
1834
- content: content.slice(0, this.maxChunkContentLength),
1835
- index: this.state.index,
1836
- ts: Date.now()
1837
- };
1838
- this.log.DEBUG?.("Add new event into the queue", event);
1839
- this.state.events.push(event);
1840
- content = content.slice(this.maxChunkContentLength);
1841
- }
1842
- }
1843
- /**
1844
- * Processes the next event in the controller thread's queue.
1845
- * If the worker thread is busy, it skips processing.
1846
- * If the event is the termination marker, it notifies the end of the controller thread.
1847
- * If the event is ready for processing, it starts the worker thread to handle it.
1848
- */
1849
- tryRunWorker() {
1850
- if (StreamContentContextWorker.isBusy(this.thread)) {
1851
- this.log.DEBUG?.("Skip event processing. Worker thread is busy");
1852
- return;
1853
- }
1854
- const event = this.getEventToProcess();
1855
- if (event == void 0) return;
1856
- if ("lastChunkReceived" in event) {
1857
- this.log.DEBUG?.(`Stopping ${_StreamContentContext.name} thread. All response content chunks were processed and no new chunks are coming`);
1858
- this.state.workerComplete = true;
1859
- this.tryToComplete();
1860
- return;
1861
- }
1862
- this.log.DEBUG?.("Notify stream worker thread about response chunk to process", event);
1863
- void StreamContentContextWorker.start(this.thread, event);
1864
- }
1865
- isEventReady(event) {
1866
- if (event == void 0) return false;
1867
- if ("lastChunkReceived" in event) return true;
1868
- if (event.content == void 0) return false;
1869
- const eventDelay = Date.now() - event.ts;
1870
- return this.state.lastChunkReceived || eventDelay >= this.debounceDuration || event.content.length >= this.maxChunkContentLength;
1871
- }
1872
- getEventToProcess() {
1873
- const firstEvent = this.state.events[0];
1874
- if (firstEvent == void 0) return void 0;
1875
- if (!this.isEventReady(firstEvent)) return void 0;
1876
- if ("lastChunkReceived" in firstEvent) return firstEvent;
1877
- this.state.events.shift();
1878
- return firstEvent;
1879
- }
1880
- tryToComplete() {
1881
- const { chatCompletion, toolsReady, requestComplete, workerComplete } = this.state;
1882
- if (!workerComplete || !requestComplete || toolsReady === false || chatCompletion == void 0) return;
1883
- const completionMessage = chatCompletion.choices?.[0]?.message;
1884
- if (completionMessage == void 0) {
1885
- throw new CreateChatCompletionStepError("Missing completion message in chat completion result", {
1886
- code: ErrorCode.STEP_LOGIC_ISSUE
1887
- });
1888
- }
1889
- this.complete();
1890
- }
1891
- get maxChunkContentLength() {
1892
- return this.data.streamSettings?.maxBufferLength ?? 100;
1893
- }
1894
- get debounceDuration() {
1895
- return this.data.streamSettings?.debounceDuration ?? 200;
1896
- }
1897
- };
1898
- _ts_decorate11([
1899
- BackgroundThreadErrorFilter,
1900
- _ts_metadata11("design:type", Function),
1901
- _ts_metadata11("design:paramtypes", []),
1902
- _ts_metadata11("design:returntype", Promise)
1903
- ], StreamContentContext.prototype, "runStep", null);
1904
- _ts_decorate11([
1905
- BackgroundThreadErrorFilter,
1906
- _ts_metadata11("design:type", Function),
1907
- _ts_metadata11("design:paramtypes", [
1908
- typeof IActionEvent === "undefined" ? Object : IActionEvent
1909
- ]),
1910
- _ts_metadata11("design:returntype", void 0)
1911
- ], StreamContentContext.prototype, "onWorkerThreadEnd", null);
1912
- _ts_decorate11([
1913
- BackgroundThreadErrorFilter,
1914
- _ts_metadata11("design:type", Function),
1915
- _ts_metadata11("design:paramtypes", [
1916
- typeof StreamContentContextHandleContentEvent === "undefined" ? Object : StreamContentContextHandleContentEvent
1917
- ]),
1918
- _ts_metadata11("design:returntype", void 0)
1919
- ], StreamContentContext.prototype, "onHandleContent", null);
1920
- _ts_decorate11([
1921
- BackgroundThreadErrorFilter,
1922
- _ts_metadata11("design:type", Function),
1923
- _ts_metadata11("design:paramtypes", []),
1924
- _ts_metadata11("design:returntype", void 0)
1925
- ], StreamContentContext.prototype, "onAllContentReceived", null);
1926
- _ts_decorate11([
1927
- Memoize4({
1928
- tags: memoizeTag3
1929
- }),
1930
- _ts_metadata11("design:type", Number),
1931
- _ts_metadata11("design:paramtypes", [])
1932
- ], StreamContentContext.prototype, "maxChunkContentLength", null);
1933
- _ts_decorate11([
1934
- Memoize4({
1935
- tags: memoizeTag3
1936
- }),
1937
- _ts_metadata11("design:type", Number),
1938
- _ts_metadata11("design:paramtypes", [])
1939
- ], StreamContentContext.prototype, "debounceDuration", null);
1940
-
1941
- // src/step.ts
1942
- import { ACTION as ACTION6 } from "@onereach/flow-sdk/types/index.js";
1943
-
1944
- // src/threads/content/content-context-utilities.ts
1945
- import { Memoize as Memoize5 } from "typescript-memoize";
1946
- function _ts_decorate12(decorators, target, key, desc) {
1947
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1948
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1949
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1950
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1951
- }
1952
- __name(_ts_decorate12, "_ts_decorate");
1953
- function _ts_metadata12(k, v) {
1954
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1955
- }
1956
- __name(_ts_metadata12, "_ts_metadata");
1957
- var ContentContextUtilities = class _ContentContextUtilities {
1958
- static {
1959
- __name(this, "ContentContextUtilities");
1960
- }
1961
- static factory(isStreaming) {
1962
- const Constructor = isStreaming ? StreamContentContext : SimpleContentContext;
1963
- return new _ContentContextUtilities(Constructor);
1964
- }
1965
- Constructor;
1966
- constructor(Constructor) {
1967
- this.Constructor = Constructor;
1968
- }
1969
- async start(thread) {
1970
- const initialState = this.Constructor.getInitialState();
1971
- await runChildThread(thread, {
1972
- id: this.getThreadId(thread.step.id),
1973
- state: {
1974
- ...initialState,
1975
- requestComplete: false,
1976
- step: thread.step.id,
1977
- class: this.Constructor.class
1978
- },
1979
- background: true
1980
- });
1981
- }
1982
- async cancel(thread) {
1983
- await stopThread(thread, this.getThreadId(thread.step.id), {
1984
- result: {
1985
- status: "cancel"
1986
- },
1987
- stopChildThreads: true
1988
- });
1989
- }
1990
- getThreadId(stepId) {
1991
- return `${baseClassIdPrefix}_${this.Constructor.class}_${stepId}`;
1992
- }
1993
- };
1994
- _ts_decorate12([
1995
- Memoize5(),
1996
- _ts_metadata12("design:type", Function),
1997
- _ts_metadata12("design:paramtypes", [
1998
- Boolean
1999
- ]),
2000
- _ts_metadata12("design:returntype", Object)
2001
- ], ContentContextUtilities, "factory", null);
2002
-
2003
- // src/utils/validate-flow-sdk-version.ts
2004
- import { readFile } from "fs/promises";
2005
- import { createRequire } from "module";
2006
- var require2 = createRequire(import.meta.url);
2007
- async function validateFlowSdkVersion(minVersion) {
2008
- if (typeof minVersion !== "string" || !/^\d+\.\d+\.\d+$/.test(minVersion)) {
2009
- throw new Error(`Invalid minimum version format: ${minVersion}`);
2010
- }
2011
- const packageJsonPath = require2.resolve("@onereach/flow-sdk/package.json");
2012
- if (!packageJsonPath) {
2013
- throw new Error(`Could not find package.json for @onereach/flow-sdk`);
2014
- }
2015
- const packageJsonContent = await readFile(packageJsonPath, "utf8");
2016
- const { version } = JSON.parse(packageJsonContent);
2017
- const { groups } = /^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/.exec(version) || {};
2018
- if (!groups) throw new Error(`Invalid Flow SDK version format: ${version}`);
2019
- const major = Number.parseInt(groups.major ?? "0", 10);
2020
- const minor = Number.parseInt(groups.minor ?? "0", 10);
2021
- const patch = Number.parseInt(groups.patch ?? "0", 10);
2022
- if (major == void 0 || minor == void 0 || patch == void 0 || Number.isNaN(major) || Number.isNaN(minor) || Number.isNaN(patch)) {
2023
- throw new Error(`Invalid version format: ${version}`);
2024
- }
2025
- const [minMajor, minMinor, minPatch] = minVersion.split(".").map((x) => Number.parseInt(x, 10));
2026
- if (minMajor == void 0 || minMinor == void 0 || minPatch == void 0 || Number.isNaN(minMajor) || Number.isNaN(minMinor) || Number.isNaN(minPatch)) {
2027
- throw new Error(`Invalid minimum version format: ${minVersion}`);
2028
- }
2029
- if (major < minMajor || major === minMajor && minor < minMinor || major === minMajor && minor === minMinor && patch < minPatch) {
2030
- throw new Error(`Flow SDK version '${version}' is lower than required minimum version '${minVersion}' for the step`);
2031
- }
2032
- }
2033
- __name(validateFlowSdkVersion, "validateFlowSdkVersion");
2034
-
2035
- // src/step.ts
2036
- function _ts_decorate13(decorators, target, key, desc) {
2037
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2038
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2039
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2040
- return c > 3 && r && Object.defineProperty(target, key, r), r;
2041
- }
2042
- __name(_ts_decorate13, "_ts_decorate");
2043
- function _ts_metadata13(k, v) {
2044
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
2045
- }
2046
- __name(_ts_metadata13, "_ts_metadata");
2047
- var MINIMAL_FLOW_SDK_VERSION = "8.0.69";
2048
- var CreateChatCompletionStep = class extends BaseStep {
2049
- static {
2050
- __name(this, "CreateChatCompletionStep");
2051
- }
2052
- runStep() {
2053
- this.triggers.hook({
2054
- name: ACTION6.end,
2055
- thread: this.contentContext.getThreadId(this.thread.step.id)
2056
- }, this.onContentContextEnd);
2057
- this.triggers.hook({
2058
- name: ACTION6.end,
2059
- thread: HistoryContext.getThreadId(this.thread.step.id)
2060
- }, this.onHistoryContextEnd);
2061
- this.triggers.local(parentEvents.cancel, this.onCancel);
2062
- this.triggers.otherwise(this.initialize);
2063
- }
2064
- async initialize() {
2065
- await validateFlowSdkVersion(MINIMAL_FLOW_SDK_VERSION);
2066
- this.validateMergeFieldType();
2067
- this.resetState();
2068
- this.state.cancelled = false;
2069
- await Promise.all([
2070
- HistoryContext.start(this.thread),
2071
- CancelContext.start(this.thread)
2072
- ]);
2073
- await HistoryContext.prepareHistory(this.thread);
2074
- await this.contentContext.start(this.thread);
2075
- }
2076
- async onContentContextEnd(event) {
2077
- try {
2078
- const { result } = event.params.thread;
2079
- this.log.DEBUG?.("ContentContext thread ended with result", result);
2080
- if (result == void 0) {
2081
- throw new CreateChatCompletionStepError("ContentContext thread ended unexpectedly", {
2082
- code: ErrorCode.STEP_LOGIC_ISSUE
2083
- });
2084
- }
2085
- if (result instanceof Error) {
2086
- const resultError = errorFilter(result, {
2087
- unknownMessage: "ContentContext thread ended with an error",
2088
- unknownCode: ErrorCode.STEP_LOGIC_ISSUE
2089
- });
2090
- throw resultError;
2091
- }
2092
- if (result.status === "cancel") {
2093
- await this.resetStep();
2094
- return;
2095
- }
2096
- const { status, chatCompletion, toolsResults } = result;
2097
- if (status !== "ok" || chatCompletion == void 0 || toolsResults == void 0) {
2098
- throw new CreateChatCompletionStepError("Unexpected end of the ContentContext thread", {
2099
- code: ErrorCode.STEP_LOGIC_ISSUE,
2100
- data: event.params.result
2101
- });
2102
- }
2103
- this.log.DEBUG?.("Create Chat Completion result", chatCompletion);
2104
- this.state.contentReady = true;
2105
- this.state.chatCompletion = chatCompletion;
2106
- this.state.toolsResults = toolsResults;
2107
- await this.tryToExitStep();
2108
- } catch (error) {
2109
- await this.resetStep().catch(() => {
2110
- });
2111
- throw error;
2112
- }
2113
- }
2114
- async onHistoryContextEnd(event) {
2115
- try {
2116
- const { result } = event.params.thread;
2117
- this.log.DEBUG?.(`${HistoryContext.name} thread ended with result`, result);
2118
- if (result instanceof Error) {
2119
- const resultError = errorFilter(result, {
2120
- unknownMessage: `${HistoryContext.name} thread ended with an error`,
2121
- unknownCode: ErrorCode.STEP_LOGIC_ISSUE
2122
- });
2123
- throw resultError;
2124
- }
2125
- } catch (error) {
2126
- await CancelContext.stop(this.thread).catch(() => {
2127
- });
2128
- throw error;
2129
- }
2130
- }
2131
- async onCancel() {
2132
- await this.resetStep();
2133
- }
2134
- async tryToExitStep() {
2135
- const { cancelled, contentReady, chatCompletion, toolsResults } = this.state;
2136
- if (cancelled) {
2137
- this.resetState();
2138
- return;
2139
- }
2140
- if (!contentReady) return;
2141
- if (chatCompletion == void 0) {
2142
- throw new CreateChatCompletionStepError("Missing chat completion result, but request is marked as processed", {
2143
- code: ErrorCode.STEP_LOGIC_ISSUE
2144
- });
2145
- }
2146
- const completionMessage = chatCompletion.choices?.[0]?.message;
2147
- if (completionMessage == void 0) {
2148
- throw new CreateChatCompletionStepError("Missing completion message in chat completion result", {
2149
- code: ErrorCode.STEP_LOGIC_ISSUE
2150
- });
2151
- }
2152
- await Promise.all([
2153
- // add completion message and possible tool results to history
2154
- HistoryContext.appendToHistory(this.thread, [
2155
- completionMessage,
2156
- ...toolsResults
2157
- ]),
2158
- // at this point there's nothing to cancel, so CancelContext thread is not needed anymore
2159
- CancelContext.stop(this.thread)
2160
- ]);
2161
- const finishReason = chatCompletion.choices[0]?.finish_reason;
2162
- if (finishReason === "length") {
2163
- throw new CreateChatCompletionStepError("Reached limit of tokens for LLM completion response", {
2164
- code: ErrorCode.LLM_FINISH_LENGTH
2165
- });
2166
- } else if (finishReason === "tool_calls") {
2167
- this.thread.jumpTo(this.currentStepId);
2168
- return;
2169
- }
2170
- this.log.DEBUG?.("Exiting step with collected data", chatCompletion);
2171
- this.exitStep("next", chatCompletion);
2172
- }
2173
- /**
2174
- * Reset the step so it can handle new request
2175
- *
2176
- * - Reset state of initial thread (e.g. main) to original state;
2177
- * - End CancelContext thread;
2178
- * - End ContentController (with child thread);
2179
- *
2180
- * @throws if there any issues with stopping threads
2181
- */
2182
- async resetStep() {
2183
- this.resetState();
2184
- this.state.cancelled = true;
2185
- const resetResults = await Promise.allSettled([
2186
- endChildThreads(this.thread, {
2187
- status: "cancel"
2188
- }),
2189
- HistoryContext.adjustOnCancel(this.thread)
2190
- ]);
2191
- const failedTask = resetResults.find((task) => task.status === "rejected");
2192
- if (failedTask) throw failedTask.reason;
2193
- }
2194
- resetState() {
2195
- this.state.contentReady = false;
2196
- this.state.chatCompletion = void 0;
2197
- this.state.toolsResults = [];
2198
- }
2199
- validateMergeFieldType() {
2200
- const { functionsList } = this.data;
2201
- if (!Array.isArray(functionsList) || functionsList.length === 0) return;
2202
- if (this.dataOut == void 0 || this.getMergeField(this.dataOut).type !== "thread") {
2203
- throw new CreateChatCompletionStepError('Merge-field type has to be "Thread" if you are using functions', {
2204
- code: ErrorCode.VALIDATION
2205
- });
2206
- }
2207
- }
2208
- get contentContext() {
2209
- const isStreaming = this.data.streamResponse;
2210
- return ContentContextUtilities.factory(isStreaming);
2211
- }
2212
- };
2213
- _ts_decorate13([
2214
- ErrorFilter,
2215
- _ts_metadata13("design:type", Function),
2216
- _ts_metadata13("design:paramtypes", []),
2217
- _ts_metadata13("design:returntype", void 0)
2218
- ], CreateChatCompletionStep.prototype, "runStep", null);
2219
- _ts_decorate13([
2220
- ErrorFilter,
2221
- _ts_metadata13("design:type", Function),
2222
- _ts_metadata13("design:paramtypes", []),
2223
- _ts_metadata13("design:returntype", Promise)
2224
- ], CreateChatCompletionStep.prototype, "initialize", null);
2225
- _ts_decorate13([
2226
- BackgroundThreadErrorFilter({
2227
- allowHandleError: true
2228
- }),
2229
- _ts_metadata13("design:type", Function),
2230
- _ts_metadata13("design:paramtypes", [
2231
- typeof IActionEvent === "undefined" ? Object : IActionEvent
2232
- ]),
2233
- _ts_metadata13("design:returntype", Promise)
2234
- ], CreateChatCompletionStep.prototype, "onContentContextEnd", null);
2235
- _ts_decorate13([
2236
- BackgroundThreadErrorFilter({
2237
- allowHandleError: true
2238
- }),
2239
- _ts_metadata13("design:type", Function),
2240
- _ts_metadata13("design:paramtypes", [
2241
- typeof IActionEvent === "undefined" ? Object : IActionEvent
2242
- ]),
2243
- _ts_metadata13("design:returntype", Promise)
2244
- ], CreateChatCompletionStep.prototype, "onHistoryContextEnd", null);
2245
- _ts_decorate13([
2246
- ErrorFilter,
2247
- _ts_metadata13("design:type", Function),
2248
- _ts_metadata13("design:paramtypes", []),
2249
- _ts_metadata13("design:returntype", Promise)
2250
- ], CreateChatCompletionStep.prototype, "onCancel", null);
2251
-
2252
- // src/index.ts
2253
- install();
2254
- var states = {
2255
- [HistoryContext.class]: HistoryContext,
2256
- [RequestContext.class]: RequestContext,
2257
- [ToolsContext.class]: ToolsContext,
2258
- [ToolsContextWorker.class]: ToolsContextWorker,
2259
- [SimpleContentContext.class]: SimpleContentContext,
2260
- [StreamContentContext.class]: StreamContentContext,
2261
- [StreamContentContextWorker.class]: StreamContentContextWorker,
2262
- [CancelContext.class]: CancelContext
2263
- };
2264
- export {
2265
- states,
2266
- CreateChatCompletionStep as step
2267
- };
1
+ var Qe=Object.defineProperty;var l=(n,e)=>Qe(n,"name",{value:e,configurable:!0});import{install as It}from"source-map-support";import{ACTION as Xe}from"@onereach/flow-sdk/types/index.js";import xe from"@onereach/flow-sdk/errors/timeout.js";import{z as we}from"zod/v4-mini";import Te from"@onereach/flow-sdk/errors/base.js";var p={UNKNOWN:"UNKNOWN",STEP_LOGIC_ISSUE:"STEP_LOGIC_ISSUE",AUTH:"AUTH",TIMEOUT:"TIMEOUT",VALIDATION:"VALIDATION",INVALID_REQUEST:"INVALID_REQUEST",RATE_LIMITED:"RATE_LIMITED",LLM_FINISH_LENGTH:"LLM_FINISH_LENGTH",SERVER_ERROR:"SERVER_ERROR",UNSUPPORTED:"UNSUPPORTED"},h=class extends Te{static{l(this,"CreateChatCompletionStepError")}},ne=class extends Te{static{l(this,"AbortRequestError")}};var Ze=[h,xe];function x(n,e={}){let{preserveErrors:t=Ze,unknownMessage:r="Unknown error",unknownCode:i=p.UNKNOWN}=e;if(t?.some(o=>n instanceof o))return n;if(n instanceof we.core.$ZodError)return new h(we.prettifyError(n),n,{code:p.VALIDATION});if(typeof n=="string")return new h(n,{code:p.UNKNOWN});if(n instanceof Error){if(n.message.includes("Request timed out"))return new xe("Timeout",n,{code:p.TIMEOUT});let o=n?.response?.data?.error?.message;return o!=null?new h(o,n,{code:p.SERVER_ERROR}):new h(r,n,{code:i})}return new h(r,{code:i,data:n})}l(x,"errorFilter");function oe(n,e){function t(r,i,o,a){let c=n(r,i,o,a);if(typeof c=="function"){let f=a.value,w=c;if(f?.name!=null)try{Object.defineProperty(w,"name",{value:f.name})}catch{}return a.value=w,a}return c??a}return l(t,"runApply"),l(function(i,o,a){if(i!==null&&typeof i=="object"&&(typeof o=="string"||typeof o=="symbol")&&a!==null&&typeof a=="object")return t(e,i,o,a);{let c=i??e;return(f,w,m)=>t(c,f,w,m)}},"decoratorOrFactory")}l(oe,"createMethodDecoratorWithOptionalArguments");var Ye={allowHandleError:!1,useErrorFilter:!0},u=oe((n,e,t,r)=>{let i=r.value;if(!(!i||typeof i!="function"))return function(...o){try{let a=i.apply(this,o);return a instanceof Promise?a.catch(c=>be.call(this,n,c,t)):a}catch(a){return be.call(this,n,a,t)}}},Ye);function be(n,e,t){let r=t.toString();this.log.DEBUG?.(`Error in method '${r}'`,e);let i=n.useErrorFilter?x(e):e;if(i instanceof Error)if(n.allowHandleError){this.thread.enqueue({name:Xe.error,error:i});return}else{this.end(i);return}throw i}l(be,"errorHandler");function _e({thread:n,...e}){let t=pe({thread:n,...e}),r=n.process;if(r.cache[t]!=null)throw new Error(`AbortController for key '${t}' already exists`);return r.cache[t]=new AbortController}l(_e,"createAbortController");function ve({thread:n,...e}){let t=pe({thread:n,...e});return n.process.cache[t]}l(ve,"getAbortController");function z({thread:n,...e}){let t=pe({thread:n,...e});delete n.process.cache[t]}l(z,"deleteAbortController");function pe({thread:n,prefix:e,stepId:t=n.currentStepId}){return`${e?`${e}:`:""}abrt_ctrl:${t}`}l(pe,"getAbortControllerKey");import{ACTION as Re}from"@onereach/flow-sdk/types/index.js";async function k(n,{id:e,state:t,local:r,...i},{force:o}={}){let a=n.process.getThread(e);if(n.log.DEBUG?.(`runThread: thread with id '${e}'`,i),a==null)return await n.thread.runThread({...i,id:e,state:t,local:{...r,parent:n.thread.id,active:!0}});if(!o&&!a.ended&&a.local.active)throw new Error(`Thread with id '${e}' has not ended yet, cannot start it again`);if(t==null)throw new Error(`Thread with id '${e}' must have a state to run`);return a.local.active=!0,await a.enqueueAndRun({name:Re.goto,state:{...t,active:!0}})}l(k,"runChildThread");function Se(n,e){let t=n.process.getThread(e);return!!(t!=null&&t.local.active&&!t.ended)}l(Se,"isThreadBusy");function et(n,e=n.id){let t=[];for(let r in n.process.threads){let i=n.process.threads[r];i?.local.parent===e&&t.push(i)}return t}l(et,"listChildThreads");async function J(n,e,{result:t,stopChildThreads:r}={}){let i=n.process.getThread(e);n.log.DEBUG?.(`stopThread: thread with id '${e}'`),i&&(r&&await ue(i,t),i.local.active&&(i.local.active=!1,!i.ended&&await i.enqueueAndRun({name:Re.ending,result:t})))}l(J,"stopThread");async function ue(n,e,t={}){let i=et(n).map(async c=>await J(n,c.id,{stopChildThreads:!0,result:e})),o=await Promise.allSettled(i);if(t.throw??!0){let c=o.find(f=>f.status==="rejected");if(c)throw c.reason}return o}l(ue,"endChildThreads");import st from"@onereach/flow-sdk/step.js";var tt={},U=oe((n,e,t,r)=>{let i=r.value;if(!(!i||typeof i!="function"))return function(...o){try{let a=i.apply(this,o);return a instanceof Promise?a.catch(c=>{let f=t.toString();throw this.log.DEBUG?.(`Error in method '${f}'`,c),x(c)}):a}catch(a){let c=t.toString();throw this.log.DEBUG?.(`Error in method '${c}'`,a),x(a)}}},tt);import{en as nt}from"zod/v4/locales";import{z as s}from"zod/v4-mini";s.config(nt());var y=s.string().check(s.trim(),s.refine(n=>!["undefined","null"].includes(n),{error:"Unexpected undefined or null merge-field value"})),Ie=s.discriminatedUnion("role",[s.object({role:s.literal("system"),content:y}),s.object({role:s.literal("user"),content:y,name:s.optional(y.check(s.minLength(1),s.maxLength(64)))}),s.object({role:s.literal("assistant"),content:s.nullable(y),tool_calls:s.optional(s.array(s.object({id:y.check(s.minLength(1)),type:s.literal("function"),function:s.object({name:y.check(s.minLength(1)),arguments:y.check(s.minLength(1))})})))}),s.object({role:s.literal("tool"),content:y,name:s.optional(y.check(s.minLength(1),s.maxLength(64))),tool_call_id:y.check(s.minLength(1))})]),ot=s.object({type:s.literal("object"),properties:s.record(s.string().check(s.minLength(1),s.maxLength(64)),s.object({type:s.enum(["string","number","boolean","array","object"]),description:s.optional(s.string().check(s.maxLength(1e3))),items:s.optional(s.object({type:s.enum(["string","number","boolean","object"]),description:s.optional(s.string().check(s.maxLength(1e3)))}))}))}),Oe=s.object({provider:y.check(s.minLength(1)),model:y.check(s.minLength(1)),accountKey:N(y),typeOfStep:s.enum(["chat","completion"]),systemMessage:s.optional(y),userMessage:s.optional(y),switchAssistantsMessage:s._default(s.boolean(),!1),assistantsFirstMessage:s.optional(y),temperature:N(s.coerce.number().check(s.gte(0),s.lte(2))),topP:N(s.coerce.number().check(s.gte(0),s.lte(1))),maxTokens:N(s.coerce.number().check(s.int(),s.gte(1))),presencePenalty:N(s.coerce.number().check(s.gte(-2),s.lte(2))),frequencyPenalty:N(s.coerce.number().check(s.gte(-2),s.lte(2))),n:N(s.coerce.number().check(s.int(),s.gte(1),s.lte(128))),stop:s.optional(s.array(s.object({token:s.string().check(s.minLength(1),s.maxLength(64))})).check(s.maxLength(4))),streamResponse:s._default(s.boolean(),!1),streamSettings:s.optional(s.object({maxBufferLength:s.coerce.number().check(s.overwrite(n=>n===0?50:n),s.int(),s.gte(1),s.lte(32768)),debounceDuration:s.coerce.number().check(s.int(),s.gte(0),s.lte(1e4))})),customHistory:s._default(s.boolean(),!1),historyLength:N(s.coerce.number().check(s.int(),s.gte(1),s.lte(100))),historyMode:s.optional(s.enum(["mergefield","codeHistory"])),historyMergefield:s.optional(s.array(Ie)),historyCode:s.optional(s.array(Ie)),functionsList:s.array(s.object({name:y.check(s.minLength(1),s.maxLength(64)),description:s.optional(y.check(s.maxLength(1e3))),parameters:s.optional(s.pipe(ot,s.transform(n=>(n.required=Object.keys(n.properties),n.additionalProperties=!1,n)))),strict:s.boolean()})),processError:s.boolean(),processTimeout:s.boolean(),timeoutDuration:y.check(s.trim(),s.minLength(1))}).check(s.refine(n=>!n.streamResponse||n.provider==="openai",{error:"Streaming is only supported for OpenAI provider"}),s.refine(n=>!n.streamResponse||n.streamSettings!==void 0,{error:'Missing "Stream settings" for response streaming mode'}),s.refine(n=>n.typeOfStep!=="chat"||n.customHistory||!n.switchAssistantsMessage||n.assistantsFirstMessage!==void 0,{error:`Missing "Assistant's first message" value`}),s.refine(n=>n.customHistory||n.userMessage!==void 0,{error:'Missing "User message" value'}),s.refine(n=>n.typeOfStep!=="chat"||!n.customHistory||n.historyMode!==void 0,{error:'Missing "History mode" value'}),s.refine(n=>n.typeOfStep!=="chat"||!n.customHistory||n.historyMode!=="mergefield"||n.historyMergefield!==void 0,{error:'Missing "History" data via merge-field'}),s.refine(n=>n.typeOfStep!=="chat"||!n.customHistory||n.historyMode!=="mergefield"||n.historyMergefield?.find?.(e=>e.role==="user"),{error:'Missing at least one message with role "user" message in "History" data via merge-field'}),s.refine(n=>n.typeOfStep!=="chat"||!n.customHistory||n.historyMode!=="codeHistory"||n.historyCode!==void 0,{error:'Missing "History" data as code'}),s.refine(n=>n.typeOfStep!=="chat"||!n.customHistory||n.historyMode!=="codeHistory"||n.historyCode?.find?.(e=>e.role==="user"),{error:'Missing at least one message with role "user" message in "History" data via code'}));function N(n){return s.pipe(s.union([s.undefined(),s.literal(""),n]),s.transform(e=>e===""?void 0:e))}l(N,"optionalStringValue");function rt(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(rt,"_ts_decorate");function he(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(he,"_ts_metadata");var C=class extends st{static{l(this,"BaseStep")}async resolveDataIn(){let e=await super.resolveDataIn();return this.log.DEBUG?.("data in before validation",e),Oe.parse(e)}};rt([U,he("design:type",Function),he("design:paramtypes",[]),he("design:returntype",Promise)],C.prototype,"resolveDataIn",null);var I="llm_cmp";var ke="cncl_ctrl",re={cancel:"st_cancel"},je={cancel:"cnl_cancel"};function fe(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(fe,"_ts_decorate");function D(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(D,"_ts_metadata");var j=class n extends C{static{l(this,"CancelContext")}static class=ke;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 J(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${I}_${this.class}_${e}`}runStep(){this.triggers.local({name:je.cancel},this.onCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${n.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 r=ve({thread:this.thread});r?r.abort(new ne("Request cancelled")):this.process.enqueueAndRun({name:re.cancel,thread:e})}};fe([u,D("design:type",Function),D("design:paramtypes",[]),D("design:returntype",void 0)],j.prototype,"runStep",null);fe([u,D("design:type",Function),D("design:paramtypes",[]),D("design:returntype",void 0)],j.prototype,"initialize",null);fe([u,D("design:type",Function),D("design:paramtypes",[]),D("design:returntype",void 0)],j.prototype,"onCancel",null);import{ACTION as He}from"@onereach/flow-sdk/types/index.js";import{setImmediate as gt}from"timers/promises";import{clear as yt,Memoize as Ct}from"typescript-memoize";import{clear as ht,Memoize as Ue}from"typescript-memoize";var Ae="hst_ctrl",B={appendToHistory:"hst_append",prepareHistory:"hst_prepare",adjustOnCancel:"hst_cncl"};function Q(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(Q,"_ts_decorate");function b(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(b,"_ts_metadata");var g=class n extends C{static{l(this,"HistoryContext")}static class=Ae;static async start(e){let t=this.getThreadId(e.step.id);e.process.getThread(t)==null&&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:B.prepareHistory})}static async appendToHistory(e,t){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:B.appendToHistory,params:{message:t}})}static async adjustOnCancel(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:B.adjustOnCancel})}static getHistory(e){return e.process.getSafeThread(this.getThreadId(e.step.id)).state.messages}static getThreadId(e){return`${I}_${this.class}_${e}`}runStep(){this.triggers.local(B.prepareHistory,this.onPrepareHistory).local(B.appendToHistory,this.onAppendToHistory).local(B.adjustOnCancel,this.onAdjustOnCancel).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${n.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 t=this.state.messages.pop();this.log.DEBUG?.("Removed message from history",t)}}onPrepareHistory(){this.log.DEBUG?.("Preparing history");let{customHistory:e,historyLength:t,systemMessage:r,typeOfStep:i,userMessage:o,historyMode:a,historyMergefield:c,historyCode:f,assistantsFirstMessage:w}=this.data;if(i==="completion"){this.state.messages=[{role:"user",content:o}];return}if(e){this.state.messages=a==="mergefield"?c:f;return}let m=this.state.messages;if(m.length===0&&w!=null&&w.length>0&&m.push({role:"assistant",content:w}),m.at(-1)?.role!=="tool"&&m.push({role:"user",content:o}),!m.some(P=>P.role==="tool")&&t!=null&&t>0&&(m=m.slice(-1*t)),r!=null&&r.length>0){let P={role:"system",content:r};m[0]?.role==="system"?m[0]=P:m.unshift(P)}this.state.messages=m}onAppendToHistory(e){this.log.DEBUG?.("Appending message to history",e.params);let{message:t}=e.params,r=Array.isArray(t)?t:[t];this.state.messages.push(...r)}};Q([u,b("design:type",Function),b("design:paramtypes",[]),b("design:returntype",void 0)],g.prototype,"runStep",null);Q([u,b("design:type",Function),b("design:paramtypes",[]),b("design:returntype",void 0)],g.prototype,"initialize",null);Q([u,b("design:type",Function),b("design:paramtypes",[]),b("design:returntype",void 0)],g.prototype,"onAdjustOnCancel",null);Q([u,b("design:type",Function),b("design:paramtypes",[]),b("design:returntype",void 0)],g.prototype,"onPrepareHistory",null);Q([u,b("design:type",Function),b("design:paramtypes",[typeof HistoryContextEventAppendToHistory>"u"?Object:HistoryContextEventAppendToHistory]),b("design:returntype",void 0)],g.prototype,"onAppendToHistory",null);import{BasicThreadService as it}from"@onereach/flow-sdk/services/basic.js";import{setImmediate as at}from"timers/promises";var se={handleContent:"hnd_cnt"};var W=class extends it{static{l(this,"AbstractResponseHandler")}async handleContentByParentThread(e){await at(),await this.thread.process.enqueueAndRun({thread:this.thread.local.parent,name:se.handleContent,params:{content:e}})}};var ie=class extends W{static{l(this,"SimpleResponseHandler")}async handle(e){this.log.DEBUG?.("Start processing response from LLM"),await this.handleContentByParentThread(e),z({thread:this.thread})}};var ae=class extends W{static{l(this,"StreamResponseHandler")}async handle(e){this.log.DEBUG?.("Start processing stream of chunks from LLM");for await(let t of e)await this.handleContentByParentThread(t);z({thread:this.thread})}};function Pe({thread:n,isStreaming:e}){return e?new ae(n):new ie(n)}l(Pe,"responseHandlerFactory");var Le="rqst_ctx";import dt from"openai";import pt from"timestring";import{Memoize as ut}from"typescript-memoize";import{z as ce}from"zod/v4-mini";import{BasicThreadService as lt}from"@onereach/flow-sdk/services/basic.js";var le=class extends lt{static{l(this,"ThreadServiceWithData")}data;constructor(e,t){super(e),this.data=t}};function ct(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(ct,"_ts_decorate");function Me(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(Me,"_ts_metadata");var $=class extends le{static{l(this,"OpenAIRequestService")}async makeOpenAiRequest(e,t){let{model:r,temperature:i,topP:o,maxTokens:a,frequencyPenalty:c,presencePenalty:f,n:w,streamResponse:m}=this.data,q={messages:structuredClone(e),model:r,temperature:i,top_p:o,max_tokens:a,frequency_penalty:c,presence_penalty:f,stop:this.stopSequences,n:w,stream:m,tools:this.tools};this.log.DEBUG?.("LLM request",q);try{let P=await this.openAiClient.chat.completions.create(q,{signal:t});return m||this.log.DEBUG?.("LLM response",P),P}catch(P){throw P instanceof Error?new h("Chat completion request failed",P,{code:p.SERVER_ERROR,data:q}):P}}get tools(){let{functionsList:e}=this.data;if(!(!Array.isArray(e)||e.length===0))return e.map(t=>({type:"function",function:{name:t.name,description:t.description,parameters:!t.parameters||Object.keys(t.parameters).length===0?void 0:t.parameters,strict:t.strict}}))}get openAiClient(){let{provider:e,accountKey:t}=this.data,r={baseURL:`${this.config.env.OPENAI_API_URL}/${e}`,apiKey:"",defaultHeaders:{Authorization:this.config.authorization,"x-or-session-id":this.session.get(["reporting","sessionId"]),...t?{"x-or-account-token-name":t}:void 0},timeout:this.timeoutMs};return this.log.DEBUG?.("OpenAI client options",r),new dt(r)}get stopSequences(){return(this.data.stop??[]).map(t=>t.token).filter(t=>t.length>0)}get timeoutMs(){let e=pt(this.data.timeoutDuration,"ms");return ce.number().check(ce.int(),ce.gte(1e3),ce.lte(6e5)).parse(e)}};ct([ut(),Me("design:type",Number),Me("design:paramtypes",[])],$.prototype,"timeoutMs",null);function me(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(me,"_ts_decorate");function G(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(G,"_ts_metadata");var De=["requestInit"],_=class extends C{static{l(this,"RequestContext")}static class=Le;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`${I}_${this.class}_${e}`}async runStep(){ht(De);let e=_e({thread:this.thread});try{let t=await this.openAiService.makeOpenAiRequest(this.history,e.signal);await this.responseHandler.handle(t),this.complete()}catch(t){if(e?.signal.aborted)return this.cancel();throw t}finally{z({thread:this.thread})}}cancel(){this.end({status:"cancel"})}complete(){this.end({status:"ok"})}get history(){return g.getHistory(this.thread)}get openAiService(){return new $(this.thread,this.data)}get responseHandler(){let e=this.data.streamResponse;return Pe({thread:this.thread,isStreaming:e})}};me([u,G("design:type",Function),G("design:paramtypes",[]),G("design:returntype",Promise)],_.prototype,"runStep",null);me([Ue(),G("design:type",typeof $>"u"?Object:$),G("design:paramtypes",[])],_.prototype,"openAiService",null);me([Ue({tags:De}),G("design:type",typeof AbstractResponseHandler>"u"?Object:AbstractResponseHandler),G("design:paramtypes",[])],_.prototype,"responseHandler",null);import{ACTION as mt}from"@onereach/flow-sdk/types/index.js";var Fe="tl_ctrl",Z={callTool:"tl_run_call",lastToolCallReceived:"tl_rsp_end"};var Ne="tl_wrk";function ft(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(ft,"_ts_decorate");function ge(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(ge,"_ts_metadata");var L=class extends C{static{l(this,"ToolsContextWorker")}static class=Ne;static async start(e,t){let r=JSON.parse(t.function?.arguments);await k(e,{id:this.getThreadId(t),state:{step:e.step.id,class:this.class},local:{toolCall:t,parsedArguments:r,parent:e.id,active:!0}})}static getThreadId(e){return`${I}_${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"}};ft([U,ge("design:type",Function),ge("design:paramtypes",[]),ge("design:returntype",void 0)],L.prototype,"runStep",null);function X(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(X,"_ts_decorate");function v(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(v,"_ts_metadata");var E=class n extends C{static{l(this,"ToolsContext")}static class=Fe;static async start(e){let t=this.getThreadId(e.step.id);await k(e,{id:t,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:Z.callTool,params:{toolCall:t}})}static async lastToolCallReceived(e){await e.process.enqueueAndRun({thread:this.getThreadId(e.step.id),name:Z.lastToolCallReceived})}static getThreadId(e){return`${I}_${this.class}_${e}`}runStep(){this.triggers.local(Z.callTool,this.onCallTool).local(Z.lastToolCallReceived,this.onLastToolCallReceived).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${n.name} thread`)}onCallTool(e){let{toolCall:t}=e.params;if(t.type!=="function")throw new h("Only function tools are supported at the moment",{code:p.UNSUPPORTED});this.state.tasks[t.id]={call:t,status:"pending"},this.startToolWorkerThread(t)}onLastToolCallReceived(){this.log.DEBUG?.("No more tool calls expected",this.state.tasks),this.state.expectMoreTasks=!1,this.tryEndToolsContextThread()}onWorkerThreadEnd(e){this.log.DEBUG?.("Tool caller worker thread has ended",e);let{result:t}=e.params.thread;if(t==null)throw new h(`${L.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw x(t,{unknownMessage:`${L.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});t.status!=="cancel"&&(this.log.DEBUG?.("Tool worker thread completed successfully",{toolCallId:t.toolCallId,result:t.data}),this.setToolCallResult(t.toolCallId,t.data),this.tryEndToolsContextThread())}startToolWorkerThread(e){this.triggers.hook({name:mt.end,thread:L.getThreadId(e)},this.onWorkerThreadEnd),L.start(this.thread,e)}setToolCallResult(e,t){let r=this.state.tasks[e];if(r==null)throw new h(`Unknown tool call with id '${e}'`,{code:p.STEP_LOGIC_ISSUE});r.status="completed",r.result=t}tryEndToolsContextThread(){this.state.expectMoreTasks||Object.values(this.state.tasks).some(t=>t.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)}))}};X([u,v("design:type",Function),v("design:paramtypes",[]),v("design:returntype",void 0)],E.prototype,"runStep",null);X([u,v("design:type",Function),v("design:paramtypes",[]),v("design:returntype",void 0)],E.prototype,"initialize",null);X([u,v("design:type",Function),v("design:paramtypes",[typeof ToolsContextEventCallTool>"u"?Object:ToolsContextEventCallTool]),v("design:returntype",void 0)],E.prototype,"onCallTool",null);X([u,v("design:type",Function),v("design:paramtypes",[]),v("design:returntype",void 0)],E.prototype,"onLastToolCallReceived",null);X([u,v("design:type",Function),v("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),v("design:returntype",void 0)],E.prototype,"onWorkerThreadEnd",null);function Y(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(Y,"_ts_decorate");function R(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(R,"_ts_metadata");var Be=["toolsInit"],M=class extends C{static{l(this,"AbstractContentContext")}static get class(){throw new Error('Define "class" static field')}static getInitialState(){throw new Error('Define "getInitialState" static method')}runStep(){yt(Be),this.triggers.local(se.handleContent,this.onHandleContent).hook({name:He.end,thread:_.getThreadId(this.thread.step.id)},this.onRequestContextEnd).otherwise(this.initialize)}initialize(){this.log.DEBUG?.(`Initializing ${this.constructor.name} thread`),_.start(this.thread)}async onRequestContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${_.name} thread ended with result`,t),t==null)throw new h(`${_.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw x(t,{unknownMessage:`${_.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});if(t.status==="cancel")return this.cancel();if(t.status!=="ok")throw new h(`Unexpected end of the ${_.name} thread`,{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.state.requestComplete=!0,await this.onAllContentReceived(),this.state.toolsReady!=null&&(await gt(),await E.lastToolCallReceived(this.thread)),await this.tryToComplete()}async onToolsContextEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${E.name} thread ended`,t),t==null)throw new h(`${E.name} thread ended unexpectedly`,{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw x(t,{unknownMessage:`${E.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE});if(t.status==="cancel")return;let{status:r,messages:i}=t;if(r!=="ok"||!Array.isArray(i))throw new h(`Unexpected end of the ${E.name} thread`,{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.state.toolsResults=i,this.state.toolsReady=!0,await this.tryToComplete()}async callFunction(e){this.state.toolsReady=!1,await this.initToolsContextThread(),await E.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:He.end,thread:E.getThreadId(this.thread.step.id)},this.onToolsContextEnd),await E.start(this.thread)}cancel(){this.end({status:"cancel"})}complete(){let{chatCompletion:e}=this.state;if(e==null)throw new h("Missing chat completion content to return value",{code:p.STEP_LOGIC_ISSUE});this.end({status:"ok",chatCompletion:e,toolsResults:this.state.toolsResults??[]})}};Y([u,R("design:type",Function),R("design:paramtypes",[]),R("design:returntype",Object)],M.prototype,"runStep",null);Y([u,R("design:type",Function),R("design:paramtypes",[]),R("design:returntype",void 0)],M.prototype,"initialize",null);Y([u,R("design:type",Function),R("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),R("design:returntype",Promise)],M.prototype,"onRequestContextEnd",null);Y([u,R("design:type",Function),R("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),R("design:returntype",Promise)],M.prototype,"onToolsContextEnd",null);Y([Ct({tags:Be}),R("design:type",Function),R("design:paramtypes",[]),R("design:returntype",Promise)],M.prototype,"initToolsContextThread",null);function $e(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l($e,"_ts_decorate");function K(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(K,"_ts_metadata");var F=class extends M{static{l(this,"SimpleContentContext")}static class="cnt_cxt_smp";static getInitialState(){return{requestComplete:!1}}async onHandleContent(e){let{content:t}=e.params;this.state.chatCompletion=t;let r=t.choices[0]?.message.tool_calls;if(r!=null)for(let i of r)await this.callFunction(i)}onAllContentReceived(){this.handleMultipleChoices()}tryToComplete(){let{requestComplete:e,toolsReady:t}=this.state;!e||t===!1||this.complete()}};$e([u,K("design:type",Function),K("design:paramtypes",[typeof SimpleContentContextHandleContentEvent>"u"?Object:SimpleContentContextHandleContentEvent]),K("design:returntype",Promise)],F.prototype,"onHandleContent",null);$e([u,K("design:type",Function),K("design:paramtypes",[]),K("design:returntype",void 0)],F.prototype,"onAllContentReceived",null);var Ge="str_wrk",qe={chunk:"chunk"};function Et(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(Et,"_ts_decorate");function ye(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(ye,"_ts_metadata");var A=class extends C{static{l(this,"StreamContentContextWorker")}static class=Ge;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 Se(e,this.getThreadId(e.step.id))}static getThreadId(e){return`${I}_${this.class}_${e}`}runStep(){if(!("event"in this.state)||this.state.event==null)throw new h("Stream worker thread state must contain an event to process",{code:p.STEP_LOGIC_ISSUE});let e=this.state.event;this.log.DEBUG?.("Executing stream worker thread",e),this.exitStep(qe.chunk,{chunk:e})}};Et([U,ye("design:type",Function),ye("design:paramtypes",[]),ye("design:returntype",void 0)],A.prototype,"runStep",null);import{ACTION as Tt}from"@onereach/flow-sdk/types/index.js";import{clear as wt,Memoize as Ke}from"typescript-memoize";import{z as d}from"zod/v4-mini";var ze=d.object({name:d.string(),params:d.object({content:d.object({id:d.string(),object:d.literal("chat.completion.chunk"),created:d.number(),model:d.string(),service_tier:d.optional(d.union([d.literal("default"),d.literal("auto"),d.literal("flex"),d.literal("scale"),d.literal("priority"),d.null()])),system_fingerprint:d.optional(d.string()),choices:d.array(d.object({index:d.number(),delta:d.object({role:d.optional(d.enum(["developer","system","user","assistant","tool"])),content:d.nullish(d.string()),refusal:d.nullish(d.string()),tool_calls:d.optional(d.array(d.object({index:d.number().check(d.int(),d.gte(0)),id:d.optional(d.string()),type:d.optional(d.literal("function")),function:d.object({name:d.optional(d.string()),arguments:d.optional(d.string())})})))}),logprobs:d.optional(d.any()),finish_reason:d.union([d.literal("stop"),d.literal("length"),d.literal("tool_calls"),d.literal("content_filter"),d.null()])})),logprobs:d.nullish(d.object({content:d.nullable(d.array(d.looseObject({}))),refusal:d.nullable(d.array(d.looseObject({})))})),usage:d.nullish(d.object({prompt_tokens:d.number(),completion_tokens:d.number(),total_tokens:d.number(),completion_tokens_details:d.optional(d.object({accepted_prediction_tokens:d.optional(d.number()),audio_tokens:d.optional(d.number()),reasoning_tokens:d.optional(d.number()),rejected_prediction_tokens:d.optional(d.number())})),prompt_tokens_details:d.optional(d.object({audio_tokens:d.optional(d.number()),cached_tokens:d.optional(d.number())}))}))})})});var de=class{static{l(this,"StreamReducerService")}_result;_onToolCall;constructor(e,t){this._result=e,this._onToolCall=t}reduceChunk(e){if(Object.keys(this._result).length===0){let{choices:t,...r}=e;Object.assign(this._result,r),this._result.object!=null&&(this._result.object="chat.completion")}e.usage&&(this._result.usage=e.usage);for(let t of e.choices){let r=this._result.choices?.[t.index];if(!r){let{delta:i,...o}=t;r=o,this._result.choices??=[],this._result.choices[t.index]=r}if(t.finish_reason!=null&&(r.finish_reason=t.finish_reason),t.logprobs!=null&&(r.logprobs=t.logprobs),t.delta?.content!==void 0){let{content:i,...o}=t.delta;r.message??=o,i===null?r.message.content??=null:(r.message.content??="",r.message.content+=i)}if(t.delta?.tool_calls!=null){let{tool_calls:i,...o}=t.delta;r.message??=o,r.message.tool_calls??=[];for(let a of t.delta.tool_calls){let c=a.index;if(r.message.tool_calls[c]==null)r.message.tool_calls[c]=structuredClone(a);else{let f=r.message.tool_calls[c];if(!("function"in f)||f.function==null)continue;f.function.arguments+=a.function?.arguments??""}t.index===0&&this.tryParseToolCall(r.message.tool_calls[c])}}}}tryParseToolCall(e){try{if(!("function"in e)||e.function==null)return;let t=JSON.parse(e.function.arguments);this._onToolCall(e,t)}catch{}}};var We="cnt_cxt_str";function V(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(V,"_ts_decorate");function T(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(T,"_ts_metadata");var Ce=["streamInit"],O=class n extends M{static{l(this,"StreamContentContext")}static class=We;static getInitialState(){return{class:n.class,requestComplete:!1,workerComplete:!1,partialChatCompletion:{},events:[],lastChunkReceived:!1,index:-1}}async runStep(){await super.runStep(),wt(Ce),this.triggers.hook({name:Tt.end,thread:A.getThreadId(this.thread.step.id)},this.onWorkerThreadEnd)}onWorkerThreadEnd(e){let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${A.name} thread finished processing chunk`,t),t instanceof Error)throw x(t,{unknownMessage:`${A.name} thread ended with an error`});t?.status!=="cancel"&&this.tryRunWorker()}onHandleContent(e){let r=ze.parse(e).params.content;new de(this.state.partialChatCompletion,this.onToolCall.bind(this)).reduceChunk(r);for(let o of r.choices){if(this.log.DEBUG?.("LLM stream response chunk",o),o.index>0)continue;let a=o.delta?.content;a!=null&&a.length>0&&(this.addContentToQueue(a),this.scheduleNextContentEvent())}}onAllContentReceived(){this.log.DEBUG?.("All LLM response chunks were received. No more response chunks expected"),this.state.chatCompletion=this.state.partialChatCompletion,this.handleMultipleChoices(),this.state.events.push({lastChunkReceived:!0}),this.state.lastChunkReceived=!0,this.tryRunWorker()}onToolCall(e){this.log.DEBUG?.("Tool call received",e),this.callFunction(e)}scheduleNextContentEvent(){let e=this.state.events[0];e==null||!this.isEventReady(e)||"lastChunkReceived"in e||this.tryRunWorker()}addContentToQueue(e){let t=this.state.events.at(-1);if(t!=null){if("lastChunkReceived"in t){this.log.WARN?.("A new chunk received after the `lastChunkReceived` event. Ignoring it.");return}if(t.content.length<this.maxChunkContentLength){let r=`${t.content}${e}`;t.content=r.slice(0,this.maxChunkContentLength),this.log.DEBUG?.("Compacting response chunk into last queue event",t),e=r.slice(this.maxChunkContentLength)}}for(;e;){this.state.index+=1;let r={content:e.slice(0,this.maxChunkContentLength),index:this.state.index,ts:Date.now()};this.log.DEBUG?.("Add new event into the queue",r),this.state.events.push(r),e=e.slice(this.maxChunkContentLength)}}tryRunWorker(){if(A.isBusy(this.thread)){this.log.DEBUG?.("Skip event processing. Worker thread is busy");return}let e=this.getEventToProcess();if(e!=null){if("lastChunkReceived"in e){this.log.DEBUG?.(`Stopping ${n.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",e),A.start(this.thread,e)}}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:r,workerComplete:i}=this.state;if(!i||!r||t===!1||e==null)return;if(e.choices?.[0]?.message==null)throw new h("Missing completion message in chat completion result",{code:p.STEP_LOGIC_ISSUE});this.complete()}get maxChunkContentLength(){return this.data.streamSettings?.maxBufferLength??100}get debounceDuration(){return this.data.streamSettings?.debounceDuration??200}};V([u,T("design:type",Function),T("design:paramtypes",[]),T("design:returntype",Promise)],O.prototype,"runStep",null);V([u,T("design:type",Function),T("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),T("design:returntype",void 0)],O.prototype,"onWorkerThreadEnd",null);V([u,T("design:type",Function),T("design:paramtypes",[typeof StreamContentContextHandleContentEvent>"u"?Object:StreamContentContextHandleContentEvent]),T("design:returntype",void 0)],O.prototype,"onHandleContent",null);V([u,T("design:type",Function),T("design:paramtypes",[]),T("design:returntype",void 0)],O.prototype,"onAllContentReceived",null);V([Ke({tags:Ce}),T("design:type",Number),T("design:paramtypes",[])],O.prototype,"maxChunkContentLength",null);V([Ke({tags:Ce}),T("design:type",Number),T("design:paramtypes",[])],O.prototype,"debounceDuration",null);import{ACTION as Je}from"@onereach/flow-sdk/types/index.js";import{Memoize as bt}from"typescript-memoize";function xt(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(xt,"_ts_decorate");function Ee(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(Ee,"_ts_metadata");var ee=class n{static{l(this,"ContentContextUtilities")}static factory(e){let t=e?O:F;return new n(t)}Constructor;constructor(e){this.Constructor=e}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 J(e,this.getThreadId(e.step.id),{result:{status:"cancel"},stopChildThreads:!0})}getThreadId(e){return`${I}_${this.Constructor.class}_${e}`}};xt([bt(),Ee("design:type",Function),Ee("design:paramtypes",[Boolean]),Ee("design:returntype",Object)],ee,"factory",null);import{readFile as _t}from"fs/promises";import{createRequire as vt}from"module";var Rt=vt(import.meta.url);async function Ve(n){if(typeof n!="string"||!/^\d+\.\d+\.\d+$/.test(n))throw new Error(`Invalid minimum version format: ${n}`);let e=Rt.resolve("@onereach/flow-sdk/package.json");if(!e)throw new Error("Could not find package.json for @onereach/flow-sdk");let t=await _t(e,"utf8"),{version:r}=JSON.parse(t),{groups:i}=/^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/.exec(r)||{};if(!i)throw new Error(`Invalid Flow SDK version format: ${r}`);let o=Number.parseInt(i.major??"0",10),a=Number.parseInt(i.minor??"0",10),c=Number.parseInt(i.patch??"0",10);if(o==null||a==null||c==null||Number.isNaN(o)||Number.isNaN(a)||Number.isNaN(c))throw new Error(`Invalid version format: ${r}`);let[f,w,m]=n.split(".").map(q=>Number.parseInt(q,10));if(f==null||w==null||m==null||Number.isNaN(f)||Number.isNaN(w)||Number.isNaN(m))throw new Error(`Invalid minimum version format: ${n}`);if(o<f||o===f&&a<w||o===f&&a===w&&c<m)throw new Error(`Flow SDK version '${r}' is lower than required minimum version '${n}' for the step`)}l(Ve,"validateFlowSdkVersion");function te(n,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(n,e,t,r);else for(var c=n.length-1;c>=0;c--)(a=n[c])&&(o=(i<3?a(o):i>3?a(e,t,o):a(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o}l(te,"_ts_decorate");function S(n,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(n,e)}l(S,"_ts_metadata");var St="8.0.72",H=class extends C{static{l(this,"CreateChatCompletionStep")}runStep(){this.triggers.hook({name:Je.end,thread:this.contentContext.getThreadId(this.thread.step.id)},this.onContentContextEnd),this.triggers.hook({name:Je.end,thread:g.getThreadId(this.thread.step.id)},this.onHistoryContextEnd),this.triggers.local(re.cancel,this.onCancel),this.triggers.otherwise(this.initialize)}async initialize(){await Ve(St),this.validateMergeFieldType(),this.resetState(),this.state.cancelled=!1,await Promise.all([g.start(this.thread),j.start(this.thread)]),await g.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 h("ContentContext thread ended unexpectedly",{code:p.STEP_LOGIC_ISSUE});if(t instanceof Error)throw x(t,{unknownMessage:"ContentContext thread ended with an error",unknownCode:p.STEP_LOGIC_ISSUE});if(t.status==="cancel"){await this.resetStep();return}let{status:r,chatCompletion:i,toolsResults:o}=t;if(r!=="ok"||i==null||o==null)throw new h("Unexpected end of the ContentContext thread",{code:p.STEP_LOGIC_ISSUE,data:e.params.result});this.log.DEBUG?.("Create Chat Completion result",i),this.state.contentReady=!0,this.state.chatCompletion=i,this.state.toolsResults=o,await this.tryToExitStep()}catch(t){throw await this.resetStep().catch(()=>{}),t}}async onHistoryContextEnd(e){try{let{result:t}=e.params.thread;if(this.log.DEBUG?.(`${g.name} thread ended with result`,t),t instanceof Error)throw x(t,{unknownMessage:`${g.name} thread ended with an error`,unknownCode:p.STEP_LOGIC_ISSUE})}catch(t){throw await j.stop(this.thread).catch(()=>{}),t}}async onCancel(){await this.resetStep()}async tryToExitStep(){let{cancelled:e,contentReady:t,chatCompletion:r,toolsResults:i}=this.state;if(e){this.resetState();return}if(!t)return;if(r==null)throw new h("Missing chat completion result, but request is marked as processed",{code:p.STEP_LOGIC_ISSUE});let o=r.choices?.[0]?.message;if(o==null)throw new h("Missing completion message in chat completion result",{code:p.STEP_LOGIC_ISSUE});await Promise.all([g.appendToHistory(this.thread,[o,...i]),j.stop(this.thread)]);let a=r.choices[0]?.finish_reason;if(a==="length")throw new h("Reached limit of tokens for LLM completion response",{code:p.LLM_FINISH_LENGTH});if(a==="tool_calls"){this.thread.jumpTo(this.currentStepId);return}this.log.DEBUG?.("Exiting step with collected data",r),this.exitStep("next",r)}async resetStep(){this.resetState(),this.state.cancelled=!0;let t=(await Promise.allSettled([ue(this.thread,{status:"cancel"}),g.adjustOnCancel(this.thread)])).find(r=>r.status==="rejected");if(t)throw t.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 h('Merge-field type has to be "Thread" if you are using functions',{code:p.VALIDATION})}get contentContext(){let e=this.data.streamResponse;return ee.factory(e)}};te([U,S("design:type",Function),S("design:paramtypes",[]),S("design:returntype",void 0)],H.prototype,"runStep",null);te([U,S("design:type",Function),S("design:paramtypes",[]),S("design:returntype",Promise)],H.prototype,"initialize",null);te([u({allowHandleError:!0}),S("design:type",Function),S("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),S("design:returntype",Promise)],H.prototype,"onContentContextEnd",null);te([u({allowHandleError:!0}),S("design:type",Function),S("design:paramtypes",[typeof IActionEvent>"u"?Object:IActionEvent]),S("design:returntype",Promise)],H.prototype,"onHistoryContextEnd",null);te([U,S("design:type",Function),S("design:paramtypes",[]),S("design:returntype",Promise)],H.prototype,"onCancel",null);It();var Zr={[g.class]:g,[_.class]:_,[E.class]:E,[L.class]:L,[F.class]:F,[O.class]:O,[A.class]:A,[j.class]:j};export{Zr as states,H as step};
2268
2
  //# sourceMappingURL=index.mjs.map