Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix:切换Agent能看到集群名称

wangxin1b6f5cf2a22c
diff --git a/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx b/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx
index 9c4519cfe9..db44cc552b 100644
--- a/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/EnterpriseEntry.tsx
@@ -15,16 +15,25 @@ import { parseRuntimeScope, setRuntimeScope } from './services/runtimeScope';
type EntryPhase = 'loading' | 'ready' | 'empty' | 'error' | 'redirecting' | 'login-required';
-function movePreferredFirst(items: EnterpriseAgentContext[], preferredKey?: string): EnterpriseAgentContext[] {
- if (!preferredKey) return items;
- const preferredIndex = items.findIndex(item => agentContextKey(item) === preferredKey);
+function movePreferredFirst(
+ items: EnterpriseAgentContext[],
+ preferred?: { botId?: string; groupId?: string; userId?: string; jiuwenclawId?: string },
+): EnterpriseAgentContext[] {
+ if (!preferred?.botId || !preferred.groupId || !preferred.userId) return items;
+ const preferredIndex = items.findIndex(
+ item =>
+ item.bot_id === preferred.botId &&
+ item.group_id === preferred.groupId &&
+ item.user_id === preferred.userId &&
+ (!preferred.jiuwenclawId || item.jiuwenclaw_id === preferred.jiuwenclawId),
+ );
if (preferredIndex <= 0) return items;
return [items[preferredIndex], ...items.slice(0, preferredIndex), ...items.slice(preferredIndex + 1)];
}
export function chooseAgentContext(
contexts: EnterpriseAgentContext[],
- preferred?: { botId?: string; groupId?: string; userId?: string },
+ preferred?: { botId?: string; groupId?: string; userId?: string; jiuwenclawId?: string },
): EnterpriseAgentContext | null {
if (!contexts.length) return null;
if (preferred?.botId && preferred.groupId && preferred.userId) {
@@ -32,12 +41,17 @@ export function chooseAgentContext(
item =>
item.bot_id === preferred.botId &&
item.group_id === preferred.groupId &&
- item.user_id === preferred.userId,
+ item.user_id === preferred.userId &&
+ (!preferred.jiuwenclawId || item.jiuwenclaw_id === preferred.jiuwenclawId),
);
if (exact) return exact;
}
if (preferred?.botId) {
- const byBot = contexts.find(item => item.bot_id === preferred.botId);
+ const byBot = contexts.find(
+ item =>
+ item.bot_id === preferred.botId &&
+ (!preferred.jiuwenclawId || item.jiuwenclaw_id === preferred.jiuwenclawId),
+ );
if (byBot) return byBot;
}
return contexts[0] ?? null;
@@ -70,6 +84,10 @@ function writeKnownCluster(jiuwenclawId: string): void {
}
}
+function parsePreferredClusterId(search: string): string {
+ return new URLSearchParams(search).get('jiuwenclaw_id')?.trim() || '';
+}
+
function contextUrl(
selected: EnterpriseAgentContext,
debugContext = false,
@@ -80,6 +98,7 @@ function contextUrl(
group_id: selected.group_id,
bot_id: selected.bot_id,
});
+ if (selected.jiuwenclaw_id) query.set('jiuwenclaw_id', selected.jiuwenclaw_id);
if (debugContext) query.set('debug_context', '1');
const path = resetPath
? window.location.pathname.startsWith('/chat')
@@ -128,15 +147,17 @@ export function isDebugContext(search: string): boolean {
}
export function buildCustomContext(
- preferred: ReturnType<typeof parseRuntimeScope>,
+ preferred: ReturnType<typeof parseRuntimeScope> & { jiuwenclawId?: string },
fallbackGatewayId = '',
): EnterpriseAgentContext | null {
if (!preferred.groupId || !preferred.botId || !preferred.userId) return null;
+ const jiuwenclawId = preferred.jiuwenclawId?.trim() || fallbackGatewayId;
return {
bot_id: preferred.botId,
group_id: preferred.groupId,
user_id: preferred.userId,
- jiuwenclaw_id: fallbackGatewayId,
+ jiuwenclaw_id: jiuwenclawId,
+ jiuwenclaw_name: jiuwenclawId,
agent_name: preferred.botId,
group_name: preferred.groupId,
};
@@ -218,39 +239,47 @@ export function EnterpriseEntry({ children }: { children: ReactNode }) {
const bootstrap = async () => {
try {
const preferred = parseRuntimeScope(window.location.search);
+ const preferredClusterId =
+ parsePreferredClusterId(window.location.search) || readKnownCluster();
const [user, contexts] = await Promise.all([provider.getCurrentUser(), provider.listAgentContexts()]);
if (cancelled) return;
let selected: EnterpriseAgentContext | null = null;
if (isDebugContext(window.location.search)) {
const matched = preferred.botId
- ? contexts.find(item => item.bot_id === preferred.botId)
+ ? contexts.find(
+ item =>
+ item.bot_id === preferred.botId &&
+ (!preferredClusterId || item.jiuwenclaw_id === preferredClusterId),
+ )
: undefined;
// 旧版 debug URL 可省略 user_id(默认取登录用户),与 chooseAgentContext 对齐。
+ // jiuwenclaw_id 优先取 URL / 上次 active-cluster,避免刷新后按 bot 匹配回旧集群。
selected = buildCustomContext(
{
...preferred,
userId: preferred.userId || user.user_id,
+ jiuwenclawId: preferredClusterId || undefined,
},
matched?.jiuwenclaw_id || contexts[0]?.jiuwenclaw_id || '',
);
+ if (selected && matched?.jiuwenclaw_name && selected.jiuwenclaw_id === matched.jiuwenclaw_id) {
+ selected = { ...selected, jiuwenclaw_name: matched.jiuwenclaw_name };
+ }
}
if (!selected) {
selected = chooseAgentContext(
- movePreferredFirst(
- contexts,
- preferred.botId && preferred.groupId && preferred.userId
- ? agentContextKey({
- bot_id: preferred.botId,
- group_id: preferred.groupId,
- user_id: preferred.userId,
- })
- : undefined,
- ),
+ movePreferredFirst(contexts, {
+ botId: preferred.botId,
+ groupId: preferred.groupId,
+ userId: preferred.userId,
+ jiuwenclawId: preferredClusterId || undefined,
+ }),
{
botId: preferred.botId,
groupId: preferred.groupId,
userId: preferred.userId || user.user_id,
+ jiuwenclawId: preferredClusterId || undefined,
},
);
}
@@ -312,28 +341,40 @@ export function EnterpriseEntry({ children }: { children: ReactNode }) {
})();
},
onCustomContextApply: input => {
- if (contextSwitching) return;
+ if (contextSwitching || !provider) return;
const botId = input.botId.trim();
const groupId = input.groupId.trim();
const userId = input.userId.trim();
- if (!botId || !groupId || !userId) {
+ const jiuwenclawId = input.jiuwenclawId.trim();
+ if (!botId || !groupId || !userId || !jiuwenclawId) {
setContextError(t('sessionSidebar.enterpriseContext.customRequired'));
return;
}
+ const matched = context.contexts.find(item => item.jiuwenclaw_id === jiuwenclawId);
setContextSwitching(true);
setContextError('');
- activateContext(
- {
- bot_id: botId,
- group_id: groupId,
- user_id: userId,
- jiuwenclaw_id: context.selected.jiuwenclaw_id,
- agent_name: botId,
- group_name: groupId,
- },
- true,
- true,
- );
+ void (async () => {
+ try {
+ const clusterChanged = await ensureActiveCluster(provider, jiuwenclawId);
+ activateContext(
+ {
+ bot_id: botId,
+ group_id: groupId,
+ user_id: userId,
+ jiuwenclaw_id: jiuwenclawId,
+ jiuwenclaw_name: matched?.jiuwenclaw_name || jiuwenclawId,
+ agent_name: botId,
+ group_name: groupId,
+ },
+ true,
+ true,
+ clusterChanged,
+ );
+ } catch (switchError) {
+ setContextSwitching(false);
+ setContextError(errorText(switchError));
+ }
+ })();
},
onLogout: logout,
};
diff --git a/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts b/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts
index 189ddd2736..513bad8aa7 100644
--- a/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts
+++ b/jiuwenswarm/channels/web/frontend/src/auth/simulate/SimulatedAuthProvider.ts
@@ -11,6 +11,7 @@ const DEFAULTS = {
groupId: 'default',
groupName: 'Debug Organization',
gatewayId: 'debug-gateway',
+ gatewayName: 'Debug Cluster',
botId: 'default',
agentName: 'Debug Agent',
} as const;
@@ -24,6 +25,7 @@ function buildContext(search = ''): EnterpriseAgentContext {
group_id: groupId,
user_id: preferred.userId || DEFAULTS.userId,
jiuwenclaw_id: DEFAULTS.gatewayId,
+ jiuwenclaw_name: DEFAULTS.gatewayName,
agent_name: DEFAULTS.agentName,
group_name: DEFAULTS.groupName,
};
diff --git a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/SessionSidebar.css b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/SessionSidebar.css
index d647368a2c..6759f453f1 100644
--- a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/SessionSidebar.css
+++ b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/SessionSidebar.css
@@ -330,7 +330,7 @@
z-index: 1500;
left: 72px;
bottom: 56px;
- width: 280px;
+ width: 360px;
padding: 14px;
border: 1px solid var(--color-border-default);
border-radius: 12px;
diff --git a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
index 8e9ba0ebda..2de78ac6a3 100644
--- a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
@@ -171,6 +171,7 @@ export function SessionSidebar({
const [customBotId, setCustomBotId] = useState('');
const [customGroupId, setCustomGroupId] = useState('');
const [customUserId, setCustomUserId] = useState('');
+ const [customJiuwenclawId, setCustomJiuwenclawId] = useState('');
const contextButtonRef = useRef<HTMLButtonElement>(null);
const contextPanelRef = useRef<HTMLDivElement>(null);
@@ -233,6 +234,7 @@ export function SessionSidebar({
setCustomBotId(enterprise.selected.bot_id);
setCustomGroupId(enterprise.selected.group_id);
setCustomUserId(enterprise.selected.user_id);
+ setCustomJiuwenclawId(enterprise.selected.jiuwenclaw_id);
}, [contextOpen, enterprise?.selected]);
const activeMode: ContextMode = useMemo(() => {
@@ -261,6 +263,7 @@ export function SessionSidebar({
enterprise?.selected.bot_id,
enterprise?.selected.group_id,
enterprise?.selected.user_id,
+ enterprise?.selected.jiuwenclaw_id,
]);
const agentContextOptions = useMemo(
@@ -435,6 +438,16 @@ export function SessionSidebar({
aria-label={t('sessionSidebar.enterpriseContext.userId')}
/>
</label>
+ <label>
+ {t('sessionSidebar.enterpriseContext.jiuwenclawId')}
+ <input
+ className="enterprise-context-popover__input"
+ value={customJiuwenclawId}
+ disabled={enterprise.contextSwitching}
+ onChange={event => setCustomJiuwenclawId(event.target.value)}
+ aria-label={t('sessionSidebar.enterpriseContext.jiuwenclawId')}
+ />
+ </label>
<button
type="button"
className="enterprise-context-popover__apply"
@@ -444,6 +457,7 @@ export function SessionSidebar({
botId: customBotId,
groupId: customGroupId,
userId: customUserId,
+ jiuwenclawId: customJiuwenclawId,
})
}
>
diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
index 7c9c3ac65b..d9bb1a95dc 100644
--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/en.json
@@ -551,14 +551,15 @@
"modeAria": "Context setup mode",
"selectAgent": "Select Agent",
"custom": "Custom",
- "agentOrgLabel": "Agent name - Organization name",
+ "agentOrgLabel": "Agent name | Organization name | Cluster name",
"botId": "bot_id",
"groupId": "group_id",
"userId": "user_id",
+ "jiuwenclawId": "jiuwenclaw_id",
"apply": "Apply",
"switching": "Switching...",
"logout": "Sign out",
- "customRequired": "Please fill in bot_id, group_id, and user_id",
+ "customRequired": "Please fill in bot_id, group_id, user_id, and jiuwenclaw_id",
"noMatch": "No matches",
"pleaseSelect": "Please select"
}
diff --git a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
index 09cd5fcc11..fedf37ea14 100644
--- a/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
+++ b/jiuwenswarm/channels/web/frontend/src/i18n/locales/zh.json
@@ -552,14 +552,15 @@
"modeAria": "上下文设定方式",
"selectAgent": "选择 Agent",
"custom": "自定义",
- "agentOrgLabel": "agent名称-组织名称",
+ "agentOrgLabel": "agent名称 | 组织名称 | 集群名称",
"botId": "bot_id",
"groupId": "group_id",
"userId": "user_id",
+ "jiuwenclawId": "jiuwenclaw_id",
"apply": "应用",
"switching": "正在切换...",
"logout": "注销登录",
- "customRequired": "请填写 bot_id、group_id、user_id",
+ "customRequired": "请填写 bot_id、group_id、user_id、jiuwenclaw_id",
"noMatch": "没有匹配项",
"pleaseSelect": "请选择"
}
diff --git a/jiuwenswarm/channels/web/frontend/src/services/enterpriseContext.tsx b/jiuwenswarm/channels/web/frontend/src/services/enterpriseContext.tsx
index e94d566914..12504a8bc3 100644
--- a/jiuwenswarm/channels/web/frontend/src/services/enterpriseContext.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/services/enterpriseContext.tsx
@@ -8,6 +8,8 @@ export type EnterpriseAgentContext = {
group_id: string;
user_id: string;
jiuwenclaw_id: string;
+ /** 实例/集群展示名(对应 Manager instance_info.jiuwenclaw_name) */
+ jiuwenclaw_name: string;
agent_name: string;
group_name: string;
};
@@ -22,18 +24,26 @@ export type EnterpriseContextValue = EnterpriseContextSnapshot & {
contextError: string;
contextSwitching: boolean;
onContextChange: (key: string) => void;
- onCustomContextApply: (input: { botId: string; groupId: string; userId: string }) => void;
+ onCustomContextApply: (input: {
+ botId: string;
+ groupId: string;
+ userId: string;
+ jiuwenclawId: string;
+ }) => void;
onLogout: () => void;
};
-export function agentContextKey(item: Pick<EnterpriseAgentContext, 'bot_id' | 'group_id' | 'user_id'>): string {
- return `${item.bot_id}\u0001${item.group_id}\u0001${item.user_id}`;
+export function agentContextKey(
+ item: Pick<EnterpriseAgentContext, 'bot_id' | 'group_id' | 'user_id' | 'jiuwenclaw_id'>,
+): string {
+ return `${item.bot_id}\u0001${item.group_id}\u0001${item.user_id}\u0001${item.jiuwenclaw_id}`;
}
export function formatAgentContextLabel(item: EnterpriseAgentContext): string {
const agentName = item.agent_name?.trim() || item.bot_id;
const groupName = item.group_name?.trim() || item.group_id;
- return `${agentName}-${groupName}`;
+ const clusterName = item.jiuwenclaw_name?.trim() || item.jiuwenclaw_id;
+ return `${agentName} | ${groupName} | ${clusterName}`;
}
export const EnterpriseContext = createContext<EnterpriseContextValue | null>(null);
diff --git a/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs b/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs
index ff47eca1f4..900e123b80 100644
--- a/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs
+++ b/jiuwenswarm/channels/web/frontend/tests/userWebEntry.test.mjs
@@ -106,8 +106,8 @@ test('auth entry path guard stops User Web from redirecting /auth to itself', ()
test('agent context selection prefers an exact URL tuple and otherwise falls back', () => {
const contexts = [
- { bot_id: 'agent-1', group_id: 'group-1', user_id: 'user-1', jiuwenclaw_id: 'gw-1', agent_name: 'Agent 1', group_name: 'Group 1' },
- { bot_id: 'agent-2', group_id: 'group-2', user_id: 'user-1', jiuwenclaw_id: 'gw-2', agent_name: 'Agent 2', group_name: 'Group 2' },
+ { bot_id: 'agent-1', group_id: 'group-1', user_id: 'user-1', jiuwenclaw_id: 'gw-1', jiuwenclaw_name: 'Cluster 1', agent_name: 'Agent 1', group_name: 'Group 1' },
+ { bot_id: 'agent-2', group_id: 'group-2', user_id: 'user-1', jiuwenclaw_id: 'gw-2', jiuwenclaw_name: 'Cluster 2', agent_name: 'Agent 2', group_name: 'Group 2' },
];
assert.equal(
@@ -121,14 +121,19 @@ test('agent context selection prefers an exact URL tuple and otherwise falls bac
test('debug context preserves explicitly entered routing identifiers', () => {
const debugContext = buildCustomContext(
- { userId: 'debug-user', groupId: 'debug-group', botId: 'debug-bot' },
+ {
+ userId: 'debug-user',
+ groupId: 'debug-group',
+ botId: 'debug-bot',
+ jiuwenclawId: 'custom-gateway',
+ },
'resolved-gateway',
);
assert.equal(isDebugContext('?debug_context=1'), true);
assert.equal(isDebugContext('?debug_context=0'), false);
assert.equal(debugContext?.group_id, 'debug-group');
- assert.equal(debugContext?.jiuwenclaw_id, 'resolved-gateway');
+ assert.equal(debugContext?.jiuwenclaw_id, 'custom-gateway');
assert.equal(debugContext?.bot_id, 'debug-bot');
assert.equal(debugContext?.user_id, 'debug-user');
});