@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 150 lines 5.53 KB
Raw
1 import type { ProviderOptionChoice, ProviderOptionDescriptor } from "@t3tools/contracts";
2 import type * as EffectAcpSchema from "effect-acp/compat";
3
4 import type { AcpSessionModeState } from "./runtimeModel.ts";
5
6 export 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. */
9 export const ACP_EMPTY_OPTION_VALUE = "_t3/empty";
10
11 const MAX_OPTION_DESCRIPTORS = 16;
12 const MAX_OPTION_CHOICES = 64;
13 const MAX_TEXT_LENGTH = 256;
14 const MAX_DESCRIPTION_LENGTH = 1_024;
15
16 const boundedText = (value: string | null | undefined, maximumLength: number): string =>
17 (value ?? "").trim().slice(0, maximumLength);
18
19 const boundedOpaqueValue = (value: string, maximumLength: number): string | undefined =>
20 value.length > 0 && value === value.trim() && value.length <= maximumLength ? value : undefined;
21
22 const choiceId = (value: string): string | undefined =>
23 value === "" ? ACP_EMPTY_OPTION_VALUE : boundedOpaqueValue(value, MAX_TEXT_LENGTH);
24
25 /** Mistral Vibe 3.1 sends its reasoning level under `thinking`, a category ACP reserves but never defined. */
26 export const isAcpThoughtLevelOption = (option: EffectAcpSchema.SessionConfigOption): boolean =>
27 option.category === "thought_level" ||
28 (option.category === "thinking" && option.type === "select");
29
30 export function flattenSelectChoices(
31 options: EffectAcpSchema.SessionConfigSelectOptions,
32 ): ReadonlyArray<EffectAcpSchema.SessionConfigSelectOption> {
33 return options.flatMap((candidate) => ("value" in candidate ? [candidate] : candidate.options));
34 }
35
36 function selectChoices(
37 candidates: ReadonlyArray<{
38 readonly value: string;
39 readonly name: string;
40 readonly description?: string | null | undefined;
41 }>,
42 ): ReadonlyArray<ProviderOptionChoice> {
43 const seen = new Set<string>();
44 const choices: Array<ProviderOptionChoice> = [];
45 for (const candidate of candidates) {
46 const id = choiceId(candidate.value);
47 if (id === undefined || seen.has(id)) continue;
48 seen.add(id);
49 const description = boundedText(candidate.description, MAX_DESCRIPTION_LENGTH);
50 choices.push({
51 id,
52 label: boundedText(candidate.name, MAX_TEXT_LENGTH) || id,
53 ...(description ? { description } : {}),
54 });
55 if (choices.length === MAX_OPTION_CHOICES) break;
56 }
57 return choices;
58 }
59
60 export function acpProviderOptionDescriptors(input: {
61 readonly configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> | null | undefined;
62 readonly modeState: AcpSessionModeState | undefined;
63 }): ReadonlyArray<ProviderOptionDescriptor> {
64 const descriptors: Array<ProviderOptionDescriptor> = [];
65 const seen = new Set<string>();
66 const mirroredModeChoiceSets: Array<ReadonlySet<string>> = [];
67 let hasModeCategory = false;
68
69 for (const option of input.configOptions ?? []) {
70 // Model and collaboration_mode choices use T3 model and interaction controls.
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 const isThoughtLevel = isAcpThoughtLevelOption(option);
100 if (isThoughtLevel) {
101 mirroredModeChoiceSets.push(new Set(choices.map((choice) => choice.id)));
102 }
103 const currentValue = choiceId(option.currentValue);
104 descriptors.push({
105 ...base,
106 type: "select",
107 options: choices,
108 ...(currentValue && choices.some((choice) => choice.id === currentValue)
109 ? { currentValue }
110 : {}),
111 ...(isThoughtLevel ? { role: "reasoning" as const } : {}),
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 = choiceId(modeState.currentModeId);
126 // Some agents duplicate modes in config options; show each control once.
127 const duplicatesKnownModeDescriptor = mirroredModeChoiceSets.some(
128 (choiceIds) =>
129 choiceIds.size === choices.length && choices.every((choice) => choiceIds.has(choice.id)),
130 );
131 if (
132 choices.length > 1 &&
133 !duplicatesKnownModeDescriptor &&
134 !seen.has(ACP_SESSION_MODE_OPTION_ID)
135 ) {
136 descriptors.push({
137 id: ACP_SESSION_MODE_OPTION_ID,
138 label: "Mode",
139 description: "Session mode advertised by the ACP agent.",
140 type: "select",
141 options: choices,
142 ...(currentValue && choices.some((choice) => choice.id === currentValue)
143 ? { currentValue }
144 : {}),
145 });
146 }
147 }
148
149 return descriptors.slice(0, MAX_OPTION_DESCRIPTORS);
150 }