@niwa-labs/workflow 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** The plan of a run as the kit's plan component takes it, from what `workflow.inspect` answers. */
|
|
2
|
+
|
|
3
|
+
import type { PhasePlanPhase, PhasePlanStep, WorkState } from "@niwa-labs/ui-kit";
|
|
4
|
+
|
|
5
|
+
import type { Inspection, Step } from "./actions.ts";
|
|
6
|
+
import type { PlanTarget } from "@niwa-labs/ui-kit";
|
|
7
|
+
|
|
8
|
+
const STEP_STATE: Readonly<Record<Step["status"], WorkState>> = {
|
|
9
|
+
running: "running",
|
|
10
|
+
paused: "waiting",
|
|
11
|
+
completed: "done",
|
|
12
|
+
failed: "failed",
|
|
13
|
+
unknown: "unknown",
|
|
14
|
+
skipped: "skipped",
|
|
15
|
+
cancelled: "stopped",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function stepState(step: Step): WorkState {
|
|
19
|
+
// Reusing an unconfirmed outcome leaves its effect uncertain.
|
|
20
|
+
return step.reused === true && step.status !== "unknown" ? "reused" : STEP_STATE[step.status];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** How long a step took, in milliseconds; undefined while it has not ended. */
|
|
24
|
+
export function stepDuration(step: Step): number | undefined {
|
|
25
|
+
if (step.endedAt === undefined) return undefined;
|
|
26
|
+
const took = Date.parse(step.endedAt) - Date.parse(step.startedAt);
|
|
27
|
+
return Number.isFinite(took) && took >= 0 ? took : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The state of a declared phase: a current phase takes on how the run ended or why it waits. */
|
|
31
|
+
export function phaseState(phase: Inspection["phases"][number], status: Inspection["status"]): WorkState {
|
|
32
|
+
if (phase.state === "done") return "done";
|
|
33
|
+
if (phase.state === "ahead") return "pending";
|
|
34
|
+
if (status.state === "paused") return "waiting";
|
|
35
|
+
if (status.outcome?.status === "failed") return "failed";
|
|
36
|
+
if (status.outcome?.status === "cancelled") return "stopped";
|
|
37
|
+
return status.state === "finished" ? "done" : "running";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The state of the run's own phase, for a script that declares none. */
|
|
41
|
+
function runState(status: Inspection["status"]): WorkState {
|
|
42
|
+
if (status.state === "paused") return "waiting";
|
|
43
|
+
if (status.state === "running") return "running";
|
|
44
|
+
return status.outcome?.status === "failed" ? "failed" : status.outcome?.status === "cancelled" ? "stopped" : "done";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const STEPS_PHASE = "Steps";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The phases of the plan. Steps that named no phase are the last phase, "Steps"; a script that declares no phases has that one
|
|
51
|
+
* phase alone, so the run still has a row to hold its controls.
|
|
52
|
+
*/
|
|
53
|
+
export function planOf(inspection: Inspection, toStep: (step: Step) => PhasePlanStep): PhasePlanPhase[] {
|
|
54
|
+
const phases = inspection.phases.map(
|
|
55
|
+
(phase, index): PhasePlanPhase => ({
|
|
56
|
+
id: `phase-${String(index)}`,
|
|
57
|
+
title: phase.title,
|
|
58
|
+
state: phaseState(phase, inspection.status),
|
|
59
|
+
steps: phase.steps.map(toStep),
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
if (inspection.steps.length > 0 || phases.length === 0) {
|
|
63
|
+
phases.push({
|
|
64
|
+
id: "steps",
|
|
65
|
+
title: phases.length === 0 ? "Run" : STEPS_PHASE,
|
|
66
|
+
state: phases.length === 0 ? runState(inspection.status) : inspection.steps.some((step) => step.status === "running") ? "running" : "done",
|
|
67
|
+
steps: inspection.steps.map(toStep),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return phases;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The phases of a run as `planOf` names them, each with the steps of its row and the title a resume names it by. */
|
|
74
|
+
function rowsOf(inspection: Inspection): Array<{ id: string; title: string | undefined; steps: readonly Step[] }> {
|
|
75
|
+
return [
|
|
76
|
+
...inspection.phases.map((phase, index) => ({ id: `phase-${String(index)}`, title: phase.title, steps: phase.steps })),
|
|
77
|
+
{ id: "steps", title: undefined, steps: inspection.steps },
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What recomputing a target of the plan asks the host: a step runs again from its key, a declared phase from its title, and
|
|
83
|
+
* the row of the steps that named no phase from the top. The steps before it are taken from the run as it was.
|
|
84
|
+
*/
|
|
85
|
+
export function recomputeOf(inspection: Inspection, target: PlanTarget): { run: string; from?: string } {
|
|
86
|
+
const row = rowsOf(inspection).find((one) => one.id === target.phase);
|
|
87
|
+
if (target.step !== undefined) {
|
|
88
|
+
const step = row?.steps.find((one) => one.id === target.step);
|
|
89
|
+
if (step !== undefined) return { run: inspection.run, from: step.key };
|
|
90
|
+
}
|
|
91
|
+
return row?.title === undefined ? { run: inspection.run } : { run: inspection.run, from: row.title };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What stopping a target of the plan asks the host: a step stops by its key and the run goes on; a phase stops the run. */
|
|
95
|
+
export function stopOf(inspection: Inspection, target: PlanTarget): { run: string; at?: string } {
|
|
96
|
+
if (target.step === undefined) return { run: inspection.run };
|
|
97
|
+
const step = rowsOf(inspection)
|
|
98
|
+
.find((one) => one.id === target.phase)
|
|
99
|
+
?.steps.find((one) => one.id === target.step);
|
|
100
|
+
return step === undefined ? { run: inspection.run } : { run: inspection.run, at: step.key };
|
|
101
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { DataValue } from "@niwa-labs/ui-kit";
|
|
2
|
+
import { PageLink, useAction, useCall, useViewer } from "@niwa-labs/cockpit-sdk";
|
|
3
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { cancelAction, inspectAction, type RunEntry } from "./actions.ts";
|
|
6
|
+
import { catalogue, catalogueEvents, workflowPath } from "./catalogue-actions.ts";
|
|
7
|
+
|
|
8
|
+
type Props = { run: string; state: RunEntry["state"]; workflow?: string | undefined; compact?: boolean };
|
|
9
|
+
export function RunCommands(props: Props) {
|
|
10
|
+
const [expanded, setExpanded] = useState(false);
|
|
11
|
+
if (props.compact && !expanded)
|
|
12
|
+
return (
|
|
13
|
+
<button className="desk-disclose" onClick={() => setExpanded(true)}>
|
|
14
|
+
Управление запуском
|
|
15
|
+
</button>
|
|
16
|
+
);
|
|
17
|
+
return <Commands {...props} />;
|
|
18
|
+
}
|
|
19
|
+
function Commands({ run, state, workflow }: Props) {
|
|
20
|
+
const viewer = useViewer();
|
|
21
|
+
const client = useQueryClient();
|
|
22
|
+
const inspect = useCall(inspectAction, { run }, { enabled: viewer.can(inspectAction), refreshOn: catalogueEvents });
|
|
23
|
+
const status = inspect.data?.status;
|
|
24
|
+
const manual = status?.manualPause;
|
|
25
|
+
const pause = useAction(catalogue.pause);
|
|
26
|
+
const resume = useAction(catalogue.resume);
|
|
27
|
+
const onward = useAction(catalogue.continue);
|
|
28
|
+
const cancel = useAction(cancelAction);
|
|
29
|
+
const revise = useAction(catalogue.revise);
|
|
30
|
+
const pending = pause.isPending || resume.isPending || onward.isPending || cancel.isPending || revise.isPending;
|
|
31
|
+
const error = pause.error ?? resume.error ?? onward.error ?? cancel.error ?? revise.error ?? inspect.error;
|
|
32
|
+
const refresh = () => void client.invalidateQueries({ queryKey: ["niwa", "call"] });
|
|
33
|
+
const finished = (status?.state ?? state) === "finished";
|
|
34
|
+
const stopped = status?.state === "paused";
|
|
35
|
+
const originId = inspect.data?.origin.workflow ?? workflow;
|
|
36
|
+
const launch = new URLSearchParams({ run, ...(originId ? { id: originId } : {}) });
|
|
37
|
+
return (
|
|
38
|
+
<div className="desk-controls">
|
|
39
|
+
<div className="desk-tools">
|
|
40
|
+
{originId && <PageLink to={workflowPath(originId)}>Сценарий</PageLink>}
|
|
41
|
+
{finished && viewer.can(catalogue.start) && (
|
|
42
|
+
<PageLink to={"/workflows/launch?" + launch}>↻ Запустить снова</PageLink>
|
|
43
|
+
)}
|
|
44
|
+
{!finished && status && !manual && !stopped && viewer.can(catalogue.pause) && (
|
|
45
|
+
<button disabled={pending} onClick={() => pause.mutate({ run }, { onSettled: refresh })}>
|
|
46
|
+
Пауза
|
|
47
|
+
</button>
|
|
48
|
+
)}
|
|
49
|
+
{manual && viewer.can(catalogue.continue) && (
|
|
50
|
+
<button disabled={pending} onClick={() => onward.mutate({ run, pause: manual.id }, { onSettled: refresh })}>
|
|
51
|
+
{manual.state === "requested" ? "Снять запрос паузы" : "Продолжить"}
|
|
52
|
+
</button>
|
|
53
|
+
)}
|
|
54
|
+
{stopped &&
|
|
55
|
+
!manual &&
|
|
56
|
+
viewer.can(catalogue.resume) &&
|
|
57
|
+
(["retry", "skip"] as const)
|
|
58
|
+
.filter((c) => status.choices?.includes(c))
|
|
59
|
+
.map((choice) => (
|
|
60
|
+
<button
|
|
61
|
+
key={choice}
|
|
62
|
+
disabled={pending}
|
|
63
|
+
onClick={() => resume.mutate({ run, choice }, { onSettled: refresh })}
|
|
64
|
+
>
|
|
65
|
+
{choice === "retry" ? "Повторить шаг" : "Пропустить шаг"}
|
|
66
|
+
</button>
|
|
67
|
+
))}
|
|
68
|
+
{!finished && viewer.can(cancelAction) && (
|
|
69
|
+
<button
|
|
70
|
+
disabled={pending}
|
|
71
|
+
onClick={() => {
|
|
72
|
+
if (window.confirm("Остановить запуск " + run + "?")) cancel.mutate({ run }, { onSettled: refresh });
|
|
73
|
+
}}
|
|
74
|
+
>
|
|
75
|
+
Остановить запуск
|
|
76
|
+
</button>
|
|
77
|
+
)}
|
|
78
|
+
</div>
|
|
79
|
+
{(stopped || status?.outcome?.status === "failed") && viewer.can(catalogue.revise) && (
|
|
80
|
+
<details>
|
|
81
|
+
<summary>Пересмотреть сценарий</summary>
|
|
82
|
+
<form
|
|
83
|
+
className="desk-form"
|
|
84
|
+
onSubmit={(e) => {
|
|
85
|
+
e.preventDefault();
|
|
86
|
+
const file = String(new FormData(e.currentTarget).get("file")).trim();
|
|
87
|
+
revise.mutate({ run, file }, { onSettled: refresh });
|
|
88
|
+
}}
|
|
89
|
+
>
|
|
90
|
+
<label>
|
|
91
|
+
Исправленный файл
|
|
92
|
+
<input name="file" required defaultValue={inspect.data?.origin.file} />
|
|
93
|
+
</label>
|
|
94
|
+
<button disabled={pending}>Применить ревизию</button>
|
|
95
|
+
</form>
|
|
96
|
+
</details>
|
|
97
|
+
)}
|
|
98
|
+
{revise.data && <DataValue value={revise.data.revision}/>}
|
|
99
|
+
{error && <p role="alert">{error.message}</p>}
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { CopyValue, DataValue, FileRef } from "@niwa-labs/ui-kit";
|
|
2
|
+
/** One run: where it stands, and its plan of phases and steps with the controls to run it again or stop it. */
|
|
3
|
+
|
|
4
|
+
import { EntityLink, PageLink, useAction, useCall, useViewer, type PageProps } from "@niwa-labs/cockpit-sdk";
|
|
5
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
6
|
+
import { PhasePlan, StateRing, formatAgo, formatDuration, type PhasePlanStep, type PlanTarget } from "@niwa-labs/ui-kit";
|
|
7
|
+
|
|
8
|
+
import { inspectAction, resumeAction, stopAction, type Inspection, type Step } from "./actions.ts";
|
|
9
|
+
import { planOf, recomputeOf, stepDuration, stepState, stopOf } from "./plan.ts";
|
|
10
|
+
import { InspectedTrack } from "./track.tsx";
|
|
11
|
+
import { catalogueEvents } from "./catalogue-actions.ts";
|
|
12
|
+
import { RunCommands } from "./run-commands.tsx";
|
|
13
|
+
import { ringOf } from "./runs.tsx";
|
|
14
|
+
|
|
15
|
+
function toStep(step: Step): PhasePlanStep {
|
|
16
|
+
const took = stepDuration(step);
|
|
17
|
+
return {
|
|
18
|
+
id: step.id,
|
|
19
|
+
title: step.label === "" ? step.action : step.label,
|
|
20
|
+
state: stepState(step),
|
|
21
|
+
...(step.effect === true ? { effect: true } : {}),
|
|
22
|
+
...(took === undefined && step.reused !== true
|
|
23
|
+
? {}
|
|
24
|
+
: { meta: [step.reused === true ? "из предыдущего запуска" : undefined, took === undefined ? undefined : formatDuration(took)].filter(Boolean).join(" · ") }),
|
|
25
|
+
...(step.session === undefined && step.prompt === undefined
|
|
26
|
+
? {}
|
|
27
|
+
: {
|
|
28
|
+
detail: (
|
|
29
|
+
<span className="flex min-w-0 items-center gap-2">
|
|
30
|
+
{step.session === undefined ? null : <EntityLink entity={{ kind: "session", id: step.session }} short />}
|
|
31
|
+
{step.prompt === undefined ? null : <span className="min-w-0 truncate">{step.prompt}</span>}
|
|
32
|
+
</span>
|
|
33
|
+
),
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function RunSessions({ sessions }: { sessions: Inspection["sessions"] }) {
|
|
39
|
+
if (sessions.length === 0) return null;
|
|
40
|
+
const ids = new Map(sessions.map(({ id, ref }) => [ref, id]));
|
|
41
|
+
return (
|
|
42
|
+
<section aria-label="Сессии запуска" className="min-w-0 rounded-lg border bg-card text-card-foreground">
|
|
43
|
+
<h3 className="border-b px-3 py-2 text-sm font-semibold">Сессии запуска</h3>
|
|
44
|
+
<ul className="divide-y px-3" data-slot="run-sessions">
|
|
45
|
+
{sessions.map(({ id, ref, parentSession, startedAt }) => {
|
|
46
|
+
const parent = parentSession === undefined ? undefined : ids.get(parentSession);
|
|
47
|
+
return (
|
|
48
|
+
<li key={id} className="flex min-w-0 flex-wrap items-center gap-2 py-2">
|
|
49
|
+
<EntityLink entity={{ kind: "session", id }} />
|
|
50
|
+
<CopyValue value={ref}/>
|
|
51
|
+
{parentSession === undefined ? null : (
|
|
52
|
+
<span className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
|
|
53
|
+
<span className="shrink-0">Запустил</span>
|
|
54
|
+
{parent === undefined ? (
|
|
55
|
+
<span className="truncate" title={parentSession}>{parentSession}</span>
|
|
56
|
+
) : (
|
|
57
|
+
<EntityLink entity={{ kind: "session", id: parent }} />
|
|
58
|
+
)}
|
|
59
|
+
</span>
|
|
60
|
+
)}
|
|
61
|
+
<time className="ml-auto shrink-0 text-xs text-muted-foreground tabular-nums" dateTime={startedAt}>
|
|
62
|
+
{formatAgo(startedAt)}
|
|
63
|
+
</time>
|
|
64
|
+
</li>
|
|
65
|
+
);
|
|
66
|
+
})}
|
|
67
|
+
</ul>
|
|
68
|
+
</section>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function RunPage({ params }: PageProps<"/runs/$runId">) {
|
|
73
|
+
const run = params.runId;
|
|
74
|
+
const viewer = useViewer();
|
|
75
|
+
const queryClient = useQueryClient();
|
|
76
|
+
const inspection = useCall(inspectAction, { run }, { refreshOn: catalogueEvents });
|
|
77
|
+
const resume = useAction(resumeAction);
|
|
78
|
+
const stop = useAction(stopAction);
|
|
79
|
+
|
|
80
|
+
if (inspection.isPending) return <p className="p-4 text-sm text-muted-foreground">Загружаю запуск…</p>;
|
|
81
|
+
if (inspection.isError) {
|
|
82
|
+
return (
|
|
83
|
+
<div className="flex flex-col gap-2 p-4">
|
|
84
|
+
<p role="alert" className="text-sm text-destructive">Не удалось открыть запуск · {inspection.error.message}</p>
|
|
85
|
+
<PageLink to="/runs" className="text-sm underline-offset-2 hover:underline">Все запуски</PageLink>
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const { status, origin } = inspection.data;
|
|
91
|
+
const phases = planOf(inspection.data, toStep);
|
|
92
|
+
const current = phases.find((phase) => phase.state === "running" || phase.state === "waiting")?.id;
|
|
93
|
+
const reread = (): void => void queryClient.invalidateQueries({ queryKey: ["niwa", "call"] });
|
|
94
|
+
|
|
95
|
+
// A step or a phase runs again from where it is, the steps before it taken from the run as it was; a step stops on its
|
|
96
|
+
// own and the run goes on, a phase row stops the whole run.
|
|
97
|
+
const recompute = (target: PlanTarget): void => resume.mutate(recomputeOf(inspection.data, target), { onSettled: reread });
|
|
98
|
+
const halt = (target: PlanTarget): void => stop.mutate(stopOf(inspection.data, target), { onSettled: reread });
|
|
99
|
+
const failure = resume.error ?? stop.error;
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<article data-slot="run-page" className="flex min-w-0 flex-col gap-4 p-4">
|
|
103
|
+
<header className="flex min-w-0 flex-col gap-1">
|
|
104
|
+
<div className="flex min-w-0 items-center gap-2">
|
|
105
|
+
<StateRing state={ringOf(status.state)} label={status.state} />
|
|
106
|
+
<h2 className="min-w-0 text-lg font-semibold">{origin.label ?? origin.file.split("/").at(-1) ?? run}</h2>
|
|
107
|
+
<EntityLink entity={{ kind: "run", id: run }} />
|
|
108
|
+
</div>
|
|
109
|
+
<p className="min-w-0 truncate text-xs text-muted-foreground" data-slot="run-directory">
|
|
110
|
+
{origin.directory ? <FileRef path={origin.directory}/> : "Без рабочего каталога"}
|
|
111
|
+
</p>
|
|
112
|
+
</header>
|
|
113
|
+
<RunCommands run={run} state={status.state} workflow={origin.workflow} />
|
|
114
|
+
<InspectedTrack key={run} inspection={inspection.data} detail />
|
|
115
|
+
{status.outcome?.status === "done" && status.outcome.result !== undefined && <details open><summary>Результат</summary><DataValue value={status.outcome.result}/></details>}
|
|
116
|
+
{viewer.owner && origin.sourceSeq && <a className="text-sm underline" target="_blank" rel="noreferrer" href={"/api/source?seq=" + origin.sourceSeq}>Открыть исходник этого запуска ↗</a>}
|
|
117
|
+
<details className="nw-plan-controls"><summary>Управление фазами и шагами</summary>
|
|
118
|
+
<PhasePlan
|
|
119
|
+
phases={phases}
|
|
120
|
+
onRecompute={viewer.can(resumeAction) ? recompute : undefined}
|
|
121
|
+
onStop={viewer.can(stopAction) ? halt : undefined}
|
|
122
|
+
canRecompute={() => status.state !== "running"}
|
|
123
|
+
canStop={(target: PlanTarget) => status.state !== "finished" && (target.step !== undefined || target.phase === current)}
|
|
124
|
+
explainRecompute={(target: PlanTarget) =>
|
|
125
|
+
recomputeOf(inspection.data, target).from === undefined
|
|
126
|
+
? "Restart the run from the top. The workflow reuses recorded results where it can. Calls that run again may repeat a message or another external change."
|
|
127
|
+
: undefined
|
|
128
|
+
}
|
|
129
|
+
explainStop={(target: PlanTarget) =>
|
|
130
|
+
target.step === undefined
|
|
131
|
+
? "Ends the whole run and interrupts the sessions it started."
|
|
132
|
+
: "Stops this step: it ends as cancelled, its sessions are interrupted, and the rest of the run goes on."
|
|
133
|
+
}
|
|
134
|
+
/>
|
|
135
|
+
</details>
|
|
136
|
+
<RunSessions sessions={inspection.data.sessions} />
|
|
137
|
+
{failure === null ? null : <p role="alert" className="text-sm text-destructive">{failure.message}</p>}
|
|
138
|
+
</article>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RunEntry } from "./actions.ts";
|
|
2
|
+
|
|
3
|
+
/** The CLI's "about" text can contain truncated serialised output. Full results belong to the run detail. */
|
|
4
|
+
export function runSummary(entry: RunEntry): string {
|
|
5
|
+
if (entry.state === "finished") {
|
|
6
|
+
if (entry.about.startsWith("failed: ")) return "Ошибка · " + entry.about.slice(8);
|
|
7
|
+
if (entry.about === "cancelled") return "Остановлен";
|
|
8
|
+
return "Выполнен";
|
|
9
|
+
}
|
|
10
|
+
if (entry.requests) return "Ждёт ответа";
|
|
11
|
+
if (entry.manualPause) return "Приостановлен владельцем";
|
|
12
|
+
if (entry.state === "paused") return entry.about || "На паузе";
|
|
13
|
+
if (entry.waiting?.startsWith("sleep ")) return "Ждёт таймер";
|
|
14
|
+
if (entry.waiting?.startsWith("wait ")) return "Ждёт события";
|
|
15
|
+
return entry.phases?.find(phase => phase.state === "current")?.title ?? "Выполняется";
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** The runs of the host, in blocks by the directory they were started for. */
|
|
2
|
+
|
|
3
|
+
import { useCall, EntityLink, PageLink } from "@niwa-labs/cockpit-sdk";
|
|
4
|
+
import { Badge, MiniPhaseTrack, StateRing, cn, formatAgo, type WorkState } from "@niwa-labs/ui-kit";
|
|
5
|
+
|
|
6
|
+
import { runsAction, type RunEntry } from "./actions.ts";
|
|
7
|
+
import { groupByDirectory, type RunGroup } from "./groups.ts";
|
|
8
|
+
|
|
9
|
+
/** What a run's state is called in a ring: a paused run waits for a decision. */
|
|
10
|
+
export function ringOf(state: RunEntry["state"]): WorkState {
|
|
11
|
+
return state === "paused" ? "waiting" : state === "running" ? "running" : "done";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function RunRow({ entry }: { entry: RunEntry }) {
|
|
15
|
+
return (
|
|
16
|
+
<li data-slot="run-row" data-state={entry.state} className="flex min-w-0 flex-col gap-1 py-2">
|
|
17
|
+
<div className="flex min-w-0 items-center gap-2">
|
|
18
|
+
<StateRing state={ringOf(entry.state)} />
|
|
19
|
+
<PageLink to={`/runs/${encodeURIComponent(entry.run)}`} className="min-w-0 wrap-anywhere text-sm font-semibold hover:underline focus-visible:underline">
|
|
20
|
+
{entry.name}
|
|
21
|
+
</PageLink>
|
|
22
|
+
</div>
|
|
23
|
+
{!!entry.phases?.length && <MiniPhaseTrack phases={entry.phases}/>}
|
|
24
|
+
<div className="flex min-w-0 flex-wrap items-center gap-2 pl-4 text-xs text-muted-foreground">
|
|
25
|
+
<EntityLink entity={{ kind: "run", id: entry.run }} short />
|
|
26
|
+
{entry.requests > 0 && <Badge variant="secondary">Ожидают ответа: {entry.requests}</Badge>}
|
|
27
|
+
<time className="tabular-nums" dateTime={entry.requestedAt}>
|
|
28
|
+
{formatAgo(entry.requestedAt)}
|
|
29
|
+
</time>
|
|
30
|
+
</div>
|
|
31
|
+
</li>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One block: the directory, and the runs started for it. */
|
|
36
|
+
export function RunBlock({ group }: { group: RunGroup }) {
|
|
37
|
+
return (
|
|
38
|
+
<section data-slot="run-block" data-directory={group.directory ?? ""} className="min-w-0 rounded-lg border bg-card text-card-foreground">
|
|
39
|
+
<header className="flex min-w-0 flex-col gap-1 border-b px-3 py-2">
|
|
40
|
+
<div className="flex min-w-0 items-center gap-2">
|
|
41
|
+
<h3 className="min-w-0 wrap-anywhere text-sm font-semibold">{group.directory === undefined ? "Без проекта" : group.name}</h3>
|
|
42
|
+
<span className="ml-auto shrink-0 text-xs text-muted-foreground tabular-nums">{group.runs.length}</span>
|
|
43
|
+
</div>
|
|
44
|
+
{group.directory !== undefined && (
|
|
45
|
+
<span className="min-w-0 truncate text-xs text-muted-foreground" title={group.directory}>
|
|
46
|
+
{group.directory}
|
|
47
|
+
</span>
|
|
48
|
+
)}
|
|
49
|
+
</header>
|
|
50
|
+
<ul className="m-0 list-none divide-y px-3">
|
|
51
|
+
{group.runs.map((entry) => (
|
|
52
|
+
<RunRow key={entry.run} entry={entry} />
|
|
53
|
+
))}
|
|
54
|
+
</ul>
|
|
55
|
+
</section>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The runs grouped by directory. Reads again when a record of a workflow run arrives. */
|
|
60
|
+
export function Runs({ limit = 200, className }: { limit?: number; className?: string }) {
|
|
61
|
+
const runs = useCall(runsAction, { limit }, { refreshOn: { types: ["workflow.run.*", "workflow.phase.*"] } });
|
|
62
|
+
if (runs.isPending) return <p className="text-sm text-muted-foreground">Loading runs…</p>;
|
|
63
|
+
if (runs.isError) return <p role="alert" className="text-sm text-destructive">The runs could not be read: {runs.error.message}</p>;
|
|
64
|
+
const groups = groupByDirectory(runs.data.entries);
|
|
65
|
+
if (groups.length === 0) return <p className="text-sm text-muted-foreground">No runs yet.</p>;
|
|
66
|
+
return (
|
|
67
|
+
<div data-slot="runs" className={cn("flex min-w-0 flex-col gap-3", className)}>
|
|
68
|
+
{groups.map((group) => (
|
|
69
|
+
<RunBlock key={group.directory ?? ""} group={group} />
|
|
70
|
+
))}
|
|
71
|
+
</div>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { FileRef, CopyValue } from "@niwa-labs/ui-kit";
|
|
2
|
+
import { useCall } from "@niwa-labs/cockpit-sdk";
|
|
3
|
+
import { formatDuration, PageSection } from "@niwa-labs/ui-kit";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import type { Measures } from "../stats.ts";
|
|
6
|
+
import { catalogue, catalogueEvents } from "./catalogue-actions.ts";
|
|
7
|
+
|
|
8
|
+
const percent = (rate: number | null) => (rate === null ? "—" : Math.round(rate * 100) + "%");
|
|
9
|
+
const duration = (ms?: number) => (ms === undefined ? "—" : formatDuration(ms));
|
|
10
|
+
const metrics: [string, (m: Measures) => string | number][] = [
|
|
11
|
+
["Запуски", (m) => m.runs],
|
|
12
|
+
["Выполнены", (m) => m.outcomes.done],
|
|
13
|
+
["Ошибки", (m) => m.outcomes.failed],
|
|
14
|
+
["Остановлены", (m) => m.outcomes.cancelled],
|
|
15
|
+
["На паузе", (m) => m.outcomes.paused],
|
|
16
|
+
["В работе", (m) => m.outcomes.running],
|
|
17
|
+
["Без участия человека", (m) => percent(m.cleanRate)],
|
|
18
|
+
["Медиана", (m) => duration(m.duration?.p50)],
|
|
19
|
+
["90% запусков до", (m) => duration(m.duration?.p90)],
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export function Statistics({ id, versions = [] }: { id?: string; versions?: string[] }) {
|
|
23
|
+
const [a, setA] = useState("");
|
|
24
|
+
const [b, setB] = useState("");
|
|
25
|
+
const [compare, setCompare] = useState<[string, string]>();
|
|
26
|
+
const result = useCall(catalogue.stats, id ? { id } : {}, { refreshOn: catalogueEvents });
|
|
27
|
+
const compared = useCall(
|
|
28
|
+
catalogue.stats,
|
|
29
|
+
{ id: id ?? "", ...(compare ? { compare } : {}) },
|
|
30
|
+
{ enabled: !!compare, refreshOn: catalogueEvents },
|
|
31
|
+
);
|
|
32
|
+
const stats = result.data;
|
|
33
|
+
if (!stats) return <p role="status">{result.error?.message ?? "Загружаю статистику…"}</p>;
|
|
34
|
+
const m = stats.total;
|
|
35
|
+
const times = [
|
|
36
|
+
{ label: "Агенты", value: m.time.agentMs, color: "var(--state-running)" },
|
|
37
|
+
{ label: "Ответы человека", value: m.time.humanMs, color: "var(--state-waiting)" },
|
|
38
|
+
{ label: "Таймеры", value: m.time.timerMs, color: "var(--state-done)" },
|
|
39
|
+
{ label: "Прочее", value: m.time.otherMs, color: "var(--muted-foreground)" },
|
|
40
|
+
];
|
|
41
|
+
return (
|
|
42
|
+
<PageSection title="Как идут сценарии">
|
|
43
|
+
<dl className="desk-metrics">
|
|
44
|
+
{metrics.map(([label, read]) => (
|
|
45
|
+
<div key={label}>
|
|
46
|
+
<dt>{label}</dt>
|
|
47
|
+
<dd>{read(m)}</dd>
|
|
48
|
+
</div>
|
|
49
|
+
))}
|
|
50
|
+
</dl>
|
|
51
|
+
{m.runs > 0 && (
|
|
52
|
+
<div>
|
|
53
|
+
<div className="desk-time-bar" aria-hidden>
|
|
54
|
+
{times
|
|
55
|
+
.filter((t) => t.value > 0)
|
|
56
|
+
.map((t) => (
|
|
57
|
+
<i key={t.label} style={{ flexGrow: t.value, background: t.color }} />
|
|
58
|
+
))}
|
|
59
|
+
</div>
|
|
60
|
+
<ul className="desk-time-key">
|
|
61
|
+
{times.map((t) => (
|
|
62
|
+
<li key={t.label}>
|
|
63
|
+
<i style={{ background: t.color }} />
|
|
64
|
+
{t.label} {duration(t.value)}
|
|
65
|
+
</li>
|
|
66
|
+
))}
|
|
67
|
+
</ul>
|
|
68
|
+
{!!m.failures.length && <h3>Где ломаются</h3>}
|
|
69
|
+
{m.failures.map((f) => (
|
|
70
|
+
<p key={f.phase ?? ""}>
|
|
71
|
+
{f.phase ?? "Вне фаз"} · ошибок {f.count}
|
|
72
|
+
</p>
|
|
73
|
+
))}
|
|
74
|
+
{!!m.agentCalls.length && (
|
|
75
|
+
<section className="nw-stat-group">
|
|
76
|
+
<h3>Работа агентов</h3>
|
|
77
|
+
{m.agentCalls.map((c) => (
|
|
78
|
+
<div className="nw-stat-row" key={c.harness + "/" + c.model}>
|
|
79
|
+
<div>
|
|
80
|
+
<strong>{c.harness}</strong>{" "}
|
|
81
|
+
{c.model && (
|
|
82
|
+
<CopyValue value={c.model} label={c.model.replace(/^m:[^:]*:/, "").split(":v:")[0] ?? c.model} />
|
|
83
|
+
)}
|
|
84
|
+
</div>
|
|
85
|
+
<small>
|
|
86
|
+
Вызовы · {c.calls} Ошибки · {c.failed}
|
|
87
|
+
</small>
|
|
88
|
+
<b>{duration(c.ms)}</b>
|
|
89
|
+
</div>
|
|
90
|
+
))}
|
|
91
|
+
</section>
|
|
92
|
+
)}
|
|
93
|
+
</div>
|
|
94
|
+
)}
|
|
95
|
+
{stats.scope === "all" && stats.workflows.some((w) => w.subject.kind === "script") && (
|
|
96
|
+
<section className="nw-stat-group">
|
|
97
|
+
<h3>Запуски по файлам</h3>
|
|
98
|
+
{stats.workflows.map(
|
|
99
|
+
(w) =>
|
|
100
|
+
w.subject.kind === "script" && (
|
|
101
|
+
<div className="nw-stat-row" key={w.subject.file}>
|
|
102
|
+
<FileRef path={w.subject.file} />
|
|
103
|
+
<small>Запуски · {w.measures.runs}</small>
|
|
104
|
+
</div>
|
|
105
|
+
),
|
|
106
|
+
)}
|
|
107
|
+
</section>
|
|
108
|
+
)}
|
|
109
|
+
{stats.scope === "workflow" &&
|
|
110
|
+
stats.versions.map((v) => (
|
|
111
|
+
<p key={v.version}>
|
|
112
|
+
<code>{v.version.slice(0, 8)}</code> · запусков {v.measures.runs} · без участия человека{" "}
|
|
113
|
+
{percent(v.measures.cleanRate)}
|
|
114
|
+
</p>
|
|
115
|
+
))}
|
|
116
|
+
{versions.length >= 2 && (
|
|
117
|
+
<form
|
|
118
|
+
className="desk-form"
|
|
119
|
+
onSubmit={(e) => {
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
setCompare([a || versions.at(-2)!, b || versions.at(-1)!]);
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
<div className="desk-tools">
|
|
125
|
+
{[
|
|
126
|
+
["Первая версия", a || versions.at(-2)!],
|
|
127
|
+
["Вторая версия", b || versions.at(-1)!],
|
|
128
|
+
].map(([title, val], i) => (
|
|
129
|
+
<label key={title}>
|
|
130
|
+
{title}
|
|
131
|
+
<select value={val} onChange={(e) => (i ? setB : setA)(e.target.value)}>
|
|
132
|
+
{versions.map((v) => (
|
|
133
|
+
<option key={v} value={v}>
|
|
134
|
+
{v.slice(0, 8)}
|
|
135
|
+
</option>
|
|
136
|
+
))}
|
|
137
|
+
</select>
|
|
138
|
+
</label>
|
|
139
|
+
))}
|
|
140
|
+
<button disabled={(a || versions.at(-2)) === (b || versions.at(-1))}>Сравнить</button>
|
|
141
|
+
</div>
|
|
142
|
+
{compare && compared.error && <p role="alert">{compared.error.message}</p>}
|
|
143
|
+
{compare && compared.data?.scope === "workflow" && (
|
|
144
|
+
<table>
|
|
145
|
+
<thead>
|
|
146
|
+
<tr>
|
|
147
|
+
<th>Метрика</th>
|
|
148
|
+
{compared.data.versions.map((v) => (
|
|
149
|
+
<th key={v.version}>{v.version.slice(0, 8)}</th>
|
|
150
|
+
))}
|
|
151
|
+
</tr>
|
|
152
|
+
</thead>
|
|
153
|
+
<tbody>
|
|
154
|
+
{metrics.map(([label, read]) => (
|
|
155
|
+
<tr key={label}>
|
|
156
|
+
<th>{label}</th>
|
|
157
|
+
{compared.data?.scope === "workflow" &&
|
|
158
|
+
compared.data.versions.map((v) => <td key={v.version}>{read(v.measures)}</td>)}
|
|
159
|
+
</tr>
|
|
160
|
+
))}
|
|
161
|
+
</tbody>
|
|
162
|
+
</table>
|
|
163
|
+
)}
|
|
164
|
+
</form>
|
|
165
|
+
)}
|
|
166
|
+
{stats.caveats.length > 0 && (
|
|
167
|
+
<details>
|
|
168
|
+
<summary>Как считается статистика</summary>
|
|
169
|
+
{stats.caveats.map((note) => (
|
|
170
|
+
<p key={note}>{note}</p>
|
|
171
|
+
))}
|
|
172
|
+
</details>
|
|
173
|
+
)}
|
|
174
|
+
</PageSection>
|
|
175
|
+
);
|
|
176
|
+
}
|