Files
archie/ui/src/genui/view-library.ts
T
sam 54a78175b4 feat(workflow): render a PRD's generated view on the plan approval card
agent.run gains views: the agent may return an OpenUI Lang view of its
answer through an optional view tool, read as steps.<id>.view. human.approve
stores it as the task's plan_view, and the approval card renders it with
the dashboard's own components, falling back to the plan text when there
is no view or it does not parse. The feasibility prd step uses it.
2026-10-25 16:33:04 +11:00

208 lines
6.1 KiB
TypeScript

import {
createLibrary,
defineComponent,
type Library,
type PromptOptions,
} from "@openuidev/lang-core";
import { z } from "zod/v4";
/**
* The components an agent may compose a view from. The schemas live here,
* apart from the Vue renderers, so the agent prompt (generated in Node) and the
* dashboard renderer are built from the same definitions.
*/
export interface ViewRenderers<C> {
Stack: C;
Section: C;
Columns: C;
Heading: C;
Text: C;
Callout: C;
Facts: C;
Fact: C;
Files: C;
File: C;
Checklist: C;
Table: C;
Code: C;
}
export function viewLibrary<C>(r: ViewRenderers<C>): Library<C> {
const Heading = defineComponent({
name: "Heading",
description: "A short title for what follows.",
props: z.object({ text: z.string() }),
component: r.Heading,
});
const Text = defineComponent({
name: "Text",
description:
"Prose in Markdown: paragraphs, lists, inline code, links. Use for explanation the other components cannot carry.",
props: z.object({ markdown: z.string() }),
component: r.Text,
});
const Callout = defineComponent({
name: "Callout",
description:
"One point the reader must not miss: a risk, an open question, a caveat or a confirmation.",
props: z.object({
tone: z.enum(["info", "warning", "risk", "success"]),
title: z.string(),
body: z.string().optional(),
}),
component: r.Callout,
});
const Fact = defineComponent({
name: "Fact",
description: "One label and its short value.",
props: z.object({ label: z.string(), value: z.string() }),
component: r.Fact,
});
const Facts = defineComponent({
name: "Facts",
description:
"Short label/value pairs read at a glance, e.g. estimated diff size, risk, scope.",
props: z.object({ items: z.array(Fact.ref) }),
component: r.Facts,
});
const File = defineComponent({
name: "File",
description: "One file and how the change touches it.",
props: z.object({
path: z.string(),
change: z.enum(["added", "modified", "deleted"]),
note: z.string().optional(),
}),
component: r.File,
});
const Files = defineComponent({
name: "Files",
description: "The files a change adds, modifies or deletes.",
props: z.object({ items: z.array(File.ref) }),
component: r.Files,
});
const Checklist = defineComponent({
name: "Checklist",
description: "Criteria or steps, each independently checkable.",
props: z.object({ items: z.array(z.string()) }),
component: r.Checklist,
});
const Table = defineComponent({
name: "Table",
description: "Rows that compare along the same columns.",
props: z.object({
columns: z.array(z.string()),
rows: z.array(z.array(z.string())),
}),
component: r.Table,
});
const Code = defineComponent({
name: "Code",
description: "A code excerpt or proposed signature.",
props: z.object({ code: z.string(), language: z.string().optional() }),
component: r.Code,
});
const block = z.union([
Heading.ref,
Text.ref,
Callout.ref,
Facts.ref,
Files.ref,
Checklist.ref,
Table.ref,
Code.ref,
]);
const Section = defineComponent({
name: "Section",
description: "A titled part of the view, e.g. Problem or Proposed solution.",
props: z.object({ title: z.string(), children: z.array(block) }),
component: r.Section,
});
const Columns = defineComponent({
name: "Columns",
description:
"Two or three sections side by side, for content read in contrast, e.g. goals and non-goals.",
props: z.object({ children: z.array(Section.ref) }),
component: r.Columns,
});
const Stack = defineComponent({
name: "Stack",
description: "The view: its parts from top to bottom.",
props: z.object({
children: z.array(z.union([Section.ref, Columns.ref, block])),
}),
component: r.Stack,
});
return createLibrary({
root: "Stack",
components: [
Stack,
Section,
Columns,
Heading,
Text,
Callout,
Facts,
Fact,
Files,
File,
Checklist,
Table,
Code,
],
componentGroups: [
{ name: "Layout", components: ["Stack", "Section", "Columns"] },
{
name: "Content",
components: [
"Heading",
"Text",
"Callout",
"Facts",
"Fact",
"Files",
"File",
"Checklist",
"Table",
"Code",
],
notes: [
"- Prefer Facts, Files, Checklist and Table to prose; use Text only for explanation.",
"- Define each Section and each list item as its own line so the view streams.",
],
},
],
});
}
export const viewPromptOptions: PromptOptions = {
preamble:
"Besides your text answer, return a view of it with the `view` tool: an OpenUI Lang program the dashboard renders for the human reading your work. It presents the same content as the text, laid out for a quick decision; it never says anything the text does not.",
additionalRules: [
"The view holds only OpenUI Lang: no Markdown fences, no commentary.",
"Lead with what the reader decides on; put supporting detail after it.",
],
examples: [
`root = Stack([facts, problem, solution, scope, risk])
facts = Facts([f1, f2])
f1 = Fact("Estimated diff", "~40 lines")
f2 = Fact("Risk", "Low")
problem = Section("Problem", [problemText])
problemText = Text("Callers compute percentages by hand; \`calc\` has no helper.")
solution = Section("Proposed solution", [solutionText, sig, files])
solutionText = Text("Add one exported function to \`calc/calc.go\`.")
sig = Code("func Percent(part, whole int) (int, error)", "go")
files = Files([file1, file2])
file1 = File("calc/calc.go", "modified", "adds Percent")
file2 = File("calc/calc_test.go", "modified")
scope = Columns([accept, nongoals])
accept = Section("Acceptance criteria", [acceptList])
acceptList = Checklist(["Percent(1, 4) returns 25", "A zero whole returns an error"])
nongoals = Section("Non-goals", [nongoalList])
nongoalList = Checklist(["Floating-point percentages"])
risk = Callout("info", "No existing callers change")`,
],
};