Skip to content

Commit 10559db

Browse files
authored
feat: readable parsing error (#172)
1 parent c98b994 commit 10559db

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"tailwindcss": "^3.3.5",
4646
"webextension-polyfill": "0.10.0",
4747
"zod": "^3.22.4",
48+
"zod-validation-error": "^3.3.0",
4849
"zustand": "^4.5.2"
4950
},
5051
"devDependencies": {

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers/vision-agent/determineNextAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export async function determineNextActionWithVision(
111111
} catch (e) {
112112
console.error(e);
113113
// TODO: try use LLM to fix format when response is not valid
114-
throw new Error(`Incorrectly formatted response: ${e}`);
114+
throw new Error(`Incorrect response format: ${e}`);
115115
}
116116

117117
return {

src/helpers/vision-agent/parseResponse.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toolSchemaUnion, type ToolOperation } from "./tools";
2+
import { fromError } from "zod-validation-error";
23

34
export type Action = {
45
thought: string;
@@ -38,7 +39,14 @@ export function parseResponse(rawResponse: string): Action {
3839
if (response.thought == null || response.action == null) {
3940
throw new Error("Invalid response: Thought and Action are required");
4041
}
41-
const operation = toolSchemaUnion.parse(response.action);
42+
let operation;
43+
try {
44+
operation = toolSchemaUnion.parse(response.action);
45+
} catch (err) {
46+
const validationError = fromError(err);
47+
// user friendly error message
48+
throw new Error(validationError.toString());
49+
}
4250
if ("speak" in response) {
4351
return {
4452
thought: response.thought,

0 commit comments

Comments
 (0)