@celljs/ta-share 4.16.0 → 4.17.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.
@@ -1,4 +1,5 @@
1
1
  // ../ta-protocol/esm/common/ta-extension-host.js
2
+ var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
2
3
  var TA_HOST_EVENT = "ta:host";
3
4
  var TaHostUtil;
4
5
  (function(TaHostUtil2) {
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
  }
@@ -81,6 +114,8 @@ class Redactor {
81
114
 
82
115
  // web-src/snapshot.ts
83
116
  var MAX_TEXT = 4000;
117
+ var MAX_IMAGE = 1.5 * 1024 * 1024;
118
+ var MAX_IMAGES = 4 * 1024 * 1024;
84
119
  var ConversationSnapshot;
85
120
  ((ConversationSnapshot) => {
86
121
  function of(jsonl, options = {}) {
@@ -91,14 +126,15 @@ var ConversationSnapshot;
91
126
  }
92
127
  const redactor = new Redactor;
93
128
  let images = 0;
129
+ let budget = MAX_IMAGES;
94
130
  const messages = [];
95
131
  for (const entry of branch(entries)) {
96
132
  const message = entry.message;
97
133
  const role = message?.role;
98
- if (entry.type !== "message" || !message || role !== "user" && role !== "assistant" && role !== "toolResult") {
134
+ if (entry.type !== "message" || !message || role !== "user" && role !== "assistant" && role !== "toolResult" && role !== "custom") {
99
135
  continue;
100
136
  }
101
- const parts = (content) => {
137
+ const parts = (content, pictures2 = true) => {
102
138
  if (typeof content === "string") {
103
139
  return [{ type: "text", text: redactor.text(content) }];
104
140
  }
@@ -106,11 +142,20 @@ var ConversationSnapshot;
106
142
  for (const part of Array.isArray(content) ? content : []) {
107
143
  if (part.type === "text" && typeof part.text === "string") {
108
144
  kept.push({ type: "text", text: redactor.text(part.text) });
145
+ } else if (part.type === "thinking" && typeof part.thinking === "string" && part.thinking.trim()) {
146
+ kept.push({ type: "thinking", thinking: redactor.text(part.thinking) });
109
147
  } else if (part.type === "toolCall" && typeof part.name === "string") {
110
148
  const args = redactor.value(part.arguments ?? {}, MAX_TEXT);
111
149
  kept.push({ type: "toolCall", id: String(part.id), name: part.name, arguments: args });
112
150
  } else if (part.type === "image") {
113
- images++;
151
+ const { data, mimeType } = part;
152
+ const fits = pictures2 && typeof data === "string" && data.length <= MAX_IMAGE && data.length <= budget;
153
+ if (fits && typeof mimeType === "string" && /^image\//.test(mimeType)) {
154
+ budget -= data.length;
155
+ kept.push({ type: "image", data, mimeType });
156
+ } else {
157
+ images++;
158
+ }
114
159
  }
115
160
  }
116
161
  return kept;
@@ -131,8 +176,19 @@ var ConversationSnapshot;
131
176
  ...typeof message.errorMessage === "string" ? { errorMessage: redactor.text(message.errorMessage) } : {},
132
177
  timestamp
133
178
  });
179
+ } else if (role === "custom") {
180
+ if (message.display === true && typeof message.customType === "string") {
181
+ messages.push({
182
+ role,
183
+ customType: message.customType,
184
+ content: parts(message.content),
185
+ display: true,
186
+ ...message.details === undefined ? {} : { details: redactor.value(message.details, MAX_TEXT) },
187
+ timestamp
188
+ });
189
+ }
134
190
  } else {
135
- const output = options.outputs ? parts(message.content).flatMap((part) => part.type === "text" ? [{ type: "text", text: Redactor.cut(part.text, MAX_TEXT) }] : []) : [];
191
+ 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
192
  messages.push({
137
193
  role,
138
194
  toolCallId: String(message.toolCallId),
@@ -152,9 +208,54 @@ var ConversationSnapshot;
152
208
  ...images || !options.outputs ? { omitted: { ...options.outputs ? {} : { outputs: true }, ...images ? { images } : {} } } : {},
153
209
  ...redactor.count ? { redacted: redactor.count } : {}
154
210
  };
155
- return { conversation, stats: { messages: messages.filter((message) => message.role !== "toolResult").length, redacted: redactor.count, images } };
211
+ const stats = {
212
+ messages: messages.filter((message) => message.role === "user" || message.role === "assistant").length,
213
+ redacted: redactor.count,
214
+ images
215
+ };
216
+ return { conversation, stats, ...header.cwd ? { cwd: header.cwd } : {} };
156
217
  }
157
218
  ConversationSnapshot.of = of;
219
+ function pictures(conversation) {
220
+ const found = new Set;
221
+ for (const message of conversation.messages) {
222
+ if (message.role !== "assistant") {
223
+ continue;
224
+ }
225
+ for (const part of message.content) {
226
+ if (part.type !== "text") {
227
+ continue;
228
+ }
229
+ for (const [, src] of part.text.matchAll(PICTURE)) {
230
+ if (localPath(src) !== undefined) {
231
+ found.add(src);
232
+ }
233
+ }
234
+ }
235
+ }
236
+ return [...found];
237
+ }
238
+ ConversationSnapshot.pictures = pictures;
239
+ function localPath(src) {
240
+ let path = src;
241
+ if (/^file:/i.test(src)) {
242
+ const url = /^file:\/\/(?:localhost)?(\/[^?#]*)/i.exec(src);
243
+ if (!url) {
244
+ return;
245
+ }
246
+ path = url[1];
247
+ } else if (/^[a-z][a-z0-9+.-]*:/i.test(src) || src.startsWith("//")) {
248
+ return;
249
+ }
250
+ path = path.split(/[?#]/)[0];
251
+ try {
252
+ path = decodeURI(path);
253
+ } catch {}
254
+ return path && IMAGE_FILE.test(path) ? path : undefined;
255
+ }
256
+ ConversationSnapshot.localPath = localPath;
257
+ const PICTURE = /!\[[^\]\n]*\]\(\s*<?([^)\s>]+)>?(?:\s+"[^"]*")?\s*\)/g;
258
+ const IMAGE_FILE = /\.(png|jpe?g|gif|webp|avif|bmp)$/i;
158
259
  function branch(entries) {
159
260
  const byId = new Map(entries.filter((entry) => entry.id).map((entry) => [entry.id, entry]));
160
261
  const path = [];
@@ -226,6 +327,9 @@ var ConversationImport;
226
327
  append({ message: result(part.id, part.name, [], false, timestamp) });
227
328
  }
228
329
  }
330
+ } else if (message.role === "custom") {
331
+ const { customType, content, details } = message;
332
+ append({ message: { role: "custom", customType, content, display: true, ...details === undefined ? {} : { details }, timestamp } });
229
333
  } else {
230
334
  append({ message: result(message.toolCallId, message.toolName, message.content, !!message.isError, timestamp) });
231
335
  }
@@ -242,6 +346,15 @@ var ConversationImport;
242
346
  // web-src/share-service.ts
243
347
  var encoder = new TextEncoder;
244
348
  var decoder = new TextDecoder;
349
+ var PICTURE_TYPES = {
350
+ png: "image/png",
351
+ jpg: "image/jpeg",
352
+ jpeg: "image/jpeg",
353
+ gif: "image/gif",
354
+ webp: "image/webp",
355
+ avif: "image/avif",
356
+ bmp: "image/bmp"
357
+ };
245
358
  var FAILURES = new Set(["password", "wrong", "busy", "gone", "offline", "self", "people", "invalid", "timeout", "large"]);
246
359
 
247
360
  class ShareService {
@@ -251,10 +364,18 @@ class ShareService {
251
364
  }
252
365
  dispose() {}
253
366
  async preview(sessionFile, options = {}) {
254
- const { conversation, stats } = await this.snapshot(sessionFile, options);
367
+ const { conversation, stats } = await this.snapshot(sessionFile, options, false);
255
368
  const calls = conversation.messages.flatMap((message) => message.role === "assistant" ? message.content.filter((part) => part.type === "toolCall") : []);
256
369
  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 } };
370
+ let pictures = ConversationSnapshot.pictures(conversation).length;
371
+ const messages = conversation.messages.map((message) => {
372
+ if (typeof message.content === "string" || !message.content.some((part) => part.type === "image")) {
373
+ return message;
374
+ }
375
+ pictures += message.content.filter((part) => part.type === "image").length;
376
+ return { ...message, content: message.content.filter((part) => part.type !== "image") };
377
+ });
378
+ return { json: JSON.stringify({ ...conversation, messages }), stats: { ...stats, tools: calls.length, outputs, pictures } };
258
379
  }
259
380
  async shareConversation(sessionFile, options) {
260
381
  const { conversation, stats } = await this.snapshot(sessionFile, options);
@@ -314,11 +435,41 @@ class ShareService {
314
435
  const sessionFile = await this.host.createSession(cwd, ConversationImport.messages(conversation), conversation.title);
315
436
  return { ok: true, sessionFile };
316
437
  }
317
- async snapshot(sessionFile, options) {
438
+ async snapshot(sessionFile, options, pictures = true) {
318
439
  if (typeof sessionFile !== "string" || !isAbsolute(sessionFile) || !sessionFile.endsWith(".jsonl")) {
319
440
  throw new Error("Share a conversation by its session file.");
320
441
  }
321
- return ConversationSnapshot.of(await readFile(sessionFile, "utf8"), { title: options?.title, outputs: options?.outputs });
442
+ const { conversation, stats, cwd } = ConversationSnapshot.of(await readFile(sessionFile, "utf8"), { title: options?.title, outputs: options?.outputs });
443
+ const files = pictures ? await ShareService.pictures(conversation, cwd) : {};
444
+ if (Object.keys(files).length > 0) {
445
+ conversation.files = files;
446
+ }
447
+ return { conversation, stats };
448
+ }
449
+ static MAX_PICTURE = 1.5 * 1024 * 1024;
450
+ static MAX_PICTURES = 3 * 1024 * 1024;
451
+ static async pictures(conversation, cwd) {
452
+ const files = {};
453
+ let budget = ShareService.MAX_PICTURES;
454
+ const local = !!cwd && isAbsolute(cwd);
455
+ for (const src of ConversationSnapshot.pictures(conversation)) {
456
+ const path = ConversationSnapshot.localPath(src);
457
+ const type = path && PICTURE_TYPES[extname(path).slice(1).toLowerCase()];
458
+ if (!path || !type || !isAbsolute(path) && !local) {
459
+ continue;
460
+ }
461
+ const full = isAbsolute(path) ? path : resolve(cwd, path);
462
+ try {
463
+ const info = await stat(full);
464
+ if (!info.isFile() || info.size > ShareService.MAX_PICTURE || info.size * 1.34 > budget) {
465
+ continue;
466
+ }
467
+ const data = (await readFile(full)).toString("base64");
468
+ budget -= data.length;
469
+ files[src] = `data:${type};base64,${data}`;
470
+ } catch {}
471
+ }
472
+ return files;
322
473
  }
323
474
  async fetch(link, password) {
324
475
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-share",
3
- "version": "4.16.0",
3
+ "version": "4.17.0",
4
4
  "description": "A pi package for Ta: share a conversation, a page or a file by a link, public, with a password or for yourself, end to end through the relay; open and import what others share",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,9 +27,9 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/ta-protocol": "4.16.0",
31
- "@celljs/ta-relay": "4.16.0",
32
- "@celljs/ta-web": "4.16.0",
30
+ "@celljs/ta-protocol": "4.17.0",
31
+ "@celljs/ta-relay": "4.17.0",
32
+ "@celljs/ta-web": "4.17.0",
33
33
  "@types/react": "^19.3.0"
34
34
  },
35
35
  "publishConfig": {
package/web/index.js CHANGED
@@ -79,6 +79,7 @@ var EN = {
79
79
  stats: "{messages} messages · {tools} tool calls",
80
80
  redacted: "{n} things that look like secrets are hidden",
81
81
  images: "{n} images are left out",
82
+ pictures: "{n} pictures go with it",
82
83
  outputsLeft: "The outputs of tools are left out",
83
84
  nothing: "There is nothing to share in this conversation yet.",
84
85
  you: "You",
@@ -178,6 +179,7 @@ var ZH = {
178
179
  stats: "{messages} 条消息 · {tools} 次工具调用",
179
180
  redacted: "隐去了 {n} 处疑似密钥",
180
181
  images: "去掉了 {n} 张图片",
182
+ pictures: "附带 {n} 张图片",
181
183
  outputsLeft: "不含工具的输出",
182
184
  nothing: "这个对话里还没有可以分享的内容。",
183
185
  you: "你",
@@ -277,6 +279,7 @@ var ZH_TW = {
277
279
  stats: "{messages} 條訊息 · {tools} 次工具呼叫",
278
280
  redacted: "隱去了 {n} 處疑似金鑰",
279
281
  images: "去掉了 {n} 張圖片",
282
+ pictures: "附帶 {n} 張圖片",
280
283
  outputsLeft: "不含工具的輸出",
281
284
  nothing: "這個對話裡還沒有可以分享的內容。",
282
285
  you: "你",
@@ -376,6 +379,7 @@ var JA = {
376
379
  stats: "{messages} 件のメッセージ · {tools} 件のツール呼び出し",
377
380
  redacted: "{n} 個の秘密のような項目を非表示にしました",
378
381
  images: "{n} 個の画像を削除しました",
382
+ pictures: "{n} 枚の画像を含みます",
379
383
  outputsLeft: "ツールの出力を含めません",
380
384
  nothing: "この会話にはまだ共有できるコンテンツがありません。",
381
385
  you: "あなた",
@@ -595,6 +599,7 @@ function PreviewBox({ preview, outputs }) {
595
599
  const tools = stats?.tools ?? conversation?.messages.reduce((count, item) => count + (item.role === "assistant" ? item.content.filter((part) => part.type === "toolCall").length : 0), 0) ?? 0;
596
600
  const notes = [
597
601
  stats?.redacted ? t("redacted", { n: stats.redacted }) : "",
602
+ stats?.pictures ? t("pictures", { n: stats.pictures }) : "",
598
603
  stats?.images ? t("images", { n: stats.images }) : "",
599
604
  outputs ? "" : t("outputsLeft")
600
605
  ].filter(Boolean);
@@ -1343,7 +1348,7 @@ var STYLE = `
1343
1348
 
1344
1349
  /* My shares: the page fills its place and scrolls, its rows never wider than it. */
1345
1350
  .tsh-page { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; width: 100%; height: 100%; overflow-y: auto; box-sizing: border-box; padding: 12px 16px 32px; }
1346
- .tsh-rows { max-width: 720px; margin: 0 auto; padding: 0; list-style: none; display: flex; flex-direction: column; }
1351
+ .tsh-rows { max-width: var(--ta-page-width); margin: 0 auto; padding: 0; list-style: none; display: flex; flex-direction: column; }
1347
1352
  .tsh-item { --ta-swipe-bg: var(--ta-main); min-width: 0; border-radius: 10px; overflow: hidden; }
1348
1353
  .tsh-row { display: flex; align-items: center; gap: 4px; min-width: 0; padding-right: 6px; border-radius: 10px; transition: background 120ms, opacity 200ms; }
1349
1354
  .tsh-row:hover { background: var(--ta-hover); }
@@ -1448,7 +1453,7 @@ function activate(pi2) {
1448
1453
  openLink(pi2, link.url);
1449
1454
  return true;
1450
1455
  }, { specific: true });
1451
- pi2.registerView("shares", { title: () => t("sharesTitle"), icon: Share, render: SharesPage, discover: true, order: 80 });
1456
+ pi2.registerView("shares", { title: () => t("sharesTitle"), icon: Share, render: SharesPage, discover: true, group: "share", order: 80 });
1452
1457
  pi2.registerToolRenderer((name, next) => name === SHARE_TOOL ? next() ?? {
1453
1458
  renderCall: (args) => ({
1454
1459
  verb: t("toolShare"),