Skip to content

Commit 84b9d9b

Browse files
fix(clients): honor project default models in new threads (pingdotgg#6011)
Co-authored-by: Shivam Sharma <[email protected]>
1 parent acb599d commit 84b9d9b

12 files changed

Lines changed: 917 additions & 160 deletions

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,12 @@ export function NewTaskDraftScreen(props: {
712712
if (editingPendingTask) {
713713
flow.finishEditingPendingTask();
714714
} else {
715-
// Drop the workspace selection with the content: the next task should
716-
// re-resolve mode/branch/origin from the server's configured defaults
717-
// instead of resurrecting this task's picks.
718-
clearComposerDraftContent(draftKey, { clearWorkspaceSelection: true });
715+
// Drop draft-local model/workspace selections with the content. The
716+
// next task re-resolves project defaults before sticky app defaults.
717+
clearComposerDraftContent(draftKey, {
718+
clearModelSelection: true,
719+
clearWorkspaceSelection: true,
720+
});
719721
}
720722
navigation.getParent()?.goBack();
721723
return;
@@ -779,7 +781,10 @@ export function NewTaskDraftScreen(props: {
779781
}
780782
flow.finishEditingPendingTask();
781783
} else {
782-
clearComposerDraftContent(draftKey, { clearWorkspaceSelection: true });
784+
clearComposerDraftContent(draftKey, {
785+
clearModelSelection: true,
786+
clearWorkspaceSelection: true,
787+
});
783788
}
784789
navigation.dispatch(
785790
StackActions.replace("Thread", {

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
buildModelOptions,
3434
groupByProvider,
3535
resolveDefaultableModelSelection,
36+
resolveNewTaskModelSelection,
3637
resolveSelectableModelSelection,
3738
} from "../../lib/modelOptions";
3839
import { scopedProjectKey } from "../../lib/scopedEntities";
@@ -48,8 +49,10 @@ import {
4849
removeComposerDraftAttachment,
4950
replaceComposerDraftAttachments,
5051
setComposerDraftText,
52+
setStickyComposerModelSelection,
5153
updateComposerDraftSettings,
5254
useComposerDraft,
55+
useStickyComposerModelSelection,
5356
} from "../../state/use-composer-drafts";
5457
import { useDebouncedValue, usePaginatedBranches } from "../../state/queries";
5558
import { vcsEnvironment } from "../../state/vcs";
@@ -418,21 +421,33 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
418421
selectedEnvironmentServerConfig,
419422
selectedProject?.defaultModelSelection ?? null,
420423
);
424+
const storedStickyModelSelection = useStickyComposerModelSelection();
425+
const stickyModelSelection = resolveDefaultableModelSelection(
426+
selectedEnvironmentServerConfig,
427+
storedStickyModelSelection,
428+
);
421429
const modelOptions = useMemo(
422430
() =>
423431
buildModelOptions(
424432
selectedEnvironmentServerConfig,
425-
draftModelSelection ?? projectDefaultModelSelection,
433+
draftModelSelection ?? projectDefaultModelSelection ?? stickyModelSelection,
426434
),
427-
[selectedEnvironmentServerConfig, draftModelSelection, projectDefaultModelSelection],
435+
[
436+
selectedEnvironmentServerConfig,
437+
draftModelSelection,
438+
projectDefaultModelSelection,
439+
stickyModelSelection,
440+
],
428441
);
429442

430-
const selectedModel =
431-
draftModelSelection ??
432-
projectDefaultModelSelection ??
433-
modelOptions.find((option) => option.isDefault)?.selection ??
434-
modelOptions[0]?.selection ??
435-
null;
443+
// An unsent draft keeps its explicit pick. Fresh drafts resolve the project
444+
// default before the last manual app-wide selection and provider default.
445+
const selectedModel = resolveNewTaskModelSelection({
446+
draftSelection: draftModelSelection,
447+
projectDefaultSelection: projectDefaultModelSelection,
448+
stickySelection: stickyModelSelection,
449+
modelOptions,
450+
});
436451
const selectedModelKey = selectedModel
437452
? `${selectedModel.instanceId}:${selectedModel.model}`
438453
: null;
@@ -462,9 +477,9 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
462477
if (!option) {
463478
return;
464479
}
465-
updateComposerDraftSettings(selectedProjectDraftKey, {
466-
modelSelection: options ? { ...option.selection, options } : option.selection,
467-
});
480+
const selection = options ? { ...option.selection, options } : option.selection;
481+
updateComposerDraftSettings(selectedProjectDraftKey, { modelSelection: selection });
482+
setStickyComposerModelSelection(selection);
468483
},
469484
[modelOptions, selectedProjectDraftKey],
470485
);
@@ -482,6 +497,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
482497
updateComposerDraftSettings(selectedProjectDraftKey, {
483498
modelSelection: nextSelection,
484499
});
500+
setStickyComposerModelSelection(nextSelection);
485501
},
486502
[selectedModel, selectedProjectDraftKey],
487503
);

