Skip to content

ANcpLua/SWEN3.Paperless.RabbitMq

Repository files navigation

codecov .NET 10 NuGet License

SWEN3.Paperless.RabbitMq

RabbitMQ messaging library for .NET with SSE support and AI-powered document summarization.

Configuration

<EnablePreviewFeatures>true</EnablePreviewFeatures>
{
  "RabbitMQ": {
    "Uri": "amqp://guest:guest@localhost:5672"
  }
}

Usage

Basic Setup

// Add RabbitMQ
builder.Services.AddPaperlessRabbitMq(builder.Configuration);

// With SSE support
services.AddPaperlessRabbitMq(config, includeOcrResultStream: true, includeGenAiResultStream: true);

Publishing

var command = new OcrCommand(docId, fileName, storagePath);
await publisher.PublishOcrCommandAsync(command);

var result = new OcrEvent(jobId, "Completed", text, DateTimeOffset.UtcNow);
await publisher.PublishOcrEventAsync(result);

Consuming

await using var consumer = await factory.CreateConsumerAsync<OcrCommand>();

await foreach (var command in consumer.ConsumeAsync(cancellationToken))
{
    try
    {
        // Process message
        await consumer.AckAsync();
    }
    catch
    {
        await consumer.NackAsync(requeue: true);
    }
}

SSE Endpoint

// Map endpoint
app.MapOcrEventStream();

// Client-side
const eventSource = new EventSource('/api/v1/ocr-results');
eventSource.addEventListener('ocr-completed', (event) => {
    const data = JSON.parse(event.data);
    console.log(data);
});

GenAI Support (v1.0.4+)

// Enable GenAI features
builder.Services.AddPaperlessRabbitMq(configuration,
    includeOcrResultStream: true,
    includeGenAiResultStream: true);

// Configure Gemini
builder.Services.Configure<GeminiOptions>(configuration.GetSection("Gemini"));
builder.Services.AddHttpClient<ITextSummarizer, GeminiService>();
builder.Services.AddHostedService<GenAIWorker>();

// Publish GenAI command
var genAiCommand = new GenAICommand(request.JobId, result.Text!);
await _publisher.PublishGenAICommandAsync(genAiCommand);
{
  "Gemini": {
    "ApiKey": "your-api-key",
    "Model": "gemini-2.0-flash"
  }
}

Message Types

public record OcrCommand(Guid JobId, string FileName, string FilePath);
public record OcrEvent(Guid JobId, string Status, string? Text, DateTimeOffset ProcessedAt);
public record GenAICommand(Guid DocumentId, string Text);
public record GenAIEvent(Guid DocumentId, string? Summary, DateTimeOffset ProcessedAt, string? ErrorMessage = null);

Installation

dotnet add package SWEN3.Paperless.RabbitMq

License

This project is licensed under the MIT License.