opencode-config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import type {
  2. OpenCodeAdapterConfig,
  3. OpenCodeKeepAliveConfig,
  4. OpenCodeStartupImportMode,
  5. OpenCodeXdgConfig,
  6. } from './config.js';
  7. export interface OpenCodeAdapterDefaults {
  8. keepAlive: OpenCodeKeepAliveConfig;
  9. xdg: OpenCodeXdgConfig;
  10. spawnConcurrency: number;
  11. }
  12. /**
  13. * Parse the legacy `opencode` adapter section of `config.yaml`.
  14. *
  15. * New local sessions no longer use this path; the types stay exported because
  16. * the daemon package is still in the workspace compile surface while existing
  17. * opencode sessions are kept resumable through the legacy boundary.
  18. */
  19. export function parseOpenCodeAdapterConfig(
  20. raw: Record<string, unknown>,
  21. defaults: OpenCodeAdapterDefaults,
  22. ): OpenCodeAdapterConfig {
  23. const defaultKeepAlive = defaults.keepAlive;
  24. const defaultXdg = defaults.xdg;
  25. const defaultSpawnConcurrency = defaults.spawnConcurrency;
  26. const cloneDefault = (): OpenCodeKeepAliveConfig => ({
  27. enabled: defaultKeepAlive.enabled,
  28. maxProcesses: defaultKeepAlive.maxProcesses,
  29. maxLifetimeMs: defaultKeepAlive.maxLifetimeMs,
  30. agents: [...defaultKeepAlive.agents],
  31. });
  32. const rawOc = raw.opencode;
  33. if (rawOc == null || typeof rawOc !== 'object' || Array.isArray(rawOc)) {
  34. return {
  35. keepAlive: cloneDefault(),
  36. xdg: { ...defaultXdg },
  37. spawnConcurrency: defaultSpawnConcurrency,
  38. };
  39. }
  40. const ocObj = rawOc as Record<string, unknown>;
  41. const rawSc = ocObj.spawnConcurrency;
  42. const spawnConcurrency =
  43. typeof rawSc === 'number' && Number.isFinite(rawSc) && rawSc > 0 && Number.isInteger(rawSc)
  44. ? rawSc
  45. : defaultSpawnConcurrency;
  46. const xdg = parseXdgConfig(ocObj.xdg, defaultXdg);
  47. const rawKa = ocObj.keepAlive;
  48. if (rawKa == null || typeof rawKa !== 'object' || Array.isArray(rawKa)) {
  49. return { keepAlive: cloneDefault(), xdg, spawnConcurrency };
  50. }
  51. const ka = rawKa as Record<string, unknown>;
  52. const rawMaxP = ka.maxProcesses;
  53. const maxProcesses =
  54. typeof rawMaxP === 'number' &&
  55. Number.isFinite(rawMaxP) &&
  56. rawMaxP > 0 &&
  57. Number.isInteger(rawMaxP)
  58. ? rawMaxP
  59. : defaultKeepAlive.maxProcesses;
  60. const rawMaxL = ka.maxLifetimeMs;
  61. const maxLifetimeMs =
  62. typeof rawMaxL === 'number' &&
  63. Number.isFinite(rawMaxL) &&
  64. rawMaxL > 0 &&
  65. Number.isInteger(rawMaxL)
  66. ? rawMaxL
  67. : defaultKeepAlive.maxLifetimeMs;
  68. const rawAgents = ka.agents;
  69. let agents: string[];
  70. if (Array.isArray(rawAgents)) {
  71. agents = rawAgents.filter(
  72. (agent): agent is string => typeof agent === 'string' && agent.length > 0,
  73. );
  74. if (agents.length === 0) agents = [...defaultKeepAlive.agents];
  75. } else {
  76. agents = [...defaultKeepAlive.agents];
  77. }
  78. return {
  79. keepAlive: {
  80. enabled: typeof ka.enabled === 'boolean' ? ka.enabled : defaultKeepAlive.enabled,
  81. maxProcesses,
  82. maxLifetimeMs,
  83. agents,
  84. },
  85. xdg,
  86. spawnConcurrency,
  87. };
  88. }
  89. function parseXdgConfig(raw: unknown, defaults: OpenCodeXdgConfig): OpenCodeXdgConfig {
  90. const xdgObj =
  91. raw != null && typeof raw === 'object' && !Array.isArray(raw)
  92. ? (raw as Record<string, unknown>)
  93. : undefined;
  94. const rawStartupImport = xdgObj?.startupImport;
  95. const startupImport: OpenCodeStartupImportMode =
  96. rawStartupImport === 'background' ||
  97. rawStartupImport === 'blocking' ||
  98. rawStartupImport === 'off'
  99. ? rawStartupImport
  100. : defaults.startupImport;
  101. return {
  102. dataIsolation:
  103. typeof xdgObj?.dataIsolation === 'boolean' ? xdgObj.dataIsolation : defaults.dataIsolation,
  104. startupImport,
  105. };
  106. }