@strands-agents/sdk 0.2.2 → 0.4.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.
Files changed (241) hide show
  1. package/README.md +43 -0
  2. package/dist/src/__fixtures__/mock-hook-provider.d.ts +3 -7
  3. package/dist/src/__fixtures__/mock-hook-provider.d.ts.map +1 -1
  4. package/dist/src/__fixtures__/mock-hook-provider.js +3 -9
  5. package/dist/src/__fixtures__/mock-hook-provider.js.map +1 -1
  6. package/dist/src/__fixtures__/mock-message-model.d.ts +8 -2
  7. package/dist/src/__fixtures__/mock-message-model.d.ts.map +1 -1
  8. package/dist/src/__fixtures__/mock-message-model.js +1 -0
  9. package/dist/src/__fixtures__/mock-message-model.js.map +1 -1
  10. package/dist/src/__fixtures__/mock-storage-provider.d.ts +37 -0
  11. package/dist/src/__fixtures__/mock-storage-provider.d.ts.map +1 -0
  12. package/dist/src/__fixtures__/mock-storage-provider.js +105 -0
  13. package/dist/src/__fixtures__/mock-storage-provider.js.map +1 -0
  14. package/dist/src/__fixtures__/slim-types.d.ts +50 -0
  15. package/dist/src/__fixtures__/slim-types.d.ts.map +1 -0
  16. package/dist/src/__fixtures__/slim-types.js +6 -0
  17. package/dist/src/__fixtures__/slim-types.js.map +1 -0
  18. package/dist/src/__fixtures__/tool-helpers.d.ts +8 -3
  19. package/dist/src/__fixtures__/tool-helpers.d.ts.map +1 -1
  20. package/dist/src/__fixtures__/tool-helpers.js +1 -1
  21. package/dist/src/__fixtures__/tool-helpers.js.map +1 -1
  22. package/dist/src/agent/__tests__/agent.hook.test.js +18 -18
  23. package/dist/src/agent/__tests__/agent.hook.test.js.map +1 -1
  24. package/dist/src/agent/__tests__/agent.test.js +124 -8
  25. package/dist/src/agent/__tests__/agent.test.js.map +1 -1
  26. package/dist/src/agent/__tests__/printer.test.js +5 -9
  27. package/dist/src/agent/__tests__/printer.test.js.map +1 -1
  28. package/dist/src/agent/__tests__/snapshot.test.d.ts +2 -0
  29. package/dist/src/agent/__tests__/snapshot.test.d.ts.map +1 -0
  30. package/dist/src/agent/__tests__/snapshot.test.js +249 -0
  31. package/dist/src/agent/__tests__/snapshot.test.js.map +1 -0
  32. package/dist/src/agent/__tests__/state.test.js +19 -0
  33. package/dist/src/agent/__tests__/state.test.js.map +1 -1
  34. package/dist/src/agent/agent.d.ts +19 -4
  35. package/dist/src/agent/agent.d.ts.map +1 -1
  36. package/dist/src/agent/agent.js +104 -34
  37. package/dist/src/agent/agent.js.map +1 -1
  38. package/dist/src/agent/printer.d.ts +4 -0
  39. package/dist/src/agent/printer.d.ts.map +1 -1
  40. package/dist/src/agent/printer.js +18 -6
  41. package/dist/src/agent/printer.js.map +1 -1
  42. package/dist/src/agent/snapshot.d.ts +132 -0
  43. package/dist/src/agent/snapshot.d.ts.map +1 -0
  44. package/dist/src/agent/snapshot.js +151 -0
  45. package/dist/src/agent/snapshot.js.map +1 -0
  46. package/dist/src/agent/state.d.ts +14 -1
  47. package/dist/src/agent/state.d.ts.map +1 -1
  48. package/dist/src/agent/state.js +21 -0
  49. package/dist/src/agent/state.js.map +1 -1
  50. package/dist/src/errors.d.ts +15 -0
  51. package/dist/src/errors.d.ts.map +1 -1
  52. package/dist/src/errors.js +18 -0
  53. package/dist/src/errors.js.map +1 -1
  54. package/dist/src/hooks/__tests__/events.test.js +102 -21
  55. package/dist/src/hooks/__tests__/events.test.js.map +1 -1
  56. package/dist/src/hooks/events.d.ts +156 -22
  57. package/dist/src/hooks/events.d.ts.map +1 -1
  58. package/dist/src/hooks/events.js +158 -18
  59. package/dist/src/hooks/events.js.map +1 -1
  60. package/dist/src/hooks/index.d.ts +12 -4
  61. package/dist/src/hooks/index.d.ts.map +1 -1
  62. package/dist/src/hooks/index.js +11 -3
  63. package/dist/src/hooks/index.js.map +1 -1
  64. package/dist/src/hooks/registry.d.ts +5 -5
  65. package/dist/src/hooks/registry.d.ts.map +1 -1
  66. package/dist/src/hooks/registry.js.map +1 -1
  67. package/dist/src/hooks/types.d.ts +5 -5
  68. package/dist/src/hooks/types.d.ts.map +1 -1
  69. package/dist/src/index.d.ts +4 -2
  70. package/dist/src/index.d.ts.map +1 -1
  71. package/dist/src/index.js +4 -3
  72. package/dist/src/index.js.map +1 -1
  73. package/dist/src/models/__tests__/anthropic.test.js +31 -42
  74. package/dist/src/models/__tests__/anthropic.test.js.map +1 -1
  75. package/dist/src/models/__tests__/bedrock.test.js +70 -107
  76. package/dist/src/models/__tests__/bedrock.test.js.map +1 -1
  77. package/dist/src/models/__tests__/gemini.test.js +438 -141
  78. package/dist/src/models/__tests__/gemini.test.js.map +1 -1
  79. package/dist/src/models/__tests__/model.test.js +14 -13
  80. package/dist/src/models/__tests__/model.test.js.map +1 -1
  81. package/dist/src/models/__tests__/openai.test.js +73 -83
  82. package/dist/src/models/__tests__/openai.test.js.map +1 -1
  83. package/dist/src/models/gemini/adapters.d.ts +2 -1
  84. package/dist/src/models/gemini/adapters.d.ts.map +1 -1
  85. package/dist/src/models/gemini/adapters.js +97 -10
  86. package/dist/src/models/gemini/adapters.js.map +1 -1
  87. package/dist/src/models/gemini/model.d.ts.map +1 -1
  88. package/dist/src/models/gemini/model.js +37 -1
  89. package/dist/src/models/gemini/model.js.map +1 -1
  90. package/dist/src/models/gemini/types.d.ts +9 -1
  91. package/dist/src/models/gemini/types.d.ts.map +1 -1
  92. package/dist/src/models/model.d.ts.map +1 -1
  93. package/dist/src/models/model.js +4 -0
  94. package/dist/src/models/model.js.map +1 -1
  95. package/dist/src/models/streaming.d.ts +13 -0
  96. package/dist/src/models/streaming.d.ts.map +1 -1
  97. package/dist/src/models/streaming.js +17 -0
  98. package/dist/src/models/streaming.js.map +1 -1
  99. package/dist/src/multiagent/__tests__/nodes.test.d.ts +2 -0
  100. package/dist/src/multiagent/__tests__/nodes.test.d.ts.map +1 -0
  101. package/dist/src/multiagent/__tests__/nodes.test.js +98 -0
  102. package/dist/src/multiagent/__tests__/nodes.test.js.map +1 -0
  103. package/dist/src/multiagent/events.d.ts +24 -0
  104. package/dist/src/multiagent/events.d.ts.map +1 -0
  105. package/dist/src/multiagent/events.js +19 -0
  106. package/dist/src/multiagent/events.js.map +1 -0
  107. package/dist/src/multiagent/index.d.ts +11 -0
  108. package/dist/src/multiagent/index.d.ts.map +1 -0
  109. package/dist/src/multiagent/index.js +7 -0
  110. package/dist/src/multiagent/index.js.map +1 -0
  111. package/dist/src/multiagent/nodes.d.ts +75 -0
  112. package/dist/src/multiagent/nodes.d.ts.map +1 -0
  113. package/dist/src/multiagent/nodes.js +101 -0
  114. package/dist/src/multiagent/nodes.js.map +1 -0
  115. package/dist/src/multiagent/state.d.ts +55 -0
  116. package/dist/src/multiagent/state.d.ts.map +1 -0
  117. package/dist/src/multiagent/state.js +43 -0
  118. package/dist/src/multiagent/state.js.map +1 -0
  119. package/dist/src/multiagent/types.d.ts +5 -0
  120. package/dist/src/multiagent/types.d.ts.map +1 -0
  121. package/dist/src/multiagent/types.js +2 -0
  122. package/dist/src/multiagent/types.js.map +1 -0
  123. package/dist/src/registry/tool-registry.d.ts +2 -1
  124. package/dist/src/registry/tool-registry.d.ts.map +1 -1
  125. package/dist/src/registry/tool-registry.js +4 -2
  126. package/dist/src/registry/tool-registry.js.map +1 -1
  127. package/dist/src/session/__tests__/file-storage.test.node.d.ts +2 -0
  128. package/dist/src/session/__tests__/file-storage.test.node.d.ts.map +1 -0
  129. package/dist/src/session/__tests__/file-storage.test.node.js +218 -0
  130. package/dist/src/session/__tests__/file-storage.test.node.js.map +1 -0
  131. package/dist/src/session/__tests__/s3-storage.test.node.d.ts +2 -0
  132. package/dist/src/session/__tests__/s3-storage.test.node.d.ts.map +1 -0
  133. package/dist/src/session/__tests__/s3-storage.test.node.js +375 -0
  134. package/dist/src/session/__tests__/s3-storage.test.node.js.map +1 -0
  135. package/dist/src/session/__tests__/validation.test.d.ts +2 -0
  136. package/dist/src/session/__tests__/validation.test.d.ts.map +1 -0
  137. package/dist/src/session/__tests__/validation.test.js +20 -0
  138. package/dist/src/session/__tests__/validation.test.js.map +1 -0
  139. package/dist/src/session/file-storage.d.ts +79 -0
  140. package/dist/src/session/file-storage.d.ts.map +1 -0
  141. package/dist/src/session/file-storage.js +144 -0
  142. package/dist/src/session/file-storage.js.map +1 -0
  143. package/dist/src/session/index.d.ts +19 -0
  144. package/dist/src/session/index.d.ts.map +1 -0
  145. package/dist/src/session/index.js +18 -0
  146. package/dist/src/session/index.js.map +1 -0
  147. package/dist/src/session/s3-storage.d.ts +93 -0
  148. package/dist/src/session/s3-storage.d.ts.map +1 -0
  149. package/dist/src/session/s3-storage.js +150 -0
  150. package/dist/src/session/s3-storage.js.map +1 -0
  151. package/dist/src/session/storage.d.ts +91 -0
  152. package/dist/src/session/storage.d.ts.map +1 -0
  153. package/dist/src/session/storage.js +2 -0
  154. package/dist/src/session/storage.js.map +1 -0
  155. package/dist/src/session/types.d.ts +49 -0
  156. package/dist/src/session/types.d.ts.map +1 -0
  157. package/dist/src/session/types.js +2 -0
  158. package/dist/src/session/types.js.map +1 -0
  159. package/dist/src/session/validation.d.ts +10 -0
  160. package/dist/src/session/validation.d.ts.map +1 -0
  161. package/dist/src/session/validation.js +16 -0
  162. package/dist/src/session/validation.js.map +1 -0
  163. package/dist/src/structured-output/__tests__/context.test.d.ts +2 -0
  164. package/dist/src/structured-output/__tests__/context.test.d.ts.map +1 -0
  165. package/dist/src/structured-output/__tests__/context.test.js +201 -0
  166. package/dist/src/structured-output/__tests__/context.test.js.map +1 -0
  167. package/dist/src/structured-output/__tests__/exceptions.test.d.ts +2 -0
  168. package/dist/src/structured-output/__tests__/exceptions.test.d.ts.map +1 -0
  169. package/dist/src/structured-output/__tests__/exceptions.test.js +103 -0
  170. package/dist/src/structured-output/__tests__/exceptions.test.js.map +1 -0
  171. package/dist/src/structured-output/__tests__/tool.test.d.ts +2 -0
  172. package/dist/src/structured-output/__tests__/tool.test.d.ts.map +1 -0
  173. package/dist/src/structured-output/__tests__/tool.test.js +256 -0
  174. package/dist/src/structured-output/__tests__/tool.test.js.map +1 -0
  175. package/dist/src/structured-output/__tests__/utils.test.d.ts +2 -0
  176. package/dist/src/structured-output/__tests__/utils.test.d.ts.map +1 -0
  177. package/dist/src/structured-output/__tests__/utils.test.js +183 -0
  178. package/dist/src/structured-output/__tests__/utils.test.js.map +1 -0
  179. package/dist/src/structured-output/context.d.ts +91 -0
  180. package/dist/src/structured-output/context.d.ts.map +1 -0
  181. package/dist/src/structured-output/context.js +112 -0
  182. package/dist/src/structured-output/context.js.map +1 -0
  183. package/dist/src/structured-output/exceptions.d.ts +18 -0
  184. package/dist/src/structured-output/exceptions.d.ts.map +1 -0
  185. package/dist/src/structured-output/exceptions.js +28 -0
  186. package/dist/src/structured-output/exceptions.js.map +1 -0
  187. package/dist/src/structured-output/tool.d.ts +33 -0
  188. package/dist/src/structured-output/tool.d.ts.map +1 -0
  189. package/dist/src/structured-output/tool.js +73 -0
  190. package/dist/src/structured-output/tool.js.map +1 -0
  191. package/dist/src/structured-output/utils.d.ts +23 -0
  192. package/dist/src/structured-output/utils.d.ts.map +1 -0
  193. package/dist/src/structured-output/utils.js +104 -0
  194. package/dist/src/structured-output/utils.js.map +1 -0
  195. package/dist/src/tools/zod-tool.d.ts.map +1 -1
  196. package/dist/src/tools/zod-tool.js +2 -5
  197. package/dist/src/tools/zod-tool.js.map +1 -1
  198. package/dist/src/tsconfig.tsbuildinfo +1 -1
  199. package/dist/src/types/__tests__/media.test.js +216 -1
  200. package/dist/src/types/__tests__/media.test.js.map +1 -1
  201. package/dist/src/types/__tests__/messages.test.js +193 -4
  202. package/dist/src/types/__tests__/messages.test.js.map +1 -1
  203. package/dist/src/types/agent.d.ts +13 -7
  204. package/dist/src/types/agent.d.ts.map +1 -1
  205. package/dist/src/types/agent.js +8 -1
  206. package/dist/src/types/agent.js.map +1 -1
  207. package/dist/src/types/json.d.ts +61 -0
  208. package/dist/src/types/json.d.ts.map +1 -1
  209. package/dist/src/types/json.js +24 -0
  210. package/dist/src/types/json.js.map +1 -1
  211. package/dist/src/types/media.d.ts +84 -4
  212. package/dist/src/types/media.d.ts.map +1 -1
  213. package/dist/src/types/media.js +194 -0
  214. package/dist/src/types/media.js.map +1 -1
  215. package/dist/src/types/messages.d.ts +158 -9
  216. package/dist/src/types/messages.d.ts.map +1 -1
  217. package/dist/src/types/messages.js +242 -7
  218. package/dist/src/types/messages.js.map +1 -1
  219. package/dist/src/types/serializable.d.ts +31 -0
  220. package/dist/src/types/serializable.d.ts.map +1 -0
  221. package/dist/src/types/serializable.js +19 -0
  222. package/dist/src/types/serializable.js.map +1 -0
  223. package/dist/src/utils/zod.d.ts +11 -0
  224. package/dist/src/utils/zod.d.ts.map +1 -0
  225. package/dist/src/utils/zod.js +14 -0
  226. package/dist/src/utils/zod.js.map +1 -0
  227. package/dist/src/vended-tools/bash/__tests__/bash.test.node.d.ts +2 -0
  228. package/dist/src/vended-tools/bash/__tests__/bash.test.node.d.ts.map +1 -0
  229. package/dist/src/vended-tools/bash/__tests__/{bash.test.js → bash.test.node.js} +3 -4
  230. package/dist/src/vended-tools/bash/__tests__/bash.test.node.js.map +1 -0
  231. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.d.ts +2 -0
  232. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.d.ts.map +1 -0
  233. package/dist/src/vended-tools/file_editor/__tests__/{file-editor.test.js → file-editor.test.node.js} +1 -1
  234. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.node.js.map +1 -0
  235. package/package.json +7 -2
  236. package/dist/src/vended-tools/bash/__tests__/bash.test.d.ts +0 -2
  237. package/dist/src/vended-tools/bash/__tests__/bash.test.d.ts.map +0 -1
  238. package/dist/src/vended-tools/bash/__tests__/bash.test.js.map +0 -1
  239. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.d.ts +0 -2
  240. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.d.ts.map +0 -1
  241. package/dist/src/vended-tools/file_editor/__tests__/file-editor.test.js.map +0 -1
