Files
sorrow404null a7412824e0 chore: import original project baseline
Import the pre-repair source tree as the history baseline.
Runtime data (data/), virtualenvs, bytecode caches and logs are
gitignored so local secrets and user state stay out of the repo.
2026-09-17 16:40:01 +08:00

59 lines
3.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
core/agent —— pi agent 核心框架的 Python 1:1 重构
==================================================
模块对照(pi-main → 本包):
packages/agent/src/types.ts → types.py 数据模型(消息/事件/工具/配置)
packages/agent/src/agent-loop.ts → loop.py 核心循环(runLoop 1:1
packages/agent/src/agent.ts → agent.py Agent 状态机(prompt/steer/followUp/abort
packages/ai/src/api/*.ts → stream_fn.py OpenAI 兼容流式(vLLM+ 输出预算钳制
packages/ai/src/api/simple-options→ context.py token 估算 + clampMaxTokensToContext
packages/agent/src/compaction.ts → compaction.py 上下文压缩(切分/摘要/替换)
agent-session.ts 后置恢复逻辑 → recovery.py 重试退避 + 溢出/截断压缩恢复
packages/agent/src/tools/*.ts → tools.py 工具管线 + 内置 read/bash/write/edit
使用示例(最小闭环):
from core.agent import Agent, AgentConfig, ModelConfig
from core.agent.stream_fn import openai_stream
from core.agent.recovery import AgentRunner
from core.agent.tools import default_tools
cfg = AgentConfig(model=ModelConfig(...), tools=default_tools())
agent = Agent(cfg)
agent.set_stream_fn(openai_stream)
runner = AgentRunner(agent)
result = runner.run("你好")
"""
from .types import (AgentConfig, AgentError, AgentEvent, AgentMessage,
AgentState, AgentTool, AgentToolResult, AbortSignal,
AssistantMessageEvent, ModelConfig, RetryConfig, RunResult,
ToolCall, new_id)
from .agent import Agent
from .context import (CONTEXT_SAFETY_TOKENS, calculate_context_tokens,
clamp_max_tokens_to_context,
estimate_context_tokens, estimate_message_tokens,
should_compact)
from .compaction import (CompactionSettings,
DEFAULT_COMPACTION_SETTINGS, compact_context,
find_cut_point, prepare_compaction)
from .recovery import (AgentRunner, compute_retry_delay_ms, find_last_assistant,
is_context_overflow, is_recoverable_length,
is_retryable_assistant_error)
from .stream_fn import (classify_error, from_openai_messages, openai_stream,
to_openai_messages)
from .tools import default_tools
# 🆕 版本记录:核心框架为 pi (badlogic/pi-mono) 0.81.x 时代的 Python 移植,
# 2026-09 对齐 pi 0.85.1 的估算/锚定/钳制/重试语义(P0/P1/P2/M1/M2/M3)。
__version__ = "1.0.0 (pi 0.81.x port, aligned 0.85.1 semantics)"
__all__ = [
"Agent", "AgentConfig", "AgentError", "AgentEvent", "AgentMessage",
"AgentRunner", "AgentState", "AgentTool", "AgentToolResult",
"AbortSignal", "AssistantMessageEvent", "CONTEXT_SAFETY_TOKENS",
"ModelConfig", "RetryConfig", "RunResult", "ToolCall",
"clamp_max_tokens_to_context", "calculate_context_tokens", "classify_error", "compact_context",
"compute_retry_delay_ms", "default_tools", "estimate_context_tokens",
"find_last_assistant", "find_cut_point", "is_context_overflow",
"is_recoverable_length", "is_retryable_assistant_error", "new_id",
"openai_stream", "should_compact", "to_openai_messages",
]