@xiangyixie/dsh-superagent 0.14.7 → 0.14.9
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 +31 -0
- package/README.en.md +4 -4
- package/README.md +4 -4
- package/README.zh.md +4 -4
- package/lib/console.js +383 -9
- package/lib/index.js +266 -0
- package/lib/workbench.js +11 -2
- package/package.json +1 -1
- package/public/console.html +970 -65
package/lib/index.js
CHANGED
|
@@ -3402,6 +3402,20 @@ export function apply(ctx, config) {
|
|
|
3402
3402
|
const consoleRoot = cfg.consoleRoot ? path.resolve(cfg.consoleRoot) : workbenchRoot
|
|
3403
3403
|
const CONSOLE_SPACE_ID = 'superagent-console-space'
|
|
3404
3404
|
|
|
3405
|
+
function getSessionController() {
|
|
3406
|
+
if (typeof ctx.get === 'function') {
|
|
3407
|
+
const service = ctx.get('sessionController')
|
|
3408
|
+
if (service) return service
|
|
3409
|
+
}
|
|
3410
|
+
// Direct property is topology-sensitive; this is a fallback for hosts
|
|
3411
|
+
// that expose the service without the registry.
|
|
3412
|
+
try {
|
|
3413
|
+
return ctx.sessionController
|
|
3414
|
+
} catch {
|
|
3415
|
+
return undefined
|
|
3416
|
+
}
|
|
3417
|
+
}
|
|
3418
|
+
|
|
3405
3419
|
function consoleCapabilities() {
|
|
3406
3420
|
const session = getSessionController()
|
|
3407
3421
|
if (!session) {
|
|
@@ -3462,6 +3476,60 @@ export function apply(ctx, config) {
|
|
|
3462
3476
|
return null
|
|
3463
3477
|
}
|
|
3464
3478
|
|
|
3479
|
+
/**
|
|
3480
|
+
* The tool a running worker is spending its time on, read from that worker's
|
|
3481
|
+
* own event log: the last unanswered `tool/call`, or the most recent one
|
|
3482
|
+
* when every call already came back (a worker between calls). Null when the
|
|
3483
|
+
* log cannot be read; the page then shows no tool rather than a guess.
|
|
3484
|
+
*/
|
|
3485
|
+
function runningToolName(childId) {
|
|
3486
|
+
if (typeof childId !== 'string' || !childId) return null
|
|
3487
|
+
const holder = (() => {
|
|
3488
|
+
const sessions = consoleService('sessions')
|
|
3489
|
+
if (sessions && typeof sessions.get === 'function') {
|
|
3490
|
+
const session = sessions.get(childId)
|
|
3491
|
+
if (session) return session
|
|
3492
|
+
}
|
|
3493
|
+
const agents = getAgents()
|
|
3494
|
+
if (agents && typeof agents.get === 'function') {
|
|
3495
|
+
const agent = agents.get(childId)
|
|
3496
|
+
return agent?.session ?? agent ?? null
|
|
3497
|
+
}
|
|
3498
|
+
return null
|
|
3499
|
+
})()
|
|
3500
|
+
if (!holder || typeof holder.snapshotEvents !== 'function') return null
|
|
3501
|
+
let events
|
|
3502
|
+
try {
|
|
3503
|
+
events = holder.snapshotEvents()
|
|
3504
|
+
} catch {
|
|
3505
|
+
return null
|
|
3506
|
+
}
|
|
3507
|
+
if (!Array.isArray(events)) return null
|
|
3508
|
+
const answered = new Set()
|
|
3509
|
+
// The newest unanswered call is what the lane is running now. When every
|
|
3510
|
+
// call already came back, the most recent one still tells the user what
|
|
3511
|
+
// that lane is spending its time on.
|
|
3512
|
+
let latest = null
|
|
3513
|
+
for (let index = events.length - 1, scanned = 0; index >= 0 && scanned < 80; index -= 1, scanned += 1) {
|
|
3514
|
+
const event = events[index]
|
|
3515
|
+
const data = event?.data
|
|
3516
|
+
if (!data || typeof data !== 'object') continue
|
|
3517
|
+
const callId = data.callId || data.message?.toolCallId || data.message?.source?.callId
|
|
3518
|
+
if (event.type === 'tool/result') {
|
|
3519
|
+
if (callId) answered.add(callId)
|
|
3520
|
+
continue
|
|
3521
|
+
}
|
|
3522
|
+
if (event.type !== 'tool/call' || !callId) continue
|
|
3523
|
+
const name = typeof data.name === 'string' && data.name ? data.name : null
|
|
3524
|
+
if (answered.has(callId)) {
|
|
3525
|
+
if (!latest) latest = name
|
|
3526
|
+
continue
|
|
3527
|
+
}
|
|
3528
|
+
return name
|
|
3529
|
+
}
|
|
3530
|
+
return latest
|
|
3531
|
+
}
|
|
3532
|
+
|
|
3465
3533
|
function runView(run, { detail = false } = {}) {
|
|
3466
3534
|
const usage = run.usage && typeof run.usage === 'object' ? run.usage : null
|
|
3467
3535
|
const cost = usage ? computeCost(usage, cfg.pricing) : null
|
|
@@ -3481,6 +3549,11 @@ export function apply(ctx, config) {
|
|
|
3481
3549
|
title: subtask.title ?? null,
|
|
3482
3550
|
status: subtask.status ?? null,
|
|
3483
3551
|
childId: subtask.childId ?? null,
|
|
3552
|
+
// Timing is what lets the page show which line is slow; without it
|
|
3553
|
+
// every lane looks equally busy.
|
|
3554
|
+
startedAt: subtask.startedAt ?? null,
|
|
3555
|
+
finishedAt: subtask.finishedAt ?? null,
|
|
3556
|
+
tool: subtask.status === 'running' ? runningToolName(subtask.childId) : null,
|
|
3484
3557
|
}))
|
|
3485
3558
|
: []
|
|
3486
3559
|
const report = typeof run.report === 'string' ? run.report : null
|
|
@@ -3517,6 +3590,91 @@ export function apply(ctx, config) {
|
|
|
3517
3590
|
}
|
|
3518
3591
|
}
|
|
3519
3592
|
|
|
3593
|
+
/**
|
|
3594
|
+
* Count the files and pages a run report names, using the same rule the page
|
|
3595
|
+
* uses for artifact chips. The history row only needs the number.
|
|
3596
|
+
*/
|
|
3597
|
+
function countReportArtifacts(report) {
|
|
3598
|
+
const text = typeof report === 'string' ? report : ''
|
|
3599
|
+
if (!text) return 0
|
|
3600
|
+
const seen = new Set()
|
|
3601
|
+
const push = value => {
|
|
3602
|
+
const href = value.replace(/[.,;:,。;:]+$/, '')
|
|
3603
|
+
if (href) seen.add(href)
|
|
3604
|
+
}
|
|
3605
|
+
for (const match of text.matchAll(/https?:\/\/[^\s"'`<>)\]}|]+/g)) push(match[0])
|
|
3606
|
+
for (const match of text.matchAll(/(?:[A-Za-z]:[\\/]|\.{1,2}[\\/]|~[\\/])[^\s"'`<>|]+?\.(?:html?|md|pdf|docx?|xlsx?|csv|tsv|json|ya?ml|txt|log|png|jpe?g|gif|webp|svg|zip|pptx?|py|js|mjs|css)\b/gi)) {
|
|
3607
|
+
push(match[0])
|
|
3608
|
+
}
|
|
3609
|
+
return Math.min(seen.size, 12)
|
|
3610
|
+
}
|
|
3611
|
+
|
|
3612
|
+
const firstTextCache = new Map()
|
|
3613
|
+
|
|
3614
|
+
/**
|
|
3615
|
+
* Session records as the page consumes them (`{type:'event', event}`).
|
|
3616
|
+
* Pages back from the newest until the first turn is reached, bounded so a
|
|
3617
|
+
* very long conversation cannot stall the drawer. Null when unreadable.
|
|
3618
|
+
*/
|
|
3619
|
+
async function readSessionRecords(sessionId) {
|
|
3620
|
+
const controller = getSessionController()
|
|
3621
|
+
if (!controller || typeof controller.events !== 'function') return null
|
|
3622
|
+
const address = { kind: 'session', sessionId }
|
|
3623
|
+
const { signal } = new AbortController()
|
|
3624
|
+
try {
|
|
3625
|
+
const newest = await controller.events({ address, maxMessages: 200 }, signal)
|
|
3626
|
+
if (!newest || !Array.isArray(newest.records)) return null
|
|
3627
|
+
let records = newest.records.slice()
|
|
3628
|
+
let page = newest
|
|
3629
|
+
for (let guard = 0; page.hasMore === true && guard < 8; guard += 1) {
|
|
3630
|
+
const oldest = records[0]?.event?.seq
|
|
3631
|
+
if (!Number.isSafeInteger(oldest) || oldest <= 0) break
|
|
3632
|
+
const older = await controller.events({ address, maxMessages: 200, beforeSeq: oldest }, signal)
|
|
3633
|
+
const batch = Array.isArray(older?.records) ? older.records : []
|
|
3634
|
+
if (batch.length === 0) break
|
|
3635
|
+
records = [...batch, ...records]
|
|
3636
|
+
page = older
|
|
3637
|
+
}
|
|
3638
|
+
return records
|
|
3639
|
+
} catch (error) {
|
|
3640
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
3641
|
+
const frame = error instanceof Error && error.stack ? String(error.stack).split('\n')[1]?.trim() ?? '' : ''
|
|
3642
|
+
log(`session read error: ${message}`)
|
|
3643
|
+
log(`session read at: ${frame.slice(0, 100)}`)
|
|
3644
|
+
return null
|
|
3645
|
+
}
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
/**
|
|
3649
|
+
* The first thing the user said in a conversation, read from that session's
|
|
3650
|
+
* own log. Cached per `updatedAt` so opening the drawer does not re-read
|
|
3651
|
+
* every transcript, and null when the log cannot be read.
|
|
3652
|
+
*/
|
|
3653
|
+
async function firstUserText(sessionId, marker) {
|
|
3654
|
+
const key = `${sessionId}:${marker ?? ''}`
|
|
3655
|
+
if (firstTextCache.has(key)) return firstTextCache.get(key)
|
|
3656
|
+
const records = await readSessionRecords(sessionId)
|
|
3657
|
+
let text = null
|
|
3658
|
+
for (const record of records ?? []) {
|
|
3659
|
+
const event = record?.event
|
|
3660
|
+
if (event?.type !== 'user/message') continue
|
|
3661
|
+
if (event.data?.source?.kind !== 'user') continue
|
|
3662
|
+
const parts = Array.isArray(event.data?.content) ? event.data.content : []
|
|
3663
|
+
const joined = parts
|
|
3664
|
+
.filter(part => part?.type === 'text' && typeof part.text === 'string')
|
|
3665
|
+
.map(part => part.text)
|
|
3666
|
+
.join(' ')
|
|
3667
|
+
.trim()
|
|
3668
|
+
if (joined) {
|
|
3669
|
+
text = joined.slice(0, 140)
|
|
3670
|
+
break
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
if (text === null && records === null) log(`[superagent-console] first text unavailable for ${sessionId}`)
|
|
3674
|
+
firstTextCache.set(key, text)
|
|
3675
|
+
return text
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3520
3678
|
const consoleHost = {
|
|
3521
3679
|
capabilities: consoleCapabilities,
|
|
3522
3680
|
|
|
@@ -3563,6 +3721,28 @@ export function apply(ctx, config) {
|
|
|
3563
3721
|
return { sessionId: created.sessionId, preset: created.preset ?? cfg.consolePreset ?? null }
|
|
3564
3722
|
},
|
|
3565
3723
|
|
|
3724
|
+
/**
|
|
3725
|
+
* The directory a conversation writes into. A dropped file is stored
|
|
3726
|
+
* under it, so the answer comes from the session record, never from the
|
|
3727
|
+
* page: a page that could name its own directory could name any one.
|
|
3728
|
+
*/
|
|
3729
|
+
async sessionWorkspace(sessionId) {
|
|
3730
|
+
const controller = getSessionController()
|
|
3731
|
+
if (!controller || typeof controller.list !== 'function') return consoleRoot
|
|
3732
|
+
try {
|
|
3733
|
+
const listed = await controller.list()
|
|
3734
|
+
const items = Array.isArray(listed) ? listed : Array.isArray(listed?.items) ? listed.items : []
|
|
3735
|
+
const hit = items.find(item => item
|
|
3736
|
+
&& item.sessionId === sessionId
|
|
3737
|
+
&& item.parentSessionId === undefined
|
|
3738
|
+
&& typeof item.cwd === 'string'
|
|
3739
|
+
&& item.cwd)
|
|
3740
|
+
return hit ? path.resolve(hit.cwd) : consoleRoot
|
|
3741
|
+
} catch {
|
|
3742
|
+
return consoleRoot
|
|
3743
|
+
}
|
|
3744
|
+
},
|
|
3745
|
+
|
|
3566
3746
|
/**
|
|
3567
3747
|
* The tools this conversation actually has, read from the live agent's
|
|
3568
3748
|
* scope rather than a list kept here. Resolving a blank session starts
|
|
@@ -3625,6 +3805,92 @@ export function apply(ctx, config) {
|
|
|
3625
3805
|
return runView(run, { detail: true })
|
|
3626
3806
|
},
|
|
3627
3807
|
|
|
3808
|
+
/**
|
|
3809
|
+
* Stop one run through the same cancel path the `superagent_cancel` tool
|
|
3810
|
+
* uses. Ownership is checked against the conversation first, so a page
|
|
3811
|
+
* can only stop runs it is showing.
|
|
3812
|
+
*/
|
|
3813
|
+
async cancelRun(sessionId, runId) {
|
|
3814
|
+
await ready
|
|
3815
|
+
if (stateError) throw stateError
|
|
3816
|
+
const id = typeof runId === 'string' ? runId.trim() : ''
|
|
3817
|
+
if (!id) return null
|
|
3818
|
+
const run = await store.read(id)
|
|
3819
|
+
if (!run || run.parentSessionId !== sessionId) return null
|
|
3820
|
+
const cancelled = await orchestrator.cancel(id)
|
|
3821
|
+
return {
|
|
3822
|
+
runId: id,
|
|
3823
|
+
status: cancelled?.status ?? null,
|
|
3824
|
+
cancelPending: cancelled?.cancelPending === true,
|
|
3825
|
+
}
|
|
3826
|
+
},
|
|
3827
|
+
|
|
3828
|
+
/** The console's own conversations, newest first, with what they produced. */
|
|
3829
|
+
async listSessions() {
|
|
3830
|
+
const controller = getSessionController()
|
|
3831
|
+
if (!controller || typeof controller.list !== 'function') return []
|
|
3832
|
+
await ready
|
|
3833
|
+
if (stateError) throw stateError
|
|
3834
|
+
// The registered controller answers `{ items }`; older shapes are a bare array.
|
|
3835
|
+
const listed = await controller.list()
|
|
3836
|
+
const items = Array.isArray(listed) ? listed : Array.isArray(listed?.items) ? listed.items : []
|
|
3837
|
+
const runs = await store.list().catch(() => [])
|
|
3838
|
+
const counts = new Map()
|
|
3839
|
+
for (const run of runs) {
|
|
3840
|
+
if (!run || typeof run.parentSessionId !== 'string') continue
|
|
3841
|
+
const entry = counts.get(run.parentSessionId) ?? { runs: 0, artifacts: 0 }
|
|
3842
|
+
entry.runs += 1
|
|
3843
|
+
entry.artifacts += countReportArtifacts(run.report)
|
|
3844
|
+
counts.set(run.parentSessionId, entry)
|
|
3845
|
+
}
|
|
3846
|
+
const own = items
|
|
3847
|
+
.filter(item => item && typeof item.sessionId === 'string')
|
|
3848
|
+
.filter(item => item.parentSessionId === undefined)
|
|
3849
|
+
.filter(item => typeof item.cwd === 'string' && path.resolve(item.cwd) === consoleRoot)
|
|
3850
|
+
.sort((left, right) => (right.updatedAt ?? 0) - (left.updatedAt ?? 0))
|
|
3851
|
+
.slice(0, 12)
|
|
3852
|
+
return Promise.all(own.map(async item => ({
|
|
3853
|
+
sessionId: item.sessionId,
|
|
3854
|
+
updatedAt: item.updatedAt ?? null,
|
|
3855
|
+
running: item.running === true,
|
|
3856
|
+
blank: item.blank === true,
|
|
3857
|
+
runs: counts.get(item.sessionId)?.runs ?? 0,
|
|
3858
|
+
artifacts: counts.get(item.sessionId)?.artifacts ?? 0,
|
|
3859
|
+
first: item.blank ? null : await firstUserText(item.sessionId, item.updatedAt),
|
|
3860
|
+
title: item.projections?.values?.sessionTitle ?? item.projections?.values?.title ?? null,
|
|
3861
|
+
})))
|
|
3862
|
+
},
|
|
3863
|
+
|
|
3864
|
+
/**
|
|
3865
|
+
* One conversation's log, newest page first and then walked back to the
|
|
3866
|
+
* first turn. The controller reads cold sessions too, so a conversation
|
|
3867
|
+
* the console has never shown can still be reopened.
|
|
3868
|
+
*/
|
|
3869
|
+
async readSessionEvents(sessionId) {
|
|
3870
|
+
const records = await readSessionRecords(sessionId)
|
|
3871
|
+
return records === null ? null : records.slice(-600)
|
|
3872
|
+
},
|
|
3873
|
+
|
|
3874
|
+
/**
|
|
3875
|
+
* Resolve a conversation this console may show. Only sessions created in
|
|
3876
|
+
* the console space qualify, so the page cannot wander into the user's own
|
|
3877
|
+
* sessions by guessing an id.
|
|
3878
|
+
*/
|
|
3879
|
+
async openSession(sessionId) {
|
|
3880
|
+
const id = typeof sessionId === 'string' ? sessionId.trim() : ''
|
|
3881
|
+
if (!id) return null
|
|
3882
|
+
const controller = getSessionController()
|
|
3883
|
+
if (!controller || typeof controller.list !== 'function') return null
|
|
3884
|
+
const listed = await controller.list()
|
|
3885
|
+
const items = Array.isArray(listed) ? listed : Array.isArray(listed?.items) ? listed.items : []
|
|
3886
|
+
const hit = items.find(item => item
|
|
3887
|
+
&& item.sessionId === id
|
|
3888
|
+
&& item.parentSessionId === undefined
|
|
3889
|
+
&& typeof item.cwd === 'string'
|
|
3890
|
+
&& path.resolve(item.cwd) === consoleRoot)
|
|
3891
|
+
return hit ? { sessionId: id, updatedAt: hit.updatedAt ?? null } : null
|
|
3892
|
+
},
|
|
3893
|
+
|
|
3628
3894
|
async answer({ sessionId, runId, attentionId, answer }) {
|
|
3629
3895
|
let agent = conversationAgent(sessionId)
|
|
3630
3896
|
if (!agent) {
|
package/lib/workbench.js
CHANGED
|
@@ -152,8 +152,17 @@ function readBody(req, limit = BODY_LIMIT) {
|
|
|
152
152
|
})
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Parse a JSON request body.
|
|
157
|
+
*
|
|
158
|
+
* @param {import('node:http').IncomingMessage} req - request to read.
|
|
159
|
+
* @param {object} [options] - body cap for routes that carry a payload (an
|
|
160
|
+
* uploaded file is base64, so it needs more room than editor text).
|
|
161
|
+
* @param {number} [options.limit] - maximum body bytes; defaults to `BODY_LIMIT`.
|
|
162
|
+
* @returns {Promise<object>} the parsed object.
|
|
163
|
+
*/
|
|
164
|
+
export async function readJson(req, { limit = BODY_LIMIT } = {}) {
|
|
165
|
+
const text = (await readBody(req, limit)).trim()
|
|
157
166
|
if (!text) return {}
|
|
158
167
|
let parsed
|
|
159
168
|
try {
|