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

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.
@@ -0,0 +1,709 @@
1
+ import { ACTION, CONFIG, IActionEvent, IEvent, IStepId, IThread, IThreadId, LOCAL, RESULT, STATE } from "@onereach/flow-sdk/types/index.js";
2
+ import * as z from "zod/mini";
3
+ import Step from "@onereach/flow-sdk/step.js";
4
+ import { ChatCompletion, ChatCompletionChunk, ChatCompletionMessageFunctionToolCall, ChatCompletionMessageParam, ChatCompletionToolMessageParam } from "openai/resources";
5
+ import * as _$zod_v4_core0 from "zod/v4/core";
6
+ import { ChatCompletionMessageFunctionToolCall as ChatCompletionMessageFunctionToolCall$1, ChatCompletionMessageParam as ChatCompletionMessageParam$1, ChatCompletionToolMessageParam as ChatCompletionToolMessageParam$1 } from "openai/resources.js";
7
+ import { ChatCompletion as ChatCompletion$1, ChatCompletionChunk as ChatCompletionChunk$1, ChatCompletionToolMessageParam as ChatCompletionToolMessageParam$2 } from "openai/resources.mjs";
8
+
9
+ //#region src/schemas/data-in.d.ts
10
+ declare const dataInSchema: z.ZodMiniObject<{
11
+ provider: z.ZodMiniString<string>;
12
+ model: z.ZodMiniString<string>;
13
+ accountKey: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniString<string>]>, z.ZodMiniTransform<string | undefined, string | undefined>>>;
14
+ typeOfStep: z.ZodMiniEnum<{
15
+ chat: "chat";
16
+ completion: "completion";
17
+ }>;
18
+ systemMessage: z.ZodMiniOptional<z.ZodMiniString<string>>;
19
+ userMessage: z.ZodMiniOptional<z.ZodMiniString<string>>;
20
+ switchAssistantsMessage: z.ZodMiniDefault<z.ZodMiniBoolean<boolean>>;
21
+ assistantsFirstMessage: z.ZodMiniOptional<z.ZodMiniString<string>>;
22
+ temperature: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
23
+ topP: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
24
+ maxTokens: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
25
+ presencePenalty: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
26
+ frequencyPenalty: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
27
+ n: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
28
+ stop: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniObject<{
29
+ token: z.ZodMiniString<string>;
30
+ }, z.core.$strip>>>;
31
+ reasoningEffort: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniEnum<{
32
+ none: "none";
33
+ minimal: "minimal";
34
+ low: "low";
35
+ medium: "medium";
36
+ high: "high";
37
+ xhigh: "xhigh";
38
+ }>]>, z.ZodMiniTransform<"none" | "minimal" | "low" | "medium" | "high" | "xhigh" | undefined, "" | "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | undefined>>>;
39
+ streamResponse: z.ZodMiniDefault<z.ZodMiniBoolean<boolean>>;
40
+ streamSettings: z.ZodMiniOptional<z.ZodMiniObject<{
41
+ maxBufferLength: z.ZodMiniNumber<unknown>;
42
+ debounceDuration: z.ZodMiniNumber<unknown>;
43
+ }, z.core.$strip>>;
44
+ customHistory: z.ZodMiniDefault<z.ZodMiniBoolean<boolean>>;
45
+ historyLength: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniUnion<readonly [z.ZodMiniUndefined, z.ZodMiniLiteral<"">, z.ZodMiniNumber<unknown>]>, z.ZodMiniTransform<number | undefined, number | "" | undefined>>>;
46
+ historyMode: z.ZodMiniOptional<z.ZodMiniEnum<{
47
+ mergefield: "mergefield";
48
+ codeHistory: "codeHistory";
49
+ }>>;
50
+ historyMergefield: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniDiscriminatedUnion<[z.ZodMiniObject<{
51
+ role: z.ZodMiniLiteral<"system">;
52
+ content: z.ZodMiniString<string>;
53
+ }, z.core.$strip>, z.ZodMiniObject<{
54
+ role: z.ZodMiniLiteral<"user">;
55
+ content: z.ZodMiniString<string>;
56
+ name: z.ZodMiniOptional<z.ZodMiniString<string>>;
57
+ }, z.core.$strip>, z.ZodMiniObject<{
58
+ role: z.ZodMiniLiteral<"assistant">;
59
+ content: z.ZodMiniNullable<z.ZodMiniString<string>>;
60
+ tool_calls: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniObject<{
61
+ id: z.ZodMiniString<string>;
62
+ type: z.ZodMiniLiteral<"function">;
63
+ function: z.ZodMiniObject<{
64
+ name: z.ZodMiniString<string>;
65
+ arguments: z.ZodMiniString<string>;
66
+ }, z.core.$strip>;
67
+ }, z.core.$strip>>>;
68
+ }, z.core.$strip>, z.ZodMiniObject<{
69
+ role: z.ZodMiniLiteral<"tool">;
70
+ content: z.ZodMiniString<string>;
71
+ name: z.ZodMiniOptional<z.ZodMiniString<string>>;
72
+ tool_call_id: z.ZodMiniString<string>;
73
+ }, z.core.$strip>], "role">>>;
74
+ historyCode: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniDiscriminatedUnion<[z.ZodMiniObject<{
75
+ role: z.ZodMiniLiteral<"system">;
76
+ content: z.ZodMiniString<string>;
77
+ }, z.core.$strip>, z.ZodMiniObject<{
78
+ role: z.ZodMiniLiteral<"user">;
79
+ content: z.ZodMiniString<string>;
80
+ name: z.ZodMiniOptional<z.ZodMiniString<string>>;
81
+ }, z.core.$strip>, z.ZodMiniObject<{
82
+ role: z.ZodMiniLiteral<"assistant">;
83
+ content: z.ZodMiniNullable<z.ZodMiniString<string>>;
84
+ tool_calls: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniObject<{
85
+ id: z.ZodMiniString<string>;
86
+ type: z.ZodMiniLiteral<"function">;
87
+ function: z.ZodMiniObject<{
88
+ name: z.ZodMiniString<string>;
89
+ arguments: z.ZodMiniString<string>;
90
+ }, z.core.$strip>;
91
+ }, z.core.$strip>>>;
92
+ }, z.core.$strip>, z.ZodMiniObject<{
93
+ role: z.ZodMiniLiteral<"tool">;
94
+ content: z.ZodMiniString<string>;
95
+ name: z.ZodMiniOptional<z.ZodMiniString<string>>;
96
+ tool_call_id: z.ZodMiniString<string>;
97
+ }, z.core.$strip>], "role">>>;
98
+ functionsList: z.ZodMiniArray<z.ZodMiniObject<{
99
+ name: z.ZodMiniString<string>;
100
+ description: z.ZodMiniOptional<z.ZodMiniString<string>>;
101
+ parameters: z.ZodMiniOptional<z.ZodMiniPipe<z.ZodMiniObject<{
102
+ type: z.ZodMiniLiteral<"object">;
103
+ properties: z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniObject<{
104
+ type: z.ZodMiniEnum<{
105
+ string: "string";
106
+ number: "number";
107
+ boolean: "boolean";
108
+ object: "object";
109
+ array: "array";
110
+ }>;
111
+ description: z.ZodMiniOptional<z.ZodMiniString<string>>;
112
+ items: z.ZodMiniOptional<z.ZodMiniObject<{
113
+ type: z.ZodMiniEnum<{
114
+ string: "string";
115
+ number: "number";
116
+ boolean: "boolean";
117
+ object: "object";
118
+ }>;
119
+ description: z.ZodMiniOptional<z.ZodMiniString<string>>;
120
+ }, z.core.$strip>>;
121
+ }, z.core.$strip>>;
122
+ }, z.core.$strip>, z.ZodMiniTransform<{
123
+ type: "object";
124
+ properties: Record<string, {
125
+ type: "string" | "number" | "boolean" | "object" | "array";
126
+ description?: string | undefined;
127
+ items?: {
128
+ type: "string" | "number" | "boolean" | "object";
129
+ description?: string | undefined;
130
+ } | undefined;
131
+ }>;
132
+ } & {
133
+ additionalProperties: false;
134
+ required: string[];
135
+ }, {
136
+ type: "object";
137
+ properties: Record<string, {
138
+ type: "string" | "number" | "boolean" | "object" | "array";
139
+ description?: string | undefined;
140
+ items?: {
141
+ type: "string" | "number" | "boolean" | "object";
142
+ description?: string | undefined;
143
+ } | undefined;
144
+ }>;
145
+ } & {
146
+ additionalProperties: false;
147
+ required: string[];
148
+ }>>>;
149
+ strict: z.ZodMiniBoolean<boolean>;
150
+ }, z.core.$strip>>;
151
+ useState: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
152
+ stateSettings: z.ZodMiniOptional<z.ZodMiniObject<{
153
+ stateFunction: z.ZodMiniFunction<z.core.$ZodFunctionArgs, z.core.$ZodFunctionOut>;
154
+ }, z.core.$strip>>;
155
+ processError: z.ZodMiniBoolean<boolean>;
156
+ processTimeout: z.ZodMiniBoolean<boolean>;
157
+ timeoutDuration: z.ZodMiniString<string>;
158
+ }, z.core.$strip>;
159
+ type DataIn = z.infer<typeof dataInSchema>;
160
+ //#endregion
161
+ //#region src/threads/base-types.d.ts
162
+ interface BaseConfig extends CONFIG {
163
+ dataIn: DataIn;
164
+ }
165
+ //#endregion
166
+ //#region src/utils/thread-utilities.d.ts
167
+ type ManagedThreadLocal = {
168
+ active: boolean;
169
+ parent: IThreadId;
170
+ };
171
+ //#endregion
172
+ //#region src/threads/base-child-types.d.ts
173
+ type BaseChildResultCancel = {
174
+ status: 'cancel';
175
+ };
176
+ type BaseChildResult = BaseChildResultCancel | Error;
177
+ type BaseChildLocal = ManagedThreadLocal;
178
+ interface BaseChildConfig<TResult extends RESULT<CONFIG> = BaseChildResult, TLocal extends LOCAL<CONFIG> = BaseChildLocal> extends BaseConfig {
179
+ result: TResult;
180
+ local: TLocal;
181
+ }
182
+ //#endregion
183
+ //#region src/threads/cancel/cancel-context-types.d.ts
184
+ declare const cancelContextClassId = "cncl_ctrl";
185
+ declare const parentEvents: {
186
+ readonly cancel: "st_cancel";
187
+ };
188
+ type ParentEventName = typeof parentEvents;
189
+ declare const cancelContentEvents: {
190
+ readonly cancel: "cnl_cancel";
191
+ };
192
+ type CancelContentEventName = typeof cancelContentEvents;
193
+ type CancelContextEvents = IEvent<unknown, CancelContentEventName['cancel']>;
194
+ type CancelContextState = {
195
+ class: typeof cancelContextClassId;
196
+ };
197
+ interface CancelContextConfig extends BaseChildConfig {
198
+ state: CancelContextState;
199
+ events: CancelContextEvents;
200
+ }
201
+ //#endregion
202
+ //#region src/step-types.d.ts
203
+ type ChatCompletionOut = ChatCompletion;
204
+ type ChatCompletionState = {
205
+ cancelled: boolean;
206
+ contentReady: boolean;
207
+ chatCompletion?: ChatCompletion;
208
+ toolsResults: ChatCompletionToolMessageParam[];
209
+ };
210
+ type ChatCompletionCancelEvent = IEvent<unknown, ParentEventName['cancel']>;
211
+ type ChatCompletionEvent = ChatCompletionCancelEvent;
212
+ interface ChatCompletionConfig extends BaseConfig {
213
+ dataOut: ChatCompletionOut;
214
+ state: ChatCompletionState;
215
+ events: ChatCompletionEvent;
216
+ }
217
+ //#endregion
218
+ //#region src/threads/base.d.ts
219
+ declare class BaseStep<T extends BaseConfig> extends Step<T> {
220
+ resolveDataIn(): Promise<{
221
+ provider: string;
222
+ model: string;
223
+ typeOfStep: "chat" | "completion";
224
+ switchAssistantsMessage: boolean;
225
+ streamResponse: boolean;
226
+ customHistory: boolean;
227
+ functionsList: {
228
+ name: string;
229
+ strict: boolean;
230
+ description?: string | undefined;
231
+ parameters?: ({
232
+ type: "object";
233
+ properties: Record<string, {
234
+ type: "string" | "number" | "boolean" | "object" | "array";
235
+ description?: string | undefined;
236
+ items?: {
237
+ type: "string" | "number" | "boolean" | "object";
238
+ description?: string | undefined;
239
+ } | undefined;
240
+ }>;
241
+ } & {
242
+ additionalProperties: false;
243
+ required: string[];
244
+ }) | undefined;
245
+ }[];
246
+ processError: boolean;
247
+ processTimeout: boolean;
248
+ timeoutDuration: string;
249
+ accountKey?: string | undefined;
250
+ systemMessage?: string | undefined;
251
+ userMessage?: string | undefined;
252
+ assistantsFirstMessage?: string | undefined;
253
+ temperature?: number | undefined;
254
+ topP?: number | undefined;
255
+ maxTokens?: number | undefined;
256
+ presencePenalty?: number | undefined;
257
+ frequencyPenalty?: number | undefined;
258
+ n?: number | undefined;
259
+ stop?: {
260
+ token: string;
261
+ }[] | undefined;
262
+ reasoningEffort?: "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | undefined;
263
+ streamSettings?: {
264
+ maxBufferLength: number;
265
+ debounceDuration: number;
266
+ } | undefined;
267
+ historyLength?: number | undefined;
268
+ historyMode?: "mergefield" | "codeHistory" | undefined;
269
+ historyMergefield?: ({
270
+ role: "system";
271
+ content: string;
272
+ } | {
273
+ role: "user";
274
+ content: string;
275
+ name?: string | undefined;
276
+ } | {
277
+ role: "assistant";
278
+ content: string | null;
279
+ tool_calls?: {
280
+ id: string;
281
+ type: "function";
282
+ function: {
283
+ name: string;
284
+ arguments: string;
285
+ };
286
+ }[] | undefined;
287
+ } | {
288
+ role: "tool";
289
+ content: string;
290
+ tool_call_id: string;
291
+ name?: string | undefined;
292
+ })[] | undefined;
293
+ historyCode?: ({
294
+ role: "system";
295
+ content: string;
296
+ } | {
297
+ role: "user";
298
+ content: string;
299
+ name?: string | undefined;
300
+ } | {
301
+ role: "assistant";
302
+ content: string | null;
303
+ tool_calls?: {
304
+ id: string;
305
+ type: "function";
306
+ function: {
307
+ name: string;
308
+ arguments: string;
309
+ };
310
+ }[] | undefined;
311
+ } | {
312
+ role: "tool";
313
+ content: string;
314
+ tool_call_id: string;
315
+ name?: string | undefined;
316
+ })[] | undefined;
317
+ useState?: boolean | undefined;
318
+ stateSettings?: {
319
+ stateFunction: _$zod_v4_core0.$InferOuterFunctionType<_$zod_v4_core0.$ZodFunctionArgs, _$zod_v4_core0.$ZodFunctionOut>;
320
+ } | undefined;
321
+ }>;
322
+ }
323
+ //#endregion
324
+ //#region src/threads/cancel/cancel-context.d.ts
325
+ declare class CancelContext extends BaseStep<CancelContextConfig> {
326
+ static readonly class = "cncl_ctrl";
327
+ static start(thread: IThread<ChatCompletionConfig>): Promise<void>;
328
+ static stop(thread: IThread<ChatCompletionConfig>): Promise<void>;
329
+ static getThreadId(stepId: IStepId): IThreadId;
330
+ runStep(): void;
331
+ initialize(): void;
332
+ onCancel(): void;
333
+ }
334
+ //#endregion
335
+ //#region src/threads/request/request-context-types.d.ts
336
+ declare const requestContextClassId = "rqst_ctx";
337
+ type RequestContextState = {
338
+ class: typeof requestContextClassId;
339
+ };
340
+ type RequestContextResultOk = {
341
+ status: 'ok';
342
+ };
343
+ type RequestContextResult = RequestContextResultOk | RESULT<BaseChildConfig>;
344
+ interface RequestContextConfig extends BaseChildConfig<RequestContextResult> {
345
+ state: RequestContextState;
346
+ result: RequestContextResult;
347
+ }
348
+ //#endregion
349
+ //#region src/threads/tools/tools-context-types.d.ts
350
+ declare const toolsContextClassId = "tl_ctrl";
351
+ declare const toolContextEvents: {
352
+ readonly callTool: "tl_run_call";
353
+ readonly lastToolCallReceived: "tl_rsp_end";
354
+ };
355
+ type ToolContextEventName = typeof toolContextEvents;
356
+ type ToolCallTask = {
357
+ status: 'pending' | 'completed' | 'failed';
358
+ call: ChatCompletionMessageFunctionToolCall; /** result of the tool call, if completed */
359
+ result?: unknown;
360
+ };
361
+ type TasksMap = Record<ChatCompletionMessageFunctionToolCall['id'], ToolCallTask>;
362
+ type ToolsContextResultOk = {
363
+ status: 'ok';
364
+ messages: ChatCompletionToolMessageParam[];
365
+ };
366
+ type ToolsContextResult = ToolsContextResultOk | RESULT<BaseChildConfig>;
367
+ type CallToolParameters = {
368
+ toolCall: ChatCompletionMessageFunctionToolCall;
369
+ };
370
+ type ToolsContextEventCallTool = IEvent<CallToolParameters, ToolContextEventName['callTool']>;
371
+ type ToolsContextEventLastToolReceived = IEvent<unknown, ToolContextEventName['lastToolCallReceived']>;
372
+ type ToolsContextEvent = ToolsContextEventCallTool | ToolsContextEventLastToolReceived;
373
+ type ToolsContextState = {
374
+ class: typeof toolsContextClassId; /** tool calls from LLM response */
375
+ tasks: TasksMap; /** flag to indicate if there are more tasks to process */
376
+ expectMoreTasks: boolean;
377
+ };
378
+ interface ToolsContextConfig extends BaseChildConfig<ToolsContextResult> {
379
+ state: ToolsContextState;
380
+ events: ToolsContextEvent;
381
+ }
382
+ //#endregion
383
+ //#region src/threads/content/abstract-content-context-types.d.ts
384
+ declare const contentContextEvents: {
385
+ readonly handleContent: "hnd_cnt";
386
+ };
387
+ type ContentContentEventName = typeof contentContextEvents;
388
+ type ContentContextHandleContentEvent = IEvent<{
389
+ content: ChatCompletionChunk$1 | ChatCompletion$1;
390
+ }, ContentContentEventName['handleContent']>;
391
+ type ContentContextEvent = ContentContextHandleContentEvent;
392
+ type ContentContextState = {
393
+ requestComplete: boolean;
394
+ chatCompletion?: ChatCompletion$1;
395
+ /**
396
+ * Are tool calls ready?
397
+ * - `undefined` - no tools to wait for
398
+ * - `false` - tools are processing
399
+ * - `true` - tools finished processing
400
+ */
401
+ toolsReady?: boolean;
402
+ toolsResults?: ChatCompletionToolMessageParam$2[];
403
+ };
404
+ type ContentContextOkResult = {
405
+ status: 'ok';
406
+ chatCompletion: ChatCompletion$1;
407
+ toolsResults: ChatCompletionToolMessageParam$2[];
408
+ };
409
+ type ContentContextResult = ContentContextOkResult | RESULT<BaseChildConfig>;
410
+ interface ContentContextConfig extends BaseChildConfig<ContentContextResult> {
411
+ state: ContentContextState;
412
+ result: ContentContextResult;
413
+ events: ContentContextEvent;
414
+ }
415
+ //#endregion
416
+ //#region src/threads/content/abstract-content-context.d.ts
417
+ declare abstract class AbstractContentContext<T extends ContentContextConfig> extends BaseStep<T> {
418
+ static get class(): string;
419
+ static getInitialState<T extends ContentContextConfig>(): STATE<T>;
420
+ runStep(): void | Promise<void>;
421
+ initialize(): void;
422
+ abstract onHandleContent(event: ContentContextHandleContentEvent): void | Promise<void>;
423
+ protected abstract onAllContentReceived(): void | Promise<void>;
424
+ protected abstract tryToComplete(): void | Promise<void>;
425
+ onRequestContextEnd(event: IActionEvent<RequestContextConfig, ACTION.end>): Promise<void>;
426
+ onToolsContextEnd(event: IActionEvent<ToolsContextConfig, ACTION.end>): Promise<void>;
427
+ protected callFunction(toolCall: ChatCompletionMessageFunctionToolCall$1): Promise<void>;
428
+ protected handleMultipleChoices(): void;
429
+ protected initToolsContextThread(): Promise<void>;
430
+ protected cancel(): void;
431
+ protected complete(): void;
432
+ }
433
+ //#endregion
434
+ //#region src/threads/content/simple/simple-content-context-types.d.ts
435
+ type SimpleContentContextHandleContentEvent = IEvent<{
436
+ content: ChatCompletion;
437
+ }, ContentContentEventName['handleContent']>;
438
+ type SimpleContentContextEvent = SimpleContentContextHandleContentEvent;
439
+ interface SimpleContentContextConfig extends ContentContextConfig {
440
+ events: SimpleContentContextEvent;
441
+ }
442
+ //#endregion
443
+ //#region src/threads/content/simple/simple-content-context.d.ts
444
+ declare class SimpleContentContext extends AbstractContentContext<SimpleContentContextConfig> {
445
+ static readonly class = "cnt_cxt_smp";
446
+ static getInitialState(): STATE<SimpleContentContextConfig>;
447
+ onHandleContent(event: SimpleContentContextHandleContentEvent): Promise<void>;
448
+ onAllContentReceived(): void;
449
+ protected tryToComplete(): void;
450
+ }
451
+ //#endregion
452
+ //#region src/threads/content/stream/stream-content-context-types.d.ts
453
+ declare const streamContentContextClassId = "cnt_cxt_str";
454
+ type StreamContentContextHandleContentEvent = IEvent<{
455
+ content: ChatCompletionChunk;
456
+ }, ContentContentEventName['handleContent']>;
457
+ type StreamContentContextEvent = StreamContentContextHandleContentEvent;
458
+ type ContentChunkEvent = {
459
+ content: string;
460
+ index: number;
461
+ ts: number;
462
+ };
463
+ type ContentEndEvent = {
464
+ lastChunkReceived: true;
465
+ };
466
+ type ContentEvent = ContentChunkEvent | ContentEndEvent;
467
+ type StreamContentContextState = Omit<ContentContextState, 'class'> & {
468
+ class: typeof streamContentContextClassId; /** aggregated result of the LLM response */
469
+ partialChatCompletion: Partial<ChatCompletion>; /** queue of response chunks */
470
+ events: ContentEvent[]; /** last index for a chunk to be processed by worker */
471
+ index: number; /** marker for end of response stream */
472
+ lastChunkReceived: boolean; /** When `true` means worker finished the processing the queue */
473
+ workerComplete: boolean;
474
+ };
475
+ interface StreamContentContextConfig extends ContentContextConfig {
476
+ events: StreamContentContextEvent;
477
+ state: StreamContentContextState;
478
+ }
479
+ //#endregion
480
+ //#region src/threads/content/stream/stream-content-context-worker-types.d.ts
481
+ declare const streamContentContextWorkerClassId = "str_wrk";
482
+ declare const streamWorkerExits: {
483
+ readonly chunk: "chunk";
484
+ };
485
+ type ContentChunk = {
486
+ content: string;
487
+ index: number;
488
+ ts: number;
489
+ };
490
+ type StreamContentContextWorkerOut = {
491
+ chunk: ContentChunk;
492
+ };
493
+ type StreamContentContextWorkerState = {
494
+ class: typeof streamContentContextWorkerClassId;
495
+ event: ContentChunk;
496
+ };
497
+ type StreamContentContextWorkerResult = undefined | RESULT<BaseChildConfig>;
498
+ interface StreamContentContextWorkerConfig extends BaseChildConfig<StreamContentContextWorkerResult> {
499
+ dataOut: StreamContentContextWorkerOut;
500
+ state: StreamContentContextWorkerState;
501
+ }
502
+ //#endregion
503
+ //#region src/threads/content/stream/stream-content-context-worker.d.ts
504
+ declare class StreamContentContextWorker extends BaseStep<StreamContentContextWorkerConfig> {
505
+ static readonly class = "str_wrk";
506
+ static start(thread: IThread<StreamContentContextConfig>, event: ContentChunk): Promise<void>;
507
+ static isBusy(thread: IThread<StreamContentContextConfig>): boolean;
508
+ static getThreadId(stepId: IStepId): IThreadId;
509
+ runStep(): void;
510
+ }
511
+ //#endregion
512
+ //#region src/threads/content/stream/stream-content-context.d.ts
513
+ declare class StreamContentContext extends AbstractContentContext<StreamContentContextConfig> {
514
+ static readonly class = "cnt_cxt_str";
515
+ static getInitialState(): STATE<StreamContentContextConfig>;
516
+ runStep(): Promise<void>;
517
+ /**
518
+ * Handler of the worker thread end event.
519
+ */
520
+ onWorkerThreadEnd(event: IActionEvent<StreamContentContextWorkerConfig, ACTION.end>): void;
521
+ /**
522
+ * Handles a single chunk of the streaming response from the OpenAI API.
523
+ */
524
+ onHandleContent(event: StreamContentContextHandleContentEvent): void;
525
+ /**
526
+ * Handles the notification that all response chunks were received from the OpenAI API.
527
+ */
528
+ protected onAllContentReceived(): void;
529
+ private onToolCall;
530
+ /**
531
+ * Tries to flush the queue of response chunks to the controller thread.
532
+ * If the first event in the queue is not full, it waits for a debounce duration before flushing.
533
+ * If the first event is full, it flushes it immediately.
534
+ */
535
+ private scheduleNextContentEvent;
536
+ /**
537
+ * Compacts the content into the controller thread's events queue.
538
+ * If the content is too long, it splits it into multiple events.
539
+ * If the last event is not full, it extends it with the new content.
540
+ */
541
+ private addContentToQueue;
542
+ /**
543
+ * Processes the next event in the controller thread's queue.
544
+ * If the worker thread is busy, it skips processing.
545
+ * If the event is the termination marker, it notifies the end of the controller thread.
546
+ * If the event is ready for processing, it starts the worker thread to handle it.
547
+ */
548
+ private tryRunWorker;
549
+ private isEventReady;
550
+ private getEventToProcess;
551
+ protected tryToComplete(): void;
552
+ protected get maxChunkContentLength(): number;
553
+ protected get debounceDuration(): number;
554
+ }
555
+ //#endregion
556
+ //#region src/threads/history/history-context-types.d.ts
557
+ declare const historyContextClassId = "hst_ctrl";
558
+ declare const historyContextEvents: {
559
+ readonly appendToHistory: "hst_append";
560
+ readonly prepareHistory: "hst_prepare";
561
+ readonly adjustOnCancel: "hst_cncl";
562
+ };
563
+ type HistoryContextEventName = typeof historyContextEvents;
564
+ type HistoryContextAppendParameters = {
565
+ message: ChatCompletionMessageParam | ChatCompletionMessageParam[];
566
+ };
567
+ type HistoryContextEventAppendToHistory = IEvent<HistoryContextAppendParameters, HistoryContextEventName['appendToHistory']>;
568
+ type HistoryContextEventPrepareHistory = IEvent<unknown, HistoryContextEventName['prepareHistory']>;
569
+ type HistoryContextEventAdjustOnCancel = IEvent<unknown, HistoryContextEventName['adjustOnCancel']>;
570
+ type HistoryContextEvent = HistoryContextEventAppendToHistory | HistoryContextEventPrepareHistory | HistoryContextEventAdjustOnCancel;
571
+ type HistoryContextState = {
572
+ class: typeof historyContextClassId; /** messages to send to OpenAI API */
573
+ messages: ChatCompletionMessageParam[];
574
+ };
575
+ interface HistoryContextConfig extends BaseConfig {
576
+ state: HistoryContextState;
577
+ events: HistoryContextEvent;
578
+ }
579
+ //#endregion
580
+ //#region src/threads/history/history-context.d.ts
581
+ declare class HistoryContext extends BaseStep<HistoryContextConfig> {
582
+ static readonly class = "hst_ctrl";
583
+ static start(thread: IThread<ChatCompletionConfig>): Promise<void>;
584
+ static prepareHistory<T extends ChatCompletionConfig>(thread: IThread<T>): Promise<void>;
585
+ static appendToHistory<T extends ChatCompletionConfig>(thread: IThread<T>, message: ChatCompletionMessageParam$1 | ChatCompletionMessageParam$1[]): Promise<void>;
586
+ static adjustOnCancel<T extends ChatCompletionConfig>(thread: IThread<T>): Promise<void>;
587
+ static getHistory<T extends CONFIG>(thread: IThread<T>): ChatCompletionMessageParam$1[];
588
+ static getThreadId(stepId: IStepId): IThreadId;
589
+ runStep(): void;
590
+ initialize(): void;
591
+ onAdjustOnCancel(): void;
592
+ onPrepareHistory(): void;
593
+ onAppendToHistory(event: HistoryContextEventAppendToHistory): void;
594
+ }
595
+ //#endregion
596
+ //#region src/threads/request/request-context.d.ts
597
+ declare class RequestContext extends BaseStep<RequestContextConfig> {
598
+ static readonly class = "rqst_ctx";
599
+ static start<T extends ContentContextConfig>(thread: IThread<T>): Promise<RequestContextResult>;
600
+ static getThreadId(stepId: IStepId): IThreadId;
601
+ runStep(): Promise<void>;
602
+ private cancel;
603
+ private complete;
604
+ private getHistory;
605
+ private get openAiService();
606
+ private get responseHandler();
607
+ }
608
+ //#endregion
609
+ //#region src/threads/tools/tools-context-worker-types.d.ts
610
+ declare const toolsContextWorkerClassId = "tl_wrk";
611
+ type ToolsContextParsedArguments = Record<string, unknown>;
612
+ type ToolsContextWorkerMergeField = {
613
+ function: {
614
+ name: string;
615
+ arguments: ToolsContextParsedArguments;
616
+ };
617
+ };
618
+ type ToolsContextWorkerOut = ToolsContextWorkerMergeField;
619
+ type ToolsContextWorkerState = {
620
+ class: typeof toolsContextWorkerClassId;
621
+ };
622
+ type ToolsContextWorkerLocal = {
623
+ toolCall: ChatCompletionMessageFunctionToolCall;
624
+ parsedArguments: ToolsContextParsedArguments;
625
+ } & LOCAL<BaseChildConfig>;
626
+ type ThreadWorkerResultOk = {
627
+ status: 'ok'; /** id of the tool call that was processed */
628
+ toolCallId: string; /** result of the tool call */
629
+ data: unknown;
630
+ };
631
+ type ThreadWorkerResult = ThreadWorkerResultOk | RESULT<BaseChildConfig>;
632
+ interface ToolsContextWorkerConfig extends BaseChildConfig<ThreadWorkerResult, ToolsContextWorkerLocal> {
633
+ dataOut: ToolsContextWorkerOut;
634
+ state: ToolsContextWorkerState;
635
+ }
636
+ //#endregion
637
+ //#region src/threads/tools/tools-context-worker.d.ts
638
+ declare class ToolsContextWorker extends BaseStep<ToolsContextWorkerConfig> {
639
+ static readonly class = "tl_wrk";
640
+ static start(thread: IThread<ToolsContextConfig>, toolCall: ChatCompletionMessageFunctionToolCall$1): Promise<void>;
641
+ static getThreadId(toolCall: ChatCompletionMessageFunctionToolCall$1): IThreadId;
642
+ /**
643
+ * Initializes the tool worker thread to handle processing of repackaged content chunks.
644
+ */
645
+ runStep(): void;
646
+ protected getExitName(): string;
647
+ }
648
+ //#endregion
649
+ //#region src/threads/tools/tools-context.d.ts
650
+ declare class ToolsContext extends BaseStep<ToolsContextConfig> {
651
+ static readonly class = "tl_ctrl";
652
+ static start<T extends ContentContextConfig>(thread: IThread<T>): Promise<void>;
653
+ static callTool<T extends ContentContextConfig>(thread: IThread<T>, toolCall: ChatCompletionMessageFunctionToolCall$1): Promise<void>;
654
+ /**
655
+ * Notify tools context thread that no more tool calls are expected
656
+ */
657
+ static lastToolCallReceived<T extends ContentContextConfig>(thread: IThread<T>): Promise<void>;
658
+ static getThreadId(stepId: IStepId): IThreadId;
659
+ runStep(): void;
660
+ initialize(): void;
661
+ onCallTool(event: ToolsContextEventCallTool): void;
662
+ onLastToolCallReceived(): void;
663
+ onWorkerThreadEnd(event: IActionEvent<ToolsContextWorkerConfig, ACTION.end>): void;
664
+ /**
665
+ * Starts the tool worker thread to process the tool call.
666
+ */
667
+ protected startToolWorkerThread(toolCall: ChatCompletionMessageFunctionToolCall$1): void;
668
+ protected setToolCallResult(toolCallId: string, result: unknown): void;
669
+ protected tryEndToolsContextThread(): void;
670
+ protected get messages(): ChatCompletionToolMessageParam$1[];
671
+ }
672
+ //#endregion
673
+ //#region src/step.d.ts
674
+ declare class CreateChatCompletionStep extends BaseStep<ChatCompletionConfig> {
675
+ runStep(): void;
676
+ initialize(): Promise<void>;
677
+ onContentContextEnd(event: IActionEvent<ContentContextConfig, ACTION.end>): Promise<void>;
678
+ onHistoryContextEnd(event: IActionEvent<HistoryContextConfig, ACTION.end>): Promise<void>;
679
+ onCancel(): Promise<void>;
680
+ private tryToExitStep;
681
+ /**
682
+ * Reset the step so it can handle new request
683
+ *
684
+ * - Reset state of initial thread (e.g. main) to original state;
685
+ * - End CancelContext thread;
686
+ * - End ContentController (with child thread);
687
+ *
688
+ * @throws if there any issues with stopping threads
689
+ */
690
+ private resetStep;
691
+ private resetState;
692
+ private validateMergeFieldType;
693
+ private get contentContext();
694
+ }
695
+ //#endregion
696
+ //#region src/index.d.ts
697
+ declare const states: {
698
+ hst_ctrl: typeof HistoryContext;
699
+ rqst_ctx: typeof RequestContext;
700
+ tl_ctrl: typeof ToolsContext;
701
+ tl_wrk: typeof ToolsContextWorker;
702
+ cnt_cxt_smp: typeof SimpleContentContext;
703
+ cnt_cxt_str: typeof StreamContentContext;
704
+ str_wrk: typeof StreamContentContextWorker;
705
+ cncl_ctrl: typeof CancelContext;
706
+ };
707
+ //#endregion
708
+ export { CallToolParameters, CancelContentEventName, CancelContextConfig, CancelContextEvents, CancelContextState, ChatCompletionCancelEvent, ChatCompletionConfig, ChatCompletionEvent, ChatCompletionOut, ChatCompletionState, ContentChunk, ContentChunkEvent, ContentContentEventName, ContentContextConfig, ContentContextEvent, ContentContextHandleContentEvent, ContentContextOkResult, ContentContextResult, ContentContextState, ContentEndEvent, ContentEvent, HistoryContextAppendParameters, HistoryContextConfig, HistoryContextEvent, HistoryContextEventAdjustOnCancel, HistoryContextEventAppendToHistory, HistoryContextEventName, HistoryContextEventPrepareHistory, HistoryContextState, ParentEventName, RequestContextConfig, RequestContextResult, RequestContextResultOk, RequestContextState, SimpleContentContextConfig, SimpleContentContextEvent, SimpleContentContextHandleContentEvent, StreamContentContextConfig, StreamContentContextEvent, StreamContentContextHandleContentEvent, StreamContentContextState, StreamContentContextWorkerConfig, StreamContentContextWorkerOut, StreamContentContextWorkerResult, StreamContentContextWorkerState, TasksMap, ThreadWorkerResult, ThreadWorkerResultOk, ToolCallTask, ToolContextEventName, ToolsContextConfig, ToolsContextEvent, ToolsContextEventCallTool, ToolsContextEventLastToolReceived, ToolsContextParsedArguments, ToolsContextResult, ToolsContextResultOk, ToolsContextState, ToolsContextWorkerConfig, ToolsContextWorkerLocal, ToolsContextWorkerMergeField, ToolsContextWorkerOut, ToolsContextWorkerState, cancelContentEvents, cancelContextClassId, contentContextEvents, historyContextClassId, historyContextEvents, parentEvents, requestContextClassId, states, CreateChatCompletionStep as step, streamContentContextClassId, streamContentContextWorkerClassId, streamWorkerExits, toolContextEvents, toolsContextClassId, toolsContextWorkerClassId };
709
+ //# sourceMappingURL=index.d.mts.map