Skip to main content

JSON 模版格式规范

TGO 插件系统使用 JSON 模版描述 UI 内容,由宿主程序负责渲染。本文档定义所有支持的模版类型及其数据格式。

模版结构

所有模版响应遵循统一结构:

{
"template": "模版类型",
"data": {
// 模版特定数据
}
}

基础模版类型

text - 文本

显示纯文本内容。

{
"template": "text",
"data": {
"text": "这是一段文本内容",
"type": "default"
}
}
字段类型必填说明
textstring文本内容
typestring样式类型:default / info / success / warning / error
copyableboolean是否可复制

样式预览:

type效果
default普通黑色文本
info蓝色信息提示
success绿色成功提示
warning黄色警告提示
error红色错误提示

key_value - 键值对

以键值对形式展示数据,适合展示客户信息、配置项等。

{
"template": "key_value",
"data": {
"title": "客户信息",
"items": [
{
"label": "会员等级",
"value": "黄金会员",
"icon": "crown",
"color": "#FFD700"
},
{
"label": "累计消费",
"value": "¥12,580.00"
},
{
"label": "注册时间",
"value": "2023-06-15",
"type": "date"
},
{
"label": "联系电话",
"value": "138****1234",
"copyable": true
}
]
}
}
字段类型必填说明
titlestring区块标题
itemsarray键值对列表
items[].labelstring标签名
items[].valuestring
items[].iconstring图标名
items[].colorstring值的颜色(十六进制)
items[].typestring值类型:text / date / datetime / currency / badge
items[].copyableboolean是否可复制
items[].linkstring点击跳转链接

table - 表格

表格形式展示数据,适合订单列表、操作记录等。

{
"template": "table",
"data": {
"title": "最近订单",
"columns": [
{"key": "order_id", "label": "订单号", "width": 120},
{"key": "product", "label": "商品"},
{"key": "amount", "label": "金额", "align": "right"},
{"key": "status", "label": "状态", "type": "badge"}
],
"rows": [
{
"order_id": "ORD-2024001",
"product": "商品A",
"amount": "¥299.00",
"status": {"text": "已完成", "color": "green"}
},
{
"order_id": "ORD-2024002",
"product": "商品B",
"amount": "¥599.00",
"status": {"text": "待发货", "color": "orange"}
}
],
"pagination": {
"total": 15,
"page": 1,
"page_size": 5
},
"row_click_action": "show_detail"
}
}

columns 字段说明:

字段类型必填说明
keystring数据字段名
labelstring列标题
widthnumber列宽度(像素)
alignstring对齐:left / center / right
typestring类型:text / badge / link / date / datetime

rows 字段说明:

每行是一个对象,key 对应 columns 中定义的 key。当 column type 为 badge 时,值可以是对象:

{"text": "已完成", "color": "green"}

支持的颜色:green / orange / red / blue / gray


card - 卡片

卡片形式展示,适合商品信息、用户资料等。

{
"template": "card",
"data": {
"title": "商品详情",
"image": "https://example.com/product.jpg",
"fields": [
{"label": "商品名称", "value": "无线蓝牙耳机"},
{"label": "价格", "value": "¥299.00", "highlight": true},
{"label": "库存", "value": "128 件"}
],
"tags": [
{"text": "热销", "color": "red"},
{"text": "新品", "color": "blue"}
],
"actions": [
{"id": "view", "label": "查看详情", "type": "primary"},
{"id": "copy_link", "label": "复制链接", "type": "default"}
]
}
}
字段类型必填说明
titlestring卡片标题
subtitlestring副标题
imagestring图片 URL
image_sizestring图片尺寸:small / medium / large
fieldsarray字段列表
tagsarray标签列表
actionsarray操作按钮
footerstring底部文本

cards - 多卡片列表

展示多个卡片,适合商品推荐、搜索结果等。

