@@ -3,12 +3,14 @@ import {
33 MessageRole ,
44 SessionDeliveryMode ,
55} from "scorpio.ai" ;
6+ import { WEB_CHANNEL_TYPE } from "sbot.commons" ;
67import { config } from "./Config" ;
78import { LoggerService } from "./LoggerService" ;
89import { channelDataService } from "../Session/ChannelDataService" ;
910import { channelManager } from "../Channel/ChannelManager" ;
1011import { sessionManager } from "../Session/SessionManager" ;
1112import { SaverPool } from "../Agent/SaverPool" ;
13+ import { webService } from "../Channel/web/WebService" ;
1214
1315const logger = LoggerService . getLogger ( "triggerSession.ts" ) ;
1416
@@ -63,28 +65,36 @@ export async function triggerSession(opts: TriggerSessionOptions): Promise<Trigg
6365 return { ok : true , channelType, sessionId } ;
6466 }
6567
66- // Notify / NotifyAndRecord 都先投递文本到 channel
67- const r = await channelManager . sendTextToSession ( dbSessionId , message ) ;
68- if ( ! r . ok ) return { ok : false } ;
68+ // web channel + NotifyAndRecord:sendTextToSession 会以 Command kind 写一条 saver
69+ // 并广播;下面的 saver 写入又会作为 Normal kind 写第二条,前端会显示两条重复消息。
70+ // 跳过文本投递、改为下方手动以 Normal kind 广播一次,让前端只展示一条。
71+ const isWebNotifyAndRecord = channelType === WEB_CHANNEL_TYPE && mode === SessionDeliveryMode . NotifyAndRecord ;
72+
73+ if ( ! isWebNotifyAndRecord ) {
74+ const r = await channelManager . sendTextToSession ( dbSessionId , message ) ;
75+ if ( ! r . ok ) return { ok : false } ;
76+ }
6977
7078 // NotifyAndRecord 额外把这条投递写入 saver,作为 AI 角色的 Normal 消息,
7179 // 这样后续主对话 agent 能在上下文中看到"刚才系统已经提醒过用户",
7280 // 避免用户突然回复"已喝 / 已交"时 AI 上下文断裂。
7381 if ( mode === SessionDeliveryMode . NotifyAndRecord ) {
82+ const chatMessage = { role : MessageRole . AI , content : message } ;
7483 try {
7584 const handle = await SaverPool . getInstance ( ) . acquireByDBSessionId ( dbSessionId ) ;
7685 try {
77- await handle . saver . pushMessage (
78- { role : MessageRole . AI , content : message } ,
79- { kind : MessageKind . Normal } ,
80- ) ;
86+ await handle . saver . pushMessage ( chatMessage , { kind : MessageKind . Normal } ) ;
8187 } finally {
8288 await handle . release ( ) ;
8389 }
8490 } catch ( e : any ) {
8591 // saver 写入失败不影响投递成功——只是丢了上下文记录。记录 warn。
8692 logger . warn ( `${ tag } saver record failed: ${ e ?. message ?? String ( e ) } ` ) ;
8793 }
94+
95+ if ( isWebNotifyAndRecord ) {
96+ webService . broadcastMessage ( sessionRow . profileId , chatMessage , MessageKind . Normal ) ;
97+ }
8898 }
8999 return { ok : true , channelType, sessionId } ;
90100}
0 commit comments