Community编程与开发github.com

bitsky-tech/AmphiLoop

基于 Bridgic 的双模式代理框架技能,支持 LLM 驱动模式和确定性工作流模式,并自动回退切换。

AmphiLoop 是什么?

Bridgic Amphibious 是一个基于 Bridgic 框架的双模式代理技能,允许开发者构建兼具 LLM 驱动能力(`on_agent`)和确定性工作流能力(`on_workflow`)的智能体,并支持模式间的自动回退(AMPHIFLOW 模式)。该技能支持四种运行模式:AGENT(LLM 完全驱动)、WORKFLOW(纯确定性逻辑)、AMPHIFLOW(工作流为主,遇到失败时自动切换至 LLM 代理修复)以及 AUTO(根据重写的方法自动检测模式)。核心架构包含四层:Exposure(数据可见性抽象)、CognitiveContext(状态容器)、CognitiveWorker(纯思考单元)和 AmphibiousAutoma(编排引擎)。它还内置了人工介入(Human-in-the-Loop)机制、自定义 Pydantic 输出、阶段快照注解等功能,适用于需要灵活结合自动化和人工判断的复杂任务场景。该技能兼容 bridgic-core、bridgic-llms-openai 等依赖,并提供了 CLI 脚手架快速生成项目。

兼容平台~Claude Code✓Codex CLI~Cursor✓Antigravity
npx skills add bitsky-tech/AmphiLoop

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Bridgic Amphibious

双模式代理框架:代理在 LLM 驱动模式(on_agent)和确定性模式(on_workflow)下运行,并在两者之间自动回退。

依赖

一个 bridgic-amphibious 项目需要以下包:

包描述
bridgic-core核心框架(Worker, Automa, GraphAutoma, ASL)
bridgic-amphibious双模式代理框架
bridgic-llms-openaiLLM 提供商(仅 AGENT / AMPHIFLOW 模式需要)
python-dotenv.env 文件加载

使用此包之前,您需要通过提供的安装脚本安装依赖:

bash "skills/bridgic-amphibious/scripts/install-deps.sh" "$PWD"

该脚本会检查 uv 是否可用,如果需要会初始化一个 uv 项目,通过 uv add 安装任何缺失的包,并运行 uv sync 来最终完成环境配置。脚本成功退出后,项目即完全初始化并准备好使用——无需手动执行 uv add / uv sync 后续操作。

LLM 设置

Amphibious 代理接受一个 BaseLlm 实例,该实例来自 bridgic LLM 提供商包,并包含 astructure_output 协议。LLM 在 AGENT 和 AMPHIFLOW 模式下是必需的;纯 WORKFLOW 模式可以无需 LLM 运行。

from bridgic.llms.openai import OpenAILlm, OpenAIConfiguration

llm = OpenAILlm(
    api_key="your-api-key",
    api_base="https://api.openai.com/v1",  # 或自定义端点
    configuration=OpenAIConfiguration(model="gpt-4o", temperature=0.0),
)

其他具有相同协议的提供商:bridgic.llms.vllm.VllmServerLlm(自托管 vLLM)。

快速开始

from bridgic.amphibious import (
    AmphibiousAutoma, CognitiveContext, CognitiveWorker, think_unit,
)
from bridgic.core.agentic.tool_specs import FunctionToolSpec

async def get_weather(city: str) -> str:
    """获取某个城市的天气。"""
    return f"晴天,22°C,位于 {city}"

class WeatherAgent(AmphibiousAutoma[CognitiveContext]):
    planner = think_unit(
        CognitiveWorker.inline("查找天气并提供摘要。"),
        max_attempts=5,
    )
    async def on_agent(self, ctx: CognitiveContext):
        await self.planner

agent = WeatherAgent(llm=llm, verbose=True)
result = await agent.arun(
    goal="查看东京和伦敦的天气。",
    tools=[FunctionToolSpec.from_raw(get_weather)],
)

项目脚手架

使用 CLI 来引导一个新项目:

bridgic-amphibious create -n my_project
bridgic-amphibious create -n my_project --task "导航到 example.com 并提取数据"
bridgic-amphibious create -n my_project --base-dir /path/to/projects

