Skip to content

Commit

Permalink
Bug fixes and verbosity (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Jun 30, 2024
1 parent 891c322 commit fc4d461
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
2 changes: 1 addition & 1 deletion PSHelp.Copilot.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RootModule = 'PSHelp.Copilot.psm1'

# Version number of this module.
ModuleVersion = '1.0.5'
ModuleVersion = '1.0.6'

# Supported PSEditions
CompatiblePSEditions = @('Core', 'Desktop')
Expand Down
2 changes: 1 addition & 1 deletion private/Get-APIKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Get-APIKey {
Param(
[Parameter(Position = 0)]
[AllowNull()]
[System.Management.Automation.PSObjectPropertyDescriptor]$ApiKey,
[psobject]$ApiKey,
[switch]$PlainText
)

Expand Down
78 changes: 78 additions & 0 deletions private/Get-MaskedKeyString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using namespace System.Runtime.InteropServices

function Get-MaskedKeyString {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Source,

[Parameter(Mandatory = $true)]
[int]$First,

[Parameter(Mandatory = $true)]
[int]$Last,

[Parameter(Mandatory = $true)]
[int]$MaxNumberOfAsterisks
)

if ($Source.Length -le ($First + $Last)) {
return $Source
}

$numberOfAsterisks = $Source.Length - $First - $Last
if ($numberOfAsterisks -gt $MaxNumberOfAsterisks) {
$numberOfAsterisks = $MaxNumberOfAsterisks
}

$maskedString = $Source.Substring(0, $First) + ('*' * $numberOfAsterisks) + $Source.Substring($Source.Length - $Last, $Last)
return $maskedString
}

# this is a modified function that surfaces the API key that PSOpenAI will use
function Get-ApiKey {
[CmdletBinding()]
[OutputType([securestring])]
Param(
[Parameter(Position = 0)]
[AllowNull()]
[psobject]$ApiKey,
[switch]$PlainText
)

# Search API key below priorities.

if ($PSDefaultParameterValues.ApiKey) {
$key = [string]$PSDefaultParameterValues.ApiKey
Write-Verbose -Message 'API Key found in $PSDefaultParameterValues'
}
# 2. Global variable "OPENAI_API_KEY"
elseif ($null -ne $global:OPENAI_API_KEY -and $global:OPENAI_API_KEY -is [string]) {
$key = [string]$global:OPENAI_API_KEY
Write-Verbose -Message 'API Key found in global variable "OPENAI_API_KEY".'
}
# 3. Environment variable "OPENAI_API_KEY"
elseif ($null -ne $env:OPENAI_API_KEY -and $env:OPENAI_API_KEY -is [string]) {
$key = [string]$env:OPENAI_API_KEY
Write-Verbose -Message 'API Key found in environment variable "OPENAI_API_KEY".'
}

if ($key -is [securestring]) {
$key = Get-DecryptedString $key
}

if ($PlainText) {
return $key
}

if ($key) {
if ($key.StartsWith('sk-', [StringComparison]::Ordinal)) {
$first = 6
}else {
$first = 3
}
Get-MaskedKeyString -Source $key -First $first -Last 2 -MaxNumberOfAsterisks 45
} else {
$null
}
}
22 changes: 20 additions & 2 deletions public/Get-OpenAIProvider.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,45 @@ function Get-OpenAIProvider {
$configFile = Join-Path -Path $script:configdir -ChildPath config.json

if ($Persisted) {
Write-Verbose "Persisted switch used. Retrieving persisted configuration."
if (Test-Path -Path $configFile) {
Write-Verbose "Persisted configuration file found. Reading configuration."
Get-Content -Path $configFile -Raw | ConvertFrom-Json
} else {
Write-Warning "No persisted configuration found."
}
} else {
Write-Verbose "Retrieving current session's OpenAI provider configuration."
$context = Get-OpenAIContext
if ($context.ApiBase) {

if ($context.ApiKey) {
Write-Verbose "Context found. Processing configuration."

if ($context.ApiKey) {
Write-Verbose "ApiKey found in context. Decrypting ApiKey."
$decryptedkey = Get-DecryptedString -SecureString $context.ApiKey

if ($decryptedkey) {
Write-Verbose "ApiKey decrypted successfully. Masking ApiKey."
$splat = @{
Source = $decryptedkey
First = $first
Last = 2
MaxNumberOfAsterisks = 45
}
$maskedkey = Get-MaskedString @splat
$maskedkey = Get-MaskedKeyString @splat
} else {
Write-Verbose "Failed to decrypt ApiKey."
$maskedkey = $null
}
}

if ($PlainText) {
Write-Verbose "PlainText switch used. Returning ApiKey in plain text."
$maskedkey = $decryptedkey
}

Write-Verbose "Creating configuration object."
[pscustomobject]@{
ApiKey = $maskedkey
AuthType = $context.AuthType
Expand All @@ -70,12 +82,18 @@ function Get-OpenAIProvider {
Organization = $context.Organization
}
} else {
Write-Verbose "No context found. Attempting to retrieve ApiKey from environment."
$maskedkey = Get-ApiKey

if ($maskedkey) {
Write-Verbose "ApiKey found in environment. Setting AuthType to 'openai'."
$auth = "openai"
} else {
Write-Verbose "No ApiKey found. Setting AuthType to null."
$auth = $null
}

Write-Verbose "Creating default configuration object."
[pscustomobject]@{
ApiKey = $maskedkey
AuthType = $auth
Expand Down
44 changes: 36 additions & 8 deletions public/Set-OpenAIProvider.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,29 @@ function Set-OpenAIProvider {
[string]$Organization,
[switch]$NoPersist
)
begin {
$PSDefaultParameterValues["*-Variable:Scope"] = 1
# Retrieve the current PSDefaultParameterValues
$defaultvalues = Get-Variable -Name PSDefaultParameterValues -ErrorAction SilentlyContinue

# Check if the variable exists, if not, initialize it as an empty hashtable
if (-not $defaultvalues) {
$currentDefaults = @{}
} else {
$currentDefaults = $defaultvalues.Value
}
}
process {
if (-not $AuthType) {
$AuthType = if ($ApiType -eq 'Azure') { 'Azure' } else { 'OpenAI' }
}

if ($PSBoundParameters.Count -eq 1 -and $Deployment) {
Set-Variable -Scope 1 -Name PSDefaultParameterValues -Force -ErrorAction SilentlyContinue -Value @{
'*:Deployment' = $Deployment
'*:Model' = $Deployment
}
$currentDefaults['*:Deployment'] = $Deployment
$currentDefaults['*:Model'] = $Deployment

# Set the updated PSDefaultParameterValues back
$null = Set-Variable -Name PSDefaultParameterValues -Value $currentDefaults -Force
Get-OpenAIProvider
return
}
Expand All @@ -105,10 +117,11 @@ function Set-OpenAIProvider {
$splat.ApiVersion = $ApiVersion
}
if ($Deployment) {
Set-Variable -Scope 1 -Name PSDefaultParameterValues -Force -ErrorAction SilentlyContinue -Value @{
'*:Deployment' = $Deployment
'*:Model' = $Deployment
}
$currentDefaults['*:Deployment'] = $Deployment
$currentDefaults['*:Model'] = $Deployment

# Set the updated PSDefaultParameterValues back
$null = Set-Variable -Name PSDefaultParameterValues -Value $currentDefaults -Force
}
} else {
# Set context for OpenAI
Expand All @@ -123,6 +136,21 @@ function Set-OpenAIProvider {
}
$null = Set-OpenAIContext @splat

# get context values to pass to Get-OpenAIAPIParameter
$context = Get-OpenAIContext
$currentDefaults['Get-OpenAIAPIParameter:Parameters'] = $script:defaultapiparms = @{
ApiKey = $context.ApiKey
AuthType = $context.AuthType
Organization = $context.Organization
ApiBase = $context.ApiBase
ApiVersion = $context.ApiVersion
TimeoutSec = $context.TimeoutSec
MaxRetryCount = $context.MaxRetryCount
}
$currentDefaults['Initialize-APIKey:ApiKey'] = $context.ApiKey

$null = Set-Variable -Name PSDefaultParameterValues -Value $currentDefaults -Force

if (-not $NoPersist) {
$configFile = Join-Path -Path $script:configdir -ChildPath config.json
try {
Expand Down

0 comments on commit fc4d461

Please sign in to comment.