pi2web 0.2.1 → 0.2.8
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/README.md +53 -6
- package/dist/activity.js +178 -0
- package/dist/agent-feed.js +133 -0
- package/dist/agent-templates.js +144 -0
- package/dist/agents.js +671 -152
- package/dist/auth.js +3 -1
- package/dist/collab/baseline.js +84 -0
- package/dist/collab/dispatcher.js +45 -10
- package/dist/collab/hub.js +1392 -384
- package/dist/collab/pi-extension.js +322 -65
- package/dist/collab/review-flow.js +197 -48
- package/dist/collab/routes.js +94 -23
- package/dist/collab/schemas.js +69 -13
- package/dist/collab/scoring-flow.js +145 -39
- package/dist/collab/similarity.js +15 -0
- package/dist/collab/store.js +467 -112
- package/dist/collab/types.js +94 -10
- package/dist/collab/validate.js +62 -25
- package/dist/drafts.js +26 -6
- package/dist/http/agent-routes.js +222 -0
- package/dist/http/login-limiter.js +53 -0
- package/dist/http/mail-settings-routes.js +53 -0
- package/dist/http/system-prompt-routes.js +52 -0
- package/dist/http/utils.js +47 -0
- package/dist/http/web-assets.js +64 -0
- package/dist/http/workspace-routes.js +58 -0
- package/dist/mail-notifier.js +63 -17
- package/dist/main.js +61 -16
- package/dist/mcp.js +24 -5
- package/dist/metadata-store.js +203 -56
- package/dist/paths.js +10 -0
- package/dist/sdk-backend.js +249 -45
- package/dist/server.js +488 -614
- package/dist/session-namer.js +66 -17
- package/dist/session-performance.js +137 -0
- package/dist/sotto-stt.js +217 -0
- package/dist/terminals.js +94 -27
- package/dist/timeline.js +185 -0
- package/dist/voice.js +153 -35
- package/dist/workspaces.js +91 -26
- package/dist/ws/agent-socket.js +256 -0
- package/dist/ws/terminal-socket.js +46 -0
- package/npm-shrinkwrap.json +3685 -0
- package/package.json +11 -1
- package/packages/protocol/openapi.yaml +24 -2
- package/packages/protocol/websocket.schema.json +58 -15
- package/web/activity-view.js +121 -0
- package/web/app.js +3647 -756
- package/web/backend-state.js +59 -27
- package/web/collab.js +684 -257
- package/web/hid-keyboard.js +173 -140
- package/web/index.html +7 -2
- package/web/styles.css +21 -2
- package/web/voice-pcm.js +36 -0
package/dist/server.js
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createRequire } from 'node:module';
|
|
6
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
7
|
-
import { WebSocketServer, WebSocket } from 'ws';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { WebSocketServer } from 'ws';
|
|
8
5
|
import { AuthStore } from './auth.js';
|
|
9
6
|
import { WorkspaceStore } from './workspaces.js';
|
|
10
7
|
import { AgentManager } from './agents.js';
|
|
11
8
|
import { createSdkBackend } from './sdk-backend.js';
|
|
12
|
-
import { listMcpServers, setMcpServerEnabled } from './mcp.js';
|
|
13
9
|
import { TerminalManager } from './terminals.js';
|
|
14
10
|
import { MetadataStore } from './metadata-store.js';
|
|
11
|
+
import { SottoSttService } from './sotto-stt.js';
|
|
15
12
|
import { CollabHub } from './collab/hub.js';
|
|
16
13
|
import { CollabStore } from './collab/store.js';
|
|
17
14
|
import { CollabRouter } from './collab/routes.js';
|
|
18
15
|
import { CollabDispatcher } from './collab/dispatcher.js';
|
|
19
16
|
import { DraftHub } from './drafts.js';
|
|
17
|
+
import { AGENT_TEMPLATES_DIR, defaultDataDir } from './paths.js';
|
|
18
|
+
import { AgentTemplateStore } from './agent-templates.js';
|
|
20
19
|
import { SessionManager, VERSION as PI_VERSION } from '@earendil-works/pi-coding-agent';
|
|
21
20
|
import pkg from '../package.json' with { type: 'json' };
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const webFiles = { '/': { file: 'index.html', type: 'text/html; charset=utf-8' }, '/index.html': { file: 'index.html', type: 'text/html; charset=utf-8' }, '/app.js': { file: 'app.js', type: 'text/javascript; charset=utf-8' }, '/backend-state.js': { file: 'backend-state.js', type: 'text/javascript; charset=utf-8' }, '/hid-keyboard.js': { file: 'hid-keyboard.js', type: 'text/javascript; charset=utf-8' }, '/collab': { file: 'collab.html', type: 'text/html; charset=utf-8' }, '/collab.html': { file: 'collab.html', type: 'text/html; charset=utf-8' }, '/collab.js': { file: 'collab.js', type: 'text/javascript; charset=utf-8' }, '/styles.css': { file: 'styles.css', type: 'text/css; charset=utf-8' }, '/icon.svg': { file: 'icon.svg', type: 'image/svg+xml' }, '/favicon.ico': { file: 'icon.svg', type: 'image/svg+xml' }, '/vendor/xterm.js': { file: require.resolve('@xterm/xterm'), type: 'text/javascript; charset=utf-8' }, '/vendor/xterm.css': { file: require.resolve('@xterm/xterm/css/xterm.css'), type: 'text/css; charset=utf-8' }, '/vendor/addon-fit.js': { file: require.resolve('@xterm/addon-fit'), type: 'text/javascript; charset=utf-8' }, '/vendor/marked.js': { file: markedBrowser, type: 'text/javascript; charset=utf-8' }, '/vendor/dompurify.js': { file: require.resolve('dompurify/purify.min.js'), type: 'text/javascript; charset=utf-8' } };
|
|
21
|
+
import { LOOPBACK_HOSTS, errorStatus, isLoopbackHost, json, readBinaryBody, readJsonBody } from './http/utils.js';
|
|
22
|
+
import { serveWebAsset } from './http/web-assets.js';
|
|
23
|
+
import { LoginLimiter } from './http/login-limiter.js';
|
|
24
|
+
import { handleAgentSocket, PROTOCOL_VERSION } from './ws/agent-socket.js';
|
|
25
|
+
import { handleTerminalSocket } from './ws/terminal-socket.js';
|
|
26
|
+
import { handleAgentRoutes } from './http/agent-routes.js';
|
|
27
|
+
import { handleMailSettingsRoutes } from './http/mail-settings-routes.js';
|
|
28
|
+
import { handleWorkspaceRoutes } from './http/workspace-routes.js';
|
|
29
|
+
import { handleSystemPromptRoutes } from './http/system-prompt-routes.js';
|
|
30
|
+
export { LOOPBACK_HOSTS };
|
|
33
31
|
export class RemotePiServer {
|
|
34
32
|
server;
|
|
35
33
|
wss;
|
|
@@ -38,6 +36,7 @@ export class RemotePiServer {
|
|
|
38
36
|
agents;
|
|
39
37
|
terminals;
|
|
40
38
|
metadata;
|
|
39
|
+
templates;
|
|
41
40
|
collab;
|
|
42
41
|
collabRouter;
|
|
43
42
|
collabDispatcher;
|
|
@@ -56,14 +55,60 @@ export class RemotePiServer {
|
|
|
56
55
|
sessionIndexJobs = new Map();
|
|
57
56
|
agentIndexJobs = new Map();
|
|
58
57
|
voice;
|
|
58
|
+
sottoStt;
|
|
59
59
|
sessionNamer;
|
|
60
60
|
mailNotifier;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
loginLimiter = new LoginLimiter();
|
|
62
|
+
constructor(options = {}) {
|
|
63
|
+
this.opts = { host: options.host ?? '127.0.0.1', port: options.port ?? 11318 };
|
|
64
|
+
this.noAuth = options.noAuth ?? false;
|
|
65
|
+
this.allowInsecureNoAuth = options.allowInsecureNoAuth ?? false;
|
|
66
|
+
this.dataDir = options.dataDir ?? defaultDataDir();
|
|
67
|
+
this.auth = new AuthStore(this.dataDir);
|
|
68
|
+
this.metadata = new MetadataStore(this.dataDir);
|
|
69
|
+
this.sottoStt = new SottoSttService(this.metadata);
|
|
70
|
+
this.templates = new AgentTemplateStore(path.join(this.dataDir, AGENT_TEMPLATES_DIR));
|
|
71
|
+
this.workspaces = options.workspaces ?? new WorkspaceStore();
|
|
72
|
+
this.voice = options.voice;
|
|
73
|
+
this.sessionNamer = options.sessionNamer;
|
|
74
|
+
this.mailNotifier = options.mailNotifier;
|
|
75
|
+
this.collab = new CollabHub(new CollabStore(() => this.metadata.connection), {
|
|
76
|
+
agentStatus: agentId => this.agentStatus(agentId)
|
|
77
|
+
});
|
|
78
|
+
this.agents =
|
|
79
|
+
options.agents ??
|
|
80
|
+
new AgentManager(this.workspaces, (agentId, cwd, sessionFile, profile, backendOptions) => createSdkBackend(agentId, cwd, sessionFile, profile, profile === 'collab' ? this.collabToolBridge() : undefined, backendOptions));
|
|
81
|
+
this.terminals = options.terminals ?? new TerminalManager(this.workspaces);
|
|
82
|
+
this.collabRouter = new CollabRouter(this.collab, {
|
|
83
|
+
resolveCwd: (workspaceId, relativeCwd) => this.resolveWorkspaceCwd(workspaceId, relativeCwd),
|
|
84
|
+
verifyHuman: token => this.auth.verify(token),
|
|
85
|
+
noAuth: this.noAuth,
|
|
86
|
+
isCollabAgent: agentId => this.isCollabAgent(agentId)
|
|
87
|
+
});
|
|
88
|
+
this.collabDispatcher = new CollabDispatcher(this.collab, {
|
|
89
|
+
command: (agentId, kind, message) => this.agents.command(agentId, kind, message),
|
|
90
|
+
agentStatus: agentId => this.agentStatus(agentId),
|
|
91
|
+
isCollabAgent: agentId => this.isCollabAgent(agentId),
|
|
92
|
+
log: message => console.error(message)
|
|
93
|
+
});
|
|
94
|
+
this.server = http.createServer((req, res) => void this.handle(req, res));
|
|
95
|
+
this.wss = new WebSocketServer({ noServer: true, maxPayload: 2 * 1024 * 1024 });
|
|
96
|
+
this.server.on('upgrade', (req, socket, head) => void this.upgrade(req, socket, head));
|
|
97
|
+
this.wss.on('connection', (ws, req) => {
|
|
98
|
+
const match = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`).pathname.match(/^\/api\/v1\/terminals\/([^/]+)\/ws$/);
|
|
99
|
+
if (match)
|
|
100
|
+
handleTerminalSocket({ terminals: this.terminals, clients: this.clients }, ws, decodeURIComponent(match[1]));
|
|
101
|
+
else
|
|
102
|
+
handleAgentSocket({
|
|
103
|
+
agents: this.agents,
|
|
104
|
+
drafts: this.drafts,
|
|
105
|
+
collab: this.collab,
|
|
106
|
+
voice: this.voice,
|
|
107
|
+
sessionNamer: this.sessionNamer,
|
|
108
|
+
clients: this.clients
|
|
109
|
+
}, ws);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
67
112
|
async start() {
|
|
68
113
|
if (this.allowInsecureNoAuth && !this.noAuth)
|
|
69
114
|
throw Object.assign(new Error('allowInsecureNoAuth requires noAuth (--allow-insecure-no-auth requires --no-auth)'), { code: 'INVALID_OPTIONS' });
|
|
@@ -73,14 +118,17 @@ export class RemotePiServer {
|
|
|
73
118
|
console.warn('WARNING: --allow-insecure-no-auth disables loopback bind and Host/Origin restrictions. Anyone who can reach this server, including through a web page, can access workspaces and control agents and terminals without a pairing code. Use only on a trusted, access-controlled network.');
|
|
74
119
|
const first = this.noAuth ? {} : await this.auth.init();
|
|
75
120
|
this.metadata.init();
|
|
121
|
+
this.sottoStt.load();
|
|
76
122
|
this.collab.init();
|
|
77
123
|
this.unsubscribeCollab = this.collab.subscribe(event => this.collabNotification(event));
|
|
78
|
-
this.collabStallTimer = setInterval(() => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
124
|
+
this.collabStallTimer = setInterval(() => {
|
|
125
|
+
try {
|
|
126
|
+
this.collab.checkStalls();
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error(`Collaboration stall check failed: ${error.message}`);
|
|
130
|
+
}
|
|
131
|
+
}, 60_000);
|
|
84
132
|
this.collabStallTimer.unref?.();
|
|
85
133
|
if (this.mailNotifier) {
|
|
86
134
|
const settings = this.metadata.getSetting('mailNotifications');
|
|
@@ -98,12 +146,26 @@ export class RemotePiServer {
|
|
|
98
146
|
this.collabDispatcher.start();
|
|
99
147
|
this.unsubscribeMetadata = this.agents.subscribeAll((agentId, event) => {
|
|
100
148
|
const record = this.agents.get(agentId).record;
|
|
101
|
-
const applyName = async (name) => {
|
|
102
|
-
|
|
149
|
+
const applyName = async (name) => {
|
|
150
|
+
if (this.agents.get(agentId).record.sessionName?.trim())
|
|
151
|
+
return;
|
|
152
|
+
await this.agents.setSessionName(agentId, name);
|
|
153
|
+
this.metadata.saveAgent(this.agents.get(agentId).record);
|
|
154
|
+
};
|
|
103
155
|
// The namer owns naming whenever it is enabled, so the voice summary never races it for the same name.
|
|
104
|
-
this.sessionNamer?.handleAgentEvent(agentId, event.event, {
|
|
105
|
-
|
|
106
|
-
|
|
156
|
+
this.sessionNamer?.handleAgentEvent(agentId, event.event, {
|
|
157
|
+
sessionName: record.sessionName,
|
|
158
|
+
setSessionName: applyName
|
|
159
|
+
});
|
|
160
|
+
this.voice?.handleAgentEvent(agentId, event.event, {
|
|
161
|
+
sessionName: record.sessionName,
|
|
162
|
+
...(this.sessionNamer ? {} : { setSessionName: applyName })
|
|
163
|
+
});
|
|
164
|
+
this.mailNotifier?.handleAgentEvent(agentId, event.event, {
|
|
165
|
+
sessionName: record.sessionName,
|
|
166
|
+
workspaceLabel: this.workspaces.get(record.workspaceId)?.label,
|
|
167
|
+
cwd: record.cwd
|
|
168
|
+
});
|
|
107
169
|
if (['message_end', 'agent_end', 'agent_settled', 'session_info_changed'].includes(String(event.event.type))) {
|
|
108
170
|
const record = this.agents.get(agentId).record;
|
|
109
171
|
this.metadata.scheduleAgent(record);
|
|
@@ -116,23 +178,33 @@ export class RemotePiServer {
|
|
|
116
178
|
await new Promise(r => this.server.listen(this.opts.port, this.opts.host, r));
|
|
117
179
|
return this.address();
|
|
118
180
|
}
|
|
119
|
-
address() {
|
|
181
|
+
address() {
|
|
182
|
+
const a = this.server.address();
|
|
183
|
+
return typeof a === 'object' && a ? { host: this.opts.host, port: a.port } : undefined;
|
|
184
|
+
}
|
|
120
185
|
/** URL an agent running on this machine can use to reach the hub. Wildcard binds are rewritten to loopback. */
|
|
121
|
-
publicBaseUrl() {
|
|
186
|
+
publicBaseUrl() {
|
|
187
|
+
const port = this.address()?.port ?? this.opts.port, host = ['0.0.0.0', '::', '::1', ''].includes(this.opts.host) ? '127.0.0.1' : this.opts.host;
|
|
188
|
+
return `http://${host.includes(':') ? `[${host}]` : host}:${port}`;
|
|
189
|
+
}
|
|
122
190
|
/** Lifecycle of a local Agent; undefined once it no longer exists. The hub uses it to tell "still working" from "never answered". */
|
|
123
|
-
agentStatus(agentId) {
|
|
124
|
-
|
|
191
|
+
agentStatus(agentId) {
|
|
192
|
+
try {
|
|
193
|
+
return this.agents.get(agentId).record.status;
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
125
198
|
}
|
|
126
|
-
catch {
|
|
127
|
-
return undefined;
|
|
128
|
-
} }
|
|
129
199
|
/** A collaboration seat is only ever bound to an existing `profile=collab` Agent: anything else can never be woken. */
|
|
130
|
-
isCollabAgent(agentId) {
|
|
131
|
-
|
|
200
|
+
isCollabAgent(agentId) {
|
|
201
|
+
try {
|
|
202
|
+
return this.agents.get(agentId).record.profile === 'collab';
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
132
207
|
}
|
|
133
|
-
catch {
|
|
134
|
-
return false;
|
|
135
|
-
} }
|
|
136
208
|
/** Pi extension bridge. It intentionally calls the hub in-process, so model context never contains tokens or URLs. */
|
|
137
209
|
collabToolBridge() {
|
|
138
210
|
return {
|
|
@@ -143,15 +215,24 @@ export class RemotePiServer {
|
|
|
143
215
|
async submitCollabTool(agentId, action, payload) {
|
|
144
216
|
const participant = this.collab.participantForAgent(agentId);
|
|
145
217
|
switch (action) {
|
|
146
|
-
case 'ready':
|
|
147
|
-
|
|
148
|
-
case '
|
|
149
|
-
|
|
150
|
-
case '
|
|
151
|
-
|
|
152
|
-
case '
|
|
153
|
-
|
|
154
|
-
case '
|
|
218
|
+
case 'ready':
|
|
219
|
+
return this.collab.markImplementationReady(participant, payload);
|
|
220
|
+
case 'findings':
|
|
221
|
+
return this.collab.submitFindings(participant, payload);
|
|
222
|
+
case 'issue_votes':
|
|
223
|
+
return this.collab.submitIssueVotes(participant, payload);
|
|
224
|
+
case 'merge_votes':
|
|
225
|
+
return this.collab.submitMergeVotes(participant, payload);
|
|
226
|
+
case 'issue_discussions':
|
|
227
|
+
return this.collab.submitIssueDiscussions(participant, payload);
|
|
228
|
+
case 'nominations':
|
|
229
|
+
return this.collab.submitNominations(participant, payload);
|
|
230
|
+
case 'votes':
|
|
231
|
+
return this.collab.submitVotes(participant, payload);
|
|
232
|
+
case 'scores':
|
|
233
|
+
return this.collab.submitScores(participant, payload);
|
|
234
|
+
case 'escalate':
|
|
235
|
+
return this.collab.raiseEscalation(participant, payload);
|
|
155
236
|
case 'withdraw': {
|
|
156
237
|
const issueId = String(payload.issueId ?? '');
|
|
157
238
|
delete payload.issueId;
|
|
@@ -162,7 +243,8 @@ export class RemotePiServer {
|
|
|
162
243
|
delete payload.debateId;
|
|
163
244
|
return this.collab.submitDebateArgument(participant, debateId, payload);
|
|
164
245
|
}
|
|
165
|
-
case 'debate_arguments':
|
|
246
|
+
case 'debate_arguments':
|
|
247
|
+
return this.collab.submitDebateArguments(participant, payload);
|
|
166
248
|
}
|
|
167
249
|
}
|
|
168
250
|
/** Escalations and stalls block the whole session, so a human is mailed the moment one appears. */
|
|
@@ -175,13 +257,23 @@ export class RemotePiServer {
|
|
|
175
257
|
if (!this.allowCollabMail(event.sessionId))
|
|
176
258
|
return;
|
|
177
259
|
const escalation = this.collab.getEscalation(String(event.payload.escalationId ?? '')), session = this.collab.getSession(event.sessionId);
|
|
178
|
-
const lines = [
|
|
260
|
+
const lines = [
|
|
261
|
+
`协作会话:${session.title}(${session.kind},phase=${session.phase},round=${session.round})`,
|
|
262
|
+
`类型:${escalation.kind}|紧急度:${escalation.urgency}`,
|
|
263
|
+
`发起者:${escalation.raisedBy}`,
|
|
264
|
+
'',
|
|
265
|
+
`摘要:${escalation.summary}`,
|
|
266
|
+
`待裁定问题:${escalation.question}`
|
|
267
|
+
];
|
|
179
268
|
if (escalation.options.length)
|
|
180
269
|
lines.push(`候选方案:${escalation.options.join(' / ')}`);
|
|
181
270
|
if (escalation.positions.length)
|
|
182
271
|
lines.push('', '各方立场:', ...escalation.positions.map(position => `- ${position.participantId}(${position.stance}):${position.rationale}`));
|
|
183
272
|
lines.push('', `处理入口:${this.publicBaseUrl()}/collab.html?session=${session.sessionId}`, `API:POST /api/v1/collab/escalations/${escalation.escalationId}/resolve`);
|
|
184
|
-
this.mailNotifier.notifyCollab({
|
|
273
|
+
this.mailNotifier.notifyCollab({
|
|
274
|
+
subject: `[Remote Pi] 协作会话需要人工裁定:${session.title}`,
|
|
275
|
+
text: lines.join('\n')
|
|
276
|
+
});
|
|
185
277
|
return;
|
|
186
278
|
}
|
|
187
279
|
if (event.type === 'participant_overdue') {
|
|
@@ -189,10 +281,19 @@ export class RemotePiServer {
|
|
|
189
281
|
return;
|
|
190
282
|
const session = this.collab.getSession(event.sessionId), waiting = event.payload.waitingOn ?? [];
|
|
191
283
|
const neverCollected = event.payload.reason === 'task_never_collected';
|
|
192
|
-
this.mailNotifier.notifyCollab({
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
284
|
+
this.mailNotifier.notifyCollab({
|
|
285
|
+
subject: `[Remote Pi] 协作会话停滞:${session.title}`,
|
|
286
|
+
text: [
|
|
287
|
+
`协作会话:${session.title}(${session.kind},phase=${session.phase},round=${session.round})`,
|
|
288
|
+
neverCollected
|
|
289
|
+
? '任务已派发但从未送达:绑定的本机 Agent 可能已被删除或卡住,请在看板上把这个座位改绑到另一个 Agent。'
|
|
290
|
+
: `已等待:${Math.round(Number(event.payload.overdueBySec ?? 0))} 秒`,
|
|
291
|
+
`未提交的参与者:${waiting.join(', ') || '(未知)'}`,
|
|
292
|
+
'',
|
|
293
|
+
'中枢不会自动跳过任何人;如需推进请强制推进并说明理由。',
|
|
294
|
+
`处理入口:${this.publicBaseUrl()}/collab.html?session=${session.sessionId}`
|
|
295
|
+
].join('\n')
|
|
296
|
+
});
|
|
196
297
|
}
|
|
197
298
|
}
|
|
198
299
|
catch (error) {
|
|
@@ -213,583 +314,356 @@ export class RemotePiServer {
|
|
|
213
314
|
this.collabMailAt.delete(key);
|
|
214
315
|
return true;
|
|
215
316
|
}
|
|
216
|
-
async stop() {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
this.
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
connection(ws, _req) {
|
|
257
|
-
this.clients.add(ws);
|
|
258
|
-
const subscriptions = new Map();
|
|
259
|
-
// Pi providers may emit one delta per character. Sending every delta as its own
|
|
260
|
-
// WebSocket frame is especially expensive over a high-latency/low-bandwidth
|
|
261
|
-
// link: agent_end gets stuck behind thousands of tiny frames. Coalesce adjacent
|
|
262
|
-
// text/thinking deltas while preserving their sequence cursor and event order.
|
|
263
|
-
let pendingDelta, deltaTimer;
|
|
264
|
-
const sendNow = (message) => { if (ws.readyState !== WebSocket.OPEN)
|
|
265
|
-
return; if (ws.bufferedAmount > 2 * 1024 * 1024) {
|
|
266
|
-
ws.close(1013, 'Agent client is too slow');
|
|
267
|
-
return;
|
|
268
|
-
} ws.send(JSON.stringify(message)); };
|
|
269
|
-
const flushDelta = () => { if (deltaTimer)
|
|
270
|
-
clearTimeout(deltaTimer); deltaTimer = undefined; if (pendingDelta) {
|
|
271
|
-
const message = pendingDelta;
|
|
272
|
-
pendingDelta = undefined;
|
|
273
|
-
sendNow(message);
|
|
274
|
-
} };
|
|
275
|
-
const send = (message) => { const update = message?.type === 'agent_event' && message.event?.type === 'message_update' ? message.event.assistantMessageEvent : undefined, isDelta = update && (update.type === 'text_delta' || update.type === 'thinking_delta') && typeof update.delta === 'string'; if (!isDelta) {
|
|
276
|
-
flushDelta();
|
|
277
|
-
sendNow(message);
|
|
317
|
+
async stop() {
|
|
318
|
+
if (this.collabStallTimer)
|
|
319
|
+
clearInterval(this.collabStallTimer);
|
|
320
|
+
this.collabStallTimer = undefined;
|
|
321
|
+
this.unsubscribeCollab?.();
|
|
322
|
+
this.unsubscribeCollab = undefined;
|
|
323
|
+
await this.collabDispatcher.stop();
|
|
324
|
+
this.collab.shutdown();
|
|
325
|
+
this.unsubscribeMetadata?.();
|
|
326
|
+
this.unsubscribeMetadata = undefined;
|
|
327
|
+
this.voice?.close();
|
|
328
|
+
this.sessionNamer?.close();
|
|
329
|
+
await this.mailNotifier?.close();
|
|
330
|
+
await Promise.allSettled([...this.sessionIndexJobs.values(), ...this.agentIndexJobs.values()]);
|
|
331
|
+
this.metadata.flush();
|
|
332
|
+
for (const c of this.clients)
|
|
333
|
+
c.close();
|
|
334
|
+
for (const a of this.agents.list())
|
|
335
|
+
await this.agents.dispose(a.agentId).catch(() => { });
|
|
336
|
+
this.terminals.closeAll();
|
|
337
|
+
this.wss.close();
|
|
338
|
+
await new Promise(r => this.server.close(() => r()));
|
|
339
|
+
this.metadata.close();
|
|
340
|
+
}
|
|
341
|
+
async upgrade(req, socket, head) {
|
|
342
|
+
const protocol = req.headers['sec-websocket-protocol'];
|
|
343
|
+
const wsToken = Array.isArray(protocol)
|
|
344
|
+
? protocol.find(x => x.startsWith('access-token.'))?.slice(13)
|
|
345
|
+
: protocol
|
|
346
|
+
?.split(',')
|
|
347
|
+
.map(x => x.trim())
|
|
348
|
+
.find(x => x.startsWith('access-token.'))
|
|
349
|
+
?.slice(13);
|
|
350
|
+
const pathname = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`).pathname, voiceMatch = pathname.match(/^\/api\/v1\/agents\/([^/]+)\/transcribe\/ws$/), isTranscription = pathname === '/api/v1/transcribe/ws', isSocket = pathname === '/api/v1/ws' ||
|
|
351
|
+
/^\/api\/v1\/terminals\/[^/]+\/ws$/.test(pathname) ||
|
|
352
|
+
!!voiceMatch ||
|
|
353
|
+
isTranscription;
|
|
354
|
+
if (!isSocket) {
|
|
355
|
+
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
|
356
|
+
socket.destroy();
|
|
278
357
|
return;
|
|
279
|
-
} const previous = pendingDelta?.event?.assistantMessageEvent; if (pendingDelta && pendingDelta.agentId === message.agentId && previous?.type === update.type) {
|
|
280
|
-
previous.delta += update.delta;
|
|
281
|
-
pendingDelta.eventId = message.eventId;
|
|
282
|
-
pendingDelta.sequence = message.sequence;
|
|
283
|
-
pendingDelta.timestamp = message.timestamp;
|
|
284
358
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
} if (!deltaTimer)
|
|
289
|
-
deltaTimer = setTimeout(flushDelta, 40); };
|
|
290
|
-
const unsubscribeVoice = this.voice?.subscribe((agentId, event) => { if (subscriptions.has('*') || subscriptions.has(agentId))
|
|
291
|
-
send({ type: 'voice_event', agentId, event }); });
|
|
292
|
-
const unsubscribeNamer = this.sessionNamer?.subscribe((agentId, event) => { if (subscriptions.has('*') || subscriptions.has(agentId))
|
|
293
|
-
send({ type: 'session_namer_event', agentId, event }); });
|
|
294
|
-
ws.on('close', () => { if (deltaTimer)
|
|
295
|
-
clearTimeout(deltaTimer); pendingDelta = undefined; this.clients.delete(ws); unsubscribeVoice?.(); unsubscribeNamer?.(); for (const unsubscribe of subscriptions.values())
|
|
296
|
-
unsubscribe(); });
|
|
297
|
-
ws.on('message', raw => {
|
|
298
|
-
void (async () => {
|
|
299
|
-
let m;
|
|
300
|
-
try {
|
|
301
|
-
m = JSON.parse(raw.toString());
|
|
302
|
-
if (m.type === 'subscribe_drafts') {
|
|
303
|
-
subscriptions.get('drafts')?.();
|
|
304
|
-
send({ type: 'subscribed_drafts', protocolVersion: 1 });
|
|
305
|
-
subscriptions.set('drafts', this.drafts.subscribe((draft, replay) => send({ type: 'draft_event', draft, replay })));
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
if (m.type === 'draft_ack') {
|
|
309
|
-
this.drafts.ack(String(m.id ?? ''));
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
if (m.type === 'subscribe_collab') {
|
|
313
|
-
subscriptions.get('collab')?.();
|
|
314
|
-
subscriptions.set('collab', this.collab.subscribe(event => send({ type: 'collab_event', event })));
|
|
315
|
-
send({ type: 'subscribed_collab', protocolVersion: 1 });
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
if (m.type === 'subscribe_all') {
|
|
319
|
-
subscriptions.get('*')?.();
|
|
320
|
-
const forward = (agentId, e) => !subscriptions.has(agentId) && send({ type: 'agent_event', agentId, eventId: e.id, sequence: e.sequence, timestamp: e.timestamp, status: e.status, event: e.event });
|
|
321
|
-
subscriptions.set('*', this.agents.subscribeAll(forward));
|
|
322
|
-
send({ type: 'subscribed_all', protocolVersion: 1 });
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
if (m.type === 'subscribe') {
|
|
326
|
-
const agentId = m.agentId, fromNow = m.fromNow === true;
|
|
327
|
-
subscriptions.get(agentId)?.();
|
|
328
|
-
const forward = (e) => send({ type: 'agent_event', agentId, eventId: e.id, sequence: e.sequence, timestamp: e.timestamp, status: e.status, event: e.event });
|
|
329
|
-
let ready = false, active = true;
|
|
330
|
-
const unsubscribe = this.agents.subscribe(agentId, e => { if (ready)
|
|
331
|
-
forward(e); });
|
|
332
|
-
const stop = () => { active = false; ready = false; unsubscribe(); };
|
|
333
|
-
subscriptions.set(agentId, stop);
|
|
334
|
-
try {
|
|
335
|
-
send({ type: 'subscribed', agentId, protocolVersion: 1, currentSequence: this.agents.currentSequence(agentId) });
|
|
336
|
-
if (!fromNow) {
|
|
337
|
-
const last = Number(m.lastSequence ?? 0), replay = this.agents.events(agentId, last);
|
|
338
|
-
if (this.agents.hasReplayGap(agentId, last) || replay.length > 250) {
|
|
339
|
-
const messageLimit = m.messageLimit === undefined ? undefined : Number(m.messageLimit);
|
|
340
|
-
const snapshot = await this.agents.snapshot(agentId, Number.isFinite(messageLimit) ? messageLimit : undefined);
|
|
341
|
-
if (!active)
|
|
342
|
-
return; // Closed or replaced by a newer subscription while awaiting messages.
|
|
343
|
-
// Do not forward live events ahead of the snapshot. Catch up from the
|
|
344
|
-
// existing bounded replay cache instead of maintaining another buffer.
|
|
345
|
-
const updates = this.agents.events(agentId, snapshot.lastSequence);
|
|
346
|
-
if (this.agents.hasReplayGap(agentId, snapshot.lastSequence) || (updates.length && updates[0].sequence !== snapshot.lastSequence + 1)) {
|
|
347
|
-
stop();
|
|
348
|
-
ws.close(1013, 'Replay gap during snapshot');
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
send({ type: 'agent_snapshot', agentId, ...snapshot });
|
|
352
|
-
for (const event of updates)
|
|
353
|
-
forward(event);
|
|
354
|
-
}
|
|
355
|
-
else
|
|
356
|
-
for (const event of replay)
|
|
357
|
-
forward(event);
|
|
358
|
-
}
|
|
359
|
-
// Refresh/reconnect may already have a saved cursor but no in-memory
|
|
360
|
-
// activity. Send the current IDs after replay, even if replay was empty.
|
|
361
|
-
send({ type: 'agent_state', agentId, sequence: this.agents.currentSequence(agentId), state: this.agents.snapshotState(agentId) });
|
|
362
|
-
ready = true;
|
|
363
|
-
}
|
|
364
|
-
catch (error) {
|
|
365
|
-
if (!active)
|
|
366
|
-
return;
|
|
367
|
-
stop();
|
|
368
|
-
if (subscriptions.get(agentId) === stop)
|
|
369
|
-
subscriptions.delete(agentId);
|
|
370
|
-
throw error;
|
|
371
|
-
}
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
if (['prompt', 'steer', 'follow-up', 'abort'].includes(m.type)) {
|
|
375
|
-
if (m.type !== 'abort') {
|
|
376
|
-
this.voice?.recordUserPrompt(m.agentId, String(m.message ?? ''));
|
|
377
|
-
this.sessionNamer?.recordUserPrompt(m.agentId, String(m.message ?? ''));
|
|
378
|
-
}
|
|
379
|
-
const disposition = await this.agents.command(m.agentId, m.type, m.message);
|
|
380
|
-
send({ type: 'command_result', requestId: m.requestId, success: true, disposition });
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
if (m.type === 'clear-queue') {
|
|
384
|
-
send({ type: 'command_result', requestId: m.requestId, success: true, queue: await this.agents.clearQueue(m.agentId) });
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
throw new Error('Unknown command');
|
|
388
|
-
}
|
|
389
|
-
catch (e) {
|
|
390
|
-
send({ type: 'command_result', requestId: m?.requestId, success: false, error: { code: e.code ?? 'BAD_REQUEST', message: e instanceof SyntaxError ? 'Invalid JSON' : e.message } });
|
|
391
|
-
}
|
|
392
|
-
})();
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
terminalConnection(ws, terminalId) { this.clients.add(ws); let unsubscribe = () => { }; ws.on('close', () => { this.clients.delete(ws); unsubscribe(); }); try {
|
|
396
|
-
const send = (message) => { if (ws.readyState !== WebSocket.OPEN)
|
|
397
|
-
return; if (ws.bufferedAmount > 1024 * 1024) {
|
|
398
|
-
ws.close(1013, 'Terminal client is too slow');
|
|
359
|
+
if (this.noAuth && !this.allowInsecureNoAuth && !this.localRequest(req)) {
|
|
360
|
+
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
361
|
+
socket.destroy();
|
|
399
362
|
return;
|
|
400
|
-
} ws.send(JSON.stringify(message)); };
|
|
401
|
-
unsubscribe = this.terminals.subscribe(terminalId, event => { send(event); if (event.type === 'exit')
|
|
402
|
-
setTimeout(() => ws.close(), 50); });
|
|
403
|
-
const snapshot = this.terminals.snapshot(terminalId);
|
|
404
|
-
send({ type: 'snapshot', data: snapshot.data, record: snapshot.record });
|
|
405
|
-
ws.on('message', raw => { try {
|
|
406
|
-
const message = JSON.parse(raw.toString());
|
|
407
|
-
if (message.type === 'input')
|
|
408
|
-
this.terminals.write(terminalId, message.data);
|
|
409
|
-
else if (message.type === 'resize')
|
|
410
|
-
this.terminals.resize(terminalId, Number(message.cols), Number(message.rows));
|
|
411
|
-
else
|
|
412
|
-
throw new Error('Unknown terminal command');
|
|
413
363
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
ws.send(JSON.stringify({ type: 'error', message: error.message }));
|
|
420
|
-
ws.close();
|
|
421
|
-
} }
|
|
422
|
-
async loadMetadata() { let workspaces = this.metadata.listWorkspaces(); if (!workspaces.length) {
|
|
423
|
-
for (const workspace of this.workspaces.list())
|
|
424
|
-
this.metadata.saveWorkspace(workspace);
|
|
425
|
-
workspaces = this.metadata.listWorkspaces();
|
|
426
|
-
} this.workspaces.replace(workspaces); let agents = this.metadata.listAgents(); if (!agents.length) {
|
|
427
|
-
for (const agent of this.agents.list())
|
|
428
|
-
this.metadata.saveAgent(agent);
|
|
429
|
-
agents = this.metadata.listAgents();
|
|
430
|
-
} await this.agents.restore(agents); }
|
|
431
|
-
async ensureSessionIndex(workspace, cwd, force = false) { const key = `${workspace.id}:${cwd}`, last = this.sessionIndexTimes.get(key) ?? 0; if (!force && Date.now() - last < 30000)
|
|
432
|
-
return; const existing = this.sessionIndexJobs.get(key); if (existing)
|
|
433
|
-
return existing; const job = (async () => { const sessions = await SessionManager.list(cwd); this.metadata.syncSessions(workspace.id, cwd, sessions); this.sessionIndexTimes.set(key, Date.now()); })().finally(() => this.sessionIndexJobs.delete(key)); this.sessionIndexJobs.set(key, job); return job; }
|
|
434
|
-
async indexAgentSession(agentId) { const existing = this.agentIndexJobs.get(agentId); if (existing)
|
|
435
|
-
return existing; const job = (async () => { try {
|
|
436
|
-
const record = this.agents.get(agentId).record, summary = await this.agents.sessionInfo(agentId);
|
|
437
|
-
this.metadata.upsertSessionFromAgent(record, summary);
|
|
438
|
-
this.metadata.scheduleAgent(record);
|
|
439
|
-
}
|
|
440
|
-
catch { /* Session files may not exist until the first assistant response is persisted. */ } })().finally(() => this.agentIndexJobs.delete(agentId)); this.agentIndexJobs.set(agentId, job); return job; }
|
|
441
|
-
async resolveWorkspaceCwd(workspaceId, relativeCwd) { const workspace = this.workspaces.get(workspaceId); if (!workspace)
|
|
442
|
-
throw Object.assign(new Error('Workspace not found'), { code: 'WORKSPACE_NOT_FOUND' }); return this.workspaces.resolve(workspace, relativeCwd || '.'); }
|
|
443
|
-
/** Without a pairing code the only boundary left is the browser's: loopback Host and, if sent, a loopback Origin. */
|
|
444
|
-
localRequest(req) { if (!isLoopbackHost(String(req.headers.host ?? '')))
|
|
445
|
-
return false; const origin = String(req.headers.origin ?? ''); if (!origin)
|
|
446
|
-
return true; try {
|
|
447
|
-
return isLoopbackHost(new URL(origin).host);
|
|
448
|
-
}
|
|
449
|
-
catch {
|
|
450
|
-
return false;
|
|
451
|
-
} }
|
|
452
|
-
async authRole(value) { if (this.noAuth)
|
|
453
|
-
return 'full'; return value?.startsWith('Bearer ') ? this.auth.role(value.slice(7)) : undefined; }
|
|
454
|
-
async serveWeb(pathname, req, res) { const asset = webFiles[pathname]; if (!asset)
|
|
455
|
-
return false; try {
|
|
456
|
-
const content = await readFile(path.isAbsolute(asset.file) ? asset.file : path.join(webRoot, asset.file)), etag = `"${createHash('sha256').update(content).digest('base64url')}"`, ifNoneMatch = String(req.headers['if-none-match'] ?? '').split(',').map(value => value.trim().replace(/^W\//, ''));
|
|
457
|
-
res.setHeader('content-type', asset.type);
|
|
458
|
-
res.setHeader('cache-control', pathname.startsWith('/vendor/') ? 'public, max-age=86400' : 'no-cache');
|
|
459
|
-
res.setHeader('etag', etag);
|
|
460
|
-
res.setHeader('x-content-type-options', 'nosniff');
|
|
461
|
-
if (this.noAuth && asset.type.startsWith('text/html'))
|
|
462
|
-
res.setHeader('content-security-policy', "frame-ancestors 'self'");
|
|
463
|
-
if (ifNoneMatch.includes(etag) || ifNoneMatch.includes('*')) {
|
|
464
|
-
res.statusCode = 304;
|
|
465
|
-
res.end();
|
|
466
|
-
return true;
|
|
364
|
+
const key = this.clientKey(req), retryAfter = this.loginLimiter.retryAfterMs(key);
|
|
365
|
+
if (retryAfter > 0) {
|
|
366
|
+
socket.write(`HTTP/1.1 429 Too Many Requests\r\nRetry-After: ${Math.ceil(retryAfter / 1000)}\r\n\r\n`);
|
|
367
|
+
socket.destroy();
|
|
368
|
+
return;
|
|
467
369
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
async body(req, maxBytes = 2 * 1024 * 1024) { const raw = await this.binaryBody(req, maxBytes); return raw.length ? JSON.parse(raw.toString('utf8')) : {}; }
|
|
476
|
-
async binaryBody(req, maxBytes = 25 * 1024 * 1024) { const chunks = []; let size = 0; for await (const value of req) {
|
|
477
|
-
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
478
|
-
size += chunk.length;
|
|
479
|
-
if (size > maxBytes)
|
|
480
|
-
throw Object.assign(new Error('Request too large'), { code: 'REQUEST_TOO_LARGE' });
|
|
481
|
-
chunks.push(chunk);
|
|
482
|
-
} return Buffer.concat(chunks); }
|
|
483
|
-
async handle(req, res) { const requestId = randomUUID(); res.setHeader('x-request-id', requestId); if (this.noAuth) {
|
|
484
|
-
if (!this.allowInsecureNoAuth && !this.localRequest(req))
|
|
485
|
-
return json(res, 403, { error: { code: 'FORBIDDEN', message: 'Without authentication only loopback requests are served', requestId } });
|
|
486
|
-
if (req.headers.origin)
|
|
487
|
-
res.setHeader('access-control-allow-origin', String(req.headers.origin));
|
|
488
|
-
}
|
|
489
|
-
else
|
|
490
|
-
res.setHeader('access-control-allow-origin', '*'); res.setHeader('access-control-allow-headers', 'Authorization,Content-Type,X-Audio-Filename'); res.setHeader('access-control-allow-methods', 'GET,POST,PUT,DELETE,OPTIONS'); try {
|
|
491
|
-
if (req.method === 'OPTIONS') {
|
|
492
|
-
res.statusCode = 204;
|
|
493
|
-
return res.end();
|
|
370
|
+
const role = (await this.authRole(req.headers.authorization)) ??
|
|
371
|
+
(wsToken ? await this.auth.role(wsToken) : undefined);
|
|
372
|
+
if (!role) {
|
|
373
|
+
this.loginLimiter.recordFailure(key);
|
|
374
|
+
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
|
375
|
+
socket.destroy();
|
|
376
|
+
return;
|
|
494
377
|
}
|
|
495
|
-
|
|
496
|
-
|
|
378
|
+
if (role === 'voice') {
|
|
379
|
+
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
380
|
+
socket.destroy();
|
|
497
381
|
return;
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
return json(res, 429, { error: { code: 'TOO_MANY_ATTEMPTS', message: `Too many failed pairing attempts. Retry in ${Math.ceil(retryAfter / 1000)} seconds`, requestId } });
|
|
382
|
+
}
|
|
383
|
+
this.loginLimiter.clear(key);
|
|
384
|
+
if (voiceMatch || isTranscription) {
|
|
385
|
+
try {
|
|
386
|
+
if (voiceMatch)
|
|
387
|
+
this.agents.get(decodeURIComponent(voiceMatch[1]));
|
|
388
|
+
this.sottoStt.streamUrl();
|
|
506
389
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
return
|
|
390
|
+
catch {
|
|
391
|
+
socket.write('HTTP/1.1 404 Not Found\r\n\r\n');
|
|
392
|
+
socket.destroy();
|
|
393
|
+
return;
|
|
511
394
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
395
|
+
this.wss.handleUpgrade(req, socket, head, ws => {
|
|
396
|
+
this.clients.add(ws);
|
|
397
|
+
ws.on('close', () => this.clients.delete(ws));
|
|
398
|
+
this.sottoStt.stream(ws);
|
|
399
|
+
});
|
|
400
|
+
return;
|
|
516
401
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
this.recordLoginFailure(key);
|
|
529
|
-
else
|
|
530
|
-
this.loginFailures.delete(key);
|
|
531
|
-
return json(res, result.status, result.payload);
|
|
402
|
+
this.wss.handleUpgrade(req, socket, head, ws => this.wss.emit('connection', ws, req));
|
|
403
|
+
}
|
|
404
|
+
clientKey(req) {
|
|
405
|
+
return req.socket.remoteAddress ?? 'unknown';
|
|
406
|
+
}
|
|
407
|
+
async loadMetadata() {
|
|
408
|
+
let workspaces = this.metadata.listWorkspaces();
|
|
409
|
+
if (!workspaces.length) {
|
|
410
|
+
for (const workspace of this.workspaces.list())
|
|
411
|
+
this.metadata.saveWorkspace(workspace);
|
|
412
|
+
workspaces = this.metadata.listWorkspaces();
|
|
532
413
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
const b = await this.body(req), text = typeof b?.text === 'string' ? b.text : '';
|
|
540
|
-
if (!text.trim())
|
|
541
|
-
return json(res, 400, { error: { code: 'INVALID_REQUEST', message: 'text is required', requestId } });
|
|
542
|
-
if (text.length > 20000)
|
|
543
|
-
return json(res, 413, { error: { code: 'REQUEST_TOO_LARGE', message: 'text is too long', requestId } });
|
|
544
|
-
if (b.agentId !== undefined || b.target !== undefined)
|
|
545
|
-
return json(res, 400, { error: { code: 'INVALID_REQUEST', message: 'draft targets are not supported', requestId } });
|
|
546
|
-
if (b.mode !== undefined && b.mode !== 'append' && b.mode !== 'replace')
|
|
547
|
-
return json(res, 400, { error: { code: 'INVALID_REQUEST', message: 'mode must be append or replace', requestId } });
|
|
548
|
-
const { draft, delivered } = this.drafts.add({ text, mode: b.mode ?? 'append', source: typeof b.source === 'string' ? b.source.slice(0, 32) : undefined });
|
|
549
|
-
return json(res, 200, { data: { id: draft.id, mode: draft.mode, delivered, queued: true } });
|
|
414
|
+
this.workspaces.replace(workspaces);
|
|
415
|
+
let agents = this.metadata.listAgents();
|
|
416
|
+
if (!agents.length) {
|
|
417
|
+
for (const agent of this.agents.list())
|
|
418
|
+
this.metadata.saveAgent(agent);
|
|
419
|
+
agents = this.metadata.listAgents();
|
|
550
420
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
if (
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
421
|
+
await this.agents.restore(agents);
|
|
422
|
+
}
|
|
423
|
+
async ensureSessionIndex(workspace, cwd, force = false) {
|
|
424
|
+
const key = `${workspace.id}:${cwd}`, last = this.sessionIndexTimes.get(key) ?? 0;
|
|
425
|
+
if (!force && Date.now() - last < 30000)
|
|
426
|
+
return;
|
|
427
|
+
const existing = this.sessionIndexJobs.get(key);
|
|
428
|
+
if (existing)
|
|
429
|
+
return existing;
|
|
430
|
+
const job = (async () => {
|
|
431
|
+
const sessions = await SessionManager.list(cwd);
|
|
432
|
+
this.metadata.syncSessions(workspace.id, cwd, sessions);
|
|
433
|
+
this.sessionIndexTimes.set(key, Date.now());
|
|
434
|
+
})().finally(() => this.sessionIndexJobs.delete(key));
|
|
435
|
+
this.sessionIndexJobs.set(key, job);
|
|
436
|
+
return job;
|
|
437
|
+
}
|
|
438
|
+
async indexAgentSession(agentId) {
|
|
439
|
+
const existing = this.agentIndexJobs.get(agentId);
|
|
440
|
+
if (existing)
|
|
441
|
+
return existing;
|
|
442
|
+
const job = (async () => {
|
|
443
|
+
try {
|
|
444
|
+
const record = this.agents.get(agentId).record, summary = await this.agents.sessionInfo(agentId);
|
|
445
|
+
this.metadata.upsertSessionFromAgent(record, summary);
|
|
446
|
+
this.metadata.scheduleAgent(record);
|
|
575
447
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
throw Object.assign(new Error('collabEscalations must be a boolean'), { code: 'INVALID_MAIL_NOTIFICATION_SETTINGS' });
|
|
579
|
-
patch.collabEscalations = b.collabEscalations;
|
|
448
|
+
catch {
|
|
449
|
+
/* Session files may not exist until the first assistant response is persisted. */
|
|
580
450
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
if (
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
return json(res, 200, { data: await this.workspaces.stat(parts[3], u.searchParams.get('path') ?? '.') });
|
|
601
|
-
if (req.method === 'GET' && u.pathname === '/api/v1/sessions') {
|
|
602
|
-
const workspace = this.workspaces.get(u.searchParams.get('workspaceId') ?? '');
|
|
603
|
-
if (!workspace)
|
|
604
|
-
throw Object.assign(new Error('Workspace not found'), { code: 'WORKSPACE_NOT_FOUND' });
|
|
605
|
-
const cwd = await this.workspaces.resolve(workspace, u.searchParams.get('path') ?? '.');
|
|
606
|
-
await this.ensureSessionIndex(workspace, cwd, u.searchParams.get('refresh') === 'true');
|
|
607
|
-
const limit = u.searchParams.has('limit') ? Number(u.searchParams.get('limit')) : undefined, offset = Number(u.searchParams.get('offset') ?? 0);
|
|
608
|
-
return json(res, 200, { data: this.metadata.listSessions(cwd, limit, offset) });
|
|
609
|
-
}
|
|
610
|
-
if (req.method === 'GET' && u.pathname === '/api/v1/terminals')
|
|
611
|
-
return json(res, 200, { data: this.terminals.list() });
|
|
612
|
-
if (req.method === 'POST' && u.pathname === '/api/v1/terminals') {
|
|
613
|
-
const b = await this.body(req);
|
|
614
|
-
const terminal = await this.terminals.create(String(b.workspaceId ?? ''), String(b.relativeCwd ?? '.'), Number(b.cols ?? 80), Number(b.rows ?? 24));
|
|
615
|
-
return json(res, 201, { data: terminal });
|
|
451
|
+
})().finally(() => this.agentIndexJobs.delete(agentId));
|
|
452
|
+
this.agentIndexJobs.set(agentId, job);
|
|
453
|
+
return job;
|
|
454
|
+
}
|
|
455
|
+
async resolveWorkspaceCwd(workspaceId, relativeCwd) {
|
|
456
|
+
const workspace = this.workspaces.get(workspaceId);
|
|
457
|
+
if (!workspace)
|
|
458
|
+
throw Object.assign(new Error('Workspace not found'), { code: 'WORKSPACE_NOT_FOUND' });
|
|
459
|
+
return this.workspaces.resolve(workspace, relativeCwd || '.');
|
|
460
|
+
}
|
|
461
|
+
/** Without a pairing code the only boundary left is the browser's: loopback Host and, if sent, a loopback Origin. */
|
|
462
|
+
localRequest(req) {
|
|
463
|
+
if (!isLoopbackHost(String(req.headers.host ?? '')))
|
|
464
|
+
return false;
|
|
465
|
+
const origin = String(req.headers.origin ?? '');
|
|
466
|
+
if (!origin)
|
|
467
|
+
return true;
|
|
468
|
+
try {
|
|
469
|
+
return isLoopbackHost(new URL(origin).host);
|
|
616
470
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
if (req.method === 'GET' && parts.length === 4)
|
|
620
|
-
return json(res, 200, { data: this.terminals.get(id).record });
|
|
621
|
-
if (req.method === 'DELETE' && parts.length === 4) {
|
|
622
|
-
this.terminals.close(id);
|
|
623
|
-
return json(res, 200, { data: { closed: true } });
|
|
624
|
-
}
|
|
471
|
+
catch {
|
|
472
|
+
return false;
|
|
625
473
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
474
|
+
}
|
|
475
|
+
async authRole(value) {
|
|
476
|
+
if (this.noAuth)
|
|
477
|
+
return 'full';
|
|
478
|
+
return value?.startsWith('Bearer ') ? this.auth.role(value.slice(7)) : undefined;
|
|
479
|
+
}
|
|
480
|
+
async handle(req, res) {
|
|
481
|
+
const requestId = randomUUID();
|
|
482
|
+
res.setHeader('x-request-id', requestId);
|
|
483
|
+
if (this.noAuth) {
|
|
484
|
+
if (!this.allowInsecureNoAuth && !this.localRequest(req))
|
|
485
|
+
return json(res, 403, {
|
|
486
|
+
error: {
|
|
487
|
+
code: 'FORBIDDEN',
|
|
488
|
+
message: 'Without authentication only loopback requests are served',
|
|
489
|
+
requestId
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
if (req.headers.origin)
|
|
493
|
+
res.setHeader('access-control-allow-origin', String(req.headers.origin));
|
|
629
494
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
const workspace = this.workspaces.get(b.workspaceId);
|
|
639
|
-
if (!workspace)
|
|
640
|
-
throw Object.assign(new Error('Workspace not found'), { code: 'WORKSPACE_NOT_FOUND' });
|
|
641
|
-
const cwd = await this.workspaces.resolve(workspace, b.relativeCwd ?? '.');
|
|
642
|
-
await this.ensureSessionIndex(workspace, cwd);
|
|
643
|
-
if (!this.metadata.hasSession(cwd, b.sessionFile))
|
|
644
|
-
throw Object.assign(new Error('Session is not valid for cwd'), { code: 'SESSION_NOT_FOUND' });
|
|
645
|
-
sessionFile = b.sessionFile;
|
|
495
|
+
else
|
|
496
|
+
res.setHeader('access-control-allow-origin', '*');
|
|
497
|
+
res.setHeader('access-control-allow-headers', 'Authorization,Content-Type,X-Audio-Filename');
|
|
498
|
+
res.setHeader('access-control-allow-methods', 'GET,POST,PUT,DELETE,OPTIONS');
|
|
499
|
+
try {
|
|
500
|
+
if (req.method === 'OPTIONS') {
|
|
501
|
+
res.statusCode = 204;
|
|
502
|
+
return res.end();
|
|
646
503
|
}
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
504
|
+
const u = new URL(req.url ?? '/', `http://${req.headers.host ?? 'localhost'}`);
|
|
505
|
+
if (req.method === 'GET' && (await serveWebAsset(u.pathname, req, res, { noAuth: this.noAuth })))
|
|
506
|
+
return;
|
|
507
|
+
if (req.method === 'GET' && u.pathname === '/health')
|
|
508
|
+
return json(res, 200, { data: { status: 'ok', authRequired: !this.noAuth } });
|
|
509
|
+
const parts = u.pathname.split('/').filter(Boolean);
|
|
510
|
+
if (req.method === 'POST' && u.pathname === '/api/v1/auth/login') {
|
|
511
|
+
const key = this.clientKey(req), retryAfter = this.loginLimiter.retryAfterMs(key);
|
|
512
|
+
if (retryAfter > 0) {
|
|
513
|
+
res.setHeader('retry-after', String(Math.ceil(retryAfter / 1000)));
|
|
514
|
+
return json(res, 429, {
|
|
515
|
+
error: {
|
|
516
|
+
code: 'TOO_MANY_ATTEMPTS',
|
|
517
|
+
message: `Too many failed pairing attempts. Retry in ${Math.ceil(retryAfter / 1000)} seconds`,
|
|
518
|
+
requestId
|
|
519
|
+
}
|
|
520
|
+
});
|
|
653
521
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
522
|
+
const b = await readJsonBody(req);
|
|
523
|
+
if (await this.auth.verify(String(b.token ?? ''))) {
|
|
524
|
+
this.loginLimiter.clear(key);
|
|
525
|
+
return json(res, 200, { data: { authenticated: true } });
|
|
658
526
|
}
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
if (
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
if (
|
|
694
|
-
return json(res,
|
|
695
|
-
if (
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
if (parts[4] === 'queue') {
|
|
747
|
-
if (req.method === 'GET')
|
|
748
|
-
return json(res, 200, { data: await this.agents.queue(id) });
|
|
749
|
-
if (req.method === 'DELETE')
|
|
750
|
-
return json(res, 200, { data: await this.agents.clearQueue(id) });
|
|
751
|
-
}
|
|
752
|
-
if (parts[4] === 'mcp') {
|
|
753
|
-
const cwd = this.agents.get(id).record.cwd;
|
|
527
|
+
const lockedFor = this.loginLimiter.recordFailure(key);
|
|
528
|
+
if (lockedFor > 0)
|
|
529
|
+
res.setHeader('retry-after', String(Math.ceil(lockedFor / 1000)));
|
|
530
|
+
return json(res, 401, { error: { code: 'UNAUTHORIZED', message: 'Invalid token', requestId } });
|
|
531
|
+
}
|
|
532
|
+
if (parts[0] === 'api' && parts[1] === 'v1' && parts[2] === 'collab') {
|
|
533
|
+
const key = this.clientKey(req), retryAfter = this.loginLimiter.retryAfterMs(key);
|
|
534
|
+
if (retryAfter > 0) {
|
|
535
|
+
res.setHeader('retry-after', String(Math.ceil(retryAfter / 1000)));
|
|
536
|
+
return json(res, 429, {
|
|
537
|
+
error: {
|
|
538
|
+
code: 'TOO_MANY_ATTEMPTS',
|
|
539
|
+
message: `Too many failed attempts. Retry in ${Math.ceil(retryAfter / 1000)} seconds`,
|
|
540
|
+
requestId
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
const header = String(req.headers.authorization ?? ''), token = header.startsWith('Bearer ') ? header.slice(7) : '';
|
|
545
|
+
const result = await this.collabRouter.handle({
|
|
546
|
+
method: req.method ?? 'GET',
|
|
547
|
+
url: u,
|
|
548
|
+
parts,
|
|
549
|
+
token,
|
|
550
|
+
body: () => readJsonBody(req)
|
|
551
|
+
});
|
|
552
|
+
if (!result)
|
|
553
|
+
return json(res, 404, { error: { code: 'NOT_FOUND', message: 'Not found', requestId } });
|
|
554
|
+
if (result.authFailed)
|
|
555
|
+
this.loginLimiter.recordFailure(key);
|
|
556
|
+
else
|
|
557
|
+
this.loginLimiter.clear(key);
|
|
558
|
+
return json(res, result.status, result.payload);
|
|
559
|
+
}
|
|
560
|
+
const role = await this.authRole(req.headers.authorization);
|
|
561
|
+
if (!role)
|
|
562
|
+
return json(res, 401, { error: { code: 'UNAUTHORIZED', message: 'Authentication required', requestId } });
|
|
563
|
+
if (role === 'voice' &&
|
|
564
|
+
!((req.method === 'POST' && u.pathname === '/api/v1/draft') ||
|
|
565
|
+
(req.method === 'GET' && u.pathname === '/api/v1/system/status')))
|
|
566
|
+
return json(res, 403, {
|
|
567
|
+
error: { code: 'FORBIDDEN', message: 'This token cannot access this endpoint', requestId }
|
|
568
|
+
});
|
|
569
|
+
if (req.method === 'POST' && u.pathname === '/api/v1/draft') {
|
|
570
|
+
const b = await readJsonBody(req), text = typeof b?.text === 'string' ? b.text : '';
|
|
571
|
+
if (!text.trim())
|
|
572
|
+
return json(res, 400, { error: { code: 'INVALID_REQUEST', message: 'text is required', requestId } });
|
|
573
|
+
if (text.length > 20000)
|
|
574
|
+
return json(res, 413, { error: { code: 'REQUEST_TOO_LARGE', message: 'text is too long', requestId } });
|
|
575
|
+
if (b.agentId !== undefined || b.target !== undefined)
|
|
576
|
+
return json(res, 400, {
|
|
577
|
+
error: { code: 'INVALID_REQUEST', message: 'draft targets are not supported', requestId }
|
|
578
|
+
});
|
|
579
|
+
if (b.mode !== undefined && b.mode !== 'append' && b.mode !== 'replace')
|
|
580
|
+
return json(res, 400, {
|
|
581
|
+
error: { code: 'INVALID_REQUEST', message: 'mode must be append or replace', requestId }
|
|
582
|
+
});
|
|
583
|
+
const { draft, delivered } = this.drafts.add({
|
|
584
|
+
text,
|
|
585
|
+
mode: b.mode ?? 'append',
|
|
586
|
+
source: typeof b.source === 'string' ? b.source.slice(0, 32) : undefined
|
|
587
|
+
});
|
|
588
|
+
return json(res, 200, { data: { id: draft.id, mode: draft.mode, delivered, queued: true } });
|
|
589
|
+
}
|
|
590
|
+
if (req.method === 'GET' && u.pathname === '/api/v1/system/status')
|
|
591
|
+
return json(res, 200, {
|
|
592
|
+
data: {
|
|
593
|
+
version: pkg.version,
|
|
594
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
595
|
+
authRequired: !this.noAuth,
|
|
596
|
+
piVersion: PI_VERSION,
|
|
597
|
+
nodeVersion: process.version,
|
|
598
|
+
uptimeSeconds: Math.floor(process.uptime()),
|
|
599
|
+
agentCount: this.agents.list().length,
|
|
600
|
+
terminalCount: this.terminals.list().length,
|
|
601
|
+
webSocketConnections: this.clients.size,
|
|
602
|
+
voiceEnabled: !!this.voice,
|
|
603
|
+
voiceCapabilities: {
|
|
604
|
+
tts: this.voice?.capabilities().tts ?? false,
|
|
605
|
+
stt: (this.voice?.capabilities().stt ?? false) || this.sottoStt.getSettings().enabled
|
|
606
|
+
},
|
|
607
|
+
sottoStt: this.sottoStt.getSettings(),
|
|
608
|
+
sessionNamingEnabled: !!this.sessionNamer,
|
|
609
|
+
mailNotificationsConfigured: !!this.mailNotifier,
|
|
610
|
+
mailNotificationsEnabled: this.mailNotifier?.getSettings().enabled ?? false
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
if (u.pathname === '/api/v1/sotto-stt') {
|
|
754
614
|
if (req.method === 'GET')
|
|
755
|
-
return json(res, 200, { data:
|
|
756
|
-
if (req.method === '
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
615
|
+
return json(res, 200, { data: this.sottoStt.getSettings() });
|
|
616
|
+
if (req.method === 'PUT')
|
|
617
|
+
return json(res, 200, { data: await this.sottoStt.update(await readJsonBody(req)) });
|
|
618
|
+
}
|
|
619
|
+
if (req.method === 'POST' && u.pathname === '/api/v1/transcribe') {
|
|
620
|
+
const mime = String(req.headers['content-type'] ?? 'audio/webm').split(';')[0];
|
|
621
|
+
if (this.sottoStt.getSettings().enabled) {
|
|
622
|
+
if (mime !== 'application/octet-stream')
|
|
623
|
+
throw Object.assign(new Error('Sotto requires 16 kHz float32 PCM'), { code: 'INVALID_REQUEST' });
|
|
624
|
+
return json(res, 200, { data: await this.sottoStt.transcribe(await readBinaryBody(req)) });
|
|
761
625
|
}
|
|
762
|
-
}
|
|
763
|
-
if (req.method === 'POST' && parts[4] === 'extension-response') {
|
|
764
|
-
const b = await this.body(req);
|
|
765
|
-
await this.agents.extensionResponse(id, b.requestId, b.value);
|
|
766
|
-
return json(res, 200, { data: { success: true } });
|
|
767
|
-
}
|
|
768
|
-
if (req.method === 'POST' && parts[4] === 'transcribe') {
|
|
769
626
|
if (!this.voice)
|
|
770
627
|
throw Object.assign(new Error('Voice service is disabled'), { code: 'VOICE_DISABLED' });
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
if (req.method === 'POST' && ['prompt', 'steer', 'follow-up', 'abort'].includes(parts[4])) {
|
|
776
|
-
const b = await this.body(req);
|
|
777
|
-
if (parts[4] !== 'abort') {
|
|
778
|
-
this.voice?.recordUserPrompt(id, String(b.message ?? ''));
|
|
779
|
-
this.sessionNamer?.recordUserPrompt(id, String(b.message ?? ''));
|
|
780
|
-
}
|
|
781
|
-
const disposition = await this.agents.command(id, parts[4], b.message ?? '');
|
|
782
|
-
const record = this.agents.get(id).record;
|
|
783
|
-
this.metadata.scheduleAgent(record);
|
|
784
|
-
if (parts[4] === 'prompt')
|
|
785
|
-
void this.indexAgentSession(id);
|
|
786
|
-
return json(res, 200, { data: { success: true, disposition } });
|
|
628
|
+
const filename = String(req.headers['x-audio-filename'] ?? 'recording.webm');
|
|
629
|
+
return json(res, 200, {
|
|
630
|
+
data: await this.voice.transcribe(await readBinaryBody(req), mime, filename)
|
|
631
|
+
});
|
|
787
632
|
}
|
|
633
|
+
if (await handleMailSettingsRoutes({ mailNotifier: this.mailNotifier, metadata: this.metadata }, req, res, u))
|
|
634
|
+
return;
|
|
635
|
+
const workspaceContext = {
|
|
636
|
+
workspaces: this.workspaces,
|
|
637
|
+
terminals: this.terminals,
|
|
638
|
+
metadata: this.metadata,
|
|
639
|
+
ensureSessionIndex: (workspace, cwd, force) => this.ensureSessionIndex(workspace, cwd, force)
|
|
640
|
+
};
|
|
641
|
+
if (await handleWorkspaceRoutes(workspaceContext, req, res, u, parts, requestId))
|
|
642
|
+
return;
|
|
643
|
+
if (await handleSystemPromptRoutes({ agents: this.agents, templates: this.templates, body: () => readJsonBody(req) }, req, res, u, parts))
|
|
644
|
+
return;
|
|
645
|
+
const agentContext = {
|
|
646
|
+
agents: this.agents,
|
|
647
|
+
templates: this.templates,
|
|
648
|
+
metadata: this.metadata,
|
|
649
|
+
workspaces: this.workspaces,
|
|
650
|
+
voice: this.voice,
|
|
651
|
+
sottoStt: this.sottoStt,
|
|
652
|
+
sessionNamer: this.sessionNamer,
|
|
653
|
+
ensureSessionIndex: (workspace, cwd, force) => this.ensureSessionIndex(workspace, cwd, force),
|
|
654
|
+
indexAgentSession: (agentId) => this.indexAgentSession(agentId),
|
|
655
|
+
body: () => readJsonBody(req),
|
|
656
|
+
binaryBody: () => readBinaryBody(req)
|
|
657
|
+
};
|
|
658
|
+
if (await handleAgentRoutes(agentContext, req, res, u, parts))
|
|
659
|
+
return;
|
|
660
|
+
return json(res, 404, { error: { code: 'NOT_FOUND', message: 'Not found', requestId } });
|
|
661
|
+
}
|
|
662
|
+
catch (e) {
|
|
663
|
+
const x = e;
|
|
664
|
+
return json(res, errorStatus(x.code), {
|
|
665
|
+
error: { code: x.code ?? 'BAD_REQUEST', message: x.message ?? 'Request failed', requestId }
|
|
666
|
+
});
|
|
788
667
|
}
|
|
789
|
-
return json(res, 404, { error: { code: 'NOT_FOUND', message: 'Not found', requestId } });
|
|
790
668
|
}
|
|
791
|
-
catch (e) {
|
|
792
|
-
const x = e;
|
|
793
|
-
return json(res, x.code === 'WORKSPACE_PATH_OUTSIDE_ROOT' ? 403 : x.code === 'FILE_TOO_LARGE' || x.code === 'REQUEST_TOO_LARGE' ? 413 : x.code === 'VOICE_DISABLED' || x.code === 'VOICE_STT_DISABLED' || x.code === 'MAIL_NOTIFICATIONS_DISABLED' ? 503 : x.code?.endsWith('NOT_FOUND') ? 404 : 400, { error: { code: x.code ?? 'BAD_REQUEST', message: x.message ?? 'Request failed', requestId } });
|
|
794
|
-
} }
|
|
795
669
|
}
|