forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_schema.rs
More file actions
211 lines (200 loc) · 7.91 KB
/
Copy pathtool_schema.rs
File metadata and controls
211 lines (200 loc) · 7.91 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
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
//! `ToolSchema`: JSON-schema description of an `@tool`-annotated function.
//!
//! Lives in core (unconditional, no `ai-runtime` gate) because it is
//! embedded in core, always-compiled types: `bytecode::ActorMeta.tools`
//! and `hir::Decl`'s agent `tools` field. The `@tool` annotation is
//! permanent language surface (RFC 0010 §C.2: "Keep as-is; document
//! extensibility") independent of whether any LLM client is compiled in.
//!
//! This is a distinct type from `nulang_ai::request::ToolSchema` (used for
//! the actual LLM-provider wire format in `LlmRequest.tools`), which stays
//! in the optional `nulang-ai` crate per its "zero dependency on core"
//! charter. `runtime/agent.rs` converts between the two, behind
//! `ai-runtime`, at the one point a tool schema is handed to a provider.
use serde::{Deserialize, Serialize};
use serde_json::Map;
use crate::types::{PrimitiveType, Type};
/// JSON-schema description of a tool exposed to an LLM (or any future
/// caller of `@tool`-annotated functions).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolSchema {
/// Tool name.
pub name: String,
/// Human-readable description.
pub description: String,
/// JSON schema for the tool arguments.
pub parameters: serde_json::Value,
}
/// Convert a Nulang type into a JSON Schema value.
///
/// Supported shapes:
/// - Primitives: Int, Float, Bool, String, Unit
/// - Records: `{ type: "object", properties: {...} }`
/// - Arrays / Lists: `{ type: "array", items: ... }`
/// - Tuples: `{ type: "array", prefixItems: [...] }`
/// - Variants: `{ oneOf: [...] }`
/// - References: unwrap to the inner type
pub fn type_to_json_schema(ty: &Type) -> serde_json::Value {
match ty {
Type::Var(_) => serde_json::json!({}),
Type::Primitive(PrimitiveType::Int) => serde_json::json!({"type": "integer"}),
Type::Primitive(PrimitiveType::Float) => serde_json::json!({"type": "number"}),
Type::Primitive(PrimitiveType::Bool) => serde_json::json!({"type": "boolean"}),
Type::Primitive(PrimitiveType::String) => serde_json::json!({"type": "string"}),
Type::Primitive(PrimitiveType::Unit) => serde_json::json!({"type": "null"}),
Type::Primitive(PrimitiveType::Nil) => serde_json::json!({"type": "null"}),
Type::Primitive(PrimitiveType::Never) | Type::Primitive(PrimitiveType::Address) => {
serde_json::json!({})
}
Type::Record(fields) => {
let mut properties = Map::new();
let mut required = Vec::new();
for (name, field_ty) in fields {
properties.insert(name.clone(), type_to_json_schema(field_ty));
required.push(name.clone());
}
serde_json::json!({
"type": "object",
"properties": properties,
"required": required,
})
}
Type::Array(inner) => serde_json::json!({
"type": "array",
"items": type_to_json_schema(inner),
}),
Type::Tuple(elems) => serde_json::json!({
"type": "array",
"prefixItems": elems.iter().map(type_to_json_schema).collect::<Vec<_>>(),
}),
Type::Variant(variants) => {
let one_of: Vec<serde_json::Value> = variants
.iter()
.map(|(name, inner)| {
if let Some(inner_ty) = inner {
serde_json::json!({
"type": "object",
"properties": {
name: type_to_json_schema(inner_ty),
},
"required": [name],
})
} else {
serde_json::json!({
"type": "string",
"enum": [name],
})
}
})
.collect();
serde_json::json!({ "oneOf": one_of })
}
Type::Reference { inner, .. } => type_to_json_schema(inner),
Type::App { constructor, args } => {
// Common constructors: List[T], Array[T], Option[T]
if let Type::Var(_) | Type::Primitive(_) | Type::App { .. } = constructor.as_ref() {
// Cannot determine a concrete schema; fall through to unknown.
return serde_json::json!({});
}
let constructor_name = type_name(constructor);
match constructor_name.as_deref() {
Some("List") | Some("Array") if !args.is_empty() => serde_json::json!({
"type": "array",
"items": type_to_json_schema(&args[0]),
}),
Some("Option") if !args.is_empty() => serde_json::json!({
"anyOf": [
{"type": "null"},
type_to_json_schema(&args[0]),
],
}),
_ => serde_json::json!({}),
}
}
Type::Function { .. } => serde_json::json!({}),
Type::Actor { .. } => serde_json::json!({}),
Type::Scheme { body, .. } => type_to_json_schema(body),
Type::Nominal { underlying, .. } => type_to_json_schema(underlying),
Type::Skolem(_) => serde_json::json!({}),
}
}
/// Best-effort name extraction for type-application constructors.
fn type_name(ty: &Type) -> Option<String> {
match ty {
Type::Primitive(p) => Some(format!("{:?}", p)),
Type::Var(_) => None,
Type::App { constructor, .. } => type_name(constructor),
Type::Reference { inner, .. } => type_name(inner),
_ => None,
}
}
/// Build a `ToolSchema` from a Nulang function signature.
///
/// `params` must contain the explicit parameter types; `ret` is validated to be
/// present but is not included in the tool schema (providers only need argument
/// schemas).
pub fn function_to_tool_schema(
name: &str,
description: &str,
params: &[(String, Type)],
_ret: &Type,
) -> ToolSchema {
let mut properties = Map::new();
let mut required = Vec::new();
for (param_name, param_ty) in params {
properties.insert(param_name.clone(), type_to_json_schema(param_ty));
required.push(param_name.clone());
}
ToolSchema {
name: name.to_string(),
description: description.to_string(),
parameters: serde_json::json!({
"type": "object",
"properties": properties,
"required": required,
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_primitive_schemas() {
assert_eq!(
type_to_json_schema(&Type::Primitive(PrimitiveType::Int)),
serde_json::json!({"type": "integer"})
);
assert_eq!(
type_to_json_schema(&Type::Primitive(PrimitiveType::String)),
serde_json::json!({"type": "string"})
);
}
#[test]
fn test_function_to_tool_schema_basic() {
let params = vec![
("city".to_string(), Type::Primitive(PrimitiveType::String)),
("days".to_string(), Type::Primitive(PrimitiveType::Int)),
];
let schema = function_to_tool_schema(
"get_weather",
"Get the weather forecast",
¶ms,
&Type::Primitive(PrimitiveType::String),
);
assert_eq!(schema.name, "get_weather");
assert_eq!(schema.description, "Get the weather forecast");
assert_eq!(schema.parameters["type"], "object");
assert_eq!(
schema.parameters["required"],
serde_json::json!(["city", "days"])
);
}
#[test]
fn test_array_and_option_schemas() {
let arr = Type::Array(Box::new(Type::Primitive(PrimitiveType::Int)));
assert_eq!(
type_to_json_schema(&arr),
serde_json::json!({"type": "array", "items": {"type": "integer"}})
);
}
}