Community程式設計與開發github.com

bitsky-tech/AmphiLoop

基於 Bridgic 的公開代理技能

AmphiLoop 是什麼?

AmphiLoop 是 Bridgic 框架中的一個雙模式代理技能,讓代理程式能在 LLM 驅動的 `on_agent` 模式與確定性的 `on_workflow` 模式之間動態切換,並支援自動降級機制。此技能支援多種執行模式,包括純 AGENT、純 WORKFLOW、混合的 AMPHIFLOW(工作流程中可回溯至代理),以及自動偵測模式的 AUTO。開發者可透過宣告 `CognitiveWorker` 思考單元與上下文協調來定義代理行為,並利用四層架構(Exposure、CognitiveContext、CognitiveWorker、AmphibiousAutoma)實現高度靈活的編排。AmphiLoop 也內建人機互動機制(`request_human`)、自訂 Pydantic 輸出、階段快照等功能,適用於 Claude Code、Cursor 或 Codex 等代理執行環境,適合需要同時兼顧可預測工作流程與 LLM 彈性決策的複雜任務場景。

相容平台~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 代理接受來自 bridgic LLM 提供者套件、具備 astructure_output 協定的 BaseLlm 實例。AGENT 和 AMPHIFLOW 模式需要 LLM;純 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

參考文件

相關技能