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.
37 // Run runs the chain from input, the input of its first request, and returns
38 // the text of the final answer. An error of a log write wraps
40 func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
42 for request.Turn = "first"; ; request.Turn = "next" {
44 response, err := c.client.Create(ctx, request)
50 input, text, err = c.handle(ctx, input, response)
61 // handle logs the replies of response and runs its tool calls. It returns the
62 // input of the next request: input, the output items, the tool results, and
63 // the messages of take. After the final answer it returns no input, and the
64 // replies of the answer as its text.
65 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
68 for _, raw := range response.Output {
69 item, err := chainInputItem(raw)
74 input = append(input, item)
76 var call ChainFunctionCall
77 err = json.Unmarshal(raw, &call)
79 return nil, "", fmt.Errorf("decode output item: %w", err)
84 text, err := modelOutputText([]json.RawMessage{raw})
89 text = strings.TrimSpace(text)
94 _, err = c.tools.log.Append("reply", text)
96 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
99 replies = append(replies, text)
100 case "function_call":
101 output, err := c.tools.Call(ctx, call.Name, call.Arguments)
103 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
106 outputs = append(outputs, ChainFunctionCallOutput{
107 Type: "function_call_output",
114 if len(outputs) == 0 {
115 return nil, strings.Join(replies, "\n\n"), nil
118 input = append(input, outputs...)
119 for _, message := range c.take() {
120 input = append(input, modelUserMessage(message.Text))
123 return input, "", nil
126 // ChainFunctionCall holds the fields of a function call output item.
127 type ChainFunctionCall struct {
128 Type string `json:"type"`
129 Name string `json:"name"`
130 Arguments string `json:"arguments"`
131 CallID string `json:"call_id"`
134 // ChainFunctionCallOutput is the input item with the result of a function
136 type ChainFunctionCallOutput struct {
137 Type string `json:"type"`
138 CallID string `json:"call_id"`
139 Output string `json:"output"`
142 // chainInputItem returns an output item as an input item for the next request.
143 // The calls do not store their items, so the item goes back whole, without its
145 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
146 var item map[string]any
147 err := json.Unmarshal(raw, &item)
149 return nil, fmt.Errorf("decode output item: %w", err)