08-prompt-templates.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Prompt Templates
  3. *
  4. * File-based templates that inject content when invoked with /templatename.
  5. */
  6. import {
  7. createAgentSession,
  8. createSyntheticSourceInfo,
  9. DefaultResourceLoader,
  10. getAgentDir,
  11. type PromptTemplate,
  12. SessionManager,
  13. } from "@earendil-works/pi-coding-agent";
  14. // Define custom templates
  15. const deployTemplate: PromptTemplate = {
  16. name: "deploy",
  17. description: "Deploy the application",
  18. filePath: "/virtual/prompts/deploy.md",
  19. sourceInfo: createSyntheticSourceInfo("/virtual/prompts/deploy.md", { source: "sdk" }),
  20. content: `# Deploy Instructions
  21. 1. Build: npm run build
  22. 2. Test: npm test
  23. 3. Deploy: npm run deploy`,
  24. };
  25. const loader = new DefaultResourceLoader({
  26. cwd: process.cwd(),
  27. agentDir: getAgentDir(),
  28. promptsOverride: (current) => ({
  29. prompts: [...current.prompts, deployTemplate],
  30. diagnostics: current.diagnostics,
  31. }),
  32. });
  33. await loader.reload();
  34. // Discover templates from cwd/.pi/prompts/ and ~/.pi/agent/prompts/
  35. const discovered = loader.getPrompts().prompts;
  36. console.log("Discovered prompt templates:");
  37. for (const template of discovered) {
  38. console.log(` /${template.name}: ${template.description}`);
  39. }
  40. const { session } = await createAgentSession({
  41. resourceLoader: loader,
  42. sessionManager: SessionManager.inMemory(),
  43. });
  44. console.log(`Session created with ${discovered.length + 1} prompt templates`);
  45. session.dispose();