@xiangyixie/dsh-superagent 0.19.0 → 0.22.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/lib/console.js CHANGED
@@ -260,6 +260,7 @@ export function createConsole({
260
260
  let unsubscribe = null
261
261
  let runTimer = null
262
262
  let lastRuns = null
263
+ let lastGoals = []
263
264
  let lastRunsJson = ''
264
265
  let seq = 0
265
266
  // The host's follow opening is not a reliable transcript: after a turn its
@@ -621,6 +622,7 @@ export function createConsole({
621
622
  }
622
623
  unsubscribe = null
623
624
  lastRuns = null
625
+ lastGoals = []
624
626
  lastRunsJson = ''
625
627
  }
626
628
 
@@ -628,8 +630,11 @@ export function createConsole({
628
630
  const sessionId = currentSessionId
629
631
  if (!sessionId) return []
630
632
  let runs
633
+ let goals
631
634
  try {
632
- runs = await host.listRuns(sessionId)
635
+ const rows = await Promise.all([host.listRuns(sessionId), host.listGoals?.(sessionId) ?? []])
636
+ runs = rows[0]
637
+ goals = rows[1]
633
638
  } catch (error) {
634
639
  log(`console: cannot list runs for ${sessionId}: ${messageOf(error)}`)
635
640
  if (force) {
@@ -640,11 +645,13 @@ export function createConsole({
640
645
  return []
641
646
  }
642
647
  const rows = Array.isArray(runs) ? runs : []
643
- const json = JSON.stringify(rows)
648
+ if (currentSessionId !== sessionId) return []
649
+ lastGoals = Array.isArray(goals) ? goals : []
650
+ const json = JSON.stringify([rows, lastGoals])
644
651
  lastRuns = rows
645
652
  if (force || json !== lastRunsJson) {
646
653
  lastRunsJson = json
647
- broadcast({ type: 'runs', sessionId, runs: rows })
654
+ broadcast({ type: 'runs', sessionId, runs: rows, goals: lastGoals })
648
655
  }
649
656
  return rows
650
657
  }
@@ -694,7 +701,7 @@ export function createConsole({
694
701
  refreshRuns({ force: true }).catch(() => {})
695
702
  }
696
703
 
697
- async function createSession() {
704
+ async function createSession(options) {
698
705
  const caps = capabilities()
699
706
  if (!caps.chat) {
700
707
  throw new WorkbenchError(503, caps.reason ?? 'the host exposes no conversation session API', {
@@ -704,7 +711,7 @@ export function createConsole({
704
711
  }
705
712
  let created
706
713
  try {
707
- created = await host.createSession()
714
+ created = await host.createSession(options)
708
715
  } catch (error) {
709
716
  throw new WorkbenchError(502, `cannot create a conversation session: ${messageOf(error)}`, {
710
717
  id: 'session-create-failed',
@@ -791,11 +798,13 @@ export function createConsole({
791
798
 
792
799
  async function state() {
793
800
  const caps = capabilities()
801
+ if (currentSessionId && !runTimer) await refreshRuns()
794
802
  return {
795
803
  mode: caps.chat ? 'chat' : 'unavailable',
796
804
  capabilities: caps,
797
805
  sessionId: currentSessionId,
798
806
  runs: currentSessionId ? lastRuns ?? [] : [],
807
+ goals: currentSessionId ? lastGoals : [],
799
808
  approvals: [...approvals.values()].map(entry => entry.payload),
800
809
  readNoticeIds: [...noticeRead],
801
810
  }
@@ -838,12 +847,6 @@ export function createConsole({
838
847
  if (clients.size >= MAX_CLIENTS) {
839
848
  throw new WorkbenchError(503, 'too many console streams are already open', { id: 'too-many-streams' })
840
849
  }
841
- if (!currentSessionId) {
842
- throw new WorkbenchError(409, 'create a conversation session before opening the event stream', {
843
- id: 'no-session',
844
- hint: 'POST /api/console/session first.',
845
- })
846
- }
847
850
  res.writeHead(200, {
848
851
  'content-type': 'text/event-stream; charset=utf-8',
849
852
  'cache-control': 'no-cache, no-transform',
@@ -870,10 +873,12 @@ export function createConsole({
870
873
  writeFrame(res, { ...boundedSnapshot(cached), seq: nextSeq(), at: new Date().toISOString() })
871
874
  }
872
875
  if (lastRuns) {
873
- writeFrame(res, { type: 'runs', seq: nextSeq(), at: new Date().toISOString(), sessionId: currentSessionId, runs: lastRuns })
876
+ writeFrame(res, { type: 'runs', seq: nextSeq(), at: new Date().toISOString(), sessionId: currentSessionId, runs: lastRuns, goals: lastGoals })
877
+ }
878
+ if (currentSessionId) {
879
+ attach(currentSessionId)
880
+ if (!cached) seedFromHost(currentSessionId).catch(() => {})
874
881
  }
875
- attach(currentSessionId)
876
- if (!cached) seedFromHost(currentSessionId).catch(() => {})
877
882
  const heartbeat = setInterval(() => {
878
883
  if (res.destroyed || res.writableEnded) return
879
884
  try {
@@ -948,12 +953,21 @@ export function createConsole({
948
953
  if (!sessionId) throw new WorkbenchError(409, 'there is no conversation yet', { id: 'no-session' })
949
954
  const raw = (parsed.searchParams.get('path') || '').trim()
950
955
  if (!raw) throw new WorkbenchError(400, 'path is required', { id: 'missing-path' })
951
- const target = path.resolve(raw)
956
+ const workspace = !path.isAbsolute(raw) ? await host.sessionWorkspace?.(sessionId) : null
957
+ const target = path.resolve(workspace ?? process.cwd(), raw)
958
+ if (workspace) {
959
+ const relative = path.relative(workspace, target)
960
+ if (path.isAbsolute(relative) || relative === '..' || relative.startsWith(`..${path.sep}`)) {
961
+ throw new WorkbenchError(403, 'artifact path is outside this project', { id: 'artifact-outside-project' })
962
+ }
963
+ }
952
964
  const runId = (parsed.searchParams.get('runId') || '').trim()
953
965
  let reports = []
966
+ let verified = []
954
967
  if (runId && typeof host.readRun === 'function') {
955
968
  const run = await host.readRun(sessionId, runId)
956
969
  reports = typeof run?.report === 'string' ? [run.report] : []
970
+ verified = run?.artifacts ?? []
957
971
  } else if (typeof host.listRuns === 'function') {
958
972
  const rows = await host.listRuns(sessionId)
959
973
  reports = (Array.isArray(rows) ? rows : [])
@@ -961,7 +975,8 @@ export function createConsole({
961
975
  .filter(report => typeof report === 'string')
962
976
  }
963
977
  const variants = [raw, target, target.replace(/\\/g, '/'), target.replace(/\//g, '\\')]
964
- const listed = reports.some(report => variants.some(value => value && report.includes(value)))
978
+ const listed = verified.some(item => variants.includes(item.href))
979
+ || reports.some(report => variants.some(value => value && report.includes(value)))
965
980
  if (!listed) {
966
981
  throw new WorkbenchError(403, 'this path is not named in any run report', {
967
982
  id: 'artifact-not-listed',
@@ -1199,6 +1214,56 @@ export function createConsole({
1199
1214
  return
1200
1215
  }
1201
1216
 
1217
+ if (leaf === 'workbench' && parts.length === 3 && typeof host.workbench === 'function') {
1218
+ if (method === 'POST' && typeof host.nameWorkbench === 'function') {
1219
+ const body = await readJson(req)
1220
+ const name = typeof body.name === 'string' ? body.name.trim() : ''
1221
+ if (!name || name.length > 32 || /[\r\n]/.test(name)) {
1222
+ throw new WorkbenchError(400, '名称须在 1 到 32 个字之间', { id: 'invalid-assistant-name' })
1223
+ }
1224
+ await host.nameWorkbench(name)
1225
+ } else if (method !== 'GET') {
1226
+ throw new WorkbenchError(405, 'method not allowed', { id: 'method-not-allowed' })
1227
+ }
1228
+ const sessionId = currentSessionId
1229
+ send(res, 200, { ok: true, data: { ...(await host.workbench(sessionId)), sessionId } })
1230
+ return
1231
+ }
1232
+
1233
+ if (leaf === 'projects' && parts[3] === 'pick' && parts.length === 4 && method === 'POST' && host.pickProject) {
1234
+ const controller = new AbortController()
1235
+ const abort = () => controller.abort()
1236
+ res.once('close', abort)
1237
+ try {
1238
+ const cwd = await host.pickProject(controller.signal)
1239
+ if (!controller.signal.aborted) send(res, 200, { ok: true, data: { cwd } })
1240
+ } finally {
1241
+ res.off('close', abort)
1242
+ }
1243
+ return
1244
+ }
1245
+
1246
+ if (leaf === 'goals' && method === 'POST' && (parts.length === 3 || parts.length === 5)) {
1247
+ const body = await readJson(req)
1248
+ const creating = parts.length === 3
1249
+ const action = parts[4]
1250
+ if (creating ? !host.createGoal : !host.actOnGoal || !['answer', 'pause', 'resume', 'cancel', 'retry_phase'].includes(action)) {
1251
+ throw new WorkbenchError(404, 'goal action unavailable', { id: 'goal-action-unavailable' })
1252
+ }
1253
+ const text = String(body.text ?? '').trim()
1254
+ if (creating && !text) throw new WorkbenchError(400, '请输入要委托的目标', { id: 'goal-required' })
1255
+ const sessionId = creating ? (await ensureSession()).sessionId : currentSessionId
1256
+ if (!sessionId || (body.sessionId && body.sessionId !== sessionId)) {
1257
+ throw new WorkbenchError(409, '会话已切换,请回到目标原来的会话', { id: 'session-mismatch' })
1258
+ }
1259
+ const goal = creating ? await host.createGoal(sessionId, text)
1260
+ : await host.actOnGoal(sessionId, decodeURIComponent(parts[3]), action, body)
1261
+ if (!goal) throw new WorkbenchError(404, '本会话中没有这条目标', { id: 'unknown-goal' })
1262
+ await refreshRuns().catch(() => {})
1263
+ send(res, 200, { ok: true, data: { goal, sessionId } })
1264
+ return
1265
+ }
1266
+
1202
1267
  // Notices are derived from durable runs on every load; only the ids a
1203
1268
  // person has already read need remembering.
1204
1269
  if (leaf === 'notices' && parts.length === 4 && parts[3] === 'read') {
@@ -1251,7 +1316,8 @@ export function createConsole({
1251
1316
 
1252
1317
  if (leaf === 'session' && parts.length === 3) {
1253
1318
  if (method !== 'POST') throw new WorkbenchError(405, 'only POST is allowed here', { id: 'method-not-allowed' })
1254
- const created = await createSession()
1319
+ const body = await readJson(req)
1320
+ const created = await createSession({ cwd: body.cwd })
1255
1321
  send(res, 200, { ok: true, data: created })
1256
1322
  return
1257
1323
  }
@@ -1464,6 +1530,31 @@ export function createConsole({
1464
1530
  return
1465
1531
  }
1466
1532
 
1533
+ if (leaf === 'runs' && parts.length === 6 && parts[4] === 'worker' && method === 'GET' && host.readWorkerSession) {
1534
+ const record = currentSessionId && await host.readWorkerSession(currentSessionId, decodeURIComponent(parts[3]), decodeURIComponent(parts[5]))
1535
+ if (!record) throw new WorkbenchError(404, '本会话中没有这条工作线', { id: 'unknown-worker' })
1536
+ send(res, 200, { ok: true, data: record })
1537
+ return
1538
+ }
1539
+
1540
+ if (leaf === 'runs' && parts.length === 5 && parts[4] === 'instruction' && method === 'POST' && host.instructRun) {
1541
+ const body = await readJson(req)
1542
+ const sessionId = currentSessionId
1543
+ if (!sessionId || body.sessionId !== sessionId) {
1544
+ throw new WorkbenchError(409, '会话已切换,请回到任务原来的会话', { id: 'session-mismatch' })
1545
+ }
1546
+ const text = String(body.text ?? '').trim()
1547
+ const mode = body.mode
1548
+ if (!text || (mode !== 'followup' && (mode !== 'inject' || !Number.isInteger(body.subtaskIndex)))) {
1549
+ throw new WorkbenchError(400, '请输入指令并选择任务或工作线', { id: 'invalid-instruction' })
1550
+ }
1551
+ const receipt = await host.instructRun(sessionId, decodeURIComponent(parts[3]), { text, mode, subtaskIndex: body.subtaskIndex })
1552
+ if (!receipt) throw new WorkbenchError(404, '本会话中没有这条任务', { id: 'unknown-run' })
1553
+ await refreshRuns().catch(() => {})
1554
+ send(res, 200, { ok: true, data: receipt })
1555
+ return
1556
+ }
1557
+
1467
1558
  if (leaf === 'runs' && parts.length === 5 && parts[4] === 'cancel') {
1468
1559
  if (method !== 'POST') throw new WorkbenchError(405, 'only POST is allowed here', { id: 'method-not-allowed' })
1469
1560
  if (!currentSessionId) throw new WorkbenchError(409, 'no conversation is open', { id: 'no-session' })
package/lib/goal.js CHANGED
@@ -393,7 +393,7 @@ export function verifierPrompt(run, round, checks) {
393
393
  ...judged.map(check => `- [${check.id}] ${check.text}`),
394
394
  '',
395
395
  'Rules:',
396
- '- Mark a condition met only when the report contains concrete evidence for it (a path, a command result, a number).',
396
+ '- Mark a condition met only when the report contains concrete evidence (the requested text itself, an artifact path, or a check result). A phrase quoted only from the goal or instructions is not a deliverable.',
397
397
  '- "achieved" is true only when every condition listed above is met.',
398
398
  '- "next" must say what to change in the next round: the file, the approach, or the missing evidence. Never write',
399
399
  ' "try again" or "improve it"; that is what the next round must not hear.',