@agentstep/agent-sdk 0.5.0 → 0.5.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.
Files changed (188) hide show
  1. package/dist/auth/middleware.js +8 -8
  2. package/dist/backends/claude/args.js +7 -7
  3. package/dist/backends/claude/index.js +12 -12
  4. package/dist/backends/claude/wrapper-script.js +1 -1
  5. package/dist/backends/codex/auth.js +6 -6
  6. package/dist/backends/codex/index.js +11 -11
  7. package/dist/backends/factory/auth.js +6 -6
  8. package/dist/backends/factory/index.js +13 -13
  9. package/dist/backends/gemini/auth.js +6 -6
  10. package/dist/backends/gemini/index.js +11 -11
  11. package/dist/backends/opencode/auth.js +6 -6
  12. package/dist/backends/opencode/index.js +15 -15
  13. package/dist/backends/pi/auth.js +6 -6
  14. package/dist/backends/pi/index.js +13 -13
  15. package/dist/backends/registry.js +31 -31
  16. package/dist/backends/shared/wrap-prompt.js +1 -1
  17. package/dist/chunk-3LUY2POB.js +50 -0
  18. package/dist/{chunk-3PA4NXRP.js → chunk-3VPTHB7I.js} +2 -2
  19. package/dist/{chunk-YMCS6AB7.js → chunk-4236UQNZ.js} +2 -2
  20. package/dist/{chunk-HMOSAXVZ.js → chunk-4A4F4L4H.js} +2 -2
  21. package/dist/{chunk-JFOHGHW5.js → chunk-4B5H2MFO.js} +59 -16
  22. package/dist/{chunk-OZFSKR2W.js → chunk-5525T7PL.js} +4 -4
  23. package/dist/{chunk-AXBM7HAR.js → chunk-5EFY6UGV.js} +1 -1
  24. package/dist/chunk-5UTTNXNJ.js +166 -0
  25. package/dist/{chunk-4RDILRIO.js → chunk-5YGYUGDQ.js} +3 -3
  26. package/dist/{chunk-HWR2HYQJ.js → chunk-6DPJVMEC.js} +1 -1
  27. package/dist/{chunk-PNZF7HIU.js → chunk-6KJS42ZL.js} +8 -7
  28. package/dist/{chunk-LQP6XGFU.js → chunk-6KQFP32G.js} +3 -3
  29. package/dist/{chunk-DMMNAQUM.js → chunk-6L4AGHAV.js} +2 -2
  30. package/dist/{chunk-WEC625LQ.js → chunk-6TJBZXD4.js} +1 -1
  31. package/dist/{chunk-QTXAWC5J.js → chunk-6URMGCAC.js} +2 -2
  32. package/dist/{chunk-DJZSPWG2.js → chunk-6WUYI3DJ.js} +4 -4
  33. package/dist/{chunk-H6OT5GUL.js → chunk-7APAOJIZ.js} +3 -3
  34. package/dist/{chunk-HVLYE4S5.js → chunk-7EDLLXFQ.js} +1 -1
  35. package/dist/chunk-7IYL2O7H.js +185 -0
  36. package/dist/{chunk-KGBKIJPF.js → chunk-7JAUY5SG.js} +3 -3
  37. package/dist/{chunk-NDTIDWBE.js → chunk-7XIAKW75.js} +1 -1
  38. package/dist/{chunk-TUEBRYPZ.js → chunk-A2W3USEZ.js} +1 -1
  39. package/dist/chunk-ACPP4KN7.js +164 -0
  40. package/dist/{chunk-22OUZJAV.js → chunk-AESA3ARX.js} +3 -3
  41. package/dist/{chunk-Z4LFLXRR.js → chunk-AIAF5ZZA.js} +1 -1
  42. package/dist/{chunk-GIMDS46L.js → chunk-ANS2TQYT.js} +3 -3
  43. package/dist/{chunk-T3FQPTOA.js → chunk-BCBDHUFY.js} +2 -2
  44. package/dist/{chunk-MCWCRZM4.js → chunk-BLTCDZ3O.js} +4 -4
  45. package/dist/{chunk-VP527YC5.js → chunk-BYIU4WAQ.js} +3 -3
  46. package/dist/{chunk-L26TVIB6.js → chunk-C52BVTYC.js} +8 -8
  47. package/dist/{chunk-EWIWVXXP.js → chunk-CA76BJWL.js} +44 -14
  48. package/dist/{chunk-DKLHYSPW.js → chunk-DZGFZMUE.js} +5 -5
  49. package/dist/{chunk-R3QHLKJG.js → chunk-E35XHOFL.js} +5 -5
  50. package/dist/{chunk-PZNAQBHQ.js → chunk-FILGQF2G.js} +2 -2
  51. package/dist/{chunk-JP7Y3TKK.js → chunk-FSVDF57K.js} +5 -5
  52. package/dist/{chunk-S7DFMJR5.js → chunk-FWJOTEVY.js} +3 -3
  53. package/dist/{chunk-XSNJ7NT2.js → chunk-G4DTLYOD.js} +1 -1
  54. package/dist/{chunk-IEZFRNLC.js → chunk-GBLR2CPP.js} +107 -9
  55. package/dist/{chunk-7SY65LWY.js → chunk-GEC3NFYO.js} +4 -4
  56. package/dist/{chunk-GLKWJESP.js → chunk-GZZHVIEG.js} +2 -2
  57. package/dist/{chunk-NHAYKVXG.js → chunk-IAKR572X.js} +8 -8
  58. package/dist/{chunk-GFSRNOPI.js → chunk-IBUFLAAJ.js} +7 -7
  59. package/dist/{chunk-OKT2J4ZB.js → chunk-IZ5TUMCF.js} +23 -7
  60. package/dist/{chunk-W3JMIUHV.js → chunk-KQTTKYZY.js} +1 -1
  61. package/dist/{chunk-AJL3T5JS.js → chunk-L2DCOJTQ.js} +3 -3
  62. package/dist/{chunk-JLUCJMAQ.js → chunk-LA6IFUVR.js} +70 -24
  63. package/dist/{chunk-AB7MPL3H.js → chunk-LW4OCFIP.js} +9 -7
  64. package/dist/{chunk-BCIFFAGW.js → chunk-M3752GFZ.js} +15 -5
  65. package/dist/{chunk-5U5LRAFJ.js → chunk-MTLM36QD.js} +2 -2
  66. package/dist/{chunk-A6VORSKD.js → chunk-MV42BTU3.js} +1 -1
  67. package/dist/{chunk-5MERXOLJ.js → chunk-NRX5WHHN.js} +4 -4
  68. package/dist/{chunk-T3HMVHDG.js → chunk-O2X4XBU7.js} +1 -1
  69. package/dist/{chunk-2YZOIFVN.js → chunk-P4NU74SP.js} +47 -0
  70. package/dist/{chunk-TVODT2UR.js → chunk-P4OCCRQW.js} +1 -1
  71. package/dist/{chunk-YXOCKQZU.js → chunk-P4VJADXA.js} +2 -2
  72. package/dist/{chunk-6QZ23WRF.js → chunk-P6LESAPO.js} +54 -20
  73. package/dist/{chunk-HY3T4YJV.js → chunk-PICGSLMG.js} +5 -5
  74. package/dist/{chunk-64IQEPSD.js → chunk-PQKIXSEP.js} +2 -2
  75. package/dist/{chunk-GV6GUSCP.js → chunk-Q2JYAAJO.js} +9 -9
  76. package/dist/{chunk-IU457W7Q.js → chunk-QBJOYOVF.js} +2 -1
  77. package/dist/{chunk-MAJWADF7.js → chunk-RZOCX57A.js} +2 -2
  78. package/dist/{chunk-4D345E27.js → chunk-SQZPJFBF.js} +2 -2
  79. package/dist/{chunk-OSNMIPHV.js → chunk-TE6KCRPP.js} +1 -1
  80. package/dist/{chunk-Z6VZYRVN.js → chunk-TF36SWHG.js} +2 -2
  81. package/dist/{chunk-KUWJJD6O.js → chunk-TSR33JMZ.js} +14 -5
  82. package/dist/{chunk-L3IACZ72.js → chunk-TWH4UH6G.js} +1 -1
  83. package/dist/{chunk-445EL6J5.js → chunk-UAEVL47I.js} +1 -1
  84. package/dist/{chunk-X6QIWZ33.js → chunk-UGH5JDXX.js} +3 -3
  85. package/dist/{chunk-FSQ4HGHX.js → chunk-UJW3I3TX.js} +3 -3
  86. package/dist/{chunk-I26QP3A3.js → chunk-UQZFFG7U.js} +1 -1
  87. package/dist/{chunk-ILHIHMO3.js → chunk-VC5DELR4.js} +37 -2
  88. package/dist/{chunk-NKQVOAWN.js → chunk-VMLXV2H6.js} +1 -1
  89. package/dist/{chunk-FCUXFLNK.js → chunk-VY6IWBSK.js} +17 -4
  90. package/dist/{chunk-6CVQDSTS.js → chunk-WDAFSCYQ.js} +1 -1
  91. package/dist/{chunk-GCQDNUS2.js → chunk-WFRHLTKI.js} +3 -3
  92. package/dist/{chunk-R6EEBWM3.js → chunk-WRLCJNQF.js} +1 -1
  93. package/dist/{chunk-KKAJC3Z2.js → chunk-X4ARJS6I.js} +10 -10
  94. package/dist/{chunk-MQBMXAPU.js → chunk-Y4MI27HU.js} +1 -1
  95. package/dist/{chunk-PN3AWRMX.js → chunk-YDP3YA5D.js} +17 -17
  96. package/dist/{chunk-FVBDEOTZ.js → chunk-YE2RMJY7.js} +18 -4
  97. package/dist/{chunk-IRW7AYTP.js → chunk-YEWYRPN4.js} +42 -4
  98. package/dist/{chunk-24IDJ7LY.js → chunk-YL6IQUJC.js} +4 -4
  99. package/dist/{chunk-CLSGNQ7J.js → chunk-Z46R47QX.js} +8 -8
  100. package/dist/{chunk-STJNO6SL.js → chunk-Z6OPLBPQ.js} +1 -1
  101. package/dist/{chunk-RYJXSXCV.js → chunk-ZEJOFHR7.js} +5 -5
  102. package/dist/{chunk-EOJ66GY7.js → chunk-ZFGG62JW.js} +4 -4
  103. package/dist/config/index.js +5 -5
  104. package/dist/containers/client.js +6 -6
  105. package/dist/containers/exec.js +6 -6
  106. package/dist/containers/lifecycle.js +45 -43
  107. package/dist/containers/setup.js +9 -9
  108. package/dist/db/agents.js +6 -6
  109. package/dist/db/api_keys.js +5 -5
  110. package/dist/db/audit.js +3 -3
  111. package/dist/db/batch.js +10 -10
  112. package/dist/db/client.js +2 -2
  113. package/dist/db/credentials.js +3 -3
  114. package/dist/db/drizzle.js +4 -4
  115. package/dist/db/environments.js +6 -6
  116. package/dist/db/events.js +5 -5
  117. package/dist/db/files.js +5 -5
  118. package/dist/db/memory.js +13 -5
  119. package/dist/db/migrations.js +1 -1
  120. package/dist/db/proxy.js +5 -5
  121. package/dist/db/schema.js +5 -1
  122. package/dist/db/session-resources.js +5 -5
  123. package/dist/db/sessions.js +8 -8
  124. package/dist/db/sync.js +5 -5
  125. package/dist/db/tenants.js +3 -3
  126. package/dist/db/threads.js +27 -0
  127. package/dist/db/traces.js +5 -5
  128. package/dist/db/upstream_keys.js +3 -3
  129. package/dist/db/vaults.js +6 -6
  130. package/dist/dreaming/prompts.js +9 -0
  131. package/dist/dreaming/review.js +22 -0
  132. package/dist/handlers/agents.js +59 -59
  133. package/dist/handlers/api_keys.js +60 -60
  134. package/dist/handlers/audit.js +60 -60
  135. package/dist/handlers/batch.js +60 -60
  136. package/dist/handlers/credentials.js +60 -60
  137. package/dist/handlers/environments.js +60 -60
  138. package/dist/handlers/events.js +63 -63
  139. package/dist/handlers/files.js +60 -60
  140. package/dist/handlers/index.js +159 -144
  141. package/dist/handlers/license.js +59 -59
  142. package/dist/handlers/memory.js +66 -60
  143. package/dist/handlers/metrics.js +59 -59
  144. package/dist/handlers/models.js +60 -60
  145. package/dist/handlers/providers.js +59 -59
  146. package/dist/handlers/resources.js +59 -59
  147. package/dist/handlers/sessions.js +63 -62
  148. package/dist/handlers/settings.js +59 -59
  149. package/dist/handlers/skills-write.js +59 -59
  150. package/dist/handlers/skills.js +60 -60
  151. package/dist/handlers/stream.js +59 -59
  152. package/dist/handlers/tenants.js +60 -60
  153. package/dist/handlers/threads.js +70 -61
  154. package/dist/handlers/traces.js +60 -60
  155. package/dist/handlers/upstream_keys.js +62 -62
  156. package/dist/handlers/vaults.js +59 -59
  157. package/dist/handlers/whoami.js +59 -59
  158. package/dist/http.js +58 -58
  159. package/dist/index.js +72 -67
  160. package/dist/init.js +55 -55
  161. package/dist/lib/model-registry.js +6 -6
  162. package/dist/lib/skills-cache.js +6 -6
  163. package/dist/observability/otlp.js +12 -12
  164. package/dist/observability/redactor.js +8 -8
  165. package/dist/providers/fly.js +5 -5
  166. package/dist/providers/modal.js +5 -5
  167. package/dist/providers/registry.js +1 -1
  168. package/dist/providers/resolve-secrets.js +7 -7
  169. package/dist/providers/sprites.js +7 -7
  170. package/dist/providers/upstream-keys.js +12 -12
  171. package/dist/providers/vercel.js +6 -6
  172. package/dist/proxy/forward.js +6 -6
  173. package/dist/queue/index.js +6 -6
  174. package/dist/sessions/bus.js +10 -10
  175. package/dist/sessions/driver.js +48 -48
  176. package/dist/sessions/grader.js +5 -5
  177. package/dist/sessions/secrets.js +8 -8
  178. package/dist/sessions/sweeper.js +44 -44
  179. package/dist/sessions/threads.js +121 -55
  180. package/dist/shutdown.js +45 -45
  181. package/dist/sync/anthropic.js +10 -10
  182. package/dist/sync/container-file-sync.js +6 -6
  183. package/dist/sync/file-sync.js +17 -17
  184. package/dist/sync/memory-sync.js +79 -0
  185. package/package.json +1 -1
  186. package/dist/chunk-XTKTIFHC.js +0 -48
  187. package/dist/{chunk-UE6DNLSV.js → chunk-LT5PRO54.js} +0 -0
  188. package/dist/{dist-EY25RQ2S.js → dist-S64ZABZX.js} +3 -3
