@causyne/agent 1.20.124 → 1.20.128

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 (254) hide show
  1. package/bin/causyne.js +1 -1
  2. package/docs/THIRD_PARTY_NOTICES.md +36 -2
  3. package/package.json +6 -4
  4. package/src/acp/protocol.ts +19 -1
  5. package/src/acp/server.js +282 -8
  6. package/src/acp/server.ts +284 -8
  7. package/src/agent/abortableDelay.js +2 -2
  8. package/src/agent/abortableDelay.ts +2 -2
  9. package/src/agent/errorClassifier.js +9 -1
  10. package/src/agent/errorClassifier.ts +8 -1
  11. package/src/agentLoop.js +142 -53
  12. package/src/agentLoop.ts +151 -50
  13. package/src/approvalPolicy.js +34 -10
  14. package/src/approvalPolicy.ts +42 -11
  15. package/src/browser/causyneBrowser.js +61 -3
  16. package/src/browser/causyneBrowser.ts +65 -3
  17. package/src/browser/urlPolicy.js +14 -1
  18. package/src/browser/urlPolicy.ts +12 -1
  19. package/src/cliMode.js +21 -9
  20. package/src/cliMode.ts +20 -8
  21. package/src/commands.js +365 -45
  22. package/src/commands.ts +403 -45
  23. package/src/config.js +19 -4
  24. package/src/config.ts +18 -4
  25. package/src/core/contextCompactor.js +54 -7
  26. package/src/core/contextCompactor.ts +63 -7
  27. package/src/core/goalLoop.js +257 -0
  28. package/src/core/goalLoop.ts +310 -0
  29. package/src/entrypoint.js +135 -3
  30. package/src/entrypoint.ts +136 -3
  31. package/src/game/causyneCoreBridge.js +686 -0
  32. package/src/game/causyneCoreBridge.ts +702 -0
  33. package/src/game/gameLauncher.js +287 -0
  34. package/src/game/gameLauncher.ts +352 -0
  35. package/src/game/gameProvisioner.js +425 -0
  36. package/src/game/gameProvisioner.ts +488 -0
  37. package/src/headlessCli.js +19 -12
  38. package/src/headlessCli.ts +22 -8
  39. package/src/infra/atomicWriter.js +40 -4
  40. package/src/infra/atomicWriter.ts +36 -4
  41. package/src/launchIntent.js +73 -3
  42. package/src/launchIntent.ts +76 -3
  43. package/src/lsp/lspClient.js +83 -12
  44. package/src/lsp/lspClient.ts +75 -12
  45. package/src/mcp/mcpRegistry.js +75 -8
  46. package/src/mcp/mcpRegistry.ts +70 -7
  47. package/src/memory/contextBudget.js +2 -0
  48. package/src/memory/contextBudget.ts +1 -0
  49. package/src/memory/episodicStore.js +40 -0
  50. package/src/memory/episodicStore.ts +46 -0
  51. package/src/memory.js +67 -0
  52. package/src/memory.ts +65 -0
  53. package/src/modelVariants.js +45 -0
  54. package/src/modelVariants.ts +54 -0
  55. package/src/outputRecovery.js +3 -2
  56. package/src/outputRecovery.ts +4 -2
  57. package/src/permissionPresets.js +327 -0
  58. package/src/permissionPresets.ts +361 -0
  59. package/src/personalization.js +365 -0
  60. package/src/personalization.ts +471 -0
  61. package/src/projectInstructions.js +30 -10
  62. package/src/projectInstructions.ts +31 -12
  63. package/src/providers/acp.js +3 -2
  64. package/src/providers/acp.ts +3 -2
  65. package/src/providers/antigravity.js +64 -9
  66. package/src/providers/antigravity.ts +75 -9
  67. package/src/providers/byokHttp.js +7 -1
  68. package/src/providers/byokHttp.ts +6 -1
  69. package/src/providers/byokStream.js +21 -8
  70. package/src/providers/byokStream.ts +17 -7
  71. package/src/providers/dashscope-byok.js +21 -5
  72. package/src/providers/dashscope-byok.ts +29 -11
  73. package/src/providers/moonshot.js +26 -7
  74. package/src/providers/moonshot.ts +34 -13
  75. package/src/providers/muse.js +6 -0
  76. package/src/providers/muse.ts +8 -0
  77. package/src/providers/ollama.js +39 -7
  78. package/src/providers/ollama.ts +36 -7
  79. package/src/providers/openai-compatible.js +16 -3
  80. package/src/providers/openai-compatible.ts +13 -3
  81. package/src/providers/openai.js +62 -4
  82. package/src/providers/openai.ts +50 -4
  83. package/src/providers/openrouter.js +19 -4
  84. package/src/providers/openrouter.ts +21 -4
  85. package/src/providers/qwen-byok.js +21 -5
  86. package/src/providers/qwen-byok.ts +29 -11
  87. package/src/providers/xai.js +26 -10
  88. package/src/providers/xai.ts +34 -16
  89. package/src/reporting.js +4 -2
  90. package/src/reporting.ts +4 -2
  91. package/src/sandbox.js +34 -0
  92. package/src/sandbox.ts +43 -0
  93. package/src/scheduler/scheduler.js +372 -0
  94. package/src/scheduler/scheduler.ts +433 -0
  95. package/src/sessionSearchIndex.js +267 -0
  96. package/src/sessionSearchIndex.ts +311 -0
  97. package/src/skillsManager.js +2 -3
  98. package/src/skillsManager.ts +2 -3
  99. package/src/toolPacks.js +6 -0
  100. package/src/toolPacks.ts +6 -0
  101. package/src/tools/applyPatch.js +1 -1
  102. package/src/tools/applyPatch.ts +1 -1
  103. package/src/tools/generateMediaViaApi.js +4 -3
  104. package/src/tools/generateMediaViaApi.ts +4 -3
  105. package/src/tools/memoryWrite.js +15 -1
  106. package/src/tools/memoryWrite.ts +16 -1
  107. package/src/tools/nativeEngine.js +2 -2
  108. package/src/tools/nativeEngine.ts +2 -2
  109. package/src/tools/openSourceToolProvisioner.js +2 -2
  110. package/src/tools/openSourceToolProvisioner.ts +2 -2
  111. package/src/tools/osSandbox.js +1 -1
  112. package/src/tools/osSandbox.ts +1 -1
  113. package/src/tools/payloadSanitizer.js +0 -1
  114. package/src/tools/payloadSanitizer.ts +0 -1
  115. package/src/tools/pdfReader.js +122 -20
  116. package/src/tools/pdfReader.ts +111 -20
  117. package/src/tools/presentDeliverable.js +103 -0
  118. package/src/tools/presentDeliverable.ts +127 -0
  119. package/src/tools/ptcRuntime.js +444 -0
  120. package/src/tools/ptcRuntime.ts +490 -0
  121. package/src/tools/ptyTerminal.js +1108 -0
  122. package/src/tools/ptyTerminal.ts +1245 -0
  123. package/src/tools/registry.js +21 -12
  124. package/src/tools/registry.ts +21 -12
  125. package/src/tools/toolOutputStore.js +26 -4
  126. package/src/tools/toolOutputStore.ts +22 -4
  127. package/src/tools/webFetch.js +158 -11
  128. package/src/tools/webFetch.ts +145 -11
  129. package/src/tools/webSearch.js +123 -36
  130. package/src/tools/webSearch.ts +124 -33
  131. package/src/tui/banner.js +5 -1
  132. package/src/tui/banner.ts +6 -1
  133. package/src/tui/chatRenderer.js +1 -2
  134. package/src/tui/chatRenderer.ts +1 -2
  135. package/src/tui/completion.js +1 -0
  136. package/src/tui/completion.ts +1 -0
  137. package/src/tui/composerControls.js +111 -27
  138. package/src/tui/composerControls.ts +124 -28
  139. package/src/tui/dialogs.js +3 -16
  140. package/src/tui/dialogs.ts +3 -16
  141. package/src/tui/home.js +1 -0
  142. package/src/tui/home.ts +1 -0
  143. package/src/tui/invocation.js +1 -2
  144. package/src/tui/invocation.ts +1 -2
  145. package/src/tui/modelPicker.js +22 -12
  146. package/src/tui/modelPicker.ts +22 -12
  147. package/src/tui/overlay.js +2 -5
  148. package/src/tui/overlay.ts +2 -5
  149. package/src/tui/prompt.js +6 -4
  150. package/src/tui/prompt.ts +6 -4
  151. package/src/tui/selectList.js +4 -12
  152. package/src/tui/selectList.ts +4 -12
  153. package/src/tui/setup.js +22 -9
  154. package/src/tui/setup.ts +26 -9
  155. package/src/tui/statusLine.ts +2 -0
  156. package/src/web/client/browserPanel.js +905 -0
  157. package/src/web/client/browserPanel.ts +872 -0
  158. package/src/web/client/chatStream.js +321 -0
  159. package/src/web/client/chatStream.ts +297 -0
  160. package/src/web/client/cliPanel.js +165 -0
  161. package/src/web/client/cliPanel.ts +171 -0
  162. package/src/web/client/composer.js +204 -0
  163. package/src/web/client/composer.ts +209 -0
  164. package/src/web/client/diffPanel.js +176 -0
  165. package/src/web/client/diffPanel.ts +203 -0
  166. package/src/web/client/dom.js +271 -0
  167. package/src/web/client/dom.ts +272 -0
  168. package/src/web/client/filesAndCanvas.js +233 -0
  169. package/src/web/client/filesAndCanvas.ts +240 -0
  170. package/src/web/client/gameLauncherUi.js +78 -0
  171. package/src/web/client/gameLauncherUi.ts +78 -0
  172. package/src/web/client/main.js +316 -0
  173. package/src/web/client/main.ts +391 -0
  174. package/src/web/client/markdown.js +364 -0
  175. package/src/web/client/markdown.ts +373 -0
  176. package/src/web/client/modelPicker.js +455 -0
  177. package/src/web/client/modelPicker.ts +451 -0
  178. package/src/web/client/permissionPresetsUi.js +307 -0
  179. package/src/web/client/permissionPresetsUi.ts +333 -0
  180. package/src/web/client/sessions.js +353 -0
  181. package/src/web/client/sessions.ts +372 -0
  182. package/src/web/client/settingsModal.js +824 -0
  183. package/src/web/client/settingsModal.ts +959 -0
  184. package/src/web/client/state.js +280 -0
  185. package/src/web/client/state.ts +259 -0
  186. package/src/web/client/streamSmoother.js +127 -0
  187. package/src/web/client/streamSmoother.ts +119 -0
  188. package/src/web/client/studioLayout.js +89 -0
  189. package/src/web/client/studioLayout.ts +103 -0
  190. package/src/web/client/studioShell.js +660 -0
  191. package/src/web/client/studioShell.ts +672 -0
  192. package/src/web/client/studioStart.js +77 -0
  193. package/src/web/client/studioStart.ts +89 -0
  194. package/src/web/client/terminalPanel.js +606 -0
  195. package/src/web/client/terminalPanel.ts +632 -0
  196. package/src/web/client/terminalTheme.js +249 -0
  197. package/src/web/client/terminalTheme.ts +258 -0
  198. package/src/web/client/turnView.js +934 -0
  199. package/src/web/client/turnView.ts +981 -0
  200. package/src/web/client/types.js +1 -0
  201. package/src/web/client/types.ts +322 -0
  202. package/src/web/client/validators.js +147 -0
  203. package/src/web/client/validators.ts +140 -0
  204. package/src/web/server/activeRuns.js +152 -0
  205. package/src/web/server/activeRuns.ts +171 -0
  206. package/src/web/server/routesBrowser.js +409 -0
  207. package/src/web/server/routesBrowser.ts +435 -0
  208. package/src/web/server/routesChat.js +304 -0
  209. package/src/web/server/routesChat.ts +347 -0
  210. package/src/web/server/routesCore.js +472 -0
  211. package/src/web/server/routesCore.ts +522 -0
  212. package/src/web/server/routesSettings.js +344 -0
  213. package/src/web/server/routesSettings.ts +397 -0
  214. package/src/web/server/routesTerminal.js +162 -0
  215. package/src/web/server/routesTerminal.ts +182 -0
  216. package/src/web/server/routesWorkspace.js +510 -0
  217. package/src/web/server/routesWorkspace.ts +546 -0
  218. package/src/web/server/security.js +98 -0
  219. package/src/web/server/security.ts +108 -0
  220. package/src/web/server/sessionTranscript.js +586 -0
  221. package/src/web/server/sessionTranscript.ts +566 -0
  222. package/src/web/server/staticAssets.js +104 -0
  223. package/src/web/server/staticAssets.ts +116 -0
  224. package/src/web/server/types.js +1 -0
  225. package/src/web/server/types.ts +135 -0
  226. package/src/web/server/workspaceFiles.js +368 -0
  227. package/src/web/server/workspaceFiles.ts +363 -0
  228. package/src/web/skybridgeServer.js +78 -2448
  229. package/src/web/skybridgeServer.ts +120 -2600
  230. package/src/web/skybridgeUi.js +20 -450
  231. package/src/web/skybridgeUi.ts +24 -455
  232. package/src/web/styles/agent.css +380 -0
  233. package/src/web/styles/composer.css +193 -0
  234. package/src/web/styles/overlays.css +935 -0
  235. package/src/web/styles/shell.css +387 -0
  236. package/src/web/styles/studio.css +951 -0
  237. package/src/web/styles/thread.css +221 -0
  238. package/src/web/styles/tokens.css +94 -0
  239. package/src/web/ui/brandMark.js +73 -0
  240. package/src/web/ui/brandMark.ts +82 -0
  241. package/src/web/ui/modalsTemplate.js +388 -0
  242. package/src/web/ui/modalsTemplate.ts +388 -0
  243. package/src/web/ui/sidebarTemplate.js +63 -0
  244. package/src/web/ui/sidebarTemplate.ts +64 -0
  245. package/src/web/ui/studioTemplate.js +256 -0
  246. package/src/web/ui/studioTemplate.ts +256 -0
  247. package/src/web/ui/workspaceTemplate.js +72 -0
  248. package/src/web/ui/workspaceTemplate.ts +73 -0
  249. package/src/worktreeManager.js +16 -1
  250. package/src/worktreeManager.ts +22 -1
  251. package/src/web/skybridgeClientScript.js +0 -3951
  252. package/src/web/skybridgeClientScript.ts +0 -3952
  253. package/src/web/skybridgeStyles.js +0 -827
  254. package/src/web/skybridgeStyles.ts +0 -828
