@celljs/ta-relay 4.10.0 → 4.11.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/README.en.md +13 -1
- package/README.md +13 -1
- package/esm/common/index.d.ts +9 -0
- package/esm/common/index.js +9 -0
- package/esm/common/relay-blobs.d.ts +54 -0
- package/esm/common/relay-blobs.js +153 -0
- package/esm/common/relay-card.d.ts +23 -0
- package/esm/common/relay-card.js +30 -0
- package/esm/common/relay-content.d.ts +104 -0
- package/esm/common/relay-content.js +316 -0
- package/esm/common/relay-conversation.d.ts +59 -0
- package/esm/common/relay-conversation.js +9 -0
- package/esm/common/relay-hub-util.d.ts +91 -0
- package/esm/common/relay-hub-util.js +164 -0
- package/esm/common/relay-hub.d.ts +125 -0
- package/esm/common/relay-hub.js +438 -0
- package/esm/common/relay-hubs.d.ts +271 -0
- package/esm/common/relay-hubs.js +844 -0
- package/esm/common/relay-identity.d.ts +46 -0
- package/esm/common/relay-identity.js +126 -0
- package/esm/common/relay-protocol.d.ts +211 -2
- package/esm/common/relay-protocol.js +53 -0
- package/esm/common/relay-room-chat.d.ts +80 -0
- package/esm/common/relay-room-chat.js +152 -0
- package/esm/common/relay-socket.d.ts +8 -0
- package/esm/common/relay-socket.js +23 -2
- package/esm/guest/guest-i18n.d.ts +47 -0
- package/esm/guest/guest-i18n.js +97 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +78 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +371 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-host.d.ts +25 -3
- package/esm/node/relay-host.js +115 -4
- package/esm/node/relay-hub-files.d.ts +12 -0
- package/esm/node/relay-hub-files.js +39 -0
- package/esm/node/relay-server.d.ts +46 -1
- package/esm/node/relay-server.js +227 -1
- package/esm/node/relay-store.d.ts +72 -0
- package/esm/node/relay-store.js +123 -2
- package/esm/node/relay-viewer.d.ts +7 -0
- package/esm/node/relay-viewer.js +33 -0
- package/esm/viewer/viewer-i18n.d.ts +56 -0
- package/esm/viewer/viewer-i18n.js +117 -0
- package/esm/viewer/viewer-markdown.d.ts +7 -0
- package/esm/viewer/viewer-markdown.js +198 -0
- package/esm/viewer/viewer-style.d.ts +1 -0
- package/esm/viewer/viewer-style.js +72 -0
- package/esm/viewer/viewer.d.ts +8 -0
- package/esm/viewer/viewer.js +378 -0
- package/lib/common/index.d.ts +9 -0
- package/lib/common/index.js +9 -0
- package/lib/common/relay-blobs.d.ts +55 -0
- package/lib/common/relay-blobs.js +157 -0
- package/lib/common/relay-card.d.ts +24 -0
- package/lib/common/relay-card.js +33 -0
- package/lib/common/relay-content.d.ts +105 -0
- package/lib/common/relay-content.js +320 -0
- package/lib/common/relay-conversation.d.ts +60 -0
- package/lib/common/relay-conversation.js +12 -0
- package/lib/common/relay-hub-util.d.ts +92 -0
- package/lib/common/relay-hub-util.js +167 -0
- package/lib/common/relay-hub.d.ts +126 -0
- package/lib/common/relay-hub.js +442 -0
- package/lib/common/relay-hubs.d.ts +272 -0
- package/lib/common/relay-hubs.js +848 -0
- package/lib/common/relay-identity.d.ts +47 -0
- package/lib/common/relay-identity.js +129 -0
- package/lib/common/relay-protocol.d.ts +211 -2
- package/lib/common/relay-protocol.js +54 -1
- package/lib/common/relay-room-chat.d.ts +81 -0
- package/lib/common/relay-room-chat.js +156 -0
- package/lib/common/relay-socket.d.ts +8 -0
- package/lib/common/relay-socket.js +23 -2
- package/lib/guest/guest-i18n.d.ts +48 -0
- package/lib/guest/guest-i18n.js +100 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +81 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +373 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-host.d.ts +25 -3
- package/lib/node/relay-host.js +115 -4
- package/lib/node/relay-hub-files.d.ts +13 -0
- package/lib/node/relay-hub-files.js +43 -0
- package/lib/node/relay-server.d.ts +46 -1
- package/lib/node/relay-server.js +226 -0
- package/lib/node/relay-store.d.ts +72 -0
- package/lib/node/relay-store.js +121 -0
- package/lib/node/relay-viewer.d.ts +8 -0
- package/lib/node/relay-viewer.js +32 -0
- package/lib/viewer/viewer-i18n.d.ts +57 -0
- package/lib/viewer/viewer-i18n.js +120 -0
- package/lib/viewer/viewer-markdown.d.ts +8 -0
- package/lib/viewer/viewer-markdown.js +201 -0
- package/lib/viewer/viewer-style.d.ts +2 -0
- package/lib/viewer/viewer-style.js +75 -0
- package/lib/viewer/viewer.d.ts +9 -0
- package/lib/viewer/viewer.js +381 -0
- package/package.json +5 -4
- package/viewer/frame.html +1 -0
- package/viewer/page.html +80 -0
- package/viewer/room.html +87 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { RELAY_AGENT_KIND, RelayAgentCards } from '../common/relay-card.js';
|
|
2
|
+
import { RelayContentError, RelayContentUtil } from '../common/relay-content.js';
|
|
3
|
+
import { RELAY_CONVERSATION_KIND } from '../common/relay-conversation.js';
|
|
4
|
+
import { RELAY_FRAME_PATH } from '../common/relay-protocol.js';
|
|
5
|
+
import { ViewerI18n } from './viewer-i18n.js';
|
|
6
|
+
import { ViewerMarkdown } from './viewer-markdown.js';
|
|
7
|
+
import { VIEWER_STYLE } from './viewer-style.js';
|
|
8
|
+
const { t } = ViewerI18n;
|
|
9
|
+
const el = ViewerMarkdown.el;
|
|
10
|
+
/** A call of a tool in a line, as Ta reads it: what it does, and on what. */
|
|
11
|
+
export var ViewerTools;
|
|
12
|
+
(function (ViewerTools) {
|
|
13
|
+
const VERBS = {
|
|
14
|
+
read: ['read', ['path', 'file_path']],
|
|
15
|
+
write: ['write', ['path', 'file_path']],
|
|
16
|
+
edit: ['edit', ['path', 'file_path']],
|
|
17
|
+
bash: ['run', ['command'], true],
|
|
18
|
+
grep: ['search', ['pattern'], true],
|
|
19
|
+
find: ['find', ['pattern', 'path']],
|
|
20
|
+
ls: ['list', ['path']],
|
|
21
|
+
web_fetch: ['fetch', ['url']],
|
|
22
|
+
fetch: ['fetch', ['url']],
|
|
23
|
+
web_search: ['webSearch', ['query']],
|
|
24
|
+
codemode: ['script', []],
|
|
25
|
+
};
|
|
26
|
+
function line(name, args) {
|
|
27
|
+
const known = VERBS[name];
|
|
28
|
+
const first = (keys) => keys.map(key => args === null || args === void 0 ? void 0 : args[key]).find(value => typeof value === 'string' && value);
|
|
29
|
+
if (known) {
|
|
30
|
+
return { verb: t(known[0]), target: first(known[1]), code: known[2] };
|
|
31
|
+
}
|
|
32
|
+
const target = Object.values(args !== null && args !== void 0 ? args : {}).find(value => typeof value === 'string' && value.length < 200);
|
|
33
|
+
return { verb: t('use', { name }), target };
|
|
34
|
+
}
|
|
35
|
+
ViewerTools.line = line;
|
|
36
|
+
})(ViewerTools || (ViewerTools = {}));
|
|
37
|
+
function text(content) {
|
|
38
|
+
if (typeof content === 'string') {
|
|
39
|
+
return { words: content, images: 0 };
|
|
40
|
+
}
|
|
41
|
+
const words = content.flatMap(part => (part.type === 'text' ? [part.text] : [])).join('\n');
|
|
42
|
+
return { words, images: content.filter(part => part.type === 'image').length };
|
|
43
|
+
}
|
|
44
|
+
function toolLine(part, result) {
|
|
45
|
+
const { verb, target, code } = ViewerTools.line(part.name, part.arguments);
|
|
46
|
+
const row = el('div');
|
|
47
|
+
row.className = `tool${(result === null || result === void 0 ? void 0 : result.isError) ? ' failed' : ''}`;
|
|
48
|
+
const dot = el('span');
|
|
49
|
+
dot.className = 'dot';
|
|
50
|
+
row.append(dot, Object.assign(el('span', verb), { className: 'verb' }));
|
|
51
|
+
if (target) {
|
|
52
|
+
const span = el('span', target.split('\n')[0]);
|
|
53
|
+
span.className = `target${code ? ' code' : ''}`;
|
|
54
|
+
span.title = target;
|
|
55
|
+
row.append(span);
|
|
56
|
+
}
|
|
57
|
+
const output = result ? text(result.content).words.trim() : '';
|
|
58
|
+
if (!output) {
|
|
59
|
+
return [row];
|
|
60
|
+
}
|
|
61
|
+
const details = el('details');
|
|
62
|
+
details.className = 'out';
|
|
63
|
+
details.append(el('summary', (result === null || result === void 0 ? void 0 : result.isError) ? t('error') : t('output')), el('pre', output));
|
|
64
|
+
return [row, details];
|
|
65
|
+
}
|
|
66
|
+
function conversation(main, item) {
|
|
67
|
+
var _a, _b, _c;
|
|
68
|
+
const doc = JSON.parse(new TextDecoder().decode(item.data));
|
|
69
|
+
document.title = `${doc.title || item.title} · Ta`;
|
|
70
|
+
main.append(el('h1', doc.title || item.title));
|
|
71
|
+
(_a = main.lastElementChild) === null || _a === void 0 ? void 0 : _a.classList.add('title');
|
|
72
|
+
const meta = el('p');
|
|
73
|
+
meta.className = 'meta';
|
|
74
|
+
const visible = doc.messages.filter(message => message.role !== 'toolResult').length;
|
|
75
|
+
meta.append(...[doc.place, t('sharedAt', { date: ViewerI18n.date(item.createdAt) }), t('messages', { n: visible })]
|
|
76
|
+
.filter(Boolean)
|
|
77
|
+
.map(part => el('span', part)));
|
|
78
|
+
main.append(meta);
|
|
79
|
+
const notes = [
|
|
80
|
+
((_b = doc.omitted) === null || _b === void 0 ? void 0 : _b.outputs) ? t('outputsOmitted') : '',
|
|
81
|
+
((_c = doc.omitted) === null || _c === void 0 ? void 0 : _c.images) ? t('imagesOmitted', { n: doc.omitted.images }) : '',
|
|
82
|
+
doc.redacted ? t('redacted', { n: doc.redacted }) : '',
|
|
83
|
+
].filter(Boolean);
|
|
84
|
+
if (notes.length) {
|
|
85
|
+
const box = el('div');
|
|
86
|
+
box.className = 'notes';
|
|
87
|
+
box.append(...notes.map(note => el('span', note)));
|
|
88
|
+
main.append(box);
|
|
89
|
+
}
|
|
90
|
+
const results = new Map();
|
|
91
|
+
for (const message of doc.messages) {
|
|
92
|
+
if (message.role === 'toolResult') {
|
|
93
|
+
results.set(message.toolCallId, message);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (const message of doc.messages) {
|
|
97
|
+
if (message.role === 'user') {
|
|
98
|
+
const { words, images } = text(message.content);
|
|
99
|
+
const said = [words.trim(), images ? Array.from({ length: images }, () => t('image')).join(' ') : ''].filter(Boolean).join('\n');
|
|
100
|
+
if (!said) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const row = el('div');
|
|
104
|
+
row.className = 'msg user';
|
|
105
|
+
row.append(Object.assign(el('div', said), { className: 'bubble' }));
|
|
106
|
+
main.append(row);
|
|
107
|
+
}
|
|
108
|
+
else if (message.role === 'assistant') {
|
|
109
|
+
const row = el('div');
|
|
110
|
+
row.className = 'msg ta';
|
|
111
|
+
let tools;
|
|
112
|
+
for (const part of message.content) {
|
|
113
|
+
if (part.type === 'text' && part.text.trim()) {
|
|
114
|
+
tools = undefined;
|
|
115
|
+
const block = el('div');
|
|
116
|
+
block.className = 'text';
|
|
117
|
+
block.append(ViewerMarkdown.render(part.text));
|
|
118
|
+
row.append(block);
|
|
119
|
+
}
|
|
120
|
+
else if (part.type === 'toolCall') {
|
|
121
|
+
if (!tools) {
|
|
122
|
+
tools = el('div');
|
|
123
|
+
tools.className = 'tools';
|
|
124
|
+
row.append(tools);
|
|
125
|
+
}
|
|
126
|
+
tools.append(...toolLine(part, results.get(part.id)));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (message.errorMessage) {
|
|
130
|
+
row.append(Object.assign(el('p', message.errorMessage), { className: 'err' }));
|
|
131
|
+
}
|
|
132
|
+
if (row.childNodes.length) {
|
|
133
|
+
main.append(row);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function file(main, item) {
|
|
139
|
+
var _a;
|
|
140
|
+
document.title = `${item.title} · Ta`;
|
|
141
|
+
const type = (_a = item.type) !== null && _a !== void 0 ? _a : 'application/octet-stream';
|
|
142
|
+
if (/^text\/html\b/.test(type)) {
|
|
143
|
+
main.classList.add('wide');
|
|
144
|
+
const frame = el('iframe');
|
|
145
|
+
frame.className = 'page';
|
|
146
|
+
frame.title = item.title;
|
|
147
|
+
// Its own origin, opaque: it reaches neither this page nor the key in its address.
|
|
148
|
+
frame.setAttribute('sandbox', 'allow-scripts allow-popups allow-popups-to-escape-sandbox');
|
|
149
|
+
frame.referrerPolicy = 'no-referrer';
|
|
150
|
+
const html = new TextDecoder().decode(item.data);
|
|
151
|
+
addEventListener('message', event => {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
if (event.source === frame.contentWindow && ((_a = event.data) === null || _a === void 0 ? void 0 : _a.ready)) {
|
|
154
|
+
(_b = frame.contentWindow) === null || _b === void 0 ? void 0 : _b.postMessage({ html }, '*');
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
frame.src = RELAY_FRAME_PATH;
|
|
158
|
+
main.append(frame);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
main.append(Object.assign(el('h1', item.title), { className: 'title' }));
|
|
162
|
+
const meta = el('p', t('sharedAt', { date: ViewerI18n.date(item.createdAt) }));
|
|
163
|
+
meta.className = 'meta';
|
|
164
|
+
main.append(meta);
|
|
165
|
+
const box = el('div');
|
|
166
|
+
box.className = 'file';
|
|
167
|
+
const blob = new Blob([item.data], { type });
|
|
168
|
+
if (/^image\/(png|jpe?g|gif|webp|avif|bmp)$/.test(type)) {
|
|
169
|
+
const image = el('img');
|
|
170
|
+
image.src = URL.createObjectURL(blob);
|
|
171
|
+
image.alt = item.title;
|
|
172
|
+
box.append(image);
|
|
173
|
+
}
|
|
174
|
+
else if (/markdown/.test(type) || /\.(md|markdown)$/i.test(item.title)) {
|
|
175
|
+
const block = el('div');
|
|
176
|
+
block.className = 'text';
|
|
177
|
+
block.append(ViewerMarkdown.render(new TextDecoder().decode(item.data)));
|
|
178
|
+
box.append(block);
|
|
179
|
+
}
|
|
180
|
+
else if (/^text\/|json|xml|yaml|javascript|typescript/.test(type)) {
|
|
181
|
+
box.append(el('pre', new TextDecoder().decode(item.data)));
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
const a = el('a', t('download', { name: item.title }));
|
|
185
|
+
a.href = URL.createObjectURL(blob);
|
|
186
|
+
a.download = item.title;
|
|
187
|
+
const button = el('button');
|
|
188
|
+
button.className = 'primary';
|
|
189
|
+
button.append(a);
|
|
190
|
+
a.style.color = 'inherit';
|
|
191
|
+
a.style.textDecoration = 'none';
|
|
192
|
+
box.append(button);
|
|
193
|
+
}
|
|
194
|
+
main.append(box);
|
|
195
|
+
}
|
|
196
|
+
/** An agent's card: its face as its mark in its colour, its name and role, and how to add it. */
|
|
197
|
+
function agent(main, item) {
|
|
198
|
+
const found = RelayAgentCards.parse(item.data);
|
|
199
|
+
if (!found) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
document.title = `${found.name} · Ta`;
|
|
203
|
+
const box = el('div');
|
|
204
|
+
box.className = 'agent';
|
|
205
|
+
const face = el('div', found.mark);
|
|
206
|
+
face.className = 'agent-face';
|
|
207
|
+
if (found.color) {
|
|
208
|
+
face.style.background = found.color;
|
|
209
|
+
}
|
|
210
|
+
const head = el('div');
|
|
211
|
+
head.className = 'agent-head';
|
|
212
|
+
head.append(face, Object.assign(el('h1', `@${found.name}`), { className: 'title' }), Object.assign(el('p', t('agentKind')), { className: 'meta' }));
|
|
213
|
+
const role = el('div');
|
|
214
|
+
role.className = 'text agent-role';
|
|
215
|
+
role.append(ViewerMarkdown.render(found.role));
|
|
216
|
+
box.append(head, role);
|
|
217
|
+
if (found.redacted) {
|
|
218
|
+
box.append(Object.assign(el('p', t('redacted', { n: found.redacted })), { className: 'notes' }));
|
|
219
|
+
}
|
|
220
|
+
box.append(Object.assign(el('p', t('agentHint')), { className: 'agent-hint' }));
|
|
221
|
+
main.append(box);
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
/** Lucide's lock, user, moon and circle-alert as paths, drawn as the rest of Ta draws its icons (stroke 2, round). */
|
|
225
|
+
const ICONS = {
|
|
226
|
+
lock: 'M5 11h14a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2zM7 11V7a5 5 0 0 1 10 0v4',
|
|
227
|
+
self: 'M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2M12 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8z',
|
|
228
|
+
offline: 'M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9z',
|
|
229
|
+
alert: 'M12 8v4M12 16h.01M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z',
|
|
230
|
+
};
|
|
231
|
+
function svgIcon(name) {
|
|
232
|
+
const ns = 'http://www.w3.org/2000/svg';
|
|
233
|
+
const svg = document.createElementNS(ns, 'svg');
|
|
234
|
+
for (const [key, value] of Object.entries({
|
|
235
|
+
viewBox: '0 0 24 24',
|
|
236
|
+
width: '20',
|
|
237
|
+
height: '20',
|
|
238
|
+
fill: 'none',
|
|
239
|
+
stroke: 'currentColor',
|
|
240
|
+
'stroke-width': '2',
|
|
241
|
+
'stroke-linecap': 'round',
|
|
242
|
+
'stroke-linejoin': 'round',
|
|
243
|
+
})) {
|
|
244
|
+
svg.setAttribute(key, value);
|
|
245
|
+
}
|
|
246
|
+
const path = document.createElementNS(ns, 'path');
|
|
247
|
+
path.setAttribute('d', ICONS[name]);
|
|
248
|
+
svg.append(path);
|
|
249
|
+
return svg;
|
|
250
|
+
}
|
|
251
|
+
function card(main, icon, title, hint) {
|
|
252
|
+
main.replaceChildren();
|
|
253
|
+
const box = el('div');
|
|
254
|
+
box.className = 'card';
|
|
255
|
+
const mark = el('div');
|
|
256
|
+
mark.className = 'icon';
|
|
257
|
+
mark.append(svgIcon(icon));
|
|
258
|
+
box.append(mark, el('h2', title));
|
|
259
|
+
if (hint) {
|
|
260
|
+
box.append(el('p', hint));
|
|
261
|
+
}
|
|
262
|
+
main.append(box);
|
|
263
|
+
return box;
|
|
264
|
+
}
|
|
265
|
+
function askPassword(main, error) {
|
|
266
|
+
const box = card(main, 'lock', t('passwordTitle'), t('passwordHint'));
|
|
267
|
+
const form = el('form');
|
|
268
|
+
const input = el('input');
|
|
269
|
+
input.type = 'password';
|
|
270
|
+
input.placeholder = t('password');
|
|
271
|
+
input.autocomplete = 'off';
|
|
272
|
+
input.required = true;
|
|
273
|
+
const button = el('button', t('open'));
|
|
274
|
+
button.className = 'primary';
|
|
275
|
+
form.append(input, button);
|
|
276
|
+
const message = el('p', error !== null && error !== void 0 ? error : '');
|
|
277
|
+
message.className = 'error';
|
|
278
|
+
box.append(form, message);
|
|
279
|
+
input.focus();
|
|
280
|
+
return new Promise(resolve => form.addEventListener('submit', event => {
|
|
281
|
+
event.preventDefault();
|
|
282
|
+
if (input.value) {
|
|
283
|
+
button.setAttribute('disabled', '');
|
|
284
|
+
message.textContent = '';
|
|
285
|
+
box.append(Object.assign(el('div'), { className: 'spin' }));
|
|
286
|
+
resolve(input.value);
|
|
287
|
+
}
|
|
288
|
+
}));
|
|
289
|
+
}
|
|
290
|
+
function chrome(name) {
|
|
291
|
+
const style = el('style', VIEWER_STYLE);
|
|
292
|
+
document.head.append(style);
|
|
293
|
+
const top = el('header');
|
|
294
|
+
top.className = 'top';
|
|
295
|
+
const copy = el('button', t('copy'));
|
|
296
|
+
copy.addEventListener('click', () => {
|
|
297
|
+
var _a;
|
|
298
|
+
void ((_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.writeText(location.href).then(() => {
|
|
299
|
+
copy.textContent = t('copied');
|
|
300
|
+
setTimeout(() => (copy.textContent = t('copy')), 1600);
|
|
301
|
+
}));
|
|
302
|
+
});
|
|
303
|
+
const title = el('span', name);
|
|
304
|
+
title.className = 'name';
|
|
305
|
+
top.append(Object.assign(el('span', 'Ta'), { className: 'logo' }), title, copy);
|
|
306
|
+
const main = el('main');
|
|
307
|
+
document.body.replaceChildren(top, main);
|
|
308
|
+
return main;
|
|
309
|
+
}
|
|
310
|
+
function footer(card) {
|
|
311
|
+
const foot = el('footer');
|
|
312
|
+
foot.className = 'foot';
|
|
313
|
+
// A card says how to add it above.
|
|
314
|
+
foot.append(...(card ? [] : [el('span', t('footer'))]), el('span', t('secure')));
|
|
315
|
+
document.body.append(foot);
|
|
316
|
+
}
|
|
317
|
+
async function show() {
|
|
318
|
+
var _a;
|
|
319
|
+
const main = chrome(t('title'));
|
|
320
|
+
let link;
|
|
321
|
+
try {
|
|
322
|
+
link = RelayContentUtil.parse(location.href);
|
|
323
|
+
}
|
|
324
|
+
catch (_b) {
|
|
325
|
+
card(main, 'alert', t('invalid'));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (link.access === 'self' || link.access === 'people') {
|
|
329
|
+
card(main, 'self', t(link.access));
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
let password;
|
|
333
|
+
let error;
|
|
334
|
+
for (;;) {
|
|
335
|
+
if (link.access === 'password') {
|
|
336
|
+
password = await askPassword(main, error);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
main.replaceChildren(Object.assign(el('div'), { className: 'spin' }));
|
|
340
|
+
(_a = main.firstElementChild) === null || _a === void 0 ? void 0 : _a.setAttribute('style', 'margin-top:20vh');
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
const item = await RelayContentUtil.fetch(link, { password });
|
|
344
|
+
main.replaceChildren();
|
|
345
|
+
const name = document.querySelector('.top .name');
|
|
346
|
+
if (name) {
|
|
347
|
+
name.textContent = item.title;
|
|
348
|
+
}
|
|
349
|
+
if (item.kind === RELAY_CONVERSATION_KIND) {
|
|
350
|
+
conversation(main, item);
|
|
351
|
+
}
|
|
352
|
+
else if (!(item.kind === RELAY_AGENT_KIND && agent(main, item))) {
|
|
353
|
+
file(main, item);
|
|
354
|
+
}
|
|
355
|
+
if (!main.classList.contains('wide')) {
|
|
356
|
+
footer(item.kind === RELAY_AGENT_KIND);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
catch (failure) {
|
|
361
|
+
const reason = failure instanceof RelayContentError ? failure.reason : 'failed';
|
|
362
|
+
if (link.access === 'password' && (reason === 'wrong' || reason === 'password')) {
|
|
363
|
+
error = t('wrong');
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (link.access === 'password' && reason === 'busy') {
|
|
367
|
+
error = t('busy');
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const known = ['busy', 'gone', 'offline', 'self', 'invalid', 'timeout', 'large'];
|
|
371
|
+
card(main, reason === 'offline' ? 'offline' : 'alert', t(known.includes(reason) ? reason : 'failed'));
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
addEventListener('hashchange', () => location.reload());
|
|
377
|
+
void show();
|
|
378
|
+
//# sourceMappingURL=viewer.js.map
|
package/lib/common/index.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
export * from './qr-code';
|
|
2
|
+
export * from './relay-blobs';
|
|
2
3
|
export * from './relay-bytes';
|
|
4
|
+
export * from './relay-card';
|
|
3
5
|
export * from './relay-cipher';
|
|
6
|
+
export * from './relay-content';
|
|
7
|
+
export * from './relay-conversation';
|
|
4
8
|
export * from './relay-files';
|
|
9
|
+
export * from './relay-hub';
|
|
10
|
+
export * from './relay-hub-util';
|
|
11
|
+
export * from './relay-hubs';
|
|
12
|
+
export * from './relay-identity';
|
|
5
13
|
export * from './relay-pairing';
|
|
6
14
|
export * from './relay-protocol';
|
|
7
15
|
export * from './relay-room';
|
|
16
|
+
export * from './relay-room-chat';
|
|
8
17
|
export * from './relay-share';
|
|
9
18
|
export * from './relay-socket';
|
|
10
19
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/common/index.js
CHANGED
|
@@ -15,12 +15,21 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./qr-code"), exports);
|
|
18
|
+
__exportStar(require("./relay-blobs"), exports);
|
|
18
19
|
__exportStar(require("./relay-bytes"), exports);
|
|
20
|
+
__exportStar(require("./relay-card"), exports);
|
|
19
21
|
__exportStar(require("./relay-cipher"), exports);
|
|
22
|
+
__exportStar(require("./relay-content"), exports);
|
|
23
|
+
__exportStar(require("./relay-conversation"), exports);
|
|
20
24
|
__exportStar(require("./relay-files"), exports);
|
|
25
|
+
__exportStar(require("./relay-hub"), exports);
|
|
26
|
+
__exportStar(require("./relay-hub-util"), exports);
|
|
27
|
+
__exportStar(require("./relay-hubs"), exports);
|
|
28
|
+
__exportStar(require("./relay-identity"), exports);
|
|
21
29
|
__exportStar(require("./relay-pairing"), exports);
|
|
22
30
|
__exportStar(require("./relay-protocol"), exports);
|
|
23
31
|
__exportStar(require("./relay-room"), exports);
|
|
32
|
+
__exportStar(require("./relay-room-chat"), exports);
|
|
24
33
|
__exportStar(require("./relay-share"), exports);
|
|
25
34
|
__exportStar(require("./relay-socket"), exports);
|
|
26
35
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Where a copy is kept: a Durable Object's storage, or a map in `ta relay`. Values are structured-cloned. */
|
|
2
|
+
export interface RelayBlobStorage {
|
|
3
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
4
|
+
put(key: string, value: unknown): Promise<void>;
|
|
5
|
+
deleteAll(): Promise<void>;
|
|
6
|
+
/** When the copy runs out: the storage deletes it then (an alarm), or the relay finds it so on the next request. */
|
|
7
|
+
setAlarm?(at: number): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
interface BlobMeta {
|
|
10
|
+
/** SHA-256 of the token of the host that wrote it first, who alone writes or deletes it. */
|
|
11
|
+
writeHash: string;
|
|
12
|
+
/** SHA-256 of what reads it (RelayContentUtil.authToken). */
|
|
13
|
+
authHash: string;
|
|
14
|
+
expiresAt: number;
|
|
15
|
+
size: number;
|
|
16
|
+
chunks: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
|
|
20
|
+
* deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
|
|
21
|
+
* nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
|
|
22
|
+
*/
|
|
23
|
+
export declare class RelayBlobs {
|
|
24
|
+
protected readonly storage: RelayBlobStorage;
|
|
25
|
+
protected readonly now: () => number;
|
|
26
|
+
/** The bytes of a copy at most, sealed. */
|
|
27
|
+
static readonly MAX_BYTES: number;
|
|
28
|
+
constructor(storage: RelayBlobStorage, now?: () => number);
|
|
29
|
+
handle(request: Request): Promise<Response>;
|
|
30
|
+
protected read(request: Request): Promise<Response>;
|
|
31
|
+
protected write(request: Request): Promise<Response>;
|
|
32
|
+
protected remove(request: Request): Promise<Response>;
|
|
33
|
+
/** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
|
|
34
|
+
protected meta(): Promise<BlobMeta | undefined>;
|
|
35
|
+
protected static bearer(request: Request): string;
|
|
36
|
+
protected static hash(text: string): Promise<string>;
|
|
37
|
+
protected static answer(status: number, text: string, headers?: Record<string, string>): Response;
|
|
38
|
+
}
|
|
39
|
+
/** What the host that shared content does with its copy on the relay. */
|
|
40
|
+
export declare namespace RelayBlobClient {
|
|
41
|
+
interface Copy {
|
|
42
|
+
relay: string;
|
|
43
|
+
share: string;
|
|
44
|
+
/** The host's token of the copy. */
|
|
45
|
+
token: string;
|
|
46
|
+
/** SHA-256 of the token that reads it. */
|
|
47
|
+
authHash: string;
|
|
48
|
+
expiresAt: number;
|
|
49
|
+
bytes: Uint8Array;
|
|
50
|
+
}
|
|
51
|
+
function put(copy: Copy): Promise<void>;
|
|
52
|
+
function remove(relay: string, share: string, token: string): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=relay-blobs.d.ts.map
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayBlobClient = exports.RelayBlobs = void 0;
|
|
4
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
6
|
+
/** Below the 2 MiB of a value of a Durable Object. */
|
|
7
|
+
const CHUNK_BYTES = 1024 * 1024;
|
|
8
|
+
/** The sealing adds an IV, a tag and the header of the content. */
|
|
9
|
+
const MAX_BYTES = relay_protocol_1.RELAY_CONTENT_MAX_BYTES + 64 * 1024;
|
|
10
|
+
const FAIL_WINDOW_MS = 10 * 60 * 1000;
|
|
11
|
+
const MAX_FAILS = 20;
|
|
12
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
13
|
+
const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'x-ta-auth', 'access-control-allow-methods': 'GET, OPTIONS' };
|
|
14
|
+
/**
|
|
15
|
+
* One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
|
|
16
|
+
* deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
|
|
17
|
+
* nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
|
|
18
|
+
*/
|
|
19
|
+
class RelayBlobs {
|
|
20
|
+
constructor(storage, now = Date.now) {
|
|
21
|
+
this.storage = storage;
|
|
22
|
+
this.now = now;
|
|
23
|
+
}
|
|
24
|
+
async handle(request) {
|
|
25
|
+
switch (request.method) {
|
|
26
|
+
case 'OPTIONS':
|
|
27
|
+
return new Response(null, { status: 204, headers: READ_HEADERS });
|
|
28
|
+
case 'GET':
|
|
29
|
+
return this.read(request);
|
|
30
|
+
case 'PUT':
|
|
31
|
+
return this.write(request);
|
|
32
|
+
case 'DELETE':
|
|
33
|
+
return this.remove(request);
|
|
34
|
+
default:
|
|
35
|
+
return RelayBlobs.answer(405, 'Not allowed.');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async read(request) {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
const meta = await this.meta();
|
|
41
|
+
if (!meta) {
|
|
42
|
+
return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
|
|
43
|
+
}
|
|
44
|
+
const fails = (_a = (await this.storage.get('fails'))) !== null && _a !== void 0 ? _a : { count: 0, since: 0 };
|
|
45
|
+
const now = this.now();
|
|
46
|
+
const recent = now - fails.since < FAIL_WINDOW_MS ? fails : { count: 0, since: now };
|
|
47
|
+
if (recent.count >= MAX_FAILS) {
|
|
48
|
+
return RelayBlobs.answer(429, 'Too many tries.', READ_HEADERS);
|
|
49
|
+
}
|
|
50
|
+
if ((await RelayBlobs.hash((_b = request.headers.get('x-ta-auth')) !== null && _b !== void 0 ? _b : '')) !== meta.authHash) {
|
|
51
|
+
await this.storage.put('fails', { count: recent.count + 1, since: recent.since });
|
|
52
|
+
return RelayBlobs.answer(403, 'Refused.', READ_HEADERS);
|
|
53
|
+
}
|
|
54
|
+
const parts = [];
|
|
55
|
+
for (let index = 0; index < meta.chunks; index++) {
|
|
56
|
+
const part = await this.storage.get(`c${index}`);
|
|
57
|
+
if (!part) {
|
|
58
|
+
return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
|
|
59
|
+
}
|
|
60
|
+
parts.push(new Uint8Array(part));
|
|
61
|
+
}
|
|
62
|
+
return new Response(relay_bytes_1.RelayBytes.concat(...parts), {
|
|
63
|
+
headers: { ...READ_HEADERS, 'content-type': 'application/octet-stream', 'cache-control': 'no-store' },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async write(request) {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
var _c;
|
|
69
|
+
const writeHash = await RelayBlobs.hash(RelayBlobs.bearer(request));
|
|
70
|
+
const before = await this.storage.get('meta');
|
|
71
|
+
if (before && before.writeHash !== writeHash && before.expiresAt > this.now()) {
|
|
72
|
+
return RelayBlobs.answer(403, 'Another host keeps this copy.');
|
|
73
|
+
}
|
|
74
|
+
const authHash = (_c = request.headers.get('x-ta-auth-hash')) !== null && _c !== void 0 ? _c : '';
|
|
75
|
+
const expiresAt = Number(request.headers.get('x-ta-expires'));
|
|
76
|
+
const now = this.now();
|
|
77
|
+
if (RelayBlobs.bearer(request).length < 32 ||
|
|
78
|
+
!/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
|
|
79
|
+
!(expiresAt > now) ||
|
|
80
|
+
expiresAt > now + relay_protocol_1.RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
|
|
81
|
+
return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
|
|
82
|
+
}
|
|
83
|
+
const length = Number(request.headers.get('content-length'));
|
|
84
|
+
if (length > MAX_BYTES) {
|
|
85
|
+
return RelayBlobs.answer(413, 'Too large.');
|
|
86
|
+
}
|
|
87
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
88
|
+
if (bytes.length === 0 || bytes.length > MAX_BYTES) {
|
|
89
|
+
return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
|
|
90
|
+
}
|
|
91
|
+
await this.storage.deleteAll();
|
|
92
|
+
const chunks = Math.ceil(bytes.length / CHUNK_BYTES);
|
|
93
|
+
for (let index = 0; index < chunks; index++) {
|
|
94
|
+
await this.storage.put(`c${index}`, bytes.slice(index * CHUNK_BYTES, (index + 1) * CHUNK_BYTES));
|
|
95
|
+
}
|
|
96
|
+
await this.storage.put('meta', { writeHash, authHash, expiresAt, size: bytes.length, chunks });
|
|
97
|
+
await ((_b = (_a = this.storage).setAlarm) === null || _b === void 0 ? void 0 : _b.call(_a, expiresAt));
|
|
98
|
+
return RelayBlobs.answer(201, 'Kept.');
|
|
99
|
+
}
|
|
100
|
+
async remove(request) {
|
|
101
|
+
const meta = await this.storage.get('meta');
|
|
102
|
+
if (meta && meta.writeHash !== (await RelayBlobs.hash(RelayBlobs.bearer(request)))) {
|
|
103
|
+
return RelayBlobs.answer(403, 'Refused.');
|
|
104
|
+
}
|
|
105
|
+
await this.storage.deleteAll();
|
|
106
|
+
return RelayBlobs.answer(200, 'Deleted.');
|
|
107
|
+
}
|
|
108
|
+
/** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
|
|
109
|
+
async meta() {
|
|
110
|
+
const meta = await this.storage.get('meta');
|
|
111
|
+
if (meta && meta.expiresAt <= this.now()) {
|
|
112
|
+
await this.storage.deleteAll();
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
return meta;
|
|
116
|
+
}
|
|
117
|
+
static bearer(request) {
|
|
118
|
+
var _a;
|
|
119
|
+
var _b, _c;
|
|
120
|
+
return (_c = (_a = /^Bearer (\S+)$/.exec((_b = request.headers.get('authorization')) !== null && _b !== void 0 ? _b : '')) === null || _a === void 0 ? void 0 : _a[1]) !== null && _c !== void 0 ? _c : '';
|
|
121
|
+
}
|
|
122
|
+
static async hash(text) {
|
|
123
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
124
|
+
return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(digest));
|
|
125
|
+
}
|
|
126
|
+
static answer(status, text, headers = {}) {
|
|
127
|
+
return new Response(`${text}\n`, { status, headers: { ...headers, 'content-type': 'text/plain; charset=utf-8' } });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.RelayBlobs = RelayBlobs;
|
|
131
|
+
/** The bytes of a copy at most, sealed. */
|
|
132
|
+
RelayBlobs.MAX_BYTES = MAX_BYTES;
|
|
133
|
+
/** What the host that shared content does with its copy on the relay. */
|
|
134
|
+
var RelayBlobClient;
|
|
135
|
+
(function (RelayBlobClient) {
|
|
136
|
+
async function put(copy) {
|
|
137
|
+
const response = await fetch(`${copy.relay}${relay_protocol_1.RELAY_BLOB_PATH}${copy.share}`, {
|
|
138
|
+
method: 'PUT',
|
|
139
|
+
headers: {
|
|
140
|
+
authorization: `Bearer ${copy.token}`,
|
|
141
|
+
'x-ta-auth-hash': copy.authHash,
|
|
142
|
+
'x-ta-expires': String(copy.expiresAt),
|
|
143
|
+
'content-type': 'application/octet-stream',
|
|
144
|
+
},
|
|
145
|
+
body: copy.bytes,
|
|
146
|
+
});
|
|
147
|
+
if (!response.ok) {
|
|
148
|
+
throw new Error(`The relay did not keep the copy: ${response.status} ${(await response.text()).trim()}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
RelayBlobClient.put = put;
|
|
152
|
+
async function remove(relay, share, token) {
|
|
153
|
+
await fetch(`${relay}${relay_protocol_1.RELAY_BLOB_PATH}${share}`, { method: 'DELETE', headers: { authorization: `Bearer ${token}` } });
|
|
154
|
+
}
|
|
155
|
+
RelayBlobClient.remove = remove;
|
|
156
|
+
})(RelayBlobClient || (exports.RelayBlobClient = RelayBlobClient = {}));
|
|
157
|
+
//# sourceMappingURL=relay-blobs.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
|
|
2
|
+
export declare const RELAY_AGENT_KIND = "ta-agent";
|
|
3
|
+
/**
|
|
4
|
+
* An agent of Ta as its card shares it: who it is and its role, what its secrets taken out (Redactor), not where it
|
|
5
|
+
* works nor what it said. The Ta that adds it fits the role to the project it is added for.
|
|
6
|
+
*/
|
|
7
|
+
export interface RelayAgentCard {
|
|
8
|
+
v: 1;
|
|
9
|
+
name: string;
|
|
10
|
+
role: string;
|
|
11
|
+
/** One or two characters on its avatar. */
|
|
12
|
+
mark: string;
|
|
13
|
+
/** Which of the faces of ta-channels it has. */
|
|
14
|
+
face?: number;
|
|
15
|
+
/** The colour of its face, for a page that does not draw the faces (the viewer of the relay). */
|
|
16
|
+
color?: string;
|
|
17
|
+
/** How many places of the role were hidden as secrets. */
|
|
18
|
+
redacted?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare namespace RelayAgentCards {
|
|
21
|
+
/** A card read from what a link shares; undefined for what is none. */
|
|
22
|
+
function parse(data: Uint8Array | string): RelayAgentCard | undefined;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=relay-card.d.ts.map
|