@@ -0,0 +1,185 @@
1
+ import {
2
+ getAgent
3
+ } from "./chunk-M3752GFZ.js";
4
+ import {
5
+ init_ids,
6
+ newId
7
+ } from "./chunk-F4WUVOLE.js";
8
+ import {
9
+ init_clock,
10
+ nowMs,
11
+ toIso
12
+ } from "./chunk-HFDLUBWN.js";
13
+ import {
14
+ getDrizzle,
15
+ init_drizzle
16
+ } from "./chunk-FILGQF2G.js";
17
+ import {
18
+ schema_exports
19
+ } from "./chunk-VC5DELR4.js";
20
+
21
+ // src/db/threads.ts
22
+ init_drizzle();
23
+ import { eq, and, isNull, lt, gt, asc, desc } from "drizzle-orm";
24
+ init_ids();
25
+ init_clock();
26
+ var MAX_THREADS_PER_SESSION = 25;
27
+ function hydrate(row) {
28
+ const agent = getAgent(row.agent_id, row.agent_version);
29
+ const agentEmbed = agent ? {
30
+ type: "agent",
31
+ id: agent.id,
32
+ version: agent.version,
33
+ name: agent.name,
34
+ description: agent.description,
35
+ model: agent.model,
36
+ system: agent.system,
37
+ tools: agent.tools,
38
+ mcp_servers: agent.mcp_servers,
39
+ skills: agent.skills
40
+ } : {
41
+ type: "agent",
42
+ id: row.agent_id,
43
+ version: row.agent_version,
44
+ name: "",
45
+ description: "",
46
+ model: { id: "" },
47
+ system: null,
48
+ tools: [],
49
+ mcp_servers: [],
50
+ skills: []
51
+ };
52
+ return {
53
+ type: "session_thread",
54
+ id: row.id,
55
+ session_id: row.session_id,
56
+ status: row.status,
57
+ agent: agentEmbed,
58
+ parent_thread_id: row.parent_thread_id ?? null,
59
+ created_at: toIso(row.created_at),
60
+ updated_at: toIso(row.updated_at),
61
+ archived_at: row.archived_at ? toIso(row.archived_at) : null,
62
+ usage: {
63
+ input_tokens: row.usage_input_tokens,
64
+ output_tokens: row.usage_output_tokens,
65
+ cache_read_input_tokens: row.usage_cache_read_input_tokens,
66
+ cache_creation: {
67
+ ephemeral_5m_input_tokens: row.usage_cache_creation_input_tokens,
68
+ ephemeral_1h_input_tokens: 0
69
+ }
70
+ },
71
+ stop_reason: row.stop_reason ?? null
72
+ };
73
+ }
74
+ function createThread(opts) {
75
+ const db = getDrizzle();
76
+ const existing = db.select({ id: schema_exports.sessionThreads.id }).from(schema_exports.sessionThreads).where(
77
+ and(
78
+ eq(schema_exports.sessionThreads.session_id, opts.sessionId),
79
+ isNull(schema_exports.sessionThreads.archived_at)
80
+ )
81
+ ).all();
82
+ if (existing.length >= MAX_THREADS_PER_SESSION) {
83
+ throw new Error(`max threads per session reached (${MAX_THREADS_PER_SESSION})`);
84
+ }
85
+ const id = newId("sth");
86
+ const now = nowMs();
87
+ db.insert(schema_exports.sessionThreads).values({
88
+ id,
89
+ session_id: opts.sessionId,
90
+ agent_id: opts.agentId,
91
+ agent_version: opts.agentVersion,
92
+ parent_thread_id: opts.parentThreadId ?? null,
93
+ status: "idle",
94
+ usage_input_tokens: 0,
95
+ usage_output_tokens: 0,
96
+ usage_cache_read_input_tokens: 0,
97
+ usage_cache_creation_input_tokens: 0,
98
+ created_at: now,
99
+ updated_at: now
100
+ }).run();
101
+ return getThread(opts.sessionId, id);
102
+ }
103
+ function getThread(sessionId, threadId) {
104
+ const db = getDrizzle();
105
+ const row = db.select().from(schema_exports.sessionThreads).where(
106
+ and(
107
+ eq(schema_exports.sessionThreads.id, threadId),
108
+ eq(schema_exports.sessionThreads.session_id, sessionId)
109
+ )
110
+ ).get();
111
+ if (!row) return void 0;
112
+ return hydrate(row);
113
+ }
114
+ function listThreads(sessionId, opts) {
115
+ const db = getDrizzle();
116
+ const limit = Math.min(Math.max(opts?.limit ?? 50, 1), 100);
117
+ const orderDir = opts?.order === "asc" ? "asc" : "desc";
118
+ const conditions = [eq(schema_exports.sessionThreads.session_id, sessionId)];
119
+ if (opts?.cursor) {
120
+ conditions.push(
121
+ orderDir === "desc" ? lt(schema_exports.sessionThreads.id, opts.cursor) : gt(schema_exports.sessionThreads.id, opts.cursor)
122
+ );
123
+ }
124
+ const orderClause = orderDir === "desc" ? desc(schema_exports.sessionThreads.created_at) : asc(schema_exports.sessionThreads.created_at);
125
+ const rows = db.select().from(schema_exports.sessionThreads).where(and(...conditions)).orderBy(orderClause).limit(limit).all();
126
+ return rows.map(hydrate);
127
+ }
128
+ function updateThreadStatus(threadId, status, stopReason) {
129
+ const db = getDrizzle();
130
+ const now = nowMs();
131
+ db.update(schema_exports.sessionThreads).set({
132
+ status,
133
+ stop_reason: stopReason ?? null,
134
+ updated_at: now
135
+ }).where(eq(schema_exports.sessionThreads.id, threadId)).run();
136
+ }
137
+ function updateThreadUsage(threadId, usage) {
138
+ const db = getDrizzle();
139
+ const now = nowMs();
140
+ const row = db.select().from(schema_exports.sessionThreads).where(eq(schema_exports.sessionThreads.id, threadId)).get();
141
+ if (!row) return;
142
+ db.update(schema_exports.sessionThreads).set({
143
+ usage_input_tokens: row.usage_input_tokens + (usage.input_tokens ?? 0),
144
+ usage_output_tokens: row.usage_output_tokens + (usage.output_tokens ?? 0),
145
+ usage_cache_read_input_tokens: row.usage_cache_read_input_tokens + (usage.cache_read_input_tokens ?? 0),
146
+ usage_cache_creation_input_tokens: row.usage_cache_creation_input_tokens + (usage.cache_creation_input_tokens ?? 0),
147
+ updated_at: now
148
+ }).where(eq(schema_exports.sessionThreads.id, threadId)).run();
149
+ }
150
+ function archiveThread(sessionId, threadId) {
151
+ const db = getDrizzle();
152
+ const row = db.select().from(schema_exports.sessionThreads).where(
153
+ and(
154
+ eq(schema_exports.sessionThreads.id, threadId),
155
+ eq(schema_exports.sessionThreads.session_id, sessionId)
156
+ )
157
+ ).get();
158
+ if (!row) return void 0;
159
+ if (row.status !== "idle") {
160
+ throw new Error(`cannot archive thread in status "${row.status}" \u2014 must be idle`);
161
+ }
162
+ const now = nowMs();
163
+ db.update(schema_exports.sessionThreads).set({ archived_at: now, updated_at: now }).where(eq(schema_exports.sessionThreads.id, threadId)).run();
164
+ return getThread(sessionId, threadId);
165
+ }
166
+ function countActiveThreads(sessionId) {
167
+ const db = getDrizzle();
168
+ const rows = db.select({ id: schema_exports.sessionThreads.id }).from(schema_exports.sessionThreads).where(
169
+ and(
170
+ eq(schema_exports.sessionThreads.session_id, sessionId),
171
+ isNull(schema_exports.sessionThreads.archived_at)
172
+ )
173
+ ).all();
174
+ return rows.length;
175
+ }
176
+
177
+ export {
178
+ createThread,
179
+ getThread,
180
+ listThreads,
181
+ updateThreadStatus,
182
+ updateThreadUsage,
183
+ archiveThread,
184
+ countActiveThreads
185
+ };
@@ -10,14 +10,14 @@ import {
10
10
  import {
11
11
  getDrizzle,
12
12
  init_drizzle
13
- } from "./chunk-PZNAQBHQ.js";
13
+ } from "./chunk-FILGQF2G.js";
14
14
  import {
15
15
  schema_exports
16
- } from "./chunk-ILHIHMO3.js";
16
+ } from "./chunk-VC5DELR4.js";
17
17
  import {
18
18
  getDb,
19
19
  init_client
20
- } from "./chunk-W3JMIUHV.js";
20
+ } from "./chunk-KQTTKYZY.js";
21
21
 
