Implement metadata merging in context for sub-agent hooks

- Add a new `MergeMetadata` function in the `Context` struct to allow merging of caller-provided metadata into the context, enabling sub-agent hooks to access this information.
- Update the `Stream` method in the `Assistant` to utilize the new metadata merging functionality, enhancing the context management for sub-agents.
This commit is contained in:
Max 2026-02-16 19:00:33 +08:00
parent 8a3dd148c7
commit 2d86ee5b6e
2 changed files with 19 additions and 0 deletions

View file

@ -55,6 +55,9 @@ func (ast *Assistant) Stream(ctx *context.Context, inputMessages []context.Messa
opts = &context.Options{}
}
// Merge caller-provided metadata into ctx so sub-agent hooks can read it via ctx.metadata
ctx.MergeMetadata(opts.Metadata)
// Initialize stack and auto-handle completion/failure/restore
_, _, done := context.EnterStack(ctx, ast.ID, opts)
defer done()

View file

@ -541,3 +541,19 @@ func (ctx *Context) IsA2ACall() bool {
func (ctx *Context) IsForkedA2ACall() bool {
return ctx.Referer == RefererAgentFork
}
// MergeMetadata merges the given metadata into ctx.Metadata.
// Existing keys are overwritten by incoming values.
// This enables A2A callers to pass custom metadata (e.g. oneshot, async)
// to sub-agent hooks via ctx.metadata in JavaScript.
func (ctx *Context) MergeMetadata(metadata map[string]interface{}) {
if len(metadata) == 0 {
return
}
if ctx.Metadata == nil {
ctx.Metadata = make(map[string]interface{}, len(metadata))
}
for k, v := range metadata {
ctx.Metadata[k] = v
}
}