diff --git a/package.json b/package.json index 62ce775..f8d3e19 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8eeb529..5a09ef0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -65,6 +65,9 @@ dependencies: zod: specifier: ^3.22.4 version: 3.22.4 + zod-validation-error: + specifier: ^3.3.0 + version: 3.3.0(zod@3.22.4) zustand: specifier: ^4.5.2 version: 4.5.2(@types/react@18.2.37)(immer@10.0.3)(react@18.2.0) @@ -9021,6 +9024,15 @@ packages: engines: {node: '>=12.20'} dev: true + /zod-validation-error@3.3.0(zod@3.22.4): + resolution: {integrity: sha512-Syib9oumw1NTqEv4LT0e6U83Td9aVRk9iTXPUQr1otyV1PuXQKOvOwhMNqZIq5hluzHP2pMgnOmHEo7kPdI2mw==} + engines: {node: '>=18.0.0'} + peerDependencies: + zod: ^3.18.0 + dependencies: + zod: 3.22.4 + dev: false + /zod@3.22.4: resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} dev: false diff --git a/src/helpers/vision-agent/determineNextAction.ts b/src/helpers/vision-agent/determineNextAction.ts index 3d16a37..cbdf547 100644 --- a/src/helpers/vision-agent/determineNextAction.ts +++ b/src/helpers/vision-agent/determineNextAction.ts @@ -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 { diff --git a/src/helpers/vision-agent/parseResponse.ts b/src/helpers/vision-agent/parseResponse.ts index 1c44e01..bf5fc8d 100644 --- a/src/helpers/vision-agent/parseResponse.ts +++ b/src/helpers/vision-agent/parseResponse.ts @@ -1,4 +1,5 @@ import { toolSchemaUnion, type ToolOperation } from "./tools"; +import { fromError } from "zod-validation-error"; export type Action = { thought: string; @@ -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,