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,18 @@
/*
* 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 { useQuestionFormStore } from './use-question-form-store';
export { useSendMessage } from './use-send-message';

View File

@@ -0,0 +1,31 @@
/*
* 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 { useContext } from 'react';
import {
questionFormContext,
type QuestionFormState,
type QuestionFormAction,
} from '../context';
export const useQuestionFormStore = <T>(
selector: (s: QuestionFormState & QuestionFormAction) => T,
) => {
const store = useContext(questionFormContext);
return store(selector);
};

View File

@@ -0,0 +1,64 @@
/*
* 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 { nanoid } from 'nanoid';
import { useMemoizedFn } from 'ahooks';
import { workflowApi } from '@coze-workflow/base/api';
import { type ReceivedMessage } from '../types';
import { useQuestionFormStore } from '../hooks/use-question-form-store';
import { MessageType, ContentType } from '../constants';
import { useTestRunService } from '../../../hooks';
export const useSendMessage = () => {
const { spaceId, workflowId, executeId, messages, eventId, waiting, patch } =
useQuestionFormStore(store => ({
spaceId: store.spaceId,
workflowId: store.workflowId,
executeId: store.executeId,
messages: store.messages,
eventId: store.eventId,
waiting: store.waiting,
patch: store.patch,
}));
const testRunService = useTestRunService();
const send = useMemoizedFn(async (text: string) => {
// 前端先填入回答
const temp: ReceivedMessage = {
content: text,
type: MessageType.Answer,
content_type: ContentType.Text,
id: nanoid(),
};
const next = messages.concat([temp]);
patch({ waiting: true, messages: next });
try {
await workflowApi.WorkFlowTestResume({
workflow_id: workflowId,
space_id: spaceId,
data: text,
event_id: eventId,
execute_id: executeId,
});
} finally {
testRunService.continueTestRun();
}
});
return { send, waiting };
};