Add weighted random workflow steps and a random choice agent tool
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package agents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"g.fc.run/ai/byre/internal/mcp"
|
||||
"g.fc.run/ai/byre/internal/workflow"
|
||||
)
|
||||
|
||||
func (m *Manager) randomChoiceTool() mcp.Tool {
|
||||
return mcp.Tool{
|
||||
Name: "random_choice",
|
||||
Description: "Pick one of several choices at random, each with its weight as the chance of being picked, and get back the one picked. " +
|
||||
"When the weights add up to less than 1, the rest is the chance that none is picked, which comes back as \"No match\". " +
|
||||
"Use it whenever you are told to choose by chance, rather than choosing yourself.",
|
||||
InputSchema: json.RawMessage(`{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"choices": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"choice": {"type": "string", "description": "What this choice is, returned as it is when it is picked."},
|
||||
"weight": {"type": "number", "minimum": 0, "maximum": 1, "description": "Its chance of being picked, from 0 to 1. The weights of all the choices add up to at most 1."}
|
||||
},
|
||||
"required": ["choice", "weight"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["choices"]
|
||||
}`),
|
||||
Rich: func(ctx context.Context, raw json.RawMessage) mcp.Result {
|
||||
args := struct {
|
||||
Choices []struct {
|
||||
Choice string `json:"choice"`
|
||||
Weight float64 `json:"weight"`
|
||||
} `json:"choices"`
|
||||
}{}
|
||||
err := json.Unmarshal(raw, &args)
|
||||
if err != nil {
|
||||
return mcp.TextResult(fmt.Sprintf("malformed random_choice call: %v", err), true)
|
||||
}
|
||||
if len(args.Choices) == 0 {
|
||||
return mcp.TextResult("choices is empty", true)
|
||||
}
|
||||
weights := []float64{}
|
||||
for _, c := range args.Choices {
|
||||
weights = append(weights, c.Weight)
|
||||
}
|
||||
p := workflow.ChoiceWeightsProblem(weights)
|
||||
if p != "" {
|
||||
return mcp.TextResult(p, true)
|
||||
}
|
||||
i := workflow.Pick(weights, 1)
|
||||
if i < 0 {
|
||||
return mcp.TextResult("No match: none of the choices was picked.", false)
|
||||
}
|
||||
return mcp.TextResult(args.Choices[i].Choice, false)
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user