Skip to content

Commit

Permalink
feat: improve logging & error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Mar 25, 2024
1 parent 174be82 commit 3a1892d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/backend/server/src/config/affine.self.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ if (env.R2_OBJECT_STORAGE_ACCOUNT_ID) {
}`;
}

AFFiNE.plugins.use('copilot', {
openai: {
apiKey: 'test',
},
});
AFFiNE.plugins.use('redis');
AFFiNE.plugins.use('payment', {
stripe: {
Expand Down
35 changes: 23 additions & 12 deletions packages/backend/server/src/plugins/copilot/provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert';

import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';

import { Config } from '../../fundamentals';
import {
Expand All @@ -27,7 +27,7 @@ interface CopilotProviderDefinition<C extends CopilotProviderConfig> {
// registered provider factory
const COPILOT_PROVIDER = new Map<
CopilotProviderType,
(config: Config) => CopilotProvider
(config: Config, logger: Logger) => CopilotProvider
>();

// map of capabilities to providers
Expand All @@ -42,26 +42,36 @@ const ASSERT_CONFIG = new Map<CopilotProviderType, (config: Config) => void>();
export function registerCopilotProvider<
C extends CopilotProviderConfig = CopilotProviderConfig,
>(provider: CopilotProviderDefinition<C>) {
const factory = (config: Config) => {
assert(config.plugins.copilot);
assert(config.plugins.copilot[provider.type]);
const type = provider.type;

const factory = (config: Config, logger: Logger) => {
const providerConfig = config.plugins.copilot?.[type];
if (!provider.assetsConfig(providerConfig as C)) {
throw new Error(
`Invalid configuration for copilot provider ${type}: ${providerConfig}`
);
}
const instance = new provider(providerConfig as C);
logger.log(
`Copilot provider ${type} registered, capabilities: ${provider.capabilities.join(', ')}`
);

return new provider(config.plugins.copilot[provider.type] as C);
return instance;
};
// register the provider
COPILOT_PROVIDER.set(provider.type, factory);
COPILOT_PROVIDER.set(type, factory);
// register the provider capabilities
for (const capability of provider.capabilities) {
const providers = PROVIDER_CAPABILITY_MAP.get(capability) || [];
if (!providers.includes(provider.type)) {
providers.push(provider.type);
if (!providers.includes(type)) {
providers.push(type);
}
PROVIDER_CAPABILITY_MAP.set(capability, providers);
}
// register the provider config assertion
ASSERT_CONFIG.set(provider.type, (config: Config) => {
ASSERT_CONFIG.set(type, (config: Config) => {
assert(config.plugins.copilot);
const providerConfig = config.plugins.copilot[provider.type];
const providerConfig = config.plugins.copilot[type];
if (!providerConfig) return false;
return provider.assetsConfig(providerConfig as C);
});
Expand All @@ -78,6 +88,7 @@ export function assertProvidersConfigs(config: Config) {

@Injectable()
export class CopilotProviderService {
private readonly logger = new Logger(CopilotProviderService.name);
constructor(private readonly config: Config) {}

private readonly cachedProviders = new Map<
Expand All @@ -93,7 +104,7 @@ export class CopilotProviderService {
throw new Error(`Unknown copilot provider type: ${provider}`);
}

return providerFactory(this.config);
return providerFactory(this.config, this.logger);
}

getProvider(provider: CopilotProviderType): CopilotProvider {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/server/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './copilot';
import './gcloud';
import './oauth';
import './payment';
Expand Down
1 change: 1 addition & 0 deletions packages/backend/server/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ enum ServerDeploymentType {
}

enum ServerFeature {
Copilot
OAuth
Payment
}
Expand Down

0 comments on commit 3a1892d

Please sign in to comment.