Skip to content

Commit

Permalink
fix: resolve narrative bug
Browse files Browse the repository at this point in the history
Resolve narrative bugs when initiated simultaneously in both the options.trace.narrative and advanced configuration traceExecution
  • Loading branch information
tabkram committed Nov 25, 2023
1 parent f5bedce commit bcea36f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/trace/trace.model.ts
Expand Up @@ -48,7 +48,14 @@ export function isNodeTrace(config: NodeTrace | NodeData | unknown): config is P
}

export function isNodeExecutionTrace<I, O>(config: NodeExecutionTraceExtractor<I, O>): config is NodeExecutionTraceExtractor<I, O> {
return 'inputs' in config || 'outputs' in config || 'errors' in config || 'startTime' in config || 'endTime' in config;
return (
'inputs' in config ||
'outputs' in config ||
'errors' in config ||
'startTime' in config ||
'endTime' in config ||
'narratives' in config
);
}

export interface NodeData<I = unknown, O = unknown> extends NodeTrace, NodeExecutionTrace<I, O> {}
Expand Down
37 changes: 32 additions & 5 deletions src/trace/traceableExecution.spec.ts
Expand Up @@ -149,8 +149,22 @@ describe('TraceableExecution', () => {

// Run the sample function using the run method and create nodes in the trace
const nodeId = 'sampleFunction_custom_id_1';
traceableExecution.run(sampleFunction, ['InputParam'], { trace: { id: nodeId, narratives: ['Narrative 0'] } });
traceableExecution.run(sampleFunction, ['InputParam2'], { trace: { id: 'sampleFunction_custom_id_2' } });
traceableExecution.run(sampleFunction, ['InputParam'], {
trace: { id: nodeId, narratives: ['Narrative 0'] },
config: {
traceExecution: {
narratives: (res) => {
return [`Narrative 0 with ${res.outputs}`];
}
}
}
});
traceableExecution.run(sampleFunction, ['InputParam2'], {
trace: {
id: 'sampleFunction_custom_id_2',
narratives: ['Narrative 0 for function 2']
}
});

// Get the initial trace and assert its length
const initialTrace = traceableExecution.getTrace();
Expand All @@ -161,18 +175,31 @@ describe('TraceableExecution', () => {

// Check if the narrative was added successfully
const nodeWithNarrative = traceableExecution.getTraceNodes().find((node) => node.data.id === nodeId);
expect(nodeWithNarrative?.data.narratives).toEqual(['Narrative 0', 'Narrative 1']);
expect(nodeWithNarrative?.data.narratives).toEqual(['Narrative 0', 'Narrative 0 with Result: InputParam', 'Narrative 1']);

// Use appendNarratives to add an array of narratives to the same node
traceableExecution.appendNarratives(nodeId, ['Narrative 2', 'Narrative 3']);

// Check if the narratives were appended successfully
const nodeWithAppendedNarratives = traceableExecution.getTraceNodes().find((node) => node.data.id === nodeId);
expect(nodeWithAppendedNarratives?.data.narratives).toEqual(['Narrative 0', 'Narrative 1', 'Narrative 2', 'Narrative 3']);
expect(nodeWithAppendedNarratives?.data.narratives).toEqual([
'Narrative 0',
'Narrative 0 with Result: InputParam',
'Narrative 1',
'Narrative 2',
'Narrative 3'
]);

// Get the ordered narratives and verify their content
const orderedNarratives = traceableExecution.getOrderedNarratives();
expect(orderedNarratives).toEqual(['Narrative 0', 'Narrative 1', 'Narrative 2', 'Narrative 3']);
expect(orderedNarratives).toEqual([
'Narrative 0',
'Narrative 0 with Result: InputParam',
'Narrative 1',
'Narrative 2',
'Narrative 3',
'Narrative 0 for function 2'
]);

// Get the final trace and assert its updated length
const finalTrace = traceableExecution.getTrace();
Expand Down
4 changes: 2 additions & 2 deletions src/trace/traceableExecution.ts
Expand Up @@ -383,8 +383,8 @@ export class TraceableExecution {
data: {
...this.nodes[existingNodeIndex]?.data,
...filterExecutionTrace,
narratives: this.narratives?.[nodeTrace?.id],
...nodeTrace,
narratives: (nodeTrace.narratives ?? []).concat(this.narratives?.[nodeTrace?.id] ?? []),
parallel: options?.parallel,
abstract: isAutoCreated,
updateTime: new Date()
Expand All @@ -395,8 +395,8 @@ export class TraceableExecution {
this.nodes?.push({
data: {
...filterExecutionTrace,
narratives: this.narratives?.[nodeTrace?.id],
...nodeTrace,
narratives: (nodeTrace.narratives ?? []).concat(this.narratives?.[nodeTrace?.id] ?? []),
parallel: options?.parallel,
abstract: isAutoCreated,
createTime: new Date()
Expand Down

0 comments on commit bcea36f

Please sign in to comment.