Skip to content

Commit

Permalink
incorporate review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eneufeld committed Oct 18, 2024
1 parent 9a6fffa commit 9692ac3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
10 changes: 7 additions & 3 deletions packages/ai-chat/src/browser/ai-chat-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { UniversalChatAgent } from '../common/universal-chat-agent';
import { aiChatPreferences } from './ai-chat-preferences';
import { AICustomAgentsFrontendApplicationContribution } from './custom-agent-frontend-application-contribution';
import { FrontendChatServiceImpl } from './frontend-chat-service';
import { FactoryOfCustomAgents } from './custom-agent-factory';
import { CustomAgentFactory } from './custom-agent-factory';

export default new ContainerModule(bind => {
bindContributionProvider(bind, Agent);
Expand Down Expand Up @@ -74,13 +74,17 @@ export default new ContainerModule(bind => {
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) => {
bind(CustomAgentFactory).toFactory<CustomChatAgent, [string, string, string, string, string]>(
ctx => (id: string, name: string, description: string, prompt: string, defaultLLM: string) => {
const agent = ctx.container.get<CustomChatAgent>(CustomChatAgent);
agent.id = id;
agent.name = name;
agent.description = description;
agent.prompt = prompt;
agent.languageModelRequirements = [{
purpose: 'chat',
identifier: defaultLLM,
}];
ctx.container.get<ChatAgentService>(ChatAgentService).registerChatAgent(agent);
ctx.container.get<AgentService>(AgentService).registerAgent(agent);
return agent;
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-chat/src/browser/custom-agent-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

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

export const FactoryOfCustomAgents = Symbol('FactoryOfCustomAgents');
export type FactoryOfCustomAgents = (id: string, name: string, description: string, prompt: string) => CustomChatAgent;
export const CustomAgentFactory = Symbol('CustomAgentFactory');
export type CustomAgentFactory = (id: string, name: string, description: string, prompt: string, defaultLLM: string) => CustomChatAgent;
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { AgentService, CustomAgentDescription, PromptCustomizationService } from
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';
import { CustomAgentFactory } from './custom-agent-factory';

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

@inject(PromptCustomizationService)
protected readonly customizationService: PromptCustomizationService;
Expand All @@ -38,7 +38,7 @@ export class AICustomAgentsFrontendApplicationContribution implements FrontendAp
onStart(): void {
this.customizationService?.getCustomAgents().then(customAgents => {
customAgents.forEach(agent => {
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt);
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt, agent.defaultLLM);
this.knownCustomAgents.set(agent.id, agent);
});
}).catch(e => {
Expand All @@ -59,7 +59,7 @@ export class AICustomAgentsFrontendApplicationContribution implements FrontendAp
});
customAgentsToAdd
.forEach(agent => {
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt);
this.customAgentFactory(agent.id, agent.name, agent.description, agent.prompt, agent.defaultLLM);
this.knownCustomAgents.set(agent.id, agent);
});
}).catch(e => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ 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';
import { load, dump } from 'js-yaml';

const templateEntry = `-id: my_agent
name: My Agent
description: This is an example agent. Please adapt the properties to fit your needs.
prompt: You are an example agent. Be nice and helpful to the user.`;
const templateEntry = {
id: 'my_agent',
name: 'My Agent',
description: 'This is an example agent. Please adapt the properties to fit your needs.',
prompt: 'You are an example agent. Be nice and helpful to the user.',
defaultLLM: 'openai/gpt-4o'
};

@injectable()
export class FrontendPromptCustomizationServiceImpl implements PromptCustomizationService {
Expand Down Expand Up @@ -115,6 +118,7 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati

}));

this.onDidChangeCustomAgentsEmitter.fire();
const stat = await this.fileService.resolve(templateURI);
if (stat.children === undefined) {
return;
Expand Down Expand Up @@ -228,8 +232,12 @@ export class FrontendPromptCustomizationServiceImpl implements PromptCustomizati

async openCustomAgentYaml(): Promise<void> {
const customAgentYamlUri = (await this.getTemplatesDirectoryURI()).resolve('customAgents.yml');
const content = dump([templateEntry]);
if (! await this.fileService.exists(customAgentYamlUri)) {
await this.fileService.createFile(customAgentYamlUri, BinaryBuffer.fromString(templateEntry));
await this.fileService.createFile(customAgentYamlUri, BinaryBuffer.fromString(content));
} else {
const fileContent = (await this.fileService.readFile(customAgentYamlUri)).value;
await this.fileService.writeFile(customAgentYamlUri, BinaryBuffer.concat([fileContent, BinaryBuffer.fromString(content)]));
}
const openHandler = await this.openerService.getOpener(customAgentYamlUri);
openHandler.open(customAgentYamlUri);
Expand Down
7 changes: 5 additions & 2 deletions packages/ai-core/src/common/prompt-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface CustomAgentDescription {
name: string;
description: string;
prompt: string;
defaultLLM: string;
}
export namespace CustomAgentDescription {
export function is(entry: unknown): entry is CustomAgentDescription {
Expand All @@ -83,10 +84,12 @@ export namespace CustomAgentDescription {
&& 'name' in entry && typeof entry.name === 'string'
&& 'description' in entry && typeof entry.description === 'string'
&& 'prompt' in entry
&& typeof entry.prompt === 'string';
&& typeof entry.prompt === 'string'
&& 'defaultLLM' in entry
&& typeof entry.defaultLLM === 'string';
}
export function equals(a: CustomAgentDescription, b: CustomAgentDescription): boolean {
return a.id === b.id && a.name === b.name && a.description === b.description && a.prompt === b.prompt;
return a.id === b.id && a.name === b.name && a.description === b.description && a.prompt === b.prompt && a.defaultLLM === b.defaultLLM;
}
}

Expand Down

0 comments on commit 9692ac3

Please sign in to comment.