@@ -0,0 +1,344 @@
1
+ import path from 'node:path';
2
+ import { loadConfig, resolveProviderApiKey, saveConfig } from '../../config.js';
3
+ import { DEFAULT_MODEL_BY_PROVIDER } from '../../modelCatalogue.js';
4
+ import { getDefaultPermissionPreset, normalizePermissionPreset, setDefaultPermissionPreset } from '../../permissionPresets.js';
5
+ import { probeOsSandbox, resolveOsSandboxMode } from '../../tools/osSandbox.js';
6
+ import { deleteCausyneMemories, readPersonalizationOverview, saveMemoryPreferences, savePersonalizationInstructions } from '../../personalization.js';
7
+ import { discoverProjectMcpServers, serverStatus } from '../../mcp/mcpRegistry.js';
8
+ import { disablePlugin, enablePlugin, listPlugins, PLUGIN_ROOT } from '../../plugins/pluginManager.js';
9
+ import { auditLog } from '../../auditLog.js';
10
+ import { readJsonBody, sendJson } from './security.js';
11
+ const VAULT_PROVIDER_IDS = [
12
+ 'deepseek',
13
+ 'qwen',
14
+ 'dashscope-byok',
15
+ 'anthropic',
16
+ 'openai',
17
+ 'gemini',
18
+ 'moonshot',
19
+ 'xai',
20
+ 'openrouter',
21
+ 'groq',
22
+ 'cerebras',
23
+ 'mistral'
24
+ ];
25
+ const ALLOWED_OS_SANDBOX_MODES = new Set([
26
+ 'off',
27
+ 'workspace-write',
28
+ 'strict',
29
+ 'read-only',
30
+ 'read-only-strict'
31
+ ]);
32
+ const ALLOWED_EFFORT_LEVELS = new Set(['', 'off', 'low', 'medium', 'high', 'max']);
33
+ function buildHooksOverview(config) {
34
+ const rawHooks = Array.isArray(config.hooks) ? config.hooks : [];
35
+ const items = [];
36
+ for (let i = 0; i < rawHooks.length && i < 64; i++) {
37
+ const entry = rawHooks[i];
38
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry))
39
+ continue;
40
+ const rec = entry;
41
+ const event = typeof rec.event === 'string' && rec.event.trim() ? rec.event.trim() : 'PreToolUse';
42
+ const tools = Array.isArray(rec.tools)
43
+ ? rec.tools.filter((t) => typeof t === 'string' && t.trim().length > 0)
44
+ : [typeof rec.tool === 'string' && rec.tool.trim() ? rec.tool.trim() : '*'];
45
+ const command = Array.isArray(rec.command)
46
+ ? rec.command.map((c) => String(c)).join(' ')
47
+ : '';
48
+ const name = typeof rec.name === 'string' && rec.name.trim()
49
+ ? rec.name.trim().slice(0, 120)
50
+ : `${event}:${tools[0] || '*'}`;
51
+ const timeoutMs = typeof rec.timeoutMs === 'number' && Number.isFinite(rec.timeoutMs)
52
+ ? Math.max(100, Math.min(10_000, Math.round(rec.timeoutMs)))
53
+ : 5_000;
54
+ items.push({
55
+ index: i,
56
+ name,
57
+ event,
58
+ tools,
59
+ commandPreview: command.slice(0, 180),
60
+ timeoutMs,
61
+ enabled: rec.enabled !== false
62
+ });
63
+ }
64
+ return items;
65
+ }
66
+ function buildSettingsOverviewPayload(ctx) {
67
+ const config = loadConfig();
68
+ const osSandboxCap = probeOsSandbox();
69
+ const osSandboxResolved = resolveOsSandboxMode(process.env, config.osSandbox);
70
+ const configuredOsSandbox = typeof config.osSandbox === 'string' && ALLOWED_OS_SANDBOX_MODES.has(config.osSandbox.trim().toLowerCase())
71
+ ? config.osSandbox.trim().toLowerCase()
72
+ : 'workspace-write';
73
+ const rawBudget = config.tokenBudget && typeof config.tokenBudget === 'object' && !Array.isArray(config.tokenBudget)
74
+ ? config.tokenBudget
75
+ : {};
76
+ const tokenBudget = {
77
+ enabled: rawBudget.enabled === true,
78
+ maxTokensPerTask: typeof rawBudget.maxTokensPerTask === 'number' && Number.isFinite(rawBudget.maxTokensPerTask)
79
+ ? Math.max(1000, Math.round(rawBudget.maxTokensPerTask))
80
+ : 150000,
81
+ maxTokensPerDay: typeof rawBudget.maxTokensPerDay === 'number' && Number.isFinite(rawBudget.maxTokensPerDay)
82
+ ? Math.max(1000, Math.round(rawBudget.maxTokensPerDay))
83
+ : 1000000,
84
+ maxCostUsd: typeof rawBudget.maxCostUsd === 'number' && Number.isFinite(rawBudget.maxCostUsd)
85
+ ? Math.max(0.1, Number(rawBudget.maxCostUsd))
86
+ : 5,
87
+ hardStop: rawBudget.hardStop === true
88
+ };
89
+ const rawCompact = config.contextCompaction && typeof config.contextCompaction === 'object' && !Array.isArray(config.contextCompaction)
90
+ ? config.contextCompaction
91
+ : {};
92
+ const autoCompact = {
93
+ enabled: rawCompact.enabled !== false,
94
+ thresholdPercent: typeof rawCompact.thresholdPercent === 'number' && Number.isFinite(rawCompact.thresholdPercent)
95
+ ? Math.max(40, Math.min(95, Math.round(rawCompact.thresholdPercent)))
96
+ : 78
97
+ };
98
+ const configuredProviders = VAULT_PROVIDER_IDS.filter((id) => Boolean(resolveProviderApiKey(config, id)));
99
+ const projectMcp = discoverProjectMcpServers(ctx.workspaceRoot);
100
+ let pluginItems = [];
101
+ try {
102
+ pluginItems = listPlugins();
103
+ }
104
+ catch {
105
+ pluginItems = [];
106
+ }
107
+ return {
108
+ ok: true,
109
+ general: {
110
+ workspaceRoot: ctx.workspaceRoot,
111
+ workspaceName: path.basename(ctx.workspaceRoot),
112
+ version: ctx.version,
113
+ platform: process.platform,
114
+ provider: config.provider || 'unconfigured',
115
+ model: config.model || DEFAULT_MODEL_BY_PROVIDER[String(config.provider || '')] || 'default'
116
+ },
117
+ configuration: {
118
+ defaultPermissionPreset: getDefaultPermissionPreset(),
119
+ reasoningEffort: typeof config.reasoningEffort === 'string' ? config.reasoningEffort.trim().toLowerCase() : '',
120
+ osSandbox: {
121
+ configuredMode: configuredOsSandbox,
122
+ effectiveMode: osSandboxResolved.mode,
123
+ strict: osSandboxResolved.strict,
124
+ allowNetwork: osSandboxResolved.allowNetwork,
125
+ backend: osSandboxCap.backend,
126
+ available: osSandboxCap.available,
127
+ reason: osSandboxCap.reason
128
+ },
129
+ tokenBudget,
130
+ autoCompact
131
+ },
132
+ personalization: readPersonalizationOverview(ctx.workspaceRoot),
133
+ vault: {
134
+ activeProvider: config.provider || '',
135
+ configuredProviders
136
+ },
137
+ mcp: {
138
+ servers: serverStatus(),
139
+ projectDiscovered: projectMcp.servers.map((srv) => ({
140
+ name: srv.name,
141
+ transport: srv.url ? 'http' : 'stdio',
142
+ target: srv.url ?? [srv.command, ...(srv.args ?? [])].filter(Boolean).join(' ')
143
+ })),
144
+ projectWarnings: projectMcp.warnings,
145
+ projectFile: projectMcp.filePath
146
+ },
147
+ hooks: buildHooksOverview(config),
148
+ plugins: {
149
+ root: PLUGIN_ROOT,
150
+ items: pluginItems
151
+ }
152
+ };
153
+ }
154
+ function invalidateActiveLoopStaticContexts(ctx) {
155
+ for (const run of ctx.activeRuns.values()) {
156
+ const loopObj = run.loop;
157
+ if (loopObj && typeof loopObj === 'object' && 'staticContext' in loopObj) {
158
+ loopObj.staticContext = null;
159
+ }
160
+ }
161
+ }
162
+ export async function handleSettingsRoutes(req, res, parsedUrl, ctx) {
163
+ const pathname = parsedUrl.pathname;
164
+ if (req.method === 'GET' && pathname === '/api/settings/overview') {
165
+ sendJson(res, 200, buildSettingsOverviewPayload(ctx));
166
+ return true;
167
+ }
168
+ if (req.method === 'POST' && pathname === '/api/settings/configuration') {
169
+ const body = await readJsonBody(req);
170
+ const config = loadConfig();
171
+ if (body.defaultPermissionPreset !== undefined) {
172
+ const preset = normalizePermissionPreset(body.defaultPermissionPreset);
173
+ if (!preset) {
174
+ sendJson(res, 400, { error: 'Invalid defaultPermissionPreset value.' });
175
+ return true;
176
+ }
177
+ setDefaultPermissionPreset(preset);
178
+ config.defaultPermissionPreset = preset;
179
+ }
180
+ if (typeof body.osSandbox === 'string') {
181
+ const mode = body.osSandbox.trim().toLowerCase();
182
+ if (!ALLOWED_OS_SANDBOX_MODES.has(mode)) {
183
+ sendJson(res, 400, { error: 'Invalid osSandbox mode.' });
184
+ return true;
185
+ }
186
+ config.osSandbox = mode;
187
+ }
188
+ if (typeof body.reasoningEffort === 'string') {
189
+ const effort = body.reasoningEffort.trim().toLowerCase();
190
+ if (!ALLOWED_EFFORT_LEVELS.has(effort)) {
191
+ sendJson(res, 400, { error: 'Invalid reasoningEffort level.' });
192
+ return true;
193
+ }
194
+ if (!effort)
195
+ delete config.reasoningEffort;
196
+ else
197
+ config.reasoningEffort = effort;
198
+ }
199
+ if (body.tokenBudget && typeof body.tokenBudget === 'object' && !Array.isArray(body.tokenBudget)) {
200
+ const raw = body.tokenBudget;
201
+ const prev = config.tokenBudget && typeof config.tokenBudget === 'object'
202
+ ? config.tokenBudget
203
+ : {};
204
+ const nextBudget = { ...prev };
205
+ if (typeof raw.enabled === 'boolean')
206
+ nextBudget.enabled = raw.enabled;
207
+ if (typeof raw.hardStop === 'boolean')
208
+ nextBudget.hardStop = raw.hardStop;
209
+ if (typeof raw.maxTokensPerTask === 'number' && Number.isFinite(raw.maxTokensPerTask)) {
210
+ nextBudget.maxTokensPerTask = Math.max(1000, Math.min(100_000_000, Math.round(raw.maxTokensPerTask)));
211
+ }
212
+ if (typeof raw.maxTokensPerDay === 'number' && Number.isFinite(raw.maxTokensPerDay)) {
213
+ nextBudget.maxTokensPerDay = Math.max(1000, Math.min(1_000_000_000, Math.round(raw.maxTokensPerDay)));
214
+ }
215
+ if (typeof raw.maxCostUsd === 'number' && Number.isFinite(raw.maxCostUsd)) {
216
+ nextBudget.maxCostUsd = Math.max(0.1, Math.min(10_000, Number(raw.maxCostUsd.toFixed(2))));
217
+ }
218
+ config.tokenBudget = nextBudget;
219
+ }
220
+ if (body.autoCompact && typeof body.autoCompact === 'object' && !Array.isArray(body.autoCompact)) {
221
+ const raw = body.autoCompact;
222
+ const prev = config.contextCompaction && typeof config.contextCompaction === 'object'
223
+ ? config.contextCompaction
224
+ : {};
225
+ const nextCompact = { ...prev };
226
+ if (typeof raw.enabled === 'boolean')
227
+ nextCompact.enabled = raw.enabled;
228
+ if (typeof raw.thresholdPercent === 'number' && Number.isFinite(raw.thresholdPercent)) {
229
+ nextCompact.thresholdPercent = Math.max(40, Math.min(95, Math.round(raw.thresholdPercent)));
230
+ }
231
+ config.contextCompaction = nextCompact;
232
+ }
233
+ saveConfig(config);
234
+ auditLog.log({ event: 'skybridge_configuration_updated' });
235
+ sendJson(res, 200, buildSettingsOverviewPayload(ctx));
236
+ return true;
237
+ }
238
+ if (req.method === 'POST' && pathname === '/api/settings/personalization') {
239
+ const body = await readJsonBody(req);
240
+ const scope = body.scope === 'global' ? 'global' : 'project';
241
+ try {
242
+ if (typeof body.instructions === 'string') {
243
+ savePersonalizationInstructions(ctx.workspaceRoot, scope, body.instructions);
244
+ }
245
+ const memPatch = {};
246
+ if (typeof body.memoryEnabled === 'boolean' || body.memoryEnabled === null) {
247
+ memPatch.enabled = body.memoryEnabled;
248
+ }
249
+ if (typeof body.allowToolAssisted === 'boolean' || body.allowToolAssisted === null) {
250
+ memPatch.allowToolAssisted = body.allowToolAssisted;
251
+ }
252
+ if (memPatch.enabled !== undefined || memPatch.allowToolAssisted !== undefined) {
253
+ saveMemoryPreferences(ctx.workspaceRoot, scope, memPatch);
254
+ }
255
+ invalidateActiveLoopStaticContexts(ctx);
256
+ auditLog.log({
257
+ event: 'skybridge_personalization_updated',
258
+ scope,
259
+ updatedInstructions: typeof body.instructions === 'string',
260
+ updatedMemory: memPatch.enabled !== undefined || memPatch.allowToolAssisted !== undefined
261
+ });
262
+ sendJson(res, 200, {
263
+ ok: true,
264
+ scope,
265
+ personalization: readPersonalizationOverview(ctx.workspaceRoot)
266
+ });
267
+ return true;
268
+ }
269
+ catch (err) {
270
+ const rawMsg = err instanceof Error ? err.message : '';
271
+ const safeMsg = rawMsg.includes('exceed maximum size')
272
+ ? 'Personalization instructions exceed the 16 KB limit.'
273
+ : rawMsg.includes('symlink')
274
+ ? 'Refusing to write personalization through a symbolic link.'
275
+ : 'Could not save personalization settings.';
276
+ sendJson(res, 400, { error: safeMsg });
277
+ return true;
278
+ }
279
+ }
280
+ if (req.method === 'POST' && pathname === '/api/settings/memories/delete') {
281
+ const body = await readJsonBody(req);
282
+ const scope = body.scope === 'all' ? 'all' : 'project';
283
+ const result = deleteCausyneMemories(ctx.workspaceRoot, scope);
284
+ invalidateActiveLoopStaticContexts(ctx);
285
+ auditLog.log({
286
+ event: 'skybridge_memories_deleted',
287
+ scope,
288
+ factsRemoved: result.factsRemoved,
289
+ observationsRemoved: result.observationsRemoved,
290
+ summariesRemoved: result.summariesRemoved
291
+ });
292
+ sendJson(res, 200, {
293
+ ok: true,
294
+ ...result,
295
+ personalization: readPersonalizationOverview(ctx.workspaceRoot)
296
+ });
297
+ return true;
298
+ }
299
+ if (req.method === 'POST' && pathname === '/api/settings/hooks/toggle') {
300
+ const body = await readJsonBody(req);
301
+ const index = typeof body.index === 'number' && Number.isInteger(body.index) ? body.index : -1;
302
+ const enabled = typeof body.enabled === 'boolean' ? body.enabled : null;
303
+ if (index < 0 || enabled === null) {
304
+ sendJson(res, 400, { error: 'Valid hook index and boolean enabled flag are required.' });
305
+ return true;
306
+ }
307
+ const config = loadConfig();
308
+ if (!Array.isArray(config.hooks) || index >= config.hooks.length) {
309
+ sendJson(res, 404, { error: 'Configured hook entry not found.' });
310
+ return true;
311
+ }
312
+ const target = config.hooks[index];
313
+ if (!target || typeof target !== 'object' || Array.isArray(target)) {
314
+ sendJson(res, 400, { error: 'Hook entry is malformed.' });
315
+ return true;
316
+ }
317
+ target.enabled = enabled;
318
+ saveConfig(config);
319
+ auditLog.log({ event: 'skybridge_hook_toggled', index, enabled });
320
+ sendJson(res, 200, { ok: true, hooks: buildHooksOverview(config) });
321
+ return true;
322
+ }
323
+ if (req.method === 'POST' && pathname === '/api/settings/plugins/toggle') {
324
+ const body = await readJsonBody(req);
325
+ const name = typeof body.name === 'string' ? body.name.trim() : '';
326
+ const enabled = typeof body.enabled === 'boolean' ? body.enabled : null;
327
+ if (!name || enabled === null) {
328
+ sendJson(res, 400, { error: 'Valid plugin name and boolean enabled flag are required.' });
329
+ return true;
330
+ }
331
+ const outcome = enabled ? enablePlugin(name) : disablePlugin(name);
332
+ if (!outcome.ok) {
333
+ sendJson(res, 400, { error: 'Could not update plugin trust state.' });
334
+ return true;
335
+ }
336
+ auditLog.log({ event: 'skybridge_plugin_toggled', name, enabled });
337
+ sendJson(res, 200, {
338
+ ok: true,
339
+ plugins: { root: PLUGIN_ROOT, items: listPlugins() }
340
+ });
341
+ return true;
342
+ }
343
+ return false;
344
+ }