@@ -10,23 +10,42 @@ import {
1010 GitHubCopilotAgent ,
1111 AgentMessage
1212} from '../interfaces/agentInterface' ;
13- import { PromptService , PromptUsageContext } from '../shared/promptManager/types' ;
13+ import { PromptService , PromptUsageContext , TsxRenderOptions , TsxRenderResult } from '../shared/promptManager/types' ;
1414import { PromptServiceFactory } from '../shared/promptManager/PromptServiceFactory' ;
15+ import { AgentPrompt } from '../shared/promptManager/tsx/components/AgentPrompt' ;
16+ import { ChatMessage , DEFAULT_PRIORITIES } from '../shared/promptManager/tsx/types' ;
17+
18+ /**
19+ * Configuration interface for agent prompt handling
20+ */
21+ interface AgentPromptConfig {
22+ maxTokens : number ;
23+ historyMessageCount : number ;
24+ enablePrioritization : boolean ;
25+ fallbackToStringConcatenation : boolean ;
26+ }
1527
1628/**
1729 * Wu Wei Agent Panel Provider (Enhanced with Prompt Integration)
1830 * Provides a panel for triggering agents with messages using separated HTML, CSS, and JavaScript files
1931 * Phase 4: Added prompt selection and integration capabilities
32+ * Phase 5: Enhanced with TSX-based prompt composition (Issue #378)
2033 */
2134export class AgentPanelProvider extends BaseWebviewProvider implements vscode . WebviewViewProvider {
2235 private _agentRegistry : AgentRegistry ;
2336 private _messageHistory : AgentMessage [ ] = [ ] ;
2437 private _promptService : PromptService ;
2538 private _selectedPromptContext ?: PromptUsageContext ;
39+ private _agentPromptConfig : AgentPromptConfig = {
40+ maxTokens : 4096 ,
41+ historyMessageCount : 4 ,
42+ enablePrioritization : true ,
43+ fallbackToStringConcatenation : true
44+ } ;
2645
2746 constructor ( context : vscode . ExtensionContext ) {
2847 super ( context ) ;
29- logger . debug ( 'Wu Wei Agent Panel Provider initialized with prompt integration' ) ;
48+ logger . debug ( 'Wu Wei Agent Panel Provider initialized with prompt integration and TSX support ' ) ;
3049
3150 // Initialize prompt service
3251 this . _promptService = PromptServiceFactory . createService ( context ) ;
@@ -266,6 +285,21 @@ export class AgentPanelProvider extends BaseWebviewProvider implements vscode.We
266285 return params ;
267286 }
268287
288+ // Try TSX-based rendering first if enabled
289+ if ( this . _agentPromptConfig . enablePrioritization ) {
290+ try {
291+ return await this . enhanceParamsWithTsxPrompt ( params , promptContext , agent ) ;
292+ } catch ( error ) {
293+ logger . warn ( 'TSX prompt rendering failed, falling back to string concatenation' , error ) ;
294+
295+ // If fallback is disabled, re-throw the error
296+ if ( ! this . _agentPromptConfig . fallbackToStringConcatenation ) {
297+ throw error ;
298+ }
299+ }
300+ }
301+
302+ // Original string concatenation logic (fallback)
269303 const capabilities = agent . getCapabilities ( ) ;
270304 const promptSupport = capabilities . metadata ?. promptSupport ;
271305
@@ -320,6 +354,146 @@ export class AgentPanelProvider extends BaseWebviewProvider implements vscode.We
320354 return params ;
321355 }
322356
357+ /**
358+ * Enhanced TSX-based prompt parameter enhancement
359+ * Replaces string concatenation with intelligent TSX composition
360+ */
361+ private async enhanceParamsWithTsxPrompt (
362+ params : any ,
363+ promptContext : any ,
364+ agent : AbstractAgent
365+ ) : Promise < any > {
366+ if ( ! promptContext ) {
367+ return params ;
368+ }
369+
370+ const userInput = params . message || params . question || params . query || params . input ;
371+ if ( ! userInput ) {
372+ throw new Error ( 'Please provide a custom message to combine with the prompt template' ) ;
373+ }
374+
375+ // Render the prompt with variables
376+ const rendered = await this . _promptService . renderPromptWithVariables (
377+ promptContext . promptId ,
378+ promptContext . variables
379+ ) ;
380+
381+ // Convert message history to ChatMessage format
382+ const conversationHistory : ChatMessage [ ] = this . _messageHistory
383+ . filter ( msg => msg . type === 'request' || msg . type === 'response' )
384+ . slice ( - this . _agentPromptConfig . historyMessageCount )
385+ . map ( msg => ( {
386+ role : msg . type === 'request' ? 'user' : 'assistant' ,
387+ content : this . extractMessageContent ( msg ) ,
388+ timestamp : msg . timestamp ,
389+ id : msg . id
390+ } ) ) ;
391+
392+ // Prepare TSX rendering options
393+ const tsxOptions : TsxRenderOptions = {
394+ modelMaxPromptTokens : this . _agentPromptConfig . maxTokens ,
395+ enablePrioritization : this . _agentPromptConfig . enablePrioritization ,
396+ tokenBudget : this . _agentPromptConfig . maxTokens
397+ } ;
398+
399+ // Render TSX prompt with intelligent composition
400+ const tsxResult : TsxRenderResult = await this . _promptService . renderTsxPrompt (
401+ AgentPrompt ,
402+ {
403+ systemPrompt : rendered ,
404+ userInput : userInput ,
405+ conversationHistory : conversationHistory ,
406+ contextData : params . context || '' ,
407+ maxTokens : this . _agentPromptConfig . maxTokens ,
408+ priorityStrategy : DEFAULT_PRIORITIES
409+ } ,
410+ tsxOptions
411+ ) ;
412+
413+ // Check agent capabilities for TSX support
414+ const capabilities = agent . getCapabilities ( ) ;
415+ const promptSupport = capabilities . metadata ?. promptSupport ;
416+
417+ if ( promptSupport ?. supportsPrompts && promptSupport . supportsTsxMessages ) {
418+ // Agent supports TSX messages directly
419+ return {
420+ ...params ,
421+ messages : tsxResult . messages ,
422+ tokenCount : tsxResult . tokenCount ,
423+ renderingMetadata : tsxResult . renderingMetadata
424+ } ;
425+ } else {
426+ // Convert TSX messages back to string format for compatibility
427+ const combinedMessage = this . convertTsxMessagesToString ( tsxResult . messages ) ;
428+
429+ return {
430+ ...params ,
431+ message : combinedMessage ,
432+ tokenCount : tsxResult . tokenCount ,
433+ renderingMetadata : tsxResult . renderingMetadata
434+ } ;
435+ }
436+ }
437+
438+ /**
439+ * Extract content from AgentMessage for conversation history
440+ */
441+ private extractMessageContent ( message : AgentMessage ) : string {
442+ if ( message . type === 'request' ) {
443+ return message . params ?. message || message . params ?. query || message . params ?. input || 'Request' ;
444+ } else if ( message . type === 'response' ) {
445+ return message . result ?. message || message . result ?. content || JSON . stringify ( message . result || { } ) ;
446+ }
447+ return 'Unknown message' ;
448+ }
449+
450+ /**
451+ * Convert TSX messages to string format for agents that don't support TSX
452+ */
453+ private convertTsxMessagesToString ( messages : vscode . LanguageModelChatMessage [ ] ) : string {
454+ return messages . map ( msg => {
455+ // Map roles to string labels
456+ let role = 'USER' ;
457+ if ( msg . role === vscode . LanguageModelChatMessageRole . User ) {
458+ role = 'USER' ;
459+ } else {
460+ // For any other role (system, assistant, etc.), use generic labels
461+ role = 'ASSISTANT' ;
462+ }
463+
464+ // Extract text content from message
465+ const content = Array . isArray ( msg . content )
466+ ? msg . content . map ( part => {
467+ if ( typeof part === 'string' ) {
468+ return part ;
469+ } else if ( part && typeof part === 'object' && 'text' in part ) {
470+ return part . text ;
471+ } else if ( part && typeof part === 'object' && 'value' in part ) {
472+ return String ( part . value ) ;
473+ }
474+ return JSON . stringify ( part ) ;
475+ } ) . join ( ' ' )
476+ : String ( msg . content ) ;
477+
478+ return `${ role } : ${ content } ` ;
479+ } ) . join ( '\n\n' ) ;
480+ }
481+
482+ /**
483+ * Update agent prompt configuration
484+ */
485+ public updateAgentPromptConfig ( config : Partial < AgentPromptConfig > ) : void {
486+ this . _agentPromptConfig = { ...this . _agentPromptConfig , ...config } ;
487+ logger . info ( 'Agent prompt configuration updated' , this . _agentPromptConfig ) ;
488+ }
489+
490+ /**
491+ * Get current agent prompt configuration
492+ */
493+ public getAgentPromptConfig ( ) : AgentPromptConfig {
494+ return { ...this . _agentPromptConfig } ;
495+ }
496+
323497 private async sendAvailablePrompts ( ) : Promise < void > {
324498 try {
325499 const prompts = await this . _promptService . getAllPrompts ( ) ;
0 commit comments