@tianshu-ai/tianshu 0.140.0 → 0.141.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/server/dist/boot/moss-live-ws.d.ts +17 -0
- package/packages/server/dist/boot/moss-live-ws.d.ts.map +1 -0
- package/packages/server/dist/boot/moss-live-ws.js +211 -0
- package/packages/server/dist/boot/moss-live-ws.js.map +1 -0
- package/packages/server/dist/boot/routes-moss-live.d.ts +18 -0
- package/packages/server/dist/boot/routes-moss-live.d.ts.map +1 -0
- package/packages/server/dist/boot/routes-moss-live.js +122 -0
- package/packages/server/dist/boot/routes-moss-live.js.map +1 -0
- package/packages/server/dist/chat/handler.d.ts.map +1 -1
- package/packages/server/dist/chat/handler.js +146 -0
- package/packages/server/dist/chat/handler.js.map +1 -1
- package/packages/server/dist/chat/moss-live-tunnel.d.ts +36 -0
- package/packages/server/dist/chat/moss-live-tunnel.d.ts.map +1 -0
- package/packages/server/dist/chat/moss-live-tunnel.js +246 -0
- package/packages/server/dist/chat/moss-live-tunnel.js.map +1 -0
- package/packages/server/dist/chat/ws-protocol.d.ts +10 -0
- package/packages/server/dist/chat/ws-protocol.d.ts.map +1 -1
- package/packages/server/dist/chat/ws-protocol.js.map +1 -1
- package/packages/server/dist/core/config.d.ts +12 -0
- package/packages/server/dist/core/config.d.ts.map +1 -1
- package/packages/server/dist/core/config.js +2 -0
- package/packages/server/dist/core/config.js.map +1 -1
- package/packages/server/dist/index.js +4 -0
- package/packages/server/dist/index.js.map +1 -1
- package/packages/web/dist/assets/{DataSourceConfigForm-Blc-iaZi.js → DataSourceConfigForm-CeheiJKu.js} +1 -1
- package/packages/web/dist/assets/{bundle-web-BmyFrAag.js → bundle-web-D2sc5FJO.js} +1 -1
- package/packages/web/dist/assets/index-4Ys0HS4q.css +2 -0
- package/packages/web/dist/assets/{index-Bm7ShD92.js → index-B47RXT0t.js} +61 -45
- package/packages/web/dist/assets/{react-force-graph-2d-CoIlwddG.js → react-force-graph-2d-Cq8Hepfv.js} +1 -1
- package/packages/web/dist/index.html +2 -2
- package/plugins/messenger/dist/adapters/wechat.d.ts.map +1 -1
- package/plugins/messenger/dist/adapters/wechat.js +12 -2
- package/plugins/messenger/dist/adapters/wechat.js.map +1 -1
- package/plugins/messenger/dist/server.js +81 -24
- package/plugins/messenger/dist/server.js.map +2 -2
- package/plugins/messenger/dist/tool.d.ts.map +1 -1
- package/plugins/messenger/dist/tool.js +32 -13
- package/plugins/messenger/dist/tool.js.map +1 -1
- package/packages/web/dist/assets/index-DpbPhUTy.css +0 -2
package/package.json
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moss-Live realtime voice WebSocket proxy.
|
|
3
|
+
*
|
|
4
|
+
* Browser ←WS /moss-live→ tianshu server ←WS→ Moss-Live API
|
|
5
|
+
*
|
|
6
|
+
* The server relays audio + events bidirectionally and intercepts
|
|
7
|
+
* delegation events to route them to the tianshu agent.
|
|
8
|
+
*/
|
|
9
|
+
import type { Server as HttpServer } from "node:http";
|
|
10
|
+
import { WebSocketServer } from "ws";
|
|
11
|
+
import type { GlobalOps } from "../core/global-ops.js";
|
|
12
|
+
export interface InstallMossLiveWsDeps {
|
|
13
|
+
server: HttpServer;
|
|
14
|
+
globalOps: GlobalOps;
|
|
15
|
+
}
|
|
16
|
+
export declare function installMossLiveWebSocket(deps: InstallMossLiveWsDeps): WebSocketServer;
|
|
17
|
+
//# sourceMappingURL=moss-live-ws.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moss-live-ws.d.ts","sourceRoot":"","sources":["../../src/boot/moss-live-ws.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,EAAa,eAAe,EAAE,MAAM,IAAI,CAAC;AAQhD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAUvD,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,UAAU,CAAC;IACnB,SAAS,EAAE,SAAS,CAAC;CACtB;AAED,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,qBAAqB,GAC1B,eAAe,CAoLjB"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moss-Live realtime voice WebSocket proxy.
|
|
3
|
+
*
|
|
4
|
+
* Browser ←WS /moss-live→ tianshu server ←WS→ Moss-Live API
|
|
5
|
+
*
|
|
6
|
+
* The server relays audio + events bidirectionally and intercepts
|
|
7
|
+
* delegation events to route them to the tianshu agent.
|
|
8
|
+
*/
|
|
9
|
+
import { WebSocket, WebSocketServer } from "ws";
|
|
10
|
+
import { runIdentityChain, buildResolverChain, loadGlobalConfig, DEV_TENANT_ID, TenantNotFoundError, } from "../core/index.js";
|
|
11
|
+
import { createLogger } from "../setup/logger.js";
|
|
12
|
+
const log = createLogger("moss-live");
|
|
13
|
+
const MOSS_LIVE_URL = "wss://api.mosi.cn/v1/realtime";
|
|
14
|
+
const DEFAULT_MODEL = "moss-live-1.0";
|
|
15
|
+
export function installMossLiveWebSocket(deps) {
|
|
16
|
+
const { server, globalOps } = deps;
|
|
17
|
+
// Use noServer mode to avoid conflicting with other WSS on the same
|
|
18
|
+
// HTTP server (which breaks Vite dev proxy). We manually handle the
|
|
19
|
+
// upgrade event for the /moss-live path.
|
|
20
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
21
|
+
server.on("upgrade", (request, socket, head) => {
|
|
22
|
+
const url = new URL(request.url || "/", `http://${request.headers.host}`);
|
|
23
|
+
if (url.pathname !== "/moss-live")
|
|
24
|
+
return; // let other handlers (chat /ws) proceed
|
|
25
|
+
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
26
|
+
wss.emit("connection", ws, request);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
wss.on("connection", async (clientWs, request) => {
|
|
30
|
+
// ── Identity resolution (same as chat WS) ──
|
|
31
|
+
const { resolution, error: chainError } = runIdentityChain(request, buildResolverChain(loadGlobalConfig().auth));
|
|
32
|
+
if (chainError || !resolution || resolution.kind === "deny") {
|
|
33
|
+
clientWs.send(JSON.stringify({
|
|
34
|
+
type: "stream_error",
|
|
35
|
+
reason: chainError
|
|
36
|
+
? `identity resolver "${chainError.resolver}" threw: ${chainError.message}`
|
|
37
|
+
: resolution?.kind === "deny"
|
|
38
|
+
? `denied by ${resolution.source}: ${resolution.reason}`
|
|
39
|
+
: "no identity resolver claimed this WS upgrade",
|
|
40
|
+
}));
|
|
41
|
+
clientWs.close();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const tenantId = resolution.tenantId;
|
|
45
|
+
let ctx;
|
|
46
|
+
try {
|
|
47
|
+
ctx = globalOps.open(tenantId);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (err instanceof TenantNotFoundError) {
|
|
51
|
+
try {
|
|
52
|
+
ctx = globalOps.open(DEV_TENANT_ID);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
clientWs.send(JSON.stringify({ type: "stream_error", reason: "tenant unavailable" }));
|
|
56
|
+
clientWs.close();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
clientWs.send(JSON.stringify({ type: "stream_error", reason: String(err) }));
|
|
62
|
+
clientWs.close();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// ── Read Moss-Live config ──
|
|
67
|
+
const config = ctx.config.mossLive;
|
|
68
|
+
if (!config?.apiKey) {
|
|
69
|
+
clientWs.send(JSON.stringify({
|
|
70
|
+
type: "stream_error",
|
|
71
|
+
reason: "Moss-Live API key not configured. Go to Settings → Voice to add your key.",
|
|
72
|
+
}));
|
|
73
|
+
clientWs.close();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
log.info(`[moss-live] new session for tenant=${tenantId} user=${resolution.userId}`);
|
|
77
|
+
// ── Open upstream WS to Moss-Live ──
|
|
78
|
+
const model = config.model || DEFAULT_MODEL;
|
|
79
|
+
const upstreamUrl = `${MOSS_LIVE_URL}?model=${encodeURIComponent(model)}`;
|
|
80
|
+
const upstream = new WebSocket(upstreamUrl, {
|
|
81
|
+
headers: { Authorization: `Bearer ${config.apiKey}` },
|
|
82
|
+
});
|
|
83
|
+
let sessionId = null;
|
|
84
|
+
// ── Upstream lifecycle ──
|
|
85
|
+
upstream.on("open", () => {
|
|
86
|
+
log.info("[moss-live] upstream connected");
|
|
87
|
+
upstream.send(JSON.stringify({
|
|
88
|
+
action: "attach_session",
|
|
89
|
+
delegation: { type: "client" },
|
|
90
|
+
}));
|
|
91
|
+
});
|
|
92
|
+
upstream.on("message", (data, isBinary) => {
|
|
93
|
+
// Intercept text messages for delegation handling
|
|
94
|
+
if (!isBinary) {
|
|
95
|
+
const str = data.toString();
|
|
96
|
+
try {
|
|
97
|
+
const msg = JSON.parse(str);
|
|
98
|
+
handleUpstreamEvent(msg, upstream, ctx);
|
|
99
|
+
}
|
|
100
|
+
catch { /* not JSON */ }
|
|
101
|
+
}
|
|
102
|
+
// Relay everything to client
|
|
103
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
104
|
+
clientWs.send(data, { binary: isBinary });
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
upstream.on("error", (err) => {
|
|
108
|
+
log.error(`[moss-live] upstream error: ${err.message}`);
|
|
109
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
110
|
+
clientWs.send(JSON.stringify({
|
|
111
|
+
action: "error",
|
|
112
|
+
data: { code: "upstream_error", message: err.message },
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
upstream.on("close", (code, reason) => {
|
|
117
|
+
log.info(`[moss-live] upstream closed: code=${code}`);
|
|
118
|
+
if (clientWs.readyState === WebSocket.OPEN) {
|
|
119
|
+
clientWs.close(code, reason);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
// ── Client → Upstream relay ──
|
|
123
|
+
clientWs.on("message", (data, isBinary) => {
|
|
124
|
+
if (upstream.readyState === WebSocket.OPEN) {
|
|
125
|
+
upstream.send(data, { binary: isBinary });
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
clientWs.on("close", () => {
|
|
129
|
+
log.info("[moss-live] client disconnected");
|
|
130
|
+
if (upstream.readyState === WebSocket.OPEN)
|
|
131
|
+
upstream.close();
|
|
132
|
+
});
|
|
133
|
+
clientWs.on("error", (err) => {
|
|
134
|
+
log.error(`[moss-live] client error: ${err.message}`);
|
|
135
|
+
});
|
|
136
|
+
// ── Event handling ──
|
|
137
|
+
function handleUpstreamEvent(msg, ws, _ctx) {
|
|
138
|
+
const action = msg.action;
|
|
139
|
+
const data = msg.data;
|
|
140
|
+
if (action === "session_attached") {
|
|
141
|
+
sessionId = data?.session_id ?? null;
|
|
142
|
+
log.info(`[moss-live] session attached: ${sessionId}`);
|
|
143
|
+
// Configure persona + instructions
|
|
144
|
+
const update = {};
|
|
145
|
+
if (config.persona)
|
|
146
|
+
update.persona = { prompt: config.persona };
|
|
147
|
+
if (config.instructions)
|
|
148
|
+
update.instructions = config.instructions;
|
|
149
|
+
if (Object.keys(update).length > 0) {
|
|
150
|
+
ws.send(JSON.stringify({ action: "session.update", session: update }));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// ── Delegation: route to tianshu agent ──
|
|
154
|
+
if (action === "session.delegation.created") {
|
|
155
|
+
const delegation = data?.delegation;
|
|
156
|
+
if (!delegation)
|
|
157
|
+
return;
|
|
158
|
+
const delegationId = delegation.id;
|
|
159
|
+
const objective = delegation.objective;
|
|
160
|
+
const purpose = delegation.purpose || "task";
|
|
161
|
+
const context = delegation.context || [];
|
|
162
|
+
log.info(`[moss-live] delegation: id=${delegationId} objective=${objective?.slice(0, 80)}`);
|
|
163
|
+
// TODO: route to tianshu agent via chat handler
|
|
164
|
+
// For now, echo back a simple response
|
|
165
|
+
executeDelegation(ws, delegationId, objective, purpose, context);
|
|
166
|
+
}
|
|
167
|
+
if (action === "session.delegation.cancelled") {
|
|
168
|
+
const delegationId = data?.delegation?.id;
|
|
169
|
+
log.info(`[moss-live] delegation cancelled: ${delegationId}`);
|
|
170
|
+
// TODO: abort running agent task
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return wss;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Execute a delegation by routing to the tianshu agent.
|
|
178
|
+
* Phase 1: simple echo. Phase 2: wire to actual agent loop.
|
|
179
|
+
*/
|
|
180
|
+
async function executeDelegation(upstream, delegationId, objective, _purpose, _context) {
|
|
181
|
+
try {
|
|
182
|
+
// Report progress
|
|
183
|
+
sendAppend(upstream, delegationId, "thinking", `正在处理: ${objective.slice(0, 100)}`);
|
|
184
|
+
// TODO Phase 2: create a temporary agent session, send the objective
|
|
185
|
+
// as a user message, stream tool calls + results back as
|
|
186
|
+
// thinking/commentary appends, and return the final answer.
|
|
187
|
+
// Phase 1: echo back
|
|
188
|
+
sendAppend(upstream, delegationId, "commentary", `收到任务: ${objective}`, "completed");
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
192
|
+
log.error(`[moss-live] delegation failed: ${msg}`);
|
|
193
|
+
sendAppend(upstream, delegationId, "commentary", `执行失败: ${msg}`, "failed");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function sendAppend(ws, delegationId, type, text, status) {
|
|
197
|
+
if (ws.readyState !== WebSocket.OPEN)
|
|
198
|
+
return;
|
|
199
|
+
const payload = {
|
|
200
|
+
action: type === "thinking" ? "session.thinking.append" : "session.commentary.append",
|
|
201
|
+
delegation_id: delegationId,
|
|
202
|
+
};
|
|
203
|
+
if (type === "thinking") {
|
|
204
|
+
payload.thinking = { text };
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
payload.commentary = { text, status: status || "in_progress" };
|
|
208
|
+
}
|
|
209
|
+
ws.send(JSON.stringify(payload));
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=moss-live-ws.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moss-live-ws.js","sourceRoot":"","sources":["../../src/boot/moss-live-ws.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC;AAChD,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EAChB,aAAa,EACb,mBAAmB,GACpB,MAAM,kBAAkB,CAAC;AAI1B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,GAAG,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;AAEtC,MAAM,aAAa,GAAG,+BAA+B,CAAC;AACtD,MAAM,aAAa,GAAG,eAAe,CAAC;AAOtC,MAAM,UAAU,wBAAwB,CACtC,IAA2B;IAE3B,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;IACnC,oEAAoE;IACpE,oEAAoE;IACpE,yCAAyC;IACzC,MAAM,GAAG,GAAG,IAAI,eAAe,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAEpD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;QAC7C,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,GAAG,EAAE,UAAU,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QAC1E,IAAI,GAAG,CAAC,QAAQ,KAAK,YAAY;YAAE,OAAO,CAAC,wCAAwC;QACnF,GAAG,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI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AE/C,IAAI,EAAE,CAAC,UAAU,KAAK,SAAS,CAAC,IAAI;QAAE,OAAO;IAC7C,MAAM,OAAO,GAA4B;QACvC,MAAM,EAAE,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC,2BAA2B;QACrF,aAAa,EAAE,YAAY;KAC5B,CAAC;IACF,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACxB,OAAO,CAAC,QAAQ,GAAG,EAAE,IAAI,EAAE,CAAC;IAC9B,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,UAAU,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,IAAI,aAAa,EAAE,CAAC;IACjE,CAAC;IACD,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /api/moss-live/config — GET + PUT for Moss-Live voice settings.
|
|
3
|
+
*
|
|
4
|
+
* Two-level API key:
|
|
5
|
+
* - Tenant level: stored in tenant config.json (admin sets for all users)
|
|
6
|
+
* - User level: stored in user_preferences (individual user overrides)
|
|
7
|
+
* - Resolution: user key wins if present, else tenant key
|
|
8
|
+
*
|
|
9
|
+
* Other settings (model, persona, instructions) are tenant-level only.
|
|
10
|
+
*/
|
|
11
|
+
import type { Express } from "express";
|
|
12
|
+
/** Resolve the effective API key: user-level wins over tenant-level. */
|
|
13
|
+
export declare function resolveApiKey(ctx: {
|
|
14
|
+
tenant: any;
|
|
15
|
+
userId: string;
|
|
16
|
+
}): string | null;
|
|
17
|
+
export declare function mountMossLiveRoutes(app: Express): void;
|
|
18
|
+
//# sourceMappingURL=routes-moss-live.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes-moss-live.d.ts","sourceRoot":"","sources":["../../src/boot/routes-moss-live.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAC;AAgB1D,wEAAwE;AACxE,wBAAgB,aAAa,CAAC,GAAG,EAAE;IAAE,MAAM,EAAE,GAAG,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,GAAG,IAAI,CAWjF;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CAiGtD"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /api/moss-live/config — GET + PUT for Moss-Live voice settings.
|
|
3
|
+
*
|
|
4
|
+
* Two-level API key:
|
|
5
|
+
* - Tenant level: stored in tenant config.json (admin sets for all users)
|
|
6
|
+
* - User level: stored in user_preferences (individual user overrides)
|
|
7
|
+
* - Resolution: user key wins if present, else tenant key
|
|
8
|
+
*
|
|
9
|
+
* Other settings (model, persona, instructions) are tenant-level only.
|
|
10
|
+
*/
|
|
11
|
+
import { loadTenantConfig, writeTenantConfig, } from "../core/config.js";
|
|
12
|
+
import { createLogger } from "../setup/logger.js";
|
|
13
|
+
const log = createLogger("moss-live-routes");
|
|
14
|
+
const USER_KEY_PREF = "mossLiveApiKey";
|
|
15
|
+
function mask(key) {
|
|
16
|
+
if (key.length < 12)
|
|
17
|
+
return "***";
|
|
18
|
+
return key.slice(0, 8) + "..." + key.slice(-4);
|
|
19
|
+
}
|
|
20
|
+
/** Resolve the effective API key: user-level wins over tenant-level. */
|
|
21
|
+
export function resolveApiKey(ctx) {
|
|
22
|
+
// User-level key
|
|
23
|
+
const row = ctx.tenant.db
|
|
24
|
+
.prepare("SELECT value FROM user_preferences WHERE user_id = ? AND key = ?")
|
|
25
|
+
.get(ctx.userId, USER_KEY_PREF);
|
|
26
|
+
if (row?.value)
|
|
27
|
+
return row.value;
|
|
28
|
+
// Tenant-level key
|
|
29
|
+
return ctx.tenant.config.mossLive?.apiKey || null;
|
|
30
|
+
}
|
|
31
|
+
export function mountMossLiveRoutes(app) {
|
|
32
|
+
// GET — read current config (mask API keys)
|
|
33
|
+
app.get("/api/moss-live/config", (req, res) => {
|
|
34
|
+
const ctx = req.ctx;
|
|
35
|
+
if (!ctx?.tenant)
|
|
36
|
+
return res.status(401).json({ error: "unauthorized" });
|
|
37
|
+
const tenantCfg = ctx.tenant.config.mossLive ?? {};
|
|
38
|
+
// User-level key
|
|
39
|
+
const userKeyRow = ctx.tenant.db
|
|
40
|
+
.prepare("SELECT value FROM user_preferences WHERE user_id = ? AND key = ?")
|
|
41
|
+
.get(ctx.userId, USER_KEY_PREF);
|
|
42
|
+
const userKey = userKeyRow?.value || null;
|
|
43
|
+
// Effective key: user wins
|
|
44
|
+
const effectiveKey = userKey || tenantCfg.apiKey || null;
|
|
45
|
+
res.json({
|
|
46
|
+
...tenantCfg,
|
|
47
|
+
// Masked keys for display
|
|
48
|
+
apiKey: tenantCfg.apiKey ? mask(tenantCfg.apiKey) : null,
|
|
49
|
+
userApiKey: userKey ? mask(userKey) : null,
|
|
50
|
+
// Whether voice mode is available (effective key exists)
|
|
51
|
+
available: !!effectiveKey,
|
|
52
|
+
// Which level is active
|
|
53
|
+
keySource: userKey ? "user" : tenantCfg.apiKey ? "tenant" : null,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
// PUT — update tenant-level config (admin)
|
|
57
|
+
app.put("/api/moss-live/config", (req, res) => {
|
|
58
|
+
const ctx = req.ctx;
|
|
59
|
+
if (!ctx?.tenant)
|
|
60
|
+
return res.status(401).json({ error: "unauthorized" });
|
|
61
|
+
const tenant = ctx.tenant;
|
|
62
|
+
const body = req.body;
|
|
63
|
+
if (!body || typeof body !== "object") {
|
|
64
|
+
return res.status(400).json({ error: "invalid body" });
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const existing = loadTenantConfig(tenant.tenantId, tenant.home);
|
|
68
|
+
const mossLive = { ...(existing.mossLive ?? {}) };
|
|
69
|
+
if (body.apiKey !== undefined) {
|
|
70
|
+
const key = body.apiKey;
|
|
71
|
+
if (key && !key.includes("...")) {
|
|
72
|
+
mossLive.apiKey = key;
|
|
73
|
+
}
|
|
74
|
+
else if (key === "") {
|
|
75
|
+
// Explicitly clear tenant key
|
|
76
|
+
delete mossLive.apiKey;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (body.model !== undefined)
|
|
80
|
+
mossLive.model = body.model || undefined;
|
|
81
|
+
if (body.persona !== undefined)
|
|
82
|
+
mossLive.persona = body.persona || undefined;
|
|
83
|
+
if (body.instructions !== undefined)
|
|
84
|
+
mossLive.instructions = body.instructions || undefined;
|
|
85
|
+
const updated = { ...existing, mossLive };
|
|
86
|
+
writeTenantConfig(tenant.tenantId, updated, tenant.home);
|
|
87
|
+
log.info(`[moss-live] tenant config updated for tenant=${tenant.tenantId}`);
|
|
88
|
+
res.json({ ok: true });
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
log.error(`[moss-live] tenant config update failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
92
|
+
res.status(500).json({ error: "failed to save config" });
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
// PUT — update user-level API key
|
|
96
|
+
app.put("/api/moss-live/user-key", (req, res) => {
|
|
97
|
+
const ctx = req.ctx;
|
|
98
|
+
if (!ctx?.tenant)
|
|
99
|
+
return res.status(401).json({ error: "unauthorized" });
|
|
100
|
+
const body = req.body;
|
|
101
|
+
const key = body?.apiKey ?? "";
|
|
102
|
+
if (key && key.includes("...")) {
|
|
103
|
+
// Masked key — don't overwrite
|
|
104
|
+
return res.json({ ok: true });
|
|
105
|
+
}
|
|
106
|
+
if (key) {
|
|
107
|
+
ctx.tenant.db
|
|
108
|
+
.prepare("INSERT OR REPLACE INTO user_preferences (user_id, key, value) VALUES (?, ?, ?)")
|
|
109
|
+
.run(ctx.userId, USER_KEY_PREF, key);
|
|
110
|
+
log.info(`[moss-live] user key set for user=${ctx.userId}`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// Clear user key
|
|
114
|
+
ctx.tenant.db
|
|
115
|
+
.prepare("DELETE FROM user_preferences WHERE user_id = ? AND key = ?")
|
|
116
|
+
.run(ctx.userId, USER_KEY_PREF);
|
|
117
|
+
log.info(`[moss-live] user key cleared for user=${ctx.userId}`);
|
|
118
|
+
}
|
|
119
|
+
res.json({ ok: true });
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=routes-moss-live.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes-moss-live.js","sourceRoot":"","sources":["../../src/boot/routes-moss-live.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,GAAG,GAAG,YAAY,CAAC,kBAAkB,CAAC,CAAC;AAE7C,MAAM,aAAa,GAAG,gBAAgB,CAAC;AAEvC,SAAS,IAAI,CAAC,GAAW;IACvB,IAAI,GAAG,CAAC,MAAM,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC;IAClC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC;AAED,wEAAwE;AACxE,MAAM,UAAU,aAAa,CAAC,GAAoC;IAChE,iBAAiB;IACjB,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE;SACtB,OAAO,CACN,kEAAkE,CACnE;SACA,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,CAAyC,CAAC;IAC1E,IAAI,GAAG,EAAE,KAAK;QAAE,OAAO,GAAG,CAAC,KAAK,CAAC;IAEjC,mBAAmB;IACnB,OAAO,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,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MAAM,IAAI,GAAG,GAAG,CAAC,IAA2B,CAAC;QAC7C,MAAM,GAAG,GAAG,IAAI,EAAE,MAAM,IAAI,EAAE,CAAC;QAE/B,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/B,+BAA+B;YAC/B,OAAO,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;QAChC,CAAC;QAED,IAAI,GAAG,EAAE,CAAC;YACR,GAAG,CAAC,MAAM,CAAC,EAAE;iBACV,OAAO,CAAC,gFAAgF,CAAC;iBACzF,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;YACvC,GAAG,CAAC,IAAI,CAAC,qCAAqC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QAC9D,CAAC;aAAM,CAAC;YACN,iBAAiB;YACjB,GAAG,CAAC,MAAM,CAAC,EAAE;iBACV,OAAO,CAAC,4DAA4D,CAAC;iBACrE,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;YAClC,GAAG,CAAC,IAAI,CAAC,yCAAyC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QAClE,CAAC;QAED,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/chat/handler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/chat/handler.ts"],"names":[],"mappings":"AA2DA,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,qBAAqB,EAGrB,KAAK,mBAAmB,EACxB,KAAK,wBAAwB,EAC9B,MAAM,uBAAuB,CAAC;AAI/B,OAAO,EACL,mBAAmB,EACnB,cA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EAAE,cAAc,CAAC;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;4BAGwB;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;;;;OAQG;IACH,OAAO,CAAC,EAAE,OAAO,eAAe,EAAE,WAAW,CAAC;IAC9C;;;;;;OAMG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;kCAE8B;IAC9B,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,wBAAsB,SAAS,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAotDlE;AAwZD,wBAAgB,uBAAuB,CACrC,oBAAoB,EAAE,WAAW,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,EAC3D,2BAA2B,CAAC,EAAE,MAAM,GACnC,MAAM,CAuCR;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,oBAAoB,EAAE,MAAM,GAAG,MAAM,EAAE,CAgB3E;AA0ED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,aAAa,EAClB,SAAS,EAAE,MAAM,GAChB,MAAM,GAAG,IAAI,CA0Ef;AAolBD,wBAAgB,cAAc,IAAI,WAAW,EAAE,CAkC9C;AAGD,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
// MAX_TURNS exists because a model that decides to call its tools
|
|
18
18
|
// in a loop with no productive progress would otherwise burn budget
|
|
19
19
|
// forever. shouldStopAfterTurn enforces it.
|
|
20
|
+
import { createMossLiveTunnel } from "./moss-live-tunnel.js";
|
|
21
|
+
import { resolveApiKey as resolveMossLiveApiKey } from "../boot/routes-moss-live.js";
|
|
22
|
+
import { loadWorkerAgents } from "../core/worker-agents-fs.js";
|
|
20
23
|
import { AgentHarness, } from "./pi-compat/index.js";
|
|
21
24
|
import { BACKGROUND_CONTEXT, StorageBackedSession, withAbortSignal, } from "./pi-compat/index.js";
|
|
22
25
|
import fs from "node:fs";
|
|
@@ -311,7 +314,11 @@ export function attachChatHandler(opts) {
|
|
|
311
314
|
});
|
|
312
315
|
return p;
|
|
313
316
|
}
|
|
317
|
+
// Moss-Live realtime voice tunnel (per-connection state)
|
|
318
|
+
let mossLiveTunnel = null;
|
|
319
|
+
let mossLiveAudioLogged = false;
|
|
314
320
|
socket.on("message", (raw) => {
|
|
321
|
+
// Binary message? Could be moss-live audio (base64 handled in text)
|
|
315
322
|
let parsed;
|
|
316
323
|
try {
|
|
317
324
|
parsed = JSON.parse(raw.toString());
|
|
@@ -765,6 +772,142 @@ export function attachChatHandler(opts) {
|
|
|
765
772
|
});
|
|
766
773
|
return;
|
|
767
774
|
}
|
|
775
|
+
// ── Moss-Live realtime voice tunnel ──
|
|
776
|
+
case "moss_live_start": {
|
|
777
|
+
if (mossLiveTunnel) {
|
|
778
|
+
mossLiveTunnel.stop();
|
|
779
|
+
mossLiveTunnel = null;
|
|
780
|
+
}
|
|
781
|
+
const mlConfig = ctx.config.mossLive ?? {};
|
|
782
|
+
const effectiveApiKey = resolveMossLiveApiKey({ tenant: ctx, userId });
|
|
783
|
+
if (!effectiveApiKey) {
|
|
784
|
+
sendDirect({
|
|
785
|
+
type: "moss_live_event",
|
|
786
|
+
event: { action: "error", data: { code: "no_api_key", message: "Moss-Live API key not configured. Go to Settings → 实时语音 to add your key." } },
|
|
787
|
+
});
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
// Override apiKey with the resolved effective key
|
|
791
|
+
const effectiveConfig = { ...mlConfig, apiKey: effectiveApiKey };
|
|
792
|
+
// Get recent chat history for voice session context
|
|
793
|
+
const recentMsgs = listMessagesForUserPage(ctx, userId, { limit: 20 })
|
|
794
|
+
.messages
|
|
795
|
+
.filter((m) => m.role === "user" || m.role === "assistant")
|
|
796
|
+
.map((m) => ({ role: m.role, content: m.content }));
|
|
797
|
+
// Build capability summary from ALL sources:
|
|
798
|
+
// 1. Plugin-contributed tools (from manifest contributes.tools)
|
|
799
|
+
// 2. MCP toolsets (from toolsetsForTenant)
|
|
800
|
+
// 3. Host built-in capabilities (always available)
|
|
801
|
+
let capabilitySummary = "";
|
|
802
|
+
if (pluginRegistry) {
|
|
803
|
+
const lines = [];
|
|
804
|
+
// Plugin tools (files, messenger, datasource, web-search, etc.)
|
|
805
|
+
const pluginToolMap = new Map();
|
|
806
|
+
for (const entry of pluginRegistry.listForTenant(ctx.tenantId)) {
|
|
807
|
+
if (entry.state !== "active")
|
|
808
|
+
continue;
|
|
809
|
+
const tools = entry.manifest.contributes?.tools ?? [];
|
|
810
|
+
if (tools.length === 0)
|
|
811
|
+
continue;
|
|
812
|
+
const toolExports = entry.exports?.tools ?? {};
|
|
813
|
+
const names = [];
|
|
814
|
+
for (const t of tools) {
|
|
815
|
+
const tool = toolExports[t.module];
|
|
816
|
+
if (tool?.schema?.name)
|
|
817
|
+
names.push(tool.schema.name);
|
|
818
|
+
}
|
|
819
|
+
if (names.length > 0) {
|
|
820
|
+
pluginToolMap.set(entry.manifest.id, names);
|
|
821
|
+
lines.push(`- ${entry.manifest.id}: ${names.join(", ")}`);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
// MCP toolsets (Local Bridge, user-configured MCP servers)
|
|
825
|
+
const toolsets = pluginRegistry.toolsetsForTenant(ctx.tenantId);
|
|
826
|
+
for (const ts of toolsets) {
|
|
827
|
+
if (!ts.enabled || ts.toolCount === 0)
|
|
828
|
+
continue;
|
|
829
|
+
const toolNames = ts.snapshot?.tools?.map((t) => t.name).join(", ") || `(${ts.toolCount} tools)`;
|
|
830
|
+
lines.push(`- ${ts.displayName}: ${toolNames}`);
|
|
831
|
+
}
|
|
832
|
+
// Host built-in capabilities (always present)
|
|
833
|
+
lines.push(`- 内置: compact_context, ask_user, switch_panel, generate_image(可选)`);
|
|
834
|
+
// Worker agents (background task executors)
|
|
835
|
+
try {
|
|
836
|
+
const workers = loadWorkerAgents(ctx.tenantId, ctx.home);
|
|
837
|
+
const workerLines = [];
|
|
838
|
+
for (const w of workers) {
|
|
839
|
+
if (w.spec.enabled === false)
|
|
840
|
+
continue;
|
|
841
|
+
const name = w.spec.displayName || w.slug;
|
|
842
|
+
const desc = w.spec.description ? `: ${w.spec.description}` : "";
|
|
843
|
+
workerLines.push(`- ${name}${desc}`);
|
|
844
|
+
}
|
|
845
|
+
if (workerLines.length > 0) {
|
|
846
|
+
capabilitySummary = `\n\n# 天枢Agent当前可用工具\n${lines.join("\n")}\n\n# 天枢后台Worker(可派发任务的专业Agent)\n${workerLines.join("\n")}`;
|
|
847
|
+
}
|
|
848
|
+
else {
|
|
849
|
+
capabilitySummary = `\n\n# 天枢Agent当前可用工具\n${lines.join("\n")}`;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
catch {
|
|
853
|
+
capabilitySummary = `\n\n# 天枢Agent当前可用工具\n${lines.join("\n")}`;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
mossLiveTunnel = createMossLiveTunnel(effectiveConfig, (msg) => {
|
|
857
|
+
if (socket.readyState === 1)
|
|
858
|
+
socket.send(JSON.stringify(msg));
|
|
859
|
+
}, (buf) => {
|
|
860
|
+
if (socket.readyState === 1)
|
|
861
|
+
socket.send(buf);
|
|
862
|
+
},
|
|
863
|
+
// Delegation handler: run tianshu agent, report result back to Moss-Live
|
|
864
|
+
async (task, callbacks) => {
|
|
865
|
+
log.info(`[moss-live] delegation start: objective=${task.objective?.slice(0, 80)}`);
|
|
866
|
+
callbacks.reportProgress("正在处理...");
|
|
867
|
+
const aborter = new AbortController();
|
|
868
|
+
let agentReply = "";
|
|
869
|
+
const delegationSend = (msg) => {
|
|
870
|
+
if (msg.type === "stream_delta" && typeof msg.delta === "string") {
|
|
871
|
+
agentReply += msg.delta;
|
|
872
|
+
}
|
|
873
|
+
if (socket.readyState === 1)
|
|
874
|
+
socket.send(JSON.stringify(msg));
|
|
875
|
+
};
|
|
876
|
+
log.info(`[moss-live] calling runPrompt for delegation`);
|
|
877
|
+
await runPrompt({
|
|
878
|
+
ctx,
|
|
879
|
+
userId,
|
|
880
|
+
send: delegationSend,
|
|
881
|
+
content: `[语音助手委派任务] ${task.objective}`,
|
|
882
|
+
signal: aborter.signal,
|
|
883
|
+
pluginRegistry: pluginRegistry ?? undefined,
|
|
884
|
+
homeDir: ctx.workspaceDir,
|
|
885
|
+
});
|
|
886
|
+
const result = agentReply.trim() || "任务已完成";
|
|
887
|
+
log.info(`[moss-live] runPrompt done, reply length=${result.length}`);
|
|
888
|
+
callbacks.reportResult(result);
|
|
889
|
+
}, recentMsgs, capabilitySummary);
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
case "moss_live_stop": {
|
|
893
|
+
mossLiveTunnel?.stop();
|
|
894
|
+
mossLiveTunnel = null;
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
case "moss_live_event": {
|
|
898
|
+
const ev = parsed.event;
|
|
899
|
+
mossLiveTunnel?.handleEvent(ev);
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
case "moss_live_audio": {
|
|
903
|
+
const audio = parsed.audio;
|
|
904
|
+
if (!mossLiveAudioLogged) {
|
|
905
|
+
log.info(`[moss-live] first client audio: base64 length=${audio?.length}, tunnel=${!!mossLiveTunnel}`);
|
|
906
|
+
mossLiveAudioLogged = true;
|
|
907
|
+
}
|
|
908
|
+
mossLiveTunnel?.handleAudio(audio);
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
768
911
|
default: {
|
|
769
912
|
// Not a core message type. Offer it to plugin WS handlers
|
|
770
913
|
// (declared via manifest `contributes.wsMessages[]`). Used by
|
|
@@ -794,6 +937,9 @@ export function attachChatHandler(opts) {
|
|
|
794
937
|
// Only null out the aborter so the NEXT prompt from a new
|
|
795
938
|
// socket gets a fresh controller.
|
|
796
939
|
aborter = null;
|
|
940
|
+
// Clean up Moss-Live tunnel if active
|
|
941
|
+
mossLiveTunnel?.stop();
|
|
942
|
+
mossLiveTunnel = null;
|
|
797
943
|
unregisterUserChannel();
|
|
798
944
|
// Notify plugins that this socket is gone. The reverse-mcp plugin
|
|
799
945
|
// needs this to remove the bridge's registered tools immediately
|