🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(skill-store): stop importFromMarket failing on an already installed skill (#20089)
Arvin Xuad453d1f07f5
diff --git a/apps/server/src/services/skill/importer.test.ts b/apps/server/src/services/skill/importer.test.tsindex d41fc2c5150..9ee6a73817c 100644--- a/apps/server/src/services/skill/importer.test.ts+++ b/apps/server/src/services/skill/importer.test.ts@@ -963,6 +963,160 @@ description: A nested skillexpect(second.skill.id).toBe(first.skill.id);});+ // A market skill installed from the Skill Store UI / `lh skill install` is+ // keyed by its market identifier. Older agent-tool imports of the same+ // download URL derived a different identifier, missed that row, and failed+ // the insert on the per-user name index with a raw "Failed query" error.+ it('recognizes a skill already installed from the same URL under another identifier', async () => {+ const url = 'https://market.lobehub.com/api/v1/skills/openclaw-skills-memory-setup/download';+ mockSsrfSafeFetch.mockResolvedValue({+ arrayBuffer: async () => new ArrayBuffer(0),+ ok: true,+ status: 200,+ });+ mockParserInstance.parseZipPackage.mockResolvedValue({+ content: '# Memory Setup Skill',+ manifest: { name: 'memory-setup', description: 'Configure persistent memory' },+ resources: new Map(),+ });++ const installed = await importer.importFromUrl(+ { url },+ { identifier: 'openclaw-skills-memory-setup', source: 'market' },+ );+ const again = await importer.importFromUrl({ url });++ expect(again.status).toBe('unchanged');+ expect(again.skill.id).toBe(installed.skill.id);+ expect(again.skill.identifier).toBe('openclaw-skills-memory-setup');+ });++ it('updates a skill imported under the URL identifier even after its name changed', async () => {+ const url = 'https://market.lobehub.com/api/v1/skills/openclaw-skills-memory-setup/download';+ mockSsrfSafeFetch.mockResolvedValue({+ arrayBuffer: async () => new ArrayBuffer(0),+ ok: true,+ status: 200,+ });+ mockParserInstance.parseZipPackage.mockResolvedValueOnce({+ content: '# Memory Setup Skill',+ manifest: { name: 'memory-setup', description: 'Configure persistent memory' },+ resources: new Map(),+ });+ const legacy = await importer.importFromUrl({ url });++ mockParserInstance.parseZipPackage.mockResolvedValueOnce({+ content: '# Memory Setup Skill v2',+ manifest: { name: 'memory-setup-v2', description: 'Configure persistent memory' },+ resources: new Map(),+ });+ const again = await importer.importFromUrl(+ { url },+ { identifier: 'openclaw-skills-memory-setup', source: 'market' },+ );++ expect(again.status).toBe('updated');+ expect(again.skill.id).toBe(legacy.skill.id);+ expect(again.skill.name).toBe('memory-setup-v2');+ });++ it('does not overwrite a user skill that happens to carry the URL identifier', async () => {+ const url = 'https://market.lobehub.com/api/v1/skills/openclaw-skills-memory-setup/download';+ const userSkill = await importer.createUserSkill({+ content: '# My own notes',+ description: 'Personal skill',+ identifier: 'url.market.lobehub.com.api.v1.skills.openclaw-skills-memory-setup.download',+ name: 'my-notes',+ });+ mockSsrfSafeFetch.mockResolvedValue({+ arrayBuffer: async () => new ArrayBuffer(0),+ ok: true,+ status: 200,+ });+ mockParserInstance.parseZipPackage.mockResolvedValueOnce({+ content: '# Memory Setup Skill',+ manifest: { name: 'memory-setup', description: 'Configure persistent memory' },+ resources: new Map(),+ });++ const imported = await importer.importFromUrl(+ { url },+ { identifier: 'openclaw-skills-memory-setup', source: 'market' },+ );++ expect(imported.status).toBe('created');+ expect(imported.skill.id).not.toBe(userSkill.id);+ const untouched = await db.query.agentSkills.findFirst({+ where: eq(agentSkills.id, userSkill.id),+ });+ expect(untouched?.name).toBe('my-notes');+ expect(untouched?.content).toBe('# My own notes');+ });++ it('rejects a market import whose identifier is taken by a user skill instead of overwriting it', async () => {+ const url = 'https://market.lobehub.com/api/v1/skills/openclaw-skills-memory-setup/download';+ const userSkill = await importer.createUserSkill({+ content: '# My own notes',+ description: 'Personal skill',+ identifier: 'openclaw-skills-memory-setup',+ name: 'my-notes',+ });+ mockSsrfSafeFetch.mockResolvedValue({+ arrayBuffer: async () => new ArrayBuffer(0),+ ok: true,+ status: 200,+ });+ mockParserInstance.parseZipPackage.mockResolvedValueOnce({+ content: '# Memory Setup Skill',+ manifest: { name: 'memory-setup', description: 'Configure persistent memory' },+ resources: new Map(),+ });++ const error = await importer+ .importFromUrl({ url }, { identifier: 'openclaw-skills-memory-setup', source: 'market' })+ .catch((e) => e);++ expect(error).toBeInstanceOf(SkillImportError);+ expect(error.code).toBe('CONFLICT');+ const untouched = await db.query.agentSkills.findFirst({+ where: eq(agentSkills.id, userSkill.id),+ });+ expect(untouched?.name).toBe('my-notes');+ expect(untouched?.content).toBe('# My own notes');+ });++ it('rejects a different skill with an installed name as a CONFLICT naming the installed one', async () => {+ mockSsrfSafeFetch.mockResolvedValue({+ arrayBuffer: async () => new ArrayBuffer(0),+ ok: true,+ status: 200,+ });+ mockParserInstance.parseZipPackage.mockResolvedValue({+ content: '# Image Generation',+ manifest: { name: 'image-generation', description: 'Generate images' },+ resources: new Map(),+ });+ await importer.importFromUrl(+ {+ url: 'https://market.lobehub.com/api/v1/skills/zhayujie-cowagent-image-generation/download',+ },+ { identifier: 'zhayujie-cowagent-image-generation', source: 'market' },+ );++ const error = await importer+ .importFromUrl(+ {+ url: 'https://market.lobehub.com/api/v1/skills/onyx-dot-app-onyx-image-generation/download',+ },+ { identifier: 'onyx-dot-app-onyx-image-generation', source: 'market' },+ )+ .catch((e) => e);++ expect(error).toBeInstanceOf(SkillImportError);+ expect(error.code).toBe('CONFLICT');+ expect(error.message).toContain('zhayujie-cowagent-image-generation');+ });+it('should throw INVALID_URL error for invalid URL', async () => {await expect(importer.importFromUrl({ url: 'not-a-valid-url' })).rejects.toThrow(SkillImportError,diff --git a/apps/server/src/services/skill/importer.ts b/apps/server/src/services/skill/importer.tsindex a99dd46d7c1..275a3e5adcc 100644--- a/apps/server/src/services/skill/importer.ts+++ b/apps/server/src/services/skill/importer.ts@@ -417,11 +417,49 @@ export class SkillImporter {.replace(/^\//, '') // Remove leading slash.replace(/\.md$/i, '') // Remove .md extension.replaceAll('/', '.'); // Replace slashes with dots- const identifier = options?.identifier || `url.${url.host}.${pathPart || 'skill'}`;+ const urlIdentifier = `url.${url.host}.${pathPart || 'skill'}`;+ const identifier = options?.identifier || urlIdentifier;log('importFromUrl: identifier=%s', identifier);// 5. Check for existing skill- const existing = await this.skillModel.findByIdentifier(identifier);+ let existing = await this.skillModel.findByIdentifier(identifier);++ // A user may have authored a skill under this very identifier. Only treat+ // the row as this import when it came from the market or from this same+ // URL; otherwise updating it would overwrite the user's own skill.+ if (existing && existing.source !== 'market' && existing.manifest?.sourceUrl !== input.url) {+ throw new SkillImportError(+ `A skill with identifier "${identifier}" is already installed from another source (name: ${existing.name}). Delete it before importing this one.`,+ 'CONFLICT',+ );+ }++ // Older agent-tool imports keyed market skills by the URL-derived+ // identifier. Look that row up too, so a skill whose manifest name changed+ // since then is still updated in place rather than installed twice. Only+ // take it when it was fetched from this same URL: a user skill may carry+ // that identifier explicitly and must not be overwritten.+ if (!existing && identifier !== urlIdentifier) {+ const legacy = await this.skillModel.findByIdentifier(urlIdentifier);+ if (legacy?.manifest?.sourceUrl === input.url) existing = legacy;+ }++ // Names are unique per scope, so the same skill already installed under yet+ // another identifier would fail the insert below with a raw DB error. A+ // same-name row fetched from the same URL is this skill; any other+ // same-name row is a real conflict the caller must resolve.+ if (!existing) {+ const sameName = await this.skillModel.findByName(manifest.name);+ if (sameName) {+ if (sameName.manifest?.sourceUrl !== input.url) {+ throw new SkillImportError(+ `A skill named "${manifest.name}" is already installed (identifier: ${sameName.identifier}). Use the installed skill, or delete it before importing this one.`,+ 'CONFLICT',+ );+ }+ existing = sameName;+ }+ }// 6. Build manifest with source URLconst fullManifest: SkillManifest = {diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skillStore.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skillStore.test.tsindex 25fd4717ac6..efcd127ee2a 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skillStore.test.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skillStore.test.ts@@ -2,8 +2,10 @@ import { RBAC_PERMISSIONS } from '@lobechat/const/rbac';import { beforeEach, describe, expect, it, vi } from 'vitest';const mocks = vi.hoisted(() => ({+ getSkillDownloadUrl: vi.fn(),getUserSettings: vi.fn(),hasAnyPermission: vi.fn(),+ importFromUrl: vi.fn(),SkillImporter: vi.fn(),}));@@ -37,7 +39,7 @@ vi.mock('@/database/models/user', () => ({vi.mock('@/server/services/market', () => ({MarketService: vi.fn(function () {- return {};+ return { getSkillDownloadUrl: mocks.getSkillDownloadUrl };}),}));@@ -68,6 +70,9 @@ describe('skillStoreRuntime', () => {mocks.getUserSettings.mockResolvedValue({ market: { accessToken: 'market-token' } });// Default: caller is allowed to manage workspace skills.mocks.hasAnyPermission.mockResolvedValue(true);+ mocks.SkillImporter.mockImplementation(function () {+ return { importFromUrl: mocks.importFromUrl };+ });});// Regression guard: importing a skill while running inside a workspace must@@ -134,4 +139,35 @@ describe('skillStoreRuntime', () => {expect(mocks.hasAnyPermission).not.toHaveBeenCalled();expect(mocks.SkillImporter).toHaveBeenCalledWith(serverDB, 'user-1', undefined);});++ // The Skill Store UI and `lh skill install` store a market skill under its+ // market identifier. The agent tool used to derive one from the download URL+ // instead, so a skill already installed from the UI was not found and the+ // insert failed on the per-user name index with a raw "Failed query" error.+ it('imports a market skill under its market identifier', async () => {+ const downloadUrl =+ 'https://market.lobehub.com/api/v1/skills/openclaw-skills-memory-setup/download';+ mocks.getSkillDownloadUrl.mockReturnValue(downloadUrl);+ mocks.importFromUrl.mockResolvedValue({+ skill: { id: 'skl_memory', name: 'memory-setup' },+ status: 'unchanged',+ });+ const { skillStoreRuntime } = await import('../skillStore');++ const runtime = (await skillStoreRuntime.factory({+ serverDB,+ toolManifestMap: {},+ userId: 'user-1',+ })) as any;+ const result = await runtime.service.importFromMarket('openclaw-skills-memory-setup');++ expect(mocks.importFromUrl).toHaveBeenCalledWith(+ { url: downloadUrl },+ { identifier: 'openclaw-skills-memory-setup', source: 'market' },+ );+ expect(result).toEqual({+ skill: { id: 'skl_memory', name: 'memory-setup' },+ status: 'unchanged',+ });+ });});diff --git a/apps/server/src/services/toolExecution/serverRuntimes/skillStore.ts b/apps/server/src/services/toolExecution/serverRuntimes/skillStore.tsindex 41f7d82f87e..198213a90f7 100644--- a/apps/server/src/services/toolExecution/serverRuntimes/skillStore.ts+++ b/apps/server/src/services/toolExecution/serverRuntimes/skillStore.ts@@ -153,9 +153,12 @@ class SkillStoreServerRuntimeService implements SkillStoreRuntimeService {}};- importFromZipUrl = async (url: string): Promise<SkillImportServiceResult> => {+ importFromZipUrl = async (+ url: string,+ options?: { identifier?: string; source?: 'market' | 'user' },+ ): Promise<SkillImportServiceResult> => {try {- const result = await this.importer.importFromUrl({ url });+ const result = await this.importer.importFromUrl({ url }, options);await this.emitSkillOutcome({apiName: 'importFromZipUrl',intentClass: 'tool_command',@@ -220,7 +223,9 @@ class SkillStoreServerRuntimeService implements SkillStoreRuntimeService {const downloadUrl = this.marketService.getSkillDownloadUrl(identifier);log('Download URL: %s', downloadUrl);- const result = await this.importFromZipUrl(downloadUrl);+ // Same identifier as the Skill Store UI / `lh skill install` path, so a+ // skill installed there is recognized instead of colliding on its name.+ const result = await this.importFromZipUrl(downloadUrl, { identifier, source: 'market' });log('Import from market result: %O', result);return result;} catch (error) {