Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/extension/agents/claude/node/claudeLanguageModelServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { SSEParser } from '../../../../util/vs/base/common/sseParser';
import { generateUuid } from '../../../../util/vs/base/common/uuid';
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';

/**
* Marker string to identify user-initiated messages from VS Code in the Messages API.
*/
export const VSCODE_USER_INITIATED_MESSAGE_MARKER = '__vscode_user_initiated_message__';

export interface IClaudeLanguageModelServerConfig {
readonly port: number;
readonly nonce: string;
Expand Down Expand Up @@ -161,7 +166,35 @@ export class ClaudeLanguageModelServer extends Disposable {

// Determine if this is a user-initiated message
const lastMessage = requestBody.messages?.at(-1);
const isUserInitiatedMessage = lastMessage?.role === 'user';
const lastContentItems = !lastMessage || typeof lastMessage.content === 'string'
? []
: lastMessage.content;

// Find the index of the marker content item if it exists
const markerIndex = lastContentItems.findIndex(
c => c.type === 'text' &&
// Our marker
c.text.includes(VSCODE_USER_INITIATED_MESSAGE_MARKER) &&
// The name of the hook we are using
c.text.includes('UserPromptSubmit')
);

const isUserInitiatedMessage =
// A user initiated message would only be of role 'user'
lastMessage?.role === 'user' &&
// We expect our marker AND the user's actual message so there will be multiple content items
lastContentItems.length > 1 &&
// The marker must be in a preceding content item, not the last one (which is the actual user message)
markerIndex !== -1 &&
markerIndex !== lastContentItems.length - 1;

// Remove the marker content item and the one before it (which just provides the status of our hook)
// so they don't influence the request
if (isUserInitiatedMessage) {
// Remove marker and its preceding item (if it exists)
const indicesToRemove = markerIndex > 0 ? [markerIndex - 1, markerIndex] : [markerIndex];
lastMessage.content = lastContentItems.filter((_, i) => !indicesToRemove.includes(i));
}

const allEndpoints = await this.endpointProvider.getAllChatEndpoints();
// Filter to only endpoints that support the Messages API
Expand Down
11 changes: 10 additions & 1 deletion src/extension/agents/claude/node/hooks/loggingHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ILogService } from '../../../../../platform/log/common/logService';
import { CapturingToken } from '../../../../../platform/requestLogger/common/capturingToken';
import { IClaudeSessionStateService } from '../claudeSessionStateService';
import { registerClaudeHook } from './claudeHookRegistry';
import { VSCODE_USER_INITIATED_MESSAGE_MARKER } from '../claudeLanguageModelServer';

/**
* Logging hook for Notification events.
Expand Down Expand Up @@ -60,7 +61,15 @@ export class UserPromptSubmitLoggingHook implements HookCallbackMatcher {
// Create a capturing token for this request to group tool calls under the request
const capturingToken = new CapturingToken(hookInput.prompt, 'sparkle', false);
this.sessionStateService.setCapturingTokenForSession(hookInput.session_id, capturingToken);
return { continue: true };

return {
continue: true,
// Mark this message as user-initiated for downstream processing of PRUs
hookSpecificOutput: {
hookEventName: 'UserPromptSubmit',
additionalContext: VSCODE_USER_INITIATED_MESSAGE_MARKER
}
};
}
}
registerClaudeHook('UserPromptSubmit', UserPromptSubmitLoggingHook);
Expand Down