feat(server): Grok Build effort, auth, rewind, and usage [WIP - for content] #6383
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromt3code/audit-grok-build-gapsopened View on GitHubt3dotgg(GitHub profile, opens in a new tab) added labels[WIP - for content]
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/provider/Layers/GrokAdapter.ts line 1466Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1399,2 +1457,2 @@1399145714001458 const rollbackThread: GrokAdapterShape["rollbackThread"] = (threadId, numTurns) =>apps/server/src/provider/Layers/GrokAdapter.ts line 1003Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -946,6 +978,21 @@978+ (requestedTurnModelId979+ ? ctx.reasoningEffortMenus.get(requestedTurnModelId)980+ : undefined) ??981+ (ctx.currentModelId982+ ? ctx.reasoningEffortMenus.get(ctx.currentModelId)983+ : undefined) ??984+ [];985+ const turnSelection = yield* applyGrokAcpModelSelection({946986 runtime: ctx.acp,947987 currentModelId: ctx.currentModelId,948988 requestedModelId: requestedTurnModelId,989+ currentReasoningEffort: ctx.currentReasoningEffort,990+ requestedReasoningEffort: requestedGrokReasoningEffort(991+ turnModelSelection,992+ advertisedTurnEfforts,993+ ),949994 mapError: (cause) =>950995 mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_model", cause),951996 });997+ const currentModelId = turnSelection.modelId;998+ ctx.currentReasoningEffort = turnSelection.reasoningEffort;apps/server/src/provider/acp/GrokAcpSupport.ts lines 215 to 219OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -93,0 +199,21 @@199+ unique.set(choice.id, choice);200+ }201+ }202+ const choices = [...unique.values()];203+ const current = trimmedString(meta.reasoningEffort);204+ const supportsReasoningEffort = meta.supportsReasoningEffort === true || choices.length > 0;205+ const totalContextTokens =206+ typeof meta.totalContextTokens === "number" &&207+ Number.isFinite(meta.totalContextTokens) &&208+ meta.totalContextTokens > 0209+ ? Math.trunc(meta.totalContextTokens)210+ : undefined;211+212+ return {213+ supportsReasoningEffort,214+ ...(current ? { reasoningEffort: current } : {}),215+ reasoningEfforts: choices.map((choice) =>216+ current && choice.id === current && choice.isDefault !== true217+ ? { ...choice, isDefault: true }218+ : choice,219+ ),apps/server/src/provider/acp/GrokAcpSupport.ts lines 349 to 349OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -94,6 +330,20 @@330+ const text = [331+ error instanceof Error ? `${error.name} ${error.message}` : "",332+ typeof error === "string" ? error : "",333+ String(error),334+ ].join(" ");335+ return /authenticat|unauthorized|not logged in|login required|no credentials|cached_token|xai\.api_key|401\b/i.test(336+ text,337+ );338+}339+94340 export function applyGrokAcpModelSelection<E>(input: {95341 readonly runtime: Pick<AcpSessionRuntime.AcpSessionRuntime["Service"], "setSessionModel">;96342 readonly currentModelId: string | undefined;97343 readonly requestedModelId: string | undefined;344+ readonly currentReasoningEffort?: string;345+ readonly requestedReasoningEffort?: string;98346 readonly mapError: (cause: EffectAcpErrors.AcpError) => E;99-}): Effect.Effect<string | undefined, E> {347+}): Effect.Effect<GrokAcpSelection, E> {348+ const nextModelId = input.requestedModelId ?? input.currentModelId;349+ const nextEffort = input.requestedReasoningEffort ?? input.currentReasoningEffort;apps/server/src/provider/acp/XAiAcpExtension.ts lines 508 to 510OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -432,0 +490,21 @@490+ if (491+ /^\s*<system-reminder>/.test(preview) ||492+ /^\s*\[Plan (approved|rejected|cancelled)\]\s*$/i.test(preview.trim())493+ ) {494+ return [];495+ }496+ return [{ promptIndex, promptPreview: preview }];497+ });498+}499+500+/** Target for rolling back the last `numTurns` user prompts. Execute discards the target and everything after it. */501+export function grokRewindTargetForTurnCount(502+ points: ReadonlyArray<GrokRewindPoint>,503+ numTurns: number,504+): GrokRewindPoint | undefined {505+ if (!Number.isInteger(numTurns) || numTurns < 1 || points.length === 0) {506+ return undefined;507+ }508+ const ordered = [...points].sort((left, right) => left.promptIndex - right.promptIndex);509+ const target = ordered[Math.max(0, ordered.length - numTurns)];510+ return target;apps/server/src/provider/Layers/GrokAdapter.ts line 812OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -778,3 +805,8 @@778805 interruptedTurnIds: new Set(),779806 promptsInFlight: 0,780807 currentModelId: boundModelId,808+ currentReasoningEffort: boundSelection.reasoningEffort,809+ reasoningEffortMenus: grokReasoningEffortMenusFromSessionSetup(810+ started.sessionSetupResult,811+ ),812+ maxTokens: currentGrokMaxTokensFromSessionSetup(started.sessionSetupResult),
- apps/server/src/provider/acp/GrokAcpSupport.ts line 396Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -104,4 +355,19 @@355+356+ if (!shouldSwitchModel && !shouldSwitchEffort) {357+ return Effect.succeed({358+ modelId: input.currentModelId,359+ reasoningEffort: input.currentReasoningEffort,360+ });104361 }362+363+ if (nextModelId === undefined) {364+ return Effect.succeed({365+ modelId: undefined,366+ reasoningEffort: nextEffort,367+ });368+ }369+105370 return input.runtime106- .setSessionModel(input.requestedModelId)107- .pipe(Effect.mapError(input.mapError), Effect.as(input.requestedModelId));371+ .setSessionModel(372+ nextModelId,373+ nextEffort ? { _meta: { reasoningEffort: nextEffort } } : undefined,apps/server/src/provider/Layers/GrokAdapter.ts line 1505OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1414,0 +1485,21 @@1485+ const executePayload = yield* ctx.acp1486+ .request("_x.ai/rewind/execute", {1487+ sessionId: ctx.acpSessionId,1488+ targetPromptIndex: target.promptIndex,1489+ mode: "conversation_only",1490+ force: true,1491+ })1492+ .pipe(1493+ Effect.mapError((error) =>1494+ mapAcpToAdapterError(PROVIDER, threadId, "_x.ai/rewind/execute", error),1495+ ),1496+ );1497+ const executed = parseGrokRewindExecute(executePayload);1498+ if (!executed?.success) {1499+ return yield* new ProviderAdapterRequestError({1500+ provider: PROVIDER,1501+ method: "_x.ai/rewind/execute",1502+ detail: executed?.error ?? "Grok rewind did not succeed.",1503+ });1504+ }1505+ ctx.turns = ctx.turns.slice(0, Math.max(0, ctx.turns.length - numTurns));apps/server/src/provider/acp/XAiAcpExtension.ts line 510OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -432,0 +490,21 @@490+ if (491+ /^\s*<system-reminder>/.test(preview) ||492+ /^\s*\[Plan (approved|rejected|cancelled)\]\s*$/i.test(preview.trim())493+ ) {494+ return [];495+ }496+ return [{ promptIndex, promptPreview: preview }];497+ });498+}499+500+/** Target for rolling back the last `numTurns` user prompts. Execute discards the target and everything after it. */501+export function grokRewindTargetForTurnCount(502+ points: ReadonlyArray<GrokRewindPoint>,503+ numTurns: number,504+): GrokRewindPoint | undefined {505+ if (!Number.isInteger(numTurns) || numTurns < 1 || points.length === 0) {506+ return undefined;507+ }508+ const ordered = [...points].sort((left, right) => left.promptIndex - right.promptIndex);509+ const target = ordered[Math.max(0, ordered.length - numTurns)];510+ return target;
- @@ -93,0 +248,21 @@248+ });249+}250+251+export function fallbackGrokReasoningEffortCapabilities(): ModelCapabilities {252+ return grokReasoningEffortCapabilities([...FALLBACK_GROK_REASONING_EFFORTS]);253+}254+255+export function requestedGrokReasoningEffort(256+ modelSelection: ModelSelection | null | undefined,257+ advertised: ReadonlyArray<string>,258+): string | undefined {259+ const requested = getModelSelectionStringOptionValue(260+ modelSelection,261+ GROK_REASONING_EFFORT_OPTION_ID,262+ )?.trim();263+ if (!requested) {264+ return undefined;265+ }266+ if (advertised.includes(requested)) {267+ return requested;268+ }@@ -93,0 +185,21 @@185+export function parseGrokAcpModelMeta(meta: unknown): GrokAcpModelMeta {186+ if (!isRecord(meta)) {187+ return { supportsReasoningEffort: false, reasoningEfforts: [] };188+ }189+190+ const reasoningEfforts = Array.isArray(meta.reasoningEfforts)191+ ? meta.reasoningEfforts.flatMap((entry) => {192+ const choice = parseGrokReasoningEffortChoice(entry);193+ return choice ? [choice] : [];194+ })195+ : [];196+ const unique = new Map<string, GrokReasoningEffortChoice>();197+ for (const choice of reasoningEfforts) {198+ if (!unique.has(choice.id)) {199+ unique.set(choice.id, choice);200+ }201+ }202+ const choices = [...unique.values()];203+ const current = trimmedString(meta.reasoningEffort);204+ const defaultId = current ?? choices.find((choice) => choice.isDefault)?.id;205+ const supportsReasoningEffort = meta.supportsReasoningEffort === true || choices.length > 0;
- @@ -93,0 +246,21 @@246+ },247+ ],248+ });249+}250+251+export function fallbackGrokReasoningEffortCapabilities(): ModelCapabilities {252+ return grokReasoningEffortCapabilities([...FALLBACK_GROK_REASONING_EFFORTS]);253+}254+255+export function requestedGrokReasoningEffort(256+ modelSelection: ModelSelection | null | undefined,257+ advertised: ReadonlyArray<string>,258+): string | undefined {259+ const requested = getModelSelectionStringOptionValue(260+ modelSelection,261+ GROK_REASONING_EFFORT_OPTION_ID,262+ )?.trim();263+ if (!requested) {264+ return undefined;265+ }266+ if (advertised.includes(requested)) {
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
22ccb553-a164-4214-8cfb-1cc0715b492fYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share