dsh-aurorix-agentbook 1.3.0 → 1.4.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.
Files changed (2) hide show
  1. package/lib/host.js +111 -1
  2. package/package.json +1 -1
package/lib/host.js CHANGED
@@ -5,6 +5,23 @@
5
5
  //
6
6
  // 每个服务都用 ctx.inject 软依赖:宿主没有 systemPrompt(或 tools、commands)
7
7
  // 时,那一块就不装,其余部分照常工作,整个插件不会因此起不来。
8
+ import { spawn } from 'node:child_process'
9
+ import { delimiter, dirname } from 'node:path'
10
+
11
+ /**
12
+ * 当前能用的 shell。不同平台差得远,命令语法完全不同,所以要让模型知道自己在哪套里。
13
+ * Windows 上没有 bash:有 PowerShell 就用它,再不行退回 cmd。
14
+ */
15
+ function detectShell() {
16
+ if (process.platform !== 'win32') {
17
+ return { kind: 'posix', name: 'bash', build: (c) => ['-c', c] }
18
+ }
19
+ if (typeof process.env.PSModulePath === 'string' && process.env.PSModulePath !== '') {
20
+ return { kind: 'powershell', name: 'powershell', build: (c) => ['-NoProfile', '-NonInteractive', '-Command', c] }
21
+ }
22
+ const comspec = typeof process.env.ComSpec === 'string' ? process.env.ComSpec : 'cmd.exe'
23
+ return { kind: 'cmd', name: comspec, build: (c) => ['/d', '/s', '/c', c] }
24
+ }
8
25
  import {
9
26
  buildSnapshot,
10
27
  createAccessStore,
@@ -370,6 +387,99 @@ export function attachHost(ctx, deps) {
370
387
  },
371
388
  })
372
389
 
