Add try/catch around `fetch()`, style system messages correctly

This commit is contained in:
Niek van der Maas 2023-03-06 18:48:13 +01:00
parent 3ff30b4ccb
commit 86bb7f1001
1 changed files with 36 additions and 29 deletions

View File

@ -62,7 +62,9 @@
// Show updating bar // Show updating bar
updating = true; updating = true;
const response: Response = await ( let response: Response;
try {
response = await (
await fetch("https://api.openai.com/v1/chat/completions", { await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST", method: "POST",
headers: { headers: {
@ -85,6 +87,9 @@
}), }),
}) })
).json(); ).json();
} catch (e) {
response = { error: { message: e.message } };
}
// Hide updating bar // Hide updating bar
updating = false; updating = false;
@ -214,6 +219,7 @@
</article> </article>
{:else if message.role === "system"} {:else if message.role === "system"}
<article class="message is-danger"> <article class="message is-danger">
<div class="message-body">
<SvelteMarkdown <SvelteMarkdown
source={message.content} source={message.content}
options={markedownOptions} options={markedownOptions}
@ -221,6 +227,7 @@
/*code: Code*/ /*code: Code*/
}} }}
/> />
</div>
</article> </article>
{:else} {:else}
<article class="message is-success"> <article class="message is-success">