11import { z } from "zod" ;
2- import { JobBuilder } from "~/jobs/builder" ;
2+ import { HTTPError , JobBuilder } from "~/jobs/builder" ;
33import type {
44 ChatJob ,
55 ChatStreamOptions ,
6+ ChatToolChoiceSchema ,
67 Message ,
78 ResponseFormat ,
89} from "./schema" ;
@@ -22,6 +23,27 @@ export abstract class ChatJobBuilder<
2223 } ;
2324 }
2425
26+ async * handleStream ( response : Response ) : AsyncGenerator < Job [ "output" ] > {
27+ throw new Error ( "Not implemented" ) ;
28+ }
29+
30+ async * stream ( options ?: ChatStreamOptions ) : AsyncGenerator < Job [ "output" ] > {
31+ this . input . stream = true ;
32+ this . input . streamOptions = options ;
33+ if ( ! this . handleStream ) {
34+ throw new Error ( "Stream not supported" ) ;
35+ }
36+ const request = this . makeRequest ! ( ) ;
37+ const response = await fetch ( request ) ;
38+ if ( ! response . ok ) {
39+ throw new HTTPError (
40+ `Fetch error: ${ response . statusText } ` ,
41+ response . status ,
42+ ) ;
43+ }
44+ yield * this . handleStream ( response ) ;
45+ }
46+
2547 system ( system : string ) {
2648 this . input . system = system ;
2749 return this ;
@@ -73,7 +95,7 @@ export abstract class ChatJobBuilder<
7395 return this ;
7496 }
7597
76- toolChoice ( toolChoice : string ) {
98+ toolChoice ( toolChoice : z . infer < typeof ChatToolChoiceSchema > ) {
7799 this . input . toolChoice = toolChoice ;
78100 return this ;
79101 }
@@ -92,12 +114,4 @@ export abstract class ChatJobBuilder<
92114
93115 return this ;
94116 }
95-
96- stream ( streamOptions ?: ChatStreamOptions ) {
97- this . input . stream = true ;
98- if ( streamOptions ) {
99- this . input . streamOptions = streamOptions ;
100- }
101- return this ;
102- }
103117}
0 commit comments