Jelajahi Sumber

feat(app): support deleting PaaS agents and opening local workspace folder

StarKirby 1 hari lalu
induk
melakukan
162e61c136

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

@@ -101,6 +101,7 @@ export type PaasRequest =
   | { action: "create-agent"; packID: string; version: string; name: string }
   | { action: "threads" | "run-options" | "workspace"; agentID: string }
   | { action: "agent"; agentID: string }
+  | { action: "delete-agent"; agentID: string }
   | { action: "update-agent"; agentID: string; update: PaasAgentUpdate }
   | { action: "thread-runs"; agentID: string; threadID: string }
   | { action: "history"; agentID: string; offset?: number; includeActive?: boolean }

+ 8 - 0
packages/app/src/pages/paas/controller.ts

@@ -210,6 +210,14 @@ export function createPaasWorkspace(
         })
       }),
     catalog: () => perform("刷新目录", () => catalog().catch(() => undefined)),
+    deleteAgent: async (agentID: string) => {
+      if (!api || !state.connected || !agentID) return
+      await api.request({ action: "delete-agent", agentID })
+      if (!lifecycle.active) return
+      setState("agents", (agents) => agents.filter((row) => paasText(row.id) !== agentID))
+      if (state.agentID === agentID) selectAgent("")
+      await catalog().catch(() => undefined)
+    },
     checkConnection: () => perform("检查连接", () => catalog().catch(() => undefined)),
     history: () =>
       perform("读取任务", async () => {

+ 61 - 16
packages/app/src/pages/paas/index.tsx

@@ -545,8 +545,10 @@ export default function PaasWorkspace(
 
   async function openAgentWorkspace(agentID: string) {
     if (!platform.paas || !state.connected || ui.openingWorkspace) return
-    const conn = localServer()
-    if (!conn && !props.standalone) return
+    if (!platform.openPath && !platform.revealPath) {
+      showToast({ title: "打开工作区失败", description: "当前环境不支持打开本机文件夹。" })
+      return
+    }
     setUI("openingWorkspace", true)
     await platform.paas
       .request({ action: "workspace", agentID })
@@ -554,21 +556,13 @@ export default function PaasWorkspace(
         if (!lifecycle.active || !state.connected) return
         const directory = paasText(paasRecord(result)?.directory)
         if (!directory) throw new Error("平台没有返回可用的工作区目录。")
-        if (props.standalone) {
-          props.onOpenDeveloper?.(directory)
+        if (platform.openPath) {
+          const outcome: unknown = await platform.openPath(directory)
+          if (typeof outcome === "string" && outcome) throw new Error(outcome)
           return
         }
-        if (!conn || !global || !settings || !tabs || !server || !navigate) return
-        const projects = global.ensureServerCtx(conn).projects
-        projects.open(directory)
-        projects.touch(directory)
-        const key = ServerConnection.key(conn)
-        if (settings.general.newLayoutDesigns()) {
-          await tabs.newDraft({ server: key, directory })
-          return
-        }
-        server.setActive(key)
-        navigate(`/${base64Encode(directory)}`)
+        const ok = await platform.revealPath?.(directory)
+        if (!ok) throw new Error("无法在本机文件管理器中打开该目录。")
       })
       .catch((error: unknown) => {
         if (!lifecycle.active) return
@@ -582,6 +576,54 @@ export default function PaasWorkspace(
       })
   }
 
+  function confirmDeleteAgent(agentID: string, agentName: string) {
+    if (!platform.paas || !state.connected) return
+    dialog.show(() => {
+      const [deleting, setDeleting] = createStore({ busy: false, error: "" })
+      async function runDelete() {
+        if (deleting.busy) return
+        setDeleting({ busy: true, error: "" })
+        await workspace
+          .deleteAgent(agentID)
+          .then(() => {
+            dialog.close()
+            showToast({ title: `已删除智能体「${agentName || agentID}」` })
+          })
+          .catch((error: unknown) => {
+            setDeleting("error", error instanceof Error ? error.message : "删除智能体失败。")
+          })
+          .finally(() => {
+            setDeleting("busy", false)
+          })
+      }
+      return (
+        <Dialog title="删除智能体">
+          <div class="paas-workspace" style={{ display: "grid", gap: "14px", padding: "16px 20px" }}>
+            <p style={{ margin: 0 }}>
+              确认删除智能体 <strong>{agentName || agentID}</strong>(<code>{agentID}</code>)?
+            </p>
+            <p class="paas-muted" style={{ margin: 0 }}>
+              删除后将从智能体目录移除,已生成的历史运行记录和本地文件不会被清空。
+            </p>
+            <Show when={deleting.error}>
+              <p role="alert" class="paas-error" style={{ margin: 0 }}>
+                {deleting.error}
+              </p>
+            </Show>
+            <div class="paas-actions" style={{ "justify-content": "flex-end" }}>
+              <button type="button" disabled={deleting.busy} onClick={() => dialog.close()}>
+                取消
+              </button>
+              <button type="button" class="paas-primary" disabled={deleting.busy} onClick={() => void runDelete()}>
+                {deleting.busy ? "正在删除…" : "确认删除"}
+              </button>
+            </div>
+          </div>
+        </Dialog>
+      )
+    })
+  }
+
   function openAgentPacks() {
     setUI("extensionsTab", "discover")
     goPage("applications")
@@ -650,6 +692,7 @@ export default function PaasWorkspace(
               <DropdownMenu.Content>
                 <DropdownMenu.Item onSelect={openTasks}>查看任务</DropdownMenu.Item>
                 <DropdownMenu.Item onSelect={openResources}>检查资源</DropdownMenu.Item>
+                <DropdownMenu.Item onSelect={() => confirmDeleteAgent(id(), name())}>删除智能体</DropdownMenu.Item>
               </DropdownMenu.Content>
             </DropdownMenu.Portal>
           </DropdownMenu>
@@ -1087,7 +1130,9 @@ export default function PaasWorkspace(
               workspaceHint={
                 remoteWorkspace() ? "远程目录不能直接在本机打开;可在任务中下载工作文件,再进入开发区。" : undefined
               }
-              workspaceDisabled={remoteWorkspace() || ui.openingWorkspace || (!props.standalone && !localServer())}
+              workspaceDisabled={
+                remoteWorkspace() || ui.openingWorkspace || (!platform.openPath && !platform.revealPath)
+              }
             />
           </div>
           <Switch>

+ 19 - 0
packages/desktop/src/main/paas.test.ts

@@ -764,3 +764,22 @@ test("plugin product routes carry explicit package permission and MCP credential
     expect(calls).toHaveLength(5)
   } finally { api.disconnect(41); server.stop(true) }
 })
+
+test("delete-agent calls DELETE /api/v1/agents/{id} and rejects invalid identifiers", async () => {
+  const calls: { method: string; path: string }[] = []
+  const fetcher = (async (url: string | URL | Request, init?: RequestInit) => {
+    const pathname = new URL(String(url)).pathname
+    if (pathname.endsWith("/auth/me")) return new Response("missing", { status: 404 })
+    calls.push({ method: init?.method || "GET", path: pathname })
+    return Response.json({ agents: [], message: "Agent deleted" })
+  }) as typeof fetch
+  const api = createPaasConnections(fetcher)
+  await api.connect(51, { url: "http://127.0.0.1:8000", apiKey: "test-only" })
+  try {
+    await api.request(51, { action: "delete-agent", agentID: "ag_old" })
+    expect(calls.at(-1)).toEqual({ method: "DELETE", path: "/api/v1/agents/ag_old" })
+    await expect(api.request(51, { action: "delete-agent", agentID: ".." })).rejects.toThrow("编号无效")
+  } finally {
+    api.disconnect(51)
+  }
+})

+ 2 - 0
packages/desktop/src/main/paas.ts

@@ -534,6 +534,8 @@ export function createPaasConnections(fetcher: typeof fetch = fetch) {
         })
       }
       if (input?.action === "knowledge") return request(connection, "/knowledge")
+      if (input?.action === "delete-agent")
+        return request(connection, `/agents/${identifier(input.agentID)}`, undefined, undefined, undefined, "DELETE")
       if (input?.action === "agent" || input?.action === "update-agent") {
         const path = `/agents/${identifier(input.agentID)}`
         const agent = await request(connection, path)