22
22
  // src/db/events.ts
23
23
  init_client();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getConfig
3
- } from "./chunk-X6QIWZ33.js";
3
+ } from "./chunk-UGH5JDXX.js";
4
4
 
5
5
  // src/backends/factory/auth.ts
6
6
  function buildFactoryAuthEnv() {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getConfig
3
- } from "./chunk-X6QIWZ33.js";
3
+ } from "./chunk-UGH5JDXX.js";
4
4
  import {
5
5
  ApiError
6
6
  } from "./chunk-EZYKRG4W.js";
@@ -0,0 +1,164 @@
1
+ import {
2
+ DREAMING_SYSTEM_PROMPT,
3
+ UPDATE_MEMORIES_TOOL
4
+ } from "./chunk-3LUY2POB.js";
5
+ import {
6
+ createOrUpsertMemory,
7
+ deleteMemory,
8
+ getMemoryByPath,
9
+ getMemoryStore,
10
+ listMemories
11
+ } from "./chunk-GBLR2CPP.js";
12
+ import {
13
+ listSessions
14
+ } from "./chunk-TSR33JMZ.js";
15
+ import {
16
+ listEvents
17
+ } from "./chunk-7JAUY5SG.js";
18
+ import {
19
+ init_clock,
20
+ nowMs
21
+ } from "./chunk-HFDLUBWN.js";
22
+
23
+ // src/dreaming/review.ts
24
+ init_clock();
25
+ var MAX_SESSIONS = 20;
26
+ var MAX_EVENTS_PER_SESSION = 50;
27
+ var RELEVANT_EVENT_TYPES = ["user.message", "agent.message", "session.error"];
28
+ function extractSessionSummary(sessionId, title, events) {
29
+ const parts = [];
30
+ const errors = [];
31
+ for (const evt of events) {
32
+ let payload;
33
+ try {
34
+ payload = JSON.parse(evt.payload_json);
35
+ } catch {
36
+ continue;
37
+ }
38
+ if (evt.type === "user.message") {
39
+ const content = payload.content;
40
+ if (Array.isArray(content)) {
41
+ const text = content.filter((b) => b.type === "text").map((b) => b.text ?? "").join(" ");
42
+ if (text) parts.push(`[User] ${text.slice(0, 500)}`);
43
+ } else if (typeof content === "string") {
44
+ parts.push(`[User] ${content.slice(0, 500)}`);
45
+ }
46
+ } else if (evt.type === "agent.message") {
47
+ const content = payload.content;
48
+ if (Array.isArray(content)) {
49
+ const text = content.filter((b) => b.type === "text").map((b) => b.text ?? "").join(" ");
50
+ if (text) parts.push(`[Agent] ${text.slice(0, 500)}`);
51
+ } else if (typeof content === "string") {
52
+ parts.push(`[Agent] ${content.slice(0, 500)}`);
53
+ }
54
+ } else if (evt.type === "session.error") {
55
+ const msg = payload.message ?? payload.error ?? JSON.stringify(payload);
56
+ errors.push(msg.slice(0, 200));
57
+ }
58
+ }
59
+ const header = `Session ${sessionId}${title ? ` (${title})` : ""}:`;
60
+ const body = parts.join("\n");
61
+ const errorSection = errors.length ? `
62
+ Errors: ${errors.join("; ")}` : "";
63
+ return `${header}
64
+ ${body}${errorSection}`;
65
+ }
66
+ async function reviewSessions(opts) {
67
+ const { storeId, lookbackMs, dryRun } = opts;
68
+ const store = getMemoryStore(storeId);
69
+ if (!store) {
70
+ throw new Error(`Memory store not found: ${storeId}`);
71
+ }
72
+ const apiKey = opts.apiKey || process.env.ANTHROPIC_API_KEY;
73
+ if (!apiKey) {
74
+ throw new Error("No Anthropic API key provided. Use --api-key or set ANTHROPIC_API_KEY.");
75
+ }
76
+ const cutoff = nowMs() - lookbackMs;
77
+ const sessions = listSessions({
78
+ statuses: ["idle", "terminated"],
79
+ createdGt: cutoff,
80
+ limit: MAX_SESSIONS,
81
+ order: "desc"
82
+ });
83
+ if (sessions.length === 0) {
84
+ return { sessionCount: 0, proposedChanges: [], applied: false };
85
+ }
86
+ const summaries = [];
87
+ for (const session of sessions) {
88
+ const events = listEvents(session.id, {
89
+ limit: MAX_EVENTS_PER_SESSION,
90
+ order: "asc"
91
+ });
92
+ const relevant = events.filter((e) => RELEVANT_EVENT_TYPES.includes(e.type));
93
+ if (relevant.length === 0) continue;
94
+ const summary = extractSessionSummary(session.id, session.title ?? null, relevant);
95
+ summaries.push(summary);
96
+ }
97
+ if (summaries.length === 0) {
98
+ return { sessionCount: sessions.length, proposedChanges: [], applied: false };
99
+ }
100
+ const memories = listMemories(storeId);
101
+ const memorySection = memories.length > 0 ? `## Current Memory Store Contents
102
+
103
+ ${memories.map((m) => `### ${m.path}
104
+ ${m.content}`).join("\n\n")}` : "## Current Memory Store Contents\n\n(empty \u2014 no memories yet)";
105
+ const sessionSection = `## Recent Sessions (${summaries.length})
106
+
107
+ ${summaries.join("\n\n---\n\n")}`;
108
+ const userMessage = `${sessionSection}
109
+
110
+ ${memorySection}
111
+
112
+ Analyze the sessions above and propose memory changes using the update_memories tool.`;
113
+ const maxChars = 2e5;
114
+ const truncatedMessage = userMessage.length > maxChars ? userMessage.slice(0, maxChars) + "\n\n[... truncated for context limits]" : userMessage;
115
+ const response = await fetch("https://api.anthropic.com/v1/messages", {
116
+ method: "POST",
117
+ headers: {
118
+ "content-type": "application/json",
119
+ "x-api-key": apiKey,
120
+ "anthropic-version": "2023-06-01"
121
+ },
122
+ body: JSON.stringify({
123
+ model: "claude-sonnet-4-20250514",
124
+ max_tokens: 4096,
125
+ system: DREAMING_SYSTEM_PROMPT,
126
+ tools: [UPDATE_MEMORIES_TOOL],
127
+ tool_choice: { type: "tool", name: "update_memories" },
128
+ messages: [{ role: "user", content: truncatedMessage }]
129
+ })
130
+ });
131
+ if (!response.ok) {
132
+ const errText = await response.text().catch(() => "");
133
+ throw new Error(`Anthropic API error (${response.status}): ${errText}`);
134
+ }
135
+ const data = await response.json();
136
+ const toolUse = data.content.find(
137
+ (b) => b.type === "tool_use" && b.name === "update_memories"
138
+ );
139
+ const changes = toolUse?.input?.changes ?? [];
140
+ if (!dryRun && changes.length > 0) {
141
+ for (const change of changes) {
142
+ if (change.operation === "create" || change.operation === "update") {
143
+ if (change.content) {
144
+ createOrUpsertMemory(storeId, change.path, change.content, "dream");
145
+ }
146
+ } else if (change.operation === "delete") {
147
+ const existing = getMemoryByPath(storeId, change.path);
148
+ if (existing) {
149
+ deleteMemory(existing.id, "dream");
150
+ }
151
+ }
152
+ }
153
+ }
154
+ return {
155
+ sessionCount: sessions.length,
156
+ proposedChanges: changes,
157
+ applied: !dryRun && changes.length > 0
158
+ };
159
+ }
160
+
161
+ export {
162
+ extractSessionSummary,
163
+ reviewSessions
164
+ };
@@ -5,14 +5,14 @@ import {
5
5
  import {
6
6
  getDrizzle,
7
7
  init_drizzle
8
- } from "./chunk-PZNAQBHQ.js";
8
+ } from "./chunk-FILGQF2G.js";
9
9
  import {
10
10
  schema_exports
11
- } from "./chunk-ILHIHMO3.js";
11
+ } from "./chunk-VC5DELR4.js";
12
12
  import {
13
13
  getDb,
14
14
  init_client
15
- } from "./chunk-W3JMIUHV.js";
15
+ } from "./chunk-KQTTKYZY.js";
16
16
 
