@pmate/chat-sdk 0.12.0-next.9 → 0.12.0-next.90
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.md +216 -83
- package/lib/advanced.d.ts +10 -0
- package/lib/advanced.js +10 -0
- package/lib/chat.d.ts +5 -0
- package/lib/chat.js +11 -0
- package/lib/chatContract.d.ts +158 -0
- package/lib/chatContract.js +1 -0
- package/lib/chatError.d.ts +31 -0
- package/lib/chatError.js +18 -0
- package/lib/chatTypes.d.ts +128 -0
- package/lib/chatTypes.js +1 -0
- package/lib/commands.d.ts +20 -0
- package/lib/commands.js +12 -0
- package/lib/components/ChatInputDesktop.js +5 -4
- package/lib/components/ChatInputMobile.js +5 -4
- package/lib/components/CommandMenu.d.ts +15 -0
- package/lib/components/CommandMenu.js +10 -0
- package/lib/credentials.d.ts +26 -0
- package/lib/credentials.js +110 -0
- package/lib/fileMetadata.d.ts +9 -0
- package/lib/fileMetadata.js +18 -0
- package/lib/framework.d.ts +11 -0
- package/lib/framework.js +12 -0
- package/lib/history.d.ts +4 -0
- package/lib/history.js +8 -0
- package/lib/hooks/useChatSnapshot.d.ts +4 -0
- package/lib/hooks/useChatSnapshot.js +9 -0
- package/lib/hooks/useComposerSuggestions.d.ts +50 -0
- package/lib/hooks/useComposerSuggestions.js +70 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/internal/chatEngine.d.ts +131 -0
- package/lib/internal/chatEngine.js +306 -0
- package/lib/internal/chatRegistry.d.ts +4 -0
- package/lib/internal/chatRegistry.js +4 -0
- package/lib/internal/createPublicChat.d.ts +4 -0
- package/lib/internal/createPublicChat.js +62 -0
- package/lib/messageView.d.ts +6 -0
- package/lib/messageView.js +103 -0
- package/lib/runtime/batchedReadIndex.d.ts +16 -3
- package/lib/runtime/batchedReadIndex.js +50 -4
- package/lib/runtime/cachedMessage.d.ts +12 -0
- package/lib/runtime/cachedMessage.js +18 -0
- package/lib/runtime/chatClient.d.ts +8 -1
- package/lib/runtime/chatClient.js +18 -6
- package/lib/runtime/chatError.d.ts +5 -0
- package/lib/runtime/chatError.js +20 -0
- package/lib/runtime/chatSession.d.ts +141 -13
- package/lib/runtime/chatSession.js +797 -48
- package/lib/runtime/connectivity.d.ts +4 -0
- package/lib/runtime/connectivity.js +8 -0
- package/lib/runtime/conversationDirectory.d.ts +37 -0
- package/lib/runtime/conversationDirectory.js +116 -0
- package/lib/runtime/conversationRead.d.ts +14 -0
- package/lib/runtime/conversationRead.js +34 -0
- package/lib/runtime/conversationReading.d.ts +30 -0
- package/lib/runtime/conversationReading.js +47 -0
- package/lib/runtime/createChat.d.ts +14 -7
- package/lib/runtime/createChat.js +67 -24
- package/lib/runtime/durableSender.d.ts +97 -14
- package/lib/runtime/durableSender.js +787 -71
- package/lib/runtime/groupClient.d.ts +92 -0
- package/lib/runtime/groupClient.js +380 -0
- package/lib/runtime/groupDenial.d.ts +2 -0
- package/lib/runtime/groupDenial.js +4 -0
- package/lib/runtime/groupIdentity.d.ts +4 -0
- package/lib/runtime/groupIdentity.js +26 -0
- package/lib/runtime/inboxClient.d.ts +65 -0
- package/lib/runtime/inboxClient.js +268 -0
- package/lib/runtime/inboxGroup.d.ts +4 -0
- package/lib/runtime/inboxGroup.js +51 -0
- package/lib/runtime/inboxStore.d.ts +194 -0
- package/lib/runtime/inboxStore.js +321 -0
- package/lib/runtime/inboxTransport.d.ts +6 -0
- package/lib/runtime/inboxTransport.js +76 -0
- package/lib/runtime/index.d.ts +10 -0
- package/lib/runtime/index.js +8 -0
- package/lib/runtime/indexedDbRecovery.d.ts +6 -0
- package/lib/runtime/indexedDbRecovery.js +42 -0
- package/lib/runtime/messageClient.d.ts +37 -8
- package/lib/runtime/messageClient.js +366 -68
- package/lib/runtime/messageControls.d.ts +19 -0
- package/lib/runtime/messageControls.js +44 -0
- package/lib/runtime/messageDiscussionStore.d.ts +63 -0
- package/lib/runtime/messageDiscussionStore.js +147 -0
- package/lib/runtime/messageLaunch.d.ts +44 -0
- package/lib/runtime/messageLaunch.js +137 -0
- package/lib/runtime/messageQuotes.d.ts +3 -0
- package/lib/runtime/messageQuotes.js +17 -0
- package/lib/runtime/messageReaders.d.ts +5 -1
- package/lib/runtime/messageReaders.js +16 -5
- package/lib/runtime/messageStore.d.ts +51 -2
- package/lib/runtime/messageStore.js +264 -14
- package/lib/runtime/messageThread.d.ts +9 -0
- package/lib/runtime/messageThread.js +17 -0
- package/lib/runtime/messageTimeline.d.ts +51 -0
- package/lib/runtime/messageTimeline.js +121 -0
- package/lib/runtime/outboxSchema.d.ts +5 -0
- package/lib/runtime/outboxSchema.js +59 -0
- package/lib/runtime/outgoingContent.d.ts +95 -0
- package/lib/runtime/outgoingContent.js +121 -0
- package/lib/runtime/readBatchJournal.d.ts +35 -0
- package/lib/runtime/readBatchJournal.js +210 -0
- package/lib/runtime/readCounts.d.ts +7 -1
- package/lib/runtime/readCounts.js +108 -21
- package/lib/runtime/readIndex.d.ts +74 -7
- package/lib/runtime/readIndex.js +488 -33
- package/lib/runtime/readSchema.d.ts +2 -0
- package/lib/runtime/readSchema.js +58 -0
- package/lib/runtime/readSync.d.ts +8 -0
- package/lib/runtime/readSync.js +125 -17
- package/lib/runtime/readingClient.d.ts +22 -0
- package/lib/runtime/readingClient.js +141 -20
- package/lib/runtime/readingContent.d.ts +5 -0
- package/lib/runtime/readingContent.js +23 -0
- package/lib/runtime/responseCache.d.ts +41 -5
- package/lib/runtime/responseCache.js +182 -44
- package/lib/runtime/socialReadClient.d.ts +30 -4
- package/lib/runtime/socialReadClient.js +34 -8
- package/lib/runtime/voiceUpload.d.ts +2 -0
- package/lib/runtime/voiceUpload.js +27 -0
- package/lib/snapshot.d.ts +36 -0
- package/lib/snapshot.js +52 -0
- package/lib/types/chatInput.d.ts +3 -0
- package/package.json +32 -2
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
export type { ChatMessage } from "../chatTypes.js";
|
|
2
|
+
import { type CreateChatOptions } from "../runtime/createChat.js";
|
|
3
|
+
import type { ChatSession } from "../runtime/chatSession.js";
|
|
4
|
+
import type { CachedChatMessage } from "../runtime/cachedMessage.js";
|
|
5
|
+
import type { ConversationTarget } from "../runtime/conversationDirectory.js";
|
|
6
|
+
import type { OutgoingContent as RuntimeContent } from "../runtime/outgoingContent.js";
|
|
7
|
+
export { ChatError } from "../chatError.js";
|
|
8
|
+
export type { ChatErrorCode } from "../chatError.js";
|
|
9
|
+
export type { CreateChatOptions, ConversationTarget };
|
|
10
|
+
export type OutgoingContent = Exclude<RuntimeContent, {
|
|
11
|
+
groupCreate: unknown;
|
|
12
|
+
} | {
|
|
13
|
+
groupUpdate: unknown;
|
|
14
|
+
} | {
|
|
15
|
+
groupDissolve: unknown;
|
|
16
|
+
}>;
|
|
17
|
+
import type { ChatGroup, ChatThreadSnapshot, ChatSnapshot, ReadOptions } from "../chatTypes.js";
|
|
18
|
+
export type { ChatGroup, ConversationInfo, ChatThreadSnapshot, ChatSnapshot, ReadOptions } from "../chatTypes.js";
|
|
19
|
+
export type MessageQuery = {
|
|
20
|
+
earlier?: boolean;
|
|
21
|
+
all?: boolean;
|
|
22
|
+
refresh?: boolean;
|
|
23
|
+
};
|
|
24
|
+
export type ReadersQuery = {
|
|
25
|
+
afterProfileId?: string;
|
|
26
|
+
revision?: number;
|
|
27
|
+
};
|
|
28
|
+
export type GroupChanges = {
|
|
29
|
+
expectedRevision?: string;
|
|
30
|
+
addMembers?: readonly string[];
|
|
31
|
+
removeMembers?: readonly string[];
|
|
32
|
+
title?: string;
|
|
33
|
+
avatar?: string;
|
|
34
|
+
};
|
|
35
|
+
export type SendOptions = {
|
|
36
|
+
waitFor?: "accepted" | "delivered";
|
|
37
|
+
};
|
|
38
|
+
export type SentMessage = Readonly<{
|
|
39
|
+
id: string;
|
|
40
|
+
timestamp: number;
|
|
41
|
+
}>;
|
|
42
|
+
type SubscribeOptions<T> = {
|
|
43
|
+
fields?: readonly (keyof T)[];
|
|
44
|
+
emitCurrent?: boolean;
|
|
45
|
+
};
|
|
46
|
+
/** App-facing API. Cache coverage, raw protocol and synchronization stay in advanced. */
|
|
47
|
+
export declare class ChatEngine {
|
|
48
|
+
readonly advanced: ChatSession<CachedChatMessage>;
|
|
49
|
+
private options;
|
|
50
|
+
private groups;
|
|
51
|
+
private group;
|
|
52
|
+
private present;
|
|
53
|
+
private redactedQuotes;
|
|
54
|
+
private messageLists;
|
|
55
|
+
private messages;
|
|
56
|
+
private views;
|
|
57
|
+
private summary?;
|
|
58
|
+
private targets;
|
|
59
|
+
private errors;
|
|
60
|
+
private listeners;
|
|
61
|
+
private closed;
|
|
62
|
+
constructor(advanced: ChatSession<CachedChatMessage>, options: CreateChatOptions);
|
|
63
|
+
private operation;
|
|
64
|
+
private deny;
|
|
65
|
+
private threadOperation;
|
|
66
|
+
start(): Promise<void>;
|
|
67
|
+
/** Resolve a scoped ID locally, registering its target when needed. No remote group is created. */
|
|
68
|
+
resolveThreadId(target: ConversationTarget): Promise<string>;
|
|
69
|
+
/** A trusted recent window. Source selection and synchronization belong to the SDK. */
|
|
70
|
+
getThread(threadId: string, options?: ReadOptions): Promise<ChatThreadSnapshot>;
|
|
71
|
+
loadEarlier(threadId: string, options?: ReadOptions): Promise<ChatThreadSnapshot>;
|
|
72
|
+
refreshThread(threadId: string, options?: ReadOptions): Promise<ChatThreadSnapshot>;
|
|
73
|
+
private readForCaller;
|
|
74
|
+
private threadSubscribers;
|
|
75
|
+
private openQueue;
|
|
76
|
+
private opening;
|
|
77
|
+
private threadRecovery;
|
|
78
|
+
private drainOpens;
|
|
79
|
+
/** Listen first, then open the local window; concurrent consumers share the initial read. */
|
|
80
|
+
subscribeThread(threadId: string, listener: (snapshot: ChatThreadSnapshot) => void, options?: SubscribeOptions<ChatThreadSnapshot>): () => void;
|
|
81
|
+
/** @deprecated Use resolveThreadId. Retained only for the migration window. */
|
|
82
|
+
getConversation(target: ConversationTarget): Promise<string>;
|
|
83
|
+
getSnapshot(): ChatSnapshot;
|
|
84
|
+
getSnapshot(threadId: string): ChatThreadSnapshot;
|
|
85
|
+
getMessages(threadId: string, query?: MessageQuery): Promise<ChatThreadSnapshot>;
|
|
86
|
+
sendMessage(threadId: string, content: OutgoingContent, options?: SendOptions): Promise<SentMessage>;
|
|
87
|
+
retryMessage(messageId: string): Promise<SentMessage>;
|
|
88
|
+
subscribe(listener: (snapshot: ChatSnapshot) => void, options?: SubscribeOptions<ChatSnapshot>): () => void;
|
|
89
|
+
subscribe(threadId: string, listener: (snapshot: ChatThreadSnapshot) => void, options?: SubscribeOptions<ChatThreadSnapshot>): () => void;
|
|
90
|
+
private sessionFields;
|
|
91
|
+
private notify;
|
|
92
|
+
observeMessages(threadId: string, ids: string[], event: "visible" | "played"): Promise<void>;
|
|
93
|
+
markRead(threadId: string): Promise<void>;
|
|
94
|
+
getReaders(threadId: string, messageId: string, page?: ReadersQuery, signal?: AbortSignal): Promise<import("../runtime/messageReaders.js").MessageReadersPage>;
|
|
95
|
+
watchReaders(threadId: string | undefined, ids: string[], receiver?: string): Promise<void>;
|
|
96
|
+
handleReach(event: Parameters<ChatSession<CachedChatMessage>["handleReach"]>[0]): Promise<"handled" | undefined>;
|
|
97
|
+
getVoiceAttachment(threadId: string, messageId: string): Promise<import("../runtime/durableSender.js").VoiceAttachmentLease>;
|
|
98
|
+
getGroup(threadId: string, options?: {
|
|
99
|
+
refresh?: boolean;
|
|
100
|
+
}): Promise<ChatGroup>;
|
|
101
|
+
createGroup(input: {
|
|
102
|
+
groupId?: string;
|
|
103
|
+
title: string;
|
|
104
|
+
members: readonly string[];
|
|
105
|
+
}): Promise<{
|
|
106
|
+
threadId: string;
|
|
107
|
+
groupId: string;
|
|
108
|
+
teamId: string;
|
|
109
|
+
ownerId: string;
|
|
110
|
+
revision: string;
|
|
111
|
+
status: "active";
|
|
112
|
+
title: string;
|
|
113
|
+
avatar?: string;
|
|
114
|
+
memberIds: readonly string[];
|
|
115
|
+
} | {
|
|
116
|
+
threadId: string;
|
|
117
|
+
groupId: string;
|
|
118
|
+
teamId: string;
|
|
119
|
+
ownerId: string;
|
|
120
|
+
revision: string;
|
|
121
|
+
status: "dissolved";
|
|
122
|
+
dissolvedBy?: string;
|
|
123
|
+
dissolvedAt?: number;
|
|
124
|
+
}>;
|
|
125
|
+
updateGroup(threadId: string, changes: GroupChanges): Promise<ChatGroup>;
|
|
126
|
+
dissolveGroup(threadId: string, options?: {
|
|
127
|
+
expectedRevision?: string;
|
|
128
|
+
}): Promise<ChatGroup>;
|
|
129
|
+
revokeAccess(): Promise<void>;
|
|
130
|
+
close(): Promise<void>;
|
|
131
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { createUniqueGroup } from "../runtime/groupIdentity.js";
|
|
2
|
+
import { createMessagePresenter } from "../messageView.js";
|
|
3
|
+
import { createChat as createSession } from "../runtime/createChat.js";
|
|
4
|
+
import { asChatError } from "../runtime/chatError.js";
|
|
5
|
+
import { ChatError } from "../chatError.js";
|
|
6
|
+
export { ChatError } from "../chatError.js";
|
|
7
|
+
const emptyMessages = Object.freeze([]);
|
|
8
|
+
/** App-facing API. Cache coverage, raw protocol and synchronization stay in advanced. */
|
|
9
|
+
export class ChatEngine {
|
|
10
|
+
advanced;
|
|
11
|
+
options;
|
|
12
|
+
groups = new WeakMap();
|
|
13
|
+
group(value) {
|
|
14
|
+
if (!value)
|
|
15
|
+
return undefined;
|
|
16
|
+
let projected = this.groups.get(value);
|
|
17
|
+
if (!projected) {
|
|
18
|
+
const { versionNumber: _, ...visible } = value;
|
|
19
|
+
projected = visible;
|
|
20
|
+
this.groups.set(value, projected);
|
|
21
|
+
}
|
|
22
|
+
return projected;
|
|
23
|
+
}
|
|
24
|
+
present = createMessagePresenter();
|
|
25
|
+
redactedQuotes = new WeakMap();
|
|
26
|
+
messageLists = new WeakMap();
|
|
27
|
+
messages(items) {
|
|
28
|
+
let values = this.messageLists.get(items);
|
|
29
|
+
if (!values) {
|
|
30
|
+
const recalled = new Set(items.filter(item => item.recalled).map(item => item.id));
|
|
31
|
+
values = items.map(item => {
|
|
32
|
+
const value = this.present(item);
|
|
33
|
+
if (!value.quote?.messageId || !recalled.has(value.quote.messageId))
|
|
34
|
+
return value;
|
|
35
|
+
let hidden = this.redactedQuotes.get(value);
|
|
36
|
+
if (!hidden) {
|
|
37
|
+
hidden = { ...value, quote: { ...value.quote, content: "", recalled: true } };
|
|
38
|
+
this.redactedQuotes.set(value, hidden);
|
|
39
|
+
}
|
|
40
|
+
return hidden;
|
|
41
|
+
});
|
|
42
|
+
this.messageLists.set(items, values);
|
|
43
|
+
}
|
|
44
|
+
return values;
|
|
45
|
+
}
|
|
46
|
+
views = new Map();
|
|
47
|
+
summary;
|
|
48
|
+
targets = new Map();
|
|
49
|
+
errors = new Map();
|
|
50
|
+
listeners = new Map();
|
|
51
|
+
closed = false;
|
|
52
|
+
constructor(advanced, options) {
|
|
53
|
+
this.advanced = advanced;
|
|
54
|
+
this.options = options;
|
|
55
|
+
}
|
|
56
|
+
async operation(run) { try {
|
|
57
|
+
return await run();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw asChatError(error);
|
|
61
|
+
} }
|
|
62
|
+
async deny(threadId, failure) {
|
|
63
|
+
this.errors.set(threadId, failure);
|
|
64
|
+
this.targets.delete(threadId);
|
|
65
|
+
const retiring = this.advanced.revokeThread(threadId);
|
|
66
|
+
this.notify(threadId);
|
|
67
|
+
try {
|
|
68
|
+
await retiring;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
failure.cause = new AggregateError([failure.cause, error], "Access withdrawn; cache cleanup incomplete");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
threadOperation(threadId, run) {
|
|
75
|
+
return this.operation(async () => {
|
|
76
|
+
const denied = threadId ? this.errors.get(threadId) : undefined;
|
|
77
|
+
if (denied?.code === "access-denied")
|
|
78
|
+
throw denied;
|
|
79
|
+
try {
|
|
80
|
+
return await run();
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const failure = threadId ? await this.advanced.conversationFailure(error, threadId) : asChatError(error);
|
|
84
|
+
if (threadId && failure.code === "access-denied" && failure.threadId)
|
|
85
|
+
await this.deny(threadId, failure);
|
|
86
|
+
throw failure;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
start() { return this.operation(() => this.options.connection ? this.advanced.initialize() : this.advanced.start()); }
|
|
91
|
+
/** Resolve a scoped ID locally, registering its target when needed. No remote group is created. */
|
|
92
|
+
resolveThreadId(target) { return this.getConversation(target); }
|
|
93
|
+
/** A trusted recent window. Source selection and synchronization belong to the SDK. */
|
|
94
|
+
getThread(threadId, options = {}) { return this.readForCaller(() => this.getMessages(threadId), options); }
|
|
95
|
+
loadEarlier(threadId, options = {}) { return this.readForCaller(() => this.getMessages(threadId, { earlier: true }), options); }
|
|
96
|
+
refreshThread(threadId, options = {}) { return this.readForCaller(() => this.getMessages(threadId, { refresh: true }), options); }
|
|
97
|
+
readForCaller(run, { signal }) {
|
|
98
|
+
if (signal?.aborted)
|
|
99
|
+
return Promise.reject(signal.reason ?? new DOMException("The operation was aborted", "AbortError"));
|
|
100
|
+
if (!signal)
|
|
101
|
+
return run();
|
|
102
|
+
// A caller's cancellation never aborts another subscriber's shared read.
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
const abort = () => { signal.removeEventListener("abort", abort); reject(signal.reason ?? new DOMException("The operation was aborted", "AbortError")); };
|
|
105
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
106
|
+
run().then(value => { signal.removeEventListener("abort", abort); resolve(value); }, error => { signal.removeEventListener("abort", abort); reject(error); });
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
threadSubscribers = new Map();
|
|
110
|
+
openQueue = new Set();
|
|
111
|
+
opening = new Set();
|
|
112
|
+
threadRecovery = new Map();
|
|
113
|
+
drainOpens() {
|
|
114
|
+
while (!this.closed && this.opening.size < 4 && this.openQueue.size) {
|
|
115
|
+
const id = [...this.openQueue].find(id => !this.opening.has(id));
|
|
116
|
+
if (id === undefined)
|
|
117
|
+
break;
|
|
118
|
+
this.openQueue.delete(id);
|
|
119
|
+
if (!this.threadSubscribers.has(id))
|
|
120
|
+
continue;
|
|
121
|
+
this.opening.add(id);
|
|
122
|
+
void this.getThread(id).catch(() => { }).finally(() => { this.opening.delete(id); this.drainOpens(); });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Listen first, then open the local window; concurrent consumers share the initial read. */
|
|
126
|
+
subscribeThread(threadId, listener, options = {}) {
|
|
127
|
+
const off = this.subscribe(threadId, listener, options), count = this.threadSubscribers.get(threadId) ?? 0;
|
|
128
|
+
this.threadSubscribers.set(threadId, count + 1);
|
|
129
|
+
if (!count) {
|
|
130
|
+
// Track coverage independently of consumer field filters. A canonical history
|
|
131
|
+
// reset also invalidates open discussion windows sharing that source epoch.
|
|
132
|
+
let complete = this.advanced.getSnapshot(threadId).complete;
|
|
133
|
+
this.threadRecovery.set(threadId, this.advanced.subscribeChanges(threadId, () => {
|
|
134
|
+
const next = this.advanced.getSnapshot(threadId).complete, lost = complete && !next;
|
|
135
|
+
complete = next;
|
|
136
|
+
if (lost)
|
|
137
|
+
queueMicrotask(() => {
|
|
138
|
+
if (this.closed || !this.advanced.active || !this.threadSubscribers.has(threadId) || this.getSnapshot(threadId).status !== "pending")
|
|
139
|
+
return;
|
|
140
|
+
this.openQueue.add(threadId);
|
|
141
|
+
this.drainOpens();
|
|
142
|
+
});
|
|
143
|
+
}, ["complete"]));
|
|
144
|
+
this.openQueue.add(threadId);
|
|
145
|
+
this.drainOpens();
|
|
146
|
+
}
|
|
147
|
+
let disposed = false;
|
|
148
|
+
return () => {
|
|
149
|
+
if (disposed)
|
|
150
|
+
return;
|
|
151
|
+
disposed = true;
|
|
152
|
+
off();
|
|
153
|
+
const remaining = (this.threadSubscribers.get(threadId) ?? 1) - 1;
|
|
154
|
+
if (remaining)
|
|
155
|
+
this.threadSubscribers.set(threadId, remaining);
|
|
156
|
+
else {
|
|
157
|
+
this.threadSubscribers.delete(threadId);
|
|
158
|
+
this.openQueue.delete(threadId);
|
|
159
|
+
this.threadRecovery.get(threadId)?.();
|
|
160
|
+
this.threadRecovery.delete(threadId);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** @deprecated Use resolveThreadId. Retained only for the migration window. */
|
|
165
|
+
getConversation(target) {
|
|
166
|
+
return this.operation(async () => {
|
|
167
|
+
const id = await this.advanced.getConversation(target);
|
|
168
|
+
const value = target.groupId !== undefined ? { kind: "group", targetId: target.groupId } : { kind: "dm", targetId: target.peerId };
|
|
169
|
+
this.targets.delete(id);
|
|
170
|
+
this.targets.set(id, value);
|
|
171
|
+
if (this.targets.size > 256)
|
|
172
|
+
this.targets.delete(this.targets.keys().next().value);
|
|
173
|
+
this.notify(id);
|
|
174
|
+
return id;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
getSnapshot(threadId) {
|
|
178
|
+
if (threadId === undefined) {
|
|
179
|
+
const source = this.advanced.getSnapshot(), previous = this.summary;
|
|
180
|
+
const threads = source.threads.filter(thread => this.errors.get(thread.threadId)?.code !== "access-denied").map(thread => ({ threadId: thread.threadId, lastMessage: this.messages(this.advanced.getSnapshot(thread.threadId).timeline).at(-1), group: this.group(thread.group), unread: thread.unread, outgoingCount: thread.outgoingCount }));
|
|
181
|
+
const unreadMessageCount = source.unreadMessageCount === undefined ? undefined : threads.reduce((sum, thread) => sum + (thread.unread?.unreadMessageCount ?? 0), 0);
|
|
182
|
+
const badgeCount = source.badgeCount === undefined ? undefined : threads.reduce((sum, thread) => sum + (thread.unread?.badgeCount ?? 0), 0);
|
|
183
|
+
if (previous && previous.connection === source.connection && previous.badgeCount === badgeCount && previous.unreadMessageCount === unreadMessageCount && previous.threads.length === threads.length && threads.every((thread, i) => { const old = previous.threads[i]; return thread.threadId === old.threadId && thread.lastMessage === old.lastMessage && thread.group === old.group && thread.unread === old.unread && thread.outgoingCount === old.outgoingCount; }))
|
|
184
|
+
return previous;
|
|
185
|
+
return this.summary = { threads, connection: source.connection, badgeCount, unreadMessageCount };
|
|
186
|
+
}
|
|
187
|
+
const source = this.advanced.getSnapshot(threadId), previous = this.views.get(threadId);
|
|
188
|
+
const error = this.closed ? undefined : this.errors.get(threadId), denied = error?.code === "access-denied";
|
|
189
|
+
const target = this.closed || !this.advanced.active || denied ? undefined : this.advanced.getConversationTarget(threadId) ?? this.targets.get(threadId);
|
|
190
|
+
const conversation = previous?.conversation?.kind === target?.kind && previous?.conversation?.targetId === target?.targetId ? previous?.conversation : target ? { kind: target.kind, targetId: target.targetId } : undefined;
|
|
191
|
+
// Partial migrated rows never masquerade as a trusted recent window.
|
|
192
|
+
const next = { threadId, conversation, messages: !denied && source.complete ? this.messages(source.timeline) : emptyMessages, ready: !denied && source.complete, hasEarlier: !denied && source.hasEarlier, unread: denied ? undefined : source.unread, group: denied ? undefined : this.group(source.group), readCounts: denied ? undefined : source.readCounts, error, status: !denied && source.complete ? "ready" : error ? "error" : "pending" };
|
|
193
|
+
if (previous && Object.keys(next).every(key => next[key] === previous[key]))
|
|
194
|
+
return previous;
|
|
195
|
+
this.views.set(threadId, next);
|
|
196
|
+
return next;
|
|
197
|
+
}
|
|
198
|
+
async getMessages(threadId, query = {}) {
|
|
199
|
+
try {
|
|
200
|
+
const denied = this.errors.get(threadId);
|
|
201
|
+
if (denied?.code === "access-denied")
|
|
202
|
+
throw denied;
|
|
203
|
+
if (Object.keys(query).some(key => !["earlier", "all", "refresh"].includes(key)))
|
|
204
|
+
throw new ChatError("invalid-input", "Unsupported message query");
|
|
205
|
+
const page = await this.advanced.getMessages(threadId, query);
|
|
206
|
+
if (!page.complete)
|
|
207
|
+
throw new ChatError("unavailable", "Messages are not ready; retry when connected");
|
|
208
|
+
this.errors.delete(threadId);
|
|
209
|
+
this.notify(threadId);
|
|
210
|
+
return this.getSnapshot(threadId);
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
const failure = await this.advanced.conversationFailure(error, threadId);
|
|
214
|
+
if (!this.closed) {
|
|
215
|
+
if (failure.code === "access-denied" && failure.threadId)
|
|
216
|
+
await this.deny(threadId, failure);
|
|
217
|
+
else {
|
|
218
|
+
this.errors.set(threadId, failure);
|
|
219
|
+
this.notify(threadId);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
throw failure;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
sendMessage(threadId, content, options = {}) {
|
|
226
|
+
return this.threadOperation(threadId, async () => {
|
|
227
|
+
if (content && ("groupCreate" in content || "groupUpdate" in content || "groupDissolve" in content))
|
|
228
|
+
throw new ChatError("invalid-input", "Use group lifecycle methods for group changes");
|
|
229
|
+
const result = await this.advanced.sendMessage(threadId, content, options);
|
|
230
|
+
return { id: result.hash, timestamp: result.t };
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
retryMessage(messageId) { return this.operation(async () => { const result = await this.advanced.retryMessage(messageId); return { id: result.hash, timestamp: result.t }; }); }
|
|
234
|
+
subscribe(target, callback, options = {}) {
|
|
235
|
+
const global = typeof target === "function", key = global ? "" : target;
|
|
236
|
+
const config = global ? callback : options;
|
|
237
|
+
const read = () => global ? this.getSnapshot() : this.getSnapshot(key);
|
|
238
|
+
let previous = read();
|
|
239
|
+
const emit = () => {
|
|
240
|
+
const value = read();
|
|
241
|
+
const fields = config?.fields;
|
|
242
|
+
const changed = fields ? fields.some(field => previous[field] !== value[field]) : value !== previous;
|
|
243
|
+
previous = value;
|
|
244
|
+
if (changed) {
|
|
245
|
+
try {
|
|
246
|
+
(global ? target : callback)(value);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
try {
|
|
250
|
+
this.options.onError(error);
|
|
251
|
+
}
|
|
252
|
+
catch { /* A consumer cannot break other consumers or cache publication. */ }
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
let listeners = this.listeners.get(key);
|
|
257
|
+
if (!listeners) {
|
|
258
|
+
listeners = new Set();
|
|
259
|
+
this.listeners.set(key, listeners);
|
|
260
|
+
}
|
|
261
|
+
listeners.add(emit);
|
|
262
|
+
const off = global ? this.advanced.subscribeChanges(emit) : this.advanced.subscribeChanges(key, emit, this.sessionFields(options.fields));
|
|
263
|
+
const dispose = () => { off(); listeners.delete(emit); if (!listeners.size)
|
|
264
|
+
this.listeners.delete(key); };
|
|
265
|
+
if (config?.emitCurrent !== false) {
|
|
266
|
+
try {
|
|
267
|
+
(global ? target : callback)(previous);
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
dispose();
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return dispose;
|
|
275
|
+
}
|
|
276
|
+
sessionFields(fields) {
|
|
277
|
+
return fields ? [...new Set(fields.flatMap(field => field === "messages" ? ["timeline", "complete"] : field === "ready" || field === "status" ? ["complete"] : field === "conversation" ? ["messages", "group"] : field === "error" || field === "threadId" ? [] : [field]))] : undefined;
|
|
278
|
+
}
|
|
279
|
+
notify(threadId) { this.listeners.get(threadId)?.forEach(listener => listener()); this.listeners.get("")?.forEach(listener => listener()); }
|
|
280
|
+
observeMessages(threadId, ids, event) { return this.threadOperation(threadId, async () => { await this.advanced.observeMessages(threadId, ids, event); }); }
|
|
281
|
+
markRead(threadId) { return this.threadOperation(threadId, async () => { await this.advanced.markRead(threadId); }); }
|
|
282
|
+
getReaders(threadId, messageId, page = {}, signal) {
|
|
283
|
+
return this.threadOperation(threadId, async () => {
|
|
284
|
+
if (Object.keys(page).some(key => !["afterProfileId", "revision"].includes(key)))
|
|
285
|
+
throw new ChatError("invalid-input", "Unsupported reader query");
|
|
286
|
+
return this.advanced.getReaders(threadId, messageId, page, signal);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
watchReaders(threadId, ids, receiver) { return this.threadOperation(threadId, () => this.advanced.observeMessages(threadId, ids, "receipts", receiver)); }
|
|
290
|
+
handleReach(event) { return this.operation(() => this.advanced.handleReach(event)); }
|
|
291
|
+
getVoiceAttachment(threadId, messageId) { return this.threadOperation(threadId, () => this.advanced.getVoiceAttachment(threadId, messageId)); }
|
|
292
|
+
getGroup(threadId, options = {}) { return this.threadOperation(threadId, async () => this.group(await this.advanced.getGroup(threadId, options))); }
|
|
293
|
+
createGroup(input) {
|
|
294
|
+
return this.operation(async () => {
|
|
295
|
+
return createUniqueGroup(input.groupId, async (groupId) => {
|
|
296
|
+
const threadId = await this.getConversation({ groupId });
|
|
297
|
+
const group = await this.advanced.createGroup({ id: threadId, kind: "group", targetId: groupId, teamId: this.options.teamId }, { title: input.title, members: input.members });
|
|
298
|
+
return { ...this.group(group), threadId };
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
updateGroup(threadId, changes) { return this.threadOperation(threadId, async () => this.group(await this.advanced.updateGroup(threadId, changes))); }
|
|
303
|
+
dissolveGroup(threadId, options = {}) { return this.threadOperation(threadId, async () => this.group(await this.advanced.dissolveGroup(threadId, options))); }
|
|
304
|
+
revokeAccess() { return this.operation(() => this.advanced.revokeAccess()); }
|
|
305
|
+
close() { this.closed = true; this.threadRecovery.forEach(off => off()); this.threadRecovery.clear(); return this.operation(() => this.advanced.close()).finally(() => { this.errors.clear(); this.targets.clear(); this.listeners.clear(); this.threadSubscribers.clear(); this.openQueue.clear(); this.views.clear(); this.summary = undefined; this.present = createMessagePresenter(); this.messageLists = new WeakMap(); this.groups = new WeakMap(); this.redactedQuotes = new WeakMap(); }); }
|
|
306
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Chat } from "../chatContract.js";
|
|
2
|
+
import { type CreateChatOptions } from "../runtime/createChat.js";
|
|
3
|
+
/** Build only the public capabilities; no aliases or engine fields on the returned object. */
|
|
4
|
+
export declare function createPublicChat(options: CreateChatOptions, onClosed?: () => void): Chat;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { validMessageThreadRoot } from "../runtime/messageThread.js";
|
|
2
|
+
import { waitForCaller } from "../credentials.js";
|
|
3
|
+
import { ChatEngine } from "./chatEngine.js";
|
|
4
|
+
import { registerChat } from "./chatRegistry.js";
|
|
5
|
+
import { createChat as createSession } from "../runtime/createChat.js";
|
|
6
|
+
import { asChatError } from "../runtime/chatError.js";
|
|
7
|
+
import { ChatError } from "../chatError.js";
|
|
8
|
+
/** Build only the public capabilities; no aliases or engine fields on the returned object. */
|
|
9
|
+
export function createPublicChat(options, onClosed) {
|
|
10
|
+
const configured = { ...options, onError: (error) => options.onError?.(asChatError(error)) };
|
|
11
|
+
let session;
|
|
12
|
+
try {
|
|
13
|
+
session = createSession(configured);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
throw asChatError(error);
|
|
17
|
+
}
|
|
18
|
+
const engine = new ChatEngine(session, configured);
|
|
19
|
+
const keys = (value, allowed) => { if (Object.keys(value).some(key => !allowed.includes(key)))
|
|
20
|
+
throw new ChatError("invalid-input", "Unsupported operation options"); };
|
|
21
|
+
const read = (run, options = {}, cleanup) => {
|
|
22
|
+
try {
|
|
23
|
+
keys(options, ["signal"]);
|
|
24
|
+
options.signal?.throwIfAborted();
|
|
25
|
+
return waitForCaller(run(), options.signal, cleanup);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return Promise.reject(error);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const discussions = new Set();
|
|
32
|
+
const chat = Object.freeze({
|
|
33
|
+
openMessageThread: (threadId, threadRootId) => {
|
|
34
|
+
if (configured.threadRootId !== undefined || !validMessageThreadRoot(threadRootId))
|
|
35
|
+
throw new ChatError("invalid-input", "Nested Message Threads are not supported");
|
|
36
|
+
const child = createPublicChat({ ...configured, threadId, threadRootId, connection: session.sharedTransport() }, () => discussions.delete(child));
|
|
37
|
+
discussions.add(child);
|
|
38
|
+
return child;
|
|
39
|
+
},
|
|
40
|
+
getMessageThreads: (id, roots, options = {}) => read(() => session.getMessageThreads(id, roots, options.signal), options),
|
|
41
|
+
start: () => engine.start(), close: async () => { await Promise.all([...discussions].map(child => child.close())); discussions.clear(); await engine.close(); onClosed?.(); },
|
|
42
|
+
resolveThreadId: (target, options = {}) => read(() => engine.resolveThreadId(target), options),
|
|
43
|
+
getThread: (id, options) => read(() => engine.getThread(id), options), loadEarlier: (id, options) => read(() => engine.loadEarlier(id), options), refreshThread: (id, options) => read(() => engine.refreshThread(id), options),
|
|
44
|
+
subscribeThread: (id, listener, options = {}) => { keys(options, ["fields"]); return engine.subscribeThread(id, listener, options); }, subscribeChat: (listener, options = {}) => { keys(options, ["fields"]); return engine.subscribe(listener, options); },
|
|
45
|
+
sendMessage: (id, content, options) => engine.sendMessage(id, content, options), retryMessage: id => engine.retryMessage(id),
|
|
46
|
+
observeMessages: (id, ids, event) => engine.observeMessages(id, ids, event), markRead: id => engine.markRead(id), watchReaders: (id, ids, receiver) => engine.watchReaders(id, ids, receiver),
|
|
47
|
+
getReaders: (id, messageId, options = {}) => {
|
|
48
|
+
try {
|
|
49
|
+
keys(options, ["afterProfileId", "revision", "signal"]);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
}
|
|
54
|
+
return read(() => engine.getReaders(id, messageId, { afterProfileId: options.afterProfileId, revision: options.revision }), { signal: options.signal });
|
|
55
|
+
},
|
|
56
|
+
getVoiceAttachment: (id, messageId, options = {}) => read(() => engine.getVoiceAttachment(id, messageId), options, lease => lease.release()),
|
|
57
|
+
getGroup: (id, options = {}) => read(() => engine.getGroup(id), options), refreshGroup: (id, options = {}) => read(() => engine.getGroup(id, { refresh: true }), options),
|
|
58
|
+
createGroup: input => engine.createGroup(input), updateGroup: (id, changes) => engine.updateGroup(id, changes), dissolveGroup: (id, options) => engine.dissolveGroup(id, options),
|
|
59
|
+
});
|
|
60
|
+
registerChat(chat, engine);
|
|
61
|
+
return chat;
|
|
62
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TimelineItem } from "./runtime/messageTimeline.js";
|
|
2
|
+
import type { CachedChatMessage } from "./runtime/cachedMessage.js";
|
|
3
|
+
import type { ChatMessage } from "./chatTypes.js";
|
|
4
|
+
export type { ChatMessage } from "./chatTypes.js";
|
|
5
|
+
/** Decode a body once; reading and delivery projections reuse its normalized content. */
|
|
6
|
+
export declare function createMessagePresenter(): (item: TimelineItem<CachedChatMessage>) => ChatMessage;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { parseChatFile } from "./fileMetadata.js";
|
|
2
|
+
const object = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
3
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
4
|
+
function media(value) {
|
|
5
|
+
if (typeof value !== "string" || !value)
|
|
6
|
+
return undefined;
|
|
7
|
+
try {
|
|
8
|
+
const url = new URL(value, "https://chat.invalid");
|
|
9
|
+
return ["https:", "http:"].includes(url.protocol) && !url.username && !url.password ? value : undefined;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function quote(value) {
|
|
16
|
+
const record = object(value);
|
|
17
|
+
if (typeof record.senderName !== "string" || typeof record.content !== "string" || record.content.length > 6000)
|
|
18
|
+
return;
|
|
19
|
+
return { senderName: record.senderName.slice(0, 100), content: record.content, ...(typeof record.messageId === "string" ? { messageId: record.messageId } : {}) };
|
|
20
|
+
}
|
|
21
|
+
function legacyDuration(value) {
|
|
22
|
+
if (typeof value !== "string" || !/^\d+:\d{2}$/.test(value))
|
|
23
|
+
return 0;
|
|
24
|
+
const [minutes, seconds] = value.split(":").map(Number);
|
|
25
|
+
return (minutes * 60 + seconds) * 1000;
|
|
26
|
+
}
|
|
27
|
+
function content(message) {
|
|
28
|
+
const body = object(message.body), outer = object(body.content);
|
|
29
|
+
const envelope = body.kind === "quote" && outer.content !== undefined ? outer : body;
|
|
30
|
+
const payload = object(envelope.content);
|
|
31
|
+
const legacy = object(message.legacyDisplay);
|
|
32
|
+
const validLegacy = legacy.id === message.id && legacy.threadId === message.threadId && legacy.orgId === message.teamId && legacy.from === message.from && typeof legacy.content === "string";
|
|
33
|
+
const hint = validLegacy ? legacy : {};
|
|
34
|
+
const file = parseChatFile(payload.file === undefined ? hint.file : payload.file);
|
|
35
|
+
const contentKind = envelope.contentKind, voice = object(payload.voice);
|
|
36
|
+
const voiceHint = hint.kind === "voice";
|
|
37
|
+
const voiceUrl = media(voice.url) ?? (voiceHint ? media(hint.audioUrl) : undefined) ?? "";
|
|
38
|
+
const durationMs = typeof voice.durationMs === "number" && Number.isFinite(voice.durationMs) ? voice.durationMs : voiceHint ? legacyDuration(hint.duration) : 0;
|
|
39
|
+
const hasVoice = voiceHint || Object.keys(voice).length > 0 && (voiceUrl.length > 0 || durationMs > 0) || contentKind === "voice";
|
|
40
|
+
const imageUrl = media(payload.url) ?? (hint.kind === "image" ? media(hint.imageUrl) : undefined);
|
|
41
|
+
const messageQuote = quote(payload.quote) ?? quote(hint.quote);
|
|
42
|
+
const rawText = text(payload.text) || text(object(body.payload).message);
|
|
43
|
+
const result = { kind: "text", text: validLegacy ? text(hint.content) : rawText, senderName: text(hint.senderName) || undefined, senderAvatar: media(hint.senderAvatar), timeLabel: text(hint.createdAt) || undefined,
|
|
44
|
+
recipientCount: message.threadKind === "dm" ? 1 : validLegacy ? typeof hint.recipientCount === "number" ? hint.recipientCount : undefined : Array.isArray(message.recipients) ? new Set(message.recipients.filter(id => id !== message.from)).size : undefined };
|
|
45
|
+
if (validLegacy && ["emoji", "system", "attachment", "quote"].includes(String(hint.kind)))
|
|
46
|
+
result.kind = hint.kind;
|
|
47
|
+
if (body.kind === "contact") {
|
|
48
|
+
result.kind = ["friendRequest", "requestFriend"].includes(String(body.action)) ? "invitation" : "system";
|
|
49
|
+
if (result.kind === "system")
|
|
50
|
+
result.system = { type: "contact-updated" };
|
|
51
|
+
}
|
|
52
|
+
else if (body.kind === "group") {
|
|
53
|
+
result.kind = "system";
|
|
54
|
+
result.system = { type: body.action === "groupCreate" ? "group-created" : body.action === "groupDissolve" ? "group-dissolved" : "group-updated", title: text(object(body.payload).title) || undefined };
|
|
55
|
+
}
|
|
56
|
+
else if (file) {
|
|
57
|
+
result.kind = "attachment";
|
|
58
|
+
result.file = file;
|
|
59
|
+
}
|
|
60
|
+
else if (hasVoice) {
|
|
61
|
+
result.kind = "voice";
|
|
62
|
+
result.voice = { url: voiceUrl, durationMs, ...(text(voice.transcript) || text(hint.transcript) ? { transcript: text(voice.transcript) || text(hint.transcript) } : {}) };
|
|
63
|
+
}
|
|
64
|
+
else if ((contentKind === "image" || hint.kind === "image") && imageUrl) {
|
|
65
|
+
result.kind = "image";
|
|
66
|
+
result.image = { url: imageUrl, name: text(payload.name) || text(hint.imageAlt) || undefined };
|
|
67
|
+
}
|
|
68
|
+
else if (contentKind === "emoji") {
|
|
69
|
+
result.kind = "emoji";
|
|
70
|
+
result.text = text(payload.code) || result.text;
|
|
71
|
+
}
|
|
72
|
+
if (messageQuote) {
|
|
73
|
+
result.quote = messageQuote;
|
|
74
|
+
if (result.kind === "text")
|
|
75
|
+
result.kind = "quote";
|
|
76
|
+
}
|
|
77
|
+
if (result.kind === "attachment")
|
|
78
|
+
result.attachment = { name: text(hint.attachmentName) || undefined, size: text(hint.attachmentSize) || undefined };
|
|
79
|
+
if (payload.format !== undefined)
|
|
80
|
+
result.richContent = payload;
|
|
81
|
+
else if (hint.richContent !== undefined)
|
|
82
|
+
result.richContent = hint.richContent;
|
|
83
|
+
if (Array.isArray(payload.mentions))
|
|
84
|
+
result.mentions = payload.mentions;
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
/** Decode a body once; reading and delivery projections reuse its normalized content. */
|
|
88
|
+
export function createMessagePresenter() {
|
|
89
|
+
const bodies = new WeakMap(), items = new WeakMap();
|
|
90
|
+
return (item) => {
|
|
91
|
+
const previous = items.get(item);
|
|
92
|
+
if (previous)
|
|
93
|
+
return previous;
|
|
94
|
+
let value = { kind: "system", text: "" };
|
|
95
|
+
if (!item.recalled && item.message) {
|
|
96
|
+
value = bodies.get(item.message) ?? content(item.message);
|
|
97
|
+
bodies.set(item.message, value);
|
|
98
|
+
}
|
|
99
|
+
const result = { id: item.id, threadId: item.threadId, ...(item.threadRootId === undefined ? {} : { threadRootId: item.threadRootId }), timestamp: item.timestamp, from: item.from, mine: item.mine, status: item.status, recalled: item.recalled, unread: item.unread, attentionEligible: item.attentionEligible, reading: item.reading, reactions: item.reactions, ...value };
|
|
100
|
+
items.set(item, result);
|
|
101
|
+
return result;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { type ReadIndexPersistence, type ReadIndexState } from "./readIndex.js";
|
|
1
|
+
import { type ReadIndexPersistence, type ReadIndexState, type ReadIndexTransform, type ReadObservationScope } from "./readIndex.js";
|
|
2
2
|
type Change = {
|
|
3
|
-
|
|
3
|
+
weight: number;
|
|
4
|
+
observation?: ReadObservationScope;
|
|
5
|
+
transform: ReadIndexTransform;
|
|
4
6
|
resolve: (state: ReadIndexState) => void;
|
|
5
7
|
reject: (error: unknown) => void;
|
|
6
8
|
};
|
|
@@ -13,9 +15,20 @@ export declare class BatchedReadIndex implements ReadIndexPersistence {
|
|
|
13
15
|
private queueLimit;
|
|
14
16
|
private queues;
|
|
15
17
|
private closed;
|
|
18
|
+
private sourceCounts;
|
|
16
19
|
constructor(storage: ReadIndexPersistence, windowMs?: number, batchSize?: number, queueLimit?: number);
|
|
17
20
|
load(appId: string, profileId: string): Promise<ReadIndexState | undefined>;
|
|
18
|
-
|
|
21
|
+
loadVersioned(appId: string, profileId: string): Promise<{
|
|
22
|
+
state: ReadIndexState | undefined;
|
|
23
|
+
versions: import("./readIndex.js").ReadIndexVersions;
|
|
24
|
+
} | {
|
|
25
|
+
state: ReadIndexState | undefined;
|
|
26
|
+
versions: {};
|
|
27
|
+
}>;
|
|
28
|
+
loadVersions(appId: string, profileId: string): Promise<{}>;
|
|
29
|
+
invalidateTeam(appId: string, profileId: string, teamId: string, version: number, knownThreadIds?: readonly string[]): Promise<ReadIndexState>;
|
|
30
|
+
private weight;
|
|
31
|
+
update(appId: string, profileId: string, transform: Change["transform"], observation?: ReadObservationScope): Promise<ReadIndexState>;
|
|
19
32
|
private drain;
|
|
20
33
|
flush(appId: string, profileId: string): Promise<void>;
|
|
21
34
|
close(): Promise<void>;
|