experimental-a2 0.4.0 → 0.5.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 +51 -0
- package/dist/{ai-B4YhEnfw.d.ts → ai-CFNeCrRl.d.ts} +3 -2
- package/dist/ai-CFNeCrRl.d.ts.map +1 -0
- package/dist/ai-server.browser.js +2 -0
- package/dist/ai-server.browser.js.map +1 -0
- package/dist/ai-server.d.ts +4 -3
- package/dist/ai-server.d.ts.map +1 -0
- package/dist/ai-server.js +4 -2
- package/dist/ai-server.js.map +1 -0
- package/dist/ai.d.ts +1 -1
- package/dist/ai.js +3 -1
- package/dist/ai.js.map +1 -0
- package/dist/cli-B3VuxoDe.js +2 -0
- package/dist/cli-B3VuxoDe.js.map +1 -0
- package/dist/cli-bin.js +2 -0
- package/dist/cli-bin.js.map +1 -0
- package/dist/cli.d.ts +2 -1
- package/dist/cli.d.ts.map +1 -0
- package/dist/{client-Bt4tAKi9.js → client-BKlyLiOU.js} +295 -85
- package/dist/client-BKlyLiOU.js.map +1 -0
- package/dist/{client-BrfDXQ8A.d.ts → client-D7mvIXrF.d.ts} +40 -4
- package/dist/client-D7mvIXrF.d.ts.map +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/contract-48bUMgcL.js +2 -0
- package/dist/contract-48bUMgcL.js.map +1 -0
- package/dist/contract-jIfaR085.d.ts +2 -1
- package/dist/contract-jIfaR085.d.ts.map +1 -0
- package/dist/devtools-J_jZ2vQf.d.ts +2 -1
- package/dist/devtools-J_jZ2vQf.d.ts.map +1 -0
- package/dist/devtools-kJJaORn-.js +2 -0
- package/dist/devtools-kJJaORn-.js.map +1 -0
- package/dist/devtools-server.browser.js +2 -0
- package/dist/devtools-server.browser.js.map +1 -0
- package/dist/devtools-server.d.ts +2 -1
- package/dist/devtools-server.d.ts.map +1 -0
- package/dist/devtools-server.js +2 -0
- package/dist/devtools-server.js.map +1 -0
- package/dist/errors-BQuJpe82.js +2 -0
- package/dist/errors-BQuJpe82.js.map +1 -0
- package/dist/errors-W6nwJ-fm.d.ts +2 -1
- package/dist/errors-W6nwJ-fm.d.ts.map +1 -0
- package/dist/http.d.ts +121 -72
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +503 -178
- package/dist/http.js.map +1 -0
- package/dist/idempotent-replay-DuqEkYA7.js +2 -0
- package/dist/idempotent-replay-DuqEkYA7.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/inspection-DaxB5jM2.js +2 -0
- package/dist/inspection-DaxB5jM2.js.map +1 -0
- package/dist/{internal-aEotMzu_.js → internal-DstsI6Re.js} +3 -1
- package/dist/internal-DstsI6Re.js.map +1 -0
- package/dist/otel.d.ts +2 -1
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +2 -0
- package/dist/otel.js.map +1 -0
- package/dist/platform-B4TnJtWu.js +2 -0
- package/dist/platform-B4TnJtWu.js.map +1 -0
- package/dist/react.d.ts +12 -3
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +5 -1
- package/dist/react.js.map +1 -0
- package/dist/retryable-lazy-DZWmHpii.js +2 -0
- package/dist/retryable-lazy-DZWmHpii.js.map +1 -0
- package/dist/scheduler-qstash.d.ts +4 -3
- package/dist/scheduler-qstash.d.ts.map +1 -0
- package/dist/scheduler-qstash.js +4 -2
- package/dist/scheduler-qstash.js.map +1 -0
- package/dist/scheduler-task-BpzhPnRS.js +2 -0
- package/dist/scheduler-task-BpzhPnRS.js.map +1 -0
- package/dist/scheduler-vercel.d.ts +4 -3
- package/dist/scheduler-vercel.d.ts.map +1 -0
- package/dist/scheduler-vercel.js +4 -2
- package/dist/scheduler-vercel.js.map +1 -0
- package/dist/{server-CcNnFnoW.js → server-C72KOw51.js} +100 -48
- package/dist/server-C72KOw51.js.map +1 -0
- package/dist/{server-YtPq7hjw.d.ts → server-DUF9pjsx.d.ts} +5 -4
- package/dist/server-DUF9pjsx.d.ts.map +1 -0
- package/dist/server.browser.js +2 -0
- package/dist/server.browser.js.map +1 -0
- package/dist/server.d.ts +2 -2
- package/dist/server.js +1 -1
- package/dist/{store-C3sNAaBT.d.ts → store-DysUkTH3.d.ts} +10 -1
- package/dist/store-DysUkTH3.d.ts.map +1 -0
- package/dist/store-N8PXxDAS.js +2 -0
- package/dist/store-N8PXxDAS.js.map +1 -0
- package/dist/store-codec-DTG0Ftek.js +2 -0
- package/dist/store-codec-DTG0Ftek.js.map +1 -0
- package/dist/store-memory.d.ts +3 -2
- package/dist/store-memory.d.ts.map +1 -0
- package/dist/store-memory.js +19 -11
- package/dist/store-memory.js.map +1 -0
- package/dist/{store-polling-DgrrAE3d.js → store-polling-dSeLxzfb.js} +3 -1
- package/dist/store-polling-dSeLxzfb.js.map +1 -0
- package/dist/store-postgres.d.ts +3 -2
- package/dist/store-postgres.d.ts.map +1 -0
- package/dist/store-postgres.js +57 -1
- package/dist/store-postgres.js.map +1 -0
- package/dist/{store-redis-core-DWqx3F47.js → store-redis-core-BFLwz0Wj.js} +3 -1
- package/dist/store-redis-core-BFLwz0Wj.js.map +1 -0
- package/dist/store-redis-http.d.ts +3 -2
- package/dist/store-redis-http.d.ts.map +1 -0
- package/dist/store-redis-http.js +4 -2
- package/dist/store-redis-http.js.map +1 -0
- package/dist/store-redis.d.ts +3 -2
- package/dist/store-redis.d.ts.map +1 -0
- package/dist/store-redis.js +5 -3
- package/dist/store-redis.js.map +1 -0
- package/dist/store-sqlite.d.ts +3 -2
- package/dist/store-sqlite.d.ts.map +1 -0
- package/dist/store-sqlite.js +3 -1
- package/dist/store-sqlite.js.map +1 -0
- package/dist/telemetry-BjYHTfh2.d.ts +2 -1
- package/dist/telemetry-BjYHTfh2.d.ts.map +1 -0
- package/dist/testing.browser.js +2 -0
- package/dist/testing.browser.js.map +1 -0
- package/dist/testing.d.ts +2 -1
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +2 -0
- package/dist/testing.js.map +1 -0
- package/dist/validate-XKT4FSNn.js +2 -0
- package/dist/validate-XKT4FSNn.js.map +1 -0
- package/dist/{wire-DCUZBUlT.js → wire-BFQmSJ-9.js} +77 -15
- package/dist/wire-BFQmSJ-9.js.map +1 -0
- package/docs/guides/03-react.mdx +59 -39
- package/docs/guides/06-ai-agents.mdx +5 -27
- package/docs/guides/09-presence.mdx +19 -40
- package/docs/guides/10-transports.mdx +49 -40
- package/docs/reference/01-api.mdx +107 -26
- package/docs/reference/02-errors.mdx +4 -2
- package/package.json +2 -1
- package/src/ai-coordinator.ts +358 -0
- package/src/ai-projector.ts +524 -0
- package/src/ai-sdk-step.ts +261 -0
- package/src/ai-server.browser.ts +5 -0
- package/src/ai-server.ts +1719 -0
- package/src/ai.ts +2155 -0
- package/src/cache-indexeddb.ts +10 -0
- package/src/cli-bin.ts +5 -0
- package/src/cli.ts +1046 -0
- package/src/client.ts +1826 -0
- package/src/contract.ts +206 -0
- package/src/deterministic-id.ts +72 -0
- package/src/devtools-app.ts +989 -0
- package/src/devtools-server.browser.ts +5 -0
- package/src/devtools-server.ts +604 -0
- package/src/devtools.ts +716 -0
- package/src/errors.ts +50 -0
- package/src/http.ts +394 -0
- package/src/idempotent-replay.ts +53 -0
- package/src/index.ts +37 -0
- package/src/inspection.ts +39 -0
- package/src/internal.ts +426 -0
- package/src/otel.ts +59 -0
- package/src/platform.ts +60 -0
- package/src/push-envelope.ts +137 -0
- package/src/react.ts +284 -0
- package/src/reducer.ts +108 -0
- package/src/retryable-lazy.ts +27 -0
- package/src/scheduler-qstash.ts +915 -0
- package/src/scheduler-task.ts +106 -0
- package/src/scheduler-vercel.ts +437 -0
- package/src/server.browser.ts +12 -0
- package/src/server.ts +2700 -0
- package/src/session-socket.ts +548 -0
- package/src/sse.ts +141 -0
- package/src/standard-schema.ts +77 -0
- package/src/store-codec.ts +10 -0
- package/src/store-memory.ts +788 -0
- package/src/store-polling.ts +102 -0
- package/src/store-postgres.ts +1212 -0
- package/src/store-redis-core.ts +1494 -0
- package/src/store-redis-http.ts +116 -0
- package/src/store-redis.ts +458 -0
- package/src/store-sqlite.ts +1108 -0
- package/src/store.ts +385 -0
- package/src/telemetry.ts +47 -0
- package/src/testing.browser.ts +5 -0
- package/src/testing.ts +185 -0
- package/src/validate.ts +39 -0
- package/src/wire.ts +454 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared polling stream — how poll-based backends (sqlite,
|
|
3
|
+
* postgres) implement `A2Store.stream`.
|
|
4
|
+
*
|
|
5
|
+
* The cadence is activity-adaptive: while events are flowing, polls
|
|
6
|
+
* run at a tight floor (25ms) so streams feel pushed — a token
|
|
7
|
+
* generation reads smoothly instead of clumping at the idle interval;
|
|
8
|
+
* every empty poll doubles the gap until the idle ceiling. The cadence
|
|
9
|
+
* is owned (POLL_TIMINGS in internal.ts), not a public option: the
|
|
10
|
+
* floor already keeps active streams smooth, so tuning it is a cost
|
|
11
|
+
* decision a2 makes, not one it delegates. Correctness never depends
|
|
12
|
+
* on the cadence — each poll is a catch-up read (`index > last`, in
|
|
13
|
+
* order), so nothing can be missed, only arrive a beat later.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { POLL_TIMINGS } from './internal.ts'
|
|
17
|
+
import type { StoredEvent } from './store.ts'
|
|
18
|
+
|
|
19
|
+
/** One cancellable sleep — the injection seam for cadence tests. */
|
|
20
|
+
export type Sleeper = { promise: Promise<void>; cancel: () => void }
|
|
21
|
+
|
|
22
|
+
/** Unref'd cancellable timer — shared with the redis safety read. */
|
|
23
|
+
export const defaultSleep = (ms: number): Sleeper => {
|
|
24
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
25
|
+
let wake: (() => void) | undefined
|
|
26
|
+
const promise = new Promise<void>((resolve) => {
|
|
27
|
+
wake = resolve // assigned synchronously by the executor
|
|
28
|
+
timer = setTimeout(resolve, ms)
|
|
29
|
+
// Don't hold the process open for an idle poll.
|
|
30
|
+
;(timer as { unref?: () => void }).unref?.()
|
|
31
|
+
})
|
|
32
|
+
return {
|
|
33
|
+
promise,
|
|
34
|
+
cancel: () => {
|
|
35
|
+
clearTimeout(timer)
|
|
36
|
+
wake?.()
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Read the seam lazily — tests shrink POLL_TIMINGS in their setup.
|
|
42
|
+
const ceiling = (): number => POLL_TIMINGS.idleCeilingMs
|
|
43
|
+
const floor = (): number =>
|
|
44
|
+
Math.min(POLL_TIMINGS.activeFloorMs, POLL_TIMINGS.idleCeilingMs)
|
|
45
|
+
|
|
46
|
+
export type PollingStreamOptions = {
|
|
47
|
+
/** Deliver events strictly after this index (exclusive). */
|
|
48
|
+
startAfter?: number
|
|
49
|
+
/** Injectable sleep — tests assert the cadence without wall-clock waits. */
|
|
50
|
+
sleep?: (ms: number) => Sleeper
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Build the `stream()` iterable from a backend's catch-up read. The
|
|
55
|
+
* iterable ends when the consumer calls `return()` (a disconnecting
|
|
56
|
+
* SSE client), waking any pending sleep immediately.
|
|
57
|
+
*/
|
|
58
|
+
export function pollingStream(
|
|
59
|
+
readAfter: (afterIndex: number) => StoredEvent[] | Promise<StoredEvent[]>,
|
|
60
|
+
options: PollingStreamOptions,
|
|
61
|
+
): AsyncIterable<StoredEvent> {
|
|
62
|
+
const startAfter = options.startAfter ?? 0
|
|
63
|
+
const sleep = options.sleep ?? defaultSleep
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
[Symbol.asyncIterator](): AsyncIterator<StoredEvent> {
|
|
67
|
+
let last = startAfter
|
|
68
|
+
let buffer: StoredEvent[] = []
|
|
69
|
+
let closed = false
|
|
70
|
+
let pending: Sleeper | null = null
|
|
71
|
+
let interval = floor()
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
async next(): Promise<IteratorResult<StoredEvent>> {
|
|
75
|
+
for (;;) {
|
|
76
|
+
if (closed) return { value: undefined, done: true }
|
|
77
|
+
// oxlint-disable-next-line no-await-in-loop -- poll loop
|
|
78
|
+
if (buffer.length === 0) buffer = await readAfter(last)
|
|
79
|
+
const row = buffer.shift()
|
|
80
|
+
if (row) {
|
|
81
|
+
// Activity: tighten back to the floor.
|
|
82
|
+
interval = floor()
|
|
83
|
+
last = row.index
|
|
84
|
+
return { value: row, done: false }
|
|
85
|
+
}
|
|
86
|
+
// Empty poll: wait, then back off toward the idle ceiling.
|
|
87
|
+
pending = sleep(interval)
|
|
88
|
+
// oxlint-disable-next-line no-await-in-loop -- poll loop
|
|
89
|
+
await pending.promise
|
|
90
|
+
pending = null
|
|
91
|
+
interval = Math.min(interval * 2, ceiling())
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
async return(): Promise<IteratorResult<StoredEvent>> {
|
|
95
|
+
closed = true
|
|
96
|
+
pending?.cancel()
|
|
97
|
+
return { value: undefined, done: true }
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
}
|