fix: harden task timeline model
This commit is contained in:
@ -36,9 +36,9 @@ function isTimelineCardType(value: unknown): value is TaskTimelineCardType {
|
||||
return typeof value === 'string' && TIMELINE_CARD_TYPES.has(value as TaskTimelineCardType);
|
||||
}
|
||||
|
||||
function toTime(value: string): number {
|
||||
function toTime(value: string): number | null {
|
||||
const parsed = new Date(value).getTime();
|
||||
return Number.isFinite(parsed) ? parsed : 0;
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
function firstString(...values: unknown[]): string | undefined {
|
||||
@ -72,8 +72,9 @@ function normalizeSkillNames(metadata: Record<string, unknown> | undefined): str
|
||||
}
|
||||
|
||||
function cardTypeForEvent(event: ProcessEvent): TaskTimelineCardType | null {
|
||||
if (isTimelineCardType(event.metadata?.timeline_type)) {
|
||||
return event.metadata.timeline_type;
|
||||
const timelineType = event.metadata?.timeline_type;
|
||||
if (isTimelineCardType(timelineType)) {
|
||||
return timelineType;
|
||||
}
|
||||
|
||||
if (event.status === 'error') {
|
||||
@ -184,12 +185,37 @@ function buildRunMap(processRuns: ProcessRun[]): Map<string, ProcessRun> {
|
||||
return map;
|
||||
}
|
||||
|
||||
function lastItem<T>(items: T[]): T | null {
|
||||
return items.length > 0 ? items[items.length - 1] : null;
|
||||
}
|
||||
|
||||
function compareCardsByCreatedAt(
|
||||
a: { card: TaskTimelineCard; index: number },
|
||||
b: { card: TaskTimelineCard; index: number },
|
||||
): number {
|
||||
const aTime = toTime(a.card.createdAt);
|
||||
const bTime = toTime(b.card.createdAt);
|
||||
|
||||
if (aTime === null && bTime === null) {
|
||||
return a.index - b.index;
|
||||
}
|
||||
if (aTime === null) {
|
||||
return 1;
|
||||
}
|
||||
if (bTime === null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return aTime - bTime || a.index - b.index;
|
||||
}
|
||||
|
||||
export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): TaskTimelineCard[] {
|
||||
const { task } = input;
|
||||
const processRuns = input.processRuns ?? task.process_runs ?? [];
|
||||
const processEvents = input.processEvents ?? task.process_events ?? [];
|
||||
const processArtifacts = input.processArtifacts ?? task.process_artifacts ?? [];
|
||||
const runsById = buildRunMap(processRuns);
|
||||
const runsWithProgressEvents = new Set<string>();
|
||||
const cards: TaskTimelineCard[] = [
|
||||
{
|
||||
id: `${task.task_id}:created`,
|
||||
@ -207,6 +233,9 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
|
||||
for (const event of processEvents) {
|
||||
const type = cardTypeForEvent(event);
|
||||
if (!type) continue;
|
||||
if (type === 'agent_progress') {
|
||||
runsWithProgressEvents.add(event.run_id);
|
||||
}
|
||||
|
||||
cards.push({
|
||||
id: event.event_id,
|
||||
@ -225,6 +254,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
|
||||
|
||||
for (const run of processRuns) {
|
||||
if (!run.parent_run_id) continue;
|
||||
if (runsWithProgressEvents.has(run.run_id)) continue;
|
||||
|
||||
cards.push({
|
||||
id: `${run.run_id}:fallback-progress`,
|
||||
@ -266,7 +296,7 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
|
||||
cards.push({
|
||||
id: `${task.task_id}:result`,
|
||||
taskId: task.task_id,
|
||||
runId: task.run_ids.at(-1) ?? null,
|
||||
runId: lastItem(task.run_ids),
|
||||
type: 'result',
|
||||
title: titleForCard('result'),
|
||||
summary: resultSummary(task),
|
||||
@ -276,7 +306,8 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
|
||||
});
|
||||
}
|
||||
|
||||
for (const [index, feedback] of task.feedback.entries()) {
|
||||
for (let index = 0; index < task.feedback.length; index += 1) {
|
||||
const feedback = task.feedback[index];
|
||||
cards.push({
|
||||
id: `${task.task_id}:acceptance:${index}`,
|
||||
taskId: task.task_id,
|
||||
@ -292,6 +323,6 @@ export function buildTaskTimelineCards(input: BuildTaskTimelineCardsInput): Task
|
||||
|
||||
return cards
|
||||
.map((card, index) => ({ card, index }))
|
||||
.sort((a, b) => toTime(a.card.createdAt) - toTime(b.card.createdAt) || a.index - b.index)
|
||||
.sort(compareCardsByCreatedAt)
|
||||
.map(({ card }) => card);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user