MCPcopy
hub / github.com/TanStack/ai / runChatWithMiddleware

Function runChatWithMiddleware

packages/ai/tests/middleware.test.ts:1364–1393  ·  view source on GitHub ↗
(params: {
      threadId?: string
      conversationId?: string
    })

Source from the content-addressed store, hash-verified

1362 // ==========================================================================
1363 describe('threadId / conversationId', () => {
1364 const runChatWithMiddleware = async (params: {
1365 threadId?: string
1366 conversationId?: string
1367 }): Promise<{
1368 ctxThreadId: string | undefined
1369 ctxConvId: string | undefined
1370 }> => {
1371 let ctxThreadId: string | undefined
1372 let ctxConvId: string | undefined
1373 const { adapter } = createMockAdapter({
1374 iterations: [
1375 [ev.runStarted(), ev.textContent('hi'), ev.runFinished('stop')],
1376 ],
1377 })
1378 const middleware: ChatMiddleware = {
1379 name: 'test',
1380 onStart: (ctx) => {
1381 ctxThreadId = ctx.threadId
1382 ctxConvId = ctx.conversationId
1383 },
1384 }
1385 const stream = chat({
1386 adapter,
1387 messages: [{ role: 'user', content: 'Hi' }],
1388 middleware: [middleware],
1389 ...params,
1390 })
1391 await collectChunks(stream as AsyncIterable<StreamChunk>)
1392 return { ctxThreadId, ctxConvId }
1393 }
1394
1395 it('uses caller-provided threadId for both ctx.threadId and ctx.conversationId (legacy alias)', async () => {
1396 const { ctxThreadId, ctxConvId } = await runChatWithMiddleware({

Callers 1

middleware.test.tsFile · 0.85

Calls 3

createMockAdapterFunction · 0.90
chatFunction · 0.90
collectChunksFunction · 0.90

Tested by

no test coverage detected