🐛 fix(agent/schedulerefine): 修复复合微调分支链路问题,并将 MinContextSwitch 重构为固定坑位重排语义 - 🔧 修复 `schedulerefine` 复合路由中参数透传不完整、缺少 deterministic objective 时错误降级,以及“复合工具执行成功”与“终审通过”语义混淆的问题 - ✅ 保证新的独立复合分支能够正确执行、正确出站,并统一交由 `hard_check` 裁决最终结果 - 🔍 排查时发现 `MinContextSwitch` 上游 `context_tag` 存在整体退化为 `General` 的风险,影响MinContextSwitch - 🛡️ 为 `MinContextSwitch` 增加兜底策略:当标签整体退化时,按任务名关键词推断学科分组,避免分组能力失效 - ♻️ 将 `MinContextSwitch` 从“整周重新寻找新坑位”调整为“坑位不变,任务顺序改变” - 🎯 将落地方式从顺序 `BatchMove` 改为固定坑位原子重写,避免出现远距离跳位、跨天错迁、异常嵌入课位及循环换位冲突 - 🧹 修复 `hard_check` 在 `MinContextSwitch` 成功后仍执行 `origin_rank` 顺序归位、并导致逆序终审误判的问题 - 🚦 命中该分支后跳过顺序归位与顺序硬校验,避免 `summary` / `hard_check` 将有效重排结果误判为失败 📈 当前连续微调规划涉及的全部功能已可以稳定运行;下一步将继续扩展能力边界,并进一步优化 `schedule_plan` 流程 ♻️ refactor: 重整 agent2 架构,并迁移 quicknote/chat 新链路,目前还剩3个模块未迁移,后续迁移完成后会删除原agent并将此目录命名为agent - 🏗️ 明确 `agent2` 采用“统一分层目录 + 文件分层 + 依赖注入”的重构方案,不再沿用模块目录多层嵌套结构 - 🧩 完善 `agent2` 基础骨架,统一收口 `entrance` / `router` / `llm` / `stream` / `shared` / `model` / `prompt` / `node` / `graph` 等层级职责 - 🚚 将通用路由能力迁移至 `agent2/router`,沉淀统一的 `Action`、`RoutingDecision`、控制码解析,以及 `Dispatcher` / `Resolver` 抽象 - 💬 将普通聊天链路迁移至 `agent2/chat`,复用 `stream` 的 OpenAI 兼容输出协议与 LLM usage 聚合能力 - 📝 将 `quicknote` 链路迁移到 `agent2` 新结构,拆分为 `model` / `prompt` / `llm` / `node` / `graph` 多层实现,替换对旧 `agent/quicknote` 的直接依赖 - 🔌 调整 `agentsvc` 对 `agent2` 的引用,普通聊天、通用分流与 `quicknote` 全部切换到新链路 - ✂️ 去除 graph 内部 `runner` 转接层,改为由 node 层直接持有请求级依赖,并向 graph 暴露节点方法 - 🧹 合并 `graph/quicknote` 与 `graph/quicknote_run`,删除冗余骨架文件,收敛为单一 `quicknote graph` 文件 - 📚 新增 `agent2`《通用能力接入文档》,明确公共能力边界、接入方式以及 graph/node 协作约定 - 📝 更新 `AGENTS.md`,要求后续扩展 `agent2` 通用能力时必须同步维护接入文档 ♻️ refactor: 删除了现Agent目录内Chat模块的两条冗余Prompt
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package agentllm
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
)
|
||
|
||
// ParseJSONObject 解析模型返回中的 JSON 对象。
|
||
//
|
||
// 职责边界:
|
||
// 1. 负责处理“模型输出前后夹杂解释文字 / markdown 代码块”的常见情况;
|
||
// 2. 负责提取最外层 JSON object 并反序列化为目标结构;
|
||
// 3. 不负责业务字段合法性校验,例如 priority 是否在 1~4,应由上层 node 再校验。
|
||
func ParseJSONObject[T any](raw string) (*T, error) {
|
||
clean := strings.TrimSpace(raw)
|
||
if clean == "" {
|
||
return nil, errors.New("模型返回为空,无法解析 JSON")
|
||
}
|
||
|
||
objectText := ExtractJSONObject(clean)
|
||
if objectText == "" {
|
||
return nil, fmt.Errorf("模型返回中未找到 JSON 对象: %s", truncateForError(clean))
|
||
}
|
||
|
||
var out T
|
||
if err := json.Unmarshal([]byte(objectText), &out); err != nil {
|
||
return nil, fmt.Errorf("JSON 解析失败: %w", err)
|
||
}
|
||
return &out, nil
|
||
}
|
||
|
||
// ExtractJSONObject 从混合文本里提取第一个完整 JSON 对象。
|
||
//
|
||
// 设计说明:
|
||
// 1. LLM 很容易输出“这里是结果:{...}”这种半结构化文本;
|
||
// 2. 这里用括号计数而不是正则,避免嵌套对象一多就误截断;
|
||
// 3. 目前只提取 object,不提取 array,因为当前 agent 的路由/规划契约基本都是对象。
|
||
func ExtractJSONObject(text string) string {
|
||
clean := trimMarkdownCodeFence(strings.TrimSpace(text))
|
||
if clean == "" {
|
||
return ""
|
||
}
|
||
|
||
start := strings.Index(clean, "{")
|
||
if start < 0 {
|
||
return ""
|
||
}
|
||
|
||
depth := 0
|
||
inString := false
|
||
escaped := false
|
||
for idx := start; idx < len(clean); idx++ {
|
||
ch := clean[idx]
|
||
|
||
if escaped {
|
||
escaped = false
|
||
continue
|
||
}
|
||
if ch == '\\' && inString {
|
||
escaped = true
|
||
continue
|
||
}
|
||
if ch == '"' {
|
||
inString = !inString
|
||
continue
|
||
}
|
||
if inString {
|
||
continue
|
||
}
|
||
|
||
switch ch {
|
||
case '{':
|
||
depth++
|
||
case '}':
|
||
depth--
|
||
if depth == 0 {
|
||
return clean[start : idx+1]
|
||
}
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func trimMarkdownCodeFence(text string) string {
|
||
trimmed := strings.TrimSpace(text)
|
||
if !strings.HasPrefix(trimmed, "```") {
|
||
return trimmed
|
||
}
|
||
|
||
lines := strings.Split(trimmed, "\n")
|
||
if len(lines) == 0 {
|
||
return trimmed
|
||
}
|
||
|
||
// 1. 去掉首行 ```json / ```;
|
||
// 2. 若末行是 ```,一并去掉;
|
||
// 3. 中间正文保持原样,避免破坏 JSON 的换行结构。
|
||
body := lines[1:]
|
||
if len(body) > 0 && strings.TrimSpace(body[len(body)-1]) == "```" {
|
||
body = body[:len(body)-1]
|
||
}
|
||
return strings.TrimSpace(strings.Join(body, "\n"))
|
||
}
|
||
|
||
func truncateForError(text string) string {
|
||
if len(text) <= 160 {
|
||
return text
|
||
}
|
||
return text[:160] + "..."
|
||
}
|