@celljs/ta-glance 4.12.0 → 4.13.1

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.en.md CHANGED
@@ -4,6 +4,7 @@ A pi package that comes with Ta (on by default; Settings → Extensions of the w
4
4
 
5
5
  - **The answer first, little of the process**: under the Ta service, every turn's system prompt gets a `<ta-glance>` section: the first line is the conclusion in one sentence, what matters in bold; then only what the user needs to act or decide; a short question gets one to three sentences.
6
6
  - **Results as a card**: when a reply reports work done, results, a comparison, choices to make, or several facts or relations, Ta writes a `ta-glance` block (JSON) after the first line, which the web client draws as a card: key numbers, files changed (opened by a click), steps, before and after, bars, a flow, a Mermaid diagram, a table, choices to tap, risks and to-dos, buttons of what to ask next, folded details. The card grows as the reply streams.
7
+ - **Waiting for you**: a turn whose reply ends with a card of choices (`options`) or `status: "blocked"` is marked in the session with a `ta-ask` entry (`TA_ASK_ENTRY`, which the model never reads): the list of chats, its tab, the double's tasks and Quick open show that the conversation waits for you to decide, until you answer, by a tap or in words. Until then the card is framed in blue with a ?; once answered it shows the choice on every device, after a reload too (`WebCodeRendererProps.answer`).
7
8
  - **Elsewhere**: the preview of the list of conversations shows the first line; replies to WeChat and WeCom bots draw no card and stay short paragraphs; `ta` in a terminal replies as before. External agents (ta-agents) get the section too.
8
9
 
9
10
  The card's format is in `web-src/glance-protocol.ts`, the prompt in `web-src/glance-prompt.ts`. Other packages draw cards for fenced blocks of their own languages with `registerCodeRenderer`.
package/README.md CHANGED
@@ -4,6 +4,7 @@ Ta 自带的 pi 包(默认开启,可在 Web 客户端的 设置 → 扩展
4
4
 
5
5
  - **先说结论、少说过程**:在 Ta 服务下,每一轮的系统提示里多一段 `<ta-glance>`:第一行一句话结论、关键词加粗;只写你要行动或决定的东西;小问题一到三句。
6
6
  - **结果画成卡片**:汇报完成的工作、结果、对比、要你选的方案、多个事实或关系时,Ta 在第一行之后写一个 `ta-glance` 代码块(JSON),Web 把它画成卡片:关键数字、改了的文件(点开)、步骤、前后对比、横条、流程、Mermaid 关系图、表格、可点选的方案、风险与待办、下一步按钮、折叠的细节。卡片随回复流式长出来。
7
+ - **待你决定**:一轮回复以带选项(`options`)或 `status: "blocked"` 的卡片结束时,pi 扩展在会话里记一条 `ta-ask`(`TA_ASK_ENTRY`,模型读不到),对话列表、标签、分身任务和快捷打开就显示这个对话在等你决定,直到你回答(点选或打字都算)。卡片在你回答前用蓝框和问号标出;回答后各设备都显示已选,刷新也保留(`WebCodeRendererProps.answer`)。
7
8
  - **别处**:对话列表的预览显示第一行结论;发到微信、企业微信机器人的回复不画卡片,照常是短段落;终端里的 `ta` 回复照旧。外部 Agent(ta-agents)也会拿到这段写法。
8
9
 
9
10
  卡片的格式见 `web-src/glance-protocol.ts`,提示词见 `web-src/glance-prompt.ts`。别的包也能用 `registerCodeRenderer` 为自己的代码块语言画卡片。
@@ -5,24 +5,461 @@ var TaHostUtil;
5
5
  TaHostUtil2.WAIT_MS = 1000;
6
6
  function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
7
7
  return new Promise((resolve) => {
8
+ let heard = false;
9
+ let answered = false;
8
10
  const timer = setTimeout(() => resolve(undefined), waitMs);
9
11
  const request = {
10
12
  name,
13
+ heard: () => {
14
+ heard = true;
15
+ },
11
16
  resolve: (answer) => {
17
+ answered = true;
12
18
  clearTimeout(timer);
13
19
  resolve(answer);
14
20
  }
15
21
  };
16
22
  events.emit(TA_HOST_EVENT, request);
23
+ if (!heard && !answered) {
24
+ clearTimeout(timer);
25
+ resolve(undefined);
26
+ }
17
27
  });
18
28
  }
19
29
  TaHostUtil2.reach = reach;
30
+ function marked(systemPrompt, mark) {
31
+ return role(systemPrompt).includes(mark);
32
+ }
33
+ TaHostUtil2.marked = marked;
34
+ function role(systemPrompt) {
35
+ if (!systemPrompt) {
36
+ return "";
37
+ }
38
+ const open = `<addendum>
39
+ `;
40
+ const start = systemPrompt.indexOf(open);
41
+ const context = systemPrompt.indexOf(`<project_context>
42
+ `);
43
+ if (start < 0 || context >= 0 && context < start) {
44
+ return "";
45
+ }
46
+ const end = systemPrompt.indexOf(`
47
+ </addendum>`, start);
48
+ return systemPrompt.slice(start + open.length, end < 0 ? undefined : end);
49
+ }
50
+ TaHostUtil2.role = role;
20
51
  })(TaHostUtil || (TaHostUtil = {}));
