langchain-agentx-python 0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_agentx/__init__.py +46 -0
- langchain_agentx/command/__init__.py +28 -0
- langchain_agentx/command/builtin/__init__.py +25 -0
- langchain_agentx/command/builtin/clear.py +33 -0
- langchain_agentx/command/builtin/compact.py +33 -0
- langchain_agentx/command/builtin/memory.py +37 -0
- langchain_agentx/command/builtin/reload_plugins.py +42 -0
- langchain_agentx/command/context.py +30 -0
- langchain_agentx/command/dispatcher.py +183 -0
- langchain_agentx/command/registry.py +110 -0
- langchain_agentx/command/result.py +25 -0
- langchain_agentx/command/types.py +41 -0
- langchain_agentx/config/__init__.py +14 -0
- langchain_agentx/loop/__init__.py +47 -0
- langchain_agentx/loop/config/__init__.py +20 -0
- langchain_agentx/loop/config/agent_config.py +66 -0
- langchain_agentx/loop/config/agent_loop_config.py +72 -0
- langchain_agentx/loop/config/model_context_resolver.py +105 -0
- langchain_agentx/loop/config/runtime_settings.py +50 -0
- langchain_agentx/loop/config/token_estimator.py +133 -0
- langchain_agentx/loop/context/__init__.py +66 -0
- langchain_agentx/loop/context/blocking_guard.py +97 -0
- langchain_agentx/loop/context/compaction_service.py +60 -0
- langchain_agentx/loop/context/message_utils.py +56 -0
- langchain_agentx/loop/context/pipeline.py +127 -0
- langchain_agentx/loop/context/settings.py +103 -0
- langchain_agentx/loop/context/stages/__init__.py +29 -0
- langchain_agentx/loop/context/stages/autocompact.py +140 -0
- langchain_agentx/loop/context/stages/base.py +32 -0
- langchain_agentx/loop/context/stages/collapse.py +76 -0
- langchain_agentx/loop/context/stages/microcompact.py +76 -0
- langchain_agentx/loop/context/stages/noop.py +33 -0
- langchain_agentx/loop/context/stages/snip.py +71 -0
- langchain_agentx/loop/context/stages/tool_result_budget.py +69 -0
- langchain_agentx/loop/context/types.py +79 -0
- langchain_agentx/loop/exit/__init__.py +1 -0
- langchain_agentx/loop/exit/exit_logic.py +320 -0
- langchain_agentx/loop/exit/reason_codes.py +39 -0
- langchain_agentx/loop/graph/__init__.py +5 -0
- langchain_agentx/loop/graph/builtin_loop_control.py +197 -0
- langchain_agentx/loop/graph/factory.py +1409 -0
- langchain_agentx/loop/graph/graph_edges.py +820 -0
- langchain_agentx/loop/hook/__init__.py +48 -0
- langchain_agentx/loop/hook/async_hook_runner.py +62 -0
- langchain_agentx/loop/hook/config.py +280 -0
- langchain_agentx/loop/hook/engine.py +321 -0
- langchain_agentx/loop/hook/executors/__init__.py +9 -0
- langchain_agentx/loop/hook/executors/agent.py +107 -0
- langchain_agentx/loop/hook/executors/command.py +230 -0
- langchain_agentx/loop/hook/executors/http.py +114 -0
- langchain_agentx/loop/hook/executors/prompt.py +92 -0
- langchain_agentx/loop/hook/graph_wiring.py +134 -0
- langchain_agentx/loop/hook/registry.py +262 -0
- langchain_agentx/loop/hook/trust.py +43 -0
- langchain_agentx/loop/hook/types.py +110 -0
- langchain_agentx/loop/injection/__init__.py +13 -0
- langchain_agentx/loop/injection/dedup.py +74 -0
- langchain_agentx/loop/loop_abort.py +36 -0
- langchain_agentx/loop/model/__init__.py +1 -0
- langchain_agentx/loop/model/model_node.py +648 -0
- langchain_agentx/loop/model/model_nodes.py +661 -0
- langchain_agentx/loop/model/orphan_tool_results.py +38 -0
- langchain_agentx/loop/model/retrier.py +307 -0
- langchain_agentx/loop/model/retry_bridge.py +58 -0
- langchain_agentx/loop/model/retry_events.py +35 -0
- langchain_agentx/loop/model/retry_policy.py +56 -0
- langchain_agentx/loop/model/schema_and_format.py +153 -0
- langchain_agentx/loop/model/tool_and_model_binding.py +227 -0
- langchain_agentx/loop/model/tool_call_degradation_corrector.py +443 -0
- langchain_agentx/loop/model/tool_transcript_guard.py +225 -0
- langchain_agentx/loop/prompt/__init__.py +95 -0
- langchain_agentx/loop/prompt/builder.py +61 -0
- langchain_agentx/loop/prompt/builtin.py +218 -0
- langchain_agentx/loop/prompt/compact.py +408 -0
- langchain_agentx/loop/prompt/sections.py +120 -0
- langchain_agentx/loop/runtime/__init__.py +19 -0
- langchain_agentx/loop/runtime/context.py +34 -0
- langchain_agentx/loop/runtime/context_factory.py +107 -0
- langchain_agentx/loop/runtime/subagent_execution_paths.py +68 -0
- langchain_agentx/loop/subagent/__init__.py +53 -0
- langchain_agentx/loop/subagent/async_runner.py +215 -0
- langchain_agentx/loop/subagent/context.py +209 -0
- langchain_agentx/loop/subagent/fork_worktree_notice.py +25 -0
- langchain_agentx/loop/subagent/graph.py +72 -0
- langchain_agentx/loop/subagent/orchestrator.py +391 -0
- langchain_agentx/loop/subagent/progress.py +30 -0
- langchain_agentx/loop/subagent/prompt.py +52 -0
- langchain_agentx/loop/subagent/runner.py +504 -0
- langchain_agentx/loop/subagent/transcript.py +172 -0
- langchain_agentx/memory/__init__.py +2 -0
- langchain_agentx/memory/instruction/__init__.py +12 -0
- langchain_agentx/memory/instruction/loader.py +325 -0
- langchain_agentx/memory/instruction/resolver.py +24 -0
- langchain_agentx/memory/instruction/runtime.py +83 -0
- langchain_agentx/memory/instruction/sections.py +83 -0
- langchain_agentx/memory/instruction/types.py +59 -0
- langchain_agentx/memory/memdir/__init__.py +77 -0
- langchain_agentx/memory/memdir/age.py +36 -0
- langchain_agentx/memory/memdir/agent_memory.py +380 -0
- langchain_agentx/memory/memdir/extractor.py +309 -0
- langchain_agentx/memory/memdir/loader.py +187 -0
- langchain_agentx/memory/memdir/paths.py +63 -0
- langchain_agentx/memory/memdir/recall.py +45 -0
- langchain_agentx/memory/memdir/runtime.py +43 -0
- langchain_agentx/memory/memdir/scan.py +135 -0
- langchain_agentx/memory/memdir/types.py +104 -0
- langchain_agentx/memory/session/__init__.py +76 -0
- langchain_agentx/memory/session/compact_bridge.py +208 -0
- langchain_agentx/memory/session/prompts.py +172 -0
- langchain_agentx/memory/session/session_memory.py +282 -0
- langchain_agentx/observability/__init__.py +67 -0
- langchain_agentx/observability/evaluation/__init__.py +17 -0
- langchain_agentx/observability/evaluation/checkers/__init__.py +18 -0
- langchain_agentx/observability/evaluation/checkers/base.py +34 -0
- langchain_agentx/observability/evaluation/checkers/compaction.py +38 -0
- langchain_agentx/observability/evaluation/checkers/degradation.py +50 -0
- langchain_agentx/observability/evaluation/checkers/exit_quality.py +42 -0
- langchain_agentx/observability/evaluation/checkers/session_memory.py +45 -0
- langchain_agentx/observability/evaluation/checkers/tool_behavior.py +53 -0
- langchain_agentx/observability/evaluation/retention_scheduler.py +67 -0
- langchain_agentx/observability/evaluation/service.py +102 -0
- langchain_agentx/observability/evaluation/state.py +32 -0
- langchain_agentx/observability/evaluation/store.py +258 -0
- langchain_agentx/observability/events/__init__.py +15 -0
- langchain_agentx/observability/events/langchain_agentx_event_adapter.py +832 -0
- langchain_agentx/observability/logging/__init__.py +15 -0
- langchain_agentx/observability/logging/debug_burst.py +95 -0
- langchain_agentx/observability/logging/logging_config.py +178 -0
- langchain_agentx/observability/logging/logging_contract.py +65 -0
- langchain_agentx/observability/replay/__init__.py +35 -0
- langchain_agentx/observability/replay/cli.py +91 -0
- langchain_agentx/observability/replay/service.py +83 -0
- langchain_agentx/observability/replay/store.py +278 -0
- langchain_agentx/observability/replay/ui.py +47 -0
- langchain_agentx/observability/trace/__init__.py +25 -0
- langchain_agentx/observability/trace/collector.py +560 -0
- langchain_agentx/observability/trace/event_emitter.py +183 -0
- langchain_agentx/observability/trace/hook_event_emitter.py +49 -0
- langchain_agentx/observability/trace/models.py +144 -0
- langchain_agentx/observability/trace/sqlite_store.py +873 -0
- langchain_agentx/observability/trace/trace_callback.py +295 -0
- langchain_agentx/observability/trace/trace_lifecycle_collector.py +114 -0
- langchain_agentx/plugin/__init__.py +26 -0
- langchain_agentx/plugin/builtin.py +53 -0
- langchain_agentx/plugin/config.py +113 -0
- langchain_agentx/plugin/loader.py +386 -0
- langchain_agentx/plugin/manifest.py +154 -0
- langchain_agentx/plugin/registries.py +211 -0
- langchain_agentx/plugin/types.py +142 -0
- langchain_agentx/provider/__init__.py +27 -0
- langchain_agentx/provider/anthropic.py +121 -0
- langchain_agentx/provider/compatible_chat_openai.py +86 -0
- langchain_agentx/provider/env.py +45 -0
- langchain_agentx/provider/model_profile.py +156 -0
- langchain_agentx/provider/openai.py +89 -0
- langchain_agentx/session/__init__.py +17 -0
- langchain_agentx/session/agent_session.py +320 -0
- langchain_agentx/session/conversation_factory.py +87 -0
- langchain_agentx/session/conversation_recovery.py +156 -0
- langchain_agentx/session/conversation_session.py +198 -0
- langchain_agentx/session/factory.py +143 -0
- langchain_agentx/session/protocol.py +25 -0
- langchain_agentx/task_runtime/__init__.py +113 -0
- langchain_agentx/task_runtime/core/__init__.py +51 -0
- langchain_agentx/task_runtime/core/ids.py +33 -0
- langchain_agentx/task_runtime/core/interfaces.py +115 -0
- langchain_agentx/task_runtime/core/notification_priority.py +19 -0
- langchain_agentx/task_runtime/core/types.py +136 -0
- langchain_agentx/task_runtime/integrations/__init__.py +33 -0
- langchain_agentx/task_runtime/integrations/loop_adapter.py +91 -0
- langchain_agentx/task_runtime/integrations/loop_integration.py +61 -0
- langchain_agentx/task_runtime/integrations/prefetch_providers.py +108 -0
- langchain_agentx/task_runtime/integrations/provider_factory.py +103 -0
- langchain_agentx/task_runtime/integrations/queued_command_provider.py +184 -0
- langchain_agentx/task_runtime/integrations/sqlite_queued_command_provider.py +338 -0
- langchain_agentx/task_runtime/integrations/tool_use_summary_provider.py +254 -0
- langchain_agentx/task_runtime/orchestrator/__init__.py +5 -0
- langchain_agentx/task_runtime/orchestrator/runtime.py +386 -0
- langchain_agentx/task_runtime/output/__init__.py +5 -0
- langchain_agentx/task_runtime/output/sink.py +64 -0
- langchain_agentx/task_runtime/policy/__init__.py +11 -0
- langchain_agentx/task_runtime/policy/withhold_visibility.py +32 -0
- langchain_agentx/task_runtime/queue/__init__.py +5 -0
- langchain_agentx/task_runtime/queue/in_memory.py +55 -0
- langchain_agentx/task_runtime/skill_prefetch/__init__.py +4 -0
- langchain_agentx/task_runtime/skill_prefetch/attachments.py +46 -0
- langchain_agentx/task_runtime/skill_prefetch/models.py +37 -0
- langchain_agentx/task_runtime/skill_prefetch/provider.py +344 -0
- langchain_agentx/task_runtime/store/__init__.py +6 -0
- langchain_agentx/task_runtime/store/in_memory.py +81 -0
- langchain_agentx/task_runtime/store/sqlite_store.py +281 -0
- langchain_agentx/task_runtime/tasks/__init__.py +76 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/__init__.py +15 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/base.py +41 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/evaluation.py +67 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/registry.py +36 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/scheduler.py +70 -0
- langchain_agentx/task_runtime/tasks/base/__init__.py +6 -0
- langchain_agentx/task_runtime/tasks/base/contracts.py +24 -0
- langchain_agentx/task_runtime/tasks/custom/__init__.py +7 -0
- langchain_agentx/task_runtime/tasks/custom/executor.py +60 -0
- langchain_agentx/task_runtime/tasks/custom/notification.py +7 -0
- langchain_agentx/task_runtime/tasks/custom/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/custom/spec.py +33 -0
- langchain_agentx/task_runtime/tasks/dream_task/__init__.py +15 -0
- langchain_agentx/task_runtime/tasks/dream_task/executor.py +61 -0
- langchain_agentx/task_runtime/tasks/dream_task/notification.py +19 -0
- langchain_agentx/task_runtime/tasks/dream_task/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/dream_task/spec.py +35 -0
- langchain_agentx/task_runtime/tasks/dream_task/state.py +17 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/__init__.py +12 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/executor.py +36 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/notification.py +25 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/spec.py +63 -0
- langchain_agentx/task_runtime/tasks/local_agent/__init__.py +14 -0
- langchain_agentx/task_runtime/tasks/local_agent/executor.py +33 -0
- langchain_agentx/task_runtime/tasks/local_agent/notification.py +21 -0
- langchain_agentx/task_runtime/tasks/local_agent/runner.py +43 -0
- langchain_agentx/task_runtime/tasks/local_agent/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/local_agent/spec.py +31 -0
- langchain_agentx/task_runtime/tasks/local_bash/__init__.py +13 -0
- langchain_agentx/task_runtime/tasks/local_bash/executor.py +95 -0
- langchain_agentx/task_runtime/tasks/local_bash/notification.py +22 -0
- langchain_agentx/task_runtime/tasks/local_bash/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/local_bash/spec.py +55 -0
- langchain_agentx/task_runtime/tasks/remote_agent/__init__.py +19 -0
- langchain_agentx/task_runtime/tasks/remote_agent/backend.py +76 -0
- langchain_agentx/task_runtime/tasks/remote_agent/executor.py +37 -0
- langchain_agentx/task_runtime/tasks/remote_agent/notification.py +22 -0
- langchain_agentx/task_runtime/tasks/remote_agent/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/remote_agent/spec.py +34 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/__init__.py +19 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/bootstrap.py +95 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/executor.py +66 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/scheduler.py +169 -0
- langchain_agentx/tool_runtime/__init__.py +90 -0
- langchain_agentx/tool_runtime/adapter.py +365 -0
- langchain_agentx/tool_runtime/base.py +319 -0
- langchain_agentx/tool_runtime/errors.py +190 -0
- langchain_agentx/tool_runtime/identical_call_cache.py +110 -0
- langchain_agentx/tool_runtime/loader.py +195 -0
- langchain_agentx/tool_runtime/models.py +260 -0
- langchain_agentx/tool_runtime/permission_context.py +78 -0
- langchain_agentx/tool_runtime/pipeline.py +621 -0
- langchain_agentx/tool_runtime/policy.py +447 -0
- langchain_agentx/tool_runtime/registry.py +81 -0
- langchain_agentx/tool_runtime/resolvers/__init__.py +27 -0
- langchain_agentx/tool_runtime/resolvers/agent_session.py +125 -0
- langchain_agentx/tool_runtime/resolvers/background.py +32 -0
- langchain_agentx/tool_runtime/resolvers/base.py +20 -0
- langchain_agentx/tool_runtime/resolvers/conversation.py +22 -0
- langchain_agentx/tool_runtime/resolvers/workflow.py +73 -0
- langchain_agentx/tool_runtime/session_store.py +132 -0
- langchain_agentx/tool_runtime/smoke_test_runtime.py +294 -0
- langchain_agentx/tool_runtime/state_bridge.py +164 -0
- langchain_agentx/tools/__init__.py +26 -0
- langchain_agentx/tools/agent/__init__.py +9 -0
- langchain_agentx/tools/agent/backend.py +53 -0
- langchain_agentx/tools/agent/built_in/__init__.py +19 -0
- langchain_agentx/tools/agent/built_in/agentx_guide.py +65 -0
- langchain_agentx/tools/agent/built_in/explore.py +80 -0
- langchain_agentx/tools/agent/built_in/general.py +57 -0
- langchain_agentx/tools/agent/built_in/plan.py +89 -0
- langchain_agentx/tools/agent/built_in/statusline_setup.py +64 -0
- langchain_agentx/tools/agent/built_in/verification.py +120 -0
- langchain_agentx/tools/agent/builtin_subagent_loader.py +89 -0
- langchain_agentx/tools/agent/cwd_resolution.py +119 -0
- langchain_agentx/tools/agent/limits.py +26 -0
- langchain_agentx/tools/agent/loader.py +270 -0
- langchain_agentx/tools/agent/models.py +85 -0
- langchain_agentx/tools/agent/prompt.py +120 -0
- langchain_agentx/tools/agent/registry/__init__.py +18 -0
- langchain_agentx/tools/agent/registry/config.py +29 -0
- langchain_agentx/tools/agent/registry/registry.py +47 -0
- langchain_agentx/tools/agent/scope.py +137 -0
- langchain_agentx/tools/agent/tool.py +256 -0
- langchain_agentx/tools/bash/__init__.py +9 -0
- langchain_agentx/tools/bash/ast_security.py +571 -0
- langchain_agentx/tools/bash/backend.py +1447 -0
- langchain_agentx/tools/bash/bash_hardening.py +734 -0
- langchain_agentx/tools/bash/bash_runtime_contract.py +41 -0
- langchain_agentx/tools/bash/cwd_reporter.py +95 -0
- langchain_agentx/tools/bash/limits.py +71 -0
- langchain_agentx/tools/bash/mode_validation.py +282 -0
- langchain_agentx/tools/bash/models.py +131 -0
- langchain_agentx/tools/bash/observability.py +148 -0
- langchain_agentx/tools/bash/output_utils.py +200 -0
- langchain_agentx/tools/bash/path_security.py +2429 -0
- langchain_agentx/tools/bash/prompt.py +68 -0
- langchain_agentx/tools/bash/read_only_validation.py +589 -0
- langchain_agentx/tools/bash/result_presenter.py +324 -0
- langchain_agentx/tools/bash/sandbox_decision.py +133 -0
- langchain_agentx/tools/bash/security.py +311 -0
- langchain_agentx/tools/bash/sed_edit_parser.py +243 -0
- langchain_agentx/tools/bash/sed_validation.py +163 -0
- langchain_agentx/tools/bash/semantics.py +111 -0
- langchain_agentx/tools/bash/session_manager.py +205 -0
- langchain_agentx/tools/bash/session_runtime.py +290 -0
- langchain_agentx/tools/bash/shell_locator.py +191 -0
- langchain_agentx/tools/bash/task_runtime.py +91 -0
- langchain_agentx/tools/bash/tool.py +939 -0
- langchain_agentx/tools/bash/windows_shell_quoting.py +45 -0
- langchain_agentx/tools/glob/__init__.py +9 -0
- langchain_agentx/tools/glob/models.py +57 -0
- langchain_agentx/tools/glob/pagination.py +30 -0
- langchain_agentx/tools/glob/prompt.py +24 -0
- langchain_agentx/tools/glob/rg_list_backend.py +139 -0
- langchain_agentx/tools/glob/rg_pattern.py +44 -0
- langchain_agentx/tools/glob/tool.py +327 -0
- langchain_agentx/tools/grep/__init__.py +7 -0
- langchain_agentx/tools/grep/backend.py +375 -0
- langchain_agentx/tools/grep/models.py +127 -0
- langchain_agentx/tools/grep/prompt.py +30 -0
- langchain_agentx/tools/grep/rg_subprocess_controller.py +114 -0
- langchain_agentx/tools/grep/tool.py +475 -0
- langchain_agentx/tools/read/__init__.py +9 -0
- langchain_agentx/tools/read/backend.py +415 -0
- langchain_agentx/tools/read/limits.py +67 -0
- langchain_agentx/tools/read/models.py +156 -0
- langchain_agentx/tools/read/prompt.py +73 -0
- langchain_agentx/tools/read/tool.py +494 -0
- langchain_agentx/tools/ripgrep_plugin_exclusions.py +137 -0
- langchain_agentx/tools/skill/__init__.py +4 -0
- langchain_agentx/tools/skill/argument_substitution.py +80 -0
- langchain_agentx/tools/skill/loader.py +196 -0
- langchain_agentx/tools/skill/models.py +88 -0
- langchain_agentx/tools/skill/policy.py +80 -0
- langchain_agentx/tools/skill/prompt.py +35 -0
- langchain_agentx/tools/skill/tool.py +222 -0
- langchain_agentx/utils/__init__.py +0 -0
- langchain_agentx/utils/cwd.py +124 -0
- langchain_agentx/utils/host_platform.py +112 -0
- langchain_agentx/utils/path_hierarchy.py +48 -0
- langchain_agentx/utils/path_user_input.py +66 -0
- langchain_agentx/utils/rg_executable.py +18 -0
- langchain_agentx/utils/subprocess_text.py +101 -0
- langchain_agentx/utils/temp_paths.py +77 -0
- langchain_agentx/utils/unc_path.py +25 -0
- langchain_agentx/utils/win_reserved_paths.py +51 -0
- langchain_agentx/workflow/__init__.py +7 -0
- langchain_agentx/workflow/base.py +97 -0
- langchain_agentx/workflow/batch.py +55 -0
- langchain_agentx/workflow/dag.py +54 -0
- langchain_agentx/workspace/__init__.py +13 -0
- langchain_agentx/workspace/config.py +140 -0
- langchain_agentx/workspace/path_key_normalizer.py +30 -0
- langchain_agentx/workspace/resolver.py +74 -0
- langchain_agentx/workspace/validators.py +41 -0
- langchain_agentx_python-0.1.dist-info/LICENSE +201 -0
- langchain_agentx_python-0.1.dist-info/METADATA +513 -0
- langchain_agentx_python-0.1.dist-info/RECORD +354 -0
- langchain_agentx_python-0.1.dist-info/WHEEL +5 -0
- langchain_agentx_python-0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"""上下文压缩提示词(Compact Prompt)。
|
|
2
|
+
|
|
3
|
+
对齐 Claude Code `services/compact/prompt.ts`,提供:
|
|
4
|
+
- 压缩提示词模板(全量 / 部分 / up_to 三种变体)
|
|
5
|
+
- 工具调用抑制机制(NO_TOOLS_PREAMBLE / TRAILER)
|
|
6
|
+
- 摘要格式化(format_compact_summary)
|
|
7
|
+
- 用户续写消息包装(get_compact_user_summary_message)
|
|
8
|
+
|
|
9
|
+
设计要点:
|
|
10
|
+
- 多段提示的用途:NO_TOOLS_PREAMBLE + 主体模板(BASE / PARTIAL / PARTIAL_UP_TO)
|
|
11
|
+
+ 可选 Additional Instructions + NO_TOOLS_TRAILER 拼成一次 compact 调用的 system 侧全文
|
|
12
|
+
- partial 方向:direction="from" 对应「只总结近期片段」;"up_to" 对应「总结置于续写会话开头、
|
|
13
|
+
之后还有未展示的新消息」的措辞与第 8/9 节标题差异
|
|
14
|
+
- 后处理:format_compact_summary 去掉 <analysis>,把 <summary> 展平为 Summary:\\n...,
|
|
15
|
+
再交给 get_compact_user_summary_message 包一层用户可见续写说明
|
|
16
|
+
|
|
17
|
+
注意:
|
|
18
|
+
- 本模块无 I/O;与 CompactionSettings 解耦,供 settings 默认值与 AutocompactStage 后处理引用
|
|
19
|
+
- 从 loop/context/prompt.py 迁移到此处,统一提示词管理
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import re
|
|
25
|
+
from typing import Literal
|
|
26
|
+
|
|
27
|
+
PartialCompactDirection = Literal["from", "up_to"]
|
|
28
|
+
|
|
29
|
+
# Autocompact LLM 调用选哪套「主体模板」(对齐 compact.ts 全量 vs partial / up_to)
|
|
30
|
+
AutocompactLlmPromptKind = Literal["full", "partial_from", "partial_up_to"]
|
|
31
|
+
|
|
32
|
+
# Aggressive no-tools preamble (see TS 注释:maxTurns:1 时避免无效 tool 调用浪费轮次)
|
|
33
|
+
NO_TOOLS_PREAMBLE = """CRITICAL: Respond with TEXT ONLY. Do NOT call any tools.
|
|
34
|
+
|
|
35
|
+
- Do NOT use Read, Bash, Grep, Glob, Edit, Write, or ANY other tool.
|
|
36
|
+
- You already have all the context you need in the conversation above.
|
|
37
|
+
- Tool calls will be REJECTED and will waste your only turn — you will fail the task.
|
|
38
|
+
- Your entire response must be plain text: an <analysis> block followed by a <summary> block.
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
DETAILED_ANALYSIS_INSTRUCTION_BASE = """Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
|
|
43
|
+
|
|
44
|
+
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
|
45
|
+
- The user's explicit requests and intents
|
|
46
|
+
- Your approach to addressing the user's requests
|
|
47
|
+
- Key decisions, technical concepts and code patterns
|
|
48
|
+
- Specific details like:
|
|
49
|
+
- file names
|
|
50
|
+
- full code snippets
|
|
51
|
+
- function signatures
|
|
52
|
+
- file edits
|
|
53
|
+
- Errors that you ran into and how you fixed them
|
|
54
|
+
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
55
|
+
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly."""
|
|
56
|
+
|
|
57
|
+
DETAILED_ANALYSIS_INSTRUCTION_PARTIAL = """Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
|
|
58
|
+
|
|
59
|
+
1. Analyze the recent messages chronologically. For each section thoroughly identify:
|
|
60
|
+
- The user's explicit requests and intents
|
|
61
|
+
- Your approach to addressing the user's requests
|
|
62
|
+
- Key decisions, technical concepts and code patterns
|
|
63
|
+
- Specific details like:
|
|
64
|
+
- file names
|
|
65
|
+
- full code snippets
|
|
66
|
+
- function signatures
|
|
67
|
+
- file edits
|
|
68
|
+
- Errors that you ran into and how you fixed them
|
|
69
|
+
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
70
|
+
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly."""
|
|
71
|
+
|
|
72
|
+
BASE_COMPACT_PROMPT = f"""Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
|
|
73
|
+
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
|
|
74
|
+
|
|
75
|
+
{DETAILED_ANALYSIS_INSTRUCTION_BASE}
|
|
76
|
+
|
|
77
|
+
Your summary should include the following sections:
|
|
78
|
+
|
|
79
|
+
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
|
80
|
+
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
|
81
|
+
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
|
|
82
|
+
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
83
|
+
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
84
|
+
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
|
|
85
|
+
7. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
|
86
|
+
8. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
|
|
87
|
+
9. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
|
|
88
|
+
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
|
|
89
|
+
|
|
90
|
+
Here's an example of how your output should be structured:
|
|
91
|
+
|
|
92
|
+
<example>
|
|
93
|
+
<analysis>
|
|
94
|
+
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
95
|
+
</analysis>
|
|
96
|
+
|
|
97
|
+
<summary>
|
|
98
|
+
1. Primary Request and Intent:
|
|
99
|
+
[Detailed description]
|
|
100
|
+
|
|
101
|
+
2. Key Technical Concepts:
|
|
102
|
+
- [Concept 1]
|
|
103
|
+
- [Concept 2]
|
|
104
|
+
- [...]
|
|
105
|
+
|
|
106
|
+
3. Files and Code Sections:
|
|
107
|
+
- [File Name 1]
|
|
108
|
+
- [Summary of why this file is important]
|
|
109
|
+
- [Summary of the changes made to this file, if any]
|
|
110
|
+
- [Important Code Snippet]
|
|
111
|
+
- [File Name 2]
|
|
112
|
+
- [Important Code Snippet]
|
|
113
|
+
- [...]
|
|
114
|
+
|
|
115
|
+
4. Errors and fixes:
|
|
116
|
+
- [Detailed description of error 1]:
|
|
117
|
+
- [How you fixed the error]
|
|
118
|
+
- [User feedback on the error if any]
|
|
119
|
+
- [...]
|
|
120
|
+
|
|
121
|
+
5. Problem Solving:
|
|
122
|
+
[Description of solved problems and ongoing troubleshooting]
|
|
123
|
+
|
|
124
|
+
6. All user messages:
|
|
125
|
+
- [Detailed non tool use user message]
|
|
126
|
+
- [...]
|
|
127
|
+
|
|
128
|
+
7. Pending Tasks:
|
|
129
|
+
- [Task 1]
|
|
130
|
+
- [Task 2]
|
|
131
|
+
- [...]
|
|
132
|
+
|
|
133
|
+
8. Current Work:
|
|
134
|
+
[Precise description of current work]
|
|
135
|
+
|
|
136
|
+
9. Optional Next Step:
|
|
137
|
+
[Optional Next step to take]
|
|
138
|
+
|
|
139
|
+
</summary>
|
|
140
|
+
</example>
|
|
141
|
+
|
|
142
|
+
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
|
|
143
|
+
|
|
144
|
+
There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
|
|
145
|
+
<example>
|
|
146
|
+
## Compact Instructions
|
|
147
|
+
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
|
|
148
|
+
</example>
|
|
149
|
+
|
|
150
|
+
<example>
|
|
151
|
+
# Summary instructions
|
|
152
|
+
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
|
|
153
|
+
</example>
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
PARTIAL_COMPACT_PROMPT = f"""Your task is to create a detailed summary of the RECENT portion of the conversation — the messages that follow earlier retained context. The earlier messages are being kept intact and do NOT need to be summarized. Focus your summary on what was discussed, learned, and accomplished in the recent messages only.
|
|
157
|
+
|
|
158
|
+
{DETAILED_ANALYSIS_INSTRUCTION_PARTIAL}
|
|
159
|
+
|
|
160
|
+
Your summary should include the following sections:
|
|
161
|
+
|
|
162
|
+
1. Primary Request and Intent: Capture the user's explicit requests and intents from the recent messages
|
|
163
|
+
2. Key Technical Concepts: List important technical concepts, technologies, and frameworks discussed recently.
|
|
164
|
+
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Include full code snippets where applicable and include a summary of why this file read or edit is important.
|
|
165
|
+
4. Errors and fixes: List errors encountered and how they were fixed.
|
|
166
|
+
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
167
|
+
6. All user messages: List ALL user messages from the recent portion that are not tool results.
|
|
168
|
+
7. Pending Tasks: Outline any pending tasks from the recent messages.
|
|
169
|
+
8. Current Work: Describe precisely what was being worked on immediately before this summary request.
|
|
170
|
+
9. Optional Next Step: List the next step related to the most recent work. Include direct quotes from the most recent conversation.
|
|
171
|
+
|
|
172
|
+
Here's an example of how your output should be structured:
|
|
173
|
+
|
|
174
|
+
<example>
|
|
175
|
+
<analysis>
|
|
176
|
+
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
177
|
+
</analysis>
|
|
178
|
+
|
|
179
|
+
<summary>
|
|
180
|
+
1. Primary Request and Intent:
|
|
181
|
+
[Detailed description]
|
|
182
|
+
|
|
183
|
+
2. Key Technical Concepts:
|
|
184
|
+
- [Concept 1]
|
|
185
|
+
- [Concept 2]
|
|
186
|
+
|
|
187
|
+
3. Files and Code Sections:
|
|
188
|
+
- [File Name 1]
|
|
189
|
+
- [Summary of why this file is important]
|
|
190
|
+
- [Important Code Snippet]
|
|
191
|
+
|
|
192
|
+
4. Errors and fixes:
|
|
193
|
+
- [Error description]:
|
|
194
|
+
- [How you fixed it]
|
|
195
|
+
|
|
196
|
+
5. Problem Solving:
|
|
197
|
+
[Description]
|
|
198
|
+
|
|
199
|
+
6. All user messages:
|
|
200
|
+
- [Detailed non tool use user message]
|
|
201
|
+
|
|
202
|
+
7. Pending Tasks:
|
|
203
|
+
- [Task 1]
|
|
204
|
+
|
|
205
|
+
8. Current Work:
|
|
206
|
+
[Precise description of current work]
|
|
207
|
+
|
|
208
|
+
9. Optional Next Step:
|
|
209
|
+
[Optional Next step to take]
|
|
210
|
+
|
|
211
|
+
</summary>
|
|
212
|
+
</example>
|
|
213
|
+
|
|
214
|
+
Please provide your summary based on the RECENT messages only (after the retained earlier context), following this structure and ensuring precision and thoroughness in your response.
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
PARTIAL_COMPACT_UP_TO_PROMPT = f"""Your task is to create a detailed summary of this conversation. This summary will be placed at the start of a continuing session; newer messages that build on this context will follow after your summary (you do not see them here). Summarize thoroughly so that someone reading only your summary and then the newer messages can fully understand what happened and continue the work.
|
|
218
|
+
|
|
219
|
+
{DETAILED_ANALYSIS_INSTRUCTION_BASE}
|
|
220
|
+
|
|
221
|
+
Your summary should include the following sections:
|
|
222
|
+
|
|
223
|
+
1. Primary Request and Intent: Capture the user's explicit requests and intents in detail
|
|
224
|
+
2. Key Technical Concepts: List important technical concepts, technologies, and frameworks discussed.
|
|
225
|
+
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Include full code snippets where applicable and include a summary of why this file read or edit is important.
|
|
226
|
+
4. Errors and fixes: List errors encountered and how they were fixed.
|
|
227
|
+
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
228
|
+
6. All user messages: List ALL user messages that are not tool results.
|
|
229
|
+
7. Pending Tasks: Outline any pending tasks.
|
|
230
|
+
8. Work Completed: Describe what was accomplished by the end of this portion.
|
|
231
|
+
9. Context for Continuing Work: Summarize any context, decisions, or state that would be needed to understand and continue the work in subsequent messages.
|
|
232
|
+
|
|
233
|
+
Here's an example of how your output should be structured:
|
|
234
|
+
|
|
235
|
+
<example>
|
|
236
|
+
<analysis>
|
|
237
|
+
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
238
|
+
</analysis>
|
|
239
|
+
|
|
240
|
+
<summary>
|
|
241
|
+
1. Primary Request and Intent:
|
|
242
|
+
[Detailed description]
|
|
243
|
+
|
|
244
|
+
2. Key Technical Concepts:
|
|
245
|
+
- [Concept 1]
|
|
246
|
+
- [Concept 2]
|
|
247
|
+
|
|
248
|
+
3. Files and Code Sections:
|
|
249
|
+
- [File Name 1]
|
|
250
|
+
- [Summary of why this file is important]
|
|
251
|
+
- [Important Code Snippet]
|
|
252
|
+
|
|
253
|
+
4. Errors and fixes:
|
|
254
|
+
- [Error description]:
|
|
255
|
+
- [How you fixed it]
|
|
256
|
+
|
|
257
|
+
5. Problem Solving:
|
|
258
|
+
[Description]
|
|
259
|
+
|
|
260
|
+
6. All user messages:
|
|
261
|
+
- [Detailed non tool use user message]
|
|
262
|
+
|
|
263
|
+
7. Pending Tasks:
|
|
264
|
+
- [Task 1]
|
|
265
|
+
|
|
266
|
+
8. Work Completed:
|
|
267
|
+
[Description of what was accomplished]
|
|
268
|
+
|
|
269
|
+
9. Context for Continuing Work:
|
|
270
|
+
[Key context, decisions, or state needed to continue the work]
|
|
271
|
+
|
|
272
|
+
</summary>
|
|
273
|
+
</example>
|
|
274
|
+
|
|
275
|
+
Please provide your summary following this structure, ensuring precision and thoroughness in your response.
|
|
276
|
+
"""
|
|
277
|
+
|
|
278
|
+
NO_TOOLS_TRAILER = (
|
|
279
|
+
"\n\nREMINDER: Do NOT call any tools. Respond with plain text only — "
|
|
280
|
+
"an <analysis> block followed by a <summary> block. "
|
|
281
|
+
"Tool calls will be rejected and you will fail the task."
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def get_partial_compact_prompt(
|
|
286
|
+
custom_instructions: str | None = None,
|
|
287
|
+
direction: PartialCompactDirection = "from",
|
|
288
|
+
) -> str:
|
|
289
|
+
"""部分压缩提示词(对齐 getPartialCompactPrompt)。
|
|
290
|
+
|
|
291
|
+
Args:
|
|
292
|
+
custom_instructions: 用户自定义压缩指令(可选)
|
|
293
|
+
direction: "from" 只总结近期片段,"up_to" 摘要置于续写开头
|
|
294
|
+
"""
|
|
295
|
+
template = PARTIAL_COMPACT_UP_TO_PROMPT if direction == "up_to" else PARTIAL_COMPACT_PROMPT
|
|
296
|
+
prompt = NO_TOOLS_PREAMBLE + template
|
|
297
|
+
if custom_instructions and custom_instructions.strip() != "":
|
|
298
|
+
prompt += f"\n\nAdditional Instructions:\n{custom_instructions}"
|
|
299
|
+
prompt += NO_TOOLS_TRAILER
|
|
300
|
+
return prompt
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def get_compact_prompt(custom_instructions: str | None = None) -> str:
|
|
304
|
+
"""全量会话紧凑化用的系统侧提示(对齐 getCompactPrompt)。
|
|
305
|
+
|
|
306
|
+
Args:
|
|
307
|
+
custom_instructions: 用户自定义压缩指令(可选)
|
|
308
|
+
"""
|
|
309
|
+
prompt = NO_TOOLS_PREAMBLE + BASE_COMPACT_PROMPT
|
|
310
|
+
if custom_instructions and custom_instructions.strip() != "":
|
|
311
|
+
prompt += f"\n\nAdditional Instructions:\n{custom_instructions}"
|
|
312
|
+
prompt += NO_TOOLS_TRAILER
|
|
313
|
+
return prompt
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def build_autocompact_llm_instruction_prompt(
|
|
317
|
+
kind: AutocompactLlmPromptKind,
|
|
318
|
+
custom_instructions: str | None = None,
|
|
319
|
+
) -> str:
|
|
320
|
+
"""按 CC compact.ts 分支选择 getCompactPrompt 或 getPartialCompactPrompt 的完整指令串。
|
|
321
|
+
|
|
322
|
+
Args:
|
|
323
|
+
kind: 压缩类型
|
|
324
|
+
- "full": 全量 compact,对齐 getCompactPrompt(customInstructions)
|
|
325
|
+
- "partial_from": 只总结「保留前缀之后的近期」,对齐 getPartialCompactPrompt(..., 'from')
|
|
326
|
+
- "partial_up_to": 摘要置于续写开头、其后还有未展示消息,对齐 getPartialCompactPrompt(..., 'up_to')
|
|
327
|
+
custom_instructions: 用户自定义压缩指令(可选)
|
|
328
|
+
"""
|
|
329
|
+
if kind == "full":
|
|
330
|
+
return get_compact_prompt(custom_instructions)
|
|
331
|
+
if kind == "partial_from":
|
|
332
|
+
return get_partial_compact_prompt(custom_instructions, "from")
|
|
333
|
+
return get_partial_compact_prompt(custom_instructions, "up_to")
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def format_compact_summary(summary: str) -> str:
|
|
337
|
+
"""去掉 <analysis>,将 <summary> 展平为 Summary: 前缀(对齐 formatCompactSummary)。
|
|
338
|
+
|
|
339
|
+
Args:
|
|
340
|
+
summary: 模型原始输出(包含 <analysis> 和 <summary> 标签)
|
|
341
|
+
|
|
342
|
+
Returns:
|
|
343
|
+
格式化后的摘要文本
|
|
344
|
+
"""
|
|
345
|
+
formatted = re.sub(r"<analysis>[\s\S]*?</analysis>", "", summary)
|
|
346
|
+
m = re.search(r"<summary>([\s\S]*?)</summary>", formatted)
|
|
347
|
+
if m:
|
|
348
|
+
content = m.group(1) or ""
|
|
349
|
+
formatted = re.sub(
|
|
350
|
+
r"<summary>[\s\S]*?</summary>",
|
|
351
|
+
f"Summary:\n{content.strip()}",
|
|
352
|
+
formatted,
|
|
353
|
+
count=1,
|
|
354
|
+
)
|
|
355
|
+
formatted = re.sub(r"\n\n+", "\n\n", formatted)
|
|
356
|
+
return formatted.strip()
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def get_compact_user_summary_message(
|
|
360
|
+
summary: str,
|
|
361
|
+
suppress_follow_up_questions: bool = False,
|
|
362
|
+
transcript_path: str | None = None,
|
|
363
|
+
recent_messages_preserved: bool = False,
|
|
364
|
+
) -> str:
|
|
365
|
+
"""将模型原始输出格式化为「续写会话」用户可见说明(对齐 getCompactUserSummaryMessage)。
|
|
366
|
+
|
|
367
|
+
Args:
|
|
368
|
+
summary: 模型原始输出
|
|
369
|
+
suppress_follow_up_questions: 是否抑制追问(非交互续写模式)
|
|
370
|
+
transcript_path: 完整 transcript 文件路径(可选)
|
|
371
|
+
recent_messages_preserved: 是否保留了最近消息(partial 模式)
|
|
372
|
+
|
|
373
|
+
Returns:
|
|
374
|
+
用户可见的续写说明文本
|
|
375
|
+
"""
|
|
376
|
+
formatted_summary = format_compact_summary(summary)
|
|
377
|
+
base_summary = f"""This session is being continued from a previous conversation that ran out of context. The summary below covers the earlier portion of the conversation.
|
|
378
|
+
|
|
379
|
+
{formatted_summary}"""
|
|
380
|
+
if transcript_path:
|
|
381
|
+
base_summary += (
|
|
382
|
+
"\n\nIf you need specific details from before compaction (like exact code snippets, "
|
|
383
|
+
f"error messages, or content you generated), read the full transcript at: {transcript_path}"
|
|
384
|
+
)
|
|
385
|
+
if recent_messages_preserved:
|
|
386
|
+
base_summary += "\n\nRecent messages are preserved verbatim."
|
|
387
|
+
if suppress_follow_up_questions:
|
|
388
|
+
return f"""{base_summary}
|
|
389
|
+
Continue the conversation from where it left off without asking the user any further questions. Resume directly — do not acknowledge the summary, do not recap what was happening, do not preface with "I'll continue" or similar. Pick up the last task as if the break never happened."""
|
|
390
|
+
return base_summary
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
__all__ = [
|
|
394
|
+
"AutocompactLlmPromptKind",
|
|
395
|
+
"BASE_COMPACT_PROMPT",
|
|
396
|
+
"DETAILED_ANALYSIS_INSTRUCTION_BASE",
|
|
397
|
+
"DETAILED_ANALYSIS_INSTRUCTION_PARTIAL",
|
|
398
|
+
"NO_TOOLS_PREAMBLE",
|
|
399
|
+
"NO_TOOLS_TRAILER",
|
|
400
|
+
"PARTIAL_COMPACT_PROMPT",
|
|
401
|
+
"PARTIAL_COMPACT_UP_TO_PROMPT",
|
|
402
|
+
"PartialCompactDirection",
|
|
403
|
+
"build_autocompact_llm_instruction_prompt",
|
|
404
|
+
"format_compact_summary",
|
|
405
|
+
"get_compact_prompt",
|
|
406
|
+
"get_compact_user_summary_message",
|
|
407
|
+
"get_partial_compact_prompt",
|
|
408
|
+
]
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""SystemPromptSection — session 级提示词段注册表。
|
|
2
|
+
|
|
3
|
+
对标 Claude Code:
|
|
4
|
+
- systemPromptSection() → section()
|
|
5
|
+
- DANGEROUS_uncachedSystemPromptSection() → volatile_section()
|
|
6
|
+
- resolveSystemPromptSections() → resolve_sections()
|
|
7
|
+
|
|
8
|
+
和 CC 的核心差异:
|
|
9
|
+
- 不包含任何 cache_control / cacheScope 逻辑(本工程使用 OpenAI 兼容接口,
|
|
10
|
+
Anthropic 服务端 Prompt Cache 对本工程无效,详见
|
|
11
|
+
docs/experience/prompt-system-and-cache-experience-2026-04-13.md)
|
|
12
|
+
- 缓存存于调用方传入的 dict(来自 AgentState.section_cache),
|
|
13
|
+
生命周期与 session 绑定,session 结束时随 state 销毁。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from dataclasses import dataclass
|
|
19
|
+
from typing import Callable
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class SystemPromptSection:
|
|
24
|
+
"""可组合的系统提示词段。
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
name: 段的唯一标识名(同一 session 内作为 cache key)。
|
|
28
|
+
compute: 计算段内容的函数,返回 str 时输出该段,返回 None 时跳过该段。
|
|
29
|
+
volatile: False(默认)= session 内只计算一次,结果存入 section_cache 复用;
|
|
30
|
+
True = 每轮都重新调用 compute(),不读也不写 cache。
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
name: str
|
|
34
|
+
compute: Callable[[], str | None]
|
|
35
|
+
volatile: bool = False
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def section(
|
|
39
|
+
name: str,
|
|
40
|
+
compute: Callable[[], str | None],
|
|
41
|
+
) -> SystemPromptSection:
|
|
42
|
+
"""创建 session 级缓存的提示词段。
|
|
43
|
+
|
|
44
|
+
第一次调用 compute() 后,结果存入调用方传入的 section_cache dict;
|
|
45
|
+
同一 session 的后续轮次直接读缓存,不再重新计算。
|
|
46
|
+
|
|
47
|
+
适用场景:
|
|
48
|
+
- 读取环境变量、系统信息(平台、Python 版本等)
|
|
49
|
+
- 加载配置文件、用户偏好
|
|
50
|
+
- 语言偏好、固定的工具使用说明
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
name: 段的唯一标识名,用作 section_cache 的 key。
|
|
54
|
+
compute: 计算段内容的函数,返回 str 或 None(None 表示不输出该段)。
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
volatile=False 的 SystemPromptSection。
|
|
58
|
+
"""
|
|
59
|
+
return SystemPromptSection(name=name, compute=compute, volatile=False)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def volatile_section(
|
|
63
|
+
name: str,
|
|
64
|
+
compute: Callable[[], str | None],
|
|
65
|
+
reason: str,
|
|
66
|
+
) -> SystemPromptSection:
|
|
67
|
+
"""创建每轮重算的提示词段。
|
|
68
|
+
|
|
69
|
+
每次进入 model 节点前都重新调用 compute(),不读也不写 section_cache。
|
|
70
|
+
``reason`` 参数不影响运行逻辑,仅作代码意图标注,
|
|
71
|
+
便于审查时快速定位"为什么这里不能缓存"。
|
|
72
|
+
|
|
73
|
+
适用场景:
|
|
74
|
+
- 当前时间戳
|
|
75
|
+
- 会话中途动态加入/移除的工具列表
|
|
76
|
+
- 任何每轮都可能变化的外部状态
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
name: 段的唯一标识名(volatile 段不使用 cache,但 name 仍用于调试日志)。
|
|
80
|
+
compute: 计算段内容的函数,返回 str 或 None。
|
|
81
|
+
reason: 必须说明为何不能缓存(审查用,不影响运行)。
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
volatile=True 的 SystemPromptSection。
|
|
85
|
+
"""
|
|
86
|
+
return SystemPromptSection(name=name, compute=compute, volatile=True)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def resolve_sections(
|
|
90
|
+
sections: list[SystemPromptSection],
|
|
91
|
+
cache: dict[str, str | None],
|
|
92
|
+
) -> list[str]:
|
|
93
|
+
"""按顺序解析 sections,返回所有非 None 的文本列表。
|
|
94
|
+
|
|
95
|
+
缓存策略:
|
|
96
|
+
- volatile=False:优先读 cache;miss 时调用 compute() 并写入 cache。
|
|
97
|
+
- volatile=True :每次都调用 compute(),不读也不写 cache。
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
sections: 要解析的段列表,按列表顺序输出。
|
|
101
|
+
cache: 来自 AgentState.section_cache 的字典,session 生命周期。
|
|
102
|
+
函数会原地修改 cache(写入非 volatile 段的计算结果)。
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
所有 compute() 返回非 None 值的文本列表,顺序与 sections 一致。
|
|
106
|
+
"""
|
|
107
|
+
results: list[str] = []
|
|
108
|
+
for s in sections:
|
|
109
|
+
if s.volatile:
|
|
110
|
+
val = s.compute()
|
|
111
|
+
elif s.name not in cache:
|
|
112
|
+
cache[s.name] = s.compute()
|
|
113
|
+
val = cache[s.name]
|
|
114
|
+
else:
|
|
115
|
+
val = cache[s.name]
|
|
116
|
+
|
|
117
|
+
if val is not None:
|
|
118
|
+
results.append(val)
|
|
119
|
+
|
|
120
|
+
return results
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
loop/runtime/__init__.py — loop.runtime 包公共接口
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .context import AgentLoopConfig, SubagentContextOverrides
|
|
6
|
+
from .context_factory import RuntimeContextFactory
|
|
7
|
+
from .subagent_execution_paths import PreparedSubagentPaths, SubagentExecutionPaths
|
|
8
|
+
|
|
9
|
+
# 向后兼容:历史类型名与 AgentLoopConfig 为同一实现
|
|
10
|
+
LoopRuntimeContext = AgentLoopConfig
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"AgentLoopConfig",
|
|
14
|
+
"LoopRuntimeContext",
|
|
15
|
+
"SubagentContextOverrides",
|
|
16
|
+
"RuntimeContextFactory",
|
|
17
|
+
"PreparedSubagentPaths",
|
|
18
|
+
"SubagentExecutionPaths",
|
|
19
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""
|
|
2
|
+
loop/runtime/context.py — 子代理运行态控制面数据结构
|
|
3
|
+
|
|
4
|
+
职责:从 `loop.config` 再导出 `AgentLoopConfig`(已合并原 `LoopRuntimeContext` 全部字段);
|
|
5
|
+
以及 `SubagentContextOverrides`(子代理显式覆盖参数)。
|
|
6
|
+
在整体链路中的位置:RuntimeContextFactory.derive() 消费,SubagentOrchestrator 传递给 runner。
|
|
7
|
+
CC 对照:src/query.ts State.toolUseContext / src/utils/forkedAgent.ts createSubagentContext。
|
|
8
|
+
当前裁剪范围:不变配置强类型统一为 `AgentLoopConfig`;历史名 `LoopRuntimeContext` 由
|
|
9
|
+
`loop.runtime` 包以别名导出以保持向后兼容。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from ..config import AgentLoopConfig
|
|
18
|
+
|
|
19
|
+
__all__ = ["AgentLoopConfig", "SubagentContextOverrides"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class SubagentContextOverrides:
|
|
24
|
+
"""子代理显式覆盖参数。
|
|
25
|
+
|
|
26
|
+
None 表示"不覆盖,继承父值"。
|
|
27
|
+
CC 对照:src/utils/forkedAgent.ts forkOptions 参数。
|
|
28
|
+
"""
|
|
29
|
+
model: Any | None = None
|
|
30
|
+
max_steps: int | None = None
|
|
31
|
+
available_tools: list[Any] | None = None
|
|
32
|
+
permission_mode: str | None = None
|
|
33
|
+
isolation: str = "none"
|
|
34
|
+
cwd: str | None = None
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""
|
|
2
|
+
loop/runtime/context_factory.py — RuntimeContextFactory 继承规则单点
|
|
3
|
+
|
|
4
|
+
职责:从父 ToolExecutionContext 派生 AgentLoopConfig,应用 SubagentContextOverrides。
|
|
5
|
+
继承优先级(单点定义):显式 override > SubagentConfig 默认 > 父 ctx 字段。
|
|
6
|
+
在整体链路中的位置:SubagentOrchestrator.invoke_subagent() 调用。
|
|
7
|
+
CC 对照:src/utils/forkedAgent.ts createSubagentContext() 中的字段合并逻辑。
|
|
8
|
+
当前裁剪范围:v1 model / max_steps / available_tools / agent_depth 继承;permission_mode 直通;
|
|
9
|
+
``subagent_effective_cwd`` 由 ``SubagentExecutionPaths.prepare`` 注入(不在此 mkdir)。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import TYPE_CHECKING, Any
|
|
16
|
+
|
|
17
|
+
from langchain_agentx.workspace import resolve_agent_workspace_config
|
|
18
|
+
|
|
19
|
+
from .context import AgentLoopConfig, SubagentContextOverrides
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
from langchain_agentx.tool_runtime.models import ToolExecutionContext
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class RuntimeContextFactory:
|
|
26
|
+
"""子代理运行态上下文派生器。
|
|
27
|
+
|
|
28
|
+
继承优先级(所有字段统一在此定义,不允许在别处散落推断):
|
|
29
|
+
显式 SubagentContextOverrides 字段
|
|
30
|
+
> SubagentConfig.default_xxx
|
|
31
|
+
> 父 ToolExecutionContext 字段
|
|
32
|
+
> 系统默认值
|
|
33
|
+
|
|
34
|
+
CC 对照:createSubagentContext() 的参数合并顺序。
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def derive(
|
|
39
|
+
parent_ctx: "ToolExecutionContext",
|
|
40
|
+
overrides: SubagentContextOverrides,
|
|
41
|
+
*,
|
|
42
|
+
subagent_config: Any | None = None,
|
|
43
|
+
agent_id: str | None = None,
|
|
44
|
+
subagent_effective_cwd: str | None = None,
|
|
45
|
+
) -> AgentLoopConfig:
|
|
46
|
+
"""从父 ctx 派生子代理 AgentLoopConfig。
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
parent_ctx: 父会话工具执行上下文(含 model / agent_depth 等控制面字段)。
|
|
50
|
+
overrides: 子代理显式覆盖(None 字段表示继承)。
|
|
51
|
+
subagent_config: SubagentConfig 实例,提供 default_model / default_max_steps。
|
|
52
|
+
agent_id: 子代理 ID(调用方生成)。
|
|
53
|
+
subagent_effective_cwd: 由 ``SubagentExecutionPaths.prepare`` 解析的最终 cwd;
|
|
54
|
+
非 ``None`` 时写入 ``AgentLoopConfig.cwd``;为 ``None`` 时维持历史行为
|
|
55
|
+
``parent_ctx.cwd``(兼容未接 Agent 编排的调用方)。
|
|
56
|
+
"""
|
|
57
|
+
# 模型继承:override > config.default_model > parent_ctx.model
|
|
58
|
+
config_model = getattr(subagent_config, "default_model", None)
|
|
59
|
+
resolved_model = overrides.model or config_model or parent_ctx.model
|
|
60
|
+
|
|
61
|
+
# max_steps 继承:override > config.default_max_steps > None
|
|
62
|
+
config_max_steps = getattr(subagent_config, "default_max_steps", None)
|
|
63
|
+
resolved_max_steps = (
|
|
64
|
+
overrides.max_steps if overrides.max_steps is not None
|
|
65
|
+
else config_max_steps
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# 工具池继承:override > 父 ctx.available_tools
|
|
69
|
+
resolved_tools = (
|
|
70
|
+
overrides.available_tools
|
|
71
|
+
if overrides.available_tools is not None
|
|
72
|
+
else list(parent_ctx.available_tools or [])
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# 权限:override > 父 ctx.permission_mode > "default"
|
|
76
|
+
resolved_permission = (
|
|
77
|
+
overrides.permission_mode
|
|
78
|
+
or getattr(parent_ctx, "permission_mode", "default")
|
|
79
|
+
or "default"
|
|
80
|
+
)
|
|
81
|
+
workspace_root = getattr(parent_ctx, "workspace_root", None)
|
|
82
|
+
parent_agent_home = getattr(parent_ctx, "agent_home", None)
|
|
83
|
+
workspace_cfg = resolve_agent_workspace_config(
|
|
84
|
+
workspace_root=workspace_root or Path.cwd(),
|
|
85
|
+
agent_home=parent_agent_home,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
resolved_cwd = (
|
|
89
|
+
subagent_effective_cwd
|
|
90
|
+
if subagent_effective_cwd is not None
|
|
91
|
+
else getattr(parent_ctx, "cwd", None)
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
return AgentLoopConfig(
|
|
95
|
+
model=resolved_model,
|
|
96
|
+
session_id=getattr(parent_ctx, "session_id", "") or "",
|
|
97
|
+
agent_id=agent_id,
|
|
98
|
+
parent_agent_id=getattr(parent_ctx, "agent_id", None),
|
|
99
|
+
agent_depth=(parent_ctx.agent_depth or 0) + 1,
|
|
100
|
+
max_steps=resolved_max_steps,
|
|
101
|
+
available_tools=resolved_tools,
|
|
102
|
+
permission_mode=resolved_permission,
|
|
103
|
+
workspace_root=workspace_cfg.workspace_root,
|
|
104
|
+
agent_home=workspace_cfg.agent_home,
|
|
105
|
+
cwd=resolved_cwd,
|
|
106
|
+
trace_enabled=bool(getattr(parent_ctx, "trace_enabled", True)),
|
|
107
|
+
)
|