MCPcopy Index your code
hub / github.com/TanStack/ai / emitEvent

Method emitEvent

packages/ai-client/src/events.ts:329–387  ·  view source on GitHub ↗

* Emit an event with automatic clientId and timestamp for client/tool events

(eventName: string, data?: Record<string, any>)

Source from the content-addressed store, hash-verified

327 * Emit an event with automatic clientId and timestamp for client/tool events
328 */
329 protected emitEvent(eventName: string, data?: Record<string, any>): void {
330 const timestamp = Date.now()
331 const isUserVisibleEvent =
332 eventName.startsWith('text:') ||
333 eventName.startsWith('tools:') ||
334 eventName.startsWith('structured-output:') ||
335 eventName === 'devtools:tool-fixture:applied'
336 const includesClientContext =
337 eventName.startsWith('client:') ||
338 eventName.startsWith('tools:') ||
339 eventName.startsWith('text:') ||
340 eventName.startsWith('structured-output:') ||
341 eventName === 'devtools:tool-fixture:applied'
342 const visibility = isUserVisibleEvent ? 'user-visible' : 'client-state'
343 const envelopeContext = {
344 hookId: this.clientId,
345 ...(typeof data?.threadId === 'string'
346 ? { threadId: data.threadId }
347 : {}),
348 ...(typeof data?.runId === 'string' ? { runId: data.runId } : {}),
349 ...(typeof data?.streamId === 'string'
350 ? { streamId: data.streamId }
351 : {}),
352 ...(typeof data?.messageId === 'string'
353 ? { messageId: data.messageId }
354 : {}),
355 ...(typeof data?.toolCallId === 'string'
356 ? { toolCallId: data.toolCallId }
357 : {}),
358 }
359
360 // For client:* and tool:* events, automatically add clientId and timestamp
361 if (includesClientContext) {
362 const envelope = createAIDevtoolsEventEnvelope({
363 eventType: eventName,
364 clientId: this.clientId,
365 ...envelopeContext,
366 source: 'client',
367 visibility,
368 timestamp,
369 })
370 aiEventClient.emit(eventName as any, {
371 ...data,
372 ...envelope,
373 })
374 } else {
375 const envelope = createAIDevtoolsEventEnvelope({
376 eventType: eventName,
377 source: 'client',
378 visibility: 'client-state',
379 timestamp,
380 })
381 // For other events, just add timestamp
382 aiEventClient.emit(eventName as any, {
383 ...data,
384 ...envelope,
385 })
386 }

Callers 15

clientCreatedFunction · 0.45
loadingChangedFunction · 0.45
errorChangedFunction · 0.45
textUpdatedFunction · 0.45
toolCallStateChangedFunction · 0.45
thinkingUpdatedFunction · 0.45
structuredOutputChangedFunction · 0.45
approvalRequestedFunction · 0.45
messageAppendedFunction · 0.45
messageSentFunction · 0.45
reloadedFunction · 0.45
stoppedFunction · 0.45

Calls 3

nowMethod · 0.80
emitMethod · 0.45

Tested by

no test coverage detected