Skip to content

Commit 462608d

Browse files
fix(plugin): resolve UUID targets to active session key for media delivery
Embedded agent's message tool sends UUIDs instead of BotsChat session keys, causing "Unknown target" errors. Track last active session key per account and fall back to it when the target doesn't match session key format.
1 parent 3969e8a commit 462608d

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

packages/plugin/src/channel.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ function readAgentModel(_agentId: string): string | undefined {
100100
// Connection registry — maps accountId → live WSS client
101101
// ---------------------------------------------------------------------------
102102
const cloudClients = new Map<string, BotsChatCloudClient>();
103+
const lastSessionKeys = new Map<string, string>();
104+
105+
function isValidSessionKey(target: string): boolean {
106+
const t = target.trim();
107+
return t.startsWith("agent:") || t.startsWith("botschat:") || /^(ses_|u_)/.test(t);
108+
}
109+
110+
function resolveTarget(target: string, accountId: string): string {
111+
if (isValidSessionKey(target)) return target;
112+
const fallback = lastSessionKeys.get(accountId);
113+
if (fallback) {
114+
console.log(`[botschat] resolveTarget: "${target.slice(0, 20)}…" → fallback to lastSessionKey "${fallback}"`);
115+
return fallback;
116+
}
117+
return target;
118+
}
103119

104120
function findClientForSession(_sessionKey: string): BotsChatCloudClient | null {
105121
for (const client of cloudClients.values()) {
@@ -220,6 +236,7 @@ export const botschatPlugin = {
220236
const t = raw.trim();
221237
if (t.startsWith("agent:") || t.startsWith("botschat:")) return true;
222238
if (/^(ses_|u_)/.test(t)) return true;
239+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(t)) return true;
223240
return false;
224241
},
225242
},
@@ -257,15 +274,17 @@ export const botschatPlugin = {
257274
threadId?: string | number | null;
258275
accountId?: string | null;
259276
}) => {
260-
const client = getCloudClient(ctx.accountId ?? "default");
277+
const accountId = ctx.accountId ?? "default";
278+
const client = getCloudClient(accountId);
261279
if (!client?.connected) {
262280
return { ok: false, error: new Error("Not connected to BotsChat cloud") };
263281
}
282+
const to = resolveTarget(ctx.to, accountId);
264283
const messageId = crypto.randomUUID();
265284
let text = ctx.text;
266285
let encrypted = false;
267286

268-
console.log(`[botschat][sendText] e2eKey=${!!client.e2eKey}, textLen=${text.length}`);
287+
console.log(`[botschat][sendText] e2eKey=${!!client.e2eKey}, textLen=${text.length}, to=${to}`);
269288

270289
if (client.e2eKey) {
271290
try {
@@ -285,7 +304,7 @@ export const botschatPlugin = {
285304
: undefined;
286305
client.send({
287306
type: "agent.text",
288-
sessionKey: ctx.to,
307+
sessionKey: to,
289308
text,
290309
replyToId: ctx.replyToId ?? undefined,
291310
threadId: ctx.threadId?.toString(),
@@ -308,6 +327,7 @@ export const botschatPlugin = {
308327
if (!client?.connected) {
309328
return { ok: false, error: new Error("Not connected to BotsChat cloud") };
310329
}
330+
const to = resolveTarget(ctx.to, accountId);
311331
const messageId = crypto.randomUUID();
312332
let text = ctx.text;
313333
let encrypted = false;
@@ -375,7 +395,7 @@ export const botschatPlugin = {
375395
if (finalMediaUrl) {
376396
client.send({
377397
type: "agent.media",
378-
sessionKey: ctx.to,
398+
sessionKey: to,
379399
mediaUrl: finalMediaUrl,
380400
caption: text || undefined,
381401
messageId,
@@ -386,7 +406,7 @@ export const botschatPlugin = {
386406
} else {
387407
client.send({
388408
type: "agent.text",
389-
sessionKey: ctx.to,
409+
sessionKey: to,
390410
text: text,
391411
messageId,
392412
encrypted,
@@ -618,6 +638,8 @@ async function handleCloudMessage(
618638
): Promise<void> {
619639
switch (msg.type) {
620640
case "user.message": {
641+
lastSessionKeys.set(ctx.accountId, msg.sessionKey);
642+
621643
let text = msg.text;
622644

623645
// Decrypt if needed

0 commit comments

Comments
 (0)