|
| 1 | +import { Context, Effect, FileSystem, Layer, PlatformError, Ref } from "effect" |
| 2 | +import * as path from "node:path" |
| 3 | +import { cassetteSecretFindings, type SecretFinding } from "./redaction" |
| 4 | +import type { Cassette, CassetteMetadata, Interaction } from "./schema" |
| 5 | +import { cassetteFor, cassettePath, DEFAULT_RECORDINGS_DIR, formatCassette, parseCassette } from "./storage" |
| 6 | + |
| 7 | +export interface Entry { |
| 8 | + readonly name: string |
| 9 | + readonly path: string |
| 10 | +} |
| 11 | + |
| 12 | +export interface Interface { |
| 13 | + readonly path: (name: string) => string |
| 14 | + readonly read: (name: string) => Effect.Effect<Cassette, PlatformError.PlatformError> |
| 15 | + readonly write: (name: string, cassette: Cassette) => Effect.Effect<void, PlatformError.PlatformError> |
| 16 | + readonly append: ( |
| 17 | + name: string, |
| 18 | + interaction: Interaction, |
| 19 | + metadata: CassetteMetadata | undefined, |
| 20 | + ) => Effect.Effect< |
| 21 | + { |
| 22 | + readonly cassette: Cassette |
| 23 | + readonly findings: ReadonlyArray<SecretFinding> |
| 24 | + }, |
| 25 | + PlatformError.PlatformError |
| 26 | + > |
| 27 | + readonly exists: (name: string) => Effect.Effect<boolean> |
| 28 | + readonly list: () => Effect.Effect<ReadonlyArray<Entry>, PlatformError.PlatformError> |
| 29 | + readonly scan: (cassette: Cassette) => ReadonlyArray<SecretFinding> |
| 30 | +} |
| 31 | + |
| 32 | +export class Service extends Context.Service<Service, Interface>()("@opencode-ai/http-recorder/Cassette") {} |
| 33 | + |
| 34 | +export const layer = (options: { readonly directory?: string } = {}) => |
| 35 | + Layer.effect( |
| 36 | + Service, |
| 37 | + Effect.gen(function* () { |
| 38 | + const fileSystem = yield* FileSystem.FileSystem |
| 39 | + const directory = options.directory ?? DEFAULT_RECORDINGS_DIR |
| 40 | + const recorded = yield* Ref.make(new Map<string, ReadonlyArray<Interaction>>()) |
| 41 | + |
| 42 | + const pathFor = (name: string) => cassettePath(name, directory) |
| 43 | + |
| 44 | + const walk = (directory: string): Effect.Effect<ReadonlyArray<string>, PlatformError.PlatformError> => |
| 45 | + Effect.gen(function* () { |
| 46 | + const entries = yield* fileSystem |
| 47 | + .readDirectory(directory) |
| 48 | + .pipe(Effect.catch(() => Effect.succeed([] as string[]))) |
| 49 | + const nested = yield* Effect.forEach(entries, (entry) => { |
| 50 | + const full = path.join(directory, entry) |
| 51 | + return fileSystem.stat(full).pipe( |
| 52 | + Effect.flatMap((stat) => (stat.type === "Directory" ? walk(full) : Effect.succeed([full]))), |
| 53 | + Effect.catch(() => Effect.succeed([] as string[])), |
| 54 | + ) |
| 55 | + }) |
| 56 | + return nested.flat() |
| 57 | + }) |
| 58 | + |
| 59 | + const read = Effect.fn("Cassette.read")(function* (name: string) { |
| 60 | + return parseCassette(yield* fileSystem.readFileString(pathFor(name))) |
| 61 | + }) |
| 62 | + |
| 63 | + const write = Effect.fn("Cassette.write")(function* (name: string, cassette: Cassette) { |
| 64 | + yield* fileSystem.makeDirectory(path.dirname(pathFor(name)), { recursive: true }) |
| 65 | + yield* fileSystem.writeFileString(pathFor(name), formatCassette(cassette)) |
| 66 | + }) |
| 67 | + |
| 68 | + const append = Effect.fn("Cassette.append")(function* ( |
| 69 | + name: string, |
| 70 | + interaction: Interaction, |
| 71 | + metadata: CassetteMetadata | undefined, |
| 72 | + ) { |
| 73 | + const interactions = yield* Ref.updateAndGet(recorded, (previous) => |
| 74 | + new Map(previous).set(name, [...(previous.get(name) ?? []), interaction]), |
| 75 | + ) |
| 76 | + const cassette = cassetteFor(name, interactions.get(name) ?? [], metadata) |
| 77 | + const findings = cassetteSecretFindings(cassette) |
| 78 | + if (findings.length === 0) yield* write(name, cassette) |
| 79 | + return { cassette, findings } |
| 80 | + }) |
| 81 | + |
| 82 | + const exists = Effect.fn("Cassette.exists")(function* (name: string) { |
| 83 | + return yield* fileSystem.access(pathFor(name)).pipe( |
| 84 | + Effect.as(true), |
| 85 | + Effect.catch(() => Effect.succeed(false)), |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + const list = Effect.fn("Cassette.list")(function* () { |
| 90 | + return (yield* walk(directory)) |
| 91 | + .filter((file) => file.endsWith(".json")) |
| 92 | + .map((file) => ({ |
| 93 | + name: path.relative(directory, file).replace(/\\/g, "/").replace(/\.json$/, ""), |
| 94 | + path: file, |
| 95 | + })) |
| 96 | + .toSorted((a, b) => a.name.localeCompare(b.name)) |
| 97 | + }) |
| 98 | + |
| 99 | + return Service.of({ path: pathFor, read, write, append, exists, list, scan: cassetteSecretFindings }) |
| 100 | + }), |
| 101 | + ) |
| 102 | + |
| 103 | +export const defaultLayer = layer() |
| 104 | + |
| 105 | +export * as Cassette from "./cassette" |
0 commit comments