Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions adk/intro/http-sse-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.24.9
replace github.com/cloudwego/eino-examples => ../../..

require (
github.com/cloudwego/eino v0.7.14
github.com/cloudwego/eino v0.9.0-alpha.2
github.com/cloudwego/eino-examples v0.0.0-00010101000000-000000000000
github.com/cloudwego/hertz v0.10.3
github.com/hertz-contrib/sse v0.1.0
Expand Down Expand Up @@ -51,7 +51,7 @@ require (
github.com/tidwall/sjson v1.2.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/volcengine/volc-sdk-golang v1.0.199 // indirect
github.com/volcengine/volcengine-go-sdk v1.1.44 // indirect
github.com/volcengine/volcengine-go-sdk v1.2.4 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
golang.org/x/arch v0.19.0 // indirect
Expand Down
15 changes: 6 additions & 9 deletions adk/intro/http-sse-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ github.com/bugsnag/panicwrap v1.2.0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywR
github.com/bytedance/gopkg v0.1.1/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
github.com/bytedance/mockey v1.2.14 h1:KZaFgPdiUwW+jOWFieo3Lr7INM1P+6adO3hxZhDswY8=
github.com/bytedance/mockey v1.2.14/go.mod h1:1BPHF9sol5R1ud/+0VEHGQq/+i2lN+GTsr3O2Q9IENY=
github.com/bytedance/mockey v1.4.1 h1:M61HE/wajTkOHPKgBUKUUViaRoaMok26/ZqOSyq7ZtQ=
github.com/bytedance/mockey v1.4.1/go.mod h1:1BPHF9sol5R1ud/+0VEHGQq/+i2lN+GTsr3O2Q9IENY=
github.com/bytedance/sonic v1.14.2 h1:k1twIoe97C1DtYUo+fZQy865IuHia4PR5RPiuGPPIIE=
github.com/bytedance/sonic v1.14.2/go.mod h1:T80iDELeHiHKSc0C9tubFygiuXoGzrkjKzX2quAx980=
github.com/bytedance/sonic/loader v0.4.0 h1:olZ7lEqcxtZygCK9EKYKADnpQoYkRQxaeY2NYzevs+o=
Expand All @@ -104,11 +104,8 @@ github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5P
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
github.com/cloudwego/eino v0.7.8 h1:3a2j1UKZZuQ3SzqDToOI5g6lrlJ7xZEtMlNQkTgIvaI=
github.com/cloudwego/eino v0.7.8/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
github.com/cloudwego/eino v0.7.11/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
github.com/cloudwego/eino v0.7.14-0.20251225084034-ff42791c540f/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
github.com/cloudwego/eino v0.7.14/go.mod h1:nA8Vacmuqv3pqKBQbTWENBLQ8MmGmPt/WqiyLeB8ohQ=
github.com/cloudwego/eino v0.9.0-alpha.2 h1:YdNHQ1XqPnAe1EEy3Hm5oDzOnoPwizHCjaSdnF7iHjw=
github.com/cloudwego/eino v0.9.0-alpha.2/go.mod h1:2gsj+xyPFnmZfz7jtnbA6VghaaJsThYh2appzPeslRE=
github.com/cloudwego/eino-ext/components/model/ark v0.1.45 h1:LWvSHJVlvS1S/IxN9XUKNw/MI0I7YPePt3LMNxyCrZ0=
github.com/cloudwego/eino-ext/components/model/ark v0.1.45/go.mod h1:e8P5dGVI/JMQ1FYNgmu5EFRWA8fivBc6NwNJ9g8FBK8=
github.com/cloudwego/eino-ext/components/model/openai v0.1.5 h1:+yvGbTPw93li9GSmdm6Rix88Yy8AXg5NNBcRbWx3CQU=
Expand Down Expand Up @@ -515,8 +512,8 @@ github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/X
github.com/volcengine/volc-sdk-golang v1.0.23/go.mod h1:AfG/PZRUkHJ9inETvbjNifTDgut25Wbkm2QoYBTbvyU=
github.com/volcengine/volc-sdk-golang v1.0.199 h1:zv9QOqTl/IsLwtfC37GlJtcz6vMAHi+pjq8ILWjLYUc=
github.com/volcengine/volc-sdk-golang v1.0.199/go.mod h1:stZX+EPgv1vF4nZwOlEe8iGcriUPRBKX8zA19gXycOQ=
github.com/volcengine/volcengine-go-sdk v1.1.44 h1:WLoLlzt67ZlJeow55PPx65/Mh52DewVXqkHcFSodM9w=
github.com/volcengine/volcengine-go-sdk v1.1.44/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
github.com/volcengine/volcengine-go-sdk v1.2.4 h1:smBdDwwkXoXldCuumuZDJQASlAgVUIeL/RQ26D0OgI4=
github.com/volcengine/volcengine-go-sdk v1.2.4/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
Expand Down
179 changes: 179 additions & 0 deletions flow/agent/react/agentic/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2026 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"errors"
"fmt"
"io"
"strings"

"github.com/bytedance/sonic"
"github.com/cloudwego/eino-examples/internal/logs"
"github.com/cloudwego/eino-ext/components/model/agenticark"
"github.com/cloudwego/eino/callbacks"
"github.com/cloudwego/eino/components/model"
"github.com/cloudwego/eino/schema"
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model/responses"
)

func newAgenticModelCallback() *agenticModelCallback {
return &agenticModelCallback{}
}

type agenticModelCallback struct{}

func (a *agenticModelCallback) OnEndWithStreamOutput(ctx context.Context, runInfo *callbacks.RunInfo,
output *schema.StreamReader[*model.AgenticCallbackOutput]) context.Context {

printHeader(fmt.Sprintf("AgenticModel Name: %s", runInfo.Name), "\033[36m")

go func() {
var (
lastBlockType schema.ContentBlockType
lineLength int
)

for {
chunk, err := output.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
logs.Errorf("read streaming output failed, err: %v", err)
return
}

if chunk.Message == nil {
continue
}

for _, block := range chunk.Message.ContentBlocks {
switch block.Type {
case schema.ContentBlockTypeReasoning:
if lastBlockType != block.Type {
printHeader("reasoning", "\033[33m")
lineLength = 0
}

lineLength = printWrapped(block.Reasoning.Text, lineLength)

lastBlockType = block.Type

case schema.ContentBlockTypeFunctionToolCall:
if lastBlockType != block.Type {
printHeader(fmt.Sprintf("call function tool: %s", block.FunctionToolCall.Name), "\033[35m")
lineLength = 0
}

lineLength = printWrapped(block.FunctionToolCall.Arguments, lineLength)

lastBlockType = block.Type

case schema.ContentBlockTypeServerToolCall:
if lastBlockType != block.Type {
printHeader(fmt.Sprintf("call server tool: %s", block.ServerToolCall.Name), "\033[35m")
lineLength = 0
}

status, ok := agenticark.GetItemStatus(block)
if ok && status != responses.ItemStatus_completed.String() {
fmt.Print(status + "...\n\n")
}
if block.ServerToolCall.Arguments != nil {
fmt.Print(responses.ItemStatus_completed.String() + "!\n\n")
args, _ := sonic.MarshalIndent(block.ServerToolCall.Arguments, "", " ")
fmt.Println(string(args))
lineLength = 0
}

lastBlockType = block.Type

case schema.ContentBlockTypeServerToolResult:
if lastBlockType != block.Type {
printHeader(fmt.Sprintf("server tool result: %s", block.ServerToolResult.Name), "\033[32m")
lineLength = 0
}
if block.ServerToolResult.Result != nil {
res, _ := sonic.MarshalIndent(block.ServerToolResult.Result, "", " ")
fmt.Println(string(res))
lineLength = 0
}

lastBlockType = block.Type

case schema.ContentBlockTypeAssistantGenText:
if lastBlockType != block.Type {
printHeader("assistant generated text", "\033[36m")
lineLength = 0
}

lineLength = printWrapped(block.AssistantGenText.Text, lineLength)

lastBlockType = block.Type
}
}
}
}()

return ctx
}

