镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions packages/provider-acp-registry/src/server/driver.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { AcpRegistryOperationError, ProviderInstanceId } from "@t3tools/contracts";
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
import { AcpRegistrySettings } from "../settings.ts";
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";

import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts";
import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
import {
AcpRegistryDriver,
applyAcpRegistryAvailableCommands,
applyAcpRegistryLiveConfiguration,
buildCheckedAcpRegistrySnapshot,
checkAcpRegistryProviderReadiness,
checkAcpRegistryProviderStatus,
} from "./driver.ts";
import { normalizeAcpRegistryLiveConfiguration } from "./probe.ts";

const decodeSettings = Schema.decodeSync(AcpRegistrySettings);
const identity = {
Expand Down Expand Up @@ -432,3 +446,112 @@ describe("acpRegistrySnapshotReadiness", () => {
}),
);
});

const fakeAgentSource = `
import { readFileSync } from "node:fs";
import { createInterface } from "node:readline";
const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
createInterface({ input: process.stdin }).on("line", (line) => {
const { id, method } = JSON.parse(line);
if (id === undefined) return;
if (method === "initialize") {
send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } });
} else if (method === "session/new") {
const modelIds = JSON.parse(readFileSync(process.argv[2], "utf8"));
send({
id,
result: {
sessionId: "probe-session",
models: {
currentModelId: modelIds[0],
availableModels: modelIds.map((modelId) => ({ modelId, name: modelId })),
},
},
});
send({
method: "session/update",
params: {
sessionId: "probe-session",
update: { sessionUpdate: "available_commands_update", availableCommands: [] },
},
});
} else {
send({ id, result: {} });
}
});
`;

const driverLayer = TestProviderHost.layer({ runBackgroundWork: false }).pipe(
Layer.provideMerge(AcpRegistryRuntimeCoordinator.layer),
Layer.provideMerge(IdAllocator.layer),
Layer.provideMerge(McpProviderSessions.layer),
Layer.provideMerge(
Layer.succeed(
ProviderEventLoggers.ProviderEventLoggers,
ProviderEventLoggers.NoOpProviderEventLoggers,
),
),
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
);

it.layer(driverLayer, { excludeTestServices: true })("AcpRegistryDriver", (it) => {
it.effect("drops cached and session-reported models when its caches are invalidated", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-driver-" });
const agentPath = path.join(directory, "agent.mjs");
const modelsPath = path.join(directory, "models.json");
yield* fileSystem.writeFileString(agentPath, fakeAgentSource);
yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model"]));
const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;

const instance = yield* AcpRegistryDriver.create({
instanceId: identity.instanceId,
displayName: identity.displayName,
enabled: true,
environment: [],
config: decodeSettings({ agentId: "test-agent" }),
}).pipe(
Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, {
...catalogWithInspection({
status: "ready",
agentId: "test-agent",
version: "1.0.0",
distribution: "npx",
}),
resolve: () =>
Effect.succeed({
distribution: "npx" as const,
spawn: { command: process.execPath, args: [agentPath, modelsPath] },
}),
}),
);
const modelSlugs = instance.snapshot.streamChanges.pipe(
Stream.map((snapshot) => snapshot.models.map((model) => model.slug)),
);
const waitForModels = (expected: ReadonlyArray<string>) =>
modelSlugs.pipe(
Stream.filter((slugs) => slugs.join() === expected.join()),
Stream.runHead,
);

const probed = yield* waitForModels(["old-model"]).pipe(Effect.forkScoped);
yield* instance.snapshot.refresh;
yield* Fiber.join(probed);
yield* coordinator.publishLiveConfiguration(
identity.instanceId,
normalizeAcpRegistryLiveConfiguration([], undefined, {
currentModelId: "old-model",
availableModels: [{ modelId: "old-model", name: "old-model" }],
}),
);
yield* fileSystem.writeFileString(modelsPath, JSON.stringify(["old-model", "new-model"]));

const refreshed = yield* waitForModels(["old-model", "new-model"]).pipe(Effect.forkScoped);
yield* instance.invalidateCaches ?? Effect.void;
yield* instance.snapshot.refresh;
expect(Option.isSome(yield* Fiber.join(refreshed))).toBe(true);
}),
);
});
4 changes: 4 additions & 0 deletions packages/provider-acp-registry/src/server/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ export const AcpRegistryDriver: ProviderDriver<AcpRegistrySettings, AcpRegistryD
...snapshot,
refresh: snapshot.refresh.pipe(Effect.tap(() => controller.refreshMethods ?? Effect.void)),
},
invalidateCaches: (Option.isSome(runtimeCoordinator)
? runtimeCoordinator.value.clearLiveConfiguration(instanceId)
: Effect.void
).pipe(Effect.andThen(liveSnapshotSemaphore.withPermit(invalidateEnrichmentCache))),
orchestrationAdapter,
textGeneration: makeUnsupportedTextGeneration(),
acpSessionManagement: {
Expand Down
Loading