Response Streaming (#124)

This commit is contained in:
Gregory Schier
2024-10-09 16:27:37 -07:00
committed by GitHub
parent 2ca30bcb31
commit da6baf72f5
20 changed files with 425 additions and 301 deletions

View File

@@ -1,26 +1,34 @@
import type { EditorProps } from '../components/core/Editor';
export function languageFromContentType(contentType: string | null): EditorProps['language'] {
export function languageFromContentType(
contentType: string | null,
content: string | null = null,
): EditorProps['language'] {
const justContentType = contentType?.split(';')[0] ?? contentType ?? '';
if (justContentType.includes('json')) {
return 'json';
} else if (justContentType.includes('xml')) {
return 'xml';
} else if (justContentType.includes('html')) {
return 'html';
return detectFromContent(content);
} else if (justContentType.includes('javascript')) {
return 'javascript';
} else {
return 'text';
}
return detectFromContent(content);
}
export function isJSON(text: string): boolean {
try {
JSON.parse(text);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
return false;
}
return text.startsWith('{') || text.startsWith('[');
}
function detectFromContent(content: string | null): EditorProps['language'] {
if (content == null) return 'text';
if (content.startsWith('{') || content.startsWith('[')) {
return 'json';
} else if (content.startsWith('<!DOCTYPE') || content.startsWith('<html')) {
return 'html';
}
return 'text';
}

View File

@@ -32,8 +32,11 @@ export function cookieDomain(cookie: Cookie): string {
return 'unknown';
}
export function isResponseLoading(response: HttpResponse | GrpcConnection): boolean {
return response.elapsed === 0;
export function isResponseLoading(
response: Pick<HttpResponse | GrpcConnection, 'state'> | null,
): boolean {
if (response == null) return false;
return response.state !== 'closed';
}
export function modelsEq(a: AnyModel, b: AnyModel) {