copy path and chat about file/folder
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { createRouter } from '../../create-router';
|
||||
import { settingsPath } from './server-settings';
|
||||
|
||||
type OcrConfig = {
|
||||
url: string;
|
||||
model: string;
|
||||
};
|
||||
|
||||
export async function readOcrConfig(): Promise<OcrConfig | undefined> {
|
||||
const settings = await Bun.file(settingsPath).json().catch(() => ({}));
|
||||
return settings.ocr as OcrConfig | undefined;
|
||||
}
|
||||
|
||||
export const ocrRouter = createRouter();
|
||||
|
||||
ocrRouter.get('/', async (ctx) => {
|
||||
const ocr = await readOcrConfig();
|
||||
if (!ocr) return ctx.json(null);
|
||||
return ctx.json(ocr);
|
||||
});
|
||||
|
||||
ocrRouter.put('/', async (ctx) => {
|
||||
const body = await ctx.req.json<OcrConfig>();
|
||||
const settings = await Bun.file(settingsPath).json().catch(() => ({}));
|
||||
settings.ocr = body;
|
||||
await Bun.write(settingsPath, JSON.stringify(settings, null, 2));
|
||||
return ctx.json({ success: true });
|
||||
});
|
||||
|
||||
ocrRouter.post('/models', async (ctx) => {
|
||||
const body = await ctx.req.json<{ url: string }>();
|
||||
if (!body.url) return ctx.json({ error: 'URL required' }, 400);
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const res = await fetch(`${body.url.replace(/\/+$/, '')}/v1/models`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
if (!res.ok) return ctx.json({ error: `Server returned ${res.status}` }, 500);
|
||||
const json = (await res.json()) as { data?: { id: string }[] };
|
||||
const models = (json.data ?? []).map((m) => m.id);
|
||||
return ctx.json({ models });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown error';
|
||||
return ctx.json({ error: message }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
ocrRouter.post('/test', async (ctx) => {
|
||||
const body = await ctx.req.json<OcrConfig>();
|
||||
if (!body.url) return ctx.json({ error: 'URL required' }, 400);
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
const res = await fetch(`${body.url.replace(/\/+$/, '')}/v1/models`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
if (!res.ok) return ctx.json({ error: `Server returned ${res.status}` }, 500);
|
||||
return ctx.json({ success: true });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown error';
|
||||
return ctx.json({ error: message }, 500);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user