diff --git a/client.js b/client.js index 7afec6a..bb6ec4e 100644 --- a/client.js +++ b/client.js @@ -453,6 +453,28 @@ body[data-ds-dark-theme] .ssw-float,html[data-theme=dark] .ssw-float{background: .ssw-inline-bar{flex:none;display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px;border-bottom:1px solid var(--ssw-line);background:var(--ssw-card);font-size:12px;color:var(--ssw-muted)} .ssw-link{color:var(--ssw-blue);font-size:12px;padding:2px 4px} @media (prefers-reduced-motion:reduce){.ssw-scope *{transition:none!important}} +.ssw-tabs{display:flex;gap:6px;margin:12px 16px 0;padding:4px;border:1px solid var(--ssw-line);border-radius:9px;background:var(--ssw-side)} +.ssw-tab{flex:1;display:flex;align-items:center;justify-content:center;gap:6px;padding:7px 6px;border-radius:6px;font-size:12px;color:var(--ssw-muted)} +.ssw-tab.is-active{background:var(--ssw-card);color:var(--ssw-blue);font-weight:600;box-shadow:0 1px 2px rgba(16,24,40,.06)} +.ssw-tab-count{font-size:10px;color:var(--ssw-faint)} +.ssw-page{padding:12px 16px 0} +.ssw-page-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px} +.ssw-page-head strong{font-size:13px} +.ssw-list{display:flex;flex-direction:column;gap:8px} +.ssw-row{border:1px solid var(--ssw-line);border-radius:9px;padding:9px 11px;background:var(--ssw-card)} +.ssw-row-top{display:flex;align-items:center;justify-content:space-between;gap:8px} +.ssw-row-title{font-size:13px} +.ssw-row-meta{margin:4px 0 0;font-size:11px;color:var(--ssw-muted)} +.ssw-row-note{margin:4px 0 0;font-size:10px;color:var(--ssw-faint)} +.ssw-row-actions{display:flex;gap:10px;margin-top:6px} +.ssw-link{color:var(--ssw-blue);font-size:12px;padding:2px 2px} +.ssw-link-muted{color:var(--ssw-muted)} +.ssw-badge{font-size:10px;padding:2px 6px;border-radius:999px;background:var(--ssw-wash);color:var(--ssw-blue)} +.ssw-badge[data-status=done]{background:rgba(22,163,74,.12);color:var(--ssw-ok)} +.ssw-badge[data-status=cancelled]{background:rgba(107,114,128,.14);color:var(--ssw-muted)} +.ssw-badge[data-status=failed]{background:rgba(220,38,38,.12);color:var(--ssw-danger)} +.ssw-form{margin-bottom:12px;padding:10px;border:1px solid var(--ssw-line);border-radius:9px;background:var(--ssw-side)} +.ssw-empty-line{margin:10px 0;font-size:12px;color:var(--ssw-faint)} .ssw-tab-body{height:100%;min-height:0;display:flex;flex-direction:column} .ssw-tab-body .ssw-root{flex:1;min-height:0} .ssw-tab-title{font-size:13px} @@ -1157,6 +1179,13 @@ function apply(ctx) { const [busy, setBusy] = (0, import_react.useState)(false); const [configOpen, setConfigOpen] = (0, import_react.useState)(false); const [reportOpen, setReportOpen] = (0, import_react.useState)(false); + const [tab, setTab] = (0, import_react.useState)("court"); + const [tasks, setTasks] = (0, import_react.useState)([]); + const [artifacts, setArtifacts] = (0, import_react.useState)([]); + const [taskFormOpen, setTaskFormOpen] = (0, import_react.useState)(false); + const [taskDraft, setTaskDraft] = (0, import_react.useState)({ title: "", goal: "", ownerSeatId: SEAT_IDS[0] }); + const [artifactFormOpen, setArtifactFormOpen] = (0, import_react.useState)(false); + const [artifactDraft, setArtifactDraft] = (0, import_react.useState)({ title: "", path: "", summary: "" }); const alive = (0, import_react.useRef)(true); (0, import_react.useEffect)(() => () => { alive.current = false; @@ -1183,9 +1212,23 @@ function apply(ctx) { } catch (modelError) { console.warn("[sansheng] 模型目录加载失败:", modelError && modelError.message ? modelError.message : modelError); } + let taskList = []; + let artifactList = []; + try { + taskList = (await request("tasks", { projectId: projectView.project.id })).tasks ?? []; + } catch (taskError) { + console.warn("[sansheng] 任务加载失败:", taskError && taskError.message ? taskError.message : taskError); + } + try { + artifactList = (await request("artifacts", { projectId: projectView.project.id })).artifacts ?? []; + } catch (artifactError) { + console.warn("[sansheng] 成果加载失败:", artifactError && artifactError.message ? artifactError.message : artifactError); + } if (!alive.current) return; setView(projectView); setModels(modelCatalog); + setTasks(taskList); + setArtifacts(artifactList); setPhase("ready"); } catch (loadError) { if (!alive.current) return; @@ -1270,6 +1313,88 @@ function apply(ctx) { if (alive.current) setBusy(false); } } + async function reloadTasks(projectId) { + try { + const result = await request("tasks", { projectId }); + if (alive.current) setTasks(result.tasks ?? []); + } catch (error2) { + if (alive.current) setNotice(error2 && error2.message ? error2.message : "任务列表刷新失败"); + } + } + async function createTask() { + const projectId = view && view.project ? view.project.id : null; + if (!projectId) return; + const title = taskDraft.title.trim(); + if (!title) { + setNotice("任务标题不能为空"); + return; + } + setBusy(true); + setError(null); + try { + await request("taskCreate", { projectId, title, goal: taskDraft.goal, ownerSeatId: taskDraft.ownerSeatId }); + setTaskDraft({ title: "", goal: "", ownerSeatId: taskDraft.ownerSeatId }); + setTaskFormOpen(false); + await reloadTasks(projectId); + setNotice("已存为草稿;点「派工」才会交给部门。"); + } catch (error2) { + if (alive.current) setError(error2 && error2.message ? error2.message : "新建任务失败"); + } finally { + if (alive.current) setBusy(false); + } + } + async function dispatchTask(taskId) { + const projectId = view && view.project ? view.project.id : null; + if (!projectId) return; + setBusy(true); + setError(null); + try { + const result = await request("taskDispatch", { projectId, taskId }); + await reloadTasks(projectId); + setNotice(result.delivered ? "已派工到该部门会话。" : "已记录为待执行(当前宿主暂不支持直接发送)。"); + } catch (error2) { + if (alive.current) setError(error2 && error2.message ? error2.message : "派工失败"); + } finally { + if (alive.current) setBusy(false); + } + } + async function cancelTask(taskId) { + const projectId = view && view.project ? view.project.id : null; + if (!projectId) return; + setBusy(true); + setError(null); + try { + await request("taskCancel", { projectId, taskId }); + await reloadTasks(projectId); + setNotice("任务已取消。"); + } catch (error2) { + if (alive.current) setError(error2 && error2.message ? error2.message : "取消失败"); + } finally { + if (alive.current) setBusy(false); + } + } + async function addArtifact() { + const projectId = view && view.project ? view.project.id : null; + if (!projectId) return; + const title = artifactDraft.title.trim(); + if (!title) { + setNotice("成果名称不能为空"); + return; + } + setBusy(true); + setError(null); + try { + const result = await request("artifactAdd", { projectId, title, path: artifactDraft.path, summary: artifactDraft.summary }); + setArtifactDraft({ title: "", path: "", summary: "" }); + setArtifactFormOpen(false); + setArtifacts((list) => [...list, result.artifact]); + setNotice("成果已登记。"); + } catch (error2) { + if (alive.current) setError(error2 && error2.message ? error2.message : "登记成果失败"); + } finally { + if (alive.current) setBusy(false); + } + } const seatCard = (seat) => (0, import_react.createElement)( "button", { @@ -1445,6 +1570,100 @@ function apply(ctx) { ) : null ); const statusEl = error ? (0, import_react.createElement)("p", { className: "ssw-error", role: "alert" }, error) : notice ? (0, import_react.createElement)("p", { className: "ssw-notice", role: "status" }, notice) : null; + const tabCounts = { court: wb.project.seatCount, tasks: tasks.length, artifacts: artifacts.length }; + const tabBar = (0, import_react.createElement)( + "div", + { className: "ssw-tabs", role: "tablist" }, + [["court", "朝堂"], ["tasks", "任务"], ["artifacts", "成果"]].map(([id, label]) => (0, import_react.createElement)("button", { + key: id, + type: "button", + role: "tab", + "aria-selected": tab === id ? "true" : "false", + className: "ssw-tab" + (tab === id ? " is-active" : ""), + onClick: () => setTab(id) + }, (0, import_react.createElement)("span", null, label), (0, import_react.createElement)("span", { className: "ssw-tab-count" }, String(tabCounts[id] ?? 0)))) + ); + const TASK_STATUS_LABEL = { draft: "草稿", prepared: "待执行", dispatched: "已派工", done: "已完成", cancelled: "已取消", failed: "失败" }; + const taskRows = tasks.length === 0 ? (0, import_react.createElement)("p", { className: "ssw-empty-line" }, "这个项目还没有任务。点「新建任务」先存一张草稿。") : tasks.map((task) => (0, import_react.createElement)( + "article", + { key: task.id, className: "ssw-row", "data-task": task.id }, + (0, import_react.createElement)( + "div", + { className: "ssw-row-top" }, + (0, import_react.createElement)("strong", { className: "ssw-row-title" }, task.title), + (0, import_react.createElement)("span", { className: "ssw-badge", "data-status": task.status }, TASK_STATUS_LABEL[task.status] ?? task.status) + ), + (0, import_react.createElement)("p", { className: "ssw-row-meta" }, (SEATS[task.ownerSeatId]?.label ?? task.ownerSeatId) + (task.goal ? " · " + task.goal : "")), + task.note ? (0, import_react.createElement)("p", { className: "ssw-row-note" }, task.note) : null, + (0, import_react.createElement)( + "div", + { className: "ssw-row-actions" }, + task.status === "draft" || task.status === "prepared" || task.status === "failed" ? (0, import_react.createElement)("button", { type: "button", className: "ssw-link", disabled: busy, onClick: () => dispatchTask(task.id) }, task.status === "prepared" ? "重试派工" : "派工") : null, + task.status !== "done" && task.status !== "cancelled" ? (0, import_react.createElement)("button", { type: "button", className: "ssw-link ssw-link-muted", disabled: busy, onClick: () => cancelTask(task.id) }, "取消") : null + ) + )); + const taskView = (0, import_react.createElement)( + "section", + { className: "ssw-page" }, + (0, import_react.createElement)( + "div", + { className: "ssw-page-head" }, + (0, import_react.createElement)("strong", null, "任务 · " + tasks.length), + (0, import_react.createElement)("button", { type: "button", className: "ssw-primary", onClick: () => setTaskFormOpen((value) => !value) }, taskFormOpen ? "收起" : "新建任务") + ), + taskFormOpen ? (0, import_react.createElement)( + "form", + { className: "ssw-form", onSubmit: (event) => { + event.preventDefault(); + void createTask(); + } }, + (0, import_react.createElement)("label", { className: "ssw-label" }, "标题"), + (0, import_react.createElement)("input", { className: "ssw-field", value: taskDraft.title, placeholder: "交给部门做什么", onChange: (event) => setTaskDraft({ ...taskDraft, title: event.target.value }) }), + (0, import_react.createElement)("label", { className: "ssw-label" }, "目标(可选)"), + (0, import_react.createElement)("textarea", { className: "ssw-field ssw-textarea", rows: 3, value: taskDraft.goal, onChange: (event) => setTaskDraft({ ...taskDraft, goal: event.target.value }) }), + (0, import_react.createElement)("label", { className: "ssw-label" }, "主责部门"), + (0, import_react.createElement)( + "select", + { className: "ssw-field", value: taskDraft.ownerSeatId, onChange: (event) => setTaskDraft({ ...taskDraft, ownerSeatId: event.target.value }) }, + SEAT_IDS.map((id) => (0, import_react.createElement)("option", { key: id, value: id }, SEATS[id].label)) + ), + (0, import_react.createElement)("div", { className: "ssw-save-row" }, (0, import_react.createElement)("button", { type: "submit", className: "ssw-primary", disabled: busy }, busy ? "保存中…" : "保存草稿")) + ) : null, + (0, import_react.createElement)("div", { className: "ssw-list" }, taskRows) + ); + const artifactRows = artifacts.length === 0 ? (0, import_react.createElement)("p", { className: "ssw-empty-line" }, "这个项目还没有登记成果。") : artifacts.map((item) => (0, import_react.createElement)( + "article", + { key: item.id, className: "ssw-row", "data-artifact": item.id }, + (0, import_react.createElement)("div", { className: "ssw-row-top" }, (0, import_react.createElement)("strong", { className: "ssw-row-title" }, item.title)), + item.summary ? (0, import_react.createElement)("p", { className: "ssw-row-meta" }, item.summary) : null, + item.path ? (0, import_react.createElement)("p", { className: "ssw-row-note" }, item.path) : null + )); + const artifactView = (0, import_react.createElement)( + "section", + { className: "ssw-page" }, + (0, import_react.createElement)( + "div", + { className: "ssw-page-head" }, + (0, import_react.createElement)("strong", null, "成果 · " + artifacts.length), + (0, import_react.createElement)("button", { type: "button", className: "ssw-primary", onClick: () => setArtifactFormOpen((value) => !value) }, artifactFormOpen ? "收起" : "登记成果") + ), + artifactFormOpen ? (0, import_react.createElement)( + "form", + { className: "ssw-form", onSubmit: (event) => { + event.preventDefault(); + void addArtifact(); + } }, + (0, import_react.createElement)("label", { className: "ssw-label" }, "成果名称"), + (0, import_react.createElement)("input", { className: "ssw-field", value: artifactDraft.title, onChange: (event) => setArtifactDraft({ ...artifactDraft, title: event.target.value }) }), + (0, import_react.createElement)("label", { className: "ssw-label" }, "文件路径(可选)"), + (0, import_react.createElement)("input", { className: "ssw-field", value: artifactDraft.path, onChange: (event) => setArtifactDraft({ ...artifactDraft, path: event.target.value }) }), + (0, import_react.createElement)("label", { className: "ssw-label" }, "说明(可选)"), + (0, import_react.createElement)("textarea", { className: "ssw-field ssw-textarea", rows: 3, value: artifactDraft.summary, onChange: (event) => setArtifactDraft({ ...artifactDraft, summary: event.target.value }) }), + (0, import_react.createElement)("div", { className: "ssw-save-row" }, (0, import_react.createElement)("button", { type: "submit", className: "ssw-primary", disabled: busy }, busy ? "保存中…" : "保存")) + ) : null, + (0, import_react.createElement)("div", { className: "ssw-list" }, artifactRows) + ); + const courtView = (0, import_react.createElement)(import_react.Fragment, null, enableBlock, tree, report, currentCard, config); let body; if (phase === "loading") body = (0, import_react.createElement)("div", { className: "ssw-loading" }, "正在加载工作台…"); else if (phase === "no-workspace") body = (0, import_react.createElement)( @@ -1460,7 +1679,14 @@ function apply(ctx) { (0, import_react.createElement)("p", { className: "ssw-error" }, error || "加载失败"), errorRetryable ? (0, import_react.createElement)("button", { type: "button", className: "ssw-primary", onClick: loadProject }, "重试") : null ); - else body = (0, import_react.createElement)(import_react.Fragment, null, projectInfo, enableBlock, tree, report, currentCard, config, statusEl); + else body = (0, import_react.createElement)( + import_react.Fragment, + null, + projectInfo, + tabBar, + tab === "tasks" ? taskView : tab === "artifacts" ? artifactView : courtView, + statusEl + ); return (0, import_react.createElement)("div", { className: "ssw-root ssw-scope" }, head, body); } const NAV_PANEL_ID = "sansheng-workbench"; diff --git a/package.json b/package.json index 9439e61..04f5b55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-sansheng-liubu", - "version": "0.1.5", + "version": "0.2.0", "description": "三省六部:每个项目固定十席角色与独立会话的 DSH 插件(第一版)。", "keywords": [ "dsh-plugin", diff --git a/src/client/index.js b/src/client/index.js index 7079c77..21a3cea 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -312,6 +312,13 @@ export function apply(ctx) { const [busy, setBusy] = useState(false) const [configOpen, setConfigOpen] = useState(false) const [reportOpen, setReportOpen] = useState(false) + const [tab, setTab] = useState('court') + const [tasks, setTasks] = useState([]) + const [artifacts, setArtifacts] = useState([]) + const [taskFormOpen, setTaskFormOpen] = useState(false) + const [taskDraft, setTaskDraft] = useState({ title: '', goal: '', ownerSeatId: SEAT_IDS[0] }) + const [artifactFormOpen, setArtifactFormOpen] = useState(false) + const [artifactDraft, setArtifactDraft] = useState({ title: '', path: '', summary: '' }) const alive = useRef(true) useEffect(() => () => { alive.current = false }, []) @@ -330,9 +337,19 @@ export function apply(ctx) { try { modelCatalog = await request('models') } catch (modelError) { console.warn('[sansheng] 模型目录加载失败:', modelError && modelError.message ? modelError.message : modelError) } + let taskList = [] + let artifactList = [] + try { taskList = (await request('tasks', { projectId: projectView.project.id })).tasks ?? [] } catch (taskError) { + console.warn('[sansheng] 任务加载失败:', taskError && taskError.message ? taskError.message : taskError) + } + try { artifactList = (await request('artifacts', { projectId: projectView.project.id })).artifacts ?? [] } catch (artifactError) { + console.warn('[sansheng] 成果加载失败:', artifactError && artifactError.message ? artifactError.message : artifactError) + } if (!alive.current) return setView(projectView) setModels(modelCatalog) + setTasks(taskList) + setArtifacts(artifactList) setPhase('ready') } catch (loadError) { if (!alive.current) return @@ -419,6 +436,75 @@ export function apply(ctx) { } } + async function reloadTasks(projectId) { + try { + const result = await request('tasks', { projectId }) + if (alive.current) setTasks(result.tasks ?? []) + } catch (error) { + if (alive.current) setNotice(error && error.message ? error.message : '任务列表刷新失败') + } + } + + async function createTask() { + const projectId = view && view.project ? view.project.id : null + if (!projectId) return + const title = taskDraft.title.trim() + if (!title) { setNotice('任务标题不能为空'); return } + setBusy(true); setError(null) + try { + await request('taskCreate', { projectId, title, goal: taskDraft.goal, ownerSeatId: taskDraft.ownerSeatId }) + setTaskDraft({ title: '', goal: '', ownerSeatId: taskDraft.ownerSeatId }) + setTaskFormOpen(false) + await reloadTasks(projectId) + setNotice('已存为草稿;点「派工」才会交给部门。') + } catch (error) { + if (alive.current) setError(error && error.message ? error.message : '新建任务失败') + } finally { if (alive.current) setBusy(false) } + } + + async function dispatchTask(taskId) { + const projectId = view && view.project ? view.project.id : null + if (!projectId) return + setBusy(true); setError(null) + try { + const result = await request('taskDispatch', { projectId, taskId }) + await reloadTasks(projectId) + setNotice(result.delivered ? '已派工到该部门会话。' : '已记录为待执行(当前宿主暂不支持直接发送)。') + } catch (error) { + if (alive.current) setError(error && error.message ? error.message : '派工失败') + } finally { if (alive.current) setBusy(false) } + } + + async function cancelTask(taskId) { + const projectId = view && view.project ? view.project.id : null + if (!projectId) return + setBusy(true); setError(null) + try { + await request('taskCancel', { projectId, taskId }) + await reloadTasks(projectId) + setNotice('任务已取消。') + } catch (error) { + if (alive.current) setError(error && error.message ? error.message : '取消失败') + } finally { if (alive.current) setBusy(false) } + } + + async function addArtifact() { + const projectId = view && view.project ? view.project.id : null + if (!projectId) return + const title = artifactDraft.title.trim() + if (!title) { setNotice('成果名称不能为空'); return } + setBusy(true); setError(null) + try { + const result = await request('artifactAdd', { projectId, title, path: artifactDraft.path, summary: artifactDraft.summary }) + setArtifactDraft({ title: '', path: '', summary: '' }) + setArtifactFormOpen(false) + setArtifacts(list => [...list, result.artifact]) + setNotice('成果已登记。') + } catch (error) { + if (alive.current) setError(error && error.message ? error.message : '登记成果失败') + } finally { if (alive.current) setBusy(false) } + } + const seatCard = (seat) => h('button', { key: seat.id, type: 'button', className: 'ssw-seat', 'aria-pressed': seat.selected ? 'true' : 'false', @@ -554,6 +640,70 @@ export function apply(ctx) { ? h('p', { className: 'ssw-error', role: 'alert' }, error) : (notice ? h('p', { className: 'ssw-notice', role: 'status' }, notice) : null) + const tabCounts = { court: wb.project.seatCount, tasks: tasks.length, artifacts: artifacts.length } + const tabBar = h('div', { className: 'ssw-tabs', role: 'tablist' }, + [['court', '朝堂'], ['tasks', '任务'], ['artifacts', '成果']].map(([id, label]) => h('button', { + key: id, type: 'button', role: 'tab', 'aria-selected': tab === id ? 'true' : 'false', + className: 'ssw-tab' + (tab === id ? ' is-active' : ''), onClick: () => setTab(id), + }, h('span', null, label), h('span', { className: 'ssw-tab-count' }, String(tabCounts[id] ?? 0))))) + + const TASK_STATUS_LABEL = { draft: '草稿', prepared: '待执行', dispatched: '已派工', done: '已完成', cancelled: '已取消', failed: '失败' } + const taskRows = tasks.length === 0 + ? h('p', { className: 'ssw-empty-line' }, '这个项目还没有任务。点「新建任务」先存一张草稿。') + : tasks.map(task => h('article', { key: task.id, className: 'ssw-row', 'data-task': task.id }, + h('div', { className: 'ssw-row-top' }, + h('strong', { className: 'ssw-row-title' }, task.title), + h('span', { className: 'ssw-badge', 'data-status': task.status }, TASK_STATUS_LABEL[task.status] ?? task.status)), + h('p', { className: 'ssw-row-meta' }, (SEATS[task.ownerSeatId]?.label ?? task.ownerSeatId) + (task.goal ? ' · ' + task.goal : '')), + task.note ? h('p', { className: 'ssw-row-note' }, task.note) : null, + h('div', { className: 'ssw-row-actions' }, + (task.status === 'draft' || task.status === 'prepared' || task.status === 'failed') + ? h('button', { type: 'button', className: 'ssw-link', disabled: busy, onClick: () => dispatchTask(task.id) }, task.status === 'prepared' ? '重试派工' : '派工') + : null, + (task.status !== 'done' && task.status !== 'cancelled') + ? h('button', { type: 'button', className: 'ssw-link ssw-link-muted', disabled: busy, onClick: () => cancelTask(task.id) }, '取消') + : null))) + + const taskView = h('section', { className: 'ssw-page' }, + h('div', { className: 'ssw-page-head' }, + h('strong', null, '任务 · ' + tasks.length), + h('button', { type: 'button', className: 'ssw-primary', onClick: () => setTaskFormOpen(value => !value) }, taskFormOpen ? '收起' : '新建任务')), + taskFormOpen ? h('form', { className: 'ssw-form', onSubmit: event => { event.preventDefault(); void createTask() } }, + h('label', { className: 'ssw-label' }, '标题'), + h('input', { className: 'ssw-field', value: taskDraft.title, placeholder: '交给部门做什么', onChange: event => setTaskDraft({ ...taskDraft, title: event.target.value }) }), + h('label', { className: 'ssw-label' }, '目标(可选)'), + h('textarea', { className: 'ssw-field ssw-textarea', rows: 3, value: taskDraft.goal, onChange: event => setTaskDraft({ ...taskDraft, goal: event.target.value }) }), + h('label', { className: 'ssw-label' }, '主责部门'), + h('select', { className: 'ssw-field', value: taskDraft.ownerSeatId, onChange: event => setTaskDraft({ ...taskDraft, ownerSeatId: event.target.value }) }, + SEAT_IDS.map(id => h('option', { key: id, value: id }, SEATS[id].label))), + h('div', { className: 'ssw-save-row' }, h('button', { type: 'submit', className: 'ssw-primary', disabled: busy }, busy ? '保存中…' : '保存草稿')), + ) : null, + h('div', { className: 'ssw-list' }, taskRows)) + + const artifactRows = artifacts.length === 0 + ? h('p', { className: 'ssw-empty-line' }, '这个项目还没有登记成果。') + : artifacts.map(item => h('article', { key: item.id, className: 'ssw-row', 'data-artifact': item.id }, + h('div', { className: 'ssw-row-top' }, h('strong', { className: 'ssw-row-title' }, item.title)), + item.summary ? h('p', { className: 'ssw-row-meta' }, item.summary) : null, + item.path ? h('p', { className: 'ssw-row-note' }, item.path) : null)) + + const artifactView = h('section', { className: 'ssw-page' }, + h('div', { className: 'ssw-page-head' }, + h('strong', null, '成果 · ' + artifacts.length), + h('button', { type: 'button', className: 'ssw-primary', onClick: () => setArtifactFormOpen(value => !value) }, artifactFormOpen ? '收起' : '登记成果')), + artifactFormOpen ? h('form', { className: 'ssw-form', onSubmit: event => { event.preventDefault(); void addArtifact() } }, + h('label', { className: 'ssw-label' }, '成果名称'), + h('input', { className: 'ssw-field', value: artifactDraft.title, onChange: event => setArtifactDraft({ ...artifactDraft, title: event.target.value }) }), + h('label', { className: 'ssw-label' }, '文件路径(可选)'), + h('input', { className: 'ssw-field', value: artifactDraft.path, onChange: event => setArtifactDraft({ ...artifactDraft, path: event.target.value }) }), + h('label', { className: 'ssw-label' }, '说明(可选)'), + h('textarea', { className: 'ssw-field ssw-textarea', rows: 3, value: artifactDraft.summary, onChange: event => setArtifactDraft({ ...artifactDraft, summary: event.target.value }) }), + h('div', { className: 'ssw-save-row' }, h('button', { type: 'submit', className: 'ssw-primary', disabled: busy }, busy ? '保存中…' : '保存')), + ) : null, + h('div', { className: 'ssw-list' }, artifactRows)) + + const courtView = h(Fragment, null, enableBlock, tree, report, currentCard, config) + let body if (phase === 'loading') body = h('div', { className: 'ssw-loading' }, '正在加载工作台…') else if (phase === 'no-workspace') body = h('div', { className: 'ssw-empty' }, @@ -567,7 +717,8 @@ export function apply(ctx) { ? h('button', { type: 'button', className: 'ssw-primary', onClick: loadProject }, '重试') : null, ) - else body = h(Fragment, null, projectInfo, enableBlock, tree, report, currentCard, config, statusEl) + else body = h(Fragment, null, projectInfo, tabBar, + tab === 'tasks' ? taskView : tab === 'artifacts' ? artifactView : courtView, statusEl) return h('div', { className: 'ssw-root ssw-scope' }, head, body) } diff --git a/src/client/styles.js b/src/client/styles.js index bd7d6fa..12c5c21 100644 --- a/src/client/styles.js +++ b/src/client/styles.js @@ -85,6 +85,28 @@ body[data-ds-dark-theme] .ssw-float,html[data-theme=dark] .ssw-float{background: .ssw-inline-bar{flex:none;display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px;border-bottom:1px solid var(--ssw-line);background:var(--ssw-card);font-size:12px;color:var(--ssw-muted)} .ssw-link{color:var(--ssw-blue);font-size:12px;padding:2px 4px} @media (prefers-reduced-motion:reduce){.ssw-scope *{transition:none!important}} +.ssw-tabs{display:flex;gap:6px;margin:12px 16px 0;padding:4px;border:1px solid var(--ssw-line);border-radius:9px;background:var(--ssw-side)} +.ssw-tab{flex:1;display:flex;align-items:center;justify-content:center;gap:6px;padding:7px 6px;border-radius:6px;font-size:12px;color:var(--ssw-muted)} +.ssw-tab.is-active{background:var(--ssw-card);color:var(--ssw-blue);font-weight:600;box-shadow:0 1px 2px rgba(16,24,40,.06)} +.ssw-tab-count{font-size:10px;color:var(--ssw-faint)} +.ssw-page{padding:12px 16px 0} +.ssw-page-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px} +.ssw-page-head strong{font-size:13px} +.ssw-list{display:flex;flex-direction:column;gap:8px} +.ssw-row{border:1px solid var(--ssw-line);border-radius:9px;padding:9px 11px;background:var(--ssw-card)} +.ssw-row-top{display:flex;align-items:center;justify-content:space-between;gap:8px} +.ssw-row-title{font-size:13px} +.ssw-row-meta{margin:4px 0 0;font-size:11px;color:var(--ssw-muted)} +.ssw-row-note{margin:4px 0 0;font-size:10px;color:var(--ssw-faint)} +.ssw-row-actions{display:flex;gap:10px;margin-top:6px} +.ssw-link{color:var(--ssw-blue);font-size:12px;padding:2px 2px} +.ssw-link-muted{color:var(--ssw-muted)} +.ssw-badge{font-size:10px;padding:2px 6px;border-radius:999px;background:var(--ssw-wash);color:var(--ssw-blue)} +.ssw-badge[data-status=done]{background:rgba(22,163,74,.12);color:var(--ssw-ok)} +.ssw-badge[data-status=cancelled]{background:rgba(107,114,128,.14);color:var(--ssw-muted)} +.ssw-badge[data-status=failed]{background:rgba(220,38,38,.12);color:var(--ssw-danger)} +.ssw-form{margin-bottom:12px;padding:10px;border:1px solid var(--ssw-line);border-radius:9px;background:var(--ssw-side)} +.ssw-empty-line{margin:10px 0;font-size:12px;color:var(--ssw-faint)} .ssw-tab-body{height:100%;min-height:0;display:flex;flex-direction:column} .ssw-tab-body .ssw-root{flex:1;min-height:0} .ssw-tab-title{font-size:13px} diff --git a/src/host/api.js b/src/host/api.js index 7db66dd..884f31e 100644 --- a/src/host/api.js +++ b/src/host/api.js @@ -10,6 +10,11 @@ function requireProjectId(payload) { if (typeof id !== 'string' || id.trim() === '') throw bad('缺少 projectId') return id } +function requireTaskId(payload) { + const id = payload?.taskId + if (typeof id !== 'string' || id.trim() === '') throw bad('缺少 taskId') + return id +} // 生成 POST /api/sansheng 的处理函数:{endpoint,payload} -> {ok,value}|{ok,error} export function createApiHandler(service) { @@ -32,6 +37,18 @@ export function createApiHandler(service) { } case 'models': return { ok: true, value: await service.models() } + case 'tasks': + return { ok: true, value: await service.tasks(requireProjectId(payload)) } + case 'taskCreate': + return { ok: true, value: await service.taskCreate(requireProjectId(payload), payload ?? {}) } + case 'taskDispatch': + return { ok: true, value: await service.taskDispatch(requireProjectId(payload), requireTaskId(payload)) } + case 'taskCancel': + return { ok: true, value: await service.taskCancel(requireProjectId(payload), requireTaskId(payload)) } + case 'artifacts': + return { ok: true, value: await service.artifacts(requireProjectId(payload)) } + case 'artifactAdd': + return { ok: true, value: await service.artifactAdd(requireProjectId(payload), payload ?? {}) } default: throw bad(`未知 endpoint ${String(endpoint)}`) } diff --git a/src/host/service.js b/src/host/service.js index d4f2501..8bd9af7 100644 --- a/src/host/service.js +++ b/src/host/service.js @@ -1,4 +1,5 @@ import { SEAT_IDS, SEATS, SEAT_GROUPS } from '../shared/roles.js' +import { emptyTask, emptyArtifact } from '../shared/schema.js' import { applyModel, normalizeSelection } from './model.js' export class ServiceError extends Error { @@ -156,4 +157,119 @@ export class SanshengService { const catalog = await this.host.sessionController.modelCatalog() return normalizeCatalog(catalog) } + + async tasks(projectId) { + await this.store.ensureLoaded() + const project = this.store.get(projectId) + if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + return { tasks: [...(project.tasks ?? [])] } + } + + async taskCreate(projectId, input = {}) { + await this.store.ensureLoaded() + if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + const title = String(input.title ?? '').trim() + if (!title) throw new ServiceError('sansheng/invalid', '任务标题不能为空') + if (!SEAT_IDS.includes(input.ownerSeatId)) throw new ServiceError('sansheng/invalid', `未知主责席 ${String(input.ownerSeatId)}`) + const goal = typeof input.goal === 'string' ? input.goal : '' + const task = await this.store.mutate(s => { + const project = s.projects[projectId] + project.taskSeq = (project.taskSeq ?? 0) + 1 + const next = emptyTask({ id: `task-${project.taskSeq}`, title, goal, ownerSeatId: input.ownerSeatId }) + if (!Array.isArray(project.tasks)) project.tasks = [] + project.tasks.push(next) + project.updatedAt = Date.now() + project.revision = (project.revision ?? 0) + 1 + return { ...next } + }) + return { task } + } + + async taskDispatch(projectId, taskId) { + await this.store.ensureLoaded() + const project = this.store.get(projectId) + if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + const task = (project.tasks ?? []).find(item => item.id === taskId) + if (!task) throw new ServiceError('sansheng/not-found', `任务 ${taskId} 不存在`) + if (task.status === 'done' || task.status === 'cancelled') throw new ServiceError('sansheng/invalid', '已完成或已取消的任务不能再次派工') + const seat = project.seats[task.ownerSeatId] + if (!seat || seat.status !== 'bound' || !seat.sessionId) { + throw new ServiceError('sansheng/seat-unbound', `请先为「${SEATS[task.ownerSeatId]?.label ?? task.ownerSeatId}」接通会话再派工`) + } + const text = `【任务】${task.title}${task.goal ? '\n' + task.goal : ''}` + let delivered = false + let note = '' + const controller = this.host.sessionController + for (const name of ['sendMessage', 'appendMessage', 'prompt', 'sendUserMessage']) { + if (typeof controller?.[name] !== 'function') continue + try { + await controller[name]({ sessionId: seat.sessionId, text, content: text }) + delivered = true + break + } catch (error) { + note = `发送失败:${error?.message ?? error}` + } + } + const next = await this.store.mutate(s => { + const target = s.projects[projectId].tasks.find(item => item.id === taskId) + target.status = delivered ? 'dispatched' : 'prepared' + target.note = delivered ? '' : (note || '宿主未提供发送接口,已记录待执行') + target.sessionId = seat.sessionId + target.dispatchedAt = Date.now() + target.updatedAt = Date.now() + s.projects[projectId].updatedAt = Date.now() + s.projects[projectId].revision = (s.projects[projectId].revision ?? 0) + 1 + return { ...target } + }) + return { task: next, delivered } + } + + async taskCancel(projectId, taskId) { + await this.store.ensureLoaded() + if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + const next = await this.store.mutate(s => { + const target = s.projects[projectId].tasks.find(item => item.id === taskId) + if (!target) throw new ServiceError('sansheng/not-found', `任务 ${taskId} 不存在`) + if (target.status === 'done') throw new ServiceError('sansheng/invalid', '已完成的任务不能取消') + target.status = 'cancelled' + target.updatedAt = Date.now() + s.projects[projectId].revision = (s.projects[projectId].revision ?? 0) + 1 + return { ...target } + }) + return { task: next } + } + + async artifacts(projectId) { + await this.store.ensureLoaded() + const project = this.store.get(projectId) + if (!project) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + return { artifacts: [...(project.artifacts ?? [])] } + } + + async artifactAdd(projectId, input = {}) { + await this.store.ensureLoaded() + if (!this.store.get(projectId)) throw new ServiceError('sansheng/not-found', `项目 ${projectId} 不存在`) + const title = String(input.title ?? '').trim() + if (!title) throw new ServiceError('sansheng/invalid', '成果名称不能为空') + const artifact = await this.store.mutate(s => { + const project = s.projects[projectId] + if (input.taskId && !(project.tasks ?? []).some(item => item.id === input.taskId)) { + throw new ServiceError('sansheng/invalid', `关联任务 ${input.taskId} 不存在`) + } + project.artifactSeq = (project.artifactSeq ?? 0) + 1 + const next = emptyArtifact({ + id: `artifact-${project.artifactSeq}`, + title, + path: typeof input.path === 'string' ? input.path : '', + summary: typeof input.summary === 'string' ? input.summary : '', + taskId: input.taskId ?? null, + }) + if (!Array.isArray(project.artifacts)) project.artifacts = [] + project.artifacts.push(next) + project.updatedAt = Date.now() + project.revision = (project.revision ?? 0) + 1 + return { ...next } + }) + return { artifact } + } } diff --git a/src/host/store.js b/src/host/store.js index 860a2db..d877934 100644 --- a/src/host/store.js +++ b/src/host/store.js @@ -18,6 +18,17 @@ export class StoreError extends Error { function isObject(v) { return v !== null && typeof v === 'object' && !Array.isArray(v) } +// 旧版本状态没有任务/成果字段:读入时就地补空数组,不需要改文件版本。 +function normalizeProjects(state) { + for (const project of Object.values(state.projects ?? {})) { + if (!Array.isArray(project.tasks)) project.tasks = [] + if (!Array.isArray(project.artifacts)) project.artifacts = [] + if (!Number.isFinite(project.taskSeq)) project.taskSeq = 0 + if (!Number.isFinite(project.artifactSeq)) project.artifactSeq = 0 + } + return state +} + export class SanshengStore { constructor(file) { this.file = file @@ -64,7 +75,7 @@ export class SanshengStore { if (!valid.ok) { throw new StoreError('sansheng/state-corrupt', `状态文件损坏:${valid.error}。文件未被改动。`, { retryable: false }) } - this.state = valid.state + this.state = normalizeProjects(valid.state) this._loaded = true return } diff --git a/src/shared/migrate.js b/src/shared/migrate.js index 1dbba24..0ff1257 100644 --- a/src/shared/migrate.js +++ b/src/shared/migrate.js @@ -124,6 +124,10 @@ function migrateProject(legacy, key, usedIds, fromVersion, now, warnings) { path: isStr(legacy.workDir) ? legacy.workDir : (isStr(legacy.path) ? legacy.path : null), status: 'pending', seats, + tasks: [], + artifacts: [], + taskSeq: 0, + artifactSeq: 0, revision: 0, createdAt: Number.isFinite(legacy.createdAt) ? legacy.createdAt : now, updatedAt: Number.isFinite(legacy.updatedAt) ? legacy.updatedAt : now, diff --git a/src/shared/schema.js b/src/shared/schema.js index 8fca479..d88c3da 100644 --- a/src/shared/schema.js +++ b/src/shared/schema.js @@ -6,9 +6,12 @@ export const STATE_VERSION = 1 export const SEAT_STATUS = Object.freeze(['pending', 'creating', 'bound', 'failed', 'missing']) export const PROJECT_STATUS = Object.freeze(['pending', 'provisioning', 'ready', 'error']) +// 任务:草稿 → 已派工/待执行 → 完成 / 取消 / 失败 +export const TASK_STATUS = Object.freeze(['draft', 'dispatched', 'prepared', 'done', 'cancelled', 'failed']) const SEAT_STATUS_SET = new Set(SEAT_STATUS) const PROJECT_STATUS_SET = new Set(PROJECT_STATUS) +const TASK_STATUS_SET = new Set(TASK_STATUS) function isObject(v) { return v !== null && typeof v === 'object' && !Array.isArray(v) } function isStr(v) { return typeof v === 'string' && v.trim() !== '' } @@ -40,12 +43,25 @@ export function emptyProject({ id, name, workspaceId = null, path = null, now = path, status: 'pending', seats, + // 任务与交付成果:按项目隔离保存;旧数据没有这两个字段时按空数组处理。 + tasks: [], + artifacts: [], + taskSeq: 0, + artifactSeq: 0, revision: 0, createdAt: now, updatedAt: now, } } +export function emptyTask({ id, title = '', goal = '', ownerSeatId = 'lead', now = Date.now() }) { + return { id, title, goal, ownerSeatId, status: 'draft', note: '', sessionId: null, createdAt: now, updatedAt: now, dispatchedAt: null } +} + +export function emptyArtifact({ id, title = '', path = '', summary = '', taskId = null, now = Date.now() }) { + return { id, title, path, summary, taskId, createdAt: now } +} + export function validModel(model) { if (model === null || model === undefined) return true if (!isObject(model)) return false @@ -85,6 +101,17 @@ function projectProblem(project, id) { const problem = seatProblem(project.seats[seatId], seatId, id) if (problem) return problem } + if (project.tasks !== undefined && !Array.isArray(project.tasks)) return `项目 ${id} 的 tasks 不是数组` + for (const [index, task] of (project.tasks ?? []).entries()) { + if (!isObject(task)) return `项目 ${id} 的 tasks[${index}] 不是对象` + if (!isStr(task.id)) return `项目 ${id} 的 tasks[${index}] 缺少 id` + if (!TASK_STATUS_SET.has(task.status)) return `项目 ${id} 的任务 ${task.id} 状态非法:${String(task.status)}` + } + if (project.artifacts !== undefined && !Array.isArray(project.artifacts)) return `项目 ${id} 的 artifacts 不是数组` + for (const [index, artifact] of (project.artifacts ?? []).entries()) { + if (!isObject(artifact)) return `项目 ${id} 的 artifacts[${index}] 不是对象` + if (!isStr(artifact.id)) return `项目 ${id} 的 artifacts[${index}] 缺少 id` + } return undefined } diff --git a/test/tasks.test.js b/test/tasks.test.js new file mode 100644 index 0000000..03660eb --- /dev/null +++ b/test/tasks.test.js @@ -0,0 +1,59 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { setupService } from './helpers.js' + +async function readyProject() { + const ctx = await setupService() + const opened = await ctx.service.open({ path: '/tasks', name: '任务项目' }) + await ctx.service.enable(opened.project.id) + return { ...ctx, projectId: opened.project.id } +} + +test('任务:新建草稿、派工记录、取消,按项目保存', async () => { + const { service, projectId } = await readyProject() + const created = await service.taskCreate(projectId, { title: '整理需求', goal: '写清楚验收标准', ownerSeatId: 'zhongshu' }) + assert.equal(created.task.status, 'draft') + assert.equal(created.task.ownerSeatId, 'zhongshu') + + const listed = await service.tasks(projectId) + assert.equal(listed.tasks.length, 1) + + const dispatched = await service.taskDispatch(projectId, created.task.id) + // 测试宿主没有发送接口,因此只记录为待执行,而不是假装已送达 + assert.equal(dispatched.delivered, false) + assert.equal(dispatched.task.status, 'prepared') + assert.ok(dispatched.task.note.length > 0) + assert.ok(dispatched.task.sessionId) + + const cancelled = await service.taskCancel(projectId, created.task.id) + assert.equal(cancelled.task.status, 'cancelled') + await assert.rejects(() => service.taskDispatch(projectId, created.task.id), err => err.code === 'sansheng/invalid') +}) + +test('任务:缺标题、未知主责席被拒绝;未接通会话不能派工', async () => { + const ctx = await setupService() + const opened = await ctx.service.open({ path: '/tasks2', name: '未启用项目' }) + await assert.rejects(() => ctx.service.taskCreate(opened.project.id, { title: '', ownerSeatId: 'lead' }), err => err.code === 'sansheng/invalid') + await assert.rejects(() => ctx.service.taskCreate(opened.project.id, { title: 'x', ownerSeatId: 'nope' }), err => err.code === 'sansheng/invalid') + const created = await ctx.service.taskCreate(opened.project.id, { title: '待派工', ownerSeatId: 'lead' }) + await assert.rejects(() => ctx.service.taskDispatch(opened.project.id, created.task.id), err => err.code === 'sansheng/seat-unbound') +}) + +test('成果:登记、列表、关联任务校验', async () => { + const { service, projectId } = await readyProject() + const task = await service.taskCreate(projectId, { title: '产出文档', ownerSeatId: 'lipu' }) + const added = await service.artifactAdd(projectId, { title: '需求说明.pdf', path: 'D:/out/需求说明.pdf', summary: '首版', taskId: task.task.id }) + assert.equal(added.artifact.title, '需求说明.pdf') + const list = await service.artifacts(projectId) + assert.equal(list.artifacts.length, 1) + await assert.rejects(() => service.artifactAdd(projectId, { title: '', path: '' }), err => err.code === 'sansheng/invalid') + await assert.rejects(() => service.artifactAdd(projectId, { title: 'x', taskId: 'missing' }), err => err.code === 'sansheng/invalid') +}) + +test('任务与成果按项目隔离', async () => { + const { service, projectId } = await readyProject() + const other = await service.open({ path: '/other', name: '另一个项目' }) + await service.taskCreate(projectId, { title: 'A 项目任务', ownerSeatId: 'lead' }) + const otherTasks = await service.tasks(other.project.id) + assert.equal(otherTasks.tasks.length, 0) +})