oclaw/orchestration/trace.py
oliver ba3836f00f 初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库
- 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等)
- 提交当前工程代码与配置

Made-with: Cursor
2026-04-24 22:31:22 +08:00

56 lines
1.3 KiB
Python

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"]