harmony-protocol 0.1.0

Reverse-engineered OpenAI Harmony response format library for structured conversation handling
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;
use serde_wasm_bindgen::{from_value, to_value};
use js_sys::{Array, Object, Promise};

use crate::{
    chat::{
        Author, ChannelConfig, Content, Conversation, DeveloperContent, Message,
        ReasoningEffort, Role, SystemContent, TextContent, ToolDescription, ToolNamespaceConfig
    },
    encoding::{HarmonyEncoding, RenderConversationConfig, StreamableParser},
    load_harmony_encoding, HarmonyEncodingName,
    tiktoken::Rank,
};

// Convert Rust error to JS Error
fn rust_error_to_js(err: anyhow::Error) -> JsValue {
    JsValue::from_str(&err.to_string())
}

// WASM wrapper for Role
#[wasm_bindgen]
pub struct WasmRole {
    inner: Role,
}

#[wasm_bindgen]
impl WasmRole {
    #[wasm_bindgen(constructor)]
    pub fn new(role: &str) -> Result<WasmRole, JsValue> {
        let inner = Role::try_from(role)
            .map_err(|e| JsValue::from_str(e))?;
        Ok(WasmRole { inner })
    }

    #[wasm_bindgen(js_name = "user")]
    pub fn user() -> WasmRole {
        WasmRole { inner: Role::User }
    }

    #[wasm_bindgen(js_name = "assistant")]
    pub fn assistant() -> WasmRole {
        WasmRole { inner: Role::Assistant }
    }

    #[wasm_bindgen(js_name = "system")]
    pub fn system() -> WasmRole {
        WasmRole { inner: Role::System }
    }

    #[wasm_bindgen(js_name = "developer")]
    pub fn developer() -> WasmRole {
        WasmRole { inner: Role::Developer }
    }

    #[wasm_bindgen(js_name = "tool")]
    pub fn tool() -> WasmRole {
        WasmRole { inner: Role::Tool }
    }

    #[wasm_bindgen(js_name = "toString")]
    pub fn to_string(&self) -> String {
        self.inner.as_str().to_string()
    }
}

// WASM wrapper for ReasoningEffort
#[wasm_bindgen]
pub struct WasmReasoningEffort {
    inner: ReasoningEffort,
}

#[wasm_bindgen]
impl WasmReasoningEffort {
    #[wasm_bindgen(js_name = "low")]
    pub fn low() -> WasmReasoningEffort {
        WasmReasoningEffort { inner: ReasoningEffort::Low }
    }

    #[wasm_bindgen(js_name = "medium")]
    pub fn medium() -> WasmReasoningEffort {
        WasmReasoningEffort { inner: ReasoningEffort::Medium }
    }

    #[wasm_bindgen(js_name = "high")]
    pub fn high() -> WasmReasoningEffort {
        WasmReasoningEffort { inner: ReasoningEffort::High }
    }

    #[wasm_bindgen(js_name = "toString")]
    pub fn to_string(&self) -> String {
        match self.inner {
            ReasoningEffort::Low => "low".to_string(),
            ReasoningEffort::Medium => "medium".to_string(),
            ReasoningEffort::High => "high".to_string(),
        }
    }
}

// WASM wrapper for ToolDescription
#[wasm_bindgen]
pub struct WasmToolDescription {
    inner: ToolDescription,
}

#[wasm_bindgen]
impl WasmToolDescription {
    #[wasm_bindgen(constructor)]
    pub fn new(name: String, description: String, parameters: Option<JsValue>) -> Result<WasmToolDescription, JsValue> {
        let parameters = if let Some(params) = parameters {
            Some(from_value(params).map_err(|e| JsValue::from_str(&e.to_string()))?)
        } else {
            None
        };

        Ok(WasmToolDescription {
            inner: ToolDescription::new(name, description, parameters),
        })
    }

