// Department tab: list, create, detail (update/disable/checkpoint), recall. import * as React from 'react' import { request, postJson, bodyError, messageOf } from '../api' import { formatIso } from '../format' import { styles } from '../styles' import { Banner, Btn, Field, Pill, Section } from '../components' import { useApi } from './useApi' import { resolveWorkspace, workspaceQuery } from './workspace' const h = React.createElement const EMPTY_CREATE = { name: '', role: '', scope: 'project', skills: '', allowedTools: '', readGlobal: true, readProject: true } const ENDPOINTS = { list: '/dsh-brain/departments/list', create: '/dsh-brain/departments/create', update: '/dsh-brain/departments/update', disable: '/dsh-brain/departments/disable', checkpoint: '/dsh-brain/departments/checkpoint', recall: '/dsh-brain/departments/recall', } as const export function DepartmentTab(props: { ctx?: any; active?: boolean }): any { const active = props.active !== false const workspace = resolveWorkspace(props.ctx) const [notice, setNotice] = React.useState(null) const [busy, setBusy] = React.useState('') const [form, setForm] = React.useState({ ...EMPTY_CREATE }) const [selectedId, setSelectedId] = React.useState('') const [detail, setDetail] = React.useState({ role: '', currentTask: '', handoff: '' }) const [recallId, setRecallId] = React.useState('') const [recallQuery, setRecallQuery] = React.useState('') const [recallK, setRecallK] = React.useState('5') const [recallHits, setRecallHits] = React.useState([]) const [recallBusy, setRecallBusy] = React.useState(false) const list = useApi(async () => { const result = await request(ENDPOINTS.list + workspaceQuery(workspace)) if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取部门列表失败')) return result.body }, { visible: active, pollMs: 15000 }) const departments: any[] = Array.isArray(list.data?.departments) ? list.data!.departments : [] const selected = departments.filter((item) => item.id === selectedId)[0] React.useEffect(() => { if (selected !== undefined) { setDetail({ role: selected.role ?? '', currentTask: selected.currentTask ?? '', handoff: selected.handoff ?? '' }) } }, [selectedId, list.data]) const handleFailure = (result: any, fallback: string): boolean => { if (result.status === 409) { setNotice({ tone: 'warn', text: '数据已变化,请刷新' }) list.reload() return false } setNotice({ tone: 'bad', text: bodyError(result, fallback) }) return false } const create = async (): Promise => { if (String(form.name).trim() === '') { setNotice({ tone: 'warn', text: '请填写部门名称。' }); return } setBusy('create') try { const result = await postJson(ENDPOINTS.create, { name: String(form.name).trim(), role: String(form.role).trim() || undefined, scope: form.scope, skills: String(form.skills).split(/[,,\s]+/).filter((value) => value !== ''), allowedTools: String(form.allowedTools).split(/[,,\s]+/).filter((value) => value !== ''), readGlobal: form.readGlobal === true, readProject: form.readProject === true, workspace, }) if (result.status !== 200 || result.body === null || result.body.ok !== true) { handleFailure(result, '创建部门失败'); return } setNotice({ tone: 'ok', text: result.body.created === false ? '同名部门已存在,已复用。' : '部门已创建。' }) setForm({ ...EMPTY_CREATE }) list.reload() } catch (failure) { setNotice({ tone: 'bad', text: messageOf(failure) }) } finally { setBusy('') } } const mutate = async (action: 'update' | 'disable' | 'checkpoint'): Promise => { if (selected === undefined) return setBusy(action) const payload: any = { id: selected.id, expectedRevision: selected.revision, workspace } if (action === 'update') { payload.role = detail.role; payload.name = selected.name } if (action === 'checkpoint') { payload.currentTask = detail.currentTask; payload.handoff = detail.handoff } try { const result = await postJson(action === 'update' ? ENDPOINTS.update : action === 'disable' ? ENDPOINTS.disable : ENDPOINTS.checkpoint, payload) if (result.status !== 200 || result.body === null || result.body.ok !== true) { handleFailure(result, '操作失败'); return } setNotice({ tone: 'ok', text: action === 'update' ? '部门已更新。' : action === 'checkpoint' ? '检查点已保存。' : '部门已停用。' }) list.reload() } catch (failure) { setNotice({ tone: 'bad', text: messageOf(failure) }) } finally { setBusy('') } } const recall = async (): Promise => { if (recallId === '') { setNotice({ tone: 'warn', text: '请选择要召回的部门。' }); return } setRecallBusy(true) setRecallHits([]) try { const query = ENDPOINTS.recall + '?id=' + encodeURIComponent(recallId) + '&query=' + encodeURIComponent(recallQuery) + '&k=' + encodeURIComponent(recallK) + (workspace === undefined ? '' : '&workspace=' + encodeURIComponent(workspace)) const result = await request(query) if (result.status !== 200 || result.body === null || result.body.ok !== true) { handleFailure(result, '召回失败'); return } setRecallHits(Array.isArray(result.body.hits) ? result.body.hits : []) } catch (failure) { setNotice({ tone: 'bad', text: messageOf(failure) }) } finally { setRecallBusy(false) } } return h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } }, notice === null ? null : h(Banner, { tone: notice.tone }, notice.text), list.error === null ? null : h(Banner, { tone: 'bad' }, list.error), h(Section, { title: '部门列表', hint: '部门是本机可复用的工作身份;列表显示 scope、状态、当前任务与 revision。', aside: h(Btn, { label: '刷新', variant: 'small', onClick: () => list.reload() }) }, departments.length === 0 ? h('p', { style: styles.hint }, '还没有部门。可在下方创建。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, departments.map((item) => h('div', { key: item.id, style: styles.row }, h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, String(item.name) + '(' + String(item.scope) + ')'), h('div', { style: styles.pills }, h(Pill, { label: '状态', value: item.active === true ? '启用' : '停用', tone: item.active === true ? 'ok' : 'bad' }), h(Pill, { label: 'rev', value: item.revision ?? '—', tone: 'muted' }), ), ), h('p', { style: styles.hint }, '当前任务:' + String(item.currentTask ?? '—') + (item.handoff === undefined ? '' : ' · 交接:' + String(item.handoff))), h('div', { style: styles.actions }, h(Btn, { label: selectedId === item.id ? '收起详情' : '详情 / 编辑', variant: 'small', onClick: () => setSelectedId(selectedId === item.id ? '' : item.id) }), h(Btn, { label: '设为召回对象', variant: 'small', onClick: () => setRecallId(item.id) }), ), selectedId === item.id ? h('div', { style: styles.nested }, h('div', { style: styles.grid }, h(Field, { label: '角色', value: detail.role, onChange: (value) => setDetail({ ...detail, role: value }) }), h(Field, { label: '当前任务', value: detail.currentTask, onChange: (value) => setDetail({ ...detail, currentTask: value }) }), h(Field, { label: '交接说明', value: detail.handoff, onChange: (value) => setDetail({ ...detail, handoff: value }) }), ), h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: busy === 'update' ? '保存中…' : '保存角色', disabled: busy !== '', onClick: () => { void mutate('update') } }), h(Btn, { label: busy === 'checkpoint' ? '保存中…' : '保存检查点', disabled: busy !== '', onClick: () => { void mutate('checkpoint') } }), h(Btn, { variant: 'danger', label: busy === 'disable' ? '停用中…' : '停用部门', disabled: busy !== '' || item.active !== true, onClick: () => { void mutate('disable') } }), ), h('p', { style: styles.hint }, '创建于 ' + formatIso(item.createdAt) + ' · 更新于 ' + formatIso(item.updatedAt)), ) : null, )), ), ), h(Section, { title: '创建部门', hint: '同名部门会幂等复用,不会覆盖原角色。' }, h('div', { style: styles.grid }, h(Field, { label: '名称', value: form.name, onChange: (value) => setForm({ ...form, name: value }) }), h(Field, { label: '角色', value: form.role, onChange: (value) => setForm({ ...form, role: value }) }), h('div', { style: styles.field }, h('span', { style: styles.label }, '范围'), h('select', { value: form.scope, style: styles.input, onChange: (event) => setForm({ ...form, scope: event.target.value }) }, h('option', { value: 'project' }, '项目'), h('option', { value: 'global' }, '全局'), ), ), h(Field, { label: '技能(逗号分隔)', value: form.skills, onChange: (value) => setForm({ ...form, skills: value }) }), h(Field, { label: '允许工具(逗号分隔)', value: form.allowedTools, onChange: (value) => setForm({ ...form, allowedTools: value }) }), ), h('div', { style: styles.checkboxRow }, h('label', null, h('input', { type: 'checkbox', checked: form.readGlobal === true, onChange: (event) => setForm({ ...form, readGlobal: event.target.checked }) }), ' 可读全局记忆'), h('label', null, h('input', { type: 'checkbox', checked: form.readProject === true, onChange: (event) => setForm({ ...form, readProject: event.target.checked }) }), ' 可读项目记忆'), ), h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: busy === 'create' ? '创建中…' : '创建部门', disabled: busy !== '', onClick: () => { void create() } })), ), h(Section, { title: '部门召回', hint: '召回结果只展示服务端截断后的 contentSnippet(≤600 字符),并标注来源。' }, h('div', { style: styles.grid }, h('div', { style: styles.field }, h('span', { style: styles.label }, '部门'), h('select', { value: recallId, style: styles.input, onChange: (event) => setRecallId(event.target.value) }, h('option', { value: '' }, '选择部门…'), departments.map((item) => h('option', { key: item.id, value: item.id }, String(item.name))), ), ), h(Field, { label: '查询', value: recallQuery, onChange: setRecallQuery }), h(Field, { label: '数量(1-5)', type: 'number', value: recallK, onChange: setRecallK }), ), h('div', { style: styles.actions }, h(Btn, { label: recallBusy ? '召回中…' : '查询召回', disabled: recallBusy, onClick: () => { void recall() } })), recallHits.length === 0 ? h('p', { style: styles.hint }, '暂无召回结果。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, recallHits.map((hit: any, index: number) => h('div', { key: String(index), style: styles.row }, h('div', { style: styles.rowHead }, h('div', { style: styles.pills }, h(Pill, { label: '来源', value: hit.origin ?? '—', tone: 'muted' }), hit.record?.category === undefined ? null : h(Pill, { label: '分类', value: hit.record.category, tone: 'muted' }), )), h('div', { style: styles.clamp2 }, String(hit.contentSnippet ?? hit.content ?? '')), )), ), ), ) }