@volter/teams 0.1.72 → 0.1.74

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.
@@ -46,11 +46,17 @@ export type BrowserTurnAnswer =
46
46
  | { code: 0; turn_id?: string; door?: 'runtime' | 'pane' | 'waiting'; text?: string }
47
47
  | { code: number; text?: string };
48
48
 
49
+ /** The accepted stream ended without a valid receipt; delivery may have occurred. */
50
+ export declare class TurnDeliveryUnconfirmedError extends Error {
51
+ constructor(message: string);
52
+ }
53
+
49
54
  /**
50
55
  * One line typed into the channel's session as its user's own turn; rejects when the machine refuses the stream.
51
56
  * `turnKey`: one per typed line, reused on retry, so the machine types the line once.
52
57
  * Receipt waits time out after thirty seconds. Cancellation stops waiting; it
53
- * does not undo delivery. Reuse the same key when retrying an uncertain receipt.
58
+ * does not undo delivery. After acceptance, a lost, invalid or cancelled receipt
59
+ * rejects with TurnDeliveryUnconfirmedError. Reuse the same key when retrying it.
54
60
  */
55
61
  export function sendBrowserTurn(channel: BrowserChannel, text: string, options?: { turnKey?: string; signal?: AbortSignal }): Promise<BrowserTurnAnswer>;
56
62
 
@@ -167,6 +167,11 @@ export function openBrowserSession({ origin, teamId, sessionId, token }, { WebSo
167
167
  return connectBrowserChannel(socket, channel, `could not reach session ${sessionId} through ${origin}`, 'the server closed the session channel');
168
168
  }
169
169
 
170
+ /** A turn was accepted by its stream, but its delivery receipt was not obtained. */
171
+ export class TurnDeliveryUnconfirmedError extends Error {
172
+ constructor(message) { super(message); this.name = 'TurnDeliveryUnconfirmedError'; }
173
+ }
174
+
170
175
  /**
171
176
  * One line typed into the channel's session as its user's own turn. Resolves with the machine's answer:
172
177
  * `{ code: 0, turn_id, door: 'runtime' | 'pane' | 'waiting' }` (waiting: filed, typed when the session's composer
@@ -189,13 +194,19 @@ export async function sendBrowserTurn(channel, text, { turnKey, signal } = {}) {
189
194
  // receipt as success, or imply that a typed turn was rolled back.
190
195
  stream.close(error.message);
191
196
  };
192
- const cancelled = () => fail(Object.assign(new Error('Turn delivery wait cancelled'), { name: 'AbortError' }));
193
- const timer = setTimeout(() => fail(new Error('Turn delivery acknowledgement timed out')), 30_000);
194
- const data = chunk => { answer += chunk; if (answer.length > 256_000) fail(new Error('Turn delivery receipt too large')); };
195
- const closed = () => {
197
+ const cancelled = () => fail(new TurnDeliveryUnconfirmedError('Turn delivery wait cancelled'));
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')); };
200
+ const closed = reason => {
196
201
  if (settled) return;
197
202
  settled = true; cleanup();
198
- try { resolve(JSON.parse(answer)); } catch { resolve({ code: 1, text: answer || 'no answer from the machine' }); }
203
+ // Only an actual machine answer establishes delivery or refusal. A socket
204
+ // closing, an incomplete JSON frame or an empty receipt cannot prove either.
205
+ try {
206
+ const receipt = JSON.parse(answer);
207
+ if (!receipt || !Number.isInteger(receipt.code)) throw new Error('Invalid receipt');
208
+ resolve(receipt);
209
+ } catch { reject(new TurnDeliveryUnconfirmedError(reason || 'No complete delivery receipt from the machine')); }
199
210
  };
200
211
  stream.on('data', data);
201
212
  if (!settled) stream.on('close', closed);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/teams",
3
- "version": "0.1.72",
3
+ "version": "0.1.74",
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