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, then
26 // its output items, then the tool results. The output items hold the reasoning
27 // with its encrypted content, because the calls do not store it. So each
28 // 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 call.
47 type ChainFunctionCallOutput struct {
48 Type string `json:"type"`
49 CallID string `json:"call_id"`
50 Output any `json:"output"`
53 // Run runs the chain from input, the input of its first request, and returns
54 // the text of the final answer. An error of a log write wraps errChainLogWrite.
55 func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
57 for request.Turn = "first"; ; request.Turn = "next" {
59 response, err := c.client.Create(ctx, request)
61 return "", fmt.Errorf("create model response: %w", err)
66 input, text, err = c.handle(ctx, input, response)
68 return "", fmt.Errorf("handle model response: %w", err)
77 // handle logs the replies of response and runs its tool calls. It returns the
78 // input of the next request: input, the output items, the tool results, and the
79 // messages of take. After the final answer it returns no input, and the replies
80 // of the answer as its text.
81 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
87 for _, raw := range response.Output {
88 item, err := chainInputItem(raw)
90 return nil, "", fmt.Errorf("decode chain input: %w", err)
93 input = append(input, item)
95 var call ChainFunctionCall
97 err = json.Unmarshal(raw, &call)
99 return nil, "", fmt.Errorf("decode output item: %w", err)
104 text, err := modelOutputText([]json.RawMessage{raw})
106 return nil, "", fmt.Errorf("read response text: %w", err)
109 text = strings.TrimSpace(text)
114 _, err = c.tools.log.Append("reply", text)
116 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
119 replies = append(replies, text)
120 case "function_call":
121 output, err := c.tools.Call(ctx, call.Name, call.Arguments)
123 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
126 outputs = append(outputs, ChainFunctionCallOutput{
127 Type: "function_call_output",
134 if len(outputs) == 0 {
135 return nil, strings.Join(replies, "\n\n"), nil
138 input = append(input, outputs...)
139 for _, message := range c.take() {
140 item, err := imageUserMessage(c.images, message.Text)
142 return nil, "", fmt.Errorf("read queued images: %w", err)
145 input = append(input, item)
148 return input, "", nil
151 // chainInputItem returns an output item as an input item for the next request.
152 // The calls do not store their items, so the item goes back whole, without its
154 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
155 var item map[string]any
157 err := json.Unmarshal(raw, &item)
159 return nil, fmt.Errorf("decode output item: %w", err)