Skip to content

ylvict/NOpenCode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

104 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NOpenCode

NuGet CI Examples Integration README

Empower your .NET applications with OpenCode's AI engine. Express your intent in natural language β€” NOpenCode bridges your application logic with AI.

✨ Features

  • Zero setup β€” auto-discovers or starts opencode serve for you
  • Plain-English API β€” reads like natural language
  • Multi-turn sessions β€” ask follow-up questions in context
  • Streaming β€” receive responses chunk by chunk
  • File & code search β€” ripgrep, filename lookup, symbol search
  • Session management β€” fork, share, diff, revert, summarize
  • Provider & model discovery β€” list available models, providers, agents
  • MCP server management β€” add and list MCP servers
  • Event monitoring β€” subscribe to real-time SSE events
  • DI integration β€” AddNOpenCode() for ASP.NET Core / console hosts
  • .NET Standard 2.0 β€” compatible with .NET Framework 4.6.1+ and all modern .NET

πŸ“‹ Prerequisites

  • OpenCode CLI installed (npm install -g opencode-ai)
  • .NET SDK 8+ (for consumption) or .NET 10 (for development)

πŸ“¦ Installation

dotnet add package NOpenCode

πŸš€ Quick Start

using NOpenCode;

string answer = await OpenCode.Ask("Explain how async/await works in C#");
Console.WriteLine(answer);

πŸ’‘ Examples

🎯 One-shot

string review = await OpenCode.Ask("What is the capital of France?");

// Or use the builder for full control
await using var ai = await OpenCode
    .Configure()
    .Launch();

var reply = await ai
    .Ask("What is the population of Paris?")
    .Execute();

Console.WriteLine(reply);

πŸ’¬ Multi-turn

await using var ai = await OpenCode
    .Configure()
    .Launch();

var session = await ai.NewSession("API design").Create();

var r1 = await session.Ask("What are the key differences between REST and GraphQL?");
var r2 = await session.Ask("When would you choose one over the other?");

πŸ”„ Streaming

await session.AskStream(
    "Write a brief README for a .NET library called NOpenCode.",
    onChunk: chunk => Console.Write(chunk),
    onComplete: reply => Console.WriteLine($"\nDone, tokens: {reply.GetUsage()?.Total}"),
    onError: ex => Console.WriteLine($"Error: {ex.Message}")
);

πŸ“‹ Session lifecycle

var session = await ai.NewSession("Code review").Create();
await session.Ask("Review this code for potential bugs.");

// Fork into a new branch
var fork = await session.Fork();
await fork.Ask("Focus on security issues only.");

// Get the diff
var diff = await fork.GetDiff();
foreach (var d in diff)
    Console.WriteLine($"[{d.Type}] {d.Path}");

// Share the session
await fork.Share();

// Clean up
await fork.Delete();

πŸ” File & code search

var todos = await ai.Files.Search("TODO|FIXME");
foreach (var match in todos)
    Console.WriteLine($"{match.Path}:{match.LineNumber}  {match.Lines?.Trim()}");

var files = await ai.Files.Find("*.cs");
var content = await ai.Files.Read("Program.cs");

πŸ—ΊοΈ Discovery

var models = await ai.Models.List(Providers.OpenCode);
var providers = await ai.Providers.List();
var agents = await ai.Agents.List();
var health = await ai.Diagnostics.GetHealth();

πŸ”Œ MCP server management

await ai.Mcp.Add("filesystem", new
{
    type = "local",
    command = new[] { "npx", "-y", "@modelcontextprotocol/server-filesystem", "./" }
});

var servers = await ai.Mcp.List();

🧩 DI integration

builder.Services.AddNOpenCode();

public class ReviewService(OpenCodeClient AI)
{
    public async Task RunAsync()
    {
        var reply = await AI
            .Ask("What is the capital of France?")
            .Execute();
    }
}

πŸ“‘ Real-time events

var subscribed = ai.Events.Subscribe(
    onEvent: evt => Console.WriteLine($"[{evt.Type}] {evt.Data}"),
    onError: ex => Console.WriteLine($"Error: {ex.Message}")
);

var answer = await ai.Ask("What is 2+2?").Execute();
Console.WriteLine($"Answer: {answer}");

await Task.Delay(2000);
// subscribed completes when cancelled

πŸ“ Project Structure

src/NOpenCode/              β†’ .NET Standard 2.0 library
  OpenCode.cs               β†’ Static entry point
  NOpenCodeBuilder.cs       β†’ Fluent builder
  OpenCodeClient.cs         β†’ Main client with 11 sub-clients
  OpenCodeSession.cs        β†’ Session with full lifecycle
  AskOperation.cs           β†’ One-shot query builder
  SessionBuilder.cs         β†’ Session creation builder
  ServerManager.cs          β†’ Auto-manages opencode serve
  Clients/                  β†’ Domain-specific clients
  Models/                   β†’ 14 DTOs
  Http/                     β†’ HTTP client + SSE reader
  DependencyInjection/      β†’ IServiceCollection extensions
  Exceptions/               β†’ Custom exception hierarchy

examples/                   β†’ 10 standalone example programs (net10.0)
  HelloWorld               β†’ Simplest possible usage
  OneShot                  β†’ Single query with builder
  MultiTurn                β†’ Multi-turn conversation
  Streaming                β†’ Streaming responses
  DI                       β†’ DI integration with AddNOpenCode
  SessionLifecycle         β†’ Fork, share, diff, delete
  FileSearch               β†’ ripgrep, find, list, read
  Discovery                β†’ List models, providers, agents, commands
  McpManagement            β†’ Add and list MCP servers
  EventMonitor             β†’ Real-time SSE events

tests/NOpenCode.Tests/      β†’ xUnit tests

πŸ“„ License

MIT

About

A .NET SDK that brings OpenCode's AI engine into your applications. Express your intent in natural language.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages