advisers 0.1.0-preview.4 → 0.2.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 (53) hide show
  1. package/LICENSE +131 -21
  2. package/NOTICE +34 -0
  3. package/README-zh.md +40 -9
  4. package/README.md +53 -10
  5. package/THIRD-PARTY-NOTICES.md +277 -0
  6. package/bin/advisers.js +4230 -122
  7. package/index.js +1 -1
  8. package/lib/agent.js +414 -0
  9. package/lib/attachments.js +283 -0
  10. package/lib/capture.js +168 -0
  11. package/lib/context-window.js +123 -0
  12. package/lib/digest.js +27 -0
  13. package/lib/doc-review.js +30 -0
  14. package/lib/egress.js +27 -0
  15. package/lib/evidence.js +1995 -0
  16. package/lib/ical.js +542 -0
  17. package/lib/imap.js +181 -0
  18. package/lib/importers.js +1001 -0
  19. package/lib/learning.js +46 -0
  20. package/lib/mcp-server.js +869 -0
  21. package/lib/model.js +307 -0
  22. package/lib/notify.js +189 -0
  23. package/lib/nudge.js +10 -0
  24. package/lib/ocr.js +47 -0
  25. package/lib/pdf.js +105 -0
  26. package/lib/pet-brief-store.js +51 -0
  27. package/lib/pet-brief.js +644 -0
  28. package/lib/receipt-verify.js +624 -0
  29. package/lib/relative-date.js +393 -0
  30. package/lib/remind.js +690 -0
  31. package/lib/research.js +193 -0
  32. package/lib/review.js +88 -0
  33. package/lib/routine.js +22 -0
  34. package/lib/safety/classifier.js +392 -0
  35. package/lib/safety/explain.js +157 -0
  36. package/lib/safety/index.js +333 -0
  37. package/lib/safety/redact.js +212 -0
  38. package/lib/safety/rules.js +824 -0
  39. package/lib/scheduler.js +399 -0
  40. package/lib/source.js +294 -0
  41. package/lib/sqlite.js +1022 -0
  42. package/lib/sync.js +129 -0
  43. package/lib/timestamp.js +45 -0
  44. package/lib/tos/index.js +332 -0
  45. package/lib/tos/manifest.json +32 -0
  46. package/lib/tos/privacy.zh-CN.md +136 -0
  47. package/lib/tos/safety.zh-CN.md +101 -0
  48. package/lib/tos/summary.zh-CN.md +12 -0
  49. package/lib/tos/terms.zh-CN.md +439 -0
  50. package/lib/ui.js +49 -0
  51. package/lib/undo.js +83 -0
  52. package/lib/vault.js +51 -0
  53. package/package.json +26 -4
