@hellcoder/companion 0.96.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. package/bin/cli.ts +168 -0
  2. package/bin/ctl.ts +528 -0
  3. package/bin/generate-token.ts +28 -0
  4. package/dist/apple-touch-icon.png +0 -0
  5. package/dist/assets/AgentsPage-DCFhrJ28.js +13 -0
  6. package/dist/assets/CronManager-EGwLJONv.js +1 -0
  7. package/dist/assets/IntegrationsPage-CTMRnbQS.js +1 -0
  8. package/dist/assets/LinearOAuthSettingsPage-CgQFMIgr.js +1 -0
  9. package/dist/assets/LinearSettingsPage-C9nok1qi.js +1 -0
  10. package/dist/assets/Playground-BV3k0RbV.js +109 -0
  11. package/dist/assets/PromptsPage-CFojqNKP.js +4 -0
  12. package/dist/assets/RunsPage-DUJ1QUSa.js +1 -0
  13. package/dist/assets/SandboxManager-CrVQ-VU_.js +8 -0
  14. package/dist/assets/SettingsPage-D1fPCL19.js +1 -0
  15. package/dist/assets/TailscalePage-D06cyvyC.js +1 -0
  16. package/dist/assets/index-BhUa1e6X.css +1 -0
  17. package/dist/assets/index-DkqeP-R9.js +134 -0
  18. package/dist/assets/sw-register-BibwRdvC.js +1 -0
  19. package/dist/assets/workbox-window.prod.es5-BIl4cyR9.js +2 -0
  20. package/dist/favicon.svg +8 -0
  21. package/dist/fonts/MesloLGSNerdFontMono-Bold.woff2 +0 -0
  22. package/dist/fonts/MesloLGSNerdFontMono-Regular.woff2 +0 -0
  23. package/dist/icon-192.png +0 -0
  24. package/dist/icon-512.png +0 -0
  25. package/dist/index.html +20 -0
  26. package/dist/logo-codex.svg +14 -0
  27. package/dist/logo-docker.svg +4 -0
  28. package/dist/logo.svg +14 -0
  29. package/dist/manifest.json +24 -0
  30. package/dist/sw.js +2 -0
  31. package/package.json +104 -0
  32. package/server/agent-cron-migrator.test.ts +610 -0
  33. package/server/agent-cron-migrator.ts +85 -0
  34. package/server/agent-executor.test.ts +1108 -0
  35. package/server/agent-executor.ts +346 -0
  36. package/server/agent-store.test.ts +588 -0
  37. package/server/agent-store.ts +185 -0
  38. package/server/agent-types.ts +138 -0
  39. package/server/ai-validation-settings.test.ts +128 -0
  40. package/server/ai-validation-settings.ts +35 -0
  41. package/server/ai-validator.test.ts +387 -0
  42. package/server/ai-validator.ts +271 -0
  43. package/server/auth-manager.test.ts +83 -0
  44. package/server/auth-manager.ts +150 -0
  45. package/server/auto-namer.test.ts +252 -0
  46. package/server/auto-namer.ts +78 -0
  47. package/server/backend-adapter.test.ts +38 -0
  48. package/server/backend-adapter.ts +54 -0
  49. package/server/cache-headers.test.ts +98 -0
  50. package/server/cache-headers.ts +61 -0
  51. package/server/claude-adapter.test.ts +1363 -0
  52. package/server/claude-adapter.ts +889 -0
  53. package/server/claude-container-auth.test.ts +44 -0
  54. package/server/claude-container-auth.ts +30 -0
  55. package/server/claude-protocol-contract.test.ts +71 -0
  56. package/server/claude-protocol-drift.test.ts +78 -0
  57. package/server/claude-session-discovery.test.ts +132 -0
  58. package/server/claude-session-discovery.ts +157 -0
  59. package/server/claude-session-history.test.ts +158 -0
  60. package/server/claude-session-history.ts +410 -0
  61. package/server/cli-launcher.test.ts +1343 -0
  62. package/server/cli-launcher.ts +1298 -0
  63. package/server/cli.test.ts +16 -0
  64. package/server/codex-adapter.test.ts +5545 -0
  65. package/server/codex-adapter.ts +3062 -0
  66. package/server/codex-container-auth.test.ts +50 -0
  67. package/server/codex-container-auth.ts +24 -0
  68. package/server/codex-home.test.ts +61 -0
  69. package/server/codex-home.ts +26 -0
  70. package/server/codex-protocol-contract.test.ts +96 -0
  71. package/server/codex-protocol-drift.test.ts +123 -0
  72. package/server/codex-ws-proxy.cjs +226 -0
  73. package/server/commands-discovery.test.ts +179 -0
  74. package/server/commands-discovery.ts +81 -0
  75. package/server/constants.ts +7 -0
  76. package/server/container-manager.test.ts +1211 -0
  77. package/server/container-manager.ts +1053 -0
  78. package/server/cron-scheduler.test.ts +957 -0
  79. package/server/cron-scheduler.ts +243 -0
  80. package/server/cron-store.test.ts +422 -0
  81. package/server/cron-store.ts +148 -0
  82. package/server/cron-types.ts +63 -0
  83. package/server/env-manager.test.ts +268 -0
  84. package/server/env-manager.ts +161 -0
  85. package/server/event-bus-types.ts +64 -0
  86. package/server/event-bus.test.ts +244 -0
  87. package/server/event-bus.ts +124 -0
  88. package/server/execution-store.test.ts +307 -0
  89. package/server/execution-store.ts +170 -0
  90. package/server/fs-utils.ts +15 -0
  91. package/server/git-utils.test.ts +938 -0
  92. package/server/git-utils.ts +421 -0
  93. package/server/github-pr.test.ts +498 -0
  94. package/server/github-pr.ts +379 -0
  95. package/server/image-pull-manager.test.ts +303 -0
  96. package/server/image-pull-manager.ts +279 -0
  97. package/server/index.ts +396 -0
  98. package/server/linear-agent-bridge.test.ts +1157 -0
  99. package/server/linear-agent-bridge.ts +629 -0
  100. package/server/linear-agent.test.ts +473 -0
  101. package/server/linear-agent.ts +479 -0
  102. package/server/linear-cache.test.ts +136 -0
  103. package/server/linear-cache.ts +113 -0
  104. package/server/linear-connections.test.ts +350 -0
  105. package/server/linear-connections.ts +231 -0
  106. package/server/linear-credential-migration.test.ts +337 -0
  107. package/server/linear-credential-migration.ts +63 -0
  108. package/server/linear-oauth-connections-migration.test.ts +268 -0
  109. package/server/linear-oauth-connections.test.ts +365 -0
  110. package/server/linear-oauth-connections.ts +294 -0
  111. package/server/linear-project-manager.test.ts +162 -0
  112. package/server/linear-project-manager.ts +111 -0
  113. package/server/linear-prompt-builder.test.ts +74 -0
  114. package/server/linear-prompt-builder.ts +61 -0
  115. package/server/linear-staging.test.ts +276 -0
  116. package/server/linear-staging.ts +142 -0
  117. package/server/logger.test.ts +393 -0
  118. package/server/logger.ts +259 -0
  119. package/server/metrics-collector.test.ts +413 -0
  120. package/server/metrics-collector.ts +350 -0
  121. package/server/metrics-types.ts +108 -0
  122. package/server/middleware/managed-auth.test.ts +264 -0
  123. package/server/middleware/managed-auth.ts +195 -0
  124. package/server/novnc-proxy.test.ts +333 -0
  125. package/server/novnc-proxy.ts +99 -0
  126. package/server/path-resolver.test.ts +552 -0
  127. package/server/path-resolver.ts +186 -0
  128. package/server/paths.test.ts +31 -0
  129. package/server/paths.ts +11 -0
  130. package/server/pr-poller.test.ts +191 -0
  131. package/server/pr-poller.ts +162 -0
  132. package/server/prompt-manager.test.ts +211 -0
  133. package/server/prompt-manager.ts +211 -0
  134. package/server/protocol/claude-upstream/README.md +19 -0
  135. package/server/protocol/claude-upstream/sdk.d.ts.txt +1943 -0
  136. package/server/protocol/codex-upstream/ClientNotification.ts.txt +5 -0
  137. package/server/protocol/codex-upstream/ClientRequest.ts.txt +60 -0
  138. package/server/protocol/codex-upstream/README.md +18 -0
  139. package/server/protocol/codex-upstream/ServerNotification.ts.txt +41 -0
  140. package/server/protocol/codex-upstream/ServerRequest.ts.txt +16 -0
  141. package/server/protocol/codex-upstream/v2/DynamicToolCallParams.ts.txt +6 -0
  142. package/server/protocol/codex-upstream/v2/DynamicToolCallResponse.ts.txt +6 -0
  143. package/server/protocol-monitor.ts +50 -0
  144. package/server/recorder.test.ts +454 -0
  145. package/server/recorder.ts +374 -0
  146. package/server/recording-hub/compat-validator.test.ts +150 -0
  147. package/server/recording-hub/compat-validator.ts +284 -0
  148. package/server/recording-hub/diagnostics.test.ts +140 -0
  149. package/server/recording-hub/diagnostics.ts +299 -0
  150. package/server/recording-hub/hub-config.test.ts +44 -0
  151. package/server/recording-hub/hub-config.ts +19 -0
  152. package/server/recording-hub/hub-routes.test.ts +417 -0
  153. package/server/recording-hub/hub-routes.ts +236 -0
  154. package/server/recording-hub/hub-store.test.ts +262 -0
  155. package/server/recording-hub/hub-store.ts +265 -0
  156. package/server/recording-hub/replay-adapter.test.ts +294 -0
  157. package/server/recording-hub/replay-adapter.ts +207 -0
  158. package/server/relay-client.test.ts +337 -0
  159. package/server/relay-client.ts +320 -0
  160. package/server/replay.test.ts +200 -0
  161. package/server/replay.ts +78 -0
  162. package/server/routes/agent-routes.test.ts +1400 -0
  163. package/server/routes/agent-routes.ts +409 -0
  164. package/server/routes/cron-routes.test.ts +881 -0
  165. package/server/routes/cron-routes.ts +103 -0
  166. package/server/routes/env-routes.test.ts +383 -0
  167. package/server/routes/env-routes.ts +95 -0
  168. package/server/routes/fs-routes.test.ts +1198 -0
  169. package/server/routes/fs-routes.ts +605 -0
  170. package/server/routes/git-routes.test.ts +813 -0
  171. package/server/routes/git-routes.ts +97 -0
  172. package/server/routes/linear-agent-routes.test.ts +721 -0
  173. package/server/routes/linear-agent-routes.ts +304 -0
  174. package/server/routes/linear-connection-routes.test.ts +927 -0
  175. package/server/routes/linear-connection-routes.ts +244 -0
  176. package/server/routes/linear-oauth-connection-routes.test.ts +406 -0
  177. package/server/routes/linear-oauth-connection-routes.ts +129 -0
  178. package/server/routes/linear-routes.test.ts +1510 -0
  179. package/server/routes/linear-routes.ts +953 -0
  180. package/server/routes/metrics-routes.test.ts +103 -0
  181. package/server/routes/metrics-routes.ts +13 -0
  182. package/server/routes/prompt-routes.ts +67 -0
  183. package/server/routes/sandbox-routes.test.ts +513 -0
  184. package/server/routes/sandbox-routes.ts +127 -0
  185. package/server/routes/settings-routes.ts +270 -0
  186. package/server/routes/skills-routes.test.ts +690 -0
  187. package/server/routes/skills-routes.ts +100 -0
  188. package/server/routes/system-routes.test.ts +637 -0
  189. package/server/routes/system-routes.ts +228 -0
  190. package/server/routes/tailscale-routes.test.ts +176 -0
  191. package/server/routes/tailscale-routes.ts +22 -0
  192. package/server/routes.test.ts +4655 -0
  193. package/server/routes.ts +1277 -0
  194. package/server/sandbox-manager.test.ts +378 -0
  195. package/server/sandbox-manager.ts +168 -0
  196. package/server/service.test.ts +1419 -0
  197. package/server/service.ts +718 -0
  198. package/server/session-creation-service.test.ts +661 -0
  199. package/server/session-creation-service.ts +473 -0
  200. package/server/session-git-info.ts +104 -0
  201. package/server/session-linear-issues.test.ts +118 -0
  202. package/server/session-linear-issues.ts +88 -0
  203. package/server/session-names.test.ts +94 -0
  204. package/server/session-names.ts +67 -0
  205. package/server/session-orchestrator.test.ts +1784 -0
  206. package/server/session-orchestrator.ts +973 -0
  207. package/server/session-state-machine.test.ts +606 -0
  208. package/server/session-state-machine.ts +207 -0
  209. package/server/session-store.test.ts +290 -0
  210. package/server/session-store.ts +146 -0
  211. package/server/session-types.ts +509 -0
  212. package/server/settings-manager.test.ts +275 -0
  213. package/server/settings-manager.ts +173 -0
  214. package/server/tailscale-manager.test.ts +553 -0
  215. package/server/tailscale-manager.ts +451 -0
  216. package/server/terminal-manager.ts +240 -0
  217. package/server/update-checker.test.ts +306 -0
  218. package/server/update-checker.ts +197 -0
  219. package/server/usage-limits.test.ts +536 -0
  220. package/server/usage-limits.ts +225 -0
  221. package/server/worktree-tracker.test.ts +243 -0
  222. package/server/worktree-tracker.ts +84 -0
  223. package/server/ws-auth.test.ts +59 -0
  224. package/server/ws-auth.ts +41 -0
  225. package/server/ws-bridge-browser-ingest.test.ts +272 -0
  226. package/server/ws-bridge-browser-ingest.ts +72 -0
  227. package/server/ws-bridge-browser.ts +112 -0
  228. package/server/ws-bridge-cli-ingest.test.ts +302 -0
  229. package/server/ws-bridge-cli-ingest.ts +81 -0
  230. package/server/ws-bridge-codex.test.ts +1837 -0
  231. package/server/ws-bridge-codex.ts +266 -0
  232. package/server/ws-bridge-controls.test.ts +124 -0
  233. package/server/ws-bridge-controls.ts +20 -0
  234. package/server/ws-bridge-persist.test.ts +296 -0
  235. package/server/ws-bridge-persist.ts +66 -0
  236. package/server/ws-bridge-publish.test.ts +234 -0
  237. package/server/ws-bridge-publish.ts +79 -0
  238. package/server/ws-bridge-replay.test.ts +44 -0
  239. package/server/ws-bridge-replay.ts +61 -0
  240. package/server/ws-bridge-types.ts +106 -0
  241. package/server/ws-bridge.test.ts +4777 -0
  242. package/server/ws-bridge.ts +1279 -0
