Repository navigation
Expand file tree
/
Copy patherror.ts
More file actions
88 lines (85 loc) · 2.56 KB
/
Copy patherror.ts
File metadata and controls
88 lines (85 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export type ErrorCode =
| 'LLM_TIMEOUT'
| 'LLM_RATE_LIMITED'
| 'LLM_FAILED'
| 'CONTEXT_OVERFLOW'
| 'TOOL_NOT_FOUND'
| 'TOOL_NOT_ALLOWED'
| 'TOOL_EXECUTION_FAILED'
| 'PATH_NOT_ALLOWED'
| 'MAX_STEPS_REACHED'
| 'CONFIG_MISSING'
| 'CONFIG_INVALID'
| 'SESSION_CORRUPTED'
| 'SESSION_NOT_FOUND'
| 'SESSION_WORKSPACE_MISMATCH'
| 'AGENT_ABORTED'
| 'AGENT_LOOP_DETECTED'
| 'EMPTY_RESPONSE'
| 'SESSION_IO_ERROR';
export class AgentError extends Error {
constructor(
public readonly code: ErrorCode,
message: string,
public readonly cause?: unknown,
public readonly context?: Record<string, unknown>
) {
super(`[${code}] ${message}`);
this.name = 'AgentError';
}
static llmTimeout() {
return new AgentError('LLM_TIMEOUT', 'LLM call timed out');
}
static llmFailed(msg: unknown) {
return new AgentError('LLM_FAILED', String(msg));
}
static contextOverflow(provider: string, cause?: unknown) {
return new AgentError('CONTEXT_OVERFLOW', `Context window exceeded for ${provider}`, cause);
}
static toolNotFound(name: string) {
return new AgentError('TOOL_NOT_FOUND', `Tool "${name}" not found`);
}
static toolNotAllowed(name: string) {
return new AgentError('TOOL_NOT_ALLOWED', `Tool "${name}" is not allowed`);
}
static toolExecutionFailed(name: string, e: unknown) {
return new AgentError('TOOL_EXECUTION_FAILED', `Tool "${name}" failed: ${String(e)}`, e);
}
static pathNotAllowed(path: string) {
return new AgentError('PATH_NOT_ALLOWED', `Path "${path}" is outside allowed scope`);
}
static maxStepsReached(max: number) {
return new AgentError('MAX_STEPS_REACHED', `Max steps (${max}) reached`);
}
static configMissing(msg: string) {
return new AgentError('CONFIG_MISSING', msg);
}
static sessionNotFound(sessionId: string) {
return new AgentError('SESSION_NOT_FOUND', `Session "${sessionId}" not found`);
}
static sessionWorkspaceMismatch(sessionId: string, expectedCwd: string) {
return new AgentError(
'SESSION_WORKSPACE_MISMATCH',
`Session "${sessionId}" belongs to a different project. cd to: ${expectedCwd}`,
undefined,
{ sessionId, expectedCwd }
);
}
httpStatus(): number {
switch (this.code) {
case 'CONFIG_MISSING':
case 'CONFIG_INVALID':
return 400;
case 'SESSION_NOT_FOUND':
return 404;
case 'SESSION_WORKSPACE_MISMATCH':
return 409;
case 'TOOL_NOT_ALLOWED':
return 403;
case 'LLM_RATE_LIMITED':
return 429;
default:
return 500;
}
}
}