Skip to content

Commit

Permalink
fix: the temporary storage for narratives associated with non-found n…
Browse files Browse the repository at this point in the history
…odes in order to improve them
  • Loading branch information
tabkram committed Nov 26, 2023
1 parent 4806da8 commit 97545b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/trace/traceableExecution.spec.ts
Expand Up @@ -157,6 +157,10 @@ describe('TraceableExecution', () => {
config: { traceExecution: { narratives: ['Narrative 1 for function 2', 'Narrative 2 for function 2'] } }
});

traceableExecution.pushNarrative(
'sampleFunction_custom_id_3',
'Narrative -1 for function 3, anticipated narrative before node creation'
);
traceableExecution.run(sampleFunction, ['InputParam'], {
trace: { id: 'sampleFunction_custom_id_3', narratives: ['Narrative 0 for function 3'] },
config: { traceExecution: { narratives: true } }
Expand Down Expand Up @@ -201,6 +205,7 @@ describe('TraceableExecution', () => {
'Narrative 0 for function 2',
'Narrative 1 for function 2',
'Narrative 2 for function 2',
'Narrative -1 for function 3, anticipated narrative before node creation',
'Narrative 0 for function 3'
]);
});
Expand Down
58 changes: 33 additions & 25 deletions src/trace/traceableExecution.ts
Expand Up @@ -32,7 +32,13 @@ export type TraceableRunnerOptions = TraceOptions<Array<any>, unknown> | TraceOp
export class TraceableExecution {
private nodes: Array<Node>;
private edges: Array<Edge>;
private narratives: {

/**
* A temporary storage for narratives associated with non-found nodes.
* Narratives are stored in memory until the corresponding node is created.
* @private
*/
private narrativesForNonFoundNodes: {
[key: string]: Array<string>;
};

Expand Down Expand Up @@ -61,17 +67,19 @@ export class TraceableExecution {
}
}

private static extractNarrativeWithConfig<I, O>(
executionTrace: NodeExecutionTrace<I, O>,
private extractNarrativeWithConfig<I, O>(
nodeData: NodeData<I, O>,
narrativeConfig: NodeExecutionTraceExtractor<I, O>['narratives']
): Array<string> {
try {
const narratives = (this.narrativesForNonFoundNodes[nodeData.id] ?? [])?.concat(nodeData.narratives ?? []);

Check warning on line 75 in src/trace/traceableExecution.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
delete this.narrativesForNonFoundNodes[nodeData.id];
if (typeof narrativeConfig === 'function') {
return (executionTrace.narratives ?? []).concat(narrativeConfig(executionTrace));
return narratives.concat(narrativeConfig(nodeData));
} else if (Array.isArray(narrativeConfig)) {
return (executionTrace.narratives ?? []).concat(narrativeConfig);
return narratives.concat(narrativeConfig);
} else if (narrativeConfig === true) {
return executionTrace.narratives;
return narratives;
}
} catch (e) {
throw new Error(`error when mapping/extracting Narrative with config: "${narrativeConfig}", ${e?.message}`);
Expand All @@ -81,7 +89,7 @@ export class TraceableExecution {
initTrace(initialTrace: Trace) {
this.nodes = (initialTrace?.filter((b) => b.group === 'nodes') as Array<Node>) ?? [];
this.edges = (initialTrace?.filter((b) => b.group === 'edges') as Array<Edge>) ?? [];
this.narratives = {};
this.narrativesForNonFoundNodes = {};
}

/**
Expand Down Expand Up @@ -273,9 +281,11 @@ export class TraceableExecution {
...this.nodes[existingNodeIndex]?.data,
narratives: [...(this.nodes[existingNodeIndex]?.data?.narratives ?? []), narrative]
};
} else {
this.narrativesForNonFoundNodes[nodeId] = this.narrativesForNonFoundNodes[nodeId] ?? [];
this.narrativesForNonFoundNodes[nodeId]?.push(narrative);
}
this.narratives[nodeId] = this.narratives[nodeId] ?? [];
this.narratives[nodeId]?.push(narrative);

return this;
}

Expand All @@ -293,8 +303,9 @@ export class TraceableExecution {
...this.nodes[existingNodeIndex]?.data,
narratives: (this.nodes[existingNodeIndex]?.data?.narratives ?? [])?.concat(narratives)
};
} else {
this.narrativesForNonFoundNodes[nodeId] = (this.narrativesForNonFoundNodes[nodeId] ?? [])?.concat(narratives);
}
this.narratives[nodeId] = (this.narratives[nodeId] ?? [])?.concat(narratives);
return this;
}

Expand Down Expand Up @@ -429,42 +440,39 @@ export class TraceableExecution {
};
}

private filterNodeExecutionTrace<I, O>(
executionTrace?: NodeExecutionTrace<I, O>,
doTraceExecution?: TraceOptions<I, O>['config']['traceExecution']
) {
private filterNodeExecutionTrace<I, O>(nodeData?: NodeData<I, O>, doTraceExecution?: TraceOptions<I, O>['config']['traceExecution']) {
if (!doTraceExecution) {
return {};
}
if (doTraceExecution === true) {
return executionTrace;
return nodeData;
}
if (Array.isArray(doTraceExecution)) {
const execTrace: NodeExecutionTrace<unknown, unknown> = {};
Object.keys(executionTrace).forEach((k) => {
Object.keys(nodeData).forEach((k) => {
if (doTraceExecution.includes(k as keyof NodeExecutionTrace<I, O>)) {
execTrace[k] = executionTrace[k];
execTrace[k] = nodeData[k];
}
});
return execTrace;
}
if (isNodeExecutionTrace(doTraceExecution)) {
const execTrace: NodeExecutionTrace<unknown, unknown> = {};
execTrace.inputs = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(executionTrace.inputs, doTraceExecution.inputs);
execTrace.outputs = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(executionTrace.outputs, doTraceExecution.outputs);
execTrace.errors = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(executionTrace.errors, doTraceExecution.errors);
execTrace.inputs = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(nodeData.inputs, doTraceExecution.inputs);
execTrace.outputs = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(nodeData.outputs, doTraceExecution.outputs);
execTrace.errors = TraceableExecution.extractIOExecutionTraceWithConfig<I, O>(nodeData.errors, doTraceExecution.errors);

execTrace.narratives = TraceableExecution.extractNarrativeWithConfig<I, O>(executionTrace, doTraceExecution.narratives);
execTrace.narratives = this.extractNarrativeWithConfig<I, O>(nodeData, doTraceExecution.narratives);

if (doTraceExecution.startTime === true) {
execTrace.startTime = executionTrace.startTime;
execTrace.startTime = nodeData.startTime;
}
if (doTraceExecution.endTime === true) {
execTrace.endTime = executionTrace.endTime;
execTrace.endTime = nodeData.endTime;
}
if (doTraceExecution.startTime === true && doTraceExecution.endTime === true) {
execTrace.duration = executionTrace.duration;
execTrace.elapsedTime = executionTrace.elapsedTime;
execTrace.duration = nodeData.duration;
execTrace.elapsedTime = nodeData.elapsedTime;
}
return execTrace;
}
Expand Down

0 comments on commit 97545b2

Please sign in to comment.