Skip to content

Commit bdcb53a

Browse files
committed
feat: the code panel leads with what changed - git is the source, the full tree is one toggle away - and the chat manages itself from its own topbar
1 parent 70dac9e commit bdcb53a

8 files changed

Lines changed: 414 additions & 24 deletions

File tree

‎backend/src/project/files.controller.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,37 @@ export class FilesController {
8383
private readonly workspaces: WorkspaceService,
8484
) {}
8585

86+
/**
87+
* What the agent changed, straight from git — every workspace starts as a
88+
* clone of the template, so `status --porcelain` is the honest diff. 204
89+
* when there is no git baseline; the client falls back to the full tree.
90+
*/
91+
@Get('project/changes')
92+
@UseGuards(JWTAuthGuard)
93+
async changes(@Req() req: Request, @Query('path') projectId?: string) {
94+
if (!projectId) throw new BadRequestException('Missing path');
95+
await assertProjectAccess({
96+
projects: this.projects,
97+
req,
98+
projectPath: projectId,
99+
write: false,
100+
});
101+
102+
const workspace = await this.workspaces.for(projectId);
103+
const changes = await workspace.changedFiles();
104+
return { changes };
105+
}
106+
86107
@Get('project')
87108
@UseGuards(JWTAuthGuard)
88109
async tree(@Req() req: Request, @Query('path') projectId?: string) {
89110
if (!projectId) throw new BadRequestException('Missing path');
90-
await assertProjectAccess({ projects: this.projects, req, projectPath: projectId, write: false });
111+
await assertProjectAccess({
112+
projects: this.projects,
113+
req,
114+
projectPath: projectId,
115+
write: false,
116+
});
91117

92118
const workspace = await this.workspaces.for(projectId);
93119
const paths = await workspace.listFiles();
@@ -191,7 +217,12 @@ export class FilesController {
191217
@UseGuards(JWTAuthGuard)
192218
async read(@Req() req: Request, @Query('path') filePath?: string) {
193219
if (!filePath) throw new BadRequestException("Missing 'path'");
194-
await assertProjectAccess({ projects: this.projects, req, projectPath: filePath, write: false });
220+
await assertProjectAccess({
221+
projects: this.projects,
222+
req,
223+
projectPath: filePath,
224+
write: false,
225+
});
195226

196227
const [projectPath, ...rest] = filePath.split('/');
197228
const workspace = await this.workspaces.for(projectPath);
@@ -211,7 +242,12 @@ export class FilesController {
211242
if (!filePath || newContent == null) {
212243
throw new BadRequestException("Missing 'filePath' or 'newContent'");
213244
}
214-
await assertProjectAccess({ projects: this.projects, req, projectPath: filePath, write: true });
245+
await assertProjectAccess({
246+
projects: this.projects,
247+
req,
248+
projectPath: filePath,
249+
write: true,
250+
});
215251

216252
const [projectPath, ...rest] = filePath.split('/');
217253
try {

‎backend/src/project/host-workspace.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
import { promises as fs, createWriteStream, existsSync, mkdirSync } from 'node:fs';
1+
import {
2+
promises as fs,
3+
createWriteStream,
4+
existsSync,
5+
mkdirSync,
6+
} from 'node:fs';
27
import * as path from 'node:path';
38
import { BadRequestException, NotFoundException } from '@nestjs/common';
49
import archiver from 'archiver';
510
import { getProjectsDir, getTempDir } from '../common/utils/common-path';
611
import type { LogLine, PreviewService } from './preview.service';
7-
import { IGNORED_ENTRIES, ProjectWorkspace } from './workspace';
12+
import {
13+
ChangedFile,
14+
IGNORED_ENTRIES,
15+
parsePorcelain,
16+
ProjectWorkspace,
17+
} from './workspace';
18+
import { execFile } from 'node:child_process';
19+
import { promisify } from 'node:util';
20+
21+
const execFileAsync = promisify(execFile);
822

923
const IGNORED = new Set(IGNORED_ENTRIES);
1024

@@ -61,6 +75,20 @@ export class HostWorkspace implements ProjectWorkspace {
6175
return out;
6276
}
6377

78+
async changedFiles(): Promise<ChangedFile[] | null> {
79+
if (!existsSync(path.join(this.root, '.git'))) return null;
80+
try {
81+
const { stdout } = await execFileAsync(
82+
'git',
83+
['-C', this.root, 'status', '--porcelain'],
84+
{ maxBuffer: 4 * 1024 * 1024 },
85+
);
86+
return parsePorcelain(stdout);
87+
} catch {
88+
return null;
89+
}
90+
}
91+
6492
async listFiles(): Promise<string[]> {
6593
// A project whose directory is gone must error, not answer "no files" —
6694
// an empty 200 here is indistinguishable from a real empty project, and
@@ -138,7 +166,9 @@ export class HostWorkspace implements ProjectWorkspace {
138166
'**/*',
139167
{
140168
cwd: this.root,
141-
ignore: IGNORED_ENTRIES.map((p) => `**/${p}/**`).concat(IGNORED_ENTRIES),
169+
ignore: IGNORED_ENTRIES.map((p) => `**/${p}/**`).concat(
170+
IGNORED_ENTRIES,
171+
),
142172
dot: true,
143173
},
144174
{},

‎backend/src/project/vercel-workspace.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import type { Sandbox } from '@vercel/sandbox';
66
import { SANDBOX_ROOT as ROOT } from '../chat/sandbox-provider';
77
import { getTempDir } from '../common/utils/common-path';
88
import type { LogLine } from './preview.service';
9-
import { IGNORED_ENTRIES, ProjectWorkspace } from './workspace';
9+
import {
10+
ChangedFile,
11+
IGNORED_ENTRIES,
12+
parsePorcelain,
13+
ProjectWorkspace,
14+
} from './workspace';
1015

1116
/** Bounded so one enormous project cannot exhaust the backend's memory. */
1217
const MAX_FILES = 5000;
@@ -37,6 +42,17 @@ export class VercelWorkspace implements ProjectWorkspace {
3742
return full;
3843
}
3944

45+
async changedFiles(): Promise<ChangedFile[] | null> {
46+
const result = await this.sandbox.runCommand({
47+
cmd: 'sh',
48+
args: ['-lc', 'git status --porcelain 2>/dev/null || echo __NO_GIT__'],
49+
cwd: ROOT,
50+
});
51+
const out = await result.stdout();
52+
if (out.includes('__NO_GIT__')) return null;
53+
return parsePorcelain(out);
54+
}
55+
4056
async listFiles(): Promise<string[]> {
4157
// `find` rather than a recursive walk over the API: one round trip instead
4258
// of one per directory, which over a network is the difference between a
@@ -248,7 +264,9 @@ export class VercelWorkspace implements ProjectWorkspace {
248264
await this.sandbox
249265
.stop()
250266
.catch((error: unknown) =>
251-
this.logger.warn(`Could not stop ${this.sandbox.name}: ${String(error)}`),
267+
this.logger.warn(
268+
`Could not stop ${this.sandbox.name}: ${String(error)}`,
269+
),
252270
);
253271
}
254272
}

‎backend/src/project/workspace.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ import type { LogLine } from './preview.service';
1313
* Two implementations sit behind this: the host disk, and a Vercel microVM.
1414
* Nothing above this interface knows which one it is talking to.
1515
*/
16+
/** One file the project has diverged from its template on. */
17+
export interface ChangedFile {
18+
path: string;
19+
status: 'added' | 'modified' | 'deleted';
20+
}
21+
1622
export interface ProjectWorkspace {
1723
/**
1824
* Every file in the project, as paths relative to its root, with shared
1925
* dependencies and build output left out.
2026
*/
2127
listFiles(): Promise<string[]>;
2228

29+
/**
30+
* What the agent has actually touched — the project relative to its git
31+
* baseline (every workspace starts as a clone of the template). Null when
32+
* there is no git history to diff against.
33+
*/
34+
changedFiles(): Promise<ChangedFile[] | null>;
35+
2336
/** File contents as text, or null when there is no such file. */
2437
readFile(relativePath: string): Promise<string | null>;
2538

@@ -72,3 +85,32 @@ export const IGNORED_ENTRIES = [
7285
'coverage',
7386
'.DS_Store',
7487
];
88+
89+
/**
90+
* `git status --porcelain` lines into ChangedFile records. Renames report the
91+
* new path; the ignore list keeps agent scratch out of the view.
92+
*/
93+
export function parsePorcelain(output: string): ChangedFile[] {
94+
const changes: ChangedFile[] = [];
95+
for (const line of output.split('\n')) {
96+
if (!line.trim()) continue;
97+
const flags = line.slice(0, 2);
98+
let file = line.slice(3).trim();
99+
const arrow = file.indexOf(' -> ');
100+
if (arrow !== -1) file = file.slice(arrow + 4);
101+
file = file.replace(/^"|"$/g, '');
102+
if (
103+
IGNORED_ENTRIES.some(
104+
(name) => file === name || file.startsWith(`${name}/`),
105+
)
106+
)
107+
continue;
108+
const status: ChangedFile['status'] = flags.includes('D')
109+
? 'deleted'
110+
: flags === '??' || flags.includes('A')
111+
? 'added'
112+
: 'modified';
113+
changes.push({ path: file, status });
114+
}
115+
return changes;
116+
}

‎frontend/src/components/chat/chat-panel.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function ChatContent({
6363
className="flex h-full w-full flex-col justify-between"
6464
>
6565
<div className="sticky top-0 z-10">
66-
<ChatTopbar chatId={chatId} />
66+
<ChatTopbar chatId={chatId} onHistoryCleared={() => setMessages([])} />
6767
</div>
6868

6969
<div className="flex-grow overflow-hidden">

0 commit comments

Comments
 (0)