@absolutejs/ai 0.0.57 → 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 +21 -0
- package/README.md +104 -0
- package/changelog.json +68 -0
- package/dist/ai/index.js +476 -50
- package/dist/ai/index.js.map +14 -11
- package/dist/ai/providers/anthropic.js +14 -2
- package/dist/ai/providers/anthropic.js.map +4 -4
- package/dist/ai/providers/gemini.js +14 -2
- package/dist/ai/providers/gemini.js.map +4 -4
- package/dist/ai/providers/ollama.js +9 -1
- package/dist/ai/providers/ollama.js.map +3 -3
- package/dist/ai/providers/openai.js +14 -2
- package/dist/ai/providers/openai.js.map +4 -4
- package/dist/ai/providers/openaiCompatible.js +14 -2
- package/dist/ai/providers/openaiCompatible.js.map +4 -4
- package/dist/ai/providers/openaiResponses.js +14 -2
- package/dist/ai/providers/openaiResponses.js.map +4 -4
- package/dist/ai/providers/openrouter.js +14 -2
- package/dist/ai/providers/openrouter.js.map +4 -4
- package/dist/src/ai/contextPolicy.d.ts +46 -0
- package/dist/src/ai/generateAI.d.ts +8 -1
- package/dist/src/ai/index.d.ts +8 -2
- package/dist/src/ai/prepareTextInput.d.ts +32 -3
- package/dist/src/ai/storedToolResultRecovery.d.ts +7 -0
- package/dist/src/ai/streamAIWithTools.d.ts +4 -0
- package/dist/src/ai/textSource.d.ts +19 -0
- package/dist/types/ai.d.ts +5 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,27 @@ 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
|
+
|
|
24
|
+
## 0.0.58 — 2026-09-15
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- **Keep compacted text useful with read-only original-source search, checkpointed ingestion, and capacity-checked terminal tool loops** (`createAITextSource`, `prepareAITextInput`, `PrepareAITextInputOptions`, `streamAIWithTools`, `StreamAIWithToolsOptions`)
|
|
29
|
+
|
|
9
30
|
## 0.0.57 — 2026-09-15
|
|
10
31
|
|
|
11
32
|
### 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
|
|
@@ -371,3 +379,99 @@ multimodal histories rather than flattening them. Handle preparation errors in
|
|
|
371
379
|
the UI while preserving the original input; expose retry without clearing the
|
|
372
380
|
conversation. File upload/storage limits are application transport limits and
|
|
373
381
|
should be reported separately from model capacity.
|
|
382
|
+
|
|
383
|
+
#### Keep originals useful after compaction
|
|
384
|
+
|
|
385
|
+
`createAITextSource(messages)` builds a session-scoped, read-only source index.
|
|
386
|
+
Its `tools` map exposes `search_text_source` and `read_text_source` for use with
|
|
387
|
+
`streamAIWithTools`. Search returns bounded **verbatim** passages with stable
|
|
388
|
+
message IDs and character offsets; read can retrieve adjoining passages. This
|
|
389
|
+
is lexical search, so ask the model to try alternate terms and never treat no
|
|
390
|
+
matches as proof a fact is absent. Keep originals persisted and rebuild the
|
|
391
|
+
index on resume. The index performs no model calls or external network access.
|
|
392
|
+
|
|
393
|
+
Attach these tools when using compacted notes, and instruct the model to verify
|
|
394
|
+
numbers, dates, exceptions, and corrections in the originals before finalizing.
|
|
395
|
+
Never make the rolling summary the only accessible copy of a large document.
|
|
396
|
+
|
|
397
|
+
`prepareAITextInput` accepts a server-owned `checkpoint` and calls
|
|
398
|
+
`onCheckpoint` after each completed section. Store that checkpoint for retries;
|
|
399
|
+
its model/task and original-prefix fingerprints must match before it is reused.
|
|
400
|
+
Edited input or changed instructions invalidate it. Original text remains the
|
|
401
|
+
source of truth. Do not accept checkpoints or generated notes from a client.
|
|
402
|
+
|
|
403
|
+
`streamAIWithTools({ validateInput: true, ... })` checks every model request,
|
|
404
|
+
including accumulated tool results, before sending it. It also rejects streams
|
|
405
|
+
that end without a completion marker. `stopAfterTools: ["finish"]` stops after a
|
|
406
|
+
named tool succeeds, avoiding an extra generation or later tool side effects
|
|
407
|
+
when the application is ready to validate and persist a result.
|
|
408
|
+
|
|
409
|
+
For a bounded live regression against the intake model, set `ANTHROPIC_API_KEY`
|
|
410
|
+
and run `bun scripts/eval-text-source.ts`. It uses synthetic text, simulates a
|
|
411
|
+
small context, and verifies that original-source lookups recover later budget
|
|
412
|
+
and deadline corrections omitted from the notes. It makes real, billed model
|
|
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,74 @@
|
|
|
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
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"changes": [
|
|
58
|
+
{
|
|
59
|
+
"kind": "added",
|
|
60
|
+
"summary": "Keep compacted text useful with read-only original-source search, checkpointed ingestion, and capacity-checked terminal tool loops",
|
|
61
|
+
"symbols": [
|
|
62
|
+
"createAITextSource",
|
|
63
|
+
"prepareAITextInput",
|
|
64
|
+
"PrepareAITextInputOptions",
|
|
65
|
+
"streamAIWithTools",
|
|
66
|
+
"StreamAIWithToolsOptions"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
"date": "2026-09-15",
|
|
71
|
+
"version": "0.0.58"
|
|
72
|
+
},
|
|
5
73
|
{
|
|
6
74
|
"changes": [
|
|
7
75
|
{
|