116 lines
4.4 KiB
Vue
116 lines
4.4 KiB
Vue
<script setup lang="ts">
|
|
import { ref, watch } 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: []; working: [boolean] }>();
|
|
|
|
const text = ref("");
|
|
const reply = ref("");
|
|
const activity = ref("");
|
|
const failed = ref(false);
|
|
const busy = ref(false);
|
|
let controller: AbortController | undefined;
|
|
watch(busy, (value) => emit("working", value));
|
|
|
|
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>
|