@adroit-app/chat-core 0.0.0-stage → 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 +25 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alisue <lambdalisue@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# adroit-chat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://crates.io/crates/adroit-chat)
|
|
4
|
+
[](https://www.npmjs.com/package/@adroit-app/chat-core)
|
|
5
|
+
[](https://www.npmjs.com/package/@adroit-app/chat-ui)
|
|
6
|
+
[](https://www.npmjs.com/package/@adroit-app/chat-tauri)
|
|
7
|
+
[](https://www.npmjs.com/package/@adroit-app/session-start)
|
|
8
|
+
|
|
9
|
+
A chat window for any coding agent that speaks the
|
|
10
|
+
[Agent Client Protocol](https://agentclientprotocol.com).
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import "@adroit-app/chat-ui";
|
|
14
|
+
import { fromV1 } from "@adroit-app/chat-core";
|
|
15
|
+
|
|
16
|
+
const chat = document.createElement("adroit-chat");
|
|
17
|
+
chat.theme = "dark";
|
|
18
|
+
chat.cwd = "/path/to/project";
|
|
19
|
+
chat.connection = fromV1(session); // or an ACP v2 ChatConnection as it is
|
|
20
|
+
document.body.append(chat);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`<adroit-chat>` shows one ACP session: the transcript of what the agent said
|
|
24
|
+
and did, the asks it waits on, the plan, the prompts it holds, and a composer
|
|
25
|
+
to prompt it. Its only input is a connection to the session. What it takes
|
|
26
|
+
and promises is in [docs/spec/adroit-chat.md](docs/spec/adroit-chat.md).
|
|
27
|
+
|
|
28
|
+
The repository is <https://github.com/adroit-app/adroit-chat>.
|
|
29
|
+
|
|
30
|
+
## The window
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
adroit-chat claude [--model <model>] [--resume <session>]
|
|
34
|
+
adroit-chat codex [--model <model>] [--resume <thread>]
|
|
35
|
+
adroit-chat --acp -- <agent that speaks ACP>…
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Adroit Chat, started as `adroit-chat`, opens a window with the Chat on one agent's session, in the
|
|
39
|
+
current folder (or `--cwd <dir>`). `claude` runs your own Claude Code and
|
|
40
|
+
`codex` your own Codex, installed and signed in, through
|
|
41
|
+
[acp-adapter](https://github.com/adroit-app/acp-adapter) inside the program;
|
|
42
|
+
`--acp` runs any agent that speaks ACP. Started with no agent, as from Finder,
|
|
43
|
+
it opens a start form to choose the agent and the folder; File › New Window
|
|
44
|
+
opens another window, each with its own session. The composer's keys can be set in a
|
|
45
|
+
settings file; where it lives and what it holds is in
|
|
46
|
+
[docs/spec/app.md](docs/spec/app.md). Settings… opens a Preferences window over that
|
|
47
|
+
file.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
The window, on macOS (Apple silicon), with its `adroit-chat` command:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
brew install --cask adroit-app/tap/adroit-chat
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Each [release](https://github.com/adroit-app/adroit-chat/releases) carries
|
|
58
|
+
the window's bundles, with a `.sha256` for each: the disk image for macOS
|
|
59
|
+
(Apple silicon), the `.deb` and `.AppImage` for Linux (x86_64, aarch64), and
|
|
60
|
+
the installer for Windows (x86_64). The app is signed by a self-signed
|
|
61
|
+
certificate and not notarized; the cask lifts the quarantine Gatekeeper
|
|
62
|
+
would refuse it under. It can also be built from this repository (see
|
|
63
|
+
Development), with `just build` for the bundled app or
|
|
64
|
+
`just run -- claude` to run it from source.
|
|
65
|
+
|
|
66
|
+
What a host embeds the Chat with is published at each release:
|
|
67
|
+
|
|
68
|
+
| Package | What it is | Published to |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| `@adroit-app/chat-core` | the view model of a session, its controller, and the ACP v1 adapter | [](https://www.npmjs.com/package/@adroit-app/chat-core) |
|
|
71
|
+
| `@adroit-app/chat-ui` | the `<adroit-chat>` element | [](https://www.npmjs.com/package/@adroit-app/chat-ui) |
|
|
72
|
+
| `@adroit-app/chat-tauri` | the Chat's connection to the `adroit-chat` Tauri plugin | [](https://www.npmjs.com/package/@adroit-app/chat-tauri) |
|
|
73
|
+
| `@adroit-app/session-start` | the `<adroit-session-start>` form a session starts from | [](https://www.npmjs.com/package/@adroit-app/session-start) |
|
|
74
|
+
| `adroit-chat` | the library and Tauri plugin another Tauri app embeds the Chat with, its page built in | [](https://crates.io/crates/adroit-chat) |
|
|
75
|
+
| `@adroit-app/fake-agent` | a scripted agent for the design and the tests | no |
|
|
76
|
+
| `@adroit-app/preferences` | the window's Preferences page | no |
|
|
77
|
+
| `adroit-chat-app` | the window | the release's assets, and the Homebrew cask |
|
|
78
|
+
|
|
79
|
+
The npm packages are JavaScript with declarations, and keep the Vite asset
|
|
80
|
+
imports of their sources (`?raw`, `?url`, `?inline`), as
|
|
81
|
+
[arto-markdown](https://github.com/arto-app/arto-markdown)
|
|
82
|
+
(`@arto-app/markdown`), which renders the Chat's Markdown, does: a host
|
|
83
|
+
builds them with Vite.
|
|
84
|
+
|
|
85
|
+
Inside this repository the packages are Bun workspaces whose exports name
|
|
86
|
+
their TypeScript sources, and the crates are a Cargo workspace.
|
|
87
|
+
|
|
88
|
+
## Usage
|
|
89
|
+
|
|
90
|
+
The Storybook of main is published at
|
|
91
|
+
<https://adroit-app.github.io/adroit-chat/>.
|
|
92
|
+
|
|
93
|
+
`just storybook` opens Storybook with every scenario the fake agent plays,
|
|
94
|
+
at its end or live, in light and dark; its toolbar switches the theme and
|
|
95
|
+
its controls set the rendering policy. It serves on a local port.
|
|
96
|
+
|
|
97
|
+
`just shots` builds Storybook and takes a screenshot of every story in both
|
|
98
|
+
themes into `dist/shots/`, from the build opened from disk, without a port.
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
Enter the development shell with `nix develop` (or
|
|
103
|
+
copy `.envrc.sample` to `.envrc` and `direnv allow`), which carries Bun,
|
|
104
|
+
Node, Rust and, on Linux, the libraries Tauri builds against, and:
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
just install # the dependencies bun.lock pins
|
|
108
|
+
just fmt # format every source
|
|
109
|
+
just check # format check, lint, types, unit tests (TS and Rust), Storybook build,
|
|
110
|
+
# the packages as a release publishes them, the version stamp, the workflows
|
|
111
|
+
just ci # all of the above, and every story played in Chromium
|
|
112
|
+
just ci-vm # the web half of `just ci` and the screenshot comparison, in a vitro VM
|
|
113
|
+
just ui # build the Chat's page the adroit-chat crate embeds
|
|
114
|
+
just package # the npm packages staged into dist/npm, and the crate's package, checked
|
|
115
|
+
just build # the bundled app, into target/release/bundle
|
|
116
|
+
just bundle # what a release attaches for this platform, into dist/ (after `just ui`)
|
|
117
|
+
just run -- claude # the app from source
|
|
118
|
+
just run -- codex # the same with Codex
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The stories run in Chromium through a local port, so on a workstation they
|
|
122
|
+
run in a throwaway vitro VM (`just ci-vm`). The screenshots they are
|
|
123
|
+
compared with are taken there, in vitro's `linux` image, and only compare in
|
|
124
|
+
that image.
|
|
125
|
+
|
|
126
|
+
### Releasing
|
|
127
|
+
|
|
128
|
+
Write the notes and publish a GitHub release with a new tag `vX.Y.Z` on
|
|
129
|
+
main. The release workflow stamps that version over the manifests' `0.0.0`,
|
|
130
|
+
then publishes the `adroit-chat` crate, with the Chat's page built in, to
|
|
131
|
+
crates.io, and the npm packages to npm, their `workspace:` dependencies
|
|
132
|
+
pinned to the same version. Both go through trusted publishing; no token is
|
|
133
|
+
stored. It also bundles the window for each platform, outside Nix, signs the
|
|
134
|
+
macOS app by the identity "lambdalisue Release" (from the organization's
|
|
135
|
+
secrets `MACOS_SIGNING_P12` and `MACOS_SIGNING_PASSWORD`), attaches the
|
|
136
|
+
bundles and their checksums to the release, and dispatches to
|
|
137
|
+
[adroit-app/homebrew-tap](https://github.com/adroit-app/homebrew-tap), which
|
|
138
|
+
moves `Casks/adroit-chat.rb` to the disk image. CI bundles the same way on
|
|
139
|
+
every pull request, signed ad hoc. Nothing is committed for a release: the
|
|
140
|
+
tag is the only record of the version.
|
|
141
|
+
|
|
142
|
+
Dependabot keeps the crates, the Bun packages and the actions current,
|
|
143
|
+
except `agent-client-protocol` and `playwright`, which move by hand; a weekly
|
|
144
|
+
workflow does the same for `flake.lock`.
|
|
145
|
+
|
|
146
|
+
The design lives in [docs/concept.md](docs/concept.md), the decisions behind it
|
|
147
|
+
in [docs/adr/](docs/adr/), and the behaviour it promises in
|
|
148
|
+
[docs/spec/](docs/spec/).
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT. See [LICENSE](LICENSE).
|
package/dist/acp.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ACP types the Chat is built on: protocol v2 with its unstable
|
|
3
|
+
* features, as the official SDK defines them. Imported as types only, so
|
|
4
|
+
* nothing of the SDK's runtime reaches a bundle.
|
|
5
|
+
*/
|
|
6
|
+
export type { AvailableCommand, BackgroundWork, ContentBlock, Cost, CreateElicitationRequest, CreateElicitationResponse, ElicitationPropertySchema, ElicitationSchema, NoticeSeverity, PermissionOption, PlanEntry, PlanUpdateContent, PromptResponse, RequestPermissionOutcome, RequestPermissionRequest, RequestPermissionResponse, SessionConfigOption, SessionUpdate, StateUpdate, TerminalExitStatus, ToolCallContent, ToolCallLocation, ToolCallStatus, ToolCallUpdate, ToolKind, UpdateSessionNotification, Usage, } from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
7
|
+
import type { SessionUpdate } from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
8
|
+
/**
|
|
9
|
+
* The known variant of `SessionUpdate` tagged `tag`. The SDK's unions keep a
|
|
10
|
+
* catch-all for variants a later protocol adds, which a `switch` on the tag
|
|
11
|
+
* cannot narrow away.
|
|
12
|
+
*/
|
|
13
|
+
export type UpdateOf<Tag extends string> = Extract<SessionUpdate, {
|
|
14
|
+
sessionUpdate: Tag;
|
|
15
|
+
}>;
|
|
16
|
+
import type { ToolCallContent } from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
17
|
+
/** The known variant of `ToolCallContent` tagged `tag`. */
|
|
18
|
+
export type ContentOf<Tag extends string> = Extract<ToolCallContent, {
|
|
19
|
+
type: Tag;
|
|
20
|
+
}>;
|
|
21
|
+
/** ACP v1 with its unstable features, which agents speak today. */
|
|
22
|
+
export type * as V1 from "@agentclientprotocol/sdk/unstable";
|
package/dist/acp.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/asks.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RequestPermissionOutcome } from "./acp.ts";
|
|
2
|
+
import type { AskEntry, ChatState } from "./state.ts";
|
|
3
|
+
/** The asks the agent waits on, in the order it asked them. */
|
|
4
|
+
export declare function openAsks(state: ChatState): AskEntry[];
|
|
5
|
+
/** What an ask is about, in a line: the command, or the tool call's line or title. */
|
|
6
|
+
export declare function askSubject(entry: AskEntry, state: ChatState): string;
|
|
7
|
+
/** The tool call an ask is about, when it is about one. */
|
|
8
|
+
export declare function askTool(entry: AskEntry): string | null;
|
|
9
|
+
/** The tool calls of session `sessionId` an open ask is about. */
|
|
10
|
+
export declare function waitingTools(state: ChatState, sessionId?: string): Set<string>;
|
|
11
|
+
/** The ask, answered or open, about tool call `toolCallId` of session `sessionId`. */
|
|
12
|
+
export declare function askFor(state: ChatState, sessionId: string, toolCallId: string): AskEntry | null;
|
|
13
|
+
/** The reason a permission answer carries in `_meta.reason`, as the Chat sends one. */
|
|
14
|
+
export declare function reasonOf(outcome: RequestPermissionOutcome): string | null;
|
|
15
|
+
/** How an answered ask is remembered in the transcript, in a line. */
|
|
16
|
+
export declare function askRecord(entry: AskEntry, state: ChatState): string;
|
|
17
|
+
/** A field of a form the user answered, as it was asked, with what was entered. */
|
|
18
|
+
export interface AnsweredField {
|
|
19
|
+
label: string;
|
|
20
|
+
description: string | null;
|
|
21
|
+
required: boolean;
|
|
22
|
+
/** `choice` picks one of `options`, `choices` any of them. */
|
|
23
|
+
kind: "text" | "number" | "boolean" | "choice" | "choices";
|
|
24
|
+
options: Array<{
|
|
25
|
+
value: string;
|
|
26
|
+
label: string;
|
|
27
|
+
}>;
|
|
28
|
+
/** What was entered: none when nothing was, or the form was not sent. */
|
|
29
|
+
value: unknown;
|
|
30
|
+
}
|
|
31
|
+
/** A form the user answered: its message, its fields, and the button that answered it. */
|
|
32
|
+
export interface AnsweredForm {
|
|
33
|
+
message: string;
|
|
34
|
+
action: "accept" | "decline" | "cancel";
|
|
35
|
+
fields: AnsweredField[];
|
|
36
|
+
}
|
|
37
|
+
/** `entry` as the answered form it was, read only; none for anything but an answered form. */
|
|
38
|
+
export declare function answeredForm(entry: AskEntry): AnsweredForm | null;
|
package/dist/asks.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { toolInput, toolLine } from "./text.js";
|
|
2
|
+
/** The asks the agent waits on, in the order it asked them. */
|
|
3
|
+
export function openAsks(state) {
|
|
4
|
+
return state.entries.filter((e) => e.kind === "ask" && e.answer === null);
|
|
5
|
+
}
|
|
6
|
+
function subjectOf(entry) {
|
|
7
|
+
return entry.ask.type === "permission" ? entry.ask.request.subject : null;
|
|
8
|
+
}
|
|
9
|
+
/** The tool call `id` of the session `sessionId`: the parent's or a subagent's; any session's without one. */
|
|
10
|
+
function toolOf(id, state, sessionId) {
|
|
11
|
+
const transcripts = sessionId === undefined
|
|
12
|
+
? [state.entries, ...Object.values(state.subagents).map((s) => s.entries)]
|
|
13
|
+
: [
|
|
14
|
+
sessionId === state.sessionId
|
|
15
|
+
? state.entries
|
|
16
|
+
: (state.subagents[sessionId]?.entries ?? []),
|
|
17
|
+
];
|
|
18
|
+
for (const entries of transcripts) {
|
|
19
|
+
const tool = entries.find((e) => e.kind === "tool" && e.id === id);
|
|
20
|
+
if (tool)
|
|
21
|
+
return tool;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/** What an ask is about, in a line: the command, or the tool call's line or title. */
|
|
26
|
+
export function askSubject(entry, state) {
|
|
27
|
+
const subject = subjectOf(entry);
|
|
28
|
+
if (subject?.type === "command")
|
|
29
|
+
return subject.command;
|
|
30
|
+
if (subject?.type === "tool_call") {
|
|
31
|
+
const sessionId = entry.ask.type === "permission" ? entry.ask.request.sessionId : undefined;
|
|
32
|
+
const tool = toolOf(subject.toolCall.toolCallId, state, sessionId);
|
|
33
|
+
if (!tool)
|
|
34
|
+
return subject.toolCall.title ?? "";
|
|
35
|
+
const input = toolInput(tool);
|
|
36
|
+
return input?.language === "sh" ? input.text : toolLine(tool) || tool.title;
|
|
37
|
+
}
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
/** The tool call an ask is about, when it is about one. */
|
|
41
|
+
export function askTool(entry) {
|
|
42
|
+
if (entry.ask.type === "elicitation") {
|
|
43
|
+
const id = entry.ask.request.toolCallId;
|
|
44
|
+
return typeof id === "string" ? id : null;
|
|
45
|
+
}
|
|
46
|
+
const subject = subjectOf(entry);
|
|
47
|
+
if (subject?.type === "tool_call")
|
|
48
|
+
return subject.toolCall.toolCallId;
|
|
49
|
+
if (subject?.type === "command")
|
|
50
|
+
return subject.toolCallId ?? null;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The session an ask is for: the one its request names, else the Chat's own.
|
|
55
|
+
* A subagent's tool call ids are its own, and may be its parent's too.
|
|
56
|
+
*/
|
|
57
|
+
function askSession(entry, state) {
|
|
58
|
+
const sessionId = entry.ask.request.sessionId;
|
|
59
|
+
return typeof sessionId === "string" && sessionId !== "" ? sessionId : state.sessionId;
|
|
60
|
+
}
|
|
61
|
+
/** The tool calls of session `sessionId` an open ask is about. */
|
|
62
|
+
export function waitingTools(state, sessionId = state.sessionId) {
|
|
63
|
+
const ids = new Set();
|
|
64
|
+
for (const ask of openAsks(state)) {
|
|
65
|
+
const id = askTool(ask);
|
|
66
|
+
if (id && askSession(ask, state) === sessionId)
|
|
67
|
+
ids.add(id);
|
|
68
|
+
}
|
|
69
|
+
return ids;
|
|
70
|
+
}
|
|
71
|
+
/** The ask, answered or open, about tool call `toolCallId` of session `sessionId`. */
|
|
72
|
+
export function askFor(state, sessionId, toolCallId) {
|
|
73
|
+
return (state.entries.find((e) => e.kind === "ask" && askTool(e) === toolCallId && askSession(e, state) === sessionId) ?? null);
|
|
74
|
+
}
|
|
75
|
+
/** The reason a permission answer carries in `_meta.reason`, as the Chat sends one. */
|
|
76
|
+
export function reasonOf(outcome) {
|
|
77
|
+
const reason = outcome._meta?.reason;
|
|
78
|
+
return typeof reason === "string" && reason.trim() !== "" ? reason.trim() : null;
|
|
79
|
+
}
|
|
80
|
+
/** How an answered ask is remembered in the transcript, in a line. */
|
|
81
|
+
export function askRecord(entry, state) {
|
|
82
|
+
const answer = entry.answer;
|
|
83
|
+
if (answer?.type === "permission" && entry.ask.type === "permission") {
|
|
84
|
+
const outcome = answer.outcome;
|
|
85
|
+
const subject = askSubject(entry, state);
|
|
86
|
+
const option = entry.ask.request.options.find((o) => o.optionId === outcome.optionId);
|
|
87
|
+
const reason = reasonOf(answer.outcome);
|
|
88
|
+
const said = outcome.outcome === "selected"
|
|
89
|
+
? `${option?.name ?? outcome.optionId}${reason ? `: ${reason}` : ""}`
|
|
90
|
+
: "Cancelled";
|
|
91
|
+
return subject ? `${said} · ${subject}` : said;
|
|
92
|
+
}
|
|
93
|
+
if (answer?.type === "elicitation") {
|
|
94
|
+
const response = answer.response;
|
|
95
|
+
if (response.action === "accept") {
|
|
96
|
+
const fields = Object.entries(response.content ?? {})
|
|
97
|
+
.map(([k, v]) => `${fieldTitle(entry, k)}: ${Array.isArray(v) ? v.join(", ") : String(v)}`)
|
|
98
|
+
.join(" · ");
|
|
99
|
+
return fields ? `Sent — ${fields}` : "Done";
|
|
100
|
+
}
|
|
101
|
+
return response.action === "decline" ? "Declined" : "Cancelled";
|
|
102
|
+
}
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
105
|
+
/** How a form's field is named to the user: its title, or its description, never its id. */
|
|
106
|
+
function fieldTitle(entry, key) {
|
|
107
|
+
const schema = entry.ask.request.requestedSchema;
|
|
108
|
+
const field = schema?.properties?.[key];
|
|
109
|
+
return field?.title ?? field?.description ?? key;
|
|
110
|
+
}
|
|
111
|
+
const optionsOf = (listed, plain) => listed?.map((o) => ({ value: o.const, label: o.title ?? o.const })) ??
|
|
112
|
+
plain?.map((v) => ({ value: v, label: v })) ??
|
|
113
|
+
[];
|
|
114
|
+
/** `entry` as the answered form it was, read only; none for anything but an answered form. */
|
|
115
|
+
export function answeredForm(entry) {
|
|
116
|
+
if (entry.ask.type !== "elicitation" || entry.answer?.type !== "elicitation")
|
|
117
|
+
return null;
|
|
118
|
+
const request = entry.ask.request;
|
|
119
|
+
const response = entry.answer.response;
|
|
120
|
+
const action = response.action === "accept" || response.action === "decline" ? response.action : "cancel";
|
|
121
|
+
const required = request.requestedSchema?.required ?? [];
|
|
122
|
+
const content = action === "accept" ? (response.content ?? {}) : {};
|
|
123
|
+
const fields = Object.entries(request.requestedSchema?.properties ?? {}).map(([name, field]) => {
|
|
124
|
+
const kind = field.type === "boolean"
|
|
125
|
+
? "boolean"
|
|
126
|
+
: field.type === "array"
|
|
127
|
+
? "choices"
|
|
128
|
+
: field.oneOf || field.enum
|
|
129
|
+
? "choice"
|
|
130
|
+
: field.type === "number" || field.type === "integer"
|
|
131
|
+
? "number"
|
|
132
|
+
: "text";
|
|
133
|
+
return {
|
|
134
|
+
label: field.title ?? field.description ?? name,
|
|
135
|
+
description: field.title ? (field.description ?? null) : null,
|
|
136
|
+
required: required.includes(name),
|
|
137
|
+
kind,
|
|
138
|
+
options: kind === "choices"
|
|
139
|
+
? optionsOf(field.items?.anyOf, field.items?.enum)
|
|
140
|
+
: kind === "choice"
|
|
141
|
+
? optionsOf(field.oneOf, field.enum)
|
|
142
|
+
: [],
|
|
143
|
+
value: content[name] ?? null,
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
return { message: request.message ?? "", action, fields };
|
|
147
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { AvailableCommand, ContentBlock } from "./acp.ts";
|
|
2
|
+
/** What a completion offers. */
|
|
3
|
+
export interface CompletionItem {
|
|
4
|
+
/** How the item is listed. */
|
|
5
|
+
label: string;
|
|
6
|
+
/** What replaces the trigger and the query when it is accepted, trigger included. */
|
|
7
|
+
insertText: string;
|
|
8
|
+
/** A line about it, such as a command's description or a file's folder. */
|
|
9
|
+
detail?: string;
|
|
10
|
+
kind: "command" | "skill" | "file" | "directory" | (string & {});
|
|
11
|
+
/** A placeholder for what the item takes after it, such as a command's input. */
|
|
12
|
+
hint?: string;
|
|
13
|
+
/** The resource an accepted path stands for, sent beside the text as a `resource_link`. */
|
|
14
|
+
uri?: string;
|
|
15
|
+
}
|
|
16
|
+
/** What a provider is told besides the query. */
|
|
17
|
+
export interface CompletionContext {
|
|
18
|
+
/** Aborted when the query is stale: the user typed on, or closed the list. */
|
|
19
|
+
signal: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
export type CompletionItems = CompletionItem[] | Promise<CompletionItem[]>;
|
|
22
|
+
/**
|
|
23
|
+
* A source of completions for what follows `trigger`, written at the start
|
|
24
|
+
* of a line or after whitespace. It answers with the items at once, or
|
|
25
|
+
* streams them in batches.
|
|
26
|
+
*/
|
|
27
|
+
export interface CompletionProvider {
|
|
28
|
+
trigger: string | RegExp;
|
|
29
|
+
complete(query: string, context: CompletionContext): CompletionItems | AsyncIterable<CompletionItem[]>;
|
|
30
|
+
}
|
|
31
|
+
/** Where a completion is in the text: the trigger at `start`, the query up to `end`. */
|
|
32
|
+
export interface CompletionRange {
|
|
33
|
+
start: number;
|
|
34
|
+
end: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The completion the caret is in: a provider's trigger at the start of a
|
|
38
|
+
* line or after whitespace, and what was written after it up to the caret;
|
|
39
|
+
* none when the caret is not in one.
|
|
40
|
+
*/
|
|
41
|
+
export declare function findTrigger(text: string, caret: number, providers: CompletionProvider[]): {
|
|
42
|
+
provider: CompletionProvider;
|
|
43
|
+
start: number;
|
|
44
|
+
query: string;
|
|
45
|
+
} | null;
|
|
46
|
+
/**
|
|
47
|
+
* `text` with `item` accepted over `range`: followed by a space, or, for a
|
|
48
|
+
* directory, left open so completing goes on inside it (`again`).
|
|
49
|
+
*/
|
|
50
|
+
export declare function accept(text: string, range: CompletionRange, item: CompletionItem): {
|
|
51
|
+
text: string;
|
|
52
|
+
caret: number;
|
|
53
|
+
again: boolean;
|
|
54
|
+
};
|
|
55
|
+
/** All a provider answers for `query`, its batches joined. */
|
|
56
|
+
export declare function collect(answer: CompletionItems | AsyncIterable<CompletionItem[]>): Promise<CompletionItem[]>;
|
|
57
|
+
/**
|
|
58
|
+
* The agent's slash commands, as `/` completes them: each with its
|
|
59
|
+
* description and the hint of what it takes. acp-adapter lists Claude
|
|
60
|
+
* Code's skills among them the same way, unmarked.
|
|
61
|
+
*/
|
|
62
|
+
export declare function commandProvider(commands: () => AvailableCommand[]): CompletionProvider;
|
|
63
|
+
/**
|
|
64
|
+
* Runs a provider for the query being written: once the typing has settled
|
|
65
|
+
* for `delay` ms, aborting the query before it, and shows only the latest
|
|
66
|
+
* query's items, as they come.
|
|
67
|
+
*/
|
|
68
|
+
export declare class Completer {
|
|
69
|
+
#private;
|
|
70
|
+
constructor(delay: number, show: (items: CompletionItem[]) => void);
|
|
71
|
+
update(provider: CompletionProvider, query: string): void;
|
|
72
|
+
close(): void;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The accepted paths still written in `text`, as ACP `resource_link` blocks
|
|
76
|
+
* to send beside it.
|
|
77
|
+
*/
|
|
78
|
+
export declare function resourceLinks(text: string, accepted: CompletionItem[]): ContentBlock[];
|
|
79
|
+
/**
|
|
80
|
+
* What the command `draft` begins with takes, while nothing is written after
|
|
81
|
+
* it yet: shown faint after the caret, never written into the draft, so
|
|
82
|
+
* sending what was accepted does not send the placeholder too.
|
|
83
|
+
*/
|
|
84
|
+
export declare function argumentHint(draft: string, commands: AvailableCommand[]): string | null;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
function matches(trigger, char) {
|
|
2
|
+
return typeof trigger === "string"
|
|
3
|
+
? trigger === char
|
|
4
|
+
: new RegExp(`^(?:${trigger.source})$`, trigger.flags.replace("g", "")).test(char);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The completion the caret is in: a provider's trigger at the start of a
|
|
8
|
+
* line or after whitespace, and what was written after it up to the caret;
|
|
9
|
+
* none when the caret is not in one.
|
|
10
|
+
*/
|
|
11
|
+
export function findTrigger(text, caret, providers) {
|
|
12
|
+
const before = text.slice(0, caret);
|
|
13
|
+
const word = /\S*$/.exec(before)?.[0] ?? "";
|
|
14
|
+
if (word === "")
|
|
15
|
+
return null;
|
|
16
|
+
const start = caret - word.length;
|
|
17
|
+
const provider = providers.find((p) => matches(p.trigger, word[0]));
|
|
18
|
+
if (!provider)
|
|
19
|
+
return null;
|
|
20
|
+
return { provider, start, query: word.slice(1) };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `text` with `item` accepted over `range`: followed by a space, or, for a
|
|
24
|
+
* directory, left open so completing goes on inside it (`again`).
|
|
25
|
+
*/
|
|
26
|
+
export function accept(text, range, item) {
|
|
27
|
+
const again = item.kind === "directory";
|
|
28
|
+
const inserted = again ? item.insertText : `${item.insertText} `;
|
|
29
|
+
return {
|
|
30
|
+
text: text.slice(0, range.start) + inserted + text.slice(range.end),
|
|
31
|
+
caret: range.start + inserted.length,
|
|
32
|
+
again,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** All a provider answers for `query`, its batches joined. */
|
|
36
|
+
export async function collect(answer) {
|
|
37
|
+
if (Symbol.asyncIterator in Object(answer)) {
|
|
38
|
+
const items = [];
|
|
39
|
+
for await (const batch of answer)
|
|
40
|
+
items.push(...batch);
|
|
41
|
+
return items;
|
|
42
|
+
}
|
|
43
|
+
return answer;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The agent's slash commands, as `/` completes them: each with its
|
|
47
|
+
* description and the hint of what it takes. acp-adapter lists Claude
|
|
48
|
+
* Code's skills among them the same way, unmarked.
|
|
49
|
+
*/
|
|
50
|
+
export function commandProvider(commands) {
|
|
51
|
+
return {
|
|
52
|
+
trigger: "/",
|
|
53
|
+
complete(query) {
|
|
54
|
+
const typed = query.toLowerCase();
|
|
55
|
+
return commands()
|
|
56
|
+
.filter((c) => c.name.toLowerCase().startsWith(typed))
|
|
57
|
+
.map((c) => {
|
|
58
|
+
const hint = hintOf(c);
|
|
59
|
+
return {
|
|
60
|
+
label: `/${c.name}`,
|
|
61
|
+
insertText: `/${c.name}`,
|
|
62
|
+
...(c.description ? { detail: c.description } : {}),
|
|
63
|
+
kind: "command",
|
|
64
|
+
...(hint ? { hint } : {}),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Runs a provider for the query being written: once the typing has settled
|
|
72
|
+
* for `delay` ms, aborting the query before it, and shows only the latest
|
|
73
|
+
* query's items, as they come.
|
|
74
|
+
*/
|
|
75
|
+
export class Completer {
|
|
76
|
+
#delay;
|
|
77
|
+
#show;
|
|
78
|
+
#timer = null;
|
|
79
|
+
#running = null;
|
|
80
|
+
constructor(delay, show) {
|
|
81
|
+
this.#delay = delay;
|
|
82
|
+
this.#show = show;
|
|
83
|
+
}
|
|
84
|
+
update(provider, query) {
|
|
85
|
+
this.close();
|
|
86
|
+
this.#timer = setTimeout(() => void this.#run(provider, query), this.#delay);
|
|
87
|
+
}
|
|
88
|
+
close() {
|
|
89
|
+
if (this.#timer !== null)
|
|
90
|
+
clearTimeout(this.#timer);
|
|
91
|
+
this.#timer = null;
|
|
92
|
+
this.#running?.abort();
|
|
93
|
+
this.#running = null;
|
|
94
|
+
}
|
|
95
|
+
async #run(provider, query) {
|
|
96
|
+
const running = new AbortController();
|
|
97
|
+
this.#running = running;
|
|
98
|
+
const { signal } = running;
|
|
99
|
+
try {
|
|
100
|
+
const answer = provider.complete(query, { signal });
|
|
101
|
+
if (Symbol.asyncIterator in Object(answer)) {
|
|
102
|
+
const items = [];
|
|
103
|
+
for await (const batch of answer) {
|
|
104
|
+
if (signal.aborted)
|
|
105
|
+
return;
|
|
106
|
+
items.push(...batch);
|
|
107
|
+
this.#show([...items]);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const items = await answer;
|
|
112
|
+
if (!signal.aborted)
|
|
113
|
+
this.#show(items);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (!signal.aborted)
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The accepted paths still written in `text`, as ACP `resource_link` blocks
|
|
123
|
+
* to send beside it.
|
|
124
|
+
*/
|
|
125
|
+
export function resourceLinks(text, accepted) {
|
|
126
|
+
const seen = new Set();
|
|
127
|
+
return accepted.flatMap((item) => {
|
|
128
|
+
if (!item.uri || seen.has(item.uri) || !text.includes(item.insertText))
|
|
129
|
+
return [];
|
|
130
|
+
seen.add(item.uri);
|
|
131
|
+
return [
|
|
132
|
+
{
|
|
133
|
+
type: "resource_link",
|
|
134
|
+
uri: item.uri,
|
|
135
|
+
name: item.insertText.replace(/^@/, ""),
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function hintOf(command) {
|
|
141
|
+
return command.input?.hint;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* What the command `draft` begins with takes, while nothing is written after
|
|
145
|
+
* it yet: shown faint after the caret, never written into the draft, so
|
|
146
|
+
* sending what was accepted does not send the placeholder too.
|
|
147
|
+
*/
|
|
148
|
+
export function argumentHint(draft, commands) {
|
|
149
|
+
const name = /^\/(\S+) $/.exec(draft)?.[1];
|
|
150
|
+
if (name === undefined)
|
|
151
|
+
return null;
|
|
152
|
+
const command = commands.find((c) => c.name === name);
|
|
153
|
+
return (command && hintOf(command)) || null;
|
|
154
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SessionConfigOption } from "./acp.ts";
|
|
2
|
+
/** One of the session's settings: what it is, and its value now. */
|
|
3
|
+
export interface SessionSetting {
|
|
4
|
+
category: string;
|
|
5
|
+
label: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}
|
|
8
|
+
/** What the host started the agent with, as far as it knows. */
|
|
9
|
+
export interface StartedWith {
|
|
10
|
+
model?: string | null;
|
|
11
|
+
mode?: string | null;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The model, effort and mode the session runs with now, named as the agent
|
|
15
|
+
* names them in its config options. Where the agent reports none, what the
|
|
16
|
+
* host started it with stands in.
|
|
17
|
+
*/
|
|
18
|
+
export declare function sessionSettings(options: readonly SessionConfigOption[], startedWith?: StartedWith): SessionSetting[];
|