390
+ // Shell:整理记忆时得能跑 grep、管道、脚本,只给单个命令不够用。
391
+ const SHELL = detectShell()
392
+ const RAW_LIMIT = 200 * 1024
393
+ const sh = scope.tools.register({
394
+ name: 'aurorix_sh',
395
+ description:
396
+ '在本机 shell 里跑一条命令(bash -c)。能用管道、重定向、grep、sed、awk、jq、python 脚本——整理记忆时搜关键字、批量替换、统计都靠它。\n' +
397
+ 'abook 已经在 PATH 里,直接打 `abook ...` 就行,不用带路径。\n' +
398
+ '**唯一要记的坑**:abook 的输出不要接 head / tail / cut -c / sed -n 范围——它检测到截断会把整份结果丢弃。要控篇幅用 --limit / --page / --json;要加工就先重定向到文件,再用别的命令读那个文件。\n' +
399
+ '输出超过 200KB 会被整份拒收并提示收窄,不会给你半截。\n' +
400
+ `当前 shell:${SHELL.name}(${SHELL.kind})。` +
401
+ (SHELL.kind === 'posix'
402
+ ? '按 bash 语法写:管道 |、重定向 >、grep/sed/awk 都能用。'
403
+ : SHELL.kind === 'powershell'
404
+ ? '按 PowerShell 语法写:管道传对象而不是文本,取字段用 Select-Object / Where-Object,没有 grep;路径用反斜杠或正斜杠都行。'
405
+ : '按 cmd 语法写:可用 | 与 >,但没有 grep;复杂处理建议改用 node 或 python 脚本。'),
406
+ parameters: {
407
+ type: 'object',
408
+ properties: {
409
+ command: { type: 'string', description: '完整 shell 命令,原样交给 bash -c' },
410
+ cwd: { type: 'string', description: '工作目录;省略则用进程当前目录' },
411
+ timeout_ms: { type: 'integer', description: '超时毫秒,默认 30000,上限 300000' },
412
+ },
413
+ required: ['command'],
414
+ },
415
+ output: {
416
+ schema: {
417
+ type: 'object',
418
+ additionalProperties: false,
419
+ properties: {
420
+ code: { type: 'integer', description: '退出码;超时是 -1' },
421
+ stdout: { type: 'string' },
422
+ stderr: { type: 'string' },
423
+ },
424
+ required: ['code', 'stdout', 'stderr'],
425
+ },
426
+ render: (_args, value) => {
427
+ const parts = []
428
+ if (value.stdout !== '') parts.push(value.stdout)
429
+ if (value.stderr !== '') parts.push(`[stderr]\n${value.stderr}`)
430
+ if (parts.length === 0) parts.push(`(无输出,退出码 ${value.code})`)
431
+ if (value.code !== 0) parts.push(`[exit code: ${value.code}]`)
432
+ return [{ type: 'text', text: parts.join('\n') }]
433
+ },
434
+ },
435
+ async execute(args) {
436
+ const command = typeof args.command === 'string' ? args.command.trim() : ''
437
+ if (command === '') return { code: 2, stdout: '', stderr: 'command 不能为空' }
438
+ const timeout = Math.min(Math.max(Number(args.timeout_ms) || 30_000, 1_000), 300_000)
439
+ const cwd = typeof args.cwd === 'string' && args.cwd !== '' ? args.cwd : undefined
440
+
441
+ return await new Promise((resolve) => {
442
+ // PATH 只对这个子进程前置,不动系统环境;这样无论哪个平台,`abook` 都能直接打。
443
+ const binDir = typeof backend?.bin === 'string' && backend.bin !== '' ? dirname(backend.bin) : ''
444
+ const env = {
445
+ ...process.env,
446
+ ...(binDir !== '' ? { PATH: `${binDir}${delimiter}${process.env.PATH ?? ''}` } : {}),
447
+ ...(typeof root === 'string' && root !== '' ? { ABOOK_ROOT: root } : {}),
448
+ }
449
+ const child = spawn(SHELL.name, SHELL.build(command), { cwd, env })
450
+ const out = []
451
+ const err = []
452
+ let bytes = 0
453
+ let settled = false
454
+ const finish = (result) => {
455
+ if (settled) return
456
+ settled = true
457
+ clearTimeout(timer)
458
+ resolve(result)
459
+ }
460
+ const timer = setTimeout(() => {
461
+ try { child.kill('SIGKILL') } catch {}
462
+ finish({ code: -1, stdout: out.join(''), stderr: `${Math.round(timeout / 1000)} 秒超时,已终止` })
463
+ }, timeout)
464
+
465
+ child.stdout.on('data', (c) => {
466
+ bytes += c.length
467
+ if (bytes > RAW_LIMIT) {
468
+ try { child.kill('SIGKILL') } catch {}
469
+ finish({ code: -1, stdout: '', stderr: `输出超过 200KB,整份拒收。收窄一下:加 --limit/--page,或者把结果重定向到文件再用 grep/read 分页看。` })
470
+ return
471
+ }
472
+ out.push(c)
473
+ })
474
+ child.stderr.on('data', (c) => {
475
+ if (err.length < 500) err.push(c)
476
+ })
477
+ child.on('error', (e) => finish({ code: -1, stdout: out.join(''), stderr: String(e?.message ?? e) }))
478
+ child.on('close', (code) => finish({ code: code ?? -1, stdout: out.join(''), stderr: err.join('') }))
479
+ })
480
+ },
481
+ })
482
+
373
483
  // 附件:记忆条目 = 文字域 + 文件域,这是文件域那一半。
374
484
  const attach = scope.tools.register({
375
485
  name: 'aurorix_attach',
@@ -481,7 +591,7 @@ export function attachHost(ctx, deps) {
481
591
  })
482
592
 
483
593
  scope.effect(() => () => {
484
- for (const dispose of [recall, remember, runCmd, spec, guide, curate, attach]) {
594
+ for (const dispose of [recall, remember, runCmd, spec, guide, curate, attach, sh]) {
485
595
  try {
486
596
  if (typeof dispose === 'function') dispose()
487
597
  } 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.1",
4
4
  "description": "dsh-aurorix-agentbook:给 dsh-go 接通 Aurorix 记忆库——原生界面(状态条/胶囊/菜单/面板/保险箱)加宿主侧提示词注入、分层快照与四个工具。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",