@@ -189,6 +189,20 @@ function findStreamTrace(events: CapturedLogEvent[]) {
189189 return { child, operation, parent } ;
190190}
191191
192+ function findEmbedTrace ( events : CapturedLogEvent [ ] ) {
193+ const operation = findLatestSpan ( events , "ai-sdk-embed-operation" ) ;
194+ const parent = findParentSpan ( events , "embed" , operation ?. span . id ) ;
195+
196+ return { operation, parent } ;
197+ }
198+
199+ function findEmbedManyTrace ( events : CapturedLogEvent [ ] ) {
200+ const operation = findLatestSpan ( events , "ai-sdk-embed-many-operation" ) ;
201+ const parent = findParentSpan ( events , "embedMany" , operation ?. span . id ) ;
202+
203+ return { operation, parent } ;
204+ }
205+
192206function findToolTrace ( events : CapturedLogEvent [ ] ) {
193207 const operation = findLatestSpan ( events , "ai-sdk-tool-operation" ) ;
194208 const parent = findParentSpan ( events , "generateText" , operation ?. span . id ) ;
@@ -641,6 +655,24 @@ function expectAISDKParentSpan(span: CapturedLogEvent | undefined) {
641655 ) . toBe ( "string" ) ;
642656}
643657
658+ function expectEmbeddingTokenMetrics ( span : CapturedLogEvent | undefined ) {
659+ const metrics = span ?. metrics as Record < string , unknown > | undefined ;
660+ const totalTokens = metrics ?. tokens ;
661+ const promptTokens = metrics ?. prompt_tokens ;
662+
663+ const tokenMetric =
664+ typeof totalTokens === "number"
665+ ? totalTokens
666+ : typeof promptTokens === "number"
667+ ? promptTokens
668+ : undefined ;
669+
670+ expect ( tokenMetric ) . toEqual ( expect . any ( Number ) ) ;
671+ if ( typeof tokenMetric === "number" ) {
672+ expect ( tokenMetric ) . toBeGreaterThan ( 0 ) ;
673+ }
674+ }
675+
644676export function defineAISDKInstrumentationAssertions ( options : {
645677 agentSpanName ?: AgentSpanName ;
646678 name : string ;
@@ -754,6 +786,50 @@ export function defineAISDKInstrumentationAssertions(options: {
754786 }
755787 } ) ;
756788
789+ test ( "captures trace for embed()" , testConfig , ( ) => {
790+ const root = findLatestSpan ( events , ROOT_NAME ) ;
791+ const trace = findEmbedTrace ( events ) ;
792+
793+ expectOperationParentedByRoot ( trace . operation , root ) ;
794+ expectAISDKParentSpan ( trace . parent ) ;
795+ expect ( operationName ( trace . operation ) ) . toBe ( "embed" ) ;
796+ expectEmbeddingTokenMetrics ( trace . parent ) ;
797+ const input = isRecord ( trace . parent ?. input ) ? trace . parent . input : null ;
798+ expect ( typeof input ?. value ) . toBe ( "string" ) ;
799+ const output = extractOutputRecord ( trace . parent ) ;
800+ expect ( output ) . toBeDefined ( ) ;
801+ if ( output ) {
802+ expect ( output . embedding ) . toBeUndefined ( ) ;
803+ expect ( output . embedding_length ) . toEqual ( expect . any ( Number ) ) ;
804+ expect ( output . embedding_length ) . toBeGreaterThan ( 0 ) ;
805+ }
806+ } ) ;
807+
808+ test ( "captures trace for embedMany()" , testConfig , ( ) => {
809+ const root = findLatestSpan ( events , ROOT_NAME ) ;
810+ const trace = findEmbedManyTrace ( events ) ;
811+
812+ expectOperationParentedByRoot ( trace . operation , root ) ;
813+ expectAISDKParentSpan ( trace . parent ) ;
814+ expect ( operationName ( trace . operation ) ) . toBe ( "embed-many" ) ;
815+ expectEmbeddingTokenMetrics ( trace . parent ) ;
816+ const input = isRecord ( trace . parent ?. input ) ? trace . parent . input : null ;
817+ expect ( Array . isArray ( input ?. values ) ) . toBe ( true ) ;
818+ if ( Array . isArray ( input ?. values ) ) {
819+ expect ( input . values . length ) . toBeGreaterThanOrEqual ( 2 ) ;
820+ }
821+ const output = extractOutputRecord ( trace . parent ) ;
822+ expect ( output ) . toBeDefined ( ) ;
823+ if ( output ) {
824+ expect ( output . embeddings ) . toBeUndefined ( ) ;
825+ expect ( output . responses ) . toBeUndefined ( ) ;
826+ expect ( output . embedding_count ) . toEqual ( expect . any ( Number ) ) ;
827+ expect ( output . embedding_count ) . toBeGreaterThanOrEqual ( 2 ) ;
828+ expect ( output . embedding_length ) . toEqual ( expect . any ( Number ) ) ;
829+ expect ( output . embedding_length ) . toBeGreaterThan ( 0 ) ;
830+ }
831+ } ) ;
832+
757833 if ( options . supportsOutputObjectScenario ) {
758834 test (
759835 "captures Output.object schema on generateText()" ,
0 commit comments