Expose opt-in HTTP execution reports
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/promptkit"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
executionreport "gitea.maximumdirect.net/eric/scriptorium/internal/report"
|
||||
)
|
||||
|
||||
type Runner interface {
|
||||
@@ -111,7 +112,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
runContext, cancel = context.WithTimeout(runContext, time.Duration(*req.RunTimeoutSeconds)*time.Second)
|
||||
defer cancel()
|
||||
}
|
||||
res, err := h.runner.Run(runContext, promptkit.RunRequest{
|
||||
mappedRequest := promptkit.RunRequest{
|
||||
PromptID: req.PromptID,
|
||||
PromptVersion: req.PromptVersion,
|
||||
ProfileID: req.ProfileID,
|
||||
@@ -120,18 +121,28 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Vars: req.Vars,
|
||||
Execution: model,
|
||||
TransientRetries: req.TransientRetries,
|
||||
})
|
||||
}
|
||||
var collector *executionreport.Collector
|
||||
if req.IncludeExecutionReport {
|
||||
collector = executionreport.NewCollector()
|
||||
mappedRequest.Observer = collector.Observe
|
||||
}
|
||||
res, err := h.runner.Run(runContext, mappedRequest)
|
||||
var execution *executionreport.Execution
|
||||
if collector != nil {
|
||||
execution = collector.Snapshot()
|
||||
}
|
||||
if err != nil {
|
||||
switch runContext.Err() {
|
||||
case context.DeadlineExceeded:
|
||||
writeError(w, http.StatusGatewayTimeout, "deadline_exceeded", "run execution deadline exceeded")
|
||||
writeRunFailure(w, http.StatusGatewayTimeout, "deadline_exceeded", "run execution deadline exceeded", execution, h.options.MaxResponseBytes)
|
||||
return
|
||||
case context.Canceled:
|
||||
writeError(w, http.StatusRequestTimeout, "request_canceled", "run request canceled")
|
||||
writeRunFailure(w, http.StatusRequestTimeout, "request_canceled", "run request canceled", execution, h.options.MaxResponseBytes)
|
||||
return
|
||||
}
|
||||
status, code, message := mapRunError(err)
|
||||
writeError(w, status, code, message)
|
||||
writeRunFailure(w, status, code, message, execution, h.options.MaxResponseBytes)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -172,6 +183,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ValidationStatus: string(res.Validation.Status),
|
||||
RepairAttemptsUsed: res.Validation.RepairAttempts,
|
||||
},
|
||||
Execution: execution,
|
||||
}
|
||||
if req.IncludeRawOutput {
|
||||
raw := res.RawOutput
|
||||
@@ -180,6 +192,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
writeLimitedJSON(w, http.StatusOK, resp, h.options.MaxResponseBytes)
|
||||
}
|
||||
|
||||
func writeRunFailure(w http.ResponseWriter, status int, code, message string, execution *executionreport.Execution, maxBytes int64) {
|
||||
if execution == nil {
|
||||
writeError(w, status, code, message)
|
||||
return
|
||||
}
|
||||
writeLimitedJSON(w, status, errorResponse{Error: errorBody{Code: code, Message: message}, Execution: execution}, maxBytes)
|
||||
}
|
||||
|
||||
func executionTargetOverrideFromModelOverrideDTO(dto *modelOverrideRequestDTO) *promptkit.ExecutionTargetOverride {
|
||||
if dto == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user