@neuralnomads/codenomad 0.12.3 → 0.13.3
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.
- package/dist/auth/manager.js +9 -1
- package/dist/clients/connection-manager.js +93 -0
- package/dist/filesystem/browser.js +7 -0
- package/dist/index.js +10 -1
- package/dist/opencode-config/package.json +1 -1
- package/dist/opencode-config/plugin/codenomad.ts +30 -0
- package/dist/plugins/voice-mode.js +77 -0
- package/dist/server/http-server.js +26 -2
- package/dist/server/routes/events.js +24 -1
- package/dist/server/routes/plugin.js +18 -4
- package/dist/server/routes/settings.js +5 -4
- package/dist/server/routes/speech.js +63 -0
- package/dist/server/routes/workspaces.js +14 -0
- package/dist/settings/public-config.js +33 -0
- package/dist/settings/service.js +3 -1
- package/dist/speech/providers/openai-compatible.js +189 -0
- package/dist/speech/service.js +58 -0
- package/dist/ui/__tests__/remote-ui.test.js +21 -0
- package/dist/ui/remote-ui.js +2 -2
- package/dist/workspaces/manager.js +5 -0
- package/package.json +2 -1
- package/public/assets/ChangesTab-X8IIoIdB.js +2 -0
- package/public/assets/DiffToolbar-Djg0ksr_.js +1 -0
- package/public/assets/FilesTab-BpYPA_Zr.js +2 -0
- package/public/assets/GitChangesTab-DeGsV8--.js +2 -0
- package/public/assets/SplitFilePanel-DwrJM7GQ.js +1 -0
- package/public/assets/StatusTab-mT1B3wST.js +1 -0
- package/public/assets/bundle-full-JwmTIwMd.js +13 -0
- package/public/assets/diff-viewer-CKnTxbWB.js +1 -0
- package/public/assets/fast-diff-vendor-DgdwVvTQ.js +1 -0
- package/public/assets/git-diff-vendor-CAv-4upN.js +52 -0
- package/public/assets/highlight-vendor-8FKMu9os.js +10 -0
- package/public/assets/index-BEzx5r3P.css +1 -0
- package/public/assets/index-BHjkPi4D.js +1 -0
- package/public/assets/index-CUkIenrg.js +1 -0
- package/public/assets/index-CkYwnPl7.js +1 -0
- package/public/assets/index-D9RMxxcS.js +1 -0
- package/public/assets/index-DiFfJ0HM.js +1 -0
- package/public/assets/index-LmDdmmJP.js +1 -0
- package/public/assets/index-UaIyK67v.js +1 -0
- package/public/assets/index-lhjxIgwf.js +2 -0
- package/public/assets/index-xsUu1uOf.js +1 -0
- package/public/assets/loading-CfqkoCbA.js +1 -0
- package/public/assets/main-B3yZBpaS.js +56 -0
- package/public/assets/markdown-D2uUJada.js +58 -0
- package/public/assets/monaco-viewer-UU9TfOpS.js +15 -0
- package/public/assets/todo-i3zWdksA.js +1 -0
- package/public/assets/tool-call-Bl3Lai68.js +60 -0
- package/public/assets/unified-picker-B506AoQt.js +1 -0
- package/public/index.html +8 -4
- package/public/loading.html +8 -3
- package/public/sw.js +1 -1
- package/public/ui-version.json +1 -1
- package/public/assets/core-BSTVzpXI.js +0 -1
- package/public/assets/index-BirdJGwS.css +0 -1
- package/public/assets/index-D4PT0yE4.js +0 -1
- package/public/assets/index-DN20ggb1.js +0 -1
- package/public/assets/index-DdQ7zIzB.js +0 -1
- package/public/assets/index-sBSImhXZ.js +0 -1
- package/public/assets/loading-JRnhbEyO.js +0 -1
- package/public/assets/main-9SBWunUk.js +0 -250
- /package/public/assets/{main-HAZkIolJ.css → git-diff-vendor-HAZkIolJ.css} +0 -0
package/dist/auth/manager.js
CHANGED
|
@@ -11,7 +11,7 @@ export class AuthManager {
|
|
|
11
11
|
this.init = init;
|
|
12
12
|
this.logger = logger;
|
|
13
13
|
this.sessionManager = new SessionManager();
|
|
14
|
-
this.cookieName =
|
|
14
|
+
this.cookieName = sanitizeCookieName(init.cookieName);
|
|
15
15
|
this.authEnabled = !Boolean(init.dangerouslySkipAuth);
|
|
16
16
|
if (!this.authEnabled) {
|
|
17
17
|
this.authStore = null;
|
|
@@ -106,6 +106,14 @@ export class AuthManager {
|
|
|
106
106
|
return this.authStore;
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
+
function sanitizeCookieName(value) {
|
|
110
|
+
const trimmed = value?.trim();
|
|
111
|
+
if (!trimmed) {
|
|
112
|
+
return DEFAULT_AUTH_COOKIE_NAME;
|
|
113
|
+
}
|
|
114
|
+
const sanitized = trimmed.replace(/[^A-Za-z0-9_-]/g, "_");
|
|
115
|
+
return sanitized.length > 0 ? sanitized : DEFAULT_AUTH_COOKIE_NAME;
|
|
116
|
+
}
|
|
109
117
|
function resolveAuthFilePath(configPath) {
|
|
110
118
|
const resolvedConfigPath = resolvePath(configPath);
|
|
111
119
|
return path.join(path.dirname(resolvedConfigPath), "auth.json");
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const STALE_CONNECTION_TIMEOUT_MS = 45000;
|
|
2
|
+
const STALE_SWEEP_INTERVAL_MS = 5000;
|
|
3
|
+
export class ClientConnectionManager {
|
|
4
|
+
constructor(logger) {
|
|
5
|
+
this.logger = logger;
|
|
6
|
+
this.connections = new Map();
|
|
7
|
+
this.subscribers = new Set();
|
|
8
|
+
this.sweepTimer = setInterval(() => this.sweepStaleConnections(), STALE_SWEEP_INTERVAL_MS);
|
|
9
|
+
this.sweepTimer.unref?.();
|
|
10
|
+
}
|
|
11
|
+
shutdown() {
|
|
12
|
+
clearInterval(this.sweepTimer);
|
|
13
|
+
for (const connection of Array.from(this.connections.values())) {
|
|
14
|
+
this.disconnect(connection.key, "shutdown", false);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
subscribe(listener) {
|
|
18
|
+
this.subscribers.add(listener);
|
|
19
|
+
return () => this.subscribers.delete(listener);
|
|
20
|
+
}
|
|
21
|
+
register(input) {
|
|
22
|
+
const key = getConnectionKey(input);
|
|
23
|
+
const now = Date.now();
|
|
24
|
+
const existing = this.connections.get(key);
|
|
25
|
+
if (existing) {
|
|
26
|
+
this.logger.debug({ clientId: input.clientId, connectionId: input.connectionId }, "Replacing existing client connection");
|
|
27
|
+
this.disconnect(key, "replaced");
|
|
28
|
+
}
|
|
29
|
+
const connection = {
|
|
30
|
+
key,
|
|
31
|
+
clientId: input.clientId,
|
|
32
|
+
connectionId: input.connectionId,
|
|
33
|
+
connectedAt: now,
|
|
34
|
+
lastSeenAt: now,
|
|
35
|
+
close: input.close,
|
|
36
|
+
};
|
|
37
|
+
this.connections.set(key, connection);
|
|
38
|
+
this.logger.debug({ clientId: input.clientId, connectionId: input.connectionId }, "Client connected");
|
|
39
|
+
this.notify({ type: "connected", connection });
|
|
40
|
+
return () => this.disconnect(key, "closed");
|
|
41
|
+
}
|
|
42
|
+
pong(input) {
|
|
43
|
+
const key = getConnectionKey(input);
|
|
44
|
+
const connection = this.connections.get(key);
|
|
45
|
+
if (!connection) {
|
|
46
|
+
this.logger.debug({ clientId: input.clientId, connectionId: input.connectionId }, "Ignoring pong for unknown client connection");
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
connection.lastSeenAt = Date.now();
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
isConnected(input) {
|
|
53
|
+
return this.connections.has(getConnectionKey(input));
|
|
54
|
+
}
|
|
55
|
+
sweepStaleConnections() {
|
|
56
|
+
const cutoff = Date.now() - STALE_CONNECTION_TIMEOUT_MS;
|
|
57
|
+
for (const connection of Array.from(this.connections.values())) {
|
|
58
|
+
if (connection.lastSeenAt > cutoff)
|
|
59
|
+
continue;
|
|
60
|
+
this.logger.debug({ clientId: connection.clientId, connectionId: connection.connectionId }, "Client connection timed out");
|
|
61
|
+
this.disconnect(connection.key, "timeout");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
disconnect(key, reason, invokeClose = true) {
|
|
65
|
+
const connection = this.connections.get(key);
|
|
66
|
+
if (!connection)
|
|
67
|
+
return;
|
|
68
|
+
this.connections.delete(key);
|
|
69
|
+
this.logger.debug({ clientId: connection.clientId, connectionId: connection.connectionId, reason }, "Client disconnected");
|
|
70
|
+
if (invokeClose) {
|
|
71
|
+
try {
|
|
72
|
+
connection.close();
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
this.logger.warn({ err: error, clientId: connection.clientId, connectionId: connection.connectionId }, "Failed to close stale client connection");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
this.notify({ type: "disconnected", connection, reason });
|
|
79
|
+
}
|
|
80
|
+
notify(event) {
|
|
81
|
+
for (const subscriber of this.subscribers) {
|
|
82
|
+
try {
|
|
83
|
+
subscriber(event);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
this.logger.warn({ err: error, eventType: event.type }, "Client connection subscriber failed");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function getConnectionKey(input) {
|
|
92
|
+
return `${input.clientId}:${input.connectionId}`;
|
|
93
|
+
}
|
|
@@ -50,6 +50,13 @@ export class FileSystemBrowser {
|
|
|
50
50
|
fs.mkdirSync(absolutePath);
|
|
51
51
|
return { path: relativePath, absolutePath };
|
|
52
52
|
}
|
|
53
|
+
writeFile(relativePath, contents) {
|
|
54
|
+
if (this.unrestricted) {
|
|
55
|
+
throw new Error("writeFile is not available in unrestricted mode");
|
|
56
|
+
}
|
|
57
|
+
const resolved = this.toRestrictedAbsolute(relativePath);
|
|
58
|
+
fs.writeFileSync(resolved, contents, "utf-8");
|
|
59
|
+
}
|
|
53
60
|
readFile(relativePath) {
|
|
54
61
|
if (this.unrestricted) {
|
|
55
62
|
throw new Error("readFile is not available in unrestricted mode");
|
package/dist/index.js
CHANGED
|
@@ -18,10 +18,11 @@ import { InstanceEventBridge } from "./workspaces/instance-events";
|
|
|
18
18
|
import { createLogger } from "./logger";
|
|
19
19
|
import { launchInBrowser } from "./launcher";
|
|
20
20
|
import { resolveUi } from "./ui/remote-ui";
|
|
21
|
-
import { AuthManager, BOOTSTRAP_TOKEN_STDOUT_PREFIX, DEFAULT_AUTH_USERNAME } from "./auth/manager";
|
|
21
|
+
import { AuthManager, BOOTSTRAP_TOKEN_STDOUT_PREFIX, DEFAULT_AUTH_COOKIE_NAME, DEFAULT_AUTH_USERNAME } from "./auth/manager";
|
|
22
22
|
import { resolveHttpsOptions } from "./server/tls";
|
|
23
23
|
import { resolveNetworkAddresses } from "./server/network-addresses";
|
|
24
24
|
import { startDevReleaseMonitor } from "./releases/dev-release-monitor";
|
|
25
|
+
import { SpeechService } from "./speech/service";
|
|
25
26
|
const require = createRequire(import.meta.url);
|
|
26
27
|
const packageJson = require("../package.json");
|
|
27
28
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -61,6 +62,9 @@ function parseCliOptions(argv) {
|
|
|
61
62
|
.env("CODENOMAD_SERVER_USERNAME")
|
|
62
63
|
.default(DEFAULT_AUTH_USERNAME))
|
|
63
64
|
.addOption(new Option("--password <password>", "Password for server authentication").env("CODENOMAD_SERVER_PASSWORD"))
|
|
65
|
+
.addOption(new Option("--auth-cookie-name <name>", "Cookie name for server authentication")
|
|
66
|
+
.env("CODENOMAD_AUTH_COOKIE_NAME")
|
|
67
|
+
.default(DEFAULT_AUTH_COOKIE_NAME))
|
|
64
68
|
.addOption(new Option("--generate-token", "Emit a one-time bootstrap token for desktop")
|
|
65
69
|
.env("CODENOMAD_GENERATE_TOKEN")
|
|
66
70
|
.default(false))
|
|
@@ -105,6 +109,7 @@ function parseCliOptions(argv) {
|
|
|
105
109
|
launch: Boolean(parsed.launch),
|
|
106
110
|
authUsername: parsed.username,
|
|
107
111
|
authPassword: parsed.password,
|
|
112
|
+
authCookieName: parsed.authCookieName,
|
|
108
113
|
generateToken: Boolean(parsed.generateToken),
|
|
109
114
|
dangerouslySkipAuth: Boolean(parsed.dangerouslySkipAuth),
|
|
110
115
|
};
|
|
@@ -168,6 +173,7 @@ async function main() {
|
|
|
168
173
|
configPath: configLocation.configYamlPath,
|
|
169
174
|
username: options.authUsername,
|
|
170
175
|
password: options.authPassword,
|
|
176
|
+
cookieName: options.authCookieName,
|
|
171
177
|
generateToken: options.generateToken,
|
|
172
178
|
dangerouslySkipAuth: options.dangerouslySkipAuth,
|
|
173
179
|
}, logger.child({ component: "auth" }));
|
|
@@ -201,6 +207,7 @@ async function main() {
|
|
|
201
207
|
});
|
|
202
208
|
const fileSystemBrowser = new FileSystemBrowser({ rootDir: options.rootDir, unrestricted: options.unrestrictedRoot });
|
|
203
209
|
const instanceStore = new InstanceStore(configLocation.instancesDir);
|
|
210
|
+
const speechService = new SpeechService(settings, logger.child({ component: "speech" }));
|
|
204
211
|
const instanceEventBridge = new InstanceEventBridge({
|
|
205
212
|
workspaceManager,
|
|
206
213
|
eventBus,
|
|
@@ -273,6 +280,7 @@ async function main() {
|
|
|
273
280
|
eventBus,
|
|
274
281
|
serverMeta,
|
|
275
282
|
instanceStore,
|
|
283
|
+
speechService,
|
|
276
284
|
authManager,
|
|
277
285
|
uiStaticDir: uiResolution.uiStaticDir ?? DEFAULT_UI_STATIC_DIR,
|
|
278
286
|
uiDevServerUrl: uiResolution.uiDevServerUrl,
|
|
@@ -292,6 +300,7 @@ async function main() {
|
|
|
292
300
|
eventBus,
|
|
293
301
|
serverMeta,
|
|
294
302
|
instanceStore,
|
|
303
|
+
speechService,
|
|
295
304
|
authManager,
|
|
296
305
|
uiStaticDir: uiResolution.uiStaticDir ?? DEFAULT_UI_STATIC_DIR,
|
|
297
306
|
uiDevServerUrl: undefined,
|
|
@@ -2,6 +2,8 @@ import type { PluginInput } from "@opencode-ai/plugin"
|
|
|
2
2
|
import { createCodeNomadClient, getCodeNomadConfig } from "./lib/client"
|
|
3
3
|
import { createBackgroundProcessTools } from "./lib/background-process"
|
|
4
4
|
|
|
5
|
+
let voiceModeEnabled = false
|
|
6
|
+
|
|
5
7
|
export async function CodeNomadPlugin(input: PluginInput) {
|
|
6
8
|
const config = getCodeNomadConfig()
|
|
7
9
|
const client = createCodeNomadClient(config)
|
|
@@ -16,6 +18,11 @@ export async function CodeNomadPlugin(input: PluginInput) {
|
|
|
16
18
|
pingTs: (event.properties as any)?.ts,
|
|
17
19
|
},
|
|
18
20
|
}).catch(() => {})
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (event.type === "codenomad.voiceMode") {
|
|
25
|
+
voiceModeEnabled = Boolean((event.properties as { enabled?: unknown } | undefined)?.enabled)
|
|
19
26
|
}
|
|
20
27
|
})
|
|
21
28
|
|
|
@@ -23,6 +30,13 @@ export async function CodeNomadPlugin(input: PluginInput) {
|
|
|
23
30
|
tool: {
|
|
24
31
|
...backgroundProcessTools,
|
|
25
32
|
},
|
|
33
|
+
async "chat.message"(_input: { sessionID: string }, output: { message: { system?: string } }) {
|
|
34
|
+
if (!voiceModeEnabled) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
output.message.system = [output.message.system, buildVoiceModePrompt()].filter(Boolean).join("\n\n")
|
|
39
|
+
},
|
|
26
40
|
async event(input: { event: any }) {
|
|
27
41
|
const opencodeEvent = input?.event
|
|
28
42
|
if (!opencodeEvent || typeof opencodeEvent !== "object") return
|
|
@@ -30,3 +44,19 @@ export async function CodeNomadPlugin(input: PluginInput) {
|
|
|
30
44
|
},
|
|
31
45
|
}
|
|
32
46
|
}
|
|
47
|
+
|
|
48
|
+
function buildVoiceModePrompt(): string {
|
|
49
|
+
return [
|
|
50
|
+
"Voice conversation mode is enabled.",
|
|
51
|
+
"Prepend your reply with a fenced code block using language `spoken`.",
|
|
52
|
+
"The `spoken` block should be the natural conversational reply you would say out loud to the user. It should be a concise spoken gist of the full response in 2 to 4 natural sentences.",
|
|
53
|
+
"In the spoken block, summarize the main outcome, recommendation, or next step. Sound conversational and natural, not like a document summary.",
|
|
54
|
+
"Do not include code, bullet lists, markdown formatting, or long technical detail in the spoken block.",
|
|
55
|
+
"Do not add generic phrases about whether the user should read more.",
|
|
56
|
+
"Only mention additional written detail when there is something specific that may matter for the user's next response, such as a tradeoff, caveat, risk, open question, exact diff, or test result.",
|
|
57
|
+
"When referring to that written detail, say `below` or `in the message` rather than `detailed section`.",
|
|
58
|
+
"After the `spoken` block, continue with your normal detailed response.",
|
|
59
|
+
"Example:",
|
|
60
|
+
"```spoken\nI implemented the relay-based voice-mode flow and it works with the current plugin bridge. The reconnect caveat is explained below.\n```",
|
|
61
|
+
].join("\n\n")
|
|
62
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class VoiceModeManager {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
this.options = options;
|
|
4
|
+
this.enabledConnectionsByInstance = new Map();
|
|
5
|
+
this.aggregateByInstance = new Map();
|
|
6
|
+
this.options.connections.subscribe((event) => {
|
|
7
|
+
if (event.type !== "disconnected")
|
|
8
|
+
return;
|
|
9
|
+
this.clearConnection(event.connection);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
setEnabled(instanceId, connection, enabled) {
|
|
13
|
+
if (enabled && !this.options.connections.isConnected(connection)) {
|
|
14
|
+
this.options.logger.debug({ instanceId, clientId: connection.clientId, connectionId: connection.connectionId }, "Ignoring voice mode enable for disconnected client connection");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const key = getConnectionKey(connection);
|
|
18
|
+
const current = this.enabledConnectionsByInstance.get(instanceId) ?? new Set();
|
|
19
|
+
if (enabled) {
|
|
20
|
+
current.add(key);
|
|
21
|
+
this.enabledConnectionsByInstance.set(instanceId, current);
|
|
22
|
+
}
|
|
23
|
+
else if (current.delete(key)) {
|
|
24
|
+
if (current.size === 0) {
|
|
25
|
+
this.enabledConnectionsByInstance.delete(instanceId);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.enabledConnectionsByInstance.set(instanceId, current);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
this.options.logger.debug({ instanceId, clientId: connection.clientId, connectionId: connection.connectionId, enabled }, "Voice mode updated for client connection");
|
|
32
|
+
this.publishIfChanged(instanceId);
|
|
33
|
+
}
|
|
34
|
+
syncInstance(instanceId) {
|
|
35
|
+
this.options.channel.send(instanceId, buildVoiceModeEvent(this.isEnabled(instanceId)));
|
|
36
|
+
}
|
|
37
|
+
isEnabled(instanceId) {
|
|
38
|
+
return this.aggregateByInstance.get(instanceId) === true;
|
|
39
|
+
}
|
|
40
|
+
clearConnection(connection) {
|
|
41
|
+
const key = getConnectionKey(connection);
|
|
42
|
+
for (const [instanceId, enabledConnections] of Array.from(this.enabledConnectionsByInstance.entries())) {
|
|
43
|
+
if (!enabledConnections.delete(key))
|
|
44
|
+
continue;
|
|
45
|
+
if (enabledConnections.size === 0) {
|
|
46
|
+
this.enabledConnectionsByInstance.delete(instanceId);
|
|
47
|
+
}
|
|
48
|
+
this.publishIfChanged(instanceId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
publishIfChanged(instanceId) {
|
|
52
|
+
const enabled = (this.enabledConnectionsByInstance.get(instanceId)?.size ?? 0) > 0;
|
|
53
|
+
const previous = this.aggregateByInstance.get(instanceId) === true;
|
|
54
|
+
if (enabled === previous)
|
|
55
|
+
return;
|
|
56
|
+
if (enabled) {
|
|
57
|
+
this.aggregateByInstance.set(instanceId, true);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.aggregateByInstance.delete(instanceId);
|
|
61
|
+
}
|
|
62
|
+
this.options.logger.debug({ instanceId, enabled }, "Broadcasting aggregate voice mode");
|
|
63
|
+
this.options.channel.send(instanceId, buildVoiceModeEvent(enabled));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function buildVoiceModeEvent(enabled) {
|
|
67
|
+
return {
|
|
68
|
+
type: "codenomad.voiceMode",
|
|
69
|
+
properties: {
|
|
70
|
+
enabled,
|
|
71
|
+
formatVersion: "v1",
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function getConnectionKey(connection) {
|
|
76
|
+
return `${connection.clientId}:${connection.connectionId}`;
|
|
77
|
+
}
|
|
@@ -15,9 +15,13 @@ import { registerStorageRoutes } from "./routes/storage";
|
|
|
15
15
|
import { registerPluginRoutes } from "./routes/plugin";
|
|
16
16
|
import { registerBackgroundProcessRoutes } from "./routes/background-processes";
|
|
17
17
|
import { registerWorktreeRoutes } from "./routes/worktrees";
|
|
18
|
+
import { registerSpeechRoutes } from "./routes/speech";
|
|
18
19
|
import { BackgroundProcessManager } from "../background-processes/manager";
|
|
19
20
|
import { registerAuthRoutes } from "./routes/auth";
|
|
20
21
|
import { sendUnauthorized, wantsHtml } from "../auth/http-auth";
|
|
22
|
+
import { ClientConnectionManager } from "../clients/connection-manager";
|
|
23
|
+
import { PluginChannelManager } from "../plugins/channel";
|
|
24
|
+
import { VoiceModeManager } from "../plugins/voice-mode";
|
|
21
25
|
export function createHttpServer(deps) {
|
|
22
26
|
// Fastify's type-level RawServer inference gets noisy when toggling HTTP vs HTTPS.
|
|
23
27
|
// We keep the runtime behavior correct and cast the instance to a generic FastifyInstance.
|
|
@@ -124,6 +128,13 @@ export function createHttpServer(deps) {
|
|
|
124
128
|
eventBus: deps.eventBus,
|
|
125
129
|
logger: deps.logger.child({ component: "background-processes" }),
|
|
126
130
|
});
|
|
131
|
+
const clientConnectionManager = new ClientConnectionManager(deps.logger.child({ component: "client-connections" }));
|
|
132
|
+
const pluginChannel = new PluginChannelManager(deps.logger.child({ component: "plugin-channel" }));
|
|
133
|
+
const voiceModeManager = new VoiceModeManager({
|
|
134
|
+
connections: clientConnectionManager,
|
|
135
|
+
channel: pluginChannel,
|
|
136
|
+
logger: deps.logger.child({ component: "voice-mode" }),
|
|
137
|
+
});
|
|
127
138
|
registerAuthRoutes(app, { authManager: deps.authManager });
|
|
128
139
|
app.addHook("preHandler", (request, reply, done) => {
|
|
129
140
|
const rawUrl = request.raw.url ?? request.url;
|
|
@@ -184,14 +195,26 @@ export function createHttpServer(deps) {
|
|
|
184
195
|
registerSettingsRoutes(app, { settings: deps.settings, logger: apiLogger });
|
|
185
196
|
registerFilesystemRoutes(app, { fileSystemBrowser: deps.fileSystemBrowser });
|
|
186
197
|
registerMetaRoutes(app, { serverMeta: deps.serverMeta });
|
|
187
|
-
registerEventRoutes(app, {
|
|
198
|
+
registerEventRoutes(app, {
|
|
199
|
+
eventBus: deps.eventBus,
|
|
200
|
+
registerClient: registerSseClient,
|
|
201
|
+
logger: sseLogger,
|
|
202
|
+
connectionManager: clientConnectionManager,
|
|
203
|
+
});
|
|
188
204
|
registerWorktreeRoutes(app, { workspaceManager: deps.workspaceManager });
|
|
189
205
|
registerStorageRoutes(app, {
|
|
190
206
|
instanceStore: deps.instanceStore,
|
|
191
207
|
eventBus: deps.eventBus,
|
|
192
208
|
workspaceManager: deps.workspaceManager,
|
|
193
209
|
});
|
|
194
|
-
|
|
210
|
+
registerSpeechRoutes(app, { speechService: deps.speechService });
|
|
211
|
+
registerPluginRoutes(app, {
|
|
212
|
+
workspaceManager: deps.workspaceManager,
|
|
213
|
+
eventBus: deps.eventBus,
|
|
214
|
+
logger: proxyLogger,
|
|
215
|
+
channel: pluginChannel,
|
|
216
|
+
voiceModeManager,
|
|
217
|
+
});
|
|
195
218
|
registerBackgroundProcessRoutes(app, { backgroundProcessManager });
|
|
196
219
|
registerInstanceProxyRoutes(app, { workspaceManager: deps.workspaceManager, logger: proxyLogger });
|
|
197
220
|
if (deps.uiDevServerUrl) {
|
|
@@ -249,6 +272,7 @@ export function createHttpServer(deps) {
|
|
|
249
272
|
},
|
|
250
273
|
stop: () => {
|
|
251
274
|
closeSseClients();
|
|
275
|
+
clientConnectionManager.shutdown();
|
|
252
276
|
return app.close();
|
|
253
277
|
},
|
|
254
278
|
};
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
let nextClientId = 0;
|
|
3
|
+
const ConnectionQuerySchema = z.object({
|
|
4
|
+
clientId: z.string().trim().min(1),
|
|
5
|
+
connectionId: z.string().trim().min(1),
|
|
6
|
+
});
|
|
7
|
+
const PongBodySchema = ConnectionQuerySchema.extend({
|
|
8
|
+
pingTs: z.number().optional(),
|
|
9
|
+
});
|
|
2
10
|
export function registerEventRoutes(app, deps) {
|
|
3
11
|
app.get("/api/events", (request, reply) => {
|
|
4
12
|
const clientId = ++nextClientId;
|
|
13
|
+
const connection = ConnectionQuerySchema.parse(request.query ?? {});
|
|
5
14
|
deps.logger.debug({ clientId }, "SSE client connected");
|
|
6
15
|
const origin = request.headers.origin ?? "*";
|
|
7
16
|
reply.raw.setHeader("Access-Control-Allow-Origin", origin);
|
|
@@ -20,7 +29,8 @@ export function registerEventRoutes(app, deps) {
|
|
|
20
29
|
};
|
|
21
30
|
const unsubscribe = deps.eventBus.onEvent(send);
|
|
22
31
|
const heartbeat = setInterval(() => {
|
|
23
|
-
|
|
32
|
+
const ping = { ts: Date.now() };
|
|
33
|
+
reply.raw.write(`event: codenomad.client.ping\ndata: ${JSON.stringify(ping)}\n\n`);
|
|
24
34
|
}, 15000);
|
|
25
35
|
let closed = false;
|
|
26
36
|
const close = () => {
|
|
@@ -33,11 +43,24 @@ export function registerEventRoutes(app, deps) {
|
|
|
33
43
|
deps.logger.debug({ clientId }, "SSE client disconnected");
|
|
34
44
|
};
|
|
35
45
|
const unregister = deps.registerClient(close);
|
|
46
|
+
const unregisterConnection = deps.connectionManager.register({
|
|
47
|
+
...connection,
|
|
48
|
+
close,
|
|
49
|
+
});
|
|
36
50
|
const handleClose = () => {
|
|
37
51
|
close();
|
|
38
52
|
unregister();
|
|
53
|
+
unregisterConnection();
|
|
39
54
|
};
|
|
40
55
|
request.raw.on("close", handleClose);
|
|
41
56
|
request.raw.on("error", handleClose);
|
|
42
57
|
});
|
|
58
|
+
app.post("/api/client-connections/pong", (request, reply) => {
|
|
59
|
+
const body = PongBodySchema.parse(request.body ?? {});
|
|
60
|
+
if (!deps.connectionManager.pong(body)) {
|
|
61
|
+
reply.code(404).send({ error: "Client connection not found" });
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
reply.code(204).send();
|
|
65
|
+
});
|
|
43
66
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { PluginChannelManager } from "../../plugins/channel";
|
|
3
2
|
import { buildPingEvent, handlePluginEvent } from "../../plugins/handlers";
|
|
4
3
|
const PluginEventSchema = z.object({
|
|
5
4
|
type: z.string().min(1),
|
|
6
5
|
properties: z.record(z.unknown()).optional(),
|
|
7
6
|
});
|
|
7
|
+
const VoiceModeStateSchema = z.object({
|
|
8
|
+
enabled: z.boolean(),
|
|
9
|
+
clientId: z.string().trim().min(1),
|
|
10
|
+
connectionId: z.string().trim().min(1),
|
|
11
|
+
});
|
|
8
12
|
export function registerPluginRoutes(app, deps) {
|
|
9
|
-
const channel = new PluginChannelManager(deps.logger.child({ component: "plugin-channel" }));
|
|
10
13
|
app.get("/workspaces/:id/plugin/events", (request, reply) => {
|
|
11
14
|
const workspace = deps.workspaceManager.get(request.params.id);
|
|
12
15
|
if (!workspace) {
|
|
@@ -18,9 +21,10 @@ export function registerPluginRoutes(app, deps) {
|
|
|
18
21
|
reply.raw.setHeader("Connection", "keep-alive");
|
|
19
22
|
reply.raw.flushHeaders?.();
|
|
20
23
|
reply.hijack();
|
|
21
|
-
const registration = channel.register(request.params.id, reply);
|
|
24
|
+
const registration = deps.channel.register(request.params.id, reply);
|
|
25
|
+
deps.voiceModeManager.syncInstance(request.params.id);
|
|
22
26
|
const heartbeat = setInterval(() => {
|
|
23
|
-
channel.send(request.params.id, buildPingEvent());
|
|
27
|
+
deps.channel.send(request.params.id, buildPingEvent());
|
|
24
28
|
}, 15000);
|
|
25
29
|
const close = () => {
|
|
26
30
|
clearInterval(heartbeat);
|
|
@@ -30,6 +34,16 @@ export function registerPluginRoutes(app, deps) {
|
|
|
30
34
|
request.raw.on("close", close);
|
|
31
35
|
request.raw.on("error", close);
|
|
32
36
|
});
|
|
37
|
+
app.post("/workspaces/:id/plugin/voice-mode", (request, reply) => {
|
|
38
|
+
const workspace = deps.workspaceManager.get(request.params.id);
|
|
39
|
+
if (!workspace) {
|
|
40
|
+
reply.code(404).send({ error: "Workspace not found" });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const payload = VoiceModeStateSchema.parse(request.body ?? {});
|
|
44
|
+
deps.voiceModeManager.setEnabled(request.params.id, { clientId: payload.clientId, connectionId: payload.connectionId }, payload.enabled);
|
|
45
|
+
return { enabled: payload.enabled };
|
|
46
|
+
});
|
|
33
47
|
const handleWildcard = async (request, reply) => {
|
|
34
48
|
const workspaceId = request.params.id;
|
|
35
49
|
const workspace = deps.workspaceManager.get(workspaceId);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { probeBinaryVersion } from "../../workspaces/runtime";
|
|
3
|
+
import { sanitizeConfigDoc, sanitizeConfigOwner } from "../../settings/public-config";
|
|
3
4
|
const ValidateBinarySchema = z.object({
|
|
4
5
|
path: z.string(),
|
|
5
6
|
});
|
|
@@ -9,10 +10,10 @@ function validateBinaryPath(binaryPath) {
|
|
|
9
10
|
}
|
|
10
11
|
export function registerSettingsRoutes(app, deps) {
|
|
11
12
|
// Full-document access
|
|
12
|
-
app.get("/api/storage/config", async () => deps.settings.getDoc("config"));
|
|
13
|
+
app.get("/api/storage/config", async () => sanitizeConfigDoc(deps.settings.getDoc("config")));
|
|
13
14
|
app.patch("/api/storage/config", async (request, reply) => {
|
|
14
15
|
try {
|
|
15
|
-
return deps.settings.mergePatchDoc("config", request.body ?? {});
|
|
16
|
+
return sanitizeConfigDoc(deps.settings.mergePatchDoc("config", request.body ?? {}));
|
|
16
17
|
}
|
|
17
18
|
catch (error) {
|
|
18
19
|
reply.code(400);
|
|
@@ -20,11 +21,11 @@ export function registerSettingsRoutes(app, deps) {
|
|
|
20
21
|
}
|
|
21
22
|
});
|
|
22
23
|
app.get("/api/storage/config/:owner", async (request) => {
|
|
23
|
-
return deps.settings.getOwner("config", request.params.owner);
|
|
24
|
+
return sanitizeConfigOwner(request.params.owner, deps.settings.getOwner("config", request.params.owner));
|
|
24
25
|
});
|
|
25
26
|
app.patch("/api/storage/config/:owner", async (request, reply) => {
|
|
26
27
|
try {
|
|
27
|
-
return deps.settings.mergePatchOwner("config", request.params.owner, request.body ?? {});
|
|
28
|
+
return sanitizeConfigOwner(request.params.owner, deps.settings.mergePatchOwner("config", request.params.owner, request.body ?? {}));
|
|
28
29
|
}
|
|
29
30
|
catch (error) {
|
|
30
31
|
reply.code(400);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const TranscribeBodySchema = z.object({
|
|
3
|
+
audioBase64: z.string().min(1, "Audio payload is required"),
|
|
4
|
+
mimeType: z.string().min(1, "Audio MIME type is required"),
|
|
5
|
+
filename: z.string().optional(),
|
|
6
|
+
language: z.string().optional(),
|
|
7
|
+
prompt: z.string().optional(),
|
|
8
|
+
});
|
|
9
|
+
const SynthesizeBodySchema = z.object({
|
|
10
|
+
text: z.string().trim().min(1, "Text is required"),
|
|
11
|
+
format: z.enum(["mp3", "wav", "opus", "aac"]).optional(),
|
|
12
|
+
});
|
|
13
|
+
function getSpeechErrorStatus(error) {
|
|
14
|
+
if (error instanceof z.ZodError) {
|
|
15
|
+
return 400;
|
|
16
|
+
}
|
|
17
|
+
if (error instanceof Error && /not configured/i.test(error.message)) {
|
|
18
|
+
return 503;
|
|
19
|
+
}
|
|
20
|
+
return 502;
|
|
21
|
+
}
|
|
22
|
+
function getSpeechErrorMessage(error, fallback) {
|
|
23
|
+
return error instanceof Error ? error.message : fallback;
|
|
24
|
+
}
|
|
25
|
+
export function registerSpeechRoutes(app, deps) {
|
|
26
|
+
app.get("/api/speech/capabilities", async () => deps.speechService.getCapabilities());
|
|
27
|
+
app.post("/api/speech/transcribe", async (request, reply) => {
|
|
28
|
+
try {
|
|
29
|
+
const body = TranscribeBodySchema.parse(request.body ?? {});
|
|
30
|
+
return await deps.speechService.transcribe(body);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
request.log.error({ err: error }, "Failed to transcribe audio");
|
|
34
|
+
reply.code(getSpeechErrorStatus(error));
|
|
35
|
+
return { error: getSpeechErrorMessage(error, "Failed to transcribe audio") };
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
app.post("/api/speech/synthesize", async (request, reply) => {
|
|
39
|
+
try {
|
|
40
|
+
const body = SynthesizeBodySchema.parse(request.body ?? {});
|
|
41
|
+
return await deps.speechService.synthesize(body);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
request.log.error({ err: error }, "Failed to synthesize audio");
|
|
45
|
+
reply.code(getSpeechErrorStatus(error));
|
|
46
|
+
return { error: getSpeechErrorMessage(error, "Failed to synthesize audio") };
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
app.post("/api/speech/synthesize/stream", async (request, reply) => {
|
|
50
|
+
try {
|
|
51
|
+
const body = SynthesizeBodySchema.parse(request.body ?? {});
|
|
52
|
+
const result = await deps.speechService.synthesizeStream(body);
|
|
53
|
+
reply.header("Content-Type", result.mimeType);
|
|
54
|
+
reply.header("Cache-Control", "no-store");
|
|
55
|
+
return reply.send(result.stream);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
request.log.error({ err: error }, "Failed to stream synthesized audio");
|
|
59
|
+
reply.code(getSpeechErrorStatus(error));
|
|
60
|
+
return { error: getSpeechErrorMessage(error, "Failed to stream synthesized audio") };
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -9,6 +9,9 @@ const WorkspaceFilesQuerySchema = z.object({
|
|
|
9
9
|
const WorkspaceFileContentQuerySchema = z.object({
|
|
10
10
|
path: z.string(),
|
|
11
11
|
});
|
|
12
|
+
const WorkspaceFileContentBodySchema = z.object({
|
|
13
|
+
contents: z.string(),
|
|
14
|
+
});
|
|
12
15
|
const WorkspaceFileSearchQuerySchema = z.object({
|
|
13
16
|
q: z.string().trim().min(1, "Query is required"),
|
|
14
17
|
limit: z.coerce.number().int().positive().max(200).optional(),
|
|
@@ -78,6 +81,17 @@ export function registerWorkspaceRoutes(app, deps) {
|
|
|
78
81
|
return handleWorkspaceError(error, reply);
|
|
79
82
|
}
|
|
80
83
|
});
|
|
84
|
+
app.put("/api/workspaces/:id/files/content", async (request, reply) => {
|
|
85
|
+
try {
|
|
86
|
+
const query = WorkspaceFileContentQuerySchema.parse(request.query ?? {});
|
|
87
|
+
const body = WorkspaceFileContentBodySchema.parse(request.body ?? {});
|
|
88
|
+
deps.workspaceManager.writeFile(request.params.id, query.path, body.contents);
|
|
89
|
+
reply.code(204);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
return handleWorkspaceError(error, reply);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
81
95
|
}
|
|
82
96
|
function handleWorkspaceError(error, reply) {
|
|
83
97
|
if (error instanceof Error && error.message === "Workspace not found") {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function isPlainObject(value) {
|
|
2
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
function sanitizeServerOwner(value) {
|
|
5
|
+
const next = { ...value };
|
|
6
|
+
const speech = isPlainObject(next.speech) ? { ...next.speech } : null;
|
|
7
|
+
if (!speech) {
|
|
8
|
+
return next;
|
|
9
|
+
}
|
|
10
|
+
const rawApiKey = typeof speech.apiKey === "string" ? speech.apiKey.trim() : "";
|
|
11
|
+
if (rawApiKey) {
|
|
12
|
+
delete speech.apiKey;
|
|
13
|
+
speech.hasApiKey = true;
|
|
14
|
+
}
|
|
15
|
+
else if (!("hasApiKey" in speech)) {
|
|
16
|
+
speech.hasApiKey = false;
|
|
17
|
+
}
|
|
18
|
+
next.speech = speech;
|
|
19
|
+
return next;
|
|
20
|
+
}
|
|
21
|
+
export function sanitizeConfigOwner(owner, value) {
|
|
22
|
+
if (owner !== "server") {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
return sanitizeServerOwner(value);
|
|
26
|
+
}
|
|
27
|
+
export function sanitizeConfigDoc(value) {
|
|
28
|
+
const next = { ...value };
|
|
29
|
+
if (isPlainObject(next.server)) {
|
|
30
|
+
next.server = sanitizeServerOwner(next.server);
|
|
31
|
+
}
|
|
32
|
+
return next;
|
|
33
|
+
}
|