@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 72 lines 2.47 KB
Raw
1 import * as Provider from "alchemy/Provider";
2 import type { ResourceClass, ResourceLike } from "alchemy/Resource";
3 import * as Effect from "effect/Effect";
4 import * as Redacted from "effect/Redacted";
5
6 /**
7 * Shared helpers for the Prisma providers' local (`alchemy dev`) variants.
8 * Local variants fabricate deterministic `dev:`-prefixed identifiers and
9 * never talk to the Prisma Management API.
10 */
11
12 export type DevRecord = Record<string, unknown>;
13
14 export const DEV_TIMESTAMP = "1970-01-01T00:00:00.000Z";
15
16 export const devId = (type: string, id: string) => `dev:${type}:${id}`;
17
18 export const isRecord = (value: unknown): value is DevRecord =>
19 typeof value === "object" && value !== null;
20
21 const attr = (value: unknown, key: string) => (isRecord(value) ? value[key] : undefined);
22
23 export const attrOrString = (value: unknown, attrName: string) =>
24 typeof value === "string"
25 ? value
26 : isRecord(value) && typeof value[attrName] === "string"
27 ? value[attrName]
28 : undefined;
29
30 export const attrOrNullableString = (value: unknown, key: string) => {
31 const candidate = attr(value, key);
32 return candidate === null || typeof candidate === "string" ? candidate : undefined;
33 };
34
35 export const attrOrRedactedString = (value: unknown, key: string) => {
36 const candidate = attr(value, key);
37 return Redacted.isRedacted(candidate)
38 ? Redacted.make(String(Redacted.value(candidate)))
39 : typeof candidate === "string"
40 ? Redacted.make(candidate)
41 : undefined;
42 };
43
44 /**
45 * Build a stateless local provider stub: reconcile merges the previous
46 * outputs with freshly fabricated attributes, read echoes persisted state,
47 * and delete is a no-op.
48 */
49 export const devProvider = <R extends ResourceLike>(
50 resource: ResourceClass<R>,
51 stables: Extract<keyof R["Attributes"], string>[],
52 attrs: (input: { id: string; fqn: string; news: DevRecord; output?: DevRecord }) => DevRecord,
53 ) =>
54 Provider.succeed(resource, {
55 stables,
56 list: () => Effect.succeed([]),
57 diff: Effect.fn(function* () {
58 return { action: "update" } as const;
59 }),
60 read: Effect.fn(function* ({ output }) {
61 return output;
62 }),
63 reconcile: Effect.fn(function* ({ id, fqn, news, output }) {
64 const newsRecord = isRecord(news) ? news : {};
65 const outputRecord = isRecord(output) ? output : undefined;
66 return {
67 ...outputRecord,
68 ...attrs({ id, fqn, news: newsRecord, output: outputRecord }),
69 } as R["Attributes"];
70 }),
71 delete: Effect.fn(function* () {}),
72 });