@@ -71,6 +71,51 @@ import type { AIChatMessage, AIProvider, AIRequestResult } from './models/provid
7171import { ensureAccess , getOrgAIConfig , isProviderEnabledByOrg } from './utils/-webview/ai.utils' ;
7272import { getLocalPromptTemplate , resolvePrompt } from './utils/-webview/prompt.utils' ;
7373
74+ /**
75+ * Removes common leading whitespace from each line in a template string.
76+ * This allows you to write indented template strings but have them trimmed in the result.
77+ *
78+ * @param template The template string to dedent
79+ * @returns The dedented string
80+ *
81+ * @example
82+ * ```typescript
83+ * const str = dedent(`
84+ * Hello
85+ * World
86+ * Test
87+ * `);
88+ * // Result: "Hello\nWorld\nTest"
89+ * ```
90+ */
91+ function dedent ( template : string ) : string {
92+ const lines = template . split ( '\n' ) ;
93+
94+ // Remove leading and trailing empty lines
95+ while ( lines . length > 0 && lines [ 0 ] . trim ( ) === '' ) {
96+ lines . shift ( ) ;
97+ }
98+ while ( lines . length > 0 && lines [ lines . length - 1 ] . trim ( ) === '' ) {
99+ lines . pop ( ) ;
100+ }
101+
102+ if ( lines . length === 0 ) return '' ;
103+
104+ // Find the minimum indentation (excluding empty lines)
105+ const nonEmptyLines = lines . filter ( line => line . trim ( ) !== '' ) ;
106+ if ( nonEmptyLines . length === 0 ) return '' ;
107+
108+ const minIndent = Math . min (
109+ ...nonEmptyLines . map ( line => {
110+ const match = line . match ( / ^ ( \s * ) / ) ;
111+ return match ? match [ 1 ] . length : 0 ;
112+ } ) ,
113+ ) ;
114+
115+ // Remove the common indentation from all lines
116+ return lines . map ( line => line . slice ( minIndent ) ) . join ( '\n' ) ;
117+ }
118+
74119export interface AIResult {
75120 readonly id ?: string ;
76121 readonly content : string ;
0 commit comments