@absolutejs/ai 0.0.58 → 0.1.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/CHANGELOG.md CHANGED
@@ -6,6 +6,21 @@ This file is generated by `absolute-changelog` from the entries in
6
6
  `changelog/`. Edit an entry, not this file — and add new ones under
7
7
  `changelog/unreleased/`.
8
8
 
9
+ ## 0.1.0 — 2026-09-16
10
+
11
+ ### Added
12
+
13
+ - **Add one-section text preparation steps for durable workers, with explicit pending/ready outcomes and checkpoint persistence before yielding** (`prepareAITextInputStep`, `AITextPreparationStep`)
14
+ - **Budget final retrieval instructions and tools before compacting text, with matching checkpoint invalidation** (`PrepareAITextInputOptions`, `prepareAITextInput`)
15
+
16
+ ### Changed
17
+
18
+ - **Validate model capacity by default across generation, structured output, WebSocket and SSE entry points; reserve token headroom and support one protocol-preserving context recovery attempt. Providers without counting capability now fail explicitly; contextPolicy: false opts into raw behavior.** (`generateAI`, `generateAIWithTools`, `generateObjectAI`, `streamAI`, `streamAIToSSE`, `streamAIWithTools`, `inspectAIContext`, `createAIStoredToolResultRecovery`, `StreamAIWithToolsOptions`, `GenerateAIOptions`, `GenerateAIResult`, `GenerateObjectAIOptions`, `AIProviderConfig`, `StreamAIOptions`, `AIChatPluginConfig`)
19
+
20
+ ### Fixed
21
+
22
+ - **Preserve conversation origin across long-text preparation sections, distinguish source facts from embedded instructions, reserve more of the available context for factual notes, and invalidate checkpoints from the previous preparation policy** (`prepareAITextInput`, `prepareAITextInputStep`)
23
+
9
24
  ## 0.0.58 — 2026-09-15
10
25
 
11
26
  ### Added
package/README.md CHANGED
@@ -358,6 +358,14 @@ for await (const chunk of provider.stream(prepared.params)) {
358
358
  }
