@gakr-gakr/qqbot 0.1.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.
Files changed (149) hide show
  1. package/api.ts +56 -0
  2. package/autobot.plugin.json +167 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/index.ts +33 -0
  5. package/package.json +64 -0
  6. package/runtime-api.ts +9 -0
  7. package/secret-contract-api.ts +5 -0
  8. package/setup-entry.ts +13 -0
  9. package/setup-plugin-api.ts +3 -0
  10. package/skills/qqbot-channel/SKILL.md +262 -0
  11. package/skills/qqbot-channel/references/api_references.md +521 -0
  12. package/skills/qqbot-media/SKILL.md +37 -0
  13. package/skills/qqbot-remind/SKILL.md +153 -0
  14. package/src/bridge/approval/capability.ts +225 -0
  15. package/src/bridge/approval/handler-runtime.ts +204 -0
  16. package/src/bridge/bootstrap.ts +135 -0
  17. package/src/bridge/channel-entry.ts +18 -0
  18. package/src/bridge/commands/framework-context-adapter.ts +60 -0
  19. package/src/bridge/commands/framework-registration.ts +66 -0
  20. package/src/bridge/commands/from-parser.ts +60 -0
  21. package/src/bridge/commands/result-dispatcher.ts +76 -0
  22. package/src/bridge/config-shared.ts +132 -0
  23. package/src/bridge/config.ts +176 -0
  24. package/src/bridge/gateway.ts +178 -0
  25. package/src/bridge/logger.ts +31 -0
  26. package/src/bridge/narrowing.ts +31 -0
  27. package/src/bridge/plugin-version.ts +102 -0
  28. package/src/bridge/runtime.ts +25 -0
  29. package/src/bridge/sdk-adapter.ts +164 -0
  30. package/src/bridge/setup/finalize.ts +144 -0
  31. package/src/bridge/setup/surface.ts +34 -0
  32. package/src/bridge/tools/channel.ts +58 -0
  33. package/src/bridge/tools/index.ts +15 -0
  34. package/src/bridge/tools/remind.ts +91 -0
  35. package/src/channel.setup.ts +33 -0
  36. package/src/channel.ts +399 -0
  37. package/src/config-schema.ts +84 -0
  38. package/src/engine/access/index.ts +2 -0
  39. package/src/engine/access/resolve-policy.ts +30 -0
  40. package/src/engine/access/sender-match.ts +55 -0
  41. package/src/engine/access/types.ts +2 -0
  42. package/src/engine/adapter/audio.port.ts +27 -0
  43. package/src/engine/adapter/commands.port.ts +22 -0
  44. package/src/engine/adapter/history.port.ts +52 -0
  45. package/src/engine/adapter/index.ts +76 -0
  46. package/src/engine/adapter/mention-gate.port.ts +50 -0
  47. package/src/engine/adapter/types.ts +38 -0
  48. package/src/engine/api/api-client.ts +212 -0
  49. package/src/engine/api/media-chunked.ts +644 -0
  50. package/src/engine/api/media.ts +218 -0
  51. package/src/engine/api/messages.ts +293 -0
  52. package/src/engine/api/retry.ts +217 -0
  53. package/src/engine/api/routes.ts +95 -0
  54. package/src/engine/api/token.ts +277 -0
  55. package/src/engine/approval/index.ts +224 -0
  56. package/src/engine/commands/builtin/log-helpers.ts +341 -0
  57. package/src/engine/commands/builtin/register-all.ts +17 -0
  58. package/src/engine/commands/builtin/register-approve.ts +201 -0
  59. package/src/engine/commands/builtin/register-basic.ts +95 -0
  60. package/src/engine/commands/builtin/register-clear-storage.ts +187 -0
  61. package/src/engine/commands/builtin/register-logs.ts +20 -0
  62. package/src/engine/commands/builtin/register-streaming.ts +138 -0
  63. package/src/engine/commands/builtin/state.ts +31 -0
  64. package/src/engine/commands/slash-command-auth.ts +88 -0
  65. package/src/engine/commands/slash-command-handler.ts +168 -0
  66. package/src/engine/commands/slash-command-test-support.ts +39 -0
  67. package/src/engine/commands/slash-commands-impl.ts +61 -0
  68. package/src/engine/commands/slash-commands.ts +202 -0
  69. package/src/engine/config/credential-backup.ts +108 -0
  70. package/src/engine/config/credentials.ts +76 -0
  71. package/src/engine/config/group.ts +227 -0
  72. package/src/engine/config/resolve.ts +283 -0
  73. package/src/engine/config/setup-logic.ts +84 -0
  74. package/src/engine/gateway/active-cfg.ts +52 -0
  75. package/src/engine/gateway/codec.ts +47 -0
  76. package/src/engine/gateway/constants.ts +117 -0
  77. package/src/engine/gateway/event-dispatcher.ts +177 -0
  78. package/src/engine/gateway/gateway-connection.ts +356 -0
  79. package/src/engine/gateway/gateway.ts +267 -0
  80. package/src/engine/gateway/inbound-attachments.ts +360 -0
  81. package/src/engine/gateway/inbound-context.ts +82 -0
  82. package/src/engine/gateway/inbound-pipeline.ts +171 -0
  83. package/src/engine/gateway/interaction-handler.ts +345 -0
  84. package/src/engine/gateway/message-queue.ts +404 -0
  85. package/src/engine/gateway/outbound-dispatch.ts +590 -0
  86. package/src/engine/gateway/reconnect.ts +199 -0
  87. package/src/engine/gateway/stages/access-stage.ts +99 -0
  88. package/src/engine/gateway/stages/assembly-stage.ts +156 -0
  89. package/src/engine/gateway/stages/content-stage.ts +77 -0
  90. package/src/engine/gateway/stages/envelope-stage.ts +144 -0
  91. package/src/engine/gateway/stages/group-gate-stage.ts +223 -0
  92. package/src/engine/gateway/stages/index.ts +18 -0
  93. package/src/engine/gateway/stages/quote-stage.ts +113 -0
  94. package/src/engine/gateway/stages/refidx-stage.ts +62 -0
  95. package/src/engine/gateway/stages/stub-contexts.ts +77 -0
  96. package/src/engine/gateway/types.ts +230 -0
  97. package/src/engine/gateway/typing-keepalive.ts +102 -0
  98. package/src/engine/gateway/ws-client.ts +16 -0
  99. package/src/engine/group/activation.ts +88 -0
  100. package/src/engine/group/history.ts +321 -0
  101. package/src/engine/group/mention.ts +114 -0
  102. package/src/engine/group/message-gating.ts +108 -0
  103. package/src/engine/messaging/decode-media-path.ts +82 -0
  104. package/src/engine/messaging/media-source.ts +210 -0
  105. package/src/engine/messaging/media-type-detect.ts +27 -0
  106. package/src/engine/messaging/outbound-audio-port.ts +38 -0
  107. package/src/engine/messaging/outbound-deliver.ts +810 -0
  108. package/src/engine/messaging/outbound-media-send.ts +658 -0
  109. package/src/engine/messaging/outbound-reply.ts +27 -0
  110. package/src/engine/messaging/outbound-result-helpers.ts +54 -0
  111. package/src/engine/messaging/outbound-types.ts +47 -0
  112. package/src/engine/messaging/outbound.ts +485 -0
  113. package/src/engine/messaging/reply-dispatcher.ts +597 -0
  114. package/src/engine/messaging/reply-limiter.ts +164 -0
  115. package/src/engine/messaging/sender.ts +741 -0
  116. package/src/engine/messaging/streaming-c2c.ts +1192 -0
  117. package/src/engine/messaging/streaming-media-send.ts +544 -0
  118. package/src/engine/messaging/target-parser.ts +104 -0
  119. package/src/engine/ref/format-message-ref.ts +142 -0
  120. package/src/engine/ref/format-ref-entry.ts +27 -0
  121. package/src/engine/ref/store.ts +211 -0
  122. package/src/engine/ref/types.ts +27 -0
  123. package/src/engine/session/known-users.ts +138 -0
  124. package/src/engine/session/session-store.ts +207 -0
  125. package/src/engine/tools/channel-api.ts +244 -0
  126. package/src/engine/tools/remind-logic.ts +377 -0
  127. package/src/engine/types.ts +313 -0
  128. package/src/engine/utils/attachment-tags.ts +174 -0
  129. package/src/engine/utils/audio.ts +525 -0
  130. package/src/engine/utils/data-paths.ts +38 -0
  131. package/src/engine/utils/diagnostics.ts +93 -0
  132. package/src/engine/utils/file-utils.ts +215 -0
  133. package/src/engine/utils/format.ts +70 -0
  134. package/src/engine/utils/image-size.ts +249 -0
  135. package/src/engine/utils/log.ts +77 -0
  136. package/src/engine/utils/media-tags.ts +177 -0
  137. package/src/engine/utils/payload.ts +157 -0
  138. package/src/engine/utils/platform.ts +265 -0
  139. package/src/engine/utils/request-context.ts +60 -0
  140. package/src/engine/utils/string-normalize.ts +91 -0
  141. package/src/engine/utils/stt.ts +103 -0
  142. package/src/engine/utils/text-parsing.ts +155 -0
  143. package/src/engine/utils/upload-cache.ts +96 -0
  144. package/src/engine/utils/voice-text.ts +15 -0
  145. package/src/exec-approvals.ts +237 -0
  146. package/src/qqbot-test-support.ts +29 -0
  147. package/src/secret-contract.ts +82 -0
  148. package/src/types.ts +210 -0
  149. package/tsconfig.json +16 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Request-level context using AsyncLocalStorage.
