-
Notifications
You must be signed in to change notification settings - Fork 56
allow user format mcp content #733
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
Open
xiehuc
wants to merge
1
commit into
trpc-group:main
Choose a base branch
from
xiehuc:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+26
−13
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -542,6 +542,15 @@ func (p *FunctionCallResponseProcessor) buildMergedParallelEvent( | |
| return mergedEvent | ||
| } | ||
|
|
||
| func toolJsonContent(ctx context.Context, toolName string, toolDeclaration *tool.Declaration, jsonArgs []byte, id string, result any) (tool.Message, error) { | ||
| body, err := json.Marshal(result) | ||
| return model.Message{ | ||
| Role: model.RoleTool, | ||
| Content: string(body), | ||
| ToolID: id, | ||
| }, err | ||
| } | ||
|
|
||
| // executeToolCall executes a single tool call and returns the choice. | ||
| // Parameters: | ||
| // - ctx: context for cancellation and tracing | ||
|
|
@@ -602,27 +611,24 @@ func (p *FunctionCallResponseProcessor) executeToolCall( | |
| } | ||
| } | ||
|
|
||
| // Marshal the result to JSON. | ||
| resultBytes, err := json.Marshal(result) | ||
| format := toolJsonContent | ||
| if p.toolCallbacks.ToolFormatMessage != nil { | ||
| format = p.toolCallbacks.ToolFormatMessage | ||
| } | ||
|
|
||
| msg, err := format(ctx, toolCall.Function.Name, tl.Declaration(), toolCall.Function.Arguments, toolCall.ID, result) | ||
| if err != nil { | ||
| // Marshal failures (for example, NaN in floats) do not | ||
| // affect the overall flow. Downgrade to warning to avoid | ||
| // noisy alerts while still surfacing the issue. | ||
| log.Warnf("Failed to marshal tool result for %s: %v", | ||
| log.Warnf("Failed to format tool result for %s: %v", | ||
| toolCall.Function.Name, err) | ||
| return nil, modifiedArgs, true, | ||
| fmt.Errorf("%s: %w", ErrorMarshalResult, err) | ||
| } | ||
|
|
||
| log.Debugf("CallableTool %s executed successfully, result: %s", toolCall.Function.Name, string(resultBytes)) | ||
| log.Debugf("CallableTool %s executed successfully, result: %s %v", toolCall.Function.Name, msg) | ||
|
|
||
| return &model.Choice{ | ||
| Index: index, | ||
| Message: model.Message{ | ||
| Role: model.RoleTool, | ||
| Content: string(resultBytes), | ||
| ToolID: toolCall.ID, | ||
| }, | ||
| Index: index, | ||
| Message: msg.(model.Message), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果业务实现 callback 不是规范的 model.Message,这里可能会 panic。 |
||
| }, modifiedArgs, true, nil | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -26,12 +26,19 @@ | |
| // - error: if not nil, this error will be returned. | ||
| type AfterToolCallback func(ctx context.Context, toolName string, toolDeclaration *Declaration, jsonArgs []byte, result any, runErr error) (any, error) | ||
|
|
||
| type Message any // should be model.Message, avoid cycle import | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个导出字段需要加一下注释 // Message ... |
||
|
|
||
| // ToolFormatMessage is called to format the tool result. | ||
| type ToolFormatMessage func(ctx context.Context, toolName string, toolDeclaration *Declaration, jsonArgs []byte, id string, result any) (Message, error) | ||
|
|
||
| // Callbacks holds callbacks for tool operations. | ||
| type Callbacks struct { | ||
| // BeforeTool is a list of callbacks that are called before the tool is executed. | ||
| BeforeTool []BeforeToolCallback | ||
| // AfterTool is a list of callbacks that are called after the tool is executed. | ||
| AfterTool []AfterToolCallback | ||
|
|
||
| ToolFormatMessage ToolFormatMessage | ||
| } | ||
|
|
||
| // NewCallbacks creates a new Callbacks instance for tool. | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p.toolCallbacks 可能是 nil,得判断一下