Repository navigation
feat: fork a session from an earlier prompt #4017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b594e07
f72cd67
5f9d80d
24770b1
950b501
e5e316d
8bc5e8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| The `:fork` server endpoint accepts a `prompt_id` to fork the session from an earlier prompt instead of the latest state. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ export interface MainTurnSlice { | |
| readonly records: readonly WireRecord[]; | ||
| readonly cutoffTime?: number; | ||
| readonly lastPrompt?: string; | ||
| readonly lastTurnReason?: 'completed' | 'cancelled' | 'failed'; | ||
| } | ||
|
|
||
| export function assertForkTurnIndex(turnIndex: number | undefined): void { | ||
|
|
@@ -28,10 +29,7 @@ export function sliceMainRecordsAtTurn( | |
| sourceSessionId: string, | ||
| turnIndex: number, | ||
| ): MainTurnSlice { | ||
| const turnStarts: number[] = []; | ||
| for (let index = 0; index < records.length; index += 1) { | ||
| if (isUserVisibleTurnRecord(records[index]!)) turnStarts.push(index); | ||
| } | ||
| const turnStarts = userVisibleTurnStartIndices(records); | ||
| const start = turnStarts[turnIndex]; | ||
| if (start === undefined) { | ||
| throw new Error2( | ||
|
|
@@ -58,6 +56,7 @@ export function sliceMainRecordsAtTurn( | |
| records: retained, | ||
| cutoffTime: cutoffTimes.length === 0 ? undefined : Math.max(...cutoffTimes), | ||
| lastPrompt, | ||
| lastTurnReason: turnOutcomeOfRecords(retained), | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -77,6 +76,45 @@ export function sliceSubagentRecordsAtTime( | |
| return records.slice(0, end); | ||
| } | ||
|
|
||
| export type ForkPromptResolution = | ||
| | { readonly status: 'found'; readonly index: number } | ||
| | { readonly status: 'unknown' } | ||
| | { readonly status: 'ambiguous' }; | ||
|
|
||
| export function resolveForkPromptIndex( | ||
| records: readonly WireRecord[], | ||
| promptId: string, | ||
| ): ForkPromptResolution { | ||
| const turnStarts = userVisibleTurnStartIndices(records); | ||
| const targetIds = new Set<string>([promptId]); | ||
| for (const record of records) { | ||
| if (record.type !== 'turn.steer') continue; | ||
| const promptIds = record['promptIds']; | ||
| if (!Array.isArray(promptIds) || !promptIds.includes(promptId)) continue; | ||
| const messageId = record['messageId']; | ||
| if (typeof messageId === 'string' && messageId.length > 0) targetIds.add(messageId); | ||
| } | ||
| let found: number | undefined; | ||
| let ambiguous = false; | ||
| for (let start = 0; start < turnStarts.length; start += 1) { | ||
| const message = asRecord(records[turnStarts[start]!]!['message']); | ||
| const id = message?.['id']; | ||
| if (typeof id !== 'string' || !targetIds.has(id)) continue; | ||
| if (found !== undefined && found !== start) ambiguous = true; | ||
| found = found ?? start; | ||
| } | ||
|
liruifengv marked this conversation as resolved.
liruifengv marked this conversation as resolved.
|
||
| if (ambiguous) return { status: 'ambiguous' }; | ||
| return found === undefined ? { status: 'unknown' } : { status: 'found', index: found }; | ||
| } | ||
|
|
||
| function userVisibleTurnStartIndices(records: readonly WireRecord[]): number[] { | ||
| const turnStarts: number[] = []; | ||
| for (let index = 0; index < records.length; index += 1) { | ||
| if (isUserVisibleTurnRecord(records[index]!)) turnStarts.push(index); | ||
| } | ||
| return turnStarts; | ||
| } | ||
|
|
||
| function isUserVisibleTurnRecord(record: WireRecord): boolean { | ||
| if (record.type !== 'context.append_message') return false; | ||
| const message = asRecord(record['message']); | ||
|
|
@@ -203,6 +241,30 @@ function recordTime(record: WireRecord): number | undefined { | |
| return undefined; | ||
| } | ||
|
|
||
| function turnOutcomeOfRecords( | ||
| records: readonly WireRecord[], | ||
| ): 'completed' | 'cancelled' | 'failed' | undefined { | ||
| for (let index = records.length - 1; index >= 0; index -= 1) { | ||
| const record = records[index]!; | ||
| if (record.type === 'turn.ended') { | ||
| const reason = record['reason']; | ||
| if (reason === 'completed' || reason === 'cancelled') return reason; | ||
| if (reason === 'failed' || reason === 'blocked') return 'failed'; | ||
| return undefined; | ||
| } | ||
| if (isEngineTurnStartRecord(record) || isUserVisibleTurnRecord(record)) return undefined; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a completed turn is followed by a prompt rejected by Useful? React with 👍 / 👎. |
||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function isEngineTurnStartRecord(record: WireRecord): boolean { | ||
| return ( | ||
| record.type === 'turn.prompt' || | ||
| record.type === 'turn.steer' || | ||
| record.type === 'agent.turn.started' | ||
| ); | ||
| } | ||
|
|
||
| function promptMetadataFromTurnRecord(record: WireRecord): string | undefined { | ||
| if (record.type !== 'context.append_message') return undefined; | ||
| const message = asRecord(record['message']); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.