A guided explanation is only useful in a language the reader reads, so the panel header gets a language picker alongside the model one, defaulting to the interface language. Like the model, it is request state rather than a setting: the language travels with the read and the generation, and the one a walkthrough was written in is stored with it, so reopening a review describes what is there instead of what a fresh one would be. Only prose is translated. Hunk aliases resolve back to hunk ids and icon/importance are validated against fixed English values, so a translated one would be dropped by the normalizer — silently losing an anchor or a style. Identifiers and paths stay as they appear in the code. The language is part of the cache key, and a read now asks the cache for the exact request it was given before falling back to the pointer. Without that the panel answered a request to switch languages with the text it already had, leaving the other language unused in the cache. Alongside it: - The answer budget is derived from the resolved model instead of a flat 24k. That number was the same for a 64k-context model and for one that admits to 384k output tokens, and on the latter it was the only reason generation failed: the model spent the whole allowance reasoning and returned nothing. It is now min(96k, max(24k, a quarter of the context)) capped by the catalog's output limit, decided once so the input reserve and the request cannot drift apart. - A read no longer offers Cancel. It is a few hundred milliseconds of git with nothing to cancel, and the button flickered on every model or language change. When the panel is showing a fallback, a banner names what is on screen versus what was asked for — only once the read has settled. - The header keeps one 32px control height and drops its labels below 680px instead of squeezing them to two letters and an ellipsis. Docs and module documentation updated in every locale.
115 lines
4.4 KiB
JavaScript
115 lines
4.4 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,
|
|
language: typeof req.query.language === 'string' ? req.query.language : 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, language } = 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,
|
|
language: typeof language === 'string' ? language : 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');
|
|
}
|
|
});
|
|
}
|