Open History to see the last commit
1import { OrchestratorMcpFailure, type ServerSettings } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Environment from "../../../environment/ServerEnvironment.ts";
4import * as ThreadCommandExecutor from "../../../orchestration-v2/ThreadCommandExecutor.ts";
5import * as Settings from "../../../serverSettings.ts";
6import * as McpInvocationContext from "../../McpInvocationContext.ts";
7import { readCaller, readMutationCaller, unavailable } from "../../threadAccess.ts";
8import { EnvironmentToolkit } from "./tools.ts";
9
10export function preferences(settings: ServerSettings) {
11 const {
12 defaultThreadEnvMode,
13 newWorktreesStartFromOrigin,
14 enableProviderUpdateChecks,
15 backgroundActivity,
16 sourceControlWritingStyle,
17 } = settings;
18 const characters = Array.from(sourceControlWritingStyle.customInstructions);
19 return {
20 defaultThreadEnvMode,
21 newWorktreesStartFromOrigin,
22 enableProviderUpdateChecks,
23 backgroundActivity: { profile: backgroundActivity.profile },
24 sourceControlWritingStyle: {
25 ...sourceControlWritingStyle,
26 customInstructions: characters.slice(0, 4000).join(""),
27 truncated: characters.length > 4000,
28 },
29 };
30}
31const access = (writable = false) =>
32 Effect.gen(function* () {
33 const context = yield* writable ? readMutationCaller() : readCaller();
34 const environment = yield* Environment.ServerEnvironment;
35 const descriptor = yield* environment.getDescriptor;
36 if (descriptor.environmentId !== context.scope.environmentId)
37 return yield* new OrchestratorMcpFailure({
38 code: "capability_denied",
39 message: "This credential belongs to another environment.",
40 });
41 return { ...context, descriptor, settings: yield* Settings.ServerSettingsService };
42 });
43export const EnvironmentHandlersLive = EnvironmentToolkit.toLayer({
44 t3_environment_read: () =>
45 Effect.gen(function* () {
46 const { descriptor, settings } = yield* access();
47 const current = yield* settings.getSettings.pipe(Effect.mapError(unavailable));
48 return {
49 environmentId: descriptor.environmentId,
50 label: descriptor.label,
51 serverVersion: descriptor.serverVersion,
52 platform: descriptor.platform,
53 preferences: preferences(current),
54 };
55 }),
56 t3_environment_preferences_update: (patch) =>
57 Effect.gen(function* () {
58 const scope = yield* McpInvocationContext.McpInvocationContext;
59 const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor;
60 return yield* executor.withLock(
61 scope.threadId,
62 Effect.gen(function* () {
63 const { caller, settings } = yield* access(true);
64 if (
65 caller.archivedAt !== null ||
66 caller.runtimeMode !== "full-access" ||
67 caller.interactionMode !== "default"
68 )
69 return yield* new OrchestratorMcpFailure({
70 code: "capability_denied",
71 message: "Preference updates require a live full-access/default thread.",
72 });
73 return preferences(
74 yield* settings.updateSettings(patch).pipe(Effect.mapError(unavailable)),
75 );
76 }),
77 );
78 }),
79});