Merge epic-backlog-and-gitdiff: v2.7.14 backlog hardening + v2.7.15 git diff panel
Two plans delivered via paseo-epic in an isolated worktree, audited green: - v2.7.14: post-review backlog (external task-cancel + finalization, tool-call parser prune + pino logging, BooChat stall-timeout, view_session_history MCP tool, retire the :9502 fallback SPA). - v2.7.15: git diff panel (Files/Git tab in the file browser with stage/commit/ discard, server-side argv-safe git, sessionEvent-driven refresh).
This commit is contained in:
@@ -7,7 +7,6 @@ WORKDIR /build
|
|||||||
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml tsconfig.base.json ./
|
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml tsconfig.base.json ./
|
||||||
COPY apps/server/package.json ./apps/server/
|
COPY apps/server/package.json ./apps/server/
|
||||||
COPY apps/coder/package.json ./apps/coder/
|
COPY apps/coder/package.json ./apps/coder/
|
||||||
COPY apps/coder/web/package.json ./apps/coder/web/
|
|
||||||
|
|
||||||
RUN pnpm install --frozen-lockfile
|
RUN pnpm install --frozen-lockfile
|
||||||
|
|
||||||
@@ -16,7 +15,6 @@ COPY apps/server ./apps/server
|
|||||||
RUN pnpm -C apps/server build
|
RUN pnpm -C apps/server build
|
||||||
|
|
||||||
COPY apps/coder ./apps/coder
|
COPY apps/coder ./apps/coder
|
||||||
RUN pnpm -C apps/coder/web build
|
|
||||||
RUN pnpm -C apps/coder build
|
RUN pnpm -C apps/coder build
|
||||||
|
|
||||||
RUN pnpm deploy --filter=@boocode/coder --prod --legacy /out/coder
|
RUN pnpm deploy --filter=@boocode/coder --prod --legacy /out/coder
|
||||||
@@ -27,7 +25,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends ripgrep git ope
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /out/coder ./
|
COPY --from=builder /out/coder ./
|
||||||
COPY --from=builder /build/apps/coder/web/dist ./web
|
|
||||||
|
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
"@agentclientprotocol/sdk": "^0.22.1",
|
"@agentclientprotocol/sdk": "^0.22.1",
|
||||||
"@anthropic-ai/claude-agent-sdk": "^0.3.159",
|
"@anthropic-ai/claude-agent-sdk": "^0.3.159",
|
||||||
"@boocode/server": "workspace:*",
|
"@boocode/server": "workspace:*",
|
||||||
"@fastify/static": "^7.0.4",
|
|
||||||
"@fastify/websocket": "^10.0.1",
|
"@fastify/websocket": "^10.0.1",
|
||||||
"@modelcontextprotocol/sdk": "^1.29.0",
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
||||||
"@opencode-ai/sdk": "~1.15.0",
|
"@opencode-ai/sdk": "~1.15.0",
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
import { resolve, dirname } from 'node:path';
|
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import { existsSync } from 'node:fs';
|
|
||||||
import Fastify from 'fastify';
|
import Fastify from 'fastify';
|
||||||
import fastifyWebsocket from '@fastify/websocket';
|
import fastifyWebsocket from '@fastify/websocket';
|
||||||
import fastifyStatic from '@fastify/static';
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
|
||||||
const __dirname = dirname(__filename);
|
|
||||||
import { loadConfig } from './config.js';
|
import { loadConfig } from './config.js';
|
||||||
import { getSql, applySchema, pingDb, closeDb } from './db.js';
|
import { getSql, applySchema, pingDb, closeDb } from './db.js';
|
||||||
import { startMcpServer } from './services/mcp-server.js';
|
import { startMcpServer } from './services/mcp-server.js';
|
||||||
@@ -257,7 +250,7 @@ async function main() {
|
|||||||
registerPendingRoutes(app, sql);
|
registerPendingRoutes(app, sql);
|
||||||
registerCheckpointRoutes(app, sql);
|
registerCheckpointRoutes(app, sql);
|
||||||
registerAgentSessionRoutes(app, sql);
|
registerAgentSessionRoutes(app, sql);
|
||||||
registerTaskRoutes(app, sql, inferenceApi);
|
registerTaskRoutes(app, sql, inferenceApi, dispatcher.cancelExternalTask);
|
||||||
registerInboxRoutes(app, sql);
|
registerInboxRoutes(app, sql);
|
||||||
registerStatsRoutes(app, sql);
|
registerStatsRoutes(app, sql);
|
||||||
registerArenaRoutes(app, sql);
|
registerArenaRoutes(app, sql);
|
||||||
@@ -266,28 +259,6 @@ async function main() {
|
|||||||
registerLifecycleRoutes(app, sql);
|
registerLifecycleRoutes(app, sql);
|
||||||
registerWebSocket(app, sql, broker);
|
registerWebSocket(app, sql, broker);
|
||||||
|
|
||||||
// Serve static frontend (built web app). In production, the dist/ is
|
|
||||||
// copied to ../web relative to the dist/ directory at /app/web. In dev,
|
|
||||||
// check adjacent to the source.
|
|
||||||
const webRoot = resolve(__dirname, '../web');
|
|
||||||
if (existsSync(webRoot)) {
|
|
||||||
await app.register(fastifyStatic, {
|
|
||||||
root: webRoot,
|
|
||||||
prefix: '/',
|
|
||||||
// Don't intercept /api routes — static only serves files that exist.
|
|
||||||
wildcard: false,
|
|
||||||
});
|
|
||||||
// SPA fallback: serve index.html for non-API routes that don't match a file.
|
|
||||||
app.setNotFoundHandler(async (req, reply) => {
|
|
||||||
if (req.url.startsWith('/api')) {
|
|
||||||
reply.code(404);
|
|
||||||
return { error: 'not found' };
|
|
||||||
}
|
|
||||||
return reply.sendFile('index.html');
|
|
||||||
});
|
|
||||||
app.log.info(`serving frontend from ${webRoot}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Graceful shutdown
|
// Graceful shutdown
|
||||||
const shutdown = async () => {
|
const shutdown = async () => {
|
||||||
app.log.info('shutting down');
|
app.log.info('shutting down');
|
||||||
|
|||||||
138
apps/coder/src/routes/__tests__/tasks-cancel.test.ts
Normal file
138
apps/coder/src/routes/__tests__/tasks-cancel.test.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
import { resolve } from 'node:path';
|
||||||
|
import Fastify, { type FastifyInstance } from 'fastify';
|
||||||
|
import postgres from 'postgres';
|
||||||
|
import { registerTaskRoutes } from '../tasks.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1 — POST /api/tasks/:id/cancel route wiring.
|
||||||
|
*
|
||||||
|
* The route's job: reach the in-flight external run via `cancelExternal(taskId)`
|
||||||
|
* (the new abort hook), keep cancelling native inference for open chats unchanged,
|
||||||
|
* and land the task row in 'cancelled'. The streaming assistant message is
|
||||||
|
* finalized by the dispatcher's run-function, not here — that path is covered by
|
||||||
|
* finalize-message.test.ts. This suite pins the route's behavior against a real DB.
|
||||||
|
*/
|
||||||
|
describe.runIf(!!process.env.DATABASE_URL)('POST /api/tasks/:id/cancel (route, F1)', () => {
|
||||||
|
let sql: ReturnType<typeof postgres>;
|
||||||
|
let app: FastifyInstance;
|
||||||
|
let projectId: string;
|
||||||
|
let sessionId: string;
|
||||||
|
let chatId: string;
|
||||||
|
|
||||||
|
const externalCancelCalls: string[] = [];
|
||||||
|
const inferenceCancelCalls: Array<[string, string]> = [];
|
||||||
|
let externalReturns = true;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
sql = postgres(process.env.DATABASE_URL!, { max: 3 });
|
||||||
|
const serverSchema = resolve(__dirname, '../../../../server/src/schema.sql');
|
||||||
|
const coderSchema = resolve(__dirname, '../../schema.sql');
|
||||||
|
await sql.unsafe(readFileSync(serverSchema, 'utf8'));
|
||||||
|
await sql.unsafe(readFileSync(coderSchema, 'utf8'));
|
||||||
|
|
||||||
|
const [p] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO projects (name, path, status) VALUES ('f1-cancel-route', '/tmp/f1-cancel-route', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
projectId = p!.id;
|
||||||
|
const [s] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO sessions (project_id, name, model, status) VALUES (${projectId}, 'f1', 'm', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
sessionId = s!.id;
|
||||||
|
const [c] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO chats (session_id, name, status) VALUES (${sessionId}, 'tab', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
chatId = c!.id;
|
||||||
|
|
||||||
|
app = Fastify();
|
||||||
|
registerTaskRoutes(
|
||||||
|
app,
|
||||||
|
sql,
|
||||||
|
{
|
||||||
|
cancel: async (sid: string, cid: string) => {
|
||||||
|
inferenceCancelCalls.push([sid, cid]);
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
(taskId: string) => {
|
||||||
|
externalCancelCalls.push(taskId);
|
||||||
|
return externalReturns;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await app.ready();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (app) await app.close();
|
||||||
|
if (!sql) return;
|
||||||
|
await sql`DELETE FROM messages WHERE session_id = ${sessionId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM tasks WHERE project_id = ${projectId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM chats WHERE id = ${chatId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM sessions WHERE id = ${sessionId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM projects WHERE id = ${projectId}`.catch(() => {});
|
||||||
|
await sql.end({ timeout: 5 });
|
||||||
|
});
|
||||||
|
|
||||||
|
async function insertTask(agent: string | null, state: string): Promise<string> {
|
||||||
|
const [t] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO tasks (project_id, input, agent, session_id, state, started_at)
|
||||||
|
VALUES (${projectId}, 'do a thing', ${agent}, ${sessionId}, ${state}, clock_timestamp())
|
||||||
|
RETURNING id
|
||||||
|
`;
|
||||||
|
return t!.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('reaches cancelExternal and lands the task cancelled for a running external task', async () => {
|
||||||
|
externalReturns = true;
|
||||||
|
externalCancelCalls.length = 0;
|
||||||
|
const taskId = await insertTask('opencode', 'running');
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: `/api/tasks/${taskId}/cancel` });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.json()).toEqual({ cancelled: true });
|
||||||
|
|
||||||
|
expect(externalCancelCalls).toContain(taskId);
|
||||||
|
|
||||||
|
const [row] = await sql<{ state: string; ended_at: Date | null }[]>`
|
||||||
|
SELECT state, ended_at FROM tasks WHERE id = ${taskId}
|
||||||
|
`;
|
||||||
|
expect(row!.state).toBe('cancelled');
|
||||||
|
expect(row!.ended_at).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('still cancels a native boocode task (cancelExternal returns false → inference.cancel path unchanged)', async () => {
|
||||||
|
externalReturns = false; // native task: no controller registered
|
||||||
|
externalCancelCalls.length = 0;
|
||||||
|
inferenceCancelCalls.length = 0;
|
||||||
|
const taskId = await insertTask(null, 'running');
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: `/api/tasks/${taskId}/cancel` });
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
|
||||||
|
// The route calls cancelExternal unconditionally (cheap, returns false here)...
|
||||||
|
expect(externalCancelCalls).toContain(taskId);
|
||||||
|
// ...and the native inference.cancel path still fires for the open chat.
|
||||||
|
expect(inferenceCancelCalls).toContainEqual([sessionId, chatId]);
|
||||||
|
|
||||||
|
const [row] = await sql<{ state: string }[]>`SELECT state FROM tasks WHERE id = ${taskId}`;
|
||||||
|
expect(row!.state).toBe('cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects cancelling an already-terminal task with 409 and never touches the abort hook', async () => {
|
||||||
|
externalCancelCalls.length = 0;
|
||||||
|
const taskId = await insertTask('opencode', 'completed');
|
||||||
|
|
||||||
|
const res = await app.inject({ method: 'POST', url: `/api/tasks/${taskId}/cancel` });
|
||||||
|
expect(res.statusCode).toBe(409);
|
||||||
|
expect(externalCancelCalls).not.toContain(taskId);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns 404 for an unknown task', async () => {
|
||||||
|
const res = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/api/tasks/00000000-0000-0000-0000-000000000000/cancel`,
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(404);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,6 +8,12 @@ interface InferenceApi {
|
|||||||
cancel: (sessionId: string, chatId: string) => Promise<boolean>;
|
cancel: (sessionId: string, chatId: string) => Promise<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// F1: the dispatcher's reach into an in-flight external-agent run. Narrow by
|
||||||
|
// design (not the whole dispatcher) — the route only needs to fire the abort.
|
||||||
|
// Returns true when a controller was registered for the task (an external run was
|
||||||
|
// in flight), false otherwise (native boocode task, or already finished).
|
||||||
|
export type ExternalCancelFn = (taskId: string) => boolean;
|
||||||
|
|
||||||
const CreateBody = z.object({
|
const CreateBody = z.object({
|
||||||
project_id: z.string().uuid(),
|
project_id: z.string().uuid(),
|
||||||
input: z.string().min(1).max(64_000),
|
input: z.string().min(1).max(64_000),
|
||||||
@@ -27,7 +33,12 @@ const ListQuery = z.object({
|
|||||||
project_id: z.string().uuid().optional(),
|
project_id: z.string().uuid().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export function registerTaskRoutes(app: FastifyInstance, sql: Sql, inference: InferenceApi): void {
|
export function registerTaskRoutes(
|
||||||
|
app: FastifyInstance,
|
||||||
|
sql: Sql,
|
||||||
|
inference: InferenceApi,
|
||||||
|
cancelExternal: ExternalCancelFn,
|
||||||
|
): void {
|
||||||
// POST /api/tasks — create a new task
|
// POST /api/tasks — create a new task
|
||||||
app.post('/api/tasks', async (req, reply) => {
|
app.post('/api/tasks', async (req, reply) => {
|
||||||
const parsed = CreateBody.safeParse(req.body);
|
const parsed = CreateBody.safeParse(req.body);
|
||||||
@@ -127,7 +138,14 @@ export function registerTaskRoutes(app: FastifyInstance, sql: Sql, inference: In
|
|||||||
|
|
||||||
cancelPendingPermission(taskId);
|
cancelPendingPermission(taskId);
|
||||||
|
|
||||||
// If running, try to cancel inference
|
// F1: abort the in-flight external-agent run (opencode / goose / qwen / claude).
|
||||||
|
// Idempotent — a double-Stop re-aborts harmlessly; a native boocode task is not
|
||||||
|
// registered, so this returns false and the inference.cancel path below handles
|
||||||
|
// it unchanged. The dispatcher's run-function finalizes the streaming assistant
|
||||||
|
// message as 'cancelled' once the backend honors the signal.
|
||||||
|
cancelExternal(taskId);
|
||||||
|
|
||||||
|
// If running, try to cancel inference (native boocode path — unchanged).
|
||||||
if ((task.state === 'running' || task.state === 'blocked') && task.session_id) {
|
if ((task.state === 'running' || task.state === 'blocked') && task.session_id) {
|
||||||
// Find active chat in the task's session
|
// Find active chat in the task's session
|
||||||
const chats = await sql<{ id: string }[]>`
|
const chats = await sql<{ id: string }[]>`
|
||||||
|
|||||||
51
apps/coder/src/services/__tests__/cancel-registry.test.ts
Normal file
51
apps/coder/src/services/__tests__/cancel-registry.test.ts
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { createCancelRegistry } from '../cancel-registry.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1 — per-task abort wiring. The registry is the missing link between the Stop
|
||||||
|
* route and the in-flight external run: register an AbortController per task id,
|
||||||
|
* cancel(taskId) aborts its signal, the run's .finally deletes it. Pure (no DB /
|
||||||
|
* child / IO) so the abort + idempotency contract is unit-testable in isolation.
|
||||||
|
*/
|
||||||
|
describe('CancelRegistry (F1 abort wiring)', () => {
|
||||||
|
it('register hands back a fresh controller; cancel aborts its signal', () => {
|
||||||
|
const reg = createCancelRegistry();
|
||||||
|
const ac = reg.register('t1');
|
||||||
|
expect(ac.signal.aborted).toBe(false);
|
||||||
|
expect(reg.has('t1')).toBe(true);
|
||||||
|
|
||||||
|
expect(reg.cancel('t1')).toBe(true);
|
||||||
|
expect(ac.signal.aborted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cancel on an unknown task returns false (native task / cancel-before-register)', () => {
|
||||||
|
const reg = createCancelRegistry();
|
||||||
|
expect(reg.has('nope')).toBe(false);
|
||||||
|
expect(reg.cancel('nope')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('double-Stop is idempotent: a second cancel never throws and the signal stays aborted', () => {
|
||||||
|
const reg = createCancelRegistry();
|
||||||
|
const ac = reg.register('t1');
|
||||||
|
|
||||||
|
expect(reg.cancel('t1')).toBe(true);
|
||||||
|
// The run-function has not hit its .finally yet, so the entry is still
|
||||||
|
// present — a rapid second Stop re-aborts (abort() no-ops) without throwing.
|
||||||
|
expect(() => reg.cancel('t1')).not.toThrow();
|
||||||
|
expect(reg.cancel('t1')).toBe(true);
|
||||||
|
expect(ac.signal.aborted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cancel after delete returns false (cancel-after-natural-exit is safe)', () => {
|
||||||
|
const reg = createCancelRegistry();
|
||||||
|
reg.register('t1');
|
||||||
|
reg.delete('t1');
|
||||||
|
expect(reg.has('t1')).toBe(false);
|
||||||
|
expect(reg.cancel('t1')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('delete of an unknown id is a no-op (never throws)', () => {
|
||||||
|
const reg = createCancelRegistry();
|
||||||
|
expect(() => reg.delete('ghost')).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
163
apps/coder/src/services/__tests__/finalize-message.test.ts
Normal file
163
apps/coder/src/services/__tests__/finalize-message.test.ts
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||||
|
import { readFileSync } from 'node:fs';
|
||||||
|
import { resolve } from 'node:path';
|
||||||
|
import postgres from 'postgres';
|
||||||
|
import type { WsFrame } from '@boocode/contracts/ws-frames';
|
||||||
|
import { classifyTerminalStatus, finalizeStreamingMessage } from '../finalize-message.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1 (D-7 / OCE-001 / OCE-002) — finalizing a Stop'd or errored external turn.
|
||||||
|
*
|
||||||
|
* `classifyTerminalStatus` is the pure D-7 decision (user Stop / AbortError →
|
||||||
|
* cancelled, genuine error → failed). `finalizeStreamingMessage` writes that
|
||||||
|
* terminal state onto the streaming assistant row and publishes the matching
|
||||||
|
* message_complete frame — idempotently, guarded by `WHERE status='streaming'`,
|
||||||
|
* so a double-Stop or an abort-then-catch settles the message exactly once and
|
||||||
|
* never clobbers a row that already finished cleanly.
|
||||||
|
*/
|
||||||
|
describe('classifyTerminalStatus (pure, D-7)', () => {
|
||||||
|
it('maps a fired abort signal to cancelled (user Stop)', () => {
|
||||||
|
expect(classifyTerminalStatus({ aborted: true })).toBe('cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps a thrown AbortError to cancelled', () => {
|
||||||
|
const e = new Error('the operation was aborted');
|
||||||
|
e.name = 'AbortError';
|
||||||
|
expect(classifyTerminalStatus({ aborted: false, error: e })).toBe('cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps a genuine thrown error to failed', () => {
|
||||||
|
expect(classifyTerminalStatus({ aborted: false, error: new Error('boom') })).toBe('failed');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('defaults a no-abort / no-error catch to failed', () => {
|
||||||
|
expect(classifyTerminalStatus({ aborted: false })).toBe('failed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe.runIf(!!process.env.DATABASE_URL)('finalizeStreamingMessage (DB)', () => {
|
||||||
|
let sql: ReturnType<typeof postgres>;
|
||||||
|
let projectId: string;
|
||||||
|
let sessionId: string;
|
||||||
|
let chatId: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
sql = postgres(process.env.DATABASE_URL!, { max: 3 });
|
||||||
|
// Server schema owns messages/sessions/chats (FK targets); coder schema after.
|
||||||
|
const serverSchema = resolve(__dirname, '../../../../server/src/schema.sql');
|
||||||
|
const coderSchema = resolve(__dirname, '../../schema.sql');
|
||||||
|
await sql.unsafe(readFileSync(serverSchema, 'utf8'));
|
||||||
|
await sql.unsafe(readFileSync(coderSchema, 'utf8'));
|
||||||
|
|
||||||
|
const [p] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO projects (name, path, status) VALUES ('f1-finalize', '/tmp/f1-finalize', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
projectId = p!.id;
|
||||||
|
const [s] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO sessions (project_id, name, model, status) VALUES (${projectId}, 'f1', 'm', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
sessionId = s!.id;
|
||||||
|
const [c] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO chats (session_id, name, status) VALUES (${sessionId}, 'tab', 'open') RETURNING id
|
||||||
|
`;
|
||||||
|
chatId = c!.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (!sql) return;
|
||||||
|
await sql`DELETE FROM messages WHERE session_id = ${sessionId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM chats WHERE id = ${chatId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM sessions WHERE id = ${sessionId}`.catch(() => {});
|
||||||
|
await sql`DELETE FROM projects WHERE id = ${projectId}`.catch(() => {});
|
||||||
|
await sql.end({ timeout: 5 });
|
||||||
|
});
|
||||||
|
|
||||||
|
async function insertStreaming(): Promise<string> {
|
||||||
|
const [m] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO messages (session_id, chat_id, role, content, status)
|
||||||
|
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming') RETURNING id
|
||||||
|
`;
|
||||||
|
return m!.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('finalizes a streaming row to cancelled, persists partial content, publishes one frame', async () => {
|
||||||
|
const id = await insertStreaming();
|
||||||
|
const frames: WsFrame[] = [];
|
||||||
|
const did = await finalizeStreamingMessage(sql, (_s, f) => frames.push(f), {
|
||||||
|
sessionId,
|
||||||
|
chatId,
|
||||||
|
assistantId: id,
|
||||||
|
status: 'cancelled',
|
||||||
|
model: 'qwen',
|
||||||
|
content: 'partial answer',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(did).toBe(true);
|
||||||
|
const [row] = await sql<{ status: string; content: string; finished_at: Date | null }[]>`
|
||||||
|
SELECT status, content, finished_at FROM messages WHERE id = ${id}
|
||||||
|
`;
|
||||||
|
expect(row!.status).toBe('cancelled');
|
||||||
|
expect(row!.content).toBe('partial answer');
|
||||||
|
expect(row!.finished_at).not.toBeNull();
|
||||||
|
expect(frames).toHaveLength(1);
|
||||||
|
expect(frames[0]!.type).toBe('message_complete');
|
||||||
|
expect((frames[0] as { status?: string }).status).toBe('cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is idempotent for a double-Stop: second call updates nothing and re-publishes nothing', async () => {
|
||||||
|
const id = await insertStreaming();
|
||||||
|
const frames: WsFrame[] = [];
|
||||||
|
const push = (_s: string, f: WsFrame): void => {
|
||||||
|
frames.push(f);
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await finalizeStreamingMessage(sql, push, { sessionId, chatId, assistantId: id, status: 'cancelled', model: null }),
|
||||||
|
).toBe(true);
|
||||||
|
expect(
|
||||||
|
await finalizeStreamingMessage(sql, push, { sessionId, chatId, assistantId: id, status: 'cancelled', model: null }),
|
||||||
|
).toBe(false);
|
||||||
|
|
||||||
|
expect(frames).toHaveLength(1);
|
||||||
|
const [row] = await sql<{ status: string }[]>`SELECT status FROM messages WHERE id = ${id}`;
|
||||||
|
expect(row!.status).toBe('cancelled');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('never clobbers a row that already finished cleanly (abort raced a clean finish)', async () => {
|
||||||
|
const [m] = await sql<{ id: string }[]>`
|
||||||
|
INSERT INTO messages (session_id, chat_id, role, content, status)
|
||||||
|
VALUES (${sessionId}, ${chatId}, 'assistant', 'done', 'complete') RETURNING id
|
||||||
|
`;
|
||||||
|
const id = m!.id;
|
||||||
|
const frames: WsFrame[] = [];
|
||||||
|
|
||||||
|
const did = await finalizeStreamingMessage(sql, (_s, f) => frames.push(f), {
|
||||||
|
sessionId,
|
||||||
|
chatId,
|
||||||
|
assistantId: id,
|
||||||
|
status: 'cancelled',
|
||||||
|
model: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(did).toBe(false);
|
||||||
|
expect(frames).toHaveLength(0);
|
||||||
|
const [row] = await sql<{ status: string; content: string }[]>`
|
||||||
|
SELECT status, content FROM messages WHERE id = ${id}
|
||||||
|
`;
|
||||||
|
expect(row!.status).toBe('complete');
|
||||||
|
expect(row!.content).toBe('done');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('no-ops on an empty assistantId (throw happened before the row was created)', async () => {
|
||||||
|
const frames: WsFrame[] = [];
|
||||||
|
const did = await finalizeStreamingMessage(sql, (_s, f) => frames.push(f), {
|
||||||
|
sessionId,
|
||||||
|
chatId,
|
||||||
|
assistantId: '',
|
||||||
|
status: 'failed',
|
||||||
|
model: null,
|
||||||
|
});
|
||||||
|
expect(did).toBe(false);
|
||||||
|
expect(frames).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
50
apps/coder/src/services/cancel-registry.ts
Normal file
50
apps/coder/src/services/cancel-registry.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* F1 — per-task abort registry. A Stop on an external-agent task must reach the
|
||||||
|
* in-flight run and abort its child / prompt. Each external run-function registers
|
||||||
|
* its per-turn AbortController here keyed by task id; the cancel route calls
|
||||||
|
* `cancel(taskId)` to fire it; the run-function's `.finally` deletes the entry.
|
||||||
|
*
|
||||||
|
* Idempotent by construction:
|
||||||
|
* - `cancel()` on an already-aborted controller no-ops (AbortController.abort()
|
||||||
|
* is idempotent) → a rapid double-Stop is safe.
|
||||||
|
* - `cancel()` on an unknown / already-finished task returns false → a
|
||||||
|
* cancel-after-natural-exit (entry already deleted) and a Stop on a native
|
||||||
|
* boocode task (never registered) are both safe no-ops.
|
||||||
|
*
|
||||||
|
* Pure (no DB / child / IO) so the abort wiring + idempotency contract is
|
||||||
|
* unit-testable in isolation — mirrors the turn-guard / lifecycle-decisions
|
||||||
|
* pure-helper precedent.
|
||||||
|
*/
|
||||||
|
export interface CancelRegistry {
|
||||||
|
/** Create + store an AbortController for this task, returning it for the run. */
|
||||||
|
register(taskId: string): AbortController;
|
||||||
|
/** Abort the task's in-flight run. Returns false when no controller is registered. */
|
||||||
|
cancel(taskId: string): boolean;
|
||||||
|
/** Drop the task's entry (called from the run's `.finally`). No-op if absent. */
|
||||||
|
delete(taskId: string): void;
|
||||||
|
/** Whether a controller is currently registered for this task. */
|
||||||
|
has(taskId: string): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createCancelRegistry(): CancelRegistry {
|
||||||
|
const controllers = new Map<string, AbortController>();
|
||||||
|
return {
|
||||||
|
register(taskId) {
|
||||||
|
const ac = new AbortController();
|
||||||
|
controllers.set(taskId, ac);
|
||||||
|
return ac;
|
||||||
|
},
|
||||||
|
cancel(taskId) {
|
||||||
|
const ac = controllers.get(taskId);
|
||||||
|
if (!ac) return false;
|
||||||
|
ac.abort();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
delete(taskId) {
|
||||||
|
controllers.delete(taskId);
|
||||||
|
},
|
||||||
|
has(taskId) {
|
||||||
|
return controllers.has(taskId);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -22,6 +22,12 @@ import { shouldUseClaudeSdk } from './backends/claude-sdk-routing.js';
|
|||||||
import type { AgentBackend, AgentEvent } from './agent-backend.js';
|
import type { AgentBackend, AgentEvent } from './agent-backend.js';
|
||||||
import { publishAgentStatus } from './agent-status-publish.js';
|
import { publishAgentStatus } from './agent-status-publish.js';
|
||||||
import type { AgentStatus } from './normalize-agent-status.js';
|
import type { AgentStatus } from './normalize-agent-status.js';
|
||||||
|
import { createCancelRegistry } from './cancel-registry.js';
|
||||||
|
import {
|
||||||
|
finalizeStreamingMessage,
|
||||||
|
classifyTerminalStatus,
|
||||||
|
type TerminalMessageStatus,
|
||||||
|
} from './finalize-message.js';
|
||||||
|
|
||||||
interface InferenceRunner {
|
interface InferenceRunner {
|
||||||
enqueue: (sessionId: string, chatId: string, assistantId: string, user: string) => void;
|
enqueue: (sessionId: string, chatId: string, assistantId: string, user: string) => void;
|
||||||
@@ -43,7 +49,11 @@ interface Deps {
|
|||||||
const POLL_INTERVAL_MS = 2_000;
|
const POLL_INTERVAL_MS = 2_000;
|
||||||
const COMPLETION_POLL_MS = 2_000;
|
const COMPLETION_POLL_MS = 2_000;
|
||||||
|
|
||||||
export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<void> } {
|
export function createDispatcher(deps: Deps): {
|
||||||
|
cancelExternalTask(taskId: string): boolean;
|
||||||
|
start(): void;
|
||||||
|
stop(): Promise<void>;
|
||||||
|
} {
|
||||||
const { sql, inference, broker, log, config } = deps;
|
const { sql, inference, broker, log, config } = deps;
|
||||||
let timer: ReturnType<typeof setInterval> | null = null;
|
let timer: ReturnType<typeof setInterval> | null = null;
|
||||||
let listener: { unlisten: () => Promise<void> } | null = null;
|
let listener: { unlisten: () => Promise<void> } | null = null;
|
||||||
@@ -55,6 +65,13 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
// turn at a time.
|
// turn at a time.
|
||||||
const inflight = new Map<string, Promise<void>>();
|
const inflight = new Map<string, Promise<void>>();
|
||||||
|
|
||||||
|
// F1: per-task abort registry. Each external run-function registers its per-turn
|
||||||
|
// AbortController here (keyed by task id); the cancel route reaches it through the
|
||||||
|
// exported `cancelExternalTask`; the run's `.finally` deletes the entry. Native
|
||||||
|
// boocode tasks are never registered, so a Stop on one returns false and falls
|
||||||
|
// through to the unchanged inference.cancel path.
|
||||||
|
const taskControllers = createCancelRegistry();
|
||||||
|
|
||||||
// Shared entry point for both the poll timer and the NOTIFY listener. poll()'s
|
// Shared entry point for both the poll timer and the NOTIFY listener. poll()'s
|
||||||
// `polling`/`stopping` guard makes this safe to call concurrently — a notify
|
// `polling`/`stopping` guard makes this safe to call concurrently — a notify
|
||||||
// arriving mid-poll returns immediately and never double-dispatches.
|
// arriving mid-poll returns immediately and never double-dispatches.
|
||||||
@@ -83,6 +100,40 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
publishAgentStatus(broker.publishFrame, sessionId, chatId, agent, status, reason);
|
publishAgentStatus(broker.publishFrame, sessionId, chatId, agent, status, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// F1 (OCE-001/OCE-002): finalize a streaming assistant message into a terminal
|
||||||
|
// state and publish the matching message_complete frame. Best-effort + idempotent
|
||||||
|
// (the helper's `WHERE status='streaming'` guard) — a failure here must never mask
|
||||||
|
// the original abort/error, so it logs and swallows.
|
||||||
|
function finalizeMessage(
|
||||||
|
sessionId: string,
|
||||||
|
chatId: string,
|
||||||
|
assistantId: string,
|
||||||
|
status: TerminalMessageStatus,
|
||||||
|
model: string | null,
|
||||||
|
content?: string,
|
||||||
|
): Promise<boolean> {
|
||||||
|
return finalizeStreamingMessage(sql, broker.publishFrame, {
|
||||||
|
sessionId,
|
||||||
|
chatId,
|
||||||
|
assistantId,
|
||||||
|
status,
|
||||||
|
model,
|
||||||
|
content,
|
||||||
|
}).catch((err) => {
|
||||||
|
log.error({ err: err instanceof Error ? err.message : String(err), assistantId }, 'dispatcher: finalizeStreamingMessage failed');
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// F1: the cancel route's reach into an in-flight external run. Idempotent — a
|
||||||
|
// double-Stop re-aborts an already-aborted controller (no-op) and a Stop on a
|
||||||
|
// finished/native task returns false. Aborting only fires the backend's per-turn
|
||||||
|
// cancel (session.abort / session/cancel / interrupt / child.kill); it never kills
|
||||||
|
// a warm pool process, so persistent worktrees + pooled backends are preserved.
|
||||||
|
function cancelExternalTask(taskId: string): boolean {
|
||||||
|
return taskControllers.cancel(taskId);
|
||||||
|
}
|
||||||
|
|
||||||
async function poll(): Promise<void> {
|
async function poll(): Promise<void> {
|
||||||
// `polling` serializes poll() execution itself (timer + NOTIFY can fire
|
// `polling` serializes poll() execution itself (timer + NOTIFY can fire
|
||||||
// concurrently) so we never double-select a task. It does NOT serialize task
|
// concurrently) so we never double-select a task. It does NOT serialize task
|
||||||
@@ -116,6 +167,9 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
// with the same key is skipped and a concurrent poll can't re-pick it.
|
// with the same key is skipped and a concurrent poll can't re-pick it.
|
||||||
const p = runTask(task).finally(() => {
|
const p = runTask(task).finally(() => {
|
||||||
inflight.delete(key);
|
inflight.delete(key);
|
||||||
|
// F1: drop the abort controller once the run settles. After this, a Stop
|
||||||
|
// on the (now-finished) task returns false — cancel-after-exit is safe.
|
||||||
|
taskControllers.delete(task.id);
|
||||||
});
|
});
|
||||||
inflight.set(key, p);
|
inflight.set(key, p);
|
||||||
}
|
}
|
||||||
@@ -312,13 +366,16 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an abort controller for this task
|
// F1: register the per-task abort controller so a Stop reaches this run.
|
||||||
const ac = new AbortController();
|
const ac = taskControllers.register(taskId);
|
||||||
|
|
||||||
// #10: hoisted above the try so the catch block can report `error` status with
|
// #10: hoisted above the try so the catch block can report `error` status with
|
||||||
// the (chat, agent) key. Empty until resolved below; guarded before use.
|
// the (chat, agent) key. Empty until resolved below; guarded before use.
|
||||||
let sessionId = '';
|
let sessionId = '';
|
||||||
let chatId = '';
|
let chatId = '';
|
||||||
|
// F1: hoisted so the catch / abort short-circuit can finalize the streaming
|
||||||
|
// assistant row. Empty until the row is created; finalize no-ops on ''.
|
||||||
|
let assistantId = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Mark running
|
// Mark running
|
||||||
@@ -384,7 +441,7 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`;
|
`;
|
||||||
const assistantId = assistantMsg!.id;
|
assistantId = assistantMsg!.id;
|
||||||
|
|
||||||
// write-edit-robustness #4: pre-turn worktree checkpoint (best-effort; a
|
// write-edit-robustness #4: pre-turn worktree checkpoint (best-effort; a
|
||||||
// failure logs and never breaks dispatch). This path uses a per-task worktree
|
// failure logs and never breaks dispatch). This path uses a per-task worktree
|
||||||
@@ -526,6 +583,20 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// F1: abort short-circuit BEFORE the unconditional 'complete' write. A Stop
|
||||||
|
// (cancelExternalTask → ac.abort) or shutdown finalizes the streaming row as
|
||||||
|
// 'cancelled' (keeping whatever streamed) instead of recording 'complete',
|
||||||
|
// and skips the diff. This one-shot path owns a per-task worktree, so we DO
|
||||||
|
// tear it down here (unlike the warm paths, which keep their persistent one).
|
||||||
|
if (ac.signal.aborted || stopping) {
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, 'cancelled', task.model, assistantContent);
|
||||||
|
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
||||||
|
emitAgentStatus(sessionId, chatId, agent, 'idle', stopping ? 'shutdown' : 'cancelled');
|
||||||
|
await cleanupWorktree(projectPath, taskId);
|
||||||
|
clearTaskCommands(taskId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE messages
|
UPDATE messages
|
||||||
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
||||||
@@ -539,14 +610,6 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
model: task.model,
|
model: task.model,
|
||||||
} as WsFrame);
|
} as WsFrame);
|
||||||
|
|
||||||
if (stopping) {
|
|
||||||
await sql`
|
|
||||||
UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}
|
|
||||||
`;
|
|
||||||
await cleanupWorktree(projectPath, taskId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Diff the worktree and queue pending changes
|
// Step 3: Diff the worktree and queue pending changes
|
||||||
log.info({ taskId }, 'dispatcher: diffing worktree');
|
log.info({ taskId }, 'dispatcher: diffing worktree');
|
||||||
const diff = await diffWorktree(worktreePath, projectPath, { signal: ac.signal });
|
const diff = await diffWorktree(worktreePath, projectPath, { signal: ac.signal });
|
||||||
@@ -587,18 +650,26 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errMsg = err instanceof Error ? err.message : String(err);
|
const errMsg = err instanceof Error ? err.message : String(err);
|
||||||
|
const status = classifyTerminalStatus({ aborted: ac.signal.aborted, error: err });
|
||||||
log.error({ taskId, agent, err: errMsg }, 'dispatcher: external agent error');
|
log.error({ taskId, agent, err: errMsg }, 'dispatcher: external agent error');
|
||||||
|
|
||||||
|
// Guard `NOT IN ('cancelled','completed')` so a genuine error in the catch
|
||||||
|
// never overwrites a state the cancel route already wrote (user-Stop wins).
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE tasks
|
UPDATE tasks
|
||||||
SET state = 'failed', ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
SET state = ${status}, ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
||||||
WHERE id = ${taskId}
|
WHERE id = ${taskId} AND state NOT IN ('cancelled', 'completed')
|
||||||
`.catch(() => {});
|
`.catch(() => {});
|
||||||
|
|
||||||
|
// F1 (OCE-001): finalize the streaming assistant message — the catch
|
||||||
|
// previously updated only `tasks` and left the message 'streaming' forever
|
||||||
|
// (the BooChat 5-min sweep runs in a different process and can't reach it).
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, status, task.model);
|
||||||
|
|
||||||
// #10: external-agent turn failed/crashed. chatId may be unbound if the throw
|
// #10: external-agent turn failed/crashed. chatId may be unbound if the throw
|
||||||
// preceded its assignment — guard so the status publish never masks the real
|
// preceded its assignment — guard so the status publish never masks the real
|
||||||
// error.
|
// error.
|
||||||
if (chatId) emitAgentStatus(sessionId, chatId, agent, 'error', 'failed');
|
if (chatId) emitAgentStatus(sessionId, chatId, agent, status === 'cancelled' ? 'idle' : 'error', status === 'cancelled' ? 'cancelled' : 'failed');
|
||||||
|
|
||||||
// Best-effort cleanup
|
// Best-effort cleanup
|
||||||
await cleanupWorktree(projectPath, taskId);
|
await cleanupWorktree(projectPath, taskId);
|
||||||
@@ -652,11 +723,14 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ac = new AbortController();
|
// F1: register the per-task abort controller so a Stop reaches this run.
|
||||||
|
const ac = taskControllers.register(taskId);
|
||||||
|
|
||||||
// #10: hoisted so the catch can report `error` with the (chat, agent) key.
|
// #10: hoisted so the catch can report `error` with the (chat, agent) key.
|
||||||
let sessionId = '';
|
let sessionId = '';
|
||||||
let chatId = '';
|
let chatId = '';
|
||||||
|
// F1: hoisted so the catch / abort short-circuit can finalize the streaming row.
|
||||||
|
let assistantId = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// execution_path = 'acp' — the schema CHECK has no 'opencode_server' value
|
// execution_path = 'acp' — the schema CHECK has no 'opencode_server' value
|
||||||
@@ -728,7 +802,7 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`;
|
`;
|
||||||
const assistantId = assistantMsg!.id;
|
assistantId = assistantMsg!.id;
|
||||||
|
|
||||||
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
||||||
// worktree (best-effort; never breaks dispatch). worktreeId comes from the
|
// worktree (best-effort; never breaks dispatch). worktreeId comes from the
|
||||||
@@ -856,6 +930,18 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
|
|
||||||
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
||||||
|
|
||||||
|
// F1: abort short-circuit BEFORE the unconditional 'complete' write — fixes
|
||||||
|
// the warm success-path recording 'complete' on a Stop'd turn. The abort fired
|
||||||
|
// session.abort on the prompt only: the persistent session worktree is kept
|
||||||
|
// (no cleanup) and the pooled opencode server stays warm for the next turn.
|
||||||
|
if (ac.signal.aborted || stopping) {
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, 'cancelled', task.model, assistantContent);
|
||||||
|
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
||||||
|
emitAgentStatus(sessionId, chatId, agent, 'idle', stopping ? 'shutdown' : 'cancelled');
|
||||||
|
clearTaskCommands(taskId);
|
||||||
|
return; // worktree persists (no cleanup); backend stays warm
|
||||||
|
}
|
||||||
|
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE messages
|
UPDATE messages
|
||||||
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
||||||
@@ -868,11 +954,6 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
model: task.model,
|
model: task.model,
|
||||||
} as WsFrame);
|
} as WsFrame);
|
||||||
|
|
||||||
if (stopping) {
|
|
||||||
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
|
||||||
return; // worktree persists (no cleanup); backend stays warm
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1.10: diff the persistent worktree against its captured baseline and
|
// 1.10: diff the persistent worktree against its captured baseline and
|
||||||
// SUPERSEDE the session's prior pending row (latest-wins, one accumulating
|
// SUPERSEDE the session's prior pending row (latest-wins, one accumulating
|
||||||
// diff) instead of stacking. Stamp agent for DiffPanel attribution.
|
// diff) instead of stacking. Stamp agent for DiffPanel attribution.
|
||||||
@@ -920,14 +1001,17 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errMsg = err instanceof Error ? err.message : String(err);
|
const errMsg = err instanceof Error ? err.message : String(err);
|
||||||
|
const status = classifyTerminalStatus({ aborted: ac.signal.aborted, error: err });
|
||||||
log.error({ taskId, agent, err: errMsg }, 'dispatcher: opencode server error');
|
log.error({ taskId, agent, err: errMsg }, 'dispatcher: opencode server error');
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE tasks
|
UPDATE tasks
|
||||||
SET state = 'failed', ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
SET state = ${status}, ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
||||||
WHERE id = ${taskId}
|
WHERE id = ${taskId} AND state NOT IN ('cancelled', 'completed')
|
||||||
`.catch(() => {});
|
`.catch(() => {});
|
||||||
|
// F1 (OCE-001): finalize the streaming message (was left 'streaming').
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, status, task.model);
|
||||||
// #10: turn crashed.
|
// #10: turn crashed.
|
||||||
if (chatId) emitAgentStatus(sessionId, chatId, agent, 'error', 'crashed');
|
if (chatId) emitAgentStatus(sessionId, chatId, agent, status === 'cancelled' ? 'idle' : 'error', status === 'cancelled' ? 'cancelled' : 'crashed');
|
||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
||||||
}
|
}
|
||||||
@@ -988,7 +1072,10 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ac = new AbortController();
|
// F1: register the per-task abort controller so a Stop reaches this run.
|
||||||
|
const ac = taskControllers.register(taskId);
|
||||||
|
// F1: hoisted so the catch / abort short-circuit can finalize the streaming row.
|
||||||
|
let assistantId = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sql`
|
await sql`
|
||||||
@@ -1010,7 +1097,7 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`;
|
`;
|
||||||
const assistantId = assistantMsg!.id;
|
assistantId = assistantMsg!.id;
|
||||||
|
|
||||||
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
||||||
// worktree (best-effort; never breaks dispatch). Same worktree the opencode
|
// worktree (best-effort; never breaks dispatch). Same worktree the opencode
|
||||||
@@ -1121,6 +1208,18 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
|
|
||||||
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
||||||
|
|
||||||
|
// F1: abort short-circuit BEFORE the unconditional 'complete' write — fixes
|
||||||
|
// the warm success-path recording 'complete' on a Stop'd turn. The abort fired
|
||||||
|
// session/cancel on the warm connection only (never killed the child), so the
|
||||||
|
// persistent worktree is kept and the pooled (chat,agent) backend stays warm.
|
||||||
|
if (ac.signal.aborted || stopping) {
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, 'cancelled', task.model, assistantContent);
|
||||||
|
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
||||||
|
emitAgentStatus(sessionId, chatId, agent, 'idle', stopping ? 'shutdown' : 'cancelled');
|
||||||
|
clearTaskCommands(taskId);
|
||||||
|
return; // worktree persists (no cleanup); backend stays warm
|
||||||
|
}
|
||||||
|
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE messages
|
UPDATE messages
|
||||||
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
SET content = ${assistantContent}, status = 'complete', finished_at = clock_timestamp()
|
||||||
@@ -1133,11 +1232,6 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
model: task.model,
|
model: task.model,
|
||||||
} as WsFrame);
|
} as WsFrame);
|
||||||
|
|
||||||
if (stopping) {
|
|
||||||
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
|
||||||
return; // worktree persists (no cleanup); backend stays warm
|
|
||||||
}
|
|
||||||
|
|
||||||
// Diff the persistent worktree against its captured baseline and SUPERSEDE
|
// Diff the persistent worktree against its captured baseline and SUPERSEDE
|
||||||
// the session's prior pending row (latest-wins) — identical to opencode.
|
// the session's prior pending row (latest-wins) — identical to opencode.
|
||||||
const diff = await diffWorktree(worktreePath, projectPath, {
|
const diff = await diffWorktree(worktreePath, projectPath, {
|
||||||
@@ -1184,14 +1278,17 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errMsg = err instanceof Error ? err.message : String(err);
|
const errMsg = err instanceof Error ? err.message : String(err);
|
||||||
|
const status = classifyTerminalStatus({ aborted: ac.signal.aborted, error: err });
|
||||||
log.error({ taskId, agent, err: errMsg }, 'dispatcher: warm ACP error');
|
log.error({ taskId, agent, err: errMsg }, 'dispatcher: warm ACP error');
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE tasks
|
UPDATE tasks
|
||||||
SET state = 'failed', ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
SET state = ${status}, ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
||||||
WHERE id = ${taskId}
|
WHERE id = ${taskId} AND state NOT IN ('cancelled', 'completed')
|
||||||
`.catch(() => {});
|
`.catch(() => {});
|
||||||
|
// F1 (OCE-001): finalize the streaming message (was left 'streaming').
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, status, task.model);
|
||||||
// #10: turn crashed.
|
// #10: turn crashed.
|
||||||
emitAgentStatus(sessionId, chatId, agent, 'error', 'crashed');
|
emitAgentStatus(sessionId, chatId, agent, status === 'cancelled' ? 'idle' : 'error', status === 'cancelled' ? 'cancelled' : 'crashed');
|
||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
||||||
}
|
}
|
||||||
@@ -1245,7 +1342,10 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ac = new AbortController();
|
// F1: register the per-task abort controller so a Stop reaches this run.
|
||||||
|
const ac = taskControllers.register(taskId);
|
||||||
|
// F1: hoisted so the catch / abort short-circuit can finalize the streaming row.
|
||||||
|
let assistantId = '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sql`
|
await sql`
|
||||||
@@ -1267,7 +1367,7 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
VALUES (${sessionId}, ${chatId}, 'assistant', '', 'streaming', ${task.model}, clock_timestamp())
|
||||||
RETURNING id
|
RETURNING id
|
||||||
`;
|
`;
|
||||||
const assistantId = assistantMsg!.id;
|
assistantId = assistantMsg!.id;
|
||||||
|
|
||||||
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
// write-edit-robustness #4: pre-turn checkpoint of the persistent session
|
||||||
// worktree (best-effort; never breaks dispatch).
|
// worktree (best-effort; never breaks dispatch).
|
||||||
@@ -1376,6 +1476,18 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
|
|
||||||
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
await persistExternalAgentTurn(sql, assistantId, [...toolSnaps.values()], reasoningText);
|
||||||
|
|
||||||
|
// F1: abort short-circuit BEFORE the unconditional 'complete' write — fixes
|
||||||
|
// the warm success-path recording 'complete' on a Stop'd turn. The abort fired
|
||||||
|
// the SDK interrupt on the same query generator only (never killed the warm
|
||||||
|
// process), so the persistent worktree is kept and the backend stays warm.
|
||||||
|
if (ac.signal.aborted || stopping) {
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, 'cancelled', task.model, assistantContent);
|
||||||
|
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
||||||
|
emitAgentStatus(sessionId, chatId, agent, 'idle', stopping ? 'shutdown' : 'cancelled');
|
||||||
|
clearTaskCommands(taskId);
|
||||||
|
return; // worktree persists (no cleanup); backend stays warm
|
||||||
|
}
|
||||||
|
|
||||||
// ctx_used/ctx_max from the SDK result (1M-aware) → the assistant message, so
|
// ctx_used/ctx_max from the SDK result (1M-aware) → the assistant message, so
|
||||||
// the ContextBar renders a real context-window fill for claude.
|
// the ContextBar renders a real context-window fill for claude.
|
||||||
await sql`
|
await sql`
|
||||||
@@ -1391,11 +1503,6 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
model: task.model,
|
model: task.model,
|
||||||
} as WsFrame);
|
} as WsFrame);
|
||||||
|
|
||||||
if (stopping) {
|
|
||||||
await sql`UPDATE tasks SET state = 'cancelled', ended_at = clock_timestamp() WHERE id = ${taskId}`;
|
|
||||||
return; // worktree persists (no cleanup); backend stays warm
|
|
||||||
}
|
|
||||||
|
|
||||||
// Diff the persistent worktree against its captured baseline and SUPERSEDE
|
// Diff the persistent worktree against its captured baseline and SUPERSEDE
|
||||||
// the session's prior pending row (latest-wins) — identical to opencode/ACP.
|
// the session's prior pending row (latest-wins) — identical to opencode/ACP.
|
||||||
const diff = await diffWorktree(worktreePath, projectPath, {
|
const diff = await diffWorktree(worktreePath, projectPath, {
|
||||||
@@ -1442,14 +1549,17 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errMsg = err instanceof Error ? err.message : String(err);
|
const errMsg = err instanceof Error ? err.message : String(err);
|
||||||
|
const status = classifyTerminalStatus({ aborted: ac.signal.aborted, error: err });
|
||||||
log.error({ taskId, agent, err: errMsg }, 'dispatcher: claude SDK error');
|
log.error({ taskId, agent, err: errMsg }, 'dispatcher: claude SDK error');
|
||||||
await sql`
|
await sql`
|
||||||
UPDATE tasks
|
UPDATE tasks
|
||||||
SET state = 'failed', ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
SET state = ${status}, ended_at = clock_timestamp(), output_summary = ${errMsg.slice(0, 500)}
|
||||||
WHERE id = ${taskId}
|
WHERE id = ${taskId} AND state NOT IN ('cancelled', 'completed')
|
||||||
`.catch(() => {});
|
`.catch(() => {});
|
||||||
|
// F1 (OCE-001): finalize the streaming message (was left 'streaming').
|
||||||
|
await finalizeMessage(sessionId, chatId, assistantId, status, task.model);
|
||||||
// #10: turn crashed.
|
// #10: turn crashed.
|
||||||
emitAgentStatus(sessionId, chatId, agent, 'error', 'crashed');
|
emitAgentStatus(sessionId, chatId, agent, status === 'cancelled' ? 'idle' : 'error', status === 'cancelled' ? 'cancelled' : 'crashed');
|
||||||
clearTaskCommands(taskId);
|
clearTaskCommands(taskId);
|
||||||
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
// No worktree cleanup (persistent); backend stays warm for the next turn.
|
||||||
}
|
}
|
||||||
@@ -1476,6 +1586,7 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise<v
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
cancelExternalTask,
|
||||||
start() {
|
start() {
|
||||||
log.info('dispatcher: starting poll loop + tasks_new listener');
|
log.info('dispatcher: starting poll loop + tasks_new listener');
|
||||||
|
|
||||||
|
|||||||
76
apps/coder/src/services/finalize-message.ts
Normal file
76
apps/coder/src/services/finalize-message.ts
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import type { Sql } from '../db.js';
|
||||||
|
import type { WsFrame } from '@boocode/contracts/ws-frames';
|
||||||
|
|
||||||
|
export type TerminalMessageStatus = 'cancelled' | 'failed';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1 (D-7) — decide the terminal status a Stop'd / errored external turn lands in.
|
||||||
|
*
|
||||||
|
* A user Stop (the per-task AbortController fired) or a thrown `AbortError` is a
|
||||||
|
* deliberate, non-error outcome → `'cancelled'`. A genuine thrown error → `'failed'`.
|
||||||
|
* Keeping the two distinct keeps the human-inbox / failure surfaces honest.
|
||||||
|
*
|
||||||
|
* Pure (no DB / IO) so the mapping is unit-testable in isolation.
|
||||||
|
*/
|
||||||
|
export function classifyTerminalStatus(opts: { aborted: boolean; error?: unknown }): TerminalMessageStatus {
|
||||||
|
if (opts.aborted) return 'cancelled';
|
||||||
|
if (opts.error instanceof Error && opts.error.name === 'AbortError') return 'cancelled';
|
||||||
|
return 'failed';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F1 (OCE-001 / OCE-002) — finalize a streaming assistant message into a terminal
|
||||||
|
* state and publish the matching `message_complete` frame.
|
||||||
|
*
|
||||||
|
* Idempotent via `WHERE status = 'streaming'`: a second call (a double-Stop, or an
|
||||||
|
* abort short-circuit followed by the catch block) updates zero rows and does NOT
|
||||||
|
* re-publish, so the frontend reducer settles the message exactly once. It also
|
||||||
|
* never clobbers a row that already finished cleanly (`complete`) — the abort that
|
||||||
|
* raced a clean finish is a no-op.
|
||||||
|
*
|
||||||
|
* Returns `true` iff this call performed the finalization (the row was still
|
||||||
|
* streaming); `false` if it was already terminal or the id is absent (the throw
|
||||||
|
* preceded the row's creation).
|
||||||
|
*/
|
||||||
|
export async function finalizeStreamingMessage(
|
||||||
|
sql: Sql,
|
||||||
|
publishFrame: (sessionId: string, frame: WsFrame) => void,
|
||||||
|
opts: {
|
||||||
|
sessionId: string;
|
||||||
|
chatId: string;
|
||||||
|
assistantId: string;
|
||||||
|
status: TerminalMessageStatus;
|
||||||
|
model: string | null;
|
||||||
|
/** Partial accumulated text to persist; omit to leave the row's content untouched. */
|
||||||
|
content?: string;
|
||||||
|
},
|
||||||
|
): Promise<boolean> {
|
||||||
|
const { sessionId, chatId, assistantId, status, model, content } = opts;
|
||||||
|
if (!assistantId) return false;
|
||||||
|
|
||||||
|
const rows =
|
||||||
|
content !== undefined
|
||||||
|
? await sql<{ id: string }[]>`
|
||||||
|
UPDATE messages
|
||||||
|
SET content = ${content}, status = ${status}, finished_at = clock_timestamp()
|
||||||
|
WHERE id = ${assistantId} AND status = 'streaming'
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
: await sql<{ id: string }[]>`
|
||||||
|
UPDATE messages
|
||||||
|
SET status = ${status}, finished_at = clock_timestamp()
|
||||||
|
WHERE id = ${assistantId} AND status = 'streaming'
|
||||||
|
RETURNING id
|
||||||
|
`;
|
||||||
|
|
||||||
|
if (rows.length === 0) return false;
|
||||||
|
|
||||||
|
publishFrame(sessionId, {
|
||||||
|
type: 'message_complete',
|
||||||
|
message_id: assistantId,
|
||||||
|
chat_id: chatId,
|
||||||
|
model,
|
||||||
|
status,
|
||||||
|
} as WsFrame);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -29,6 +29,17 @@ interface ProjectPathRow {
|
|||||||
path: string;
|
path: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MessageRow {
|
||||||
|
id: string;
|
||||||
|
session_id: string;
|
||||||
|
chat_id: string | null;
|
||||||
|
role: string;
|
||||||
|
content: string;
|
||||||
|
status: string;
|
||||||
|
model: string | null;
|
||||||
|
created_at: Date;
|
||||||
|
}
|
||||||
|
|
||||||
function textResult(data: unknown) {
|
function textResult(data: unknown) {
|
||||||
return { content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }] };
|
return { content: [{ type: 'text' as const, text: JSON.stringify(data, null, 2) }] };
|
||||||
}
|
}
|
||||||
@@ -189,6 +200,56 @@ export async function startMcpServer(sql: Sql): Promise<void> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 6. boocoder.view_session_history
|
||||||
|
server.tool(
|
||||||
|
'boocoder.view_session_history',
|
||||||
|
'Retrieve the most-recent N messages of a session chat transcript (role != system) from messages_with_parts, returned in chronological (oldest→newest) order',
|
||||||
|
{
|
||||||
|
session_id: z.string().describe('Session UUID'),
|
||||||
|
chat_id: z.string().optional().describe('Optional chat UUID — narrows to one chat tab'),
|
||||||
|
limit: z
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.min(1)
|
||||||
|
.max(200)
|
||||||
|
.optional()
|
||||||
|
.describe('Max messages to return (default 50, max 200)'),
|
||||||
|
},
|
||||||
|
async (args) => {
|
||||||
|
const effectiveLimit = Math.min(args.limit ?? 50, 200);
|
||||||
|
let rows: MessageRow[];
|
||||||
|
if (args.chat_id) {
|
||||||
|
rows = await sql<MessageRow[]>`
|
||||||
|
SELECT id, session_id, chat_id, role, content, status, model, created_at
|
||||||
|
FROM (
|
||||||
|
SELECT id, session_id, chat_id, role, content, status, model, created_at
|
||||||
|
FROM messages_with_parts
|
||||||
|
WHERE session_id = ${args.session_id}
|
||||||
|
AND chat_id = ${args.chat_id}
|
||||||
|
AND role != 'system'
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT ${effectiveLimit}
|
||||||
|
) sub
|
||||||
|
ORDER BY created_at ASC
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
rows = await sql<MessageRow[]>`
|
||||||
|
SELECT id, session_id, chat_id, role, content, status, model, created_at
|
||||||
|
FROM (
|
||||||
|
SELECT id, session_id, chat_id, role, content, status, model, created_at
|
||||||
|
FROM messages_with_parts
|
||||||
|
WHERE session_id = ${args.session_id}
|
||||||
|
AND role != 'system'
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT ${effectiveLimit}
|
||||||
|
) sub
|
||||||
|
ORDER BY created_at ASC
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
return textResult({ session_id: args.session_id, count: rows.length, messages: rows });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// Connect via stdio
|
// Connect via stdio
|
||||||
const transport = new StdioServerTransport();
|
const transport = new StdioServerTransport();
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="en" class="dark">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<title>BooCoder</title>
|
|
||||||
</head>
|
|
||||||
<body class="bg-zinc-900 text-zinc-100">
|
|
||||||
<div id="root"></div>
|
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@boocode/coder-web",
|
|
||||||
"version": "2.0.0",
|
|
||||||
"private": true,
|
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
|
||||||
"dev": "vite",
|
|
||||||
"build": "tsc -b && vite build",
|
|
||||||
"typecheck": "tsc -b --noEmit",
|
|
||||||
"preview": "vite preview"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@boocode/contracts": "workspace:*",
|
|
||||||
"lucide-react": "^1.16.0",
|
|
||||||
"react": "^18.3.1",
|
|
||||||
"react-dom": "^18.3.1",
|
|
||||||
"react-markdown": "^10.1.0",
|
|
||||||
"react-router-dom": "^6.26.0",
|
|
||||||
"remark-gfm": "^4.0.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@tailwindcss/postcss": "^4.3.0",
|
|
||||||
"@types/react": "^18.3.3",
|
|
||||||
"@types/react-dom": "^18.3.0",
|
|
||||||
"@vitejs/plugin-react": "^4.3.1",
|
|
||||||
"tailwindcss": "^4.3.0",
|
|
||||||
"typescript": "^5.5.0",
|
|
||||||
"vite": "^5.3.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
export default {
|
|
||||||
plugins: {
|
|
||||||
'@tailwindcss/postcss': {},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
|
||||||
import { Home } from './pages/Home';
|
|
||||||
import { Session } from './pages/Session';
|
|
||||||
|
|
||||||
export function App() {
|
|
||||||
return (
|
|
||||||
<Routes>
|
|
||||||
<Route path="/" element={<Home />} />
|
|
||||||
<Route path="/sessions/:sessionId" element={<Session />} />
|
|
||||||
<Route path="*" element={<Navigate to="/" replace />} />
|
|
||||||
</Routes>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
import type { Project, Session, Chat, Message, PendingChange, AskUserAnswer } from './types';
|
|
||||||
|
|
||||||
export class ApiError extends Error {
|
|
||||||
constructor(
|
|
||||||
public status: number,
|
|
||||||
public body: unknown,
|
|
||||||
) {
|
|
||||||
super(
|
|
||||||
typeof body === 'object' && body && 'error' in body
|
|
||||||
? String((body as { error: unknown }).error)
|
|
||||||
: `HTTP ${status}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
||||||
const res = await fetch(path, {
|
|
||||||
...init,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
...(init.headers ?? {}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (res.status === 204) return undefined as T;
|
|
||||||
const text = await res.text();
|
|
||||||
const data = text ? JSON.parse(text) : undefined;
|
|
||||||
if (!res.ok) throw new ApiError(res.status, data);
|
|
||||||
return data as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const api = {
|
|
||||||
health: () => request<{ ok: boolean; db: boolean; tools: number }>('/api/health'),
|
|
||||||
|
|
||||||
projects: {
|
|
||||||
list: (params?: { status?: 'open' | 'archived' }) =>
|
|
||||||
request<Project[]>(`/api/projects${params?.status ? `?status=${params.status}` : ''}`),
|
|
||||||
},
|
|
||||||
|
|
||||||
sessions: {
|
|
||||||
listForProject: (projectId: string, status?: 'open' | 'archived') =>
|
|
||||||
request<Session[]>(
|
|
||||||
`/api/projects/${projectId}/sessions${status ? `?status=${status}` : ''}`,
|
|
||||||
),
|
|
||||||
get: (id: string) => request<Session>(`/api/sessions/${id}`),
|
|
||||||
},
|
|
||||||
|
|
||||||
chats: {
|
|
||||||
listForSession: (sessionId: string) =>
|
|
||||||
request<Chat[]>(`/api/sessions/${sessionId}/chats`),
|
|
||||||
create: (sessionId: string, body?: { name?: string }) =>
|
|
||||||
request<Chat>(`/api/sessions/${sessionId}/chats`, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(body ?? {}),
|
|
||||||
}),
|
|
||||||
answerUserInput: (chatId: string, toolCallId: string, answers: AskUserAnswer[]) =>
|
|
||||||
request<{ tool_message_id: string; assistant_message_id: string }>(
|
|
||||||
`/api/chats/${chatId}/answer_user_input`,
|
|
||||||
{
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ tool_call_id: toolCallId, answers }),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
},
|
|
||||||
|
|
||||||
messages: {
|
|
||||||
send: (sessionId: string, chatId: string, content: string) =>
|
|
||||||
request<{ user_message_id: string; assistant_message_id: string }>(
|
|
||||||
`/api/sessions/${sessionId}/messages`,
|
|
||||||
{
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ content, chat_id: chatId }),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
stop: (sessionId: string) =>
|
|
||||||
request<{ cancelled: boolean }>(`/api/sessions/${sessionId}/stop`, {
|
|
||||||
method: 'POST',
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
|
|
||||||
pending: {
|
|
||||||
list: (sessionId: string) =>
|
|
||||||
request<PendingChange[]>(`/api/sessions/${sessionId}/pending`),
|
|
||||||
applyAll: (sessionId: string) =>
|
|
||||||
request<{ results: Array<{ id: string; success: boolean; error?: string }> }>(
|
|
||||||
`/api/sessions/${sessionId}/pending/apply`,
|
|
||||||
{ method: 'POST' },
|
|
||||||
),
|
|
||||||
applyOne: (changeId: string) =>
|
|
||||||
request<{ success: boolean; error?: string }>(`/api/pending/${changeId}/apply`, {
|
|
||||||
method: 'POST',
|
|
||||||
}),
|
|
||||||
rejectOne: (changeId: string) =>
|
|
||||||
request<{ ok: boolean }>(`/api/pending/${changeId}/reject`, {
|
|
||||||
method: 'POST',
|
|
||||||
}),
|
|
||||||
rewindOne: (changeId: string) =>
|
|
||||||
request<{ success: boolean; error?: string }>(`/api/pending/${changeId}/rewind`, {
|
|
||||||
method: 'POST',
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
// Minimal types for the BooCoder frontend.
|
|
||||||
// Shared DB entities (same schema as BooChat).
|
|
||||||
//
|
|
||||||
// WS wire contracts are single-sourced from @boocode/contracts (the canonical
|
|
||||||
// Zod-backed schema). The DB entity types below (Project/Session/Chat/Message/
|
|
||||||
// ToolCall/ToolResult/PendingChange) are an intentional minimal SPA-local subset
|
|
||||||
// and are NOT cross-app contracts — they stay defined here.
|
|
||||||
|
|
||||||
import type { WsFrame } from '@boocode/contracts/ws-frames';
|
|
||||||
|
|
||||||
// Re-export the canonical WebSocket frame union (single source of truth). The
|
|
||||||
// coder backend publishes the full frame set; this SPA's reducer handles the
|
|
||||||
// subset it renders and ignores the rest.
|
|
||||||
export type { WsFrame };
|
|
||||||
|
|
||||||
// The error frame's `reason`, single-sourced from the canonical schema's
|
|
||||||
// frame-level reason enum (derived from WsFrame so it cannot drift from the
|
|
||||||
// wire). Distinct from message-metadata's ErrorReason, which is a different set.
|
|
||||||
export type ErrorReason = NonNullable<Extract<WsFrame, { type: 'error' }>['reason']>;
|
|
||||||
|
|
||||||
export interface Project {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
path: string;
|
|
||||||
status: 'open' | 'archived';
|
|
||||||
created_at: string;
|
|
||||||
updated_at: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Session {
|
|
||||||
id: string;
|
|
||||||
project_id: string;
|
|
||||||
name: string | null;
|
|
||||||
model: string | null;
|
|
||||||
status: 'open' | 'archived';
|
|
||||||
created_at: string;
|
|
||||||
updated_at: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Chat {
|
|
||||||
id: string;
|
|
||||||
session_id: string;
|
|
||||||
name: string | null;
|
|
||||||
status: 'open' | 'archived';
|
|
||||||
created_at: string;
|
|
||||||
updated_at: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ToolCall {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
args: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ToolResult {
|
|
||||||
tool_call_id: string;
|
|
||||||
output: unknown;
|
|
||||||
truncated?: boolean;
|
|
||||||
// Canonical wire shape: the failure message string (present only on error),
|
|
||||||
// not a boolean. ToolResultBubble treats it as truthy → renders error styling.
|
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Batch 9.7: ask_user_input shapes. The tool_call.args is { questions: AskUserQuestion[] }
|
|
||||||
// (1-3 entries); the eventual tool_result.output is { answers: AskUserAnswer[] } in the
|
|
||||||
// same order. AskUserInputCard renders questions and POSTs answers.
|
|
||||||
export type AskUserQuestionType = 'single_select' | 'multi_select';
|
|
||||||
|
|
||||||
export interface AskUserQuestion {
|
|
||||||
question: string;
|
|
||||||
type: AskUserQuestionType;
|
|
||||||
options: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AskUserAnswer {
|
|
||||||
question: string;
|
|
||||||
selected_options: string[];
|
|
||||||
free_text: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AskUserAnswerSet {
|
|
||||||
answers: AskUserAnswer[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Message {
|
|
||||||
id: string;
|
|
||||||
session_id: string;
|
|
||||||
chat_id: string;
|
|
||||||
role: 'user' | 'assistant' | 'tool' | 'system';
|
|
||||||
content: string;
|
|
||||||
kind: string;
|
|
||||||
tool_calls: ToolCall[] | null;
|
|
||||||
tool_results: ToolResult | null;
|
|
||||||
status: 'streaming' | 'complete' | 'failed' | 'cancelled';
|
|
||||||
tokens_used: number | null;
|
|
||||||
ctx_used: number | null;
|
|
||||||
ctx_max: number | null;
|
|
||||||
started_at: string | null;
|
|
||||||
finished_at: string | null;
|
|
||||||
created_at: string;
|
|
||||||
metadata: unknown;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PendingChange {
|
|
||||||
id: string;
|
|
||||||
session_id: string;
|
|
||||||
task_id: string | null;
|
|
||||||
file_path: string;
|
|
||||||
operation: 'create' | 'edit' | 'delete';
|
|
||||||
old_string: string | null;
|
|
||||||
new_string: string | null;
|
|
||||||
content: string | null;
|
|
||||||
diff: string | null;
|
|
||||||
status: 'pending' | 'applied' | 'rejected' | 'reverted';
|
|
||||||
created_at: string;
|
|
||||||
applied_at: string | null;
|
|
||||||
}
|
|
||||||
@@ -1,323 +0,0 @@
|
|||||||
import { useMemo, useState } from 'react';
|
|
||||||
import { Check } from 'lucide-react';
|
|
||||||
import { api } from '@/api/client';
|
|
||||||
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
|
||||||
import { Button } from '@/components/ui/button';
|
|
||||||
import type {
|
|
||||||
AskUserAnswer,
|
|
||||||
AskUserAnswerSet,
|
|
||||||
AskUserQuestion,
|
|
||||||
ToolCall,
|
|
||||||
ToolResult,
|
|
||||||
} from '@/api/types';
|
|
||||||
|
|
||||||
// Batch 9.7: Inline interactive picker. Renders inside MessageList in place of
|
|
||||||
// the standard ToolCallLine when the assistant emits an ask_user_input tool
|
|
||||||
// call. While the tool result is null (server pre-stamps a sentinel with
|
|
||||||
// output=null), shows the form; once the WS tool_result frame arrives with a
|
|
||||||
// real AnswerSet, flips to read-only review mode.
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
toolCall: ToolCall;
|
|
||||||
toolResult: ToolResult | null;
|
|
||||||
chatId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseQuestions(raw: unknown): AskUserQuestion[] {
|
|
||||||
if (!raw || typeof raw !== 'object' || !('questions' in raw)) return [];
|
|
||||||
const arr = (raw as { questions: unknown }).questions;
|
|
||||||
if (!Array.isArray(arr)) return [];
|
|
||||||
const out: AskUserQuestion[] = [];
|
|
||||||
for (const item of arr) {
|
|
||||||
if (!item || typeof item !== 'object') continue;
|
|
||||||
const q = item as { question?: unknown; type?: unknown; options?: unknown };
|
|
||||||
if (typeof q.question !== 'string') continue;
|
|
||||||
if (q.type !== 'single_select' && q.type !== 'multi_select') continue;
|
|
||||||
if (!Array.isArray(q.options)) continue;
|
|
||||||
const opts = q.options.filter((o): o is string => typeof o === 'string');
|
|
||||||
if (opts.length < 2) continue;
|
|
||||||
out.push({ question: q.question, type: q.type, options: opts });
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseAnswerSet(raw: unknown): AskUserAnswerSet | null {
|
|
||||||
if (!raw || typeof raw !== 'object' || !('answers' in raw)) return null;
|
|
||||||
const arr = (raw as { answers: unknown }).answers;
|
|
||||||
if (!Array.isArray(arr)) return null;
|
|
||||||
const answers: AskUserAnswer[] = [];
|
|
||||||
for (const item of arr) {
|
|
||||||
if (!item || typeof item !== 'object') continue;
|
|
||||||
const a = item as { question?: unknown; selected_options?: unknown; free_text?: unknown };
|
|
||||||
if (typeof a.question !== 'string') continue;
|
|
||||||
if (!Array.isArray(a.selected_options)) continue;
|
|
||||||
if (a.free_text !== null && typeof a.free_text !== 'string') continue;
|
|
||||||
const sel = a.selected_options.filter((s): s is string => typeof s === 'string');
|
|
||||||
answers.push({
|
|
||||||
question: a.question,
|
|
||||||
selected_options: sel,
|
|
||||||
free_text: (a.free_text as string | null) ?? null,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return { answers };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AskUserInputCard({ toolCall, toolResult, chatId }: Props) {
|
|
||||||
const questions = useMemo(() => parseQuestions(toolCall.args), [toolCall.args]);
|
|
||||||
|
|
||||||
if (questions.length === 0) {
|
|
||||||
return (
|
|
||||||
<div className="rounded border border-destructive/40 bg-destructive/10 text-xs px-3 py-2 text-destructive">
|
|
||||||
ask_user_input: malformed tool args
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tool result with a non-null output means the answer is already submitted.
|
|
||||||
// The pending sentinel uses output=null, so this branch only triggers after
|
|
||||||
// the real WS tool_result frame lands.
|
|
||||||
const answered = toolResult && toolResult.output !== null;
|
|
||||||
if (answered) {
|
|
||||||
const answerSet = parseAnswerSet(toolResult!.output);
|
|
||||||
return <AnsweredView questions={questions} answers={answerSet} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<PendingView questions={questions} toolCallId={toolCall.id} chatId={chatId} />
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function PendingView({
|
|
||||||
questions,
|
|
||||||
toolCallId,
|
|
||||||
chatId,
|
|
||||||
}: {
|
|
||||||
questions: AskUserQuestion[];
|
|
||||||
toolCallId: string;
|
|
||||||
chatId: string;
|
|
||||||
}) {
|
|
||||||
// Per-question selections + free text. Selections are option arrays so the
|
|
||||||
// multi_select case is uniform; single_select just constrains to length 1.
|
|
||||||
const [selections, setSelections] = useState<string[][]>(() => questions.map(() => []));
|
|
||||||
const [freeTexts, setFreeTexts] = useState<string[]>(() => questions.map(() => ''));
|
|
||||||
const [submitting, setSubmitting] = useState(false);
|
|
||||||
|
|
||||||
const singleQuestion = questions.length === 1;
|
|
||||||
const anyFreeText = freeTexts.some((t) => t.trim().length > 0);
|
|
||||||
|
|
||||||
// Submit button shows when:
|
|
||||||
// - more than one question (always batched), OR
|
|
||||||
// - one question and the user has typed free text (committing it needs an
|
|
||||||
// explicit Submit so an accidental Tab/click doesn't lose it).
|
|
||||||
// For one question with no free text, clicking an option submits inline.
|
|
||||||
const showSubmitButton = !singleQuestion || anyFreeText;
|
|
||||||
|
|
||||||
// Every question must have at least one of (option, free text).
|
|
||||||
const allComplete = questions.every((_, i) => {
|
|
||||||
return selections[i]!.length > 0 || freeTexts[i]!.trim().length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
function buildAnswers(): AskUserAnswer[] {
|
|
||||||
return questions.map((q, i) => {
|
|
||||||
const freeText = freeTexts[i]!.trim();
|
|
||||||
return {
|
|
||||||
question: q.question,
|
|
||||||
selected_options: selections[i]!,
|
|
||||||
free_text: freeText.length > 0 ? freeText : null,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function submit(answers: AskUserAnswer[]) {
|
|
||||||
if (submitting) return;
|
|
||||||
setSubmitting(true);
|
|
||||||
try {
|
|
||||||
await api.chats.answerUserInput(chatId, toolCallId, answers);
|
|
||||||
// Card stays mounted; the incoming WS tool_result frame will flip it
|
|
||||||
// into AnsweredView via the parent prop change.
|
|
||||||
} catch (err) {
|
|
||||||
console.error('ask_user_input submit failed:', err instanceof Error ? err.message : err);
|
|
||||||
setSubmitting(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function pickSingle(qIdx: number, option: string) {
|
|
||||||
setSelections((prev) => prev.map((arr, i) => (i === qIdx ? [option] : arr)));
|
|
||||||
// Immediate submit for the single-question single-select shortcut. Only
|
|
||||||
// fires when no free text exists anywhere — once the user typed, the
|
|
||||||
// Submit button takes over so the typed text isn't silently dropped.
|
|
||||||
if (singleQuestion && !anyFreeText) {
|
|
||||||
const answers: AskUserAnswer[] = [
|
|
||||||
{
|
|
||||||
question: questions[0]!.question,
|
|
||||||
selected_options: [option],
|
|
||||||
free_text: null,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
void submit(answers);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleMulti(qIdx: number, option: string) {
|
|
||||||
setSelections((prev) =>
|
|
||||||
prev.map((arr, i) => {
|
|
||||||
if (i !== qIdx) return arr;
|
|
||||||
return arr.includes(option) ? arr.filter((o) => o !== option) : [...arr, option];
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setFreeText(qIdx: number, value: string) {
|
|
||||||
setFreeTexts((prev) => prev.map((t, i) => (i === qIdx ? value : t)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="rounded-lg border bg-muted/20 text-sm">
|
|
||||||
<div className="px-4 py-3 space-y-4">
|
|
||||||
{questions.map((q, i) => (
|
|
||||||
<div key={i} className="space-y-2">
|
|
||||||
{questions.length > 1 && (
|
|
||||||
<div className="text-[10px] uppercase tracking-wide text-muted-foreground/70">
|
|
||||||
Question {i + 1}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className="font-medium leading-snug">{q.question}</div>
|
|
||||||
{q.type === 'single_select' ? (
|
|
||||||
<RadioGroup
|
|
||||||
value={selections[i]![0] ?? ''}
|
|
||||||
onValueChange={(v) => pickSingle(i, v)}
|
|
||||||
disabled={submitting}
|
|
||||||
className="gap-1.5"
|
|
||||||
>
|
|
||||||
{q.options.map((opt, j) => {
|
|
||||||
const id = `q${i}-opt${j}`;
|
|
||||||
return (
|
|
||||||
<label
|
|
||||||
key={j}
|
|
||||||
htmlFor={id}
|
|
||||||
className="flex items-start gap-2 text-sm leading-snug cursor-pointer rounded px-1 py-0.5 hover:bg-muted/40"
|
|
||||||
>
|
|
||||||
<RadioGroupItem id={id} value={opt} className="mt-0.5" />
|
|
||||||
<span>{opt}</span>
|
|
||||||
</label>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</RadioGroup>
|
|
||||||
) : (
|
|
||||||
<div className="grid gap-1.5">
|
|
||||||
{q.options.map((opt, j) => {
|
|
||||||
const id = `q${i}-opt${j}`;
|
|
||||||
const checked = selections[i]!.includes(opt);
|
|
||||||
return (
|
|
||||||
<label
|
|
||||||
key={j}
|
|
||||||
htmlFor={id}
|
|
||||||
className="flex items-start gap-2 text-sm leading-snug cursor-pointer rounded px-1 py-0.5 hover:bg-muted/40"
|
|
||||||
>
|
|
||||||
<input
|
|
||||||
id={id}
|
|
||||||
type="checkbox"
|
|
||||||
checked={checked}
|
|
||||||
disabled={submitting}
|
|
||||||
onChange={() => toggleMulti(i, opt)}
|
|
||||||
className="mt-1 size-3.5 rounded border-input accent-primary"
|
|
||||||
/>
|
|
||||||
<span>{opt}</span>
|
|
||||||
</label>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className="pt-1 space-y-1">
|
|
||||||
<div className="text-[10px] uppercase tracking-wide text-muted-foreground/70">
|
|
||||||
Or type a custom answer
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={freeTexts[i]}
|
|
||||||
disabled={submitting}
|
|
||||||
placeholder="Free text…"
|
|
||||||
onChange={(e) => setFreeText(i, e.target.value)}
|
|
||||||
className="w-full rounded border border-input bg-background px-2 py-1 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring/40 disabled:opacity-60"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
{showSubmitButton && (
|
|
||||||
<div className="flex justify-end gap-2 border-t px-4 py-2">
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
size="sm"
|
|
||||||
disabled={!allComplete || submitting}
|
|
||||||
onClick={() => void submit(buildAnswers())}
|
|
||||||
>
|
|
||||||
{submitting ? 'Submitting…' : 'Submit'}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function AnsweredView({
|
|
||||||
questions,
|
|
||||||
answers,
|
|
||||||
}: {
|
|
||||||
questions: AskUserQuestion[];
|
|
||||||
answers: AskUserAnswerSet | null;
|
|
||||||
}) {
|
|
||||||
if (!answers) {
|
|
||||||
return (
|
|
||||||
<div className="rounded-lg border bg-muted/20 text-xs px-4 py-3 text-muted-foreground">
|
|
||||||
ask_user_input: answers unavailable
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="rounded-lg border bg-muted/10 text-sm">
|
|
||||||
<div className="px-4 py-3 space-y-3">
|
|
||||||
{questions.map((q, i) => {
|
|
||||||
const a = answers.answers[i];
|
|
||||||
if (!a) return null;
|
|
||||||
return (
|
|
||||||
<div key={i} className="space-y-1.5">
|
|
||||||
{questions.length > 1 && (
|
|
||||||
<div className="text-[10px] uppercase tracking-wide text-muted-foreground/70">
|
|
||||||
Question {i + 1}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div className="font-medium leading-snug">{q.question}</div>
|
|
||||||
<div className="space-y-0.5">
|
|
||||||
{q.options.map((opt, j) => {
|
|
||||||
const selected = a.selected_options.includes(opt);
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={j}
|
|
||||||
className={
|
|
||||||
selected
|
|
||||||
? 'flex items-start gap-2 text-sm leading-snug text-foreground'
|
|
||||||
: 'flex items-start gap-2 text-sm leading-snug text-muted-foreground/60 line-through'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<span className="mt-0.5 size-3.5 shrink-0 inline-flex items-center justify-center">
|
|
||||||
{selected && <Check className="size-3 text-primary" />}
|
|
||||||
</span>
|
|
||||||
<span>{opt}</span>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
{a.free_text && (
|
|
||||||
<div className="rounded bg-background border px-2 py-1 text-xs font-mono whitespace-pre-wrap">
|
|
||||||
{a.free_text}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
import { useState, useRef, useEffect } from 'react';
|
|
||||||
import { Send, Square } from 'lucide-react';
|
|
||||||
import type { Message, ToolResult } from '@/api/types';
|
|
||||||
import { api } from '@/api/client';
|
|
||||||
import { MessageBubble } from './MessageBubble';
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
sessionId: string;
|
|
||||||
chatId: string;
|
|
||||||
messages: Message[];
|
|
||||||
isStreaming: boolean;
|
|
||||||
connected: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ChatPane({ sessionId, chatId, messages, isStreaming, connected }: Props) {
|
|
||||||
const [input, setInput] = useState('');
|
|
||||||
const [sending, setSending] = useState(false);
|
|
||||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
|
||||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
||||||
|
|
||||||
// Auto-scroll to bottom when messages change
|
|
||||||
useEffect(() => {
|
|
||||||
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
||||||
}, [messages]);
|
|
||||||
|
|
||||||
// Auto-resize textarea
|
|
||||||
useEffect(() => {
|
|
||||||
const el = textareaRef.current;
|
|
||||||
if (!el) return;
|
|
||||||
el.style.height = 'auto';
|
|
||||||
el.style.height = Math.min(el.scrollHeight, 200) + 'px';
|
|
||||||
}, [input]);
|
|
||||||
|
|
||||||
const handleSend = async () => {
|
|
||||||
const content = input.trim();
|
|
||||||
if (!content || sending || isStreaming) return;
|
|
||||||
|
|
||||||
setInput('');
|
|
||||||
setSending(true);
|
|
||||||
try {
|
|
||||||
await api.messages.send(sessionId, chatId, content);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('send failed:', err);
|
|
||||||
// Restore input on failure
|
|
||||||
setInput(content);
|
|
||||||
} finally {
|
|
||||||
setSending(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleStop = async () => {
|
|
||||||
try {
|
|
||||||
await api.messages.stop(sessionId);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('stop failed:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
|
||||||
e.preventDefault();
|
|
||||||
handleSend();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Filter out system messages for display (sentinels)
|
|
||||||
const visibleMessages = messages.filter((m) => m.role !== 'system');
|
|
||||||
|
|
||||||
// Build a lookup map from tool_call_id -> ToolResult for all messages
|
|
||||||
const toolResultsMap: Record<string, ToolResult> = {};
|
|
||||||
for (const msg of messages) {
|
|
||||||
if (msg.tool_results) {
|
|
||||||
toolResultsMap[msg.tool_results.tool_call_id] = msg.tool_results;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col h-full">
|
|
||||||
{/* Connection indicator */}
|
|
||||||
<div className="flex items-center gap-2 px-4 py-2 border-b border-zinc-800 text-xs text-zinc-500">
|
|
||||||
<div
|
|
||||||
className={`w-1.5 h-1.5 rounded-full ${connected ? 'bg-green-500' : 'bg-red-500'}`}
|
|
||||||
/>
|
|
||||||
<span>{connected ? 'Connected' : 'Disconnected'}</span>
|
|
||||||
{isStreaming && (
|
|
||||||
<span className="text-blue-400 ml-auto">Generating...</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Messages list */}
|
|
||||||
<div className="flex-1 overflow-y-auto px-4 py-4">
|
|
||||||
{visibleMessages.length === 0 && (
|
|
||||||
<div className="text-center text-zinc-500 mt-8">
|
|
||||||
<p className="text-lg font-medium">BooCoder</p>
|
|
||||||
<p className="text-sm mt-1">Send a message to start coding.</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{visibleMessages.map((msg) => (
|
|
||||||
<MessageBubble key={msg.id} message={msg} chatId={msg.chat_id} toolResultsMap={toolResultsMap} />
|
|
||||||
))}
|
|
||||||
<div ref={messagesEndRef} />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Input area */}
|
|
||||||
<div className="border-t border-zinc-800 px-4 py-3">
|
|
||||||
<div className="flex items-end gap-2">
|
|
||||||
<textarea
|
|
||||||
ref={textareaRef}
|
|
||||||
value={input}
|
|
||||||
onChange={(e) => setInput(e.target.value)}
|
|
||||||
onKeyDown={handleKeyDown}
|
|
||||||
placeholder="Message BooCoder..."
|
|
||||||
rows={1}
|
|
||||||
className="flex-1 bg-zinc-800 border border-zinc-700 rounded-lg px-3 py-2 text-sm text-zinc-100 placeholder-zinc-500 resize-none focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
|
|
||||||
disabled={sending}
|
|
||||||
/>
|
|
||||||
{isStreaming ? (
|
|
||||||
<button
|
|
||||||
onClick={handleStop}
|
|
||||||
className="p-2 rounded-lg bg-red-600 hover:bg-red-500 text-white transition-colors"
|
|
||||||
title="Stop generation"
|
|
||||||
>
|
|
||||||
<Square size={18} />
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<button
|
|
||||||
onClick={handleSend}
|
|
||||||
disabled={!input.trim() || sending}
|
|
||||||
className="p-2 rounded-lg bg-blue-600 hover:bg-blue-500 disabled:opacity-40 disabled:cursor-not-allowed text-white transition-colors"
|
|
||||||
title="Send message"
|
|
||||||
>
|
|
||||||
<Send size={18} />
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,337 +0,0 @@
|
|||||||
import { useState, useEffect, useCallback } from 'react';
|
|
||||||
import { Check, X, RotateCcw, FileText, FilePlus, Trash2, RefreshCw } from 'lucide-react';
|
|
||||||
import type { PendingChange } from '@/api/types';
|
|
||||||
import { api } from '@/api/client';
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
sessionId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function DiffPane({ sessionId }: Props) {
|
|
||||||
const [changes, setChanges] = useState<PendingChange[]>([]);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [expandedId, setExpandedId] = useState<string | null>(null);
|
|
||||||
|
|
||||||
const fetchPending = useCallback(async () => {
|
|
||||||
try {
|
|
||||||
const result = await api.pending.list(sessionId);
|
|
||||||
setChanges(result);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('fetch pending failed:', err);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}, [sessionId]);
|
|
||||||
|
|
||||||
// Initial load. Pending changes are delivered over HTTP (list + apply/reject/
|
|
||||||
// rewind below); there is no WS pending-change frame, so the list refreshes on
|
|
||||||
// mount, on the Refresh button, and optimistically as the user acts on it.
|
|
||||||
useEffect(() => {
|
|
||||||
fetchPending();
|
|
||||||
}, [fetchPending]);
|
|
||||||
|
|
||||||
const pendingChanges = changes.filter((c) => c.status === 'pending');
|
|
||||||
const resolvedChanges = changes.filter((c) => c.status !== 'pending');
|
|
||||||
|
|
||||||
const handleApplyOne = async (id: string) => {
|
|
||||||
try {
|
|
||||||
await api.pending.applyOne(id);
|
|
||||||
setChanges((prev) =>
|
|
||||||
prev.map((c) => (c.id === id ? { ...c, status: 'applied' as const } : c)),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('apply failed:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRejectOne = async (id: string) => {
|
|
||||||
try {
|
|
||||||
await api.pending.rejectOne(id);
|
|
||||||
setChanges((prev) =>
|
|
||||||
prev.map((c) => (c.id === id ? { ...c, status: 'rejected' as const } : c)),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('reject failed:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRewindOne = async (id: string) => {
|
|
||||||
try {
|
|
||||||
await api.pending.rewindOne(id);
|
|
||||||
setChanges((prev) =>
|
|
||||||
prev.map((c) => (c.id === id ? { ...c, status: 'reverted' as const } : c)),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('rewind failed:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleApplyAll = async () => {
|
|
||||||
try {
|
|
||||||
const result = await api.pending.applyAll(sessionId);
|
|
||||||
const appliedIds = new Set(
|
|
||||||
result.results.filter((r) => r.success).map((r) => r.id),
|
|
||||||
);
|
|
||||||
setChanges((prev) =>
|
|
||||||
prev.map((c) =>
|
|
||||||
appliedIds.has(c.id) ? { ...c, status: 'applied' as const } : c,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('apply all failed:', err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRejectAll = async () => {
|
|
||||||
// Reject each pending change individually (no batch reject endpoint)
|
|
||||||
for (const c of pendingChanges) {
|
|
||||||
await handleRejectOne(c.id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const OpIcon = ({ op }: { op: PendingChange['operation'] }) => {
|
|
||||||
switch (op) {
|
|
||||||
case 'create':
|
|
||||||
return <FilePlus size={14} className="text-green-400" />;
|
|
||||||
case 'edit':
|
|
||||||
return <FileText size={14} className="text-blue-400" />;
|
|
||||||
case 'delete':
|
|
||||||
return <Trash2 size={14} className="text-red-400" />;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const StatusBadge = ({ status }: { status: PendingChange['status'] }) => {
|
|
||||||
const colors: Record<PendingChange['status'], string> = {
|
|
||||||
pending: 'bg-yellow-500/20 text-yellow-400',
|
|
||||||
applied: 'bg-green-500/20 text-green-400',
|
|
||||||
rejected: 'bg-zinc-500/20 text-zinc-400',
|
|
||||||
reverted: 'bg-orange-500/20 text-orange-400',
|
|
||||||
};
|
|
||||||
return (
|
|
||||||
<span className={`text-[10px] px-1.5 py-0.5 rounded ${colors[status]}`}>
|
|
||||||
{status}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col h-full">
|
|
||||||
{/* Header */}
|
|
||||||
<div className="flex items-center justify-between px-4 py-2 border-b border-zinc-800">
|
|
||||||
<h2 className="text-sm font-medium text-zinc-300">
|
|
||||||
Pending Changes
|
|
||||||
{pendingChanges.length > 0 && (
|
|
||||||
<span className="ml-1.5 text-xs text-zinc-500">
|
|
||||||
({pendingChanges.length})
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</h2>
|
|
||||||
<div className="flex items-center gap-1">
|
|
||||||
<button
|
|
||||||
onClick={fetchPending}
|
|
||||||
className="p-1 rounded hover:bg-zinc-800 text-zinc-400 hover:text-zinc-200"
|
|
||||||
title="Refresh"
|
|
||||||
>
|
|
||||||
<RefreshCw size={14} />
|
|
||||||
</button>
|
|
||||||
{pendingChanges.length > 0 && (
|
|
||||||
<>
|
|
||||||
<button
|
|
||||||
onClick={handleApplyAll}
|
|
||||||
className="text-xs px-2 py-1 rounded bg-green-600/80 hover:bg-green-600 text-white"
|
|
||||||
>
|
|
||||||
Apply All
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={handleRejectAll}
|
|
||||||
className="text-xs px-2 py-1 rounded bg-zinc-700 hover:bg-zinc-600 text-zinc-300"
|
|
||||||
>
|
|
||||||
Reject All
|
|
||||||
</button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Changes list */}
|
|
||||||
<div className="flex-1 overflow-y-auto">
|
|
||||||
{loading && (
|
|
||||||
<div className="text-center text-zinc-500 text-sm py-8">Loading...</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{!loading && changes.length === 0 && (
|
|
||||||
<div className="text-center text-zinc-500 text-sm py-8">
|
|
||||||
No pending changes yet.
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Pending changes first */}
|
|
||||||
{pendingChanges.map((change) => (
|
|
||||||
<ChangeItem
|
|
||||||
key={change.id}
|
|
||||||
change={change}
|
|
||||||
expanded={expandedId === change.id}
|
|
||||||
onToggle={() =>
|
|
||||||
setExpandedId((prev) => (prev === change.id ? null : change.id))
|
|
||||||
}
|
|
||||||
onApply={() => handleApplyOne(change.id)}
|
|
||||||
onReject={() => handleRejectOne(change.id)}
|
|
||||||
OpIcon={OpIcon}
|
|
||||||
StatusBadge={StatusBadge}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
|
|
||||||
{/* Resolved changes */}
|
|
||||||
{resolvedChanges.length > 0 && pendingChanges.length > 0 && (
|
|
||||||
<div className="border-t border-zinc-800 my-1" />
|
|
||||||
)}
|
|
||||||
{resolvedChanges.map((change) => (
|
|
||||||
<ChangeItem
|
|
||||||
key={change.id}
|
|
||||||
change={change}
|
|
||||||
expanded={expandedId === change.id}
|
|
||||||
onToggle={() =>
|
|
||||||
setExpandedId((prev) => (prev === change.id ? null : change.id))
|
|
||||||
}
|
|
||||||
onRewind={
|
|
||||||
change.status === 'applied'
|
|
||||||
? () => handleRewindOne(change.id)
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
OpIcon={OpIcon}
|
|
||||||
StatusBadge={StatusBadge}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ChangeItemProps {
|
|
||||||
change: PendingChange;
|
|
||||||
expanded: boolean;
|
|
||||||
onToggle: () => void;
|
|
||||||
onApply?: () => void;
|
|
||||||
onReject?: () => void;
|
|
||||||
onRewind?: () => void;
|
|
||||||
OpIcon: React.ComponentType<{ op: PendingChange['operation'] }>;
|
|
||||||
StatusBadge: React.ComponentType<{ status: PendingChange['status'] }>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function ChangeItem({
|
|
||||||
change,
|
|
||||||
expanded,
|
|
||||||
onToggle,
|
|
||||||
onApply,
|
|
||||||
onReject,
|
|
||||||
onRewind,
|
|
||||||
OpIcon,
|
|
||||||
StatusBadge,
|
|
||||||
}: ChangeItemProps) {
|
|
||||||
const fileName = change.file_path.split('/').pop() || change.file_path;
|
|
||||||
const dirPath = change.file_path.split('/').slice(0, -1).join('/');
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="border-b border-zinc-800/50">
|
|
||||||
<div
|
|
||||||
className="flex items-center gap-2 px-4 py-2 hover:bg-zinc-800/50 cursor-pointer"
|
|
||||||
onClick={onToggle}
|
|
||||||
>
|
|
||||||
<OpIcon op={change.operation} />
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<span className="text-sm font-mono text-zinc-200 truncate block">
|
|
||||||
{fileName}
|
|
||||||
</span>
|
|
||||||
{dirPath && (
|
|
||||||
<span className="text-[11px] text-zinc-500 truncate block">
|
|
||||||
{dirPath}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<StatusBadge status={change.status} />
|
|
||||||
{change.status === 'pending' && (
|
|
||||||
<div className="flex items-center gap-1 ml-1">
|
|
||||||
<button
|
|
||||||
onClick={(e) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
onApply?.();
|
|
||||||
}}
|
|
||||||
className="p-1 rounded hover:bg-green-600/30 text-green-400"
|
|
||||||
title="Apply"
|
|
||||||
>
|
|
||||||
<Check size={14} />
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={(e) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
onReject?.();
|
|
||||||
}}
|
|
||||||
className="p-1 rounded hover:bg-red-600/30 text-red-400"
|
|
||||||
title="Reject"
|
|
||||||
>
|
|
||||||
<X size={14} />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{change.status === 'applied' && onRewind && (
|
|
||||||
<button
|
|
||||||
onClick={(e) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
onRewind();
|
|
||||||
}}
|
|
||||||
className="p-1 rounded hover:bg-orange-600/30 text-orange-400"
|
|
||||||
title="Rewind"
|
|
||||||
>
|
|
||||||
<RotateCcw size={14} />
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{expanded && (
|
|
||||||
<div className="px-4 pb-3">
|
|
||||||
{change.operation === 'edit' && (
|
|
||||||
<div className="space-y-2">
|
|
||||||
{change.old_string && (
|
|
||||||
<div className="rounded bg-red-950/30 border border-red-900/30 p-2">
|
|
||||||
<div className="text-[10px] text-red-400 mb-1 font-medium">
|
|
||||||
Remove
|
|
||||||
</div>
|
|
||||||
<pre className="text-xs text-red-200 whitespace-pre-wrap break-all font-mono">
|
|
||||||
{change.old_string}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{change.new_string && (
|
|
||||||
<div className="rounded bg-green-950/30 border border-green-900/30 p-2">
|
|
||||||
<div className="text-[10px] text-green-400 mb-1 font-medium">
|
|
||||||
Add
|
|
||||||
</div>
|
|
||||||
<pre className="text-xs text-green-200 whitespace-pre-wrap break-all font-mono">
|
|
||||||
{change.new_string}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{change.operation === 'create' && change.content && (
|
|
||||||
<div className="rounded bg-green-950/30 border border-green-900/30 p-2">
|
|
||||||
<div className="text-[10px] text-green-400 mb-1 font-medium">
|
|
||||||
New file
|
|
||||||
</div>
|
|
||||||
<pre className="text-xs text-green-200 whitespace-pre-wrap break-all font-mono max-h-60 overflow-y-auto">
|
|
||||||
{change.content.length > 2000
|
|
||||||
? change.content.slice(0, 2000) + '\n... (truncated)'
|
|
||||||
: change.content}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{change.operation === 'delete' && (
|
|
||||||
<div className="rounded bg-red-950/30 border border-red-900/30 p-2 text-xs text-red-300">
|
|
||||||
This file will be deleted.
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
import { useState } from 'react';
|
|
||||||
import { Code2, MessageSquare, GitPullRequest } from 'lucide-react';
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
chatPane: React.ReactNode;
|
|
||||||
diffPane: React.ReactNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Layout({ chatPane, diffPane }: Props) {
|
|
||||||
const [activeTab, setActiveTab] = useState<'chat' | 'diff'>('chat');
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col h-screen bg-zinc-900">
|
|
||||||
{/* Top bar */}
|
|
||||||
<header className="flex items-center gap-3 px-4 py-2 border-b border-zinc-800 bg-zinc-900/95">
|
|
||||||
<Code2 size={20} className="text-blue-400" />
|
|
||||||
<h1 className="text-sm font-semibold text-zinc-200">BooCoder</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
{/* Mobile tab bar (visible below lg breakpoint) */}
|
|
||||||
<div className="lg:hidden flex border-b border-zinc-800">
|
|
||||||
<button
|
|
||||||
onClick={() => setActiveTab('chat')}
|
|
||||||
className={`flex-1 flex items-center justify-center gap-1.5 py-2 text-sm ${
|
|
||||||
activeTab === 'chat'
|
|
||||||
? 'text-blue-400 border-b-2 border-blue-400'
|
|
||||||
: 'text-zinc-500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<MessageSquare size={14} />
|
|
||||||
Chat
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
onClick={() => setActiveTab('diff')}
|
|
||||||
className={`flex-1 flex items-center justify-center gap-1.5 py-2 text-sm ${
|
|
||||||
activeTab === 'diff'
|
|
||||||
? 'text-blue-400 border-b-2 border-blue-400'
|
|
||||||
: 'text-zinc-500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<GitPullRequest size={14} />
|
|
||||||
Changes
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Desktop split layout */}
|
|
||||||
<div className="flex-1 hidden lg:flex overflow-hidden">
|
|
||||||
<div className="w-[60%] border-r border-zinc-800 overflow-hidden">
|
|
||||||
{chatPane}
|
|
||||||
</div>
|
|
||||||
<div className="w-[40%] overflow-hidden">
|
|
||||||
{diffPane}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Mobile: show only the active tab */}
|
|
||||||
<div className="flex-1 lg:hidden overflow-hidden">
|
|
||||||
{activeTab === 'chat' ? chatPane : diffPane}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
import Markdown from 'react-markdown';
|
|
||||||
import remarkGfm from 'remark-gfm';
|
|
||||||
import type { Message, ToolResult } from '@/api/types';
|
|
||||||
import { Wrench, AlertCircle, Loader2 } from 'lucide-react';
|
|
||||||
import { AskUserInputCard } from './AskUserInputCard';
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
message: Message;
|
|
||||||
chatId: string;
|
|
||||||
toolResultsMap: Record<string, ToolResult>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function MessageBubble({ message, chatId }: Props) {
|
|
||||||
if (message.role === 'tool') {
|
|
||||||
return <ToolResultBubble message={message} />;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isUser = message.role === 'user';
|
|
||||||
const isStreaming = message.status === 'streaming';
|
|
||||||
const isFailed = message.status === 'failed';
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className={`flex ${isUser ? 'justify-end' : 'justify-start'} mb-3`}>
|
|
||||||
<div
|
|
||||||
className={`max-w-[85%] rounded-lg px-4 py-2.5 ${
|
|
||||||
isUser
|
|
||||||
? 'bg-blue-600 text-white'
|
|
||||||
: 'bg-zinc-800 text-zinc-100 border border-zinc-700'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{isFailed && (
|
|
||||||
<div className="flex items-center gap-1.5 text-red-400 text-xs mb-1">
|
|
||||||
<AlertCircle size={12} />
|
|
||||||
<span>Failed</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{message.tool_calls && message.tool_calls.length > 0 && (
|
|
||||||
<div className="mb-2 space-y-1">
|
|
||||||
{message.tool_calls.map((tc) => {
|
|
||||||
if (tc.name === 'ask_user_input') {
|
|
||||||
const result = message.tool_results ?? null;
|
|
||||||
return (
|
|
||||||
<AskUserInputCard
|
|
||||||
key={tc.id}
|
|
||||||
toolCall={tc}
|
|
||||||
toolResult={result}
|
|
||||||
chatId={chatId}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={tc.id}
|
|
||||||
className="flex items-center gap-1.5 text-xs text-zinc-400 bg-zinc-900/50 rounded px-2 py-1"
|
|
||||||
>
|
|
||||||
<Wrench size={11} />
|
|
||||||
<span className="font-mono">{tc.name}</span>
|
|
||||||
<span className="text-zinc-500 truncate max-w-[200px]">
|
|
||||||
{truncateArgs(tc.args)}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{message.content.trim() && (
|
|
||||||
<div className="prose prose-invert prose-sm max-w-none [&_pre]:bg-zinc-900 [&_pre]:p-3 [&_pre]:rounded [&_pre]:overflow-x-auto [&_code]:text-zinc-300 [&_p]:my-1.5">
|
|
||||||
<Markdown remarkPlugins={[remarkGfm]}>{message.content}</Markdown>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{isStreaming && !message.content.trim() && (
|
|
||||||
<div className="flex items-center gap-1.5 text-zinc-400">
|
|
||||||
<Loader2 size={14} className="animate-spin" />
|
|
||||||
<span className="text-xs">Thinking...</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{isStreaming && message.content.trim() && (
|
|
||||||
<span className="inline-block w-1.5 h-4 bg-zinc-400 animate-pulse ml-0.5 align-middle" />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function ToolResultBubble({ message }: { message: Message }) {
|
|
||||||
const result = message.tool_results;
|
|
||||||
if (!result) return null;
|
|
||||||
|
|
||||||
const isError = result.error;
|
|
||||||
const output = result.output != null ? String(result.output) : '';
|
|
||||||
const displayOutput =
|
|
||||||
output.length > 300 ? output.slice(0, 300) + '...' : output;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex justify-start mb-2 ml-6">
|
|
||||||
<div
|
|
||||||
className={`max-w-[80%] rounded px-3 py-2 text-xs font-mono border ${
|
|
||||||
isError
|
|
||||||
? 'bg-red-950/30 border-red-800/50 text-red-300'
|
|
||||||
: 'bg-zinc-800/50 border-zinc-700/50 text-zinc-400'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{result.truncated && (
|
|
||||||
<span className="text-yellow-500 text-[10px] block mb-1">
|
|
||||||
[truncated]
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
<pre className="whitespace-pre-wrap break-all">{displayOutput}</pre>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function truncateArgs(args: unknown): string {
|
|
||||||
if (!args) return '';
|
|
||||||
try {
|
|
||||||
if (typeof args === 'object' && args !== null) {
|
|
||||||
const obj = args as Record<string, unknown>;
|
|
||||||
const keys = Object.keys(obj);
|
|
||||||
if (keys.length === 0) return '';
|
|
||||||
const first = keys[0]!;
|
|
||||||
const val = String(obj[first] ?? '');
|
|
||||||
const display = val.length > 40 ? val.slice(0, 40) + '...' : val;
|
|
||||||
return `${first}: ${display}`;
|
|
||||||
}
|
|
||||||
const str = String(args);
|
|
||||||
return str.length > 50 ? str.slice(0, 50) + '...' : str;
|
|
||||||
} catch {
|
|
||||||
return String(args).length > 50 ? String(args).slice(0, 50) + '...' : String(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
import * as React from 'react';
|
|
||||||
|
|
||||||
export interface ButtonProps
|
|
||||||
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
||||||
variant?: 'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link';
|
|
||||||
size?: 'default' | 'sm' | 'lg' | 'icon';
|
|
||||||
}
|
|
||||||
|
|
||||||
const variantClasses: Record<string, string> = {
|
|
||||||
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
|
|
||||||
destructive: 'bg-destructive text-destructive-foreground hover:bg-destructive/90',
|
|
||||||
outline: 'border border-input bg-background hover:bg-accent hover:text-accent-foreground',
|
|
||||||
secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
|
|
||||||
ghost: 'hover:bg-accent hover:text-accent-foreground',
|
|
||||||
link: 'text-primary underline-offset-4 hover:underline',
|
|
||||||
};
|
|
||||||
|
|
||||||
const sizeClasses: Record<string, string> = {
|
|
||||||
default: 'h-9 px-4 py-2',
|
|
||||||
sm: 'h-8 rounded-md px-3 text-xs',
|
|
||||||
lg: 'h-10 rounded-md px-8',
|
|
||||||
icon: 'h-9 w-9',
|
|
||||||
};
|
|
||||||
|
|
||||||
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|
||||||
({ className, variant = 'default', size = 'default', ...props }, ref) => {
|
|
||||||
const base =
|
|
||||||
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/40 disabled:pointer-events-none disabled:opacity-60';
|
|
||||||
const cls = [base, variantClasses[variant] ?? '', sizeClasses[size] ?? '', className ?? ''].join(' ');
|
|
||||||
return <button className={cls} ref={ref} {...props} />;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
Button.displayName = 'Button';
|
|
||||||
|
|
||||||
export { Button };
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
import * as React from 'react';
|
|
||||||
|
|
||||||
const RadioGroupContext = React.createContext<{
|
|
||||||
value: string | undefined;
|
|
||||||
onValueChange: (v: string) => void;
|
|
||||||
disabled?: boolean;
|
|
||||||
} | null>(null);
|
|
||||||
|
|
||||||
interface RadioGroupProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
||||||
value?: string;
|
|
||||||
onValueChange?: (value: string) => void;
|
|
||||||
disabled?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>(
|
|
||||||
({ className, value, onValueChange, disabled, ...props }, ref) => {
|
|
||||||
const ctx = React.useMemo(() => ({ value, onValueChange: onValueChange ?? (() => {}), disabled }), [value, onValueChange, disabled]);
|
|
||||||
return (
|
|
||||||
<RadioGroupContext.Provider value={ctx}>
|
|
||||||
<div
|
|
||||||
ref={ref}
|
|
||||||
role="radiogroup"
|
|
||||||
className={className}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
</RadioGroupContext.Provider>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
RadioGroup.displayName = 'RadioGroup';
|
|
||||||
|
|
||||||
interface RadioGroupItemProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
||||||
value: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const RadioGroupItem = React.forwardRef<HTMLInputElement, RadioGroupItemProps>(
|
|
||||||
({ className, value, ...props }, ref) => {
|
|
||||||
const ctx = React.useContext(RadioGroupContext);
|
|
||||||
if (!ctx) return <input ref={ref} type="radio" className={className} value={value} {...props} />;
|
|
||||||
const checked = ctx.value === value;
|
|
||||||
return (
|
|
||||||
<input
|
|
||||||
ref={ref}
|
|
||||||
type="radio"
|
|
||||||
checked={checked}
|
|
||||||
disabled={ctx.disabled}
|
|
||||||
onChange={() => ctx.onValueChange(value)}
|
|
||||||
className={className}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
RadioGroupItem.displayName = 'RadioGroupItem';
|
|
||||||
|
|
||||||
export { RadioGroup, RadioGroupItem };
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
@import "tailwindcss";
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
min-height: 100vh;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Scrollbar styling for dark theme */
|
|
||||||
::-webkit-scrollbar {
|
|
||||||
width: 8px;
|
|
||||||
height: 8px;
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-track {
|
|
||||||
background: transparent;
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-thumb {
|
|
||||||
background: #3f3f46;
|
|
||||||
border-radius: 4px;
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-thumb:hover {
|
|
||||||
background: #52525b;
|
|
||||||
}
|
|
||||||
@@ -1,217 +0,0 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
|
||||||
import type { Message, WsFrame } from '@/api/types';
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
messages: Message[];
|
|
||||||
connected: boolean;
|
|
||||||
error: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyFrame(state: State, frame: WsFrame): State {
|
|
||||||
switch (frame.type) {
|
|
||||||
case 'snapshot': {
|
|
||||||
// Canonical SnapshotFrame.messages is opaque (z.array(z.unknown())); the
|
|
||||||
// coder backend sends Message-shaped rows, so cast to the SPA's local type.
|
|
||||||
return { ...state, messages: frame.messages as Message[] };
|
|
||||||
}
|
|
||||||
case 'message_started': {
|
|
||||||
const exists = state.messages.some((m) => m.id === frame.message_id);
|
|
||||||
if (exists) return state;
|
|
||||||
const newMsg: Message = {
|
|
||||||
id: frame.message_id,
|
|
||||||
session_id: '',
|
|
||||||
chat_id: frame.chat_id ?? '',
|
|
||||||
role: frame.role,
|
|
||||||
content: '',
|
|
||||||
kind: 'message',
|
|
||||||
tool_calls: null,
|
|
||||||
tool_results: null,
|
|
||||||
status: frame.role === 'system' ? 'complete' : 'streaming',
|
|
||||||
tokens_used: null,
|
|
||||||
ctx_used: null,
|
|
||||||
ctx_max: null,
|
|
||||||
started_at: null,
|
|
||||||
finished_at: null,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
metadata: null,
|
|
||||||
};
|
|
||||||
return { ...state, messages: [...state.messages, newMsg] };
|
|
||||||
}
|
|
||||||
case 'delta': {
|
|
||||||
const next = state.messages.map((m) =>
|
|
||||||
m.id === frame.message_id ? { ...m, content: m.content + frame.content } : m,
|
|
||||||
);
|
|
||||||
return { ...state, messages: next };
|
|
||||||
}
|
|
||||||
case 'tool_call': {
|
|
||||||
const next = state.messages.map((m) =>
|
|
||||||
m.id === frame.message_id
|
|
||||||
? { ...m, tool_calls: [...(m.tool_calls ?? []), frame.tool_call] }
|
|
||||||
: m,
|
|
||||||
);
|
|
||||||
return { ...state, messages: next };
|
|
||||||
}
|
|
||||||
case 'tool_result': {
|
|
||||||
const exists = state.messages.some((m) => m.id === frame.tool_message_id);
|
|
||||||
if (exists) {
|
|
||||||
const next = state.messages.map((m) =>
|
|
||||||
m.id === frame.tool_message_id
|
|
||||||
? {
|
|
||||||
...m,
|
|
||||||
role: 'tool' as const,
|
|
||||||
tool_results: {
|
|
||||||
tool_call_id: frame.tool_call_id,
|
|
||||||
output: frame.output,
|
|
||||||
truncated: frame.truncated,
|
|
||||||
...(frame.error ? { error: frame.error } : {}),
|
|
||||||
},
|
|
||||||
status: 'complete' as const,
|
|
||||||
}
|
|
||||||
: m,
|
|
||||||
);
|
|
||||||
return { ...state, messages: next };
|
|
||||||
}
|
|
||||||
const newMsg: Message = {
|
|
||||||
id: frame.tool_message_id,
|
|
||||||
session_id: '',
|
|
||||||
chat_id: frame.chat_id ?? '',
|
|
||||||
role: 'tool',
|
|
||||||
content: '',
|
|
||||||
kind: 'message',
|
|
||||||
tool_calls: null,
|
|
||||||
tool_results: {
|
|
||||||
tool_call_id: frame.tool_call_id,
|
|
||||||
output: frame.output,
|
|
||||||
truncated: frame.truncated,
|
|
||||||
...(frame.error ? { error: frame.error } : {}),
|
|
||||||
},
|
|
||||||
status: 'complete',
|
|
||||||
tokens_used: null,
|
|
||||||
ctx_used: null,
|
|
||||||
ctx_max: null,
|
|
||||||
started_at: null,
|
|
||||||
finished_at: null,
|
|
||||||
created_at: new Date().toISOString(),
|
|
||||||
metadata: null,
|
|
||||||
};
|
|
||||||
return { ...state, messages: [...state.messages, newMsg] };
|
|
||||||
}
|
|
||||||
case 'message_complete': {
|
|
||||||
const next = state.messages.map((m) =>
|
|
||||||
m.id === frame.message_id
|
|
||||||
? {
|
|
||||||
...m,
|
|
||||||
status: 'complete' as const,
|
|
||||||
...(frame.tokens_used !== undefined ? { tokens_used: frame.tokens_used } : {}),
|
|
||||||
...(frame.ctx_used !== undefined ? { ctx_used: frame.ctx_used } : {}),
|
|
||||||
...(frame.ctx_max !== undefined ? { ctx_max: frame.ctx_max } : {}),
|
|
||||||
...(frame.started_at !== undefined ? { started_at: frame.started_at } : {}),
|
|
||||||
...(frame.finished_at !== undefined ? { finished_at: frame.finished_at } : {}),
|
|
||||||
...(frame.metadata !== undefined ? { metadata: frame.metadata } : {}),
|
|
||||||
}
|
|
||||||
: m,
|
|
||||||
);
|
|
||||||
return { ...state, messages: next };
|
|
||||||
}
|
|
||||||
case 'error': {
|
|
||||||
const next = frame.message_id
|
|
||||||
? state.messages.map((m) =>
|
|
||||||
m.id === frame.message_id ? { ...m, status: 'failed' as const } : m,
|
|
||||||
)
|
|
||||||
: state.messages;
|
|
||||||
return { ...state, messages: next, error: frame.error };
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// The canonical WsFrame carries the full set of frames the coder backend
|
|
||||||
// can publish; this SPA only renders the subset handled above and safely
|
|
||||||
// ignores the rest (reasoning_delta, usage, permission_*, agent_*, and the
|
|
||||||
// per-user sidebar frames). pending_change_* frames have no publisher —
|
|
||||||
// pending changes are delivered over HTTP, so there is nothing to handle.
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const RECONNECT_INITIAL_MS = 1000;
|
|
||||||
const RECONNECT_MAX_MS = 30_000;
|
|
||||||
|
|
||||||
interface SessionStreamResult {
|
|
||||||
messages: Message[];
|
|
||||||
connected: boolean;
|
|
||||||
error: string | null;
|
|
||||||
isStreaming: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useSessionStream(sessionId: string | undefined): SessionStreamResult {
|
|
||||||
const [state, setState] = useState<State>({ messages: [], connected: false, error: null });
|
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!sessionId) return;
|
|
||||||
|
|
||||||
setState({ messages: [], connected: false, error: null });
|
|
||||||
|
|
||||||
let unmounted = false;
|
|
||||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
let reconnectDelay = RECONNECT_INITIAL_MS;
|
|
||||||
|
|
||||||
const connect = () => {
|
|
||||||
if (unmounted) return;
|
|
||||||
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
|
|
||||||
const url = `${proto}://${window.location.host}/api/ws/sessions/${sessionId}`;
|
|
||||||
const ws = new WebSocket(url);
|
|
||||||
wsRef.current = ws;
|
|
||||||
|
|
||||||
ws.onopen = () => {
|
|
||||||
reconnectDelay = RECONNECT_INITIAL_MS;
|
|
||||||
setState((s) => ({ ...s, connected: true, error: null }));
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onmessage = (ev) => {
|
|
||||||
let frame: WsFrame;
|
|
||||||
try {
|
|
||||||
frame = JSON.parse(typeof ev.data === 'string' ? ev.data : '') as WsFrame;
|
|
||||||
} catch {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setState((s) => applyFrame(s, frame));
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onerror = () => {
|
|
||||||
try {
|
|
||||||
ws.close();
|
|
||||||
} catch {}
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onclose = () => {
|
|
||||||
if (unmounted) return;
|
|
||||||
setState((s) => ({ ...s, connected: false }));
|
|
||||||
const delay = reconnectDelay;
|
|
||||||
reconnectDelay = Math.min(reconnectDelay * 2, RECONNECT_MAX_MS);
|
|
||||||
reconnectTimer = setTimeout(connect, delay);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
connect();
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
unmounted = true;
|
|
||||||
if (reconnectTimer) clearTimeout(reconnectTimer);
|
|
||||||
const ws = wsRef.current;
|
|
||||||
wsRef.current = null;
|
|
||||||
if (ws)
|
|
||||||
try {
|
|
||||||
ws.close();
|
|
||||||
} catch {}
|
|
||||||
};
|
|
||||||
}, [sessionId]);
|
|
||||||
|
|
||||||
const isStreaming = state.messages.some((m) => m.status === 'streaming');
|
|
||||||
|
|
||||||
return {
|
|
||||||
messages: state.messages,
|
|
||||||
connected: state.connected,
|
|
||||||
error: state.error,
|
|
||||||
isStreaming,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import { StrictMode } from 'react';
|
|
||||||
import { createRoot } from 'react-dom/client';
|
|
||||||
import { BrowserRouter } from 'react-router-dom';
|
|
||||||
import { App } from './App';
|
|
||||||
import './globals.css';
|
|
||||||
|
|
||||||
createRoot(document.getElementById('root')!).render(
|
|
||||||
<StrictMode>
|
|
||||||
<BrowserRouter>
|
|
||||||
<App />
|
|
||||||
</BrowserRouter>
|
|
||||||
</StrictMode>,
|
|
||||||
);
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { useNavigate } from 'react-router-dom';
|
|
||||||
import { Code2, Folder, ArrowRight } from 'lucide-react';
|
|
||||||
import type { Project, Session } from '@/api/types';
|
|
||||||
import { api } from '@/api/client';
|
|
||||||
|
|
||||||
export function Home() {
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const [projects, setProjects] = useState<Project[]>([]);
|
|
||||||
const [sessions, setSessions] = useState<Session[]>([]);
|
|
||||||
const [selectedProject, setSelectedProject] = useState<string | null>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
|
|
||||||
// Fetch projects on mount
|
|
||||||
useEffect(() => {
|
|
||||||
api.projects
|
|
||||||
.list({ status: 'open' })
|
|
||||||
.then(setProjects)
|
|
||||||
.catch(console.error)
|
|
||||||
.finally(() => setLoading(false));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Fetch sessions when a project is selected
|
|
||||||
useEffect(() => {
|
|
||||||
if (!selectedProject) {
|
|
||||||
setSessions([]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
api.sessions
|
|
||||||
.listForProject(selectedProject, 'open')
|
|
||||||
.then(setSessions)
|
|
||||||
.catch(console.error);
|
|
||||||
}, [selectedProject]);
|
|
||||||
|
|
||||||
const handleSessionClick = (session: Session) => {
|
|
||||||
navigate(`/sessions/${session.id}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen bg-zinc-900 flex items-center justify-center">
|
|
||||||
<div className="text-zinc-500">Loading...</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen bg-zinc-900 p-6">
|
|
||||||
<div className="max-w-2xl mx-auto">
|
|
||||||
{/* Header */}
|
|
||||||
<div className="flex items-center gap-3 mb-8">
|
|
||||||
<Code2 size={28} className="text-blue-400" />
|
|
||||||
<h1 className="text-2xl font-bold text-zinc-100">BooCoder</h1>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Projects list */}
|
|
||||||
<div className="mb-8">
|
|
||||||
<h2 className="text-sm font-medium text-zinc-400 uppercase tracking-wide mb-3">
|
|
||||||
Projects
|
|
||||||
</h2>
|
|
||||||
{projects.length === 0 ? (
|
|
||||||
<p className="text-zinc-500 text-sm">
|
|
||||||
No projects found. Create one in BooChat first.
|
|
||||||
</p>
|
|
||||||
) : (
|
|
||||||
<div className="space-y-1">
|
|
||||||
{projects.map((project) => (
|
|
||||||
<button
|
|
||||||
key={project.id}
|
|
||||||
onClick={() => setSelectedProject(project.id)}
|
|
||||||
className={`w-full flex items-center gap-3 px-4 py-3 rounded-lg text-left transition-colors ${
|
|
||||||
selectedProject === project.id
|
|
||||||
? 'bg-blue-600/20 border border-blue-500/40'
|
|
||||||
: 'bg-zinc-800/50 border border-zinc-800 hover:bg-zinc-800'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<Folder
|
|
||||||
size={16}
|
|
||||||
className={
|
|
||||||
selectedProject === project.id
|
|
||||||
? 'text-blue-400'
|
|
||||||
: 'text-zinc-500'
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<div className="text-sm font-medium text-zinc-200 truncate">
|
|
||||||
{project.name}
|
|
||||||
</div>
|
|
||||||
<div className="text-xs text-zinc-500 truncate">
|
|
||||||
{project.path}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Sessions list */}
|
|
||||||
{selectedProject && (
|
|
||||||
<div>
|
|
||||||
<h2 className="text-sm font-medium text-zinc-400 uppercase tracking-wide mb-3">
|
|
||||||
Sessions
|
|
||||||
</h2>
|
|
||||||
{sessions.length === 0 ? (
|
|
||||||
<p className="text-zinc-500 text-sm">
|
|
||||||
No open sessions. Create one in BooChat first.
|
|
||||||
</p>
|
|
||||||
) : (
|
|
||||||
<div className="space-y-1">
|
|
||||||
{sessions.map((session) => (
|
|
||||||
<button
|
|
||||||
key={session.id}
|
|
||||||
onClick={() => handleSessionClick(session)}
|
|
||||||
className="w-full flex items-center gap-3 px-4 py-3 rounded-lg bg-zinc-800/50 border border-zinc-800 hover:bg-zinc-800 text-left transition-colors group"
|
|
||||||
>
|
|
||||||
<div className="flex-1 min-w-0">
|
|
||||||
<div className="text-sm font-medium text-zinc-200 truncate">
|
|
||||||
{session.name || 'Untitled session'}
|
|
||||||
</div>
|
|
||||||
<div className="text-xs text-zinc-500">
|
|
||||||
{new Date(session.updated_at).toLocaleDateString()}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<ArrowRight
|
|
||||||
size={16}
|
|
||||||
className="text-zinc-600 group-hover:text-zinc-400 transition-colors"
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import { useParams, useNavigate } from 'react-router-dom';
|
|
||||||
import { ArrowLeft } from 'lucide-react';
|
|
||||||
import type { Chat } from '@/api/types';
|
|
||||||
import { api } from '@/api/client';
|
|
||||||
import { useSessionStream } from '@/hooks/useSessionStream';
|
|
||||||
import { ChatPane } from '@/components/ChatPane';
|
|
||||||
import { DiffPane } from '@/components/DiffPane';
|
|
||||||
import { Layout } from '@/components/Layout';
|
|
||||||
|
|
||||||
export function Session() {
|
|
||||||
const { sessionId } = useParams<{ sessionId: string }>();
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const [chat, setChat] = useState<Chat | null>(null);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
|
|
||||||
const { messages, connected, isStreaming } = useSessionStream(sessionId);
|
|
||||||
|
|
||||||
// Get or create a chat for this session
|
|
||||||
useEffect(() => {
|
|
||||||
if (!sessionId) return;
|
|
||||||
|
|
||||||
api.chats
|
|
||||||
.listForSession(sessionId)
|
|
||||||
.then((chats) => {
|
|
||||||
// Use the first open chat, or create one
|
|
||||||
const openChat = chats.find((c) => c.status === 'open');
|
|
||||||
if (openChat) {
|
|
||||||
setChat(openChat);
|
|
||||||
} else {
|
|
||||||
// Create a new chat
|
|
||||||
return api.chats.create(sessionId).then((newChat) => {
|
|
||||||
setChat(newChat);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(console.error)
|
|
||||||
.finally(() => setLoading(false));
|
|
||||||
}, [sessionId]);
|
|
||||||
|
|
||||||
if (!sessionId) {
|
|
||||||
navigate('/');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen bg-zinc-900 flex items-center justify-center">
|
|
||||||
<div className="text-zinc-500">Loading session...</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!chat) {
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen bg-zinc-900 flex flex-col items-center justify-center gap-4">
|
|
||||||
<div className="text-zinc-500">Could not load chat for this session.</div>
|
|
||||||
<button
|
|
||||||
onClick={() => navigate('/')}
|
|
||||||
className="flex items-center gap-2 text-sm text-blue-400 hover:text-blue-300"
|
|
||||||
>
|
|
||||||
<ArrowLeft size={14} />
|
|
||||||
Back to projects
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Layout
|
|
||||||
chatPane={
|
|
||||||
<ChatPane
|
|
||||||
sessionId={sessionId}
|
|
||||||
chatId={chat.id}
|
|
||||||
messages={messages}
|
|
||||||
isStreaming={isStreaming}
|
|
||||||
connected={connected}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
diffPane={<DiffPane sessionId={sessionId} />}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
1
apps/coder/web/src/vite-env.d.ts
vendored
1
apps/coder/web/src/vite-env.d.ts
vendored
@@ -1 +0,0 @@
|
|||||||
/// <reference types="vite/client" />
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "ES2022",
|
|
||||||
"module": "ESNext",
|
|
||||||
"moduleResolution": "Bundler",
|
|
||||||
"strict": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"forceConsistentCasingInFileNames": true,
|
|
||||||
"resolveJsonModule": true,
|
|
||||||
"allowSyntheticDefaultImports": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"noUncheckedIndexedAccess": true,
|
|
||||||
"composite": true,
|
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
|
||||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
||||||
"jsx": "react-jsx",
|
|
||||||
"noEmit": true,
|
|
||||||
"useDefineForClassFields": true,
|
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"baseUrl": ".",
|
|
||||||
"paths": {
|
|
||||||
"@/*": ["./src/*"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"files": [],
|
|
||||||
"references": [
|
|
||||||
{ "path": "./tsconfig.app.json" },
|
|
||||||
{ "path": "./tsconfig.node.json" }
|
|
||||||
],
|
|
||||||
"compilerOptions": {
|
|
||||||
"baseUrl": ".",
|
|
||||||
"paths": {
|
|
||||||
"@/*": ["./src/*"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "ES2022",
|
|
||||||
"module": "ESNext",
|
|
||||||
"moduleResolution": "Bundler",
|
|
||||||
"strict": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"composite": true,
|
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
|
||||||
"allowSyntheticDefaultImports": true
|
|
||||||
},
|
|
||||||
"include": ["vite.config.ts"]
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import { defineConfig } from 'vite';
|
|
||||||
import react from '@vitejs/plugin-react';
|
|
||||||
import path from 'node:path';
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
plugins: [react()],
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'@': path.resolve(__dirname, './src'),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
server: {
|
|
||||||
port: 5174,
|
|
||||||
proxy: {
|
|
||||||
'/api': {
|
|
||||||
target: 'http://127.0.0.1:3000',
|
|
||||||
changeOrigin: true,
|
|
||||||
ws: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
build: {
|
|
||||||
outDir: 'dist',
|
|
||||||
emptyOutDir: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@@ -10,6 +10,18 @@ import { resolveProjectRoot, PathScopeError } from '../services/path_guard.js';
|
|||||||
import { listDir, viewFile } from '../services/file_ops.js';
|
import { listDir, viewFile } from '../services/file_ops.js';
|
||||||
import { getProjectFiles } from '../services/file_index.js';
|
import { getProjectFiles } from '../services/file_index.js';
|
||||||
import { getGitMeta } from '../services/git_meta.js';
|
import { getGitMeta } from '../services/git_meta.js';
|
||||||
|
import {
|
||||||
|
getGitDiff,
|
||||||
|
stageFiles,
|
||||||
|
unstageFiles,
|
||||||
|
commitFiles,
|
||||||
|
discardFiles,
|
||||||
|
detectInProgress,
|
||||||
|
isRepoDirty,
|
||||||
|
autoSelectMode,
|
||||||
|
GitWriteError,
|
||||||
|
} from '../services/git_diff.js';
|
||||||
|
import type { GitDiffMode } from '../services/git_diff.js';
|
||||||
import {
|
import {
|
||||||
bootstrapProject,
|
bootstrapProject,
|
||||||
BootstrapNameError,
|
BootstrapNameError,
|
||||||
@@ -453,6 +465,178 @@ export function registerProjectRoutes(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// GET /api/projects/:id/git/diff?mode=uncommitted|committed
|
||||||
|
// Returns the structured diff payload for the project repository. mode param
|
||||||
|
// selects the comparison: uncommitted (working tree vs HEAD) or committed
|
||||||
|
// (branch vs its upstream/default-branch base). When mode is absent the server
|
||||||
|
// auto-selects based on dirty state (FIX 1: dirty → uncommitted, clean → committed).
|
||||||
|
// Always includes auto_mode (the dirty-state-derived mode) so the client can
|
||||||
|
// show a suggestion when a pinned mode diverges from what would be auto-selected.
|
||||||
|
// Returns { git_repo: false } when the path is not a git repository.
|
||||||
|
app.get<{ Params: { id: string }; Querystring: { mode?: string } }>(
|
||||||
|
'/api/projects/:id/git/diff',
|
||||||
|
async (req, reply) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
const rawMode = req.query.mode;
|
||||||
|
|
||||||
|
const projectPath = await selectProjectPath(sql, id);
|
||||||
|
if (projectPath === null) {
|
||||||
|
reply.code(404);
|
||||||
|
return { error: 'not found' };
|
||||||
|
}
|
||||||
|
let projectRoot: string;
|
||||||
|
try {
|
||||||
|
projectRoot = await resolveProjectRoot(projectPath);
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof PathScopeError) {
|
||||||
|
reply.code(404);
|
||||||
|
return { error: err.message };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always detect dirty state: used for auto-select (FIX 1) and suggestion (FIX 4).
|
||||||
|
const dirty = await isRepoDirty(projectRoot);
|
||||||
|
const auto_mode = autoSelectMode(dirty);
|
||||||
|
|
||||||
|
const mode: GitDiffMode =
|
||||||
|
rawMode === 'committed' ? 'committed' :
|
||||||
|
rawMode === 'uncommitted' ? 'uncommitted' :
|
||||||
|
auto_mode; // no mode param → auto-select (FIX 1)
|
||||||
|
|
||||||
|
const result = await getGitDiff(projectRoot, mode);
|
||||||
|
if (result === null) {
|
||||||
|
return { git_repo: false, mode, auto_mode, base_label: null, in_progress_op: null, files: [] };
|
||||||
|
}
|
||||||
|
return { git_repo: true, ...result, auto_mode };
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// ── Git write routes (Phase 2) ─────────────────────────────────────────────
|
||||||
|
// These are user UI actions — NOT registered in the assistant tool registry.
|
||||||
|
// D-3: argv-safe runGit/execFile with -- separators (never shell strings).
|
||||||
|
// D-4: per-file pathGuard validation via validateWritePath.
|
||||||
|
// D-5: commit identity server-derived; request body .strict(), no author fields.
|
||||||
|
// D-7: index-lock → 409; in-progress op → 409.
|
||||||
|
// D-13: NOT in ALL_TOOLS.
|
||||||
|
|
||||||
|
const GitFilesBody = z.object({ files: z.array(z.string().min(1)).min(1) });
|
||||||
|
|
||||||
|
const GitCommitBody = z
|
||||||
|
.object({
|
||||||
|
message: z.string().min(1),
|
||||||
|
files: z.array(z.string().min(1)).optional(),
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const GitDiscardBody = z.object({
|
||||||
|
files: z.array(
|
||||||
|
z
|
||||||
|
.object({
|
||||||
|
path: z.string().min(1),
|
||||||
|
change_type: z.string().min(1),
|
||||||
|
staged: z.boolean(),
|
||||||
|
})
|
||||||
|
.strict(),
|
||||||
|
).min(1),
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /api/projects/:id/git/stage — stage whole files
|
||||||
|
app.post<{ Params: { id: string } }>(
|
||||||
|
'/api/projects/:id/git/stage',
|
||||||
|
async (req, reply) => {
|
||||||
|
const body = GitFilesBody.safeParse(req.body);
|
||||||
|
if (!body.success) { reply.code(400); return { error: body.error.message }; }
|
||||||
|
const { id } = req.params;
|
||||||
|
const projectPath = await selectProjectPath(sql, id);
|
||||||
|
if (!projectPath) { reply.code(404); return { error: 'not found' }; }
|
||||||
|
let root: string;
|
||||||
|
try { root = await resolveProjectRoot(projectPath); }
|
||||||
|
catch (err) { if (err instanceof PathScopeError) { reply.code(404); return { error: (err as Error).message }; } throw err; }
|
||||||
|
const inProg = await detectInProgress(root);
|
||||||
|
if (inProg) { reply.code(409); return { error: `git ${inProg} in progress — write actions disabled` }; }
|
||||||
|
try {
|
||||||
|
await stageFiles(root, body.data.files);
|
||||||
|
return { ok: true };
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof GitWriteError) { reply.code(err.busy ? 409 : 500); return { error: err.message }; }
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/projects/:id/git/unstage — unstage whole files
|
||||||
|
app.post<{ Params: { id: string } }>(
|
||||||
|
'/api/projects/:id/git/unstage',
|
||||||
|
async (req, reply) => {
|
||||||
|
const body = GitFilesBody.safeParse(req.body);
|
||||||
|
if (!body.success) { reply.code(400); return { error: body.error.message }; }
|
||||||
|
const { id } = req.params;
|
||||||
|
const projectPath = await selectProjectPath(sql, id);
|
||||||
|
if (!projectPath) { reply.code(404); return { error: 'not found' }; }
|
||||||
|
let root: string;
|
||||||
|
try { root = await resolveProjectRoot(projectPath); }
|
||||||
|
catch (err) { if (err instanceof PathScopeError) { reply.code(404); return { error: (err as Error).message }; } throw err; }
|
||||||
|
const inProg = await detectInProgress(root);
|
||||||
|
if (inProg) { reply.code(409); return { error: `git ${inProg} in progress — write actions disabled` }; }
|
||||||
|
try {
|
||||||
|
await unstageFiles(root, body.data.files);
|
||||||
|
return { ok: true };
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof GitWriteError) { reply.code(err.busy ? 409 : 500); return { error: err.message }; }
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/projects/:id/git/commit — commit staged files (identity server-derived)
|
||||||
|
app.post<{ Params: { id: string } }>(
|
||||||
|
'/api/projects/:id/git/commit',
|
||||||
|
async (req, reply) => {
|
||||||
|
const body = GitCommitBody.safeParse(req.body);
|
||||||
|
if (!body.success) { reply.code(400); return { error: body.error.message }; }
|
||||||
|
const { id } = req.params;
|
||||||
|
const projectPath = await selectProjectPath(sql, id);
|
||||||
|
if (!projectPath) { reply.code(404); return { error: 'not found' }; }
|
||||||
|
let root: string;
|
||||||
|
try { root = await resolveProjectRoot(projectPath); }
|
||||||
|
catch (err) { if (err instanceof PathScopeError) { reply.code(404); return { error: (err as Error).message }; } throw err; }
|
||||||
|
const inProg = await detectInProgress(root);
|
||||||
|
if (inProg) { reply.code(409); return { error: `git ${inProg} in progress — write actions disabled` }; }
|
||||||
|
try {
|
||||||
|
await commitFiles(root, body.data.message, body.data.files);
|
||||||
|
return { ok: true };
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof GitWriteError) { reply.code(err.busy ? 409 : 500); return { error: err.message }; }
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/projects/:id/git/discard — discard file changes (irrecoverable)
|
||||||
|
app.post<{ Params: { id: string } }>(
|
||||||
|
'/api/projects/:id/git/discard',
|
||||||
|
async (req, reply) => {
|
||||||
|
const body = GitDiscardBody.safeParse(req.body);
|
||||||
|
if (!body.success) { reply.code(400); return { error: body.error.message }; }
|
||||||
|
const { id } = req.params;
|
||||||
|
const projectPath = await selectProjectPath(sql, id);
|
||||||
|
if (!projectPath) { reply.code(404); return { error: 'not found' }; }
|
||||||
|
let root: string;
|
||||||
|
try { root = await resolveProjectRoot(projectPath); }
|
||||||
|
catch (err) { if (err instanceof PathScopeError) { reply.code(404); return { error: (err as Error).message }; } throw err; }
|
||||||
|
const inProg = await detectInProgress(root);
|
||||||
|
if (inProg) { reply.code(409); return { error: `git ${inProg} in progress — write actions disabled` }; }
|
||||||
|
try {
|
||||||
|
await discardFiles(root, body.data.files);
|
||||||
|
return { ok: true };
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof GitWriteError) { reply.code(err.busy ? 409 : 500); return { error: err.message }; }
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// GET /api/projects/:id/files
|
// GET /api/projects/:id/files
|
||||||
app.get<{ Params: { id: string } }>(
|
app.get<{ Params: { id: string } }>(
|
||||||
'/api/projects/:id/files',
|
'/api/projects/:id/files',
|
||||||
|
|||||||
346
apps/server/src/services/__tests__/git_diff.test.ts
Normal file
346
apps/server/src/services/__tests__/git_diff.test.ts
Normal file
@@ -0,0 +1,346 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||||
|
import { mkdtemp, rm, mkdir, writeFile } from 'node:fs/promises';
|
||||||
|
import { tmpdir } from 'node:os';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { realpath } from 'node:fs/promises';
|
||||||
|
import { execFile } from 'node:child_process';
|
||||||
|
import { promisify } from 'node:util';
|
||||||
|
import {
|
||||||
|
parseNameStatus,
|
||||||
|
splitDiffByFile,
|
||||||
|
classifyDiffBody,
|
||||||
|
autoSelectMode,
|
||||||
|
detectInProgress,
|
||||||
|
resolveCommittedBase,
|
||||||
|
canCommit,
|
||||||
|
getGitDiff,
|
||||||
|
} from '../git_diff.js';
|
||||||
|
import type { GitDiffFile } from '../git_diff.js';
|
||||||
|
|
||||||
|
const execFileAsync = promisify(execFile);
|
||||||
|
|
||||||
|
// ── T1: parseNameStatus ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('parseNameStatus', () => {
|
||||||
|
it('parses modified file', () => {
|
||||||
|
const files = parseNameStatus('M\tsrc/foo.ts\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
expect(files[0]).toMatchObject({ path: 'src/foo.ts', change_type: 'modified', old_path: null });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses added file', () => {
|
||||||
|
const files = parseNameStatus('A\tnewfile.ts\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
expect(files[0]).toMatchObject({ path: 'newfile.ts', change_type: 'added' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses deleted file', () => {
|
||||||
|
const files = parseNameStatus('D\tremoved.ts\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
expect(files[0]).toMatchObject({ path: 'removed.ts', change_type: 'deleted' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses renamed file with similarity score', () => {
|
||||||
|
const files = parseNameStatus('R100\told.ts\tnew.ts\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
expect(files[0]).toMatchObject({ path: 'new.ts', old_path: 'old.ts', change_type: 'renamed' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses type-changed file as modified', () => {
|
||||||
|
const files = parseNameStatus('T\tsymlink.ts\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
expect(files[0]).toMatchObject({ path: 'symlink.ts', change_type: 'modified' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses multiple files from multiline output', () => {
|
||||||
|
const output = 'M\ta.ts\nA\tb.ts\nD\tc.ts\n';
|
||||||
|
const files = parseNameStatus(output);
|
||||||
|
expect(files).toHaveLength(3);
|
||||||
|
expect(files.map((f) => f.change_type)).toEqual(['modified', 'added', 'deleted']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores blank lines', () => {
|
||||||
|
const files = parseNameStatus('\n\nM\ta.ts\n\n');
|
||||||
|
expect(files).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array for empty input', () => {
|
||||||
|
expect(parseNameStatus('')).toHaveLength(0);
|
||||||
|
expect(parseNameStatus('\n')).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T2: splitDiffByFile ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('splitDiffByFile', () => {
|
||||||
|
const FIXTURE = `diff --git a/src/a.ts b/src/a.ts
|
||||||
|
index abc1234..def5678 100644
|
||||||
|
--- a/src/a.ts
|
||||||
|
+++ b/src/a.ts
|
||||||
|
@@ -1,3 +1,4 @@
|
||||||
|
context
|
||||||
|
-old line
|
||||||
|
+new line
|
||||||
|
more context
|
||||||
|
diff --git a/src/b.ts b/src/b.ts
|
||||||
|
index 1111111..2222222 100644
|
||||||
|
--- a/src/b.ts
|
||||||
|
+++ b/src/b.ts
|
||||||
|
@@ -10,2 +10,3 @@
|
||||||
|
ctx
|
||||||
|
+added
|
||||||
|
`;
|
||||||
|
|
||||||
|
it('splits two-file diff into two entries', () => {
|
||||||
|
const map = splitDiffByFile(FIXTURE);
|
||||||
|
expect(map.size).toBe(2);
|
||||||
|
expect(map.has('src/a.ts')).toBe(true);
|
||||||
|
expect(map.has('src/b.ts')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('each segment starts with diff --git header', () => {
|
||||||
|
const map = splitDiffByFile(FIXTURE);
|
||||||
|
expect(map.get('src/a.ts')).toMatch(/^diff --git a\/src\/a\.ts/);
|
||||||
|
expect(map.get('src/b.ts')).toMatch(/^diff --git a\/src\/b\.ts/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles deleted file (no +++ b/ line)', () => {
|
||||||
|
const deleted = `diff --git a/gone.ts b/gone.ts
|
||||||
|
deleted file mode 100644
|
||||||
|
--- a/gone.ts
|
||||||
|
+++ /dev/null
|
||||||
|
@@ -1,2 +0,0 @@
|
||||||
|
-line1
|
||||||
|
-line2
|
||||||
|
`;
|
||||||
|
const map = splitDiffByFile(deleted);
|
||||||
|
expect(map.size).toBe(1);
|
||||||
|
expect(map.has('gone.ts')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty map for empty input', () => {
|
||||||
|
expect(splitDiffByFile('').size).toBe(0);
|
||||||
|
expect(splitDiffByFile('\n').size).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T3: resolveCommittedBase (integration with temp git repo) ──────────────
|
||||||
|
|
||||||
|
describe('resolveCommittedBase', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-gitdiff-base-')));
|
||||||
|
await execFileAsync('git', ['init'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['-c', 'user.email=test@test.com', '-c', 'user.name=Test',
|
||||||
|
'commit', '--allow-empty', '-m', 'init'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null base when no upstream and no origin', async () => {
|
||||||
|
const { base, label } = await resolveCommittedBase(tmp);
|
||||||
|
expect(base).toBeNull();
|
||||||
|
expect(label).toBeTruthy(); // still has a descriptive label
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T4: autoSelectMode ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('autoSelectMode', () => {
|
||||||
|
it('returns uncommitted when dirty', () => {
|
||||||
|
expect(autoSelectMode(true)).toBe('uncommitted');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns committed when clean', () => {
|
||||||
|
expect(autoSelectMode(false)).toBe('committed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T5: classifyDiffBody ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('classifyDiffBody', () => {
|
||||||
|
it('classifies a normal diff as diff', () => {
|
||||||
|
const body = `diff --git a/foo b/foo
|
||||||
|
--- a/foo
|
||||||
|
+++ b/foo
|
||||||
|
@@ -1 +1 @@
|
||||||
|
-old
|
||||||
|
+new
|
||||||
|
`;
|
||||||
|
expect(classifyDiffBody(body)).toBe('diff');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('classifies binary diff as binary', () => {
|
||||||
|
const body = `diff --git a/image.png b/image.png
|
||||||
|
index abc..def 100644
|
||||||
|
Binary files a/image.png and b/image.png differ
|
||||||
|
`;
|
||||||
|
expect(classifyDiffBody(body)).toBe('binary');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('classifies oversized diff as too_large', () => {
|
||||||
|
const big = 'a'.repeat(600 * 1024); // 600KB > default cap
|
||||||
|
expect(classifyDiffBody(big)).toBe('too_large');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('respects custom cap', () => {
|
||||||
|
const body = 'a'.repeat(100);
|
||||||
|
expect(classifyDiffBody(body, 50)).toBe('too_large');
|
||||||
|
expect(classifyDiffBody(body, 200)).toBe('diff');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T6: detectInProgress ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('detectInProgress', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-inprogress-')));
|
||||||
|
await mkdir(join(tmp, '.git'));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when no sentinel files present', async () => {
|
||||||
|
expect(await detectInProgress(tmp)).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects merge via MERGE_HEAD', async () => {
|
||||||
|
await writeFile(join(tmp, '.git', 'MERGE_HEAD'), 'abc');
|
||||||
|
expect(await detectInProgress(tmp)).toBe('merge');
|
||||||
|
await rm(join(tmp, '.git', 'MERGE_HEAD'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects cherry-pick via CHERRY_PICK_HEAD', async () => {
|
||||||
|
await writeFile(join(tmp, '.git', 'CHERRY_PICK_HEAD'), 'abc');
|
||||||
|
expect(await detectInProgress(tmp)).toBe('cherry-pick');
|
||||||
|
await rm(join(tmp, '.git', 'CHERRY_PICK_HEAD'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects bisect via BISECT_LOG', async () => {
|
||||||
|
await writeFile(join(tmp, '.git', 'BISECT_LOG'), 'abc');
|
||||||
|
expect(await detectInProgress(tmp)).toBe('bisect');
|
||||||
|
await rm(join(tmp, '.git', 'BISECT_LOG'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects rebase via rebase-merge directory', async () => {
|
||||||
|
await mkdir(join(tmp, '.git', 'rebase-merge'));
|
||||||
|
expect(await detectInProgress(tmp)).toBe('rebase');
|
||||||
|
await rm(join(tmp, '.git', 'rebase-merge'), { recursive: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('detects rebase via rebase-apply directory', async () => {
|
||||||
|
await mkdir(join(tmp, '.git', 'rebase-apply'));
|
||||||
|
expect(await detectInProgress(tmp)).toBe('rebase');
|
||||||
|
await rm(join(tmp, '.git', 'rebase-apply'), { recursive: true });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T7: canCommit ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('canCommit', () => {
|
||||||
|
const stagedFile: GitDiffFile = {
|
||||||
|
path: 'a.ts',
|
||||||
|
old_path: null,
|
||||||
|
change_type: 'modified',
|
||||||
|
added_lines: 1,
|
||||||
|
removed_lines: 0,
|
||||||
|
staged: true,
|
||||||
|
diff_body: '+new',
|
||||||
|
is_binary: false,
|
||||||
|
is_too_large: false,
|
||||||
|
};
|
||||||
|
const unstagedFile: GitDiffFile = { ...stagedFile, staged: false };
|
||||||
|
|
||||||
|
it('returns true when at least one file is staged', () => {
|
||||||
|
expect(canCommit([stagedFile, unstagedFile])).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns false when no files are staged', () => {
|
||||||
|
expect(canCommit([unstagedFile])).toBe(false);
|
||||||
|
expect(canCommit([])).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T8: getGitDiff integration test ───────────────────────────────────────
|
||||||
|
|
||||||
|
describe('getGitDiff integration (temp repo)', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-gitdiff-int-')));
|
||||||
|
|
||||||
|
// Init repo + initial commit
|
||||||
|
await execFileAsync('git', ['init'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['config', 'user.email', 'test@test.com'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['config', 'user.name', 'Test'], { cwd: tmp });
|
||||||
|
await writeFile(join(tmp, 'existing.ts'), 'const x = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', '.'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['commit', '-m', 'init'], { cwd: tmp });
|
||||||
|
|
||||||
|
// Modify existing file (unstaged)
|
||||||
|
await writeFile(join(tmp, 'existing.ts'), 'const x = 2;\n');
|
||||||
|
// Add new untracked file
|
||||||
|
await writeFile(join(tmp, 'untracked.ts'), 'export {};\n');
|
||||||
|
// Stage a new file
|
||||||
|
await writeFile(join(tmp, 'staged.ts'), 'export const y = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', 'staged.ts'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getGitDiff returns git_repo true for a git repo', async () => {
|
||||||
|
const result = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result!.mode).toBe('uncommitted');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('includes modified file in uncommitted mode', async () => {
|
||||||
|
const result = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const paths = result!.files.map((f: GitDiffFile) => f.path);
|
||||||
|
expect(paths).toContain('existing.ts');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('includes staged file with staged=true', async () => {
|
||||||
|
const result = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const staged = result!.files.find((f: GitDiffFile) => f.path === 'staged.ts');
|
||||||
|
expect(staged).toBeDefined();
|
||||||
|
expect(staged!.staged).toBe(true);
|
||||||
|
expect(staged!.change_type).toBe('added');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('includes untracked file with change_type=untracked', async () => {
|
||||||
|
const result = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const untracked = result!.files.find((f: GitDiffFile) => f.path === 'untracked.ts');
|
||||||
|
expect(untracked).toBeDefined();
|
||||||
|
expect(untracked!.change_type).toBe('untracked');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for a non-git directory', async () => {
|
||||||
|
const nonGit = await realpath(await mkdtemp(join(tmpdir(), 'boocode-nongit-')));
|
||||||
|
try {
|
||||||
|
const result = await getGitDiff(nonGit, 'uncommitted');
|
||||||
|
expect(result).toBeNull();
|
||||||
|
} finally {
|
||||||
|
await rm(nonGit, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns in_progress_op when MERGE_HEAD exists', async () => {
|
||||||
|
await writeFile(join(tmp, '.git', 'MERGE_HEAD'), 'abc\n');
|
||||||
|
try {
|
||||||
|
const result = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(result!.in_progress_op).toBe('merge');
|
||||||
|
} finally {
|
||||||
|
await rm(join(tmp, '.git', 'MERGE_HEAD'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
379
apps/server/src/services/__tests__/git_diff_write.test.ts
Normal file
379
apps/server/src/services/__tests__/git_diff_write.test.ts
Normal file
@@ -0,0 +1,379 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||||
|
import { mkdtemp, rm, writeFile, mkdir, access, symlink } from 'node:fs/promises';
|
||||||
|
import { tmpdir } from 'node:os';
|
||||||
|
import { join } from 'node:path';
|
||||||
|
import { realpath } from 'node:fs/promises';
|
||||||
|
import { execFile } from 'node:child_process';
|
||||||
|
import { promisify } from 'node:util';
|
||||||
|
import {
|
||||||
|
validateWritePath,
|
||||||
|
checkSymlinkEscape,
|
||||||
|
stageFiles,
|
||||||
|
unstageFiles,
|
||||||
|
commitFiles,
|
||||||
|
discardFiles,
|
||||||
|
deriveCommitIdentity,
|
||||||
|
GitWriteError,
|
||||||
|
getGitDiff,
|
||||||
|
} from '../git_diff.js';
|
||||||
|
|
||||||
|
const execFileAsync = promisify(execFile);
|
||||||
|
|
||||||
|
// ── T12: validateWritePath — pure validation ──────────────────────────────
|
||||||
|
|
||||||
|
describe('validateWritePath', () => {
|
||||||
|
const root = '/repo/root';
|
||||||
|
|
||||||
|
it('accepts a simple relative path', () => {
|
||||||
|
expect(() => validateWritePath(root, 'src/foo.ts')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts a nested path', () => {
|
||||||
|
expect(() => validateWritePath(root, 'a/b/c.ts')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects empty string', () => {
|
||||||
|
expect(() => validateWritePath(root, '')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects path starting with - (flag injection)', () => {
|
||||||
|
expect(() => validateWritePath(root, '-flag')).toThrow(GitWriteError);
|
||||||
|
expect(() => validateWritePath(root, '--option')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects "." (repo root discard)', () => {
|
||||||
|
expect(() => validateWritePath(root, '.')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects absolute paths', () => {
|
||||||
|
expect(() => validateWritePath(root, '/etc/passwd')).toThrow(GitWriteError);
|
||||||
|
expect(() => validateWritePath(root, '/repo/root/file.ts')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects ".." traversal escaping root', () => {
|
||||||
|
expect(() => validateWritePath(root, '../outside/file.ts')).toThrow(GitWriteError);
|
||||||
|
expect(() => validateWritePath(root, 'a/../../outside')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects path resolving exactly to root', () => {
|
||||||
|
// e.g. "a/.." resolves to /repo/root which is the root itself
|
||||||
|
expect(() => validateWritePath(root, 'a/..')).toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws GitWriteError not just Error', () => {
|
||||||
|
try {
|
||||||
|
validateWritePath(root, '-bad');
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toBeInstanceOf(GitWriteError);
|
||||||
|
expect((err as GitWriteError).busy).toBe(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── Integration tests (temp git repo) ─────────────────────────────────────
|
||||||
|
|
||||||
|
async function initRepo(dir: string) {
|
||||||
|
await execFileAsync('git', ['init'], { cwd: dir });
|
||||||
|
await execFileAsync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir });
|
||||||
|
await execFileAsync('git', ['config', 'user.name', 'Test User'], { cwd: dir });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fileExists(path: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
await access(path);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── T9: stage / unstage round-trip ────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('stageFiles / unstageFiles round-trip (temp repo)', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-write-stage-')));
|
||||||
|
await initRepo(tmp);
|
||||||
|
await writeFile(join(tmp, 'initial.ts'), 'const a = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', '.'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['commit', '-m', 'init'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('staging an untracked file shows it as staged in diff', async () => {
|
||||||
|
await writeFile(join(tmp, 'new.ts'), 'export const x = 1;\n');
|
||||||
|
// Before staging
|
||||||
|
const before = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const untrackedBefore = before!.files.find((f) => f.path === 'new.ts');
|
||||||
|
expect(untrackedBefore?.change_type).toBe('untracked');
|
||||||
|
expect(untrackedBefore?.staged).toBe(false);
|
||||||
|
|
||||||
|
// Stage
|
||||||
|
await stageFiles(tmp, ['new.ts']);
|
||||||
|
|
||||||
|
// After staging
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const stagedAfter = after!.files.find((f) => f.path === 'new.ts');
|
||||||
|
expect(stagedAfter?.staged).toBe(true);
|
||||||
|
expect(stagedAfter?.change_type).toBe('added');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('unstaging removes file from staged set', async () => {
|
||||||
|
// new.ts is currently staged from the previous test
|
||||||
|
await unstageFiles(tmp, ['new.ts']);
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const f = after!.files.find((f) => f.path === 'new.ts');
|
||||||
|
expect(f?.staged).toBe(false);
|
||||||
|
expect(f?.change_type).toBe('untracked');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stageFiles rejects a path starting with -', async () => {
|
||||||
|
await expect(stageFiles(tmp, ['-bad'])).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stageFiles rejects path traversal', async () => {
|
||||||
|
await expect(stageFiles(tmp, ['../outside.ts'])).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T10: commit with server-derived identity ──────────────────────────────
|
||||||
|
|
||||||
|
describe('commitFiles with server-derived identity (temp repo)', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-write-commit-')));
|
||||||
|
await initRepo(tmp);
|
||||||
|
await writeFile(join(tmp, 'base.ts'), 'export const a = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', '.'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['commit', '-m', 'init'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('deriveCommitIdentity falls back when no git config set', async () => {
|
||||||
|
// New repo initialized without global user config — may or may not have local config.
|
||||||
|
// The function should always return a non-empty name and email.
|
||||||
|
const identity = await deriveCommitIdentity(tmp);
|
||||||
|
expect(identity.name).toBeTruthy();
|
||||||
|
expect(identity.email).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('deriveCommitIdentity uses git config when set', async () => {
|
||||||
|
const identity = await deriveCommitIdentity(tmp);
|
||||||
|
// We set user.email/name in initRepo above
|
||||||
|
expect(identity.name).toBe('Test User');
|
||||||
|
expect(identity.email).toBe('test@test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('commit creates a new commit and the staged file is no longer in diff', async () => {
|
||||||
|
await writeFile(join(tmp, 'newfile.ts'), 'export const b = 2;\n');
|
||||||
|
await stageFiles(tmp, ['newfile.ts']);
|
||||||
|
|
||||||
|
const before = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(before!.files.find((f) => f.path === 'newfile.ts')).toBeDefined();
|
||||||
|
|
||||||
|
await commitFiles(tmp, 'add newfile');
|
||||||
|
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(after!.files.find((f) => f.path === 'newfile.ts')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('commit with specific files only commits those files', async () => {
|
||||||
|
await writeFile(join(tmp, 'a.ts'), 'const a = 1;\n');
|
||||||
|
await writeFile(join(tmp, 'b.ts'), 'const b = 2;\n');
|
||||||
|
await stageFiles(tmp, ['a.ts', 'b.ts']);
|
||||||
|
|
||||||
|
await commitFiles(tmp, 'partial commit', ['a.ts']);
|
||||||
|
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
const aFile = after!.files.find((f) => f.path === 'a.ts');
|
||||||
|
const bFile = after!.files.find((f) => f.path === 'b.ts');
|
||||||
|
// a.ts was committed — should not appear in uncommitted diff
|
||||||
|
expect(aFile).toBeUndefined();
|
||||||
|
// b.ts is still staged
|
||||||
|
expect(bFile?.staged).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('commit rejects a path starting with - in files list', async () => {
|
||||||
|
await expect(commitFiles(tmp, 'msg', ['-bad'])).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T11: discard tracked vs untracked ─────────────────────────────────────
|
||||||
|
|
||||||
|
describe('discardFiles (temp repo)', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-write-discard-')));
|
||||||
|
await initRepo(tmp);
|
||||||
|
await writeFile(join(tmp, 'tracked.ts'), 'const orig = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', '.'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['commit', '-m', 'init'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discarding a modified tracked file reverts its content', async () => {
|
||||||
|
await writeFile(join(tmp, 'tracked.ts'), 'const modified = 99;\n');
|
||||||
|
const before = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(before!.files.find((f) => f.path === 'tracked.ts')).toBeDefined();
|
||||||
|
|
||||||
|
await discardFiles(tmp, [{ path: 'tracked.ts', change_type: 'modified', staged: false }]);
|
||||||
|
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(after!.files.find((f) => f.path === 'tracked.ts')).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discarding an untracked file removes it from disk', async () => {
|
||||||
|
await writeFile(join(tmp, 'untracked.ts'), 'orphan\n');
|
||||||
|
const exists = await fileExists(join(tmp, 'untracked.ts'));
|
||||||
|
expect(exists).toBe(true);
|
||||||
|
|
||||||
|
await discardFiles(tmp, [{ path: 'untracked.ts', change_type: 'untracked', staged: false }]);
|
||||||
|
|
||||||
|
expect(await fileExists(join(tmp, 'untracked.ts'))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discarding a staged-addition file removes it from index and disk', async () => {
|
||||||
|
await writeFile(join(tmp, 'staged-add.ts'), 'new file\n');
|
||||||
|
await stageFiles(tmp, ['staged-add.ts']);
|
||||||
|
|
||||||
|
const before = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(before!.files.find((f) => f.path === 'staged-add.ts')?.staged).toBe(true);
|
||||||
|
|
||||||
|
await discardFiles(tmp, [{ path: 'staged-add.ts', change_type: 'added', staged: true }]);
|
||||||
|
|
||||||
|
const after = await getGitDiff(tmp, 'uncommitted');
|
||||||
|
expect(after!.files.find((f) => f.path === 'staged-add.ts')).toBeUndefined();
|
||||||
|
expect(await fileExists(join(tmp, 'staged-add.ts'))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discardFiles rejects "." (repo root)', async () => {
|
||||||
|
await expect(
|
||||||
|
discardFiles(tmp, [{ path: '.', change_type: 'modified', staged: false }]),
|
||||||
|
).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('discardFiles rejects path traversal', async () => {
|
||||||
|
await expect(
|
||||||
|
discardFiles(tmp, [{ path: '../outside', change_type: 'untracked', staged: false }]),
|
||||||
|
).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── Index-lock → busy error ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('index-lock detection', () => {
|
||||||
|
let tmp: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
tmp = await realpath(await mkdtemp(join(tmpdir(), 'boocode-write-lock-')));
|
||||||
|
await initRepo(tmp);
|
||||||
|
await writeFile(join(tmp, 'file.ts'), 'const x = 1;\n');
|
||||||
|
await execFileAsync('git', ['add', '.'], { cwd: tmp });
|
||||||
|
await execFileAsync('git', ['commit', '-m', 'init'], { cwd: tmp });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(tmp, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stageFiles throws GitWriteError with busy=true when index.lock exists', async () => {
|
||||||
|
await writeFile(join(tmp, 'new.ts'), 'export {};\n');
|
||||||
|
// Simulate a lock by creating .git/index.lock
|
||||||
|
await mkdir(join(tmp, '.git'), { recursive: true });
|
||||||
|
await writeFile(join(tmp, '.git', 'index.lock'), '');
|
||||||
|
try {
|
||||||
|
await stageFiles(tmp, ['new.ts']);
|
||||||
|
// Should not reach here
|
||||||
|
expect(true).toBe(false);
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toBeInstanceOf(GitWriteError);
|
||||||
|
expect((err as GitWriteError).busy).toBe(true);
|
||||||
|
} finally {
|
||||||
|
try { await rm(join(tmp, '.git', 'index.lock')); } catch { /* already gone */ }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── Commit request schema: reject unknown author fields ───────────────────
|
||||||
|
|
||||||
|
describe('GitCommitBody schema strictness (unit)', () => {
|
||||||
|
it('rejects extra author/email fields via Zod strict', () => {
|
||||||
|
// We import Zod inline to mirror the route's schema
|
||||||
|
const { z } = require('zod');
|
||||||
|
const GitCommitBody = z
|
||||||
|
.object({
|
||||||
|
message: z.string().min(1),
|
||||||
|
files: z.array(z.string().min(1)).optional(),
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const result = GitCommitBody.safeParse({
|
||||||
|
message: 'test commit',
|
||||||
|
author: 'Evil <evil@hack.com>',
|
||||||
|
email: 'evil@hack.com',
|
||||||
|
});
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts valid commit body with message only', () => {
|
||||||
|
const { z } = require('zod');
|
||||||
|
const GitCommitBody = z
|
||||||
|
.object({
|
||||||
|
message: z.string().min(1),
|
||||||
|
files: z.array(z.string().min(1)).optional(),
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const result = GitCommitBody.safeParse({ message: 'add feature' });
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── T13: checkSymlinkEscape (FIX 3) ──────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('checkSymlinkEscape', () => {
|
||||||
|
let repoDir: string;
|
||||||
|
let outsideDir: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
repoDir = await realpath(await mkdtemp(join(tmpdir(), 'boocode-symlink-repo-')));
|
||||||
|
outsideDir = await realpath(await mkdtemp(join(tmpdir(), 'boocode-symlink-outside-')));
|
||||||
|
await writeFile(join(outsideDir, 'secret.ts'), 'secret data\n');
|
||||||
|
// Symlink inside repo pointing to outside dir
|
||||||
|
await symlink(outsideDir, join(repoDir, 'evil'));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rm(repoDir, { recursive: true, force: true });
|
||||||
|
await rm(outsideDir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects a path that escapes via a directory symlink', async () => {
|
||||||
|
await expect(checkSymlinkEscape(repoDir, 'evil/secret.ts')).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects a path that resolves to the symlink itself (outside)', async () => {
|
||||||
|
await expect(checkSymlinkEscape(repoDir, 'evil')).rejects.toThrow(GitWriteError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts a path that resolves within the repo', async () => {
|
||||||
|
await writeFile(join(repoDir, 'legit.ts'), 'export {};\n');
|
||||||
|
await expect(checkSymlinkEscape(repoDir, 'legit.ts')).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts a non-existent path (new file being staged)', async () => {
|
||||||
|
await expect(checkSymlinkEscape(repoDir, 'brand-new-file-not-yet-created.ts')).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { classifyStreamError } from '../inference/stream-error-classifier.js';
|
||||||
|
|
||||||
|
describe('classifyStreamError', () => {
|
||||||
|
it("classifies AbortError as 'stall'", () => {
|
||||||
|
const err = new Error('aborted');
|
||||||
|
err.name = 'AbortError';
|
||||||
|
expect(classifyStreamError(err)).toBe('stall');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("classifies a 503 HTTP error as 'transient'", () => {
|
||||||
|
const err = Object.assign(new Error('Service Unavailable'), { status: 503 });
|
||||||
|
expect(classifyStreamError(err)).toBe('transient');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("classifies a 500 HTTP error as 'transient'", () => {
|
||||||
|
const err = Object.assign(new Error('Internal Server Error'), { status: 500 });
|
||||||
|
expect(classifyStreamError(err)).toBe('transient');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("classifies a 4xx HTTP error as 'non-retryable'", () => {
|
||||||
|
const err = Object.assign(new Error('Bad Request'), { status: 400 });
|
||||||
|
expect(classifyStreamError(err)).toBe('non-retryable');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("classifies a generic Error as 'non-retryable'", () => {
|
||||||
|
expect(classifyStreamError(new Error('something went wrong'))).toBe('non-retryable');
|
||||||
|
});
|
||||||
|
});
|
||||||
153
apps/server/src/services/__tests__/stream-phase-adapter.test.ts
Normal file
153
apps/server/src/services/__tests__/stream-phase-adapter.test.ts
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
// Gate test: pins the <invoke>-as-text fallback in the stream-phase text-delta
|
||||||
|
// path. This test will fail if extractToolCallBlocks is ever removed from the
|
||||||
|
// text-delta branch of streamCompletion, which is the only guard for models
|
||||||
|
// that emit tool calls as inline XML rather than structured tool_calls.
|
||||||
|
import { describe, expect, it, vi, afterEach } from 'vitest';
|
||||||
|
import type { FastifyBaseLogger } from 'fastify';
|
||||||
|
|
||||||
|
// vi.mock is hoisted before all module imports. Spread the original so all
|
||||||
|
// other ai exports (tool, jsonSchema, types, …) remain real; only streamText
|
||||||
|
// is replaced with a controllable spy.
|
||||||
|
vi.mock('ai', async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import('ai')>();
|
||||||
|
return { ...actual, streamText: vi.fn() };
|
||||||
|
});
|
||||||
|
|
||||||
|
import { streamText } from 'ai';
|
||||||
|
import { streamCompletion, STALL_TIMEOUT_MS } from '../inference/stream-phase-adapter.js';
|
||||||
|
import type { StreamAdapterContext } from '../inference/stream-phase-adapter.js';
|
||||||
|
|
||||||
|
const INVOKE_BLOCK =
|
||||||
|
'<invoke name="view_file"><parameter name="path">/tmp/test.ts</parameter></invoke>';
|
||||||
|
|
||||||
|
// One-shot async generator that yields a single text-delta carrying a complete
|
||||||
|
// <invoke> block, simulating a model that emits its tool call as plain XML text.
|
||||||
|
async function* makeInvokeTextDeltaStream() {
|
||||||
|
yield { type: 'text-delta' as const, text: INVOKE_BLOCK };
|
||||||
|
}
|
||||||
|
|
||||||
|
const fakeLog = {
|
||||||
|
debug: vi.fn(),
|
||||||
|
info: vi.fn(),
|
||||||
|
warn: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
fatal: vi.fn(),
|
||||||
|
trace: vi.fn(),
|
||||||
|
child: vi.fn(),
|
||||||
|
} as unknown as FastifyBaseLogger;
|
||||||
|
|
||||||
|
const fakeCtx: StreamAdapterContext = {
|
||||||
|
config: { LLAMA_SWAP_URL: 'http://localhost:11434' } as StreamAdapterContext['config'],
|
||||||
|
log: fakeLog,
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('<invoke>-as-text fallback gate (stream-phase text-delta path)', () => {
|
||||||
|
it('surfaces a plain-text <invoke> block as a toolCall and strips markup from content and deltas', async () => {
|
||||||
|
vi.mocked(streamText).mockReturnValue({
|
||||||
|
fullStream: makeInvokeTextDeltaStream(),
|
||||||
|
usage: Promise.resolve({ inputTokens: 1, outputTokens: 1 }),
|
||||||
|
} as unknown as ReturnType<typeof streamText>);
|
||||||
|
|
||||||
|
const deltas: string[] = [];
|
||||||
|
const result = await streamCompletion(
|
||||||
|
fakeCtx,
|
||||||
|
'test-model',
|
||||||
|
[{ role: 'user', content: 'call a tool' }],
|
||||||
|
{ tools: null },
|
||||||
|
(d) => deltas.push(d),
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
// The <invoke> block must surface as a structured tool call
|
||||||
|
expect(result.toolCalls).toHaveLength(1);
|
||||||
|
expect(result.toolCalls[0]).toMatchObject({
|
||||||
|
id: 'xml_call_0',
|
||||||
|
name: 'view_file',
|
||||||
|
args: { path: '/tmp/test.ts' },
|
||||||
|
});
|
||||||
|
|
||||||
|
// The XML markup must not appear in the saved content or any flushed delta
|
||||||
|
expect(result.content).not.toContain('<invoke');
|
||||||
|
expect(result.content).not.toContain('</invoke>');
|
||||||
|
expect(deltas.join('')).not.toContain('<invoke');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// T9: stall timeout — fake hanging stream fires AbortError after STALL_TIMEOUT_MS.
|
||||||
|
describe('stall timeout (F6)', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`aborts the stream after ${STALL_TIMEOUT_MS}ms with no chunks (stall path)`, async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
|
||||||
|
// Capture the effectiveSignal the adapter passes to streamText so the fake
|
||||||
|
// generator can unblock when the stall fires (matching real ReadableStream
|
||||||
|
// abort behavior: the stream ends rather than throwing into the generator).
|
||||||
|
let capturedSignal: AbortSignal | undefined;
|
||||||
|
vi.mocked(streamText).mockImplementation((opts: Parameters<typeof streamText>[0]) => {
|
||||||
|
capturedSignal = opts.abortSignal as AbortSignal | undefined;
|
||||||
|
return {
|
||||||
|
// Hang until the effective signal fires, then return without emitting
|
||||||
|
// any parts — mirrors how a real fetch stream ends when aborted.
|
||||||
|
fullStream: (async function* () {
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
if (capturedSignal?.aborted) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
capturedSignal?.addEventListener('abort', () => resolve(), { once: true });
|
||||||
|
});
|
||||||
|
})(),
|
||||||
|
// Never resolves; the stall throw happens before usage is awaited.
|
||||||
|
usage: new Promise<never>(() => {}),
|
||||||
|
} as unknown as ReturnType<typeof streamText>;
|
||||||
|
});
|
||||||
|
|
||||||
|
const streamPromise = streamCompletion(
|
||||||
|
fakeCtx,
|
||||||
|
'test-model',
|
||||||
|
[{ role: 'user', content: 'hang' }],
|
||||||
|
{ tools: null },
|
||||||
|
() => {},
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Attach the rejection handler BEFORE advancing timers so the rejection is
|
||||||
|
// never unhandled (Node emits PromiseRejectionHandledWarning otherwise).
|
||||||
|
const assertion = expect(streamPromise).rejects.toMatchObject({ name: 'AbortError' });
|
||||||
|
|
||||||
|
// Advance past the stall deadline — the stallAc fires, the hanging generator
|
||||||
|
// resolves, the post-loop check sees stallAc.signal.aborted and throws.
|
||||||
|
await vi.advanceTimersByTimeAsync(STALL_TIMEOUT_MS);
|
||||||
|
|
||||||
|
await assertion;
|
||||||
|
});
|
||||||
|
|
||||||
|
// T10: regression pin — the original post-loop signal check for user-initiated
|
||||||
|
// abort must still fire correctly after the stall logic was added.
|
||||||
|
it('throws AbortError when the inbound signal is aborted (user-abort regression pin)', async () => {
|
||||||
|
const ac = new AbortController();
|
||||||
|
ac.abort();
|
||||||
|
|
||||||
|
vi.mocked(streamText).mockReturnValue({
|
||||||
|
fullStream: (async function* () {
|
||||||
|
// Yield nothing — stream ends immediately after user abort is already set
|
||||||
|
})(),
|
||||||
|
usage: Promise.resolve({ inputTokens: 0, outputTokens: 0 }),
|
||||||
|
} as unknown as ReturnType<typeof streamText>);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
streamCompletion(
|
||||||
|
fakeCtx,
|
||||||
|
'test-model',
|
||||||
|
[{ role: 'user', content: 'aborted' }],
|
||||||
|
{ tools: null },
|
||||||
|
() => {},
|
||||||
|
undefined,
|
||||||
|
ac.signal,
|
||||||
|
),
|
||||||
|
).rejects.toMatchObject({ name: 'AbortError' });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,179 +1,9 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import {
|
import {
|
||||||
parseXmlToolCall,
|
|
||||||
parseInvokeToolCall,
|
|
||||||
partialXmlOpenerStart,
|
|
||||||
extractToolCallBlocks,
|
extractToolCallBlocks,
|
||||||
stripToolMarkup,
|
stripToolMarkup,
|
||||||
XML_TOOL_OPEN,
|
|
||||||
XML_TOOL_CLOSE,
|
|
||||||
INVOKE_TOOL_OPEN,
|
|
||||||
INVOKE_TOOL_CLOSE,
|
|
||||||
} from '../inference/tool-call-parser.js';
|
} from '../inference/tool-call-parser.js';
|
||||||
|
|
||||||
// ── Ported from xml-parser.test.ts ───────────────────────────────────────
|
|
||||||
|
|
||||||
describe('parseXmlToolCall (Qwen/Hermes <tool_call>)', () => {
|
|
||||||
it('parses a well-formed single-parameter call', () => {
|
|
||||||
const block = '<tool_call><function=view_file><parameter=path>/tmp/foo</parameter></function></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('parses multi-parameter call', () => {
|
|
||||||
const block = '<tool_call><function=grep><parameter=pattern>foo</parameter><parameter=path>src/</parameter></function></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toEqual({
|
|
||||||
name: 'grep',
|
|
||||||
args: { pattern: 'foo', path: 'src/' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('JSON-parses numeric parameter values', () => {
|
|
||||||
const block = '<tool_call><function=foo><parameter=count>42</parameter></function></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toEqual({ name: 'foo', args: { count: 42 } });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('tolerates whitespace around = in function (v1.13.16 tightening)', () => {
|
|
||||||
const block = '<tool_call><function = view_file><parameter=path>/tmp/foo</parameter></function></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('tolerates whitespace around = in parameter (v1.13.16 tightening)', () => {
|
|
||||||
const block = '<tool_call><function=view_file><parameter = path>/tmp/foo</parameter></function></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns null when function name is missing', () => {
|
|
||||||
const block = '<tool_call><parameter=path>/tmp/foo</parameter></tool_call>';
|
|
||||||
expect(parseXmlToolCall(block)).toBeNull();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('parseInvokeToolCall (Anthropic <invoke>) — v1.13.16', () => {
|
|
||||||
it('parses a well-formed single-parameter call (spec case 1)', () => {
|
|
||||||
const block = '<invoke name="view_file"><parameter name="path">/tmp/foo</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('parses a multi-parameter call (spec case 2)', () => {
|
|
||||||
const block = '<invoke name="grep"><parameter name="pattern">foo</parameter><parameter name="path">src/</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'grep',
|
|
||||||
args: { pattern: 'foo', path: 'src/' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('tolerates newlines and spaces in attributes (spec case 3)', () => {
|
|
||||||
const block = `<invoke
|
|
||||||
name="view_file"
|
|
||||||
>
|
|
||||||
<parameter
|
|
||||||
name="path"
|
|
||||||
>/tmp/foo</parameter>
|
|
||||||
</invoke>`;
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('parses a call whose name is not a registered BooCode tool (spec case 4)', () => {
|
|
||||||
const block = '<invoke name="read_file"><parameter name="path">/tmp/foo</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'read_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('supports single-quoted attribute values', () => {
|
|
||||||
const block = "<invoke name='view_file'><parameter name='path'>/tmp/foo</parameter></invoke>";
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('JSON-parses numeric parameter values', () => {
|
|
||||||
const block = '<invoke name="foo"><parameter name="count">42</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({ name: 'foo', args: { count: 42 } });
|
|
||||||
});
|
|
||||||
|
|
||||||
it('tolerates spaces around = inside name attribute', () => {
|
|
||||||
const block = '<invoke name = "view_file"><parameter name = "path">/tmp/foo</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toEqual({
|
|
||||||
name: 'view_file',
|
|
||||||
args: { path: '/tmp/foo' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns null when name attribute is missing', () => {
|
|
||||||
const block = '<invoke><parameter name="path">/tmp/foo</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns null when name attribute is empty', () => {
|
|
||||||
const block = '<invoke name=""><parameter name="path">/tmp/foo</parameter></invoke>';
|
|
||||||
expect(parseInvokeToolCall(block)).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('exports the expected delimiters', () => {
|
|
||||||
expect(INVOKE_TOOL_OPEN).toBe('<invoke');
|
|
||||||
expect(INVOKE_TOOL_CLOSE).toBe('</invoke>');
|
|
||||||
expect(XML_TOOL_OPEN).toBe('<tool_call>');
|
|
||||||
expect(XML_TOOL_CLOSE).toBe('</tool_call>');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('partialXmlOpenerStart (v1.13.16 — both flavors)', () => {
|
|
||||||
it('returns -1 when the buffer is empty', () => {
|
|
||||||
expect(partialXmlOpenerStart('')).toBe(-1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns -1 when the buffer has no openers', () => {
|
|
||||||
expect(partialXmlOpenerStart('plain prose, no markup')).toBe(-1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns the index of a complete <tool_call> opener (existing)', () => {
|
|
||||||
expect(partialXmlOpenerStart('prose <tool_call>more')).toBe(6);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns the index of a complete <invoke opener (v1.13.16)', () => {
|
|
||||||
expect(partialXmlOpenerStart('prose <invoke name=')).toBe(6);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('holds a partial <tool_ prefix at end of buffer', () => {
|
|
||||||
expect(partialXmlOpenerStart('text <tool_')).toBe(5);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('holds a partial <invo prefix at end of buffer (v1.13.16)', () => {
|
|
||||||
expect(partialXmlOpenerStart('text <invo')).toBe(5);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('holds a bare < at end of buffer', () => {
|
|
||||||
expect(partialXmlOpenerStart('text <')).toBe(5);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns -1 when < is followed by non-opener text', () => {
|
|
||||||
expect(partialXmlOpenerStart('text <unknown>')).toBe(-1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns the earliest opener when both flavors are present', () => {
|
|
||||||
expect(partialXmlOpenerStart('xxx <tool_call>YYY <invoke>')).toBe(4);
|
|
||||||
expect(partialXmlOpenerStart('xxx <invoke>YYY <tool_call>')).toBe(4);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('extractToolCallBlocks (v1.13.16 — unified extraction)', () => {
|
describe('extractToolCallBlocks (v1.13.16 — unified extraction)', () => {
|
||||||
it('extracts a single <invoke> block (spec case 1)', () => {
|
it('extracts a single <invoke> block (spec case 1)', () => {
|
||||||
const input = '<invoke name="view_file"><parameter name="path">/tmp/foo</parameter></invoke>';
|
const input = '<invoke name="view_file"><parameter name="path">/tmp/foo</parameter></invoke>';
|
||||||
@@ -341,11 +171,3 @@ describe('stripToolMarkup', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('delimiter constants', () => {
|
|
||||||
it('exports the expected delimiters', () => {
|
|
||||||
expect(INVOKE_TOOL_OPEN).toBe('<invoke');
|
|
||||||
expect(INVOKE_TOOL_CLOSE).toBe('</invoke>');
|
|
||||||
expect(XML_TOOL_OPEN).toBe('<tool_call>');
|
|
||||||
expect(XML_TOOL_CLOSE).toBe('</tool_call>');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
554
apps/server/src/services/git_diff.ts
Normal file
554
apps/server/src/services/git_diff.ts
Normal file
@@ -0,0 +1,554 @@
|
|||||||
|
import { execFile } from 'node:child_process';
|
||||||
|
import { promisify } from 'node:util';
|
||||||
|
import { stat, realpath } from 'node:fs/promises';
|
||||||
|
import { isAbsolute, join, resolve, sep } from 'node:path';
|
||||||
|
|
||||||
|
const execFileAsync = promisify(execFile);
|
||||||
|
|
||||||
|
const GIT_TIMEOUT_MS = 30_000;
|
||||||
|
const GIT_MAX_BUFFER = 10 * 1024 * 1024; // 10MB
|
||||||
|
const FILE_DIFF_CAP = 512 * 1024; // 512KB per-file display cap
|
||||||
|
|
||||||
|
export type GitDiffMode = 'uncommitted' | 'committed';
|
||||||
|
export type ChangeType = 'added' | 'modified' | 'deleted' | 'renamed' | 'untracked';
|
||||||
|
|
||||||
|
export interface GitDiffFile {
|
||||||
|
path: string;
|
||||||
|
old_path: string | null;
|
||||||
|
change_type: ChangeType;
|
||||||
|
added_lines: number;
|
||||||
|
removed_lines: number;
|
||||||
|
staged: boolean;
|
||||||
|
diff_body: string | null; // null when is_binary or is_too_large
|
||||||
|
is_binary: boolean;
|
||||||
|
is_too_large: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GitDiffResult {
|
||||||
|
mode: GitDiffMode;
|
||||||
|
base_label: string | null;
|
||||||
|
in_progress_op: string | null;
|
||||||
|
files: GitDiffFile[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// runGit with 30s deadline and 10MB buffer for diff payloads. Returns null on
|
||||||
|
// any failure so callers can degrade gracefully without surfacing git errors.
|
||||||
|
async function runGit(args: string[], cwd: string): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const { stdout } = await execFileAsync('git', args, {
|
||||||
|
cwd,
|
||||||
|
timeout: GIT_TIMEOUT_MS,
|
||||||
|
windowsHide: true,
|
||||||
|
maxBuffer: GIT_MAX_BUFFER,
|
||||||
|
});
|
||||||
|
return stdout.toString();
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Pure helpers (unit-testable without spawning git) ──────────────────────
|
||||||
|
|
||||||
|
/** Parses a single `git diff --name-status` output line. Returns null on garbage. */
|
||||||
|
function parseNameStatusLine(line: string): {
|
||||||
|
path: string;
|
||||||
|
old_path: string | null;
|
||||||
|
change_type: ChangeType;
|
||||||
|
} | null {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed) return null;
|
||||||
|
const parts = trimmed.split('\t');
|
||||||
|
if (parts.length < 2) return null;
|
||||||
|
const code = parts[0] ?? '';
|
||||||
|
// Rename: R<score>\told\tnew Copy: C<score>\told\tnew
|
||||||
|
if (code.startsWith('R') || code.startsWith('C')) {
|
||||||
|
if (parts.length < 3) return null;
|
||||||
|
return { path: parts[2] ?? '', old_path: parts[1] ?? null, change_type: 'renamed' };
|
||||||
|
}
|
||||||
|
const path = parts[1] ?? '';
|
||||||
|
if (!path) return null;
|
||||||
|
switch (code[0]) {
|
||||||
|
case 'A': return { path, old_path: null, change_type: 'added' };
|
||||||
|
case 'M':
|
||||||
|
case 'T': // type changed
|
||||||
|
case 'U': // unmerged
|
||||||
|
return { path, old_path: null, change_type: 'modified' };
|
||||||
|
case 'D': return { path, old_path: null, change_type: 'deleted' };
|
||||||
|
default: return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parses multi-line `git diff --name-status` output into a file list. */
|
||||||
|
export function parseNameStatus(output: string): {
|
||||||
|
path: string;
|
||||||
|
old_path: string | null;
|
||||||
|
change_type: ChangeType;
|
||||||
|
}[] {
|
||||||
|
return output
|
||||||
|
.split('\n')
|
||||||
|
.map((l) => parseNameStatusLine(l))
|
||||||
|
.filter((x): x is NonNullable<typeof x> => x !== null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parses a single `git diff --numstat` output line. */
|
||||||
|
export function parseNumStatLine(line: string): {
|
||||||
|
path: string;
|
||||||
|
added: number;
|
||||||
|
removed: number;
|
||||||
|
binary: boolean;
|
||||||
|
} | null {
|
||||||
|
const parts = line.trim().split('\t');
|
||||||
|
if (parts.length < 3) return null;
|
||||||
|
const [added, removed, path] = parts;
|
||||||
|
if (!path) return null;
|
||||||
|
if (added === '-' && removed === '-') {
|
||||||
|
return { path, added: 0, removed: 0, binary: true };
|
||||||
|
}
|
||||||
|
const a = parseInt(added ?? '', 10);
|
||||||
|
const r = parseInt(removed ?? '', 10);
|
||||||
|
if (isNaN(a) || isNaN(r)) return null;
|
||||||
|
return { path, added: a, removed: r, binary: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Splits a unified diff text into per-file bodies keyed by current path. */
|
||||||
|
export function splitDiffByFile(diffText: string): Map<string, string> {
|
||||||
|
const result = new Map<string, string>();
|
||||||
|
if (!diffText.trim()) return result;
|
||||||
|
|
||||||
|
// Split at each "diff --git" header (lookahead keeps the header with its section)
|
||||||
|
const sections = diffText.split(/(?=^diff --git )/m);
|
||||||
|
for (const section of sections) {
|
||||||
|
if (!section.trim()) continue;
|
||||||
|
|
||||||
|
// Current path: prefer "+++ b/<path>" (absent for pure renames / deleted files)
|
||||||
|
const pppMatch = section.match(/^\+{3} b\/(.+)$/m);
|
||||||
|
if (pppMatch) {
|
||||||
|
result.set((pppMatch[1] ?? '').trim(), section);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deleted file: "--- a/<path>" with "+++ /dev/null"
|
||||||
|
const mmmMatch = section.match(/^-{3} a\/(.+)$/m);
|
||||||
|
if (mmmMatch) {
|
||||||
|
const p = (mmmMatch[1] ?? '').trim();
|
||||||
|
if (p && p !== '/dev/null') {
|
||||||
|
result.set(p, section);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pure rename with no content change: extract from "diff --git a/... b/..."
|
||||||
|
// Take everything after the last " b/" on that line.
|
||||||
|
const gitLineMatch = section.match(/^diff --git a\/.+ b\/(.+)$/m);
|
||||||
|
if (gitLineMatch) {
|
||||||
|
result.set((gitLineMatch[1] ?? '').trim(), section);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Classifies a diff body segment as diff | binary | too_large. */
|
||||||
|
export function classifyDiffBody(body: string, cap = FILE_DIFF_CAP): 'diff' | 'binary' | 'too_large' {
|
||||||
|
if (/^Binary files /m.test(body)) return 'binary';
|
||||||
|
if (body.length > cap) return 'too_large';
|
||||||
|
return 'diff';
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the auto-selected diff mode based on dirty state. */
|
||||||
|
export function autoSelectMode(isDirty: boolean): GitDiffMode {
|
||||||
|
return isDirty ? 'uncommitted' : 'committed';
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns true when at least one file is staged (commit is possible). */
|
||||||
|
export function canCommit(files: GitDiffFile[]): boolean {
|
||||||
|
return files.some((f) => f.staged);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns true when the working tree has uncommitted changes (staged or unstaged). */
|
||||||
|
export async function isRepoDirty(cwd: string): Promise<boolean> {
|
||||||
|
const gitRoot = await resolveGitRoot(cwd);
|
||||||
|
if (!gitRoot) return false;
|
||||||
|
const out = await runGit(['status', '--porcelain'], gitRoot);
|
||||||
|
if (out === null) return true; // can't determine — assume dirty
|
||||||
|
return out.trim().length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async per-file symlink-escape guard (FIX 3 / D-4). Resolves the real path of
|
||||||
|
* the target (if it already exists on disk) and rejects when it falls outside
|
||||||
|
* the repo root. Non-existent paths (new files being staged) are allowed — there
|
||||||
|
* is no symlink to follow when the file hasn't been created yet.
|
||||||
|
*/
|
||||||
|
export async function checkSymlinkEscape(repoRoot: string, filePath: string): Promise<void> {
|
||||||
|
const resolved = resolve(repoRoot, filePath);
|
||||||
|
let real: string;
|
||||||
|
try {
|
||||||
|
real = await realpath(resolved);
|
||||||
|
} catch {
|
||||||
|
// File doesn't exist yet — no symlink to resolve, safe to proceed.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (real !== repoRoot && !real.startsWith(repoRoot + sep)) {
|
||||||
|
throw new GitWriteError(`path escapes repository root via symlink: ${filePath}`, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Async helpers ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** Resolves the base ref for Committed mode with fallback chain. */
|
||||||
|
export async function resolveCommittedBase(
|
||||||
|
cwd: string,
|
||||||
|
): Promise<{ base: string | null; label: string }> {
|
||||||
|
// 1. Tracking branch (@{upstream})
|
||||||
|
const upstream = await runGit(
|
||||||
|
['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'],
|
||||||
|
cwd,
|
||||||
|
);
|
||||||
|
if (upstream !== null) {
|
||||||
|
const trimmed = upstream.trim();
|
||||||
|
if (trimmed && !trimmed.includes('fatal')) {
|
||||||
|
return { base: trimmed, label: trimmed };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. origin/HEAD (default branch)
|
||||||
|
const originHead = await runGit(['rev-parse', '--abbrev-ref', 'origin/HEAD'], cwd);
|
||||||
|
if (originHead !== null) {
|
||||||
|
const trimmed = originHead.trim();
|
||||||
|
if (trimmed && !trimmed.includes('fatal') && !trimmed.includes('unknown')) {
|
||||||
|
return { base: trimmed, label: trimmed };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { base: null, label: 'uncommitted (no base found)' };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Detects in-progress git operations via .git sentinel files/dirs. */
|
||||||
|
export async function detectInProgress(repoRoot: string): Promise<string | null> {
|
||||||
|
const fileChecks: [string, string][] = [
|
||||||
|
['MERGE_HEAD', 'merge'],
|
||||||
|
['CHERRY_PICK_HEAD', 'cherry-pick'],
|
||||||
|
['BISECT_LOG', 'bisect'],
|
||||||
|
];
|
||||||
|
for (const [file, op] of fileChecks) {
|
||||||
|
try {
|
||||||
|
await stat(join(repoRoot, '.git', file));
|
||||||
|
return op;
|
||||||
|
} catch {
|
||||||
|
// sentinel not present — continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const dir of ['rebase-merge', 'rebase-apply']) {
|
||||||
|
try {
|
||||||
|
await stat(join(repoRoot, '.git', dir));
|
||||||
|
return 'rebase';
|
||||||
|
} catch {
|
||||||
|
// not present — continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Read logic ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** Resolves the git work-tree root for the given path. Returns null if not a repo. */
|
||||||
|
async function resolveGitRoot(cwd: string): Promise<string | null> {
|
||||||
|
const out = await runGit(['rev-parse', '--show-toplevel'], cwd);
|
||||||
|
return out !== null ? out.trim() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildNumstatMap(
|
||||||
|
output: string,
|
||||||
|
): Map<string, { added: number; removed: number; binary: boolean }> {
|
||||||
|
const map = new Map<string, { added: number; removed: number; binary: boolean }>();
|
||||||
|
for (const line of output.split('\n')) {
|
||||||
|
const parsed = parseNumStatLine(line);
|
||||||
|
if (parsed) map.set(parsed.path, { added: parsed.added, removed: parsed.removed, binary: parsed.binary });
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getUncommittedDiff(
|
||||||
|
gitRoot: string,
|
||||||
|
inProgress: string | null,
|
||||||
|
): Promise<GitDiffResult> {
|
||||||
|
const hasCommits = (await runGit(['rev-parse', '--verify', 'HEAD'], gitRoot)) !== null;
|
||||||
|
|
||||||
|
const [nameStatusOut, cachedNameStatusOut, untrackedOut, numstatOut, diffOut, cachedDiffOut] =
|
||||||
|
await Promise.all([
|
||||||
|
hasCommits
|
||||||
|
? runGit(['diff', '--name-status', 'HEAD'], gitRoot)
|
||||||
|
: Promise.resolve(''),
|
||||||
|
hasCommits
|
||||||
|
? runGit(['diff', '--cached', '--name-status', 'HEAD'], gitRoot)
|
||||||
|
: runGit(['diff', '--cached', '--name-status'], gitRoot),
|
||||||
|
runGit(['ls-files', '--others', '--exclude-standard'], gitRoot),
|
||||||
|
hasCommits ? runGit(['diff', '--numstat', 'HEAD'], gitRoot) : Promise.resolve(''),
|
||||||
|
hasCommits ? runGit(['diff', 'HEAD'], gitRoot) : Promise.resolve(''),
|
||||||
|
hasCommits
|
||||||
|
? runGit(['diff', '--cached', 'HEAD'], gitRoot)
|
||||||
|
: runGit(['diff', '--cached'], gitRoot),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const allChanged = parseNameStatus(nameStatusOut ?? '');
|
||||||
|
const stagedSet = new Set(
|
||||||
|
parseNameStatus(cachedNameStatusOut ?? '').map((f) => f.path),
|
||||||
|
);
|
||||||
|
const untracked = (untrackedOut ?? '').split('\n').filter(Boolean);
|
||||||
|
|
||||||
|
const numstatMap = buildNumstatMap(numstatOut ?? '');
|
||||||
|
|
||||||
|
// Merge unstaged and staged diff maps
|
||||||
|
const diffMap = splitDiffByFile(diffOut ?? '');
|
||||||
|
const cachedDiffMap = splitDiffByFile(cachedDiffOut ?? '');
|
||||||
|
// Staged-only files won't be in diffOut; supplement from cachedDiffMap
|
||||||
|
for (const [k, v] of cachedDiffMap) {
|
||||||
|
if (!diffMap.has(k)) diffMap.set(k, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
const files: GitDiffFile[] = [];
|
||||||
|
|
||||||
|
for (const entry of allChanged) {
|
||||||
|
const ns = numstatMap.get(entry.path);
|
||||||
|
const body = diffMap.get(entry.path) ?? null;
|
||||||
|
const kind = body !== null ? classifyDiffBody(body) : ns?.binary ? 'binary' : 'diff';
|
||||||
|
files.push({
|
||||||
|
path: entry.path,
|
||||||
|
old_path: entry.old_path,
|
||||||
|
change_type: entry.change_type,
|
||||||
|
added_lines: ns?.added ?? 0,
|
||||||
|
removed_lines: ns?.removed ?? 0,
|
||||||
|
staged: stagedSet.has(entry.path),
|
||||||
|
diff_body: kind === 'diff' ? body : null,
|
||||||
|
is_binary: kind === 'binary',
|
||||||
|
is_too_large: kind === 'too_large',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const p of untracked) {
|
||||||
|
files.push({
|
||||||
|
path: p,
|
||||||
|
old_path: null,
|
||||||
|
change_type: 'untracked',
|
||||||
|
added_lines: 0,
|
||||||
|
removed_lines: 0,
|
||||||
|
staged: false,
|
||||||
|
diff_body: null,
|
||||||
|
is_binary: false,
|
||||||
|
is_too_large: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { mode: 'uncommitted', base_label: null, in_progress_op: inProgress, files };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getCommittedDiff(
|
||||||
|
gitRoot: string,
|
||||||
|
base: string,
|
||||||
|
label: string,
|
||||||
|
inProgress: string | null,
|
||||||
|
): Promise<GitDiffResult> {
|
||||||
|
const [nameStatusOut, numstatOut, diffOut] = await Promise.all([
|
||||||
|
runGit(['diff', '--name-status', base, 'HEAD'], gitRoot),
|
||||||
|
runGit(['diff', '--numstat', base, 'HEAD'], gitRoot),
|
||||||
|
runGit(['diff', base, 'HEAD'], gitRoot),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const allChanged = parseNameStatus(nameStatusOut ?? '');
|
||||||
|
const numstatMap = buildNumstatMap(numstatOut ?? '');
|
||||||
|
const diffMap = splitDiffByFile(diffOut ?? '');
|
||||||
|
|
||||||
|
const files: GitDiffFile[] = allChanged.map((entry) => {
|
||||||
|
const ns = numstatMap.get(entry.path);
|
||||||
|
const body = diffMap.get(entry.path) ?? null;
|
||||||
|
const kind = body !== null ? classifyDiffBody(body) : ns?.binary ? 'binary' : 'diff';
|
||||||
|
return {
|
||||||
|
path: entry.path,
|
||||||
|
old_path: entry.old_path,
|
||||||
|
change_type: entry.change_type,
|
||||||
|
added_lines: ns?.added ?? 0,
|
||||||
|
removed_lines: ns?.removed ?? 0,
|
||||||
|
staged: false, // staged concept does not apply in committed mode
|
||||||
|
diff_body: kind === 'diff' ? body : null,
|
||||||
|
is_binary: kind === 'binary',
|
||||||
|
is_too_large: kind === 'too_large',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return { mode: 'committed', base_label: label, in_progress_op: inProgress, files };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the structured git diff for the given directory and mode, or null if
|
||||||
|
* the directory is not a git repository. On a null committed-mode base, falls
|
||||||
|
* back to uncommitted and labels the result accordingly.
|
||||||
|
*/
|
||||||
|
export async function getGitDiff(cwd: string, mode: GitDiffMode): Promise<GitDiffResult | null> {
|
||||||
|
const gitRoot = await resolveGitRoot(cwd);
|
||||||
|
if (!gitRoot) return null;
|
||||||
|
|
||||||
|
const inProgress = await detectInProgress(gitRoot);
|
||||||
|
|
||||||
|
if (mode === 'uncommitted') {
|
||||||
|
return getUncommittedDiff(gitRoot, inProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { base, label } = await resolveCommittedBase(gitRoot);
|
||||||
|
if (!base) {
|
||||||
|
// Fall back to uncommitted with a descriptive label
|
||||||
|
const result = await getUncommittedDiff(gitRoot, inProgress);
|
||||||
|
return { ...result, base_label: label };
|
||||||
|
}
|
||||||
|
return getCommittedDiff(gitRoot, base, label, inProgress);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Phase 2: Write helpers ─────────────────────────────────────────────────
|
||||||
|
|
||||||
|
// Fallback identity matching project_bootstrap.ts constants.
|
||||||
|
const GIT_USER_NAME = 'indifferentketchup';
|
||||||
|
const GIT_USER_EMAIL = 'samkintop@gmail.com';
|
||||||
|
|
||||||
|
export class GitWriteError extends Error {
|
||||||
|
constructor(
|
||||||
|
message: string,
|
||||||
|
public readonly busy: boolean,
|
||||||
|
) {
|
||||||
|
super(message);
|
||||||
|
this.name = 'GitWriteError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates a per-file path argument for write operations.
|
||||||
|
* Rejects flag injection (leading `-`), repo-root discard (`.`), absolute
|
||||||
|
* paths, and `..` traversal without requiring the file to exist on disk.
|
||||||
|
*/
|
||||||
|
export function validateWritePath(repoRoot: string, filePath: string): void {
|
||||||
|
if (!filePath || typeof filePath !== 'string' || filePath.trim() === '') {
|
||||||
|
throw new GitWriteError('path is required', false);
|
||||||
|
}
|
||||||
|
if (filePath.startsWith('-')) {
|
||||||
|
throw new GitWriteError(`invalid path (flag injection): ${filePath}`, false);
|
||||||
|
}
|
||||||
|
if (filePath === '.') {
|
||||||
|
throw new GitWriteError('cannot operate on repository root (.)', false);
|
||||||
|
}
|
||||||
|
if (isAbsolute(filePath)) {
|
||||||
|
throw new GitWriteError(`path must be relative: ${filePath}`, false);
|
||||||
|
}
|
||||||
|
const resolved = resolve(repoRoot, filePath);
|
||||||
|
if (resolved === repoRoot || !resolved.startsWith(repoRoot + sep)) {
|
||||||
|
throw new GitWriteError(`path escapes repository root: ${filePath}`, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Reads git config user.name/email, falling back to bootstrap constants. */
|
||||||
|
export async function deriveCommitIdentity(
|
||||||
|
repoRoot: string,
|
||||||
|
): Promise<{ name: string; email: string }> {
|
||||||
|
const [nameOut, emailOut] = await Promise.all([
|
||||||
|
runGit(['config', 'user.name'], repoRoot),
|
||||||
|
runGit(['config', 'user.email'], repoRoot),
|
||||||
|
]);
|
||||||
|
return {
|
||||||
|
name: nameOut?.trim() || GIT_USER_NAME,
|
||||||
|
email: emailOut?.trim() || GIT_USER_EMAIL,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Runs a git write operation, propagating errors. Throws GitWriteError. */
|
||||||
|
async function runGitWrite(args: string[], cwd: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await execFileAsync('git', args, { cwd, timeout: GIT_TIMEOUT_MS, windowsHide: true });
|
||||||
|
} catch (err) {
|
||||||
|
const msg = err instanceof Error ? err.message : String(err);
|
||||||
|
const busy = msg.includes('index.lock') || msg.includes('Another git process');
|
||||||
|
throw new GitWriteError(busy ? 'repository is busy, try again' : msg, busy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Stages the given files (`git add -- <files>`). */
|
||||||
|
export async function stageFiles(repoRoot: string, files: string[]): Promise<void> {
|
||||||
|
for (const f of files) {
|
||||||
|
validateWritePath(repoRoot, f);
|
||||||
|
await checkSymlinkEscape(repoRoot, f);
|
||||||
|
}
|
||||||
|
await runGitWrite(['add', '--', ...files], repoRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Unstages the given files (`git restore --staged -- <files>`). */
|
||||||
|
export async function unstageFiles(repoRoot: string, files: string[]): Promise<void> {
|
||||||
|
for (const f of files) {
|
||||||
|
validateWritePath(repoRoot, f);
|
||||||
|
await checkSymlinkEscape(repoRoot, f);
|
||||||
|
}
|
||||||
|
await runGitWrite(['restore', '--staged', '--', ...files], repoRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Commits staged files with a server-derived identity. */
|
||||||
|
export async function commitFiles(
|
||||||
|
repoRoot: string,
|
||||||
|
message: string,
|
||||||
|
files?: string[],
|
||||||
|
): Promise<void> {
|
||||||
|
if (files && files.length > 0) {
|
||||||
|
for (const f of files) {
|
||||||
|
validateWritePath(repoRoot, f);
|
||||||
|
await checkSymlinkEscape(repoRoot, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const id = await deriveCommitIdentity(repoRoot);
|
||||||
|
const args = ['-c', `user.name=${id.name}`, '-c', `user.email=${id.email}`, 'commit', '-m', message];
|
||||||
|
if (files && files.length > 0) args.push('--', ...files);
|
||||||
|
await runGitWrite(args, repoRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DiscardFileInfo {
|
||||||
|
path: string;
|
||||||
|
change_type: string;
|
||||||
|
staged: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discards changes for the given files.
|
||||||
|
* - Untracked files: `git clean -f -- <path>`
|
||||||
|
* - Staged additions (new file staged, no HEAD version): unstage then clean
|
||||||
|
* - All other tracked files: `git restore HEAD -- <path>` (undoes staged + unstaged)
|
||||||
|
*/
|
||||||
|
export async function discardFiles(repoRoot: string, files: DiscardFileInfo[]): Promise<void> {
|
||||||
|
for (const { path } of files) {
|
||||||
|
validateWritePath(repoRoot, path);
|
||||||
|
await checkSymlinkEscape(repoRoot, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
const untracked: string[] = [];
|
||||||
|
const stagedAdditions: string[] = [];
|
||||||
|
const tracked: string[] = [];
|
||||||
|
|
||||||
|
for (const f of files) {
|
||||||
|
if (f.change_type === 'untracked') {
|
||||||
|
untracked.push(f.path);
|
||||||
|
} else if (f.change_type === 'added' && f.staged) {
|
||||||
|
stagedAdditions.push(f.path);
|
||||||
|
} else {
|
||||||
|
tracked.push(f.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore tracked files from HEAD (handles staged + unstaged modifications/deletions).
|
||||||
|
// git checkout HEAD -- <file> is the most portable form: resets index + worktree.
|
||||||
|
if (tracked.length > 0) {
|
||||||
|
await runGitWrite(['checkout', 'HEAD', '--', ...tracked], repoRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Staged additions: unstage first, then remove from working tree.
|
||||||
|
for (const p of stagedAdditions) {
|
||||||
|
await runGitWrite(['restore', '--staged', '--', p], repoRoot);
|
||||||
|
await runGitWrite(['clean', '-f', '--', p], repoRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Untracked files: clean (hard delete).
|
||||||
|
if (untracked.length > 0) {
|
||||||
|
await runGitWrite(['clean', '-f', '--', ...untracked], repoRoot);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// Pure classifier for errors thrown from the fullStream loop. Establishes the
|
||||||
|
// retry seam for when llama-swap gains restart-in-place-with-clear-partial
|
||||||
|
// semantics. No retry is performed today (partial-stream re-emit is
|
||||||
|
// non-idempotent at single-local-instance scale).
|
||||||
|
export type StreamErrorKind = 'stall' | 'transient' | 'non-retryable';
|
||||||
|
|
||||||
|
export function classifyStreamError(err: unknown): StreamErrorKind {
|
||||||
|
if (err instanceof Error && err.name === 'AbortError') {
|
||||||
|
return 'stall';
|
||||||
|
}
|
||||||
|
if (err != null && typeof err === 'object') {
|
||||||
|
const status = (err as Record<string, unknown>).status;
|
||||||
|
if (typeof status === 'number' && status >= 500 && status < 600) {
|
||||||
|
return 'transient';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'non-retryable';
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import type { Agent, ToolCall } from '../../types/api.js';
|
|||||||
import type { ToolJsonSchema } from '../tools.js';
|
import type { ToolJsonSchema } from '../tools.js';
|
||||||
import type { OpenAiMessage } from './payload.js';
|
import type { OpenAiMessage } from './payload.js';
|
||||||
import { extractToolCallBlocks } from './tool-call-parser.js';
|
import { extractToolCallBlocks } from './tool-call-parser.js';
|
||||||
|
import { classifyStreamError } from './stream-error-classifier.js';
|
||||||
import type { StreamResult } from './types.js';
|
import type { StreamResult } from './types.js';
|
||||||
import { upstreamModel } from './provider.js';
|
import { upstreamModel } from './provider.js';
|
||||||
import {
|
import {
|
||||||
@@ -193,6 +194,10 @@ function buildAiTools(schemas: ToolJsonSchema[]): Record<string, ReturnType<type
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// F6: per-chunk stall deadline. Exported so tests can advance fake timers by
|
||||||
|
// exactly this value without hardcoding a magic number.
|
||||||
|
export const STALL_TIMEOUT_MS = 90_000;
|
||||||
|
|
||||||
// v1.10.5 Qwen-coder XML fallback. Some local models (notably qwen3-coder via
|
// v1.10.5 Qwen-coder XML fallback. Some local models (notably qwen3-coder via
|
||||||
// llama-swap) emit tool calls as inline XML inside delta.content rather than
|
// llama-swap) emit tool calls as inline XML inside delta.content rather than
|
||||||
// the structured tool_calls field. We extract them out of the streamed text
|
// the structured tool_calls field. We extract them out of the streamed text
|
||||||
@@ -267,6 +272,22 @@ export async function streamCompletion(
|
|||||||
// before this. They now go through the same extraBody path as the new params.
|
// before this. They now go through the same extraBody path as the new params.
|
||||||
const samplerBody = buildSamplerProviderOptions(opts);
|
const samplerBody = buildSamplerProviderOptions(opts);
|
||||||
|
|
||||||
|
// F6: per-chunk stall deadline. If the model stops emitting chunks for
|
||||||
|
// STALL_TIMEOUT_MS the stallAc fires through AbortSignal.any; the post-loop
|
||||||
|
// abort check below then throws AbortError → handleAbortOrError writes
|
||||||
|
// 'cancelled'. Timer is bumped on every chunk and cleared in the finally.
|
||||||
|
// NO retry: partial-stream re-emit is non-idempotent at single-local-instance
|
||||||
|
// scale; see stream-error-classifier.ts for the future retry seam.
|
||||||
|
const stallAc = new AbortController();
|
||||||
|
let stallTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
const bumpStallTimer = () => {
|
||||||
|
if (stallTimer !== null) clearTimeout(stallTimer);
|
||||||
|
stallTimer = setTimeout(() => stallAc.abort(), STALL_TIMEOUT_MS);
|
||||||
|
};
|
||||||
|
const effectiveSignal = signal
|
||||||
|
? AbortSignal.any([signal, stallAc.signal])
|
||||||
|
: stallAc.signal;
|
||||||
|
|
||||||
const result = streamText({
|
const result = streamText({
|
||||||
model: upstreamModel(ctx.config, model, agent ?? null),
|
model: upstreamModel(ctx.config, model, agent ?? null),
|
||||||
messages: aiMessages,
|
messages: aiMessages,
|
||||||
@@ -277,7 +298,7 @@ export async function streamCompletion(
|
|||||||
...(typeof opts.top_p === 'number' ? { topP: opts.top_p } : {}),
|
...(typeof opts.top_p === 'number' ? { topP: opts.top_p } : {}),
|
||||||
...(typeof opts.presence_penalty === 'number' ? { presencePenalty: opts.presence_penalty } : {}),
|
...(typeof opts.presence_penalty === 'number' ? { presencePenalty: opts.presence_penalty } : {}),
|
||||||
...(samplerBody ? { providerOptions: { openaiCompatible: samplerBody } } : {}),
|
...(samplerBody ? { providerOptions: { openaiCompatible: samplerBody } } : {}),
|
||||||
abortSignal: signal,
|
abortSignal: effectiveSignal,
|
||||||
});
|
});
|
||||||
|
|
||||||
let content = '';
|
let content = '';
|
||||||
@@ -289,7 +310,11 @@ export async function streamCompletion(
|
|||||||
// same flat list and keep the v1.10.5 synthetic id convention.
|
// same flat list and keep the v1.10.5 synthetic id convention.
|
||||||
const toolCalls: ToolCall[] = [];
|
const toolCalls: ToolCall[] = [];
|
||||||
|
|
||||||
|
bumpStallTimer();
|
||||||
|
|
||||||
|
try {
|
||||||
for await (const part of result.fullStream) {
|
for await (const part of result.fullStream) {
|
||||||
|
bumpStallTimer();
|
||||||
switch (part.type) {
|
switch (part.type) {
|
||||||
case 'text-delta': {
|
case 'text-delta': {
|
||||||
pendingBuffer += part.text;
|
pendingBuffer += part.text;
|
||||||
@@ -297,7 +322,7 @@ export async function streamCompletion(
|
|||||||
// complete <tool_call> or <invoke> block, flushes prose between/around
|
// complete <tool_call> or <invoke> block, flushes prose between/around
|
||||||
// them, holds any partial opener for the next chunk, and silently
|
// them, holds any partial opener for the next chunk, and silently
|
||||||
// drops blocks that fail to parse (matches pre-v1.13.16 behavior).
|
// drops blocks that fail to parse (matches pre-v1.13.16 behavior).
|
||||||
const extracted = extractToolCallBlocks(pendingBuffer);
|
const extracted = extractToolCallBlocks(pendingBuffer, ctx.log);
|
||||||
if (extracted.flushed.length > 0) {
|
if (extracted.flushed.length > 0) {
|
||||||
content += extracted.flushed;
|
content += extracted.flushed;
|
||||||
onDelta(extracted.flushed);
|
onDelta(extracted.flushed);
|
||||||
@@ -339,7 +364,9 @@ export async function streamCompletion(
|
|||||||
}
|
}
|
||||||
case 'error': {
|
case 'error': {
|
||||||
const err = part.error;
|
const err = part.error;
|
||||||
throw err instanceof Error ? err : new Error(String(err));
|
const actualErr = err instanceof Error ? err : new Error(String(err));
|
||||||
|
ctx.log.warn({ kind: classifyStreamError(actualErr) }, 'stream error part');
|
||||||
|
throw actualErr;
|
||||||
}
|
}
|
||||||
// Intentional no-op: start, start-step, text-start, text-end,
|
// Intentional no-op: start, start-step, text-start, text-end,
|
||||||
// reasoning-start, reasoning-end, source, file, tool-input-start,
|
// reasoning-start, reasoning-end, source, file, tool-input-start,
|
||||||
@@ -365,7 +392,8 @@ export async function streamCompletion(
|
|||||||
// Without this throw the row would land as status='complete' with partial
|
// Without this throw the row would land as status='complete' with partial
|
||||||
// content instead of going through handleAbortOrError → status='cancelled'.
|
// content instead of going through handleAbortOrError → status='cancelled'.
|
||||||
// Smoke D caught this in v1.13.1-A — don't refactor it away.
|
// Smoke D caught this in v1.13.1-A — don't refactor it away.
|
||||||
if (signal?.aborted) {
|
// F6: also catch the stall timeout arm (stallAc.signal.aborted).
|
||||||
|
if (signal?.aborted || stallAc.signal.aborted) {
|
||||||
const abortErr = new Error('aborted');
|
const abortErr = new Error('aborted');
|
||||||
abortErr.name = 'AbortError';
|
abortErr.name = 'AbortError';
|
||||||
throw abortErr;
|
throw abortErr;
|
||||||
@@ -402,4 +430,12 @@ export async function streamCompletion(
|
|||||||
completionTokens,
|
completionTokens,
|
||||||
reasoning: reasoningAccumulated,
|
reasoning: reasoningAccumulated,
|
||||||
};
|
};
|
||||||
|
} finally {
|
||||||
|
// Clear the stall timer whether the stream completes normally, throws, or
|
||||||
|
// is aborted — prevents a dangling timer from firing after the turn ends.
|
||||||
|
if (stallTimer !== null) {
|
||||||
|
clearTimeout(stallTimer);
|
||||||
|
stallTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
|
|
||||||
// ── Constants ────────────────────────────────────────────────────────────
|
// ── Constants ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
export const XML_TOOL_OPEN = '<tool_call>';
|
const XML_TOOL_OPEN = '<tool_call>';
|
||||||
export const XML_TOOL_CLOSE = '</tool_call>';
|
const XML_TOOL_CLOSE = '</tool_call>';
|
||||||
export const INVOKE_TOOL_OPEN = '<invoke';
|
const INVOKE_TOOL_OPEN = '<invoke';
|
||||||
export const INVOKE_TOOL_CLOSE = '</invoke>';
|
const INVOKE_TOOL_CLOSE = '</invoke>';
|
||||||
|
|
||||||
// ── Strip patterns ───────────────────────────────────────────────────────
|
// ── Strip patterns ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ export interface ParsedCall {
|
|||||||
const PLACEHOLDER_LITERALS = new Set(['...', 'placeholder', '<path>', '<file>']);
|
const PLACEHOLDER_LITERALS = new Set(['...', 'placeholder', '<path>', '<file>']);
|
||||||
const ANGLE_BRACKET_SENTINEL_RE = /^<[^>]+>$/;
|
const ANGLE_BRACKET_SENTINEL_RE = /^<[^>]+>$/;
|
||||||
|
|
||||||
export function isPlaceholderArgValue(value: unknown): boolean {
|
function isPlaceholderArgValue(value: unknown): boolean {
|
||||||
if (typeof value !== 'string') return false;
|
if (typeof value !== 'string') return false;
|
||||||
const trimmed = value.trim();
|
const trimmed = value.trim();
|
||||||
if (trimmed === '') return true;
|
if (trimmed === '') return true;
|
||||||
@@ -61,17 +61,21 @@ function hasPlaceholderArgs(args: Record<string, unknown>): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function logRejectedPlaceholder(parsed: ParsedCall): void {
|
type MinLogger = { debug(obj: object, msg: string): void };
|
||||||
console.debug(
|
|
||||||
|
function logRejectedPlaceholder(parsed: ParsedCall, log?: MinLogger): void {
|
||||||
|
if (log) {
|
||||||
|
log.debug(
|
||||||
{ toolName: parsed.name, args: parsed.args },
|
{ toolName: parsed.name, args: parsed.args },
|
||||||
'rejected placeholder tool call at parse time',
|
'rejected placeholder tool call at parse time',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const QWEN_FUNCTION_RE = /<function\s*=\s*([^>\s]+)\s*>/;
|
const QWEN_FUNCTION_RE = /<function\s*=\s*([^>\s]+)\s*>/;
|
||||||
const QWEN_PARAM_RE = /<parameter\s*=\s*([^>\s]+)\s*>([\s\S]*?)<\/parameter>/g;
|
const QWEN_PARAM_RE = /<parameter\s*=\s*([^>\s]+)\s*>([\s\S]*?)<\/parameter>/g;
|
||||||
|
|
||||||
export function parseXmlToolCall(block: string): ParsedCall | null {
|
function parseXmlToolCall(block: string): ParsedCall | null {
|
||||||
const nameMatch = block.match(QWEN_FUNCTION_RE);
|
const nameMatch = block.match(QWEN_FUNCTION_RE);
|
||||||
if (!nameMatch || !nameMatch[1]) return null;
|
if (!nameMatch || !nameMatch[1]) return null;
|
||||||
const name = nameMatch[1].trim();
|
const name = nameMatch[1].trim();
|
||||||
@@ -95,7 +99,7 @@ const INVOKE_NAME_RE =
|
|||||||
const INVOKE_PARAM_RE =
|
const INVOKE_PARAM_RE =
|
||||||
/<parameter\s+name\s*=\s*("([^"]*)"|'([^']*)')\s*>([\s\S]*?)<\/parameter>/g;
|
/<parameter\s+name\s*=\s*("([^"]*)"|'([^']*)')\s*>([\s\S]*?)<\/parameter>/g;
|
||||||
|
|
||||||
export function parseInvokeToolCall(block: string): ParsedCall | null {
|
function parseInvokeToolCall(block: string): ParsedCall | null {
|
||||||
const nameMatch = block.match(INVOKE_NAME_RE);
|
const nameMatch = block.match(INVOKE_NAME_RE);
|
||||||
if (!nameMatch) return null;
|
if (!nameMatch) return null;
|
||||||
const name = (nameMatch[2] ?? nameMatch[3] ?? '').trim();
|
const name = (nameMatch[2] ?? nameMatch[3] ?? '').trim();
|
||||||
@@ -116,7 +120,7 @@ export function parseInvokeToolCall(block: string): ParsedCall | null {
|
|||||||
|
|
||||||
const ALL_OPENERS = [XML_TOOL_OPEN, INVOKE_TOOL_OPEN] as const;
|
const ALL_OPENERS = [XML_TOOL_OPEN, INVOKE_TOOL_OPEN] as const;
|
||||||
|
|
||||||
export function partialXmlOpenerStart(s: string): number {
|
function partialXmlOpenerStart(s: string): number {
|
||||||
let earliest = -1;
|
let earliest = -1;
|
||||||
for (const op of ALL_OPENERS) {
|
for (const op of ALL_OPENERS) {
|
||||||
const idx = s.indexOf(op);
|
const idx = s.indexOf(op);
|
||||||
@@ -150,7 +154,7 @@ const OPENER_SPECS: ReadonlyArray<OpenerSpec> = [
|
|||||||
{ open: INVOKE_TOOL_OPEN, close: INVOKE_TOOL_CLOSE, parse: parseInvokeToolCall },
|
{ open: INVOKE_TOOL_OPEN, close: INVOKE_TOOL_CLOSE, parse: parseInvokeToolCall },
|
||||||
];
|
];
|
||||||
|
|
||||||
export function extractToolCallBlocks(buffer: string): ToolCallExtraction {
|
export function extractToolCallBlocks(buffer: string, log?: MinLogger): ToolCallExtraction {
|
||||||
let flushed = '';
|
let flushed = '';
|
||||||
const calls: ParsedCall[] = [];
|
const calls: ParsedCall[] = [];
|
||||||
let pos = 0;
|
let pos = 0;
|
||||||
@@ -176,7 +180,7 @@ export function extractToolCallBlocks(buffer: string): ToolCallExtraction {
|
|||||||
const parsed = next.spec.parse(block);
|
const parsed = next.spec.parse(block);
|
||||||
if (parsed) {
|
if (parsed) {
|
||||||
if (hasPlaceholderArgs(parsed.args)) {
|
if (hasPlaceholderArgs(parsed.args)) {
|
||||||
logRejectedPlaceholder(parsed);
|
logRejectedPlaceholder(parsed, log);
|
||||||
flushed += block;
|
flushed += block;
|
||||||
} else {
|
} else {
|
||||||
calls.push(parsed);
|
calls.push(parsed);
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ import type {
|
|||||||
ViewFileResult,
|
ViewFileResult,
|
||||||
AgentsResponse,
|
AgentsResponse,
|
||||||
GitMeta,
|
GitMeta,
|
||||||
|
GitDiffMode,
|
||||||
|
GitDiffResult,
|
||||||
|
GitDiscardFileInfo,
|
||||||
Skill,
|
Skill,
|
||||||
ToolCostStat,
|
ToolCostStat,
|
||||||
ProviderSnapshotEntry,
|
ProviderSnapshotEntry,
|
||||||
@@ -151,6 +154,32 @@ export const api = {
|
|||||||
request<{ files: string[] }>(`/api/projects/${id}/files`),
|
request<{ files: string[] }>(`/api/projects/${id}/files`),
|
||||||
git: (id: string) =>
|
git: (id: string) =>
|
||||||
request<GitMeta>(`/api/projects/${id}/git`),
|
request<GitMeta>(`/api/projects/${id}/git`),
|
||||||
|
gitDiff: (id: string, mode: GitDiffMode | null) =>
|
||||||
|
request<GitDiffResult>(
|
||||||
|
mode !== null
|
||||||
|
? `/api/projects/${id}/git/diff?mode=${mode}`
|
||||||
|
: `/api/projects/${id}/git/diff`,
|
||||||
|
),
|
||||||
|
gitStage: (id: string, files: string[]) =>
|
||||||
|
request<{ ok: boolean }>(`/api/projects/${id}/git/stage`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ files }),
|
||||||
|
}),
|
||||||
|
gitUnstage: (id: string, files: string[]) =>
|
||||||
|
request<{ ok: boolean }>(`/api/projects/${id}/git/unstage`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ files }),
|
||||||
|
}),
|
||||||
|
gitCommit: (id: string, body: { message: string; files?: string[] }) =>
|
||||||
|
request<{ ok: boolean }>(`/api/projects/${id}/git/commit`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
}),
|
||||||
|
gitDiscard: (id: string, files: GitDiscardFileInfo[]) =>
|
||||||
|
request<{ ok: boolean }>(`/api/projects/${id}/git/discard`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ files }),
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
|
|
||||||
sessions: {
|
sessions: {
|
||||||
|
|||||||
@@ -312,6 +312,39 @@ export interface GitMeta {
|
|||||||
behind: number;
|
behind: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// git-diff-panel Phase 1: shapes returned by GET /api/projects/:id/git/diff.
|
||||||
|
export type GitDiffMode = 'uncommitted' | 'committed';
|
||||||
|
export type GitDiffChangeType = 'added' | 'modified' | 'deleted' | 'renamed' | 'untracked';
|
||||||
|
|
||||||
|
export interface GitDiffFile {
|
||||||
|
path: string;
|
||||||
|
old_path: string | null;
|
||||||
|
change_type: GitDiffChangeType;
|
||||||
|
added_lines: number;
|
||||||
|
removed_lines: number;
|
||||||
|
staged: boolean;
|
||||||
|
diff_body: string | null;
|
||||||
|
is_binary: boolean;
|
||||||
|
is_too_large: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GitDiffResult {
|
||||||
|
git_repo: boolean;
|
||||||
|
mode: GitDiffMode;
|
||||||
|
/** Server-computed mode based on dirty state — used for auto-select (FIX 1) and mode suggestion (FIX 4). */
|
||||||
|
auto_mode?: GitDiffMode;
|
||||||
|
base_label: string | null;
|
||||||
|
in_progress_op: string | null;
|
||||||
|
files: GitDiffFile[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// git-diff-panel Phase 2: per-file info for the discard endpoint.
|
||||||
|
export interface GitDiscardFileInfo {
|
||||||
|
path: string;
|
||||||
|
change_type: GitDiffChangeType;
|
||||||
|
staged: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
// Batch 9.6: skill catalog row. Returned by GET /api/skills and consumed by
|
// Batch 9.6: skill catalog row. Returned by GET /api/skills and consumed by
|
||||||
// the slash-command dropdown. `path` and `mtime` are exposed for debug surface
|
// the slash-command dropdown. `path` and `mtime` are exposed for debug surface
|
||||||
// (/api/skills) but the dropdown only renders name + description.
|
// (/api/skills) but the dropdown only renders name + description.
|
||||||
@@ -442,6 +475,11 @@ export type WsFrame =
|
|||||||
// cap-hit sentinels (and any future stamped-on-complete metadata) flow
|
// cap-hit sentinels (and any future stamped-on-complete metadata) flow
|
||||||
// to the client without a refetch.
|
// to the client without a refetch.
|
||||||
metadata?: MessageMetadata | null;
|
metadata?: MessageMetadata | null;
|
||||||
|
// F1 (D-8): terminal status of the assistant message. Absent on the normal
|
||||||
|
// path (reducer defaults to 'complete'); the BooCoder dispatcher stamps it
|
||||||
|
// 'cancelled' on a user Stop / stall and 'failed' on a thrown error so the
|
||||||
|
// reducer renders a muted "Stopped" / failed state — no new frame type.
|
||||||
|
status?: 'complete' | 'cancelled' | 'failed';
|
||||||
}
|
}
|
||||||
// v1.12.2: live throughput frame, published mid-stream every ~500ms with
|
// v1.12.2: live throughput frame, published mid-stream every ~500ms with
|
||||||
// the latest token + ctx counts so ChatThroughput can render tok/s and
|
// the latest token + ctx counts so ChatThroughput can render tok/s and
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ interface Props {
|
|||||||
// queues while busy). Omitting onStop falls back to a (disabled) Send button.
|
// queues while busy). Omitting onStop falls back to a (disabled) Send button.
|
||||||
generating?: boolean;
|
generating?: boolean;
|
||||||
onStop?: () => void | Promise<void>;
|
onStop?: () => void | Promise<void>;
|
||||||
|
// F1: disable the Stop button while a cancel request is already in flight, so a
|
||||||
|
// rapid second click can't fire a duplicate Stop. Optional — BooChat omits it.
|
||||||
|
stopDisabled?: boolean;
|
||||||
// Batch 9.6: slash-command dispatch. When the input parses to a known skill,
|
// Batch 9.6: slash-command dispatch. When the input parses to a known skill,
|
||||||
// ChatInput calls this with the skill name + the post-name args (possibly
|
// ChatInput calls this with the skill name + the post-name args (possibly
|
||||||
// empty). Callers wire this to api.chats.skillInvoke. Omitting the prop
|
// empty). Callers wire this to api.chats.skillInvoke. Omitting the prop
|
||||||
@@ -76,7 +79,7 @@ interface Props {
|
|||||||
modelContextLimit?: number | null;
|
modelContextLimit?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ChatInput({ disabled, projectId, agentId, onAgentChange, sessionId, webSearchEnabled, onSend, onForceSend, generating, onStop, onSlashCommand, slashGroups, chatId, chatLabel, messages, modelContextLimit }: Props) {
|
export function ChatInput({ disabled, projectId, agentId, onAgentChange, sessionId, webSearchEnabled, onSend, onForceSend, generating, onStop, stopDisabled, onSlashCommand, slashGroups, chatId, chatLabel, messages, modelContextLimit }: Props) {
|
||||||
const { isMobile } = useViewport();
|
const { isMobile } = useViewport();
|
||||||
const [value, setValue] = useState('');
|
const [value, setValue] = useState('');
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
@@ -701,10 +704,11 @@ export function ChatInput({ disabled, projectId, agentId, onAgentChange, session
|
|||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => void onStop()}
|
onClick={() => void onStop()}
|
||||||
|
disabled={stopDisabled}
|
||||||
size="icon"
|
size="icon"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
aria-label="Stop generating"
|
aria-label="Stop generating"
|
||||||
title="Stop generating"
|
title={stopDisabled ? 'Stopping…' : 'Stop generating'}
|
||||||
>
|
>
|
||||||
<Square className="fill-current size-3.5" />
|
<Square className="fill-current size-3.5" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
493
apps/web/src/components/GitDiffView.tsx
Normal file
493
apps/web/src/components/GitDiffView.tsx
Normal file
@@ -0,0 +1,493 @@
|
|||||||
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { ChevronDown, ChevronRight, GitBranch, RefreshCw, Trash2 } from 'lucide-react';
|
||||||
|
import { codeToHtml } from 'shiki';
|
||||||
|
import type { GitDiffFile, GitDiffMode, GitDiffResult, GitDiscardFileInfo } from '@/api/types';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
interface WriteProps {
|
||||||
|
mutating: boolean;
|
||||||
|
mutateError: string | null;
|
||||||
|
onStage: (files: string[]) => Promise<boolean>;
|
||||||
|
onUnstage: (files: string[]) => Promise<boolean>;
|
||||||
|
onCommit: (message: string, files?: string[]) => Promise<boolean>;
|
||||||
|
onDiscard: (files: GitDiscardFileInfo[]) => Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props extends WriteProps {
|
||||||
|
result: GitDiffResult | null;
|
||||||
|
loading: boolean;
|
||||||
|
error: string | null;
|
||||||
|
mode: GitDiffMode;
|
||||||
|
onSelectMode: (m: GitDiffMode) => void;
|
||||||
|
onRefresh: () => void;
|
||||||
|
/** FIX 4: non-null when the repo's dirty state suggests a different mode than the pinned one. */
|
||||||
|
modeSuggestion?: GitDiffMode | null;
|
||||||
|
/** FIX 5: pending-changes count from the Coder pane — shown in empty state as a hint. */
|
||||||
|
pendingCount?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CHANGE_TYPE_LABELS: Record<string, string> = {
|
||||||
|
added: 'A',
|
||||||
|
modified: 'M',
|
||||||
|
deleted: 'D',
|
||||||
|
renamed: 'R',
|
||||||
|
untracked: '?',
|
||||||
|
};
|
||||||
|
|
||||||
|
const CHANGE_TYPE_COLORS: Record<string, string> = {
|
||||||
|
added: 'text-green-500',
|
||||||
|
modified: 'text-yellow-500',
|
||||||
|
deleted: 'text-red-500',
|
||||||
|
renamed: 'text-blue-500',
|
||||||
|
untracked: 'text-muted-foreground',
|
||||||
|
};
|
||||||
|
|
||||||
|
interface DiscardConfirmState {
|
||||||
|
file: GitDiffFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
function DiscardConfirmDialog({
|
||||||
|
state,
|
||||||
|
onConfirm,
|
||||||
|
onCancel,
|
||||||
|
}: {
|
||||||
|
state: DiscardConfirmState;
|
||||||
|
onConfirm: () => void;
|
||||||
|
onCancel: () => void;
|
||||||
|
}) {
|
||||||
|
const isUntracked = state.file.change_type === 'untracked';
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 px-4"
|
||||||
|
>
|
||||||
|
<div className="bg-popover border rounded-lg shadow-lg max-w-sm w-full p-4 flex flex-col gap-3">
|
||||||
|
<p className="text-sm font-medium">
|
||||||
|
{isUntracked ? 'Permanently delete file?' : 'Discard changes?'}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{isUntracked
|
||||||
|
? `${state.file.path} will be permanently deleted. This cannot be undone.`
|
||||||
|
: `Changes to ${state.file.path} will be reverted to the last commit. This cannot be undone.`}
|
||||||
|
</p>
|
||||||
|
<div className="flex gap-2 justify-end">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onCancel}
|
||||||
|
className="text-xs px-3 py-1.5 rounded border hover:bg-muted max-md:min-h-[44px] max-md:min-w-[44px]"
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onConfirm}
|
||||||
|
className="text-xs px-3 py-1.5 rounded bg-destructive text-destructive-foreground hover:bg-destructive/90 max-md:min-h-[44px] max-md:min-w-[44px]"
|
||||||
|
>
|
||||||
|
{isUntracked ? 'Delete' : 'Discard'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function FileDiffRow({
|
||||||
|
file,
|
||||||
|
uncommitted,
|
||||||
|
disabled,
|
||||||
|
onStage,
|
||||||
|
onUnstage,
|
||||||
|
onDiscardRequest,
|
||||||
|
}: {
|
||||||
|
file: GitDiffFile;
|
||||||
|
uncommitted: boolean;
|
||||||
|
disabled: boolean;
|
||||||
|
onStage: (path: string) => void;
|
||||||
|
onUnstage: (path: string) => void;
|
||||||
|
onDiscardRequest: (file: GitDiffFile) => void;
|
||||||
|
}) {
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
const [html, setHtml] = useState<string | null>(null);
|
||||||
|
const [highlighting, setHighlighting] = useState(false);
|
||||||
|
const highlightRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!expanded || !file.diff_body) return;
|
||||||
|
if (html !== null) return;
|
||||||
|
let cancelled = false;
|
||||||
|
setHighlighting(true);
|
||||||
|
void codeToHtml(file.diff_body, { lang: 'diff', theme: 'github-dark' })
|
||||||
|
.then((result) => { if (!cancelled) setHtml(result); })
|
||||||
|
.catch(() => { if (!cancelled) setHtml(null); })
|
||||||
|
.finally(() => { if (!cancelled) setHighlighting(false); });
|
||||||
|
return () => { cancelled = true; };
|
||||||
|
}, [expanded, file.diff_body, html]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (highlightRef.current && html !== null) {
|
||||||
|
// Shiki generates sanitized HTML — not user-supplied content.
|
||||||
|
// eslint-disable-next-line no-unsanitized/property
|
||||||
|
highlightRef.current.innerHTML = html;
|
||||||
|
}
|
||||||
|
}, [html]);
|
||||||
|
|
||||||
|
const typeLabel = CHANGE_TYPE_LABELS[file.change_type] ?? '?';
|
||||||
|
const typeColor = CHANGE_TYPE_COLORS[file.change_type] ?? 'text-muted-foreground';
|
||||||
|
const displayPath = file.old_path ? `${file.old_path} → ${file.path}` : file.path;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li className="border-b border-border/30 last:border-0">
|
||||||
|
<div className="flex items-center group">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="flex-1 flex items-center gap-1.5 px-2 py-1.5 text-xs hover:bg-muted/40 text-left max-md:min-h-[44px] min-w-0"
|
||||||
|
onClick={() => setExpanded((p) => !p)}
|
||||||
|
aria-expanded={expanded}
|
||||||
|
>
|
||||||
|
{expanded
|
||||||
|
? <ChevronDown size={10} className="shrink-0 text-muted-foreground" />
|
||||||
|
: <ChevronRight size={10} className="shrink-0 text-muted-foreground" />}
|
||||||
|
<span className={cn('font-mono font-bold w-3 shrink-0', typeColor)}>{typeLabel}</span>
|
||||||
|
<span className="truncate flex-1">{displayPath}</span>
|
||||||
|
{(file.added_lines > 0 || file.removed_lines > 0) && (
|
||||||
|
<span className="shrink-0 text-muted-foreground/70 font-mono text-[10px]">
|
||||||
|
{file.added_lines > 0 && <span className="text-green-500">+{file.added_lines}</span>}
|
||||||
|
{file.added_lines > 0 && file.removed_lines > 0 && <span className="mx-0.5">/</span>}
|
||||||
|
{file.removed_lines > 0 && <span className="text-red-500">-{file.removed_lines}</span>}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{file.staged && (
|
||||||
|
<span className="shrink-0 text-[10px] bg-blue-500/15 text-blue-400 px-1 rounded">staged</span>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{/* Write affordances — Uncommitted mode only */}
|
||||||
|
{uncommitted && (
|
||||||
|
<div className="flex items-center gap-0.5 px-1 shrink-0">
|
||||||
|
{/* Stage / Unstage toggle */}
|
||||||
|
{file.change_type !== 'deleted' && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={disabled}
|
||||||
|
onClick={() => file.staged ? onUnstage(file.path) : onStage(file.path)}
|
||||||
|
className="text-[10px] px-1.5 py-0.5 rounded border border-border/50 hover:bg-muted disabled:opacity-40 max-md:min-h-[44px] max-md:min-w-[44px]"
|
||||||
|
title={file.staged ? 'Unstage' : 'Stage'}
|
||||||
|
>
|
||||||
|
{file.staged ? '−' : '+'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{/* Discard — separated secondary affordance */}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={disabled}
|
||||||
|
onClick={() => onDiscardRequest(file)}
|
||||||
|
className="p-1 rounded hover:bg-destructive/15 hover:text-destructive text-muted-foreground/50 disabled:opacity-40 max-md:min-h-[44px] max-md:min-w-[44px]"
|
||||||
|
title={file.change_type === 'untracked' ? 'Delete file' : 'Discard changes'}
|
||||||
|
>
|
||||||
|
<Trash2 size={10} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{expanded && (
|
||||||
|
<div className="px-2 pb-2">
|
||||||
|
{file.is_binary && (
|
||||||
|
<p className="text-xs text-muted-foreground italic px-2 py-1">Binary file</p>
|
||||||
|
)}
|
||||||
|
{file.is_too_large && (
|
||||||
|
<p className="text-xs text-muted-foreground italic px-2 py-1">Diff too large to display</p>
|
||||||
|
)}
|
||||||
|
{file.change_type === 'untracked' && (
|
||||||
|
<p className="text-xs text-muted-foreground italic px-2 py-1">Untracked — not yet staged</p>
|
||||||
|
)}
|
||||||
|
{!file.is_binary && !file.is_too_large && file.diff_body && (
|
||||||
|
<>
|
||||||
|
{highlighting && (
|
||||||
|
<p className="text-xs text-muted-foreground px-2 py-1">Highlighting…</p>
|
||||||
|
)}
|
||||||
|
{!highlighting && html !== null ? (
|
||||||
|
<div
|
||||||
|
ref={highlightRef}
|
||||||
|
className="text-[11px] overflow-x-auto rounded bg-[#0d1117] [&_pre]:!p-2 [&_pre]:!m-0 [&_pre]:overflow-x-auto"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
!highlighting && (
|
||||||
|
<pre className="text-[11px] overflow-x-auto rounded bg-muted/30 p-2 whitespace-pre">
|
||||||
|
{file.diff_body}
|
||||||
|
</pre>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GitDiffView({
|
||||||
|
result,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
mode,
|
||||||
|
onSelectMode,
|
||||||
|
onRefresh,
|
||||||
|
mutating,
|
||||||
|
mutateError,
|
||||||
|
onStage,
|
||||||
|
onUnstage,
|
||||||
|
onCommit,
|
||||||
|
onDiscard,
|
||||||
|
modeSuggestion,
|
||||||
|
pendingCount,
|
||||||
|
}: Props) {
|
||||||
|
const [commitMessage, setCommitMessage] = useState('');
|
||||||
|
const [discardTarget, setDiscardTarget] = useState<DiscardConfirmState | null>(null);
|
||||||
|
const [lastAction, setLastAction] = useState<string | null>(null);
|
||||||
|
const lastActionTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
|
function flashAction(msg: string) {
|
||||||
|
setLastAction(msg);
|
||||||
|
if (lastActionTimer.current) clearTimeout(lastActionTimer.current);
|
||||||
|
lastActionTimer.current = setTimeout(() => setLastAction(null), 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uncommitted = mode === 'uncommitted';
|
||||||
|
const inProgress = result?.in_progress_op ?? null;
|
||||||
|
const writeDisabled = mutating || !!inProgress;
|
||||||
|
const stagedFiles = result?.files.filter((f) => f.staged) ?? [];
|
||||||
|
const canDoCommit = uncommitted && stagedFiles.length > 0 && commitMessage.trim().length > 0 && !writeDisabled;
|
||||||
|
|
||||||
|
async function handleStage(path: string) {
|
||||||
|
const ok = await onStage([path]);
|
||||||
|
if (ok) flashAction('Staged');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleUnstage(path: string) {
|
||||||
|
const ok = await onUnstage([path]);
|
||||||
|
if (ok) flashAction('Unstaged');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDiscardRequest(file: GitDiffFile) {
|
||||||
|
setDiscardTarget({ file });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDiscardConfirm() {
|
||||||
|
if (!discardTarget) return;
|
||||||
|
const { file } = discardTarget;
|
||||||
|
setDiscardTarget(null);
|
||||||
|
const info: GitDiscardFileInfo = {
|
||||||
|
path: file.path,
|
||||||
|
change_type: file.change_type,
|
||||||
|
staged: file.staged,
|
||||||
|
};
|
||||||
|
const ok = await onDiscard([info]);
|
||||||
|
if (ok) flashAction(file.change_type === 'untracked' ? 'Deleted' : 'Discarded');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleCommit() {
|
||||||
|
const msg = commitMessage.trim();
|
||||||
|
if (!msg) return;
|
||||||
|
const ok = await onCommit(msg);
|
||||||
|
if (ok) {
|
||||||
|
setCommitMessage('');
|
||||||
|
flashAction('Committed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading && !result) {
|
||||||
|
return (
|
||||||
|
<div className="flex-1 flex items-center justify-center text-xs text-muted-foreground">
|
||||||
|
Loading diff…
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<div className="flex-1 flex flex-col items-center justify-center gap-2 px-4 text-center">
|
||||||
|
<p className="text-xs text-destructive">{error}</p>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onRefresh}
|
||||||
|
className="text-xs text-muted-foreground hover:text-foreground flex items-center gap-1 max-md:min-h-[44px]"
|
||||||
|
>
|
||||||
|
<RefreshCw size={12} />
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result || !result.git_repo) {
|
||||||
|
return (
|
||||||
|
<div className="flex-1 flex items-center justify-center text-xs text-muted-foreground px-4 text-center">
|
||||||
|
Not a git repository
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { files, base_label } = result;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col flex-1 overflow-hidden">
|
||||||
|
{/* Mode selector */}
|
||||||
|
<div className="flex items-center gap-1 px-2 py-1.5 border-b shrink-0">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onSelectMode('uncommitted')}
|
||||||
|
className={cn(
|
||||||
|
'text-xs px-2 py-0.5 rounded max-md:min-h-[44px]',
|
||||||
|
mode === 'uncommitted'
|
||||||
|
? 'bg-muted text-foreground font-medium'
|
||||||
|
: 'text-muted-foreground hover:text-foreground',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Uncommitted
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onSelectMode('committed')}
|
||||||
|
className={cn(
|
||||||
|
'text-xs px-2 py-0.5 rounded max-md:min-h-[44px]',
|
||||||
|
mode === 'committed'
|
||||||
|
? 'bg-muted text-foreground font-medium'
|
||||||
|
: 'text-muted-foreground hover:text-foreground',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Committed
|
||||||
|
</button>
|
||||||
|
<div className="flex-1" />
|
||||||
|
{(loading || mutating) && (
|
||||||
|
<span className="text-[10px] text-muted-foreground">{mutating ? 'Working…' : 'Refreshing…'}</span>
|
||||||
|
)}
|
||||||
|
{lastAction && !mutating && (
|
||||||
|
<span className="text-[10px] text-green-500">{lastAction}</span>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onRefresh}
|
||||||
|
disabled={loading || mutating}
|
||||||
|
className="p-1 rounded hover:bg-muted text-muted-foreground disabled:opacity-40 max-md:min-h-[44px] max-md:min-w-[44px]"
|
||||||
|
aria-label="Refresh diff"
|
||||||
|
title="Refresh"
|
||||||
|
>
|
||||||
|
<RefreshCw size={12} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Committed-mode base label */}
|
||||||
|
{result.mode === 'committed' && base_label && (
|
||||||
|
<div className="px-2 py-1 text-[10px] text-muted-foreground border-b flex items-center gap-1 shrink-0">
|
||||||
|
<GitBranch size={10} />
|
||||||
|
<span className="truncate">vs {base_label}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* FIX 2: Fallback label — committed was requested but no base branch found */}
|
||||||
|
{result.mode === 'uncommitted' && result.base_label && (
|
||||||
|
<div className="px-2 py-1 text-[10px] text-amber-600 dark:text-amber-400 border-b flex items-center gap-1 shrink-0">
|
||||||
|
<GitBranch size={10} />
|
||||||
|
<span className="truncate">{result.base_label}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* FIX 4: Mode suggestion — shown when pinned mode diverges from auto-selected mode */}
|
||||||
|
{modeSuggestion && (
|
||||||
|
<div className="px-2 py-1 text-[10px] text-muted-foreground border-b shrink-0 flex items-center gap-1">
|
||||||
|
<span>Repo is now {modeSuggestion === 'uncommitted' ? 'dirty' : 'clean'} —</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onSelectMode(modeSuggestion)}
|
||||||
|
className="underline hover:text-foreground"
|
||||||
|
>
|
||||||
|
switch to {modeSuggestion === 'uncommitted' ? 'Uncommitted' : 'Committed'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* In-progress op banner */}
|
||||||
|
{inProgress && (
|
||||||
|
<div className="px-2 py-1 text-[10px] text-yellow-500 bg-yellow-500/10 border-b shrink-0">
|
||||||
|
{inProgress} in progress — write actions disabled
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Mutation error */}
|
||||||
|
{mutateError && (
|
||||||
|
<div className="px-2 py-1 text-[10px] text-destructive bg-destructive/10 border-b shrink-0 truncate">
|
||||||
|
{mutateError}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* File list */}
|
||||||
|
<div className="flex-1 overflow-y-auto">
|
||||||
|
{files.length === 0 ? (
|
||||||
|
<div className="flex flex-col items-center justify-center px-4 py-8 text-xs text-muted-foreground text-center gap-1.5">
|
||||||
|
<span>{mode === 'uncommitted' ? 'No uncommitted changes' : 'No changes vs. the base branch'}</span>
|
||||||
|
{/* FIX 5: hint when pending changes exist in the Coder pane */}
|
||||||
|
{!!pendingCount && (
|
||||||
|
<span className="text-[10px]">
|
||||||
|
{pendingCount} pending {pendingCount === 1 ? 'change' : 'changes'} visible in the Coder pane
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<ul className="list-none">
|
||||||
|
{files.map((file) => (
|
||||||
|
<FileDiffRow
|
||||||
|
key={file.path}
|
||||||
|
file={file}
|
||||||
|
uncommitted={uncommitted}
|
||||||
|
disabled={writeDisabled}
|
||||||
|
onStage={handleStage}
|
||||||
|
onUnstage={handleUnstage}
|
||||||
|
onDiscardRequest={handleDiscardRequest}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Commit panel — Uncommitted mode only */}
|
||||||
|
{uncommitted && (
|
||||||
|
<div className="shrink-0 border-t px-2 py-2 flex flex-col gap-1.5">
|
||||||
|
<textarea
|
||||||
|
value={commitMessage}
|
||||||
|
onChange={(e) => setCommitMessage(e.target.value)}
|
||||||
|
disabled={writeDisabled}
|
||||||
|
placeholder="Commit message…"
|
||||||
|
rows={2}
|
||||||
|
className="w-full text-xs rounded border bg-background px-2 py-1 resize-none focus:outline-none focus:ring-1 focus:ring-ring disabled:opacity-40 placeholder:text-muted-foreground"
|
||||||
|
/>
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
<span className="text-[10px] text-muted-foreground flex-1">
|
||||||
|
{stagedFiles.length > 0
|
||||||
|
? `${stagedFiles.length} file${stagedFiles.length > 1 ? 's' : ''} staged`
|
||||||
|
: 'No files staged'}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={!canDoCommit}
|
||||||
|
onClick={handleCommit}
|
||||||
|
className="text-xs px-3 py-1 rounded bg-primary text-primary-foreground hover:bg-primary/90 disabled:opacity-40 max-md:min-h-[44px]"
|
||||||
|
>
|
||||||
|
Commit
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Discard confirmation dialog */}
|
||||||
|
{discardTarget && (
|
||||||
|
<DiscardConfirmDialog
|
||||||
|
state={discardTarget}
|
||||||
|
onConfirm={handleDiscardConfirm}
|
||||||
|
onCancel={() => setDiscardTarget(null)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -780,6 +780,10 @@ export const MessageBubble = memo(function MessageBubble({
|
|||||||
|
|
||||||
const isStreaming = message.status === 'streaming';
|
const isStreaming = message.status === 'streaming';
|
||||||
const failed = message.status === 'failed';
|
const failed = message.status === 'failed';
|
||||||
|
// F1 (D-10): a user Stop finalizes the turn as 'cancelled' — surface a muted
|
||||||
|
// "Stopped" label (not the red "message failed" — a deliberate Stop is not a
|
||||||
|
// failure), keeping whatever streamed before the abort.
|
||||||
|
const cancelled = message.status === 'cancelled';
|
||||||
// v1.13.7: match the MessageList.flatten trim guard so a whitespace-only
|
// v1.13.7: match the MessageList.flatten trim guard so a whitespace-only
|
||||||
// assistant turn doesn't render an empty bubble + dangling ActionRow.
|
// assistant turn doesn't render an empty bubble + dangling ActionRow.
|
||||||
const hasContent = message.content.trim().length > 0;
|
const hasContent = message.content.trim().length > 0;
|
||||||
@@ -826,6 +830,7 @@ export const MessageBubble = memo(function MessageBubble({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{cancelled && <div className="text-xs text-muted-foreground">Stopped</div>}
|
||||||
{!isStreaming && (modelLabel || null) && (
|
{!isStreaming && (modelLabel || null) && (
|
||||||
<span
|
<span
|
||||||
className="inline-flex w-fit items-center rounded-full border border-primary/25 bg-primary/10 px-2 py-0.5 text-[10px] font-mono text-primary/90"
|
className="inline-flex w-fit items-center rounded-full border border-primary/25 bg-primary/10 px-2 py-0.5 text-[10px] font-mono text-primary/90"
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import { inferLanguage } from '@/lib/attachments';
|
|||||||
import { sessionEvents } from '@/hooks/sessionEvents';
|
import { sessionEvents } from '@/hooks/sessionEvents';
|
||||||
import { useRightRailDrawer } from '@/hooks/useRightRailDrawer';
|
import { useRightRailDrawer } from '@/hooks/useRightRailDrawer';
|
||||||
import { useViewport } from '@/hooks/useViewport';
|
import { useViewport } from '@/hooks/useViewport';
|
||||||
|
import { useProjectGit } from '@/hooks/useProjectGit';
|
||||||
|
import { useGitDiff } from '@/hooks/useGitDiff';
|
||||||
import { FileViewerOverlay } from '@/components/FileViewerOverlay';
|
import { FileViewerOverlay } from '@/components/FileViewerOverlay';
|
||||||
|
import { GitDiffView } from '@/components/GitDiffView';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Textarea } from '@/components/ui/textarea';
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -21,6 +24,8 @@ import {
|
|||||||
} from '@/components/ui/dialog';
|
} from '@/components/ui/dialog';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
type RailTab = 'files' | 'git';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
projectId: string;
|
projectId: string;
|
||||||
sessionId: string;
|
sessionId: string;
|
||||||
@@ -45,12 +50,38 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
const [open, setOpen] = useState(() => {
|
const [open, setOpen] = useState(() => {
|
||||||
try { return localStorage.getItem(`${STORAGE_KEY}.open`) !== 'false'; } catch { return true; }
|
try { return localStorage.getItem(`${STORAGE_KEY}.open`) !== 'false'; } catch { return true; }
|
||||||
});
|
});
|
||||||
|
const [tab, setTab] = useState<RailTab>('files');
|
||||||
const [filter, setFilter] = useState('');
|
const [filter, setFilter] = useState('');
|
||||||
const [expandedDirs, setExpandedDirs] = useState<Set<string>>(new Set());
|
const [expandedDirs, setExpandedDirs] = useState<Set<string>>(new Set());
|
||||||
const [cache, setCache] = useState<Map<string, FileEntry[]>>(new Map());
|
const [cache, setCache] = useState<Map<string, FileEntry[]>>(new Map());
|
||||||
const [fullFileList, setFullFileList] = useState<string[] | null>(null);
|
const [fullFileList, setFullFileList] = useState<string[] | null>(null);
|
||||||
const [viewerFile, setViewerFile] = useState<{ path: string; content: string } | null>(null);
|
const [viewerFile, setViewerFile] = useState<{ path: string; content: string } | null>(null);
|
||||||
|
|
||||||
|
// Git metadata: dirty dot on the Git tab (no new fetch — reuses the 30s poll).
|
||||||
|
const git = useProjectGit(projectId);
|
||||||
|
const isDirty = git?.is_dirty ?? false;
|
||||||
|
|
||||||
|
// Git diff view state (Phase 2: includes write callbacks).
|
||||||
|
const { result: gitDiff, loading: gitLoading, error: gitError, mode: gitMode, selectMode, refresh: refreshDiff, mutating: gitMutating, mutateError: gitMutateError, stage: gitStage, unstage: gitUnstage, commit: gitCommit, discard: gitDiscard, modeSuggestion: gitModeSuggestion } = useGitDiff(projectId);
|
||||||
|
const showGitTab = gitDiff === null || gitDiff.git_repo;
|
||||||
|
|
||||||
|
// FIX 5: pending-changes count — fetched when git tab is active so the empty state
|
||||||
|
// can hint that unapplied pending changes exist in the Coder pane.
|
||||||
|
const [pendingCount, setPendingCount] = useState(0);
|
||||||
|
useEffect(() => {
|
||||||
|
if (tab !== 'git') return;
|
||||||
|
const check = () => {
|
||||||
|
fetch(`/api/coder/sessions/${sessionId}/pending`)
|
||||||
|
.then((r) => r.ok ? r.json() as Promise<Array<{ status: string }>> : [])
|
||||||
|
.then((data) => setPendingCount(data.filter((c) => c.status === 'pending').length))
|
||||||
|
.catch(() => {});
|
||||||
|
};
|
||||||
|
check();
|
||||||
|
return sessionEvents.subscribe((e) => {
|
||||||
|
if (e.type === 'git_diff_refresh') check();
|
||||||
|
});
|
||||||
|
}, [tab, sessionId]);
|
||||||
|
|
||||||
// New-file-from-pasted-text modal. Queues a pending_changes create via
|
// New-file-from-pasted-text modal. Queues a pending_changes create via
|
||||||
// BooCoder; it then shows in the CoderPane DiffPanel for explicit apply.
|
// BooCoder; it then shows in the CoderPane DiffPanel for explicit apply.
|
||||||
const [newFileOpen, setNewFileOpen] = useState(false);
|
const [newFileOpen, setNewFileOpen] = useState(false);
|
||||||
@@ -167,6 +198,11 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
return [];
|
return [];
|
||||||
}, [filterActive, trimmed, fullFileList]);
|
}, [filterActive, trimmed, fullFileList]);
|
||||||
|
|
||||||
|
// Trigger a git diff refresh whenever the Git tab becomes active.
|
||||||
|
useEffect(() => {
|
||||||
|
if (tab === 'git') sessionEvents.emit({ type: 'git_diff_refresh' });
|
||||||
|
}, [tab]);
|
||||||
|
|
||||||
// Listen for open_file_in_browser events
|
// Listen for open_file_in_browser events
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return sessionEvents.subscribe((event) => {
|
return sessionEvents.subscribe((event) => {
|
||||||
@@ -206,8 +242,35 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<aside className={asideCls}>
|
<aside className={asideCls}>
|
||||||
<div className="flex items-center gap-2 px-3 py-2 border-b shrink-0">
|
{/* Header: Files / Git tab strip, FilePlus (Files only), close */}
|
||||||
<span className="text-xs font-medium flex-1">Files</span>
|
<div className="flex items-center gap-1 px-2 py-1.5 border-b shrink-0">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setTab('files')}
|
||||||
|
className={cn(
|
||||||
|
'text-xs px-2 py-0.5 rounded max-md:min-h-[44px]',
|
||||||
|
tab === 'files' ? 'bg-muted text-foreground font-medium' : 'text-muted-foreground hover:text-foreground',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Files
|
||||||
|
</button>
|
||||||
|
{showGitTab && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setTab('git')}
|
||||||
|
className={cn(
|
||||||
|
'relative text-xs px-2 py-0.5 rounded max-md:min-h-[44px] flex items-center gap-1',
|
||||||
|
tab === 'git' ? 'bg-muted text-foreground font-medium' : 'text-muted-foreground hover:text-foreground',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Git
|
||||||
|
{isDirty && (
|
||||||
|
<span className="w-1.5 h-1.5 rounded-full bg-yellow-400 shrink-0" aria-label="dirty" />
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<div className="flex-1" />
|
||||||
|
{tab === 'files' && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={openNewFile}
|
onClick={openNewFile}
|
||||||
@@ -217,6 +280,7 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
>
|
>
|
||||||
<FilePlus size={14} />
|
<FilePlus size={14} />
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={closeRail}
|
onClick={closeRail}
|
||||||
@@ -226,6 +290,10 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
<PanelRightClose size={14} />
|
<PanelRightClose size={14} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Files tab content */}
|
||||||
|
{tab === 'files' && (
|
||||||
|
<>
|
||||||
<div className="px-2 py-1.5 shrink-0">
|
<div className="px-2 py-1.5 shrink-0">
|
||||||
<Input
|
<Input
|
||||||
value={filter}
|
value={filter}
|
||||||
@@ -267,6 +335,28 @@ export function RightRail({ projectId, sessionId }: Props) {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Git tab content */}
|
||||||
|
{tab === 'git' && (
|
||||||
|
<GitDiffView
|
||||||
|
result={gitDiff}
|
||||||
|
loading={gitLoading}
|
||||||
|
error={gitError}
|
||||||
|
mode={gitMode}
|
||||||
|
onSelectMode={selectMode}
|
||||||
|
onRefresh={refreshDiff}
|
||||||
|
mutating={gitMutating}
|
||||||
|
mutateError={gitMutateError}
|
||||||
|
onStage={gitStage}
|
||||||
|
onUnstage={gitUnstage}
|
||||||
|
onCommit={gitCommit}
|
||||||
|
onDiscard={gitDiscard}
|
||||||
|
modeSuggestion={gitModeSuggestion}
|
||||||
|
pendingCount={pendingCount}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
{viewerFile && (
|
{viewerFile && (
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ export interface CoderMessageWire {
|
|||||||
id: string;
|
id: string;
|
||||||
role: 'user' | 'assistant' | 'system';
|
role: 'user' | 'assistant' | 'system';
|
||||||
content: string;
|
content: string;
|
||||||
status?: 'streaming' | 'complete' | 'failed';
|
// F1: 'cancelled' — a user Stop / stall finalized the turn (renders "Stopped").
|
||||||
|
status?: 'streaming' | 'complete' | 'failed' | 'cancelled';
|
||||||
model?: string | null;
|
model?: string | null;
|
||||||
reasoning_text?: string;
|
reasoning_text?: string;
|
||||||
tool_calls?: CoderToolCallWire[];
|
tool_calls?: CoderToolCallWire[];
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import { providerIcon, providerLabel } from '@/components/coder/providerIcons';
|
|||||||
import { refreshAgentSessions } from '@/hooks/useAgentSessions';
|
import { refreshAgentSessions } from '@/hooks/useAgentSessions';
|
||||||
import { useAgentStatus, type AgentStatus, type AgentStatusEntry } from '@/hooks/useAgentStatus';
|
import { useAgentStatus, type AgentStatus, type AgentStatusEntry } from '@/hooks/useAgentStatus';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
import { sessionEvents } from '@/hooks/sessionEvents';
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Types
|
// Types
|
||||||
@@ -29,7 +30,8 @@ interface CoderMessage {
|
|||||||
id: string;
|
id: string;
|
||||||
role: 'user' | 'assistant' | 'system';
|
role: 'user' | 'assistant' | 'system';
|
||||||
content: string;
|
content: string;
|
||||||
status?: 'streaming' | 'complete' | 'failed';
|
// F1: 'cancelled' — a user Stop / stall finalized the turn (renders "Stopped").
|
||||||
|
status?: 'streaming' | 'complete' | 'failed' | 'cancelled';
|
||||||
// model-attribution: which model produced this assistant message (chip).
|
// model-attribution: which model produced this assistant message (chip).
|
||||||
model?: string | null;
|
model?: string | null;
|
||||||
reasoning_text?: string;
|
reasoning_text?: string;
|
||||||
@@ -296,7 +298,10 @@ function useCoderMessages(sessionId: string, chatId: string | undefined, handler
|
|||||||
m.id === frame.message_id && m.role !== 'tool'
|
m.id === frame.message_id && m.role !== 'tool'
|
||||||
? {
|
? {
|
||||||
...m,
|
...m,
|
||||||
status: 'complete' as const,
|
// F1 (D-8): the terminal frame carries an optional status —
|
||||||
|
// 'cancelled' on a Stop/stall, 'failed' on error. Absent on the
|
||||||
|
// normal path → defaults to 'complete'.
|
||||||
|
status: ((frame as any).status ?? 'complete') as CoderMessage['status'],
|
||||||
model: (frame as any).model ?? (m as any).model ?? null,
|
model: (frame as any).model ?? (m as any).model ?? null,
|
||||||
ctx_used: (frame as any).ctx_used ?? (m as any).ctx_used ?? null,
|
ctx_used: (frame as any).ctx_used ?? (m as any).ctx_used ?? null,
|
||||||
ctx_max: (frame as any).ctx_max ?? (m as any).ctx_max ?? null,
|
ctx_max: (frame as any).ctx_max ?? (m as any).ctx_max ?? null,
|
||||||
@@ -433,6 +438,7 @@ function usePendingChanges(sessionId: string) {
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
setChanges((prev) => prev.map((c) => c.id === changeId ? { ...c, status: 'approved' } : c));
|
setChanges((prev) => prev.map((c) => c.id === changeId ? { ...c, status: 'approved' } : c));
|
||||||
|
sessionEvents.emit({ type: 'git_diff_refresh' });
|
||||||
}
|
}
|
||||||
}, [sessionId]);
|
}, [sessionId]);
|
||||||
|
|
||||||
@@ -442,6 +448,7 @@ function usePendingChanges(sessionId: string) {
|
|||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
setChanges((prev) => prev.map((c) => c.id === changeId ? { ...c, status: 'rejected' } : c));
|
setChanges((prev) => prev.map((c) => c.id === changeId ? { ...c, status: 'rejected' } : c));
|
||||||
|
sessionEvents.emit({ type: 'git_diff_refresh' });
|
||||||
}
|
}
|
||||||
}, [sessionId]);
|
}, [sessionId]);
|
||||||
|
|
||||||
@@ -669,6 +676,9 @@ export function CoderPane({
|
|||||||
onAgentLabelChange?.(parts.join(' · '));
|
onAgentLabelChange?.(parts.join(' · '));
|
||||||
}, [agentConfig.provider, agentConfig.model, onAgentLabelChange]);
|
}, [agentConfig.provider, agentConfig.model, onAgentLabelChange]);
|
||||||
const [activeTaskId, setActiveTaskId] = useState<string | null>(null);
|
const [activeTaskId, setActiveTaskId] = useState<string | null>(null);
|
||||||
|
// F1: true while the Stop POST is in flight — disables the Stop button and makes
|
||||||
|
// a rapid double-click a no-op (the abort is idempotent server-side regardless).
|
||||||
|
const [stopping, setStopping] = useState(false);
|
||||||
const [permissionPrompt, setPermissionPrompt] = useState<PermissionPrompt | null>(null);
|
const [permissionPrompt, setPermissionPrompt] = useState<PermissionPrompt | null>(null);
|
||||||
const [permissionBusy, setPermissionBusy] = useState(false);
|
const [permissionBusy, setPermissionBusy] = useState(false);
|
||||||
const [providerCommands, setProviderCommands] = useState<AgentCommand[]>([]);
|
const [providerCommands, setProviderCommands] = useState<AgentCommand[]>([]);
|
||||||
@@ -986,14 +996,17 @@ export function CoderPane({
|
|||||||
|
|
||||||
const handleStop = useCallback(async () => {
|
const handleStop = useCallback(async () => {
|
||||||
const taskId = activeTaskId;
|
const taskId = activeTaskId;
|
||||||
if (!taskId) return;
|
if (!taskId || stopping) return; // ignore a second Stop while the POST is in flight
|
||||||
|
setStopping(true);
|
||||||
try {
|
try {
|
||||||
await api.coder.cancelTask(taskId);
|
await api.coder.cancelTask(taskId);
|
||||||
setActiveTaskId(null); // optimistic; WS/poll terminal-state also clears it
|
setActiveTaskId(null); // optimistic; WS/poll terminal-state also clears it
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error(err instanceof Error ? err.message : 'stop failed');
|
toast.error(err instanceof Error ? err.message : 'stop failed');
|
||||||
|
} finally {
|
||||||
|
setStopping(false);
|
||||||
}
|
}
|
||||||
}, [activeTaskId]);
|
}, [activeTaskId, stopping]);
|
||||||
|
|
||||||
// write-edit-robustness #4: reset the worktree to a message's checkpoint and
|
// write-edit-robustness #4: reset the worktree to a message's checkpoint and
|
||||||
// trim the transcript past it. The confirm lives in MessageBubble's ActionRow
|
// trim the transcript past it. The confirm lives in MessageBubble's ActionRow
|
||||||
@@ -1125,6 +1138,7 @@ export function CoderPane({
|
|||||||
onSend={handleChatInputSend}
|
onSend={handleChatInputSend}
|
||||||
generating={generating}
|
generating={generating}
|
||||||
onStop={handleStop}
|
onStop={handleStop}
|
||||||
|
stopDisabled={stopping}
|
||||||
onSlashCommand={handleChatInputSlash}
|
onSlashCommand={handleChatInputSlash}
|
||||||
slashGroups={slashGroups}
|
slashGroups={slashGroups}
|
||||||
chatId={chatId ?? undefined}
|
chatId={chatId ?? undefined}
|
||||||
|
|||||||
@@ -178,6 +178,12 @@ export interface RefetchMessagesEvent {
|
|||||||
type: 'refetch_messages';
|
type: 'refetch_messages';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// git-diff-panel Phase 1: emitted client-side to trigger a panel refresh.
|
||||||
|
// Not a WS frame — no @boocode/contracts change required.
|
||||||
|
export interface GitDiffRefreshEvent {
|
||||||
|
type: 'git_diff_refresh';
|
||||||
|
}
|
||||||
|
|
||||||
export type SessionEvent =
|
export type SessionEvent =
|
||||||
| SessionRenamedEvent
|
| SessionRenamedEvent
|
||||||
| ProjectCreatedEvent
|
| ProjectCreatedEvent
|
||||||
@@ -204,7 +210,8 @@ export type SessionEvent =
|
|||||||
| ProjectUnarchivedEvent
|
| ProjectUnarchivedEvent
|
||||||
| ProjectUpdatedEvent
|
| ProjectUpdatedEvent
|
||||||
| ChatStatusEvent
|
| ChatStatusEvent
|
||||||
| RefetchMessagesEvent;
|
| RefetchMessagesEvent
|
||||||
|
| GitDiffRefreshEvent;
|
||||||
type Listener = (event: SessionEvent) => void;
|
type Listener = (event: SessionEvent) => void;
|
||||||
|
|
||||||
const listeners = new Set<Listener>();
|
const listeners = new Set<Listener>();
|
||||||
|
|||||||
114
apps/web/src/hooks/useGitDiff.ts
Normal file
114
apps/web/src/hooks/useGitDiff.ts
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { api } from '@/api/client';
|
||||||
|
import type { GitDiffMode, GitDiffResult, GitDiscardFileInfo } from '@/api/types';
|
||||||
|
import { sessionEvents } from './sessionEvents';
|
||||||
|
|
||||||
|
export function useGitDiff(projectId: string | null | undefined) {
|
||||||
|
const [mode, setMode] = useState<GitDiffMode>('uncommitted');
|
||||||
|
const [pinned, setPinned] = useState(false);
|
||||||
|
const [result, setResult] = useState<GitDiffResult | null>(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
// FIX 4: non-null when user has pinned a mode that differs from the server's auto-selected mode.
|
||||||
|
const [modeSuggestion, setModeSuggestion] = useState<GitDiffMode | null>(null);
|
||||||
|
|
||||||
|
// Coalescence guard: absorb concurrent refresh triggers into the running request.
|
||||||
|
const inFlightRef = useRef(false);
|
||||||
|
|
||||||
|
const refresh = useCallback(() => {
|
||||||
|
if (!projectId || inFlightRef.current) return;
|
||||||
|
inFlightRef.current = true;
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
// FIX 1: when not pinned, omit mode param so the server auto-selects based on
|
||||||
|
// dirty state (dirty → uncommitted, clean → committed).
|
||||||
|
api.projects
|
||||||
|
.gitDiff(projectId, pinned ? mode : null)
|
||||||
|
.then((r) => {
|
||||||
|
if (!pinned) {
|
||||||
|
setMode(r.mode);
|
||||||
|
}
|
||||||
|
// FIX 4: if pinned and the server's auto-selected mode differs, surface a suggestion.
|
||||||
|
if (pinned && r.auto_mode && r.auto_mode !== mode) {
|
||||||
|
setModeSuggestion(r.auto_mode);
|
||||||
|
} else {
|
||||||
|
setModeSuggestion(null);
|
||||||
|
}
|
||||||
|
setResult(r);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to load diff');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
inFlightRef.current = false;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
}, [projectId, mode, pinned]);
|
||||||
|
|
||||||
|
// Re-run refresh when mode changes (user pinned a new mode).
|
||||||
|
useEffect(() => {
|
||||||
|
if (!projectId) {
|
||||||
|
setResult(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
refresh();
|
||||||
|
}, [projectId, mode]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
// Subscribe to git_diff_refresh events (tab open, message_complete, manual).
|
||||||
|
useEffect(() => {
|
||||||
|
return sessionEvents.subscribe((event) => {
|
||||||
|
if (event.type === 'git_diff_refresh') refresh();
|
||||||
|
});
|
||||||
|
}, [refresh]);
|
||||||
|
|
||||||
|
const selectMode = useCallback((m: GitDiffMode) => {
|
||||||
|
setPinned(true);
|
||||||
|
setMode(m);
|
||||||
|
setModeSuggestion(null); // FIX 4: clear suggestion on explicit mode pick
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [mutating, setMutating] = useState(false);
|
||||||
|
const [mutateError, setMutateError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const runMutation = useCallback(
|
||||||
|
async (fn: () => Promise<unknown>): Promise<boolean> => {
|
||||||
|
if (!projectId) return false;
|
||||||
|
setMutating(true);
|
||||||
|
setMutateError(null);
|
||||||
|
try {
|
||||||
|
await fn();
|
||||||
|
sessionEvents.emit({ type: 'git_diff_refresh' });
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
setMutateError(err instanceof Error ? err.message : 'Operation failed');
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
setMutating(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[projectId],
|
||||||
|
);
|
||||||
|
|
||||||
|
const stage = useCallback(
|
||||||
|
(files: string[]) => runMutation(() => api.projects.gitStage(projectId!, files)),
|
||||||
|
[projectId, runMutation],
|
||||||
|
);
|
||||||
|
|
||||||
|
const unstage = useCallback(
|
||||||
|
(files: string[]) => runMutation(() => api.projects.gitUnstage(projectId!, files)),
|
||||||
|
[projectId, runMutation],
|
||||||
|
);
|
||||||
|
|
||||||
|
const commit = useCallback(
|
||||||
|
(message: string, files?: string[]) =>
|
||||||
|
runMutation(() => api.projects.gitCommit(projectId!, { message, files })),
|
||||||
|
[projectId, runMutation],
|
||||||
|
);
|
||||||
|
|
||||||
|
const discard = useCallback(
|
||||||
|
(files: GitDiscardFileInfo[]) => runMutation(() => api.projects.gitDiscard(projectId!, files)),
|
||||||
|
[projectId, runMutation],
|
||||||
|
);
|
||||||
|
|
||||||
|
return { result, loading, error, mode, selectMode, refresh, mutating, mutateError, stage, unstage, commit, discard, modeSuggestion };
|
||||||
|
}
|
||||||
@@ -273,6 +273,10 @@ export function useSessionStream(sessionId: string | undefined) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setState((s) => applyFrame(s, frame));
|
setState((s) => applyFrame(s, frame));
|
||||||
|
// Trigger git diff refresh after each completed assistant turn.
|
||||||
|
if (frame.type === 'message_complete') {
|
||||||
|
sessionEvents.emit({ type: 'git_diff_refresh' });
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('bad ws frame', err);
|
console.warn('bad ws frame', err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -186,6 +186,8 @@ function applyEvent(prev: SidebarResponse, event: import('./sessionEvents').Sess
|
|||||||
case 'chat_deleted':
|
case 'chat_deleted':
|
||||||
case 'chat_status':
|
case 'chat_status':
|
||||||
case 'refetch_messages':
|
case 'refetch_messages':
|
||||||
|
case 'git_diff_refresh':
|
||||||
|
// Consumed by useGitDiff; no sidebar state change needed.
|
||||||
return prev;
|
return prev;
|
||||||
case 'project_archived': {
|
case 'project_archived': {
|
||||||
const next = prev.projects.filter((p) => p.id !== event.project_id);
|
const next = prev.projects.filter((p) => p.id !== event.project_id);
|
||||||
|
|||||||
@@ -396,10 +396,13 @@ function SessionInner({ sessionId }: { sessionId: string }) {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={toggleRightRail}
|
onClick={toggleRightRail}
|
||||||
className="inline-flex items-center justify-center -mr-1 min-w-[44px] min-h-[44px] rounded text-muted-foreground hover:bg-muted hover:text-foreground shrink-0"
|
className="relative inline-flex items-center justify-center -mr-1 min-w-[44px] min-h-[44px] rounded text-muted-foreground hover:bg-muted hover:text-foreground shrink-0"
|
||||||
aria-label="Toggle file browser"
|
aria-label="Toggle file browser"
|
||||||
>
|
>
|
||||||
<FolderTree className="size-5" />
|
<FolderTree className="size-5" />
|
||||||
|
{git?.is_dirty && (
|
||||||
|
<span className="absolute top-2 right-2 w-1.5 h-1.5 rounded-full bg-yellow-400" aria-hidden="true" />
|
||||||
|
)}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -124,6 +124,12 @@ export const MessageCompleteFrame = z.object({
|
|||||||
// status:'complete' transition) is dropped.
|
// status:'complete' transition) is dropped.
|
||||||
model: z.string().nullable().optional(),
|
model: z.string().nullable().optional(),
|
||||||
metadata: OpaqueObject.nullable().optional(),
|
metadata: OpaqueObject.nullable().optional(),
|
||||||
|
// F1 (D-8): the terminal status of the assistant message. Absent on the native
|
||||||
|
// BooChat path (reducer defaults to 'complete'); the BooCoder dispatcher stamps
|
||||||
|
// it 'cancelled' on a user Stop / stall and 'failed' on a thrown error so the
|
||||||
|
// web reducer can render a muted "Stopped" / failed state without a new frame
|
||||||
|
// type. Optional → fail-closed publishFrame must keep, not strip, it.
|
||||||
|
status: z.enum(['complete', 'cancelled', 'failed']).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const UsageFrame = z.object({
|
export const UsageFrame = z.object({
|
||||||
|
|||||||
49
pnpm-lock.yaml
generated
49
pnpm-lock.yaml
generated
@@ -57,9 +57,6 @@ importers:
|
|||||||
'@boocode/server':
|
'@boocode/server':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../server
|
version: link:../server
|
||||||
'@fastify/static':
|
|
||||||
specifier: ^7.0.4
|
|
||||||
version: 7.0.4
|
|
||||||
'@fastify/websocket':
|
'@fastify/websocket':
|
||||||
specifier: ^10.0.1
|
specifier: ^10.0.1
|
||||||
version: 10.0.1
|
version: 10.0.1
|
||||||
@@ -98,52 +95,6 @@ importers:
|
|||||||
specifier: ^3.0.0
|
specifier: ^3.0.0
|
||||||
version: 3.2.4(@types/debug@4.1.13)(@types/node@20.19.41)(lightningcss@1.32.0)(msw@2.14.6(@types/node@20.19.41)(typescript@5.9.3))
|
version: 3.2.4(@types/debug@4.1.13)(@types/node@20.19.41)(lightningcss@1.32.0)(msw@2.14.6(@types/node@20.19.41)(typescript@5.9.3))
|
||||||
|
|
||||||
apps/coder/web:
|
|
||||||
dependencies:
|
|
||||||
'@boocode/contracts':
|
|
||||||
specifier: workspace:*
|
|
||||||
version: link:../../../packages/contracts
|
|
||||||
lucide-react:
|
|
||||||
specifier: ^1.16.0
|
|
||||||
version: 1.16.0(react@18.3.1)
|
|
||||||
react:
|
|
||||||
specifier: ^18.3.1
|
|
||||||
version: 18.3.1
|
|
||||||
react-dom:
|
|
||||||
specifier: ^18.3.1
|
|
||||||
version: 18.3.1(react@18.3.1)
|
|
||||||
react-markdown:
|
|
||||||
specifier: ^10.1.0
|
|
||||||
version: 10.1.0(@types/react@18.3.28)(react@18.3.1)
|
|
||||||
react-router-dom:
|
|
||||||
specifier: ^6.26.0
|
|
||||||
version: 6.30.3(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
|
||||||
remark-gfm:
|
|
||||||
specifier: ^4.0.1
|
|
||||||
version: 4.0.1
|
|
||||||
devDependencies:
|
|
||||||
'@tailwindcss/postcss':
|
|
||||||
specifier: ^4.3.0
|
|
||||||
version: 4.3.0
|
|
||||||
'@types/react':
|
|
||||||
specifier: ^18.3.3
|
|
||||||
version: 18.3.28
|
|
||||||
'@types/react-dom':
|
|
||||||
specifier: ^18.3.0
|
|
||||||
version: 18.3.7(@types/react@18.3.28)
|
|
||||||
'@vitejs/plugin-react':
|
|
||||||
specifier: ^4.3.1
|
|
||||||
version: 4.7.0(vite@5.4.21(@types/node@20.19.41)(lightningcss@1.32.0))
|
|
||||||
tailwindcss:
|
|
||||||
specifier: ^4.3.0
|
|
||||||
version: 4.3.0
|
|
||||||
typescript:
|
|
||||||
specifier: ^5.5.0
|
|
||||||
version: 5.9.3
|
|
||||||
vite:
|
|
||||||
specifier: ^5.3.4
|
|
||||||
version: 5.4.21(@types/node@20.19.41)(lightningcss@1.32.0)
|
|
||||||
|
|
||||||
apps/server:
|
apps/server:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@ai-sdk/openai-compatible':
|
'@ai-sdk/openai-compatible':
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
packages:
|
packages:
|
||||||
- "packages/*"
|
- "packages/*"
|
||||||
- "apps/*"
|
- "apps/*"
|
||||||
- "apps/coder/web"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user