52
+ var TA_ASK_ENTRY = "ta-ask";
53
+ var TaAskUtil;
54
+ (function(TaAskUtil2) {
55
+ const LENGTH = 120;
56
+ function entry(question) {
57
+ const line = question.replace(/\s+/g, " ").trim();
58
+ return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
59
+ }
60
+ TaAskUtil2.entry = entry;
61
+ function asking(entries) {
62
+ var _a;
63
+ for (let i = entries.length - 1;i >= 0; i--) {
64
+ const entry2 = entries[i];
65
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
66
+ return;
67
+ }
68
+ if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
69
+ return read(entry2);
70
+ }
71
+ }
72
+ return;
73
+ }
74
+ TaAskUtil2.asking = asking;
75
+ function next(asked, entry2) {
76
+ var _a;
77
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
78
+ return;
79
+ }
80
+ return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
81
+ }
82
+ TaAskUtil2.next = next;
83
+ function read(found) {
84
+ var _a;
85
+ const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
86
+ if (typeof question !== "string" || !question.trim()) {
87
+ return;
88
+ }
89
+ const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
90
+ return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
91
+ }
92
+ })(TaAskUtil || (TaAskUtil = {}));
93
+
94
+ // web-src/glance-json.ts
95
+ var GlanceJson;
96
+ ((GlanceJson) => {
97
+ const TRIES = 40;
98
+ function parse(text, partial) {
99
+ const source = text.trim().replace(/,\s*([}\]])/g, "$1");
100
+ if (!source) {
101
+ return;
102
+ }
103
+ try {
104
+ return JSON.parse(source);
105
+ } catch {
106
+ if (!partial) {
107
+ return;
108
+ }
109
+ }
110
+ let end = source.length;
111
+ for (let tries = 0;tries < TRIES && end > 0; tries++) {
112
+ const closed = close(source.slice(0, end));
113
+ if (closed !== undefined) {
114
+ try {
115
+ return JSON.parse(closed);
116
+ } catch {}
117
+ }
118
+ end = Math.max(source.lastIndexOf(",", end - 1), source.lastIndexOf("{", end - 1) + 1, source.lastIndexOf("[", end - 1) + 1);
119
+ }
120
+ return;
121
+ }
122
+ GlanceJson.parse = parse;
123
+ function close(text) {
124
+ const open = [];
125
+ let inString = false;
126
+ let escaped = false;
127
+ for (const char of text) {
128
+ if (inString) {
129
+ if (escaped) {
130
+ escaped = false;
131
+ } else if (char === "\\") {
132
+ escaped = true;
133
+ } else if (char === '"') {
134
+ inString = false;
135
+ }
136
+ } else if (char === '"') {
137
+ inString = true;
138
+ } else if (char === "{" || char === "[") {
139
+ open.push(char === "{" ? "}" : "]");
140
+ } else if (char === "}" || char === "]") {
141
+ open.pop();
142
+ }
143
+ }
144
+ if (!open.length && !inString) {
145
+ return;
146
+ }
147
+ let body = text;
148
+ if (inString) {
149
+ body = `${escaped ? body.slice(0, -1) : body}"`;
150
+ }
151
+ body = body.replace(/[\s,:]+$/, "");
152
+ if (open[open.length - 1] === "}" && /[{,]\s*"(?:[^"\\]|\\.)*"$/.test(body)) {
153
+ body = body.replace(/,?\s*"(?:[^"\\]|\\.)*"$/, "");
154
+ }
155
+ return body + open.reverse().join("");
156
+ }
157
+ })(GlanceJson ||= {});
21
158
 
22
159
  // web-src/glance-protocol.ts
23
160
  var NAME = "@celljs/ta-glance";
24
161
  var LANGUAGE = "ta-glance";
25
162
 
