Dashboard › opencode › Distillation
11b50c44-0e00-4a11-bfab-c7d9a5a39e34["lore_tm_v1_WgnuvhFO1LUFu-sQWQQeHWQqJelIfGKO3I-T2By4ddg","lore_tm_v1_Kcf8SDUOtN5Zx9KjFmD2oKX4FXjBH2FlHvT9h9IAXD8"]
/home/byk/Code/opencode-lore-v2/packages/core/src/fetch-interceptor.ts: “Never intercept requests already going to the gateway”; direct-gateway requests remain unchanged but their headers may be observed for in-process extension authentication.isLocalHost() recognizes localhost, 127.0.0.1, 0.0.0.0, ::1, and [::1], because local requests must “never be routed through the gateway.”rewriteRequest() “never reads the request body: known paths” can safely carry streaming bodies, while unknown paths “always pass through.”/home/byk/Code/opencode-lore-v2/packages/core/src/fetch-interceptor.ts adds exported types DynamicRequestHeaders = Readonly<Record<string, string>> and DynamicRequestHeadersSource = DynamicRequestHeaders | (() => DynamicRequestHeaders)./home/byk/Code/opencode-lore-v2/packages/core/src/fetch-interceptor.ts adds mayBeLLMRequest(url), which treats URLs containing /messages, /completions, or /responses as potentially requiring LLM routing or direct-gateway authentication observation./home/byk/Code/opencode-lore-v2/packages/core/src/fetch-interceptor.ts adds public async rewriteRequest(request, gatewayBase, dynamicHeaders, onRequestHeaders?), backed by rewriteRequestForProtocol(): it returns the original Request for non-LLM URLs, direct-gateway URLs, local hosts, and unknown/unrecognized paths; known LLM paths are rewritten via interceptUrl(), while an explicitly supplied detected BodyProtocol uses interceptUrlForProtocol().Request objects are created with new Request(rewrite.gatewayUrl, request), preserving method, body, authentication, content headers, query parameters, and streaming-body behavior without consuming the original body./home/byk/Code/opencode-lore-v2/packages/core/src/fetch-interceptor.ts was refactored from buildGatewayHeaders(input, init, ..., config) to mutating applyGatewayHeaders(headers, upstreamBase, upstreamPath, dynamicHeaders). It sets x-lore-upstream-url and x-lore-upstream-path, resolves dynamic headers from either a record or callback, preserves caller-provided headers rather than clobbering them, and logs fetch-interceptor: getHeaders() failed: if dynamic-header resolution throws.observeRequestHeaders() now accepts an optional callback directly rather than the full FetchInterceptorConfig; callback failures remain isolated and are logged as fetch-interceptor: onRequestHeaders() failed:.installFetchInterceptor() now shares mayBeLLMRequest(), isLocalHost(), and rewriteRequestForProtocol() with the public request primitive. It only calls extractBodyString() when the URL path does not match a known API path but pathLooksLLMLike() returns true; recognized body shapes route through a canonical protocol endpoint, while unrecognized LLM-looking paths bypass Lore and emit the existing once-per-host/path warning.originalFetch(input, init); an intercepted Request input is passed as originalFetch(routed); non-Request inputs use originalFetch(routed.url, { ...init, headers: routed.headers })./home/byk/Code/opencode-lore-v2/packages/core/test/fetch-interceptor-request.test.ts contains 7 test definitions, including one parameterized over 4 local URLs. Tests verify: (1) a V2-style OpenRouter request retains POST body, authorization, x-api-key, /api/v1/chat/completions, query ?stream=true&trace=abc, dynamic Lore headers, and header observation while routing to https://gateway.example.com/v1/chat/completions; (2) caller header x-lore-session-id: request-session overrides dynamic session-123; (3) direct-gateway requests remain reference-identical and are observed without injecting x-lore-project; (4) localhost, 127.0.0.1, 0.0.0.0, and [::1] requests remain unchanged; (5) a registry request remains unchanged; (6) a known Anthropic /v1/messages streaming body routes without being read; and (7) an unknown /v2/chat/completions?stream=true streaming body remains unchanged and unread./home/byk/Code/opencode-lore-v2/packages/opencode/package.json adds the ./server export with types: "./src/server.ts", bun: "./dist/server.js", and default: "./dist/server.js"; changes build from a no-op message to tsx script/build.ts; adds optional peer dependency @opencode/plugin: ">=0.0.0" with dev dependency version 0.0.0-beta-19378; and includes dist/ in published files while retaining the legacy raw-TypeScript root export./home/byk/Code/opencode-lore-v2/packages/opencode/script/build.ts:1-22 deletes and recreates dist, then uses esbuild.build() with entry src/server.ts, output dist/server.js, bundle: true, format: "esm", platform: "node", target: "esnext", sourcemap: true, externals ["@opencode/plugin", "@loreai/gateway"], and logLevel: "info"./home/byk/Code/opencode-lore-v2/packages/opencode/src/internal.ts adds parseUpstreamExtraHeaders(value), which splits on "\n", accepts the first colon as separator, trims names and values, ignores lines lacking a nonempty name, preserves empty values, and naturally strips CR from CRLF through trimming. /home/byk/Code/opencode-lore-v2/packages/opencode/src/index.ts now uses this helper instead of inline parsing before applying shouldForwardUpstreamExtraHeader()./home/byk/Code/opencode-lore-v2/packages/opencode/src/server-runtime.ts defines LoreServerRuntime, ServerRuntimeDependencies, reference-counted GatewayLease, acquireServerRuntime(), resetServerRuntimeForTest(), gateway discovery/startup, and buildServerHeaders().acquireServerRuntime(projectPath, dependencies) is inactive in tests or .test. processes unless LORE_OPENCODE_FORCE_ACTIVE === "1", and inactive when LORE_DISABLED is "1" or "true". Active runtimes share one gateway lease across project locations, resolve each project’s git remote separately, release the lease if git identity lookup throws, and expose an idempotent release()./home/byk/Code/opencode-lore-v2/packages/opencode/src/server-runtime.ts is: reachable LORE_REMOTE_URL after removing one trailing slash; reachable LORE_GATEWAY_URL; port from @loreai/gateway’s readPortFile() when available; then known loopback ports 3207 and 5673. An unreachable remote logs a fall-through message; if none resolve, startGateway({ quiet: true, local: true }) starts an in-process gateway at http://127.0.0.1:${handle.port}.refs, optional shutdown, and an in-progress closing promise. Owned gateways shut down only when the final location releases; a new acquisition waits for prior shutdown to finish before resolving a replacement; failed lease creation clears the cached promise.buildServerHeaders() constructs x-lore-session-id, x-lore-agent, x-lore-project, x-lore-git-remote, and x-lore-provider, merges gateway access headers, conditionally adds x-parent-session-id, maps provider IDs to LORE_UPSTREAM_${PROVIDER_ID_UPPERCASE_WITH_HYPHENS_REPLACED_BY_UNDERSCORES} for x-lore-upstream-url, and forwards parsed LORE_UPSTREAM_EXTRA_HEADERS only when allowed by shouldForwardUpstreamExtraHeader()./home/byk/Code/opencode-lore-v2/packages/opencode/src/server.ts exports a V2 Plugin.Plugin definition with id: "lore" and location-scoped setup(). It acquires runtime using ctx.location.project.directory, registers the 3 workers lore-distill, lore-curator, and lore-query-expand with their existing descriptions and explicit mode: "subagent" plus hidden: true, and leaves the legacy entrypoint unchanged.model.request hook looks up the session to obtain optional parentID, builds attribution/routing headers from sessionID, agent, and event.model.providerID, merges them into event.headers, and changes event.baseURL to ${runtime.gatewayBase}/v1 only when x-lore-upstream-url is present.http.request hook replaces event.request with await rewriteRequest(event.request, runtime.gatewayBase, {}).Promise.allSettled() before rethrowing the original setup error; returned cleanup is idempotent, disposes all registrations and releases runtime concurrently, and throws AggregateError(failures, "Lore plugin cleanup failed") if any cleanup operation rejects./home/byk/Code/opencode-lore-v2/packages/opencode/test/internal.test.ts adds a regression test proving parseUpstreamExtraHeaders("x-empty:\r\nx-team: platform\r\ninvalid") returns exactly [["x-empty", ""], ["x-team", "platform"]]./home/byk/Code/opencode-lore-v2/packages/opencode/test/package.test.ts builds and packs @loreai/opencode, extracts the tarball, verifies the exact ./server export map, verifies dist/server.js contains neither interface LoreServerRuntime, import type , nor @opencode/plugin, and imports the packed server entrypoint without a host SDK to obtain { id: "lore", setup: "function" }./home/byk/Code/opencode-lore-v2/packages/opencode/test/server-runtime.test.ts verifies: LORE_DISABLED values "1" and "true" suppress activation; two locations share one gateway and only the final release shuts it down; git-remote resolution failure releases the gateway; reacquisition waits for previous owned-gateway shutdown and then changes from port 3207 to 5673; and attribution headers include project/session/parent/agent/provider/upstream/team data while excluding authorization, x-api-key, and forged x-lore-forged./home/byk/Code/opencode-lore-v2/packages/opencode/test/server.test.ts verifies the V2 definition while requiring the legacy module to retain exactly LorePlugin and default; exact hidden-subagent worker registrations; model.request and http.request hooks; use of project root /workspace/project rather than nested directory; preservation of provider authorization; parent-session omission when lookup fails; disposal/release after setup failure; independence of 2 location scopes; 3 registration disposals during normal cleanup; and idempotent cleanup with each disposer and runtime release called exactly once.