Jelajahi Sumber

feat(paas): pass project workspace to workspace assistant

241880436@seecoder.cn 4 hari lalu
induk
melakukan
06073b979b

+ 6 - 1
packages/app/e2e/paas/fixture.tsx

@@ -13,7 +13,12 @@ const filesMode = query.has("files")
 const completedMode = query.has("completed")
 const agent = { id: "research", name: "科研助手", description: "研究规划与验证", current_version: 1 }
 const unusedAgent = { id: "unused", name: "未使用智能体", description: "从未开始过会话", current_version: 1 }
-const agents = query.has("groups") ? [agent, unusedAgent] : [agent]
+const workspaceAgent = { id: "workspace", name: "工作区助手", description: "文件与命令", current_version: 1 }
+const agents = query.has("groups")
+  ? [agent, unusedAgent]
+  : query.has("workspace")
+    ? [agent, workspaceAgent]
+    : [agent]
 const run: Record<string, unknown> = {
   run_id: "run_1",
   agent_id: agent.id,

+ 32 - 0
packages/app/e2e/paas/project-workflow.spec.ts

@@ -1,5 +1,37 @@
 import { expect, test } from "@playwright/test"
 
+test("workspace assistant requires and forwards the project folder", async ({ page }) => {
+  await page.goto("/e2e/paas/index.html?workspace")
+  await page.getByLabel("API Key", { exact: true }).fill("workspace-context")
+  await page.getByRole("button", { name: "连接平台", exact: true }).click()
+  await page.getByLabel("选择智能体", { exact: true }).selectOption({ label: "工作区助手" })
+  await page.getByRole("textbox", { name: "消息", exact: true }).fill("整理这个文件夹")
+  await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
+  await expect(page.getByText("需要绑定项目工作文件夹", { exact: true })).toBeVisible()
+  expect(
+    await page.evaluate(
+      () => (window as unknown as { paasTestCalls: { action: string }[] }).paasTestCalls.filter((call) => call.action === "execute").length,
+    ),
+  ).toBe(0)
+
+  await page.getByRole("button", { name: "新建项目", exact: true }).click()
+  await page.getByLabel("项目名称", { exact: true }).fill("工作区研究")
+  await page.getByRole("button", { name: "创建项目", exact: true }).click()
+  await page.getByRole("button", { name: "绑定工作文件夹", exact: true }).click()
+  await page.getByLabel("选择智能体", { exact: true }).selectOption({ label: "工作区助手" })
+  await page.getByRole("textbox", { name: "消息", exact: true }).fill("整理这个文件夹并改 README")
+  await page.getByRole("textbox", { name: "消息", exact: true }).press("Enter")
+  await expect
+    .poll(() =>
+      page.evaluate(
+        () => (window as unknown as { paasTestCalls: { action: string; workDir?: string }[] }).paasTestCalls.find(
+          (call) => call.action === "execute",
+        )?.workDir,
+      ),
+    )
+    .toBe("D:/Research/optics")
+})
+
 test("projects isolate chat history and keep a bound workspace after reload", async ({ page }) => {
   await page.goto("/e2e/paas/index.html")
   await page.getByLabel("API Key", { exact: true }).fill("project-test")

+ 1 - 0
packages/app/src/paas.ts

@@ -58,6 +58,7 @@ export type PaasRequest =
       requestID?: string
       effort?: PaasEffort
       files?: PaasFile[]
+      workDir?: string
     }
   | { action: "lookup-request"; agentID: string; requestID: string }
   | { action: "export-report"; name: string; text: string; format?: "md" | "docx" | "pdf" }

+ 2 - 1
packages/app/src/pages/paas/chat-state.ts

@@ -587,7 +587,7 @@ export function createResearchChats(props: {
       if (selected(ticket)) setState("error", error instanceof Error ? error.message : "无法打开任务")
     }
   }
-  async function send() {
+  async function send(workDir?: string) {
     const chat = current()
     if (!chat || (!chat.draft.trim() && !chat.reference?.text) || job(chat) || !props.connected || !props.api) return
     const input = chat.reference?.text
@@ -642,6 +642,7 @@ export function createResearchChats(props: {
           executionID,
           requestID: executionID,
           effort: chat.effort,
+          workDir,
         }),
       )
       if (!life.active || epoch !== life.epoch) return

+ 7 - 1
packages/app/src/pages/paas/chat.tsx

@@ -8,6 +8,7 @@ import { researchElapsed } from "./progress"
 import { externalToolPhase, researchToolLabel } from "./tool-label"
 import { useDialog } from "@opencode-ai/ui/context/dialog"
 import { ResearchIssue } from "./issue"
+import { resolveAgentProfile } from "./agent-profiles"
 import { ReportReader } from "./report-reader"
 import { Icon } from "@opencode-ai/ui/icon"
 import { usePlatform } from "@/context/platform"
@@ -351,9 +352,14 @@ export function PaasChat(props: {
     if (id) setState("chats", (c) => c.id === id, "draft", value)
   }
   const send = () => {
+    const profile = currentAgent() ? resolveAgentProfile(currentAgent()!) : undefined
+    if (profile?.requiresWorkspace && !props.workspace) {
+      setState("error", "工作区助手需要先为当前项目绑定工作文件夹。")
+      return
+    }
     if (current() && !materials.busy(current()!.id)) {
       bottom()
-      void model.send()
+      void model.send(profile?.requiresWorkspace ? props.workspace : undefined)
     }
   }
   const elapsed = (message: ResearchMessage) => researchElapsed(message, ui.now)

+ 6 - 0
packages/app/src/pages/paas/issue.tsx

@@ -1,6 +1,12 @@
 import { Show } from "solid-js"
 
 export function researchIssue(message: string) {
+  if (/WORKSPACE_DIR_REQUIRED|工作区助手.*工作文件夹/.test(message))
+    return {
+      title: "需要绑定项目工作文件夹",
+      help: "在当前项目下选择工作文件夹后再发送;工作区助手不会自动读取其他目录。",
+      action: "",
+    }
   if (/执行服务尚未就绪|LOCAL_SERVICE_UNAVAILABLE|DEPENDENCY_NOT_CONFIGURED/.test(message))
     return {
       title: "平台执行服务尚未就绪",

+ 6 - 0
packages/app/src/pages/paas/progress.test.ts

@@ -34,6 +34,12 @@ test("account balance, research budget, network and permissions suggest differen
   expect(researchIssue("连接中断").action).toBe("tasks")
 })
 
+test("workspace assistant explains how to unblock a missing project folder", () => {
+  const issue = researchIssue("工作区助手需要先为当前项目绑定工作文件夹。")
+  expect(issue.title).toBe("需要绑定项目工作文件夹")
+  expect(issue.help).toContain("工作文件夹")
+})
+
 test("missing artifacts are not reported as an account permission problem", () => {
   const missing = researchIssue("AgentPaaS 返回 HTTP 404,请检查权限与平台版本。")
   expect(missing.title).toBe("文件或资源不存在")

+ 18 - 1
packages/desktop/src/main/paas.test.ts

@@ -1,7 +1,24 @@
 import { expect, test } from "bun:test"
 import { tmpdir } from "node:os"
 import type { PaasProgress } from "@opencode-ai/app/paas"
-import { createPaasConnections, readPaasStream } from "./paas"
+import { createPaasConnections, executePayload, readPaasStream } from "./paas"
+
+test("workspace execution forwards work_dir in context", () => {
+  expect(
+    executePayload({
+      action: "execute",
+      agentID: "workspace-assistant",
+      input: "整理这个目录",
+      workDir: "D:/Research/optics",
+    }),
+  ).toMatchObject({
+    context: { work_dir: "D:/Research/optics" },
+    mode: "iterate",
+  })
+  expect(executePayload({ action: "execute", agentID: "research67", input: "制定研究计划" })).toMatchObject({
+    context: {},
+  })
+})
 
 test.each([
   "LOCAL_SERVICE_UNAVAILABLE",

+ 17 - 11
packages/desktop/src/main/paas.ts

@@ -22,6 +22,22 @@ function isLoopback(value: string) {
   return ["localhost", "127.0.0.1", "[::1]"].includes(new URL(value).hostname)
 }
 
+type ExecuteInput = Extract<PaasRequest, { action: "execute" }>
+
+export function executePayload(input: ExecuteInput) {
+  return {
+    input: input.input.trim(),
+    input_files: input.files || [],
+    thread_id: input.threadID || "",
+    parameters: {},
+    context: input.workDir ? { work_dir: input.workDir } : {},
+    mode: "iterate",
+    target_subagent: "",
+    effort: input.effort,
+    cancel_on_disconnect: false,
+  }
+}
+
 async function localCredentials() {
   const config: unknown = await readFile(join(homedir(), ".agentpaas", "config.json"), "utf8")
     .then((value) => JSON.parse(value) as unknown)
@@ -255,17 +271,7 @@ export function createPaasConnections(fetcher: typeof fetch = fetch) {
         return request(
           connection,
           path,
-          {
-            input: input.input.trim(),
-            input_files: input.files || [],
-            thread_id: input.threadID || "",
-            parameters: {},
-            context: {},
-            mode: "iterate",
-            target_subagent: "",
-            effort: input.effort,
-            cancel_on_disconnect: false,
-          },
+          executePayload(input),
           {
             controller,
             progress: (event) => {