Files
archie/ui/src/dashboard/activity-detail.ts
T

61 lines
2.1 KiB
TypeScript

// Live activity's detail column.
//
// The daemon puts whatever a stage produced into an event's detail, so the
// column holds anything from "session-memory" to a few thousand characters of
// agent transcript. Printed raw, one row would dwarf the table.
//
// The rule is one line in the cell, the whole payload still available. The cut
// happens here rather than in CSS because the row also needs to know whether
// there is more to show.
// Long enough for a useful parked reason, short enough that every row is the
// same height at the widths the table is used at.
const CELL_LIMIT = 160;
/** An activity event, as far as this column reads it. Deliberately narrower
* than the event the dashboard holds: this is the whole of what the cell
* renders, so a caller can pass anything carrying either field. */
export interface ActivityDetailInput {
detail?: unknown;
message?: unknown;
actor_id?: string;
principal_id?: string;
}
/** The cell's content: the rendered line, the payload, and whether they differ. */
export interface ActivityDetail {
text: string;
full: string;
truncated: boolean;
}
/**
* activityDetail returns `{ text, full, truncated }` for one event:
* `text` is the single line the cell renders, `full` the untouched payload,
* and `truncated` whether the two differ.
*/
export function activityDetail(
event: ActivityDetailInput | undefined,
identityName?: (id: string) => string,
): ActivityDetail {
let full = String(event?.detail || event?.message || "");
if (identityName) {
for (const id of [event?.actor_id, event?.principal_id])
if (id) full = full.replaceAll(id, identityName(id));
}
if (!full) return { text: "", full: "", truncated: false };
// The first line that says something: agent output often opens with blanks.
const firstLine =
full
.split("\n")
.map((line) => line.trim())
.find(Boolean) || "";
const clipped =
firstLine.length > CELL_LIMIT
? `${firstLine.slice(0, CELL_LIMIT).trimEnd()}…`
: firstLine;
return { text: clipped, full, truncated: clipped !== full };
}