instar 0.7.46 → 0.7.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/middleware.js +5 -0
- package/dist/server/routes.js +52 -3
- package/package.json +1 -1
|
@@ -32,6 +32,11 @@ export function authMiddleware(authToken) {
|
|
|
32
32
|
next();
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
|
+
// Internal endpoints are localhost-only (server binds 127.0.0.1) — skip auth
|
|
36
|
+
if (req.path.startsWith('/internal/')) {
|
|
37
|
+
next();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
35
40
|
// View routes support signed URLs for browser access (see ?sig= below)
|
|
36
41
|
if (req.path.startsWith('/view/') && req.method === 'GET') {
|
|
37
42
|
const sig = typeof req.query.sig === 'string' ? req.query.sig : null;
|
package/dist/server/routes.js
CHANGED
|
@@ -1420,9 +1420,58 @@ export function createRoutes(ctx) {
|
|
|
1420
1420
|
}
|
|
1421
1421
|
}
|
|
1422
1422
|
else if (ctx.sessionManager) {
|
|
1423
|
-
// No telegram
|
|
1424
|
-
|
|
1425
|
-
|
|
1423
|
+
// No TelegramAdapter (--no-telegram mode) — route using topic-session registry on disk
|
|
1424
|
+
const registryPath = path.join(ctx.config.stateDir, 'topic-session-registry.json');
|
|
1425
|
+
let targetSession = null;
|
|
1426
|
+
try {
|
|
1427
|
+
if (fs.existsSync(registryPath)) {
|
|
1428
|
+
const registry = JSON.parse(fs.readFileSync(registryPath, 'utf-8'));
|
|
1429
|
+
targetSession = registry.topicToSession?.[String(topicId)] ?? null;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
catch { /* registry read failed — fall through to spawn */ }
|
|
1433
|
+
if (targetSession && ctx.sessionManager.isSessionAlive(targetSession)) {
|
|
1434
|
+
// Session exists and is alive — inject message
|
|
1435
|
+
console.log(`[telegram-forward] Injecting into ${targetSession}: "${text.slice(0, 80)}"`);
|
|
1436
|
+
ctx.sessionManager.injectTelegramMessage(targetSession, topicId, text);
|
|
1437
|
+
res.json({ ok: true, forwarded: true, method: 'registry-inject', session: targetSession });
|
|
1438
|
+
}
|
|
1439
|
+
else {
|
|
1440
|
+
// No session or session dead — auto-spawn a new one
|
|
1441
|
+
const topicName = targetSession || `topic-${topicId}`;
|
|
1442
|
+
console.log(`[telegram-forward] No live session for topic ${topicId}, spawning "${topicName}"...`);
|
|
1443
|
+
const contextLines = [
|
|
1444
|
+
`This session was auto-created for Telegram topic ${topicId}.`,
|
|
1445
|
+
``,
|
|
1446
|
+
`CRITICAL: You MUST relay your response back to Telegram after responding.`,
|
|
1447
|
+
`Use the relay script:`,
|
|
1448
|
+
``,
|
|
1449
|
+
`cat <<'EOF' | .claude/scripts/telegram-reply.sh ${topicId}`,
|
|
1450
|
+
`Your response text here`,
|
|
1451
|
+
`EOF`,
|
|
1452
|
+
``,
|
|
1453
|
+
`Strip the [telegram:${topicId}] prefix before interpreting the message.`,
|
|
1454
|
+
`Only relay conversational text — not tool output or internal reasoning.`,
|
|
1455
|
+
];
|
|
1456
|
+
const tmpDir = '/tmp/instar-telegram';
|
|
1457
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
1458
|
+
const ctxPath = path.join(tmpDir, `ctx-${topicId}-${Date.now()}.txt`);
|
|
1459
|
+
fs.writeFileSync(ctxPath, contextLines.join('\n'));
|
|
1460
|
+
const bootstrapMessage = `[telegram:${topicId}] ${text} (IMPORTANT: Read ${ctxPath} for Telegram relay instructions — you MUST relay your response back.)`;
|
|
1461
|
+
ctx.sessionManager.spawnInteractiveSession(bootstrapMessage, topicName, { telegramTopicId: topicId }).then((newSessionName) => {
|
|
1462
|
+
// Update registry on disk
|
|
1463
|
+
try {
|
|
1464
|
+
const reg = fs.existsSync(registryPath) ? JSON.parse(fs.readFileSync(registryPath, 'utf-8')) : { topicToSession: {}, topicToName: {} };
|
|
1465
|
+
reg.topicToSession[String(topicId)] = newSessionName;
|
|
1466
|
+
fs.writeFileSync(registryPath, JSON.stringify(reg, null, 2));
|
|
1467
|
+
}
|
|
1468
|
+
catch { /* non-critical */ }
|
|
1469
|
+
console.log(`[telegram-forward] Spawned "${newSessionName}" for topic ${topicId}`);
|
|
1470
|
+
}).catch((err) => {
|
|
1471
|
+
console.error(`[telegram-forward] Spawn failed:`, err);
|
|
1472
|
+
});
|
|
1473
|
+
res.json({ ok: true, forwarded: true, method: 'spawn', topicName });
|
|
1474
|
+
}
|
|
1426
1475
|
}
|
|
1427
1476
|
else {
|
|
1428
1477
|
res.status(503).json({ error: 'No message routing available' });
|