diff --git a/modules/governance/accounts_api.go b/modules/governance/accounts_api.go index 1f32aff62..75b127476 100644 --- a/modules/governance/accounts_api.go +++ b/modules/governance/accounts_api.go @@ -608,13 +608,13 @@ func (m *Module) capabilities(ctx context.Context, _ *struct{}) (*httpapi.Reply[ sections := map[string]bool{"chat": signedIn, "memory": signedIn} for _, name := range []string{ "settings", "events", "workflows", "runs", "credentials", "extensions", "secret_engines", "models", "controls", "memory_admin", - "home", "system_status", "org_config", + "home", "system_status", "org_config", "workflow_editor", } { sections[name] = p.InstanceAdmin() } for _, name := range []string{ "tasks", "work_requests", "logs", "skills", "curators", "channels", - "captures", "mappings", "bindings", "workflow_editor", "extension_catalogue", "model_aliases", + "captures", "mappings", "bindings", "extension_catalogue", "model_aliases", "task_execution", "organisations", "tools", "plugins", "container_runtime", "agent_profiles", "org_agents", "org_audit", "org_usage", "org_work_intake", } { diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index c4c093eea..fb53abe8a 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -185,7 +185,7 @@ async function request( export const api = { // The archie binary's module API; the wire types are below the object. - runs: (limit = 200) => request("/api/runs" + qs({ limit })), + runs: (limit = 200, workflow?: string) => request("/api/runs" + qs({ limit, workflow })), run: (id: string) => request(`/api/runs/${encodeURIComponent(id)}`), /** Queues a parked run again from the step that parked it, or from `from`. */ retryRun: (id: string, from?: string) => @@ -212,9 +212,32 @@ export const api = { workflowList: () => request("/api/workflows"), workflow: (name: string, version?: number) => request(`/api/workflows/${encodeURIComponent(name)}` + qs({ version })), - putWorkflow: (name: string, body: string) => - request(`/api/workflows/${encodeURIComponent(name)}`, { method: "PUT", body: { body } }), - workflowStepTypes: () => request<{ name: string; needs_repository: boolean; settings?: unknown }[]>("/api/workflow-step-types"), + /** Publishes a document as the next version; `expectedVersion` makes the save fail with 409 when another is stored. */ + putWorkflow: (name: string, body: string, expectedVersion?: number) => + request(`/api/workflows/${encodeURIComponent(name)}`, { method: "PUT", body: { body, expected_version: expectedVersion } }), + deleteWorkflow: (name: string) => request(`/api/workflows/${encodeURIComponent(name)}`, { method: "DELETE", parse: false }), + setWorkflowEnabled: (name: string, enabled: boolean) => + request(`/api/workflows/${encodeURIComponent(name)}/enabled`, { method: "PUT", body: { enabled } }), + runWorkflow: (name: string, input?: Record) => + request(`/api/workflows/${encodeURIComponent(name)}/runs`, { method: "POST", body: { input } }), + workflowDrafts: () => request("/api/workflow-drafts"), + /** A workflow's draft, or null when it has none. */ + workflowDraft: async (name: string) => { + try { + return await request(`/api/workflows/${encodeURIComponent(name)}/draft`); + } catch (cause) { + if (cause instanceof ApiError && cause.status === 404) return null; + throw cause; + } + }, + /** Saves a draft; `revision` is the one last read, 0 for none, and a stale one fails with 409. */ + putWorkflowDraft: (name: string, body: string, revision: number) => + request(`/api/workflows/${encodeURIComponent(name)}/draft`, { method: "PUT", body: { body, revision } }), + discardWorkflowDraft: (name: string) => request(`/api/workflows/${encodeURIComponent(name)}/draft`, { method: "DELETE", parse: false }), + testWorkflowDraft: (name: string, input?: Record) => + request(`/api/workflows/${encodeURIComponent(name)}/draft/test-run`, { method: "POST", body: { input } }), + workflowStats: () => request("/api/workflow-stats"), + workflowStepTypes: () => request("/api/workflow-step-types"), extensionKinds: () => request("/api/extension-kinds"), extensions: (kind: ExtensionKind) => request(`/api/${kind}`), acceptExtension: (kind: ExtensionKind, name: string, body: ExtensionInput) => @@ -450,12 +473,6 @@ export const api = { setup: () => request("/api/setup"), capabilities: () => request("/api/capabilities"), workflows: () => request("/api/workflows"), - workflowEnabled: (id: string, enabled: boolean) => - request(`/api/workflows/${encodeURIComponent(id)}/enabled`, { - method: "PUT", - body: { enabled }, - parse: false, - }), checkRepository: (input: { owner: string; repository: string; identity_id: string }) => request("/api/control-plane/resources/repository-policies/check", { method: "POST", body: input, timeoutMs: 35000 }), repositories: () => request("/api/repositories"), workRequest: (workRequest: Payload) => @@ -702,6 +719,8 @@ export interface Run { created_at: string; finished_at?: string; result?: unknown; + /** A test run of a draft, which changes nothing. */ + dry?: boolean; } /** A channel account linked to the signed-in account. */ @@ -826,7 +845,28 @@ export interface RunEntry { output?: string; error?: string; unknown?: boolean; - tool_calls?: { name: string; input: string }[]; + /** The top-level step of the definition that made the entry. */ + step?: string; + tool_calls?: { id?: string; name: string; input: string }[]; + /** What a step recorded, when it is not an agent's own call. */ + value?: unknown; + reasoning?: string; + /** The model request exactly as sent: messages, tools and sampling settings. */ + request?: { messages?: { role: string; content?: string }[] }; + usage?: { prompt_tokens?: number; completion_tokens?: number; cached_tokens?: number }; + finish_reason?: string; + input?: string; + call_id?: string; +} + +/** How far one top-level step of a run's definition got. */ +export interface RunStage { + name: string; + type: string; + state: "pending" | "running" | "waiting" | "succeeded" | "failed" | "skipped" | "cancelled"; + started_at?: string; + ended_at?: string; + error?: string; } export interface RunTrail { @@ -834,6 +874,9 @@ export interface RunTrail { origin?: { event_id?: string; source?: string; type?: string; trigger?: string; received_at?: string }; actions?: RunAction[]; entries?: RunEntry[]; + /** The document the run executes: its version, or the draft a test run tested. */ + definition?: string; + stages: RunStage[]; } export interface EventRecord { @@ -899,6 +942,40 @@ export interface WorkflowVersion { steps?: string[]; created_at: string; created_by?: string; + /** False for a workflow nothing may start. */ + enabled: boolean; +} + +/** The unpublished edit of a workflow. */ +export interface WorkflowDraft { + name: string; + body: string; + /** The published version it began from, 0 for a workflow not yet published. */ + base_version: number; + /** Counts the draft's writes; a save names the one it last read. */ + revision: number; + updated_by: string; + updated_at: string; +} + +export interface WorkflowStats { + workflow: string; + total: number; + succeeded: number; + /** Runs that failed, parked or were declined. */ + failed: number; + /** Runs that have not ended. */ + active: number; + /** The share of ended runs that succeeded; absent until one has ended. */ + success_rate?: number; + last_run: { id: string; state: string; at: string }; +} + +export interface StepType { + name: string; + title: string; + needs_repository: boolean; + settings?: unknown; } export type ExtensionGroup = "platform" | "integration" | "agent"; diff --git a/ui/src/router/routes.ts b/ui/src/router/routes.ts index f307d515b..e40d8cc60 100644 --- a/ui/src/router/routes.ts +++ b/ui/src/router/routes.ts @@ -107,14 +107,6 @@ export const routes: RouteRecordRaw[] = [ { path: "/workflows/:id", name: "workflow", - component: () => import("@/workflows/WorkflowPage.vue"), - meta: { label: "Workflow", nav: false, navPath: "/workflows", section: "workflows" }, - }, - // The visual editor, with drafts, test runs and per-step statistics; it - // needs workflow drafts and run stages the binary does not serve yet. - { - path: "/workflow-editor/:id", - name: "workflow-editor", component: WorkflowEditorPage, meta: { label: "Workflow", nav: false, navPath: "/workflows", section: "workflow_editor" }, }, diff --git a/ui/src/settings/SystemTasksPage.vue b/ui/src/settings/SystemTasksPage.vue index 5db4be024..5b18f3f7c 100644 --- a/ui/src/settings/SystemTasksPage.vue +++ b/ui/src/settings/SystemTasksPage.vue @@ -3,10 +3,8 @@ import { Alert, AlertDescription } from "@/components/ui/alert"; import ApplyStatusRows from "./ApplyStatusRows.vue"; import KindGate from "./KindGate.vue"; import { computed, onMounted, ref } from "vue"; -import { RotateCcw } from "@lucide/vue"; import { storeToRefs } from "pinia"; -import { Button } from "@/components/ui/button"; import PageHeader from "@/base/PageHeader.vue"; import { DurationInput, formatGoDuration, parseGoDuration } from "@/components/ui/duration-input"; import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field"; @@ -17,18 +15,7 @@ import { NumberFieldIncrement, NumberFieldInput, } from "@/components/ui/number-field"; -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, -} from "@/components/ui/alert-dialog"; -import { cloneControlPlaneValue, resourcesForPage, useSettingsStore } from "@/stores/settings"; -import { restoreShippedDefinitions, useWorkflowsStore } from "@/stores/workflows"; +import { resourcesForPage, useSettingsStore } from "@/stores/settings"; import DraftHint from "./DraftHint.vue"; import HistoryLink from "./HistoryLink.vue"; @@ -71,12 +58,6 @@ function durationSeconds(text: string): number | undefined { const ms = parseGoDuration(text); return ms === null ? undefined : Math.round(ms / 1000); } - -const workflows = useWorkflowsStore(); -const confirmRestore = ref(false); -async function restoreWorkflows(): Promise { - await workflows.save(cloneControlPlaneValue(restoreShippedDefinitions(workflows.collection, workflows.shipped))); -} diff --git a/ui/src/stores/workflows.ts b/ui/src/stores/workflows.ts index 72df4c245..1f3eebccb 100644 --- a/ui/src/stores/workflows.ts +++ b/ui/src/stores/workflows.ts @@ -1,104 +1,140 @@ import { defineStore } from "pinia"; -import { computed } from "vue"; +import { computed, reactive, ref } from "vue"; +import { errors } from "../copy/errors/index.ts"; +import { api, ApiError, describeError } from "../lib/api.ts"; import type { StepTypeInfo } from "../workflows/workflow-yaml.ts"; -import { useSettingsStore } from "./settings.ts"; - -const KIND = "workflow-definitions"; -const DRAFTS = "workflow-drafts"; export interface WorkflowDefinitionEntry { id: string; yaml: string; + /** The published version the yaml is. */ + version: number; + enabled: boolean; } export interface WorkflowDefinitionCollection { definitions: WorkflowDefinitionEntry[]; } -export function upsertWorkflowDefinition( - collection: WorkflowDefinitionCollection, - entry: WorkflowDefinitionEntry, -): WorkflowDefinitionCollection { - const found = collection.definitions.some(({ id }) => id === entry.id); - return { - definitions: found - ? collection.definitions.map((definition) => - definition.id === entry.id ? entry : definition, - ) - : [...collection.definitions, entry], - }; +/** A workflow's unpublished edit. */ +export interface WorkflowDraftEntry { + id: string; + yaml: string; + /** The draft's revision as last read; a save names it. */ + revision: number; + /** The published version the draft began from, 0 for a workflow not yet published. */ + baseVersion: number; } -// restoreShippedDefinitions resets every shipped workflow to what archie ships -// and keeps the operator's custom ones, which archie has no copy of to restore. -export function restoreShippedDefinitions( - collection: WorkflowDefinitionCollection, - shipped: WorkflowDefinitionCollection, -): WorkflowDefinitionCollection { - return shipped.definitions.reduce(upsertWorkflowDefinition, collection); -} - -export function removeWorkflowDefinition( - collection: WorkflowDefinitionCollection, - id: string, -): WorkflowDefinitionCollection { - return { - definitions: collection.definitions.filter( - (definition) => definition.id !== id, - ), - }; -} - -function parse(json: string | undefined, pick: (value: Record) => unknown, fallback: T): T { - if (!json) return fallback; - try { - return (pick(JSON.parse(json)) as T | undefined) ?? fallback; - } catch { - return fallback; - } -} - -/** The workflow definitions: one control-plane resource, with the shipped - * copies and step vocabulary its catalog entry serves. */ +/** The published workflows, their drafts and the step vocabulary the server serves. */ export const useWorkflowsStore = defineStore("workflows", () => { - const settings = useSettingsStore(); - const descriptor = computed(() => settings.catalog.find((resource) => resource.kind === KIND)); + const definitions = ref([]); + const drafts = ref([]); + const stepTypes = ref([]); + const loaded = ref(false); + const error = ref(""); + const saving = ref(false); + const draftState = reactive({ saving: false, error: "", conflict: false }); - const state = computed(() => settings.stateFor(KIND)); - const collection = computed( - () => (state.value.resource?.value as WorkflowDefinitionCollection | undefined) ?? { definitions: [] }, - ); - /** The shipped definitions the catalog offers as "restore shipped". */ - const shipped = computed(() => - parse(descriptor.value?.defaults_json, (value) => value, { definitions: [] }), - ); - /** The registered workflow step types, served in the definitions schema. */ - const stepTypes = computed(() => - parse(descriptor.value?.schema_json, (value) => value["x-step-types"], []), - ); + const collection = computed(() => ({ definitions: definitions.value })); + const state = computed(() => ({ resource: loaded.value ? collection.value : undefined, saving: saving.value, error: error.value })); - /** Unpublished edits by workflow id; only a test run runs a draft. */ - const drafts = computed( - () => ((settings.stateFor(DRAFTS).resource?.value as { drafts?: WorkflowDefinitionEntry[] } | undefined)?.drafts ?? []), - ); - /** Writes a workflow's draft, or removes it when yaml is undefined. */ - function putDraft(id: string, yaml: string | undefined): Promise { - const rest = drafts.value.filter((draft) => draft.id !== id); - if (yaml === undefined && rest.length === drafts.value.length) return Promise.resolve(true); - return settings.replace(DRAFTS, { drafts: yaml === undefined ? rest : [...rest, { id, yaml }] }); + async function load(): Promise { + try { + const [published, held, types] = await Promise.all([api.workflowList(), api.workflowDrafts(), api.workflowStepTypes()]); + definitions.value = published.map((w) => ({ id: w.name, yaml: w.body, version: w.version, enabled: w.enabled })); + drafts.value = held.map(draftEntry); + stepTypes.value = types as StepTypeInfo[]; + loaded.value = true; + error.value = ""; + } catch (cause) { + error.value = describeError(cause, errors.common.unknown); + } } - return { - state, - drafts, - draftState: computed(() => settings.stateFor(DRAFTS)), - putDraft, - reloadDrafts: () => settings.loadResource(DRAFTS), - error: computed(() => settings.catalogError || state.value.error), - collection, - shipped, - stepTypes, - load: settings.load, - save: (next: WorkflowDefinitionCollection) => settings.replace(KIND, next), - }; + async function reloadDrafts(): Promise { + try { + drafts.value = (await api.workflowDrafts()).map(draftEntry); + } catch (cause) { + draftState.error = describeError(cause, errors.common.unknown); + } + } + + // Writes to one workflow's draft run one after another, each naming the + // revision the one before it returned. + let writes: Promise = Promise.resolve(); + + /** Writes a workflow's draft, or discards it when yaml is undefined. A draft + * changed elsewhere since it was read is not overwritten: the write fails + * and draftState.conflict says why. */ + function putDraft(id: string, yaml: string | undefined): Promise { + const write = writes.then(() => writeDraft(id, yaml)); + writes = write.catch(() => undefined); + return write; + } + + async function writeDraft(id: string, yaml: string | undefined): Promise { + draftState.saving = true; + draftState.error = ""; + draftState.conflict = false; + try { + const held = drafts.value.find((draft) => draft.id === id); + if (yaml === undefined) { + if (held) await api.discardWorkflowDraft(id); + drafts.value = drafts.value.filter((draft) => draft.id !== id); + return true; + } + const saved = draftEntry(await api.putWorkflowDraft(id, yaml, held?.revision ?? 0)); + drafts.value = [...drafts.value.filter((draft) => draft.id !== id), saved]; + return true; + } catch (cause) { + if (cause instanceof ApiError && cause.status === 409) draftState.conflict = true; + else draftState.error = describeError(cause, errors.common.unknown); + return false; + } finally { + draftState.saving = false; + } + } + + /** Publishes yaml as the next version of workflow id. `expected` is the + * version it was edited from, so a version published since is not overwritten. */ + async function publish(id: string, yaml: string, expected: number): Promise { + saving.value = true; + error.value = ""; + try { + const w = await api.putWorkflow(id, yaml, expected); + const entry = { id: w.name, yaml: w.body, version: w.version, enabled: w.enabled }; + definitions.value = [...definitions.value.filter((d) => d.id !== id), entry].sort((a, b) => a.id.localeCompare(b.id)); + drafts.value = drafts.value.filter((draft) => draft.id !== id); + return true; + } catch (cause) { + error.value = describeError(cause, errors.common.unknown); + return false; + } finally { + saving.value = false; + } + } + + /** Deletes a published workflow with its draft; the server refuses while anything still names it. */ + async function remove(id: string): Promise { + saving.value = true; + error.value = ""; + try { + await api.deleteWorkflow(id); + definitions.value = definitions.value.filter((d) => d.id !== id); + drafts.value = drafts.value.filter((draft) => draft.id !== id); + return true; + } catch (cause) { + error.value = describeError(cause, errors.common.unknown); + return false; + } finally { + saving.value = false; + } + } + + return { state, drafts, draftState, putDraft, reloadDrafts, error, collection, stepTypes, load, publish, remove }; }); + +function draftEntry(draft: { name: string; body: string; revision: number; base_version: number }): WorkflowDraftEntry { + return { id: draft.name, yaml: draft.body, revision: draft.revision, baseVersion: draft.base_version }; +} diff --git a/ui/src/workflows/AgentTranscript.vue b/ui/src/workflows/AgentTranscript.vue index 6eecfc970..5d0a59c42 100644 --- a/ui/src/workflows/AgentTranscript.vue +++ b/ui/src/workflows/AgentTranscript.vue @@ -1,13 +1,12 @@ @@ -293,9 +287,7 @@ const conversations = computed(() => { - - - No transcript recorded + No transcript recorded diff --git a/ui/src/workflows/RunWorkflowButton.vue b/ui/src/workflows/RunWorkflowButton.vue index 65cf9de9c..0448617c8 100644 --- a/ui/src/workflows/RunWorkflowButton.vue +++ b/ui/src/workflows/RunWorkflowButton.vue @@ -1,47 +1,128 @@