Skip to content

feat(rig-core): add ToolCallContext for per-call runtime context in tool dispatch#1537

Open
redroy44 wants to merge 2 commits into0xPlaygrounds:mainfrom
redroy44:feat/tool-call-context
Open

feat(rig-core): add ToolCallContext for per-call runtime context in tool dispatch#1537
redroy44 wants to merge 2 commits into0xPlaygrounds:mainfrom
redroy44:feat/tool-call-context

Conversation

@redroy44
Copy link
Copy Markdown
Contributor

Summary

  • Adds ToolCallContext, a type-erased, cloneable type-map (modeled after http::Extensions' AnyClone pattern) that lets callers inject per-call runtime values (auth tokens, session IDs, conversation state) into tool execution
  • Threads context through the full dispatch chain: ToolServerHandle → channel → ToolServerToolSetToolTypeToolDynTool
  • Fully backward-compatible: all existing Tool and ToolDyn implementations continue to work without changes

New public API

Item Description
ToolCallContext (struct) Type-map with new(), insert<T>(), get<T>(), get_mut<T>(), remove<T>(), contains<T>(). Re-exported from crate root.
Tool::call_with_context(args, &ctx) Default method — delegates to call(). Override to access context.
ToolDyn::call_with_context(args, ctx) Default method — delegates to call(). Blanket impl threads to Tool::call_with_context.
ToolSet::call_with_context(name, args, ctx) Dispatches to tool with context.
ToolServerHandle::call_tool_with_context(name, args, ctx) Sends context across the channel to the tool server.

No existing public API signatures were changed. No breaking changes.

Design

ToolCallContext uses Option<Box<HashMap<TypeId, Box<dyn AnyClone>>>> so empty contexts (the common path) have zero allocation. WASM-compatible via cfg(target_family = "wasm") gating on Send + Sync bounds.

Delegation direction:

  • Dispatch layers (handle → set → dyn): old method delegates to new _with_context method + empty context
  • Tool trait: new call_with_context default delegates to call (ignoring context)

Test plan

  • 12 unit tests for ToolCallContext (insert/get/get_mut/remove/clone/contains/debug/empty allocation)
  • 3 integration tests proving context flows end-to-end through ToolServer dispatch
  • All 454 pre-existing + new lib tests pass
  • cargo clippy --lib --tests -- -D warnings clean
  • cargo fmt clean

This PR was generated with AI assistance (Claude).

Fixes #1536

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(rig-core): Per-call runtime context for tool dispatch (ToolCallContext)

1 participant