cu-backend-io.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import yaml from 'js-yaml';
  4. import { writePrivateConfigFileSync } from './private-config-file.js';
  5. import { getConfig, getConfigPath, resetConfig } from './config.js';
  6. import {
  7. type CuBackend,
  8. DEFAULT_CU_BACKEND,
  9. assertCuBackendSupported as assertCuBackendSupportedPure,
  10. parseCuBackend,
  11. } from './cu-backend.js';
  12. /** Set of accepted backend identifiers used for input validation. */
  13. const VALID_BACKENDS: ReadonlySet<CuBackend> = new Set(['native', 'mcp']);
  14. /**
  15. * Read the resolved Computer Use backend from `config.yaml`.
  16. *
  17. * Goes through the central {@link getConfig} parser so the resolved value
  18. * always matches what the rest of the runtime sees.
  19. */
  20. export function getCuBackend(): CuBackend {
  21. return parseCuBackend((getConfig() as { cuBackend?: unknown }).cuBackend);
  22. }
  23. /**
  24. * Persist the Computer Use backend selection to `config.yaml` and reset the
  25. * cached `Config` so the next {@link getCuBackend} call observes the change.
  26. *
  27. * NOTE: This writer performs a non-atomic read-modify-write and is **not**
  28. * lockfile-protected. Concurrent mutations should go through the runtime
  29. * config writer/facade so hosts can deep-merge, lock, and emit
  30. * `config.updated`. This helper exists for one-shot CLI / test paths that do
  31. * not need that machinery.
  32. */
  33. export function setCuBackend(value: CuBackend): void {
  34. if (!VALID_BACKENDS.has(value)) {
  35. throw new Error(`invalid cuBackend value: ${String(value)}`);
  36. }
  37. const configPath = getConfigPath();
  38. let raw: Record<string, unknown> = {};
  39. try {
  40. const text = fs.readFileSync(configPath, 'utf-8');
  41. const parsed = yaml.load(text);
  42. if (parsed != null && typeof parsed === 'object' && !Array.isArray(parsed)) {
  43. raw = parsed as Record<string, unknown>;
  44. }
  45. } catch {
  46. // Missing / unreadable file → treat as empty so we still write the field.
  47. }
  48. raw.cuBackend = value;
  49. fs.mkdirSync(path.dirname(configPath), { recursive: true });
  50. writePrivateConfigFileSync(configPath, yaml.dump(raw));
  51. resetConfig();
  52. }
  53. /**
  54. * Daemon-side wrapper around the pure
  55. * {@link assertCuBackendSupportedPure} guard. Defaults to {@link getCuBackend}
  56. * when the caller does not pass an explicit backend, sparing them an
  57. * extra config lookup at the call site.
  58. *
  59. * Renderer code should keep using the pure variant from
  60. * `@mavis/config/cu-backend` and feed it the value from the renderer-side
  61. * cache instead of touching `config.yaml`.
  62. */
  63. export function assertCuBackendSupported(backend: CuBackend = getCuBackend()): void {
  64. assertCuBackendSupportedPure(backend);
  65. }
  66. // Re-export the pure surface for callers that want a single import target.
  67. export { type CuBackend, DEFAULT_CU_BACKEND, parseCuBackend };