@clawchatsai/connector 0.1.28 → 0.1.29

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@ import { buildSharePage } from '../share/viewer-page.js';
17
17
  const ID_RE = /^[A-Za-z0-9_-]{22}$/;
18
18
  const MAX_TITLE = 200;
19
19
  const MAX_PAGE_BYTES = 5 * 1024 * 1024;
20
- const TYPES = new Set(['html', 'svg', 'markdown', 'mermaid', 'csv', 'code']);
20
+ const TYPES = new Set(['html', 'svg', 'markdown', 'mermaid', 'csv', 'code', 'chat']);
21
21
  const SWEEP_MS = 60 * 60 * 1000;
22
22
  const objectKey = id => `shares/${id}.html`;
23
23
 
@@ -143,3 +143,11 @@ test('share page embeds the envelope safely, and its decrypt code opens link and
143
143
  assert.deepEqual(await decryptShare(pw.envelope, { password: 'correct horse battery' }), payload);
144
144
  await assert.rejects(decryptShare(pw.envelope, { password: 'nope' }), { code: 'bad-password' });
145
145
  });
146
+
147
+ test('whole-chat shares are accepted and uploaded like artifacts', async () => {
148
+ const { ops, h } = setup();
149
+ const r = await call(h.handleCreate, { envelope: ENV, title: 'A chat', type: 'chat' });
150
+ assert.equal(r.status, 201);
151
+ assert.equal(r.body.share.type, 'chat');
152
+ assert.ok(ops[0].body.includes('function chatPage'), 'the share page can render chats');
153
+ });
@@ -61,7 +61,7 @@ main { flex:1; display:flex; min-height:0; }
61
61
 
