11import type { User } from '@databuddy/auth' ;
2- import type { Website } from '@databuddy/shared' ;
2+ import type { StreamingUpdate } from '@databuddy/shared' ;
3+ import { createId , type Website } from '@databuddy/shared' ;
34import type { AssistantRequestType } from '../../schemas' ;
4- import type { StreamingUpdate } from '../utils/stream-utils' ;
55import { AIService } from './ai-service' ;
66import { AssistantSession , type SessionMetrics } from './assistant-session' ;
77import { ConversationRepository } from './conversation-repository' ;
@@ -30,6 +30,7 @@ export class AssistantOrchestrator {
3030 try {
3131 // Step 1: Generate AI response
3232 const aiResponse = await this . aiService . generateResponse ( session ) ;
33+ const aiMessageId = createId ( ) ;
3334
3435 if ( ! aiResponse . content ) {
3536 session . log ( 'AI response was empty' ) ;
@@ -42,12 +43,24 @@ export class AssistantOrchestrator {
4243 ] ;
4344 }
4445
46+ const streamingUpdates : StreamingUpdate [ ] = [
47+ {
48+ type : 'metadata' ,
49+ data : {
50+ conversationId : session . getContext ( ) . conversationId ,
51+ messageId : aiMessageId ,
52+ } ,
53+ } ,
54+ ] ;
55+
4556 // Step 2: Process the response into streaming updates
46- const streamingUpdates = await this . responseProcessor . process (
57+ const aiResponseUpdates = await this . responseProcessor . process (
4758 aiResponse . content ,
4859 session
4960 ) ;
5061
62+ streamingUpdates . push ( ...aiResponseUpdates ) ;
63+
5164 // Step 3: Save to database (async, don't block response)
5265 const finalResult = streamingUpdates . at ( - 1 ) ;
5366 if ( finalResult ) {
@@ -57,6 +70,7 @@ export class AssistantOrchestrator {
5770 this . saveConversationAsync (
5871 session ,
5972 aiResponse . content ,
73+ aiMessageId ,
6074 finalResult ,
6175 metrics
6276 ) ;
@@ -85,13 +99,15 @@ export class AssistantOrchestrator {
8599 private async saveConversationAsync (
86100 session : AssistantSession ,
87101 aiResponse : AIResponseContent ,
102+ messageId : string ,
88103 finalResult : StreamingUpdate ,
89104 metrics : SessionMetrics
90105 ) : Promise < void > {
91106 try {
92107 await this . conversationRepo . saveConversation (
93108 session ,
94109 aiResponse ,
110+ messageId ,
95111 finalResult ,
96112 metrics
97113 ) ;
@@ -110,15 +126,21 @@ export class AssistantOrchestrator {
110126 try {
111127 const errorAIResponse = {
112128 response_type : 'text' as const ,
113- text_response : errorResponse . content ,
129+ text_response :
130+ errorResponse . type === 'error'
131+ ? errorResponse . content
132+ : 'Oops! Something unexpected happened. Mind trying that again?' ,
114133 thinking_steps : [
115134 `Error: ${ originalError instanceof Error ? originalError . message : 'Unknown error' } ` ,
116135 ] ,
117136 } ;
118137
138+ const messageId = createId ( 'NANOID' ) ;
139+
119140 await this . conversationRepo . saveConversation (
120141 session ,
121142 errorAIResponse ,
143+ messageId ,
122144 errorResponse ,
123145 metrics
124146 ) ;
0 commit comments