@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 86 lines 3.28 KB
Raw
1 import { OrchestratorMcpFailure } from "@t3tools/contracts";
2 import { THEME_GUIDE } from "@t3tools/shared/themeCss";
3 import { MOBILE_THEME_IDS } from "@t3tools/shared/themePalettes";
4 import * as Effect from "effect/Effect";
5 import * as Struct from "effect/Struct";
6 import * as EnvironmentTheme from "../../../environmentTheme.ts";
7 import * as ThreadCommandExecutor from "../../../orchestration-v2/ThreadCommandExecutor.ts";
8 import * as McpInvocationContext from "../../McpInvocationContext.ts";
9 import * as McpToolAccess from "../../McpToolAccess.ts";
10 import { unavailable } from "../../threadAccess.ts";
11 import { ThemeToolkit } from "./tools.ts";
12
13 export const layer = McpToolAccess.toLayer(ThemeToolkit, {
14 t3_theme_read: McpToolAccess.reads(({ themeId }) =>
15 Effect.gen(function* () {
16 const themes = yield* EnvironmentTheme.EnvironmentThemeService;
17 const {
18 current,
19 themesDir,
20 themes: published,
21 theme,
22 } = yield* themes.read(themeId).pipe(
23 Effect.catchTags({
24 EnvironmentThemeRefusedError: (error) =>
25 Effect.fail(
26 new OrchestratorMcpFailure({ code: "invalid_request", message: error.message }),
27 ),
28 ServerSettingsError: () => Effect.fail(unavailable()),
29 }),
30 );
31 return {
32 current,
33 builtIn: MOBILE_THEME_IDS,
34 published: published.map(({ id, name, css, background }) => ({
35 id,
36 name,
37 hasCss: Boolean(css),
38 hasBackground: background !== undefined,
39 })),
40 ...(theme === undefined ? {} : { theme: Struct.omit(theme, ["id", "background"]) }),
41 themesDir,
42 guide: THEME_GUIDE,
43 };
44 }),
45 ),
46 t3_theme_set: McpToolAccess.writesEnvironment(({ themeId, theme, appearance }, check) =>
47 Effect.gen(function* () {
48 const input =
49 themeId !== undefined
50 ? { themeId, theme, appearance }
51 : theme === undefined && appearance !== undefined
52 ? { appearance }
53 : undefined;
54 if (input === undefined) {
55 return yield* new OrchestratorMcpFailure({
56 code: "invalid_request",
57 message: "Give `themeId`, `appearance`, or both. `theme` needs a `themeId`.",
58 });
59 }
60 const scope = yield* McpInvocationContext.McpInvocationContext;
61 const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor;
62 const themes = yield* EnvironmentTheme.EnvironmentThemeService;
63 const set = Effect.gen(function* () {
64 // The turn may have ended, or the thread's modes changed, while this waited for the lock.
65 yield* check;
66 yield* themes.apply(input).pipe(
67 Effect.catchTags({
68 EnvironmentThemeRefusedError: (error) =>
69 Effect.fail(
70 new OrchestratorMcpFailure({ code: "invalid_request", message: error.message }),
71 ),
72 EnvironmentThemeNotSelectedError: () => Effect.fail(unavailable()),
73 }),
74 );
75 return {
76 ...(themeId === undefined ? {} : { themeId }),
77 ...(appearance === undefined ? {} : { appearance }),
78 published: theme !== undefined,
79 };
80 });
81 return yield* scope.thread === undefined
82 ? set
83 : executor.withLock(scope.thread.threadId, set);
84 }),
85 ),
86 });