π€― 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: refresh agent skills list after skill import via onAfterCall (#16392)
Arvin Xu2c5779420f99
diff --git a/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.test.ts b/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.test.tsnew file mode 100644index 00000000000..a5f53c65995--- /dev/null+++ b/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.test.ts@@ -0,0 +1,60 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { SkillStoreExecutionRuntime, type SkillStoreRuntimeService } from './index';++const importResult = { skill: { id: 's1', name: 'My Skill' }, status: 'created' as const };++const createService = (): SkillStoreRuntimeService => ({+ importFromGitHub: vi.fn().mockResolvedValue(importResult),+ importFromMarket: vi.fn().mockResolvedValue(importResult),+ importFromUrl: vi.fn().mockResolvedValue(importResult),+ importFromZipUrl: vi.fn().mockResolvedValue(importResult),+ onSkillImported: vi.fn().mockResolvedValue(undefined),+ searchSkill: vi.fn(),+});++describe('SkillStoreExecutionRuntime β direct/local invoke refresh', () => {+ let service: SkillStoreRuntimeService;+ let runtime: SkillStoreExecutionRuntime;++ beforeEach(() => {+ service = createService();+ runtime = new SkillStoreExecutionRuntime({ service });+ });++ it('refreshes the skills list inline after a successful importSkill', async () => {+ const result = await runtime.importSkill({ type: 'url', url: 'https://example.com/skill' });++ expect(result.success).toBe(true);+ expect(service.onSkillImported).toHaveBeenCalledTimes(1);+ });++ it('refreshes the skills list inline after a successful importFromMarket', async () => {+ const result = await runtime.importFromMarket({ identifier: 'cool-skill' });++ expect(result.success).toBe(true);+ expect(service.onSkillImported).toHaveBeenCalledTimes(1);+ });++ it('does not refresh when the import throws', async () => {+ (service.importFromUrl as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('boom'));++ const result = await runtime.importSkill({ type: 'url', url: 'https://example.com/skill' });++ expect(result.success).toBe(false);+ expect(service.onSkillImported).not.toHaveBeenCalled();+ });++ it('does not refresh for searchSkill', async () => {+ (service.searchSkill as ReturnType<typeof vi.fn>).mockResolvedValueOnce({+ items: [],+ page: 1,+ pageSize: 10,+ total: 0,+ });++ await runtime.searchSkill({ q: 'anything' });++ expect(service.onSkillImported).not.toHaveBeenCalled();+ });+});diff --git a/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.ts b/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.tsindex 463ad971622..df3758968e7 100644--- a/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.ts+++ b/packages/builtin-tool-skill-store/src/ExecutionRuntime/index.ts@@ -34,6 +34,17 @@ export class SkillStoreExecutionRuntime {this.service = options.service;}+ /**+ * Notify the host that a skill was imported so it can refresh client state+ * (e.g. the agent skills list). Invoked from the executor's `onAfterCall`+ * lifecycle hook, which fires on `tool_end` regardless of whether the import+ * actually ran client- or server-side β covering the server-runtime path the+ * inline service callback can't reach.+ */+ notifyImported(): Promise<void> {+ return this.service.onSkillImported?.() ?? Promise.resolve();+ }+async importSkill(args: ImportSkillParams): Promise<BuiltinServerRuntimeOutput> {const { url, type } = args;@@ -57,7 +68,10 @@ export class SkillStoreExecutionRuntime {result = await this.service.importFromUrl(url);}- // Refresh skills list so the new skill becomes available+ // Refresh the skills list for the direct/local client invoke path+ // (`invokeBuiltinTool`), which never dispatches the executor's+ // `onAfterCall`. The gateway path covers itself via `onAfterCall` and+ // routes import through the server runtime, so this never double-fires.await this.service.onSkillImported?.();return {@@ -133,7 +147,7 @@ export class SkillStoreExecutionRuntime {try {const result = await this.service.importFromMarket(identifier);- // Refresh skills list so the new skill becomes available+ // See importSkill: refresh for the direct/local client invoke path.await this.service.onSkillImported?.();return {diff --git a/packages/builtin-tool-skill-store/src/executor/index.test.ts b/packages/builtin-tool-skill-store/src/executor/index.test.tsnew file mode 100644index 00000000000..6a793c5e6e8--- /dev/null+++ b/packages/builtin-tool-skill-store/src/executor/index.test.ts@@ -0,0 +1,48 @@+import type { ToolAfterCallContext } from '@lobechat/types';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import type { SkillStoreExecutionRuntime } from '../ExecutionRuntime';+import { SkillStoreApiName, SkillStoreIdentifier } from '../types';+import { SkillStoreExecutor } from './index';++describe('SkillStoreExecutor.onAfterCall', () => {+ const notifyImported = vi.fn().mockResolvedValue(undefined);+ // Only `notifyImported` is exercised by onAfterCall; cast a minimal stub.+ const runtime = { notifyImported } as unknown as SkillStoreExecutionRuntime;+ const executor = new SkillStoreExecutor(runtime);++ const createCtx = (apiName: string, success: boolean): ToolAfterCallContext => ({+ apiName,+ identifier: SkillStoreIdentifier,+ params: {},+ result: { content: '', success },+ });++ beforeEach(() => {+ notifyImported.mockClear();+ });++ it('refreshes the skills list after a successful importSkill', async () => {+ await executor.onAfterCall(createCtx(SkillStoreApiName.importSkill, true));++ expect(notifyImported).toHaveBeenCalledTimes(1);+ });++ it('refreshes the skills list after a successful importFromMarket', async () => {+ await executor.onAfterCall(createCtx(SkillStoreApiName.importFromMarket, true));++ expect(notifyImported).toHaveBeenCalledTimes(1);+ });++ it('does not refresh for searchSkill', async () => {+ await executor.onAfterCall(createCtx(SkillStoreApiName.searchSkill, true));++ expect(notifyImported).not.toHaveBeenCalled();+ });++ it('does not refresh when the import failed', async () => {+ await executor.onAfterCall(createCtx(SkillStoreApiName.importSkill, false));++ expect(notifyImported).not.toHaveBeenCalled();+ });+});diff --git a/packages/builtin-tool-skill-store/src/executor/index.ts b/packages/builtin-tool-skill-store/src/executor/index.tsindex 2ce2bb2ee73..fc77635627d 100644--- a/packages/builtin-tool-skill-store/src/executor/index.ts+++ b/packages/builtin-tool-skill-store/src/executor/index.ts@@ -1,4 +1,9 @@-import { BaseExecutor, type BuiltinToolContext, type BuiltinToolResult } from '@lobechat/types';+import {+ BaseExecutor,+ type BuiltinToolContext,+ type BuiltinToolResult,+ type ToolAfterCallContext,+} from '@lobechat/types';import type { SkillStoreExecutionRuntime } from '../ExecutionRuntime';import {@@ -9,6 +14,13 @@ import {SkillStoreIdentifier,} from '../types';+// APIs that import a skill into the user's library. Used by `onAfterCall` to+// decide when to refresh the client-side skills list.+const IMPORT_APIS = new Set<string>([+ SkillStoreApiName.importSkill,+ SkillStoreApiName.importFromMarket,+]);+class SkillStoreExecutor extends BaseExecutor<typeof SkillStoreApiName> {readonly identifier = SkillStoreIdentifier;protected readonly apiEnum = SkillStoreApiName;@@ -20,6 +32,15 @@ class SkillStoreExecutor extends BaseExecutor<typeof SkillStoreApiName> {this.runtime = runtime;}+ // Refresh the client skills list after a successful import. Lives here rather+ // than inline in the runtime so it fires on `tool_end` regardless of whether+ // the import ran client- or server-side β the server-runtime path never+ // touches the client store otherwise.+ onAfterCall = async ({ apiName, result }: ToolAfterCallContext): Promise<void> => {+ if (!IMPORT_APIS.has(apiName) || !result.success) return;+ await this.runtime.notifyImported();+ };+importSkill = async (params: ImportSkillParams,ctx: BuiltinToolContext,diff --git a/packages/builtin-tool-skill-store/vitest.config.mts b/packages/builtin-tool-skill-store/vitest.config.mtsnew file mode 100644index 00000000000..4ac6027d578--- /dev/null+++ b/packages/builtin-tool-skill-store/vitest.config.mts@@ -0,0 +1,7 @@+import { defineConfig } from 'vitest/config';++export default defineConfig({+ test: {+ environment: 'node',+ },+});