Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Mar 8, 2024
1 parent 1e20354 commit e648660
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ let package = Package(
],
path: "Sources/Lite",
resources: [.process("Resources")]
),
.testTarget(
name: "LiteTests",
dependencies: [
"Lite"
],
path: "Sources/Tests"
)
]
)
33 changes: 13 additions & 20 deletions Sources/Lite/Intramodular/Lite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ extension Lite {
///
public final class Lite: _CancellablesProviding, Logging, ObservableObject {
private let queue = TaskQueue()

private var shouldAutoinitializeServices: Bool

@Published private var autoinitializedServices: [any _MIService]? = nil
@Published private var manuallyAddedServices: [any _MIService] = []

// @Published public var modelIdentifierScope: _MLModelIdentifierScope?

public var services: [any _MIService] {
Expand All @@ -54,10 +54,10 @@ public final class Lite: _CancellablesProviding, Logging, ObservableObject {
self.setUp()
}
}

private init() {
shouldAutoinitializeServices = true

Task { @MainActor in
self.setUp()
}
Expand Down Expand Up @@ -120,34 +120,27 @@ extension Lite {
/// Converts Lite accounts loaded from Lite's managed account store to CoreMI accounts.
@MainActor
private func _serviceAccounts() throws -> [_AnyMIServiceAccount] {
try LTAccountStore.shared.accounts.compactMap {
let allAccounts: IdentifierIndexingArrayOf<LTAccount> = LTAccountStore.shared.accounts + (LTAccountStore.shared._testAccounts ?? [])

return try allAccounts.compactMap { (account: LTAccount) in
let credential = _MIServiceAPIKeyCredential(
apiKey: ($0.credential as! _LTAccountCredential.APIKey).key
apiKey: (account.credential as! _LTAccountCredential.APIKey).key
)
let service: _MIServiceTypeIdentifier = try $0.accountType.__conversion()
let service: _MIServiceTypeIdentifier = try account.accountType.__conversion()

return _AnyMIServiceAccount(
serviceIdentifier: service,
credential: credential
)
}
}

private func _serviceTypes() throws -> [any _MIService.Type] {
try _SwiftRuntime.index
.fetch(
.nonAppleFramework,
.conformsTo((any _MIService).self)
)
.map({ try cast($0) })
}


/// Initializes all CoreMI services that can be initialized using the loaded Lite accounts.
public func _makeServices() async throws -> [any _MIService] {
let serviceTypes = try _serviceTypes()
let accounts = try await _serviceAccounts()
let serviceTypes: [any _MIService.Type] = try TypeMetadata._queryAll(.nonAppleFramework, .conformsTo((any _MIService).self))
let serviceAccounts: [any _MIServiceAccount] = try await _serviceAccounts()

var result: [any _MIService] = await accounts
var result: [any _MIService] = await serviceAccounts
.concurrentMap { account in
await serviceTypes.first(byUnwrapping: { type -> (any _MIService)? in
do {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Copyright (c) Vatsal Manot
//


import Foundation

public struct _PreternaturalDotFile: Codable, Hashable, Sendable {
public var TEST_OPENAI_KEY: String?
}
28 changes: 27 additions & 1 deletion Sources/Lite/Intramodular/Models/LTAccountStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import Swallow
import SwiftUIX

@MainActor
@Singleton
public final class LTAccountStore: ObservableObject {
public static let shared = LTAccountStore()

@FileStorage(
directory: .appDocuments,
path: "Lite/Accounts",
Expand All @@ -19,6 +20,9 @@ public final class LTAccountStore: ObservableObject {
)
public var accounts: IdentifierIndexingArrayOf<LTAccount>

@Published
public var _testAccounts: IdentifierIndexingArrayOf<LTAccount>?

private(set) lazy var allKnownAccountTypeDescriptions = {
IdentifierIndexingArray<any LTAccountTypeDescription, LTAccountTypeIdentifier>(
try! _SwiftRuntime.index
Expand All @@ -32,6 +36,28 @@ public final class LTAccountStore: ObservableObject {
)
.sorted(by: { $0.title < $1.title })
}()

public init() {
_loadTestAccountsIfNeeded()
}
}

extension LTAccountStore {
fileprivate func _loadTestAccountsIfNeeded() {
@FileStorage(
url: URL.homeDirectory.appending(path: ".preternatural.toml"),
coder: TOMLCoder()
)
var dotfile: _PreternaturalDotFile? = nil

if let TEST_OPENAI_KEY = dotfile?.TEST_OPENAI_KEY {
self._testAccounts = [LTAccount(
accountType: LTAccountTypeDescriptions.OpenAI().accountType,
credential: _LTAccountCredential.APIKey(key: TEST_OPENAI_KEY),
description: nil
)]
}
}
}

extension LTAccountStore {
Expand Down
15 changes: 15 additions & 0 deletions Sources/Tests/LTAccountStoreTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright (c) Vatsal Manot
//

import Lite
import XCTest

final class LTAccountStoreTests: XCTestCase {
@MainActor
func testAccountStore() async throws {
let store = LTAccountStore.shared

store
}
}

0 comments on commit e648660

Please sign in to comment.