mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 23:24:00 +03:00
fix(core): present the client certificate on every model request of the provider
This commit is contained in:
@@ -33,7 +33,10 @@ export const layer: Layer.Layer<Service, never, RequestExecutor.Service> = Layer
|
||||
return Service.of({
|
||||
generate: Effect.fnUntraced(function* (request) {
|
||||
const route = request.model.route
|
||||
const response = yield* route.generate(request, executor.execute)
|
||||
const response = yield* route.generate(
|
||||
request,
|
||||
RequestExecutor.withTls(executor.execute, request.model.http?.tls),
|
||||
)
|
||||
if (answersMatch(request.questions, response.answers))
|
||||
return response as DecisionResponseFor<typeof request.questions>
|
||||
return yield* new AIError({
|
||||
|
||||
@@ -31,6 +31,7 @@ export const make = <Self, Req extends RoutedRequest<Req, Event, Response>, Even
|
||||
service,
|
||||
Effect.gen(function* () {
|
||||
const executor = yield* RequestExecutor.Service
|
||||
const execute = (model: Req["model"]) => RequestExecutor.withTls(executor.execute, model.http?.tls)
|
||||
const notQueued = (route: MediaRoute.AnyRoute<Req, Event, Response>, operation: string) =>
|
||||
new AIError({
|
||||
reason: new UnsupportedOperationError({
|
||||
@@ -43,27 +44,29 @@ export const make = <Self, Req extends RoutedRequest<Req, Event, Response>, Even
|
||||
const start = (request: Req) => {
|
||||
const route = request.model.route
|
||||
if (route.kind !== "queued") return Effect.fail(notQueued(route, "start"))
|
||||
return route.start(request, executor.execute)
|
||||
return route.start(request, execute(request.model))
|
||||
}
|
||||
return service.of({
|
||||
start,
|
||||
resume: (model, token) => {
|
||||
if (model.route.kind !== "queued") return Effect.fail(notQueued(model.route, "resume"))
|
||||
return model.route.resume(model, token, executor.execute)
|
||||
return model.route.resume(model, token, execute(model))
|
||||
},
|
||||
generate: (request, options) => {
|
||||
const route = request.model.route
|
||||
if (route.kind !== "queued") return route.generate(request, executor.execute)
|
||||
if (route.kind !== "queued") return route.generate(request, execute(request.model))
|
||||
return start(request).pipe(Effect.flatMap((generation) => generation.await(options)))
|
||||
},
|
||||
stream: (request, options) => {
|
||||
const route = request.model.route
|
||||
if (route.kind === "stream") return route.stream(request, executor.execute)
|
||||
if (route.kind === "stream") return route.stream(request, execute(request.model))
|
||||
if (route.kind === "queued")
|
||||
return Stream.unwrap(
|
||||
start(request).pipe(Effect.map((generation) => resultEvents(generation, input.responseEvents, options))),
|
||||
)
|
||||
return Stream.fromIterableEffect(Effect.map(route.generate(request, executor.execute), input.responseEvents))
|
||||
return Stream.fromIterableEffect(
|
||||
Effect.map(route.generate(request, execute(request.model)), input.responseEvents),
|
||||
)
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
@@ -608,7 +608,8 @@ const streamRequestWith = (runtime: TransportRuntime) => (request: LLMRequest, o
|
||||
Stream.unwrap(
|
||||
Effect.gen(function* () {
|
||||
const compiled = yield* compile(request, options)
|
||||
return compiled.route.streamPrepared(compiled.prepared, compiled.request, runtime, options)
|
||||
const http = { execute: RequestExecutor.withTls(runtime.http.execute, compiled.request.http?.tls) }
|
||||
return compiled.route.streamPrepared(compiled.prepared, compiled.request, { http }, options)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -670,10 +671,12 @@ export const layer: Layer.Layer<Service, never, RequestExecutor.Service> = Layer
|
||||
): Effect.Effect<CompactionResponse, AIError>
|
||||
function compact(request: LLMRequest, options?: EndpointCompactOptions | TriggerCompactOptions) {
|
||||
return Effect.suspend((): Effect.Effect<CompactionResponse | CompactionCheckpointResponse, AIError> => {
|
||||
const prepared = prepareRequest(request)
|
||||
const http = { execute: RequestExecutor.withTls(executor.execute, prepared.http?.tls) }
|
||||
if (options?.mechanism === "trigger" && canCompact(request, options))
|
||||
return request.model.route.compact.trigger(prepareRequest(request), executor, options)
|
||||
return request.model.route.compact.trigger(prepared, http, options)
|
||||
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
||||
return request.model.route.compact.endpoint(prepareRequest(request), executor, options)
|
||||
return request.model.route.compact.endpoint(prepared, http, options)
|
||||
return unsupportedCompaction(request.model, options?.mechanism)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,7 +9,15 @@ import {
|
||||
} from "effect/http"
|
||||
import { HttpContext, HttpRateLimitDetails, AIError, TransportError } from "../schema/index.js"
|
||||
import { classifyProviderFailure, providerErrorMessage } from "../provider-error.js"
|
||||
import { headerDetails, responseHttp, Service, type HttpMiddleware, type Interface } from "./executor-service.js"
|
||||
import {
|
||||
CurrentTls,
|
||||
headerDetails,
|
||||
responseHttp,
|
||||
Service,
|
||||
type HttpMiddleware,
|
||||
type Interface,
|
||||
} from "./executor-service.js"
|
||||
import type { ClientTls } from "../schema/options.js"
|
||||
|
||||
export { CurrentTls, responseHttp, Service } from "./executor-service.js"
|
||||
export type { HttpHandler, HttpMiddleware, Interface, RequestExecutorService } from "./executor-service.js"
|
||||
@@ -253,6 +261,10 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient> = Layer.e
|
||||
|
||||
export const fetchLayer = layer.pipe(Layer.provide(FetchHttpClient.layer))
|
||||
|
||||
/** Sends through `execute`, presenting `tls` on each request. The executor's HTTP client reads it from `CurrentTls`. */
|
||||
export const withTls = (execute: Interface["execute"], tls: ClientTls | undefined): Interface["execute"] =>
|
||||
tls ? (request, middleware) => execute(request, middleware).pipe(Effect.provideService(CurrentTls, tls)) : execute
|
||||
|
||||
/**
|
||||
* Per-call middleware that sends through `fetch` instead of the executor's HTTP client. Executor middleware and
|
||||
* `middleware` still wrap the call, and status classification still applies.
|
||||
|
||||
@@ -110,7 +110,6 @@ export const httpJson = <Body, Frame>(input: HttpJsonInput<Body, Frame>): HttpJs
|
||||
// Unlike the header and chunk limits, the whole-request budget has no default.
|
||||
const total = limits.total ? Duration.millis(limits.total) : Duration.infinity
|
||||
const response = yield* runtime.http.execute(prepared.request, prepared.middleware).pipe(
|
||||
Effect.provideService(RequestExecutor.CurrentTls, request.http?.tls),
|
||||
Effect.timeoutOrElse({
|
||||
duration: Duration.min(timeoutDuration(limits.headers), total),
|
||||
orElse: () => timeout("request", "Timed out waiting for response headers"),
|
||||
|
||||
@@ -313,7 +313,8 @@ const fromCatalogMedia = Effect.fn("ModelResolver.fromCatalogMedia")(function* <
|
||||
credential?: Credential.Value,
|
||||
dependencies?: Dependencies,
|
||||
) {
|
||||
const { resolved, module, settings } = yield* loadCatalogPackage(model, credential, dependencies)
|
||||
const { resolved, configured, module, settings } = yield* loadCatalogPackage(model, credential, dependencies)
|
||||
const tls = Provider.tls(configured)
|
||||
const runtime: unknown = yield* Effect.try({
|
||||
try: () => module.model(resolved.modelID ?? resolved.id, settings),
|
||||
catch: (cause) => constructionError(resolved, cause),
|
||||
@@ -328,7 +329,12 @@ const fromCatalogMedia = Effect.fn("ModelResolver.fromCatalogMedia")(function* <
|
||||
})
|
||||
: runtime
|
||||
if (!is(adapted)) return yield* unsupported(resolved)
|
||||
return adapted
|
||||
if (!tls || !(adapted instanceof DecisionModel || adapted instanceof ImageModel)) return adapted
|
||||
// Media and decision models are plain classes with readonly fields, so copy onto the same prototype.
|
||||
const copy: Runtime = Object.assign(Object.create(Object.getPrototypeOf(adapted)), adapted, {
|
||||
http: { ...adapted.http, tls },
|
||||
})
|
||||
return copy
|
||||
})
|
||||
|
||||
const constructionError = (model: RuntimeInfo, cause: unknown) =>
|
||||
@@ -502,7 +508,8 @@ export const layer = Layer.effect(
|
||||
limit: selected.limit,
|
||||
compaction: runtimeInfo.settings?.compaction,
|
||||
cache: runtimeInfo.settings?.cache,
|
||||
transport: provider?.settings?.transport,
|
||||
// The WebSocket transport can't present a client certificate, so `tls` keeps the model on HTTP.
|
||||
transport: Provider.tls(provider?.settings ?? {}) ? "http" : provider?.settings?.transport,
|
||||
chunkTimeout: Provider.timeout(provider?.settings?.chunkTimeout),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -119,7 +119,9 @@ TLS handshake only sends it to servers that ask for one.
|
||||
- `passphrase` is only needed for an encrypted key.
|
||||
- Requests with a client certificate don't follow redirects, so the certificate isn't presented to another host.
|
||||
|
||||
Only model requests present the certificate. Tools such as webfetch, remote MCP servers and plugin installs don't.
|
||||
Every model request of that provider presents the certificate: chat, compaction, image and decision models. A provider
|
||||
with `tls` uses HTTP instead of WebSockets. Tools such as webfetch, remote MCP servers and plugin installs don't present
|
||||
it.
|
||||
|
||||
## Foreground
|
||||
|
||||
|
||||
Reference in New Issue
Block a user