mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 05:50:44 +08:00
重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
This commit is contained in:
parent
ba3836f00f
commit
4a23b715a2
498 changed files with 2760 additions and 2200 deletions
56
runtime/orchestration/trace.py
Normal file
56
runtime/orchestration/trace.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
def new_trace_id() -> str:
|
||||
return f"tr_{uuid.uuid4().hex[:20]}"
|
||||
|
||||
|
||||
def new_span_id() -> str:
|
||||
return f"sp_{uuid.uuid4().hex[:16]}"
|
||||
|
||||
|
||||
def estimate_tokens(text: str) -> int:
|
||||
# rough heuristic: ~4 chars per token for English-ish; Chinese is denser but ok for hint.
|
||||
s = (text or "").strip()
|
||||
return max(0, int(len(s) / 4))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TraceContext:
|
||||
trace_id: str
|
||||
span_id: str
|
||||
parent_span_id: str | None = None
|
||||
|
||||
|
||||
class TraceEmitter:
|
||||
def __init__(self, store: Any):
|
||||
self.store = store
|
||||
|
||||
def emit(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
ctx: TraceContext,
|
||||
event_type: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
try:
|
||||
self.store.add_trace_event(
|
||||
session_id=session_id,
|
||||
trace_id=ctx.trace_id,
|
||||
span_id=ctx.span_id,
|
||||
parent_span_id=ctx.parent_span_id,
|
||||
event_type=event_type,
|
||||
payload=payload or {},
|
||||
)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
__all__ = ["TraceContext", "TraceEmitter", "new_trace_id", "new_span_id", "estimate_tokens"]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue