Move execution reporting into the HTTP adapter
This commit is contained in:
104
internal/adapter/http/execution_report.go
Normal file
104
internal/adapter/http/execution_report.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package httpadapter
|
||||
|
||||
import "go.maximumdirect.net/promptkit"
|
||||
|
||||
// executionReport is the terminal observation exposed by the HTTP adapter.
|
||||
type executionReport struct {
|
||||
RunID string `json:"run_id,omitempty"`
|
||||
PromptID string `json:"prompt_id,omitempty"`
|
||||
ProfileID string `json:"profile_id,omitempty"`
|
||||
BackendID string `json:"backend_id,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Outcome string `json:"outcome"`
|
||||
Failure string `json:"failure"`
|
||||
StatusCode int `json:"status_code,omitempty"`
|
||||
RetriesExhausted bool `json:"retries_exhausted"`
|
||||
Attempts uint64 `json:"attempts"`
|
||||
TransientRetries uint64 `json:"transient_retries"`
|
||||
RepairCalls uint64 `json:"repair_calls"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
CapacityWaitMS int64 `json:"capacity_wait_ms"`
|
||||
GenerationDurationMS int64 `json:"generation_duration_ms"`
|
||||
RetryDelayMS int64 `json:"retry_delay_ms"`
|
||||
Usage executionUsage `json:"usage"`
|
||||
}
|
||||
|
||||
type executionUsage struct {
|
||||
PromptTokens executionTokenCount `json:"prompt_tokens"`
|
||||
CompletionTokens executionTokenCount `json:"completion_tokens"`
|
||||
TotalTokens executionTokenCount `json:"total_tokens"`
|
||||
CachedTokens executionTokenCount `json:"cached_tokens"`
|
||||
CacheWriteTokens executionTokenCount `json:"cache_write_tokens"`
|
||||
}
|
||||
|
||||
type executionTokenCount struct {
|
||||
Value int `json:"value"`
|
||||
Known bool `json:"known"`
|
||||
Complete bool `json:"complete"`
|
||||
}
|
||||
|
||||
// executionCollector retains at most one terminal observation from a synchronous run.
|
||||
type executionCollector struct {
|
||||
report *executionReport
|
||||
}
|
||||
|
||||
func newExecutionCollector() *executionCollector { return &executionCollector{} }
|
||||
|
||||
func (collector *executionCollector) Observe(event promptkit.ExecutionEvent) {
|
||||
if event.Kind != promptkit.ExecutionFinished {
|
||||
return
|
||||
}
|
||||
count := func(value promptkit.ObservedTokenCount) executionTokenCount {
|
||||
return executionTokenCount{Value: value.Value, Known: value.Known, Complete: value.Complete}
|
||||
}
|
||||
collector.report = &executionReport{
|
||||
RunID: event.RunID, PromptID: event.PromptID,
|
||||
ProfileID: event.ProfileID, BackendID: event.BackendID, Model: event.Model,
|
||||
Outcome: executionOutcome(event.Outcome), Failure: executionFailure(event.Failure),
|
||||
StatusCode: event.StatusCode, RetriesExhausted: event.RetriesExhausted,
|
||||
Attempts: event.Attempts, TransientRetries: event.TransientRetries, RepairCalls: event.RepairCalls,
|
||||
DurationMS: event.Duration.Milliseconds(), CapacityWaitMS: event.CapacityWait.Milliseconds(),
|
||||
GenerationDurationMS: event.GenerationDuration.Milliseconds(), RetryDelayMS: event.RetryDelay.Milliseconds(),
|
||||
Usage: executionUsage{
|
||||
PromptTokens: count(event.Usage.PromptTokens), CompletionTokens: count(event.Usage.CompletionTokens),
|
||||
TotalTokens: count(event.Usage.TotalTokens), CachedTokens: count(event.Usage.CachedTokens),
|
||||
CacheWriteTokens: count(event.Usage.CacheWriteTokens),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (collector *executionCollector) Snapshot() *executionReport {
|
||||
if collector.report == nil {
|
||||
return nil
|
||||
}
|
||||
value := *collector.report
|
||||
return &value
|
||||
}
|
||||
|
||||
func executionOutcome(value promptkit.ExecutionOutcome) string {
|
||||
switch value {
|
||||
case promptkit.ExecutionOutcomeSuccess, promptkit.ExecutionOutcomeInvalidOutput, promptkit.ExecutionOutcomeError:
|
||||
return string(value)
|
||||
default:
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
|
||||
func executionFailure(value promptkit.ExecutionFailureKind) string {
|
||||
switch value {
|
||||
case promptkit.ExecutionFailureNone:
|
||||
return "none"
|
||||
case promptkit.ExecutionFailureCanceled, promptkit.ExecutionFailureDeadline,
|
||||
promptkit.ExecutionFailureTransport, promptkit.ExecutionFailureProviderStatus,
|
||||
promptkit.ExecutionFailureMalformedResponse, promptkit.ExecutionFailureCapacity,
|
||||
promptkit.ExecutionFailureInvalidConfig, promptkit.ExecutionFailureInvalidRequest,
|
||||
promptkit.ExecutionFailurePromptNotFound, promptkit.ExecutionFailureProfileNotFound,
|
||||
promptkit.ExecutionFailurePromptLoad, promptkit.ExecutionFailureProfileLoad,
|
||||
promptkit.ExecutionFailureArtifactLoad, promptkit.ExecutionFailurePromptRender,
|
||||
promptkit.ExecutionFailureValidation, promptkit.ExecutionFailureGeneration,
|
||||
promptkit.ExecutionFailureOther:
|
||||
return string(value)
|
||||
default:
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user