359
359
  ```
360
360
 
361
+ When compacted requests need extra retrieval tools or different instructions,
362
+ pass them as `options.compactedContext` (`systemPrompt` and/or `tools`). These
363
+ replace the corresponding fields only on the compacted path. Preparation checks
364
+ that final context before reading the source and budgets the notes against it;
365
+ send the returned `params` without adding uncounted tools afterward. Changing the
366
+ compacted context invalidates a saved preparation checkpoint. Already-fitting
367
+ requests retain their original tools and instructions.
368
+
361
369
  Anthropic and Gemini retrieve native model metadata and count using their native
362
370
  APIs. OpenAI uses its input-token counting API and an exact, documented model
363
371
  catalog because its model-list API does not expose context limits. OpenAI
@@ -403,3 +411,67 @@ and run `bun scripts/eval-text-source.ts`. It uses synthetic text, simulates a
403
411
  small context, and verifies that original-source lookups recover later budget
404
412
  and deadline corrections omitted from the notes. It makes real, billed model
405
413
  calls; the ordinary test suite uses deterministic local providers instead.
414
+
415
+ For durable workers, `prepareAITextInputStep(provider, params, options)` processes
416
+ at most one source section and returns either `{ status: "pending", checkpoint }`
417
+ or `{ status: "ready", prepared }`. Persist originals and the exact request,
418
+ save the checkpoint under the current job attempt, and enqueue another step when
419
+ pending. The final continuation validates the assembled request without another
420
+ model call. A pending checkpoint is not a completed model input. Persistence
421
+ errors propagate before a step reports success; the caller owns atomic scheduling,
422
+ retry limits and attempt fencing. `prepareAITextInput` retains its existing
423
+ all-sections convenience behavior.
424
+
425
+ ### Automatic context policy (next breaking release)
426
+
427
+ High-level generation, structured-output, streaming and chat-plugin entry points
428
+ now validate every final model request automatically, including requests after
429
+ local tools and structured-output repair. The provider owns model metadata and
430
+ counting; unknown models or providers without that capability raise
431
+ `AIInputError` with `code: "capacity_unavailable"`. A character count is never
432
+ used as a model limit. Counting can require an additional provider API request.
433
+
434
+ The shared `contextPolicy` reserves the provider's effective output budget, plus
435
+ 1% of available input (capped at 2,048 tokens) for counting margin and, when local
436
+ tools are enabled, 10% (capped at 8,192 tokens) for tool-result growth. These are
437
+ configurable working-budget defaults, not claimed model limits or quality-optimal
438
+ settings. `workingInputTokens` can set a lower application target.
439
+ `prepareAITextInput` and `prepareAITextInputStep` use the same budgets through
440
+ `contextBudget`, so preparation does not fill headroom reserved by generation.
441
+
442
+ ```ts
443
+ const contextPolicy = {
444
+ workingInputTokens: 80_000,
445
+ // Only tools backed by immutable, authorized saved originals:
446
+ recover: createAIStoredToolResultRecovery(["search_text_source", "read_text_source"]),
447
+ };
448
+ const result = await generateAIWithTools({
449
+ provider, model, messages, tools, contextPolicy,
450
+ });
451
+ ```
452
+
453
+ The saved-source recovery helper replaces only older lookup results with explicit
454
+ reread instructions. It keeps the newest lookup, source-call arguments and every
455
+ other tool result. It never deletes originals, runs tools, invents a summary or
456
+ truncates the newest passage. Callers must ensure listed tools can reread the same
457
+ immutable versions. If this cannot fit the budget, `input_too_large` is returned.
458
+ This helper is not appropriate for unsaved or changing external results.
459
+
460
+ A custom `recover({params, capacity, reason})` may instead return compacted or
461
+ retrieved messages after saving originals. It runs at most once per model turn,
462
+ receives an isolated snapshot, and must actually reduce token usage and fit the
463
+ working budget. Tool calls/results must keep their IDs and grouping; signed
464
+ thinking, media, provider data and system messages cannot change. The recovered
465
+ history is retained across later tool turns. Recovery never reexecutes a tool.
466
+ A second rejection, unsupported error, or error after any emitted chunk propagates
467
+ without a capacity retry. Recognized provider rejection formats are deliberately
468
+ narrow; other adapters may supply `inputCapacity.isContextError`.
469
+
470
+ Migration: supply provider capacity support, or explicitly use
471
+ `contextPolicy: false` to retain raw behavior. Direct `provider.stream(params)`
472
+ remains the raw seam. The old `streamAIWithTools.validateInput` flag is deprecated;
473
+ its explicit `false` remains an opt-out unless `contextPolicy` is supplied.
474
+ `contextPolicy` also passes through `aiChat` and the compatible RAG chat plugin.
475
+ Applications should surface typed failures with saved-input retry UI. These
476
+ changes do not guarantee detection of every provider-side accounting discrepancy
477
+ or provide exactly-once external execution after a process crash.
package/changelog.json CHANGED
@@ -2,6 +2,57 @@
2
2
  "contract": 1,
3
3
  "name": "@absolutejs/ai",
4
4
  "releases": [
5
+ {
6
+ "changes": [
7
+ {
8
+ "kind": "changed",
9
+ "summary": "Validate model capacity by default across generation, structured output, WebSocket and SSE entry points; reserve token headroom and support one protocol-preserving context recovery attempt. Providers without counting capability now fail explicitly; contextPolicy: false opts into raw behavior.",
10
+ "symbols": [
11
+ "generateAI",
12
+ "generateAIWithTools",
13
+ "generateObjectAI",
14
+ "streamAI",
15
+ "streamAIToSSE",
16
+ "streamAIWithTools",
17
+ "inspectAIContext",
18
+ "createAIStoredToolResultRecovery",
19
+ "StreamAIWithToolsOptions",
20
+ "GenerateAIOptions",
21
+ "GenerateAIResult",
22
+ "GenerateObjectAIOptions",
23
+ "AIProviderConfig",
24
+ "StreamAIOptions",
25
+ "AIChatPluginConfig"
26
+ ]
27
+ },
28
+ {
29
+ "kind": "added",
30
+ "summary": "Add one-section text preparation steps for durable workers, with explicit pending/ready outcomes and checkpoint persistence before yielding",
31
+ "symbols": [
32
+ "prepareAITextInputStep",
33
+ "AITextPreparationStep"
34
+ ]
35
+ },
36
+ {
37
+ "kind": "added",
38
+ "summary": "Budget final retrieval instructions and tools before compacting text, with matching checkpoint invalidation",
39
+ "symbols": [
40
+ "PrepareAITextInputOptions",
41
+ "prepareAITextInput"
42
+ ]
43
+ },
44
+ {
45
+ "kind": "fixed",
46
+ "summary": "Preserve conversation origin across long-text preparation sections, distinguish source facts from embedded instructions, reserve more of the available context for factual notes, and invalidate checkpoints from the previous preparation policy",
47
+ "symbols": [
48
+ "prepareAITextInput",
49
+ "prepareAITextInputStep"
50
+ ]
51
+ }
52
+ ],
53
+ "date": "2026-09-16",
54
+ "version": "0.1.0"
55
+ },
5
56
  {
6
57
  "changes": [
7
58
  {