1- import { RoomsSocket , RoomsSocketConfig } from "../utils/socket-utils.js" ;
2- import { createAxiosClient } from "../utils/axios-client.js" ;
3- import { AgentConversation , AgentMessage } from "./agents.types.js" ;
1+ import { RoomsSocket } from "../utils/socket-utils.js" ;
2+ import { AgentConversation , AgentMessage } from "./types.js" ;
43import { AxiosInstance } from "axios" ;
4+ import { ModelFilterParams } from "../types.js" ;
55
66export type AgentsModuleConfig = {
77 axios : AxiosInstance ;
@@ -14,7 +14,6 @@ export function createAgentsModule({
1414 socket,
1515 appId,
1616} : AgentsModuleConfig ) {
17- let currentConversation : any = null ;
1817 const baseURL = `/apps/${ appId } /agents` ;
1918
2019 const getConversations = ( ) => {
@@ -27,48 +26,50 @@ export function createAgentsModule({
2726 ) ;
2827 } ;
2928
30- const listConversations = ( filterParams : any ) => {
29+ const listConversations = ( filterParams : ModelFilterParams ) => {
3130 return axios . get < any , AgentConversation [ ] > ( `${ baseURL } /conversations` , {
3231 params : filterParams ,
3332 } ) ;
3433 } ;
3534
36- const createConversation = ( conversation : any ) => {
35+ const createConversation = ( conversation : {
36+ agent_name : string ;
37+ metadata ?: Record < string , any > ;
38+ } ) => {
3739 return axios . post < any , AgentConversation > (
3840 `${ baseURL } /conversations` ,
3941 conversation
4042 ) ;
4143 } ;
42-
43- const addMessage = ( conversation : any , message : any ) => {
44- // this whole trick with current conversation so that we can call the onUpdateModel with the latest messages
45- let convLatestMessages = null ;
46- if ( currentConversation && currentConversation . id === conversation . id ) {
47- convLatestMessages = currentConversation . messages ;
48- } else {
49- currentConversation = conversation ;
50- convLatestMessages = conversation . messages ;
51- }
52- conversation . messages = [ ...convLatestMessages , message ] ;
53- socket . handlers . update_model ( {
54- room : `/agent-conversations/${ conversation . id } ` ,
55- data : JSON . stringify ( conversation ) ,
56- } ) ;
44+
45+ const addMessage = async (
46+ conversation : AgentConversation ,
47+ message : AgentMessage
48+ ) => {
49+ const room = `/agent-conversations/${ conversation . id } ` ;
50+ await socket . updateModel (
51+ room ,
52+ {
53+ ...conversation ,
54+ messages : [ ...( conversation . messages || [ ] ) , message ] ,
55+ }
56+ ) ;
5757 return axios . post < any , AgentMessage > (
5858 `${ baseURL } /conversations/${ conversation . id } /messages` ,
5959 message
6060 ) ;
6161 } ;
6262
63- const subscribeToConversation = ( conversationId : string , onUpdate : any ) => {
64- return socket . subscribeToRoom ( `/agent-conversations/${ conversationId } ` , {
63+ const subscribeToConversation = (
64+ conversationId : string ,
65+ onUpdate ?: ( conversation : AgentConversation ) => void
66+ ) => {
67+ const room = `/agent-conversations/${ conversationId } ` ;
68+ return socket . subscribeToRoom ( room , {
6569 connect : ( ) => { } ,
6670 update_model : ( { data : jsonStr } ) => {
67- const data = JSON . parse ( jsonStr ) as { } & { id : string } ;
68- if ( currentConversation && currentConversation . id === data . id ) {
69- currentConversation = data ;
70- }
71- onUpdate ( data ) ;
71+ const conv = JSON . parse ( jsonStr ) as AgentConversation ;
72+ onUpdate ?.( conv ) ;
7273 } ,
7374 } ) ;
7475 } ;
0 commit comments