2026-02-13 12:31:36 +00:00
|
|
|
export type WsRequest =
|
2026-02-16 20:34:03 +00:00
|
|
|
| {
|
|
|
|
|
type: "chat";
|
|
|
|
|
messages: Message[];
|
|
|
|
|
config: ProviderConfig;
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
type: "cancel";
|
2026-02-23 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
type: "permission_response";
|
|
|
|
|
request_id: string;
|
|
|
|
|
approved: boolean;
|
2026-02-27 10:00:33 +00:00
|
|
|
always_allow: boolean;
|
2026-02-24 13:05:30 +00:00
|
|
|
}
|
2026-03-14 18:09:30 +00:00
|
|
|
| { type: "ping" }
|
|
|
|
|
| {
|
|
|
|
|
type: "side_question";
|
|
|
|
|
question: string;
|
|
|
|
|
context_messages: Message[];
|
|
|
|
|
config: ProviderConfig;
|
|
|
|
|
};
|
2026-02-13 12:31:36 +00:00
|
|
|
|
2026-02-23 13:23:35 +00:00
|
|
|
export interface AgentAssignment {
|
|
|
|
|
agent_name: string;
|
|
|
|
|
model: string | null;
|
|
|
|
|
status: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 19:39:19 +00:00
|
|
|
export interface PipelineStageItem {
|
|
|
|
|
story_id: string;
|
|
|
|
|
name: string | null;
|
|
|
|
|
error: string | null;
|
2026-02-27 10:23:30 +00:00
|
|
|
merge_failure: string | null;
|
2026-02-23 13:23:35 +00:00
|
|
|
agent: AgentAssignment | null;
|
2026-03-18 15:45:45 +00:00
|
|
|
review_hold: boolean | null;
|
|
|
|
|
manual_qa: boolean | null;
|
2026-02-20 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface PipelineState {
|
2026-03-18 14:31:12 +00:00
|
|
|
backlog: PipelineStageItem[];
|
2026-02-20 19:39:19 +00:00
|
|
|
current: PipelineStageItem[];
|
|
|
|
|
qa: PipelineStageItem[];
|
|
|
|
|
merge: PipelineStageItem[];
|
2026-02-24 23:42:59 +00:00
|
|
|
done: PipelineStageItem[];
|
2026-02-20 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
export type WsResponse =
|
2026-02-16 20:34:03 +00:00
|
|
|
| { type: "token"; content: string }
|
|
|
|
|
| { type: "update"; messages: Message[] }
|
2026-02-20 11:51:19 +00:00
|
|
|
| { type: "session_id"; session_id: string }
|
2026-02-20 19:39:19 +00:00
|
|
|
| { type: "error"; message: string }
|
2026-02-23 12:59:55 +00:00
|
|
|
| {
|
|
|
|
|
type: "pipeline_state";
|
2026-03-18 14:31:12 +00:00
|
|
|
backlog: PipelineStageItem[];
|
2026-02-23 12:59:55 +00:00
|
|
|
current: PipelineStageItem[];
|
|
|
|
|
qa: PipelineStageItem[];
|
|
|
|
|
merge: PipelineStageItem[];
|
2026-02-24 23:42:59 +00:00
|
|
|
done: PipelineStageItem[];
|
2026-02-23 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
type: "permission_request";
|
|
|
|
|
request_id: string;
|
|
|
|
|
tool_name: string;
|
|
|
|
|
tool_input: Record<string, unknown>;
|
2026-02-23 18:38:15 +00:00
|
|
|
}
|
2026-02-23 22:50:57 +00:00
|
|
|
| { type: "tool_activity"; tool_name: string }
|
|
|
|
|
| {
|
|
|
|
|
type: "reconciliation_progress";
|
|
|
|
|
story_id: string;
|
|
|
|
|
status: string;
|
|
|
|
|
message: string;
|
2026-02-23 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
/** `.story_kit/project.toml` was modified; re-fetch the agent roster. */
|
2026-02-24 12:33:31 +00:00
|
|
|
| { type: "agent_config_changed" }
|
2026-02-24 23:09:13 +00:00
|
|
|
/** An agent started, stopped, or changed state; re-fetch agent list. */
|
|
|
|
|
| { type: "agent_state_changed" }
|
2026-02-24 13:05:30 +00:00
|
|
|
| { type: "tool_activity"; tool_name: string }
|
|
|
|
|
/** Heartbeat response confirming the connection is alive. */
|
2026-02-24 15:34:31 +00:00
|
|
|
| { type: "pong" }
|
|
|
|
|
/** Sent on connect when the project still needs onboarding (specs are placeholders). */
|
2026-02-25 09:32:48 +00:00
|
|
|
| { type: "onboarding_status"; needs_onboarding: boolean }
|
|
|
|
|
/** Streaming thinking token from an extended-thinking block, separate from regular text. */
|
2026-03-14 18:09:30 +00:00
|
|
|
| { type: "thinking_token"; content: string }
|
|
|
|
|
/** Streaming token from a /btw side question response. */
|
|
|
|
|
| { type: "side_question_token"; content: string }
|
|
|
|
|
/** Final signal that the /btw side question has been fully answered. */
|
2026-03-19 01:29:33 +00:00
|
|
|
| { type: "side_question_done"; response: string }
|
|
|
|
|
/** A single server log entry (bulk on connect, then live). */
|
|
|
|
|
| { type: "log_entry"; timestamp: string; level: string; message: string };
|
2026-02-13 12:31:36 +00:00
|
|
|
|
|
|
|
|
export interface ProviderConfig {
|
2026-02-16 20:34:03 +00:00
|
|
|
provider: string;
|
|
|
|
|
model: string;
|
|
|
|
|
base_url?: string;
|
|
|
|
|
enable_tools?: boolean;
|
2026-02-20 11:51:19 +00:00
|
|
|
session_id?: string;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Role = "system" | "user" | "assistant" | "tool";
|
|
|
|
|
|
|
|
|
|
export interface ToolCall {
|
2026-02-16 20:34:03 +00:00
|
|
|
id?: string;
|
|
|
|
|
type: string;
|
|
|
|
|
function: {
|
|
|
|
|
name: string;
|
|
|
|
|
arguments: string;
|
|
|
|
|
};
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Message {
|
2026-02-16 20:34:03 +00:00
|
|
|
role: Role;
|
|
|
|
|
content: string;
|
|
|
|
|
tool_calls?: ToolCall[];
|
|
|
|
|
tool_call_id?: string;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-27 11:21:35 +00:00
|
|
|
export interface WorkItemContent {
|
|
|
|
|
content: string;
|
|
|
|
|
stage: string;
|
|
|
|
|
name: string | null;
|
2026-03-18 10:29:37 +00:00
|
|
|
agent: string | null;
|
2026-02-27 11:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-28 09:38:51 +00:00
|
|
|
export interface TestCaseResult {
|
|
|
|
|
name: string;
|
|
|
|
|
status: "pass" | "fail";
|
|
|
|
|
details: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TestResultsResponse {
|
|
|
|
|
unit: TestCaseResult[];
|
|
|
|
|
integration: TestCaseResult[];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
export interface FileEntry {
|
2026-02-16 20:34:03 +00:00
|
|
|
name: string;
|
|
|
|
|
kind: "file" | "dir";
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SearchResult {
|
2026-02-16 20:34:03 +00:00
|
|
|
path: string;
|
|
|
|
|
matches: number;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CommandOutput {
|
2026-02-16 20:34:03 +00:00
|
|
|
stdout: string;
|
|
|
|
|
stderr: string;
|
|
|
|
|
exit_code: number;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-19 17:29:50 +00:00
|
|
|
declare const __STORYKIT_PORT__: string;
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
const DEFAULT_API_BASE = "/api";
|
|
|
|
|
const DEFAULT_WS_PATH = "/ws";
|
|
|
|
|
|
2026-02-19 17:14:33 +00:00
|
|
|
export function resolveWsHost(
|
|
|
|
|
isDev: boolean,
|
|
|
|
|
envPort: string | undefined,
|
|
|
|
|
locationHost: string,
|
|
|
|
|
): string {
|
|
|
|
|
return isDev ? `127.0.0.1:${envPort || "3001"}` : locationHost;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
function buildApiUrl(path: string, baseUrl = DEFAULT_API_BASE): string {
|
2026-02-16 20:34:03 +00:00
|
|
|
return `${baseUrl}${path}`;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function requestJson<T>(
|
2026-02-16 20:34:03 +00:00
|
|
|
path: string,
|
|
|
|
|
options: RequestInit = {},
|
|
|
|
|
baseUrl = DEFAULT_API_BASE,
|
2026-02-13 12:31:36 +00:00
|
|
|
): Promise<T> {
|
2026-02-16 20:34:03 +00:00
|
|
|
const res = await fetch(buildApiUrl(path, baseUrl), {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
...(options.headers ?? {}),
|
|
|
|
|
},
|
|
|
|
|
...options,
|
|
|
|
|
});
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (!res.ok) {
|
|
|
|
|
const text = await res.text();
|
|
|
|
|
throw new Error(text || `Request failed (${res.status})`);
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
return res.json() as Promise<T>;
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const api = {
|
2026-02-16 20:34:03 +00:00
|
|
|
getCurrentProject(baseUrl?: string) {
|
|
|
|
|
return requestJson<string | null>("/project", {}, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
getKnownProjects(baseUrl?: string) {
|
|
|
|
|
return requestJson<string[]>("/projects", {}, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
forgetKnownProject(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>(
|
|
|
|
|
"/projects/forget",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
openProject(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<string>(
|
|
|
|
|
"/project",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
closeProject(baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>("/project", { method: "DELETE" }, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
getModelPreference(baseUrl?: string) {
|
|
|
|
|
return requestJson<string | null>("/model", {}, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
setModelPreference(model: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>(
|
|
|
|
|
"/model",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ model }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
getOllamaModels(baseUrlParam?: string, baseUrl?: string) {
|
|
|
|
|
const url = new URL(
|
|
|
|
|
buildApiUrl("/ollama/models", baseUrl),
|
|
|
|
|
window.location.origin,
|
|
|
|
|
);
|
|
|
|
|
if (baseUrlParam) {
|
|
|
|
|
url.searchParams.set("base_url", baseUrlParam);
|
|
|
|
|
}
|
|
|
|
|
return requestJson<string[]>(url.pathname + url.search, {}, "");
|
|
|
|
|
},
|
|
|
|
|
getAnthropicApiKeyExists(baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>("/anthropic/key/exists", {}, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
getAnthropicModels(baseUrl?: string) {
|
|
|
|
|
return requestJson<string[]>("/anthropic/models", {}, baseUrl);
|
|
|
|
|
},
|
|
|
|
|
setAnthropicApiKey(api_key: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>(
|
|
|
|
|
"/anthropic/key",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ api_key }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
readFile(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<string>(
|
|
|
|
|
"/fs/read",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
writeFile(path: string, content: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>(
|
|
|
|
|
"/fs/write",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path, content }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
listDirectory(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<FileEntry[]>(
|
|
|
|
|
"/fs/list",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
listDirectoryAbsolute(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<FileEntry[]>(
|
|
|
|
|
"/io/fs/list/absolute",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
createDirectoryAbsolute(path: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>(
|
|
|
|
|
"/io/fs/create/absolute",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ path }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
getHomeDirectory(baseUrl?: string) {
|
|
|
|
|
return requestJson<string>("/io/fs/home", {}, baseUrl);
|
|
|
|
|
},
|
2026-03-17 17:56:24 +00:00
|
|
|
listProjectFiles(baseUrl?: string) {
|
|
|
|
|
return requestJson<string[]>("/io/fs/files", {}, baseUrl);
|
|
|
|
|
},
|
2026-02-16 20:34:03 +00:00
|
|
|
searchFiles(query: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<SearchResult[]>(
|
|
|
|
|
"/fs/search",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ query }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
execShell(command: string, args: string[], baseUrl?: string) {
|
|
|
|
|
return requestJson<CommandOutput>(
|
|
|
|
|
"/shell/exec",
|
|
|
|
|
{ method: "POST", body: JSON.stringify({ command, args }) },
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
cancelChat(baseUrl?: string) {
|
|
|
|
|
return requestJson<boolean>("/chat/cancel", { method: "POST" }, baseUrl);
|
|
|
|
|
},
|
2026-02-27 11:21:35 +00:00
|
|
|
getWorkItemContent(storyId: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<WorkItemContent>(
|
|
|
|
|
`/work-items/${encodeURIComponent(storyId)}`,
|
|
|
|
|
{},
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-02-28 09:38:51 +00:00
|
|
|
getTestResults(storyId: string, baseUrl?: string) {
|
|
|
|
|
return requestJson<TestResultsResponse | null>(
|
|
|
|
|
`/work-items/${encodeURIComponent(storyId)}/test-results`,
|
|
|
|
|
{},
|
|
|
|
|
baseUrl,
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-03-18 15:45:45 +00:00
|
|
|
/** Approve a story in QA, moving it to merge. */
|
|
|
|
|
approveQa(storyId: string) {
|
|
|
|
|
return callMcpTool("approve_qa", { story_id: storyId });
|
|
|
|
|
},
|
|
|
|
|
/** Reject a story in QA, moving it back to current with notes. */
|
|
|
|
|
rejectQa(storyId: string, notes: string) {
|
|
|
|
|
return callMcpTool("reject_qa", { story_id: storyId, notes });
|
|
|
|
|
},
|
|
|
|
|
/** Launch the QA app for a story's worktree. */
|
|
|
|
|
launchQaApp(storyId: string) {
|
|
|
|
|
return callMcpTool("launch_qa_app", { story_id: storyId });
|
|
|
|
|
},
|
2026-02-13 12:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
2026-03-18 15:45:45 +00:00
|
|
|
async function callMcpTool(
|
|
|
|
|
toolName: string,
|
|
|
|
|
args: Record<string, unknown>,
|
|
|
|
|
): Promise<string> {
|
|
|
|
|
const res = await fetch("/mcp", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
jsonrpc: "2.0",
|
|
|
|
|
id: 1,
|
|
|
|
|
method: "tools/call",
|
|
|
|
|
params: { name: toolName, arguments: args },
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
const json = await res.json();
|
|
|
|
|
if (json.error) {
|
|
|
|
|
throw new Error(json.error.message);
|
|
|
|
|
}
|
|
|
|
|
const text = json.result?.content?.[0]?.text ?? "";
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
export class ChatWebSocket {
|
2026-02-16 20:34:03 +00:00
|
|
|
private static sharedSocket: WebSocket | null = null;
|
|
|
|
|
private static refCount = 0;
|
|
|
|
|
private socket?: WebSocket;
|
|
|
|
|
private onToken?: (content: string) => void;
|
2026-02-25 09:32:48 +00:00
|
|
|
private onThinkingToken?: (content: string) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
private onUpdate?: (messages: Message[]) => void;
|
2026-02-20 11:51:19 +00:00
|
|
|
private onSessionId?: (sessionId: string) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
private onError?: (message: string) => void;
|
2026-02-20 19:39:19 +00:00
|
|
|
private onPipelineState?: (state: PipelineState) => void;
|
2026-02-23 16:01:22 +00:00
|
|
|
private onPermissionRequest?: (
|
|
|
|
|
requestId: string,
|
|
|
|
|
toolName: string,
|
|
|
|
|
toolInput: Record<string, unknown>,
|
|
|
|
|
) => void;
|
2026-02-23 18:38:15 +00:00
|
|
|
private onActivity?: (toolName: string) => void;
|
2026-02-23 22:50:57 +00:00
|
|
|
private onReconciliationProgress?: (
|
|
|
|
|
storyId: string,
|
|
|
|
|
status: string,
|
|
|
|
|
message: string,
|
|
|
|
|
) => void;
|
2026-02-23 22:58:51 +00:00
|
|
|
private onAgentConfigChanged?: () => void;
|
2026-02-24 23:09:13 +00:00
|
|
|
private onAgentStateChanged?: () => void;
|
2026-02-24 15:34:31 +00:00
|
|
|
private onOnboardingStatus?: (needsOnboarding: boolean) => void;
|
2026-03-14 18:09:30 +00:00
|
|
|
private onSideQuestionToken?: (content: string) => void;
|
|
|
|
|
private onSideQuestionDone?: (response: string) => void;
|
2026-03-19 01:29:33 +00:00
|
|
|
private onLogEntry?: (
|
|
|
|
|
timestamp: string,
|
|
|
|
|
level: string,
|
|
|
|
|
message: string,
|
|
|
|
|
) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
private connected = false;
|
|
|
|
|
private closeTimer?: number;
|
2026-02-23 13:33:33 +00:00
|
|
|
private wsPath = DEFAULT_WS_PATH;
|
|
|
|
|
private reconnectTimer?: number;
|
|
|
|
|
private reconnectDelay = 1000;
|
|
|
|
|
private shouldReconnect = false;
|
2026-02-24 13:05:30 +00:00
|
|
|
private heartbeatInterval?: number;
|
|
|
|
|
private heartbeatTimeout?: number;
|
|
|
|
|
private static readonly HEARTBEAT_INTERVAL = 30_000;
|
|
|
|
|
private static readonly HEARTBEAT_TIMEOUT = 5_000;
|
|
|
|
|
|
|
|
|
|
private _startHeartbeat(): void {
|
|
|
|
|
this._stopHeartbeat();
|
|
|
|
|
this.heartbeatInterval = window.setInterval(() => {
|
|
|
|
|
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) return;
|
|
|
|
|
const ping: WsRequest = { type: "ping" };
|
|
|
|
|
this.socket.send(JSON.stringify(ping));
|
|
|
|
|
this.heartbeatTimeout = window.setTimeout(() => {
|
|
|
|
|
// No pong received within timeout; close socket to trigger reconnect.
|
|
|
|
|
this.socket?.close();
|
|
|
|
|
}, ChatWebSocket.HEARTBEAT_TIMEOUT);
|
|
|
|
|
}, ChatWebSocket.HEARTBEAT_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _stopHeartbeat(): void {
|
|
|
|
|
window.clearInterval(this.heartbeatInterval);
|
|
|
|
|
window.clearTimeout(this.heartbeatTimeout);
|
|
|
|
|
this.heartbeatInterval = undefined;
|
|
|
|
|
this.heartbeatTimeout = undefined;
|
|
|
|
|
}
|
2026-02-23 13:33:33 +00:00
|
|
|
|
|
|
|
|
private _buildWsUrl(): string {
|
|
|
|
|
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
|
|
|
|
|
const wsHost = resolveWsHost(
|
|
|
|
|
import.meta.env.DEV,
|
|
|
|
|
typeof __STORYKIT_PORT__ !== "undefined" ? __STORYKIT_PORT__ : undefined,
|
|
|
|
|
window.location.host,
|
|
|
|
|
);
|
|
|
|
|
return `${protocol}://${wsHost}${this.wsPath}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _attachHandlers(): void {
|
|
|
|
|
if (!this.socket) return;
|
|
|
|
|
this.socket.onopen = () => {
|
|
|
|
|
this.reconnectDelay = 1000;
|
2026-02-24 13:05:30 +00:00
|
|
|
this._startHeartbeat();
|
2026-02-23 13:33:33 +00:00
|
|
|
};
|
|
|
|
|
this.socket.onmessage = (event) => {
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(event.data) as WsResponse;
|
|
|
|
|
if (data.type === "token") this.onToken?.(data.content);
|
2026-02-25 09:32:48 +00:00
|
|
|
if (data.type === "thinking_token")
|
|
|
|
|
this.onThinkingToken?.(data.content);
|
2026-02-23 13:33:33 +00:00
|
|
|
if (data.type === "update") this.onUpdate?.(data.messages);
|
|
|
|
|
if (data.type === "session_id") this.onSessionId?.(data.session_id);
|
|
|
|
|
if (data.type === "error") this.onError?.(data.message);
|
|
|
|
|
if (data.type === "pipeline_state")
|
|
|
|
|
this.onPipelineState?.({
|
2026-03-18 14:31:12 +00:00
|
|
|
backlog: data.backlog,
|
2026-02-23 13:33:33 +00:00
|
|
|
current: data.current,
|
|
|
|
|
qa: data.qa,
|
|
|
|
|
merge: data.merge,
|
2026-02-24 23:42:59 +00:00
|
|
|
done: data.done,
|
2026-02-23 13:33:33 +00:00
|
|
|
});
|
2026-02-23 16:01:22 +00:00
|
|
|
if (data.type === "permission_request")
|
|
|
|
|
this.onPermissionRequest?.(
|
|
|
|
|
data.request_id,
|
|
|
|
|
data.tool_name,
|
|
|
|
|
data.tool_input,
|
|
|
|
|
);
|
2026-02-23 18:38:15 +00:00
|
|
|
if (data.type === "tool_activity") this.onActivity?.(data.tool_name);
|
2026-02-23 22:50:57 +00:00
|
|
|
if (data.type === "reconciliation_progress")
|
|
|
|
|
this.onReconciliationProgress?.(
|
|
|
|
|
data.story_id,
|
|
|
|
|
data.status,
|
|
|
|
|
data.message,
|
|
|
|
|
);
|
2026-02-23 22:58:51 +00:00
|
|
|
if (data.type === "agent_config_changed") this.onAgentConfigChanged?.();
|
2026-02-24 23:09:13 +00:00
|
|
|
if (data.type === "agent_state_changed") this.onAgentStateChanged?.();
|
2026-02-24 15:34:31 +00:00
|
|
|
if (data.type === "onboarding_status")
|
|
|
|
|
this.onOnboardingStatus?.(data.needs_onboarding);
|
2026-03-14 18:09:30 +00:00
|
|
|
if (data.type === "side_question_token")
|
|
|
|
|
this.onSideQuestionToken?.(data.content);
|
|
|
|
|
if (data.type === "side_question_done")
|
|
|
|
|
this.onSideQuestionDone?.(data.response);
|
2026-03-19 01:29:33 +00:00
|
|
|
if (data.type === "log_entry")
|
|
|
|
|
this.onLogEntry?.(data.timestamp, data.level, data.message);
|
2026-02-24 13:05:30 +00:00
|
|
|
if (data.type === "pong") {
|
|
|
|
|
window.clearTimeout(this.heartbeatTimeout);
|
|
|
|
|
this.heartbeatTimeout = undefined;
|
|
|
|
|
}
|
2026-02-23 13:33:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
this.onError?.(String(err));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.socket.onerror = () => {
|
|
|
|
|
this.onError?.("WebSocket error");
|
|
|
|
|
};
|
|
|
|
|
this.socket.onclose = () => {
|
|
|
|
|
if (this.shouldReconnect && this.connected) {
|
|
|
|
|
this._scheduleReconnect();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _scheduleReconnect(): void {
|
|
|
|
|
window.clearTimeout(this.reconnectTimer);
|
|
|
|
|
const delay = this.reconnectDelay;
|
|
|
|
|
this.reconnectDelay = Math.min(this.reconnectDelay * 2, 30000);
|
|
|
|
|
this.reconnectTimer = window.setTimeout(() => {
|
|
|
|
|
this.reconnectTimer = undefined;
|
|
|
|
|
const wsUrl = this._buildWsUrl();
|
|
|
|
|
ChatWebSocket.sharedSocket = new WebSocket(wsUrl);
|
|
|
|
|
this.socket = ChatWebSocket.sharedSocket;
|
|
|
|
|
this._attachHandlers();
|
|
|
|
|
}, delay);
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
connect(
|
|
|
|
|
handlers: {
|
|
|
|
|
onToken?: (content: string) => void;
|
2026-02-25 09:32:48 +00:00
|
|
|
onThinkingToken?: (content: string) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
onUpdate?: (messages: Message[]) => void;
|
2026-02-20 11:51:19 +00:00
|
|
|
onSessionId?: (sessionId: string) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
onError?: (message: string) => void;
|
2026-02-20 19:39:19 +00:00
|
|
|
onPipelineState?: (state: PipelineState) => void;
|
2026-02-23 16:01:22 +00:00
|
|
|
onPermissionRequest?: (
|
|
|
|
|
requestId: string,
|
|
|
|
|
toolName: string,
|
|
|
|
|
toolInput: Record<string, unknown>,
|
|
|
|
|
) => void;
|
2026-02-23 18:38:15 +00:00
|
|
|
onActivity?: (toolName: string) => void;
|
2026-02-23 22:50:57 +00:00
|
|
|
onReconciliationProgress?: (
|
|
|
|
|
storyId: string,
|
|
|
|
|
status: string,
|
|
|
|
|
message: string,
|
|
|
|
|
) => void;
|
2026-02-23 22:58:51 +00:00
|
|
|
onAgentConfigChanged?: () => void;
|
2026-02-24 23:09:13 +00:00
|
|
|
onAgentStateChanged?: () => void;
|
2026-02-24 15:34:31 +00:00
|
|
|
onOnboardingStatus?: (needsOnboarding: boolean) => void;
|
2026-03-14 18:09:30 +00:00
|
|
|
onSideQuestionToken?: (content: string) => void;
|
|
|
|
|
onSideQuestionDone?: (response: string) => void;
|
2026-03-19 01:29:33 +00:00
|
|
|
onLogEntry?: (timestamp: string, level: string, message: string) => void;
|
2026-02-16 20:34:03 +00:00
|
|
|
},
|
|
|
|
|
wsPath = DEFAULT_WS_PATH,
|
|
|
|
|
) {
|
|
|
|
|
this.onToken = handlers.onToken;
|
2026-02-25 09:32:48 +00:00
|
|
|
this.onThinkingToken = handlers.onThinkingToken;
|
2026-02-16 20:34:03 +00:00
|
|
|
this.onUpdate = handlers.onUpdate;
|
2026-02-20 11:51:19 +00:00
|
|
|
this.onSessionId = handlers.onSessionId;
|
2026-02-16 20:34:03 +00:00
|
|
|
this.onError = handlers.onError;
|
2026-02-20 19:39:19 +00:00
|
|
|
this.onPipelineState = handlers.onPipelineState;
|
2026-02-23 16:01:22 +00:00
|
|
|
this.onPermissionRequest = handlers.onPermissionRequest;
|
2026-02-23 18:38:15 +00:00
|
|
|
this.onActivity = handlers.onActivity;
|
2026-02-23 22:50:57 +00:00
|
|
|
this.onReconciliationProgress = handlers.onReconciliationProgress;
|
2026-02-23 22:58:51 +00:00
|
|
|
this.onAgentConfigChanged = handlers.onAgentConfigChanged;
|
2026-02-24 23:09:13 +00:00
|
|
|
this.onAgentStateChanged = handlers.onAgentStateChanged;
|
2026-02-24 15:34:31 +00:00
|
|
|
this.onOnboardingStatus = handlers.onOnboardingStatus;
|
2026-03-14 18:09:30 +00:00
|
|
|
this.onSideQuestionToken = handlers.onSideQuestionToken;
|
|
|
|
|
this.onSideQuestionDone = handlers.onSideQuestionDone;
|
2026-03-19 01:29:33 +00:00
|
|
|
this.onLogEntry = handlers.onLogEntry;
|
2026-02-23 13:33:33 +00:00
|
|
|
this.wsPath = wsPath;
|
|
|
|
|
this.shouldReconnect = true;
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (this.connected) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.connected = true;
|
|
|
|
|
ChatWebSocket.refCount += 1;
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (
|
|
|
|
|
!ChatWebSocket.sharedSocket ||
|
|
|
|
|
ChatWebSocket.sharedSocket.readyState === WebSocket.CLOSED ||
|
|
|
|
|
ChatWebSocket.sharedSocket.readyState === WebSocket.CLOSING
|
|
|
|
|
) {
|
2026-02-23 13:33:33 +00:00
|
|
|
const wsUrl = this._buildWsUrl();
|
2026-02-16 20:34:03 +00:00
|
|
|
ChatWebSocket.sharedSocket = new WebSocket(wsUrl);
|
|
|
|
|
}
|
|
|
|
|
this.socket = ChatWebSocket.sharedSocket;
|
2026-02-23 13:33:33 +00:00
|
|
|
this._attachHandlers();
|
2026-02-16 20:34:03 +00:00
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
sendChat(messages: Message[], config: ProviderConfig) {
|
|
|
|
|
this.send({ type: "chat", messages, config });
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-03-14 18:09:30 +00:00
|
|
|
sendSideQuestion(
|
|
|
|
|
question: string,
|
|
|
|
|
contextMessages: Message[],
|
|
|
|
|
config: ProviderConfig,
|
|
|
|
|
) {
|
|
|
|
|
this.send({
|
|
|
|
|
type: "side_question",
|
|
|
|
|
question,
|
|
|
|
|
context_messages: contextMessages,
|
|
|
|
|
config,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
cancel() {
|
|
|
|
|
this.send({ type: "cancel" });
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-27 10:00:33 +00:00
|
|
|
sendPermissionResponse(
|
|
|
|
|
requestId: string,
|
|
|
|
|
approved: boolean,
|
|
|
|
|
alwaysAllow = false,
|
|
|
|
|
) {
|
|
|
|
|
this.send({
|
|
|
|
|
type: "permission_response",
|
|
|
|
|
request_id: requestId,
|
|
|
|
|
approved,
|
|
|
|
|
always_allow: alwaysAllow,
|
|
|
|
|
});
|
2026-02-23 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
close() {
|
2026-02-23 13:33:33 +00:00
|
|
|
this.shouldReconnect = false;
|
2026-02-24 13:05:30 +00:00
|
|
|
this._stopHeartbeat();
|
2026-02-23 13:33:33 +00:00
|
|
|
window.clearTimeout(this.reconnectTimer);
|
|
|
|
|
this.reconnectTimer = undefined;
|
|
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (!this.connected) return;
|
|
|
|
|
this.connected = false;
|
|
|
|
|
ChatWebSocket.refCount = Math.max(0, ChatWebSocket.refCount - 1);
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (import.meta.env.DEV) {
|
|
|
|
|
if (this.closeTimer) {
|
|
|
|
|
window.clearTimeout(this.closeTimer);
|
|
|
|
|
}
|
|
|
|
|
this.closeTimer = window.setTimeout(() => {
|
|
|
|
|
if (ChatWebSocket.refCount === 0) {
|
|
|
|
|
ChatWebSocket.sharedSocket?.close();
|
|
|
|
|
ChatWebSocket.sharedSocket = null;
|
|
|
|
|
}
|
|
|
|
|
this.socket = ChatWebSocket.sharedSocket ?? undefined;
|
|
|
|
|
this.closeTimer = undefined;
|
|
|
|
|
}, 250);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
if (ChatWebSocket.refCount === 0) {
|
|
|
|
|
ChatWebSocket.sharedSocket?.close();
|
|
|
|
|
ChatWebSocket.sharedSocket = null;
|
|
|
|
|
}
|
|
|
|
|
this.socket = ChatWebSocket.sharedSocket ?? undefined;
|
|
|
|
|
}
|
2026-02-16 18:57:39 +00:00
|
|
|
|
2026-02-16 20:34:03 +00:00
|
|
|
private send(payload: WsRequest) {
|
|
|
|
|
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
|
|
|
this.onError?.("WebSocket is not connected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.socket.send(JSON.stringify(payload));
|
|
|
|
|
}
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|