π€― 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
import type { ChatToolPayload } from '@lobechat/types';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { BuiltinToolsExecutor } from '../builtin';
import { hasServerRuntime } from '../serverRuntimes';
import type { ToolExecutionContext } from '../types';
const mocks = vi.hoisted(() => ({
apiHandler: vi.fn(),
executeLobehubSkill: vi.fn(),
}));
const mockApiHandler = mocks.apiHandler;
vi.mock('../serverRuntimes', () => ({
hasServerRuntime: vi.fn().mockReturnValue(true),
getServerRuntime: vi.fn(async () => ({
createDocument: mocks.apiHandler,
getCommandOutput: mocks.apiHandler,
runCommand: mocks.apiHandler,
searchUserMemory: mocks.apiHandler,
})),
}));
vi.mock('@/server/services/composio', () => ({
ComposioService: vi.fn().mockImplementation(function () {
return {};
}),
}));
vi.mock('@/server/services/market', () => ({
MarketService: vi.fn().mockImplementation(function () {
return {
executeLobehubSkill: mocks.executeLobehubSkill,
};
}),
}));
// The runtime mock above only exposes `createDocument`, but the manifest is the
// authoritative source of declared APIs β it also lists `listDocuments`, so an
// UNKNOWN_API hint sourced from the manifest must surface both.
//
// The share-gate exports mirror the real module's semantics on this mock's
// registry: both mocked identifiers are "builtin", neither is share-allowed β
// except `lobe-user-memory`, granted so the memory-permission dispatch tests
// below can exercise the per-API data-tool rules.
vi.mock('@lobechat/builtin-tools', () => ({
AGENT_SHARE_ALLOWED_BUILTIN_IDENTIFIERS: new Set(['lobe-user-memory']),
AGENT_SHARE_IMPLIED_BUILTIN_IDENTIFIERS: new Set<string>(),
isBuiltinToolIdentifier: (id: string) =>
['lobe-notebook', 'lobe-task', 'lobe-user-memory'].includes(id),
builtinTools: [
{
identifier: 'lobe-notebook',
manifest: { api: [{ name: 'createDocument' }, { name: 'listDocuments' }] },
},
{
identifier: 'lobe-local-system',
manifest: {
api: [
{
name: 'runCommand',
parameters: {
properties: { command: { type: 'string' }, description: { type: 'string' } },
required: ['description', 'command'],
type: 'object',
},
},
{
name: 'getCommandOutput',
parameters: { properties: { shell_id: { type: 'string' } }, type: 'object' },
},
],
},
},
{
identifier: 'lobe-user-memory',
manifest: {
api: [
{ name: 'searchUserMemory' },
{ name: 'addContextMemory' },
// intervention-gated: the dispatch gate must not re-block it
{ humanIntervention: 'required', name: 'consentGated' },
],
},
},
{
identifier: 'lobe-task',
manifest: {
api: [
{ name: 'createTask', work: { action: 'create', resourceType: 'task' } },
{ name: 'createTasks', work: { action: 'create', resourceType: 'task' } },
{ name: 'editTask', work: { action: 'update', resourceType: 'task' } },
{ name: 'listTasks' },
],
},
},
],
}));
const buildPayload = (argsStr: string): ChatToolPayload => ({
apiName: 'createDocument',
arguments: argsStr,
id: 't1',
identifier: 'lobe-notebook',
type: 'default' as any,
});
const context: ToolExecutionContext = {
toolManifestMap: {},
userId: 'user-1',
};
describe('BuiltinToolsExecutor truncated arguments', () => {
const executor = new BuiltinToolsExecutor({} as any, 'user-1');
beforeEach(() => {
mockApiHandler.mockReset();
mocks.executeLobehubSkill.mockReset();
});
it('short-circuits with TRUNCATED_ARGUMENTS when JSON is cut mid-object', async () => {
const truncated = '{"title": "Report", "description": "foo", "type": "report"';
const result = await executor.execute(buildPayload(truncated), context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('TRUNCATED_ARGUMENTS');
expect(result.content).toMatch(/truncated/i);
// Both causes are listed and neither is asserted: blaming max_tokens made
// models tell users the platform caps output when the stream was cut.
expect(result.content).toMatch(/output length limit/);
expect(result.content).toMatch(/stream was interrupted/);
expect(result.content).not.toMatch(/max_tokens/);
// The raw truncated payload is echoed back so the model sees exactly what
// it produced and cannot blame upstream for a different payload.
expect(result.content).toContain(truncated);
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('short-circuits with TRUNCATED_ARGUMENTS when a string value is unterminated', async () => {
const truncated = '{"title": "Report", "content": "this is cut';
const result = await executor.execute(buildPayload(truncated), context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('TRUNCATED_ARGUMENTS');
expect(result.content).toMatch(/unterminated string/);
expect(result.content).toContain(truncated);
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('still dispatches to the runtime for valid JSON missing required fields', async () => {
mockApiHandler.mockResolvedValueOnce({
content: 'Error: Missing content. The document content is required.',
success: false,
});
const result = await executor.execute(
buildPayload('{"title": "Report", "type": "report"}'),
context,
);
expect(mockApiHandler).toHaveBeenCalledWith({ title: 'Report', type: 'report' }, context);
// The schema-level error from the runtime passes through untouched.
expect(result.success).toBe(false);
expect(result.content).toMatch(/Missing content/);
});
it('returns INVALID_JSON_ARGUMENTS for balanced-but-invalid JSON (not truncated)', async () => {
// Balanced brackets but invalid syntax (unquoted key). Not a truncation,
// but still unparseable β reject with a non-truncation error rather than
// silently passing `{}` to the tool.
const invalid = '{title: "Report"}';
const result = await executor.execute(buildPayload(invalid), context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('INVALID_JSON_ARGUMENTS');
expect(result.content).toMatch(/not valid JSON/);
expect(result.content).toContain(invalid);
expect(mockApiHandler).not.toHaveBeenCalled();
});
// verify the self-reflection signal survives the new persist-time
// sanitizer. The fix sanitizes `tool_calls[].arguments` only at DB/state
// boundaries (to unbreak strict providers), so the raw bad string must still
// reach the executor β otherwise the model loses the "fix your JSON syntax"
// feedback and degrades to a generic "missing required field" error.
it('emits INVALID_JSON_ARGUMENTS for the Qwen shape with raw args echoed', async () => {
const invalid = '{, "description": "Create data models", "language": "python"}';
const result = await executor.execute(buildPayload(invalid), context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('INVALID_JSON_ARGUMENTS');
expect(result.content).toMatch(/not valid JSON/);
// Critical: the raw malformed string must appear in the tool-result content
// so the model can self-correct based on what it actually produced.
expect(result.content).toContain(invalid);
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('still dispatches normally when argsStr is empty', async () => {
mockApiHandler.mockResolvedValueOnce({ content: 'ok', success: true });
// Empty arguments are legitimate for tools that take no params β
// parse falls through to `{}` without triggering the invalid-JSON guard.
const result = await executor.execute(buildPayload(''), context);
expect(mockApiHandler).toHaveBeenCalledWith({}, context);
expect(result.success).toBe(true);
});
describe('empty arguments string', () => {
const buildRunCommandPayload = (argsStr: string): ChatToolPayload => ({
apiName: 'runCommand',
arguments: argsStr,
id: 't-run',
identifier: 'lobe-local-system',
type: 'builtin' as any,
});
// A tool call whose argument deltas never arrived (e.g. an OpenAI-compatible
// proxy that drops them) accumulates to `arguments: ""`. Silently passing
// `{}` made the device reply "command is required", which misled the model
// into blaming the platform instead of resending the call.
it('returns EMPTY_ARGUMENTS without invoking an API that declares required params', async () => {
const result = await executor.execute(buildRunCommandPayload(''), context);
expect(mockApiHandler).not.toHaveBeenCalled();
expect(result.success).toBe(false);
expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
expect(result.content).toMatch(/empty arguments string/);
expect(result.content).toMatch(/command/);
});
it('treats a whitespace-only arguments string as empty', async () => {
const result = await executor.execute(buildRunCommandPayload(' '), context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('prefers the manifest from the execution context when present', async () => {
const result = await executor.execute(
{ ...buildRunCommandPayload(''), apiName: 'createDocument', identifier: 'lobe-notebook' },
{
...context,
toolManifestMap: {
'lobe-notebook': {
api: [
{
description: '',
name: 'createDocument',
parameters: {
properties: { title: { type: 'string' } },
required: ['title'],
type: 'object',
},
},
],
identifier: 'lobe-notebook',
meta: {},
type: 'builtin',
} as any,
},
},
);
expect(mockApiHandler).not.toHaveBeenCalled();
expect(result.error?.code).toBe('EMPTY_ARGUMENTS');
});
it('still dispatches with {} for an API without required params', async () => {
mockApiHandler.mockResolvedValueOnce({ content: 'ok', success: true });
const result = await executor.execute(
{ ...buildRunCommandPayload(''), apiName: 'getCommandOutput' },
context,
);
expect(mockApiHandler).toHaveBeenCalledWith({}, context);
expect(result.success).toBe(true);
});
});
it('returns a recoverable UNKNOWN_API error for a hallucinated apiName', async () => {
// The runtime mock only exposes `createDocument`; calling a non-existent
// API (e.g. a model hallucinating `viewTopic`) must NOT throw a hard error
// β it should return a structured result that lists the real APIs so the
// model can self-correct.
const result = await executor.execute({ ...buildPayload('{}'), apiName: 'viewTopic' }, context);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('UNKNOWN_API');
expect(result.content).toContain('viewTopic');
// The available APIs are surfaced to guide the model.
expect(result.content).toContain('createDocument');
// Sourced from the manifest, not the runtime instance: `listDocuments` is
// declared in the manifest yet absent from the mocked runtime's own keys,
// so its presence proves the hint reads the manifest.
expect(result.content).toContain('listDocuments');
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('lists prototype-method APIs via the fallback when no manifest is available', async () => {
// A runtime whose APIs are class prototype methods (the common case).
// `Object.keys(runtime)` would miss these, collapsing the hint to an empty
// list; the prototype-chain fallback must surface them.
class FooRuntime {
async barApi() {
return { content: 'ok', success: true };
}
}
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce(new FooRuntime() as any);
const result = await executor.execute(
{ ...buildPayload('{}'), apiName: 'hallucinated', identifier: 'lobe-unknown-tool' },
context,
);
expect(result.error?.code).toBe('UNKNOWN_API');
expect(result.content).toContain('barApi');
});
it('emits a Linear skill Work intent after a successful server-side LobeHub Skill tool call', async () => {
mocks.executeLobehubSkill.mockResolvedValueOnce({
content: JSON.stringify({
id: 'LINEAR-10966',
status: 'In Progress',
title: 'Linear Work issue',
url: 'https://linear.app/lobehub/issue/LINEAR-10966/linear-work-issue',
}),
success: true,
});
const result = await executor.execute(
{
apiName: 'save_issue',
arguments: '{"id":"LINEAR-10966","state":"In Progress"}',
id: 'tool-call-linear',
identifier: 'linear',
source: 'lobehubSkill',
type: 'default' as any,
},
{ ...context, executionTimeoutMs: 45_000, topicId: 'topic-1' },
);
expect(result.success).toBe(true);
expect(mocks.executeLobehubSkill).toHaveBeenCalledWith({
args: { id: 'LINEAR-10966', state: 'In Progress' },
context: { topicId: 'topic-1' },
provider: 'linear',
timeoutMs: 45_000,
toolName: 'save_issue',
});
// The executor no longer writes the Work β it hands the runtime an intent
// carrying the UNTRUNCATED payload; provenance + cost are stamped by the
// agent runtime at persist time.
expect(result.workRegistration).toEqual({
args: { id: 'LINEAR-10966', state: 'In Progress' },
data: {
id: 'LINEAR-10966',
status: 'In Progress',
title: 'Linear Work issue',
url: 'https://linear.app/lobehub/issue/LINEAR-10966/linear-work-issue',
},
provider: 'linear',
toolName: 'save_issue',
type: 'skill',
});
});
it('emits a GitHub skill Work intent after a successful server-side LobeHub Skill tool call', async () => {
mocks.executeLobehubSkill.mockResolvedValueOnce({
content: JSON.stringify({
html_url: 'https://github.com/lobehub/lobehub/issues/123',
node_id: 'I_kwDOJj1234',
number: 123,
state: 'open',
title: 'GitHub Work issue',
}),
success: true,
});
const result = await executor.execute(
{
apiName: 'create_issue',
arguments: '{"owner":"lobehub","repo":"lobehub","title":"GitHub Work issue"}',
id: 'tool-call-github',
identifier: 'github',
source: 'lobehubSkill',
type: 'default' as any,
},
{ ...context, topicId: 'topic-1' },
);
expect(result.success).toBe(true);
expect(result.workRegistration).toEqual(
expect.objectContaining({
data: expect.objectContaining({ number: 123 }),
provider: 'github',
toolName: 'create_issue',
type: 'skill',
}),
);
});
it('emits no Work intent for non-adapted skill providers', async () => {
mocks.executeLobehubSkill.mockResolvedValueOnce({
content: JSON.stringify({ id: 'msg-1' }),
success: true,
});
const result = await executor.execute(
{
apiName: 'send_message',
arguments: '{}',
id: 'tool-call-ms',
identifier: 'microsoft',
source: 'lobehubSkill',
type: 'default' as any,
},
{ ...context, topicId: 'topic-1' },
);
expect(result.success).toBe(true);
expect(result.workRegistration).toBeUndefined();
});
});
describe('BuiltinToolsExecutor manifest-driven Work registration', () => {
const executor = new BuiltinToolsExecutor({} as any, 'user-1');
const taskContext: ToolExecutionContext = {
agentId: 'agent-1',
operationId: 'op-child',
rootOperationId: 'op-root',
serverDB: {} as NonNullable<ToolExecutionContext['serverDB']>,
threadId: 'thread-1',
toolCallId: 'tool-call-task',
toolManifestMap: {},
toolMessageId: 'msg-tool-task',
topicId: 'topic-1',
userId: 'user-1',
workspaceId: 'workspace-1',
};
const taskPayload = (apiName: string, argsStr = '{}'): ChatToolPayload => ({
apiName,
arguments: argsStr,
id: 'tool-call-task',
identifier: 'lobe-task',
type: 'default' as any,
});
it('emits a task Work intent after a successful createTask, reading identity from state', async () => {
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce({
createTask: vi.fn().mockResolvedValue({
content: 'ok',
state: { identifier: 'T-1', success: true, taskId: 'task_1' },
success: true,
}),
} as any);
const result = await executor.execute(
taskPayload('createTask', '{"name":"A","instruction":"do"}'),
taskContext,
);
expect(result.success).toBe(true);
// Provenance (agent / operation / message / tool-call ids) is added by the
// agent runtime at persist time, so the intent carries only the resolved
// action + targets.
expect(result.workRegistration).toEqual({
action: 'create',
changeType: 'created',
targets: [{ taskId: 'task_1', taskIdentifier: undefined }],
type: 'task',
});
});
it('emits an intent for only the succeeded items of a partial-failure batch', async () => {
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce({
createTasks: vi.fn().mockResolvedValue({
content: 'ok',
state: {
failed: 1,
results: [
{ identifier: 'T-A', name: 'A', success: true },
{ error: 'boom', name: 'B', success: false },
],
succeeded: 1,
},
success: false,
}),
} as any);
const result = await executor.execute(taskPayload('createTasks', '{"tasks":[]}'), taskContext);
expect(result.workRegistration).toEqual({
action: 'create',
changeType: 'created',
targets: [{ taskIdentifier: 'T-A' }],
type: 'task',
});
});
it('emits no intent for an API without a work config', async () => {
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce({
listTasks: vi.fn().mockResolvedValue({ content: 'ok', success: true }),
} as any);
const result = await executor.execute(taskPayload('listTasks'), taskContext);
expect(result.workRegistration).toBeUndefined();
});
it('emits no intent when the update failed (no extractable target)', async () => {
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce({
editTask: vi.fn().mockResolvedValue({ content: 'Task not found', success: false }),
} as any);
const result = await executor.execute(
taskPayload('editTask', '{"identifier":"T-404"}'),
taskContext,
);
expect(result.workRegistration).toBeUndefined();
});
it('emits an update intent for a successful editTask', async () => {
const { getServerRuntime } = await import('../serverRuntimes');
vi.mocked(getServerRuntime).mockResolvedValueOnce({
editTask: vi.fn().mockResolvedValue({ content: 'edited', success: true }),
} as any);
const result = await executor.execute(
taskPayload('editTask', '{"identifier":"T-1","name":"Edited"}'),
taskContext,
);
expect(result.success).toBe(true);
expect(result.workRegistration).toEqual({
action: 'update',
changeType: 'updated',
targets: [{ taskIdentifier: 'T-1' }],
type: 'task',
});
});
});
// Regression for the share-visitor dispatch gate: the assembly-time tool-set
// trim only shapes what the model is OFFERED β the executor must re-block on
// its own, or any path that bypasses assembly (resume, recovery hints, future
// discovery routes) executes with the creator's full permissions.
describe('BuiltinToolsExecutor share-visitor gate', () => {
const executor = new BuiltinToolsExecutor({} as any, 'user-1');
const memoryPayload = (apiName: string): ChatToolPayload => ({
apiName,
arguments: '{"query":"hi"}',
id: 't-share',
identifier: 'lobe-user-memory',
type: 'default' as any,
});
// AgentShareVisitorContext now requires these id fields for billing
// attribution and revocation checks; the gate tests below only vary the
// permission fields, so share the ids across fixtures.
const visitorIds = { agentId: 'agent-1', shareId: 'share-1', visitorUserId: 'visitor-1' };
beforeEach(() => {
mockApiHandler.mockReset();
mockApiHandler.mockResolvedValue({ content: 'ok', success: true });
});
it('blocks a builtin identifier outside the share allowlist', async () => {
const result = await executor.execute(buildPayload('{}'), {
...context,
agentShareVisitor: { ...visitorIds },
});
expect(result.success).toBe(false);
expect(result.error?.code).toBe('SHARE_GATE_BLOCKED');
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('executes the same call untouched when the run is not a share run', async () => {
const result = await executor.execute(buildPayload('{}'), context);
expect(result.error?.code).not.toBe('SHARE_GATE_BLOCKED');
expect(mockApiHandler).toHaveBeenCalled();
});
it('blocks an allowlisted builtin the owner did not enable for the share', async () => {
// On the master allowlist AND permission-granted, but absent from the
// owner's toolGrants picker β must still be blocked at dispatch.
const result = await executor.execute(memoryPayload('searchUserMemory'), {
...context,
agentShareVisitor: { ...visitorIds, allowReadMemory: true },
});
expect(result.error?.code).toBe('SHARE_GATE_BLOCKED');
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('does not share-block a granted consent-gated (humanIntervention) API', async () => {
// Share runs keep the intervention config and the visitor approves the
// call before it is dispatched, so the dispatch gate must not re-block it.
const result = await executor.execute(memoryPayload('consentGated'), {
...context,
agentShareVisitor: {
...visitorIds,
allowReadMemory: true,
toolGrants: [{ identifier: 'lobe-user-memory' }],
},
});
expect(result.error?.code).not.toBe('SHARE_GATE_BLOCKED');
});
it('blocks memory reads without allowReadMemory', async () => {
const result = await executor.execute(memoryPayload('searchUserMemory'), {
...context,
agentShareVisitor: { ...visitorIds, toolGrants: [{ identifier: 'lobe-user-memory' }] },
});
expect(result.error?.code).toBe('SHARE_GATE_BLOCKED');
expect(mockApiHandler).not.toHaveBeenCalled();
});
it('allows memory reads with allowReadMemory but still blocks memory writes', async () => {
const read = await executor.execute(memoryPayload('searchUserMemory'), {
...context,
agentShareVisitor: {
...visitorIds,
allowReadMemory: true,
toolGrants: [{ identifier: 'lobe-user-memory' }],
},
});
expect(read.error?.code).not.toBe('SHARE_GATE_BLOCKED');
expect(mockApiHandler).toHaveBeenCalled();
const write = await executor.execute(memoryPayload('addContextMemory'), {
...context,
agentShareVisitor: {
...visitorIds,
allowReadMemory: true,
toolGrants: [{ identifier: 'lobe-user-memory' }],
},
});
expect(write.error?.code).toBe('SHARE_GATE_BLOCKED');
});
});
describe('BuiltinToolsExecutor Composio app without an active connection', () => {
const executor = new BuiltinToolsExecutor({} as any, 'user-1');
// Only an ACTIVE Composio connection is routed with `source: 'composio'`. A
// stale activation (or a resumed run whose toolset predates a status change)
// lands here without that source β the model must learn the app is not
// connected, not that the tool "is not implemented".
it('returns COMPOSIO_NOT_CONNECTED instead of throwing "not implemented"', async () => {
vi.mocked(hasServerRuntime).mockReturnValueOnce(false);
const result = await executor.execute(
{
apiName: 'GMAIL_FETCH_EMAILS',
arguments: '{"query":"newer_than:3d"}',
id: 'call-gmail',
identifier: 'gmail',
type: 'default' as any,
},
context,
);
expect(result.success).toBe(false);
expect(result.error?.code).toBe('COMPOSIO_NOT_CONNECTED');
expect(result.content).toContain('Gmail is not connected');
expect(result.content).toContain('Settings β Connectors');
expect(result.content).not.toMatch(/not implemented/);
});
it('still reports an unknown non-Composio builtin as not implemented', async () => {
vi.mocked(hasServerRuntime).mockReturnValueOnce(false);
await expect(
executor.execute(
{
apiName: 'doThing',
arguments: '{}',
id: 'call-x',
identifier: 'lobe-missing-tool',
type: 'default' as any,
},
context,
),
).rejects.toThrow('Builtin tool "lobe-missing-tool" is not implemented');
});
});