@@ -5,9 +5,6 @@ import { streamText } from '~/lib/.server/llm/stream-text';
55import { stripIndents } from '~/utils/stripIndent' ;
66import type { IProviderSetting , ProviderInfo } from '~/types/model' ;
77
8- const encoder = new TextEncoder ( ) ;
9- const decoder = new TextDecoder ( ) ;
10-
118export async function action ( args : ActionFunctionArgs ) {
129 return enhancerAction ( args ) ;
1310}
@@ -107,45 +104,7 @@ async function enhancerAction({ context, request }: ActionFunctionArgs) {
107104 providerSettings,
108105 } ) ;
109106
110- const transformStream = new TransformStream ( {
111- transform ( chunk , controller ) {
112- const text = decoder . decode ( chunk ) ;
113- const lines = text . split ( '\n' ) . filter ( ( line ) => line . trim ( ) !== '' ) ;
114-
115- for ( const line of lines ) {
116- try {
117- // Handle token-based streaming format
118- if ( line . includes ( '0:"' ) ) {
119- // Extract all token contents and join them
120- const tokens = line . match ( / 0 : " ( [ ^ " ] + ) " / g) || [ ] ;
121- const content = tokens
122- . map ( token => token . slice ( 3 , - 1 ) ) // Remove the '0:"' prefix and '"' suffix
123- . join ( '' ) ;
124-
125- if ( content ) {
126- controller . enqueue ( encoder . encode ( content ) ) ;
127- }
128- continue ;
129- }
130-
131- // Try to parse as JSON if it's not token-based format
132- const parsed = JSON . parse ( line ) ;
133- if ( parsed . type === 'text' ) {
134- controller . enqueue ( encoder . encode ( parsed . value ) ) ;
135- }
136- } catch ( e ) {
137- // If not JSON and not token-based, treat as plain text
138- if ( ! line . includes ( 'e:' ) && ! line . includes ( 'd:' ) ) { // Skip metadata lines
139- controller . enqueue ( encoder . encode ( line ) ) ;
140- }
141- }
142- }
143- } ,
144- } ) ;
145-
146- const transformedStream = result . toDataStream ( ) . pipeThrough ( transformStream ) ;
147-
148- return new Response ( transformedStream , {
107+ return new Response ( result . textStream , {
149108 status : 200 ,
150109 headers : {
151110 'Content-Type' : 'text/plain; charset=utf-8' ,
0 commit comments