-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSHelp.Copilot.psm1
76 lines (67 loc) · 2.37 KB
/
PSHelp.Copilot.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
$script:PSModuleRoot = $script:ModuleRoot = $PSScriptRoot
$script:totalcost = 0.0
$script:threadcache = @{}
switch ($PSVersionTable.Platform) {
"Unix" {
$script:configdir = "$home/.config/PSHelp.Copilot"
if (-not (Test-Path -Path $script:configdir)) {
$null = New-Item -Path $script:configdir -ItemType Directory -Force
}
}
default {
$script:configdir = "$env:APPDATA\PSHelp.Copilot"
if (-not (Test-Path -Path $script:configdir)) {
$null = New-Item -Path $script:configdir -ItemType Directory -Force
}
}
}
function Import-ModuleFile {
[CmdletBinding()]
Param (
[string]
$Path
)
if ($doDotSource) { . $Path }
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null) }
}
foreach ($function in (Get-ChildItem "$ModuleRoot\private\" -Filter "*.ps1" -Recurse -ErrorAction Ignore)) {
. Import-ModuleFile -Path $function.FullName
}
# Import all public functions
foreach ($function in (Get-ChildItem "$ModuleRoot\public" -Filter "*.ps1" -Recurse -ErrorAction Ignore)) {
. Import-ModuleFile -Path $function.FullName
}
Set-Alias -Name Reset-OpenAIProvider -Value Clear-OpenAIProvider
Set-Alias -Name askhelp -Value Invoke-HelpChat
$configFile = Join-Path -Path $script:configdir -ChildPath config.json
if (Test-Path -Path $configFile) {
$persisted = Get-Content -Path $configFile -Raw | ConvertFrom-Json
$splat = @{}
if ($persisted.ApiKey) {
$splat.ApiKey = $persisted.ApiKey
}
if ($persisted.ApiBase) {
$splat.ApiBase = $persisted.ApiBase
}
if ($persisted.Deployment) {
$splat.Deployment = $persisted.Deployment
}
if ($persisted.ApiType) {
$splat.ApiType = $persisted.ApiType
}
if ($persisted.ApiVersion) {
$splat.ApiVersion = $persisted.ApiVersion
}
if ($persisted.AuthType) {
$splat.AuthType = $persisted.AuthType
}
if ($persisted.Organization) {
$splat.Organization = $persisted.Organization
}
$null = Set-OpenAIProvider @splat
}
$PSDefaultParameterValues['Import-Module:Verbose'] = $false
$PSDefaultParameterValues['Add-Type:Verbose'] = $false
if (-not (Get-OpenAIProvider).ApiKey) {
Write-Warning "No API key found. Use Set-OpenAIProvider or `$env:OPENAI_API_KEY to set the API key."
}