Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: readable parsing error #172

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading