c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
209 lines
6.1 KiB
Go
209 lines
6.1 KiB
Go
package stage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const ModelType = "model"
|
|
|
|
const ModelID = "deepseek-v4.1-flash"
|
|
|
|
var providerID = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`)
|
|
var modelID = regexp.MustCompile(`^[A-Za-z0-9_./:-]+$`)
|
|
|
|
func ValidModel(provider, model string) bool {
|
|
return len(provider) <= 200 && len(model) <= 300 && providerID.MatchString(provider) && modelID.MatchString(model)
|
|
}
|
|
|
|
// 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
|
|
Timeout time.Duration
|
|
BudgetUSD float64
|
|
BudgetTokens int64
|
|
Budget *Budget
|
|
ResultSchema map[string]any
|
|
}
|
|
|
|
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", "permissions":
|
|
default:
|
|
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
|
|
}
|
|
}
|
|
if _, _, err := ToolNames(def); err != nil {
|
|
return ModelSpec{}, err
|
|
}
|
|
if _, err := StagePermissions(def); err != nil {
|
|
return ModelSpec{}, err
|
|
}
|
|
for _, key := range []string{"provider", "model"} {
|
|
if def.Configs[key] == "" {
|
|
return ModelSpec{}, fmt.Errorf("missing config %s", key)
|
|
}
|
|
}
|
|
for key := range def.Configs {
|
|
switch key {
|
|
case "provider", "model", "retries", "timeout", "budget_usd", "budget_tokens":
|
|
default:
|
|
return ModelSpec{}, fmt.Errorf("unknown config %q", key)
|
|
}
|
|
}
|
|
if !ValidModel(def.Configs["provider"], def.Configs["model"]) {
|
|
return ModelSpec{}, fmt.Errorf("invalid provider/model identifier; use the exact IDs validated during setup")
|
|
}
|
|
retries := DefaultRetries
|
|
var err error
|
|
if value := def.Configs["retries"]; value != "" {
|
|
retries, err = strconv.Atoi(value)
|
|
}
|
|
if err != nil || retries < 0 || retries > 100 {
|
|
return ModelSpec{}, fmt.Errorf("retries must be an integer from 0 to 100")
|
|
}
|
|
timeout, err := StageTimeout(def)
|
|
if err != nil {
|
|
return ModelSpec{}, err
|
|
}
|
|
usd, tokens, err := parseBudget(def)
|
|
if err != nil {
|
|
return ModelSpec{}, err
|
|
}
|
|
return ModelSpec{
|
|
Output: def.Blocks["output"], Prompt: def.Blocks["prompt"],
|
|
Config: ModelConfig{Provider: def.Configs["provider"], Model: def.Configs["model"], Retries: retries, Timeout: timeout, BudgetUSD: usd, BudgetTokens: tokens},
|
|
}, 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
|
|
}
|
|
spec.Config.ResultSchema = resultSchema(def.Edges)
|
|
prompt, err := modelPrompt(def, spec, input)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
spec.Config.Budget = budgetFromContext(ctx)
|
|
if spec.Config.Budget == nil {
|
|
spec.Config.Budget = NewBudget(spec.Config.BudgetUSD, spec.Config.BudgetTokens)
|
|
}
|
|
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{}
|
|
}
|
|
if spec.Config.Budget.Enabled() {
|
|
capable, ok := client.(interface{ BudgetCapable() bool })
|
|
if !ok || !capable.BudgetCapable() {
|
|
return Result{}, fmt.Errorf("%w: generator cannot enforce usage limits", ErrBudgetAccounting)
|
|
}
|
|
}
|
|
// Replaying admitted tool work can repeat mutations. Only pre-admission
|
|
// transport errors may automatically retry an audited agent.
|
|
if agent, ok := client.(*AgentClient); ok {
|
|
if tools, explicit, _ := ToolNames(def); explicit {
|
|
agent.Tools = tools
|
|
}
|
|
agent.Policy, _ = StagePermissions(def)
|
|
}
|
|
for attempt := 0; attempt <= spec.Config.Retries; attempt++ {
|
|
if err := ctx.Err(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
if err := spec.Config.Budget.Check(); err != nil {
|
|
return Result{}, err
|
|
}
|
|
spec.Config.Budget.Attempt()
|
|
attemptCtx, cancel := context.WithTimeout(ctx, spec.Config.Timeout)
|
|
text, generateErr := client.Generate(attemptCtx, spec.Config, prompt)
|
|
if generateErr == nil && attemptCtx.Err() != nil {
|
|
generateErr = attemptCtx.Err()
|
|
}
|
|
if generateErr == nil {
|
|
result, resultErr := decodeModelResult(stageResultText(text), def.Edges)
|
|
if resultErr != nil {
|
|
result, resultErr = repairModelResult(attemptCtx, client, spec.Config, text, def.Edges, resultErr)
|
|
}
|
|
if attemptCtx.Err() != nil {
|
|
resultErr = errors.Join(resultErr, attemptCtx.Err())
|
|
}
|
|
cancel()
|
|
return result, resultErr
|
|
}
|
|
err = generateErr
|
|
if attemptCtx.Err() != nil && !errors.Is(err, ErrBudgetExceeded) && !errors.Is(err, ErrBudgetAccounting) {
|
|
err = errors.Join(err, attemptCtx.Err())
|
|
}
|
|
cancel()
|
|
if ctx.Err() != nil {
|
|
return Result{}, ctx.Err()
|
|
}
|
|
if errors.Is(err, ErrBudgetExceeded) || errors.Is(err, ErrBudgetAccounting) || errors.Is(err, ErrStageCleanup) {
|
|
break
|
|
}
|
|
if agent, ok := client.(*AgentClient); ok && agent.WorkStarted {
|
|
break
|
|
}
|
|
if attempt == spec.Config.Retries || (!retryableModelError(err) && !errors.Is(err, context.DeadlineExceeded)) {
|
|
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)
|
|
}
|