Commits merged: - docs(FN-3717): update openclaw runtime and settings docs - test(FN-3717): fix openclaw engine test typing - test(FN-3717): strengthen openclaw bridge coverage - feat(FN-3717): complete Step 2 — wire engine and bundle delivery - fix(FN-3717): address openclaw bridge review feedback - feat(FN-3717): complete Step 1 — add OpenClaw MCP bridge Files changed: .changeset/fn-3717-openclaw-tool-bridge.md | 5 ++ docs/settings-reference.md | 28 +++++--- packages/cli/src/__tests__/bundle-output.test.ts | 9 +++ packages/cli/tsup.config.ts | 7 ++ .../src/__tests__/openclaw-runtime-e2e.test.ts | 28 ++++++++ .../__tests__/openclaw-runtime-integration.test.ts | 10 +++ plugins/fusion-plugin-openclaw-runtime/README.md | 13 ++++ .../src/__tests__/cli-spawn.test.ts | 82 +++++++++++++++++++--- .../src/__tests__/mcp-config.test.ts | 45 ++++++++++++ .../src/__tests__/runtime-adapter.test.ts | 46 ++++++++++++ .../fusion-plugin-openclaw-runtime/src/index.ts | 5 ++ .../src/mcp-config.ts | 60 ++++++++++++++++ .../src/mcp-schema-server.cjs | 59 ++++++++++++++++ .../src/pi-module.ts | 52 ++++++++++++-- .../src/runtime-adapter.ts | 26 +++++++ .../fusion-plugin-openclaw-runtime/src/types.ts | 4 ++ 16 files changed, 457 insertions(+), 22 deletions(-) Fusion-Task-Id: FN-3717
203 lines
6.4 KiB
TypeScript
203 lines
6.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import type { AgentRuntime } from "../agent-runtime.js";
|
|
import { resolveRuntime } from "../runtime-resolution.js";
|
|
import { createResolvedAgentSession } from "../agent-session-helpers.js";
|
|
import type { PluginRunner } from "../plugin-runner.js";
|
|
import type { PluginRuntimeRegistration } from "@fusion/core";
|
|
|
|
const mockCreateFnAgent = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("../logger.js", () => ({
|
|
createLogger: vi.fn(() => ({
|
|
log: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
})),
|
|
}));
|
|
|
|
vi.mock("../pi.js", () => ({
|
|
createFnAgent: mockCreateFnAgent,
|
|
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
|
describeModel: vi.fn().mockReturnValue("pi/default"),
|
|
}));
|
|
|
|
function isAgentRuntime(value: unknown): value is AgentRuntime {
|
|
return (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
"id" in value &&
|
|
"name" in value &&
|
|
typeof (value as AgentRuntime).createSession === "function" &&
|
|
typeof (value as AgentRuntime).promptWithFallback === "function" &&
|
|
typeof (value as AgentRuntime).describeModel === "function"
|
|
);
|
|
}
|
|
|
|
function createMockPluginRunner(overrides: Partial<PluginRunner> = {}): PluginRunner {
|
|
return {
|
|
getPluginRuntimes: vi.fn().mockReturnValue([]),
|
|
getRuntimeById: vi.fn().mockReturnValue(undefined),
|
|
createRuntimeContext: vi.fn().mockResolvedValue({
|
|
pluginId: "fusion-plugin-openclaw-runtime",
|
|
taskStore: {},
|
|
settings: {},
|
|
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
|
emitEvent: vi.fn(),
|
|
}),
|
|
...overrides,
|
|
} as unknown as PluginRunner;
|
|
}
|
|
|
|
function createOpenClawRegistration(factoryImpl?: () => unknown): {
|
|
pluginId: string;
|
|
runtime: PluginRuntimeRegistration;
|
|
} {
|
|
return {
|
|
pluginId: "fusion-plugin-openclaw-runtime",
|
|
runtime: {
|
|
metadata: {
|
|
runtimeId: "openclaw",
|
|
name: "OpenClaw Runtime",
|
|
description: "OpenClaw-backed AI session using the local OpenClaw gateway",
|
|
version: "0.1.0",
|
|
},
|
|
factory: vi.fn().mockImplementation(async () =>
|
|
factoryImpl
|
|
? factoryImpl()
|
|
: {
|
|
id: "openclaw",
|
|
name: "OpenClaw Runtime",
|
|
createSession: vi.fn().mockResolvedValue({
|
|
session: { runtime: "openclaw", prompt: vi.fn() },
|
|
sessionFile: "/tmp/openclaw.session.json",
|
|
}),
|
|
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
|
describeModel: vi.fn().mockReturnValue("openclaw/main"),
|
|
},
|
|
),
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("OpenClaw runtime integration via engine resolution pipeline", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockCreateFnAgent.mockResolvedValue({
|
|
session: { runtime: "pi", prompt: vi.fn() },
|
|
sessionFile: "/tmp/pi.session.json",
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("resolves OpenClaw runtime through PluginRunner lookup when runtimeHint is openclaw", async () => {
|
|
const registration = createOpenClawRegistration();
|
|
const pluginRunner = createMockPluginRunner({
|
|
getRuntimeById: vi.fn().mockReturnValue(registration),
|
|
});
|
|
|
|
const resolved = await resolveRuntime({
|
|
sessionPurpose: "executor",
|
|
runtimeHint: "openclaw",
|
|
pluginRunner,
|
|
});
|
|
|
|
expect(resolved.runtimeId).toBe("openclaw");
|
|
expect(resolved.wasConfigured).toBe(true);
|
|
expect(resolved.runtime.id).toBe("openclaw");
|
|
expect(resolved.runtime.name).toBe("OpenClaw Runtime");
|
|
expect(pluginRunner.getRuntimeById).toHaveBeenCalledWith("openclaw");
|
|
expect(pluginRunner.createRuntimeContext).toHaveBeenCalledWith("fusion-plugin-openclaw-runtime");
|
|
});
|
|
|
|
it("returns a runtime object that conforms to AgentRuntime", async () => {
|
|
const registration = createOpenClawRegistration();
|
|
const pluginRunner = createMockPluginRunner({
|
|
getRuntimeById: vi.fn().mockReturnValue(registration),
|
|
});
|
|
|
|
const resolved = await resolveRuntime({
|
|
sessionPurpose: "executor",
|
|
runtimeHint: "openclaw",
|
|
pluginRunner,
|
|
});
|
|
|
|
expect(isAgentRuntime(resolved.runtime)).toBe(true);
|
|
});
|
|
|
|
it("createResolvedAgentSession uses OpenClaw runtime and reports configured runtime metadata", async () => {
|
|
const runtimeSession = { runtime: "openclaw", prompt: vi.fn() };
|
|
const createSession = vi.fn().mockResolvedValue({
|
|
session: runtimeSession,
|
|
sessionFile: "/tmp/openclaw.session.json",
|
|
});
|
|
const registration = createOpenClawRegistration(() => ({
|
|
id: "openclaw",
|
|
name: "OpenClaw Runtime",
|
|
createSession,
|
|
promptWithFallback: vi.fn().mockResolvedValue(undefined),
|
|
describeModel: vi.fn().mockReturnValue("openclaw/main"),
|
|
}));
|
|
|
|
const pluginRunner = createMockPluginRunner({
|
|
getRuntimeById: vi.fn().mockReturnValue(registration),
|
|
});
|
|
|
|
const customTool = {
|
|
name: "fn_task_show",
|
|
label: "fn_task_show",
|
|
description: "show",
|
|
parameters: { type: "object" },
|
|
execute: vi.fn(),
|
|
} as any;
|
|
|
|
const result = await createResolvedAgentSession({
|
|
sessionPurpose: "executor",
|
|
runtimeHint: "openclaw",
|
|
pluginRunner,
|
|
cwd: "/tmp/project",
|
|
systemPrompt: "You are helpful",
|
|
tools: "coding",
|
|
customTools: [customTool],
|
|
});
|
|
|
|
expect(result.runtimeId).toBe("openclaw");
|
|
expect(result.wasConfigured).toBe(true);
|
|
expect(result.session).toBe(runtimeSession);
|
|
expect(result.sessionFile).toBe("/tmp/openclaw.session.json");
|
|
expect(createSession).toHaveBeenCalledWith({
|
|
cwd: "/tmp/project",
|
|
systemPrompt: "You are helpful",
|
|
tools: "coding",
|
|
customTools: [customTool],
|
|
});
|
|
});
|
|
|
|
it("falls back to default pi runtime when OpenClaw factory throws", async () => {
|
|
const registration = createOpenClawRegistration(() => {
|
|
throw new Error("factory exploded");
|
|
});
|
|
|
|
const pluginRunner = createMockPluginRunner({
|
|
getRuntimeById: vi.fn().mockReturnValue(registration),
|
|
});
|
|
|
|
const result = await createResolvedAgentSession({
|
|
sessionPurpose: "executor",
|
|
runtimeHint: "openclaw",
|
|
pluginRunner,
|
|
cwd: "/tmp/project",
|
|
systemPrompt: "Use fallback",
|
|
});
|
|
|
|
expect(result.runtimeId).toBe("pi");
|
|
expect(result.wasConfigured).toBe(false);
|
|
expect(mockCreateFnAgent).toHaveBeenCalledWith({
|
|
cwd: "/tmp/project",
|
|
systemPrompt: "Use fallback",
|
|
});
|
|
});
|
|
});
|