@@ -182,14 +182,12 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => {
182
182
continue ;
183
183
}
184
184
185
- if ( attributes [ AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE ] != undefined ) {
186
- attributes [ GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE ] = attributes [ AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE ] ;
187
- delete attributes [ AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE ] ;
188
- }
189
- if ( attributes [ AI_USAGE_PROMPT_TOKENS_ATTRIBUTE ] != undefined ) {
190
- attributes [ GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE ] = attributes [ AI_USAGE_PROMPT_TOKENS_ATTRIBUTE ] ;
191
- delete attributes [ AI_USAGE_PROMPT_TOKENS_ATTRIBUTE ] ;
192
- }
185
+ renameAttributeKey (
186
+ attributes ,
187
+ AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE ,
188
+ GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE ,
189
+ ) ;
190
+ renameAttributeKey ( attributes , AI_USAGE_PROMPT_TOKENS_ATTRIBUTE , GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE ) ;
193
191
if (
194
192
typeof attributes [ GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE ] === 'number' &&
195
193
typeof attributes [ GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE ] === 'number'
@@ -199,22 +197,10 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => {
199
197
}
200
198
201
199
// Rename AI SDK attributes to standardized gen_ai attributes
202
- if ( attributes [ AI_PROMPT_MESSAGES_ATTRIBUTE ] != undefined ) {
203
- attributes [ 'gen_ai.request.messages' ] = attributes [ AI_PROMPT_MESSAGES_ATTRIBUTE ] ;
204
- delete attributes [ AI_PROMPT_MESSAGES_ATTRIBUTE ] ;
205
- }
206
- if ( attributes [ AI_RESPONSE_TEXT_ATTRIBUTE ] != undefined ) {
207
- attributes [ 'gen_ai.response.text' ] = attributes [ AI_RESPONSE_TEXT_ATTRIBUTE ] ;
208
- delete attributes [ AI_RESPONSE_TEXT_ATTRIBUTE ] ;
209
- }
210
- if ( attributes [ AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] != undefined ) {
211
- attributes [ 'gen_ai.response.tool_calls' ] = attributes [ AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] ;
212
- delete attributes [ AI_RESPONSE_TOOL_CALLS_ATTRIBUTE ] ;
213
- }
214
- if ( attributes [ AI_PROMPT_TOOLS_ATTRIBUTE ] != undefined ) {
215
- attributes [ 'gen_ai.request.available_tools' ] = attributes [ AI_PROMPT_TOOLS_ATTRIBUTE ] ;
216
- delete attributes [ AI_PROMPT_TOOLS_ATTRIBUTE ] ;
217
- }
200
+ renameAttributeKey ( attributes , AI_PROMPT_MESSAGES_ATTRIBUTE , 'gen_ai.request.messages' ) ;
201
+ renameAttributeKey ( attributes , AI_RESPONSE_TEXT_ATTRIBUTE , 'gen_ai.response.text' ) ;
202
+ renameAttributeKey ( attributes , AI_RESPONSE_TOOL_CALLS_ATTRIBUTE , 'gen_ai.response.tool_calls' ) ;
203
+ renameAttributeKey ( attributes , AI_PROMPT_TOOLS_ATTRIBUTE , 'gen_ai.request.available_tools' ) ;
218
204
}
219
205
}
220
206
@@ -274,3 +260,14 @@ const _vercelAIIntegration = ((options: VercelAiOptions = {}) => {
274
260
* });
275
261
*/
276
262
export const vercelAIIntegration = defineIntegration ( _vercelAIIntegration ) ;
263
+
264
+ /**
265
+ * Renames an attribute key in the provided attributes object if the old key exists.
266
+ * This function safely handles null and undefined values.
267
+ */
268
+ function renameAttributeKey ( attributes : Record < string , unknown > , oldKey : string , newKey : string ) : void {
269
+ if ( attributes [ oldKey ] != null ) {
270
+ attributes [ newKey ] = attributes [ oldKey ] ;
271
+ delete attributes [ oldKey ] ;
272
+ }
273
+ }
0 commit comments