Skip to content

Commit 618c16f

Browse files
committed
fix(trae-cn): reject ambiguous model selections
1 parent 22ed436 commit 618c16f

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

clis/trae-cn/trae-cn.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it, vi } from 'vitest';
22
import { JSDOM } from 'jsdom';
3-
import { CommandExecutionError, TimeoutError } from '@jackwener/opencli/errors';
3+
import { ArgumentError, CommandExecutionError, TimeoutError } from '@jackwener/opencli/errors';
44
import { activityCommand } from './activity.js';
55
import { approveCommand } from './approve.js';
66
import { askCommand } from './ask.js';
@@ -335,6 +335,24 @@ describe('trae-cn commands', () => {
335335
.rejects.toBeInstanceOf(CommandExecutionError);
336336
});
337337

338+
it('select-model rejects ambiguous partial model names before changing selection', async () => {
339+
const page = {
340+
evaluate: vi.fn()
341+
.mockResolvedValueOnce([])
342+
.mockResolvedValueOnce([
343+
{ Index: 0, Model: 'GPT 5.4' },
344+
{ Index: 1, Model: 'GPT 5.5' },
345+
]),
346+
click: vi.fn().mockResolvedValue(undefined),
347+
wait: vi.fn().mockResolvedValue(undefined),
348+
};
349+
350+
await expect(selectModelCommand.func(page, { name: 'GPT 5' }))
351+
.rejects.toBeInstanceOf(ArgumentError);
352+
expect(page.click).toHaveBeenCalledTimes(1);
353+
expect(page.click).toHaveBeenCalledWith(expect.stringContaining('icd-model-select-trigger'));
354+
});
355+
338356
it('new fails closed when clicking new task does not prove a fresh empty composer', async () => {
339357
const page = {
340358
evaluate: vi.fn()

clis/trae-cn/utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,16 @@ export async function selectTraeModel(page, name) {
716716
await page.wait(0.8);
717717
models = await page.evaluate(listOpenModelItemsScript());
718718
}
719-
const match = models.find((item) => normalizeModelLabel(item.Model) === wantedKey)
720-
|| models.find((item) => normalizeModelLabel(item.Model).includes(wantedKey));
719+
const exactMatch = models.find((item) => normalizeModelLabel(item.Model) === wantedKey);
720+
const containsMatches = exactMatch
721+
? []
722+
: models.filter((item) => normalizeModelLabel(item.Model).includes(wantedKey));
723+
if (!exactMatch && containsMatches.length > 1) {
724+
throw new ArgumentError(
725+
`Model "${wanted}" is ambiguous in Trae CN model menu: ${containsMatches.map(item => item.Model).join(', ')}`,
726+
);
727+
}
728+
const match = exactMatch || containsMatches[0];
721729
if (!match) {
722730
throw new ArgumentError(`Model "${wanted}" not found in Trae CN model menu`);
723731
}

0 commit comments

Comments
 (0)