|
| 1 | +# Moss .NET SDK |
| 2 | + |
| 3 | +The .NET SDK for [Moss](https://github.com/usemoss/moss) — fast on-device |
| 4 | +retrieval. It wraps the native `libmoss` runtime through P/Invoke and exposes an |
| 5 | +idiomatic, async C# API for index management, hybrid search, and metadata |
| 6 | +filtering. |
| 7 | + |
| 8 | +## Architecture |
| 9 | + |
| 10 | +``` |
| 11 | + ┌──────────────────────────────────┐ |
| 12 | + │ Your application code │ |
| 13 | + └──────────────┬───────────────────┘ |
| 14 | + │ |
| 15 | + ┌──────────────▼───────────────────┐ |
| 16 | + │ Moss (managed C#) │ ← src/Moss |
| 17 | + │ MossClient — async API for │ |
| 18 | + │ indexing, querying, management │ |
| 19 | + └──────────────┬───────────────────┘ |
| 20 | + │ P/Invoke ([DllImport("moss")]) |
| 21 | + ┌──────────────▼───────────────────┐ |
| 22 | + │ libmoss (native C ABI) │ ← prebuilt runtime |
| 23 | + │ hybrid search, data models │ |
| 24 | + └──────────────────────────────────┘ |
| 25 | +``` |
| 26 | + |
| 27 | +- `src/Moss/` — the public SDK. `MossClient` plus the data models. |
| 28 | +- `src/Moss/Interop/` — the P/Invoke layer: raw `libmoss` declarations, C-ABI |
| 29 | + struct mirrors, UTF-8 marshaling, and native-memory conversion/cleanup. |
| 30 | + |
| 31 | +The interop layer targets the same stable C ABI (`libmoss.h`) that the Go |
| 32 | +bindings bind via cgo. |
| 33 | + |
| 34 | +## Quick start |
| 35 | + |
| 36 | +```csharp |
| 37 | +using Moss; |
| 38 | + |
| 39 | +using var client = new MossClient("your_project_id", "your_project_key"); |
| 40 | + |
| 41 | +await client.CreateIndexAsync("support-docs", new[] |
| 42 | +{ |
| 43 | + new DocumentInfo("1", "Refunds are processed within 3-5 business days."), |
| 44 | + new DocumentInfo("2", "You can track your order on the dashboard."), |
| 45 | +}); |
| 46 | + |
| 47 | +await client.LoadIndexAsync("support-docs"); |
| 48 | + |
| 49 | +var results = await client.QueryAsync( |
| 50 | + "support-docs", "how long do refunds take?", new QueryOptions { TopK = 3 }); |
| 51 | + |
| 52 | +foreach (var doc in results.Docs) |
| 53 | + Console.WriteLine($"[{doc.Score:F3}] {doc.Text}"); |
| 54 | +``` |
| 55 | + |
| 56 | +### Metadata filtering |
| 57 | + |
| 58 | +Attach string metadata at index time and pass a JSON filter at query time: |
| 59 | + |
| 60 | +```csharp |
| 61 | +await client.AddDocsAsync("support-docs", new[] |
| 62 | +{ |
| 63 | + new DocumentInfo("3", "EU refund policy…", |
| 64 | + metadata: new Dictionary<string, string> { ["region"] = "eu" }), |
| 65 | +}); |
| 66 | + |
| 67 | +var results = await client.QueryAsync("support-docs", "refund policy", |
| 68 | + new QueryOptions |
| 69 | + { |
| 70 | + TopK = 5, |
| 71 | + FilterJson = "{\"region\": \"eu\"}", |
| 72 | + }); |
| 73 | +``` |
| 74 | + |
| 75 | +## API surface |
| 76 | + |
| 77 | +| Area | Methods | |
| 78 | +|------|---------| |
| 79 | +| Indexes | `CreateIndexAsync`, `GetIndexAsync`, `ListIndexesAsync`, `DeleteIndexAsync` | |
| 80 | +| Documents | `AddDocsAsync`, `DeleteDocsAsync`, `GetDocsAsync` | |
| 81 | +| Jobs | `GetJobStatusAsync` | |
| 82 | +| Local runtime | `LoadIndexAsync`, `UnloadIndexAsync`, `RefreshIndexAsync`, `QueryAsync` | |
| 83 | + |
| 84 | +All methods are asynchronous and accept a `CancellationToken`. Failures from the |
| 85 | +native runtime surface as `MossException` (carrying the status `Code` and the |
| 86 | +`moss_last_error` message). |
| 87 | + |
| 88 | +## The native runtime |
| 89 | + |
| 90 | +The SDK calls into `libmoss`, distributed as a prebuilt native library |
| 91 | +(`libmoss.so` on Linux, `libmoss.dylib` on macOS, `moss.dll` on Windows). It |
| 92 | +must be discoverable at runtime — on the standard library search path, next to |
| 93 | +your application, or via `NativeLibrary` resolution. Building and unit-testing |
| 94 | +the SDK does **not** require the native library; only running queries does. |
| 95 | + |
| 96 | +## Building and testing |
| 97 | + |
| 98 | +Requires the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0). |
| 99 | + |
| 100 | +```bash |
| 101 | +cd sdks/dotnet |
| 102 | +dotnet build |
| 103 | +dotnet test # unit tests run without libmoss |
| 104 | +``` |
| 105 | + |
| 106 | +The unit tests cover the managed logic and the marshaling layer (UTF-8 |
| 107 | +round-trips, native buffer packing, ABI struct sizes) and do not load the native |
| 108 | +library. |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +[BSD 2-Clause License](../../LICENSE) |
0 commit comments