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

Function createMockConnectionAdapter

packages/ai-client/tests/test-utils.ts:83–135  ·  view source on GitHub ↗
(
  options: MockConnectionAdapterOptions = {},
)

Source from the content-addressed store, hash-verified

81 * ```
82 */
83export function createMockConnectionAdapter(
84 options: MockConnectionAdapterOptions = {},
85): ConnectConnectionAdapter {
86 const {
87 chunks = [],
88 chunkDelay = 0,
89 shouldError = false,
90 error = new Error('Mock adapter error'),
91 onConnect,
92 onAbort,
93 } = options
94
95 return {
96 async *connect(messages, data, abortSignal) {
97 if (onConnect) {
98 // Type assertion: messages can be ModelMessage[] or UIMessage[]
99 // Filter out system messages if present
100 const filteredMessages = (messages as any[]).filter(
101 (m: any) => !('role' in m) || m.role !== 'system',
102 )
103 onConnect(filteredMessages as any, data, abortSignal)
104 }
105
106 if (shouldError) {
107 throw error
108 }
109
110 for (const chunk of chunks) {
111 // Check abort signal before yielding
112 if (abortSignal?.aborted) {
113 if (onAbort) {
114 onAbort(abortSignal)
115 }
116 return
117 }
118
119 if (chunkDelay > 0) {
120 await new Promise((resolve) => setTimeout(resolve, chunkDelay))
121 }
122
123 // Check again after delay
124 if (abortSignal?.aborted) {
125 if (onAbort) {
126 onAbort(abortSignal)
127 }
128 return
129 }
130
131 yield chunk
132 }
133 },
134 }
135}
136
137/**
138 * Helper to create simple text content chunks (AG-UI format)

Calls

no outgoing calls

Tested by 4

mountStructuredHostFunction · 0.72
createClientFunction · 0.72
useChangingChatFunction · 0.72
useChangingChatFunction · 0.72