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