Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dashboard/src/api/types/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export interface QQConfig extends BaseChannelConfig {
history_limit?: number;
history_ttl_seconds?: number;
clear_after_reply?: boolean;
owner_commands_only?: boolean;
owner_tools_only?: boolean;
owner_ids?: string[];
groups?: Record<string, Record<string, unknown>>;
};
}
Expand Down
8 changes: 8 additions & 0 deletions dashboard/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,14 @@
"qqQrSuccess": "QQ Bot bound successfully",
"qqQrFailed": "QQ Bot binding failed",
"qqGroupPolicyTitle": "Group chat policy",
"qqGroupContextEnabled": "Enable group message context",
"qqGroupOwnerCommandsOnly": "Only owners can use / commands in group chats",
"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.",
"qqGroupOwnerToolsOnly": "Only owners can use tools in group chats",
"qqGroupOwnerToolsOnlyDesc": "Only allowlisted members can invoke agent tools; other members can still chat. Turning off group context keeps this restriction enabled.",
"qqGroupOwnerIds": "Owner allowlist",
"qqGroupOwnerIdsHelp": "Enter QQ group member IDs (not QQ numbers). Leaving this empty prevents all group members from using protected / commands and tools.",
"qqGroupOwnerIdsPlaceholder": "Enter a member ID and press Enter",
"qqGroupPolicyDescription": "Control which group messages the bot can use and when they start an agent reply.",
"qqGroupVisibility": "Visible group messages",
"qqGroupVisibilityAuto": "Auto",
Expand Down
8 changes: 8 additions & 0 deletions dashboard/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,14 @@
"qqQrSuccess": "QQ 机器人绑定成功",
"qqQrFailed": "QQ 机器人绑定失败",
"qqGroupPolicyTitle": "群聊策略",
"qqGroupContextEnabled": "启用群消息上下文",
"qqGroupOwnerCommandsOnly": "仅所有者可在群聊中使用 / 命令",
"qqGroupOwnerCommandsOnlyDesc": "仅白名单成员可在群内使用 /model、/new、/delete 等 Octop 命令,普通聊天不受影响。关闭群消息上下文不会关闭此保护。",
"qqGroupOwnerToolsOnly": "仅所有者可在群聊中调用工具",
"qqGroupOwnerToolsOnlyDesc": "仅白名单成员可以调用专家工具,其他成员仍可聊天。关闭群消息上下文不会关闭此限制。",
"qqGroupOwnerIds": "所有者白名单",
"qqGroupOwnerIdsHelp": "请填写 QQ 群成员 ID(不是 QQ 号),留空则所有群成员都无法使用受保护的 / 命令和工具。",
"qqGroupOwnerIdsPlaceholder": "输入成员 ID,按回车添加",
"qqGroupPolicyDescription": "控制机器人能看到哪些群消息,以及什么时候启动专家回复。",
"qqGroupVisibility": "群消息可见范围",
"qqGroupVisibilityAuto": "自动适配",
Expand Down
132 changes: 132 additions & 0 deletions dashboard/src/pages/Agent/Channels/ChannelsPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* - saving a new channel sends exactly one POST (no PATCH churn)
*/

import "@testing-library/jest-dom/vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
Expand Down Expand Up @@ -202,6 +203,137 @@ describe("<ChannelsPanel /> create-flow default", () => {
expectBefore(displaySettings, groupPolicy);
});

async function openQqEditDrawer(groupContext?: Record<string, unknown>) {
const row = { id: "qq-policy", kind: "qq", name: "qq", enabled: true };
api.mockImplementation(async (url, init) => {
if (init?.method === "PATCH") return row;
if (url.endsWith("/qq-policy")) {
return {
...row,
config: {
app_id: "offline-app",
secret: "offline-secret",
group_context: groupContext,
},
};
}
return [row];
});
render(<ChannelsPanel agentId="ag1" />);
await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]);
}

it.each([undefined, true, false])(
"round-trips QQ command protection %s and the owner allowlist",
async (ownerOnly) => {
await openQqEditDrawer({
enabled: true,
owner_ids: ["owner-1"],
...(ownerOnly === undefined ? {} : { owner_commands_only: ownerOnly }),
});
const toggle = await screen.findByLabelText(
"channels.qqGroupOwnerCommandsOnly",
);
await waitFor(() => {
expect(toggle.getAttribute("aria-checked")).toBe(
String(ownerOnly ?? true),
);
expect(screen.getByText("owner-1")).toBeInTheDocument();
});
const owners = screen.getByRole("combobox", {
name: "channels.qqGroupOwnerIds",
});
if (ownerOnly === false) expect(owners).toBeDisabled();
else expect(owners).toBeEnabled();
await userEvent.click(
screen.getByRole("button", { name: "common.save" }),
);
await waitFor(() => {
const patch = api.mock.calls.find(
([, init]) => init?.method === "PATCH",
);
expect(patch).toBeDefined();
expect(
JSON.parse(String(patch![1]!.body)).config.group_context,
).toMatchObject({
enabled: true,
owner_commands_only: ownerOnly ?? true,
owner_ids: ["owner-1"],
});
});
},
);