62
62
  (() => {
63
63
  const $ = id => document.getElementById(id);
64
- const EXT = { html: 'html', svg: 'svg', markdown: 'md', mermaid: 'mmd', csv: 'csv', code: 'txt' };
64
+ const EXT = { html: 'html', svg: 'svg', markdown: 'md', mermaid: 'mmd', csv: 'csv', code: 'txt', chat: 'md' };
65
65
  const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
66
66
  const BASE_CSS = 'body{margin:0;padding:24px;font:15px/1.6 system-ui,-apple-system,"Segoe UI",sans-serif;color:#1f2328;background:#fff}'
67
67
  + 'pre{margin:0;white-space:pre-wrap;word-break:break-word;font:13px/1.5 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}'
@@ -97,10 +97,87 @@ main { flex:1; display:flex; min-height:0; }
97
97
  + body.map(r => `<tr>${r.map(c => `<td>${esc(c)}</td>`).join('')}</tr>`).join('') + '</tbody></table>';
98
98
  }
99
99
 
100
+
101
+ // ── Whole-chat snapshots (type 'chat'): content = {messages, images} built by the ClawChats frontend
102
+ // (frontend/share/chat-snapshot.js). Markdown is rendered inside the sandboxed frame; raw HTML in it is
103
+ // shown as text, like in the app.
104
+ const CHAT_CSS = 'body{padding:0;background:#f6f7f9}@media(prefers-color-scheme:dark){body{background:#0f1115;color:#e6e8ee}'
105
+ + '.msg.user .bubble{background:#1d2a44!important}.tools,.files span{background:#171a21!important;border-color:#2a2f3a!important}'
106
+ + '.md pre,.md code{background:#171a21!important}.tools pre{background:#0b0d11!important;color:#d6dae3}.meta,.divider{color:#8b93a7!important}.divider:before,.divider:after{background:#2a2f3a!important}'
107
+ + 'td,th{border-color:#2a2f3a!important}th{background:#171a21!important}a{color:#6f9bff}}'
108
+ + '.chat{max-width:820px;margin:0 auto;padding:24px 20px 48px}.msg{margin:0 0 22px}.meta{font-size:12px;color:#6b7280;margin:0 0 6px}'
109
+ + '.msg.user{display:flex;flex-direction:column;align-items:flex-end}.msg.user .bubble{background:#e8effd;border-radius:14px;padding:10px 14px;max-width:85%}'
110
+ + '.plain{white-space:pre-wrap;word-break:break-word}.imgs{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px}.imgs img{max-width:240px;max-height:240px;border-radius:8px}'
111
+ + '.md{line-height:1.6;word-break:break-word}.md img{max-width:100%;border-radius:8px}.md pre{background:#eef0f3;padding:12px;border-radius:8px;overflow:auto}'
112
+ + '.md code{background:#eef0f3;padding:1px 4px;border-radius:4px}.md pre code{padding:0;background:none}.md p{margin:0 0 10px}'
113
+ + '.tools{border:1px solid #e3e6eb;border-radius:10px;background:#fff;padding:6px 12px;margin:0 0 10px;font-size:13px}.tools summary{cursor:pointer;color:#5d6676}'
114
+ + '.tools ul{list-style:none;margin:6px 0 2px;padding:0}.tools li{margin:4px 0;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:12.5px;word-break:break-all}'
115
+ + '.tools .err{color:#d1242f}.tools pre{max-height:320px;overflow:auto;background:#f6f8fa;padding:8px;border-radius:6px;margin:4px 0}'
116
+ + '.files{display:flex;flex-wrap:wrap;gap:6px;margin-top:8px}.files span{font-size:12.5px;border:1px solid #e3e6eb;border-radius:999px;padding:2px 10px;background:#fff}'
117
+ + '.divider{display:flex;align-items:center;gap:10px;color:#6b7280;font-size:12px;margin:26px 0}.divider:before,.divider:after{content:"";flex:1;height:1px;background:#e3e6eb}';
118
+
119
+ function chatPage(content) {
120
+ const messages = Array.isArray(content?.messages) ? content.messages : [];
121
+ const md = [];
122
+ const when = m => (m.time ? ` · <time data-t="${esc(m.time)}"></time>` : '');
123
+ const files = m => (m.files?.length ? `<div class="files">${m.files.map(f => `<span>📎 ${esc(f)}</span>`).join('')}</div>` : '');
124
+ const tools = m => {
125
+ if (!m.tools?.length) return '';
126
+ const rows = m.tools.map(t => {
127
+ const io = t.input != null || t.output != null
128
+ ? `<details><summary>input / output</summary>${t.input != null ? `<pre>${esc(t.input)}</pre>` : ''}${t.output != null ? `<pre>${esc(t.output)}</pre>` : ''}</details>` : '';
129
+ return `<li class="${t.error ? 'err' : ''}">${t.error ? '✗' : '✓'} ${esc(t.title)}${io}</li>`;
130
+ }).join('');
131
+ const label = m.toolSummary || `${m.tools.length} tool call${m.tools.length === 1 ? '' : 's'}`;
132
+ return `<details class="tools"><summary>🔧 ${esc(label)}</summary><ul>${rows}</ul></details>`;
133
+ };
134
+ const body = messages.map(m => {
135
+ if (m.role === 'divider') return `<div class="divider">${esc(m.text || '')}</div>`;
136
+ if (m.role === 'user') {
137
+ const imgs = m.images?.length ? `<div class="imgs">${m.images.map(r => `<img alt="" data-img="${esc(r)}">`).join('')}</div>` : '';
138
+ return `<div class="msg user"><div class="meta">${esc(m.author || 'You')}${when(m)}</div><div class="bubble">`
139
+ + `${m.text ? `<div class="plain">${esc(m.text)}</div>` : ''}${imgs}${files(m)}</div></div>`;
140
+ }
141
+ md.push(String(m.text || ''));
142
+ return `<div class="msg asst"><div class="meta">${esc(m.author || 'Assistant')}${when(m)}</div>${tools(m)}`
143
+ + `<div class="md" data-md="${md.length - 1}"></div>${files(m)}</div>`;
144
+ }).join('');
145
+ const data = JSON.stringify({ md, img: Array.isArray(content?.images) ? content.images : [] }).replace(/</g, '\\u003c');
146
+ const script = '<script>addEventListener("DOMContentLoaded",()=>{'
147
+ + 'const D=JSON.parse(document.getElementById("chat-data").textContent);'
148
+ + 'const esc=s=>String(s).replace(/[&<>"\']/g,c=>({"&":"&amp;","<":"&lt;",">":"&gt;","\\"":"&quot;","\'":"&#39;"}[c]));'
149
+ + 'const img=r=>{const m=/^cc-img:(\\d+)$/.exec(r||"");return m&&D.img[+m[1]]||""};'
150
+ + 'marked.use({gfm:true,breaks:true,renderer:{html(t){return esc(t.text||t.raw||"")}}});'
151
+ + 'document.querySelectorAll("[data-md]").forEach(el=>{el.innerHTML=marked.parse(D.md[+el.dataset.md]||"")});'
152
+ + 'document.querySelectorAll("img").forEach(i=>{const r=i.dataset.img||i.getAttribute("src");if(/^cc-img:/.test(r||"")){const u=img(r);if(u)i.src=u;else{i.replaceWith(Object.assign(document.createElement("em"),{textContent:"[image not included]"}))}}});'
153
+ + 'document.querySelectorAll("a[href]").forEach(a=>{a.target="_blank";a.rel="noopener noreferrer"});'
154
+ + 'document.querySelectorAll("time[data-t]").forEach(t=>{const d=new Date(t.dataset.t);if(!isNaN(d))t.textContent=d.toLocaleString(undefined,{month:"short",day:"numeric",hour:"numeric",minute:"2-digit"})});'
155
+ + '})<\/script>';
156
+ return page(`<main class="chat">${body}</main><script type="application/json" id="chat-data">${data}<\/script>`,
157
+ `<style>${CHAT_CSS}</style>${MARKED}${script}`);
158
+ }
159
+
160
+ // Markdown transcript for the Download button.
161
+ function chatMarkdown(title, content) {
162
+ const lines = [`# ${title}`, ''];
163
+ for (const m of content?.messages || []) {
164
+ if (m.role === 'divider') { lines.push('---', ''); continue; }
165
+ const who = m.author || (m.role === 'user' ? 'You' : 'Assistant');
166
+ lines.push(`**${who}**${m.time ? ` · ${m.time}` : ''}`, '');
167
+ if (m.tools?.length) lines.push(m.tools.map(t => `- ${t.error ? '✗' : '✓'} \`${t.title}\``).join('\n'), '');
168
+ const text = String(m.text || '').replace(/!\[([^\]]*)\]\(cc-img:\d+\)/g, '[image: $1]');
169
+ if (text) lines.push(text, '');
170
+ if (m.images?.length) lines.push(m.images.map(() => '[image]').join(' '), '');
171
+ if (m.files?.length) lines.push(m.files.map(f => `📎 ${f}`).join(' '), '');
172
+ }
173
+ return lines.join('\n');
174
+ }
175
+
100
176
  // The artifact document for the sandboxed iframe. Untrusted content: it only ever goes into srcdoc.
101
177
  function build(type, content) {
102
178
  switch (type) {
103
179
  case 'html': return String(content);
180
+ case 'chat': return chatPage(content);
104
181
  case 'svg': return page(`<div class="center">${content}</div>`);
105
182
  case 'mermaid':
106
183
  return page(`<pre class="mermaid">${esc(content)}</pre>`, MERMAID
@@ -126,7 +203,8 @@ main { flex:1; display:flex; min-height:0; }
126
203
  btn.onclick = () => {
127
204
  const ext = payload.type === 'code' ? (payload.language || 'txt') : (EXT[payload.type] || 'txt');
128
205
  const a = document.createElement('a');
129
- a.href = URL.createObjectURL(new Blob([payload.content], { type: 'text/plain' }));
206
+ const text = payload.type === 'chat' ? chatMarkdown(title, payload.content) : String(payload.content);
207
+ a.href = URL.createObjectURL(new Blob([text], { type: 'text/plain' }));
130
208
  a.download = `${title.replace(/[^\w.-]+/g, '-').replace(/^-+|-+$/g, '') || 'artifact'}.${String(ext).replace(/\W/g, '') || 'txt'}`;
131
209
  a.click();
132
210
  setTimeout(() => URL.revokeObjectURL(a.href), 1000);