12-full-control.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Full Control
  3. *
  4. * Replace everything - no discovery, explicit configuration.
  5. */
  6. import { getModel } from "@earendil-works/pi-ai";
  7. import {
  8. AuthStorage,
  9. createAgentSession,
  10. createExtensionRuntime,
  11. ModelRegistry,
  12. type ResourceLoader,
  13. SessionManager,
  14. SettingsManager,
  15. } from "@earendil-works/pi-coding-agent";
  16. // Custom auth storage location
  17. const authStorage = AuthStorage.create("/tmp/my-agent/auth.json");
  18. // Runtime API key override (not persisted)
  19. if (process.env.MY_ANTHROPIC_KEY) {
  20. authStorage.setRuntimeApiKey("anthropic", process.env.MY_ANTHROPIC_KEY);
  21. }
  22. // Model registry with no custom models.json
  23. const modelRegistry = ModelRegistry.inMemory(authStorage);
  24. const model = getModel("anthropic", "claude-sonnet-4-20250514");
  25. if (!model) throw new Error("Model not found");
  26. // In-memory settings with overrides
  27. const settingsManager = SettingsManager.inMemory({
  28. compaction: { enabled: false },
  29. retry: { enabled: true, maxRetries: 2 },
  30. });
  31. const cwd = process.cwd();
  32. const resourceLoader: ResourceLoader = {
  33. getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
  34. getSkills: () => ({ skills: [], diagnostics: [] }),
  35. getPrompts: () => ({ prompts: [], diagnostics: [] }),
  36. getThemes: () => ({ themes: [], diagnostics: [] }),
  37. getAgentsFiles: () => ({ agentsFiles: [] }),
  38. getSystemPrompt: () => `You are a minimal assistant.
  39. Available: read, bash. Be concise.`,
  40. getAppendSystemPrompt: () => [],
  41. extendResources: () => {},
  42. reload: async () => {},
  43. };
  44. const { session } = await createAgentSession({
  45. cwd,
  46. agentDir: "/tmp/my-agent",
  47. model,
  48. thinkingLevel: "off",
  49. authStorage,
  50. modelRegistry,
  51. resourceLoader,
  52. tools: ["read", "bash"],
  53. sessionManager: SessionManager.inMemory(cwd),
  54. settingsManager,
  55. });
  56. try {
  57. session.subscribe((event) => {
  58. if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
  59. process.stdout.write(event.assistantMessageEvent.delta);
  60. }
  61. });
  62. await session.prompt("List files in the current directory.");
  63. console.log();
  64. } finally {
  65. session.dispose();
  66. }