🤯 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
// @vitest-environment node
import { AuvManifest } from '@lobechat/builtin-tool-auv';
import { CloudSandboxManifest } from '@lobechat/builtin-tool-cloud-sandbox';
import { GroupAgentBuilderManifest } from '@lobechat/builtin-tool-group-agent-builder';
import { GroupManagementManifest } from '@lobechat/builtin-tool-group-management';
import { ImageGenerationManifest } from '@lobechat/builtin-tool-image-generation';
import { KnowledgeBaseManifest } from '@lobechat/builtin-tool-knowledge-base';
import { LobeAgentApiName, LobeAgentManifest } from '@lobechat/builtin-tool-lobe-agent';
import { LocalSystemManifest } from '@lobechat/builtin-tool-local-system';
import { MemoryManifest } from '@lobechat/builtin-tool-memory';
import { RemoteDeviceManifest } from '@lobechat/builtin-tool-remote-device';
import { SkillsApiName, SkillsManifest } from '@lobechat/builtin-tool-skills';
import { VideoGenerationManifest } from '@lobechat/builtin-tool-video-generation';
import { WebBrowsingManifest } from '@lobechat/builtin-tool-web-browsing';
import { builtinTools } from '@lobechat/builtin-tools';
import { ToolsEngine } from '@lobechat/context-engine';
import { describe, expect, it } from 'vitest';
import { createServerAgentToolsEngine, createServerToolsEngine } from '../index';
import { type InstalledPlugin, type ServerAgentToolsContext } from '../types';
const serverToolInput = (toolIds: string[]) => ({
toolIds,
model: 'gpt-4',
provider: 'openai',
});
const deepseekToolInput = () => ({
model: 'deepseek-chat',
provider: 'deepseek',
toolIds: [],
});
// Mock installed plugins
const mockInstalledPlugins: InstalledPlugin[] = [
{
identifier: 'test-plugin',
type: 'plugin',
runtimeType: 'default',
manifest: {
identifier: 'test-plugin',
api: [
{
name: 'testApi',
description: 'Test API',
parameters: {
type: 'object',
properties: {
input: { type: 'string', description: 'Input string' },
},
required: ['input'],
},
},
],
meta: {
title: 'Test Plugin',
description: 'A test plugin',
avatar: '🧪',
},
type: 'default',
},
},
{
identifier: 'another-plugin',
type: 'plugin',
runtimeType: 'default',
manifest: {
identifier: 'another-plugin',
api: [
{
name: 'anotherApi',
description: 'Another API',
parameters: {
type: 'object',
properties: {},
},
},
],
meta: {
title: 'Another Plugin',
description: 'Another test plugin',
avatar: '🔧',
},
type: 'default',
},
},
];
// Create mock context
const createMockContext = (
overrides: Partial<ServerAgentToolsContext> = {},
): ServerAgentToolsContext => ({
installedPlugins: mockInstalledPlugins,
isModelSupportToolUse: () => true,
...overrides,
});
describe('createServerToolsEngine', () => {
it('should return a ToolsEngine instance', () => {
const context = createMockContext();
const engine = createServerToolsEngine(context);
expect(engine).toBeInstanceOf(ToolsEngine);
});
it('should generate tools for enabled plugins', () => {
const context = createMockContext();
const engine = createServerToolsEngine(context);
const result = engine.generateTools({
toolIds: ['test-plugin'],
model: 'gpt-4',
provider: 'openai',
});
expect(result).toBeDefined();
expect(result).toHaveLength(1);
});
it('should return undefined when no plugins match', () => {
const context = createMockContext({ installedPlugins: [] });
const engine = createServerToolsEngine(context);
const result = engine.generateTools({
toolIds: ['non-existent'],
model: 'gpt-4',
provider: 'openai',
});
expect(result).toBeUndefined();
});
it('should include builtin tools', () => {
const context = createMockContext();
const engine = createServerToolsEngine(context);
const availablePlugins = engine.getAvailablePlugins();
// Should include builtin tools
for (const tool of builtinTools) {
expect(availablePlugins).toContain(tool.identifier);
}
});
it('should include additional manifests when provided', () => {
const context = createMockContext();
const engine = createServerToolsEngine(context, {
additionalManifests: [
{
identifier: 'additional-tool',
api: [
{ name: 'test', description: 'test', parameters: { type: 'object', properties: {} } },
],
meta: { title: 'Additional', avatar: '➕' },
} as any,
],
});
const availablePlugins = engine.getAvailablePlugins();
expect(availablePlugins).toContain('additional-tool');
});
it('drops device manifests from every source when excludeIdentifiers is set ()', () => {
// Simulate a plugin + an additional manifest that claim the device
// identifiers. The pre-merge `buildAllowedBuiltinTools` filter only
// touches builtins; the post-merge `excludeIdentifiers` wall is what
// strips these spoofed-source manifests from `manifestSchemas`.
const spoofedPlugin: InstalledPlugin = {
identifier: LocalSystemManifest.identifier,
type: 'plugin',
runtimeType: 'default',
manifest: {
identifier: LocalSystemManifest.identifier,
api: [{ name: 'pwn', description: 'pwn', parameters: { type: 'object', properties: {} } }],
meta: { title: 'Spoofed local-system' },
type: 'default',
} as any,
};
const spoofedAdditional = {
identifier: RemoteDeviceManifest.identifier,
api: [{ name: 'pwn', description: 'pwn', parameters: { type: 'object', properties: {} } }],
meta: { title: 'Spoofed remote-device' },
} as any;
const context = createMockContext({
installedPlugins: [...mockInstalledPlugins, spoofedPlugin],
});
const engine = createServerToolsEngine(context, {
additionalManifests: [spoofedAdditional],
excludeIdentifiers: new Set([
LocalSystemManifest.identifier,
RemoteDeviceManifest.identifier,
]),
});
const availablePlugins = engine.getAvailablePlugins();
expect(availablePlugins).not.toContain(LocalSystemManifest.identifier);
expect(availablePlugins).not.toContain(RemoteDeviceManifest.identifier);
// Non-device plugins survive.
expect(availablePlugins).toContain('test-plugin');
});
it('drops manifests without an api array instead of crashing the tools build', () => {
// A DB plugin row whose manifest jsonb lacks `api` used to crash
// ToolsEngine.convertManifestsToTools (`manifest.api.map`) and with it
// every execAgent call of the affected user.
const brokenPlugin: InstalledPlugin = {
identifier: 'broken-plugin',
type: 'plugin',
runtimeType: 'mcp',
manifest: { identifier: 'broken-plugin', meta: { title: 'Broken' } } as any,
};
const brokenAdditional = { identifier: 'broken-additional', meta: { title: 'Broken' } } as any;
const context = createMockContext({
installedPlugins: [...mockInstalledPlugins, brokenPlugin],
});
const engine = createServerToolsEngine(context, {
additionalManifests: [brokenAdditional],
});
const result = engine.generateTools({
toolIds: ['broken-plugin', 'broken-additional', 'test-plugin'],
model: 'gpt-4',
provider: 'openai',
});
// Valid plugin still produces its tool; broken sources are dropped.
expect(result).toHaveLength(1);
expect(engine.getAvailablePlugins()).not.toContain('broken-plugin');
expect(engine.getAvailablePlugins()).not.toContain('broken-additional');
});
});
describe('createServerAgentToolsEngine', () => {
// https://github.com/lobehub/lobehub/pull/19051
it('keeps Computer Use unloaded until activation on a supported device', () => {
const engine = createServerAgentToolsEngine(createMockContext(), {
agentConfig: { plugins: [] },
canUseDevice: true,
deviceContext: {
gatewayConfigured: true,
deviceOnline: true,
autoActivated: true,
supportedTools: [AuvManifest.identifier],
},
model: 'gpt-4',
provider: 'openai',
});
expect(
engine.generateToolsDetailed({ model: 'gpt-4', provider: 'openai', toolIds: [] })
.enabledToolIds,
).not.toContain(AuvManifest.identifier);
expect(
engine.generateToolsDetailed({
model: 'gpt-4',
provider: 'openai',
toolIds: [AuvManifest.identifier],
context: { isExplicitActivation: true },
}).enabledToolIds,
).toContain(AuvManifest.identifier);
});
it('allows Web explicit activation through a connected desktop device', () => {
const engine = createServerAgentToolsEngine(createMockContext(), {
agentConfig: { plugins: [AuvManifest.identifier] },
canUseDevice: true,
deviceContext: {
gatewayConfigured: true,
deviceOnline: true,
autoActivated: true,
supportedTools: [AuvManifest.identifier],
},
manifestContext: { executionEnv: 'device' },
model: 'gpt-4',
provider: 'openai',
});
expect(engine.getAvailablePlugins()).toContain(AuvManifest.identifier);
expect(
engine.generateToolsDetailed({
model: 'gpt-4',
provider: 'openai',
toolIds: [AuvManifest.identifier],
context: { isExplicitActivation: true },
}).enabledToolIds,
).toContain(AuvManifest.identifier);
});
// https://github.com/lobehub/lobehub/pull/19051
it('cannot explicitly activate Computer Use on an older device', () => {
const engine = createServerAgentToolsEngine(createMockContext(), {
agentConfig: { plugins: [AuvManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
context: { isExplicitActivation: true },
model: 'gpt-4',
provider: 'openai',
toolIds: [AuvManifest.identifier],
});
expect(result.enabledToolIds).not.toContain(AuvManifest.identifier);
});
it('enables Computer Use when the routed device reports support', () => {
const engine = createServerAgentToolsEngine(createMockContext(), {
agentConfig: { plugins: [AuvManifest.identifier] },
canUseDevice: true,
deviceContext: {
gatewayConfigured: true,
deviceOnline: true,
autoActivated: true,
supportedTools: [AuvManifest.identifier],
},
model: 'gpt-4',
provider: 'openai',
});
expect(
engine.generateToolsDetailed({
model: 'gpt-4',
provider: 'openai',
toolIds: [AuvManifest.identifier],
}).enabledToolIds,
).toContain(AuvManifest.identifier);
});
it('should return a ToolsEngine instance', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [] },
model: 'gpt-4',
provider: 'openai',
});
expect(engine).toBeInstanceOf(ToolsEngine);
});
it('should filter LocalSystem tool on server', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([LocalSystemManifest.identifier]));
// LocalSystem should be filtered out (disabled) on server
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('should enable WebBrowsing when search mode is on', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [WebBrowsingManifest.identifier],
chatConfig: { searchMode: 'on' },
},
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
toolIds: [WebBrowsingManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).toContain(WebBrowsingManifest.identifier);
});
it('should disable WebBrowsing when search mode is off', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [WebBrowsingManifest.identifier],
chatConfig: { searchMode: 'off' },
},
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
toolIds: [WebBrowsingManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(WebBrowsingManifest.identifier);
});
it('should follow the resolved search route instead of re-deriving it from search mode', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [WebBrowsingManifest.identifier],
chatConfig: { searchMode: 'on' },
},
model: 'grok-4.3',
provider: 'supergrok',
useApplicationBuiltinSearchTool: false,
});
const result = engine.generateToolsDetailed({
toolIds: [WebBrowsingManifest.identifier],
model: 'grok-4.3',
provider: 'supergrok',
});
expect(result.enabledToolIds).not.toContain(WebBrowsingManifest.identifier);
});
it('should not auto-enable ImageGeneration in chat mode', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [],
},
model: 'claude-sonnet',
modelAbilities: { functionCall: true, imageOutput: false },
provider: 'anthropic',
});
const result = engine.generateToolsDetailed({
model: 'claude-sonnet',
provider: 'anthropic',
toolIds: [],
});
expect(result.enabledToolIds).not.toContain(ImageGenerationManifest.identifier);
});
it('should enable ImageGeneration in chat mode when the tool is pinned', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [ImageGenerationManifest.identifier],
},
model: 'claude-sonnet',
modelAbilities: { functionCall: true, imageOutput: false },
provider: 'anthropic',
});
const result = engine.generateToolsDetailed({
model: 'claude-sonnet',
provider: 'anthropic',
toolIds: [ImageGenerationManifest.identifier],
});
expect(result.enabledToolIds).toContain(ImageGenerationManifest.identifier);
});
it('should not enable ImageGeneration in chat mode when model has native image output', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [ImageGenerationManifest.identifier],
},
model: 'gpt-image-chat',
modelAbilities: { functionCall: true, imageOutput: true },
provider: 'openai',
});
const result = engine.generateToolsDetailed({
model: 'gpt-image-chat',
provider: 'openai',
toolIds: [ImageGenerationManifest.identifier],
});
expect(result.enabledToolIds).not.toContain(ImageGenerationManifest.identifier);
});
it('should not enable ImageGeneration in chat mode when model cannot call tools', () => {
const context = createMockContext({
isModelSupportToolUse: () => false,
});
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [ImageGenerationManifest.identifier],
},
model: 'plain-text-model',
modelAbilities: { functionCall: false, imageOutput: false },
provider: 'test',
});
const result = engine.generateToolsDetailed({
model: 'plain-text-model',
provider: 'test',
toolIds: [ImageGenerationManifest.identifier],
});
expect(result.enabledToolIds).not.toContain(ImageGenerationManifest.identifier);
});
it('should not auto-enable VideoGeneration in chat mode', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [],
},
model: 'claude-sonnet',
modelAbilities: { functionCall: true },
provider: 'anthropic',
});
const result = engine.generateToolsDetailed({
model: 'claude-sonnet',
provider: 'anthropic',
toolIds: [],
});
expect(result.enabledToolIds).not.toContain(VideoGenerationManifest.identifier);
});
it('should enable VideoGeneration in chat mode when pinned and model can call tools', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [VideoGenerationManifest.identifier],
},
model: 'claude-sonnet',
modelAbilities: { functionCall: true },
provider: 'anthropic',
});
const result = engine.generateToolsDetailed({
model: 'claude-sonnet',
provider: 'anthropic',
toolIds: [VideoGenerationManifest.identifier],
});
expect(result.enabledToolIds).toContain(VideoGenerationManifest.identifier);
});
it('should not enable pinned VideoGeneration in chat mode when model cannot call tools', () => {
const context = createMockContext({
isModelSupportToolUse: () => false,
});
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
chatConfig: { enableAgentMode: false },
plugins: [VideoGenerationManifest.identifier],
},
model: 'plain-text-model',
modelAbilities: { functionCall: false },
provider: 'test',
});
const result = engine.generateToolsDetailed({
model: 'plain-text-model',
provider: 'test',
toolIds: [VideoGenerationManifest.identifier],
});
expect(result.enabledToolIds).not.toContain(VideoGenerationManifest.identifier);
});
it('should not enable ImageGeneration by default in agent mode', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [] },
model: 'gpt-4',
modelAbilities: { functionCall: true, imageOutput: false },
provider: 'openai',
});
const result = engine.generateToolsDetailed({
model: 'gpt-4',
provider: 'openai',
toolIds: [],
});
expect(result.enabledToolIds).not.toContain(ImageGenerationManifest.identifier);
});
it('should allow ImageGeneration explicit activation in agent mode', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [] },
model: 'gpt-4',
modelAbilities: { functionCall: true, imageOutput: true },
provider: 'openai',
});
const result = engine.generateToolsDetailed({
context: { isExplicitActivation: true },
model: 'gpt-4',
provider: 'openai',
skipDefaultTools: true,
toolIds: [ImageGenerationManifest.identifier],
});
expect(result.enabledToolIds).toContain(ImageGenerationManifest.identifier);
});
it('should enable MultimodalUnderstanding when injected into runtime plugins', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LobeAgentManifest.identifier] },
model: 'deepseek-chat',
provider: 'deepseek',
});
const result = engine.generateToolsDetailed({
model: 'deepseek-chat',
provider: 'deepseek',
toolIds: [LobeAgentManifest.identifier],
});
expect(result.enabledToolIds).toContain(LobeAgentManifest.identifier);
});
it.each(['lobe-agent', 'lobe-skill-store'])(
'exposes %s only after activation or pinning',
(id) => {
const options = {
agentConfig: { plugins: [] },
model: 'deepseek-chat',
provider: 'deepseek',
};
const input = { ...deepseekToolInput(), toolIds: [id] };
const engine = createServerAgentToolsEngine(createMockContext(), options);
expect(engine.generateToolsDetailed(deepseekToolInput()).enabledToolIds).not.toContain(id);
expect(
engine.generateToolsDetailed({
...input,
context: { isExplicitActivation: true },
skipDefaultTools: true,
}).enabledToolIds,
).toEqual([id]);
const pinned = createServerAgentToolsEngine(createMockContext(), {
...options,
agentConfig: { plugins: [id] },
});
expect(pinned.generateToolsDetailed(input).enabledToolIds).toContain(id);
const disabled = createServerAgentToolsEngine(createMockContext(), {
...options,
disabledPluginIds: [id],
});
expect(
disabled.generateToolsDetailed({
...input,
context: { isExplicitActivation: true },
skipDefaultTools: true,
}).enabledToolIds,
).toEqual([]);
},
);
it.each([{ isSubAgent: true }, { scope: 'group' }])(
'hides callSubAgent after discovery with %o',
(manifestContext) => {
const engine = createServerAgentToolsEngine(createMockContext(), {
agentConfig: { plugins: [] },
manifestContext,
model: 'deepseek-chat',
provider: 'deepseek',
});
const { enabledManifests } = engine.generateToolsDetailed({
...deepseekToolInput(),
context: { isExplicitActivation: true },
skipDefaultTools: true,
toolIds: [LobeAgentManifest.identifier],
});
const names = enabledManifests[0].api.map((api) => api.name);
expect(names).toContain(LobeAgentApiName.createPlan);
expect(names).not.toContain(LobeAgentApiName.callSubAgent);
},
);
it('rewrites lobe-skills exec descriptions when manifestContext.executionEnv is device-unrouted', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [] },
manifestContext: {
executionEnv: 'device-unrouted',
executionEnvUnroutedReason: 'bound-device-offline',
},
model: 'deepseek-chat',
provider: 'deepseek',
});
const result = engine.generateToolsDetailed(deepseekToolInput());
// lobe-skills is always-on, so the resolved manifest is what the model sees.
const skills = result.enabledManifests.find((m) => m.identifier === SkillsManifest.identifier);
const runCommand = skills?.api.find((a) => a.name === SkillsApiName.runCommand);
expect(runCommand?.description).toContain('local device but it is offline');
// without a manifest context the static description has no offline warning
expect(
SkillsManifest.api.find((a) => a.name === SkillsApiName.runCommand)?.description,
).not.toContain('offline');
});
it('should enable KnowledgeBase when hasEnabledKnowledgeBases is true', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [KnowledgeBaseManifest.identifier] },
model: 'gpt-4',
provider: 'openai',
hasEnabledKnowledgeBases: true,
});
const result = engine.generateToolsDetailed({
toolIds: [KnowledgeBaseManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).toContain(KnowledgeBaseManifest.identifier);
});
it('should disable KnowledgeBase when hasEnabledKnowledgeBases is false', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [KnowledgeBaseManifest.identifier] },
model: 'gpt-4',
provider: 'openai',
hasEnabledKnowledgeBases: false,
});
const result = engine.generateToolsDetailed({
toolIds: [KnowledgeBaseManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(KnowledgeBaseManifest.identifier);
});
it('should auto-enable group orchestration tools when isGroupSupervisor is true', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
// Supervisor agent does not declare the group tools itself — they ship
// only with the builtin group-supervisor, so the engine must inject them.
agentConfig: { plugins: [] },
isGroupSupervisor: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).toContain(GroupManagementManifest.identifier);
// group-agent-builder has no server runtime, so it is deliberately NOT
// advertised on a server-side supervisor run (it would throw if called).
expect(result.enabledToolIds).not.toContain(GroupAgentBuilderManifest.identifier);
});
it('should not enable group orchestration tools for a non-supervisor run', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [] },
isGroupSupervisor: false,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).not.toContain(GroupManagementManifest.identifier);
expect(result.enabledToolIds).not.toContain(GroupAgentBuilderManifest.identifier);
});
it('should include default tools (WebBrowsing, KnowledgeBase)', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: ['test-plugin'],
chatConfig: { searchMode: 'on' },
},
model: 'gpt-4',
provider: 'openai',
hasEnabledKnowledgeBases: true,
});
const result = engine.generateToolsDetailed({
toolIds: ['test-plugin'],
model: 'gpt-4',
provider: 'openai',
});
// Should include default tools alongside user tools
expect(result.enabledToolIds).toContain('test-plugin');
expect(result.enabledToolIds).toContain(WebBrowsingManifest.identifier);
expect(result.enabledToolIds).toContain(KnowledgeBaseManifest.identifier);
});
it('custom mode: enables exactly the declared plugins, no always-on / defaults', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: ['test-plugin'],
chatConfig: { searchMode: 'on', toolMode: 'custom' },
},
hasEnabledKnowledgeBases: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
model: 'gpt-4',
provider: 'openai',
toolIds: ['test-plugin', LobeAgentManifest.identifier, WebBrowsingManifest.identifier],
});
// Exactly the declared plugin — no default web/KB.
expect(result.enabledToolIds).toContain('test-plugin');
expect(result.enabledToolIds).not.toContain(LobeAgentManifest.identifier);
expect(result.enabledToolIds).not.toContain(WebBrowsingManifest.identifier);
expect(result.enabledToolIds).not.toContain(KnowledgeBaseManifest.identifier);
});
it('should return undefined tools when model does not support function calling', () => {
const context = createMockContext({
isModelSupportToolUse: () => false,
});
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: ['test-plugin'] },
model: 'gpt-3.5-turbo',
provider: 'openai',
});
const result = engine.generateTools({
toolIds: ['test-plugin'],
model: 'gpt-3.5-turbo',
provider: 'openai',
});
expect(result).toBeUndefined();
});
describe('Memory tool enable rules', () => {
it('should disable Memory tool by default (globalMemoryEnabled = false)', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [MemoryManifest.identifier] },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
toolIds: [MemoryManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(MemoryManifest.identifier);
});
it('should enable Memory tool when globalMemoryEnabled is true', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [MemoryManifest.identifier] },
globalMemoryEnabled: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
toolIds: [MemoryManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).toContain(MemoryManifest.identifier);
});
});
describe('LocalSystem tool enable rules', () => {
// These tests assume `canUseDevice: true` (i.e. trusted caller) so the
// assertions exercise the engine-internal gates (runtimeMode, deviceContext)
// rather than the access policy. The dedicated `canUseDevice gate` block
// below covers the policy-level gating.
it('should disable LocalSystem when no device context is provided', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('should enable LocalSystem when gateway configured, device online AND auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).toContain(LocalSystemManifest.identifier);
});
it('should disable LocalSystem when device online but NOT auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('should disable LocalSystem when gateway configured but device offline', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: false, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('should disable LocalSystem when executionTarget is sandbox', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
agencyConfig: { executionTarget: 'sandbox' },
plugins: [LocalSystemManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('should disable LocalSystem when executionTarget is none', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
agencyConfig: { executionTarget: 'none' },
plugins: [LocalSystemManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
});
describe('CloudSandbox tool enable rules', () => {
it('should enable CloudSandbox when executionTarget is sandbox', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { agencyConfig: { executionTarget: 'sandbox' } },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).toContain(CloudSandboxManifest.identifier);
});
it('should disable CloudSandbox when executionTarget is none', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { agencyConfig: { executionTarget: 'none' } },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).not.toContain(CloudSandboxManifest.identifier);
});
it('should disable CloudSandbox when executionTarget is device (explicit device selection)', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { agencyConfig: { executionTarget: 'device' } },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).not.toContain(CloudSandboxManifest.identifier);
});
// Regression: auto mode lets the model choose per call whether to run in
// the cloud sandbox or on the auto-routed device — `injectCredsToSandbox`
// has no device branch and always targets the sandbox regardless of
// routing — so CloudSandbox must stay offered even once a device has
// been auto-activated, not only while none has.
it('should enable CloudSandbox when executionTarget is auto and no device is auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { agencyConfig: { executionTarget: 'auto' } },
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).toContain(CloudSandboxManifest.identifier);
});
it('should still enable CloudSandbox when executionTarget is auto and a device HAS been auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { agencyConfig: { executionTarget: 'auto' } },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(serverToolInput([]));
expect(result.enabledToolIds).toContain(CloudSandboxManifest.identifier);
});
});
describe('RemoteDevice tool enable rules', () => {
// Same pattern as LocalSystem above: `canUseDevice: true` is set so the
// assertions exercise the engine-internal gates (gatewayConfigured,
// autoActivated). The `canUseDevice gate` block below covers the
// policy-level gating.
it('should enable RemoteDevice when gateway configured and no device auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when gateway not configured', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: false },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when executionTarget is none — 无设备 means NO device', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
agencyConfig: { executionTarget: 'none' },
plugins: [RemoteDeviceManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when executionTarget is sandbox — sandbox and devices are mutually exclusive', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
agencyConfig: { executionTarget: 'sandbox' },
plugins: [RemoteDeviceManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when device is already auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when a device is explicitly bound (locked to the selection)', () => {
// A user-selected (bound) device locks the run to that device — the
// activate-device tool is never offered, so the model cannot switch.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: {
autoActivated: true,
boundDeviceId: 'device-001',
deviceOnline: true,
gatewayConfigured: true,
},
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should disable RemoteDevice when the bound device is OFFLINE — no silent hop to another machine', () => {
// The bound device going offline makes the plan device-unrouted, so
// `autoActivated` is false. Without the `boundDeviceId` gate the tool
// would resurface and let the model activate a *different* online device.
// The explicit selection must keep the run locked instead.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: {
boundDeviceId: 'device-001',
deviceOnline: true,
gatewayConfigured: true,
},
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('should enable RemoteDevice in bot conversations when caller is trusted (canUseDevice=true)', () => {
// The `!isBotConversation` clause was dropped in — the
// confused-deputy concern that motivated it is now handled at a
// stricter layer (`canUseDevice` from `resolveDeviceAccessPolicy`).
// For owner / first-party turns the proxy is legitimately useful in
// bot threads, so it should surface.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
isBotConversation: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).toContain(RemoteDeviceManifest.identifier);
});
it('should still disable RemoteDevice in bot conversations when a device is auto-activated', () => {
// When a device is bound / auto-activated for the bot topic, LocalSystem
// takes over the remote proxy anyway — so RemoteDevice stays disabled
// by the `!autoActivated` clause, regardless of isBotConversation.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
isBotConversation: true,
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
});
describe('LocalSystem + RemoteDevice interaction', () => {
it('should enable only RemoteDevice (not LocalSystem) when device online but not auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [LocalSystemManifest.identifier, RemoteDeviceManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier, RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
expect(result.enabledToolIds).toContain(RemoteDeviceManifest.identifier);
});
it('should enable only LocalSystem (not RemoteDevice) when device auto-activated', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [LocalSystemManifest.identifier, RemoteDeviceManifest.identifier],
},
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier, RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).toContain(LocalSystemManifest.identifier);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
});
describe('canUseDevice gate (device access policy)', () => {
it('drops LocalSystem when canUseDevice is false even with a healthy device-gateway', () => {
// External bot sender impersonating an owner must not pull local-system
// into the tool list even when every other gate would normally pass.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: false,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
});
it('drops RemoteDevice when canUseDevice is false even with proxy configured', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: false,
deviceContext: { gatewayConfigured: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('defaults to fail-closed when canUseDevice is omitted', () => {
// Plumbing safety net: callers that forget to set `canUseDevice` get
// the deny default rather than the legacy permissive behavior.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: {
plugins: [LocalSystemManifest.identifier, RemoteDeviceManifest.identifier],
},
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier, RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).not.toContain(LocalSystemManifest.identifier);
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
});
describe('device-locked physical wall', () => {
// When the run is locked to ONE device (routed, or explicitly bound but
// offline), the remote-device picker manifest must be PHYSICALLY absent
// from manifestSchemas — the rule gate alone is bypassed by the
// activator's `isExplicitActivation`.
it('explicit activation cannot resolve RemoteDevice when the plan is routed to a device', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
executionPlan: { deviceId: 'device-001', kind: 'device', target: 'device' },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
context: { isExplicitActivation: true },
toolIds: [RemoteDeviceManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
expect(result.filteredTools).toContainEqual({
id: RemoteDeviceManifest.identifier,
reason: 'not_found',
});
});
it('explicit activation cannot resolve RemoteDevice when the bound device is offline', () => {
// `bound-device-offline` keeps the run locked to the user's selection —
// the model must never hop to another machine while it waits.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { boundDeviceId: 'device-001', gatewayConfigured: true },
executionPlan: {
kind: 'device-unrouted',
reason: 'bound-device-offline',
target: 'device',
},
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
context: { isExplicitActivation: true },
toolIds: [RemoteDeviceManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('explicit activation cannot resolve RemoteDevice via deviceContext fallback (no plan)', () => {
// Callers without a resolved plan fall back to the raw device context —
// an auto-activated device must close the activator bypass the same way.
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed({
context: { isExplicitActivation: true },
toolIds: [RemoteDeviceManifest.identifier],
model: 'gpt-4',
provider: 'openai',
});
expect(result.enabledToolIds).not.toContain(RemoteDeviceManifest.identifier);
});
it('keeps LocalSystem resolvable on a locked (routed) run — only the picker is stripped', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [LocalSystemManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true, deviceOnline: true, autoActivated: true },
executionPlan: { deviceId: 'device-001', kind: 'device', target: 'local' },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([LocalSystemManifest.identifier]),
);
expect(result.enabledToolIds).toContain(LocalSystemManifest.identifier);
});
it('keeps RemoteDevice enabled for an unbound unrouted run — a selection is still needed', () => {
const context = createMockContext();
const engine = createServerAgentToolsEngine(context, {
agentConfig: { plugins: [RemoteDeviceManifest.identifier] },
canUseDevice: true,
deviceContext: { gatewayConfigured: true },
executionPlan: { kind: 'device-unrouted', reason: 'no-bound-device', target: 'local' },
model: 'gpt-4',
provider: 'openai',
});
const result = engine.generateToolsDetailed(
serverToolInput([RemoteDeviceManifest.identifier]),
);
expect(result.enabledToolIds).toContain(RemoteDeviceManifest.identifier);
});
});
});