refactor(opencode): migrate llm tests to layer nodes (#34479)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
|
||||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import { FSUtil } from "@opencode-ai/core/fs-util"
|
||||
import { ModelsDev } from "@opencode-ai/core/models-dev"
|
||||
import { HttpRecorder } from "@opencode-ai/http-recorder"
|
||||
import { HttpRecorderInternal } from "@opencode-ai/http-recorder/internal"
|
||||
@@ -10,14 +9,11 @@ import { Effect, Layer, Option, Schema, Stream } from "effect"
|
||||
import path from "node:path"
|
||||
import z from "zod"
|
||||
import { Auth } from "@/auth"
|
||||
import { Config } from "@/config/config"
|
||||
import { Plugin } from "@/plugin"
|
||||
import { Provider } from "@/provider/provider"
|
||||
|
||||
import { Filesystem } from "@/util/filesystem"
|
||||
import { LLMEvent, LLMResponse } from "@opencode-ai/llm"
|
||||
import { LLMClient, RequestExecutor, WebSocketExecutor } from "@opencode-ai/llm/route"
|
||||
import { Env } from "@/env"
|
||||
import { RequestExecutor } from "@opencode-ai/llm/route"
|
||||
import { RuntimeFlags } from "@/effect/runtime-flags"
|
||||
import type { Agent } from "../../src/agent/agent"
|
||||
import { LLM } from "../../src/session/llm"
|
||||
@@ -26,6 +22,9 @@ import { TestInstance } from "../fixture/fixture"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
||||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
import { LayerNodePlatform } from "@opencode-ai/core/effect/app-node-platform"
|
||||
|
||||
const FIXTURES_DIR = path.join(import.meta.dir, "../fixtures/recordings")
|
||||
|
||||
@@ -240,7 +239,7 @@ const redactRecordedBody = (body: string) =>
|
||||
|
||||
function authLayer(scenario: RecordedScenario) {
|
||||
const replayAuth = shouldRecord ? scenario.recordAuth?.() : scenario.replayAuth
|
||||
if (!replayAuth) return Auth.defaultLayer
|
||||
if (!replayAuth) return undefined
|
||||
return Layer.mock(Auth.Service)({
|
||||
get: (providerID) => Effect.succeed(providerID === scenario.providerID ? replayAuth : undefined),
|
||||
all: () => Effect.succeed({ [scenario.providerID]: replayAuth }),
|
||||
@@ -262,15 +261,6 @@ const modelsFixture = Filesystem.readJson<Record<string, ModelsDev.Provider>>(
|
||||
|
||||
function recordedNativeLLMLayer(scenario: RecordedScenario) {
|
||||
const auth = authLayer(scenario)
|
||||
const provider = Provider.layer.pipe(
|
||||
Layer.provide(FSUtil.defaultLayer),
|
||||
Layer.provide(Env.defaultLayer),
|
||||
Layer.provide(Config.defaultLayer),
|
||||
Layer.provide(auth),
|
||||
Layer.provide(Plugin.defaultLayer),
|
||||
Layer.provide(ModelsDev.defaultLayer),
|
||||
Layer.provide(RuntimeFlags.defaultLayer),
|
||||
)
|
||||
// Only the HTTP client is recorded; RequestExecutor and the opencode LLM stack remain real.
|
||||
const metadata = {
|
||||
provider: scenario.providerID,
|
||||
@@ -290,21 +280,11 @@ function recordedNativeLLMLayer(scenario: RecordedScenario) {
|
||||
redactor: HttpRecorderInternal.Redactor.make(redact),
|
||||
})
|
||||
: HttpRecorder.http(scenario.cassette, { directory: FIXTURES_DIR, metadata, redact })
|
||||
const recordedClient = LLMClient.layer.pipe(
|
||||
Layer.provide(Layer.mergeAll(RequestExecutor.layer.pipe(Layer.provide(recordedHttp)), WebSocketExecutor.layer)),
|
||||
)
|
||||
|
||||
return Layer.mergeAll(
|
||||
provider,
|
||||
LLM.layer.pipe(
|
||||
Layer.provide(auth),
|
||||
Layer.provide(Config.defaultLayer),
|
||||
Layer.provide(provider),
|
||||
Layer.provide(Plugin.defaultLayer),
|
||||
Layer.provide(recordedClient),
|
||||
Layer.provide(RuntimeFlags.layer({ experimentalNativeLlm: true })),
|
||||
),
|
||||
)
|
||||
return AppNodeBuilder.build(LayerNode.group([Provider.node, LLM.node]), [
|
||||
[LayerNodePlatform.requestExecutor, RequestExecutor.layer.pipe(Layer.provide(recordedHttp))],
|
||||
[RuntimeFlags.node, RuntimeFlags.layer({ experimentalNativeLlm: true })],
|
||||
...(auth ? ([[Auth.node, auth]] as const) : []),
|
||||
])
|
||||
}
|
||||
|
||||
const writeConfig = (directory: string, scenario: RecordedScenario, model: ModelsDev.Provider["models"][string]) =>
|
||||
|
||||
Reference in New Issue
Block a user