163
+ // web-src/glance-spec.ts
164
+ var ITEMS = 12;
165
+ var BLOCKS = 8;
166
+ var TEXT = 400;
167
+ var MARKDOWN = 8000;
168
+ var STATUSES = ["done", "partial", "failed", "blocked", "info"];
169
+ var TONES = ["good", "bad", "warn", "neutral"];
170
+ var STEPS = ["done", "doing", "todo", "failed", "skipped"];
171
+ var NOTES = ["risk", "info", "todo"];
172
+ function object(value) {
173
+ return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
174
+ }
175
+ function text(value, max = TEXT) {
176
+ if (typeof value === "number" && Number.isFinite(value)) {
177
+ return String(value);
178
+ }
179
+ if (typeof value !== "string") {
180
+ return;
181
+ }
182
+ const trimmed = value.trim();
183
+ return trimmed ? trimmed.slice(0, max) : undefined;
184
+ }
185
+ function number(value) {
186
+ const parsed = typeof value === "string" ? Number.parseFloat(value.replace(/[^\d.eE+-]/g, "")) : value;
187
+ return typeof parsed === "number" && Number.isFinite(parsed) ? parsed : undefined;
188
+ }
189
+ function oneOf(value, values) {
190
+ const lower = typeof value === "string" ? value.trim().toLowerCase() : undefined;
191
+ return values.find((candidate) => candidate === lower);
192
+ }
193
+ function items(value, read) {
194
+ if (!Array.isArray(value)) {
195
+ return [];
196
+ }
197
+ const out = [];
198
+ for (const item of value) {
199
+ const made = read(item);
200
+ if (made !== undefined) {
201
+ out.push(made);
202
+ }
203
+ if (out.length === ITEMS) {
204
+ break;
205
+ }
206
+ }
207
+ return out;
208
+ }
209
+ function strings(value, max = ITEMS) {
210
+ return items(value, (item) => text(item) ?? text(object(item)?.label) ?? text(object(item)?.text)).slice(0, max);
211
+ }
212
+ function stat(item) {
213
+ const value = object(item);
214
+ const label = text(value?.label);
215
+ const shown = text(value?.value);
216
+ if (!label || shown === undefined) {
217
+ return;
218
+ }
219
+ const tone = oneOf(value?.tone, TONES);
220
+ const delta = text(value?.delta);
221
+ return { label, value: shown, ...tone ? { tone } : {}, ...delta ? { delta } : {} };
222
+ }
223
+ function change(item) {
224
+ const value = object(item);
225
+ const path = text(value?.path ?? item);
226
+ if (!path) {
227
+ return;
228
+ }
229
+ const kind = oneOf(value?.kind, ["add", "edit", "delete"]) ?? "edit";
230
+ const add = number(value?.add);
231
+ const del = number(value?.del);
232
+ const note = text(value?.note);
233
+ return {
234
+ path,
235
+ kind,
236
+ ...add !== undefined ? { add: Math.max(0, Math.round(add)) } : {},
237
+ ...del !== undefined ? { del: Math.max(0, Math.round(del)) } : {},
238
+ ...note ? { note } : {}
239
+ };
240
+ }
241
+ function step(item, partial) {
242
+ const value = object(item);
243
+ const shown = text(value?.text ?? value?.label ?? value?.title ?? item);
244
+ return shown ? { text: shown, status: oneOf(value?.status, STEPS) ?? (partial ? "todo" : "done") } : undefined;
245
+ }
246
+ function option(item) {
247
+ const value = object(item);
248
+ const label = text(value?.label ?? item);
249
+ if (!label) {
250
+ return;
251
+ }
252
+ const detail = text(value?.detail);
253
+ return {
254
+ label,
255
+ ...detail ? { detail } : {},
256
+ pros: strings(value?.pros, 4),
257
+ cons: strings(value?.cons, 4),
258
+ ...value?.recommended === true ? { recommended: true } : {}
259
+ };
260
+ }
261
+ function note(item) {
262
+ const value = object(item);
263
+ const shown = text(value?.text ?? item);
264
+ return shown ? { text: shown, tone: oneOf(value?.tone, NOTES) ?? "info" } : undefined;
265
+ }
266
+ function isPath(value) {
267
+ return !/[,。;!?、]/.test(value) && (!/\s/.test(value) || /[\\/]/.test(value));
268
+ }
269
+ var LABELS = { before: "Before", after: "After" };
270
+ function block(item, partial, labels) {
271
+ const value = object(item);
272
+ const type = typeof value?.type === "string" ? value.type.trim().toLowerCase() : undefined;
273
+ const unit = text(value?.unit, 16);
274
+ switch (type) {
275
+ case "stats": {
276
+ const list = items(value?.items, stat).slice(0, 6);
277
+ return list.length ? { type, items: list } : undefined;
278
+ }
279
+ case "changes": {
280
+ const raw = Array.isArray(value?.items) ? value.items : [];
281
+ if (raw.length && raw.every((entry) => typeof entry === "string" && !isPath(entry))) {
282
+ const list2 = items(raw, note);
283
+ return list2.length ? { type: "notes", items: list2 } : undefined;
284
+ }
285
+ const list = items(value?.items, change);
286
+ return list.length ? { type, items: list } : undefined;
287
+ }
288
+ case "steps": {
289
+ const list = items(value?.items, (entry) => step(entry, partial));
290
+ return list.length ? { type, items: list } : undefined;
291
+ }
292
+ case "compare": {
293
+ const list = items(value?.items, (entry) => {
294
+ const measure = object(entry);
295
+ const label = text(measure?.label);
296
+ const before = number(measure?.before);
297
+ const after = number(measure?.after);
298
+ return label && before !== undefined && after !== undefined ? { label, before, after } : undefined;
299
+ });
300
+ const better = oneOf(value?.better, ["lower", "higher"]) ?? "lower";
301
+ if (list.length) {
302
+ return { type, better, items: list, ...unit ? { unit } : {} };
303
+ }
304
+ const rows = items(value?.items, (entry) => {
305
+ const measure = object(entry);
306
+ const label = text(measure?.label);
307
+ const before = text(measure?.before);
308
+ const after = text(measure?.after);
309
+ return label && (before || after) ? [label, before ?? "", after ?? ""] : undefined;
310
+ });
311
+ return rows.length ? { type: "table", columns: ["", labels.before, labels.after], rows } : undefined;
312
+ }
313
+ case "bars": {
314
+ const list = items(value?.items, (entry) => {
315
+ const amount = object(entry);
316
+ const label = text(amount?.label);
317
+ const shown = number(amount?.value);
318
+ return label && shown !== undefined ? { label, value: shown } : undefined;
319
+ });
320
+ return list.length ? { type, items: list, ...unit ? { unit } : {} } : undefined;
321
+ }
322
+ case "flow": {
323
+ const list = strings(value?.items);
324
+ return list.length ? { type, items: list } : undefined;
325
+ }
326
+ case "diagram": {
327
+ const mermaid = text(value?.mermaid, MARKDOWN);
328
+ return mermaid ? { type, mermaid } : undefined;
329
+ }
330
+ case "table": {
331
+ const columns = strings(value?.columns, 6);
332
+ const rows = items(value?.rows, (row) => Array.isArray(row) ? columns.map((_, index) => text(row[index]) ?? "") : undefined);
333
+ return columns.length && rows.length ? { type, columns, rows } : undefined;
334
+ }
335
+ case "options": {
336
+ const list = items(value?.items, option).slice(0, 5);
337
+ const question = text(value?.question);
338
+ return list.length ? { type, items: list, ...question ? { question } : {} } : undefined;
339
+ }
340
+ case "notes": {
341
+ const list = items(value?.items, note);
342
+ return list.length ? { type, items: list } : undefined;
343
+ }
344
+ case "next": {
345
+ const list = strings(value?.items, 3);
346
+ return list.length ? { type, items: list } : undefined;
347
+ }
348
+ case "details": {
349
+ const markdown = text(value?.markdown, MARKDOWN);
350
+ const title = text(value?.title);
351
+ return markdown ? { type, markdown, ...title ? { title } : {} } : undefined;
352
+ }
353
+ default:
354
+ return;
355
+ }
356
+ }
357
+ function loose(card) {
358
+ const out = [];
359
+ for (const [key, entries] of Object.entries(card)) {
360
+ const type = key === "files" ? "changes" : key;
361
+ if (Array.isArray(entries) && LISTS.includes(type)) {
362
+ out.push({ type, items: entries, ...type === "options" ? { question: card.question } : {} });
363
+ }
364
+ }
365
+ return out;
366
+ }
367
+ var LISTS = ["stats", "changes", "steps", "flow", "options", "notes", "next"];
368
+ var GlanceSpec;
369
+ ((GlanceSpec) => {
370
+ function read(value, partial = false, labels = LABELS) {
371
+ const card = object(value);
372
+ if (!card) {
373
+ return;
374
+ }
375
+ const status = oneOf(card.status, STATUSES);
376
+ const title = text(card.title);
377
+ const blocks = items(Array.isArray(card.blocks) ? card.blocks : loose(card), (entry) => block(entry, partial, labels)).slice(0, BLOCKS);
378
+ if (!blocks.length && !title) {
379
+ return;
380
+ }
381
+ return { blocks, ...status ? { status } : {}, ...title ? { title } : {} };
382
+ }
383
+ GlanceSpec.read = read;
384
+ })(GlanceSpec ||= {});
385
+
386
+ // web-src/glance-ask.ts
387
+ var GlanceAsk;
388
+ ((GlanceAsk) => {
389
+ function ofRun(messages) {
390
+ for (let i = messages.length - 1;i >= 0; i--) {
391
+ const message = messages[i];
392
+ if (message.role === "assistant") {
393
+ return message.stopReason === "aborted" || message.stopReason === "error" ? undefined : question(text2(message.content));
394
+ }
395
+ }
396
+ return;
397
+ }
398
+ GlanceAsk.ofRun = ofRun;
399
+ function question(reply) {
400
+ let asked;
401
+ let asks = false;
402
+ for (const code of cards(reply)) {
403
+ const card = GlanceSpec.read(GlanceJson.parse(code, false));
404
+ if (!card) {
405
+ continue;
406
+ }
407
+ const options = card.blocks.find((block2) => block2.type === "options");
408
+ if (options || card.status === "blocked") {
409
+ asks = true;
410
+ asked = (options?.type === "options" ? options.question : undefined) ?? card.title ?? asked;
411
+ }
412
+ }
413
+ return asks ? asked ?? firstLine(reply) : undefined;
414
+ }
415
+ GlanceAsk.question = question;
416
+ function text2(content) {
417
+ if (typeof content === "string") {
418
+ return content;
419
+ }
420
+ return Array.isArray(content) ? content.filter((part) => part?.type === "text" && typeof part.text === "string").map((part) => part.text).join(`
421
+ `) : "";
422
+ }
423
+ function cards(reply) {
424
+ const found = [];
425
+ let fence;
426
+ for (const line of reply.split(`
427
+ `)) {
428
+ const opening = /^\s*(`{3,}|~{3,})\s*([\w-]*)/.exec(line);
429
+ if (!fence) {
430
+ if (opening) {
431
+ fence = { mark: opening[1], lines: [], card: opening[2] === LANGUAGE };
432
+ }
433
+ continue;
434
+ }
435
+ const mark = /^\s*(`{3,}|~{3,})\s*$/.exec(line)?.[1];
436
+ if (mark && mark[0] === fence.mark[0] && mark.length >= fence.mark.length) {
437
+ if (fence.card) {
438
+ found.push(fence.lines.join(`
439
+ `));
440
+ }
441
+ fence = undefined;
442
+ } else {
443
+ fence.lines.push(line);
444
+ }
445
+ }
446
+ return found;
447
+ }
448
+ function firstLine(reply) {
449
+ for (const line of reply.split(`
450
+ `)) {
451
+ if (/^\s*(`{3,}|~{3,})/.test(line)) {
452
+ return;
453
+ }
454
+ const said = line.replace(/^\s*(?:#{1,6}\s+|>\s*|[-*+]\s+|\d+\.\s+)/, "").replace(/[*_`]{1,3}/g, "").trim();
455
+ if (said) {
456
+ return said;
457
+ }
458
+ }
459
+ return;
460
+ }
461
+ })(GlanceAsk ||= {});
462
+
26
463
  // web-src/glance-prompt.ts
