The open-source agent harness - the runtime layer that turns an LLM into a working agent.
fix: keep web search enabled by default in new chats if capability is enabled (#1126)
Heer Ambavie057a23f138d
diff --git a/.changeset/new-chat-web-search-default.md b/.changeset/new-chat-web-search-default.mdnew file mode 100644index 000000000..9638f0486--- /dev/null+++ b/.changeset/new-chat-web-search-default.md@@ -0,0 +1,5 @@+---+"@truefoundry/trueforge-ui": patch+---++Keep web search enabled by default in New Chat when the web search capability is available.diff --git a/packages/trueforge-ui/src/server/ShellModeContext.tsx b/packages/trueforge-ui/src/server/ShellModeContext.tsxindex 72a8213e8..e15371050 100644--- a/packages/trueforge-ui/src/server/ShellModeContext.tsx+++ b/packages/trueforge-ui/src/server/ShellModeContext.tsx@@ -27,6 +27,7 @@ import {useOptionalSandboxEnvironmentServer,useOptionalScheduleServer,useServerCapabilities,+ useServerCapabilitiesSettled,} from './ServerContext.js';import type { AgentLibraryEntry, AgentSpec } from './types.js';@@ -254,6 +255,7 @@ export function ShellModeProvider({children: ReactNode;}) {const capabilities = useServerCapabilities();+ const capabilitiesPending = !useServerCapabilitiesSettled();const catalog = useOptionalCatalogServer();const refreshCapabilities = useOptionalRefreshServerCapabilities();const sessionsServer = useOptionalAgentSessionsServer();@@ -599,16 +601,25 @@ export function ShellModeProvider({[selectLibraryAgent],);+ const sandboxEnabled = capabilities?.sandbox.enabled;+ const webSearchEnabled = capabilities?.webSearch?.enabled;+const openDraft = useCallback(() => {if (!isComposerEnabled) return;refreshCapabilities?.();setHistoryAgentFilter(null);// Prefer storage so a just-toggled web-search preference is not lost if the// in-memory seed has not been rewritten yet this tick.- const seed = readDraftSpecPreferences('chat') ?? chatSeedRef.current;+ // Align web-search with capability so inline New Chat sessions admit enabled:true+ // even if createSession races the preference bridge.+ const seed = withCapabilitiesWebSearch({+ spec: readDraftSpecPreferences('chat') ?? chatSeedRef.current,+ webSearchEnabled,+ kind: 'chat',+ });chatSeedRef.current = seed;selectLibraryAgent({ isMutable: true, isCreateAgent: false, agentSpec: seed });- }, [isComposerEnabled, refreshCapabilities, selectLibraryAgent]);+ }, [isComposerEnabled, refreshCapabilities, selectLibraryAgent, webSearchEnabled]);const isActiveAgentBuilder =effectiveMode.status === 'active' && effectiveMode.isMutable && effectiveMode.isCreateAgent;@@ -664,8 +675,6 @@ export function ShellModeProvider({[applyFreshAgentBuilderSeed, isComposerEnabled, refreshCapabilities, selectLibraryAgent],);- const sandboxEnabled = capabilities?.sandbox.enabled;- const webSearchEnabled = capabilities?.webSearch?.enabled;// Child effects (DraftSpecPreferenceBridge) run before this updates — so remember still// sees the prior capability when Settings refresh flips web-search on.const prevWebSearchEnabledRef = useRef(webSearchEnabled);@@ -686,6 +695,7 @@ export function ShellModeProvider({spec: aligned,webSearchEnabled,previousWebSearchEnabled,+ capabilitiesPending,kind,});if (kind === 'chat') {@@ -701,12 +711,13 @@ export function ShellModeProvider({spec: agentAligned,webSearchEnabled,previousWebSearchEnabled,+ capabilitiesPending,kind: 'agent',});}writeDraftSpecPreferences(kind, preferences);},- [sandboxEnabled, webSearchEnabled],+ [sandboxEnabled, webSearchEnabled, capabilitiesPending],);const openHistorySession = useCallback(@@ -817,7 +828,11 @@ export function ShellModeProvider({const agentSpec =kind === 'agent'? (liveSpec ?? effectiveMode.agentSpec ?? agentSeedRef.current)- : (readDraftSpecPreferences('chat') ?? chatSeedRef.current);+ : withCapabilitiesWebSearch({+ spec: readDraftSpecPreferences('chat') ?? chatSeedRef.current,+ webSearchEnabled,+ kind: 'chat',+ });if (kind === 'chat') {chatSeedRef.current = agentSpec;}@@ -836,7 +851,7 @@ export function ShellModeProvider({}bumpEpoch(false);},- [effectiveMode, bumpEpoch],+ [effectiveMode, bumpEpoch, webSearchEnabled],);const openLibraryHome = useCallback(() => {diff --git a/packages/trueforge-ui/src/server/draftSpecPreferences.ts b/packages/trueforge-ui/src/server/draftSpecPreferences.tsindex 207b5999b..d27213306 100644--- a/packages/trueforge-ui/src/server/draftSpecPreferences.ts+++ b/packages/trueforge-ui/src/server/draftSpecPreferences.ts@@ -212,8 +212,13 @@ export function withoutWebSearchPreference(spec: AgentSpec): AgentSpec {}/**- * When web-search flips unavailable → available, a live `enabled: false` may be leftover- * force-off. Default-fill on only when that false is not an explicit user/saved choice.+ * When web-search becomes available, a live `enabled: false` may be leftover force-off+ * or a schema default from an inline session created before capabilities loaded.+ * Default-fill on only when that false is not an explicit user/saved choice.+ *+ * New Chat (`kind: 'chat'`) also recovers when capability first becomes known+ * (`null`/`undefined` → true), so a premature createSession does not stick off.+ * Agent drafts keep an explicit off across first capability load.*/export function recoverWebSearchAfterCapabilityAvailable({spec,@@ -230,8 +235,13 @@ export function recoverWebSearchAfterCapabilityAvailable({kind?: DraftPreferenceKind;}): AgentSpec {if (explicitOff) return spec;- if (previousWebSearchEnabled !== false || webSearchEnabled !== true) return spec;+ if (webSearchEnabled !== true) return spec;if (readWebSearchEnabled(spec) !== false) return spec;++ const capabilityReturned = previousWebSearchEnabled === false;+ const capabilityBecameKnown = previousWebSearchEnabled == null && kind === 'chat';+ if (!capabilityReturned && !capabilityBecameKnown) return spec;+return withCapabilitiesWebSearch({spec: withoutWebSearchPreference(spec),webSearchEnabled: true,@@ -256,22 +266,25 @@ export function persistedWebSearchEnabled(kind: DraftPreferenceKind): boolean |}/**- * Remember a user web-search choice. Capability-forced off is omitted unless a choice- * was already stored, and that stored off is not rewritten to on when search returns.+ * Remember a user web-search choice. Capability-forced off, or any value seen before capabilities+ * settle (a schema default, not a choice), is omitted unless a choice was already stored; a stored off+ * is not rewritten to on when search returns.*/export function webSearchPreferenceToStore({spec,webSearchEnabled,previousWebSearchEnabled,+ capabilitiesPending = false,kind,}: {spec: AgentSpec;webSearchEnabled: boolean | null | undefined;previousWebSearchEnabled: boolean | null | undefined;+ capabilitiesPending?: boolean;kind: DraftPreferenceKind;}): AgentSpec {const persistedEnabled = persistedWebSearchEnabled(kind);- if (webSearchEnabled === false) {+ if (webSearchEnabled === false || capabilitiesPending) {const stripped = withoutWebSearchPreference(spec);return persistedEnabled === undefined ? stripped : withWebSearchEnabled(stripped, persistedEnabled);}diff --git a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx b/packages/trueforge-ui/test/server/ShellModeContext.test.tsxindex ecceb52d5..6bc64a811 100644--- a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx+++ b/packages/trueforge-ui/test/server/ShellModeContext.test.tsx@@ -9,7 +9,7 @@ import {DRAFT_SPEC_PREFERENCES_STORAGE_KEY,readDraftSpecPreferences,} from '@/server/draftSpecPreferences.js';-import { ServerProvider, useServerCapabilities } from '@/server/ServerContext.js';+import { ServerProvider, useServerCapabilities, useServerCapabilitiesSettled } from '@/server/ServerContext.js';import { ShellModeProvider, useOptionalShellMode, useShellMode, type AgentConfig } from '@/server/ShellModeContext.js';import type { AgentUIServer } from '@/server/types.js';import { createMockAgentSessionsServer, createMockAgentUIServer, createMockCatalog } from './mockServer.js';@@ -543,8 +543,8 @@ describe('ShellModeProvider', () => {expect(result.current.runtimeKey).not.toBe(keyBefore);});- it('clearChat and openDraft keep a disabled chat web-search preference', () => {- const { result } = renderHook(() => useShellMode(), {+ it('clearChat and openDraft keep a disabled chat web-search preference', async () => {+ const { result } = renderHook(() => ({ shell: useShellMode(), settled: useServerCapabilitiesSettled() }), {wrapper: wrap({mode: 'AgentComposer',defaultAgentSpec: {@@ -553,9 +553,10 @@ describe('ShellModeProvider', () => {},}),});+ await waitFor(() => expect(result.current.settled).toBe(true));act(() =>- result.current.rememberDraftSpec(+ result.current.shell.rememberDraftSpec({model: { name: 'openai-main/gpt-4.1' },config: { webSearch: { enabled: false } },@@ -564,8 +565,8 @@ describe('ShellModeProvider', () => {),);- act(() => result.current.clearChat());- expect(result.current.mode).toMatchObject({+ act(() => result.current.shell.clearChat());+ expect(result.current.shell.mode).toMatchObject({status: 'active',isMutable: true,isCreateAgent: false,@@ -573,7 +574,7 @@ describe('ShellModeProvider', () => {});act(() =>- result.current.rememberDraftSpec(+ result.current.shell.rememberDraftSpec({model: { name: 'openai-main/gpt-4.1' },config: { webSearch: { enabled: false } },@@ -581,12 +582,50 @@ describe('ShellModeProvider', () => {'chat',),);- act(() => result.current.openDraft());- expect(result.current.mode).toMatchObject({+ act(() => result.current.shell.openDraft());+ expect(result.current.shell.mode).toMatchObject({agentSpec: { config: { webSearch: { enabled: false } } },});});+ it('openDraft and clearChat enable web search on New Chat when the capability is available', async () => {+ const getCapabilities = vi.fn(async () => ({+ data: {+ sandbox: { enabled: true },+ skill: { enabled: true },+ settings: { enabled: true },+ webSearch: { enabled: true },+ },+ }));+ const server = createMockAgentUIServer({ catalog: createMockCatalog(), getCapabilities });+ const { result } = renderHook(+ () => ({+ shell: useShellMode(),+ capabilities: useServerCapabilities(),+ }),+ {+ wrapper: wrapWithServer(server, {+ mode: 'AgentComposer',+ defaultAgentSpec: { model: { name: 'openai-main/gpt-4.1' } },+ }),+ },+ );+ await waitFor(() => expect(result.current.capabilities?.webSearch?.enabled).toBe(true));++ act(() => result.current.shell.openDraft());+ expect(result.current.shell.mode).toMatchObject({+ status: 'active',+ isMutable: true,+ isCreateAgent: false,+ agentSpec: { config: { webSearch: { enabled: true } } },+ });++ act(() => result.current.shell.clearChat());+ expect(result.current.shell.mode).toMatchObject({+ agentSpec: { config: { webSearch: { enabled: true } } },+ });+ });+it('does not persist capability-forced web-search off as a chat preference', async () => {const getCapabilities = vi.fn(async () => ({data: {diff --git a/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts b/packages/trueforge-ui/test/server/draftSpecPreferences.test.tsindex 2096c1ffb..b35f9d8a5 100644--- a/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts+++ b/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts@@ -377,6 +377,32 @@ describe('recoverWebSearchAfterCapabilityAvailable', () => {}),).toBe(spec);});++ it('default-fills New Chat on when capability first becomes known after a schema-default off', () => {+ expect(+ recoverWebSearchAfterCapabilityAvailable({+ spec: { model: { name: 'model' }, config: { webSearch: { enabled: false } } },+ previousWebSearchEnabled: undefined,+ webSearchEnabled: true,+ kind: 'chat',+ }),+ ).toEqual({+ model: { name: 'model' },+ config: { webSearch: { enabled: true } },+ });+ });++ it('does not default-fill agent drafts when capability first becomes known', () => {+ const spec = { model: { name: 'model' }, config: { webSearch: { enabled: false } } };+ expect(+ recoverWebSearchAfterCapabilityAvailable({+ spec,+ previousWebSearchEnabled: undefined,+ webSearchEnabled: true,+ kind: 'agent',+ }),+ ).toBe(spec);+ });});describe('webSearchPreferenceToStore', () => {@@ -420,6 +446,28 @@ describe('webSearchPreferenceToStore', () => {).toEqual(forcedOff);});+ it('does not store a schema-default off while capabilities are pending, so it recovers once known', () => {+ const schemaDefault = { model: { name: 'model' }, config: { webSearch: { enabled: false } } };+ const stored = webSearchPreferenceToStore({+ spec: schemaDefault,+ webSearchEnabled: undefined,+ previousWebSearchEnabled: undefined,+ capabilitiesPending: true,+ kind: 'chat',+ });+ expect(stored).toEqual({ model: { name: 'model' } });++ writeDraftSpecPreferences('chat', stored);+ expect(+ webSearchPreferenceToStore({+ spec: schemaDefault,+ webSearchEnabled: true,+ previousWebSearchEnabled: undefined,+ kind: 'chat',+ }),+ ).toEqual({ model: { name: 'model' }, config: { webSearch: { enabled: true } } });+ });+it('default-fills on after an outage when no choice was stored', () => {expect(webSearchPreferenceToStore({