refactor(chatModel):

* create a separate type to represent a response part
* rename existing `ResponsePart` to `InternalResponsePart`
* fix? handle if response part's resolved value is a markdown string, which is allowed in the `IResponse` interface
This commit is contained in:
Ulugbek Abdullaev
2023-09-20 11:01:36 +02:00
parent 029ba9ec98
commit 95f79a91e4

View File

@@ -23,10 +23,21 @@ export interface IChatRequestModel {
readonly response: IChatResponseModel | undefined; readonly response: IChatResponseModel | undefined;
} }
export type ResponsePart =
| string
| IMarkdownString
| { treeData: IChatResponseProgressFileTreeData }
| {
placeholder: string;
resolvedContent?: Promise<
string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData }
>;
};
export interface IResponse { export interface IResponse {
readonly value: (IMarkdownString | IPlaceholderMarkdownString | IChatResponseProgressFileTreeData)[]; readonly value: (IMarkdownString | IPlaceholderMarkdownString | IChatResponseProgressFileTreeData)[];
onDidChangeValue: Event<void>; onDidChangeValue: Event<void>;
updateContent(responsePart: string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData } | { placeholder: string; resolvedContent?: Promise<string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData }> }, quiet?: boolean): void; updateContent(responsePart: ResponsePart, quiet?: boolean): void;
asString(): string; asString(): string;
} }
@@ -93,7 +104,10 @@ export interface IPlaceholderMarkdownString extends IMarkdownString {
isPlaceholder: boolean; isPlaceholder: boolean;
} }
type ResponsePart = { string: IMarkdownString; isPlaceholder?: boolean } | { treeData: IChatResponseProgressFileTreeData; isPlaceholder?: undefined }; type InternalResponsePart =
| { string: IMarkdownString; isPlaceholder?: boolean }
| { treeData: IChatResponseProgressFileTreeData; isPlaceholder?: undefined };
export class Response implements IResponse { export class Response implements IResponse {
private _onDidChangeValue = new Emitter<void>(); private _onDidChangeValue = new Emitter<void>();
public get onDidChangeValue() { public get onDidChangeValue() {
@@ -101,7 +115,7 @@ export class Response implements IResponse {
} }
// responseParts internally tracks all the response parts, including strings which are currently resolving, so that they can be updated when they do resolve // responseParts internally tracks all the response parts, including strings which are currently resolving, so that they can be updated when they do resolve
private _responseParts: ResponsePart[]; private _responseParts: InternalResponsePart[];
// responseData externally presents the response parts with consolidated contiguous strings (including strings which were previously resolving) // responseData externally presents the response parts with consolidated contiguous strings (including strings which were previously resolving)
private _responseData: (IMarkdownString | IPlaceholderMarkdownString | IChatResponseProgressFileTreeData)[]; private _responseData: (IMarkdownString | IPlaceholderMarkdownString | IChatResponseProgressFileTreeData)[];
// responseRepr externally presents the response parts with consolidated contiguous strings (excluding tree data) // responseRepr externally presents the response parts with consolidated contiguous strings (excluding tree data)
@@ -126,7 +140,7 @@ export class Response implements IResponse {
return this._responseRepr; return this._responseRepr;
} }
updateContent(responsePart: string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData } | { placeholder: string; resolvedContent?: Promise<string | { treeData: IChatResponseProgressFileTreeData }> }, quiet?: boolean): void { updateContent(responsePart: ResponsePart, quiet?: boolean): void {
if (typeof responsePart === 'string' || isMarkdownString(responsePart)) { if (typeof responsePart === 'string' || isMarkdownString(responsePart)) {
const responsePartLength = this._responseParts.length - 1; const responsePartLength = this._responseParts.length - 1;
const lastResponsePart = this._responseParts[responsePartLength]; const lastResponsePart = this._responseParts[responsePartLength];
@@ -154,6 +168,9 @@ export class Response implements IResponse {
if (typeof content === 'string') { if (typeof content === 'string') {
this._responseParts[responsePosition] = { string: new MarkdownString(content), isPlaceholder: true }; this._responseParts[responsePosition] = { string: new MarkdownString(content), isPlaceholder: true };
this._updateRepr(quiet); this._updateRepr(quiet);
} else if ('value' in content) {
this._responseParts[responsePosition] = { string: content, isPlaceholder: true };
this._updateRepr(quiet);
} else if (content.treeData) { } else if (content.treeData) {
this._responseParts[responsePosition] = { treeData: content.treeData }; this._responseParts[responsePosition] = { treeData: content.treeData };
this._updateRepr(quiet); this._updateRepr(quiet);
@@ -257,7 +274,7 @@ export class ChatResponseModel extends Disposable implements IChatResponseModel
this._id = 'response_' + ChatResponseModel.nextId++; this._id = 'response_' + ChatResponseModel.nextId++;
} }
updateContent(responsePart: string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData } | { placeholder: string; resolvedContent?: Promise<string | IMarkdownString | { treeData: IChatResponseProgressFileTreeData }> }, quiet?: boolean) { updateContent(responsePart: ResponsePart, quiet?: boolean) {
this._response.updateContent(responsePart, quiet); this._response.updateContent(responsePart, quiet);
} }