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

sessionConfig.ts

162 lines6.0 KB

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​
22// Longer than MAX_TEXT_LENGTH, so no advertised value is ever offered under this ID.
23export const ACP_LITERAL_EMPTY_OPTION_VALUE = `${ACP_EMPTY_OPTION_VALUE}/`.padEnd(257, "_");
24const choiceId = (value: string): string | undefined =>
25 value === ""
26 ? ACP_EMPTY_OPTION_VALUE
27 : value === ACP_EMPTY_OPTION_VALUE
28 ? ACP_LITERAL_EMPTY_OPTION_VALUE
29 : boundedOpaqueValue(value, MAX_TEXT_LENGTH);
30​
31/** Mistral Vibe 3.1 sends its reasoning level under `thinking`, a category ACP reserves but never defined. */
32export const isAcpThoughtLevelOption = (option: EffectAcpSchema.SessionConfigOption): boolean =>
33 option.category === "thought_level" ||
34 (option.category === "thinking" && option.type === "select");
35​
36export function flattenSelectChoices(
37 options: EffectAcpSchema.SessionConfigSelectOptions,
38): ReadonlyArray<EffectAcpSchema.SessionConfigSelectOption> {
39 return options.flatMap((candidate) => ("value" in candidate ? [candidate] : candidate.options));
41​
42function selectChoices(
43 candidates: ReadonlyArray<{
44 readonly value: string;
45 readonly name: string;
46 readonly description?: string | null | undefined;
47 }>,
48): ReadonlyArray<ProviderOptionChoice> {
49 const seen = new Set<string>();
50 const choices: Array<ProviderOptionChoice> = [];
51 for (const candidate of candidates) {
52 const id = choiceId(candidate.value);
53 if (id === undefined || seen.has(id)) continue;
54 seen.add(id);
55 const description = boundedText(candidate.description, MAX_DESCRIPTION_LENGTH);
56 choices.push({
57 id,
58 label: boundedText(candidate.name, MAX_TEXT_LENGTH) || candidate.value || id,
59 ...(description ? { description } : {}),
60 });
61 if (choices.length === MAX_OPTION_CHOICES) break;
62 }
63 return choices;
65​
66export function acpProviderOptionDescriptors(input: {
67 readonly configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> | null | undefined;
68 readonly modeState: AcpSessionModeState | undefined;
69}): ReadonlyArray<ProviderOptionDescriptor> {
70 const descriptors: Array<ProviderOptionDescriptor> = [];
71 const seen = new Set<string>();
72 const mirroredModeChoiceSets: Array<ReadonlySet<string>> = [];
73 let hasModeCategory = false;
74​
75 for (const option of input.configOptions ?? []) {
76 // Model and collaboration_mode choices use T3 model and interaction controls.
77 if (option.category === "model" || option.category === "collaboration_mode") {
78 continue;
79 }
80 const id = boundedOpaqueValue(option.id, MAX_TEXT_LENGTH);
81 if (id === undefined || seen.has(id)) continue;
82 seen.add(id);
83 const description = boundedText(option.description, MAX_DESCRIPTION_LENGTH);
84 const base = {
85 id,
86 label: boundedText(option.name, MAX_TEXT_LENGTH) || id,
87 ...(description ? { description } : {}),
88 } as const;
89 if (option.type === "boolean") {
90 if (option.category === "mode") hasModeCategory = true;
91 descriptors.push({
92 ...base,
93 type: "boolean",
94 currentValue: option.currentValue,
95 ...(option.category === "mode" ? { role: "mode" as const } : {}),
96 });
97 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
98 continue;
99 }
100 const choices = selectChoices(flattenSelectChoices(option.options));
101 if (choices.length === 0) {
102 seen.delete(id);
103 continue;
104 }
105 if (option.category === "mode") hasModeCategory = true;
106 const isThoughtLevel = isAcpThoughtLevelOption(option);
107 if (isThoughtLevel) {
108 mirroredModeChoiceSets.push(new Set(choices.map((choice) => choice.id)));
109 }
110 const currentValue = choiceId(option.currentValue);
111 descriptors.push({
112 ...base,
113 type: "select",
114 options: choices,
115 ...(currentValue && choices.some((choice) => choice.id === currentValue)
116 ? { currentValue }
117 : {}),
118 ...(isThoughtLevel
119 ? { role: "reasoning" as const }
120 : option.category === "mode"
121 ? { role: "mode" as const }
122 : {}),
123 });
124 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
125 }
126​
127 const modeState = input.modeState;
128 if (modeState !== undefined && !hasModeCategory) {
129 const choices = selectChoices(
130 modeState.availableModes.map((mode) => ({
131 value: mode.id,
132 name: mode.name,
133 description: mode.description,
134 })),
135 );
136 const currentValue = choiceId(modeState.currentModeId);
137 // Some agents duplicate modes in config options; show each control once.
138 const duplicatesKnownModeDescriptor = mirroredModeChoiceSets.some(
139 (choiceIds) =>
140 choiceIds.size === choices.length && choices.every((choice) => choiceIds.has(choice.id)),
141 );
142 if (
143 choices.length > 1 &&
144 !duplicatesKnownModeDescriptor &&
145 !seen.has(ACP_SESSION_MODE_OPTION_ID)
146 ) {
147 descriptors.push({
148 id: ACP_SESSION_MODE_OPTION_ID,
149 label: "Mode",
150 description: "Session mode advertised by the ACP agent.",
151 type: "select",
152 options: choices,
153 ...(currentValue && choices.some((choice) => choice.id === currentValue)
154 ? { currentValue }
155 : {}),
156 role: "mode",
157 });
158 }
159 }
160​
161 return descriptors.slice(0, MAX_OPTION_DESCRIPTORS);