152 lines
4.1 KiB
Go
152 lines
4.1 KiB
Go
package stage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const ModelType = "model"
|
|
|
|
const ModelID = "deepseek-v4.1-flash"
|
|
|
|
// ModelConfig and ModelSpec group the settings/contracts that belong specifically
|
|
// to an LLM stage. Other stage types need not have a prompt, provider, or model.
|
|
type ModelConfig struct {
|
|
Provider string
|
|
Model string
|
|
Retries int
|
|
}
|
|
|
|
type ModelSpec struct {
|
|
Output string
|
|
Prompt string
|
|
Config ModelConfig
|
|
}
|
|
|
|
// ModelGenerator isolates the provider integration from stage instructions/results.
|
|
type ModelGenerator interface {
|
|
Generate(context.Context, ModelConfig, string) (string, error)
|
|
}
|
|
|
|
type ModelStage struct {
|
|
Client ModelGenerator // nil uses the locally authenticated OpenCode service
|
|
}
|
|
|
|
var _ Stage = ModelStage{}
|
|
|
|
func (m ModelStage) Validate(def Definition) error {
|
|
_, err := m.Spec(def)
|
|
return err
|
|
}
|
|
|
|
func (ModelStage) Spec(def Definition) (ModelSpec, error) {
|
|
if EffectiveType(def.Type) != ModelType {
|
|
return ModelSpec{}, fmt.Errorf("expected model stage, got %q", def.Type)
|
|
}
|
|
for _, block := range []string{"output", "prompt", "configs"} {
|
|
if strings.TrimSpace(def.Blocks[block]) == "" {
|
|
return ModelSpec{}, fmt.Errorf("required block %s is missing or empty", block)
|
|
}
|
|
}
|
|
for block := range def.Blocks {
|
|
switch block {
|
|
case "output", "prompt", "edges", "configs", "tools":
|
|
default:
|
|
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
|
|
}
|
|
}
|
|
if _, _, err := ToolNames(def); err != nil {
|
|
return ModelSpec{}, err
|
|
}
|
|
for _, key := range []string{"provider", "model", "retries"} {
|
|
if def.Configs[key] == "" {
|
|
return ModelSpec{}, fmt.Errorf("missing config %s", key)
|
|
}
|
|
}
|
|
for key := range def.Configs {
|
|
switch key {
|
|
case "provider", "model", "retries":
|
|
default:
|
|
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
|
|
}
|
|
}
|
|
if provider := def.Configs["provider"]; provider != "opencode-go" {
|
|
return ModelSpec{}, fmt.Errorf("unsupported provider %q", provider)
|
|
}
|
|
if model := def.Configs["model"]; model != ModelID {
|
|
return ModelSpec{}, fmt.Errorf("unsupported model %q", model)
|
|
}
|
|
retries, err := strconv.Atoi(def.Configs["retries"])
|
|
if err != nil || retries < 0 || retries > 100 {
|
|
return ModelSpec{}, fmt.Errorf("retries must be an integer from 0 to 100")
|
|
}
|
|
return ModelSpec{
|
|
Output: def.Blocks["output"], Prompt: def.Blocks["prompt"],
|
|
Config: ModelConfig{Provider: def.Configs["provider"], Model: def.Configs["model"], Retries: retries},
|
|
}, nil
|
|
}
|
|
|
|
func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Result, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
spec, err := m.Spec(def)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
prompt, err := modelPrompt(def, spec, input)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
|
defer cancel()
|
|
client := m.Client
|
|
if client == nil {
|
|
tools, _, _ := ToolNames(def)
|
|
if len(tools) > 0 {
|
|
return Result{}, fmt.Errorf("tool-enabled stages require an audited agent client")
|
|
}
|
|
client = OpenCodeClient{}
|
|
}
|
|
// Replaying an agent can repeat file mutations. Graph feedback edges, not
|
|
// transport/result retries, own additional tool-enabled attempts.
|
|
if agent, ok := client.(*AgentClient); ok {
|
|
if tools, explicit, _ := ToolNames(def); explicit {
|
|
agent.Tools = tools
|
|
}
|
|
spec.Config.Retries = 0
|
|
}
|
|
for attempt := 0; attempt <= spec.Config.Retries; attempt++ {
|
|
if err := ctx.Err(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
var text string
|
|
text, err = client.Generate(ctx, spec.Config, prompt)
|
|
if err == nil {
|
|
var result Result
|
|
result, err = decodeModelResult(text, def.Edges)
|
|
if err == nil {
|
|
return result, nil
|
|
}
|
|
}
|
|
if ctx.Err() != nil {
|
|
return Result{}, ctx.Err()
|
|
}
|
|
if attempt == spec.Config.Retries || !retryableModelError(err) {
|
|
break
|
|
}
|
|
delay := time.Duration(1<<min(attempt, 4)) * 100 * time.Millisecond
|
|
timer := time.NewTimer(delay)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return Result{}, ctx.Err()
|
|
case <-timer.C:
|
|
}
|
|
}
|
|
return Result{}, fmt.Errorf("model stage %s: %w", def.Key, err)
|
|
}
|