import { IObservable } from "../../../../../base/common/observable.js"; import { URI } from "../../../../../base/common/uri.js"; import type { AgentHostUriMapper } from "../../../../../platform/agentHost/common/agentHostUri.js"; import { FileEditKind, type Turn } from "../../../../../platform/agentHost/common/state/sessionState.js"; import { IChatSessionFileChange2 } from "../../../../../workbench/contrib/chat/common/chatSessionsService.js"; import { ISessionChatCustomization, ISessionTurnFileChange, ISessionWorkspace } from "../../../../services/sessions/common/session.js"; import { type CustomizationIndexFactory, type ICustomizationJobScheduler } from "../../../../../workbench/contrib/chat/common/agentHostSessionCustomizations.js"; import { IAgentHostAdapterOptions } from "./baseAgentHostSessionsProvider.js"; /** * A single file edit emitted by a tool call, decoded from the protocol so the * reducer can classify it. Ordered so creations seen before edits keep the * "created" classification. */ export interface IParsedFileEdit { readonly kind: FileEditKind; /** After-state URI (create/edit/rename target). */ readonly afterUri?: URI; /** Before-state URI (delete source / rename origin). */ readonly beforeUri?: URI; /** Before-content URI, used to render a diff for modified files. */ readonly beforeContentUri?: URI; /** After-content URI, used to render the read-only modified side. */ readonly afterContentUri?: URI; /** Lines added by this edit, from the protocol diff metadata (0 when absent). */ readonly insertions: number; /** Lines removed by this edit, from the protocol diff metadata (0 when absent). */ readonly deletions: number; } /** * The observable outputs derived from an agent-host session's live output * stream (its chat-state turns), exposed per chat and computed on demand so a * chat nobody asks about is never subscribed to. */ export interface ISessionOutputObs { /** * Returns the advertised chat's last-turn file edits, classified against the session workspace. */ getLastTurnChanges(chatUri: URI): IObservable; /** * Returns the customizations a specific chat used or read, keyed by that * chat's AHP chat URI. Ordered by first reference and de-duplicated. */ getChatCustomizations(chatUri: URI): IObservable; /** * Drops cached observables and parser state for a removed advertised chat, including a former default. */ releaseChat(chatUri: URI): void; } /** * Builds the observable outputs derived from a session's live output stream. * * The data is parsed from the agent-host chat-state turns: the turn's response * parts are scanned for tool calls, and each tool call's file-edit results (and * pending edits) are collected. * * Everything is scoped to a single chat and produced on demand: asking for a * chat's {@link ISessionOutputObs.getLastTurnChanges | last-turn changes} or * {@link ISessionOutputObs.getChatCustomizations | customizations} is what * opens that chat's state subscription, so peer chats nobody reads cost * nothing. Only the chat's last turn is ever parsed for file edits — completed * earlier turns are never walked. * * Computation only happens for the active, non-archived session: archived * sessions never open a live chat-state subscription, so no parsing work is * done for them. */ export declare function createSessionOutputObs(sessionUri: URI, options: IAgentHostAdapterOptions, isActiveSessionObs: IObservable, isArchivedObs: IObservable, workspaceObs: IObservable, cache: Map, createCustomizationIndex?: CustomizationIndexFactory, customizationScheduler?: ICustomizationJobScheduler): ISessionOutputObs; /** * Minimal shape of a turn needed to parse its file edits. {@link Turn} is * structurally assignable to this, so production passes a real `ChatState` * while tests can build lightweight fixtures. */ export interface IFileEditTurn { readonly id: string; readonly responseParts: Turn["responseParts"]; } /** A chat state reduced to just the fields needed to parse its file edits. */ export interface IFileEditChatState { readonly turns?: readonly IFileEditTurn[]; readonly activeTurn?: { readonly responseParts: Turn["responseParts"]; }; } /** Parses the file edits contained in a single turn's response parts. */ export type ParseTurnFileEdits = (responseParts: Turn["responseParts"]) => readonly IParsedFileEdit[]; /** * Creates a stateful parser that extracts a chat's **last turn** file edits. * * The last turn is the in-progress `activeTurn` when streaming, otherwise the * most recently completed turn. Completed turns are immutable once finalized, * so the completed last turn is parsed once and memoized by its id; only the * in-progress turn is re-parsed on every streamed delta. Earlier turns are * never walked, so the cost of a delta is O(last turn) regardless of how long * the chat has been running. * * @param mapDiffUri Optional URI mapper applied while parsing. * @param parseTurn Per-turn parse function. Defaults to {@link parseResponseParts}; * injectable so tests can observe how often each turn is (re)parsed. */ export declare function createIncrementalChatFileEditsParser(mapDiffUri?: AgentHostUriMapper, parseTurn?: ParseTurnFileEdits): (chatState: IFileEditChatState) => readonly IParsedFileEdit[]; /** Parses the file edits contained in a turn's response parts (stateless). */ export declare function parseResponseParts(responseParts: Turn["responseParts"], mapDiffUri?: AgentHostUriMapper): IParsedFileEdit[]; /** * Reduces a single turn's parsed file edits into one {@link ISessionTurnFileChange} * per file, aggregating diff stats. Mirrors the "Last Turn Changes" changeset * so consumers (e.g. the chat input status pills) can reflect the last turn * straight from the output stream. * * Rules: * - Repeated edits to the same file collapse into a single change whose * insertions/deletions are the sum of the individual edits. * - A file created during the turn stays a creation (no original side) even if * edited afterwards. * - A create/edit followed by a delete in the same turn nets out; a pre-existing * file deleted during the turn is surfaced as a deletion (no modified side to * preview) but still counted in the stats. * - Renames drop the source and surface the target as an edit of its * before-content, matching the changeset's classification. * - Every change records whether its resource is outside all workspace roots. */ export declare function reduceTurnChanges(edits: readonly IParsedFileEdit[], folderRoots?: readonly URI[], cache?: Map): (IChatSessionFileChange2 & ISessionTurnFileChange)[];