27
464
  var GLANCE_PROMPT = `The user reads your replies in Ta, often on a phone, and wants to take in the result at a glance, not read long text.
28
465
 
@@ -35,11 +472,11 @@ How to reply:
35
472
 
36
473
  The card: {"status": "done" | "partial" | "failed" | "blocked" | "info", "title"?: string, "blocks": [...]}
37
474
  status: done = finished, partial = done in part, failed, blocked = waiting for the user, info = an answer, no work. Leave out title: the first line already says it.
38
- Blocks, 1 to 4 of them, the most important first. Every text a few words, in the user's language; inline Markdown works (**bold**, \`path/to/file\` opens the file).
475
+ Blocks, 1 to 4 of them, the most important first, each with only the fields shown. Every text a few words, in the user's language; inline Markdown works (**bold**, \`path/to/file\` opens the file).
39
476
  - {"type":"stats","items":[{"label":"Tests","value":"128/128","tone":"good"},{"label":"Startup","value":"210 ms","delta":"-34%","tone":"good"}]}: key numbers, at most 6; tone good | bad | warn.
40
- - {"type":"changes","items":[{"path":"src/a.ts","kind":"edit","add":12,"del":3,"note":"why, in a few words"}]}: files changed; kind add | edit | delete.
477
+ - {"type":"changes","items":[{"path":"src/a.ts","kind":"edit","add":12,"del":3,"note":"why, in a few words"}]}: files changed, a path each (points go in notes); kind add | edit | delete.
41
478
  - {"type":"steps","items":[{"text":"...","status":"done"}]}: progress or a plan; status done | doing | todo | failed | skipped.
42
- - {"type":"compare","unit":"ms","better":"lower","items":[{"label":"Startup","before":320,"after":210}]}: before and after, numbers only.
479
+ - {"type":"compare","unit":"ms","better":"lower","items":[{"label":"Startup","before":320,"after":210}]}: before and after, numbers only (words go in a table).
43
480
  - {"type":"bars","unit":"MB","items":[{"label":"ta","value":66},{"label":"pi","value":129}]}: amounts side by side.
44
481
  - {"type":"flow","items":["Request","Gateway","Session","Model"]}: a sequence or a path.
45
482
  - {"type":"diagram","mermaid":"graph LR; A-->B"}: relations a flow cannot show, under 12 nodes.
@@ -61,6 +498,12 @@ function activate(pi) {
61
498
  event.systemPromptOptions.sections = { ...event.systemPromptOptions.sections, [LANGUAGE]: GLANCE_PROMPT };
62
499
  }
63
500
  });
501
+ pi.on("agent_end", async (event) => {
502
+ const question = GlanceAsk.ofRun(event.messages ?? []);
503
+ if (question && await underServe()) {
504
+ pi.appendEntry(TA_ASK_ENTRY, TaAskUtil.entry(question));
505
+ }
506
+ });
64
507
  }
65
508
  export {
66
509
  activate as default
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-glance",
3
- "version": "4.12.0",
3
+ "version": "4.13.1",
4
4
  "description": "A pi package for Ta: replies lead with the answer, short, and show results as cards (key numbers, files, steps, comparisons, choices) rather than paragraphs",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -26,8 +26,8 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@celljs/ta-protocol": "4.12.0",
30
- "@celljs/ta-web": "4.12.0",
29
+ "@celljs/ta-protocol": "4.13.1",
30
+ "@celljs/ta-web": "4.13.1",
31
31
  "@types/react": "^19.3.0"
32
32
  },
33
33
  "publishConfig": {
package/web/index.js CHANGED
@@ -143,7 +143,7 @@ function change(item) {
143
143
  }
144
144
  function step(item, partial) {
145
145
  const value = object(item);
146
- const shown = text(value?.text ?? value?.label ?? item);
146
+ const shown = text(value?.text ?? value?.label ?? value?.title ?? item);
147
147
  return shown ? { text: shown, status: oneOf(value?.status, STEPS) ?? (partial ? "todo" : "done") } : undefined;
148
148
  }
149
149
  function option(item) {
@@ -166,7 +166,11 @@ function note(item) {
166
166
  const shown = text(value?.text ?? item);
167
167
  return shown ? { text: shown, tone: oneOf(value?.tone, NOTES) ?? "info" } : undefined;
168
168
  }
169
- function block(item, partial) {
169
+ function isPath(value) {
170
+ return !/[,。;!?、]/.test(value) && (!/\s/.test(value) || /[\\/]/.test(value));
171
+ }
172
+ var LABELS = { before: "Before", after: "After" };
173
+ function block(item, partial, labels) {
170
174
  const value = object(item);
171
175
  const type = typeof value?.type === "string" ? value.type.trim().toLowerCase() : undefined;
172
176
  const unit = text(value?.unit, 16);
@@ -176,6 +180,11 @@ function block(item, partial) {
176
180
  return list.length ? { type, items: list } : undefined;
177
181
  }
178
182
  case "changes": {
183
+ const raw = Array.isArray(value?.items) ? value.items : [];
184
+ if (raw.length && raw.every((entry) => typeof entry === "string" && !isPath(entry))) {
185
+ const list2 = items(raw, note);
186
+ return list2.length ? { type: "notes", items: list2 } : undefined;
187
+ }
179
188
  const list = items(value?.items, change);
180
189
  return list.length ? { type, items: list } : undefined;
181
190
  }
@@ -192,7 +201,17 @@ function block(item, partial) {
192
201
  return label && before !== undefined && after !== undefined ? { label, before, after } : undefined;
193
202
  });
194
203
  const better = oneOf(value?.better, ["lower", "higher"]) ?? "lower";
195
- return list.length ? { type, better, items: list, ...unit ? { unit } : {} } : undefined;
204
+ if (list.length) {
205
+ return { type, better, items: list, ...unit ? { unit } : {} };
206
+ }
207
+ const rows = items(value?.items, (entry) => {
208
+ const measure = object(entry);
209
+ const label = text(measure?.label);
210
+ const before = text(measure?.before);
211
+ const after = text(measure?.after);
212
+ return label && (before || after) ? [label, before ?? "", after ?? ""] : undefined;
213
+ });
214
+ return rows.length ? { type: "table", columns: ["", labels.before, labels.after], rows } : undefined;
196
215
  }
197
216
  case "bars": {
198
217
  const list = items(value?.items, (entry) => {
@@ -238,16 +257,27 @@ function block(item, partial) {
238
257
  return;
239
258
  }
240
259
  }
260
+ function loose(card) {
261
+ const out = [];
262
+ for (const [key, entries] of Object.entries(card)) {
263
+ const type = key === "files" ? "changes" : key;
264
+ if (Array.isArray(entries) && LISTS.includes(type)) {
265
+ out.push({ type, items: entries, ...type === "options" ? { question: card.question } : {} });
266
+ }
267
+ }
268
+ return out;
269
+ }
270
+ var LISTS = ["stats", "changes", "steps", "flow", "options", "notes", "next"];
241
271
  var GlanceSpec;
242
272
  ((GlanceSpec) => {
243
- function read(value, partial = false) {
273
+ function read(value, partial = false, labels = LABELS) {
244
274
  const card = object(value);
245
275
  if (!card) {
246
276
  return;
247
277
  }
248
278
  const status = oneOf(card.status, STATUSES);
249
279
  const title = text(card.title);
250
- const blocks = items(card.blocks, (entry) => block(entry, partial)).slice(0, BLOCKS);
280
+ const blocks = items(Array.isArray(card.blocks) ? card.blocks : loose(card), (entry) => block(entry, partial, labels)).slice(0, BLOCKS);
251
281
  if (!blocks.length && !title) {
252
282
  return;
253
283
  }
@@ -298,6 +328,11 @@ var GlanceWords;
298
328
  return language === "zh-CN" ? ZH : EN;
299
329
  }
300
330
  GlanceWords.of = of;
331
+ function picked(answer, labels) {
332
+ const said = answer.trim();
333
+ return labels.find((label) => said === label || said === format(EN.pick, { label }) || said === format(ZH.pick, { label }));
334
+ }
335
+ GlanceWords.picked = picked;
301
336
  function format(text2, values) {
302
337
  return text2.replace(/\{(\w+)\}/g, (all, key) => (key in values) ? String(values[key]) : all);
303
338
  }
@@ -327,6 +362,8 @@ function count(value) {
327
362
  function glanceCard(pi) {
328
363
  const icons = pi.ui.icons;
329
364
  const Markdown = pi.ui.Markdown;
365
+ const Reply = React.createContext(undefined);
366
+ const Answer = React.createContext({ known: false });
330
367
  function Text({ text: text2 }) {
331
368
  return MARKS.test(text2) ? /* @__PURE__ */ React.createElement("span", {
332
369
  className: "tag-inline"
@@ -539,16 +576,26 @@ function glanceCard(pi) {
539
576
  }))))))));
540
577
  }
541
578
  function Options({ block: block2, words, closed }) {
542
- const [chosen, setChosen] = React.useState();
579
+ const [tapped, setChosen] = React.useState();
580
+ const send = React.useContext(Reply);
581
+ const answer = React.useContext(Answer);
582
+ const said = answer.text === undefined ? undefined : GlanceWords.picked(answer.text, block2.items.map((item) => item.label));
583
+ const chosen = tapped ?? said;
584
+ const done = chosen !== undefined || answer.text !== undefined;
585
+ const pending = answer.known && closed && !done;
543
586
  const choose = (label) => {
587
+ if (!send) {
588
+ return;
589
+ }
544
590
  setChosen(label);
545
- pi.sendUserMessage(GlanceWords.format(words.pick, { label })).catch((error) => {
591
+ send(GlanceWords.format(words.pick, { label })).catch((error) => {
546
592
  setChosen(undefined);
547
593
  pi.ui.notify(error instanceof Error ? error.message : String(error), "error");
548
594
  });
549
595
  };
550
596
  return /* @__PURE__ */ React.createElement("div", {
551
- className: "tag-options-block"
597
+ className: "tag-options-block",
598
+ "data-pending": pending || undefined
552
599
  }, block2.question && /* @__PURE__ */ React.createElement("div", {
553
600
  className: "tag-question"
554
601
  }, /* @__PURE__ */ React.createElement(Text, {
@@ -562,7 +609,7 @@ function glanceCard(pi) {
562
609
  className: "tag-option",
563
610
  "data-recommended": item.recommended || undefined,
564
611
  "data-chosen": picked || undefined,
565
- "data-other": chosen && !picked || undefined
612
+ "data-other": done && !picked || undefined
566
613
  }, /* @__PURE__ */ React.createElement("div", {
567
614
  className: "tag-option-main"
568
615
  }, /* @__PURE__ */ React.createElement("div", {
@@ -591,8 +638,8 @@ function glanceCard(pi) {
591
638
  text: point
592
639
  }))))), /* @__PURE__ */ React.createElement("button", {
593
640
  type: "button",
594
- className: `ta-button tag-choose${item.recommended && !chosen ? " ta-primary" : ""}`,
595
- disabled: !closed || chosen !== undefined,
641
+ className: `ta-button tag-choose${item.recommended && !done ? " ta-primary" : ""}`,
642
+ disabled: !closed || !send || done,
596
643
  onClick: () => choose(item.label)
597
644
  }, picked && /* @__PURE__ */ React.createElement(Icon, {
598
645
  name: "Check",
@@ -617,14 +664,15 @@ function glanceCard(pi) {
617
664
  })))));
618
665
  }
619
666
  function Next({ block: block2, closed }) {
667
+ const send = React.useContext(Reply);
620
668
  return /* @__PURE__ */ React.createElement("div", {
621
669
  className: "tag-next"
622
670
  }, block2.items.map((item, index) => /* @__PURE__ */ React.createElement("button", {
623
671
  key: index,
624
672
  type: "button",
625
673
  className: "tag-chip",
626
- disabled: !closed,
627
- onClick: () => void pi.sendUserMessage(item)
674
+ disabled: !closed || !send,
675
+ onClick: () => void send?.(item).catch((error) => pi.ui.notify(error instanceof Error ? error.message : String(error), "error"))
628
676
  }, /* @__PURE__ */ React.createElement(Icon, {
629
677
  name: "CornerDownRight",
630
678
  size: 13
@@ -724,9 +772,13 @@ ${block2.mermaid}
724
772
  text: card.title
725
773
  })));
726
774
  }
727
- return function GlanceCard({ code, closed }) {
775
+ return function GlanceCard(props) {
776
+ const { code, closed } = props;
777
+ const send = "send" in props ? props.send : pi.sendUserMessage;
778
+ const known = "answer" in props;
779
+ const answer = React.useMemo(() => ({ known, text: props.answer }), [known, props.answer]);
728
780
  const words = GlanceWords.of(pi.ui.language);
729
- const card = React.useMemo(() => GlanceSpec.read(GlanceJson.parse(code, !closed), !closed), [code, closed]);
781
+ const card = React.useMemo(() => GlanceSpec.read(GlanceJson.parse(code, !closed), !closed, words), [code, closed, words]);
730
782
  if (!card) {
731
783
  return closed ? /* @__PURE__ */ React.createElement(Markdown, {
732
784
  text: `\`\`\`json
@@ -738,7 +790,11 @@ ${code}
738
790
  className: "ta-spinner"
739
791
  }), /* @__PURE__ */ React.createElement("span", null, words.writing));
740
792
  }
741
- return /* @__PURE__ */ React.createElement("div", {
793
+ return /* @__PURE__ */ React.createElement(Reply.Provider, {
794
+ value: send
795
+ }, /* @__PURE__ */ React.createElement(Answer.Provider, {
796
+ value: answer
797
+ }, /* @__PURE__ */ React.createElement("div", {
742
798
  className: "tag-card",
743
799
  "data-streaming": !closed || undefined
744
800
  }, /* @__PURE__ */ React.createElement(Head, {
@@ -752,12 +808,13 @@ ${code}
752
808
  block: block2,
753
809
  words,
754
810
  closed
755
- }))));
811
+ }))))));
756
812
  };
757
813
  }
758
814
 
759
815
  // web-src/glance-protocol.ts
760
816
  var LANGUAGE = "ta-glance";
817
+ var ALIASES = ["glance", "glance-choice"];
761
818
 
762
819
  // web-src/style.ts
763
820
  var STYLE = `
