Skip to content

Commit

Permalink
feat(agent): improve LinePrefixParser error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
  • Loading branch information
Tomas2D committed Oct 3, 2024
1 parent 9ee7957 commit 710f75b
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/agents/parsers/linePrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,17 @@ export class LinePrefixParser<
if (this.lastNodeKey) {
const lastNode = this.nodes[this.lastNodeKey];
if (!lastNode.next.includes(parsedLine.key)) {
throw new LinePrefixParserError(
this.throwWithContext(
`Transition from '${this.lastNodeKey}' to '${parsedLine.key}' does not exist!`,
{ line },
);
}

await this.emitFinalUpdate(this.lastNodeKey, lastNode.field);
} else if (!this.nodes[parsedLine.key].isStart) {
throw new LinePrefixParserError(
this.throwWithContext(
`Parsed text line corresponds to a node "${parsedLine.key}" which is not a start node!`,
{ line },
);
}

Expand Down Expand Up @@ -206,19 +208,30 @@ export class LinePrefixParser<
}
}

protected throwWithContext(
message: string,
extra: { line?: Line; errors?: Error[] } = {},
): never {
throw new LinePrefixParserError(
[`The generated output does not adhere to the schema.`, message].join(NEW_LINE_CHARACTER),
extra.errors,
{
context: {
lines: linesToString(this.lines.concat(extra.line ? [extra.line] : [])),
excludedLines: linesToString(this.excludedLines),
},
},
);
}

async end() {
if (this.done) {
return this.finalState;
}
this.done = true;

if (!this.lastNodeKey) {
throw new LinePrefixParserError("Nothing valid has been parsed yet!", [], {
context: {
lines: linesToString(this.lines),
excludedLines: linesToString(this.excludedLines),
},
});
this.throwWithContext("Nothing valid has been parsed yet!");
}

const stash = linesToString(this.lines);
Expand All @@ -238,7 +251,7 @@ export class LinePrefixParser<

const currentNode = this.nodes[this.lastNodeKey];
if (!currentNode.isEnd) {
throw new LinePrefixParserError(`Node '${this.lastNodeKey}' is not an end node.`);
this.throwWithContext(`Node '${this.lastNodeKey}' is not an end node.`);
}

await Promise.allSettled(Object.values(this.nodes).map(({ field }) => field.end()));
Expand All @@ -247,9 +260,7 @@ export class LinePrefixParser<

protected async emitPartialUpdate(data: InferCallbackValue<Callbacks<T>["partialUpdate"]>) {
if (data.key in this.finalState) {
throw new LinePrefixParserError(
`Cannot update partial event for completed key '${data.key}'`,
);
this.throwWithContext(`Cannot update partial event for completed key '${data.key}'`);
}
if (!(data.key in this.partialState)) {
this.partialState[data.key] = "";
Expand All @@ -260,7 +271,7 @@ export class LinePrefixParser<

protected async emitFinalUpdate(key: StringKey<T>, field: ParserField<any, any>) {
if (key in this.finalState) {
throw new LinePrefixParserError(`Duplicated key '${key}'`);
this.throwWithContext(`Duplicated key '${key}'`);
}

try {
Expand All @@ -273,9 +284,9 @@ export class LinePrefixParser<
});
} catch (e) {
if (e instanceof ZodError) {
throw new LinePrefixParserError(
this.throwWithContext(
`Value for ${key} cannot be retrieved because it's value does not adhere to the appropriate schema.`,
[e],
{ errors: [e] },
);
}
throw e;
Expand Down

0 comments on commit 710f75b

Please sign in to comment.