17
17
  // src/db/sync.ts
18
18
  init_drizzle();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getConfig
3
- } from "./chunk-X6QIWZ33.js";
3
+ } from "./chunk-UGH5JDXX.js";
4
4
  import {
5
5
  serverBusy
6
6
  } from "./chunk-EZYKRG4W.js";
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  withGatewayPreamble
3
- } from "./chunk-FVBDEOTZ.js";
3
+ } from "./chunk-YE2RMJY7.js";
4
4
  import {
5
5
  resolveToolset
6
6
  } from "./chunk-XBHDQK4Z.js";
7
7
  import {
8
8
  getConfig
9
- } from "./chunk-X6QIWZ33.js";
9
+ } from "./chunk-UGH5JDXX.js";
10
10
 
11
11
  // src/backends/claude/args.ts
12
12
  function buildClaudeArgs(input) {
@@ -28,7 +28,7 @@ function buildClaudeArgs(input) {
28
28
  argv.push("--resume", input.claudeSessionId);
29
29
  }
30
30
  const tools = resolveToolset(input.agent.tools);
31
- let systemPrompt = withGatewayPreamble(input.agent.system);
31
+ let systemPrompt = withGatewayPreamble(input.agent.system, input.memoryStores);
32
32
  if (tools.customToolNames.size > 0) {
33
33
  const toolList = Array.from(tools.customToolNames).map((name) => `mcp__tool-bridge__${name}`).join(", ");
34
34
  systemPrompt += `
@@ -9,10 +9,10 @@ import {
9
9
  import {
10
10
  getDrizzle,
11
11
  init_drizzle
12
- } from "./chunk-PZNAQBHQ.js";
12
+ } from "./chunk-FILGQF2G.js";
13
13
  import {
14
14
  schema_exports
15
- } from "./chunk-ILHIHMO3.js";
15
+ } from "./chunk-VC5DELR4.js";
16
16
 
17
17
  // src/db/api_keys.ts
18
18
  init_drizzle();
@@ -1,20 +1,20 @@
1
1
  import {
2
2
  createSession
3
- } from "./chunk-KUWJJD6O.js";
3
+ } from "./chunk-TSR33JMZ.js";
4
4
  import {
5
5
  createEnvironment,
6
6
  deleteEnvironment,
7
7
  getEnvironment
8
- } from "./chunk-H6OT5GUL.js";
8
+ } from "./chunk-7APAOJIZ.js";
9
9
  import {
10
10
  archiveAgent,
11
11
  createAgent,
12
12
  getAgent
13
- } from "./chunk-BCIFFAGW.js";
13
+ } from "./chunk-M3752GFZ.js";
14
14
  import {
15
15
  getDrizzle,
16
16
  init_drizzle
17
- } from "./chunk-PZNAQBHQ.js";
17
+ } from "./chunk-FILGQF2G.js";
18
18
 
19
19
  // src/db/batch.ts
20
20
  init_drizzle();
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-AIBH32FN.js";
5
5
  import {
6
6
  DEFAULT_TENANT_ID
7
- } from "./chunk-I26QP3A3.js";
7
+ } from "./chunk-UQZFFG7U.js";
8
8
  import {
9
9
  init_ids,
10
10
  newId
@@ -17,10 +17,10 @@ import {
17
17
  import {
18
18
  getDrizzle,
19
19
  init_drizzle
20
- } from "./chunk-PZNAQBHQ.js";
20
+ } from "./chunk-FILGQF2G.js";
21
21
  import {
22
22
  schema_exports
23
- } from "./chunk-ILHIHMO3.js";
23
+ } from "./chunk-VC5DELR4.js";
24
24
 
25
25
  // src/db/vaults.ts
26
26
  init_drizzle();
@@ -1,3 +1,10 @@
1
+ import {
2
+ buildFactoryArgs
3
+ } from "./chunk-TPMZO6S2.js";
4
+ import {
5
+ buildFactoryAuthEnv,
6
+ validateFactoryRuntime
7
+ } from "./chunk-7XIAKW75.js";
1
8
  import {
2
9
  prepareFactoryOnSandbox
3
10
  } from "./chunk-YSUPRYX2.js";
@@ -7,16 +14,9 @@ import {
7
14
  import {
8
15
  FACTORY_WRAPPER_PATH
9
16
  } from "./chunk-QQDSHL27.js";
10
- import {
11
- buildFactoryArgs
12
- } from "./chunk-TPMZO6S2.js";
13
- import {
14
- buildFactoryAuthEnv,
15
- validateFactoryRuntime
16
- } from "./chunk-NDTIDWBE.js";
17
17
  import {
18
18
  wrapPromptWithSystem
19
- } from "./chunk-FVBDEOTZ.js";
19
+ } from "./chunk-YE2RMJY7.js";
20
20
  import {
21
21
  ApiError
22
22
  } from "./chunk-EZYKRG4W.js";
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  syncAndCreateSession
3
- } from "./chunk-7SY65LWY.js";
3
+ } from "./chunk-GEC3NFYO.js";
4
4
  import {
5
5
  resolveRemoteSessionId,
6
6
  upsertSync
7
- } from "./chunk-22OUZJAV.js";
7
+ } from "./chunk-AESA3ARX.js";
8
8
  import {
9
9
  assertResourceTenant,
10
10
  tenantFilter
@@ -12,18 +12,21 @@ import {
12
12
  import {
13
13
  interruptSession
14
14
  } from "./chunk-DC2UMEQH.js";
15
+ import {
16
+ getMemoryStore
17
+ } from "./chunk-GBLR2CPP.js";
15
18
  import {
16
19
  kickoffEnvironmentSetup
17
- } from "./chunk-LQP6XGFU.js";
20
+ } from "./chunk-6KQFP32G.js";
18
21
  import {
19
22
  decodeCursor,
20
23
  jsonOk,
21
24
  paginatedOk,
22
25
  routeWrap
23
- } from "./chunk-QTXAWC5J.js";
26
+ } from "./chunk-6URMGCAC.js";
24
27
  import {
25
28
  forwardToAnthropic
26
- } from "./chunk-MQBMXAPU.js";
29
+ } from "./chunk-Y4MI27HU.js";
27
30
  import {
28
31
  errors_exports,
29
32
  init_errors
@@ -33,18 +36,18 @@ import {
33
36
  isProxied,
34
37
  markProxied,
35
38
  unmarkProxied
36
- } from "./chunk-5U5LRAFJ.js";
39
+ } from "./chunk-MTLM36QD.js";
37
40
  import {
38
41
  dropActor,
39
42
  getActor
40
43
  } from "./chunk-LAWTTG2E.js";
41
44
  import {
42
45
  releaseSession
43
- } from "./chunk-JFOHGHW5.js";
46
+ } from "./chunk-4B5H2MFO.js";
44
47
  import {
45
48
  appendEvent,
46
49
  dropEmitter
47
- } from "./chunk-5MERXOLJ.js";
50
+ } from "./chunk-NRX5WHHN.js";
48
51
  import {
49
52
  archiveSession,
50
53
  createSession,
@@ -52,13 +55,13 @@ import {
52
55
  listSessions,
53
56
  updateSessionMutable,
54
57
  updateSessionStatus
55
- } from "./chunk-KUWJJD6O.js";
58
+ } from "./chunk-TSR33JMZ.js";
56
59
  import {
57
60
  getEnvironment
58
- } from "./chunk-H6OT5GUL.js";
61
+ } from "./chunk-7APAOJIZ.js";
59
62
  import {
60
63
  getAgent
61
- } from "./chunk-BCIFFAGW.js";
64
+ } from "./chunk-M3752GFZ.js";
62
65
  import {
63
66
  init_clock,
64
67
  nowMs
@@ -66,7 +69,7 @@ import {
66
69
  import {
67
70
  getDb,
68
71
  init_client
69
- } from "./chunk-W3JMIUHV.js";
72
+ } from "./chunk-KQTTKYZY.js";
70
73
  import {
71
74
  badRequest,
72
75
  notFound
@@ -115,7 +118,7 @@ var AgentRef = z.union([
115
118
  z.object({ id: z.string(), version: z.number().int(), type: z.literal("agent").optional() })
116
119
  ]);
117
120
  var ResourceSchema = z.object({
118
- type: z.enum(["uri", "text", "file", "github_repository"]),
121
+ type: z.enum(["uri", "text", "file", "github_repository", "memory_store"]),
119
122
  uri: z.string().optional(),
120
123
  content: z.string().optional(),
121
124
  file_id: z.string().optional(),
@@ -131,7 +134,11 @@ var ResourceSchema = z.object({
131
134
  name: z.string().optional(),
132
135
  sha: z.string().optional()
133
136
  }).optional(),
134
- authorization_token: z.string().optional()
137
+ authorization_token: z.string().optional(),
138
+ // Memory store resource fields
139
+ memory_store_id: z.string().optional(),
140
+ access: z.enum(["read_only", "read_write"]).optional(),
141
+ instructions: z.string().optional()
135
142
  });
136
143
  var CreateSchema = z.object({
137
144
  agent: AgentRef,
@@ -243,6 +250,20 @@ function handleCreateSession(request) {
243
250
  }
244
251
  }
245
252
  }
253
+ if (data.resources?.length) {
254
+ const memStoreResources = data.resources.filter((r) => r.type === "memory_store");
255
+ if (memStoreResources.length > 8) {
256
+ throw badRequest(`max 8 memory_store resources per session (got ${memStoreResources.length})`);
257
+ }
258
+ for (const r of memStoreResources) {
259
+ if (!r.memory_store_id) {
260
+ throw badRequest(`memory_store resource requires memory_store_id`);
261
+ }
262
+ const store = getMemoryStore(r.memory_store_id);
263
+ if (!store) throw notFound(`memory store not found: ${r.memory_store_id}`);
264
+ if (store.archived_at) throw badRequest(`memory store ${r.memory_store_id} is archived`);
265
+ }
266
+ }
246
267
  if (env.config?.provider === "anthropic") {
247
268
  const { resolveAnthropicKey } = await import("./providers/upstream-keys.js");
248
269
  const resolved = resolveAnthropicKey({ vaultIds: data.vault_ids ?? void 0 });
@@ -342,6 +363,15 @@ function handleCreateSession(request) {
342
363
  if (data.resources?.length) {
343
364
  const { createResource } = await import("./db/session-resources.js");
344
365
  for (const r of data.resources) {
366
+ if (r.type === "memory_store") {
367
+ createResource(session.id, {
368
+ type: "memory_store",
369
+ memory_store_id: r.memory_store_id,
370
+ access: r.access ?? "read_write",
371
+ instructions: r.instructions
372
+ });
373
+ continue;
374
+ }
345
375
  let repoUrl = r.type === "github_repository" ? r.repository_url ?? r.url : r.uri;
346
376
  if (r.type === "github_repository" && r.authorization_token && repoUrl) {
347
377
  try {