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/** Encodes a valid empty ACP choice as a non-empty T3 ID; map back before applying. */
9export const ACP_EMPTY_OPTION_VALUE = "_t3/empty";
10
11const MAX_OPTION_DESCRIPTORS = 16;
12const MAX_OPTION_CHOICES = 64;
13const MAX_TEXT_LENGTH = 256;
14const MAX_DESCRIPTION_LENGTH = 1_024;
15
16const boundedText = (value: string | null | undefined, maximumLength: number): string =>
17 (value ?? "").trim().slice(0, maximumLength);
18
19const boundedOpaqueValue = (value: string, maximumLength: number): string | undefined =>
20 value.length > 0 && value === value.trim() && value.length <= maximumLength ? value : undefined;
21
22const choiceId = (value: string): string | undefined =>
23 value === "" ? ACP_EMPTY_OPTION_VALUE : boundedOpaqueValue(value, MAX_TEXT_LENGTH);
24
25export function flattenSelectChoices(
26 options: EffectAcpSchema.SessionConfigSelectOptions,
27): ReadonlyArray<EffectAcpSchema.SessionConfigSelectOption> {
28 return options.flatMap((candidate) => ("value" in candidate ? [candidate] : candidate.options));
29}
30
31function selectChoices(
32 candidates: ReadonlyArray<{
33 readonly value: string;
34 readonly name: string;
35 readonly description?: string | null | undefined;
36 }>,
37): ReadonlyArray<ProviderOptionChoice> {
38 const seen = new Set<string>();
39 const choices: Array<ProviderOptionChoice> = [];
40 for (const candidate of candidates) {
41 const id = choiceId(candidate.value);
42 if (id === undefined || seen.has(id)) continue;
43 seen.add(id);
44 const description = boundedText(candidate.description, MAX_DESCRIPTION_LENGTH);
45 choices.push({
46 id,
47 label: boundedText(candidate.name, MAX_TEXT_LENGTH) || id,
48 ...(description ? { description } : {}),
49 });
50 if (choices.length === MAX_OPTION_CHOICES) break;
51 }
52 return choices;
53}
54
55export function acpProviderOptionDescriptors(input: {
56 readonly configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> | null | undefined;
57 readonly modeState: AcpSessionModeState | undefined;
58}): ReadonlyArray<ProviderOptionDescriptor> {
59 const descriptors: Array<ProviderOptionDescriptor> = [];
60 const seen = new Set<string>();
61 const mirroredModeChoiceSets: Array<ReadonlySet<string>> = [];
62 let hasModeCategory = false;
63
64 for (const option of input.configOptions ?? []) {
65 // Model and collaboration_mode choices use T3 model and interaction controls.
66 if (option.category === "model" || option.category === "collaboration_mode") {
67 continue;
68 }
69 const id = boundedOpaqueValue(option.id, MAX_TEXT_LENGTH);
70 if (id === undefined || seen.has(id)) continue;
71 seen.add(id);
72 const description = boundedText(option.description, MAX_DESCRIPTION_LENGTH);
73 const base = {
74 id,
75 label: boundedText(option.name, MAX_TEXT_LENGTH) || id,
76 ...(description ? { description } : {}),
77 } as const;
78 if (option.type === "boolean") {
79 if (option.category === "mode") hasModeCategory = true;
80 descriptors.push({
81 ...base,
82 type: "boolean",
83 currentValue: option.currentValue,
84 });
85 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
86 continue;
87 }
88 const choices = selectChoices(flattenSelectChoices(option.options));
89 if (choices.length === 0) {
90 seen.delete(id);
91 continue;
92 }
93 if (option.category === "mode") hasModeCategory = true;
94 if (option.category === "thought_level") {
95 mirroredModeChoiceSets.push(new Set(choices.map((choice) => choice.id)));
96 }
97 const currentValue = choiceId(option.currentValue);
98 descriptors.push({
99 ...base,
100 type: "select",
101 options: choices,
102 ...(currentValue && choices.some((choice) => choice.id === currentValue)
103 ? { currentValue }
104 : {}),
105 });
106 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
107 }
108
109 const modeState = input.modeState;
110 if (modeState !== undefined && !hasModeCategory) {
111 const choices = selectChoices(
112 modeState.availableModes.map((mode) => ({
113 value: mode.id,
114 name: mode.name,
115 description: mode.description,
116 })),
117 );
118 const currentValue = choiceId(modeState.currentModeId);
119 // Some agents duplicate modes in config options; show each control once.
120 const duplicatesKnownModeDescriptor = mirroredModeChoiceSets.some(
121 (choiceIds) =>
122 choiceIds.size === choices.length && choices.every((choice) => choiceIds.has(choice.id)),
123 );
124 if (
125 choices.length > 1 &&
126 !duplicatesKnownModeDescriptor &&
127 !seen.has(ACP_SESSION_MODE_OPTION_ID)
128 ) {
129 descriptors.push({
130 id: ACP_SESSION_MODE_OPTION_ID,
131 label: "Mode",
132 description: "Session mode advertised by the ACP agent.",
133 type: "select",
134 options: choices,
135 ...(currentValue && choices.some((choice) => choice.id === currentValue)
136 ? { currentValue }
137 : {}),
138 });
139 }
140 }
141
142 return descriptors.slice(0, MAX_OPTION_DESCRIPTORS);
143}