Architectural improvements in the http adapter

This commit is contained in:
2026-05-05 08:25:32 -05:00
parent c5ce270090
commit 281202e313
7 changed files with 81 additions and 40 deletions

View File

@@ -2,8 +2,6 @@ package httpadapter
import (
"time"
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
)
type runRequestDTO struct {
@@ -30,10 +28,10 @@ type modelOverrideRequestDTO struct {
}
type runResponseDTO struct {
Artifact artifactDTO `json:"artifact"`
Validation domain.ValidationResult `json:"validation"`
Metadata metadataDTO `json:"metadata"`
RawModelOutput string `json:"raw_model_output"`
Artifact artifactDTO `json:"artifact"`
Validation validationDTO `json:"validation"`
Metadata metadataDTO `json:"metadata"`
RawModelOutput string `json:"raw_model_output"`
}
type artifactDTO struct {
@@ -52,11 +50,26 @@ type metadataDTO struct {
Endpoint string `json:"endpoint"`
InputHashes map[string]string `json:"input_hashes"`
PromptHash string `json:"prompt_hash"`
Usage domain.TokenUsage `json:"usage"`
Usage tokenUsageDTO `json:"usage"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
type tokenUsageDTO struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
type validationDTO struct {
Status string `json:"status"`
Mode string `json:"mode"`
Errors []string `json:"errors,omitempty"`
SchemaPath string `json:"schema_path,omitempty"`
RepairAttempts int `json:"repair_attempts"`
IsValid bool `json:"is_valid"`
}
type errorResponse struct {
Error errorBody `json:"error"`
}

View File

@@ -93,7 +93,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Size: res.Artifact.Size,
Hash: res.Artifact.Hash,
},
Validation: res.Validation,
Validation: mapValidation(res.Validation),
Metadata: metadataDTO{
ProfileID: res.ProfileID,
ProfileVersion: res.ProfileVersion,
@@ -101,14 +101,29 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Endpoint: res.Endpoint,
InputHashes: res.InputHashes,
PromptHash: res.PromptHash,
Usage: res.Usage,
StartTime: res.StartTime,
EndTime: res.EndTime,
Usage: tokenUsageDTO{
PromptTokens: res.Usage.PromptTokens,
CompletionTokens: res.Usage.CompletionTokens,
TotalTokens: res.Usage.TotalTokens,
},
StartTime: res.StartTime,
EndTime: res.EndTime,
},
RawModelOutput: res.RawOutput,
})
}
func mapValidation(v domain.ValidationResult) validationDTO {
return validationDTO{
Status: string(v.Status),
Mode: string(v.Mode),
Errors: v.Errors,
SchemaPath: v.SchemaPath,
RepairAttempts: v.RepairAttempts,
IsValid: v.IsValid,
}
}
func mapRunError(err error) (int, string) {
switch {
case errors.Is(err, profile.ErrProfileNotFound):

View File

@@ -81,6 +81,15 @@ func TestHandlerPostRunsSuccess(t *testing.T) {
if artifact["body"] != "hello" {
t.Fatalf("expected artifact body hello, got %#v", artifact["body"])
}
validation := resp["validation"].(map[string]any)
if validation["status"] != "passed" {
t.Fatalf("expected validation.status passed, got %#v", validation["status"])
}
metadata := resp["metadata"].(map[string]any)
usage := metadata["usage"].(map[string]any)
if usage["total_tokens"] != float64(3) {
t.Fatalf("expected usage.total_tokens=3, got %#v", usage["total_tokens"])
}
if resp["raw_model_output"] != "hello" {
t.Fatalf("expected raw model output hello, got %#v", resp["raw_model_output"])
}
@@ -172,8 +181,8 @@ func TestHandlerValidationFailureStillSuccess(t *testing.T) {
t.Fatalf("invalid JSON response: %v", err)
}
validation := resp["validation"].(map[string]any)
if status, ok := validation["Status"].(string); !ok || status != "failed" {
t.Fatalf("expected validation Status=failed, got %#v", validation["Status"])
if status, ok := validation["status"].(string); !ok || status != "failed" {
t.Fatalf("expected validation status=failed, got %#v", validation["status"])
}
}