Skip to content

Commit cee986c

Browse files
newfish-cmykc121914yu
authored andcommitted
add v2 completions (#4364)
* add v2 completions * completion config * config version * fix * frontend * doc * fix
1 parent a99b9ee commit cee986c

File tree

9 files changed

+1194
-80
lines changed

9 files changed

+1194
-80
lines changed

docSite/content/zh-cn/docs/development/openapi/chat.md

Lines changed: 431 additions & 2 deletions
Large diffs are not rendered by default.

packages/global/core/workflow/runtime/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum SseResponseEventEnum {
55
answer = 'answer', // animation stream
66
fastAnswer = 'fastAnswer', // direct answer text, not animation
77
flowNodeStatus = 'flowNodeStatus', // update node status
8+
flowNodeResponse = 'flowNodeResponse', // node response
89

910
toolCall = 'toolCall', // tool start
1011
toolParams = 'toolParams', // tool params return

packages/global/core/workflow/runtime/type.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export type ChatDispatchProps = {
5959
isToolCall?: boolean;
6060
workflowStreamResponse?: WorkflowResponseType;
6161
workflowDispatchDeep?: number;
62+
version?: 'v1' | 'v2';
6263
};
6364

6465
export type ModuleDispatchProps<T> = ChatDispatchProps & {

packages/service/common/response/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,29 @@ export function responseWrite({
129129
res,
130130
write,
131131
event,
132-
data
132+
data,
133+
version = 'v1'
133134
}: {
134135
res?: NextApiResponse;
135136
write?: (text: string) => void;
136137
event?: string;
137138
data: string;
139+
version?: 'v1' | 'v2';
138140
}) {
139141
const Write = write || res?.write;
140142

141143
if (!Write) return;
142144

143-
event && Write(`event: ${event}\n`);
144-
Write(`data: ${data}\n\n`);
145+
if (version === 'v1') {
146+
event && Write(`event: ${event}\n`);
147+
Write(`data: ${data}\n\n`);
148+
} else {
149+
const dataJSON = {
150+
event,
151+
data
152+
};
153+
Write(`data: ${JSON.stringify(dataJSON)}\n\n`);
154+
}
145155
}
146156

147157
export const responseWriteNodeStatus = ({

packages/service/core/workflow/dispatch/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
130130
timezone,
131131
externalProvider,
132132
stream = false,
133+
version = 'v1',
133134
...props
134135
} = data;
135136

@@ -626,6 +627,20 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
626627
};
627628
})();
628629

630+
if (
631+
!props.isToolCall &&
632+
version === 'v2' &&
633+
!props.runningAppInfo.isChildApp &&
634+
formatResponseData
635+
) {
636+
props.workflowStreamResponse?.({
637+
event: SseResponseEventEnum.flowNodeResponse,
638+
data: {
639+
...formatResponseData
640+
}
641+
});
642+
}
643+
629644
// Add output default value
630645
node.outputs.forEach((item) => {
631646
if (!item.required) return;

packages/service/core/workflow/dispatch/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ export const getWorkflowResponseWrite = ({
2121
detail,
2222
streamResponse,
2323
id = getNanoid(24),
24-
showNodeStatus = true
24+
showNodeStatus = true,
25+
version = 'v1'
2526
}: {
2627
res?: NextApiResponse;
2728
detail: boolean;
2829
streamResponse: boolean;
2930
id?: string;
3031
showNodeStatus?: boolean;
32+
version?: 'v1' | 'v2';
3133
}) => {
3234
return ({
3335
write,
@@ -70,7 +72,8 @@ export const getWorkflowResponseWrite = ({
7072
res,
7173
write,
7274
event: detail ? event : undefined,
73-
data: JSON.stringify(data)
75+
data: JSON.stringify(data),
76+
version
7477
});
7578
};
7679
};

projects/app/src/pages/api/core/chat/chatTest.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
150150
detail: true,
151151
streamResponse: true,
152152
id: chatId,
153-
showNodeStatus: true
153+
showNodeStatus: true,
154+
version: 'v2'
154155
});
155156

156157
/* start process */
@@ -182,7 +183,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
182183
histories: newHistories,
183184
stream: true,
184185
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
185-
workflowStreamResponse: workflowResponseWrite
186+
workflowStreamResponse: workflowResponseWrite,
187+
version: 'v2'
186188
});
187189

188190
workflowResponseWrite({
@@ -195,12 +197,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
195197
responseWrite({
196198
res,
197199
event: SseResponseEventEnum.answer,
198-
data: '[DONE]'
199-
});
200-
responseWrite({
201-
res,
202-
event: SseResponseEventEnum.flowResponses,
203-
data: JSON.stringify(flowResponses)
200+
data: '[DONE]',
201+
version: 'v2'
204202
});
205203

206204
// save chat

0 commit comments

Comments
 (0)