tress

Connect an existing app

View .md

The integration adds the native Tress terminal as another client. Keep your assistant-ui interface, existing agent, tools, and login. There is no Tress React component, browser client package, or stylesheet to install.

This source preview needs a Statewire host using Harness protocol 2026-09-13. The browser and terminal must connect to that same thread host. An ordinary AI SDK /api/chat route does not, by itself, own a shared conversation.

If you already have an authorized server-owned Harness endpoint and an app-issued terminal credential, add discovery and connect the terminal. No new Tress server or browser dependency is required. Otherwise, the runnable example demonstrates the host and credential boundary.

1. Reuse the thread host

If your app already has a server-owned Harness thread, keep it and its existing session/access checks. Its WebSocket upgrade handler is the integration point. createTressAccess is optional. Tress does not require another auth provider or a wrapper around an already authorized host.

// Your upgrade handler for GET /api/threads/:id/ws. It returns a refusal
// Response or the host's accept; Pinboard's `listenFetch({ fetch, upgrade })`
// (or `appFetch` inside a Hono route) completes the handshake. Another
// WebSocket server accepts `statewire.v1` and calls `accepted.attach(socket)`.
const upgrade = async (request: Request) => {
  // Application code: check Origin for cookie sessions, then this thread's
  // permissions. Credentials sent in the first frame reach the host's
  // `authorize({ headers })` (see the access guide).
  await app.requireThreadAccess(request, threadId);
  return existingThreadHost.upgrade(request);
};

Keep serving existingThreadHost.fetch at /stream and /frames while older Tress CLI releases may still connect over HTTP. Current clients attach over the WebSocket only.

existingThreadHost, app, and threadId come from your app. Resolve the thread and its owner before selecting the host. Do not create a fresh host on every HTTP request. A public thread ID alone does not authorize a connection. The terminal needs a credential your host understands; the browser's cookie is not automatically transferred to the CLI.

The access guide covers the demo's built-in session sharing, Harness's native authorize hook, and the optional access adapter.

2. If the browser currently owns the transport

Move that transport to a server-owned host first. The upstream source packages provide this composition:

import { statewireHost } from "statewire-node";
import { AISDKTransport } from "@harness-sdk/ai-sdk";
import { HARNESS_HOST_PROTOCOL } from "harness-sdk/host";
import { DirectChatTransport } from "ai";

const existingThreadHost = statewireHost({
  protocol: HARNESS_HOST_PROTOCOL,
  key: [threadId],
  transport: (restored) => AISDKTransport({
    threadId,
    transport: new DirectChatTransport({ agent: existingAgent }),
    ...(restored ? { initialState: restored as AISDKTransport.State } : {}),
  }),
  storage: {
    load: () => app.threadSnapshots.load(threadId),
    save: (snapshot) => app.threadSnapshots.save(threadId, snapshot),
  },
  onError: (error) => app.reportStorageError(error),
});

This is a composition example: supply your existing AI SDK agent, snapshot store, and error reporting. These Harness adapters are currently upstream source packages; see versions. There is no required CHAT_BACKEND_URL. A transport can call the existing agent in-process. If your app already uses HTTP between services, keep its own configured transport.

3. Only if needed: point assistant-ui at the shared thread

Skip this step if your browser already uses that server-owned Harness thread. Keep its existing components and provider. If you moved a browser-owned transport to the server in step 2, update only the runtime/provider wiring:

import { AuiConfig, AuiProvider } from "@assistant-ui/react";
import { HarnessThread } from "@assistant-ui/react-harness-sdk";
import { StatewireWebsocket } from "statewire";

const config = AuiConfig({
  thread: HarnessThread({
    transport: StatewireWebsocket({ url: "/api/threads/main" }),
  }),
});

export function Assistant() {
  return <AuiProvider config={config}><YourExistingChat /></AuiProvider>;
}

For dynamic conversations, scope the provider and endpoint to the selected thread. The browser dials /api/threads/main/ws and sends its same-origin session cookie on the upgrade.

4. Make the app discoverable and connect

Publish GET /.well-known/tress.json as application/json:

{
  "version": 1,
  "threads": "/api/thread-list",
  "thread": "/api/threads/{id}"
}

Point threads at the app's authorized Harness thread-list transport (harness-sdk-threads, version 2026-09-13). Return all conversations this credential permits, including archived ones. Reuse your existing index and storage. Point thread at the corresponding server-owned Harness hosts. Both endpoints serve Statewire's WebSocket at /ws on the same app origin. Filter the list by the authenticated user and check each thread independently. The source integration guide specifies the manifest and ID constraints; the example server demonstrates it.

The user connects once, then chooses from the app's accessible threads:

# MY_APP_API_KEY contains the user's app-issued API key / bearer token.
tress attach https://chat.example.com --api-key-env MY_APP_API_KEY

# If the application explicitly permits anonymous access:
tress attach https://chat.example.com

No header file is required for either flow. For custom header authentication, use --headers-file app-headers.json with a private JSON object of headers; see the access guide. The app chooses its auth policy. Managed Harness keys remain on the application's backend, which can proxy both the list and threads using the same authorization.

Attachment automatically lists every accessible thread, including those never visited in this terminal. Choose a number to load existing history. Add --ui for the terminal interface. /threads or Ctrl-T refreshes the list; plain mode also supports /thread ID. The CLI refreshes on connection and picker opening, not continuously in the background.

Direct thread URLs still work when a specific conversation is known:

chmod 600 app-headers.json
tress attach https://chat.example.com/api/threads/main \
  --headers-file app-headers.json

The Tress demo uses its separate secret-session command. It grants access to that one session, not the visitor's entire account history. Discovery does not change the existing demo's access policy.

Verify messages from each client, reconnect, and your app's expiry policy before following the production walkthrough. These commands are a source preview; build this checkout. Published v0.3.0 does not include app discovery. Generic CLI browser login, automatic token refresh, and thread creation remain future work.