Skip to content

Commit 8a7dab0

Browse files
authored
feat(qq): configure group slash command owners
* feat(qq): configure group slash command owners * fix(gateway): preserve tool limits with legacy inbound messages
1 parent d0c454e commit 8a7dab0

14 files changed

Lines changed: 362 additions & 14 deletions

File tree

‎dashboard/src/api/types/channel.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export interface QQConfig extends BaseChannelConfig {
5656
history_limit?: number;
5757
history_ttl_seconds?: number;
5858
clear_after_reply?: boolean;
59+
owner_commands_only?: boolean;
60+
owner_tools_only?: boolean;
61+
owner_ids?: string[];
5962
groups?: Record<string, Record<string, unknown>>;
6063
};
6164
}

‎dashboard/src/locales/en.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,6 +3167,14 @@
31673167
"qqQrSuccess": "QQ Bot bound successfully",
31683168
"qqQrFailed": "QQ Bot binding failed",
31693169
"qqGroupPolicyTitle": "Group chat policy",
3170+
"qqGroupContextEnabled": "Enable group message context",
3171+
"qqGroupOwnerCommandsOnly": "Only owners can use / commands in group chats",
3172+
"qqGroupOwnerCommandsOnlyDesc": "Only allowlisted members can use Octop commands such as /model, /new and /delete in groups. Normal chat is unaffected. Turning off group context keeps this protection enabled.",
3173+
"qqGroupOwnerToolsOnly": "Only owners can use tools in group chats",
3174+
"qqGroupOwnerToolsOnlyDesc": "Only allowlisted members can invoke agent tools; other members can still chat. Turning off group context keeps this restriction enabled.",
3175+
"qqGroupOwnerIds": "Owner allowlist",
3176+
"qqGroupOwnerIdsHelp": "Enter QQ group member IDs (not QQ numbers). Leaving this empty prevents all group members from using protected / commands and tools.",
3177+
"qqGroupOwnerIdsPlaceholder": "Enter a member ID and press Enter",
31703178
"qqGroupPolicyDescription": "Control which group messages the bot can use and when they start an agent reply.",
31713179
"qqGroupVisibility": "Visible group messages",
31723180
"qqGroupVisibilityAuto": "Auto",

‎dashboard/src/locales/zh.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,6 +3167,14 @@
31673167
"qqQrSuccess": "QQ 机器人绑定成功",
31683168
"qqQrFailed": "QQ 机器人绑定失败",
31693169
"qqGroupPolicyTitle": "群聊策略",
3170+
"qqGroupContextEnabled": "启用群消息上下文",
3171+
"qqGroupOwnerCommandsOnly": "仅所有者可在群聊中使用 / 命令",
3172+
"qqGroupOwnerCommandsOnlyDesc": "仅白名单成员可在群内使用 /model、/new、/delete 等 Octop 命令,普通聊天不受影响。关闭群消息上下文不会关闭此保护。",
3173+
"qqGroupOwnerToolsOnly": "仅所有者可在群聊中调用工具",
3174+
"qqGroupOwnerToolsOnlyDesc": "仅白名单成员可以调用专家工具,其他成员仍可聊天。关闭群消息上下文不会关闭此限制。",
3175+
"qqGroupOwnerIds": "所有者白名单",
3176+
"qqGroupOwnerIdsHelp": "请填写 QQ 群成员 ID(不是 QQ 号),留空则所有群成员都无法使用受保护的 / 命令和工具。",
3177+
"qqGroupOwnerIdsPlaceholder": "输入成员 ID,按回车添加",
31703178
"qqGroupPolicyDescription": "控制机器人能看到哪些群消息,以及什么时候启动专家回复。",
31713179
"qqGroupVisibility": "群消息可见范围",
31723180
"qqGroupVisibilityAuto": "自动适配",

‎dashboard/src/pages/Agent/Channels/ChannelsPanel.test.tsx‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* - saving a new channel sends exactly one POST (no PATCH churn)
1313
*/
1414

15+
import "@testing-library/jest-dom/vitest";
1516
import { describe, it, expect, vi, beforeEach } from "vitest";
1617
import { render, screen, waitFor } from "@testing-library/react";
1718
import userEvent from "@testing-library/user-event";
@@ -305,6 +306,137 @@ describe("<ChannelsPanel /> create-flow default", () => {
305306
expectBefore(displaySettings, groupPolicy);
306307
});
307308

