section 12: Phase 12 — Per-source storage, service and routes

This commit is contained in:
Chris Scott committed 2026-10-08 15:53:27 -04:00
1 parent 11c23da67e
commit d5f56fc061
12 files changed
+459 -152

No files matched your search

+76 -57
View File
@@ -7,6 +7,80 @@ export const WALKTHROUGH_TEXT_MAX_CHARS = 2000;
export const WALKTHROUGH_OUTLINE_PREVIEW_LINES = 6;
export const WALKTHROUGH_OUTLINE_LINE_MAX_CHARS = 160;
export const GitRefSchema = z.string().trim().min(1).max(200).regex(/^[^-\s][^\s]*$/)
export const WalkthroughSourceSchema = z.discriminatedUnion('kind', [
z.object({ kind: z.literal('session') }),
z.object({ kind: z.literal('uncommitted') }),
z.object({ kind: z.literal('staged') }),
z.object({ kind: z.literal('unstaged') }),
z.object({ kind: z.literal('branch'), base: GitRefSchema.optional() }),
z.object({ kind: z.literal('pullRequest'), number: z.number().int().positive(), base: GitRefSchema.optional() }),
])
export type WalkthroughSource = z.infer<typeof WalkthroughSourceSchema>
export const DEFAULT_WALKTHROUGH_SOURCE: WalkthroughSource = { kind: 'session' }
export function walkthroughSourceKey(source: WalkthroughSource): string {
switch (source.kind) {
case 'session':
case 'uncommitted':
case 'staged':
case 'unstaged':
return source.kind
case 'branch':
return `branch:${source.base ?? ''}`
case 'pullRequest':
return `pr:${source.number}:${source.base ?? ''}`
}
}
export function describeWalkthroughSource(source: WalkthroughSource): string {
switch (source.kind) {
case 'session':
return 'session changes'
case 'uncommitted':
return 'uncommitted changes'
case 'staged':
return 'staged changes'
case 'unstaged':
return 'unstaged changes'
case 'branch':
return source.base ? `branch against ${source.base}` : 'branch against the default branch'
case 'pullRequest':
return `pull request #${source.number}`
}
}
export function parseWalkthroughSourceKey(key: string): WalkthroughSource | null {
if (key === 'session' || key === 'uncommitted' || key === 'staged' || key === 'unstaged') {
return { kind: key }
}
if (key.startsWith('branch:')) {
const base = key.slice('branch:'.length)
return parseWalkthroughSourceCandidate(base ? { kind: 'branch', base } : { kind: 'branch' })
}
if (key.startsWith('pr:')) {
const rest = key.slice('pr:'.length)
const separator = rest.indexOf(':')
if (separator < 0) {
return null
}
const number = Number(rest.slice(0, separator))
const base = rest.slice(separator + 1)
return parseWalkthroughSourceCandidate(
base ? { kind: 'pullRequest', number, base } : { kind: 'pullRequest', number },
)
}
return null
}
function parseWalkthroughSourceCandidate(candidate: unknown): WalkthroughSource | null {
const parsed = WalkthroughSourceSchema.safeParse(candidate)
return parsed.success ? parsed.data : null
}
export const WalkthroughHunkSchema = z.object({
id: z.string(),
file: z.string(),
@@ -40,6 +114,7 @@ export type WalkthroughOmittedFile = z.infer<typeof WalkthroughOmittedFileSchema
export const ChangeWalkthroughSchema = z.object({
sessionId: z.string(),
source: WalkthroughSourceSchema,
diffHash: z.string(),
model: z.string().nullable(),
summary: z.string(),
@@ -71,63 +146,7 @@ export type ChangeWalkthroughState = z.infer<typeof ChangeWalkthroughStateSchema
export const GenerateChangeWalkthroughRequestSchema = z.object({
regenerate: z.boolean().optional(),
source: WalkthroughSourceSchema.optional(),
});
export type GenerateChangeWalkthroughRequest = z.infer<typeof GenerateChangeWalkthroughRequestSchema>;
export const GitRefSchema = z.string().trim().min(1).max(200).regex(/^[^-\s][^\s]*$/)
export const WalkthroughSourceSchema = z.discriminatedUnion('kind', [
z.object({ kind: z.literal('session') }),
z.object({ kind: z.literal('uncommitted') }),
z.object({ kind: z.literal('staged') }),
z.object({ kind: z.literal('unstaged') }),
z.object({ kind: z.literal('branch'), base: GitRefSchema.optional() }),
z.object({ kind: z.literal('pullRequest'), number: z.number().int().positive(), base: GitRefSchema.optional() }),
])
export type WalkthroughSource = z.infer<typeof WalkthroughSourceSchema>
export const DEFAULT_WALKTHROUGH_SOURCE: WalkthroughSource = { kind: 'session' }
export function walkthroughSourceKey(source: WalkthroughSource): string {
switch (source.kind) {
case 'session':
case 'uncommitted':
case 'staged':
case 'unstaged':
return source.kind
case 'branch':
return `branch:${source.base ?? ''}`
case 'pullRequest':
return `pr:${source.number}:${source.base ?? ''}`
}
}
export function parseWalkthroughSourceKey(key: string): WalkthroughSource | null {
if (key === 'session' || key === 'uncommitted' || key === 'staged' || key === 'unstaged') {
return { kind: key }
}
if (key.startsWith('branch:')) {
const base = key.slice('branch:'.length)
return parseWalkthroughSourceCandidate(base ? { kind: 'branch', base } : { kind: 'branch' })
}
if (key.startsWith('pr:')) {
const rest = key.slice('pr:'.length)
const separator = rest.indexOf(':')
if (separator < 0) {
return null
}
const number = Number(rest.slice(0, separator))
const base = rest.slice(separator + 1)
return parseWalkthroughSourceCandidate(
base ? { kind: 'pullRequest', number, base } : { kind: 'pullRequest', number },
)
}
return null
}
function parseWalkthroughSourceCandidate(candidate: unknown): WalkthroughSource | null {
const parsed = WalkthroughSourceSchema.safeParse(candidate)
return parsed.success ? parsed.data : null
}