mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 21:29:11 +00:00
Fix rendering image on chat response in obsidian client
This commit is contained in:
@@ -2,6 +2,12 @@ import { App, MarkdownRenderer, Modal, request, requestUrl, setIcon } from 'obsi
|
|||||||
import { KhojSetting } from 'src/settings';
|
import { KhojSetting } from 'src/settings';
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
|
export interface ChatJsonResult {
|
||||||
|
image?: string;
|
||||||
|
detail?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export class KhojChatModal extends Modal {
|
export class KhojChatModal extends Modal {
|
||||||
result: string;
|
result: string;
|
||||||
setting: KhojSetting;
|
setting: KhojSetting;
|
||||||
@@ -270,6 +276,23 @@ export class KhojChatModal extends Modal {
|
|||||||
|
|
||||||
this.result = "";
|
this.result = "";
|
||||||
responseElement.innerHTML = "";
|
responseElement.innerHTML = "";
|
||||||
|
if (response.headers.get("content-type") == "application/json") {
|
||||||
|
let responseText = ""
|
||||||
|
try {
|
||||||
|
const responseAsJson = await response.json() as ChatJsonResult;
|
||||||
|
if (responseAsJson.image) {
|
||||||
|
responseText = ``;
|
||||||
|
} else if (responseAsJson.detail) {
|
||||||
|
responseText = responseAsJson.detail;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// If the chunk is not a JSON object, just display it as is
|
||||||
|
responseText = response.body.read().toString()
|
||||||
|
} finally {
|
||||||
|
this.renderIncrementalMessage(responseElement, responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for await (const chunk of response.body) {
|
for await (const chunk of response.body) {
|
||||||
let responseText = chunk.toString();
|
let responseText = chunk.toString();
|
||||||
if (responseText.includes("### compiled references:")) {
|
if (responseText.includes("### compiled references:")) {
|
||||||
@@ -315,17 +338,6 @@ export class KhojChatModal extends Modal {
|
|||||||
references.appendChild(referenceSection);
|
references.appendChild(referenceSection);
|
||||||
} else {
|
} else {
|
||||||
if (responseText.startsWith("{") && responseText.endsWith("}")) {
|
if (responseText.startsWith("{") && responseText.endsWith("}")) {
|
||||||
try {
|
|
||||||
const responseAsJson = JSON.parse(responseText);
|
|
||||||
if (responseAsJson.image) {
|
|
||||||
responseText = ``;
|
|
||||||
} else if (responseAsJson.detail) {
|
|
||||||
responseText = responseAsJson.detail;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// If the chunk is not a JSON object, just display it as is
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// If the chunk is not a JSON object, just display it as is
|
// If the chunk is not a JSON object, just display it as is
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user