    #[wasm_bindgen(getter)]
    pub fn name(&self) -> String {
        self.inner.name.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn description(&self) -> String {
        self.inner.description.clone()
    }
}

// WASM wrapper for SystemContent
#[wasm_bindgen]
pub struct WasmSystemContent {
    inner: SystemContent,
}

#[wasm_bindgen]
impl WasmSystemContent {
    #[wasm_bindgen(constructor)]
    pub fn new() -> WasmSystemContent {
        WasmSystemContent {
            inner: SystemContent::new(),
        }
    }

    #[wasm_bindgen(js_name = "withModelIdentity")]
    pub fn with_model_identity(mut self, model_identity: String) -> WasmSystemContent {
        self.inner = self.inner.with_model_identity(model_identity);
        self
    }

    #[wasm_bindgen(js_name = "withReasoningEffort")]
    pub fn with_reasoning_effort(mut self, effort: WasmReasoningEffort) -> WasmSystemContent {
        self.inner = self.inner.with_reasoning_effort(effort.inner);
        self
    }

    #[wasm_bindgen(js_name = "withRequiredChannels")]
    pub fn with_required_channels(mut self, channels: JsValue) -> Result<WasmSystemContent, JsValue> {
        let channels: Vec<String> = from_value(channels)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;
        self.inner = self.inner.with_required_channels(channels);
        Ok(self)
    }

    #[wasm_bindgen(js_name = "withBrowserTool")]
    pub fn with_browser_tool(mut self) -> WasmSystemContent {
        self.inner = self.inner.with_browser_tool();
        self
    }

    #[wasm_bindgen(js_name = "withPythonTool")]
    pub fn with_python_tool(mut self) -> WasmSystemContent {
        self.inner = self.inner.with_python_tool();
        self
    }

    #[wasm_bindgen(js_name = "withConversationStartDate")]
    pub fn with_conversation_start_date(mut self, date: String) -> WasmSystemContent {
        self.inner = self.inner.with_conversation_start_date(date);
        self
    }

    #[wasm_bindgen(js_name = "withKnowledgeCutoff")]
    pub fn with_knowledge_cutoff(mut self, cutoff: String) -> WasmSystemContent {
        self.inner = self.inner.with_knowledge_cutoff(cutoff);
        self
    }
}

// WASM wrapper for DeveloperContent
#[wasm_bindgen]
pub struct WasmDeveloperContent {
    inner: DeveloperContent,
}

#[wasm_bindgen]
impl WasmDeveloperContent {
    #[wasm_bindgen(constructor)]
    pub fn new() -> WasmDeveloperContent {
        WasmDeveloperContent {
            inner: DeveloperContent::new(),
        }
    }

    #[wasm_bindgen(js_name = "withInstructions")]
    pub fn with_instructions(mut self, instructions: String) -> WasmDeveloperContent {
        self.inner = self.inner.with_instructions(instructions);
        self
    }

    #[wasm_bindgen(js_name = "withFunctionTools")]
    pub fn with_function_tools(mut self, tools: JsValue) -> Result<WasmDeveloperContent, JsValue> {
        let tool_descriptions: Vec<WasmToolDescription> = from_value(tools)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;
        let tools: Vec<ToolDescription> = tool_descriptions.into_iter().map(|t| t.inner).collect();
        self.inner = self.inner.with_function_tools(tools);
        Ok(self)
    }
}

// WASM wrapper for Message
#[wasm_bindgen]
pub struct WasmMessage {
    inner: Message,
}

#[wasm_bindgen]
impl WasmMessage {
    #[wasm_bindgen(js_name = "fromRoleAndContent")]
    pub fn from_role_and_content(role: WasmRole, content: JsValue) -> Result<WasmMessage, JsValue> {
        let content = if content.is_instance_of::<WasmSystemContent>() {
            let sys_content: WasmSystemContent = from_value(content)
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
            Content::SystemContent(sys_content.inner)
        } else if content.is_instance_of::<WasmDeveloperContent>() {
            let dev_content: WasmDeveloperContent = from_value(content)
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
            Content::DeveloperContent(dev_content.inner)
        } else {
            let text: String = from_value(content)
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
            Content::Text(TextContent { text })
        };

        Ok(WasmMessage {
            inner: Message::from_role_and_content(role.inner, content),
        })
    }

