GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/opencode/src/session/instruction.ts at dev · ngbrown/opencode Expand file tree/ Copy pathinstruction.tsMore file actions 237 lines (208 loc) · 8.38 KB / Copy pathinstruction.tsFile metadata and controls237 lines (208 loc) · 8.38 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { httpClient } from "@opencode-ai/core/effect/app-node-platform" import path from "path" import { SessionV1 } from "@opencode-ai/core/v1/session" import { Effect, Layer, Context } from "effect" import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http" import { Config } from "@/config/config" import { InstanceState } from "@/effect/instance-state" import { RuntimeFlags } from "@/effect/runtime-flags" import { Flag } from "@opencode-ai/core/flag/flag" import { FSUtil } from "@opencode-ai/core/fs-util" import { withTransientReadRetry } from "@/util/effect-http-client" import { Global } from "@opencode-ai/core/global" import type { MessageV2 } from "./message-v2" import type { MessageID } from "./schema" function extract(messages: SessionV1.WithParts[]) { const paths = new Set<string>() for (const msg of messages) { for (const part of msg.parts) { if (part.type === "tool" && part.tool === "read" && part.state.status === "completed") { if (part.state.time.compacted) continue const loaded = part.state.metadata?.loaded if (!loaded || !Array.isArray(loaded)) continue for (const p of loaded) { if (typeof p === "string") paths.add(p) } } } } return paths } export interface Interface { readonly clear: (messageID: MessageID) => Effect.Effect<void> readonly systemPaths: () => Effect.Effect<Set<string>, FSUtil.Error> readonly system: () => Effect.Effect<string[], FSUtil.Error> readonly find: (dir: string) => Effect.Effect<string | undefined, FSUtil.Error> readonly resolve: ( messages: SessionV1.WithParts[], filepath: string, messageID: MessageID, ) => Effect.Effect<{ filepath: string; content: string }[], FSUtil.Error> } export class Service extends Context.Service<Service, Interface>()("@opencode/Instruction") {} const layer: Layer.Layer< Service, never, FSUtil.Service | Config.Service | Global.Service | HttpClient.HttpClient | RuntimeFlags.Service > = Layer.effect( Service, Effect.gen(function* () { const cfg = yield* Config.Service const fs = yield* FSUtil.Service const global = yield* Global.Service const flags = yield* RuntimeFlags.Service const http = HttpClient.filterStatusOk(withTransientReadRetry(yield* HttpClient.HttpClient)) const globalFiles = [ path.join(global.config, "AGENTS.md"), ...(!flags.disableClaudeCodePrompt ? [path.join(global.home, ".claude", "CLAUDE.md")] : []), ] const instructionFiles = [ "AGENTS.md", ...(!flags.disableClaudeCodePrompt ? ["CLAUDE.md"] : []), "CONTEXT.md", // deprecated ] const state = yield* InstanceState.make( Effect.fn("Instruction.state")(() => Effect.succeed({ // Track which instruction files have already been attached for a given assistant message. claims: new Map<MessageID, Set<string>>(), }), ), ) const relative = Effect.fnUntraced(function* (instruction: string) { const ctx = yield* InstanceState.context if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { return yield* fs .globUp(instruction, ctx.directory, ctx.worktree) .pipe(Effect.catch(() => Effect.succeed([] as string[]))) } return yield* fs .globUp(instruction, global.config, global.config) .pipe(Effect.catch(() => Effect.succeed([] as string[]))) }) const read = Effect.fnUntraced(function* (filepath: string) { return yield* fs.readFileString(filepath).pipe(Effect.catch(() => Effect.succeed(""))) }) const fetch = Effect.fnUntraced(function* (url: string) { const res = yield* http.execute(HttpClientRequest.get(url)).pipe( Effect.timeout(5000), Effect.catch(() => Effect.succeed(null)), ) if (!res) return "" const body = yield* res.arrayBuffer.pipe(Effect.catch(() => Effect.succeed(new ArrayBuffer(0)))) return new TextDecoder().decode(body) }) const clear = Effect.fn("Instruction.clear")(function* (messageID: MessageID) { const s = yield* InstanceState.get(state) s.claims.delete(messageID) }) const systemPaths = Effect.fn("Instruction.systemPaths")(function* () { const config = yield* cfg.get() const ctx = yield* InstanceState.context const paths = new Set<string>() for (const file of globalFiles) { if (yield* fs.existsSafe(file)) { paths.add(path.resolve(file)) break } } // The first project-level match wins so we don't stack AGENTS.md/CLAUDE.md from every ancestor. if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { for (const file of instructionFiles) { const matches = yield* fs .findUp(file, ctx.directory, ctx.worktree) .pipe(Effect.catch(() => Effect.succeed([]))) if (matches.length > 0) { matches.forEach((item) => paths.add(path.resolve(item))) break } } } if (config.instructions) { for (const raw of config.instructions) { if (raw.startsWith("https://") || raw.startsWith("http://")) continue const instruction = raw.startsWith("~/") ? path.join(global.home, raw.slice(2)) : raw const matches = yield* ( path.isAbsolute(instruction) ? fs.glob(path.basename(instruction), { cwd: path.dirname(instruction), absolute: true, include: "file", }) : relative(instruction) ).pipe(Effect.catch(() => Effect.succeed([] as string[]))) matches.forEach((item) => paths.add(path.resolve(item))) } } return paths }) const system = Effect.fn("Instruction.system")(function* () { const config = yield* cfg.get() const paths = yield* systemPaths() const urls = (config.instructions ?? []).filter( (item) => item.startsWith("https://") || item.startsWith("http://"), ) const files = yield* Effect.forEach(Array.from(paths), read, { concurrency: 8 }) const remote = yield* Effect.forEach(urls, fetch, { concurrency: 4 }) return [ ...Array.from(paths).flatMap((item, i) => (files[i] ? [`Instructions from: ${item}\n${files[i]}`] : [])), ...urls.flatMap((item, i) => (remote[i] ? [`Instructions from: ${item}\n${remote[i]}`] : [])), ] }) const find = Effect.fn("Instruction.find")(function* (dir: string) { for (const file of instructionFiles) { const filepath = path.resolve(path.join(dir, file)) if (yield* fs.existsSafe(filepath)) return filepath } return undefined }) const resolve = Effect.fn("Instruction.resolve")(function* ( messages: SessionV1.WithParts[], filepath: string, messageID: MessageID, ) { const sys = yield* systemPaths() const already = extract(messages) const results: { filepath: string; content: string }[] = [] const s = yield* InstanceState.get(state) const root = path.resolve(yield* InstanceState.directory) const target = path.resolve(filepath) let current = path.dirname(target) // Walk upward from the file being read and attach nearby instruction files once per message. while (current.startsWith(root) && current !== root) { const found = yield* find(current) if (!found || found === target || sys.has(found) || already.has(found)) { current = path.dirname(current) continue } let set = s.claims.get(messageID) if (!set) { set = new Set() s.claims.set(messageID, set) } if (set.has(found)) { current = path.dirname(current) continue } set.add(found) const content = yield* read(found) if (content) { results.push({ filepath: found, content: `Instructions from: ${found}\n${content}` }) } current = path.dirname(current) } return results }) return Service.of({ clear, systemPaths, system, find, resolve }) }), ) export function loaded(messages: SessionV1.WithParts[]) { return extract(messages) } export const node = LayerNode.make({ service: Service, layer: layer, deps: [Config.node, FSUtil.node, Global.node, RuntimeFlags.node, httpClient], }) export * as Instruction from "./instruction" |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |