@@ -24,6 +24,52 @@ export interface ModelInput {
2424 stats : ParticipantPromptProperties ;
2525}
2626
27+ export function getContentLength (
28+ message : vscode . LanguageModelChatMessage
29+ ) : number {
30+ const content = message . content as any ;
31+ if ( typeof content === 'string' ) {
32+ return content . trim ( ) . length ;
33+ }
34+
35+ // TODO: https://github.com/microsoft/vscode/pull/231788 made it so message.content is no longer a string,
36+ // but an array of things that a message can contain. This will eventually be reflected in the type definitions
37+ // but until then, we're manually checking the array contents to ensure we don't break when this PR gets released
38+ // in the stable channel.
39+ if ( Array . isArray ( content ) ) {
40+ return content . reduce ( ( acc : number , element ) => {
41+ const value = element ?. value ?? element ?. content ?. value ;
42+ if ( typeof value === 'string' ) {
43+ return acc + value . length ;
44+ }
45+
46+ return acc ;
47+ } , 0 ) ;
48+ }
49+
50+ return 0 ;
51+ }
52+
53+ export function isContentEmpty (
54+ message : vscode . LanguageModelChatMessage
55+ ) : boolean {
56+ const content = message . content as any ;
57+ if ( typeof content === 'string' ) {
58+ return content . trim ( ) . length === 0 ;
59+ }
60+
61+ if ( Array . isArray ( content ) ) {
62+ for ( const element of content ) {
63+ const value = element ?. value ?? element ?. content ?. value ;
64+ if ( typeof value === 'string' && value . trim ( ) . length > 0 ) {
65+ return false ;
66+ }
67+ }
68+ }
69+
70+ return true ;
71+ }
72+
2773export abstract class PromptBase < TArgs extends PromptArgsBase > {
2874 protected abstract getAssistantPrompt ( args : TArgs ) : string ;
2975
@@ -92,7 +138,7 @@ export abstract class PromptBase<TArgs extends PromptArgsBase> {
92138 ) : ParticipantPromptProperties {
93139 return {
94140 total_message_length : messages . reduce (
95- ( acc , message ) => acc + message . content . length ,
141+ ( acc , message ) => acc + getContentLength ( message ) ,
96142 0
97143 ) ,
98144 user_input_length : request . prompt . length ,
0 commit comments