    #[wasm_bindgen(js_name = "withChannel")]
    pub fn with_channel(mut self, channel: String) -> WasmMessage {
        self.inner = self.inner.with_channel(channel);
        self
    }

    #[wasm_bindgen(js_name = "withRecipient")]
    pub fn with_recipient(mut self, recipient: String) -> WasmMessage {
        self.inner = self.inner.with_recipient(recipient);
        self
    }

    #[wasm_bindgen(js_name = "withContentType")]
    pub fn with_content_type(mut self, content_type: String) -> WasmMessage {
        self.inner = self.inner.with_content_type(content_type);
        self
    }

    #[wasm_bindgen(getter)]
    pub fn channel(&self) -> Option<String> {
        self.inner.channel.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn recipient(&self) -> Option<String> {
        self.inner.recipient.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn role(&self) -> String {
        self.inner.author.role.as_str().to_string()
    }
}

// WASM wrapper for Conversation
#[wasm_bindgen]
pub struct WasmConversation {
    inner: Conversation,
}

#[wasm_bindgen]
impl WasmConversation {
    #[wasm_bindgen(js_name = "fromMessages")]
    pub fn from_messages(messages: JsValue) -> Result<WasmConversation, JsValue> {
        let wasm_messages: Vec<WasmMessage> = from_value(messages)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;
        let messages: Vec<Message> = wasm_messages.into_iter().map(|m| m.inner).collect();
        Ok(WasmConversation {
            inner: Conversation::from_messages(messages),
        })
    }

    #[wasm_bindgen(getter)]
    pub fn length(&self) -> usize {
        self.inner.messages.len()
    }
}

// WASM wrapper for HarmonyEncoding
#[wasm_bindgen]
pub struct WasmHarmonyEncoding {
    inner: HarmonyEncoding,
}

#[wasm_bindgen]
impl WasmHarmonyEncoding {
    #[wasm_bindgen(getter)]
    pub fn name(&self) -> String {
        self.inner.name().to_string()
    }

    #[wasm_bindgen(js_name = "renderConversationForCompletion")]
    pub fn render_conversation_for_completion(
        &self,
        conversation: WasmConversation,
        next_turn_role: WasmRole
    ) -> Result<Array, JsValue> {
        let tokens = self.inner
            .render_conversation_for_completion(&conversation.inner, next_turn_role.inner, None)
            .map_err(rust_error_to_js)?;

        let js_array = Array::new();
        for token in tokens {
            js_array.push(&JsValue::from(token));
        }
        Ok(js_array)
    }

    #[wasm_bindgen(js_name = "renderConversation")]
    pub fn render_conversation(&self, conversation: WasmConversation) -> Result<Array, JsValue> {
        let tokens = self.inner
            .render_conversation(&conversation.inner, None)
            .map_err(rust_error_to_js)?;

        let js_array = Array::new();
        for token in tokens {
            js_array.push(&JsValue::from(token));
        }
        Ok(js_array)
    }

    #[wasm_bindgen(js_name = "parseMessagesFromCompletionTokens")]
    pub fn parse_messages_from_completion_tokens(
        &self,
        tokens: JsValue,
        role: Option<WasmRole>
    ) -> Result<Array, JsValue> {
        let tokens: Vec<u32> = from_value(tokens)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;
        let role = role.map(|r| r.inner);

        let messages = self.inner
            .parse_messages_from_completion_tokens(tokens, role)
            .map_err(rust_error_to_js)?;

        let js_array = Array::new();
        for message in messages {
            let wasm_message = WasmMessage { inner: message };
            js_array.push(&to_value(&wasm_message).map_err(|e| JsValue::from_str(&e.to_string()))?);
        }
        Ok(js_array)
    }
}

// WASM wrapper for HarmonyEncodingName
#[wasm_bindgen]
pub struct WasmHarmonyEncodingName {
    inner: HarmonyEncodingName,
}

#[wasm_bindgen]
impl WasmHarmonyEncodingName {
    #[wasm_bindgen(js_name = "harmonyGptOss")]
    pub fn harmony_gpt_oss() -> WasmHarmonyEncodingName {
        WasmHarmonyEncodingName {
            inner: HarmonyEncodingName::HarmonyGptOss,
        }
    }

    #[wasm_bindgen(js_name = "toString")]
    pub fn to_string(&self) -> String {
        self.inner.to_string()
    }
}

// WASM wrapper for StreamableParser
#[wasm_bindgen]
pub struct WasmStreamableParser {
    inner: StreamableParser,
}

#[wasm_bindgen]
impl WasmStreamableParser {
    #[wasm_bindgen(constructor)]
    pub fn new(encoding: WasmHarmonyEncoding, role: Option<WasmRole>) -> Result<WasmStreamableParser, JsValue> {
        let role = role.map(|r| r.inner);
        let inner = StreamableParser::new(encoding.inner, role)
            .map_err(rust_error_to_js)?;
        Ok(WasmStreamableParser { inner })
    }

    pub fn process(&mut self, token: u32) -> Result<(), JsValue> {
        self.inner.process(token).map_err(rust_error_to_js)
    }

    #[wasm_bindgen(js_name = "processEos")]
    pub fn process_eos(&mut self) -> Result<(), JsValue> {
        self.inner.process_eos().map_err(rust_error_to_js)
    }

    #[wasm_bindgen(js_name = "currentContent")]
    pub fn current_content(&self) -> Result<String, JsValue> {
        self.inner.current_content().map_err(rust_error_to_js)
    }

    #[wasm_bindgen(js_name = "currentRole")]
    pub fn current_role(&self) -> Option<WasmRole> {
        self.inner.current_role().map(|r| WasmRole { inner: r })
    }

    #[wasm_bindgen(js_name = "lastContentDelta")]
    pub fn last_content_delta(&self) -> Result<Option<String>, JsValue> {
        self.inner.last_content_delta().map_err(rust_error_to_js)
    }

    #[wasm_bindgen(js_name = "intoMessages")]
    pub fn into_messages(self) -> Result<Array, JsValue> {
        let messages = self.inner.into_messages();
        let js_array = Array::new();
        for message in messages {
            let wasm_message = WasmMessage { inner: message };
            js_array.push(&to_value(&wasm_message).map_err(|e| JsValue::from_str(&e.to_string()))?);
        }
        Ok(js_array)
    }

    #[wasm_bindgen(js_name = "currentChannel")]
    pub fn current_channel(&self) -> Option<String> {
        self.inner.current_channel()
    }

    #[wasm_bindgen(js_name = "currentRecipient")]
    pub fn current_recipient(&self) -> Option<String> {
        self.inner.current_recipient()
    }
}

// Main loading function (async for WASM)
#[wasm_bindgen(js_name = "loadHarmonyEncoding")]
pub fn load_harmony_encoding_wasm(name: WasmHarmonyEncodingName) -> Promise {
    let inner_name = name.inner;
    future_to_promise(async move {
        let inner = crate::load_harmony_encoding(inner_name)
            .await
            .map_err(rust_error_to_js)?;
        let wasm_encoding = WasmHarmonyEncoding { inner };
        to_value(&wasm_encoding).map_err(|e| JsValue::from_str(&e.to_string()))
    })
}

// Set panic hook for better error messages in WASM
#[wasm_bindgen(start)]
pub fn main() {
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}