Przeglądaj źródła

feat(paas): aggregate inbox across projects

241880436@seecoder.cn 4 dni temu
rodzic
commit
7c5382b22d

+ 25 - 0
packages/app/e2e/paas/global-inbox.spec.ts

@@ -0,0 +1,25 @@
+import { expect, test } from "@playwright/test"
+
+test("global inbox shows another project and opens it", async ({ page }) => {
+  await page.goto("/e2e/paas/index.html?completed")
+  await page.getByLabel("API Key", { exact: true }).fill("global-inbox")
+  await page.getByRole("button", { name: "连接平台", exact: true }).click()
+  await page
+    .getByRole("button", { name: /已有研究/ })
+    .first()
+    .click()
+
+  await page.getByRole("button", { name: "新建项目", exact: true }).click()
+  await page.getByLabel("项目名称", { exact: true }).fill("第二个项目")
+  await page.getByRole("button", { name: "创建项目", exact: true }).click()
+  await expect(page.getByLabel("切换项目", { exact: true })).toContainText("第二个项目")
+
+  const inbox = page.getByRole("button", { name: /待处理 1 项/ })
+  await expect(inbox).toBeVisible()
+  await inbox.click()
+  const panel = page.getByRole("dialog", { name: "待处理" })
+  await expect(panel.getByText("我的研究", { exact: true })).toBeVisible()
+  await panel.getByRole("button", { name: /已有研究/ }).click()
+  await expect(page.getByLabel("切换项目", { exact: true })).toContainText("我的研究")
+  await expect(page.getByText("研究计划", { exact: true })).toBeVisible()
+})

+ 10 - 1
packages/app/src/pages/paas/README.md

@@ -34,7 +34,16 @@
 - 非活动会话的完成未读按「本机已保存的会话 → 平台任务索引」恢复:重新连接后即可计入 Inbox,无需再次打开该会话。
 - 非活动会话的完成未读按「本机已保存的会话 → 平台任务索引」恢复:重新连接后即可计入 Inbox,无需再次打开该会话。
 - v1 恢复限制:终态任务若早于当前任务历史窗口,或会话没有保存 `thread_id`,可能要打开会话后才会出现在 Inbox。
 - v1 恢复限制:终态任务若早于当前任务历史窗口,或会话没有保存 `thread_id`,可能要打开会话后才会出现在 Inbox。
 - 本机归档的会话不进入 Inbox,归档的失败与完成不会长期占用顶栏入口。
 - 本机归档的会话不进入 Inbox,归档的失败与完成不会长期占用顶栏入口。
-- Inbox v1 只覆盖当前项目;跨项目全局 Inbox 留到后续计划。
+- Inbox v1 只覆盖当前项目;P0-C 将其扩展为全局项目聚合。
+
+## P0-C Global Inbox(2026-11-30)
+
+- 顶栏待处理入口聚合所有项目的待确认、失败和完成未读。
+- 条目显示项目名称;点击会先激活目标会话,再切换项目并进入会话。
+- 已读状态继续按项目隔离;点击其他项目条目只写入该项目的已读 key。
+- 归档项目和归档会话都不进入 Inbox。
+- 未归属任何项目线程的平台任务不进入 v1;需要在 P1 任务中心一并处理。
+- 复用线程仍沿用首次项目的归属,跨项目线程所有权留到后续。
 
 
 ---
 ---
 
 

+ 10 - 6
packages/app/src/pages/paas/chat.tsx

