mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 11:31:17 +08:00
feat/https-compose-caddy
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
f14906e046 |
refactor(integrations): share tool-wrapper metrics between langchain, crewai, autogen (#4132)
## Description
The LangChain, CrewAI and AutoGen tool wrappers each had their own
verbatim copy of `ToolCompressionMetrics`, `ToolMetricsCollector` and
the `_record_metrics` body. The UTC timestamp fix (#3117) had to land in
all three. `OptimizationMetrics` was also byte-identical in the
LangChain, Agno and Strands model wrappers.
This PR moves them into one stdlib-only module,
`headroom/integrations/_metrics.py`. Every integration re-exports the
names, so all existing import paths still work
(`headroom.integrations.*`, `.langchain`, `.crewai`, `.autogen`,
`.agno`, `.strands`, and each `agents` / `model` module).
Refs #4136
## Type of Change
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [x] Code refactoring
## Changes Made
- New `headroom/integrations/_metrics.py`: `OptimizationMetrics`,
`ToolCompressionMetrics`, `ToolMetricsCollector` and the private helper
`_record_tool_metrics(collector, tool_name, original, compressed,
was_compressed, log)`.
- `langchain/agents.py`, `crewai/agents.py`, `autogen/agents.py`: drop
their copies and import the shared ones. Each module keeps its own
`_global_metrics`, `get_tool_metrics`, `reset_tool_metrics`, its
module-local `compress_tool_result` import (tests patch it per module)
and its `_record_metrics` (now a delegate with the same signature).
- Each wrapper passes its own module logger, so the savings line keeps
its logger name. The LangChain line moved from an f-string to `%`-style.
The text is identical: `{r:.1%}` and `"%.1f%%" % (r*100)` matched on
200,000 random ratios.
- `langchain/chat_model.py`, `agno/model.py`, `strands/model.py`: drop
their `OptimizationMetrics` copies and import the shared one.
- Tests: drop the CrewAI and AutoGen copies of the dataclass and
collector tests (9 tests).
`tests/test_integrations/langchain/test_agents.py` covers the same cases
(create, required fields, empty summary, add and summary, cap at 1000)
against the now-shared class.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [ ] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
# repo venv: langchain/test_agents, test_extended, timestamps_utc, crewai, autogen, langchain/test_chat_model, agno/test_model, strands/test_model_unit
origin/main: 165 passed, 65 skipped
this branch: 165 passed, 56 skipped (the 9 removed tests were skipped here: no crewai/autogen in the repo venv)
# scratch venv with crewai 1.15.27 + autogen-agentchat 0.7.5 (these files never run in CI)
$ pytest tests/test_integrations/crewai tests/test_integrations/autogen tests/test_integration_metric_timestamps_utc.py
origin/main: 28 passed
this branch: 19 passed (9 removed)
# scratch venv with strands-agents: tests/integrations/test_strands/test_model_unit.py test_hooks_unit.py
origin/main: 61 passed this branch: 61 passed
# tests/test_integrations/agno/test_model.py
origin/main: 54 passed, 5 skipped this branch: 54 passed, 5 skipped
$ uvx ruff@0.16.8 check <touched files> -> All checks passed!
$ uvx ruff@0.16.8 format <touched files> -> unchanged
$ mypy <7 touched product files> --ignore-missing-imports
headroom/integrations/langchain/chat_model.py:380: error: Function "RunnableBinding" could always be true [truthy-function]
(pre-existing: the same error on origin/main, on an untouched line)
```
## Real Behavior Proof
- Environment: macOS, Python 3.12, repo venv plus a scratch venv with
crewai 1.15.27, autogen-agentchat 0.7.5 and strands-agents.
- Exact command / steps: ran the test sets above on origin/main and on
this branch. Then `python -c "import headroom.integrations as I;
print(I.ToolMetricsCollector is I.CrewAIToolMetricsCollector is
I.AutoGenToolMetricsCollector, I.OptimizationMetrics is
I.AgnoOptimizationMetrics)"`.
- Observed result: same pass counts, minus the 9 removed tests. Prints
`True True`. All public names and aliases still import from their old
paths.
- Smoke harness (real proxy started from this checkout against a local
fake upstream; temp HOME and workspace, offline):
```text
branch: cleanup/cli-integrations-shared-tool-metrics @
|
||
|
|
e8bff1cfe3 |
feat: add CrewAI and AutoGen tool compression integrations (#1384)
## Description Add CrewAI and AutoGen tool compression integrations, following the same patterns as the existing LangChain agent integration (`HeadroomToolWrapper` / `wrap_tools_with_headroom`). Both delegate compression to `compress_tool_result()` from the MCP integration, with per-tool metrics tracking via `ToolCompressionMetrics` / `ToolMetricsCollector`. Closes #1379 ## Type of Change - [x] New feature (non-breaking change that adds functionality) ## Changes Made - Add `headroom/integrations/crewai/` — `HeadroomToolWrapper` subclasses CrewAI `BaseTool`, wraps `_run()` with compression - Add `headroom/integrations/autogen/` — `HeadroomToolWrapper` wraps AutoGen `FunctionTool` (sync and async) with compression - Wire both into `headroom/integrations/__init__.py` with aliased re-exports (avoids name collision with LangChain's `HeadroomToolWrapper`) - Add `[crewai]` and `[autogen]` optional dependency extras to `pyproject.toml` - Add 24 unit tests (12 per framework) under `tests/test_integrations/` - Add `.mdx` doc pages for both frameworks under `docs/content/docs/` - Update `CHANGELOG.md` with entries under `### Added` ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ ruff check headroom/integrations/crewai headroom/integrations/autogen tests/test_integrations/crewai tests/test_integrations/autogen All checks passed! $ pytest tests/test_integrations/autogen -v 12 passed $ pytest tests/test_integrations/crewai -v 12 passed ``` ## Real Behavior Proof - Environment: Windows 11, Python 3.11, crewai 1.14.7, autogen-agentchat 0.7.5 - Exact command / steps: Ran standalone adapter demos and benchmark runner across 4 task types - Observed result: | Task | Tokens (raw) | Tokens (compressed) | Savings | |------|-------------|-------------------|---------| | Inventory JSON (80 items) | 5,044 | 1,532 | 69.6% | | Server logs (150 lines) | 8,712 | 314 | 96.4% | | Analytics query (100 rows) | 10,762 | 10,762 | 0% | | API docs (20 endpoints) | 8,043 | 8,043 | 0% | Compression results are identical across CrewAI and AutoGen — expected since both route through the same `compress_tool_result()` pipeline. - Not tested: Full end-to-end with a live LLM agent loop (demos test the compression pipeline standalone). LangGraph not included — headroom already has `headroom/integrations/langchain/langgraph.py`. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I have updated the CHANGELOG.md if applicable ## Additional Notes - LangGraph integration is intentionally excluded — headroom already has one at `headroom/integrations/langchain/langgraph.py` - Re-exports in `__init__.py` are aliased (`CrewAIToolWrapper`, `AutoGenToolWrapper`) to avoid collision with the existing LangChain `HeadroomToolWrapper` - Both integrations follow the exact same conventions as the existing LangChain agents module: optional dep guard, `compress_tool_result()` delegation, metrics with 1000-entry cap, Google-style docstrings - `mypy` not checked due to Rust build dependency (`maturin`) that requires Application Control policy changes on this machine --------- Co-authored-by: Sneha27feb <sroy27.ai@gmail.com> |