项目文件夹

文件
wehub-resource-sync 0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:03:20 +08:00

179 行
5.4 KiB
Python

"""Canonical Headroom pipeline lifecycle and extension contracts."""
from __future__ import annotations
import importlib.metadata
import logging
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Protocol
log = logging.getLogger(__name__)
ENTRY_POINT_GROUP = "headroom.pipeline_extension"
class PipelineStage(str, Enum):
"""Stable lifecycle stages for the canonical Headroom pipeline."""
SETUP = "setup"
PRE_START = "pre_start"
POST_START = "post_start"
INPUT_RECEIVED = "input_received"
INPUT_CACHED = "input_cached"
INPUT_ROUTED = "input_routed"
INPUT_COMPRESSED = "input_compressed"
INPUT_REMEMBERED = "input_remembered"
PRE_SEND = "pre_send"
POST_SEND = "post_send"
RESPONSE_RECEIVED = "response_received"
CANONICAL_PIPELINE_STAGES: tuple[PipelineStage, ...] = (
PipelineStage.SETUP,
PipelineStage.PRE_START,
PipelineStage.POST_START,
PipelineStage.INPUT_RECEIVED,
PipelineStage.INPUT_CACHED,
PipelineStage.INPUT_ROUTED,
PipelineStage.INPUT_COMPRESSED,
PipelineStage.INPUT_REMEMBERED,
PipelineStage.PRE_SEND,
PipelineStage.POST_SEND,
PipelineStage.RESPONSE_RECEIVED,
)
@dataclass
class PipelineEvent:
"""Event emitted at a canonical pipeline stage.
Extensions may mutate ``messages``, ``tools``, ``headers``, or ``metadata`` in
place, or return a replacement ``PipelineEvent`` from ``on_pipeline_event``.
"""
stage: PipelineStage
operation: str
request_id: str = ""
provider: str = ""
model: str = ""
messages: list[dict[str, Any]] | None = None
tools: list[dict[str, Any]] | None = None
headers: dict[str, str] | None = None
response: Any = None
metadata: dict[str, Any] = field(default_factory=dict)
class PipelineExtension(Protocol):
"""Request lifecycle extension contract for the canonical pipeline."""
def on_pipeline_event(self, event: PipelineEvent) -> PipelineEvent | None:
"""Handle a canonical pipeline event."""
def discover_pipeline_extensions() -> list[PipelineExtension]:
"""Load registered pipeline extensions from Python entry points."""
discovered: list[PipelineExtension] = []
try:
entries = importlib.metadata.entry_points(group=ENTRY_POINT_GROUP)
except Exception as exc: # noqa: BLE001 - importlib metadata varies by runtime
log.debug("pipeline extensions: entry-point enumeration failed: %s", exc)
return discovered
for entry in entries:
try:
extension = entry.load()
except Exception as exc: # noqa: BLE001 - third-party load failures are isolated
log.warning("pipeline extension %r failed to load: %s", entry.name, exc)
continue
if isinstance(extension, type):
try:
extension = extension()
except Exception as exc: # noqa: BLE001
log.warning("pipeline extension %r failed to initialize: %s", entry.name, exc)
continue
discovered.append(extension)
return discovered
def summarize_routing_markers(transforms_applied: list[str]) -> list[str]:
"""Return the routed transform markers emitted by ContentRouter."""
return [item for item in transforms_applied if item.startswith("router:")]
class PipelineExtensionManager:
"""Dispatch canonical pipeline events to configured extensions."""
def __init__(
self,
*,
hooks: Any = None,
extensions: list[Any] | None = None,
discover: bool = True,
) -> None:
resolved: list[Any] = []
if hooks is not None and callable(getattr(hooks, "on_pipeline_event", None)):
resolved.append(hooks)
if extensions:
resolved.extend(extensions)
if discover:
resolved.extend(discover_pipeline_extensions())
self._extensions = resolved
@property
def enabled(self) -> bool:
return bool(self._extensions)
def emit(
self,
stage: PipelineStage,
*,
operation: str,
request_id: str = "",
provider: str = "",
model: str = "",
messages: list[dict[str, Any]] | None = None,
tools: list[dict[str, Any]] | None = None,
headers: dict[str, str] | None = None,
response: Any = None,
metadata: dict[str, Any] | None = None,
) -> PipelineEvent:
"""Emit a canonical lifecycle event and return the final event state."""
event = PipelineEvent(
stage=stage,
operation=operation,
request_id=request_id,
provider=provider,
model=model,
messages=messages,
tools=tools,
headers=headers,
response=response,
metadata=metadata or {},
)
for extension in self._extensions:
handler = getattr(extension, "on_pipeline_event", None)
if not callable(handler):
continue
try:
updated = handler(event)
except Exception as exc: # noqa: BLE001 - preserve hook fail-open behavior
log.warning(
"pipeline extension %r failed during %s: %s",
type(extension).__name__,
stage.value,
exc,
)
continue
if isinstance(updated, PipelineEvent):
event = updated
return event