MCPcopy Index your code
hub / github.com/anomalyco/opencode / withContext

Function withContext

packages/opencode/test/server/httpapi-exercise/runner.ts:62–198  ·  view source on GitHub ↗
(
  options: Options,
  scenario: ActiveScenario,
  label: string,
  use: (ctx: SeededContext<unknown>) => Effect.Effect<A, E>,
)

Source from the content-addressed store, hash-verified

60}
61
62function withContext<A, E>(
63 options: Options,
64 scenario: ActiveScenario,
65 label: string,
66 use: (ctx: SeededContext<unknown>) => Effect.Effect<A, E>,
67) {
68 return Effect.acquireRelease(
69 Effect.gen(function* () {
70 yield* trace(options, scenario, `${label} context acquire start`)
71 const llm = scenario.project?.llm ? yield* TestLLMServer : undefined
72 const project = scenario.project
73 const dir = project
74 ? yield* Effect.promise(async () => (await runtime()).tmpdir(projectOptions(project, llm?.url)))
75 : undefined
76 yield* trace(options, scenario, `${label} context acquire done`)
77 return { dir, llm }
78 }),
79 (ctx) =>
80 Effect.gen(function* () {
81 yield* trace(options, scenario, `${label} tmpdir cleanup start`)
82 yield* Effect.promise(async () => {
83 await ctx.dir?.[Symbol.asyncDispose]()
84 }).pipe(Effect.ignore)
85 yield* trace(options, scenario, `${label} tmpdir cleanup done`)
86 }),
87 ).pipe(
88 Effect.flatMap((context) =>
89 Effect.gen(function* () {
90 yield* trace(options, scenario, `${label} runtime start`)
91 const modules = yield* Effect.promise(() => runtime())
92 const scope = yield* Scope.Scope
93 const app = yield* Layer.buildWithMemoMap(modules.AppLayer, modules.memoMap, scope)
94 yield* trace(options, scenario, `${label} runtime done`)
95 const path = context.dir?.path
96 const instance = path
97 ? yield* trace(options, scenario, `${label} instance load start`).pipe(
98 Effect.andThen(
99 modules.InstanceStore.Service.use((store) => store.load({ directory: path })).pipe(
100 Effect.provide(app),
101 Effect.catchCause((cause) =>
102 Effect.sleep("100 millis").pipe(
103 Effect.andThen(
104 modules.InstanceStore.Service.use((store) => store.load({ directory: path })).pipe(
105 Effect.provide(app),
106 ),
107 ),
108 Effect.catchCause(() => Effect.failCause(cause)),
109 ),
110 ),
111 ),
112 ),
113 Effect.tap(() => trace(options, scenario, `${label} instance load done`)),
114 )
115 : undefined
116 const run = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
117 effect.pipe(Effect.provideService(modules.InstanceRef, instance), Effect.provide(app))
118 const directory = () => {
119 if (!context.dir?.path) throw new Error("scenario needs a project directory")

Callers 1

runActiveFunction · 0.85

Calls 15

runtimeFunction · 0.90
projectOptionsFunction · 0.85
writeMethod · 0.80
syncMethod · 0.80
traceFunction · 0.70
directoryFunction · 0.70
runFunction · 0.70
llmFunction · 0.70
createMethod · 0.65
getMethod · 0.65
updateMethod · 0.65
removeMethod · 0.65

Tested by

no test coverage detected