76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
|
|
package preflight
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
|
||
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
||
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/normalization"
|
||
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
||
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/contracts"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Plan holds the local processing decisions for one invocation.
|
||
|
|
type Plan struct {
|
||
|
|
Config *config.Config
|
||
|
|
Specs []contracts.ModuleRunSpec
|
||
|
|
Transcript *schema.Transcript
|
||
|
|
Summary *normalization.NormalizationSummary
|
||
|
|
Sections []chunking.Section
|
||
|
|
Profiles []string
|
||
|
|
}
|
||
|
|
|
||
|
|
// BuildPlan resolves processing selections, normalizes the input, and fixes its chunks.
|
||
|
|
func BuildPlan(ctx context.Context, cfg *config.Config, source *schema.SourceTranscript, inspect func(context.Context, string) error) (*Plan, error) {
|
||
|
|
if cfg == nil {
|
||
|
|
return nil, fmt.Errorf("application configuration is required")
|
||
|
|
}
|
||
|
|
if source == nil {
|
||
|
|
return nil, fmt.Errorf("transcript is required")
|
||
|
|
}
|
||
|
|
var profiles []string
|
||
|
|
var inspectAndCollect func(context.Context, string) error
|
||
|
|
if inspect != nil {
|
||
|
|
inspectAndCollect = func(ctx context.Context, id string) error {
|
||
|
|
if err := inspect(ctx, id); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
profiles = append(profiles, id)
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
specs, err := ProcessingPlan(ctx, cfg, inspectAndCollect)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
normalizer := normalization.NewNormalizer(normalization.NormalizationConfig{
|
||
|
|
MaxSegmentGap: cfg.Normalization.MaxSegmentGap, EllipsisGap: cfg.Normalization.EllipsisGap,
|
||
|
|
MaxSegmentDuration: cfg.Normalization.MaxSegmentDuration, MaxSegmentTokens: cfg.Normalization.MaxSegmentTokens,
|
||
|
|
})
|
||
|
|
transcript, summary := normalizer.Normalize(source.Canonical())
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return nil, fmt.Errorf("normalize transcript: %w", err)
|
||
|
|
}
|
||
|
|
chunker := chunking.NewChunker(chunking.ChunkingConfig{
|
||
|
|
MaxSectionTokens: cfg.Chunking.MaxSectionTokens, MinSectionTokens: cfg.Chunking.MinSectionTokens,
|
||
|
|
TargetSections: cfg.Chunking.TargetSections,
|
||
|
|
})
|
||
|
|
sections, err := chunker.ChunkTranscript(transcript)
|
||
|
|
if err != nil {
|
||
|
|
var infeasible *chunking.InfeasibleTargetError
|
||
|
|
if errors.As(err, &infeasible) {
|
||
|
|
return nil, &SelectionError{Cause: fmt.Errorf("chunk normalized transcript: %w", err)}
|
||
|
|
}
|
||
|
|
return nil, fmt.Errorf("chunk normalized transcript: %w", err)
|
||
|
|
}
|
||
|
|
if err := ctx.Err(); err != nil {
|
||
|
|
return nil, fmt.Errorf("chunk normalized transcript: %w", err)
|
||
|
|
}
|
||
|
|
return &Plan{Config: cfg, Specs: specs, Transcript: transcript, Summary: summary, Sections: sections, Profiles: profiles}, nil
|
||
|
|
}
|