@theokit/sdk 4.0.2 → 4.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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * SE41 — `FsSessionStore`, the DEFAULT reference implementation of the pluggable
3
+ * {@link SessionStore} seam. It reads and append-writes the native Claude-shaped
4
+ * `.jsonl` transcript at `<baseDir>/projects/<encoded-cwd>/<agentId>.jsonl` — the
5
+ * exact on-disk format SE40 introduced (the file the Claude Code CLI can
6
+ * `--continue`). Omitting `local.sessionStore` resolves to this store, so the
7
+ * default persistence path is byte-identical to SE40 behavior.
8
+ *
9
+ * `readRecords` is `readTranscript(transcriptPath(...))` (a missing session →
10
+ * `[]`, not an error — a fresh agent has no history). `appendRecords` is a TRUE
11
+ * append: it reads the prior records, concatenates the new-turn delta, and
12
+ * rewrites the whole line set atomically under the SE40 cross-process file lock
13
+ * (`writeTranscript` never shrinks — the native format is an append-only
14
+ * `parentUuid` DAG). The parent dir is created BEFORE acquiring the lock because
15
+ * the lock's companion `<path>.lock` file needs an existing parent dir (the SE40
16
+ * `mkdir(dirname)`-before-lock fix).
17
+ *
18
+ * @internal
19
+ */
20
+ /** Options identifying the on-disk transcript location for the FS default store. */
21
+ export interface FsSessionStoreOptions {
22
+ /** Transcript base dir (`~/.theokit` default, `~/.claude` for CLI interop) — already `~`-expanded. */
23
+ baseDir: string;
24
+ /** The workspace cwd whose encoded form is the transcript project dir. */
25
+ cwd: string;
26
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * SE41 — `FsSessionStore`, the DEFAULT reference implementation of the pluggable
3
+ * {@link SessionStore} seam. It reads and append-writes the native Claude-shaped
4
+ * `.jsonl` transcript at `<baseDir>/projects/<encoded-cwd>/<agentId>.jsonl` — the
5
+ * exact on-disk format SE40 introduced (the file the Claude Code CLI can
6
+ * `--continue`). Omitting `local.sessionStore` resolves to this store, so the
7
+ * default persistence path is byte-identical to SE40 behavior.
8
+ *
9
+ * `readRecords` is `readTranscript(transcriptPath(...))` (a missing session →
10
+ * `[]`, not an error — a fresh agent has no history). `appendRecords` is a TRUE
11
+ * append: it reads the prior records, concatenates the new-turn delta, and
12
+ * rewrites the whole line set atomically under the SE40 cross-process file lock
13
+ * (`writeTranscript` never shrinks — the native format is an append-only
14
+ * `parentUuid` DAG). The parent dir is created BEFORE acquiring the lock because
15
+ * the lock's companion `<path>.lock` file needs an existing parent dir (the SE40
16
+ * `mkdir(dirname)`-before-lock fix).
17
+ *
18
+ * @internal
19
+ */
20
+ /** Options identifying the on-disk transcript location for the FS default store. */
21
+ export interface FsSessionStoreOptions {
22
+ /** Transcript base dir (`~/.theokit` default, `~/.claude` for CLI interop) — already `~`-expanded. */
23
+ baseDir: string;
24
+ /** The workspace cwd whose encoded form is the transcript project dir. */
25
+ cwd: string;
26
+ }
@@ -11,37 +11,45 @@
11
11
  *
12
12
  * @internal
13
13
  */
14
- import type { ConversationTurn } from "../../../types/conversation.js";
14
+ import type { SessionStore } from "../../../types/session-store.js";
15
15
  import type { SessionMessage } from "./session-types.js";
16
16
  /** The turn to persist: the user's text plus the rich per-turn conversation view. */
17
17
  export interface PersistTurnInput {
18
18
  userText: string;
19
19
  conversation: readonly ConversationTurn[];
20
20
  }
