@cmach_socket/opencode-qq-bot 0.6.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -71
- package/package.json +1 -1
- package/src/config.ts +136 -0
- package/src/diary/collector.ts +187 -0
- package/src/diary/command.ts +277 -0
- package/src/diary/format.ts +157 -0
- package/src/diary/scheduler.ts +169 -0
- package/src/diary/store.ts +144 -0
- package/src/diary/summary.ts +380 -0
- package/src/index.ts +103 -2
- package/src/naplink/render.ts +42 -0
- package/src/tools/policy.ts +11 -3
- package/src/tools/qq.ts +224 -0
- package/tool-profiles.example.json +6 -2
package/src/index.ts
CHANGED
|
@@ -7,6 +7,11 @@ import { acquireBotLock, type BotLock, botLockPath } from "./bot-lock"
|
|
|
7
7
|
import { type ChatTarget, targetKey, targetLabel } from "./chat"
|
|
8
8
|
import { allowedTargets, isTargetAllowed } from "./access"
|
|
9
9
|
import { MIGRATION_WARNING_PREFIX, resolveConfig, type QQBotConfig } from "./config"
|
|
10
|
+
import { DiaryCollector } from "./diary/collector"
|
|
11
|
+
import { createDiaryRunner, diaryCommand } from "./diary/command"
|
|
12
|
+
import { DiaryScheduler } from "./diary/scheduler"
|
|
13
|
+
import { DiaryStore } from "./diary/store"
|
|
14
|
+
import { DiarySummarizer } from "./diary/summary"
|
|
10
15
|
import { resolveHome } from "./home"
|
|
11
16
|
import { claimInstance, takeHandover } from "./instance"
|
|
12
17
|
import { KeepAlive } from "./keepalive"
|
|
@@ -16,7 +21,13 @@ import { formatModelMessage } from "./model-view"
|
|
|
16
21
|
import { QQClient } from "./naplink/client"
|
|
17
22
|
import { createHistoryProvider } from "./naplink/history"
|
|
18
23
|
import { createMediaStore } from "./naplink/media"
|
|
19
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
describeMentions,
|
|
26
|
+
extractDiaryText,
|
|
27
|
+
renderMessage,
|
|
28
|
+
resolveUserName,
|
|
29
|
+
shouldProcess,
|
|
30
|
+
} from "./naplink/render"
|
|
20
31
|
import { SendRateLimiter } from "./rate-limit"
|
|
21
32
|
import { packageRoot, seedFile } from "./seed"
|
|
22
33
|
import { collectEnvSecrets, redactUrl, secretsFromUrl, SensitiveChecker } from "./sensitive"
|
|
@@ -406,6 +417,40 @@ export default Plugin.define({
|
|
|
406
417
|
const favoriteIndex = new FavoriteIndex(ctx.storage, logger)
|
|
407
418
|
const labeler = new EmojiLabeler({ ctx, config, home, logger, media, favorites })
|
|
408
419
|
cleanup.add("注销表情标注", () => labeler.dispose(), CleanupOrder.labeler)
|
|
420
|
+
// 日记:素材累积在 raw/ 下,成品是按天一个的 Markdown 文件。
|
|
421
|
+
// 日记目录默认落在机器人工作目录里,因此天然在出站媒体白名单内。
|
|
422
|
+
const diaryRoot = config.diary.path.trim()
|
|
423
|
+
? resolve(home.directory, config.diary.path)
|
|
424
|
+
: join(home.directory, "diary")
|
|
425
|
+
const diaryStore = new DiaryStore({ root: diaryRoot, logger })
|
|
426
|
+
const diaryCollector = new DiaryCollector({
|
|
427
|
+
root: diaryRoot,
|
|
428
|
+
logger,
|
|
429
|
+
maxLineChars: config.diary.maxLineChars,
|
|
430
|
+
maxLinesPerDay: config.diary.maxLinesPerDay,
|
|
431
|
+
})
|
|
432
|
+
const diarySummarizer = new DiarySummarizer({
|
|
433
|
+
ctx,
|
|
434
|
+
config,
|
|
435
|
+
home,
|
|
436
|
+
logger,
|
|
437
|
+
collector: diaryCollector,
|
|
438
|
+
store: diaryStore,
|
|
439
|
+
timeoutMs: config.diary.timeoutMs,
|
|
440
|
+
})
|
|
441
|
+
cleanup.add("注销日记总结", () => diarySummarizer.dispose(), CleanupOrder.labeler)
|
|
442
|
+
const diaryScheduler = new DiaryScheduler({
|
|
443
|
+
config,
|
|
444
|
+
logger,
|
|
445
|
+
client,
|
|
446
|
+
limiter,
|
|
447
|
+
outbound,
|
|
448
|
+
sensitive,
|
|
449
|
+
store: diaryStore,
|
|
450
|
+
collector: diaryCollector,
|
|
451
|
+
summarizer: diarySummarizer,
|
|
452
|
+
})
|
|
453
|
+
cleanup.add("停止日记调度", () => diaryScheduler.dispose(), CleanupOrder.events)
|
|
409
454
|
// QQ 工具只有持有机器级锁的实例才注册:工具表在进程内共享,
|
|
410
455
|
// 没锁的实例注册了会覆盖持有者的实现,模型就会调到没有连接的那一份。
|
|
411
456
|
let toolRegistration: { dispose(): Promise<void> } | undefined
|
|
@@ -424,6 +469,7 @@ export default Plugin.define({
|
|
|
424
469
|
limiter,
|
|
425
470
|
sensitive,
|
|
426
471
|
outbound,
|
|
472
|
+
diary: diaryStore,
|
|
427
473
|
tracer,
|
|
428
474
|
// 没有机器级锁的实例不能发送:那是别的实例的机器人。
|
|
429
475
|
canSend: () => botLock !== undefined,
|
|
@@ -431,7 +477,7 @@ export default Plugin.define({
|
|
|
431
477
|
toolRegistration = registration
|
|
432
478
|
cleanup.add("注销工具", () => registration.dispose(), CleanupOrder.tools)
|
|
433
479
|
}
|
|
434
|
-
// 命令表:管理命令 +
|
|
480
|
+
// 命令表:管理命令 + 表情标注 + 日记,QQ 私聊与 TUI 共用同一套实现。
|
|
435
481
|
const commands = [
|
|
436
482
|
...COMMANDS,
|
|
437
483
|
emojiCommand(
|
|
@@ -445,6 +491,19 @@ export default Plugin.define({
|
|
|
445
491
|
limiter,
|
|
446
492
|
}),
|
|
447
493
|
),
|
|
494
|
+
diaryCommand(
|
|
495
|
+
createDiaryRunner({
|
|
496
|
+
config,
|
|
497
|
+
logger,
|
|
498
|
+
client,
|
|
499
|
+
limiter,
|
|
500
|
+
outbound,
|
|
501
|
+
sensitive,
|
|
502
|
+
store: diaryStore,
|
|
503
|
+
collector: diaryCollector,
|
|
504
|
+
summarizer: diarySummarizer,
|
|
505
|
+
}),
|
|
506
|
+
),
|
|
448
507
|
]
|
|
449
508
|
// 命令结果统一走限流;发送失败只记日志,不影响命令本身。
|
|
450
509
|
const replyTo = createPlainSender(client, {
|
|
@@ -489,6 +548,11 @@ export default Plugin.define({
|
|
|
489
548
|
// 它的输入是一张图片,工具一律摘掉,只让模型输出标签。
|
|
490
549
|
if (await labeler.isLabelSession(event.sessionID)) {
|
|
491
550
|
for (const name of Object.keys(event.tools)) delete event.tools[name]
|
|
551
|
+
return
|
|
552
|
+
}
|
|
553
|
+
// 日记总结会话的输入是群友可控的聊天记录,同样不带任何工具。
|
|
554
|
+
if (await diarySummarizer.isSummarySession(event.sessionID)) {
|
|
555
|
+
for (const name of Object.keys(event.tools)) delete event.tools[name]
|
|
492
556
|
}
|
|
493
557
|
return
|
|
494
558
|
}
|
|
@@ -725,6 +789,35 @@ export default Plugin.define({
|
|
|
725
789
|
}
|
|
726
790
|
return mutex
|
|
727
791
|
}
|
|
792
|
+
/**
|
|
793
|
+
* 采集一条入站消息的日记素材。
|
|
794
|
+
*
|
|
795
|
+
* 落盘是异步的,不挡消息处理:写失败只记日志,不影响聊天。
|
|
796
|
+
*/
|
|
797
|
+
const collectDiaryLine = (event: MessageEvent, target: ChatTarget, selfId: string): void => {
|
|
798
|
+
if (!config.diary.enabled) return
|
|
799
|
+
const content = extractDiaryText(event.message)
|
|
800
|
+
if (!content) return
|
|
801
|
+
const userId = String(event.user_id)
|
|
802
|
+
void (async () => {
|
|
803
|
+
let name = ""
|
|
804
|
+
try {
|
|
805
|
+
name = await resolveUserName(renderContext(target, selfId), userId)
|
|
806
|
+
} catch {
|
|
807
|
+
// 取不到显示名就退回 QQ 号。
|
|
808
|
+
}
|
|
809
|
+
await diaryCollector.append(targetKey(target), {
|
|
810
|
+
id: String(event.message_id),
|
|
811
|
+
time: event.time,
|
|
812
|
+
userId,
|
|
813
|
+
name: name || userId,
|
|
814
|
+
text: content,
|
|
815
|
+
self: Boolean(selfId) && userId === selfId,
|
|
816
|
+
backfilled: false,
|
|
817
|
+
})
|
|
818
|
+
})().catch((error: unknown) => logger.error("采集日记素材失败", error))
|
|
819
|
+
}
|
|
820
|
+
|
|
728
821
|
const scheduleMessage = (event: MessageEvent) => {
|
|
729
822
|
// 没有占用机器人连接时不消费消息,避免和别的实例重复投递。
|
|
730
823
|
if (!botLock) return
|
|
@@ -738,6 +831,8 @@ export default Plugin.define({
|
|
|
738
831
|
return
|
|
739
832
|
}
|
|
740
833
|
manager.touch(target)
|
|
834
|
+
// 日记素材:放在去重之后,重复投递的消息只会被记一次。
|
|
835
|
+
collectDiaryLine(event, target, selfId)
|
|
741
836
|
logger.info(`${targetLabel(target)} 收到 QQ 消息`, {
|
|
742
837
|
user: String(event.user_id),
|
|
743
838
|
message_id: String(event.message_id),
|
|
@@ -836,6 +931,12 @@ export default Plugin.define({
|
|
|
836
931
|
unrefTimer(proactiveTimer)
|
|
837
932
|
}
|
|
838
933
|
|
|
934
|
+
// 日记调度:到点生成当天的日记并推送。定时器随插件一起卸载,
|
|
935
|
+
// 要保证它一直跑需要 experimental.location_idle_timeout: false。
|
|
936
|
+
if (config.diary.enabled) {
|
|
937
|
+
diaryScheduler.start()
|
|
938
|
+
}
|
|
939
|
+
|
|
839
940
|
// 工具先注册,模型这一轮就能看到 qq_*;再连 NapCat 开始消费。
|
|
840
941
|
await registerRuntime()
|
|
841
942
|
await startConsuming()
|
package/src/naplink/render.ts
CHANGED
|
@@ -108,6 +108,48 @@ export function shouldProcess(message: readonly MessageSegment[], userId?: strin
|
|
|
108
108
|
})
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* 轻量提取一条消息的文本。
|
|
113
|
+
*
|
|
114
|
+
* 日记素材用:只出占位文本,不下载图片、不转写语音,
|
|
115
|
+
* 因此不需要任何 API 调用,可以在入站链路上同步跑完。
|
|
116
|
+
*/
|
|
117
|
+
export function extractDiaryText(message: readonly MessageSegment[]): string {
|
|
118
|
+
const parts: string[] = []
|
|
119
|
+
for (const segment of segments(message)) {
|
|
120
|
+
switch (segment.type) {
|
|
121
|
+
case "text": {
|
|
122
|
+
const content = sanitizeUntrustedText(segment.data.text ?? "").trim()
|
|
123
|
+
if (content) parts.push(content)
|
|
124
|
+
break
|
|
125
|
+
}
|
|
126
|
+
case "at":
|
|
127
|
+
parts.push(`@${String(segment.data.qq)}`)
|
|
128
|
+
break
|
|
129
|
+
case "face":
|
|
130
|
+
parts.push("[表情]")
|
|
131
|
+
break
|
|
132
|
+
case "image":
|
|
133
|
+
parts.push("[图片]")
|
|
134
|
+
break
|
|
135
|
+
case "record":
|
|
136
|
+
case "audio":
|
|
137
|
+
parts.push("[语音]")
|
|
138
|
+
break
|
|
139
|
+
case "video":
|
|
140
|
+
parts.push("[视频]")
|
|
141
|
+
break
|
|
142
|
+
case "file":
|
|
143
|
+
parts.push("[文件]")
|
|
144
|
+
break
|
|
145
|
+
default:
|
|
146
|
+
// 引用、转发与结构化段不进素材。
|
|
147
|
+
break
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return parts.join(" ").trim()
|
|
151
|
+
}
|
|
152
|
+
|
|
111
153
|
/** 判断消息里是否 @ 了指定 QQ 号。 */
|
|
112
154
|
export function mentionsUser(
|
|
113
155
|
message: readonly MessageSegment[],
|
package/src/tools/policy.ts
CHANGED
|
@@ -41,15 +41,23 @@ export interface ToolPolicyFile {
|
|
|
41
41
|
chats: Record<string, string>
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* 只对 owner 与 admin 开放的工具。
|
|
46
|
+
*
|
|
47
|
+
* 日记记录聊天内容,发文件能把本地文件送出去:两者都只给管理档。
|
|
48
|
+
* 用 deny 而不是不放进 allow,是为了用户自定义配置档时也不会误放开。
|
|
49
|
+
*/
|
|
50
|
+
const DIARY_DENY = ["qq_diary_*", "qq_send_file"]
|
|
51
|
+
|
|
44
52
|
/** 内置默认值:普通聊天只留联网搜索、网页抓取和发消息工具,管理档全开。 */
|
|
45
53
|
export function defaultToolPolicy(): ToolPolicyFile {
|
|
46
54
|
return {
|
|
47
55
|
defaultProfile: "chat",
|
|
48
56
|
profiles: {
|
|
49
|
-
chat: { allow: ["websearch", "webfetch", "qq_*"], deny: [] },
|
|
50
|
-
readonly: { allow: ["read", "glob", "grep", "qq_*"], deny: [] },
|
|
57
|
+
chat: { allow: ["websearch", "webfetch", "qq_*"], deny: [...DIARY_DENY] },
|
|
58
|
+
readonly: { allow: ["read", "glob", "grep", "qq_*"], deny: [...DIARY_DENY] },
|
|
51
59
|
// 主动说话没有人参与,权限不能跟着聊天内容走:与普通聊天同档。
|
|
52
|
-
proactive: { allow: ["websearch", "webfetch", "qq_*"], deny: [] },
|
|
60
|
+
proactive: { allow: ["websearch", "webfetch", "qq_*"], deny: [...DIARY_DENY] },
|
|
53
61
|
admin: { allow: ["*"], deny: [] },
|
|
54
62
|
},
|
|
55
63
|
chats: {},
|
package/src/tools/qq.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { basename } from "node:path"
|
|
1
2
|
import type { Plugin } from "@opencode/plugin"
|
|
2
3
|
import { isTargetAllowed } from "../access"
|
|
3
4
|
import { delay } from "../async"
|
|
4
5
|
import { type ChatTarget, targetKey, targetLabel } from "../chat"
|
|
5
6
|
import type { QQBotConfig } from "../config"
|
|
7
|
+
import { dateKey, isDateKey } from "../diary/format"
|
|
8
|
+
import type { DiaryStore } from "../diary/store"
|
|
6
9
|
import {
|
|
7
10
|
CUSTOM_FACE_SUB_TYPE,
|
|
8
11
|
type FavoriteStore,
|
|
@@ -12,6 +15,7 @@ import {
|
|
|
12
15
|
import type { FavoriteIndex } from "../emoji/favorite-index"
|
|
13
16
|
import type { Logger } from "../logger"
|
|
14
17
|
import type { OutboundMedia } from "../media-path"
|
|
18
|
+
import { neutralizeStructureTags, sanitizeUntrustedText } from "../model-view"
|
|
15
19
|
import type { QQClient } from "../naplink/client"
|
|
16
20
|
import { splitGraphemes, splitMessage, segmentDelayMs, toPlainText } from "../outbound"
|
|
17
21
|
import type { RateLimitVerdict, SendRateLimiter } from "../rate-limit"
|
|
@@ -50,6 +54,10 @@ export const QQ_TOOL_NAMES = [
|
|
|
50
54
|
"qq_get_group_member_info",
|
|
51
55
|
"qq_fetch_ptt_text",
|
|
52
56
|
"qq_get_login_info",
|
|
57
|
+
"qq_send_file",
|
|
58
|
+
"qq_diary_write",
|
|
59
|
+
"qq_diary_read",
|
|
60
|
+
"qq_diary_list",
|
|
53
61
|
] as const
|
|
54
62
|
|
|
55
63
|
export interface ToolDeps {
|
|
@@ -68,6 +76,8 @@ export interface ToolDeps {
|
|
|
68
76
|
sensitive: SensitiveChecker
|
|
69
77
|
/** 出站媒体的路径与地址把关。 */
|
|
70
78
|
outbound: OutboundMedia
|
|
79
|
+
/** 日记读写,供 qq_diary_* 使用;不传就不注册日记工具。 */
|
|
80
|
+
diary?: DiaryStore
|
|
71
81
|
/** 调用链追踪器;不传就不记追踪。 */
|
|
72
82
|
tracer?: Tracer
|
|
73
83
|
/**
|
|
@@ -111,6 +121,25 @@ interface MessageIdInput {
|
|
|
111
121
|
message_id?: number | string
|
|
112
122
|
}
|
|
113
123
|
|
|
124
|
+
interface DiaryWriteInput {
|
|
125
|
+
content?: string
|
|
126
|
+
date?: string
|
|
127
|
+
title?: string
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
interface DiaryReadInput {
|
|
131
|
+
date?: string
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface DiaryListInput {
|
|
135
|
+
days?: number
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface FileInput {
|
|
139
|
+
file?: string
|
|
140
|
+
name?: string
|
|
141
|
+
}
|
|
142
|
+
|
|
114
143
|
/** 合并转发里单个节点的字数上限,留出余量避免被 NapCat 截断。 */
|
|
115
144
|
const FORWARD_NODE_CHARS = 3000
|
|
116
145
|
|
|
@@ -123,6 +152,30 @@ function text(value: string, metadata?: Record<string, unknown>) {
|
|
|
123
152
|
/** 工具返回值:content 是给模型看的文本,metadata 带失败标记。 */
|
|
124
153
|
type ToolResult = ReturnType<typeof text>
|
|
125
154
|
|
|
155
|
+
/** 日记正文进模型上下文的固定上限,超出部分截断。 */
|
|
156
|
+
const DIARY_READ_MAX_CHARS = 4000
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 净化日记正文。
|
|
160
|
+
*
|
|
161
|
+
* 逐行净化再整体中和结构标签:日记是多段文本,
|
|
162
|
+
* 整体净化会把换行压成空格,段落就没了。
|
|
163
|
+
*/
|
|
164
|
+
function sanitizeDiaryBody(body: string): string {
|
|
165
|
+
const lines = String(body ?? "")
|
|
166
|
+
.replace(/\r\n/g, "\n")
|
|
167
|
+
.split("\n")
|
|
168
|
+
.map((line) => sanitizeUntrustedText(line))
|
|
169
|
+
return neutralizeStructureTags(lines.join("\n"))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 组装给模型看的日记正文:标题加正文,超长截断。 */
|
|
173
|
+
function formatDiaryBody(date: string, title: string, body: string): string {
|
|
174
|
+
const plain = sanitizeDiaryBody(body)
|
|
175
|
+
const clipped = Array.from(plain).slice(0, DIARY_READ_MAX_CHARS).join("")
|
|
176
|
+
return [`${date} 的日记:${sanitizeUntrustedText(title)}`, "", clipped].join("\n")
|
|
177
|
+
}
|
|
178
|
+
|
|
126
179
|
/** 按目标类型把消息投递出去:群聊与私聊走不同的 API。 */
|
|
127
180
|
function sendPayload(client: QQClient, target: ChatTarget, payload: string | unknown[]) {
|
|
128
181
|
return target.kind === "group"
|
|
@@ -199,6 +252,34 @@ export function createPlainSender(
|
|
|
199
252
|
}
|
|
200
253
|
}
|
|
201
254
|
|
|
255
|
+
/**
|
|
256
|
+
* 向一个目标发送本地文件。
|
|
257
|
+
*
|
|
258
|
+
* 路径先过出站白名单:只允许白名单目录里的普通文件,软链接指向别处也拒绝。
|
|
259
|
+
* 文件内容不扫描,只看显示名与路径,避免把凭证文件指出去。
|
|
260
|
+
*/
|
|
261
|
+
export async function sendFileMessage(
|
|
262
|
+
client: QQClient,
|
|
263
|
+
target: ChatTarget,
|
|
264
|
+
file: string,
|
|
265
|
+
options: {
|
|
266
|
+
/** 对方看到的文件名;不传时用真实文件名。 */
|
|
267
|
+
name?: string
|
|
268
|
+
limiter: SendRateLimiter
|
|
269
|
+
outbound: OutboundMedia
|
|
270
|
+
sensitive: SensitiveChecker
|
|
271
|
+
},
|
|
272
|
+
): Promise<void> {
|
|
273
|
+
const plan = await options.outbound.check(file)
|
|
274
|
+
if (plan.kind !== "local") throw new Error("只能发送本地文件")
|
|
275
|
+
const path = plan.path
|
|
276
|
+
const name = options.name?.trim() || basename(path)
|
|
277
|
+
const verdict = options.sensitive.check(`${name}\n${path}`)
|
|
278
|
+
if (!verdict.allowed) throw new Error(verdict.reason)
|
|
279
|
+
await options.limiter.pace(pluginSendScope(target), nextPlainOperation())
|
|
280
|
+
await sendPayload(client, target, [{ type: "file", data: { file: path, name } }])
|
|
281
|
+
}
|
|
282
|
+
|
|
202
283
|
/**
|
|
203
284
|
* 注册 QQ 命名空间下的工具。
|
|
204
285
|
*
|
|
@@ -217,6 +298,7 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
217
298
|
limiter,
|
|
218
299
|
sensitive,
|
|
219
300
|
outbound,
|
|
301
|
+
diary,
|
|
220
302
|
tracer,
|
|
221
303
|
} = deps
|
|
222
304
|
|
|
@@ -906,5 +988,147 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
906
988
|
return text(JSON.stringify(info))
|
|
907
989
|
},
|
|
908
990
|
})
|
|
991
|
+
|
|
992
|
+
// 日记工具依赖日记存储;没配存储时不注册,免得模型调到一个必然失败的工具。
|
|
993
|
+
const diaryStore = diary
|
|
994
|
+
if (diaryStore) {
|
|
995
|
+
/** 日记工具的目标:必须是当前会话绑定且放行的那个聊天。 */
|
|
996
|
+
const diaryTarget = (sessionID: string): ChatTarget | undefined => requireTarget(sessionID)
|
|
997
|
+
|
|
998
|
+
/** 找不到日记时把可用的日期一起回给模型,省得它来回试。 */
|
|
999
|
+
const diaryMissing = async (chat: string, date: string): Promise<string> => {
|
|
1000
|
+
const available = (await diaryStore.list(chat, 10)).map((item) => item.date)
|
|
1001
|
+
const hint = available.length > 0 ? `(可用的日期:${available.join("、")})` : ""
|
|
1002
|
+
return `error: 没有找到 ${date} 的日记${hint}`
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
editor.add({
|
|
1006
|
+
name: "diary_write",
|
|
1007
|
+
description:
|
|
1008
|
+
"给当前 QQ 群聊或私聊写一篇日记。同一个聊天同一天只有一篇日记,重复写会覆盖当天那一篇。content 是日记正文;date 是日期,格式 YYYY-MM-DD,省略表示今天;title 是标题,省略时用日期做标题。",
|
|
1009
|
+
input: {
|
|
1010
|
+
type: "object",
|
|
1011
|
+
properties: {
|
|
1012
|
+
content: { type: "string", description: "日记正文(必填)" },
|
|
1013
|
+
date: { type: "string", description: "日期,格式 YYYY-MM-DD;省略表示今天" },
|
|
1014
|
+
title: { type: "string", description: "日记标题;省略时用日期做标题" },
|
|
1015
|
+
},
|
|
1016
|
+
required: ["content"],
|
|
1017
|
+
additionalProperties: false,
|
|
1018
|
+
},
|
|
1019
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
1020
|
+
execute: async (raw, context) => {
|
|
1021
|
+
const input = (raw ?? {}) as DiaryWriteInput
|
|
1022
|
+
const target = diaryTarget(context.sessionID)
|
|
1023
|
+
if (!target) return text("error: 当前会话没有绑定的聊天,无法读写日记")
|
|
1024
|
+
const content = (input.content ?? "").trim()
|
|
1025
|
+
if (!content) return text("error: 缺少 content")
|
|
1026
|
+
const date = input.date?.trim() || dateKey()
|
|
1027
|
+
if (!isDateKey(date)) return text(`error: 日期格式不对:${date},请用 YYYY-MM-DD`)
|
|
1028
|
+
const entry = await diaryStore.write(targetKey(target), date, {
|
|
1029
|
+
title: input.title?.trim() ?? "",
|
|
1030
|
+
body: content,
|
|
1031
|
+
source: "model",
|
|
1032
|
+
})
|
|
1033
|
+
return text(`已写入 ${date} 的日记:${entry.title}`)
|
|
1034
|
+
},
|
|
1035
|
+
})
|
|
1036
|
+
|
|
1037
|
+
editor.add({
|
|
1038
|
+
name: "diary_read",
|
|
1039
|
+
description:
|
|
1040
|
+
"读取当前 QQ 群聊或私聊的日记正文。date 是日期,格式 YYYY-MM-DD;省略时返回最近一篇。返回的是日记文本,不是文件列表。",
|
|
1041
|
+
input: {
|
|
1042
|
+
type: "object",
|
|
1043
|
+
properties: {
|
|
1044
|
+
date: { type: "string", description: "日期,格式 YYYY-MM-DD;省略表示最近一篇" },
|
|
1045
|
+
},
|
|
1046
|
+
additionalProperties: false,
|
|
1047
|
+
},
|
|
1048
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
1049
|
+
execute: async (raw, context) => {
|
|
1050
|
+
const input = (raw ?? {}) as DiaryReadInput
|
|
1051
|
+
const target = diaryTarget(context.sessionID)
|
|
1052
|
+
if (!target) return text("error: 当前会话没有绑定的聊天,无法读写日记")
|
|
1053
|
+
const chat = targetKey(target)
|
|
1054
|
+
const date = input.date?.trim()
|
|
1055
|
+
if (!date) {
|
|
1056
|
+
const latest = await diaryStore.latest(chat)
|
|
1057
|
+
if (!latest) return text("error: 这个聊天还没有日记")
|
|
1058
|
+
return text(formatDiaryBody(latest.date, latest.title, latest.body))
|
|
1059
|
+
}
|
|
1060
|
+
if (!isDateKey(date)) return text(`error: 日期格式不对:${date},请用 YYYY-MM-DD`)
|
|
1061
|
+
const entry = await diaryStore.read(chat, date)
|
|
1062
|
+
if (!entry) return text(await diaryMissing(chat, date))
|
|
1063
|
+
return text(formatDiaryBody(entry.date, entry.title, entry.body))
|
|
1064
|
+
},
|
|
1065
|
+
})
|
|
1066
|
+
|
|
1067
|
+
editor.add({
|
|
1068
|
+
name: "diary_list",
|
|
1069
|
+
description:
|
|
1070
|
+
"列出当前 QQ 群聊或私聊最近的日记,只返回日期和标题。days 是看几篇,默认 7 篇。",
|
|
1071
|
+
input: {
|
|
1072
|
+
type: "object",
|
|
1073
|
+
properties: {
|
|
1074
|
+
days: { type: "number", description: "看最近几篇,默认 7" },
|
|
1075
|
+
},
|
|
1076
|
+
additionalProperties: false,
|
|
1077
|
+
},
|
|
1078
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
1079
|
+
execute: async (raw, context) => {
|
|
1080
|
+
const input = (raw ?? {}) as DiaryListInput
|
|
1081
|
+
const target = diaryTarget(context.sessionID)
|
|
1082
|
+
if (!target) return text("error: 当前会话没有绑定的聊天,无法读写日记")
|
|
1083
|
+
const days = Number.isInteger(input.days) && (input.days ?? 0) > 0 ? (input.days ?? 7) : 7
|
|
1084
|
+
const entries = await diaryStore.list(targetKey(target), days)
|
|
1085
|
+
if (entries.length === 0) return text("这个聊天还没有日记。")
|
|
1086
|
+
return text(entries.map((entry) => `${entry.date} ${entry.title}`).join("\n"))
|
|
1087
|
+
},
|
|
1088
|
+
})
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
editor.add({
|
|
1092
|
+
name: "send_file",
|
|
1093
|
+
description:
|
|
1094
|
+
"向当前 QQ 群聊或私聊发送一个本地文件。file 是文件路径,只能是机器人允许发送的目录里的普通文件;name 是对方看到的文件名,省略时用原文件名。",
|
|
1095
|
+
input: {
|
|
1096
|
+
type: "object",
|
|
1097
|
+
properties: {
|
|
1098
|
+
file: { type: "string", description: "本地文件路径(必填)" },
|
|
1099
|
+
name: { type: "string", description: "对方看到的文件名;省略时用原文件名" },
|
|
1100
|
+
},
|
|
1101
|
+
required: ["file"],
|
|
1102
|
+
additionalProperties: false,
|
|
1103
|
+
},
|
|
1104
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
1105
|
+
execute: async (raw, context) => {
|
|
1106
|
+
const input = (raw ?? {}) as FileInput
|
|
1107
|
+
const prepared = await prepareSend(context.sessionID)
|
|
1108
|
+
if (prepared.error) return prepared.error
|
|
1109
|
+
const target = prepared.target
|
|
1110
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
1111
|
+
if (denied) return text(denied)
|
|
1112
|
+
const file = input.file?.trim()
|
|
1113
|
+
if (!file) return text("error: 缺少 file")
|
|
1114
|
+
let plan
|
|
1115
|
+
try {
|
|
1116
|
+
plan = await outbound.check(file)
|
|
1117
|
+
} catch (error) {
|
|
1118
|
+
return text(`error: ${error instanceof Error ? error.message : String(error)}`)
|
|
1119
|
+
}
|
|
1120
|
+
if (plan.kind !== "local") return text("error: 只能发送本地文件")
|
|
1121
|
+
const path = plan.path
|
|
1122
|
+
const name = input.name?.trim() || basename(path)
|
|
1123
|
+
// 文件内容没法扫描,路径与显示名同样过一遍出站检查。
|
|
1124
|
+
guardOutbound(target, `${name}\n${path}`)
|
|
1125
|
+
const verdict = await permit(target, nextOperation())
|
|
1126
|
+
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
1127
|
+
const response = await send(target, [{ type: "file", data: { file: path, name } }])
|
|
1128
|
+
trackSent(target, response)
|
|
1129
|
+
manager.markMessageSent(context.sessionID)
|
|
1130
|
+
return text("你已成功发送文件,可以选择结束对话")
|
|
1131
|
+
},
|
|
1132
|
+
})
|
|
909
1133
|
})
|
|
910
1134
|
}
|
|
@@ -3,11 +3,15 @@
|
|
|
3
3
|
"profiles": {
|
|
4
4
|
"chat": {
|
|
5
5
|
"allow": ["websearch", "webfetch", "qq_*"],
|
|
6
|
-
"deny": []
|
|
6
|
+
"deny": ["qq_diary_*", "qq_send_file"]
|
|
7
7
|
},
|
|
8
8
|
"readonly": {
|
|
9
9
|
"allow": ["read", "glob", "grep", "qq_*"],
|
|
10
|
-
"deny": []
|
|
10
|
+
"deny": ["qq_diary_*", "qq_send_file"]
|
|
11
|
+
},
|
|
12
|
+
"proactive": {
|
|
13
|
+
"allow": ["websearch", "webfetch", "qq_*"],
|
|
14
|
+
"deny": ["qq_diary_*", "qq_send_file"]
|
|
11
15
|
},
|
|
12
16
|
"admin": {
|
|
13
17
|
"allow": ["*"],
|