Commit b2232985 authored by yuguo's avatar yuguo

feat(navigate-tool): add embeddable flag, return embed action for inline views

Co-Authored-By: default avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 3025da6e
This diff is collapsed.
package model
import "time"
// RouteEntry 前端路由注册表(供 AI 导航工具查询)
type RouteEntry struct {
ID uint `gorm:"primaryKey" json:"id"`
PageCode string `gorm:"type:varchar(100);uniqueIndex" json:"page_code"`
PageName string `gorm:"type:varchar(200)" json:"page_name"`
Module string `gorm:"type:varchar(50)" json:"module"` // admin/patient/doctor
Route string `gorm:"type:varchar(200)" json:"route"`
EditRoute string `gorm:"type:varchar(200)" json:"edit_route"`
AddRoute string `gorm:"type:varchar(200)" json:"add_route"`
Operations string `gorm:"type:jsonb;default:'[]'" json:"operations"` // ["view","create","edit","delete"]
RoleAccess string `gorm:"type:varchar(100)" json:"role_access"` // "admin"
Description string `gorm:"type:text" json:"description"`
Status string `gorm:"type:varchar(20);default:'active'" json:"status"`
SortOrder int `gorm:"default:0" json:"sort_order"`
Embeddable bool `gorm:"default:false" json:"embeddable"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
package tools
import (
"context"
"encoding/json"
"fmt"
"internet-hospital/internal/model"
agentpkg "internet-hospital/pkg/agent"
"internet-hospital/pkg/database"
)
// NavigateTool 根据用户意图生成页面导航指令
type NavigateTool struct{}
func (t *NavigateTool) Name() string { return "navigate" }
func (t *NavigateTool) Description() string {
return `根据用户意图导航到指定页面,前端会渲染为可点击的导航卡片。
常用 page_code:
- doctor_management: 医生管理(添加/编辑/查看医生)
- patient_management: 患者管理
- department_management: 科室管理
- pharmacy_management: 药品库管理
- consultation_management: 问诊管理
- prescription_regulation: 处方监管
- agent_management: Agent管理
- tool_center: 工具中心
- workflow_management: 工作流
- knowledge_base: 知识库
- dashboard: 运营大盘`
}
func (t *NavigateTool) Parameters() []agentpkg.ToolParameter {
return []agentpkg.ToolParameter{
{Name: "page_code", Type: "string", Description: "目标页面代码,如: doctor_management, patient_management, pharmacy_management", Required: true},
{Name: "operation", Type: "string", Description: "操作类型: view_list(查看列表), open_add(打开新增), open_edit(打开编辑)", Required: false, Enum: []string{"view_list", "open_edit", "open_add"}},
{Name: "record_id", Type: "string", Description: "编辑/查看的记录ID(operation 为 open_edit 时需要)", Required: false},
}
}
func (t *NavigateTool) Execute(_ context.Context, params map[string]interface{}) (interface{}, error) {
pageCode, _ := params["page_code"].(string)
if pageCode == "" {
return nil, fmt.Errorf("page_code 必填")
}
operation, _ := params["operation"].(string)
if operation == "" {
operation = "view_list"
}
recordID, _ := params["record_id"].(string)
db := database.GetDB()
if db == nil {
return nil, fmt.Errorf("数据库未初始化")
}
var entry model.RouteEntry
if err := db.Where("page_code = ? AND status = 'active'", pageCode).First(&entry).Error; err != nil {
return nil, fmt.Errorf("页面 %s 不存在或未启用", pageCode)
}
// 构建实际路由(始终以列表页路由为基础,通过 query param 传递操作意图)
route := entry.Route
actionParams := map[string]interface{}{}
switch operation {
case "open_edit":
if recordID != "" {
route = route + "?action=edit&id=" + recordID
actionParams["action"] = "edit"
actionParams["id"] = recordID
} else if entry.EditRoute != "" {
route = entry.EditRoute
}
case "open_add":
route = route + "?action=add"
actionParams["action"] = "add"
if recordID != "" {
route = route + "&parent_id=" + recordID
actionParams["parent_id"] = recordID
}
}
// 解析支持的操作
var ops []string
json.Unmarshal([]byte(entry.Operations), &ops)
// Determine action type: embed (inline in chat) or navigate (open page)
actionType := "navigate"
if entry.Embeddable {
actionType = "embed"
}
return map[string]interface{}{
"action": actionType,
"page": entry.PageName,
"page_code": entry.PageCode,
"module": entry.Module,
"operation": operation,
"route": route,
"params": actionParams,
"operations": ops,
"description": entry.Description,
"embeddable": entry.Embeddable,
}, nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment