Skip to content

Commit

Permalink
feat: readable parsing error (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen authored May 23, 2024
1 parent c98b994 commit 10559db
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"tailwindcss": "^3.3.5",
"webextension-polyfill": "0.10.0",
"zod": "^3.22.4",
"zod-validation-error": "^3.3.0",
"zustand": "^4.5.2"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/helpers/vision-agent/determineNextAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function determineNextActionWithVision(
} catch (e) {
console.error(e);
// TODO: try use LLM to fix format when response is not valid
throw new Error(`Incorrectly formatted response: ${e}`);
throw new Error(`Incorrect response format: ${e}`);
}

return {
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/vision-agent/parseResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toolSchemaUnion, type ToolOperation } from "./tools";
import { fromError } from "zod-validation-error";

export type Action = {
thought: string;
Expand Down Expand Up @@ -38,7 +39,14 @@ export function parseResponse(rawResponse: string): Action {
if (response.thought == null || response.action == null) {
throw new Error("Invalid response: Thought and Action are required");
}
const operation = toolSchemaUnion.parse(response.action);
let operation;
try {
operation = toolSchemaUnion.parse(response.action);
} catch (err) {
const validationError = fromError(err);
// user friendly error message
throw new Error(validationError.toString());
}
if ("speak" in response) {
return {
thought: response.thought,
Expand Down

0 comments on commit 10559db

Please sign in to comment.