@@ -26,6 +26,11 @@ import { SSEParser } from '../../../../util/vs/base/common/sseParser';
2626import { generateUuid } from '../../../../util/vs/base/common/uuid' ;
2727import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation' ;
2828
29+ /**
30+ * Marker string to identify user-initiated messages from VS Code in the Messages API.
31+ */
32+ export const VSCODE_USER_INITIATED_MESSAGE_MARKER = '__vscode_user_initiated_message__' ;
33+
2934export interface IClaudeLanguageModelServerConfig {
3035 readonly port : number ;
3136 readonly nonce : string ;
@@ -161,7 +166,35 @@ export class ClaudeLanguageModelServer extends Disposable {
161166
162167 // Determine if this is a user-initiated message
163168 const lastMessage = requestBody . messages ?. at ( - 1 ) ;
164- const isUserInitiatedMessage = lastMessage ?. role === 'user' ;
169+ const lastContentItems = ! lastMessage || typeof lastMessage . content === 'string'
170+ ? [ ]
171+ : lastMessage . content ;
172+
173+ // Find the index of the marker content item if it exists
174+ const markerIndex = lastContentItems . findIndex (
175+ c => c . type === 'text' &&
176+ // Our marker
177+ c . text . includes ( VSCODE_USER_INITIATED_MESSAGE_MARKER ) &&
178+ // The name of the hook we are using
179+ c . text . includes ( 'UserPromptSubmit' )
180+ ) ;
181+
182+ const isUserInitiatedMessage =
183+ // A user initiated message would only be of role 'user'
184+ lastMessage ?. role === 'user' &&
185+ // We expect our marker AND the user's actual message so there will be multiple content items
186+ lastContentItems . length > 1 &&
187+ // The marker must be in a preceding content item, not the last one (which is the actual user message)
188+ markerIndex !== - 1 &&
189+ markerIndex !== lastContentItems . length - 1 ;
190+
191+ // Remove the marker content item and the one before it (which just provides the status of our hook)
192+ // so they don't influence the request
193+ if ( isUserInitiatedMessage ) {
194+ // Remove marker and its preceding item (if it exists)
195+ const indicesToRemove = markerIndex > 0 ? [ markerIndex - 1 , markerIndex ] : [ markerIndex ] ;
196+ lastMessage . content = lastContentItems . filter ( ( _ , i ) => ! indicesToRemove . includes ( i ) ) ;
197+ }
165198
166199 const allEndpoints = await this . endpointProvider . getAllChatEndpoints ( ) ;
167200 // Filter to only endpoints that support the Messages API
0 commit comments