21
- /** Options identifying the on-disk transcript for one agent. */
21
+ /**
22
+ * The per-agent transcript metadata used to seed a {@link SessionTranscript}. The
23
+ * actual record I/O goes through the injected {@link SessionStore} (SE41), so this
24
+ * no longer carries a `baseDir` — the store is already bound to its location.
25
+ */
22
26
  export interface TranscriptLocation {
23
- baseDir: string;
24
27
  cwd: string;
25
28
  agentId: string;
26
29
  model: string;
27
30
  }
28
31
  /**
29
- * Read the persisted transcript and reconstruct the resumable session history,
30
- * narrowed to the in-memory {@link SessionMessage} shape (user/assistant text).
31
- * Tool turns fold into assistant-role context so resume keeps tool history.
32
+ * Read the session's records via the {@link SessionStore} and reconstruct the
33
+ * resumable history, narrowed to the in-memory {@link SessionMessage} shape
34
+ * (user/assistant text). Tool turns fold into assistant-role context so resume
35
+ * keeps tool history. Works identically for the FS default and an external store.
32
36
  */
33
- export declare function readSessionMessages(baseDir: string, cwd: string, agentId: string): Promise<SessionMessage[]>;
37
+ export declare function readSessionMessages(store: SessionStore, agentId: string): Promise<SessionMessage[]>;
34
38
  /**
35
- * Persist one conversation turn (user + assistant/tool records) to the native
36
- * transcript, append-only. Seeds the parent chain from the on-disk records under
37
- * a cross-process file lock so concurrent processes never tear the DAG.
39
+ * Persist one conversation turn (user + assistant/tool records) to the session
40
+ * store, append-only. Reads the prior records to seed the parent chain, folds the
41
+ * new turn, and appends ONLY the delta via {@link SessionStore.appendRecords}
42
+ * (the store owns the append atomicity — the FS default serializes under a file
43
+ * lock). Within one process, turns for an agent are already chained upstream, so
44
+ * the read→append is ordered; cross-host ordering is the external store's contract.
38
45
  */
39
- export declare function persistTurn(loc: TranscriptLocation, sessionId: string, turn: PersistTurnInput): Promise<void>;
46
+ export declare function persistTurn(store: SessionStore, loc: TranscriptLocation, sessionId: string, turn: PersistTurnInput): Promise<void>;
40
47
  /**
41
48
  * Append-only compaction: add a `compact_boundary` system record (a new root) so
42
- * resume replays only the post-boundary continuation. NEVER shrinks the line set.
49
+ * resume replays only the post-boundary continuation. Appends only the boundary
50
+ * record via the store — NEVER rewrites or shrinks the prior records.
43
51
  */
