-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
92 lines (72 loc) · 2.69 KB
/
Program.cs
File metadata and controls
92 lines (72 loc) · 2.69 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
// Tool registration example — register host functions callable from guest code.
//
// Mirrors: src/wasm_sandbox/examples/python_basics.rs (tool dispatch section)
using System.Text.Json.Serialization;
using HyperlightSandbox.Api;
using HyperlightSandbox.Guest.Python;
Console.WriteLine("=== Hyperlight Sandbox .NET — Tool Registration Example ===\n");
using var sandbox = new SandboxBuilder()
.WithPythonModule()
.Build();
// --- Register typed tools ---
sandbox.RegisterTool<MathArgs, double>("add", args => args.A + args.B);
sandbox.RegisterTool<MathArgs, double>("multiply", args => args.A * args.B);
sandbox.RegisterTool<GreetArgs, string>("greet", args => $"Hello, {args.Name}!");
// --- Register a raw JSON tool ---
sandbox.RegisterTool("lookup", (string json) =>
{
// Simple key-value lookup.
if (json.Contains("api_key"))
return """{"result": "sk-demo-12345"}""";
if (json.Contains("model"))
return """{"result": "gpt-4"}""";
return """{"result": "not found"}""";
});
// --- Register an async typed tool (e.g. simulating an external API call) ---
sandbox.RegisterToolAsync<MathArgs, double>("add_async", async args =>
{
await Task.Delay(10).ConfigureAwait(false); // Simulate I/O latency
return args.A + args.B;
});
// --- Test 1: Typed tool dispatch ---
Console.WriteLine("═══ Test 1: Typed tool dispatch ═══");
var result = sandbox.Run("""
sum_result = call_tool("add", a=10, b=32)
print(f"10 + 32 = {sum_result}")
product = call_tool("multiply", a=7, b=6)
print(f"7 × 6 = {product}")
greeting = call_tool("greet", name="Hyperlight")
print(f"Greeting: {greeting}")
""");
Console.WriteLine($"stdout:\n{result.Stdout}");
// --- Test 2: Raw JSON tool dispatch ---
Console.WriteLine("═══ Test 2: Raw JSON tool dispatch ═══");
result = sandbox.Run("""
key = call_tool("lookup", key="api_key")
print(f"API key: {key}")
model = call_tool("lookup", key="model")
print(f"Model: {model}")
""");
Console.WriteLine($"stdout:\n{result.Stdout}");
// --- Test 3: Async tool dispatch ---
Console.WriteLine("═══ Test 3: Async tool dispatch ═══");
result = sandbox.Run("""
async_sum = call_tool("add_async", a=100, b=200)
print(f"Async 100 + 200 = {async_sum}")
""");
Console.WriteLine($"stdout:\n{result.Stdout}");
Console.WriteLine("✅ Tool registration example finished successfully!");
return 0;
// --- DTOs ---
internal sealed class MathArgs
{
[JsonPropertyName("a")]
public double A { get; set; }
[JsonPropertyName("b")]
public double B { get; set; }
}
internal sealed class GreetArgs
{
[JsonPropertyName("name")]
public string Name { get; set; } = "world";
}