Files
archie/ui/src/workflows/WorkflowSettings.vue
T

225 lines
11 KiB
Vue

<script setup lang="ts">
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { Input } from "@/components/ui/input";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
import { computed, ref } from "vue";
import { Plus, Trash2, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Switch } from "@/components/ui/switch";
import { cn } from "@/lib/utils";
import { statusKind, statusLabel, type Binding } from "@/bindings/binding-draft";
import { useOrgStore } from "@/stores/org";
import BindEvent from "./BindEvent.vue";
import { setWorkflowField, workflowField } from "./workflow-edit";
import type { WorkflowTrigger } from "./workflow-triggers";
const props = defineProps<{ yaml: string; triggers: WorkflowTrigger[] }>();
const emit = defineEmits<{ "update:yaml": [string]; close: []; bound: [] }>();
// The bind flow replaces the settings in this panel; null binding is a new one.
const binding = ref<Binding | null | undefined>();
function bound(): void {
binding.value = undefined;
emit("bound");
}
type Spec = { type?: string; required?: boolean };
const TYPES = ["string", "number", "bool", "object", "array", "any"];
// Kept out of the template: a literal reference there would close the
// template's own interpolation.
const EMPTY = {
inputs: "Takes none. Steps read declared inputs as {{ inputs.name }}.",
outputs: "Writes none. A calling workflow can read declared outputs.",
};
const text = (key: string) => {
const value = workflowField(props.yaml, key);
return typeof value === "string" ? value : "";
};
const specs = (key: "inputs" | "outputs") =>
computed(() => Object.entries((workflowField(props.yaml, key) as Record<string, Spec> | undefined) ?? {}));
const inputs = specs("inputs");
const outputs = specs("outputs");
function set(key: string, value: unknown): void {
emit("update:yaml", setWorkflowField(props.yaml, key, value));
}
function setSpec(key: "inputs" | "outputs", entries: [string, Spec][]): void {
set(key, Object.fromEntries(entries.filter(([name]) => name)));
}
function editSpec(key: "inputs" | "outputs", list: [string, Spec][], index: number, name: string, spec: Spec): void {
setSpec(key, list.map((entry, i) => (i === index ? [name, spec] : entry)));
}
function addSpec(key: "inputs" | "outputs", list: [string, Spec][]): void {
let n = list.length + 1;
while (list.some(([name]) => name === `${key.slice(0, -1)}_${n}`)) n++;
setSpec(key, [...list, [`${key.slice(0, -1)}_${n}`, { type: "string" }]]);
}
// The identity a run acts as must be an agent, never a person; a workflow
// naming none is refused when it runs.
const identities = useOrgStore();
identities.watchIdentities();
const agents = computed(() => identities.identities.filter((i) => i.kind !== "user" && i.lifecycle === "active"));
const MODES = [
{ value: "required", label: "Repository", hint: "Clones the task's repository" },
{ value: "optional", label: "If given", hint: "Clones one when the run names it" },
{ value: "none", label: "None", hint: "Scratch workspace, no clone" },
];
const repository = computed(() => text("repository") || "required");
// needs is the workflow's declared harness requirements: captures (a stage
// returns structured output through capture tools) and the largest gate-retry
// budget any stage declares.
const needs = computed<{ captures?: boolean; gate_retries?: number }>(() => {
const value = workflowField(props.yaml, "needs");
return value && typeof value === "object" ? (value as { captures?: boolean; gate_retries?: number }) : {};
});
function setNeeds(next: { captures?: boolean; gate_retries?: number }): void {
set("needs", next);
}
</script>
<template>
<aside class="flex h-full w-[24rem] max-w-full flex-col border-l border-border bg-card shadow-xl">
<BindEvent v-if="binding !== undefined" :yaml="yaml" :binding="binding" @back="binding = undefined" @saved="bound" />
<template v-else>
<header class="flex items-center gap-2 border-b border-border px-4 py-3">
<div class="min-w-0 flex-1">
<div class="text-sm font-medium">Workflow settings</div>
</div>
<Button type="button" variant="ghost" size="icon-sm" aria-label="Close" @click="emit('close')"><X /></Button>
</header>
<div class="flex-1 space-y-5 overflow-y-auto px-4 py-4">
<section class="space-y-1.5">
<div class="text-xs font-medium text-muted-foreground">Works on</div>
<ToggleGroup
type="single"
variant="outline"
size="sm"
class="w-full"
:model-value="repository"
aria-label="Works on"
@update:model-value="(value) => { if (value) set('repository', value === 'required' ? undefined : String(value)); }"
>
<ToggleGroupItem v-for="mode in MODES" :key="mode.value" :value="mode.value" :title="mode.hint" class="flex-1">{{ mode.label }}</ToggleGroupItem>
</ToggleGroup>
</section>
<section v-for="[key, list] in [['inputs', inputs], ['outputs', outputs]] as const" :key="key" class="space-y-1.5">
<div class="flex items-center text-xs font-medium text-muted-foreground">
{{ key === "inputs" ? "Inputs" : "Outputs" }}
<Button type="button" variant="ghost" size="xs" class="ml-auto" @click="addSpec(key, list)"><Plus data-icon="inline-start" />Add</Button>
</div>
<div v-if="list.length" class="divide-y divide-border rounded-md border border-border">
<div v-for="([name, spec], i) in list" :key="i" class="flex items-center gap-2 px-2 py-1.5">
<Input
:model-value="name"
class="h-7 min-w-0 flex-1 font-mono text-xs"
:aria-label="`${key} name`"
@change="editSpec(key, list, i, ($event.target as HTMLInputElement).value.trim(), spec)"
/>
<NativeSelect
size="sm"
:model-value="spec.type ?? 'string'"
@update:model-value="(value) => editSpec(key, list, i, name, { ...spec, type: String(value) })"
>
<NativeSelectOption v-for="type in TYPES" :key="type" :value="type">{{ type }}</NativeSelectOption>
</NativeSelect>
<Switch
:model-value="spec.required === true"
:title="spec.required ? 'Required' : 'Optional'"
@update:model-value="editSpec(key, list, i, name, { ...spec, required: $event || undefined })"
/>
<Button type="button" variant="ghost" size="icon-xs" :aria-label="`Remove ${name}`" @click="setSpec(key, list.filter((_: unknown, j: number) => j !== i))">
<Trash2 />
</Button>
</div>
</div>
<p v-else class="text-xs text-fg-subtle">
{{ EMPTY[key] }}
</p>
</section>
<section class="space-y-1.5">
<div class="text-xs font-medium text-muted-foreground">Runs as</div>
<div class="divide-y divide-border rounded-md border border-border">
<label class="flex min-h-10 items-center gap-3 px-3 py-1.5">
<span class="flex-1 text-[13px]">Identity</span>
<Input
:model-value="text('identity')"
list="workflow-identities"
placeholder="required"
title="Every run acts as this identity; a workflow naming none is refused."
:class="cn('h-7 w-44 text-[13px]', !text('identity') && 'border-danger/60')"
@change="set('identity', ($event.target as HTMLInputElement).value.trim())"
/>
<datalist id="workflow-identities">
<option v-for="agent in agents" :key="agent.id" :value="agent.id">{{ agent.display_name }}</option>
</datalist>
</label>
<label class="flex min-h-10 items-center gap-3 px-3 py-1.5">
<span class="flex-1 text-[13px]">Container profile</span>
<Input
:model-value="text('profile')"
placeholder="default"
class="h-7 w-44 text-[13px]"
@change="set('profile', ($event.target as HTMLInputElement).value.trim())"
/>
</label>
</div>
</section>
<section class="space-y-1.5">
<div class="text-xs font-medium text-muted-foreground">Run needs</div>
<div class="divide-y divide-border rounded-md border border-border">
<label class="flex min-h-10 items-center gap-3 px-3 py-1.5">
<span class="flex-1 text-[13px]">Captures</span>
<Switch :model-value="needs.captures === true" @update:model-value="setNeeds({ ...needs, captures: $event || undefined })" />
</label>
<label class="flex min-h-10 items-center gap-3 px-3 py-1.5">
<span class="flex-1 text-[13px]">Gate retries</span>
<Input
type="number"
min="0"
placeholder="0"
class="h-7 w-20 text-right text-[13px]"
:model-value="needs.gate_retries ?? ''"
@update:model-value="(value) => setNeeds({ ...needs, gate_retries: String(value) === '' ? undefined : Number(String(value)) })"
/>
</label>
</div>
</section>
<section class="space-y-1.5">
<div class="flex items-center text-xs font-medium text-muted-foreground">Starts when
<Button type="button" variant="ghost" size="xs" class="ml-auto" @click="binding = null"><Plus data-icon="inline-start" />Bind an event</Button>
</div>
<ul v-if="triggers.length" class="divide-y divide-border rounded-md border border-border">
<li v-for="trigger in triggers" :key="trigger.kind + trigger.label" class="text-[13px]">
<button v-if="trigger.binding" type="button" class="flex w-full items-center gap-2 px-3 py-2 text-left hover:bg-secondary" @click="binding = trigger.binding">
<span class="truncate">{{ trigger.label }}</span>
<Badge v-if="trigger.binding.status !== 'armed'" :variant="statusKind(trigger.binding.status)">{{ statusLabel(trigger.binding.status) }}</Badge>
<span v-if="trigger.detail" class="ml-auto truncate text-xs text-fg-subtle">{{ trigger.detail }}</span>
</button>
<RouterLink v-else-if="trigger.kind==='workflow' || trigger.kind==='playbook'" :to="trigger.kind==='workflow'?{path:'/workflows',query:{workflow:trigger.label}}:{path:'/settings/playbooks'}" class="flex items-center gap-2 px-3 py-2 hover:bg-secondary">
<span class="truncate">{{trigger.label}}</span><span class="ml-auto truncate text-xs text-fg-subtle">{{trigger.detail}}</span>
</RouterLink>
<div v-else class="flex items-center gap-2 px-3 py-2" title="Built-in issue routing. Disable this workflow to stop it running automatically.">
<span class="truncate">{{ trigger.label }}</span>
<Badge v-if="trigger.kind==='issue'" variant="outline" class="ml-auto">Built-in</Badge>
<span v-if="trigger.detail" class="ml-auto truncate text-xs text-fg-subtle">{{ trigger.detail }}</span>
</div>
</li>
</ul>
<p v-else class="text-xs text-fg-subtle">Nothing starts it automatically.</p>
</section>
</div>
</template>
</aside>
</template>