GeezerVison is showing github.com — feat(server): add a reasoning effort picker for Grok · pingdotgg/t3code@c7b7a54
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokAdapter.ts +32-2Lines changed: 32 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | type GrokSettings, | 4 | 4 | EventId, | 5 | 5 | type ProviderApprovalDecision, | | 6 | + type ProviderOptionSelection, | 6 | 7 | type ProviderRuntimeEvent, | 7 | 8 | type ProviderSession, | 8 | 9 | type ProviderUserInputAnswers, | @@ -32,6 +33,8 @@ import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawne | 32 | 33 | import * as EffectAcpErrors from "effect-acp/errors"; | 33 | 34 | import type * as EffectAcpSchema from "effect-acp/schema"; | 34 | 35 |
| | 36 | +import { getProviderOptionStringSelectionValue } from "@t3tools/shared/model"; | | 37 | + | 35 | 38 | import { resolveAttachmentPath } from "../../attachmentStore.ts"; | 36 | 39 | import { ServerConfig } from "../../config.ts"; | 37 | 40 | import * as McpProviderSession from "../../mcp/McpProviderSession.ts"; | @@ -56,6 +59,8 @@ import { makeAcpNativeLoggerFactory } from "../acp/AcpNativeLogging.ts"; | 56 | 59 | import { | 57 | 60 | applyGrokAcpModelSelection, | 58 | 61 | currentGrokModelIdFromSessionSetup, | | 62 | + currentGrokReasoningEffortFromSessionSetup, | | 63 | + GROK_REASONING_EFFORT_OPTION_ID, | 59 | 64 | makeGrokAcpRuntime, | 60 | 65 | resolveGrokAcpBaseModelId, | 61 | 66 | } from "../acp/GrokAcpSupport.ts"; | @@ -117,9 +122,24 @@ interface GrokSessionContext { | 117 | 122 | * continues it, and only the last remaining prompt settles the turn. */ | 118 | 123 | promptsInFlight: number; | 119 | 124 | currentModelId: string | undefined; | | 125 | + /** Reasoning effort last sent through `session/set_model`. */ | | 126 | + currentReasoningEffort: string | undefined; | 120 | 127 | stopped: boolean; | 121 | 128 | } | 122 | 129 |
| | 130 | +function resolveRequestedGrokReasoningEffort( | | 131 | + modelSelection: | | 132 | + | { readonly options?: ReadonlyArray<ProviderOptionSelection> | null | undefined } | | 133 | + | undefined, | | 134 | +): string | undefined { | | 135 | + return ( | | 136 | + getProviderOptionStringSelectionValue( | | 137 | + modelSelection?.options, | | 138 | + GROK_REASONING_EFFORT_OPTION_ID, | | 139 | + ) ?? undefined | | 140 | + ); | | 141 | +} | | 142 | + | 123 | 143 | function settlePendingApprovalsAsCancelled( | 124 | 144 | pendingApprovals: ReadonlyMap<ApprovalRequestId, PendingApproval>, | 125 | 145 | ): Effect.Effect<void> { | @@ -738,13 +758,18 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 738 | 758 | const requestedStartModelId = grokModelSelection?.model | 739 | 759 | ? resolveGrokAcpBaseModelId(grokModelSelection.model) | 740 | 760 | : undefined; | 741 | | - const boundModelId = yield* applyGrokAcpModelSelection({ | | 761 | + const bound = yield* applyGrokAcpModelSelection({ | 742 | 762 | runtime: acp, | 743 | 763 | currentModelId: currentGrokModelIdFromSessionSetup(started.sessionSetupResult), | 744 | 764 | requestedModelId: requestedStartModelId, | | 765 | + currentReasoningEffort: currentGrokReasoningEffortFromSessionSetup( | | 766 | + started.sessionSetupResult, | | 767 | + ), | | 768 | + requestedReasoningEffort: resolveRequestedGrokReasoningEffort(grokModelSelection), | 745 | 769 | mapError: (cause) => | 746 | 770 | mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_model", cause), | 747 | 771 | }); | | 772 | + const boundModelId = bound.modelId; | 748 | 773 |
| 749 | 774 | const now = yield* nowIso; | 750 | 775 | const session: ProviderSession = { | @@ -778,6 +803,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 778 | 803 | interruptedTurnIds: new Set(), | 779 | 804 | promptsInFlight: 0, | 780 | 805 | currentModelId: boundModelId, | | 806 | + currentReasoningEffort: bound.reasoningEffort, | 781 | 807 | stopped: false, | 782 | 808 | }; | 783 | 809 |
| @@ -942,13 +968,16 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 942 | 968 | const requestedTurnModelId = turnModelSelection?.model | 943 | 969 | ? resolveGrokAcpBaseModelId(turnModelSelection.model) | 944 | 970 | : undefined; | 945 | | - const currentModelId = yield* applyGrokAcpModelSelection({ | | 971 | + const turnSelection = yield* applyGrokAcpModelSelection({ | 946 | 972 | runtime: ctx.acp, | 947 | 973 | currentModelId: ctx.currentModelId, | 948 | 974 | requestedModelId: requestedTurnModelId, | | 975 | + currentReasoningEffort: ctx.currentReasoningEffort, | | 976 | + requestedReasoningEffort: resolveRequestedGrokReasoningEffort(turnModelSelection), | 949 | 977 | mapError: (cause) => | 950 | 978 | mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_model", cause), | 951 | 979 | }); | | 980 | + const currentModelId = turnSelection.modelId; | 952 | 981 |
| 953 | 982 | const text = input.input?.trim(); | 954 | 983 | const imagePromptParts = yield* Effect.forEach( | @@ -998,6 +1027,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte | 998 | 1027 | } | 999 | 1028 |
| 1000 | 1029 | ctx.currentModelId = currentModelId; | | 1030 | + ctx.currentReasoningEffort = turnSelection.reasoningEffort; | 1001 | 1031 | const displayModel = currentModelId | 1002 | 1032 | ? resolveGrokAcpBaseModelId(currentModelId) | 1003 | 1033 | : undefined; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokProvider.test.ts +34-1Lines changed: 34 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,10 +6,43 @@ import * as Path from "effect/Path"; | 6 | 6 | import * as Schema from "effect/Schema"; | 7 | 7 | import { GrokSettings } from "@t3tools/contracts"; | 8 | 8 |
| 9 | | -import { buildInitialGrokProviderSnapshot, checkGrokProviderStatus } from "./GrokProvider.ts"; | | 9 | +import { | | 10 | + buildGrokModelCapabilities, | | 11 | + buildInitialGrokProviderSnapshot, | | 12 | + checkGrokProviderStatus, | | 13 | +} from "./GrokProvider.ts"; | 10 | 14 |
| 11 | 15 | const decodeGrokSettings = Schema.decodeSync(GrokSettings); | 12 | 16 |
| | 17 | +describe("buildGrokModelCapabilities", () => { | | 18 | + it("exposes the reasoning picker advertised by the model metadata", () => { | | 19 | + const capabilities = buildGrokModelCapabilities({ | | 20 | + supportsReasoningEffort: true, | | 21 | + reasoningEffort: "high", | | 22 | + reasoningEfforts: [ | | 23 | + { id: "high", value: "high", label: "High Effort", default: true }, | | 24 | + { id: "low", value: "low", label: "Low Effort", default: false }, | | 25 | + ], | | 26 | + }); | | 27 | + expect(capabilities.optionDescriptors).toEqual([ | | 28 | + { | | 29 | + id: "reasoningEffort", | | 30 | + label: "Reasoning", | | 31 | + type: "select", | | 32 | + options: [ | | 33 | + { id: "high", label: "High Effort", isDefault: true }, | | 34 | + { id: "low", label: "Low Effort" }, | | 35 | + ], | | 36 | + currentValue: "high", | | 37 | + }, | | 38 | + ]); | | 39 | + }); | | 40 | + | | 41 | + it("exposes no options for models without reasoning metadata", () => { | | 42 | + expect(buildGrokModelCapabilities(null).optionDescriptors).toEqual([]); | | 43 | + }); | | 44 | +}); | | 45 | + | 13 | 46 | describe("buildInitialGrokProviderSnapshot", () => { | 14 | 47 | it.effect("returns a disabled snapshot when settings.enabled is false", () => | 15 | 48 | Effect.gen(function* () { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/Layers/GrokProvider.ts +28-2Lines changed: 28 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import { createModelCapabilities } from "@t3tools/shared/model"; | 18 | 18 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; | 19 | 19 |
| 20 | 20 | import { | | 21 | + buildSelectOptionDescriptor, | 21 | 22 | buildServerProvider, | 22 | 23 | isCommandMissingCause, | 23 | 24 | parseGenericCliVersion, | @@ -29,7 +30,12 @@ import { | 29 | 30 | enrichProviderSnapshotWithVersionAdvisory, | 30 | 31 | type ProviderMaintenanceCapabilities, | 31 | 32 | } from "../providerMaintenance.ts"; | 32 | | -import { makeGrokAcpRuntime, resolveGrokAcpBaseModelId } from "../acp/GrokAcpSupport.ts"; | | 33 | +import { | | 34 | + GROK_REASONING_EFFORT_OPTION_ID, | | 35 | + grokReasoningEffortLevelsFromModelMeta, | | 36 | + makeGrokAcpRuntime, | | 37 | + resolveGrokAcpBaseModelId, | | 38 | +} from "../acp/GrokAcpSupport.ts"; | 33 | 39 |
| 34 | 40 | const GROK_PRESENTATION = { | 35 | 41 | displayName: "Grok", | @@ -99,6 +105,26 @@ function grokModelsFromSettings( | 99 | 105 | return providerModelsFromSettings(builtInModels, customModels ?? [], EMPTY_CAPABILITIES); | 100 | 106 | } | 101 | 107 |
| | 108 | +export function buildGrokModelCapabilities(meta: unknown | null | undefined): ModelCapabilities { | | 109 | + const reasoningEffortLevels = grokReasoningEffortLevelsFromModelMeta(meta); | | 110 | + if (reasoningEffortLevels.length === 0) { | | 111 | + return EMPTY_CAPABILITIES; | | 112 | + } | | 113 | + return createModelCapabilities({ | | 114 | + optionDescriptors: [ | | 115 | + buildSelectOptionDescriptor({ | | 116 | + id: GROK_REASONING_EFFORT_OPTION_ID, | | 117 | + label: "Reasoning", | | 118 | + options: reasoningEffortLevels.map((level) => ({ | | 119 | + value: level.value, | | 120 | + label: level.label, | | 121 | + ...(level.isDefault ? { isDefault: true } : {}), | | 122 | + })), | | 123 | + }), | | 124 | + ], | | 125 | + }); | | 126 | +} | | 127 | + | 102 | 128 | function buildGrokDiscoveredModelsFromSessionModelState( | 103 | 129 | modelState: EffectAcpSchema.SessionModelState | null | undefined, | 104 | 130 | ): ReadonlyArray<ServerProviderModel> { | @@ -117,7 +143,7 @@ function buildGrokDiscoveredModelsFromSessionModelState( | 117 | 143 | slug, | 118 | 144 | name: model.name.trim() || slug, | 119 | 145 | isCustom: false, | 120 | | - capabilities: EMPTY_CAPABILITIES, | | 146 | + capabilities: buildGrokModelCapabilities(model._meta), | 121 | 147 | }; | 122 | 148 | }) | 123 | 149 | .filter((model): model is ServerProviderModel => model !== undefined); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/acp/AcpSessionRuntime.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -226,6 +226,10 @@ export class AcpSessionRuntime extends Context.Service< | 226 | 226 | */ | 227 | 227 | readonly setSessionModel: ( | 228 | 228 | modelId: string, | | 229 | + options?: { | | 230 | + /** Agent-specific request metadata, merged into the request `_meta`. */ | | 231 | + readonly meta?: Readonly<Record<string, unknown>>; | | 232 | + }, | 229 | 233 | ) => Effect.Effect<EffectAcpSchema.SetSessionModelResponse, EffectAcpErrors.AcpError>; | 230 | 234 | /** | 231 | 235 | * Sends a generic ACP extension request and records it through the request logger. | @@ -789,12 +793,14 @@ export const make = ( | 789 | 793 | Effect.flatMap((started) => setConfigOption(started.modelConfigId ?? "model", model)), | 790 | 794 | Effect.asVoid, | 791 | 795 | ), | 792 | | - setSessionModel: (modelId) => | | 796 | + setSessionModel: (modelId, setModelOptions) => | 793 | 797 | getStartedState.pipe( | 794 | 798 | Effect.flatMap((started) => { | | 799 | + const meta = setModelOptions?.meta; | 795 | 800 | const requestPayload = { | 796 | 801 | sessionId: started.sessionId, | 797 | 802 | modelId, | | 803 | + ...(meta && Object.keys(meta).length > 0 ? { _meta: meta } : {}), | 798 | 804 | } satisfies EffectAcpSchema.SetSessionModelRequest; | 799 | 805 | return runLoggedRequest( | 800 | 806 | "session/set_model", | |
|