-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(cli): apply cloud-recommended default thinking effort #3616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Upgrade the default thinking effort to the recommended level for eligible users. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { | ||
| getClientConfig, | ||
| peekClientConfig, | ||
| resetClientConfigCache, | ||
| type ClientConfigFetchOptions, | ||
| } from '#/utils/client-configs'; | ||
|
|
||
| const CONFIG_NAME = 'recommended_effort'; | ||
|
|
||
| export interface RecommendedEffortEntry { | ||
| version: number; | ||
| recommended_default_effort: string; | ||
| } | ||
|
|
||
| export type RecommendedEffortConfig = Record<string, RecommendedEffortEntry>; | ||
|
|
||
| const recommendedEffortEntrySchema = z.object({ | ||
| version: z.number().int().min(0), | ||
| recommended_default_effort: z.string(), | ||
| }); | ||
|
|
||
| const recommendedEffortConfigSchema = z | ||
| .record(z.string(), z.unknown()) | ||
| .transform((record): RecommendedEffortConfig => { | ||
| const config: RecommendedEffortConfig = {}; | ||
| for (const [model, rawEntry] of Object.entries(record)) { | ||
| const parsed = recommendedEffortEntrySchema.safeParse(rawEntry); | ||
| if (parsed.success) config[model] = parsed.data; | ||
| } | ||
| return config; | ||
| }); | ||
|
|
||
| export async function getRecommendedEffortConfig( | ||
| options: ClientConfigFetchOptions = {}, | ||
| ): Promise<RecommendedEffortConfig | undefined> { | ||
| return getClientConfig(CONFIG_NAME, recommendedEffortConfigSchema, options); | ||
|
Grapedge marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function peekRecommendedEffortConfig(now?: number): RecommendedEffortConfig | undefined { | ||
| return peekClientConfig(CONFIG_NAME, recommendedEffortConfigSchema, now); | ||
| } | ||
|
|
||
| export function resetRecommendedEffortConfigCache(): void { | ||
| resetClientConfigCache(CONFIG_NAME); | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
apps/kimi-code/src/utils/recommended-effort-state-store.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { getRecommendedEffortStateFile } from '#/utils/paths'; | ||
| import { readJsonFile, writeJsonFileSync } from '#/utils/persistence'; | ||
|
|
||
| const RecommendedEffortStateSchema = z.record( | ||
| z.string(), | ||
| z.object({ | ||
| version: z.number().int().min(0), | ||
| applied_at: z.string(), | ||
| }), | ||
| ); | ||
|
|
||
| export type RecommendedEffortState = z.infer<typeof RecommendedEffortStateSchema>; | ||
|
|
||
| export async function readRecommendedEffortState( | ||
| filePath: string = getRecommendedEffortStateFile(), | ||
| ): Promise<RecommendedEffortState> { | ||
| try { | ||
| return await readJsonFile(filePath, RecommendedEffortStateSchema, {}); | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| export function writeRecommendedEffortState( | ||
| state: RecommendedEffortState, | ||
| filePath: string = getRecommendedEffortStateFile(), | ||
| ): void { | ||
| writeJsonFileSync(filePath, RecommendedEffortStateSchema, state); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { isManagedKimiCodeBaseUrl } from '@moonshot-ai/kimi-code-oauth'; | ||
| import type { KimiConfig, KimiConfigPatch, ModelAlias } from '@moonshot-ai/kimi-code-sdk'; | ||
| import type { TelemetryProperties } from '@moonshot-ai/kimi-telemetry'; | ||
|
|
||
| import { getRecommendedEffortStateFile } from '#/utils/paths'; | ||
| import type { RecommendedEffortConfig } from '#/utils/recommended-effort-config'; | ||
| import { | ||
| readRecommendedEffortState, | ||
| writeRecommendedEffortState, | ||
| } from '#/utils/recommended-effort-state-store'; | ||
|
|
||
| export interface ApplyRecommendedEffortDeps { | ||
| fetchConfig: () => Promise<RecommendedEffortConfig | undefined>; | ||
| getConfig: () => Promise<KimiConfig>; | ||
| setConfig: (patch: KimiConfigPatch) => Promise<unknown>; | ||
| track: (event: string, properties?: TelemetryProperties) => void; | ||
| stateFile?: string; | ||
| now?: () => Date; | ||
| } | ||
|
|
||
| function eligibleModelEntry(config: KimiConfig): ModelAlias | undefined { | ||
| if (config.thinking?.enabled === false) return undefined; | ||
| const alias = config.defaultModel; | ||
| if (alias === undefined) return undefined; | ||
| const entry = config.models?.[alias]; | ||
| if (entry === undefined) return undefined; | ||
| const baseUrl = entry.baseUrl ?? config.providers[entry.provider]?.baseUrl; | ||
| return isManagedKimiCodeBaseUrl(baseUrl) ? entry : undefined; | ||
| } | ||
|
|
||
| export async function applyRecommendedEffort(deps: ApplyRecommendedEffortDeps): Promise<void> { | ||
| try { | ||
| if (eligibleModelEntry(await deps.getConfig()) === undefined) return; | ||
| const cloud = await deps.fetchConfig(); | ||
| if (cloud === undefined) return; | ||
|
|
||
| const config = await deps.getConfig(); | ||
| const modelEntry = eligibleModelEntry(config); | ||
| if (modelEntry === undefined) return; | ||
| const campaign = cloud[modelEntry.model]; | ||
| if (campaign === undefined) return; | ||
| const supportEfforts = modelEntry.overrides?.supportEfforts ?? modelEntry.supportEfforts; | ||
| if (!supportEfforts?.includes(campaign.recommended_default_effort)) return; | ||
|
|
||
| const stateFile = deps.stateFile ?? getRecommendedEffortStateFile(); | ||
| const state = await readRecommendedEffortState(stateFile); | ||
| const applied = state[modelEntry.model]; | ||
| if (applied !== undefined && campaign.version <= applied.version) return; | ||
|
|
||
| const previousEffort = config.thinking?.effort; | ||
| if (previousEffort !== campaign.recommended_default_effort) { | ||
| await deps.setConfig({ thinking: { effort: campaign.recommended_default_effort } }); | ||
| } | ||
| writeRecommendedEffortState( | ||
| { | ||
| ...state, | ||
| [modelEntry.model]: { | ||
| version: campaign.version, | ||
| applied_at: (deps.now?.() ?? new Date()).toISOString(), | ||
| }, | ||
| }, | ||
| stateFile, | ||
| ); | ||
| deps.track('recommended_effort_applied', { | ||
| model: modelEntry.model, | ||
| version: campaign.version, | ||
| effort: campaign.recommended_default_effort, | ||
| previous_effort: previousEffort, | ||
| }); | ||
| } catch { | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.