WIP Hub 1.6.0: AI proxy for Cross Events 2.0
Signed /installs/ai/status and /installs/ai/complete (protocol 3b): the Hub holds the Claude key, holds each call's worst case against the licence's monthly cap in a locked transaction, calls Claude Haiku 5.5 and meters real cost and charged amount (markup) in rand. Settings -> AI, licence markup override, AI use on the install page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ const { requestMessage, requestedVersionOf, isOutdated } = require('../utils/upd
|
||||
const { sendMail } = require('../utils/mail');
|
||||
const { releasesForInstall } = require('../utils/releases');
|
||||
const { noticesForInstall } = require('../utils/notices');
|
||||
const ai = require('../utils/ai');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -24,6 +25,8 @@ const HEARTBEAT_MINUTES = 15;
|
||||
const registerLimiter = rateLimit({ windowMs: 60 * 60 * 1000, max: 20, standardHeaders: true, legacyHeaders: false, message: { message: 'Too many attempts.', code: 'RATE_LIMITED' } });
|
||||
const heartbeatLimiter = rateLimit({ windowMs: 60 * 1000, max: 30, standardHeaders: true, legacyHeaders: false, message: { message: 'Too many requests.', code: 'RATE_LIMITED' } });
|
||||
const updateRequestLimiter = rateLimit({ windowMs: 60 * 60 * 1000, max: 10, standardHeaders: true, legacyHeaders: false, message: { message: 'Too many requests.', code: 'RATE_LIMITED' } });
|
||||
// AI is limited by money (the cap); this only stops a runaway loop.
|
||||
const aiLimiter = rateLimit({ windowMs: 60 * 1000, max: 60, standardHeaders: true, legacyHeaders: false, message: { message: 'Too many AI requests — wait a minute.', code: 'RATE_LIMITED' } });
|
||||
|
||||
const fail = (res, status, code, message) => res.status(status).json({ code, message });
|
||||
|
||||
@@ -246,5 +249,33 @@ router.post('/installs/update-request/cancel', updateRequestLimiter, async (req,
|
||||
}
|
||||
});
|
||||
|
||||
// ─── AI (Cross Events 2.0, §3b) ───────────────────────────────────────────────
|
||||
|
||||
// What the install's AI buttons should do: hidden, working, or disabled until the allowance renews.
|
||||
router.post('/installs/ai/status', aiLimiter, async (req, res) => {
|
||||
try {
|
||||
const install = await signedInstall(req, res);
|
||||
if (!install) return;
|
||||
res.json(await ai.statusFor(install));
|
||||
} catch (e) {
|
||||
console.error('[ai/status]', e);
|
||||
fail(res, 500, 'SERVER_ERROR', 'The Hub could not check the AI allowance.');
|
||||
}
|
||||
});
|
||||
|
||||
// One helper call: the Hub adds the key, checks the cap, calls Claude and meters the call.
|
||||
router.post('/installs/ai/complete', aiLimiter, async (req, res) => {
|
||||
let install;
|
||||
try {
|
||||
install = await signedInstall(req, res);
|
||||
if (!install) return;
|
||||
res.json(await ai.complete(install, req.body));
|
||||
} catch (e) {
|
||||
if (e.status && e.code) return res.status(e.status).json({ code: e.code, message: e.message, ...(e.aiStatus ? { status: e.aiStatus } : {}) });
|
||||
console.error('[ai/complete]', e);
|
||||
fail(res, 500, 'SERVER_ERROR', 'The Hub could not run this AI request.');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports.PROTOCOL_VERSIONS = PROTOCOL_VERSIONS;
|
||||
|
||||
Reference in New Issue
Block a user