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,148 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tools/bash/observability.py — BashRuntimeTool 可观测性协议与多端投影器
|
|
3
|
+
|
|
4
|
+
目标:
|
|
5
|
+
- 标准化 decision trace / execution events schema(P1)
|
|
6
|
+
- 统一 trace_context(thread/run/tool_call)
|
|
7
|
+
- 供 UI/CLI/日志消费者做稳定投影(Phase 5)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import time
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from langchain_agentx.tool_runtime.models import ToolExecutionContext
|
|
17
|
+
from langchain_agentx.observability.replay.store import (
|
|
18
|
+
DECISION_LAYERS,
|
|
19
|
+
REASON_CODES,
|
|
20
|
+
sample_events,
|
|
21
|
+
sanitize_value,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
SCHEMA_VERSION = "bash-observability/v1"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class TraceContext:
|
|
29
|
+
thread_id: str | None
|
|
30
|
+
run_id: str | None
|
|
31
|
+
tool_call_id: str | None
|
|
32
|
+
tool_name: str | None
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def from_ctx(cls, ctx: ToolExecutionContext) -> "TraceContext":
|
|
36
|
+
return cls(
|
|
37
|
+
thread_id=ctx.thread_id,
|
|
38
|
+
run_id=ctx.run_id,
|
|
39
|
+
tool_call_id=ctx.tool_call_id,
|
|
40
|
+
tool_name=ctx.tool_name,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
def to_dict(self) -> dict[str, Any]:
|
|
44
|
+
return {
|
|
45
|
+
"thread_id": self.thread_id,
|
|
46
|
+
"run_id": self.run_id,
|
|
47
|
+
"tool_call_id": self.tool_call_id,
|
|
48
|
+
"tool_name": self.tool_name,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class BashEventBuffer:
|
|
53
|
+
"""最小事件流缓冲:提供 seq + ts_ms + event。"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, *, trace_context: TraceContext, scenario: str) -> None:
|
|
56
|
+
self._trace_context = trace_context
|
|
57
|
+
self._scenario = scenario
|
|
58
|
+
self._seq = 0
|
|
59
|
+
self._events: list[dict[str, Any]] = []
|
|
60
|
+
|
|
61
|
+
def add(self, event: str, **payload: Any) -> None:
|
|
62
|
+
self._seq += 1
|
|
63
|
+
self._events.append(
|
|
64
|
+
{
|
|
65
|
+
"seq": self._seq,
|
|
66
|
+
"ts_ms": int(time.time() * 1000),
|
|
67
|
+
"event": event,
|
|
68
|
+
**payload,
|
|
69
|
+
}
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def to_observability(self) -> dict[str, Any]:
|
|
73
|
+
return {
|
|
74
|
+
"schema_version": SCHEMA_VERSION,
|
|
75
|
+
"scenario": self._scenario,
|
|
76
|
+
"trace_context": self._trace_context.to_dict(),
|
|
77
|
+
"events": self._events,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def build_decision_observability(
|
|
82
|
+
*,
|
|
83
|
+
trace: list[dict[str, Any]],
|
|
84
|
+
trace_context: TraceContext,
|
|
85
|
+
) -> dict[str, Any]:
|
|
86
|
+
return {
|
|
87
|
+
"schema_version": SCHEMA_VERSION,
|
|
88
|
+
"decision_layer_dict": sorted(DECISION_LAYERS),
|
|
89
|
+
"trace_context": trace_context.to_dict(),
|
|
90
|
+
"decision_trace": trace,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def finalize_observability_payload(
|
|
95
|
+
payload: dict[str, Any],
|
|
96
|
+
*,
|
|
97
|
+
sample_limit: int = 200,
|
|
98
|
+
) -> dict[str, Any]:
|
|
99
|
+
cleaned = sanitize_value(payload)
|
|
100
|
+
events = cleaned.get("events")
|
|
101
|
+
if isinstance(events, list):
|
|
102
|
+
cleaned["events"] = sample_events(events, limit=sample_limit)
|
|
103
|
+
cleaned.setdefault("reason_code_dict", sorted(REASON_CODES))
|
|
104
|
+
return cleaned
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def project_for_cli(observability: dict[str, Any] | None) -> list[str]:
|
|
108
|
+
if not observability:
|
|
109
|
+
return []
|
|
110
|
+
lines: list[str] = []
|
|
111
|
+
for e in observability.get("events", []):
|
|
112
|
+
payload = {k: v for k, v in e.items() if k not in {"seq", "event", "ts_ms"}}
|
|
113
|
+
lines.append(
|
|
114
|
+
f"[{e.get('seq')}] {e.get('event')} ts={e.get('ts_ms')} payload={payload}"
|
|
115
|
+
)
|
|
116
|
+
return lines
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def project_for_ui(observability: dict[str, Any] | None) -> list[dict[str, Any]]:
|
|
120
|
+
if not observability:
|
|
121
|
+
return []
|
|
122
|
+
cards: list[dict[str, Any]] = []
|
|
123
|
+
for e in observability.get("events", []):
|
|
124
|
+
cards.append(
|
|
125
|
+
{
|
|
126
|
+
"type": "runtime_event",
|
|
127
|
+
"title": e.get("event"),
|
|
128
|
+
"seq": e.get("seq"),
|
|
129
|
+
"ts_ms": e.get("ts_ms"),
|
|
130
|
+
"payload": {k: v for k, v in e.items() if k not in {"seq", "event", "ts_ms"}},
|
|
131
|
+
}
|
|
132
|
+
)
|
|
133
|
+
return cards
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def project_for_log(
|
|
137
|
+
*,
|
|
138
|
+
observability: dict[str, Any] | None,
|
|
139
|
+
decision_trace: list[dict[str, Any]] | None = None,
|
|
140
|
+
) -> dict[str, Any]:
|
|
141
|
+
return {
|
|
142
|
+
"schema_version": SCHEMA_VERSION,
|
|
143
|
+
"trace_context": (observability or {}).get("trace_context"),
|
|
144
|
+
"scenario": (observability or {}).get("scenario"),
|
|
145
|
+
"events": (observability or {}).get("events", []),
|
|
146
|
+
"decision_trace": decision_trace or [],
|
|
147
|
+
}
|
|
148
|
+
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tools/bash/output_utils.py — BashRuntimeTool 模块5:结果输出辅助能力
|
|
3
|
+
|
|
4
|
+
职责:
|
|
5
|
+
承接 Bash 结果展示层里与“输出内容形态”相关的通用处理,避免这些细节继续堆在
|
|
6
|
+
`tool.py` 或 `result_presenter.py` 中。
|
|
7
|
+
|
|
8
|
+
在整体链路中的位置:
|
|
9
|
+
- `invoke()` 之后、`present()` 之前使用
|
|
10
|
+
- 由 `result_presenter.py` 调用,负责把 stdout 中的特殊输出形态转成结构化结果
|
|
11
|
+
|
|
12
|
+
当前包含:
|
|
13
|
+
1. image data URI 检测与解析
|
|
14
|
+
2. 基于 Pillow 软依赖的可选 resize/downsample
|
|
15
|
+
3. Bash 图片输出 artifact 构造
|
|
16
|
+
|
|
17
|
+
与 CC 对照:
|
|
18
|
+
对应 CC `utils.ts` 中的 `isImageOutput()`、`parseDataUri()`、
|
|
19
|
+
`resizeShellImageOutput()` 等输出辅助逻辑。
|
|
20
|
+
当前为基础版:先把图片输出识别、artifact 化和可选缩放立住,不复制完整 UI 层。
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import base64
|
|
26
|
+
import io
|
|
27
|
+
import re
|
|
28
|
+
from dataclasses import dataclass
|
|
29
|
+
|
|
30
|
+
from langchain_agentx.tool_runtime.models import ToolArtifact
|
|
31
|
+
|
|
32
|
+
_IMAGE_DATA_URI_RE = re.compile(
|
|
33
|
+
r"^data:(image/[a-z0-9.+_-]+);base64,([A-Za-z0-9+/=\s]+)$",
|
|
34
|
+
re.IGNORECASE,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
_MAX_IMAGE_BYTES = 5 * 1024 * 1024
|
|
38
|
+
_MAX_IMAGE_DIMENSION = 2048
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class BashImageArtifact:
|
|
43
|
+
"""图片输出的结构化表示。"""
|
|
44
|
+
|
|
45
|
+
artifact: ToolArtifact
|
|
46
|
+
media_type: str
|
|
47
|
+
width: int | None = None
|
|
48
|
+
height: int | None = None
|
|
49
|
+
size_bytes: int | None = None
|
|
50
|
+
was_resized: bool = False
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def is_image_output(content: str) -> bool:
|
|
54
|
+
"""判断 stdout 是否为 image data URI。"""
|
|
55
|
+
return _IMAGE_DATA_URI_RE.match(content.strip()) is not None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def parse_image_data_uri(content: str) -> tuple[str, str] | None:
|
|
59
|
+
"""
|
|
60
|
+
解析 image data URI,返回 `(media_type, base64_payload)`。
|
|
61
|
+
|
|
62
|
+
返回 None 表示不是受支持的 image data URI。
|
|
63
|
+
"""
|
|
64
|
+
match = _IMAGE_DATA_URI_RE.match(content.strip())
|
|
65
|
+
if not match:
|
|
66
|
+
return None
|
|
67
|
+
return match.group(1), re.sub(r"\s+", "", match.group(2))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class BashOutputArtifactBuilder:
|
|
71
|
+
"""
|
|
72
|
+
Bash 输出 artifact 构造器。
|
|
73
|
+
|
|
74
|
+
只负责把 stdout 中的特殊输出识别为结构化 artifact;
|
|
75
|
+
不负责 summary/hints/payload 的展示语义。
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def build_image_artifact(self, stdout: str) -> BashImageArtifact | None:
|
|
79
|
+
parsed = parse_image_data_uri(stdout)
|
|
80
|
+
if parsed is None:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
media_type, payload = parsed
|
|
84
|
+
try:
|
|
85
|
+
raw = base64.b64decode(payload, validate=True)
|
|
86
|
+
except Exception:
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
optimized = self._maybe_resize_image(raw=raw, media_type=media_type)
|
|
90
|
+
encoded = base64.b64encode(optimized.data).decode("ascii")
|
|
91
|
+
data_uri = f"data:{optimized.media_type};base64,{encoded}"
|
|
92
|
+
return BashImageArtifact(
|
|
93
|
+
artifact=ToolArtifact(
|
|
94
|
+
uri=data_uri,
|
|
95
|
+
media_type=optimized.media_type,
|
|
96
|
+
description="Image artifact emitted by bash command output",
|
|
97
|
+
),
|
|
98
|
+
media_type=optimized.media_type,
|
|
99
|
+
width=optimized.width,
|
|
100
|
+
height=optimized.height,
|
|
101
|
+
size_bytes=len(optimized.data),
|
|
102
|
+
was_resized=optimized.was_resized,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
def _maybe_resize_image(self, *, raw: bytes, media_type: str) -> "_ImageOptimizationResult":
|
|
106
|
+
"""
|
|
107
|
+
在 Pillow 可用时对图片做轻量压缩/缩放。
|
|
108
|
+
|
|
109
|
+
目标不是强依赖图片栈,而是在环境支持时减少超大图片对上下文与后续传输的压力。
|
|
110
|
+
"""
|
|
111
|
+
try:
|
|
112
|
+
from PIL import Image # type: ignore[import]
|
|
113
|
+
except Exception:
|
|
114
|
+
return _ImageOptimizationResult(
|
|
115
|
+
data=raw,
|
|
116
|
+
media_type=media_type,
|
|
117
|
+
width=None,
|
|
118
|
+
height=None,
|
|
119
|
+
was_resized=False,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
with Image.open(io.BytesIO(raw)) as image:
|
|
124
|
+
width, height = image.size
|
|
125
|
+
needs_resize = (
|
|
126
|
+
len(raw) > _MAX_IMAGE_BYTES
|
|
127
|
+
or max(width, height) > _MAX_IMAGE_DIMENSION
|
|
128
|
+
)
|
|
129
|
+
if not needs_resize:
|
|
130
|
+
return _ImageOptimizationResult(
|
|
131
|
+
data=raw,
|
|
132
|
+
media_type=media_type,
|
|
133
|
+
width=width,
|
|
134
|
+
height=height,
|
|
135
|
+
was_resized=False,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
resized = image.copy()
|
|
139
|
+
resized.thumbnail((_MAX_IMAGE_DIMENSION, _MAX_IMAGE_DIMENSION))
|
|
140
|
+
|
|
141
|
+
buffer = io.BytesIO()
|
|
142
|
+
target_format = self._pil_format_for_media_type(media_type)
|
|
143
|
+
save_kwargs: dict[str, int | bool] = {}
|
|
144
|
+
if target_format in {"JPEG", "WEBP"}:
|
|
145
|
+
save_kwargs = {"quality": 85, "optimize": True}
|
|
146
|
+
elif target_format == "PNG":
|
|
147
|
+
save_kwargs = {"optimize": True}
|
|
148
|
+
|
|
149
|
+
resized.save(buffer, format=target_format, **save_kwargs)
|
|
150
|
+
resized_width, resized_height = resized.size
|
|
151
|
+
optimized_bytes = buffer.getvalue()
|
|
152
|
+
optimized_media_type = self._media_type_for_pil_format(
|
|
153
|
+
target_format, fallback=media_type
|
|
154
|
+
)
|
|
155
|
+
return _ImageOptimizationResult(
|
|
156
|
+
data=optimized_bytes,
|
|
157
|
+
media_type=optimized_media_type,
|
|
158
|
+
width=resized_width,
|
|
159
|
+
height=resized_height,
|
|
160
|
+
was_resized=True,
|
|
161
|
+
)
|
|
162
|
+
except Exception:
|
|
163
|
+
return _ImageOptimizationResult(
|
|
164
|
+
data=raw,
|
|
165
|
+
media_type=media_type,
|
|
166
|
+
width=None,
|
|
167
|
+
height=None,
|
|
168
|
+
was_resized=False,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def _pil_format_for_media_type(media_type: str) -> str:
|
|
173
|
+
media_type = media_type.lower()
|
|
174
|
+
return {
|
|
175
|
+
"image/jpeg": "JPEG",
|
|
176
|
+
"image/jpg": "JPEG",
|
|
177
|
+
"image/png": "PNG",
|
|
178
|
+
"image/gif": "GIF",
|
|
179
|
+
"image/webp": "WEBP",
|
|
180
|
+
}.get(media_type, "PNG")
|
|
181
|
+
|
|
182
|
+
@staticmethod
|
|
183
|
+
def _media_type_for_pil_format(pil_format: str, *, fallback: str) -> str:
|
|
184
|
+
return {
|
|
185
|
+
"JPEG": "image/jpeg",
|
|
186
|
+
"PNG": "image/png",
|
|
187
|
+
"GIF": "image/gif",
|
|
188
|
+
"WEBP": "image/webp",
|
|
189
|
+
}.get(pil_format.upper(), fallback)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@dataclass(frozen=True)
|
|
193
|
+
class _ImageOptimizationResult:
|
|
194
|
+
"""内部图片优化结果。"""
|
|
195
|
+
|
|
196
|
+
data: bytes
|
|
197
|
+
media_type: str
|
|
198
|
+
width: int | None
|
|
199
|
+
height: int | None
|
|
200
|
+
was_resized: bool
|