跳到正文

HOST

.NET

net8.0 sveda-dotnet-sdk。ASP.NET 为 MapPost 加布局。

.NET

net8.0 sveda-dotnet-sdk。ASP.NET 为 MapPost 加布局。

sveda-dotnet-sdk StartHostSessionAsync,在 ASP.NET Core 中 MapPost /sveda/session,JS 或 iframe 聊天。

AGENT PROMPT

Paste into Cursor

Copy this into Cursor, Claude Code, or any coding agent. It installs Sveda against this stack using public packages.

Install Sveda into this .NET project.

Read:
- https://sveda.dev/docs/hosts/dotnet
- https://sveda.dev/docs/client
- https://sveda.dev/docs/deploy
- https://sveda.dev/llms.txt

Follow the numbered steps on the .NET page. If unsure which frontend this repo uses, ask the user.

1. Run sveda-server

# ghcr.io/neresson/sveda-server:latest
# curl -fsSL https://raw.githubusercontent.com/neresson/sveda/main/deploy/compose.yaml -o compose.yaml && curl -fsSL https://raw.githubusercontent.com/neresson/sveda/main/deploy/compose.env -o .env && # Edit .env: a model key, SVEDA_EMBED_HOST_API_KEY, SVEDA_CORS_ORIGINS &&  && docker compose up -d && curl -s http://127.0.0.1:8787/sveda/ready

services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: sveda
      POSTGRES_PASSWORD: sveda
      POSTGRES_DB: sveda
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U sveda -d sveda"]
      interval: 5s
      timeout: 3s
      retries: 10
    volumes:
      - sveda-postgres:/var/lib/postgresql/data
  redis:
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10
  sveda-server:
    image: ghcr.io/neresson/sveda-server:latest
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "8787:8787"
    environment:
      SVEDA_BIND: 0.0.0.0:8787
      SVEDA_EMBED_ENABLED: "true"
      SVEDA_DATABASE_URL: postgres://sveda:sveda@postgres:5432/sveda
      SVEDA_REDIS_URL: redis://redis:6379
      SVEDA_APP_KEY: ${SVEDA_APP_KEY:-dev-app-key-change-me}
      SVEDA_ADMIN_API_KEY: ${SVEDA_ADMIN_API_KEY:-dev-admin-key-change-me}
      SVEDA_EMBED_HOST_API_KEY: ${SVEDA_EMBED_HOST_API_KEY:-dev-host-key-change-me}
      SVEDA_CORS_ORIGINS: ${SVEDA_CORS_ORIGINS:-*}
      DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-}
volumes:
  sveda-postgres:

2. Install sveda-dotnet-sdk

Target net8.0. NuGet: sveda-dotnet-sdk (C# types stay in Sveda.Client). Usable from a console app or worker — ASP.NET is optional.

dotnet add package sveda-dotnet-sdk

3. Env

SVEDA_CLIENT_BASE_URL=http://127.0.0.1:8787
SVEDA_CLIENT_HOST_API_KEY=replace-me

4. Mint

Pass optional policy (and tighten-only grants) on CreateTokenRequest when mapping roles — embed policies.

using var client = new SvedaClient(new SvedaClientOptions
{
    BaseUrl = baseUrl,
    HostApiKey = hostApiKey,
});
var session = await client.StartHostSessionAsync(new CreateTokenRequest
{
    VisitorId = "aspnet-playground",
});

5. ASP.NET Core

JSON property names stay snake_case for expires_in so sveda-host.js can read them. Hide layout buttons when origin is empty.

app.MapPost("/sveda/session", async (IConfiguration config, CancellationToken cancellationToken) =>
{
    var baseUrl = config["SVEDA_CLIENT_BASE_URL"]?.TrimEnd('/') ?? "";
    var hostApiKey = config["SVEDA_CLIENT_HOST_API_KEY"] ?? "";
    if (baseUrl.Length == 0 || hostApiKey.Length == 0)
    {
        return Results.Json(new { message = "Set SVEDA_CLIENT_BASE_URL and SVEDA_CLIENT_HOST_API_KEY" },
            statusCode: StatusCodes.Status503ServiceUnavailable);
    }
    using var client = new SvedaClient(new SvedaClientOptions { BaseUrl = baseUrl, HostApiKey = hostApiKey });
    var session = await client.StartHostSessionAsync(new CreateTokenRequest { VisitorId = "aspnet-playground" }, cancellationToken);
    return Results.Json(new { origin = session.Origin, token = session.Token, expires_in = session.ExpiresIn, appearance = session.Appearance });
});

6. Page markup

<html
  lang="en"
  data-sveda-origin="http://127.0.0.1:8787"
  data-sveda-session="/sveda/session"
>
  <body>
    <button type="button" data-sveda-open="js">Open JS chat</button>
    <button type="button" data-sveda-open="iframe">Open iframe chat</button>
    <script type="module" src="/sveda-host.js"></script>
  </body>
</html>

Mint from the host.

Install the language SDK. Expose POST /sveda/session. Open JS chat or an iframe.

Host SDKs