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)
46 return "", fmt.Errorf("create model response: %w", err)
51 input, text, err = c.handle(ctx, input, response)
62 // handle logs the replies of response and runs its tool calls. It returns the
63 // input of the next request: input, the output items, the tool results, and
64 // the messages of take. After the final answer it returns no input, and the
65 // replies of the answer as its text.
66 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
72 for _, raw := range response.Output {
73 item, err := chainInputItem(raw)
78 input = append(input, item)
80 var call ChainFunctionCall
82 err = json.Unmarshal(raw, &call)
84 return nil, "", fmt.Errorf("decode output item: %w", err)
89 text, err := modelOutputText([]json.RawMessage{raw})
94 text = strings.TrimSpace(text)
99 _, err = c.tools.log.Append("reply", text)
101 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
104 replies = append(replies, text)
105 case "function_call":
106 output, err := c.tools.Call(ctx, call.Name, call.Arguments)
108 return nil, "", fmt.Errorf("%w: %w", errChainLogWrite, err)
111 outputs = append(outputs, ChainFunctionCallOutput{
112 Type: "function_call_output",
119 if len(outputs) == 0 {
120 return nil, strings.Join(replies, "\n\n"), nil
123 input = append(input, outputs...)
124 for _, message := range c.take() {
125 input = append(input, modelUserMessage(message.Text))
128 return input, "", nil
131 // ChainFunctionCall holds the fields of a function call output item.
132 type ChainFunctionCall struct {
133 Type string `json:"type"`
134 Name string `json:"name"`
135 Arguments string `json:"arguments"`
136 CallID string `json:"call_id"`
139 // ChainFunctionCallOutput is the input item with the result of a function
141 type ChainFunctionCallOutput struct {
142 Type string `json:"type"`
143 CallID string `json:"call_id"`
144 Output string `json:"output"`
147 // chainInputItem returns an output item as an input item for the next request.
148 // The calls do not store their items, so the item goes back whole, without its
150 func chainInputItem(raw json.RawMessage) (map[string]any, error) {
151 var item map[string]any
153 err := json.Unmarshal(raw, &item)
155 return nil, fmt.Errorf("decode output item: %w", err)