func printWrapped(content string, currentLength int) int {
for _, r := range content {
if r == '\n' {
currentLength = 0
fmt.Print(string(r))
continue
}

w := 1
if r > 127 {
w = 2
}

if currentLength+w > 120 {
fmt.Print("\n")
currentLength = 0
}

fmt.Print(string(r))
currentLength += w
}
return currentLength
}

func printHeader(content string, color string) {
const lineLength = 60
separator := strings.Repeat("=", lineLength)

contentLength := len(content)
if contentLength >= lineLength {
// Content too long, just print it within separators
fmt.Printf("\n\n%s%s\n%s\n%s\033[0m\n\n", color, separator, content, separator)
return
}

padding := lineLength - contentLength
leftPad := padding / 2

// Create padding string with spaces
padStr := strings.Repeat(" ", leftPad)

fmt.Printf("\n\n%s%s\n%s%s\n%s\033[0m\n\n", color, separator, padStr, content, separator)
}
130 changes: 130 additions & 0 deletions flow/agent/react/agentic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2026 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"fmt"
"io"
"log"
"os"

"github.com/bytedance/sonic"
"github.com/cloudwego/eino-ext/components/model/agenticark"
"github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/schema"
"github.com/cloudwego/eino/utils/callbacks"
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model/responses"
)