{
"template": "cards",
"data": {
"title": "相关商品",
"layout": "grid",
"columns": 2,
"items": [
{
"id": "p1",
"title": "商品A",
"image": "https://...",
"description": "商品描述...",
"price": "¥199"
},
{
"id": "p2",
"title": "商品B",
"image": "https://...",
"description": "商品描述...",
"price": "¥299"
}
],
"item_click_action": "show_product"
}
}
字段类型必填说明
layoutstring布局:grid / list
columnsnumbergrid 布局的列数
itemsarray卡片列表
item_click_actionstring点击卡片的动作 ID

显示可点击的链接。

{
"template": "link",
"data": {
"text": "查看订单详情",
"url": "https://crm.example.com/orders/12345",
"icon": "external-link",
"target": "_blank"
}
}
字段类型必填说明
textstring链接文本
urlstring链接地址
iconstring图标
targetstring打开方式:_blank / _self
descriptionstring链接描述

显示多个链接。

{
"template": "links",
"data": {
"title": "快捷入口",
"items": [
{"text": "订单管理", "url": "https://...", "icon": "package"},
{"text": "客户档案", "url": "https://...", "icon": "user"},
{"text": "数据报表", "url": "https://...", "icon": "bar-chart"}
]
}
}

button - 按钮

显示单个操作按钮。

{
"template": "button",
"data": {
"label": "点击操作",
"action_id": "btn_click",
"type": "primary",
"size": "md",
"icon": "plus"
}
}
字段类型必填说明
labelstring按钮文本
action_idstring动作 ID
typestring样式:primary / secondary / danger / link
sizestring尺寸:xs / sm / md / lg
iconstring图标
disabledboolean是否禁用

action - 操作按钮组

显示多个操作按钮,点击触发插件动作。

{
"template": "action",
"data": {
"buttons": [
{
"id": "create_ticket",
"label": "创建工单",
"icon": "plus",
"type": "primary",
"size": "sm"
},
{
"id": "sync_crm",
"label": "同步到 CRM",
"icon": "refresh-cw",
"type": "default"
}
]
}
}
字段类型必填说明
idstring动作 ID,触发时回传
labelstring按钮文本
iconstring图标
typestring样式:primary / default / danger / ghost
sizestring尺寸:xs / sm / md / lg
disabledboolean是否禁用
confirmstring点击确认提示文本
loadingboolean是否显示加载状态

当按钮被点击时,宿主会发送请求:

{
"method": "visitor_panel/action",
"params": {
"action_id": "create_ticket",
"visitor_id": "v_abc123",
"session_id": "s_xyz789"
}
}

markdown - Markdown 内容

渲染 Markdown 格式内容。

{
"template": "markdown",
"data": {
"content": "## 帮助文档\n\n这是一段 **Markdown** 内容。\n\n- 列表项 1\n- 列表项 2\n\n```javascript\nconsole.log('Hello');\n```"
}
}
字段类型必填说明
contentstringMarkdown 内容
max_heightnumber最大高度(像素),超出显示滚动条

list - 列表

简单列表展示。

{
"template": "list",
"data": {
"title": "操作记录",
"items": [
{
"title": "创建了工单",
"description": "工单号: TK-001",
"time": "2024-12-27 14:30",
"icon": "plus"
},
{
"title": "发送了消息",
"description": "您好,请问有什么可以帮您?",
"time": "2024-12-27 14:25",
"icon": "message-circle"
}
],
"show_more": true,
"more_action": "load_more"
}
}

timeline - 时间线

时间轴形式展示历史记录。

{
"template": "timeline",
"data": {
"title": "服务记录",
"items": [
{
"time": "2024-12-27 14:30",
"title": "会话开始",
"description": "访客发起咨询",
"status": "success"
},
{
"time": "2024-12-27 14:32",
"title": "智能体接待",
"description": "AI 助手自动回复",
"status": "info"
},
{
"time": "2024-12-27 14:35",
"title": "转接人工",
"description": "转接至客服: 张三",
"status": "warning"
}
]
}
}
status效果
success绿色节点
info蓝色节点
warning黄色节点
error红色节点
default灰色节点

