diff --git a/package-lock.json b/package-lock.json index ee56eef8f6..4b00be06d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24148,7 +24148,7 @@ }, "packages/botonic-plugin-ai-agents": { "name": "@botonic/plugin-ai-agents", - "version": "0.47.1", + "version": "0.47.2", "dependencies": { "@botonic/core": "^0.47.0", "@openai/agents": "^0.3.9", diff --git a/packages/botonic-plugin-ai-agents/CHANGELOG.md b/packages/botonic-plugin-ai-agents/CHANGELOG.md index bd8e6c86e0..6fdb77ec93 100644 --- a/packages/botonic-plugin-ai-agents/CHANGELOG.md +++ b/packages/botonic-plugin-ai-agents/CHANGELOG.md @@ -20,6 +20,10 @@ All notable changes to Botonic will be documented in this file. +## [0.47.2] - 2026-04-15 + +- [PR-3199](https://github.com/hubtype/botonic/pull/3199): Fix async calls to track LLM runs. + ## [0.47.1] - 2026-03-31 - [PR-3196](https://github.com/hubtype/botonic/pull/3196): Disable global sdk tracing. diff --git a/packages/botonic-plugin-ai-agents/package.json b/packages/botonic-plugin-ai-agents/package.json index 232b84ff5c..9d2e8e4fc0 100644 --- a/packages/botonic-plugin-ai-agents/package.json +++ b/packages/botonic-plugin-ai-agents/package.json @@ -1,6 +1,6 @@ { "name": "@botonic/plugin-ai-agents", - "version": "0.47.1", + "version": "0.47.2", "main": "./lib/cjs/index.js", "module": "./lib/esm/index.js", "description": "Use AI Agents to generate your contents", diff --git a/packages/botonic-plugin-ai-agents/src/guardrails/input.ts b/packages/botonic-plugin-ai-agents/src/guardrails/input.ts index 03a6a9b60d..a65c09f5d6 100644 --- a/packages/botonic-plugin-ai-agents/src/guardrails/input.ts +++ b/packages/botonic-plugin-ai-agents/src/guardrails/input.ts @@ -5,8 +5,8 @@ import { type UserMessageItem, } from '@openai/agents' import { z } from 'zod' -import { AZURE_OPENAI_API_VERSION, isProd, OPENAI_PROVIDER } from '../constants' -import type { LLMConfig } from '../llm-config' +import { isProd } from '../constants' +import { getApiVersion, type LLMConfig } from '../llm-config' import { HubtypeApiClient } from '../services/hubtype-api-client' import { TrackFeature, TrackProductName } from '../services/types' import type { GuardrailRule, ResultRawResponse } from '../types' @@ -53,7 +53,7 @@ export function createInputGuardrail( const result = await runner.run(agent, [lastMessage], { context }) const endTime = Date.now() - void sendGuardrailLlmRunTracking( + await sendGuardrailLlmRunTracking( result, trackingContext, llmConfig, @@ -97,7 +97,7 @@ async function sendGuardrailLlmRunTracking( const durationPerCall = Math.round(totalDuration / rawResponses.length) const temperature = (llmConfig.modelSettings.temperature as number | undefined) ?? 0 - const apiVersion = OPENAI_PROVIDER === 'azure' ? AZURE_OPENAI_API_VERSION : '' + const apiVersion = getApiVersion() const llmRuns = rawResponses.map(response => ({ inference_id: trackingContext.inferenceId, diff --git a/packages/botonic-plugin-ai-agents/src/llm-config.ts b/packages/botonic-plugin-ai-agents/src/llm-config.ts index 6b088d27f8..8cf4a91e7a 100644 --- a/packages/botonic-plugin-ai-agents/src/llm-config.ts +++ b/packages/botonic-plugin-ai-agents/src/llm-config.ts @@ -94,3 +94,11 @@ export class LLMConfig { throw new Error(`Unsupported model: ${model}`) } } + +export function getApiVersion(): string { + // Return NOT_API_VERSION_FOR_OPENAI_PROVIDER if OPENAI_PROVIDER + // is not azure to avoid error when tracking in llm_runs endpoint + return OPENAI_PROVIDER === 'azure' + ? AZURE_OPENAI_API_VERSION + : 'NOT_API_VERSION_FOR_OPENAI_PROVIDER' +} diff --git a/packages/botonic-plugin-ai-agents/src/runner.ts b/packages/botonic-plugin-ai-agents/src/runner.ts index 828bb7087c..5d43bb12a8 100644 --- a/packages/botonic-plugin-ai-agents/src/runner.ts +++ b/packages/botonic-plugin-ai-agents/src/runner.ts @@ -9,9 +9,9 @@ import { RunToolCallItem, RunToolCallOutputItem, } from '@openai/agents' -import { AZURE_OPENAI_API_VERSION, isProd, OPENAI_PROVIDER } from './constants' +import { isProd, OPENAI_PROVIDER } from './constants' import type { DebugLogger } from './debug-logger' -import type { LLMConfig } from './llm-config' +import { getApiVersion, type LLMConfig } from './llm-config' import { HubtypeApiClient } from './services/hubtype-api-client' import { TrackFeature, TrackProductName } from './services/types' import { retrieveKnowledge } from './tools' @@ -90,7 +90,7 @@ export class AIAgentRunner< const endTime = Date.now() - void this.sendLlmRunTracking(result, context, startTime, endTime) + await this.sendLlmRunTracking(result, context, startTime, endTime) const outputMessages = result.finalOutput?.messages || [] const hasExit = @@ -158,8 +158,7 @@ export class AIAgentRunner< const durationPerCall = Math.round(totalDuration / rawResponses.length) const temperature = (this.llmConfig.modelSettings.temperature as number | undefined) ?? 0 - const apiVersion = - OPENAI_PROVIDER === 'azure' ? AZURE_OPENAI_API_VERSION : '' + const apiVersion = getApiVersion() const llmRuns = rawResponses.map(response => ({ inference_id: this.inferenceId,