@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 137 lines 5.17 KB
Raw
1 import { RuntimeContext } from "alchemy/RuntimeContext";
2 import * as Arr from "effect/Array";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import {
6 BackingPersistence,
7 PersistenceError,
8 type BackingPersistenceStore,
9 } from "effect/persistence/Persistence";
10 import { DurableObjectState } from "./DurableObjectState.ts";
11
12 /**
13 * A `BackingPersistence` layer (Effect AI persistence module) backed
14 * by the surrounding Durable Object's `state.storage`. Drop-in storage
15 * for `Persistence.layerResultPersisted({ storeId })` so chat history,
16 * cached AI responses, or any other persisted state lives in the DO
17 * SQLite store with `${storeId}:` key namespacing.
18 *
19 * Multiple `storeId`s can coexist within a single Durable Object —
20 * keys are namespaced with a `${storeId}:` prefix so they don't
21 * collide.
22 *
23 * :::caution
24 * TTL is currently ignored; Durable Object storage has no native TTL.
25 * If you need expiry, layer a TTL-aware backing store on top, or run
26 * a periodic `clear`.
27 * :::
28 *
29 *
30 * ### Wiring it into a chat-backing DO
31 * **Example:** Persisted chat history per DO instance
32 * `Persistence.layerResultPersisted({ storeId })` is the seam Effect
33 * AI exposes for cached/replayable AI calls. Layer
34 * `DurableObjectChatPersistence` underneath and every entry is stored
35 * in the DO's `state.storage` under the `alchemy.chat:` prefix.
36 * ```typescript
37 * import * as Cloudflare from "@alchemy.run/cloudflare";
38 * import * as Effect from "effect/Effect";
39 * import * as Layer from "effect/Layer";
40 * import { Chat, LanguageModel } from "effect/ai";
41 * import { Persistence } from "effect/persistence";
42 *
43 * export default class ChatBackend extends Cloudflare.DurableObject<ChatBackend>()(
44 * "ChatBackend",
45 * Effect.gen(function* () {
46 * return Effect.gen(function* () {
47 * const persistence = yield* Persistence.layerResultPersisted({
48 * storeId: "alchemy.chat",
49 * }).pipe(Layer.provide(Cloudflare.AI.DurableObjectChatPersistence));
50 *
51 * return {
52 * send: (threadId: string, prompt: string) =>
53 * LanguageModel.generateText({ prompt }).pipe(Effect.provide(persistence)),
54 * };
55 * });
56 * }),
57 * ) {}
58 * ```
59 *
60 * ### Multiple stores in the same DO
61 * **Example:** Separate `storeId`s coexist
62 * Different `storeId`s namespace their keys with `${storeId}:`, so
63 * one DO can keep, say, chat history *and* an audit log in separate
64 * stores without colliding.
65 * ```typescript
66 * const aiPersistence = yield* Persistence.layerResultPersisted({
67 * storeId: "alchemy.chat",
68 * }).pipe(Layer.provide(Cloudflare.AI.DurableObjectChatPersistence));
69 *
70 * const auditPersistence = yield* Persistence.layerResultPersisted({
71 * storeId: "alchemy.audit",
72 * }).pipe(Layer.provide(Cloudflare.AI.DurableObjectChatPersistence));
73 * ```
74 *
75 * @binding
76 * @product Workers
77 * @category Workers & Compute
78 */
79 export const DurableObjectChatPersistence = Layer.effect(BackingPersistence)(
80 Effect.gen(function* () {
81 const state = yield* DurableObjectState;
82 const storage = state.storage;
83
84 const wrapErr = (op: string, key?: string) => (cause: unknown) =>
85 PersistenceError.make({
86 message: `Failed to ${op}${key !== undefined ? ` key ${key}` : ""} in DurableObject storage`,
87 cause,
88 });
89
90 return BackingPersistence.of({
91 make: (storeId) =>
92 Effect.sync(() => {
93 const prefixed = (k: string) => `${storeId}:${k}`;
94 return {
95 get: (key) =>
96 storage
97 .get<object>(prefixed(key))
98 .pipe(Effect.mapError(wrapErr("get", key)), Effect.provide(RuntimeContext.phantom)),
99 getMany: (keys) =>
100 storage.get<object>(keys.map(prefixed)).pipe(
101 Effect.mapError(wrapErr("getMany")),
102 Effect.map(
103 (map) =>
104 keys.map((k) => map.get(prefixed(k))) as Arr.NonEmptyArray<object | undefined>,
105 ),
106 Effect.provide(RuntimeContext.phantom),
107 ),
108 set: (key, value, _ttl) =>
109 storage
110 .put(prefixed(key), value)
111 .pipe(Effect.mapError(wrapErr("set", key)), Effect.provide(RuntimeContext.phantom)),
112 setMany: (entries) =>
113 storage
114 .put(Object.fromEntries(entries.map(([k, v]) => [prefixed(k), v])))
115 .pipe(Effect.mapError(wrapErr("setMany")), Effect.provide(RuntimeContext.phantom)),
116 remove: (key) =>
117 storage
118 .delete(prefixed(key))
119 .pipe(
120 Effect.asVoid,
121 Effect.mapError(wrapErr("remove", key)),
122 Effect.provide(RuntimeContext.phantom),
123 ),
124 clear: storage.list({ prefix: `${storeId}:` }).pipe(
125 Effect.flatMap((map) => {
126 const ks = [...map.keys()];
127 if (ks.length === 0) return Effect.void;
128 return Effect.asVoid(storage.delete(ks));
129 }),
130 Effect.mapError(wrapErr("clear")),
131 Effect.provide(RuntimeContext.phantom),
132 ),
133 } satisfies BackingPersistenceStore;
134 }),
135 });
136 }),
137 );