CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/guesslang.ts
Views: 687
1
import { ModelOperations } from "@vscode/vscode-languagedetection";
2
import getParams from "lib/api/get-params";
3
4
import { apiRoute, apiRouteOperation } from "lib/api";
5
import {
6
GuesslangInputSchema,
7
GuesslangOutputSchema
8
} from "lib/api/schema/guesslang";
9
10
const modelOperations = new ModelOperations();
11
12
async function handle(req, res) {
13
const { code, cutoff = 5 } = getParams(req);
14
try {
15
const result = (await modelOperations.runModel(code))
16
.slice(0, parseInt(cutoff))
17
.map((x) => x.languageId);
18
res.json({ result });
19
} catch (err) {
20
res.json({ error: `${err.message ? err.message : err}` });
21
}
22
}
23
24
export default apiRoute({
25
guesslang: apiRouteOperation({
26
method: "POST",
27
openApiOperation: {
28
tags: ["Utils"]
29
},
30
})
31
.input({
32
contentType: "application/json",
33
body: GuesslangInputSchema,
34
})
35
.outputs([
36
{
37
status: 200,
38
contentType: "application/json",
39
body: GuesslangOutputSchema,
40
},
41
])
42
.handler(handle),
43
});
44
45