Skip to content
Open
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
48 changes: 38 additions & 10 deletions js/src/auto-instrumentations/configs/ai-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,33 @@ export const aiSDKConfigs: InstrumentationConfig[] = [
},
},

// streamText - function returning stream
// streamText - async function (v3 only, before the sync refactor in v4)
{
channelName: aiSDKChannels.streamText.channelName,
module: {
name: "ai",
versionRange: ">=3.0.0",
versionRange: ">=3.0.0 <4.0.0",
filePath: "dist/index.mjs",
},
functionQuery: {
functionName: "streamText",
kind: "Async",
},
},

// streamText - sync function returning stream (v4+)
{
channelName: aiSDKChannels.streamTextSync.channelName,
module: {
name: "ai",
versionRange: ">=4.0.0",
filePath: "dist/index.mjs",
},
functionQuery: {
functionName: "streamText",
kind: "Sync",
},
},
{
channelName: aiSDKChannels.streamText.channelName,
module: {
Expand Down Expand Up @@ -91,19 +105,33 @@ export const aiSDKConfigs: InstrumentationConfig[] = [
},
},

// streamObject - function returning stream
// streamObject - async function (v3 only, before the sync refactor in v4)
{
channelName: aiSDKChannels.streamObject.channelName,
module: {
name: "ai",
versionRange: ">=3.0.0",
versionRange: ">=3.0.0 <4.0.0",
filePath: "dist/index.mjs",
},
functionQuery: {
functionName: "streamObject",
kind: "Async",
},
},

// streamObject - sync function returning stream (v4+)
{
channelName: aiSDKChannels.streamObjectSync.channelName,
module: {
name: "ai",
versionRange: ">=4.0.0",
filePath: "dist/index.mjs",
},
functionQuery: {
functionName: "streamObject",
kind: "Sync",
},
},
{
channelName: aiSDKChannels.streamObject.channelName,
module: {
Expand Down Expand Up @@ -147,32 +175,32 @@ export const aiSDKConfigs: InstrumentationConfig[] = [
},
},

// Agent.stream - async method (v5 only)
// Agent.stream - sync method (v5 only)
// The compiled AI SDK bundle emits this as an anonymous class method, so we
// target the first async `stream` method in the file instead of a class name.
// target the first sync `stream` method in the file instead of a class name.
{
channelName: aiSDKChannels.agentStream.channelName,
channelName: aiSDKChannels.agentStreamSync.channelName,
module: {
name: "ai",
versionRange: ">=5.0.0 <6.0.0",
filePath: "dist/index.mjs",
},
functionQuery: {
methodName: "stream",
kind: "Async",
kind: "Sync",
index: 0,
},
},
{
channelName: aiSDKChannels.agentStream.channelName,
channelName: aiSDKChannels.agentStreamSync.channelName,
module: {
name: "ai",
versionRange: ">=5.0.0 <6.0.0",
filePath: "dist/index.js",
},
functionQuery: {
methodName: "stream",
kind: "Async",
kind: "Sync",
index: 0,
},
},
Expand Down
9 changes: 9 additions & 0 deletions js/src/instrumentation/plugins/ai-sdk-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ export const aiSDKChannels = defineChannels("ai", {
channelName: "Agent.stream",
kind: "async",
}),
agentStreamSync: channel<
[AISDKCallParams],
AISDKResult,
AISDKChannelContext,
unknown
>({
channelName: "Agent.stream.sync",
kind: "sync-stream",
}),
toolLoopAgentGenerate: channel<
[AISDKCallParams],
AISDKStreamResult,
Expand Down
20 changes: 19 additions & 1 deletion js/src/instrumentation/plugins/ai-sdk-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class AISDKPlugin extends BasePlugin {
}),
);

// Agent.stream - async method returning stream
// Agent.stream - async method returning stream (v5, used by wrapAISDK)
this.unsubscribers.push(
traceStreamingChannel(aiSDKChannels.agentStream, {
name: "Agent.stream",
Expand All @@ -236,6 +236,24 @@ export class AISDKPlugin extends BasePlugin {
}),
);

// Agent.stream - sync method returning stream (v5, used by auto-hook)
this.unsubscribers.push(
traceSyncStreamChannel(aiSDKChannels.agentStreamSync, {
name: "Agent.stream",
type: SpanTypeAttribute.LLM,
extractInput: ([params], event, span) =>
prepareAISDKInput(params, event, span, denyOutputPaths),
patchResult: ({ endEvent, result, span, startTime }) =>
patchAISDKStreamingResult({
defaultDenyOutputPaths: denyOutputPaths,
endEvent,
result,
span,
startTime,
}),
}),
);

// ToolLoopAgent.generate - async method
this.unsubscribers.push(
traceStreamingChannel(aiSDKChannels.toolLoopAgentGenerate, {
Expand Down
Loading