Repository navigation
Expand file tree
/
Copy pathnative-agent-tools.js
More file actions
117 lines (113 loc) · 10.2 KB
/
Copy pathnative-agent-tools.js
File metadata and controls
117 lines (113 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Shared tool declarations for the native Agent and JSON protocol checks.
// Business effects are owned by AgentService adapters, not these schemas.
const object = (properties, required = []) => ({ type: 'object', properties, required, additionalProperties: false })
const string = (description) => ({ type: 'string', description })
export const nativeAgentToolSchemas = [
{ name: 'list_notebooks', description: '列出当前笔记库的笔记本。', parameters: object({}) },
{
name: 'update_notebook',
description: '修改笔记本的名称、描述、关键词或 Agent 维护规则。动手前先阅读 list_notebooks 返回的 agentRules 与 manualRules;两者冲突时以 manualRules 为准。严禁修改 manualRules(仅人工维护、只读,写入会被宿主拒绝),也严禁用改写笔记的方式绕过它的限制。',
parameters: object({
notebook: string('笔记本名称'),
nextName: string('可选的新名称'),
description: string('可选的新描述'),
keywords: { type: 'array', items: { type: 'string' }, description: '可选的新关键词列表' },
agentRules: string('可选,Agent 自动维护规则的完整新内容'),
}, ['notebook']),
},
{ name: 'list_notes', description: '列出某个笔记本里的笔记。', parameters: object({ notebook: string('笔记本名称') }, ['notebook']) },
{ name: 'read_note', description: '读取一篇笔记的 Markdown 内容。', parameters: object({ path: string('相对根目录的笔记路径') }, ['path']) },
{ name: 'read_todo', description: '读取待办列表。', parameters: object({}) },
{ name: 'read_stickies', description: '读取便笺。', parameters: object({}) },
{ name: 'read_settings', description: '读取随记设置。', parameters: object({}) },
{ name: 'web_fetch', description: '获取公开网页的正文文本。', parameters: object({ url: string('http 或 https URL') }, ['url']) },
{ name: 'create_note', description: '在指定笔记本新建笔记。', parameters: object({ notebook: string('笔记本名称'), title: string('笔记标题'), body: string('Markdown 正文') }, ['notebook', 'title', 'body']) },
{ name: 'write_note', description: '写入已有笔记的完整内容。', parameters: object({ path: string('笔记路径'), content: string('完整 Markdown') }, ['path', 'content']) },
{ name: 'rename_note', description: '重命名笔记。', parameters: object({ path: string('笔记路径'), title: string('新标题') }, ['path', 'title']) },
{ name: 'move_note', description: '移动笔记到另一个笔记本。', parameters: object({ path: string('笔记路径'), notebook: string('目标笔记本'), title: string('可选新标题') }, ['path', 'notebook']) },
{ name: 'delete_note', description: '删除单篇笔记,默认可直接执行;一次删除多篇需要确认。', parameters: object({ path: string('笔记路径') }, ['path']) },
{ name: 'reorder_notes', description: '调整某个笔记本里笔记的顺序。paths 必须是该笔记本内全部笔记按新顺序排列的相对路径。', parameters: object({ notebook: string('笔记本名称'), paths: { type: 'array', items: { type: 'string' }, description: '按新顺序排列的全部笔记路径' } }, ['notebook', 'paths']) },
{ name: 'reorder_notebooks', description: '调整笔记本顺序。names 必须是全部笔记本按新顺序排列的名称。', parameters: object({ names: { type: 'array', items: { type: 'string' }, description: '按新顺序排列的全部笔记本名' } }, ['names']) },
{ name: 'update_frontmatter', description: '更新笔记的 tags、summary 等 frontmatter。', parameters: object({ path: string('笔记路径'), set: { type: 'object' }, unset: { type: 'array', items: { type: 'string' } } }, ['path']) },
{ name: 'todo_add', description: '新增待办。', parameters: object({ text: string('待办内容'), tags: { type: 'array', items: { type: 'string' } } }, ['text']) },
{ name: 'todo_update', description: '更新待办。', parameters: object({ id: string('待办 ID'), text: string('新内容'), tags: { type: 'array', items: { type: 'string' } }, done: { type: 'boolean' } }, ['id']) },
{ name: 'todo_delete', description: '删除待办。', parameters: object({ id: string('待办 ID') }, ['id']) },
{ name: 'memo_create', description: '新建便笺。', parameters: object({ items: { type: 'array', items: { type: 'string' } }, color: string('颜色') }, ['items']) },
{ name: 'memo_update', description: '更新便笺。', parameters: object({ id: string('便笺 ID'), items: { type: 'array', items: { type: 'string' } }, color: string('颜色') }, ['id']) },
{ name: 'memo_delete', description: '删除便笺。', parameters: object({ id: string('便笺 ID') }, ['id']) },
{ name: 'settings_update', description: '修改随记设置。', parameters: object({ set: { type: 'object' } }, ['set']) },
{ name: 'set_privacy', description: '开关首页隐私模式。', parameters: object({ enabled: { type: 'boolean' } }, ['enabled']) },
{
name: 'stage_operations',
description: '暂存一组操作;仅长笔记、批量删除/移动需要用户确认。',
parameters: object({
operations: {
type: 'array',
items: object({
op: { type: 'string', enum: ['write', 'create', 'rename', 'move', 'delete', 'frontmatter', 'todoAdd', 'todoUpdate', 'todoDelete', 'memoCreate', 'memoUpdate', 'memoDelete', 'settingsUpdate', 'setPrivacyMode', 'reorderNotes', 'reorderNotebooks', 'updateNotebook', 'renameNotebook', 'deleteNotebook'] },
path: string('相对路径,适用于 write/rename/move/delete/frontmatter'),
content: string('完整内容,适用于 write'),
notebook: string('笔记本名称,适用于 create/move/reorderNotes/updateNotebook'),
paths: { type: 'array', items: { type: 'string' }, description: '按新顺序排列的笔记路径,适用于 reorderNotes' },
names: { type: 'array', items: { type: 'string' }, description: '按新顺序排列的笔记本名,适用于 reorderNotebooks' },
title: string('标题,适用于 create/rename/move'),
body: string('正文,适用于 create'),
id: string('待办或便笺 ID'),
text: string('待办文本'),
tags: { type: 'array', items: { type: 'string' } },
done: { type: 'boolean' },
items: { type: 'array', items: { type: 'string' } },
color: string('便笺颜色'),
set: { type: 'object' },
unset: { type: 'array', items: { type: 'string' } },
}, ['op']),
},
}, ['operations']),
},
{ name: 'apply_operations', description: '提交一组操作;服务端会按统一规则决定是否直接执行或转待确认。', parameters: object({ operations: { type: 'array', items: { type: 'object' } } }, ['operations']) },
{ name: 'create_surface', description: '初始化或更新当前事件唯一的提示卡片,返回 surfaceId;后续进展、问题均复用此 ID。', parameters: object({ title: string('简短任务标题,无需固定状态前缀'), body: string('正文'), size: { type: 'string', enum: ['small', 'large'] }, kind: string('progress/question/confirm'), options: { type: 'array', items: { type: 'object' } }, allowFreeText: { type: 'boolean' }, awaitResponse: { type: 'boolean', description: '需要用户回答时设为 true,结束本轮等待用户后续回复' } }, ['title', 'body']) },
{ name: 'update_surface', description: '更新当前事件的提示内容;有效更新重新显示被隐藏的卡片。需要回答时设 awaitResponse。', parameters: object({ surfaceId: string('当前事件的提示 ID'), title: string('任务标题'), body: string('正文'), size: { type: 'string', enum: ['small', 'large'] }, options: { type: 'array', items: { type: 'object' } }, kind: string('progress/question/confirm'), allowFreeText: { type: 'boolean' }, awaitResponse: { type: 'boolean' } }, ['surfaceId']) },
{ name: 'close_surface', description: '暂时隐藏当前执行提示,不取消任务;下一次有效内容更新重新显示。', parameters: object({ surfaceId: string('surface ID') }, ['surfaceId']) },
{ name: 'finish', description: '结束当前轮次并给用户结果。', parameters: object({ reply: string('给用户看的结果') }, ['reply']) },
]
export const nativeAgentToolNames = nativeAgentToolSchemas.map((tool) => tool.name)
export function createNativeToolExecutor(service) {
const call = (name, args) => service[name](args)
return async ({ name, args }) => {
const handlers = {
list_notebooks: () => call('listNotebooks', args),
list_notes: () => call('listNotes', args),
update_notebook: () => call('updateNotebook', args),
read_note: () => call('readNote', args),
read_todo: () => call('readTodo', args),
read_stickies: () => call('readStickies', args),
read_settings: () => call('readSettings', args),
web_fetch: () => call('webFetch', args),
create_note: () => call('createNote', args),
write_note: () => call('writeNote', args),
rename_note: () => call('renameNote', args),
move_note: () => call('moveNote', args),
delete_note: () => call('deleteNote', args),
reorder_notes: () => call('reorderNotes', args),
reorder_notebooks: () => call('reorderNotebooks', args),
update_frontmatter: () => call('updateFrontmatter', args),
todo_add: () => call('todoAdd', args),
todo_update: () => call('todoUpdate', args),
todo_delete: () => call('todoDelete', args),
memo_create: () => call('memoCreate', args),
memo_update: () => call('memoUpdate', args),
memo_delete: () => call('memoDelete', args),
settings_update: () => call('settingsUpdate', args),
set_privacy: () => call('setPrivacy', args),
stage_operations: () => call('stageOperations', args),
apply_operations: () => call('applyOperations', args),
create_surface: () => call('createSurface', args),
update_surface: () => call('updateSurface', args),
close_surface: () => call('closeSurface', args),
}
const handler = handlers[name]
if (!handler) throw new Error(`未知工具:${name}`)
const result = await handler()
return result === undefined ? { ok: true } : result
}
}