Files
LLM_Proxy/hooks/example_hook.py
T
Von e734cf7246 refactor: simplify proxy architecture around openai claude codex
- trim public provider protocols to openai_chat, openai_responses, claude_chat and codex\n- remove public stream_format and gemini-family downstream surfaces\n- add executor/decoder/translator pipeline, provider help popovers and /v1/models metadata\n- refresh README, 4+1 architecture docs, sample config, and supporting tests\n- include local-time storage scripts and related repository/service coverage
2026-03-31 07:41:36 +08:00

49 lines
1.7 KiB
Python

from typing import Any
from src.hooks import BaseHook, HookContext
class Hook(BaseHook):
"""Example hook that demonstrates the header/guard extension points."""
def header_hook(self, ctx: HookContext, headers: dict[str, str]) -> dict[str, str]:
ctx.logger.info(
"header_hook invoked: provider=%s model=%s stream=%s",
ctx.provider_name,
ctx.request_model,
ctx.stream,
)
headers["X-Custom-Header"] = "custom-value"
return headers
def request_guard(self, ctx: HookContext, body: dict[str, Any]) -> dict[str, Any]:
ctx.logger.info(
"request_guard invoked: provider=%s source=%s target=%s",
ctx.provider_name,
ctx.provider_source_format,
ctx.provider_target_format,
)
messages = body.get("messages")
if isinstance(messages, list):
for msg in messages:
if isinstance(msg, dict) and msg.get("role") == "user":
original = str(msg.get("content", ""))
msg["content"] = f"[PREFIX] {original}"
return body
def response_guard(self, ctx: HookContext, body: Any) -> Any:
if not isinstance(body, dict):
return body
choices = body.get("choices")
if isinstance(choices, list):
for choice in choices:
if not isinstance(choice, dict):
continue
delta = choice.get("delta") or choice.get("message")
if isinstance(delta, dict) and "content" in delta and isinstance(delta.get("content"), str):
original = str(delta.get("content", ""))
delta["content"] = f"[MODIFIED] {original}"
return body