@celljs/ta-share 4.16.0 → 4.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/host/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // web-src/share-service.ts
2
- import { readFile } from "node:fs/promises";
3
- import { isAbsolute } from "node:path";
2
+ import { readFile, stat } from "node:fs/promises";
3
+ import { extname, isAbsolute, resolve } from "node:path";
4
4
 
5
5
  // web-src/share-protocol.ts
6
6
  var CONVERSATION_KIND = "ta-conversation";
@@ -25,15 +25,15 @@ var ShareLink;
25
25
  // ../ta-protocol/esm/common/redactor.js
26
26
  var REDACTED = "[redacted]";
27
27
  var WHOLE = [
28
- /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g,
29
- /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}/g,
30
- /\bgh[pousr]_[A-Za-z0-9]{30,}\b/g,
31
- /\bgithub_pat_[A-Za-z0-9_]{40,}\b/g,
32
- /\bglpat-[A-Za-z0-9_-]{20,}\b/g,
33
- /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
34
- /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
35
- /\bAIza[0-9A-Za-z_-]{35}\b/g,
36
- /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g
28
+ ["PRIVATE_KEY", /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g],
29
+ ["API_KEY", /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{20,}/g],
30
+ ["GITHUB_TOKEN", /\bgh[pousr]_[A-Za-z0-9]{30,}\b/g],
31
+ ["GITHUB_TOKEN", /\bgithub_pat_[A-Za-z0-9_]{40,}\b/g],
32
+ ["GITLAB_TOKEN", /\bglpat-[A-Za-z0-9_-]{20,}\b/g],
33
+ ["SLACK_TOKEN", /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g],
34
+ ["AWS_ACCESS_KEY_ID", /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g],
35
+ ["GOOGLE_API_KEY", /\bAIza[0-9A-Za-z_-]{35}\b/g],
36
+ ["JWT", /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g]
37
37
  ];
