Add output schema registry and public contract docs
This commit is contained in:
72
internal/core/outputschema/registry.go
Normal file
72
internal/core/outputschema/registry.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package outputschema
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/internal/core/schema"
|
||||
)
|
||||
|
||||
const (
|
||||
SchemaBareSegments = "bare-segments"
|
||||
SchemaAuditaV1 = "audita-v1"
|
||||
)
|
||||
|
||||
type Encoder func(*schema.Transcript) ([]byte, error)
|
||||
|
||||
type Definition struct {
|
||||
Key string
|
||||
Encoder Encoder
|
||||
}
|
||||
|
||||
var definitions = map[string]Definition{
|
||||
SchemaBareSegments: {
|
||||
Key: SchemaBareSegments,
|
||||
Encoder: schema.TranscriptToJSON,
|
||||
},
|
||||
SchemaAuditaV1: {
|
||||
Key: SchemaAuditaV1,
|
||||
Encoder: encodeAuditaV1,
|
||||
},
|
||||
}
|
||||
|
||||
func Resolve(key string) (Definition, error) {
|
||||
normalized := strings.TrimSpace(key)
|
||||
if normalized == "" {
|
||||
return Definition{}, fmt.Errorf("output schema must not be empty")
|
||||
}
|
||||
def, ok := definitions[normalized]
|
||||
if !ok {
|
||||
return Definition{}, fmt.Errorf("unsupported output schema %q", normalized)
|
||||
}
|
||||
return def, nil
|
||||
}
|
||||
|
||||
func encodeAuditaV1(transcript *schema.Transcript) ([]byte, error) {
|
||||
if transcript == nil {
|
||||
transcript = &schema.Transcript{}
|
||||
}
|
||||
payload := map[string]any{
|
||||
"schema": "audita-v1",
|
||||
"version": "v1",
|
||||
"segments": func() []map[string]any {
|
||||
out := make([]map[string]any, len(transcript.Segments))
|
||||
for i, s := range transcript.Segments {
|
||||
item := map[string]any{
|
||||
"id": s.ID,
|
||||
"speaker": s.Speaker,
|
||||
"start": s.Start,
|
||||
"end": s.End,
|
||||
"text": s.Text,
|
||||
}
|
||||
if len(s.Categories) > 0 {
|
||||
item["categories"] = s.Categories
|
||||
}
|
||||
out[i] = item
|
||||
}
|
||||
return out
|
||||
}(),
|
||||
}
|
||||
return json.MarshalIndent(payload, "", " ")
|
||||
}
|
||||
Reference in New Issue
Block a user