forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalInferenceService.swift
More file actions
103 lines (90 loc) · 2.92 KB
/
Copy pathLocalInferenceService.swift
File metadata and controls
103 lines (90 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import Foundation
/// Engine identifiers the runtime knows how to select.
///
/// AFM is named so `forceLocalInferenceEngine=afm` fails closed in this shard
/// (S12 lands the adapter). There is no cloud case: a missing or failed local
/// engine becomes the deterministic minimum, never luna.
enum LocalInferenceEngineID: String, Sendable, Equatable, CaseIterable {
case localServer = "local-server"
case afm = "afm"
static func parse(_ raw: String) -> LocalInferenceEngineID? {
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
switch trimmed {
case Self.localServer.rawValue, "local_server", "local", "ollama", "llamacpp":
return .localServer
case Self.afm.rawValue, "foundation-models", "foundation_models":
return .afm
default:
return nil
}
}
}
struct LocalInferenceCapabilities: Sendable, Equatable {
var structuredOutput: Bool
var toolLoop: Bool
var contextWindowTokens: Int
}
struct LocalInferenceJSONSchema: Sendable, Equatable {
var name: String
var json: Data
}
struct LocalInferenceToolSpec: Sendable, Equatable {
var name: String
var description: String
var parametersJSON: Data
}
struct ToolLoopBudget: Sendable, Equatable {
var maxIterations: Int
}
struct ToolLoopResult: Sendable, Equatable {
var content: String
var toolCallNames: [String]
}
enum LocalInferenceError: Error, Equatable {
case disabled
case engineUnavailable(LocalInferenceEngineID)
case unknownEngine(String)
case nonLoopbackBaseURL(String)
case engineFailed(String)
case capabilityUnavailable(String)
case invalidResponse(String)
case httpStatus(Int)
}
/// Local LLM execution port. Engines implement this; the runtime selects one
/// and fail-closes to the deterministic minimum. Tool loops ship as a stub
/// (TBD-2 / S12) and stay feature-detected per engine.
protocol LocalInferenceService: Sendable {
var engineID: LocalInferenceEngineID { get }
var capabilities: LocalInferenceCapabilities { get }
func generateStructured<T: Decodable>(prompt: String, schema: LocalInferenceJSONSchema) async throws -> T
func runToolLoop(prompt: String, tools: [LocalInferenceToolSpec], budget: ToolLoopBudget) async throws
-> ToolLoopResult
}
enum LocalInferenceGeneration<T: Sendable>: Sendable {
case engine(T, engineID: LocalInferenceEngineID)
case deterministicMinimum(DeterministicConversationMinimum)
}
protocol LocalInferenceFallbackRecording: Sendable {
func recordLocalInferenceFallback(
from: String,
to: String,
reason: String,
outcome: DesktopFallbackOutcome
)
}
struct DesktopLocalInferenceFallbackRecorder: LocalInferenceFallbackRecording {
func recordLocalInferenceFallback(
from: String,
to: String,
reason: String,
outcome: DesktopFallbackOutcome
) {
DesktopDiagnosticsManager.shared.recordFallback(
area: "local_llm",
from: from,
to: to,
reason: reason,
outcome: outcome
)
}
}