@@ -0,0 +1,193 @@
1
+ /**
2
+ * DeepSeek server-side web tools (Messages API).
3
+ * Search and URL reading run on the service; no local tool is executed.
4
+ * Citations are provider output, not local evidence.
5
+ *
6
+ * Network hardening (2026-11-17 robustness audit P1-5): every request has an
7
+ * AbortController timeout, a hard wall-clock deadline that does not depend on
8
+ * the fetch implementation honoring the signal, and bounded retries for
9
+ * 429/5xx (same policy as lib/model.js).
10
+ *
11
+ * Content safety (0.2.0): the query/URL text is classified locally BEFORE any
12
+ * network request (surface research.query); a block throws ContentSafetyError
13
+ * and no fetch happens. A model answer that is flagged is returned as-is with
14
+ * an additive safetyWarnings field (the provider call already happened; the
15
+ * design keeps research output at warn, never a silent rewrite). The protected
16
+ * matter egress gate lives in the CLI (lib/egress.js) and is unchanged.
17
+ */
18
+ import { classify, safetyErrorFrom, buildAuditReason } from './safety/index.js'
19
+
20
+ const MAX_TOKENS = 4096
21
+ const RETRY_STATUS = new Set([429, 500, 502, 503, 504])
22
+
23
+ const RESEARCH_INPUT_SURFACE = 'research.query'
24
+ const RESEARCH_OUTPUT_SURFACE = 'research.output'
25
+
26
+ function safetyWarningOf(result, direction) {
27
+ if (!result || result.action === 'allow') return null
28
+ const surface = result.surface || RESEARCH_INPUT_SURFACE
29
+ const ruleIds = Array.isArray(result.ruleIds) ? [...result.ruleIds] : []
30
+ return {
31
+ direction,
32
+ action: result.action,
33
+ code: result.code,
34
+ categories: Array.isArray(result.categories) ? [...result.categories] : [],
35
+ ruleIds,
36
+ surface,
37
+ rulesetVersion: result.rulesetVersion,
38
+ evidenceHash: result.evidenceHash || null,
39
+ crisis: Boolean(result.crisis),
40
+ auditReason: buildAuditReason({
41
+ rulesetVersion: result.rulesetVersion,
42
+ surface,
43
+ action: result.action,
44
+ code: result.code,
45
+ ruleIds,
46
+ evidenceHash: result.evidenceHash,
47
+ }),
48
+ }
49
+ }
50
+
51
+ function root(baseUrl) {
52
+ const base = String(baseUrl || 'https://api.deepseek.com').replace(/\/+$/, '')
53
+ return base.endsWith('/anthropic/v1') ? base : `${base}/anthropic/v1`
54
+ }
55
+
56
+ function normalizeSources(blocks = []) {
57
+ const sources = []
58
+ for (const block of blocks) {
59
+ if (block.type === 'web_search_tool_result' && Array.isArray(block.content)) {
60
+ for (const result of block.content) {
61
+ if (result.type !== 'web_search_result') continue
62
+ sources.push({ url: result.url, title: result.title ?? null, pageAge: result.page_age ?? null, snippet: result.snippet ?? null, siteName: result.site_name ?? null, kind: 'search' })
63
+ }
64
+ }
65
+ if (block.type === 'web_fetch_tool_result') {
66
+ const content = block.content?.type === 'web_fetch_result' ? block.content : null
67
+ if (content) sources.push({ url: content.url, title: content.content?.title ?? null, pageAge: content.published_at ?? null, snippet: null, siteName: null, kind: block.kind ?? 'read_link' })
68
+ }
69
+ }
70
+ return sources
71
+ }
72
+
73
+ function normalizeCitations(blocks = []) {
74
+ const citations = []
75
+ for (const block of blocks) {
76
+ if (block.type !== 'text' || !Array.isArray(block.citations)) continue
77
+ for (const c of block.citations) citations.push({ url: c.url, title: c.title ?? null, citedText: c.cited_text ?? '' })
78
+ }
79
+ return citations
80
+ }
81
+
82
+ async function sleep(ms) {
83
+ return new Promise((resolve) => setTimeout(resolve, ms))
84
+ }
85
+
86
+ export class ResearchError extends Error {
87
+ constructor(message, { status, code, retryable = false } = {}) {
88
+ super(message)
89
+ this.name = 'ResearchError'
90
+ this.status = status
91
+ this.code = code
92
+ this.retryable = retryable
93
+ }
94
+ }
95
+
96
+ export async function research({
97
+ query, urls = [], apiKey, baseUrl, fetchImpl = fetch, deep = false, maxTokens = MAX_TOKENS,
98
+ timeoutMs = Number(process.env.ADVISERS_RESEARCH_TIMEOUT_MS) || 60_000,
99
+ retries = 2,
100
+ signal,
101
+ safetyKey,
102
+ }) {
103
+ if (!apiKey) throw new ResearchError('research requires ADVISERS_API_KEY', { code: 'MISSING_API_KEY' })
104
+ const text = [String(query || '').trim(), ...urls].filter(Boolean).join('\n')
105
+ if (!text) throw new ResearchError('research requires a query or URL', { code: 'INVALID_REQUEST' })
106
+ // Local pre-flight check: a blocked query/URL never opens a network request.
107
+ const requestSafety = classify(text, { surface: RESEARCH_INPUT_SURFACE, hmacKey: safetyKey })
108
+ if (requestSafety.action === 'block') throw safetyErrorFrom(requestSafety, { surface: RESEARCH_INPUT_SURFACE, provider: 'deepseek' })
109
+ const safetyWarnings = []
110
+ const requestWarning = safetyWarningOf(requestSafety, 'input')
111
+ if (requestWarning) safetyWarnings.push(requestWarning)
112
+ const tools = urls.length
113
+ ? [{ type: 'deepseek_read_url', name: 'deepseek_read_url' }]
114
+ : [{ type: 'deepseek_web_search', name: 'deepseek_web_search' }]
115
+ const body = {
116
+ model: process.env.ADVISERS_MODEL || 'deepseek-flash',
117
+ max_tokens: maxTokens,
118
+ thinking: { type: deep ? 'enabled' : 'disabled' },
119
+ tools,
120
+ messages: [{ role: 'user', content: text }],
121
+ }
122
+ let lastError
123
+ for (let attempt = 0; attempt <= retries; attempt++) {
124
+ const controller = new AbortController()
125
+ let timer
126
+ const onAbort = () => controller.abort()
127
+ signal?.addEventListener?.('abort', onAbort, { once: true })
128
+ // Race the fetch against a wall-clock deadline so a hung fetchImpl cannot
129
+ // keep the CLI (or a test) waiting forever even if it ignores the signal.
130
+ const deadline = new Promise((_, reject) => {
131
+ timer = setTimeout(() => {
132
+ controller.abort()
133
+ reject(new ResearchError(`research timeout after ${timeoutMs}ms`, { code: 'TIMEOUT' }))
134
+ }, timeoutMs)
135
+ // The deadline timer must stay referenced: a fetch implementation that
136
+ // ignores the abort signal may leave the event loop empty, and an unref'd
137
+ // timer would never fire (Node 22 treats that as a pending promise at exit).
138
+ })
139
+ try {
140
+ const request = fetchImpl(`${root(baseUrl)}/messages`, {
141
+ method: 'POST',
142
+ headers: { 'content-type': 'application/json', 'x-api-key': apiKey, 'anthropic-version': '2023-06-01' },
143
+ body: JSON.stringify(body),
144
+ signal: controller.signal,
145
+ })
146
+ // Avoid an unhandled rejection when the deadline wins the race.
147
+ request.catch?.(() => {})
148
+ const response = await Promise.race([request, deadline])
149
+ if (!response.ok) {
150
+ const raw = await response.text().catch(() => '')
151
+ const retryable = RETRY_STATUS.has(response.status)
152
+ const error = new ResearchError(`research HTTP ${response.status}: ${raw.slice(0, 300)}`, {
153
+ status: response.status,
154
+ code: response.status === 401 || response.status === 403 ? 'AUTH' : response.status === 429 ? 'RATE_LIMITED' : response.status === 400 ? 'INVALID_REQUEST' : 'UPSTREAM',
155
+ retryable,
156
+ })
157
+ if (!retryable || attempt === retries) throw error
158
+ lastError = error
159
+ } else {
160
+ const data = await response.json()
161
+ const blocks = Array.isArray(data.content) ? data.content : []
162
+ const answer = blocks.filter((b) => b.type === 'text').map((b) => b.text || '').join('')
163
+ const sources = normalizeSources(blocks)
164
+ const citations = normalizeCitations(blocks)
165
+ if (data.stop_reason === 'refusal') throw new ResearchError('research refused by provider safety review', { code: 'REFUSAL' })
166
+ // Output stays at warn: the response already exists, so it is not
167
+ // rewritten; callers get an additive safetyWarnings field instead.
168
+ const answerWarning = safetyWarningOf(classify(answer, { surface: RESEARCH_OUTPUT_SURFACE, hmacKey: safetyKey }), 'output')
169
+ if (answerWarning) safetyWarnings.push(answerWarning)
170
+ return {
171
+ answer,
172
+ sources,
173
+ citations,
174
+ usage: data.usage,
175
+ stopReason: data.stop_reason,
176
+ blocks,
177
+ ...(safetyWarnings.length ? { safetyWarnings } : {}),
178
+ }
179
+ }
180
+ } catch (error) {
181
+ if (signal?.aborted) throw new ResearchError('research cancelled by user', { code: 'CANCELLED_BY_USER' })
182
+ if (error?.name === 'AbortError') throw new ResearchError(`research timeout after ${timeoutMs}ms`, { code: 'TIMEOUT' })
183
+ if (error instanceof ResearchError && !error.retryable) throw error
184
+ lastError = error
185
+ if (attempt === retries) throw error
186
+ } finally {
187
+ if (timer) clearTimeout(timer)
188
+ signal?.removeEventListener?.('abort', onAbort)
189
+ }
190
+ await sleep(400 * 2 ** attempt)
191
+ }
192
+ throw lastError ?? new ResearchError('research request failed', { code: 'UPSTREAM' })
193
+ }
package/lib/review.js ADDED
@@ -0,0 +1,88 @@
1
+ import { chatCompletion } from './model.js'
2
+
3
+ const MAX_NAME = 60
4
+
5
+ export async function reviewRun({ run, routines = [], apiKey, baseUrl, model, fetchImpl = fetch }) {
6
+ const response = await chatCompletion({
7
+ apiKey,
8
+ baseUrl,
9
+ model,
10
+ fetchImpl,
11
+ task: 'review',
12
+ thinking: true,
13
+ effort: 'high',
14
+ messages: [
15
+ {
16
+ role: 'system',
17
+ content: `You review finished secretary task trajectories. Return ONLY JSON:
18
+ {"verdict":"SAVE|FOLD_INTO|NONE","name":"<=60 chars","description":"<=60 chars","steps":["tool or step"],"verification":["file:path"],"foldInto":"routine id or null","pitfalls":["..."],"reason":"..."}
19
+ Rules: only tools/artifacts present in the run; never invent a file; SAVE only if repeatable; FOLD_INTO only an existing routine id; NONE is a good answer.`,
20
+ },
21
+ { role: 'user', content: JSON.stringify({ run, routines: routines.map((r) => ({ id: r.id, name: r.name, description: r.description })) }) },
22
+ ],
23
+ })
24
+ return parseReview(response.content)
25
+ }
26
+
27
+ export function parseReview(raw) {
28
+ if (typeof raw !== 'string') return { verdict: 'NONE', reason: 'empty review' }
29
+ let text = raw.trim().replace(/^```(?:json)?\s*/i, '').replace(/```$/i, '').trim()
30
+ try {
31
+ const parsed = JSON.parse(text)
32
+ const verdict = ['SAVE', 'FOLD_INTO', 'NONE'].includes(parsed.verdict) ? parsed.verdict : 'NONE'
33
+ return { ...parsed, verdict, name: String(parsed.name ?? '').slice(0, MAX_NAME), description: String(parsed.description ?? '').slice(0, MAX_NAME) }
34
+ } catch {
35
+ return { verdict: 'NONE', reason: 'invalid review JSON' }
36
+ }
37
+ }
38
+
39
+ /** Deterministic lint: the model's proposal must be backed by this run. */
40
+ export function lintReview(review, { run, routines = [] }) {
41
+ if (review.verdict === 'NONE') return { ok: true, review }
42
+ const tools = new Set((run.tools ?? []).map((t) => (typeof t === 'string' ? t : t.name)))
43
+ const artifacts = new Set(run.artifacts ?? [])
44
+ if (review.verdict === 'SAVE') {
45
+ if (!review.name || !review.description) return { ok: false, error: 'name/description required' }
46
+ if (!Array.isArray(review.steps) || review.steps.length < 2) return { ok: false, error: 'steps required' }
47
+ for (const step of review.steps) {
48
+ if (!tools.has(step)) return { ok: false, error: `step not in run: ${step}` }
49
+ }
50
+ const checks = (review.verification ?? []).filter((v) => String(v).startsWith('file:'))
51
+ for (const check of checks) {
52
+ if (!artifacts.has(check.slice(5))) return { ok: false, error: `verification not in run: ${check}` }
53
+ }
54
+ if (checks.length === 0) return { ok: false, error: 'verification must reference an artifact' }
55
+ return { ok: true, review: { ...review, verification: checks } }
56
+ }
57
+ if (review.verdict === 'FOLD_INTO') {
58
+ const target = routines.find((r) => r.id === review.foldInto)
59
+ if (!target) return { ok: false, error: 'unknown foldInto routine' }
60
+ return { ok: true, review }
61
+ }
62
+ return { ok: false, error: 'unknown verdict' }
63
+ }
64
+
65
+ export function materializeReview(review, { run, ledger }) {
66
+ if (review.verdict === 'NONE') return { verdict: 'NONE' }
67
+ if (review.verdict === 'SAVE') {
68
+ const routine = ledger.addRoutine({
69
+ name: review.name,
70
+ description: review.description,
71
+ trigger: [],
72
+ steps: review.steps,
73
+ preferences: [],
74
+ pitfalls: review.pitfalls ?? [],
75
+ verification: review.verification,
76
+ createdBy: 'agent',
77
+ })
78
+ return { verdict: 'SAVE', routine }
79
+ }
80
+ if (review.verdict === 'FOLD_INTO') {
81
+ const patch = ledger.proposeRoutinePatch(review.foldInto, {
82
+ pitfalls: review.pitfalls ?? [],
83
+ verification: review.verification ?? [],
84
+ })
85
+ return { verdict: 'FOLD_INTO', patch }
86
+ }
87
+ return { verdict: 'NONE' }
88
+ }
package/lib/routine.js ADDED
@@ -0,0 +1,22 @@
1
+ import { existsSync, statSync } from 'node:fs'
2
+ import { resolve } from 'node:path'
3
+
4
+ /**
5
+ * Verification entries are intentionally small and local:
6
+ * "file:out/weekly.docx" -> file exists and is non-empty
7
+ * anything else -> manual check (never auto-passes a failed file)
8
+ */
9
+ export function checkRoutine(routine, { cwd = process.cwd() } = {}) {
10
+ const results = []
11
+ for (const entry of routine.verification ?? []) {
12
+ if (entry.startsWith('file:')) {
13
+ const path = resolve(cwd, entry.slice(5).trim())
14
+ const exists = existsSync(path)
15
+ const bytes = exists ? statSync(path).size : 0
16
+ results.push({ entry, kind: 'file', ok: exists && bytes > 0, path, bytes })
17
+ } else {
18
+ results.push({ entry, kind: 'manual', ok: true })
19
+ }
20
+ }
21
+ return { ok: results.every((r) => r.ok), results }
22
+ }