@farukada/aws-langgraph-dynamodb-ts 1.0.0-rc.1 → 1.0.0-rc.2
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/README.md +1621 -288
- package/dist/backfill/backfill.d.ts +168 -0
- package/dist/backfill/backfill.js +393 -0
- package/dist/checkpointer/actions/delete-thread.d.ts +44 -4
- package/dist/checkpointer/actions/delete-thread.js +56 -22
- package/dist/checkpointer/actions/get-tuple.d.ts +29 -5
- package/dist/checkpointer/actions/get-tuple.js +41 -13
- package/dist/checkpointer/actions/list.d.ts +42 -10
- package/dist/checkpointer/actions/list.js +86 -57
- package/dist/checkpointer/actions/put-writes.d.ts +41 -9
- package/dist/checkpointer/actions/put-writes.js +62 -48
- package/dist/checkpointer/actions/put.d.ts +82 -10
- package/dist/checkpointer/actions/put.js +166 -44
- package/dist/checkpointer/internal/delta-history.d.ts +112 -0
- package/dist/checkpointer/internal/delta-history.js +252 -0
- package/dist/checkpointer/internal/listing.d.ts +149 -0
- package/dist/checkpointer/internal/listing.js +245 -0
- package/dist/checkpointer/internal/parse.d.ts +262 -0
- package/dist/checkpointer/internal/parse.js +372 -0
- package/dist/checkpointer/internal/pending-writes.d.ts +275 -0
- package/dist/checkpointer/internal/pending-writes.js +588 -0
- package/dist/checkpointer/internal/read.d.ts +130 -0
- package/dist/checkpointer/internal/read.js +264 -0
- package/dist/checkpointer/internal/rows.d.ts +571 -0
- package/dist/checkpointer/internal/rows.js +834 -0
- package/dist/checkpointer/internal/setup.d.ts +42 -21
- package/dist/checkpointer/internal/setup.js +65 -31
- package/dist/checkpointer/saver.d.ts +242 -42
- package/dist/checkpointer/saver.js +257 -53
- package/dist/checkpointer/types.d.ts +37 -44
- package/dist/checkpointer/types.js +10 -0
- package/dist/factory/factory.d.ts +120 -11
- package/dist/factory/factory.js +195 -16
- package/dist/factory/types.d.ts +23 -4
- package/dist/factory/types.js +8 -0
- package/dist/history/actions/add-messages.d.ts +30 -2
- package/dist/history/actions/add-messages.js +37 -57
- package/dist/history/actions/clear.d.ts +46 -4
- package/dist/history/actions/clear.js +64 -15
- package/dist/history/actions/get-messages.d.ts +41 -0
- package/dist/history/actions/get-messages.js +94 -32
- package/dist/history/actions/list-sessions.d.ts +52 -10
- package/dist/history/actions/list-sessions.js +141 -38
- package/dist/history/actions/reconcile-count.d.ts +41 -8
- package/dist/history/actions/reconcile-count.js +44 -57
- package/dist/history/chat-message-history.d.ts +200 -38
- package/dist/history/chat-message-history.js +218 -51
- package/dist/history/internal/append.d.ts +212 -0
- package/dist/history/internal/append.js +500 -0
- package/dist/history/internal/message-read.d.ts +84 -0
- package/dist/history/internal/message-read.js +204 -0
- package/dist/history/internal/parse.d.ts +153 -0
- package/dist/history/internal/parse.js +252 -0
- package/dist/history/internal/rows.d.ts +195 -0
- package/dist/history/internal/rows.js +250 -0
- package/dist/history/internal/session.d.ts +331 -0
- package/dist/history/internal/session.js +628 -0
- package/dist/history/internal/setup.d.ts +52 -19
- package/dist/history/internal/setup.js +90 -27
- package/dist/history/session-adapter.d.ts +95 -6
- package/dist/history/session-adapter.js +99 -8
- package/dist/history/types.d.ts +61 -31
- package/dist/history/types.js +10 -0
- package/dist/index.d.ts +38 -14
- package/dist/index.js +31 -12
- package/dist/shared/adapter.d.ts +135 -0
- package/dist/shared/adapter.js +143 -0
- package/dist/shared/clock.d.ts +48 -4
- package/dist/shared/clock.js +51 -4
- package/dist/shared/codec/codec.d.ts +275 -9
- package/dist/shared/codec/codec.js +394 -19
- package/dist/shared/codec/compression.d.ts +43 -6
- package/dist/shared/codec/compression.js +52 -11
- package/dist/shared/codec/json-serde.d.ts +76 -3
- package/dist/shared/codec/json-serde.js +181 -7
- package/dist/shared/codec/s3/client-types.d.ts +20 -1
- package/dist/shared/codec/s3/client-types.js +20 -1
- package/dist/shared/codec/s3/client.d.ts +35 -8
- package/dist/shared/codec/s3/client.js +61 -10
- package/dist/shared/codec/s3/config.d.ts +226 -22
- package/dist/shared/codec/s3/config.js +274 -24
- package/dist/shared/codec/s3/lifecycle.d.ts +163 -11
- package/dist/shared/codec/s3/lifecycle.js +328 -36
- package/dist/shared/codec/s3/offloader.d.ts +390 -19
- package/dist/shared/codec/s3/offloader.js +590 -50
- package/dist/shared/concurrency.d.ts +40 -3
- package/dist/shared/concurrency.js +55 -7
- package/dist/shared/dynamodb/abort.d.ts +45 -7
- package/dist/shared/dynamodb/abort.js +46 -14
- package/dist/shared/dynamodb/batch-write.d.ts +77 -13
- package/dist/shared/dynamodb/batch-write.js +146 -26
- package/dist/shared/dynamodb/cancellation.d.ts +121 -3
- package/dist/shared/dynamodb/cancellation.js +147 -2
- package/dist/shared/dynamodb/client.d.ts +159 -12
- package/dist/shared/dynamodb/client.js +135 -10
- package/dist/shared/dynamodb/idempotent-write.d.ts +551 -0
- package/dist/shared/dynamodb/idempotent-write.js +593 -0
- package/dist/shared/dynamodb/paginate.d.ts +105 -8
- package/dist/shared/dynamodb/paginate.js +175 -6
- package/dist/shared/dynamodb/partition-delete.d.ts +181 -16
- package/dist/shared/dynamodb/partition-delete.js +312 -43
- package/dist/shared/dynamodb/recency-index.d.ts +231 -0
- package/dist/shared/dynamodb/recency-index.js +377 -0
- package/dist/shared/dynamodb/retry.d.ts +265 -10
- package/dist/shared/dynamodb/retry.js +426 -20
- package/dist/shared/dynamodb/table-schema.d.ts +190 -0
- package/dist/shared/dynamodb/table-schema.js +209 -0
- package/dist/shared/errors/base-error.d.ts +174 -8
- package/dist/shared/errors/base-error.js +154 -7
- package/dist/shared/errors/boundary.d.ts +63 -10
- package/dist/shared/errors/boundary.js +110 -13
- package/dist/shared/errors/classify.d.ts +97 -0
- package/dist/shared/errors/classify.js +257 -0
- package/dist/shared/errors/error-code.d.ts +76 -1
- package/dist/shared/errors/error-code.js +83 -1
- package/dist/shared/errors/errors.d.ts +157 -58
- package/dist/shared/errors/errors.js +218 -93
- package/dist/shared/logging/logger.d.ts +63 -1
- package/dist/shared/logging/logger.js +97 -2
- package/dist/shared/logging/redaction.d.ts +86 -6
- package/dist/shared/logging/redaction.js +265 -24
- package/dist/shared/logging/secret-patterns.d.ts +136 -24
- package/dist/shared/logging/secret-patterns.js +165 -30
- package/dist/shared/logging/truncate.d.ts +197 -0
- package/dist/shared/logging/truncate.js +231 -0
- package/dist/shared/options.d.ts +52 -5
- package/dist/shared/options.js +9 -0
- package/dist/shared/ulid.d.ts +71 -14
- package/dist/shared/ulid.js +77 -14
- package/dist/shared/validation/collaborators.d.ts +141 -0
- package/dist/shared/validation/collaborators.js +188 -0
- package/dist/shared/validation/option-shape.d.ts +89 -0
- package/dist/shared/validation/option-shape.js +113 -0
- package/dist/shared/validation/options.d.ts +142 -5
- package/dist/shared/validation/options.js +284 -46
- package/dist/shared/validation/primitives.d.ts +287 -24
- package/dist/shared/validation/primitives.js +347 -42
- package/dist/shared/validation/ttl.d.ts +61 -12
- package/dist/shared/validation/ttl.js +102 -22
- package/dist/store/actions/list-namespaces.d.ts +76 -5
- package/dist/store/actions/list-namespaces.js +160 -22
- package/dist/store/actions/put.d.ts +33 -7
- package/dist/store/actions/put.js +53 -71
- package/dist/store/actions/reconcile-vector-index.d.ts +28 -8
- package/dist/store/actions/reconcile-vector-index.js +33 -13
- package/dist/store/actions/search.d.ts +34 -5
- package/dist/store/actions/search.js +52 -20
- package/dist/store/internal/batch-plan.d.ts +24 -9
- package/dist/store/internal/batch-plan.js +91 -34
- package/dist/store/internal/filter.d.ts +36 -2
- package/dist/store/internal/filter.js +66 -14
- package/dist/store/internal/get-item.d.ts +45 -0
- package/dist/store/internal/get-item.js +115 -0
- package/dist/store/internal/item-write.d.ts +230 -0
- package/dist/store/internal/item-write.js +463 -0
- package/dist/store/internal/parse.d.ts +225 -0
- package/dist/store/internal/parse.js +350 -0
- package/dist/store/internal/rows.d.ts +355 -0
- package/dist/store/internal/rows.js +447 -0
- package/dist/store/internal/semantic-search.d.ts +146 -12
- package/dist/store/internal/semantic-search.js +317 -15
- package/dist/store/internal/setup.d.ts +86 -21
- package/dist/store/internal/setup.js +209 -26
- package/dist/store/internal/table-search.d.ts +100 -0
- package/dist/store/internal/table-search.js +213 -0
- package/dist/store/internal/vector-index.d.ts +247 -0
- package/dist/store/internal/vector-index.js +546 -0
- package/dist/store/store.d.ts +258 -33
- package/dist/store/store.js +309 -53
- package/dist/store/types.d.ts +74 -28
- package/dist/store/types.js +13 -0
- package/dist/store/vector-backend.d.ts +64 -3
- package/dist/store/vector-backend.js +15 -0
- package/package.json +41 -25
- package/dist/checkpointer/internal/assemble.d.ts +0 -17
- package/dist/checkpointer/internal/assemble.js +0 -38
- package/dist/checkpointer/internal/checkpoint-write-verify.d.ts +0 -24
- package/dist/checkpointer/internal/checkpoint-write-verify.js +0 -67
- package/dist/checkpointer/internal/configurable.d.ts +0 -12
- package/dist/checkpointer/internal/configurable.js +0 -24
- package/dist/checkpointer/internal/fetch.d.ts +0 -19
- package/dist/checkpointer/internal/fetch.js +0 -88
- package/dist/checkpointer/internal/filter-match.d.ts +0 -13
- package/dist/checkpointer/internal/filter-match.js +0 -15
- package/dist/checkpointer/internal/item-reader.d.ts +0 -66
- package/dist/checkpointer/internal/item-reader.js +0 -113
- package/dist/checkpointer/internal/item-writer.d.ts +0 -35
- package/dist/checkpointer/internal/item-writer.js +0 -107
- package/dist/checkpointer/internal/keys.d.ts +0 -41
- package/dist/checkpointer/internal/keys.js +0 -117
- package/dist/checkpointer/internal/list-scope.d.ts +0 -61
- package/dist/checkpointer/internal/list-scope.js +0 -108
- package/dist/checkpointer/internal/pending-sends.d.ts +0 -13
- package/dist/checkpointer/internal/pending-sends.js +0 -29
- package/dist/checkpointer/internal/query.d.ts +0 -21
- package/dist/checkpointer/internal/query.js +0 -42
- package/dist/checkpointer/internal/regular-write.d.ts +0 -28
- package/dist/checkpointer/internal/regular-write.js +0 -66
- package/dist/checkpointer/internal/special-write-cas.d.ts +0 -29
- package/dist/checkpointer/internal/special-write-cas.js +0 -104
- package/dist/checkpointer/internal/special-write-cleanup.d.ts +0 -23
- package/dist/checkpointer/internal/special-write-cleanup.js +0 -50
- package/dist/checkpointer/internal/special-write-verify.d.ts +0 -53
- package/dist/checkpointer/internal/special-write-verify.js +0 -80
- package/dist/checkpointer/internal/stored-channels.d.ts +0 -31
- package/dist/checkpointer/internal/stored-channels.js +0 -70
- package/dist/checkpointer/internal/validation.d.ts +0 -18
- package/dist/checkpointer/internal/validation.js +0 -40
- package/dist/checkpointer/internal/write-guard.d.ts +0 -20
- package/dist/checkpointer/internal/write-guard.js +0 -48
- package/dist/checkpointer/internal/write-index.d.ts +0 -36
- package/dist/checkpointer/internal/write-index.js +0 -41
- package/dist/history/internal/append-saga.d.ts +0 -25
- package/dist/history/internal/append-saga.js +0 -79
- package/dist/history/internal/compensation.d.ts +0 -25
- package/dist/history/internal/compensation.js +0 -92
- package/dist/history/internal/item-mapper.d.ts +0 -9
- package/dist/history/internal/item-mapper.js +0 -27
- package/dist/history/internal/keys.d.ts +0 -16
- package/dist/history/internal/keys.js +0 -48
- package/dist/history/internal/message-chunker.d.ts +0 -13
- package/dist/history/internal/message-chunker.js +0 -67
- package/dist/history/internal/message-transaction.d.ts +0 -25
- package/dist/history/internal/message-transaction.js +0 -61
- package/dist/history/internal/message-window.d.ts +0 -17
- package/dist/history/internal/message-window.js +0 -49
- package/dist/history/internal/query.d.ts +0 -23
- package/dist/history/internal/query.js +0 -39
- package/dist/history/internal/session-count.d.ts +0 -47
- package/dist/history/internal/session-count.js +0 -115
- package/dist/history/internal/session-title.d.ts +0 -19
- package/dist/history/internal/session-title.js +0 -43
- package/dist/history/internal/session-update.d.ts +0 -27
- package/dist/history/internal/session-update.js +0 -69
- package/dist/history/internal/title-generator.d.ts +0 -12
- package/dist/history/internal/title-generator.js +0 -41
- package/dist/history/internal/ttl-anchor.d.ts +0 -24
- package/dist/history/internal/ttl-anchor.js +0 -38
- package/dist/history/internal/validation.d.ts +0 -25
- package/dist/history/internal/validation.js +0 -53
- package/dist/shared/codec/descriptor-keys.d.ts +0 -12
- package/dist/shared/codec/descriptor-keys.js +0 -14
- package/dist/shared/codec/encode.d.ts +0 -13
- package/dist/shared/codec/encode.js +0 -43
- package/dist/shared/codec/payload-loss.d.ts +0 -12
- package/dist/shared/codec/payload-loss.js +0 -38
- package/dist/shared/codec/s3/adapter-config.d.ts +0 -13
- package/dist/shared/codec/s3/adapter-config.js +0 -22
- package/dist/shared/codec/s3/bounded-body.d.ts +0 -14
- package/dist/shared/codec/s3/bounded-body.js +0 -47
- package/dist/shared/codec/s3/delete.d.ts +0 -7
- package/dist/shared/codec/s3/delete.js +0 -28
- package/dist/shared/codec/s3/key-scope.d.ts +0 -23
- package/dist/shared/codec/s3/key-scope.js +0 -44
- package/dist/shared/codec/s3/orphans.d.ts +0 -23
- package/dist/shared/codec/s3/orphans.js +0 -71
- package/dist/shared/codec/s3/read-write.d.ts +0 -18
- package/dist/shared/codec/s3/read-write.js +0 -51
- package/dist/shared/codec/s3/retry.d.ts +0 -5
- package/dist/shared/codec/s3/retry.js +0 -22
- package/dist/shared/constants.d.ts +0 -112
- package/dist/shared/constants.js +0 -115
- package/dist/shared/dynamodb/backoff.d.ts +0 -15
- package/dist/shared/dynamodb/backoff.js +0 -48
- package/dist/shared/dynamodb/conditional-put.d.ts +0 -67
- package/dist/shared/dynamodb/conditional-put.js +0 -76
- package/dist/shared/dynamodb/drain-unprocessed.d.ts +0 -21
- package/dist/shared/dynamodb/drain-unprocessed.js +0 -43
- package/dist/shared/dynamodb/expiry.d.ts +0 -16
- package/dist/shared/dynamodb/expiry.js +0 -29
- package/dist/shared/dynamodb/paginate-core.d.ts +0 -24
- package/dist/shared/dynamodb/paginate-core.js +0 -82
- package/dist/shared/dynamodb/retry-classifier.d.ts +0 -17
- package/dist/shared/dynamodb/retry-classifier.js +0 -118
- package/dist/shared/dynamodb/retry-policy.d.ts +0 -26
- package/dist/shared/dynamodb/retry-policy.js +0 -22
- package/dist/shared/dynamodb/scan.d.ts +0 -14
- package/dist/shared/dynamodb/scan.js +0 -19
- package/dist/shared/dynamodb/types.d.ts +0 -23
- package/dist/shared/dynamodb/types.js +0 -2
- package/dist/shared/errors/upstream-error.d.ts +0 -16
- package/dist/shared/errors/upstream-error.js +0 -27
- package/dist/shared/errors/wrap-error.d.ts +0 -7
- package/dist/shared/errors/wrap-error.js +0 -15
- package/dist/shared/logging/redaction-walk.d.ts +0 -23
- package/dist/shared/logging/redaction-walk.js +0 -112
- package/dist/store/actions/get.d.ts +0 -13
- package/dist/store/actions/get.js +0 -73
- package/dist/store/internal/backend-search.d.ts +0 -4
- package/dist/store/internal/backend-search.js +0 -74
- package/dist/store/internal/candidates.d.ts +0 -23
- package/dist/store/internal/candidates.js +0 -94
- package/dist/store/internal/index-reconcile.d.ts +0 -21
- package/dist/store/internal/index-reconcile.js +0 -118
- package/dist/store/internal/index-sync.d.ts +0 -10
- package/dist/store/internal/index-sync.js +0 -25
- package/dist/store/internal/item-mapper.d.ts +0 -27
- package/dist/store/internal/item-mapper.js +0 -61
- package/dist/store/internal/keys.d.ts +0 -17
- package/dist/store/internal/keys.js +0 -41
- package/dist/store/internal/namespace-match.d.ts +0 -11
- package/dist/store/internal/namespace-match.js +0 -40
- package/dist/store/internal/option-validation.d.ts +0 -13
- package/dist/store/internal/option-validation.js +0 -77
- package/dist/store/internal/overwrite-swap.d.ts +0 -32
- package/dist/store/internal/overwrite-swap.js +0 -68
- package/dist/store/internal/persist.d.ts +0 -28
- package/dist/store/internal/persist.js +0 -64
- package/dist/store/internal/query.d.ts +0 -11
- package/dist/store/internal/query.js +0 -48
- package/dist/store/internal/ranker.d.ts +0 -18
- package/dist/store/internal/ranker.js +0 -39
- package/dist/store/internal/read-existing.d.ts +0 -23
- package/dist/store/internal/read-existing.js +0 -41
- package/dist/store/internal/score-direction.d.ts +0 -31
- package/dist/store/internal/score-direction.js +0 -38
- package/dist/store/internal/search-filter.d.ts +0 -3
- package/dist/store/internal/search-filter.js +0 -10
- package/dist/store/internal/text-path.d.ts +0 -17
- package/dist/store/internal/text-path.js +0 -147
- package/dist/store/internal/validation.d.ts +0 -18
- package/dist/store/internal/validation.js +0 -51
- package/dist/store/internal/write-verify.d.ts +0 -42
- package/dist/store/internal/write-verify.js +0 -71
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { CancelOptions } from '../../shared/options';
|
|
2
|
-
import type { ChatMessageItem, MessageWindow } from '../types';
|
|
3
|
-
import type { HistoryContext } from './setup';
|
|
4
|
-
/**
|
|
5
|
-
* Read the live message items a window selects, in chronological order.
|
|
6
|
-
*
|
|
7
|
-
* Without `limit` the query walks the session oldest-first. With it the query
|
|
8
|
-
* walks newest-first with a matching page cap and stops as soon as `limit`
|
|
9
|
-
* live items are in hand — rows past their TTL are skipped here, so a page can
|
|
10
|
-
* come back short and the walk simply continues — and the tail is then
|
|
11
|
-
* reversed back into chronological order. `before` becomes an exclusive upper
|
|
12
|
-
* sort-key bound: the message prefix plus the ULID time characters of that
|
|
13
|
-
* instant, which every message id from that millisecond onwards sorts after.
|
|
14
|
-
* Reads are strongly consistent so the turn just appended is visible to the
|
|
15
|
-
* very next read.
|
|
16
|
-
*/
|
|
17
|
-
export declare function readWindow(context: HistoryContext, sessionId: string, options: MessageWindow & CancelOptions): Promise<ChatMessageItem[]>;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readWindow = readWindow;
|
|
4
|
-
const clock_1 = require("../../shared/clock");
|
|
5
|
-
const expiry_1 = require("../../shared/dynamodb/expiry");
|
|
6
|
-
const paginate_1 = require("../../shared/dynamodb/paginate");
|
|
7
|
-
const retry_policy_1 = require("../../shared/dynamodb/retry-policy");
|
|
8
|
-
const ulid_1 = require("../../shared/ulid");
|
|
9
|
-
const keys_1 = require("./keys");
|
|
10
|
-
const query_1 = require("./query");
|
|
11
|
-
/**
|
|
12
|
-
* Read the live message items a window selects, in chronological order.
|
|
13
|
-
*
|
|
14
|
-
* Without `limit` the query walks the session oldest-first. With it the query
|
|
15
|
-
* walks newest-first with a matching page cap and stops as soon as `limit`
|
|
16
|
-
* live items are in hand — rows past their TTL are skipped here, so a page can
|
|
17
|
-
* come back short and the walk simply continues — and the tail is then
|
|
18
|
-
* reversed back into chronological order. `before` becomes an exclusive upper
|
|
19
|
-
* sort-key bound: the message prefix plus the ULID time characters of that
|
|
20
|
-
* instant, which every message id from that millisecond onwards sorts after.
|
|
21
|
-
* Reads are strongly consistent so the turn just appended is visible to the
|
|
22
|
-
* very next read.
|
|
23
|
-
*/
|
|
24
|
-
async function readWindow(context, sessionId, options) {
|
|
25
|
-
const now = (0, clock_1.nowSeconds)();
|
|
26
|
-
const limit = options.limit ?? Number.POSITIVE_INFINITY;
|
|
27
|
-
const items = [];
|
|
28
|
-
for await (const raw of (0, paginate_1.paginateQuery)({
|
|
29
|
-
retry: (0, retry_policy_1.retryFor)(context, options.signal),
|
|
30
|
-
signal: options.signal,
|
|
31
|
-
client: context.client,
|
|
32
|
-
params: (0, query_1.messageQuery)(context.tableName, sessionId, {
|
|
33
|
-
consistent: true,
|
|
34
|
-
descending: options.limit !== undefined,
|
|
35
|
-
limit: options.limit,
|
|
36
|
-
beforeSortKey: options.before && (0, keys_1.messageSortKey)((0, ulid_1.ulidTimePrefix)(options.before.getTime())),
|
|
37
|
-
}),
|
|
38
|
-
maxItems: Number.POSITIVE_INFINITY,
|
|
39
|
-
maxIterations: Number.POSITIVE_INFINITY,
|
|
40
|
-
})) {
|
|
41
|
-
const item = raw;
|
|
42
|
-
if ((0, expiry_1.isExpiredRow)(item, now))
|
|
43
|
-
continue;
|
|
44
|
-
items.push(item);
|
|
45
|
-
if (items.length >= limit)
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
return options.limit === undefined ? items : items.reverse();
|
|
49
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { QueryCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
2
|
-
/** Options for {@link sessionItemsQuery}. */
|
|
3
|
-
export interface SessionItemsQueryOptions {
|
|
4
|
-
consistent?: boolean;
|
|
5
|
-
}
|
|
6
|
-
/** Query input selecting every item in a session's partition (messages + metadata). */
|
|
7
|
-
export declare function sessionItemsQuery(tableName: string, sessionId: string, options?: SessionItemsQueryOptions): QueryCommandInput;
|
|
8
|
-
/** Options for {@link messageQuery}. */
|
|
9
|
-
export interface MessageQueryOptions extends SessionItemsQueryOptions {
|
|
10
|
-
/** Walk the messages newest-first; the caller restores chronological order. */
|
|
11
|
-
descending?: boolean;
|
|
12
|
-
/** Cap the rows DynamoDB evaluates per page. */
|
|
13
|
-
limit?: number;
|
|
14
|
-
/**
|
|
15
|
-
* Exclusive upper sort-key bound. Expressed as `BETWEEN prefix AND bound`
|
|
16
|
-
* because a key condition allows one sort-key operator: the bound is the
|
|
17
|
-
* message prefix plus the ULID time characters of an instant, so every
|
|
18
|
-
* real message key from that millisecond onwards sorts strictly after it.
|
|
19
|
-
*/
|
|
20
|
-
beforeSortKey?: string;
|
|
21
|
-
}
|
|
22
|
-
/** Query input selecting a session's message items, chronological unless `descending`. */
|
|
23
|
-
export declare function messageQuery(tableName: string, sessionId: string, options?: MessageQueryOptions): QueryCommandInput;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sessionItemsQuery = sessionItemsQuery;
|
|
4
|
-
exports.messageQuery = messageQuery;
|
|
5
|
-
const keys_1 = require("./keys");
|
|
6
|
-
/** Query input selecting every item in a session's partition (messages + metadata). */
|
|
7
|
-
function sessionItemsQuery(tableName, sessionId, options = {}) {
|
|
8
|
-
const params = {
|
|
9
|
-
TableName: tableName,
|
|
10
|
-
KeyConditionExpression: '#pk = :pk',
|
|
11
|
-
ExpressionAttributeNames: { '#pk': 'PK' },
|
|
12
|
-
ExpressionAttributeValues: { ':pk': (0, keys_1.sessionPartition)(sessionId) },
|
|
13
|
-
};
|
|
14
|
-
if (options.consistent)
|
|
15
|
-
params.ConsistentRead = true;
|
|
16
|
-
return params;
|
|
17
|
-
}
|
|
18
|
-
/** Query input selecting a session's message items, chronological unless `descending`. */
|
|
19
|
-
function messageQuery(tableName, sessionId, options = {}) {
|
|
20
|
-
const params = {
|
|
21
|
-
TableName: tableName,
|
|
22
|
-
KeyConditionExpression: '#pk = :pk AND begins_with(#sk, :skp)',
|
|
23
|
-
ExpressionAttributeNames: { '#pk': 'PK', '#sk': 'SK' },
|
|
24
|
-
ExpressionAttributeValues: {
|
|
25
|
-
':pk': (0, keys_1.sessionPartition)(sessionId),
|
|
26
|
-
':skp': (0, keys_1.messageSortKeyPrefix)(),
|
|
27
|
-
},
|
|
28
|
-
ScanIndexForward: !options.descending,
|
|
29
|
-
};
|
|
30
|
-
if (options.beforeSortKey !== undefined) {
|
|
31
|
-
params.KeyConditionExpression = '#pk = :pk AND #sk BETWEEN :skp AND :before';
|
|
32
|
-
params.ExpressionAttributeValues[':before'] = options.beforeSortKey;
|
|
33
|
-
}
|
|
34
|
-
if (options.limit !== undefined)
|
|
35
|
-
params.Limit = options.limit;
|
|
36
|
-
if (options.consistent)
|
|
37
|
-
params.ConsistentRead = true;
|
|
38
|
-
return params;
|
|
39
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { HistoryContext } from './setup';
|
|
2
|
-
/**
|
|
3
|
-
* Subtract a previously-added count from the session, leaving it consistent.
|
|
4
|
-
* Guarded so a concurrently-deleted SESSION row is never resurrected as a
|
|
5
|
-
* permanent, ttl-less junk row: if the row is already gone there is nothing
|
|
6
|
-
* to revert, so that specific condition failure is swallowed rather than
|
|
7
|
-
* surfaced — this runs only from an already-in-progress rollback, where a
|
|
8
|
-
* spurious error for a no-op would misrepresent what happened.
|
|
9
|
-
*
|
|
10
|
-
* The same condition also pins the *incarnation*: `createdAt <= createdBefore`
|
|
11
|
-
* (this call's own append timestamp). A session `clear()`-ed and re-created by
|
|
12
|
-
* another caller between this call's commit and its rollback carries a later
|
|
13
|
-
* `createdAt`; decrementing it would corrupt the new incarnation's count, and
|
|
14
|
-
* its rows were never this call's to revert. That rejection is swallowed too,
|
|
15
|
-
* for the same reason as a vanished row.
|
|
16
|
-
*
|
|
17
|
-
* Deliberately does not revert a `forceTtlRefresh`-driven ttl SET from an
|
|
18
|
-
* earlier committed chunk: the healed anchor is never shorter than what
|
|
19
|
-
* was there before, so leaving it in place after a rollback only means the
|
|
20
|
-
* session's metadata row outlives its content a bit longer than ideal —
|
|
21
|
-
* self-healing (the next successful append, or DynamoDB's own TTL sweep,
|
|
22
|
-
* resolves it), unlike reverting, which would need to re-check for a
|
|
23
|
-
* concurrent legitimate extension to avoid regressing it. See README.md's
|
|
24
|
-
* "TTL expiry" section.
|
|
25
|
-
*/
|
|
26
|
-
export declare function revertSessionCount(context: HistoryContext, sessionId: string, delta: number, createdBefore: string): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* Undo a rolled-back append's effect on the session row.
|
|
29
|
-
*
|
|
30
|
-
* When this call is the one that created the row — `createdAt` still equals
|
|
31
|
-
* this call's timestamp, and the only messages counted on it are the ones
|
|
32
|
-
* being reverted — the whole row is deleted. Without that, a failed first
|
|
33
|
-
* append left a "ghost session": `title`, `createdAt` and `sessionId` are all
|
|
34
|
-
* written via `if_not_exists`, so they were never reverted and never set
|
|
35
|
-
* again, leaving `listSessions()` reporting a session with `messageCount: 0`
|
|
36
|
-
* whose title still held up to 80 characters of a message the caller was told
|
|
37
|
-
* had not persisted, with no API to clear it.
|
|
38
|
-
*
|
|
39
|
-
* Both conditions are load-bearing. `createdAt = :now` establishes that this
|
|
40
|
-
* call created the row; `messageCount = :total` establishes that nothing else
|
|
41
|
-
* has added to it since. A concurrent append to the same brand-new session
|
|
42
|
-
* fails the count check, because deleting the row would destroy that caller's
|
|
43
|
-
* committed messages — so it falls through to the plain decrement, and then
|
|
44
|
-
* strips just the title this call contributed, which is the only part of the
|
|
45
|
-
* row still carrying rolled-back message content.
|
|
46
|
-
*/
|
|
47
|
-
export declare function revertSessionCreation(context: HistoryContext, sessionId: string, total: number, createdAt: string, title?: string): Promise<void>;
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.revertSessionCount = revertSessionCount;
|
|
4
|
-
exports.revertSessionCreation = revertSessionCreation;
|
|
5
|
-
const node_crypto_1 = require("node:crypto");
|
|
6
|
-
const cancellation_1 = require("../../shared/dynamodb/cancellation");
|
|
7
|
-
const retry_1 = require("../../shared/dynamodb/retry");
|
|
8
|
-
const keys_1 = require("./keys");
|
|
9
|
-
const session_title_1 = require("./session-title");
|
|
10
|
-
/**
|
|
11
|
-
* True when a TransactWriteItems cancellation was caused by a
|
|
12
|
-
* ConditionalCheckFailed reason. Named distinctly from
|
|
13
|
-
* checkpointer/actions/put-writes.ts's isConditionalCheckFailed, which
|
|
14
|
-
* checks a different thing entirely (a raw PutItem exception name, not a
|
|
15
|
-
* transaction cancellation reason) — this function had that same name
|
|
16
|
-
* until now, a real trap for whoever read one assuming it was the other.
|
|
17
|
-
*/
|
|
18
|
-
function isCancelledByCondition(error) {
|
|
19
|
-
const reasons = (0, cancellation_1.getCancellationReasons)(error);
|
|
20
|
-
return reasons?.[0]?.Code === 'ConditionalCheckFailed';
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Subtract a previously-added count from the session, leaving it consistent.
|
|
24
|
-
* Guarded so a concurrently-deleted SESSION row is never resurrected as a
|
|
25
|
-
* permanent, ttl-less junk row: if the row is already gone there is nothing
|
|
26
|
-
* to revert, so that specific condition failure is swallowed rather than
|
|
27
|
-
* surfaced — this runs only from an already-in-progress rollback, where a
|
|
28
|
-
* spurious error for a no-op would misrepresent what happened.
|
|
29
|
-
*
|
|
30
|
-
* The same condition also pins the *incarnation*: `createdAt <= createdBefore`
|
|
31
|
-
* (this call's own append timestamp). A session `clear()`-ed and re-created by
|
|
32
|
-
* another caller between this call's commit and its rollback carries a later
|
|
33
|
-
* `createdAt`; decrementing it would corrupt the new incarnation's count, and
|
|
34
|
-
* its rows were never this call's to revert. That rejection is swallowed too,
|
|
35
|
-
* for the same reason as a vanished row.
|
|
36
|
-
*
|
|
37
|
-
* Deliberately does not revert a `forceTtlRefresh`-driven ttl SET from an
|
|
38
|
-
* earlier committed chunk: the healed anchor is never shorter than what
|
|
39
|
-
* was there before, so leaving it in place after a rollback only means the
|
|
40
|
-
* session's metadata row outlives its content a bit longer than ideal —
|
|
41
|
-
* self-healing (the next successful append, or DynamoDB's own TTL sweep,
|
|
42
|
-
* resolves it), unlike reverting, which would need to re-check for a
|
|
43
|
-
* concurrent legitimate extension to avoid regressing it. See README.md's
|
|
44
|
-
* "TTL expiry" section.
|
|
45
|
-
*/
|
|
46
|
-
async function revertSessionCount(context, sessionId, delta, createdBefore) {
|
|
47
|
-
if (delta === 0)
|
|
48
|
-
return;
|
|
49
|
-
const update = {
|
|
50
|
-
TableName: context.tableName,
|
|
51
|
-
Key: { PK: (0, keys_1.sessionPartition)(sessionId), SK: keys_1.SESSION_SORT_KEY },
|
|
52
|
-
UpdateExpression: 'ADD #count :neg',
|
|
53
|
-
ConditionExpression: 'attribute_exists(PK) AND #c <= :now',
|
|
54
|
-
ExpressionAttributeNames: { '#count': 'messageCount', '#c': 'createdAt' },
|
|
55
|
-
ExpressionAttributeValues: { ':neg': -delta, ':now': createdBefore },
|
|
56
|
-
};
|
|
57
|
-
const input = { TransactItems: [{ Update: update }], ClientRequestToken: (0, node_crypto_1.randomUUID)() };
|
|
58
|
-
try {
|
|
59
|
-
await (0, retry_1.withDynamoDBRetry)(() => context.client.transactWrite(input), context.retry);
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
if (isCancelledByCondition(error))
|
|
63
|
-
return;
|
|
64
|
-
throw error;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Undo a rolled-back append's effect on the session row.
|
|
69
|
-
*
|
|
70
|
-
* When this call is the one that created the row — `createdAt` still equals
|
|
71
|
-
* this call's timestamp, and the only messages counted on it are the ones
|
|
72
|
-
* being reverted — the whole row is deleted. Without that, a failed first
|
|
73
|
-
* append left a "ghost session": `title`, `createdAt` and `sessionId` are all
|
|
74
|
-
* written via `if_not_exists`, so they were never reverted and never set
|
|
75
|
-
* again, leaving `listSessions()` reporting a session with `messageCount: 0`
|
|
76
|
-
* whose title still held up to 80 characters of a message the caller was told
|
|
77
|
-
* had not persisted, with no API to clear it.
|
|
78
|
-
*
|
|
79
|
-
* Both conditions are load-bearing. `createdAt = :now` establishes that this
|
|
80
|
-
* call created the row; `messageCount = :total` establishes that nothing else
|
|
81
|
-
* has added to it since. A concurrent append to the same brand-new session
|
|
82
|
-
* fails the count check, because deleting the row would destroy that caller's
|
|
83
|
-
* committed messages — so it falls through to the plain decrement, and then
|
|
84
|
-
* strips just the title this call contributed, which is the only part of the
|
|
85
|
-
* row still carrying rolled-back message content.
|
|
86
|
-
*/
|
|
87
|
-
async function revertSessionCreation(context, sessionId, total, createdAt, title) {
|
|
88
|
-
if (total === 0)
|
|
89
|
-
return;
|
|
90
|
-
const input = {
|
|
91
|
-
TransactItems: [
|
|
92
|
-
{
|
|
93
|
-
Delete: {
|
|
94
|
-
TableName: context.tableName,
|
|
95
|
-
Key: { PK: (0, keys_1.sessionPartition)(sessionId), SK: keys_1.SESSION_SORT_KEY },
|
|
96
|
-
ConditionExpression: '#count = :total AND #c = :now',
|
|
97
|
-
ExpressionAttributeNames: { '#count': 'messageCount', '#c': 'createdAt' },
|
|
98
|
-
ExpressionAttributeValues: { ':total': total, ':now': createdAt },
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
],
|
|
102
|
-
ClientRequestToken: (0, node_crypto_1.randomUUID)(),
|
|
103
|
-
};
|
|
104
|
-
try {
|
|
105
|
-
await (0, retry_1.withDynamoDBRetry)(() => context.client.transactWrite(input), context.retry);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
if (!isCancelledByCondition(error))
|
|
110
|
-
throw error;
|
|
111
|
-
}
|
|
112
|
-
await revertSessionCount(context, sessionId, total, createdAt);
|
|
113
|
-
if (title !== undefined)
|
|
114
|
-
await (0, session_title_1.removeRolledBackTitle)(context, sessionId, createdAt, title);
|
|
115
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { HistoryContext } from './setup';
|
|
2
|
-
/**
|
|
3
|
-
* Strip a title this call contributed to a session row it created, when the
|
|
4
|
-
* row itself could not be deleted because a concurrent append has since added
|
|
5
|
-
* messages to it.
|
|
6
|
-
*
|
|
7
|
-
* Without this, that narrow window reopens exactly the leak C4 closes: the
|
|
8
|
-
* title is derived from the first human message of an append the caller was
|
|
9
|
-
* told had failed, and `if_not_exists` means nothing ever overwrites it — so
|
|
10
|
-
* a row that now belongs to a different caller keeps up to 80 characters of
|
|
11
|
-
* rolled-back message content.
|
|
12
|
-
*
|
|
13
|
-
* Both guards are load-bearing. `createdAt = :now` establishes that this call
|
|
14
|
-
* created the row, and `title = :title` that the title on it is still the one
|
|
15
|
-
* this call wrote — so a pre-existing title, or one a concurrent caller won
|
|
16
|
-
* the `if_not_exists` race for, is never removed. A condition rejection means
|
|
17
|
-
* exactly that and is not an error; anything else is.
|
|
18
|
-
*/
|
|
19
|
-
export declare function removeRolledBackTitle(context: HistoryContext, sessionId: string, createdAt: string, title: string): Promise<void>;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.removeRolledBackTitle = removeRolledBackTitle;
|
|
4
|
-
const retry_1 = require("../../shared/dynamodb/retry");
|
|
5
|
-
const keys_1 = require("./keys");
|
|
6
|
-
/** True when a plain UpdateItem was turned away by its ConditionExpression. */
|
|
7
|
-
function isConditionRejected(error) {
|
|
8
|
-
return error.name === 'ConditionalCheckFailedException';
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Strip a title this call contributed to a session row it created, when the
|
|
12
|
-
* row itself could not be deleted because a concurrent append has since added
|
|
13
|
-
* messages to it.
|
|
14
|
-
*
|
|
15
|
-
* Without this, that narrow window reopens exactly the leak C4 closes: the
|
|
16
|
-
* title is derived from the first human message of an append the caller was
|
|
17
|
-
* told had failed, and `if_not_exists` means nothing ever overwrites it — so
|
|
18
|
-
* a row that now belongs to a different caller keeps up to 80 characters of
|
|
19
|
-
* rolled-back message content.
|
|
20
|
-
*
|
|
21
|
-
* Both guards are load-bearing. `createdAt = :now` establishes that this call
|
|
22
|
-
* created the row, and `title = :title` that the title on it is still the one
|
|
23
|
-
* this call wrote — so a pre-existing title, or one a concurrent caller won
|
|
24
|
-
* the `if_not_exists` race for, is never removed. A condition rejection means
|
|
25
|
-
* exactly that and is not an error; anything else is.
|
|
26
|
-
*/
|
|
27
|
-
async function removeRolledBackTitle(context, sessionId, createdAt, title) {
|
|
28
|
-
try {
|
|
29
|
-
await (0, retry_1.withDynamoDBRetry)(() => context.client.update({
|
|
30
|
-
TableName: context.tableName,
|
|
31
|
-
Key: { PK: (0, keys_1.sessionPartition)(sessionId), SK: keys_1.SESSION_SORT_KEY },
|
|
32
|
-
UpdateExpression: 'REMOVE #title',
|
|
33
|
-
ConditionExpression: '#c = :now AND #title = :title',
|
|
34
|
-
ExpressionAttributeNames: { '#title': 'title', '#c': 'createdAt' },
|
|
35
|
-
ExpressionAttributeValues: { ':now': createdAt, ':title': title },
|
|
36
|
-
}), context.retry);
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
if (isConditionRejected(error))
|
|
40
|
-
return;
|
|
41
|
-
throw error;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb';
|
|
2
|
-
/** One member of a {@link TransactWriteCommandInput} `TransactItems` list. */
|
|
3
|
-
export type HistoryTransactItem = NonNullable<TransactWriteCommandInput['TransactItems']>[number];
|
|
4
|
-
/** Fields driving the per-session metadata update inside the append transaction. */
|
|
5
|
-
export interface SessionUpdateFields {
|
|
6
|
-
sessionId: string;
|
|
7
|
-
count: number;
|
|
8
|
-
now: string;
|
|
9
|
-
title?: string;
|
|
10
|
-
ttlTimestamp?: number;
|
|
11
|
-
forceTtlRefresh?: boolean;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Build the metadata `Update` transact-item: `ADD` the message count and `SET`
|
|
15
|
-
* `updatedAt` every time, while `createdAt`, `sessionId`, `title`, and the `ttl`
|
|
16
|
-
* anchor are written once via `if_not_exists`. Folding the `ttl` anchor in here
|
|
17
|
-
* means the first append fixes one shared expiry atomically with the count, with
|
|
18
|
-
* no separate pre-write that could orphan a metadata-only row. When
|
|
19
|
-
* `forceTtlRefresh` is set (because {@link resolveTtlAnchor} found the persisted
|
|
20
|
-
* anchor missing or already expired), the `ttl` clause instead does a plain
|
|
21
|
-
* `SET`, so the SESSION row's own stale attribute actually heals instead of
|
|
22
|
-
* being permanently blocked by `if_not_exists`. When forceTtlRefresh is set,
|
|
23
|
-
* the SET is additionally guarded by a ConditionExpression so a concurrent
|
|
24
|
-
* caller's already-healed anchor can never be regressed backward — see
|
|
25
|
-
* message-transaction.ts for how a lost race is retried without forcing.
|
|
26
|
-
*/
|
|
27
|
-
export declare function buildSessionUpdateItem(tableName: string, fields: SessionUpdateFields): HistoryTransactItem;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildSessionUpdateItem = buildSessionUpdateItem;
|
|
4
|
-
const keys_1 = require("./keys");
|
|
5
|
-
/**
|
|
6
|
-
* Build the metadata `Update` transact-item: `ADD` the message count and `SET`
|
|
7
|
-
* `updatedAt` every time, while `createdAt`, `sessionId`, `title`, and the `ttl`
|
|
8
|
-
* anchor are written once via `if_not_exists`. Folding the `ttl` anchor in here
|
|
9
|
-
* means the first append fixes one shared expiry atomically with the count, with
|
|
10
|
-
* no separate pre-write that could orphan a metadata-only row. When
|
|
11
|
-
* `forceTtlRefresh` is set (because {@link resolveTtlAnchor} found the persisted
|
|
12
|
-
* anchor missing or already expired), the `ttl` clause instead does a plain
|
|
13
|
-
* `SET`, so the SESSION row's own stale attribute actually heals instead of
|
|
14
|
-
* being permanently blocked by `if_not_exists`. When forceTtlRefresh is set,
|
|
15
|
-
* the SET is additionally guarded by a ConditionExpression so a concurrent
|
|
16
|
-
* caller's already-healed anchor can never be regressed backward — see
|
|
17
|
-
* message-transaction.ts for how a lost race is retried without forcing.
|
|
18
|
-
*/
|
|
19
|
-
function buildSessionUpdateItem(tableName, fields) {
|
|
20
|
-
const names = {
|
|
21
|
-
'#count': 'messageCount',
|
|
22
|
-
'#u': 'updatedAt',
|
|
23
|
-
'#c': 'createdAt',
|
|
24
|
-
'#sid': 'sessionId',
|
|
25
|
-
};
|
|
26
|
-
const values = {
|
|
27
|
-
':n': fields.count,
|
|
28
|
-
':u': fields.now,
|
|
29
|
-
':c': fields.now,
|
|
30
|
-
':sid': fields.sessionId,
|
|
31
|
-
};
|
|
32
|
-
const sets = ['#u = :u', '#c = if_not_exists(#c, :c)', '#sid = if_not_exists(#sid, :sid)'];
|
|
33
|
-
if (fields.title !== undefined) {
|
|
34
|
-
names['#title'] = 'title';
|
|
35
|
-
values[':title'] = fields.title;
|
|
36
|
-
sets.push('#title = if_not_exists(#title, :title)');
|
|
37
|
-
}
|
|
38
|
-
let conditionExpression;
|
|
39
|
-
if (fields.ttlTimestamp !== undefined) {
|
|
40
|
-
names['#ttl'] = 'ttl';
|
|
41
|
-
values[':ttl'] = fields.ttlTimestamp;
|
|
42
|
-
if (fields.forceTtlRefresh) {
|
|
43
|
-
sets.push('#ttl = :ttl');
|
|
44
|
-
/**
|
|
45
|
-
* Guards the force-overwrite so a concurrent caller's already-healed,
|
|
46
|
-
* equal-or-later anchor can never be regressed backward by this one.
|
|
47
|
-
* `<=` (not `<`): two concurrent healers of the same stale anchor
|
|
48
|
-
* typically compute the identical target timestamp, and `<=` lets
|
|
49
|
-
* the second one succeed by re-applying the same value instead of
|
|
50
|
-
* failing the condition and paying a full transaction retry for a
|
|
51
|
-
* write that was never actually a regression.
|
|
52
|
-
*/
|
|
53
|
-
conditionExpression = 'attribute_not_exists(#ttl) OR #ttl <= :ttl';
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
sets.push('#ttl = if_not_exists(#ttl, :ttl)');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
Update: {
|
|
61
|
-
TableName: tableName,
|
|
62
|
-
Key: { PK: (0, keys_1.sessionPartition)(fields.sessionId), SK: keys_1.SESSION_SORT_KEY },
|
|
63
|
-
UpdateExpression: `ADD #count :n SET ${sets.join(', ')}`,
|
|
64
|
-
...(conditionExpression ? { ConditionExpression: conditionExpression } : {}),
|
|
65
|
-
ExpressionAttributeNames: names,
|
|
66
|
-
ExpressionAttributeValues: values,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { StoredMessage } from '@langchain/core/messages';
|
|
2
|
-
/**
|
|
3
|
-
* Derive a session title from the first human message's text content, at most
|
|
4
|
-
* {@link MAX_TITLE_LENGTH} characters including the ellipsis. Returns undefined
|
|
5
|
-
* when there is no usable text.
|
|
6
|
-
*
|
|
7
|
-
* Truncation counts *code points*, not UTF-16 code units: slicing by index
|
|
8
|
-
* could cut a surrogate pair in half, leaving a lone surrogate that no longer
|
|
9
|
-
* round-trips through UTF-8. The ellipsis is also counted against the maximum
|
|
10
|
-
* rather than appended past it.
|
|
11
|
-
*/
|
|
12
|
-
export declare function deriveTitle(messages: StoredMessage[]): string | undefined;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deriveTitle = deriveTitle;
|
|
4
|
-
const MAX_TITLE_LENGTH = 80;
|
|
5
|
-
function isTextBlock(block) {
|
|
6
|
-
const candidate = block;
|
|
7
|
-
return candidate.type === 'text' && typeof candidate.text === 'string';
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* The human-readable text of a message's `content`: the string itself, or the
|
|
11
|
-
* first `text` block of a content-block array (a multimodal message carries
|
|
12
|
-
* image and text blocks side by side). Undefined when neither yields text.
|
|
13
|
-
*/
|
|
14
|
-
function textOf(content) {
|
|
15
|
-
if (typeof content === 'string')
|
|
16
|
-
return content;
|
|
17
|
-
if (!Array.isArray(content))
|
|
18
|
-
return undefined;
|
|
19
|
-
const block = content.find((entry) => typeof entry === 'object' && entry !== null && isTextBlock(entry));
|
|
20
|
-
return block?.text;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Derive a session title from the first human message's text content, at most
|
|
24
|
-
* {@link MAX_TITLE_LENGTH} characters including the ellipsis. Returns undefined
|
|
25
|
-
* when there is no usable text.
|
|
26
|
-
*
|
|
27
|
-
* Truncation counts *code points*, not UTF-16 code units: slicing by index
|
|
28
|
-
* could cut a surrogate pair in half, leaving a lone surrogate that no longer
|
|
29
|
-
* round-trips through UTF-8. The ellipsis is also counted against the maximum
|
|
30
|
-
* rather than appended past it.
|
|
31
|
-
*/
|
|
32
|
-
function deriveTitle(messages) {
|
|
33
|
-
const firstHuman = messages.find((message) => message.type === 'human');
|
|
34
|
-
const content = firstHuman === undefined ? undefined : textOf(firstHuman.data.content);
|
|
35
|
-
if (content === undefined || content.length === 0)
|
|
36
|
-
return undefined;
|
|
37
|
-
const codePoints = [...content];
|
|
38
|
-
if (codePoints.length <= MAX_TITLE_LENGTH)
|
|
39
|
-
return content;
|
|
40
|
-
return `${codePoints.slice(0, MAX_TITLE_LENGTH - 1).join('')}…`;
|
|
41
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { HistoryContext } from './setup';
|
|
2
|
-
/** The resolved ttl anchor plus whether the persisted SESSION-row value must be force-refreshed. */
|
|
3
|
-
export interface TtlAnchorResult {
|
|
4
|
-
ttlTimestamp: number;
|
|
5
|
-
refresh: boolean;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Resolve the session's creation-anchored TTL: the value already stored on the
|
|
9
|
-
* SESSION item, if one exists AND is still in the future; otherwise the
|
|
10
|
-
* supplied `candidate`, with `refresh: true` so the caller force-overwrites
|
|
11
|
-
* the stale/missing persisted anchor instead of leaving it stuck (DynamoDB's
|
|
12
|
-
* `if_not_exists` would otherwise never correct an already-expired anchor).
|
|
13
|
-
* This is a strongly-consistent read, never a write, so it cannot leave a
|
|
14
|
-
* metadata-only orphan row when the following append transaction fails.
|
|
15
|
-
*
|
|
16
|
-
* When a stale anchor is healed, only the persisted SESSION row's `ttl` is
|
|
17
|
-
* force-refreshed — the message rows already written under the expired
|
|
18
|
-
* anchor keep their own (already-expired) `ttl` and get swept independently
|
|
19
|
-
* by DynamoDB's TTL sweep. Until that sweep runs (and until
|
|
20
|
-
* `reconcileMessageCount` repairs the count), `messageCount` can therefore be
|
|
21
|
-
* temporarily overstated relative to what `getMessages` actually returns.
|
|
22
|
-
* This is expected, not a bug.
|
|
23
|
-
*/
|
|
24
|
-
export declare function resolveTtlAnchor(context: HistoryContext, sessionId: string, candidate: number, signal?: AbortSignal): Promise<TtlAnchorResult>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveTtlAnchor = resolveTtlAnchor;
|
|
4
|
-
const clock_1 = require("../../shared/clock");
|
|
5
|
-
const retry_1 = require("../../shared/dynamodb/retry");
|
|
6
|
-
const retry_policy_1 = require("../../shared/dynamodb/retry-policy");
|
|
7
|
-
const keys_1 = require("./keys");
|
|
8
|
-
/**
|
|
9
|
-
* Resolve the session's creation-anchored TTL: the value already stored on the
|
|
10
|
-
* SESSION item, if one exists AND is still in the future; otherwise the
|
|
11
|
-
* supplied `candidate`, with `refresh: true` so the caller force-overwrites
|
|
12
|
-
* the stale/missing persisted anchor instead of leaving it stuck (DynamoDB's
|
|
13
|
-
* `if_not_exists` would otherwise never correct an already-expired anchor).
|
|
14
|
-
* This is a strongly-consistent read, never a write, so it cannot leave a
|
|
15
|
-
* metadata-only orphan row when the following append transaction fails.
|
|
16
|
-
*
|
|
17
|
-
* When a stale anchor is healed, only the persisted SESSION row's `ttl` is
|
|
18
|
-
* force-refreshed — the message rows already written under the expired
|
|
19
|
-
* anchor keep their own (already-expired) `ttl` and get swept independently
|
|
20
|
-
* by DynamoDB's TTL sweep. Until that sweep runs (and until
|
|
21
|
-
* `reconcileMessageCount` repairs the count), `messageCount` can therefore be
|
|
22
|
-
* temporarily overstated relative to what `getMessages` actually returns.
|
|
23
|
-
* This is expected, not a bug.
|
|
24
|
-
*/
|
|
25
|
-
async function resolveTtlAnchor(context, sessionId, candidate, signal) {
|
|
26
|
-
const result = await (0, retry_1.withDynamoDBRetry)(() => context.client.get({
|
|
27
|
-
TableName: context.tableName,
|
|
28
|
-
Key: { PK: (0, keys_1.sessionPartition)(sessionId), SK: keys_1.SESSION_SORT_KEY },
|
|
29
|
-
ConsistentRead: true,
|
|
30
|
-
ProjectionExpression: '#ttl',
|
|
31
|
-
ExpressionAttributeNames: { '#ttl': 'ttl' },
|
|
32
|
-
}), (0, retry_policy_1.retryFor)(context, signal));
|
|
33
|
-
const ttl = result.Item?.ttl;
|
|
34
|
-
if (typeof ttl === 'number' && ttl > (0, clock_1.nowSeconds)()) {
|
|
35
|
-
return { ttlTimestamp: ttl, refresh: false };
|
|
36
|
-
}
|
|
37
|
-
return { ttlTimestamp: candidate, refresh: true };
|
|
38
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { type StoredMessage } from '@langchain/core/messages';
|
|
2
|
-
import type { MessageWindow } from '../types';
|
|
3
|
-
/**
|
|
4
|
-
* Validate a session id is a non-blank, separator- and control-char-free string
|
|
5
|
-
* of at most 1024 bytes. Applied on every entry point, not just the write path: a bad value
|
|
6
|
-
* used to reach DynamoDB and surface as a raw AWS SDK exception on reads,
|
|
7
|
-
* while the identical value threw this library's typed `ValidationError` on a
|
|
8
|
-
* write.
|
|
9
|
-
*/
|
|
10
|
-
export declare function validateSessionId(sessionId: string): void;
|
|
11
|
-
/**
|
|
12
|
-
* Reject a message this adapter could never read back. The check *is* the
|
|
13
|
-
* read side's own rebuild (`mapStoredMessagesToChatMessages`), so write and
|
|
14
|
-
* read agree by construction: a `RemoveMessage`, or a tool, function or
|
|
15
|
-
* generic message missing its required field, fails here with a typed error
|
|
16
|
-
* instead of being persisted and then skipped or thrown by `getMessages`.
|
|
17
|
-
*/
|
|
18
|
-
export declare function validateStorableMessages(stored: StoredMessage[]): void;
|
|
19
|
-
/**
|
|
20
|
-
* Validate a `getMessages` window: `limit`, when given, a positive integer;
|
|
21
|
-
* `before`, when given, a valid `Date`. Checked before any DynamoDB call so a
|
|
22
|
-
* bad value fails with a typed error rather than a raw `ValidationException`
|
|
23
|
-
* (or, for an invalid Date, a NaN-derived sort key that matches nothing).
|
|
24
|
-
*/
|
|
25
|
-
export declare function validateMessageWindow(window: MessageWindow): void;
|