From e65c8d66be0295a03593c7f223d4f5957b253188 Mon Sep 17 00:00:00 2001 From: Doug Rathbone Date: Wed, 2 Sep 2026 22:00:37 +1000 Subject: [PATCH 1/5] Add Conduit-wide prompt instructions and files (#3) * feat: add Conduit-wide prompt instructions and files Operators can create instructions (prepended to every agent prompt) and files (written into each run workspace and inlined in the prompt) that apply to every run, with the same ownership/sharing model as global MCPs. Co-authored-by: Doug Rathbone * Move prompt components into Settings Drop the sidebar nav item and standalone /prompt-components page. Add and edit Conduit-wide instructions and files from Settings instead. The old route still opens Settings so existing links keep working. Co-authored-by: Doug Rathbone --------- Co-authored-by: Cursor Agent Co-authored-by: Doug Rathbone --- CLAUDE.md | 8 + README.md | 1 + src/main/db/index.ts | 12 + src/main/db/queries/access.ts | 1 + src/main/db/queries/globalPromptComponents.ts | 143 +++++++ src/main/db/schema.ts | 12 + .../components/agents/AgentEditor.tsx | 12 +- .../settings/PromptComponentManager.tsx | 397 ++++++++++++++++++ .../components/settings/SettingsManager.tsx | 9 +- .../hooks/useGlobalPromptComponents.ts | 49 +++ src/renderer/hooks/useShares.ts | 2 + src/renderer/lib/ipc.ts | 3 + src/renderer/lib/ws-client.ts | 12 + src/renderer/store/ui.ts | 29 +- src/server/index.ts | 48 +++ src/server/runner.ts | 7 +- src/server/workers/localWorker.ts | 4 + src/server/workspaceFiles.test.ts | 43 ++ src/server/workspaceFiles.ts | 36 ++ src/shared/promptComponents.test.ts | 87 ++++ src/shared/promptComponents.ts | 118 ++++++ src/shared/types.ts | 38 +- src/shared/worker.ts | 6 + 23 files changed, 1065 insertions(+), 12 deletions(-) create mode 100644 src/main/db/queries/globalPromptComponents.ts create mode 100644 src/renderer/components/settings/PromptComponentManager.tsx create mode 100644 src/renderer/hooks/useGlobalPromptComponents.ts create mode 100644 src/server/workspaceFiles.test.ts create mode 100644 src/server/workspaceFiles.ts create mode 100644 src/shared/promptComponents.test.ts create mode 100644 src/shared/promptComponents.ts diff --git a/CLAUDE.md b/CLAUDE.md index c61b846..fd398f2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -225,6 +225,14 @@ per-run configs post-run plus a startup sweep (6h grace). Runs record any workspace/config they created when `startRun` throws; `fixedDir` workspaces are local-only. +## Conduit-wide prompt components + +Enabled rows in `global_prompt_components` are applied to **every** run (same +injection model as global MCPs — sharing only controls who can see/edit them +in the UI, under **Settings → Prompt components**). Instructions are prepended to the prompt; files are written into +the materialized workspace (`RunSpec.workspaceFiles`) and also inlined in the +prompt. Path validation lives in `src/shared/promptComponents.ts`. + ## Key Patterns **Adding a new entity type** — follow the publish targets pattern: diff --git a/README.md b/README.md index 22e6f0f..d77fb60 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ A self-hosted web app for managing and running AI CLI agents (Claude Code, Amp, - **Multi-runner** — Claude Code, Amp, and Cursor agents - **Live terminal streaming** — xterm.js with full ANSI colour output - **MCP management** — global and per-agent servers with OAuth support and health indicators +- **Prompt components** — Conduit-wide instructions and files prepended to every agent run (Settings) - **Pluggable execution** — run agents in-process, on remote workers, or as ephemeral EKS Jobs / Fargate tasks - **Scheduled runs** — cron triggers per agent - **Publish targets** — deliver agent output to Slack, email (SMTP), or signed webhooks diff --git a/src/main/db/index.ts b/src/main/db/index.ts index 453c894..e44195a 100644 --- a/src/main/db/index.ts +++ b/src/main/db/index.ts @@ -164,6 +164,18 @@ export async function initDb(): Promise { updated_at BIGINT NOT NULL ); + CREATE TABLE IF NOT EXISTS global_prompt_components ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + kind TEXT NOT NULL, + content TEXT NOT NULL DEFAULT '', + file_path TEXT, + enabled BOOLEAN NOT NULL DEFAULT TRUE, + owner_id TEXT, + created_at BIGINT NOT NULL, + updated_at BIGINT NOT NULL + ); + CREATE TABLE IF NOT EXISTS runs ( id TEXT PRIMARY KEY, agent_id TEXT NOT NULL REFERENCES agents(id), diff --git a/src/main/db/queries/access.ts b/src/main/db/queries/access.ts index dd6b368..8bac246 100644 --- a/src/main/db/queries/access.ts +++ b/src/main/db/queries/access.ts @@ -7,6 +7,7 @@ const TABLE_MAP: Record = { publishTarget: 'publish_targets', repository: 'repositories', globalMcpServer: 'global_mcp_servers', + globalPromptComponent: 'global_prompt_components', } // Rewrites `?` placeholders to pg-style `$1..$n` in positional order. diff --git a/src/main/db/queries/globalPromptComponents.ts b/src/main/db/queries/globalPromptComponents.ts new file mode 100644 index 0000000..afd1a82 --- /dev/null +++ b/src/main/db/queries/globalPromptComponents.ts @@ -0,0 +1,143 @@ +import { eq } from 'drizzle-orm' +import { getDb } from '../index' +import { globalPromptComponents } from '../schema' +import { getVisibleEntityIds } from './access' +import { deleteSharesForEntity } from './shares' +import type { GlobalPromptComponent, GlobalPromptComponentKind } from '../../../shared/types' +import { validatePromptComponentInput } from '../../../shared/promptComponents' + +function rowToComponent(row: typeof globalPromptComponents.$inferSelect): GlobalPromptComponent { + return { + id: row.id, + name: row.name, + kind: row.kind as GlobalPromptComponentKind, + content: row.content, + filePath: row.filePath ?? undefined, + enabled: row.enabled, + ownerId: row.ownerId ?? undefined, + createdAt: row.createdAt, + updatedAt: row.updatedAt, + } +} + +async function findFilePathConflict(filePath: string, excludeId?: string): Promise { + const rows = await getDb().select().from(globalPromptComponents) + const hit = rows.find((r) => r.kind === 'file' && r.filePath === filePath && r.id !== excludeId) + return hit ? hit.name : null +} + +export async function listGlobalPromptComponents( + userId: string, + userGroupIds: string[] +): Promise { + const visibleIds = await getVisibleEntityIds('globalPromptComponent', userId, userGroupIds) + if (visibleIds.length === 0) return [] + const rows = await getDb().select().from(globalPromptComponents) + return rows.filter((r) => visibleIds.includes(r.id)).map(rowToComponent) +} + +/** All enabled components — injected into every run regardless of sharing. */ +export async function listEnabledGlobalPromptComponents(): Promise { + const rows = await getDb() + .select() + .from(globalPromptComponents) + .where(eq(globalPromptComponents.enabled, true)) + return rows.map(rowToComponent) +} + +export async function getGlobalPromptComponent(id: string): Promise { + const rows = await getDb() + .select() + .from(globalPromptComponents) + .where(eq(globalPromptComponents.id, id)) + return rows.length ? rowToComponent(rows[0]) : null +} + +export async function createGlobalPromptComponent( + data: Omit, + ownerId: string +): Promise { + const validated = validatePromptComponentInput(data) + if (validated.kind === 'file' && validated.filePath) { + const conflict = await findFilePathConflict(validated.filePath) + if (conflict) { + throw new Error( + `A file component already writes "${validated.filePath}" (component "${conflict}"). File paths must be unique.` + ) + } + } + + const now = Date.now() + const id = crypto.randomUUID() + await getDb().insert(globalPromptComponents).values({ + id, + name: data.name.trim(), + kind: validated.kind, + content: data.content ?? '', + filePath: validated.kind === 'file' ? validated.filePath : null, + enabled: data.enabled, + ownerId, + createdAt: now, + updatedAt: now, + }) + + const rows = await getDb() + .select() + .from(globalPromptComponents) + .where(eq(globalPromptComponents.id, id)) + if (rows.length === 0) throw new Error(`Failed to create prompt component with id ${id}`) + return rowToComponent(rows[0]) +} + +export async function updateGlobalPromptComponent( + id: string, + data: Partial> +): Promise { + const existing = await getGlobalPromptComponent(id) + if (!existing) throw new Error(`Prompt component with id ${id} not found`) + + const merged = { + name: data.name ?? existing.name, + kind: data.kind ?? existing.kind, + content: data.content ?? existing.content, + filePath: data.filePath !== undefined ? data.filePath : existing.filePath, + enabled: data.enabled ?? existing.enabled, + } + const validated = validatePromptComponentInput(merged) + if (validated.kind === 'file' && validated.filePath) { + const conflict = await findFilePathConflict(validated.filePath, id) + if (conflict) { + throw new Error( + `A file component already writes "${validated.filePath}" (component "${conflict}"). File paths must be unique.` + ) + } + } + + await getDb() + .update(globalPromptComponents) + .set({ + name: merged.name.trim(), + kind: validated.kind, + content: merged.content, + filePath: validated.kind === 'file' ? validated.filePath : null, + enabled: merged.enabled, + updatedAt: Date.now(), + }) + .where(eq(globalPromptComponents.id, id)) + + const rows = await getDb() + .select() + .from(globalPromptComponents) + .where(eq(globalPromptComponents.id, id)) + if (rows.length === 0) throw new Error(`Prompt component with id ${id} not found after update`) + return rowToComponent(rows[0]) +} + +export async function deleteGlobalPromptComponent(id: string): Promise { + await deleteSharesForEntity('globalPromptComponent', id) + const deleted = await getDb() + .delete(globalPromptComponents) + .where(eq(globalPromptComponents.id, id)) + .returning({ id: globalPromptComponents.id }) + return deleted.length +} diff --git a/src/main/db/schema.ts b/src/main/db/schema.ts index 696dcc7..a497879 100644 --- a/src/main/db/schema.ts +++ b/src/main/db/schema.ts @@ -160,6 +160,18 @@ export const globalMcpServers = pgTable('global_mcp_servers', { updatedAt: bigint('updated_at', { mode: 'number' }).notNull(), }) +export const globalPromptComponents = pgTable('global_prompt_components', { + id: text('id').primaryKey(), + name: text('name').notNull(), + kind: text('kind').notNull(), + content: text('content').notNull().default(''), + filePath: text('file_path'), + enabled: boolean('enabled').notNull().default(true), + ownerId: text('owner_id'), + createdAt: bigint('created_at', { mode: 'number' }).notNull(), + updatedAt: bigint('updated_at', { mode: 'number' }).notNull(), +}) + export const publishTargets = pgTable('publish_targets', { id: text('id').primaryKey(), name: text('name').notNull(), diff --git a/src/renderer/components/agents/AgentEditor.tsx b/src/renderer/components/agents/AgentEditor.tsx index 850b878..094c0c5 100644 --- a/src/renderer/components/agents/AgentEditor.tsx +++ b/src/renderer/components/agents/AgentEditor.tsx @@ -176,7 +176,7 @@ export const AgentEditor = forwardRef(funct const { data: allRepos = [] } = useRepositories() const { data: triggers = [] } = useTriggers(agentId) useRepoSyncEvents() - const { setShowPublishTargets, setShowRepositories } = useUIStore() + const { setShowPublishTargets, setShowRepositories, setShowSettings } = useUIStore() const [draft, setDraft] = useState>({}) const [saveState, setSaveState] = useState('idle') @@ -422,6 +422,16 @@ export const AgentEditor = forwardRef(funct {/* Prompt — the heart of the agent */} +

+ Conduit-wide instructions and files are prepended automatically.{' '} + +

handleChange('prompt', v)} diff --git a/src/renderer/components/settings/PromptComponentManager.tsx b/src/renderer/components/settings/PromptComponentManager.tsx new file mode 100644 index 0000000..7fd26ef --- /dev/null +++ b/src/renderer/components/settings/PromptComponentManager.tsx @@ -0,0 +1,397 @@ +import React, { useState } from 'react' +import { Plus, Pencil, Trash2, Info, Loader2, X, Check, Share2, FileText, ScrollText } from 'lucide-react' +import { Button } from '@renderer/components/ui/button' +import { Input } from '@renderer/components/ui/input' +import { ShareDialog } from '@renderer/components/ShareDialog' +import { + useGlobalPromptComponents, + useCreateGlobalPromptComponent, + useUpdateGlobalPromptComponent, + useDeleteGlobalPromptComponent, +} from '@renderer/hooks/useGlobalPromptComponents' +import { useAuth } from '@renderer/contexts/AuthContext' +import { useToast } from '@renderer/contexts/ToastContext' +import { cn } from '@renderer/lib/utils' +import type { GlobalPromptComponent, GlobalPromptComponentKind } from '@shared/types' + +interface FormState { + name: string + kind: GlobalPromptComponentKind + content: string + filePath: string + enabled: boolean +} + +const emptyForm = (): FormState => ({ + name: '', + kind: 'instruction', + content: '', + filePath: '', + enabled: true, +}) + +function formFrom(item: GlobalPromptComponent): FormState { + return { + name: item.name, + kind: item.kind, + content: item.content, + filePath: item.filePath ?? '', + enabled: item.enabled, + } +} + +function ComponentForm({ + initial, + onSave, + onClose, + saving, +}: { + initial: FormState + onSave: (values: FormState) => Promise<{ ok: boolean; error?: string }> + onClose: () => void + saving: boolean +}) { + const [form, setForm] = useState(initial) + const [error, setError] = useState(null) + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault() + setError(null) + const result = await onSave(form) + if (result.ok) onClose() + else setError(result.error ?? 'Save failed') + } + + return ( +
+
+ + {initial.name ? 'Edit component' : 'New component'} + + +
+ +
+ + +
+ + {form.kind === 'file' && ( + + )} + +