Skip to content

Commit d93b6fc

Browse files
committed
creating a signature for every type in the IR
1 parent 3cc0de1 commit d93b6fc

File tree

20 files changed

+660
-68
lines changed

20 files changed

+660
-68
lines changed

engine/Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
"language_client_python",
1111
"language_client_ruby/ext/ruby_ffi",
1212
"language_client_typescript",
13+
"boundary-collector",
1314
"sandbox",
1415
]
1516
default-members = [
@@ -26,6 +27,7 @@ default-members = [
2627
"language_client_python",
2728
"language_client_ruby/ext/ruby_ffi",
2829
"language_client_typescript",
30+
"boundary-collector",
2931
]
3032

3133
[workspace.dependencies]
@@ -55,6 +57,8 @@ internal-baml-core = { path = "baml-lib/baml-core" }
5557
internal-baml-jinja = { path = "baml-lib/jinja-runtime" }
5658
internal-baml-schema-ast = { path = "baml-lib/schema-ast" }
5759
internal-llm-client = { path = "baml-lib/llm-client" }
60+
baml-runtime = { path = "baml-runtime" }
61+
5862
log = "0.4.20"
5963
# TODO: disable imports, etc
6064
minijinja = { version = "1.0.16", default-features = false, features = [

engine/baml-lib/baml-core/src/ir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod jinja_helpers;
33
mod json_schema;
44
pub mod repr;
55
mod walker;
6+
pub mod signature;
67

78
pub use ir_helpers::{
89
scope_diagnostics, ArgCoercer, ClassFieldWalker, ClassWalker, ClientWalker, EnumValueWalker,

engine/baml-lib/baml-core/src/ir/repr.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use internal_llm_client::{ClientProvider, ClientSpec, UnresolvedClientProperty};
2323
use serde::Serialize;
2424

2525
use crate::Configuration;
26-
26+
use crate::ir::signature::{BamlHash, ProvideBamlHash};
2727
/// This class represents the intermediate representation of the BAML AST.
2828
/// It is a representation of the BAML AST that is easier to work with than the
2929
/// raw BAML AST, and should include all information necessary to generate
@@ -327,6 +327,23 @@ impl IntermediateRepr {
327327
recursive_aliases,
328328
))
329329
}
330+
331+
pub fn create_baml_hash(&self) -> String {
332+
// hash everything we can based on the IR
333+
let enum_names = self
334+
.enums
335+
.iter()
336+
.map(|e| {
337+
let hash = e.to_baml_hash();
338+
339+
(
340+
e.elem.name.as_str(),
341+
hash,
342+
)
343+
})
344+
.collect::<IndexMap<_, _>>();
345+
return "TODO".to_string();
346+
}
330347
}
331348

332349
// TODO:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use baml_types::{Resolvable, StringOr};
2+
3+
mod r#enum;
4+
mod class;
5+
mod template_string;
6+
mod test_case;
7+
mod client;
8+
mod function;
9+
mod type_alias;
10+
#[cfg(test)]
11+
mod test;
12+
13+
/// Used to identify unique signatures of types.
14+
15+
pub(super) trait Signature {
16+
fn type_name(&self) -> &'static str;
17+
fn interface(&self) -> Option<String>;
18+
fn impl_(&self) -> Option<String> {
19+
None
20+
}
21+
}
22+
23+
pub struct BamlHash {
24+
type_name: &'static str,
25+
interface_hash: Option<u64>,
26+
impl_hash: Option<u64>,
27+
}
28+
29+
pub trait ProvideBamlHash {
30+
fn to_baml_hash(&self) -> BamlHash;
31+
}
32+
33+
fn hash_string(type_name: &str, s: &str) -> u64 {
34+
let mut hasher = std::collections::hash_map::DefaultHasher::new();
35+
std::hash::Hash::hash(&(type_name, s), &mut hasher);
36+
std::hash::Hasher::finish(&hasher)
37+
}
38+
39+
impl<T: Signature> ProvideBamlHash for T {
40+
fn to_baml_hash(&self) -> BamlHash {
41+
BamlHash {
42+
type_name: self.type_name(),
43+
interface_hash: self.interface().map(|s| hash_string(self.type_name(), &s)),
44+
impl_hash: self.impl_().map(|s| hash_string(self.type_name(), &s)),
45+
}
46+
}
47+
}
48+
49+
impl<Meta> Signature for Resolvable<StringOr, Meta> {
50+
fn type_name(&self) -> &'static str {
51+
match self {
52+
Resolvable::String(s, _) => s.type_name(),
53+
Resolvable::Numeric(n, _) => "numeric",
54+
Resolvable::Bool(b, _) => "bool",
55+
Resolvable::Array(_, _) => "array",
56+
Resolvable::Map(_, _) => "map",
57+
Resolvable::Null(_) => "null",
58+
}
59+
}
60+
61+
fn interface(&self) -> Option<String> {
62+
match self {
63+
Resolvable::String(value, _) => value.interface(),
64+
Resolvable::Numeric(value, _) => Some(value.to_string()),
65+
Resolvable::Bool(value, _) => Some(value.to_string()),
66+
Resolvable::Array(resolvables, _) => Some(resolvables
67+
.iter()
68+
.filter_map(|r| r.interface())
69+
.collect::<Vec<_>>()
70+
.join("_BAML_ARRAY_")),
71+
Resolvable::Map(index_map, _) => Some(index_map
72+
.iter()
73+
.filter_map(|(k, (_, v))| v.interface().map(|s| format!("{}: {}", k, s)))
74+
.collect::<Vec<_>>()
75+
.join("_BAML_MAP_")),
76+
Resolvable::Null(_) => Some("null".to_string()),
77+
}
78+
}
79+
}
80+
81+
impl Signature for StringOr {
82+
fn type_name(&self) -> &'static str {
83+
match self {
84+
StringOr::EnvVar(_) => "env_var",
85+
StringOr::Value(_) => "value",
86+
StringOr::JinjaExpression(j) => j.type_name(),
87+
}
88+
}
89+
90+
fn interface(&self) -> Option<String> {
91+
match self {
92+
StringOr::EnvVar(name) => Some(name.clone()),
93+
StringOr::Value(value) => Some(value.clone()),
94+
StringOr::JinjaExpression(expression) => expression.interface(),
95+
}
96+
}
97+
}
98+
99+
impl Signature for baml_types::JinjaExpression {
100+
fn type_name(&self) -> &'static str {
101+
"jinja_expression"
102+
}
103+
104+
fn interface(&self) -> Option<String> {
105+
Some(self.to_string())
106+
}
107+
}

0 commit comments

Comments
 (0)