@zhin.js/adapter-qq 5.0.0 → 5.0.2
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/CHANGELOG.md +35 -0
- package/README.md +50 -1
- package/{skills/qq → agent}/PERMITS.md +1 -1
- package/agent/tools/add_role.ts +23 -0
- package/agent/tools/channel_info.ts +19 -0
- package/agent/tools/create_role.ts +22 -0
- package/agent/tools/list_channels.ts +20 -0
- package/agent/tools/list_guilds.ts +17 -0
- package/agent/tools/list_roles.ts +20 -0
- package/agent/tools/member_detail.ts +20 -0
- package/agent/tools/remove_role.ts +23 -0
- package/dist/index.js +2 -2
- package/lib/agent/tools/add_role.js +23 -0
- package/lib/agent/tools/add_role.js.map +1 -0
- package/lib/agent/tools/channel_info.js +19 -0
- package/lib/agent/tools/channel_info.js.map +1 -0
- package/lib/agent/tools/create_role.js +22 -0
- package/lib/agent/tools/create_role.js.map +1 -0
- package/lib/agent/tools/list_channels.js +20 -0
- package/lib/agent/tools/list_channels.js.map +1 -0
- package/lib/agent/tools/list_guilds.js +17 -0
- package/lib/agent/tools/list_guilds.js.map +1 -0
- package/lib/agent/tools/list_roles.js +20 -0
- package/lib/agent/tools/list_roles.js.map +1 -0
- package/lib/agent/tools/member_detail.js +20 -0
- package/lib/agent/tools/member_detail.js.map +1 -0
- package/lib/agent/tools/remove_role.js +23 -0
- package/lib/agent/tools/remove_role.js.map +1 -0
- package/lib/package.json +105 -0
- package/lib/{adapter.js → src/adapter.js} +12 -5
- package/lib/src/adapter.js.map +1 -0
- package/lib/{endpoint.js → src/endpoint.js} +80 -3
- package/lib/src/endpoint.js.map +1 -0
- package/lib/{gateway-config.js → src/gateway-config.js} +3 -3
- package/lib/src/gateway-config.js.map +1 -0
- package/lib/{group-at-normalize.js → src/group-at-normalize.js} +2 -5
- package/lib/src/group-at-normalize.js.map +1 -0
- package/lib/src/inbound-action.js +57 -0
- package/lib/src/inbound-action.js.map +1 -0
- package/lib/src/inbound-normalize.js.map +1 -0
- package/lib/src/index.js +194 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/mention-enrich.js +52 -0
- package/lib/src/mention-enrich.js.map +1 -0
- package/lib/src/outbound-keyboard.js +100 -0
- package/lib/src/outbound-keyboard.js.map +1 -0
- package/lib/src/outbound-markdown.js +189 -0
- package/lib/src/outbound-markdown.js.map +1 -0
- package/lib/{outbound-media.js → src/outbound-media.js} +2 -1
- package/lib/src/outbound-media.js.map +1 -0
- package/lib/src/outbound-mixed-media.js +78 -0
- package/lib/src/outbound-mixed-media.js.map +1 -0
- package/lib/{platform-permit.js → src/platform-permit.js} +5 -2
- package/lib/src/platform-permit.js.map +1 -0
- package/lib/src/qq-agent-deps.js +10 -0
- package/lib/src/qq-agent-deps.js.map +1 -0
- package/lib/{qq-bind-api.js → src/qq-bind-api.js} +9 -5
- package/lib/src/qq-bind-api.js.map +1 -0
- package/lib/{qq-bind-flow.js → src/qq-bind-flow.js} +11 -2
- package/lib/src/qq-bind-flow.js.map +1 -0
- package/lib/src/qq-bind-persist.js +52 -0
- package/lib/src/qq-bind-persist.js.map +1 -0
- package/lib/{qq-endpoint-manager.js → src/qq-endpoint-manager.js} +43 -8
- package/lib/src/qq-endpoint-manager.js.map +1 -0
- package/lib/src/sdk-version.js.map +1 -0
- package/lib/src/segment-mapper.js +2 -0
- package/lib/src/segment-mapper.js.map +1 -0
- package/lib/src/types.js.map +1 -0
- package/package.json +27 -17
- package/src/adapter.ts +14 -10
- package/src/endpoint.ts +119 -12
- package/src/gateway-config.ts +5 -5
- package/src/group-at-normalize.ts +2 -5
- package/src/inbound-action.ts +87 -0
- package/src/index.ts +13 -196
- package/src/mention-enrich.ts +71 -0
- package/src/outbound-keyboard.ts +117 -0
- package/src/outbound-markdown.ts +141 -22
- package/src/outbound-media.ts +2 -1
- package/src/outbound-mixed-media.ts +101 -0
- package/src/platform-permit.ts +3 -4
- package/src/qq-agent-deps.ts +23 -0
- package/src/qq-bind-api.ts +24 -5
- package/src/qq-bind-flow.ts +13 -3
- package/src/qq-bind-persist.ts +71 -0
- package/src/qq-endpoint-manager.ts +64 -11
- package/src/segment-mapper.ts +1 -0
- package/src/types.ts +8 -2
- package/lib/adapter.d.ts +0 -25
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js.map +0 -1
- package/lib/endpoint.d.ts +0 -120
- package/lib/endpoint.d.ts.map +0 -1
- package/lib/endpoint.js.map +0 -1
- package/lib/gateway-config.d.ts +0 -14
- package/lib/gateway-config.d.ts.map +0 -1
- package/lib/gateway-config.js.map +0 -1
- package/lib/group-at-normalize.d.ts +0 -8
- package/lib/group-at-normalize.d.ts.map +0 -1
- package/lib/group-at-normalize.js.map +0 -1
- package/lib/inbound-normalize.d.ts +0 -11
- package/lib/inbound-normalize.d.ts.map +0 -1
- package/lib/inbound-normalize.js.map +0 -1
- package/lib/index.d.ts +0 -23
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -375
- package/lib/index.js.map +0 -1
- package/lib/outbound-markdown.d.ts +0 -12
- package/lib/outbound-markdown.d.ts.map +0 -1
- package/lib/outbound-markdown.js +0 -78
- package/lib/outbound-markdown.js.map +0 -1
- package/lib/outbound-media.d.ts +0 -9
- package/lib/outbound-media.d.ts.map +0 -1
- package/lib/outbound-media.js.map +0 -1
- package/lib/platform-permit.d.ts +0 -17
- package/lib/platform-permit.d.ts.map +0 -1
- package/lib/platform-permit.js.map +0 -1
- package/lib/qq-bind-api.d.ts +0 -27
- package/lib/qq-bind-api.d.ts.map +0 -1
- package/lib/qq-bind-api.js.map +0 -1
- package/lib/qq-bind-flow.d.ts +0 -19
- package/lib/qq-bind-flow.d.ts.map +0 -1
- package/lib/qq-bind-flow.js.map +0 -1
- package/lib/qq-endpoint-manager.d.ts +0 -16
- package/lib/qq-endpoint-manager.d.ts.map +0 -1
- package/lib/qq-endpoint-manager.js.map +0 -1
- package/lib/sdk-version.d.ts +0 -8
- package/lib/sdk-version.d.ts.map +0 -1
- package/lib/sdk-version.js.map +0 -1
- package/lib/types.d.ts +0 -38
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- /package/{skills/qq/SKILL.md → agent/skills/qq.md} +0 -0
- /package/lib/{inbound-normalize.js → src/inbound-normalize.js} +0 -0
- /package/lib/{sdk-version.js → src/sdk-version.js} +0 -0
- /package/lib/{types.js → src/types.js} +0 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将 core keyboard 段转为 qq-official-bot 可识别的 button / markdown 段。
|
|
3
|
+
* @see https://bot.q.qq.com/wiki/develop/api-v2/server-inter/message/trans/msg-btn.html
|
|
4
|
+
*/
|
|
5
|
+
import { isKeyboardSegment, KeyboardSegment, type ButtonData, type KeyboardSegmentData, type MessageSegment, type SendContent } from 'zhin.js';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function asKeyboardData(item: unknown): KeyboardSegmentData | null {
|
|
9
|
+
if (item instanceof KeyboardSegment) return item.data;
|
|
10
|
+
if (item != null && typeof item === "object" && isKeyboardSegment(item as MessageSegment)) {
|
|
11
|
+
return (item as MessageSegment).data as KeyboardSegmentData;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type QqButton = {
|
|
17
|
+
id: string;
|
|
18
|
+
render_data: { label: string; visited_label: string; style: number };
|
|
19
|
+
action: {
|
|
20
|
+
type: number;
|
|
21
|
+
permission: { type: number };
|
|
22
|
+
data: string;
|
|
23
|
+
click_limit: number;
|
|
24
|
+
unsupport_tips: string;
|
|
25
|
+
enter?: boolean;
|
|
26
|
+
reply?: boolean;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function coreButtonToQq(btn: ButtonData): QqButton {
|
|
31
|
+
const isCommand = btn.mode === 'command';
|
|
32
|
+
const action: QqButton['action'] = {
|
|
33
|
+
type: isCommand ? 2 : 1,
|
|
34
|
+
permission: { type: 2 },
|
|
35
|
+
data: btn.payload,
|
|
36
|
+
click_limit: btn.disabled ? 0 : 10,
|
|
37
|
+
unsupport_tips: btn.disabled ? "该按钮不可用" : "",
|
|
38
|
+
};
|
|
39
|
+
if (isCommand) {
|
|
40
|
+
if (btn.command?.enter != null) action.enter = btn.command.enter;
|
|
41
|
+
if (btn.command?.reply != null) action.reply = btn.command.reply;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
id: btn.id,
|
|
45
|
+
render_data: {
|
|
46
|
+
label: btn.label,
|
|
47
|
+
visited_label: btn.label,
|
|
48
|
+
style: 0,
|
|
49
|
+
},
|
|
50
|
+
action,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function keyboardDataToQqSegments(data: KeyboardSegmentData): MessageSegment[] {
|
|
55
|
+
const out: MessageSegment[] = [];
|
|
56
|
+
for (const row of data.rows) {
|
|
57
|
+
out.push({
|
|
58
|
+
type: "button",
|
|
59
|
+
data: {
|
|
60
|
+
buttons: row.map(coreButtonToQq),
|
|
61
|
+
},
|
|
62
|
+
} as MessageSegment);
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** QQ 官方出站:keyboard → button 行;前置 text 合并为 markdown(按钮消息需带 markdown) */
|
|
68
|
+
export function expandKeyboardSegmentsForQq(content: SendContent): SendContent {
|
|
69
|
+
const items = Array.isArray(content) ? content : [content];
|
|
70
|
+
const out: (string | MessageSegment)[] = [];
|
|
71
|
+
const textParts: string[] = [];
|
|
72
|
+
|
|
73
|
+
for (const item of items) {
|
|
74
|
+
if (typeof item === "string") {
|
|
75
|
+
textParts.push(item);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const keyboardData = asKeyboardData(item);
|
|
79
|
+
if (keyboardData) {
|
|
80
|
+
if (textParts.length > 0) {
|
|
81
|
+
out.push({
|
|
82
|
+
type: "markdown",
|
|
83
|
+
data: { content: textParts.join("\n") },
|
|
84
|
+
} as MessageSegment);
|
|
85
|
+
textParts.length = 0;
|
|
86
|
+
}
|
|
87
|
+
out.push(...keyboardDataToQqSegments(keyboardData));
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (item.type === "text") {
|
|
92
|
+
const t = item.data?.text ?? item.data?.content ?? "";
|
|
93
|
+
if (t) textParts.push(String(t));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (textParts.length > 0) {
|
|
98
|
+
out.push({
|
|
99
|
+
type: "markdown",
|
|
100
|
+
data: { content: textParts.join("\n") },
|
|
101
|
+
} as MessageSegment);
|
|
102
|
+
textParts.length = 0;
|
|
103
|
+
}
|
|
104
|
+
out.push(item as MessageSegment);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (textParts.length > 0) {
|
|
108
|
+
out.push({
|
|
109
|
+
type: "markdown",
|
|
110
|
+
data: { content: textParts.join("\n") },
|
|
111
|
+
} as MessageSegment);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (out.length === 0) return { type: "text", data: { text: "" } };
|
|
115
|
+
if (out.length === 1) return out[0]!;
|
|
116
|
+
return out;
|
|
117
|
+
}
|
package/src/outbound-markdown.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 将 AI 出站纯文本段合并为 QQ Markdown 消息(msg_type=2)。
|
|
3
|
-
*
|
|
3
|
+
* 图文混排时合并为单条 Markdown(图片语法见 QQ 官方文档)。
|
|
4
4
|
*/
|
|
5
5
|
import { segment, type MessageElement, type MessageSegment, type SendContent } from "zhin.js";
|
|
6
|
+
import { resolveMediaFile } from "./outbound-media.js";
|
|
6
7
|
|
|
7
8
|
function isMessageSegment(seg: MessageElement): seg is MessageSegment {
|
|
8
9
|
return typeof seg.type === "string";
|
|
@@ -10,12 +11,7 @@ function isMessageSegment(seg: MessageElement): seg is MessageSegment {
|
|
|
10
11
|
|
|
11
12
|
export type OutboundMarkdownMode = boolean | "auto";
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
const MARKDOWN_HINT =
|
|
15
|
-
/(?:\*\*|__|`|\[.+\]\(.+\)|^#{1,6}\s|^>\s|^\s*[-*+]\s|^```|\|.+\|)/m;
|
|
16
|
-
|
|
17
|
-
const BODY_RICH_TYPES = new Set([
|
|
18
|
-
"image",
|
|
14
|
+
const BLOCKING_RICH_TYPES = new Set([
|
|
19
15
|
"audio",
|
|
20
16
|
"video",
|
|
21
17
|
"file",
|
|
@@ -27,11 +23,13 @@ const BODY_RICH_TYPES = new Set([
|
|
|
27
23
|
"face",
|
|
28
24
|
]);
|
|
29
25
|
|
|
26
|
+
const TEXT_IMAGE_MIX_TYPES = new Set(["text", "at", "image"]);
|
|
27
|
+
|
|
30
28
|
function textSegment(text: string): MessageSegment {
|
|
31
29
|
return { type: "text", data: { text } };
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
function
|
|
32
|
+
export function asOutboundSegments(content: SendContent): MessageSegment[] {
|
|
35
33
|
if (typeof content === "string") return [textSegment(content)];
|
|
36
34
|
if (!Array.isArray(content)) {
|
|
37
35
|
return isMessageSegment(content) ? [content] : [];
|
|
@@ -41,39 +39,160 @@ function asSegments(content: SendContent): MessageSegment[] {
|
|
|
41
39
|
);
|
|
42
40
|
}
|
|
43
41
|
|
|
42
|
+
export function splitReplyPrefix(segments: MessageSegment[]): {
|
|
43
|
+
prefix: MessageSegment[];
|
|
44
|
+
body: MessageSegment[];
|
|
45
|
+
} {
|
|
46
|
+
const prefix: MessageSegment[] = [];
|
|
47
|
+
let i = 0;
|
|
48
|
+
while (i < segments.length && segments[i].type === "reply") {
|
|
49
|
+
prefix.push(segments[i]);
|
|
50
|
+
i++;
|
|
51
|
+
}
|
|
52
|
+
return { prefix, body: segments.slice(i) };
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
function textFromSegment(seg: MessageSegment): string {
|
|
45
56
|
if (seg.type !== "text") return "";
|
|
46
57
|
return seg.data.text ?? seg.data.content ?? "";
|
|
47
58
|
}
|
|
48
59
|
|
|
49
|
-
function
|
|
60
|
+
function isRemoteUrl(value: string): boolean {
|
|
61
|
+
return /^https?:\/\//i.test(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isInlineMedia(value: string): boolean {
|
|
65
|
+
return value.startsWith("base64://")
|
|
66
|
+
|| /^data:[^/]+\/[^;]+;base64,/i.test(value)
|
|
67
|
+
|| value.startsWith("file://")
|
|
68
|
+
|| (value.startsWith("/") && !isRemoteUrl(value));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** QQ Markdown 图片语法: */
|
|
72
|
+
export function formatQqMarkdownImage(
|
|
73
|
+
url: string,
|
|
74
|
+
data: Record<string, unknown> = {},
|
|
75
|
+
): string {
|
|
76
|
+
const width = typeof data.width === "number" && data.width > 0 ? data.width : 200;
|
|
77
|
+
const height = typeof data.height === "number" && data.height > 0 ? data.height : width;
|
|
78
|
+
const label = typeof data.name === "string" && data.name.trim() ? data.name.trim() : "图片";
|
|
79
|
+
return ``;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 将 image 段解析为 Markdown 可嵌入的 URL(https 或 data URI) */
|
|
83
|
+
export function resolveImageMarkdownUrl(data: Record<string, unknown>): string | null {
|
|
84
|
+
const url = typeof data.url === "string" ? data.url : undefined;
|
|
85
|
+
const file = resolveMediaFile(data);
|
|
86
|
+
|
|
87
|
+
if (url && isRemoteUrl(url)) return url;
|
|
88
|
+
if (file && isRemoteUrl(file)) return file;
|
|
89
|
+
|
|
90
|
+
const inline = file ?? (url && isInlineMedia(url) ? url : undefined);
|
|
91
|
+
if (!inline) return null;
|
|
92
|
+
|
|
93
|
+
if (inline.startsWith("base64://")) {
|
|
94
|
+
return `data:image/png;base64,${inline.slice(9)}`;
|
|
95
|
+
}
|
|
96
|
+
if (/^data:[^/]+\/[^;]+;base64,/i.test(inline)) {
|
|
97
|
+
return inline;
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function isTextImageMixedBody(body: MessageSegment[]): boolean {
|
|
103
|
+
if (body.length === 0) return false;
|
|
104
|
+
if (body.some((seg) => !TEXT_IMAGE_MIX_TYPES.has(seg.type))) return false;
|
|
105
|
+
const hasImage = body.some((seg) => seg.type === "image");
|
|
106
|
+
const hasText = body.some((seg) => seg.type === "text" || seg.type === "at");
|
|
107
|
+
return hasImage && hasText;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 图文混排且含 base64/本地图(无法直接嵌入 Markdown 公网 URL) */
|
|
111
|
+
export function isTextImageMixedWithInlineMedia(body: MessageSegment[]): boolean {
|
|
112
|
+
if (!isTextImageMixedBody(body)) return false;
|
|
113
|
+
return body.some((seg) => {
|
|
114
|
+
if (seg.type !== "image") return false;
|
|
115
|
+
const url = typeof seg.data.url === "string" ? seg.data.url : undefined;
|
|
116
|
+
const file = resolveMediaFile(seg.data);
|
|
117
|
+
if (url && isRemoteUrl(url)) return false;
|
|
118
|
+
if (file && isRemoteUrl(file)) return false;
|
|
119
|
+
return Boolean(resolveMediaFile(seg.data) || (url && isInlineMedia(url)));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function segmentToMarkdownPart(seg: MessageSegment): string {
|
|
124
|
+
if (seg.type === "text") return textFromSegment(seg);
|
|
125
|
+
if (seg.type === "at") return `<@${String(seg.data.user_id ?? "")}>`;
|
|
126
|
+
if (seg.type === "image") {
|
|
127
|
+
const src = resolveImageMarkdownUrl(seg.data);
|
|
128
|
+
return src ? formatQqMarkdownImage(src, seg.data) : "";
|
|
129
|
+
}
|
|
130
|
+
return "";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function shouldConvertTextOnly(mode: OutboundMarkdownMode | undefined, bodyText: string): boolean {
|
|
50
134
|
if (mode === false) return false;
|
|
51
135
|
if (mode === true) return bodyText.trim().length > 0;
|
|
52
|
-
return
|
|
136
|
+
return hasMarkdownHint(bodyText);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function hasMarkdownHint(text: string): boolean {
|
|
140
|
+
if (text.includes('**') || text.includes('__') || text.includes('`')) return true;
|
|
141
|
+
if (text.includes('](') || text.includes('|')) return true;
|
|
142
|
+
return text.split('\n').some((line) => {
|
|
143
|
+
const trimmed = line.trimStart();
|
|
144
|
+
return trimmed.startsWith('# ') || trimmed.startsWith('## ')
|
|
145
|
+
|| trimmed.startsWith('> ')
|
|
146
|
+
|| trimmed.startsWith('- ')
|
|
147
|
+
|| trimmed.startsWith('* ')
|
|
148
|
+
|| trimmed.startsWith('+ ');
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function mergeTextImageToMarkdown(
|
|
153
|
+
prefix: MessageSegment[],
|
|
154
|
+
body: MessageSegment[],
|
|
155
|
+
): SendContent | null {
|
|
156
|
+
if (body.some((seg) => seg.type === "image" && !resolveImageMarkdownUrl(seg.data))) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const merged = body.map(segmentToMarkdownPart).join("");
|
|
160
|
+
if (!merged.trim()) return null;
|
|
161
|
+
return [...prefix, segment("markdown", { content: merged })];
|
|
53
162
|
}
|
|
54
163
|
|
|
55
164
|
/**
|
|
56
|
-
* 出站归一化:保留 leading `reply`,将连续 text 合并为单条 markdown 段。
|
|
57
|
-
*
|
|
165
|
+
* 出站归一化:保留 leading `reply`,将连续 text 或 text+image 合并为单条 markdown 段。
|
|
166
|
+
* 含内联 base64 图的图文混排须走 outbound-mixed-media(msg_type=7),此处不处理。
|
|
58
167
|
*/
|
|
59
168
|
export function normalizeOutboundMarkdown(
|
|
60
169
|
content: SendContent,
|
|
61
170
|
mode: OutboundMarkdownMode | undefined = "auto",
|
|
62
171
|
): SendContent {
|
|
63
|
-
const segments =
|
|
172
|
+
const segments = asOutboundSegments(content);
|
|
64
173
|
if (segments.length === 0) return content;
|
|
65
174
|
|
|
66
|
-
const prefix
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
175
|
+
const { prefix, body } = splitReplyPrefix(segments);
|
|
176
|
+
if (body.length === 0) return content;
|
|
177
|
+
if (mode === false) return content;
|
|
178
|
+
|
|
179
|
+
if (body.some((seg) => BLOCKING_RICH_TYPES.has(seg.type))) {
|
|
180
|
+
return content;
|
|
71
181
|
}
|
|
72
182
|
|
|
73
|
-
|
|
74
|
-
|
|
183
|
+
if (body.some((seg) => seg.type === "markdown")) {
|
|
184
|
+
return content;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (isTextImageMixedBody(body)) {
|
|
188
|
+
if (isTextImageMixedWithInlineMedia(body)) {
|
|
189
|
+
return content;
|
|
190
|
+
}
|
|
191
|
+
const mixed = mergeTextImageToMarkdown(prefix, body);
|
|
192
|
+
return mixed ?? content;
|
|
193
|
+
}
|
|
75
194
|
|
|
76
|
-
if (body.some((seg) =>
|
|
195
|
+
if (body.some((seg) => seg.type === "image")) {
|
|
77
196
|
return content;
|
|
78
197
|
}
|
|
79
198
|
|
|
@@ -87,7 +206,7 @@ export function normalizeOutboundMarkdown(
|
|
|
87
206
|
: textFromSegment(seg)))
|
|
88
207
|
.join("");
|
|
89
208
|
|
|
90
|
-
if (!
|
|
209
|
+
if (!shouldConvertTextOnly(mode, merged)) return content;
|
|
91
210
|
|
|
92
211
|
return [...prefix, segment("markdown", { content: merged })];
|
|
93
212
|
}
|
package/src/outbound-media.ts
CHANGED
|
@@ -19,7 +19,8 @@ function isInlineBase64(value: string): boolean {
|
|
|
19
19
|
return value.startsWith("base64://") || /^data:[^/]+\/[^;]+;base64,/i.test(value);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
/** 解析 image/audio/video/file 段的本地或内联 payload(供 mixed-media 上传复用) */
|
|
23
|
+
export function resolveMediaFile(data: Record<string, unknown>): string | undefined {
|
|
23
24
|
const url = typeof data.url === "string" ? data.url : undefined;
|
|
24
25
|
const file = typeof data.file === "string" ? data.file : undefined;
|
|
25
26
|
const base64 = typeof data.base64 === "string" ? data.base64 : undefined;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ 群/私聊图文混排(msg_type=7)。
|
|
3
|
+
* 内联 base64 图无法作为 Markdown 公网 URL,须先上传 file_info 再与文本同条发送。
|
|
4
|
+
*/
|
|
5
|
+
import { randomInt } from "node:crypto";
|
|
6
|
+
import type { MessageSegment, SendContent } from "zhin.js";
|
|
7
|
+
import {
|
|
8
|
+
asOutboundSegments,
|
|
9
|
+
isTextImageMixedWithInlineMedia,
|
|
10
|
+
splitReplyPrefix,
|
|
11
|
+
} from "./outbound-markdown.js";
|
|
12
|
+
import { resolveMediaFile } from "./outbound-media.js";
|
|
13
|
+
|
|
14
|
+
function textFromSegment(seg: MessageSegment): string {
|
|
15
|
+
if (seg.type !== "text") return "";
|
|
16
|
+
return seg.data.text ?? seg.data.content ?? "";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isRemoteUrl(value: string): boolean {
|
|
20
|
+
return /^https?:\/\//i.test(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function extractInlineFileData(source: string): string {
|
|
24
|
+
if (source.startsWith("base64://")) return source.slice(9);
|
|
25
|
+
if (/^data:[^/]+\/[^;]+;base64,/i.test(source)) {
|
|
26
|
+
return source.replace(/^data:[^/]+\/[^;]+;base64,/i, "");
|
|
27
|
+
}
|
|
28
|
+
return source;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function buildQqImageUploadPayload(
|
|
32
|
+
imageSeg: MessageSegment,
|
|
33
|
+
): { file_type: 1; url?: string; file_data?: string } {
|
|
34
|
+
const data = imageSeg.data;
|
|
35
|
+
const url = typeof data.url === "string" ? data.url : undefined;
|
|
36
|
+
const file = resolveMediaFile(data);
|
|
37
|
+
const remote = [url, file].find((item) => item && isRemoteUrl(item));
|
|
38
|
+
if (remote) {
|
|
39
|
+
return { file_type: 1, url: remote };
|
|
40
|
+
}
|
|
41
|
+
const inline = file ?? url;
|
|
42
|
+
if (!inline) {
|
|
43
|
+
throw new Error("QQ 图文混排:图片段缺少 url/file/base64");
|
|
44
|
+
}
|
|
45
|
+
return { file_type: 1, file_data: extractInlineFileData(inline) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildTextImageMixedBodyText(body: MessageSegment[]): string {
|
|
49
|
+
return body
|
|
50
|
+
.map((seg) => {
|
|
51
|
+
if (seg.type === "text") return textFromSegment(seg);
|
|
52
|
+
if (seg.type === "at") return `<@${String(seg.data.user_id ?? "")}>`;
|
|
53
|
+
return "";
|
|
54
|
+
})
|
|
55
|
+
.join("");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function firstImageSegment(body: MessageSegment[]): MessageSegment | undefined {
|
|
59
|
+
return body.find((seg) => seg.type === "image");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function shouldSendTextImageMixedMedia(
|
|
63
|
+
content: SendContent,
|
|
64
|
+
mode: boolean | "auto" | undefined,
|
|
65
|
+
): boolean {
|
|
66
|
+
if (mode === false) return false;
|
|
67
|
+
const { body } = splitReplyPrefix(asOutboundSegments(content));
|
|
68
|
+
return isTextImageMixedWithInlineMedia(body);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface QqMixedMediaMessagePayload {
|
|
72
|
+
msg_type: 7;
|
|
73
|
+
content: string;
|
|
74
|
+
media: { file_info: string };
|
|
75
|
+
msg_seq: number;
|
|
76
|
+
msg_id?: string;
|
|
77
|
+
event_id?: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function buildMixedMediaMessagePayload(
|
|
81
|
+
content: SendContent,
|
|
82
|
+
fileInfo: string,
|
|
83
|
+
): QqMixedMediaMessagePayload {
|
|
84
|
+
const segments = asOutboundSegments(content);
|
|
85
|
+
const { prefix, body } = splitReplyPrefix(segments);
|
|
86
|
+
const payload: QqMixedMediaMessagePayload = {
|
|
87
|
+
msg_type: 7,
|
|
88
|
+
content: buildTextImageMixedBodyText(body),
|
|
89
|
+
media: { file_info: fileInfo },
|
|
90
|
+
msg_seq: randomInt(1, 1_000_000),
|
|
91
|
+
};
|
|
92
|
+
const reply = prefix[prefix.length - 1];
|
|
93
|
+
if (reply?.type === "reply") {
|
|
94
|
+
if (typeof reply.data.event_id === "string") {
|
|
95
|
+
payload.event_id = reply.data.event_id;
|
|
96
|
+
} else if (reply.data.id != null) {
|
|
97
|
+
payload.msg_id = String(reply.data.id);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return payload;
|
|
101
|
+
}
|
package/src/platform-permit.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* QQ 官方频道 platform permit(guild/channel,非 QQ 群 group_*)
|
|
3
3
|
*/
|
|
4
|
-
import type
|
|
5
|
-
import { registerPlatformPermitChecker } from 'zhin.js';
|
|
4
|
+
import { registerPlatformPermitChecker, type Message } from 'zhin.js';
|
|
6
5
|
|
|
7
6
|
const ADAPTER = 'qq';
|
|
8
7
|
|
|
@@ -12,8 +11,8 @@ export function platformPermit(perm: string): string {
|
|
|
12
11
|
|
|
13
12
|
/** 官方频道工厂工具:映射到 guild 语义,不用 group_* */
|
|
14
13
|
const FACTORY_PERM_MAP: Record<string, string> = {
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
scene_admin: 'guild_admin',
|
|
15
|
+
scene_owner: 'guild_owner',
|
|
17
16
|
};
|
|
18
17
|
|
|
19
18
|
export function qqGuildPermitResolver(logicalPerm: string): string {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared runtime deps for qq agent/ authoring tools.
|
|
3
|
+
* Set once from plugin index during init — never call getPlugin() from tool execute.
|
|
4
|
+
*/
|
|
5
|
+
import type { QQAdapter } from './adapter.js';
|
|
6
|
+
import type { QQEndpoint } from './endpoint.js';
|
|
7
|
+
import type { ReceiverMode } from './types.js';
|
|
8
|
+
|
|
9
|
+
export interface QqAgentDeps {
|
|
10
|
+
getEndpoint: (endpointId: string) => QQEndpoint<ReceiverMode>;
|
|
11
|
+
getAdapter: () => QQAdapter;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let _deps: QqAgentDeps | null = null;
|
|
15
|
+
|
|
16
|
+
export function setQqAgentDeps(deps: QqAgentDeps): void {
|
|
17
|
+
_deps = deps;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getQqAgentDeps(): QqAgentDeps {
|
|
21
|
+
if (!_deps) throw new Error('qq agent deps not initialized');
|
|
22
|
+
return _deps;
|
|
23
|
+
}
|
package/src/qq-bind-api.ts
CHANGED
|
@@ -102,6 +102,26 @@ export interface PollBindResultOk {
|
|
|
102
102
|
status: BindStatus;
|
|
103
103
|
botAppId: string;
|
|
104
104
|
botEncryptSecret: string;
|
|
105
|
+
/** 扫码绑定开发者 QQ 的 OpenID(poll_bind_result 返回) */
|
|
106
|
+
userOpenId: string;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function parsePollBindResultData(data: {
|
|
110
|
+
status?: number;
|
|
111
|
+
bot_appid?: string | number;
|
|
112
|
+
bot_encrypt_secret?: string;
|
|
113
|
+
user_openid?: string;
|
|
114
|
+
userOpenid?: string;
|
|
115
|
+
openid?: string;
|
|
116
|
+
} | undefined): PollBindResultOk {
|
|
117
|
+
return {
|
|
118
|
+
status: (data?.status ?? BindStatus.NONE) as BindStatus,
|
|
119
|
+
botAppId: String(data?.bot_appid ?? ''),
|
|
120
|
+
botEncryptSecret: data?.bot_encrypt_secret ?? '',
|
|
121
|
+
userOpenId: String(
|
|
122
|
+
data?.user_openid ?? data?.userOpenid ?? data?.openid ?? '',
|
|
123
|
+
),
|
|
124
|
+
};
|
|
105
125
|
}
|
|
106
126
|
|
|
107
127
|
export async function pollBindResult(
|
|
@@ -114,13 +134,12 @@ export async function pollBindResult(
|
|
|
114
134
|
status?: number;
|
|
115
135
|
bot_appid?: string | number;
|
|
116
136
|
bot_encrypt_secret?: string;
|
|
137
|
+
user_openid?: string;
|
|
138
|
+
userOpenid?: string;
|
|
139
|
+
openid?: string;
|
|
117
140
|
}>(url, { task_id: taskId }, timeoutMs);
|
|
118
141
|
if (res.retcode !== 0) {
|
|
119
142
|
throw new Error(res.msg ?? 'poll_bind_result failed');
|
|
120
143
|
}
|
|
121
|
-
return
|
|
122
|
-
status: (res.data?.status ?? BindStatus.NONE) as BindStatus,
|
|
123
|
-
botAppId: String(res.data?.bot_appid ?? ''),
|
|
124
|
-
botEncryptSecret: res.data?.bot_encrypt_secret ?? '',
|
|
125
|
-
};
|
|
144
|
+
return parsePollBindResultData(res.data);
|
|
126
145
|
}
|
package/src/qq-bind-flow.ts
CHANGED
|
@@ -14,6 +14,7 @@ const POLL_INTERVAL_MS = 2000;
|
|
|
14
14
|
export interface QqBindCredentials {
|
|
15
15
|
appId: string;
|
|
16
16
|
appSecret: string;
|
|
17
|
+
userOpenId?: string;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export interface QqBindCallbacks {
|
|
@@ -50,7 +51,7 @@ async function pollUntilResult(
|
|
|
50
51
|
taskId: string,
|
|
51
52
|
key: string,
|
|
52
53
|
signal?: AbortSignal,
|
|
53
|
-
): Promise<{ outcome: 'scanned'; appId: string; appSecret: string } | { outcome: 'expired' }> {
|
|
54
|
+
): Promise<{ outcome: 'scanned'; appId: string; appSecret: string; userOpenId: string } | { outcome: 'expired' }> {
|
|
54
55
|
while (!signal?.aborted) {
|
|
55
56
|
let result;
|
|
56
57
|
try {
|
|
@@ -61,7 +62,12 @@ async function pollUntilResult(
|
|
|
61
62
|
}
|
|
62
63
|
if (result.status === BindStatus.COMPLETED) {
|
|
63
64
|
const appSecret = decryptSecret(result.botEncryptSecret, key);
|
|
64
|
-
return {
|
|
65
|
+
return {
|
|
66
|
+
outcome: 'scanned',
|
|
67
|
+
appId: result.botAppId,
|
|
68
|
+
appSecret,
|
|
69
|
+
userOpenId: result.userOpenId,
|
|
70
|
+
};
|
|
65
71
|
}
|
|
66
72
|
if (result.status === BindStatus.EXPIRED) {
|
|
67
73
|
return { outcome: 'expired' };
|
|
@@ -101,7 +107,11 @@ export function startQqBindFlow(
|
|
|
101
107
|
await callbacks.onQrDisplayed?.(connectUrl);
|
|
102
108
|
const pollResult = await pollUntilResult(task.taskId, task.key, signal);
|
|
103
109
|
if (pollResult.outcome === 'scanned') {
|
|
104
|
-
callbacks.onSuccess([{
|
|
110
|
+
callbacks.onSuccess([{
|
|
111
|
+
appId: pollResult.appId,
|
|
112
|
+
appSecret: pollResult.appSecret,
|
|
113
|
+
userOpenId: pollResult.userOpenId || undefined,
|
|
114
|
+
}]);
|
|
105
115
|
return;
|
|
106
116
|
}
|
|
107
117
|
await callbacks.onQrExpired?.();
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { runtimeCwd } from 'zhin.js';
|
|
4
|
+
|
|
5
|
+
export interface QqCredentialEnvKeys {
|
|
6
|
+
appidKey: string;
|
|
7
|
+
secretKey: string;
|
|
8
|
+
appidRef: string;
|
|
9
|
+
secretRef: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** 按 endpoint 名称生成唯一 env 键(如 `QQ_MOCK_QQ_BOT_APPID`) */
|
|
13
|
+
export function buildQqCredentialEnvKeys(endpointName: string): QqCredentialEnvKeys {
|
|
14
|
+
const slug = endpointName.replace(/[^a-zA-Z0-9_]/g, '_').toUpperCase();
|
|
15
|
+
const appidKey = `QQ_${slug}_APPID`;
|
|
16
|
+
const secretKey = `QQ_${slug}_SECRET`;
|
|
17
|
+
return {
|
|
18
|
+
appidKey,
|
|
19
|
+
secretKey,
|
|
20
|
+
appidRef: `\${${appidKey}}`,
|
|
21
|
+
secretRef: `\${${secretKey}}`,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveProjectRoot(): string {
|
|
26
|
+
const envRoot = process.env.ZHIN_PROJECT_ROOT?.trim();
|
|
27
|
+
return path.resolve(envRoot || runtimeCwd());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function escapeRegExp(text: string): string {
|
|
31
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function upsertEnvLine(content: string, key: string, value: string): string {
|
|
35
|
+
const lineRe = new RegExp(`^${escapeRegExp(key)}\\s*=.*$`, 'm');
|
|
36
|
+
const newLine = `${key}=${value}`;
|
|
37
|
+
if (lineRe.test(content)) {
|
|
38
|
+
return content.replace(lineRe, newLine);
|
|
39
|
+
}
|
|
40
|
+
const trimmed = content.replace(/\s*$/, '');
|
|
41
|
+
if (trimmed.length === 0) {
|
|
42
|
+
return `${newLine}\n`;
|
|
43
|
+
}
|
|
44
|
+
return `${trimmed}\n${newLine}\n`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 写入或更新 `.env` 中的键值,并同步到当前进程 `process.env` */
|
|
48
|
+
export function persistQqCredentialsToEnv(
|
|
49
|
+
endpointName: string,
|
|
50
|
+
appId: string,
|
|
51
|
+
appSecret: string,
|
|
52
|
+
projectRoot?: string,
|
|
53
|
+
): QqCredentialEnvKeys {
|
|
54
|
+
const keys = buildQqCredentialEnvKeys(endpointName);
|
|
55
|
+
const root = projectRoot ?? resolveProjectRoot();
|
|
56
|
+
const envPath = path.join(root, '.env');
|
|
57
|
+
let content = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf-8') : '';
|
|
58
|
+
|
|
59
|
+
const entries: Record<string, string> = {
|
|
60
|
+
[keys.appidKey]: appId,
|
|
61
|
+
[keys.secretKey]: appSecret,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
65
|
+
content = upsertEnvLine(content, key, value);
|
|
66
|
+
process.env[key] = value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fs.writeFileSync(envPath, content);
|
|
70
|
+
return keys;
|
|
71
|
+
}
|