-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_example.rs
More file actions
136 lines (116 loc) · 3.65 KB
/
tool_example.rs
File metadata and controls
136 lines (116 loc) · 3.65 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
//! Example: Creating custom tools
//!
//! Demonstrates:
//! - Tool trait implementation
//! - Capability requirements
//! - Resource bounds
//! - Response normalization
use oracle_omen_core::{
capability::Capability,
hash::Hash,
tool::{Determinism, ResourceBounds, SideEffect, ToolError, ToolId, ToolResult},
};
use oracle_omen_runtime::tools::{DynTool, ToolMetadata};
use serde::{Deserialize, Serialize};
/// Custom calculation tool
#[derive(Clone)]
struct CalculatorTool;
impl DynTool for CalculatorTool {
fn id(&self) -> &ToolId {
static ID: ToolId = ToolId::new("calculator", "1.0.0");
&ID
}
fn capabilities(&self) -> Vec<String> {
vec!["compute:arithmetics".to_string()]
}
fn side_effects(&self) -> SideEffect {
SideEffect::Pure
}
fn resource_bounds(&self) -> &ResourceBounds {
static BOUNDS: ResourceBounds = ResourceBounds::with_timeout(1000);
&BOUNDS
}
fn execute(&self, input: &[u8], _metadata: &ToolMetadata) -> ToolResult<Vec<u8>> {
// Parse input
let request: CalcRequest = serde_json::from_slice(input)
.map_err(|e| ToolError::InvalidInput {
tool: "calculator".to_string(),
reason: e.to_string(),
})?;
// Perform calculation
let result = match request.op {
Op::Add => request.a + request.b,
Op::Sub => request.a - request.b,
Op::Mul => request.a * request.b,
Op::Div => {
if request.b == 0 {
return Err(ToolError::ExecutionFailed {
tool: "calculator".to_string(),
reason: "Division by zero".to_string(),
});
}
request.a / request.b
}
};
// Serialize response
let response = CalcResponse { result };
serde_json::to_vec(&response).map_err(|e| ToolError::SerializationFailed {
tool: "calculator".to_string(),
reason: e.to_string(),
})
}
fn input_schema(&self) -> &str {
r#"{"type": "object", "properties": {"a": {"type": "integer"}, "b": {"type": "integer"}, "op": {"type": "string"}}}"#
}
fn output_schema(&self) -> &str {
r#"{"type": "object", "properties": {"result": {"type": "integer"}}}"#
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct CalcRequest {
a: i64,
b: i64,
op: Op,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
enum Op {
Add,
Sub,
Mul,
Div,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct CalcResponse {
result: i64,
}
fn main() {
println!("Oracle Omen - Custom Tool Example");
println!("=================================\n");
let tool = CalculatorTool;
let metadata = ToolMetadata {
logical_time: 0,
run_id: 1,
seed: None,
};
println!("Tool ID: {}", tool.id());
println!("Capabilities: {:?}", tool.capabilities());
println!("Side effects: {:?}", tool.side_effects());
println!("Determinism: {:?}", tool.determinism());
println!("Resource bounds: {:?}", tool.resource_bounds());
println!();
// Example: 15 + 27 = 42
let request = CalcRequest {
a: 15,
b: 27,
op: Op::Add,
};
let input = serde_json::to_vec(&request).unwrap();
println!("Input: {}", String::from_utf8_lossy(&input));
match tool.execute(&input, &metadata) {
Ok(output) => {
let response: CalcResponse = serde_json::from_slice(&output).unwrap();
println!("Result: {}", response.result);
}
Err(e) => println!("Error: {}", e),
}
}