@tiens.nguyen/gu-cli 1.0.818 → 1.0.820

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/gu-repl.mjs CHANGED
@@ -154,6 +154,7 @@ import {
154
154
  parseModelFallback,
155
155
  } from "./model-fallback.mjs";
156
156
  import { codingChoiceTags, coderDisplayName, coderWhere, whereOfActiveCoder } from "./coding-choice-tags.mjs";
157
+ import { TITLE_POP, TITLE_PUSH, idleTitle, needsSummary, summariseForTitle, titleSeq, trimTitle } from "./terminal-title.mjs";
157
158
  import { IMAGE_NEEDS_CLAUDE, coderTakesImagesFor, clipboardImageToFile, fitImageNotice, imageMarkersForDisplay, createImageStore, imagePasteKey, imagePathFromPaste, isClaudeCoderUrl, isImagePasteKey, storeImageFile } from "./image-paste.mjs";
158
159
  import {
159
160
  isDoubleClick,
@@ -1603,6 +1604,40 @@ if (IS_TTY) {
1603
1604
  process.on("exit", () => _stdoutWrite(PASTE_MODE_OFF));
1604
1605
  }
1605
1606
 
1607
+ // THE TAB SAYS WHAT IS BEING WORKED ON (task #357) — see terminal-title.mjs. Until the first
1608
+ // question it names the folder, which is already more than the `node /opt/homebrew/…/gu` a
1609
+ // terminal shows for a program that never names itself.
1610
+ const TITLE_ON = IS_TTY && process.env.GU_NO_TITLE !== "1";
1611
+ let _titleSeq = 0; // which question the title belongs to; a late summary for an older one is dropped
1612
+ const writeTitle = (text) => {
1613
+ if (TITLE_ON) ui(titleSeq(text));
1614
+ };
1615
+ if (TITLE_ON) {
1616
+ ui(TITLE_PUSH);
1617
+ writeTitle(idleTitle(process.cwd()));
1618
+ // Hand the title back: restore the one that was there where the terminal keeps a stack, and
1619
+ // clear ours where it does not, so the shell is not left wearing the last question.
1620
+ process.on("exit", () => _stdoutWrite(titleSeq("") + TITLE_POP));
1621
+ }
1622
+
1623
+ /**
1624
+ * A new question: the trimmed text now, and — only when it had to be cut — the agent model's
1625
+ * few-word summary whenever it arrives. Fire-and-forget by design: it never delays the turn,
1626
+ * never prints, and a summary that lands after the NEXT question is thrown away.
1627
+ */
1628
+ function showQuestionInTitle(question) {
1629
+ if (!TITLE_ON) return;
1630
+ const mine = ++_titleSeq;
1631
+ writeTitle(trimTitle(question));
1632
+ if (!needsSummary(question)) return;
1633
+ void (async () => {
1634
+ const settings = await fetchWorkerSettings();
1635
+ if (!settings || mine !== _titleSeq) return;
1636
+ const title = await summariseForTitle(question, settings, { fetchImpl: doctorFetch });
1637
+ if (title && mine === _titleSeq) writeTitle(title);
1638
+ })().catch(() => {});
1639
+ }
1640
+
1606
1641
  // ">>" is a MARGIN MARKER like the ● bullets — it hangs in the gutter (no leading gutter) so
1607
1642
  // the user's typed input lands on the SAME text column (GUTTER+1) as the agent's bullets and
1608
1643
  // answer. ">> " is exactly GUTTER-wide, so input aligns; the ">>" right-edge sits at the same
@@ -14348,6 +14383,9 @@ async function main() {
14348
14383
  history.push({ role: "user", content: taskLine });
14349
14384
  }
14350
14385
  await saveSession(cwd, history, { unfinishedTask: isResumeWord(taskLine) ? "" : taskLine });
14386
+ // #357: the tab is this question until the next one. A bare "continue" with nothing to
14387
+ // resume says nothing about the work, so the title it follows stays.
14388
+ if (!isResumeWord(taskLine)) showQuestionInTitle(taskLine);
14351
14389
  try {
14352
14390
  const turnStartMs = Date.now();
14353
14391
  // #323: priced by the coder this turn STARTS on — a /model switch afterwards must not put
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiens.nguyen/gu-cli",
3
- "version": "1.0.818",
3
+ "version": "1.0.820",
4
4
  "description": "Gu CLI — the gu terminal plus the local worker that runs agent / OCR / PDF / embedding jobs on your Mac (Ollama / MLX / OpenAI-compatible).",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -115,6 +115,7 @@
115
115
  "model-doctor.mjs",
116
116
  "coding-key-prompt.mjs",
117
117
  "model-fallback.mjs",
118
+ "terminal-title.mjs",
118
119
  "server-keys.mjs",
119
120
  "add-server.mjs",
120
121
  "powershell-alias.mjs",
@@ -0,0 +1,165 @@
1
+ /**
2
+ * THE TERMINAL'S TITLE IS THE QUESTION BEING WORKED ON (task #357).
3
+ *
4
+ * Reported: the tab said `node /opt/homebrew/…/gu` — where the gu bin lives, which is what a
5
+ * terminal shows when the program inside never names itself. With several gu tabs open that is
6
+ * the same useless string on every one. The ask: show the current turn's question instead,
7
+ * shortened, and keep it there until the next question.
8
+ *
9
+ * TWO STEPS, AND THE FIRST NEVER WAITS FOR THE SECOND. The trimmed question goes up the moment
10
+ * the turn starts. Only a question too long to fit is then sent to the agent model for a
11
+ * few-word summary, which replaces the trimmed one if — and only if — it arrives usable and the
12
+ * user has not asked something else in the meantime. Every way that call can fail (no model
13
+ * configured, a provider that wants a key this side does not hold, a timeout, a model that
14
+ * answers with an essay) ends the same way: the trimmed question stays.
15
+ *
16
+ * WHY SHORT QUESTIONS ARE NOT SUMMARISED. The agent model is the one the turn itself is about to
17
+ * use, and a single-GPU Ollama queues requests rather than running them side by side — a summary
18
+ * of "fix the login test" would cost the turn its first seconds to say the same thing in fewer
19
+ * words. A question that already fits is its own best title.
20
+ *
21
+ * Pure: no terminal and no network of its own. `fetchImpl` is injected, and the caller writes
22
+ * the bytes.
23
+ */
24
+
25
+ /** What fits on a tab next to its neighbours. Terminals clip a title; they do not wrap it. */
26
+ export const TITLE_MAX = 60;
27
+
28
+ /** How much of a long question the summary is given — the ask is in the opening lines. */
29
+ export const SUMMARY_INPUT_MAX = 1200;
30
+
31
+ const ELLIPSIS = "…";
32
+
33
+ /**
34
+ * One line of plain text, safe to put inside an escape sequence.
35
+ *
36
+ * NOT COSMETIC. The title is written as `ESC ] 0 ; <text> BEL`, so a BEL or ESC inside the text
37
+ * ends the sequence early and the rest is read by the terminal as commands — and the text is
38
+ * whatever was typed or PASTED as a question. Every C0/C1 control character goes, newlines
39
+ * included (as spaces: a pasted stack trace is one title, not twenty).
40
+ */
41
+ export function cleanTitleText(text) {
42
+ return String(text ?? "")
43
+ .replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ")
44
+ .replace(/\s+/g, " ")
45
+ .trim();
46
+ }
47
+
48
+ /** The question cut to fit, on a word boundary when there is one in the last third. */
49
+ export function trimTitle(text, max = TITLE_MAX) {
50
+ const s = cleanTitleText(text);
51
+ const chars = Array.from(s); // by code point: never half of an emoji or a Vietnamese letter
52
+ if (chars.length <= max) return s;
53
+ const cut = chars.slice(0, max - 1).join("");
54
+ const space = cut.lastIndexOf(" ");
55
+ const head = space >= Math.floor((max * 2) / 3) ? cut.slice(0, space) : cut;
56
+ return head.replace(/[\s,;:.\-–—]+$/u, "") + ELLIPSIS;
57
+ }
58
+
59
+ /** Only a question that had to be cut is worth a model call (see the header). */
60
+ export function needsSummary(text, max = TITLE_MAX) {
61
+ return Array.from(cleanTitleText(text)).length > max;
62
+ }
63
+
64
+ /** The bytes that set the window and tab title. "" hands the title back to the terminal. */
65
+ export function titleSeq(text) {
66
+ return `\x1b]0;${cleanTitleText(text)}\x07`;
67
+ }
68
+
69
+ /** xterm's title stack: remember the title that was there, and put it back on the way out.
70
+ * Terminals that do not have one ignore both. */
71
+ export const TITLE_PUSH = "\x1b[22;0t";
72
+ export const TITLE_POP = "\x1b[23;0t";
73
+
74
+ /** What the tab says before anything has been asked. */
75
+ export function idleTitle(folder) {
76
+ const name = cleanTitleText(String(folder ?? "").split(/[\\/]/).filter(Boolean).pop() ?? "");
77
+ return name ? `gu · ${trimTitle(name, TITLE_MAX - 5)}` : "gu";
78
+ }
79
+
80
+ /** Where the agent model answers chat, from a base that may or may not already end in /v1. */
81
+ export function chatEndpoint(url) {
82
+ const root = String(url ?? "").trim().replace(/\/+$/, "");
83
+ if (!root) return "";
84
+ return /\/v1$/i.test(root) ? `${root}/chat/completions` : `${root}/v1/chat/completions`;
85
+ }
86
+
87
+ /**
88
+ * The agent model this machine is set to use, from the settings row.
89
+ *
90
+ * With a direct URL and no name the API itself sends the literal "default_model" (an MLX server
91
+ * answers to anything), so the same is sent here rather than skipping the summary on every MLX
92
+ * host.
93
+ */
94
+ export function agentModelFromSettings(settings) {
95
+ const str = (v) => String(v ?? "").trim();
96
+ const url = str(settings?.agentModelUrl);
97
+ if (!url) return null;
98
+ return { url, model: str(settings?.agentModelName) || "default_model" };
99
+ }
100
+
101
+ export function summaryRequest(model, question) {
102
+ const q = Array.from(cleanTitleText(question)).slice(0, SUMMARY_INPUT_MAX).join("");
103
+ return {
104
+ model,
105
+ stream: false,
106
+ max_tokens: 200, // room for a reasoning model to think briefly and still answer
107
+ messages: [
108
+ {
109
+ role: "system",
110
+ content:
111
+ "You write window titles. Reply with ONLY a title of at most 8 words that says what " +
112
+ "the user's request is about, in the language the request is written in. No quotes, " +
113
+ "no trailing punctuation, no explanation. Do not answer or carry out the request.",
114
+ },
115
+ { role: "user", content: `Request:\n${q}` },
116
+ ],
117
+ };
118
+ }
119
+
120
+ /**
121
+ * The title out of the model's reply, or "" when the reply is not one.
122
+ *
123
+ * A model that ignores the instruction and starts answering the question must not get its first
124
+ * paragraph into the tab, so anything that is not a single short line is refused, and the caller
125
+ * keeps the trimmed question.
126
+ */
127
+ export function pickSummary(body, max = TITLE_MAX) {
128
+ let raw = body?.choices?.[0]?.message?.content;
129
+ if (typeof raw !== "string") return "";
130
+ raw = raw.replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
131
+ const lines = raw.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
132
+ if (lines.length !== 1) return "";
133
+ const line = cleanTitleText(lines[0])
134
+ .replace(/^title\s*:\s*/i, "")
135
+ .replace(/^["'“”‘’`*]+|["'“”‘’`*]+$/gu, "")
136
+ .replace(/[.。!?]+$/u, "")
137
+ .trim();
138
+ const n = Array.from(line).length;
139
+ return n > 0 && n <= max ? line : "";
140
+ }
141
+
142
+ /**
143
+ * Ask the agent model for a title. → the title, or "" for every reason there is not one.
144
+ * Never throws: this runs beside a turn and must not be able to disturb it.
145
+ */
146
+ export async function summariseForTitle(
147
+ question,
148
+ settings,
149
+ { fetchImpl = fetch, timeoutMs = 20_000 } = {},
150
+ ) {
151
+ const agent = agentModelFromSettings(settings);
152
+ if (!agent || !needsSummary(question)) return "";
153
+ try {
154
+ const res = await fetchImpl(chatEndpoint(agent.url), {
155
+ method: "POST",
156
+ headers: { "Content-Type": "application/json" },
157
+ body: JSON.stringify(summaryRequest(agent.model, question)),
158
+ signal: AbortSignal.timeout(timeoutMs),
159
+ });
160
+ if (!res?.ok) return "";
161
+ return pickSummary(await res.json());
162
+ } catch {
163
+ return "";
164
+ }
165
+ }