Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-colts-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai': minor
---

Added telemetry field to activities and events
15 changes: 12 additions & 3 deletions packages/typescript/ai/src/activities/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
RunFinishedEvent,
SchemaInput,
StreamChunk,
TelemetrySettings,
TextMessageContentEvent,
TextOptions,
Tool,
Expand Down Expand Up @@ -130,6 +131,10 @@ export interface TextActivityOptions<
* ```
*/
stream?: TStream
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: TextOptions['telemetry']
}

// ===========================
Expand Down Expand Up @@ -209,6 +214,7 @@ class TextEngine<
private readonly streamId: string
private readonly effectiveRequest?: Request | RequestInit
private readonly effectiveSignal?: AbortSignal
private readonly telemetry?: TelemetrySettings

private messages: Array<ModelMessage>
private iterationCount = 0
Expand Down Expand Up @@ -241,6 +247,7 @@ class TextEngine<
? { signal: config.params.abortController.signal }
: undefined
this.effectiveSignal = config.params.abortController?.signal
this.telemetry = config.params.telemetry
}

/** Get the accumulated content after the chat loop completes */
Expand Down Expand Up @@ -942,6 +949,7 @@ class TextEngine<
messageCount: number
hasTools: boolean
streaming: boolean
telemetry?: TelemetrySettings
} {
return {
requestId: this.requestId,
Expand All @@ -960,6 +968,7 @@ class TextEngine<
messageCount: this.initialMessageCount,
hasTools: this.tools.length > 0,
streaming: true,
telemetry: this.telemetry,
}
}

Expand Down Expand Up @@ -1194,10 +1203,10 @@ async function runAgenticStructuredOutput<TSchema extends SchemaInput>(
}

// Re-export adapter types
export { BaseTextAdapter } from './adapter'
export type {
TextAdapter,
TextAdapterConfig,
StructuredOutputOptions,
StructuredOutputResult,
TextAdapter,
TextAdapterConfig,
} from './adapter'
export { BaseTextAdapter } from './adapter'
13 changes: 10 additions & 3 deletions packages/typescript/ai/src/activities/generateImage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { aiEventClient } from '../../event-client.js'
import type { ImageGenerationOptions, ImageGenerationResult } from '../../types'
import type { ImageAdapter } from './adapter'
import type { ImageGenerationResult } from '../../types'

// ===========================
// Activity Kind
Expand Down Expand Up @@ -74,6 +74,10 @@ export interface ImageActivityOptions<
size?: ImageSizeForModel<TAdapter, TAdapter['model']>
/** Provider-specific options for image generation */
modelOptions?: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: ImageGenerationOptions['telemetry']
}

// ===========================
Expand Down Expand Up @@ -152,6 +156,7 @@ export async function generateImage<
numberOfImages: rest.numberOfImages,
size: rest.size as string | undefined,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: startTime,
})

Expand All @@ -168,6 +173,7 @@ export async function generateImage<
})),
duration,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: Date.now(),
})

Expand All @@ -177,6 +183,7 @@ export async function generateImage<
model,
usage: result.usage,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: Date.now(),
})
}
Expand All @@ -199,9 +206,9 @@ export function createImageOptions<
}

// Re-export adapter types
export { BaseImageAdapter } from './adapter'
export type {
AnyImageAdapter,
ImageAdapter,
ImageAdapterConfig,
AnyImageAdapter,
} from './adapter'
export { BaseImageAdapter } from './adapter'
10 changes: 8 additions & 2 deletions packages/typescript/ai/src/activities/generateSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { aiEventClient } from '../../event-client.js'
import type { TTSOptions, TTSResult } from '../../types'
import type { TTSAdapter } from './adapter'
import type { TTSResult } from '../../types'

// ===========================
// Activity Kind
Expand Down Expand Up @@ -53,6 +53,10 @@ export interface TTSActivityOptions<
speed?: number
/** Provider-specific options for TTS generation */
modelOptions?: TTSProviderOptions<TAdapter>
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: TTSOptions['telemetry']
}

// ===========================
Expand Down Expand Up @@ -117,6 +121,7 @@ export async function generateSpeech<
format: rest.format,
speed: rest.speed,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: startTime,
})

Expand All @@ -133,6 +138,7 @@ export async function generateSpeech<
contentType: result.contentType,
duration,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: Date.now(),
})

Expand All @@ -154,5 +160,5 @@ export function createSpeechOptions<
}

// Re-export adapter types
export type { TTSAdapter, TTSAdapterConfig, AnyTTSAdapter } from './adapter'
export { BaseTTSAdapter } from './adapter'
export type { AnyTTSAdapter, TTSAdapter, TTSAdapterConfig } from './adapter'
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { aiEventClient } from '../../event-client.js'
import type { TranscriptionOptions, TranscriptionResult } from '../../types'
import type { TranscriptionAdapter } from './adapter'
import type { TranscriptionResult } from '../../types'

// ===========================
// Activity Kind
Expand Down Expand Up @@ -53,6 +53,10 @@ export interface TranscriptionActivityOptions<
responseFormat?: 'json' | 'text' | 'srt' | 'verbose_json' | 'vtt'
/** Provider-specific options for transcription */
modelOptions?: TranscriptionProviderOptions<TAdapter>
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: TranscriptionOptions['telemetry']
}

// ===========================
Expand Down Expand Up @@ -120,6 +124,7 @@ export async function generateTranscription<
prompt: rest.prompt,
responseFormat: rest.responseFormat,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: startTime,
})

Expand All @@ -134,6 +139,7 @@ export async function generateTranscription<
language: result.language,
duration,
modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
telemetry: rest.telemetry,
timestamp: Date.now(),
})

Expand All @@ -156,9 +162,9 @@ export function createTranscriptionOptions<
}

// Re-export adapter types
export { BaseTranscriptionAdapter } from './adapter'
export type {
AnyTranscriptionAdapter,
TranscriptionAdapter,
TranscriptionAdapterConfig,
AnyTranscriptionAdapter,
} from './adapter'
export { BaseTranscriptionAdapter } from './adapter'
24 changes: 16 additions & 8 deletions packages/typescript/ai/src/activities/generateVideo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
*/

import { aiEventClient } from '../../event-client.js'
import type { VideoAdapter } from './adapter'
import type {
VideoGenerationOptions,
VideoJobResult,
VideoStatusResult,
VideoUrlResult,
} from '../../types'
import type { VideoAdapter } from './adapter'

// ===========================
// Activity Kind
Expand Down Expand Up @@ -51,6 +52,10 @@ interface VideoActivityBaseOptions<
> {
/** The video adapter to use (must be created with a model) */
adapter: TAdapter & { kind: typeof kind }
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: VideoGenerationOptions['telemetry']
}

/**
Expand Down Expand Up @@ -202,16 +207,15 @@ export async function generateVideo<
*/
export async function getVideoJobStatus<
TAdapter extends VideoAdapter<string, object>,
>(options: {
adapter: TAdapter & { kind: typeof kind }
jobId: string
}): Promise<{
>(
options: VideoStatusOptions<TAdapter>,
): Promise<{
status: 'pending' | 'processing' | 'completed' | 'failed'
progress?: number
url?: string
error?: string
}> {
const { adapter, jobId } = options
const { adapter, jobId, ...rest } = options
const requestId = createId('video-status')
const startTime = Date.now()

Expand All @@ -221,6 +225,7 @@ export async function getVideoJobStatus<
model: adapter.model,
requestType: 'status',
jobId,
telemetry: rest.telemetry,
timestamp: startTime,
})

Expand All @@ -241,6 +246,7 @@ export async function getVideoJobStatus<
progress: statusResult.progress,
url: urlResult.url,
duration: Date.now() - startTime,
telemetry: rest.telemetry,
timestamp: Date.now(),
})
return {
Expand All @@ -260,6 +266,7 @@ export async function getVideoJobStatus<
error:
error instanceof Error ? error.message : 'Failed to get video URL',
duration: Date.now() - startTime,
telemetry: rest.telemetry,
timestamp: Date.now(),
})
// If URL fetch fails, still return status
Expand All @@ -282,6 +289,7 @@ export async function getVideoJobStatus<
progress: statusResult.progress,
error: statusResult.error,
duration: Date.now() - startTime,
telemetry: rest.telemetry,
timestamp: Date.now(),
})

Expand All @@ -307,9 +315,9 @@ export function createVideoOptions<
}

// Re-export adapter types
export { BaseVideoAdapter } from './adapter'
export type {
AnyVideoAdapter,
VideoAdapter,
VideoAdapterConfig,
AnyVideoAdapter,
} from './adapter'
export { BaseVideoAdapter } from './adapter'
16 changes: 12 additions & 4 deletions packages/typescript/ai/src/activities/summarize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/

import { aiEventClient } from '../../event-client.js'
import type { SummarizeAdapter } from './adapter'
import type {
StreamChunk,
SummarizationOptions,
SummarizationResult,
} from '../../types'
import type { SummarizeAdapter } from './adapter'

// ===========================
// Activity Kind
Expand Down Expand Up @@ -65,6 +65,10 @@ export interface SummarizeActivityOptions<
* @default false
*/
stream?: TStream
/**
* Telemetry data for tracking and monitoring.
*/
telemetry?: SummarizationOptions['telemetry']
}

// ===========================
Expand Down Expand Up @@ -174,7 +178,7 @@ export function summarize<
async function runSummarize(
options: SummarizeActivityOptions<SummarizeAdapter<string, object>, false>,
): Promise<SummarizationResult> {
const { adapter, text, maxLength, style, focus } = options
const { adapter, text, maxLength, style, focus, telemetry } = options
const model = adapter.model
const requestId = createId('summarize')
const inputLength = text.length
Expand All @@ -185,6 +189,7 @@ async function runSummarize(
provider: adapter.name,
model,
inputLength,
telemetry,
timestamp: startTime,
})

Expand All @@ -194,6 +199,7 @@ async function runSummarize(
maxLength,
style,
focus,
telemetry,
}

const result = await adapter.summarize(summarizeOptions)
Expand All @@ -208,6 +214,7 @@ async function runSummarize(
inputLength,
outputLength,
duration,
telemetry,
timestamp: Date.now(),
})

Expand All @@ -231,6 +238,7 @@ async function* runStreamingSummarize(
maxLength,
style,
focus,
telemetry: options.telemetry,
}

// Use real streaming if the adapter supports it
Expand Down Expand Up @@ -280,9 +288,9 @@ export function createSummarizeOptions<
}

// Re-export adapter types
export { BaseSummarizeAdapter } from './adapter'
export type {
AnySummarizeAdapter,
SummarizeAdapter,
SummarizeAdapterConfig,
AnySummarizeAdapter,
} from './adapter'
export { BaseSummarizeAdapter } from './adapter'
Loading
Loading