Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Gate behind a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bfollington committed Mar 19, 2024
1 parent 49b3ecc commit 1a9beb2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
20 changes: 20 additions & 0 deletions xcode/Subconscious/Shared/Components/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ enum AppAction: Hashable {
/// Set and persist experimental block editor enabled
case persistBlockEditorEnabled(Bool)
case persistNoosphereLogLevel(Noosphere.NoosphereLogLevel)
case persistAiFeaturesEnabled(Bool)

/// Reset Noosphere Service.
/// This calls `Noosphere.reset` which resets memoized instances of
Expand Down Expand Up @@ -580,6 +581,7 @@ struct AppModel: ModelProtocol {
/// Is experimental block editor enabled?
var isBlockEditorEnabled = false
var noosphereLogLevel: Noosphere.NoosphereLogLevel = .basic
var areAiFeaturesEnabled = false

/// Should recovery mode be presented?
var isRecoveryModePresented = false
Expand Down Expand Up @@ -911,6 +913,12 @@ struct AppModel: ModelProtocol {
environment: environment,
isBlockEditorEnabled: isBlockEditorEnabled
)
case let .persistAiFeaturesEnabled(areAiFeaturesEnabled):
return persistAiFeaturesEnabled(
state: state,
environment: environment,
areAiFeaturesEnabled: areAiFeaturesEnabled
)
case let .persistNoosphereLogLevel(level):
return persistNoosphereLogLevel(
state: state,
Expand Down Expand Up @@ -1822,6 +1830,18 @@ struct AppModel: ModelProtocol {
return Update(state: model)
}

static func persistAiFeaturesEnabled(
state: AppModel,
environment: AppEnvironment,
areAiFeaturesEnabled: Bool
) -> Update<AppModel> {
// Persist value
AppDefaults.standard.areAiFeaturesEnabled = areAiFeaturesEnabled
var model = state
model.areAiFeaturesEnabled = areAiFeaturesEnabled
return Update(state: model)
}

static func persistNoosphereLogLevel(
state: AppModel,
environment: AppEnvironment,
Expand Down
54 changes: 31 additions & 23 deletions xcode/Subconscious/Shared/Components/Deck/DeckView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -860,32 +860,40 @@ struct DeckModel: ModelProtocol {
.eraseToAnyPublisher()

var model = state
model.buffer.append(card)

if model.buffer.count >= 4 {
let entries = model.buffer.compactMap { card in card.entry }
let openAiFx = Future.detached {
let result = await environment.openAiService.sendTextToOpenAI(
entries: entries,
prompt: [OpenAIService.contemplate, OpenAIService.poem, OpenAIService.question, OpenAIService.summarize].randomElement()!
)

switch result {
case .success(let msg):
return DeckAction.prependCards([CardModel(card: .reward(msg), liked: false)])
case .failure(let error):
logger.error("OpenAI: \(error)")
return DeckAction.setAiPrompt("error")
if AppDefaults.standard.areAiFeaturesEnabled {
model.buffer.append(card)

if model.buffer.count >= 4 {
let entries = model.buffer.compactMap { card in card.entry }
let openAiFx = Future.detached {
let result = await environment.openAiService.sendTextToOpenAI(
entries: entries,
prompt: [
OpenAIService.contemplate,
OpenAIService.poem,
OpenAIService.question,
OpenAIService.summarize
].randomElement()!
)

switch result {
case .success(let msg):
return DeckAction.prependCards([CardModel(card: .reward(msg), liked: false)])
case .failure(let error):
logger.error("OpenAI: \(error)")
return DeckAction.setAiPrompt("error")
}
}
.eraseToAnyPublisher()

model.buffer.removeAll()
return update(
state: model,
actions: [.nextCard],
environment: environment
).mergeFx(fx).mergeFx(openAiFx).animation(.easeOutCubic())
}
.eraseToAnyPublisher()

model.buffer.removeAll()
return update(
state: model,
actions: [.nextCard],
environment: environment
).mergeFx(fx).mergeFx(openAiFx).animation(.easeOutCubic())
}

return update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,24 @@ struct DeveloperSettingsView: View {
.pickerStyle(DefaultPickerStyle())
}

Section(header: Text("OpenAI API Key")) {
SecureField("API Key", text: $apiKey)
.disableAutocorrection(true)
Button("Save", action: {
Task { await saveApiKey() }
})
Section(header: Text("AI Features")) {
Toggle(
isOn: app.binding(
get: \.areAiFeaturesEnabled,
tag: AppAction.persistAiFeaturesEnabled
),
label: {
Text("Enable AI Features")
}
)

if app.state.areAiFeaturesEnabled {
SecureField("Open AI API Key", text: $apiKey)
.disableAutocorrection(true)
Button("Save", action: {
Task { await saveApiKey() }
})
}
}
}
.navigationTitle("Developer")
Expand Down
3 changes: 3 additions & 0 deletions xcode/Subconscious/Shared/Services/AppDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct AppDefaults {
@UserDefaultsProperty(forKey: "blockEditor")
var isBlockEditorEnabled: Bool = false

@UserDefaultsProperty(forKey: "aiFeatures")
var areAiFeaturesEnabled: Bool = false

@UserDefaultsProperty(forKey: "selectedAppTab")
// default to the notebook on first run because there will be nothing in the feed
// enums must be serialized when stored as AppDefaults:
Expand Down

0 comments on commit 1a9beb2

Please sign in to comment.