Skip to content

Commit

Permalink
feat: use appendNarratives instead of deprecated pushNarrative
Browse files Browse the repository at this point in the history
  • Loading branch information
tabkram committed Nov 26, 2023
1 parent e7ac964 commit 73aad23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/trace/traceableExecution.spec.ts
Expand Up @@ -215,7 +215,7 @@ describe('TraceableExecution', () => {
config: { traceExecution: { narratives: ['Narrative 1 for function 2', 'Narrative 2 for function 2'] } }
});

traceableExecution.pushNarrative(
traceableExecution.appendNarratives(
'sampleFunction_custom_id_3',
'Narrative -1 for function 3, anticipated narrative before node creation'
);
Expand All @@ -233,7 +233,7 @@ describe('TraceableExecution', () => {
expect(trace?.length).toEqual(7);

// Use pushNarrative to add a single narrative to the specified node
traceableExecution.pushNarrative(nodeId, 'Narrative 1');
traceableExecution.appendNarratives(nodeId, 'Narrative 1');

// Check if the narrative was added successfully
const nodeWithNarrative = traceableExecution.getTraceNodes().find((node) => node.data.id === nodeId);
Expand Down
38 changes: 11 additions & 27 deletions src/trace/traceableExecution.ts
Expand Up @@ -268,44 +268,28 @@ export class TraceableExecution {
}

/**
* Push a string narrative to a trace node
* Pushes or appends narratives to a trace node.
* @param nodeId - The ID of the node.
* @param narrative - The narrative to be pushed.
* @param narratives - The narrative or array of narratives to be processed.
* @returns The updated instance of TraceableExecution.
*/
pushNarrative(nodeId: NodeTrace['id'], narrative: string) {
appendNarratives(nodeId: NodeTrace['id'], narratives: string | string[]) {
const existingNodeIndex = this.nodes?.findIndex((n) => n.data.id === nodeId);
if (existingNodeIndex >= 0) {
// it means that we are too late and node has already been created with narratives:
this.nodes[existingNodeIndex].data = {
...this.nodes[existingNodeIndex]?.data,
narratives: [...(this.nodes[existingNodeIndex]?.data?.narratives ?? []), narrative]
};
} else {
this.narrativesForNonFoundNodes[nodeId] = this.narrativesForNonFoundNodes[nodeId] ?? [];
this.narrativesForNonFoundNodes[nodeId]?.push(narrative);
}

return this;
}

/**
* Appends an array of narratives to a trace node.
* @param nodeId - The ID of the node.
* @param narratives - An array of narratives to be appended.
* @returns The updated instance of TraceableExecution.
*/
appendNarratives(nodeId: NodeTrace['id'], narratives: Array<string>) {
const existingNodeIndex = this.nodes?.findIndex((n) => n.data.id === nodeId);
if (existingNodeIndex >= 0) {
// it means that we are too late and node has already been created with narratives:
// Node already exists, update its narratives
this.nodes[existingNodeIndex].data = {
...this.nodes[existingNodeIndex]?.data,
narratives: (this.nodes[existingNodeIndex]?.data?.narratives ?? [])?.concat(narratives)
narratives: [...(this.nodes[existingNodeIndex]?.data?.narratives ?? []), ...(Array.isArray(narratives) ? narratives : [narratives])]

Check warning on line 283 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
};
} else {
this.narrativesForNonFoundNodes[nodeId] = (this.narrativesForNonFoundNodes[nodeId] ?? [])?.concat(narratives);
// Node doesn't exist, store narratives for later use
this.narrativesForNonFoundNodes[nodeId] = [
...(this.narrativesForNonFoundNodes[nodeId] ?? []),
...(Array.isArray(narratives) ? narratives : [narratives])
];
}

return this;
}

Expand Down

0 comments on commit 73aad23

Please sign in to comment.