Initial CodeCraft: web-based coding harness for AI models
Rust backend (axum/tokio): agent loop with streaming, tools (fs, bash, web_fetch, todo, ask_user, subagents, skills, craft snapshots/compare), permissions with modes and rules, Anthropic + OpenAI-compatible providers, MCP client (stdio/HTTP, elicitation), SQLite sessions, usage statistics, compaction, commented TOML config editable via toml_edit. React PWA: chat with live streaming, permission/question cards, modes (build/plan/craft), settings for providers/models/prices/MCP/permissions, raw config editor, statistics dashboard, i18n (en/de). Docker setup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f3a2fe3816
62 files changed
+21835
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import type { ServerEvent } from "./types";
|
||||
|
||||
const TOKEN_KEY = "codecraft.token";
|
||||
|
||||
export function getToken(): string {
|
||||
return localStorage.getItem(TOKEN_KEY) ?? "";
|
||||
}
|
||||
|
||||
export function setToken(token: string) {
|
||||
localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
message: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function api<T = any>(path: string, init: RequestInit & { json?: unknown } = {}): Promise<T> {
|
||||
const headers: Record<string, string> = { ...(init.headers as Record<string, string>) };
|
||||
const token = getToken();
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
let body = init.body;
|
||||
if (init.json !== undefined) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
body = JSON.stringify(init.json);
|
||||
}
|
||||
const res = await fetch(`/api${path}`, { ...init, headers, body });
|
||||
const text = await res.text();
|
||||
const data = text ? JSON.parse(text) : null;
|
||||
if (!res.ok) throw new ApiError(res.status, data?.error ?? res.statusText);
|
||||
return data as T;
|
||||
}
|
||||
|
||||
export const get = <T = any>(path: string) => api<T>(path);
|
||||
export const post = <T = any>(path: string, json?: unknown) => api<T>(path, { method: "POST", json: json ?? {} });
|
||||
export const patch = <T = any>(path: string, json: unknown) => api<T>(path, { method: "PATCH", json });
|
||||
export const put = <T = any>(path: string, json: unknown) => api<T>(path, { method: "PUT", json });
|
||||
export const del = <T = any>(path: string) => api<T>(path, { method: "DELETE" });
|
||||
|
||||
/** WebSocket with automatic reconnect. `onOpen` runs after every (re)connect so
|
||||
* the caller can resync state that may have been missed. */
|
||||
export function connectEvents(handlers: {
|
||||
onEvent: (ev: ServerEvent) => void;
|
||||
onOpen: () => void;
|
||||
onClose: () => void;
|
||||
}): () => void {
|
||||
let ws: WebSocket | null = null;
|
||||
let stopped = false;
|
||||
let retry = 0;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const open = () => {
|
||||
const proto = location.protocol === "https:" ? "wss" : "ws";
|
||||
const token = encodeURIComponent(getToken());
|
||||
ws = new WebSocket(`${proto}://${location.host}/api/ws?token=${token}`);
|
||||
ws.onopen = () => {
|
||||
retry = 0;
|
||||
handlers.onOpen();
|
||||
};
|
||||
ws.onmessage = (m) => {
|
||||
try {
|
||||
handlers.onEvent(JSON.parse(m.data));
|
||||
} catch {
|
||||
/* ignore malformed */
|
||||
}
|
||||
};
|
||||
ws.onclose = () => {
|
||||
handlers.onClose();
|
||||
if (stopped) return;
|
||||
retry = Math.min(retry + 1, 6);
|
||||
timer = setTimeout(open, 500 * 2 ** retry);
|
||||
};
|
||||
};
|
||||
open();
|
||||
// Reconnect immediately when the app comes back to the foreground (mobile PWA).
|
||||
const onVisible = () => {
|
||||
if (document.visibilityState === "visible" && ws && ws.readyState > WebSocket.OPEN) {
|
||||
clearTimeout(timer);
|
||||
open();
|
||||
}
|
||||
};
|
||||
document.addEventListener("visibilitychange", onVisible);
|
||||
return () => {
|
||||
stopped = true;
|
||||
clearTimeout(timer);
|
||||
document.removeEventListener("visibilitychange", onVisible);
|
||||
ws?.close();
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user