Skip to content

Commit ac80587

Browse files
authored
fix(agent): error instance for validation feedback. (#520)
1 parent db0aa39 commit ac80587

5 files changed

Lines changed: 107 additions & 9 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { AgenticaConstant } from "../constants/AgenticaConstant";
2+
3+
export class AgenticaJsonParseError extends Error {
4+
public readonly arguments: string;
5+
public readonly reason: string;
6+
7+
public constructor(props: AgenticaJsonParseError.IProps) {
8+
super(`Invalid JSON format. The parsing failed after ${AgenticaConstant.RETRY} retries.`);
9+
10+
const proto = new.target.prototype;
11+
12+
// eslint-disable-next-line
13+
if (Object.setPrototypeOf) {
14+
Object.setPrototypeOf(this, proto);
15+
}
16+
else {
17+
// eslint-disable-next-line
18+
(this as any).__proto__ = proto;
19+
}
20+
21+
this.arguments = props.arguments;
22+
this.reason = props.reason;
23+
}
24+
25+
public get name(): "AgenticaJsonParseError" {
26+
return "AgenticaJsonParseError";
27+
}
28+
29+
public toJSON(): AgenticaJsonParseError.IJson {
30+
return {
31+
name: "AgenticaJsonParseError",
32+
message: this.message,
33+
arguments: this.arguments,
34+
reason: this.reason,
35+
};
36+
}
37+
}
38+
export namespace AgenticaJsonParseError {
39+
export interface IProps {
40+
arguments: string;
41+
reason: string;
42+
}
43+
export interface IJson extends IProps {
44+
name: "AgenticaJsonParseError";
45+
message: string;
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { IValidation } from "@samchon/openapi";
2+
3+
import { AgenticaConstant } from "../constants/AgenticaConstant";
4+
5+
export class AgenticaValidationError extends Error {
6+
public readonly arguments: unknown;
7+
public readonly errors: IValidation.IError[];
8+
9+
public constructor(props: AgenticaValidationError.IProps) {
10+
super(`Invalid arguments. The validation failed after ${AgenticaConstant.RETRY} retries.`);
11+
12+
const proto = new.target.prototype;
13+
14+
// eslint-disable-next-line
15+
if (Object.setPrototypeOf) {
16+
Object.setPrototypeOf(this, proto);
17+
}
18+
else {
19+
// eslint-disable-next-line
20+
(this as any).__proto__ = proto;
21+
}
22+
23+
this.arguments = props.arguments;
24+
this.errors = props.errors;
25+
}
26+
27+
public get name(): "AgenticaValidationError" {
28+
return "AgenticaValidationError";
29+
}
30+
31+
public toJSON(): AgenticaValidationError.IJson {
32+
return {
33+
name: "AgenticaValidationError",
34+
message: this.message,
35+
arguments: this.arguments,
36+
errors: this.errors,
37+
};
38+
}
39+
}
40+
export namespace AgenticaValidationError {
41+
export interface IProps {
42+
arguments: unknown;
43+
errors: IValidation.IError[];
44+
}
45+
export interface IJson extends IProps {
46+
name: "AgenticaValidationError";
47+
message: string;
48+
}
49+
}

packages/core/src/errors/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./AgenticaJsonParseError";
2+
export * from "./AgenticaValidationError";

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./Agentica";
22

33
export * from "./constants";
44
export * from "./context";
5+
export * from "./errors";
56
export * from "./events";
67
export * as factory from "./factory";
78
export * from "./functional";

packages/core/src/orchestrate/call.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { AgenticaConstant } from "../constants/AgenticaConstant";
2020
import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
2121
import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
2222
import { isAgenticaContext } from "../context/internal/isAgenticaContext";
23+
import { AgenticaJsonParseError } from "../errors/AgenticaJsonParseError";
24+
import { AgenticaValidationError } from "../errors/AgenticaValidationError";
2325
import { createAssistantMessageEvent, createCallEvent, createExecuteEvent, createJsonParseErrorEvent, createValidateEvent } from "../factory/events";
2426
import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
2527
import { __get_retry } from "../utils/__retry";
@@ -223,11 +225,10 @@ async function correctTypeError(
223225
call_id: callEvent.id,
224226
operation: callEvent.operation,
225227
arguments: callEvent.arguments,
226-
value: {
227-
name: "ValidationError",
228-
message: `Invalid arguments. The validation failed after ${AgenticaConstant.RETRY} retries.`,
228+
value: new AgenticaValidationError({
229+
arguments: callEvent.arguments,
229230
errors: validateEvent.result.errors,
230-
},
231+
}),
231232
success: false,
232233
}),
233234
operation: callEvent.operation,
@@ -283,12 +284,10 @@ async function correctJsonError(
283284
call_id: toolCall.id,
284285
operation: parseErrorEvent.operation,
285286
arguments: {},
286-
value: {
287-
name: "JsonParseError",
288-
message: `Invalid JSON format. The parsing failed after ${AgenticaConstant.RETRY} retries.`,
287+
value: new AgenticaJsonParseError({
289288
arguments: parseErrorEvent.arguments,
290-
errorMessage: parseErrorEvent.errorMessage,
291-
},
289+
reason: parseErrorEvent.errorMessage,
290+
}),
292291
success: false,
293292
}),
294293
operation: parseErrorEvent.operation,

0 commit comments

Comments
 (0)