@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 135 lines 4.92 KB
Raw
1 import {
2 type ModelCapabilities,
3 type ProviderOptionChoice,
4 type ProviderOptionDescriptor,
5 } from "@t3tools/contracts";
6 import { createModelCapabilities } from "@t3tools/shared/model";
7 import * as Predicate from "effect/Predicate";
8
9 // xhigh/max require per-model thinkingLevelMap opt-in; global advertising makes set_thinking_level fail.
10 const PI_THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
11
12 export type PiThinkingLevel = (typeof PI_THINKING_LEVELS)[number];
13
14 export function isPiThinkingLevel(value: string): value is PiThinkingLevel {
15 return PI_THINKING_LEVELS.some((level) => level === value);
16 }
17
18 const PI_THINKING_LEVEL_LABELS: Record<PiThinkingLevel, string> = {
19 off: "Off",
20 minimal: "Minimal",
21 low: "Low",
22 medium: "Medium",
23 high: "High",
24 xhigh: "Extra High",
25 max: "Max",
26 };
27
28 export const EMPTY_PI_MODEL_CAPABILITIES: ModelCapabilities = createModelCapabilities({
29 optionDescriptors: [],
30 });
31
32 /** Selecting Standard sends no `service_tier`, leaving the endpoint's default. */
33 export const PI_STANDARD_SERVICE_TIER = "default";
34
35 export function piModelCapabilities(
36 model: unknown,
37 defaultThinkingLevel: unknown,
38 ): ModelCapabilities {
39 const optionDescriptors = [
40 thinkingDescriptor(model, defaultThinkingLevel),
41 serviceTierDescriptor(model),
42 ].filter(Predicate.isNotUndefined);
43 return optionDescriptors.length === 0
44 ? EMPTY_PI_MODEL_CAPABILITIES
45 : createModelCapabilities({ optionDescriptors });
46 }
47
48 function thinkingDescriptor(
49 model: unknown,
50 defaultThinkingLevel: unknown,
51 ): ProviderOptionDescriptor | undefined {
52 const levels = supportedPiThinkingLevelsFromModel(model);
53 if (levels.length === 0) return undefined;
54 const defaultLevel = clampPiThinkingLevel(defaultThinkingLevel, levels);
55 return {
56 id: "thinking",
57 label: "Thinking",
58 type: "select",
59 options: levels.map((level): ProviderOptionChoice => ({
60 id: level,
61 label: PI_THINKING_LEVEL_LABELS[level],
62 ...(level === defaultLevel ? { isDefault: true } : {}),
63 })),
64 };
65 }
66
67 // Pi has no service tier concept; provider extensions advertise `serviceTiers` on a model,
68 // and T3's injected extension sends the chosen id as the request's `service_tier`.
69 function serviceTierDescriptor(model: unknown): ProviderOptionDescriptor | undefined {
70 const tiers = recordField(model, "serviceTiers");
71 if (!Array.isArray(tiers)) return undefined;
72 const seen = new Set<string>([PI_STANDARD_SERVICE_TIER]);
73 const options: Array<ProviderOptionChoice> = [];
74 for (const tier of tiers) {
75 const id = recordField(tier, "id");
76 if (typeof id !== "string" || id.trim() === "" || seen.has(id)) continue;
77 seen.add(id);
78 const name = recordField(tier, "name");
79 const description = recordField(tier, "description");
80 options.push({
81 id,
82 label: typeof name === "string" && name.trim() !== "" ? name : id,
83 ...(typeof description === "string" && description.trim() !== "" ? { description } : {}),
84 });
85 }
86 if (options.length === 0) return undefined;
87 return {
88 id: "serviceTier",
89 label: "Service Tier",
90 type: "select",
91 options: [{ id: PI_STANDARD_SERVICE_TIER, label: "Standard", isDefault: true }, ...options],
92 currentValue: PI_STANDARD_SERVICE_TIER,
93 };
94 }
95
96 function clampPiThinkingLevel(
97 input: unknown,
98 availableLevels: ReadonlyArray<PiThinkingLevel>,
99 ): PiThinkingLevel | undefined {
100 if (typeof input !== "string") return undefined;
101 const requestedIndex = PI_THINKING_LEVELS.findIndex((level) => level === input);
102 if (requestedIndex === -1) return undefined;
103 const exact = availableLevels.find((level) => level === input);
104 if (exact !== undefined) return exact;
105 for (let index = requestedIndex + 1; index < PI_THINKING_LEVELS.length; index += 1) {
106 const higher = availableLevels.find((level) => level === PI_THINKING_LEVELS[index]);
107 if (higher !== undefined) return higher;
108 }
109 for (let index = requestedIndex - 1; index >= 0; index -= 1) {
110 const lower = availableLevels.find((level) => level === PI_THINKING_LEVELS[index]);
111 if (lower !== undefined) return lower;
112 }
113 return availableLevels[0];
114 }
115
116 function supportedPiThinkingLevelsFromModel(model: unknown): ReadonlyArray<PiThinkingLevel> {
117 if (recordField(model, "reasoning") !== true) return [];
118 const thinkingLevelMap = thinkingLevelMapFromModel(model);
119 return PI_THINKING_LEVELS.filter((level) => {
120 const mapped = thinkingLevelMap?.[level];
121 if (mapped === null) return false;
122 if (level === "xhigh" || level === "max") return mapped !== undefined;
123 return true;
124 });
125 }
126
127 function thinkingLevelMapFromModel(model: unknown): Record<string, unknown> | undefined {
128 const value = recordField(model, "thinkingLevelMap");
129 if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
130 return value as Record<string, unknown>;
131 }
132
133 function recordField(input: unknown, key: string): unknown {
134 return Predicate.isObject(input) ? input[key] : undefined;
135 }