mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-08-28 05:25:04 +00:00
Some checks are pending
Pre-commit / run (ubuntu-latest) (push) Waiting to run
Tests ReMe / Unit Tests - py3.13 (push) Waiting to run
Tests ReMe / Unit Tests - py3.11 (push) Waiting to run
Tests ReMe / Unit Tests - py3.12 (push) Waiting to run
Windows Smoke / CLI smoke - py3.11 (push) Waiting to run
* feat: add ssh proxy * feat: add ssh proxy * feat: add ssh proxy * feat: add ssh proxy * feat: add prompt * feat: add agent wrapper * feat: add agent wrapper * feat: add agent wrapper * feat: add tushare skill * feat: add tushare skill * feat: add tushare skill * feat: add none stream * chore(deps): update dependency versions in pyproject.toml - Bump claude-agent-sdk from 0.2.123 to 0.2.126 - Upgrade pre-commit to version 4.6.1 or higher - Upgrade pytest to version 9.1.1 or higher * feat(agent_wrapper): add session compaction support and unify session commands - Introduce compact_session method to BaseAgentWrapper and implement it in AsAgentWrapper, CcAgentWrapper, and CodexAgentWrapper - Add session_command module with SessionCommandResult dataclass and handle_session_command function for /clear and /compact commands - Update __init__.py exports to include session_command handlers - Modify DingTalkWaitStep to handle session commands via handle_session_command function - Remove streaming mode from DingTalkWaitStep and simplify reply handling to final Markdown replies only - Add unit tests for session compaction methods and session command handling across wrappers and DingTalk integration - Clean up and remove obsolete streaming and card rendering code from DingTalk wait step - Adjust daily_cookbook.yaml to remove stream and card_update_interval config entries for DingTalk wait step * feat(auto_fin): add Auto Fin simulated portfolio cookbook workflow - Add comprehensive Auto Fin schema exports for multiple models and enums - Implement base class and helpers for Auto Fin analysis steps - Create file, state, and formatting utilities for Auto Fin with atomic file writes and locking - Define Auto Fin pipeline with four analysis agents: backtest, event, portfolio, and US correlation - Register Auto Fin package in cookbook workflows and schema initialization - Add detailed documentation in markdown describing the system design, workflow, and data contracts * feat(outbound_proxy): add application-scoped outbound HTTP proxy components - Introduce BaseOutboundProxy and OutboundProxyEndpoint as core contracts - Implement FixedHttpOutboundProxy for external HTTP proxy integration - Add SshHttpOutboundProxy providing SSH-backed local HTTP proxy tunnels - Register outbound proxy components in component registry and enumeration - Update components package to include outbound_proxy module - Add dependency on pproxy for SSH HTTP proxy bridging - Include comprehensive unit tests covering proxy lifecycle, validation, environment merging, error handling, readiness, and monitoring mechanisms * refactor(network): replace SSH proxy with explicit HTTP outbound proxy - Remove SSH proxy helper implementation and references in codebase - Add support for explicit HTTP proxy URL in arXiv and HuggingFace clients - Modify clients to use async context manager for consistent resource handling - Update daily paper steps to forward outbound proxy configuration explicitly - Change tests to cover new proxy usage model and remove SSH proxy mocks - Add outbound proxy component configuration in daily_cookbook.yaml - Ensure proxy URL usage disables environment trust in HTTP clients - Fix app context component enum access to be defensive against missing keys * feat(agent_wrapper): add managed proxy support for command environments - Introduce BaseOutboundProxy binding in BaseAgentWrapper for outbound proxy management - Add bash_environment and command_proxy_environment properties to apply proxy settings - Update WorkspaceBackend instantiation in AsAgentWrapper to use bash_environment - Inject managed proxy export commands into Claude Code Bash commands via hooks - Enhance CodexAgentWrapper to include managed proxy in shell environment policy - Modify daily_cookbook.yaml steps to specify outbound_proxy as default where needed - Add comprehensive unit tests verifying managed proxy injection and environment isolation - Ensure subprocess_environment remains unchanged while proxy is applied selectively to commands * refactor(memory): replace search job_tools with memory in daily cookbook config - Change workspace_dir default from .reme to reme_workspace - Replace search job_tools with memory across multiple components and jobs - Update descriptions to reflect long-term memory retrieval instead of search - Modify system prompts to instruct using memory for retrieving notes - Adjust unit tests to verify memory job_tools and job presence instead of search - Ensure consistency in configuration and tests for memory backend usage * refactor(config): rename memory to memory_search in daily cookbook config - Change all occurrences of "memory" to "memory_search" in job_tools and job definitions - Update related system prompts to reflect the new memory_search terminology - Modify unit tests to assert the presence of memory_search instead of memory - Ensure consistency across skills, job tools, and backend configurations in multiple components * feat(auto_fin): add deterministic quantitative research and ranking fusion - Introduce new schema models: EtfScore, RankingMetrics, ExtremeAnalysis, DimensionRanking, and FusionRanking to represent deterministic research outputs - Add ranking data to event, backtest, us_correlation, and portfolio analysis outputs - Implement ranking_section renderer to format Top20 scores and diagnostics in Markdown - Develop AutoFinQuantStep for deterministic ETF ranking using TuShare data, Polars, and a custom extremely randomized tree ensemble - Integrate quantitative rankings into backtest and portfolio analysis steps and reports - Extend auto_fin pipeline with new quant_enabled and quant_required config options - Enforce ranking constraints like unique codes, contiguous ranks, and normalized fusion weights - Update analysis YAMLs with rules limiting data freshness, universe, and ranking usage - Incorporate ranking outputs into all major markdown report bodies in Auto Fin pipeline - Add concurrency-limited asynchronous TuShare client to fetch required market data - Introduce cross-sectional rank correlation and NDCG metrics for ranking quality evaluation * feat(auto_fin): implement stage-wise notification and reporting for analysis pipeline - Refactor notification config in daily_cookbook.yaml to support dispatch steps - Update AutoFinNotificationStep to deduplicate notifications per run stage - Add _notify_stage method in pipeline to send notifications for each analysis stage - Implement persistence and notification for event, backtest, US correlation, and portfolio stages - Modify pipeline flow to persist reports and notify after each stage completion - Adjust metadata to track notifications and errors per stage - Update tests to verify stage-wise notification sending and deduplication - Remove older combined report persistence in favor of modular stage handling * feat(auto_fin): add outbound proxy support for Tushare API usage - Introduce BaseOutboundProxy reference in AutoFinPipelineStep and AutoFinQuantStep - Update TushareResearchClient and trade calendar fetch to accept and use proxy URL - Create _ProxiedTushareApi adapter to route Tushare requests via explicit HTTP proxy - Modify create_tushare_api utility to optionally return proxied API client - Add unit tests covering proxy forwarding and client behavior with managed proxies - Ensure proxy usage respects explicit proxy URL over environment fallback - Integrate outbound proxy into data fetching and quantitative research steps * feat(auto_fin): enforce checkpoint time validation and add state models - Introduce AnalysisState base class and specific states for event, backtest, and US correlation analyses - Replace analysis output types with corresponding state classes in run schemas - Add require_checkpoint_reached method to validate decision_at/data_cutoff against current time - Enforce checkpoint time checks before analysis steps in event, backtest, portfolio, and quant analyses - Refactor quant data loading to include adjustment factors and apply price adjustments without fallback - Update analysis YAML docs to require real-time checkpoint validation and forbid using future data - Improve portfolio run serialization by excluding redundant legacy fields and nested proposed actions - Add helper to extract readable sections from persisted checkpoint documents - Fix event analysis output validation to reject events and sources with future timestamps * feat(auto_fin): auto-select latest reached checkpoint if none specified - Extend checkpoint config to accept empty string for auto selection - Add static method to compute latest checkpoint reached by current time - Modify pipeline step to auto-select checkpoint based on trade calendar and time - Adjust force flag default depending on whether checkpoint is explicit or auto - Log details when checkpoint is auto-selected to improve observability - Add comprehensive tests for auto checkpoint selection logic and edge cases - Remove deprecated default and required constraints from force parameter in config * refactor(auto_fin): unify datetime comparison with compare_datetimes utility - Replace direct datetime comparisons with compare_datetimes function calls - Use cmp_to_key with compare_datetimes for sorting datetime tuples and lists - Update validation logic in backtest, event, analysis, and ledger modules for consistent datetime handling - Add unit tests to verify handling of naive and aware datetime comparisons in event and backtest validations - Ensure marked_at and interval_end timestamps are set and compared consistently using compare_datetimes - Improve correctness of ordering and conditional checks related to timestamps throughout auto_fin steps and ledger code * feat(auto_fin): add datetime comparison helper for mixed timezone data - Implement compare_datetimes function to handle naive and aware datetimes - Ensure naive datetime is interpreted in the known timezone of the counterpart - Facilitate comparisons between legacy and timezone-aware Auto Fin data - Add module docstring explaining purpose of the helpers * docs(auto_fin): enforce unique ETF representative per sub-theme in analysis rules - Update backtest.yaml to recommend or highlight only one ETF per sub-theme for ETF analyses - Modify event.yaml to map only one representative ETF per sub-theme, avoiding duplicate recommendations - Revise portfolio.yaml to restrict holdings/buys to a single ETF per sub-theme, preventing repeated buys of highly overlapping ETFs - Adjust us_correlation.yaml to retain only one representative A-share ETF per sub-theme for mapping or recommendation - Add test to verify presence of new sub-theme uniqueness guidance in step prompts * feat(auto_fin): separate draft model and include deterministic fusion ranking - Introduce _PortfolioProposalDraft pydantic model for agent-authored fields before ranking - Discard any "fusion_ranking" data from draft to prevent conflicts with canonical ranking - Modify AutoFinPortfolioStep to receive draft, enrich with fusion_ranking, and produce final output - Update tests to use _PortfolioProposalDraft and validate deterministic fusion ranking propagation - Add async test verifying fusion ranking is correctly set in portfolio output with no errors * refactor(auto_fin): rewrite and simplify Auto Fin schema and steps - Remove legacy Auto Fin analysis step modules and helpers - Replace complex ranking and portfolio models with simplified current-news models - Update schema to focus on news-case workflow with new domain models - Remove A-share decision checkpoints and backtest details from schema - Simplify recommendation and decision output structures - Clean up deprecated state and utility functions - Update Auto Fin steps initialization to new pipeline steps only - Improve uniqueness validation for themes and ETFs in research plan * feat(auto_fin): implement full local cache and analysis workflow for Auto Fin - Add AutoFinDataStep to prepare and cache daily TuShare data with lookback - Add AutoFinAnalysisStep to analyze cached data and generate Markdown report - Implement detailed time window, ETF filtering, and historical case validation - Introduce YAML prompts for planning and decision-making steps - Update .gitignore to include reme_workspace/ - Clean up config and import structure for auto_fin steps - Remove old pipeline.py and consolidate functionality into new modules - Use polars for efficient CSV reading and data processing - Ensure atomic writes and strict JSON serialization for cache files - Enforce rules on news timing, ETF universe, and historical case usage * fix(auto_fin): restrict news data source to '财联社' in analysis and cache - Update analysis templates to specify current news as from '财联社' only - Modify news fetching functions to filter by source '财联社' - Add validation method to check cached news source correctness - Update news caching logic to exclude non-'财联社' news - Enhance unit tests with multiple sources to ensure filtering works - Confirm news API calls include source filter parameter as '财联社' * refactor(auto_fin): convert I/O methods to asynchronous implementations - Change _news, _dataset, and _theme_data methods to async for improved concurrency - Move JSONL and CSV reading operations to asynchronous wrappers using asyncio.to_thread - Remove synchronous _read_jsonl and _read_csv functions, integrate them as static async class methods - Update cache validation methods to async, awaiting I/O operations accordingly - Adjust usage of dataset and news retrieval in analysis step to await asynchronous methods - Add async unit test to validate JSONL reading with unicode line separators - Preserve existing functionality while enabling non-blocking file and data access * fix(nx_file_graph): defer networkx import and improve dependency handling - Move networkx import inside NxFileGraph constructor for lazy loading - Raise ImportError with original exception context if networkx is missing - Remove module-level fallback assignment of nx to None - Expand test to block loading of multiple optional core dependencies eagerly - Change exception type in test from ModuleNotFoundError to AssertionError - Update test comments to reflect broader optional dependency checks * feat(embedding_store): add quota retry delay mechanism for embedding requests - Introduce quota_retry_delay parameter to configure wait time before retry on quota exhaustion - Implement detection of insufficient quota errors in LocalEmbeddingStore without external SDK - Add retry logic with custom delay when quota is insufficient during embedding requests - Update configuration to set max_retries and quota_retry_delay defaults for embedding store - Add unit tests covering quota exhaustion retry behavior with delay and opt-in control - Ensure existing retry behavior remains unchanged if quota_retry_delay is not set * feat(auto_fin): add detailed logging to analysis and data fetching steps - Add _preview static method for bounded diagnostic output in analysis.py - Log prompt start, completion, errors, and validation details in _reply method - Add info logs for major processing steps in execute method of analysis.py - Add debug and info logs for cache validation, data fetching, and pagination in data.py - Log conditions for skipping reports and cache plans in data.py execute method - Log download summaries and cache writes for news and ETF data - Improve error logging with exception details in cache validation functions - Ensure all logs include context such as record counts, paths, and parameters * refactor(auto_fin): overhaul Auto Fin workflow and schema contracts - Replace old Auto Fin schema models with comprehensive new data classes - Remove legacy Auto Fin analysis step in favor of modular agent-based steps - Introduce AutoFinAgentStep for validating structured agent replies - Simplify data cleaning and JSONL writing utilities for news cache - Remove synchronous and asynchronous dataset methods from analysis step - Redefine Auto Fin analysis configuration for 360-day news retention and multi-step pipeline - Remove embedded analysis prompt templates and replace with agent-driven logic - Update __init__.py exports to match new step implementations and remove deprecated classes - Improve error handling and validation in agent step reply processing - Clean up redundant imports and unused code in analysis and data preparation modules * feat(auto_fin): add detailed logging for analysis and data processing steps - Add timing logs to measure agent prompt processing duration in analysis.py - Log news cache hits and news write paths with record counts in data.py - Include detailed info logs for news download start and completion in data.py - Add start, progress, and completion logs with topic and event counts in history.py - Log start and completion of merge step including path and ETF count in merge.py - Add start and done logs with window and news counts in topic.py * feat(auto_fin): enhance schema and steps with detailed ETF and event modeling - Replace and add multiple AutoFin schema classes to support detailed ETF selection, historical research, market analysis, forecast models, and report output with validation - Implement Shanghai timezone normalization and strict validation in schema models - Remove deprecated AutoFin analysis agent step and consolidate reply handling in base step - Introduce AutoFinStep base class with shared helpers for prompt handling, data fetching, logging, and JSONL file operations - Add AutoFinDataStep to manage daily news data complete with schedule validation, caching, and source validation logic - Update cookbook configuration to customize auto_fin step parameters and simplify outbound proxy settings - Refactor imports and clean unused code for better maintainability * feat(auto_fin): introduce detailed historical event resolution and market similarity analysis - Add AutoFinHistoricalEventReference and AutoFinHistoricalSimilarity models for refined event referencing and similarity judgment - Implement validation to ensure non-empty critical fields and uniqueness of historical news IDs - Develop method to resolve Agent-selected historical event references from workspace files with strict path and existence checks - Enrich historical events with market entry and future returns data after resolution - Redesign market step to calculate similarity-weighted ETF forecasts based on matched historical event similarities - Enforce validation on matched historical events for uniqueness and proper weight summation - Simplify merge step output to final Markdown report without YAML frontmatter and redundant fields - Update user instructions for history search, market, and merge steps to reflect new data structures and responsibilities - Adjust test suite to cover new schema and step behavior changes, including enhanced validation and JSON output formats * feat(auto_fin): add new cron jobs and output analysis jsonl - Add new cron jobs auto_fin_1145_cron and auto_fin_1800_cron with auto_fin_steps - Change auto_fin_0930_cron schedule to run Monday to Sunday - Extend merge step to write analysis data to auto_fin_analysis.jsonl - Update unit tests to verify new cron jobs and their steps configuration * fix(auto_fin): improve atomic file write and refresh daily index - Change temporary file naming to include UUID for uniqueness and hidden prefix - Replace atomic write method from using Path.replace to os.replace with safe unlink - Add import and use os.replace for safer file replace operation - Refresh daily index after writing auto finance markdown and JSONL files - Import and call refresh_day_index in merge step to update file index asynchronously * docs(cookbook): add optional SSH proxy configuration in README files - Introduce optional SSH proxy setup in auto-fin and daily_paper cookbooks - Provide instructions to enable outbound proxy via `daily_cookbook.yaml` and environment variables - Add `REME_PROXY_IP` and `REME_PROXY_ACCOUNT` environment variables descriptions in multiple README files - Update English and Chinese README and README_ZH documents with proxy details - Maintain consistent formatting of environment variable tables across documents * fix(file_io): include schema_version in hidden metadata keys - Added "schema_version" to _INDEX_HIDDEN_METADATA_KEYS in _daily_index.py - Updated _render_notes_block to always include additional keys regardless of schema_version fix(deps): move pproxy dependency to later in pyproject.toml - Removed pproxy from early dependencies list - Added pproxy back near the end of dependency list for better ordering fix(outbound_proxy): require pproxy package for ssh_http proxy - Added importlib.util check for pproxy package presence - Raise RuntimeError if pproxy is not installed when using SSH HTTP outbound proxy - Improved error message suggests installing reme-ai with 'core' extra * docs(readme): update News section with new Cookbook workflows - Clarify introduction of optional Cookbooks with Daily Paper and Auto Fin workflows - Update English README to reflect both paper discovery and file-native ETF event research - Revise Chinese README to include financial news and historical market data research capability - Maintain announcement of paper acceptance at Findings of ACL 2026 * feat(auto_fin): add calculation results to final Markdown output - Implement _calculation_results to summarize forecast for each ETF analyzed - Include program-calculated results in the JSON input for the Markdown report - Update YAML template to incorporate calculation results and adjust recommendation rules - Refine recommendation logic to rely on event impact judgments combined with calculation outputs - Modify tests to verify presence of calculation results and updated report content and format * up prompt * fix(keyword_index): ignore non-indexable chunks during keyword sync - Add is_indexable method to base and BM25 keyword index classes to check text tokenizability - Update local file store to exclude non-indexable chunks from expected document IDs to prevent rebuild - Fix JSONL chunker to correctly handle Unicode line separator U+2028 inside JSON strings without splitting - Add test to ensure non-empty but non-indexable chunk does not trigger keyword index rebuild - Add test to verify U+2028 character does not cause incorrect JSONL record splitting
561 lines
24 KiB
Python
561 lines
24 KiB
Python
"""Claude Code SDK backend for the unified agent wrapper."""
|
|
|
|
import json
|
|
import shlex
|
|
from collections.abc import AsyncGenerator
|
|
from contextlib import aclosing
|
|
from dataclasses import asdict, dataclass, fields
|
|
from pathlib import Path
|
|
from typing import Any, TYPE_CHECKING
|
|
|
|
from .base_agent_wrapper import BaseAgentWrapper
|
|
from ..component_registry import R
|
|
from ...enumeration import ChunkEnum
|
|
from ...schema import StreamChunk
|
|
|
|
if TYPE_CHECKING:
|
|
from claude_agent_sdk import AssistantMessage, ResultMessage, UserMessage
|
|
|
|
from ..job.base_job import BaseJob
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class _BlockState:
|
|
"""Metadata needed to correlate one streamed content block."""
|
|
|
|
block_id: str | None
|
|
block_type: str
|
|
tool_name: str | None
|
|
|
|
|
|
@R.register("claude_code")
|
|
class CcAgentWrapper(BaseAgentWrapper):
|
|
"""Agent wrapper backed by Claude Code SDK."""
|
|
|
|
SDK_PACKAGE = "claude-agent-sdk"
|
|
DEFAULT_DISALLOWED_TOOLS = ["WebSearch"]
|
|
MCP_SERVER_NAME = "mcp_server"
|
|
|
|
@property
|
|
def session_path(self) -> Path:
|
|
"""Directory used for persisted Claude Code sessions."""
|
|
if self.app_context is None:
|
|
return self.workspace_path / "mem_session"
|
|
return self.workspace_path / self.app_context.app_config.mem_session_dir
|
|
|
|
def _ensure_claude_skill_dir(self, config_dir: Path, skills: list[str] | str) -> None:
|
|
"""Add selected project skills to Claude Code discovery locations."""
|
|
sources = self._resolve_project_skills(skills)
|
|
if not sources:
|
|
return
|
|
|
|
for target in (self.project_path / ".claude" / "skills", config_dir / "skills"):
|
|
try:
|
|
if target.is_symlink():
|
|
self.logger.warning(f"Preserving existing Claude Code skills link: {target}")
|
|
continue
|
|
if target.exists() and not target.is_dir():
|
|
self.logger.warning(f"Preserving existing Claude Code skills path: {target}")
|
|
continue
|
|
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
for skill_name, source in sources.items():
|
|
skill_target = target / skill_name
|
|
if skill_target.is_symlink():
|
|
if skill_target.resolve() != source.resolve():
|
|
self.logger.warning(f"Preserving existing Claude Code skill link: {skill_target}")
|
|
continue
|
|
if skill_target.exists():
|
|
self.logger.warning(f"Preserving existing Claude Code skill path: {skill_target}")
|
|
continue
|
|
skill_target.symlink_to(source, target_is_directory=True)
|
|
except OSError as exc:
|
|
self.logger.warning(f"Failed to link Claude Code skills into {target}: {exc}")
|
|
|
|
@classmethod
|
|
def _make_tool(cls, job: "BaseJob", tool_context_id: str | None = None):
|
|
from claude_agent_sdk import SdkMcpTool
|
|
|
|
async def run_job(args):
|
|
call_args = dict(args)
|
|
if tool_context_id:
|
|
if "tool_context_id" in call_args:
|
|
raise ValueError("tool_context_id is injected by agent_wrapper")
|
|
call_args["tool_context_id"] = tool_context_id
|
|
response = await job(**call_args)
|
|
return {
|
|
"content": [{"type": "text", "text": str(response.answer)}],
|
|
"is_error": not response.success,
|
|
}
|
|
|
|
return SdkMcpTool(
|
|
name=job.name,
|
|
description=job.description,
|
|
input_schema=job.parameters,
|
|
handler=run_job,
|
|
)
|
|
|
|
def _add_bash_proxy_hook(self, opts: Any) -> None:
|
|
"""Inject managed proxy exports into Claude Code Bash commands."""
|
|
proxy_environment = self.command_proxy_environment
|
|
if not proxy_environment:
|
|
return
|
|
|
|
from claude_agent_sdk import HookMatcher
|
|
|
|
exports = " ".join(f"{name}={shlex.quote(value)}" for name, value in proxy_environment.items())
|
|
|
|
async def inject_proxy(hook_input, _tool_use_id, _context):
|
|
tool_input = dict(hook_input["tool_input"])
|
|
command = tool_input.get("command")
|
|
if not isinstance(command, str):
|
|
return {}
|
|
tool_input["command"] = f"export {exports}; {command}"
|
|
return {
|
|
"hookSpecificOutput": {
|
|
"hookEventName": "PreToolUse",
|
|
"updatedInput": tool_input,
|
|
},
|
|
}
|
|
|
|
hooks = dict(opts.hooks or {})
|
|
pre_tool_use = list(hooks.get("PreToolUse") or [])
|
|
pre_tool_use.append(HookMatcher(matcher="Bash", hooks=[inject_proxy]))
|
|
hooks["PreToolUse"] = pre_tool_use
|
|
opts.hooks = hooks
|
|
|
|
def _build_options(self, inputs: Any, stream: bool = False, **kwargs) -> Any:
|
|
"""Build ClaudeAgentOptions from kwargs.
|
|
|
|
``stream=True`` enables ``include_partial_messages`` so that
|
|
``StreamEvent`` messages are emitted alongside the final
|
|
``ResultMessage``.
|
|
"""
|
|
from claude_agent_sdk import ClaudeAgentOptions, create_sdk_mcp_server
|
|
|
|
if not isinstance(inputs, str):
|
|
raise NotImplementedError("Only string input is supported for Claude Code.")
|
|
|
|
selected_skills = kwargs.get("skills")
|
|
if isinstance(selected_skills, str) and selected_skills != "all":
|
|
selected_skills = [selected_skills]
|
|
|
|
if "setting_sources" not in kwargs and kwargs.get("skills") is None:
|
|
kwargs["setting_sources"] = []
|
|
skip_keys = {"job_tools", "output_schema", "api_key", "base_url", "credential"}
|
|
option_fields = {field.name for field in fields(ClaudeAgentOptions)}
|
|
option_kwargs = {key: value for key, value in kwargs.items() if key not in skip_keys and key in option_fields}
|
|
option_kwargs["disallowed_tools"] = list(
|
|
dict.fromkeys(
|
|
[
|
|
*(kwargs.get("disallowed_tools") or []),
|
|
*self.DEFAULT_DISALLOWED_TOOLS,
|
|
],
|
|
),
|
|
)
|
|
if selected_skills is not None:
|
|
option_kwargs["skills"] = selected_skills
|
|
if stream:
|
|
option_kwargs["include_partial_messages"] = True
|
|
opts = ClaudeAgentOptions(**option_kwargs)
|
|
|
|
opts.env = dict(opts.env)
|
|
opts.env.update(self.subprocess_environment)
|
|
api_key = kwargs.get("api_key")
|
|
base_url = kwargs.get("base_url")
|
|
extra_env_dict = {
|
|
"ANTHROPIC_AUTH_TOKEN": api_key if isinstance(api_key, str) else "",
|
|
"ANTHROPIC_BASE_URL": base_url if isinstance(base_url, str) else "",
|
|
}
|
|
if opts.model:
|
|
extra_env_dict.update(
|
|
{
|
|
"ANTHROPIC_MODEL": opts.model,
|
|
"ANTHROPIC_DEFAULT_HAIKU_MODEL": opts.model,
|
|
"ANTHROPIC_DEFAULT_SONNET_MODEL": opts.model,
|
|
"ANTHROPIC_DEFAULT_OPUS_MODEL": opts.model,
|
|
},
|
|
)
|
|
opts.env.update(extra_env_dict)
|
|
self._add_bash_proxy_hook(opts)
|
|
self.session_path.mkdir(parents=True, exist_ok=True)
|
|
opts.cwd = opts.cwd or self.cwd
|
|
claude_config_dir = self.session_path / "claude_config"
|
|
opts.env.setdefault("CLAUDE_CONFIG_DIR", str(claude_config_dir))
|
|
if selected_skills is not None:
|
|
self._ensure_claude_skill_dir(claude_config_dir, selected_skills)
|
|
|
|
job_tools: list[str] = kwargs.get("job_tools", [])
|
|
resolved_jobs = self._resolve_job_tools(job_tools)
|
|
if resolved_jobs:
|
|
if not isinstance(opts.mcp_servers, dict):
|
|
raise ValueError("job_tools require mcp_servers to be a mapping so the ReMe SDK server can be merged")
|
|
opts.mcp_servers = dict(opts.mcp_servers)
|
|
if self.MCP_SERVER_NAME in opts.mcp_servers:
|
|
raise ValueError(f"mcp_servers already contains reserved server name {self.MCP_SERVER_NAME!r}")
|
|
sdk_tools = [self._make_tool(job, kwargs.get("tool_context_id")) for job in resolved_jobs]
|
|
opts.mcp_servers[self.MCP_SERVER_NAME] = create_sdk_mcp_server(
|
|
name=self.MCP_SERVER_NAME,
|
|
tools=sdk_tools,
|
|
)
|
|
opts.allowed_tools = list(opts.allowed_tools)
|
|
opts.allowed_tools.extend(job.name for job in resolved_jobs)
|
|
|
|
if (output_schema := kwargs.get("output_schema")) is not None:
|
|
opts.output_format = {"type": "json_schema", "schema": output_schema}
|
|
|
|
return opts
|
|
|
|
# ----- StreamChunk conversion -------------------------------------------
|
|
|
|
@classmethod
|
|
# pylint: disable=too-many-return-statements
|
|
def _raw_event_to_chunk(
|
|
cls,
|
|
raw: dict,
|
|
session_id: str | None = None,
|
|
block_states: dict[int, _BlockState] | None = None,
|
|
) -> StreamChunk | None:
|
|
"""Convert a raw Anthropic streaming event dict to a StreamChunk.
|
|
|
|
``block_states`` maps each content-block index to metadata captured at
|
|
``content_block_start`` for use by later delta and stop events.
|
|
|
|
Returns ``None`` for events that should be silently skipped.
|
|
"""
|
|
event_type = raw.get("type")
|
|
|
|
# --- Message-level lifecycle ----------------------------------------
|
|
|
|
if event_type == "message_start":
|
|
if block_states is not None:
|
|
block_states.clear()
|
|
message = raw.get("message", {})
|
|
meta = {
|
|
"message_id": message.get("id"),
|
|
"model": message.get("model"),
|
|
"role": message.get("role"),
|
|
}
|
|
return cls._chunk(ChunkEnum.REPLY_START, session_id=session_id, chunk="", metadata=meta)
|
|
|
|
if event_type == "message_delta":
|
|
delta = raw.get("delta", {})
|
|
usage = raw.get("usage", {})
|
|
return cls._chunk(
|
|
ChunkEnum.USAGE,
|
|
session_id=session_id,
|
|
chunk=json.dumps(usage),
|
|
output_tokens=usage.get("output_tokens"),
|
|
metadata={"stop_reason": delta.get("stop_reason")},
|
|
)
|
|
|
|
if event_type == "message_stop":
|
|
return cls._chunk(ChunkEnum.REPLY_END, session_id=session_id, chunk="")
|
|
|
|
# --- Content-block lifecycle ----------------------------------------
|
|
|
|
if event_type == "content_block_start":
|
|
idx, content_block = raw.get("index", 0), raw.get("content_block", {})
|
|
block_type, bid = content_block.get("type", ""), content_block.get("id", "")
|
|
if block_states is not None:
|
|
block_states[idx] = _BlockState(bid or None, block_type, content_block.get("name"))
|
|
|
|
if block_type == "text":
|
|
return cls._chunk(ChunkEnum.CONTENT, block_id=bid, chunk=content_block.get("text", ""))
|
|
if block_type == "thinking":
|
|
return cls._chunk(
|
|
ChunkEnum.THINK,
|
|
block_id=bid,
|
|
chunk=content_block.get("thinking", ""),
|
|
)
|
|
if block_type in {"tool_use", "server_tool_use"}:
|
|
payload = {
|
|
"name": content_block.get("name"),
|
|
"id": content_block.get("id"),
|
|
}
|
|
return cls._chunk(
|
|
ChunkEnum.TOOL_CALL,
|
|
block_id=bid,
|
|
tool_call_id=content_block.get("id"),
|
|
tool_call_name=content_block.get("name"),
|
|
chunk=json.dumps(payload),
|
|
)
|
|
return None
|
|
|
|
if event_type == "content_block_delta":
|
|
delta = raw.get("delta", {})
|
|
delta_type = delta.get("type", "")
|
|
idx = raw.get("index", 0)
|
|
state = block_states.get(idx) if block_states else None
|
|
bid = state.block_id if state else None
|
|
tool_name = state.tool_name if state else None
|
|
|
|
if delta_type == "text_delta":
|
|
return cls._chunk(ChunkEnum.CONTENT, block_id=bid, chunk=delta.get("text", ""))
|
|
if delta_type == "thinking_delta":
|
|
return cls._chunk(ChunkEnum.THINK, block_id=bid, chunk=delta.get("thinking", ""))
|
|
if delta_type == "input_json_delta":
|
|
return cls._chunk(
|
|
ChunkEnum.TOOL_CALL,
|
|
block_id=bid,
|
|
tool_call_id=bid,
|
|
tool_call_name=tool_name,
|
|
chunk=delta.get("partial_json", ""),
|
|
)
|
|
return None
|
|
|
|
if event_type == "content_block_stop":
|
|
idx = raw.get("index", 0)
|
|
state = block_states.pop(idx, None) if block_states else None
|
|
bid = state.block_id if state else None
|
|
block_type = state.block_type if state else None
|
|
tool_name = state.tool_name if state else None
|
|
|
|
if block_type in {"tool_use", "server_tool_use"}:
|
|
return cls._chunk(
|
|
ChunkEnum.TOOL_CALL,
|
|
block_id=bid,
|
|
tool_call_id=bid,
|
|
tool_call_name=tool_name,
|
|
chunk="",
|
|
)
|
|
if block_type == "thinking":
|
|
return cls._chunk(ChunkEnum.THINK, block_id=bid, chunk="")
|
|
# text or unknown -> CONTENT
|
|
return cls._chunk(ChunkEnum.CONTENT, block_id=bid, chunk="")
|
|
|
|
# Ping / other unknown types -> skip
|
|
return None
|
|
|
|
@classmethod
|
|
def _message_content_to_chunks(
|
|
cls,
|
|
msg: "AssistantMessage | UserMessage",
|
|
session_id: str | None = None,
|
|
visible_tool_call_ids: set[str] | None = None,
|
|
include_text: bool = False,
|
|
) -> list[StreamChunk]:
|
|
"""Convert typed SDK content blocks that are not partial events."""
|
|
from claude_agent_sdk import ServerToolResultBlock, TextBlock, ToolResultBlock
|
|
|
|
chunks: list[StreamChunk] = []
|
|
content = getattr(msg, "content", None)
|
|
if isinstance(content, str):
|
|
if include_text and content:
|
|
chunks.append(cls._chunk(ChunkEnum.CONTENT, session_id=session_id, chunk=content))
|
|
return chunks
|
|
if content is None:
|
|
return chunks
|
|
|
|
for block in content:
|
|
if include_text and isinstance(block, TextBlock) and block.text:
|
|
chunks.append(cls._chunk(ChunkEnum.CONTENT, session_id=session_id, chunk=block.text))
|
|
elif isinstance(block, (ToolResultBlock, ServerToolResultBlock)):
|
|
tool_use_id = block.tool_use_id
|
|
if visible_tool_call_ids is not None and tool_use_id not in visible_tool_call_ids:
|
|
continue
|
|
payload: dict[str, Any] = {
|
|
"tool_use_id": tool_use_id,
|
|
"content": block.content,
|
|
}
|
|
if isinstance(block, ToolResultBlock):
|
|
payload["is_error"] = block.is_error
|
|
chunks.append(
|
|
cls._chunk(
|
|
ChunkEnum.TOOL_RESULT,
|
|
session_id=session_id,
|
|
block_id=tool_use_id,
|
|
tool_call_id=tool_use_id,
|
|
chunk=payload,
|
|
),
|
|
)
|
|
|
|
return chunks
|
|
|
|
@classmethod
|
|
def _result_error_text(cls, msg: "ResultMessage") -> str:
|
|
"""Return the error text used by both the SDK and unified chunks."""
|
|
return "; ".join(msg.errors or []) or str(msg.subtype)
|
|
|
|
@classmethod
|
|
def _result_message_to_chunks(cls, msg: "ResultMessage") -> list[StreamChunk]:
|
|
"""Convert the SDK terminal result into usage and error chunks."""
|
|
session_id = msg.session_id or ""
|
|
usage = msg.usage or {}
|
|
chunks = [
|
|
cls._chunk(
|
|
ChunkEnum.USAGE,
|
|
session_id=session_id,
|
|
chunk=json.dumps(usage),
|
|
input_tokens=usage.get("input_tokens"),
|
|
output_tokens=usage.get("output_tokens"),
|
|
metadata={
|
|
"duration_ms": msg.duration_ms,
|
|
"duration_api_ms": msg.duration_api_ms,
|
|
"stop_reason": msg.stop_reason,
|
|
"num_turns": msg.num_turns,
|
|
"total_cost_usd": msg.total_cost_usd,
|
|
"model_usage": msg.model_usage,
|
|
"permission_denials": msg.permission_denials,
|
|
"deferred_tool_use": (asdict(msg.deferred_tool_use) if msg.deferred_tool_use else None),
|
|
"api_error_status": msg.api_error_status,
|
|
},
|
|
),
|
|
]
|
|
if msg.is_error:
|
|
chunks.append(
|
|
cls._chunk(
|
|
ChunkEnum.ERROR,
|
|
session_id=session_id,
|
|
chunk=cls._result_error_text(msg),
|
|
metadata={"api_error_status": msg.api_error_status},
|
|
),
|
|
)
|
|
return chunks
|
|
|
|
# ----- reply / reply_stream --------------------------------------------
|
|
|
|
async def compact_session(self, session_id: str) -> None:
|
|
"""Compact a Claude Code session through its native command."""
|
|
result = await self.reply("/compact", resume=session_id)
|
|
if result["last_message"].get("is_error"):
|
|
raise RuntimeError("Claude Code session compaction failed")
|
|
|
|
async def reply(self, inputs: Any, **kwargs) -> dict:
|
|
from claude_agent_sdk import query, ResultMessage
|
|
|
|
kwargs = self._merged_kwargs(kwargs)
|
|
opts = self._build_options(inputs, stream=False, **kwargs)
|
|
|
|
last_msg = None
|
|
async with aclosing(query(prompt=inputs, options=opts)) as stream:
|
|
async for msg in stream:
|
|
if isinstance(msg, ResultMessage):
|
|
last_msg = msg
|
|
|
|
if last_msg is None:
|
|
raise ValueError("No message received from Claude Code.")
|
|
|
|
result = {
|
|
"session_id": last_msg.session_id or "",
|
|
"last_message": asdict(last_msg),
|
|
"result": last_msg.result,
|
|
}
|
|
if kwargs.get("output_schema") is not None:
|
|
result["structured_output"] = last_msg.structured_output
|
|
return result
|
|
|
|
async def reply_stream(self, inputs: Any, **kwargs) -> AsyncGenerator[StreamChunk, None]:
|
|
"""Stream Claude Code events as unified StreamChunk objects."""
|
|
from claude_agent_sdk import (
|
|
AssistantMessage,
|
|
MirrorErrorMessage,
|
|
query,
|
|
RateLimitEvent,
|
|
ResultMessage,
|
|
StreamEvent,
|
|
SystemMessage,
|
|
UserMessage,
|
|
)
|
|
|
|
kwargs = self._merged_stream_kwargs(kwargs)
|
|
opts = self._build_options(inputs, stream=True, **kwargs)
|
|
|
|
block_states: dict[int, _BlockState] = {}
|
|
visible_tool_call_ids: set[str] = set()
|
|
current_session_id: str | None = None
|
|
emitted_content = False
|
|
emitted_reply_end = False
|
|
reply_open = False
|
|
expected_trailing_error: str | None = None
|
|
|
|
try:
|
|
async with aclosing(query(prompt=inputs, options=opts)) as stream:
|
|
async for msg in stream:
|
|
if expected_trailing_error is not None and not (
|
|
isinstance(msg, SystemMessage) and msg.subtype == "session_state_changed"
|
|
):
|
|
expected_trailing_error = None
|
|
|
|
if isinstance(msg, StreamEvent):
|
|
current_session_id = msg.session_id or current_session_id
|
|
chunk = self._raw_event_to_chunk(
|
|
msg.event,
|
|
session_id=msg.session_id,
|
|
block_states=block_states,
|
|
)
|
|
if chunk is not None:
|
|
chunk.session_id = chunk.session_id or msg.session_id
|
|
if chunk.chunk_type == ChunkEnum.TOOL_CALL and chunk.tool_call_id:
|
|
visible_tool_call_ids.add(chunk.tool_call_id)
|
|
if chunk.chunk_type == ChunkEnum.CONTENT and chunk.chunk:
|
|
emitted_content = True
|
|
if chunk.chunk_type == ChunkEnum.REPLY_START:
|
|
reply_open = True
|
|
if chunk.chunk_type == ChunkEnum.REPLY_END:
|
|
emitted_reply_end = True
|
|
reply_open = False
|
|
yield chunk
|
|
|
|
elif isinstance(msg, UserMessage):
|
|
for chunk in self._message_content_to_chunks(msg, current_session_id, visible_tool_call_ids):
|
|
yield chunk
|
|
|
|
elif isinstance(msg, ResultMessage):
|
|
if msg.is_error:
|
|
expected_trailing_error = (
|
|
f"Claude Code returned an error result: {self._result_error_text(msg)}"
|
|
)
|
|
current_session_id = msg.session_id or current_session_id
|
|
if not emitted_content and msg.result:
|
|
emitted_content = True
|
|
yield self._chunk(
|
|
ChunkEnum.CONTENT,
|
|
session_id=msg.session_id or "",
|
|
chunk=msg.result,
|
|
)
|
|
for chunk in self._result_message_to_chunks(msg):
|
|
yield chunk
|
|
if reply_open or not emitted_reply_end:
|
|
emitted_reply_end = True
|
|
reply_open = False
|
|
yield self._chunk(
|
|
ChunkEnum.REPLY_END,
|
|
session_id=current_session_id,
|
|
chunk="",
|
|
)
|
|
|
|
elif isinstance(msg, AssistantMessage):
|
|
current_session_id = msg.session_id or current_session_id
|
|
for chunk in self._message_content_to_chunks(
|
|
msg,
|
|
current_session_id,
|
|
visible_tool_call_ids,
|
|
include_text=not emitted_content,
|
|
):
|
|
if chunk.chunk_type == ChunkEnum.CONTENT and chunk.chunk:
|
|
emitted_content = True
|
|
yield chunk
|
|
|
|
elif isinstance(msg, MirrorErrorMessage):
|
|
self.logger.warning(f"Claude Code session mirror failed: {msg.error}")
|
|
yield self._chunk(
|
|
ChunkEnum.DATA,
|
|
session_id=current_session_id,
|
|
chunk=f"Session mirror failed: {msg.error}",
|
|
metadata={
|
|
"event": "session_mirror_error",
|
|
"session_key": msg.key,
|
|
},
|
|
)
|
|
|
|
elif isinstance(msg, RateLimitEvent) and msg.rate_limit_info.status == "rejected":
|
|
yield self._chunk(
|
|
ChunkEnum.ERROR,
|
|
session_id=msg.session_id,
|
|
chunk="Rate limit exceeded",
|
|
)
|
|
except Exception as exc:
|
|
if expected_trailing_error is None or str(exc) != expected_trailing_error:
|
|
raise
|
|
self.logger.debug(f"Ignoring Claude Code process exit after error result: {exc}")
|