-
Notifications
You must be signed in to change notification settings - Fork 78
/
lib.rs
152 lines (128 loc) · 3.64 KB
/
lib.rs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
#[doc(hidden)]
pub mod bindings {
wit_bindgen::generate!({
world: "aici",
path: "../../wit",
additional_derives: [serde::Serialize, serde::Deserialize],
pub_export_macro: true
});
pub use self::{aici::abi::*, exports::aici::abi::*};
}
pub use bindings::{controller::*, runtime, runtime_storage, tokenizer};
pub mod svob;
pub use svob::SimpleVob;
#[macro_export]
macro_rules! export {
($ty:ident) => {
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
$crate::bindings::export!($ty with_types_in $crate::bindings);
};
}
pub mod bytes;
mod host;
pub mod recognizer;
pub mod rng;
pub mod toktree;
#[cfg(feature = "cfg")]
pub mod cfg;
#[cfg(feature = "cfg")]
mod lex;
#[cfg(feature = "rx")]
pub mod rx;
pub mod substring;
pub use host::{aici_stop, StorageCmd, StorageOp, StorageResp};
// Workaround for WIT not supporting empty record types, define some here, and then we will pass ()
// to the guest.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct InitPromptResult();
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct PreProcessArg();
impl PreProcessResult {
pub fn new(num_forks: u32) -> Self {
PreProcessResult {
num_forks,
suspend: false,
ff_tokens: vec![],
}
}
pub fn continue_() -> Self {
PreProcessResult::new(1)
}
pub fn suspend() -> Self {
PreProcessResult {
num_forks: 1,
suspend: true,
ff_tokens: vec![],
}
}
pub fn stop() -> Self {
PreProcessResult::new(0)
}
pub fn ff_tokens(toks: Vec<TokenId>) -> Self {
PreProcessResult {
num_forks: 1,
suspend: false,
ff_tokens: toks,
}
}
}
impl PostProcessResult {
pub fn stop() -> Self {
PostProcessResult { stop: true }
}
pub fn continue_() -> Self {
PostProcessResult { stop: false }
}
pub fn from_arg(arg: &PostProcessArg) -> Self {
let stop = arg.tokens.contains(&tokenizer::eos_token());
PostProcessResult { stop }
}
}
pub trait AiciCtrl {
fn init_prompt(&mut self, _arg: InitPromptArg) -> InitPromptResult {
InitPromptResult()
}
fn pre_process(&mut self, PreProcessArg(): PreProcessArg) -> PreProcessResult {
PreProcessResult::continue_()
}
fn mid_process(&mut self, arg: MidProcessArg) -> MidProcessResult;
fn post_process(&mut self, arg: PostProcessArg) -> PostProcessResult {
PostProcessResult::from_arg(&arg)
}
}
pub trait Program: AiciCtrl {
fn new(_: String) -> Self;
}
pub struct ExportedProgram<C: AiciCtrl> {
mutable_controller: Arc<Mutex<C>>,
}
impl<C: AiciCtrl> ExportedProgram<C> {
pub fn new(controller: C) -> Self {
ExportedProgram {
mutable_controller: Arc::new(Mutex::new(controller)),
}
}
}
impl<C: Program + 'static> GuestRunner for ExportedProgram<C> {
fn new(arg: String) -> Self {
ExportedProgram::new(C::new(arg))
}
fn init_prompt(&self, arg: InitPromptArg) {
self.mutable_controller.lock().unwrap().init_prompt(arg);
}
fn pre_process(&self) -> PreProcessResult {
self.mutable_controller
.lock()
.unwrap()
.pre_process(PreProcessArg())
}
fn mid_process(&self, arg: MidProcessArg) -> MidProcessResult {
self.mutable_controller.lock().unwrap().mid_process(arg)
}
fn post_process(&self, arg: PostProcessArg) -> PostProcessResult {
self.mutable_controller.lock().unwrap().post_process(arg)
}
}