96 lines
3.5 KiB
Vue
96 lines
3.5 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from "vue";
|
|
import { useRouter } from "vue-router";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { api } from "@/lib/api";
|
|
import { useOrgStore } from "@/stores/org";
|
|
import NewTaskDialog from "@/tasks/NewTaskDialog.vue";
|
|
import { firstLine } from "@/tasks/first-line";
|
|
import type { WorkflowDefinition } from "@/workflows/workflow-rows";
|
|
import { declaredInputs } from "@/workflows/work-request";
|
|
|
|
/**
|
|
* Start a task from a prompt. A workflow that needs more than the prompt (a
|
|
* repository, a required input) opens the full form with the prompt carried
|
|
* over, rather than being refused by the server.
|
|
*/
|
|
const router = useRouter();
|
|
const prompt = ref("");
|
|
const workflow = ref("");
|
|
const definitions = ref<WorkflowDefinition[]>([]);
|
|
const loadError = ref("");
|
|
const error = ref("");
|
|
const submitting = ref(false);
|
|
const formOpen = ref(false);
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
const list = await api.workflows<{ definitions?: WorkflowDefinition[] }>();
|
|
definitions.value = (list?.definitions ?? []).filter((d) => d.enabled);
|
|
workflow.value ||= definitions.value[0]?.id ?? "";
|
|
} catch (err) {
|
|
loadError.value = String((err as Error).message || err);
|
|
}
|
|
});
|
|
|
|
const org = useOrgStore();
|
|
org.watchIdentities();
|
|
const actor = computed(() => org.identities.find((i) => i.kind !== "system" && i.lifecycle === "active"));
|
|
|
|
const selected = computed(() => definitions.value.find((d) => d.id === workflow.value));
|
|
const needsForm = computed(() => {
|
|
const mode = selected.value?.repository ?? "required";
|
|
return (mode !== "none" && mode !== "optional") || declaredInputs(selected.value).some((i) => i.required) || !actor.value;
|
|
});
|
|
|
|
async function run() {
|
|
if (!prompt.value.trim() || !workflow.value || submitting.value) return;
|
|
if (needsForm.value) {
|
|
formOpen.value = true;
|
|
return;
|
|
}
|
|
submitting.value = true;
|
|
error.value = "";
|
|
try {
|
|
const result = await api.workRequest<{ task_id: number }>({
|
|
identity: actor.value!.id,
|
|
repository: "",
|
|
workflow: workflow.value,
|
|
title: firstLine(prompt.value),
|
|
instructions: prompt.value,
|
|
inputs: {},
|
|
});
|
|
void router.push(`/tasks/${result.task_id}`);
|
|
} catch (err) {
|
|
error.value = String((err as Error).message || err);
|
|
} finally {
|
|
submitting.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<form class="grid gap-1.5" @submit.prevent="run">
|
|
<div class="flex gap-2 max-sm:flex-wrap">
|
|
<Input v-model="prompt" class="min-w-0 flex-1" placeholder="Describe a task" aria-label="Task" />
|
|
<NativeSelect v-model="workflow" aria-label="Workflow" class="font-mono text-[13px]" :disabled="!definitions.length">
|
|
<NativeSelectOption v-for="d in definitions" :key="d.id" :value="d.id">{{ d.name || d.id }}</NativeSelectOption>
|
|
</NativeSelect>
|
|
<Button type="submit" :disabled="!prompt.trim() || !workflow || submitting">
|
|
<Spinner v-if="submitting" data-icon="inline-start" />Run
|
|
</Button>
|
|
</div>
|
|
<p v-if="loadError || error" class="text-sm text-danger" role="alert">{{ loadError || error }}</p>
|
|
<p v-else-if="!definitions.length" class="text-xs text-fg-subtle">
|
|
No enabled workflows. <RouterLink to="/workflows" class="underline">Enable one</RouterLink>
|
|
</p>
|
|
</form>
|
|
<NewTaskDialog v-model:open="formOpen" :workflow="workflow" :instructions="prompt" />
|
|
</div>
|
|
</template>
|