创建文件:task.md、config.py、tools.py、workers.py、agents.py、skills/、result/、log/。

核心概念

代理 = 思考单元 + 上下文编排。 代理通过声明 CognitiveWorker 思考单元并在 on_agent() 或 on_workflow() 中进行编排来定义。

四层架构:

  1. Exposure — 数据可见性抽象(LayeredExposure / EntireExposure)
  2. CognitiveContext — 状态容器(目标、工具、技能、历史)
  3. CognitiveWorker — 纯思考单元(观察-思考-行动)
  4. AmphibiousAutoma — 编排引擎(模式路由、生命周期)

OTC 循环: 观察 -> 思考 -> 行动,每个阶段都有钩子点。

四种运行模式: AGENT(LLM 驱动)、WORKFLOW(确定性)、AMPHIFLOW(工作流 + 代理回退)、AUTO(根据重写的方法自动检测,默认)。

AUTO 解析: 仅重写 on_agent → AGENT;仅重写 on_workflow → WORKFLOW;两者都重写 → AMPHIFLOW。

关键模式

代理模式 — LLM 决策

class MyAgent(AmphibiousAutoma[CognitiveContext]):
    worker = think_unit(CognitiveWorker.inline("决定下一步。"), max_attempts=10)
    async def on_agent(self, ctx):
        await self.worker

工作流模式 — 开发者决策

from bridgic.amphibious import ActionCall

class MyWorkflow(AmphibiousAutoma[CognitiveContext]):
    async def on_workflow(self, ctx):
        result = yield ActionCall("tool_name", arg1="value")
        # result 是 List[ToolResult]

# 纯工作流模式不需要 LLM。
await MyWorkflow().arun(goal="...", tools=[...])

Amphiflow 模式 — 工作流带代理回退

from bridgic.amphibious import RunMode, AgentCall

class MyHybrid(AmphibiousAutoma[CognitiveContext]):
    fixer = think_unit(CognitiveWorker.inline("修复问题。"), max_attempts=5)
    async def on_agent(self, ctx): await self.fixer
    async def on_workflow(self, ctx):
        yield ActionCall("fill_field", name="user", value="john")
        yield ActionCall("click_button", name="submit")

await MyHybrid(llm=llm).arun(
    goal="...", tools=[...],
    mode=RunMode.AMPHIFLOW, max_consecutive_fallbacks=2,
)

人工介入

from bridgic.amphibious import ActionCall, HumanCall

class MyAgent(AmphibiousAutoma[CognitiveContext]):
    worker = think_unit(CognitiveWorker.inline("执行步骤。"), max_attempts=10)

    async def on_agent(self, ctx):
        await self.worker
        feedback = await self.request_human("继续吗?")  # 入口 1:代码级别

    async def on_workflow(self, ctx):
        yield ActionCall("do_something", arg="value")
        feedback = yield HumanCall(prompt="确认?")     # 入口 2:工作流 yield

# 入口 3:LLM 工具 — `request_human` 自动注入到每个代理的
# 工具列表中,因此 LLM 可以直接调用,无需你在 tools=[...] 中列出。
# (显式传递 `request_human_tool` 是无害的——它会去重。)
await MyAgent(llm=llm).arun(goal="...", tools=[my_tool])

# 如果你想显式传参,导入并传递 `request_human_tool` 同样可以。
from bridgic.amphibious.builtin_tools import request_human_tool
await agent.arun(goal="...", tools=[request_human_tool, ...])  # 也可以

自定义 Pydantic 输出

from pydantic import BaseModel

class Plan(BaseModel):
    phases: list[str]

class Planner(AmphibiousAutoma[CognitiveContext]):
    plan = think_unit(
        CognitiveWorker.inline("创建一个计划。", output_schema=Plan),
        max_attempts=1,
    )
    async def on_agent(self, ctx):
        result = await self.plan  # 返回 Plan 实例

阶段注解(快照)

async def on_agent(self, ctx):
    async with self.snapshot(goal="研究阶段"):
        await self.researcher
    async with self.snapshot(goal="写作阶段"):
        await self.writer

参考文件

相关技能