@xiangyixie/dsh-superagent 0.15.4 → 0.16.0

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/lib/console.js CHANGED
@@ -12,7 +12,7 @@
12
12
  import { randomBytes } from 'node:crypto'
13
13
  import { readFileSync } from 'node:fs'
14
14
  import fsp from 'node:fs/promises'
15
- import { createServer } from 'node:http'
15
+ import { createServer, request } from 'node:http'
16
16
  import path from 'node:path'
17
17
  import { isInside } from './memory.js'
18
18
  import {
@@ -1483,9 +1483,66 @@ export function createConsole({
1483
1483
  })
1484
1484
  }
1485
1485
 
1486
+ /**
1487
+ * Forward one request from the host's own origin to this console.
1488
+ *
1489
+ * The host's main panel renders natively inside the shell page, so it cannot
1490
+ * frame this loopback page: a cross-origin fetch would need CORS headers and
1491
+ * a private-network exemption. Instead the node half exposes a single prefix
1492
+ * route on the host web server (`/superagent/api` → `/api`) and this handler
1493
+ * relays it, adding the bearer token the browser never sees. Streaming
1494
+ * responses (the event feed) are piped, not buffered.
1495
+ *
1496
+ * @param {import('node:http').IncomingMessage} req - request from the host origin.
1497
+ * @param {import('node:http').ServerResponse} res - response back to the host origin.
1498
+ */
1499
+ function proxy(req, res) {
1500
+ if (!boundPort) {
1501
+ res.writeHead(503, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
1502
+ res.end(JSON.stringify({ ok: false, message: 'the console is not listening yet', reason: 'not-ready' }))
1503
+ return
1504
+ }
1505
+ const prefix = '/superagent/api'
1506
+ const incoming = String(req.url ?? '/')
1507
+ const target = incoming.startsWith(prefix) ? `/api${incoming.slice(prefix.length)}` : '/api/console/state'
1508
+ const headers = { ...req.headers, host: `127.0.0.1:${boundPort}`, authorization: `Bearer ${token}` }
1509
+ // The upstream answer is neither compressed nor chunk-framed for us.
1510
+ delete headers['accept-encoding']
1511
+ delete headers['if-none-match']
1512
+ delete headers['if-modified-since']
1513
+ const upstream = request({
1514
+ host: '127.0.0.1',
1515
+ port: boundPort,
1516
+ method: req.method,
1517
+ path: target,
1518
+ headers,
1519
+ }, answer => {
1520
+ // Hop-by-hop headers belong to each leg, not to the relayed message.
1521
+ const carried = { ...answer.headers }
1522
+ delete carried.connection
1523
+ delete carried['keep-alive']
1524
+ delete carried['transfer-encoding']
1525
+ res.writeHead(answer.statusCode ?? 502, carried)
1526
+ res.flushHeaders?.()
1527
+ answer.pipe(res)
1528
+ })
1529
+ upstream.on('error', error => {
1530
+ if (!res.headersSent) {
1531
+ res.writeHead(502, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
1532
+ res.end(JSON.stringify({ ok: false, message: `the console did not answer: ${messageOf(error)}` }))
1533
+ return
1534
+ }
1535
+ res.destroy()
1536
+ })
1537
+ // A closed panel or a finished stream stops the upstream leg too.
1538
+ res.on('close', () => upstream.destroy())
1539
+ req.pipe(upstream)
1540
+ }
1541
+
1486
1542
  return {
1487
1543
  discoveryPath,
1488
1544
  askApproval,
1545
+ proxy,
1489
1546
  /** The conversation this page currently shows; null before one is created. */
1490
1547
  get sessionId() {
1491
1548
  return currentSessionId
package/lib/index.js CHANGED
@@ -4161,6 +4161,14 @@ export function apply(ctx, config) {
4161
4161
  }))
4162
4162
  },
4163
4163
  }), 'dsh-superagent console address')
4164
+ // The main panel renders natively on the host origin, so it reaches
4165
+ // the loopback console through this prefix relay instead of a
4166
+ // cross-origin CDN-less fetch. The token stays server-side.
4167
+ webCtx.effect(() => webCtx.webServer.register({
4168
+ kind: 'prefix',
4169
+ path: '/superagent/api',
4170
+ handler: (req, res) => conversationConsole.proxy(req, res),
4171
+ }), 'dsh-superagent console proxy')
4164
4172
  } catch (error) {
4165
4173
  log(`cannot publish the console address route: ${error?.message ?? error}`)
4166
4174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiangyixie/dsh-superagent",
3
- "version": "0.15.4",
3
+ "version": "0.16.0",
4
4
  "description": "长周期自主执行插件:一个目标拆成可并行的子任务,交付一份合并报告,重启后仍可继续。",
5
5
  "author": "Admire (LittleChenLiya)",
6
6
  "type": "module",