🤯 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 AgentState, normalizeAgentState } from '@lobechat/agent-runtime';
import { LobeActivatorManifest } from '@lobechat/builtin-tool-activator';
import { CredsIdentifier, CredsManifest } from '@lobechat/builtin-tool-creds';
import { LocalSystemManifest } from '@lobechat/builtin-tool-local-system';
import { describe, expect, it } from 'vitest';
import { resolveServerCallLlmTooling } from '../serverCallLlmTooling';
const buildState = (
state: Pick<
AgentState,
| 'binding'
| 'operationToolSet'
| 'plan'
| 'principal'
| 'toolExecutorMap'
| 'toolManifestMap'
| 'toolSourceMap'
| 'tools'
> = {},
): AgentState => state as AgentState;
const webBrowsing = {
api: [{ description: 'Search the web', name: 'search', parameters: {} }],
identifier: 'lobe-web-browsing',
meta: { title: 'Web Browsing' },
type: 'builtin',
} as unknown as NonNullable<AgentState['toolManifestMap']>[string];
const searchTool = {
function: { name: 'lobe-web-browsing____search' },
type: 'function',
} as unknown as NonNullable<AgentState['tools']>[number];
describe('resolveServerCallLlmTooling', () => {
it('offers credential discovery before activation and the credential workflow afterwards', () => {
const ctx = { operationId: 'op-1', stepIndex: 0 };
const state = buildState({
operationToolSet: {
enabledToolIds: [LobeActivatorManifest.identifier],
manifestMap: {
[LobeActivatorManifest.identifier]: LobeActivatorManifest,
[CredsIdentifier]: CredsManifest,
},
sourceMap: {},
tools: [],
},
});
const before = resolveServerCallLlmTooling(ctx, state);
const discovery = before.toolDiscoveryConfig?.availableTools.find(
(tool) => tool.identifier === CredsIdentifier,
);
expect(discovery?.description).toContain('API keys');
expect(before.resolved.promptManifestMap).not.toHaveProperty(CredsIdentifier);
const after = resolveServerCallLlmTooling(
{ ...ctx, stepIndex: 1 },
{
...state,
activatedStepTools: [
{ activatedAtStep: 0, id: CredsIdentifier, manifest: CredsManifest, source: 'discovery' },
],
},
);
for (const { name } of CredsManifest.api) {
expect(
before.resolved.promptManifestMap[LobeActivatorManifest.identifier].systemRole,
).not.toContain(name);
expect(discovery?.description).not.toContain(name);
expect(after.resolved.promptManifestMap[CredsIdentifier].systemRole).toContain(name);
}
});
it.each([false, true])(
'limits tools after mid-run device selection to the operation pool (allowed=%s)',
(allowed) => {
const callerTool = {
type: 'function' as const,
function: { name: 'lobe-client-fn____inspect' },
};
const result = resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
buildState({
binding: { device: { id: 'device-1' } },
plan: {
execution: { kind: 'device-unrouted', reason: 'no-bound-device', target: 'device' },
},
operationToolSet: {
enabledToolIds: ['lobe-client-fn'],
executorMap: {},
manifestMap: allowed ? { 'lobe-local-system': LocalSystemManifest } : {},
sourceMap: { 'lobe-client-fn': 'client' },
tools: [callerTool],
},
}),
);
expect(result.tools).toContainEqual(callerTool);
expect(result.resolved.enabledToolIds.includes('lobe-local-system')).toBe(allowed);
if (!allowed) expect(result.tools).toEqual([callerTool]);
else expect(result.tools!.length).toBeGreaterThan(1);
},
);
it('defers the local system functions when a device is already routed at start', () => {
const result = resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
buildState({
binding: { device: { id: 'device-1' } },
plan: { execution: { deviceId: 'device-1', kind: 'device', target: 'local' } },
operationToolSet: {
enabledToolIds: ['lobe-activator'],
manifestMap: { 'lobe-local-system': LocalSystemManifest },
sourceMap: {},
tools: [],
},
}),
);
expect(result.resolved.enabledToolIds).not.toContain('lobe-local-system');
expect(result.toolDiscoveryConfig?.availableTools.map((t) => t.identifier)).toEqual([
'lobe-local-system',
]);
});
// Regression: `serverCallLlmContextBuilder` needs this to compute
// `creds_sandbox_reachable` — `sandbox_enabled` alone (whether the
// dedicated Cloud Sandbox tool is offered) doesn't tell it whether
// `runCommand`/`execScript` will actually land in that sandbox or on a
// routed device, so it must read the same single-track device gate the
// rest of the run executors use.
it('exposes the active device id when a device is routed for this run', () => {
const result = resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
buildState({
binding: { device: { id: 'device-1' } },
plan: { execution: { deviceId: 'device-1', kind: 'device', target: 'device' } },
}),
);
expect(result.activeDeviceId).toBe('device-1');
});
it('leaves the active device id undefined when no device is routed', () => {
const result = resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
buildState({ plan: { execution: { kind: 'sandbox', target: 'sandbox' } } }),
);
expect(result.activeDeviceId).toBeUndefined();
});
// The state stores the tool set once, on the operation slot. This is the exit:
// if the payload stops finding its tools here, the model loses them entirely.
it('resolves the tools from the operation slot alone', () => {
const result = resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
buildState({
operationToolSet: {
enabledToolIds: ['lobe-web-browsing'],
executorMap: {},
manifestMap: { 'lobe-web-browsing': webBrowsing },
sourceMap: { 'lobe-web-browsing': 'builtin' },
tools: [searchTool],
},
}),
);
expect(result.tools).toEqual([searchTool]);
expect(result.resolved.enabledToolIds).toEqual(['lobe-web-browsing']);
});
// An operation that started before the slot existed keeps its tool set in the
// legacy top-level mirrors; the load path lifts them and the payload is the same.
it('still resolves the tools of a pre-slot operation', () => {
const legacy = buildState({
toolExecutorMap: {},
toolManifestMap: { 'lobe-web-browsing': webBrowsing },
toolSourceMap: { 'lobe-web-browsing': 'builtin' },
tools: [searchTool],
});
expect(
resolveServerCallLlmTooling({ operationId: 'op-1', stepIndex: 0 }, legacy).tools,
).toEqual([searchTool]);
expect(
resolveServerCallLlmTooling(
{ operationId: 'op-1', stepIndex: 0 },
normalizeAgentState(legacy),
).tools,
).toEqual([searchTool]);
});
it('keeps toolDiscoveryConfig stable across mid-operation tool activations', () => {
const ctx = { operationId: 'op-1', stepIndex: 0 };
const state = buildState({
operationToolSet: {
enabledToolIds: [LobeActivatorManifest.identifier],
manifestMap: {
[LobeActivatorManifest.identifier]: LobeActivatorManifest,
[CredsIdentifier]: CredsManifest,
'lobe-web-browsing': webBrowsing,
},
sourceMap: {},
tools: [],
},
});
const before = resolveServerCallLlmTooling(ctx, state);
const after = resolveServerCallLlmTooling(
{ ...ctx, stepIndex: 1 },
{
...state,
activatedStepTools: [
{
activatedAtStep: 0,
id: 'lobe-web-browsing',
manifest: webBrowsing,
source: 'discovery',
},
],
},
);
expect(after.resolved.enabledToolIds).toContain('lobe-web-browsing');
expect(after.toolDiscoveryConfig).toEqual(before.toolDiscoveryConfig);
const forcedFinish = resolveServerCallLlmTooling(
{ ...ctx, stepIndex: 2 },
{
...state,
activatedStepTools: [
{
activatedAtStep: 0,
id: 'lobe-web-browsing',
manifest: webBrowsing,
source: 'discovery',
},
],
forceFinish: true,
},
);
expect(forcedFinish.toolDiscoveryConfig).toBeUndefined();
});
it('leaves the active device id undefined with no binding at all', () => {
const result = resolveServerCallLlmTooling({ operationId: 'op-1', stepIndex: 0 }, buildState());
expect(result.activeDeviceId).toBeUndefined();
});
it('keeps the shell of a deferred lobe-skills and restores its skill APIs once activated', () => {
const api = (name: string) => ({ description: name, name, parameters: {} });
const shellManifest = {
api: [api('runCommand'), api('exportFile')],
identifier: 'lobe-skills',
meta: { title: 'Skills' },
systemRole: 'shell only',
type: 'builtin' as const,
};
const fullManifest = {
...shellManifest,
api: [api('activateSkill'), ...shellManifest.api],
systemRole: 'full',
};
const ctx = { operationId: 'op-1', stepIndex: 0 };
const state = buildState({
operationToolSet: {
deferredManifestMap: { 'lobe-skills': fullManifest },
enabledToolIds: [LobeActivatorManifest.identifier, 'lobe-skills'],
manifestMap: {
[LobeActivatorManifest.identifier]: LobeActivatorManifest,
[CredsIdentifier]: CredsManifest,
'lobe-skills': shellManifest,
},
sourceMap: { 'lobe-skills': 'builtin' },
tools: [
{ function: { name: 'lobe-skills____runCommand' }, type: 'function' },
{ function: { name: 'lobe-skills____exportFile' }, type: 'function' },
] as unknown as NonNullable<AgentState['tools']>,
},
});
const before = resolveServerCallLlmTooling(ctx, state);
expect(before.tools?.map((tool) => tool.function.name)).toEqual([
'lobe-skills____runCommand',
'lobe-skills____exportFile',
]);
expect(before.resolved.promptManifestMap['lobe-skills']).toEqual(shellManifest);
// A deferred tool is promoted by the runtime, never offered for activation.
expect(before.toolDiscoveryConfig?.availableTools.map((tool) => tool.identifier)).toEqual([
CredsIdentifier,
]);
const after = resolveServerCallLlmTooling(
{ ...ctx, stepIndex: 1 },
{
...state,
activatedStepTools: [
{ activatedAtStep: 0, id: 'lobe-skills', manifest: fullManifest, source: 'discovery' },
],
},
);
expect(after.resolved.promptManifestMap['lobe-skills']).toEqual(fullManifest);
expect(after.tools?.map((tool) => tool.function.name)).toEqual([
'lobe-skills____runCommand',
'lobe-skills____exportFile',
'lobe-skills____activateSkill',
]);
});
});