@oh-my-pi/pi-utils 17.2.8 → 17.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/types/acp/connection.d.ts +118 -0
- package/dist/types/acp/protocol.d.ts +526 -0
- package/dist/types/acp/schema.d.ts +41 -0
- package/dist/types/acp/stream.d.ts +8 -0
- package/dist/types/acp/transport.d.ts +81 -0
- package/dist/types/acp.d.ts +6 -0
- package/dist/types/browsers.d.ts +68 -0
- package/dist/types/chalk.d.ts +125 -0
- package/dist/types/dates.d.ts +7 -0
- package/dist/types/docx/converter.d.ts +46 -0
- package/dist/types/docx/xml.d.ts +26 -0
- package/dist/types/docx/zip.d.ts +6 -0
- package/dist/types/docx.d.ts +11 -0
- package/dist/types/dom/core.d.ts +431 -0
- package/dist/types/dom/parser.d.ts +7 -0
- package/dist/types/dom/selector.d.ts +5 -0
- package/dist/types/dom.d.ts +5 -0
- package/dist/types/headers.d.ts +34 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/logger/rotating-file.d.ts +18 -0
- package/dist/types/lru.d.ts +46 -0
- package/dist/types/marked/core.d.ts +445 -0
- package/dist/types/marked.d.ts +2 -0
- package/dist/types/postmortem.d.ts +14 -0
- package/dist/types/procmgr.d.ts +16 -0
- package/dist/types/prompt.d.ts +2 -2
- package/dist/types/readability/readability.d.ts +9 -0
- package/dist/types/readability/readerable.d.ts +10 -0
- package/dist/types/readability/types.d.ts +70 -0
- package/dist/types/readability.d.ts +4 -0
- package/dist/types/template.d.ts +62 -0
- package/dist/types/turndown/gfm.d.ts +11 -0
- package/dist/types/turndown/html.d.ts +5 -0
- package/dist/types/turndown/service.d.ts +21 -0
- package/dist/types/turndown/types.d.ts +70 -0
- package/dist/types/turndown.d.ts +4 -0
- package/dist/types/version.d.ts +18 -0
- package/dist/types/vterm/buffer.d.ts +99 -0
- package/dist/types/vterm/terminal.d.ts +44 -0
- package/dist/types/vterm.d.ts +8 -0
- package/dist/types/xml.d.ts +31 -0
- package/package.json +2 -5
- package/src/acp/connection.ts +344 -0
- package/src/acp/protocol.ts +466 -0
- package/src/acp/schema.ts +160 -0
- package/src/acp/stream.ts +82 -0
- package/src/acp/transport.ts +213 -0
- package/src/acp.ts +6 -0
- package/src/browsers.ts +501 -0
- package/src/chalk.ts +312 -0
- package/src/dates.ts +194 -0
- package/src/docx/converter.ts +681 -0
- package/src/docx/xml.ts +166 -0
- package/src/docx/zip.ts +87 -0
- package/src/docx.ts +20 -0
- package/src/dom/core.ts +1254 -0
- package/src/dom/parser.ts +370 -0
- package/src/dom/selector.ts +290 -0
- package/src/dom.ts +33 -0
- package/src/fetch-retry.ts +7 -1
- package/src/frontmatter.ts +1 -1
- package/src/headers.ts +167 -0
- package/src/index.ts +1 -0
- package/src/logger/rotating-file.ts +149 -0
- package/src/logger.ts +12 -28
- package/src/lru.ts +185 -0
- package/src/marked/core.ts +1576 -0
- package/src/marked.ts +2 -0
- package/src/postmortem.ts +44 -1
- package/src/procmgr.ts +21 -4
- package/src/prompt.ts +5 -22
- package/src/readability/readability.ts +533 -0
- package/src/readability/readerable.ts +51 -0
- package/src/readability/types.ts +72 -0
- package/src/readability.ts +11 -0
- package/src/template.ts +586 -0
- package/src/turndown/gfm.ts +106 -0
- package/src/turndown/html.ts +257 -0
- package/src/turndown/service.ts +334 -0
- package/src/turndown/types.ts +81 -0
- package/src/turndown.ts +5 -0
- package/src/version.ts +99 -0
- package/src/vterm/buffer.ts +218 -0
- package/src/vterm/terminal.ts +773 -0
- package/src/vterm.ts +8 -0
- package/src/which.ts +6 -4
- package/src/xml.ts +313 -0
- package/src/winston-daily-rotate-file.d.ts +0 -6
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
/** Behavior-compatible reimplementation of @agentclientprotocol/sdk's used protocol surface. */
|
|
2
|
+
|
|
3
|
+
/** Metadata reserved for protocol extensions. */
|
|
4
|
+
export interface Meta {
|
|
5
|
+
_meta?: Record<string, unknown> | null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** A value which may be produced asynchronously. */
|
|
9
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
10
|
+
/** ACP protocol version identifier. */
|
|
11
|
+
export type ProtocolVersion = number;
|
|
12
|
+
/** Current ACP protocol version. */
|
|
13
|
+
export const PROTOCOL_VERSION = 1;
|
|
14
|
+
/** Conversation session identifier. */
|
|
15
|
+
export type SessionId = string;
|
|
16
|
+
/** Tool call identifier. */
|
|
17
|
+
export type ToolCallId = string;
|
|
18
|
+
/** ACP tool category. */
|
|
19
|
+
export type ToolKind =
|
|
20
|
+
| "read"
|
|
21
|
+
| "edit"
|
|
22
|
+
| "delete"
|
|
23
|
+
| "move"
|
|
24
|
+
| "search"
|
|
25
|
+
| "execute"
|
|
26
|
+
| "think"
|
|
27
|
+
| "fetch"
|
|
28
|
+
| "switch_mode"
|
|
29
|
+
| "other";
|
|
30
|
+
/** ACP tool execution state. */
|
|
31
|
+
export type ToolCallStatus = "pending" | "in_progress" | "completed" | "failed";
|
|
32
|
+
/** ACP prompt stop reason. */
|
|
33
|
+
export type StopReason = "end_turn" | "max_tokens" | "max_turn_requests" | "refusal" | "cancelled";
|
|
34
|
+
/** ACP permission choice kind. */
|
|
35
|
+
export type PermissionOptionKind = "allow_once" | "allow_always" | "reject_once" | "reject_always";
|
|
36
|
+
|
|
37
|
+
/** Text content block. */
|
|
38
|
+
export interface TextContent extends Meta {
|
|
39
|
+
type: "text";
|
|
40
|
+
text: string;
|
|
41
|
+
}
|
|
42
|
+
/** Image content block. */
|
|
43
|
+
export interface ImageContent extends Meta {
|
|
44
|
+
type: "image";
|
|
45
|
+
data: string;
|
|
46
|
+
mimeType: string;
|
|
47
|
+
}
|
|
48
|
+
/** Audio content block. */
|
|
49
|
+
export interface AudioContent extends Meta {
|
|
50
|
+
type: "audio";
|
|
51
|
+
data: string;
|
|
52
|
+
mimeType: string;
|
|
53
|
+
}
|
|
54
|
+
/** Resource link content block. */
|
|
55
|
+
export interface ResourceLink extends Meta {
|
|
56
|
+
type: "resource_link";
|
|
57
|
+
uri: string;
|
|
58
|
+
name?: string;
|
|
59
|
+
title?: string;
|
|
60
|
+
mimeType?: string | null;
|
|
61
|
+
description?: string | null;
|
|
62
|
+
size?: number | null;
|
|
63
|
+
}
|
|
64
|
+
/** Text or blob resource embedded in a prompt. */
|
|
65
|
+
export type EmbeddedResourceValue = (
|
|
66
|
+
| { uri: string; text: string; mimeType?: string }
|
|
67
|
+
| { uri: string; blob: string; mimeType?: string }
|
|
68
|
+
) &
|
|
69
|
+
Meta;
|
|
70
|
+
/** Embedded resource content block. */
|
|
71
|
+
export interface EmbeddedResource extends Meta {
|
|
72
|
+
type: "resource";
|
|
73
|
+
resource: EmbeddedResourceValue;
|
|
74
|
+
}
|
|
75
|
+
/** Displayable ACP content. */
|
|
76
|
+
export type ContentBlock = TextContent | ImageContent | AudioContent | ResourceLink | EmbeddedResource;
|
|
77
|
+
/** Content wrapper used by tool calls. */
|
|
78
|
+
export interface ToolContent extends Meta {
|
|
79
|
+
type: "content";
|
|
80
|
+
content: ContentBlock;
|
|
81
|
+
}
|
|
82
|
+
/** File diff emitted by a tool call. */
|
|
83
|
+
export interface ToolDiff extends Meta {
|
|
84
|
+
type: "diff";
|
|
85
|
+
path: string;
|
|
86
|
+
oldText?: string | null;
|
|
87
|
+
newText: string;
|
|
88
|
+
}
|
|
89
|
+
/** Terminal reference emitted by a tool call. */
|
|
90
|
+
export interface ToolTerminal extends Meta {
|
|
91
|
+
type: "terminal";
|
|
92
|
+
terminalId: string;
|
|
93
|
+
}
|
|
94
|
+
/** Tool call output item. */
|
|
95
|
+
export type ToolCallContent = ToolContent | ToolDiff | ToolTerminal;
|
|
96
|
+
/** File location associated with a tool call. */
|
|
97
|
+
export interface ToolCallLocation extends Meta {
|
|
98
|
+
path: string;
|
|
99
|
+
line?: number | null;
|
|
100
|
+
}
|
|
101
|
+
/** Initial tool call notification. */
|
|
102
|
+
export interface ToolCall extends Meta {
|
|
103
|
+
toolCallId: ToolCallId;
|
|
104
|
+
title: string;
|
|
105
|
+
kind?: ToolKind | null;
|
|
106
|
+
status?: ToolCallStatus | null;
|
|
107
|
+
content?: ToolCallContent[] | null;
|
|
108
|
+
locations?: ToolCallLocation[] | null;
|
|
109
|
+
rawInput?: unknown;
|
|
110
|
+
rawOutput?: unknown;
|
|
111
|
+
}
|
|
112
|
+
/** Partial update to a tool call. */
|
|
113
|
+
export interface ToolCallUpdate extends Meta {
|
|
114
|
+
toolCallId: ToolCallId;
|
|
115
|
+
title?: string | null;
|
|
116
|
+
kind?: ToolKind | null;
|
|
117
|
+
status?: ToolCallStatus | null;
|
|
118
|
+
content?: ToolCallContent[] | null;
|
|
119
|
+
locations?: ToolCallLocation[] | null;
|
|
120
|
+
rawInput?: unknown;
|
|
121
|
+
rawOutput?: unknown;
|
|
122
|
+
}
|
|
123
|
+
/** Permission option presented by the client. */
|
|
124
|
+
export interface PermissionOption extends Meta {
|
|
125
|
+
optionId: string;
|
|
126
|
+
name: string;
|
|
127
|
+
kind: PermissionOptionKind;
|
|
128
|
+
}
|
|
129
|
+
/** Request for permission to execute a tool call. */
|
|
130
|
+
export interface RequestPermissionRequest extends Meta {
|
|
131
|
+
sessionId: SessionId;
|
|
132
|
+
toolCall: ToolCallUpdate;
|
|
133
|
+
options: PermissionOption[];
|
|
134
|
+
}
|
|
135
|
+
/** Permission request response. */
|
|
136
|
+
export interface RequestPermissionResponse extends Meta {
|
|
137
|
+
outcome: { outcome: "cancelled" } | { outcome: "selected"; optionId: string };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Client filesystem capabilities. */
|
|
141
|
+
export interface FileSystemCapabilities {
|
|
142
|
+
readTextFile?: boolean;
|
|
143
|
+
writeTextFile?: boolean;
|
|
144
|
+
}
|
|
145
|
+
/** Capabilities advertised by an ACP client. */
|
|
146
|
+
export interface ClientCapabilities {
|
|
147
|
+
fs?: FileSystemCapabilities;
|
|
148
|
+
terminal?: boolean;
|
|
149
|
+
auth?: { terminal?: boolean };
|
|
150
|
+
elicitation?: { form?: Record<string, unknown>; url?: Record<string, unknown> };
|
|
151
|
+
_meta?: Record<string, unknown>;
|
|
152
|
+
}
|
|
153
|
+
/** Implementation identity sent during initialization. */
|
|
154
|
+
export interface Implementation {
|
|
155
|
+
name: string;
|
|
156
|
+
version: string;
|
|
157
|
+
title?: string;
|
|
158
|
+
}
|
|
159
|
+
/** Initialization request. */
|
|
160
|
+
export interface InitializeRequest extends Meta {
|
|
161
|
+
protocolVersion: ProtocolVersion;
|
|
162
|
+
clientCapabilities?: ClientCapabilities;
|
|
163
|
+
clientInfo?: Implementation | null;
|
|
164
|
+
}
|
|
165
|
+
/** Authentication method using an environment variable. */
|
|
166
|
+
export interface AuthMethodEnvVar extends Meta {
|
|
167
|
+
type: "env_var";
|
|
168
|
+
id: string;
|
|
169
|
+
name: string;
|
|
170
|
+
description?: string | null;
|
|
171
|
+
variable: string;
|
|
172
|
+
}
|
|
173
|
+
/** Authentication method using a terminal command. */
|
|
174
|
+
export interface AuthMethodTerminal extends Meta {
|
|
175
|
+
type: "terminal";
|
|
176
|
+
id: string;
|
|
177
|
+
name: string;
|
|
178
|
+
description?: string | null;
|
|
179
|
+
command?: string;
|
|
180
|
+
args?: string[];
|
|
181
|
+
}
|
|
182
|
+
/** Authentication method handled by the agent. */
|
|
183
|
+
export interface AuthMethodAgent extends Meta {
|
|
184
|
+
type?: "agent";
|
|
185
|
+
id: string;
|
|
186
|
+
name: string;
|
|
187
|
+
description?: string | null;
|
|
188
|
+
}
|
|
189
|
+
/** Authentication method advertised by an agent. */
|
|
190
|
+
export type AuthMethod = AuthMethodEnvVar | AuthMethodTerminal | AuthMethodAgent;
|
|
191
|
+
/** Initialization response. */
|
|
192
|
+
export interface InitializeResponse extends Meta {
|
|
193
|
+
protocolVersion: ProtocolVersion;
|
|
194
|
+
agentCapabilities?: Record<string, unknown>;
|
|
195
|
+
authMethods?: AuthMethod[];
|
|
196
|
+
agentInfo?: Implementation | null;
|
|
197
|
+
}
|
|
198
|
+
/** Authentication request. */
|
|
199
|
+
export interface AuthenticateRequest extends Meta {
|
|
200
|
+
methodId: string;
|
|
201
|
+
}
|
|
202
|
+
/** Authentication response. */
|
|
203
|
+
export interface AuthenticateResponse extends Meta {}
|
|
204
|
+
|
|
205
|
+
/** ACP MCP server configuration. */
|
|
206
|
+
export type McpServer = (
|
|
207
|
+
| { type?: "stdio"; name: string; command: string; args?: string[]; env: Array<{ name: string; value: string }> }
|
|
208
|
+
| { type: "http" | "sse" | "acp"; name: string; url: string; headers: Array<{ name: string; value: string }> }
|
|
209
|
+
) &
|
|
210
|
+
Meta;
|
|
211
|
+
/** Session mode descriptor. */
|
|
212
|
+
export interface SessionMode extends Meta {
|
|
213
|
+
id: string;
|
|
214
|
+
name: string;
|
|
215
|
+
description?: string | null;
|
|
216
|
+
}
|
|
217
|
+
/** Current session mode and available choices. */
|
|
218
|
+
export interface SessionModeState extends Meta {
|
|
219
|
+
currentModeId: string;
|
|
220
|
+
availableModes: SessionMode[];
|
|
221
|
+
}
|
|
222
|
+
/** Select configuration value. */
|
|
223
|
+
export interface SessionConfigSelectOption extends Meta {
|
|
224
|
+
value: string;
|
|
225
|
+
name: string;
|
|
226
|
+
description?: string | null;
|
|
227
|
+
}
|
|
228
|
+
/** Session configuration option. */
|
|
229
|
+
export type SessionConfigOption = (
|
|
230
|
+
| { type: "select"; currentValue: string; options: SessionConfigSelectOption[] }
|
|
231
|
+
| { type: "boolean"; currentValue: boolean }
|
|
232
|
+
) & { id: string; name: string; description?: string | null; category?: string | null } & Meta;
|
|
233
|
+
/** Shared fields for creating or restoring a session. */
|
|
234
|
+
export interface SessionSetupRequest extends Meta {
|
|
235
|
+
cwd: string;
|
|
236
|
+
mcpServers: McpServer[];
|
|
237
|
+
additionalDirectories?: string[];
|
|
238
|
+
}
|
|
239
|
+
/** New-session request. */
|
|
240
|
+
export interface NewSessionRequest extends SessionSetupRequest {}
|
|
241
|
+
/** New-session response. */
|
|
242
|
+
export interface NewSessionResponse extends Meta {
|
|
243
|
+
sessionId: SessionId;
|
|
244
|
+
modes?: SessionModeState | null;
|
|
245
|
+
configOptions?: SessionConfigOption[] | null;
|
|
246
|
+
}
|
|
247
|
+
/** Load-session request. */
|
|
248
|
+
export interface LoadSessionRequest extends SessionSetupRequest {
|
|
249
|
+
sessionId: SessionId;
|
|
250
|
+
}
|
|
251
|
+
/** Load-session response. */
|
|
252
|
+
export interface LoadSessionResponse extends Meta {
|
|
253
|
+
modes?: SessionModeState | null;
|
|
254
|
+
configOptions?: SessionConfigOption[] | null;
|
|
255
|
+
}
|
|
256
|
+
/** Resume-session request. */
|
|
257
|
+
export interface ResumeSessionRequest extends LoadSessionRequest {}
|
|
258
|
+
/** Resume-session response. */
|
|
259
|
+
export interface ResumeSessionResponse extends LoadSessionResponse {}
|
|
260
|
+
/** Fork-session request. */
|
|
261
|
+
export interface ForkSessionRequest extends LoadSessionRequest {}
|
|
262
|
+
/** Fork-session response. */
|
|
263
|
+
export interface ForkSessionResponse extends NewSessionResponse {}
|
|
264
|
+
/** Close-session request. */
|
|
265
|
+
export interface CloseSessionRequest extends Meta {
|
|
266
|
+
sessionId: SessionId;
|
|
267
|
+
}
|
|
268
|
+
/** Close-session response. */
|
|
269
|
+
export interface CloseSessionResponse extends Meta {}
|
|
270
|
+
/** Session-list request. */
|
|
271
|
+
export interface ListSessionsRequest extends Meta {
|
|
272
|
+
cwd?: string | null;
|
|
273
|
+
cursor?: string | null;
|
|
274
|
+
additionalDirectories?: string[];
|
|
275
|
+
}
|
|
276
|
+
/** Session metadata. */
|
|
277
|
+
export interface SessionInfo extends Meta {
|
|
278
|
+
sessionId: SessionId;
|
|
279
|
+
cwd: string;
|
|
280
|
+
additionalDirectories?: string[];
|
|
281
|
+
title?: string | null;
|
|
282
|
+
updatedAt?: string | null;
|
|
283
|
+
}
|
|
284
|
+
/** Session-list response. */
|
|
285
|
+
export interface ListSessionsResponse extends Meta {
|
|
286
|
+
sessions: SessionInfo[];
|
|
287
|
+
nextCursor?: string | null;
|
|
288
|
+
}
|
|
289
|
+
/** Set-mode request. */
|
|
290
|
+
export interface SetSessionModeRequest extends Meta {
|
|
291
|
+
sessionId: SessionId;
|
|
292
|
+
modeId: string;
|
|
293
|
+
}
|
|
294
|
+
/** Set-mode response. */
|
|
295
|
+
export interface SetSessionModeResponse extends Meta {}
|
|
296
|
+
/** Set-config request. */
|
|
297
|
+
export interface SetSessionConfigOptionRequest extends Meta {
|
|
298
|
+
sessionId: SessionId;
|
|
299
|
+
configId: string;
|
|
300
|
+
value: string | boolean;
|
|
301
|
+
}
|
|
302
|
+
/** Set-config response. */
|
|
303
|
+
export interface SetSessionConfigOptionResponse extends Meta {
|
|
304
|
+
configOptions: SessionConfigOption[];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Slash command advertised by an agent. */
|
|
308
|
+
export interface AvailableCommand extends Meta {
|
|
309
|
+
name: string;
|
|
310
|
+
description: string;
|
|
311
|
+
input?: { hint: string } | null;
|
|
312
|
+
}
|
|
313
|
+
/** Session plan entry. */
|
|
314
|
+
export interface PlanEntry extends Meta {
|
|
315
|
+
content: string;
|
|
316
|
+
priority: "high" | "medium" | "low";
|
|
317
|
+
status: "pending" | "in_progress" | "completed";
|
|
318
|
+
}
|
|
319
|
+
/** Session update payload. */
|
|
320
|
+
export type SessionUpdate =
|
|
321
|
+
| ({
|
|
322
|
+
sessionUpdate: "agent_message_chunk" | "agent_thought_chunk" | "user_message_chunk";
|
|
323
|
+
content: ContentBlock;
|
|
324
|
+
messageId?: string;
|
|
325
|
+
} & Meta)
|
|
326
|
+
| ({ sessionUpdate: "tool_call" } & ToolCall)
|
|
327
|
+
| ({ sessionUpdate: "tool_call_update" } & ToolCallUpdate)
|
|
328
|
+
| ({ sessionUpdate: "plan"; entries: PlanEntry[] } & Meta)
|
|
329
|
+
| ({ sessionUpdate: "current_mode_update"; currentModeId: string } & Meta)
|
|
330
|
+
| ({ sessionUpdate: "config_option_update"; configOptions: SessionConfigOption[] } & Meta)
|
|
331
|
+
| ({ sessionUpdate: "available_commands_update"; availableCommands: AvailableCommand[] } & Meta)
|
|
332
|
+
| ({ sessionUpdate: "session_info_update"; title?: string | null; updatedAt?: string | null } & Meta)
|
|
333
|
+
| ({
|
|
334
|
+
sessionUpdate: "usage_update";
|
|
335
|
+
size: number;
|
|
336
|
+
used: number;
|
|
337
|
+
cost?: { amount: number; currency: string } | null;
|
|
338
|
+
} & Meta);
|
|
339
|
+
/** Notification containing a session update. */
|
|
340
|
+
export interface SessionNotification extends Meta {
|
|
341
|
+
sessionId: SessionId;
|
|
342
|
+
update: SessionUpdate;
|
|
343
|
+
}
|
|
344
|
+
/** Prompt request. */
|
|
345
|
+
export interface PromptRequest extends Meta {
|
|
346
|
+
sessionId: SessionId;
|
|
347
|
+
prompt: ContentBlock[];
|
|
348
|
+
}
|
|
349
|
+
/** Prompt token usage. */
|
|
350
|
+
export interface Usage extends Meta {
|
|
351
|
+
totalTokens: number;
|
|
352
|
+
inputTokens: number;
|
|
353
|
+
outputTokens: number;
|
|
354
|
+
cachedReadTokens?: number | null;
|
|
355
|
+
cachedWriteTokens?: number | null;
|
|
356
|
+
thoughtTokens?: number | null;
|
|
357
|
+
}
|
|
358
|
+
/** Prompt response. */
|
|
359
|
+
export interface PromptResponse extends Meta {
|
|
360
|
+
stopReason: StopReason;
|
|
361
|
+
usage?: Usage | null;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Read-file request. */
|
|
365
|
+
export interface ReadTextFileRequest extends Meta {
|
|
366
|
+
sessionId: string;
|
|
367
|
+
path: string;
|
|
368
|
+
line?: number | null;
|
|
369
|
+
limit?: number | null;
|
|
370
|
+
}
|
|
371
|
+
/** Read-file response. */
|
|
372
|
+
export interface ReadTextFileResponse extends Meta {
|
|
373
|
+
content: string;
|
|
374
|
+
}
|
|
375
|
+
/** Write-file request. */
|
|
376
|
+
export interface WriteTextFileRequest extends Meta {
|
|
377
|
+
sessionId: string;
|
|
378
|
+
path: string;
|
|
379
|
+
content: string;
|
|
380
|
+
}
|
|
381
|
+
/** Write-file response. */
|
|
382
|
+
export interface WriteTextFileResponse extends Meta {}
|
|
383
|
+
/** Create-terminal request. */
|
|
384
|
+
export interface CreateTerminalRequest extends Meta {
|
|
385
|
+
sessionId: string;
|
|
386
|
+
command: string;
|
|
387
|
+
args?: string[];
|
|
388
|
+
env?: Array<{ name: string; value: string }>;
|
|
389
|
+
cwd?: string;
|
|
390
|
+
outputByteLimit?: number;
|
|
391
|
+
}
|
|
392
|
+
/** Terminal output response. */
|
|
393
|
+
export interface TerminalOutputResponse extends Meta {
|
|
394
|
+
output: string;
|
|
395
|
+
truncated: boolean;
|
|
396
|
+
exitStatus?: { exitCode?: number | null; signal?: string | null } | null;
|
|
397
|
+
}
|
|
398
|
+
/** Terminal exit response. */
|
|
399
|
+
export interface WaitForTerminalExitResponse extends Meta {
|
|
400
|
+
exitCode?: number | null;
|
|
401
|
+
signal?: string | null;
|
|
402
|
+
}
|
|
403
|
+
/** Empty terminal response. */
|
|
404
|
+
export interface TerminalActionResponse extends Meta {}
|
|
405
|
+
|
|
406
|
+
/** Elicitation scalar value. */
|
|
407
|
+
export type ElicitationContentValue = string | number | boolean | string[];
|
|
408
|
+
/** Elicitation property schema. */
|
|
409
|
+
export type ElicitationPropertySchema = Record<string, unknown> & {
|
|
410
|
+
type: string;
|
|
411
|
+
title?: string;
|
|
412
|
+
description?: string;
|
|
413
|
+
};
|
|
414
|
+
/** Elicitation creation request. */
|
|
415
|
+
export type CreateElicitationRequest =
|
|
416
|
+
| ({
|
|
417
|
+
mode: "form";
|
|
418
|
+
sessionId: string;
|
|
419
|
+
message: string;
|
|
420
|
+
requestedSchema: {
|
|
421
|
+
type: "object";
|
|
422
|
+
properties: Record<string, ElicitationPropertySchema>;
|
|
423
|
+
required?: string[];
|
|
424
|
+
};
|
|
425
|
+
} & Meta)
|
|
426
|
+
| ({ mode: "url"; sessionId: string; message: string; url: string; elicitationId: string } & Meta)
|
|
427
|
+
| ({
|
|
428
|
+
mode: string;
|
|
429
|
+
sessionId: string;
|
|
430
|
+
message: string;
|
|
431
|
+
requestedSchema?: {
|
|
432
|
+
type: "object";
|
|
433
|
+
properties: Record<string, ElicitationPropertySchema>;
|
|
434
|
+
required?: string[];
|
|
435
|
+
};
|
|
436
|
+
url?: string;
|
|
437
|
+
elicitationId?: string;
|
|
438
|
+
} & Meta);
|
|
439
|
+
/** Elicitation creation response. */
|
|
440
|
+
export type CreateElicitationResponse =
|
|
441
|
+
| ({ action: "accept"; content: Record<string, ElicitationContentValue> } & Meta)
|
|
442
|
+
| ({ action: "decline" | "cancel"; content?: never } & Meta)
|
|
443
|
+
| ({ action: string; content?: Record<string, ElicitationContentValue> } & Meta);
|
|
444
|
+
/** Completion notification for URL elicitation. */
|
|
445
|
+
export interface CompleteElicitationNotification extends Meta {
|
|
446
|
+
sessionId: string;
|
|
447
|
+
elicitationId: string;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** Agent-side request handler contract. */
|
|
451
|
+
export interface Agent {
|
|
452
|
+
initialize(params: InitializeRequest): MaybePromise<InitializeResponse>;
|
|
453
|
+
newSession(params: NewSessionRequest): MaybePromise<NewSessionResponse>;
|
|
454
|
+
prompt(params: PromptRequest): MaybePromise<PromptResponse>;
|
|
455
|
+
cancel(params: { sessionId: string }): MaybePromise<void>;
|
|
456
|
+
authenticate?(params: AuthenticateRequest): MaybePromise<AuthenticateResponse | void>;
|
|
457
|
+
loadSession?(params: LoadSessionRequest): MaybePromise<LoadSessionResponse | void>;
|
|
458
|
+
listSessions?(params: ListSessionsRequest): MaybePromise<ListSessionsResponse>;
|
|
459
|
+
resumeSession?(params: ResumeSessionRequest): MaybePromise<ResumeSessionResponse>;
|
|
460
|
+
unstable_forkSession?(params: ForkSessionRequest): MaybePromise<ForkSessionResponse>;
|
|
461
|
+
closeSession?(params: CloseSessionRequest): MaybePromise<CloseSessionResponse | void>;
|
|
462
|
+
setSessionMode?(params: SetSessionModeRequest): MaybePromise<SetSessionModeResponse | void>;
|
|
463
|
+
setSessionConfigOption?(params: SetSessionConfigOptionRequest): MaybePromise<SetSessionConfigOptionResponse>;
|
|
464
|
+
extMethod?(method: string, params: Record<string, unknown>): MaybePromise<Record<string, unknown>>;
|
|
465
|
+
extNotification?(method: string, params: Record<string, unknown>): MaybePromise<void>;
|
|
466
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ForkSessionResponse,
|
|
3
|
+
LoadSessionResponse,
|
|
4
|
+
NewSessionResponse,
|
|
5
|
+
PromptResponse,
|
|
6
|
+
SessionNotification,
|
|
7
|
+
} from "./protocol";
|
|
8
|
+
|
|
9
|
+
/** Validation failure compatible with the used Zod error surface. */
|
|
10
|
+
export interface ValidationError {
|
|
11
|
+
issues: Array<{ path: Array<string | number>; message: string }>;
|
|
12
|
+
}
|
|
13
|
+
/** Successful validation result. */
|
|
14
|
+
export interface ValidationSuccess<T> {
|
|
15
|
+
success: true;
|
|
16
|
+
data: T;
|
|
17
|
+
}
|
|
18
|
+
/** Failed validation result. */
|
|
19
|
+
export interface ValidationFailure {
|
|
20
|
+
success: false;
|
|
21
|
+
error: ValidationError;
|
|
22
|
+
}
|
|
23
|
+
/** Runtime validator compatible with the used schema call shape. */
|
|
24
|
+
export interface Validator<T> {
|
|
25
|
+
safeParse(value: unknown): ValidationSuccess<T> | ValidationFailure;
|
|
26
|
+
parse(value: unknown): T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function validator<T>(check: (value: unknown) => boolean, label: string): Validator<T> {
|
|
30
|
+
return {
|
|
31
|
+
safeParse(value) {
|
|
32
|
+
return check(value)
|
|
33
|
+
? { success: true, data: value as T }
|
|
34
|
+
: { success: false, error: { issues: [{ path: [], message: `Invalid ${label}` }] } };
|
|
35
|
+
},
|
|
36
|
+
parse(value) {
|
|
37
|
+
if (!check(value)) throw new Error(`Invalid ${label}`);
|
|
38
|
+
return value as T;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function objectWithString(value: unknown, key: string): boolean {
|
|
44
|
+
return typeof value === "object" && value !== null && typeof (value as Record<string, unknown>)[key] === "string";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function validModes(value: unknown): boolean {
|
|
48
|
+
if (value === undefined || value === null) return true;
|
|
49
|
+
if (typeof value !== "object" || value === null) return false;
|
|
50
|
+
const modes = value as Record<string, unknown>;
|
|
51
|
+
return (
|
|
52
|
+
typeof modes.currentModeId === "string" &&
|
|
53
|
+
Array.isArray(modes.availableModes) &&
|
|
54
|
+
modes.availableModes.every(mode => objectWithString(mode, "id") && objectWithString(mode, "name"))
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function sessionResponse(value: unknown, needsId: boolean): boolean {
|
|
59
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
60
|
+
const response = value as Record<string, unknown>;
|
|
61
|
+
if (needsId && typeof response.sessionId !== "string") return false;
|
|
62
|
+
if (!validModes(response.modes)) return false;
|
|
63
|
+
return (
|
|
64
|
+
response.configOptions === undefined || response.configOptions === null || Array.isArray(response.configOptions)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function contentBlock(value: unknown): boolean {
|
|
69
|
+
if (typeof value !== "object" || value === null) return false;
|
|
70
|
+
const block = value as Record<string, unknown>;
|
|
71
|
+
switch (block.type) {
|
|
72
|
+
case "text":
|
|
73
|
+
return typeof block.text === "string";
|
|
74
|
+
case "image":
|
|
75
|
+
case "audio":
|
|
76
|
+
return typeof block.data === "string" && typeof block.mimeType === "string";
|
|
77
|
+
case "resource_link":
|
|
78
|
+
return typeof block.uri === "string" && typeof block.name === "string";
|
|
79
|
+
case "resource":
|
|
80
|
+
return typeof block.resource === "object" && block.resource !== null;
|
|
81
|
+
default:
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function sessionNotification(value: unknown): boolean {
|
|
87
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
88
|
+
const notification = value as Record<string, unknown>;
|
|
89
|
+
if (
|
|
90
|
+
typeof notification.sessionId !== "string" ||
|
|
91
|
+
typeof notification.update !== "object" ||
|
|
92
|
+
notification.update === null
|
|
93
|
+
)
|
|
94
|
+
return false;
|
|
95
|
+
const update = notification.update as Record<string, unknown>;
|
|
96
|
+
if (typeof update.sessionUpdate !== "string") return false;
|
|
97
|
+
switch (update.sessionUpdate) {
|
|
98
|
+
case "agent_message_chunk":
|
|
99
|
+
case "agent_thought_chunk":
|
|
100
|
+
case "user_message_chunk":
|
|
101
|
+
return contentBlock(update.content);
|
|
102
|
+
case "tool_call":
|
|
103
|
+
return typeof update.toolCallId === "string" && typeof update.title === "string";
|
|
104
|
+
case "tool_call_update":
|
|
105
|
+
return typeof update.toolCallId === "string";
|
|
106
|
+
case "plan":
|
|
107
|
+
return Array.isArray(update.entries);
|
|
108
|
+
case "current_mode_update":
|
|
109
|
+
return typeof update.currentModeId === "string";
|
|
110
|
+
case "available_commands_update":
|
|
111
|
+
return Array.isArray(update.availableCommands);
|
|
112
|
+
case "config_option_update":
|
|
113
|
+
return Array.isArray(update.configOptions);
|
|
114
|
+
case "session_info_update":
|
|
115
|
+
return update.title === undefined || update.title === null || typeof update.title === "string";
|
|
116
|
+
case "usage_update":
|
|
117
|
+
return typeof update.size === "number" && typeof update.used === "number";
|
|
118
|
+
default:
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Validator for new-session responses. */
|
|
124
|
+
export const zNewSessionResponse = validator<NewSessionResponse>(
|
|
125
|
+
value => sessionResponse(value, true),
|
|
126
|
+
"new session response",
|
|
127
|
+
);
|
|
128
|
+
/** Validator for load-session responses. */
|
|
129
|
+
export const zLoadSessionResponse = validator<LoadSessionResponse>(
|
|
130
|
+
value => sessionResponse(value, false),
|
|
131
|
+
"load session response",
|
|
132
|
+
);
|
|
133
|
+
/** Validator for fork-session responses. */
|
|
134
|
+
export const zForkSessionResponse = validator<ForkSessionResponse>(
|
|
135
|
+
value => sessionResponse(value, true),
|
|
136
|
+
"fork session response",
|
|
137
|
+
);
|
|
138
|
+
/** Validator for prompt responses. */
|
|
139
|
+
export const zPromptResponse = validator<PromptResponse>(value => {
|
|
140
|
+
if (typeof value !== "object" || value === null) return false;
|
|
141
|
+
const stopReason = (value as Record<string, unknown>).stopReason;
|
|
142
|
+
return (
|
|
143
|
+
stopReason === "end_turn" ||
|
|
144
|
+
stopReason === "max_tokens" ||
|
|
145
|
+
stopReason === "max_turn_requests" ||
|
|
146
|
+
stopReason === "refusal" ||
|
|
147
|
+
stopReason === "cancelled"
|
|
148
|
+
);
|
|
149
|
+
}, "prompt response");
|
|
150
|
+
/** Validator for session notifications. */
|
|
151
|
+
export const zSessionNotification = validator<SessionNotification>(sessionNotification, "session notification");
|
|
152
|
+
|
|
153
|
+
/** ACP runtime validators. */
|
|
154
|
+
export const schema = {
|
|
155
|
+
zNewSessionResponse,
|
|
156
|
+
zLoadSessionResponse,
|
|
157
|
+
zForkSessionResponse,
|
|
158
|
+
zPromptResponse,
|
|
159
|
+
zSessionNotification,
|
|
160
|
+
} as const;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { AnyMessage } from "./transport";
|
|
2
|
+
|
|
3
|
+
/** Bidirectional JSON-RPC message transport. */
|
|
4
|
+
export interface Stream {
|
|
5
|
+
writable: WritableStream<AnyMessage>;
|
|
6
|
+
readable: ReadableStream<AnyMessage>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Converts byte-oriented newline-delimited JSON streams to an ACP message transport. */
|
|
10
|
+
export function ndJsonStream(output: WritableStream<Uint8Array>, input: ReadableStream<Uint8Array>): Stream {
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
const decoder = new TextDecoder();
|
|
13
|
+
const writable = new WritableStream<AnyMessage>({
|
|
14
|
+
async write(message) {
|
|
15
|
+
const writer = output.getWriter();
|
|
16
|
+
try {
|
|
17
|
+
await writer.write(encoder.encode(`${JSON.stringify(message)}\n`));
|
|
18
|
+
} finally {
|
|
19
|
+
writer.releaseLock();
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
async close() {
|
|
23
|
+
const writer = output.getWriter();
|
|
24
|
+
try {
|
|
25
|
+
await writer.close();
|
|
26
|
+
} finally {
|
|
27
|
+
writer.releaseLock();
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
async abort(reason) {
|
|
31
|
+
const writer = output.getWriter();
|
|
32
|
+
try {
|
|
33
|
+
await writer.abort(reason);
|
|
34
|
+
} finally {
|
|
35
|
+
writer.releaseLock();
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
let buffered = "";
|
|
40
|
+
const readable = new ReadableStream<AnyMessage>({
|
|
41
|
+
async start(controller) {
|
|
42
|
+
const reader = input.getReader();
|
|
43
|
+
try {
|
|
44
|
+
while (true) {
|
|
45
|
+
const next = await reader.read();
|
|
46
|
+
if (next.done) break;
|
|
47
|
+
buffered += decoder.decode(next.value, { stream: true });
|
|
48
|
+
let newline = buffered.indexOf("\n");
|
|
49
|
+
while (newline >= 0) {
|
|
50
|
+
const line = buffered.slice(0, newline).trimEnd();
|
|
51
|
+
buffered = buffered.slice(newline + 1);
|
|
52
|
+
if (line.length > 0) controller.enqueue(parseMessage(line));
|
|
53
|
+
newline = buffered.indexOf("\n");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
buffered += decoder.decode();
|
|
57
|
+
const finalLine = buffered.trim();
|
|
58
|
+
if (finalLine.length > 0) controller.enqueue(parseMessage(finalLine));
|
|
59
|
+
controller.close();
|
|
60
|
+
} catch (error) {
|
|
61
|
+
controller.error(error);
|
|
62
|
+
} finally {
|
|
63
|
+
reader.releaseLock();
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
return { writable, readable };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseMessage(line: string): AnyMessage {
|
|
71
|
+
const value: unknown = JSON.parse(line);
|
|
72
|
+
if (
|
|
73
|
+
typeof value !== "object" ||
|
|
74
|
+
value === null ||
|
|
75
|
+
Array.isArray(value) ||
|
|
76
|
+
!("jsonrpc" in value) ||
|
|
77
|
+
value.jsonrpc !== "2.0"
|
|
78
|
+
) {
|
|
79
|
+
throw new Error("Invalid JSON-RPC message");
|
|
80
|
+
}
|
|
81
|
+
return value as AnyMessage;
|
|
82
|
+
}
|