Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpSessionConfig.ts

153 lines5.7 KB

1import type { ProviderOptionChoice, ProviderOptionDescriptor } from "@t3tools/contracts";
2import type * as EffectAcpSchema from "effect-acp/compat";
3​
4import type { AcpSessionModeState } from "./AcpRuntimeModel.ts";
5​
6/**
7 * Maps ACP session configuration and session modes onto T3's provider option
8 * descriptors so the existing model-options UI can drive them.
9 *
10 * Model selection stays on the dedicated model picker: `category: "model"`
11 * options are excluded here because they already surface as models. Session
12 * modes advertised through the modes API (rather than a config option) are
13 * folded into one synthetic descriptor that the ACP adapter routes to
14 * `session/set_mode`.
15 */
16​
17/** Synthetic descriptor ID for agents that expose modes outside config options. */
18export const ACP_SESSION_MODE_OPTION_ID = "_t3/session-mode";
19​
20const MAX_OPTION_DESCRIPTORS = 16;
21const MAX_OPTION_CHOICES = 64;
22const MAX_TEXT_LENGTH = 256;
23const MAX_DESCRIPTION_LENGTH = 1_024;
24​
25const boundedText = (value: string | null | undefined, maximumLength: number): string =>
26 (value ?? "").trim().slice(0, maximumLength);
27​
28const boundedOpaqueValue = (value: string, maximumLength: number): string | undefined =>
29 value.length > 0 && value === value.trim() && value.length <= maximumLength ? value : undefined;
30​
31function flattenSelectChoices(
32 options: EffectAcpSchema.SessionConfigSelectOptions,
33): ReadonlyArray<EffectAcpSchema.SessionConfigSelectOption> {
34 return options.flatMap((candidate) => ("value" in candidate ? [candidate] : candidate.options));
36​
37function selectChoices(
38 candidates: ReadonlyArray<{
39 readonly value: string;
40 readonly name: string;
41 readonly description?: string | null | undefined;
42 }>,
43): ReadonlyArray<ProviderOptionChoice> {
44 const seen = new Set<string>();
45 const choices: Array<ProviderOptionChoice> = [];
46 for (const candidate of candidates) {
47 const id = boundedOpaqueValue(candidate.value, MAX_TEXT_LENGTH);
48 if (id === undefined || seen.has(id)) continue;
49 seen.add(id);
50 const description = boundedText(candidate.description, MAX_DESCRIPTION_LENGTH);
51 choices.push({
52 id,
53 label: boundedText(candidate.name, MAX_TEXT_LENGTH) || id,
54 ...(description ? { description } : {}),
55 });
56 if (choices.length === MAX_OPTION_CHOICES) break;
57 }
58 return choices;
60​
61export function acpProviderOptionDescriptors(input: {
62 readonly configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> | null | undefined;
63 readonly modeState: AcpSessionModeState | undefined;
64}): ReadonlyArray<ProviderOptionDescriptor> {
65 const descriptors: Array<ProviderOptionDescriptor> = [];
66 const seen = new Set<string>();
67 const mirroredModeChoiceSets: Array<ReadonlySet<string>> = [];
68 let hasModeCategory = false;
69​
70 for (const option of input.configOptions ?? []) {
71 // "model" options surface as the model list; "collaboration_mode" options
72 // are driven by T3's own plan/build interaction mode in the ACP adapter.
73 if (option.category === "model" || option.category === "collaboration_mode") {
74 continue;
75 }
76 const id = boundedOpaqueValue(option.id, MAX_TEXT_LENGTH);
77 if (id === undefined || seen.has(id)) continue;
78 seen.add(id);
79 const description = boundedText(option.description, MAX_DESCRIPTION_LENGTH);
80 const base = {
81 id,
82 label: boundedText(option.name, MAX_TEXT_LENGTH) || id,
83 ...(description ? { description } : {}),
84 } as const;
85 if (option.type === "boolean") {
86 if (option.category === "mode") hasModeCategory = true;
87 descriptors.push({
88 ...base,
89 type: "boolean",
90 currentValue: option.currentValue,
91 });
92 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
93 continue;
94 }
95 const choices = selectChoices(flattenSelectChoices(option.options));
96 if (choices.length === 0) {
97 seen.delete(id);
98 continue;
99 }
100 if (option.category === "mode") hasModeCategory = true;
101 if (option.category === "thought_level") {
102 mirroredModeChoiceSets.push(new Set(choices.map((choice) => choice.id)));
103 }
104 const currentValue = option.currentValue;
105 descriptors.push({
106 ...base,
107 type: "select",
108 options: choices,
109 ...(currentValue && choices.some((choice) => choice.id === currentValue)
110 ? { currentValue }
111 : {}),
112 });
113 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
114 }
115​
116 const modeState = input.modeState;
117 if (modeState !== undefined && !hasModeCategory) {
118 const choices = selectChoices(
119 modeState.availableModes.map((mode) => ({
120 value: mode.id,
121 name: mode.name,
122 description: mode.description,
123 })),
124 );
125 const currentValue = modeState.currentModeId;
126 // Some agents mirror one knob through both the modes API and a config
127 // option (codex-acp advertises its thinking levels as modes too). Skip
128 // the synthetic descriptor when an existing descriptor already exposes
129 // the same choice set, so the composer shows the knob once.
130 const duplicatesKnownModeDescriptor = mirroredModeChoiceSets.some(
131 (choiceIds) =>
132 choiceIds.size === choices.length && choices.every((choice) => choiceIds.has(choice.id)),
133 );
134 if (
135 choices.length > 1 &&
136 !duplicatesKnownModeDescriptor &&
137 !seen.has(ACP_SESSION_MODE_OPTION_ID)
138 ) {
139 descriptors.push({
140 id: ACP_SESSION_MODE_OPTION_ID,
141 label: "Mode",
142 description: "Session mode advertised by the ACP agent.",
143 type: "select",
144 options: choices,
145 ...(currentValue && choices.some((choice) => choice.id === currentValue)
146 ? { currentValue }
147 : {}),
148 });
149 }
150 }
151​
152 return descriptors.slice(0, MAX_OPTION_DESCRIPTORS);