@galaxy-stack/blackhole-cli 2.3.0 → 3.0.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/README.md +78 -47
- package/dist/application.d.ts.map +1 -1
- package/dist/application.js +23 -2
- package/dist/application.js.map +1 -1
- package/dist/authorization.d.ts +22 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/authorization.js +42 -0
- package/dist/authorization.js.map +1 -0
- package/dist/cli-meta.d.ts +8 -0
- package/dist/cli-meta.d.ts.map +1 -1
- package/dist/cli-meta.js +11 -0
- package/dist/cli-meta.js.map +1 -1
- package/dist/cli.js +51 -27
- package/dist/cli.js.map +1 -1
- package/dist/config-command.d.ts +11 -14
- package/dist/config-command.d.ts.map +1 -1
- package/dist/config-command.js +41 -118
- package/dist/config-command.js.map +1 -1
- package/dist/doctor.d.ts +8 -2
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +11 -15
- package/dist/doctor.js.map +1 -1
- package/dist/file-index.d.ts +50 -0
- package/dist/file-index.d.ts.map +1 -1
- package/dist/file-index.js +124 -0
- package/dist/file-index.js.map +1 -1
- package/dist/harness/boot.d.ts +87 -0
- package/dist/harness/boot.d.ts.map +1 -0
- package/dist/harness/boot.js +111 -0
- package/dist/harness/boot.js.map +1 -0
- package/dist/harness/catalogs.d.ts +50 -0
- package/dist/harness/catalogs.d.ts.map +1 -0
- package/dist/harness/catalogs.js +34 -0
- package/dist/harness/catalogs.js.map +1 -0
- package/dist/harness/chat.d.ts +90 -0
- package/dist/harness/chat.d.ts.map +1 -0
- package/dist/harness/chat.js +260 -0
- package/dist/harness/chat.js.map +1 -0
- package/dist/harness/client.d.ts +72 -0
- package/dist/harness/client.d.ts.map +1 -0
- package/dist/harness/client.js +173 -0
- package/dist/harness/client.js.map +1 -0
- package/dist/harness/command.d.ts +22 -0
- package/dist/harness/command.d.ts.map +1 -0
- package/dist/harness/command.js +142 -0
- package/dist/harness/command.js.map +1 -0
- package/dist/harness/composer.d.ts +46 -0
- package/dist/harness/composer.d.ts.map +1 -0
- package/dist/harness/composer.js +127 -0
- package/dist/harness/composer.js.map +1 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.d.ts.map +1 -0
- package/dist/harness/conversation.js +196 -0
- package/dist/harness/conversation.js.map +1 -0
- package/dist/harness/help.d.ts +56 -0
- package/dist/harness/help.d.ts.map +1 -0
- package/dist/harness/help.js +85 -0
- package/dist/harness/help.js.map +1 -0
- package/dist/harness/history.d.ts +108 -0
- package/dist/harness/history.d.ts.map +1 -0
- package/dist/harness/history.js +268 -0
- package/dist/harness/history.js.map +1 -0
- package/dist/harness/labels.d.ts +164 -0
- package/dist/harness/labels.d.ts.map +1 -0
- package/dist/harness/labels.js +271 -0
- package/dist/harness/labels.js.map +1 -0
- package/dist/harness/locate.d.ts +24 -0
- package/dist/harness/locate.d.ts.map +1 -0
- package/dist/harness/locate.js +69 -0
- package/dist/harness/locate.js.map +1 -0
- package/dist/harness/menus.d.ts +74 -0
- package/dist/harness/menus.d.ts.map +1 -0
- package/dist/harness/menus.js +140 -0
- package/dist/harness/menus.js.map +1 -0
- package/dist/harness/model-name.d.ts +73 -0
- package/dist/harness/model-name.d.ts.map +1 -0
- package/dist/harness/model-name.js +101 -0
- package/dist/harness/model-name.js.map +1 -0
- package/dist/harness/parts.d.ts +239 -0
- package/dist/harness/parts.d.ts.map +1 -0
- package/dist/harness/parts.js +266 -0
- package/dist/harness/parts.js.map +1 -0
- package/dist/harness/preset.d.ts +124 -0
- package/dist/harness/preset.d.ts.map +1 -0
- package/dist/harness/preset.js +197 -0
- package/dist/harness/preset.js.map +1 -0
- package/dist/harness/process.d.ts +55 -0
- package/dist/harness/process.d.ts.map +1 -0
- package/dist/harness/process.js +119 -0
- package/dist/harness/process.js.map +1 -0
- package/dist/harness/screen.d.ts +33 -0
- package/dist/harness/screen.d.ts.map +1 -0
- package/dist/harness/screen.js +1070 -0
- package/dist/harness/screen.js.map +1 -0
- package/dist/harness/sessions.d.ts +55 -0
- package/dist/harness/sessions.d.ts.map +1 -0
- package/dist/harness/sessions.js +92 -0
- package/dist/harness/sessions.js.map +1 -0
- package/dist/harness/skills.d.ts +90 -0
- package/dist/harness/skills.d.ts.map +1 -0
- package/dist/harness/skills.js +133 -0
- package/dist/harness/skills.js.map +1 -0
- package/dist/harness/slash-palette.d.ts +32 -0
- package/dist/harness/slash-palette.d.ts.map +1 -0
- package/dist/harness/slash-palette.js +48 -0
- package/dist/harness/slash-palette.js.map +1 -0
- package/dist/harness/slash.d.ts +113 -0
- package/dist/harness/slash.d.ts.map +1 -0
- package/dist/harness/slash.js +187 -0
- package/dist/harness/slash.js.map +1 -0
- package/dist/harness/trajectory.d.ts +74 -0
- package/dist/harness/trajectory.d.ts.map +1 -0
- package/dist/harness/trajectory.js +270 -0
- package/dist/harness/trajectory.js.map +1 -0
- package/dist/harness/tui.d.ts +28 -0
- package/dist/harness/tui.d.ts.map +1 -0
- package/dist/harness/tui.js +143 -0
- package/dist/harness/tui.js.map +1 -0
- package/dist/manual-config.d.ts +23 -10
- package/dist/manual-config.d.ts.map +1 -1
- package/dist/manual-config.js +45 -37
- package/dist/manual-config.js.map +1 -1
- package/dist/migrate.d.ts +32 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +155 -0
- package/dist/migrate.js.map +1 -0
- package/dist/options.d.ts +10 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +23 -4
- package/dist/options.js.map +1 -1
- package/dist/setup.d.ts +14 -10
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +25 -92
- package/dist/setup.js.map +1 -1
- package/dist/timeline.d.ts.map +1 -1
- package/dist/timeline.js +11 -0
- package/dist/timeline.js.map +1 -1
- package/dist/web/command.d.ts +5 -1
- package/dist/web/command.d.ts.map +1 -1
- package/dist/web/command.js +22 -6
- package/dist/web/command.js.map +1 -1
- package/dist/web/credentials.d.ts +7 -5
- package/dist/web/credentials.d.ts.map +1 -1
- package/dist/web/credentials.js +13 -48
- package/dist/web/credentials.js.map +1 -1
- package/dist/web/doctor.d.ts +1 -1
- package/dist/web/doctor.d.ts.map +1 -1
- package/dist/web/doctor.js +42 -64
- package/dist/web/doctor.js.map +1 -1
- package/dist/web/options.d.ts +26 -2
- package/dist/web/options.d.ts.map +1 -1
- package/dist/web/options.js +54 -18
- package/dist/web/options.js.map +1 -1
- package/dist/web/paths.d.ts +1 -1
- package/dist/web/paths.d.ts.map +1 -1
- package/dist/web/paths.js +2 -2
- package/dist/web/paths.js.map +1 -1
- package/dist/web/web.d.ts +38 -16
- package/dist/web/web.d.ts.map +1 -1
- package/dist/web/web.js +166 -168
- package/dist/web/web.js.map +1 -1
- package/package.json +5 -4
- package/web/brand/lib/client.js +4 -4
- package/web/patches/gbh-ui.patch.yml +6 -24
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `blackhole chat` over the Galaxy Blackhole harness.
|
|
3
|
+
*
|
|
4
|
+
* Boots the app's own harness host on a free loopback port, exchanges the boot
|
|
5
|
+
* token for the session cookie, and hands the conversation to the terminal (or,
|
|
6
|
+
* with `--once <text>`, to stdout for scripts and measurements). The host log is
|
|
7
|
+
* kept on disk and its path is always reported.
|
|
8
|
+
*/
|
|
9
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { HarnessClient } from "./client.js";
|
|
12
|
+
import { HarnessChat } from "./chat.js";
|
|
13
|
+
import { HarnessBootError, spawnHarnessHost } from "./process.js";
|
|
14
|
+
import { BOOT_PHASES, bootPhaseFor, createBootReporter } from "./boot.js";
|
|
15
|
+
import { promoteLegacyCredential } from "../config-command.js";
|
|
16
|
+
function stamp() {
|
|
17
|
+
return new Date().toISOString().replace(/[:.]/g, "-");
|
|
18
|
+
}
|
|
19
|
+
function redact(url) {
|
|
20
|
+
return url.replace(/token=[^&]*/u, "token=…");
|
|
21
|
+
}
|
|
22
|
+
/** Print the failure and the launcher's own last words. */
|
|
23
|
+
function reportBootFailure(error) {
|
|
24
|
+
const failure = error instanceof HarnessBootError ? error : undefined;
|
|
25
|
+
process.stderr.write("Galaxy Blackhole: " + (error instanceof Error ? error.message : String(error)) + "\n");
|
|
26
|
+
if (failure?.logFile !== undefined)
|
|
27
|
+
process.stderr.write(" log: " + failure.logFile + "\n");
|
|
28
|
+
if (failure !== undefined && failure.tail.length > 0)
|
|
29
|
+
process.stderr.write(failure.tail.split("\n").slice(-12).map(line => " " + line).join("\n") + "\n");
|
|
30
|
+
}
|
|
31
|
+
/** Write one prompt, the answer and the outcome to a plain-text transcript. */
|
|
32
|
+
function writeTranscript(path, prompt, result) {
|
|
33
|
+
appendFileSync(path, [
|
|
34
|
+
"--- " + new Date().toISOString() + " ---",
|
|
35
|
+
"session: " + result.sessionId,
|
|
36
|
+
"state: " + result.state,
|
|
37
|
+
"prompt: " + prompt,
|
|
38
|
+
"answer: " + result.text,
|
|
39
|
+
...(result.error === undefined ? [] : ["error: " + result.error]),
|
|
40
|
+
"",
|
|
41
|
+
].join("\n"), "utf8");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Run one chat session on the harness.
|
|
45
|
+
* @param options - workspace, log directory, optional single prompt.
|
|
46
|
+
* @returns the process exit code.
|
|
47
|
+
*/
|
|
48
|
+
export async function runHarnessChat(options) {
|
|
49
|
+
// The harness reads the shared document only; a key left in the legacy DSH store is promoted
|
|
50
|
+
// once, before boot, so the setup gate and the harness agree on what "configured" means.
|
|
51
|
+
const promoted = promoteLegacyCredential();
|
|
52
|
+
if (promoted !== undefined)
|
|
53
|
+
process.stdout.write("Galaxy Blackhole: đã chuyển API key từ store DSH cũ sang " + promoted + "\n");
|
|
54
|
+
const logDir = join(options.stateDir, "harness");
|
|
55
|
+
mkdirSync(logDir, { recursive: true, mode: 0o700 });
|
|
56
|
+
// One reporter, the same one blackhole web paints: a terminal gets a single line rewritten
|
|
57
|
+
// in place, parked on the last row (the TUI's own status row) so the screen does not jump when
|
|
58
|
+
// it mounts; a script gets nothing, because the host log already holds every launcher line.
|
|
59
|
+
const interactive = options.once === undefined;
|
|
60
|
+
const boot = interactive
|
|
61
|
+
? createBootReporter(process.stdout, BOOT_PHASES[0].label, { bottom: true })
|
|
62
|
+
: process.stderr.isTTY === true
|
|
63
|
+
? createBootReporter(process.stderr, BOOT_PHASES[0].label)
|
|
64
|
+
: undefined;
|
|
65
|
+
let phaseIndex = 0;
|
|
66
|
+
const advance = (phase) => {
|
|
67
|
+
if (phase === undefined)
|
|
68
|
+
return;
|
|
69
|
+
const index = BOOT_PHASES.findIndex(entry => entry.id === phase.id);
|
|
70
|
+
if (index <= phaseIndex)
|
|
71
|
+
return;
|
|
72
|
+
phaseIndex = index;
|
|
73
|
+
boot?.step(phase.label);
|
|
74
|
+
};
|
|
75
|
+
const stopBoot = () => { boot?.clear(); };
|
|
76
|
+
const onLine = (line) => { advance(bootPhaseFor(line)); };
|
|
77
|
+
let host;
|
|
78
|
+
try {
|
|
79
|
+
host = await spawnHarnessHost({ logDir, onLine, ...(options.profile === undefined ? {} : { profile: options.profile }), ...(options.home === undefined ? {} : { home: options.home }) });
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
stopBoot();
|
|
83
|
+
reportBootFailure(error);
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
let client;
|
|
87
|
+
try {
|
|
88
|
+
client = await HarnessClient.connect(host.url);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
stopBoot();
|
|
92
|
+
reportBootFailure(error);
|
|
93
|
+
await host.stop();
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
stopBoot();
|
|
97
|
+
const chat = new HarnessChat(client, { cwd: options.cwd, ...(options.model === undefined ? {} : { model: options.model }) });
|
|
98
|
+
const transcript = join(logDir, "chat-" + stamp() + ".log");
|
|
99
|
+
try {
|
|
100
|
+
if (options.once === undefined) {
|
|
101
|
+
const { startHarnessTui } = await import("./tui.js");
|
|
102
|
+
await startHarnessTui({ chat, host, client, cwd: options.cwd, ...(options.model === undefined ? {} : { model: options.model }) });
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
const controller = new AbortController();
|
|
106
|
+
const cancel = () => controller.abort();
|
|
107
|
+
process.once("SIGINT", cancel);
|
|
108
|
+
process.once("SIGTERM", cancel);
|
|
109
|
+
let answer = "";
|
|
110
|
+
// A script reads the structured record on stdout; live tokens are for a human watching a terminal.
|
|
111
|
+
const live = process.stderr.isTTY === true;
|
|
112
|
+
const result = await chat.send(options.once, (chunk) => {
|
|
113
|
+
if (chunk.kind === "text") {
|
|
114
|
+
answer += chunk.text;
|
|
115
|
+
if (live)
|
|
116
|
+
process.stderr.write(chunk.text);
|
|
117
|
+
}
|
|
118
|
+
else if (chunk.kind === "tool-start" && live)
|
|
119
|
+
process.stderr.write("\n[tool] " + chunk.name + "\n");
|
|
120
|
+
else if (chunk.kind === "error")
|
|
121
|
+
process.stderr.write("\n[error] " + chunk.message + "\n");
|
|
122
|
+
}, controller.signal);
|
|
123
|
+
process.off("SIGINT", cancel);
|
|
124
|
+
process.off("SIGTERM", cancel);
|
|
125
|
+
writeTranscript(transcript, options.once, result);
|
|
126
|
+
process.stdout.write([
|
|
127
|
+
"harness: " + redact(host.url),
|
|
128
|
+
"session: " + result.sessionId,
|
|
129
|
+
"prompt: " + options.once,
|
|
130
|
+
"answer: " + (result.text.length > 0 ? result.text : answer),
|
|
131
|
+
"state: " + result.state,
|
|
132
|
+
"log: " + host.logFile,
|
|
133
|
+
"transcript: " + transcript,
|
|
134
|
+
"",
|
|
135
|
+
].join("\n"));
|
|
136
|
+
return result.state === "completed" ? 0 : result.state === "aborted" ? 130 : 1;
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
await host.stop();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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O,GAAG,IAAI,CAAC,CAAC;QAC7F,CAAC,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;QACtB,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC9B,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAC/B,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAClD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;YACnB,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;YAC9B,WAAW,GAAG,MAAM,CAAC,SAAS;YAC9B,UAAU,GAAG,OAAO,CAAC,IAAI;YACzB,UAAU,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;YAC5D,SAAS,GAAG,MAAM,CAAC,KAAK;YACxB,OAAO,GAAG,IAAI,CAAC,OAAO;YACtB,cAAc,GAAG,UAAU;YAC3B,EAAE;SACH,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACd,OAAO,MAAM,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACjF,CAAC;YAAS,CAAC;QACT,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;IACpB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The composer row: one message line and the two chip rows the desktop window
|
|
3
|
+
* puts under it — access mode on the left, model and send on the right.
|
|
4
|
+
*
|
|
5
|
+
* The terminal owns the caret, not React: the visible window is computed with
|
|
6
|
+
* grapheme-aware widths (`composerWindow`) and the native cursor is placed on
|
|
7
|
+
* the exact cell so IME composition lands where the user is typing.
|
|
8
|
+
*/
|
|
9
|
+
import { type ReactElement } from "react";
|
|
10
|
+
/** Everything the composer line and its chips need from the screen. */
|
|
11
|
+
export interface ChatComposerProps {
|
|
12
|
+
readonly value: string;
|
|
13
|
+
readonly onChange: (value: string) => void;
|
|
14
|
+
readonly onSubmit: () => void;
|
|
15
|
+
readonly active: boolean;
|
|
16
|
+
/** Whole terminal row of the text line, for the native caret. */
|
|
17
|
+
readonly row: number;
|
|
18
|
+
/** Column where the text starts, after the border, padding and prompt glyph. */
|
|
19
|
+
readonly column: number;
|
|
20
|
+
/** Width available to the text itself. */
|
|
21
|
+
readonly width: number;
|
|
22
|
+
readonly height: number;
|
|
23
|
+
readonly running: boolean;
|
|
24
|
+
/** Agent mode the session runs, as the mode chip words it ("Chế độ PTC"). */
|
|
25
|
+
readonly preset: string;
|
|
26
|
+
readonly access: string;
|
|
27
|
+
/** The route as a person reads it ("Galaxy Blackhole/Auto"). */
|
|
28
|
+
readonly model: string;
|
|
29
|
+
/** The model's own name, for a row too narrow to carry the provider's too. */
|
|
30
|
+
readonly modelShort?: string;
|
|
31
|
+
readonly effort?: string;
|
|
32
|
+
/** True while the @ picker owns Enter and the arrows. */
|
|
33
|
+
readonly picker?: boolean;
|
|
34
|
+
/** True when the chip row owns the arrow keys. */
|
|
35
|
+
readonly chipsFocused?: boolean;
|
|
36
|
+
/** Which chip is highlighted, in the same order the row is drawn. */
|
|
37
|
+
readonly chipIndex?: number;
|
|
38
|
+
}
|
|
39
|
+
/** The chips of the composer, in the order Tab and the arrows walk them. */
|
|
40
|
+
export declare const COMPOSER_CHIPS: readonly ["more", "mode", "access", "model", "send"];
|
|
41
|
+
/**
|
|
42
|
+
* One-line composer with a caret that survives wide, combining and pasted text.
|
|
43
|
+
* Enter submits through the screen, so a running turn can politely refuse it.
|
|
44
|
+
*/
|
|
45
|
+
export declare function ChatComposer(props: ChatComposerProps): ReactElement;
|
|
46
|
+
//# sourceMappingURL=composer.d.ts.map
|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GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,YAAY,CA+EnE"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The composer row: one message line and the two chip rows the desktop window
|
|
4
|
+
* puts under it — access mode on the left, model and send on the right.
|
|
5
|
+
*
|
|
6
|
+
* The terminal owns the caret, not React: the visible window is computed with
|
|
7
|
+
* grapheme-aware widths (`composerWindow`) and the native cursor is placed on
|
|
8
|
+
* the exact cell so IME composition lands where the user is typing.
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useRef, useState } from "react";
|
|
11
|
+
import { Box, Text, useCursor, useInput } from "ink";
|
|
12
|
+
import stringWidth from "string-width";
|
|
13
|
+
import { composerWindow } from "../composer.js";
|
|
14
|
+
import { terminalText } from "../timeline.js";
|
|
15
|
+
import { UI } from "./labels.js";
|
|
16
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
17
|
+
const graphemes = (text) => [...segmenter.segment(text)].map(part => part.segment);
|
|
18
|
+
/** The chips of the composer, in the order Tab and the arrows walk them. */
|
|
19
|
+
export const COMPOSER_CHIPS = Object.freeze(["more", "mode", "access", "model", "send"]);
|
|
20
|
+
/**
|
|
21
|
+
* One chip of the row. A focused chip is inverted so the key that will act on it is
|
|
22
|
+
* obvious; the rest stay dim until the composer itself has focus.
|
|
23
|
+
* @param props.focused - this chip owns the arrow keys now.
|
|
24
|
+
* @param props.active - the composer owns the keyboard.
|
|
25
|
+
* @param props.label - the chip text.
|
|
26
|
+
*/
|
|
27
|
+
function Chip(props) {
|
|
28
|
+
if (props.focused)
|
|
29
|
+
return _jsx(Text, { backgroundColor: "cyan", color: "black", bold: true, children: " " + props.label + " " });
|
|
30
|
+
return _jsx(Text, { color: props.active ? "white" : "gray", dimColor: !props.active, children: " " + props.label + " " });
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* One-line composer with a caret that survives wide, combining and pasted text.
|
|
34
|
+
* Enter submits through the screen, so a running turn can politely refuse it.
|
|
35
|
+
*/
|
|
36
|
+
export function ChatComposer(props) {
|
|
37
|
+
const chars = graphemes(props.value);
|
|
38
|
+
const [cursor, setCursor] = useState(() => chars.length);
|
|
39
|
+
const cursorRef = useRef(cursor);
|
|
40
|
+
const textRef = useRef(props.value);
|
|
41
|
+
useEffect(() => { textRef.current = props.value; }, [props.value]);
|
|
42
|
+
useEffect(() => { if (cursor > graphemes(props.value).length)
|
|
43
|
+
setCursor(graphemes(props.value).length); }, [props.value, cursor]);
|
|
44
|
+
const move = (position) => { cursorRef.current = position; setCursor(position); };
|
|
45
|
+
const change = (next) => { textRef.current = next; props.onChange(next); };
|
|
46
|
+
const view = composerWindow(props.value, Math.min(cursor, chars.length), Math.max(2, props.width));
|
|
47
|
+
const { setCursorPosition } = useCursor();
|
|
48
|
+
setCursorPosition(props.active
|
|
49
|
+
? { x: props.column + stringWidth(view.before), y: props.row }
|
|
50
|
+
: undefined);
|
|
51
|
+
useInput((input, key) => {
|
|
52
|
+
if (key.eventType === "release")
|
|
53
|
+
return;
|
|
54
|
+
const current = graphemes(textRef.current);
|
|
55
|
+
let position = Math.min(cursorRef.current, current.length);
|
|
56
|
+
// While a picker is open, Enter chooses a row: the screen owns it, not the composer.
|
|
57
|
+
if (key.return) {
|
|
58
|
+
if (props.picker !== true)
|
|
59
|
+
props.onSubmit();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (key.escape || key.tab || key.upArrow || key.downArrow || key.pageUp || key.pageDown)
|
|
63
|
+
return;
|
|
64
|
+
if (key.leftArrow) {
|
|
65
|
+
move(Math.max(0, position - 1));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (key.rightArrow) {
|
|
69
|
+
move(Math.min(current.length, position + 1));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (key.home || (key.ctrl && input === "a")) {
|
|
73
|
+
move(0);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (key.end || (key.ctrl && input === "e")) {
|
|
77
|
+
move(current.length);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (key.ctrl && input === "u") {
|
|
81
|
+
current.splice(0, position);
|
|
82
|
+
position = 0;
|
|
83
|
+
}
|
|
84
|
+
else if (key.ctrl && input === "w") {
|
|
85
|
+
const end = position;
|
|
86
|
+
while (position > 0 && /\s/u.test(current[position - 1]))
|
|
87
|
+
position -= 1;
|
|
88
|
+
while (position > 0 && !/\s/u.test(current[position - 1]))
|
|
89
|
+
position -= 1;
|
|
90
|
+
current.splice(position, end - position);
|
|
91
|
+
}
|
|
92
|
+
else if (key.ctrl || key.meta)
|
|
93
|
+
return;
|
|
94
|
+
else if (key.backspace) {
|
|
95
|
+
if (position > 0)
|
|
96
|
+
current.splice(--position, 1);
|
|
97
|
+
}
|
|
98
|
+
else if (key.delete)
|
|
99
|
+
current.splice(position, 1);
|
|
100
|
+
else if (input.length > 0) {
|
|
101
|
+
const inserted = terminalText(input).replace(/[\n\t]+/gu, " ");
|
|
102
|
+
const prefix = current.slice(0, position).join("") + inserted;
|
|
103
|
+
change(prefix + current.slice(position).join(""));
|
|
104
|
+
move(graphemes(prefix).length);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
change(current.join(""));
|
|
108
|
+
move(position);
|
|
109
|
+
}, { isActive: props.active });
|
|
110
|
+
const narrow = props.width < 42;
|
|
111
|
+
// The route chip now carries the provider's name, which is ~20 columns the row never spent
|
|
112
|
+
// before: when the whole row no longer fits, the chip keeps the model's own name instead of
|
|
113
|
+
// squeezing every other label until each one reads like debris.
|
|
114
|
+
const effort = props.effort === undefined ? "" : " · " + props.effort;
|
|
115
|
+
const routeChip = props.model + effort + " ▾";
|
|
116
|
+
const shortChip = (props.modelShort ?? props.model) + effort + " ▾";
|
|
117
|
+
const sendChip = (props.running ? "■ " + UI.stop : "↑ " + UI.send) + " ▾";
|
|
118
|
+
const chip = (label) => " " + label + " ";
|
|
119
|
+
const leftChips = chip("+ " + UI.more) + chip("◈ " + props.preset + " ▾") + chip("⚙ " + props.access + " ▾");
|
|
120
|
+
const room = Math.max(0, props.width + 8 - 4);
|
|
121
|
+
const route = room >= stringWidth(leftChips + chip(routeChip) + chip(sendChip)) ? routeChip : shortChip;
|
|
122
|
+
const hintFits = room >= stringWidth(leftChips + " " + UI.chipsHint + chip(route) + chip(sendChip));
|
|
123
|
+
return _jsxs(Box, { flexDirection: "column", width: Math.max(20, props.width + 8), borderStyle: "round", borderColor: props.active ? "cyan" : "gray", paddingX: 1, overflow: "hidden", children: [_jsxs(Box, { height: 1, overflow: "hidden", children: [_jsx(Text, { color: "cyan", bold: props.active, children: "❯ " }), props.value.length === 0
|
|
124
|
+
? _jsx(Text, { dimColor: true, wrap: "truncate-end", children: UI.placeholder })
|
|
125
|
+
: _jsxs(Text, { wrap: "truncate-end", children: [view.before, _jsx(Text, { inverse: props.active, children: view.caret }), view.after] })] }), props.height >= 4 && _jsxs(Box, { height: 1, overflow: "hidden", children: [_jsxs(Box, { flexGrow: 1, overflow: "hidden", children: [_jsx(Chip, { focused: props.chipsFocused === true && props.chipIndex === 0, active: props.active, label: "+ " + UI.more }), _jsx(Chip, { focused: props.chipsFocused === true && props.chipIndex === 1, active: props.active, label: "◈ " + props.preset + " ▾" }), _jsx(Chip, { focused: props.chipsFocused === true && props.chipIndex === 2, active: props.active, label: "⚙ " + props.access + " ▾" }), narrow || !hintFits ? null : _jsx(Text, { dimColor: true, children: " " + (props.chipsFocused === true ? UI.chipsHintFocused : UI.chipsHint) })] }), _jsxs(Box, { flexShrink: 0, children: [_jsx(Chip, { focused: props.chipsFocused === true && props.chipIndex === 3, active: props.active, label: route }), _jsx(Chip, { focused: props.chipsFocused === true && props.chipIndex === 4, active: props.active, label: props.running ? "■ " + UI.stop : "↑ " + UI.send })] })] })] });
|
|
126
|
+
}
|
|
127
|
+
//#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AK,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,KAAK,CAAC,MAAM,GAAG,IAAI,GAAI,EAC/H,MAAM,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAC,IAAI,IAAC,QAAQ,kBAAE,IAAI,GAAG,CAAC,KAAK,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,GAAQ,IAC3H,EACN,MAAC,GAAG,IAAC,UAAU,EAAE,CAAC,aAChB,KAAC,IAAI,IAAC,OAAO,EAAE,KAAK,CAAC,YAAY,KAAK,IAAI,IAAI,KAAK,CAAC,SAAS,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,GAAI,EAC3G,KAAC,IAAI,IAAC,OAAO,EAAE,KAAK,CAAC,YAAY,KAAK,IAAI,IAAI,KAAK,CAAC,SAAS,KAAK,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,GAAI,IACjJ,IACF,IACF,CAAC;AACT,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The conversation's own state, between the harness driver and the screen.
|
|
3
|
+
*
|
|
4
|
+
* `HarnessChat.send` reports facts — text, reasoning, tool lifecycles, a turn's
|
|
5
|
+
* model, tokens and wall time — and this reducer turns them into the rows a
|
|
6
|
+
* desktop conversation shows. It never talks to the host and never owns a
|
|
7
|
+
* protocol: switching sessions is a reset, and a cancelled turn is the same
|
|
8
|
+
* path with an aborted result.
|
|
9
|
+
*/
|
|
10
|
+
import type { ChatChunk, ChatTurnResult } from "./chat.js";
|
|
11
|
+
import { type TranscriptMessage } from "./history.js";
|
|
12
|
+
import { type ModelNames } from "./model-name.js";
|
|
13
|
+
/** One turn as the "Luồng chạy" view lists it. */
|
|
14
|
+
export interface TurnRecord {
|
|
15
|
+
readonly index: number;
|
|
16
|
+
readonly prompt: string;
|
|
17
|
+
state: "running" | "completed" | "aborted" | "error";
|
|
18
|
+
steps: number;
|
|
19
|
+
toolCalls: number;
|
|
20
|
+
tokens?: number;
|
|
21
|
+
durationMs?: number;
|
|
22
|
+
model?: string;
|
|
23
|
+
readonly startedAt: number;
|
|
24
|
+
}
|
|
25
|
+
/** Everything the main column draws. */
|
|
26
|
+
export interface ChatState {
|
|
27
|
+
readonly messages: readonly TranscriptMessage[];
|
|
28
|
+
readonly turns: readonly TurnRecord[];
|
|
29
|
+
readonly running: boolean;
|
|
30
|
+
readonly status: string;
|
|
31
|
+
/** True while the newest turn is reasoning rather than writing or calling a tool. */
|
|
32
|
+
readonly thinking: boolean;
|
|
33
|
+
readonly steps: number;
|
|
34
|
+
readonly tokens?: number;
|
|
35
|
+
readonly durationMs?: number;
|
|
36
|
+
readonly model?: string;
|
|
37
|
+
readonly provider?: string;
|
|
38
|
+
readonly effort?: string;
|
|
39
|
+
readonly error?: string;
|
|
40
|
+
}
|
|
41
|
+
/** What a freshly opened conversation starts from. */
|
|
42
|
+
export declare function emptyChatState(): ChatState;
|
|
43
|
+
/** One action the screen dispatches while a conversation is open. */
|
|
44
|
+
export type ChatAction = {
|
|
45
|
+
readonly type: "reset";
|
|
46
|
+
readonly messages: readonly TranscriptMessage[];
|
|
47
|
+
readonly steps: number;
|
|
48
|
+
readonly tokens?: number;
|
|
49
|
+
} | {
|
|
50
|
+
readonly type: "prompt";
|
|
51
|
+
readonly text: string;
|
|
52
|
+
readonly at: number;
|
|
53
|
+
} | {
|
|
54
|
+
readonly type: "chunk";
|
|
55
|
+
readonly chunk: ChatChunk;
|
|
56
|
+
} | {
|
|
57
|
+
readonly type: "settled";
|
|
58
|
+
readonly result: ChatTurnResult;
|
|
59
|
+
} | {
|
|
60
|
+
readonly type: "stats";
|
|
61
|
+
readonly steps?: number;
|
|
62
|
+
readonly tokens?: number;
|
|
63
|
+
} | {
|
|
64
|
+
readonly type: "notice";
|
|
65
|
+
readonly text: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Apply one conversation fact.
|
|
69
|
+
* @param state - the current screen state.
|
|
70
|
+
* @param action - a prompt, a driver chunk, a settled turn or a notice.
|
|
71
|
+
* @param names - display names for the route a turn's meta row prints.
|
|
72
|
+
* @returns the next state; never mutates the previous one.
|
|
73
|
+
*/
|
|
74
|
+
export declare function chatReducer(state: ChatState, action: ChatAction, names?: ModelNames): ChatState;
|
|
75
|
+
//# sourceMappingURL=conversation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../../src/harness/conversation.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAA4B,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEhF,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGlF,kDAAkD;AAClD,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,KAAK,EAAE,SAAS,GAAG,WAAW,GAAG,SAAS,GAAG,OAAO,CAAC;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,wCAAwC;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,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;CAAE,GAC7D;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC7E;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,GAAE,UAAgC,GAAG,SAAS,CAoHpH"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { todoItems, withTodoBlock } from "./history.js";
|
|
2
|
+
import { cleanToolResult, toolTitle, UI } from "./labels.js";
|
|
3
|
+
import { DEFAULT_MODEL_NAMES, modelName } from "./model-name.js";
|
|
4
|
+
/** What a freshly opened conversation starts from. */
|
|
5
|
+
export function emptyChatState() {
|
|
6
|
+
return Object.freeze({ messages: Object.freeze([]), turns: Object.freeze([]), running: false, status: "", thinking: false, steps: 0 });
|
|
7
|
+
}
|
|
8
|
+
/** The status `HarnessChat` emits once per agent step; it is the live step counter. */
|
|
9
|
+
const STEP_STATUS = "Model đang trả lời…";
|
|
10
|
+
/** Append one notice row the transcript already knows how to draw. */
|
|
11
|
+
function notice(message) {
|
|
12
|
+
return { role: "assistant", content: [{ type: "data", name: "galaxy-notice", data: { text: message } }] };
|
|
13
|
+
}
|
|
14
|
+
/** Return a copy whose last assistant message is new; a user message forces a new assistant row. */
|
|
15
|
+
function withAssistant(state, update) {
|
|
16
|
+
const messages = [...state.messages];
|
|
17
|
+
let index = messages.length - 1;
|
|
18
|
+
let target = messages[index];
|
|
19
|
+
if (target === undefined || target.role !== "assistant" || target.content.some(part => part.type === "data" && part.name === "galaxy-meta")) {
|
|
20
|
+
target = { role: "assistant", content: [] };
|
|
21
|
+
messages.push(target);
|
|
22
|
+
index = messages.length - 1;
|
|
23
|
+
}
|
|
24
|
+
const content = [...target.content];
|
|
25
|
+
update(content);
|
|
26
|
+
messages[index] = { role: "assistant", content };
|
|
27
|
+
return { ...state, messages };
|
|
28
|
+
}
|
|
29
|
+
/** Append text or reasoning, merging into the adjacent run of the same kind. */
|
|
30
|
+
function append(content, type, text) {
|
|
31
|
+
if (text.length === 0)
|
|
32
|
+
return;
|
|
33
|
+
const previous = content.at(-1);
|
|
34
|
+
if (previous?.type === type)
|
|
35
|
+
content[content.length - 1] = { ...previous, text: previous.text + text };
|
|
36
|
+
else
|
|
37
|
+
content.push({ type, text });
|
|
38
|
+
}
|
|
39
|
+
/** Mark the newest still-running tool with an outcome. */
|
|
40
|
+
function settleTool(state, id, update) {
|
|
41
|
+
for (let messageIndex = state.messages.length - 1; messageIndex >= 0; messageIndex -= 1) {
|
|
42
|
+
const message = state.messages[messageIndex];
|
|
43
|
+
const partIndex = message.content.findIndex(part => part.type === "data" && part.name === "galaxy-tool" && part.data.id === id);
|
|
44
|
+
if (partIndex < 0)
|
|
45
|
+
continue;
|
|
46
|
+
const part = message.content[partIndex];
|
|
47
|
+
if (part.type !== "data")
|
|
48
|
+
continue;
|
|
49
|
+
const content = [...message.content];
|
|
50
|
+
content[partIndex] = { ...part, data: update(part.data) };
|
|
51
|
+
const messages = [...state.messages];
|
|
52
|
+
messages[messageIndex] = { role: message.role, content };
|
|
53
|
+
return { ...state, messages };
|
|
54
|
+
}
|
|
55
|
+
return state;
|
|
56
|
+
}
|
|
57
|
+
/** Update the running turn, or create one when the driver starts a new prompt. */
|
|
58
|
+
function withTurn(state, update) {
|
|
59
|
+
const index = state.turns.findLastIndex(turn => turn.state === "running");
|
|
60
|
+
if (index < 0)
|
|
61
|
+
return state;
|
|
62
|
+
const turns = [...state.turns];
|
|
63
|
+
turns[index] = update(turns[index]);
|
|
64
|
+
return { ...state, turns };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Apply one conversation fact.
|
|
68
|
+
* @param state - the current screen state.
|
|
69
|
+
* @param action - a prompt, a driver chunk, a settled turn or a notice.
|
|
70
|
+
* @param names - display names for the route a turn's meta row prints.
|
|
71
|
+
* @returns the next state; never mutates the previous one.
|
|
72
|
+
*/
|
|
73
|
+
export function chatReducer(state, action, names = DEFAULT_MODEL_NAMES) {
|
|
74
|
+
if (action.type === "reset") {
|
|
75
|
+
return {
|
|
76
|
+
messages: action.messages,
|
|
77
|
+
turns: [],
|
|
78
|
+
running: false,
|
|
79
|
+
status: "",
|
|
80
|
+
thinking: false,
|
|
81
|
+
steps: action.steps,
|
|
82
|
+
...(action.tokens === undefined ? {} : { tokens: action.tokens }),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (action.type === "notice")
|
|
86
|
+
return { ...state, messages: [...state.messages, notice(action.text)] };
|
|
87
|
+
if (action.type === "stats") {
|
|
88
|
+
return {
|
|
89
|
+
...state,
|
|
90
|
+
...(action.steps === undefined ? {} : { steps: action.steps }),
|
|
91
|
+
...(action.tokens === undefined ? {} : { tokens: action.tokens }),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (action.type === "prompt") {
|
|
95
|
+
const messages = [...state.messages, { role: "user", content: [{ type: "text", text: action.text }] }];
|
|
96
|
+
const turns = [...state.turns, {
|
|
97
|
+
index: state.turns.length + 1,
|
|
98
|
+
prompt: action.text,
|
|
99
|
+
state: "running",
|
|
100
|
+
steps: 0,
|
|
101
|
+
toolCalls: 0,
|
|
102
|
+
startedAt: action.at,
|
|
103
|
+
}];
|
|
104
|
+
// A new prompt clears the previous failure: the error key is dropped, not set to undefined.
|
|
105
|
+
const { error: _previousFailure, ...rest } = state;
|
|
106
|
+
return { ...rest, messages, turns, running: true, status: "Đang gửi…", thinking: true };
|
|
107
|
+
}
|
|
108
|
+
if (action.type === "settled") {
|
|
109
|
+
const result = action.result;
|
|
110
|
+
const turns = state.turns.map(turn => turn.state === "running"
|
|
111
|
+
? { ...turn, state: result.state === "completed" ? "completed" : result.state === "aborted" ? "aborted" : "error" }
|
|
112
|
+
: turn);
|
|
113
|
+
return {
|
|
114
|
+
...state,
|
|
115
|
+
turns,
|
|
116
|
+
running: false,
|
|
117
|
+
status: "",
|
|
118
|
+
thinking: false,
|
|
119
|
+
...(result.error === undefined ? {} : { error: result.error }),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const chunk = action.chunk;
|
|
123
|
+
if (chunk.kind === "text")
|
|
124
|
+
return { ...withAssistant(state, content => append(content, "text", chunk.text)), thinking: false };
|
|
125
|
+
if (chunk.kind === "reasoning")
|
|
126
|
+
return { ...withAssistant(state, content => append(content, "reasoning", chunk.text)), thinking: true };
|
|
127
|
+
if (chunk.kind === "tool-start") {
|
|
128
|
+
const tool = {
|
|
129
|
+
id: chunk.id,
|
|
130
|
+
label: toolTitle(chunk.name, chunk.args),
|
|
131
|
+
status: "running",
|
|
132
|
+
summary: "",
|
|
133
|
+
details: JSON.stringify({ arguments: chunk.args }),
|
|
134
|
+
};
|
|
135
|
+
const counted = withTurn(state, turn => ({ ...turn, toolCalls: turn.toolCalls + 1 }));
|
|
136
|
+
const next = { ...withAssistant(counted, content => { content.push({ type: "data", name: "galaxy-tool", data: tool }); }), thinking: false };
|
|
137
|
+
// A planning call carries the whole list in its own arguments, so the live turn shows the
|
|
138
|
+
// "Tác vụ" block the reopened session will show — one block, always the newest list.
|
|
139
|
+
const todos = todoItems(chunk.args["todos"]);
|
|
140
|
+
if (todos !== undefined)
|
|
141
|
+
return { ...next, messages: withTodoBlock(next.messages, todos), thinking: false };
|
|
142
|
+
return next;
|
|
143
|
+
}
|
|
144
|
+
if (chunk.kind === "tool-end") {
|
|
145
|
+
return settleTool(state, chunk.id, tool => ({ ...tool, status: chunk.ok ? "success" : "error", summary: cleanToolResult(chunk.summary) }));
|
|
146
|
+
}
|
|
147
|
+
// The host's own plan event: one block, always the newest list, exactly as a reopened
|
|
148
|
+
// session folds it. A turn that plans in PTC mode only ever reports the list this way.
|
|
149
|
+
if (chunk.kind === "todo")
|
|
150
|
+
return { ...state, messages: withTodoBlock(state.messages, chunk.todos) };
|
|
151
|
+
if (chunk.kind === "meta") {
|
|
152
|
+
const tokens = chunk.tokens;
|
|
153
|
+
const provider = chunk.model?.provider;
|
|
154
|
+
// The meta row names the route the way the host names it; the wire id never prints.
|
|
155
|
+
const model = modelName(provider, chunk.model?.model, names);
|
|
156
|
+
const withMeta = withAssistant(state, content => {
|
|
157
|
+
content.push({
|
|
158
|
+
type: "data",
|
|
159
|
+
name: "galaxy-meta",
|
|
160
|
+
data: {
|
|
161
|
+
brand: UI.brand,
|
|
162
|
+
...(model === undefined ? {} : { model }),
|
|
163
|
+
...(tokens === undefined ? {} : { tokens }),
|
|
164
|
+
durationMs: chunk.durationMs,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
const turns = withMeta.turns.map(turn => turn.state === "running"
|
|
169
|
+
? {
|
|
170
|
+
...turn,
|
|
171
|
+
...(tokens === undefined ? {} : { tokens }),
|
|
172
|
+
durationMs: chunk.durationMs,
|
|
173
|
+
...(model === undefined ? {} : { model }),
|
|
174
|
+
}
|
|
175
|
+
: turn);
|
|
176
|
+
return {
|
|
177
|
+
...withMeta,
|
|
178
|
+
turns: turns.length === 0 ? withMeta.turns : Object.freeze(turns),
|
|
179
|
+
...(tokens === undefined ? {} : { tokens }),
|
|
180
|
+
durationMs: chunk.durationMs,
|
|
181
|
+
...(model === undefined ? {} : { model }),
|
|
182
|
+
thinking: false,
|
|
183
|
+
...(provider === undefined ? {} : { provider }),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (chunk.kind === "error") {
|
|
187
|
+
return { ...state, messages: [...state.messages, notice(chunk.message)], error: chunk.message };
|
|
188
|
+
}
|
|
189
|
+
// A status is the driver's own sentence: "Đang gửi…", "Model đang trả lời…", "Đã dừng".
|
|
190
|
+
if (chunk.label.length === 0)
|
|
191
|
+
return { ...state, status: "" };
|
|
192
|
+
const steps = chunk.label === STEP_STATUS ? Math.max(1, state.steps + 1) : state.steps;
|
|
193
|
+
const counted = withTurn(state, turn => (chunk.label === STEP_STATUS ? { ...turn, steps: Math.max(1, turn.steps + 1) } : turn));
|
|
194
|
+
return { ...counted, status: chunk.label, steps };
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=conversation.js.map
|