feat(workflows): iterate on a workflow's draft with Archie from the canvas

This commit is contained in:
sam committed 2026-10-18 02:27:41 +11:00
1 parent ee8c418cb2
commit 7725cf1c1f
9 files changed
+330 -87

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
<script setup lang="ts">
import { ref } from "vue";
import { ArrowUp, Sparkles, Square } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
import { api } from "@/lib/api";
import { randomUUID } from "@/lib/uuid";
import { channelID } from "@/chat/channel";
/**
* Archie edits the workflow with the operator. Each request is a chat turn in
* a conversation of its own per workflow, so "not that one, try again" has the
* earlier turns as context. Archie writes the draft; the operator keeps or
* reverts each change in the draft review and publishes.
*/
const props = defineProps<{ workflow: string; empty: boolean; beforeSend: () => Promise<void> }>();
const emit = defineEmits<{ proposed: [] }>();
const text = ref("");
const reply = ref("");
const activity = ref("");
const failed = ref(false);
const busy = ref(false);
let controller: AbortController | undefined;
interface Frame { type?: string; text?: string; tool?: string }
async function send(): Promise<void> {
const request = text.value.trim();
if (!request || busy.value) return;
busy.value = true;
failed.value = false;
reply.value = "";
activity.value = "";
text.value = "";
controller = new AbortController();
try {
await props.beforeSend();
const response = await api.chatStream(
{
channel_id: `${channelID()}:workflow:${props.workflow}`,
source_id: randomUUID(),
text: `[Editing workflow "${props.workflow}"; write changes with workflow_draft] ${request}`,
page: location.pathname + location.search,
},
{ signal: controller.signal },
);
if (!response.body) throw new Error("no response body");
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let finished = false;
// Read to the end: a turn that stops without a done frame failed.
for (;;) {
const { value, done } = await reader.read();
buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
const frames = buffer.split("\n\n");
buffer = frames.pop() || "";
for (const frame of frames) {
const line = frame.split("\n").find((part) => part.startsWith("data: "));
if (!line) continue;
const event = JSON.parse(line.slice(6)) as Frame;
if (event.type === "delta") reply.value += event.text ?? "";
if (event.type === "tool" && event.tool) activity.value = event.tool.replaceAll("_", " ");
if (event.type === "done") {
finished = true;
if (!reply.value) reply.value = event.text ?? "";
}
if (event.type === "error") throw new Error(event.text);
}
if (done) break;
}
if (!finished) throw new Error("the turn ended early");
} catch (error) {
if ((error as Error).name !== "AbortError") {
failed.value = true;
reply.value = String((error as Error).message || error);
}
} finally {
busy.value = false;
activity.value = "";
controller = undefined;
// Archie may have written the draft even on a turn that failed later.
emit("proposed");
}
}
</script>
<template>
<div class="pointer-events-auto w-full max-w-xl rounded-lg border border-border bg-popover shadow-[var(--shadow-md)]">
<p
v-if="reply"
class="max-h-40 overflow-y-auto whitespace-pre-wrap border-b border-border px-3 py-2 text-sm"
:class="failed && 'text-danger'"
:role="failed ? 'alert' : 'status'"
>{{ reply }}</p>
<form class="flex items-center gap-2 px-3 py-2" @submit.prevent="send">
<Sparkles class="size-4 shrink-0 text-primary" aria-hidden="true" />
<input
v-model="text"
class="min-w-0 flex-1 bg-transparent text-sm outline-none placeholder:text-fg-subtle"
:placeholder="empty ? 'Build with Archie' : 'Iterate with Archie'"
:aria-label="empty ? 'Build with Archie' : 'Iterate with Archie'"
:disabled="busy"
/>
<span v-if="busy" class="flex shrink-0 items-center gap-1.5 text-xs text-fg-subtle">
<Spinner />{{ activity }}
</span>
<Button v-if="busy" type="button" variant="ghost" size="icon-sm" aria-label="Stop" @click="controller?.abort()"><Square /></Button>
<Button v-else type="submit" size="icon-sm" aria-label="Send" :disabled="!text.trim()"><ArrowUp /></Button>
</form>
</div>
</template>
+27
View File
@@ -37,6 +37,7 @@ import { useWorkflowHistory } from "./workflow-history";
import { revisionOfRun, type WorkflowRevision } from "./workflow-revisions";
import { deleteStep, duplicateStep, insertStep, moveStep, replaceStep, revertWorkflowChange, stepAt, workflowChanges, workflowField, type StepPath } from "./workflow-edit";
import ChangeList from "@/components/ChangeList.vue";
import CoBuilder from "./CoBuilder.vue";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { useWorkflowRuns } from "./workflow-runs";
import { useWorkflowTriggers } from "./workflow-triggers";
@@ -220,6 +221,22 @@ async function dropDraft(target: string): Promise<void> {
unsaved = undefined;
await workflows.putDraft(target, undefined);
}
// Archie works from the saved draft, so pending edits are written first.
async function flushDraft(): Promise<void> {
clearTimeout(pending);
unsaved = undefined;
await persistDraft(selected.value, yaml.value);
}
// Archie's draft replaces what the editor shows; the review then lists its
// changes against the published workflow.
async function takeProposal(): Promise<void> {
await workflows.reloadDrafts();
const draft = workflows.drafts.find((entry) => entry.id === selected.value)?.yaml;
if (draft === undefined || draft === yaml.value) return;
clearTimeout(pending);
unsaved = undefined;
load(selected.value);
}
async function discard(): Promise<void> {
await dropDraft(selected.value);
load(selected.value);
@@ -431,6 +448,16 @@ function syncScroll(event: Event): void {
</template>
</template>
</WorkflowCanvas>
<div class="pointer-events-none absolute inset-x-0 bottom-3 z-10 flex justify-center px-3">
<CoBuilder
v-if="selected"
:key="selected"
:workflow="selected"
:empty="!parsed.ok || parsed.steps.length === 0"
:before-send="flushDraft"
@proposed="takeProposal"
/>
</div>
</div>
</TabsContent>
<TabsContent value="history">