@lota-sdk/core 0.4.9 → 0.4.11

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 (182) hide show
  1. package/package.json +2 -2
  2. package/src/ai/embedding-cache.ts +3 -1
  3. package/src/ai-gateway/ai-gateway.ts +164 -82
  4. package/src/ai-gateway/index.ts +16 -1
  5. package/src/config/agent-defaults.ts +4 -107
  6. package/src/config/agent-types.ts +1 -1
  7. package/src/config/background-processing.ts +1 -1
  8. package/src/config/index.ts +0 -1
  9. package/src/config/logger.ts +22 -25
  10. package/src/config/thread-defaults.ts +1 -10
  11. package/src/create-runtime.ts +145 -670
  12. package/src/db/base.service.ts +30 -38
  13. package/src/db/memory-query-builder.ts +2 -1
  14. package/src/db/memory-store.ts +29 -20
  15. package/src/db/memory.ts +188 -195
  16. package/src/db/service-normalization.ts +97 -64
  17. package/src/db/service.ts +496 -384
  18. package/src/db/startup.ts +30 -19
  19. package/src/effect/helpers.ts +30 -5
  20. package/src/effect/index.ts +7 -7
  21. package/src/effect/layers.ts +75 -72
  22. package/src/effect/services.ts +15 -11
  23. package/src/embeddings/provider.ts +65 -71
  24. package/src/index.ts +13 -12
  25. package/src/queues/autonomous-job.queue.ts +177 -143
  26. package/src/queues/context-compaction.queue.ts +41 -39
  27. package/src/queues/delayed-node-promotion.queue.ts +61 -42
  28. package/src/queues/document-processor.queue.ts +5 -3
  29. package/src/queues/index.ts +1 -0
  30. package/src/queues/memory-consolidation.queue.ts +79 -53
  31. package/src/queues/organization-learning.queue.ts +70 -33
  32. package/src/queues/plan-agent-heartbeat.queue.ts +111 -83
  33. package/src/queues/plan-scheduler.queue.ts +101 -97
  34. package/src/queues/post-chat-memory.queue.ts +56 -46
  35. package/src/queues/queue-factory.ts +146 -69
  36. package/src/queues/queues.service.ts +61 -0
  37. package/src/queues/title-generation.queue.ts +44 -44
  38. package/src/redis/connection.ts +181 -164
  39. package/src/redis/org-memory-lock.ts +24 -9
  40. package/src/redis/redis-lease-lock.ts +8 -1
  41. package/src/redis/stream-context.ts +17 -9
  42. package/src/runtime/agent-identity-overrides.ts +7 -3
  43. package/src/runtime/agent-runtime-policy.ts +10 -5
  44. package/src/runtime/agent-stream-helpers.ts +24 -15
  45. package/src/runtime/chat-run-orchestration.ts +1 -1
  46. package/src/runtime/context-compaction/context-compaction-runtime.ts +28 -32
  47. package/src/runtime/context-compaction/context-compaction.ts +131 -85
  48. package/src/runtime/domain-layer.ts +203 -0
  49. package/src/runtime/execution-plan-visibility.ts +5 -2
  50. package/src/runtime/graph-designer.ts +0 -14
  51. package/src/runtime/helper-model.ts +8 -4
  52. package/src/runtime/index.ts +1 -1
  53. package/src/runtime/indexed-repositories-policy.ts +2 -6
  54. package/src/runtime/memory/memory-block.ts +19 -9
  55. package/src/runtime/memory/memory-pipeline.ts +53 -66
  56. package/src/runtime/memory/memory-scope.ts +33 -29
  57. package/src/runtime/plugin-resolution.ts +58 -62
  58. package/src/runtime/post-turn-side-effects.ts +139 -161
  59. package/src/runtime/retrieval-adapters.ts +4 -4
  60. package/src/runtime/runtime-config.ts +3 -9
  61. package/src/runtime/runtime-extensions.ts +0 -43
  62. package/src/runtime/runtime-lifecycle.ts +124 -0
  63. package/src/runtime/runtime-services.ts +455 -0
  64. package/src/runtime/runtime-worker-registry.ts +113 -30
  65. package/src/runtime/social-chat/social-chat-agent-runner.ts +13 -8
  66. package/src/runtime/social-chat/social-chat-history.ts +24 -13
  67. package/src/runtime/social-chat/social-chat.ts +420 -369
  68. package/src/runtime/team-consultation/team-consultation-orchestrator.ts +64 -57
  69. package/src/runtime/team-consultation/team-consultation-prompts.ts +11 -6
  70. package/src/runtime/thread-chat-helpers.ts +18 -9
  71. package/src/runtime/thread-turn-context.ts +28 -74
  72. package/src/runtime/turn-lifecycle.ts +6 -14
  73. package/src/services/agent-activity.service.ts +169 -176
  74. package/src/services/agent-executor.service.ts +207 -196
  75. package/src/services/artifact.service.ts +10 -5
  76. package/src/services/attachment.service.ts +16 -48
  77. package/src/services/autonomous-job.service.ts +81 -87
  78. package/src/services/background-work.service.ts +54 -0
  79. package/src/services/chat-run-registry.service.ts +3 -1
  80. package/src/services/context-compaction.service.ts +8 -10
  81. package/src/services/document-chunk.service.ts +8 -17
  82. package/src/services/execution-plan/execution-plan-graph.ts +122 -109
  83. package/src/services/execution-plan/execution-plan-schedule.ts +1 -15
  84. package/src/services/execution-plan/execution-plan.service.ts +68 -51
  85. package/src/services/feedback-loop.service.ts +1 -1
  86. package/src/services/global-orchestrator.service.ts +49 -15
  87. package/src/services/graph-full-routing.ts +49 -37
  88. package/src/services/index.ts +1 -0
  89. package/src/services/institutional-memory.service.ts +8 -17
  90. package/src/services/learned-skill.service.ts +38 -35
  91. package/src/services/memory/memory-conversation.ts +10 -5
  92. package/src/services/memory/memory-errors.ts +27 -0
  93. package/src/services/memory/memory-org-memory.ts +14 -3
  94. package/src/services/memory/memory-preseeded.ts +10 -4
  95. package/src/services/memory/memory-utils.ts +2 -1
  96. package/src/services/memory/memory.service.ts +37 -52
  97. package/src/services/memory/rerank.service.ts +3 -11
  98. package/src/services/monitoring-window.service.ts +1 -1
  99. package/src/services/mutating-approval.service.ts +1 -1
  100. package/src/services/node-workspace.service.ts +2 -2
  101. package/src/services/notification.service.ts +16 -4
  102. package/src/services/organization-member.service.ts +1 -1
  103. package/src/services/organization.service.ts +34 -51
  104. package/src/services/ownership-dispatcher.service.ts +148 -95
  105. package/src/services/plan/plan-agent-heartbeat.service.ts +30 -16
  106. package/src/services/plan/plan-agent-query.service.ts +13 -9
  107. package/src/services/plan/plan-approval.service.ts +52 -48
  108. package/src/services/plan/plan-artifact.service.ts +2 -2
  109. package/src/services/plan/plan-builder.service.ts +2 -2
  110. package/src/services/plan/plan-checkpoint.service.ts +1 -1
  111. package/src/services/plan/plan-compiler.service.ts +1 -1
  112. package/src/services/plan/plan-completion-side-effects.ts +99 -113
  113. package/src/services/plan/plan-coordination.service.ts +1 -1
  114. package/src/services/plan/plan-cycle.service.ts +171 -202
  115. package/src/services/plan/plan-deadline.service.ts +304 -307
  116. package/src/services/plan/plan-event-delivery.service.ts +84 -72
  117. package/src/services/plan/plan-executor-context.ts +2 -0
  118. package/src/services/plan/plan-executor-graph.ts +375 -353
  119. package/src/services/plan/plan-executor-helpers.ts +60 -75
  120. package/src/services/plan/plan-executor.service.ts +494 -489
  121. package/src/services/plan/plan-run.service.ts +12 -19
  122. package/src/services/plan/plan-scheduler.service.ts +89 -82
  123. package/src/services/plan/plan-template.service.ts +1 -1
  124. package/src/services/plan/plan-transaction-events.ts +8 -5
  125. package/src/services/plan/plan-validator.service.ts +1 -1
  126. package/src/services/plan/plan-workspace.service.ts +17 -11
  127. package/src/services/plugin-executor.service.ts +26 -21
  128. package/src/services/quality-metrics.service.ts +1 -1
  129. package/src/services/queue-job.service.ts +8 -17
  130. package/src/services/recent-activity-title.service.ts +22 -10
  131. package/src/services/recent-activity.service.ts +1 -1
  132. package/src/services/skill-resolver.service.ts +1 -1
  133. package/src/services/social-chat-history.service.ts +37 -20
  134. package/src/services/system-executor.service.ts +25 -20
  135. package/src/services/thread/thread-bootstrap.ts +37 -19
  136. package/src/services/thread/thread-listing.ts +2 -1
  137. package/src/services/thread/thread-memory-block.ts +18 -5
  138. package/src/services/thread/thread-message.service.ts +30 -13
  139. package/src/services/thread/thread-title.service.ts +1 -1
  140. package/src/services/thread/thread-turn-execution.ts +87 -83
  141. package/src/services/thread/thread-turn-preparation.service.ts +65 -40
  142. package/src/services/thread/thread-turn-streaming.ts +32 -36
  143. package/src/services/thread/thread-turn.ts +43 -29
  144. package/src/services/thread/thread.service.ts +32 -8
  145. package/src/services/user.service.ts +1 -1
  146. package/src/services/write-intent-validator.service.ts +1 -1
  147. package/src/storage/attachment-storage.service.ts +7 -4
  148. package/src/storage/generated-document-storage.service.ts +1 -1
  149. package/src/system-agents/context-compaction.agent.ts +1 -1
  150. package/src/system-agents/helper-agent-options.ts +1 -1
  151. package/src/system-agents/memory-reranker.agent.ts +1 -1
  152. package/src/system-agents/memory.agent.ts +1 -1
  153. package/src/system-agents/recent-activity-title-refiner.agent.ts +9 -6
  154. package/src/system-agents/regular-chat-memory-digest.agent.ts +1 -1
  155. package/src/system-agents/skill-extractor.agent.ts +1 -1
  156. package/src/system-agents/skill-manager.agent.ts +1 -1
  157. package/src/system-agents/thread-router.agent.ts +23 -20
  158. package/src/system-agents/title-generator.agent.ts +1 -1
  159. package/src/tools/execution-plan.tool.ts +36 -20
  160. package/src/tools/fetch-webpage.tool.ts +30 -22
  161. package/src/tools/firecrawl-client.ts +1 -6
  162. package/src/tools/plan-approval.tool.ts +9 -1
  163. package/src/tools/remember-memory.tool.ts +3 -6
  164. package/src/tools/research-topic.tool.ts +12 -3
  165. package/src/tools/search-web.tool.ts +26 -18
  166. package/src/tools/search.tool.ts +4 -5
  167. package/src/tools/team-think.tool.ts +139 -121
  168. package/src/utils/async.ts +15 -6
  169. package/src/utils/errors.ts +27 -15
  170. package/src/workers/bootstrap.ts +34 -58
  171. package/src/workers/memory-consolidation.worker.ts +4 -1
  172. package/src/workers/organization-learning.worker.ts +16 -3
  173. package/src/workers/regular-chat-memory-digest.helpers.ts +3 -4
  174. package/src/workers/regular-chat-memory-digest.runner.ts +46 -29
  175. package/src/workers/skill-extraction.runner.ts +13 -15
  176. package/src/workers/worker-utils.ts +14 -8
  177. package/src/config/search.ts +0 -3
  178. package/src/effect/awaitable-effect.ts +0 -87
  179. package/src/effect/runtime-ref.ts +0 -25
  180. package/src/effect/runtime.ts +0 -31
  181. package/src/redis/runtime-connection.ts +0 -10
  182. package/src/runtime/agent-types.ts +0 -1
