Download src/utils/batch-definition.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/batch-definition.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/batch-definition.js
-
curl -L -o batch-definition.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/batch-definition.js
2.54 kB
| // ๋ฐฐ์น ์คํ ์ค์ ๊ณผ ํ ์คํธ ์ฆ๊ฑฐ์ ๊ณตํต ๊ณ์ฝ. docs/specs/batch-certification.md v1.0.0 | |
| const { createHash } = require('crypto'); | |
| function ordered(value) { | |
| if (Array.isArray(value)) return value.map(ordered); | |
| if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(key => [key, ordered(value[key])])); | |
| return value; | |
| } | |
| function execution(task) { | |
| const steps = task.steps_json ? JSON.parse(task.steps_json) : task.steps || []; | |
| if (!Array.isArray(steps)) throw new Error('Batch steps must be an array'); | |
| return JSON.stringify(ordered({ project: task.project_id, steps, | |
| legacy: steps.length ? null : [task.prompt || 'hello', task.max_turns || 30], | |
| loop: require('../config/loop-configuration').normalize(task.loop_config, { requireGoals: false }) })); | |
| } | |
| const fingerprint = task => createHash('sha256').update(execution(task)).digest('hex'); | |
| function certified(task) { | |
| try { return task?.is_tested_success === 1 && task.tested_fingerprint === fingerprint(task); } | |
| catch { return false; } | |
| } | |
| function forSave(previous, task) { | |
| let valid = false; | |
| try { valid = certified(previous) && previous.tested_fingerprint === fingerprint(task); } catch {} | |
| return { is_tested_success: valid ? 1 : 0, tested_fingerprint: valid ? previous.tested_fingerprint : null }; | |
| } | |
| function assertCertified(task) { if (!certified(task)) throw new Error('ํ์ฌ ์คํ ์ค์ ์ผ๋ก ๊ตฌ๋ ํ ์คํธ๋ฅผ ์ฑ๊ณต์ํจ ๋ค ์ค์ผ์ค์ ํ์ฑํํ์ธ์.'); } | |
| function testStatus(task, status, testedFingerprint) { | |
| if (!status) return { is_tested_success: 0, tested_fingerprint: null }; | |
| if (!task || testedFingerprint !== fingerprint(task)) throw new Error('Tested batch fingerprint does not match the saved definition'); | |
| return { is_tested_success: 1, tested_fingerprint: testedFingerprint }; | |
| } | |
| // ์ฒดํฌํฌ์ธํธ ์ ์์ ์์ฅ์ ์๊ธฐ ์ ์ฑ ์ ๋น๊ตํ๋ค. ๋ถ๋ชจ ์์ ๊ธ์ก์ ๋ ํด ์ ์๋ค. | |
| function assertRequestReservation(definition, record) { | |
| const { normalize } = require('../services/runtime-request-reservation'); | |
| const own = normalize(definition.loop?.request_reserve_usd), effective = normalize(record?.requestReserveUsd); | |
| if (own !== normalize(record?.requestReserveOwnUsd) || own !== null && (effective === null || effective < own)) throw new Error('Batch request reservation differs from the saved definition'); | |
| } | |
| module.exports = { ordered, execution, fingerprint, certified, forSave, assertCertified, testStatus, assertRequestReservation }; | |