Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
Token sign-in page: one command, other setups folded away
Pixel (kwant bot)a0420c251df3
diff --git a/frontend/src/components/AuthGate.tsx b/frontend/src/components/AuthGate.tsxindex ed034e3b..ef871f87 100644--- a/frontend/src/components/AuthGate.tsx+++ b/frontend/src/components/AuthGate.tsx@@ -1,5 +1,5 @@import React, { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';-import { Check, Copy, KeyRound, Loader2, RefreshCw, Terminal, X } from 'lucide-react';+import { Check, ChevronRight, Copy, KeyRound, Loader2, RefreshCw, Terminal, X } from 'lucide-react';import { ModernAvatar } from './ModernAvatar';import {PERSON_PROMPT_CLOSED_EVENT,@@ -184,20 +184,25 @@ export const PersonTokenDialog: React.FC<{ onClose: () => void; onSignedIn: () =);};-/** Where kwant runs, as the person sees it: decides which steps get the token. */-export type TokenPlace = 'server' | 'docker' | 'here';--/** The port kwant listens on, as far as this page can tell (behind a proxy it can't). */-function kwantPort(loc: Pick<Location, 'port'>): string {- return loc.port || '8799';+/** Where a locked page is: Docker publishes kwant on 127.0.0.1 and still asks for the token there. */+export function isLoopback(hostname: string): boolean {+ const h = hostname.replace(/^\[|\]$/g, '').toLowerCase();+ return h === 'localhost' || h === '::1' || h.startsWith('127.');}-/** Docker publishes kwant on 127.0.0.1 and still asks for the token there, so a locked loopback page is Docker. */-export function guessPlace(hostname: string): TokenPlace {- const h = hostname.replace(/^\[|\]$/g, '').toLowerCase();- return h === 'localhost' || h === '::1' || h.startsWith('127.') ? 'docker' : 'server';+/**+ * The SSH tunnel that skips the token, or null when it wouldn't work: only for kwant itself on plain http at a+ * network address with its port in the URL. Behind an HTTPS proxy, or with no port, the page can't know kwant's port.+ */+export function tunnelCommand(loc: Pick<Location, 'protocol' | 'hostname' | 'port'>): { command: string; url: string } | null {+ if (loc.protocol !== 'http:' || !loc.port || !loc.hostname || isLoopback(loc.hostname)) return null;+ const host = loc.hostname.includes(':') ? `[${loc.hostname}]` : loc.hostname;+ return { command: `ssh -N -L ${loc.port}:127.0.0.1:${loc.port} user@${host}`, url: `http://127.0.0.1:${loc.port}` };}+const DOCKER_COMMAND = 'docker compose exec kwnt kwnt token --raw';+const FILE_COMMAND = 'cat ~/.kwnt/api-token';+/** The token from what was pasted: the token itself, or the whole sign-in link. */export function tokenFromPaste(value: string): string {const v = value.trim();@@ -206,14 +211,8 @@ export function tokenFromPaste(value: string): string {return v;}-const PLACES: { value: TokenPlace; label: string }[] = [- { value: 'server', label: 'On a server' },- { value: 'docker', label: 'In Docker' },- { value: 'here', label: 'On this computer' },-];-/** One command, selectable in a tap, with a copy button where the browser allows it. */-const Command: React.FC<{ children: string; note?: string }> = ({ children, note }) => {+const Command: React.FC<{ children: string }> = ({ children }) => {const [copied, setCopied] = useState(false);const copy = async () => {try {@@ -237,76 +236,53 @@ const Command: React.FC<{ children: string; note?: string }> = ({ children, note}};return (- <div className="space-y-0.5">- <div className="flex items-start gap-1.5 bg-zinc-950 border border-zinc-800 rounded-md pl-2 pr-1 py-1">- <Terminal size={12} className="shrink-0 mt-[7px] text-zinc-500" aria-hidden="true" />- <code className="flex-1 min-w-0 py-1 break-words select-all text-[11px] font-mono text-zinc-200">{children}</code>- <button- type="button"- onClick={copy}- aria-label={copied ? 'Copied' : `Copy: ${children}`}- className="shrink-0 p-1.5 rounded text-zinc-500 hover:text-zinc-200 hover:bg-zinc-800 transition-colors"- >- {copied ? <Check size={12} className="text-emerald-400" /> : <Copy size={12} />}- </button>- </div>- {note && <p className="text-[11px] text-zinc-500 leading-snug">{note}</p>}+ <div className="flex items-start gap-1.5 bg-zinc-950 border border-zinc-800 rounded-md pl-2 pr-1 py-1">+ <Terminal size={12} className="shrink-0 mt-[7px] text-zinc-500" aria-hidden="true" />+ <code className="flex-1 min-w-0 py-1 break-words select-all text-[11px] font-mono text-zinc-200">{children}</code>+ <button+ type="button"+ onClick={copy}+ aria-label={copied ? 'Copied' : `Copy: ${children}`}+ className="shrink-0 p-1.5 rounded text-zinc-500 hover:text-zinc-200 hover:bg-zinc-800 transition-colors"+ >+ {copied ? <Check size={12} className="text-emerald-400" /> : <Copy size={12} />}+ </button></div>);};-/** How to get the token, for where kwant runs. Exported for tests. */-export const TokenSteps: React.FC<{ place: TokenPlace; hostname: string; port: string }> = ({ place, hostname, port }) => {- if (place === 'here') {- const url = `http://127.0.0.1:${port}`;- return (- <div className="space-y-2 text-xs text-zinc-400 leading-relaxed">- <p>- Open kwant bots at{' '}- <a href={url} className="font-mono text-sky-400 hover:text-sky-300 underline underline-offset-2">- {url}- </a>{' '}- instead. On the computer it runs on, kwant bots doesn't ask for a token, but only at <span className="font-mono">127.0.0.1</span> or{' '}- <span className="font-mono">localhost</span>, not at this computer's network address.- </p>- <p>- Started with Docker? Docker still asks for the token: choose <span className="text-zinc-200">In Docker</span>.- </p>- </div>- );- }- if (place === 'docker') {- return (- <div className="space-y-2 text-xs text-zinc-400 leading-relaxed">- <p>On the machine that runs Docker (over SSH if it's a server), in the folder with <span className="font-mono">docker-compose.yml</span>:</p>- <Command>docker compose exec kwnt kwnt token --raw</Command>- <Command note="Started with docker run instead? Use your container's name or ID (docker ps lists it).">- {'docker exec <container> kwnt token --raw'}- </Command>- </div>- );- }- const host = hostname.includes(':') ? `[${hostname}]` : hostname;+/** The less common ways to get the token, one line each. Exported for tests. */+export const OtherSetups: React.FC<{ loc: Pick<Location, 'protocol' | 'hostname' | 'port'>; docker: boolean }> = ({ loc, docker }) => {+ const tunnel = tunnelCommand(loc);return (- <div className="space-y-2 text-xs text-zinc-400 leading-relaxed">- <p>SSH into the server as the user that runs kwant bots, then run:</p>- <Command note="Running it as a systemd service with KWNT_DATA_DIR? Read that folder instead, e.g. sudo cat /var/lib/kwnt/api-token.">- cat ~/.kwnt/api-token- </Command>- <p>- Or <span className="font-mono">kwnt token --raw</span>. Built from source and not on your PATH? Run it from the source folder:- </p>- <Command>./target/release/kwnt token --raw</Command>- <p className="pt-1">- <span className="text-zinc-200">No token at all:</span> on this computer, open an SSH tunnel, then go to{' '}- <span className="font-mono">http://127.0.0.1:{port}</span>. kwant bots sees the tunnel as local. Not for Docker.- </p>- <Command>{`ssh -N -L ${port}:127.0.0.1:${port} user@${host}`}</Command>- </div>+ <details className="group text-xs text-zinc-400">+ <summary className="flex items-center gap-1 w-fit min-h-[32px] cursor-pointer select-none list-none [&::-webkit-details-marker]:hidden text-zinc-400 hover:text-zinc-200 rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-500">+ <ChevronRight size={13} className="transition-transform group-open:rotate-90 motion-reduce:transition-none" aria-hidden="true" />+ Other setups+ </summary>+ <div className="mt-2 space-y-2.5 leading-relaxed">+ {docker ? (+ <Setup label="Without Docker, as the user that runs kwant bots">{FILE_COMMAND}</Setup>+ ) : (+ <Setup label="Docker, in the folder with docker-compose.yml">{DOCKER_COMMAND}</Setup>+ )}+ <Setup label="systemd service">sudo cat /var/lib/kwnt/api-token</Setup>+ <Setup label="Built from source and not on your PATH">./target/release/kwnt token --raw</Setup>+ {tunnel && (+ <Setup label={`No token: open an SSH tunnel on this computer, then go to ${tunnel.url} (not for Docker)`}>{tunnel.command}</Setup>+ )}+ </div>+ </details>);};-/** Shown when the server rejects a pasted token. Exported for tests. */+const Setup: React.FC<{ label: string; children: string }> = ({ label, children }) => (+ <div className="space-y-1">+ <p className="text-[11px] text-zinc-500 leading-snug">{label}</p>+ <Command>{children}</Command>+ </div>+);+/** After a rejected token, focus and select the box so the next paste replaces it. Exported for tests. */export const selectForRetry = (el: Pick<HTMLInputElement, 'focus' | 'select'> | null) => {if (!el) return;@@ -315,6 +291,8 @@ export const selectForRetry = (el: Pick<HTMLInputElement, 'focus' | 'select'> |};export const WRONG_TOKEN = "That token isn't right. It has to come from the machine running kwant bots, as the user running it.";+/** Only shown with the wrong-token error, not up front. */+export const OWN_TOKEN_HINT = "Each install has its own token, so one copied from another computer won't work here.";export const TokenForm: React.FC<{ notice: string | null; onSignedIn: () => void; personOnly?: boolean; onCancel?: () => void }> = ({notice,@@ -325,9 +303,9 @@ export const TokenForm: React.FC<{ notice: string | null; onSignedIn: () => voidconst [token, setToken] = useState('');const [busy, setBusy] = useState(false);const [error, setError] = useState<string | null>(null);- const hostname = typeof window === 'undefined' ? 'your-server' : window.location.hostname || 'your-server';- const port = typeof window === 'undefined' ? '8799' : kwantPort(window.location);- const [place, setPlace] = useState<TokenPlace>(() => guessPlace(hostname));+ const loc = typeof window === 'undefined' ? { protocol: 'https:', hostname: '', port: '' } : window.location;+ // A locked page on loopback is Docker, so its command comes first there+ const docker = isLoopback(loc.hostname);const inputRef = useRef<HTMLInputElement>(null);useEffect(() => {@@ -360,66 +338,40 @@ export const TokenForm: React.FC<{ notice: string | null; onSignedIn: () => void<h2 id={personOnly ? 'kwnt-person-token-title' : undefined} className="flex items-center gap-2 text-sm font-medium"><KeyRound size={15} className="text-sky-400" /> {personOnly ? 'Confirm it’s you' : 'Enter your access token'}</h2>- <p className="text-xs text-zinc-400 leading-relaxed">- {personOnly- ? 'Saved passwords, answers to approval cards and deleting bots or teams need your access token from another computer, so a bot can’t do them for you. You need it once per browser.'- : 'You’re opening kwant bots from another computer, so this browser needs its access token once.'}- </p>+ {personOnly && (+ <p className="text-xs text-zinc-400 leading-relaxed">+ Saved passwords, answers to approval cards and deleting bots or teams need your access token from another computer, so a bot can’t do them for you. You need it once per browser.+ </p>+ )}+ <p className="text-xs text-zinc-400 leading-relaxed">Find it on the machine running kwant bots:</p></div>- <fieldset className="space-y-3">- <legend className="text-xs font-medium text-zinc-300 mb-2">Where does kwant bots run?</legend>- <div className="grid grid-cols-3 gap-1 p-1 bg-zinc-950 border border-zinc-800 rounded-lg">- {PLACES.map((p) => (- <label- key={p.value}- className={`relative flex items-center justify-center text-center min-h-[36px] px-1.5 rounded-md text-[11px] font-medium cursor-pointer transition-colors has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-sky-500 ${- place === p.value ? 'bg-zinc-800 text-zinc-100' : 'text-zinc-400 hover:text-zinc-200'- }`}- >- <input- type="radio"- name="kwnt-token-place"- value={p.value}- checked={place === p.value}- onChange={() => setPlace(p.value)}- className="sr-only"- />- {p.label}- </label>- ))}- </div>- <TokenSteps place={place} hostname={hostname} port={port} />- </fieldset>+ <Command>{docker ? DOCKER_COMMAND : FILE_COMMAND}</Command>{notice && <p className="text-xs text-amber-300" role="status">{notice}</p>}- {place !== 'here' && (- <div>- <label htmlFor="kwnt-token" className="block text-xs font-medium text-zinc-400 mb-1">- Token- </label>- <p id="kwnt-token-hint" className="text-[11px] text-zinc-500 leading-snug mb-1.5">- Each install has its own token, so a token copied from another computer won't work here.+ <div>+ <label htmlFor="kwnt-token" className="sr-only">+ Token+ </label>+ <input+ id="kwnt-token"+ ref={inputRef}+ type="password"+ autoComplete="off"+ spellCheck={false}+ autoFocus+ value={token}+ onChange={(e) => setToken(e.target.value)}+ aria-invalid={!!error}+ aria-describedby={error ? 'kwnt-token-error' : undefined}+ className="w-full bg-zinc-950 border border-zinc-800 rounded-lg px-3 py-2 text-xs font-mono text-zinc-100 placeholder-zinc-600 focus:border-sky-500 focus:outline-none transition-colors"+ placeholder="Paste the token or the sign-in link"+ />+ {error && (+ <p id="kwnt-token-error" className="mt-1.5 text-[11px] text-rose-400 leading-snug" role="alert">+ {error}+ {error === WRONG_TOKEN && ` ${OWN_TOKEN_HINT}`}</p>- <input- id="kwnt-token"- ref={inputRef}- type="password"- autoComplete="off"- spellCheck={false}- autoFocus- value={token}- onChange={(e) => setToken(e.target.value)}- aria-invalid={!!error}- aria-describedby={error ? ['kwnt-token-hint', 'kwnt-token-error'].join(' ') : 'kwnt-token-hint'}- className="w-full bg-zinc-950 border border-zinc-800 rounded-lg px-3 py-2 text-xs font-mono text-zinc-100 placeholder-zinc-600 focus:border-sky-500 focus:outline-none transition-colors"- placeholder="Paste the token or the sign-in link"- />- {error && (- <p id="kwnt-token-error" className="mt-1.5 text-[11px] text-rose-400" role="alert">- {error}- </p>- )}- </div>- )}+ )}+ </div><div className="flex gap-2">{onCancel && (<button@@ -430,17 +382,16 @@ export const TokenForm: React.FC<{ notice: string | null; onSignedIn: () => voidNot now</button>)}- {place !== 'here' && (- <button- type="submit"- disabled={busy || !token.trim()}- className="flex-1 w-full flex items-center justify-center gap-1.5 px-4 py-2 rounded-lg text-xs font-medium text-white bg-sky-600 hover:bg-sky-500 transition-colors disabled:opacity-50"- >- {busy && <Loader2 size={13} className="animate-spin motion-reduce:animate-none" />} Continue- </button>- )}+ <button+ type="submit"+ disabled={busy || !token.trim()}+ className="flex-1 w-full flex items-center justify-center gap-1.5 px-4 py-2 rounded-lg text-xs font-medium text-white bg-sky-600 hover:bg-sky-500 transition-colors disabled:opacity-50"+ >+ {busy && <Loader2 size={13} className="animate-spin motion-reduce:animate-none" />} Continue+ </button></div>- <p className="text-[11px] text-zinc-500">The browser keeps you signed in for a year. Anyone with the token controls this server.</p>+ <OtherSetups loc={loc} docker={docker} />+ <p className="text-[11px] text-zinc-500">Stays signed in for a year. Anyone with the token controls this server.</p></form>);};diff --git a/frontend/tests/savedPasswords.test.mjs b/frontend/tests/savedPasswords.test.mjsindex 4853b2cb..37809d9e 100644--- a/frontend/tests/savedPasswords.test.mjs+++ b/frontend/tests/savedPasswords.test.mjs@@ -221,7 +221,8 @@ test('the token dialog says why it is asked from another computer and can be disassert.match(t, /Confirm it's you/);assert.match(t, /deleting bots or teams need your access token from another computer, so a bot can't do them for you/);assert.ok(!t.includes('even on this computer'), 'this computer never asks');- assert.match(t, /Where does kwant bots run\?/);+ assert.match(t, /Find it on the machine running kwant bots:/);+ assert.match(t, /Other setups/);assert.match(t, /kwnt token --raw/);assert.ok(buttons(html).includes('Not now'));assert.ok(buttons(html).includes('Continue'));diff --git a/frontend/tests/tokenSteps.test.mjs b/frontend/tests/tokenSteps.test.mjsindex aca6eabe..dcaea88c 100644--- a/frontend/tests/tokenSteps.test.mjs+++ b/frontend/tests/tokenSteps.test.mjs@@ -1,4 +1,4 @@-// The token screen: steps that work for where kwant runs, without kwnt on the PATH.+// The token screen: one command up front, other setups folded away, steps that work without kwnt on the PATH.import assert from 'node:assert/strict';import test from 'node:test';import { loadBundled } from './bundle.mjs';@@ -6,47 +6,64 @@ import { loadBundled } from './bundle.mjs';const m = await loadBundled(`import React from 'react';import { renderToStaticMarkup } from 'react-dom/server.browser';- import { TokenForm, TokenSteps, WRONG_TOKEN, guessPlace, selectForRetry, tokenFromPaste } from './src/components/AuthGate';- export { WRONG_TOKEN, guessPlace, selectForRetry, tokenFromPaste };+ import { OWN_TOKEN_HINT, OtherSetups, TokenForm, WRONG_TOKEN, isLoopback, selectForRetry, tokenFromPaste, tunnelCommand } from './src/components/AuthGate';+ export { OWN_TOKEN_HINT, WRONG_TOKEN, isLoopback, selectForRetry, tokenFromPaste, tunnelCommand };export const form = () => renderToStaticMarkup(React.createElement(TokenForm, { notice: null, onSignedIn: () => {} }));- export const steps = (place, hostname = 'vps.example.com', port = '8799') =>- renderToStaticMarkup(React.createElement(TokenSteps, { place, hostname, port }));+ export const other = (loc, docker = false) => renderToStaticMarkup(React.createElement(OtherSetups, { loc, docker }));`);const text = (html) => html.replace(/<[^>]+>/g, ' ').replace(/'|'/g, "'").replace(/</g, '<').replace(/>/g, '>').replace(/\s+/g, ' ');+const http = (hostname, port = '8799') => ({ protocol: 'http:', hostname, port });-test('server: the token file and a source-build path, so it works without kwnt on the PATH', () => {- const t = text(m.steps('server'));- assert.match(t, /as the user that runs kwant bots/);+test('the default view: one command, the token box and a folded "Other setups", no picker', () => {+ const html = m.form();+ const t = text(html);+ assert.match(t, /Find it on the machine running kwant bots:/);assert.match(t, /cat ~\/\.kwnt\/api-token/);+ assert.match(html, /placeholder="Paste the token or the sign-in link"/);+ assert.match(html, /<details class="[^"]*">/, 'closed by default');+ assert.match(t, /Other setups/);+ assert.match(t, /Stays signed in for a year\. Anyone with the token controls this server\./);+ assert.ok(!/Where does kwant bots run|On a server|type="radio"/.test(html));+ assert.ok(!t.includes(m.OWN_TOKEN_HINT.slice(0, 30)), 'the own-token hint only shows with a wrong token');+});++test('other setups: Docker, systemd and a source build, so it works without kwnt on the PATH', () => {+ const t = text(m.other(http('vps.example.com')));+ assert.match(t, /docker compose exec kwnt kwnt token --raw/);assert.match(t, /sudo cat \/var\/lib\/kwnt\/api-token/);assert.match(t, /\.\/target\/release\/kwnt token --raw/);});-test('server: an SSH tunnel to this host and port skips the token', () => {- const t = text(m.steps('server', 'vps.example.com', '9000'));- assert.match(t, /ssh -N -L 9000:127\.0\.0\.1:9000 user@vps\.example\.com/);- assert.match(t, /http:\/\/127\.0\.0\.1:9000/);- assert.match(t, /Not for Docker/);- assert.match(text(m.steps('server', '2001:db8::1')), /user@\[2001:db8::1\]/);+test('on loopback (Docker) the Docker command leads and the file command moves into other setups', () => {+ const t = text(m.other(http('127.0.0.1'), true));+ assert.match(t, /cat ~\/\.kwnt\/api-token/);+ assert.ok(!t.includes('docker compose'));});-test('docker: compose and plain docker, no tunnel', () => {- const t = text(m.steps('docker'));- assert.match(t, /docker compose exec kwnt kwnt token --raw/);- assert.match(t, /docker exec <container> kwnt token --raw/);- assert.ok(!t.includes('ssh -N'));+test('SSH tunnel: on plain http at a network address with a port, using that port', () => {+ const t = text(m.other(http('vps.example.com', '9000')));+ assert.match(t, /ssh -N -L 9000:127\.0\.0\.1:9000 user@vps\.example\.com/);+ assert.match(t, /http:\/\/127\.0\.0\.1:9000/);+ assert.equal(m.tunnelCommand(http('2001:db8::1')).command, 'ssh -N -L 8799:127.0.0.1:8799 user@[2001:db8::1]');});-test('this computer: open 127.0.0.1, which never asks', () => {- const html = m.steps('here', '192.168.1.20', '8799');- assert.match(html, /href="http:\/\/127\.0\.0\.1:8799"/);- assert.match(text(html), /not at this computer's network address/);+test('SSH tunnel: left out behind https, without a port, and on loopback', () => {+ for (const loc of [+ { protocol: 'https:', hostname: 'kwant.jumpin.games', port: '' },+ { protocol: 'https:', hostname: 'vps.example.com', port: '8443' },+ http('vps.example.com', ''),+ http('127.0.0.1'),+ http('localhost'),+ ]) {+ assert.equal(m.tunnelCommand(loc), null, JSON.stringify(loc));+ assert.ok(!text(m.other(loc)).includes('ssh -N'), JSON.stringify(loc));+ }});-test('a locked page on loopback is Docker; anywhere else a server', () => {- for (const h of ['127.0.0.1', 'localhost', '[::1]']) assert.equal(m.guessPlace(h), 'docker', h);- for (const h of ['vps.example.com', '10.0.0.5', 'mac.local']) assert.equal(m.guessPlace(h), 'server', h);+test('loopback hosts', () => {+ for (const h of ['127.0.0.1', 'localhost', '[::1]', '::1']) assert.ok(m.isLoopback(h), h);+ for (const h of ['vps.example.com', '10.0.0.5', 'mac.local']) assert.ok(!m.isLoopback(h), h);});test('pasting the whole sign-in link works as well as the token', () => {@@ -55,11 +72,9 @@ test('pasting the whole sign-in link works as well as the token', () => {assert.equal(m.tokenFromPaste('http://<this-host>:8799/#kwnt_token=tok'), 'tok');});-test('a token from another computer: the screen and the error say where it has to come from', () => {+test('a token from another computer: the error says where it has to come from', () => {assert.match(m.WRONG_TOKEN, /machine running kwant bots, as the user running it/);- const html = m.form();- assert.match(text(html), /a token copied from another computer won't work here/);- assert.match(html, /aria-describedby="kwnt-token-hint"/);+ assert.match(m.OWN_TOKEN_HINT, /one copied from another computer won't work here/);});test('after a wrong token the box is focused and selected, so the next paste replaces it', () => {