parseChoiceToken extracts the branch index from a choice_N token and verifies it is within range.
(token string, branchCount int)
| 243 | // verifies it is within range. |
| 244 | func parseChoiceToken(token string, branchCount int) (int, error) { |
| 245 | const prefix = "choice_" |
| 246 | rest, ok := strings.CutPrefix(token, prefix) |
| 247 | if !ok { |
| 248 | return 0, fmt.Errorf("invalid choice token %q", token) |
| 249 | } |
| 250 | idx, err := strconv.Atoi(rest) |
| 251 | if err != nil || idx < 0 || idx >= branchCount { |
| 252 | return 0, fmt.Errorf("invalid choice token %q", token) |
| 253 | } |
| 254 | return idx, nil |
| 255 | } |
| 256 | |
| 257 | // Tools exposes this Agent as an LLM-callable tool. Following the llmproxy |
| 258 | // agent.AsTool convention, the tool accepts a single "prompt" string and |
| 259 | // returns the agent's final answer. |
no outgoing calls