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

Method constructor

packages/ai-client/src/chat-client.ts:170–436  ·  view source on GitHub ↗
(options: ChatClientOptions<TTools, TContext>)

Source from the content-addressed store, hash-verified

168 }
169
170 constructor(options: ChatClientOptions<TTools, TContext>) {
171 this.uniqueId = options.id || this.generateUniqueId('chat')
172 this.threadId = options.threadId || this.generateUniqueId('thread')
173 if (options.persistence) {
174 this.persistor = new ChatPersistor(
175 options.persistence,
176 this.uniqueId,
177 (messages) => this.processor.setMessages(messages),
178 )
179 }
180 // Both `body` (deprecated) and `forwardedProps` populate the AG-UI
181 // `RunAgentInput.forwardedProps` wire field. They are stored
182 // separately so `updateOptions` can replace one without touching the
183 // other; the merge happens at send time, with `forwardedProps`
184 // winning on key collision.
185 this.bodyOption = options.body || {}
186 this.forwardedPropsOption = options.forwardedProps || {}
187 this.context = options.context
188 this.connection = normalizeConnectionAdapter(resolveTransport(options))
189
190 // Build client tools map
191 this.clientToolsRef = { current: new Map() }
192 if (options.tools) {
193 for (const tool of options.tools) {
194 this.clientToolsRef.current.set(tool.name, tool)
195 }
196 }
197
198 this.devtoolsBridge = (
199 options.devtoolsBridgeFactory ?? createNoOpChatDevtoolsBridge
200 )(this.buildDevtoolsBridgeOptions(options.devtools))
201 this.events = this.devtoolsBridge.events
202
203 this.callbacksRef = {
204 current: {
205 onResponse: options.onResponse || (() => {}),
206 onChunk: options.onChunk || (() => {}),
207 onFinish: options.onFinish || (() => {}),
208 onError: options.onError || (() => {}),
209 onMessagesChange: options.onMessagesChange || (() => {}),
210 onLoadingChange: options.onLoadingChange || (() => {}),
211 onErrorChange: options.onErrorChange || (() => {}),
212 onStatusChange: options.onStatusChange || (() => {}),
213 onSubscriptionChange: options.onSubscriptionChange || (() => {}),
214 onConnectionStatusChange:
215 options.onConnectionStatusChange || (() => {}),
216 onSessionGeneratingChange:
217 options.onSessionGeneratingChange || (() => {}),
218 onCustomEvent: options.onCustomEvent || (() => {}),
219 },
220 }
221
222 // Create StreamProcessor with event handlers.
223 // Use conditional spreads so we don't pass `undefined` into
224 // `StreamProcessorOptions` fields under `exactOptionalPropertyTypes`.
225 const persistedMessages = this.persistor?.readInitial()
226 const initialMessages = Array.isArray(persistedMessages)
227 ? persistedMessages

Callers

nothing calls this directly

Calls 15

generateUniqueIdMethod · 0.95
setStatusMethod · 0.95
resolveProcessingMethod · 0.95
reportStreamErrorMethod · 0.95
setMessagesMethod · 0.80
readInitialMethod · 0.80
notifyMessagesChangedMethod · 0.80

Tested by

no test coverage detected