Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions apps/web/components/teams/team-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ import { autonomyDraft, teamAutonomyInput, type TeamApprovalChoice } from "@/lib
import type { AgentTeam, TeamRepository, WorkSource } from "@/lib/types";
import { cn } from "@/lib/utils";

import { cleanWorkflow, workflowFromSteps, workflowIssues, type Workflow } from "@/lib/workflow";
import {
agentLabel,
cleanWorkflow,
workflowFromSteps,
workflowIssues,
type AgentListState,
type Workflow,
} from "@/lib/workflow";

import { MODE_HINT, MODE_LABEL, StepNumber, modelLabel, type TeamMode } from "./team-parts";
import { WorkflowEditor } from "./workflow/workflow-editor";
Expand Down Expand Up @@ -270,6 +277,9 @@ export function TeamForm({
});

const agentOptions = (agents.data?.items ?? []).filter((agent) => agent.status !== "archived");
// The editor gets archived agents too, so a step that still names one says "Archived agent".
const workflowAgents = (agents.data?.items ?? []).map((agent) => ({ ...agent, archived: agent.status === "archived" }));
const agentsState: AgentListState = agents.isError ? "error" : agents.isPending ? "loading" : "ready";
const agentById = new Map((agents.data?.items ?? []).map((agent) => [agent.id, agent]));
const primary = agentById.get(draft.steps[0]?.agentId ?? "");
const isWorkflow = draft.mode === "workflow";
Expand Down Expand Up @@ -511,7 +521,8 @@ export function TeamForm({
<WorkflowEditor
value={workflow}
onChange={(next) => set("workflow", next)}
agents={agentOptions}
agents={workflowAgents}
agentsState={agentsState}
issues={workflowProblems}
revealProblem={revealProblem}
/>
Expand Down Expand Up @@ -549,7 +560,11 @@ export function TeamForm({
{agent.harness ? ` (${brandName(agent.harness)})` : ""}
</option>
))}
{step.agentId && !chosenAgent ? <option value={step.agentId}>Unavailable agent</option> : null}
{step.agentId && (!chosenAgent || chosenAgent.status === "archived") ? (
<option value={step.agentId}>
{agentLabel(step.agentId, workflowAgents, agentsState)}
</option>
) : null}
</Select>
<p className="text-xs text-muted-foreground">
{index === 0
Expand Down
57 changes: 39 additions & 18 deletions apps/web/components/teams/workflow/workflow-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
WORKFLOW_EXAMPLES,
WORKFLOW_START,
addStep,
agentLabel,
apart,
conditionOfPort,
conditionPhrase,
Expand All @@ -39,6 +40,8 @@ import {
isStartEdge,
lastStep,
loopSteps,
nextInput,
nextOutput,
portOf,
patchEdge,
patchStep,
Expand All @@ -51,6 +54,7 @@ import {
suggestCondition,
tidy,
withPositions,
type AgentListState,
type Workflow,
type WorkflowEdge,
type WorkflowIssue,
Expand All @@ -64,6 +68,8 @@ export interface AgentOption {
id: string;
name: string;
harness?: string | null;
/** Archived agents are passed too, so a step that still names one can say so; they are not offered. */
archived?: boolean;
}

/** "success" | "failure" | "always" | "outcome:<label>" for one select. */
Expand Down Expand Up @@ -214,13 +220,15 @@ function StepFields({
workflow,
node,
agents,
agentsState,
onChange,
onSelect,
inLoop,
}: {
workflow: Workflow;
node: WorkflowNode;
agents: AgentOption[];
agentsState: AgentListState;
onChange: (next: Workflow) => void;
onSelect: (selection: WorkflowSelection) => void;
inLoop: boolean;
Expand All @@ -231,7 +239,10 @@ function StepFields({
const starts = startsRun(workflow, node.id);
const others = workflow.nodes.filter((item) => item.id !== node.id);
const outcomes = node.outcomes ?? [];
const known = agents.some((agent) => agent.id === node.agentId);
const offered = agents.filter((agent) => !agent.archived);
const known = offered.some((agent) => agent.id === node.agentId);
const addInput = nextInput(workflow, node.id);
const addOutput = nextOutput(workflow, node.id);
const unused = outcomes.filter((outcome) => !lines.some((edge) => edge.when === "outcome" && edge.outcome === outcome));
const key = `step-${node.id}`;
const ports = stepPorts(workflow, node, true);
Expand All @@ -251,13 +262,15 @@ function StepFields({
<Field label="Agent" htmlFor={`${key}-agent`}>
<Select id={`${key}-agent`} value={node.agentId} onChange={(event) => patch({ agentId: event.target.value })}>
<option value="">Choose an agent…</option>
{agents.map((agent) => (
{offered.map((agent) => (
<option key={agent.id} value={agent.id}>
{agent.name}
{agent.harness ? ` (${brandName(agent.harness)})` : ""}
</option>
))}
{node.agentId && !known ? <option value={node.agentId}>Unavailable agent</option> : null}
{node.agentId && !known ? (
<option value={node.agentId}>{agentLabel(node.agentId, agents, agentsState)}</option>
) : null}
</Select>
</Field>
</div>
Expand Down Expand Up @@ -354,15 +367,16 @@ function StepFields({
type="button"
size="sm"
variant="outline"
disabled={!others.length}
onClick={() => {
const source = others.find((item) => !incoming.some((edge) => edge.from === item.id)) ?? others[0];
const suggestion = suggestCondition(workflow, source.id);
onChange(connect(workflow, source.id, node.id, suggestion.when, suggestion.outcome).workflow);
}}
disabled={!addInput}
onClick={() => addInput && onChange(addInput)}
>
<Plus /> Add an input
</Button>
{!addInput && others.length ? (
<p className="text-[13px] text-muted-foreground">
Every other step already leads here. Change a line&apos;s condition, or draw one on the canvas.
</p>
) : null}
</section>

<section className="space-y-3 rounded-lg border bg-subtle/60 p-3" aria-label="Outputs">
Expand Down Expand Up @@ -433,16 +447,17 @@ function StepFields({
type="button"
size="sm"
variant="outline"
onClick={() => {
const suggestion = suggestCondition(workflow, node.id);
// With no other step to go to, the step can still repeat itself.
const target = others.find((item) => !lines.some((edge) => edge.to === item.id)) ?? others[0] ?? node;
const result = connect(workflow, node.id, target.id, suggestion.when, suggestion.outcome);
onChange(result.workflow);
}}
disabled={!addOutput}
// With no other step to go to, the step can still repeat itself.
onClick={() => addOutput && onChange(addOutput)}
>
<Plus /> Add an output
</Button>
{!addOutput ? (
<p className="text-[13px] text-muted-foreground">
This output already leads to every step. Change a line&apos;s condition, or add an outcome first.
</p>
) : null}
</section>

{inLoop ? (
Expand Down Expand Up @@ -605,12 +620,15 @@ export function WorkflowEditor({
value,
onChange,
agents,
agentsState = "ready",
issues,
revealProblem,
}: {
value: Workflow;
onChange: (next: Workflow) => void;
agents: AgentOption[];
/** While the agent list loads (or if it failed), steps do not call their agents unavailable. */
agentsState?: AgentListState;
issues: WorkflowIssue[];
/** Changes when a save was refused: open the first step or line that has a problem. */
revealProblem?: number;
Expand All @@ -631,6 +649,8 @@ export function WorkflowEditor({
const [, redraw] = useState(0);
const change = useCallback(
(next: Workflow) => {
// A change that changes nothing ("Add an output" when the line exists) is no Undo step.
if (next === current.current) return;
const log = history.current;
const now = Date.now();
if (now - log.at > 700) {
Expand Down Expand Up @@ -671,8 +691,7 @@ export function WorkflowEditor({
return () => document.removeEventListener("keydown", onKeyDown);
}, [step]);

const names = useMemo(() => new Map(agents.map((agent) => [agent.id, agent.name])), [agents]);
const agentName = useCallback((id: string) => names.get(id) ?? "Unavailable agent", [names]);
const agentName = useCallback((id: string) => agentLabel(id, agents, agentsState), [agents, agentsState]);
const loops = useMemo(() => loopSteps(value), [value]);
const problems = useMemo(
() => new Set(issues.flatMap((issue) => (issue.nodeId ? [issue.nodeId] : []))),
Expand Down Expand Up @@ -855,6 +874,7 @@ export function WorkflowEditor({
workflow={value}
node={node}
agents={agents}
agentsState={agentsState}
onChange={change}
onSelect={setSelection}
inLoop={loops.has(node.id)}
Expand Down Expand Up @@ -951,6 +971,7 @@ export function WorkflowEditor({
workflow={value}
node={selectedNode}
agents={agents}
agentsState={agentsState}
onChange={change}
onSelect={setSelection}
inLoop={loops.has(selectedNode.id)}
Expand Down
53 changes: 53 additions & 0 deletions apps/web/lib/workflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";

import {
WORKFLOW_EXAMPLES,
WORKFLOW_START,
addStep,
agentLabel,
apart,
cleanWorkflow,
conditionOfPort,
Expand All @@ -16,6 +18,8 @@ import {
limitReached,
liveEdges,
loopSteps,
nextInput,
nextOutput,
nodeRunStates,
nodeHeight,
patchStep,
Expand Down Expand Up @@ -349,3 +353,52 @@ test("names become ids", () => {
assert.equal(slugify("Run the tests!"), "run-the-tests");
assert.equal(slugify(" "), "");
});

test("the editor agrees with the server on the shared validation cases", () => {
// The same file is asserted by `validate` in crates/orbit-domain/src/workflow.rs.
const fixture = JSON.parse(
readFileSync(new URL("../../../crates/orbit-domain/tests/fixtures/workflow-validation.json", import.meta.url), "utf8"),
) as { cases: { name: string; valid: boolean; workflow: Workflow }[] };
assert.ok(fixture.cases.length > 20);
for (const { name, valid, workflow } of fixture.cases) {
const issues = workflowIssues(workflow);
if (valid) assert.deepEqual(issues, [], name);
else assert.ok(issues.length > 0, `${name}: the editor accepts what the server refuses`);
}
});

test("adding an input or output never returns the same workflow", () => {
const two: Workflow = {
nodes: [
{ id: "a", name: "A", agentId: "x" },
{ id: "b", name: "B", agentId: "x" },
],
edges: [{ id: "s", from: WORKFLOW_START, to: "a", when: "always" }],
};
const withLine = nextOutput(two, "a");
assert.ok(withLine);
assert.deepEqual(withLine.edges.at(-1), { id: withLine.edges.at(-1)?.id, from: "a", to: "b", when: "success", outcome: null });
// The suggested line now exists: nothing is left to add, in either direction.
assert.equal(nextOutput(withLine, "a"), null);
assert.equal(nextInput(withLine, "b"), null);
// b can still feed a.
assert.equal(nextInput(withLine, "a")?.edges.at(-1)?.from, "b");
// A step on its own can repeat itself once.
const single: Workflow = { nodes: [{ id: "a", name: "A", agentId: "x" }], edges: [] };
const loop = nextOutput(single, "a");
assert.equal(loop?.edges[0].to, "a");
assert.equal(nextOutput(loop!, "a"), null);
assert.equal(nextInput(single, "a"), null);
});

test("steps name archived agents and do not call a loading list unavailable", () => {
const agents = [
{ id: "1", name: "Builder" },
{ id: "2", name: "Old reviewer", archived: true },
];
assert.equal(agentLabel("1", agents), "Builder");
assert.equal(agentLabel("2", agents), "Archived agent: Old reviewer");
assert.equal(agentLabel("3", agents), "Unavailable agent");
assert.equal(agentLabel("3", [], "loading"), "Loading agents…");
assert.equal(agentLabel("3", [], "error"), "Agents could not be loaded");
});
Loading