advisers 0.2.3 → 0.3.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.
- package/LICENSE +0 -0
- package/NOTICE +0 -0
- package/README-zh.md +10 -6
- package/README.md +15 -6
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +3323 -222
- package/index.js +1 -1
- package/lib/agent.js +0 -0
- package/lib/attachments.js +0 -0
- package/lib/audit-anchor.js +683 -0
- package/lib/backup.js +1345 -0
- package/lib/capture.js +0 -0
- package/lib/context-window.js +0 -0
- package/lib/digest.js +0 -0
- package/lib/doc-review.js +0 -0
- package/lib/egress.js +0 -0
- package/lib/error-codes.js +1373 -0
- package/lib/evidence.js +202 -26
- package/lib/fs-safe.js +70 -7
- package/lib/ical.js +0 -0
- package/lib/imap.js +6 -2
- package/lib/importers.js +0 -0
- package/lib/learning.js +0 -0
- package/lib/ledger-read.js +608 -0
- package/lib/mcp-server.js +294 -38
- package/lib/model.js +0 -0
- package/lib/node-warning-filter.js +15 -0
- package/lib/notify.js +13 -3
- package/lib/nudge.js +0 -0
- package/lib/ocr.js +0 -0
- package/lib/pdf.js +0 -0
- package/lib/pet-brief-store.js +0 -0
- package/lib/pet-brief.js +102 -39
- package/lib/receipt-verify.js +213 -15
- package/lib/relative-date.js +0 -0
- package/lib/remind.js +91 -36
- package/lib/research.js +0 -0
- package/lib/review.js +0 -0
- package/lib/routine.js +0 -0
- package/lib/safety/classifier.js +0 -0
- package/lib/safety/explain.js +0 -0
- package/lib/safety/index.js +0 -0
- package/lib/safety/redact.js +0 -0
- package/lib/safety/rules.js +0 -0
- package/lib/scheduler.js +0 -0
- package/lib/source.js +0 -0
- package/lib/sqlite.js +1214 -24
- package/lib/sync.js +0 -0
- package/lib/text-safe.js +229 -0
- package/lib/timestamp.js +0 -0
- package/lib/tos/index.js +1 -1
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +12 -9
- package/lib/tos/safety.zh-CN.md +4 -3
- package/lib/tos/summary.zh-CN.md +3 -3
- package/lib/tos/terms.zh-CN.md +8 -6
- package/lib/tui/color.js +164 -0
- package/lib/tui/index.js +33 -0
- package/lib/tui/json.js +124 -0
- package/lib/tui/labels.js +50 -0
- package/lib/tui/policy.js +108 -0
- package/lib/tui/render.js +371 -0
- package/lib/tui/sanitize.js +142 -0
- package/lib/tui/text-file.js +116 -0
- package/lib/tui/width.js +577 -0
- package/lib/ui.js +0 -0
- package/lib/undo.js +0 -0
- package/lib/untrusted.js +0 -0
- package/lib/vault.js +0 -0
- package/package.json +6 -5
package/lib/tui/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisers 0.3.0 output kernel (pure functions, no terminal I/O).
|
|
3
|
+
*
|
|
4
|
+
* Modules:
|
|
5
|
+
* width.js display width / graphemes / wrap / truncate / pad / columns
|
|
6
|
+
* sanitize.js sanitizeDisplay / jsonSafe / display safety predicates
|
|
7
|
+
* color.js --color / NO_COLOR precedence + 16-colour SGR whitelist
|
|
8
|
+
* policy.js resolveOutputPolicy: TTY / plain / accessible / interactive
|
|
9
|
+
* render.js renderStatus / renderResult / renderError / renderConfirm / renderSnapshot
|
|
10
|
+
* json.js JSON purity helpers (stringifyJson / isJsonPure / assertJsonPure)
|
|
11
|
+
* text-file.js BOM-aware text/JSON reads + BOM-less atomic writes
|
|
12
|
+
*
|
|
13
|
+
* lib/text-safe.js holds the low-level console/log primitives (sanitizeConsoleText);
|
|
14
|
+
* it is re-exported here for convenience. bin/advisers.js wiring lands in Wave 2; this
|
|
15
|
+
* barrel adds no side effects and does not read process.argv/env.
|
|
16
|
+
*/
|
|
17
|
+
export * from './width.js'
|
|
18
|
+
export * from './sanitize.js'
|
|
19
|
+
export * from './color.js'
|
|
20
|
+
export * from './policy.js'
|
|
21
|
+
export * from './render.js'
|
|
22
|
+
export * from './json.js'
|
|
23
|
+
export * from './text-file.js'
|
|
24
|
+
export {
|
|
25
|
+
sanitizeConsoleText,
|
|
26
|
+
stripBom,
|
|
27
|
+
stripTerminalSequences,
|
|
28
|
+
stripBidiControls,
|
|
29
|
+
stripControlChars,
|
|
30
|
+
DEFAULT_CONSOLE_TEXT_MAX,
|
|
31
|
+
TRUNCATION_MARK,
|
|
32
|
+
} from '../text-safe.js'
|
|
33
|
+
|
package/lib/tui/json.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON purity helpers for --json / --out consumers (0.3.0 output kernel).
|
|
3
|
+
*
|
|
4
|
+
* The CLI JSON contract says: stdout is exactly one JSON document, no human prose, no
|
|
5
|
+
* ANSI, diagnostics on stderr. These helpers make that checkable in unit tests and in
|
|
6
|
+
* CI without parsing human text:
|
|
7
|
+
*
|
|
8
|
+
* stringifyJson(value) deterministic serialisation; optional display sanitisation
|
|
9
|
+
* isJsonPure(text) parseable JSON + no raw C0/C1/DEL bytes
|
|
10
|
+
* assertJsonPure(text) throws JsonPurityError with the offending offset
|
|
11
|
+
*
|
|
12
|
+
* Purity is about the emitted byte stream, not about rewriting data. Data fidelity is
|
|
13
|
+
* the JSON contract's default (see docs/CLI-JSON.md); jsonSafe() from sanitize.js is
|
|
14
|
+
* opt-in for human-facing fields (message/hint/subject).
|
|
15
|
+
*/
|
|
16
|
+
import { jsonSafe } from './sanitize.js'
|
|
17
|
+
|
|
18
|
+
/** Error code reported by assertJsonPure(). */
|
|
19
|
+
export const JSON_PURITY_CODE = 'E_JSON_IMPURE'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when a --json stream is not pure JSON.
|
|
23
|
+
*/
|
|
24
|
+
export class JsonPurityError extends Error {
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} message
|
|
27
|
+
* @param {{ offset?: number, reason?: string }} [details]
|
|
28
|
+
*/
|
|
29
|
+
constructor(message, { offset = -1, reason = 'unknown' } = {}) {
|
|
30
|
+
super(message)
|
|
31
|
+
this.name = 'JsonPurityError'
|
|
32
|
+
this.code = JSON_PURITY_CODE
|
|
33
|
+
this.offset = offset
|
|
34
|
+
this.reason = reason
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Serialise a value as JSON.
|
|
40
|
+
*
|
|
41
|
+
* @param {unknown} value
|
|
42
|
+
* @param {{ sanitize?: boolean, space?: number }} [options] `sanitize: true` runs
|
|
43
|
+
* jsonSafe() on strings/keys first (display fields only; not the data default).
|
|
44
|
+
* @returns {string}
|
|
45
|
+
*/
|
|
46
|
+
export function stringifyJson(value, { sanitize = false, space = 0 } = {}) {
|
|
47
|
+
const prepared = sanitize ? jsonSafe(value) : value
|
|
48
|
+
const text = JSON.stringify(prepared, null, space)
|
|
49
|
+
if (typeof text !== 'string') throw new TypeError('stringifyJson: value is not JSON-serialisable')
|
|
50
|
+
return text
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Locate the first reason a text is not a pure JSON stream.
|
|
55
|
+
*
|
|
56
|
+
* @param {unknown} text
|
|
57
|
+
* @returns {{ reason: 'not-json' | 'empty' | 'control-char' | 'c1-control', offset: number } | null}
|
|
58
|
+
*/
|
|
59
|
+
export function findJsonImpurity(text) {
|
|
60
|
+
if (typeof text !== 'string') return { reason: 'not-json', offset: -1 }
|
|
61
|
+
const trimmed = text.trim()
|
|
62
|
+
if (trimmed === '') return { reason: 'empty', offset: -1 }
|
|
63
|
+
try {
|
|
64
|
+
JSON.parse(trimmed)
|
|
65
|
+
} catch {
|
|
66
|
+
return { reason: 'not-json', offset: -1 }
|
|
67
|
+
}
|
|
68
|
+
for (let index = 0; index < trimmed.length; index += 1) {
|
|
69
|
+
const code = trimmed.charCodeAt(index)
|
|
70
|
+
const isForbidden = code <= 0x08 || (code >= 0x0b && code <= 0x0c) || (code >= 0x0e && code <= 0x1f) || (code >= 0x7f && code <= 0x9f)
|
|
71
|
+
if (!isForbidden) continue
|
|
72
|
+
return { reason: code >= 0x7f ? 'c1-control' : 'control-char', offset: index }
|
|
73
|
+
}
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* True when text is exactly one JSON document with no raw control bytes that could
|
|
79
|
+
* forge terminal state (ANSI/C1). Human prose mixed into the stream fails this check.
|
|
80
|
+
*
|
|
81
|
+
* @param {unknown} text
|
|
82
|
+
* @returns {boolean}
|
|
83
|
+
*/
|
|
84
|
+
export function isJsonPure(text) {
|
|
85
|
+
return findJsonImpurity(text) === null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Assert a --json stream is pure; throws JsonPurityError otherwise.
|
|
90
|
+
*
|
|
91
|
+
* @param {unknown} text
|
|
92
|
+
* @param {{ label?: string }} [options] label names the stream in the error message
|
|
93
|
+
* @returns {string} the input text (for fluent use)
|
|
94
|
+
*/
|
|
95
|
+
export function assertJsonPure(text, { label = 'stdout' } = {}) {
|
|
96
|
+
if (typeof text !== 'string') throw new JsonPurityError(`${label}: expected a JSON string, got ${typeof text}`, { reason: 'not-json' })
|
|
97
|
+
const impurity = findJsonImpurity(text)
|
|
98
|
+
if (impurity === null) return text
|
|
99
|
+
if (impurity.reason === 'empty') throw new JsonPurityError(`${label}: expected one JSON document, got an empty stream`, { reason: 'empty' })
|
|
100
|
+
if (impurity.reason === 'not-json') {
|
|
101
|
+
const preview = text.trim().slice(0, 80).replace(/[\u0000-\u001f\u007f-\u009f]/g, '?')
|
|
102
|
+
throw new JsonPurityError(`${label}: stream is not one JSON document (human text or a second document leaked in): ${preview}`, { reason: 'not-json' })
|
|
103
|
+
}
|
|
104
|
+
const codePoint = text.trim().charCodeAt(impurity.offset)
|
|
105
|
+
const hex = codePoint.toString(16).toUpperCase().padStart(2, '0')
|
|
106
|
+
throw new JsonPurityError(`${label}: raw control character U+${hex} at offset ${impurity.offset} would corrupt a terminal; JSON output must not contain ANSI/C0/C1 bytes`, { offset: impurity.offset, reason: impurity.reason })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Build a stderr error envelope object for a failed --json command.
|
|
111
|
+
*
|
|
112
|
+
* Rendering is the caller's job (stdout stays empty); this helper keeps the envelope
|
|
113
|
+
* shape consistent without owning the error-code registry.
|
|
114
|
+
*
|
|
115
|
+
* @param {{ code: string, message: string, hint?: string, details?: unknown }} error
|
|
116
|
+
* @returns {{ error: { code: string, message: string, hint?: string, details?: unknown } }}
|
|
117
|
+
*/
|
|
118
|
+
export function errorEnvelope({ code, message, hint, details } = {}) {
|
|
119
|
+
const envelope = { error: { code: String(code ?? 'E_UNKNOWN'), message: String(message ?? '') } }
|
|
120
|
+
if (hint !== undefined) envelope.error.hint = String(hint)
|
|
121
|
+
if (details !== undefined) envelope.error.details = jsonSafe(details)
|
|
122
|
+
return envelope
|
|
123
|
+
}
|
|
124
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default (zh-CN) label table for the 0.3.0 output kernel.
|
|
3
|
+
*
|
|
4
|
+
* 0.3.0 ships Simplified Chinese human-facing text. This table is the kernel's single
|
|
5
|
+
* place for display words so callers can inject a different table (tests use it to
|
|
6
|
+
* prove no bare literals leak into render logic) and so the 0.3.1 i18n seam can
|
|
7
|
+
* re-export or replace it without touching render.js.
|
|
8
|
+
*
|
|
9
|
+
* Machine-facing strings (command names, flags, JSON keys, error codes) are English
|
|
10
|
+
* and never live here.
|
|
11
|
+
*/
|
|
12
|
+
export const DEFAULT_LABELS = Object.freeze({
|
|
13
|
+
userPrefix: '> ',
|
|
14
|
+
itemPrefix: ' - ',
|
|
15
|
+
indent: ' ',
|
|
16
|
+
filePrefix: ' 文件:',
|
|
17
|
+
checkPrefix: ' 校验:',
|
|
18
|
+
warningPrefix: ' ! ',
|
|
19
|
+
nextPrefix: ' 下一步:',
|
|
20
|
+
errorPrefix: '! 没做成:',
|
|
21
|
+
confirmPrefix: '! 需要确认:',
|
|
22
|
+
reasonPrefix: ' 原因:',
|
|
23
|
+
keptPrefix: ' 已保留:',
|
|
24
|
+
writePrefix: ' 写入:',
|
|
25
|
+
summaryPrefix: ' 摘要:',
|
|
26
|
+
digestPrefix: ' 摘要:',
|
|
27
|
+
codePrefix: ' 代码:',
|
|
28
|
+
confirmPrompt: 'y 写入 / n 取消(默认 n)',
|
|
29
|
+
statusSeparator: ' · ',
|
|
30
|
+
statusAccessiblePrefix: '[状态] ',
|
|
31
|
+
statusReady: '就绪',
|
|
32
|
+
statusBusy: (seconds) => `工作中 ${seconds}s · Esc 中断`,
|
|
33
|
+
statusBusyNoTimer: '工作中 · Esc 中断',
|
|
34
|
+
statusConfirm: '待确认 · y 写入 / n 取消(默认 n)',
|
|
35
|
+
statusError: (reason) => `上次失败:${reason} · /retry`,
|
|
36
|
+
statusUnknownReason: '未记录',
|
|
37
|
+
statusOffline: '模型不可达(上次失败) · /status',
|
|
38
|
+
statusLocalModel: '本地模式',
|
|
39
|
+
statusHelpHint: '/help',
|
|
40
|
+
statusToday: (due, overdue) => `今天 ${due} 件到期 / ${overdue} 件逾期`,
|
|
41
|
+
statusTodayClear: '今天没有到期项',
|
|
42
|
+
overflowItems: (count) => ` …(+${count} 条,/more 或 --json 查看)`,
|
|
43
|
+
overflowLines: (count) => ` …(+${count} 行,/more 或 --json 查看)`,
|
|
44
|
+
warningOverflow: (count) => `(+${count} 条)`,
|
|
45
|
+
verboseTool: (name, ok) => ` · ${name} ${ok ? 'ok' : 'FAILED'}`,
|
|
46
|
+
verboseDuration: (duration) => ` 耗时:${duration}`,
|
|
47
|
+
artifactBytes: (bytes) => bytes,
|
|
48
|
+
snapshotTitle: 'Advisers TUI',
|
|
49
|
+
})
|
|
50
|
+
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output policy: TTY detection, plain/accessible modes and colour resolution.
|
|
3
|
+
*
|
|
4
|
+
* One policy object is computed at process start and passed to render* functions;
|
|
5
|
+
* it decides every terminal capability question in one place:
|
|
6
|
+
*
|
|
7
|
+
* plain no SGR, no cursor control (CR, clear-line, hide cursor, alt-screen)
|
|
8
|
+
* accessible plain + [状态] status lines + no timed refresh (screen readers)
|
|
9
|
+
* color SGR allowed (see lib/tui/color.js for the precedence)
|
|
10
|
+
* cursorControl CR / ESC[2K / cursor visibility sequences allowed
|
|
11
|
+
* statusRefresh the status line may be rewritten in place (TTY, not accessible)
|
|
12
|
+
* interactive stdin is a TTY, so a shell may prompt
|
|
13
|
+
*
|
|
14
|
+
* Precedence: --accessible and --plain force plain; otherwise non-TTY stdout or
|
|
15
|
+
* TERM=dumb forces plain; colour still follows --color/NO_COLOR inside non-plain
|
|
16
|
+
* mode. A non-TTY stdin means no prompt is possible (interactive=false) even when
|
|
17
|
+
* stdout is a TTY.
|
|
18
|
+
*/
|
|
19
|
+
import { noColorEnv, readColorArg, resolveColorEnabled } from './color.js'
|
|
20
|
+
|
|
21
|
+
const TRUTHY_ENV = /^(1|true|yes|on)$/i
|
|
22
|
+
const FALSY_ENV = /^(0|false|no|off)$/i
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Environment flag: set and not empty/false.
|
|
26
|
+
*
|
|
27
|
+
* @param {Record<string, unknown> | undefined} env
|
|
28
|
+
* @param {string} name
|
|
29
|
+
* @returns {boolean}
|
|
30
|
+
*/
|
|
31
|
+
export function envFlag(env, name) {
|
|
32
|
+
if (!env) return false
|
|
33
|
+
const value = env[name]
|
|
34
|
+
if (value === undefined || value === null) return false
|
|
35
|
+
const text = String(value).trim()
|
|
36
|
+
if (text === '') return false
|
|
37
|
+
if (FALSY_ENV.test(text)) return false
|
|
38
|
+
if (TRUTHY_ENV.test(text)) return true
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve the output policy for one invocation.
|
|
44
|
+
*
|
|
45
|
+
* @param {{
|
|
46
|
+
* argv?: string[],
|
|
47
|
+
* env?: Record<string, unknown>,
|
|
48
|
+
* stdinIsTTY?: boolean,
|
|
49
|
+
* stdoutIsTTY?: boolean,
|
|
50
|
+
* stderrIsTTY?: boolean,
|
|
51
|
+
* }} [options]
|
|
52
|
+
* @returns {Readonly<{ mode: 'tty' | 'plain' | 'accessible', color: boolean, plain: boolean,
|
|
53
|
+
* accessible: boolean, cursorControl: boolean, statusRefresh: boolean,
|
|
54
|
+
* interactive: boolean, stdinIsTTY: boolean, stdoutIsTTY: boolean, stderrIsTTY: boolean,
|
|
55
|
+
* colorMode: 'auto' | 'always' | 'never', reason: string, colorReason: string }>}
|
|
56
|
+
*/
|
|
57
|
+
export function resolveOutputPolicy({ argv = [], env = process.env, stdinIsTTY = false, stdoutIsTTY = false, stderrIsTTY = false } = {}) {
|
|
58
|
+
const args = Array.isArray(argv) ? argv : []
|
|
59
|
+
const colorArg = readColorArg(args)
|
|
60
|
+
if (colorArg.invalid) throw new RangeError('--color must be auto, always or never')
|
|
61
|
+
const accessible = args.includes('--accessible') || envFlag(env, 'ADVISERS_ACCESSIBLE')
|
|
62
|
+
const plainFlag = args.includes('--plain') || envFlag(env, 'ADVISERS_PLAIN')
|
|
63
|
+
const hardPlain = accessible || plainFlag
|
|
64
|
+
const term = typeof env?.TERM === 'string' ? env.TERM : ''
|
|
65
|
+
const autoPlain = !stdoutIsTTY || term === 'dumb'
|
|
66
|
+
let reason = 'tty'
|
|
67
|
+
if (accessible) reason = 'accessible'
|
|
68
|
+
else if (plainFlag) reason = '--plain'
|
|
69
|
+
else if (!stdoutIsTTY) reason = 'stdout-not-tty'
|
|
70
|
+
else if (term === 'dumb') reason = 'term-dumb'
|
|
71
|
+
const colorMode = colorArg.found ? colorArg.mode : 'auto'
|
|
72
|
+
let color = false
|
|
73
|
+
let colorReason = 'plain'
|
|
74
|
+
if (!hardPlain) {
|
|
75
|
+
color = resolveColorEnabled({ mode: colorMode, env, stdoutIsTTY, term })
|
|
76
|
+
if (colorMode === 'always') colorReason = 'forced-always'
|
|
77
|
+
else if (colorMode === 'never') colorReason = 'forced-never'
|
|
78
|
+
else if (noColorEnv(env)) colorReason = 'no-color-env'
|
|
79
|
+
else if (term === 'dumb') colorReason = 'term-dumb'
|
|
80
|
+
else if (!stdoutIsTTY) colorReason = 'stdout-not-tty'
|
|
81
|
+
else colorReason = 'auto-tty'
|
|
82
|
+
}
|
|
83
|
+
// --plain/--accessible always mean "no ANSI at all"; auto-detection (non-TTY stdout,
|
|
84
|
+
// TERM=dumb) disables colour in auto mode and always suppresses cursor control. An
|
|
85
|
+
// explicit --color=always keeps its SGR even in auto-plain mode, but cursor control
|
|
86
|
+
// stays off, so `plain` still describes the suppressed interactive features.
|
|
87
|
+
const plain = hardPlain || autoPlain
|
|
88
|
+
const cursorControl = stdoutIsTTY && !hardPlain && !autoPlain
|
|
89
|
+
const statusRefresh = cursorControl && !accessible
|
|
90
|
+
return Object.freeze({
|
|
91
|
+
mode: accessible ? 'accessible' : plain ? 'plain' : 'tty',
|
|
92
|
+
color,
|
|
93
|
+
plain,
|
|
94
|
+
accessible,
|
|
95
|
+
cursorControl,
|
|
96
|
+
statusRefresh,
|
|
97
|
+
interactive: stdinIsTTY,
|
|
98
|
+
stdinIsTTY,
|
|
99
|
+
stdoutIsTTY,
|
|
100
|
+
stderrIsTTY,
|
|
101
|
+
colorMode,
|
|
102
|
+
reason,
|
|
103
|
+
colorReason,
|
|
104
|
+
hardPlain,
|
|
105
|
+
autoPlain,
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|