@@ -34,26 +34,44 @@ function createAIGatewayClient(config: {
3434
3535``` typescript
3636interface AIGatewayClient {
37- chatCompletionStream (request : AIGatewayChatRequest ): AsyncIterable <string >;
37+ streamChatCompletion (request : AIGatewayChatRequest ): AsyncIterable <AIChatCompletionEvent >;
3838}
3939```
4040
41- The iterable yields text deltas. Concatenate them to build the assistant response.
41+ The iterable yields completion events:
42+
43+ - ` text-delta ` — append ` event.text ` to build the assistant response
44+ - ` done ` — terminal event with an optional ` finishReason `
4245
4346## Related Types
4447
4548``` typescript
46- interface AIGatewayChatMessage {
47- role: " system" | " user" | " assistant" ;
48- content: string ;
49- }
49+ type AIGatewayChatMessage =
50+ | {
51+ role: " system" | " user" ;
52+ content: string ;
53+ }
54+ | {
55+ role: " assistant" ;
56+ content? : string ;
57+ };
5058
5159interface AIGatewayChatRequest {
5260 model: string ;
5361 messages: AIGatewayChatMessage [];
5462 stream? : boolean ;
5563 signal? : AbortSignal ;
5664}
65+
66+ type AIChatCompletionEvent =
67+ | {
68+ type: " text-delta" ;
69+ text: string ;
70+ }
71+ | {
72+ type: " done" ;
73+ finishReason? : string ;
74+ };
5775```
5876
5977## Usage
@@ -71,23 +89,26 @@ const aiClient = createAIGatewayClient({
7189 authClient ,
7290});
7391
74- const deltas : string [] = [] ;
75- for await (const delta of aiClient .chatCompletionStream ({
92+ let text = " " ;
93+ for await (const event of aiClient .streamChatCompletion ({
7694 model: " gpt-5-mini" ,
7795 messages: [{ role: " user" , content: " Hello" }],
7896})) {
79- deltas .push (delta );
97+ if (event .type === " text-delta" ) {
98+ text += event .text ;
99+ }
80100}
81101
82- console .log (deltas . join ( " " ) );
102+ console .log (text );
83103```
84104
85105## Notes
86106
87107- ` stream ` defaults to ` true `
88108- Pass ` stream: false ` when the endpoint returns a single JSON response instead of SSE
89- - Gemini-backed routes often need ` stream: false `
90- - The public API is intentionally text-only
109+ - ` stream: false ` still yields the same event shape: zero or one ` text-delta ` , then ` done `
110+ - The low-level API is intentionally narrow: text deltas plus completion metadata
111+ - ` request.signal ` is passed through so callers can abort in-flight work
91112
92113## Related
93114
0 commit comments