Skip to content
Myr

memory.ts

This file

Path

packages/drafting/src/drafter/memory.ts

41 lines · 1.3 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d

lines 1–41why, one step back
1import { Context, Effect, Layer, Ref, Schema } from "effect";
23import { DraftFailed } from "../drafted.ts";4import type { DraftedAsk, DraftInput } from "../drafted.ts";5import { Drafter } from "../drafter.ts";67const isFailure = Schema.is(DraftFailed);89export class DraftsAsked extends Context.Service<10 DraftsAsked,11 { readonly inputs: Effect.Effect<readonly DraftInput[]> }12>()("drafting/DraftsAsked") {}1314export const DrafterMemory = (15 script: readonly (DraftedAsk | DraftFailed)[]16): Layer.Layer<Drafter | DraftsAsked> =>17 Layer.effectContext(18 Effect.gen(function* () {19 const left = yield* Ref.make(script);20 const asked = yield* Ref.make<readonly DraftInput[]>([]);2122 const draft = (input: DraftInput) =>23 Effect.gen(function* () {24 yield* Ref.update(asked, (all) => [...all, input]);2526 const [next, ...rest] = yield* Ref.get(left);2728 if (next === undefined) {29 return yield* Effect.die("the memory drafter's script ran out");30 }3132 yield* Ref.set(left, rest);3334 return isFailure(next) ? yield* next : next;35 });3637 return Context.make(Drafter, Drafter.of({ draft })).pipe(38 Context.add(DraftsAsked, DraftsAsked.of({ inputs: Ref.get(asked) }))39 );40 })41 );