fix(ai-connectors): return actual usage tokens in OpenAiCompatibleAdapter and aggregate telemetry across all logs

オープン
#51 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
56/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
postgresql, typescript

調査の方向性

Start with packages/ai-connectors/src/adapters/openai-compatible.adapter.ts and its executeCompletion method, then read AdapterExecuteResult in base.ts to confirm the token fields. Next inspect getTelemetry and dispatchCompletion in apps/api/src/ai-connectors/ai-connectors.service.ts, along with the Prisma aggregation APIs used by this project. Done means provider token counts are preserved and telemetry totals cover all matching logs while recent failovers remain limited to 10.

索引モデルが issue の本文から書いたものです。

説明

backend bug good first issue
Problem Description
  1. Actual token usage is discarded for 8 of 9 AI providers:
    In packages/ai-connectors/src/adapters/openai-compatible.adapter.ts (executeCompletion, lines 108-112), the adapter only returns { text, latencyMs, tokensUsed } and omits inputTokens (data?.usage?.prompt_tokens) and outputTokens (data?.usage?.completion_tokens), even though AdapterExecuteResult (base.ts, lines 24-25) defines both fields.
    As a result, AiConnectorsService.dispatchCompletion (apps/api/src/ai-connectors/ai-connectors.service.ts, lines 645-646) falls back to chars / 4 estimates for groq, openai, xai, deepseek, gemini, openrouter, mistral, perplexity, and custom_* connectors.

  2. Telemetry totals are truncated to the latest 100 rows:
    In apps/api/src/ai-connectors/ai-connectors.service.ts (getTelemetry, lines 328-397), findMany({ where: { companyId }, take: 100 }) loads only the most recent 100 AiExecutionLog rows and computes totalInboundTokens, totalOutboundTokens, totalEstimatedCost, totalRequests, and providerStats from that 100-row slice. At 1k-10k+ users, totals cap out and fluctuate as older requests drop out of the window.


Expected Behavior
  1. OpenAiCompatibleAdapter.executeCompletion returns inputTokens and outputTokens from data?.usage (falling back to chars / 4 only when a provider omits usage).
  2. getTelemetry computes workspace totals and per-provider breakdowns using database aggregations across all matching logs (not take: 100), while keeping a small take: 10 query only for recentFailovers.

Where to Look
  • packages/ai-connectors/src/adapters/openai-compatible.adapter.ts (executeCompletion, lines 108-112)
  • apps/api/src/ai-connectors/ai-connectors.service.ts (getTelemetry lines 328-397, dispatchCompletion lines 645-646)

Solution / Implementation Guide
  1. Return actual usage tokens (openai-compatible.adapter.ts):
    • Extract const inputTokens = data?.usage?.prompt_tokens; and const outputTokens = data?.usage?.completion_tokens; and include them in the returned AdapterExecuteResult.
  2. Aggregate telemetry in PostgreSQL (ai-connectors.service.ts):
    • Replace the take: 100 in-memory loop in getTelemetry with prisma.aiExecutionLog.aggregate (for totalInboundTokens, totalOutboundTokens, totalEstimatedCost, totalLatency, totalRequests) and prisma.aiExecutionLog.groupBy({ by: ['providerKey', 'status'], ... }) for providerStats.
    • Fetch recentFailovers with a separate take: 10 query filtered to wasFallback: true or status: 'FAILED'.
主要言語
TypeScript
スター
2
フォーク
1
平均マージ
6分
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sumamakhan761/tryinstance のほかの issue

sumamakhan761/tryinstance の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。