-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartChat.ps1
More file actions
158 lines (139 loc) · 5.49 KB
/
StartChat.ps1
File metadata and controls
158 lines (139 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
param (
[string]$PreviousCliVersion = "0.0.1"
)
."$PSScriptRoot\components\Banner.ps1"
."$PSScriptRoot\components\LoaderCard.ps1"
."$PSScriptRoot\components\MessageCard.ps1"
."$PSScriptRoot\components\PromptCard.ps1"
."$PSScriptRoot\utilities\AssistantUtilities.ps1"
."$PSScriptRoot\utilities\HostUtilities.ps1"
."$PSScriptRoot\utilities\SystemUtilities.ps1"
."$PSScriptRoot\utilities\UserUtilities.ps1"
$script:previousWindowSizeWidth = $Host.UI.RawUI.WindowSize.Width
$script:previousPromptInput = ""
Reset-Host
Write-Banner -FromVersion $PreviousCliVersion
Show-PromptCard -PromptInput $script:previousPromptInput
while ($true) {
$currentWindowSizeWidth = $Host.UI.RawUI.WindowSize.Width
$currentPromptInput = $script:previousPromptInput
$isEnterKeyPressed = $false
if ($currentWindowSizeWidth -ne $script:previousWindowSizeWidth) {
Reset-Host
Write-Banner -FromVersion $PreviousCliVersion
$messages = @(Get-MessagesFromHistory)
Write-MessageCards -Messages $messages
if (Test-AssistantJobRunning) {
Show-LoaderCard
}
else {
Show-PromptCard -PromptInput $currentPromptInput
}
$script:previousWindowSizeWidth = $currentWindowSizeWidth
}
if ([Console]::KeyAvailable) {
$keyInfo = [Console]::ReadKey($true)
$key = $keyInfo.Key
$keyModifiers = $keyInfo.Modifiers
if (!(Test-AssistantJobRunning)) {
switch ($key) {
"UpArrow" {
Update-UserHistoryIndex -Decrease
$currentPromptInput = Get-UserMessageContentFromHistoryIndex
}
"DownArrow" {
Update-UserHistoryIndex -Increase
$currentPromptInput = Get-UserMessageContentFromHistoryIndex
}
"Escape" {
# Do nothing
}
"Tab" {
# Do nothing
}
"Backspace" {
if ($currentPromptInput.Length -gt 0) {
$currentPromptInput = $currentPromptInput.Substring(0, $currentPromptInput.Length - 1)
}
}
"Enter" {
if ($keyModifiers -eq "Control") {
$currentPromptInput += "`n"
}
elseif ($currentPromptInput.Length -gt 0) {
$isEnterKeyPressed = $true
}
}
default {
if ($keyInfo.KeyChar -and $keyInfo.KeyChar -ne [char]0) {
$currentPromptInput += $keyInfo.KeyChar
}
}
}
}
}
if ($currentPromptInput -ne $script:previousPromptInput) {
Update-PromptCard -PromptInput $currentPromptInput
}
if (Test-AssistantJobCompleted) {
$assistantMessage = Get-AssistantMessage
Add-MessageToHistory -Message $assistantMessage
Remove-LoaderCard
Write-AssistantMessageCard -Message $assistantMessage
Show-PromptCard -PromptInput $currentPromptInput
}
if (Test-AssistantJobRunning) {
Update-LoaderCard
}
if ($isEnterKeyPressed) {
Remove-PromptCard
$userMessage = New-UserMessage -Message $currentPromptInput
Add-MessageToHistory -Message $userMessage
Write-UserMessageCard -Message $userMessage
if ($currentPromptInput.StartsWith("/")) {
$command = $currentPromptInput.Substring(1)
switch ($command) {
'about' {
$systemAboutMessage = New-SystemAboutMessage
Add-MessageToHistory -Message $systemAboutMessage
Write-SystemMessageCard -Message $systemAboutMessage
}
'clear' {
Reset-MessageHistory
Reset-Host
Write-Banner -FromVersion $PreviousCliVersion
}
'docs' {
$url = "https://github.com/startdevx/dx-cli/tree/main/docs"
$systemDocsMessage = New-SystemDocsMessage -Url $url
Add-MessageToHistory -Message $systemDocsMessage
Write-SystemMessageCard -Message $systemDocsMessage
Start-Process -FilePath $url
}
'help' {
$systemHelpMessage = New-SystemHelpMessage
Add-MessageToHistory -Message $systemHelpMessage
Write-SystemMessageCard -Message $systemHelpMessage
}
'quit' {
$systemExitMessage = New-SystemExitMessage
Write-SystemMessageCard -Message $systemExitMessage
exit 0
}
default {
$unknownCommandMessage = New-SystemUnknownCommandMessage -Command $command
Add-MessageToHistory -Message $unknownCommandMessage
Write-SystemMessageCard -Message $unknownCommandMessage
}
}
$currentPromptInput = ""
Show-PromptCard -PromptInput $currentPromptInput
}
else {
Show-LoaderCard
Start-AssistantJob -PromptInput $currentPromptInput -MessageHistory @(Get-MessagesFromHistory)
$currentPromptInput = ""
}
}
$script:previousPromptInput = $currentPromptInput
}