@@ -1,13 +1,11 @@
1
1
  /**
2
2
  * Normalization helpers for SurrealDB service inputs.
3
3
  *
4
- * Functions in this module throw `SurrealDBError` on invalid inputs.
5
- * Callers in `service.ts` invoke these inside `Effect.tryPromise` `try` blocks
6
- * or `Effect.gen` generators, so thrown errors are caught by the surrounding
7
- * `catch` handler and surfaced as typed `SurrealDBError` failures.
8
- * Do not call these outside an Effect error boundary.
4
+ * Helpers that can fail return `Effect<T, SurrealDBError>` so callers can
5
+ * `yield*` them inside `Effect.gen` and use `catchTag('SurrealDBError')`
6
+ * without losing the typed failure channel.
9
7
  */
10
- import { Schema } from 'effect'
8
+ import { Effect, Schema } from 'effect'
11
9
  import { BoundQuery, RecordId, StringRecordId, Table, and, eq } from 'surrealdb'
12
10
  import type { ExprLike, Mutation } from 'surrealdb'
13
11
  import type { z } from 'zod'
@@ -29,6 +27,20 @@ type TransactionQueryDescriptor = { query: string; bindings?: Record<string, unk
29
27
 
30
28
  type ParseSchema = <TSchema extends z.ZodTypeAny>(schema: TSchema, value: unknown) => z.infer<TSchema>
31
29
 
30
+ function isSurrealDBError(error: unknown): error is SurrealDBError {
31
+ return isRecord(error) && error._tag === 'SurrealDBError'
32
+ }
33
+
34
+ function toSurrealDBError(error: unknown, fallbackMessage: string): SurrealDBError {
35
+ if (isSurrealDBError(error)) {
36
+ return error
37
+ }
38
+ if (error instanceof Error) {
39
+ return new SurrealDBError({ message: `${fallbackMessage}: ${error.message}`, cause: error })
40
+ }
41
+ return new SurrealDBError({ message: fallbackMessage })
42
+ }
43
+
32
44
  function isExplicitRecordIdValue(value: unknown): value is RecordIdInput {
33
45
  if (value instanceof RecordId || value instanceof StringRecordId) {
34
46
  return true
@@ -65,35 +77,38 @@ function isTransactionQueryDescriptor(value: unknown): value is TransactionQuery
65
77
  return isRecord(value) && typeof value.query === 'string'
66
78
  }
67
79
 
68
- export function normalizeTransactionRecordId(value: unknown, context: string): RecordId {
80
+ export function normalizeTransactionRecordId(value: unknown, context: string): Effect.Effect<RecordId, SurrealDBError> {
69
81
  if (!isRecordIdInput(value)) {
70
- throw new SurrealDBError({ message: `Invalid record id for ${context}` })
82
+ return Effect.fail(new SurrealDBError({ message: `Invalid record id for ${context}` }))
71
83
  }
72
84
 
73
- return ensureRecordId(value)
85
+ return Effect.try({
86
+ try: () => ensureRecordId(value),
87
+ catch: (error) => toSurrealDBError(error, `Invalid record id for ${context}`),
88
+ })
74
89
  }
75
90
 
76
- export function normalizeRecordIdForTable(id: unknown, table: DatabaseTable): ReturnType<typeof ensureRecordId> {
77
- try {
78
- if (!isRecordIdInput(id)) {
79
- throw new SurrealDBError({ message: `Invalid record id for table "${table}"` })
80
- }
81
-
82
- const recordId = ensureRecordId(id, table)
83
- const resolvedTable = String(recordId.table)
84
- if (resolvedTable !== table) {
85
- throw new SurrealDBError({ message: `Record id table mismatch: expected "${table}" but got "${resolvedTable}"` })
86
- }
87
- return recordId
88
- } catch (error) {
89
- if (error instanceof SurrealDBError) {
90
- throw error
91
- }
92
- if (error instanceof Error) {
93
- throw new SurrealDBError({ message: `Invalid record id for table "${table}": ${error.message}`, cause: error })
94
- }
95
- throw new SurrealDBError({ message: `Invalid record id for table "${table}"` })
91
+ export function normalizeRecordIdForTable(
92
+ id: unknown,
93
+ table: DatabaseTable,
94
+ ): Effect.Effect<ReturnType<typeof ensureRecordId>, SurrealDBError> {
95
+ if (!isRecordIdInput(id)) {
96
+ return Effect.fail(new SurrealDBError({ message: `Invalid record id for table "${table}"` }))
96
97
  }
98
+
99
+ return Effect.try({
100
+ try: () => {
101
+ const recordId = ensureRecordId(id, table)
102
+ const resolvedTable = String(recordId.table)
103
+ if (resolvedTable !== table) {
104
+ throw new SurrealDBError({
105
+ message: `Record id table mismatch: expected "${table}" but got "${resolvedTable}"`,
106
+ })
107
+ }
108
+ return recordId
109
+ },
110
+ catch: (error) => toSurrealDBError(error, `Invalid record id for table "${table}"`),
111
+ })
97
112
  }
98
113
 
99
114
  export function normalizeSurrealValue(value: unknown): unknown {
@@ -137,10 +152,11 @@ export function normalizeQueryRows(
137
152
  return schema ? [parseSchema(schema, statement)] : [statement]
138
153
  }
139
154
 
140
- export function assertValidIdentifier(name: string, context: string): void {
155
+ export function assertValidIdentifier(name: string, context: string): Effect.Effect<void, SurrealDBError> {
141
156
  if (!/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/.test(name)) {
142
- throw new SurrealDBError({ message: `Invalid ${context}: "${name}"` })
157
+ return Effect.fail(new SurrealDBError({ message: `Invalid ${context}: "${name}"` }))
143
158
  }
159
+ return Effect.void
144
160
  }
145
161
 
146
162
  export function buildFilterExpression(filter: Record<string, unknown>): ExprLike | undefined {
@@ -157,54 +173,68 @@ export function buildFilterExpression(filter: Record<string, unknown>): ExprLike
157
173
  return and(...expressions)
158
174
  }
159
175
 
160
- export function buildBoundFilterClauses(filter: Record<string, unknown>): {
161
- clause: string
162
- bindings: Record<string, unknown>
163
- } {
176
+ export function buildBoundFilterClauses(
177
+ filter: Record<string, unknown>,
178
+ ): Effect.Effect<{ clause: string; bindings: Record<string, unknown> }, SurrealDBError> {
164
179
  const entries = Object.entries(filter)
165
180
  if (entries.length === 0) {
166
- throw new SurrealDBError({ message: 'Expected a non-empty filter' })
181
+ return Effect.fail(new SurrealDBError({ message: 'Expected a non-empty filter' }))
167
182
  }
168
183
 
169
- const bindings: Record<string, unknown> = {}
170
- const clauses = entries.map(([field, value], index) => {
171
- assertValidIdentifier(field, 'filter field')
184
+ return Effect.gen(function* () {
185
+ const bindings: Record<string, unknown> = {}
186
+ const clauses: string[] = []
187
+ for (let index = 0; index < entries.length; index++) {
188
+ const [field, value] = entries[index]
189
+ yield* assertValidIdentifier(field, 'filter field')
190
+
191
+ const bindingKey = `filter_${index}`
192
+ bindings[bindingKey] = yield* Effect.try({
193
+ try: () => normalizeSurrealValue(value),
194
+ catch: (error) => toSurrealDBError(error, `Invalid filter value for field "${field}"`),
195
+ })
196
+ clauses.push(`${field} = $${bindingKey}`)
197
+ }
172
198
 
173
- const bindingKey = `filter_${index}`
174
- bindings[bindingKey] = normalizeSurrealValue(value)
175
- return `${field} = $${bindingKey}`
199
+ return { clause: clauses.join(' AND '), bindings }
176
200
  })
177
-
178
- return { clause: clauses.join(' AND '), bindings }
179
201
  }
180
202
 
181
- export function normalizeBoundQuery<T extends unknown[] = unknown[]>(query: BoundQuery<T>): BoundQuery<T> {
182
- return new BoundQuery<T>(query.query, normalizeBindings(query.bindings))
203
+ export function normalizeBoundQuery<T extends unknown[] = unknown[]>(
204
+ query: BoundQuery<T>,
205
+ ): Effect.Effect<BoundQuery<T>, SurrealDBError> {
206
+ return Effect.map(normalizeBindings(query.bindings), (bindings) => new BoundQuery<T>(query.query, bindings))
183
207
  }
184
208
 
185
- export function normalizeTransactionQuery(query: unknown): BoundQuery {
209
+ export function normalizeTransactionQuery(query: unknown): Effect.Effect<BoundQuery, SurrealDBError> {
186
210
  if (query instanceof BoundQuery) {
187
- return new BoundQuery(query.query, normalizeBindings(query.bindings))
211
+ return Effect.map(normalizeBindings(query.bindings), (bindings) => new BoundQuery(query.query, bindings))
188
212
  }
189
213
 
190
214
  if (isTransactionQueryDescriptor(query)) {
191
- return new BoundQuery(query.query, normalizeBindings(query.bindings))
215
+ return Effect.map(normalizeBindings(query.bindings), (bindings) => new BoundQuery(query.query, bindings))
192
216
  }
193
217
 
194
- throw new SurrealDBError({ message: 'Invalid transaction query' })
218
+ return Effect.fail(new SurrealDBError({ message: 'Invalid transaction query' }))
195
219
  }
196
220
 
197
- function normalizeBindings(bindings?: Record<string, unknown>): Record<string, unknown> | undefined {
221
+ function normalizeBindings(
222
+ bindings?: Record<string, unknown>,
223
+ ): Effect.Effect<Record<string, unknown> | undefined, SurrealDBError> {
198
224
  if (!bindings) {
199
- return undefined
200
- }
201
-
202
- const normalized = normalizeSurrealValue(bindings)
203
- if (!isRecord(normalized)) {
204
- throw new SurrealDBError({ message: 'Invalid bindings value' })
225
+ return Effect.void as Effect.Effect<undefined, SurrealDBError>
205
226
  }
206
227
 
207
- return normalized
228
+ return Effect.try({
229
+ try: () => {
230
+ const normalized = normalizeSurrealValue(bindings)
231
+ if (!isRecord(normalized)) {
232
+ throw new SurrealDBError({ message: 'Invalid bindings value' })
233
+ }
234
+ return normalized
235
+ },
236
+ catch: (error) => toSurrealDBError(error, 'Invalid bindings value'),
237
+ })
208
238
  }
209
239
 
210
240
  export function normalizeMutationData(data: Record<string, unknown>): Record<string, unknown> {
@@ -215,24 +245,27 @@ export function normalizeMutationData(data: Record<string, unknown>): Record<str
215
245
  )
216
246
  }
217
247
 
218
- export function normalizeTableValue(value: unknown): Table<string> {
248
+ export function normalizeTableValue(value: unknown): Effect.Effect<Table<string>, SurrealDBError> {
219
249
  if (value instanceof Table) {
220
- return new Table<string>(value.name as string)
250
+ return Effect.succeed(new Table<string>(value.name as string))
221
251
  }
222
252
 
223
253
  if (typeof value === 'string' && value.length > 0) {
224
- return new Table(value)
254
+ return Effect.succeed(new Table(value))
225
255
  }
226
256
 
227
- throw new SurrealDBError({ message: 'Invalid table value' })
257
+ return Effect.fail(new SurrealDBError({ message: 'Invalid table value' }))
228
258
  }
229
259
 
230
- export function normalizeCreateTarget(value: unknown): Table<string> | RecordId {
260
+ export function normalizeCreateTarget(value: unknown): Effect.Effect<Table<string> | RecordId, SurrealDBError> {
231
261
  if (
232
262
  isExplicitRecordIdValue(value) ||
233
263
  (typeof value === 'string' && isRecordIdInput(value) && /^[A-Za-z_][A-Za-z0-9_]*:/.test(value))
234
264
  ) {
235
- return ensureRecordId(value)
265
+ return Effect.try({
266
+ try: () => ensureRecordId(value),
267
+ catch: (error) => toSurrealDBError(error, 'Invalid record id'),
268
+ })
236
269
  }
237
270
 
238
271
  return normalizeTableValue(value)