advisers 0.2.0-preview.71 → 0.2.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/model.js CHANGED
@@ -4,6 +4,13 @@
4
4
  * Thinking-mode reasoning_content is returned only as wire state; callers must
5
5
  * never persist it or expose it as evidence.
6
6
  */
7
+ import {
8
+ inspectMessages,
9
+ normalizeProviderRefusal,
10
+ safetyErrorFrom,
11
+ buildAuditReason,
12
+ } from './safety/index.js'
13
+
7
14
  const RETRY_STATUS = new Set([429, 500, 502, 503, 504])
8
15
  const EFFORTS = new Set(['low', 'high', 'max'])
9
16
 
@@ -27,6 +34,102 @@ const TASK_EFFORT = {
27
34
  max: 'max',
28
35
  }
29
36
 
37
+ // ---------------------------------------------------------------------------
38
+ // content safety (local rule layer; never sends the original text anywhere)
39
+ //
40
+ // chatCompletion is the single choke point for every model call (chat, agent,
41
+ // capture, review, doc-review). Outbound messages and the returned assistant
42
+ // message are classified locally before/after the request:
43
+ // block -> ContentSafetyError (code/ruleIds/rulesetVersion,
44
+ // message has no original text; no network request is
45
+ // made for a blocked request)
46
+ // warn/confirm/redact -> request proceeds, safetyWarnings is attached to
47
+ // the result (additive optional field)
48
+ // The classifier surface is model.output (the registered surface for
49
+ // model-bound text; egress, no local-file downgrade). Wiring is documented in
50
+ // docs/CONTENT-SAFETY.md section 8.
51
+ // ---------------------------------------------------------------------------
52
+ const SAFETY_SURFACE = 'model.output'
53
+ /** Disable the optional DeepSeek safety_check network-side gate with '0'. */
54
+ const SAFETY_CLOUD_ENV = 'ADVISERS_SAFETY_CLOUD'
55
+
56
+ function safetyWarningOf(result, direction) {
57
+ if (!result || result.action === 'allow') return null
58
+ const surface = result.surface || SAFETY_SURFACE
59
+ const ruleIds = Array.isArray(result.ruleIds) ? [...result.ruleIds] : []
60
+ const auditReason = buildAuditReason({
61
+ rulesetVersion: result.rulesetVersion,
62
+ surface,
63
+ action: result.action,
64
+ code: result.code,
65
+ ruleIds,
66
+ evidenceHash: result.evidenceHash,
67
+ })
68
+ return {
69
+ direction,
70
+ action: result.action,
71
+ code: result.code,
72
+ categories: Array.isArray(result.categories) ? [...result.categories] : [],
73
+ ruleIds,
74
+ surface,
75
+ rulesetVersion: result.rulesetVersion,
76
+ evidenceHash: result.evidenceHash || null,
77
+ crisis: Boolean(result.crisis),
78
+ auditReason,
79
+ }
80
+ }
81
+
82
+ function safetyWarningsOf(result, direction) {
83
+ const warning = safetyWarningOf(result, direction)
84
+ return warning ? [warning] : []
85
+ }
86
+
87
+ /** Throw a typed, text-free error for a blocked pre-request classification. */
88
+ function throwIfBlocked(result, provider, direction) {
89
+ if (!result || result.action !== 'block') return
90
+ const error = safetyErrorFrom(result, { provider, surface: SAFETY_SURFACE })
91
+ error.direction = direction
92
+ throw error
93
+ }
94
+
95
+ /** Map a provider-side refusal/400 to a typed, text-free ContentSafetyError. */
96
+ function throwIfProviderRefused(payload, { provider, status, direction }) {
97
+ const refusal = normalizeProviderRefusal(payload, { status })
98
+ if (!refusal.blocked) return
99
+ const error = safetyErrorFrom(
100
+ { action: 'block', code: refusal.code, categories: [], ruleIds: [], surface: SAFETY_SURFACE, evidenceHash: null },
101
+ { provider: refusal.provider || provider, surface: SAFETY_SURFACE },
102
+ )
103
+ error.direction = direction
104
+ error.protocol = refusal.protocol
105
+ error.safetyTokens = refusal.safetyTokens
106
+ if (refusal.imagesBlocked?.length) error.imagesBlocked = [...refusal.imagesBlocked]
107
+ throw error
108
+ }
109
+
110
+ function cloudSafetyEnabled(env = process.env) {
111
+ const raw = String(env[SAFETY_CLOUD_ENV] ?? '').trim().toLowerCase()
112
+ if (!raw) return true
113
+ return !['0', 'off', 'false', 'no', 'disabled'].includes(raw)
114
+ }
115
+
116
+ /** Explicit option > env default; DeepSeek-only (other providers stay portable). */
117
+ function resolveCloudSafety(safetyCheck, env = process.env) {
118
+ if (safetyCheck === false) return null
119
+ if (safetyCheck === true) return { level: 'high' }
120
+ if (safetyCheck && typeof safetyCheck === 'object') return safetyCheck
121
+ return cloudSafetyEnabled(env) ? { level: 'high' } : null
122
+ }
123
+
124
+ function parseJsonObject(text) {
125
+ try {
126
+ const value = JSON.parse(String(text ?? ''))
127
+ return value && typeof value === 'object' ? value : null
128
+ } catch {
129
+ return null
130
+ }
131
+ }
132
+
30
133
  export function resolveThinking({ task = 'agent', explicit, env = process.env } = {}) {
31
134
  const mode = String(env.ADVISERS_THINKING || 'auto').toLowerCase()
32
135
  const explicitEffort = normalizeEffort(explicit ?? env.ADVISERS_THINKING_EFFORT)
@@ -50,6 +153,13 @@ function normalizeUsage(usage) {
50
153
  reasoningTokens: reasoning,
51
154
  cachedTokens: Number(usage.prompt_tokens_details?.cached_tokens ?? usage.prompt_cache_hit_tokens ?? 0),
52
155
  totalTokens: Number(usage.total_tokens ?? 0),
156
+ safetyTokens: Number(
157
+ usage.completion_tokens_details?.safety_tokens
158
+ ?? usage.output_tokens_details?.safety_tokens
159
+ ?? usage.input_tokens_details?.safety_tokens
160
+ ?? usage.safety_tokens
161
+ ?? 0,
162
+ ) || 0,
53
163
  }
54
164
  }