func main() {
ctx := context.Background()

am, err := agenticark.New(ctx, &agenticark.Config{
Model: os.Getenv("ARK_MODEL_ID"),
APIKey: os.Getenv("ARK_API_KEY"),
Thinking: &responses.ResponsesThinking{
Type: responses.ThinkingType_enabled.Enum(),
},
ServerTools: []*agenticark.ServerToolConfig{
{
WebSearch: &responses.ToolWebSearch{
Type: responses.ToolType_web_search,
},
},
},
Reasoning: &responses.ResponsesReasoning{
Effort: responses.ReasoningEffort_low,
},
})
if err != nil {
log.Fatalf("failed to create agentic model, err=%v", err)
}

r, err := NewAgent(ctx, &AgentConfig{
Model: am,
ToolsConfig: compose.ToolsNodeConfig{
Tools: []tool.BaseTool{
&SummarizeNewsTool{},
&GetUserLocationTool{},
},
},
ToolReturnDirectly: map[string]struct{}{
summarizeNewsToolName: {},
},
})
if err != nil {
log.Fatalf("failed to create agent, err=%v", err)
}

input := []*schema.AgenticMessage{
schema.SystemAgenticMessage("You are a news assistant that helps users search for recent news. " +
"Before using the `summarize_news` tool, " +
"You MUST use the `get_user_location` tool to get the user's country." +
"You MUST use the `summarize_news` tool to summarize the news and return the result."),
schema.UserAgenticMessage("What news has been happening in the last three days?"),
}

cb := callbacks.NewHandlerHelper().AgenticModel(&callbacks.AgenticModelCallbackHandler{
OnEndWithStreamOutput: newAgenticModelCallback().OnEndWithStreamOutput,
}).Handler()

sr, err := r.Stream(ctx, input, WithComposeOptions(compose.WithCallbacks(cb)))
if err != nil {
log.Fatalf("failed to stream, err=%v", err)
}

var msgs []*schema.AgenticMessage
for {
msg, err := sr.Recv()
if err != nil {
if err == io.EOF {
break
}
log.Fatalf("failed to recv, err=%v", err)
}
msgs = append(msgs, msg)
}

msg, err := schema.ConcatAgenticMessages(msgs)
if err != nil {
log.Fatalf("failed to concat messages, err=%v", err)
}

printHeader("final output", "\033[36m")

for _, block := range msg.ContentBlocks {
switch block.Type {
case schema.ContentBlockTypeFunctionToolResult:
var res *SummarizeNewsToolInput
if err = sonic.UnmarshalString(block.FunctionToolResult.Result, &res); err != nil {
log.Fatalf("failed to unmarshal function tool result, err=%v", err)
}

b, err := sonic.MarshalIndent(res.News, "", " ")
if err != nil {
log.Fatalf("failed to marshal function tool result, err=%v", err)
}

fmt.Println(string(b))

default:
fmt.Println(fmt.Sprintf("%s", block))
}
}
}
Loading