A diff is ordered by file path, which is almost never the order in which a change makes sense. This adds a Walkthrough surface that reorders it: the model groups related hunks into stops, explains what each group changes about behavior, and orders the stops so each builds on the last. It explains and orders; judging code stays with the existing Review action. Reviews uncommitted work (all, staged, unstaged), a branch against its base, or a pull request. Generation is always user-initiated — nothing runs on a timer, on a file change, or as a side effect of opening a panel. Invariants worth preserving: - Hunk identity is derived on the server and only there. Ids are content hashes, so an anchor that no longer resolves is proof the code it described changed, and staleness needs no heuristics. The client matches ids to ids and never recomputes them; two implementations would have to agree forever. - The digest is never truncated. A diff that does not fit the model's context is refused with an actionable reason, because a walkthrough written against half a diff reads as confident and is wrong. - Nothing disappears. Lockfiles and other generated output are excluded from the model's input by name — never by size — and everything no stop covers is listed at the end, so "have I seen all of it" stays answerable. - Cost is explicit. Results are content-addressed, so returning the working tree to an earlier state costs nothing; generation outlives its request, so a refresh detaches the client rather than discarding paid-for work, and only an explicit cancel stops it. Supporting changes to shared modules: - git: expose the existing getRangeDiff as GET /api/git listUntrackedPaths and getUntrackedDiffs. The latter resolve the repository once for a batch instead of per file, taking a panel ~340ms on an 80-file working tree. - small-model: structured output across four wire forma and abort signal, and an onOverflow policy so an oversized prompt fails loudly instead of being silently clipped. A provider remembered so the prompt-side fallback goes first next time. - models.dev metadata: surface structured_output as tri false blocks a model, a missing field does not, because the catalog omits it for roughly half of all models. Desktop and tablet only: VS Code serves Git through its these routes, and the mobile shell does not consume the surface registry. Docs: packages/docs walkthrough page in English and all eight locales.
108 lines
4.2 KiB
JavaScript
108 lines
4.2 KiB
JavaScript
// `req.destroyed` is true for every healthy request once the body parser has
|
|
// consumed the stream, so using it as a disconnect check silently swallows every
|
|
// response. The response socket is the one that actually reflects whether the
|
|
// client is still there.
|
|
const clientIsGone = (res) => res.writableEnded || res.destroyed;
|
|
|
|
export function registerWalkthroughRoutes(app, { getWalkthroughService }) {
|
|
const respondWithError = (res, error, fallback) => {
|
|
const statusCode = Number(error?.statusCode) || 500;
|
|
if (statusCode >= 500) {
|
|
console.error(`${fallback}:`, error);
|
|
}
|
|
res.status(statusCode).json({
|
|
error: error?.message || fallback,
|
|
...(error?.code ? { code: error.code } : {}),
|
|
...(error?.model ? { model: error.model } : {}),
|
|
...(Number.isFinite(error?.requiredChars) ? { requiredChars: error.requiredChars } : {}),
|
|
...(Number.isFinite(error?.availableChars) ? { availableChars: error.availableChars } : {}),
|
|
});
|
|
};
|
|
|
|
const readSource = (value) => {
|
|
if (typeof value !== 'string' || !value) return null;
|
|
try {
|
|
return JSON.parse(value);
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
app.get('/api/walkthrough', async (req, res) => {
|
|
try {
|
|
const { getWalkthrough, getPullRequestDiff } = await getWalkthroughService();
|
|
const directory = typeof req.query.directory === 'string' ? req.query.directory : '';
|
|
if (!directory) {
|
|
return res.status(400).json({ error: 'directory parameter is required' });
|
|
}
|
|
|
|
const result = await getWalkthrough(
|
|
{
|
|
directory,
|
|
source: readSource(req.query.source),
|
|
model: typeof req.query.model === 'string' ? req.query.model : undefined,
|
|
},
|
|
{ getPullRequestDiff },
|
|
);
|
|
res.json(result);
|
|
} catch (error) {
|
|
respondWithError(res, error, 'Failed to load walkthrough');
|
|
}
|
|
});
|
|
|
|
// Deliberately not aborted when the client disconnects: generation runs for
|
|
// minutes and a refresh must not throw the work away. Leaving detaches the
|
|
// client; the job finishes and caches its result. Stopping is an explicit
|
|
// request below.
|
|
app.post('/api/walkthrough/generate', async (req, res) => {
|
|
try {
|
|
const { generateWalkthrough, getPullRequestDiff } = await getWalkthroughService();
|
|
const { directory, source, force, model } = req.body || {};
|
|
if (!directory || typeof directory !== 'string') {
|
|
return res.status(400).json({ error: 'directory is required' });
|
|
}
|
|
|
|
const result = await generateWalkthrough(
|
|
{ directory, source, force: force === true, model: typeof model === 'string' ? model : undefined },
|
|
{ getPullRequestDiff },
|
|
);
|
|
if (clientIsGone(res)) return;
|
|
res.json(result);
|
|
} catch (error) {
|
|
if (clientIsGone(res)) return;
|
|
respondWithError(res, error, 'Failed to generate walkthrough');
|
|
}
|
|
});
|
|
|
|
// Memory-only, so it is safe to poll while a generation runs. The full read
|
|
// re-runs the whole git pipeline and must not be used for this.
|
|
app.get('/api/walkthrough/progress', async (req, res) => {
|
|
try {
|
|
const { getGenerationStage, getRepositoryRootFor } = await getWalkthroughService();
|
|
const directory = typeof req.query.directory === 'string' ? req.query.directory : '';
|
|
if (!directory) {
|
|
return res.status(400).json({ error: 'directory parameter is required' });
|
|
}
|
|
|
|
const { repoRoot, sourceKey } = await getRepositoryRootFor(directory, readSource(req.query.source));
|
|
res.json({ stage: getGenerationStage(repoRoot, sourceKey) });
|
|
} catch (error) {
|
|
respondWithError(res, error, 'Failed to read walkthrough progress');
|
|
}
|
|
});
|
|
|
|
app.post('/api/walkthrough/cancel', async (req, res) => {
|
|
try {
|
|
const { cancelWalkthroughGeneration } = await getWalkthroughService();
|
|
const { directory, source } = req.body || {};
|
|
if (!directory || typeof directory !== 'string') {
|
|
return res.status(400).json({ error: 'directory is required' });
|
|
}
|
|
|
|
res.json(await cancelWalkthroughGeneration({ directory, source }));
|
|
} catch (error) {
|
|
respondWithError(res, error, 'Failed to cancel walkthrough generation');
|
|
}
|
|
});
|
|
}
|