Просмотр исходного кода

feat(paas): bind research chats to projects

241880436@seecoder.cn 4 дней назад
Родитель
Сommit
ecc840a6da

+ 12 - 0
packages/app/src/pages/paas/chat-cache.test.ts

@@ -141,6 +141,18 @@ test("one corrupt message preserves other conversations, drafts and healthy mess
   expect(m.data.get("lambda-scientist.chats.v2:s:main:a:message:m")).toBe("{broken")
 })
 
+test("chat metadata keeps project ownership across reload", () => {
+  const m = memory()
+  const first = createChatCache(m.storage, "s", "main", () => {})
+  first.stage({ ...chat("a"), projectID: "project-optics" })
+  first.index(["a"], "a")
+  first.close()
+
+  const next = createChatCache(m.storage, "s", "main", () => {})
+  expect(next.load().chats[0]?.projectID).toBe("project-optics")
+  next.close()
+})
+
 test("a corrupt navigation index can be rebuilt from healthy conversation records", () => {
   const m = memory(),
     first = createChatCache(m.storage, "s", "main", () => {})

+ 1 - 0
packages/app/src/pages/paas/chat-cache.ts

@@ -145,6 +145,7 @@ export function createChatCache(
     const loaded = chat.cache?.loaded !== false
     const value: Metadata = {
       id: chat.id,
+      projectID: chat.projectID,
       agentID: chat.agentID,
       name: chat.name,
       title: chat.title,

+ 4 - 0
packages/app/src/pages/paas/chat-state.ts

@@ -33,6 +33,7 @@ export type ResearchMessage = {
 }
 export type ResearchChat = {
   id: string
+  projectID?: string
   agentID: string
   name: string
   title: string
@@ -62,6 +63,7 @@ export function createResearchChats(props: {
   scope?: string
   legacyScope?: string
   windowID?: string
+  projectID: () => string
   onNotice?: (notice: { chatID: string; title: string; body: string }) => void
 }) {
   const [state, setState] = createStore({
@@ -307,6 +309,7 @@ export function createResearchChats(props: {
               )
               .map((c) => ({
                 ...c,
+                projectID: typeof c.projectID === "string" && c.projectID ? c.projectID : "project-default",
                 name: typeof c.name === "string" ? c.name : "研究助手",
                 title: typeof c.title === "string" ? c.title : "历史会话",
                 threadID: typeof c.threadID === "string" ? c.threadID : "",
@@ -452,6 +455,7 @@ export function createResearchChats(props: {
     }
     const chat: ResearchChat = {
       id: crypto.randomUUID(),
+      projectID: props.projectID(),
       agentID,
       name: paasText(agent.name),
       title: "新会话",

+ 18 - 0
packages/app/src/pages/paas/chat.tsx

@@ -40,6 +40,7 @@ export function PaasChat(props: {
   visible: boolean
   scope?: string
   legacyScope?: string
+  projectID: string
   agents: Record<string, unknown>[]
   openAgentID: string
   onAgentOpened: () => void
@@ -99,6 +100,7 @@ export function PaasChat(props: {
     get legacyScope() {
       return props.legacyScope
     },
+    projectID: () => props.projectID,
     windowID: platform.windowID,
     onNotice: (notice) => {
       setUI("notices", (rows) => [
@@ -117,6 +119,20 @@ export function PaasChat(props: {
   const { state, setState, current } = model
   const currentAgent = createMemo(() => props.agents.find((agent) => agent.id === current()?.agentID))
   const currentTitle = createMemo(() => current()?.title || paasText(currentAgent()?.name) || "科研工作台")
+  createEffect(
+    on(
+      () => props.projectID,
+      (projectID) => {
+        if (!state.restored || current()?.projectID === projectID) return
+        const next = state.chats.find(
+          (chat) => chat.projectID === projectID && !chat.archived && hasConversationHistory(chat),
+        )
+        if (next) model.activate(next.id)
+        else if (props.agents[0]) model.choose(props.agents[0], true)
+      },
+      { defer: true },
+    ),
+  )
   createEffect(() => props.onTitle?.(currentTitle()))
   let previousChat: string | undefined
   createEffect(
@@ -393,6 +409,7 @@ export function PaasChat(props: {
       const chats = state.chats.filter(
         (chat) =>
           chat.agentID === agent.id &&
+          chat.projectID === props.projectID &&
           !!chat.archived === ui.archived &&
           hasConversationHistory(chat) &&
           matches(chat.title),
@@ -420,6 +437,7 @@ export function PaasChat(props: {
   const drafts = createMemo(() =>
     state.chats.filter(
       (chat) =>
+        chat.projectID === props.projectID &&
         !!chat.archived === ui.archived &&
         !hasConversationHistory(chat) &&
         hasResearchContent(chat) &&

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

@@ -856,6 +856,7 @@ export default function PaasWorkspace(
               connected={state.connected}
               scope={state.scope}
               legacyScope={state.legacyScope}
+              projectID="project-default"
               onOpenTasks={() => {
                 workspace.setState("page", "tasks")
                 void tasks.refresh()