1
1
import { api } from '@/trpc/server' ;
2
2
import { trackEvent } from '@/utils/analytics/server' ;
3
- import { convertToStreamMessages , getToolSetFromType } from '@onlook/ai' ;
3
+ import { AgentStreamer , RootAgent } from '@onlook/ai' ;
4
4
import { toDbMessage } from '@onlook/db' ;
5
- import { ChatType , type ChatMessage , type ChatMetadata } from '@onlook/models' ;
6
- import { stepCountIs , streamText } from 'ai' ;
5
+ import { ChatType , type ChatMessage } from '@onlook/models' ;
7
6
import { type NextRequest } from 'next/server' ;
8
7
import { v4 as uuidv4 } from 'uuid' ;
9
- import { checkMessageLimit , decrementUsage , errorHandler , getModelFromType , getSupabaseUser , getSystemPromptFromType , incrementUsage , repairToolCall } from './helpers' ;
10
-
11
- const MAX_STEPS = 20 ;
8
+ import { checkMessageLimit , decrementUsage , errorHandler , getSupabaseUser , incrementUsage , repairToolCall } from './helpers' ;
12
9
13
10
export async function POST ( req : NextRequest ) {
14
11
try {
@@ -77,65 +74,41 @@ export const streamResponse = async (req: NextRequest, userId: string) => {
77
74
if ( chatType === ChatType . EDIT ) {
78
75
usageRecord = await incrementUsage ( req , traceId ) ;
79
76
}
80
- const modelConfig = await getModelFromType ( chatType ) ;
81
- const { model, providerOptions, headers } = modelConfig ;
82
- const systemPrompt = getSystemPromptFromType ( chatType ) ;
83
- const tools = getToolSetFromType ( chatType ) ;
84
- const result = streamText ( {
85
- model,
86
- headers,
87
- tools,
88
- stopWhen : stepCountIs ( MAX_STEPS ) ,
89
- messages : [
90
- {
91
- role : 'system' ,
92
- content : systemPrompt ,
93
- providerOptions,
94
- } ,
95
- ...convertToStreamMessages ( messages ) ,
96
- ] ,
97
- experimental_telemetry : {
98
- isEnabled : true ,
99
- metadata : {
100
- conversationId,
101
- projectId,
102
- userId,
103
- chatType : chatType ,
104
- tags : [ 'chat' ] ,
105
- langfuseTraceId : traceId ,
106
- sessionId : conversationId ,
107
- } ,
108
- } ,
109
- experimental_repairToolCall : repairToolCall ,
110
- onError : async ( error ) => {
111
- console . error ( 'Error in chat stream call' , error ) ;
112
- // if there was an error with the API, do not penalize the user
113
- await decrementUsage ( req , usageRecord ) ;
114
77
115
- // Ensure the stream stops on error by re-throwing
116
- if ( error instanceof Error ) {
117
- throw error ;
118
- } else {
119
- const errorMessage = typeof error === 'string' ? error : JSON . stringify ( error ) ;
120
- throw new Error ( errorMessage ) ;
121
- }
122
- }
123
- } )
78
+ // Create RootAgent instance
79
+ const agent = await RootAgent . create ( chatType ) ;
80
+ const streamer = new AgentStreamer ( agent , conversationId ) ;
124
81
125
- return result . toUIMessageStreamResponse < ChatMessage > (
126
- {
127
- originalMessages : messages ,
128
- generateMessageId : ( ) => uuidv4 ( ) ,
129
- messageMetadata : ( { part } ) => {
130
- return {
131
- createdAt : new Date ( ) ,
82
+ return streamer . streamText ( messages , {
83
+ streamTextConfig : {
84
+ experimental_telemetry : {
85
+ isEnabled : true ,
86
+ metadata : {
132
87
conversationId,
133
- context : [ ] ,
134
- checkpoints : [ ] ,
135
- finishReason : part . type === 'finish-step' ? part . finishReason : undefined ,
136
- usage : part . type === 'finish-step' ? part . usage : undefined ,
137
- } satisfies ChatMetadata ;
88
+ projectId,
89
+ userId,
90
+ chatType : chatType ,
91
+ tags : [ 'chat' ] ,
92
+ langfuseTraceId : traceId ,
93
+ sessionId : conversationId ,
94
+ } ,
138
95
} ,
96
+ experimental_repairToolCall : repairToolCall ,
97
+ onError : async ( error ) => {
98
+ console . error ( 'Error in chat stream call' , error ) ;
99
+ // if there was an error with the API, do not penalize the user
100
+ await decrementUsage ( req , usageRecord ) ;
101
+
102
+ // Ensure the stream stops on error by re-throwing
103
+ if ( error instanceof Error ) {
104
+ throw error ;
105
+ } else {
106
+ const errorMessage = typeof error === 'string' ? error : JSON . stringify ( error ) ;
107
+ throw new Error ( errorMessage ) ;
108
+ }
109
+ } ,
110
+ } ,
111
+ toUIMessageStreamResponseConfig : {
139
112
onFinish : async ( { messages : finalMessages } ) => {
140
113
const messagesToStore = finalMessages
141
114
. filter ( msg =>
@@ -149,8 +122,8 @@ export const streamResponse = async (req: NextRequest, userId: string) => {
149
122
} ) ;
150
123
} ,
151
124
onError : errorHandler ,
152
- }
153
- ) ;
125
+ } ,
126
+ } ) ;
154
127
} catch ( error ) {
155
128
console . error ( 'Error in streamResponse setup' , error ) ;
156
129
// If there was an error setting up the stream and we incremented usage, revert it
0 commit comments