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 a268877..877a89f 100644 --- a/src/renderer/components/agents/AgentEditor.tsx +++ b/src/renderer/components/agents/AgentEditor.tsx @@ -174,7 +174,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') @@ -438,6 +438,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' && ( + + )} + +