* Version-specific syntax guidance. The model's training data covers both * editions but it can't tell which one it's targeting, so it either emits * pwsh-7 syntax on 5.1 (parser error → exit 1) or needlessly avoids && on 7.
(edition: PowerShellEdition | null)
| 49 | * pwsh-7 syntax on 5.1 (parser error → exit 1) or needlessly avoids && on 7. |
| 50 | */ |
| 51 | function getEditionSection(edition: PowerShellEdition | null): string { |
| 52 | if (edition === 'desktop') { |
| 53 | return `PowerShell edition: Windows PowerShell 5.1 (powershell.exe) |
| 54 | - Pipeline chain operators \`&&\` and \`||\` are NOT available — they cause a parser error. To run B only if A succeeds: \`A; if ($?) { B }\`. To chain unconditionally: \`A; B\`. |
| 55 | - Ternary (\`?:\`), null-coalescing (\`??\`), and null-conditional (\`?.\`) operators are NOT available. Use \`if/else\` and explicit \`$null -eq\` checks instead. |
| 56 | - Avoid \`2>&1\` on native executables. In 5.1, redirecting a native command's stderr inside PowerShell wraps each line in an ErrorRecord (NativeCommandError) and sets \`$?\` to \`$false\` even when the exe returned exit code 0. stderr is already captured for you — don't redirect it. |
| 57 | - Default file encoding is UTF-16 LE (with BOM). When writing files other tools will read, pass \`-Encoding utf8\` to \`Out-File\`/\`Set-Content\`. |
| 58 | - \`ConvertFrom-Json\` returns a PSCustomObject, not a hashtable. \`-AsHashtable\` is not available.` |
| 59 | } |
| 60 | if (edition === 'core') { |
| 61 | return `PowerShell edition: PowerShell 7+ (pwsh) |
| 62 | - Pipeline chain operators \`&&\` and \`||\` ARE available and work like bash. Prefer \`cmd1 && cmd2\` over \`cmd1; cmd2\` when cmd2 should only run if cmd1 succeeds. |
| 63 | - Ternary (\`$cond ? $a : $b\`), null-coalescing (\`??\`), and null-conditional (\`?.\`) operators are available. |
| 64 | - Default file encoding is UTF-8 without BOM.` |
| 65 | } |
| 66 | // Detection not yet resolved (first prompt build before any tool call) or |
| 67 | // PS not installed. Give the conservative 5.1-safe guidance. |
| 68 | return `PowerShell edition: unknown — assume Windows PowerShell 5.1 for compatibility |
| 69 | - Do NOT use \`&&\`, \`||\`, ternary \`?:\`, null-coalescing \`??\`, or null-conditional \`?.\`. These are PowerShell 7+ only and parser-error on 5.1. |
| 70 | - To chain commands conditionally: \`A; if ($?) { B }\`. Unconditionally: \`A; B\`.` |
| 71 | } |
| 72 | |
| 73 | export async function getPrompt(): Promise<string> { |
| 74 | const backgroundNote = getBackgroundUsageNote() |