it("keeps the shared allowlist enabled when only tool protection is on", async () => {
await openQqEditDrawer({
enabled: true,
owner_commands_only: false,
owner_tools_only: true,
owner_ids: ["owner-1"],
});
const owners = await screen.findByRole("combobox", {
name: "channels.qqGroupOwnerIds",
});
expect(owners).toBeEnabled();
await userEvent.click(
screen.getByLabelText("channels.qqGroupOwnerToolsOnly"),
);
expect(owners).toBeDisabled();
await userEvent.click(screen.getByRole("button", { name: "common.save" }));
await waitFor(() => {
const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH");
expect(
JSON.parse(String(patch![1]!.body)).config.group_context,
).toMatchObject({
owner_commands_only: false,
owner_tools_only: false,
owner_ids: ["owner-1"],
});
});
});

it("enables tool protection for new QQ channels", async () => {
render(<ChannelsPanel agentId="ag1" />);
await userEvent.click((await screen.findAllByText("channels.label_qq"))[0]);
expect(
await screen.findByLabelText("channels.qqGroupOwnerToolsOnly"),
).toBeChecked();
});

it("hides QQ command controls while group context is off", async () => {
await openQqEditDrawer({ enabled: false, owner_ids: ["stored-owner"] });
expect(
screen.queryByLabelText("channels.qqGroupOwnerCommandsOnly"),
).not.toBeInTheDocument();
expect(
screen.queryByLabelText("channels.qqGroupOwnerToolsOnly"),
).not.toBeInTheDocument();
expect(
screen.queryByRole("combobox", { name: "channels.qqGroupOwnerIds" }),
).not.toBeInTheDocument();

await userEvent.click(
screen.getByLabelText("channels.qqGroupContextEnabled"),
);
const toggle = await screen.findByLabelText(
"channels.qqGroupOwnerCommandsOnly",
);
expect(toggle).toBeChecked();
expect(await screen.findByText("stored-owner")).toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: "common.save" }));
await waitFor(() => {
const patch = api.mock.calls.find(([, init]) => init?.method === "PATCH");
expect(patch).toBeDefined();
expect(
JSON.parse(String(patch![1]!.body)).config.group_context,
).toMatchObject({
enabled: true,
owner_commands_only: true,
owner_ids: ["stored-owner"],
});
});
});

