GeezerVison is showing github.com — test: migrate prompt tests to HTTP mock LLM server (#20304) · Rwanbt/unifia@d3dc1f9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/bus/index.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,8 +90,9 @@ export namespace Bus { | 90 | 90 | if (ps) yield* PubSub.publish(ps, payload) | 91 | 91 | yield* PubSub.publish(state.wildcard, payload) | 92 | 92 |
| | 93 | + const dir = yield* InstanceState.directory | 93 | 94 | GlobalBus.emit("event", { | 94 | | - directory: Instance.directory, | | 95 | + directory: dir, | 95 | 96 | payload, | 96 | 97 | }) | 97 | 98 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1486,7 +1486,8 @@ export namespace Config { | 1486 | 1486 | }) | 1487 | 1487 |
| 1488 | 1488 | const update = Effect.fn("Config.update")(function* (config: Info) { | 1489 | | - const file = path.join(Instance.directory, "config.json") | | 1489 | + const dir = yield* InstanceState.directory | | 1490 | + const file = path.join(dir, "config.json") | 1490 | 1491 | const existing = yield* loadFile(file) | 1491 | 1492 | yield* fs.writeFileString(file, JSON.stringify(mergeDeep(existing, config), null, 2)).pipe(Effect.orDie) | 1492 | 1493 | yield* Effect.promise(() => Instance.dispose()) | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { ServiceMap } from "effect" | | 2 | +import type { InstanceContext } from "@/project/instance" | | 3 | + | | 4 | +export const InstanceRef = ServiceMap.Reference<InstanceContext | undefined>("~opencode/InstanceRef", { | | 5 | + defaultValue: () => undefined, | | 6 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/effect/instance-state.ts +40-5Lines changed: 40 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { Effect, ScopedCache, Scope } from "effect" | | 1 | +import { Effect, Fiber, ScopedCache, Scope, ServiceMap } from "effect" | 2 | 2 | import { Instance, type InstanceContext } from "@/project/instance" | | 3 | +import { Context } from "@/util/context" | | 4 | +import { InstanceRef } from "./instance-ref" | 3 | 5 | import { registerDisposer } from "./instance-registry" | 4 | 6 |
| 5 | 7 | const TypeId = "~opencode/InstanceState" | @@ -10,13 +12,34 @@ export interface InstanceState<A, E = never, R = never> { | 10 | 12 | } | 11 | 13 |
| 12 | 14 | export namespace InstanceState { | | 15 | + export const bind = <F extends (...args: any[]) => any>(fn: F): F => { | | 16 | + try { | | 17 | + return Instance.bind(fn) | | 18 | + } catch (err) { | | 19 | + if (!(err instanceof Context.NotFound)) throw err | | 20 | + } | | 21 | + const fiber = Fiber.getCurrent() | | 22 | + const ctx = fiber ? ServiceMap.getReferenceUnsafe(fiber.services, InstanceRef) : undefined | | 23 | + if (!ctx) return fn | | 24 | + return ((...args: any[]) => Instance.restore(ctx, () => fn(...args))) as F | | 25 | + } | | 26 | + | | 27 | + export const context = Effect.fnUntraced(function* () { | | 28 | + return (yield* InstanceRef) ?? Instance.current | | 29 | + })() | | 30 | + | | 31 | + export const directory = Effect.map(context, (ctx) => ctx.directory) | | 32 | + | 13 | 33 | export const make = <A, E = never, R = never>( | 14 | 34 | init: (ctx: InstanceContext) => Effect.Effect<A, E, R | Scope.Scope>, | 15 | 35 | ): Effect.Effect<InstanceState<A, E, Exclude<R, Scope.Scope>>, never, R | Scope.Scope> => | 16 | 36 | Effect.gen(function* () { | 17 | 37 | const cache = yield* ScopedCache.make<string, A, E, R>({ | 18 | 38 | capacity: Number.POSITIVE_INFINITY, | 19 | | - lookup: () => init(Instance.current), | | 39 | + lookup: () => | | 40 | + Effect.fnUntraced(function* () { | | 41 | + return yield* init(yield* context) | | 42 | + })(), | 20 | 43 | }) | 21 | 44 |
| 22 | 45 | const off = registerDisposer((directory) => Effect.runPromise(ScopedCache.invalidate(cache, directory))) | @@ -29,7 +52,9 @@ export namespace InstanceState { | 29 | 52 | }) | 30 | 53 |
| 31 | 54 | export const get = <A, E, R>(self: InstanceState<A, E, R>) => | 32 | | - Effect.suspend(() => ScopedCache.get(self.cache, Instance.directory)) | | 55 | + Effect.gen(function* () { | | 56 | + return yield* ScopedCache.get(self.cache, yield* directory) | | 57 | + }) | 33 | 58 |
| 34 | 59 | export const use = <A, E, R, B>(self: InstanceState<A, E, R>, select: (value: A) => B) => | 35 | 60 | Effect.map(get(self), select) | @@ -40,8 +65,18 @@ export namespace InstanceState { | 40 | 65 | ) => Effect.flatMap(get(self), select) | 41 | 66 |
| 42 | 67 | export const has = <A, E, R>(self: InstanceState<A, E, R>) => | 43 | | - Effect.suspend(() => ScopedCache.has(self.cache, Instance.directory)) | | 68 | + Effect.gen(function* () { | | 69 | + return yield* ScopedCache.has(self.cache, yield* directory) | | 70 | + }) | 44 | 71 |
| 45 | 72 | export const invalidate = <A, E, R>(self: InstanceState<A, E, R>) => | 46 | | - Effect.suspend(() => ScopedCache.invalidate(self.cache, Instance.directory)) | | 73 | + Effect.gen(function* () { | | 74 | + return yield* ScopedCache.invalidate(self.cache, yield* directory) | | 75 | + }) | | 76 | + | | 77 | + /** | | 78 | + * Effect finalizers run on the fiber scheduler after the original async | | 79 | + * boundary, so ALS reads like Instance.directory can be gone by then. | | 80 | + */ | | 81 | + export const withALS = <T>(fn: () => T) => Effect.map(context, (ctx) => Instance.restore(ctx, fn)) | 47 | 82 | } |
Collapse file Copy file name to clipboard +19-5Lines changed: 19 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Effect, Layer, ManagedRuntime } from "effect" | 2 | 2 | import * as ServiceMap from "effect/ServiceMap" | | 3 | +import { Instance } from "@/project/instance" | | 4 | +import { Context } from "@/util/context" | | 5 | +import { InstanceRef } from "./instance-ref" | 3 | 6 |
| 4 | 7 | export const memoMap = Layer.makeMemoMapUnsafe() | 5 | 8 |
| | 9 | +function attach<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> { | | 10 | + try { | | 11 | + const ctx = Instance.current | | 12 | + return Effect.provideService(effect, InstanceRef, ctx) | | 13 | + } catch (err) { | | 14 | + if (!(err instanceof Context.NotFound)) throw err | | 15 | + } | | 16 | + return effect | | 17 | +} | | 18 | + | 6 | 19 | export function makeRuntime<I, S, E>(service: ServiceMap.Service<I, S>, layer: Layer.Layer<I, E>) { | 7 | 20 | let rt: ManagedRuntime.ManagedRuntime<I, E> | undefined | 8 | 21 | const getRuntime = () => (rt ??= ManagedRuntime.make(layer, { memoMap })) | 9 | 22 |
| 10 | 23 | return { | 11 | | - runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(service.use(fn)), | | 24 | + runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(attach(service.use(fn))), | 12 | 25 | runPromiseExit: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) => | 13 | | - getRuntime().runPromiseExit(service.use(fn), options), | | 26 | + getRuntime().runPromiseExit(attach(service.use(fn)), options), | 14 | 27 | runPromise: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) => | 15 | | - getRuntime().runPromise(service.use(fn), options), | 16 | | - runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(service.use(fn)), | 17 | | - runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runCallback(service.use(fn)), | | 28 | + getRuntime().runPromise(attach(service.use(fn)), options), | | 29 | + runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(attach(service.use(fn))), | | 30 | + runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => | | 31 | + getRuntime().runCallback(attach(service.use(fn))), | 18 | 32 | } | 19 | 33 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/format/index.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -108,10 +108,11 @@ export namespace Format { | 108 | 108 | for (const item of yield* Effect.promise(() => getFormatter(ext))) { | 109 | 109 | log.info("running", { command: item.command }) | 110 | 110 | const cmd = item.command.map((x) => x.replace("$FILE", filepath)) | | 111 | + const dir = yield* InstanceState.directory | 111 | 112 | const code = yield* spawner | 112 | 113 | .spawn( | 113 | 114 | ChildProcess.make(cmd[0]!, cmd.slice(1), { | 114 | | - cwd: Instance.directory, | | 115 | + cwd: dir, | 115 | 116 | env: item.environment, | 116 | 117 | extendEnv: true, | 117 | 118 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/installation/index.ts +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,11 +9,7 @@ import z from "zod" | 9 | 9 | import { BusEvent } from "@/bus/bus-event" | 10 | 10 | import { Flag } from "../flag/flag" | 11 | 11 | import { Log } from "../util/log" | 12 | | - | 13 | | -declare global { | 14 | | - const OPENCODE_VERSION: string | 15 | | - const OPENCODE_CHANNEL: string | 16 | | -} | | 12 | +import { CHANNEL as channel, VERSION as version } from "./meta" | 17 | 13 |
| 18 | 14 | import semver from "semver" | 19 | 15 |
| @@ -60,8 +56,8 @@ export namespace Installation { | 60 | 56 | }) | 61 | 57 | export type Info = z.infer<typeof Info> | 62 | 58 |
| 63 | | - export const VERSION = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "local" | 64 | | - export const CHANNEL = typeof OPENCODE_CHANNEL === "string" ? OPENCODE_CHANNEL : "local" | | 59 | + export const VERSION = version | | 60 | + export const CHANNEL = channel | 65 | 61 | export const USER_AGENT = `opencode/${CHANNEL}/${VERSION}/${Flag.OPENCODE_CLIENT}` | 66 | 62 |
| 67 | 63 | export function isPreview() { | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +declare global { | | 2 | + const OPENCODE_VERSION: string | | 3 | + const OPENCODE_CHANNEL: string | | 4 | +} | | 5 | + | | 6 | +export const VERSION = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "local" | | 7 | +export const CHANNEL = typeof OPENCODE_CHANNEL === "string" ? OPENCODE_CHANNEL : "local" |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/project/instance.ts +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,14 @@ export const Instance = { | 114 | 114 | const ctx = context.use() | 115 | 115 | return ((...args: any[]) => context.provide(ctx, () => fn(...args))) as F | 116 | 116 | }, | | 117 | + /** | | 118 | + * Run a synchronous function within the given instance context ALS. | | 119 | + * Use this to bridge from Effect (where InstanceRef carries context) | | 120 | + * back to sync code that reads Instance.directory from ALS. | | 121 | + */ | | 122 | + restore<R>(ctx: InstanceContext, fn: () => R): R { | | 123 | + return context.provide(ctx, fn) | | 124 | + }, | 117 | 125 | state<S>(init: () => S, dispose?: (state: Awaited<S>) => Promise<void>): () => S { | 118 | 126 | return State.create(() => Instance.directory, init, dispose) | 119 | 127 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/compaction.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ import { NotFoundError } from "@/storage/db" | 17 | 17 | import { ModelID, ProviderID } from "@/provider/schema" | 18 | 18 | import { Effect, Layer, ServiceMap } from "effect" | 19 | 19 | import { makeRuntime } from "@/effect/run-service" | | 20 | +import { InstanceState } from "@/effect/instance-state" | 20 | 21 | import { isOverflow as overflow } from "./overflow" | 21 | 22 |
| 22 | 23 | export namespace SessionCompaction { | @@ -213,6 +214,7 @@ When constructing the summary, try to stick to this template: | 213 | 214 | const msgs = structuredClone(messages) | 214 | 215 | yield* plugin.trigger("experimental.chat.messages.transform", {}, { messages: msgs }) | 215 | 216 | const modelMessages = yield* Effect.promise(() => MessageV2.toModelMessages(msgs, model, { stripMedia: true })) | | 217 | + const ctx = yield* InstanceState.context | 216 | 218 | const msg: MessageV2.Assistant = { | 217 | 219 | id: MessageID.ascending(), | 218 | 220 | role: "assistant", | @@ -223,8 +225,8 @@ When constructing the summary, try to stick to this template: | 223 | 225 | variant: userMessage.variant, | 224 | 226 | summary: true, | 225 | 227 | path: { | 226 | | - cwd: Instance.directory, | 227 | | - root: Instance.worktree, | | 228 | + cwd: ctx.directory, | | 229 | + root: ctx.worktree, | 228 | 230 | }, | 229 | 231 | cost: 0, | 230 | 232 | tokens: { | |
|