11 // errChainLogWrite marks an error of a log write, which ends the chain.
12 var errChainLogWrite = errors.New("log write failed")
14 // ChainClient sends model calls.
15 type ChainClient interface {
16 Create(ctx context.Context, request ModelRequest) (ModelResponse, error)
19 // Chain is one request chain of a turn or of a subagent. It sends request with
20 // the input of the first request, runs the tool calls of each response, and
21 // sends their results back, until a response has no tool call: the final
22 // answer. The replies, the tool calls, and the results go to the log of tools
23 // as they happen. The thoughts never do.
25 // Each later request begins with the whole input of the request before it,
26 // then its output items, then the tool results. The output items hold the
27 // reasoning with its encrypted content, because the calls do not store it. So
28 // each request begins with all of the previous one and reads it from the prompt
29 // cache. The messages of take join each later request after the tool results.
38 // ChainFunctionCall holds the fields of a function call output item.
39 type ChainFunctionCall struct {
40 Type string `json:"type"`
41 Name string `json:"name"`
42 Arguments string `json:"arguments"`
43 CallID string `json:"call_id"`
46 // ChainFunctionCallOutput is the input item with the result of a function
48 type ChainFunctionCallOutput struct {
49 Type string `json:"type"`
50 CallID string `json:"call_id"`
51 Output any `json:"output"`
54 // Run runs the chain from input, the input of its first request, and returns
55 // the text of the final answer. An error of a log write wraps
57 func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
59 for request.Turn = "first"; ; request.Turn = "next" {
61 response, err := c.client.Create(ctx, request)
63 return "", fmt.Errorf("create model response: %w", err)
68 input, text, err = c.handle(ctx, input, response)
79 // handle logs the replies of response and runs its tool calls. It returns the
80 // input of the next request: input, the output items, the tool results, and
81 // the messages of take. After the final answer it returns no input, and the
82 // replies of the answer as its text.
83 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
89 for _, raw := range response.Output {
90 item, err := chainInputItem(raw)
95 input = append(input, item)
97 var call ChainFunctionCall
99 err = json.Unmarshal(raw, &call)
101 return nil, "", fmt.Errorf("decode output item: %w", err)
106 text, err := modelOutputText([]json.RawMessage{raw})
111 text = strings.TrimSpace(text)
116 _, err = c.tools.log.Append("reply", text)
118 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
121 replies = append(replies, text)
122 case "function_call":
123 output, err := c.tools.Call(ctx, call.Name, call.Arguments)
125 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
128 outputs = append(outputs, ChainFunctionCallOutput{
129 Type: "function_call_output",
136 if len(outputs) == 0 {
137 return nil, strings.Join(replies, "\n\n"), nil
140 input = append(input, outputs...)
141 for _, message := range c.take() {
142 item, err := imageUserMessage(c.images, message.Text)
144 return nil, "", fmt.Errorf("read queued images: %w", err)
147 input = append(input, item)
150 return input, "", nil
153 // chainInputItem returns an output item as an input item for the next request.
154 // The calls do not store their items, so the item goes back whole, without its
156 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
157 var item map[string]any
159 err := json.Unmarshal(raw, &item)
161 return nil, fmt.Errorf("decode output item: %w", err)