五块卵石 Agent 引擎 - 基于生命周期钩子的插件化 Agent 核心
Project description
fp-core — 五块卵石 Agent 引擎
简介
fp-core 是五块卵石(Five Pebbles)Agent 框架的核心引擎。它提供了 Agent 生命周期管理、LLM 交互、工具执行、会话管理及插件化扩展等基础能力,是整个框架的基石。
如果你只是使用 Agent,请安装
fp主包;如果你要自定义/扩展 Agent 行为,你正在正确的地方。
特性
- 生命周期钩子系统 — 在 Agent 的启动、消息处理、工具调用、响应生成等阶段注入自定义逻辑
- 插件化架构 — 通过插件动态加载工具、命令、事件监听器,无需修改核心代码
- 多 LLM 支持 — 统一的 LLM 交互接口,可接入 OpenAI、Claude、本地模型等
- 工具执行引擎 — 安全的沙箱化工具执行,支持同步/异步、超时控制、结果缓存
- 会话管理 — 持久化的会话存储与恢复,支持多会话并行
- 提示词模板 — 结构化提示词组装,支持动态注入上下文与历史
安装
pip install fp-core
要求 Python >= 3.11。
快速使用
初始化 Agent 引擎
from fp_core import AgentEngine
engine = AgentEngine(config={
"llm": {"provider": "openai", "model": "gpt-4o"},
"session": {"storage": "file", "path": "./sessions"},
})
注册插件
from fp_core.plugins import BasePlugin
class MyPlugin(BasePlugin):
name = "my_plugin"
def on_agent_start(self, ctx):
print("Agent 启动了!")
def on_message(self, ctx, message):
# 在消息处理前注入逻辑
return message
engine.plugin_manager.register(MyPlugin())
执行一次对话
response = engine.chat("你好,请介绍一下你自己")
print(response)
架构概览
fp_core/
├── core/ # 核心运行时:AgentEngine, LLM 接口, 上下文管理
│ ├── agent.py # Agent 主循环
│ ├── llm.py # LLM 客户端抽象
│ └── context.py # 会话上下文
├── commands/ # 内置命令(内置工具)
├── tools/ # 工具执行引擎与内置工具集
│ └── plugins/ # 工具插件加载器
├── plugins/ # 插件系统
│ └── base/ # 插件基类与钩子定义
├── prompts/ # 提示词模板管理
├── skills/ # 技能定义与加载
└── sessions/ # 会话存储与管理
生命周期钩子
插件可以通过实现以下钩子方法介入 Agent 运行流程:
| 钩子 | 触发时机 | 典型用途 |
|---|---|---|
on_agent_start |
Agent 初始化完成 | 加载自定义资源 |
on_agent_stop |
Agent 关闭前 | 资源清理 |
on_message |
收到用户消息后 | 消息预处理/校验 |
before_llm_call |
调用 LLM 前 | 注入系统提示词 |
after_llm_call |
收到 LLM 响应后 | 响应后处理/过滤 |
before_tool_exec |
执行工具前 | 权限检查/参数校验 |
after_tool_exec |
工具执行完成后 | 结果格式化/缓存 |
on_response |
生成最终响应前 | 响应润色/转译 |
依赖
| 依赖 | 版本 | 用途 |
|---|---|---|
httpx |
>= 0.28.0 | LLM API 异步 HTTP 请求 |
rich |
>= 14.0.0 | 控制台输出格式化 |
wcwidth |
>= 0.2.0 | 终端文本宽度计算 |
pyyaml |
>= 6.0 | 配置文件解析 |
扩展开发
编写自定义插件
from fp_core.plugins import BasePlugin
from fp_core.plugins.base import hook
class LogPlugin(BasePlugin):
name = "logger"
@hook("on_message")
def log_message(self, ctx, message):
print(f"[LOG] 收到消息: {message[:50]}...")
return message
@hook("before_llm_call")
def inject_system_prompt(self, ctx, messages):
messages.insert(0, {"role": "system", "content": "请用简洁的语言回答"})
return messages
编写自定义工具
from fp_core.tools import BaseTool
class WeatherTool(BaseTool):
name = "get_weather"
description = "获取指定城市的天气"
async def execute(self, city: str) -> dict:
# 实现天气查询逻辑
return {"city": city, "temperature": 22}
注册到 Agent:
engine.tool_manager.register_tool(WeatherTool())
许可
MIT © zpb
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fp_core-0.1.5.tar.gz.
File metadata
- Download URL: fp_core-0.1.5.tar.gz
- Upload date:
- Size: 90.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daa0702c2aaced502a6bf4b8b8bf09828e0d0c99ea672ed151519fddaaef749a
|
|
| MD5 |
be75f4ac893f01ccab8b13950b6d9267
|
|
| BLAKE2b-256 |
beb34bcb9fc6788cc8a32f6995e42148ebc2a6569637128fe9e5ba608c61cd9d
|
Provenance
The following attestation bundles were made for fp_core-0.1.5.tar.gz:
Publisher:
release.yml on zpb911km/fp-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fp_core-0.1.5.tar.gz -
Subject digest:
daa0702c2aaced502a6bf4b8b8bf09828e0d0c99ea672ed151519fddaaef749a - Sigstore transparency entry: 1840062675
- Sigstore integration time:
-
Permalink:
zpb911km/fp-agent@bb7647f5a0cb70defaa291888a92fe23188fd7f0 -
Branch / Tag:
refs/tags/0.1.5 - Owner: https://github.com/zpb911km
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bb7647f5a0cb70defaa291888a92fe23188fd7f0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fp_core-0.1.5-py3-none-any.whl.
File metadata
- Download URL: fp_core-0.1.5-py3-none-any.whl
- Upload date:
- Size: 103.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
857627c1bbdd05d0250fe336b5b6f583e83d96e7b90ab8ea6afe447b1bcb2338
|
|
| MD5 |
55c19e99c9af4c2885e33d7b1ebe859b
|
|
| BLAKE2b-256 |
bc1baded97272f2d7bc38ee6780142a3b5271e74c28c09251694c8ada68c729b
|
Provenance
The following attestation bundles were made for fp_core-0.1.5-py3-none-any.whl:
Publisher:
release.yml on zpb911km/fp-agent
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fp_core-0.1.5-py3-none-any.whl -
Subject digest:
857627c1bbdd05d0250fe336b5b6f583e83d96e7b90ab8ea6afe447b1bcb2338 - Sigstore transparency entry: 1840062878
- Sigstore integration time:
-
Permalink:
zpb911km/fp-agent@bb7647f5a0cb70defaa291888a92fe23188fd7f0 -
Branch / Tag:
refs/tags/0.1.5 - Owner: https://github.com/zpb911km
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bb7647f5a0cb70defaa291888a92fe23188fd7f0 -
Trigger Event:
push
-
Statement type: