feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { bindContributions, bindContributionProvider } from '@flowgram-adapter/common';
import { definePluginCreator, LifecycleContribution } from '../common';
import { PreferencesManager } from './preferences-manager';
import { PreferencesContribution } from './preferences-contribution';
import { PreferenceContribution } from './preference-contribution';
interface PreferencesPluginOptions {
defaultData?: any;
}
const createPreferencesPlugin = definePluginCreator<PreferencesPluginOptions>({
onBind({ bind }) {
bind(PreferencesManager).toSelf().inSingletonScope();
bindContributions(bind, PreferencesContribution, [LifecycleContribution]);
bindContributionProvider(bind, PreferenceContribution);
},
onInit(ctx, opts) {
ctx.container.get(PreferencesManager).init(opts?.defaultData);
},
});
export { createPreferencesPlugin, type PreferencesPluginOptions };

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {
PreferenceContribution,
type PreferenceSchema,
} from './preference-contribution';
export { PreferencesManager } from './preferences-manager';
export {
createPreferencesPlugin,
type PreferencesPluginOptions,
} from './create-preferences-plugin';

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { type SchemaDecoration } from '@flowgram-adapter/common';
interface PreferenceSchema {
properties: Record<string, SchemaDecoration>;
}
interface PreferenceContribution {
configuration: PreferenceSchema;
}
const PreferenceContribution = Symbol('PreferenceContribution');
export { PreferenceContribution, type PreferenceSchema };

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { inject, injectable, named } from 'inversify';
import { ContributionProvider } from '@flowgram-adapter/common';
import { type LifecycleContribution } from '../common';
import { PreferencesManager } from './preferences-manager';
import { PreferenceContribution } from './preference-contribution';
@injectable()
class PreferencesContribution implements LifecycleContribution {
@inject(ContributionProvider)
@named(PreferenceContribution)
protected readonly preferenceContributions: ContributionProvider<PreferenceContribution>;
@inject(PreferencesManager)
protected readonly preferencesManager: PreferencesManager;
onInit() {
this.preferenceContributions.getContributions().forEach(contrib => {
this.preferencesManager.setSchema(contrib.configuration);
});
}
}
export { PreferencesContribution };

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { injectable } from 'inversify';
import { isObject, type SchemaDecoration, Emitter } from '@flowgram-adapter/common';
import { type PreferenceSchema } from './preference-contribution';
@injectable()
class PreferencesManager {
private readonly preferences: Record<string, any> = {};
readonly schema: PreferenceSchema = {
properties: {},
};
private readonly preferencesChange = new Emitter<void>();
onDidPreferencesChange = this.preferencesChange.event;
public init(data: any) {
/**
* 从远程或者本地读取用户配置
*/
Object.assign(this.preferences, data);
this.preferencesChange.fire();
}
public setSchema(schema: PreferenceSchema) {
const { properties } = schema;
/** 这里先做简单校验,后面要做整个 validateSchema */
if (!properties || !isObject(properties)) {
return;
}
Object.entries<SchemaDecoration>(properties).forEach(([key, value]) => {
if (this.schema.properties[key]) {
// 重复定义的不覆盖,先报个警告
console.error(
'Preference name collision detected in the schema for property: ',
key,
);
return;
}
this.schema.properties[key] = value;
});
}
getPreferenceData(key: string) {
return this.preferences[key];
}
}
export { PreferencesManager };