|
6 | 6 | createTriggerChatTransport, |
7 | 7 | TriggerChatTransport, |
8 | 8 | } from "./chatTransport.js"; |
| 9 | +import type { TriggerChatStream } from "./types.js"; |
9 | 10 | import type { UIMessage, UIMessageChunk } from "ai"; |
10 | 11 | import type { |
11 | 12 | TriggerChatRunState, |
@@ -149,6 +150,72 @@ describe("TriggerChatTransport", function () { |
149 | 150 | expect(observedStreamPath).toBe("/realtime/v1/streams/run_encoded_stream/chat%2Fspecial%20stream"); |
150 | 151 | }); |
151 | 152 |
|
| 153 | + it("uses defined stream object id when provided", async function () { |
| 154 | + let observedStreamPath: string | undefined; |
| 155 | + |
| 156 | + const streamDefinition = { |
| 157 | + id: "typed-stream-id", |
| 158 | + pipe: async function pipe() { |
| 159 | + throw new Error("not used in this test"); |
| 160 | + }, |
| 161 | + } as unknown as TriggerChatStream<UIMessage>; |
| 162 | + |
| 163 | + const server = await startServer(function (req, res) { |
| 164 | + if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") { |
| 165 | + res.writeHead(200, { |
| 166 | + "content-type": "application/json", |
| 167 | + "x-trigger-jwt": "pk_run_stream_object", |
| 168 | + }); |
| 169 | + res.end(JSON.stringify({ id: "run_stream_object" })); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + if (req.method === "GET") { |
| 174 | + observedStreamPath = req.url ?? ""; |
| 175 | + } |
| 176 | + |
| 177 | + if (req.method === "GET" && req.url === "/realtime/v1/streams/run_stream_object/typed-stream-id") { |
| 178 | + res.writeHead(200, { |
| 179 | + "content-type": "text/event-stream", |
| 180 | + }); |
| 181 | + writeSSE( |
| 182 | + res, |
| 183 | + "1-0", |
| 184 | + JSON.stringify({ type: "text-start", id: "typed_1" }) |
| 185 | + ); |
| 186 | + writeSSE( |
| 187 | + res, |
| 188 | + "2-0", |
| 189 | + JSON.stringify({ type: "text-end", id: "typed_1" }) |
| 190 | + ); |
| 191 | + res.end(); |
| 192 | + return; |
| 193 | + } |
| 194 | + |
| 195 | + res.writeHead(404); |
| 196 | + res.end(); |
| 197 | + }); |
| 198 | + |
| 199 | + const transport = new TriggerChatTransport({ |
| 200 | + task: "chat-task", |
| 201 | + accessToken: "pk_trigger", |
| 202 | + baseURL: server.url, |
| 203 | + stream: streamDefinition, |
| 204 | + }); |
| 205 | + |
| 206 | + const stream = await transport.sendMessages({ |
| 207 | + trigger: "submit-message", |
| 208 | + chatId: "chat-typed-stream", |
| 209 | + messageId: undefined, |
| 210 | + messages: [], |
| 211 | + abortSignal: undefined, |
| 212 | + }); |
| 213 | + |
| 214 | + const chunks = await readChunks(stream); |
| 215 | + expect(chunks).toHaveLength(2); |
| 216 | + expect(observedStreamPath).toBe("/realtime/v1/streams/run_stream_object/typed-stream-id"); |
| 217 | + }); |
| 218 | + |
152 | 219 | it("triggers task and streams chunks with rich default payload", async function () { |
153 | 220 | let receivedTriggerBody: Record<string, unknown> | undefined; |
154 | 221 |
|
|
0 commit comments