Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor(skills): deduplicate Skills prompt and align device-routed systemRole (#23484)

AmAzing-35b070ac3d56
diff --git a/packages/builtin-tool-skills/src/index.test.ts b/packages/builtin-tool-skills/src/index.test.ts
new file mode 100644
index 00000000000..cf72630c828
--- /dev/null
+++ b/packages/builtin-tool-skills/src/index.test.ts
@@ -0,0 +1,31 @@
+import { fileURLToPath } from 'node:url';
+
+import { build } from 'vite';
+import { expect, it } from 'vitest';
+
+import { vitePlatformResolve } from '../../../plugins/vite/platformResolve';
+
+it.each(['web', 'desktop'] as const)(
+ 'bundles the public Skills prompts for %s',
+ async (platform) => {
+ const result = await build({
+ build: {
+ lib: { entry: fileURLToPath(new URL('./index.ts', import.meta.url)), formats: ['es'] },
+ write: false,
+ },
+ configFile: false,
+ logLevel: 'silent',
+ plugins: [vitePlatformResolve(platform)],
+ });
+
+ const bundle = Array.isArray(result) ? result[0] : result;
+ expect(bundle).toMatchObject({
+ output: expect.arrayContaining([
+ expect.objectContaining({
+ exports: expect.arrayContaining(['deviceSystemPrompt', 'systemPrompt']),
+ isEntry: true,
+ }),
+ ]),
+ });
+ },
+);
diff --git a/packages/builtin-tool-skills/src/index.ts b/packages/builtin-tool-skills/src/index.ts
index b24b063e27b..f51aec1eb50 100644
--- a/packages/builtin-tool-skills/src/index.ts
+++ b/packages/builtin-tool-skills/src/index.ts
@@ -4,7 +4,7 @@ export {
agentShareSystemPrompt,
resolveSkillsRestrictedManifest,
} from './resolveRestrictedManifest';
-export { systemPrompt } from './systemRole';
+export { deviceSystemPrompt, systemPrompt } from './systemRole';
export {
type ActivateSkillParams,
type ActivateSkillSource,
diff --git a/packages/builtin-tool-skills/src/manifest.desktop.ts b/packages/builtin-tool-skills/src/manifest.desktop.ts
index 8463cc47011..c7efac8aae3 100644
--- a/packages/builtin-tool-skills/src/manifest.desktop.ts
+++ b/packages/builtin-tool-skills/src/manifest.desktop.ts
@@ -6,7 +6,7 @@ import {
manifestMeta,
readReferenceApi,
} from './manifest.base';
-import { systemPrompt } from './systemRole';
+import { systemPrompt } from './systemRole.desktop';
import { SkillsApiName, SkillsIdentifier } from './types';
export const SkillsManifest: BuiltinToolManifest = {
diff --git a/packages/builtin-tool-skills/src/resolveManifest.test.ts b/packages/builtin-tool-skills/src/resolveManifest.test.ts
index b94f78f64bf..9cd8404de6c 100644
--- a/packages/builtin-tool-skills/src/resolveManifest.test.ts
+++ b/packages/builtin-tool-skills/src/resolveManifest.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
import { SkillsManifest } from './manifest';
import { resolveSkillsManifest } from './resolveManifest';
-import { systemPrompt } from './systemRole';
+import { deviceSystemPrompt, systemPrompt } from './systemRole';
import { SkillsApiName } from './types';
const EXEC_APIS = [SkillsApiName.runCommand, SkillsApiName.execScript, SkillsApiName.exportFile];
@@ -48,8 +48,12 @@ describe('resolveSkillsManifest', () => {
expect(apiNames).toContain(SkillsApiName.activateSkill);
expect(apiNames).toContain(SkillsApiName.readReference);
- // cross-tool arbitration rides the tool systemRole, not the descriptions
- expect(result.systemRole).toContain(systemPrompt);
+ // cross-tool arbitration rides the tool systemRole, not the descriptions,
+ // and the device role omits cloud-sandbox runCommand / exportFile / sandbox_environment
+ expect(result.systemRole).toContain(deviceSystemPrompt);
+ expect(result.systemRole).not.toContain('exportFile');
+ expect(result.systemRole).not.toContain('<sandbox_environment>');
+ expect(result.systemRole).not.toContain('{{sandbox_workspace}}');
expect(result.systemRole).toContain('`execScript` runs skill scripts on the device');
expect(result.systemRole).toContain('`lobe-local-system` runCommand');
},
diff --git a/packages/builtin-tool-skills/src/resolveManifest.ts b/packages/builtin-tool-skills/src/resolveManifest.ts
index 54f88ea69be..21ed65762ac 100644
--- a/packages/builtin-tool-skills/src/resolveManifest.ts
+++ b/packages/builtin-tool-skills/src/resolveManifest.ts
@@ -1,6 +1,7 @@
import type { BuiltinManifestResolver, BuiltinToolResolveContext } from '@lobechat/types';
import { SkillsManifest } from './manifest';
+import { deviceSystemPrompt } from './systemRole';
import { SkillsApiName } from './types';
/**
@@ -148,6 +149,7 @@ export const resolveSkillsManifest: BuiltinManifestResolver = (context) => {
: {};
const preamble = unrouted.preamble ?? basePreamble;
const fact = unrouted.fact ?? (executionEnv && EXEC_ENV_FACTS[executionEnv]);
+ const baseSystemRole = isDeviceRun ? deviceSystemPrompt : SkillsManifest.systemRole;
return {
...SkillsManifest,
@@ -158,8 +160,8 @@ export const resolveSkillsManifest: BuiltinManifestResolver = (context) => {
? { ...api, description: `${preamble} ${api.description}` }
: api,
),
- ...(fact && {
- systemRole: `${SkillsManifest.systemRole}\n<execution_environment>\n${fact}\n</execution_environment>\n`,
- }),
+ systemRole: fact
+ ? `${baseSystemRole}\n<execution_environment>\n${fact}\n</execution_environment>\n`
+ : baseSystemRole,
};
};
diff --git a/packages/builtin-tool-skills/src/systemRole.desktop.ts b/packages/builtin-tool-skills/src/systemRole.desktop.ts
index b35a798fd58..8f2771b4dd3 100644
--- a/packages/builtin-tool-skills/src/systemRole.desktop.ts
+++ b/packages/builtin-tool-skills/src/systemRole.desktop.ts
@@ -1,42 +1 @@
-export const systemPrompt = `You have access to a Skills tool that can activate skills and execute their instructions. Skills are reusable instruction packages that extend your capabilities.
-
-<core_capabilities>
-1. Activate a skill by name to load its instructions (activateSkill)
-2. Read reference files attached to a skill (readReference)
-3. Execute shell commands specified in a skill's instructions (execScript)
-</core_capabilities>
-
-<workflow>
-1. When the user's task matches an available skill, call activateSkill to load its instructions
-2. Follow the skill's instructions to complete the task
-3. If the skill content references additional files, use readReference to load them
-4. If the skill content instructs you to run CLI commands, use execScript to execute them
-</workflow>
-
-<tool_selection_guidelines>
-- **activateSkill**: Call this when the user's task matches one of the available skills
- - Provide the exact skill name
- - Returns the skill content (instructions, templates, guidelines) that you should follow
- - If the skill is not found, you'll receive a list of available skills
- - **IMPORTANT**: If a skill's content is already provided in \`<selected_skill_context>\` within the user message, do NOT call activateSkill for that skill β€” its instructions are already loaded and ready to use
-
-- **readReference**: Call this to read reference files mentioned in a skill's content
- - Requires the id (returned by activateSkill) and the file path
- - Returns the file content for you to use as context
- - Only use paths that are referenced in the skill content
-
-- **execScript**: Call this to execute shell commands mentioned in a skill's content
- - The system automatically uses activated skills context from previous activateSkill calls
- - Commands run directly on the local system (OS: {{platform}})
- - Provide the command to execute and a clear description of what it does
- - Returns the command output (stdout/stderr)
- - Only execute commands that are specified or suggested in the skill content
- - Requires user confirmation before execution
-</tool_selection_guidelines>
-
-<best_practices>
-- Follow the skill's instructions carefully once loaded
-- Use readReference only for files explicitly mentioned in the skill content
-- Use execScript only for commands specified in the skill content
-</best_practices>
-`;
+export { deviceSystemPrompt, deviceSystemPrompt as systemPrompt } from './systemRole';
diff --git a/packages/builtin-tool-skills/src/systemRole.ts b/packages/builtin-tool-skills/src/systemRole.ts
index 4200d2bae94..26d494bb16a 100644
--- a/packages/builtin-tool-skills/src/systemRole.ts
+++ b/packages/builtin-tool-skills/src/systemRole.ts
@@ -1,91 +1,34 @@
-export const systemPrompt = `You have access to a Skills tool that can activate skills and execute their instructions. Skills are reusable instruction packages that extend your capabilities.
+const intro = `You have access to a Skills tool that loads and executes skills β€” reusable instruction packages that extend your capabilities.`;
-<core_capabilities>
-1. Activate a skill by name to load its instructions (activateSkill)
-2. Read reference files attached to a skill (readReference)
-3. Execute shell commands in the cloud sandbox (runCommand)
-4. Execute skill-specific scripts with resource context (execScript)
-5. Export files generated during skill execution to cloud storage (exportFile)
-</core_capabilities>
+const sharedSkillGuidelines = `- **activateSkill**: Call when the user's task matches an available skill to load and follow its instructions. Do NOT call \`activateSkill\` if the skill's content is already provided in \`<selected_skill_context>\` within the user message.
+- **readReference**: Load reference files explicitly mentioned in an activated skill's instructions.`;
-<workflow>
-1. When the user's task matches an available skill, call activateSkill to load its instructions
-2. Follow the skill's instructions to complete the task
-3. If the skill content references additional files, use readReference to load them
-4. If the skill content instructs you to run CLI commands, use runCommand to execute them
-5. If the command requires skill-bundled resources, use execScript instead
-6. If the skill execution generates output files, use exportFile to save them for the user
-</workflow>
+export const systemPrompt = `${intro}
-<tool_selection_guidelines>
-- **activateSkill**: Call this when the user's task matches one of the available skills
- - Provide the exact skill name
- - Returns the skill content (instructions, templates, guidelines) that you should follow
- - If the skill is not found, you'll receive a list of available skills
- - **IMPORTANT**: If a skill's content is already provided in \`<selected_skill_context>\` within the user message, do NOT call activateSkill for that skill β€” its instructions are already loaded and ready to use
-
-- **readReference**: Call this to read reference files mentioned in a skill's content
- - Requires the id (returned by activateSkill) and the file path
- - Returns the file content for you to use as context
- - Only use paths that are referenced in the skill content
-
-- **runCommand**: Call this to execute shell commands in the cloud sandbox
- - Use for general CLI commands, platform tools (e.g., \`lh\` CLI), and ad-hoc operations
- - If \`lobe-local-system\` runCommand is also available, default shell execution to it β€” use this sandbox runCommand only when the task needs LobeHub-managed credentials, isolation, or a tool missing on the local device
- - Provide the command to execute and a clear description of what it does
- - Returns the command output (stdout/stderr) and exit code
- - Requires user confirmation before execution
-
-- **execScript**: Call this to execute skill-specific scripts that need resource context
- - The system automatically uses activated skills context from previous activateSkill calls
- - Automatically locates and provides skill resources (ZIP package with scripts, config files, dependencies)
- - Best for: commands that require skill-bundled files or dependencies
- - Returns the command output (stdout/stderr)
- - Requires user confirmation before execution
-
-- **exportFile**: Call this to export files generated during skill execution
- - Use this when a skill generates output files that the user needs to download
- - Provide the file path in the execution environment and the desired filename
- - Returns a permanent download URL for the exported file
- - Best for: skill outputs, generated reports, processed data files, result artifacts
-</tool_selection_guidelines>
-
-<runcommand_vs_execscript>
-**When to use runCommand vs execScript:**
-
-- **runCommand (default of the two)**:
- - Use for general shell commands and CLI tools (e.g., \`lh kb list\`, \`npm install\`)
- - Use for platform tool commands (LobeHub CLI, etc.)
- - No skill context needed β€” just provide the command
- - Best for: CLI operations, system commands, tool invocations
-
-- **execScript (For skill-bundled scripts)**:
- - Use only when a command needs access to skill-bundled resources (ZIP packages, config files)
- - The system automatically tracks activated skills and provides their resources
- - Best for: running scripts bundled within a skill package
-
-**Example workflow:**
-1. User activates a skill with activateSkill
-2. Skill content instructs to run a CLI command (e.g., \`lh kb list\`) β†’ use runCommand
-3. Skill content instructs to run a bundled script (e.g., \`python scripts/init.py\`) β†’ use execScript
-</runcommand_vs_execscript>
+<guidelines>
+${sharedSkillGuidelines}
+- **runCommand vs execScript**:
+ - **runCommand (default)**: Execute general CLI and shell commands (e.g., \`lh\` CLI, \`npm\`, system commands) in the cloud sandbox. If \`lobe-local-system\` \`runCommand\` is also available, default shell execution to \`lobe-local-system\` and use this sandbox \`runCommand\` only when the task needs LobeHub-managed credentials, isolation, or a tool missing on the local device.
+ - **execScript**: Use only when a command requires skill-bundled resources (ZIP packages, scripts, or config files) from the activated skill.
+- **exportFile**: Export files generated in the cloud sandbox to return a permanent download URL for the user.
+</guidelines>
<sandbox_environment>
-\`runCommand\` and \`execScript\` run in the same cloud sandbox session as every other
-sandbox tool in this conversation, in the same working directory. What that
-directory is, and whether what you leave there survives the session, is below β€”
-and it decides where a checkout or a generated file belongs.
+\`runCommand\` and \`execScript\` run in the conversation's isolated cloud sandbox session and working directory:
{{sandbox_workspace}}
</sandbox_environment>
+`;
-
-<best_practices>
-- Only activate skills when the user's task clearly matches the skill's purpose
-- Follow the skill's instructions carefully once loaded
-- Use readReference only for files explicitly mentioned in the skill content
-- Use runCommand for CLI commands and general operations
-- Use execScript when the command needs skill-bundled resources
-- Use exportFile when the skill generates output files that need to be saved
-- If activateSkill returns an error with available skills, inform the user what skills are available
-</best_practices>
+/**
+ * System prompt for device-routed runs (`executionEnv: 'device' | 'local'`),
+ * where `runCommand` and `exportFile` are stripped from the manifest and
+ * `execScript` executes directly on the user's device rather than in the
+ * cloud sandbox.
+ */
+export const deviceSystemPrompt = `${intro}
+
+<guidelines>
+${sharedSkillGuidelines}
+- **execScript**: Execute skill-bundled scripts from an activated skill on the user's device.
+</guidelines>
`;
diff --git a/packages/mecha/src/contextEngineering/facts/gatherContextFacts.test.ts b/packages/mecha/src/contextEngineering/facts/gatherContextFacts.test.ts
index 7b223a8eb58..a302fa2248c 100644
--- a/packages/mecha/src/contextEngineering/facts/gatherContextFacts.test.ts
+++ b/packages/mecha/src/contextEngineering/facts/gatherContextFacts.test.ts
@@ -587,6 +587,16 @@ describe('which tool counts as reaching the sandbox', () => {
expect(resolveSandboxPersistence).not.toHaveBeenCalled();
});
+
+ it('skips sandbox persistence lookup when lobe-skills is routed to a local device without cloud-sandbox', async () => {
+ const resolveSandboxPersistence = vi.fn(async () => persistence);
+ await gatherContextFacts(
+ request({ activeDeviceId: 'dev_1', enabledToolIds: ['lobe-skills'] }),
+ { resolveSandboxPersistence },
+ );
+
+ expect(resolveSandboxPersistence).not.toHaveBeenCalled();
+ });
});
describe('gatherContextFacts β€” revoked document reads', () => {
diff --git a/packages/mecha/src/contextEngineering/facts/gatherContextFacts.ts b/packages/mecha/src/contextEngineering/facts/gatherContextFacts.ts
index c5daf59ce65..c5627b14fb1 100644
--- a/packages/mecha/src/contextEngineering/facts/gatherContextFacts.ts
+++ b/packages/mecha/src/contextEngineering/facts/gatherContextFacts.ts
@@ -558,12 +558,14 @@ export const gatherContextFacts = async (
const docsAgentId = documentsAgentId(request);
const sandboxEnabled = request.enabledToolIds.includes(CloudSandboxIdentifier);
// Two tools reach the sandbox, and the optional one is not the common case:
- // `lobe-skills` is always on, and its runCommand / execScript open the SAME
- // session in the SAME directory as the cloud-sandbox tool. Asking only
- // whether the optional tool is enabled told a skills-driven run the
- // ephemeral wording while its commands ran in a persistent workspace β€” so it
- // cloned into /tmp and the user's file browser stayed empty.
- const sandboxShellEnabled = sandboxEnabled || request.enabledToolIds.includes(SkillsIdentifier);
+ // `lobe-skills` is always on, and when no device is routed its runCommand /
+ // execScript open the SAME session in the SAME directory as the cloud-sandbox
+ // tool. When a device IS routed and the cloud-sandbox tool is not enabled,
+ // `lobe-skills` executes on the device and uses `deviceSystemPrompt` (which
+ // has no `{{sandbox_workspace}}` placeholder), so no sandbox lookup is needed.
+ const sandboxShellEnabled =
+ sandboxEnabled ||
+ (!request.activeDeviceId && request.enabledToolIds.includes(SkillsIdentifier));
const tasks = [
() =>