dsh-aurorix-agentbook 1.3.0 → 1.4.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.
Files changed (2) hide show
  1. package/lib/host.js +85 -1
  2. package/package.json +1 -1
package/lib/host.js CHANGED
@@ -5,6 +5,7 @@
5
5
  //
6
6
  // 每个服务都用 ctx.inject 软依赖:宿主没有 systemPrompt(或 tools、commands)
7
7
  // 时,那一块就不装,其余部分照常工作,整个插件不会因此起不来。
8
+ import { spawn } from 'node:child_process'
8
9
  import {
9
10
  buildSnapshot,
10
11
  createAccessStore,
@@ -370,6 +371,89 @@ export function attachHost(ctx, deps) {
370
371
  },
371
372
  })
372
373
 
374
+ // Shell:整理记忆时得能跑 grep、管道、脚本,只给单个命令不够用。
375
+ const RAW_LIMIT = 200 * 1024
376
+ const sh = scope.tools.register({
377
+ name: 'aurorix_sh',
378
+ description:
379
+ '在本机 shell 里跑一条命令(bash -c)。能用管道、重定向、grep、sed、awk、jq、python 脚本——整理记忆时搜关键字、批量替换、统计都靠它。\n' +
380
+ 'abook 已经在 PATH 里,直接打 `abook ...` 就行,不用带路径。\n' +
381
+ '**唯一要记的坑**:abook 的输出不要接 head / tail / cut -c / sed -n 范围——它检测到截断会把整份结果丢弃。要控篇幅用 --limit / --page / --json;要加工就先重定向到文件,再用别的命令读那个文件。\n' +
382
+ '输出超过 200KB 会被整份拒收并提示收窄,不会给你半截。',
383
+ parameters: {
384
+ type: 'object',
385
+ properties: {
386
+ command: { type: 'string', description: '完整 shell 命令,原样交给 bash -c' },
387
+ cwd: { type: 'string', description: '工作目录;省略则用进程当前目录' },
388
+ timeout_ms: { type: 'integer', description: '超时毫秒,默认 30000,上限 300000' },
389
+ },
390
+ required: ['command'],
391
+ },
392
+ output: {
393
+ schema: {
394
+ type: 'object',
395
+ additionalProperties: false,
396
+ properties: {
397
+ code: { type: 'integer', description: '退出码;超时是 -1' },
398
+ stdout: { type: 'string' },
399
+ stderr: { type: 'string' },
400
+ },
401
+ required: ['code', 'stdout', 'stderr'],
402
+ },
403
+ render: (_args, value) => {
404
+ const parts = []
405
+ if (value.stdout !== '') parts.push(value.stdout)
406
+ if (value.stderr !== '') parts.push(`[stderr]\n${value.stderr}`)
407
+ if (parts.length === 0) parts.push(`(无输出,退出码 ${value.code})`)
408
+ if (value.code !== 0) parts.push(`[exit code: ${value.code}]`)
409
+ return [{ type: 'text', text: parts.join('\n') }]
410
+ },
411
+ },
412
+ async execute(args) {
413
+ const command = typeof args.command === 'string' ? args.command.trim() : ''
414
+ if (command === '') return { code: 2, stdout: '', stderr: 'command 不能为空' }
415
+ const timeout = Math.min(Math.max(Number(args.timeout_ms) || 30_000, 1_000), 300_000)
416
+ const cwd = typeof args.cwd === 'string' && args.cwd !== '' ? args.cwd : undefined
417
+
418
+ return await new Promise((resolve) => {
419
+ const child = spawn('bash', ['-c', command], {
420
+ cwd,
421
+ // 让 shell 里的 abook 认定同一个库:程序在 cache,靠位置推导会错。
422
+ env: { ...process.env, ...(typeof root === 'string' && root !== '' ? { ABOOK_ROOT: root } : {}) },
423
+ })
424
+ const out = []
425
+ const err = []
426
+ let bytes = 0
427
+ let settled = false
428
+ const finish = (result) => {
429
+ if (settled) return
430
+ settled = true
431
+ clearTimeout(timer)
432
+ resolve(result)
433
+ }
434
+ const timer = setTimeout(() => {
435
+ try { child.kill('SIGKILL') } catch {}
436
+ finish({ code: -1, stdout: out.join(''), stderr: `${Math.round(timeout / 1000)} 秒超时,已终止` })
437
+ }, timeout)
438
+
439
+ child.stdout.on('data', (c) => {
440
+ bytes += c.length
441
+ if (bytes > RAW_LIMIT) {
442
+ try { child.kill('SIGKILL') } catch {}
443
+ finish({ code: -1, stdout: '', stderr: `输出超过 200KB,整份拒收。收窄一下:加 --limit/--page,或者把结果重定向到文件再用 grep/read 分页看。` })
444
+ return
445
+ }
446
+ out.push(c)
447
+ })
448
+ child.stderr.on('data', (c) => {
449
+ if (err.length < 500) err.push(c)
450
+ })
451
+ child.on('error', (e) => finish({ code: -1, stdout: out.join(''), stderr: String(e?.message ?? e) }))
452
+ child.on('close', (code) => finish({ code: code ?? -1, stdout: out.join(''), stderr: err.join('') }))
453
+ })
454
+ },
455
+ })
456
+
373
457
  // 附件:记忆条目 = 文字域 + 文件域,这是文件域那一半。
374
458
  const attach = scope.tools.register({
375
459
  name: 'aurorix_attach',
@@ -481,7 +565,7 @@ export function attachHost(ctx, deps) {
481
565
  })
482
566
 
483
567
  scope.effect(() => () => {
484
- for (const dispose of [recall, remember, runCmd, spec, guide, curate, attach]) {
568
+ for (const dispose of [recall, remember, runCmd, spec, guide, curate, attach, sh]) {
485
569
  try {
486
570
  if (typeof dispose === 'function') dispose()
487
571
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-aurorix-agentbook",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "dsh-aurorix-agentbook:给 dsh-go 接通 Aurorix 记忆库——原生界面(状态条/胶囊/菜单/面板/保险箱)加宿主侧提示词注入、分层快照与四个工具。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",