Download app.js from saltiresoftware/coregx-triangles: direct link, hf CLI and curl.
- Browser
- Download file 17.5 kB
-
https://huggingface.co/spaces/saltiresoftware/coregx-triangles/resolve/main/app.js
- Command line
-
hf download hf://spaces/saltiresoftware/coregx-triangles/app.js
-
curl -L -o app.js https://huggingface.co/spaces/saltiresoftware/coregx-triangles/resolve/main/app.js
17.5 kB
| const BUILD_ID = "20261001-triangles-6"; | |
| const COREGX_PROXY_URL = window.TRIANGLE_STUDIO_CONFIG.coregxProxyUrl; | |
| const EXAMPLE_BANKS = { | |
| "sides": [ | |
| "Draw a 3, 4, 5 triangle.", | |
| "Triangle ABC with AB=5, BC=6, AC=7.", | |
| "Draw triangle PQR with PQ=8, QR=10 and PR=12.", | |
| "A scalene triangle with sides 7, 9 and 11.", | |
| "Draw triangle ABC with symbolic side lengths AB=x, BC=2*x, and AC=2.5*x." | |
| ], | |
| "angles": [ | |
| "Triangle ABC with AB=8, AC=6 and angle BAC=60 degrees.", | |
| "Draw triangle ABC with AB=10, angle BAC=40 degrees and angle ABC=65 degrees.", | |
| "Triangle PQR: PQ=7, PR=5, angle QPR=75 degrees.", | |
| "Triangle ABC with AB=6, angle BAC=30 degrees and angle ABC=90 degrees.", | |
| "Draw triangle ABC with AB=8, AC=6, and angle BAC=pi/3 radians." | |
| ], | |
| "special": [ | |
| "An equilateral triangle with side length 6.", | |
| "An isosceles triangle with equal sides AB=7 and AC=7, and base BC=8.", | |
| "A right triangle ABC with right angle at B, AB=3 and BC=4.", | |
| "Animate an isosceles triangle ABC with equal sides AB=AC=t and base BC=6, varying t from 5 to 8 over 3 seconds.", | |
| "An isosceles right triangle ABC with the right angle at A and legs AB=5 and AC=5." | |
| ], | |
| "constructions": [ | |
| "A 3, 4, 5 triangle with a median from A to BC.", | |
| "Triangle ABC with AB=5, BC=6 and AC=7. Draw all three medians.", | |
| "Triangle ABC with AB=6, AC=6 and BC=8. Draw the altitude from A to BC.", | |
| "Draw a triangle with sides AB=7, BC=8, AC=9 and the angle bisector at B.", | |
| "Triangle ABC has sides AB=5, BC=6, AC=7. Mark the midpoints of all sides and join them to form the medial triangle." | |
| ] | |
| }; | |
| const exampleIndices = Object.fromEntries(Object.keys(EXAMPLE_BANKS).map(name => [name, 0])); | |
| const ui = { | |
| description: document.querySelector("#description"), | |
| build: document.querySelector("#build"), | |
| status: document.querySelector("#status"), | |
| preview: document.querySelector("#preview"), | |
| program: document.querySelector("#program"), | |
| downloads: document.querySelector("#downloads"), | |
| downloadProgram: document.querySelector("#download-program"), | |
| downloadSvg: document.querySelector("#download-svg"), | |
| downloadCad: document.querySelector("#download-cad"), | |
| }; | |
| let programUrl; | |
| let svgUrl; | |
| let cadUrl; | |
| function setStatus(message, isError = false) { | |
| ui.status.textContent = message; | |
| ui.status.classList.toggle("error", isError); | |
| } | |
| function showSvg(svg) { | |
| ui.preview.innerHTML = svg; | |
| } | |
| function stripCodeFence(value) { | |
| const text = value.trim(); | |
| if (!text.startsWith("```")) return text; | |
| const lines = text.split(/\r?\n/); | |
| lines.shift(); | |
| if (lines.at(-1)?.trim() === "```") lines.pop(); | |
| return lines.join("\n").trim(); | |
| } | |
| function alignNamedRightAngle(description, program) { | |
| const stated = description.match(/right angle\s+(?:is\s+)?at\s+([a-z][a-z0-9_]*)/i); | |
| const triangle = program.match(/^triangle\s+(\w+)\s+(\w+)\s+(\w+)\s*$/im); | |
| if (!stated || !triangle || !/^anglemode\s+degree\s*$/im.test(program)) return program; | |
| const vertices = triangle.slice(1); | |
| const vertex = vertices.find(name => name.toLowerCase() === stated[1].toLowerCase()); | |
| if (!vertex) return program; | |
| const lines = program.split(/\r?\n/); | |
| const rightAngles = lines.map((line, index) => ({ parts: line.trim().split(/\s+/), index })) | |
| .filter(({ parts }) => parts.length === 5 && parts[0].toLowerCase() === "angle" && /^90(?:\.0+)?$/.test(parts[4])); | |
| if (rightAngles.length !== 1) return program; | |
| const { parts, index } = rightAngles[0]; | |
| if (parts.slice(1, 4).some(name => !vertices.includes(name))) return program; | |
| if (parts[2] === vertex) return program; | |
| const otherVertices = vertices.filter(name => name !== vertex); | |
| lines[index] = `angle ${otherVertices[0]} ${vertex} ${otherVertices[1]} ${parts[4]}`; | |
| return lines.join("\n"); | |
| } | |
| function proxyEndpoint(path) { | |
| if (!/^https:\/\/.+/.test(COREGX_PROXY_URL)) { | |
| throw new Error("Add the Cloudflare Worker URL in config.js."); | |
| } | |
| const base = COREGX_PROXY_URL.endsWith("/") ? COREGX_PROXY_URL : `${COREGX_PROXY_URL}/`; | |
| return new URL(path.replace(/^\//, ""), base).toString(); | |
| } | |
| async function proxyFetch(route, options) { | |
| const endpoint = proxyEndpoint(route); | |
| try { | |
| return await fetch(endpoint, options); | |
| } catch (error) { | |
| const detail = error instanceof Error ? error.message : String(error); | |
| throw new Error(`Could not reach the ${route} proxy from ${window.location.origin}: ${detail}`); | |
| } | |
| } | |
| async function generateProgram(description) { | |
| setStatus("Converting to CoreGX using " + window.TRIANGLE_STUDIO_CONFIG.modelName + "…"); | |
| const promptResponse = await fetch("system_prompt.txt?v=" + BUILD_ID, { cache: "reload" }); | |
| if (!promptResponse.ok) throw new Error("Could not load the triangle prompt."); | |
| const response = await proxyFetch("generate", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ messages: [ | |
| { role: "system", content: await promptResponse.text() }, | |
| { role: "user", content: description }, | |
| ] }), | |
| }); | |
| const result = await response.json().catch(() => ({})); | |
| if (!response.ok || !result.ok) throw new Error(String(result.error || "Model request failed with HTTP " + response.status)); | |
| if (typeof result.content !== "string" || !result.content.trim()) throw new Error("The model returned no program."); | |
| return "defaultproperties constraint visible=true\n\n" + alignNamedRightAngle(description, stripCodeFence(result.content)) + "\n"; | |
| } | |
| async function renderCoreGX(program) { | |
| const response = await proxyFetch("render", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ program }), | |
| }); | |
| const result = await response.json().catch(() => ({})); | |
| if (!response.ok) throw new Error(`CoreGX request failed with HTTP ${response.status}.`); | |
| if (!result.ok) throw new Error(String(result.error || "CoreGX rejected the program.")); | |
| if (typeof result?.value?.svg !== "string") throw new Error("CoreGX returned no SVG."); | |
| return result.value.svg; | |
| } | |
| async function requestCad(program) { | |
| const options = { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| program, | |
| }), | |
| }; | |
| const response = await proxyFetch("cad", options); | |
| const result = await response.json().catch(() => ({})); | |
| if (!response.ok || !result.ok) { | |
| throw new Error(String(result.error || `CAD export failed with HTTP ${response.status}.`)); | |
| } | |
| const dxf = result?.value?.dxf ?? result.dxf; | |
| if (typeof dxf !== "string" || !dxf.trim()) throw new Error("CoreGX returned no CAD file."); | |
| return dxf; | |
| } | |
| async function exportCad() { | |
| const program = ui.program.textContent.trim(); | |
| if (!program) return; | |
| ui.downloadCad.disabled = true; | |
| setStatus("Exporting CAD…"); | |
| try { | |
| const dxf = await requestCad(program); | |
| if (cadUrl) URL.revokeObjectURL(cadUrl); | |
| cadUrl = URL.createObjectURL(new Blob([dxf], { type: "application/dxf;charset=utf-8" })); | |
| const link = document.createElement("a"); | |
| link.href = cadUrl; | |
| link.download = "triangle.dxf"; | |
| link.click(); | |
| setStatus("✓ CAD exported"); | |
| } catch (error) { | |
| setStatus(`⚠ ${error instanceof Error ? error.message : String(error)}`, true); | |
| } finally { | |
| ui.downloadCad.disabled = false; | |
| } | |
| } | |
| function setDownloads(program, svg) { | |
| if (programUrl) URL.revokeObjectURL(programUrl); | |
| if (svgUrl) URL.revokeObjectURL(svgUrl); | |
| programUrl = URL.createObjectURL(new Blob([program], { type: "text/plain;charset=utf-8" })); | |
| svgUrl = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml;charset=utf-8" })); | |
| ui.downloadProgram.href = programUrl; | |
| ui.downloadSvg.href = svgUrl; | |
| ui.downloadProgram.classList.remove("is-disabled"); | |
| ui.downloadSvg.classList.remove("is-disabled"); | |
| ui.downloadProgram.removeAttribute("aria-disabled"); | |
| ui.downloadSvg.removeAttribute("aria-disabled"); | |
| ui.downloadCad.disabled = false; | |
| } | |
| async function buildTriangle() { | |
| const description = ui.description.value.trim(); | |
| if (!description) return setStatus("Describe a triangle first.", true); | |
| ui.build.disabled = true; | |
| ui.build.textContent = "Building…"; | |
| try { | |
| const program = await generateProgram(description); | |
| setStatus("Drawing the triangle…"); | |
| const svg = await renderCoreGX(program); | |
| showSvg(svg); | |
| ui.program.textContent = program; | |
| setDownloads(program, svg); | |
| setStatus("✓ Triangle built"); | |
| } catch (error) { | |
| setStatus(error instanceof Error ? error.message : String(error), true); | |
| } finally { | |
| ui.build.disabled = false; | |
| ui.build.textContent = "Build triangle"; | |
| } | |
| } | |
| document.querySelectorAll("[data-example]").forEach((button) => { | |
| button.addEventListener("click", () => { | |
| const name = button.dataset.example; | |
| const bank = EXAMPLE_BANKS[name]; | |
| const index = exampleIndices[name]; | |
| ui.description.value = bank[index]; | |
| exampleIndices[name] = (index + 1) % bank.length; | |
| ui.description.focus(); | |
| }); | |
| }); | |
| ui.build.addEventListener("click", buildTriangle); | |
| ui.downloadCad.addEventListener("click", exportCad); | |
| ui.description.addEventListener("keydown", (event) => { | |
| if ((event.ctrlKey || event.metaKey) && event.key === "Enter") buildTriangle(); | |
| }); | |
| window.addEventListener("beforeunload", () => { | |
| if (programUrl) URL.revokeObjectURL(programUrl); | |
| if (svgUrl) URL.revokeObjectURL(svgUrl); | |
| if (cadUrl) URL.revokeObjectURL(cadUrl); | |
| }); | |
| const STARTING_EXAMPLE_SVG = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 500 500'>\n<defs>\n<marker id = 'arrowhead'\nmarkerWidth = '10'\nmarkerHeight = '7'\nrefX = '10'\nrefY = '3.5'\norient = 'auto'\nmarkerUnits = 'strokeWidth'>\n<polygon points = '0 0, 10 3.5, 0 7' style = 'fill: context-stroke'/>\n</marker>\n<marker id = 'distance-arrowhead-end'\nmarkerWidth = '5'\nmarkerHeight = '5'\nrefX = '5'\nrefY = '2.5'\norient = 'auto'\nmarkerUnits = 'strokeWidth'>\n<polygon points = '0 0, 5 2.5, 0 5' style = 'fill: context-stroke'/>\n</marker>\n<marker id = 'distance-arrowhead-start'\nmarkerWidth = '5'\nmarkerHeight = '5'\nrefX = '0'\nrefY = '2.5'\norient = 'auto'\nmarkerUnits = 'strokeWidth'>\n<polygon points = '5 0, 0 2.5, 5 5' style = 'fill: context-stroke'/>\n</marker>\n</defs>\n<defs>\n<path id='m0_g0' d='M127 463C163 463 186 489 186 520C186 549 166 575 139 578C135 579 132 580 132 580C131 581 144 594 152 599C176 616 210 626 240 626C260 626 272 622 287 612C313 596 322 569 322 525C322 449 293 379 232 371C226 370 177 366 174 366C173 366 171 365 169 363C166 360 166 360 166 348C166 336 166 335 168 332L171 330L213 329C259 328 259 328 274 322C314 302 338 257 338 195L338 175C338 125 334 99 321 74C303 40 273 21 238 21C189 21 145 40 114 66L121 68C150 75 169 99 169 130C169 166 142 193 105 193C71 193 42 168 42 130C42 115 45 96 52 81C76 19 150-22 238-22C329-22 407 29 438 96C451 122 457 148 457 175C457 232 426 282 375 315C333 343 299 345 299 348C299 349 300 349 302 350C372 376 430 441 430 522C430 532 429 542 427 549C416 589 388 621 344 643C322 653 301 660 277 664C268 665 248 665 233 665L218 665C145 656 69 605 69 524C69 489 90 463 127 463Z'/>\n<g id='m0_label'>\n<use xlink:href='#m0_g0' transform='translate(0,750) scale(1,-1)'/>\n</g>\n<path id='m1_g0' d='M462 0L471 0L471 46L442 46C401 47 392 48 385 54C379 60 379 59 379 114L379 165L471 165L471 211L379 211L379 671L373 677L355 677C338 677 336 677 334 675C332 674 274 586 179 442L28 211L28 165L287 165L287 115C287 81 287 63 286 61C284 57 279 52 274 51C266 48 253 47 221 46L190 46L190 0L199 0C213 3 306 3 333 3C359 3 448 3 462 0M293 211L183 211L74 212L293 545Z'/>\n<g id='m1_label'>\n<use xlink:href='#m1_g0' transform='translate(0,750) scale(1,-1)'/>\n</g>\n<path id='m2_g0' d='M107 214C74 214 50 193 50 154C50 148 51 141 51 135C56 102 68 71 89 46C96 36 113 19 122 13C151-10 182-22 229-22C355-22 449 81 449 201C449 307 385 393 297 415C284 418 270 420 256 420C203 420 170 399 139 374L139 546L149 544C166 540 186 538 205 538C301 538 365 581 410 635L410 648C410 660 408 666 402 666C399 666 395 665 389 662C347 644 300 634 253 634C201 634 160 644 120 660C111 663 103 666 102 666C101 666 99 665 98 664C93 664 92 646 92 608C92 579 93 539 93 485C93 329 93 314 95 310C97 305 100 304 114 304C129 304 131 305 136 314C159 353 200 382 254 382C287 382 302 369 318 349C338 322 345 287 345 210C345 148 340 109 326 82C307 45 275 22 224 22C165 22 124 56 105 96L102 101L109 101C135 101 164 124 164 156C164 190 139 214 107 214Z'/>\n<g id='m2_label'>\n<use xlink:href='#m2_g0' transform='translate(0,750) scale(1,-1)'/>\n</g>\n</defs>\n<defs>\n<clipPath id='view-clip'>\n<rect width='500' height='500'/>\n</clipPath>\n</defs>\n<g clip-path='url(#view-clip)'>\n<g transform='translate(0 500) scale(1,-1)'>\n<g transform='scale(76.21134257) translate(5.981139004 8.174169756)'>\n<g>\n<line id='DistanceSymbol' x1='-0.129588204755486' y1='-5.79534280908257' x2='0.364103343105282' y2='-5.41005237808996' stroke='gray' stroke-width='0.01312140642'/>\n<line id='DistanceSymbol' x1='-1.97531205062512' y1='-3.4303262161623' x2='-1.48162050276436' y2='-3.0450357851697' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='0.281350425998425' y1='-5.47463502536467' x2='-0.558027405299916' y2='-4.39909899477572' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='-0.724995588572869' y1='-4.18515446303336' x2='-1.56437341987121' y2='-3.1096184324444' stroke='gray' stroke-width='0.01312140642'/>\n<polygon points='0.281350426,-5.474635025 0.2587737271,-5.39238826 0.2070531539,-5.432752415' fill='gray' stroke='none'/>\n<polygon points='-1.56437342,-3.109618432 -1.541796721,-3.191865198 -1.490076148,-3.151501043' fill='gray' stroke='none'/>\n<line id='DistanceSymbol' x1='-2.0858137239113' y1='-3.41669851378819' x2='-2.59415241129521' y2='-2.76533919306607' stroke='gray' stroke-width='0.01312140642'/>\n<line id='DistanceSymbol' x1='-5.23916918113832' y1='-5.87766364161437' x2='-5.74750786852223' y2='-5.22630432089225' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='-2.5295697640205' y1='-2.84809211017293' x2='-4.01767799927241' y2='-4.00945261109353' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='-4.19481698599562' y1='-4.1476967370785' x2='-5.68292522124753' y2='-5.30905723799911' stroke='gray' stroke-width='0.01312140642'/>\n<polygon points='-2.529569764,-2.84809211 -2.611816529,-2.870668809 -2.571452375,-2.922389382' fill='gray' stroke='none'/>\n<polygon points='-5.682925221,-5.309057238 -5.600678456,-5.286480539 -5.641042611,-5.234759966' fill='gray' stroke='none'/>\n<line id='DistanceSymbol' x1='-0.19014211691889' y1='-5.92249373607633' x2='-0.170448818048794' y2='-6.94854757720824' stroke='gray' stroke-width='0.01312140642'/>\n<line id='DistanceSymbol' x1='-5.18922142001555' y1='-6.01844227098225' x2='-5.16952812114546' y2='-7.04449611211416' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='-0.172463185604446' y1='-6.84359565515793' x2='-2.58343334379117' y2='-6.88986998696071' stroke='gray' stroke-width='0.01312140642'/>\n<line id='MeasurementLine' x1='-2.76057233051438' y1='-6.89326985826107' x2='-5.17154248870111' y2='-6.93954419006385' stroke='gray' stroke-width='0.01312140642'/>\n<polygon points='-0.1724631856,-6.843595655 -0.251806617,-6.812308955 -0.2505476373,-6.877903906' fill='gray' stroke='none'/>\n<polygon points='-5.171542489,-6.93954419 -5.092199057,-6.97083089 -5.093458037,-6.905235939' fill='gray' stroke='none'/>\n</g>\n<g>\n<line id='AB' x1='-0.191652892585629' y1='-5.8437797945386' x2='-2.03737673845527' y2='-3.47876320161833' stroke='#000000' stroke-width='0.02624281285'/>\n<line id='BC' x1='-2.03737673845527' y1='-3.47876320161833' x2='-5.19073219568229' y2='-5.93972832944451' stroke='#000000' stroke-width='0.02624281285'/>\n<line id='AC' x1='-0.191652892585629' y1='-5.8437797945386' x2='-5.19073219568229' y2='-5.93972832944451' stroke='#000000' stroke-width='0.02624281285'/>\n</g>\n<g>\n<circle id='A' cx='-0.1916528926' cy='-5.843779795' r='0.06560703212' fill='#A463BF' />\n<circle id='B' cx='-2.037376738' cy='-3.478763202' r='0.06560703212' fill='#A463BF' />\n<circle id='C' cx='-5.190732196' cy='-5.939728329' r='0.06560703212' fill='#A463BF' />\n</g>\n</g>\n</g>\n<g>\n<text x='456.4612453' y='329.3023047' font-size='15' fill='black' font-style='normal' text-anchor='start' dominant-baseline='middle'>A</text>\n<text x='305.8369379' y='124.5008104' font-size='15' fill='black' font-style='normal' text-anchor='start' dominant-baseline='middle'>B</text>\n<text x='39.25965676' y='337.262071' font-size='15' fill='black' font-style='normal' text-anchor='start' dominant-baseline='middle'>C</text>\n<g transform='translate(406.9401811,204.144289) scale(0.015) translate(-250,-428.5)' fill='black' opacity='1'>\n<use xlink:href='#m0_label'/>\n</g>\n<g transform='translate(142.8879992,187.8692002) scale(0.015) translate(-250,-411.5)' fill='black' opacity='1'>\n<use xlink:href='#m1_label'/>\n</g>\n<g transform='translate(252.19371,402.2513447) scale(0.015) translate(-250,-428)' fill='black' opacity='1'>\n<use xlink:href='#m2_label'/>\n</g>\n</g>\n</g>\n</svg>"; | |
| const STARTING_EXAMPLE_PROGRAM = "triangle A B C\ndistance AB 3\ndistance AC 4\ndistance BC 5"; | |
| showSvg(STARTING_EXAMPLE_SVG); | |
| ui.program.textContent = STARTING_EXAMPLE_PROGRAM; | |
| setDownloads(STARTING_EXAMPLE_PROGRAM, STARTING_EXAMPLE_SVG); |