@agentsignalnet/claude-plugin 0.2.57 → 0.2.59
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/.claude-plugin/plugin.json +1 -1
- package/package.json +1 -1
- package/scripts/chat-viewer.mjs +341 -55
package/package.json
CHANGED
package/scripts/chat-viewer.mjs
CHANGED
|
@@ -68,7 +68,10 @@ function borrowFromMcpConfig() {
|
|
|
68
68
|
try {
|
|
69
69
|
config = JSON.parse(readFileSync(join(homedir(), '.claude.json'), 'utf8'));
|
|
70
70
|
} catch {
|
|
71
|
-
|
|
71
|
+
// An empty list, not null: every caller now treats this as "the servers
|
|
72
|
+
// that hold a key", and no file is none of them rather than an absence of
|
|
73
|
+
// an answer.
|
|
74
|
+
return [];
|
|
72
75
|
}
|
|
73
76
|
|
|
74
77
|
const found = [];
|
|
@@ -88,10 +91,24 @@ function borrowFromMcpConfig() {
|
|
|
88
91
|
};
|
|
89
92
|
walk(config);
|
|
90
93
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
/**
|
|
95
|
+
* ALL OF THEM, and the caller decides.
|
|
96
|
+
*
|
|
97
|
+
* This used to answer "ambiguous" above one and the viewer exited, on the
|
|
98
|
+
* reasoning that there is no principled way to CHOOSE -- which was true and
|
|
99
|
+
* is no longer the question. Nothing chooses now: every key this machine
|
|
100
|
+
* holds becomes a channel in the rail, named, and the reader picks by
|
|
101
|
+
* clicking. Refusing to start was only ever the right answer while showing
|
|
102
|
+
* one of them silently was the alternative.
|
|
103
|
+
*
|
|
104
|
+
* Two servers may be two ACCOUNTS rather than two channels of one, which is
|
|
105
|
+
* a real difference and not one that matters here: either way they are
|
|
106
|
+
* conversations this machine has the key for, and either way the rail says
|
|
107
|
+
* which is which. What DOES matter is the base URL, and the caller filters
|
|
108
|
+
* on it -- a key for a local API and a key for production are not two
|
|
109
|
+
* channels, they are two different APIs.
|
|
110
|
+
*/
|
|
111
|
+
return found;
|
|
95
112
|
}
|
|
96
113
|
|
|
97
114
|
/**
|
|
@@ -126,21 +143,16 @@ const port = Number(process.env.PORT ?? 8899);
|
|
|
126
143
|
*/
|
|
127
144
|
const token = randomUUID();
|
|
128
145
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
apiKey = borrowed.key;
|
|
140
|
-
baseUrl ??= borrowed.baseUrl;
|
|
141
|
-
console.log(`Using the API key from MCP server "${borrowed.name}" in ~/.claude.json.`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
146
|
+
/**
|
|
147
|
+
* Servers configured in ~/.claude.json that hold a key.
|
|
148
|
+
*
|
|
149
|
+
* Collected before the base URL is settled, because the first of them is
|
|
150
|
+
* allowed to decide it: somebody running the viewer against a local API has
|
|
151
|
+
* said so once, in the server they added, and should not have to say it again
|
|
152
|
+
* in the environment.
|
|
153
|
+
*/
|
|
154
|
+
const borrowed = apiKey ? [] : borrowFromMcpConfig();
|
|
155
|
+
if (borrowed.length) baseUrl ??= borrowed[0].baseUrl;
|
|
144
156
|
|
|
145
157
|
baseUrl = (baseUrl ?? 'https://api.agentsignal.net').replace(/\/+$/, '');
|
|
146
158
|
|
|
@@ -165,36 +177,166 @@ const identities = identitiesHere(baseUrl);
|
|
|
165
177
|
* Announced, like the borrowed one, because a viewer that goes looking for
|
|
166
178
|
* credentials silently is indistinguishable from one that steals them.
|
|
167
179
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
180
|
+
/**
|
|
181
|
+
* EVERY CHANNEL THIS MACHINE CAN OPEN, not one of them.
|
|
182
|
+
*
|
|
183
|
+
* This used to refuse: two different keys meant two channels, and rather than
|
|
184
|
+
* choose wrong it stopped and asked for AGENTSIGNAL_API_KEY. Choosing wrong
|
|
185
|
+
* would indeed be bad -- it shows somebody else's conversation -- but refusing
|
|
186
|
+
* is not the only alternative to guessing. Showing both and saying which is
|
|
187
|
+
* which is the other, and it is what somebody who joined two channels
|
|
188
|
+
* actually wanted.
|
|
189
|
+
*
|
|
190
|
+
* ARCHIVED IDENTITIES COUNT. Joining a second channel mints a new recipient,
|
|
191
|
+
* because a recipient belongs to one channel, and the previous identity is
|
|
192
|
+
* moved to `previous/` rather than deleted -- deliberately, so nothing is
|
|
193
|
+
* lost. That archive is therefore exactly where the FIRST channel's key lives
|
|
194
|
+
* once a second channel has been joined, which is the moment somebody most
|
|
195
|
+
* wants to see both. Read here, never written, and any key the API refuses is
|
|
196
|
+
* dropped quietly at startup rather than taken as a reason not to run.
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Which channel a key opens, read off the key.
|
|
200
|
+
*
|
|
201
|
+
* Local, so the rail can name the channels before a single request is made,
|
|
202
|
+
* and so two keys for one channel can be recognised as one entry rather than
|
|
203
|
+
* shown as two.
|
|
204
|
+
*/
|
|
205
|
+
function channelOfKey(key) {
|
|
206
|
+
const parts = String(key).split('_');
|
|
207
|
+
return parts.length >= 4 && parts[0] === 'as' && (parts[1] === 'live' || parts[1] === 'test')
|
|
208
|
+
? parts[2]
|
|
209
|
+
: null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function keyOf(holder) {
|
|
213
|
+
try {
|
|
214
|
+
return holder.api_key_ref ? readKeychain(holder.api_key_ref) : holder.api_key;
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// Named rather than swallowed. A key that exists and cannot be read is a
|
|
217
|
+
// locked keychain, not a missing key, and the fix is different.
|
|
172
218
|
console.error(
|
|
173
|
-
|
|
174
|
-
.
|
|
175
|
-
.join(', ')}).\n` +
|
|
176
|
-
'Those are different channels, so set AGENTSIGNAL_API_KEY to say which.',
|
|
219
|
+
`${holder.handle ?? holder.key} holds a key at ${holder.api_key_ref}, ` +
|
|
220
|
+
`and it could not be read (${error.message}). That channel is not shown.`,
|
|
177
221
|
);
|
|
178
|
-
|
|
222
|
+
return null;
|
|
179
223
|
}
|
|
180
|
-
|
|
181
|
-
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Identities this machine has been, including the ones it no longer is. */
|
|
227
|
+
function everyHolder() {
|
|
228
|
+
const holders = identities.filter((who) => who.api_key_ref || who.api_key);
|
|
229
|
+
const seen = new Set(holders.map((who) => who.key));
|
|
230
|
+
|
|
231
|
+
for (const who of identities) {
|
|
232
|
+
let names;
|
|
182
233
|
try {
|
|
183
|
-
|
|
184
|
-
} catch
|
|
185
|
-
|
|
186
|
-
// be a lie -- there is one, it is exactly where it says, and the fix is
|
|
187
|
-
// to unlock the keychain rather than to go and find a key.
|
|
188
|
-
console.error(
|
|
189
|
-
`${holder.handle ?? holder.key} holds a key at ${holder.api_key_ref}, ` +
|
|
190
|
-
`and it could not be read.\n${error.message}\n` +
|
|
191
|
-
'Unlock the keychain, or set AGENTSIGNAL_API_KEY for this run.',
|
|
192
|
-
);
|
|
193
|
-
process.exit(1);
|
|
234
|
+
names = readdirSync(join(who.dir, 'previous'));
|
|
235
|
+
} catch {
|
|
236
|
+
continue;
|
|
194
237
|
}
|
|
238
|
+
for (const name of names) {
|
|
239
|
+
if (!name.endsWith('.json')) continue;
|
|
240
|
+
try {
|
|
241
|
+
const past = JSON.parse(readFileSync(join(who.dir, 'previous', name), 'utf8'));
|
|
242
|
+
if (past.base_url !== baseUrl) continue;
|
|
243
|
+
if (!past.api_key && !past.api_key_ref) continue;
|
|
244
|
+
if (seen.has(past.key)) continue;
|
|
245
|
+
seen.add(past.key);
|
|
246
|
+
holders.push({ ...past, dir: who.dir, archived: true });
|
|
247
|
+
} catch {
|
|
248
|
+
/* not an identity */
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return holders;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The channels, in the order they will be offered.
|
|
257
|
+
*
|
|
258
|
+
* An explicit AGENTSIGNAL_API_KEY still wins outright: somebody who named a
|
|
259
|
+
* key meant that key, and quietly adding channels beside it would undo the
|
|
260
|
+
* only way there is to say "just this one".
|
|
261
|
+
*/
|
|
262
|
+
const channels = [];
|
|
263
|
+
|
|
264
|
+
if (apiKey) {
|
|
265
|
+
channels.push({ key: apiKey, via: 'AGENTSIGNAL_API_KEY', ref: null, name: null });
|
|
266
|
+
} else {
|
|
267
|
+
/**
|
|
268
|
+
* DEDUPED BY CHANNEL, not by key.
|
|
269
|
+
*
|
|
270
|
+
* Every agent gets its OWN key, so two agents this machine has been in the
|
|
271
|
+
* same channel are two different keys opening one conversation -- and keyed
|
|
272
|
+
* by value they both appeared, listing the same channel twice under two
|
|
273
|
+
* handles. The channel is in the key: they are minted
|
|
274
|
+
* as_{live|test}_{channelRef}_{secret}, the same shape a join code has and
|
|
275
|
+
* the same position.
|
|
276
|
+
*
|
|
277
|
+
* A key of an unrecognised shape keys on its own value, which lists it
|
|
278
|
+
* separately rather than silently merging it with something it may not be.
|
|
279
|
+
*/
|
|
280
|
+
const byChannel = new Map();
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* CONFIGURED SERVERS FIRST, then identities.
|
|
284
|
+
*
|
|
285
|
+
* A key somebody put in a config is a key they chose; a key found in an
|
|
286
|
+
* identity file is one this machine minted for itself. When both open the
|
|
287
|
+
* same channel the chosen one wins, which is the same precedence the
|
|
288
|
+
* environment already gets over everything.
|
|
289
|
+
*
|
|
290
|
+
* Filtered on base URL, and this is the one place the difference bites: two
|
|
291
|
+
* servers may be two accounts, which is fine and shows as two channels, but
|
|
292
|
+
* a server pointed at a local API is not a channel of the production one.
|
|
293
|
+
* Mixing those would put a development conversation in the same rail as a
|
|
294
|
+
* real one, named identically, with no way to tell them apart.
|
|
295
|
+
*/
|
|
296
|
+
for (const server of borrowed) {
|
|
297
|
+
const at = (server.baseUrl ?? baseUrl).replace(/\/+$/, '');
|
|
298
|
+
if (at !== baseUrl) continue;
|
|
299
|
+
const ref = channelOfKey(server.key);
|
|
300
|
+
if (byChannel.has(ref ?? server.key)) continue;
|
|
301
|
+
byChannel.set(ref ?? server.key, {
|
|
302
|
+
key: server.key,
|
|
303
|
+
via: `MCP server "${server.name}"`,
|
|
304
|
+
from: join(homedir(), '.claude.json'),
|
|
305
|
+
ref,
|
|
306
|
+
name: null,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
for (const holder of everyHolder()) {
|
|
311
|
+
const key = keyOf(holder);
|
|
312
|
+
if (!key) continue;
|
|
313
|
+
const ref = channelOfKey(key);
|
|
314
|
+
const at = ref ?? key;
|
|
315
|
+
// First wins, and the live identity is first, so a channel this machine
|
|
316
|
+
// is still in is opened with the key it currently holds rather than with
|
|
317
|
+
// a displaced one that may since have been revoked.
|
|
318
|
+
if (byChannel.has(at)) continue;
|
|
319
|
+
byChannel.set(at, {
|
|
320
|
+
key,
|
|
321
|
+
via: `${holder.handle ?? holder.key}${holder.archived ? ' (previously)' : ''}`,
|
|
322
|
+
// WHERE it came from, kept so startup can say it. A viewer that goes
|
|
323
|
+
// looking for credentials silently is indistinguishable from one that
|
|
324
|
+
// steals them, and naming the holder is not naming the source.
|
|
325
|
+
from: holder.api_key_ref ?? join(holder.dir, holder.archived ? 'previous' : '', 'agent.json'),
|
|
326
|
+
ref,
|
|
327
|
+
name: null,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
channels.push(...byChannel.values());
|
|
331
|
+
// The first is the default, and the tools that act rather than read use it.
|
|
332
|
+
apiKey = channels[0]?.key;
|
|
333
|
+
|
|
334
|
+
if (channels.length) {
|
|
195
335
|
console.log(
|
|
196
|
-
|
|
197
|
-
|
|
336
|
+
channels.length === 1
|
|
337
|
+
? `Using ${channels[0].via}'s own channel key, from ${channels[0].from}.`
|
|
338
|
+
: `Watching ${channels.length} channels:\n` +
|
|
339
|
+
channels.map((c) => ` ${c.ref ?? 'channel'} — ${c.via}'s key, from ${c.from}`).join('\n'),
|
|
198
340
|
);
|
|
199
341
|
}
|
|
200
342
|
}
|
|
@@ -670,7 +812,8 @@ const PAGE = `<!doctype html>
|
|
|
670
812
|
</header>
|
|
671
813
|
<div id="shell">
|
|
672
814
|
<aside id="side" aria-label="Channel">
|
|
673
|
-
<p class="rail-title">
|
|
815
|
+
<div id="channels" hidden><p class="rail-title">Channels</p><nav id="chan-nav"></nav></div>
|
|
816
|
+
<p class="rail-title">In this channel</p>
|
|
674
817
|
<nav id="who-nav"></nav>
|
|
675
818
|
<details id="setup"><summary>This machine</summary><div id="setupbody">reading…</div></details>
|
|
676
819
|
</aside>
|
|
@@ -902,7 +1045,10 @@ const PAGE = `<!doctype html>
|
|
|
902
1045
|
async function roster() {
|
|
903
1046
|
let list;
|
|
904
1047
|
try {
|
|
905
|
-
|
|
1048
|
+
// Same channel the log is showing, carried from the URL for the same
|
|
1049
|
+
// reason the poll carries it.
|
|
1050
|
+
const on = new URLSearchParams(location.search).get('channel');
|
|
1051
|
+
list = await (await fetch(q('/agents' + (on ? '?channel=' + encodeURIComponent(on) : '')))).json();
|
|
906
1052
|
} catch {
|
|
907
1053
|
return;
|
|
908
1054
|
}
|
|
@@ -1039,6 +1185,41 @@ const PAGE = `<!doctype html>
|
|
|
1039
1185
|
branch(null, 0);
|
|
1040
1186
|
}
|
|
1041
1187
|
|
|
1188
|
+
/**
|
|
1189
|
+
* The channels this machine can open, when there is more than one.
|
|
1190
|
+
*
|
|
1191
|
+
* HIDDEN AT ONE, because a switcher offering a single choice is furniture:
|
|
1192
|
+
* it takes rail space to say what the header already says. It appears the
|
|
1193
|
+
* moment a second channel exists and not before.
|
|
1194
|
+
*
|
|
1195
|
+
* A real link rather than in-page filtering, unlike the agent nav below.
|
|
1196
|
+
* Switching channel changes which KEY the server polls with and which
|
|
1197
|
+
* history file it reads, so it is a different conversation rather than a
|
|
1198
|
+
* subset of this one -- and reloading is honest about that. The token is
|
|
1199
|
+
* carried through, because dropping it is what made the agent links
|
|
1200
|
+
* answer "Not found" before they stopped being links at all.
|
|
1201
|
+
*/
|
|
1202
|
+
function renderChannels(list) {
|
|
1203
|
+
const box = document.getElementById('channels');
|
|
1204
|
+
if (!box) return;
|
|
1205
|
+
if (!list || list.length < 2) {
|
|
1206
|
+
box.hidden = true;
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
box.hidden = false;
|
|
1210
|
+
document.getElementById('chan-nav').innerHTML = list
|
|
1211
|
+
.map((c) => {
|
|
1212
|
+
const href = c.ref ? q('/?channel=' + encodeURIComponent(c.ref)) : q('/');
|
|
1213
|
+
return (
|
|
1214
|
+
'<a href="' + esc(href) + '" class="' + (c.active ? 'on' : '') + '">' +
|
|
1215
|
+
'<span class="stack"><span class="nm">' + esc(c.name || c.ref || 'channel') + '</span>' +
|
|
1216
|
+
(c.ref ? '<small class="alias">' + esc(c.ref) + '</small>' : '') +
|
|
1217
|
+
'</span></a>'
|
|
1218
|
+
);
|
|
1219
|
+
})
|
|
1220
|
+
.join('');
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1042
1223
|
/** Whether a nav link is the one currently filtering. Key or handle, either may be in the URL. */
|
|
1043
1224
|
function matchesNav(link) {
|
|
1044
1225
|
const at = link.dataset.agent ?? '';
|
|
@@ -1143,7 +1324,20 @@ const PAGE = `<!doctype html>
|
|
|
1143
1324
|
async function tick() {
|
|
1144
1325
|
let data;
|
|
1145
1326
|
try {
|
|
1146
|
-
|
|
1327
|
+
/**
|
|
1328
|
+
* The channel comes from the URL, not from memory.
|
|
1329
|
+
*
|
|
1330
|
+
* Switching is a navigation, so the selection lives in location.search
|
|
1331
|
+
* and the poll has to carry it -- without this the page would sit at
|
|
1332
|
+
* /?channel=other while every two seconds asking for, and rendering,
|
|
1333
|
+
* the default channel's traffic. The address bar would say one thing
|
|
1334
|
+
* and the conversation another.
|
|
1335
|
+
*/
|
|
1336
|
+
const chan = new URLSearchParams(location.search).get('channel');
|
|
1337
|
+
const params = [];
|
|
1338
|
+
if (AGENT) params.push('agent=' + encodeURIComponent(AGENT));
|
|
1339
|
+
if (chan) params.push('channel=' + encodeURIComponent(chan));
|
|
1340
|
+
data = await (await fetch(q('/messages' + (params.length ? '?' + params.join('&') : '')))).json();
|
|
1147
1341
|
} catch {
|
|
1148
1342
|
document.getElementById('dot').style.background = '#f85149';
|
|
1149
1343
|
return;
|
|
@@ -1159,6 +1353,7 @@ const PAGE = `<!doctype html>
|
|
|
1159
1353
|
|
|
1160
1354
|
document.getElementById('channel').textContent =
|
|
1161
1355
|
data.channel ? data.channel.name + ' · ' + data.channel.ref : '';
|
|
1356
|
+
renderChannels(data.channels);
|
|
1162
1357
|
document.getElementById('count').textContent = data.messages.length;
|
|
1163
1358
|
|
|
1164
1359
|
const log = document.getElementById('log');
|
|
@@ -1436,7 +1631,15 @@ const PAGE = `<!doctype html>
|
|
|
1436
1631
|
const note = document.getElementById('sent');
|
|
1437
1632
|
if (!to || !say.value.trim()) return;
|
|
1438
1633
|
note.textContent = 'sending…';
|
|
1439
|
-
|
|
1634
|
+
// The channel on screen, said explicitly. A message composed under a
|
|
1635
|
+
// Sandbox header and delivered into a different channel is the worst
|
|
1636
|
+
// outcome this switcher could produce, so the view names its own channel
|
|
1637
|
+
// rather than letting the server assume the default.
|
|
1638
|
+
const result = await post('/send', {
|
|
1639
|
+
to,
|
|
1640
|
+
body: say.value.trim(),
|
|
1641
|
+
channel: new URLSearchParams(location.search).get('channel') ?? undefined,
|
|
1642
|
+
});
|
|
1440
1643
|
if (result.error) {
|
|
1441
1644
|
note.textContent = result.error;
|
|
1442
1645
|
return;
|
|
@@ -1874,11 +2077,23 @@ const PAGE = `<!doctype html>
|
|
|
1874
2077
|
</body>
|
|
1875
2078
|
</html>`;
|
|
1876
2079
|
|
|
2080
|
+
/**
|
|
2081
|
+
* The channel a request is for.
|
|
2082
|
+
*
|
|
2083
|
+
* Matched on ref once known, and otherwise the first -- which is the identity
|
|
2084
|
+
* this machine currently is. An unknown ref falls back rather than erroring:
|
|
2085
|
+
* a stale bookmark should show something, not a stack trace.
|
|
2086
|
+
*/
|
|
2087
|
+
function channelFor(ref) {
|
|
2088
|
+
if (!ref) return channels[0];
|
|
2089
|
+
return channels.find((c) => c.ref === ref) ?? channels[0];
|
|
2090
|
+
}
|
|
2091
|
+
|
|
1877
2092
|
/** One upstream call, one place that turns a non-2xx into something readable. */
|
|
1878
|
-
async function upstream(path) {
|
|
2093
|
+
async function upstream(path, key = apiKey) {
|
|
1879
2094
|
try {
|
|
1880
2095
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
1881
|
-
headers: { Authorization: `Bearer ${
|
|
2096
|
+
headers: { Authorization: `Bearer ${key}` },
|
|
1882
2097
|
});
|
|
1883
2098
|
const body = await response.json();
|
|
1884
2099
|
return response.ok
|
|
@@ -2710,7 +2925,7 @@ const server = createServer(async (req, res) => {
|
|
|
2710
2925
|
const body = await upstreamPost(
|
|
2711
2926
|
'/v1/messages',
|
|
2712
2927
|
{ to: payload.to, body: payload.body, ...(payload.title ? { title: payload.title } : {}) },
|
|
2713
|
-
`Bearer ${
|
|
2928
|
+
`Bearer ${channelFor(payload.channel ?? url.searchParams.get('channel')).key}`,
|
|
2714
2929
|
);
|
|
2715
2930
|
res.writeHead(body.error ? 400 : 200, { 'Content-Type': 'application/json' });
|
|
2716
2931
|
res.end(JSON.stringify(body));
|
|
@@ -2732,16 +2947,34 @@ const server = createServer(async (req, res) => {
|
|
|
2732
2947
|
* messages newer than a moment that has not happened yet and never see
|
|
2733
2948
|
* the ones in between.
|
|
2734
2949
|
*/
|
|
2735
|
-
|
|
2950
|
+
/**
|
|
2951
|
+
* WHICH CHANNEL, and therefore which key.
|
|
2952
|
+
*
|
|
2953
|
+
* Defaults to the first, which is the identity this machine is now. The
|
|
2954
|
+
* page names the others and links to them; nothing is guessed from the
|
|
2955
|
+
* traffic.
|
|
2956
|
+
*/
|
|
2957
|
+
const want = url.searchParams.get('channel');
|
|
2958
|
+
const on = channelFor(want);
|
|
2959
|
+
|
|
2960
|
+
// Per channel, because history is per channel: sharing one `since` across
|
|
2961
|
+
// two would make each ask for messages newer than the other's newest and
|
|
2962
|
+
// leave a hole in whichever is quieter.
|
|
2963
|
+
const kept = readHistory(on.ref ?? channelRef);
|
|
2736
2964
|
const newest = kept.length ? kept[0].created_at : null;
|
|
2737
2965
|
const body = await upstream(
|
|
2738
2966
|
'/v1/messages?limit=100' + (newest ? '&since=' + encodeURIComponent(newest) : ''),
|
|
2967
|
+
on.key,
|
|
2739
2968
|
);
|
|
2740
2969
|
|
|
2741
2970
|
if (!body.error) {
|
|
2742
2971
|
// The ref is only known once the API has answered once; until then the
|
|
2743
2972
|
// history lands in a generic file and is adopted on the next start.
|
|
2744
|
-
|
|
2973
|
+
if (body.channel?.ref) {
|
|
2974
|
+
on.ref = body.channel.ref;
|
|
2975
|
+
on.name = body.channel.name ?? on.name;
|
|
2976
|
+
}
|
|
2977
|
+
channelRef = on.ref ?? channelRef;
|
|
2745
2978
|
|
|
2746
2979
|
const fresh = (body.messages ?? []).map(keepable).filter(Boolean);
|
|
2747
2980
|
// Deduplicated by id, because `since` is inclusive at the boundary on a
|
|
@@ -2749,7 +2982,15 @@ const server = createServer(async (req, res) => {
|
|
|
2749
2982
|
const byId = new Map(kept.map((m) => [m.id, m]));
|
|
2750
2983
|
for (const m of fresh) byId.set(m.id, m);
|
|
2751
2984
|
|
|
2752
|
-
body.messages = writeHistory(channelRef, [...byId.values()], settings().history_days);
|
|
2985
|
+
body.messages = writeHistory(on.ref ?? channelRef, [...byId.values()], settings().history_days);
|
|
2986
|
+
// Every channel this viewer can open, so the page can offer them. The
|
|
2987
|
+
// key never leaves this process -- only the ref and the name.
|
|
2988
|
+
body.channels = channels.map((c) => ({
|
|
2989
|
+
ref: c.ref,
|
|
2990
|
+
name: c.name ?? c.via,
|
|
2991
|
+
via: c.via,
|
|
2992
|
+
active: c === on,
|
|
2993
|
+
}));
|
|
2753
2994
|
// What this call actually cost, so the page can say so rather than
|
|
2754
2995
|
// implying every refresh is free or that every refresh is billed.
|
|
2755
2996
|
body.fetched = fresh.length;
|
|
@@ -2794,7 +3035,12 @@ const server = createServer(async (req, res) => {
|
|
|
2794
3035
|
* than kept beside its replacement. Two paths to one value is how the
|
|
2795
3036
|
* copies in this codebase drift.
|
|
2796
3037
|
*/
|
|
2797
|
-
|
|
3038
|
+
// The SELECTED channel's roster, not the default one's. Without this the
|
|
3039
|
+
// conversation switched and the list of who is in it did not, so the rail
|
|
3040
|
+
// named agents that had never been in the channel on screen -- which reads
|
|
3041
|
+
// as the directory being wrong rather than as the viewer asking the wrong
|
|
3042
|
+
// one.
|
|
3043
|
+
const body = await upstream('/v1/agents', channelFor(url.searchParams.get('channel')).key);
|
|
2798
3044
|
|
|
2799
3045
|
|
|
2800
3046
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
@@ -2817,6 +3063,41 @@ const server = createServer(async (req, res) => {
|
|
|
2817
3063
|
|
|
2818
3064
|
resolveSelf();
|
|
2819
3065
|
|
|
3066
|
+
/**
|
|
3067
|
+
* Learn what the other channels are CALLED, once, in the background.
|
|
3068
|
+
*
|
|
3069
|
+
* The ref comes free out of the key, but the name does not, and a rail that
|
|
3070
|
+
* lists one channel by name and the rest by whichever handle happens to hold
|
|
3071
|
+
* their key is a rail that looks broken. The active channel names itself on
|
|
3072
|
+
* its first poll; this is for the ones nobody has opened yet.
|
|
3073
|
+
*
|
|
3074
|
+
* `since` set to now so the read comes back EMPTY and is refunded -- the
|
|
3075
|
+
* channel block arrives either way. Naming the rail should not cost a
|
|
3076
|
+
* delivery per channel per start.
|
|
3077
|
+
*
|
|
3078
|
+
* Failures are left alone deliberately: a key that has been revoked since it
|
|
3079
|
+
* was archived simply keeps its ref as a label, and the channel is still
|
|
3080
|
+
* listed rather than vanishing. Finding out it does not work by clicking it
|
|
3081
|
+
* is better than being quietly shown one channel and told nothing.
|
|
3082
|
+
*/
|
|
3083
|
+
async function nameChannels() {
|
|
3084
|
+
const now = new Date().toISOString();
|
|
3085
|
+
await Promise.all(
|
|
3086
|
+
// SKIP THE FIRST. It is the one the page opens on, so its first poll names
|
|
3087
|
+
// it for free -- asking here as well is a second request for an answer
|
|
3088
|
+
// already on its way, and it breaks the property that a viewer watching
|
|
3089
|
+
// one channel makes exactly one call to fetch and one to keep up.
|
|
3090
|
+
channels.slice(1).map(async (c) => {
|
|
3091
|
+
if (c.name) return;
|
|
3092
|
+
const body = await upstream(`/v1/messages?limit=1&since=${encodeURIComponent(now)}`, c.key);
|
|
3093
|
+
if (body?.channel?.name) {
|
|
3094
|
+
c.name = body.channel.name;
|
|
3095
|
+
c.ref = body.channel.ref ?? c.ref;
|
|
3096
|
+
}
|
|
3097
|
+
}),
|
|
3098
|
+
);
|
|
3099
|
+
}
|
|
3100
|
+
|
|
2820
3101
|
server.listen(port, '127.0.0.1', () => {
|
|
2821
3102
|
// The port the OS actually gave us, not the one asked for. They differ
|
|
2822
3103
|
// whenever PORT is 0 -- which is how anything automated starts a server
|
|
@@ -2826,4 +3107,9 @@ server.listen(port, '127.0.0.1', () => {
|
|
|
2826
3107
|
console.error(`Watching ${baseUrl} on http://127.0.0.1:${bound}/?t=${token}`);
|
|
2827
3108
|
console.error('Loopback only, and the link carries a one-run token: it can now send and answer.');
|
|
2828
3109
|
console.error('The key stays in this process; the page only ever talks to this server.');
|
|
3110
|
+
// After the link is printed, not before: the viewer should be usable the
|
|
3111
|
+
// moment it says it is, and the names arrive a beat later.
|
|
3112
|
+
nameChannels().catch(() => {
|
|
3113
|
+
/* the rail falls back to refs, which is a label and not a failure */
|
|
3114
|
+
});
|
|
2829
3115
|
});
|