@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, 03:37 UTC

main
ts 87 lines 3.49 KB
Raw
1 import { DEFAULT_SERVER_SETTINGS, type ServerSettings } from "@t3tools/contracts";
2 import {
3 noCompatibilityPolicy,
4 type ProviderCompatibilityLookup,
5 } from "@t3tools/provider-core/server/compatibility";
6 import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
7 import * as Context from "effect/Context";
8 import * as Effect from "effect/Effect";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Layer from "effect/Layer";
11 import * as Option from "effect/Option";
12 import * as Path from "effect/Path";
13 import * as Ref from "effect/Ref";
14 import * as Stream from "effect/Stream";
15
16 export interface TestProviderHostOptions {
17 readonly cwd?: string;
18 readonly settings?: ServerSettings;
19 readonly runBackgroundWork?: boolean;
20 readonly compatibility?: ProviderCompatibilityLookup;
21 }
22
23 export class TestProviderHostSettings extends Context.Service<
24 TestProviderHostSettings,
25 { readonly set: (settings: ServerSettings) => Effect.Effect<void> }
26 >()("@t3tools/provider-testing/TestProviderHost/TestProviderHostSettings") {}
27
28 export const layer = (
29 options: TestProviderHostOptions = {},
30 ): Layer.Layer<
31 ProviderHost.ProviderHost | TestProviderHostSettings,
32 never,
33 FileSystem.FileSystem | Path.Path
34 > =>
35 Layer.effectContext(
36 Effect.gen(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38 const path = yield* Path.Path;
39 const baseDir = yield* fileSystem
40 .makeTempDirectoryScoped({ prefix: "t3-provider-host-" })
41 .pipe(Effect.orDie);
42 const stateDir = path.join(baseDir, "userdata");
43 const providerStatusCacheDir = path.join(baseDir, "caches");
44 const attachmentsDir = path.join(stateDir, "attachments");
45 for (const directory of [stateDir, providerStatusCacheDir, attachmentsDir]) {
46 yield* fileSystem.makeDirectory(directory, { recursive: true }).pipe(Effect.orDie);
47 }
48 const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS);
49 const credentials = new Map<string, Uint8Array>();
50 const host = ProviderHost.ProviderHost.of({
51 compatibility: options.compatibility ?? noCompatibilityPolicy,
52 paths: {
53 cwd: options.cwd ?? process.cwd(),
54 baseDir,
55 stateDir,
56 providerStatusCacheDir,
57 attachmentsDir,
58 },
59 settings: {
60 get: Ref.get(settings),
61 withSnapshot: (use) => Ref.get(settings).pipe(Effect.flatMap(use)),
62 changes: Stream.empty,
63 subscribe: Effect.succeed(Stream.empty),
64 },
65 shouldRunBackgroundWork: Effect.fn("ProviderHost.shouldRunBackgroundWork")(function () {
66 return Effect.succeed(options.runBackgroundWork ?? true);
67 }),
68 resolveAttachmentPath: (attachment) => path.join(attachmentsDir, attachment.id),
69 credentials: Effect.fn("ProviderHost.credentials")(function (namespace, bindingId) {
70 return Effect.sync(() => {
71 const key = `${namespace}:${bindingId}`;
72 return {
73 binding: { owner: "t3" as const, key },
74 get: Effect.sync(() => Option.fromUndefinedOr(credentials.get(key))),
75 set: (value: Uint8Array) => Effect.sync(() => void credentials.set(key, value)),
76 remove: Effect.sync(() => void credentials.delete(key)),
77 };
78 });
79 }),
80 });
81 return Context.make(ProviderHost.ProviderHost, host).pipe(
82 Context.add(TestProviderHostSettings, {
83 set: (next) => Ref.set(settings, next),
84 }),
85 );
86 }),
87 );