Load managed-settings.json (#42)

The SDK doesn't currently load [Claude Code's
`managed-settings.json`](https://docs.anthropic.com/en/docs/claude-code/settings#settings-files)
- now the adapter does (following [the rules in the
docs](https://docs.anthropic.com/en/docs/claude-code/settings#settings-files)),
but we can remove this once the SDK supports it.
This commit is contained in:
Richard Feldman
2025-09-11 08:47:33 -04:00
committed by GitHub
parent d6c91b1ec1
commit f691024350
2 changed files with 50 additions and 0 deletions

View File

@@ -1,5 +1,13 @@
#!/usr/bin/env node #!/usr/bin/env node
// Load managed settings and apply environment variables
import { loadManagedSettings, applyEnvironmentSettings } from "./utils.js";
const managedSettings = loadManagedSettings();
if (managedSettings) {
applyEnvironmentSettings(managedSettings);
}
// stdout is used to send messages to the client // stdout is used to send messages to the client
// we redirect everything else to stderr to make sure it doesn't interfere with ACP // we redirect everything else to stderr to make sure it doesn't interfere with ACP
console.log = console.error; console.log = console.error;

View File

@@ -2,6 +2,8 @@
import { Readable, Writable } from "node:stream"; import { Readable, Writable } from "node:stream";
import { WritableStream, ReadableStream } from "node:stream/web"; import { WritableStream, ReadableStream } from "node:stream/web";
import { readFileSync } from "node:fs";
import { platform } from "node:os";
// Useful for bridging push-based and async-iterator-based code. // Useful for bridging push-based and async-iterator-based code.
export class Pushable<T> implements AsyncIterable<T> { export class Pushable<T> implements AsyncIterable<T> {
@@ -80,3 +82,43 @@ export function unreachable(value: never): never {
export function sleep(time: number): Promise<void> { export function sleep(time: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, time)); return new Promise((resolve) => setTimeout(resolve, time));
} }
interface ManagedSettings {
permissions?: {
allow?: string[];
deny?: string[];
};
env?: Record<string, string>;
}
// Following the rules in https://docs.anthropic.com/en/docs/claude-code/settings#settings-files
// This can be removed once the SDK supports it natively.
function getManagedSettingsPath(): string {
const os = platform();
switch (os) {
case "darwin":
return "/Library/Application Support/ClaudeCode/managed-settings.json";
case "linux": // including WSL
return "/etc/claude-code/managed-settings.json";
case "win32":
return "C:\\ProgramData\\ClaudeCode\\managed-settings.json";
default:
return "/etc/claude-code/managed-settings.json";
}
}
export function loadManagedSettings(): ManagedSettings | null {
try {
return JSON.parse(readFileSync(getManagedSettingsPath(), "utf8")) as ManagedSettings;
} catch {
return null;
}
}
export function applyEnvironmentSettings(settings: ManagedSettings): void {
if (settings.env) {
for (const [key, value] of Object.entries(settings.env)) {
process.env[key] = value;
}
}
}