38
38
  var NAMED = [
39
39
  /(\bBearer\s+)[A-Za-z0-9._~+/-]{16,}=*/gi,
@@ -47,7 +47,7 @@ class Redactor {
47
47
  }
48
48
  text(text) {
49
49
  let out = text;
50
- for (const pattern of WHOLE) {
50
+ for (const [, pattern] of WHOLE) {
51
51
  out = out.replace(pattern, () => this.hide());
52
52
  }
53
53
  out = out.replace(NAMED[0], (_match, before) => before + this.hide());
@@ -67,6 +67,39 @@ class Redactor {
67
67
  }
68
68
  return value;
69
69
  }
70
+ static spans(text) {
71
+ var _a;
72
+ var _b;
73
+ const found = [];
74
+ for (const [kind, pattern] of WHOLE) {
75
+ for (const match of text.matchAll(pattern)) {
76
+ found.push({ start: match.index, end: match.index + match[0].length, kind });
77
+ }
78
+ }
79
+ for (const match of text.matchAll(NAMED[0])) {
80
+ found.push({ start: match.index + match[1].length, end: match.index + match[0].length, kind: "BEARER_TOKEN" });
81
+ }
82
+ for (const match of text.matchAll(NAMED[1])) {
83
+ if (!Redactor.placeholder(match[2])) {
84
+ const start = match.index + match[1].length;
85
+ const name = (_b = (_a = /[A-Za-z0-9_.-]+/.exec(match[1])) === null || _a === undefined ? undefined : _a[0]) !== null && _b !== undefined ? _b : "SECRET";
86
+ found.push({ start, end: start + match[2].length, kind: name.replace(/[^A-Za-z0-9]+/g, "_").toUpperCase() });
87
+ }
88
+ }
89
+ for (const match of text.matchAll(NAMED[2])) {
90
+ const start = match.index + match[1].length;
91
+ found.push({ start, end: start + match[2].length, kind: "URL_PASSWORD" });
92
+ }
93
+ found.sort((a, b) => a.start - b.start || b.end - a.end);
94
+ const spans = [];
95
+ for (const span of found) {
96
+ const last = spans.at(-1);
97
+ if (!last || span.start >= last.end) {
98
+ spans.push(span);
99
+ }
100
+ }
101
+ return spans;
102
+ }
70
103
  static cut(text, max) {
71
104
  return text.length > max ? `${text.slice(0, max)}…` : text;
72
105
  }
@@ -79,8 +112,503 @@ class Redactor {
79
112
  }
80
113
  }
81
114
 
115
+ // ../ta-protocol/esm/common/message-protocol.js
116
+ var TaMessageContext;
117
+ (function(TaMessageContext2) {
118
+ const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
119
+ function wrap(context, message) {
120
+ return `<context>
121
+ ${context}
122
+ </context>
123
+ ${message}`;
124
+ }
125
+ TaMessageContext2.wrap = wrap;
126
+ function strip(message) {
127
+ return message.replace(BLOCK, "");
128
+ }
129
+ TaMessageContext2.strip = strip;
130
+ function read(message) {
131
+ var _a;
132
+ const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
133
+ return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
134
+ }
135
+ TaMessageContext2.read = read;
136
+ })(TaMessageContext || (TaMessageContext = {}));
137
+ var TaMessageSource;
138
+ (function(TaMessageSource2) {
139
+ TaMessageSource2.DOUBLE = "ta:double";
140
+ const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
141
+ function mark(message, from, detail) {
142
+ return marked(message, line(from, detail));
143
+ }
144
+ TaMessageSource2.mark = mark;
145
+ function notice(message, from, detail) {
146
+ return marked(message, line(from, detail, true));
147
+ }
148
+ TaMessageSource2.notice = notice;
149
+ function line(from, detail, notice2 = false) {
150
+ return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
151
+ }
152
+ TaMessageSource2.line = line;
153
+ function of(message) {
154
+ const context = TaMessageContext.read(message);
155
+ const match = context === undefined ? null : LINE.exec(context);
156
+ return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
157
+ }
158
+ TaMessageSource2.of = of;
159
+ function isNotice(message) {
160
+ var _a;
161
+ return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
162
+ }
163
+ TaMessageSource2.isNotice = isNotice;
164
+ TaMessageSource2.SECTION = "ta-sources";
165
+ TaMessageSource2.PROMPT = [
166
+ "Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
167
+ "One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
168
+ "- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
169
+ "- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
170
+ "- Never quote it as the user's words nor answer it as if they had said it."
171
+ ].join(`
172
+ `);
173
+ function marked(message, source) {
174
+ if (/^[/!]/.test(message) || of(message)) {
175
+ return message;
176
+ }
177
+ const context = TaMessageContext.read(message);
178
+ return TaMessageContext.wrap(context === undefined ? source : `${context}
179
+ ${source}`, TaMessageContext.strip(message));
180
+ }
181
+ })(TaMessageSource || (TaMessageSource = {}));
182
+ var TaImageNote;
183
+ (function(TaImageNote2) {
184
+ const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$/;
185
+ function strip(text) {
186
+ const lines = text.split(`
187
+ `);
188
+ const kept = lines.filter((line) => !NOTE.test(line.trim()));
189
+ if (kept.length === lines.length) {
190
+ return text;
191
+ }
192
+ return kept.join(`
193
+ `).replace(/\n+$/, "").replace(/^\n+/, "");
194
+ }
195
+ TaImageNote2.strip = strip;
196
+ })(TaImageNote || (TaImageNote = {}));
197
+ var TaImageSize;
198
+ (function(TaImageSize2) {
199
+ const JPEG_BYTES = 256 * 1024;
200
+ function of(base64) {
201
+ var _a, _b, _c;
202
+ let bytes;
203
+ try {
204
+ const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
205
+ bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
206
+ } catch (_d) {
207
+ return;
208
+ }
209
+ const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
210
+ return size && size.width > 0 && size.height > 0 ? size : undefined;
211
+ }
212
+ TaImageSize2.of = of;
213
+ function u16be(bytes, at) {
214
+ return bytes[at] << 8 | bytes[at + 1];
215
+ }
216
+ function u16le(bytes, at) {
217
+ return bytes[at] | bytes[at + 1] << 8;
218
+ }
219
+ function u24le(bytes, at) {
220
+ return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
221
+ }
222
+ function u32be(bytes, at) {
223
+ return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
224
+ }
225
+ function text(bytes, at, length) {
226
+ return String.fromCharCode(...bytes.subarray(at, at + length));
227
+ }
228
+ function png(bytes) {
229
+ return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
230
+ }
231
+ function gif(bytes) {
232
+ return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
233
+ }
234
+ function webp(bytes) {
235
+ if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
236
+ return;
237
+ }
238
+ switch (text(bytes, 12, 4)) {
239
+ case "VP8 ":
240
+ return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
241
+ case "VP8L": {
242
+ const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
243
+ return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
244
+ }
245
+ case "VP8X":
246
+ return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
247
+ default:
248
+ return;
249
+ }
250
+ }
251
+ function jpeg(bytes) {
252
+ if (bytes[0] !== 255 || bytes[1] !== 216) {
253
+ return;
254
+ }
255
+ let at = 2;
256
+ while (at + 9 < bytes.length) {
257
+ if (bytes[at] !== 255) {
258
+ return;
259
+ }
260
+ const marker = bytes[at + 1];
261
+ if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
262
+ return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
263
+ }
264
+ at += 2 + u16be(bytes, at + 2);
265
+ }
266
+ return;
267
+ }
268
+ })(TaImageSize || (TaImageSize = {}));
269
+ var TaAgentSummary;
270
+ (function(TaAgentSummary2) {
271
+ const START = /^\s*<conversation_history_summary>/;
272
+ const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
273
+ function read(text) {
274
+ if (!START.test(text)) {
275
+ return;
276
+ }
277
+ return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
278
+ }
279
+ TaAgentSummary2.read = read;
280
+ function strip(text) {
281
+ return text.replace(BLOCK, "");
282
+ }
283
+ TaAgentSummary2.strip = strip;
284
+ })(TaAgentSummary || (TaAgentSummary = {}));
285
+ var TaSkillUse;
286
+ (function(TaSkillUse2) {
287
+ const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
288
+ function parse(message) {
289
+ var _a;
290
+ const match = BLOCK.exec(message);
291
+ if (!match) {
292
+ return;
293
+ }
294
+ return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
295
+ }
296
+ TaSkillUse2.parse = parse;
297
+ function command(use) {
298
+ return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
299
+ }
300
+ TaSkillUse2.command = command;
301
+ })(TaSkillUse || (TaSkillUse = {}));
302
+ var TaTaskNote;
303
+ (function(TaTaskNote2) {
304
+ const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
305
+ const ATTRIBUTE = /(\w+)="([^"]*)"/g;
306
+ function write(note) {
307
+ const attributes = [`id="${quote(note.id)}"`];
308
+ if (note.was !== undefined) {
309
+ attributes.push(`was="${quote(note.was)}"`);
310
+ }
311
+ attributes.push(`title="${quote(note.title)}"`);
312
+ if (note.status !== undefined) {
313
+ attributes.push(`status="${quote(note.status)}"`);
314
+ }
315
+ return `<task_${note.kind} ${attributes.join(" ")}>
316
+ ${note.body}
317
+ </task_${note.kind}>`;
318
+ }
319
+ TaTaskNote2.write = write;
320
+ function parse(text) {
321
+ const match = NOTE.exec(text.trim());
322
+ if (!match) {
323
+ return;
324
+ }
325
+ const attributes = {};
326
+ for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
327
+ attributes[name] = unquote(value);
328
+ }
329
+ if (!attributes.id || attributes.title === undefined) {
330
+ return;
331
+ }
332
+ const { id, title, status, was } = attributes;
333
+ return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
334
+ }
335
+ TaTaskNote2.parse = parse;
336
+ function quote(text) {
337
+ return text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/\n/g, " ");
338
+ }
339
+ function unquote(text) {
340
+ return text.replace(/&quot;/g, '"').replace(/&amp;/g, "&");
341
+ }
342
+ })(TaTaskNote || (TaTaskNote = {}));
343
+ var TaSecretRef;
344
+ (function(TaSecretRef2) {
345
+ TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
346
+ const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
347
+ const KINDS = [
348
+ [/^PHONE(?:_\d+)?$/, "phone"],
349
+ [/^EMAIL(?:_\d+)?$/, "email"],
350
+ [/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
351
+ [/^BANK_CARD(?:_\d+)?$/, "bankCard"]
352
+ ];
353
+ function has(text) {
354
+ return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
355
+ }
356
+ TaSecretRef2.has = has;
357
+ function kind(name) {
358
+ var _a;
359
+ var _b;
360
+ return (_b = (_a = KINDS.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
361
+ }
362
+ TaSecretRef2.kind = kind;
363
+ function split(text) {
364
+ if (!has(text)) {
365
+ return [{ text }];
366
+ }
367
+ const parts = [];
368
+ let at = 0;
369
+ for (const match of text.matchAll(REF)) {
370
+ if (match.index > at) {
371
+ parts.push({ text: text.slice(at, match.index) });
372
+ }
373
+ parts.push({ secret: match[1], kind: kind(match[1]) });
374
+ at = match.index + match[0].length;
375
+ }
376
+ if (at < text.length) {
377
+ parts.push({ text: text.slice(at) });
378
+ }
379
+ return parts;
380
+ }
381
+ TaSecretRef2.split = split;
382
+ function plain(text, label) {
383
+ return has(text) ? text.replace(REF, (_whole, name) => label(name, kind(name))) : text;
384
+ }
385
+ TaSecretRef2.plain = plain;
386
+ })(TaSecretRef || (TaSecretRef = {}));
387
+
388
+ // ../ta-protocol/esm/common/said-protocol.js
389
+ var TaSaid;
390
+ (function(TaSaid2) {
391
+ const CARD = /^ta-/i;
392
+ const READ = 4000;
393
+ const FENCE = /^\s*(`{3,}|~{3,})\s*([^\s`]*)/;
394
+ const TAG_LINE = /^<\/?[A-Za-z][\w:.-]*(?:\s[^<>]*)?\/?>$/;
395
+ const MARKS_ONLY = /^[-*_=|:+\s]+$/;
396
+ const MARKS = [
397
+ ["context", /<\/?context>/],
398
+ ["source", /\[ta:(?:from|notice|by) /],
399
+ ["image note", /\[Image: original \d+x\d+|\[Image converted from /],
400
+ ["skill", /<skill name="|<\/skill>/],
401
+ ["task note", /<\/?task_(?:update|adopted|renamed)\b/],
402
+ ["summary", /<\/?(?:conversation_history_summary|summary)>/]
403
+ ];
404
+ function text(content, joiner = `
405
+ `) {
406
+ if (typeof content === "string") {
407
+ return content;
408
+ }
409
+ return Array.isArray(content) ? content.filter((part) => (part === null || part === undefined ? undefined : part.type) === "text" && typeof part.text === "string").map((part) => part.text).join(joiner) : "";
410
+ }
411
+ TaSaid2.text = text;
412
+ function words(role, text2, options = {}) {
413
+ const said = role === "user" ? userWords(text2) : TaAgentSummary.strip(text2);
414
+ return withoutFences(said, (language) => options.cards === false && CARD.test(language), true).trim();
415
+ }
416
+ TaSaid2.words = words;
417
+ function of(message, options) {
418
+ return message ? words(message.role, text(message.content), options) : "";
419
+ }
420
+ TaSaid2.of = of;
421
+ function notice(message) {
422
+ return (message === null || message === undefined ? undefined : message.role) === "user" && TaMessageSource.isNotice(text(message.content));
423
+ }
424
+ TaSaid2.notice = notice;
425
+ function marksOnly(role, text2) {
426
+ return !!text2.trim() && !line(words(role, text2), { chars: Number.POSITIVE_INFINITY });
427
+ }
428
+ TaSaid2.marksOnly = marksOnly;
429
+ function line(markdown, options) {
430
+ var _a;
431
+ const read = markdown.slice(0, READ);
432
+ const prose = withoutFences(read, () => true, true).trim();
433
+ const lines = plain(prose || codeOf(read)).split(`
434
+ `).map((part) => part.replace(/^\s*(?:#{1,6}\s+|>\s*|[-*+]\s+|\d+[.)]\s+)/, "").replace(/\*{1,3}|`+|~~|(?<![\p{L}\p{N}])_{1,3}|_{1,3}(?![\p{L}\p{N}])/gu, "").replace(/\s+/g, " ").trim()).filter((part) => part && !MARKS_ONLY.test(part) && !TAG_LINE.test(part));
435
+ const count = (_a = options.lines) !== null && _a !== undefined ? _a : 1;
436
+ const joined = count <= 1 ? lines.join(" ") : lines.slice(0, count).join(`
437
+ `);
438
+ return joined.length > options.chars ? `${joined.slice(0, Math.max(0, options.chars - 1)).trimEnd()}…` : joined;
439
+ }
440
+ TaSaid2.line = line;
441
+ function leaks(shown, options = {}) {
442
+ const found = MARKS.filter(([, mark]) => mark.test(shown)).map(([name]) => name);
443
+ const lines = shown.split(`
444
+ `);
445
+ if (options.cards !== true && lines.some((part) => {
446
+ var _a;
447
+ var _b;
448
+ return CARD.test((_b = (_a = FENCE.exec(part)) === null || _a === undefined ? undefined : _a[2]) !== null && _b !== undefined ? _b : "");
449
+ })) {
450
+ found.push("card");
451
+ }
452
+ if (lines.some((part) => TAG_LINE.test(part.trim()))) {
453
+ found.push("tag");
454
+ }
455
+ return found;
456
+ }
457
+ TaSaid2.leaks = leaks;
458
+ const SUMMARY = `<conversation_history_summary>
459
+ <summary>
460
+ 1. Primary Request and Intent:
461
+ The user works on the relay.
462
+ </summary>
463
+ </conversation_history_summary>`;
464
+ TaSaid2.SAMPLES = [
465
+ {
466
+ mark: "context and where it came from",
467
+ role: "user",
468
+ text: `<context>
469
+ The channel #ops, its members.
470
+ [ta:from @celljs/ta-channels #ops]
471
+ </context>
472
+ Check the build`,
473
+ said: "Check the build"
474
+ },
475
+ {
476
+ mark: "a notice, not the user",
477
+ role: "user",
478
+ text: `<context>
479
+ [ta:notice @celljs/ta-call]
480
+ </context>
481
+ The work finished. Its last words: tests pass.`,
482
+ said: "The work finished. Its last words: tests pass."
483
+ },
484
+ { mark: "one let in", role: "user", text: `<context>
485
+ [ta:by p-7 Alice]
486
+ </context>
487
+ Hello there`, said: "Hello there" },
488
+ {
489
+ mark: "notes of images",
490
+ role: "user",
491
+ text: `Look at this
492
+ [Image: original 3024x4032, displayed at 1512x2016. Multiply coordinates by 2.00 to map to original image.]
493
+ [Image converted from image/heic to image/png.]`,
494
+ said: "Look at this"
495
+ },
496
+ {
497
+ mark: "a skill run",
498
+ role: "user",
499
+ text: `<skill name="ta-package" location="/skills/ta-package/SKILL.md">
500
+ # Ta package
501
+ Steps.
502
+ </skill>
503
+
504
+ Make a card`,
505
+ said: "Make a card"
506
+ },
507
+ {
508
+ mark: "a note of a task",
509
+ role: "user",
510
+ text: `<task_update id="t1" title="Fix login" status="done">
511
+ All tests pass.
512
+ </task_update>`,
513
+ said: "Fix login"
514
+ },
515
+ { mark: "a note of a new id", role: "user", text: `<task_renamed id="t2" was="t1" title="Fix login">
516
+ Now t2.
517
+ </task_renamed>` },
518
+ { mark: "a summary, then the answer", role: "assistant", text: `${SUMMARY}
519
+ Switched to voice.`, said: "Switched to voice." },
520
+ { mark: "a summary alone", role: "assistant", text: SUMMARY },
521
+ { mark: "a summary as it streams", role: "assistant", text: `<conversation_history_summary>
522
+ <summary>` },
523
+ {
524
+ mark: "a card after the answer",
525
+ role: "assistant",
526
+ text: '**Done**: tests pass.\n\n```ta-glance\n{"status":"done","blocks":[]}\n```',
527
+ said: "tests pass"
528
+ },
529
+ { mark: "a card alone", role: "assistant", text: '```ta-glance\n{"status":"done","blocks":[]}\n```' },
530
+ { mark: "a proposal", role: "assistant", text: 'Committed.\n\n```ta-propose\n{"kind":"git.push","title":"Push"}\n```', said: "Committed." },
531
+ { mark: "what a call says aloud", role: "assistant", text: "All fixed.\n\n```ta-say\nIt is fixed.\n```", said: "All fixed." },
532
+ { mark: "a tag not known yet", role: "assistant", text: `<system-reminder>
533
+ </system-reminder>
534
+ Ready.`, said: "Ready." }
535
+ ];
536
+ function userWords(text2) {
537
+ const said = TaImageNote.strip(TaMessageContext.strip(text2));
538
+ const skill = TaSkillUse.parse(said);
539
+ if (skill) {
540
+ return TaSkillUse.command(skill);
541
+ }
542
+ const note = TaTaskNote.parse(said);
543
+ if (!note) {
544
+ return said;
545
+ }
546
+ if (note.kind === "renamed") {
547
+ return "";
548
+ }
549
+ return note.kind === "update" && note.body.trim() ? `${note.title} · ${note.body}` : note.title;
550
+ }
551
+ function withoutFences(text2, drop, tags = false) {
552
+ const kept = [];
553
+ let fence;
554
+ let left = false;
555
+ for (const part of text2.split(`
556
+ `)) {
557
+ const open = FENCE.exec(part);
558
+ if (!fence && open) {
559
+ fence = { mark: open[1], dropped: drop(open[2]) };
560
+ } else if (fence && closes(part, fence.mark)) {
561
+ const dropped = fence.dropped;
562
+ fence = undefined;
563
+ if (dropped) {
564
+ continue;
565
+ }
566
+ }
567
+ if ((fence === null || fence === undefined ? undefined : fence.dropped) || !fence && tags && TAG_LINE.test(part.trim())) {
568
+ left = true;
569
+ } else {
570
+ kept.push(part);
571
+ }
572
+ }
573
+ return left ? kept.join(`
574
+ `).replace(/\n{3,}/g, `
575
+
576
+ `) : kept.join(`
577
+ `);
578
+ }
579
+ function codeOf(text2) {
580
+ const code = [];
581
+ let fence;
582
+ for (const part of text2.split(`
583
+ `)) {
584
+ const open = FENCE.exec(part);
585
+ if (!fence && open) {
586
+ fence = { mark: open[1], card: CARD.test(open[2]) };
587
+ } else if (fence && closes(part, fence.mark)) {
588
+ fence = undefined;
589
+ } else if (fence && !fence.card) {
590
+ code.push(part);
591
+ }
592
+ }
593
+ return code.join(`
594
+ `);
595
+ }
596
+ function closes(part, mark) {
597
+ var _a;
598
+ const close = (_a = /^\s*(`{3,}|~{3,})\s*$/.exec(part)) === null || _a === undefined ? undefined : _a[1];
599
+ return !!close && close[0] === mark[0] && close.length >= mark.length;
600
+ }
601
+ function plain(text2) {
602
+ const linked = text2.replace(/(?<!!)\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
603
+ const words2 = linked.replace(/!\[[^\]\n]*\]\([^)\n]*\)/g, "").trim();
604
+ return words2 || linked.replace(/!\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
605
+ }
606
+ })(TaSaid || (TaSaid = {}));
607
+
82
608
  // web-src/snapshot.ts
83
609
  var MAX_TEXT = 4000;
610
+ var MAX_IMAGE = 1.5 * 1024 * 1024;
611
+ var MAX_IMAGES = 4 * 1024 * 1024;
84
612
  var ConversationSnapshot;
85
613
  ((ConversationSnapshot) => {
86
614
  function of(jsonl, options = {}) {
@@ -91,26 +619,41 @@ var ConversationSnapshot;
91
619
  }
92
620
  const redactor = new Redactor;
93
621
  let images = 0;
622
+ let budget = MAX_IMAGES;
94
623
  const messages = [];
95
624
  for (const entry of branch(entries)) {
96
625
  const message = entry.message;
97
626
  const role = message?.role;
98
- if (entry.type !== "message" || !message || role !== "user" && role !== "assistant" && role !== "toolResult") {
627
+ if (entry.type !== "message" || !message || role !== "user" && role !== "assistant" && role !== "toolResult" && role !== "custom") {
99
628
  continue;
100
629
  }
101
- const parts = (content) => {
630
+ const said = (text) => role === "user" || role === "assistant" ? TaSaid.words(role, text) : text;
631
+ const parts = (content, pictures2 = true) => {
102
632
  if (typeof content === "string") {
103
- return [{ type: "text", text: redactor.text(content) }];
633
+ const text = said(content);
634
+ return text ? [{ type: "text", text: redactor.text(text) }] : [];
104
635
  }
105
636
  const kept = [];
106
637
  for (const part of Array.isArray(content) ? content : []) {
107
- if (part.type === "text" && typeof part.text === "string") {
108
- kept.push({ type: "text", text: redactor.text(part.text) });
638
+ const text = part.type === "text" && typeof part.text === "string" ? said(part.text) : undefined;
639
+ if (text !== undefined) {
640
+ if (text) {
641
+ kept.push({ type: "text", text: redactor.text(text) });
642
+ }
643
+ } else if (part.type === "thinking" && typeof part.thinking === "string" && part.thinking.trim()) {
644
+ kept.push({ type: "thinking", thinking: redactor.text(part.thinking) });
109
645
  } else if (part.type === "toolCall" && typeof part.name === "string") {
110
646
  const args = redactor.value(part.arguments ?? {}, MAX_TEXT);
111
647
  kept.push({ type: "toolCall", id: String(part.id), name: part.name, arguments: args });
112
648
  } else if (part.type === "image") {
113
- images++;
649
+ const { data, mimeType } = part;
650
+ const fits = pictures2 && typeof data === "string" && data.length <= MAX_IMAGE && data.length <= budget;
651
+ if (fits && typeof mimeType === "string" && /^image\//.test(mimeType)) {
652
+ budget -= data.length;
653
+ kept.push({ type: "image", data, mimeType });
654
+ } else {
655
+ images++;
656
+ }
114
657
  }
115
658
  }
116
659
  return kept;
@@ -131,8 +674,19 @@ var ConversationSnapshot;
131
674
  ...typeof message.errorMessage === "string" ? { errorMessage: redactor.text(message.errorMessage) } : {},
132
675
  timestamp
133
676
  });
677
+ } else if (role === "custom") {
678
+ if (message.display === true && typeof message.customType === "string") {
679
+ messages.push({
680
+ role,
681
+ customType: message.customType,
682
+ content: parts(message.content),
683
+ display: true,
684
+ ...message.details === undefined ? {} : { details: redactor.value(message.details, MAX_TEXT) },
685
+ timestamp
686
+ });
687
+ }
134
688
  } else {
135
- const output = options.outputs ? parts(message.content).flatMap((part) => part.type === "text" ? [{ type: "text", text: Redactor.cut(part.text, MAX_TEXT) }] : []) : [];
689
+ const output = options.outputs ? parts(message.content).flatMap((part) => part.type === "text" ? [{ type: "text", text: Redactor.cut(part.text, MAX_TEXT) }] : part.type === "image" ? [part] : []) : [];
136
690
  messages.push({
137
691
  role,
138
692
  toolCallId: String(message.toolCallId),
@@ -152,9 +706,54 @@ var ConversationSnapshot;
152
706
  ...images || !options.outputs ? { omitted: { ...options.outputs ? {} : { outputs: true }, ...images ? { images } : {} } } : {},
153
707
  ...redactor.count ? { redacted: redactor.count } : {}
154
708
  };
155
- return { conversation, stats: { messages: messages.filter((message) => message.role !== "toolResult").length, redacted: redactor.count, images } };
709
+ const stats = {
710
+ messages: messages.filter((message) => message.role === "user" || message.role === "assistant").length,
711
+ redacted: redactor.count,
712
+ images
713
+ };
714
+ return { conversation, stats, ...header.cwd ? { cwd: header.cwd } : {} };
156
715
  }
157
716
  ConversationSnapshot.of = of;
717
+ function pictures(conversation) {
718
+ const found = new Set;
719
+ for (const message of conversation.messages) {
720
+ if (message.role !== "assistant") {
721
+ continue;
722
+ }
723
+ for (const part of message.content) {
724
+ if (part.type !== "text") {
725
+ continue;
726
+ }
727
+ for (const [, src] of part.text.matchAll(PICTURE)) {
728
+ if (localPath(src) !== undefined) {
729
+ found.add(src);
730
+ }
731
+ }
732
+ }
733
+ }
734
+ return [...found];
735
+ }
736
+ ConversationSnapshot.pictures = pictures;
737
+ function localPath(src) {
738
+ let path = src;
739
+ if (/^file:/i.test(src)) {
740
+ const url = /^file:\/\/(?:localhost)?(\/[^?#]*)/i.exec(src);
741
+ if (!url) {
742
+ return;
743
+ }
744
+ path = url[1];
745
+ } else if (/^[a-z][a-z0-9+.-]*:/i.test(src) || src.startsWith("//")) {
746
+ return;
747
+ }
748
+ path = path.split(/[?#]/)[0];
749
+ try {
750
+ path = decodeURI(path);
751
+ } catch {}
752
+ return path && IMAGE_FILE.test(path) ? path : undefined;
753
+ }
754
+ ConversationSnapshot.localPath = localPath;
755
+ const PICTURE = /!\[[^\]\n]*\]\(\s*<?([^)\s>]+)>?(?:\s+"[^"]*")?\s*\)/g;
756
+ const IMAGE_FILE = /\.(png|jpe?g|gif|webp|avif|bmp)$/i;
158
757
  function branch(entries) {
159
758
  const byId = new Map(entries.filter((entry) => entry.id).map((entry) => [entry.id, entry]));
160
759
  const path = [];
@@ -226,6 +825,9 @@ var ConversationImport;
226
825
  append({ message: result(part.id, part.name, [], false, timestamp) });
227
826
  }
228
827
  }
828
+ } else if (message.role === "custom") {
829
+ const { customType, content, details } = message;
830
+ append({ message: { role: "custom", customType, content, display: true, ...details === undefined ? {} : { details }, timestamp } });
229
831
  } else {
230
832
  append({ message: result(message.toolCallId, message.toolName, message.content, !!message.isError, timestamp) });
231
833
  }
@@ -242,6 +844,15 @@ var ConversationImport;
242
844
  // web-src/share-service.ts
243
845
  var encoder = new TextEncoder;
244
846
  var decoder = new TextDecoder;
847
+ var PICTURE_TYPES = {
848
+ png: "image/png",
849
+ jpg: "image/jpeg",
850
+ jpeg: "image/jpeg",
851
+ gif: "image/gif",
852
+ webp: "image/webp",
853
+ avif: "image/avif",
854
+ bmp: "image/bmp"
855
+ };
245
856
  var FAILURES = new Set(["password", "wrong", "busy", "gone", "offline", "self", "people", "invalid", "timeout", "large"]);
246
857
 
247
858
  class ShareService {
@@ -251,10 +862,18 @@ class ShareService {
251
862
  }
252
863
  dispose() {}
253
864
  async preview(sessionFile, options = {}) {
254
- const { conversation, stats } = await this.snapshot(sessionFile, options);
865
+ const { conversation, stats } = await this.snapshot(sessionFile, options, false);
255
866
  const calls = conversation.messages.flatMap((message) => message.role === "assistant" ? message.content.filter((part) => part.type === "toolCall") : []);
256
867
  const outputs = conversation.messages.filter((message) => message.role === "toolResult" && message.content.length > 0).length;
257
- return { json: JSON.stringify(conversation), stats: { ...stats, tools: calls.length, outputs } };
868
+ let pictures = ConversationSnapshot.pictures(conversation).length;
869
+ const messages = conversation.messages.map((message) => {
870
+ if (typeof message.content === "string" || !message.content.some((part) => part.type === "image")) {
871
+ return message;
872
+ }
873
+ pictures += message.content.filter((part) => part.type === "image").length;
874
+ return { ...message, content: message.content.filter((part) => part.type !== "image") };
875
+ });
876
+ return { json: JSON.stringify({ ...conversation, messages }), stats: { ...stats, tools: calls.length, outputs, pictures } };
258
877
  }
259
878
  async shareConversation(sessionFile, options) {
260
879
  const { conversation, stats } = await this.snapshot(sessionFile, options);
@@ -314,11 +933,41 @@ class ShareService {
314
933
  const sessionFile = await this.host.createSession(cwd, ConversationImport.messages(conversation), conversation.title);
315
934
  return { ok: true, sessionFile };
316
935
  }
317
- async snapshot(sessionFile, options) {
936
+ async snapshot(sessionFile, options, pictures = true) {
318
937
  if (typeof sessionFile !== "string" || !isAbsolute(sessionFile) || !sessionFile.endsWith(".jsonl")) {
319
938
  throw new Error("Share a conversation by its session file.");
320
939
  }
321
- return ConversationSnapshot.of(await readFile(sessionFile, "utf8"), { title: options?.title, outputs: options?.outputs });
940
+ const { conversation, stats, cwd } = ConversationSnapshot.of(await readFile(sessionFile, "utf8"), { title: options?.title, outputs: options?.outputs });
941
+ const files = pictures ? await ShareService.pictures(conversation, cwd) : {};
942
+ if (Object.keys(files).length > 0) {
943
+ conversation.files = files;
944
+ }
945
+ return { conversation, stats };
946
+ }
947
+ static MAX_PICTURE = 1.5 * 1024 * 1024;
948
+ static MAX_PICTURES = 3 * 1024 * 1024;
949
+ static async pictures(conversation, cwd) {
950
+ const files = {};
951
+ let budget = ShareService.MAX_PICTURES;
952
+ const local = !!cwd && isAbsolute(cwd);
953
+ for (const src of ConversationSnapshot.pictures(conversation)) {
954
+ const path = ConversationSnapshot.localPath(src);
955
+ const type = path && PICTURE_TYPES[extname(path).slice(1).toLowerCase()];
956
+ if (!path || !type || !isAbsolute(path) && !local) {
957
+ continue;
958
+ }
959
+ const full = isAbsolute(path) ? path : resolve(cwd, path);
960
+ try {
961
+ const info = await stat(full);
962
+ if (!info.isFile() || info.size > ShareService.MAX_PICTURE || info.size * 1.34 > budget) {
963
+ continue;
964
+ }
965
+ const data = (await readFile(full)).toString("base64");
966
+ budget -= data.length;
967
+ files[src] = `data:${type};base64,${data}`;
968
+ } catch {}
969
+ }
970
+ return files;
322
971
  }
323
972
  async fetch(link, password) {
324
973
  try {