fix(ai-chat) - fix browser context injection (#20809)
Move the browsing context out of the system prompt and injecting it directly into the last user message instead. Previously (before this PR), browsing context change, update system prompt then break whole conversation history ... and caching. Now, browsing context is sent with last message only if changed. "Benchmark" this PR vs main : - same conv with 3-4 turns - 60% -> 85% cache ratio || 0.31 credits -> 0.13
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
} from '@/ai/states/agentChatDraftsByThreadIdState';
|
||||
import { agentChatErrorComponentFamilyState } from '@/ai/states/agentChatErrorComponentFamilyState';
|
||||
import { agentChatInputState } from '@/ai/states/agentChatInputState';
|
||||
import { agentChatLastSentBrowsingContextFamilyState } from '@/ai/states/agentChatLastSentBrowsingContextFamilyState';
|
||||
import { agentChatMessagesComponentFamilyState } from '@/ai/states/agentChatMessagesComponentFamilyState';
|
||||
import { agentChatSelectedFilesState } from '@/ai/states/agentChatSelectedFilesState';
|
||||
import { agentChatUploadedFilesState } from '@/ai/states/agentChatUploadedFilesState';
|
||||
@@ -96,6 +97,17 @@ export const useAgentChat = (
|
||||
}));
|
||||
|
||||
const browsingContext = getBrowsingContext();
|
||||
const lastSentBrowsingContextAtom =
|
||||
agentChatLastSentBrowsingContextFamilyState.atomFamily(threadId);
|
||||
const lastSentBrowsingContext = store.get(lastSentBrowsingContextAtom);
|
||||
const isBrowsingContextChanged =
|
||||
lastSentBrowsingContext === undefined
|
||||
? browsingContext !== null
|
||||
: JSON.stringify(browsingContext) !==
|
||||
JSON.stringify(lastSentBrowsingContext);
|
||||
const browsingContextToSend = isBrowsingContextChanged
|
||||
? browsingContext
|
||||
: null;
|
||||
const messageId = v4();
|
||||
const optimisticMessageCreatedAt = new Date().toISOString();
|
||||
const rollbackOptimisticUnarchive = applyOptimisticUnarchive(
|
||||
@@ -151,13 +163,17 @@ export const useAgentChat = (
|
||||
threadId,
|
||||
text: contentToSend,
|
||||
messageId,
|
||||
browsingContext: browsingContext ?? null,
|
||||
browsingContext: browsingContextToSend,
|
||||
modelId: modelIdForRequest ?? undefined,
|
||||
fileAttachments:
|
||||
fileAttachments.length > 0 ? fileAttachments : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
if (isBrowsingContextChanged) {
|
||||
store.set(lastSentBrowsingContextAtom, browsingContext);
|
||||
}
|
||||
|
||||
if (data?.sendChatMessage?.queued) {
|
||||
const latestMessages = store.get(messagesAtom);
|
||||
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { createAtomFamilyState } from '@/ui/utilities/state/jotai/utils/createAtomFamilyState';
|
||||
import { type BrowsingContext } from '@/ai/types/BrowsingContext';
|
||||
|
||||
export const agentChatLastSentBrowsingContextFamilyState =
|
||||
createAtomFamilyState<BrowsingContext | null | undefined, string>({
|
||||
key: 'ai/agentChatLastSentBrowsingContextFamilyState',
|
||||
defaultValue: undefined,
|
||||
});
|
||||
Reference in New Issue
Block a user