Skip to content

Commit e648660

Browse files
committed
Update package
1 parent 1e20354 commit e648660

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

Package.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ let package = Package(
6666
],
6767
path: "Sources/Lite",
6868
resources: [.process("Resources")]
69+
),
70+
.testTarget(
71+
name: "LiteTests",
72+
dependencies: [
73+
"Lite"
74+
],
75+
path: "Sources/Tests"
6976
)
7077
]
7178
)

Sources/Lite/Intramodular/Lite.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ extension Lite {
2727
///
2828
public final class Lite: _CancellablesProviding, Logging, ObservableObject {
2929
private let queue = TaskQueue()
30-
30+
3131
private var shouldAutoinitializeServices: Bool
3232

3333
@Published private var autoinitializedServices: [any _MIService]? = nil
3434
@Published private var manuallyAddedServices: [any _MIService] = []
35-
35+
3636
// @Published public var modelIdentifierScope: _MLModelIdentifierScope?
3737

3838
public var services: [any _MIService] {
@@ -54,10 +54,10 @@ public final class Lite: _CancellablesProviding, Logging, ObservableObject {
5454
self.setUp()
5555
}
5656
}
57-
57+
5858
private init() {
5959
shouldAutoinitializeServices = true
60-
60+
6161
Task { @MainActor in
6262
self.setUp()
6363
}
@@ -120,34 +120,27 @@ extension Lite {
120120
/// Converts Lite accounts loaded from Lite's managed account store to CoreMI accounts.
121121
@MainActor
122122
private func _serviceAccounts() throws -> [_AnyMIServiceAccount] {
123-
try LTAccountStore.shared.accounts.compactMap {
123+
let allAccounts: IdentifierIndexingArrayOf<LTAccount> = LTAccountStore.shared.accounts + (LTAccountStore.shared._testAccounts ?? [])
124+
125+
return try allAccounts.compactMap { (account: LTAccount) in
124126
let credential = _MIServiceAPIKeyCredential(
125-
apiKey: ($0.credential as! _LTAccountCredential.APIKey).key
127+
apiKey: (account.credential as! _LTAccountCredential.APIKey).key
126128
)
127-
let service: _MIServiceTypeIdentifier = try $0.accountType.__conversion()
129+
let service: _MIServiceTypeIdentifier = try account.accountType.__conversion()
128130

129131
return _AnyMIServiceAccount(
130132
serviceIdentifier: service,
131133
credential: credential
132134
)
133135
}
134136
}
135-
136-
private func _serviceTypes() throws -> [any _MIService.Type] {
137-
try _SwiftRuntime.index
138-
.fetch(
139-
.nonAppleFramework,
140-
.conformsTo((any _MIService).self)
141-
)
142-
.map({ try cast($0) })
143-
}
144-
137+
145138
/// Initializes all CoreMI services that can be initialized using the loaded Lite accounts.
146139
public func _makeServices() async throws -> [any _MIService] {
147-
let serviceTypes = try _serviceTypes()
148-
let accounts = try await _serviceAccounts()
140+
let serviceTypes: [any _MIService.Type] = try TypeMetadata._queryAll(.nonAppleFramework, .conformsTo((any _MIService).self))
141+
let serviceAccounts: [any _MIServiceAccount] = try await _serviceAccounts()
149142

150-
var result: [any _MIService] = await accounts
143+
var result: [any _MIService] = await serviceAccounts
151144
.concurrentMap { account in
152145
await serviceTypes.first(byUnwrapping: { type -> (any _MIService)? in
153146
do {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Copyright (c) Vatsal Manot
3+
//
4+
5+
6+
import Foundation
7+
8+
public struct _PreternaturalDotFile: Codable, Hashable, Sendable {
9+
public var TEST_OPENAI_KEY: String?
10+
}

Sources/Lite/Intramodular/Models/LTAccountStore.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import Swallow
88
import SwiftUIX
99

1010
@MainActor
11-
@Singleton
1211
public final class LTAccountStore: ObservableObject {
12+
public static let shared = LTAccountStore()
13+
1314
@FileStorage(
1415
directory: .appDocuments,
1516
path: "Lite/Accounts",
@@ -19,6 +20,9 @@ public final class LTAccountStore: ObservableObject {
1920
)
2021
public var accounts: IdentifierIndexingArrayOf<LTAccount>
2122

23+
@Published
24+
public var _testAccounts: IdentifierIndexingArrayOf<LTAccount>?
25+
2226
private(set) lazy var allKnownAccountTypeDescriptions = {
2327
IdentifierIndexingArray<any LTAccountTypeDescription, LTAccountTypeIdentifier>(
2428
try! _SwiftRuntime.index
@@ -32,6 +36,28 @@ public final class LTAccountStore: ObservableObject {
3236
)
3337
.sorted(by: { $0.title < $1.title })
3438
}()
39+
40+
public init() {
41+
_loadTestAccountsIfNeeded()
42+
}
43+
}
44+
45+
extension LTAccountStore {
46+
fileprivate func _loadTestAccountsIfNeeded() {
47+
@FileStorage(
48+
url: URL.homeDirectory.appending(path: ".preternatural.toml"),
49+
coder: TOMLCoder()
50+
)
51+
var dotfile: _PreternaturalDotFile? = nil
52+
53+
if let TEST_OPENAI_KEY = dotfile?.TEST_OPENAI_KEY {
54+
self._testAccounts = [LTAccount(
55+
accountType: LTAccountTypeDescriptions.OpenAI().accountType,
56+
credential: _LTAccountCredential.APIKey(key: TEST_OPENAI_KEY),
57+
description: nil
58+
)]
59+
}
60+
}
3561
}
3662

3763
extension LTAccountStore {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright (c) Vatsal Manot
3+
//
4+
5+
import Lite
6+
import XCTest
7+
8+
final class LTAccountStoreTests: XCTestCase {
9+
@MainActor
10+
func testAccountStore() async throws {
11+
let store = LTAccountStore.shared
12+
13+
store
14+
}
15+
}

0 commit comments

Comments
 (0)