309+
async function openQqEditDrawer(groupContext?: Record<string, unknown>) {
310+
const row = { id: "qq-policy", kind: "qq", name: "qq", enabled: true };
311+
api.mockImplementation(async (url, init) => {
312+
if (init?.method === "PATCH") return row;
313+
if (url.endsWith("/qq-policy")) {
314+
return {
315+
...row,
316+
config: {
317+
app_id: "offline-app",
318+
secret: "offline-secret",
319+
group_context: groupContext,
320+
},
321+
};
322+
}
323+
return [row];
324+
});
325+
render(<ChannelsPanel agentId="ag1" />);
326+
await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]);
327+
}
328+
329+
it.each([undefined, true, false])(
330+
"round-trips QQ command protection %s and the owner allowlist",
331+
async (ownerOnly) => {
332+
await openQqEditDrawer({
333+
enabled: true,
334+
owner_ids: ["owner-1"],
335+
...(ownerOnly === undefined ? {} : { owner_commands_only: ownerOnly }),
336+
});
337+
const toggle = await screen.findByLabelText(
338+
"channels.qqGroupOwnerCommandsOnly",
339+
);
340+
await waitFor(() => {
341+
expect(toggle.getAttribute("aria-checked")).toBe(
342+
String(ownerOnly ?? true),
343+
);
344+
expect(screen.getByText("owner-1")).toBeInTheDocument();
345+
});
346+
const owners = screen.getByRole("combobox", {
347+
name: "channels.qqGroupOwnerIds",
348+
});
349+
if (ownerOnly === false) expect(owners).toBeDisabled();
350+
else expect(owners).toBeEnabled();
351+
await userEvent.click(
352+
screen.getByRole("button", { name: "common.save" }),
353+
);
354+
await waitFor(() => {
355+
const patch = api.mock.calls.find(
356+
([, init]) => init?.method === "PATCH",
357+
);
358+
expect(patch).toBeDefined();
359+
expect(
360+
JSON.parse(String(patch![1]!.body)).config.group_context,
361+
).toMatchObject({
362+
enabled: true,
363+
owner_commands_only: ownerOnly ?? true,
364+
owner_ids: ["owner-1"],
365+
});
366+
});
367+
},
368+
);
369+
370+
it("keeps the shared allowlist enabled when only tool protection is on", async () => {
371+
await openQqEditDrawer({
372+
enabled: true,
373+
owner_commands_only: false,
374+
owner_tools_only: true,
375+
owner_ids: ["owner-1"],
376+
});
377+
const owners = await screen.findByRole("combobox", {
378+
name: "channels.qqGroupOwnerIds",
379+
});
380+
expect(owners).toBeEnabled();
381+
await userEvent.click(
382+
screen.getByLabelText("channels.qqGroupOwnerToolsOnly"),
383+
);
384+
expect(owners).toBeDisabled();
385+
await userEvent.click(screen.getByRole("button", { name: "common.save" }));
386+
await waitFor(() => {
387+
const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH");
388+
expect(
389+
JSON.parse(String(patch![1]!.body)).config.group_context,
390+
).toMatchObject({
391+
owner_commands_only: false,
392+
owner_tools_only: false,
393+
owner_ids: ["owner-1"],
394+
});
395+
});
396+
});
397+
398+
it("enables tool protection for new QQ channels", async () => {
399+
render(<ChannelsPanel agentId="ag1" />);
400+
await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]);
401+
expect(
402+
await screen.findByLabelText("channels.qqGroupOwnerToolsOnly"),
403+
).toBeChecked();
404+
});
405+
406+
it("hides QQ command controls while group context is off", async () => {
407+
await openQqEditDrawer({ enabled: false, owner_ids: ["stored-owner"] });
408+
expect(
409+
screen.queryByLabelText("channels.qqGroupOwnerCommandsOnly"),
410+
).not.toBeInTheDocument();
411+
expect(
412+
screen.queryByLabelText("channels.qqGroupOwnerToolsOnly"),
413+
).not.toBeInTheDocument();
414+
expect(
415+
screen.queryByRole("combobox", { name: "channels.qqGroupOwnerIds" }),
416+
).not.toBeInTheDocument();
417+
418+
await userEvent.click(
419+
screen.getByLabelText("channels.qqGroupContextEnabled"),
420+
);
421+
const toggle = await screen.findByLabelText(
422+
"channels.qqGroupOwnerCommandsOnly",
423+
);
424+
expect(toggle).toBeChecked();
425+
expect(await screen.findByText("stored-owner")).toBeInTheDocument();
426+
await userEvent.click(screen.getByRole("button", { name: "common.save" }));
427+
await waitFor(() => {
428+
const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH");
429+
expect(patch).toBeDefined();
430+
expect(
431+
JSON.parse(String(patch![1]!.body)).config.group_context,
432+
).toMatchObject({
433+
enabled: true,
434+
owner_commands_only: true,
435+
owner_ids: ["stored-owner"],
436+
});
437+
});
438+
});
439+
308440
it("edits a CLI-created QQ channel whose secret is stored as client_secret", async () => {
309441
const row = {
310442
id: "qq-channel-2",

‎dashboard/src/pages/Agent/Channels/ChannelsPanel.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ export default function ChannelsPanel({ agentId }: ChannelsPanelProps) {
195195
),
196196
...(kind === "discord" ? { allow_all_channels: true } : {}),
197197
...(kind === "qq"
198-
? { group_context: { ...DEFAULT_QQ_GROUP_CONTEXT_CONFIG } }
198+
? {
199+
group_context: {
200+
...DEFAULT_QQ_GROUP_CONTEXT_CONFIG,
201+
owner_tools_only: true,
202+
},
203+
}
199204
: {}),
200205
};
201206
setSecretsSet([]);

‎dashboard/src/pages/Agent/Channels/components/ChannelDrawer.tsx‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ function QqGroupContextPolicyFields({
368368
["group_context", "activation"],
369369
form,
370370
);
371+
const watchedOwnerCommandsOnly = Form.useWatch(
372+
["group_context", "owner_commands_only"],
373+
form,
374+
);
375+
const watchedOwnerToolsOnly = Form.useWatch(
376+
["group_context", "owner_tools_only"],
377+
form,
378+
);
371379
const policy = normalizeQqGroupContextConfig(
372380
form.getFieldValue("group_context"),
373381
);
@@ -377,6 +385,14 @@ function QqGroupContextPolicyFields({
377385
policy.visibility) as QqGroupVisibility;
378386
const activation = (watchedActivation ??
379387
policy.activation) as QqGroupActivation;
388+
const ownerCommandsOnly =
389+
typeof watchedOwnerCommandsOnly === "boolean"
390+
? watchedOwnerCommandsOnly
391+
: policy.owner_commands_only;
392+
const ownerToolsOnly =
393+
typeof watchedOwnerToolsOnly === "boolean"
394+
? watchedOwnerToolsOnly
395+
: policy.owner_tools_only;
380396

381397
const updatePolicy = (patch: Partial<QqGroupContextConfig>) => {
382398
form.setFieldValue("group_context", {
@@ -396,7 +412,11 @@ function QqGroupContextPolicyFields({
396412

397413
return (
398414
<div className={styles.qqGroupPolicy}>
399-
<div className={styles.qqGroupPolicyHeader}>
415+
<div
416+
className={`${styles.qqGroupPolicyHeader} ${
417+
enabled ? "" : styles.qqGroupPolicyHeaderCollapsed
418+
}`}
419+
>
400420
<div>
401421
<div className={styles.qqGroupPolicyTitle}>
402422
{t("channels.qqGroupPolicyTitle")}
@@ -410,7 +430,7 @@ function QqGroupContextPolicyFields({
410430
valuePropName="checked"
411431
noStyle
412432
>
413-
<Switch />
433+
<Switch aria-label={t("channels.qqGroupContextEnabled")} />
414434
</Form.Item>
415435
</div>
416436

@@ -479,9 +499,44 @@ function QqGroupContextPolicyFields({
479499
</Form.Item>
480500
</div>
481501

502+
<Form.Item
503+
name={["group_context", "owner_commands_only"]}
504+
label={t("channels.qqGroupOwnerCommandsOnly")}
505+
tooltip={t("channels.qqGroupOwnerCommandsOnlyDesc")}
506+
valuePropName="checked"
507+
>
508+
<Switch />
509+
</Form.Item>
510+
<Form.Item
511+
name={["group_context", "owner_tools_only"]}
512+
label={t("channels.qqGroupOwnerToolsOnly")}
513+
tooltip={t("channels.qqGroupOwnerToolsOnlyDesc")}
514+
valuePropName="checked"
515+
>
516+
<Switch />
517+
</Form.Item>
518+
<Form.Item
519+
name={["group_context", "owner_ids"]}
520+
label={t("channels.qqGroupOwnerIds")}
521+
extra={
522+
<div className={styles.qqGroupPolicyDescription}>
523+
{t("channels.qqGroupOwnerIdsHelp")}
524+
</div>
525+
}
526+
>
527+
<Select
528+
mode="tags"
529+
open={false}
530+
suffixIcon={null}
531+
disabled={!ownerCommandsOnly && !ownerToolsOnly}
532+
tokenSeparators={[",", ",", " ", "\n"]}
533+
placeholder={t("channels.qqGroupOwnerIdsPlaceholder")}
534+
/>
535+
</Form.Item>
482536
<Form.Item
483537
name={["group_context", "clear_after_reply"]}
484538
label={t("channels.qqGroupClearAfterReply")}
539+
className={styles.qqGroupPolicyClearAfterReply}
485540
tooltip={t("channels.qqGroupClearAfterReplyDesc")}
486541
valuePropName="checked"
487542
>

‎dashboard/src/pages/Agent/Channels/components/constants.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,37 @@ describe("normalizeQqGroupContextConfig", () => {
9999
});
100100
});
101101

102+
it("protects legacy group configs and preserves explicit opt-out", () => {
103+
expect(normalizeQqGroupContextConfig({ enabled: false })).toMatchObject({
104+
enabled: false,
105+
owner_commands_only: true,
106+
owner_tools_only: false,
107+
owner_ids: [],
108+
});
109+
expect(
110+
normalizeQqGroupContextConfig({ owner_commands_only: false }),
111+
).toMatchObject({ owner_commands_only: false });
112+
expect(
113+
normalizeQqGroupContextConfig({ owner_tools_only: true }),
114+
).toMatchObject({ owner_tools_only: true });
115+
expect(
116+
normalizeQqGroupContextConfig({ owner_tools_only: false }),
117+
).toMatchObject({ owner_tools_only: false });
118+
});
119+
120+
it("normalizes owner IDs without losing per-group authorization overrides", () => {
121+
const groups = { "group-1": { owner_ids: ["group-owner"] } };
122+
expect(
123+
normalizeQqGroupContextConfig({
124+
owner_ids: [" owner-1 ", "owner-2", "owner-1", "", 123],
125+
groups,
126+
}),
127+
).toMatchObject({ owner_ids: ["owner-1", "owner-2"], groups });
128+
expect(
129+
normalizeQqGroupContextConfig({ owner_ids: "owner-1, owner-2\nowner-1" }),
130+
).toMatchObject({ owner_ids: ["owner-1", "owner-2"] });
131+
});
132+
102133
it("forces mention-only visibility to discard passive history", () => {
103134
expect(
104135
normalizeQqGroupContextConfig({

‎dashboard/src/pages/Agent/Channels/components/constants.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,12 @@ export interface QqGroupContextConfig {
200200
history_limit: number;
201201
history_ttl_seconds: number;
202202
clear_after_reply: boolean;
203+
owner_commands_only: boolean;
204+
owner_tools_only: boolean;
205+
owner_ids: string[];
203206
groups?: Record<
204207
string,
205-
Record<string, string | number | boolean | null | undefined>
208+
Record<string, string | string[] | number | boolean | null | undefined>
206209
>;
207210
}
208211

@@ -214,6 +217,9 @@ export const DEFAULT_QQ_GROUP_CONTEXT_CONFIG: QqGroupContextConfig = {
214217
history_limit: 10,
215218
history_ttl_seconds: 300,
216219
clear_after_reply: true,
220+
owner_commands_only: true,
221+
owner_tools_only: false,
222+
owner_ids: [],
217223
};
218224

219225
function isRecord(value: unknown): value is Record<string, unknown> {
@@ -245,6 +251,12 @@ export function normalizeQqGroupContextConfig(
245251
parsed.activation === "always" ? "always" : ("mention" as const);
246252
const rawLimit = Number(parsed.history_limit);
247253
const rawTtl = Number(parsed.history_ttl_seconds);
254+
const rawOwnerIds =
255+
typeof parsed.owner_ids === "string"
256+
? parsed.owner_ids.split(/[\s,,]+/u)
257+
: Array.isArray(parsed.owner_ids)
258+
? parsed.owner_ids
259+
: [];
248260

249261
return {
250262
...parsed,
@@ -270,6 +282,22 @@ export function normalizeQqGroupContextConfig(
270282
typeof parsed.clear_after_reply === "boolean"
271283
? parsed.clear_after_reply
272284
: DEFAULT_QQ_GROUP_CONTEXT_CONFIG.clear_after_reply,
285+
owner_commands_only:
286+
typeof parsed.owner_commands_only === "boolean"
287+
? parsed.owner_commands_only
288+
: DEFAULT_QQ_GROUP_CONTEXT_CONFIG.owner_commands_only,
289+
owner_tools_only:
290+
typeof parsed.owner_tools_only === "boolean"
291+
? parsed.owner_tools_only
292+
: false,
293+
owner_ids: [
294+
...new Set(
295+
rawOwnerIds
296+
.filter((id): id is string => typeof id === "string")
297+
.map((id) => id.trim())
298+
.filter(Boolean),
299+
),
300+
],
273301
};
274302
}
275303

0 commit comments

Comments
 (0)