@lota-sdk/core 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/infrastructure/schema/00_identity.surql +0 -2
- package/infrastructure/schema/01_memory.surql +1 -1
- package/infrastructure/schema/02_execution_plan.surql +62 -1
- package/infrastructure/schema/03_learned_skill.surql +1 -1
- package/infrastructure/schema/06_playbook.surql +25 -0
- package/infrastructure/schema/07_institutional_memory.surql +13 -0
- package/infrastructure/schema/08_quality_metrics.surql +17 -0
- package/package.json +9 -8
- package/src/ai/definitions.ts +80 -2
- package/src/ai/embedding-cache.ts +7 -6
- package/src/ai/index.ts +0 -1
- package/src/bifrost/bifrost.ts +14 -14
- package/src/config/agent-defaults.ts +32 -22
- package/src/config/agent-types.ts +11 -0
- package/src/config/constants.ts +2 -14
- package/src/config/debug-logger.ts +5 -1
- package/src/config/index.ts +3 -0
- package/src/config/logger.ts +7 -9
- package/src/config/model-constants.ts +16 -34
- package/src/config/search.ts +1 -15
- package/src/create-runtime.ts +453 -0
- package/src/db/cursor-pagination.ts +3 -6
- package/src/db/index.ts +2 -0
- package/src/db/memory-store.rows.ts +7 -7
- package/src/db/memory-store.ts +24 -24
- package/src/db/memory.ts +18 -16
- package/src/db/schema-fingerprint.ts +1 -0
- package/src/db/service.ts +193 -122
- package/src/db/startup.ts +9 -13
- package/src/db/surreal-mutation.ts +43 -0
- package/src/db/tables.ts +7 -0
- package/src/db/workstream-message-row.ts +15 -0
- package/src/embeddings/provider.ts +1 -1
- package/src/index.ts +1 -1
- package/src/queues/context-compaction.queue.ts +17 -52
- package/src/queues/delayed-node-promotion.queue.ts +41 -0
- package/src/queues/document-processor.queue.ts +7 -7
- package/src/queues/index.ts +3 -0
- package/src/queues/memory-consolidation.queue.ts +18 -54
- package/src/queues/plan-scheduler.queue.ts +97 -0
- package/src/queues/post-chat-memory.queue.ts +15 -60
- package/src/queues/queue-factory.ts +100 -0
- package/src/queues/recent-activity-title-refinement.queue.ts +15 -54
- package/src/queues/regular-chat-memory-digest.queue.ts +16 -55
- package/src/queues/skill-extraction.queue.ts +15 -50
- package/src/queues/workstream-title-generation.queue.ts +15 -51
- package/src/redis/connection.ts +12 -3
- package/src/redis/index.ts +2 -1
- package/src/redis/org-memory-lock.ts +1 -1
- package/src/redis/redis-lease-lock.ts +41 -8
- package/src/redis/stream-context.ts +11 -0
- package/src/runtime/agent-runtime-policy.ts +106 -21
- package/src/runtime/agent-stream-helpers.ts +2 -1
- package/src/runtime/approval-continuation.ts +12 -6
- package/src/runtime/context-compaction-constants.ts +1 -1
- package/src/runtime/context-compaction-runtime.ts +7 -5
- package/src/runtime/context-compaction.ts +40 -97
- package/src/runtime/execution-plan.ts +23 -19
- package/src/runtime/graph-designer.ts +15 -0
- package/src/runtime/helper-model.ts +10 -196
- package/src/runtime/index.ts +14 -1
- package/src/runtime/llm-content.ts +1 -1
- package/src/runtime/memory-block.ts +11 -12
- package/src/runtime/memory-pipeline.ts +26 -10
- package/src/runtime/plugin-resolution.ts +35 -0
- package/src/runtime/plugin-types.ts +73 -1
- package/src/runtime/retrieval-adapters.ts +1 -1
- package/src/runtime/runtime-config.ts +25 -12
- package/src/runtime/runtime-extensions.ts +91 -15
- package/src/runtime/runtime-worker-registry.ts +6 -0
- package/src/runtime/team-consultation-orchestrator.ts +45 -28
- package/src/runtime/team-consultation-prompts.ts +11 -2
- package/src/runtime/title-helpers.ts +11 -4
- package/src/runtime/workstream-chat-helpers.ts +6 -7
- package/src/runtime/workstream-routing-policy.ts +0 -30
- package/src/runtime/workstream-state.ts +17 -7
- package/src/services/adaptive-playbook.service.ts +152 -0
- package/src/services/agent-executor.service.ts +293 -0
- package/src/services/artifact-provenance.service.ts +172 -0
- package/src/services/attachment.service.ts +7 -12
- package/src/services/context-compaction.service.ts +75 -58
- package/src/services/context-enrichment.service.ts +33 -0
- package/src/services/coordination-registry.service.ts +117 -0
- package/src/services/document-chunk.service.ts +38 -33
- package/src/services/domain-agent-executor.service.ts +71 -0
- package/src/services/execution-plan.service.ts +271 -50
- package/src/services/feedback-loop.service.ts +96 -0
- package/src/services/global-orchestrator.service.ts +148 -0
- package/src/services/index.ts +26 -0
- package/src/services/institutional-memory.service.ts +145 -0
- package/src/services/learned-skill.service.ts +30 -15
- package/src/services/memory-assessment.service.ts +3 -2
- package/src/services/{memory.utils.ts → memory-utils.ts} +4 -13
- package/src/services/memory.service.ts +55 -69
- package/src/services/monitoring-window.service.ts +86 -0
- package/src/services/mutating-approval.service.ts +1 -1
- package/src/services/node-workspace.service.ts +155 -0
- package/src/services/notification.service.ts +39 -0
- package/src/services/organization-member.service.ts +12 -5
- package/src/services/organization.service.ts +5 -5
- package/src/services/ownership-dispatcher.service.ts +403 -0
- package/src/services/plan-approval.service.ts +1 -1
- package/src/services/plan-artifact.service.ts +1 -0
- package/src/services/plan-builder.service.ts +1 -0
- package/src/services/plan-checkpoint.service.ts +30 -2
- package/src/services/plan-compiler.service.ts +5 -0
- package/src/services/plan-coordination.service.ts +152 -0
- package/src/services/plan-cycle.service.ts +284 -0
- package/src/services/plan-deadline.service.ts +287 -0
- package/src/services/plan-executor.service.ts +386 -58
- package/src/services/plan-helpers.ts +15 -0
- package/src/services/plan-run.service.ts +41 -7
- package/src/services/plan-scheduler.service.ts +240 -0
- package/src/services/plan-template.service.ts +117 -0
- package/src/services/plan-validator.service.ts +87 -20
- package/src/services/plan-workspace.service.ts +83 -0
- package/src/services/playbook-registry.service.ts +67 -0
- package/src/services/plugin-executor.service.ts +103 -0
- package/src/services/quality-metrics.service.ts +132 -0
- package/src/services/recent-activity-title.service.ts +3 -10
- package/src/services/recent-activity.service.ts +33 -43
- package/src/services/skill-resolver.service.ts +19 -0
- package/src/services/system-executor.service.ts +105 -0
- package/src/services/workstream-message.service.ts +29 -41
- package/src/services/workstream-plan-registry.service.ts +22 -0
- package/src/services/workstream-title.service.ts +3 -9
- package/src/services/{workstream-turn-preparation.ts → workstream-turn-preparation.service.ts} +428 -373
- package/src/services/workstream-turn.ts +2 -2
- package/src/services/workstream.service.ts +55 -65
- package/src/services/workstream.types.ts +10 -19
- package/src/services/write-intent-validator.service.ts +81 -0
- package/src/storage/attachment-parser.ts +1 -1
- package/src/storage/attachment-storage.service.ts +4 -4
- package/src/storage/{attachments.utils.ts → attachment-utils.ts} +2 -5
- package/src/storage/generated-document-storage.service.ts +3 -2
- package/src/storage/index.ts +2 -2
- package/src/system-agents/{context-compacter.agent.ts → context-compaction.agent.ts} +4 -4
- package/src/system-agents/delegated-agent-factory.ts +5 -2
- package/src/system-agents/index.ts +8 -0
- package/src/system-agents/memory-reranker.agent.ts +1 -1
- package/src/system-agents/memory.agent.ts +1 -1
- package/src/system-agents/recent-activity-title-refiner.agent.ts +1 -1
- package/src/tools/execution-plan.tool.ts +17 -19
- package/src/tools/fetch-webpage.tool.ts +20 -18
- package/src/tools/index.ts +2 -3
- package/src/tools/read-file-parts.tool.ts +1 -1
- package/src/tools/search-web.tool.ts +18 -15
- package/src/tools/{search-tools.ts → search.tool.ts} +1 -1
- package/src/tools/team-think.tool.ts +14 -8
- package/src/tools/{tool-contract.ts → tool-contracts.ts} +9 -2
- package/src/utils/async.ts +3 -2
- package/src/utils/date-time.ts +4 -32
- package/src/utils/env.ts +8 -0
- package/src/utils/errors.ts +47 -0
- package/src/utils/hono-error-handler.ts +1 -2
- package/src/utils/index.ts +19 -2
- package/src/utils/string.ts +128 -1
- package/src/workers/bootstrap.ts +2 -2
- package/src/workers/index.ts +1 -0
- package/src/workers/memory-consolidation.worker.ts +12 -12
- package/src/workers/regular-chat-memory-digest.helpers.ts +2 -7
- package/src/workers/regular-chat-memory-digest.runner.ts +11 -105
- package/src/workers/skill-extraction.runner.ts +8 -102
- package/src/workers/utils/file-section-chunker.ts +6 -3
- package/src/workers/utils/repomix-file-sections.ts +2 -2
- package/src/workers/utils/sandbox-error.ts +11 -2
- package/src/workers/utils/workstream-message-query.ts +97 -0
- package/src/workers/worker-utils.ts +6 -2
- package/src/runtime/retrieval-pipeline.ts +0 -3
- package/src/runtime.ts +0 -387
- package/src/tools/log-hello-world.tool.ts +0 -17
- package/src/utils/error.ts +0 -10
- /package/src/services/{context-compaction-runtime.ts → context-compaction-runtime.singleton.ts} +0 -0
- /package/src/storage/{attachments.types.ts → attachment-types.ts} +0 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import { parseRowMetadata, recordIdSchema, requireTimestamp, withCreatedAtMetadata } from '@lota-sdk/shared'
|
|
2
4
|
import type { ChatMessage } from '@lota-sdk/shared'
|
|
3
5
|
import { RecordId, surql } from 'surrealdb'
|
|
4
6
|
import { z } from 'zod'
|
|
@@ -10,40 +12,33 @@ import { recordIdToString } from '../db/record-id'
|
|
|
10
12
|
import type { RecordIdRef } from '../db/record-id'
|
|
11
13
|
import { databaseService } from '../db/service'
|
|
12
14
|
import { TABLES } from '../db/tables'
|
|
15
|
+
import { WorkstreamMessageRowSchema } from '../db/workstream-message-row'
|
|
16
|
+
import type { WorkstreamMessageRow } from '../db/workstream-message-row'
|
|
13
17
|
|
|
14
|
-
const
|
|
15
|
-
id: z.unknown(),
|
|
16
|
-
workstreamId: z.unknown(),
|
|
17
|
-
messageId: z.string(),
|
|
18
|
-
role: z.enum(['system', 'user', 'assistant']),
|
|
19
|
-
parts: z.array(z.record(z.string(), z.unknown())).optional(),
|
|
20
|
-
metadata: z.record(z.string(), z.unknown()).nullish(),
|
|
21
|
-
createdAt: z.union([z.date(), z.string(), z.number()]),
|
|
22
|
-
updatedAt: z.union([z.date(), z.string(), z.number()]).optional(),
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
type WorkstreamMessageRow = z.infer<typeof WorkstreamMessageRowSchema>
|
|
26
|
-
|
|
27
|
-
const WorkstreamMessageExistingRowSchema = z.object({
|
|
28
|
-
id: z.unknown(),
|
|
29
|
-
createdAt: z.union([z.date(), z.string(), z.number()]),
|
|
30
|
-
})
|
|
18
|
+
const WorkstreamMessageExistingRowSchema = z.object({ id: recordIdSchema, createdAt: z.coerce.date() })
|
|
31
19
|
|
|
32
20
|
function toMessageId(value: string | RecordIdRef): string {
|
|
33
21
|
return recordIdToString(value, TABLES.WORKSTREAM_MESSAGE)
|
|
34
22
|
}
|
|
35
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Builds a collision-free row id by hashing the workstream + message id pair.
|
|
26
|
+
* Previous implementation replaced non-alphanumeric chars with '_', which was
|
|
27
|
+
* lossy (e.g. "msg:foo" and "msg_foo" mapped to the same row id).
|
|
28
|
+
* Now uses a 32-char SHA-256 hex prefix -- short enough for ergonomic ids,
|
|
29
|
+
* long enough (128 bits) to make collisions negligible.
|
|
30
|
+
*/
|
|
36
31
|
function toWorkstreamMessageRowId(workstreamId: RecordIdRef, messageId: string): RecordId {
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
return new RecordId(TABLES.WORKSTREAM_MESSAGE,
|
|
32
|
+
const workstreamStr = recordIdToString(workstreamId, TABLES.WORKSTREAM)
|
|
33
|
+
const digest = createHash('sha256').update(`${workstreamStr}\0${messageId}`).digest('hex').slice(0, 32)
|
|
34
|
+
return new RecordId(TABLES.WORKSTREAM_MESSAGE, digest)
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
function toChatMessage(row: WorkstreamMessageRow): ChatMessage {
|
|
43
|
-
const rowCreatedAt =
|
|
38
|
+
const rowCreatedAt = requireTimestamp(row.createdAt)
|
|
44
39
|
const metadata = withCreatedAtMetadata(parseRowMetadata(row.metadata), rowCreatedAt)
|
|
45
40
|
|
|
46
|
-
return { id: row.messageId, role: row.role, parts:
|
|
41
|
+
return { id: row.messageId, role: row.role, parts: row.parts as ChatMessage['parts'], metadata }
|
|
47
42
|
}
|
|
48
43
|
|
|
49
44
|
const workstreamPaginationConfig: CursorPaginationConfig = {
|
|
@@ -74,16 +69,16 @@ class WorkstreamMessageService {
|
|
|
74
69
|
async upsertMessages(params: { workstreamId: RecordIdRef; messages: ChatMessage[] }): Promise<void> {
|
|
75
70
|
const workstreamId = params.workstreamId
|
|
76
71
|
|
|
77
|
-
|
|
72
|
+
const upsertPromises = params.messages.map(async (message) => {
|
|
78
73
|
const messageId = message.id.trim()
|
|
79
|
-
if (!messageId)
|
|
74
|
+
if (!messageId) return
|
|
80
75
|
|
|
81
76
|
const role = message.role
|
|
82
77
|
const parts = Array.isArray(message.parts)
|
|
83
78
|
? message.parts.map((part) => structuredClone(part) as Record<string, unknown>)
|
|
84
79
|
: []
|
|
85
80
|
if (parts.length === 0) {
|
|
86
|
-
if (role === 'assistant')
|
|
81
|
+
if (role === 'assistant') return
|
|
87
82
|
throw new Error(`Refusing to persist workstream message "${messageId}" with empty parts`)
|
|
88
83
|
}
|
|
89
84
|
const rowId = toWorkstreamMessageRowId(workstreamId, messageId)
|
|
@@ -93,7 +88,7 @@ class WorkstreamMessageService {
|
|
|
93
88
|
WorkstreamMessageExistingRowSchema,
|
|
94
89
|
)
|
|
95
90
|
const persistedCreatedAt =
|
|
96
|
-
existingRow === null ?
|
|
91
|
+
existingRow === null ? requireTimestamp(message.metadata?.createdAt) : requireTimestamp(existingRow.createdAt)
|
|
97
92
|
const metadata = withCreatedAtMetadata({ ...message.metadata, createdAt: persistedCreatedAt })
|
|
98
93
|
|
|
99
94
|
await databaseService.upsert(
|
|
@@ -105,12 +100,13 @@ class WorkstreamMessageService {
|
|
|
105
100
|
role,
|
|
106
101
|
parts,
|
|
107
102
|
metadata,
|
|
108
|
-
createdAt: existingRow ?
|
|
103
|
+
createdAt: existingRow ? existingRow.createdAt : new Date(persistedCreatedAt),
|
|
109
104
|
},
|
|
110
105
|
WorkstreamMessageRowSchema,
|
|
111
106
|
{ mutation: 'content' },
|
|
112
107
|
)
|
|
113
|
-
}
|
|
108
|
+
})
|
|
109
|
+
await Promise.all(upsertPromises)
|
|
114
110
|
}
|
|
115
111
|
|
|
116
112
|
async listMessages(workstreamId: RecordIdRef): Promise<ChatMessage[]> {
|
|
@@ -138,7 +134,7 @@ class WorkstreamMessageService {
|
|
|
138
134
|
async listMessagesAfterCursor(workstreamId: RecordIdRef, afterMessageId?: string): Promise<ChatMessage[]> {
|
|
139
135
|
const cursorMessageId = afterMessageId?.trim()
|
|
140
136
|
if (!cursorMessageId) {
|
|
141
|
-
return
|
|
137
|
+
return this.listMessages(workstreamId)
|
|
142
138
|
}
|
|
143
139
|
|
|
144
140
|
const cursorRow = await databaseService.findOne(
|
|
@@ -151,7 +147,7 @@ class WorkstreamMessageService {
|
|
|
151
147
|
throw new Error(`Workstream cursor message not found: ${cursorMessageId}`)
|
|
152
148
|
}
|
|
153
149
|
|
|
154
|
-
const cursorCreatedAt =
|
|
150
|
+
const cursorCreatedAt = cursorRow.createdAt
|
|
155
151
|
const cursorId = toWorkstreamMessageRowId(workstreamId, cursorMessageId)
|
|
156
152
|
const rows = await databaseService.query<unknown>(surql`
|
|
157
153
|
SELECT * FROM workstreamMessage
|
|
@@ -195,7 +191,7 @@ class WorkstreamMessageService {
|
|
|
195
191
|
.map((message) => ({
|
|
196
192
|
id: message.id,
|
|
197
193
|
role: message.role as 'user' | 'assistant',
|
|
198
|
-
createdAt: new Date(
|
|
194
|
+
createdAt: new Date(requireTimestamp(message.metadata?.createdAt)).toISOString(),
|
|
199
195
|
content: message.parts
|
|
200
196
|
.flatMap((part) => (part.type === 'text' && typeof part.text === 'string' ? [part.text] : []))
|
|
201
197
|
.join('\n')
|
|
@@ -231,7 +227,7 @@ class WorkstreamMessageService {
|
|
|
231
227
|
id: toMessageId(params.messageId),
|
|
232
228
|
role: 'assistant',
|
|
233
229
|
parts: params.parts,
|
|
234
|
-
metadata: withCreatedAtMetadata(params.metadata),
|
|
230
|
+
metadata: withCreatedAtMetadata(params.metadata, Date.now()),
|
|
235
231
|
}
|
|
236
232
|
|
|
237
233
|
await this.upsertMessages({ workstreamId: params.workstreamId, messages: [message] })
|
|
@@ -269,14 +265,6 @@ class WorkstreamMessageService {
|
|
|
269
265
|
],
|
|
270
266
|
})
|
|
271
267
|
}
|
|
272
|
-
|
|
273
|
-
async listAllMessages(workstreamId: RecordIdRef): Promise<ChatMessage[]> {
|
|
274
|
-
return await this.listMessages(workstreamId)
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async addAttachments(): Promise<void> {
|
|
278
|
-
// Attachments are no longer persisted via workstreamMessage service in AI SDK mode.
|
|
279
|
-
}
|
|
280
268
|
}
|
|
281
269
|
|
|
282
270
|
export const workstreamMessageService = new WorkstreamMessageService()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PlanRunRecord, SerializableExecutionPlan } from '@lota-sdk/shared'
|
|
2
|
+
|
|
3
|
+
import type { RecordIdInput } from '../db/record-id'
|
|
4
|
+
import { planRunService } from './plan-run.service'
|
|
5
|
+
|
|
6
|
+
class WorkstreamPlanRegistryService {
|
|
7
|
+
async listActiveRuns(workstreamId: RecordIdInput): Promise<PlanRunRecord[]> {
|
|
8
|
+
return planRunService.getActiveRunRecords(workstreamId)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async countActiveRuns(workstreamId: RecordIdInput): Promise<number> {
|
|
12
|
+
const runs = await this.listActiveRuns(workstreamId)
|
|
13
|
+
return runs.length
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async listActivePlans(workstreamId: RecordIdInput): Promise<SerializableExecutionPlan[]> {
|
|
17
|
+
const runs = await this.listActiveRuns(workstreamId)
|
|
18
|
+
return Promise.all(runs.map((run) => planRunService.toSerializablePlan(run)))
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const workstreamPlanRegistryService = new WorkstreamPlanRegistryService()
|
|
@@ -3,20 +3,14 @@ import { WORKSTREAM } from '@lota-sdk/shared'
|
|
|
3
3
|
import { chatLogger } from '../config/logger'
|
|
4
4
|
import type { RecordIdRef } from '../db/record-id'
|
|
5
5
|
import { createHelperModelRuntime } from '../runtime/helper-model'
|
|
6
|
-
import { deriveTitle, limitTitleWords } from '../runtime/title-helpers'
|
|
6
|
+
import { deriveTitle, limitTitleWords, normalizeTitle } from '../runtime/title-helpers'
|
|
7
7
|
import {
|
|
8
8
|
createWorkstreamTitleGeneratorAgent,
|
|
9
9
|
WORKSTREAM_TITLE_GENERATOR_PROMPT,
|
|
10
10
|
} from '../system-agents/title-generator.agent'
|
|
11
|
-
import { compactWhitespace } from '../utils/string'
|
|
12
11
|
import { workstreamService } from './workstream.service'
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
const normalized = compactWhitespace(value)
|
|
16
|
-
.replace(/^["'`]+|["'`]+$/g, '')
|
|
17
|
-
.replace(/[.!?,;:]+$/g, '')
|
|
18
|
-
return normalized.length <= 80 ? normalized : normalized.slice(0, 80).trim()
|
|
19
|
-
}
|
|
13
|
+
const WORKSTREAM_TITLE_TIMEOUT_MS = 5_000
|
|
20
14
|
|
|
21
15
|
class WorkstreamTitleService {
|
|
22
16
|
helperRuntime = createHelperModelRuntime()
|
|
@@ -29,7 +23,7 @@ class WorkstreamTitleService {
|
|
|
29
23
|
tag: 'workstream-title',
|
|
30
24
|
createAgent: createWorkstreamTitleGeneratorAgent,
|
|
31
25
|
defaultSystemPrompt: WORKSTREAM_TITLE_GENERATOR_PROMPT,
|
|
32
|
-
timeoutMs:
|
|
26
|
+
timeoutMs: WORKSTREAM_TITLE_TIMEOUT_MS,
|
|
33
27
|
messages: [{ role: 'user', content: sourceText }],
|
|
34
28
|
}),
|
|
35
29
|
)
|