-
Multi-model run
+
{promptBatchDisplay(handle).title}
{handle.batchId}
diff --git a/pkg/cli/webapp/src/PromptDetailPane.test.tsx b/pkg/cli/webapp/src/PromptDetailPane.test.tsx
index 00bf0b0c..00bcf83a 100644
--- a/pkg/cli/webapp/src/PromptDetailPane.test.tsx
+++ b/pkg/cli/webapp/src/PromptDetailPane.test.tsx
@@ -1,4 +1,4 @@
-import { cleanup, render, screen } from "@testing-library/react";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { RuntimePreset, RuntimeProfile } from "@flanksource/clicky-ui/ai";
import { PromptDetailPane } from "./PromptDetailPane";
@@ -86,3 +86,33 @@ describe("PromptDetailPane runtime presets", () => {
expect(runEditorProps[0]).not.toHaveProperty("presets");
});
});
+
+describe("PromptDetailPane fan-out", () => {
+ const source = "---\n# keep this comment\nmodel: api:sonnet\n---\nReview {{item.name}}.\n";
+ const detail = { ...SCRATCH_PROMPT, id: "review", writable: true, content: source };
+
+ it("applies a script and concurrency to the prompt draft", () => {
+ const props = baseProps();
+ render(
);
+
+ fireEvent.change(screen.getByRole("textbox", { name: /^Script/ }), { target: { value: "printf '[]'" } });
+ fireEvent.change(screen.getByLabelText("Concurrent sessions (optional)"), { target: { value: "2" } });
+ fireEvent.click(screen.getByRole("button", { name: "Apply to draft" }));
+
+ expect(props.onDraftChange).toHaveBeenCalledWith(expect.stringMatching(
+ /# keep this comment[\s\S]*fanout:[\s\S]*exec: printf '\[\]'[\s\S]*concurrency: 2[\s\S]*Review \{\{item.name\}\}\./,
+ ));
+ });
+
+ it("keeps invalid fan-out settings out of the draft", () => {
+ const props = baseProps();
+ render(
);
+
+ fireEvent.change(screen.getByLabelText("Concurrent sessions (optional)"), { target: { value: "1.5" } });
+ fireEvent.click(screen.getByRole("button", { name: "Apply to draft" }));
+
+ expect(screen.getByText("Enter a script.")).toBeInTheDocument();
+ expect(screen.getByText("Enter a whole number of at least 1.")).toBeInTheDocument();
+ expect(props.onDraftChange).not.toHaveBeenCalled();
+ });
+});
diff --git a/pkg/cli/webapp/src/PromptDetailPane.tsx b/pkg/cli/webapp/src/PromptDetailPane.tsx
index a68b272e..5e292032 100644
--- a/pkg/cli/webapp/src/PromptDetailPane.tsx
+++ b/pkg/cli/webapp/src/PromptDetailPane.tsx
@@ -25,11 +25,12 @@ import { resolveProvider } from "./promptWorkbenchHelpers";
import { PromptRunStream } from "./PromptRunStream";
import { PromptBatchInspector } from "./PromptBatchInspector";
import { PromptSchemaEditor } from "./PromptSchemaEditor";
+import { PromptFanOutEditor } from "./PromptFanOutEditor";
import { RunningPromptsRunsTab } from "./RunningPrompts";
import { PromptSourceMarkdownEditor } from "./PromptWriteModal";
import type { PromptBatchHandle } from "./hooks/usePromptRunStream";
import type { PromptDetail, PromptPreviewResult } from "./promptData";
-import type { PromptSchemaKind } from "./promptSchemaSource";
+import { promptDeclaresFanOut, type PromptSchemaKind } from "./promptSchemaSource";
import { isScratchPrompt, promptRuntimeForDisplay } from "./promptDetailState";
import {
CAPTAIN_SECRET_SELECTOR,
@@ -38,11 +39,12 @@ import {
type PromptSchemaDoc,
} from "./promptWorkbenchApi";
-export type DetailTab = "source" | "runner" | "schema" | "runs";
+export type DetailTab = "source" | "runner" | "schema" | "runs" | "fanout";
const RUN_TAB = { id: "runner", label: "Run", icon: UiPlay };
const SCHEMA_TAB = { id: "schema", label: "Schema", icon: UiListTree };
const SOURCE_TAB = { id: "source", label: "Source", icon: UiCode2 };
+const FANOUT_TAB = { id: "fanout", label: "Fan-out", icon: UiTerminal };
const RUNS_TAB = { id: "runs", label: "Runs", icon: UiTerminal };
export function PromptDetailPane({
@@ -73,6 +75,7 @@ export function PromptDetailPane({
onResolveProfile,
previewResult,
activeRunID,
+ runNotice,
activeBatch,
onEditBatch,
onSelectRun,
@@ -112,6 +115,7 @@ export function PromptDetailPane({
onResolveProfile?: (request: RuntimeProfileResolveRequest) => Promise
;
previewResult?: PromptPreviewResult;
activeRunID?: string;
+ runNotice?: string;
activeBatch?: PromptBatchHandle;
onEditBatch: () => void;
onSelectRun: (id: string | undefined) => void;
@@ -154,12 +158,13 @@ export function PromptDetailPane({
? [RUN_TAB, RUNS_TAB]
: repair
? [SOURCE_TAB]
- : [RUN_TAB, SCHEMA_TAB, SOURCE_TAB, RUNS_TAB];
+ : [RUN_TAB, SCHEMA_TAB, FANOUT_TAB, SOURCE_TAB, RUNS_TAB];
const activeTab = repair
? "source"
- : scratch && (tab === "source" || tab === "schema")
+ : scratch && tab !== "runner" && tab !== "runs"
? "runner"
: tab;
+ const fanout = !scratch && promptDeclaresFanOut(draft);
const schema = scratch
? undefined
: normalizeObjectSchema(detail.inputSchema);
@@ -225,6 +230,11 @@ export function PromptDetailPane({
onSourceChange={onDraftChange}
onValidityChange={onSchemaValidityChange}
/>
+ ) : activeTab === "fanout" ? (
+
+
onTabChange("source")} />
+ {draftDirty && This prompt has unsaved changes.
}
+
) : (
@@ -263,6 +273,12 @@ export function PromptDetailPane({
: {})}
/>
+ {fanout && (
+
+
Render previews the template. Run fan-out executes the script.
+
+
+ )}
{draftDirty && (
)}
@@ -341,13 +358,18 @@ function SourceEditor({
function RunnerOutput({
previewResult,
activeRunID,
+ runNotice,
}: {
previewResult?: PromptPreviewResult;
activeRunID?: string;
+ runNotice?: string;
}) {
if (activeRunID) {
return
;
}
+ if (runNotice) {
+ return
{runNotice}
;
+ }
if (previewResult) {
return (
diff --git a/pkg/cli/webapp/src/PromptFanOutEditor.tsx b/pkg/cli/webapp/src/PromptFanOutEditor.tsx
new file mode 100644
index 00000000..ede764af
--- /dev/null
+++ b/pkg/cli/webapp/src/PromptFanOutEditor.tsx
@@ -0,0 +1,89 @@
+import { useId, useState } from "react";
+import { Button, Field, InputField } from "@flanksource/clicky-ui/components";
+import { errorMessage } from "./promptWorkbenchApi";
+import { readPromptFanOut, updatePromptFanOutSource } from "./promptSchemaSource";
+
+export function PromptFanOutEditor({ source, onChange, onEditSource }: {
+ source: string;
+ onChange: (source: string) => void;
+ onEditSource: () => void;
+}) {
+ try {
+ return
;
+ } catch (error) {
+ return (
+
+
{errorMessage(error)}
+
+
+ );
+ }
+}
+
+function FanOutFields({ source, initial, onChange }: {
+ source: string;
+ initial: ReturnType
;
+ onChange: (source: string) => void;
+}) {
+ const id = useId();
+ const [script, setScript] = useState(initial?.exec ?? "");
+ const [workers, setWorkers] = useState(String(initial?.concurrency || ""));
+ const [showScriptError, setShowScriptError] = useState(false);
+ const [showWorkersError, setShowWorkersError] = useState(false);
+ const concurrency = Number(workers);
+ const validScript = Boolean(script.trim());
+ const validWorkers = workers === "" || (/^\d+$/.test(workers) && Number.isSafeInteger(concurrency) && concurrency >= 1);
+ const scriptError = showScriptError && !validScript;
+ const workersError = showWorkersError && !validWorkers;
+
+ return (
+
+ );
+}
diff --git a/pkg/cli/webapp/src/PromptWorkbench.tsx b/pkg/cli/webapp/src/PromptWorkbench.tsx
index ab2bdf63..68e3d30a 100644
--- a/pkg/cli/webapp/src/PromptWorkbench.tsx
+++ b/pkg/cli/webapp/src/PromptWorkbench.tsx
@@ -64,6 +64,7 @@ import {
submitPromptOperation,
} from "./promptWorkbenchApi";
import { AGENT_TOOLS } from "./promptAgentTools";
+import { promptDeclaresFanOut } from "./promptSchemaSource";
import { usePromptRuntimeProfiles } from "./runtimeProfilesData";
import { useWhoamiCatalog } from "./whoamiCatalog";
@@ -197,6 +198,7 @@ function usePromptWorkbenchView({
});
const scratch = isScratchPrompt(detail);
const dirty = Boolean(detail) && isPromptDirty(selectedDetailState, detail);
+ const fanout = !scratch && promptDeclaresFanOut(selectedDetailState.draft);
const repair = Boolean(detail?.parseError);
const writableSources = useMemo(
() =>
@@ -248,6 +250,7 @@ function usePromptWorkbenchView({
}
function selectRun(id: string | undefined) {
+ dispatchDetailState({ type: "run-notice", detail });
dispatchDetailState({ type: "active-batch", detail, value: undefined });
dispatchDetailState({ type: "active-run", detail, value: id });
if (id) setTab("runner");
@@ -259,6 +262,7 @@ function usePromptWorkbenchView({
return {
...selectedDetailState.runRequest,
...(dirty ? { content: selectedDetailState.draft } : {}),
+ ...(fanout ? { chat: false } : {}),
};
}
@@ -303,6 +307,7 @@ function usePromptWorkbenchView({
async function previewPrompt() {
if (!detail || !promptOps.preview) return;
+ dispatchDetailState({ type: "run-notice", detail });
dispatchDetailState({ type: "action-error", detail, value: undefined });
dispatchDetailState({ type: "action-loading", detail, value: "preview" });
try {
@@ -327,6 +332,7 @@ function usePromptWorkbenchView({
async function runPrompt() {
if (!detail || !promptOps.run) return;
+ dispatchDetailState({ type: "run-notice", detail });
dispatchDetailState({ type: "action-error", detail, value: undefined });
dispatchDetailState({ type: "action-loading", detail, value: "run" });
try {
@@ -344,8 +350,15 @@ function usePromptWorkbenchView({
dispatchDetailState({
type: "active-run",
detail,
- value: handle.runId,
+ value: "runId" in handle ? handle.runId : undefined,
});
+ if (!("runId" in handle)) {
+ dispatchDetailState({
+ type: "run-notice",
+ detail,
+ value: handle.skipReason ?? (fanout ? "The script returned an empty array. No sessions started." : "The prompt completed without starting a session."),
+ });
+ }
}
setTab("runner");
} catch (error) {
@@ -483,6 +496,7 @@ function usePromptWorkbenchView({
{...runtimeProfiles.editorProps}
previewResult={selectedDetailState.previewResult}
activeRunID={selectedDetailState.activeRunID}
+ runNotice={selectedDetailState.runNotice}
activeBatch={selectedDetailState.activeBatch}
onEditBatch={() => dispatchDetailState({ type: "active-batch", detail, value: undefined })}
onSelectRun={selectRun}
diff --git a/pkg/cli/webapp/src/hooks/usePromptRunStream.ts b/pkg/cli/webapp/src/hooks/usePromptRunStream.ts
index 77b4c454..4fd4477a 100644
--- a/pkg/cli/webapp/src/hooks/usePromptRunStream.ts
+++ b/pkg/cli/webapp/src/hooks/usePromptRunStream.ts
@@ -26,6 +26,7 @@ export interface PromptRunHandle {
export interface PromptBatchRunHandle {
runId: string;
sessionId: string;
+ fanOutIndex?: number;
selector?: string;
status: string;
model?: string;
@@ -44,7 +45,12 @@ export interface PromptBatchHandle {
runs: PromptBatchRunHandle[];
}
-export type PromptExecutionHandle = PromptRunHandle | PromptBatchHandle;
+export interface PromptNoSessionResult {
+ status: "completed" | "skipped";
+ skipReason?: string;
+}
+
+export type PromptExecutionHandle = PromptRunHandle | PromptBatchHandle | PromptNoSessionResult;
export function isPromptBatchHandle(
handle: PromptExecutionHandle,
diff --git a/pkg/cli/webapp/src/promptDetailState.ts b/pkg/cli/webapp/src/promptDetailState.ts
index c0cbb2bc..725765b2 100644
--- a/pkg/cli/webapp/src/promptDetailState.ts
+++ b/pkg/cli/webapp/src/promptDetailState.ts
@@ -48,6 +48,7 @@ export type PromptDetailState = {
previewResult?: PromptPreviewResult;
activeRunID?: string;
activeBatch?: PromptBatchHandle;
+ runNotice?: string;
actionError?: string;
actionLoading?: "save" | "preview" | "run" | "delete";
};
@@ -77,6 +78,7 @@ export type PromptDetailStateAction =
}
| { type: "active-run"; detail?: PromptDetail; value?: string }
| { type: "active-batch"; detail?: PromptDetail; value?: PromptBatchHandle }
+ | { type: "run-notice"; detail?: PromptDetail; value?: string }
| { type: "action-error"; detail?: PromptDetail; value?: string }
| {
type: "action-loading";
@@ -99,9 +101,9 @@ function applyAction(
): PromptDetailState {
switch (action.type) {
case "draft":
- return { ...current, draft: action.value, previewResult: undefined };
+ return { ...current, draft: action.value, previewResult: undefined, runNotice: undefined };
case "run-request":
- return { ...current, runRequest: action.value, previewResult: undefined };
+ return { ...current, runRequest: action.value, previewResult: undefined, runNotice: undefined };
case "variables-validity":
return { ...current, variablesValid: action.value };
case "schema-validity":
@@ -119,6 +121,8 @@ function applyAction(
return { ...current, activeRunID: action.value };
case "active-batch":
return { ...current, activeBatch: action.value };
+ case "run-notice":
+ return { ...current, runNotice: action.value };
case "action-error":
return { ...current, actionError: action.value };
case "action-loading":
diff --git a/pkg/cli/webapp/src/promptSchemaSource.test.ts b/pkg/cli/webapp/src/promptSchemaSource.test.ts
index 3ad190fd..8eb1222c 100644
--- a/pkg/cli/webapp/src/promptSchemaSource.test.ts
+++ b/pkg/cli/webapp/src/promptSchemaSource.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import {
parseSchemaEditorValue,
+ promptDeclaresFanOut,
readPromptSchemas,
updatePromptSchemaSource,
updatePromptSchemaTypeSource,
@@ -25,6 +26,13 @@ output:
Write about {{topic}}.
`;
+describe("promptDeclaresFanOut", () => {
+ it("reads the command declaration only from frontmatter", () => {
+ expect(promptDeclaresFanOut("---\nfanout:\n exec: |\n printf '[]'\n---\nCheck {{item}}.")).toBe(true);
+ expect(promptDeclaresFanOut("fanout:\n exec: |\n printf '[]'")).toBe(false);
+ });
+});
+
describe("updatePromptSchemaSource", () => {
it("updates only the selected prompt-file schema", () => {
const updated = updatePromptSchemaSource(SOURCE, "input", {
diff --git a/pkg/cli/webapp/src/promptSchemaSource.ts b/pkg/cli/webapp/src/promptSchemaSource.ts
index 374554ac..9a614ae3 100644
--- a/pkg/cli/webapp/src/promptSchemaSource.ts
+++ b/pkg/cli/webapp/src/promptSchemaSource.ts
@@ -54,6 +54,37 @@ export function updatePromptSchemaTypeSource(
return `${prefix}---\n${document.toString()}---\n${body}`;
}
+export function promptDeclaresFanOut(source: string) {
+ const document = parseDocument(splitPromptSource(source).frontmatter);
+ return isMap(document.contents) && document.has("fanout");
+}
+
+type PromptFanOut = { exec: string; concurrency?: number };
+
+export function readPromptFanOut(source: string): PromptFanOut | undefined {
+ const document = parsePromptFrontmatter(splitPromptSource(source).frontmatter);
+ if (!document.has("fanout")) return undefined;
+ const exec = document.getIn(["fanout", "exec"]);
+ const concurrency = document.getIn(["fanout", "concurrency"]);
+ if (typeof exec !== "string" || (concurrency !== undefined && typeof concurrency !== "number")) {
+ throw new Error("Fix the fanout.exec or fanout.concurrency declaration in Source.");
+ }
+ return concurrency === undefined ? { exec } : { exec, concurrency };
+}
+
+export function updatePromptFanOutSource(source: string, fanout?: PromptFanOut): string {
+ const { prefix, frontmatter, body } = splitPromptSource(source);
+ const document = parsePromptFrontmatter(frontmatter);
+ if (fanout) {
+ document.setIn(["fanout", "exec"], fanout.exec);
+ if (fanout.concurrency === undefined) document.deleteIn(["fanout", "concurrency"]);
+ else document.setIn(["fanout", "concurrency"], fanout.concurrency);
+ } else {
+ document.delete("fanout");
+ }
+ return `${prefix}---\n${document.toString()}---\n${body}`;
+}
+
export function readPromptSchemas(source: string) {
const document = parsePromptFrontmatter(splitPromptSource(source).frontmatter);
const frontmatter = document.toJS() as Record;
diff --git a/pkg/cli/webapp/src/sessionCollection.test.ts b/pkg/cli/webapp/src/sessionCollection.test.ts
index bf77e82d..9f316738 100644
--- a/pkg/cli/webapp/src/sessionCollection.test.ts
+++ b/pkg/cli/webapp/src/sessionCollection.test.ts
@@ -40,6 +40,19 @@ const HANDLE: PromptBatchHandle = {
};
describe("batch session collection", () => {
+ it("identifies a command fan-out before the batch session has loaded", () => {
+ const collection = batchSessionCollection({
+ ...HANDLE,
+ total: 1,
+ runs: [{ runId: "run-1", sessionId: "session-1", status: "running", fanOutIndex: 1, selector: "item 1" }],
+ });
+
+ expect(collection.sessions).toMatchObject([
+ { label: "Command fan-out", summary: { provider: "fan-out" } },
+ { label: "item 1", parentId: "batch-1" },
+ ]);
+ });
+
it("selects every direct model session by default", () => {
const collection = batchSessionCollection(HANDLE);
diff --git a/pkg/cli/webapp/src/sessionCollection.ts b/pkg/cli/webapp/src/sessionCollection.ts
index b7a0591d..48aae4dc 100644
--- a/pkg/cli/webapp/src/sessionCollection.ts
+++ b/pkg/cli/webapp/src/sessionCollection.ts
@@ -34,15 +34,16 @@ export function batchSessionCollection(
result?.sessions.map((item) => [item.captainId, item]) ?? [],
);
const root = persisted.get(handle.batchId);
+ const { title, provider } = promptBatchDisplay(handle);
const sessions: SessionCollectionItem[] = [
root
? sessionCollectionItem(root)
: {
id: handle.batchId,
- label: "Multi-model run",
+ label: title,
status: handle.status,
- summary: { provider: "multi-model", status: handle.status },
- session: emptySession(handle.batchId, "captain", "Multi-model run"),
+ summary: { provider, status: handle.status },
+ session: emptySession(handle.batchId, "captain", title),
},
...handle.runs.map((run) => {
const saved = persisted.get(run.sessionId);
@@ -79,6 +80,12 @@ export function batchSessionCollection(
};
}
+export function promptBatchDisplay(handle: PromptBatchHandle) {
+ return handle.runs.some((run) => run.fanOutIndex !== undefined)
+ ? { title: "Command fan-out", provider: "fan-out" }
+ : { title: "Multi-model run", provider: "multi-model" };
+}
+
export function batchChatTargets(handle: PromptBatchHandle) {
return handle.runs.filter(
(run) =>