Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,3 @@
|
||||
开始任务前先读取 `/dm/AGENTS.md`,再读取目标路径适用的子级规则。
|
||||
|
||||
本项目使用 `core/skills/dever/SKILL.md` 的目录和边界合同,语法以 LANGUAGE 为准。Agent 状态和工具计算留在 Dever,JavaScript 只适配模型协议。Test 的 Port fake 只用于隔离模型边界,不把 fake 结果写成真实模型调用或受保护 HTTP 验收通过。
|
||||
@@ -0,0 +1,73 @@
|
||||
# 小型 ReAct Agent
|
||||
|
||||
本例演示 ReAct 工具调用:模型选择工具,Dever 执行计算并将结果交给模型,直到得到回答或耗尽轮次。模型通过 Ollama 的 [Chat API](https://docs.ollama.com/api/chat) 调用。[工具往返测试](test/assistant/agent/tool_roundtrip.dv)仅替换模型 Port,实际执行工具、消息追加和轮次推进。
|
||||
|
||||
## 从这里读
|
||||
|
||||
1. [chat.dv](module/assistant/agent/api/web/chat.dv):一行受保护的 `long post ask`,外部模型调用不放进数据库事务。
|
||||
2. [flow/agent.dv](module/assistant/agent/flow/agent.dv):先看 `ask → think → step → respond`。
|
||||
3. 同文件 `executeTools → executeTool → calculate`:真正执行加法、乘法,把结果追加为 tool 消息。
|
||||
4. [port/agent.dv](module/assistant/agent/port/agent.dv) 和 [适配器](module/assistant/agent/javascript/chat.mjs):一次模型调用的类型边界与 Ollama 协议转换。
|
||||
|
||||
```text
|
||||
ask
|
||||
think:最多调用模型 max_rounds 次
|
||||
step:携带完整消息历史调用模型
|
||||
respond
|
||||
有工具调用 → 顺序执行 → 追加工具结果 → 下一轮
|
||||
无工具调用 → 保存最终回答 → 结束循环
|
||||
check:有回答则返回;预算耗尽仍未回答则明确失败
|
||||
```
|
||||
|
||||
工具是普通的 Dever 计算,没有动态执行代码、工具注册表或通用 Agent 框架。`calculate` 的两行就是允许执行的全部工具。这里的工具无副作用;以后添加发邮件、支付等工具,须另行定义权限、确认和失败合同,不能照搬纯计算工具的处理。
|
||||
|
||||
## 怎么用
|
||||
|
||||
先在 [setting.json](config/setting.json) 填入实际 Ollama `/api/chat` 地址、支持 function tools 的模型名和每次请求的超时。示例地址故意使用 `.invalid`,不会指向某个已有服务。不自动启动、下载模型或调用收费服务。
|
||||
|
||||
HTTP 入口默认要求身份与对应权限,不公开模型调用能力。当前配置只列出站点路径、监听地址和模型设置,**真实登录 Provider 尚未接入,不能直接启动为完整服务**。运行前须接入 Provider、verify、登录和授权;缺失时明确报错,不增加假身份或降为匿名。
|
||||
|
||||
在项目目录执行 `dever test .` 验证业务;用 `dever lib install .` 准备受管 Node Worker 及原生依赖锁。真实认证和模型配置准备好之后,网页发送下列请求,携带已有登录凭据:
|
||||
|
||||
```http
|
||||
POST /assistant/agent/web/chat/ask
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"question": "先算 12 加 30,再把结果乘以 2。请使用工具。",
|
||||
"max_rounds": 5
|
||||
}
|
||||
```
|
||||
|
||||
一种可能的调用过程如下;这不是实测记录,也不保证模型每次选择完全相同的路径:
|
||||
|
||||
| 轮次 | 模型请求 | Dever 执行 |
|
||||
| --- | --- | --- |
|
||||
| 1 | `Add(a=12, b=30)` | 得到 42,写入消息历史 |
|
||||
| 2 | `Multiply(a=42, b=2)` | 得到 84,写入消息历史 |
|
||||
| 3 | 最终回答 | 返回 `answer` 和 `rounds=3` |
|
||||
|
||||
同一轮多个工具按模型返回顺序执行,所有结果都带工具名称回传。模型最后一轮仍要求工具时,循环消耗完预算,主流程的 check 报 `BudgetExceeded`。`max_rounds` 统计模型调用次数,不是工具数。
|
||||
|
||||
消息追加直接写在 step 中,最终回答直接放入 State。工具循环只接收消息列表,不搬运整个 Agent 状态;不另设 remember、complete、finish 转发函数。
|
||||
|
||||
## 哪些检查只做一次
|
||||
|
||||
`Arguments` 声明参数类型与范围,受管 Worker 返回到 Dever 时按生成的合同校验,内部 `calculate` 不再检查。ToolName 使用普通枚举;未知工具名、错误参数和超范围值在 Port 边界拒绝。Ollama 的工具描述提供协议所需的名称与 JSON 形状,不在 JavaScript 重写参数范围或业务计算。
|
||||
|
||||
网络、超时、取消、JSON 和 Worker 合同错误直接传播,不自动重试,不补空答案。Ollama 普通回答可省略 `tool_calls`,适配器按协议转成空列表;明确的 null 或损坏结构不作“没有工具”处理。适配器接收并传播 Worker 的取消信号。
|
||||
|
||||
本例尚未完成真实 Ollama 模型调用与真实登录 Provider 接入。Worker 范围/枚举、受管资源和取消收尾由核心专项测试覆盖,不能据此声称已完成该项目的外部环境验收。JavaScript 单测只核对协议转换与错误传播。工作流由 [link.md](module/assistant/agent/link.md) 从真实源码生成。
|
||||
|
||||
## 新编译器的验收输入
|
||||
|
||||
| 场景 | 应有结果 |
|
||||
| --- | --- |
|
||||
| 未登录或没有入口权限 | 在 HTTP 边界拒绝,不调用模型 |
|
||||
| 第一轮直接回答 | 返回答案,rounds=1 |
|
||||
| 加法后乘法,再回答 | 工具结果进入下一轮消息,最终为 84 |
|
||||
| 同轮多个工具 | 按顺序各执行一次,保留全部结果 |
|
||||
| 每轮都要求工具 | 达到预算报 BudgetExceeded |
|
||||
| 未知工具、缺参数、超范围 | Port 边界拒绝,不执行工具 |
|
||||
| 网络失败、模型超时 | 保留失败,不返回空字符串成功 |
|
||||
| 调用取消 | 停止网络与后续轮次,等待 Worker 收尾 |
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"adapter": {
|
||||
"assistant.agent": {
|
||||
"setting": {
|
||||
"endpoint": "http://ollama.example.invalid:11434/api/chat",
|
||||
"model": "replace-with-your-tool-capable-model",
|
||||
"timeout_ms": 30000
|
||||
}
|
||||
}
|
||||
},
|
||||
"sites": { "web": { "path": "web" } },
|
||||
"http": { "host": "127.0.0.1", "port": 18091 },
|
||||
"log": { "level": "info" }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
负责一次有预算的工具型 Agent 调用。主流程和工具规则见 [flow/agent.dv](flow/agent.dv),使用例子见 [项目说明](../../../README.md),符号说明见 [Agent 主题](doc/agent.md)。
|
||||
@@ -0,0 +1,9 @@
|
||||
setting {
|
||||
endpoint: Text
|
||||
model: Text
|
||||
timeout_ms: Int >= 1 and <= 3600000
|
||||
}
|
||||
|
||||
external npm "javascript/chat.mjs" {
|
||||
allow network
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
long post ask = app.ask
|
||||
@@ -0,0 +1,13 @@
|
||||
# Agent
|
||||
|
||||
## assistant.agent.app.ask
|
||||
|
||||
模型可以直接回答,也可以请求使用计算工具。函数返回模型的最终回答和调用次数。如果用完允许的轮次仍没有回答,函数会报错。
|
||||
|
||||
## assistant.agent.app.think
|
||||
|
||||
每次消费一个轮次,得到最终回答便停止。全部轮次共享消息历史,工具结果进入下一次模型请求。
|
||||
|
||||
## assistant.agent.app.calculate
|
||||
|
||||
根据工具名选择加法或乘法。参数类型和范围由 Port 返回边界保证,这里只做计算。
|
||||
@@ -0,0 +1,73 @@
|
||||
type ToolName {
|
||||
Add
|
||||
Multiply
|
||||
}
|
||||
|
||||
type Arguments {
|
||||
a: Int >= -1000000 and <= 1000000
|
||||
b: Int >= -1000000 and <= 1000000
|
||||
}
|
||||
|
||||
type ToolCall {
|
||||
name: ToolName
|
||||
arguments: Arguments
|
||||
}
|
||||
|
||||
type Message {
|
||||
role: Text
|
||||
content: Text
|
||||
calls: List<ToolCall>
|
||||
tool_name: ToolName?
|
||||
}
|
||||
|
||||
type Turn {
|
||||
content: Text
|
||||
calls: List<ToolCall>
|
||||
}
|
||||
|
||||
type State {
|
||||
messages: List<Message>
|
||||
answer: Text?
|
||||
rounds: Int
|
||||
}
|
||||
|
||||
type Reply {
|
||||
answer: Text
|
||||
rounds: Int
|
||||
}
|
||||
|
||||
type Problem {
|
||||
error BudgetExceeded
|
||||
}
|
||||
|
||||
flow ask(question: Text, max_rounds: Int >= 1 and <= 10) (reply: Reply) {
|
||||
state = app.think(question, max_rounds)
|
||||
answer = state.answer
|
||||
check answer != null else Problem.BudgetExceeded
|
||||
reply = Reply { answer = answer, rounds = state.rounds }
|
||||
}
|
||||
|
||||
flow step(index: Int, current: State) (acc: State, stop: Bool) {
|
||||
turn = port.chat(current.messages)
|
||||
messages = append(current.messages, Message { role = "assistant", content = turn.content, calls = turn.calls, tool_name = null })
|
||||
stop = length(turn.calls) == 0
|
||||
acc = app.respond(stop, turn, messages, index + 1)
|
||||
}
|
||||
|
||||
start(question: Text) (state: State) {
|
||||
state = State {
|
||||
messages = [Message {
|
||||
role = "system"
|
||||
content = "你是计算助手。加法和乘法必须调用工具,用工具结果继续任务,完成后给出简短中文答案。"
|
||||
calls = []
|
||||
tool_name = null
|
||||
}, Message { role = "user", content = question, calls = [], tool_name = null }]
|
||||
answer = null
|
||||
rounds = 0
|
||||
}
|
||||
}
|
||||
|
||||
executeTool(call: ToolCall, messages: List<Message>) (next: List<Message>) {
|
||||
value = app.calculate(call.name, call.arguments)
|
||||
next = append(messages, Message { role = "tool", content = int.toText(value), calls = [], tool_name = call.name })
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// 只转换 Ollama 协议;选工具、执行工具和推进轮次都在 Dever 中。
|
||||
const tools = ['Add', 'Multiply'].map(name => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name,
|
||||
description: name === 'Add' ? '计算两个整数的和' : '计算两个整数的乘积',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
a: { type: 'integer' },
|
||||
b: { type: 'integer' }
|
||||
},
|
||||
required: ['a', 'b'],
|
||||
additionalProperties: false
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
function toMessage(message) {
|
||||
const output = { role: message.role, content: message.content };
|
||||
if (message.calls.length > 0) {
|
||||
output.tool_calls = message.calls.map(call => ({ function: call }));
|
||||
}
|
||||
if (message.tool_name !== null) output.tool_name = message.tool_name;
|
||||
return output;
|
||||
}
|
||||
|
||||
export async function chat(payload, setting, signal) {
|
||||
const response = await fetch(setting.endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model: setting.model,
|
||||
messages: payload.messages.map(toMessage),
|
||||
tools,
|
||||
stream: false
|
||||
}),
|
||||
signal: AbortSignal.any([signal, AbortSignal.timeout(setting.timeout_ms)])
|
||||
});
|
||||
if (!response.ok) throw new Error(`Ollama HTTP ${response.status}`);
|
||||
const { message } = await response.json();
|
||||
// tool_calls 在普通回答中可省略,这是协议定义的缺省,不是失败兜底。
|
||||
const calls = message.tool_calls === undefined ? [] : message.tool_calls;
|
||||
return {
|
||||
turn: {
|
||||
content: message.content,
|
||||
calls: calls.map(({ function: call }) => ({
|
||||
name: call.name,
|
||||
arguments: call.arguments
|
||||
}))
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,513 @@
|
||||
<!-- Generated by dever inspect --links. Do not edit. -->
|
||||
<!-- source-sha256: d0add6b9bc387eed7ab8561bafa659d8df2e8cc009d1c4523f11ee31aa171138 -->
|
||||
|
||||
# assistant/agent
|
||||
|
||||
按源码顺序阅读。图显示静态路径;运行故障结束当前调用,不是某次执行记录。
|
||||
|
||||
<a id="flow-274afd57a15a6a9cbfc1daeda85f059f1fb494e6285b6adb1e242d7f6213d70d"></a>
|
||||
|
||||
## assistant.agent.adapter.Setting
|
||||
|
||||
```dever
|
||||
type assistant.agent.adapter.Setting
|
||||
```
|
||||
|
||||
[打开源码](<adapter/agent.dv#L1,1>)
|
||||
|
||||
|
||||
<a id="flow-3ac77485267f2f31580e6e2c6bf87b474f7b128ff06c29d914adeaea6bfc9aa8"></a>
|
||||
|
||||
## assistant.agent.adapter.setting
|
||||
|
||||
```dever
|
||||
setting assistant.agent.adapter.setting: Setting
|
||||
```
|
||||
|
||||
[打开源码](<adapter/agent.dv#L1,1>)
|
||||
|
||||
|
||||
<a id="flow-a3aa464e21a51002ef06804f11ad182c00fc1f83b63e3179ab06309fa705feef"></a>
|
||||
|
||||
## assistant.agent.adapter.port.chat
|
||||
|
||||
```dever
|
||||
assistant.agent.adapter.port.chat(messages: List<app.Message>) (turn: app.Turn)
|
||||
```
|
||||
|
||||
[打开源码](<adapter/agent.dv#L7,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.adapter.port.chat"])
|
||||
click n0 href "adapter/agent.dv#L7,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2["调用外部 Worker:握手后复用会话;响应按 Port 解码"]
|
||||
click n2 href "adapter/agent.dv#L7,1" "打开源码"
|
||||
n3["交付结果;取消时排空响应,协议故障回收进程组"]
|
||||
click n3 href "adapter/agent.dv#L7,1" "打开源码"
|
||||
n4(["传播原错误"])
|
||||
n5["port · turn = external npm #34;javascript/chat.mjs#34; { allow network }"]
|
||||
click n5 href "adapter/agent.dv#L7,1" "打开源码"
|
||||
n0 --> n2
|
||||
n2 --> n3
|
||||
n2 -->|"失败"| n4
|
||||
n3 --> n5
|
||||
n5 --> n1
|
||||
```
|
||||
|
||||
|
||||
<a id="flow-6f694083978d64cd744cea63939dfc841312dd01f6b300998d70d89f4b150d2d"></a>
|
||||
|
||||
## POST /assistant/agent/web/chat/ask
|
||||
|
||||
```dever
|
||||
long post ask = app.ask
|
||||
|
||||
```
|
||||
|
||||
[打开源码](<api/web/chat.dv#L1,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0["POST /assistant/agent/web/chat/ask"]
|
||||
click n0 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n1["站点 Host 校验"]
|
||||
click n1 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n2["签名、站点与有效期 → 项目 verify"]
|
||||
click n2 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n3["按可信身份选择物理租户库;global 保留控制库"]
|
||||
click n3 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n4["查询当前角色与精确入口权限"]
|
||||
click n4 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n5["按参数类型解码;拒绝未知、重复及缺失字段"]
|
||||
click n5 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n6[["assistant.agent.app.ask"]]
|
||||
click n6 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n7["返回 code / message / data"]
|
||||
click n7 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n8["错误响应;内部原因仅记服务日志"]
|
||||
click n8 href "api/web/chat.dv#L1,1" "打开源码"
|
||||
n0 -->|"成功"| n1
|
||||
n1 -->|"通过"| n2
|
||||
n2 -->|"通过"| n3
|
||||
n3 -->|"通过"| n4
|
||||
n4 -->|"通过"| n5
|
||||
n5 -->|"通过"| n6
|
||||
n6 -->|"成功"| n7
|
||||
n1 -->|"失败"| n8
|
||||
n2 -->|"失败"| n8
|
||||
n3 -->|"失败"| n8
|
||||
n4 -->|"失败"| n8
|
||||
n5 -->|"失败"| n8
|
||||
n6 -->|"失败"| n8
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.ask](<link.md#flow-cced3f5750acdabc70274dd6b50b2dc66bec2ec9c2de329a6093013752606e98>)
|
||||
|
||||
<a id="flow-b68502d89d9c9df86b85908291f30d10ac90137ac0e8ea41d23939a2288732cf"></a>
|
||||
|
||||
## assistant.agent.app.ToolName
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.ToolName
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L1,1>)
|
||||
|
||||
|
||||
<a id="flow-d72936d1428133b976d5b5104cfc0d1f0cc182d548509e85c5dab7fe9c143477"></a>
|
||||
|
||||
## assistant.agent.app.Arguments
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.Arguments
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L6,1>)
|
||||
|
||||
|
||||
<a id="flow-fcf7fc516bc34e2836302ca8e958b950c39e2c411043d64fb20be89a1ded1f72"></a>
|
||||
|
||||
## assistant.agent.app.ToolCall
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.ToolCall
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L11,1>)
|
||||
|
||||
|
||||
<a id="flow-b4c0c8f75b2439058680a70a363bea8d8f5817fe4225f7d96fafdbefa7bfcedb"></a>
|
||||
|
||||
## assistant.agent.app.Message
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.Message
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L16,1>)
|
||||
|
||||
|
||||
<a id="flow-9e0ee8ce21af8a55fb93fdca007630e49409e90d8d53dd700ed2330b1103a5bd"></a>
|
||||
|
||||
## assistant.agent.app.Turn
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.Turn
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L23,1>)
|
||||
|
||||
|
||||
<a id="flow-50bf94a1a786c8d6f4030a6cedafc280ae843a47713d1083cbc617e3a6e41189"></a>
|
||||
|
||||
## assistant.agent.app.State
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.State
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L28,1>)
|
||||
|
||||
|
||||
<a id="flow-7d5425798884f00a8899d9c6a872c9a22623235aa35c4be73f8c002697d3825e"></a>
|
||||
|
||||
## assistant.agent.app.Reply
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.Reply
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L34,1>)
|
||||
|
||||
|
||||
<a id="flow-8c6491db0aa0a3624df68fb73f2c346be0e683c268a00b39852478598b342da2"></a>
|
||||
|
||||
## assistant.agent.app.Problem
|
||||
|
||||
```dever
|
||||
type assistant.agent.app.Problem
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L39,1>)
|
||||
|
||||
|
||||
<a id="flow-cced3f5750acdabc70274dd6b50b2dc66bec2ec9c2de329a6093013752606e98"></a>
|
||||
|
||||
## assistant.agent.app.ask
|
||||
|
||||
```dever
|
||||
flow assistant.agent.app.ask(question: Text, max_rounds: Int >= 1 and <= 10) (reply: Reply)
|
||||
```
|
||||
|
||||
模型可以直接回答,也可以请求使用计算工具。函数返回模型的最终回答和调用次数。如果用完允许的轮次仍没有回答,函数会报错。
|
||||
|
||||
[打开源码](<flow/agent.dv#L43,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.ask"])
|
||||
click n0 href "flow/agent.dv#L43,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2[["挂起 · app.think(question, max_rounds)"]]
|
||||
click n2 href "flow/agent.dv#L44,11" "打开源码"
|
||||
n3(["传播原错误"])
|
||||
n4["loop · state = app.think(question, max_rounds)"]
|
||||
click n4 href "flow/agent.dv#L44,3" "打开源码"
|
||||
n5["calc · answer = state.answer"]
|
||||
click n5 href "flow/agent.dv#L45,3" "打开源码"
|
||||
n6{"check · answer != null"}
|
||||
click n6 href "flow/agent.dv#L46,3" "打开源码"
|
||||
n7["校验通过"]
|
||||
n8["Problem.BudgetExceeded"]
|
||||
click n8 href "flow/agent.dv#L46,29" "打开源码"
|
||||
n9["calc · reply = Reply { answer = answer, rounds = state.rounds }"]
|
||||
click n9 href "flow/agent.dv#L47,3" "打开源码"
|
||||
n0 --> n2
|
||||
n2 -->|"失败"| n3
|
||||
n2 --> n4
|
||||
n4 --> n5
|
||||
n5 --> n6
|
||||
n6 -->|"成立"| n7
|
||||
n6 -->|"不成立"| n8
|
||||
n8 -->|"失败"| n3
|
||||
n7 --> n9
|
||||
n9 --> n1
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.think](<link.md#flow-601fc7d46004bfb0408dc6f04811585a7dae0be561564f706068c01eed7ce47c>)
|
||||
|
||||
<a id="flow-57d7e01fc5b7bc1793b5e455a88448f5e7a9a5c465491f982ea948c1863158b7"></a>
|
||||
|
||||
## assistant.agent.app.step
|
||||
|
||||
```dever
|
||||
flow assistant.agent.app.step(index: Int, current: State) (acc: State, stop: Bool)
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L50,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.step"])
|
||||
click n0 href "flow/agent.dv#L50,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2[["挂起 · port.chat(current.messages)"]]
|
||||
click n2 href "flow/agent.dv#L51,10" "打开源码"
|
||||
n3(["传播原错误"])
|
||||
n4["port · turn = port.chat(current.messages)"]
|
||||
click n4 href "flow/agent.dv#L51,3" "打开源码"
|
||||
n5["calc · messages = append(current.messages, Message { role = #34;assistant#34;, content = turn.content, calls = turn.calls, tool_name = null })"]
|
||||
click n5 href "flow/agent.dv#L52,3" "打开源码"
|
||||
n6["calc · stop = length(turn.calls) == 0"]
|
||||
click n6 href "flow/agent.dv#L53,3" "打开源码"
|
||||
n7[["app.respond(stop, turn, messages, index + 1)"]]
|
||||
click n7 href "flow/agent.dv#L54,9" "打开源码"
|
||||
n8["rule · acc = app.respond(stop, turn, messages, index + 1)"]
|
||||
click n8 href "flow/agent.dv#L54,3" "打开源码"
|
||||
n0 --> n2
|
||||
n2 -->|"失败"| n3
|
||||
n2 --> n4
|
||||
n4 --> n5
|
||||
n5 --> n6
|
||||
n6 --> n7
|
||||
n7 --> n8
|
||||
n8 --> n1
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.adapter.port.chat](<link.md#flow-a3aa464e21a51002ef06804f11ad182c00fc1f83b63e3179ab06309fa705feef>)
|
||||
- 子流程 [assistant.agent.app.respond](<link.md#flow-4b72c07e9544551c93a81251675b198b906f26b506d3cbf90920855875ed4aef>)
|
||||
|
||||
<a id="flow-9de7b5ba6751c54e8b3198b49ddb4a8f9630f5b568b3ea8e67da38dee25d449f"></a>
|
||||
|
||||
## assistant.agent.app.start
|
||||
|
||||
```dever
|
||||
assistant.agent.app.start(question: Text) (state: State)
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L57,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.start"])
|
||||
click n0 href "flow/agent.dv#L57,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2["state = State { messages = [Message { role = #34;system#34; content = #34;你是计算助手。加法和乘法必须调用工具,用工具结果继续任务,完成后给出简短中文答案。#34; calls = [] tool_name = null }, Message { role = #34;user#34;, content = question, calls = [], tool_name = null }] answer = null rounds = 0 }"]
|
||||
click n2 href "flow/agent.dv#L58,11" "打开源码"
|
||||
n0 --> n2
|
||||
n2 --> n1
|
||||
```
|
||||
|
||||
|
||||
<a id="flow-b10562e5aa621d3f01b7629c0aa8aa651fd3f6ccd8f71e199e76a3c425203014"></a>
|
||||
|
||||
## assistant.agent.app.executeTool
|
||||
|
||||
```dever
|
||||
assistant.agent.app.executeTool(call: ToolCall, messages: List<Message>) (next: List<Message>)
|
||||
```
|
||||
|
||||
[打开源码](<flow/agent.dv#L70,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.executeTool"])
|
||||
click n0 href "flow/agent.dv#L70,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2[["app.calculate(call.name, call.arguments)"]]
|
||||
click n2 href "flow/agent.dv#L71,11" "打开源码"
|
||||
n3["value = app.calculate(call.name, call.arguments)"]
|
||||
click n3 href "flow/agent.dv#L71,11" "打开源码"
|
||||
n4[["int.toText(value)"]]
|
||||
click n4 href "flow/agent.dv#L72,62" "打开源码"
|
||||
n5["next = append(messages, Message { role = #34;tool#34;, content = int.toText(value), calls = [], tool_name = call.name })"]
|
||||
click n5 href "flow/agent.dv#L72,10" "打开源码"
|
||||
n0 --> n2
|
||||
n2 --> n3
|
||||
n3 --> n4
|
||||
n4 --> n5
|
||||
n5 --> n1
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.calculate](<link.md#flow-fc4b94463ebbc65f8e5e9bc05b18295ec6aa43e7fa8ebec24a4becba0f05955a>)
|
||||
- 子流程 [dever.int.app.toText](<../../dever/int/link.md#flow-6ee889c4b5160675479210a68a4370d8ec43f5806985e52d961dc433a86fc8d1>)
|
||||
|
||||
<a id="flow-601fc7d46004bfb0408dc6f04811585a7dae0be561564f706068c01eed7ce47c"></a>
|
||||
|
||||
## assistant.agent.app.think
|
||||
|
||||
```dever
|
||||
loop assistant.agent.app.think(question: Text, max_rounds: Int >= 1 and <= 10) (state: State)
|
||||
```
|
||||
|
||||
每次消费一个轮次,得到最终回答便停止。全部轮次共享消息历史,工具结果进入下一次模型请求。
|
||||
|
||||
[打开源码](<loop/agent.dv#L1,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.think"])
|
||||
click n0 href "loop/agent.dv#L1,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2[["app.start(question)"]]
|
||||
click n2 href "loop/agent.dv#L3,23" "打开源码"
|
||||
n3["初始化 current: app.start(question)"]
|
||||
click n3 href "loop/agent.dv#L3,23" "打开源码"
|
||||
n4{"全部行完成则停止,否则拉取下一元素"}
|
||||
click n4 href "loop/agent.dv#L2,1" "打开源码"
|
||||
n5["foldUntil(current = app.start(question), app.step(index, current))"]
|
||||
click n5 href "loop/agent.dv#L3,3" "打开源码"
|
||||
n6["下一输出行"]
|
||||
n7{"本行是否已完成"}
|
||||
click n7 href "loop/agent.dv#L3,3" "打开源码"
|
||||
n8[["挂起 · app.step(index, current)"]]
|
||||
click n8 href "loop/agent.dv#L3,44" "打开源码"
|
||||
n9(["传播原错误"])
|
||||
n10["采用新的累积状态"]
|
||||
click n10 href "loop/agent.dv#L3,3" "打开源码"
|
||||
n11{"stop 是否为 true"}
|
||||
click n11 href "loop/agent.dv#L3,3" "打开源码"
|
||||
n0 --> n2
|
||||
n2 -->|"只求值一次"| n3
|
||||
n3 -->|"来源只求值一次"| n4
|
||||
n4 -->|"来源耗尽或全部完成"| n1
|
||||
n4 --> n7
|
||||
n7 -->|"已完成,跳过"| n6
|
||||
n7 -->|"未完成"| n5
|
||||
n5 --> n8
|
||||
n8 -->|"失败"| n9
|
||||
n8 -->|"更新成功"| n10
|
||||
n10 -->|"随后判断停止"| n11
|
||||
n11 -->|"是,标记本行完成"| n6
|
||||
n11 -->|"否,本行继续累积"| n6
|
||||
n6 -->|"继续"| n4
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.start](<link.md#flow-9de7b5ba6751c54e8b3198b49ddb4a8f9630f5b568b3ea8e67da38dee25d449f>)
|
||||
- 子流程 [assistant.agent.app.step](<link.md#flow-57d7e01fc5b7bc1793b5e455a88448f5e7a9a5c465491f982ea948c1863158b7>)
|
||||
|
||||
<a id="flow-7812dd91228edaecfeb6fa6d35bf912008c8dadfed8d620ae1d188a1ca37f240"></a>
|
||||
|
||||
## assistant.agent.app.executeTools
|
||||
|
||||
```dever
|
||||
loop assistant.agent.app.executeTools(calls: List<ToolCall>, messages: List<Message>) (next: List<Message>)
|
||||
```
|
||||
|
||||
[打开源码](<loop/agent.dv#L6,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.executeTools"])
|
||||
click n0 href "loop/agent.dv#L6,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2["初始化 current: messages"]
|
||||
click n2 href "loop/agent.dv#L8,18" "打开源码"
|
||||
n3{"全部行完成则停止,否则拉取下一元素"}
|
||||
click n3 href "loop/agent.dv#L7,1" "打开源码"
|
||||
n4["fold(current = messages, app.executeTool(call, current))"]
|
||||
click n4 href "loop/agent.dv#L8,3" "打开源码"
|
||||
n5["下一输出行"]
|
||||
n6[["app.executeTool(call, current)"]]
|
||||
click n6 href "loop/agent.dv#L8,28" "打开源码"
|
||||
n0 -->|"只求值一次"| n2
|
||||
n2 -->|"来源只求值一次"| n3
|
||||
n3 -->|"来源耗尽或全部完成"| n1
|
||||
n3 --> n4
|
||||
n4 --> n6
|
||||
n6 -->|"更新本行结果"| n5
|
||||
n5 -->|"继续"| n3
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.executeTool](<link.md#flow-b10562e5aa621d3f01b7629c0aa8aa651fd3f6ccd8f71e199e76a3c425203014>)
|
||||
|
||||
<a id="flow-31a99349d15c55180048fb9e394ae9b6238e21625731653fa2f8389bcf34e184"></a>
|
||||
|
||||
## assistant.agent.port.chat
|
||||
|
||||
```dever
|
||||
assistant.agent.port.chat(messages: List<app.Message>) (turn: app.Turn) fails dever.process.Error
|
||||
```
|
||||
|
||||
[打开源码](<port/agent.dv#L1,1>)
|
||||
|
||||
- 入口绑定 [assistant.agent.adapter.port.chat](<link.md#flow-a3aa464e21a51002ef06804f11ad182c00fc1f83b63e3179ab06309fa705feef>)
|
||||
|
||||
<a id="flow-4b72c07e9544551c93a81251675b198b906f26b506d3cbf90920855875ed4aef"></a>
|
||||
|
||||
## assistant.agent.app.respond
|
||||
|
||||
```dever
|
||||
rule assistant.agent.app.respond(finished: Bool, turn: Turn, messages: List<Message>, rounds: Int) (state: State)
|
||||
```
|
||||
|
||||
[打开源码](<rule/agent.dv#L1,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.respond"])
|
||||
click n0 href "rule/agent.dv#L1,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2{"按输入选择唯一规则行"}
|
||||
click n2 href "rule/agent.dv#L1,1" "打开源码"
|
||||
n3["finished: true"]
|
||||
click n3 href "rule/agent.dv#L3,3" "打开源码"
|
||||
n4["state = State { messages = messages, answer = turn.content, rounds = rounds }"]
|
||||
click n4 href "rule/agent.dv#L3,11" "打开源码"
|
||||
n5["finished: false"]
|
||||
click n5 href "rule/agent.dv#L4,3" "打开源码"
|
||||
n6[["app.executeTools(turn.calls, messages)"]]
|
||||
click n6 href "rule/agent.dv#L4,31" "打开源码"
|
||||
n7["state = State { messages = app.executeTools(turn.calls, messages), answer = null, rounds = rounds }"]
|
||||
click n7 href "rule/agent.dv#L4,12" "打开源码"
|
||||
n0 --> n2
|
||||
n2 -->|"匹配"| n3
|
||||
n3 --> n4
|
||||
n4 --> n1
|
||||
n2 -->|"匹配"| n5
|
||||
n5 --> n6
|
||||
n6 --> n7
|
||||
n7 --> n1
|
||||
```
|
||||
|
||||
- 子流程 [assistant.agent.app.executeTools](<link.md#flow-7812dd91228edaecfeb6fa6d35bf912008c8dadfed8d620ae1d188a1ca37f240>)
|
||||
|
||||
<a id="flow-fc4b94463ebbc65f8e5e9bc05b18295ec6aa43e7fa8ebec24a4becba0f05955a"></a>
|
||||
|
||||
## assistant.agent.app.calculate
|
||||
|
||||
```dever
|
||||
rule assistant.agent.app.calculate(name: ToolName, args: Arguments) (value: Int)
|
||||
```
|
||||
|
||||
根据工具名选择加法或乘法。参数类型和范围由 Port 返回边界保证,这里只做计算。
|
||||
|
||||
[打开源码](<rule/agent.dv#L7,1>)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
n0(["assistant.agent.app.calculate"])
|
||||
click n0 href "rule/agent.dv#L7,1" "打开源码"
|
||||
n1(["返回结果"])
|
||||
n2{"按输入选择唯一规则行"}
|
||||
click n2 href "rule/agent.dv#L7,1" "打开源码"
|
||||
n3["name: Add"]
|
||||
click n3 href "rule/agent.dv#L9,3" "打开源码"
|
||||
n4["value = args.a + args.b"]
|
||||
click n4 href "rule/agent.dv#L9,10" "打开源码"
|
||||
n5["name: Multiply"]
|
||||
click n5 href "rule/agent.dv#L10,3" "打开源码"
|
||||
n6["value = args.a * args.b"]
|
||||
click n6 href "rule/agent.dv#L10,15" "打开源码"
|
||||
n0 --> n2
|
||||
n2 -->|"匹配"| n3
|
||||
n3 --> n4
|
||||
n4 --> n1
|
||||
n2 -->|"匹配"| n5
|
||||
n5 --> n6
|
||||
n6 --> n1
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
loop think(question: Text, max_rounds: Int >= 1 and <= 10) (state: State)
|
||||
index in range(0, max_rounds) {
|
||||
foldUntil(current = app.start(question), app.step(index, current))
|
||||
}
|
||||
|
||||
loop executeTools(calls: List<ToolCall>, messages: List<Message>) (next: List<Message>)
|
||||
call in calls {
|
||||
fold(current = messages, app.executeTool(call, current))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
chat(messages: List<app.Message>) (turn: app.Turn) fails dever.process.Error
|
||||
@@ -0,0 +1,11 @@
|
||||
rule respond(finished: Bool, turn: Turn, messages: List<Message>, rounds: Int) (state: State)
|
||||
finished {
|
||||
true => State { messages = messages, answer = turn.content, rounds = rounds }
|
||||
false => State { messages = app.executeTools(turn.calls, messages), answer = null, rounds = rounds }
|
||||
}
|
||||
|
||||
rule calculate(name: ToolName, args: Arguments) (value: Int)
|
||||
name {
|
||||
Add => args.a + args.b
|
||||
Multiply => args.a * args.b
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
flow toolRoundtrip() ()
|
||||
| step | kind | do |
|
||||
| reply | flow | app.ask("计算 12 + 30", 4) |
|
||||
| - | calc | assertEq(reply.answer, "42") |
|
||||
| - | calc | assertEq(reply.rounds, 2) |
|
||||
|
||||
port.chat(messages: List<app.Message>) (turn: app.Turn) {
|
||||
turn = next(length(messages), messages)
|
||||
}
|
||||
|
||||
rule next(count: Int, messages: List<app.Message>) (turn: app.Turn)
|
||||
| count | messages | turn |
|
||||
| <= 2 | - | app.Turn {
|
||||
content = ""
|
||||
calls = [app.ToolCall { name = app.ToolName.Add, arguments = app.Arguments { a = 12, b = 30 } }]
|
||||
} |
|
||||
| > 2 | - | finished(messages) |
|
||||
|
||||
finished(messages: List<app.Message>) (turn: app.Turn) {
|
||||
last = get(messages, length(messages) - 1)
|
||||
check last != null else dever.process.Error.Failed("missing tool message")
|
||||
assertEq(last.role, "tool")
|
||||
assertEq(last.tool_name, app.ToolName.Add)
|
||||
turn = app.Turn { content = last.content, calls = [] }
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { chat } from '../../module/assistant/agent/javascript/chat.mjs';
|
||||
|
||||
const setting = { endpoint: 'https://example.invalid/api/chat', model: 'test-model', timeout_ms: 1000 };
|
||||
const call = { name: 'Add', arguments: { a: 12, b: 30 } };
|
||||
|
||||
test('发送工具历史并转换模型调用,不把供应商 index 传入 Dever', async t => {
|
||||
let sent;
|
||||
t.mock.method(globalThis, 'fetch', async (url, request) => {
|
||||
assert.equal(url, setting.endpoint);
|
||||
sent = JSON.parse(request.body);
|
||||
return Response.json({ message: { content: '', tool_calls: [{ function: { index: 0, ...call } }] } });
|
||||
});
|
||||
const messages = [
|
||||
{ role: 'user', content: '计算', calls: [], tool_name: null },
|
||||
{ role: 'assistant', content: '', calls: [call], tool_name: null },
|
||||
{ role: 'tool', content: '42', calls: [], tool_name: 'Add' }
|
||||
];
|
||||
const output = await chat({ messages }, setting, new AbortController().signal);
|
||||
assert.deepEqual(output, { turn: { content: '', calls: [call] } });
|
||||
assert.deepEqual(sent.messages, [
|
||||
{ role: 'user', content: '计算' },
|
||||
{ role: 'assistant', content: '', tool_calls: [{ function: call }] },
|
||||
{ role: 'tool', content: '42', tool_name: 'Add' }
|
||||
]);
|
||||
assert.deepEqual(sent.tools.map(tool => tool.function.name), ['Add', 'Multiply']);
|
||||
assert.equal(sent.stream, false);
|
||||
assert.equal(sent.model, setting.model);
|
||||
});
|
||||
|
||||
test('普通回答允许省略 tool_calls', async t => {
|
||||
t.mock.method(globalThis, 'fetch', async () => Response.json({ message: { content: '答案是 84。' } }));
|
||||
assert.deepEqual(await chat({ messages: [] }, setting, new AbortController().signal), {
|
||||
turn: { content: '答案是 84。', calls: [] }
|
||||
});
|
||||
});
|
||||
|
||||
test('HTTP 失败向上传播', async t => {
|
||||
t.mock.method(globalThis, 'fetch', async () => new Response('', { status: 503 }));
|
||||
await assert.rejects(chat({ messages: [] }, setting, new AbortController().signal), /Ollama HTTP 503/);
|
||||
});
|
||||
|
||||
test('取消信号到达 fetch,不产生成功结果', async t => {
|
||||
t.mock.method(globalThis, 'fetch', async (_url, request) => {
|
||||
request.signal.throwIfAborted();
|
||||
assert.fail('已取消的请求不应继续');
|
||||
});
|
||||
const controller = new AbortController();
|
||||
controller.abort(new Error('test cancellation'));
|
||||
await assert.rejects(chat({ messages: [] }, setting, controller.signal), /test cancellation/);
|
||||
});
|
||||
|
||||
test('损坏的 tool_calls 不当成没有工具', async t => {
|
||||
t.mock.method(globalThis, 'fetch', async () => Response.json({ message: { content: '', tool_calls: null } }));
|
||||
await assert.rejects(chat({ messages: [] }, setting, new AbortController().signal), TypeError);
|
||||
});
|
||||
Reference in new issue
Block a user