agent-runtime-config.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Agent runtime selection config — pure type-level module.
  3. *
  4. * Defines the local runtime knob for freshly-created sessions. New local
  5. * sessions run through Pi, while any stale session metadata is handled as
  6. * archive-only by the local host.
  7. *
  8. * Intentionally has **zero** Node-only imports so this file can be consumed
  9. * unchanged across the workspace. Config sits beneath the local/cloud runtime
  10. * packages in the dependency graph, so keep this module as plain data parsing.
  11. *
  12. * Keep this module Node-free so Electron, CLI, and local runtime can share it.
  13. */
  14. /**
  15. * New local sessions have one live runtime: Pi. Existing opencode sessions
  16. * keep their persisted runtime and route through the legacy boundary instead
  17. * of this config knob.
  18. */
  19. export type AgentRuntimeFramework = 'pi-agent';
  20. /** All recognised config framework strings. Used by `parseAgentRuntimeFramework`. */
  21. export const AGENT_RUNTIME_FRAMEWORKS: ReadonlyArray<AgentRuntimeFramework> = ['pi-agent'];
  22. /**
  23. * Default runtime used when `config.yaml` omits `agentRuntime` or sets an
  24. * unrecognised value.
  25. */
  26. export const DEFAULT_AGENT_RUNTIME_FRAMEWORK: AgentRuntimeFramework = 'pi-agent';
  27. /**
  28. * User-facing config:
  29. *
  30. * ```yaml
  31. * agentRuntime:
  32. * defaultFramework: pi-agent
  33. * ```
  34. *
  35. * Only affects newly-created local Pi sessions. Unknown values are treated as
  36. * stale config and normalized to Pi.
  37. */
  38. export interface AgentRuntimeConfig {
  39. /** Default runtime for new root / no-parent sessions. */
  40. defaultFramework: AgentRuntimeFramework;
  41. }
  42. /**
  43. * Coerce an unknown raw value (typically a string from `config.yaml`) into
  44. * an {@link AgentRuntimeFramework}. Returns
  45. * {@link DEFAULT_AGENT_RUNTIME_FRAMEWORK} for anything that is not one of
  46. * {@link AGENT_RUNTIME_FRAMEWORKS}.
  47. *
  48. * A typo (`'pi'` instead of `'pi-agent'`) falls back to Pi instead of creating
  49. * another runtime mode.
  50. */
  51. export function parseAgentRuntimeFramework(raw: unknown): AgentRuntimeFramework {
  52. if (typeof raw !== 'string') return DEFAULT_AGENT_RUNTIME_FRAMEWORK;
  53. if ((AGENT_RUNTIME_FRAMEWORKS as readonly string[]).includes(raw)) {
  54. return raw as AgentRuntimeFramework;
  55. }
  56. return DEFAULT_AGENT_RUNTIME_FRAMEWORK;
  57. }
  58. /**
  59. * Parse the `agentRuntime` section of a raw `config.yaml`. Always returns a
  60. * fully-populated config — missing / malformed fields fall back to defaults.
  61. */
  62. export function parseAgentRuntimeConfig(raw: unknown): AgentRuntimeConfig {
  63. if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) {
  64. return { defaultFramework: DEFAULT_AGENT_RUNTIME_FRAMEWORK };
  65. }
  66. const obj = raw as Record<string, unknown>;
  67. return {
  68. defaultFramework: parseAgentRuntimeFramework(obj.defaultFramework),
  69. };
  70. }