‎apps/mobile/src/lib/modelOptions.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { ProviderInstanceId, type ServerConfig } from "@t3tools/contracts";
3+
import { ProviderInstanceId, type ModelSelection, type ServerConfig } from "@t3tools/contracts";
44

55
import {
66
buildModelOptions,
77
groupByProvider,
88
resolveDefaultableModelSelection,
9+
resolveNewTaskModelSelection,
910
resolveSelectableModelSelection,
11+
type ModelOption,
1012
} from "./modelOptions";
1113

1214
describe("mobile model options", () => {
@@ -220,4 +222,30 @@ describe("mobile model options", () => {
220222
// Offline: nothing to validate against, selection passes through.
221223
expect(resolveDefaultableModelSelection(null, legacy)).toBe(legacy);
222224
});
225+
226+
it("resolves new tasks from draft, project, sticky, then provider defaults", () => {
227+
const draft = { instanceId: ProviderInstanceId.make("codex"), model: "draft" };
228+
const project = { instanceId: ProviderInstanceId.make("codex"), model: "project" };
229+
const sticky = { instanceId: ProviderInstanceId.make("codex"), model: "sticky" };
230+
const providerDefault = {
231+
selection: { instanceId: ProviderInstanceId.make("codex"), model: "default" },
232+
isDefault: true,
233+
} as ModelOption;
234+
const resolve = (
235+
draftSelection: ModelSelection | null,
236+
projectDefaultSelection: ModelSelection | null,
237+
stickySelection: ModelSelection | null,
238+
) =>
239+
resolveNewTaskModelSelection({
240+
draftSelection,
241+
projectDefaultSelection,
242+
stickySelection,
243+
modelOptions: [providerDefault],
244+
});
245+
246+
expect(resolve(draft, project, sticky)).toBe(draft);
247+
expect(resolve(null, project, sticky)).toBe(project);
248+
expect(resolve(null, null, sticky)).toBe(sticky);
249+
expect(resolve(null, null, null)).toBe(providerDefault.selection);
250+
});
223251
});

‎apps/mobile/src/lib/modelOptions.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ export function resolveDefaultableModelSelection(
104104
return model?.isLegacy === true ? null : usable;
105105
}
106106

107+
export function resolveNewTaskModelSelection(input: {
108+
readonly draftSelection: ModelSelection | null;
109+
readonly projectDefaultSelection: ModelSelection | null;
110+
readonly stickySelection: ModelSelection | null;
111+
readonly modelOptions: ReadonlyArray<ModelOption>;
112+
}): ModelSelection | null {
113+
return (
114+
input.draftSelection ??
115+
input.projectDefaultSelection ??
116+
input.stickySelection ??
117+
input.modelOptions.find((option) => option.isDefault)?.selection ??
118+
input.modelOptions[0]?.selection ??
119+
null
120+
);
121+
}
122+
107123
export function buildModelOptions(
108124
config: T3ServerConfig | null | undefined,
109125
fallbackModelSelection: ModelSelection | null,

0 commit comments

Comments
 (0)