e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
type ComposerStateLike = {
|
|
isEmpty: boolean;
|
|
text: string;
|
|
attachments: readonly unknown[];
|
|
};
|
|
|
|
type RunStartsByThread = Map<string, number[]>;
|
|
|
|
export function getComposerMessageMetrics(
|
|
composerState: ComposerStateLike,
|
|
): { messageLength: number; attachmentsCount: number } | undefined {
|
|
if (composerState.isEmpty) return undefined;
|
|
|
|
return {
|
|
messageLength: composerState.text.length,
|
|
attachmentsCount: composerState.attachments.length,
|
|
};
|
|
}
|
|
|
|
export const queueMicrotaskSafe =
|
|
typeof queueMicrotask === "function"
|
|
? queueMicrotask
|
|
: (callback: () => void) => Promise.resolve().then(callback);
|
|
|
|
export function recordRunStartedAt(
|
|
runStartsByThread: RunStartsByThread,
|
|
threadId: string,
|
|
startedAt: number,
|
|
): void {
|
|
const existing = runStartsByThread.get(threadId);
|
|
if (existing) {
|
|
existing.push(startedAt);
|
|
return;
|
|
}
|
|
|
|
runStartsByThread.set(threadId, [startedAt]);
|
|
}
|
|
|
|
export function consumeRunStartedAt(
|
|
runStartsByThread: RunStartsByThread,
|
|
threadId: string,
|
|
): number | undefined {
|
|
const existing = runStartsByThread.get(threadId);
|
|
if (existing === undefined || existing.length === 0) return undefined;
|
|
|
|
const startedAt = existing.shift();
|
|
if (existing.length === 0) {
|
|
runStartsByThread.delete(threadId);
|
|
}
|
|
|
|
return startedAt;
|
|
}
|
|
|
|
export function pruneStaleRunStarts(
|
|
runStartsByThread: RunStartsByThread,
|
|
now: number,
|
|
staleThresholdMs: number,
|
|
): void {
|
|
for (const [threadId, startedAtQueue] of runStartsByThread.entries()) {
|
|
const freshStarts = startedAtQueue.filter(
|
|
(startedAt) => now - startedAt <= staleThresholdMs,
|
|
);
|
|
|
|
if (freshStarts.length === 0) {
|
|
runStartsByThread.delete(threadId);
|
|
continue;
|
|
}
|
|
|
|
if (freshStarts.length !== startedAtQueue.length) {
|
|
runStartsByThread.set(threadId, freshStarts);
|
|
}
|
|
}
|
|
}
|