Files
DeepTutor/web/components/partners/PartnerChannels.tsx
Bingxi Zhao (Frank) bd80a4d28a release: v1.5.17
Partners stop being a deployment-wide admin resource and become
per-user: three nested rights (manage / use / neither) replace the
blanket admin gate on the router, each member can build and own their
own, and every person's conversations with a partner stay private to
their account. Grants keep working and now offer only admin-managed
partners. Channel runtime state moves from one directory per channel to
one per partner — copied, not moved, and only when its owner is
unambiguous — so two partners can finally run the same channel.

Around that: a knowledge base can track GitHub repositories and re-sync
their Markdown (#903); Antigravity CLI joins My Agents now that Gemini
CLI is retired (#828); personal WeChat signs in from a QR code drawn in
the browser instead of the server log (#951); a chat account can be
linked to a DeepTutor account with /link; containers re-apply the extras
they declare instead of losing them at compose down (#762); and
`deeptutor doctor` answers whether a workspace is ready to run (#959).

Reliability, largely from community PRs: embeddings survive a
rate-limited provider through key rotation, a retry budget and a global
throttle (#962); MinerU parsing and code execution work on Windows
(#844, #964); indexing fails bounded instead of hanging (#946); MinerU
page-layout blocks stop being indexed as content (#949); voice uploads
carry a MIME the transcription API accepts (#945); and reasoning effort
can be overridden per conversation (#641).

Release plumbing: notes in assets/releases/ver1-5-17.md, v1.5.13
archived, __version__ 1.5.17, README Releases row and all eleven
translations synced.

Two defects found by the release gate and fixed here:

- Six partner routes — the four channel-onboarding endpoints and both
  WeChat QR endpoints — kept the _auth-only mount they had inherited
  from the blanket admin gate, so any signed-in account could start a
  QR login for someone else's partner and write a bot token into its
  channel config. All six now declare _MANAGEABLE, and a sweep test
  asserts no {partner_id} route is left ungated.
- A configured api_key became `str | list[str]` when key rotation
  landed, but ten call sites pass it somewhere that takes one key; a
  list would have rendered into an Authorization header verbatim.
  keypool.primary_api_key reduces at each boundary.

Gates: pre-commit (all 15 hooks), ruff check + format, pytest
5276 passed, web 605 node tests, docs build.
2026-08-25 02:24:35 +08:00

334 lines
12 KiB
TypeScript

"use client";
/**
* Channel configuration panel — Partners channel-config logic preserved:
* schema-driven form for any channel (built-in or plugin), secret masking
* with explicit reveal, delivery flags, and live listener reload on save.
*/
import { useCallback, useEffect, useState } from "react";
import { Loader2, Save } from "lucide-react";
import { useTranslation } from "react-i18next";
import { apiFetch, apiUrl } from "@/lib/api";
import {
supportsChannelOnboarding,
getChannelSchemas,
type ChannelsSchemaResponse,
} from "@/lib/partners-api";
import {
SchemaField,
defaultFor,
type JsonSchema,
} from "@/components/partners/schema-form";
import WeixinQrLogin from "@/components/partners/WeixinQrLogin";
import ChannelIcon from "@/components/partners/ChannelIcon";
import ChannelOnboardingPanel from "@/components/partners/ChannelOnboardingPanel";
const LEGACY_GLOBAL_DELIVERY_KEYS = new Set([
"send_progress",
"send_tool_hints",
"sendProgress",
"sendToolHints",
]);
function stripLegacyGlobalDelivery(channels: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(channels).filter(
([key]) => !LEGACY_GLOBAL_DELIVERY_KEYS.has(key),
),
);
}
export default function PartnerChannels({
partnerId,
onToast,
}: {
partnerId: string;
onToast: (msg: string) => void;
}) {
const { t } = useTranslation();
const [schemaCatalog, setSchemaCatalog] =
useState<ChannelsSchemaResponse | null>(null);
const [channels, setChannels] = useState<Record<string, unknown>>({});
const [activeChannel, setActiveChannel] = useState<string | null>(null);
const [reloadError, setReloadError] = useState<string | null>(null);
const [loadingDetail, setLoadingDetail] = useState(true);
const [saving, setSaving] = useState(false);
/** dot-paths of secrets the user has explicitly toggled to plaintext. */
const [revealed, setRevealed] = useState<Set<string>>(new Set());
useEffect(() => {
void (async () => {
try {
setSchemaCatalog(await getChannelSchemas());
} catch {
/* leave catalog null → renders fallback message */
}
})();
}, []);
const loadDetail = useCallback(async () => {
setLoadingDetail(true);
try {
// Edit form needs raw secrets to populate fields. Default GET masks them.
const res = await apiFetch(
apiUrl(`/api/v1/partners/${partnerId}?include_secrets=true`),
);
if (!res.ok) return;
const data = await res.json();
const raw = (data.channels ?? {}) as Record<string, unknown>;
setChannels(stripLegacyGlobalDelivery(raw));
setReloadError(
typeof data.last_reload_error === "string"
? data.last_reload_error
: null,
);
} finally {
setLoadingDetail(false);
}
}, [partnerId]);
useEffect(() => {
setRevealed(new Set());
void loadDetail();
}, [loadDetail]);
// Default active channel: prefer one already enabled.
useEffect(() => {
if (activeChannel || !schemaCatalog) return;
const names = Object.keys(schemaCatalog.channels);
const enabled = names.find((n) => {
const cfg = channels[n];
return (
cfg &&
typeof cfg === "object" &&
(cfg as Record<string, unknown>).enabled === true
);
});
setActiveChannel(enabled ?? names[0] ?? null);
}, [schemaCatalog, channels, activeChannel]);
// A confirmed WeChat scan wrote the token into the config server-side;
// re-read so the form shows what is actually stored.
const onWeixinConnected = useCallback(() => {
onToast(t("WeChat connected."));
void loadDetail();
}, [loadDetail, onToast, t]);
const toggleSecret = useCallback((path: string) => {
setRevealed((prev) => {
const next = new Set(prev);
if (next.has(path)) next.delete(path);
else next.add(path);
return next;
});
}, []);
const setActiveChannelConfig = (next: unknown) => {
if (!activeChannel) return;
setChannels((prev) => ({ ...prev, [activeChannel]: next }));
};
const save = async () => {
setSaving(true);
try {
const res = await apiFetch(apiUrl(`/api/v1/partners/${partnerId}`), {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ channels: stripLegacyGlobalDelivery(channels) }),
});
if (res.ok) {
onToast(t("Channels saved"));
await loadDetail();
} else if (res.status === 422) {
const err = (await res.json().catch(() => ({}))) as {
detail?: { message?: string } | string;
};
const detail = err.detail;
onToast(
typeof detail === "string"
? detail
: (detail?.message ?? t("Invalid channel configuration")),
);
} else {
const err = (await res.json().catch(() => ({}))) as { detail?: string };
onToast(err.detail ?? t("Save failed"));
}
} catch (error) {
onToast(error instanceof Error ? error.message : t("Save failed"));
} finally {
setSaving(false);
}
};
if (loadingDetail || !schemaCatalog) {
return (
<div className="flex justify-center py-10">
<Loader2 className="h-5 w-5 animate-spin text-[var(--muted-foreground)]" />
</div>
);
}
const channelEntries = Object.entries(schemaCatalog.channels).sort(
([, a], [, b]) => a.display_name.localeCompare(b.display_name),
);
const activeEntry = activeChannel
? schemaCatalog.channels[activeChannel]
: undefined;
const activeValue =
activeChannel &&
channels[activeChannel] &&
typeof channels[activeChannel] === "object"
? (channels[activeChannel] as Record<string, unknown>)
: (activeEntry?.default_config ?? {});
const activeSecretSet = new Set(activeEntry?.secret_fields ?? []);
return (
<div className="space-y-4">
{reloadError && (
<div className="rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-[12px] text-amber-700 dark:text-amber-300">
<strong className="font-medium">
{t("Channel listeners failed to restart:")}
</strong>{" "}
<span className="font-mono">{reloadError}</span>{" "}
<span className="opacity-80">
{t("Config is saved on disk; stop and start the partner to apply.")}
</span>
</div>
)}
<div className="flex justify-end">
<button
type="button"
onClick={() => void save()}
disabled={saving}
className="inline-flex items-center gap-1.5 rounded-lg bg-[var(--primary)] px-3 py-1.5 text-[12px] font-medium text-[var(--primary-foreground)] disabled:opacity-40"
>
{saving ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<Save className="h-3.5 w-3.5" />
)}
{t("Save")}
</button>
</div>
{/* Channel master-detail */}
<div className="grid grid-cols-1 gap-4 md:grid-cols-[180px_1fr]">
<aside className="h-fit rounded-xl border border-[var(--border)] p-2">
<ul className="space-y-0.5">
{channelEntries.map(([name, entry]) => {
const cfg = channels[name] as Record<string, unknown> | undefined;
const enabled = cfg?.enabled === true;
const isActive = activeChannel === name;
const unavailable = entry.available === false;
return (
<li key={name}>
<button
type="button"
onClick={() => setActiveChannel(name)}
title={unavailable ? entry.unavailable_reason : undefined}
className={`group flex w-full items-center gap-2 rounded-md px-2.5 py-1.5 text-left text-[13px] transition-colors ${
isActive
? "bg-[var(--muted)] font-medium text-[var(--foreground)]"
: "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
} ${unavailable ? "opacity-45" : ""}`}
>
<ChannelIcon name={name} size={15} />
<span className="min-w-0 flex-1 truncate">
{entry.display_name}
</span>
{enabled && (
<span
aria-label={t("Enabled")}
title={t("Enabled")}
className="h-1.5 w-1.5 shrink-0 rounded-full bg-emerald-500"
/>
)}
</button>
</li>
);
})}
</ul>
</aside>
<section className="rounded-xl border border-[var(--border)] p-4 space-y-3">
{!activeEntry ? (
<p className="text-[13px] text-[var(--muted-foreground)]">
{t("Select a channel.")}
</p>
) : (
<>
<div className="flex items-center justify-between">
<h3 className="inline-flex items-center gap-2 text-[14px] font-medium text-[var(--foreground)]">
<ChannelIcon name={activeEntry.name} size={18} />
{activeEntry.display_name}
</h3>
<code className="text-[11px] text-[var(--muted-foreground)]">
{activeEntry.name}
</code>
</div>
{activeEntry.available === false || !activeEntry.json_schema ? (
<div className="rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-[12px] text-amber-700 dark:text-amber-300">
<strong className="font-medium">
{t("This channel is not available on the server.")}
</strong>{" "}
{activeEntry.unavailable_reason && (
<span className="font-mono">
{activeEntry.unavailable_reason}
</span>
)}
</div>
) : (
<>
{supportsChannelOnboarding(
activeChannel ?? "",
activeEntry.available,
) && (
<ChannelOnboardingPanel
partnerId={partnerId}
channel={activeChannel === "wecom" ? "wecom" : "feishu"}
onApplied={loadDetail}
onToast={onToast}
/>
)}
{(activeEntry.json_schema as JsonSchema).description && (
<p className="text-[11px] text-[var(--muted-foreground)]">
{(activeEntry.json_schema as JsonSchema).description}
</p>
)}
{/* Personal WeChat authenticates by scanning, not by pasting
a secret; the fields below stay as the manual fallback. */}
{activeChannel === "weixin" && partnerId && (
<WeixinQrLogin
partnerId={partnerId}
onConfirmed={onWeixinConnected}
/>
)}
{Object.entries(
(activeEntry.json_schema as JsonSchema).properties ?? {},
).map(([k, child]) => (
<SchemaField
key={k}
fieldKey={k}
schema={child}
value={activeValue[k] ?? defaultFor(child)}
onChange={(next) =>
setActiveChannelConfig({ ...activeValue, [k]: next })
}
secretFields={activeSecretSet}
path={k}
showSecretFor={revealed}
toggleSecret={toggleSecret}
/>
))}
</>
)}
</>
)}
</section>
</div>
</div>
);
}