1 // This file is automatically generated, so please do not edit it.
2 // @generated by `flutter_rust_bridge`@ 2.12.0.
3
4 // ignore_for_file: type=lint, unused_import, unnecessary_import
5
6 // ignore_for_file: invalid_use_of_internal_member
7
8 import 'frb_generated.dart';
9 import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
10 import 'package:freezed_annotation/freezed_annotation.dart' hide protected;
11 part 'api.freezed.dart';
12
13 // These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `assert_receiver_is_total_eq`, `assert_receiver_is_total_eq`, `clone`, `clone`, `clone`, `clone`, `clone`, `clone`, `clone`, `clone`, `clone`, `eq`, `eq`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`
14
15 /// Return the platform-appropriate default `GgufModelConfig`.
16 ///
17 /// Selects the 1.5B model on iOS / tvOS / Android and the 3B model on
18 /// macOS / Windows / Linux.
19 GgufModelConfig defaultModelConfig() =>
20 RustLib.instance.api.crateApiDefaultModelConfig();
21
22 /// `GgufModelConfig` for Qwen 2.5 1.5B Instruct Q4_K_M (~941 MB).
23 GgufModelConfig qwen2515BConfig() =>
24 RustLib.instance.api.crateApiQwen2515BConfig();
25
26 /// `GgufModelConfig` for Qwen 2.5 3B Instruct Q4_K_M (~1.93 GB).
27 GgufModelConfig qwen253BConfig() =>
28 RustLib.instance.api.crateApiQwen253BConfig();
29
30 /// `GgufModelConfig` for Qwen 2.5 Coder 1.5B Instruct Q4_K_M (~941 MB).
31 GgufModelConfig qwen25Coder15BConfig() =>
32 RustLib.instance.api.crateApiQwen25Coder15BConfig();
33
34 /// `GgufModelConfig` for Qwen 2.5 Coder 3B Instruct Q4_K_M (~1.93 GB).
35 GgufModelConfig qwen25Coder3BConfig() =>
36 RustLib.instance.api.crateApiQwen25Coder3BConfig();
37
38 /// Default sampling config: `temperature=0.7`, `top_p=0.95`, `max_tokens=512`.
39 SamplingConfig defaultSamplingConfig() =>
40 RustLib.instance.api.crateApiDefaultSamplingConfig();
41
42 /// Deterministic sampling config: `temperature=0.0`, greedy, `max_tokens=512`.
43 SamplingConfig deterministicSamplingConfig() =>
44 RustLib.instance.api.crateApiDeterministicSamplingConfig();
45
46 /// Mobile sampling config: `temperature=0.7`, `top_p=0.95`, `max_tokens=128`.
47 SamplingConfig mobileSamplingConfig() =>
48 RustLib.instance.api.crateApiMobileSamplingConfig();
49
50 /// Seed the HuggingFace cache environment for sandboxed platforms.
51 ///
52 /// On iOS (and Android), the default `~/.cache/huggingface/hub` path is outside
53 /// the app sandbox and writes fail with `Operation not permitted (os error 1)`.
54 ///
55 /// Call this **once** at startup — before any `load_gguf_model` or
56 /// `download_model` — with the app's writable data directory.
57 ///
58 /// # Dart usage
59 ///
60 /// ```dart
61 /// import 'package:path_provider/path_provider.dart';
62 ///
63 /// final dir = await getApplicationSupportDirectory();
64 /// configureCacheDir(appDataDir: dir.path);
65 /// ```
66 ///
67 /// Internally this:
68 /// 1. Creates `<app_data_dir>/huggingface/hub/` if it doesn't exist.
69 /// 2. Sets the `HF_HOME` and `HF_HUB_CACHE` environment variables.
70 /// 3. Seeds `mistralrs_core::GLOBAL_HF_CACHE` so the model loader never
71 /// falls back to `Cache::default()`.
72 void configureCacheDir({required String appDataDir}) =>
73 RustLib.instance.api.crateApiConfigureCacheDir(appDataDir: appDataDir);
74
75 // Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<OndeChatEngine>>
76 abstract class OndeChatEngine implements RustOpaqueInterface {
77 /// Clear the conversation history while keeping the model loaded.
78 ///
79 /// Returns the number of turns that were removed.
80 Future<BigInt> clearHistory();
81
82 /// Clear the current system prompt.
83 Future<void> clearSystemPrompt();
84
85 /// One-shot generation from an explicit message list.
86 ///
87 /// Does **not** modify the conversation history.
88 Future<InferenceResult> generate({
89 required List<ChatMessage> messages,
90 SamplingConfig? sampling,
91 });
92
93 /// Returns a clone of the full conversation history.
94 Future<List<ChatMessage>> history();
95
96 /// Returns a point-in-time snapshot of the engine's state.
97 Future<EngineInfo> info();
98
99 /// Returns `true` if a model is currently loaded and ready.
100 Future<bool> isLoaded();
101
102 /// Load the model assigned to this Onde app from the dashboard.
103 ///
104 /// Returns the wall-clock seconds taken to load the model.
105 Future<double> loadAssignedModel({
106 required String appId,
107 required String appSecret,
108 String? systemPrompt,
109 SamplingConfig? sampling,
110 });
111
112 /// Load a GGUF model into the engine.
113 ///
114 /// Returns the load duration in seconds on success.
115 Future<double> loadGgufModel({
116 required GgufModelConfig config,
117 String? systemPrompt,
118 SamplingConfig? sampling,
119 });
120
121 /// Create a new engine with no model loaded.
122 factory OndeChatEngine() => RustLib.instance.api.crateApiOndeChatEngineNew();
123
124 /// Append a message to history without running inference.
125 ///
126 /// Useful for restoring a saved conversation or injecting context.
127 Future<void> pushHistory({required ChatMessage message});
128
129 /// Send a user message and receive a complete assistant reply.
130 ///
131 /// Both the user message and assistant reply are appended to the
132 /// conversation history on success.
133 Future<InferenceResult> sendMessage({required String message});
134
135 /// Replace the sampling configuration used for all subsequent inference.
136 Future<void> setSampling({required SamplingConfig sampling});
137
138 /// Set or replace the system prompt.
139 ///
140 /// The prompt is prepended to every inference request and is **not**
141 /// stored in the conversation history.
142 Future<void> setSystemPrompt({required String prompt});
143
144 /// Stream a reply token-by-token.
145 ///
146 /// Each [`StreamChunk`] carries a `delta` string. The final chunk has
147 /// `done == true`. The `sink` is closed automatically when the stream
148 /// ends or if the receiver is dropped.
149 Stream<StreamChunk> streamMessage({required String message});
150
151 /// Unload the current model and release its memory.
152 ///
153 /// Returns the display name of the model that was unloaded, or `None`
154 /// if no model was loaded.
155 Future<String?> unloadModel();
156 }
157
158 /// A single message in a conversation.
159 class ChatMessage {
160 final ChatRole role;
161 final String content;
162
163 const ChatMessage({required this.role, required this.content});
164
165 @override
166 int get hashCode => role.hashCode ^ content.hashCode;
167
168 @override
169 bool operator ==(Object other) =>
170 identical(this, other) ||
171 other is ChatMessage &&
172 runtimeType == other.runtimeType &&
173 role == other.role &&
174 content == other.content;
175 }
176
177 /// Role of a participant in a chat conversation.
178 enum ChatRole { system, user, assistant }
179
180 /// A point-in-time snapshot of the engine's state.
181 class EngineInfo {
182 final EngineStatus status;
183
184 /// Display name of the currently loaded model, if any.
185 final String? modelName;
186
187 /// Approximate memory footprint of the loaded model, if any.
188 final String? approxMemory;
189
190 /// Number of conversation turns in the current history.
191 final BigInt historyLength;
192
193 const EngineInfo({
194 required this.status,
195 this.modelName,
196 this.approxMemory,
197 required this.historyLength,
198 });
199
200 @override
201 int get hashCode =>
202 status.hashCode ^
203 modelName.hashCode ^
204 approxMemory.hashCode ^
205 historyLength.hashCode;
206
207 @override
208 bool operator ==(Object other) =>
209 identical(this, other) ||
210 other is EngineInfo &&
211 runtimeType == other.runtimeType &&
212 status == other.status &&
213 modelName == other.modelName &&
214 approxMemory == other.approxMemory &&
215 historyLength == other.historyLength;
216 }
217
218 /// Lifecycle status of the inference engine.
219 enum EngineStatus { unloaded, loading, ready, generating, error }
220
221 /// Configuration for loading a pre-quantised GGUF model.
222 class GgufModelConfig {
223 /// HuggingFace repository ID, e.g. `"bartowski/Qwen2.5-1.5B-Instruct-GGUF"`.
224 final String modelId;
225
226 /// GGUF filename(s) within the repository.
227 final List<String> files;
228
229 /// Optional explicit tokenizer model ID (required on Android).
230 final String? tokModelId;
231
232 /// Human-friendly display name, e.g. `"Qwen 2.5 1.5B"`.
233 final String displayName;
234
235 /// Approximate memory footprint, e.g. `"~941 MB (GGUF Q4_K_M)"`.
236 final String approxMemory;
237
238 const GgufModelConfig({
239 required this.modelId,
240 required this.files,
241 this.tokModelId,
242 required this.displayName,
243 required this.approxMemory,
244 });
245
246 @override
247 int get hashCode =>
248 modelId.hashCode ^
249 files.hashCode ^
250 tokModelId.hashCode ^
251 displayName.hashCode ^
252 approxMemory.hashCode;
253
254 @override
255 bool operator ==(Object other) =>
256 identical(this, other) ||
257 other is GgufModelConfig &&
258 runtimeType == other.runtimeType &&
259 modelId == other.modelId &&
260 files == other.files &&
261 tokModelId == other.tokModelId &&
262 displayName == other.displayName &&
263 approxMemory == other.approxMemory;
264 }
265
266 /// The result of a completed inference request.
267 class InferenceResult {
268 /// The generated assistant reply text.
269 final String text;
270
271 /// Wall-clock inference duration in seconds.
272 final double durationSecs;
273
274 /// Human-readable duration string, e.g. `"4.5s"` or `"2m 3.1s"`.
275 final String durationDisplay;
276
277 /// Finish reason reported by the model, e.g. `"stop"` or `"length"`.
278 final String finishReason;
279
280 /// Tool calls requested by the model (empty if none).
281 final List<ToolCallInfo> toolCalls;
282
283 const InferenceResult({
284 required this.text,
285 required this.durationSecs,
286 required this.durationDisplay,
287 required this.finishReason,
288 required this.toolCalls,
289 });
290
291 @override
292 int get hashCode =>
293 text.hashCode ^
294 durationSecs.hashCode ^
295 durationDisplay.hashCode ^
296 finishReason.hashCode ^
297 toolCalls.hashCode;
298
299 @override
300 bool operator ==(Object other) =>
301 identical(this, other) ||
302 other is InferenceResult &&
303 runtimeType == other.runtimeType &&
304 text == other.text &&
305 durationSecs == other.durationSecs &&
306 durationDisplay == other.durationDisplay &&
307 finishReason == other.finishReason &&
308 toolCalls == other.toolCalls;
309 }
310
311 @freezed
312 sealed class OndeError with _$OndeError implements FrbException {
313 const OndeError._();
314
315 const factory OndeError.noModelLoaded() = OndeError_NoModelLoaded;
316 const factory OndeError.alreadyLoaded({required String modelName}) =
317 OndeError_AlreadyLoaded;
318 const factory OndeError.modelBuild({required String reason}) =
319 OndeError_ModelBuild;
320 const factory OndeError.inference({required String reason}) =
321 OndeError_Inference;
322 const factory OndeError.cancelled() = OndeError_Cancelled;
323 const factory OndeError.other({required String reason}) = OndeError_Other;
324 }
325
326 /// Sampling parameters for text generation. All fields are optional — `None`
327 /// means "use the engine default".
328 class SamplingConfig {
329 final double? temperature;
330 final double? topP;
331 final BigInt? topK;
332 final double? minP;
333 final BigInt? maxTokens;
334 final double? frequencyPenalty;
335 final double? presencePenalty;
336
337 const SamplingConfig({
338 this.temperature,
339 this.topP,
340 this.topK,
341 this.minP,
342 this.maxTokens,
343 this.frequencyPenalty,
344 this.presencePenalty,
345 });
346
347 @override
348 int get hashCode =>
349 temperature.hashCode ^
350 topP.hashCode ^
351 topK.hashCode ^
352 minP.hashCode ^
353 maxTokens.hashCode ^
354 frequencyPenalty.hashCode ^
355 presencePenalty.hashCode;
356
357 @override
358 bool operator ==(Object other) =>
359 identical(this, other) ||
360 other is SamplingConfig &&
361 runtimeType == other.runtimeType &&
362 temperature == other.temperature &&
363 topP == other.topP &&
364 topK == other.topK &&
365 minP == other.minP &&
366 maxTokens == other.maxTokens &&
367 frequencyPenalty == other.frequencyPenalty &&
368 presencePenalty == other.presencePenalty;
369 }
370
371 /// A single streaming token chunk emitted during inference.
372 class StreamChunk {
373 /// The token text delta (may be empty on the final chunk).
374 final String delta;
375
376 /// `true` when this is the last chunk in the stream.
377 final bool done;
378
379 /// Finish reason (set only on the final chunk).
380 final String? finishReason;
381
382 const StreamChunk({
383 required this.delta,
384 required this.done,
385 this.finishReason,
386 });
387
388 @override
389 int get hashCode => delta.hashCode ^ done.hashCode ^ finishReason.hashCode;
390
391 @override
392 bool operator ==(Object other) =>
393 identical(this, other) ||
394 other is StreamChunk &&
395 runtimeType == other.runtimeType &&
396 delta == other.delta &&
397 done == other.done &&
398 finishReason == other.finishReason;
399 }
400
401 /// Structured tool-call request emitted by the model.
402 class ToolCallInfo {
403 final String id;
404 final String functionName;
405 final String arguments;
406
407 const ToolCallInfo({
408 required this.id,
409 required this.functionName,
410 required this.arguments,
411 });
412
413 @override
414 int get hashCode => id.hashCode ^ functionName.hashCode ^ arguments.hashCode;
415
416 @override
417 bool operator ==(Object other) =>
418 identical(this, other) ||
419 other is ToolCallInfo &&
420 runtimeType == other.runtimeType &&
421 id == other.id &&
422 functionName == other.functionName &&
423 arguments == other.arguments;
424 }