@@ -303,11 +303,6 @@ class IdeaBridge {
303303 case "listCommands/response" :
304304 this . resviceCommandList ( res ) ;
305305 break ;
306- // 这里暂时不用,因为获取到的只有 key,信息不全
307- // 所以用 resviceSettings 来获取
308- // case "getKey/response":
309- // this.resviceAccessKey(res.payload.key);
310- // break;
311306 case "addContext/notify" :
312307 this . resviesContext ( res ) ;
313308 break ;
@@ -331,15 +326,18 @@ class IdeaBridge {
331326 }
332327
333328 resviceSendUserMessage ( res ) {
334- this . handle . chatWithDevChat ( {
329+ this . executeHandlers ( "chatWithDevChat" , {
335330 command : "chatWithDevChat" ,
336331 message : res . payload . message || "" ,
337332 } ) ;
338333 }
339334
340335 resviceDeleteMessage ( res ) {
341336 const hash = res ?. payload ?. promptHash || "" ;
342- this . handle . deletedChatMessage ( {
337+ // this.handle.deletedChatMessage({
338+ // hash,
339+ // });
340+ this . executeHandlers ( "deletedChatMessage" , {
343341 hash,
344342 } ) ;
345343 }
@@ -354,8 +352,11 @@ class IdeaBridge {
354352 } ) ;
355353 } ) ;
356354 }
357-
358- this . handle . reloadMessage ( {
355+ // this.handle.reloadMessage({
356+ // entries: list.reverse(),
357+ // pageIndex: 0,
358+ // });
359+ this . executeHandlers ( "reloadMessage" , {
359360 entries : list . reverse ( ) ,
360361 pageIndex : 0 ,
361362 } ) ;
@@ -371,7 +372,10 @@ class IdeaBridge {
371372
372373 resviceTopicList ( res ) {
373374 const list = res . payload . topics ;
374- this . handle . listTopics ( list ) ;
375+ // this.handle.listTopics(list);
376+ this . executeHandlers ( "listTopics" , {
377+ list,
378+ } ) ;
375379 }
376380
377381 resviesContext ( res ) {
@@ -385,7 +389,8 @@ class IdeaBridge {
385389 command : "" ,
386390 } ;
387391 params . result = JSON . stringify ( contextObj ) ;
388- this . handle . contextDetailResponse ( params ) ;
392+ // this.handle.contextDetailResponse(params);
393+ this . executeHandlers ( "contextDetailResponse" , params ) ;
389394 }
390395
391396 resviceSettings ( res ) {
@@ -399,76 +404,93 @@ class IdeaBridge {
399404 }
400405
401406 // 当前的默认模型
402- this . handle . getSetting ( {
407+ // this.handle.getSetting({});
408+
409+ this . executeHandlers ( "getSetting" , {
403410 value : setting . currentModel ,
404411 key2 : "defaultModel" ,
405412 } ) ;
406413
407- this . handle . getUserAccessKey ( {
414+ // this.handle.getUserAccessKey({
415+ // endPoint: setting.apiBase,
416+ // accessKey: key,
417+ // keyType: key.startsWith("DC") ? "DevChat" : "OpenAi",
418+ // });
419+
420+ this . executeHandlers ( "getUserAccessKey" , {
408421 endPoint : setting . apiBase ,
409422 accessKey : key ,
410423 keyType : key . startsWith ( "DC" ) ? "DevChat" : "OpenAi" ,
411424 } ) ;
412- this . handle . getSetting ( {
425+
426+ // this.handle.getSetting({
427+ // value: setting.language,
428+ // key2: "Language",
429+ // });
430+
431+ this . executeHandlers ( "getSetting" , {
413432 value : setting . language ,
414433 key2 : "Language" ,
415434 } ) ;
416435 }
417436
418- resviceAccessKey ( res : string = "" ) {
419- const params = {
420- endPoint : "" ,
421- accessKey : res ,
422- keyType : res . startsWith ( "DC" ) ? "DevChat" : "OpenAi" ,
423- } ;
424- this . handle . getUserAccessKey ( params ) ;
425- }
426-
427437 resviceCommandList ( res ) {
428438 const result = res . payload . commands . map ( ( item ) => ( {
429439 name : item . name ,
430440 pattern : item . name ,
431441 description : item . description ,
432442 } ) ) ;
433- this . handle . regCommandList ( {
443+ // this.handle.regCommandList({
444+ // result,
445+ // });
446+ this . executeHandlers ( "regCommandList" , {
434447 result,
435448 } ) ;
436449 }
437450
438451 resviceContextList ( res ) {
439452 // 接受到的上下文列表
440-
441453 const result = res . payload . contexts . map ( ( item ) => ( {
442454 name : item . command ,
443455 pattern : item . command ,
444456 description : item . description ,
445457 } ) ) ;
446458
447- this . handle . regContextList ( { result } ) ;
459+ // this.handle.regContextList({ result });
460+ this . executeHandlers ( "regContextList" , {
461+ result,
462+ } ) ;
448463 }
449464
450465 resviceModelList ( response : any ) {
451466 // 接受到模型列表
452- this . handle [ "regModelList" ] ( {
467+ // this.handle["regModelList"]({
468+ // result: response.payload.models,
469+ // });
470+ this . executeHandlers ( "regModelList" , {
453471 result : response . payload . models ,
454472 } ) ;
455473 }
456474
457475 resviceMessage ( response : any ) {
458- console . log (
459- "response.metadata.isFinalChunk: " ,
460- response . metadata . isFinalChunk
461- ) ;
462476 // 接受到消息
463477 if ( response . metadata ?. isFinalChunk ) {
464478 // 结束对话
465- this . handle [ "receiveMessage" ] ( {
479+ // this.handle["receiveMessage"]({
480+ // text: response.payload?.message || response.metadata?.error || "",
481+ // isError: response.metadata?.error.length > 0,
482+ // hash: response.payload?.promptHash || "",
483+ // });
484+ this . executeHandlers ( "receiveMessage" , {
466485 text : response . payload ?. message || response . metadata ?. error || "" ,
467486 isError : response . metadata ?. error . length > 0 ,
468487 hash : response . payload ?. promptHash || "" ,
469488 } ) ;
470489 } else {
471- this . handle [ "receiveMessagePartial" ] ( {
490+ // this.handle["receiveMessagePartial"]({
491+ // text: response?.payload?.message || "",
492+ // });
493+ this . executeHandlers ( "receiveMessagePartial" , {
472494 text : response ?. payload ?. message || "" ,
473495 } ) ;
474496 }
@@ -482,8 +504,18 @@ class IdeaBridge {
482504 }
483505
484506 registerHandler ( messageType : string , handler : any ) {
485- // 注册回调函数
486- this . handle [ messageType ] = handler ;
507+ if ( ! this . handle [ messageType ] ) {
508+ this . handle [ messageType ] = [ ] ;
509+ }
510+ this . handle [ messageType ] . push ( handler ) ;
511+ }
512+
513+ executeHandlers ( messageType : string , data : any ) {
514+ if ( this . handle [ messageType ] ) {
515+ this . handle [ messageType ] . forEach ( ( handler ) => {
516+ handler ( data ) ;
517+ } ) ;
518+ }
487519 }
488520
489521 sendMessage ( message : any ) {
0 commit comments