Skip to content

Commit 7660403

Browse files
committed
feat(ui-tars): refact GUIAgentError
1 parent 6e53e07 commit 7660403

File tree

4 files changed

+46
-49
lines changed

4 files changed

+46
-49
lines changed

apps/ui-tars/src/main/services/runAgent.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ export const runAgent = async (
171171
setState({
172172
...getState(),
173173
status: StatusEnum.ERROR,
174-
errorMsg: JSON.stringify(error),
174+
errorMsg: JSON.stringify({
175+
status: error.status,
176+
message: error.message,
177+
stack: error.stack,
178+
}),
175179
});
176180
},
177181
retry: {

apps/ui-tars/src/renderer/src/components/RunMessages/Messages.tsx

+3-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const ErrorMessage = ({ text }: { text: string }) => {
3131
let parsedError: GUIAgentError | null = null;
3232
try {
3333
const parsed = JSON.parse(text);
34-
if (parsed && typeof parsed === 'object' && 'code' in parsed) {
34+
if (parsed && typeof parsed === 'object' && 'status' in parsed) {
3535
parsedError = parsed as GUIAgentError;
3636
}
3737
} catch {
@@ -44,7 +44,7 @@ export const ErrorMessage = ({ text }: { text: string }) => {
4444
<AlertCircle className="w-5 h-5 text-red-500 shrink-0" />
4545
<span className="font-medium text-red-500">
4646
{parsedError
47-
? ErrorStatusEnum[parsedError.code] || 'UNKNOWN_ERROR'
47+
? ErrorStatusEnum[parsedError.status] || 'UNKNOWN_ERROR'
4848
: 'Error'}
4949
</span>
5050
</div>
@@ -53,16 +53,10 @@ export const ErrorMessage = ({ text }: { text: string }) => {
5353
<div className="text-sm text-red-500/90 font-medium">
5454
{parsedError.message}
5555
</div>
56-
{parsedError.stack ? (
56+
{parsedError.stack && (
5757
<div className="text-xs text-red-500/70 font-mono mt-2">
5858
{parsedError.stack}
5959
</div>
60-
) : (
61-
parsedError.detail && (
62-
<div className="text-xs text-red-500/70 font-mono mt-2">
63-
{parsedError.detail}
64-
</div>
65-
)
6660
)}
6761
</div>
6862
) : (

packages/ui-tars/sdk/src/GUIAgent.ts

+34-28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
SYSTEM_PROMPT,
3333
SYSTEM_PROMPT_TEMPLATE,
3434
} from './constants';
35+
import { InternalServerError } from 'openai';
3536

3637
export class GUIAgent<T extends Operator> extends BaseGUIAgent<
3738
GUIAgentConfig<T>
@@ -489,66 +490,71 @@ export class GUIAgent<T extends Operator> extends BaseGUIAgent<
489490
error: unknown,
490491
type: ErrorStatusEnum | null = null,
491492
): GUIAgentError {
492-
if (
493-
error instanceof Error &&
494-
'status' in error &&
495-
'error' in error &&
496-
error.status === 500 &&
497-
typeof error.error === 'string' &&
498-
error.error.includes('unhandled errors in a TaskGroup')
499-
) {
500-
return new GUIAgentError(
493+
this.logger.error('[GUIAgent] guiAgentErrorParser:', error);
494+
495+
let parseError = null;
496+
497+
if (error instanceof InternalServerError) {
498+
this.logger.error(
499+
'[GUIAgent] guiAgentErrorParser instanceof InternalServerError.',
500+
);
501+
parseError = new GUIAgentError(
501502
ErrorStatusEnum.MODEL_SERVICE_ERROR,
502503
error.message,
503504
error.stack,
504-
JSON.stringify(error),
505505
);
506506
}
507507

508-
if (type === ErrorStatusEnum.REACH_MAXLOOP_ERROR) {
509-
return new GUIAgentError(
508+
if (!parseError && type === ErrorStatusEnum.REACH_MAXLOOP_ERROR) {
509+
parseError = new GUIAgentError(
510510
ErrorStatusEnum.REACH_MAXLOOP_ERROR,
511511
'Has reached max loop count',
512512
);
513513
}
514514

515-
if (type === ErrorStatusEnum.SCREENSHOT_RETRY_ERROR) {
516-
return new GUIAgentError(
515+
if (!parseError && type === ErrorStatusEnum.SCREENSHOT_RETRY_ERROR) {
516+
parseError = new GUIAgentError(
517517
ErrorStatusEnum.SCREENSHOT_RETRY_ERROR,
518518
'Too many screenshot failures',
519519
);
520520
}
521521

522-
if (type === ErrorStatusEnum.INVOKE_RETRY_ERROR) {
523-
return new GUIAgentError(
522+
if (!parseError && type === ErrorStatusEnum.INVOKE_RETRY_ERROR) {
523+
parseError = new GUIAgentError(
524524
ErrorStatusEnum.INVOKE_RETRY_ERROR,
525525
'Too many model invoke failures',
526526
'null',
527-
JSON.stringify(error),
528527
);
529528
}
530529

531-
if (type === ErrorStatusEnum.EXECUTE_RETRY_ERROR) {
532-
return new GUIAgentError(
530+
if (!parseError && type === ErrorStatusEnum.EXECUTE_RETRY_ERROR) {
531+
parseError = new GUIAgentError(
533532
ErrorStatusEnum.EXECUTE_RETRY_ERROR,
534533
'Too many action execute failures',
535534
'null',
536-
JSON.stringify(error),
537535
);
538536
}
539537

540-
if (type === ErrorStatusEnum.ENVIRONMENT_ERROR) {
541-
return new GUIAgentError(
538+
if (!parseError && type === ErrorStatusEnum.ENVIRONMENT_ERROR) {
539+
parseError = new GUIAgentError(
542540
ErrorStatusEnum.ENVIRONMENT_ERROR,
543541
'The environment error occurred when parsing the action',
544542
);
545543
}
546544

547-
return new GUIAgentError(
548-
ErrorStatusEnum.UNKNOWN_ERROR,
549-
error instanceof Error ? error.message : 'Unknown error occurred',
550-
error instanceof Error ? error.stack || 'null' : 'null',
551-
JSON.stringify(error),
552-
);
545+
if (!parseError) {
546+
parseError = new GUIAgentError(
547+
ErrorStatusEnum.UNKNOWN_ERROR,
548+
error instanceof Error ? error.message : 'Unknown error occurred',
549+
error instanceof Error ? error.stack || 'null' : 'null',
550+
);
551+
}
552+
553+
if (!parseError.stack) {
554+
// Avoid guiAgentErrorParser it self in stack trace
555+
Error.captureStackTrace(parseError, this.guiAgentErrorParser);
556+
}
557+
558+
return parseError;
553559
}
554560
}

packages/ui-tars/shared/src/types/agent.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,15 @@ export enum ErrorStatusEnum {
2525
}
2626

2727
export class GUIAgentError extends Error {
28-
code: ErrorStatusEnum;
28+
status: ErrorStatusEnum;
2929
message: string;
3030
stack?: string;
31-
detail?: string;
3231

33-
constructor(
34-
code: ErrorStatusEnum,
35-
message: string,
36-
stack?: string,
37-
detail?: string,
38-
) {
39-
super(code.toString());
40-
this.code = code;
32+
constructor(status: ErrorStatusEnum, message: string, stack?: string) {
33+
super(message);
34+
this.status = status;
4135
this.message = message;
4236
this.stack = stack;
43-
this.detail = detail;
4437
}
4538
}
4639

0 commit comments

Comments
 (0)