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

sessionConfig.ts

156 lines5.7 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​
22const 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. */
26export const isAcpThoughtLevelOption = (option: EffectAcpSchema.SessionConfigOption): boolean =>
27 option.category === "thought_level" ||
28 (option.category === "thinking" && option.type === "select");
29​
30export function flattenSelectChoices(
31 options: EffectAcpSchema.SessionConfigSelectOptions,
32): ReadonlyArray<EffectAcpSchema.SessionConfigSelectOption> {
33 return options.flatMap((candidate) => ("value" in candidate ? [candidate] : candidate.options));
35​
36function 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;
59​
60export 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 ...(option.category === "mode" ? { role: "mode" as const } : {}),
90 });
91 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
92 continue;
93 }
94 const choices = selectChoices(flattenSelectChoices(option.options));
95 if (choices.length === 0) {
96 seen.delete(id);
97 continue;
98 }
99 if (option.category === "mode") hasModeCategory = true;
100 const isThoughtLevel = isAcpThoughtLevelOption(option);
101 if (isThoughtLevel) {
102 mirroredModeChoiceSets.push(new Set(choices.map((choice) => choice.id)));
103 }
104 const currentValue = choiceId(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 ...(isThoughtLevel
113 ? { role: "reasoning" as const }
114 : option.category === "mode"
115 ? { role: "mode" as const }
116 : {}),
117 });
118 if (descriptors.length === MAX_OPTION_DESCRIPTORS) return descriptors;
119 }
120​
121 const modeState = input.modeState;
122 if (modeState !== undefined && !hasModeCategory) {
123 const choices = selectChoices(
124 modeState.availableModes.map((mode) => ({
125 value: mode.id,
126 name: mode.name,
127 description: mode.description,
128 })),
129 );
130 const currentValue = choiceId(modeState.currentModeId);
131 // Some agents duplicate modes in config options; show each control once.
132 const duplicatesKnownModeDescriptor = mirroredModeChoiceSets.some(
133 (choiceIds) =>
134 choiceIds.size === choices.length && choices.every((choice) => choiceIds.has(choice.id)),
135 );
136 if (
137 choices.length > 1 &&
138 !duplicatesKnownModeDescriptor &&
139 !seen.has(ACP_SESSION_MODE_OPTION_ID)
140 ) {
141 descriptors.push({
142 id: ACP_SESSION_MODE_OPTION_ID,
143 label: "Mode",
144 description: "Session mode advertised by the ACP agent.",
145 type: "select",
146 options: choices,
147 ...(currentValue && choices.some((choice) => choice.id === currentValue)
148 ? { currentValue }
149 : {}),
150 role: "mode",
151 });
152 }
153 }
154​
155 return descriptors.slice(0, MAX_OPTION_DESCRIPTORS);