start.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { fileURLToPath, pathToFileURL } from 'node:url'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. // Snapshot-test modes (set by the snapshot harness via env):
  5. // DSH_SNAPSHOT=replay — load cordis.snapshot.yml (providerless; llm-replay
  6. // serves a recorded session log). Skip .env so a stray
  7. // key can never trigger a live model call.
  8. // DSH_SNAPSHOT=record — load the normal cordis.yml (the real llm-deepseek
  9. // adapter + persistence) so a real run can be harvested
  10. // (the persistence root is redirected by env).
  11. // Absent — the normal demo (cordis.yml), driven by a real editor.
  12. const snapshotMode = process.env.DSH_SNAPSHOT
  13. const configPath = snapshotMode === 'replay' ? './cordis.snapshot.yml' : './cordis.yml'
  14. // Load DEEPSEEK_API_KEY / DEEPSEEK_BASE_URL from a gitignored repo-root .env
  15. // (Node native). Absent file is fine — the environment may already carry them.
  16. // In REPLAY mode we deliberately skip this: replay must never reach the network,
  17. // so we don't want a present .env to enable a live call.
  18. //
  19. // IMPORTANT: this server speaks ACP JSON-RPC on stdout. Do NOT add any
  20. // stdout logging here or in cordis.yml — it would corrupt the protocol frames.
  21. // A present-but-unreadable/malformed .env is a real misconfiguration: surface
  22. // it on STDERR (never stdout) rather than silently running with the wrong env.
  23. if (snapshotMode !== 'replay') {
  24. try {
  25. process.loadEnvFile(new URL('../../.env', import.meta.url).pathname)
  26. } catch (error) {
  27. if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') {
  28. process.stderr.write(`acp-agent: failed to load .env: ${String(error)}\n`)
  29. }
  30. // ENOENT (no .env) is fine — rely on the ambient environment.
  31. }
  32. }
  33. // Resolve relative cordis.yml paths from the repo root no matter where the
  34. // editor launches this demo command.
  35. process.chdir(fileURLToPath(new URL('../..', import.meta.url)))
  36. const ctx = new Context()
  37. ctx.baseUrl = pathToFileURL(import.meta.dirname).href + '/'
  38. await ctx.plugin(Loader)
  39. await ctx.loader.create({
  40. name: '@cordisjs/plugin-include',
  41. config: {
  42. path: configPath,
  43. },
  44. })
  45. // Graceful shutdown for snapshot runs (both replay and record): when the client
  46. // closes our stdin (it is done driving the session), dispose the whole context.
  47. // Disposal awaits the agent-loop teardown and the persistence backend's final
  48. // `session/flush`, so the session `.jsonl` is fully written before the process
  49. // exits and the harness harvests it (and the subprocess exits cleanly so the
  50. // harness's waitForExit resolves). (In a normal editor session stdin stays open
  51. // for the connection's lifetime; the editor kills the process, so this never
  52. // fires.)
  53. if (snapshotMode !== undefined) {
  54. process.stdin.on('end', () => {
  55. void ctx.fiber.dispose().then(() => { process.exit(0) })
  56. })
  57. }