Skip to content

Commit

Permalink
fixing error while showing Set API key in command palette
Browse files Browse the repository at this point in the history
  • Loading branch information
lamg committed Aug 24, 2024
1 parent f417f08 commit 3a833a0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.1.4] - 2024-08-24

Changed:

- configuration moved from $HOME/.config/r0b0t.json to $HOME/.config/r0b0t/conf.json
- internal design based on events, moving event handlers to GtkGui module and simplifying @lanayx style dependency injection, eliminating it for the Core module. Navigation was simplified by relying on fixed configuration groups rather than a tree.

Fixed:

- Display of existing API keys in command palette

# [0.1.3] - 2024-08-09

Added:
Expand Down
30 changes: 18 additions & 12 deletions R0b0t.Lib/Configuration.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Configuration

open System
open System.IO

type Key = Key of string

Expand Down Expand Up @@ -52,11 +53,13 @@ let providersModels =
HuggingFace, huggingFaceModels
ImaginePro, imagineProAiModels ]

let confPath =
(LamgEnv.getEnv "HOME" |> Option.defaultValue "~")
:: [ ".config"; "r0b0t.json" ]
|> List.toArray
|> System.IO.Path.Join
let confDir =
(LamgEnv.getEnv "HOME" |> Option.defaultValue "~") :: [ ".config"; "r0b0t" ]

let confPath = confDir @ [ "conf.json" ] |> List.toArray |> Path.Join

let ensureConfDirExists () =
confDir |> List.toArray |> Path.Join |> Directory.CreateDirectory |> ignore

type ConfigurationManager() =
let mutable conf =
Expand Down Expand Up @@ -128,13 +131,16 @@ type ConfigurationManager() =
|> List.map (fun (p, Key k) -> p.ToString(), k)
|> Map.ofList

{ provider = provider
model = model
provider_keys = keys }
|> (fun v ->
let opts = Text.Json.JsonSerializerOptions(WriteIndented = true)
Text.Json.JsonSerializer.Serialize<SerializableConf>(v, opts))
|> fun json -> IO.File.WriteAllText(confPath, json)
let serializable =
{ provider = provider
model = model
provider_keys = keys }

let opts = Text.Json.JsonSerializerOptions(WriteIndented = true)
let json = Text.Json.JsonSerializer.Serialize<SerializableConf>(serializable, opts)

ensureConfDirExists ()
File.WriteAllText(confPath, json)
with e ->
eprintfn $"failed to store configuration: {e.Message}"

Expand Down
2 changes: 1 addition & 1 deletion R0b0t.Lib/GtkGui.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let populateListBox (l: ListBox) (xs: Row array) =
let appendRow { label = label; control = control } =
match control with
| Checkbox v -> boolSetting label v
| Entry _ -> stringSetting label ""
| Entry text -> stringSetting label text
| SettingGroup descr -> settingGroup (label, descr)
|> l.Append

Expand Down
32 changes: 19 additions & 13 deletions R0b0t.Lib/Navigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ type Setting = { row: Row; request: Request }
let settingGroups =
[| { row =
{ label = "Set provider"
control = SettingGroup "SetProvider" }
control = SettingGroup "AI platform giving access to LLMs through an API" }
request = Skip }
{ row =
{ label = "Set model"
control = SettingGroup "Set model" }
control = SettingGroup "Large Language Models available for the selected provider" }
request = Skip }
{ row =
{ label = "Set API key"
control = SettingGroup "Set API key" }
control = SettingGroup "API key for the selected provider for authorizing the requests" }
request = Skip } |]

let providers = [| OpenAI; Anthropic; HuggingFace; GitHub; ImaginePro |]
Expand Down Expand Up @@ -69,13 +69,14 @@ let setModelsGroup =
(p, settings))
|> Map.ofList

let newSetApiKeyGroup (providerKeys: Map<Provider, Key>) =
providerKeys
|> Map.map (fun p key ->
[| { row =
{ label = "API key"
control = Entry(let (Key k) = key in k) }
request = SetApiKey(p, key) } |])
let apiKeySetting provider (Key key) =
[| { row =
{ label = "API key"
control = Entry key }
request = SetApiKey(provider, (Key key)) } |]


let newSetApiKeyGroup (providerKeys: Map<Provider, Key>) = providerKeys |> Map.map apiKeySetting


type NavigationHandler(conf: Configuration, requester: Event<Request>) =
Expand All @@ -100,12 +101,17 @@ type NavigationHandler(conf: Configuration, requester: Event<Request>) =
2, Entry "" |]

let getGroupSettings group =
let activeProvider = fst groupActiveItem[0]
let activeProvider = providers[fst groupActiveItem[0]]

match group with
| 0 -> setProviderGroup
| 1 -> setModelsGroup[providers[activeProvider]]
| 2 -> setApiKeysGroup[providers[activeProvider]]
| 1 -> setModelsGroup[activeProvider]
| 2 ->
if setApiKeysGroup.ContainsKey activeProvider then
setApiKeysGroup[activeProvider]
else
apiKeySetting activeProvider (Key "")

| _ -> failwith $"mismatch between UI and NavigationHandler, setting group {group} out of range"

// index: optional index of the item the user wants to activate
Expand Down
2 changes: 1 addition & 1 deletion r0b0t/r0b0t.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageOutputPath>./nupkg</PackageOutputPath>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>0.1.3</Version>
<Version>0.1.4</Version>
<Authors>Luis Ángel Méndez Gort</Authors>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<AssemblyName>r0b0t</AssemblyName>
Expand Down

0 comments on commit 3a833a0

Please sign in to comment.