本文へスキップ

DOCS

JS クライアント

SvedaClient は attach 面。Vue、React、Svelte、Solid と web component は任意。

JS クライアント

SvedaClient は attach 面。Vue、React、Svelte、Solid と web component は任意。

SvedaClient オプション、endpoint、header、session.send、frontend tool レジストリ、context。パッケージ @sveda-ai/core。

Package

npm i @sveda-ai/core. Main class SvedaClient. Session class SvedaChatSession. Product overview: JS client.

SvedaClient options

endpoints.stream is required. It is the POST target for a turn. Optional endpoints match the runtime: message, histories, history(chatId), documentsExtract.

  • protocolMode defaults to 'sveda'. These docs cover that mode.
  • headers is a Record<string, string> or a getter () => Record<string, string> resolved on each request.
  • tools is a SvedaToolRegistry. If omitted, the client constructs one.
  • context is a SvedaContextRegistry. Snapshots merge into the stream body context field.
  • autoSubmitFrontendToolResults defaults to true. After a tool.call with target: "frontend", the session runs the handler and posts results on a follow-up stream turn.
import { SvedaClient, SvedaToolRegistry, SvedaContextRegistry } from '@sveda-ai/core'

const tools = new SvedaToolRegistry()
const context = new SvedaContextRegistry()

const client = new SvedaClient({
  endpoints: {
    stream: 'http://127.0.0.1:8787/sveda/stream',
    message: 'http://127.0.0.1:8787/sveda/message',
    histories: 'http://127.0.0.1:8787/sveda/chat-histories',
    history: (chatId) => `http://127.0.0.1:8787/sveda/chat-histories/${chatId}`,
    documentsExtract: 'http://127.0.0.1:8787/sveda/documents/extract',
  },
  protocolMode: 'sveda',
  headers: {
    'x-sveda-embed-token': token,
  },
  tools,
  context,
  autoSubmitFrontendToolResults: true,
})

Headers

Typical embed header: x-sveda-embed-token. The runtime also accepts Authorization: Bearer sveda_embed_…. A getter is useful when the token rotates.

const client = new SvedaClient({
  endpoints: { stream: '/sveda/stream' },
  headers: () => ({
    'x-sveda-embed-token': currentToken(),
  }),
})

session.send and stop

client.session(chatId) (or createSession()) returns SvedaChatSession. send(text, options?) POSTs JSON to endpoints.stream with messages, prompt, chatId, context, clientTools, optional toolDecisions when resolving a confirming tool call, and optional model, provider, options. stop() aborts the in-flight request.

const session = client.session('chat_1')
await session.send('Summarize the last deploy.')
session.stop()

Tool confirmation

When the stream emits tool.call with confirmation: "required", call resolveToolConfirmation after the user chooses. Backend tools send toolDecisions without repeating arguments; frontend tools run the handler only on approve. While a call is pending, do not send a new user prompt — the server returns 409. Protocol and UI details: tool confirmation.

await session.resolveToolConfirmation('call_del_1', 'approve')
// deny → host MCP is not called; frontend handler is skipped

Accept

In protocolMode: 'sveda' the client sets this Accept header. The server still responds with SSE (text/event-stream). See stream protocol.

Accept: application/vnd.sveda.stream+json

Optional UI packages

@sveda-ai/vue, @sveda-ai/react, @sveda-ai/svelte, @sveda-ai/solid, and @sveda-ai/element wrap the same core client. Mount snippets: chat UI. They are not required to stream a turn.

npm i @sveda-ai/vue
npm i @sveda-ai/react
npm i @sveda-ai/svelte
npm i @sveda-ai/solid
npm i @sveda-ai/element

The runtime owns the loop.

sveda-server is Axum. Agent loop, catalog, tools, and MCP sit beside the host.

Runtime