@@ -809,11 +866,15 @@ var STYLE = `
809
866
  /* The head: how it went, as a pill, and a title beside it that wraps under it when long */
810
867
  .tag-head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; padding: var(--tag-y) var(--tag-x) 0; }
811
868
  .tag-card > .tag-head + .tag-part { border-top: 0; padding-top: 8px; }
869
+ /* A table or a diagram reaches the edges: under the head it keeps the hairline, a gap above it */
870
+ .tag-card > .tag-head + .tag-part:is([data-type='table'], [data-type='diagram']) { margin-top: var(--tag-y); padding-top: 0; border-top: 1px solid var(--tag-line); }
812
871
  .tag-title { flex: 1 1 14em; min-width: 0; font-weight: 600; font-size: calc(14px * var(--tag-s)); line-height: 1.45; color: var(--ta-text); }
813
872
  .tag-status { --tag-tone: var(--ta-muted); flex: none; display: inline-flex; align-items: center; gap: 6px; height: calc(22px * var(--tag-s)); padding: 0 9px; border-radius: 999px; font-size: var(--tag-small); font-weight: 600; line-height: 1; white-space: nowrap; color: var(--tag-tone); background: color-mix(in srgb, var(--tag-tone) 12%, transparent); }
814
873
  .tag-status i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
815
874
  .tag-status[data-status='done'] { --tag-tone: var(--ta-green); }
816
- .tag-status[data-status='partial'], .tag-status[data-status='blocked'] { --tag-tone: var(--ta-yellow); }
875
+ .tag-status[data-status='partial'] { --tag-tone: var(--ta-yellow); }
876
+ /* Needs you: the blue of what waits for the user, as the ? of the list of chats. */
877
+ .tag-status[data-status='blocked'] { --tag-tone: var(--ta-blue); }
817
878
  .tag-status[data-status='failed'] { --tag-tone: var(--ta-red); }
818
879
 
819
880
  /* What a part is about, in small muted type above it */
@@ -941,6 +1002,10 @@ var STYLE = `
941
1002
  .tag-option[data-recommended] { border-color: color-mix(in srgb, var(--ta-green) 50%, var(--ta-border)); background: color-mix(in srgb, var(--ta-green) 4%, transparent); }
