mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-03 20:21:51 +08:00
feat(chat,proxy): add @schedule mentions and localize proxy/schedule errors
- Support @schedule mentions in DB search, server mention resolution, and frontend mention parsing/i18n. - Return structured product error codes for proxy route validation and localize them in proxy/schedule UI. - Preserve unchanged proxy routes when rescheduling an existing schedule whose pool has no active nodes.
This commit is contained in:
1 parent
8568c41528
commit
e6241dda09
15 files changed
+312
-49
No files matched your search
@@ -13,6 +13,7 @@ import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { localizedError } from "@/i18n/errors";
|
||||
import { useLiveTranslation } from "@/i18n/use-live-translation";
|
||||
import { api } from "@/lib/api";
|
||||
import type { ContainerRouteStatusItem, ProxyPoolEntry, ProxyRoutesState } from "@/lib/types";
|
||||
@@ -26,7 +27,7 @@ export function ContainerRoutesTab({
|
||||
routesState: ProxyRoutesState | null;
|
||||
onRefresh: () => Promise<void>;
|
||||
}) {
|
||||
const { isEn, locale } = useLiveTranslation();
|
||||
const { isEn, locale, liveT } = useLiveTranslation();
|
||||
const L = (zh: string, en: string) => (isEn ? en : zh);
|
||||
const formatProxyValue = (raw?: string) => {
|
||||
const clean = (raw ?? "").replace(/\s*\((全局|Global)\)\s*$/i, "").trim();
|
||||
@@ -139,7 +140,7 @@ export function ContainerRoutesTab({
|
||||
await onRefresh();
|
||||
toast.success(L("容器出口路由已生效", "Container egress route saved"));
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : String(err));
|
||||
toast.error(localizedError(err, liveT));
|
||||
} finally {
|
||||
setSavingRoute(false);
|
||||
}
|
||||
@@ -165,7 +166,7 @@ export function ContainerRoutesTab({
|
||||
await onRefresh();
|
||||
toast.success(L("已更新容器出口路由", "Container egress route updated"));
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : String(e));
|
||||
toast.error(localizedError(e, liveT));
|
||||
} finally {
|
||||
setBusyKey(null);
|
||||
}
|
||||
@@ -179,7 +180,7 @@ export function ContainerRoutesTab({
|
||||
await onRefresh();
|
||||
toast.success(L("已恢复继承全局代理", "Reverted to global proxy"));
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : String(e));
|
||||
toast.error(localizedError(e, liveT));
|
||||
} finally {
|
||||
setBusyKey(null);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Popover, PopoverContent, PopoverDescription, PopoverHeader, PopoverTitle, PopoverTrigger } from "@/components/ui/popover";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { localizedError } from "@/i18n/errors";
|
||||
import { useLiveTranslation } from "@/i18n/use-live-translation";
|
||||
import { api } from "@/lib/api";
|
||||
import type { ProxyPoolEntry, TaskProxyRoute } from "@/lib/types";
|
||||
@@ -198,7 +199,7 @@ export function ContainerProxyControlSection({
|
||||
open: boolean;
|
||||
onSaved?: () => void;
|
||||
}) {
|
||||
const { isEn } = useLiveTranslation();
|
||||
const { isEn, liveT } = useLiveTranslation();
|
||||
const L = (zh: string, en: string) => (isEn ? en : zh);
|
||||
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
@@ -263,7 +264,7 @@ export function ContainerProxyControlSection({
|
||||
toast.success(L("容器代理路由已更新并实时生效", "Container proxy route updated"));
|
||||
onSaved?.();
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : String(e));
|
||||
toast.error(localizedError(e, liveT));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
|
||||
@@ -43,6 +43,26 @@ export const errorMessages = {
|
||||
"error.password_not_initialized": ["密码未初始化,请先设置密码", "Set up a password first"],
|
||||
"error.current_password_incorrect": ["当前密码错误", "The current password is incorrect"],
|
||||
"error.invalid_credentials": ["用户名或密码错误", "Incorrect username or password"],
|
||||
"error.proxy_pool_no_available_nodes": [
|
||||
"代理池暂无可用节点,请先在代理设置中添加并启用节点",
|
||||
"No available nodes in the proxy pool. Add and enable a node in Proxy Settings first.",
|
||||
],
|
||||
"error.proxy_route_invalid_mode": [
|
||||
"代理模式无效,必须是 inherit、direct、pool 或 proxy",
|
||||
"Invalid proxy mode. Must be inherit, direct, pool, or proxy.",
|
||||
],
|
||||
"error.proxy_pool_node_not_found": [
|
||||
"未找到代理池节点:{{id}}",
|
||||
"Proxy pool node not found: {{id}}",
|
||||
],
|
||||
"error.proxy_route_invalid_target": [
|
||||
"无效的容器路由目标(scope 必须为 task 或 session)",
|
||||
"Invalid container route target (scope must be task or session).",
|
||||
],
|
||||
"error.proxy_route_invalid_url": [
|
||||
"代理地址无效:{{detail}}",
|
||||
"Invalid proxy URL: {{detail}}",
|
||||
],
|
||||
"update.available": ["新版本", "Update available"],
|
||||
"update.hint": ["发现新版本 {{version}},点击前往更新", "Version {{version}} is available. Click to update."],
|
||||
} satisfies Record<string, readonly [string, string]>;
|
||||
@@ -297,6 +297,17 @@ test("schedule windows, counts and stored system messages follow the selected la
|
||||
const original = "用户的计划与备注,包含 start_time";
|
||||
assert.equal(en.scheduleErrorText(original), original);
|
||||
assert.equal(en.scheduleErrorText("任务 #42:unknown original detail"), "Task #42: unknown original detail");
|
||||
assert.equal(
|
||||
en.scheduleErrorText("代理池暂无可用节点,请先在代理设置中添加并启用节点"),
|
||||
"No available nodes in the proxy pool. Add and enable a node in Proxy Settings first.",
|
||||
);
|
||||
const proxyErr = Object.assign(new Error("代理池暂无可用节点,请先在代理设置中添加并启用节点"), {
|
||||
code: "proxy_pool_no_available_nodes",
|
||||
});
|
||||
assert.equal(
|
||||
localizedError(proxyErr, i18n.t.bind(i18n)),
|
||||
"No available nodes in the proxy pool. Add and enable a node in Proxy Settings first.",
|
||||
);
|
||||
for (const [key, singular, plural] of [
|
||||
["schedule.started", "Started 1 task immediately", "Started 2 tasks immediately"],
|
||||
["schedule.deleted", "Deleted 1 schedule", "Deleted 2 schedules"],
|
||||
|
||||
@@ -118,6 +118,7 @@ export const messages = {
|
||||
"auth.saving": ["保存中...", "Saving…"],
|
||||
"auth.setupAndLogin": ["设置密码并登录", "Set password and sign in"],
|
||||
"mention.task": ["任务", "Task"],
|
||||
"mention.schedule": ["计划任务", "Schedule"],
|
||||
"mention.finding": ["漏洞", "Finding"],
|
||||
"mention.asset": ["资产", "Asset"],
|
||||
"mention.company": ["企业", "Company"],
|
||||
|
||||
@@ -27,6 +27,7 @@ const messages: Record<string, string> = {
|
||||
立即运行: "schedule.runtime.runNow",
|
||||
"立即运行(显式恢复人工暂停)": "schedule.runtime.forceResume",
|
||||
计划窗口: "schedule.runtime.window",
|
||||
"代理池暂无可用节点,请先在代理设置中添加并启用节点": "error.proxy_pool_no_available_nodes",
|
||||
};
|
||||
const taskMessages: [RegExp, string][] = [
|
||||
[/^任务 #(\d+) 未找到$/, "schedule.runtime.taskNotFound"],
|
||||
|
||||
@@ -10,11 +10,16 @@ test("mention trigger supports Chinese and cursor placement without hijacking em
|
||||
});
|
||||
|
||||
test("categories, Chinese aliases, IP and keyword search", () => {
|
||||
assert.equal(mentionSearch("").categories.length, 10);
|
||||
assert.equal(mentionSearch("").categories.length, 11);
|
||||
assert.equal(mentionSearch("任").categories[0].kind, "task");
|
||||
assert.equal(mentionSearch("任务").kind, "task");
|
||||
assert.equal(mentionSearch("任务 5").query, "5");
|
||||
assert.equal(mentionSearch("task 5").kind, "task");
|
||||
assert.equal(mentionSearch("计划").categories[0].kind, "schedule");
|
||||
assert.equal(mentionSearch("计划任务").kind, "schedule");
|
||||
assert.equal(mentionSearch("计划任务 夜间").query, "夜间");
|
||||
assert.equal(mentionSearch("schedule 2").kind, "schedule");
|
||||
assert.equal(mentionSearch("schedule 2").query, "2");
|
||||
assert.equal(mentionSearch("漏").categories[0].kind, "finding");
|
||||
assert.equal(mentionSearch("漏洞").kind, "finding");
|
||||
assert.equal(mentionSearch("漏洞SQL注入").query, "SQL注入");
|
||||
@@ -35,8 +40,8 @@ test("tokens roundtrip labels and removing one reference preserves its neighbors
|
||||
});
|
||||
|
||||
test("all mention kinds use stable tokens and recognize their legacy Chinese form", () => {
|
||||
const kinds = ["task", "finding", "asset", "company", "endpoint", "ip", "app", "root_domain", "subdomain", "service"];
|
||||
const legacy = ["任务", "漏洞", "资产", "企业", "接口", "IP", "应用", "域名", "子域名", "服务"];
|
||||
const kinds = ["task", "schedule", "finding", "asset", "company", "endpoint", "ip", "app", "root_domain", "subdomain", "service"];
|
||||
const legacy = ["任务", "计划任务", "漏洞", "资产", "企业", "接口", "IP", "应用", "域名", "子域名", "服务"];
|
||||
for (const [i, kind] of kinds.entries()) {
|
||||
const token = mentionToken({kind, id:i + 1, label:"Keep 用户名称"});
|
||||
assert.ok(token.startsWith("@[" + kind + "#"));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export const mentionKinds = [
|
||||
{ kind: "task", label: "任务", alias: "task", english: "Task" },
|
||||
{ kind: "schedule", label: "计划任务", alias: "schedule", english: "Schedule" },
|
||||
{ kind: "finding", label: "漏洞", alias: "finding", english: "Finding" },
|
||||
{ kind: "asset", label: "资产", alias: "asset", english: "Asset" },
|
||||
{ kind: "company", label: "企业", alias: "company", english: "Company" },
|
||||
@@ -64,10 +65,11 @@ export function selectedMentions(value: string, translate?: (key: string) => str
|
||||
// Keep recognizing Chinese tokens in saved drafts and conversation history.
|
||||
return [
|
||||
...value.matchAll(
|
||||
/@\[(task|finding|asset|company|endpoint|ip|app|root_domain|subdomain|service|任务|漏洞|资产|企业|接口|IP|应用|域名|子域名|服务)#([0-9]+)(?: ([^\]\r\n]*))?\]/g,
|
||||
/@\[(schedule|task|finding|asset|company|endpoint|ip|app|root_domain|subdomain|service|计划任务|计划|任务|漏洞|资产|企业|接口|IP|应用|域名|子域名|服务)#([0-9]+)(?: ([^\]\r\n]*))?\]/g,
|
||||
),
|
||||
].map((match) => {
|
||||
const entry = mentionKinds.find((item) => item.kind === match[1] || item.label === match[1]);
|
||||
const rawKind = match[1] === "计划" ? "schedule" : match[1];
|
||||
const entry = mentionKinds.find((item) => item.kind === rawKind || item.label === rawKind);
|
||||
const label = translate ? translate(`mention.${entry?.kind ?? "asset"}`) : (entry?.label ?? match[1]);
|
||||
return {
|
||||
token: match[0],
|
||||
|
||||
Reference in new issue
Block a user