@xiangyixie/dsh-superagent 0.15.4 → 0.16.1
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/CHANGELOG.md +46 -0
- package/README.en.md +3 -3
- package/README.md +3 -3
- package/README.zh.md +3 -3
- package/lib/client.js +967 -171
- package/lib/console.js +83 -1
- package/lib/index.js +22 -0
- package/package.json +1 -1
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 {
|
|
@@ -1239,6 +1239,31 @@ export function createConsole({
|
|
|
1239
1239
|
return
|
|
1240
1240
|
}
|
|
1241
1241
|
|
|
1242
|
+
if (leaf === 'cancel' && parts.length === 3) {
|
|
1243
|
+
if (method !== 'POST') throw new WorkbenchError(405, 'only POST is allowed here', { id: 'method-not-allowed' })
|
|
1244
|
+
const body = await readJson(req)
|
|
1245
|
+
const ensured = await ensureSession()
|
|
1246
|
+
if (typeof body.sessionId === 'string' && body.sessionId && body.sessionId !== ensured.sessionId) {
|
|
1247
|
+
throw new WorkbenchError(409, 'the cancel targets a conversation this console is not showing', {
|
|
1248
|
+
id: 'session-mismatch',
|
|
1249
|
+
hint: 'reload the page; the console keeps one active conversation at a time.',
|
|
1250
|
+
})
|
|
1251
|
+
}
|
|
1252
|
+
if (typeof host.cancelTurn !== 'function') {
|
|
1253
|
+
throw new WorkbenchError(501, 'this host cannot stop a turn', { id: 'cancel-unsupported' })
|
|
1254
|
+
}
|
|
1255
|
+
try {
|
|
1256
|
+
await host.cancelTurn(ensured.sessionId)
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
throw new WorkbenchError(502, `the host refused to stop the turn: ${messageOf(error)}`, {
|
|
1259
|
+
id: 'cancel-failed',
|
|
1260
|
+
})
|
|
1261
|
+
}
|
|
1262
|
+
broadcast({ type: 'canceling', sessionId: ensured.sessionId })
|
|
1263
|
+
send(res, 200, { ok: true, data: { canceling: true } })
|
|
1264
|
+
return
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1242
1267
|
if (leaf === 'message' && parts.length === 3) {
|
|
1243
1268
|
if (method !== 'POST') throw new WorkbenchError(405, 'only POST is allowed here', { id: 'method-not-allowed' })
|
|
1244
1269
|
const body = await readJson(req)
|
|
@@ -1483,9 +1508,66 @@ export function createConsole({
|
|
|
1483
1508
|
})
|
|
1484
1509
|
}
|
|
1485
1510
|
|
|
1511
|
+
/**
|
|
1512
|
+
* Forward one request from the host's own origin to this console.
|
|
1513
|
+
*
|
|
1514
|
+
* The host's main panel renders natively inside the shell page, so it cannot
|
|
1515
|
+
* frame this loopback page: a cross-origin fetch would need CORS headers and
|
|
1516
|
+
* a private-network exemption. Instead the node half exposes a single prefix
|
|
1517
|
+
* route on the host web server (`/superagent/api` → `/api`) and this handler
|
|
1518
|
+
* relays it, adding the bearer token the browser never sees. Streaming
|
|
1519
|
+
* responses (the event feed) are piped, not buffered.
|
|
1520
|
+
*
|
|
1521
|
+
* @param {import('node:http').IncomingMessage} req - request from the host origin.
|
|
1522
|
+
* @param {import('node:http').ServerResponse} res - response back to the host origin.
|
|
1523
|
+
*/
|
|
1524
|
+
function proxy(req, res) {
|
|
1525
|
+
if (!boundPort) {
|
|
1526
|
+
res.writeHead(503, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
1527
|
+
res.end(JSON.stringify({ ok: false, message: 'the console is not listening yet', reason: 'not-ready' }))
|
|
1528
|
+
return
|
|
1529
|
+
}
|
|
1530
|
+
const prefix = '/superagent/api'
|
|
1531
|
+
const incoming = String(req.url ?? '/')
|
|
1532
|
+
const target = incoming.startsWith(prefix) ? `/api${incoming.slice(prefix.length)}` : '/api/console/state'
|
|
1533
|
+
const headers = { ...req.headers, host: `127.0.0.1:${boundPort}`, authorization: `Bearer ${token}` }
|
|
1534
|
+
// The upstream answer is neither compressed nor chunk-framed for us.
|
|
1535
|
+
delete headers['accept-encoding']
|
|
1536
|
+
delete headers['if-none-match']
|
|
1537
|
+
delete headers['if-modified-since']
|
|
1538
|
+
const upstream = request({
|
|
1539
|
+
host: '127.0.0.1',
|
|
1540
|
+
port: boundPort,
|
|
1541
|
+
method: req.method,
|
|
1542
|
+
path: target,
|
|
1543
|
+
headers,
|
|
1544
|
+
}, answer => {
|
|
1545
|
+
// Hop-by-hop headers belong to each leg, not to the relayed message.
|
|
1546
|
+
const carried = { ...answer.headers }
|
|
1547
|
+
delete carried.connection
|
|
1548
|
+
delete carried['keep-alive']
|
|
1549
|
+
delete carried['transfer-encoding']
|
|
1550
|
+
res.writeHead(answer.statusCode ?? 502, carried)
|
|
1551
|
+
res.flushHeaders?.()
|
|
1552
|
+
answer.pipe(res)
|
|
1553
|
+
})
|
|
1554
|
+
upstream.on('error', error => {
|
|
1555
|
+
if (!res.headersSent) {
|
|
1556
|
+
res.writeHead(502, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
1557
|
+
res.end(JSON.stringify({ ok: false, message: `the console did not answer: ${messageOf(error)}` }))
|
|
1558
|
+
return
|
|
1559
|
+
}
|
|
1560
|
+
res.destroy()
|
|
1561
|
+
})
|
|
1562
|
+
// A closed panel or a finished stream stops the upstream leg too.
|
|
1563
|
+
res.on('close', () => upstream.destroy())
|
|
1564
|
+
req.pipe(upstream)
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1486
1567
|
return {
|
|
1487
1568
|
discoveryPath,
|
|
1488
1569
|
askApproval,
|
|
1570
|
+
proxy,
|
|
1489
1571
|
/** The conversation this page currently shows; null before one is created. */
|
|
1490
1572
|
get sessionId() {
|
|
1491
1573
|
return currentSessionId
|
package/lib/index.js
CHANGED
|
@@ -3807,6 +3807,20 @@ export function apply(ctx, config) {
|
|
|
3807
3807
|
return { accepted: true }
|
|
3808
3808
|
},
|
|
3809
3809
|
|
|
3810
|
+
/**
|
|
3811
|
+
* Stop the turn this conversation is currently running. The session
|
|
3812
|
+
* controller owns admission, so an idle conversation simply acknowledges
|
|
3813
|
+
* that there was nothing left to cancel.
|
|
3814
|
+
*/
|
|
3815
|
+
async cancelTurn(sessionId) {
|
|
3816
|
+
await ready
|
|
3817
|
+
const session = getSessionController()
|
|
3818
|
+
if (!session) throw new Error('sessionController is not mounted')
|
|
3819
|
+
if (typeof session.cancel !== 'function') throw new Error('this session controller cannot stop a turn')
|
|
3820
|
+
const value = await session.cancel({ sessionId })
|
|
3821
|
+
return { accepted: value?.accepted !== false }
|
|
3822
|
+
},
|
|
3823
|
+
|
|
3810
3824
|
async listRuns(sessionId) {
|
|
3811
3825
|
await ready
|
|
3812
3826
|
if (stateError) throw stateError
|
|
@@ -4161,6 +4175,14 @@ export function apply(ctx, config) {
|
|
|
4161
4175
|
}))
|
|
4162
4176
|
},
|
|
4163
4177
|
}), 'dsh-superagent console address')
|
|
4178
|
+
// The main panel renders natively on the host origin, so it reaches
|
|
4179
|
+
// the loopback console through this prefix relay instead of a
|
|
4180
|
+
// cross-origin CDN-less fetch. The token stays server-side.
|
|
4181
|
+
webCtx.effect(() => webCtx.webServer.register({
|
|
4182
|
+
kind: 'prefix',
|
|
4183
|
+
path: '/superagent/api',
|
|
4184
|
+
handler: (req, res) => conversationConsole.proxy(req, res),
|
|
4185
|
+
}), 'dsh-superagent console proxy')
|
|
4164
4186
|
} catch (error) {
|
|
4165
4187
|
log(`cannot publish the console address route: ${error?.message ?? error}`)
|
|
4166
4188
|
}
|