-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPSWeave.psm1
203 lines (157 loc) · 5.73 KB
/
PSWeave.psm1
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Class InstructionPrompts : System.Management.Automation.IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$prompts = Get-InstructionPromptNames
return ($prompts.Keys | Sort-Object)
}
}
$script:instructionPromptPath = [System.Collections.Generic.HashSet[string]]::new()
$script:instructionPromptPath += "$PSScriptRoot\InstructionPrompts"
<#
.SYNOPSIS
Weaves instructions into a prompt.
.DESCRIPTION
The Weave function is used to weave instructions into a prompt. It takes user input and processes it based on the specified instructions.
.PARAMETER Instructions
Specifies the set of instructions to use for weaving. Valid values are obtained from the Get-InstructionPromptNames function.
.PARAMETER UserInput
Specifies the user input to be processed. This parameter accepts input from the pipeline.
.SWITCHPARAMETER Chat
Indicates whether to enable chat mode. This feature is not yet implemented.
.SWITCHPARAMETER ListPrompts
Indicates whether to list all available instruction prompts.
.INPUTS
System.Object
.OUTPUTS
System.Object
.EXAMPLE
"arm templates, bicep, ansible, terraform" | Weave CompareAndContrast
| | Arm Templates | Bicep | Ansible | Terraform |
|-----------|---------------|-------|---------|-----------|
| Language | JSON | Bicep | YAML | HCL |
| Complexity| High | Low | Medium | Medium |
| Provisioning| Yes | Yes | Yes | Yes |
| Configuration Management| No | No | Yes | No |
| Cloud Compatibility| Azure | Azure | Multi-Cloud | Multi-Cloud |
| Community Support| Good | Growing| Strong | Strong |
| Learning Curve| Steep | Moderate | Moderate | Moderate |
.EXAMPLE
Weave -ListPrompt
This example lists all available instruction prompts.
#>
function Weave {
[CmdletBinding()]
param(
[ValidateSet([InstructionPrompts])]
$Instructions,
[Parameter(ValueFromPipeline)]
$UserInput,
[Switch]$ListPrompt,
[Switch]$Chat
)
Process { $lines += @($UserInput) }
End {
if ($ListPrompt) {
(Get-InstructionPromptNames).Keys | Sort-Object
return
}
$instructionsFile = (Get-InstructionPromptNames)[$instructions]
$instructionPrompt = Get-Content $instructionsFile -Raw
if ($Chat) {
'Not yet implemented, coming soon...'
}
else {
$instructionPrompt = $ExecutionContext.InvokeCommand.ExpandString($instructionPrompt)
Write-Verbose $instructionPrompt
$lines | Invoke-OAIChat -Instructions $instructionPrompt
}
}
}
<#
.SYNOPSIS
Retrieves the names and full paths of instruction prompt files.
.DESCRIPTION
The Get-InstructionPromptNames function retrieves the names and full paths of instruction prompt files located in the script's instructionPromptPath directory. It returns an ordered hashtable where the keys are the base names of the files and the values are their corresponding full paths.
.PARAMETER None
This function does not accept any parameters.
.EXAMPLE
Get-InstructionPromptNames
This example demonstrates how to use the Get-InstructionPromptNames function to retrieve the names and full paths of instruction prompt files.
.OUTPUTS
System.Collections.Specialized.OrderedDictionary
The function returns an ordered hashtable where the keys are the base names of the instruction prompt files and the values are their corresponding full paths.
#>
function Get-InstructionPromptNames {
[CmdletBinding()]
$map = [Ordered]@{}
Get-ChildItem $script:instructionPromptPath | ForEach-Object {
$map[$_.BaseName] = $_.FullName
}
$map
}
<#
.SYNOPSIS
Adds a path to the instruction prompt path.
.DESCRIPTION
The Add-PromptPath function adds a specified path to the instruction prompt path.
The instruction prompt path is a collection of paths that are used by the instruction prompt to search for executable files.
.PARAMETER Path
Specifies the path to be added to the instruction prompt path.
.EXAMPLE
Add-PromptPath -Path "C:\Program Files\MyApp"
This example adds "C:\Program Files\MyApp" to the instruction prompt path.
#>
function Add-PromptPath {
[CmdletBinding()]
param(
[string]$Path
)
if (-not (Test-Path $Path)) {
Write-Error "Path $Path does not exist"
return
}
$Path = (Resolve-Path $Path).Path
$script:instructionPromptPath += @($Path)
}
<#
.SYNOPSIS
Retrieves the prompt path.
.DESCRIPTION
The Get-WeavePromptPath function retrieves the prompt path.
.PARAMETER None
This function does not accept any parameters.
.EXAMPLE
Get-WeavePromptPath
This example demonstrates how to use the Get-WeavePromptPath function to retrieve the prompt path.
.OUTPUTS
[System.String]
The function returns a string representing the prompt path.
#>
function Get-WeavePromptPath {
[CmdletBinding()]
param()
$script:instructionPromptPath
}
<#
.SYNOPSIS
Retrieves the content of a specified instruction file.
.DESCRIPTION
The Get-WeaveContent function retrieves the content of a specified instruction file. It searches for the file recursively in the instruction prompt path and returns the content as a string array.
.PARAMETER Instructions
Specifies the type of instruction prompts to retrieve. Valid values are "InstructionPrompts".
.EXAMPLE
Get-WeaveContent -Instructions InstructionPrompts
# Retrieves the content of the "InstructionPrompts.txt" file.
.INPUTS
None.
.OUTPUTS
System.String[]
#>
function Get-WeaveContent {
[CmdletBinding()]
param(
[ValidateSet([InstructionPrompts])]
$Instructions
)
$instructionFile = "$($Instructions).txt"
Get-ChildItem $script:instructionPromptPath -Recurse $instructionFile | Get-Content
}