@yeaft/webchat-agent 0.1.83 → 0.1.85
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/conversation.js +47 -0
- package/package.json +1 -1
package/conversation.js
CHANGED
|
@@ -607,6 +607,53 @@ export async function handleUserInput(msg) {
|
|
|
607
607
|
console.log(`[RolePlay] Human responded, resuming from ${fromRole}'s request`);
|
|
608
608
|
}
|
|
609
609
|
|
|
610
|
+
// ★ RolePlay: handle @mention targetRole routing
|
|
611
|
+
const targetRole = msg.targetRole;
|
|
612
|
+
if (targetRole && rpSession && rpSession._routeInitialized) {
|
|
613
|
+
const roleNames = new Set(rpSession.roles.map(r => r.name));
|
|
614
|
+
if (roleNames.has(targetRole)) {
|
|
615
|
+
const targetRoleConfig = rpSession.roles.find(r => r.name === targetRole);
|
|
616
|
+
const targetLabel = targetRoleConfig
|
|
617
|
+
? (targetRoleConfig.icon ? `${targetRoleConfig.icon} ${targetRoleConfig.displayName}` : targetRoleConfig.displayName)
|
|
618
|
+
: targetRole;
|
|
619
|
+
const targetClaudeMd = targetRoleConfig?.claudeMd || '';
|
|
620
|
+
|
|
621
|
+
// Prepend ROLE signal so Claude responds as the target role
|
|
622
|
+
let rolePrefix = `---ROLE: ${targetRole}---\n\n`;
|
|
623
|
+
rolePrefix += `用户指定由 ${targetLabel} 来回复。\n`;
|
|
624
|
+
if (targetClaudeMd) {
|
|
625
|
+
rolePrefix += `<role-context>\n${targetClaudeMd}\n</role-context>\n\n`;
|
|
626
|
+
}
|
|
627
|
+
rolePrefix += `请以 ${targetLabel} 的身份回复以下消息:\n\n`;
|
|
628
|
+
effectivePrompt = rolePrefix + effectivePrompt;
|
|
629
|
+
|
|
630
|
+
// Update rpSession state
|
|
631
|
+
const prevRole = rpSession.currentRole;
|
|
632
|
+
rpSession.currentRole = targetRole;
|
|
633
|
+
if (rpSession.roleStates[targetRole]) {
|
|
634
|
+
rpSession.roleStates[targetRole].status = 'active';
|
|
635
|
+
}
|
|
636
|
+
if (prevRole && prevRole !== targetRole && rpSession.roleStates[prevRole]) {
|
|
637
|
+
rpSession.roleStates[prevRole].status = 'idle';
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Send roleplay_status update to frontend
|
|
641
|
+
ctx.sendToServer({
|
|
642
|
+
type: 'roleplay_status',
|
|
643
|
+
conversationId,
|
|
644
|
+
currentRole: rpSession.currentRole,
|
|
645
|
+
round: rpSession.round,
|
|
646
|
+
features: rpSession.features ? Array.from(rpSession.features.values()) : [],
|
|
647
|
+
roleStates: rpSession.roleStates || {},
|
|
648
|
+
waitingHuman: false
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
console.log(`[RolePlay] @mention routing: ${prevRole || 'none'} -> ${targetRole}`);
|
|
652
|
+
} else {
|
|
653
|
+
console.warn(`[RolePlay] @mention target role not found: ${targetRole}`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
610
657
|
const userMessage = {
|
|
611
658
|
type: 'user',
|
|
612
659
|
message: { role: 'user', content: effectivePrompt }
|