-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathtypes.ts
More file actions
470 lines (415 loc) · 11.3 KB
/
Copy pathtypes.ts
File metadata and controls
470 lines (415 loc) · 11.3 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import type {
AgentCapabilities,
AnyMessage,
McpServer,
RequestPermissionRequest,
SessionNotification,
SessionConfigOption,
SessionInfo,
SetSessionConfigOptionResponse,
StopReason,
ToolKind,
} from "@agentclientprotocol/sdk";
export type { McpServer, SessionNotification } from "@agentclientprotocol/sdk";
import type { PromptInput } from "./prompt-content.js";
export type AcpPermissionRequest = {
sessionId: string;
raw: RequestPermissionRequest;
inferredKind: ToolKind | undefined;
};
export type AcpPermissionDecision =
| { outcome: "allow_once" }
| { outcome: "allow_always" }
| { outcome: "reject_once" }
| { outcome: "reject_always" }
| { outcome: "cancel" };
export const EXIT_CODES = {
SUCCESS: 0,
ERROR: 1,
USAGE: 2,
TIMEOUT: 3,
NO_SESSION: 4,
PERMISSION_DENIED: 5,
INTERRUPTED: 130,
} as const;
export type ExitCode = (typeof EXIT_CODES)[keyof typeof EXIT_CODES];
export const OUTPUT_FORMATS = ["text", "json", "quiet"] as const;
export type OutputFormat = (typeof OUTPUT_FORMATS)[number];
export const PERMISSION_MODES = ["approve-all", "approve-reads", "deny-all"] as const;
export type PermissionMode = (typeof PERMISSION_MODES)[number];
export const AUTH_POLICIES = ["skip", "fail"] as const;
export type AuthPolicy = (typeof AUTH_POLICIES)[number];
export const NON_INTERACTIVE_PERMISSION_POLICIES = ["deny", "fail"] as const;
export type NonInteractivePermissionPolicy = (typeof NON_INTERACTIVE_PERMISSION_POLICIES)[number];
export const PERMISSION_POLICY_ACTIONS = ["approve", "deny", "escalate"] as const;
export type PermissionPolicyAction = (typeof PERMISSION_POLICY_ACTIONS)[number];
export type PermissionPolicy = {
autoApprove?: string[];
autoDeny?: string[];
escalate?: string[];
defaultAction?: PermissionPolicyAction;
};
export type PermissionEscalationEvent = {
type: "permission_escalation";
sessionId: string;
toolCallId: string;
toolName?: string;
toolTitle: string;
toolInput?: unknown;
toolKind?: ToolKind;
action: "escalate";
matchedRule?: string;
message: string;
timestamp: string;
};
export const SESSION_RESUME_POLICIES = ["allow-new", "same-session-only"] as const;
export type SessionResumePolicy = (typeof SESSION_RESUME_POLICIES)[number];
export const OUTPUT_STREAMS = ["prompt", "control"] as const;
export type OutputStream = (typeof OUTPUT_STREAMS)[number];
export type AcpJsonRpcMessage = AnyMessage;
export type AcpMessageDirection = "outbound" | "inbound";
export const OUTPUT_ERROR_CODES = [
"NO_SESSION",
"TIMEOUT",
"PERMISSION_DENIED",
"PERMISSION_PROMPT_UNAVAILABLE",
"RUNTIME",
"USAGE",
] as const;
export type OutputErrorCode = (typeof OUTPUT_ERROR_CODES)[number];
export const OUTPUT_ERROR_ORIGINS = ["cli", "runtime", "queue", "acp"] as const;
export type OutputErrorOrigin = (typeof OUTPUT_ERROR_ORIGINS)[number];
export const QUEUE_ERROR_DETAIL_CODES = [
"QUEUE_OWNER_CLOSED",
"QUEUE_OWNER_SHUTTING_DOWN",
"QUEUE_OWNER_OVERLOADED",
"QUEUE_OWNER_GENERATION_MISMATCH",
"QUEUE_REQUEST_INVALID",
"QUEUE_REQUEST_PAYLOAD_INVALID_JSON",
"QUEUE_ACK_MISSING",
"QUEUE_DISCONNECTED_BEFORE_ACK",
"QUEUE_DISCONNECTED_BEFORE_COMPLETION",
"QUEUE_PROTOCOL_INVALID_JSON",
"QUEUE_PROTOCOL_MALFORMED_MESSAGE",
"QUEUE_PROTOCOL_UNEXPECTED_RESPONSE",
"QUEUE_NOT_ACCEPTING_REQUESTS",
"QUEUE_CONTROL_REQUEST_FAILED",
"QUEUE_RUNTIME_PROMPT_FAILED",
] as const;
export type QueueErrorDetailCode = (typeof QUEUE_ERROR_DETAIL_CODES)[number];
export type OutputErrorAcpPayload = {
code: number;
message: string;
data?: unknown;
};
export type PermissionStats = {
requested: number;
approved: number;
denied: number;
cancelled: number;
};
export type ClientOperationMethod =
| "fs/read_text_file"
| "fs/write_text_file"
| "terminal/create"
| "terminal/output"
| "terminal/wait_for_exit"
| "terminal/kill"
| "terminal/release";
export type ClientOperationStatus = "running" | "completed" | "failed";
export type ClientOperation = {
method: ClientOperationMethod;
status: ClientOperationStatus;
summary: string;
details?: string;
timestamp: string;
};
export type SessionEventLog = {
active_path: string;
segment_count: number;
max_segment_bytes: number;
max_segments: number;
last_write_at?: string;
last_write_error?: string | null;
};
export type PerfMetricSummary = {
count: number;
totalMs: number;
maxMs: number;
};
export type PerfMetricsSnapshot = {
counters: Record<string, number>;
timings: Record<string, PerfMetricSummary>;
gauges: Record<string, number>;
};
export type OutputFormatterContext = {
sessionId: string;
};
export type OutputPolicy = {
format: OutputFormat;
jsonStrict: boolean;
suppressReads: boolean;
suppressNonJsonStderr: boolean;
queueErrorAlreadyEmitted: boolean;
suppressSdkConsoleErrors: boolean;
};
export type OutputErrorEmissionPolicy = {
queueErrorAlreadyEmitted: boolean;
};
export interface OutputFormatter {
setContext(context: OutputFormatterContext): void;
onAcpMessage(message: AcpJsonRpcMessage): void;
onError(params: {
code: OutputErrorCode;
detailCode?: string;
origin?: OutputErrorOrigin;
message: string;
retryable?: boolean;
acp?: OutputErrorAcpPayload;
timestamp?: string;
}): void;
onPermissionEscalation(event: PermissionEscalationEvent): void;
flush(): void;
}
export type AcpClientOptions = {
agentCommand: string;
cwd: string;
mcpServers?: McpServer[];
permissionMode: PermissionMode;
nonInteractivePermissions?: NonInteractivePermissionPolicy;
permissionPolicy?: PermissionPolicy;
authCredentials?: Record<string, string>;
authPolicy?: AuthPolicy;
terminal?: boolean;
suppressSdkConsoleErrors?: boolean;
verbose?: boolean;
sessionOptions?: {
model?: string;
allowedTools?: string[];
maxTurns?: number;
systemPrompt?: string | { append: string };
env?: Record<string, string>;
};
onAcpMessage?: (direction: AcpMessageDirection, message: AcpJsonRpcMessage) => void;
onAcpOutputMessage?: (direction: AcpMessageDirection, message: AcpJsonRpcMessage) => void;
onSessionUpdate?: (notification: SessionNotification) => void;
onClientOperation?: (operation: ClientOperation) => void;
onPermissionEscalation?: (event: PermissionEscalationEvent) => void;
onPermissionRequest?: (
req: AcpPermissionRequest,
ctx: { signal: AbortSignal },
) => Promise<AcpPermissionDecision | undefined>;
};
export const SESSION_RECORD_SCHEMA = "acpx.session.v1" as const;
export type SessionMessageImage = {
source: string;
size?: {
width: number;
height: number;
} | null;
};
export type SessionMessageAudio = {
source: string;
mime_type: string;
};
export type SessionUserContent =
| {
Text: string;
}
| {
Mention: {
uri: string;
content: string;
};
}
| {
Image: SessionMessageImage;
}
| {
Audio: SessionMessageAudio;
};
export type SessionToolUse = {
id: string;
name: string;
raw_input: string;
input: unknown;
is_input_complete: boolean;
thought_signature?: string | null;
};
export type SessionToolResultContent =
| {
Text: string;
}
| {
Image: SessionMessageImage;
};
export type SessionToolResult = {
tool_use_id: string;
tool_name: string;
is_error: boolean;
content: SessionToolResultContent;
output?: unknown;
};
export type SessionAgentContent =
| {
Text: string;
}
| {
Thinking: {
text: string;
signature?: string | null;
};
}
| {
RedactedThinking: string;
}
| {
ToolUse: SessionToolUse;
};
export type SessionUserMessage = {
id: string;
content: SessionUserContent[];
};
export type SessionAgentMessage = {
content: SessionAgentContent[];
tool_results: Record<string, SessionToolResult>;
reasoning_details?: unknown;
};
export type SessionMessage =
| {
User: SessionUserMessage;
}
| {
Agent: SessionAgentMessage;
}
| "Resume";
export type SessionTokenUsage = {
input_tokens?: number;
output_tokens?: number;
cache_creation_input_tokens?: number;
cache_read_input_tokens?: number;
thought_tokens?: number;
total_tokens?: number;
};
export type SessionUsageCost = {
amount?: number;
currency?: string;
};
export type SessionAvailableCommand = {
name: string;
description?: string;
has_input?: boolean;
};
export type SessionConversation = {
title?: string | null;
messages: SessionMessage[];
updated_at: string;
cumulative_token_usage: SessionTokenUsage;
cumulative_cost?: SessionUsageCost;
request_token_usage: Record<string, SessionTokenUsage>;
};
export type SessionAcpxState = {
reset_on_next_ensure?: boolean;
current_mode_id?: string;
desired_mode_id?: string;
desired_config_options?: Record<string, string>;
current_model_id?: string;
available_models?: string[];
model_control?: "config_option" | "legacy_set_model";
available_commands?: SessionAvailableCommand[];
config_options?: SessionConfigOption[];
session_options?: {
model?: string;
allowed_tools?: string[];
max_turns?: number;
system_prompt?: string | { append: string };
env?: Record<string, string>;
};
};
export type SessionImportedFrom = {
recordId: string;
cwdOriginal: string;
exportedBy: string;
exportedAt: string;
};
export type SessionRecord = {
schema: typeof SESSION_RECORD_SCHEMA;
acpxRecordId: string;
acpSessionId: string;
agentSessionId?: string;
agentCommand: string;
cwd: string;
name?: string;
createdAt: string;
lastUsedAt: string;
lastSeq: number;
lastRequestId?: string;
eventLog: SessionEventLog;
closed?: boolean;
closedAt?: string;
pid?: number;
agentStartedAt?: string;
lastPromptAt?: string;
lastAgentExitCode?: number | null;
lastAgentExitSignal?: NodeJS.Signals | null;
lastAgentExitAt?: string;
lastAgentDisconnectReason?: string;
protocolVersion?: number;
agentCapabilities?: AgentCapabilities;
title?: string | null;
messages: SessionMessage[];
updated_at: string;
cumulative_token_usage: SessionTokenUsage;
cumulative_cost?: SessionUsageCost;
request_token_usage: Record<string, SessionTokenUsage>;
acpx?: SessionAcpxState;
importedFrom?: SessionImportedFrom;
};
export type RunPromptResult = {
stopReason: StopReason;
permissionStats: PermissionStats;
sessionId: string;
};
export type SessionSendResult = RunPromptResult & {
record: SessionRecord;
resumed: boolean;
loadError?: string;
};
export type SessionSetModeResult = {
record: SessionRecord;
resumed: boolean;
loadError?: string;
};
export type SessionSetConfigOptionResult = {
record: SessionRecord;
response: SetSessionConfigOptionResponse;
resumed: boolean;
loadError?: string;
};
export type SessionSetModelResult = {
record: SessionRecord;
response?: SetSessionConfigOptionResponse;
resumed: boolean;
loadError?: string;
};
export type SessionEnsureResult = {
record: SessionRecord;
created: boolean;
};
export type AgentSessionListResult = {
_meta?: {
[key: string]: unknown;
} | null;
source: "agent";
sessions: SessionInfo[];
cursor?: string;
cwd?: string;
nextCursor?: string | null;
};
export type SessionEnqueueResult = {
queued: true;
sessionId: string;
requestId: string;
};
export type SessionSendOutcome = SessionSendResult | SessionEnqueueResult;
export type { PromptInput };