Skip to content

Commit

Permalink
fix: manage AI bindings separately per connection (#14760)
Browse files Browse the repository at this point in the history
Moves all AI backend bindings into ConnectionContainerModules.
Therefore these are now scoped per connection (i.e. frontend) instead
of globally for all connections.

The global bindings lead to a lot of interference when multiple
connections were open. In fact AI streaming did only work for the
latest connection.
  • Loading branch information
sdirix authored Jan 29, 2025
1 parent 548f100 commit d011f3b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
8 changes: 7 additions & 1 deletion packages/ai-anthropic/src/node/anthropic-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import { ContainerModule } from '@theia/core/shared/inversify';
import { ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, AnthropicLanguageModelsManager } from '../common/anthropic-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { AnthropicLanguageModelsManagerImpl } from './anthropic-language-models-manager-impl';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const anthropicConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(AnthropicLanguageModelsManagerImpl).toSelf().inSingletonScope();
bind(AnthropicLanguageModelsManager).toService(AnthropicLanguageModelsManagerImpl);
bind(ConnectionHandler).toDynamicValue(ctx =>
new RpcConnectionHandler(ANTHROPIC_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(AnthropicLanguageModelsManager))
).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(anthropicConnectionModule);
});
10 changes: 9 additions & 1 deletion packages/ai-core/src/node/ai-core-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
RpcConnectionHandler,
bindContributionProvider,
} from '@theia/core';
import {
ConnectionContainerModule
} from '@theia/core/lib/node/messaging/connection-container-module';
import {
LanguageModelRegistry,
LanguageModelProvider,
Expand All @@ -37,7 +40,8 @@ import {
} from '../common';
import { BackendLanguageModelRegistry } from './backend-language-model-registry';

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const aiCoreConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bindContributionProvider(bind, LanguageModelProvider);
bind(BackendLanguageModelRegistry).toSelf().inSingletonScope();
bind(LanguageModelRegistry).toService(BackendLanguageModelRegistry);
Expand Down Expand Up @@ -81,3 +85,7 @@ export default new ContainerModule(bind => {
bind(PromptServiceImpl).toSelf().inSingletonScope();
bind(PromptService).toService(PromptServiceImpl);
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(aiCoreConnectionModule);
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import { ContainerModule } from '@theia/core/shared/inversify';
import { HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, HuggingFaceLanguageModelsManager } from '../common/huggingface-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';
import { HuggingFaceLanguageModelsManagerImpl } from './huggingface-language-models-manager-impl';

export const HuggingFaceModelFactory = Symbol('HuggingFaceModelFactory');

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const huggingfaceConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(HuggingFaceLanguageModelsManagerImpl).toSelf().inSingletonScope();
bind(HuggingFaceLanguageModelsManager).toService(HuggingFaceLanguageModelsManagerImpl);
bind(ConnectionHandler).toDynamicValue(ctx =>
new RpcConnectionHandler(HUGGINGFACE_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(HuggingFaceLanguageModelsManager))
).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(huggingfaceConnectionModule);
});
8 changes: 7 additions & 1 deletion packages/ai-llamafile/src/node/llamafile-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import { ContainerModule } from '@theia/core/shared/inversify';
import { LlamafileManagerImpl } from './llamafile-manager-impl';
import { LlamafileManager, LlamafileServerManagerClient, LlamafileManagerPath } from '../common/llamafile-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const llamafileConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(LlamafileManager).to(LlamafileManagerImpl).inSingletonScope();
bind(ConnectionHandler).toDynamicValue(ctx => new RpcConnectionHandler<LlamafileServerManagerClient>(
LlamafileManagerPath,
Expand All @@ -30,3 +32,7 @@ export default new ContainerModule(bind => {
}
)).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(llamafileConnectionModule);
});
8 changes: 7 additions & 1 deletion packages/ai-mcp/src/node/mcp-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import { ContainerModule } from '@theia/core/shared/inversify';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { MCPServerManagerImpl } from './mcp-server-manager-impl';
import { MCPServerManager, MCPServerManagerPath } from '../common/mcp-server-manager';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const mcpConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(MCPServerManager).to(MCPServerManagerImpl).inSingletonScope();
bind(ConnectionHandler).toDynamicValue(ctx => new RpcConnectionHandler(
MCPServerManagerPath,
Expand All @@ -29,3 +31,7 @@ export default new ContainerModule(bind => {
}
)).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(mcpConnectionModule);
});
8 changes: 7 additions & 1 deletion packages/ai-ollama/src/node/ollama-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ import { ContainerModule } from '@theia/core/shared/inversify';
import { OLLAMA_LANGUAGE_MODELS_MANAGER_PATH, OllamaLanguageModelsManager } from '../common/ollama-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { OllamaLanguageModelsManagerImpl } from './ollama-language-models-manager-impl';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';

export const OllamaModelFactory = Symbol('OllamaModelFactory');

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const ollamaConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(OllamaLanguageModelsManagerImpl).toSelf().inSingletonScope();
bind(OllamaLanguageModelsManager).toService(OllamaLanguageModelsManagerImpl);
bind(ConnectionHandler).toDynamicValue(ctx =>
new RpcConnectionHandler(OLLAMA_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(OllamaLanguageModelsManager))
).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(ollamaConnectionModule);
});
8 changes: 7 additions & 1 deletion packages/ai-openai/src/node/openai-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ import { ContainerModule } from '@theia/core/shared/inversify';
import { OPENAI_LANGUAGE_MODELS_MANAGER_PATH, OpenAiLanguageModelsManager } from '../common/openai-language-models-manager';
import { ConnectionHandler, RpcConnectionHandler } from '@theia/core';
import { OpenAiLanguageModelsManagerImpl } from './openai-language-models-manager-impl';
import { ConnectionContainerModule } from '@theia/core/lib/node/messaging/connection-container-module';

export const OpenAiModelFactory = Symbol('OpenAiModelFactory');

export default new ContainerModule(bind => {
// We use a connection module to handle AI services separately for each frontend.
const openAiConnectionModule = ConnectionContainerModule.create(({ bind, bindBackendService, bindFrontendService }) => {
bind(OpenAiLanguageModelsManagerImpl).toSelf().inSingletonScope();
bind(OpenAiLanguageModelsManager).toService(OpenAiLanguageModelsManagerImpl);
bind(ConnectionHandler).toDynamicValue(ctx =>
new RpcConnectionHandler(OPENAI_LANGUAGE_MODELS_MANAGER_PATH, () => ctx.container.get(OpenAiLanguageModelsManager))
).inSingletonScope();
});

export default new ContainerModule(bind => {
bind(ConnectionContainerModule).toConstantValue(openAiConnectionModule);
});

0 comments on commit d011f3b

Please sign in to comment.