-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathsession-config.ts
More file actions
395 lines (366 loc) · 12.3 KB
/
Copy pathsession-config.ts
File metadata and controls
395 lines (366 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import type {
SessionConfigOption,
SessionConfigSelectOption,
} from "@agentclientprotocol/sdk";
import {
CODEX_MODE_PRESETS,
type CodexModePreset,
type ExecutionMode,
resolveCloudInitialPermissionMode,
restrictedModelMeta,
} from "@posthog/shared";
import {
type GatewayModel,
isOpenAIModel,
type ModelInfo,
} from "../../gateway-models";
import { getReasoningEffortOptions } from "./models";
/**
* Session config + mode synthesis for the codex app-server adapter. The native
* app-server has no "mode" RPC (a thread is configured by `approvalPolicy` +
* `sandbox`), so modes are synthesized here and applied per-turn.
*/
/**
* Per-turn sandbox the mode maps to (subset of codex's SandboxPolicy). This is
* what makes read-only/plan actually block edits — `approvalPolicy` alone is
* neutralized because the process spawns editable.
*/
export type CodexSandboxPolicy =
| { type: "readOnly"; networkAccess: boolean }
| { type: "workspaceWrite"; networkAccess: boolean; writableRoots?: string[] }
| { type: "dangerFullAccess" };
export interface CodexMode {
id: string;
name: string;
description: string;
/** codex AskForApproval the mode maps to, applied per-turn on turn/start. */
approvalPolicy: string;
/**
* Per-turn sandbox, sent on every turn/start. codex keeps turn overrides for
* subsequent turns, so every mode states its full sandbox — omitting it would
* leave the previous mode's sandbox active (e.g. plan's readOnly bleeding
* into auto, which then prompts for every command and edit). Only applied off
* the cloud sandbox, where a non-danger policy would re-engage the
* unavailable linux-sandbox and panic.
*/
sandboxPolicy: CodexSandboxPolicy;
/**
* codex's native collaboration mode (per-turn on `turn/start`). "plan" unlocks
* plan proposals + `request_user_input`; everything else runs "default".
*/
collaborationMode?: "plan" | "default";
}
/**
* The editable sandbox for a platform, mirroring spawn.ts's `sandbox_mode`:
* macOS Seatbelt supports workspace-write; linux/windows have no sandbox
* launcher (a managed sandbox would panic), so danger-full-access. Network
* stays restricted so commands that need egress still go through codex's
* escalation prompt — broadening that is a security decision, not a UX fix.
*/
function editableSandboxPolicy(platform: string): CodexSandboxPolicy {
return platform === "darwin"
? { type: "workspaceWrite", networkAccess: false }
: { type: "dangerFullAccess" };
}
// Flattened Claude-style presets: the `{id, name, description}` literals live
// in @posthog/shared (one copy for every picker); this map owns the behavior.
// Restriction is driven by approvalPolicy + sandboxPolicy: plan/read-only block
// edits, auto/full-access restore the platform's editable sandbox.
function modePolicies(
platform: string,
): Record<
CodexModePreset["id"],
Pick<CodexMode, "approvalPolicy" | "sandboxPolicy" | "collaborationMode">
> {
return {
plan: {
approvalPolicy: "on-request",
sandboxPolicy: { type: "readOnly", networkAccess: true },
collaborationMode: "plan",
},
"read-only": {
approvalPolicy: "untrusted",
sandboxPolicy: { type: "readOnly", networkAccess: true },
},
auto: {
approvalPolicy: "on-request",
sandboxPolicy: editableSandboxPolicy(platform),
},
"full-access": {
approvalPolicy: "never",
sandboxPolicy: { type: "dangerFullAccess" },
},
};
}
/** Test seam: the mode table for a given platform; CODEX_MODES uses the live one. */
export function buildCodexModes(platform: string): CodexMode[] {
const policies = modePolicies(platform);
return CODEX_MODE_PRESETS.map((preset) => ({
...preset,
...policies[preset.id],
}));
}
export const CODEX_MODES: CodexMode[] = buildCodexModes(process.platform);
export const DEFAULT_MODE = "auto";
export function modeApprovalPolicy(
modeId: string | undefined,
): string | undefined {
return CODEX_MODES.find((m) => m.id === modeId)?.approvalPolicy;
}
/** Per-turn sandbox for a mode id (sent every turn — codex turn overrides are sticky). */
export function sandboxPolicyFor(
modeId: string | undefined,
): CodexSandboxPolicy | undefined {
return CODEX_MODES.find((m) => m.id === modeId)?.sandboxPolicy;
}
/** codex collaboration mode for a preset — "plan" only for Plan, else "default". */
export function collaborationModeFor(
modeId: string | undefined,
): "plan" | "default" {
return (
CODEX_MODES.find((m) => m.id === modeId)?.collaborationMode ?? "default"
);
}
/**
* Resolve a host permission mode or live picker value to a codex mode. A
* recognized mode is honored; Claude's bypass mode maps to Codex full access.
* Other unknown modes fall back to default.
*/
export function resolveCodexMode(mode: string | undefined): string {
if (!mode) return DEFAULT_MODE;
return resolveCloudInitialPermissionMode("codex", mode as ExecutionMode);
}
/** Codex's standard reasoning efforts; used when model/list doesn't expose them. */
export const DEFAULT_EFFORTS = ["low", "medium", "high"];
// Display labels for reasoning efforts; the host renders `name` verbatim.
const EFFORT_LABELS: Record<string, string> = {
low: "Low",
medium: "Medium",
high: "High",
xhigh: "Extra High",
max: "Max",
};
function humanizeEffort(effort: string): string {
return EFFORT_LABELS[effort] ?? effort;
}
/** The current selector values `buildConfigOptions` projects into ACP options. */
export interface ConfigSelectors {
/** Current permission/collaboration preset id (one of CODEX_MODES). */
mode: string;
model: string;
effort?: string;
/** From model/list; falls back to the single current model when empty. */
models: Array<{
id: string;
name: string;
_meta?: Record<string, unknown>;
}>;
efforts: string[];
}
/** Builds the ACP configOptions (mode + model + thought_level) the host renders. */
export function buildConfigOptions(s: ConfigSelectors): SessionConfigOption[] {
const baseModels = s.models.length
? s.models
: [{ id: s.model, name: s.model }];
// Ensure the active model stays selectable, else currentValue points at nothing.
const models = baseModels.some((m) => m.id === s.model)
? baseModels
: [...baseModels, { id: s.model, name: s.model }];
const baseEfforts = s.efforts.length ? s.efforts : DEFAULT_EFFORTS;
const currentEffort = s.effort ?? baseEfforts[0];
const efforts = baseEfforts.includes(currentEffort)
? baseEfforts
: [...baseEfforts, currentEffort];
return [
{
type: "select",
id: "mode",
name: "Mode",
category: "mode",
currentValue: s.mode,
options: CODEX_MODES.map((m) => ({
name: m.name,
value: m.id,
description: m.description,
})),
} as unknown as SessionConfigOption,
{
type: "select",
id: "model",
name: "Model",
category: "model",
currentValue: s.model,
options: models.map(
(m): SessionConfigSelectOption => ({
name: m.name,
value: m.id,
...(m._meta ? { _meta: m._meta } : {}),
}),
),
} as unknown as SessionConfigOption,
{
type: "select",
id: "effort",
name: "Reasoning effort",
category: "thought_level",
currentValue: currentEffort,
options: efforts.map((e) => ({ name: humanizeEffort(e), value: e })),
} as unknown as SessionConfigOption,
];
}
/** A model entry from the app-server's `model/list` (loosely typed). */
interface RawModel {
id?: string;
model?: string;
displayName?: string;
hidden?: boolean;
supportedReasoningEfforts?: Array<{ reasoningEffort?: string } | string>;
}
/**
* Stateful holder for a codex session's model / effort / mode selectors and the
* ACP `configOptions` derived from them — synthesizing the Claude-style picker
* the app-server has no native concept of, rebuilt on every change.
*/
export class SessionConfigState {
private _model: string;
private _effort?: string;
private _mode = DEFAULT_MODE;
private models: Array<{
id: string;
name: string;
_meta?: Record<string, unknown>;
}> = [];
private efforts: string[] = [];
private _options: SessionConfigOption[] = [];
private readonly gatewayModels?: ReadonlyArray<ModelInfo>;
private readonly allowedModelIds?: ReadonlySet<string>;
constructor(
model: string,
effort?: string,
gatewayModels?: ReadonlyArray<ModelInfo>,
) {
this._model = model;
this._effort = effort;
this.gatewayModels = gatewayModels?.length ? gatewayModels : undefined;
this.allowedModelIds = this.gatewayModels
? new Set(
this.gatewayModels
.filter((gatewayModel) => gatewayModel.allowed)
.map((gatewayModel) => gatewayModel.id),
)
: undefined;
this.rebuild();
}
get model(): string {
return this._model;
}
get effort(): string | undefined {
return this._effort;
}
get mode(): string {
return this._mode;
}
get options(): SessionConfigOption[] {
return this._options;
}
/** Apply the host's initial approval mode (from `_meta.permissionMode`). */
setInitialMode(permissionMode: string | undefined): void {
this._mode = resolveCodexMode(permissionMode);
this.rebuild();
}
/** Apply a `setSessionConfigOption` change; returns whether the mode changed. */
setOption(
configId: string | undefined,
value: unknown,
): { modeChanged: boolean } {
let modeChanged = false;
if (typeof value === "string") {
if (
configId === "model" &&
(!this.gatewayModels || this.allowedModelIds?.has(value))
) {
this._model = value;
} else if (configId === "effort") this._effort = value;
else if (configId === "mode") {
this._mode = resolveCodexMode(value);
modeChanged = true;
}
}
this.rebuild();
return { modeChanged };
}
/**
* Populate the model + effort selectors from a `model/list` `data` array. The
* gateway also serves Claude models, so drop non-OpenAI ones; it doesn't
* populate efforts, so fall back to the shared codex model→effort map.
*/
loadModels(rawModels: RawModel[]): void {
const liveModels = rawModels
.filter((m) => !m?.hidden)
.filter((m) => isOpenAIModel(m as unknown as GatewayModel))
.map((m) => ({
id: (m.id ?? m.model) as string,
name: (m.displayName ?? m.id ?? m.model) as string,
}));
if (this.gatewayModels) {
const liveModelsById = new Map(
liveModels.map((model) => [model.id, model]),
);
this.models = this.gatewayModels.map((gatewayModel) => ({
...(liveModelsById.get(gatewayModel.id) ?? {
id: gatewayModel.id,
name: gatewayModel.id,
}),
...(gatewayModel.allowed ? {} : { _meta: restrictedModelMeta() }),
}));
} else {
this.models = liveModels;
}
const current = rawModels.find(
(m) => m.id === this._model || m.model === this._model,
);
const liveEfforts = (current?.supportedReasoningEfforts ?? [])
.map((e) => (typeof e === "string" ? e : e?.reasoningEffort))
.filter((e): e is string => typeof e === "string");
this.efforts = liveEfforts.length
? liveEfforts
: getReasoningEffortOptions(this._model).map((o) => o.value);
this.rebuild();
}
/** Reset the model/effort lists (model/list failed); keeps the current model. */
clearModels(): void {
this.models =
this.gatewayModels?.map((gatewayModel) => ({
id: gatewayModel.id,
name: gatewayModel.id,
...(gatewayModel.allowed ? {} : { _meta: restrictedModelMeta() }),
})) ?? [];
this.efforts = [];
this.rebuild();
}
/**
* codex's per-turn `collaborationMode`: `{ mode, settings: { model } }`. The
* model must be a string (not the null in collaborationMode/list output).
*/
collaborationModeForTurn(): unknown {
return {
mode: collaborationModeFor(this._mode),
settings: { model: this._model },
};
}
approvalPolicy(): string | undefined {
return modeApprovalPolicy(this._mode);
}
sandboxPolicy(): CodexSandboxPolicy | undefined {
return sandboxPolicyFor(this._mode);
}
private rebuild(): void {
this._options = buildConfigOptions({
mode: this._mode,
model: this._model,
effort: this._effort,
models: this.models,
efforts: this.efforts,
});
}
}