mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 21:00:15 +08:00
fix: 修复旧文本模型推理默认值,关闭推理时置灰深度选项
This commit is contained in:
1 parent
2d7cc12948
commit
63e5485a9d
6 files changed
+75
-11
No files matched your search
@@ -374,9 +374,14 @@ func readModel(row sqlc.Model, err error) (Model, error) {
|
||||
Kind: Kind(row.Kind), Provider: Provider(row.Provider), ProviderOptions: row.ProviderOptions,
|
||||
BaseURL: row.BaseUrl, ProxyID: row.ProxyID, APIKey: row.ApiKey, APIKeyConfigured: row.ApiKey != "",
|
||||
CreditMultiplier: row.CreditMultiplier, Enabled: row.Enabled, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt}
|
||||
// Seed the legacy text-model default before decoding so an explicit false still wins.
|
||||
item.AdvancedConfig.SupportsReasoning = item.Kind == KindText
|
||||
if err := json.Unmarshal(row.AdvancedConfig, &item.AdvancedConfig); err != nil {
|
||||
return Model{}, fmt.Errorf("解析模型高级配置: %w", err)
|
||||
}
|
||||
if item.Kind == KindText && item.AdvancedConfig.SupportsReasoning && item.AdvancedConfig.Reasoning == nil {
|
||||
item.AdvancedConfig.Reasoning = defaultReasoning()
|
||||
}
|
||||
if len(row.ImageConfig) != 0 {
|
||||
if err := json.Unmarshal(row.ImageConfig, &item.ImageConfig); err != nil {
|
||||
return Model{}, fmt.Errorf("解析生图配置: %w", err)
|
||||
|
||||
@@ -10,6 +10,10 @@ type ReasoningConfig struct {
|
||||
DefaultEffort string `json:"default_effort"`
|
||||
}
|
||||
|
||||
func defaultReasoning() *ReasoningConfig {
|
||||
return &ReasoningConfig{SupportedEfforts: []string{"minimal", "low", "medium", "high", "xhigh", "max"}, DefaultEffort: "medium"}
|
||||
}
|
||||
|
||||
func (c *AdvancedConfig) normalizeReasoning() error {
|
||||
if !c.SupportsReasoning {
|
||||
if c.Reasoning != nil {
|
||||
@@ -17,9 +21,9 @@ func (c *AdvancedConfig) normalizeReasoning() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
levels := []string{"minimal", "low", "medium", "high", "xhigh", "max"}
|
||||
defaults := defaultReasoning()
|
||||
if c.Reasoning == nil {
|
||||
c.Reasoning = &ReasoningConfig{SupportedEfforts: levels, DefaultEffort: "medium"}
|
||||
c.Reasoning = defaults
|
||||
return nil
|
||||
}
|
||||
r := c.Reasoning
|
||||
@@ -27,7 +31,7 @@ func (c *AdvancedConfig) normalizeReasoning() error {
|
||||
return errors.New("supported_efforts 必须为数组")
|
||||
}
|
||||
for i, level := range r.SupportedEfforts {
|
||||
if !slices.Contains(levels, level) || slices.Contains(r.SupportedEfforts[:i], level) {
|
||||
if !slices.Contains(defaults.SupportedEfforts, level) || slices.Contains(r.SupportedEfforts[:i], level) {
|
||||
return errors.New("支持的思考深度无效或重复")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,32 @@ package model
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/chaitin/MonkeyCode/monkeyai/backend/internal/model/sqlc"
|
||||
)
|
||||
|
||||
func TestReadModelReasoningDefaults(t *testing.T) {
|
||||
defaults := &ReasoningConfig{[]string{"minimal", "low", "medium", "high", "xhigh", "max"}, "medium"}
|
||||
for _, tc := range []struct {
|
||||
name, kind, config string
|
||||
enabled bool
|
||||
depth *ReasoningConfig
|
||||
}{
|
||||
{"旧文本模型", "text", "{}", true, defaults},
|
||||
{"显式关闭", "text", `{"supports_reasoning":false}`, false, nil},
|
||||
{"显式开启", "text", `{"supports_reasoning":true}`, true, defaults},
|
||||
{"保留自定义档位", "text", `{"reasoning":{"supported_efforts":["low"],"default_effort":"low"}}`, true, &ReasoningConfig{[]string{"low"}, "low"}},
|
||||
{"生图", "image", "{}", false, nil}, {"视频", "video", "{}", false, nil}, {"向量", "embedding", "{}", false, nil},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
m, err := readModel(sqlc.Model{Kind: tc.kind, AdvancedConfig: []byte(tc.config)}, nil)
|
||||
if err != nil || m.AdvancedConfig.SupportsReasoning != tc.enabled || !reflect.DeepEqual(m.AdvancedConfig.Reasoning, tc.depth) {
|
||||
t.Fatalf("读取结果:%+v,错误:%v", m.AdvancedConfig, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReasoningDepthLifecycle(t *testing.T) {
|
||||
repo := &repositoryStub{}
|
||||
s := NewService(repo)
|
||||
|
||||
@@ -35,6 +35,8 @@ const scenarios = [
|
||||
{ name: "add-clear-and-follow", clearFollow: true },
|
||||
{ name: "add-empty-name-fallback", emptyName: true },
|
||||
{ name: "edit-preserve", edit: true },
|
||||
{ name: "edit-reasoning-legacy", edit: true, reasoningFlag: "missing" },
|
||||
{ name: "edit-reasoning-disabled", edit: true, reasoningFlag: false },
|
||||
{ name: "edit-id-preserve-custom", edit: true, customName: true },
|
||||
{
|
||||
name: "edit-empty-name-follows",
|
||||
@@ -303,6 +305,10 @@ try {
|
||||
? Object.keys(videoDefaultPrices)
|
||||
: ["720p", "480p", "1080p"]
|
||||
const fixture = structuredClone(original)
|
||||
// These edit-and-save cases need a token limit representable in whole K.
|
||||
if (scenario.reasoningFlag !== undefined) fixture.advanced_config.max_output_tokens = 8000
|
||||
if (scenario.reasoningFlag === "missing") delete fixture.advanced_config.supports_reasoning
|
||||
if (scenario.reasoningFlag === false) fixture.advanced_config.supports_reasoning = false
|
||||
if (scenario.emptyStoredName) fixture.display_name = ""
|
||||
if (kind === "image")
|
||||
Object.assign(fixture, {
|
||||
@@ -560,6 +566,21 @@ try {
|
||||
document.getElementById("model-advanced-trigger")
|
||||
)
|
||||
await closed(page)
|
||||
if (scenario.reasoningFlag !== undefined) {
|
||||
const enabled = scenario.reasoningFlag !== false
|
||||
assert.equal(await page.locator("#model-reasoning").isChecked(), enabled)
|
||||
await click(page, "#model-advanced-trigger")
|
||||
await opened(page)
|
||||
const depth = page.locator("#model-default-reasoning-depth")
|
||||
assert.equal(await depth.isVisible(), true)
|
||||
assert.equal(await depth.isDisabled(), !enabled)
|
||||
const levels = page.getByRole("group", { name: "支持的思考深度" }).getByRole("checkbox")
|
||||
assert.equal(await levels.count(), 6)
|
||||
for (const level of await levels.all()) assert.equal(await level.isDisabled(), !enabled)
|
||||
if (enabled) assert.match(await depth.innerText(), /中等/)
|
||||
await click(page, "#model-advanced-trigger")
|
||||
await closed(page)
|
||||
}
|
||||
if (scenario.drafts) {
|
||||
const tab = (name) => page.getByRole("tab", { name, exact: true }).click()
|
||||
await page.locator("#model-id").fill("draft-text")
|
||||
@@ -1100,7 +1121,7 @@ try {
|
||||
await closed(page)
|
||||
}
|
||||
let expectedContext = scenario.edit ? 128000 : 256000,
|
||||
expectedOutput = scenario.edit ? 8192 : 16384,
|
||||
expectedOutput = scenario.edit ? fixture.advanced_config.max_output_tokens : 16384,
|
||||
expectedMultiplier = scenario.edit ? 2.5 : 1,
|
||||
expectedProxy = scenario.edit ? "test-proxy" : ""
|
||||
if (scenario.customAdvanced) {
|
||||
@@ -1378,6 +1399,11 @@ try {
|
||||
assert.equal(writes.length, 1)
|
||||
assert.equal(writes[0].method, scenario.edit ? "PUT" : "POST")
|
||||
const body = writes[0].body
|
||||
if (scenario.reasoningFlag !== undefined) {
|
||||
const enabled = scenario.reasoningFlag !== false
|
||||
assert.equal(body.advanced_config.supports_reasoning, enabled)
|
||||
assert.equal(body.advanced_config.reasoning?.default_effort, enabled ? "medium" : undefined)
|
||||
}
|
||||
assert.equal(body.model_id, expectedId)
|
||||
assert.equal(body.display_name, expectedName)
|
||||
assert.equal(body.proxy_id, expectedProxy)
|
||||
|
||||
@@ -14,9 +14,11 @@ import { reasoningLevels, type Level, type ReasoningDepth } from "@/lib/reasonin
|
||||
export function ReasoningDepthFields({
|
||||
value,
|
||||
onChange,
|
||||
disabled,
|
||||
}: {
|
||||
value: ReasoningDepth
|
||||
onChange: (next: ReasoningDepth) => void
|
||||
disabled: boolean
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const label = (level: Level) => t("pages.models.reasoningDepth." + level)
|
||||
@@ -35,7 +37,7 @@ export function ReasoningDepthFields({
|
||||
}))
|
||||
return (
|
||||
<>
|
||||
<fieldset className="min-w-0 space-y-2 sm:col-span-2">
|
||||
<fieldset disabled={disabled} className="group/depth min-w-0 space-y-2 disabled:text-muted-foreground sm:col-span-2">
|
||||
<legend className="text-sm font-medium">
|
||||
{t("pages.models.reasoningDepth.supported")}
|
||||
</legend>
|
||||
@@ -45,9 +47,10 @@ export function ReasoningDepthFields({
|
||||
return (
|
||||
<label
|
||||
key={level}
|
||||
className="flex min-h-8 flex-auto shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-sm px-1.5 py-1 text-sm hover:bg-muted/50"
|
||||
className="flex min-h-8 flex-auto shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-sm px-1.5 py-1 text-sm hover:bg-muted/50 group-disabled/depth:cursor-not-allowed group-disabled/depth:hover:bg-transparent"
|
||||
>
|
||||
<Checkbox
|
||||
disabled={disabled}
|
||||
checked={selected}
|
||||
onCheckedChange={(checked) => toggle(level, checked)}
|
||||
/>
|
||||
@@ -57,11 +60,12 @@ export function ReasoningDepthFields({
|
||||
})}
|
||||
</div>
|
||||
</fieldset>
|
||||
<Field>
|
||||
<Field data-disabled={disabled}>
|
||||
<FieldLabel htmlFor="model-default-reasoning-depth">
|
||||
{t("pages.models.reasoningDepth.defaultLabel")}
|
||||
</FieldLabel>
|
||||
<Select<Level>
|
||||
disabled={disabled}
|
||||
items={items}
|
||||
value={value.defaultLevel}
|
||||
onValueChange={(level) => {
|
||||
|
||||
@@ -293,6 +293,7 @@ type AuthorizationSubject = {
|
||||
}
|
||||
|
||||
function fromApiModel(model: ApiModel): Model {
|
||||
const kind = model.kind ?? "text"
|
||||
return {
|
||||
id: model.id,
|
||||
modelId: model.model_id,
|
||||
@@ -301,12 +302,12 @@ function fromApiModel(model: ApiModel): Model {
|
||||
contextSizeK: (model.advanced_config?.context_window_tokens ?? 0) / 1000,
|
||||
maxOutputTokensK: (model.advanced_config?.max_output_tokens ?? 0) / 1000,
|
||||
supportsVision: model.advanced_config?.supports_vision ?? false,
|
||||
supportsReasoning: model.advanced_config?.supports_reasoning ?? false,
|
||||
supportsReasoning: model.advanced_config?.supports_reasoning ?? (kind === "text"),
|
||||
reasoningDepth: fromApiReasoning(model.advanced_config?.reasoning),
|
||||
baseUrl: model.base_url,
|
||||
proxyId: model.proxy_id ?? "",
|
||||
protocol: model.protocol,
|
||||
kind: model.kind ?? "text",
|
||||
kind,
|
||||
provider: model.provider ?? "passthrough",
|
||||
imageConfig: model.image_config,
|
||||
imagePricing: model.image_pricing,
|
||||
@@ -2024,8 +2025,8 @@ export function ModelsPage() {
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
{kind === "text" && supportsReasoning && (
|
||||
<ReasoningDepthFields value={reasoningDepth} onChange={setReasoningDepth} />
|
||||
{kind === "text" && (
|
||||
<ReasoningDepthFields value={reasoningDepth} onChange={setReasoningDepth} disabled={!supportsReasoning} />
|
||||
)}
|
||||
{(kind === "text" || kind === "embedding") && (
|
||||
<Field>
|
||||
|
||||
Reference in new issue
Block a user