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

feat: add support for custom agents #14301

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 21 additions & 4 deletions packages/ai-chat/src/browser/ai-chat-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { Agent, AIVariableContribution } from '@theia/ai-core/lib/common';
import { Agent, AgentService, AIVariableContribution } from '@theia/ai-core/lib/common';
import { bindContributionProvider } from '@theia/core';
import { PreferenceContribution } from '@theia/core/lib/browser';
import { FrontendApplicationContribution, PreferenceContribution } from '@theia/core/lib/browser';
import { ContainerModule } from '@theia/core/shared/inversify';
import {
ChatAgent,
Expand All @@ -27,13 +27,16 @@ import {
ChatService,
DefaultChatAgentId
} from '../common';
import { ChatAgentsVariableContribution } from '../common/chat-agents-variable-contribution';
import { CommandChatAgent } from '../common/command-chat-agents';
import { CustomChatAgent } from '../common/custom-chat-agent';
import { OrchestratorChatAgent, OrchestratorChatAgentId } from '../common/orchestrator-chat-agent';
import { DefaultResponseContentFactory, DefaultResponseContentMatcherProvider, ResponseContentMatcherProvider } from '../common/response-content-matcher';
import { UniversalChatAgent } from '../common/universal-chat-agent';
import { aiChatPreferences } from './ai-chat-preferences';
import { ChatAgentsVariableContribution } from '../common/chat-agents-variable-contribution';
import { AICustomAgentsFrontendApplicationContribution } from './custom-agent-frontend-application-contribution';
import { FrontendChatServiceImpl } from './frontend-chat-service';
import { DefaultResponseContentMatcherProvider, DefaultResponseContentFactory, ResponseContentMatcherProvider } from '../common/response-content-matcher';
import { FactoryOfCustomAgents } from './custom-agent-factory';

export default new ContainerModule(bind => {
bindContributionProvider(bind, Agent);
Expand Down Expand Up @@ -69,4 +72,18 @@ export default new ContainerModule(bind => {
bind(ChatAgent).toService(CommandChatAgent);

bind(PreferenceContribution).toConstantValue({ schema: aiChatPreferences });

bind(CustomChatAgent).toSelf();
bind(FactoryOfCustomAgents).toFactory<CustomChatAgent, [string, string, string, string]>(
ctx => (id: string, name: string, description: string, prompt: string) => {
const agent = ctx.container.get<CustomChatAgent>(CustomChatAgent);
agent.id = id;
agent.name = name;
agent.description = description;
agent.prompt = prompt;
ctx.container.get<ChatAgentService>(ChatAgentService).registerChatAgent(agent);
ctx.container.get<AgentService>(AgentService).registerAgent(agent);
return agent;
});
bind(FrontendApplicationContribution).to(AICustomAgentsFrontendApplicationContribution).inSingletonScope();
});
20 changes: 20 additions & 0 deletions packages/ai-chat/src/browser/custom-agent-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { CustomChatAgent } from '../common';

export const FactoryOfCustomAgents = Symbol('FactoryOfCustomAgents');
export type FactoryOfCustomAgents = (id: string, name: string, description: string, prompt: string) => CustomChatAgent;
sdirix marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { AgentService, CustomAgentDescription, PromptCustomizationService } from '@theia/ai-core';
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
import { inject, injectable } from '@theia/core/shared/inversify';
import { ChatAgentService } from '../common';
import { FactoryOfCustomAgents } from './custom-agent-factory';

@injectable()
export class AICustomAgentsFrontendApplicationContribution implements FrontendApplicationContribution {
@inject(FactoryOfCustomAgents)
protected readonly customAgentFactory: FactoryOfCustomAgents;

@inject(PromptCustomizationService)
protected readonly customizationService: PromptCustomizationService;

@inject(AgentService)
private readonly agentService: AgentService;

@inject(ChatAgentService)
private readonly chatAgentService: ChatAgentService;

private knownCustomAgents: Map<string, CustomAgentDescription> = new Map();
onStart(): void {
this.customizationService?.getCustomAgents().then(customAgents => {
customAgents.forEach(agent => {
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt);
this.knownCustomAgents.set(agent.id, agent);
});
}).catch(e => {
console.error('Failed to load custom agents', e);
});
this.customizationService?.onDidChangeCustomAgents(() => {
this.customizationService?.getCustomAgents().then(customAgents => {
const customAgentsToAdd = customAgents.filter(agent =>
!this.knownCustomAgents.has(agent.id) || !CustomAgentDescription.equals(this.knownCustomAgents.get(agent.id)!, agent));
const customAgentIdsToRemove = [...this.knownCustomAgents.values()].filter(agent =>
!customAgents.find(a => CustomAgentDescription.equals(a, agent))).map(a => a.id);

// delete first so we don't have to deal with the case where we add and remove the same agentId
customAgentIdsToRemove.forEach(id => {
this.chatAgentService.unregisterChatAgent(id);
this.agentService.unregisterAgent(id);
this.knownCustomAgents.delete(id);
});
customAgentsToAdd
.forEach(agent => {
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt);
this.knownCustomAgents.set(agent.id, agent);
});
}).catch(e => {
console.error('Failed to load custom agents', e);
});
});
}

onStop(): void {
}
}
15 changes: 15 additions & 0 deletions packages/ai-chat/src/common/chat-agent-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export interface ChatAgentService {
* Returns all agents, including disabled ones.
*/
getAllAgents(): ChatAgent[];

/**
* Allows to register a chat agent programmatically.
* @param agent the agent to register
*/
registerChatAgent(agent: ChatAgent): void;

/**
* Allows to unregister a chat agent programmatically.
* @param agentId the agent id to unregister
*/
unregisterChatAgent(agentId: string): void;
}
@injectable()
export class ChatAgentServiceImpl implements ChatAgentService {
Expand All @@ -65,6 +77,9 @@ export class ChatAgentServiceImpl implements ChatAgentService {
registerChatAgent(agent: ChatAgent): void {
this._agents.push(agent);
}
unregisterChatAgent(agentId: string): void {
this._agents = this._agents.filter(a => a.id !== agentId);
}

getAgent(id: string): ChatAgent | undefined {
if (!this._agentIsEnabled(id)) {
Expand Down
44 changes: 44 additions & 0 deletions packages/ai-chat/src/common/custom-chat-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { AgentSpecificVariables, PromptTemplate } from '@theia/ai-core';
import { AbstractStreamParsingChatAgent, ChatAgent, SystemMessageDescription } from './chat-agents';
import { injectable } from '@theia/core/shared/inversify';

@injectable()
export class CustomChatAgent
extends AbstractStreamParsingChatAgent
implements ChatAgent {
name: string;
description: string;
readonly variables: string[] = [];
readonly functions: string[] = [];
readonly promptTemplates: PromptTemplate[] = [];
readonly agentSpecificVariables: AgentSpecificVariables[] = [];

constructor(
) {
super('CustomChatAgent', [{ purpose: 'chat' }], 'chat');
}
protected override async getSystemMessageDescription(): Promise<SystemMessageDescription | undefined> {
const resolvedPrompt = await this.promptService.getPrompt(`${this.name}_prompt`);
return resolvedPrompt ? SystemMessageDescription.fromResolvedPromptTemplate(resolvedPrompt) : undefined;
}

set prompt(prompt: string) {
this.promptTemplates.push({ id: `${this.name}_system`, template: prompt });
}
}
7 changes: 4 additions & 3 deletions packages/ai-chat/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
export * from './chat-agent-service';
export * from './chat-agents';
export * from './chat-agent-service';
export * from './chat-model';
export * from './parsed-chat-request';
export * from './chat-request-parser';
export * from './chat-service';
export * from './command-chat-agents';
export * from './universal-chat-agent';
export * from './custom-chat-agent';
export * from './parsed-chat-request';
export * from './orchestrator-chat-agent';
export * from './universal-chat-agent';
2 changes: 2 additions & 0 deletions packages/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"@theia/output": "1.54.0",
"@theia/variable-resolver": "1.54.0",
"@theia/workspace": "1.54.0",
"@types/js-yaml": "^4.0.9",
"js-yaml": "^4.1.0",
"minimatch": "^5.1.0",
"tslib": "^2.6.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class AIAgentConfigurationWidget extends ReactWidget {

this.aiSettingsService.onDidChange(() => this.update());
this.aiConfigurationSelectionService.onDidAgentChange(() => this.update());
this.agentService.onDidChangeAgents(() => this.update());
this.update();
}

Expand All @@ -100,6 +101,7 @@ export class AIAgentConfigurationWidget extends ReactWidget {
</li>
)}
</ul>
<button style={{ marginLeft: 0 }} className='theia-button main' onClick={() => this.addCustomAgent()}>Add Custom Agent</button>
</div>
<div className='configuration-agent-panel preferences-editor-widget'>
{this.renderAgentDetails()}
Expand Down Expand Up @@ -205,6 +207,10 @@ export class AIAgentConfigurationWidget extends ReactWidget {
this.aiConfigurationSelectionService.selectConfigurationTab(AIVariableConfigurationWidget.ID);
}

protected addCustomAgent(): void {
this.promptCustomizationService.openCustomAgentYaml();
}

protected setActiveAgent(agent: Agent): void {
this.aiConfigurationSelectionService.setActiveAgent(agent);
this.update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import { DisposableCollection, URI, Event, Emitter } from '@theia/core';
import { OpenerService } from '@theia/core/lib/browser';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { PromptCustomizationService, PromptTemplate } from '../common';
import { PromptCustomizationService, PromptTemplate, CustomAgentDescription } from '../common';
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { FileChangesEvent } from '@theia/filesystem/lib/common/files';
import { AICorePreferences, PREFERENCE_NAME_PROMPT_TEMPLATES } from './ai-core-preferences';
import { AgentService } from '../common/agent-service';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { load } from 'js-yaml';

@injectable()
export class FrontendPromptCustomizationServiceImpl implements PromptCustomizationService {
Expand Down Expand Up @@ -51,6 +52,9 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati
private readonly onDidChangePromptEmitter = new Emitter<string>();
readonly onDidChangePrompt: Event<string> = this.onDidChangePromptEmitter.event;

private readonly onDidChangeCustomAgentsEmitter = new Emitter<void>();
readonly onDidChangeCustomAgents = this.onDidChangeCustomAgentsEmitter.event;

@postConstruct()
protected init(): void {
this.preferences.onPreferenceChanged(event => {
Expand All @@ -72,6 +76,9 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati

this.toDispose.push(this.fileService.watch(templateURI, { recursive: true, excludes: [] }));
this.toDispose.push(this.fileService.onDidFilesChange(async (event: FileChangesEvent) => {
if (event.changes.some(change => change.resource.toString().endsWith('customAgents.yml'))) {
this.onDidChangeCustomAgentsEmitter.fire();
}
// check deleted templates
for (const deletedFile of event.getDeleted()) {
if (this.trackedTemplateURIs.has(deletedFile.resource.toString())) {
Expand Down Expand Up @@ -194,4 +201,32 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati
return undefined;
}

async getCustomAgents(): Promise<CustomAgentDescription[]> {
const customAgentYamlUri = (await this.getTemplatesDirectoryURI()).resolve('customAgents.yml');
const yamlExists = await this.fileService.exists(customAgentYamlUri);
if (!yamlExists) {
return [];
}
const filecontent = await this.fileService.read(customAgentYamlUri, { encoding: 'utf-8' });
try {
const doc = load(filecontent.value);
if (!Array.isArray(doc) || !doc.every(entry => CustomAgentDescription.is(entry))) {
console.debug('Invalid customAgents.yml file content');
return [];
}
return doc as CustomAgentDescription[];
} catch (e) {
console.debug(e.message, e);
return [];
}
}

async openCustomAgentYaml(): Promise<void> {
const customAgentYamlUri = (await this.getTemplatesDirectoryURI()).resolve('customAgents.yml');
if (! await this.fileService.exists(customAgentYamlUri)) {
await this.fileService.createFile(customAgentYamlUri);
}
const openHandler = await this.openerService.getOpener(customAgentYamlUri);
openHandler.open(customAgentYamlUri);
}
}
29 changes: 28 additions & 1 deletion packages/ai-core/src/common/agent-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { inject, injectable, named, optional, postConstruct } from '@theia/core/shared/inversify';
import { ContributionProvider } from '@theia/core';
import { ContributionProvider, Emitter, Event } from '@theia/core';
import { Agent } from './agent';
import { AISettingsService } from './settings-service';

Expand Down Expand Up @@ -48,6 +48,24 @@ export interface AgentService {
* @return true if the agent is enabled, false otherwise.
*/
isEnabled(agentId: string): boolean;

/**
* Allows to register an agent programmatically.
* @param agent the agent to register
*/
registerAgent(agent: Agent): void;

/**
* Allows to unregister an agent programmatically.
* @param agentId the agent id to unregister
*/
unregisterAgent(agentId: string): void;

/**
* Emitted when the list of agents changes.
* This can be used to update the UI when agents are added or removed.
*/
onDidChangeAgents: Event<void>;
}

@injectable()
Expand All @@ -63,6 +81,9 @@ export class AgentServiceImpl implements AgentService {

protected _agents: Agent[] = [];

private readonly onDidChangeAgentsEmitter = new Emitter<void>();
readonly onDidChangeAgents = this.onDidChangeAgentsEmitter.event;

@postConstruct()
protected init(): void {
this.aiSettingsService?.getSettings().then(settings => {
Expand All @@ -82,6 +103,12 @@ export class AgentServiceImpl implements AgentService {

registerAgent(agent: Agent): void {
this._agents.push(agent);
this.onDidChangeAgentsEmitter.fire();
}

unregisterAgent(agentId: string): void {
this._agents = this._agents.filter(a => a.id !== agentId);
this.onDidChangeAgentsEmitter.fire();
}

getAgents(): Agent[] {
Expand Down
Loading
Loading