44
- export declare function appendCompactBoundaryRecord(loc: TranscriptLocation, sessionId: string, meta: {
52
+ export declare function appendCompactBoundaryRecord(store: SessionStore, loc: TranscriptLocation, sessionId: string, meta: {
45
53
  preTokens: number;
46
54
  trigger: string;
47
55
  }): Promise<void>;
@@ -28,6 +28,16 @@ export interface LocalOptions {
28
28
  * Set to `~/.claude` to write sessions the Claude Code CLI can `--continue`.
29
29
  */
30
30
  baseDir?: string;
31
+ /**
32
+ * SE41 — inject an external {@link import("./session-store.js").SessionStore}
33
+ * (Postgres / Redis / KV / durable object) as the PRIMARY session store and
34
+ * resume source. Omit for the default FS transcript store (`baseDir` above) —
35
+ * byte-identical to SE40. Use this for serverless (ephemeral FS) or multi-host /
36
+ * multi-pod deployments where a resumed agent must read its history from a shared
37
+ * store instead of local disk. The records stay the native Claude-shaped shape,
38
+ * so `--continue` interop is preserved (a store may also mirror to `~/.claude`).
39
+ */
40
+ sessionStore?: import("./session-store.js").SessionStore;
31
41
  }
32
42
  /**
33
43
  * Repo to clone into a cloud agent's VM.
@@ -11,6 +11,7 @@ export type * from "./messages.js";
11
11
  export type * from "./processors.js";
12
12
  export type * from "./providers.js";
13
13
  export type * from "./run.js";
14
+ export type * from "./session-store.js";
14
15
  export type * from "./task.js";
15
16
  export type * from "./theokit.js";
16
17
  export type * from "./trajectory.js";
@@ -0,0 +1,59 @@
1
+ /**
2
+ * SE41 — the pluggable `SessionStore` seam over the NATIVE session transcript.
3
+ *
4
+ * A minimal, two-method port so an external store (Postgres / Redis / KV /
5
+ * durable object) can be the **primary store AND resume source** — the
6
+ * serverless (ephemeral FS) and multi-host / multi-pod use case that SE40
7
+ * dropped when it removed the `ConversationStorageAdapter`. This is deliberately
8
+ * NOT that removed ~10-method adapter: the seam is JUST record read/append over
9
+ * the native {@link SessionRecord} shape (no getMessages / getSessionMeta /
10
+ * delete / objective methods).
11
+ *
12
+ * The SDK ships a real default implementation, `FsSessionStore`, that reads and
13
+ * append-writes the native Claude-shaped `.jsonl` transcript — omitting
14
+ * `local.sessionStore` yields byte-identical current behavior (back-compat, zero
15
+ * consumer change). Injected via `local.sessionStore` for external stores.
16
+ *
17
+ * Consistency contract: `appendRecords` is append-only and ordering-preserving.
18
+ * The FS default serializes appends per agent with a cross-process file lock;
19
+ * external implementations own (and MUST document) their own concurrency
20
+ * guarantees for two hosts appending to the same `agentId`.
21
+ *
22
+ * @public
23
+ */
24
+ import type { SessionRecord } from "../internal/persistence/session-transcript.js";
25
+ export type { SessionRecord } from "../internal/persistence/session-transcript.js";
26
+ /**
27
+ * The pluggable session-store seam. Exactly two methods over the native
28
+ * {@link SessionRecord} shape.
29
+ *
30
+ * @public
31
+ */
32
+ export interface SessionStore {
33
+ /**
34
+ * Return every persisted record for `agentId`, in append order. A session
35
+ * that was never written MUST resolve to `[]` (not throw) — a fresh agent has
36
+ * no history. The SDK reconstructs the resumable `LlmMessage[]` from these
37
+ * records via the native DAG reader, so the shape MUST be the exact
38
+ * {@link SessionRecord} the SDK writes.
39
+ *
40
+ * A store that cannot READ (e.g. the backing DB is unreachable on resume)
41
+ * MUST throw a typed error rather than silently returning `[]` — a silent
42
+ * empty read would masquerade as "no history" and drop the conversation.
43
+ */
44
+ readRecords(agentId: string): Promise<SessionRecord[]>;
45
+ /**
46
+ * Append `records` (the new-turn delta) to `agentId`'s session, append-only.
47
+ * MUST preserve order and MUST NOT drop or rewrite prior records — the native
48
+ * format is an append-only `parentUuid` DAG (compaction is a new-root
49
+ * `compact_boundary` record, still an append).
50
+ *
51
+ * Note on the write path: per-turn persistence is fire-and-forget so `send()`
52
+ * is never blocked by store I/O — an `appendRecords` rejection is logged to
53
+ * stderr, NOT thrown to the caller (best-effort write). An external store that
54
+ * must guarantee durability should make `appendRecords` resilient (retry /
55
+ * durable write) internally. This differs from {@link SessionStore.readRecords},
56
+ * which MUST throw on failure (a resume cannot proceed on a silent partial history).
57
+ */
58
+ appendRecords(agentId: string, records: readonly SessionRecord[]): Promise<void>;
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theokit/sdk",
3
- "version": "4.0.2",
3
+ "version": "4.1.0",
4
4
  "description": "TypeScript SDK for the Theo agent harness — same surface, local or cloud.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/usetheo/theokit-sdk#readme",