@@ -0,0 +1,275 @@
1
+ import { mkdtempSync, rmSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { tmpdir } from "node:os";
4
+ import {
5
+ getSettings,
6
+ updateSettings,
7
+ _resetForTest,
8
+ DEFAULT_ANTHROPIC_MODEL,
9
+ } from "./settings-manager.js";
10
+
11
+ let tempDir: string;
12
+ let settingsPath: string;
13
+
14
+ beforeEach(() => {
15
+ tempDir = mkdtempSync(join(tmpdir(), "settings-manager-test-"));
16
+ settingsPath = join(tempDir, "settings.json");
17
+ _resetForTest(settingsPath);
18
+ });
19
+
20
+ afterEach(() => {
21
+ rmSync(tempDir, { recursive: true, force: true });
22
+ _resetForTest();
23
+ });
24
+
25
+ describe("settings-manager", () => {
26
+ it("returns defaults when file is missing", () => {
27
+ expect(getSettings()).toEqual({
28
+ anthropicApiKey: "",
29
+ anthropicModel: DEFAULT_ANTHROPIC_MODEL,
30
+ linearApiKey: "",
31
+ linearAutoTransition: false,
32
+ linearAutoTransitionStateId: "",
33
+ linearAutoTransitionStateName: "",
34
+ linearArchiveTransition: false,
35
+ linearArchiveTransitionStateId: "",
36
+ linearArchiveTransitionStateName: "",
37
+ linearOAuthClientId: "",
38
+ linearOAuthClientSecret: "",
39
+ linearOAuthWebhookSecret: "",
40
+ linearOAuthAccessToken: "",
41
+ linearOAuthRefreshToken: "",
42
+ claudeCodeOAuthToken: "",
43
+ openaiApiKey: "",
44
+ onboardingCompleted: false,
45
+ aiValidationEnabled: false,
46
+ aiValidationAutoApprove: true,
47
+ aiValidationAutoDeny: false,
48
+ publicUrl: "",
49
+ updateChannel: "stable",
50
+ dockerAutoUpdate: false,
51
+ updatedAt: 0,
52
+ });
53
+ });
54
+
55
+ it("updates and persists settings", () => {
56
+ const updated = updateSettings({ anthropicApiKey: "sk-ant-key" });
57
+ expect(updated.anthropicApiKey).toBe("sk-ant-key");
58
+ expect(updated.anthropicModel).toBe(DEFAULT_ANTHROPIC_MODEL);
59
+ expect(updated.linearApiKey).toBe("");
60
+ expect(updated.updatedAt).toBeGreaterThan(0);
61
+
62
+ const saved = JSON.parse(readFileSync(settingsPath, "utf-8"));
63
+ expect(saved.anthropicApiKey).toBe("sk-ant-key");
64
+ expect(saved.anthropicModel).toBe(DEFAULT_ANTHROPIC_MODEL);
65
+ expect(saved.linearApiKey).toBe("");
66
+ });
67
+
68
+ it("loads existing settings from disk", () => {
69
+ writeFileSync(
70
+ settingsPath,
71
+ JSON.stringify({
72
+ anthropicApiKey: "existing",
73
+ anthropicModel: "claude-haiku-3",
74
+ linearApiKey: "lin_api_abc",
75
+ updatedAt: 123,
76
+ }),
77
+ "utf-8",
78
+ );
79
+
80
+ _resetForTest(settingsPath);
81
+
82
+ expect(getSettings()).toEqual({
83
+ anthropicApiKey: "existing",
84
+ anthropicModel: "claude-haiku-3",
85
+ linearApiKey: "lin_api_abc",
86
+ linearAutoTransition: false,
87
+ linearAutoTransitionStateId: "",
88
+ linearAutoTransitionStateName: "",
89
+ linearArchiveTransition: false,
90
+ linearArchiveTransitionStateId: "",
91
+ linearArchiveTransitionStateName: "",
92
+ linearOAuthClientId: "",
93
+ linearOAuthClientSecret: "",
94
+ linearOAuthWebhookSecret: "",
95
+ linearOAuthAccessToken: "",
96
+ linearOAuthRefreshToken: "",
97
+ claudeCodeOAuthToken: "",
98
+ openaiApiKey: "",
99
+ onboardingCompleted: false,
100
+ aiValidationEnabled: false,
101
+ aiValidationAutoApprove: true,
102
+ aiValidationAutoDeny: false,
103
+ publicUrl: "",
104
+ updateChannel: "stable",
105
+ dockerAutoUpdate: false,
106
+ updatedAt: 123,
107
+ });
108
+ });
109
+
110
+ it("falls back to defaults for invalid JSON", () => {
111
+ writeFileSync(settingsPath, "not-json", "utf-8");
112
+ _resetForTest(settingsPath);
113
+
114
+ expect(getSettings().anthropicModel).toBe(DEFAULT_ANTHROPIC_MODEL);
115
+ });
116
+
117
+ // Migration: existing users with the old dot-form model ID should be auto-corrected
118
+ it("migrates persisted claude-sonnet-4.6 (dot) to claude-sonnet-4-6 (hyphen)", () => {
119
+ writeFileSync(
120
+ settingsPath,
121
+ JSON.stringify({
122
+ anthropicApiKey: "sk-ant-existing",
123
+ anthropicModel: "claude-sonnet-4.6",
124
+ }),
125
+ "utf-8",
126
+ );
127
+ _resetForTest(settingsPath);
128
+
129
+ const settings = getSettings();
130
+ expect(settings.anthropicModel).toBe(DEFAULT_ANTHROPIC_MODEL);
131
+ expect(settings.anthropicApiKey).toBe("sk-ant-existing");
132
+ });
133
+
134
+ it("updates only model while preserving existing key", () => {
135
+ updateSettings({ anthropicApiKey: "sk-ant-key" });
136
+ const updated = updateSettings({ anthropicModel: "claude-haiku-3" });
137
+
138
+ expect(updated.anthropicApiKey).toBe("sk-ant-key");
139
+ expect(updated.anthropicModel).toBe("claude-haiku-3");
140
+ expect(updated.linearApiKey).toBe("");
141
+ });
142
+
143
+ it("uses default model when empty model is provided", () => {
144
+ const updated = updateSettings({ anthropicModel: "" });
145
+ expect(updated.anthropicModel).toBe(DEFAULT_ANTHROPIC_MODEL);
146
+ });
147
+
148
+ it("normalizes malformed file shape to defaults", () => {
149
+ writeFileSync(
150
+ settingsPath,
151
+ JSON.stringify({
152
+ anthropicApiKey: 123,
153
+ anthropicModel: null,
154
+ linearApiKey: 123,
155
+ updatedAt: "x",
156
+ }),
157
+ "utf-8",
158
+ );
159
+ _resetForTest(settingsPath);
160
+
161
+ expect(getSettings()).toEqual({
162
+ anthropicApiKey: "",
163
+ anthropicModel: DEFAULT_ANTHROPIC_MODEL,
164
+ linearApiKey: "",
165
+ linearAutoTransition: false,
166
+ linearAutoTransitionStateId: "",
167
+ linearAutoTransitionStateName: "",
168
+ linearArchiveTransition: false,
169
+ linearArchiveTransitionStateId: "",
170
+ linearArchiveTransitionStateName: "",
171
+ linearOAuthClientId: "",
172
+ linearOAuthClientSecret: "",
173
+ linearOAuthWebhookSecret: "",
174
+ linearOAuthAccessToken: "",
175
+ linearOAuthRefreshToken: "",
176
+ claudeCodeOAuthToken: "",
177
+ openaiApiKey: "",
178
+ onboardingCompleted: false,
179
+ aiValidationEnabled: false,
180
+ aiValidationAutoApprove: true,
181
+ aiValidationAutoDeny: false,
182
+ publicUrl: "",
183
+ updateChannel: "stable",
184
+ dockerAutoUpdate: false,
185
+ updatedAt: 0,
186
+ });
187
+ });
188
+
189
+ it("updates linear key without touching anthropic settings", () => {
190
+ updateSettings({ anthropicApiKey: "sk-ant-key", anthropicModel: "claude-sonnet-4-6" });
191
+ const updated = updateSettings({ linearApiKey: "lin_api_123" });
192
+
193
+ expect(updated.anthropicApiKey).toBe("sk-ant-key");
194
+ expect(updated.anthropicModel).toBe("claude-sonnet-4-6");
195
+ expect(updated.linearApiKey).toBe("lin_api_123");
196
+ });
197
+
198
+ it("ignores undefined patch values and preserves existing keys", () => {
199
+ updateSettings({ anthropicApiKey: "sk-ant-key", linearApiKey: "lin_api_123" });
200
+ const updated = updateSettings({
201
+ anthropicApiKey: undefined,
202
+ anthropicModel: "claude-haiku-3",
203
+ linearApiKey: undefined,
204
+ });
205
+
206
+ expect(updated.anthropicApiKey).toBe("sk-ant-key");
207
+ expect(updated.anthropicModel).toBe("claude-haiku-3");
208
+ expect(updated.linearApiKey).toBe("lin_api_123");
209
+ });
210
+
211
+ it("updates updateChannel to prerelease", () => {
212
+ const updated = updateSettings({ updateChannel: "prerelease" });
213
+ expect(updated.updateChannel).toBe("prerelease");
214
+ });
215
+
216
+ it("defaults updateChannel to stable for invalid values", () => {
217
+ writeFileSync(
218
+ settingsPath,
219
+ JSON.stringify({ updateChannel: "invalid" }),
220
+ "utf-8",
221
+ );
222
+ _resetForTest(settingsPath);
223
+ expect(getSettings().updateChannel).toBe("stable");
224
+ });
225
+
226
+ it("preserves updateChannel when updating other settings", () => {
227
+ updateSettings({ updateChannel: "prerelease" });
228
+ const updated = updateSettings({ anthropicModel: "claude-haiku-3" });
229
+ expect(updated.updateChannel).toBe("prerelease");
230
+ });
231
+
232
+ // ─── publicUrl tests ────────────────────────────────────────────────────────
233
+
234
+ // Default settings include publicUrl as empty string
235
+ it("default settings include publicUrl as empty string", () => {
236
+ expect(getSettings().publicUrl).toBe("");
237
+ });
238
+
239
+ // updateSettings saves publicUrl when a valid URL is provided
240
+ it("saves publicUrl via updateSettings", () => {
241
+ const updated = updateSettings({ publicUrl: "https://example.com" });
242
+ expect(updated.publicUrl).toBe("https://example.com");
243
+
244
+ const saved = JSON.parse(readFileSync(settingsPath, "utf-8"));
245
+ expect(saved.publicUrl).toBe("https://example.com");
246
+ });
247
+
248
+ // updateSettings strips trailing slashes from publicUrl
249
+ it("strips trailing slashes from publicUrl", () => {
250
+ const updated = updateSettings({ publicUrl: "https://example.com///" });
251
+ expect(updated.publicUrl).toBe("https://example.com");
252
+ });
253
+
254
+ // Missing publicUrl in raw JSON on disk normalizes to empty string
255
+ it("normalizes missing publicUrl in raw JSON to empty string", () => {
256
+ writeFileSync(
257
+ settingsPath,
258
+ JSON.stringify({
259
+ anthropicApiKey: "key",
260
+ anthropicModel: "claude-sonnet-4-6",
261
+ }),
262
+ "utf-8",
263
+ );
264
+ _resetForTest(settingsPath);
265
+
266
+ expect(getSettings().publicUrl).toBe("");
267
+ });
268
+
269
+ // Updating other settings preserves an existing publicUrl value
270
+ it("preserves publicUrl when updating other settings", () => {
271
+ updateSettings({ publicUrl: "https://example.com" });
272
+ const updated = updateSettings({ anthropicModel: "claude-haiku-3" });
273
+ expect(updated.publicUrl).toBe("https://example.com");
274
+ });
275
+ });
@@ -0,0 +1,173 @@
1
+ import {
2
+ mkdirSync,
3
+ readFileSync,
4
+ writeFileSync,
5
+ existsSync,
6
+ } from "node:fs";
7
+ import { join, dirname } from "node:path";
8
+ import { COMPANION_HOME } from "./paths.js";
9
+
10
+ export const DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-4-6";
11
+
12
+ export type UpdateChannel = "stable" | "prerelease";
13
+
14
+ export interface CompanionSettings {
15
+ anthropicApiKey: string;
16
+ anthropicModel: string;
17
+ /** OAuth token obtained via `claude setup-token` — injected as CLAUDE_CODE_OAUTH_TOKEN */
18
+ claudeCodeOAuthToken: string;
19
+ /** OpenAI API key for Codex — injected as OPENAI_API_KEY */
20
+ openaiApiKey: string;
21
+ /** Whether the onboarding wizard has been completed */
22
+ onboardingCompleted: boolean;
23
+ linearApiKey: string;
24
+ linearAutoTransition: boolean;
25
+ linearAutoTransitionStateId: string;
26
+ linearAutoTransitionStateName: string;
27
+ linearArchiveTransition: boolean;
28
+ linearArchiveTransitionStateId: string;
29
+ linearArchiveTransitionStateName: string;
30
+ /** @deprecated Used only as staging during wizard flow. Per-agent credentials are in AgentConfig.triggers.linear. */
31
+ linearOAuthClientId: string;
32
+ /** @deprecated Used only as staging during wizard flow. Per-agent credentials are in AgentConfig.triggers.linear. */
33
+ linearOAuthClientSecret: string;
34
+ /** @deprecated Used only as staging during wizard flow. Per-agent credentials are in AgentConfig.triggers.linear. */
35
+ linearOAuthWebhookSecret: string;
36
+ /** @deprecated Used only as staging during wizard flow. Per-agent credentials are in AgentConfig.triggers.linear. */
37
+ linearOAuthAccessToken: string;
38
+ /** @deprecated Used only as staging during wizard flow. Per-agent credentials are in AgentConfig.triggers.linear. */
39
+ linearOAuthRefreshToken: string;
40
+ aiValidationEnabled: boolean;
41
+ aiValidationAutoApprove: boolean;
42
+ aiValidationAutoDeny: boolean;
43
+ publicUrl: string;
44
+ updateChannel: UpdateChannel;
45
+ dockerAutoUpdate: boolean;
46
+ updatedAt: number;
47
+ }
48
+
49
+ const DEFAULT_PATH = join(COMPANION_HOME, "settings.json");
50
+
51
+ let loaded = false;
52
+ let filePath = DEFAULT_PATH;
53
+ let settings: CompanionSettings = {
54
+ anthropicApiKey: "",
55
+ anthropicModel: DEFAULT_ANTHROPIC_MODEL,
56
+ claudeCodeOAuthToken: "",
57
+ openaiApiKey: "",
58
+ onboardingCompleted: false,
59
+ linearApiKey: "",
60
+ linearAutoTransition: false,
61
+ linearAutoTransitionStateId: "",
62
+ linearAutoTransitionStateName: "",
63
+ linearArchiveTransition: false,
64
+ linearArchiveTransitionStateId: "",
65
+ linearArchiveTransitionStateName: "",
66
+ linearOAuthClientId: "",
67
+ linearOAuthClientSecret: "",
68
+ linearOAuthWebhookSecret: "",
69
+ linearOAuthAccessToken: "",
70
+ linearOAuthRefreshToken: "",
71
+ aiValidationEnabled: false,
72
+ aiValidationAutoApprove: true,
73
+ aiValidationAutoDeny: false,
74
+ publicUrl: "",
75
+ updateChannel: "stable",
76
+ dockerAutoUpdate: false,
77
+ updatedAt: 0,
78
+ };
79
+
80
+ function normalize(raw: Partial<CompanionSettings> | null | undefined): CompanionSettings {
81
+ return {
82
+ anthropicApiKey: typeof raw?.anthropicApiKey === "string" ? raw.anthropicApiKey : "",
83
+ anthropicModel:
84
+ typeof raw?.anthropicModel === "string" && raw.anthropicModel.trim()
85
+ ? raw.anthropicModel === "claude-sonnet-4.6" ? DEFAULT_ANTHROPIC_MODEL : raw.anthropicModel
86
+ : DEFAULT_ANTHROPIC_MODEL,
87
+ claudeCodeOAuthToken: typeof raw?.claudeCodeOAuthToken === "string" ? raw.claudeCodeOAuthToken : "",
88
+ openaiApiKey: typeof raw?.openaiApiKey === "string" ? raw.openaiApiKey : "",
89
+ onboardingCompleted: typeof raw?.onboardingCompleted === "boolean" ? raw.onboardingCompleted : false,
90
+ linearApiKey: typeof raw?.linearApiKey === "string" ? raw.linearApiKey : "",
91
+ linearAutoTransition: typeof raw?.linearAutoTransition === "boolean" ? raw.linearAutoTransition : false,
92
+ linearAutoTransitionStateId: typeof raw?.linearAutoTransitionStateId === "string" ? raw.linearAutoTransitionStateId : "",
93
+ linearAutoTransitionStateName: typeof raw?.linearAutoTransitionStateName === "string" ? raw.linearAutoTransitionStateName : "",
94
+ linearArchiveTransition: typeof raw?.linearArchiveTransition === "boolean" ? raw.linearArchiveTransition : false,
95
+ linearArchiveTransitionStateId: typeof raw?.linearArchiveTransitionStateId === "string" ? raw.linearArchiveTransitionStateId : "",
96
+ linearArchiveTransitionStateName: typeof raw?.linearArchiveTransitionStateName === "string" ? raw.linearArchiveTransitionStateName : "",
97
+ linearOAuthClientId: typeof raw?.linearOAuthClientId === "string" ? raw.linearOAuthClientId : "",
98
+ linearOAuthClientSecret: typeof raw?.linearOAuthClientSecret === "string" ? raw.linearOAuthClientSecret : "",
99
+ linearOAuthWebhookSecret: typeof raw?.linearOAuthWebhookSecret === "string" ? raw.linearOAuthWebhookSecret : "",
100
+ linearOAuthAccessToken: typeof raw?.linearOAuthAccessToken === "string" ? raw.linearOAuthAccessToken : "",
101
+ linearOAuthRefreshToken: typeof raw?.linearOAuthRefreshToken === "string" ? raw.linearOAuthRefreshToken : "",
102
+ aiValidationEnabled: typeof raw?.aiValidationEnabled === "boolean" ? raw.aiValidationEnabled : false,
103
+ aiValidationAutoApprove: typeof raw?.aiValidationAutoApprove === "boolean" ? raw.aiValidationAutoApprove : true,
104
+ aiValidationAutoDeny: typeof raw?.aiValidationAutoDeny === "boolean" ? raw.aiValidationAutoDeny : false,
105
+ publicUrl: typeof raw?.publicUrl === "string" ? raw.publicUrl.trim().replace(/\/+$/, "") : "",
106
+ updateChannel: raw?.updateChannel === "prerelease" ? "prerelease" : "stable",
107
+ dockerAutoUpdate: typeof raw?.dockerAutoUpdate === "boolean" ? raw.dockerAutoUpdate : false,
108
+ updatedAt: typeof raw?.updatedAt === "number" ? raw.updatedAt : 0,
109
+ };
110
+ }
111
+
112
+ function ensureLoaded(): void {
113
+ if (loaded) return;
114
+ try {
115
+ if (existsSync(filePath)) {
116
+ const raw = readFileSync(filePath, "utf-8");
117
+ settings = normalize(JSON.parse(raw) as Partial<CompanionSettings>);
118
+ }
119
+ } catch {
120
+ settings = normalize(null);
121
+ }
122
+ loaded = true;
123
+ }
124
+
125
+ function persist(): void {
126
+ mkdirSync(dirname(filePath), { recursive: true });
127
+ writeFileSync(filePath, JSON.stringify(settings, null, 2), "utf-8");
128
+ }
129
+
130
+ export function getSettings(): CompanionSettings {
131
+ ensureLoaded();
132
+ return { ...settings };
133
+ }
134
+
135
+ export function updateSettings(
136
+ patch: Partial<Pick<CompanionSettings, "anthropicApiKey" | "anthropicModel" | "claudeCodeOAuthToken" | "openaiApiKey" | "onboardingCompleted" | "linearApiKey" | "linearAutoTransition" | "linearAutoTransitionStateId" | "linearAutoTransitionStateName" | "linearArchiveTransition" | "linearArchiveTransitionStateId" | "linearArchiveTransitionStateName" | "linearOAuthClientId" | "linearOAuthClientSecret" | "linearOAuthWebhookSecret" | "linearOAuthAccessToken" | "linearOAuthRefreshToken" | "aiValidationEnabled" | "aiValidationAutoApprove" | "aiValidationAutoDeny" | "publicUrl" | "updateChannel" | "dockerAutoUpdate">>,
137
+ ): CompanionSettings {
138
+ ensureLoaded();
139
+ settings = normalize({
140
+ anthropicApiKey: patch.anthropicApiKey ?? settings.anthropicApiKey,
141
+ anthropicModel: patch.anthropicModel ?? settings.anthropicModel,
142
+ claudeCodeOAuthToken: patch.claudeCodeOAuthToken ?? settings.claudeCodeOAuthToken,
143
+ openaiApiKey: patch.openaiApiKey ?? settings.openaiApiKey,
144
+ onboardingCompleted: patch.onboardingCompleted ?? settings.onboardingCompleted,
145
+ linearApiKey: patch.linearApiKey ?? settings.linearApiKey,
146
+ linearAutoTransition: patch.linearAutoTransition ?? settings.linearAutoTransition,
147
+ linearAutoTransitionStateId: patch.linearAutoTransitionStateId ?? settings.linearAutoTransitionStateId,
148
+ linearAutoTransitionStateName: patch.linearAutoTransitionStateName ?? settings.linearAutoTransitionStateName,
149
+ linearArchiveTransition: patch.linearArchiveTransition ?? settings.linearArchiveTransition,
150
+ linearArchiveTransitionStateId: patch.linearArchiveTransitionStateId ?? settings.linearArchiveTransitionStateId,
151
+ linearArchiveTransitionStateName: patch.linearArchiveTransitionStateName ?? settings.linearArchiveTransitionStateName,
152
+ linearOAuthClientId: patch.linearOAuthClientId ?? settings.linearOAuthClientId,
153
+ linearOAuthClientSecret: patch.linearOAuthClientSecret ?? settings.linearOAuthClientSecret,
154
+ linearOAuthWebhookSecret: patch.linearOAuthWebhookSecret ?? settings.linearOAuthWebhookSecret,
155
+ linearOAuthAccessToken: patch.linearOAuthAccessToken ?? settings.linearOAuthAccessToken,
156
+ linearOAuthRefreshToken: patch.linearOAuthRefreshToken ?? settings.linearOAuthRefreshToken,
157
+ aiValidationEnabled: patch.aiValidationEnabled ?? settings.aiValidationEnabled,
158
+ aiValidationAutoApprove: patch.aiValidationAutoApprove ?? settings.aiValidationAutoApprove,
159
+ aiValidationAutoDeny: patch.aiValidationAutoDeny ?? settings.aiValidationAutoDeny,
160
+ publicUrl: patch.publicUrl ?? settings.publicUrl,
161
+ updateChannel: patch.updateChannel ?? settings.updateChannel,
162
+ dockerAutoUpdate: patch.dockerAutoUpdate ?? settings.dockerAutoUpdate,
163
+ updatedAt: Date.now(),
164
+ });
165
+ persist();
166
+ return { ...settings };
167
+ }
168
+
169
+ export function _resetForTest(customPath?: string): void {
170
+ loaded = false;
171
+ filePath = customPath || DEFAULT_PATH;
172
+ settings = normalize(null);
173
+ }