|
| 1 | +/* |
| 2 | + * Copyright 2026 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/bytedance/sonic" |
| 27 | + "github.com/cloudwego/eino-examples/internal/logs" |
| 28 | + "github.com/cloudwego/eino-ext/components/model/agenticark" |
| 29 | + "github.com/cloudwego/eino/callbacks" |
| 30 | + "github.com/cloudwego/eino/components/model" |
| 31 | + "github.com/cloudwego/eino/schema" |
| 32 | + "github.com/volcengine/volcengine-go-sdk/service/arkruntime/model/responses" |
| 33 | +) |
| 34 | + |
| 35 | +func newAgenticModelCallback() *agenticModelCallback { |
| 36 | + return &agenticModelCallback{} |
| 37 | +} |
| 38 | + |
| 39 | +type agenticModelCallback struct{} |
| 40 | + |
| 41 | +func (a *agenticModelCallback) OnEndWithStreamOutput(ctx context.Context, runInfo *callbacks.RunInfo, |
| 42 | + output *schema.StreamReader[*model.AgenticCallbackOutput]) context.Context { |
| 43 | + |
| 44 | + printHeader(fmt.Sprintf("AgenticModel Name: %s", runInfo.Name), "\033[36m") |
| 45 | + |
| 46 | + go func() { |
| 47 | + var ( |
| 48 | + lastBlockType schema.ContentBlockType |
| 49 | + lineLength int |
| 50 | + ) |
| 51 | + |
| 52 | + for { |
| 53 | + chunk, err := output.Recv() |
| 54 | + if err != nil { |
| 55 | + if errors.Is(err, io.EOF) { |
| 56 | + break |
| 57 | + } |
| 58 | + logs.Errorf("read streaming output failed, err: %v", err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if chunk.Message == nil { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + for _, block := range chunk.Message.ContentBlocks { |
| 67 | + switch block.Type { |
| 68 | + case schema.ContentBlockTypeReasoning: |
| 69 | + if lastBlockType != block.Type { |
| 70 | + printHeader("reasoning", "\033[33m") |
| 71 | + lineLength = 0 |
| 72 | + } |
| 73 | + |
| 74 | + lineLength = printWrapped(block.Reasoning.Text, lineLength) |
| 75 | + |
| 76 | + lastBlockType = block.Type |
| 77 | + |
| 78 | + case schema.ContentBlockTypeFunctionToolCall: |
| 79 | + if lastBlockType != block.Type { |
| 80 | + printHeader(fmt.Sprintf("call function tool: %s", block.FunctionToolCall.Name), "\033[35m") |
| 81 | + lineLength = 0 |
| 82 | + } |
| 83 | + |
| 84 | + lineLength = printWrapped(block.FunctionToolCall.Arguments, lineLength) |
| 85 | + |
| 86 | + lastBlockType = block.Type |
| 87 | + |
| 88 | + case schema.ContentBlockTypeServerToolCall: |
| 89 | + if lastBlockType != block.Type { |
| 90 | + printHeader(fmt.Sprintf("call server tool: %s", block.ServerToolCall.Name), "\033[35m") |
| 91 | + lineLength = 0 |
| 92 | + } |
| 93 | + |
| 94 | + status, ok := agenticark.GetItemStatus(block) |
| 95 | + if ok && status != responses.ItemStatus_completed.String() { |
| 96 | + fmt.Print(status + "...\n\n") |
| 97 | + } |
| 98 | + if block.ServerToolCall.Arguments != nil { |
| 99 | + fmt.Print(responses.ItemStatus_completed.String() + "!\n\n") |
| 100 | + args, _ := sonic.MarshalIndent(block.ServerToolCall.Arguments, "", " ") |
| 101 | + fmt.Println(string(args)) |
| 102 | + lineLength = 0 |
| 103 | + } |
| 104 | + |
| 105 | + lastBlockType = block.Type |
| 106 | + |
| 107 | + case schema.ContentBlockTypeServerToolResult: |
| 108 | + if lastBlockType != block.Type { |
| 109 | + printHeader(fmt.Sprintf("server tool result: %s", block.ServerToolResult.Name), "\033[32m") |
| 110 | + lineLength = 0 |
| 111 | + } |
| 112 | + if block.ServerToolResult.Result != nil { |
| 113 | + res, _ := sonic.MarshalIndent(block.ServerToolResult.Result, "", " ") |
| 114 | + fmt.Println(string(res)) |
| 115 | + lineLength = 0 |
| 116 | + } |
| 117 | + |
| 118 | + lastBlockType = block.Type |
| 119 | + |
| 120 | + case schema.ContentBlockTypeAssistantGenText: |
| 121 | + if lastBlockType != block.Type { |
| 122 | + printHeader("assistant generated text", "\033[36m") |
| 123 | + lineLength = 0 |
| 124 | + } |
| 125 | + |
| 126 | + lineLength = printWrapped(block.AssistantGenText.Text, lineLength) |
| 127 | + |
| 128 | + lastBlockType = block.Type |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }() |
| 133 | + |
| 134 | + return ctx |
| 135 | +} |
| 136 | + |
| 137 | +func printWrapped(content string, currentLength int) int { |
| 138 | + for _, r := range content { |
| 139 | + if r == '\n' { |
| 140 | + currentLength = 0 |
| 141 | + fmt.Print(string(r)) |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + w := 1 |
| 146 | + if r > 127 { |
| 147 | + w = 2 |
| 148 | + } |
| 149 | + |
| 150 | + if currentLength+w > 120 { |
| 151 | + fmt.Print("\n") |
| 152 | + currentLength = 0 |
| 153 | + } |
| 154 | + |
| 155 | + fmt.Print(string(r)) |
| 156 | + currentLength += w |
| 157 | + } |
| 158 | + return currentLength |
| 159 | +} |
| 160 | + |
| 161 | +func printHeader(content string, color string) { |
| 162 | + const lineLength = 60 |
| 163 | + separator := strings.Repeat("=", lineLength) |
| 164 | + |
| 165 | + contentLength := len(content) |
| 166 | + if contentLength >= lineLength { |
| 167 | + // Content too long, just print it within separators |
| 168 | + fmt.Printf("\n\n%s%s\n%s\n%s\033[0m\n\n", color, separator, content, separator) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + padding := lineLength - contentLength |
| 173 | + leftPad := padding / 2 |
| 174 | + |
| 175 | + // Create padding string with spaces |
| 176 | + padStr := strings.Repeat(" ", leftPad) |
| 177 | + |
| 178 | + fmt.Printf("\n\n%s%s\n%s%s\n%s\033[0m\n\n", color, separator, padStr, content, separator) |
| 179 | +} |
0 commit comments