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,31 @@
import { mergeConfig } from 'vite';
import svgr from 'vite-plugin-svgr';
/** @type { import('@storybook/react-vite').StorybookConfig } */
const config = {
stories: ['../stories/**/*.mdx', '../stories/**/*.stories.tsx'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-onboarding',
'@storybook/addon-interactions',
],
framework: {
name: '@storybook/react-vite',
options: {},
},
docs: {
autodocs: 'tag',
},
viteFinal: config =>
mergeConfig(config, {
plugins: [
svgr({
svgrOptions: {
native: false,
},
}),
],
}),
};
export default config;

View File

@@ -0,0 +1,14 @@
/** @type { import('@storybook/react').Preview } */
const preview = {
parameters: {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};
export default preview;

View File

@@ -0,0 +1,5 @@
const { defineConfig } = require('@coze-arch/stylelint-config');
module.exports = defineConfig({
extends: [],
});

View File

@@ -0,0 +1,16 @@
# @coze-agent-ide/bot-input-length-limit
限制 bot 描述、bot 开场白等输入内容长度的逻辑
## Features
- [x] eslint & ts
- [x] esm bundle
- [x] umd bundle
- [x] storybook
## Commands
- init: `rush update`
- dev: `npm run dev`
- build: `npm run build`

View File

@@ -0,0 +1,208 @@
/*
* 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 { describe, it, expect, vi, beforeEach } from 'vitest';
import {
type BotInputLengthConfig,
type WorkInfoOnboardingContent,
} from '../src/services/type';
import { BotInputLengthService, botInputLengthService } from '../src/services';
// 模拟 SuggestedQuestionsShowMode 枚举
enum SuggestedQuestionsShowMode {
Random = 0,
All = 1,
}
// 模拟配置
const mockConfig: BotInputLengthConfig = {
botName: 10,
botDescription: 100,
onboarding: 50,
onboardingSuggestion: 20,
suggestionPrompt: 200,
projectName: 10,
projectDescription: 100,
};
// 模拟获取配置的函数
const mockGetConfig = vi.fn().mockReturnValue(mockConfig);
describe('BotInputLengthService', () => {
let service: BotInputLengthService;
beforeEach(() => {
// 重置模拟
vi.clearAllMocks();
// 创建服务实例
service = new BotInputLengthService(mockGetConfig);
});
describe('getInputLengthLimit', () => {
it('应该返回指定字段的长度限制', () => {
expect(service.getInputLengthLimit('botName')).toBe(10);
expect(service.getInputLengthLimit('botDescription')).toBe(100);
expect(service.getInputLengthLimit('onboarding')).toBe(50);
expect(service.getInputLengthLimit('onboardingSuggestion')).toBe(20);
expect(service.getInputLengthLimit('suggestionPrompt')).toBe(200);
expect(service.getInputLengthLimit('projectName')).toBe(10);
expect(service.getInputLengthLimit('projectDescription')).toBe(100);
// 验证配置获取函数被调用
expect(mockGetConfig).toHaveBeenCalledTimes(7);
});
});
describe('getValueLength', () => {
it('应该返回字符串的字形簇数量', () => {
// 普通字符串
expect(service.getValueLength('hello')).toBe(5);
// 包含表情符号的字符串(表情符号算作一个字形簇)
expect(service.getValueLength('hi😊')).toBe(3);
// 包含组合字符的字符串
expect(service.getValueLength('café')).toBe(4);
// 空字符串
expect(service.getValueLength('')).toBe(0);
// undefined
expect(service.getValueLength(undefined)).toBe(0);
});
});
describe('sliceStringByMaxLength', () => {
it('应该根据字段限制截取字符串', () => {
// 字符串长度小于限制
expect(
service.sliceStringByMaxLength({ value: 'hello', field: 'botName' }),
).toBe('hello');
// 字符串长度等于限制
expect(
service.sliceStringByMaxLength({
value: '1234567890',
field: 'botName',
}),
).toBe('1234567890');
// 字符串长度大于限制
expect(
service.sliceStringByMaxLength({
value: '12345678901234567890',
field: 'botName',
}),
).toBe('1234567890');
// 包含表情符号的字符串
expect(
service.sliceStringByMaxLength({
value: 'hello😊world',
field: 'botName',
}),
).toBe('hello😊worl');
// 验证配置获取函数被调用
expect(mockGetConfig).toHaveBeenCalledTimes(4);
});
});
describe('sliceWorkInfoOnboardingByMaxLength', () => {
it('应该截取工作信息的开场白和建议问题', () => {
const workInfo: WorkInfoOnboardingContent = {
prologue:
'This is a very long prologue that exceeds the limit of 50 characters and should be truncated',
suggested_questions: [
{
id: '1',
content:
'This is a very long suggested question that exceeds the limit',
highlight: true,
},
{ id: '2', content: 'Short question' },
{
id: '3',
content:
'Another very long suggested question that should be truncated',
highlight: false,
},
],
suggested_questions_show_mode: SuggestedQuestionsShowMode.All,
};
const result = service.sliceWorkInfoOnboardingByMaxLength(workInfo);
// 验证开场白被截取
expect(result.prologue).toBe(
'This is a very long prologue that exceeds the limi',
);
expect(result.prologue.length).toBeLessThanOrEqual(50);
// 验证建议问题被截取
expect(result.suggested_questions[0]?.content).toBe(
'This is a very long ',
);
expect(result.suggested_questions[0]?.content.length).toBeLessThanOrEqual(
20,
);
expect(result.suggested_questions[0]?.id).toBe('1');
expect(result.suggested_questions[0]?.highlight).toBe(true);
expect(result.suggested_questions[1]?.content).toBe('Short question');
expect(result.suggested_questions[1]?.id).toBe('2');
expect(result.suggested_questions[2]?.content).toBe(
'Another very long su',
);
expect(result.suggested_questions[2]?.content.length).toBeLessThanOrEqual(
20,
);
expect(result.suggested_questions[2]?.id).toBe('3');
expect(result.suggested_questions[2]?.highlight).toBe(false);
// 验证显示模式保持不变
expect(result.suggested_questions_show_mode).toBe(
SuggestedQuestionsShowMode.All,
);
});
it('应该处理空的工作信息', () => {
const workInfo: WorkInfoOnboardingContent = {
prologue: '',
suggested_questions: [],
suggested_questions_show_mode: SuggestedQuestionsShowMode.Random,
};
const result = service.sliceWorkInfoOnboardingByMaxLength(workInfo);
expect(result.prologue).toBe('');
expect(result.suggested_questions).toEqual([]);
expect(result.suggested_questions_show_mode).toBe(
SuggestedQuestionsShowMode.Random,
);
});
});
});
// 测试导出的单例
describe('botInputLengthService', () => {
it('应该导出一个 BotInputLengthService 的实例', () => {
// 验证导出的单例是 BotInputLengthService 的实例
expect(botInputLengthService).toBeInstanceOf(BotInputLengthService);
});
});

View File

@@ -0,0 +1,12 @@
{
"operationSettings": [
{
"operationName": "test:cov",
"outputFolderNames": ["coverage"]
},
{
"operationName": "ts-check",
"outputFolderNames": ["./dist"]
}
]
}

View File

@@ -0,0 +1,6 @@
{
"codecov": {
"coverage": 0,
"incrementCoverage": 0
}
}

View File

@@ -0,0 +1,7 @@
const { defineConfig } = require('@coze-arch/eslint-config');
module.exports = defineConfig({
packageRoot: __dirname,
preset: 'web',
rules: {},
});

View File

@@ -0,0 +1,46 @@
{
"name": "@coze-agent-ide/bot-input-length-limit",
"version": "0.0.1",
"description": "bot input length limit",
"license": "Apache-2.0",
"author": "gaoyuanhan.duty@bytedance.com",
"maintainers": [],
"main": "src/index.ts",
"scripts": {
"build": "exit 0",
"lint": "eslint ./ --cache",
"test": "vitest --run --passWithNoTests",
"test:cov": "npm run test -- --coverage"
},
"dependencies": {
"@coze-arch/bot-api": "workspace:*",
"@coze-arch/bot-flags": "workspace:*",
"classnames": "^2.3.2",
"grapheme-splitter": "~1.0.4",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@coze-arch/bot-typings": "workspace:*",
"@coze-arch/eslint-config": "workspace:*",
"@coze-arch/stylelint-config": "workspace:*",
"@coze-arch/ts-config": "workspace:*",
"@coze-arch/vitest-config": "workspace:*",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.1.2",
"@testing-library/react-hooks": "^8.0.1",
"@types/lodash-es": "^4.17.10",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.15",
"@vitest/coverage-v8": "~3.0.5",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"stylelint": "^15.11.0",
"vite-plugin-svgr": "~3.3.0",
"vitest": "~3.0.5"
},
"peerDependencies": {
"react": ">=18.2.0",
"react-dom": ">=18.2.0"
}
}

View File

@@ -0,0 +1,17 @@
/*
* 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 { botInputLengthService } from './services';

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 { type BotInputLengthConfig } from './type';
const CN_INPUT_LENGTH_CONFIG: BotInputLengthConfig = {
botName: 20,
botDescription: 500,
onboarding: 300,
onboardingSuggestion: 50,
suggestionPrompt: 5000,
projectName: 20,
projectDescription: 500,
};
const OVERSEA_INPUT_LENGTH_CONFIG: BotInputLengthConfig = {
botName: 40,
botDescription: 800,
onboarding: 800,
onboardingSuggestion: 90,
suggestionPrompt: 5000,
projectName: 40,
projectDescription: 800,
};
export const getBotInputLengthConfig = () =>
IS_OVERSEA ? OVERSEA_INPUT_LENGTH_CONFIG : CN_INPUT_LENGTH_CONFIG;

View File

@@ -0,0 +1,75 @@
/*
* 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 { cloneDeep } from 'lodash-es';
import GraphemeSplitter from 'grapheme-splitter';
import {
type BotInputLengthConfig,
type WorkInfoOnboardingContent,
} from './type';
import { getBotInputLengthConfig } from './constants';
export class BotInputLengthService {
graphemeSplitter: GraphemeSplitter;
constructor(private getInputLengthConfig: () => BotInputLengthConfig) {
this.graphemeSplitter = new GraphemeSplitter();
}
getInputLengthLimit: (field: keyof BotInputLengthConfig) => number = field =>
this.getInputLengthConfig()[field];
getValueLength: (value: string | undefined) => number = value => {
if (typeof value === 'undefined') {
return 0;
}
return this.graphemeSplitter.countGraphemes(value);
};
sliceStringByMaxLength: (param: {
value: string;
field: keyof BotInputLengthConfig;
}) => string = ({ value, field }) =>
this.graphemeSplitter
.splitGraphemes(value)
.slice(0, this.getInputLengthLimit(field))
.join('');
sliceWorkInfoOnboardingByMaxLength = (
param: WorkInfoOnboardingContent,
): WorkInfoOnboardingContent => {
const { prologue, suggested_questions, suggested_questions_show_mode } =
cloneDeep(param);
return {
prologue: this.sliceStringByMaxLength({
value: prologue,
field: 'onboarding',
}),
suggested_questions: suggested_questions.map(sug => ({
...sug,
content: this.sliceStringByMaxLength({
value: sug.content,
field: 'onboardingSuggestion',
}),
})),
suggested_questions_show_mode,
};
};
}
export const botInputLengthService = new BotInputLengthService(
getBotInputLengthConfig,
);

View File

@@ -0,0 +1,45 @@
/*
* 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 SuggestedQuestionsShowMode } from '@coze-arch/bot-api/playground_api';
export interface BotInputLengthConfig {
/** Agent 名称的长度 */
botName: number;
/** Agent 描述的长度 */
botDescription: number;
/** Agent 开场白的长度 */
onboarding: number;
/** Agent 单条开场白建议的长度 */
onboardingSuggestion: number;
/** 用户问题建议自定义 prompt 长度 */
suggestionPrompt: number;
/** Project 名称的长度 */
projectName: number;
/** Project 描述的长度 */
projectDescription: number;
}
export interface SuggestQuestionMessage {
id: string;
content: string;
highlight?: boolean;
}
export interface WorkInfoOnboardingContent {
prologue: string;
suggested_questions: SuggestQuestionMessage[];
suggested_questions_show_mode: SuggestedQuestionsShowMode;
}

View File

@@ -0,0 +1,17 @@
/*
* 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.
*/
/// <reference types='@coze-arch/bot-typings' />

View File

@@ -0,0 +1,40 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"compilerOptions": {
"types": [],
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"useUnknownInCatchVariables": true,
"strictPropertyInitialization": true,
"noUncheckedIndexedAccess": true,
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "./dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"references": [
{
"path": "../../arch/bot-api/tsconfig.build.json"
},
{
"path": "../../arch/bot-flags/tsconfig.build.json"
},
{
"path": "../../arch/bot-typings/tsconfig.build.json"
},
{
"path": "../../../config/eslint-config/tsconfig.build.json"
},
{
"path": "../../../config/stylelint-config/tsconfig.build.json"
},
{
"path": "../../../config/ts-config/tsconfig.build.json"
},
{
"path": "../../../config/vitest-config/tsconfig.build.json"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "./tsconfig.build.json"
},
{
"path": "./tsconfig.misc.json"
}
],
"exclude": ["**/*"]
}

View File

@@ -0,0 +1,22 @@
{
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["__tests__", "stories", "vitest.config.ts", "tailwind.config.ts"],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"rootDir": "./",
"outDir": "./dist",
"types": ["vitest/globals"],
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"useUnknownInCatchVariables": true,
"strictPropertyInitialization": true,
"noUncheckedIndexedAccess": true
}
}

View File

@@ -0,0 +1,22 @@
/*
* 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 { defineConfig } from '@coze-arch/vitest-config';
export default defineConfig({
dirname: __dirname,
preset: 'web',
});