Document extraction is unavailable. Configure SURVEYJS_AI_PROVIDER (openai, anthropic or ollama) and its key
(OPENAI_API_KEY or ANTHROPIC_API_KEY) or SURVEYJS_OLLAMA_BASE_URL in .env, then restart the app.
- Source file
- Document ID
- Answers to review
Review the draft before submitting.
Source document · Open document
JSON definition · shared/definitions/extract-from-paper.json
View on GitHub
{
"title": "Work order",
"description": "Completed on paper by the technician and scanned at the office",
"elements": [
{
"type": "text",
"name": "customer_name",
"title": "Customer name"
},
{
"type": "text",
"name": "site_address",
"title": "Site address"
},
{
"type": "text",
"name": "visit_date",
"title": "Visit date",
"inputType": "date"
},
{
"type": "radiogroup",
"name": "work_type",
"title": "Type of work",
"choices": [
{
"value": "installation",
"text": "Installation"
},
{
"value": "repair",
"text": "Repair"
},
{
"value": "inspection",
"text": "Inspection"
}
]
},
{
"type": "checkbox",
"name": "parts",
"title": "Parts replaced",
"choices": [
{
"value": "filter",
"text": "Filter"
},
{
"value": "valve",
"text": "Valve"
},
{
"value": "pump",
"text": "Pump"
},
{
"value": "sensor",
"text": "Sensor"
}
]
},
{
"type": "text",
"name": "hours",
"title": "Hours on site",
"inputType": "number"
},
{
"type": "boolean",
"name": "resolved",
"title": "Issue resolved"
},
{
"type": "text",
"name": "technician",
"title": "Technician"
},
{
"type": "comment",
"name": "notes",
"title": "Notes"
}
]
}
Client · shared/client/extract-from-paper.js · sjs:extract-from-paper.client
View on GitHub
// Extract a draft without saving it and validate the response before replacing any answers
async function extract(file, signal) {
const body = new FormData();
body.append("scan", file);
const response = await fetch("/api/work-orders/extract", { method: "POST", body, signal });
if (!response.ok) throw await responseError(response, "Couldn't extract the answers. Try again or enter the answers directly in the form.");
const result = await response.json();
if (!result.answers || typeof result.answers !== "object" || Array.isArray(result.answers) || !Array.isArray(result.confidence)) {
throw new Error("The extraction result couldn't be read. Try again or enter the answers directly in the form.");
}
return result;
}
Client · shared/client/extract-from-paper.js · sjs:extract-from-paper.save
View on GitHub
// Save only after human review and confirm the response ID returned by the backend
survey.onComplete.add(async (sender, options) => {
lock(true, true);
sender.completedHtml = "<h3>Saving the reviewed response...</h3>";
options.showSaveInProgress();
try {
const data = Object.fromEntries(sender.getAllQuestions()
.filter(q => q !== reviewed).map(q => [q.valueName || q.name, q.isEmpty() ? null : q.value]));
const response = await fetch("/api/responses", {
method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ formId: "work-order", data })
});
if (!response.ok) throw await responseError(response, "Couldn't save the reviewed response. Please try again.");
const { id } = await response.json();
if (!Number.isInteger(id) || id < 1) throw new Error("The server didn't confirm a saved response. Please try again.");
setStoredQuery({ id });
sender.completedHtml = `<h3>The reviewed response has been saved.</h3><p>Response ID: ${id}</p>`;
showStatus(`Reviewed response #${id} saved.`);
options.showSaveSuccess();
} catch (error) {
sender.completedHtml = "<h3>The reviewed response hasn't been saved.</h3>";
options.showSaveError(error instanceof TypeError ? "Couldn't save the reviewed response. Please try again." : error.message);
} finally { lock(false); }
});
Server · routes/extract-from-paper.js · sjs:extract-from-paper.template
View on GitHub
// Generate a blank, fillable PDF using the same schema as extraction
app.get("/api/work-orders/blank.pdf", async (req, res) => {
const form = workOrder(req);
if (!form) throw httpError(404, "Unknown work order form");
const definition = JSON.parse(form.json);
if (lintSurvey(definition, lintOptions).findings.some((finding) => finding.severity === "error")) {
surveyjs.logFailure("blank work order", "the work-order definition has errors");
return res.status(500).json({ error: "The stored definition has errors" });
}
const pdf = await surveyjs.renderPdf(definition).catch((error) => error); // new SurveyPDF(…) without answers
if (!surveyjs.isPdf(pdf)) { // it threw, or its bytes aren't a PDF
surveyjs.logFailure("blank work order", pdf);
return res.status(500).json({ error: "Couldn't generate the PDF" });
}
res.set({ "Content-Disposition": 'attachment; filename="work-order.pdf"', "Cache-Control": "private, no-store" });
res.type("pdf").send(pdf);
});
Server · routes/extract-from-paper.js · sjs:extract-from-paper.server
View on GitHub
// POST /api/work-orders/extract — read the answers from the uploaded scan; return them as a draft response
app.post("/api/work-orders/extract", upload.single("scan"), async (req, res) => {
if (!req.file) throw httpError(400, 'Upload the scan in a field named "scan"');
if (!detectType(req.file.buffer)) throw httpError(400, "The file is not a PDF, PNG, JPEG, WebP or GIF document");
const definition = JSON.parse(workOrder(req).json);
if (lintSurvey(definition, lintOptions).findings.some((finding) => finding.severity === "error")) {
surveyjs.logFailure("extract-from-paper", "the work-order definition has errors");
return res.status(500).json({ error: "The stored definition has errors" });
}
let result;
try {
const provider = aiProvider.create(req.app.locals.config.extraction); // from SURVEYJS_AI_PROVIDER and its key
const extractor = createExtractor({ provider, adapter: "surveyjs", options: {} });
const formDefinition = surveyjs.toFormDefinition(definition);
result = await surveyjs.withTimeout(extractor.extractFromImage({ image: req.file.buffer, formDefinition }), 120_000);
if (!isObject(result?.data)) throw new TypeError("The extractor returned no answers object");
} catch (error) {
const status = error instanceof AiConfigurationError ? 503 : error.name === "TimeoutError" ? 504 : 502;
if (status !== 503) surveyjs.logFailure("extract-from-paper", error, { withMessage: false }); // it may quote the answers
return res.status(status).json({ error: EXTRACTION_ERRORS[status] });
}
const { data: answers, confidence = [], uniqueId = null } = result; // never rawResponse: it repeats the whole form
res.json({ answers, confidence, uniqueId });
}, scanErrors);