Skip to content
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

Update ChatModel naming #14913

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ChatAgent,
ChatMessage,
ChatModel,
ChatRequestModelImpl,
MutableChatRequestModel,
lastProgressMessage,
QuestionResponseContentImpl,
unansweredQuestions
Expand Down Expand Up @@ -127,7 +127,7 @@ export class AskAndContinueChatAgent extends AbstractStreamParsingChatAgent {
this.contentMatchers.push({
start: /^<question>.*$/m,
end: /^<\/question>$/m,
contentFactory: (content: string, request: ChatRequestModelImpl) => {
contentFactory: (content: string, request: MutableChatRequestModel) => {
const question = content.replace(/^<question>\n|<\/question>$/g, '');
const parsedQuestion = JSON.parse(question);
return new QuestionResponseContentImpl(parsedQuestion.question, parsedQuestion.options, request, selectedOption => {
Expand All @@ -137,7 +137,7 @@ export class AskAndContinueChatAgent extends AbstractStreamParsingChatAgent {
});
}

protected override async onResponseComplete(request: ChatRequestModelImpl): Promise<void> {
protected override async onResponseComplete(request: MutableChatRequestModel): Promise<void> {
const unansweredQs = unansweredQuestions(request);
if (unansweredQs.length < 1) {
return super.onResponseComplete(request);
Expand All @@ -146,7 +146,7 @@ export class AskAndContinueChatAgent extends AbstractStreamParsingChatAgent {
request.response.waitForInput();
}

protected handleAnswer(selectedOption: { text: string; value?: string; }, request: ChatRequestModelImpl): void {
protected handleAnswer(selectedOption: { text: string; value?: string; }, request: MutableChatRequestModel): void {
const progressMessage = lastProgressMessage(request);
if (progressMessage) {
request.response.updateProgressMessage({ ...progressMessage, show: 'untilFirstContent', status: 'completed' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
AbstractStreamParsingChatAgent,
ChangeSetImpl,
ChatAgent,
ChatRequestModelImpl,
MutableChatRequestModel,
MarkdownChatResponseContentImpl,
SystemMessageDescription
} from '@theia/ai-chat';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class ChangeSetChatAgent extends AbstractStreamParsingChatAgent {
@inject(ChangeSetFileElementFactory)
protected readonly fileChangeFactory: ChangeSetFileElementFactory;

override async invoke(request: ChatRequestModelImpl): Promise<void> {
override async invoke(request: MutableChatRequestModel): Promise<void> {
const roots = this.workspaceService.tryGetRoots();
if (roots.length === 0) {
request.response.response.addContent(new MarkdownChatResponseContentImpl(
Expand Down
24 changes: 12 additions & 12 deletions packages/ai-chat/src/common/chat-agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { inject, injectable, named, postConstruct } from '@theia/core/shared/inv
import { ChatAgentService } from './chat-agent-service';
import {
ChatModel,
ChatRequestModelImpl,
MutableChatRequestModel,
ChatResponseContent,
ErrorChatResponseContentImpl,
MarkdownChatResponseContentImpl,
Expand Down Expand Up @@ -117,7 +117,7 @@ export const ChatAgent = Symbol('ChatAgent');
export interface ChatAgent extends Agent {
locations: ChatAgentLocation[];
iconClass?: string;
invoke(request: ChatRequestModelImpl, chatAgentService?: ChatAgentService): Promise<void>;
invoke(request: MutableChatRequestModel, chatAgentService?: ChatAgentService): Promise<void>;
}

@injectable()
Expand Down Expand Up @@ -161,7 +161,7 @@ export abstract class AbstractChatAgent implements ChatAgent {
this.contentMatchers.push(...contributedContentMatchers);
}

async invoke(request: ChatRequestModelImpl): Promise<void> {
async invoke(request: MutableChatRequestModel): Promise<void> {
try {
const languageModel = await this.getLanguageModel(this.defaultLanguageModelPurpose);
if (!languageModel) {
Expand Down Expand Up @@ -213,7 +213,7 @@ export abstract class AbstractChatAgent implements ChatAgent {
}
}

protected parseContents(text: string, request: ChatRequestModelImpl): ChatResponseContent[] {
protected parseContents(text: string, request: MutableChatRequestModel): ChatResponseContent[] {
return parseContents(
text,
request,
Expand All @@ -222,7 +222,7 @@ export abstract class AbstractChatAgent implements ChatAgent {
);
};

protected handleError(request: ChatRequestModelImpl, error: Error): void {
protected handleError(request: MutableChatRequestModel, error: Error): void {
request.response.response.addContent(new ErrorChatResponseContentImpl(error));
request.response.error(error);
}
Expand Down Expand Up @@ -303,17 +303,17 @@ export abstract class AbstractChatAgent implements ChatAgent {
* The default implementation sets the state of the response to `complete`.
* Subclasses may override this method to perform additional actions or keep the response open for processing further requests.
*/
protected async onResponseComplete(request: ChatRequestModelImpl): Promise<void> {
protected async onResponseComplete(request: MutableChatRequestModel): Promise<void> {
return request.response.complete();
}

protected abstract addContentsToResponse(languageModelResponse: LanguageModelResponse, request: ChatRequestModelImpl): Promise<void>;
protected abstract addContentsToResponse(languageModelResponse: LanguageModelResponse, request: MutableChatRequestModel): Promise<void>;
}

@injectable()
export abstract class AbstractTextToModelParsingChatAgent<T> extends AbstractChatAgent {

protected async addContentsToResponse(languageModelResponse: LanguageModelResponse, request: ChatRequestModelImpl): Promise<void> {
protected async addContentsToResponse(languageModelResponse: LanguageModelResponse, request: MutableChatRequestModel): Promise<void> {
const responseAsText = await getTextOfResponse(languageModelResponse);
const parsedCommand = await this.parseTextResponse(responseAsText);
const content = this.createResponseContent(parsedCommand, request);
Expand All @@ -322,13 +322,13 @@ export abstract class AbstractTextToModelParsingChatAgent<T> extends AbstractCha

protected abstract parseTextResponse(text: string): Promise<T>;

protected abstract createResponseContent(parsedModel: T, request: ChatRequestModelImpl): ChatResponseContent;
protected abstract createResponseContent(parsedModel: T, request: MutableChatRequestModel): ChatResponseContent;
}

@injectable()
export abstract class AbstractStreamParsingChatAgent extends AbstractChatAgent {

protected override async addContentsToResponse(languageModelResponse: LanguageModelResponse, request: ChatRequestModelImpl): Promise<void> {
protected override async addContentsToResponse(languageModelResponse: LanguageModelResponse, request: MutableChatRequestModel): Promise<void> {
if (isLanguageModelTextResponse(languageModelResponse)) {
const contents = this.parseContents(languageModelResponse.text, request);
request.response.response.addContents(contents);
Expand All @@ -348,7 +348,7 @@ export abstract class AbstractStreamParsingChatAgent extends AbstractChatAgent {
);
}

protected async addStreamResponse(languageModelResponse: LanguageModelStreamResponse, request: ChatRequestModelImpl): Promise<void> {
protected async addStreamResponse(languageModelResponse: LanguageModelStreamResponse, request: MutableChatRequestModel): Promise<void> {
for await (const token of languageModelResponse.stream) {
const newContents = this.parse(token, request);
if (isArray(newContents)) {
Expand All @@ -375,7 +375,7 @@ export abstract class AbstractStreamParsingChatAgent extends AbstractChatAgent {
}
}

protected parse(token: LanguageModelStreamResponsePart, request: ChatRequestModelImpl): ChatResponseContent | ChatResponseContent[] {
protected parse(token: LanguageModelStreamResponsePart, request: MutableChatRequestModel): ChatResponseContent | ChatResponseContent[] {
const content = token.content;
// eslint-disable-next-line no-null/no-null
if (content !== undefined && content !== null) {
Expand Down
36 changes: 18 additions & 18 deletions packages/ai-chat/src/common/chat-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export interface QuestionResponseContent extends ChatResponseContent {
options: { text: string, value?: string }[];
selectedOption?: { text: string, value?: string };
handler: QuestionResponseHandler;
request: ChatRequestModelImpl;
request: MutableChatRequestModel;
}

export namespace QuestionResponseContent {
Expand All @@ -390,7 +390,7 @@ export namespace QuestionResponseContent {
'handler' in obj &&
typeof (obj as { handler: unknown }).handler === 'function' &&
'request' in obj &&
obj.request instanceof ChatRequestModelImpl
obj.request instanceof MutableChatRequestModel
);
}
}
Expand Down Expand Up @@ -459,11 +459,11 @@ export interface ChatResponseModel {
* Implementations
**********************/

export class ChatModelImpl implements ChatModel {
export class MutableChatModel implements ChatModel {
protected readonly _onDidChangeEmitter = new Emitter<ChatChangeEvent>();
onDidChange: Event<ChatChangeEvent> = this._onDidChangeEmitter.event;

protected _requests: ChatRequestModelImpl[];
protected _requests: MutableChatRequestModel[];
protected _id: string;
protected _changeSetListener?: Disposable;
protected _changeSet?: ChangeSetImpl;
Expand All @@ -474,11 +474,11 @@ export class ChatModelImpl implements ChatModel {
this._id = generateUuid();
}

getRequests(): ChatRequestModelImpl[] {
getRequests(): MutableChatRequestModel[] {
return this._requests;
}

getRequest(id: string): ChatRequestModelImpl | undefined {
getRequest(id: string): MutableChatRequestModel | undefined {
return this._requests.find(request => request.id === id);
}

Expand Down Expand Up @@ -522,8 +522,8 @@ export class ChatModelImpl implements ChatModel {
}
}

addRequest(parsedChatRequest: ParsedChatRequest, agentId?: string, context: ResolvedAIVariable[] = []): ChatRequestModelImpl {
const requestModel = new ChatRequestModelImpl(this, parsedChatRequest, agentId, context);
addRequest(parsedChatRequest: ParsedChatRequest, agentId?: string, context: ResolvedAIVariable[] = []): MutableChatRequestModel {
const requestModel = new MutableChatRequestModel(this, parsedChatRequest, agentId, context);
this._requests.push(requestModel);
this._onDidChangeEmitter.fire({
kind: 'addRequest',
Expand Down Expand Up @@ -586,22 +586,22 @@ export class ChangeSetImpl implements ChangeSet {
}
}

export class ChatRequestModelImpl implements ChatRequestModel {
export class MutableChatRequestModel implements ChatRequestModel {
protected readonly _id: string;
protected _session: ChatModelImpl;
protected _session: MutableChatModel;
protected _request: ChatRequest;
protected _response: ChatResponseModelImpl;
protected _response: MutableChatResponseModel;
protected _context: ResolvedAIVariable[];
protected _agentId?: string;
protected _data: { [key: string]: unknown };

constructor(session: ChatModelImpl, public readonly message: ParsedChatRequest, agentId?: string,
constructor(session: MutableChatModel, public readonly message: ParsedChatRequest, agentId?: string,
context: ResolvedAIVariable[] = [], data: { [key: string]: unknown } = {}) {
// TODO accept serialized data as a parameter to restore a previously saved ChatRequestModel
this._request = message.request;
this._id = generateUuid();
this._session = session;
this._response = new ChatResponseModelImpl(this._id, agentId);
this._response = new MutableChatResponseModel(this._id, agentId);
this._context = context.concat(message.parts.filter(part => part.kind === 'var').map(part => (part as ParsedChatRequestVariablePart).resolution));
this._agentId = agentId;
this._data = data;
Expand All @@ -623,15 +623,15 @@ export class ChatRequestModelImpl implements ChatRequestModel {
return this._id;
}

get session(): ChatModelImpl {
get session(): MutableChatModel {
return this._session;
}

get request(): ChatRequest {
return this._request;
}

get response(): ChatResponseModelImpl {
get response(): MutableChatResponseModel {
return this._response;
}

Expand Down Expand Up @@ -864,7 +864,7 @@ export class QuestionResponseContentImpl implements QuestionResponseContent {
readonly kind = 'question';
protected _selectedOption: { text: string; value?: string } | undefined;
constructor(public question: string, public options: { text: string, value?: string }[],
public request: ChatRequestModelImpl, public handler: QuestionResponseHandler) {
public request: MutableChatRequestModel, public handler: QuestionResponseHandler) {
}
set selectedOption(option: { text: string; value?: string; } | undefined) {
this._selectedOption = option;
Expand Down Expand Up @@ -963,7 +963,7 @@ class ChatResponseImpl implements ChatResponse {
}
}

class ChatResponseModelImpl implements ChatResponseModel {
class MutableChatResponseModel implements ChatResponseModel {
protected readonly _onDidChangeEmitter = new Emitter<void>();
onDidChange: Event<void> = this._onDidChangeEmitter.event;

Expand Down Expand Up @@ -1103,7 +1103,7 @@ class ChatResponseModelImpl implements ChatResponseModel {
}
}

export class ErrorChatResponseModelImpl extends ChatResponseModelImpl {
export class ErrorChatResponseModel extends MutableChatResponseModel {
constructor(requestId: string, error: Error, agentId?: string) {
super(requestId, agentId);
this.error(error);
Expand Down
10 changes: 5 additions & 5 deletions packages/ai-chat/src/common/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import { inject, injectable, optional } from '@theia/core/shared/inversify';
import {
ChatModel,
ChatModelImpl,
MutableChatModel,
ChatRequest,
ChatRequestModel,
ChatResponseModel,
ErrorChatResponseModelImpl,
ErrorChatResponseModel,
} from './chat-model';
import { ChatAgentService } from './chat-agent-service';
import { Emitter, ILogger, generateUuid } from '@theia/core';
Expand Down Expand Up @@ -105,7 +105,7 @@ export interface ChatService {
}

interface ChatSessionInternal extends ChatSession {
model: ChatModelImpl;
model: MutableChatModel;
}

@injectable()
Expand Down Expand Up @@ -142,7 +142,7 @@ export class ChatServiceImpl implements ChatService {
}

createSession(location = ChatAgentLocation.Panel, options?: SessionOptions): ChatSession {
const model = new ChatModelImpl(location);
const model = new MutableChatModel(location);
const session: ChatSessionInternal = {
id: model.id,
model,
Expand Down Expand Up @@ -184,7 +184,7 @@ export class ChatServiceImpl implements ChatService {
if (agent === undefined) {
const error = 'No ChatAgents available to handle request!';
this.logger.error(error);
const chatResponseModel = new ErrorChatResponseModelImpl(generateUuid(), new Error(error));
const chatResponseModel = new ErrorChatResponseModel(generateUuid(), new Error(error));
return {
requestCompleted: Promise.reject(error),
responseCreated: Promise.reject(error),
Expand Down
10 changes: 5 additions & 5 deletions packages/ai-chat/src/common/chat-tool-request-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import { ToolRequest } from '@theia/ai-core';
import { injectable } from '@theia/core/shared/inversify';
import { ChatRequestModelImpl } from './chat-model';
import { MutableChatRequestModel } from './chat-model';

export interface ChatToolRequest extends ToolRequest {
handler: (
arg_string: string,
context: ChatRequestModelImpl,
context: MutableChatRequestModel,
) => Promise<unknown>;
}

Expand All @@ -34,22 +34,22 @@ export interface ChatToolRequest extends ToolRequest {
@injectable()
export class ChatToolRequestService {

getChatToolRequests(request: ChatRequestModelImpl): ChatToolRequest[] {
getChatToolRequests(request: MutableChatRequestModel): ChatToolRequest[] {
const toolRequests = request.message.toolRequests.size > 0 ? [...request.message.toolRequests.values()] : undefined;
if (!toolRequests) {
return [];
}
return this.toChatToolRequests(toolRequests, request);
}

toChatToolRequests(toolRequests: ToolRequest[] | undefined, request: ChatRequestModelImpl): ChatToolRequest[] {
toChatToolRequests(toolRequests: ToolRequest[] | undefined, request: MutableChatRequestModel): ChatToolRequest[] {
if (!toolRequests) {
return [];
}
return toolRequests.map(toolRequest => this.toChatToolRequest(toolRequest, request));
}

protected toChatToolRequest(toolRequest: ToolRequest, request: ChatRequestModelImpl): ChatToolRequest {
protected toChatToolRequest(toolRequest: ToolRequest, request: MutableChatRequestModel): ChatToolRequest {
return {
...toolRequest,
handler: async (arg_string: string) => toolRequest.handler(arg_string, request)
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-chat/src/common/parse-contents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// *****************************************************************************

import { expect } from 'chai';
import { ChatRequestModelImpl, ChatResponseContent, CodeChatResponseContentImpl, MarkdownChatResponseContentImpl } from './chat-model';
import { MutableChatRequestModel, ChatResponseContent, CodeChatResponseContentImpl, MarkdownChatResponseContentImpl } from './chat-model';
import { parseContents } from './parse-contents';
import { CodeContentMatcher, ResponseContentMatcher } from './response-content-matcher';

Expand All @@ -33,7 +33,7 @@ export const CommandContentMatcher: ResponseContentMatcher = {
}
};

const fakeRequest = {} as ChatRequestModelImpl;
const fakeRequest = {} as MutableChatRequestModel;

describe('parseContents', () => {
it('should parse code content', () => {
Expand Down
Loading
Loading