55
165
 
@@ -78,14 +188,28 @@ export async function chatCompletion({
78
188
  thinking,
79
189
  effort,
80
190
  signal,
191
+ safetyKey,
192
+ safetyCheck,
81
193
  }) {
82
194
  if (!apiKey) throw new ModelError('missing API key', { code: 'MISSING_API_KEY' })
83
195
  const policy = thinking === false ? { enabled: false } : { enabled: true, ...resolveThinking({ task, explicit: effort }) }
84
196
  if (thinking === true && !policy.enabled) policy.enabled = true
85
197
  const provider = (process.env.ADVISERS_PROVIDER || 'deepseek').toLowerCase()
198
+
199
+ // Local pre-flight classification: a blocked request never reaches the network.
200
+ const requestSafety = inspectMessages(messages, { surface: SAFETY_SURFACE, hmacKey: safetyKey })
201
+ throwIfBlocked(requestSafety, provider, 'input')
202
+ const safetyWarnings = safetyWarningsOf(requestSafety, 'input')
203
+
86
204
  const body = { model, messages, max_tokens: maxTokens }
87
205
  if (tools?.length) body.tools = tools
88
206
  if (provider === 'deepseek') {
207
+ // Optional network-side second net (DeepSeek supports safety_check on the
208
+ // chat completions endpoint; default on for this provider only). Set
209
+ // ADVISERS_SAFETY_CLOUD=0 to disable. Live compatibility is tracked as a
210
+ // documented TODO — the parameter is only added for the DeepSeek provider.
211
+ const cloudSafety = resolveCloudSafety(safetyCheck)
212
+ if (cloudSafety) body.safety_check = cloudSafety
89
213
  if (policy.enabled) {
90
214
  body.thinking = { type: 'enabled' }
91
215
  body.reasoning_effort = normalizeEffort(policy.effort) || 'high'
@@ -113,6 +237,16 @@ export async function chatCompletion({
113
237
  })
114
238
  if (!response.ok) {
115
239
  const text = await response.text().catch(() => '')
240
+ // Upstream content-risk refusals become typed ContentSafetyErrors (the
241
+ // upstream body is never copied into the error). Other HTTP errors keep
242
+ // their existing ModelError shape and retry policy.
243
+ if (provider === 'deepseek') {
244
+ throwIfProviderRefused(parseJsonObject(text) ?? { error: { message: text } }, {
245
+ provider,
246
+ status: response.status,
247
+ direction: 'input',
248
+ })
249
+ }
116
250
  const retryable = RETRY_STATUS.has(response.status)
117
251
  const error = new ModelError(`model HTTP ${response.status}`, { status: response.status, code: response.status === 401 || response.status === 403 ? 'AUTH' : response.status === 400 ? 'INVALID_REQUEST' : 'UPSTREAM', retryable })
118
252
  error.body = text
@@ -120,20 +254,35 @@ export async function chatCompletion({
120
254
  lastError = error
121
255
  } else {
122
256
  const data = await response.json()
257
+ // Documented rejection shapes (finish_reason=content_filter,
258
+ // stop_reason=refusal, message.refusal, image blocks) fail closed.
259
+ throwIfProviderRefused(data, { provider, status: response.status, direction: 'output' })
123
260
  const choice = data?.choices?.[0]
124
261
  const message = choice?.message
125
262
  if (!message) throw new ModelError('model returned no message', { code: 'EMPTY_RESPONSE' })
263
+ const content = typeof message.content === 'string' ? message.content : ''
264
+ const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : []
265
+ // Post-flight classification of the assistant message (text + tool args).
266
+ const outputSafety = inspectMessages([{ role: 'assistant', content, ...(toolCalls.length ? { tool_calls: toolCalls } : {}) }], {
267
+ surface: SAFETY_SURFACE,
268
+ hmacKey: safetyKey,
269
+ })
270
+ throwIfBlocked(outputSafety, provider, 'output')
271
+ safetyWarnings.push(...safetyWarningsOf(outputSafety, 'output'))
126
272
  return {
127
- content: typeof message.content === 'string' ? message.content : '',
273
+ content,
128
274
  reasoningContent: typeof message.reasoning_content === 'string' ? message.reasoning_content : '',
129
- toolCalls: Array.isArray(message.tool_calls) ? message.tool_calls : [],
275
+ toolCalls,
130
276
  finishReason: choice?.finish_reason,
131
277
  usage: normalizeUsage(data.usage),
132
278
  thinking: policy,
133
279
  model,
280
+ ...(safetyWarnings.length ? { safetyWarnings } : {}),
134
281
  }
135
282
  }
136
283
  } catch (error) {
284
+ // A content-safety block is terminal: never retried, never forwarded.
285
+ if (error?.name === 'ContentSafetyError') throw error
137
286
  lastError = error
138
287
  if (signal?.aborted) throw new ModelError('cancelled by user', { code: 'CANCELLED_BY_USER' })
139
288
  if (error.name === 'AbortError') throw new ModelError(`model timeout after ${timeoutMs}ms`, { code: 'TIMEOUT', retryable: true })
package/lib/notify.js CHANGED
@@ -1,16 +1,45 @@
1
1
  /**
2
2
  * E1-T3: best-effort local notification adapter.
3
3
  *
4
- * No cloud push. The caller decides the body; Advisers only sends a short
5
- * count-style body (not client names/quotes). If no channel is available the
6
- * caller keeps the local reminder log; this module reports `sent:false`.
4
+ * No implicit cloud push. The caller decides the body; Advisers only sends a
5
+ * short count-style body (not client names/quotes). If no local channel is
6
+ * available the caller keeps the local reminder log; this module reports
7
+ * `sent:false`. The only outbound channel is the explicit, off-by-default
8
+ * `ADVISERS_NOTIFY_WEBHOOK` adapter below, which sends counts/levels/dates only.
7
9
  */
8
10
  import { existsSync } from 'node:fs'
9
11
  import { spawn } from 'node:child_process'
12
+ import { localDay } from './remind.js'
13
+
14
+ /**
15
+ * Environment for the notification subprocess: an explicit allowlist.
16
+ *
17
+ * The command is a local third-party program (custom notifier, osascript,
18
+ * notify-send, powershell). Inheriting process.env would hand it every Advisers
19
+ * secret — ADVISERS_API_KEY, ADVISERS_AUDIT_KEY (the audit HMAC key),
20
+ * vault/sync/export passphrases and provider URLs (security audit SEC2-03,
21
+ * 2026-11-17). Only OS/runtime basics needed to start a program are passed
22
+ * through; credentials never are.
23
+ */
24
+ const ADAPTER_ENV_KEYS = Object.freeze([
25
+ 'PATH', 'Path', 'PATHEXT', 'COMSPEC', 'SYSTEMROOT', 'SystemRoot', 'windir',
26
+ 'HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA',
27
+ 'TEMP', 'TMP', 'TMPDIR', 'LANG', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'TZ',
28
+ 'PSModulePath',
29
+ ])
30
+
31
+ function adapterEnv(source = process.env) {
32
+ const env = {}
33
+ for (const key of ADAPTER_ENV_KEYS) {
34
+ const value = source?.[key]
35
+ if (typeof value === 'string' && value) env[key] = value
36
+ }
37
+ return env
38
+ }
10
39
 
11
40
  function run(cmd, args, timeoutMs = 5000) {
12
41
  return new Promise((resolve, reject) => {
13
- const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'], timeout: timeoutMs, windowsHide: true })
42
+ const child = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'], timeout: timeoutMs, windowsHide: true, env: adapterEnv() })
14
43
  let err = ''
15
44
  child.stderr.on('data', (b) => { err += b.toString('utf8') })
16
45
  child.on('error', reject)
@@ -31,6 +60,129 @@ function powershellToastScript(title, body) {
31
60
  ].join('; ')
32
61
  }
33
62
 
63
+
64
+ /** Env var that opts a machine into the outbound JSON webhook (off by default). */
65
+ export const WEBHOOK_ENV = 'ADVISERS_NOTIFY_WEBHOOK'
66
+ /** Versioned payload schema; consumers can reject unknown versions. */
67
+ export const WEBHOOK_SCHEMA = 'advisers.notify.webhook.v1'
68
+ const DEFAULT_WEBHOOK_TIMEOUT_MS = 5000
69
+ const MAX_WEBHOOK_DATES = 31
70
+
71
+ export function isWebhookConfigured(env = process.env) {
72
+ return String(env?.[WEBHOOK_ENV] ?? '').trim() !== ''
73
+ }
74
+
75
+ /**
76
+ * Reduce due reminders to the only data allowed to leave the machine: a count,
77
+ * per-level counts and the affected local dates. Titles, creditors, actions,
78
+ * evidence and quotes are never read.
79
+ */
80
+ export function summarizeReminders(reminders) {
81
+ if (!Array.isArray(reminders)) throw new TypeError('summarizeReminders requires an array of reminders')
82
+ const levels = { L1: 0, L2: 0, L3: 0 }
83
+ const dates = new Set()
84
+ let count = 0
85
+ for (const reminder of reminders) {
86
+ if (!reminder || typeof reminder !== 'object') continue
87
+ count += 1
88
+ if (reminder.level === 'L1' || reminder.level === 'L2' || reminder.level === 'L3') levels[reminder.level] += 1
89
+ const day = localDay(reminder.dueAt)
90
+ if (day) dates.add(day)
91
+ }
92
+ return {
93
+ schema: WEBHOOK_SCHEMA,
94
+ count,
95
+ levels,
96
+ dates: [...dates].sort().slice(0, MAX_WEBHOOK_DATES),
97
+ }
98
+ }
99
+
100
+ function resolveWebhookTarget(options) {
101
+ const raw = options.url === undefined ? process.env[WEBHOOK_ENV] : options.url
102
+ if (raw == null || String(raw).trim() === '') return { configured: false }
103
+ try {
104
+ const url = new URL(String(raw).trim())
105
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return { configured: true, error: 'unsupported-protocol' }
106
+ return { configured: true, url }
107
+ } catch {
108
+ return { configured: true, error: 'invalid-url' }
109
+ }
110
+ }
111
+
112
+ function truncateDetail(value, limit = 200) {
113
+ const text = typeof value === 'string' ? value : String(value ?? '')
114
+ return text.length > limit ? text.slice(0, limit) : text
115
+ }
116
+
117
+ /**
118
+ * Opt-in outbound webhook: POST the sanitized summary as JSON.
119
+ *
120
+ * Disabled unless `ADVISERS_NOTIFY_WEBHOOK` (or an explicit `url`) is set. The
121
+ * function never throws for network/config failures; it returns a structured
122
+ * result so the caller can fall back to the local log:
123
+ *
124
+ * { sent: true, channel: 'webhook', configured: true, status, payload }
125
+ * { sent: false, channel: 'webhook', configured: false, reason: 'not-configured' }
126
+ * { sent: false, channel: 'webhook', configured: true, error: 'network'|'timeout'|'http'|'invalid-url'|'unsupported-protocol', status?, detail? }
127
+ */
128
+ export async function sendWebhookNotification(reminders, options = {}) {
129
+ const opts = options ?? {}
130
+ const summary = summarizeReminders(reminders)
131
+ const target = resolveWebhookTarget(opts)
132
+ if (!target.configured) {
133
+ return { sent: false, channel: 'webhook', configured: false, reason: 'not-configured' }
134
+ }
135
+ if (target.error) {
136
+ return { sent: false, channel: 'webhook', configured: true, error: target.error }
137
+ }
138
+ const fetchImpl = opts.fetch ?? globalThis.fetch
139
+ if (typeof fetchImpl !== 'function') {
140
+ return { sent: false, channel: 'webhook', configured: true, error: 'fetch-unavailable' }
141
+ }
142
+ const timeoutMs = Number.isFinite(opts.timeoutMs) && opts.timeoutMs > 0 ? opts.timeoutMs : DEFAULT_WEBHOOK_TIMEOUT_MS
143
+ const signal = typeof AbortSignal?.timeout === 'function' ? AbortSignal.timeout(timeoutMs) : undefined
144
+ try {
145
+ const response = await fetchImpl(target.url.href, {
146
+ method: 'POST',
147
+ headers: { 'content-type': 'application/json', 'user-agent': 'advisers-webhook/1' },
148
+ body: JSON.stringify(summary),
149
+ signal,
150
+ })
151
+ if (!response || typeof response !== 'object') {
152
+ return { sent: false, channel: 'webhook', configured: true, error: 'network', detail: 'empty response' }
153
+ }
154
+ if (!response.ok) {
155
+ let detail
156
+ try {
157
+ detail = truncateDetail(await response.text())
158
+ } catch {
159
+ detail = undefined
160
+ }
161
+ return {
162
+ sent: false,
163
+ channel: 'webhook',
164
+ configured: true,
165
+ error: 'http',
166
+ status: response.status,
167
+ ...(detail ? { detail } : {}),
168
+ }
169
+ }
170
+ return { sent: true, channel: 'webhook', configured: true, status: response.status, payload: summary }
171
+ } catch (error) {
172
+ const name = String(error?.name ?? '')
173
+ if (name === 'TimeoutError' || name === 'AbortError') {
174
+ return { sent: false, channel: 'webhook', configured: true, error: 'timeout', detail: `no response within ${timeoutMs}ms` }
175
+ }
176
+ return {
177
+ sent: false,
178
+ channel: 'webhook',
179
+ configured: true,
180
+ error: 'network',
181
+ detail: truncateDetail(error?.message ?? error),
182
+ }
183
+ }
184
+ }
185
+
34
186
  export async function sendNotification({ title = 'Advisers', body = '', cmd = process.env.ADVISERS_NOTIFY_CMD } = {}) {
35
187
  if (cmd) {
36
188
  await run(cmd, [title, body])
package/lib/ocr.js CHANGED
@@ -11,6 +11,31 @@ import { spawn } from 'node:child_process'
11
11
  import { statSync } from 'node:fs'
12
12
  import { resolve, isAbsolute, relative } from 'node:path'
13
13
 
14
+ /**
15
+ * Environment for the adapter subprocess: an explicit allowlist.
16
+ *
17
+ * The adapter is a local third-party program (e.g. an OCR engine). Inheriting
18
+ * process.env would hand it every Advisers secret — ADVISERS_API_KEY,
19
+ * ADVISERS_AUDIT_KEY (the audit HMAC key), vault/sync/export passphrases and
20
+ * provider URLs (security audit SEC2-03, 2026-11-17). Only OS/runtime basics
21
+ * needed to start a program are passed through; credentials never are.
22
+ */
23
+ const ADAPTER_ENV_KEYS = Object.freeze([
24
+ 'PATH', 'Path', 'PATHEXT', 'COMSPEC', 'SYSTEMROOT', 'SystemRoot', 'windir',
25
+ 'HOME', 'USERPROFILE', 'HOMEDRIVE', 'HOMEPATH', 'APPDATA', 'LOCALAPPDATA',
26
+ 'TEMP', 'TMP', 'TMPDIR', 'LANG', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'TZ',
27
+ 'PSModulePath',
28
+ ])
29
+
30
+ function adapterEnv(source = process.env) {
31
+ const env = {}
32
+ for (const key of ADAPTER_ENV_KEYS) {
33
+ const value = source?.[key]
34
+ if (typeof value === 'string' && value) env[key] = value
35
+ }
36
+ return env
37
+ }
38
+
14
39
  export const MAX_OCR_BYTES = 200_000
15
40
  export const MAX_OCR_MS = 30_000
16
41
 
@@ -32,7 +57,7 @@ export async function runLocalOcr(file, { cmd = process.env.ADVISERS_OCR_CMD, ti
32
57
  if (!cmd) throw new Error('OCR is not configured; set ADVISERS_OCR_CMD to a local OCR executable (no cloud OCR is used)')
33
58
  const abs = assertOcrPath(file, cwd)
34
59
  return await new Promise((resolvePromise, reject) => {
35
- const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true })
60
+ const child = spawn(cmd, [abs], { stdio: ['ignore', 'pipe', 'pipe'], timeout: timeoutMs, windowsHide: true, env: adapterEnv() })
36
61
  let out = '', err = ''
37
62
  child.stdout.on('data', (b) => { out += b.toString('utf8'); if (out.length > MAX_OCR_BYTES) { child.kill(); reject(new Error(`OCR output exceeds ${MAX_OCR_BYTES} chars`)) } })
38
63
  child.stderr.on('data', (b) => { err += b.toString('utf8') })
package/lib/pdf.js CHANGED
@@ -26,14 +26,52 @@ export function findCjkFont() {
26
26
  return null
27
27
  }
28
28
 
29
- function wrap(text, font, size, maxWidth) {
29
+ /**
30
+ * P1-5: per-font/per-size width cache.
31
+ *
32
+ * The old wrap() measured every candidate prefix with
33
+ * font.widthOfTextAtSize(current + ch) — that re-shapes the whole line for
34
+ * every character, so one paragraph cost O(chars²) shaping work (1000 lines
35
+ * took ~900ms end to end). The CJK fonts Advisers supports have additive
36
+ * advance widths (no pair kerning), so measuring each character once and
37
+ * summing the advances yields the identical line breaks in O(chars).
38
+ */
39
+ const WIDTH_CACHES = new WeakMap()
40
+
41
+ function widthOfChar(font, size, ch) {
42
+ let cache = WIDTH_CACHES.get(font)
43
+ if (!cache) { cache = new Map(); WIDTH_CACHES.set(font, cache) }
44
+ const key = size + '\u0000' + ch
45
+ let width = cache.get(key)
46
+ if (width === undefined) {
47
+ width = font.widthOfTextAtSize(ch, size)
48
+ cache.set(key, width)
49
+ }
50
+ return width
51
+ }
52
+
53
+ /**
54
+ * Break one paragraph into lines at maxWidth.
55
+ *
56
+ * Exported so test/perf-regression.test.js can compare it against the original
57
+ * prefix-measuring algorithm (equivalence is asserted on an additive reference
58
+ * font, which is the class of fonts this builder supports).
59
+ */
60
+ export function wrapToWidth(text, font, size, maxWidth) {
30
61
  const lines = []
31
62
  let current = ''
63
+ let currentWidth = 0
32
64
  for (const ch of String(text)) {
33
- if (ch === '\n') { lines.push(current); current = ''; continue }
34
- const next = current + ch
35
- if (font.widthOfTextAtSize(next, size) > maxWidth && current) { lines.push(current); current = ch }
36
- else current = next
65
+ if (ch === '\n') { lines.push(current); current = ''; currentWidth = 0; continue }
66
+ const charWidth = widthOfChar(font, size, ch)
67
+ if (current && currentWidth + charWidth > maxWidth) {
68
+ lines.push(current)
69
+ current = ch
70
+ currentWidth = charWidth
71
+ } else {
72
+ current += ch
73
+ currentWidth += charWidth
74
+ }
37
75
  }
38
76
  if (current) lines.push(current)
39
77
  return lines
@@ -43,6 +81,14 @@ export async function buildSimplePdf({ title = 'Advisers', paragraphs = [], font
43
81
  if (!fontPath) throw new Error('no CJK font found; set ADVISERS_PDF_FONT to a .ttf/.otf font (e.g. simhei.ttf)')
44
82
  const bytes = readFileSync(fontPath)
45
83
  const doc = await PDFDocument.create()
84
+ // PDFDocument.create() calls updateInfoDict(), which stamps /ModDate and
85
+ // /CreationDate with the wall clock. Two runs over identical input therefore
86
+ // produced different bytes whenever they crossed a wall-clock second (the PDF
87
+ // date format has second precision), which made the perf regression test and
88
+ // any byte-level receipt comparison flake. Pin both dates to the caller-
89
+ // supplied generation time so one input always serializes to the same bytes.
90
+ doc.setCreationDate(createdAt)
91
+ doc.setModificationDate(createdAt)
46
92
  doc.registerFontkit(fontkit)
47
93
  const font = await doc.embedFont(bytes, { subset: true })
48
94
  const size = 10.5
@@ -51,7 +97,7 @@ export async function buildSimplePdf({ title = 'Advisers', paragraphs = [], font
51
97
  const { width, height } = page.getSize()
52
98
  let y = height - margin
53
99
  const add = (text, fontSize = size) => {
54
- for (const line of wrap(text, font, fontSize, width - margin * 2)) {
100
+ for (const line of wrapToWidth(text, font, fontSize, width - margin * 2)) {
55
101
  if (y < margin + fontSize * 2) { page = doc.addPage([595, 842]); y = height - margin }
56
102
  page.drawText(line, { x: margin, y, size: fontSize, font })
57
103
  y -= fontSize * 1.55