Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 3x 3x 3x 3x 3x 10x 7x 7x 2x 5x 1x 4x 4x 7x 6x 22x 14x 14x 5x 5x 5x 5x 9x 9x 9x 9x 8x 9x 9x 9x | import type { ScenarioRun } from "@/objects/ScenarioRun";
import { formatUsd } from "@/components/cost/format";
/**
* Reading a scenario file before it is sent (docs/guides/scenario-runs.md),
* and saying where a run is. svc-core validates the file whole and says what
* is wrong with it; this only reads enough to show what is about to run.
*/
export type ReadScenario =
| {
document: unknown;
name: string;
agents: number;
conversations: number;
turns: number;
error: null;
}
| { error: string };
/** A chosen file's text. `FileReader` rather than `Blob.text()`, which
* some browsers (and jsdom) lack. */
export function fileText(file: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(typeof reader.result === "string" ? reader.result : "");
reader.onerror = () => reject(reader.error ?? new Error("unreadable"));
reader.readAsText(file);
});
}
function count(value: unknown): number {
return Array.isArray(value) ? value.length : 0;
}
/** A scenario file's text, read as one, or why it is not one. */
export function readScenario(text: string): ReadScenario {
let document: unknown;
try {
document = JSON.parse(text);
} catch {
return { error: "This file is not JSON." };
}
if (!document || typeof document !== "object" || Array.isArray(document)) {
return { error: "A scenario file is one JSON object." };
}
const file = document as Record<string, unknown>;
const conversations = Array.isArray(file.conversations) ? file.conversations : [];
return {
document,
name: typeof file.name === "string" && file.name ? file.name : "Unnamed scenario",
agents: count(file.agents),
conversations: conversations.length,
turns: conversations.reduce<number>(
(sum, c) => sum + count((c as Record<string, unknown> | null)?.turns),
0,
),
error: null,
};
}
const plural = (n: number, one: string) => `${n} ${one}${n === 1 ? "" : "s"}`;
/** Where a run is, in one line: its progress while it goes, else what it
* did. */
export function runLine(run: ScenarioRun): string {
const total = run.progress?.total ?? run.summary?.turns ?? 0;
if (run.status === "running") {
const done = run.progress?.done ?? 0;
const now = run.progress?.current;
const where = run.cancel_requested
? "stopping"
: now === "provisioning"
? "making its agents"
: now ?? null;
return [`${done} of ${plural(total, "turn")}`, where].filter(Boolean).join(" · ");
}
const summary = run.summary;
Iif (!summary) return plural(total, "turn");
const parts = [plural(summary.turns, "turn")];
if (summary.checks) {
parts.push(`${summary.checks_passed} of ${plural(summary.checks, "check")} passed`);
}
Eif (summary.cost_usd != null) {
parts.push(
`${formatUsd(summary.cost_usd)}${summary.unpriced_turns ? " (at least)" : ""}`,
);
}
return parts.join(" · ");
}
|