generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 593
feat(adk): adk implementation #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0584f28
feat: adk implementation
luohq-bytedance b1c83cb
fix: Runner run with new runctx
luohq-bytedance 78b8546
feat: agent as tool (#293)
meguminnnnnnnnn a82fd16
feat: the input will not be passed when calling subagent because the …
meguminnnnnnnnn d41f1e5
feat: add Agent CallOption for adk (#312)
hi-pender a8d7cfd
feat: prebuilt/supervisor (#329)
luohq-bytedance 8c8d7c7
fix: disallow to parrent for sub agents (#336)
hi-pender f890d8b
St/adk1 (#339)
shentongmartin dd77822
revert(adk): check tool_call until stream end (#346)
hi-pender 61006e7
fix: only set automatic close on events added to session and emitted …
shentongmartin 1e68920
Feat/wdz/adk interrupt (#311)
meguminnnnnnnnn b812e69
fix(adk): reserve stream event (#378)
meguminnnnnnnnn 460a3a1
feat(adk): optimize pass event (#379)
meguminnnnnnnnn e93b9e3
fix(adk): use uuid as mock transfer tool call id (#388)
meguminnnnnnnnn 08186c5
feat: adk -- implement plan_execute_replan (#386)
luohq-bytedance b9d01ea
feat: support skip transfer messages (#399)
meguminnnnnnnnn f05729f
feat(adk): history entry support user input (#404)
meguminnnnnnnnn d8e87d7
fix: resume chatmodel agent panic (#414)
meguminnnnnnnnn 6db8425
fix: encode agent event wrapper & input message in history rewrite (#…
meguminnnnnnnnn d92a8f6
fix: history rewrite user input correctly (#419)
meguminnnnnnnnn 19a3221
fix: gob will discord tool call index(*int), so concat message stream…
meguminnnnnnnnn 469121d
fix: plan_execute: improve plan instruction (#427)
luohq-bytedance 6b30855
refactor: change run path from string to RunStep (#423)
shentongmartin d0c3988
feat(adk): support WithSessionValues agent run option (#428)
meguminnnnnnnnn b9643dd
feat: enable history rewriter in root agent & report run ctx nil in r…
meguminnnnnnnnn 1e8fd57
fix(adk_deterministic_transfer): support deterministic transfer inter…
meguminnnnnnnnn bebf0ff
feat(adk): add GenInputFn for planner/executor/replanner (#420)
hi-pender b830cae
Feat/workflow inherit (#439)
shentongmartin 70f76d7
refactor: add sub directory for prebuilt agent and add comments (#443)
hi-pender 525d4e3
feat: use MaxIterations instead of MaxSteps for ChatModelAgent (#447)
hi-pender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| /* | ||
| * Copyright 2025 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 adk | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/bytedance/sonic" | ||
|
|
||
| "github.com/cloudwego/eino/components/tool" | ||
| "github.com/cloudwego/eino/compose" | ||
| "github.com/cloudwego/eino/schema" | ||
| ) | ||
|
|
||
| var ( | ||
| defaultAgentToolParam = schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{ | ||
| "request": { | ||
| Desc: "request to be processed", | ||
| Required: true, | ||
| Type: schema.String, | ||
| }, | ||
| }) | ||
| ) | ||
|
|
||
| type agentToolOptions struct { | ||
| agentName string | ||
| opts []AgentRunOption | ||
| } | ||
|
|
||
| func withAgentToolOptions(agentName string, opts []AgentRunOption) tool.Option { | ||
| return tool.WrapImplSpecificOptFn(func(opt *agentToolOptions) { | ||
| opt.agentName = agentName | ||
| opt.opts = opts | ||
| }) | ||
| } | ||
|
|
||
| func getOptionsByAgentName(agentName string, opts []tool.Option) []AgentRunOption { | ||
| var ret []AgentRunOption | ||
| for _, opt := range opts { | ||
| o := tool.GetImplSpecificOptions[agentToolOptions](nil, opt) | ||
| if o != nil && o.agentName == agentName { | ||
| ret = append(ret, o.opts...) | ||
| } | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| type agentTool struct { | ||
| agent Agent | ||
|
|
||
| fullChatHistoryAsInput bool | ||
| } | ||
|
|
||
| func (at *agentTool) Info(ctx context.Context) (*schema.ToolInfo, error) { | ||
| var param *schema.ParamsOneOf | ||
| if !at.fullChatHistoryAsInput { | ||
| param = defaultAgentToolParam | ||
| } | ||
|
|
||
| return &schema.ToolInfo{ | ||
| Name: at.agent.Name(ctx), | ||
| Desc: at.agent.Description(ctx), | ||
| ParamsOneOf: param, | ||
| }, nil | ||
| } | ||
|
|
||
| func (at *agentTool) InvokableRun(ctx context.Context, argumentsInJSON string, opts ...tool.Option) (string, error) { | ||
| var intData *agentToolInterruptInfo | ||
| var bResume bool | ||
| err := compose.ProcessState(ctx, func(ctx context.Context, s *State) error { | ||
| toolCallID := compose.GetToolCallID(ctx) | ||
| intData, bResume = s.AgentToolInterruptData[toolCallID] | ||
| if bResume { | ||
| delete(s.AgentToolInterruptData, toolCallID) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| // cannot resume | ||
| bResume = false | ||
| } | ||
|
|
||
| var ms *mockStore | ||
| var iter *AsyncIterator[*AgentEvent] | ||
| if bResume { | ||
| ms = newResumeStore(intData.Data) | ||
|
|
||
| iter, err = newInvokableAgentToolRunner(at.agent, ms).Resume(ctx, mockCheckPointID, getOptionsByAgentName(at.agent.Name(ctx), opts)...) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| } else { | ||
| ms = newEmptyStore() | ||
| var input []Message | ||
| if at.fullChatHistoryAsInput { | ||
| history, err := getReactChatHistory(ctx, at.agent.Name(ctx)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| input = history | ||
| } else { | ||
| type request struct { | ||
| Request string `json:"request"` | ||
| } | ||
|
|
||
| req := &request{} | ||
| err := sonic.UnmarshalString(argumentsInJSON, req) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| input = []Message{ | ||
| schema.UserMessage(req.Request), | ||
| } | ||
| } | ||
|
|
||
| iter = newInvokableAgentToolRunner(at.agent, ms).Run(ctx, input, append(getOptionsByAgentName(at.agent.Name(ctx), opts), WithCheckPointID(mockCheckPointID))...) | ||
| } | ||
|
|
||
| var lastEvent *AgentEvent | ||
| for { | ||
| event, ok := iter.Next() | ||
| if !ok { | ||
| break | ||
| } | ||
|
|
||
| if event.Err != nil { | ||
| return "", event.Err | ||
| } | ||
|
|
||
| lastEvent = event | ||
| } | ||
|
|
||
| if lastEvent != nil && lastEvent.Action != nil && lastEvent.Action.Interrupted != nil { | ||
| data, existed, err_ := ms.Get(ctx, mockCheckPointID) | ||
| if err_ != nil { | ||
| return "", fmt.Errorf("failed to get interrupt info: %w", err_) | ||
| } | ||
| if !existed { | ||
| return "", fmt.Errorf("interrupt has happened, but cannot find interrupt info") | ||
| } | ||
| err = compose.ProcessState(ctx, func(ctx context.Context, st *State) error { | ||
| st.AgentToolInterruptData[compose.GetToolCallID(ctx)] = &agentToolInterruptInfo{ | ||
| LastEvent: lastEvent, | ||
| Data: data, | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to save agent tool checkpoint to state: %w", err) | ||
| } | ||
| return "", compose.InterruptAndRerun | ||
| } | ||
|
|
||
| if lastEvent == nil { | ||
| return "", errors.New("no event returned") | ||
| } | ||
|
|
||
| var ret string | ||
| if lastEvent.Output != nil { | ||
| if output := lastEvent.Output.MessageOutput; output != nil { | ||
| if !output.IsStreaming { | ||
| ret = output.Message.Content | ||
| } else { | ||
| msg, err := schema.ConcatMessageStream(output.MessageStream) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| ret = msg.Content | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ret, nil | ||
| } | ||
|
|
||
| type AgentToolOptions struct { | ||
| fullChatHistoryAsInput bool | ||
| } | ||
|
|
||
| type AgentToolOption func(*AgentToolOptions) | ||
|
|
||
| func WithFullChatHistoryAsInput() AgentToolOption { | ||
| return func(options *AgentToolOptions) { | ||
| options.fullChatHistoryAsInput = true | ||
| } | ||
| } | ||
|
|
||
| func getReactChatHistory(ctx context.Context, destAgentName string) ([]Message, error) { | ||
| var messages []Message | ||
| var agentName string | ||
| err := compose.ProcessState(ctx, func(ctx context.Context, st *State) error { | ||
| messages = st.Messages | ||
| agentName = st.AgentName | ||
| return nil | ||
| }) | ||
|
|
||
| messages = messages[:len(messages)-1] // remove the last assistant message, which is the tool call message | ||
| history := make([]Message, 0, len(messages)) | ||
| history = append(history, messages...) | ||
| a, t := GenTransferMessages(ctx, destAgentName) | ||
| history = append(history, a, t) | ||
| for _, msg := range messages { | ||
| if msg.Role == schema.System { | ||
| continue | ||
| } | ||
|
|
||
| if msg.Role == schema.Assistant || msg.Role == schema.Tool { | ||
| msg = rewriteMessage(msg, agentName) | ||
| } | ||
|
|
||
| history = append(history, msg) | ||
| } | ||
|
|
||
| return history, err | ||
| } | ||
|
|
||
| func NewAgentTool(_ context.Context, agent Agent, options ...AgentToolOption) tool.BaseTool { | ||
| opts := &AgentToolOptions{} | ||
| for _, opt := range options { | ||
| opt(opts) | ||
| } | ||
|
|
||
| return &agentTool{agent: agent, fullChatHistoryAsInput: opts.fullChatHistoryAsInput} | ||
| } | ||
|
|
||
| func newInvokableAgentToolRunner(agent Agent, store compose.CheckPointStore) *Runner { | ||
| return &Runner{ | ||
| a: agent, | ||
| enableStreaming: false, | ||
| store: store, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.