@hallohuman/hightea-ahp-host 0.0.0-stage → 0.0.2

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.
@@ -0,0 +1,67 @@
1
+ // src/index.ts
2
+ class ActiveTurnTokenVault2 {
3
+ #activeBySession = new Map;
4
+ bindActiveTurn(credential) {
5
+ if (this.#activeBySession.has(credential.sessionId)) {
6
+ throw new Error(`Session ${credential.sessionId} already has an active turn credential; a queued wake cannot overwrite it`);
7
+ }
8
+ this.#activeBySession.set(credential.sessionId, credential);
9
+ }
10
+ renewActiveTurn(context, token, expiresAtMs, nowMs = Date.now()) {
11
+ const entry = this.#activeBySession.get(context.sessionId);
12
+ if (entry === undefined || entry.chatId !== context.chatId || entry.turnId !== context.turnId || entry.deliveryId !== context.deliveryId || entry.houseId !== context.houseId)
13
+ return false;
14
+ if (entry.expiresAtMs <= nowMs) {
15
+ this.#activeBySession.delete(context.sessionId);
16
+ return false;
17
+ }
18
+ if (token === "" || !Number.isFinite(expiresAtMs) || expiresAtMs <= entry.expiresAtMs)
19
+ return false;
20
+ this.#activeBySession.set(context.sessionId, { ...entry, token, expiresAtMs });
21
+ return true;
22
+ }
23
+ resolveForToolCall(context, nowMs = Date.now()) {
24
+ const entry = this.#activeBySession.get(context.sessionId);
25
+ if (entry === undefined) {
26
+ throw new Error(`No active-turn credential for session ${context.sessionId}`);
27
+ }
28
+ if (entry.chatId !== context.chatId || entry.turnId !== context.turnId) {
29
+ throw new Error(`Credential turn mismatch for session ${context.sessionId}: expected ${entry.chatId}/${entry.turnId}, got ${context.chatId}/${context.turnId}`);
30
+ }
31
+ if (nowMs >= entry.expiresAtMs) {
32
+ this.#activeBySession.delete(context.sessionId);
33
+ throw new Error(`Active-turn credential expired for session ${context.sessionId}`);
34
+ }
35
+ return {
36
+ token: entry.token,
37
+ deliveryId: entry.deliveryId,
38
+ threadRef: entry.threadRef,
39
+ ...entry.afterMessageId === undefined ? {} : { afterMessageId: entry.afterMessageId }
40
+ };
41
+ }
42
+ recordThreadRead(context, messageId, nowMs = Date.now()) {
43
+ this.resolveForToolCall(context, nowMs);
44
+ if (messageId === "")
45
+ throw new Error("read thread returned an empty message ID");
46
+ const entry = this.#activeBySession.get(context.sessionId);
47
+ this.#activeBySession.set(context.sessionId, { ...entry, afterMessageId: messageId });
48
+ }
49
+ revokeMember(houseId, agentMemberId) {
50
+ let dropped = 0;
51
+ for (const [sessionId, entry] of this.#activeBySession) {
52
+ if (entry.houseId === houseId && entry.agentMemberId === agentMemberId) {
53
+ this.#activeBySession.delete(sessionId);
54
+ dropped++;
55
+ }
56
+ }
57
+ return dropped;
58
+ }
59
+ clearTurn(sessionId, turnId) {
60
+ const entry = this.#activeBySession.get(sessionId);
61
+ if (entry === undefined || entry.turnId !== turnId)
62
+ return false;
63
+ return this.#activeBySession.delete(sessionId);
64
+ }
65
+ }
66
+
67
+ export { ActiveTurnTokenVault2 };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ ActiveTurnTokenVault2
3
+ } from "./chunk-92tg572q.js";
4
+ export {
5
+ ActiveTurnTokenVault2 as ActiveTurnTokenVault
6
+ };