89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
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,
|
|
},
|
|
}
|
|
|
|
var supportedKeys = []string{
|
|
SchemaBareSegments,
|
|
SchemaAuditaV1,
|
|
}
|
|
|
|
func SupportedKeys() []string {
|
|
out := make([]string, len(supportedKeys))
|
|
copy(out, supportedKeys)
|
|
return out
|
|
}
|
|
|
|
func IsSupported(key string) bool {
|
|
_, ok := definitions[strings.TrimSpace(key)]
|
|
return ok
|
|
}
|
|
|
|
func Resolve(key string) (Definition, error) {
|
|
normalized := strings.TrimSpace(key)
|
|
if normalized == "" {
|
|
return Definition{}, fmt.Errorf("output schema must not be empty")
|
|
}
|
|
if !IsSupported(normalized) {
|
|
return Definition{}, fmt.Errorf("unsupported output schema %q", normalized)
|
|
}
|
|
def := definitions[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, "", " ")
|
|
}
|