agentengine-sdk-python 0.8.3__py3-none-any.whl → 0.8.5__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.
- {agentengine_sdk_python-0.8.3.dist-info → agentengine_sdk_python-0.8.5.dist-info}/METADATA +13 -9
- agentengine_sdk_python-0.8.5.dist-info/RECORD +1296 -0
- ksadk/_build_provenance.json +1 -1
- ksadk/_process.py +83 -0
- ksadk/_str_enum.py +17 -0
- ksadk/a2ui/core.py +3 -3
- ksadk/api/client.py +111 -91
- ksadk/builders/code_builder.py +74 -21
- ksadk/builders/framework_requirements.py +22 -3
- ksadk/builders/ks3_uploader.py +36 -7
- ksadk/builders/managed_runtime_builder.py +1 -0
- ksadk/cli/__init__.py +68 -3
- ksadk/cli/cmd_config.py +4 -4
- ksadk/cli/cmd_create.py +12 -12
- ksadk/cli/cmd_hermes.py +24 -2
- ksadk/cli/cmd_invoke.py +28 -5
- ksadk/cli/cmd_managed_runtime.py +13 -1
- ksadk/cli/cmd_openclaw.py +26 -1
- ksadk/cli/cmd_plugin.py +76 -13
- ksadk/cli/cmd_studio.py +64 -16
- ksadk/cli/cmd_web.py +2 -5
- ksadk/cli/openclaw_env.py +1 -1
- ksadk/cli/resource_common.py +26 -0
- ksadk/cli/runtime_bootstrap.py +23 -1
- ksadk/codex/client.py +425 -96
- ksadk/codex/projection.py +24 -0
- ksadk/codex/runtime.py +56 -298
- ksadk/common/auth.py +16 -10
- ksadk/configs/__init__.py +1 -1
- ksadk/configs/env_registry.py +153 -31
- ksadk/configs/env_registry_studio.py +86 -0
- ksadk/configs/global_config.py +1 -1
- ksadk/configs/settings.py +3 -3
- ksadk/connector/__init__.py +17 -0
- ksadk/connector/channel.py +477 -0
- ksadk/context_engine/capabilities.py +52 -4
- ksadk/conversations/attachment_storage.py +10 -3
- ksadk/conversations/attachments.py +16 -5
- ksadk/conversations/message_projection.py +133 -21
- ksadk/conversations/normalize.py +28 -10
- ksadk/conversations/projector.py +4 -6
- ksadk/conversations/runtime_baseline.py +37 -0
- ksadk/conversations/runtime_invocation.py +22 -4
- ksadk/conversations/runtime_observability.py +39 -10
- ksadk/conversations/runtime_payloads.py +9 -0
- ksadk/conversations/runtime_persistence.py +105 -9
- ksadk/conversations/runtime_preparation.py +42 -33
- ksadk/conversations/runtime_resume.py +50 -2
- ksadk/conversations/runtime_stream_events.py +34 -35
- ksadk/conversations/runtime_streaming.py +33 -7
- ksadk/deployment/env_forward.py +2 -2
- ksadk/deployment/providers/serverless.py +1 -1
- ksadk/events/adapters/_codex_items.py +16 -8
- ksadk/events/adapters/_codex_validators.py +18 -7
- ksadk/events/adapters/_langgraph_support.py +34 -0
- ksadk/events/adapters/langgraph.py +15 -1
- ksadk/events/canonical_store.py +3 -1
- ksadk/events/projections.py +1 -1
- ksadk/events/session_event.py +2 -2
- ksadk/harness/__init__.py +14 -0
- ksadk/harness/app.py +11 -0
- ksadk/harness/artifact_store.py +213 -0
- ksadk/harness/capabilities.py +115 -0
- ksadk/harness/capability_runtime.py +237 -0
- ksadk/harness/closure_contract.py +92 -0
- ksadk/harness/compaction_record.py +122 -0
- ksadk/harness/compiler.py +284 -0
- ksadk/harness/config.py +2 -2
- ksadk/harness/conformance/__init__.py +33 -0
- ksadk/harness/conformance/contract.py +506 -0
- ksadk/harness/conformance/fixtures.py +102 -0
- ksadk/harness/context_engine.py +403 -0
- ksadk/harness/context_maintenance.py +259 -0
- ksadk/harness/context_manifest.py +188 -0
- ksadk/harness/context_tuning.py +168 -0
- ksadk/harness/draft_runtime.py +322 -0
- ksadk/harness/engine/__init__.py +40 -0
- ksadk/harness/engine/base.py +122 -0
- ksadk/harness/engine/budgets.py +108 -0
- ksadk/harness/engine/capability_declarations.py +59 -0
- ksadk/harness/engine/completion_guard.py +42 -0
- ksadk/harness/engine/context_pipeline.py +579 -0
- ksadk/harness/engine/graph_builder.py +859 -0
- ksadk/harness/engine/langgraph.py +931 -0
- ksadk/harness/engine/mcp_disclosure.py +750 -0
- ksadk/harness/engine/policy_runtime.py +70 -0
- ksadk/harness/engine/postgres_checkpointer.py +63 -0
- ksadk/harness/engine/run_control_bridge.py +80 -0
- ksadk/harness/engine/run_types.py +88 -0
- ksadk/harness/engine/skill_disclosure.py +189 -0
- ksadk/harness/engine/spans.py +41 -0
- ksadk/harness/engine/subagents.py +343 -0
- ksadk/harness/engine/thread_ids.py +112 -0
- ksadk/harness/engine/tool_dispatch.py +144 -0
- ksadk/harness/event_tree.py +212 -0
- ksadk/harness/events.py +461 -0
- ksadk/harness/execution_policy.py +94 -0
- ksadk/harness/insights.py +210 -0
- ksadk/harness/lifecycle.py +578 -0
- ksadk/harness/lifecycle_conformance.py +233 -0
- ksadk/harness/lifecycle_local_plane.py +104 -0
- ksadk/harness/loop/__init__.py +44 -0
- ksadk/harness/loop/reason.py +454 -0
- ksadk/harness/loop/tools.py +762 -0
- ksadk/harness/managed_runtime.py +884 -0
- ksadk/harness/mcp_matrix.py +474 -0
- ksadk/harness/mcp_runtime.py +471 -0
- ksadk/harness/memory_consolidation.py +157 -0
- ksadk/harness/memory_loop.py +156 -0
- ksadk/harness/memory_runtime.py +541 -0
- ksadk/harness/metrics.py +90 -0
- ksadk/harness/model_capability.py +253 -0
- ksadk/harness/model_provider.py +220 -0
- ksadk/harness/observability/__init__.py +29 -0
- ksadk/harness/observability/inspection.py +528 -0
- ksadk/harness/observability/metrics.py +167 -0
- ksadk/harness/observability/otel_exporter.py +218 -0
- ksadk/harness/prompt_cache.py +70 -0
- ksadk/harness/public_activity.py +154 -0
- ksadk/harness/readiness.py +249 -0
- ksadk/harness/reasoner.py +466 -13
- ksadk/harness/release_readiness.py +140 -0
- ksadk/harness/resource_ref.py +69 -0
- ksadk/harness/run_control.py +668 -0
- ksadk/harness/runtime.py +171 -136
- ksadk/harness/runtime_capabilities.py +158 -0
- ksadk/harness/runtime_server.py +514 -0
- ksadk/harness/sandbox_adapters.py +739 -0
- ksadk/harness/sandbox_backend.py +530 -0
- ksadk/harness/sandbox_conformance.py +288 -0
- ksadk/harness/sandbox_lease.py +77 -0
- ksadk/harness/sandbox_lease_conformance.py +259 -0
- ksadk/harness/sandbox_matrix.py +170 -0
- ksadk/harness/sandbox_reconciliation.py +140 -0
- ksadk/harness/sandbox_recovery.py +586 -0
- ksadk/harness/sandbox_recovery_conformance.py +246 -0
- ksadk/harness/session_store.py +243 -0
- ksadk/harness/skill_composition.py +212 -0
- ksadk/harness/skill_feedback.py +123 -0
- ksadk/harness/skill_matrix.py +233 -0
- ksadk/harness/skill_runtime.py +259 -0
- ksadk/harness/spec.py +332 -0
- ksadk/harness/state.py +153 -0
- ksadk/harness/strategies.py +149 -0
- ksadk/harness/subagent.py +253 -0
- ksadk/harness/tool_policy.py +131 -0
- ksadk/harness/tool_receipts.py +96 -0
- ksadk/harness/tool_reliability.py +81 -0
- ksadk/harness/tools.py +38 -13
- ksadk/harness/user_errors.py +69 -0
- ksadk/harness/working_context.py +81 -0
- ksadk/harness/working_context_patch.py +99 -0
- ksadk/identity/resolver.py +18 -0
- ksadk/interaction/__init__.py +1 -1
- ksadk/interaction/ledger.py +1 -1
- ksadk/interaction/provider.py +2 -1
- ksadk/interaction/providers/__init__.py +3 -1
- ksadk/interaction/providers/adk.py +1 -1
- ksadk/interaction/providers/codex.py +1 -1
- ksadk/interaction/providers/harness.py +50 -0
- ksadk/interaction/providers/langgraph.py +2 -1
- ksadk/kernel/__init__.py +10 -0
- ksadk/kernel/approval_presentation.py +91 -0
- ksadk/kernel/authorization.py +1 -1
- ksadk/kernel/bootstrap.py +114 -106
- ksadk/kernel/contract_fingerprints.py +1 -1
- ksadk/kernel/contracts.py +5 -0
- ksadk/kernel/control.py +32 -1
- ksadk/kernel/execution_grants.py +221 -0
- ksadk/kernel/execution_grants_memory.py +133 -0
- ksadk/kernel/execution_grants_postgres.py +217 -0
- ksadk/kernel/execution_grants_sqlite.py +209 -0
- ksadk/kernel/ingress.py +42 -29
- ksadk/kernel/mapping.py +1 -1
- ksadk/kernel/memory_store.py +24 -11
- ksadk/kernel/postgres_store.py +63 -33
- ksadk/kernel/recovery.py +78 -14
- ksadk/kernel/sql/001_agent_kernel.sql +23 -0
- ksadk/kernel/sqlite_store.py +331 -25
- ksadk/kernel/state.py +2 -3
- ksadk/kernel/store.py +72 -2
- ksadk/kernel/worker.py +144 -51
- ksadk/kernel/worker_history.py +72 -0
- ksadk/kernel/worker_identity.py +47 -0
- ksadk/knowledge_base/client.py +57 -27
- ksadk/mcp_runtime/__init__.py +12 -4
- ksadk/memory/adk/backends/sdk_ltm_backend.py +161 -41
- ksadk/memory/adk/resilient_session_service.py +25 -17
- ksadk/memory/adk/short_term_memory.py +26 -9
- ksadk/memory/coordinator.py +22 -6
- ksadk/memory/extraction.py +40 -1
- ksadk/memory/models.py +84 -5
- ksadk/memory/provider_resolver.py +8 -0
- ksadk/memory/providers/local_postgres.py +200 -0
- ksadk/memory/providers/local_sqlite.py +42 -3
- ksadk/memory/retrieval.py +189 -0
- ksadk/memory/store_adapter.py +150 -0
- ksadk/model_policy.py +2 -2
- ksadk/model_proxy/detect.py +254 -0
- ksadk/model_proxy/server.py +14 -7
- ksadk/model_proxy/transform.py +40 -2
- ksadk/plugins/__init__.py +5 -1
- ksadk/plugins/artifacts.py +275 -0
- ksadk/plugins/bridges/__init__.py +4 -2
- ksadk/plugins/bridges/codex.py +120 -13
- ksadk/plugins/bridges/dsh.py +351 -156
- ksadk/plugins/builtins.py +72 -35
- ksadk/plugins/bundle_security.py +28 -4
- ksadk/plugins/codex_manifest.py +983 -0
- ksadk/plugins/companion_artifacts.py +83 -0
- ksadk/plugins/companions.py +368 -0
- ksadk/plugins/composition.py +8 -0
- ksadk/plugins/contracts.py +119 -1
- ksadk/plugins/delivery.py +153 -0
- ksadk/plugins/dsh_channels.py +156 -0
- ksadk/plugins/dsh_home.py +140 -0
- ksadk/plugins/dsh_installation_digest.py +71 -0
- ksadk/plugins/dsh_teams.py +105 -0
- ksadk/plugins/dsh_toolchain.py +203 -32
- ksadk/plugins/ecosystem_probe.py +6 -6
- ksadk/plugins/execution_host.py +99 -0
- ksadk/plugins/host.py +102 -39
- ksadk/plugins/kernel_adapter.py +205 -0
- ksadk/plugins/providers/__init__.py +23 -0
- ksadk/plugins/providers/bundles/dsh-channels-client/client.js +19 -0
- ksadk/plugins/providers/bundles/dsh-channels-client/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-channels-client/index.mjs +7 -0
- ksadk/plugins/providers/bundles/dsh-channels-client/package.json +12 -0
- ksadk/plugins/providers/bundles/dsh-knowledge/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-knowledge/index.mjs +53 -0
- ksadk/plugins/providers/bundles/dsh-knowledge/package.json +16 -0
- ksadk/plugins/providers/bundles/dsh-memory/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-memory/index.mjs +175 -0
- ksadk/plugins/providers/bundles/dsh-memory/package.json +16 -0
- ksadk/plugins/providers/bundles/dsh-platform-resources/client.mjs +166 -0
- ksadk/plugins/providers/bundles/dsh-platform-resources/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-platform-resources/index.mjs +9 -0
- ksadk/plugins/providers/bundles/dsh-platform-resources/package.json +12 -0
- ksadk/plugins/providers/bundles/dsh-skill-center/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-skill-center/index.mjs +125 -0
- ksadk/plugins/providers/bundles/dsh-skill-center/package.json +16 -0
- ksadk/plugins/providers/bundles/dsh-teams-client/client.js +29 -0
- ksadk/plugins/providers/bundles/dsh-teams-client/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-client/index.mjs +11 -0
- ksadk/plugins/providers/bundles/dsh-teams-client/package.json +35 -0
- ksadk/plugins/providers/bundles/dsh-teams-leader-policy/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-leader-policy/index.mjs +13 -0
- ksadk/plugins/providers/bundles/dsh-teams-leader-policy/package.json +27 -0
- ksadk/plugins/providers/bundles/dsh-teams-profile/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-profile/index.mjs +12 -0
- ksadk/plugins/providers/bundles/dsh-teams-profile/package.json +26 -0
- ksadk/plugins/providers/bundles/dsh-teams-router/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-router/index.mjs +16 -0
- ksadk/plugins/providers/bundles/dsh-teams-router/package.json +27 -0
- ksadk/plugins/providers/bundles/dsh-teams-tasks/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-tasks/index.mjs +16 -0
- ksadk/plugins/providers/bundles/dsh-teams-tasks/package.json +27 -0
- ksadk/plugins/providers/bundles/dsh-teams-tools/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/dsh-teams-tools/index.mjs +25 -0
- ksadk/plugins/providers/bundles/dsh-teams-tools/package.json +29 -0
- ksadk/plugins/providers/bundles/dsh-teams-tools/tools.json +235 -0
- ksadk/plugins/providers/bundles/ksadk-codex/index.mjs +1 -1
- ksadk/plugins/providers/bundles/ksadk-dsh-capability-host/cordis.patch.yml +3 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-capability-host/index.mjs +920 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-capability-host/package.json +27 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-companion-host/client.mjs +133 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-companion-host/index.mjs +14 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-studio/client.js +115 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-studio/index.mjs +19 -0
- ksadk/plugins/providers/bundles/ksadk-dsh-studio/package.json +7 -0
- ksadk/plugins/providers/bundles/ksadk-harness/index.mjs +1 -1
- ksadk/plugins/providers/codex.py +344 -70
- ksadk/plugins/providers/codex_native.py +368 -0
- ksadk/plugins/providers/codex_turn.py +301 -0
- ksadk/plugins/providers/dsh_capabilities.py +1450 -0
- ksadk/plugins/providers/dsh_mcp.py +467 -0
- ksadk/plugins/providers/harness.py +294 -141
- ksadk/plugins/providers/harness_delegation.py +593 -0
- ksadk/plugins/providers/harness_managed.py +456 -0
- ksadk/plugins/providers/harness_tools.py +326 -0
- ksadk/plugins/providers/mcp_projection.py +150 -0
- ksadk/plugins/providers/platform_resources.py +657 -0
- ksadk/plugins/providers/shipped_dsh.py +4 -1
- ksadk/plugins/resolver.py +8 -1
- ksadk/plugins/subagent_providers/codex.py +28 -6
- ksadk/plugins/teams/__init__.py +5 -0
- ksadk/plugins/teams/api.py +405 -0
- ksadk/plugins/teams/application.py +680 -0
- ksadk/plugins/teams/artifacts.py +60 -0
- ksadk/plugins/teams/context.py +96 -0
- ksadk/plugins/teams/contracts.py +136 -0
- ksadk/plugins/teams/domain.py +1427 -0
- ksadk/plugins/teams/domain_values.py +24 -0
- ksadk/plugins/teams/errors.py +13 -0
- ksadk/plugins/teams/migration.py +311 -0
- ksadk/plugins/teams/migration_cli.py +322 -0
- ksadk/plugins/teams/postgres_store.py +138 -0
- ksadk/plugins/teams/runtime.py +777 -0
- ksadk/plugins/teams/source_projection.py +84 -0
- ksadk/plugins/teams/store.py +311 -0
- ksadk/plugins/teams/task_decisions.py +423 -0
- ksadk/plugins/teams/transport.py +91 -0
- ksadk/plugins/teams/workspaces.py +255 -0
- ksadk/resource_runtime/__init__.py +4 -0
- ksadk/resource_runtime/broker.py +616 -0
- ksadk/resource_runtime/build_artifacts.py +165 -0
- ksadk/resource_runtime/contracts.py +138 -0
- ksadk/resource_runtime/discovery_receipts.py +100 -0
- ksadk/resource_runtime/discovery_skills.py +172 -0
- ksadk/resource_runtime/errors.py +6 -0
- ksadk/resource_runtime/ipc.py +142 -0
- ksadk/resource_runtime/kernel_approval.py +165 -0
- ksadk/resource_runtime/knowledge.py +114 -0
- ksadk/resource_runtime/langgraph.py +94 -0
- ksadk/resource_runtime/leases.py +191 -0
- ksadk/resource_runtime/managed_projection.py +186 -0
- ksadk/resource_runtime/mcp_server.py +138 -0
- ksadk/resource_runtime/memory_provider.py +318 -0
- ksadk/resource_runtime/memory_receipts.py +34 -0
- ksadk/resource_runtime/operation_ledger.py +211 -0
- ksadk/resource_runtime/plugin_config.py +48 -0
- ksadk/resource_runtime/policy_authorization.py +54 -0
- ksadk/resource_runtime/process.py +110 -0
- ksadk/resource_runtime/skill_artifacts.py +153 -0
- ksadk/resource_runtime/skill_runtime.py +42 -0
- ksadk/resource_runtime/skill_workspace.py +118 -0
- ksadk/resource_runtime/skills.py +215 -0
- ksadk/resource_runtime/snapshots.py +180 -0
- ksadk/resource_runtime/socket_server.py +103 -0
- ksadk/resource_runtime/supervisor.py +380 -0
- ksadk/resource_runtime/worker.py +504 -0
- ksadk/runners/_langgraph_runner_streams.py +182 -38
- ksadk/runners/_langgraph_state_reader.py +106 -0
- ksadk/runners/_session_identity.py +285 -0
- ksadk/runners/adk_runner.py +166 -63
- ksadk/runners/base_runner.py +29 -1
- ksadk/runners/langgraph_runner.py +542 -195
- ksadk/runners/patch_langchain.py +47 -0
- ksadk/runners/usage_accumulator.py +11 -3
- ksadk/runtime/_runner_adapter/stream_mapping.py +194 -20
- ksadk/runtime/adapter.py +2 -0
- ksadk/runtime/conversation_execution.py +117 -17
- ksadk/runtime/executor.py +87 -3
- ksadk/runtime/factory.py +65 -274
- ksadk/runtime/framework_adapters.py +23 -0
- ksadk/runtime/preprocessing.py +21 -5
- ksadk/runtime/runner_adapter.py +106 -18
- ksadk/runtime/skill_eval_result.py +36 -0
- ksadk/runtime/timing.py +46 -0
- ksadk/runtime/usage.py +49 -18
- ksadk/runtime_context.py +142 -2
- ksadk/sandbox/backends/e2b.py +288 -25
- ksadk/sandbox/backends/local_process.py +72 -39
- ksadk/sandbox/base.py +64 -1
- ksadk/sandbox/diagnostics.py +82 -0
- ksadk/sandbox/e2b_connection.py +66 -0
- ksadk/sandbox/local_controls.py +746 -0
- ksadk/sandbox/local_launcher.py +90 -0
- ksadk/scheduler/dispatcher.py +1 -1
- ksadk/scheduler/sqlite_store.py +18 -0
- ksadk/server/composition.py +2 -0
- ksadk/server/factory.py +5 -0
- ksadk/server/invocation_identity.py +118 -0
- ksadk/server/persistence_capability.py +516 -0
- ksadk/server/routes/common.py +32 -3
- ksadk/server/routes/control.py +271 -52
- ksadk/server/routes/dependencies.py +16 -0
- ksadk/server/routes/kernel_ingress.py +9 -1
- ksadk/server/routes/misc.py +233 -25
- ksadk/server/routes/models.py +47 -4
- ksadk/server/routes/openai_compat.py +76 -13
- ksadk/server/routes/projection.py +252 -12
- ksadk/server/routes/run.py +106 -35
- ksadk/server/routes/sessions.py +491 -138
- ksadk/server/routes/streaming.py +43 -9
- ksadk/server/routes/workspace.py +130 -21
- ksadk/server/static/assets/{ArtifactsPanel-10GPXIJ6.js → ArtifactsPanel-C1YoaKhx.js} +1 -1
- ksadk/server/static/assets/{CodeBlock-BXk1l-G1.js → CodeBlock-DHuAWfr5.js} +10 -7
- ksadk/server/static/assets/MathMessageMarkdown-cvdrBwYr.js +8 -0
- ksadk/server/static/assets/{MermaidBlock--OEYoXIJ.js → MermaidBlock-DeBkbJvI.js} +9 -9
- ksadk/server/static/assets/{NativeTerminalPanel-DrEe1syq.js → NativeTerminalPanel-oJj44_sV.js} +1 -1
- ksadk/server/static/assets/{abnfDiagram-VCTEODGH-eeTFJLvo.js → abnfDiagram-VCTEODGH-Dhfv25o2.js} +1 -1
- ksadk/server/static/assets/{arc-S3ZNbIAd.js → arc-C35w-pn1.js} +1 -1
- ksadk/server/static/assets/architecture-7GRP2DOG-BCsa26zi.js +1 -0
- ksadk/server/static/assets/{architectureDiagram-5GKGNRK7-CRjgEJDX.js → architectureDiagram-5GKGNRK7-FoMY_u6D.js} +1 -1
- ksadk/server/static/assets/{blockDiagram-NRAW4CY4-C7bsBoNH.js → blockDiagram-NRAW4CY4-zD4Rt7oE.js} +1 -1
- ksadk/server/static/assets/{c4Diagram-UCG6FXSJ-BX3c33Ob.js → c4Diagram-UCG6FXSJ-BCtGGkhN.js} +1 -1
- ksadk/server/static/assets/channel-_KDifNys.js +1 -0
- ksadk/server/static/assets/chunk-2Q5K7J3B-HdVkJ9F0.js +1 -0
- ksadk/server/static/assets/{chunk-5VM5RSS4-bZ7ws-8m.js → chunk-5VM5RSS4-C2Ms13_k.js} +1 -1
- ksadk/server/static/assets/{chunk-F27PBJKO-D2r0pvhY.js → chunk-F27PBJKO-BoTj-qgx.js} +1 -1
- ksadk/server/static/assets/{chunk-G27WJ6UU-CZwF3oUP.js → chunk-G27WJ6UU-DOMyyjHH.js} +1 -1
- ksadk/server/static/assets/{chunk-JWPE2WC7-DigFYCML.js → chunk-JWPE2WC7-DbJJqDli.js} +1 -1
- ksadk/server/static/assets/{chunk-LCL6LL3I-CnorGV5q.js → chunk-LCL6LL3I-CLuRCt_W.js} +1 -1
- ksadk/server/static/assets/{chunk-POPQ4Y6H-CexntQA-.js → chunk-POPQ4Y6H-D0Dz847e.js} +1 -1
- ksadk/server/static/assets/{chunk-RHFEMEQ7-CHTC7UxA.js → chunk-RHFEMEQ7-BWSRzEcd.js} +1 -1
- ksadk/server/static/assets/{chunk-SVP7TREG-BC1EWt_-.js → chunk-SVP7TREG-CPoHqkCY.js} +1 -1
- ksadk/server/static/assets/{chunk-XXDRQBXY-BTG24xN0.js → chunk-XXDRQBXY-Bx1jS-X-.js} +1 -1
- ksadk/server/static/assets/classDiagram-DTDB5LWJ-BVCW3ZqZ.js +1 -0
- ksadk/server/static/assets/classDiagram-v2-JRS7N3AN-BVCW3ZqZ.js +1 -0
- ksadk/server/static/assets/{cose-bilkent-JH36ORCC-CkonRdAS.js → cose-bilkent-JH36ORCC-LzyWm4m0.js} +1 -1
- ksadk/server/static/assets/cynefin-OW5HDTMX-CNXjFctL.js +1 -0
- ksadk/server/static/assets/{cynefinDiagram-5FMLGOSQ-DEOCBe15.js → cynefinDiagram-5FMLGOSQ-BlW_bHED.js} +1 -1
- ksadk/server/static/assets/{dagre-3AP2YEHR-DBThPuLa.js → dagre-3AP2YEHR-Bu_S86_i.js} +1 -1
- ksadk/server/static/assets/{dagre-ClTPrTht.js → dagre-B7nCawed.js} +1 -1
- ksadk/server/static/assets/{diagram-S7CK7UJ4-DsZHrnpJ.js → diagram-S7CK7UJ4-W_Zow2Km.js} +1 -1
- ksadk/server/static/assets/{diagram-UQ7AKVKN-BJR4nhVr.js → diagram-UQ7AKVKN-CLIKAHON.js} +1 -1
- ksadk/server/static/assets/{diagram-VSXAHHWV-xzcERX0a.js → diagram-VSXAHHWV-Dv6_QAlN.js} +1 -1
- ksadk/server/static/assets/{diagram-VX7I27RA-tn2xM1xu.js → diagram-VX7I27RA-BWAWRDtZ.js} +1 -1
- ksadk/server/static/assets/{diagram-Z3DM3KII-BZPGBkw5.js → diagram-Z3DM3KII-XA_rWMss.js} +1 -1
- ksadk/server/static/assets/dist-0X_j2Rcc.js +1 -0
- ksadk/server/static/assets/{dist-4zN1_tq5.js → dist-1nXCdsej.js} +1 -1
- ksadk/server/static/assets/{dist-M67Hwu0n.js → dist-2vL2sAdF.js} +1 -1
- ksadk/server/static/assets/{dist-BJLUwH6W.js → dist-B3bA05-e.js} +1 -1
- ksadk/server/static/assets/{dist-8VWI-2bZ.js → dist-BLpRy_Yx.js} +1 -1
- ksadk/server/static/assets/{dist-D1V3f1B4.js → dist-BO_UwatH.js} +1 -1
- ksadk/server/static/assets/{dist-Cq4OL6nO.js → dist-BdNFAney.js} +1 -1
- ksadk/server/static/assets/{dist-DfGFrb0v.js → dist-C0J5_3qM.js} +1 -1
- ksadk/server/static/assets/{dist-DyqV4fL4.js → dist-CGt_UGeO.js} +1 -1
- ksadk/server/static/assets/{dist-DjhwGUgZ.js → dist-CIRMvUM4.js} +1 -1
- ksadk/server/static/assets/{dist-B1oWRmrH.js → dist-CIpWk_5f.js} +1 -1
- ksadk/server/static/assets/{dist-CXpH_Rdd.js → dist-CPyieXEf.js} +1 -1
- ksadk/server/static/assets/{dist-Btyo4vX4.js → dist-D-92S-Fd.js} +1 -1
- ksadk/server/static/assets/{dist-u8tVH5Ak.js → dist-DNKuMzWc.js} +1 -1
- ksadk/server/static/assets/{dist-DQAB0qn_.js → dist-DWfdmz9s.js} +2 -2
- ksadk/server/static/assets/{dist-B-46q3Hk.js → dist-DppE_r-D.js} +1 -1
- ksadk/server/static/assets/{dist-C71CsujO.js → dist-DyG6Ya-z.js} +1 -1
- ksadk/server/static/assets/{dist-DJmCPRVB.js → dist-M-SrdhhZ.js} +1 -1
- ksadk/server/static/assets/{dist-Bbwqa8TG.js → dist-mqD5TZup.js} +1 -1
- ksadk/server/static/assets/{dist-BI1qQH4n.js → dist-n3n6t_FO.js} +1 -1
- ksadk/server/static/assets/{dist-DUwKa_l6.js → dist-qe8xjesy.js} +1 -1
- ksadk/server/static/assets/{dist-B0gdk4rl.js → dist-t6MzE0jU.js} +1 -1
- ksadk/server/static/assets/{ebnfDiagram-PWID7BFC-DVfMsLge.js → ebnfDiagram-PWID7BFC-BfCzF1Xi.js} +1 -1
- ksadk/server/static/assets/{erDiagram-SSCWMZ5O-CWEZSYaw.js → erDiagram-SSCWMZ5O-Dpz9_0zn.js} +1 -1
- ksadk/server/static/assets/eventmodeling-NTZA5JFV-B5-8RH9c.js +1 -0
- ksadk/server/static/assets/flowDiagram-A5DVABFB-BppFxEKw.js +1 -0
- ksadk/server/static/assets/{ganttDiagram-EL5Y4UJY-TWIxoyYL.js → ganttDiagram-EL5Y4UJY-DDaG1f4_.js} +1 -1
- ksadk/server/static/assets/gitGraph-4MIJSDKK-B-AhYj4C.js +1 -0
- ksadk/server/static/assets/{gitGraphDiagram-WWUBYQGX-DDh2hd2S.js → gitGraphDiagram-WWUBYQGX-DcxzRwYC.js} +1 -1
- ksadk/server/static/assets/index-BMd2mucF.css +2 -0
- ksadk/server/static/assets/index-D5qyyhTH.js +244 -0
- ksadk/server/static/assets/info-A6RAGUB7-0uiKrC35.js +1 -0
- ksadk/server/static/assets/{infoDiagram-RXCK75RN-CgoyWiDR.js → infoDiagram-RXCK75RN-CYMdn6lQ.js} +1 -1
- ksadk/server/static/assets/{ishikawaDiagram-5VMMS53U-CyRIu_om.js → ishikawaDiagram-5VMMS53U-CLHFAD6c.js} +1 -1
- ksadk/server/static/assets/{journeyDiagram-EYS64GPL-DN5JXHzN.js → journeyDiagram-EYS64GPL-BHSKQGAl.js} +1 -1
- ksadk/server/static/assets/{kanban-definition-3QL26DDD-CNP3ui66.js → kanban-definition-3QL26DDD-D5W3tjTT.js} +1 -1
- ksadk/server/static/assets/{katex-DWEzQy1d.js → katex-mtjOi4KM.js} +1 -1
- ksadk/server/static/assets/{linear-C-qHp7d2.js → linear-BETqsxE_.js} +1 -1
- ksadk/server/static/assets/{mermaid-parser.core-Cl-K943T.js → mermaid-parser.core-DPWrvs90.js} +2 -2
- ksadk/server/static/assets/{mindmap-definition-FBJOCRG2-D5DB5mYm.js → mindmap-definition-FBJOCRG2-CJG2mfBP.js} +1 -1
- ksadk/server/static/assets/packet-AYTQ26CC-BVTmZ_cw.js +1 -0
- ksadk/server/static/assets/{pegDiagram-XKGWAZYB-BQVc9ia_.js → pegDiagram-XKGWAZYB-GjAPNaqn.js} +1 -1
- ksadk/server/static/assets/pie-WAS4IAKB-DyfxBU7V.js +1 -0
- ksadk/server/static/assets/{pieDiagram-E7YTZNPT-B6UCAvtY.js → pieDiagram-E7YTZNPT-ToIewrqr.js} +1 -1
- ksadk/server/static/assets/{quadrantDiagram-AXDQQJYC-mIr2d3eb.js → quadrantDiagram-AXDQQJYC-4d78KiEY.js} +1 -1
- ksadk/server/static/assets/radar-RG4KPBEZ-FFontjL2.js +1 -0
- ksadk/server/static/assets/railroad-74A4TZTK-D0Uidzen.js +1 -0
- ksadk/server/static/assets/railroad-abnf-HS5TGJTU-BOo_9T2h.js +1 -0
- ksadk/server/static/assets/railroad-ebnf-LZEXJU2U-D6-BjW7Z.js +1 -0
- ksadk/server/static/assets/railroad-peg-WCYAUIDC-CXx__yMm.js +1 -0
- ksadk/server/static/assets/{railroadDiagram-O6MQD6OU-Dt3FFDmK.js → railroadDiagram-O6MQD6OU-DKzx4xfv.js} +1 -1
- ksadk/server/static/assets/{requirementDiagram-EFPCY7ZU-Cz_vQtgF.js → requirementDiagram-EFPCY7ZU-CJYljxQS.js} +1 -1
- ksadk/server/static/assets/{sankeyDiagram-P5KCCOFB-wlO1TsH5.js → sankeyDiagram-P5KCCOFB-DLU2fV1w.js} +1 -1
- ksadk/server/static/assets/{sequenceDiagram-WJ2MYXX4-CHRbElYq.js → sequenceDiagram-WJ2MYXX4-CVqysrA9.js} +1 -1
- ksadk/server/static/assets/{sizeCapture-X5ZJPWSS-DXX54iyO.js → sizeCapture-X5ZJPWSS-BUxfmTHc.js} +1 -1
- ksadk/server/static/assets/{stateDiagram-HBIQ2CUA-Dp_lw9II.js → stateDiagram-HBIQ2CUA-CNFgYI9E.js} +1 -1
- ksadk/server/static/assets/stateDiagram-v2-4QOOHH4V-BXAFScz8.js +1 -0
- ksadk/server/static/assets/{swimlanes-XN3QIQJK-D8O5UlLG.js → swimlanes-XN3QIQJK-B6qCMe8N.js} +1 -1
- ksadk/server/static/assets/swimlanesDiagram-VK2B7HYN-uIQ2lJKH.js +8 -0
- ksadk/server/static/assets/{timeline-definition-24CTP7MA-8mfKcxvG.js → timeline-definition-24CTP7MA-CMD3x_U_.js} +1 -1
- ksadk/server/static/assets/treeView-Q6P3EWNA-CK5GgucZ.js +1 -0
- ksadk/server/static/assets/treemap-WGGIJYW6-Bq1u1SpG.js +1 -0
- ksadk/server/static/assets/{vennDiagram-4TSXK5OY-EnNl0DE7.js → vennDiagram-4TSXK5OY-BqtUqvXM.js} +1 -1
- ksadk/server/static/assets/wardley-WFR3VGLG-QAt06uEz.js +1 -0
- ksadk/server/static/assets/{wardleyDiagram-VM6X3IG4-DBu5fbYr.js → wardleyDiagram-VM6X3IG4-B5KfDjI-.js} +1 -1
- ksadk/server/static/assets/{xychartDiagram-S5SC5T6Z-BSVXs4IY.js → xychartDiagram-S5SC5T6Z-BaUlSZrn.js} +1 -1
- ksadk/server/static/index.html +2 -2
- ksadk/server/terminal_sessions.py +34 -2
- ksadk/session_context.py +66 -0
- ksadk/sessions/__init__.py +11 -21
- ksadk/sessions/_local_service_sync.py +263 -1
- ksadk/sessions/base.py +176 -0
- ksadk/sessions/errors.py +4 -0
- ksadk/sessions/in_memory.py +309 -19
- ksadk/sessions/invocation_identity.py +235 -0
- ksadk/sessions/local_service.py +83 -1
- ksadk/sessions/persistence.py +275 -0
- ksadk/sessions/postgres_service.py +358 -0
- ksadk/sessions/resilience.py +8 -0
- ksadk/sessions/resilient.py +362 -4
- ksadk/sessions/topology.py +135 -0
- ksadk/skills/__init__.py +26 -0
- ksadk/skills/events.py +464 -0
- ksadk/skills/manifest_cache.py +263 -0
- ksadk/skills/mcp_server/__init__.py +0 -0
- ksadk/skills/mcp_server/_registry.py +33 -0
- ksadk/skills/mcp_server/_routing.py +26 -0
- ksadk/skills/mcp_server/_workspace_files.py +160 -0
- ksadk/skills/mcp_server/register.py +415 -0
- ksadk/skills/mcp_server/server.py +218 -0
- ksadk/skills/models.py +37 -4
- ksadk/skills/observability.py +132 -0
- ksadk/skills/package_store.py +261 -40
- ksadk/skills/runtime/agent.py +67 -9
- ksadk/skills/runtime/artifact_delivery.py +139 -0
- ksadk/skills/runtime/artifacts.py +87 -0
- ksadk/skills/runtime/backends/e2b.py +460 -27
- ksadk/skills/runtime/backends/local.py +629 -62
- ksadk/skills/runtime/base.py +81 -7
- ksadk/skills/runtime/executor.py +268 -1
- ksadk/skills/runtime/loader.py +118 -5
- ksadk/skills/runtime/pinned.py +114 -0
- ksadk/skills/runtime/request.py +49 -0
- ksadk/skills/service_client.py +167 -28
- ksadk/skills/tool_defs.py +108 -3
- ksadk/studio/api.py +810 -93
- ksadk/studio/api_catalog_routes.py +18 -0
- ksadk/studio/api_contracts.py +8 -2
- ksadk/studio/api_plugin_routes.py +562 -104
- ksadk/studio/api_resource_connections.py +221 -0
- ksadk/studio/authoring.py +12 -6
- ksadk/studio/authoring_coordinator.py +23 -16
- ksadk/studio/builder.py +88 -0
- ksadk/studio/capabilities.py +23 -6
- ksadk/studio/channel_connections.py +333 -0
- ksadk/studio/cloud.py +370 -112
- ksadk/studio/cloud_shared_web.py +638 -0
- ksadk/studio/codex_agent_service.py +130 -28
- ksadk/studio/codex_builder.py +242 -3
- ksadk/studio/codex_credentials.py +90 -0
- ksadk/studio/codex_manifest.py +31 -4
- ksadk/studio/codex_plugin_store.py +746 -0
- ksadk/studio/codex_provider_build.py +231 -0
- ksadk/studio/codex_run.py +190 -26
- ksadk/studio/compatibility_report.py +12 -1
- ksadk/studio/compiler.py +88 -14
- ksadk/studio/configuration.py +182 -0
- ksadk/studio/contracts.py +165 -11
- ksadk/studio/dsh_application.py +247 -0
- ksadk/studio/dsh_capability_service.py +1374 -0
- ksadk/studio/dsh_lifecycle.py +58 -0
- ksadk/studio/dsh_models.py +59 -0
- ksadk/studio/dsh_provider_registration.py +367 -21
- ksadk/studio/entry.py +53 -0
- ksadk/studio/event_store.py +36 -5
- ksadk/studio/execution_host.py +525 -0
- ksadk/studio/framework_run.py +32 -10
- ksadk/studio/hosted_kernel.py +89 -14
- ksadk/studio/kernel_registry.py +418 -0
- ksadk/studio/kernel_sqlite_migration.py +181 -0
- ksadk/studio/mcp_runtime.py +11 -3
- ksadk/studio/model_client.py +1180 -66
- ksadk/studio/model_profile_service.py +26 -11
- ksadk/studio/native_documents.py +172 -0
- ksadk/studio/plugin_composition.py +202 -24
- ksadk/studio/plugin_kernel_adapter.py +21 -106
- ksadk/studio/plugin_lifecycle.py +80 -0
- ksadk/studio/plugin_runtime.py +448 -56
- ksadk/studio/provider_recovery.py +59 -0
- ksadk/studio/recovery_page.py +70 -0
- ksadk/studio/resource_authority.py +664 -0
- ksadk/studio/resource_binding_validation.py +77 -0
- ksadk/studio/resource_build_admission.py +196 -0
- ksadk/studio/resource_build_materializer.py +149 -0
- ksadk/studio/resource_catalog.py +149 -2
- ksadk/studio/resource_connections.py +220 -0
- ksadk/studio/run_activity.py +895 -0
- ksadk/studio/run_documents.py +127 -0
- ksadk/studio/run_service.py +789 -83
- ksadk/studio/runtime_catalog.py +53 -10
- ksadk/studio/runtime_source.py +16 -8
- ksadk/studio/scheduler_assistant.py +317 -0
- ksadk/studio/scheduler_runtime.py +32 -280
- ksadk/studio/scheduler_service.py +3 -0
- ksadk/studio/service.py +861 -120
- ksadk/studio/shared_web.py +579 -82
- ksadk/studio/static/assets/ChannelsPage-D_PGmKVq.js +8 -0
- ksadk/studio/static/assets/CodeBlock-DMIf4h33-UIqo-BPW.js +44 -0
- ksadk/studio/static/assets/ExecutionTree-v3HexvPo-Bk8wzGSy.js +1 -0
- ksadk/studio/static/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- ksadk/studio/static/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- ksadk/studio/static/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- ksadk/studio/static/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- ksadk/studio/static/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- ksadk/studio/static/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- ksadk/studio/static/assets/MathMessageMarkdown-BesSeCYl-BEOyPC3w.css +1 -0
- ksadk/studio/static/assets/MathMessageMarkdown-BesSeCYl-OhNwH4K9.js +8 -0
- ksadk/studio/static/assets/MermaidBlock-H_0qzQDf-BdXtxug_.js +11 -0
- ksadk/studio/static/assets/MessageMarkdown-CwMTvgFj-g0I-I4dD.js +59 -0
- ksadk/studio/static/assets/PrismRenderer-5pK6tUdb.js +3 -0
- ksadk/studio/static/assets/TeamsPage-DqX4HWKQ.js +3 -0
- ksadk/studio/static/assets/abnfDiagram-VCTEODGH-Bnb1O3tA.js +1 -0
- ksadk/studio/static/assets/arc-Pry-7G1_.js +1 -0
- ksadk/studio/static/assets/architecture-7GRP2DOG-BMOPAkUC.js +1 -0
- ksadk/studio/static/assets/architectureDiagram-5GKGNRK7-5Zn1gqJv.js +36 -0
- ksadk/studio/static/assets/array-BifhSqXX.js +1 -0
- ksadk/studio/static/assets/blockDiagram-I7D4REHJ-CLB4L11u.js +129 -0
- ksadk/studio/static/assets/c4Diagram-7LVT6UL2-BOma_Z_F.js +38 -0
- ksadk/studio/static/assets/channel-r8itPEik.js +1 -0
- ksadk/studio/static/assets/chunk-2E4U76K2-BFf_huql.js +2 -0
- ksadk/studio/static/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- ksadk/studio/static/assets/chunk-2ZTRR5NV-C2qZJila.js +2 -0
- ksadk/studio/static/assets/chunk-3Z5EZCMW-DMniOlPd.js +1 -0
- ksadk/studio/static/assets/chunk-4HAMMTFA-DVtDP4rm.js +62 -0
- ksadk/studio/static/assets/chunk-5V3GS4D5-i-ZDOEnP.js +1 -0
- ksadk/studio/static/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- ksadk/studio/static/assets/chunk-6AZGARVD-BB6FqMl-.js +1 -0
- ksadk/studio/static/assets/chunk-6EIED4P4-DdUJkP0f.js +1 -0
- ksadk/studio/static/assets/chunk-6K3QC6MW-CqVsVn-y.js +1 -0
- ksadk/studio/static/assets/chunk-6TQVIW2G-DKlLbNIP.js +1 -0
- ksadk/studio/static/assets/chunk-747NJXEK-BFkZPlab.js +2 -0
- ksadk/studio/static/assets/chunk-75Z2AOVW-BZNhM4vB.js +2 -0
- ksadk/studio/static/assets/chunk-CLGD4ZFX-D-GTtyKs.js +1 -0
- ksadk/studio/static/assets/chunk-DU6HZSFF-CpEZCUgg.js +128 -0
- ksadk/studio/static/assets/chunk-F27PBJKO-D7pvKf0C.js +1 -0
- ksadk/studio/static/assets/chunk-FOHPRMQF-DHwB1DNv.js +161 -0
- ksadk/studio/static/assets/chunk-GMAD6QVW-DE_8H717.js +72 -0
- ksadk/studio/static/assets/chunk-GVQU2GXP-BfEB0lNv.js +1 -0
- ksadk/studio/static/assets/chunk-I5DQTOEV-Hm2LfiYI.js +1 -0
- ksadk/studio/static/assets/chunk-ICYGCRZG-749o-pGJ.js +1 -0
- ksadk/studio/static/assets/chunk-IH6LHLGP-CC6I9F3i.js +1 -0
- ksadk/studio/static/assets/chunk-IMKFNOWR-D38axYOR.js +231 -0
- ksadk/studio/static/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- ksadk/studio/static/assets/chunk-KI3K4JFJ-Rma-0k_M.js +1 -0
- ksadk/studio/static/assets/chunk-L3NEJ4N5-Dq5xn7aL.js +1 -0
- ksadk/studio/static/assets/chunk-LNGE3PJU-DWpGwHrW.js +34 -0
- ksadk/studio/static/assets/chunk-OSK3NFVY-DiCQjx9b.js +10 -0
- ksadk/studio/static/assets/chunk-OUJLGHUK-D0j2JRSg.js +2 -0
- ksadk/studio/static/assets/chunk-P2QGCYS3-VdhKmfYq.js +1 -0
- ksadk/studio/static/assets/chunk-POPQ4Y6H-C1_rzO5M.js +1 -0
- ksadk/studio/static/assets/chunk-PWAF6VOD-B45HvflI.js +1 -0
- ksadk/studio/static/assets/chunk-SHT3W25Y-DsCFnRVE.js +168 -0
- ksadk/studio/static/assets/chunk-SVP7TREG-DljApgRX.js +88 -0
- ksadk/studio/static/assets/chunk-TICWLB2K-DWqRtyqa.js +206 -0
- ksadk/studio/static/assets/chunk-TLUHSLCS-DUeGgnD5.js +2 -0
- ksadk/studio/static/assets/chunk-UY3FDG6J-CnmaAkG5.js +1 -0
- ksadk/studio/static/assets/chunk-XHIXRSVI-BFQ6B8BE.js +1 -0
- ksadk/studio/static/assets/chunk-XXDRQBXY-DqAYeljN.js +1 -0
- ksadk/studio/static/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- ksadk/studio/static/assets/classDiagram-ZZMXUADV-DjeMNJmu.js +1 -0
- ksadk/studio/static/assets/classDiagram-v2-VYDZK3BY-DjeMNJmu.js +1 -0
- ksadk/studio/static/assets/clsx-DB0hHKMi.js +1 -0
- ksadk/studio/static/assets/cose-bilkent-JH36ORCC-om8AY0Mc.js +1 -0
- ksadk/studio/static/assets/createLucideIcon-DR0LGTP5.js +1 -0
- ksadk/studio/static/assets/cynefin-OW5HDTMX-7tP7QfM3.js +1 -0
- ksadk/studio/static/assets/cynefinDiagram-5FMLGOSQ-6BrmK52S.js +62 -0
- ksadk/studio/static/assets/cytoscape.esm-CGd-uY3x.js +321 -0
- ksadk/studio/static/assets/dagre-GXQ25YYZ-buZBRlwz.js +4 -0
- ksadk/studio/static/assets/dagre-PrKaheQc.js +1 -0
- ksadk/studio/static/assets/defaultLocale-BFoDCU3G.js +1 -0
- ksadk/studio/static/assets/diagram-S7CK7UJ4-C6aH78c4.js +30 -0
- ksadk/studio/static/assets/diagram-UQ7AKVKN-DY_tQ6C_.js +41 -0
- ksadk/studio/static/assets/diagram-VSXAHHWV-DX9p5cQT.js +3 -0
- ksadk/studio/static/assets/diagram-VX7I27RA-DB2WSH8X.js +24 -0
- ksadk/studio/static/assets/diagram-Z3DM3KII-Ce1BJwHE.js +24 -0
- ksadk/studio/static/assets/dist-v5Q1xZ2K.js +1 -0
- ksadk/studio/static/assets/ebnfDiagram-PWID7BFC-Daukxwqf.js +1 -0
- ksadk/studio/static/assets/erDiagram-RLTQ6QDP-CGthTMmJ.js +99 -0
- ksadk/studio/static/assets/eventmodeling-NTZA5JFV-BR2FzK4R.js +1 -0
- ksadk/studio/static/assets/flowDiagram-HODETNUW-CZ1tEenp.js +1 -0
- ksadk/studio/static/assets/ganttDiagram-EL5Y4UJY-BfoAZW9X.js +292 -0
- ksadk/studio/static/assets/gitGraph-4MIJSDKK-DYlXv_yJ.js +1 -0
- ksadk/studio/static/assets/gitGraphDiagram-WWUBYQGX-S985OIJF.js +106 -0
- ksadk/studio/static/assets/graphlib-DS17s2tU.js +1 -0
- ksadk/studio/static/assets/iconfont-4HN3C9Pg.woff2 +0 -0
- ksadk/studio/static/assets/index-CfEnms6Y.css +2 -0
- ksadk/studio/static/assets/index-Dp61rOWZ.js +0 -0
- ksadk/studio/static/assets/index.dom-BQenBtFm.js +1 -0
- ksadk/studio/static/assets/info-A6RAGUB7-BlU-8_xu.js +1 -0
- ksadk/studio/static/assets/infoDiagram-27XIBGKW-CO3W8tFn.js +2 -0
- ksadk/studio/static/assets/init-C-OQMol4.js +1 -0
- ksadk/studio/static/assets/ishikawaDiagram-5VMMS53U-BUGEl8cO.js +70 -0
- ksadk/studio/static/assets/journeyDiagram-3NMN7TZE-Ch2ZYxvr.js +139 -0
- ksadk/studio/static/assets/kanban-definition-UXKFOSKX-Da-Fp4vC.js +89 -0
- ksadk/studio/static/assets/katex-DhON-x74.js +257 -0
- ksadk/studio/static/assets/kingsoft-cloud-BOcLiQ3e.ico +0 -0
- ksadk/studio/static/assets/line-BDaeq300.js +1 -0
- ksadk/studio/static/assets/linear-DmucvK78.js +1 -0
- ksadk/studio/static/assets/mermaid-parser.core-Cj5mYdBi.js +4 -0
- ksadk/studio/static/assets/mindmap-definition-YA3MSWOX-CvlsVQ38.js +96 -0
- ksadk/studio/static/assets/ordinal-BDEzSJ7C.js +1 -0
- ksadk/studio/static/assets/packet-AYTQ26CC-BSJEH9Lb.js +1 -0
- ksadk/studio/static/assets/path-fybaL0A-.js +1 -0
- ksadk/studio/static/assets/pegDiagram-XKGWAZYB-BuHuNgT5.js +1 -0
- ksadk/studio/static/assets/pie-WAS4IAKB-qxv01Y8W.js +1 -0
- ksadk/studio/static/assets/pieDiagram-E7YTZNPT-DlrWiSOv.js +39 -0
- ksadk/studio/static/assets/preload-helper-CO68xz6u.js +1 -0
- ksadk/studio/static/assets/presentation-yYw7xK60-BmcZriri.js +1 -0
- ksadk/studio/static/assets/quadrantDiagram-AXDQQJYC-BA3hpUZM.js +7 -0
- ksadk/studio/static/assets/radar-RG4KPBEZ-B6h6pWOs.js +1 -0
- ksadk/studio/static/assets/railroad-74A4TZTK-CGtTWdGc.js +1 -0
- ksadk/studio/static/assets/railroad-abnf-HS5TGJTU-Cq7y22IA.js +1 -0
- ksadk/studio/static/assets/railroad-ebnf-LZEXJU2U-V0aQVv9m.js +1 -0
- ksadk/studio/static/assets/railroad-peg-WCYAUIDC-BQiIsX_p.js +1 -0
- ksadk/studio/static/assets/railroadDiagram-O6MQD6OU-BxpAazym.js +1 -0
- ksadk/studio/static/assets/requirementDiagram-BXWQKSXE-E-Ge0FcV.js +84 -0
- ksadk/studio/static/assets/rolldown-runtime-Dy4uBu1J-DK3Fl9T5.js +1 -0
- ksadk/studio/static/assets/rough.esm-Dy-Kn_BL.js +1 -0
- ksadk/studio/static/assets/sankeyDiagram-P5KCCOFB-CA6YKhQ3.js +40 -0
- ksadk/studio/static/assets/sequenceDiagram-WJ2MYXX4-CcElXGg4.js +162 -0
- ksadk/studio/static/assets/sizeCapture-INFHLROL-B0uUizjq.js +1 -0
- ksadk/studio/static/assets/src-B6xuSHsQ.js +1 -0
- ksadk/studio/static/assets/stateDiagram-D77RDMKH-DdXZOFqY.js +1 -0
- ksadk/studio/static/assets/stateDiagram-v2-MP3YSRHH-CJbOM-QK.js +1 -0
- ksadk/studio/static/assets/swimlanes-42K2YHIH-NtgEEHLY.js +1 -0
- ksadk/studio/static/assets/swimlanesDiagram-VR7AAH4N-D-AVlW8B.js +8 -0
- ksadk/studio/static/assets/timeline-definition-24CTP7MA-CIuYhNy3.js +120 -0
- ksadk/studio/static/assets/treeView-Q6P3EWNA-BTTYcvrF.js +1 -0
- ksadk/studio/static/assets/treemap-WGGIJYW6-Cg7tyfH8.js +1 -0
- ksadk/studio/static/assets/vennDiagram-4TSXK5OY-7HbApbrv.js +34 -0
- ksadk/studio/static/assets/wardley-WFR3VGLG-C6N1rULY.js +1 -0
- ksadk/studio/static/assets/wardleyDiagram-VM6X3IG4-CMJiaedy.js +78 -0
- ksadk/studio/static/assets/xychartDiagram-S5SC5T6Z-BdgtMvW2.js +7 -0
- ksadk/studio/static/index.html +8 -2
- ksadk/studio/teams_catalog.py +133 -0
- ksadk/studio/teams_installation.py +214 -0
- ksadk/studio/teams_node.py +545 -0
- ksadk/studio/teams_node_runner.py +69 -0
- ksadk/studio/teams_remote.py +274 -0
- ksadk/studio/templates.py +40 -19
- ksadk/studio/validator.py +13 -0
- ksadk/studio/workspace.py +19 -4
- ksadk/studio/workspace_plugins.py +136 -0
- ksadk/studio/workspace_registry.py +286 -0
- ksadk/toolsets/__init__.py +11 -0
- ksadk/toolsets/skills.py +446 -378
- ksadk/toolsets/workspace.py +3 -3
- ksadk/toolsets/workspace_identity.py +15 -0
- ksadk/version.py +1 -1
- ksadk_runtime_common/memory_backend/providers/lancedb.py +56 -2
- ksadk_runtime_common/schemas/memory_backend_manifest.schema.json +148 -148
- ksadk_runtime_common/workspace_files/router.py +26 -13
- agentengine_sdk_python-0.8.3.dist-info/RECORD +0 -833
- ksadk/server/static/assets/MathMessageMarkdown-D9uB55Oh.js +0 -8
- ksadk/server/static/assets/architecture-7GRP2DOG-Dk9nRQ6-.js +0 -1
- ksadk/server/static/assets/channel-s354Yo6o.js +0 -1
- ksadk/server/static/assets/chunk-2Q5K7J3B-CDpPpKR5.js +0 -1
- ksadk/server/static/assets/classDiagram-DTDB5LWJ-Dd0nucl9.js +0 -1
- ksadk/server/static/assets/classDiagram-v2-JRS7N3AN-Dd0nucl9.js +0 -1
- ksadk/server/static/assets/cynefin-OW5HDTMX-BBlT4uMf.js +0 -1
- ksadk/server/static/assets/dist-DdRBIceA.js +0 -1
- ksadk/server/static/assets/eventmodeling-NTZA5JFV-Dr5okd8V.js +0 -1
- ksadk/server/static/assets/flowDiagram-A5DVABFB-VEO-cVcO.js +0 -1
- ksadk/server/static/assets/gitGraph-4MIJSDKK-CKiKY9Cf.js +0 -1
- ksadk/server/static/assets/index-B2k_urY8.js +0 -241
- ksadk/server/static/assets/index-ILPt6IWF.css +0 -2
- ksadk/server/static/assets/info-A6RAGUB7-ubA4Nm_p.js +0 -1
- ksadk/server/static/assets/packet-AYTQ26CC-C7R3NUlK.js +0 -1
- ksadk/server/static/assets/pie-WAS4IAKB-BIT6qjr3.js +0 -1
- ksadk/server/static/assets/radar-RG4KPBEZ-l5noef9X.js +0 -1
- ksadk/server/static/assets/railroad-74A4TZTK-Baea6ZvM.js +0 -1
- ksadk/server/static/assets/railroad-abnf-HS5TGJTU-OKn8zZnU.js +0 -1
- ksadk/server/static/assets/railroad-ebnf-LZEXJU2U-DdKAKC4l.js +0 -1
- ksadk/server/static/assets/railroad-peg-WCYAUIDC-Bds8udlG.js +0 -1
- ksadk/server/static/assets/stateDiagram-v2-4QOOHH4V-5b-8o2iS.js +0 -1
- ksadk/server/static/assets/swimlanesDiagram-VK2B7HYN-uf6WPRA4.js +0 -8
- ksadk/server/static/assets/treeView-Q6P3EWNA-CK5CSrSO.js +0 -1
- ksadk/server/static/assets/treemap-WGGIJYW6-Ch2BKgAM.js +0 -1
- ksadk/server/static/assets/wardley-WFR3VGLG-YW3Q0ml_.js +0 -1
- ksadk/studio/static/assets/PrismRenderer-IYN-ffww.js +0 -3
- ksadk/studio/static/assets/index-DXs1Eq4M.css +0 -1
- ksadk/studio/static/assets/index-DeDqwTsD.js +0 -283
- {agentengine_sdk_python-0.8.3.dist-info → agentengine_sdk_python-0.8.5.dist-info}/WHEEL +0 -0
- {agentengine_sdk_python-0.8.3.dist-info → agentengine_sdk_python-0.8.5.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.8.3.dist-info → agentengine_sdk_python-0.8.5.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.8.3.dist-info → agentengine_sdk_python-0.8.5.dist-info}/top_level.txt +0 -0
ksadk/studio/model_client.py
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
|
+
import hashlib
|
|
7
|
+
import html
|
|
6
8
|
import ipaddress
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
7
11
|
import os
|
|
12
|
+
import re
|
|
8
13
|
import socket
|
|
14
|
+
import time
|
|
15
|
+
from collections.abc import AsyncIterator
|
|
9
16
|
from dataclasses import dataclass, field
|
|
10
17
|
from threading import RLock
|
|
11
18
|
from typing import Any, Awaitable, Callable
|
|
@@ -28,6 +35,79 @@ _LENGTH_FINISH_REASONS = {"length", "incomplete", "max_output_tokens"}
|
|
|
28
35
|
#: length 截断重试时把 max_tokens 提到的目标值;未配置 max_tokens 的 profile
|
|
29
36
|
#: 首次截断重试也用该值兜底(authoring 输出完整 JSON 需要宽松上限)。
|
|
30
37
|
_LENGTH_RETRY_TARGET_TOKENS = 16384
|
|
38
|
+
_REPETITION_MIN_UNIT_CHARS = 24
|
|
39
|
+
_REPETITION_MAX_UNIT_CHARS = 512
|
|
40
|
+
_REPETITION_COUNT = 4
|
|
41
|
+
|
|
42
|
+
_LOGGER = logging.getLogger(__name__)
|
|
43
|
+
|
|
44
|
+
_DSML_INVOKE_RE = re.compile(
|
|
45
|
+
r"<\s*(?:\|\s*){1,2}DSML\s*(?:\|\s*){1,2}invoke\b(?P<attrs>[^>]*)>"
|
|
46
|
+
r"(?P<body>.*?)"
|
|
47
|
+
r"<\s*(?:\|\s*){1,2}DSML\s*(?:\|\s*){1,2}/invoke\s*>",
|
|
48
|
+
re.IGNORECASE | re.DOTALL,
|
|
49
|
+
)
|
|
50
|
+
_DSML_PARAMETER_RE = re.compile(
|
|
51
|
+
r"<\s*(?:\|\s*){1,2}DSML\s*(?:\|\s*){1,2}parameter\b(?P<attrs>[^>]*)>"
|
|
52
|
+
r"(?P<value>.*?)"
|
|
53
|
+
r"<\s*(?:\|\s*){1,2}DSML\s*(?:\|\s*){1,2}/parameter\s*>",
|
|
54
|
+
re.IGNORECASE | re.DOTALL,
|
|
55
|
+
)
|
|
56
|
+
_DSML_CALLS_WRAPPER_RE = re.compile(
|
|
57
|
+
r"<\s*(?:\|\s*){1,2}DSML\s*(?:\|\s*){1,2}/?calls\s*>",
|
|
58
|
+
re.IGNORECASE,
|
|
59
|
+
)
|
|
60
|
+
_DSML_ATTRIBUTE_RE = re.compile(
|
|
61
|
+
r"(?P<name>[A-Za-z_][\w.-]*)\s*=\s*(?P<quote>['\"])(?P<value>.*?)(?P=quote)",
|
|
62
|
+
re.DOTALL,
|
|
63
|
+
)
|
|
64
|
+
_DSML_SIMPLE_ARGUMENT_RE = re.compile(
|
|
65
|
+
r"<(?P<name>[A-Za-z_][\w.-]*)>(?P<value>.*?)</(?P=name)>",
|
|
66
|
+
re.DOTALL,
|
|
67
|
+
)
|
|
68
|
+
_DSML_STREAM_LOOKBEHIND = 64
|
|
69
|
+
_GLM_TOOL_CALL_RE = re.compile(
|
|
70
|
+
r"<tool_call>\s*(?P<name>[A-Za-z_][\w.-]*)\s*(?P<body>.*?)</tool_call>",
|
|
71
|
+
re.IGNORECASE | re.DOTALL,
|
|
72
|
+
)
|
|
73
|
+
_GLM_ARGUMENT_TAG_RE = re.compile(
|
|
74
|
+
r"<(?P<tag>arg_key|arg_value)>(?P<value>.*?)</(?:arg_key|arg_value)>",
|
|
75
|
+
re.IGNORECASE | re.DOTALL,
|
|
76
|
+
)
|
|
77
|
+
_GLM_ASSIGNMENT_RE = re.compile(
|
|
78
|
+
r"^(?P<name>[A-Za-z_][\w.-]*)\s*=\s*(?P<quote>['\"])(?P<value>.*)(?P=quote)$",
|
|
79
|
+
re.DOTALL,
|
|
80
|
+
)
|
|
81
|
+
_GLM_ARGUMENT_KEY_RESIDUE_RE = re.compile(
|
|
82
|
+
r"(?:</?\s*arg_(?:key|value)\s*>)+$",
|
|
83
|
+
re.IGNORECASE,
|
|
84
|
+
)
|
|
85
|
+
_GLM_MALFORMED_TOOL_START_RE = re.compile(
|
|
86
|
+
r"<\s*tool_call\s*>\s*(?P<name>[A-Za-z_][\w.-]*)",
|
|
87
|
+
re.IGNORECASE,
|
|
88
|
+
)
|
|
89
|
+
_GLM_MALFORMED_KEY_RE = re.compile(
|
|
90
|
+
r"(?:<\s*arg_value\s*>\s*)?(?P<name>[A-Za-z_][\w.-]*)\s*"
|
|
91
|
+
r"</\s*arg_key\s*>",
|
|
92
|
+
re.IGNORECASE,
|
|
93
|
+
)
|
|
94
|
+
_GLM_MALFORMED_ASSIGNMENT_RE = re.compile(
|
|
95
|
+
r"(?P<name>[A-Za-z_][\w.-]*)\s*=\s*(?P<quote>['\"])(?P<value>.*?)(?P=quote)"
|
|
96
|
+
r"\s*</\s*arg_value\s*>",
|
|
97
|
+
re.IGNORECASE | re.DOTALL,
|
|
98
|
+
)
|
|
99
|
+
_SAFE_TOOL_NAME_ALIASES = {
|
|
100
|
+
# GLM 5.2 has emitted this shorter DSML name for the explicitly
|
|
101
|
+
# advertised workspace writer. The alias is accepted only when the target
|
|
102
|
+
# tool is present in the current request's declared schema.
|
|
103
|
+
"writefile": "writeworkspacefile",
|
|
104
|
+
}
|
|
105
|
+
_SAFE_TOOL_ARGUMENT_ALIASES = {
|
|
106
|
+
"write_workspace_file": {
|
|
107
|
+
"filename": "path",
|
|
108
|
+
"filepath": "path",
|
|
109
|
+
},
|
|
110
|
+
}
|
|
31
111
|
|
|
32
112
|
|
|
33
113
|
@dataclass(frozen=True)
|
|
@@ -38,6 +118,89 @@ class ToolCall:
|
|
|
38
118
|
raw: dict[str, Any] = field(default_factory=dict)
|
|
39
119
|
|
|
40
120
|
|
|
121
|
+
def _tool_call_arguments_text(value: Any) -> str:
|
|
122
|
+
"""Return provider tool arguments as valid JSON text when possible."""
|
|
123
|
+
|
|
124
|
+
if value is None:
|
|
125
|
+
return ""
|
|
126
|
+
if isinstance(value, str):
|
|
127
|
+
return value
|
|
128
|
+
return json.dumps(value, ensure_ascii=False)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _accumulate_chat_tool_calls(
|
|
132
|
+
fragments: dict[int, dict[str, str]],
|
|
133
|
+
calls: Any,
|
|
134
|
+
*,
|
|
135
|
+
snapshot: bool,
|
|
136
|
+
) -> None:
|
|
137
|
+
"""Merge incremental or final-snapshot chat-completion tool calls.
|
|
138
|
+
|
|
139
|
+
Most OpenAI-compatible providers stream arguments through
|
|
140
|
+
``choice.delta.tool_calls``. Some gateways only include the complete
|
|
141
|
+
function call on the finishing choice (under ``message.tool_calls`` or
|
|
142
|
+
``choice.tool_calls``). Supporting both forms prevents a valid long call
|
|
143
|
+
from degrading to an empty ``{}`` invocation at stream completion.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
if not isinstance(calls, list):
|
|
147
|
+
return
|
|
148
|
+
for fallback_index, raw_call in enumerate(calls):
|
|
149
|
+
if not isinstance(raw_call, dict):
|
|
150
|
+
continue
|
|
151
|
+
raw_index = raw_call.get("index")
|
|
152
|
+
try:
|
|
153
|
+
index = int(raw_index if raw_index is not None else fallback_index)
|
|
154
|
+
except (TypeError, ValueError):
|
|
155
|
+
index = fallback_index
|
|
156
|
+
function = raw_call.get("function") or {}
|
|
157
|
+
if not isinstance(function, dict):
|
|
158
|
+
function = {}
|
|
159
|
+
current = fragments.setdefault(index, {"id": "", "name": "", "arguments": ""})
|
|
160
|
+
incoming_id = str(raw_call.get("id") or "")
|
|
161
|
+
incoming_name = str(function.get("name") or raw_call.get("name") or "")
|
|
162
|
+
if "arguments" in function:
|
|
163
|
+
raw_arguments = function.get("arguments")
|
|
164
|
+
elif "arguments" in raw_call:
|
|
165
|
+
raw_arguments = raw_call.get("arguments")
|
|
166
|
+
elif "input" in function:
|
|
167
|
+
raw_arguments = function.get("input")
|
|
168
|
+
else:
|
|
169
|
+
raw_arguments = raw_call.get("input")
|
|
170
|
+
incoming_arguments = _tool_call_arguments_text(raw_arguments)
|
|
171
|
+
if snapshot:
|
|
172
|
+
if incoming_id:
|
|
173
|
+
current["id"] = incoming_id
|
|
174
|
+
if incoming_name:
|
|
175
|
+
current["name"] = incoming_name
|
|
176
|
+
# Do not let an empty/default snapshot overwrite arguments that
|
|
177
|
+
# were already reconstructed from deltas.
|
|
178
|
+
if incoming_arguments and (incoming_arguments != "{}" or not current["arguments"]):
|
|
179
|
+
current["arguments"] = incoming_arguments
|
|
180
|
+
continue
|
|
181
|
+
current["id"] += incoming_id
|
|
182
|
+
current["name"] += incoming_name
|
|
183
|
+
current["arguments"] += incoming_arguments
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _reasoning_from_responses_output(output: Any) -> str:
|
|
187
|
+
"""从 Responses wire 的 output 数组提取 reasoning 项文本(无则空串)。"""
|
|
188
|
+
|
|
189
|
+
if not isinstance(output, list):
|
|
190
|
+
return ""
|
|
191
|
+
parts: list[str] = []
|
|
192
|
+
for item in output:
|
|
193
|
+
if not isinstance(item, dict) or item.get("type") != "reasoning":
|
|
194
|
+
continue
|
|
195
|
+
for block in item.get("summary") or []:
|
|
196
|
+
if isinstance(block, dict) and block.get("type") in {"summary_text", "text"}:
|
|
197
|
+
parts.append(str(block.get("text") or ""))
|
|
198
|
+
for part in item.get("content") or []:
|
|
199
|
+
if isinstance(part, dict) and part.get("type") in {"reasoning_text", "text"}:
|
|
200
|
+
parts.append(str(part.get("text") or ""))
|
|
201
|
+
return "".join(parts)
|
|
202
|
+
|
|
203
|
+
|
|
41
204
|
@dataclass(frozen=True)
|
|
42
205
|
class ModelResponse:
|
|
43
206
|
content: str
|
|
@@ -45,13 +208,482 @@ class ModelResponse:
|
|
|
45
208
|
usage: Usage
|
|
46
209
|
tool_calls: list[ToolCall]
|
|
47
210
|
raw_message: dict[str, Any]
|
|
211
|
+
#: 推理文本(chat wire 的 reasoning_content / Responses wire 的 reasoning 项)。
|
|
212
|
+
reasoning: str = ""
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@dataclass(frozen=True)
|
|
216
|
+
class ModelStreamChunk:
|
|
217
|
+
"""One OpenAI-compatible chat stream delta."""
|
|
218
|
+
|
|
219
|
+
text: str = ""
|
|
220
|
+
reasoning: str = ""
|
|
221
|
+
tool_calls: tuple[ToolCall, ...] = ()
|
|
222
|
+
usage: Usage | None = None
|
|
223
|
+
done: bool = False
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _dsml_attributes(value: str) -> dict[str, str]:
|
|
227
|
+
return {
|
|
228
|
+
match.group("name"): html.unescape(match.group("value"))
|
|
229
|
+
for match in _DSML_ATTRIBUTE_RE.finditer(value)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _declared_tool_names(tools: list[dict[str, Any]] | None) -> dict[str, str]:
|
|
234
|
+
declared: dict[str, str] = {}
|
|
235
|
+
for tool in tools or []:
|
|
236
|
+
function = tool.get("function") or tool
|
|
237
|
+
name = str(function.get("name") or "").strip()
|
|
238
|
+
if name:
|
|
239
|
+
declared[re.sub(r"[^a-z0-9]", "", name.lower())] = name
|
|
240
|
+
return declared
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _canonical_declared_tool_name(
|
|
244
|
+
requested_name: str,
|
|
245
|
+
declared: dict[str, str],
|
|
246
|
+
) -> str | None:
|
|
247
|
+
key = re.sub(r"[^a-z0-9]", "", requested_name.lower())
|
|
248
|
+
canonical = declared.get(key)
|
|
249
|
+
if canonical:
|
|
250
|
+
return canonical
|
|
251
|
+
alias_target = _SAFE_TOOL_NAME_ALIASES.get(key)
|
|
252
|
+
canonical = declared.get(alias_target or "")
|
|
253
|
+
if canonical:
|
|
254
|
+
return canonical
|
|
255
|
+
# GLM 5.2 occasionally appends a leaked textual call id to a declared
|
|
256
|
+
# name (for example ``write_file_ide49a``). Only strip this narrowly
|
|
257
|
+
# shaped suffix and only accept the prefix when it resolves to an
|
|
258
|
+
# explicitly advertised tool/alias.
|
|
259
|
+
leaked_id = re.fullmatch(r"(?P<name>.+?)_id[a-z0-9]+", requested_name, re.IGNORECASE)
|
|
260
|
+
if leaked_id:
|
|
261
|
+
prefix = re.sub(r"[^a-z0-9]", "", leaked_id.group("name").lower())
|
|
262
|
+
return declared.get(prefix) or declared.get(_SAFE_TOOL_NAME_ALIASES.get(prefix, ""))
|
|
263
|
+
return None
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _canonical_declared_argument_name(
|
|
267
|
+
requested_name: str,
|
|
268
|
+
canonical_tool_name: str,
|
|
269
|
+
declared_properties: frozenset[str],
|
|
270
|
+
) -> str | None:
|
|
271
|
+
if requested_name in declared_properties:
|
|
272
|
+
return requested_name
|
|
273
|
+
key = re.sub(r"[^a-z0-9]", "", requested_name.lower())
|
|
274
|
+
for declared_name in declared_properties:
|
|
275
|
+
if re.sub(r"[^a-z0-9]", "", declared_name.lower()) == key:
|
|
276
|
+
return declared_name
|
|
277
|
+
alias = _SAFE_TOOL_ARGUMENT_ALIASES.get(canonical_tool_name, {}).get(key)
|
|
278
|
+
return alias if alias in declared_properties else None
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _declared_tool_properties(
|
|
282
|
+
tools: list[dict[str, Any]] | None,
|
|
283
|
+
) -> dict[str, frozenset[str]]:
|
|
284
|
+
"""Return declared JSON argument names keyed by canonical tool name.
|
|
285
|
+
|
|
286
|
+
A few OpenAI-compatible GLM gateways have emitted a native function-call
|
|
287
|
+
JSON key such as ``task</arg_key>``. We only repair that wire residue when
|
|
288
|
+
the cleaned name is an explicitly declared property; arbitrary model keys
|
|
289
|
+
are never guessed or renamed.
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
declared: dict[str, frozenset[str]] = {}
|
|
293
|
+
for tool in tools or []:
|
|
294
|
+
function = tool.get("function") or tool
|
|
295
|
+
name = str(function.get("name") or "").strip()
|
|
296
|
+
parameters = function.get("parameters") or {}
|
|
297
|
+
properties = parameters.get("properties") if isinstance(parameters, dict) else {}
|
|
298
|
+
if name:
|
|
299
|
+
declared[name] = (
|
|
300
|
+
frozenset(str(key) for key in properties if isinstance(key, str))
|
|
301
|
+
if isinstance(properties, dict)
|
|
302
|
+
else frozenset()
|
|
303
|
+
)
|
|
304
|
+
return declared
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _normalize_tool_call_arguments(
|
|
308
|
+
calls: tuple[ToolCall, ...] | list[ToolCall],
|
|
309
|
+
tools: list[dict[str, Any]] | None,
|
|
310
|
+
) -> tuple[ToolCall, ...]:
|
|
311
|
+
"""Normalize known provider wire residue in native function arguments."""
|
|
312
|
+
|
|
313
|
+
properties_by_tool = _declared_tool_properties(tools)
|
|
314
|
+
declared_names = _declared_tool_names(tools)
|
|
315
|
+
normalized_calls: list[ToolCall] = []
|
|
316
|
+
for call in calls:
|
|
317
|
+
canonical_name = _canonical_declared_tool_name(call.name, declared_names) or call.name
|
|
318
|
+
properties = properties_by_tool.get(canonical_name, frozenset())
|
|
319
|
+
if not properties:
|
|
320
|
+
normalized_calls.append(
|
|
321
|
+
ToolCall(
|
|
322
|
+
id=call.id,
|
|
323
|
+
name=canonical_name,
|
|
324
|
+
arguments=call.arguments,
|
|
325
|
+
raw=call.raw,
|
|
326
|
+
)
|
|
327
|
+
)
|
|
328
|
+
continue
|
|
329
|
+
try:
|
|
330
|
+
arguments = json.loads(call.arguments or "{}")
|
|
331
|
+
except (TypeError, ValueError):
|
|
332
|
+
normalized_calls.append(call)
|
|
333
|
+
continue
|
|
334
|
+
if not isinstance(arguments, dict):
|
|
335
|
+
normalized_calls.append(call)
|
|
336
|
+
continue
|
|
337
|
+
repaired: dict[str, Any] = {}
|
|
338
|
+
changed = False
|
|
339
|
+
for raw_key, value in arguments.items():
|
|
340
|
+
key = str(raw_key)
|
|
341
|
+
candidate = _GLM_ARGUMENT_KEY_RESIDUE_RE.sub("", html.unescape(key).strip()).strip()
|
|
342
|
+
if key not in properties and candidate in properties:
|
|
343
|
+
key = candidate
|
|
344
|
+
changed = True
|
|
345
|
+
if key in repaired:
|
|
346
|
+
raise StudioError(
|
|
347
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
348
|
+
"模型返回了重复的工具参数",
|
|
349
|
+
status_code=502,
|
|
350
|
+
details={"toolName": call.name, "argument": key},
|
|
351
|
+
)
|
|
352
|
+
repaired[key] = value
|
|
353
|
+
normalized_calls.append(
|
|
354
|
+
ToolCall(
|
|
355
|
+
id=call.id,
|
|
356
|
+
name=canonical_name,
|
|
357
|
+
arguments=(json.dumps(repaired, ensure_ascii=False) if changed else call.arguments),
|
|
358
|
+
raw=call.raw,
|
|
359
|
+
)
|
|
360
|
+
)
|
|
361
|
+
return tuple(normalized_calls)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _dsml_argument_value(raw: str, attributes: dict[str, str]) -> Any:
|
|
365
|
+
value = html.unescape(raw.strip())
|
|
366
|
+
if attributes.get("string", "").lower() == "true":
|
|
367
|
+
return value
|
|
368
|
+
try:
|
|
369
|
+
return json.loads(value)
|
|
370
|
+
except (TypeError, ValueError):
|
|
371
|
+
return value
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def _recover_dsml_tool_calls(
|
|
375
|
+
content: str,
|
|
376
|
+
tools: list[dict[str, Any]] | None,
|
|
377
|
+
) -> tuple[str, list[ToolCall], bool]:
|
|
378
|
+
"""Recover DeepSeek's textual DSML tool protocol without exposing it as prose."""
|
|
379
|
+
|
|
380
|
+
normalized = content.replace("|", "|")
|
|
381
|
+
if "DSML" not in normalized.upper():
|
|
382
|
+
return content, [], False
|
|
383
|
+
declared = _declared_tool_names(tools)
|
|
384
|
+
calls: list[ToolCall] = []
|
|
385
|
+
spans: list[tuple[int, int]] = []
|
|
386
|
+
for index, match in enumerate(_DSML_INVOKE_RE.finditer(normalized)):
|
|
387
|
+
attrs = _dsml_attributes(match.group("attrs"))
|
|
388
|
+
requested_name = str(attrs.get("name") or "").strip()
|
|
389
|
+
canonical_name = _canonical_declared_tool_name(requested_name, declared)
|
|
390
|
+
if not canonical_name:
|
|
391
|
+
raise StudioError(
|
|
392
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
393
|
+
f"模型返回了未声明的工具调用:{requested_name or 'unknown'}",
|
|
394
|
+
status_code=502,
|
|
395
|
+
details={"toolName": requested_name or "unknown"},
|
|
396
|
+
)
|
|
397
|
+
body = match.group("body")
|
|
398
|
+
arguments: dict[str, Any] = {}
|
|
399
|
+
for parameter in _DSML_PARAMETER_RE.finditer(body):
|
|
400
|
+
parameter_attrs = _dsml_attributes(parameter.group("attrs"))
|
|
401
|
+
parameter_name = str(parameter_attrs.get("name") or "").strip()
|
|
402
|
+
if parameter_name:
|
|
403
|
+
arguments[parameter_name] = _dsml_argument_value(
|
|
404
|
+
parameter.group("value"), parameter_attrs
|
|
405
|
+
)
|
|
406
|
+
if not arguments:
|
|
407
|
+
for parameter in _DSML_SIMPLE_ARGUMENT_RE.finditer(body):
|
|
408
|
+
parameter_name = parameter.group("name")
|
|
409
|
+
arguments[parameter_name] = _dsml_argument_value(parameter.group("value"), {})
|
|
410
|
+
digest = hashlib.sha256(f"{canonical_name}:{index}:{match.group(0)}".encode()).hexdigest()[
|
|
411
|
+
:20
|
|
412
|
+
]
|
|
413
|
+
calls.append(
|
|
414
|
+
ToolCall(
|
|
415
|
+
id=f"call_dsml_{digest}",
|
|
416
|
+
name=canonical_name,
|
|
417
|
+
arguments=json.dumps(arguments, ensure_ascii=False),
|
|
418
|
+
raw={"protocol": "dsml"},
|
|
419
|
+
)
|
|
420
|
+
)
|
|
421
|
+
spans.append(match.span())
|
|
422
|
+
if not calls:
|
|
423
|
+
raise StudioError(
|
|
424
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
425
|
+
"模型返回了无法解析的工具调用",
|
|
426
|
+
status_code=502,
|
|
427
|
+
)
|
|
428
|
+
cleaned = normalized
|
|
429
|
+
for start, end in reversed(spans):
|
|
430
|
+
cleaned = cleaned[:start] + cleaned[end:]
|
|
431
|
+
cleaned = _DSML_CALLS_WRAPPER_RE.sub("", cleaned).strip()
|
|
432
|
+
if "DSML" in cleaned.upper():
|
|
433
|
+
raise StudioError(
|
|
434
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
435
|
+
"模型返回了不完整的工具调用",
|
|
436
|
+
status_code=502,
|
|
437
|
+
)
|
|
438
|
+
return cleaned, calls, True
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def _recover_glm_tool_calls(
|
|
442
|
+
content: str,
|
|
443
|
+
tools: list[dict[str, Any]] | None,
|
|
444
|
+
) -> tuple[str, list[ToolCall], bool]:
|
|
445
|
+
"""Recover GLM's textual ``<tool_call>`` fallback protocol.
|
|
446
|
+
|
|
447
|
+
Some OpenAI-compatible GLM endpoints put tool calls in ``content`` rather
|
|
448
|
+
than ``delta.tool_calls``. Real responses also occasionally encode an
|
|
449
|
+
argument as ``<arg_value>name=\"value\"</arg_value>`` (or close an
|
|
450
|
+
``arg_key`` with ``</arg_value>``), so parsing is intentionally tolerant
|
|
451
|
+
about those two wire quirks while remaining fail-closed on tool names.
|
|
452
|
+
"""
|
|
453
|
+
|
|
454
|
+
if "<tool_call" not in content.lower():
|
|
455
|
+
return content, [], False
|
|
456
|
+
declared = _declared_tool_names(tools)
|
|
457
|
+
calls: list[ToolCall] = []
|
|
458
|
+
spans: list[tuple[int, int]] = []
|
|
459
|
+
for index, match in enumerate(_GLM_TOOL_CALL_RE.finditer(content)):
|
|
460
|
+
requested_name = match.group("name").strip()
|
|
461
|
+
canonical_name = _canonical_declared_tool_name(requested_name, declared)
|
|
462
|
+
if not canonical_name:
|
|
463
|
+
raise StudioError(
|
|
464
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
465
|
+
f"模型返回了未声明的工具调用:{requested_name or 'unknown'}",
|
|
466
|
+
status_code=502,
|
|
467
|
+
details={"toolName": requested_name or "unknown"},
|
|
468
|
+
)
|
|
469
|
+
arguments: dict[str, Any] = {}
|
|
470
|
+
pending_key = ""
|
|
471
|
+
for token in _GLM_ARGUMENT_TAG_RE.finditer(match.group("body")):
|
|
472
|
+
tag = token.group("tag").lower()
|
|
473
|
+
raw_value = html.unescape(token.group("value").strip())
|
|
474
|
+
assignment = _GLM_ASSIGNMENT_RE.match(raw_value)
|
|
475
|
+
if assignment:
|
|
476
|
+
arguments[assignment.group("name")] = _dsml_argument_value(
|
|
477
|
+
assignment.group("value"), {"string": "true"}
|
|
478
|
+
)
|
|
479
|
+
pending_key = ""
|
|
480
|
+
elif tag == "arg_key":
|
|
481
|
+
pending_key = raw_value
|
|
482
|
+
elif pending_key:
|
|
483
|
+
arguments[pending_key] = _dsml_argument_value(raw_value, {})
|
|
484
|
+
pending_key = ""
|
|
485
|
+
else:
|
|
486
|
+
raise StudioError(
|
|
487
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
488
|
+
"模型返回了无法解析的工具参数",
|
|
489
|
+
status_code=502,
|
|
490
|
+
)
|
|
491
|
+
if pending_key or not arguments:
|
|
492
|
+
raise StudioError(
|
|
493
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
494
|
+
"模型返回了不完整的工具调用",
|
|
495
|
+
status_code=502,
|
|
496
|
+
)
|
|
497
|
+
digest = hashlib.sha256(f"{canonical_name}:{index}:{match.group(0)}".encode()).hexdigest()[
|
|
498
|
+
:20
|
|
499
|
+
]
|
|
500
|
+
calls.append(
|
|
501
|
+
ToolCall(
|
|
502
|
+
id=f"call_glm_{digest}",
|
|
503
|
+
name=canonical_name,
|
|
504
|
+
arguments=json.dumps(arguments, ensure_ascii=False),
|
|
505
|
+
raw={"protocol": "glm-text"},
|
|
506
|
+
)
|
|
507
|
+
)
|
|
508
|
+
spans.append(match.span())
|
|
509
|
+
if not calls:
|
|
510
|
+
raise StudioError(
|
|
511
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
512
|
+
"模型返回了无法解析的工具调用",
|
|
513
|
+
status_code=502,
|
|
514
|
+
)
|
|
515
|
+
cleaned = content
|
|
516
|
+
for start, end in reversed(spans):
|
|
517
|
+
cleaned = cleaned[:start] + cleaned[end:]
|
|
518
|
+
if "<tool_call" in cleaned.lower():
|
|
519
|
+
raise StudioError(
|
|
520
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
521
|
+
"模型返回了不完整的工具调用",
|
|
522
|
+
status_code=502,
|
|
523
|
+
)
|
|
524
|
+
return cleaned.strip(), calls, True
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def _recover_malformed_glm_tool_call(
|
|
528
|
+
content: str,
|
|
529
|
+
tools: list[dict[str, Any]] | None,
|
|
530
|
+
) -> tuple[str, list[ToolCall], bool]:
|
|
531
|
+
"""Recover the bounded malformed fallback emitted by GLM 5.2.
|
|
532
|
+
|
|
533
|
+
Some GLM 5.2 compatible gateways emit a textual ``<tool_call>`` marker
|
|
534
|
+
but lose the closing marker and mix up ``arg_key``/``arg_value`` tags.
|
|
535
|
+
Recovery remains fail-closed: the tool must resolve to a declared schema,
|
|
536
|
+
every recovered argument must resolve to a declared property, and at
|
|
537
|
+
least one argument must be recovered. Text after the marker is discarded
|
|
538
|
+
so a hallucinated "file created" sentence cannot escape as visible output
|
|
539
|
+
before the tool actually executes.
|
|
540
|
+
"""
|
|
541
|
+
|
|
542
|
+
start = _GLM_MALFORMED_TOOL_START_RE.search(content)
|
|
543
|
+
if start is None:
|
|
544
|
+
return content, [], False
|
|
545
|
+
declared_names = _declared_tool_names(tools)
|
|
546
|
+
requested_name = start.group("name").strip()
|
|
547
|
+
canonical_name = _canonical_declared_tool_name(requested_name, declared_names)
|
|
548
|
+
if not canonical_name:
|
|
549
|
+
raise StudioError(
|
|
550
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
551
|
+
f"模型返回了未声明的工具调用:{requested_name or 'unknown'}",
|
|
552
|
+
status_code=502,
|
|
553
|
+
details={"toolName": requested_name or "unknown"},
|
|
554
|
+
)
|
|
555
|
+
properties = _declared_tool_properties(tools).get(canonical_name, frozenset())
|
|
556
|
+
body = content[start.end() :]
|
|
557
|
+
raw_arguments: list[tuple[str, str]] = []
|
|
558
|
+
|
|
559
|
+
key_matches = list(_GLM_MALFORMED_KEY_RE.finditer(body))
|
|
560
|
+
for index, key_match in enumerate(key_matches):
|
|
561
|
+
next_start = key_matches[index + 1].start() if index + 1 < len(key_matches) else len(body)
|
|
562
|
+
value = body[key_match.end() : next_start]
|
|
563
|
+
value = re.sub(r"^\s*<\s*arg_value\s*>\s*", "", value, flags=re.IGNORECASE)
|
|
564
|
+
value = re.split(r"</\s*arg_value\s*>", value, maxsplit=1, flags=re.IGNORECASE)[0]
|
|
565
|
+
raw_arguments.append((key_match.group("name"), html.unescape(value.strip())))
|
|
566
|
+
|
|
567
|
+
if not raw_arguments:
|
|
568
|
+
raw_arguments.extend(
|
|
569
|
+
(match.group("name"), html.unescape(match.group("value")))
|
|
570
|
+
for match in _GLM_MALFORMED_ASSIGNMENT_RE.finditer(body)
|
|
571
|
+
)
|
|
572
|
+
|
|
573
|
+
arguments: dict[str, Any] = {}
|
|
574
|
+
for raw_name, raw_value in raw_arguments:
|
|
575
|
+
argument_name = _canonical_declared_argument_name(
|
|
576
|
+
raw_name,
|
|
577
|
+
canonical_name,
|
|
578
|
+
properties,
|
|
579
|
+
)
|
|
580
|
+
if not argument_name:
|
|
581
|
+
raise StudioError(
|
|
582
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
583
|
+
"模型返回了未声明的工具参数",
|
|
584
|
+
status_code=502,
|
|
585
|
+
details={"toolName": canonical_name, "argument": raw_name},
|
|
586
|
+
)
|
|
587
|
+
if argument_name in arguments:
|
|
588
|
+
raise StudioError(
|
|
589
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
590
|
+
"模型返回了重复的工具参数",
|
|
591
|
+
status_code=502,
|
|
592
|
+
details={"toolName": canonical_name, "argument": argument_name},
|
|
593
|
+
)
|
|
594
|
+
arguments[argument_name] = _dsml_argument_value(raw_value, {"string": "true"})
|
|
595
|
+
|
|
596
|
+
if not arguments:
|
|
597
|
+
raise StudioError(
|
|
598
|
+
"MODEL_TOOL_PROTOCOL_INVALID",
|
|
599
|
+
"模型返回了无法解析的工具调用",
|
|
600
|
+
status_code=502,
|
|
601
|
+
)
|
|
602
|
+
digest = hashlib.sha256(
|
|
603
|
+
f"{canonical_name}:malformed:{content[start.start() :]}".encode()
|
|
604
|
+
).hexdigest()[:20]
|
|
605
|
+
return (
|
|
606
|
+
content[: start.start()].strip(),
|
|
607
|
+
[
|
|
608
|
+
ToolCall(
|
|
609
|
+
id=f"call_glm_{digest}",
|
|
610
|
+
name=canonical_name,
|
|
611
|
+
arguments=json.dumps(arguments, ensure_ascii=False),
|
|
612
|
+
raw={"protocol": "glm-text-recovered"},
|
|
613
|
+
)
|
|
614
|
+
],
|
|
615
|
+
True,
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
def _recover_textual_tool_calls(
|
|
620
|
+
content: str,
|
|
621
|
+
tools: list[dict[str, Any]] | None,
|
|
622
|
+
) -> tuple[str, list[ToolCall], bool]:
|
|
623
|
+
if "DSML" in content.replace("|", "|").upper():
|
|
624
|
+
return _recover_dsml_tool_calls(content, tools)
|
|
625
|
+
try:
|
|
626
|
+
return _recover_glm_tool_calls(content, tools)
|
|
627
|
+
except StudioError as exc:
|
|
628
|
+
if exc.code != "MODEL_TOOL_PROTOCOL_INVALID":
|
|
629
|
+
raise
|
|
630
|
+
return _recover_malformed_glm_tool_call(content, tools)
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
def _partition_tool_stream_text(value: str) -> tuple[str, str, bool]:
|
|
634
|
+
"""Split a streamed text buffer before a possible textual tool marker.
|
|
635
|
+
|
|
636
|
+
Ordinary model prose must remain truly streaming even when tools are
|
|
637
|
+
available. Only the short suffix beginning at ``<`` is held long enough
|
|
638
|
+
to decide whether a provider is starting a textual DSML tool call.
|
|
639
|
+
"""
|
|
640
|
+
|
|
641
|
+
normalized = value.replace("|", "|")
|
|
642
|
+
marker = re.search(
|
|
643
|
+
r"<\s*(?:\|\s*){1,2}DSML|<\s*tool_call\s*>",
|
|
644
|
+
normalized,
|
|
645
|
+
re.IGNORECASE,
|
|
646
|
+
)
|
|
647
|
+
if marker:
|
|
648
|
+
start = marker.start()
|
|
649
|
+
return value[:start], value[start:], True
|
|
650
|
+
candidate_start = value.rfind("<")
|
|
651
|
+
if candidate_start >= 0 and len(value) - candidate_start <= _DSML_STREAM_LOOKBEHIND:
|
|
652
|
+
return value[:candidate_start], value[candidate_start:], False
|
|
653
|
+
return value, "", False
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
def _repeated_output_unit(value: str) -> str:
|
|
657
|
+
"""Return a repeated tail unit when a provider is stuck in a text loop."""
|
|
658
|
+
|
|
659
|
+
if len(value) < _REPETITION_MIN_UNIT_CHARS * _REPETITION_COUNT:
|
|
660
|
+
return ""
|
|
661
|
+
window = value[-(_REPETITION_MAX_UNIT_CHARS * _REPETITION_COUNT) :]
|
|
662
|
+
max_unit = min(_REPETITION_MAX_UNIT_CHARS, len(window) // _REPETITION_COUNT)
|
|
663
|
+
for unit_length in range(_REPETITION_MIN_UNIT_CHARS, max_unit + 1):
|
|
664
|
+
unit = window[-unit_length:]
|
|
665
|
+
if window.endswith(unit * _REPETITION_COUNT) and len(set(unit.strip())) >= 6:
|
|
666
|
+
return unit
|
|
667
|
+
return ""
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
def _raise_for_repetitive_output(value: str) -> None:
|
|
671
|
+
unit = _repeated_output_unit(value)
|
|
672
|
+
if not unit:
|
|
673
|
+
return
|
|
674
|
+
raise StudioError(
|
|
675
|
+
"MODEL_REPETITIVE_OUTPUT",
|
|
676
|
+
"模型输出出现重复循环,已停止本次生成",
|
|
677
|
+
status_code=502,
|
|
678
|
+
details={"repeatedChars": len(unit)},
|
|
679
|
+
)
|
|
48
680
|
|
|
49
681
|
|
|
50
682
|
class CredentialResolver:
|
|
51
|
-
"""Resolve
|
|
683
|
+
"""Resolve Secret references through the shared workspace configuration.
|
|
52
684
|
|
|
53
|
-
|
|
54
|
-
|
|
685
|
+
Workspace credentials use ``.agentkit/config.yaml`` with explicit imports
|
|
686
|
+
and legacy migration. Standalone instances retain process/session behavior.
|
|
55
687
|
"""
|
|
56
688
|
|
|
57
689
|
_FALLBACK_PAIRS: tuple[tuple[str, str], ...] = (
|
|
@@ -59,52 +691,19 @@ class CredentialResolver:
|
|
|
59
691
|
("OPENAI_API_KEY", "AGENTKIT_MODEL_API_KEY"),
|
|
60
692
|
)
|
|
61
693
|
|
|
62
|
-
def __init__(self, workspace: Any = None) -> None:
|
|
694
|
+
def __init__(self, workspace: Any = None, *, configuration: Any = None) -> None:
|
|
63
695
|
self._session_values: dict[str, bytearray] = {}
|
|
64
696
|
self._lock = RLock()
|
|
65
697
|
self._workspace = workspace
|
|
66
|
-
|
|
698
|
+
from ksadk.studio.configuration import WorkspaceConfiguration
|
|
67
699
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return self._workspace.resolve(".agentkit/secrets.env")
|
|
73
|
-
except Exception:
|
|
74
|
-
return None
|
|
700
|
+
self.configuration = configuration or (
|
|
701
|
+
WorkspaceConfiguration(workspace) if workspace is not None else None
|
|
702
|
+
)
|
|
703
|
+
self._persisted: dict[str, str] | None = None
|
|
75
704
|
|
|
76
705
|
def _load_persisted(self) -> dict[str, str]:
|
|
77
|
-
if self.
|
|
78
|
-
return self._persisted
|
|
79
|
-
self._persisted = {}
|
|
80
|
-
path = self._secrets_path()
|
|
81
|
-
if path is not None and path.is_file():
|
|
82
|
-
try:
|
|
83
|
-
for line in path.read_text(encoding="utf-8").splitlines():
|
|
84
|
-
if not line or line.startswith("#") or "=" not in line:
|
|
85
|
-
continue
|
|
86
|
-
key, _, value = line.partition("=")
|
|
87
|
-
key = key.strip()
|
|
88
|
-
if key:
|
|
89
|
-
self._persisted[key] = value
|
|
90
|
-
except Exception:
|
|
91
|
-
pass
|
|
92
|
-
return self._persisted
|
|
93
|
-
|
|
94
|
-
def _write_persisted(self) -> None:
|
|
95
|
-
path = self._secrets_path()
|
|
96
|
-
if path is None:
|
|
97
|
-
return
|
|
98
|
-
lines = [f"{k}={v}" for k, v in sorted(self._persisted.items())]
|
|
99
|
-
try:
|
|
100
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
101
|
-
path.write_text("\n".join(lines) + ("\n" if lines else ""), encoding="utf-8")
|
|
102
|
-
try:
|
|
103
|
-
path.chmod(0o600)
|
|
104
|
-
except Exception:
|
|
105
|
-
pass
|
|
106
|
-
except Exception:
|
|
107
|
-
pass
|
|
706
|
+
return self.configuration.secrets() if self.configuration is not None else {}
|
|
108
707
|
|
|
109
708
|
@staticmethod
|
|
110
709
|
def _validate_name(name: str) -> str:
|
|
@@ -153,11 +752,8 @@ class CredentialResolver:
|
|
|
153
752
|
previous = self._session_values.get(name)
|
|
154
753
|
self._session_values[name] = encoded
|
|
155
754
|
self._zero(previous)
|
|
156
|
-
if self.
|
|
157
|
-
|
|
158
|
-
if persisted.get(name) != value:
|
|
159
|
-
persisted[name] = value
|
|
160
|
-
self._write_persisted()
|
|
755
|
+
if self.configuration is not None:
|
|
756
|
+
self.configuration.put_secret(name, value)
|
|
161
757
|
return self.status(f"env://{name}")
|
|
162
758
|
|
|
163
759
|
def delete_session(self, name: str) -> dict[str, str | bool]:
|
|
@@ -165,11 +761,8 @@ class CredentialResolver:
|
|
|
165
761
|
with self._lock:
|
|
166
762
|
previous = self._session_values.pop(name, None)
|
|
167
763
|
self._zero(previous)
|
|
168
|
-
if self.
|
|
169
|
-
|
|
170
|
-
if name in persisted:
|
|
171
|
-
persisted.pop(name, None)
|
|
172
|
-
self._write_persisted()
|
|
764
|
+
if self.configuration is not None:
|
|
765
|
+
self.configuration.put_secret(name, None)
|
|
173
766
|
return self.status(f"env://{name}")
|
|
174
767
|
|
|
175
768
|
def clear_session(self) -> None:
|
|
@@ -188,6 +781,19 @@ class CredentialResolver:
|
|
|
188
781
|
def _resolve_source(self, name: str) -> tuple[bool, str]:
|
|
189
782
|
"""Return (configured, source) considering session, persisted file, env, and fallback."""
|
|
190
783
|
fallback = self._fallback_name(name)
|
|
784
|
+
if self.configuration is not None:
|
|
785
|
+
value, source = self.configuration.resolve_candidates(
|
|
786
|
+
[name, *([fallback] if fallback else [])]
|
|
787
|
+
)
|
|
788
|
+
with self._lock:
|
|
789
|
+
# put_session() keeps the session overlay and the persisted copy in
|
|
790
|
+
# sync; if the persisted copy vanished (deleted via another resolver
|
|
791
|
+
# instance) the session entry is stale and must not shadow it.
|
|
792
|
+
if value and name in self._session_values:
|
|
793
|
+
return True, "session"
|
|
794
|
+
if value and fallback and fallback in self._session_values:
|
|
795
|
+
return True, "session-alias"
|
|
796
|
+
return bool(value), source
|
|
191
797
|
with self._lock:
|
|
192
798
|
if name in self._session_values:
|
|
193
799
|
return True, "session"
|
|
@@ -220,10 +826,20 @@ class CredentialResolver:
|
|
|
220
826
|
"persistence": source,
|
|
221
827
|
}
|
|
222
828
|
|
|
223
|
-
def resolve(self, reference: str) -> str:
|
|
829
|
+
def resolve(self, reference: str, *, allow_aliases: bool = True) -> str:
|
|
224
830
|
name = self._environment_name(reference)
|
|
225
|
-
fallback = self._fallback_name(name)
|
|
831
|
+
fallback = self._fallback_name(name) if allow_aliases else None
|
|
226
832
|
aliases = [name, *([fallback] if fallback else [])]
|
|
833
|
+
if self.configuration is not None:
|
|
834
|
+
value, _source = self.configuration.resolve_candidates(aliases)
|
|
835
|
+
if value:
|
|
836
|
+
return value
|
|
837
|
+
raise StudioError(
|
|
838
|
+
"SECRET_NOT_FOUND",
|
|
839
|
+
"凭证尚未配置,请在设置中填写",
|
|
840
|
+
status_code=422,
|
|
841
|
+
details={"reference": reference},
|
|
842
|
+
)
|
|
227
843
|
for candidate in aliases:
|
|
228
844
|
value = self._session_value(candidate)
|
|
229
845
|
if value is not None:
|
|
@@ -319,6 +935,34 @@ class OpenAICompatibleModelClient:
|
|
|
319
935
|
self.network_guard = network_guard or NetworkGuard()
|
|
320
936
|
self.transport = transport
|
|
321
937
|
self.sleep = sleep
|
|
938
|
+
# Keep one AsyncClient for the lifetime of the Studio service. Creating
|
|
939
|
+
# a client for every turn defeats HTTP keep-alive and forces a fresh
|
|
940
|
+
# TCP/TLS handshake before the model can produce its first token.
|
|
941
|
+
self._client: httpx.AsyncClient | None = None
|
|
942
|
+
self._client_lock = RLock()
|
|
943
|
+
|
|
944
|
+
def _http_client(self) -> httpx.AsyncClient:
|
|
945
|
+
with self._client_lock:
|
|
946
|
+
if self._client is None or self._client.is_closed:
|
|
947
|
+
self._client = httpx.AsyncClient(
|
|
948
|
+
transport=self.transport,
|
|
949
|
+
follow_redirects=False,
|
|
950
|
+
limits=httpx.Limits(
|
|
951
|
+
max_connections=20,
|
|
952
|
+
max_keepalive_connections=10,
|
|
953
|
+
keepalive_expiry=30.0,
|
|
954
|
+
),
|
|
955
|
+
)
|
|
956
|
+
return self._client
|
|
957
|
+
|
|
958
|
+
async def aclose(self) -> None:
|
|
959
|
+
"""Close the shared connection pool during Studio shutdown."""
|
|
960
|
+
|
|
961
|
+
with self._client_lock:
|
|
962
|
+
client = self._client
|
|
963
|
+
self._client = None
|
|
964
|
+
if client is not None and not client.is_closed:
|
|
965
|
+
await client.aclose()
|
|
322
966
|
|
|
323
967
|
async def complete(
|
|
324
968
|
self,
|
|
@@ -333,6 +977,7 @@ class OpenAICompatibleModelClient:
|
|
|
333
977
|
allow_empty: bool = False,
|
|
334
978
|
response_format: dict[str, Any] | None = None,
|
|
335
979
|
retry_on_length: bool = False,
|
|
980
|
+
max_output_tokens: int | None = None,
|
|
336
981
|
) -> ModelResponse:
|
|
337
982
|
await self.network_guard.check(model.endpoint_url, network_policy)
|
|
338
983
|
credential = self.credential_resolver.resolve(model.credential_ref)
|
|
@@ -359,6 +1004,10 @@ class OpenAICompatibleModelClient:
|
|
|
359
1004
|
payload["tool_choice"] = "auto"
|
|
360
1005
|
if response_format and model.parameters.allow_json_response_format:
|
|
361
1006
|
payload["response_format"] = response_format
|
|
1007
|
+
if max_output_tokens is not None:
|
|
1008
|
+
payload["max_output_tokens" if wire_api == "responses" else "max_tokens"] = (
|
|
1009
|
+
max_output_tokens
|
|
1010
|
+
)
|
|
362
1011
|
headers = {
|
|
363
1012
|
"Authorization": f"Bearer {credential}",
|
|
364
1013
|
"Content-Type": "application/json",
|
|
@@ -367,11 +1016,8 @@ class OpenAICompatibleModelClient:
|
|
|
367
1016
|
dropped_response_format = False
|
|
368
1017
|
length_retried = False
|
|
369
1018
|
last_length_error: StudioError | None = None
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
timeout=timeout,
|
|
373
|
-
follow_redirects=False,
|
|
374
|
-
) as client:
|
|
1019
|
+
client = self._http_client()
|
|
1020
|
+
try:
|
|
375
1021
|
# 额外 1 次迭代仅用于 response_format 400 降级重发,
|
|
376
1022
|
# 其余失败路径仍受 max_attempts 约束(会在原上限处 raise)。
|
|
377
1023
|
for attempt in range(1, max_attempts + 2):
|
|
@@ -380,6 +1026,7 @@ class OpenAICompatibleModelClient:
|
|
|
380
1026
|
model.endpoint_url,
|
|
381
1027
|
headers=headers,
|
|
382
1028
|
json=payload,
|
|
1029
|
+
timeout=timeout,
|
|
383
1030
|
)
|
|
384
1031
|
except (httpx.TimeoutException, httpx.TransportError) as exc:
|
|
385
1032
|
if attempt >= max_attempts:
|
|
@@ -461,11 +1108,470 @@ class OpenAICompatibleModelClient:
|
|
|
461
1108
|
length_retried = True
|
|
462
1109
|
payload = self._expand_length_budget(payload)
|
|
463
1110
|
continue
|
|
1111
|
+
if not (
|
|
1112
|
+
"DSML" in parsed.content.replace("|", "|").upper()
|
|
1113
|
+
or "<tool_call" in parsed.content.lower()
|
|
1114
|
+
):
|
|
1115
|
+
_raise_for_repetitive_output(parsed.content)
|
|
1116
|
+
if wire_api != "responses" and not parsed.tool_calls:
|
|
1117
|
+
cleaned, recovered_calls, recovered = _recover_textual_tool_calls(
|
|
1118
|
+
parsed.content, tools
|
|
1119
|
+
)
|
|
1120
|
+
if recovered:
|
|
1121
|
+
parsed = ModelResponse(
|
|
1122
|
+
content=cleaned,
|
|
1123
|
+
finish_reason="tool_calls",
|
|
1124
|
+
usage=parsed.usage,
|
|
1125
|
+
tool_calls=recovered_calls,
|
|
1126
|
+
raw_message=parsed.raw_message,
|
|
1127
|
+
reasoning=parsed.reasoning,
|
|
1128
|
+
)
|
|
1129
|
+
if parsed.tool_calls:
|
|
1130
|
+
parsed = ModelResponse(
|
|
1131
|
+
content=parsed.content,
|
|
1132
|
+
finish_reason=parsed.finish_reason,
|
|
1133
|
+
usage=parsed.usage,
|
|
1134
|
+
tool_calls=list(_normalize_tool_call_arguments(parsed.tool_calls, tools)),
|
|
1135
|
+
raw_message=parsed.raw_message,
|
|
1136
|
+
reasoning=parsed.reasoning,
|
|
1137
|
+
)
|
|
464
1138
|
return parsed
|
|
1139
|
+
except asyncio.CancelledError:
|
|
1140
|
+
raise
|
|
465
1141
|
if last_length_error is not None:
|
|
466
1142
|
raise last_length_error
|
|
467
1143
|
raise AssertionError("unreachable")
|
|
468
1144
|
|
|
1145
|
+
async def stream(
|
|
1146
|
+
self,
|
|
1147
|
+
model: ResolvedModel,
|
|
1148
|
+
*,
|
|
1149
|
+
messages: list[dict[str, Any]],
|
|
1150
|
+
network_policy: NetworkPolicy,
|
|
1151
|
+
timeout_seconds: int,
|
|
1152
|
+
tools: list[dict[str, Any]] | None = None,
|
|
1153
|
+
max_output_tokens: int | None = None,
|
|
1154
|
+
) -> AsyncIterator[ModelStreamChunk]:
|
|
1155
|
+
"""Stream chat-completions deltas without buffering the response.
|
|
1156
|
+
|
|
1157
|
+
Studio's Harness path uses the same credential and network policy as
|
|
1158
|
+
``complete``. Responses-wire profiles currently remain on the
|
|
1159
|
+
buffered path because their event vocabulary is provider-specific.
|
|
1160
|
+
"""
|
|
1161
|
+
if (model.wire_api or "chat").strip().lower() == "responses":
|
|
1162
|
+
async for chunk in self._stream_responses(
|
|
1163
|
+
model,
|
|
1164
|
+
messages=messages,
|
|
1165
|
+
network_policy=network_policy,
|
|
1166
|
+
timeout_seconds=timeout_seconds,
|
|
1167
|
+
tools=tools,
|
|
1168
|
+
max_output_tokens=max_output_tokens,
|
|
1169
|
+
):
|
|
1170
|
+
yield chunk
|
|
1171
|
+
return
|
|
1172
|
+
await self.network_guard.check(model.endpoint_url, network_policy)
|
|
1173
|
+
credential = self.credential_resolver.resolve(model.credential_ref)
|
|
1174
|
+
payload: dict[str, Any] = {
|
|
1175
|
+
"model": model.model,
|
|
1176
|
+
"messages": messages,
|
|
1177
|
+
"stream": True,
|
|
1178
|
+
}
|
|
1179
|
+
if model.parameters.temperature is not None:
|
|
1180
|
+
payload["temperature"] = model.parameters.temperature
|
|
1181
|
+
if model.parameters.max_tokens is not None:
|
|
1182
|
+
payload["max_tokens"] = model.parameters.max_tokens
|
|
1183
|
+
if max_output_tokens is not None:
|
|
1184
|
+
payload["max_tokens"] = max_output_tokens
|
|
1185
|
+
if model.parameters.top_p is not None:
|
|
1186
|
+
payload["top_p"] = model.parameters.top_p
|
|
1187
|
+
if tools:
|
|
1188
|
+
payload["tools"] = tools
|
|
1189
|
+
payload["tool_choice"] = "auto"
|
|
1190
|
+
headers = {
|
|
1191
|
+
"Authorization": f"Bearer {credential}",
|
|
1192
|
+
"Content-Type": "application/json",
|
|
1193
|
+
"Accept": "text/event-stream",
|
|
1194
|
+
}
|
|
1195
|
+
timeout = httpx.Timeout(timeout_seconds, connect=min(10, timeout_seconds))
|
|
1196
|
+
client = self._http_client()
|
|
1197
|
+
request_started = time.monotonic()
|
|
1198
|
+
try:
|
|
1199
|
+
async with client.stream(
|
|
1200
|
+
"POST", model.endpoint_url, headers=headers, json=payload, timeout=timeout
|
|
1201
|
+
) as response:
|
|
1202
|
+
if response.is_redirect or response.status_code >= 400:
|
|
1203
|
+
raise StudioError(
|
|
1204
|
+
"MODEL_REQUEST_FAILED",
|
|
1205
|
+
"模型流式请求失败",
|
|
1206
|
+
status_code=502,
|
|
1207
|
+
details={"upstreamStatus": response.status_code},
|
|
1208
|
+
)
|
|
1209
|
+
tool_fragments: dict[int, dict[str, str]] = {}
|
|
1210
|
+
first_chunk_logged = False
|
|
1211
|
+
raw_text = ""
|
|
1212
|
+
emitted_text = ""
|
|
1213
|
+
pending_visible_text = ""
|
|
1214
|
+
textual_tool_detected = False
|
|
1215
|
+
done_emitted = False
|
|
1216
|
+
last_repetition_check_length = 0
|
|
1217
|
+
async for line in response.aiter_lines():
|
|
1218
|
+
if not line.startswith("data:"):
|
|
1219
|
+
continue
|
|
1220
|
+
raw = line[5:].strip()
|
|
1221
|
+
if raw == "[DONE]":
|
|
1222
|
+
break
|
|
1223
|
+
if not raw:
|
|
1224
|
+
continue
|
|
1225
|
+
try:
|
|
1226
|
+
chunk = json.loads(raw)
|
|
1227
|
+
except (TypeError, ValueError):
|
|
1228
|
+
continue
|
|
1229
|
+
usage = chunk.get("usage") or {}
|
|
1230
|
+
usage_value = (
|
|
1231
|
+
Usage(
|
|
1232
|
+
input_tokens=int(usage.get("prompt_tokens") or 0),
|
|
1233
|
+
output_tokens=int(usage.get("completion_tokens") or 0),
|
|
1234
|
+
total_tokens=int(usage.get("total_tokens") or 0),
|
|
1235
|
+
cached_input_tokens=int(
|
|
1236
|
+
(usage.get("prompt_tokens_details") or {}).get("cached_tokens") or 0
|
|
1237
|
+
),
|
|
1238
|
+
reasoning_output_tokens=int(
|
|
1239
|
+
(usage.get("completion_tokens_details") or {}).get(
|
|
1240
|
+
"reasoning_tokens"
|
|
1241
|
+
)
|
|
1242
|
+
or 0
|
|
1243
|
+
),
|
|
1244
|
+
reported=True,
|
|
1245
|
+
source="model-provider",
|
|
1246
|
+
)
|
|
1247
|
+
if usage
|
|
1248
|
+
else None
|
|
1249
|
+
)
|
|
1250
|
+
_accumulate_chat_tool_calls(
|
|
1251
|
+
tool_fragments,
|
|
1252
|
+
chunk.get("tool_calls"),
|
|
1253
|
+
snapshot=True,
|
|
1254
|
+
)
|
|
1255
|
+
root_message = chunk.get("message") or {}
|
|
1256
|
+
if isinstance(root_message, dict):
|
|
1257
|
+
_accumulate_chat_tool_calls(
|
|
1258
|
+
tool_fragments,
|
|
1259
|
+
root_message.get("tool_calls"),
|
|
1260
|
+
snapshot=True,
|
|
1261
|
+
)
|
|
1262
|
+
if isinstance(root_message.get("function_call"), dict):
|
|
1263
|
+
_accumulate_chat_tool_calls(
|
|
1264
|
+
tool_fragments,
|
|
1265
|
+
[{"index": 0, "function": root_message["function_call"]}],
|
|
1266
|
+
snapshot=True,
|
|
1267
|
+
)
|
|
1268
|
+
for choice in chunk.get("choices") or []:
|
|
1269
|
+
delta = choice.get("delta") or {}
|
|
1270
|
+
text = str(delta.get("content") or "")
|
|
1271
|
+
raw_text += text
|
|
1272
|
+
pending_visible_text += text
|
|
1273
|
+
reasoning = str(
|
|
1274
|
+
delta.get("reasoning_content") or delta.get("reasoning") or ""
|
|
1275
|
+
)
|
|
1276
|
+
_accumulate_chat_tool_calls(
|
|
1277
|
+
tool_fragments,
|
|
1278
|
+
delta.get("tool_calls"),
|
|
1279
|
+
snapshot=False,
|
|
1280
|
+
)
|
|
1281
|
+
if isinstance(delta.get("function_call"), dict):
|
|
1282
|
+
_accumulate_chat_tool_calls(
|
|
1283
|
+
tool_fragments,
|
|
1284
|
+
[{"index": 0, "function": delta["function_call"]}],
|
|
1285
|
+
snapshot=False,
|
|
1286
|
+
)
|
|
1287
|
+
message = choice.get("message") or {}
|
|
1288
|
+
if isinstance(message, dict):
|
|
1289
|
+
_accumulate_chat_tool_calls(
|
|
1290
|
+
tool_fragments,
|
|
1291
|
+
message.get("tool_calls"),
|
|
1292
|
+
snapshot=True,
|
|
1293
|
+
)
|
|
1294
|
+
if isinstance(message.get("function_call"), dict):
|
|
1295
|
+
_accumulate_chat_tool_calls(
|
|
1296
|
+
tool_fragments,
|
|
1297
|
+
[{"index": 0, "function": message["function_call"]}],
|
|
1298
|
+
snapshot=True,
|
|
1299
|
+
)
|
|
1300
|
+
_accumulate_chat_tool_calls(
|
|
1301
|
+
tool_fragments,
|
|
1302
|
+
choice.get("tool_calls"),
|
|
1303
|
+
snapshot=True,
|
|
1304
|
+
)
|
|
1305
|
+
if isinstance(choice.get("function_call"), dict):
|
|
1306
|
+
_accumulate_chat_tool_calls(
|
|
1307
|
+
tool_fragments,
|
|
1308
|
+
[{"index": 0, "function": choice["function_call"]}],
|
|
1309
|
+
snapshot=True,
|
|
1310
|
+
)
|
|
1311
|
+
visible_text = ""
|
|
1312
|
+
if text and not textual_tool_detected:
|
|
1313
|
+
visible_text, pending_visible_text, marker_found = (
|
|
1314
|
+
_partition_tool_stream_text(pending_visible_text)
|
|
1315
|
+
)
|
|
1316
|
+
textual_tool_detected = marker_found
|
|
1317
|
+
emitted_text += visible_text
|
|
1318
|
+
visible_length = len(emitted_text) + len(pending_visible_text)
|
|
1319
|
+
if (
|
|
1320
|
+
not textual_tool_detected
|
|
1321
|
+
and visible_length - last_repetition_check_length >= 64
|
|
1322
|
+
):
|
|
1323
|
+
last_repetition_check_length = visible_length
|
|
1324
|
+
_raise_for_repetitive_output(emitted_text + pending_visible_text)
|
|
1325
|
+
if visible_text or reasoning or usage_value is not None:
|
|
1326
|
+
if not first_chunk_logged and (text or reasoning):
|
|
1327
|
+
first_chunk_logged = True
|
|
1328
|
+
_LOGGER.info(
|
|
1329
|
+
"model stream first delta: model=%s ttfb_ms=%d",
|
|
1330
|
+
model.model,
|
|
1331
|
+
int((time.monotonic() - request_started) * 1000),
|
|
1332
|
+
)
|
|
1333
|
+
yield ModelStreamChunk(
|
|
1334
|
+
text=visible_text,
|
|
1335
|
+
reasoning=reasoning,
|
|
1336
|
+
usage=usage_value,
|
|
1337
|
+
)
|
|
1338
|
+
finish = any(
|
|
1339
|
+
str(choice.get("finish_reason") or "")
|
|
1340
|
+
for choice in chunk.get("choices") or []
|
|
1341
|
+
)
|
|
1342
|
+
incomplete_native_call = any(
|
|
1343
|
+
value["name"] and not value["arguments"]
|
|
1344
|
+
for value in tool_fragments.values()
|
|
1345
|
+
)
|
|
1346
|
+
if finish and not done_emitted and not incomplete_native_call:
|
|
1347
|
+
calls = tuple(
|
|
1348
|
+
ToolCall(
|
|
1349
|
+
id=value["id"],
|
|
1350
|
+
name=value["name"],
|
|
1351
|
+
arguments=value["arguments"] or "{}",
|
|
1352
|
+
)
|
|
1353
|
+
for value in tool_fragments.values()
|
|
1354
|
+
if value["name"]
|
|
1355
|
+
)
|
|
1356
|
+
final_text = raw_text
|
|
1357
|
+
if not calls and textual_tool_detected:
|
|
1358
|
+
final_text, recovered_calls, _ = _recover_textual_tool_calls(
|
|
1359
|
+
raw_text, tools
|
|
1360
|
+
)
|
|
1361
|
+
calls = tuple(recovered_calls)
|
|
1362
|
+
calls = _normalize_tool_call_arguments(calls, tools)
|
|
1363
|
+
if not final_text.strip() and not calls:
|
|
1364
|
+
raise StudioError(
|
|
1365
|
+
"MODEL_EMPTY_RESPONSE",
|
|
1366
|
+
"模型未返回可用内容",
|
|
1367
|
+
status_code=502,
|
|
1368
|
+
details={"finishReason": "stream-completed"},
|
|
1369
|
+
)
|
|
1370
|
+
if not calls and not textual_tool_detected:
|
|
1371
|
+
_raise_for_repetitive_output(final_text)
|
|
1372
|
+
remaining_text = final_text
|
|
1373
|
+
if emitted_text and final_text.startswith(emitted_text):
|
|
1374
|
+
remaining_text = final_text[len(emitted_text) :]
|
|
1375
|
+
if remaining_text:
|
|
1376
|
+
yield ModelStreamChunk(text=remaining_text)
|
|
1377
|
+
yield ModelStreamChunk(tool_calls=calls, done=True)
|
|
1378
|
+
done_emitted = True
|
|
1379
|
+
if not done_emitted:
|
|
1380
|
+
calls = tuple(
|
|
1381
|
+
ToolCall(
|
|
1382
|
+
id=value["id"],
|
|
1383
|
+
name=value["name"],
|
|
1384
|
+
arguments=value["arguments"] or "{}",
|
|
1385
|
+
)
|
|
1386
|
+
for value in tool_fragments.values()
|
|
1387
|
+
if value["name"]
|
|
1388
|
+
)
|
|
1389
|
+
final_text = raw_text
|
|
1390
|
+
if not calls and (
|
|
1391
|
+
textual_tool_detected
|
|
1392
|
+
or "DSML" in raw_text.replace("|", "|").upper()
|
|
1393
|
+
or "<tool_call" in raw_text.lower()
|
|
1394
|
+
):
|
|
1395
|
+
final_text, recovered_calls, _ = _recover_textual_tool_calls(
|
|
1396
|
+
raw_text, tools
|
|
1397
|
+
)
|
|
1398
|
+
calls = tuple(recovered_calls)
|
|
1399
|
+
calls = _normalize_tool_call_arguments(calls, tools)
|
|
1400
|
+
if not final_text.strip() and not calls:
|
|
1401
|
+
raise StudioError(
|
|
1402
|
+
"MODEL_EMPTY_RESPONSE",
|
|
1403
|
+
"模型未返回可用内容",
|
|
1404
|
+
status_code=502,
|
|
1405
|
+
details={"finishReason": "stream-ended"},
|
|
1406
|
+
)
|
|
1407
|
+
if not calls and not textual_tool_detected:
|
|
1408
|
+
_raise_for_repetitive_output(final_text)
|
|
1409
|
+
remaining_text = final_text
|
|
1410
|
+
if emitted_text and final_text.startswith(emitted_text):
|
|
1411
|
+
remaining_text = final_text[len(emitted_text) :]
|
|
1412
|
+
if remaining_text:
|
|
1413
|
+
yield ModelStreamChunk(text=remaining_text)
|
|
1414
|
+
yield ModelStreamChunk(tool_calls=calls, done=True)
|
|
1415
|
+
except asyncio.CancelledError:
|
|
1416
|
+
raise
|
|
1417
|
+
|
|
1418
|
+
async def _stream_responses(
|
|
1419
|
+
self,
|
|
1420
|
+
model: ResolvedModel,
|
|
1421
|
+
*,
|
|
1422
|
+
messages: list[dict[str, Any]],
|
|
1423
|
+
network_policy: NetworkPolicy,
|
|
1424
|
+
timeout_seconds: int,
|
|
1425
|
+
tools: list[dict[str, Any]] | None = None,
|
|
1426
|
+
max_output_tokens: int | None = None,
|
|
1427
|
+
) -> AsyncIterator[ModelStreamChunk]:
|
|
1428
|
+
"""Adapt the native Responses SSE event stream to Harness chunks."""
|
|
1429
|
+
|
|
1430
|
+
await self.network_guard.check(model.endpoint_url, network_policy)
|
|
1431
|
+
credential = self.credential_resolver.resolve(model.credential_ref)
|
|
1432
|
+
payload = self._responses_payload(model, messages, tools)
|
|
1433
|
+
payload["stream"] = True
|
|
1434
|
+
if max_output_tokens is not None:
|
|
1435
|
+
payload["max_output_tokens"] = max_output_tokens
|
|
1436
|
+
headers = {
|
|
1437
|
+
"Authorization": f"Bearer {credential}",
|
|
1438
|
+
"Content-Type": "application/json",
|
|
1439
|
+
"Accept": "text/event-stream",
|
|
1440
|
+
}
|
|
1441
|
+
timeout = httpx.Timeout(timeout_seconds, connect=min(10, timeout_seconds))
|
|
1442
|
+
client = self._http_client()
|
|
1443
|
+
request_started = time.monotonic()
|
|
1444
|
+
first_delta_logged = False
|
|
1445
|
+
function_calls: dict[str, dict[str, str]] = {}
|
|
1446
|
+
emitted_calls: set[str] = set()
|
|
1447
|
+
async with client.stream(
|
|
1448
|
+
"POST", model.endpoint_url, headers=headers, json=payload, timeout=timeout
|
|
1449
|
+
) as response:
|
|
1450
|
+
if response.is_redirect or response.status_code >= 400:
|
|
1451
|
+
raise StudioError(
|
|
1452
|
+
"MODEL_REQUEST_FAILED",
|
|
1453
|
+
"模型 Responses 流式请求失败",
|
|
1454
|
+
status_code=502,
|
|
1455
|
+
details={"upstreamStatus": response.status_code},
|
|
1456
|
+
)
|
|
1457
|
+
async for line in response.aiter_lines():
|
|
1458
|
+
if not line.startswith("data:"):
|
|
1459
|
+
continue
|
|
1460
|
+
raw = line[5:].strip()
|
|
1461
|
+
if not raw or raw == "[DONE]":
|
|
1462
|
+
continue
|
|
1463
|
+
try:
|
|
1464
|
+
event = json.loads(raw)
|
|
1465
|
+
except (TypeError, ValueError):
|
|
1466
|
+
continue
|
|
1467
|
+
event_type = str(event.get("type") or "")
|
|
1468
|
+
text = ""
|
|
1469
|
+
reasoning = ""
|
|
1470
|
+
usage_value: Usage | None = None
|
|
1471
|
+
if event_type == "response.output_text.delta":
|
|
1472
|
+
text = str(event.get("delta") or "")
|
|
1473
|
+
elif event_type in {
|
|
1474
|
+
"response.reasoning_summary_text.delta",
|
|
1475
|
+
"response.reasoning_text.delta",
|
|
1476
|
+
}:
|
|
1477
|
+
reasoning = str(event.get("delta") or "")
|
|
1478
|
+
elif event_type == "response.function_call_arguments.delta":
|
|
1479
|
+
key = str(event.get("item_id") or event.get("output_index") or "0")
|
|
1480
|
+
current = function_calls.setdefault(
|
|
1481
|
+
key, {"id": "", "name": "", "arguments": ""}
|
|
1482
|
+
)
|
|
1483
|
+
current["arguments"] += str(event.get("delta") or "")
|
|
1484
|
+
elif event_type == "response.output_item.added":
|
|
1485
|
+
item = event.get("item") or {}
|
|
1486
|
+
if item.get("type") == "function_call":
|
|
1487
|
+
key = str(
|
|
1488
|
+
item.get("id")
|
|
1489
|
+
or event.get("item_id")
|
|
1490
|
+
or event.get("output_index")
|
|
1491
|
+
or "0"
|
|
1492
|
+
)
|
|
1493
|
+
current = function_calls.setdefault(
|
|
1494
|
+
key, {"id": "", "name": "", "arguments": ""}
|
|
1495
|
+
)
|
|
1496
|
+
current["id"] = str(item.get("call_id") or item.get("id") or "")
|
|
1497
|
+
current["name"] = str(item.get("name") or "")
|
|
1498
|
+
current["arguments"] = str(item.get("arguments") or "")
|
|
1499
|
+
elif event_type == "response.output_item.done":
|
|
1500
|
+
item = event.get("item") or {}
|
|
1501
|
+
if item.get("type") == "function_call":
|
|
1502
|
+
key = str(
|
|
1503
|
+
item.get("id")
|
|
1504
|
+
or event.get("item_id")
|
|
1505
|
+
or event.get("output_index")
|
|
1506
|
+
or "0"
|
|
1507
|
+
)
|
|
1508
|
+
current = function_calls.setdefault(
|
|
1509
|
+
key, {"id": "", "name": "", "arguments": ""}
|
|
1510
|
+
)
|
|
1511
|
+
current["id"] = str(item.get("call_id") or item.get("id") or current["id"])
|
|
1512
|
+
current["name"] = str(item.get("name") or current["name"])
|
|
1513
|
+
current["arguments"] = str(
|
|
1514
|
+
item.get("arguments") or current["arguments"] or "{}"
|
|
1515
|
+
)
|
|
1516
|
+
elif event_type in {"response.completed", "response.incomplete"}:
|
|
1517
|
+
response_payload = event.get("response") or {}
|
|
1518
|
+
raw_usage = response_payload.get("usage") or {}
|
|
1519
|
+
if raw_usage:
|
|
1520
|
+
usage_value = Usage(
|
|
1521
|
+
input_tokens=int(raw_usage.get("input_tokens") or 0),
|
|
1522
|
+
output_tokens=int(raw_usage.get("output_tokens") or 0),
|
|
1523
|
+
total_tokens=int(raw_usage.get("total_tokens") or 0),
|
|
1524
|
+
cached_input_tokens=int(
|
|
1525
|
+
(raw_usage.get("input_tokens_details") or {}).get("cached_tokens")
|
|
1526
|
+
or 0
|
|
1527
|
+
),
|
|
1528
|
+
reasoning_output_tokens=int(
|
|
1529
|
+
(raw_usage.get("output_tokens_details") or {}).get(
|
|
1530
|
+
"reasoning_tokens"
|
|
1531
|
+
)
|
|
1532
|
+
or 0
|
|
1533
|
+
),
|
|
1534
|
+
reported=True,
|
|
1535
|
+
source="model-provider",
|
|
1536
|
+
)
|
|
1537
|
+
elif event_type in {"response.failed", "response.cancelled", "response.canceled"}:
|
|
1538
|
+
raise StudioError(
|
|
1539
|
+
"MODEL_REQUEST_FAILED",
|
|
1540
|
+
"模型 Responses 流式响应未完成",
|
|
1541
|
+
status_code=502,
|
|
1542
|
+
details={"eventType": event_type},
|
|
1543
|
+
)
|
|
1544
|
+
if text or reasoning:
|
|
1545
|
+
if not first_delta_logged:
|
|
1546
|
+
first_delta_logged = True
|
|
1547
|
+
_LOGGER.info(
|
|
1548
|
+
"model Responses stream first delta: model=%s ttfb_ms=%d",
|
|
1549
|
+
model.model,
|
|
1550
|
+
int((time.monotonic() - request_started) * 1000),
|
|
1551
|
+
)
|
|
1552
|
+
yield ModelStreamChunk(text=text, reasoning=reasoning)
|
|
1553
|
+
if event_type == "response.output_item.done":
|
|
1554
|
+
completed_key = str(
|
|
1555
|
+
(event.get("item") or {}).get("id")
|
|
1556
|
+
or event.get("item_id")
|
|
1557
|
+
or event.get("output_index")
|
|
1558
|
+
or "0"
|
|
1559
|
+
)
|
|
1560
|
+
calls = tuple(
|
|
1561
|
+
ToolCall(
|
|
1562
|
+
id=value["id"],
|
|
1563
|
+
name=value["name"],
|
|
1564
|
+
arguments=value["arguments"] or "{}",
|
|
1565
|
+
)
|
|
1566
|
+
for key, value in function_calls.items()
|
|
1567
|
+
if key == completed_key and key not in emitted_calls and value["name"]
|
|
1568
|
+
)
|
|
1569
|
+
if calls:
|
|
1570
|
+
emitted_calls.add(completed_key)
|
|
1571
|
+
yield ModelStreamChunk(tool_calls=calls)
|
|
1572
|
+
if event_type in {"response.completed", "response.incomplete"}:
|
|
1573
|
+
yield ModelStreamChunk(usage=usage_value, done=True)
|
|
1574
|
+
|
|
469
1575
|
@staticmethod
|
|
470
1576
|
def _is_length_truncation(exc: StudioError) -> bool:
|
|
471
1577
|
if exc.code != "MODEL_EMPTY_RESPONSE":
|
|
@@ -486,12 +1592,16 @@ class OpenAICompatibleModelClient:
|
|
|
486
1592
|
tools: list[dict[str, Any]] | None,
|
|
487
1593
|
) -> dict[str, Any]:
|
|
488
1594
|
"""把 chat 语义消息映射到 Responses API 最小可用载荷。"""
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
)
|
|
1595
|
+
# Responses tools are flat objects, unlike Chat Completions' nested
|
|
1596
|
+
# ``function`` envelope. Accept both shapes because Harness supplies
|
|
1597
|
+
# Chat-style schemas internally.
|
|
1598
|
+
responses_tools = []
|
|
1599
|
+
for tool in tools or []:
|
|
1600
|
+
function = tool.get("function") if isinstance(tool, dict) else None
|
|
1601
|
+
if isinstance(function, dict):
|
|
1602
|
+
responses_tools.append({"type": "function", **function})
|
|
1603
|
+
elif isinstance(tool, dict):
|
|
1604
|
+
responses_tools.append(dict(tool))
|
|
495
1605
|
instructions: list[str] = []
|
|
496
1606
|
items: list[dict[str, Any]] = []
|
|
497
1607
|
for message in messages:
|
|
@@ -506,6 +1616,8 @@ class OpenAICompatibleModelClient:
|
|
|
506
1616
|
"model": model.model,
|
|
507
1617
|
"input": items,
|
|
508
1618
|
}
|
|
1619
|
+
if responses_tools:
|
|
1620
|
+
payload["tools"] = responses_tools
|
|
509
1621
|
if model.parameters.max_tokens is not None:
|
|
510
1622
|
payload["max_output_tokens"] = model.parameters.max_tokens
|
|
511
1623
|
if instructions:
|
|
@@ -566,6 +1678,7 @@ class OpenAICompatibleModelClient:
|
|
|
566
1678
|
usage=usage,
|
|
567
1679
|
tool_calls=[],
|
|
568
1680
|
raw_message={"output": payload.get("output")},
|
|
1681
|
+
reasoning=_reasoning_from_responses_output(payload.get("output")),
|
|
569
1682
|
)
|
|
570
1683
|
|
|
571
1684
|
@staticmethod
|
|
@@ -626,4 +1739,5 @@ class OpenAICompatibleModelClient:
|
|
|
626
1739
|
usage=usage,
|
|
627
1740
|
tool_calls=calls,
|
|
628
1741
|
raw_message=message,
|
|
1742
|
+
reasoning=str(message.get("reasoning_content") or message.get("reasoning") or ""),
|
|
629
1743
|
)
|