mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
126 lines
4.1 KiB
TypeScript
126 lines
4.1 KiB
TypeScript
import { PRODUCT_IDENTITY } from "../identity.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import type {
|
|
ClientConfigurationDocument,
|
|
ClientInlineConfiguration,
|
|
} from "./ClientConfigurationDocument.js";
|
|
import {
|
|
throwCleanupFailure,
|
|
reachedReadOnlyFile,
|
|
reachedReadOnlyFileDetail,
|
|
readConfigurationFile,
|
|
} from "./ClientConfigurationFiles.js";
|
|
import {
|
|
currentUser,
|
|
readOpenCodeManagedPreferences,
|
|
} from "./OpenCodeManagedPreferences.js";
|
|
import {
|
|
existingOpenCodeConfigPath,
|
|
openCodeConfigurationPaths,
|
|
openCodeManagedPreferencePaths,
|
|
openCodeSystemConfigPaths,
|
|
} from "./SupportedClientLocations.js";
|
|
import type {
|
|
ClientLayeredLocation,
|
|
ClientPathContext,
|
|
ClientPathFailure,
|
|
} from "./SupportedClients.js";
|
|
|
|
/** The system and managed files as one read-only layer, in load order. */
|
|
const readUpperLayers = async (
|
|
context: ClientPathContext,
|
|
): Promise<
|
|
Result<ClientInlineConfiguration | undefined, ClientPathFailure>
|
|
> => {
|
|
const documents: (ClientConfigurationDocument & { readOnlyPath: string })[] =
|
|
[];
|
|
let declaring: string | undefined;
|
|
let last: string | undefined;
|
|
const add = (path: string, document: ClientConfigurationDocument) => {
|
|
documents.push({ ...document, readOnlyPath: path });
|
|
last = path;
|
|
const name = PRODUCT_IDENTITY.mcpServerKey;
|
|
if (
|
|
Object.hasOwn(document.servers, name) ||
|
|
Object.hasOwn(document.legacyServers, name)
|
|
)
|
|
declaring = path;
|
|
};
|
|
for (const path of openCodeSystemConfigPaths(context)) {
|
|
const read = await readConfigurationFile(
|
|
path,
|
|
"opencode",
|
|
context.resources,
|
|
);
|
|
if (!read.ok) {
|
|
throwCleanupFailure(read.error, context.resources);
|
|
return err({ path, message: read.error.detail });
|
|
}
|
|
if (read.value !== undefined) add(path, read.value);
|
|
}
|
|
// Windows alone lets any local account create these files by default. Any
|
|
// of them may be what a message relies on, so the caution names them all.
|
|
const system = documents.map(({ readOnlyPath }) => readOnlyPath);
|
|
const caution =
|
|
context.platform === "win32" && system.length > 0
|
|
? `On Windows any local account can create files under %ProgramData% by default, so confirm an administrator owns ${system.join(" and ")} before trusting these settings.`
|
|
: undefined;
|
|
const managed = await readOpenCodeManagedPreferences(context);
|
|
if (!managed.ok) return managed;
|
|
if (managed.value !== undefined)
|
|
add(managed.value.path, managed.value.document);
|
|
const origin = declaring ?? last;
|
|
return ok(
|
|
origin === undefined
|
|
? undefined
|
|
: {
|
|
origin,
|
|
path: origin,
|
|
documents,
|
|
...(caution === undefined ? {} : { caution }),
|
|
},
|
|
);
|
|
};
|
|
|
|
/**
|
|
* The user files REA may write, beneath OpenCode's system files and macOS
|
|
* managed preferences, which override them and which REA never edits.
|
|
*/
|
|
export const openCodeLayeredLocation = async (
|
|
context: ClientPathContext,
|
|
): Promise<Result<ClientLayeredLocation, ClientPathFailure>> => {
|
|
const configPath = existingOpenCodeConfigPath(context);
|
|
const configPaths = openCodeConfigurationPaths(context);
|
|
const system = openCodeSystemConfigPaths(context);
|
|
const readOnlyFiles = [
|
|
...system,
|
|
...openCodeManagedPreferencePaths(context, currentUser()),
|
|
];
|
|
// A user path or link reaching one of these files would let REA edit it.
|
|
const reached = await reachedReadOnlyFile(
|
|
[configPath, ...configPaths],
|
|
readOnlyFiles,
|
|
context.platform,
|
|
);
|
|
if (reached !== undefined)
|
|
return err({
|
|
path: reached.path,
|
|
message: reachedReadOnlyFileDetail(reached),
|
|
});
|
|
const upper = await readUpperLayers(context);
|
|
if (!upper.ok) return upper;
|
|
return ok({
|
|
configPath,
|
|
configPaths,
|
|
// Later reads use the scope of the operation that asks for them.
|
|
readUpperLayers: (resources) => {
|
|
const { resources: _selection, ...rest } = context;
|
|
return readUpperLayers(
|
|
resources === undefined ? rest : { ...rest, resources },
|
|
);
|
|
},
|
|
readOnlyFiles,
|
|
...(upper.value === undefined ? {} : { inlineConfiguration: upper.value }),
|
|
});
|
|
};
|