@adroit-app/chat-ui 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/attach.d.ts +18 -0
- package/dist/attach.js +52 -0
- package/dist/chat.d.ts +330 -0
- package/dist/chat.js +2090 -0
- package/dist/composer.d.ts +27 -0
- package/dist/composer.js +46 -0
- package/dist/follow.d.ts +12 -0
- package/dist/follow.js +91 -0
- package/dist/form.d.ts +12 -0
- package/dist/form.js +15 -0
- package/dist/icons.d.ts +56 -0
- package/dist/icons.js +128 -0
- package/dist/image-actions.d.ts +33 -0
- package/dist/image-actions.js +10 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +10 -0
- package/dist/keymap.d.ts +29 -0
- package/dist/keymap.js +69 -0
- package/dist/markdown/frontend.d.ts +6 -0
- package/dist/markdown/frontend.js +103 -0
- package/dist/markdown/picture.d.ts +22 -0
- package/dist/markdown/picture.js +57 -0
- package/dist/markdown/render.d.ts +71 -0
- package/dist/markdown/render.js +183 -0
- package/dist/markdown/view.d.ts +28 -0
- package/dist/markdown/view.js +39 -0
- package/dist/read.d.ts +32 -0
- package/dist/read.js +149 -0
- package/dist/scale.d.ts +7 -0
- package/dist/scale.js +9 -0
- package/dist/styles.d.ts +4 -0
- package/dist/styles.js +2166 -0
- package/dist/tools.d.ts +175 -0
- package/dist/tools.js +487 -0
- package/dist/view.d.ts +33 -0
- package/dist/view.js +37 -0
- package/dist/viewer.d.ts +78 -0
- package/dist/viewer.js +496 -0
- package/package.json +22 -3
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { type AskedQuestions, type AskEntry, type ToolEntry } from "@adroit-app/chat-core";
|
|
2
|
+
import type { MarkdownPolicy } from "./markdown/render.ts";
|
|
3
|
+
/** A tool call as a renderer sees it: the call the agent reported, and its text. */
|
|
4
|
+
export interface ToolCallView {
|
|
5
|
+
/** The tool call: its ACP kind, title, status, content, locations and raw input and output. */
|
|
6
|
+
call: Readonly<ToolEntry>;
|
|
7
|
+
/** What it was given, as text: its command, or its input as JSON; null when given nothing. */
|
|
8
|
+
inputText: string | null;
|
|
9
|
+
/** What it gave back, as text: its text content, or its raw output; null when nothing. */
|
|
10
|
+
outputText: string | null;
|
|
11
|
+
/** The ask the Chat answered for this call, such as the form of a question it asked; none when none. */
|
|
12
|
+
ask: AskEntry | null;
|
|
13
|
+
}
|
|
14
|
+
/** One labelled value, as a short row. */
|
|
15
|
+
export interface Field {
|
|
16
|
+
label: string;
|
|
17
|
+
value: string;
|
|
18
|
+
/** How the value reads: a file path, code, a URL, or text (the default). */
|
|
19
|
+
as?: "path" | "code" | "url";
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* How one part of a tool call is drawn. `text`, where a variant takes it,
|
|
23
|
+
* draws that instead of the part's own text.
|
|
24
|
+
*/
|
|
25
|
+
export type Rendering =
|
|
26
|
+
/** As plain text, coloured by nothing. */
|
|
27
|
+
{
|
|
28
|
+
as: "plain";
|
|
29
|
+
text?: string;
|
|
30
|
+
}
|
|
31
|
+
/** As code in `language`, as highlight.js names it. */
|
|
32
|
+
| {
|
|
33
|
+
as: "code";
|
|
34
|
+
language: string;
|
|
35
|
+
text?: string;
|
|
36
|
+
}
|
|
37
|
+
/** As JSON, laid out when it parses, plain when it does not. */
|
|
38
|
+
| {
|
|
39
|
+
as: "json";
|
|
40
|
+
text?: string;
|
|
41
|
+
}
|
|
42
|
+
/** As Markdown, under the Chat's policy with `options` over it. */
|
|
43
|
+
| {
|
|
44
|
+
as: "markdown";
|
|
45
|
+
options?: Partial<MarkdownPolicy>;
|
|
46
|
+
text?: string;
|
|
47
|
+
}
|
|
48
|
+
/** As a unified diff. */
|
|
49
|
+
| {
|
|
50
|
+
as: "diff";
|
|
51
|
+
text?: string;
|
|
52
|
+
}
|
|
53
|
+
/** As labelled rows. */
|
|
54
|
+
| {
|
|
55
|
+
as: "fields";
|
|
56
|
+
fields: Field[];
|
|
57
|
+
}
|
|
58
|
+
/** As the form questions were asked in, read only: each with its options, the chosen checked. */
|
|
59
|
+
| ({
|
|
60
|
+
as: "answers";
|
|
61
|
+
} & AskedQuestions)
|
|
62
|
+
/** As several renderings, one under another. */
|
|
63
|
+
| {
|
|
64
|
+
as: "group";
|
|
65
|
+
parts: Rendering[];
|
|
66
|
+
}
|
|
67
|
+
/** As one of several renderings the user switches between, the first shown first. */
|
|
68
|
+
| {
|
|
69
|
+
as: "views";
|
|
70
|
+
views: Array<{
|
|
71
|
+
label: string;
|
|
72
|
+
rendering: Rendering;
|
|
73
|
+
}>;
|
|
74
|
+
}
|
|
75
|
+
/** As images, each opening on its own on a click. */
|
|
76
|
+
| {
|
|
77
|
+
as: "images";
|
|
78
|
+
images: ToolImage[];
|
|
79
|
+
}
|
|
80
|
+
/** Not at all. */
|
|
81
|
+
| {
|
|
82
|
+
as: "none";
|
|
83
|
+
}
|
|
84
|
+
/** As an element the host built; the Chat only places it. */
|
|
85
|
+
| {
|
|
86
|
+
as: "element";
|
|
87
|
+
element: HTMLElement;
|
|
88
|
+
};
|
|
89
|
+
/** An image a tool gave back: its bytes, and its file when it has one. */
|
|
90
|
+
export interface ToolImage {
|
|
91
|
+
/** Base64 of the image's bytes. */
|
|
92
|
+
data: string;
|
|
93
|
+
mimeType: string;
|
|
94
|
+
name?: string;
|
|
95
|
+
path?: string;
|
|
96
|
+
}
|
|
97
|
+
/** How a call's parts are drawn; a part left out is someone else's to choose. */
|
|
98
|
+
export type ToolRendering = {
|
|
99
|
+
input?: Rendering;
|
|
100
|
+
output?: Rendering;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Chooses how a tool call's input and output are drawn. The Chat draws a
|
|
104
|
+
* part it leaves out, or every part of a call it returns nothing for, as
|
|
105
|
+
* `defaultToolRenderer` does.
|
|
106
|
+
*/
|
|
107
|
+
export type ToolRenderer = (view: ToolCallView) => ToolRendering | undefined | null;
|
|
108
|
+
/** What a part is drawn as, once settled. */
|
|
109
|
+
export type Drawn = {
|
|
110
|
+
as: "code";
|
|
111
|
+
language: string;
|
|
112
|
+
text: string;
|
|
113
|
+
} | {
|
|
114
|
+
as: "markdown";
|
|
115
|
+
text: string;
|
|
116
|
+
options: Partial<MarkdownPolicy>;
|
|
117
|
+
} | {
|
|
118
|
+
as: "fields";
|
|
119
|
+
fields: Field[];
|
|
120
|
+
} | ({
|
|
121
|
+
as: "answers";
|
|
122
|
+
} & AskedQuestions) | {
|
|
123
|
+
as: "group";
|
|
124
|
+
parts: Drawn[];
|
|
125
|
+
} | {
|
|
126
|
+
as: "views";
|
|
127
|
+
views: Array<{
|
|
128
|
+
label: string;
|
|
129
|
+
drawn: Drawn;
|
|
130
|
+
}>;
|
|
131
|
+
} | {
|
|
132
|
+
as: "images";
|
|
133
|
+
images: ToolImage[];
|
|
134
|
+
} | {
|
|
135
|
+
as: "element";
|
|
136
|
+
element: HTMLElement;
|
|
137
|
+
};
|
|
138
|
+
export declare function viewOf(call: ToolEntry, ask?: AskEntry | null): ToolCallView;
|
|
139
|
+
/**
|
|
140
|
+
* `renderers` as one: each part is drawn as the first of them that chooses
|
|
141
|
+
* for it.
|
|
142
|
+
*/
|
|
143
|
+
export declare function composeToolRenderers(...renderers: ToolRenderer[]): ToolRenderer;
|
|
144
|
+
/**
|
|
145
|
+
* How `call`'s input and output are drawn: as `renderer` chooses, and as
|
|
146
|
+
* `defaultToolRenderer` does for what it leaves out.
|
|
147
|
+
*/
|
|
148
|
+
export declare function drawnParts(call: ToolEntry, renderer: ToolRenderer | null | undefined, ask?: AskEntry | null): {
|
|
149
|
+
input: Drawn | null;
|
|
150
|
+
output: Drawn | null;
|
|
151
|
+
};
|
|
152
|
+
/** The default renderers, one per kind of call, which a host can compose with its own. */
|
|
153
|
+
export declare const toolRenderers: {
|
|
154
|
+
question: ToolRenderer;
|
|
155
|
+
command: ToolRenderer;
|
|
156
|
+
edit: ToolRenderer;
|
|
157
|
+
read: ToolRenderer;
|
|
158
|
+
search: ToolRenderer;
|
|
159
|
+
fetch: ToolRenderer;
|
|
160
|
+
think: ToolRenderer;
|
|
161
|
+
move: ToolRenderer;
|
|
162
|
+
delete: ToolRenderer;
|
|
163
|
+
otherInput: ToolRenderer;
|
|
164
|
+
readOutput: ToolRenderer;
|
|
165
|
+
output: ToolRenderer;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* How the Chat draws a call when its host does not choose: by the call's
|
|
169
|
+
* ACP kind and what its input carries. A command is shell; an edit is its
|
|
170
|
+
* file and its change; a read, a search, a fetch, a move or a delete are
|
|
171
|
+
* their paths, patterns and URLs as labelled rows; thinking is its text.
|
|
172
|
+
* Other input is labelled rows when flat and JSON when not; output is JSON
|
|
173
|
+
* when it parses and plain otherwise.
|
|
174
|
+
*/
|
|
175
|
+
export declare const defaultToolRenderer: ToolRenderer;
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { askedQuestions, textOf, toolDiffs, toolInput, unifiedDiff, } from "@adroit-app/chat-core";
|
|
2
|
+
import { base64Utf8 } from "./markdown/picture.js";
|
|
3
|
+
import { fileName, languageOfPath, numberedLines, readBlocks, unfenced } from "./read.js";
|
|
4
|
+
export function viewOf(call, ask = null) {
|
|
5
|
+
const texts = call.content.flatMap((c) => c.type === "content" ? [blockText(c.content)] : []);
|
|
6
|
+
const raw = call.rawOutput;
|
|
7
|
+
const outputText = texts.length > 0
|
|
8
|
+
? texts.join("\n")
|
|
9
|
+
: typeof raw === "string"
|
|
10
|
+
? raw
|
|
11
|
+
: raw === undefined || raw === null
|
|
12
|
+
? null
|
|
13
|
+
: JSON.stringify(raw);
|
|
14
|
+
return {
|
|
15
|
+
call,
|
|
16
|
+
inputText: toolInput(call)?.text ?? null,
|
|
17
|
+
outputText: outputText?.trim() ? outputText : null,
|
|
18
|
+
ask,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `renderers` as one: each part is drawn as the first of them that chooses
|
|
23
|
+
* for it.
|
|
24
|
+
*/
|
|
25
|
+
export function composeToolRenderers(...renderers) {
|
|
26
|
+
return (view) => {
|
|
27
|
+
const chosen = {};
|
|
28
|
+
for (const renderer of renderers) {
|
|
29
|
+
if (chosen.input && chosen.output)
|
|
30
|
+
break;
|
|
31
|
+
const r = renderer(view);
|
|
32
|
+
if (!r)
|
|
33
|
+
continue;
|
|
34
|
+
chosen.input ??= r.input;
|
|
35
|
+
chosen.output ??= r.output;
|
|
36
|
+
}
|
|
37
|
+
return chosen;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* How `call`'s input and output are drawn: as `renderer` chooses, and as
|
|
42
|
+
* `defaultToolRenderer` does for what it leaves out.
|
|
43
|
+
*/
|
|
44
|
+
export function drawnParts(call, renderer, ask = null) {
|
|
45
|
+
const view = viewOf(call, ask);
|
|
46
|
+
const chosen = composeToolRenderers(renderer ?? defaultToolRenderer, defaultToolRenderer)(view);
|
|
47
|
+
return {
|
|
48
|
+
input: settle(chosen?.input, view.inputText),
|
|
49
|
+
output: settle(chosen?.output, view.outputText),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function settle(rendering, own) {
|
|
53
|
+
switch (rendering?.as) {
|
|
54
|
+
case "element":
|
|
55
|
+
case "answers":
|
|
56
|
+
return rendering;
|
|
57
|
+
case "none":
|
|
58
|
+
return null;
|
|
59
|
+
case "fields":
|
|
60
|
+
return rendering.fields.length > 0 ? { as: "fields", fields: rendering.fields } : null;
|
|
61
|
+
case "group": {
|
|
62
|
+
const parts = rendering.parts.flatMap((p) => settle(p, own) ?? []);
|
|
63
|
+
return parts.length > 0 ? { as: "group", parts } : null;
|
|
64
|
+
}
|
|
65
|
+
case "views": {
|
|
66
|
+
const views = rendering.views.flatMap((v) => {
|
|
67
|
+
const drawn = settle(v.rendering, own);
|
|
68
|
+
return drawn ? [{ label: v.label, drawn }] : [];
|
|
69
|
+
});
|
|
70
|
+
if (views.length === 0)
|
|
71
|
+
return null;
|
|
72
|
+
return views.length === 1 ? views[0].drawn : { as: "views", views };
|
|
73
|
+
}
|
|
74
|
+
case "images":
|
|
75
|
+
return rendering.images.length > 0 ? rendering : null;
|
|
76
|
+
}
|
|
77
|
+
const text = rendering?.text ?? own;
|
|
78
|
+
if (text === null)
|
|
79
|
+
return null;
|
|
80
|
+
switch (rendering?.as) {
|
|
81
|
+
case undefined:
|
|
82
|
+
case "plain":
|
|
83
|
+
return { as: "code", language: "plaintext", text };
|
|
84
|
+
case "code":
|
|
85
|
+
return { as: "code", language: rendering.language, text };
|
|
86
|
+
case "diff":
|
|
87
|
+
return { as: "code", language: "diff", text };
|
|
88
|
+
case "markdown":
|
|
89
|
+
return { as: "markdown", text, options: rendering.options ?? {} };
|
|
90
|
+
case "json":
|
|
91
|
+
try {
|
|
92
|
+
return { as: "code", language: "json", text: JSON.stringify(JSON.parse(text), null, 2) };
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return { as: "code", language: "plaintext", text };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function inputOf(call) {
|
|
100
|
+
const raw = call.rawInput;
|
|
101
|
+
return raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw : null;
|
|
102
|
+
}
|
|
103
|
+
function flat(value) {
|
|
104
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
105
|
+
}
|
|
106
|
+
function string(input, ...keys) {
|
|
107
|
+
for (const key of keys) {
|
|
108
|
+
const value = input?.[key];
|
|
109
|
+
if (typeof value === "string" && value !== "")
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
function count(input, key) {
|
|
115
|
+
const value = input?.[key];
|
|
116
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
117
|
+
}
|
|
118
|
+
function field(label, value, as) {
|
|
119
|
+
return as ? { label, value, as } : { label, value };
|
|
120
|
+
}
|
|
121
|
+
/** The fields `input` has besides `used`, and a JSON part for what is not flat. */
|
|
122
|
+
function rest(input, used) {
|
|
123
|
+
if (!input)
|
|
124
|
+
return [];
|
|
125
|
+
const fields = [];
|
|
126
|
+
const nested = {};
|
|
127
|
+
for (const [key, value] of Object.entries(input)) {
|
|
128
|
+
if (used.includes(key) || value === undefined || value === null)
|
|
129
|
+
continue;
|
|
130
|
+
if (flat(value))
|
|
131
|
+
fields.push(field(key, String(value)));
|
|
132
|
+
else
|
|
133
|
+
nested[key] = value;
|
|
134
|
+
}
|
|
135
|
+
const parts = [];
|
|
136
|
+
if (fields.length > 0)
|
|
137
|
+
parts.push({ as: "fields", fields });
|
|
138
|
+
if (Object.keys(nested).length > 0) {
|
|
139
|
+
parts.push({ as: "code", language: "json", text: JSON.stringify(nested, null, 2) });
|
|
140
|
+
}
|
|
141
|
+
return parts;
|
|
142
|
+
}
|
|
143
|
+
/** `parts` as one rendering: fields side by side are joined, a single part stands alone. */
|
|
144
|
+
function together(...parts) {
|
|
145
|
+
const joined = [];
|
|
146
|
+
for (const part of parts) {
|
|
147
|
+
const last = joined[joined.length - 1];
|
|
148
|
+
if (part.as === "fields" && last?.as === "fields") {
|
|
149
|
+
joined[joined.length - 1] = { as: "fields", fields: [...last.fields, ...part.fields] };
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
joined.push(part);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (joined.length === 0)
|
|
156
|
+
return { as: "none" };
|
|
157
|
+
return joined.length === 1 ? joined[0] : { as: "group", parts: joined };
|
|
158
|
+
}
|
|
159
|
+
function at(path, line) {
|
|
160
|
+
return line != null ? `${path}:${line}` : path;
|
|
161
|
+
}
|
|
162
|
+
/** Questions asked of the user: each with its options, the chosen marked, and what was written. */
|
|
163
|
+
const questionRenderer = ({ call, ask }) => {
|
|
164
|
+
const asked = askedQuestions(call, ask);
|
|
165
|
+
if (!asked)
|
|
166
|
+
return undefined;
|
|
167
|
+
return { input: { as: "answers", ...asked }, output: { as: "none" } };
|
|
168
|
+
};
|
|
169
|
+
/** A command, whatever kind its call is, as shell. */
|
|
170
|
+
const commandRenderer = ({ call }) => {
|
|
171
|
+
const block = toolInput(call);
|
|
172
|
+
return block?.language === "sh"
|
|
173
|
+
? { input: { as: "code", language: "sh", text: block.text } }
|
|
174
|
+
: undefined;
|
|
175
|
+
};
|
|
176
|
+
const EDIT_KEYS = ["file_path", "notebook_path", "old_string", "new_string", "edits", "content"];
|
|
177
|
+
/** An edit or a write: its file and its change, which ACP diff content shows when there is one. */
|
|
178
|
+
const editRenderer = ({ call }) => {
|
|
179
|
+
const input = inputOf(call);
|
|
180
|
+
const edits = input && ("old_string" in input || "new_string" in input || "edits" in input);
|
|
181
|
+
if (call.toolKind !== "edit" && !edits)
|
|
182
|
+
return undefined;
|
|
183
|
+
if (toolDiffs(call).length > 0)
|
|
184
|
+
return { input: { as: "none" } };
|
|
185
|
+
const path = string(input, "file_path", "notebook_path", "path") ?? call.locations[0]?.path;
|
|
186
|
+
const changes = Array.isArray(input?.edits) ? input.edits : input ? [input] : [];
|
|
187
|
+
const diff = changes
|
|
188
|
+
.flatMap((change) => {
|
|
189
|
+
const after = string(change, "new_string") ?? (change.new_string === "" ? "" : null);
|
|
190
|
+
if (after !== null)
|
|
191
|
+
return [unifiedDiff(string(change, "old_string") ?? "", after)];
|
|
192
|
+
const written = change.content;
|
|
193
|
+
return typeof written === "string" ? [unifiedDiff(null, written)] : [];
|
|
194
|
+
})
|
|
195
|
+
.filter((d) => d !== "")
|
|
196
|
+
.join("\n");
|
|
197
|
+
return {
|
|
198
|
+
input: together(...(path ? [fieldsOf([field("file", at(path, call.locations[0]?.line), "path")])] : []), ...rest(input, [...EDIT_KEYS, "path"]), ...(diff ? [{ as: "diff", text: diff }] : [])),
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
function fieldsOf(fields) {
|
|
202
|
+
return { as: "fields", fields };
|
|
203
|
+
}
|
|
204
|
+
/** A read: its file, at the line it starts from, and how many lines. */
|
|
205
|
+
const readRenderer = ({ call }) => {
|
|
206
|
+
if (call.toolKind !== "read")
|
|
207
|
+
return undefined;
|
|
208
|
+
const input = inputOf(call);
|
|
209
|
+
const location = call.locations[0];
|
|
210
|
+
const path = string(input, "file_path", "notebook_path", "path") ?? location?.path;
|
|
211
|
+
const offset = count(input, "offset");
|
|
212
|
+
const limit = count(input, "limit");
|
|
213
|
+
const fields = [];
|
|
214
|
+
if (path)
|
|
215
|
+
fields.push(field("file", at(path, offset ?? location?.line), "path"));
|
|
216
|
+
if (offset !== null && limit !== null)
|
|
217
|
+
fields.push(field("lines", `${offset}–${offset + limit - 1}`));
|
|
218
|
+
else if (limit !== null)
|
|
219
|
+
fields.push(field("lines", `1–${limit}`));
|
|
220
|
+
return {
|
|
221
|
+
input: together(fieldsOf(fields), ...rest(input, ["file_path", "notebook_path", "path", "offset", "limit"])),
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* What a read gave back, drawn as its file: an image as the image, SVG as
|
|
226
|
+
* an image beside its code, Markdown rendered beside its code, and other
|
|
227
|
+
* text as code in the language its extension names, without the line
|
|
228
|
+
* numbers the tool put before each line.
|
|
229
|
+
*
|
|
230
|
+
* A read of part of a file, by its offset or limit or by lines numbered from
|
|
231
|
+
* past the first, says which lines it holds, as their numbers say. Its
|
|
232
|
+
* Markdown opens on its code and its SVG is only code, since a fragment
|
|
233
|
+
* rendered looks like the whole. One whose lines are not numbered cannot
|
|
234
|
+
* say which lines it holds, so it is drawn as any output is, as is a read
|
|
235
|
+
* that failed or whose file is not in what it gave back.
|
|
236
|
+
*/
|
|
237
|
+
const readOutputRenderer = ({ call }) => {
|
|
238
|
+
if (call.toolKind !== "read" || call.status === "failed")
|
|
239
|
+
return undefined;
|
|
240
|
+
const input = inputOf(call);
|
|
241
|
+
const path = string(input, "file_path", "notebook_path", "path") ?? call.locations[0]?.path;
|
|
242
|
+
if (!path)
|
|
243
|
+
return undefined;
|
|
244
|
+
const name = fileName(path);
|
|
245
|
+
const blocks = readBlocks(call.rawOutput);
|
|
246
|
+
if (blocks.some((b) => b.type === "image")) {
|
|
247
|
+
// Text beside an image, such as a page's caption or a warning, stays where it was said.
|
|
248
|
+
const parts = [];
|
|
249
|
+
for (const block of blocks) {
|
|
250
|
+
const last = parts[parts.length - 1];
|
|
251
|
+
if (block.type === "text")
|
|
252
|
+
parts.push({ as: "plain", text: block.text });
|
|
253
|
+
else {
|
|
254
|
+
const image = { data: block.data, mimeType: block.mimeType, name, path };
|
|
255
|
+
if (last?.as === "images")
|
|
256
|
+
last.images.push(image);
|
|
257
|
+
else
|
|
258
|
+
parts.push({ as: "images", images: [image] });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return { output: parts.length === 1 ? parts[0] : { as: "group", parts } };
|
|
262
|
+
}
|
|
263
|
+
const read = readText(call);
|
|
264
|
+
if (read === null)
|
|
265
|
+
return undefined;
|
|
266
|
+
const numbered = numberedLines(read);
|
|
267
|
+
const partial = count(input, "offset") !== null || count(input, "limit") !== null || (numbered?.first ?? 1) > 1;
|
|
268
|
+
if (partial && !numbered)
|
|
269
|
+
return undefined;
|
|
270
|
+
const text = numbered?.text ?? read;
|
|
271
|
+
const language = languageOfPath(path);
|
|
272
|
+
const file = (() => {
|
|
273
|
+
const code = { as: "code", language: language ?? "plaintext", text };
|
|
274
|
+
if (language === "markdown") {
|
|
275
|
+
const views = [
|
|
276
|
+
{ label: "Rendered", rendering: { as: "markdown", text } },
|
|
277
|
+
{ label: "Code", rendering: code },
|
|
278
|
+
];
|
|
279
|
+
return { as: "views", views: partial ? views.reverse() : views };
|
|
280
|
+
}
|
|
281
|
+
if (!partial && /\.svg$/i.test(path) && /<svg[\s>]/i.test(text)) {
|
|
282
|
+
const image = { data: base64Utf8(text), mimeType: "image/svg+xml", name, path };
|
|
283
|
+
return {
|
|
284
|
+
as: "views",
|
|
285
|
+
views: [
|
|
286
|
+
{ label: "Image", rendering: { as: "images", images: [image] } },
|
|
287
|
+
{ label: "Code", rendering: code },
|
|
288
|
+
],
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return code;
|
|
292
|
+
})();
|
|
293
|
+
if (!partial || !numbered)
|
|
294
|
+
return { output: file };
|
|
295
|
+
const last = numbered.first + text.split("\n").length - 1;
|
|
296
|
+
const lines = last === numbered.first ? `line ${last}` : `lines ${numbered.first}–${last}`;
|
|
297
|
+
return {
|
|
298
|
+
output: {
|
|
299
|
+
as: "group",
|
|
300
|
+
parts: [fieldsOf([field("read", `${lines}, part of the file`)]), file],
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* The text a read gave back: its raw output when that is text, as the
|
|
306
|
+
* agent's tool gave it, or else its one text content when that is a
|
|
307
|
+
* single fenced block, as an adapter that fences a file's text sends it.
|
|
308
|
+
*/
|
|
309
|
+
function readText(call) {
|
|
310
|
+
const raw = call.rawOutput;
|
|
311
|
+
if (typeof raw === "string")
|
|
312
|
+
return raw;
|
|
313
|
+
if (Array.isArray(raw) && raw.length > 0) {
|
|
314
|
+
const texts = raw.map((b) => b !== null && typeof b === "object" && b.type === "text"
|
|
315
|
+
? b.text
|
|
316
|
+
: null);
|
|
317
|
+
if (texts.every((t) => typeof t === "string"))
|
|
318
|
+
return texts.join("\n");
|
|
319
|
+
}
|
|
320
|
+
const blocks = call.content.flatMap((c) => c.type === "content" ? [c.content] : []);
|
|
321
|
+
const only = blocks.length === 1 ? blocks[0] : null;
|
|
322
|
+
return only?.type === "text" ? unfenced(textOf([only])) : null;
|
|
323
|
+
}
|
|
324
|
+
/** A search: its pattern, where it looks, and its options. */
|
|
325
|
+
const searchRenderer = ({ call }) => {
|
|
326
|
+
if (call.toolKind !== "search")
|
|
327
|
+
return undefined;
|
|
328
|
+
const input = inputOf(call);
|
|
329
|
+
if (!input)
|
|
330
|
+
return undefined;
|
|
331
|
+
const fields = [];
|
|
332
|
+
const pattern = string(input, "pattern");
|
|
333
|
+
const query = string(input, "query");
|
|
334
|
+
const path = string(input, "path");
|
|
335
|
+
const glob = string(input, "glob");
|
|
336
|
+
if (pattern)
|
|
337
|
+
fields.push(field("pattern", pattern, "code"));
|
|
338
|
+
if (query)
|
|
339
|
+
fields.push(field("query", query));
|
|
340
|
+
if (path)
|
|
341
|
+
fields.push(field("path", path, "path"));
|
|
342
|
+
if (glob)
|
|
343
|
+
fields.push(field("glob", glob, "code"));
|
|
344
|
+
return {
|
|
345
|
+
input: together(fieldsOf(fields), ...rest(input, ["pattern", "query", "path", "glob"])),
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
/** A fetch: its URL, and the prompt or query it was given. */
|
|
349
|
+
const fetchRenderer = ({ call }) => {
|
|
350
|
+
if (call.toolKind !== "fetch")
|
|
351
|
+
return undefined;
|
|
352
|
+
const input = inputOf(call);
|
|
353
|
+
if (!input)
|
|
354
|
+
return undefined;
|
|
355
|
+
const fields = [];
|
|
356
|
+
const url = string(input, "url");
|
|
357
|
+
if (url)
|
|
358
|
+
fields.push(field("url", url, "url"));
|
|
359
|
+
return { input: together(fieldsOf(fields), ...rest(input, ["url"])) };
|
|
360
|
+
};
|
|
361
|
+
/** Thinking and planning: what it was told, as text. */
|
|
362
|
+
const thinkRenderer = ({ call }) => {
|
|
363
|
+
if (call.toolKind !== "think" && call.toolKind !== "switch_mode")
|
|
364
|
+
return undefined;
|
|
365
|
+
const input = inputOf(call);
|
|
366
|
+
if (!input)
|
|
367
|
+
return undefined;
|
|
368
|
+
const texts = [];
|
|
369
|
+
for (const key of ["prompt", "thought", "text"]) {
|
|
370
|
+
const text = string(input, key);
|
|
371
|
+
if (text)
|
|
372
|
+
texts.push({ as: "plain", text });
|
|
373
|
+
}
|
|
374
|
+
const plan = string(input, "plan");
|
|
375
|
+
if (plan)
|
|
376
|
+
texts.push({ as: "markdown", text: plan });
|
|
377
|
+
const todos = Array.isArray(input.todos) ? input.todos : null;
|
|
378
|
+
if (todos) {
|
|
379
|
+
const marks = { completed: "●", in_progress: "◐" };
|
|
380
|
+
const lines = todos.map((t) => `${marks[String(t.status)] ?? "○"} ${String(t.content ?? "")}`);
|
|
381
|
+
texts.push({ as: "plain", text: lines.join("\n") });
|
|
382
|
+
}
|
|
383
|
+
return {
|
|
384
|
+
input: together(...rest(input, ["prompt", "thought", "text", "plan", "todos"]), ...texts),
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
/** A move: where from and where to. */
|
|
388
|
+
const moveRenderer = ({ call }) => {
|
|
389
|
+
if (call.toolKind !== "move")
|
|
390
|
+
return undefined;
|
|
391
|
+
const input = inputOf(call);
|
|
392
|
+
const from = string(input, ...MOVE_FROM) ?? call.locations[0]?.path;
|
|
393
|
+
const to = string(input, ...MOVE_TO) ?? call.locations[1]?.path;
|
|
394
|
+
if (!from || !to)
|
|
395
|
+
return undefined;
|
|
396
|
+
return {
|
|
397
|
+
input: together(fieldsOf([field("from", from, "path"), field("to", to, "path")]), ...rest(input, [...MOVE_FROM, ...MOVE_TO])),
|
|
398
|
+
};
|
|
399
|
+
};
|
|
400
|
+
const MOVE_FROM = ["source", "from", "old_path", "source_path"];
|
|
401
|
+
const MOVE_TO = ["destination", "to", "new_path", "destination_path"];
|
|
402
|
+
/** A delete: the path it removes. */
|
|
403
|
+
const deleteRenderer = ({ call }) => {
|
|
404
|
+
if (call.toolKind !== "delete")
|
|
405
|
+
return undefined;
|
|
406
|
+
const input = inputOf(call);
|
|
407
|
+
const path = string(input, "path", "file_path") ?? call.locations[0]?.path;
|
|
408
|
+
if (!path)
|
|
409
|
+
return undefined;
|
|
410
|
+
return {
|
|
411
|
+
input: together(fieldsOf([field("path", path, "path")]), ...rest(input, ["path", "file_path"])),
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
/** Any other input: flat as labelled rows, nested as JSON, text as it is. */
|
|
415
|
+
const otherInputRenderer = ({ call }) => {
|
|
416
|
+
const raw = call.rawInput;
|
|
417
|
+
if (raw === undefined || raw === null)
|
|
418
|
+
return { input: { as: "none" } };
|
|
419
|
+
if (typeof raw === "string")
|
|
420
|
+
return { input: { as: "plain", text: raw } };
|
|
421
|
+
const input = inputOf(call);
|
|
422
|
+
if (!input)
|
|
423
|
+
return { input: { as: "json" } };
|
|
424
|
+
const values = Object.values(input).filter((v) => v !== undefined && v !== null);
|
|
425
|
+
if (values.length === 0)
|
|
426
|
+
return { input: { as: "none" } };
|
|
427
|
+
if (values.every(flat))
|
|
428
|
+
return { input: together(...rest(input, [])) };
|
|
429
|
+
return { input: { as: "code", language: "json", text: JSON.stringify(input, null, 2) } };
|
|
430
|
+
};
|
|
431
|
+
/** Output as JSON when it is a JSON object or list, and plain otherwise: never Markdown. */
|
|
432
|
+
const outputRenderer = ({ call, outputText }) => {
|
|
433
|
+
if (outputText === null)
|
|
434
|
+
return { output: { as: "none" } };
|
|
435
|
+
// ACP: text in a text content block is CommonMark wherever it appears,
|
|
436
|
+
// tool calls included; an embedded text resource is Markdown only by type.
|
|
437
|
+
const blocks = call.content.flatMap((c) => c.type === "content" ? [c.content] : []);
|
|
438
|
+
if (blocks.length > 0) {
|
|
439
|
+
const markdown = blocks.every((b) => b.type === "text" || (b.type === "resource" && isMarkdown(resourceOf(b)?.mimeType)));
|
|
440
|
+
return { output: markdown ? { as: "markdown" } : { as: "plain" } };
|
|
441
|
+
}
|
|
442
|
+
try {
|
|
443
|
+
const value = JSON.parse(outputText.trim());
|
|
444
|
+
if (value !== null && typeof value === "object")
|
|
445
|
+
return { output: { as: "json" } };
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
// Not JSON: shown as the tool gave it.
|
|
449
|
+
}
|
|
450
|
+
return { output: { as: "plain" } };
|
|
451
|
+
};
|
|
452
|
+
/** The default renderers, one per kind of call, which a host can compose with its own. */
|
|
453
|
+
export const toolRenderers = {
|
|
454
|
+
question: questionRenderer,
|
|
455
|
+
command: commandRenderer,
|
|
456
|
+
edit: editRenderer,
|
|
457
|
+
read: readRenderer,
|
|
458
|
+
search: searchRenderer,
|
|
459
|
+
fetch: fetchRenderer,
|
|
460
|
+
think: thinkRenderer,
|
|
461
|
+
move: moveRenderer,
|
|
462
|
+
delete: deleteRenderer,
|
|
463
|
+
otherInput: otherInputRenderer,
|
|
464
|
+
readOutput: readOutputRenderer,
|
|
465
|
+
output: outputRenderer,
|
|
466
|
+
};
|
|
467
|
+
/**
|
|
468
|
+
* How the Chat draws a call when its host does not choose: by the call's
|
|
469
|
+
* ACP kind and what its input carries. A command is shell; an edit is its
|
|
470
|
+
* file and its change; a read, a search, a fetch, a move or a delete are
|
|
471
|
+
* their paths, patterns and URLs as labelled rows; thinking is its text.
|
|
472
|
+
* Other input is labelled rows when flat and JSON when not; output is JSON
|
|
473
|
+
* when it parses and plain otherwise.
|
|
474
|
+
*/
|
|
475
|
+
export const defaultToolRenderer = composeToolRenderers(...Object.values(toolRenderers));
|
|
476
|
+
function resourceOf(block) {
|
|
477
|
+
return block.type === "resource"
|
|
478
|
+
? (block.resource ?? null)
|
|
479
|
+
: null;
|
|
480
|
+
}
|
|
481
|
+
function isMarkdown(mimeType) {
|
|
482
|
+
return mimeType === "text/markdown" || mimeType === "text/x-markdown";
|
|
483
|
+
}
|
|
484
|
+
/** A content block's text: its own, or an embedded text resource's. */
|
|
485
|
+
function blockText(block) {
|
|
486
|
+
return block.type === "resource" ? (resourceOf(block)?.text ?? "") : textOf([block]);
|
|
487
|
+
}
|
package/dist/view.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** How an image is shown in a viewer: its scale and where its top-left corner is. */
|
|
2
|
+
export interface View {
|
|
3
|
+
scale: number;
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
interface Size {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
interface Point {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
/** The scales zooming stays between. */
|
|
16
|
+
export declare const SCALES: {
|
|
17
|
+
readonly min: 0.1;
|
|
18
|
+
readonly max: 32;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The scale that shows `image` whole in `box`, `margin` clear of each
|
|
22
|
+
* edge, never larger than its own size.
|
|
23
|
+
*/
|
|
24
|
+
export declare function fitScale(image: Size, box: Size, margin?: number): number;
|
|
25
|
+
/** How much a wheel turned by `deltaY`, counted in `deltaMode`, zooms. */
|
|
26
|
+
export declare function wheelFactor(deltaY: number, deltaMode: number): number;
|
|
27
|
+
/** `view` zoomed by `factor`, keeping the image point under `at` where it is. */
|
|
28
|
+
export declare function zoomedAt(view: View, factor: number, at: Point): View;
|
|
29
|
+
/** `view` moved by `by`, as a drag moves it. */
|
|
30
|
+
export declare function panned(view: View, by: Point): View;
|
|
31
|
+
/** `view` with `image` at `scale`, centred in `box`. */
|
|
32
|
+
export declare function centred(image: Size, box: Size, scale: number): View;
|
|
33
|
+
export {};
|