Intermediate commit
This commit is contained in:
parent
0e9e49df16
commit
3d4cfeff1a
65 changed files with 5507 additions and 333 deletions
|
|
@ -40,6 +40,8 @@ let eventSource: EventSource | null = null;
|
|||
let activeStreamRunId: number | null = null;
|
||||
const ACTIVE_STATUSES = new Set(["running", "resolving"]);
|
||||
|
||||
type StreamDisplayIdentifier = PublicationItem["display_identifier"];
|
||||
|
||||
function parseRunId(value: unknown): number | null {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return value;
|
||||
|
|
@ -86,6 +88,46 @@ function parsePublicationCount(value: unknown, fallback: number): number {
|
|||
return fallback;
|
||||
}
|
||||
|
||||
function parseDisplayIdentifier(value: unknown): StreamDisplayIdentifier {
|
||||
if (!value || typeof value !== "object") {
|
||||
return null;
|
||||
}
|
||||
const payload = value as Record<string, unknown>;
|
||||
if (typeof payload.kind !== "string" || typeof payload.value !== "string" || typeof payload.label !== "string") {
|
||||
return null;
|
||||
}
|
||||
if (typeof payload.confidence_score !== "number" || !Number.isFinite(payload.confidence_score)) {
|
||||
return null;
|
||||
}
|
||||
const url = typeof payload.url === "string" ? payload.url : null;
|
||||
return {
|
||||
kind: payload.kind,
|
||||
value: payload.value,
|
||||
label: payload.label,
|
||||
url,
|
||||
confidence_score: payload.confidence_score,
|
||||
};
|
||||
}
|
||||
|
||||
function withUpdatedDisplayIdentifier(
|
||||
items: PublicationItem[],
|
||||
update: {
|
||||
publicationId: number;
|
||||
displayIdentifier: StreamDisplayIdentifier;
|
||||
},
|
||||
): PublicationItem[] {
|
||||
const { publicationId, displayIdentifier } = update;
|
||||
let changed = false;
|
||||
const next = items.map((item) => {
|
||||
if (item.publication_id !== publicationId) {
|
||||
return item;
|
||||
}
|
||||
changed = true;
|
||||
return { ...item, display_identifier: displayIdentifier };
|
||||
});
|
||||
return changed ? next : items;
|
||||
}
|
||||
|
||||
function reconcileRunCounters(previous: RunListItem | null, next: RunListItem | null): RunListItem | null {
|
||||
if (previous === null || next === null) {
|
||||
return next;
|
||||
|
|
@ -200,7 +242,7 @@ export const useRunStatusStore = defineStore("runStatus", {
|
|||
this.updateEventSource();
|
||||
},
|
||||
updateEventSource(): void {
|
||||
const targetRunId = this.latestRun?.status === "running" ? this.latestRun.id : null;
|
||||
const targetRunId = isActiveStatus(this.latestRun?.status) ? this.latestRun?.id ?? null : null;
|
||||
if (activeStreamRunId === targetRunId) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -251,6 +293,25 @@ export const useRunStatusStore = defineStore("runStatus", {
|
|||
console.error("Failed to parse SSE event", err);
|
||||
}
|
||||
});
|
||||
eventSource.addEventListener("identifier_updated", (e) => {
|
||||
try {
|
||||
const data = JSON.parse(e.data);
|
||||
const publicationId = parseRunId(data?.publication_id);
|
||||
const displayIdentifier = parseDisplayIdentifier(data?.display_identifier);
|
||||
if (publicationId === null || displayIdentifier === null) {
|
||||
return;
|
||||
}
|
||||
this.livePublications = withUpdatedDisplayIdentifier(
|
||||
this.livePublications,
|
||||
{
|
||||
publicationId,
|
||||
displayIdentifier,
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("Failed to parse SSE event", err);
|
||||
}
|
||||
});
|
||||
eventSource.onerror = () => {
|
||||
// Reconnecting is handled automatically by EventSource,
|
||||
// but if it's permanently closed, we could do something here.
|
||||
|
|
@ -429,6 +490,7 @@ export const useRunStatusStore = defineStore("runStatus", {
|
|||
this.lastErrorRequestId = null;
|
||||
this.lastSyncAt = null;
|
||||
this.safetyState = createDefaultSafetyState();
|
||||
this.livePublications = [];
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue