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 @@
# @data/llmPlugins
> Project template for react component with storybook.
## 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,12 @@
{
"operationSettings": [
{
"operationName": "test:cov",
"outputFolderNames": ["coverage"]
},
{
"operationName": "ts-check",
"outputFolderNames": ["./dist"]
}
]
}

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,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 { RecallSlices } from './src';

View File

@@ -0,0 +1,46 @@
{
"name": "@coze-data/llmPlugins",
"version": "0.0.1",
"description": "llm output plugins of dataengine team",
"license": "Apache-2.0",
"author": "gaoshuda@bytedance.com",
"maintainers": [],
"main": "src/index.tsx",
"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/i18n": "workspace:*",
"@coze-arch/report-events": "workspace:*",
"@coze-data/reporter": "workspace:*",
"classnames": "^2.3.2"
},
"devDependencies": {
"@coze-arch/bot-semi": "workspace:*",
"@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/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,5 @@
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M6.49289 5.50001L5.42716 4.43428C5.23292 4.24003 5.23604 3.92454 5.43029 3.73029C5.62453 3.53605 5.94002 3.53292 6.13427 3.72717L8.23744 5.83034C8.33507 5.92797 8.33507 6.08626 8.23744 6.18389L6.14377 8.27756C5.94986 8.47147 5.63229 8.46975 5.43838 8.27584C5.24447 8.08193 5.24275 7.76436 5.43666 7.57046L6.50711 6.50001H1.5C1.22386 6.50001 1 6.27615 1 6.00001C1 5.72387 1.22386 5.50001 1.5 5.50001H6.49289ZM2 2V4.5H1C1 4.1411 1 2.9017 1 1.99943C1 1.44714 1.44772 1 2 1L10 1C10.5523 1 11 1.44772 11 2V10C11 10.5523 10.5523 11 10 11H2C1.44772 11 1 10.5523 1 10V7.5H2V10H10V2H2Z"
fill="#4D53E8" />
</svg>

After

Width:  |  Height:  |  Size: 718 B

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 { RecallSlices, LLMOutput } from './plugins';

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 { RecallSlices, LLMOutput } from './recall-slices';

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.
*/
export const filterUnnecessaryContentFromSlice = (slice: string): string => {
let res = slice;
// 过滤img 标签
res = res.replaceAll(/<(\n)*img((?!(<(\n)*img))(.|\n))*>/g, '');
return res;
};

View File

@@ -0,0 +1,71 @@
.recall-slices {
width: 100%;
:global {
.semi-card-body {
padding: 10px;
}
}
}
.recall-slice {
margin-bottom: 8px;
&-title {
margin-bottom: 7px;
font-size: 12px;
font-weight: 500;
line-height: 16px;
color: #1D1C23CC;
&-icon {
cursor: pointer;
position: relative;
top: 2px;
display: inline-flex;
align-items: center;
margin-left: 5px;
}
}
&-content {
&-collapsed {
overflow: hidden;
display: -webkit-box;
text-overflow: ellipsis;
-webkit-box-orient: vertical;
-webkit-line-clamp: 10;
}
&-action {
cursor: pointer;
color: #4D53E8;
}
}
&-tag {
margin-bottom: 7px;
font-weight: bold;
color: #1D1C2399;
}
&-tags {
display: flex;
gap: 6px;
justify-content: flex-start;
margin-bottom: 10px;
:global {
.semi-tag-content {
max-width: 120px;
}
}
}
}

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 React, { useEffect, useMemo, useRef, useState } from 'react';
import { DataNamespace, dataReporter } from '@coze-data/reporter';
import { REPORT_EVENTS } from '@coze-arch/report-events';
import { I18n } from '@coze-arch/i18n';
import { Card, Tag, Tooltip } from '@coze-arch/bot-semi';
import { DocumentSource, FormatType } from '@coze-arch/bot-api/knowledge';
import { ReactComponent as LinkToKnowledgeIcon } from '../../assets/link-to-knowledge.svg';
import { filterUnnecessaryContentFromSlice } from './helpers/filter-unnecessary-content-from-slice';
import styles from './index.module.less';
export interface LLMOutput {
meta: {
dataset: {
id: number;
name: string;
};
document: {
id: number;
source_type: number;
format_type: number;
name: string;
};
link: {
title: string;
url: string;
};
};
score: number;
slice: string;
}
const getSourceTypeDescription = (sourceType: number): string | undefined =>
({
[DocumentSource.Custom]: I18n.t('chat-area-knowledge-custom-data-source'),
[DocumentSource.Document]: I18n.t('chat-area-knowledge-local-data-source'),
[DocumentSource.FeishuWeb]: I18n.t(
'chat-area-knowledge-feishu-data-source',
),
[DocumentSource.Web]: I18n.t('chat-area-knowledge-online-data-source'),
[DocumentSource.FrontCrawl]: I18n.t(
'chat-area-knowledge-crawl-data-source',
),
[DocumentSource.GoogleDrive]: I18n.t(
'chat-area-knowledge-google-data-source',
),
[DocumentSource.Notion]: I18n.t('chat-area-knowledge-notion-data-source'),
[DocumentSource.LarkWeb]: I18n.t('Lark_00002'),
})[sourceType];
const getFormatTypeDescription = (formatType: number): string | undefined =>
({
[FormatType.Table]: I18n.t('knowledge-dataset-type-table'),
[FormatType.Text]: I18n.t('knowledge-dataset-type-text'),
[FormatType.Image]: I18n.t('knowledge_photo_025'),
})[formatType];
function RecallSlice(props: { llmOutput: LLMOutput; index: number }) {
const { llmOutput, index } = props;
const { meta, slice, score } = llmOutput;
const [isOpen, setIsOpen] = useState(false);
const [needCollapse, setNeedCollapse] = useState(false);
const sliceContentRef = useRef<HTMLDivElement>(null);
useEffect(() => {
setNeedCollapse(
(sliceContentRef.current?.scrollHeight ?? 0) >
(sliceContentRef.current?.clientHeight ?? 0),
);
}, []);
const formatTypeDesc = useMemo(
() =>
meta.document.format_type
? getFormatTypeDescription(meta.document.format_type)
: null,
[meta.document],
);
const sourceTypeDesc = useMemo(
() =>
meta.document.source_type
? getSourceTypeDescription(meta.document.source_type)
: null,
[meta.document],
);
// 后面改成 staring
const sliceTag = `Recall slice ${index + 1}`;
const filteredSlice = filterUnnecessaryContentFromSlice(slice);
return (
<div className={styles['recall-slice']}>
<Card>
<Tag className={styles['recall-slice-tag']}>{sliceTag}</Tag>
<div className={styles['recall-slice-title']}>
{meta.document?.name ?? ''}
<div
onClick={() => {
const { href, origin } = window.location;
const hrefSlices = href.split('/');
const spaceIndex = hrefSlices.indexOf('space');
if (spaceIndex === -1 || spaceIndex === hrefSlices.length - 1) {
dataReporter.errorEvent(DataNamespace.KNOWLEDGE, {
eventName: REPORT_EVENTS.KnowledgeSourceGetURL,
error: new Error(),
});
}
const spaceId = hrefSlices[spaceIndex + 1];
if (!/^[1-9][0-9]+$/.test(spaceId)) {
dataReporter.errorEvent(DataNamespace.KNOWLEDGE, {
eventName: REPORT_EVENTS.KnowledgeSourceGetURL,
error: new Error(),
});
}
window.open(
`${origin}/space/${spaceId}/knowledge/${meta.dataset.id}?first_auto_open_edit_document_id=${meta.document.id}`,
'_blank',
);
}}
className={styles['recall-slice-title-icon']}
>
<LinkToKnowledgeIcon />
</div>
</div>
<div className={styles['recall-slice-tags']}>
{meta.dataset.name ? (
<Tooltip content={meta.dataset.name}>
<Tag>{meta.dataset.name}</Tag>
</Tooltip>
) : null}
{formatTypeDesc ? (
<Tooltip content={formatTypeDesc}>
<Tag>{formatTypeDesc} </Tag>
</Tooltip>
) : null}
{sourceTypeDesc ? (
<Tooltip content={sourceTypeDesc}>
<Tag>{sourceTypeDesc} </Tag>
</Tooltip>
) : null}
{score > 0 && <Tag>{`Score: ${score.toFixed(2)}`}</Tag>}
</div>
<div className={styles['recall-slice-content']}>
<div
ref={sliceContentRef}
className={
isOpen
? styles['recall-slice-content-open']
: styles['recall-slice-content-collapsed']
}
>
{filteredSlice}
</div>
{needCollapse ? (
isOpen ? (
<div
className={styles['recall-slice-content-action']}
onClick={() => setIsOpen(false)}
>
{I18n.t('collapse-chat-knowledge-source-header')}
</div>
) : (
<div
className={styles['recall-slice-content-action']}
onClick={() => setIsOpen(true)}
>
{I18n.t('view-all-chat-knowledge-source-header')}
</div>
)
) : null}
</div>
</Card>
</div>
);
}
export function RecallSlices(props: { llmOutputs: Array<LLMOutput> }) {
return (
<div className={styles['recall-slices']}>
{props.llmOutputs.map((llmOutput, index) => (
<RecallSlice llmOutput={llmOutput} index={index} />
))}
</div>
);
}

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,37 @@
/*
* 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 { DemoRecallSlices } from './demo';
export default {
title: 'Example/Demo',
component: DemoRecallSlices,
parameters: {
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
layout: 'centered',
},
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
tags: ['autodocs'],
// More on argTypes: https://storybook.js.org/docs/api/argtypes
argTypes: {},
};
// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Base = {
args: {
name: 'tecvan',
},
};

View File

@@ -0,0 +1,76 @@
/*
* 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.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import { RecallSlices } from '../src';
export function DemoRecallSlices() {
const mockLLMOutputs = [
{
meta: {
dataset: {
id: 111,
type: '文档',
name: '笔记软件评测',
},
document: {
id: 222,
source: '本地',
name: 'Flomo',
},
link: {
title:
'[Research for Operations] KK Search Comprehensive User Mental Research Report',
// cp-disable-next-line
url: 'https://flomoapp.com/',
uniq_key: '文档_笔记软件评测_本地_Flomo',
},
title:
'[Research for Operations] KK Search Comprehensive User Mental Research Report',
score: 0.7,
},
content:
'Flomo特色是「便捷记录闪念笔记」可以通过微信服务号、iOS快捷指令和API输入笔记用于捕捉一闪即逝的想法。还可以不断地在阅读、消费信息的过程中记录这是一个解释的过程.',
},
{
meta: {
dataset: {
id: 111,
type: '文档',
name: '笔记软件评测',
},
document: {
id: 222,
source: '本地',
name: 'Flomo',
},
link: {
title:
'[Research for Operations] KK Search Comprehensive User Mental Research Report',
// cp-disable-next-line
url: 'https://flomoapp.com/',
uniq_key: '文档_笔记软件评测_本地_Flomo',
},
title:
'[Research for Operations] KK Search Comprehensive User Mental Research Report',
score: 0.7,
},
content:
'Flomo特色是「便捷记录闪念笔记」可以通过微信服务号、iOS快捷指令和API输入笔记用于捕捉一闪即逝的想法。还可以不断地在阅读、消费信息的过程中记录这是一个解释的过程也是一个向大脑“写入”的过程,特色是「便捷记录闪念笔记」可以通过微信服务号、iOS快捷指令和API输入笔记用于捕捉一闪即逝的想法。还可以不断地在阅读、消费信息的过程中记录这是一个解释的过程也是一个向大脑“写入”的过程,特色是「便捷记录闪念笔记」可以通过微信服务号、iOS快捷指令和API输入笔记用于捕捉一闪即逝的想法。还可以不断地在阅读、消费信息的过程中记录这是一个解释的过程也是一个向大脑“写入”的过程.',
},
];
return <RecallSlices llmOutputs={mockLLMOutputs as unknown as any} />;
}

View File

@@ -0,0 +1,34 @@
import { Meta } from "@storybook/blocks";
<Meta title="Hello world" />
<div className="sb-container">
<div className='sb-section-title'>
# Hello world
Hello world
</div>
</div>
<style>
{`
.sb-container {
margin-bottom: 48px;
}
.sb-section {
width: 100%;
display: flex;
flex-direction: row;
gap: 20px;
}
img {
object-fit: cover;
}
.sb-section-title {
margin-bottom: 32px;
}
`}
</style>

View File

@@ -0,0 +1,43 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"types": [],
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
},
"include": ["src"],
"references": [
{
"path": "../../../arch/bot-api/tsconfig.build.json"
},
{
"path": "../../../arch/bot-typings/tsconfig.build.json"
},
{
"path": "../../../arch/i18n/tsconfig.build.json"
},
{
"path": "../../../arch/report-events/tsconfig.build.json"
},
{
"path": "../../common/reporter/tsconfig.build.json"
},
{
"path": "../../../components/bot-semi/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,21 @@
{
"extends": "@coze-arch/ts-config/tsconfig.web.json",
"$schema": "https://json.schemastore.org/tsconfig",
"include": [
"index.ts",
"__tests__",
"stories",
"vitest.config.ts",
"tailwind.config.ts"
],
"exclude": ["./dist"],
"references": [
{
"path": "./tsconfig.build.json"
}
],
"compilerOptions": {
"rootDir": "./",
"outDir": "./dist"
}
}

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',
});