@@ -0,0 +1,144 @@
1
+ import { promises as fs } from 'fs';
2
+ import { join, dirname } from 'path';
3
+ import { SessionError } from '../errors.js';
4
+ import { validateIdentifier } from './validation.js';
5
+ const MANIFEST = 'manifest.json';
6
+ const SNAPSHOT_LATEST = 'snapshot_latest.json';
7
+ const IMMUTABLE_HISTORY = 'immutable_history';
8
+ const SNAPSHOT_REGEX = /snapshot_(\d+)\.json$/;
9
+ const SCHEMA_VERSION = '1.0';
10
+ const DEFAULT_SNAPSHOT_ID = '1';
11
+ /**
12
+ * File-based implementation of SnapshotStorage for persisting session snapshots
13
+ */
14
+ export class FileStorage {
15
+ /** Base directory path */
16
+ _baseDir;
17
+ /**
18
+ * Creates new FileStorage instance
19
+ * @param baseDir - Base directory path for storing snapshots
20
+ */
21
+ constructor(baseDir) {
22
+ this._baseDir = baseDir;
23
+ }
24
+ /**
25
+ * Generates file path for session scope snapshots
26
+ */
27
+ _getPath(location, filename) {
28
+ validateIdentifier(location.sessionId);
29
+ validateIdentifier(location.scopeId);
30
+ return join(this._baseDir, location.sessionId, 'scopes', location.scope, location.scopeId, 'snapshots', filename);
31
+ }
32
+ /**
33
+ * Saves snapshot to file, optionally marking as latest
34
+ */
35
+ async saveSnapshot(params) {
36
+ await this._writeJSON(this._getHistorySnapshotPath(params.location, params.snapshotId), params.snapshot);
37
+ if (params.isLatest) {
38
+ await this._writeJSON(this._getLatestSnapshotPath(params.location), params.snapshot);
39
+ }
40
+ }
41
+ /**
42
+ * Loads snapshot by ID or latest if null
43
+ */
44
+ async loadSnapshot(params) {
45
+ const path = params.snapshotId === undefined
46
+ ? this._getLatestSnapshotPath(params.location)
47
+ : this._getHistorySnapshotPath(params.location, params.snapshotId);
48
+ return this._readJSON(path);
49
+ }
50
+ /**
51
+ * Checks if an error is a file not found error (ENOENT)
52
+ */
53
+ _isFileNotFoundError(error) {
54
+ return error !== null && typeof error === 'object' && 'code' in error && error.code === 'ENOENT';
55
+ }
56
+ /**
57
+ * Lists all snapshot IDs for a session scope.
58
+ *
59
+ * TODO: Add pagination support for long-running agents with many snapshots.
60
+ * Future signature could be:
61
+ * ```typescript
62
+ * listSnapshots(params: {
63
+ * sessionId: string
64
+ * scope: Scope
65
+ * limit?: number // Max results to return (e.g., 100)
66
+ * startAfter?: string // Snapshot ID to start after (for cursor-based pagination)
67
+ * }): Promise<{ snapshotIds: string[]; nextToken?: string }>
68
+ * ```
69
+ */
70
+ async listSnapshotIds(params) {
71
+ const dirPath = this._getPath(params.location, IMMUTABLE_HISTORY);
72
+ try {
73
+ const files = await fs.readdir(dirPath);
74
+ return files
75
+ .map((file) => file.match(SNAPSHOT_REGEX)?.[1])
76
+ .filter((id) => id !== undefined)
77
+ .sort((a, b) => parseInt(a) - parseInt(b));
78
+ }
79
+ catch (error) {
80
+ if (this._isFileNotFoundError(error)) {
81
+ return [];
82
+ }
83
+ throw new SessionError(`Failed to list snapshots for session ${params.location.sessionId}`, { cause: error });
84
+ }
85
+ }
86
+ /**
87
+ * Loads manifest or returns default if not found
88
+ */
89
+ async loadManifest(params) {
90
+ const path = this._getPath(params.location, MANIFEST);
91
+ const manifest = await this._readJSON(path);
92
+ return (manifest ?? {
93
+ schemaVersion: SCHEMA_VERSION,
94
+ nextSnapshotId: DEFAULT_SNAPSHOT_ID,
95
+ updatedAt: new Date().toISOString(),
96
+ });
97
+ }
98
+ /**
99
+ * Saves manifest to file
100
+ */
101
+ async saveManifest(params) {
102
+ const path = this._getPath(params.location, MANIFEST);
103
+ await this._writeJSON(path, params.manifest);
104
+ }
105
+ /**
106
+ * Writes JSON data to file atomically
107
+ */
108
+ async _writeJSON(path, data) {
109
+ try {
110
+ await fs.mkdir(dirname(path), { recursive: true });
111
+ const tmpPath = `${path}.tmp`;
112
+ await fs.writeFile(tmpPath, JSON.stringify(data, null, 2), 'utf8');
113
+ await fs.rename(tmpPath, path);
114
+ }
115
+ catch (error) {
116
+ throw new SessionError(`Failed to write file ${path}`, { cause: error });
117
+ }
118
+ }
119
+ /**
120
+ * Reads and parses JSON from file
121
+ */
122
+ async _readJSON(path) {
123
+ try {
124
+ const content = await fs.readFile(path, 'utf8');
125
+ return JSON.parse(content);
126
+ }
127
+ catch (error) {
128
+ if (this._isFileNotFoundError(error)) {
129
+ return null;
130
+ }
131
+ if (error instanceof SyntaxError) {
132
+ throw new SessionError(`Invalid JSON in file ${path}`, { cause: error });
133
+ }
134
+ throw new SessionError(`File system error reading ${path}`, { cause: error });
135
+ }
136
+ }
137
+ _getLatestSnapshotPath(location) {
138
+ return this._getPath(location, SNAPSHOT_LATEST);
139
+ }
140
+ _getHistorySnapshotPath(location, snapshotId) {
141
+ return this._getPath(location, `${IMMUTABLE_HISTORY}/snapshot_${String(snapshotId).padStart(5, '0')}.json`);
142
+ }
143
+ }
144
+ //# sourceMappingURL=file-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage.js","sourceRoot":"","sources":["../../../src/session/file-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,IAAI,CAAA;AACnC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAIpC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAEpD,MAAM,QAAQ,GAAG,eAAe,CAAA;AAChC,MAAM,eAAe,GAAG,sBAAsB,CAAA;AAC9C,MAAM,iBAAiB,GAAG,mBAAmB,CAAA;AAC7C,MAAM,cAAc,GAAG,uBAAuB,CAAA;AAC9C,MAAM,cAAc,GAAG,KAAK,CAAA;AAC5B,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAE/B;;GAEG;AACH,MAAM,OAAO,WAAW;IACtB,0BAA0B;IACT,QAAQ,CAAQ;IAEjC;;;OAGG;IACH,YAAY,OAAe;QACzB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;IACzB,CAAC;IAED;;OAEG;IACK,QAAQ,C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C,MAAM,IAAI,YAAY,CAAC,wBAAwB,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;YAC1E,CAAC;YACD,MAAM,IAAI,YAAY,CAAC,6BAA6B,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;QAC/E,CAAC;IACH,CAAC;IAEO,sBAAsB,CAAC,QAA0B;QACvD,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAA;IACjD,CAAC;IAEO,uBAAuB,CAAC,QAA0B,EAAE,UAAkB;QAC5E,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,GAAG,iBAAiB,aAAa,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;IAC7G,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Session management module for conversation persistence and restoration.
3
+ *
4
+ * Provides snapshot-based session management with pluggable storage backends.
5
+ * Supports conversation history, state persistence, and branching.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { FileStorage, SnapshotStorage } from '@strands/agents/session'
10
+ *
11
+ * const storage = new FileStorage('./sessions')
12
+ * await storage.saveSnapshot({ sessionId, scope, isLatest: true, snapshot })
13
+ * ```
14
+ */
15
+ export type { Scope, Snapshot, SnapshotManifest, SnapshotTriggerCallback } from './types.js';
16
+ export type { SessionStorage, SnapshotStorage, SnapshotLocation } from './storage.js';
17
+ export { FileStorage } from './file-storage.js';
18
+ export { S3Storage, type S3StorageConfig } from './s3-storage.js';
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,YAAY,EAAE,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAG5F,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAGrF,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Session management module for conversation persistence and restoration.
3
+ *
4
+ * Provides snapshot-based session management with pluggable storage backends.
5
+ * Supports conversation history, state persistence, and branching.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { FileStorage, SnapshotStorage } from '@strands/agents/session'
10
+ *
11
+ * const storage = new FileStorage('./sessions')
12
+ * await storage.saveSnapshot({ sessionId, scope, isLatest: true, snapshot })
13
+ * ```
14
+ */
15
+ // Storage implementations
16
+ export { FileStorage } from './file-storage.js';
17
+ export { S3Storage } from './s3-storage.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AASH,0BAA0B;AAC1B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAwB,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,93 @@
1
+ import { S3Client } from '@aws-sdk/client-s3';
2
+ import type { SnapshotStorage, SnapshotLocation } from './storage.js';
3
+ import type { Snapshot, SnapshotManifest } from './types.js';
4
+ /**
5
+ * Configuration options for S3Storage
6
+ */
7
+ export type S3StorageConfig = {
8
+ /** S3 bucket name */
9
+ bucket: string;
10
+ /** Optional key prefix for all objects */
11
+ prefix?: string;
12
+ /** AWS region (default: us-east-1). Cannot be used with s3Client */
13
+ region?: string;
14
+ /** Pre-configured S3 client. Cannot be used with region */
15
+ s3Client?: S3Client;
16
+ };
17
+ /**
18
+ * S3-based implementation of SnapshotStorage for persisting session snapshots
19
+ */
20
+ export declare class S3Storage implements SnapshotStorage {
21
+ /** S3 client instance */
22
+ private readonly _s3;
23
+ /** S3 bucket name */
24
+ private readonly _bucket;
25
+ /** Key prefix for all objects */
26
+ private readonly _prefix;
27
+ /**
28
+ * Creates new S3Storage instance
29
+ * @param config - Configuration options
30
+ */
31
+ constructor(config: S3StorageConfig);
32
+ /**
33
+ * Generates S3 key path for session scope snapshots
34
+ */
35
+ private _getKey;
36
+ /**
37
+ * Saves snapshot to S3, optionally marking as latest
38
+ */
39
+ saveSnapshot(params: {
40
+ location: SnapshotLocation;
41
+ snapshotId: string;
42
+ isLatest: boolean;
43
+ snapshot: Snapshot;
44
+ }): Promise<void>;
45
+ /**
46
+ * Loads snapshot by ID or latest if undefined
47
+ */
48
+ loadSnapshot(params: {
49
+ location: SnapshotLocation;
50
+ snapshotId?: string;
51
+ }): Promise<Snapshot | null>;
52
+ /**
53
+ * Lists all snapshot IDs for a session scope.
54
+ *
55
+ * TODO: Add pagination support for long-running agents with many snapshots.
56
+ * Future signature could be:
57
+ * ```typescript
58
+ * listSnapshots(params: {
59
+ * sessionId: string
60
+ * scope: Scope
61
+ * limit?: number // Max results to return (e.g., 100)
62
+ * startAfter?: string // Snapshot ID to start after (for cursor-based pagination)
63
+ * }): Promise<{ snapshotIds: string[]; nextToken?: string }>
64
+ * ```
65
+ */
66
+ listSnapshotIds(params: {
67
+ location: SnapshotLocation;
68
+ }): Promise<string[]>;
69
+ /**
70
+ * Loads manifest or returns default if not found
71
+ */
72
+ loadManifest(params: {
73
+ location: SnapshotLocation;
74
+ }): Promise<SnapshotManifest>;
75
+ /**
76
+ * Saves manifest to S3
77
+ */
78
+ saveManifest(params: {
79
+ location: SnapshotLocation;
80
+ manifest: SnapshotManifest;
81
+ }): Promise<void>;
82
+ /**
83
+ * Writes JSON data to S3
84
+ */
85
+ private _writeJSON;
86
+ /**
87
+ * Reads and parses JSON from S3
88
+ */
89
+ private _readJSON;
90
+ private _getLatestSnapshotKey;
91
+ private _getHistorySnapshotKey;
92
+ }
93
+ //# sourceMappingURL=s3-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-storage.d.ts","sourceRoot":"","sources":["../../../src/session/s3-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA4D,MAAM,oBAAoB,CAAA;AAEvG,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AACrE,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAW5D;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,qBAAqB;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,0CAA0C;IAC1C,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,QAAQ,CAAA;CACpB,CAAA;AAED;;GAEG;AACH,qBAAa,SAAU,YAAW,eAAe;IAC/C,yBAAyB;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAU;IAC9B,qBAAqB;IACrB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAChC,iCAAiC;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAEhC;;;OAGG;gBACS,MAAM,EAAE,eAAe;IAUnC;;OAEG;IACH,OAAO,CAAC,OAAO;IAOf;;OAEG;IACG,YAAY,CAAC,MAAM,EAAE;QACzB,QAAQ,EAAE,gBAAgB,CAAA;QAC1B,UAAU,EAAE,MAAM,CAAA;QAClB,QAAQ,EAAE,OAAO,CAAA;QACjB,QAAQ,EAAE,QAAQ,CAAA;KACnB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOjB;;OAEG;IACG,YAAY,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,gBAAgB,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAQzG;;;;;;;;;;;;;OAaG;IACG,eAAe,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,gBAAgB,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAgBhF;;OAEG;IACG,YAAY,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,gBAAgB,CAAA;KAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAarF;;OAEG;IACG,YAAY,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,gBAAgB,CAAC;QAAC,QAAQ,EAAE,gBAAgB,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAKrG;;OAEG;YACW,UAAU;IAexB;;OAEG;YACW,SAAS;IAiBvB,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,sBAAsB;CAG/B"}
@@ -0,0 +1,150 @@
1
+ import { S3Client, PutObjectCommand, GetObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
2
+ import { SessionError } from '../errors.js';
3
+ import { validateIdentifier } from './validation.js';
4
+ const MANIFEST = 'manifest.json';
5
+ const SNAPSHOT_LATEST = 'snapshot_latest.json';
6
+ const IMMUTABLE_HISTORY = 'immutable_history/';
7
+ const SNAPSHOT_REGEX = /snapshot_(\d+)\.json$/;
8
+ const SCHEMA_VERSION = '1.0';
9
+ const DEFAULT_SNAPSHOT_ID = '1';
10
+ /**
11
+ * S3-based implementation of SnapshotStorage for persisting session snapshots
12
+ */
13
+ export class S3Storage {
14
+ /** S3 client instance */
15
+ _s3;
16
+ /** S3 bucket name */
17
+ _bucket;
18
+ /** Key prefix for all objects */
19
+ _prefix;
20
+ /**
21
+ * Creates new S3Storage instance
22
+ * @param config - Configuration options
23
+ */
24
+ constructor(config) {
25
+ if (config.s3Client && config.region) {
26
+ throw new SessionError('Cannot specify both s3Client and region. Configure region in the S3Client instead.');
27
+ }
28
+ this._bucket = config.bucket;
29
+ this._prefix = config.prefix ?? '';
30
+ this._s3 = config.s3Client ?? new S3Client({ region: config.region ?? 'us-east-1' });
31
+ }
32
+ /**
33
+ * Generates S3 key path for session scope snapshots
34
+ */
35
+ _getKey(location, path) {
36
+ validateIdentifier(location.sessionId);
37
+ validateIdentifier(location.scopeId);
38
+ const base = this._prefix ? `${this._prefix}/` : '';
39
+ return `${base}${location.sessionId}/scopes/${location.scope}/${location.scopeId}/snapshots/${path}`;
40
+ }
41
+ /**
42
+ * Saves snapshot to S3, optionally marking as latest
43
+ */
44
+ async saveSnapshot(params) {
45
+ await this._writeJSON(this._getHistorySnapshotKey(params.location, params.snapshotId), params.snapshot);
46
+ if (params.isLatest) {
47
+ await this._writeJSON(this._getLatestSnapshotKey(params.location), params.snapshot);
48
+ }
49
+ }
50
+ /**
51
+ * Loads snapshot by ID or latest if undefined
52
+ */
53
+ async loadSnapshot(params) {
54
+ const key = params.snapshotId === undefined
55
+ ? this._getLatestSnapshotKey(params.location)
56
+ : this._getHistorySnapshotKey(params.location, params.snapshotId);
57
+ return this._readJSON(key);
58
+ }
59
+ /**
60
+ * Lists all snapshot IDs for a session scope.
61
+ *
62
+ * TODO: Add pagination support for long-running agents with many snapshots.
63
+ * Future signature could be:
64
+ * ```typescript
65
+ * listSnapshots(params: {
66
+ * sessionId: string
67
+ * scope: Scope
68
+ * limit?: number // Max results to return (e.g., 100)
69
+ * startAfter?: string // Snapshot ID to start after (for cursor-based pagination)
70
+ * }): Promise<{ snapshotIds: string[]; nextToken?: string }>
71
+ * ```
72
+ */
73
+ async listSnapshotIds(params) {
74
+ const prefix = this._getKey(params.location, IMMUTABLE_HISTORY);
75
+ try {
76
+ const response = await this._s3.send(new ListObjectsV2Command({ Bucket: this._bucket, Prefix: prefix }));
77
+ return (response.Contents ?? [])
78
+ .map((obj) => obj.Key?.match(SNAPSHOT_REGEX)?.[1])
79
+ .filter((id) => id !== undefined)
80
+ .map((id) => String(parseInt(id)))
81
+ .sort((a, b) => parseInt(a) - parseInt(b));
82
+ }
83
+ catch (error) {
84
+ throw new SessionError(`Failed to list snapshots for session ${params.location.sessionId}`, { cause: error });
85
+ }
86
+ }
87
+ /**
88
+ * Loads manifest or returns default if not found
89
+ */
90
+ async loadManifest(params) {
91
+ const key = this._getKey(params.location, MANIFEST);
92
+ const manifest = await this._readJSON(key);
93
+ return (manifest ?? {
94
+ schemaVersion: SCHEMA_VERSION,
95
+ nextSnapshotId: DEFAULT_SNAPSHOT_ID,
96
+ updatedAt: new Date().toISOString(),
97
+ });
98
+ }
99
+ /**
100
+ * Saves manifest to S3
101
+ */
102
+ async saveManifest(params) {
103
+ const key = this._getKey(params.location, MANIFEST);
104
+ await this._writeJSON(key, params.manifest);
105
+ }
106
+ /**
107
+ * Writes JSON data to S3
108
+ */
109
+ async _writeJSON(key, data) {
110
+ try {
111
+ await this._s3.send(new PutObjectCommand({
112
+ Bucket: this._bucket,
113
+ Key: key,
114
+ Body: JSON.stringify(data, null, 2),
115
+ ContentType: 'application/json',
116
+ }));
117
+ }
118
+ catch (error) {
119
+ throw new SessionError(`Failed to write S3 object ${key}`, { cause: error });
120
+ }
121
+ }
122
+ /**
123
+ * Reads and parses JSON from S3
124
+ */
125
+ async _readJSON(key) {
126
+ try {
127
+ const response = await this._s3.send(new GetObjectCommand({ Bucket: this._bucket, Key: key }));
128
+ const body = await response.Body?.transformToString();
129
+ if (!body)
130
+ return null;
131
+ return JSON.parse(body);
132
+ }
133
+ catch (error) {
134
+ if (error && typeof error === 'object' && 'name' in error && error.name === 'NoSuchKey') {
135
+ return null;
136
+ }
137
+ if (error instanceof SyntaxError) {
138
+ throw new SessionError(`Invalid JSON in S3 object ${key}`, { cause: error });
139
+ }
140
+ throw new SessionError(`S3 error reading ${key}`, { cause: error });
141
+ }
142
+ }
143
+ _getLatestSnapshotKey(location) {
144
+ return this._getKey(location, SNAPSHOT_LATEST);
145
+ }
146
+ _getHistorySnapshotKey(location, snapshotId) {
147
+ return this._getKey(location, `${IMMUTABLE_HISTORY}snapshot_${String(snapshotId).padStart(5, '0')}.json`);
148
+ }
149
+ }
150
+ //# sourceMappingURL=s3-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-storage.js","sourceRoot":"","sources":["../../../src/session/s3-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAIvG,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAEpD,MAAM,QAAQ,GAAG,eAAe,CAAA;AAChC,MAAM,eAAe,GAAG,sBAAsB,CAAA;AAC9C,MAAM,iBAAiB,GAAG,oBAAoB,CAAA;AAC9C,MAAM,cAAc,GAAG,uBAAuB,CAAA;AAC9C,MAAM,cAAc,GAAG,KAAK,CAAA;AAC5B,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAgB/B;;GAEG;AACH,MAAM,OAAO,SAAS;IACpB,yBAAyB;IACR,GAAG,CAAU;IAC9B,qBAAqB;IACJ,OAAO,CAAQ;IAChC,iCAAiC;IAChB,OAAO,CAAQ;IAEhC;;;OAGG;IACH,YAAY,MAAuB;QACjC,IAAI,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YACrC,MAAM,IAAI,YAAY,CAAC,oFAAoF,CAAC,CAAA;QAC9G,CAAC;QAED,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAA;QAClC,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,QAAQ,IAAI,IAAI,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,WAAW,EAAE,CAAC,CAAA;IACtF,CAAC;IAED;;OAEG;IACK,OAAO,CAAC,QAA0B,EAAE,IAAY;QAC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AK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;gBACxF,OAAO,IAAI,CAAA;YACb,CAAC;YACD,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;gBACjC,MAAM,IAAI,YAAY,CAAC,6BAA6B,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;YAC9E,CAAC;YACD,MAAM,IAAI,YAAY,CAAC,oBAAoB,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;QACrE,CAAC;IACH,CAAC;IAEO,qBAAqB,CAAC,QAA0B;QACtD,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAA;IAChD,CAAC;IAEO,sBAAsB,CAAC,QAA0B,EAAE,UAAkB;QAC3E,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,iBAAiB,YAAY,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;IAC3G,CAAC;CACF"}
@@ -0,0 +1,91 @@
1
+ import type { Scope, Snapshot, SnapshotManifest } from './types.js';
2
+ /**
3
+ * Identifies the location of a snapshot within the storage hierarchy.
4
+ */
5
+ export type SnapshotLocation = {
6
+ /** Session identifier */
7
+ sessionId: string;
8
+ /** Scope of the snapshot (agent or multi-agent) */
9
+ scope: Scope;
10
+ /** Scope-specific identifier (agentId or multiAgentId) */
11
+ scopeId: string;
12
+ };
13
+ /**
14
+ * SessionStorage configuration for pluggable storage backends.
15
+ * Allows users to configure snapshot and transcript storage independently.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const storage: SessionStorage = {
20
+ * snapshot: new S3Storage({ bucket: 'my-bucket' })
21
+ * }
22
+ * ```
23
+ */
24
+ export type SessionStorage = {
25
+ snapshot: SnapshotStorage;
26
+ };
27
+ /**
28
+ * Interface for snapshot persistence.
29
+ * Implementations provide storage backends (S3, filesystem, etc.).
30
+ *
31
+ * File layout convention:
32
+ * ```
33
+ * sessions/<session_id>/
34
+ * scopes/
35
+ * agent/<scope_id>/
36
+ * snapshots/
37
+ * snapshot_latest.json
38
+ * manifest.json
39
+ * immutable_history/
40
+ * snapshot_00001.json
41
+ * snapshot_00002.json
42
+ * ```
43
+ */
44
+ export interface SnapshotStorage {
45
+ /**
46
+ * Persists a snapshot to storage.
47
+ */
48
+ saveSnapshot(params: {
49
+ location: SnapshotLocation;
50
+ snapshotId: string;
51
+ isLatest: boolean;
52
+ snapshot: Snapshot;
53
+ }): Promise<void>;
54
+ /**
55
+ * Loads a snapshot from storage.
56
+ */
57
+ loadSnapshot(params: {
58
+ location: SnapshotLocation;
59
+ snapshotId?: string;
60
+ }): Promise<Snapshot | null>;
61
+ /**
62
+ * Lists all available snapshot IDs for a session scope.
63
+ *
64
+ * TODO: Add pagination support for long-running agents with many snapshots.
65
+ * Future signature could be:
66
+ * ```typescript
67
+ * listSnapshots(params: {
68
+ * location: SnapshotLocation
69
+ * limit?: number // Max results to return (e.g., 100)
70
+ * startAfter?: string // Snapshot ID to start after (for cursor-based pagination)
71
+ * }): Promise<{ snapshotIds: string[]; nextToken?: string }>
72
+ * ```
73
+ */
74
+ listSnapshotIds(params: {
75
+ location: SnapshotLocation;
76
+ }): Promise<string[]>;
77
+ /**
78
+ * Loads the snapshot manifest.
79
+ */
80
+ loadManifest(params: {
81
+ location: SnapshotLocation;
82
+ }): Promise<SnapshotManifest>;
83
+ /**
84
+ * Saves the snapshot manifest.
85
+ */
86
+ saveManifest(params: {
87
+ location: SnapshotLocation;
88
+ manifest: SnapshotManifest;
89
+ }): Promise<void>;
90
+ }
91
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/session/storage.ts"],"names":[],"mappings":"AAAA,OAAO,K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
1
+ export {};
2
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/session/storage.ts"],"names":[],"mappings":""}
@@ -0,0 +1,49 @@
1
+ import type { AgentData } from '../types/agent.js';
2
+ export type { Snapshot, Scope } from '../agent/snapshot.js';
3
+ /**
4
+ * Manifest tracks snapshot metadata and ID allocation.
5
+ * Stored alongside snapshots to manage versioning.
6
+ */
7
+ export interface SnapshotManifest {
8
+ /** Schema version for forward/backward compatibility */
9
+ schemaVersion: string;
10
+ /** Next available snapshot ID for allocation */
11
+ nextSnapshotId: string;
12
+ /** ISO 8601 timestamp of last manifest update */
13
+ updatedAt: string;
14
+ }
15
+ /**
16
+ * Parameters passed to SnapshotTriggerCallback to determine when to create snapshots.
17
+ */
18
+ export interface SnapshotTriggerParams {
19
+ /** Number of agent invocations (turns) since session started */
20
+ turnCount: number;
21
+ /** Timestamp (ms) of last immutable snapshot creation, undefined if no snapshot yet */
22
+ lastSnapshotAt?: number;
23
+ /** Current agent data including messages and state */
24
+ agentData: AgentData;
25
+ }
26
+ /**
27
+ * Callback function to determine when to create immutable snapshots.
28
+ * Called after each agent invocation to decide if a snapshot should be saved.
29
+ *
30
+ * @param params - Snapshot trigger parameters
31
+ * @returns true to create a snapshot, false to skip
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * // Snapshot every 5 turns
36
+ * const trigger: SnapshotTriggerCallback = ({ turnCount }) => turnCount % 5 === 0
37
+ *
38
+ * // Snapshot every 60 seconds
39
+ * const trigger: SnapshotTriggerCallback = ({ lastSnapshotAt }) => {
40
+ * if (!lastSnapshotAt) return false
41
+ * return Date.now() - lastSnapshotAt > 60000
42
+ * }
43
+ *
44
+ * // Snapshot when conversation exceeds 10 messages
45
+ * const trigger: SnapshotTriggerCallback = ({ agentData }) => agentData.messages.length > 10
46
+ * ```
47
+ */
48
+ export type SnapshotTriggerCallback = (params: SnapshotTriggerParams) => boolean;
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/session/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAGlD,YAAY,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAE3D;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,wDAAwD;IACxD,aAAa,EAAE,MAAM,CAAA;IACrB,gDAAgD;IAChD,cAAc,EAAE,MAAM,CAAA;IACtB,iDAAiD;IACjD,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAA;IACjB,uFAAuF;IACvF,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,sDAAsD;IACtD,SAAS,EAAE,SAAS,CAAA;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,uBAAuB,GAAG,CAAC,MAAM,EAAE,qBAAqB,KAAK,OAAO,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/session/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Validates that an identifier contains only allowed characters.
3
+ * Allowed characters: lowercase letters (a-z), numbers (0-9), hyphens (-), and underscores (_)
4
+ *
5
+ * @param id - The identifier to validate
6
+ * @returns The validated identifier
7
+ * @throws Error if identifier contains invalid characters
8
+ */
9
+ export declare function validateIdentifier(id: string): string;
10
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/session/validation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAMrD"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Validates that an identifier contains only allowed characters.
3
+ * Allowed characters: lowercase letters (a-z), numbers (0-9), hyphens (-), and underscores (_)
4
+ *
5
+ * @param id - The identifier to validate
6
+ * @returns The validated identifier
7
+ * @throws Error if identifier contains invalid characters
8
+ */
9
+ export function validateIdentifier(id) {
10
+ const validPattern = /^[a-z0-9_-]+$/;
11
+ if (!validPattern.test(id)) {
12
+ throw new Error(`Identifier '${id}' can only contain lowercase letters, numbers, hyphens, and underscores`);
13
+ }
14
+ return id;
15
+ }
16
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../../src/session/validation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,EAAU;IAC3C,MAAM,YAAY,GAAG,eAAe,CAAA;IACpC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,KAAK,CAAC,eAAe,EAAE,yEAAyE,CAAC,CAAA;IAC7G,CAAC;IACD,OAAO,EAAE,CAAA;AACX,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=context.test.d.ts.map