@galaxy-stack/blackhole-cli 2.4.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.
Files changed (160) hide show
  1. package/README.md +78 -47
  2. package/dist/application.d.ts.map +1 -1
  3. package/dist/application.js +13 -2
  4. package/dist/application.js.map +1 -1
  5. package/dist/cli-meta.d.ts +8 -0
  6. package/dist/cli-meta.d.ts.map +1 -1
  7. package/dist/cli-meta.js +11 -0
  8. package/dist/cli-meta.js.map +1 -1
  9. package/dist/cli.js +26 -9
  10. package/dist/cli.js.map +1 -1
  11. package/dist/config-command.d.ts +11 -14
  12. package/dist/config-command.d.ts.map +1 -1
  13. package/dist/config-command.js +41 -118
  14. package/dist/config-command.js.map +1 -1
  15. package/dist/doctor.d.ts +8 -2
  16. package/dist/doctor.d.ts.map +1 -1
  17. package/dist/doctor.js +11 -15
  18. package/dist/doctor.js.map +1 -1
  19. package/dist/file-index.d.ts +50 -0
  20. package/dist/file-index.d.ts.map +1 -1
  21. package/dist/file-index.js +124 -0
  22. package/dist/file-index.js.map +1 -1
  23. package/dist/harness/boot.d.ts +87 -0
  24. package/dist/harness/boot.d.ts.map +1 -0
  25. package/dist/harness/boot.js +111 -0
  26. package/dist/harness/boot.js.map +1 -0
  27. package/dist/harness/catalogs.d.ts +50 -0
  28. package/dist/harness/catalogs.d.ts.map +1 -0
  29. package/dist/harness/catalogs.js +34 -0
  30. package/dist/harness/catalogs.js.map +1 -0
  31. package/dist/harness/chat.d.ts +90 -0
  32. package/dist/harness/chat.d.ts.map +1 -0
  33. package/dist/harness/chat.js +260 -0
  34. package/dist/harness/chat.js.map +1 -0
  35. package/dist/harness/client.d.ts +72 -0
  36. package/dist/harness/client.d.ts.map +1 -0
  37. package/dist/harness/client.js +173 -0
  38. package/dist/harness/client.js.map +1 -0
  39. package/dist/harness/command.d.ts +22 -0
  40. package/dist/harness/command.d.ts.map +1 -0
  41. package/dist/harness/command.js +142 -0
  42. package/dist/harness/command.js.map +1 -0
  43. package/dist/harness/composer.d.ts +46 -0
  44. package/dist/harness/composer.d.ts.map +1 -0
  45. package/dist/harness/composer.js +127 -0
  46. package/dist/harness/composer.js.map +1 -0
  47. package/dist/harness/conversation.d.ts +75 -0
  48. package/dist/harness/conversation.d.ts.map +1 -0
  49. package/dist/harness/conversation.js +196 -0
  50. package/dist/harness/conversation.js.map +1 -0
  51. package/dist/harness/help.d.ts +56 -0
  52. package/dist/harness/help.d.ts.map +1 -0
  53. package/dist/harness/help.js +85 -0
  54. package/dist/harness/help.js.map +1 -0
  55. package/dist/harness/history.d.ts +108 -0
  56. package/dist/harness/history.d.ts.map +1 -0
  57. package/dist/harness/history.js +268 -0
  58. package/dist/harness/history.js.map +1 -0
  59. package/dist/harness/labels.d.ts +164 -0
  60. package/dist/harness/labels.d.ts.map +1 -0
  61. package/dist/harness/labels.js +271 -0
  62. package/dist/harness/labels.js.map +1 -0
  63. package/dist/harness/locate.d.ts +24 -0
  64. package/dist/harness/locate.d.ts.map +1 -0
  65. package/dist/harness/locate.js +69 -0
  66. package/dist/harness/locate.js.map +1 -0
  67. package/dist/harness/menus.d.ts +74 -0
  68. package/dist/harness/menus.d.ts.map +1 -0
  69. package/dist/harness/menus.js +140 -0
  70. package/dist/harness/menus.js.map +1 -0
  71. package/dist/harness/model-name.d.ts +73 -0
  72. package/dist/harness/model-name.d.ts.map +1 -0
  73. package/dist/harness/model-name.js +101 -0
  74. package/dist/harness/model-name.js.map +1 -0
  75. package/dist/harness/parts.d.ts +239 -0
  76. package/dist/harness/parts.d.ts.map +1 -0
  77. package/dist/harness/parts.js +266 -0
  78. package/dist/harness/parts.js.map +1 -0
  79. package/dist/harness/preset.d.ts +124 -0
  80. package/dist/harness/preset.d.ts.map +1 -0
  81. package/dist/harness/preset.js +197 -0
  82. package/dist/harness/preset.js.map +1 -0
  83. package/dist/harness/process.d.ts +55 -0
  84. package/dist/harness/process.d.ts.map +1 -0
  85. package/dist/harness/process.js +119 -0
  86. package/dist/harness/process.js.map +1 -0
  87. package/dist/harness/screen.d.ts +33 -0
  88. package/dist/harness/screen.d.ts.map +1 -0
  89. package/dist/harness/screen.js +1070 -0
  90. package/dist/harness/screen.js.map +1 -0
  91. package/dist/harness/sessions.d.ts +55 -0
  92. package/dist/harness/sessions.d.ts.map +1 -0
  93. package/dist/harness/sessions.js +92 -0
  94. package/dist/harness/sessions.js.map +1 -0
  95. package/dist/harness/skills.d.ts +90 -0
  96. package/dist/harness/skills.d.ts.map +1 -0
  97. package/dist/harness/skills.js +133 -0
  98. package/dist/harness/skills.js.map +1 -0
  99. package/dist/harness/slash-palette.d.ts +32 -0
  100. package/dist/harness/slash-palette.d.ts.map +1 -0
  101. package/dist/harness/slash-palette.js +48 -0
  102. package/dist/harness/slash-palette.js.map +1 -0
  103. package/dist/harness/slash.d.ts +113 -0
  104. package/dist/harness/slash.d.ts.map +1 -0
  105. package/dist/harness/slash.js +187 -0
  106. package/dist/harness/slash.js.map +1 -0
  107. package/dist/harness/trajectory.d.ts +74 -0
  108. package/dist/harness/trajectory.d.ts.map +1 -0
  109. package/dist/harness/trajectory.js +270 -0
  110. package/dist/harness/trajectory.js.map +1 -0
  111. package/dist/harness/tui.d.ts +28 -0
  112. package/dist/harness/tui.d.ts.map +1 -0
  113. package/dist/harness/tui.js +143 -0
  114. package/dist/harness/tui.js.map +1 -0
  115. package/dist/manual-config.d.ts +23 -10
  116. package/dist/manual-config.d.ts.map +1 -1
  117. package/dist/manual-config.js +45 -37
  118. package/dist/manual-config.js.map +1 -1
  119. package/dist/migrate.d.ts +32 -0
  120. package/dist/migrate.d.ts.map +1 -0
  121. package/dist/migrate.js +155 -0
  122. package/dist/migrate.js.map +1 -0
  123. package/dist/options.d.ts +5 -1
  124. package/dist/options.d.ts.map +1 -1
  125. package/dist/options.js +23 -4
  126. package/dist/options.js.map +1 -1
  127. package/dist/setup.d.ts +14 -10
  128. package/dist/setup.d.ts.map +1 -1
  129. package/dist/setup.js +25 -92
  130. package/dist/setup.js.map +1 -1
  131. package/dist/timeline.d.ts.map +1 -1
  132. package/dist/timeline.js +11 -0
  133. package/dist/timeline.js.map +1 -1
  134. package/dist/web/command.d.ts +5 -1
  135. package/dist/web/command.d.ts.map +1 -1
  136. package/dist/web/command.js +22 -6
  137. package/dist/web/command.js.map +1 -1
  138. package/dist/web/credentials.d.ts +7 -5
  139. package/dist/web/credentials.d.ts.map +1 -1
  140. package/dist/web/credentials.js +13 -48
  141. package/dist/web/credentials.js.map +1 -1
  142. package/dist/web/doctor.d.ts +1 -1
  143. package/dist/web/doctor.d.ts.map +1 -1
  144. package/dist/web/doctor.js +42 -64
  145. package/dist/web/doctor.js.map +1 -1
  146. package/dist/web/options.d.ts +26 -2
  147. package/dist/web/options.d.ts.map +1 -1
  148. package/dist/web/options.js +54 -18
  149. package/dist/web/options.js.map +1 -1
  150. package/dist/web/paths.d.ts +1 -1
  151. package/dist/web/paths.d.ts.map +1 -1
  152. package/dist/web/paths.js +2 -2
  153. package/dist/web/paths.js.map +1 -1
  154. package/dist/web/web.d.ts +38 -16
  155. package/dist/web/web.d.ts.map +1 -1
  156. package/dist/web/web.js +166 -168
  157. package/dist/web/web.js.map +1 -1
  158. package/package.json +4 -3
  159. package/web/brand/lib/client.js +4 -4
  160. package/web/patches/gbh-ui.patch.yml +6 -24
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Boot progress for every surface that waits on the harness (~20-40 s on this machine).
3
+ *
4
+ * Booting is three real phases and one spinner: the launcher mounts plugins, writes
5
+ * the composed config, then the host serves and prints its URL. The launcher already
6
+ * says which of those it reached in its own log, so the milestones are derived from
7
+ * that log rather than invented by a timer.
8
+ *
9
+ * ONE line, ONE wording, ONE erase for both surfaces: `blackhole web` and
10
+ * `blackhole chat` paint the same spinner and brand here, and both clear it the same
11
+ * way. A terminal gets one line rewritten in place; `chat` paints it on the last row,
12
+ * which is where the TUI's own status row sits, so the screen does not jump when the
13
+ * terminal takes over. A pipe gets nothing at all, because a progress spinner in a log
14
+ * file is noise.
15
+ */
16
+ /** One named milestone of a harness boot. */
17
+ export interface BootPhase {
18
+ /** Stable id, used by tests and by the phase table. */
19
+ readonly id: "plugins" | "config" | "host";
20
+ /** The Vietnamese line the user reads. */
21
+ readonly label: string;
22
+ }
23
+ /** The milestones in the order a boot reaches them. */
24
+ export declare const BOOT_PHASES: readonly BootPhase[];
25
+ /** The brand both surfaces print beside the milestone. */
26
+ export declare const BOOT_BRAND = "Galaxy Blackhole";
27
+ /** The spinner frames, shared by every surface. */
28
+ export declare const SPINNER_FRAMES: readonly string[];
29
+ /**
30
+ * The frame for one tick.
31
+ * @param tick - a counter that only ever grows.
32
+ * @returns one spinner glyph.
33
+ */
34
+ export declare function spinnerFrame(tick: number): string;
35
+ /**
36
+ * The one boot line every surface paints: spinner, brand, milestone.
37
+ * @param tick - the spinner counter.
38
+ * @param label - the milestone the launcher proved.
39
+ * @returns the text of the line, without any cursor control.
40
+ */
41
+ export declare function bootLine(tick: number, label: string): string;
42
+ /** Erase the whole current line, the sequence both surfaces end a boot with. */
43
+ export declare const ERASE_LINE = "\r\u001B[2K";
44
+ /**
45
+ * Which milestone one launcher log line has reached, if it names one.
46
+ *
47
+ * The launcher prints its banner before it mounts plugins ("[bh] node", "[bh] harness",
48
+ * "[bh] argv"), the preset probes while plugins load ("agent-preset[...]"), and the
49
+ * web app last ("dsh web:" / "gbh web:"). Anything else keeps the current milestone.
50
+ * @param line - one line of launcher output, without its newline.
51
+ * @returns the phase the line proves, or undefined when it proves none.
52
+ */
53
+ export declare function bootPhaseFor(line: string): BootPhase | undefined;
54
+ /** What a boot reporter offers its caller. */
55
+ export interface BootReporter {
56
+ /** The milestone currently shown. */
57
+ readonly label: string;
58
+ /** Move to another milestone (never backwards); repeats are ignored. */
59
+ step(label: string): void;
60
+ /** Stop the spinner and erase the line. */
61
+ clear(): void;
62
+ }
63
+ /** Where a reporter writes, and how often it redraws. */
64
+ export interface BootReporterOptions {
65
+ /** Milliseconds between frames; 100 reads as motion without flicker. */
66
+ readonly intervalMs?: number;
67
+ /** Force the carriage-return surface on or off; defaults to the stream TTY flag. */
68
+ readonly tty?: boolean;
69
+ /**
70
+ * Paint on the terminal's last row instead of the current one. The TUI's status row
71
+ * lives there, so the boot line and the screen that replaces it occupy one row.
72
+ */
73
+ readonly bottom?: boolean;
74
+ }
75
+ /**
76
+ * The one boot reporter, used by `blackhole web` and `blackhole chat` alike.
77
+ *
78
+ * The line is rewritten in place with a carriage return, so a 25-second boot costs one
79
+ * row instead of a wall of log. On a terminal with rows, `bottom` parks that row at the
80
+ * bottom of the screen, where the TUI's status row will be.
81
+ * @param stream - the stream the line belongs to.
82
+ * @param initial - the first milestone label.
83
+ * @param options - interval, an explicit TTY override, and the bottom-row choice.
84
+ * @returns the reporter the caller advances.
85
+ */
86
+ export declare function createBootReporter(stream: NodeJS.WriteStream, initial: string, options?: BootReporterOptions): BootReporter;
87
+ //# sourceMappingURL=boot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boot.d.ts","sourceRoot":"","sources":["../../src/harness/boot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,6CAA6C;AAC7C,MAAM,WAAW,SAAS;IACxB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,EAAE,SAAS,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC3C,0CAA0C;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,uDAAuD;AACvD,eAAO,MAAM,WAAW,EAAE,SAAS,SAAS,EAI1C,CAAC;AAEH,0DAA0D;AAC1D,eAAO,MAAM,UAAU,qBAAqB,CAAC;AAE7C,mDAAmD;AACnD,eAAO,MAAM,cAAc,EAAE,SAAS,MAAM,EAAsE,CAAC;AAEnH;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED,gFAAgF;AAChF,eAAO,MAAM,UAAU,gBAAgB,CAAC;AAExC;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAKhE;AAED,8CAA8C;AAC9C,MAAM,WAAW,YAAY;IAC3B,qCAAqC;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,2CAA2C;IAC3C,KAAK,IAAI,IAAI,CAAC;CACf;AAED,yDAAyD;AACzD,MAAM,WAAW,mBAAmB;IAClC,wEAAwE;IACxE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,oFAAoF;IACpF,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,CAAC,WAAW,EAC1B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,mBAAwB,GAChC,YAAY,CA8Bd"}
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Boot progress for every surface that waits on the harness (~20-40 s on this machine).
3
+ *
4
+ * Booting is three real phases and one spinner: the launcher mounts plugins, writes
5
+ * the composed config, then the host serves and prints its URL. The launcher already
6
+ * says which of those it reached in its own log, so the milestones are derived from
7
+ * that log rather than invented by a timer.
8
+ *
9
+ * ONE line, ONE wording, ONE erase for both surfaces: `blackhole web` and
10
+ * `blackhole chat` paint the same spinner and brand here, and both clear it the same
11
+ * way. A terminal gets one line rewritten in place; `chat` paints it on the last row,
12
+ * which is where the TUI's own status row sits, so the screen does not jump when the
13
+ * terminal takes over. A pipe gets nothing at all, because a progress spinner in a log
14
+ * file is noise.
15
+ */
16
+ /** The milestones in the order a boot reaches them. */
17
+ export const BOOT_PHASES = Object.freeze([
18
+ Object.freeze({ id: "plugins", label: "Đang nạp plugin…" }),
19
+ Object.freeze({ id: "config", label: "Đang dựng cấu hình…" }),
20
+ Object.freeze({ id: "host", label: "Đang chờ host…" }),
21
+ ]);
22
+ /** The brand both surfaces print beside the milestone. */
23
+ export const BOOT_BRAND = "Galaxy Blackhole";
24
+ /** The spinner frames, shared by every surface. */
25
+ export const SPINNER_FRAMES = Object.freeze(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]);
26
+ /**
27
+ * The frame for one tick.
28
+ * @param tick - a counter that only ever grows.
29
+ * @returns one spinner glyph.
30
+ */
31
+ export function spinnerFrame(tick) {
32
+ return SPINNER_FRAMES[Math.abs(tick) % SPINNER_FRAMES.length] ?? SPINNER_FRAMES[0];
33
+ }
34
+ /**
35
+ * The one boot line every surface paints: spinner, brand, milestone.
36
+ * @param tick - the spinner counter.
37
+ * @param label - the milestone the launcher proved.
38
+ * @returns the text of the line, without any cursor control.
39
+ */
40
+ export function bootLine(tick, label) {
41
+ return spinnerFrame(tick) + " " + BOOT_BRAND + " · " + label;
42
+ }
43
+ /** Erase the whole current line, the sequence both surfaces end a boot with. */
44
+ export const ERASE_LINE = "\r\u001b[2K";
45
+ /**
46
+ * Which milestone one launcher log line has reached, if it names one.
47
+ *
48
+ * The launcher prints its banner before it mounts plugins ("[bh] node", "[bh] harness",
49
+ * "[bh] argv"), the preset probes while plugins load ("agent-preset[...]"), and the
50
+ * web app last ("dsh web:" / "gbh web:"). Anything else keeps the current milestone.
51
+ * @param line - one line of launcher output, without its newline.
52
+ * @returns the phase the line proves, or undefined when it proves none.
53
+ */
54
+ export function bootPhaseFor(line) {
55
+ if (/^(?:dsh|gbh) web: |devtools:/.test(line))
56
+ return BOOT_PHASES[2];
57
+ if (/^agent-preset\[/.test(line) || /mount|plugin|alias/.test(line))
58
+ return BOOT_PHASES[0];
59
+ if (/^\[bh\] (?:node|harness|DSH_HOME|profile|patch|rewrite|argv)\b/.test(line) || /^\[bh\] (?:doi id|chep icon)/.test(line))
60
+ return BOOT_PHASES[1];
61
+ return undefined;
62
+ }
63
+ /**
64
+ * The one boot reporter, used by `blackhole web` and `blackhole chat` alike.
65
+ *
66
+ * The line is rewritten in place with a carriage return, so a 25-second boot costs one
67
+ * row instead of a wall of log. On a terminal with rows, `bottom` parks that row at the
68
+ * bottom of the screen, where the TUI's status row will be.
69
+ * @param stream - the stream the line belongs to.
70
+ * @param initial - the first milestone label.
71
+ * @param options - interval, an explicit TTY override, and the bottom-row choice.
72
+ * @returns the reporter the caller advances.
73
+ */
74
+ export function createBootReporter(stream, initial, options = {}) {
75
+ const intervalMs = Math.max(16, options.intervalMs ?? 100);
76
+ const tty = options.tty ?? stream.isTTY === true;
77
+ const rows = options.bottom === true ? (stream.rows ?? 0) : 0;
78
+ // Moving to the bottom row costs one escape; erasing still happens in place.
79
+ const place = rows > 0 ? "\u001b[" + String(rows) + ";1H" : "";
80
+ let label = initial;
81
+ let tick = 0;
82
+ let stopped = false;
83
+ const paint = () => {
84
+ if (!tty || stopped)
85
+ return;
86
+ stream.write(place + ERASE_LINE + bootLine(tick, label));
87
+ };
88
+ const timer = tty ? setInterval(() => { tick += 1; paint(); }, intervalMs) : undefined;
89
+ if (timer !== undefined)
90
+ timer.unref?.();
91
+ paint();
92
+ return {
93
+ get label() { return label; },
94
+ step(next) {
95
+ if (stopped || next === label)
96
+ return;
97
+ label = next;
98
+ paint();
99
+ },
100
+ clear() {
101
+ if (stopped)
102
+ return;
103
+ stopped = true;
104
+ if (timer !== undefined)
105
+ clearInterval(timer);
106
+ if (tty)
107
+ stream.write(place + ERASE_LINE);
108
+ },
109
+ };
110
+ }
111
+ //# sourceMappingURL=boot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boot.js","sourceRoot":"","sources":["../../src/harness/boot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;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AAG,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACvF,IAAI,KAAK,KAAK,SAAS;QAAE,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;IACzC,KAAK,EAAE,CAAC;IACR,OAAO;QACL,IAAI,KAAK,KAAa,OAAO,KAAK,CAAC,CAAC,CAAC;QACrC,IAAI,CAAC,IAAY;YACf,IAAI,OAAO,IAAI,IAAI,KAAK,KAAK;gBAAE,OAAO;YACtC,KAAK,GAAG,IAAI,CAAC;YACb,KAAK,EAAE,CAAC;QACV,CAAC;QACD,KAAK;YACH,IAAI,OAAO;gBAAE,OAAO;YACpB,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,KAAK,KAAK,SAAS;gBAAE,aAAa,CAAC,KAAK,CAAC,CAAC;YAC9C,IAAI,GAAG;gBAAE,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC;QAC5C,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The two host catalogs the composer reads: permission presets and models.
3
+ *
4
+ * Both answers are additive across host versions — every field is optional — so the
5
+ * shapes here mirror the wire loosely and the screen only ever sees the folded result.
6
+ * `flattenModels` is the one interesting fold: the catalog groups models by provider,
7
+ * and the menu wants one flat list with each route's default effort and every effort
8
+ * that route accepts, because the reasoning submenu is opened from a model row.
9
+ */
10
+ import type { ModelOption } from "./parts.js";
11
+ /** The permission catalog, as far as this terminal reads it. */
12
+ export interface PermissionCatalogLike {
13
+ readonly options?: readonly {
14
+ readonly value?: string;
15
+ readonly name?: string;
16
+ }[];
17
+ readonly defaultPreset?: string;
18
+ }
19
+ /** The model catalog, as far as this terminal reads it. */
20
+ export interface ModelCatalogLike {
21
+ readonly default?: {
22
+ readonly provider?: string;
23
+ readonly model?: string;
24
+ readonly reasoningEffort?: string;
25
+ };
26
+ readonly groups?: readonly {
27
+ readonly id?: string;
28
+ readonly name?: string;
29
+ readonly models?: readonly {
30
+ readonly id?: string;
31
+ readonly name?: string;
32
+ readonly description?: string;
33
+ readonly reasoning?: {
34
+ readonly defaultEffort?: string;
35
+ readonly efforts?: readonly {
36
+ readonly id?: string;
37
+ readonly name?: string;
38
+ readonly description?: string;
39
+ }[];
40
+ };
41
+ }[];
42
+ }[];
43
+ }
44
+ /**
45
+ * Flatten the provider groups into one menu list, keeping each route's default effort.
46
+ * @param catalog - the host's `session.modelCatalog` answer.
47
+ * @returns one row per provider/model route, in catalog order.
48
+ */
49
+ export declare function flattenModels(catalog: ModelCatalogLike): readonly ModelOption[];
50
+ //# sourceMappingURL=catalogs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalogs.d.ts","sourceRoot":"","sources":["../../src/harness/catalogs.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,gEAAgE;AAChE,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAClF,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;CACjC;AAED,2DAA2D;AAC3D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9G,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS;QACzB,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS;YACzB,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;YACrB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;YACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;YAC9B,QAAQ,CAAC,SAAS,CAAC,EAAE;gBACnB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;gBAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS;oBAAE,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;oBAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;oBAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;iBAAE,EAAE,CAAC;aAC/G,CAAC;SACH,EAAE,CAAC;KACL,EAAE,CAAC;CACL;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,WAAW,EAAE,CAyB/E"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Flatten the provider groups into one menu list, keeping each route's default effort.
3
+ * @param catalog - the host's `session.modelCatalog` answer.
4
+ * @returns one row per provider/model route, in catalog order.
5
+ */
6
+ export function flattenModels(catalog) {
7
+ const options = [];
8
+ for (const group of catalog.groups ?? []) {
9
+ const provider = group.id;
10
+ if (provider === undefined)
11
+ continue;
12
+ for (const model of group.models ?? []) {
13
+ if (model.id === undefined)
14
+ continue;
15
+ const effort = model.reasoning?.defaultEffort;
16
+ const effortName = effort === undefined ? undefined : model.reasoning?.efforts?.find(entry => entry.id === effort)?.name;
17
+ const efforts = (model.reasoning?.efforts ?? []).flatMap(entry => (entry.id === undefined
18
+ ? []
19
+ : [{ id: entry.id, name: entry.name ?? entry.id, ...(entry.description === undefined ? {} : { description: entry.description }) }]));
20
+ options.push({
21
+ provider,
22
+ model: model.id,
23
+ name: model.name ?? model.id,
24
+ ...(model.description === undefined ? {} : { description: model.description }),
25
+ ...(effort === undefined ? {} : { effort }),
26
+ ...(effortName === undefined ? {} : { effortName }),
27
+ ...(efforts.length === 0 ? {} : { efforts }),
28
+ ...(group.name === undefined ? {} : { hint: group.name }),
29
+ });
30
+ }
31
+ }
32
+ return options;
33
+ }
34
+ //# sourceMappingURL=catalogs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalogs.js","sourceRoot":"","sources":["../../src/harness/catalogs.ts"],"names":[],"mappings":"AAmCA;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,OAAyB;IACrD,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC;QACzC,MAAM,QAAQ,GAAG,KAAK,CAAC,EAAE,CAAC;QAC1B,IAAI,QAAQ,KAAK,SAAS;YAAE,SAAS;QACrC,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC;YACvC,IAAI,KAAK,CAAC,EAAE,KAAK,SAAS;gBAAE,SAAS;YACrC,MAAM,MAAM,GAAG,KAAK,CAAC,SAAS,EAAE,aAAa,CAAC;YAC9C,MAAM,UAAU,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,MAAM,CAAC,EAAE,IAAI,CAAC;YACzH,MAAM,OAAO,GAAG,CAAC,KAAK,CAAC,SAAS,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,SAAS;gBACvF,CAAC,CAAC,EAAE;gBACJ,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YACvI,OAAO,CAAC,IAAI,CAAC;gBACX,QAAQ;gBACR,KAAK,EAAE,KAAK,CAAC,EAAE;gBACf,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE;gBAC5B,GAAG,CAAC,KAAK,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC;gBAC9E,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;gBAC3C,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC;gBACnD,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC;gBAC5C,GAAG,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;aAC1D,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,90 @@
1
+ /**
2
+ * One conversation on the Galaxy Blackhole harness.
3
+ *
4
+ * A turn is: open the session's follow stream, send the prompt, translate the
5
+ * harness's durable events (and its live assistant chunks) into terminal-sized
6
+ * facts, and stop at `turn/end`. Nothing here reaches for the old ai-coder-core
7
+ * runtime: the model, the tools and the transcript all live in the harness.
8
+ */
9
+ import type { HarnessClient } from "./client.js";
10
+ import { type TodoItem } from "./history.js";
11
+ /** The model call this turn used, when the harness reported one. */
12
+ export interface ChatModelFact {
13
+ readonly provider?: string;
14
+ readonly model?: string;
15
+ }
16
+ /** One fact the terminal can render. */
17
+ export type ChatChunk = {
18
+ readonly kind: "status";
19
+ readonly label: string;
20
+ } | {
21
+ readonly kind: "text";
22
+ readonly text: string;
23
+ } | {
24
+ readonly kind: "reasoning";
25
+ readonly text: string;
26
+ } | {
27
+ readonly kind: "tool-start";
28
+ readonly id: string;
29
+ readonly name: string;
30
+ readonly args: Readonly<Record<string, unknown>>;
31
+ } | {
32
+ readonly kind: "tool-end";
33
+ readonly id: string;
34
+ readonly ok: boolean;
35
+ readonly summary: string;
36
+ } | {
37
+ readonly kind: "todo";
38
+ readonly todos: readonly TodoItem[];
39
+ } | {
40
+ readonly kind: "meta";
41
+ readonly sessionId: string;
42
+ readonly durationMs: number;
43
+ readonly model?: ChatModelFact;
44
+ readonly tokens?: number;
45
+ } | {
46
+ readonly kind: "error";
47
+ readonly message: string;
48
+ };
49
+ /** How one turn ended. */
50
+ export interface ChatTurnResult {
51
+ readonly state: "completed" | "aborted" | "error";
52
+ /** Everything the assistant said, joined. */
53
+ readonly text: string;
54
+ readonly error?: string;
55
+ readonly sessionId: string;
56
+ readonly turn?: number;
57
+ }
58
+ /** One followable session and its next turn. */
59
+ export declare class HarnessChat {
60
+ private readonly client;
61
+ private readonly options;
62
+ private sessionId;
63
+ private baselineTurn;
64
+ private model;
65
+ constructor(client: HarnessClient, options: Readonly<{
66
+ cwd: string;
67
+ model?: string;
68
+ sessionId?: string;
69
+ preset?: string;
70
+ }>);
71
+ /**
72
+ * The harness session behind this conversation.
73
+ * @returns the session named at construction, or the draft created on first use.
74
+ */
75
+ ensureSession(): Promise<string>;
76
+ /** Facts for the /session panel. */
77
+ info(): Readonly<{
78
+ sessionId?: string;
79
+ base: string;
80
+ }>;
81
+ /**
82
+ * Send one prompt and translate the turn.
83
+ * @param goal - the text the user typed.
84
+ * @param onChunk - called for every renderable fact, in order.
85
+ * @param signal - cancellation; the turn is cancelled on the host too.
86
+ * @returns the settled turn.
87
+ */
88
+ send(goal: string, onChunk: (chunk: ChatChunk) => void, signal: AbortSignal): Promise<ChatTurnResult>;
89
+ }
90
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/harness/chat.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAA4B,aAAa,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAGxD,oEAAoE;AACpE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,wCAAwC;AACxC,MAAM,MAAM,SAAS,GACjB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;CA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AAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAM5J;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAUtC,oCAAoC;IACpC,IAAI,IAAI,QAAQ,CAAC;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAItD;;;;;;OAMG;IACG,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,cAAc,CAAC;CA0I5G"}
@@ -0,0 +1,260 @@
1
+ import { todoItems } from "./history.js";
2
+ import { sessionCreateRequest } from "./preset.js";
3
+ function record(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
5
+ }
6
+ function text(value) {
7
+ return typeof value === "string" && value.length > 0 ? value : undefined;
8
+ }
9
+ function number(value) {
10
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
11
+ }
12
+ /** Visible text blocks of one assistant or tool message. */
13
+ function contentText(value) {
14
+ const message = record(value);
15
+ const content = message?.["content"];
16
+ if (!Array.isArray(content))
17
+ return typeof content === "string" ? content : "";
18
+ return content
19
+ .map(part => {
20
+ const block = record(part);
21
+ return block?.["type"] === "text" ? text(block["text"]) ?? "" : "";
22
+ })
23
+ .join("");
24
+ }
25
+ /** Reasoning blocks of one assistant message. */
26
+ function contentReasoning(value) {
27
+ const message = record(value);
28
+ const content = message?.["content"];
29
+ if (!Array.isArray(content))
30
+ return "";
31
+ return content
32
+ .map(part => {
33
+ const block = record(part);
34
+ return block?.["type"] === "reasoning" ? text(block["text"]) ?? "" : "";
35
+ })
36
+ .join("");
37
+ }
38
+ /** Parse the raw arguments string a tool call carries. */
39
+ function toolArguments(value) {
40
+ if (typeof value === "string") {
41
+ try {
42
+ const parsed = JSON.parse(value);
43
+ return record(parsed) ?? { raw: value };
44
+ }
45
+ catch {
46
+ return { raw: value };
47
+ }
48
+ }
49
+ return record(value) ?? {};
50
+ }
51
+ /** A live stream text delta, when the frame carries one. */
52
+ function deltaOf(frame) {
53
+ if (frame.type !== "assistant-stream")
54
+ return undefined;
55
+ const chunk = frame.frame?.chunk;
56
+ return chunk?.type === "text-delta" ? text(chunk.text) : undefined;
57
+ }
58
+ /** One followable session and its next turn. */
59
+ export class HarnessChat {
60
+ client;
61
+ options;
62
+ sessionId;
63
+ baselineTurn = 0;
64
+ model = {};
65
+ constructor(client, options) {
66
+ this.client = client;
67
+ this.options = options;
68
+ // An opened conversation names its session up front; `session.prompt` resumes it, so the
69
+ // terminal never creates a sibling under the same identity just to read it.
70
+ this.sessionId = options.sessionId;
71
+ }
72
+ /**
73
+ * The harness session behind this conversation.
74
+ * @returns the session named at construction, or the draft created on first use.
75
+ */
76
+ async ensureSession() {
77
+ if (this.sessionId !== undefined)
78
+ return this.sessionId;
79
+ // The mode the composer shows is part of the creation body: a session born on the host's
80
+ // default and re-pointed afterwards is a mode the user never really chose.
81
+ const body = sessionCreateRequest(this.options.cwd, this.options.preset);
82
+ const value = await this.client.call("session.create", { request: body.request });
83
+ this.sessionId = value.sessionId;
84
+ return value.sessionId;
85
+ }
86
+ /** Facts for the /session panel. */
87
+ info() {
88
+ return Object.freeze({ ...(this.sessionId === undefined ? {} : { sessionId: this.sessionId }), base: this.client.base });
89
+ }
90
+ /**
91
+ * Send one prompt and translate the turn.
92
+ * @param goal - the text the user typed.
93
+ * @param onChunk - called for every renderable fact, in order.
94
+ * @param signal - cancellation; the turn is cancelled on the host too.
95
+ * @returns the settled turn.
96
+ */
97
+ async send(goal, onChunk, signal) {
98
+ const sessionId = await this.ensureSession();
99
+ const startedAt = Date.now();
100
+ const controller = new AbortController();
101
+ const turnSignal = AbortSignal.any([signal, controller.signal]);
102
+ const iterator = this.client
103
+ .stream("session/follow", { request: { address: { kind: "session", sessionId }, assistantStream: true } }, turnSignal)[Symbol.asyncIterator]();
104
+ let turn = 0;
105
+ let liveText = "";
106
+ let answer = "";
107
+ let state = "completed";
108
+ let failure;
109
+ let tokens;
110
+ const emit = (chunk) => onChunk(chunk);
111
+ const applyEvent = (event) => {
112
+ if (event.type === "turn/start") {
113
+ turn = number(record(event.data)?.["turn"]) ?? turn + 1;
114
+ liveText = "";
115
+ emit({ kind: "status", label: "Đang xử lý…" });
116
+ }
117
+ if (event.type === "step/start") {
118
+ liveText = "";
119
+ emit({ kind: "status", label: "Model đang trả lời…" });
120
+ }
121
+ if (event.type === "request/header") {
122
+ const config = record(record(event.data)?.["header"])?.["config"];
123
+ const provider = text(record(config)?.["provider"]);
124
+ const model = text(record(config)?.["model"]);
125
+ if (provider !== undefined)
126
+ this.model = { ...this.model, provider };
127
+ if (model !== undefined)
128
+ this.model = { ...this.model, model };
129
+ }
130
+ if (event.type === "assistant/message") {
131
+ const data = record(event.data);
132
+ const full = contentText(data?.["message"]);
133
+ const reasoning = contentReasoning(data?.["message"]);
134
+ if (reasoning.length > 0)
135
+ emit({ kind: "reasoning", text: reasoning });
136
+ if (full.length > 0) {
137
+ const remainder = liveText.length === 0 ? full : full.startsWith(liveText) ? full.slice(liveText.length) : "";
138
+ if (remainder.length > 0) {
139
+ answer += remainder;
140
+ emit({ kind: "text", text: remainder });
141
+ }
142
+ }
143
+ const usage = record(data?.["usage"]);
144
+ const input = number(usage?.["inputTokens"]);
145
+ const output = number(usage?.["outputTokens"]);
146
+ if (input !== undefined || output !== undefined)
147
+ tokens = (input ?? 0) + (output ?? 0);
148
+ }
149
+ if (event.type === "tool/call") {
150
+ const data = record(event.data);
151
+ const id = text(data?.["callId"]);
152
+ const name = text(data?.["name"]);
153
+ if (id !== undefined && name !== undefined)
154
+ emit({ kind: "tool-start", id, name, args: toolArguments(data?.["arguments"]) });
155
+ }
156
+ // The plan is a durable fact, not a tool call: in PTC mode the model writes a program that
157
+ // calls `todo_write`, so the only event carrying the list is `todo/write`.
158
+ if (event.type === "todo/write") {
159
+ const todos = todoItems(record(event.data)?.["todos"]);
160
+ if (todos !== undefined)
161
+ emit({ kind: "todo", todos });
162
+ }
163
+ if (event.type === "tool/result") {
164
+ const data = record(event.data);
165
+ const message = record(data?.["message"]);
166
+ const id = text(message?.["toolCallId"]);
167
+ if (id !== undefined) {
168
+ const summary = contentText(message).slice(0, 800);
169
+ emit({ kind: "tool-end", id, ok: message?.["isError"] !== true && data?.["error"] === undefined, summary });
170
+ }
171
+ }
172
+ if (event.type === "turn/end") {
173
+ const reason = record(record(event.data)?.["reason"]);
174
+ const kind = text(reason?.["kind"]);
175
+ const endedTurn = number(record(event.data)?.["turn"]) ?? turn;
176
+ if (endedTurn <= this.baselineTurn)
177
+ return false;
178
+ state = kind === "completed" ? "completed" : kind === "aborted" ? "aborted" : "error";
179
+ failure = text(record(reason?.["error"])?.["message"]);
180
+ emit({ kind: "status", label: state === "completed" ? "" : state === "aborted" ? "Đã dừng" : "Có lỗi" });
181
+ return true;
182
+ }
183
+ return false;
184
+ };
185
+ try {
186
+ const opening = await iterator.next();
187
+ const snapshot = opening.done === true ? undefined : opening.value;
188
+ for (const item of snapshot?.records ?? []) {
189
+ if (item.event.type !== "turn/start")
190
+ continue;
191
+ this.baselineTurn = Math.max(this.baselineTurn, number(record(item.event.data)?.["turn"]) ?? 0);
192
+ }
193
+ emit({ kind: "status", label: "Đang gửi…" });
194
+ await this.client.call("session.prompt", {
195
+ request: {
196
+ requestId: "bhcli-" + String(Date.now()) + "-" + Math.random().toString(36).slice(2, 8),
197
+ sessionId,
198
+ mode: "queue",
199
+ content: [{ type: "text", text: goal }],
200
+ },
201
+ }, turnSignal);
202
+ while (true) {
203
+ const next = await iterator.next();
204
+ if (next.done === true)
205
+ break;
206
+ const frame = next.value;
207
+ const delta = deltaOf(frame);
208
+ if (delta !== undefined) {
209
+ liveText += delta;
210
+ answer += delta;
211
+ emit({ kind: "text", text: delta });
212
+ continue;
213
+ }
214
+ if (frame.type === "event" && frame.event !== undefined && applyEvent(frame.event))
215
+ break;
216
+ }
217
+ if (signal.aborted && state === "completed") {
218
+ state = "aborted";
219
+ void this.client.call("session.cancel", { request: { sessionId } }).catch(() => undefined);
220
+ }
221
+ }
222
+ catch (error) {
223
+ if (turnSignal.aborted) {
224
+ state = "aborted";
225
+ void this.client.call("session.cancel", { request: { sessionId } }).catch(() => undefined);
226
+ }
227
+ else {
228
+ state = "error";
229
+ failure = error instanceof Error ? error.message : String(error);
230
+ emit({ kind: "error", message: failure });
231
+ }
232
+ }
233
+ finally {
234
+ controller.abort();
235
+ try {
236
+ await iterator.return?.();
237
+ }
238
+ catch { /* the stream is already closed */ }
239
+ }
240
+ if (state === "aborted") {
241
+ emit({ kind: "status", label: "Đã dừng" });
242
+ answer = answer.length > 0 ? answer : "(đã dừng)";
243
+ }
244
+ emit({
245
+ kind: "meta",
246
+ sessionId,
247
+ durationMs: Date.now() - startedAt,
248
+ ...(this.model.model === undefined && this.options.model === undefined ? {} : { model: this.model.model === undefined ? { model: this.options.model } : this.model }),
249
+ ...(tokens === undefined ? {} : { tokens }),
250
+ });
251
+ return Object.freeze({
252
+ state,
253
+ text: answer,
254
+ sessionId,
255
+ ...(turn === 0 ? {} : { turn }),
256
+ ...(failure === undefined ? {} : { error: failure }),
257
+ });
258
+ }
259
+ }
260
+ //# sourceMappingURL=chat.js.map