teapot-coding-agent 0.29.20 → 0.29.21
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/dist/agent/agent.js +17 -0
- package/dist/agent/editorconfig.js +183 -0
- package/dist/agent/tools.js +31 -9
- package/dist/master.js +78 -2
- package/dist/session-title.js +79 -0
- package/package.json +1 -1
- package/public/assets/index-BmZUyZ-a.js +30 -0
- package/public/assets/index-BmZUyZ-a.js.map +1 -0
- package/public/assets/index-BzXydM8C.css +1 -0
- package/public/assets/{shiki-CcBgg4wu.js → shiki-D2bNrwi7.js} +2 -2
- package/public/assets/{shiki-CcBgg4wu.js.map → shiki-D2bNrwi7.js.map} +1 -1
- package/public/index.html +2 -2
- package/public/assets/index-BBZyydfB.js +0 -30
- package/public/assets/index-BBZyydfB.js.map +0 -1
- package/public/assets/index-BlFofSVr.css +0 -1
package/dist/agent/agent.js
CHANGED
|
@@ -12,6 +12,7 @@ import { EventLog, readEvents } from "../log/events.js";
|
|
|
12
12
|
import { chat, chatStream } from "./llm.js";
|
|
13
13
|
import { executeTool, toolSpecs, currentSkills, safeJoin, isContextOverflow, hasRunningBgShells, killBgShells } from "./tools.js";
|
|
14
14
|
import { effortChoosable } from "./llm.js";
|
|
15
|
+
import { sessionTitleFrom } from "../session-title.js";
|
|
15
16
|
import { skillRootsFingerprint, foreignSkillRoots } from "./skills.js";
|
|
16
17
|
import { bus } from "../bus.js";
|
|
17
18
|
/**
|
|
@@ -470,6 +471,11 @@ export class Agent {
|
|
|
470
471
|
* Recovered from the log so the continuity survives a restart.
|
|
471
472
|
*/
|
|
472
473
|
lastCompactSummary;
|
|
474
|
+
/**
|
|
475
|
+
* A short human title for this session (#155), derived from the operator's first
|
|
476
|
+
* prompt. Undefined until the log is read (or when there is no usable prompt).
|
|
477
|
+
*/
|
|
478
|
+
sessionTitle;
|
|
473
479
|
/** overflow recoveries used in the current turn; reset when a turn succeeds */
|
|
474
480
|
overflowRecoveries = 0;
|
|
475
481
|
/**
|
|
@@ -1004,6 +1010,13 @@ export class Agent {
|
|
|
1004
1010
|
// the whole file (not just the current branch) to match what the runtime
|
|
1005
1011
|
// panel showed before the restart.
|
|
1006
1012
|
for (const e of own) {
|
|
1013
|
+
// #155: the title comes from the FIRST user prompt, which never changes, so
|
|
1014
|
+
// deriving it here is free — this loop already walks the whole log at
|
|
1015
|
+
// restore. Only the first one is taken; later prompts are a different
|
|
1016
|
+
// subject and would make the sidebar label churn under the reader.
|
|
1017
|
+
if (this.sessionTitle === undefined && e.type === "prompt" && e.data?.source === "user") {
|
|
1018
|
+
this.sessionTitle = sessionTitleFrom(e.data?.text) ?? undefined;
|
|
1019
|
+
}
|
|
1007
1020
|
if (e.type === "state") {
|
|
1008
1021
|
const d = e.data;
|
|
1009
1022
|
if (d.detail === "llm turn start")
|
|
@@ -1374,6 +1387,10 @@ export class Agent {
|
|
|
1374
1387
|
workspace: this.workspace,
|
|
1375
1388
|
workspaceMissing: this.workspaceMissing,
|
|
1376
1389
|
session: this.currentSession,
|
|
1390
|
+
// #155: a human label for this session, so the sidebar does not ask the
|
|
1391
|
+
// operator to read `teapot-10-16854917`. Absent when the first prompt had
|
|
1392
|
+
// no usable line — the UI then falls back to the id.
|
|
1393
|
+
...(this.sessionTitle ? { sessionTitle: this.sessionTitle } : {}),
|
|
1377
1394
|
branch: this.currentBranch,
|
|
1378
1395
|
goal: {
|
|
1379
1396
|
status: this.goal.status,
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `.editorconfig` support (#109) — "edit_file の改行コードを .editorconfig に合わせて
|
|
3
|
+
* 正規化するとか?".
|
|
4
|
+
*
|
|
5
|
+
* ## What this DOES and deliberately does NOT do
|
|
6
|
+
*
|
|
7
|
+
* An EXISTING file already keeps its own line ending (#84, `detectEol`): the file's
|
|
8
|
+
* own bytes are the strongest possible evidence, and a `.editorconfig` that
|
|
9
|
+
* disagrees with the file on disk is nearly always stale relative to it. So this
|
|
10
|
+
* only supplies an answer where there was NONE:
|
|
11
|
+
*
|
|
12
|
+
* - a NEW file (nothing to detect), and
|
|
13
|
+
* - a MIXED-EOL file, where `detectEol` returns null on purpose because choosing
|
|
14
|
+
* a winner would be a silent whole-file rewrite.
|
|
15
|
+
*
|
|
16
|
+
* That keeps one rule: **the file's own content outranks configuration, which
|
|
17
|
+
* outranks the LF default.** `end_of_line = crlf` in a repo whose files are LF does
|
|
18
|
+
* not silently convert every file the agent touches.
|
|
19
|
+
*
|
|
20
|
+
* ## Why this is not a general editorconfig library
|
|
21
|
+
*
|
|
22
|
+
* The format has glob sections, brace expansion and a precedence order. Only the
|
|
23
|
+
* facts that change how a WRITE behaves are read — today that is `end_of_line`
|
|
24
|
+
* (plus `insert_final_newline`, which decides whether a new file ends in a
|
|
25
|
+
* newline). Anything else (`indent_style`, `charset`, …) has no in-repo consumer,
|
|
26
|
+
* and guessing at it would be untested surface.
|
|
27
|
+
*/
|
|
28
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
29
|
+
import path from "node:path";
|
|
30
|
+
const NONE = { eol: null };
|
|
31
|
+
/**
|
|
32
|
+
* Read the `.editorconfig` files that apply to `targetPath`, nearest-wins.
|
|
33
|
+
*
|
|
34
|
+
* Walks from the file's own directory up to the filesystem root so a nested
|
|
35
|
+
* `.editorconfig` overrides the repo root, which is the documented precedence.
|
|
36
|
+
* Sections are matched against the path RELATIVE to the file that declared them.
|
|
37
|
+
* A `root = true` stops the walk, as the format requires.
|
|
38
|
+
*/
|
|
39
|
+
export function editorConfigFor(targetPath) {
|
|
40
|
+
const dirs = [];
|
|
41
|
+
let dir = path.dirname(path.resolve(targetPath));
|
|
42
|
+
for (;;) {
|
|
43
|
+
dirs.push(dir); // nearest first, which is also the precedence order
|
|
44
|
+
const parent = path.dirname(dir);
|
|
45
|
+
if (parent === dir)
|
|
46
|
+
break;
|
|
47
|
+
dir = parent;
|
|
48
|
+
}
|
|
49
|
+
// nearest-first, so the FIRST answer found wins; `root = true` truncates the walk
|
|
50
|
+
const applicable = [];
|
|
51
|
+
for (const d of dirs) {
|
|
52
|
+
const file = path.join(d, ".editorconfig");
|
|
53
|
+
if (!existsSync(file))
|
|
54
|
+
continue;
|
|
55
|
+
let text = "";
|
|
56
|
+
try {
|
|
57
|
+
text = readFileSync(file, "utf8");
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
applicable.push({ dir: d, text });
|
|
63
|
+
if (/^\s*root\s*=\s*true\s*$/im.test(text))
|
|
64
|
+
break; // this file is the root
|
|
65
|
+
}
|
|
66
|
+
// apply outermost -> innermost so the nearest file's value lands last
|
|
67
|
+
let out = NONE;
|
|
68
|
+
for (const { dir: declDir, text } of applicable.reverse()) {
|
|
69
|
+
const rel = path.relative(declDir, path.resolve(targetPath)).split(path.sep).join("/");
|
|
70
|
+
for (const section of parseSections(text)) {
|
|
71
|
+
if (!sectionMatches(section.pattern, rel))
|
|
72
|
+
continue;
|
|
73
|
+
const eol = section.props.get("end_of_line");
|
|
74
|
+
if (eol === "lf")
|
|
75
|
+
out = { ...out, eol: "\n" };
|
|
76
|
+
else if (eol === "crlf")
|
|
77
|
+
out = { ...out, eol: "\r\n" };
|
|
78
|
+
else if (eol === "cr")
|
|
79
|
+
out = { ...out, eol: "\r" };
|
|
80
|
+
const fn = section.props.get("insert_final_newline");
|
|
81
|
+
if (fn === "true")
|
|
82
|
+
out = { ...out, finalNewline: true };
|
|
83
|
+
else if (fn === "false")
|
|
84
|
+
out = { ...out, finalNewline: false };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** `[section]` blocks with their `key = value` pairs. Comments and blanks ignored. */
|
|
90
|
+
function parseSections(text) {
|
|
91
|
+
const sections = [];
|
|
92
|
+
let current = null;
|
|
93
|
+
for (const raw of text.split(/\r\n|\r|\n/)) {
|
|
94
|
+
const line = raw.trim();
|
|
95
|
+
if (!line || line.startsWith("#") || line.startsWith(";"))
|
|
96
|
+
continue;
|
|
97
|
+
const header = line.match(/^\[(.+)\]$/);
|
|
98
|
+
if (header) {
|
|
99
|
+
current = { pattern: header[1].trim(), props: new Map() };
|
|
100
|
+
sections.push(current);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (!current)
|
|
104
|
+
continue; // a property outside any section applies to nothing
|
|
105
|
+
const eq = line.indexOf("=");
|
|
106
|
+
if (eq === -1)
|
|
107
|
+
continue;
|
|
108
|
+
const key = line.slice(0, eq).trim().toLowerCase();
|
|
109
|
+
const value = line.slice(eq + 1).trim().toLowerCase();
|
|
110
|
+
// the FIRST value wins within a section (the format's rule)
|
|
111
|
+
if (key && value && !current.props.has(key))
|
|
112
|
+
current.props.set(key, value);
|
|
113
|
+
}
|
|
114
|
+
return sections;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Does an editorconfig glob match a POSIX-style relative path?
|
|
118
|
+
*
|
|
119
|
+
* Supports the parts that appear in real configs: `*` (within a segment), `**`
|
|
120
|
+
* (across segments), `?`, `{a,b}` alternation, and a `[abc]` class. A section with
|
|
121
|
+
* no `/` matches the BASENAME at any depth, which is the format's documented special
|
|
122
|
+
* case (and why `[*.md]` in a root file applies to `docs/x.md`).
|
|
123
|
+
*/
|
|
124
|
+
export function sectionMatches(pattern, relPath) {
|
|
125
|
+
const anchored = pattern.includes("/");
|
|
126
|
+
const names = anchored ? [relPath] : [relPath.split("/").pop() ?? ""];
|
|
127
|
+
const body = anchored ? pattern.replace(/^\//, "") : pattern;
|
|
128
|
+
const re = globToRegExp(body, anchored);
|
|
129
|
+
return names.some((n) => re.test(n));
|
|
130
|
+
}
|
|
131
|
+
/** Translate an editorconfig glob into a RegExp. */
|
|
132
|
+
function globToRegExp(glob, anchored) {
|
|
133
|
+
let out = "";
|
|
134
|
+
for (let i = 0; i < glob.length; i++) {
|
|
135
|
+
const c = glob[i];
|
|
136
|
+
if (c === "*") {
|
|
137
|
+
if (glob[i + 1] === "*") {
|
|
138
|
+
// `**/` matches zero or more leading segments; a bare `**` is `.*`
|
|
139
|
+
if (glob[i + 2] === "/") {
|
|
140
|
+
out += "(?:.*/)?";
|
|
141
|
+
i += 2;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
out += ".*";
|
|
145
|
+
i += 1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
out += "[^/]*"; // a single star never crosses a separator
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (c === "?") {
|
|
154
|
+
out += "[^/]";
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (c === "{") {
|
|
158
|
+
const close = glob.indexOf("}", i);
|
|
159
|
+
if (close !== -1) {
|
|
160
|
+
const alts = glob
|
|
161
|
+
.slice(i + 1, close)
|
|
162
|
+
.split(",")
|
|
163
|
+
.map((a) => a.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
164
|
+
out += `(?:${alts.join("|")})`;
|
|
165
|
+
i = close;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (c === "[") {
|
|
170
|
+
const close = glob.indexOf("]", i);
|
|
171
|
+
if (close !== -1) {
|
|
172
|
+
// a character class is passed through as-is, minus any escaping surprises
|
|
173
|
+
out += glob.slice(i, close + 1).replace(/[\\^]/g, "\\$&");
|
|
174
|
+
i = close;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
out += c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
179
|
+
}
|
|
180
|
+
// anchored patterns are relative to the config's directory; a basename pattern
|
|
181
|
+
// already matched against the basename, so neither needs a prefix
|
|
182
|
+
return new RegExp(`^${out}$`, anchored ? "" : "");
|
|
183
|
+
}
|
package/dist/agent/tools.js
CHANGED
|
@@ -8,6 +8,7 @@ import path from "node:path";
|
|
|
8
8
|
import { discoverSkills, isValidSkillName, readSkillFile, saveSkill, SKILL_FILE, } from "./skills.js";
|
|
9
9
|
import { isReasoningEffort } from "../model-meta.js";
|
|
10
10
|
import { killTree, describeKill } from "./kill.js";
|
|
11
|
+
import { editorConfigFor } from "./editorconfig.js";
|
|
11
12
|
const str = (v, fallback = "") => (typeof v === "string" ? v : fallback);
|
|
12
13
|
const num = (v, fallback) => (typeof v === "number" ? v : fallback);
|
|
13
14
|
function clip(s, max) {
|
|
@@ -18,6 +19,22 @@ function clip(s, max) {
|
|
|
18
19
|
async function readText(p) {
|
|
19
20
|
return fs.readFile(p, "utf8");
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Persist a `write_file` payload and report what line endings it used (#84/#109).
|
|
24
|
+
*
|
|
25
|
+
* Extracted so the two paths — a file whose own EOL was detected, and one decided
|
|
26
|
+
* by `.editorconfig` — share one write. The message names CRLF/CR only when a
|
|
27
|
+
* conversion actually happened, so an ordinary LF write stays quiet.
|
|
28
|
+
*/
|
|
29
|
+
async function finishWrite(p, out, eol, original) {
|
|
30
|
+
await fs.writeFile(p, out, "utf8");
|
|
31
|
+
const converted = eol !== null && eol !== "\n" && out !== original;
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
result: `wrote ${p} (${out.length} bytes)` +
|
|
35
|
+
(converted ? ` (kept ${eol === "\r\n" ? "CRLF" : "CR"} line endings)` : ""),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
21
38
|
/**
|
|
22
39
|
* The file's dominant line ending, or null when it is mixed or empty.
|
|
23
40
|
*
|
|
@@ -1177,16 +1194,21 @@ export const TOOLS = [
|
|
|
1177
1194
|
// So an EXISTING file's dominant EOL is re-applied. detectEol returns null
|
|
1178
1195
|
// for a mixed file, which is left untouched on purpose: choosing a winner
|
|
1179
1196
|
// there would itself be the silent whole-file rewrite this prevents.
|
|
1197
|
+
//
|
|
1198
|
+
// #109: when the file CANNOT answer — it is new, or its EOL is mixed — ask
|
|
1199
|
+
// `.editorconfig` before falling back to LF. The order is deliberate: the
|
|
1200
|
+
// file's own bytes outrank configuration, which outranks the default. A
|
|
1201
|
+
// `end_of_line = crlf` in a repo whose files are all LF must not convert
|
|
1202
|
+
// every file the agent touches, so config is never consulted for a file that
|
|
1203
|
+
// already has an unambiguous ending.
|
|
1180
1204
|
const before = await currentOrNull(p);
|
|
1181
|
-
const
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
const
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
(converted ? ` (kept ${eol === "\r\n" ? "CRLF" : "CR"} line endings)` : ""),
|
|
1189
|
-
};
|
|
1205
|
+
const detected = before === null ? null : detectEol(before);
|
|
1206
|
+
if (detected !== null)
|
|
1207
|
+
return finishWrite(p, applyEol(args.content, detected), detected, args.content);
|
|
1208
|
+
const ec = editorConfigFor(p);
|
|
1209
|
+
const out = applyEol(args.content, ec.eol);
|
|
1210
|
+
const final = ec.finalNewline === true && out !== "" && !out.endsWith("\n") ? `${out}\n` : out;
|
|
1211
|
+
return finishWrite(p, final, ec.eol, args.content);
|
|
1190
1212
|
},
|
|
1191
1213
|
},
|
|
1192
1214
|
{
|
package/dist/master.js
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* Agents are in-process async loops (I/O bound only); all CPU-heavy work is
|
|
4
4
|
* delegated to subprocesses managed by the bash tool with hard timeouts.
|
|
5
5
|
*/
|
|
6
|
-
import { readFileSync, existsSync, mkdirSync, writeFileSync, readdirSync, statSync, lstatSync, renameSync, unlinkSync, openSync, closeSync, fsyncSync, } from "node:fs";
|
|
6
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync, readdirSync, statSync, lstatSync, renameSync, unlinkSync, openSync, closeSync, readSync, fsyncSync, } from "node:fs";
|
|
7
7
|
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { sessionTitleFrom } from "./session-title.js";
|
|
8
9
|
import path from "node:path";
|
|
9
10
|
import os from "node:os";
|
|
10
11
|
import { expandTilde } from "./home.js";
|
|
@@ -1390,6 +1391,65 @@ When this task is done (or truly blocked), call finish() with a summary for @${p
|
|
|
1390
1391
|
return null; // no log / unreadable head — cannot prove anything
|
|
1391
1392
|
}
|
|
1392
1393
|
}
|
|
1394
|
+
/**
|
|
1395
|
+
* A human title for a session dir, from its first user prompt (#155), or null.
|
|
1396
|
+
*
|
|
1397
|
+
* Reads in bounded chunks and stops at the FIRST prompt event of any source —
|
|
1398
|
+
* see the note in allSessions() for why that early exit is what makes this
|
|
1399
|
+
* affordable. A torn final line is skipped rather than parsed: a chunk boundary
|
|
1400
|
+
* can cut a JSON object in half, and that must not abort the scan.
|
|
1401
|
+
*/
|
|
1402
|
+
sessionTitleOf(dirPath) {
|
|
1403
|
+
let fd;
|
|
1404
|
+
try {
|
|
1405
|
+
fd = openSync(path.join(dirPath, "chat.jsonl"), "r");
|
|
1406
|
+
const CHUNK = 16 * 1024;
|
|
1407
|
+
// cap per session so one enormous log cannot dominate boot
|
|
1408
|
+
const CAP = 256 * 1024;
|
|
1409
|
+
const buf = Buffer.alloc(CHUNK);
|
|
1410
|
+
let offset = 0;
|
|
1411
|
+
let carry = "";
|
|
1412
|
+
while (offset < CAP) {
|
|
1413
|
+
const n = readSync(fd, buf, 0, CHUNK, offset);
|
|
1414
|
+
if (n <= 0)
|
|
1415
|
+
break;
|
|
1416
|
+
offset += n;
|
|
1417
|
+
const lines = (carry + buf.subarray(0, n).toString("utf8")).split("\n");
|
|
1418
|
+
carry = lines.pop() ?? "";
|
|
1419
|
+
for (const l of lines) {
|
|
1420
|
+
if (!l)
|
|
1421
|
+
continue;
|
|
1422
|
+
let e;
|
|
1423
|
+
try {
|
|
1424
|
+
e = JSON.parse(l);
|
|
1425
|
+
}
|
|
1426
|
+
catch {
|
|
1427
|
+
continue; // torn line at a chunk boundary
|
|
1428
|
+
}
|
|
1429
|
+
if (e.type !== "prompt")
|
|
1430
|
+
continue;
|
|
1431
|
+
// a sub-agent session receives only harness prompts and is labelled from
|
|
1432
|
+
// its id, so a harness prompt is not a title
|
|
1433
|
+
if (e.data?.source !== "user")
|
|
1434
|
+
return null;
|
|
1435
|
+
return sessionTitleFrom(typeof e.data.text === "string" ? e.data.text : null);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
return null;
|
|
1439
|
+
}
|
|
1440
|
+
catch {
|
|
1441
|
+
return null; // no log / unreadable — fall back to the id
|
|
1442
|
+
}
|
|
1443
|
+
finally {
|
|
1444
|
+
if (fd !== undefined)
|
|
1445
|
+
try {
|
|
1446
|
+
closeSync(fd);
|
|
1447
|
+
}
|
|
1448
|
+
catch {
|
|
1449
|
+
/* already closed */
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1393
1453
|
writeSessionManifest(dirPath, agentId) {
|
|
1394
1454
|
try {
|
|
1395
1455
|
const file = path.join(dirPath, "session.json");
|
|
@@ -1575,7 +1635,23 @@ When this task is done (or truly blocked), call finish() with a summary for @${p
|
|
|
1575
1635
|
size = st.size;
|
|
1576
1636
|
}
|
|
1577
1637
|
catch { /* no log yet — still listable */ }
|
|
1578
|
-
|
|
1638
|
+
// #155: a title derived from the session's FIRST user prompt, so the
|
|
1639
|
+
// sidebar does not make the operator read `teapot-10-16854917`.
|
|
1640
|
+
//
|
|
1641
|
+
// Read here rather than from the live agent because boot leaves agents
|
|
1642
|
+
// STOPPED: their logs are not restored, so a title computed during restore
|
|
1643
|
+
// would be missing for every session the operator has not opened yet —
|
|
1644
|
+
// i.e. exactly the ones they need it for.
|
|
1645
|
+
//
|
|
1646
|
+
// BOUNDED and cheap: the scan stops at the FIRST `prompt` event of ANY
|
|
1647
|
+
// source, which is what makes it affordable. Measured over 817 real
|
|
1648
|
+
// sessions: 36 ms / 7.9 MB with the early exit, versus 804 ms / 169 MB
|
|
1649
|
+
// scanning to find a USER prompt specifically (sub-agents receive only
|
|
1650
|
+
// `harness` prompts, 758 of the 817, so they were what made it slow).
|
|
1651
|
+
// Those sub-sessions correctly produce no title — the sidebar already
|
|
1652
|
+
// labels them from their id via subAgentDisplayName().
|
|
1653
|
+
const title = this.sessionTitleOf(dirPath);
|
|
1654
|
+
out.push({ id: d, agentId: owner, mtimeMs: m, sizeBytes: size, ...(title ? { title } : {}) });
|
|
1579
1655
|
}
|
|
1580
1656
|
return out.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
1581
1657
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session titles (#155) — "いろんなチャットアプリが実装してる、CodexやDSHとかも実装してそう /
|
|
3
|
+
* 自動生成とかサブエージェント名とかだとわかりにくい".
|
|
4
|
+
*
|
|
5
|
+
* The sidebar renders the raw session id, so `teapot-10-16854917` and
|
|
6
|
+
* `teapot-10-sub-trim-…` read as machinery: an operator with several sessions
|
|
7
|
+
* cannot tell which is which, and the timeline in front of them is the only clue.
|
|
8
|
+
*
|
|
9
|
+
* ## Why the FIRST USER PROMPT, and not a model call
|
|
10
|
+
*
|
|
11
|
+
* Every harness that does this derives the title from the opening request — it is
|
|
12
|
+
* already written by the human, it costs nothing, and it is stable (the first
|
|
13
|
+
* message of a session never changes, so the title never churns under the reader).
|
|
14
|
+
* A generated title would need an LLM call at exactly the moment the session starts,
|
|
15
|
+
* and would then drift as the session's real subject emerges.
|
|
16
|
+
*
|
|
17
|
+
* ## This is a DISPLAY rule, so it is deliberately lossy
|
|
18
|
+
*
|
|
19
|
+
* The input is arbitrary operator text: markdown, fenced code, URLs, a pasted
|
|
20
|
+
* 200-line log. A sidebar row is ~40 characters wide. So the rule takes the first
|
|
21
|
+
* MEANINGFUL line and trims it, rather than trying to summarise — a title that
|
|
22
|
+
* silently reorders what the operator wrote would be worse than a truncated one.
|
|
23
|
+
*/
|
|
24
|
+
export function sessionTitleFrom(firstUserPrompt) {
|
|
25
|
+
if (typeof firstUserPrompt !== "string")
|
|
26
|
+
return null;
|
|
27
|
+
const lines = firstUserPrompt.split("\n");
|
|
28
|
+
// A fenced block is CONTENT, not a subject: the operator pasted a log or a diff
|
|
29
|
+
// and the prose follows it. Taking the first line inside the fence titled a
|
|
30
|
+
// session "log" (measured). So skip from an opening fence to its closing one.
|
|
31
|
+
let inFence = false;
|
|
32
|
+
let fence = "";
|
|
33
|
+
for (const raw of lines) {
|
|
34
|
+
const fenceOpen = raw.match(/^\s{0,3}(`{3,}|~{3,})/);
|
|
35
|
+
if (fenceOpen) {
|
|
36
|
+
const marker = fenceOpen[1];
|
|
37
|
+
if (!inFence) {
|
|
38
|
+
inFence = true;
|
|
39
|
+
fence = marker[0];
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (marker[0] === fence) {
|
|
43
|
+
inFence = false; // closing fence
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (inFence)
|
|
48
|
+
continue; // inside a block: its body is not a title
|
|
49
|
+
// strip the markdown that would otherwise eat the visible width: ATX headings,
|
|
50
|
+
// bullets/ordered markers, blockquotes, and emphasis characters
|
|
51
|
+
const line = raw
|
|
52
|
+
.replace(/^\s{0,3}#{1,6}\s*/, "")
|
|
53
|
+
.replace(/^\s{0,3}>\s?/, "")
|
|
54
|
+
.replace(/^\s*(?:[-*+]|\d+[.)])\s+/, "")
|
|
55
|
+
.replace(/[*_`~]/g, "")
|
|
56
|
+
.trim();
|
|
57
|
+
if (!line)
|
|
58
|
+
continue;
|
|
59
|
+
// a bare URL or a lone issue/PR reference carries no subject — the NEXT line
|
|
60
|
+
// usually does (a prompt may OPEN with "https://…" or a lone "2.")
|
|
61
|
+
if (/^https?:\/\/\S+$/.test(line))
|
|
62
|
+
continue;
|
|
63
|
+
if (/^#?\d+[.)]?$/.test(line))
|
|
64
|
+
continue;
|
|
65
|
+
return clip(line, 60);
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/** Trim to `max` characters on a word boundary, with an ellipsis when cut. */
|
|
70
|
+
function clip(text, max) {
|
|
71
|
+
if (text.length <= max)
|
|
72
|
+
return text;
|
|
73
|
+
const cut = text.slice(0, max);
|
|
74
|
+
// prefer the last space so a word is not sliced in half; fall back to a hard cut
|
|
75
|
+
// when the line has no space at all (a single long token / CJK run)
|
|
76
|
+
const at = cut.lastIndexOf(" ");
|
|
77
|
+
const body = at > max * 0.6 ? cut.slice(0, at) : cut;
|
|
78
|
+
return `${body.trimEnd()}…`;
|
|
79
|
+
}
|