Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
feat(skills): 返回技能文件位置
zszz3f820a79cf6dc
diff --git a/apps/desktop/electron/main/builtin-skills.ts b/apps/desktop/electron/main/builtin-skills.tsindex 3dcb12005..f1687e745 100644--- a/apps/desktop/electron/main/builtin-skills.ts+++ b/apps/desktop/electron/main/builtin-skills.ts@@ -2,6 +2,7 @@ import { existsSync, readFileSync } from "node:fs";import { join } from "node:path";import { parseSkillFrontmatter } from "@pi-desktop/plugin-sdk";import type { PluginSkillDef } from "@pi-desktop/agent-runtime";+import type { LoadedSkillDocument } from "./skill-document";/*** Skills PI-Desktop ships itself.@@ -67,11 +68,11 @@ export function isPluginWorkspace(}/** Front matter carries the skill's title and applicability line. */-function readBuiltinSkill(fileName: string): string | null {+function readBuiltinSkill(fileName: string): { path: string; raw: string } | null {const path = resolveBuiltinSkillPath(fileName);if (!path) return null;try {- return readFileSync(path, "utf8");+ return { path, raw: readFileSync(path, "utf8") };} catch {return null;}@@ -93,8 +94,8 @@ export function builtinSkills(input: BuiltinSkillInput): PluginSkillDef[] {return ids.flatMap((id) => {const file = id === IMAGE_GENERATION_SKILL_ID ? IMAGE_GENERATION_SKILL_FILE : PLUGIN_DEV_SKILL_FILE;const raw = readBuiltinSkill(file);- if (!raw?.trim()) return [];- const parsed = parseSkillFrontmatter(raw);+ if (!raw?.raw.trim()) return [];+ const parsed = parseSkillFrontmatter(raw.raw);return parsed.body ? [{ id, name: parsed.name ?? id, description: parsed.description }] : [];});}@@ -103,17 +104,16 @@ export function builtinSkills(input: BuiltinSkillInput): PluginSkillDef[] {* Load a built-in skill body for the `Skill` tool. Returns null for any id the* host does not ship, which is the caller's cue to try the plugin registry.*/-export function loadBuiltinSkillBody(- id: string,-): { id: string; name: string; body: string } | null {+export function loadBuiltinSkillBody(id: string): LoadedSkillDocument | null {if (id !== PLUGIN_DEV_SKILL_ID && id !== IMAGE_GENERATION_SKILL_ID) return null;const raw = readBuiltinSkill(id === IMAGE_GENERATION_SKILL_ID ? IMAGE_GENERATION_SKILL_FILE : PLUGIN_DEV_SKILL_FILE);- if (!raw?.trim()) return null;- const parsed = parseSkillFrontmatter(raw);+ if (!raw?.raw.trim()) return null;+ const parsed = parseSkillFrontmatter(raw.raw);if (!parsed.body) return null;return {id,name: parsed.name ?? id,body: parsed.body,+ location: raw.path,};}diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.tsindex 89ac117eb..0f91a258a 100644--- a/apps/desktop/electron/main/plugin-runtime.ts+++ b/apps/desktop/electron/main/plugin-runtime.ts@@ -12,6 +12,7 @@ import type { Stats } from "node:fs";import { open as openFile } from "node:fs/promises";import { randomUUID } from "node:crypto";import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";+import type { LoadedSkillDocument } from "./skill-document";import {busTopicAllowed,isDeniedFsPath,@@ -1505,7 +1506,7 @@ export class PluginRuntime {* only, and the size cap is re-checked because the file may have changed* since load.*/- loadSkillBody(id: string): { id: string; name: string; body: string } {+ loadSkillBody(id: string): LoadedSkillDocument {const skill = this.skills.get(id);if (!skill) throw apiError("NOT_FOUND", `unknown skill: ${id}`);if (!this.loaded.has(skill.pluginId)) {@@ -1531,7 +1532,7 @@ export class PluginRuntime {skillId: skill.id,ts: Date.now(),});- return { id: skill.id, name: skill.name, body: parsed.body };+ return { id: skill.id, name: skill.name, body: parsed.body, location: skill.path };}getLoaded(pluginId: string): LoadedPlugin | undefined {diff --git a/apps/desktop/electron/main/runtime/session-launch.ts b/apps/desktop/electron/main/runtime/session-launch.tsindex afa2f6158..ab0e83d9b 100644--- a/apps/desktop/electron/main/runtime/session-launch.ts+++ b/apps/desktop/electron/main/runtime/session-launch.ts@@ -41,6 +41,7 @@ import type { PluginRuntime } from "../plugin-runtime";import type { UserMcpRuntime } from "../user-mcp";import type { RuntimeState } from "./context";import type { RuntimeProvider } from "./provider-catalog";+import type { LoadedSkillDocument } from "../skill-document";const ErrorCodes = {...SharedErrorCodes,@@ -220,7 +221,7 @@ export function createSessionLaunchRuntime({async function loadUserSkillBody(id: string,projectPath: string | null,- ): Promise<{ id: string; name: string; body: string } | null> {+ ): Promise<LoadedSkillDocument | null> {if (!runtimeState.host || id.includes("/")) return null;const result = await runtimeState.host!.call<{skill: UserSkillRecord | null;@@ -231,7 +232,7 @@ export function createSessionLaunchRuntime({if (!isActiveInProject(skill, projectPath)) {throw new Error(`skill "${id}" is not enabled for this project`);}- return { id: skill.id, name: skill.name, body: result.body };+ return { id: skill.id, name: skill.name, body: result.body, location: skill.path };}async function resolveEffectiveCommandShell(): Promise<CommandShellCatalog> {diff --git a/apps/desktop/electron/main/runtime/sidecar.ts b/apps/desktop/electron/main/runtime/sidecar.tsindex d12348d66..3a4b53837 100644--- a/apps/desktop/electron/main/runtime/sidecar.ts+++ b/apps/desktop/electron/main/runtime/sidecar.ts@@ -22,6 +22,7 @@ import type { PluginRuntime } from "../plugin-runtime";import type { UserMcpRuntime } from "../user-mcp";import type { RuntimeState } from "./context";import type { FinishTurn } from "./plans";+import { formatSkillToolContent, type LoadedSkillDocument } from "../skill-document";export type SidecarRuntimeDependencies = {runtimeState: RuntimeState;@@ -52,7 +53,10 @@ export type SidecarRuntimeDependencies = {browserHost: BrowserHost;plugins: PluginRuntime;sessionProjects: Map<string, string | null>;- loadUserSkillBody: (id: string, projectPath: string | null) => Promise<any>;+ loadUserSkillBody: (+ id: string,+ projectPath: string | null,+ ) => Promise<LoadedSkillDocument | null>;activeUserSkills: (projectPath: string | undefined) => Promise<any[]>;pluginActiveInProject: (pluginId: string, projectPath: string | null | undefined) => boolean;currentNetworkProxy: () => any;@@ -544,13 +548,13 @@ export function createSidecarRuntime({// Bundled skills answer first; they are not owned by any plugin. A user// skill is looked up next, and only then a plugin's — the ids cannot// collide, since a plugin skill id always carries a `<pluginId>/` prefix.- const skill =+ const skill: LoadedSkillDocument =loadBuiltinSkillBody(id) ??(await loadUserSkillBody(id, projectPath)) ??plugins.loadSkillBody(id);return {ok: true,- content: `# Skill: ${skill.name} (${skill.id})\n\n${skill.body}`,+ content: formatSkillToolContent(skill),};} catch (error) {const userIds = (await activeUserSkills(projectPath ?? undefined)).map(diff --git a/apps/desktop/electron/main/skill-document.ts b/apps/desktop/electron/main/skill-document.tsnew file mode 100644index 000000000..190369d7c--- /dev/null+++ b/apps/desktop/electron/main/skill-document.ts@@ -0,0 +1,20 @@+import { dirname } from "node:path";++export type LoadedSkillDocument = {+ id: string;+ name: string;+ body: string;+ /** Absolute path of the loaded SKILL.md document. */+ location: string;+};++/** Add path metadata only when a skill is loaded, not to the catalog prompt. */+export function formatSkillToolContent(skill: LoadedSkillDocument): string {+ return [+ `# Skill: ${skill.name} (${skill.id})`,+ `Location: ${skill.location}`,+ `References are relative to ${dirname(skill.location)}.`,+ "",+ skill.body,+ ].join("\n");+}diff --git a/apps/desktop/test/imported-package-skills-runtime.test.mjs b/apps/desktop/test/imported-package-skills-runtime.test.mjsindex a8512ab20..f7b7f5444 100644--- a/apps/desktop/test/imported-package-skills-runtime.test.mjs+++ b/apps/desktop/test/imported-package-skills-runtime.test.mjs@@ -97,7 +97,10 @@ function assertSkillCatalog(runtime, imported, bodies, paths) {assert.equal(realpathSync(skill.path), realpathSync(join(imported.path, "src", paths.get(skill.name))));assert.equal(skill.description, `Test ${skill.name} skill.`);assert.deepEqual(runtime.loadSkillBody(skill.id), {- id: skill.id, name: skill.name, body: bodies.get(skill.name),+ id: skill.id,+ name: skill.name,+ body: bodies.get(skill.name),+ location: skill.path,});}return catalog;diff --git a/apps/desktop/test/plugin-skills.test.mjs b/apps/desktop/test/plugin-skills.test.mjsindex bbd50a64d..fa3f1689f 100644--- a/apps/desktop/test/plugin-skills.test.mjs+++ b/apps/desktop/test/plugin-skills.test.mjs@@ -23,6 +23,10 @@ const agentRuntimeSrc = readFileSync("utf8",);const sidecarSrc = readFileSync(join(repoRoot, "packages/agent-runtime/src/sidecar.ts"), "utf8");+const desktopSidecarSrc = readFileSync(+ join(desktopRoot, "electron/main/runtime/sidecar.ts"),+ "utf8",+);const composerAutocompleteSrc = readFileSync(join(desktopRoot, "src/components/ComposerAutocomplete.tsx"),"utf8",@@ -119,6 +123,9 @@ test("the built-in skill body loads through the same Skill tool", () => {/loadBuiltinSkillBody\(id\) \?\?\s*\(await loadUserSkillBody\(id, projectPath\)\) \?\?\s*plugins\.loadSkillBody\(id\)/,);assert.match(mainSrc, /const userIds = \(await activeUserSkills/);+ assert.match(desktopSidecarSrc, /formatSkillToolContent\(skill\)/);+ assert.match(builtinSrc, /location: raw\.path/);+ assert.match(runtimeSrc, /location: skill\.path/);});test("the built-in skill ships as a packaged resource with a dev fallback", () => {diff --git a/apps/desktop/test/skill-tool-result.test.mjs b/apps/desktop/test/skill-tool-result.test.mjsnew file mode 100644index 000000000..050e06712--- /dev/null+++ b/apps/desktop/test/skill-tool-result.test.mjs@@ -0,0 +1,17 @@+import assert from "node:assert/strict";+import test from "node:test";++import { formatSkillToolContent } from "../electron/main/skill-document.ts";++test("Skill tool content identifies the document and its reference directory", () => {+ const content = formatSkillToolContent({+ id: "redmine",+ name: "Redmine",+ location: "/Users/example/.agents/skills/redmine/SKILL.md",+ body: "Read `SECRET.md` from this skill directory.",+ });++ assert.match(content, /Location: \/Users\/example\/\.agents\/skills\/redmine\/SKILL\.md/);+ assert.match(content, /References are relative to \/Users\/example\/\.agents\/skills\/redmine\./);+ assert.ok(content.endsWith("Read `SECRET.md` from this skill directory."));+});diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex e94cb4407..a535e1d55 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -2823,7 +2823,7 @@ identify the platform validation still needed.- **Preconditions**: `examples/plugins/hello` enabled with `agent.prompt.inject` granted; a second copy of the manifest without that permission available; one workspace that is a plugin directory and one that is not.- **Steps**: 1) Start a session and ask the agent what skills it has. 2) Ask it to follow the Hello demo skill so it calls the `Skill` tool. 3) Edit the skill document and repeat step 2. 4) Disable the plugin and start a new turn. 5) Load the variant without `agent.prompt.inject` and repeat step 1. 6) Declare a document larger than the per-skill cap. 7) Open each of the two workspaces in turn.-- **Expected**: The catalog lists the skill id, name, and trimmed description but no body, after the built-in skills and before the project instruction chain; the `Skill` schema is loaded through `ToolSearch` only when requested and reads the edited file without a restart; disabling the plugin rebuilds the runtime so the skill disappears from the next turn; the variant without the permission loads normally and contributes no skills; the oversized document is skipped with an audit line rather than clamped into the prompt; the built-in `plugin-development` skill is catalogued in the plugin workspace and absent in the other, while `PluginCheck` is listed in the bounded on-demand tool catalog in both.+- **Expected**: The catalog lists the skill id, name, and trimmed description but no body or path, after the built-in skills and before the project instruction chain; the `Skill` schema is loaded through `ToolSearch` only when requested and reads the edited file without a restart; the loaded result identifies the actual `SKILL.md` path and its parent directory before the body so relative references resolve against that document; disabling the plugin rebuilds the runtime so the skill disappears from the next turn; the variant without the permission loads normally and contributes no skills; the oversized document is skipped with an audit line rather than clamped into the prompt; the built-in `plugin-development` skill is catalogued in the plugin workspace and absent in the other, while `PluginCheck` is listed in the bounded on-demand tool catalog in both.- **Specs linked**: `07-plugins/02-plugin-manifest-schema.md`, `07-plugins/04-plugin-security.md` §7.1, `07-plugins/10-plugin-devex.md`, ADR 0039, ADR 0037, D174- **Acceptance**: G (skill activation) + E (tools & permissions) + D (high-risk permission gating)- **Status**: Unit-covered (`plugin-skills.test.mjs`, agent-runtime prompt/digest tests); agent-facing scenario Draftdiff --git a/docs/spec/07-plugins/01-plugin-system.md b/docs/spec/07-plugins/01-plugin-system.mdindex 652d80606..a42bc7626 100644--- a/docs/spec/07-plugins/01-plugin-system.md+++ b/docs/spec/07-plugins/01-plugin-system.md@@ -306,7 +306,9 @@ Namespace: `pi.plugin.*`Skills are contributed declaratively (`contributes.skills` + `agent.prompt.inject`),not invoked by the plugin: the host puts the catalog in the system prompt and themodel loads a body through the built-in `Skill` tool (D174). Planned, not-currently exposed: `pi.agent.appendSystemHint(text)`.+currently exposed: `pi.agent.appendSystemHint(text)`. When the body is loaded,+the tool result also identifies the `SKILL.md` location and the directory to use+when resolving relative references; the catalog remains metadata-only.### Background services (requires `background.service`)- `pi.services.register({ id, start, stop? })`diff --git a/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md b/docs/spec/07-plugins/12-plugin-ipc-and-host-services.mdindex cc30776cb..7b5867c52 100644--- a/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md+++ b/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md@@ -262,4 +262,8 @@ Skills use a separate, simpler path. The catalog (id, name, description) is partof the base system prompt, the `Skill` schema is itself deferred behind`ToolSearch`, and its body is fetched by a local `Skill` tool that Electron mainserves directly — the sidecar never holds skill text, and a skill document-reaches the model only when it asks for it (D174/D185).+reaches the model only when it asks for it (D174/D185). The loaded result+includes the absolute `SKILL.md` location and a sentence naming its parent+directory, so relative references such as `references/foo.md` and `SECRET.md`+resolve against the document that was actually loaded. The catalog remains+unchanged and carries no path metadata.