[FEATURE] Support block/proc for dynamic context setup in Agent - #903
[FEATURE] Support block/proc for dynamic context setup in Agent#903mikemikimike wants to merge 4 commits into
Conversation
Signed-off-by: mikemikimike <13286568797@163.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #903 +/- ##
==========================================
- Coverage 94.44% 94.43% -0.02%
==========================================
Files 238 238
Lines 11940 12085 +145
Branches 2030 2069 +39
==========================================
+ Hits 11277 11412 +135
Misses 285 285
- Partials 378 388 +10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
The feature is implemented coherently with added specs, and the remaining feedback is limited to small documentation/efficiency fixes.
Pull request overview
This PR extends RubyLLM::Agent’s DSL so context can be configured with a deferred block/Proc, enabling per-run context setup based on declared inputs while ensuring plain (non-Rails) agent chats/instances resolve the context once before chat construction.
Changes:
- Allow
Agent.contextto accept either a static Context or a deferredProc/block. - Resolve deferred contexts from runtime inputs before building plain agent chats and instances, and reuse the resolved context during configuration to avoid double evaluation.
- Add specs covering deferred context resolution from inputs and “evaluate once” behavior.
File summaries
| File | Description |
|---|---|
| lib/ruby_llm/agent.rb | Adds deferred context support, context pre-resolution for plain builds, and reuses resolved contexts during configuration. |
| spec/ruby_llm/agent_dsl_spec.rb | Adds test coverage for input-driven context blocks and single-evaluation behavior. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| apply_protocol(record) | ||
| apply_context(record) | ||
| apply_context(record, runtime_context(chat: record, inputs: input_values)) | ||
| runtime = runtime_context(chat: record, inputs: input_values) | ||
| apply_instructions( |
| # Sets a Context whose configuration chats this agent builds should | ||
| # use, applied via Chat#with_context. Called with no argument, returns | ||
| # the configured context. | ||
| def context(value = nil) | ||
| return @context if value.nil? | ||
| # use, applied via Chat#with_context. A block defers evaluation until a | ||
| # chat is built, with declared ::inputs available as methods. Called | ||
| # with no argument, returns the configured context. | ||
| # |
Closes #900
Summary:
Validation: