]> git.giacomolli.uk - onechat/blobdiff - chain.go
Exempt tracker metadata from commit signing
[onechat] / chain.go
index 59ce563524543d058a7809633451f09a867f02ef..c85b2a2b0b0bc8e809c04c13cda1689e02283124 100644 (file)
--- a/chain.go
+++ b/chain.go
@@ -22,10 +22,10 @@ type ChainClient interface {
 // answer. The replies, the tool calls, and the results go to the log of tools
 // as they happen. The thoughts never do.
 //
-// Each later request begins with the whole input of the request before it,
-// then its output items, then the tool results. The output items hold the
-// reasoning with its encrypted content, because the calls do not store it. So
-// each request begins with all of the previous one and reads it from the prompt
+// Each later request begins with the whole input of the request before it, then
+// its output items, then the tool results. The output items hold the reasoning
+// with its encrypted content, because the calls do not store it. So each
+// request begins with all of the previous one and reads it from the prompt
 // cache. The messages of take join each later request after the tool results.
 type Chain struct {
        client  ChainClient
@@ -43,8 +43,7 @@ type ChainFunctionCall struct {
        CallID    string `json:"call_id"`
 }
 
-// ChainFunctionCallOutput is the input item with the result of a function
-// call.
+// ChainFunctionCallOutput is the input item with the result of a function call.
 type ChainFunctionCallOutput struct {
        Type   string `json:"type"`
        CallID string `json:"call_id"`
@@ -52,8 +51,7 @@ type ChainFunctionCallOutput struct {
 }
 
 // Run runs the chain from input, the input of its first request, and returns
-// the text of the final answer. An error of a log write wraps
-// errChainLogWrite.
+// the text of the final answer. An error of a log write wraps errChainLogWrite.
 func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
        request := c.request
        for request.Turn = "first"; ; request.Turn = "next" {
@@ -67,7 +65,7 @@ func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
 
                input, text, err = c.handle(ctx, input, response)
                if err != nil {
-                       return "", err
+                       return "", fmt.Errorf("handle model response: %w", err)
                }
 
                if len(input) == 0 {
@@ -77,9 +75,9 @@ func (c *Chain) Run(ctx context.Context, input []any) (string, error) {
 }
 
 // handle logs the replies of response and runs its tool calls. It returns the
-// input of the next request: input, the output items, the tool results, and
-// the messages of take. After the final answer it returns no input, and the
-// replies of the answer as its text.
+// input of the next request: input, the output items, the tool results, and the
+// messages of take. After the final answer it returns no input, and the replies
+// of the answer as its text.
 func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse) ([]any, string, error) {
        var (
                outputs []any
@@ -89,7 +87,7 @@ func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse)
        for _, raw := range response.Output {
                item, err := chainInputItem(raw)
                if err != nil {
-                       return nil, "", err
+                       return nil, "", fmt.Errorf("decode chain input: %w", err)
                }
 
                input = append(input, item)
@@ -105,7 +103,7 @@ func (c *Chain) handle(ctx context.Context, input []any, response ModelResponse)
                case "message":
                        text, err := modelOutputText([]json.RawMessage{raw})
                        if err != nil {
-                               return nil, "", err
+                               return nil, "", fmt.Errorf("read response text: %w", err)
                        }
 
                        text = strings.TrimSpace(text)