Skip to content

Commit 400cba3

Browse files
committed
feat(call-wrapper): extend output handling to include raw data (without mapping yet)
1 parent d4a3300 commit 400cba3

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/call-wrapper.ts

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,26 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
2222
private prompt: PromptBuilder<I, O, T>;
2323
private started = false;
2424
private promptId?: string;
25-
private output: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> = {} as any;
25+
private output: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> = {} as any;
2626

2727
private onPreviewFn?: (ev: Blob, promptId?: string) => void;
2828
private onPendingFn?: (promptId?: string) => void;
2929
private onStartFn?: (promptId?: string) => void;
30-
private onOutputFn?: (key: keyof PromptBuilder<I, string, T>["mapOutputKeys"], data: any, promptId?: string) => void;
31-
private onFinishedFn?: (data: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any>, promptId?: string) => void;
30+
private onOutputFn?: (
31+
key: keyof PromptBuilder<I, string, T>["mapOutputKeys"] | "_raw",
32+
data: any,
33+
promptId?: string
34+
) => void;
35+
private onFinishedFn?: (
36+
data: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> & {
37+
/**
38+
* The raw output data from the workflow execution.
39+
* Key is node_id, value is node output.
40+
*/
41+
_raw?: Record<string, any>;
42+
},
43+
promptId?: string
44+
) => void;
3245
private onFailedFn?: (err: Error, promptId?: string) => void;
3346
private onProgressFn?: (info: NodeProgress, promptId?: string) => void;
3447

@@ -95,7 +108,7 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
95108
* @param fn - The callback function to handle the output.
96109
* @returns The current instance of the class.
97110
*/
98-
onOutput(fn: (key: keyof PromptBuilder<I, O, T>["mapOutputKeys"], data: any, promptId?: string) => void) {
111+
onOutput(fn: (key: keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", data: any, promptId?: string) => void) {
99112
this.onOutputFn = fn;
100113
return this;
101114
}
@@ -107,7 +120,18 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
107120
* and an optional promptId parameter.
108121
* @returns The current instance of the CallWrapper.
109122
*/
110-
onFinished(fn: (data: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any>, promptId?: string) => void) {
123+
onFinished(
124+
fn: (
125+
data: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> & {
126+
/**
127+
* The raw output data from the workflow execution.
128+
* Key is node_id, value is node output.
129+
*/
130+
_raw?: Record<string, any>;
131+
},
132+
promptId?: string
133+
) => void
134+
) {
111135
this.onFinishedFn = fn;
112136
return this;
113137
}
@@ -143,7 +167,7 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
143167
* or `undefined` if the job is not found,
144168
* or `false` if the job execution fails.
145169
*/
146-
async run(): Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | undefined | false> {
170+
async run(): Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | undefined | false> {
147171
/**
148172
* Start the job execution.
149173
*/
@@ -158,12 +182,11 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
158182
promptLoadTrigger = resolve;
159183
});
160184

161-
let jobDoneTrigger!: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | false) => void;
162-
const jobDonePromise: Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | false> = new Promise(
163-
(resolve) => {
185+
let jobDoneTrigger!: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | false) => void;
186+
const jobDonePromise: Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | false> =
187+
new Promise((resolve) => {
164188
jobDoneTrigger = resolve;
165-
}
166-
);
189+
});
167190

168191
/**
169192
* Declare the function to check if the job is executing.
@@ -195,8 +218,9 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
195218
// race condition handling
196219
let wentMissing = false;
197220
let cachedOutputDone = false;
198-
let cachedOutputPromise: Promise<false | Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | null> =
199-
Promise.resolve(null);
221+
let cachedOutputPromise: Promise<
222+
false | Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | null
223+
> = Promise.resolve(null);
200224

201225
const statusHandler = async () => {
202226
const queue = await this.client.getQueue();
@@ -358,7 +382,7 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
358382

359383
private async handleCachedOutput(
360384
promptId: string
361-
): Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | false | null> {
385+
): Promise<Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | false | null> {
362386
const hisData = await this.client.getHistory(promptId);
363387
if (hisData?.status?.completed) {
364388
const output = this.mapOutput(hisData.outputs);
@@ -372,14 +396,19 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
372396
return null;
373397
}
374398

375-
private mapOutput(outputNodes: any): Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> {
399+
private mapOutput(outputNodes: any): Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> {
376400
const outputMapped = this.prompt.mapOutputKeys;
377-
const output: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> = {} as any;
401+
const output: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> = {} as any;
378402

379403
for (const key in outputMapped) {
380404
const node = outputMapped[key];
381405
if (node) {
382406
output[key as keyof PromptBuilder<I, O, T>["mapOutputKeys"]] = outputNodes[node];
407+
} else {
408+
if (!output._raw) {
409+
output._raw = {};
410+
}
411+
output._raw[key] = outputNodes[key];
383412
}
384413
}
385414

@@ -388,7 +417,7 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
388417

389418
private handleJobExecution(
390419
promptId: string,
391-
jobDoneTrigger: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | false) => void
420+
jobDoneTrigger: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | false) => void
392421
): void {
393422
const reverseOutputMapped = this.reverseMapOutputKeys();
394423

@@ -406,6 +435,10 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
406435
this.output[outputKey as keyof PromptBuilder<I, O, T>["mapOutputKeys"]] = ev.detail.output;
407436
this.onOutputFn?.(outputKey, ev.detail.output, this.promptId);
408437
remainingOutput--;
438+
} else {
439+
this.output._raw = this.output._raw || {};
440+
this.output._raw[ev.detail.node as string] = ev.detail.output;
441+
this.onOutputFn?.(ev.detail.node as string, ev.detail.output, this.promptId);
409442
}
410443

411444
if (remainingOutput === 0) {
@@ -467,7 +500,7 @@ export class CallWrapper<I extends string, O extends string, T extends NodeData>
467500
private handleError(
468501
ev: CustomEvent,
469502
promptId: string,
470-
resolve: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"], any> | false) => void
503+
resolve: (value: Record<keyof PromptBuilder<I, O, T>["mapOutputKeys"] | "_raw", any> | false) => void
471504
) {
472505
if (ev.detail.prompt_id !== promptId) return;
473506
this.onFailedFn?.(new CustomEventError(ev.detail.exception_type, { cause: ev.detail }), ev.detail.prompt_id);

src/prompt-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class PromptBuilder<I extends string, O extends string, T extends NodeDat
216216
* @throws {Error} - If the key is not found.
217217
*/
218218
inputRaw<V = string | number | undefined>(key: string, value: V, encodeOs?: OSType) {
219-
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
219+
if (key === "__proto__" || key === "constructor" || key === "prototype") {
220220
throw new Error(`Invalid key: ${key}`);
221221
}
222222
if (value !== undefined) {

0 commit comments

Comments
 (0)