Files
opencode-manager/shared/src/schemas/settings.ts
T
Chris Scott b21fdf5b94 feat(sandbox): per-session sandbox VMs with resource settings and provider arbitration
- Sandbox new root sessions by default with a per-session composer toggle;
  child sessions inherit their root session's mode (OM-82)
- Run each sandboxed root session in its own microVM that mounts only the
  session's repository, with per-session CPU/memory overrides, Settings
  defaults, idle stop, and a cap on running VMs (OM-89)
- Arbitrate shell sandboxing with other plugins (opencode-forge) through the
  shared opencode.sandbox-arbiter.v1 registry so exactly one provider
  sandboxes each call regardless of load order; fail closed on claim errors
- Throw when another plugin rewrites a pinned sandbox shell setting
- Tag sandboxed tool results with the owning provider and show it in the badge
- Hint the agent to start Docker inside the VM only when OCM owns the session
2026-10-15 16:53:49 -04:00

480 lines
15 KiB
TypeScript

import { z } from "zod";
import { NotificationPreferencesSchema, DEFAULT_NOTIFICATION_PREFERENCES } from "./notifications";
import { OPENCODE_CONFIG_SOURCE_NAMES } from "../config/defaults";
import { ColorThemeIdSchema, MANAGER_COLOR_THEME_ID } from "../themes";
import { SessionPermissionModeSchema } from "./session-permissions";
import { GOAL_MAX_CONTINUATIONS_MIN, GOAL_MAX_CONTINUATIONS_MAX } from "./limits";
import { SessionSandboxResourcesSchema } from "./session-sandbox";
export const CustomCommandSchema = z.object({
name: z.string(),
description: z.string(),
promptTemplate: z.string(),
});
export const TTSConfigSchema = z.object({
enabled: z.boolean(),
provider: z.enum(['external', 'builtin']).default('external'),
autoPlay: z.boolean().default(false),
endpoint: z.string(),
apiKey: z.string(),
voice: z.string(),
model: z.string(),
speed: z.number().min(0.25).max(4.0),
availableVoices: z.array(z.string()).optional(),
availableModels: z.array(z.string()).optional(),
lastVoicesFetch: z.number().optional(),
lastModelsFetch: z.number().optional(),
});
export const STTConfigSchema = z.object({
enabled: z.boolean(),
provider: z.enum(['external', 'builtin']).default('builtin'),
endpoint: z.string(),
apiKey: z.string(),
model: z.string(),
language: z.string().default('en-US'),
availableModels: z.array(z.string()).optional(),
lastModelsFetch: z.number().optional(),
});
export type TTSConfig = {
enabled: boolean;
provider: 'external' | 'builtin';
autoPlay: boolean;
endpoint: string;
apiKey: string;
voice: string;
model: string;
speed: number;
availableVoices?: string[];
availableModels?: string[];
lastVoicesFetch?: number;
lastModelsFetch?: number;
};
export type STTConfig = {
enabled: boolean;
provider: 'external' | 'builtin';
endpoint: string;
apiKey: string;
model: string;
language: string;
availableModels?: string[];
lastModelsFetch?: number;
};
export const DEFAULT_LEADER_KEY = 'Ctrl+X';
export const DEFAULT_KEYBOARD_SHORTCUTS = {
submit: 'Cmd+Enter',
abort: 'Escape',
clearPrompt: '',
toggleMode: 'Shift+Tab',
undo: 'U',
redo: 'R',
compact: 'C',
fork: '',
settings: '',
sessions: 'L',
switchPickerTab: 'Ctrl+S',
newSession: 'N',
closeSession: 'W',
toggleSidebar: 'B',
selectModel: 'M',
variantCycle: 'Ctrl+T',
favoriteCycle: 'Ctrl+R',
toggleTerminal: 'T',
toggleSourceControl: '',
timeline: 'G',
exportSession: 'X',
halfPageUp: 'Ctrl+U',
halfPageDown: 'Ctrl+D',
speakVisible: 'S',
} satisfies Record<string, string>;
export type KeyboardShortcutAction = keyof typeof DEFAULT_KEYBOARD_SHORTCUTS;
export const DEFAULT_DIRECT_SHORTCUTS = ['submit', 'abort', 'clearPrompt', 'toggleMode', 'variantCycle', 'favoriteCycle', 'halfPageUp', 'halfPageDown', 'switchPickerTab'];
export const GitCredentialSchema = z.object({
id: z.string().optional(),
name: z.string(),
host: z.string(),
type: z.enum(['pat', 'ssh']).default('pat'),
token: z.string().optional(),
sshPrivateKey: z.string().optional(),
sshPrivateKeyEncrypted: z.string().optional(),
hasPassphrase: z.boolean().optional(),
username: z.string().optional(),
passphrase: z.string().optional(),
});
export type GitCredential = z.infer<typeof GitCredentialSchema>;
export const GitIdentitySchema = z.object({
name: z.string(),
email: z.string(),
});
export type GitIdentity = z.infer<typeof GitIdentitySchema>;
export const GitIdentityProfileSchema = GitIdentitySchema.extend({
id: z.string().min(1),
});
export type GitIdentityProfile = z.infer<typeof GitIdentityProfileSchema>;
export const ServerEnvVarSchema = z.object({
key: z.string().min(1),
value: z.string(),
});
type ServerEnvVar = z.infer<typeof ServerEnvVarSchema>
export const BLOCKED_SERVER_ENV_KEYS = [
'OPENCODE_PASSWORD',
'OPENCODE_SERVER_PASSWORD',
'OPENCODE_CONFIG',
'OPENCODE_CONFIG_DIR',
'OPENCODE_CONFIG_CONTENT',
'XDG_DATA_HOME',
'XDG_STATE_HOME',
'XDG_CONFIG_HOME',
'TMPDIR',
] as const;
export const DEFAULT_GIT_IDENTITY: GitIdentity = {
name: 'OpenCode Agent',
email: '',
};
export const DEFAULT_SANDBOX_IDLE_STOP_MINUTES = 30;
export const DEFAULT_SANDBOX_MAX_RUNNING_VMS = 4;
export const SandboxPreferencesSchema = z.object({
enabled: z.boolean(),
gitCredentials: z.boolean().optional(),
defaultForNewSessions: z.boolean().optional(),
cpus: SessionSandboxResourcesSchema.shape.cpus.optional(),
memoryMib: SessionSandboxResourcesSchema.shape.memoryMib.optional(),
idleStopMinutes: z.number().int().min(1).max(1440).optional(),
maxRunningVms: z.number().int().min(1).max(32).optional(),
});
export type SandboxPreferences = z.infer<typeof SandboxPreferencesSchema>;
export const DEFAULT_SANDBOX_PREFERENCES: SandboxPreferences = {
enabled: false,
gitCredentials: false,
defaultForNewSessions: true,
idleStopMinutes: DEFAULT_SANDBOX_IDLE_STOP_MINUTES,
maxRunningVms: DEFAULT_SANDBOX_MAX_RUNNING_VMS,
};
export const SessionDefaultsSchema = z.object({
permissionMode: SessionPermissionModeSchema,
goalMaxContinuations: z.number().int().min(GOAL_MAX_CONTINUATIONS_MIN).max(GOAL_MAX_CONTINUATIONS_MAX).optional(),
goalTokenBudget: z.number().int().positive().optional(),
goalAuditorModel: z.string().optional(),
});
export type SessionDefaults = z.infer<typeof SessionDefaultsSchema>;
export const DEFAULT_SESSION_DEFAULTS = {
permissionMode: 'ask',
goalMaxContinuations: 20,
} satisfies SessionDefaults;
export const UserPreferencesSchema = z.object({
theme: z.enum(["dark", "light", "system"]),
colorTheme: ColorThemeIdSchema.optional(),
mode: z.enum(["plan", "build"]),
defaultModel: z.string().optional(),
walkthroughModel: z.string().optional(),
defaultAgent: z.string().optional(),
autoScroll: z.boolean(),
expandDiffs: z.boolean(),
expandToolCalls: z.boolean(),
groupToolCalls: z.boolean(),
showReasoning: z.boolean(),
simpleChatMode: z.boolean(),
leaderKey: z.string().optional(),
directShortcuts: z.array(z.string()).optional(),
keyboardShortcuts: z.record(z.string(), z.string()),
customCommands: z.array(CustomCommandSchema),
gitCredentials: z.array(GitCredentialSchema).optional(),
defaultGitCredentialId: z.string().optional(),
gitIdentity: GitIdentitySchema.optional(),
gitIdentities: z.array(GitIdentityProfileSchema).optional(),
tts: TTSConfigSchema.optional(),
stt: STTConfigSchema.optional(),
notifications: NotificationPreferencesSchema.optional(),
lastKnownGoodConfig: z.string().optional(),
repoOrder: z.array(z.number()).optional(),
repoSortMode: z.enum(['recent', 'manual', 'name']).optional(),
serverEnvVars: z.array(ServerEnvVarSchema).optional(),
sandbox: SandboxPreferencesSchema.optional(),
sessionDefaults: SessionDefaultsSchema.optional(),
disabledDefaultServerEnvVars: z.array(z.string()).optional(),
});
export const DEFAULT_TTS_CONFIG: TTSConfig = {
enabled: false,
provider: 'external',
autoPlay: false,
endpoint: "https://api.openai.com",
apiKey: "",
voice: "alloy",
model: "tts-1",
speed: 1.0,
availableVoices: [],
availableModels: [],
lastVoicesFetch: 0,
lastModelsFetch: 0,
};
export const DEFAULT_STT_CONFIG: STTConfig = {
enabled: false,
provider: 'builtin',
endpoint: "https://api.openai.com",
apiKey: "",
model: '',
language: 'en-US',
availableModels: [],
lastModelsFetch: 0,
};
export const DEFAULT_USER_PREFERENCES = {
theme: "dark" as const,
colorTheme: MANAGER_COLOR_THEME_ID,
mode: "build" as const,
autoScroll: true,
expandDiffs: true,
expandToolCalls: false,
groupToolCalls: true,
showReasoning: false,
simpleChatMode: false,
keyboardShortcuts: {} as Record<string, string>,
customCommands: [],
customAgents: [],
gitCredentials: [] as GitCredential[],
defaultGitCredentialId: undefined as string | undefined,
gitIdentity: DEFAULT_GIT_IDENTITY,
tts: DEFAULT_TTS_CONFIG,
stt: DEFAULT_STT_CONFIG,
notifications: DEFAULT_NOTIFICATION_PREFERENCES,
repoSortMode: 'recent' as const,
serverEnvVars: [] as ServerEnvVar[],
sandbox: DEFAULT_SANDBOX_PREFERENCES,
sessionDefaults: DEFAULT_SESSION_DEFAULTS,
};
export const SettingsResponseSchema = z.object({
preferences: UserPreferencesSchema,
updatedAt: z.number(),
});
export const UpdateSettingsRequestSchema = z.object({
preferences: UserPreferencesSchema.partial(),
});
export const ProviderApiConfigSchema = z.object({
url: z.string(),
npm: z.string().optional(),
});
export type ProviderApiConfig = z.infer<typeof ProviderApiConfigSchema>;
export const ModelCapabilitiesSchema = z.object({
temperature: z.boolean(),
reasoning: z.boolean(),
attachment: z.boolean(),
toolcall: z.boolean(),
input: z.object({
text: z.boolean(),
audio: z.boolean(),
image: z.boolean(),
video: z.boolean(),
pdf: z.boolean(),
}),
output: z.object({
text: z.boolean(),
audio: z.boolean(),
image: z.boolean(),
video: z.boolean(),
pdf: z.boolean(),
}),
interleaved: z.union([
z.boolean(),
z.object({
field: z.enum(["reasoning_content", "reasoning_details"]),
}),
]),
});
export const ModelCostSchema = z.object({
input: z.number(),
output: z.number(),
cache: z.object({
read: z.number(),
write: z.number(),
}).optional(),
experimentalOver200K: z.object({
input: z.number(),
output: z.number(),
cache: z.object({
read: z.number(),
write: z.number(),
}).optional(),
}).optional(),
});
export const ModelLimitSchema = z.object({
context: z.number(),
input: z.number().optional(),
output: z.number(),
});
export const ModelConfigSchema = z.object({
id: z.string().optional(),
providerID: z.string().optional(),
api: ProviderApiConfigSchema.optional(),
name: z.string().optional(),
family: z.string().optional(),
capabilities: ModelCapabilitiesSchema.optional(),
cost: ModelCostSchema.optional(),
limit: ModelLimitSchema.optional(),
status: z.enum(["alpha", "beta", "deprecated", "active"]).optional(),
options: z.record(z.string(), z.any()).optional(),
headers: z.record(z.string(), z.string()).optional(),
release_date: z.string().optional(),
variants: z.record(z.string(), z.record(z.string(), z.any())).optional(),
});
export type ModelConfig = z.infer<typeof ModelConfigSchema>;
export const ProviderSourceSchema = z.enum(["env", "config", "custom", "api"]);
export const ProviderConfigSchema = z.object({
id: z.string().optional(),
name: z.string().optional(),
source: ProviderSourceSchema.optional(),
env: z.array(z.string()).optional().default([]),
key: z.string().optional(),
api: z.string().optional(),
npm: z.string().optional(),
whitelist: z.array(z.string()).optional(),
blacklist: z.array(z.string()).optional(),
options: z.record(z.string(), z.any()).optional(),
models: z.record(z.string(), ModelConfigSchema).optional(),
});
export type ProviderSource = z.infer<typeof ProviderSourceSchema>;
export type ProviderConfig = z.infer<typeof ProviderConfigSchema>;
export const OpenCodePluginOptionsSchema = z.record(z.string(), z.unknown());
export const OpenCodePluginSpecSchema = z.union([
z.string(),
z.tuple([z.string(), OpenCodePluginOptionsSchema]),
]);
export const OpenCodeNativePluginSchema = z.union([
z.string(),
z.object({
package: z.string(),
options: OpenCodePluginOptionsSchema.optional(),
}),
]);
export const OpenCodePermissionRuleSchema = z.object({
action: z.string(),
resource: z.string(),
effect: z.enum(["allow", "deny", "ask"]),
});
export const OpenCodeConfigSchema = z.object({
$schema: z.string().optional(),
theme: z.string().optional(),
model: z.string().optional(),
small_model: z.string().optional(),
default_agent: z.string().optional(),
provider: z.record(z.string(), ProviderConfigSchema).optional(),
providers: z.record(z.string(), z.record(z.string(), z.any())).optional(),
agent: z.record(z.string(), z.any()).optional(),
agents: z.record(z.string(), z.record(z.string(), z.any())).optional(),
command: z.record(z.string(), z.any()).optional(),
commands: z.record(z.string(), z.record(z.string(), z.any())).optional(),
keybinds: z.record(z.string(), z.any()).optional(),
autoupdate: z.union([z.boolean(), z.literal("notify")]).optional(),
update: z.enum(["disable", "notify", "auto"]).optional(),
formatter: z.record(z.string(), z.any()).optional(),
permission: z.record(z.string(), z.any()).optional(),
permissions: z.array(OpenCodePermissionRuleSchema).optional(),
mcp: z.record(z.string(), z.any()).optional(),
instructions: z.array(z.string()).optional(),
disabled_providers: z.array(z.string()).optional(),
share: z.enum(["manual", "auto", "disabled"]).optional(),
plugin: z.array(OpenCodePluginSpecSchema).optional(),
plugins: z.array(OpenCodeNativePluginSchema).optional(),
skills: z.union([
z.array(z.string()),
z.object({
paths: z.array(z.string()).optional(),
urls: z.array(z.string()).optional(),
}),
]).optional(),
snapshots: z.boolean().optional(),
username: z.string().optional(),
media: z.record(z.string(), z.any()).optional(),
references: z.record(z.string(), z.any()).optional(),
worktree: z.record(z.string(), z.any()).optional(),
websearch: z.union([z.literal(false), z.record(z.string(), z.any())]).optional(),
warming: z.union([z.boolean(), z.record(z.string(), z.any())]).optional(),
compaction: z.record(z.string(), z.any()).optional(),
experimental: z.record(z.string(), z.any()).optional(),
tool_output: z.record(z.string(), z.any()).optional(),
watcher: z.record(z.string(), z.any()).optional(),
}).strip();
export const OpenCodeConfigValidationIssueSchema = z.object({
path: z.string(),
message: z.string(),
});
export const OpenCodeConfigSourceNameSchema = z.enum(OPENCODE_CONFIG_SOURCE_NAMES);
export const OpenCodeConfigSourceFileSchema = z.object({
name: OpenCodeConfigSourceNameSchema,
path: z.string(),
rawContent: z.string(),
content: z.record(z.string(), z.unknown()),
isValid: z.boolean(),
validationIssues: z.array(OpenCodeConfigValidationIssueSchema).optional(),
updatedAt: z.number(),
});
export const OpenCodeConfigFileSchema = z.object({
path: z.string(),
content: z.record(z.string(), z.unknown()),
rawContent: z.string(),
isValid: z.boolean(),
validationIssues: z.array(OpenCodeConfigValidationIssueSchema).optional(),
updatedAt: z.number(),
sources: z.array(OpenCodeConfigSourceFileSchema),
revision: z.string(),
});
export const UpdateOpenCodeConfigRequestSchema = z.object({
content: z.union([z.record(z.string(), z.unknown()), z.string()]),
source: OpenCodeConfigSourceNameSchema.optional(),
expectedRevision: z.string().optional(),
});
export const UpdateOpenCodeConfigPatchRequestSchema = z.object({
patch: z.record(z.string(), z.unknown()),
source: OpenCodeConfigSourceNameSchema.optional(),
expectedRevision: z.string().optional(),
});