package runs import ( "context" "encoding/json/jsontext" json "encoding/json/v2" "errors" "fmt" "strings" "github.com/google/jsonschema-go/jsonschema" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/archie-core/modules/connectors" ) // resultTool is the name of the tool that accepts a structured result. const resultTool = "return_result" const resultRule = "Return your result by calling " + resultTool + " exactly once, when you have it." // resultSchema compiles the step's result schema. Tool parameters must be // a JSON object. func (s agentSettings) resultSchema() ([]byte, *jsonschema.Resolved, error) { // Tool schemas are part of the journaled model request digest. raw, err := json.Marshal(s.Result, json.Deterministic(true)) if err != nil { return nil, nil, fmt.Errorf("schema: %w", err) } var schema jsonschema.Schema if err := json.Unmarshal(raw, &schema); err != nil { return nil, nil, fmt.Errorf("schema: %w", err) } if schema.Type != "object" { return nil, nil, errors.New("a JSON Schema with type object") } resolved, err := schema.Resolve(nil) if err != nil { return nil, nil, fmt.Errorf("schema: %w", err) } return raw, resolved, nil } // resultFields are the fields the result schema declares, or nil when it // declares none, which accepts any field. func (s agentSettings) resultFields() map[string]bool { props, _ := s.Result["properties"].(map[string]any) if len(props) == 0 { return nil } fields := make(map[string]bool, len(props)) for name := range props { fields[name] = true } return fields } // system is the part of the agent's system prompt the step adds: its mission, // its extra rules and, with a result schema, how to return it. func (s agentSettings) system() string { parts := []string{s.Mission} if strings.TrimSpace(s.ExtraRules) != "" { parts = append(parts, s.ExtraRules) } if s.Result != nil { parts = append(parts, resultRule) } return strings.Join(parts, "\n\n") } // newResultTool returns validation errors to the agent so it can correct // its result. func newResultTool(raw []byte, schema *jsonschema.Resolved) *core.Tool { return &core.Tool{ Name: resultTool, Description: "Return the result of this step.", Parameters: raw, Execute: func(_ context.Context, input string) (string, error) { if _, err := decodeResult(jsontext.Value(input), schema); err != nil { return "", err } return "recorded", nil }, } } // decodeResult reads what the agent returned and checks it against the schema. func decodeResult(raw jsontext.Value, schema *jsonschema.Resolved) (map[string]any, error) { var out map[string]any if err := json.Unmarshal(raw, &out); err != nil { return nil, fmt.Errorf("the result is not a JSON object: %w", err) } if err := schema.Validate(out); err != nil { return nil, fmt.Errorf("the result doesn't match its schema: %w", err) } return out, nil } // readOnly filters tool declarations to read-only tools. func readOnly(decls []connectors.Declaration) []connectors.Declaration { out := make([]connectors.Declaration, 0, len(decls)) for _, d := range decls { if d.ReadOnly { out = append(out, d) } } return out }