@@ -35,7 +35,7 @@ import {
 import { createMaterials } from "./materials"
 import { createMaterials } from "./materials"
 import { PaasGeneratedFiles } from "./generated-files"
 import { PaasGeneratedFiles } from "./generated-files"
 import type { PaasPreviewFile } from "./preview"
 import type { PaasPreviewFile } from "./preview"
-import { buildProjectInbox, type InboxItem } from "./inbox"
+import { buildInbox, type InboxItem, type InboxProject } from "./inbox"
 import { inboxSeenKey, markInboxSeen, readInboxSeen } from "./inbox-seen"
 import { inboxSeenKey, markInboxSeen, readInboxSeen } from "./inbox-seen"
 import { InboxPanel } from "./inbox-panel"
 import { InboxPanel } from "./inbox-panel"
 
 
@@ -46,6 +46,8 @@ export function PaasChat(props: {
   scope?: string
   scope?: string
   legacyScope?: string
   legacyScope?: string
   projectID: string
   projectID: string
+  projects: InboxProject[]
+  onProjectChange: (id: string) => void
   workspace?: string
   workspace?: string
   agents: Record<string, unknown>[]
   agents: Record<string, unknown>[]
   inboxTasks: () => Record<string, unknown>[]
   inboxTasks: () => Record<string, unknown>[]
@@ -128,26 +130,28 @@ export function PaasChat(props: {
   })
   })
   const { state, setState, current } = model
   const { state, setState, current } = model
   const [seenVersion, setSeenVersion] = createSignal(0)
   const [seenVersion, setSeenVersion] = createSignal(0)
-  const seenKey = createMemo(() => inboxSeenKey(props.scope || "local", platform.windowID || "main", props.projectID))
+  const seenKey = (projectID: string) =>
+    inboxSeenKey(props.scope || "local", platform.windowID || "main", projectID)
   const seen = createMemo(() => {
   const seen = createMemo(() => {
     seenVersion()
     seenVersion()
-    return readInboxSeen(localStorage, seenKey())
+    return new Map(props.projects.map((project) => [project.id, readInboxSeen(localStorage, seenKey(project.id))]))
   })
   })
   const inbox = createMemo(() =>
   const inbox = createMemo(() =>
-    buildProjectInbox({
-      projectID: props.projectID,
+    buildInbox({
       chats: state.chats,
       chats: state.chats,
       tasks: props.inboxTasks(),
       tasks: props.inboxTasks(),
+      projects: props.projects,
       seen: seen(),
       seen: seen(),
     }),
     }),
   )
   )
   const openInboxItem = (item: InboxItem) => {
   const openInboxItem = (item: InboxItem) => {
     model.activate(item.chatID)
     model.activate(item.chatID)
+    props.onProjectChange(item.projectID)
     props.onShowChat?.()
     props.onShowChat?.()
     // Approvals and failures stay actionable, so only completed results are marked seen.
     // Approvals and failures stay actionable, so only completed results are marked seen.
     if (item.kind !== "unread") return
     if (item.kind !== "unread") return
     try {
     try {
-      markInboxSeen(localStorage, seenKey(), item.id)
+      markInboxSeen(localStorage, seenKey(item.projectID), item.id)
       setSeenVersion((value) => value + 1)
       setSeenVersion((value) => value + 1)
     } catch {
     } catch {
       // Storage can be unavailable in restricted webviews; keep the item until the session ends.
       // Storage can be unavailable in restricted webviews; keep the item until the session ends.

+ 1 - 0
packages/app/src/pages/paas/inbox-panel.tsx

@@ -68,6 +68,7 @@ export function InboxPanel(props: { items: InboxItem[]; onOpen: (item: InboxItem
                   }}
                   }}
                 >
                 >
                   <span>{labels[item().kind]}</span>
                   <span>{labels[item().kind]}</span>
+                  <span class="paas-inbox-project">{item().projectName}</span>
                   <strong>{item().title}</strong>
                   <strong>{item().title}</strong>
                   <small>{item().detail}</small>
                   <small>{item().detail}</small>
                   <Show when={stamp(at())}>
                   <Show when={stamp(at())}>

+ 40 - 24
packages/app/src/pages/paas/inbox.test.ts

@@ -1,5 +1,5 @@
 import { expect, test } from "bun:test"
 import { expect, test } from "bun:test"
-import { buildProjectInbox } from "./inbox"
+import { buildInbox } from "./inbox"
 
 
 type TestMessage = {
 type TestMessage = {
   id: string
   id: string
@@ -12,6 +12,11 @@ type TestMessage = {
   started?: number
   started?: number
 }
 }
 
 
+const projects = [
+  { id: "p1", name: "光学" },
+  { id: "p2", name: "数据分析" },
+]
+
 const chat = (id: string, projectID: string, messages: TestMessage[]) => ({
 const chat = (id: string, projectID: string, messages: TestMessage[]) => ({
   id,
   id,
   projectID,
   projectID,
@@ -24,10 +29,10 @@ const chat = (id: string, projectID: string, messages: TestMessage[]) => ({
   messages,
   messages,
 })
 })
 
 
-test("builds approvals, failures and unread completions for the current project", () => {
-  const items = buildProjectInbox({
-    projectID: "p1",
-    seen: new Set<string>(),
+test("builds approvals, failures and unread completions across projects", () => {
+  const items = buildInbox({
+    projects,
+    seen: new Map(),
     chats: [
     chats: [
       chat("a", "p1", [
       chat("a", "p1", [
         { id: "m1", role: "assistant", runID: "run-a", status: "waiting_human", text: "请确认" },
         { id: "m1", role: "assistant", runID: "run-a", status: "waiting_human", text: "请确认" },
@@ -37,17 +42,23 @@ test("builds approvals, failures and unread completions for the current project"
       chat("b", "p2", [
       chat("b", "p2", [
         { id: "m4", role: "assistant", runID: "run-d", status: "waiting_human", text: "其他项目" },
         { id: "m4", role: "assistant", runID: "run-d", status: "waiting_human", text: "其他项目" },
       ]),
       ]),
+      chat("c", "p3", [{ id: "m5", role: "assistant", runID: "run-e", status: "failed", text: "" }]),
     ],
     ],
     tasks: [],
     tasks: [],
   })
   })
-  expect(items.map((item) => item.kind)).toEqual(["approval", "failed", "unread"])
-  expect(items.every((item) => item.projectID === "p1")).toBe(true)
+  expect(items.map((item) => [item.projectID, item.projectName, item.kind])).toEqual([
+    ["p1", "光学", "approval"],
+    ["p2", "数据分析", "approval"],
+    ["p1", "光学", "failed"],
+    ["p1", "光学", "unread"],
+  ])
+  expect(items.some((item) => item.projectID === "p3")).toBe(false)
 })
 })
 
 
-test("task rows only join through current project threads and dedupe runs", () => {
-  const items = buildProjectInbox({
-    projectID: "p1",
-    seen: new Set<string>(),
+test("task rows join through project threads and dedupe runs", () => {
+  const items = buildInbox({
+    projects,
+    seen: new Map(),
     chats: [chat("a", "p1", [{ id: "m1", role: "assistant", runID: "run-a", status: "waiting_human", text: "" }])],
     chats: [chat("a", "p1", [{ id: "m1", role: "assistant", runID: "run-a", status: "waiting_human", text: "" }])],
     tasks: [
     tasks: [
       { run_id: "run-a", thread_id: "thread-a", status: "waiting_human", input: "重复" },
       { run_id: "run-a", thread_id: "thread-a", status: "waiting_human", input: "重复" },
@@ -57,28 +68,33 @@ test("task rows only join through current project threads and dedupe runs", () =
   })
   })
   expect(items.filter((item) => item.runID === "run-a")).toHaveLength(1)
   expect(items.filter((item) => item.runID === "run-a")).toHaveLength(1)
   expect(items.some((item) => item.runID === "run-b")).toBe(false)
   expect(items.some((item) => item.runID === "run-b")).toBe(false)
-  expect(items.some((item) => item.runID === "run-c" && item.kind === "unread")).toBe(true)
+  expect(items.find((item) => item.runID === "run-c")).toMatchObject({
+    kind: "unread",
+    projectID: "p1",
+    projectName: "光学",
+  })
 })
 })
 
 
-test("seen only hides completed unread items", () => {
-  const items = buildProjectInbox({
-    projectID: "p1",
-    seen: new Set(["unread:run-b"]),
+test("seen state is evaluated per project", () => {
+  const items = buildInbox({
+    projects,
+    seen: new Map([
+      ["p1", new Set(["unread:run-a"])],
+      ["p2", new Set()],
+    ]),
     chats: [
     chats: [
-      chat("a", "p1", [
-        { id: "m1", role: "assistant", runID: "run-a", status: "waiting_human", text: "" },
-        { id: "m2", role: "assistant", runID: "run-b", status: "completed", text: "完成" },
-      ]),
+      chat("a", "p1", [{ id: "m1", role: "assistant", runID: "run-a", status: "completed", text: "A" }]),
+      chat("b", "p2", [{ id: "m2", role: "assistant", runID: "run-b", status: "completed", text: "B" }]),
     ],
     ],
     tasks: [],
     tasks: [],
   })
   })
-  expect(items.map((item) => item.kind)).toEqual(["approval"])
+  expect(items.map((item) => item.projectID)).toEqual(["p2"])
 })
 })
 
 
 test("archived chats contribute no inbox items, including task-index rows", () => {
 test("archived chats contribute no inbox items, including task-index rows", () => {
-  const items = buildProjectInbox({
-    projectID: "p1",
-    seen: new Set<string>(),
+  const items = buildInbox({
+    projects,
+    seen: new Map(),
     chats: [
     chats: [
       {
       {
         ...chat("a", "p1", [
         ...chat("a", "p1", [

+ 33 - 16
packages/app/src/pages/paas/inbox.ts

@@ -1,6 +1,7 @@
 import { paasStatus, paasText } from "@/paas"
 import { paasStatus, paasText } from "@/paas"
 
 
 export type InboxKind = "approval" | "failed" | "unread"
 export type InboxKind = "approval" | "failed" | "unread"
+export type InboxProject = { id: string; name: string }
 
 
 export type InboxItem = {
 export type InboxItem = {
   id: string
   id: string
@@ -8,6 +9,7 @@ export type InboxItem = {
   title: string
   title: string
   detail: string
   detail: string
   projectID: string
   projectID: string
+  projectName: string
   chatID: string
   chatID: string
   runID?: string
   runID?: string
   at: number
   at: number
@@ -39,22 +41,28 @@ const time = (value: unknown) => {
   return Number.isFinite(result) ? result : 0
   return Number.isFinite(result) ? result : 0
 }
 }
 
 
-export function buildProjectInbox(input: {
-  projectID: string
+export function buildInbox(input: {
   chats: Chat[]
   chats: Chat[]
   tasks: Record<string, unknown>[]
   tasks: Record<string, unknown>[]
-  seen: Set<string>
+  projects: InboxProject[]
+  seen: Map<string, Set<string>>
 }): InboxItem[] {
 }): InboxItem[] {
-  // Archived chats are intentionally inert: otherwise an archived failure would pin the badge forever.
-  const chats = input.chats.filter((chat) => chat.projectID === input.projectID && !chat.archived)
-  const threads = new Set(chats.map((chat) => chat.threadID).filter(Boolean))
+  const projectNames = new Map(input.projects.map((project) => [project.id, project.name]))
+  const chats = input.chats.filter((chat) => !!chat.projectID && projectNames.has(chat.projectID) && !chat.archived)
+  // Later tasks only join through a known project thread; unassigned platform runs stay out of v1.
+  const threads = new Map<string, Chat>()
+  for (const chat of chats) if (chat.threadID && !threads.has(chat.threadID)) threads.set(chat.threadID, chat)
   const items = new Map<string, InboxItem>()
   const items = new Map<string, InboxItem>()
   const add = (item: InboxItem) => {
   const add = (item: InboxItem) => {
     const key = item.runID || item.id
     const key = item.runID || item.id
     if (!items.has(key)) items.set(key, item)
     if (!items.has(key)) items.set(key, item)
   }
   }
+  const seenFor = (projectID: string) => input.seen.get(projectID) || new Set<string>()
 
 
   for (const chat of chats) {
   for (const chat of chats) {
+    const projectID = chat.projectID!
+    const projectName = projectNames.get(projectID)!
+    const seen = seenFor(projectID)
     for (const message of chat.messages) {
     for (const message of chat.messages) {
       if (message.role !== "assistant" || !message.runID) continue
       if (message.role !== "assistant" || !message.runID) continue
       const at = message.updated || message.started || 0
       const at = message.updated || message.started || 0
@@ -64,7 +72,8 @@ export function buildProjectInbox(input: {
           kind: "approval",
           kind: "approval",
           title: chat.title,
           title: chat.title,
           detail: "需要确认后才能继续",
           detail: "需要确认后才能继续",
-          projectID: input.projectID,
+          projectID,
+          projectName,
           chatID: chat.id,
           chatID: chat.id,
           runID: message.runID,
           runID: message.runID,
           at,
           at,
@@ -75,7 +84,8 @@ export function buildProjectInbox(input: {
           kind: "failed",
           kind: "failed",
           title: chat.title,
           title: chat.title,
           detail: message.error || "任务执行失败",
           detail: message.error || "任务执行失败",
-          projectID: input.projectID,
+          projectID,
+          projectName,
           chatID: chat.id,
           chatID: chat.id,
           runID: message.runID,
           runID: message.runID,
           at,
           at,
@@ -83,14 +93,15 @@ export function buildProjectInbox(input: {
       else if (
       else if (
         ["completed", "partial"].includes(message.status || "") &&
         ["completed", "partial"].includes(message.status || "") &&
         message.text &&
         message.text &&
-        !input.seen.has(`unread:${message.runID}`)
+        !seen.has(`unread:${message.runID}`)
       )
       )
         add({
         add({
           id: `unread:${message.runID}`,
           id: `unread:${message.runID}`,
           kind: "unread",
           kind: "unread",
           title: chat.title,
           title: chat.title,
           detail: chat.name,
           detail: chat.name,
-          projectID: input.projectID,
+          projectID,
+          projectName,
           chatID: chat.id,
           chatID: chat.id,
           runID: message.runID,
           runID: message.runID,
           at,
           at,
@@ -101,8 +112,11 @@ export function buildProjectInbox(input: {
   for (const task of input.tasks) {
   for (const task of input.tasks) {
     const runID = paasText(task.run_id) || paasText(task.id)
     const runID = paasText(task.run_id) || paasText(task.id)
     const threadID = paasText(task.thread_id) || paasText(task.session_id)
     const threadID = paasText(task.thread_id) || paasText(task.session_id)
-    if (!runID || !threadID || !threads.has(threadID) || items.has(runID)) continue
-    const chat = chats.find((item) => item.threadID === threadID)!
+    const chat = threadID ? threads.get(threadID) : undefined
+    if (!runID || !chat || items.has(runID) || !chat.projectID) continue
+    const projectID = chat.projectID
+    const projectName = projectNames.get(projectID)!
+    const seen = seenFor(projectID)
     const status = paasStatus(task)
     const status = paasStatus(task)
     const at = time(task.updated_at) || time(task.created_at)
     const at = time(task.updated_at) || time(task.created_at)
     if (status.startsWith("waiting"))
     if (status.startsWith("waiting"))
@@ -111,7 +125,8 @@ export function buildProjectInbox(input: {
         kind: "approval",
         kind: "approval",
         title: chat.title,
         title: chat.title,
         detail: "任务需要确认",
         detail: "任务需要确认",
-        projectID: input.projectID,
+        projectID,
+        projectName,
         chatID: chat.id,
         chatID: chat.id,
         runID,
         runID,
         at,
         at,
@@ -122,18 +137,20 @@ export function buildProjectInbox(input: {
         kind: "failed",
         kind: "failed",
         title: chat.title,
         title: chat.title,
         detail: paasText(task.error_message) || "任务执行失败",
         detail: paasText(task.error_message) || "任务执行失败",
-        projectID: input.projectID,
+        projectID,
+        projectName,
         chatID: chat.id,
         chatID: chat.id,
         runID,
         runID,
         at,
         at,
       })
       })
-    else if (["completed", "partial"].includes(status) && !input.seen.has(`unread:${runID}`))
+    else if (["completed", "partial"].includes(status) && !seen.has(`unread:${runID}`))
       add({
       add({
         id: `unread:${runID}`,
         id: `unread:${runID}`,
         kind: "unread",
         kind: "unread",
         title: chat.title,
         title: chat.title,
         detail: chat.name,
         detail: chat.name,
-        projectID: input.projectID,
+        projectID,
+        projectName,
         chatID: chat.id,
         chatID: chat.id,
         runID,
         runID,
         at,
         at,

+ 5 - 0
packages/app/src/pages/paas/index.tsx

@@ -996,6 +996,11 @@ export default function PaasWorkspace(
               scope={state.scope}
               scope={state.scope}
               legacyScope={state.legacyScope}
               legacyScope={state.legacyScope}
               projectID={projects.activeID}
               projectID={projects.activeID}
+              projects={projects.projects.filter((project) => !project.archivedAt)}
+              onProjectChange={(id) => {
+                setProjects("activeID", id)
+                saveProjects()
+              }}
               workspace={activeProject()?.workspace}
               workspace={activeProject()?.workspace}
               onOpenTasks={() => {
               onOpenTasks={() => {
                 workspace.setState("page", "tasks")
                 workspace.setState("page", "tasks")

+ 5 - 0
packages/app/src/pages/paas/studio.css

@@ -949,3 +949,8 @@
     width: auto;
     width: auto;
   }
   }
 }
 }
+.paas-inbox-item > .paas-inbox-project {
+  color: var(--text-weak);
+  font-size: var(--paas-type-meta);
+  font-weight: 500;
+}