218 lines
7.6 KiB
JavaScript
218 lines
7.6 KiB
JavaScript
import "./pi-embedded-helpers-s_r2sMSQ.js";
|
|
import { J as requestHeartbeatNow, _ as loadSessionEntry, pr as enqueueSystemEvent } from "./reply-5K4M-ATj.js";
|
|
import "./paths-BDd7_JUB.js";
|
|
import { P as normalizeMainKey } from "./agent-scope-CrgUOY3f.js";
|
|
import { c as defaultRuntime } from "./subsystem-46MXi6Ip.js";
|
|
import "./utils-Dg0Xbl6w.js";
|
|
import "./exec-CTo4hK94.js";
|
|
import "./model-selection-Cp1maz7B.js";
|
|
import "./github-copilot-token-VZsS4013.js";
|
|
import "./boolean-CE7i9tBR.js";
|
|
import "./env-DOcCob95.js";
|
|
import { r as loadConfig } from "./config-BAN8QfW5.js";
|
|
import "./manifest-registry-BFpLJJDB.js";
|
|
import { r as normalizeChannelId } from "./plugins-D1CxUobm.js";
|
|
import { g as updateSessionStore } from "./sandbox-CRRd7UQ-.js";
|
|
import "./image-Bx6WKexe.js";
|
|
import "./pi-model-discovery-DjGamP_B.js";
|
|
import "./chrome-D2LUApAY.js";
|
|
import "./skills-C4b1FA1e.js";
|
|
import "./routes-CB4ih7ZH.js";
|
|
import "./server-context-BjC2Yruc.js";
|
|
import "./message-channel-BD3kuDks.js";
|
|
import "./logging-BdnOSVPD.js";
|
|
import "./accounts-BlHoTziG.js";
|
|
import "./paths-50eo6DV6.js";
|
|
import "./redact-BKh-zp-c.js";
|
|
import "./tool-display-rIUh61kT.js";
|
|
import "./deliver-Do5KTPQ4.js";
|
|
import "./dispatcher-DLhjSne5.js";
|
|
import "./manager-BArueSTR.js";
|
|
import "./sqlite-Dz6S6ijV.js";
|
|
import "./channel-summary-DvcMwADq.js";
|
|
import "./client-i9GLCujO.js";
|
|
import "./call-BxBOsbXG.js";
|
|
import "./login-qr-f0O41dc6.js";
|
|
import "./pairing-store-BnMngoWQ.js";
|
|
import "./links-C9fyAH-V.js";
|
|
import "./progress-uNDQDtGB.js";
|
|
import "./pi-tools.policy-B0NP2v9o.js";
|
|
import "./prompt-style-gfROyHgB.js";
|
|
import "./pairing-labels-DK2aLSd2.js";
|
|
import "./control-service-yGq0G92l.js";
|
|
import "./restart-sentinel-CdcBcziq.js";
|
|
import "./channel-selection-Cmg7RpCn.js";
|
|
import "./deps-DhwPoDgU.js";
|
|
import { t as agentCommand } from "./agent-vIBVyXVW.js";
|
|
import { t as formatForLog } from "./ws-log-DQ6nkLys.js";
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
//#region src/gateway/server-node-events.ts
|
|
const handleNodeEvent = async (ctx, nodeId, evt) => {
|
|
switch (evt.event) {
|
|
case "voice.transcript": {
|
|
if (!evt.payloadJSON) return;
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(evt.payloadJSON);
|
|
} catch {
|
|
return;
|
|
}
|
|
const obj = typeof payload === "object" && payload !== null ? payload : {};
|
|
const text = typeof obj.text === "string" ? obj.text.trim() : "";
|
|
if (!text) return;
|
|
if (text.length > 2e4) return;
|
|
const sessionKeyRaw = typeof obj.sessionKey === "string" ? obj.sessionKey.trim() : "";
|
|
const rawMainKey = normalizeMainKey(loadConfig().session?.mainKey);
|
|
const sessionKey = sessionKeyRaw.length > 0 ? sessionKeyRaw : rawMainKey;
|
|
const { storePath, entry, canonicalKey } = loadSessionEntry(sessionKey);
|
|
const now = Date.now();
|
|
const sessionId = entry?.sessionId ?? randomUUID();
|
|
if (storePath) await updateSessionStore(storePath, (store) => {
|
|
store[canonicalKey] = {
|
|
sessionId,
|
|
updatedAt: now,
|
|
thinkingLevel: entry?.thinkingLevel,
|
|
verboseLevel: entry?.verboseLevel,
|
|
reasoningLevel: entry?.reasoningLevel,
|
|
systemSent: entry?.systemSent,
|
|
sendPolicy: entry?.sendPolicy,
|
|
lastChannel: entry?.lastChannel,
|
|
lastTo: entry?.lastTo
|
|
};
|
|
});
|
|
ctx.addChatRun(sessionId, {
|
|
sessionKey,
|
|
clientRunId: `voice-${randomUUID()}`
|
|
});
|
|
agentCommand({
|
|
message: text,
|
|
sessionId,
|
|
sessionKey,
|
|
thinking: "low",
|
|
deliver: false,
|
|
messageChannel: "node"
|
|
}, defaultRuntime, ctx.deps).catch((err) => {
|
|
ctx.logGateway.warn(`agent failed node=${nodeId}: ${formatForLog(err)}`);
|
|
});
|
|
return;
|
|
}
|
|
case "agent.request": {
|
|
if (!evt.payloadJSON) return;
|
|
let link = null;
|
|
try {
|
|
link = JSON.parse(evt.payloadJSON);
|
|
} catch {
|
|
return;
|
|
}
|
|
const message = (link?.message ?? "").trim();
|
|
if (!message) return;
|
|
if (message.length > 2e4) return;
|
|
const channel = normalizeChannelId(typeof link?.channel === "string" ? link.channel.trim() : "") ?? void 0;
|
|
const to = typeof link?.to === "string" && link.to.trim() ? link.to.trim() : void 0;
|
|
const deliver = Boolean(link?.deliver) && Boolean(channel);
|
|
const sessionKeyRaw = (link?.sessionKey ?? "").trim();
|
|
const sessionKey = sessionKeyRaw.length > 0 ? sessionKeyRaw : `node-${nodeId}`;
|
|
const { storePath, entry, canonicalKey } = loadSessionEntry(sessionKey);
|
|
const now = Date.now();
|
|
const sessionId = entry?.sessionId ?? randomUUID();
|
|
if (storePath) await updateSessionStore(storePath, (store) => {
|
|
store[canonicalKey] = {
|
|
sessionId,
|
|
updatedAt: now,
|
|
thinkingLevel: entry?.thinkingLevel,
|
|
verboseLevel: entry?.verboseLevel,
|
|
reasoningLevel: entry?.reasoningLevel,
|
|
systemSent: entry?.systemSent,
|
|
sendPolicy: entry?.sendPolicy,
|
|
lastChannel: entry?.lastChannel,
|
|
lastTo: entry?.lastTo
|
|
};
|
|
});
|
|
agentCommand({
|
|
message,
|
|
sessionId,
|
|
sessionKey,
|
|
thinking: link?.thinking ?? void 0,
|
|
deliver,
|
|
to,
|
|
channel,
|
|
timeout: typeof link?.timeoutSeconds === "number" ? link.timeoutSeconds.toString() : void 0,
|
|
messageChannel: "node"
|
|
}, defaultRuntime, ctx.deps).catch((err) => {
|
|
ctx.logGateway.warn(`agent failed node=${nodeId}: ${formatForLog(err)}`);
|
|
});
|
|
return;
|
|
}
|
|
case "chat.subscribe": {
|
|
if (!evt.payloadJSON) return;
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(evt.payloadJSON);
|
|
} catch {
|
|
return;
|
|
}
|
|
const obj = typeof payload === "object" && payload !== null ? payload : {};
|
|
const sessionKey = typeof obj.sessionKey === "string" ? obj.sessionKey.trim() : "";
|
|
if (!sessionKey) return;
|
|
ctx.nodeSubscribe(nodeId, sessionKey);
|
|
return;
|
|
}
|
|
case "chat.unsubscribe": {
|
|
if (!evt.payloadJSON) return;
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(evt.payloadJSON);
|
|
} catch {
|
|
return;
|
|
}
|
|
const obj = typeof payload === "object" && payload !== null ? payload : {};
|
|
const sessionKey = typeof obj.sessionKey === "string" ? obj.sessionKey.trim() : "";
|
|
if (!sessionKey) return;
|
|
ctx.nodeUnsubscribe(nodeId, sessionKey);
|
|
return;
|
|
}
|
|
case "exec.started":
|
|
case "exec.finished":
|
|
case "exec.denied": {
|
|
if (!evt.payloadJSON) return;
|
|
let payload;
|
|
try {
|
|
payload = JSON.parse(evt.payloadJSON);
|
|
} catch {
|
|
return;
|
|
}
|
|
const obj = typeof payload === "object" && payload !== null ? payload : {};
|
|
const sessionKey = typeof obj.sessionKey === "string" ? obj.sessionKey.trim() : `node-${nodeId}`;
|
|
if (!sessionKey) return;
|
|
const runId = typeof obj.runId === "string" ? obj.runId.trim() : "";
|
|
const command = typeof obj.command === "string" ? obj.command.trim() : "";
|
|
const exitCode = typeof obj.exitCode === "number" && Number.isFinite(obj.exitCode) ? obj.exitCode : void 0;
|
|
const timedOut = obj.timedOut === true;
|
|
const output = typeof obj.output === "string" ? obj.output.trim() : "";
|
|
const reason = typeof obj.reason === "string" ? obj.reason.trim() : "";
|
|
let text = "";
|
|
if (evt.event === "exec.started") {
|
|
text = `Exec started (node=${nodeId}${runId ? ` id=${runId}` : ""})`;
|
|
if (command) text += `: ${command}`;
|
|
} else if (evt.event === "exec.finished") {
|
|
const exitLabel = timedOut ? "timeout" : `code ${exitCode ?? "?"}`;
|
|
text = `Exec finished (node=${nodeId}${runId ? ` id=${runId}` : ""}, ${exitLabel})`;
|
|
if (output) text += `\n${output}`;
|
|
} else {
|
|
text = `Exec denied (node=${nodeId}${runId ? ` id=${runId}` : ""}${reason ? `, ${reason}` : ""})`;
|
|
if (command) text += `: ${command}`;
|
|
}
|
|
enqueueSystemEvent(text, {
|
|
sessionKey,
|
|
contextKey: runId ? `exec:${runId}` : "exec"
|
|
});
|
|
requestHeartbeatNow({ reason: "exec-event" });
|
|
return;
|
|
}
|
|
default: return;
|
|
}
|
|
};
|
|
|
|
//#endregion
|
|
export { handleNodeEvent }; |