Conversation
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
WalkthroughAdded support for capturing and leveraging OpenAI image response metadata to enable more accurate cost estimation. The changes introduce new types to represent image usage data, extend cost estimation interfaces to accept metadata, and implement logic to extract and propagate metadata through HTTP proxying and event logging. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Proxy
participant OpenAI as OpenAI API
participant Handler
participant CostEstimator
participant EventLog
Client->>Proxy: POST /images/generations
Proxy->>OpenAI: Forward request
OpenAI->>Proxy: JSON response with metadata
Proxy->>Proxy: Extract metadata (imageResponseMetadataFromBytes)
Proxy->>Proxy: Store in context (setCtxImageResponseMetadata)
Proxy->>Client: Return response
Proxy->>Handler: Process event with metadata
Handler->>CostEstimator: EstimateImagesCost(model, quality, resolution, metadata)
alt Metadata available
CostEstimator->>CostEstimator: estimateImageByMetadata (text + image tokens)
else Fallback
CostEstimator->>CostEstimator: Resolution-based pricing
end
CostEstimator->>Handler: Cost calculated
Handler->>EventLog: Store event with ImageResponseMetadata
EventLog->>EventLog: Log complete
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@internal/server/web/proxy/image.go`:
- Around line 126-143: The setter and getter use mismatched types:
setCtxImageResponseMetadata stores imageResponse.Usage (ImageResponseUsage) but
getCtxImageResponseMetadata type-asserts to openai.ImageResponseMetadata,
causing the assertion to fail; fix by storing the full metadata object in
setCtxImageResponseMetadata (store imageResponse itself) and update the getter's
type assertion to expect a *openai.ImageResponseMetadata (or the concrete value
you store) so getCtxImageResponseMetadata returns the stored metadata pointer
correctly.
🧹 Nitpick comments (1)
internal/provider/openai/cost.go (1)
472-506: Metadata-based cost estimation looks good, but silently ignores missing cost entries.The function correctly calculates costs from text tokens, image tokens, and output tokens. However, the
okreturn values from cost map lookups (lines 486, 494, 502) are ignored. If a model is inimageModelsWithTokensCostbut missing from one of the token cost maps, the cost for that component will silently be 0.Consider adding validation or logging when a model passes the initial check but is missing from a specific cost map.
♻️ Optional: Add logging for missing cost entries
textInputCost, _ := textInputCostMap[model] + // Note: If model is missing, textInputCost defaults to 0 totalCost += (float64(textInputTokens) / 1000) * textInputCostOr return an error if the lookup fails:
- textInputCost, _ := textInputCostMap[model] + textInputCost, ok := textInputCostMap[model] + if !ok { + return 0, fmt.Errorf("model %s not found in prompt cost map", model) + }
https://bugtracker.codiodev.com/issue/codio-17369/Upgrade-BricksLLM-to-Support-OpenAI-Image-Generation
Summary by CodeRabbit