@adroit-app/chat-core 0.0.0 → 0.1.0
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/LICENSE +21 -0
- package/README.md +151 -2
- package/dist/acp.d.ts +22 -0
- package/dist/acp.js +1 -0
- package/dist/asks.d.ts +38 -0
- package/dist/asks.js +147 -0
- package/dist/completion.d.ts +84 -0
- package/dist/completion.js +154 -0
- package/dist/config.d.ts +18 -0
- package/dist/config.js +36 -0
- package/dist/connection.d.ts +85 -0
- package/dist/connection.js +1 -0
- package/dist/controller.d.ts +59 -0
- package/dist/controller.js +235 -0
- package/dist/diff.d.ts +6 -0
- package/dist/diff.js +137 -0
- package/dist/images.d.ts +48 -0
- package/dist/images.js +45 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/input.d.ts +42 -0
- package/dist/input.js +37 -0
- package/dist/layout.d.ts +25 -0
- package/dist/layout.js +48 -0
- package/dist/locations.d.ts +33 -0
- package/dist/locations.js +116 -0
- package/dist/questions.d.ts +28 -0
- package/dist/questions.js +67 -0
- package/dist/reduce.d.ts +67 -0
- package/dist/reduce.js +559 -0
- package/dist/state.d.ts +200 -0
- package/dist/state.js +25 -0
- package/dist/text.d.ts +64 -0
- package/dist/text.js +190 -0
- package/dist/timing.d.ts +18 -0
- package/dist/timing.js +43 -0
- package/dist/title.d.ts +10 -0
- package/dist/title.js +15 -0
- package/dist/v1.d.ts +60 -0
- package/dist/v1.js +311 -0
- package/package.json +18 -3
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import type { AvailableCommand, BackgroundWork, ContentBlock, Cost, CreateElicitationRequest, CreateElicitationResponse, NoticeSeverity, PlanUpdateContent, RequestPermissionOutcome, RequestPermissionRequest, SessionConfigOption, TerminalExitStatus, ToolCallContent, ToolCallLocation, ToolCallStatus, ToolKind, Usage } from "./acp.ts";
|
|
2
|
+
/** A message, by the `messageId` the agent gave it, and what it holds so far. */
|
|
3
|
+
export interface MessageEntry {
|
|
4
|
+
kind: "user" | "agent" | "thought";
|
|
5
|
+
id: string;
|
|
6
|
+
content: ContentBlock[];
|
|
7
|
+
/** How many tokens a thought ran to, when the agent counts it without saying it (acp-adapter). */
|
|
8
|
+
thinkingTokens?: number;
|
|
9
|
+
/** A thought known only by its count: nothing of it to read. */
|
|
10
|
+
tally?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** A message sent between sessions, as a subagent's task from its parent. */
|
|
13
|
+
export interface SessionMessageEntry {
|
|
14
|
+
kind: "session-message";
|
|
15
|
+
id: string;
|
|
16
|
+
senderSessionId: string | null;
|
|
17
|
+
recipientSessionId: string | null;
|
|
18
|
+
content: ContentBlock[];
|
|
19
|
+
}
|
|
20
|
+
export interface ToolEntry {
|
|
21
|
+
kind: "tool";
|
|
22
|
+
/** The ACP tool call id. */
|
|
23
|
+
id: string;
|
|
24
|
+
title: string;
|
|
25
|
+
/** The agent's own name for the tool, when it says. */
|
|
26
|
+
name: string | null;
|
|
27
|
+
toolKind: ToolKind;
|
|
28
|
+
status: ToolCallStatus;
|
|
29
|
+
content: ToolCallContent[];
|
|
30
|
+
locations: ToolCallLocation[];
|
|
31
|
+
rawInput: unknown;
|
|
32
|
+
rawOutput: unknown;
|
|
33
|
+
/**
|
|
34
|
+
* When the call started and ended (ms since the epoch): the agent's own
|
|
35
|
+
* times when it gave them, otherwise when this Chat saw it start and end.
|
|
36
|
+
* None for a call first seen already finished, as in a replay.
|
|
37
|
+
*/
|
|
38
|
+
times?: {
|
|
39
|
+
startedAt: number | null;
|
|
40
|
+
endedAt: number | null;
|
|
41
|
+
agent: boolean;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** How a stretch of foreground work went. */
|
|
45
|
+
export type TurnOutcome = "running" | "ended" | "cancelled" | "failed";
|
|
46
|
+
/** A stretch of foreground work, from the agent reporting it active to reporting it idle. */
|
|
47
|
+
export interface TurnEntry {
|
|
48
|
+
kind: "turn";
|
|
49
|
+
id: string;
|
|
50
|
+
outcome: TurnOutcome;
|
|
51
|
+
/** The stop reason the agent gave, as it gave it. */
|
|
52
|
+
stopReason: string | null;
|
|
53
|
+
usage: Usage | null;
|
|
54
|
+
error: string | null;
|
|
55
|
+
/** Tokens the agent thought in this turn without saying what, when it counted them. */
|
|
56
|
+
thinkingTokens: number | null;
|
|
57
|
+
}
|
|
58
|
+
export type AskRequest = {
|
|
59
|
+
type: "permission";
|
|
60
|
+
request: RequestPermissionRequest;
|
|
61
|
+
} | {
|
|
62
|
+
type: "elicitation";
|
|
63
|
+
request: CreateElicitationRequest;
|
|
64
|
+
};
|
|
65
|
+
export type AskAnswer = {
|
|
66
|
+
type: "permission";
|
|
67
|
+
outcome: RequestPermissionOutcome;
|
|
68
|
+
} | {
|
|
69
|
+
type: "elicitation";
|
|
70
|
+
response: CreateElicitationResponse;
|
|
71
|
+
};
|
|
72
|
+
export interface AskEntry {
|
|
73
|
+
kind: "ask";
|
|
74
|
+
id: string;
|
|
75
|
+
ask: AskRequest;
|
|
76
|
+
/** How it was answered; none while it waits. */
|
|
77
|
+
answer: AskAnswer | null;
|
|
78
|
+
}
|
|
79
|
+
export interface NoticeEntry {
|
|
80
|
+
kind: "notice";
|
|
81
|
+
id: string;
|
|
82
|
+
severity: NoticeSeverity;
|
|
83
|
+
title: string;
|
|
84
|
+
description: string | null;
|
|
85
|
+
}
|
|
86
|
+
export interface CompactionEntry {
|
|
87
|
+
kind: "compaction";
|
|
88
|
+
id: string;
|
|
89
|
+
status: string | null;
|
|
90
|
+
summary: ContentBlock[];
|
|
91
|
+
error: string | null;
|
|
92
|
+
}
|
|
93
|
+
/** Where a subagent was announced; what it did is in `ChatState.subagents`. */
|
|
94
|
+
export interface SubagentEntry {
|
|
95
|
+
kind: "subagent";
|
|
96
|
+
id: string;
|
|
97
|
+
}
|
|
98
|
+
export type Entry = MessageEntry | SessionMessageEntry | ToolEntry | TurnEntry | AskEntry | NoticeEntry | CompactionEntry | SubagentEntry;
|
|
99
|
+
/** A display-only terminal the agent owns, which tool calls refer to. */
|
|
100
|
+
export interface TerminalView {
|
|
101
|
+
id: string;
|
|
102
|
+
command: string | null;
|
|
103
|
+
output: string;
|
|
104
|
+
exitStatus: TerminalExitStatus | null;
|
|
105
|
+
}
|
|
106
|
+
/** What a session does: the foreground state, and its transcript. */
|
|
107
|
+
export interface Transcript {
|
|
108
|
+
entries: Entry[];
|
|
109
|
+
terminals: Record<string, TerminalView>;
|
|
110
|
+
/** `idle`, `running`, `requires_action`, `unknown`, or what the agent said. */
|
|
111
|
+
activity: string;
|
|
112
|
+
}
|
|
113
|
+
/** A session this one created and owns, and what it did. */
|
|
114
|
+
export interface Subagent extends Transcript {
|
|
115
|
+
sessionId: string;
|
|
116
|
+
title: string | null;
|
|
117
|
+
description: string | null;
|
|
118
|
+
}
|
|
119
|
+
/** A prompt the user sent, and the key every attempt to send it carries. */
|
|
120
|
+
export interface Submission {
|
|
121
|
+
id: string;
|
|
122
|
+
prompt: ContentBlock[];
|
|
123
|
+
idempotencyKey: string;
|
|
124
|
+
/** Why the last attempt to send it did not reach the transcript. */
|
|
125
|
+
error: string | null;
|
|
126
|
+
/**
|
|
127
|
+
* Sent while nothing ran, waited on the user, or waited to be taken in:
|
|
128
|
+
* the agent takes it next, so it reads as the user's message on its way,
|
|
129
|
+
* not as queued. Fixed when sent, so the turn it starts does not queue it.
|
|
130
|
+
*/
|
|
131
|
+
sending: boolean;
|
|
132
|
+
}
|
|
133
|
+
/** JSON-RPC's `auth_required`: the agent needs the user to sign in. */
|
|
134
|
+
export declare const AUTH_REQUIRED = -32000;
|
|
135
|
+
/** A way to sign in the agent offers, as ACP's `authMethods` lists it. */
|
|
136
|
+
export interface SignInMethod {
|
|
137
|
+
id: string;
|
|
138
|
+
name: string;
|
|
139
|
+
description?: string | null;
|
|
140
|
+
/** `terminal` runs the agent with `args` for the user; `agent` (or none) is the agent's own. */
|
|
141
|
+
type?: string;
|
|
142
|
+
args?: string[];
|
|
143
|
+
env?: Record<string, string>;
|
|
144
|
+
}
|
|
145
|
+
/** A prompt to send again as it was, under its own key. */
|
|
146
|
+
export interface Resend {
|
|
147
|
+
prompt: ContentBlock[];
|
|
148
|
+
idempotencyKey: string;
|
|
149
|
+
}
|
|
150
|
+
/** The agent waits for the user to sign in. */
|
|
151
|
+
export interface SignIn {
|
|
152
|
+
/** Not signed in when the session opened, or signed out since. */
|
|
153
|
+
reason: "not_signed_in" | "signed_out";
|
|
154
|
+
/** The prompt that failed for want of sign-in, to send again once signed in. */
|
|
155
|
+
retry: Resend | null;
|
|
156
|
+
/** Why the last attempt to sign in failed. */
|
|
157
|
+
error: string | null;
|
|
158
|
+
}
|
|
159
|
+
/** Who the agent is signed in as, as it last reported. */
|
|
160
|
+
export interface Account {
|
|
161
|
+
signedIn: boolean;
|
|
162
|
+
label: string | null;
|
|
163
|
+
}
|
|
164
|
+
export interface ContextUsage {
|
|
165
|
+
used: number;
|
|
166
|
+
size: number;
|
|
167
|
+
cost: Cost | null;
|
|
168
|
+
}
|
|
169
|
+
export interface ChatState extends Transcript {
|
|
170
|
+
sessionId: string;
|
|
171
|
+
subagents: Record<string, Subagent>;
|
|
172
|
+
/** Plans by id, in the order they were first reported. */
|
|
173
|
+
plans: Record<string, PlanUpdateContent>;
|
|
174
|
+
/** Work running apart from the foreground, as last reported; none when not reported. */
|
|
175
|
+
background: BackgroundWork[] | null;
|
|
176
|
+
context: ContextUsage | null;
|
|
177
|
+
commands: AvailableCommand[];
|
|
178
|
+
/** The agent has sent a list of its commands, if an empty one. */
|
|
179
|
+
commandsReported: boolean;
|
|
180
|
+
/** The session's settings as the agent reports them (ACP's config options), as last reported. */
|
|
181
|
+
configOptions: SessionConfigOption[];
|
|
182
|
+
title: string | null;
|
|
183
|
+
/**
|
|
184
|
+
* Prompts sent and not yet inserted: the agent holds them while it works,
|
|
185
|
+
* or the request failed. Mirrors the agent's queue; it is not one.
|
|
186
|
+
*/
|
|
187
|
+
pending: Submission[];
|
|
188
|
+
/** A cancel was asked for the running work and the agent has not reported it idle. */
|
|
189
|
+
cancelling: boolean;
|
|
190
|
+
/** The agent waits for the user to sign in; none while it does not. */
|
|
191
|
+
signIn: SignIn | null;
|
|
192
|
+
/** A prompt offered to send again now that the user signed in. */
|
|
193
|
+
retry: Resend | null;
|
|
194
|
+
account: Account | null;
|
|
195
|
+
/** The last prompt the agent took in, which a sign-in may need to send again. */
|
|
196
|
+
lastSubmission: Resend | null;
|
|
197
|
+
/** The next number this state hands out for an id of its own. */
|
|
198
|
+
serial: number;
|
|
199
|
+
}
|
|
200
|
+
export declare function initialState(sessionId: string): ChatState;
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** JSON-RPC's `auth_required`: the agent needs the user to sign in. */
|
|
2
|
+
export const AUTH_REQUIRED = -32000;
|
|
3
|
+
export function initialState(sessionId) {
|
|
4
|
+
return {
|
|
5
|
+
sessionId,
|
|
6
|
+
entries: [],
|
|
7
|
+
terminals: {},
|
|
8
|
+
activity: "idle",
|
|
9
|
+
subagents: {},
|
|
10
|
+
plans: {},
|
|
11
|
+
background: null,
|
|
12
|
+
context: null,
|
|
13
|
+
commands: [],
|
|
14
|
+
commandsReported: false,
|
|
15
|
+
configOptions: [],
|
|
16
|
+
title: null,
|
|
17
|
+
pending: [],
|
|
18
|
+
cancelling: false,
|
|
19
|
+
signIn: null,
|
|
20
|
+
retry: null,
|
|
21
|
+
account: null,
|
|
22
|
+
lastSubmission: null,
|
|
23
|
+
serial: 0,
|
|
24
|
+
};
|
|
25
|
+
}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ContentBlock, Cost } from "./acp.ts";
|
|
2
|
+
import type { ContextUsage, ToolEntry } from "./state.ts";
|
|
3
|
+
/** What a message holds besides its text. */
|
|
4
|
+
export type Attachment = {
|
|
5
|
+
type: "image";
|
|
6
|
+
mimeType: string;
|
|
7
|
+
data: string;
|
|
8
|
+
} | {
|
|
9
|
+
type: "link";
|
|
10
|
+
name: string;
|
|
11
|
+
uri: string;
|
|
12
|
+
};
|
|
13
|
+
/** The text of `blocks`, joined as they were sent. */
|
|
14
|
+
export declare function textOf(blocks: readonly ContentBlock[]): string;
|
|
15
|
+
/** What `blocks` carry besides their text. */
|
|
16
|
+
export declare function attachmentsOf(blocks: readonly ContentBlock[]): Attachment[];
|
|
17
|
+
/** A block of code and the language it is coloured as. */
|
|
18
|
+
export interface CodeBlock {
|
|
19
|
+
as: "code";
|
|
20
|
+
language: string;
|
|
21
|
+
text: string;
|
|
22
|
+
}
|
|
23
|
+
/** The first line of `text` that holds anything, for a row too narrow for the rest. */
|
|
24
|
+
export declare function firstLine(text: string): string;
|
|
25
|
+
/** Whether `text` is too long to show whole at first: more than a few lines, or a long paragraph. */
|
|
26
|
+
export declare function isLong(text: string): boolean;
|
|
27
|
+
/** `text` cut to `chars` characters, marked when it was cut. */
|
|
28
|
+
export declare function cut(text: string, chars: number): string;
|
|
29
|
+
/** Each field of `value` but those `skipped`, as its name and its value cut short, on one line. */
|
|
30
|
+
export declare function briefly(value: unknown, skipped?: readonly string[]): string;
|
|
31
|
+
/** A count of tokens as it is read at a glance. */
|
|
32
|
+
export declare function tokens(n: number): string;
|
|
33
|
+
export declare function money(cost: Cost): string;
|
|
34
|
+
/** How much of the context window is used, as a percentage; nothing when its size is unknown. */
|
|
35
|
+
export declare function contextShare(context: ContextUsage): string;
|
|
36
|
+
/**
|
|
37
|
+
* What a tool was given, as one line beside its title: what it runs or reads
|
|
38
|
+
* when its input says, or else each field briefly; nothing when the title
|
|
39
|
+
* already says as much.
|
|
40
|
+
*/
|
|
41
|
+
export declare function toolLine(tool: ToolEntry): string;
|
|
42
|
+
/** What the agent said a call is for, when it said. */
|
|
43
|
+
export declare function toolNote(tool: ToolEntry): string | null;
|
|
44
|
+
/** What a tool was given, whole: the shell command it ran, or else its input as JSON. */
|
|
45
|
+
export declare function toolInput(tool: ToolEntry): CodeBlock | null;
|
|
46
|
+
/**
|
|
47
|
+
* What a tool gave back, whole: laid out as JSON when it is a JSON object or
|
|
48
|
+
* list, and plain otherwise. Never read as Markdown, which a tool's output is
|
|
49
|
+
* not known to be, nor coloured by a language a highlighter would guess.
|
|
50
|
+
*/
|
|
51
|
+
export declare function toolOutput(tool: ToolEntry): CodeBlock | null;
|
|
52
|
+
/** A change a tool made to files: what it touched, and its patch when the agent sent one. */
|
|
53
|
+
export interface DiffView {
|
|
54
|
+
changes: Array<{
|
|
55
|
+
operation: string;
|
|
56
|
+
path: string;
|
|
57
|
+
oldPath: string | null;
|
|
58
|
+
}>;
|
|
59
|
+
patch: string | null;
|
|
60
|
+
}
|
|
61
|
+
/** The changes `tool` reports making. */
|
|
62
|
+
export declare function toolDiffs(tool: ToolEntry): DiffView[];
|
|
63
|
+
/** The terminals `tool` shows. */
|
|
64
|
+
export declare function toolTerminals(tool: ToolEntry): string[];
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/** The text of `blocks`, joined as they were sent. */
|
|
2
|
+
export function textOf(blocks) {
|
|
3
|
+
return blocks.map((b) => (b.type === "text" ? b.text : "")).join("");
|
|
4
|
+
}
|
|
5
|
+
/** What `blocks` carry besides their text. */
|
|
6
|
+
export function attachmentsOf(blocks) {
|
|
7
|
+
return blocks.flatMap((b) => {
|
|
8
|
+
switch (b.type) {
|
|
9
|
+
case "image": {
|
|
10
|
+
const image = b;
|
|
11
|
+
return [{ type: "image", mimeType: image.mimeType, data: image.data }];
|
|
12
|
+
}
|
|
13
|
+
case "resource_link": {
|
|
14
|
+
const link = b;
|
|
15
|
+
return [{ type: "link", name: link.title ?? link.name, uri: link.uri }];
|
|
16
|
+
}
|
|
17
|
+
case "resource": {
|
|
18
|
+
const { uri } = b.resource;
|
|
19
|
+
return [{ type: "link", name: uri.split("/").pop() || uri, uri }];
|
|
20
|
+
}
|
|
21
|
+
default:
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** The first line of `text` that holds anything, for a row too narrow for the rest. */
|
|
27
|
+
export function firstLine(text) {
|
|
28
|
+
return (text
|
|
29
|
+
.split("\n")
|
|
30
|
+
.map((l) => l.trim())
|
|
31
|
+
.find((l) => l !== "") ?? "");
|
|
32
|
+
}
|
|
33
|
+
/** Whether `text` is too long to show whole at first: more than a few lines, or a long paragraph. */
|
|
34
|
+
export function isLong(text) {
|
|
35
|
+
const trimmed = text.trimEnd();
|
|
36
|
+
return trimmed.split("\n").length > 6 || [...trimmed].length > 600;
|
|
37
|
+
}
|
|
38
|
+
/** `text` cut to `chars` characters, marked when it was cut. */
|
|
39
|
+
export function cut(text, chars) {
|
|
40
|
+
const all = [...text];
|
|
41
|
+
return all.length > chars ? `${all.slice(0, chars).join("")}…` : text;
|
|
42
|
+
}
|
|
43
|
+
/** Each field of `value` but those `skipped`, as its name and its value cut short, on one line. */
|
|
44
|
+
export function briefly(value, skipped = []) {
|
|
45
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
46
|
+
return "";
|
|
47
|
+
return Object.entries(value)
|
|
48
|
+
.filter(([name]) => !skipped.includes(name))
|
|
49
|
+
.map(([name, v]) => {
|
|
50
|
+
const shown = typeof v === "string"
|
|
51
|
+
? cut(v, 40)
|
|
52
|
+
: Array.isArray(v)
|
|
53
|
+
? `[${v.length}]`
|
|
54
|
+
: v !== null && typeof v === "object"
|
|
55
|
+
? "{…}"
|
|
56
|
+
: String(v);
|
|
57
|
+
return `${name.replaceAll("_", " ")} ${shown}`;
|
|
58
|
+
})
|
|
59
|
+
.join(", ");
|
|
60
|
+
}
|
|
61
|
+
/** A count of tokens as it is read at a glance. */
|
|
62
|
+
export function tokens(n) {
|
|
63
|
+
if (n >= 1_000_000)
|
|
64
|
+
return `${(n / 1_000_000).toFixed(1)}M`;
|
|
65
|
+
if (n >= 1_000)
|
|
66
|
+
return `${(n / 1_000).toFixed(1)}k`;
|
|
67
|
+
return String(n);
|
|
68
|
+
}
|
|
69
|
+
export function money(cost) {
|
|
70
|
+
return cost.currency === "USD"
|
|
71
|
+
? `$${cost.amount.toFixed(2)}`
|
|
72
|
+
: `${cost.amount.toFixed(2)} ${cost.currency}`;
|
|
73
|
+
}
|
|
74
|
+
/** How much of the context window is used, as a percentage; nothing when its size is unknown. */
|
|
75
|
+
export function contextShare(context) {
|
|
76
|
+
if (context.size <= 0)
|
|
77
|
+
return "";
|
|
78
|
+
return `${Math.round((context.used / context.size) * 100)}%`;
|
|
79
|
+
}
|
|
80
|
+
/** The fields of a tool's input that say what it runs or reads. */
|
|
81
|
+
const NAMED = ["command", "cmd", "file_path", "path", "pattern", "url", "query"];
|
|
82
|
+
function commandOf(input) {
|
|
83
|
+
if (input === null || typeof input !== "object")
|
|
84
|
+
return null;
|
|
85
|
+
const record = input;
|
|
86
|
+
const command = record.command ?? record.cmd;
|
|
87
|
+
if (typeof command === "string")
|
|
88
|
+
return command;
|
|
89
|
+
if (Array.isArray(command) && command.every((c) => typeof c === "string")) {
|
|
90
|
+
return command.join(" ");
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* What a tool was given, as one line beside its title: what it runs or reads
|
|
96
|
+
* when its input says, or else each field briefly; nothing when the title
|
|
97
|
+
* already says as much.
|
|
98
|
+
*/
|
|
99
|
+
export function toolLine(tool) {
|
|
100
|
+
const input = tool.rawInput;
|
|
101
|
+
let line = commandOf(input);
|
|
102
|
+
if (line === null && input !== null && typeof input === "object") {
|
|
103
|
+
const record = input;
|
|
104
|
+
const named = NAMED.map((n) => record[n]).find((v) => typeof v === "string");
|
|
105
|
+
line = typeof named === "string" ? named : briefly(input, ["description"]);
|
|
106
|
+
}
|
|
107
|
+
if (!line) {
|
|
108
|
+
const at = tool.locations[0];
|
|
109
|
+
line = at ? (at.line != null ? `${at.path}:${at.line}` : at.path) : "";
|
|
110
|
+
}
|
|
111
|
+
if (line && tool.title.includes(line))
|
|
112
|
+
return "";
|
|
113
|
+
return cut(line, 120);
|
|
114
|
+
}
|
|
115
|
+
/** What the agent said a call is for, when it said. */
|
|
116
|
+
export function toolNote(tool) {
|
|
117
|
+
const input = tool.rawInput;
|
|
118
|
+
if (input === null || typeof input !== "object")
|
|
119
|
+
return null;
|
|
120
|
+
const note = input.description;
|
|
121
|
+
return typeof note === "string" && note.trim() !== "" ? note.trim() : null;
|
|
122
|
+
}
|
|
123
|
+
/** What a tool was given, whole: the shell command it ran, or else its input as JSON. */
|
|
124
|
+
export function toolInput(tool) {
|
|
125
|
+
const command = commandOf(tool.rawInput);
|
|
126
|
+
if (command !== null) {
|
|
127
|
+
return { as: "code", language: "sh", text: command };
|
|
128
|
+
}
|
|
129
|
+
const input = tool.rawInput;
|
|
130
|
+
if (input === undefined || input === null)
|
|
131
|
+
return null;
|
|
132
|
+
if (typeof input === "object" && Object.keys(input).length === 0)
|
|
133
|
+
return null;
|
|
134
|
+
if (typeof input === "string")
|
|
135
|
+
return { as: "code", language: "plaintext", text: input };
|
|
136
|
+
return { as: "code", language: "json", text: JSON.stringify(input, null, 2) };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* What a tool gave back, whole: laid out as JSON when it is a JSON object or
|
|
140
|
+
* list, and plain otherwise. Never read as Markdown, which a tool's output is
|
|
141
|
+
* not known to be, nor coloured by a language a highlighter would guess.
|
|
142
|
+
*/
|
|
143
|
+
export function toolOutput(tool) {
|
|
144
|
+
const texts = tool.content.flatMap((c) => c.type === "content" ? [textOf([c.content])] : []);
|
|
145
|
+
const raw = tool.rawOutput;
|
|
146
|
+
const text = texts.length > 0
|
|
147
|
+
? texts.join("\n")
|
|
148
|
+
: typeof raw === "string"
|
|
149
|
+
? raw
|
|
150
|
+
: raw === undefined || raw === null
|
|
151
|
+
? ""
|
|
152
|
+
: JSON.stringify(raw);
|
|
153
|
+
if (text.trim() === "")
|
|
154
|
+
return null;
|
|
155
|
+
try {
|
|
156
|
+
const value = JSON.parse(text.trim());
|
|
157
|
+
if (value !== null && typeof value === "object") {
|
|
158
|
+
return { as: "code", language: "json", text: JSON.stringify(value, null, 2) };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// Not JSON: shown as the tool gave it.
|
|
163
|
+
}
|
|
164
|
+
return { as: "code", language: "plaintext", text };
|
|
165
|
+
}
|
|
166
|
+
/** The changes `tool` reports making. */
|
|
167
|
+
export function toolDiffs(tool) {
|
|
168
|
+
return tool.content.flatMap((c) => {
|
|
169
|
+
if (c.type !== "diff")
|
|
170
|
+
return [];
|
|
171
|
+
const diff = c;
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
changes: diff.changes.map((change) => {
|
|
175
|
+
const named = change;
|
|
176
|
+
return {
|
|
177
|
+
operation: named.operation,
|
|
178
|
+
path: named.path ?? "",
|
|
179
|
+
oldPath: named.oldPath ?? null,
|
|
180
|
+
};
|
|
181
|
+
}),
|
|
182
|
+
patch: diff.patch?.text ?? null,
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/** The terminals `tool` shows. */
|
|
188
|
+
export function toolTerminals(tool) {
|
|
189
|
+
return tool.content.flatMap((c) => c.type === "terminal" ? [c.terminalId] : []);
|
|
190
|
+
}
|
package/dist/timing.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ToolEntry } from "./state.ts";
|
|
2
|
+
/** A duration in the fewest characters that still read: `0.4s`, `12s`, `1m 05s`, `1h 02m`. */
|
|
3
|
+
export declare function formatDuration(ms: number): string;
|
|
4
|
+
/**
|
|
5
|
+
* How long `tool` took, or has run so far at `now` (ms since the epoch):
|
|
6
|
+
* by the agent's own times when it gave them, otherwise as measured here.
|
|
7
|
+
* Unknown for a call this Chat first saw already finished.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toolDuration(tool: ToolEntry, now: number): number | null;
|
|
10
|
+
/**
|
|
11
|
+
* What a tool's row says after its title: what the agent said the call is
|
|
12
|
+
* for, as text, when it adds to the title; else the call's line, as code,
|
|
13
|
+
* when the title does not already say it; else nothing.
|
|
14
|
+
*/
|
|
15
|
+
export declare function toolDetail(tool: ToolEntry): {
|
|
16
|
+
text: string;
|
|
17
|
+
code: boolean;
|
|
18
|
+
} | null;
|
package/dist/timing.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { toolLine, toolNote } from "./text.js";
|
|
2
|
+
/** A duration in the fewest characters that still read: `0.4s`, `12s`, `1m 05s`, `1h 02m`. */
|
|
3
|
+
export function formatDuration(ms) {
|
|
4
|
+
const seconds = Math.max(0, ms) / 1000;
|
|
5
|
+
if (seconds < 10)
|
|
6
|
+
return `${(Math.floor(seconds * 10) / 10).toFixed(1)}s`;
|
|
7
|
+
if (seconds < 60)
|
|
8
|
+
return `${Math.floor(seconds)}s`;
|
|
9
|
+
const minutes = Math.floor(seconds / 60);
|
|
10
|
+
if (minutes < 60)
|
|
11
|
+
return `${minutes}m ${String(Math.floor(seconds % 60)).padStart(2, "0")}s`;
|
|
12
|
+
return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, "0")}m`;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* How long `tool` took, or has run so far at `now` (ms since the epoch):
|
|
16
|
+
* by the agent's own times when it gave them, otherwise as measured here.
|
|
17
|
+
* Unknown for a call this Chat first saw already finished.
|
|
18
|
+
*/
|
|
19
|
+
export function toolDuration(tool, now) {
|
|
20
|
+
const times = tool.times;
|
|
21
|
+
if (!times)
|
|
22
|
+
return null;
|
|
23
|
+
if (times.startedAt === null)
|
|
24
|
+
return null;
|
|
25
|
+
return (times.endedAt ?? now) - times.startedAt;
|
|
26
|
+
}
|
|
27
|
+
const same = (text) => text
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.replace(/[`\s]+/g, " ")
|
|
30
|
+
.trim();
|
|
31
|
+
/**
|
|
32
|
+
* What a tool's row says after its title: what the agent said the call is
|
|
33
|
+
* for, as text, when it adds to the title; else the call's line, as code,
|
|
34
|
+
* when the title does not already say it; else nothing.
|
|
35
|
+
*/
|
|
36
|
+
export function toolDetail(tool) {
|
|
37
|
+
const title = same(tool.title);
|
|
38
|
+
const note = toolNote(tool);
|
|
39
|
+
if (note)
|
|
40
|
+
return title.includes(same(note)) ? null : { text: note, code: false };
|
|
41
|
+
const line = toolLine(tool);
|
|
42
|
+
return line && !title.includes(same(line)) ? { text: line, code: true } : null;
|
|
43
|
+
}
|
package/dist/title.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Entry } from "./state.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The session's title as the Chat shows it: the name the user gave it here,
|
|
4
|
+
* else the agent's title (`session_info_update`), else the first line of
|
|
5
|
+
* the first prompt, else "New session".
|
|
6
|
+
*/
|
|
7
|
+
export declare function displayTitle(state: {
|
|
8
|
+
title: string | null;
|
|
9
|
+
entries: readonly Pick<Entry, "kind">[];
|
|
10
|
+
}, local: string | null): string;
|
package/dist/title.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { firstLine, textOf } from "./text.js";
|
|
2
|
+
/**
|
|
3
|
+
* The session's title as the Chat shows it: the name the user gave it here,
|
|
4
|
+
* else the agent's title (`session_info_update`), else the first line of
|
|
5
|
+
* the first prompt, else "New session".
|
|
6
|
+
*/
|
|
7
|
+
export function displayTitle(state, local) {
|
|
8
|
+
if (local?.trim())
|
|
9
|
+
return local.trim();
|
|
10
|
+
if (state.title?.trim())
|
|
11
|
+
return state.title.trim();
|
|
12
|
+
const first = state.entries.find((e) => e.kind === "user");
|
|
13
|
+
const line = first ? firstLine(textOf(first.content)).trim() : "";
|
|
14
|
+
return line || "New session";
|
|
15
|
+
}
|
package/dist/v1.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { V1 } from "./acp.ts";
|
|
2
|
+
import type { AuthHandle, AuthStatus, ChatConnection, PromptCapabilities } from "./connection.ts";
|
|
3
|
+
/** A held prompt as acp-adapter lists it. */
|
|
4
|
+
export interface HeldPrompt {
|
|
5
|
+
promptId: string;
|
|
6
|
+
prompt: V1.ContentBlock[];
|
|
7
|
+
}
|
|
8
|
+
/** One ACP v1 session, as a host reaches it. */
|
|
9
|
+
export interface V1Connection {
|
|
10
|
+
readonly sessionId: string;
|
|
11
|
+
/** `session/prompt`; resolves when the prompt's turn ends. */
|
|
12
|
+
prompt(request: {
|
|
13
|
+
prompt: V1.ContentBlock[];
|
|
14
|
+
_meta?: Record<string, unknown>;
|
|
15
|
+
}): Promise<V1.PromptResponse>;
|
|
16
|
+
/** `session/cancel`. */
|
|
17
|
+
cancel(): Promise<void>;
|
|
18
|
+
/** Routes the agent's calls on this session and its subagents' to `client`. */
|
|
19
|
+
connect(client: V1Client): () => void;
|
|
20
|
+
/**
|
|
21
|
+
* acp-adapter's held-prompt extension, when the agent side offers it:
|
|
22
|
+
* prompts sent during a turn are held there, and can be removed.
|
|
23
|
+
*/
|
|
24
|
+
queue?: {
|
|
25
|
+
remove(promptId: string): Promise<boolean>;
|
|
26
|
+
};
|
|
27
|
+
/** The session could not open until the user signs in. */
|
|
28
|
+
readonly signInRequired?: boolean;
|
|
29
|
+
/** ACP v1's `promptCapabilities`: what a prompt may carry besides text. */
|
|
30
|
+
readonly promptCapabilities?: PromptCapabilities;
|
|
31
|
+
/** The session's config options, as the response that opened it gave them. */
|
|
32
|
+
readonly configOptions?: V1.SessionConfigOption[] | null;
|
|
33
|
+
/** The session's modes, as the response that opened it gave them: v1's settings before config options. */
|
|
34
|
+
readonly modes?: V1.SessionModeState | null;
|
|
35
|
+
/** ACP's `authMethods` and `authenticate`, when the agent asks for sign-in. */
|
|
36
|
+
readonly auth?: AuthHandle;
|
|
37
|
+
}
|
|
38
|
+
/** The v1 client methods the Chat answers, with acp-adapter's queue notification. */
|
|
39
|
+
export interface V1Client {
|
|
40
|
+
sessionUpdate(notification: V1.SessionNotification): void;
|
|
41
|
+
requestPermission(request: V1.RequestPermissionRequest): Promise<V1.RequestPermissionResponse>;
|
|
42
|
+
createElicitation(request: V1.CreateElicitationRequest): Promise<V1.CreateElicitationResponse>;
|
|
43
|
+
/** `_acp-adapter/queue/changed`: the prompts the session holds, in order. */
|
|
44
|
+
queueChanged(prompts: HeldPrompt[]): void;
|
|
45
|
+
/** ACP's `auth/status_update`. */
|
|
46
|
+
authStatus?(status: AuthStatus): void;
|
|
47
|
+
/** The connection to the agent closed, with why when it is known. */
|
|
48
|
+
closed?(error: string | null): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* `connection` seen as an ACP v2 session, so the Chat keeps one model of a
|
|
52
|
+
* session whichever protocol the agent speaks. v1 tells foreground work by a
|
|
53
|
+
* prompt's request lasting the turn; this tells it as v2 does, by state
|
|
54
|
+
* updates, and gives messages the ids v1 leaves out.
|
|
55
|
+
*
|
|
56
|
+
* With acp-adapter's queue, prompts sent during a turn go to the agent at
|
|
57
|
+
* once and are held there. A plain v1 agent takes one prompt at a time, so
|
|
58
|
+
* they wait here until the turn ends.
|
|
59
|
+
*/
|
|
60
|
+
export declare function fromV1(connection: V1Connection): ChatConnection;
|