* Internal helper to build config from already-resolved ports. * @param {{backendPort: number, vscodePort: number}} ports * @param {string} cwd * @param {Record } env * @returns {SafeDevConfig}
(ports, cwd, env)
| 732 | * @returns {SafeDevConfig} |
| 733 | */ |
| 734 | function buildConfigFromPorts(ports, cwd, env) { |
| 735 | const { backendPort, vscodePort } = ports; |
| 736 | const stateDir = path.resolve( |
| 737 | cwd, |
| 738 | env.OH_CANVAS_SAFE_STATE_DIR || |
| 739 | path.join(homedir(), ".openhands", "agent-canvas"), |
| 740 | ); |
| 741 | const conversationsPath = path.join(stateDir, "dev_conversations"); |
| 742 | const workspacesPath = path.join(stateDir, "workspaces"); |
| 743 | // Use provided secret key, or read/generate one persisted to |
| 744 | // ~/.openhands/agent-canvas/secret-key.txt. Persisting ensures dev mode |
| 745 | // and Docker mode share the same encryption key when they mount the same |
| 746 | // ~/.openhands directory (docker/entrypoint.sh reads/writes the same file). |
| 747 | const secretKeyPath = env.OH_SECRET_KEY_PATH || DEFAULT_SECRET_KEY_PATH; |
| 748 | const secretKey = |
| 749 | env.OH_SECRET_KEY || getOrCreatePersistedApiKey(secretKeyPath, "secret"); |
| 750 | // Use the user-provided LOCAL_BACKEND_API_KEY or fall back to a key |
| 751 | // persisted to ~/.openhands/agent-canvas/api-key.txt. Persisting on disk |
| 752 | // keeps the agent-server, the Vite-baked VITE_SESSION_API_KEY, and any |
| 753 | // `openhands-backends` localStorage entries the frontend has cached all |
| 754 | // pointing at the same value across dev restarts. |
| 755 | // |
| 756 | // LOCAL_BACKEND_API_KEY is the single user-facing env var for the API key. |
| 757 | // OH_SESSION_API_KEY_PATH overrides the persisted file path (used by tests). |
| 758 | const persistedKeyPath = env.OH_SESSION_API_KEY_PATH || DEFAULT_API_KEY_PATH; |
| 759 | const sessionApiKey = |
| 760 | env.LOCAL_BACKEND_API_KEY || |
| 761 | getOrCreatePersistedApiKeyFile(persistedKeyPath); |
| 762 | |
| 763 | // Host directory containing the legacy canvas_ui Python module. Persisted |
| 764 | // conversations created before the client_tools migration still reference |
| 765 | // its module qualname, so the agent-server can import it when resuming them. |
| 766 | const canvasToolsDir = fileURLToPath(new URL("../tools", import.meta.url)); |
| 767 | |
| 768 | return { |
| 769 | cwd, |
| 770 | backendPort, |
| 771 | vscodePort, |
| 772 | vscodeBasePath: VSCODE_BASE_PATH, |
| 773 | stateDir, |
| 774 | // tmux socket directory. Defaults to <stateDir>/tmux (under |
| 775 | // ~/.openhands/agent-canvas), matching where the rest of dev state lives |
| 776 | // and persisting across restarts. |
| 777 | // |
| 778 | // Do NOT use os.tmpdir() here: on macOS it resolves to the per-user |
| 779 | // $TMPDIR (/var/folders/.../T), which the OS periodically reaps |
| 780 | // (com.apple.bsd.dirhelper deletes entries untouched for a few days). |
| 781 | // Reaping deletes the live tmux socket while the server process keeps |
| 782 | // running, orphaning it — every later new-window then fails with |
| 783 | // "error connecting to .../openhands (No such file or directory)". |
| 784 | // |
| 785 | // The only hosts where <stateDir>/tmux can't hold the socket are those |
| 786 | // whose $HOME is a network/overlay mount without Unix-domain-socket |
| 787 | // support (some devcontainers, NFS/CIFS homes). Those rare cases can point |
| 788 | // tmux at a local, socket-capable path with the standard TMUX_TMPDIR env |
| 789 | // var (e.g. TMUX_TMPDIR=/tmp), which we honor and pass through below. |
| 790 | tmuxTmpDir: env.TMUX_TMPDIR || path.join(stateDir, "tmux"), |
| 791 | conversationsPath, |
no test coverage detected