942
1003
  .tag-option[data-chosen] { border-color: var(--ta-green); box-shadow: 0 0 0 1px var(--ta-green) inset; }
943
1004
  .tag-option[data-other] { opacity: 0.5; }
1005
+ /* Choices that wait for the user: the card is framed in blue and its question marked with the ? of the list, until they answer. */
1006
+ .tag-card:has(.tag-options-block[data-pending]) { border-color: color-mix(in srgb, var(--ta-blue) 55%, var(--ta-border)); box-shadow: 0 0 0 1px color-mix(in srgb, var(--ta-blue) 22%, transparent); }
1007
+ .tag-options-block[data-pending] > .tag-question { display: flex; align-items: center; gap: 6px; }
1008
+ .tag-options-block[data-pending] > .tag-question::before { content: '?'; flex: none; width: 16px; height: 16px; border-radius: 50%; background: var(--ta-blue); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; text-align: center; }
944
1009
  .tag-option-head { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 6px; }
945
1010
  .tag-option-label { font-weight: 600; }
946
1011
  .tag-badge { flex: none; padding: 0 6px; border-radius: 999px; font-size: var(--tag-tiny); line-height: calc(18px * var(--tag-s)); font-weight: 600; white-space: nowrap; color: var(--ta-green); background: color-mix(in srgb, var(--ta-green) 13%, transparent); }
@@ -1005,7 +1070,10 @@ function activate(pi) {
1005
1070
  };
1006
1071
  }
1007
1072
  const removeStyle = injectStyle();
1008
- pi.registerCodeRenderer(LANGUAGE, glanceCard(pi));
1073
+ const card = glanceCard(pi);
1074
+ for (const language of [LANGUAGE, ...ALIASES]) {
1075
+ pi.registerCodeRenderer(language, card);
1076
+ }
1009
1077
  return removeStyle;
1010
1078
  }
1011
1079
  export {