stats - 统计数据

展示统计数据卡片。

{
"template": "stats",
"data": {
"items": [
{
"label": "历史会话",
"value": "23",
"icon": "message-square",
"trend": "+5",
"trend_type": "up"
},
{
"label": "累计消费",
"value": "¥12,580",
"icon": "dollar-sign"
},
{
"label": "满意度",
"value": "98%",
"icon": "smile",
"color": "green"
}
]
}
}

empty - 空状态

当没有数据时显示的占位内容。

{
"template": "empty",
"data": {
"icon": "inbox",
"title": "暂无订单",
"description": "该访客还没有下过订单",
"action": {
"id": "create_order",
"label": "创建订单"
}
}
}

loading - 加载状态

显示加载中状态。

{
"template": "loading",
"data": {
"text": "正在加载订单信息..."
}
}

组合模版

group - 分组容器

将多个模版组合在一起。

{
"template": "group",
"data": {
"items": [
{
"template": "stats",
"data": {
"items": [
{"label": "会话数", "value": "23"},
{"label": "满意度", "value": "98%"}
]
}
},
{
"template": "key_value",
"data": {
"title": "基本信息",
"items": [
{"label": "姓名", "value": "张三"},
{"label": "电话", "value": "138****1234"}
]
}
},
{
"template": "table",
"data": {
"title": "最近订单",
"columns": [...],
"rows": [...]
}
}
]
}
}

tabs - 标签页

多标签页容器。

{
"template": "tabs",
"data": {
"items": [
{
"key": "orders",
"label": "订单",
"icon": "package",
"content": {
"template": "table",
"data": {...}
}
},
{
"key": "records",
"label": "服务记录",
"icon": "clock",
"content": {
"template": "timeline",
"data": {...}
}
}
],
"default_tab": "orders"
}
}

表单模版

用于聊天工具栏弹窗。

form - 表单

{
"template": "form",
"data": {
"title": "创建工单",
"fields": [
{
"name": "title",
"label": "工单标题",
"type": "text",
"required": true,
"placeholder": "请输入工单标题"
},
{
"name": "priority",
"label": "优先级",
"type": "select",
"options": [
{"value": "low", "label": "低"},
{"value": "medium", "label": "中"},
{"value": "high", "label": "高"}
],
"default": "medium"
},
{
"name": "category",
"label": "分类",
"type": "radio",
"options": [
{"value": "bug", "label": "Bug"},
{"value": "feature", "label": "功能需求"},
{"value": "question", "label": "咨询"}
]
},
{
"name": "tags",
"label": "标签",
"type": "checkbox",
"options": [
{"value": "urgent", "label": "紧急"},
{"value": "vip", "label": "VIP客户"}
]
},
{
"name": "description",
"label": "描述",
"type": "textarea",
"rows": 4
}
],
"submit_text": "提交",
"cancel_text": "取消"
}
}

表单字段类型:

type说明
text单行文本
textarea多行文本
number数字输入
select下拉选择
radio单选按钮
checkbox多选框
date日期选择
datetime日期时间
switch开关
hidden隐藏字段

最佳实践

1. 优雅降级

当数据不可用时,返回友好的空状态:

{
"template": "empty",
"data": {
"title": "暂无数据",
"description": "系统暂时无法获取信息,请稍后重试"
}
}

2. 加载状态

长时间操作时先返回加载状态:

{
"template": "loading",
"data": {
"text": "正在查询..."
}
}

3. 分页处理

大量数据使用分页:

{
"template": "table",
"data": {
"rows": [...],
"pagination": {
"total": 100,
"page": 1,
"page_size": 10
}
}
}

4. 操作反馈

按钮操作后返回结果提示:

{
"template": "text",
"data": {
"text": "工单创建成功!工单号: TK-12345",
"type": "success"
}
}

下一步