-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (113 loc) · 4.41 KB
/
Program.cs
File metadata and controls
136 lines (113 loc) · 4.41 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
// Aura FFI example for C# (.NET 8+).
//
// Demonstrates calling the Aura shared library from C# via P/Invoke.
//
// Build the shared library first:
// cargo build --release --no-default-features --features "encryption,ffi"
//
// Then run:
// dotnet run
//
// Make sure aura.dll (Windows) / libaura.so (Linux) / libaura.dylib (macOS)
// is in the output directory or system library path.
using System;
using System.Runtime.InteropServices;
// ── P/Invoke declarations ──
static class AuraFFI
{
const string LibName = "aura";
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr aura_open(
[MarshalAs(UnmanagedType.LPUTF8Str)] string path,
out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int aura_close(IntPtr handle, out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void aura_free(IntPtr handle);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void aura_free_string(IntPtr s);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr aura_store(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string content,
byte level,
[MarshalAs(UnmanagedType.LPUTF8Str)] string? tagsJson,
[MarshalAs(UnmanagedType.LPUTF8Str)] string? ns,
out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr aura_recall(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string query,
int tokenBudget,
out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr aura_recall_structured(
IntPtr handle,
[MarshalAs(UnmanagedType.LPUTF8Str)] string query,
int topK,
out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int aura_run_maintenance(IntPtr handle, out IntPtr outError);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern long aura_count(IntPtr handle);
// Helper: read and free a Rust string
public static string? ReadAndFree(IntPtr ptr)
{
if (ptr == IntPtr.Zero) return null;
string result = Marshal.PtrToStringUTF8(ptr)!;
aura_free_string(ptr);
return result;
}
public static string? GetError(IntPtr errPtr)
{
return ReadAndFree(errPtr);
}
}
// ── Main ──
class Program
{
static void Main()
{
Console.WriteLine("=== Aura C# FFI Example ===");
// Open brain
var handle = AuraFFI.aura_open("./csharp_brain", out var err);
if (handle == IntPtr.Zero)
{
Console.WriteLine($"Error: {AuraFFI.GetError(err)}");
return;
}
try
{
// Store memories
var id1 = AuraFFI.ReadAndFree(
AuraFFI.aura_store(handle, "User prefers C# and .NET",
4, // Identity
"[\"preference\",\"dotnet\"]", null, out err));
Console.WriteLine($"Stored: {id1}");
var id2 = AuraFFI.ReadAndFree(
AuraFFI.aura_store(handle, "Always write unit tests first",
2, // Decisions
"[\"workflow\",\"testing\"]", null, out err));
Console.WriteLine($"Stored: {id2}");
// Count
var count = AuraFFI.aura_count(handle);
Console.WriteLine($"Total records: {count}");
// Recall (text)
var context = AuraFFI.ReadAndFree(
AuraFFI.aura_recall(handle, "user preferences", 1024, out err));
Console.WriteLine($"\nRecall (text):\n{context}");
// Recall structured (JSON)
var json = AuraFFI.ReadAndFree(
AuraFFI.aura_recall_structured(handle, "testing workflow", 5, out err));
Console.WriteLine($"\nRecall structured (JSON):\n{json}");
// Maintenance
AuraFFI.aura_run_maintenance(handle, out err);
Console.WriteLine("\nMaintenance cycle complete.");
}
finally
{
AuraFFI.aura_close(handle, out err);
AuraFFI.aura_free(handle);
}
}
}