@textui/chat 0.6.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 +76 -0
- package/dist/blocks.d.ts +75 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +50 -0
- package/dist/bubble.d.ts +122 -0
- package/dist/bubble.d.ts.map +1 -0
- package/dist/bubble.js +108 -0
- package/dist/composer.d.ts +67 -0
- package/dist/composer.d.ts.map +1 -0
- package/dist/composer.js +194 -0
- package/dist/controls.d.ts +66 -0
- package/dist/controls.d.ts.map +1 -0
- package/dist/controls.js +77 -0
- package/dist/details.d.ts +66 -0
- package/dist/details.d.ts.map +1 -0
- package/dist/details.js +65 -0
- package/dist/diff.d.ts +45 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +111 -0
- package/dist/filediff.d.ts +30 -0
- package/dist/filediff.d.ts.map +1 -0
- package/dist/filediff.js +24 -0
- package/dist/hitl.d.ts +85 -0
- package/dist/hitl.d.ts.map +1 -0
- package/dist/hitl.js +134 -0
- package/dist/icons.d.ts +14 -0
- package/dist/icons.d.ts.map +1 -0
- package/dist/icons.js +71 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/measure.d.ts +14 -0
- package/dist/measure.d.ts.map +1 -0
- package/dist/measure.js +19 -0
- package/dist/picker.d.ts +43 -0
- package/dist/picker.d.ts.map +1 -0
- package/dist/picker.js +79 -0
- package/dist/sessionhead.d.ts +42 -0
- package/dist/sessionhead.d.ts.map +1 -0
- package/dist/sessionhead.js +58 -0
- package/dist/sessions.d.ts +35 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +58 -0
- package/dist/toolcall.d.ts +28 -0
- package/dist/toolcall.d.ts.map +1 -0
- package/dist/toolcall.js +54 -0
- package/dist/transcript.d.ts +53 -0
- package/dist/transcript.d.ts.map +1 -0
- package/dist/transcript.js +67 -0
- package/dist/types.d.ts +176 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/package.json +58 -0
- package/src/blocks.ts +73 -0
- package/src/bubble.tsx +266 -0
- package/src/composer.tsx +302 -0
- package/src/controls.tsx +222 -0
- package/src/details.tsx +162 -0
- package/src/diff.ts +132 -0
- package/src/filediff.tsx +118 -0
- package/src/hitl.tsx +392 -0
- package/src/icons.ts +109 -0
- package/src/index.ts +16 -0
- package/src/measure.ts +21 -0
- package/src/picker.ts +105 -0
- package/src/sessionhead.tsx +105 -0
- package/src/sessions.tsx +146 -0
- package/src/toolcall.tsx +136 -0
- package/src/transcript.tsx +221 -0
- package/src/types.ts +171 -0
package/src/hitl.tsx
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import type { BoxProps, Rect, RenderOutput } from '@textui/core';
|
|
2
|
+
import {
|
|
3
|
+
defineComponent, useApp, useFocusScope, useInput, useTheme,
|
|
4
|
+
} from '@textui/core';
|
|
5
|
+
import {
|
|
6
|
+
Button,
|
|
7
|
+
Checkbox,
|
|
8
|
+
Column,
|
|
9
|
+
MarkdownView,
|
|
10
|
+
Panel,
|
|
11
|
+
RadioGroup,
|
|
12
|
+
Row,
|
|
13
|
+
TextArea,
|
|
14
|
+
TextInput,
|
|
15
|
+
} from '@textui/widgets';
|
|
16
|
+
import type { ChatAnswer, ChatPendingInput, ChatQuestion, ChatSendStatus } from './types.js';
|
|
17
|
+
import { useReportMeasure } from './measure.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The block that means the agent is stopped, waiting on a person.
|
|
21
|
+
*
|
|
22
|
+
* It is the only thing on the screen that is waiting on the reader, so it
|
|
23
|
+
* takes the focus, traps it, and is answerable without leaving the keyboard.
|
|
24
|
+
* Rendered as one more bubble in the transcript it scrolls away, and a blocked
|
|
25
|
+
* agent that has scrolled off is a session that looks merely slow.
|
|
26
|
+
*
|
|
27
|
+
* Two kinds, and they are nothing alike:
|
|
28
|
+
*
|
|
29
|
+
* a **tool confirmation** is a yes or a no about a command, with named
|
|
30
|
+
* options where the host offers them;
|
|
31
|
+
*
|
|
32
|
+
* a **question** carries no tool call at all. Its prose is the request's own
|
|
33
|
+
* message and what is being asked is its questions. Rendering it as a
|
|
34
|
+
* confirmation loses the entire request - the choices vanish, the question
|
|
35
|
+
* is never shown, and what is left is a heading and an Approve button.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export interface ChatHitlProps extends BoxProps {
|
|
39
|
+
input: ChatPendingInput;
|
|
40
|
+
/**
|
|
41
|
+
* The answers so far, keyed by question id.
|
|
42
|
+
*
|
|
43
|
+
* Owned by whoever mounts this rather than by the block: a host may have an
|
|
44
|
+
* action for a draft answer precisely because another client can be looking
|
|
45
|
+
* at the same question, and a value only this box knows is one nobody else
|
|
46
|
+
* can see. Unused by a tool confirmation.
|
|
47
|
+
*/
|
|
48
|
+
draft?: Record<string, ChatAnswer>;
|
|
49
|
+
onDraft?(next: Record<string, ChatAnswer>): void;
|
|
50
|
+
onApprove(optionId?: string): void;
|
|
51
|
+
onDeny(): void;
|
|
52
|
+
onAnswer(answers: Record<string, ChatAnswer>, accepted: boolean): void;
|
|
53
|
+
/** Leave it up, but give the keyboard back. */
|
|
54
|
+
onEscape?(): void;
|
|
55
|
+
/** Where the block is on screen whenever that changes, and `null` once it is gone. */
|
|
56
|
+
onMeasure?(rect: Rect | null): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const ChatHitl: (props: ChatHitlProps) => RenderOutput =
|
|
60
|
+
defineComponent<ChatHitlProps>('ChatHitl', (props) => {
|
|
61
|
+
// It sits above the composer rather than inside it, so it says where it
|
|
62
|
+
// starts: whoever keeps clear of the composer has to keep clear of this.
|
|
63
|
+
const { input, draft, onDraft, onApprove, onDeny, onAnswer, onEscape, onMeasure, ...rest } = props;
|
|
64
|
+
useReportMeasure(onMeasure);
|
|
65
|
+
const theme = useTheme();
|
|
66
|
+
// Focused on arrival, and not trapped.
|
|
67
|
+
//
|
|
68
|
+
// Trapping read well - the answer is the only thing to do - and it was
|
|
69
|
+
// wrong. A trap routes every key inside the scope, so while a session was
|
|
70
|
+
// blocked the transcript could not be scrolled, escape could not leave the
|
|
71
|
+
// screen, and the block that said `esc read` was the one thing making that
|
|
72
|
+
// impossible. You approve a command on the strength of what is written
|
|
73
|
+
// above it, so reading has to stay available while it is up.
|
|
74
|
+
//
|
|
75
|
+
// The keys that answer it are global (see `ConfirmRequest`), so they still
|
|
76
|
+
// work from wherever the reader has gone.
|
|
77
|
+
useFocusScope({ id: 'chat.hitl', autoFocus: true, restore: true });
|
|
78
|
+
|
|
79
|
+
// Wrapped, so that what `onMeasure` reports is the box this occupies
|
|
80
|
+
// rather than the room inside its border, which is one row lower.
|
|
81
|
+
return (
|
|
82
|
+
<Column {...rest}>
|
|
83
|
+
<Panel
|
|
84
|
+
title={input.kind === 'toolConfirmation' ? (input.call.confirmationTitle ?? 'The agent asks') : 'The agent asks'}
|
|
85
|
+
tone="warning"
|
|
86
|
+
border={theme.border}
|
|
87
|
+
meta={`${theme.glyphs.warning} waiting on you`}
|
|
88
|
+
>
|
|
89
|
+
{input.kind === 'toolConfirmation'
|
|
90
|
+
? <ConfirmRequest input={input} onApprove={onApprove} onDeny={onDeny} {...(onEscape ? { onEscape } : {})} />
|
|
91
|
+
: (
|
|
92
|
+
<QuestionForm
|
|
93
|
+
input={input}
|
|
94
|
+
draft={draft ?? {}}
|
|
95
|
+
onDraft={onDraft ?? (() => {})}
|
|
96
|
+
onAnswer={onAnswer}
|
|
97
|
+
{...(onEscape ? { onEscape } : {})}
|
|
98
|
+
/>
|
|
99
|
+
)}
|
|
100
|
+
</Panel>
|
|
101
|
+
</Column>
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
/** A yes or a no about a command, and the named options the host offered. */
|
|
106
|
+
export const ConfirmRequest = defineComponent<{
|
|
107
|
+
input: Extract<ChatPendingInput, { kind: 'toolConfirmation' }>;
|
|
108
|
+
onApprove(optionId?: string): void;
|
|
109
|
+
onDeny(): void;
|
|
110
|
+
onEscape?(): void;
|
|
111
|
+
}>('ConfirmRequest', ({ input, onApprove, onDeny, onEscape }) => {
|
|
112
|
+
const theme = useTheme();
|
|
113
|
+
const options = input.call.options ?? [];
|
|
114
|
+
|
|
115
|
+
// The letters are on the block, not on the application: they only exist
|
|
116
|
+
// while it is up, and while it is up nothing else wants them.
|
|
117
|
+
useInput((event) => {
|
|
118
|
+
if (event.name === 'a') { onApprove(); return true; }
|
|
119
|
+
if (event.name === 'd') { onDeny(); return true; }
|
|
120
|
+
if (event.name === 'escape') { onEscape?.(); return true; }
|
|
121
|
+
const digit = Number(event.name);
|
|
122
|
+
if (Number.isInteger(digit) && digit >= 1 && digit <= options.length) {
|
|
123
|
+
onApprove(options[digit - 1]?.id);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}, { global: true });
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<Column gap={1}>
|
|
131
|
+
{input.call.intention ? <MarkdownView content={input.call.intention} quiet /> : null}
|
|
132
|
+
{input.call.input ? (
|
|
133
|
+
<Column bg="surfaceAlt" padding={[0, 1]}>
|
|
134
|
+
<text content={input.call.input} wrap="word" />
|
|
135
|
+
</Column>
|
|
136
|
+
) : null}
|
|
137
|
+
|
|
138
|
+
{/* Numbered, because option ids are opaque - a live host sends whole
|
|
139
|
+
sentences with punctuation in them - so the number is what a person
|
|
140
|
+
can actually answer with, and it is on the control rather than in a
|
|
141
|
+
list above it. */}
|
|
142
|
+
<Row gap={2}>
|
|
143
|
+
<Button label="Approve" tone="success" variant="solid" icon={theme.glyphs.check} hint="a" autoFocus onPress={() => onApprove()} />
|
|
144
|
+
<Button label="Deny" tone="danger" icon={theme.glyphs.cross} hint="d" onPress={onDeny} />
|
|
145
|
+
{options.map((option, i) => (
|
|
146
|
+
<Button key={option.id} label={option.label} variant="ghost" hint={String(i + 1)} onPress={() => onApprove(option.id)} />
|
|
147
|
+
))}
|
|
148
|
+
</Row>
|
|
149
|
+
</Column>
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* A question, with its questions.
|
|
155
|
+
*
|
|
156
|
+
* Every answer names its own kind, keyed by question id, and a required one
|
|
157
|
+
* left empty is not sendable - accepting with no answers resumes the agent on
|
|
158
|
+
* the answers it already had, which for a question it has just asked is none.
|
|
159
|
+
*/
|
|
160
|
+
export const QuestionForm = defineComponent<{
|
|
161
|
+
input: Extract<ChatPendingInput, { kind: 'chatInput' }>;
|
|
162
|
+
/** The answers so far. Controlled: every change goes out through `onDraft`. */
|
|
163
|
+
draft: Record<string, ChatAnswer>;
|
|
164
|
+
onDraft(next: Record<string, ChatAnswer>): void;
|
|
165
|
+
onAnswer(answers: Record<string, ChatAnswer>, accepted: boolean): void;
|
|
166
|
+
onEscape?(): void;
|
|
167
|
+
}>('QuestionForm', ({ input, draft, onDraft, onAnswer, onEscape }) => {
|
|
168
|
+
const theme = useTheme();
|
|
169
|
+
const app = useApp();
|
|
170
|
+
const answers = draft;
|
|
171
|
+
|
|
172
|
+
const set = (id: string, answer: ChatAnswer | null): void => {
|
|
173
|
+
const next = { ...answers };
|
|
174
|
+
if (answer === null) delete next[id];
|
|
175
|
+
else next[id] = answer;
|
|
176
|
+
onDraft(next);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const missing = input.questions.filter((question) => question.required && !answers[question.id]);
|
|
180
|
+
const send = (): void => { if (missing.length === 0) onAnswer(answers, true); };
|
|
181
|
+
|
|
182
|
+
useInput((event) => {
|
|
183
|
+
if (event.name === 'escape') { onEscape?.(); return true; }
|
|
184
|
+
// Enter sends, which is what the hint row under this says while a question
|
|
185
|
+
// is up - but only from inside the block. A reader who has deliberately
|
|
186
|
+
// tabbed down to the composer to queue a message is pressing enter at the
|
|
187
|
+
// composer, and answering the question with it would be the same mistake
|
|
188
|
+
// in the other direction. The fields that consume enter themselves - a
|
|
189
|
+
// text input, a checkbox, the Send button - never reach this.
|
|
190
|
+
if (event.name === 'enter') {
|
|
191
|
+
const at = app.focus.focused();
|
|
192
|
+
if (at === null || app.focus.scopeOf(at) !== 'chat.hitl') return false;
|
|
193
|
+
send();
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}, { global: true });
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<Column gap={1}>
|
|
201
|
+
<MarkdownView content={input.message} />
|
|
202
|
+
{input.questions.map((question, i) => (
|
|
203
|
+
<QuestionField
|
|
204
|
+
key={question.id}
|
|
205
|
+
question={question}
|
|
206
|
+
answer={answers[question.id] ?? null}
|
|
207
|
+
// The first field takes the keyboard, and the scope asking for it is
|
|
208
|
+
// not enough: a scope's `autoFocus` only claims focus when nothing
|
|
209
|
+
// holds any, and while a question is up the composer holds it. So a
|
|
210
|
+
// question rendered its field, put `required` beside it, disabled
|
|
211
|
+
// Send until it was answered - and every key typed at it went into
|
|
212
|
+
// the composer behind. A control that says it wants focus is the one
|
|
213
|
+
// thing that takes it off another scope, which is why the
|
|
214
|
+
// confirmation's Approve button works and this did not.
|
|
215
|
+
autoFocus={i === 0}
|
|
216
|
+
onChange={(answer) => set(question.id, answer)}
|
|
217
|
+
onSubmit={send}
|
|
218
|
+
/>
|
|
219
|
+
))}
|
|
220
|
+
|
|
221
|
+
<Row gap={2}>
|
|
222
|
+
<Button
|
|
223
|
+
label="Send"
|
|
224
|
+
tone="success"
|
|
225
|
+
variant="solid"
|
|
226
|
+
icon={theme.glyphs.check}
|
|
227
|
+
hint="enter"
|
|
228
|
+
disabled={missing.length > 0}
|
|
229
|
+
onPress={() => onAnswer(answers, true)}
|
|
230
|
+
/>
|
|
231
|
+
<Button label="Decline" variant="ghost" icon={theme.glyphs.cross} onPress={() => onAnswer({}, false)} />
|
|
232
|
+
{missing.length > 0 ? (
|
|
233
|
+
<text content={`${theme.glyphs.warning} ${missing.length} still to answer`} fg="warning" />
|
|
234
|
+
) : null}
|
|
235
|
+
</Row>
|
|
236
|
+
</Column>
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
/** One question, rendered by its kind. The kind decides the answer's shape. */
|
|
241
|
+
const QuestionField = defineComponent<{
|
|
242
|
+
question: ChatQuestion;
|
|
243
|
+
answer: ChatAnswer | null;
|
|
244
|
+
/** Passed to whichever control this kind renders, and to only one of them. */
|
|
245
|
+
autoFocus?: boolean;
|
|
246
|
+
onChange(answer: ChatAnswer | null): void;
|
|
247
|
+
/** Enter, from a field that reads it before anything else does. */
|
|
248
|
+
onSubmit(): void;
|
|
249
|
+
}>('QuestionField', ({ question, answer, autoFocus, onChange, onSubmit }) => {
|
|
250
|
+
const theme = useTheme();
|
|
251
|
+
const options = question.options ?? [];
|
|
252
|
+
const selectedMany = answer?.kind === 'selected-many' ? answer.value : [];
|
|
253
|
+
|
|
254
|
+
return (
|
|
255
|
+
<Column gap={0}>
|
|
256
|
+
<Row gap={1}>
|
|
257
|
+
<text content={question.message} bold wrap="word" flex={1} />
|
|
258
|
+
{question.required ? <text content="required" fg="warning" /> : null}
|
|
259
|
+
</Row>
|
|
260
|
+
|
|
261
|
+
{question.kind === 'boolean' ? (
|
|
262
|
+
<Checkbox
|
|
263
|
+
label="yes"
|
|
264
|
+
autoFocus={autoFocus === true}
|
|
265
|
+
checked={answer?.kind === 'boolean' ? answer.value : false}
|
|
266
|
+
onChange={(checked: boolean) => onChange({ kind: 'boolean', value: checked })}
|
|
267
|
+
/>
|
|
268
|
+
) : null}
|
|
269
|
+
|
|
270
|
+
{question.kind === 'single-select' ? (
|
|
271
|
+
<RadioGroup
|
|
272
|
+
autoFocus={autoFocus === true}
|
|
273
|
+
options={options.map((option, i) => ({ value: option.id, label: `${i + 1}. ${option.label}` }))}
|
|
274
|
+
{...(answer?.kind === 'selected' ? { value: answer.value } : {})}
|
|
275
|
+
onChange={(value: string) => onChange({ kind: 'selected', value })}
|
|
276
|
+
/>
|
|
277
|
+
) : null}
|
|
278
|
+
|
|
279
|
+
{question.kind === 'multi-select' ? (
|
|
280
|
+
<Column>
|
|
281
|
+
{options.map((option, i) => (
|
|
282
|
+
<Checkbox
|
|
283
|
+
key={option.id}
|
|
284
|
+
label={`${i + 1}. ${option.label}`}
|
|
285
|
+
checked={selectedMany.includes(option.id)}
|
|
286
|
+
onChange={(checked: boolean) => onChange({
|
|
287
|
+
kind: 'selected-many',
|
|
288
|
+
value: checked
|
|
289
|
+
? [...selectedMany, option.id]
|
|
290
|
+
: selectedMany.filter((id) => id !== option.id),
|
|
291
|
+
})}
|
|
292
|
+
/>
|
|
293
|
+
))}
|
|
294
|
+
</Column>
|
|
295
|
+
) : null}
|
|
296
|
+
|
|
297
|
+
{/*
|
|
298
|
+
* Prose, in a field that looks like one.
|
|
299
|
+
*
|
|
300
|
+
* A `TextArea` rather than a `TextInput` because what a host asks for
|
|
301
|
+
* in words is answered in words: "which file, and why" is two lines
|
|
302
|
+
* more often than one, and a single-line field silently makes the
|
|
303
|
+
* second one impossible. Enter sends - the hint row under this says so
|
|
304
|
+
* - and `alt+enter` (or `ctrl+enter`) is the newline, which is the
|
|
305
|
+
* composer's bargain in the same screen.
|
|
306
|
+
*
|
|
307
|
+
* The placeholder is not decoration. An empty bordered box with no
|
|
308
|
+
* caret in it is not obviously somewhere you can type, and a question
|
|
309
|
+
* whose field reads as a gap in the layout is a question that looks
|
|
310
|
+
* unanswerable.
|
|
311
|
+
*/}
|
|
312
|
+
{question.kind === 'text' ? (
|
|
313
|
+
<TextArea
|
|
314
|
+
value={answer?.kind === 'text' ? answer.value : ''}
|
|
315
|
+
autoFocus={autoFocus === true}
|
|
316
|
+
maxRows={6}
|
|
317
|
+
border={theme.border}
|
|
318
|
+
placeholder="Type your answer · enter sends · alt+enter for a new line"
|
|
319
|
+
caretTone="accent"
|
|
320
|
+
onSubmit={onSubmit}
|
|
321
|
+
onChange={(value: string) => onChange(value === '' ? null : { kind: 'text', value })}
|
|
322
|
+
/>
|
|
323
|
+
) : null}
|
|
324
|
+
|
|
325
|
+
{question.kind === 'number' || question.kind === 'integer' ? (
|
|
326
|
+
<TextInput
|
|
327
|
+
value={answer && 'value' in answer ? String(answer.value) : ''}
|
|
328
|
+
autoFocus={autoFocus === true}
|
|
329
|
+
label="number"
|
|
330
|
+
hideLabel
|
|
331
|
+
placeholder="A number"
|
|
332
|
+
onSubmit={onSubmit}
|
|
333
|
+
onChange={(value: string) => onChange(
|
|
334
|
+
value === '' ? null : { kind: 'number', value: Number(value) },
|
|
335
|
+
)}
|
|
336
|
+
/>
|
|
337
|
+
) : null}
|
|
338
|
+
|
|
339
|
+
{question.allowFreeformInput && question.kind !== 'text' ? (
|
|
340
|
+
// Freeform answers *instead of* choosing, never as one more choice: a
|
|
341
|
+
// host reading only the selection must not be handed typed words in
|
|
342
|
+
// the field where it expects an option id.
|
|
343
|
+
<Row gap={1}>
|
|
344
|
+
<text content={theme.glyphs.chevronRight} fg="subtle" />
|
|
345
|
+
<TextInput
|
|
346
|
+
value={answer?.kind === 'text' ? answer.value : ''}
|
|
347
|
+
label="or say it in words"
|
|
348
|
+
placeholder="or say it in words"
|
|
349
|
+
hideLabel
|
|
350
|
+
flex={1}
|
|
351
|
+
onSubmit={onSubmit}
|
|
352
|
+
onChange={(value: string) => onChange(value === '' ? null : { kind: 'text', value })}
|
|
353
|
+
/>
|
|
354
|
+
</Row>
|
|
355
|
+
) : null}
|
|
356
|
+
</Column>
|
|
357
|
+
);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* What came of the answer, on the row between the block and the composer.
|
|
362
|
+
*
|
|
363
|
+
* That row was a blank one, and it is where a person is already looking after
|
|
364
|
+
* pressing a button on the block above it. A press that reaches a host which
|
|
365
|
+
* then says nothing is indistinguishable from a key that was never read - so
|
|
366
|
+
* this says the answer has gone, and says so in red when it did not.
|
|
367
|
+
*
|
|
368
|
+
* Nothing to say is no row rather than an empty one: a status line that is
|
|
369
|
+
* always there is a row of chrome, and the composer is a row further from the
|
|
370
|
+
* conversation for the whole of every session in which nothing goes wrong.
|
|
371
|
+
*/
|
|
372
|
+
export interface ChatInputStatusProps extends BoxProps {
|
|
373
|
+
/** Nothing to say is `null`, and no row. */
|
|
374
|
+
status: ChatSendStatus | null;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export const ChatInputStatus: (props: ChatInputStatusProps) => RenderOutput =
|
|
378
|
+
defineComponent<ChatInputStatusProps>('ChatInputStatus', (props) => {
|
|
379
|
+
const theme = useTheme();
|
|
380
|
+
const { status, ...rest } = props;
|
|
381
|
+
if (!status) return null;
|
|
382
|
+
const failed = status.state === 'failed';
|
|
383
|
+
const tone = failed ? 'danger' : 'muted';
|
|
384
|
+
return (
|
|
385
|
+
<Row gap={1} {...rest}>
|
|
386
|
+
{/* The hollow dot this screen already uses for "in progress", rather
|
|
387
|
+
than an ellipsis in front of a sentence that ends in one. */}
|
|
388
|
+
<text content={failed ? theme.glyphs.warning : theme.glyphs.bulletHollow} fg={tone} />
|
|
389
|
+
<text content={status.text} fg={tone} flex={1} truncate="end" />
|
|
390
|
+
</Row>
|
|
391
|
+
);
|
|
392
|
+
});
|
package/src/icons.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { UnicodeLevel } from '@textui/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A mark for a setting, and for the value it is set to.
|
|
5
|
+
*
|
|
6
|
+
* These are hints, not decisions, and the distinction is the whole design.
|
|
7
|
+
* The settings a session has are the *host's* - their keys, their titles,
|
|
8
|
+
* their values and the sentence under each one all arrive over the wire, and
|
|
9
|
+
* a client that switched on them would be back to working against one host.
|
|
10
|
+
* So nothing here is required to match: an unrecognised setting gets a
|
|
11
|
+
* neutral mark and keeps every word the host gave it.
|
|
12
|
+
*
|
|
13
|
+
* What the mark buys is a row that survives being squeezed. The control row
|
|
14
|
+
* truncates labels from the right as the terminal narrows, and a chip that is
|
|
15
|
+
* only a label truncates to nothing legible; a chip that leads with a mark
|
|
16
|
+
* still says which question it is at four cells wide.
|
|
17
|
+
*
|
|
18
|
+
* Matched on the key *and* on the label, because hosts disagree about both:
|
|
19
|
+
* the same question is `permissionMode` on one and `autoApprove` on the next,
|
|
20
|
+
* and is titled "Approvals" by one of them and "Agent Mode" by the other.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
interface Mark {
|
|
24
|
+
full: string;
|
|
25
|
+
ascii: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const pick = (mark: Mark, unicode: UnicodeLevel): string =>
|
|
29
|
+
(unicode === 'ascii' ? mark.ascii : mark.full);
|
|
30
|
+
|
|
31
|
+
/** Which question. Ordered: the first that matches wins. */
|
|
32
|
+
const SETTINGS: { when: RegExp; mark: Mark }[] = [
|
|
33
|
+
{ when: /harness|provider|agent(?!\s*mode)/, mark: { full: '◆', ascii: '@' } },
|
|
34
|
+
{ when: /model/, mark: { full: '◇', ascii: '#' } },
|
|
35
|
+
{ when: /branch/, mark: { full: '⋔', ascii: 'Y' } },
|
|
36
|
+
{ when: /workspace|director|folder|cwd|path/, mark: { full: '⌂', ascii: '~' } },
|
|
37
|
+
{ when: /isolat|worktree|target/, mark: { full: '▣', ascii: '=' } },
|
|
38
|
+
{ when: /approv|permission|mode/, mark: { full: '◉', ascii: '%' } },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const OTHER_SETTING: Mark = { full: '▪', ascii: '-' };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Which answer.
|
|
45
|
+
*
|
|
46
|
+
* The approval modes are the reason this exists: five of them, all named in
|
|
47
|
+
* the same two words - "Auto Mode", "Plan Mode", "Ask Before Edits" - and a
|
|
48
|
+
* list of five look-alike labels is a list you read twice. The marks put them
|
|
49
|
+
* in an order you can see, from "asks about everything" to "asks about
|
|
50
|
+
* nothing".
|
|
51
|
+
*
|
|
52
|
+
* Ordered, and the value is tested before the label: `bypassPermissions` and
|
|
53
|
+
* `autoApprove` both contain "auto", and only one of them means "decide for
|
|
54
|
+
* yourself".
|
|
55
|
+
*/
|
|
56
|
+
const VALUES: { when: RegExp; mark: Mark }[] = [
|
|
57
|
+
{ when: /bypass|approveall|autoapprove|allow all|yolo/, mark: { full: '⚠', ascii: '!' } },
|
|
58
|
+
{ when: /plan/, mark: { full: '≡', ascii: '=' } },
|
|
59
|
+
{ when: /auto|assist/, mark: { full: '⊙', ascii: 'o' } },
|
|
60
|
+
{ when: /accept|edit|write/, mark: { full: '✓', ascii: 'v' } },
|
|
61
|
+
{ when: /ask|default|manual|interactive|confirm/, mark: { full: '?', ascii: '?' } },
|
|
62
|
+
{ when: /worktree/, mark: { full: '⋔', ascii: 'Y' } },
|
|
63
|
+
// Not the house, which is the *workspace* chip's mark. Working in place and
|
|
64
|
+
// the directory being worked in are two chips side by side, and giving them
|
|
65
|
+
// the same mark is the one thing a mark is supposed to prevent.
|
|
66
|
+
{ when: /folder|workspace|inplace|in place/, mark: { full: '▣', ascii: '=' } },
|
|
67
|
+
{ when: /observe|readonly|read only/, mark: { full: '◌', ascii: '.' } },
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const OTHER_VALUE: Mark = { full: '·', ascii: '-' };
|
|
71
|
+
|
|
72
|
+
const normalise = (text: string): string => text.toLowerCase().replace(/[\s_-]+/g, '');
|
|
73
|
+
|
|
74
|
+
function match(table: { when: RegExp; mark: Mark }[], ...against: (string | undefined)[]): Mark | null {
|
|
75
|
+
for (const text of against) {
|
|
76
|
+
if (!text) continue;
|
|
77
|
+
const flat = normalise(text);
|
|
78
|
+
// Both spellings: `when` clauses like /allow all/ want the words apart and
|
|
79
|
+
// /autoapprove/ wants them together, and which one a host used is not
|
|
80
|
+
// something to have an opinion about.
|
|
81
|
+
const spaced = text.toLowerCase();
|
|
82
|
+
const found = table.find((entry) => entry.when.test(flat) || entry.when.test(spaced));
|
|
83
|
+
if (found) return found.mark;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The mark for one of the host's questions. Never absent. */
|
|
89
|
+
export function settingIcon(unicode: UnicodeLevel, key: string, title?: string): string {
|
|
90
|
+
return pick(match(SETTINGS, key, title) ?? OTHER_SETTING, unicode);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The mark for one of its answers.
|
|
95
|
+
*
|
|
96
|
+
* Absent rather than neutral when nothing matches *and* the setting is not one
|
|
97
|
+
* whose values are worth marking - a branch name is not a mode, and a column
|
|
98
|
+
* of identical dots beside a list of branches is noise with a shape.
|
|
99
|
+
*/
|
|
100
|
+
export function valueIcon(
|
|
101
|
+
unicode: UnicodeLevel,
|
|
102
|
+
value: string,
|
|
103
|
+
label?: string,
|
|
104
|
+
options: { fallback?: boolean } = {},
|
|
105
|
+
): string | undefined {
|
|
106
|
+
const found = match(VALUES, value, label);
|
|
107
|
+
if (found) return pick(found, unicode);
|
|
108
|
+
return options.fallback === true ? pick(OTHER_VALUE, unicode) : undefined;
|
|
109
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './blocks.js';
|
|
3
|
+
export * from './diff.js';
|
|
4
|
+
export * from './icons.js';
|
|
5
|
+
export * from './picker.js';
|
|
6
|
+
export * from './measure.js';
|
|
7
|
+
export * from './bubble.js';
|
|
8
|
+
export * from './transcript.js';
|
|
9
|
+
export * from './toolcall.js';
|
|
10
|
+
export * from './controls.js';
|
|
11
|
+
export * from './composer.js';
|
|
12
|
+
export * from './hitl.js';
|
|
13
|
+
export * from './details.js';
|
|
14
|
+
export * from './sessions.js';
|
|
15
|
+
export * from './sessionhead.js';
|
|
16
|
+
export * from './filediff.js';
|
package/src/measure.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Rect } from '@textui/core';
|
|
2
|
+
import { useEffect, useMeasure, useRef } from '@textui/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Tell whoever asked where this component is, and when it is gone.
|
|
6
|
+
*
|
|
7
|
+
* Reported on every change rather than once: a composer grows a slash menu
|
|
8
|
+
* upward and grows again with a wrapped draft, and the block that asks about
|
|
9
|
+
* a tool is only there while something is waiting. `null` on the way out, or
|
|
10
|
+
* a screen that had one would keep making room for it after it had gone.
|
|
11
|
+
*
|
|
12
|
+
* The callback is read through a ref so an inline arrow - which is a new
|
|
13
|
+
* function every render - does not re-run the unmount report every frame.
|
|
14
|
+
*/
|
|
15
|
+
export function useReportMeasure(onMeasure: ((rect: Rect | null) => void) | undefined): void {
|
|
16
|
+
const rect = useMeasure();
|
|
17
|
+
const latest = useRef(onMeasure);
|
|
18
|
+
latest.current = onMeasure;
|
|
19
|
+
useEffect(() => { latest.current?.(rect); }, [rect.x, rect.y, rect.width, rect.height]);
|
|
20
|
+
useEffect(() => () => { latest.current?.(null); }, []);
|
|
21
|
+
}
|
package/src/picker.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { TextUIApp } from '@textui/core';
|
|
2
|
+
import { argumentOf } from '@textui/widgets';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The palette, moved.
|
|
6
|
+
*
|
|
7
|
+
* A chip on the composer's control row asks one question - which harness,
|
|
8
|
+
* which model, how much it may do without asking, which workspace - and every
|
|
9
|
+
* one of those is a command with an argument. The command palette already
|
|
10
|
+
* knows how to ask about exactly that: `openAt` drills straight into an
|
|
11
|
+
* argument, `choices` may be a list, a function or a promise, an argument
|
|
12
|
+
* *without* choices is answered by typing, and `preview` shows what a
|
|
13
|
+
* highlighted value would do.
|
|
14
|
+
*
|
|
15
|
+
* So this is not a second overlay. It is the same one, anchored above the
|
|
16
|
+
* control that asked, and shown one command's question rather than the whole
|
|
17
|
+
* registry. Anything registered later - a workspace list, a thinking level, a
|
|
18
|
+
* subagent - is a command with an argument and needs nothing here.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const PICKER = 'chat.picker';
|
|
22
|
+
|
|
23
|
+
export interface PickerOptions {
|
|
24
|
+
/** The command whose argument is being asked about. */
|
|
25
|
+
commandId: string;
|
|
26
|
+
/** The node the panel sits above: the chip's own focus id. */
|
|
27
|
+
anchorId: string;
|
|
28
|
+
/**
|
|
29
|
+
* A fixed width. Left off, the panel is as wide as its widest answer, up to
|
|
30
|
+
* `maxWidth` - which is what a chip wants: "Worktree" and "Step-by-step
|
|
31
|
+
* collaboration" are the same question asked at two very different widths.
|
|
32
|
+
*/
|
|
33
|
+
width?: number;
|
|
34
|
+
maxWidth?: number;
|
|
35
|
+
visibleRows?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Put each value's sentence on its own line.
|
|
38
|
+
*
|
|
39
|
+
* For the questions whose answers are two words apart - "Accept edits" and
|
|
40
|
+
* "Plan only" - and told apart entirely by the line under them. Inline,
|
|
41
|
+
* that line shares the width with the label and shows the same truncated
|
|
42
|
+
* half of every answer.
|
|
43
|
+
*/
|
|
44
|
+
descriptions?: 'inline' | 'below';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function openPicker(app: TextUIApp, options: PickerOptions): void {
|
|
48
|
+
const command = app.commands.get(options.commandId);
|
|
49
|
+
if (!command) return;
|
|
50
|
+
// A chip whose command has no question to ask runs it: the workspace chip
|
|
51
|
+
// opens a dialog of its own, and a palette drilled into nothing is a list
|
|
52
|
+
// of one row that runs it anyway.
|
|
53
|
+
if (!argumentOf(command)) { void app.execute(options.commandId, undefined, 'menu'); return; }
|
|
54
|
+
|
|
55
|
+
// The chip that opened this one closes it. A control whose panel is already
|
|
56
|
+
// showing is a toggle - clicking it again to make it go away is the first
|
|
57
|
+
// thing anybody tries, and reopening it looks like the click did nothing.
|
|
58
|
+
const showing = app.layers.entries().find((entry) => entry.id === PICKER);
|
|
59
|
+
const already = showing?.node as { openAt?: string } | undefined;
|
|
60
|
+
if (already?.openAt === options.commandId) {
|
|
61
|
+
app.layers.close(PICKER);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Closed first, so opening a second chip's panel replaces the first rather
|
|
66
|
+
// than stacking two of them with the same id.
|
|
67
|
+
app.layers.close(PICKER);
|
|
68
|
+
app.layers.open({
|
|
69
|
+
id: PICKER,
|
|
70
|
+
// Floating, not modal: there is no scrim, because the row it is asking
|
|
71
|
+
// about has to stay readable underneath it. Which value is in force is
|
|
72
|
+
// half of what makes the question answerable.
|
|
73
|
+
layer: 'floating',
|
|
74
|
+
trapFocus: true,
|
|
75
|
+
dismissOnEscape: true,
|
|
76
|
+
dismissOnOutsideClick: true,
|
|
77
|
+
// Above the chip, aligned to its left edge - so it rises out of the
|
|
78
|
+
// control rather than appearing somewhere else on the screen. A composer
|
|
79
|
+
// sits at the bottom, so `top` is the side with room.
|
|
80
|
+
position: { kind: 'anchor', targetId: options.anchorId, side: 'top', align: 'start' },
|
|
81
|
+
node: {
|
|
82
|
+
component: 'CommandPalette',
|
|
83
|
+
...(options.width !== undefined ? { width: options.width } : {}),
|
|
84
|
+
maxWidth: options.maxWidth ?? 52,
|
|
85
|
+
visibleRows: options.visibleRows ?? 6,
|
|
86
|
+
// One command, so the panel opens on its values instead of on a search
|
|
87
|
+
// box with everything in it.
|
|
88
|
+
commands: [command],
|
|
89
|
+
openAt: options.commandId,
|
|
90
|
+
...(options.descriptions ? { descriptions: options.descriptions } : {}),
|
|
91
|
+
onClose: { handler: () => app.layers.close(PICKER) },
|
|
92
|
+
},
|
|
93
|
+
// Back to the chip that asked, however it was closed.
|
|
94
|
+
//
|
|
95
|
+
// The layer's own scope restores focus when a choice is made, and does not
|
|
96
|
+
// when the panel is dismissed with escape - which strands the keyboard on
|
|
97
|
+
// a node inside a layer that is no longer there, and the next tab starts
|
|
98
|
+
// from nowhere. Saying where focus goes is cheaper than depending on which
|
|
99
|
+
// of the two paths ran.
|
|
100
|
+
// Back to the chip that asked, whichever way it was closed. The layer's
|
|
101
|
+
// own scope restores focus after a choice; a dismissal would otherwise
|
|
102
|
+
// leave the keyboard on a node that is no longer mounted.
|
|
103
|
+
onClose: () => app.focus.focus(options.anchorId),
|
|
104
|
+
});
|
|
105
|
+
}
|