feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -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';
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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 };
|
||||
};
|
||||
Reference in New Issue
Block a user