fix: finalize growth cost controls

This commit is contained in:
2026-08-21 00:56:59 +03:30
parent bb4a85cd65
commit 3e2d1f32c8
7 changed files with 86 additions and 26 deletions

View File

@@ -80,6 +80,8 @@ type responseEnvelope struct {
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
} `json:"usage"`
EstimatedCost json.RawMessage `json:"estimated_cost"`
ProviderRequestID string `json:"-"`
}
type outputItem struct {
@@ -109,6 +111,8 @@ func (c *AvalAIClient) workflow(ctx context.Context, base map[string]any, eviden
}
}
usage := Usage{InputTokens: first.Usage.InputTokens, OutputTokens: first.Usage.OutputTokens, LatencyMS: latency}
usage.ProviderRequestID = first.ProviderRequestID
usage.EstimatedCost = costString(first.EstimatedCost)
final := first
if len(calls) > 0 {
if executor == nil {
@@ -144,6 +148,12 @@ func (c *AvalAIClient) workflow(ctx context.Context, base map[string]any, eviden
usage.InputTokens += second.Usage.InputTokens
usage.OutputTokens += second.Usage.OutputTokens
usage.LatencyMS += secondLatency
if second.ProviderRequestID != "" {
usage.ProviderRequestID = second.ProviderRequestID
}
if cost := costString(second.EstimatedCost); cost != "" {
usage.EstimatedCost = cost
}
final = second
for _, raw := range second.Output {
var item outputItem
@@ -195,9 +205,33 @@ func (c *AvalAIClient) do(parent context.Context, bodyValue map[string]any) (res
if result.Status != "completed" {
return responseEnvelope{}, 0, false, fmt.Errorf("avalai: incomplete response status %q", result.Status)
}
result.ProviderRequestID = resp.Header.Get("X-Request-ID")
return result, time.Since(started).Milliseconds(), false, nil
}
func costString(raw json.RawMessage) string {
if len(raw) == 0 {
return ""
}
var text string
if json.Unmarshal(raw, &text) == nil {
if len(text) <= 64 {
return text
}
return ""
}
text = strings.TrimSpace(string(raw))
if len(text) > 64 {
return ""
}
for _, r := range text {
if !(unicode.IsDigit(r) || r == '.' || r == '-' || r == 'e' || r == 'E') {
return ""
}
}
return text
}
func parseAICopy(result responseEnvelope, known map[string]struct{}) (AICopy, error) {
var text string
for _, raw := range result.Output {