172 lines
7.8 KiB
Vue
172 lines
7.8 KiB
Vue
<script setup lang="ts">
|
|
import { Label } from "@/components/ui/label";
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { firstLine } from "./first-line";
|
|
import { computed, onMounted, ref, useId, watchEffect } from "vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Kbd } from "@/components/ui/kbd";
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
|
|
import { SuggestInput } from "@/components/ui/suggest-input";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { api } from "@/lib/api";
|
|
import { delivered } from "@/lib/delivered";
|
|
import type { WorkflowDefinition, WorkflowStats } from "@/workflows/workflow-rows";
|
|
import {
|
|
declaredInputs,
|
|
inputValues,
|
|
missingRequiredInputs,
|
|
} from "@/workflows/work-request";
|
|
|
|
/** Starting work enters Archie's normal admitted task queue, not a side door. */
|
|
const props = defineProps<{
|
|
/** Starts this workflow, with no chooser. */ workflow?: string;
|
|
/** An unsaved definition of `workflow` to test-run instead of the stored one. */ draft?: string;
|
|
/** Preselected in the chooser, as the dashboard's quick start hands over. */ initialWorkflow?: string;
|
|
initialInstructions?: string;
|
|
}>();
|
|
const emit = defineEmits<{ started: [taskId: number]; cancel: [] }>();
|
|
const uid = useId();
|
|
|
|
const definitions = ref<WorkflowDefinition[]>([]);
|
|
const stats = ref<WorkflowStats[]>([]);
|
|
const recentRepos = ref<string[]>([]);
|
|
const loadError = ref("");
|
|
onMounted(async () => {
|
|
try {
|
|
const [workflows, repositories] = await Promise.all([
|
|
api.workflows<{ definitions?: WorkflowDefinition[]; workflows?: WorkflowStats[] }>(),
|
|
api.repositories<{ owner: string; name: string }[]>(),
|
|
]);
|
|
definitions.value = (workflows?.definitions ?? []).filter((d) => d.enabled);
|
|
stats.value = workflows?.workflows ?? [];
|
|
recentRepos.value = (repositories ?? []).map((repo) => `${repo.owner}/${repo.name}`);
|
|
} catch (err) {
|
|
loadError.value = String((err as Error).message || err);
|
|
}
|
|
});
|
|
|
|
const repository = ref("");
|
|
const workflow = ref(props.workflow ?? props.initialWorkflow ?? "");
|
|
const instructions = ref(props.initialInstructions ?? "");
|
|
const title = ref(firstLine(instructions.value));
|
|
// One text field per declared input, by input name: the chosen workflow's
|
|
// declaration decides what is rendered and what the request carries.
|
|
const inputText = ref<Record<string, string>>({});
|
|
watchEffect(() => {
|
|
if (!workflow.value && definitions.value[0]) workflow.value = definitions.value[0].id;
|
|
});
|
|
|
|
const selected = computed(() => definitions.value.find((d) => d.id === workflow.value));
|
|
const declared = computed(() => declaredInputs(selected.value));
|
|
const missing = computed(() => missingRequiredInputs(declared.value, inputText.value));
|
|
|
|
// The chosen workflow's repository mode decides the field: none hides it,
|
|
// optional relaxes it, and anything else (required, or undeclared) needs it.
|
|
const repoMode = computed(() => selected.value?.repository ?? "required");
|
|
const repoRequired = computed(() => repoMode.value !== "optional" && repoMode.value !== "none");
|
|
|
|
const statFor = (id: string) => stats.value.find((s) => s.workflow === id);
|
|
|
|
const submitting = ref(false);
|
|
const error = ref("");
|
|
const ready = computed(
|
|
() =>
|
|
!!((repository.value.trim() || !repoRequired.value) && workflow.value && title.value.trim()) &&
|
|
missing.value.length === 0,
|
|
);
|
|
async function submit() {
|
|
if (!ready.value || submitting.value) return;
|
|
submitting.value = true;
|
|
error.value = "";
|
|
try {
|
|
const result = await api.workRequest<{ task_id: number }>({
|
|
repository: repoMode.value === "none" ? "" : repository.value.trim(),
|
|
workflow: workflow.value,
|
|
title: title.value.trim(),
|
|
instructions: instructions.value,
|
|
inputs: inputValues(declared.value, inputText.value),
|
|
...(props.draft ? { draft: props.draft } : {}),
|
|
});
|
|
emit("started", result.task_id);
|
|
} catch (err) {
|
|
error.value = String((err as Error).message || err);
|
|
} finally {
|
|
submitting.value = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<form class="grid gap-5" @submit.prevent="submit" @keydown.meta.enter.prevent="submit" @keydown.ctrl.enter.prevent="submit">
|
|
<Alert v-if="loadError" variant="destructive"><AlertDescription>{{ loadError }}</AlertDescription></Alert>
|
|
|
|
<div v-if="repoMode !== 'none'" class="grid gap-1.5">
|
|
<Label :for="`${uid}-repo`" class="text-label font-medium">Repository<span v-if="!repoRequired" class="font-normal text-fg-subtle"> (optional)</span>
|
|
</Label>
|
|
<SuggestInput :id="`${uid}-repo`" v-model="repository" class="font-mono" placeholder="owner/repository" :options="recentRepos" :required="repoRequired" />
|
|
</div>
|
|
|
|
<div v-if="!props.workflow" class="grid gap-1.5">
|
|
<span :id="`${uid}-wf`" class="text-label font-medium">Workflow</span>
|
|
<RadioGroup v-model="workflow" :aria-labelledby="`${uid}-wf`" class="grid grid-cols-1 gap-2 sm:grid-cols-3" loop>
|
|
<Label
|
|
v-for="d in definitions"
|
|
:key="d.id"
|
|
class="flex cursor-pointer items-start gap-3 rounded-md border border-border bg-card px-3 py-2.5 text-left transition-colors hover:bg-secondary has-data-checked:border-primary has-data-checked:bg-secondary"
|
|
>
|
|
<RadioGroupItem :value="d.id" class="mt-0.5" />
|
|
<span class="min-w-0">
|
|
<span class="block truncate text-label font-medium">{{ d.name || d.id }}</span>
|
|
<span class="block text-xs text-fg-subtle">
|
|
{{ statFor(d.id)?.runs || 0 }} runs<template v-if="statFor(d.id)?.runs">
|
|
· {{ Math.round((delivered(statFor(d.id)) / (statFor(d.id)?.runs || 1)) * 100) }}% delivered</template
|
|
>
|
|
</span>
|
|
</span>
|
|
</Label>
|
|
</RadioGroup>
|
|
<p v-if="!definitions.length && !loadError" class="text-xs text-fg-subtle">No enabled workflows.</p>
|
|
</div>
|
|
|
|
<!-- One field per input the chosen workflow declares: pr-review cannot run
|
|
without its pr_number, and the server refuses a request that does not
|
|
set what the workflow declares. The declared type is the hint, so the
|
|
value is typed the way the workflow reads it. -->
|
|
<div v-if="declared.length" class="grid gap-3">
|
|
<div v-for="field in declared" :key="field.name" class="grid gap-1.5">
|
|
<Label :for="`${uid}-input-${field.name}`" class="text-label font-medium">
|
|
<span class="font-mono">{{ field.name }}</span>
|
|
<span v-if="field.required" class="text-fg-subtle"> · required</span>
|
|
</Label>
|
|
<Input
|
|
:id="`${uid}-input-${field.name}`"
|
|
:model-value="inputText[field.name]"
|
|
:type="field.type === 'number' ? 'number' : 'text'"
|
|
class="font-mono"
|
|
:required="field.required"
|
|
@update:model-value="(value) => (inputText[field.name] = String(value ?? ''))"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="grid gap-1.5">
|
|
<Label :for="`${uid}-title`" class="text-label font-medium">Title</Label>
|
|
<Input :id="`${uid}-title`" v-model="title" maxlength="80" required />
|
|
</div>
|
|
<div class="grid gap-1.5">
|
|
<Label :for="`${uid}-instr`" class="text-label font-medium">Description</Label>
|
|
<Textarea :id="`${uid}-instr`" v-model="instructions" :rows="4" />
|
|
</div>
|
|
|
|
<Alert v-if="error" variant="destructive"><AlertDescription>{{ error }}</AlertDescription></Alert>
|
|
<div class="flex items-center gap-2 border-t border-border pt-4">
|
|
<span class="mr-auto text-xs text-fg-subtle"><Kbd>⌘</Kbd> <Kbd>↵</Kbd> to start</span>
|
|
<Button type="button" variant="ghost" @click="emit('cancel')">Cancel</Button>
|
|
<Button type="submit" :disabled="!ready || submitting"><Spinner v-if="submitting" data-icon="inline-start" />{{ props.draft ? "Start test" : "Start task" }}</Button>
|
|
</div>
|
|
</form>
|
|
</template>
|