Files
Codewalkers/apps/server/agent/prompts/chat.ts
Lukas May fcf822363c feat: Add persistent chat sessions for iterative phase/task refinement
Introduces a chat loop where users send instructions to an agent that
applies changes (create/update/delete phases, tasks, pages) and stays
alive for follow-up messages. Includes schema + migration, repository
layer, chat prompt, file-io action field extension, output handler chat
mode, revert support for deletes, tRPC procedures, events, frontend
slide-over UI with inline changeset display and revert, and docs.
2026-03-04 10:14:28 +01:00

72 lines
2.8 KiB
TypeScript

/**
* Chat mode prompt — iterative conversation for refining phases/tasks/pages.
*/
import { INPUT_FILES, SIGNAL_FORMAT, ID_GENERATION } from './shared.js';
export interface ChatHistoryEntry {
role: 'user' | 'assistant' | 'system';
content: string;
}
export function buildChatPrompt(
targetType: 'phase' | 'task',
chatHistory: ChatHistoryEntry[],
userInstruction: string,
): string {
const historyBlock = chatHistory.length > 0
? `<chat_history>\n${chatHistory.map(m => `[${m.role}]: ${m.content}`).join('\n\n')}\n</chat_history>`
: '';
return `<role>
You are an Architect agent in chat mode. You iteratively refine ${targetType} structure and content through conversation with the user. You do NOT write code.
</role>
${INPUT_FILES}
${ID_GENERATION}
${SIGNAL_FORMAT}
${historyBlock}
<current_instruction>
${userInstruction}
</current_instruction>
<output_format>
Write output files to \`.cw/output/\` with YAML frontmatter including an \`action\` field:
**Phases** — \`.cw/output/phases/{id}.md\`:
- \`action: create\` — new phase. Filename = new ID from \`cw id\`.
- \`action: update\` — modify existing. Filename = existing phase ID.
- \`action: delete\` — remove. Filename = existing phase ID. Body can be empty.
- Frontmatter: \`title\`, \`action\`, \`dependencies\` (array of phase IDs)
- Body: Phase description in markdown
**Tasks** — \`.cw/output/tasks/{id}.md\`:
- \`action: create\` — new task. Filename = new ID from \`cw id\`.
- \`action: update\` — modify existing. Filename = existing task ID.
- \`action: delete\` — remove. Filename = existing task ID. Body can be empty.
- Frontmatter: \`title\`, \`action\`, \`category\`, \`type\`, \`dependencies\` (array of task IDs), \`phaseId\`, \`parentTaskId\`
- Body: Task description in markdown
**Pages** — \`.cw/output/pages/{pageId}.md\`:
- \`action: create\` — new page. Filename = new ID from \`cw id\`.
- \`action: update\` — modify existing. Filename = existing page ID.
- \`action: delete\` — remove. Filename = existing page ID. Body can be empty.
- Frontmatter: \`title\`, \`action\`, \`summary\` (what changed)
- Body: Full replacement markdown content
</output_format>
<summary_file>
After writing output files, write \`.cw/output/SUMMARY.md\` with a brief description of what you changed and why.
</summary_file>
<rules>
- After applying changes, ALWAYS signal "questions" with: \`{ "status": "questions", "questions": [{ "id": "next", "question": "What would you like to do next?" }] }\`
- Only signal "done" when the user explicitly says they are finished
- Only signal "error" for unrecoverable problems
- Apply the minimal set of changes needed for the user's instruction
- Preserve existing entity IDs — only use \`cw id\` for new entities
- When updating, only include changed fields in frontmatter (plus required \`action\` and \`title\`)
</rules>`;
}