Skip to content

Commit

Permalink
feat: when error add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed Mar 22, 2023
1 parent 4b22cf4 commit 9c25674
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
8 changes: 7 additions & 1 deletion components/tool/ToolDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ const {
reset,
toggleHistory,
error,
resend,
} = useAi(props.tool);
function submit(data: any) {
send(data);
if (error.value) {
resend();
} else {
send(data);
}
}
function stop() {
cancel();
Expand Down Expand Up @@ -60,6 +65,7 @@ defineExpose({
@stop="stop"
@reset="handleReset"
:tool="tool"
:error="error"
/>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion components/tool/ToolForms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineComponent({
tool: Object as PropType<ToolItem>,
readonly: Boolean,
loading: Boolean,
error: {} as any,
},
setup(props) {
const { formData } = useToolFormData(props.tool!);
Expand Down Expand Up @@ -87,7 +88,8 @@ export default defineComponent({
<el-button flex-1 @click="submit" type="primary" w-full>
<div flex items-center>
<el-icon class="text-xl! i-carbon:send-alt-filled mr-1"></el-icon>
<span>{{ $t("tool.forms.submit") }}</span>
<span v-if="!error">{{ $t("tool.forms.submit") }}</span>
<span v-else="">{{ $t("tool.forms.retry") }}</span>
<div ml-4 hidden md:block>
<kbd>Shift</kbd>
<span>+</span>
Expand Down
49 changes: 29 additions & 20 deletions composables/useAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,8 @@ export const useAi = (_tool: MaybeRef<ToolItem>) => {
let signal = controller.signal;
const error = ref<any>(null);

const send = async (data: MaybeRef<Record<string, any>>) => {
const _send = async (messages: OpenAIMessages) => {
error.value = null;
const tool = unref(_tool);
data = unref(data);
let messages = parseRoles(data, tool?.roles!);
if (messages.some((v) => !v.content)) {
ElMessage.error("Content cannot be empty");
return;
}
if (!tool.chat) {
reset();
}

if (!contexts.value.length) {
contexts.value.push(...messages);
create(contexts.value);
} else if (tool.chat) {
contexts.value.push(messages[messages.length - 1]);
}
messages = contexts.value;

result.value = "";
loading.value = true;
try {
Expand Down Expand Up @@ -68,6 +49,33 @@ export const useAi = (_tool: MaybeRef<ToolItem>) => {
});
loading.value = false;
};

const send = async (data: MaybeRef<Record<string, any>>) => {
const tool = unref(_tool);
data = unref(data);
let messages = parseRoles(data, tool?.roles!);
if (messages.some((v) => !v.content)) {
ElMessage.error("Content cannot be empty");
return;
}
if (!tool.chat) {
reset();
}

if (!contexts.value.length) {
contexts.value.push(...messages);
create(contexts.value);
} else if (tool.chat) {
contexts.value.push(messages[messages.length - 1]);
}
messages = contexts.value;
await _send(messages);
};

const resend = async () => {
_send(contexts.value);
};

const cancel = () => {
controller.abort();
controller = new AbortController();
Expand Down Expand Up @@ -96,6 +104,7 @@ export const useAi = (_tool: MaybeRef<ToolItem>) => {
};
return {
send,
resend,
resultHtml,
result,
loading,
Expand Down
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"app.gpt-setting-forms.default-provider": "Default Provider",
"create.the-role.chat.label": "With Context (Chat Mode)",
"tool.forms.stop": "Stop",
"tool.forms.new": "New Chat"
"tool.forms.new": "New Chat",
"tool.forms.retry": "Retry"
}
3 changes: 2 additions & 1 deletion locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"tool.forms.stop": "停止",
"app.gpt-setting-forms.default-provider": "默认GPT提供商",
"create.the-role.chat.label": "关联上下文 (Chat Mode)",
"tool.forms.new": "新会话"
"tool.forms.new": "新会话",
"tool.forms.retry": "重试"
}

0 comments on commit 9c25674

Please sign in to comment.