it("edits a CLI-created QQ channel whose secret is stored as client_secret", async () => {
const row = {
id: "qq-channel-2",
Expand Down
7 changes: 6 additions & 1 deletion dashboard/src/pages/Agent/Channels/ChannelsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ export default function ChannelsPanel({ agentId }: ChannelsPanelProps) {
...DEFAULT_CHANNEL_DISPLAY_CONFIG,
...(kind === "discord" ? { allow_all_channels: true } : {}),
...(kind === "qq"
? { group_context: { ...DEFAULT_QQ_GROUP_CONTEXT_CONFIG } }
? {
group_context: {
...DEFAULT_QQ_GROUP_CONTEXT_CONFIG,
owner_tools_only: true,
},
}
: {}),
};
setSecretsSet([]);
Expand Down
59 changes: 57 additions & 2 deletions dashboard/src/pages/Agent/Channels/components/ChannelDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ function QqGroupContextPolicyFields({
["group_context", "activation"],
form,
);
const watchedOwnerCommandsOnly = Form.useWatch(
["group_context", "owner_commands_only"],
form,
);
const watchedOwnerToolsOnly = Form.useWatch(
["group_context", "owner_tools_only"],
form,
);
const policy = normalizeQqGroupContextConfig(
form.getFieldValue("group_context"),
);
Expand All @@ -363,6 +371,14 @@ function QqGroupContextPolicyFields({
policy.visibility) as QqGroupVisibility;
const activation = (watchedActivation ??
policy.activation) as QqGroupActivation;
const ownerCommandsOnly =
typeof watchedOwnerCommandsOnly === "boolean"
? watchedOwnerCommandsOnly
: policy.owner_commands_only;
const ownerToolsOnly =
typeof watchedOwnerToolsOnly === "boolean"
? watchedOwnerToolsOnly
: policy.owner_tools_only;

const updatePolicy = (patch: Partial<QqGroupContextConfig>) => {
form.setFieldValue("group_context", {
Expand All @@ -382,7 +398,11 @@ function QqGroupContextPolicyFields({

return (
<div className={styles.qqGroupPolicy}>
<div className={styles.qqGroupPolicyHeader}>
<div
className={`${styles.qqGroupPolicyHeader} ${
enabled ? "" : styles.qqGroupPolicyHeaderCollapsed
}`}
>
<div>
<div className={styles.qqGroupPolicyTitle}>
{t("channels.qqGroupPolicyTitle")}
Expand All @@ -396,7 +416,7 @@ function QqGroupContextPolicyFields({
valuePropName="checked"
noStyle
>
<Switch />
<Switch aria-label={t("channels.qqGroupContextEnabled")} />
</Form.Item>
</div>

Expand Down Expand Up @@ -465,9 +485,44 @@ function QqGroupContextPolicyFields({
</Form.Item>
</div>

<Form.Item
name={["group_context", "owner_commands_only"]}
label={t("channels.qqGroupOwnerCommandsOnly")}
tooltip={t("channels.qqGroupOwnerCommandsOnlyDesc")}
valuePropName="checked"
>
<Switch />
</Form.Item>
<Form.Item
name={["group_context", "owner_tools_only"]}
label={t("channels.qqGroupOwnerToolsOnly")}
tooltip={t("channels.qqGroupOwnerToolsOnlyDesc")}
valuePropName="checked"
>
<Switch />
</Form.Item>
<Form.Item
name={["group_context", "owner_ids"]}
label={t("channels.qqGroupOwnerIds")}
extra={
<div className={styles.qqGroupPolicyDescription}>
{t("channels.qqGroupOwnerIdsHelp")}
</div>
}
>
<Select
mode="tags"
open={false}
suffixIcon={null}
disabled={!ownerCommandsOnly && !ownerToolsOnly}
tokenSeparators={[",", ",", " ", "\n"]}
placeholder={t("channels.qqGroupOwnerIdsPlaceholder")}
/>
</Form.Item>
<Form.Item
name={["group_context", "clear_after_reply"]}
label={t("channels.qqGroupClearAfterReply")}
className={styles.qqGroupPolicyClearAfterReply}
tooltip={t("channels.qqGroupClearAfterReplyDesc")}
valuePropName="checked"
>
Expand Down
31 changes: 31 additions & 0 deletions dashboard/src/pages/Agent/Channels/components/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,37 @@ describe("normalizeQqGroupContextConfig", () => {
});
});

it("protects legacy group configs and preserves explicit opt-out", () => {
expect(normalizeQqGroupContextConfig({ enabled: false })).toMatchObject({
enabled: false,
owner_commands_only: true,
owner_tools_only: false,
owner_ids: [],
});
expect(
normalizeQqGroupContextConfig({ owner_commands_only: false }),
).toMatchObject({ owner_commands_only: false });
expect(
normalizeQqGroupContextConfig({ owner_tools_only: true }),
).toMatchObject({ owner_tools_only: true });
expect(
normalizeQqGroupContextConfig({ owner_tools_only: false }),
).toMatchObject({ owner_tools_only: false });
});

it("normalizes owner IDs without losing per-group authorization overrides", () => {
const groups = { "group-1": { owner_ids: ["group-owner"] } };
expect(
normalizeQqGroupContextConfig({
owner_ids: [" owner-1 ", "owner-2", "owner-1", "", 123],
groups,
}),
).toMatchObject({ owner_ids: ["owner-1", "owner-2"], groups });
expect(
normalizeQqGroupContextConfig({ owner_ids: "owner-1, owner-2\nowner-1" }),
).toMatchObject({ owner_ids: ["owner-1", "owner-2"] });
});

it("forces mention-only visibility to discard passive history", () => {
expect(
normalizeQqGroupContextConfig({
Expand Down
30 changes: 29 additions & 1 deletion dashboard/src/pages/Agent/Channels/components/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ export interface QqGroupContextConfig {
history_limit: number;
history_ttl_seconds: number;
clear_after_reply: boolean;
owner_commands_only: boolean;
owner_tools_only: boolean;
owner_ids: string[];
groups?: Record<
string,
Record<string, string | number | boolean | null | undefined>
Record<string, string | string[] | number | boolean | null | undefined>
>;
}

Expand All @@ -212,6 +215,9 @@ export const DEFAULT_QQ_GROUP_CONTEXT_CONFIG: QqGroupContextConfig = {
history_limit: 10,
history_ttl_seconds: 300,
clear_after_reply: true,
owner_commands_only: true,
owner_tools_only: false,
owner_ids: [],
};

function isRecord(value: unknown): value is Record<string, unknown> {
Expand Down Expand Up @@ -243,6 +249,12 @@ export function normalizeQqGroupContextConfig(
parsed.activation === "always" ? "always" : ("mention" as const);
const rawLimit = Number(parsed.history_limit);
const rawTtl = Number(parsed.history_ttl_seconds);
const rawOwnerIds =
typeof parsed.owner_ids === "string"
? parsed.owner_ids.split(/[\s,,]+/u)
: Array.isArray(parsed.owner_ids)
? parsed.owner_ids
: [];

return {
...parsed,
Expand All @@ -268,6 +280,22 @@ export function normalizeQqGroupContextConfig(
typeof parsed.clear_after_reply === "boolean"
? parsed.clear_after_reply
: DEFAULT_QQ_GROUP_CONTEXT_CONFIG.clear_after_reply,
owner_commands_only:
typeof parsed.owner_commands_only === "boolean"
? parsed.owner_commands_only
: DEFAULT_QQ_GROUP_CONTEXT_CONFIG.owner_commands_only,
owner_tools_only:
typeof parsed.owner_tools_only === "boolean"
? parsed.owner_tools_only
: false,
owner_ids: [
...new Set(
rawOwnerIds
.filter((id): id is string => typeof id === "string")
.map((id) => id.trim())
.filter(Boolean),
),
],
};
}

Expand Down
Loading
Loading