From a37479a92cb78fe0065ed33e46884fdf4eade6e3 Mon Sep 17 00:00:00 2001 From: wusumac <736139669@qq.com> Date: Tue, 15 Sep 2026 09:56:12 +0800 Subject: [PATCH] fix(web,hub): let the team settings dialog set a lead session Teams created without a lead previously dead-ended: members cannot be spawned until a lead exists. PATCH /api/teams/:id now accepts leadSessionId; the settings dialog exposes a lead picker and delivers the lead brief on save. --- hub/src/teams/teamService.test.ts | 20 ++++++++ hub/src/teams/teamService.ts | 18 +++++++- hub/src/web/routes/teams.test.ts | 8 ++++ hub/src/web/routes/teams.ts | 3 +- shared/src/apiTypes.ts | 11 +++-- .../components/teams/TeamSettingsDialog.tsx | 46 ++++++++++++++++--- web/src/lib/locales/en.ts | 4 ++ web/src/lib/locales/zh-CN.ts | 4 ++ 8 files changed, 101 insertions(+), 13 deletions(-) diff --git a/hub/src/teams/teamService.test.ts b/hub/src/teams/teamService.test.ts index 0ab62fc1..d7b68b6e 100644 --- a/hub/src/teams/teamService.test.ts +++ b/hub/src/teams/teamService.test.ts @@ -519,6 +519,26 @@ describe('TeamService web task management', () => { } }) + it('sets a lead on a leaderless team and delivers the brief', async () => { + const { runtime, sessions, delivered } = createRuntime() + const service = new TeamService(new TeamStore(':memory:'), () => {}, runtime) + addCallerSession(sessions) + try { + const team = service.createTeam('alpha', { name: 'Refactor auth' }) + delivered.length = 0 + const updated = service.updateTeamMeta('alpha', team.id, { leadSessionId: 'sess-lead' }) + expect(updated.leadSessionId).toBe('sess-lead') + expect(service.getTeamDetail(team.id, 'alpha')?.members.some((m) => m.role === 'lead')).toBe(true) + await flushAsync() + expect(delivered.some((message) => message.sessionId === 'sess-lead')).toBe(true) + + expect(() => service.updateTeamMeta('alpha', team.id, { leadSessionId: 'sess-missing' })) + .toThrow('Lead session not found') + } finally { + service.close() + } + }) + it('renames, archives and deletes teams from the web', async () => { const { service, team } = setup() try { diff --git a/hub/src/teams/teamService.ts b/hub/src/teams/teamService.ts index b57f3a39..8f9cb8ec 100644 --- a/hub/src/teams/teamService.ts +++ b/hub/src/teams/teamService.ts @@ -454,12 +454,26 @@ export class TeamService { return updated } - /** Rename / archive a team from the web app. */ - updateTeamMeta(namespace: string, teamId: string, patch: { name?: string; status?: 'active' | 'archived' }): TeamRecord { + /** Rename / archive / set-lead from the web app. */ + updateTeamMeta( + namespace: string, + teamId: string, + patch: { name?: string; status?: 'active' | 'archived'; leadSessionId?: string | null } + ): TeamRecord { + if (patch.leadSessionId) { + const view = this.runtime?.resolveSession(patch.leadSessionId) + if (this.runtime && !view) { + throw new TeamServiceError('invalid', 'Lead session not found on this hub') + } + } const updated = this.store.updateTeam(teamId, namespace, patch) if (!updated) { throw new TeamServiceError('not_found', 'Team not found') } + if (patch.leadSessionId) { + this.store.addMember(teamId, patch.leadSessionId, 'lead') + void this.deliverLeadBrief(updated, patch.leadSessionId).catch(() => {}) + } this.publishUpdate(updated) return updated } diff --git a/hub/src/web/routes/teams.test.ts b/hub/src/web/routes/teams.test.ts index 21e06425..89e5b44b 100644 --- a/hub/src/web/routes/teams.test.ts +++ b/hub/src/web/routes/teams.test.ts @@ -193,6 +193,14 @@ describe('Agent Team member routes', () => { }) expect(invalid.status).toBe(400) + const led = await app.request(`/api/teams/${teamId}`, { + method: 'PATCH', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ leadSessionId: 'sess-lead' }) + }) + expect(led.status).toBe(200) + expect((await led.json() as { team: { leadSessionId: string } }).team.leadSessionId).toBe('sess-lead') + const renamed = await app.request(`/api/teams/${teamId}`, { method: 'PATCH', headers: { 'content-type': 'application/json' }, diff --git a/hub/src/web/routes/teams.ts b/hub/src/web/routes/teams.ts index d11f5690..8c179945 100644 --- a/hub/src/web/routes/teams.ts +++ b/hub/src/web/routes/teams.ts @@ -183,7 +183,8 @@ export function createTeamsRoutes(teams: TeamService): Hono { try { const team = teams.updateTeamMeta(c.get('namespace'), c.req.param('id'), { ...(parsed.data.name !== undefined ? { name: parsed.data.name } : {}), - ...(parsed.data.status !== undefined ? { status: parsed.data.status } : {}) + ...(parsed.data.status !== undefined ? { status: parsed.data.status } : {}), + ...(parsed.data.leadSessionId !== undefined ? { leadSessionId: parsed.data.leadSessionId } : {}) }) c.header('Cache-Control', 'no-store') return c.json({ team }) diff --git a/shared/src/apiTypes.ts b/shared/src/apiTypes.ts index 2c760f1f..20278c79 100644 --- a/shared/src/apiTypes.ts +++ b/shared/src/apiTypes.ts @@ -501,10 +501,13 @@ export type CreateTeamTaskRequest = z.infer export const UpdateTeamRequestSchema = z.object({ name: z.string().min(1).max(200).optional(), - status: z.enum(['active', 'archived']).optional() -}).refine((value) => value.name !== undefined || value.status !== undefined, { - message: 'name or status is required' -}) + status: z.enum(['active', 'archived']).optional(), + /** Set (or clear) the lead session. */ + leadSessionId: z.string().min(1).nullable().optional() +}).refine( + (value) => value.name !== undefined || value.status !== undefined || value.leadSessionId !== undefined, + { message: 'name, status or leadSessionId is required' } +) export type UpdateTeamRequest = z.infer diff --git a/web/src/components/teams/TeamSettingsDialog.tsx b/web/src/components/teams/TeamSettingsDialog.tsx index 0cdc99c2..d9b46367 100644 --- a/web/src/components/teams/TeamSettingsDialog.tsx +++ b/web/src/components/teams/TeamSettingsDialog.tsx @@ -1,11 +1,21 @@ import { useEffect, useState } from 'react' import { useMutation, useQueryClient } from '@tanstack/react-query' import type { TeamSummary } from '@/types/team' +import type { SessionSummary } from '@/types/api' import { useAppContext } from '@/lib/app-context' import { useTranslation } from '@/lib/use-translation' import { queryKeys } from '@/lib/query-keys' +import { useSessions } from '@/hooks/queries/useSessions' import { TeamDialog } from './TeamDialog' +function sessionLabel(session: SessionSummary): string { + const name = session.metadata?.name?.trim() + if (name) return name + const path = session.metadata?.path ?? '' + const base = path.split('/').filter(Boolean).pop() + return base ?? session.id.slice(0, 8) +} + export function TeamSettingsDialog(props: { open: boolean team: TeamSummary | null @@ -16,22 +26,31 @@ export function TeamSettingsDialog(props: { const { api } = useAppContext() const { t } = useTranslation() const queryClient = useQueryClient() + const { sessions } = useSessions(api) const [name, setName] = useState('') + const [leadSessionId, setLeadSessionId] = useState('') const [confirmDelete, setConfirmDelete] = useState(false) const [error, setError] = useState(null) useEffect(() => { if (props.team) { setName(props.team.name) + setLeadSessionId(props.team.leadSessionId ?? '') setConfirmDelete(false) setError(null) } }, [props.team]) - const rename = useMutation({ + const leadChanged = Boolean(props.team) && (props.team?.leadSessionId ?? '') !== leadSessionId + const nameChanged = Boolean(props.team) && name.trim().length > 0 && name.trim() !== props.team?.name + + const saveSettings = useMutation({ mutationFn: async () => { if (!api || !props.team) throw new Error('API unavailable') - return await api.updateTeam(props.team.id, { name: name.trim() }) + return await api.updateTeam(props.team.id, { + ...(nameChanged ? { name: name.trim() } : {}), + ...(leadChanged ? { leadSessionId: leadSessionId || null } : {}), + }) }, onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: queryKeys.teams }) @@ -71,7 +90,7 @@ export function TeamSettingsDialog(props: { onError: (mutationError) => setError(mutationError instanceof Error ? mutationError.message : String(mutationError)), }) - const busy = rename.isPending || setStatus.isPending || remove.isPending + const busy = saveSettings.isPending || setStatus.isPending || remove.isPending const archived = props.team?.status === 'archived' return ( @@ -90,11 +109,11 @@ export function TeamSettingsDialog(props: { } @@ -109,6 +128,21 @@ export function TeamSettingsDialog(props: { /> + +