跳转到正文
wise-* 包教程 20 个可独立安装的 Composer 能力包:选包、装包、调用、扩展

智能代理 wise-agent

1 2026-11-25 23:02:34

让 AI 操作业务数据的底座:多模型接入、对话记忆、Function Calling、AI 定时任务。它的核心不是"调模型",而是把插件的业务方法变成 AI 能调用的工具。

安装

composer require wiseadmin/wise-agent
php think vendor:publish
php think wise:vendor:publish
php think migrate:run
php think wise:agent:discover

装完会往 .env 播种:AI_ENCRYPT_KEY(自动生成,别手工改)、OPENAI_API_KEY、DEEPSEEK_API_KEY(自己填)。

配置里两处必须动

config/wise-agent.php:

节关键项
default / providersenv('AI_DEFAULT_PROVIDER', 'openai');每家的 class、api_key、base_url、model、timeout、max_retry
agentmax_steps = 10、max_tokens_per_step = 4096、temperature = 0.7
memory.defaultdatabase(或 redis)
toolsfile_read / file_write / db_query / http_request 默认关闭,orchestrate_step 开启
webenable = true、prefix = admin/ai、groups.admin.middleware 默认为空
schemaauto_discover = true、cache_ttl = 3600、defaults.write_requires_confirmation = true

web.groups.admin.middleware 为空意味着 /admin/ai/* 没有鉴权。 生产环境第一件事就是挂 RbacMiddleware;这和"包应该自带保护"的直觉相反,但它是刻意的——中间件由宿主决定,包不替你选鉴权模型。

入口:全局函数 + 三个类

composer 会加载 src/helper.php,所以有两个全局函数:

echo wise_agent_chat('你好,介绍一下自己');
$reply = wise_agent()->chat('查询本月销售额');

三个核心类:

类方法
AgentManager(wise\agent\agent\)run()、runStream()、chat()、chatStream()、dispatchAsync()、getTrackingStatus()、withTools()
AiClientFactory(wise\agent\client\)create(?string $provider = null)、register()、clearCache()、getProviders()
ToolRegistry(wise\agent\tool\)register()、registerClass()、get()、has()、all()、getToolSchemas()、getByNamespace()、removeNamespace()

run() 与 chat() 的差别是"要不要走工具循环":chat() 单轮问答,run() 会进 ReAct 循环(模型可以连续调工具再回答)。

插件能力是怎么被发现的

插件在 manifest.json 声明,四条正向 + 一条反向:

层级声明产出工具名
L1ai_providers.tool → ToolInterface 类plugin:{ident}.{name}
L2ai_actions → 声明式 service 方法plugin:{ident}.{action}
L3控制器上的 #[AgentAction] 注解ma_{...}
L4ai_schema.auto_introspect → 表结构内省plugin:{ident}.auto.{op}
反向ai_triggers → 事件唤起 AI—

零代码最小声明就是一行:

{ "ai_schema": { "auto_introspect": true } }

然后 php think wise:agent:discover --force。命令还有 --schema-only、--dump=plugin、--dump-md、--bootstrap、--materialize;另有 wise:agent:scan-timeouts 用于清理卡死的异步任务。

L4 是最容易踩的一层:它按表结构自动生成增删改工具,所以 write_requires_confirmation 这个开关很重要——默认 true,意味着 AI 的写操作会走人工确认,别为了"顺畅"关掉它。

与另外两个包的关系

wise-agent 是写侧(调工具改数据)、wise-rag 是读侧(查知识库)、wise-mcp 是协议层(把能力暴露给外部客户端)。三者可独立使用,也可以串起来:RAG 自动往 agent 注册 kb.search,MCP 聚合两者的工具。

框架侧概览见AI 与智能体。

四个容易搞错的认知

一、"/admin/ai/* 自带权限"。 默认中间件为空,必须自己挂。
二、"工具方法名随便取"。 工具名参与权限标识与审批记录,改名等于换了一个新工具(旧授权不生效)。
三、"withTools() 能临时加任意工具"。 它只从 config('wise-agent.tools') 里挑内置工具,别人的工具要在 ToolRegistry 里注册。
四、"没配密钥会静默失败"。 会明确报密钥缺失——看到这个报错先查 .env。

自查:php think wise:agent:discover --force 应输出发现的 schema 数量;wise_agent_chat('hi') 能返回内容(说明密钥与客户端配置都对)。

这篇文档对您有帮助吗?

相关文档