@volter/teams 0.1.73 → 0.1.75

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.
@@ -54,7 +54,8 @@ export declare class TurnDeliveryUnconfirmedError extends Error {
54
54
  /**
55
55
  * One line typed into the channel's session as its user's own turn; rejects when the machine refuses the stream.
56
56
  * `turnKey`: one per typed line, reused on retry, so the machine types the line once.
57
- * Receipt waits time out after thirty seconds. Cancellation stops waiting; it
57
+ * Receipt waits time out after thirty seconds and retain at most 256 KB of UTF-8 text.
58
+ * Cancellation stops waiting; it
58
59
  * does not undo delivery. After acceptance, a lost, invalid or cancelled receipt
59
60
  * rejects with TurnDeliveryUnconfirmedError. Reuse the same key when retrying it.
60
61
  */
@@ -185,7 +185,7 @@ export async function sendBrowserTurn(channel, text, { turnKey, signal } = {}) {
185
185
  signal?.throwIfAborted();
186
186
  const stream = await channel.open('mail', { request: { op: 'turn', text, ...(turnKey ? { turn_key: String(turnKey) } : {}) } });
187
187
  return new Promise((resolve, reject) => {
188
- let answer = '', settled = false;
188
+ let answer = '', bytes = 0, settled = false;
189
189
  const cleanup = () => { clearTimeout(timer); stream.off('data', data); stream.off('close', closed); signal?.removeEventListener('abort', cancelled); };
190
190
  const fail = error => {
191
191
  if (settled) return;
@@ -196,7 +196,13 @@ export async function sendBrowserTurn(channel, text, { turnKey, signal } = {}) {
196
196
  };
197
197
  const cancelled = () => fail(new TurnDeliveryUnconfirmedError('Turn delivery wait cancelled'));
198
198
  const timer = setTimeout(() => fail(new TurnDeliveryUnconfirmedError('Turn delivery acknowledgement timed out')), 30_000);
199
- const data = chunk => { answer += chunk; if (answer.length > 256_000) fail(new TurnDeliveryUnconfirmedError('Turn delivery receipt too large')); };
199
+ const data = chunk => {
200
+ if (typeof chunk !== 'string') { fail(new TurnDeliveryUnconfirmedError('Invalid turn delivery receipt frame')); return; }
201
+ if (chunk.length > 256_000 - bytes) { fail(new TurnDeliveryUnconfirmedError('Turn delivery receipt too large')); return; }
202
+ bytes += encoder.encode(chunk).byteLength;
203
+ if (bytes > 256_000) { fail(new TurnDeliveryUnconfirmedError('Turn delivery receipt too large')); return; }
204
+ answer += chunk;
205
+ };
200
206
  const closed = reason => {
201
207
  if (settled) return;
202
208
  settled = true; cleanup();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.1.73",
3
+ "version": "0.1.75",
4
4
  "type": "module",
5
5
  "description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
6
6
  "license": "MIT",
@@ -210,19 +210,36 @@ export function withPeople(transcript, messages, { agentName = null, sessionId =
210
210
  export function withMail(transcript, rows, { agentName = 'This session', agentId = '', agentImage: picture = agentImage } = {}) {
211
211
  // An older mailbox must not jump ahead of the page of history being viewed.
212
212
  // Its envelopes become visible when the person loads that earlier history.
213
- const times = transcript.map(entry => entry.ts).filter(Number.isFinite);
214
- if (times.length) rows = rows.filter(row => row.at >= Math.min(...times));
213
+ let earliest = Infinity;
214
+ for (const entry of transcript) if (Number.isFinite(entry.ts)) earliest = Math.min(earliest, entry.ts);
215
+ if (earliest !== Infinity) rows = rows.filter(row => row.at >= earliest);
215
216
  const nameOf = actor => actor?.self ? agentName : actor?.name || 'Unknown sender';
216
217
  const mail = rows.map(row => ({ id: `mail:${row.id}`, role: 'notice', text: row.text, ts: row.at, truncated: false,
217
218
  mail: { id: row.id, from: nameOf(row.fromActor), to: row.toActor ? nameOf(row.toActor) : row.direction === 'in' ? agentName : 'Another session',
218
219
  image: row.fromActor?.kind === 'agent' ? picture(row.fromActor.self ? agentId : row.fromActor.id) : `https://brand.volter.ai/bot/svg?seed=${encodeURIComponent(row.fromActor?.id || row.from)}&size=24&radius=4`,
219
220
  subject: row.subject || undefined, via: row.fromActor?.via, direction: row.direction, inReplyTo: row.inReplyTo } }));
220
- const result = transcript.filter(entry => !entry.mail || !rows.some(row => row.id === entry.mail.id || row.id.startsWith(entry.mail.id)));
221
- // Keep native tool/result ordering intact (some native entries have no time).
222
- for (const entry of mail.sort((a, b) => a.ts - b.ts)) {
223
- const index = result.findIndex(item => Number.isFinite(item.ts) && item.ts > entry.ts);
224
- result.splice(index < 0 ? result.length : index, 0, entry);
221
+ // Some native envelopes use a shortened ID. It identifies a copy only if
222
+ // there is exactly one matching full ID; ambiguity must not erase native mail.
223
+ const fullIds = new Set(rows.map(row => row.id));
224
+ const sortedIds = [...fullIds].sort();
225
+ const inMailbox = id => {
226
+ if (!id) return false;
227
+ if (fullIds.has(id)) return true;
228
+ let low = 0, high = sortedIds.length;
229
+ while (low < high) { const middle = (low + high) >>> 1; if (sortedIds[middle] < id) low = middle + 1; else high = middle; }
230
+ return sortedIds[low]?.startsWith(id) === true && sortedIds[low + 1]?.startsWith(id) !== true;
231
+ };
232
+ mail.sort((a, b) => a.ts - b.ts);
233
+ const result = []; let next = 0;
234
+ // Keep native order, including untimed tools/results. Ascending envelopes
235
+ // can be merged before the first later timed entry without repeated splices
236
+ // or sorting the native transcript itself.
237
+ for (const entry of transcript) {
238
+ if (entry.mail && inMailbox(entry.mail.id)) continue;
239
+ while (next < mail.length && Number.isFinite(entry.ts) && entry.ts > mail[next].ts) result.push(mail[next++]);
240
+ result.push(entry);
225
241
  }
242
+ while (next < mail.length) result.push(mail[next++]);
226
243
  return result;
227
244
  }
228
245