3
+ *
4
+ * Provides ambient context (accountId, target openid, chat type, etc.)
5
+ * throughout the request lifecycle without explicit parameter threading.
6
+ *
7
+ * Gateway establishes the scope around each inbound message via
8
+ * `runWithRequestContext()`; any async code within that scope (including
9
+ * AI agent calls and tool `execute` callbacks) can retrieve the current
10
+ * request via `getRequestContext()` without racing with concurrent
11
+ * inbound messages.
12
+ *
13
+ * This is a pure Node.js module with zero framework dependencies,
14
+ * making it trivially portable between the built-in and standalone
15
+ * versions of QQBot.
16
+ */
17
+
18
+ import { AsyncLocalStorage } from "node:async_hooks";
19
+
20
+ /** Context values available during one inbound message handling cycle. */
21
+ interface RequestContext {
22
+ /** The account ID handling this request. */
23
+ accountId: string;
24
+ /**
25
+ * Fully qualified delivery target, e.g. `qqbot:c2c:<openid>` or
26
+ * `qqbot:group:<openid>`. This is what downstream code (e.g. the
27
+ * `qqbot_remind` tool building a cron job) uses verbatim.
28
+ */
29
+ target?: string;
30
+ /** The target openid (C2C) or group openid (group). */
31
+ targetId?: string;
32
+ /** Chat type of the originating event. */
33
+ chatType?: "c2c" | "group" | "guild" | "dm" | "channel";
34
+ }
35
+
36
+ const store = new AsyncLocalStorage<RequestContext>();
37
+
38
+ /**
39
+ * Execute an async function with request-scoped context.
40
+ *
41
+ * All code running within `fn` (including nested async calls) can
42
+ * retrieve the context via `getRequestContext()`.
43
+ *
44
+ * @param ctx - The context to attach to this request.
45
+ * @param fn - The async function to run within the context.
46
+ * @returns The return value of `fn`.
47
+ */
48
+ export function runWithRequestContext<T>(ctx: RequestContext, fn: () => T): T {
49
+ return store.run(ctx, fn);
50
+ }
51
+
52
+ /**
53
+ * Retrieve the current request context.
54
+ *
55
+ * Returns `undefined` when called outside of a `runWithRequestContext`
56
+ * scope.
57
+ */
58
+ export function getRequestContext(): RequestContext | undefined {
59
+ return store.getStore();
60
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * String normalization and record-coercion helpers.
3
+ *
4
+ * These are self-contained re-implementations of the functions that
5
+ * the plugin previously imported from broad SDK text barrels
6
+ * and shared record/string coercion helpers.
7
+ *
8
+ * core/ modules use these instead of importing plugin-sdk, keeping the
9
+ * shared layer portable between the built-in and standalone versions.
10
+ */
11
+
12
+ // ---- String coercion ----
13
+
14
+ /** Return the trimmed string or `null` when the value is not a non-empty string. */
15
+ function normalizeNullableString(value: unknown): string | null {
16
+ if (typeof value !== "string") {
17
+ return null;
18
+ }
19
+ const trimmed = value.trim();
20
+ return trimmed ? trimmed : null;
21
+ }
22
+
23
+ /** Return the trimmed string or `undefined` when the value is not a non-empty string. */
24
+ export function normalizeOptionalString(value: unknown): string | undefined {
25
+ return normalizeNullableString(value) ?? undefined;
26
+ }
27
+
28
+ /**
29
+ * Stringify then normalize. Accepts `string | number | boolean | bigint`.
30
+ * Returns `undefined` for objects, arrays, null, and undefined.
31
+ */
32
+ export function normalizeStringifiedOptionalString(value: unknown): string | undefined {
33
+ if (typeof value === "string") {
34
+ return normalizeOptionalString(value);
35
+ }
36
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
37
+ return normalizeOptionalString(String(value));
38
+ }
39
+ return undefined;
40
+ }
41
+
42
+ /** Return the trimmed lowercase string or `undefined`. */
43
+ export function normalizeOptionalLowercaseString(value: unknown): string | undefined {
44
+ return normalizeOptionalString(value)?.toLowerCase();
45
+ }
46
+
47
+ /** Return the trimmed lowercase string or `""`. */
48
+ export function normalizeLowercaseStringOrEmpty(value: unknown): string {
49
+ return normalizeOptionalLowercaseString(value) ?? "";
50
+ }
51
+
52
+ // ---- Record coercion ----
53
+
54
+ /** Coerce a value into a `Record<string, unknown>` or `undefined`. */
55
+ export function asOptionalObjectRecord(value: unknown): Record<string, unknown> | undefined {
56
+ return value && typeof value === "object" ? (value as Record<string, unknown>) : undefined;
57
+ }
58
+
59
+ /** Read a string field from a record. */
60
+ export function readStringField(
61
+ record: Record<string, unknown> | null | undefined,
62
+ key: string,
63
+ ): string | undefined {
64
+ const v = record?.[key];
65
+ return typeof v === "string" ? v : undefined;
66
+ }
67
+
68
+ // ---- Filename normalization ----
69
+
70
+ /**
71
+ * Normalize filenames into a UTF-8 form that the QQ Bot API accepts reliably.
72
+ *
73
+ * Decodes percent-escaped names, converts Unicode to NFC, and strips
74
+ * ASCII control characters.
75
+ */
76
+ export function sanitizeFileName(name: string): string {
77
+ if (!name) {
78
+ return name;
79
+ }
80
+ let result = name.trim();
81
+ if (result.includes("%")) {
82
+ try {
83
+ result = decodeURIComponent(result);
84
+ } catch {
85
+ // Keep the raw value if it is not valid percent-encoding.
86
+ }
87
+ }
88
+ result = result.normalize("NFC");
89
+ result = result.replace(/\p{Cc}/gu, "");
90
+ return result;
91
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * OpenAI-compatible STT (Speech-to-Text) configuration and transcription.
3
+ *
4
+ * Migrated from `src/stt.ts` — uses core/utils/string-normalize instead
5
+ * of broad SDK text barrels.
6
+ */
7
+
8
+ import * as fs from "node:fs";
9
+ import path from "node:path";
10
+ import { mimeTypeFromFilePath } from "autobot/plugin-sdk/media-mime";
11
+ import { fetchWithSsrFGuard } from "autobot/plugin-sdk/ssrf-runtime";
12
+ import {
13
+ normalizeOptionalString,
14
+ asOptionalObjectRecord as asRecord,
15
+ readStringField as readString,
16
+ sanitizeFileName,
17
+ } from "./string-normalize.js";
18
+
19
+ interface STTConfig {
20
+ baseUrl: string;
21
+ apiKey: string;
22
+ model: string;
23
+ }
24
+
25
+ /** Resolve the STT configuration from the nested config object. */
26
+ export function resolveSTTConfig(cfg: Record<string, unknown>): STTConfig | null {
27
+ const channels = asRecord(cfg.channels);
28
+ const qqbot = asRecord(channels?.qqbot);
29
+ const channelStt = asRecord(qqbot?.stt);
30
+ const models = asRecord(cfg.models);
31
+ const providers = asRecord(models?.providers);
32
+
33
+ // Prefer plugin-specific STT config.
34
+ if (channelStt && channelStt.enabled !== false) {
35
+ const providerId = readString(channelStt, "provider") ?? "openai";
36
+ const providerCfg = asRecord(providers?.[providerId]);
37
+ const baseUrl = readString(channelStt, "baseUrl") ?? readString(providerCfg, "baseUrl");
38
+ const apiKey = readString(channelStt, "apiKey") ?? readString(providerCfg, "apiKey");
39
+ const model = readString(channelStt, "model") ?? "whisper-1";
40
+ if (baseUrl && apiKey) {
41
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
42
+ }
43
+ }
44
+
45
+ // Fall back to framework-level audio model config.
46
+ const tools = asRecord(cfg.tools);
47
+ const media = asRecord(tools?.media);
48
+ const audio = asRecord(media?.audio);
49
+ const audioModels = audio?.models;
50
+ const audioModelEntry = Array.isArray(audioModels) ? asRecord(audioModels[0]) : undefined;
51
+ if (audioModelEntry) {
52
+ const providerId = readString(audioModelEntry, "provider") ?? "openai";
53
+ const providerCfg = asRecord(providers?.[providerId]);
54
+ const baseUrl = readString(audioModelEntry, "baseUrl") ?? readString(providerCfg, "baseUrl");
55
+ const apiKey = readString(audioModelEntry, "apiKey") ?? readString(providerCfg, "apiKey");
56
+ const model = readString(audioModelEntry, "model") ?? "whisper-1";
57
+ if (baseUrl && apiKey) {
58
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
59
+ }
60
+ }
61
+
62
+ return null;
63
+ }
64
+
65
+ /** Send audio to an OpenAI-compatible STT endpoint and return the transcript. */
66
+ export async function transcribeAudio(
67
+ audioPath: string,
68
+ cfg: Record<string, unknown>,
69
+ ): Promise<string | null> {
70
+ const sttCfg = resolveSTTConfig(cfg);
71
+ if (!sttCfg) {
72
+ return null;
73
+ }
74
+
75
+ const fileBuffer = fs.readFileSync(audioPath);
76
+ const fileName = sanitizeFileName(path.basename(audioPath));
77
+ const mime = mimeTypeFromFilePath(fileName) ?? "application/octet-stream";
78
+
79
+ const form = new FormData();
80
+ form.append("file", new Blob([fileBuffer], { type: mime }), fileName);
81
+ form.append("model", sttCfg.model);
82
+
83
+ const { response: resp, release } = await fetchWithSsrFGuard({
84
+ url: `${sttCfg.baseUrl}/audio/transcriptions`,
85
+ auditContext: "qqbot-stt",
86
+ init: {
87
+ method: "POST",
88
+ headers: { Authorization: `Bearer ${sttCfg.apiKey}` },
89
+ body: form,
90
+ },
91
+ });
92
+ try {
93
+ if (!resp.ok) {
94
+ const detail = await resp.text().catch(() => "");
95
+ throw new Error(`STT failed (HTTP ${resp.status}): ${detail.slice(0, 300)}`);
96
+ }
97
+
98
+ const result = (await resp.json()) as { text?: string };
99
+ return normalizeOptionalString(result.text) ?? null;
100
+ } finally {
101
+ await release();
102
+ }
103
+ }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Text parsing utilities — zero external dependency.
3
+ *
4
+ * Contains pure functions for message text processing.
5
+ */
6
+
7
+ import type { RefAttachmentSummary } from "../ref/types.js";
8
+
9
+ // ============ Internal markers ============
10
+
11
+ const INTERNAL_MARKER_RE = /\[internal:?\s*[^\]]*\]|\[debug:?\s*[^\]]*\]|\[system:?\s*[^\]]*\]/gi;
12
+
13
+ /** Remove internal markers like `[internal:...]`, `[debug:...]`, `[system:...]`. */
14
+ export function filterInternalMarkers(text: string | undefined | null): string {
15
+ if (!text) {
16
+ return "";
17
+ }
18
+ return text.replace(INTERNAL_MARKER_RE, "").trim();
19
+ }
20
+
21
+ // ============ Ref indices ============
22
+
23
+ /** QQ 引用(回复)消息类型常量。 */
24
+ export const MSG_TYPE_QUOTE = 103;
25
+
26
+ /**
27
+ * Parse message_scene.ext to extract refMsgIdx and msgIdx.
28
+ *
29
+ * Supports both ext prefix formats:
30
+ * - `ref_msg_idx=` / `msg_idx=` (platform native format)
31
+ * - `refMsgIdx:` / `msgIdx:` (legacy internal format)
32
+ *
33
+ * When `messageType` equals `MSG_TYPE_QUOTE` (103) and `msgElements` is
34
+ * provided, `msgElements[0].msg_idx` takes precedence over the ext-parsed
35
+ * `refMsgIdx` value — the element-level index is more authoritative for
36
+ * quote messages.
37
+ */
38
+ export function parseRefIndices(
39
+ ext?: string[],
40
+ messageType?: number,
41
+ msgElements?: Array<{ msg_idx?: string }>,
42
+ ): { refMsgIdx?: string; msgIdx?: string } {
43
+ let refMsgIdx: string | undefined;
44
+ let msgIdx: string | undefined;
45
+
46
+ if (ext && ext.length > 0) {
47
+ for (const item of ext) {
48
+ if (typeof item !== "string") {
49
+ continue;
50
+ }
51
+ // Platform native format: ref_msg_idx= / msg_idx=
52
+ if (item.startsWith("ref_msg_idx=")) {
53
+ refMsgIdx = item.slice("ref_msg_idx=".length).trim();
54
+ } else if (item.startsWith("msg_idx=")) {
55
+ msgIdx = item.slice("msg_idx=".length).trim();
56
+ }
57
+ // Legacy internal format: refMsgIdx: / msgIdx:
58
+ else if (item.startsWith("refMsgIdx:")) {
59
+ refMsgIdx = item.slice("refMsgIdx:".length).trim();
60
+ } else if (item.startsWith("msgIdx:")) {
61
+ msgIdx = item.slice("msgIdx:".length).trim();
62
+ }
63
+ }
64
+ }
65
+
66
+ // For quote messages, msg_elements[0].msg_idx is more authoritative.
67
+ if (messageType === MSG_TYPE_QUOTE) {
68
+ const refElement = msgElements?.[0];
69
+ if (refElement?.msg_idx) {
70
+ refMsgIdx = refElement.msg_idx;
71
+ }
72
+ }
73
+
74
+ return { refMsgIdx, msgIdx };
75
+ }
76
+
77
+ // ============ Face tags ============
78
+
79
+ const MAX_FACE_EXT_BYTES = 64 * 1024;
80
+
81
+ /** Estimate Base64 decoded byte size (replaces plugin-sdk estimateBase64DecodedBytes). */
82
+ function estimateBase64Size(base64: string): number {
83
+ const len = base64.length;
84
+ const padding = base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0;
85
+ return Math.ceil((len * 3) / 4) - padding;
86
+ }
87
+
88
+ /** Replace QQ face tags with readable text labels. */
89
+ export function parseFaceTags(text: string | undefined | null): string {
90
+ if (!text) {
91
+ return "";
92
+ }
93
+
94
+ return text.replace(/<faceType=\d+,faceId="[^"]*",ext="([^"]*)">/g, (_match, ext: string) => {
95
+ try {
96
+ if (estimateBase64Size(ext) > MAX_FACE_EXT_BYTES) {
97
+ return "[Emoji: unknown emoji]";
98
+ }
99
+ const decoded = Buffer.from(ext, "base64").toString("utf-8");
100
+ const parsed = JSON.parse(decoded);
101
+ const faceName = parsed.text || "unknown emoji";
102
+ return `[Emoji: ${faceName}]`;
103
+ } catch {
104
+ return _match;
105
+ }
106
+ });
107
+ }
108
+
109
+ // ============ Attachment summaries ============
110
+
111
+ /** Lowercase a string safely (replaces plugin-sdk normalizeLowercaseStringOrEmpty). */
112
+ function lc(s: string | undefined | null): string {
113
+ return (s ?? "").toLowerCase();
114
+ }
115
+
116
+ /** Build attachment summaries for ref-index caching. */
117
+ export function buildAttachmentSummaries(
118
+ attachments?: Array<{
119
+ content_type: string;
120
+ url: string;
121
+ filename?: string;
122
+ voice_wav_url?: string;
123
+ }>,
124
+ localPaths?: Array<string | null>,
125
+ ): RefAttachmentSummary[] | undefined {
126
+ if (!attachments || attachments.length === 0) {
127
+ return undefined;
128
+ }
129
+
130
+ return attachments.map((att, idx) => {
131
+ const ct = lc(att.content_type);
132
+ let type: RefAttachmentSummary["type"] = "unknown";
133
+ if (ct.startsWith("image/")) {
134
+ type = "image";
135
+ } else if (
136
+ ct === "voice" ||
137
+ ct.startsWith("audio/") ||
138
+ ct.includes("silk") ||
139
+ ct.includes("amr")
140
+ ) {
141
+ type = "voice";
142
+ } else if (ct.startsWith("video/")) {
143
+ type = "video";
144
+ } else if (ct.startsWith("application/") || ct.startsWith("text/")) {
145
+ type = "file";
146
+ }
147
+
148
+ return {
149
+ type,
150
+ filename: att.filename,
151
+ contentType: att.content_type,
152
+ localPath: localPaths?.[idx] ?? undefined,
153
+ };
154
+ });
155
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Cache `file_info` values returned by the QQ Bot API so identical uploads can be reused
3
+ * before the server-side TTL expires.
4
+ */
5
+
6
+ import * as crypto from "node:crypto";
7
+ import type { ChatScope } from "../types.js";
8
+ import { debugLog } from "./log.js";
9
+
10
+ interface CacheEntry {
11
+ fileInfo: string;
12
+ fileUuid: string;
13
+ expiresAt: number;
14
+ }
15
+
16
+ const cache = new Map<string, CacheEntry>();
17
+ const MAX_CACHE_SIZE = 500;
18
+
19
+ /** Compute an MD5 hash used as part of the cache key. */
20
+ export function computeFileHash(data: string | Buffer): string {
21
+ const content = typeof data === "string" ? data : data;
22
+ return crypto.createHash("md5").update(content).digest("hex");
23
+ }
24
+
25
+ /** Build the in-memory cache key. */
26
+ function buildCacheKey(
27
+ contentHash: string,
28
+ scope: string,
29
+ targetId: string,
30
+ fileType: number,
31
+ ): string {
32
+ return `${contentHash}:${scope}:${targetId}:${fileType}`;
33
+ }
34
+
35
+ /** Look up a cached `file_info` value. */
36
+ export function getCachedFileInfo(
37
+ contentHash: string,
38
+ scope: ChatScope,
39
+ targetId: string,
40
+ fileType: number,
41
+ ): string | null {
42
+ const key = buildCacheKey(contentHash, scope, targetId, fileType);
43
+ const entry = cache.get(key);
44
+
45
+ if (!entry) {
46
+ return null;
47
+ }
48
+
49
+ if (Date.now() >= entry.expiresAt) {
50
+ cache.delete(key);
51
+ return null;
52
+ }
53
+
54
+ debugLog(`[upload-cache] Cache HIT: key=${key.slice(0, 40)}..., fileUuid=${entry.fileUuid}`);
55
+ return entry.fileInfo;
56
+ }
57
+
58
+ /** Store an upload result in the cache. */
59
+ export function setCachedFileInfo(
60
+ contentHash: string,
61
+ scope: ChatScope,
62
+ targetId: string,
63
+ fileType: number,
64
+ fileInfo: string,
65
+ fileUuid: string,
66
+ ttl: number,
67
+ ): void {
68
+ if (cache.size >= MAX_CACHE_SIZE) {
69
+ const now = Date.now();
70
+ for (const [k, v] of cache) {
71
+ if (now >= v.expiresAt) {
72
+ cache.delete(k);
73
+ }
74
+ }
75
+ if (cache.size >= MAX_CACHE_SIZE) {
76
+ const keys = Array.from(cache.keys());
77
+ for (let i = 0; i < keys.length / 2; i++) {
78
+ cache.delete(keys[i]);
79
+ }
80
+ }
81
+ }
82
+
83
+ const key = buildCacheKey(contentHash, scope, targetId, fileType);
84
+ const safetyMargin = 60;
85
+ const effectiveTtl = Math.max(ttl - safetyMargin, 10);
86
+
87
+ cache.set(key, {
88
+ fileInfo,
89
+ fileUuid,
90
+ expiresAt: Date.now() + effectiveTtl * 1000,
91
+ });
92
+
93
+ debugLog(
94
+ `[upload-cache] Cache SET: key=${key.slice(0, 40)}..., ttl=${effectiveTtl}s, uuid=${fileUuid}`,
95
+ );
96
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Voice transcript formatting utility.
3
+ *
4
+ * Zero external dependencies — pure string formatting.
5
+ */
6
+
7
+ /** Format voice transcripts into user-visible text. */
8
+ export function formatVoiceText(transcripts: string[]): string {
9
+ if (transcripts.length === 0) {
10
+ return "";
11
+ }
12
+ return transcripts.length === 1
13
+ ? `[Voice message] ${transcripts[0]}`
14
+ : transcripts.map((t, i) => `[Voice ${i + 1}] ${t}`).join("\n");
15
+ }