@vellumai/assistant 0.5.2 → 0.5.3
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/ARCHITECTURE.md +109 -0
- package/docs/skills.md +100 -0
- package/package.json +1 -1
- package/src/__tests__/conversation-agent-loop-overflow.test.ts +7 -0
- package/src/__tests__/conversation-agent-loop.test.ts +7 -0
- package/src/__tests__/conversation-memory-dirty-tail.test.ts +150 -0
- package/src/__tests__/conversation-provider-retry-repair.test.ts +7 -0
- package/src/__tests__/conversation-wipe.test.ts +226 -0
- package/src/__tests__/db-memory-archive-migration.test.ts +372 -0
- package/src/__tests__/db-memory-brief-state-migration.test.ts +213 -0
- package/src/__tests__/db-memory-reducer-checkpoints.test.ts +273 -0
- package/src/__tests__/inline-command-runner.test.ts +311 -0
- package/src/__tests__/inline-skill-authoring-guard.test.ts +220 -0
- package/src/__tests__/inline-skill-load-permissions.test.ts +435 -0
- package/src/__tests__/list-messages-attachments.test.ts +96 -0
- package/src/__tests__/memory-brief-open-loops.test.ts +530 -0
- package/src/__tests__/memory-brief-time.test.ts +285 -0
- package/src/__tests__/memory-brief-wrapper.test.ts +311 -0
- package/src/__tests__/memory-chunk-archive.test.ts +400 -0
- package/src/__tests__/memory-chunk-dual-write.test.ts +453 -0
- package/src/__tests__/memory-episode-archive.test.ts +370 -0
- package/src/__tests__/memory-episode-dual-write.test.ts +626 -0
- package/src/__tests__/memory-observation-archive.test.ts +375 -0
- package/src/__tests__/memory-observation-dual-write.test.ts +318 -0
- package/src/__tests__/memory-recall-quality.test.ts +2 -2
- package/src/__tests__/memory-reducer-store.test.ts +728 -0
- package/src/__tests__/memory-reducer-types.test.ts +699 -0
- package/src/__tests__/memory-reducer.test.ts +698 -0
- package/src/__tests__/memory-regressions.test.ts +6 -4
- package/src/__tests__/memory-simplified-config.test.ts +281 -0
- package/src/__tests__/parse-identity-fields.test.ts +129 -0
- package/src/__tests__/skill-load-inline-command.test.ts +598 -0
- package/src/__tests__/skill-load-inline-includes.test.ts +644 -0
- package/src/__tests__/skills-inline-command-expansions.test.ts +301 -0
- package/src/__tests__/skills-transitive-hash.test.ts +333 -0
- package/src/__tests__/vellum-self-knowledge-inline-command.test.ts +320 -0
- package/src/__tests__/workspace-migration-backfill-installation-id.test.ts +4 -4
- package/src/config/bundled-skills/app-builder/SKILL.md +8 -8
- package/src/config/bundled-skills/skill-management/SKILL.md +1 -1
- package/src/config/bundled-skills/skill-management/TOOLS.json +2 -2
- package/src/config/feature-flag-registry.json +16 -0
- package/src/config/loader.ts +1 -0
- package/src/config/raw-config-utils.ts +28 -0
- package/src/config/schema.ts +12 -0
- package/src/config/schemas/memory-simplified.ts +101 -0
- package/src/config/schemas/memory.ts +4 -0
- package/src/config/skills.ts +50 -4
- package/src/daemon/conversation-agent-loop-handlers.ts +8 -3
- package/src/daemon/conversation-agent-loop.ts +71 -1
- package/src/daemon/conversation-lifecycle.ts +11 -1
- package/src/daemon/conversation-runtime-assembly.ts +2 -1
- package/src/daemon/conversation-surfaces.ts +31 -8
- package/src/daemon/conversation.ts +40 -23
- package/src/daemon/handlers/config-embeddings.ts +10 -2
- package/src/daemon/handlers/config-model.ts +0 -9
- package/src/daemon/handlers/identity.ts +12 -1
- package/src/daemon/lifecycle.ts +9 -1
- package/src/daemon/message-types/conversations.ts +0 -1
- package/src/daemon/server.ts +1 -1
- package/src/followups/followup-store.ts +47 -1
- package/src/memory/archive-store.ts +400 -0
- package/src/memory/brief-formatting.ts +33 -0
- package/src/memory/brief-open-loops.ts +266 -0
- package/src/memory/brief-time.ts +161 -0
- package/src/memory/brief.ts +75 -0
- package/src/memory/conversation-crud.ts +245 -101
- package/src/memory/db-init.ts +12 -0
- package/src/memory/indexer.ts +106 -15
- package/src/memory/job-handlers/embedding.test.ts +1 -0
- package/src/memory/job-handlers/embedding.ts +83 -0
- package/src/memory/job-utils.ts +1 -1
- package/src/memory/jobs-store.ts +6 -0
- package/src/memory/jobs-worker.ts +12 -0
- package/src/memory/migrations/185-memory-brief-state.ts +52 -0
- package/src/memory/migrations/186-memory-archive.ts +109 -0
- package/src/memory/migrations/187-memory-reducer-checkpoints.ts +19 -0
- package/src/memory/migrations/index.ts +3 -0
- package/src/memory/qdrant-client.ts +23 -4
- package/src/memory/reducer-store.ts +271 -0
- package/src/memory/reducer-types.ts +99 -0
- package/src/memory/reducer.ts +453 -0
- package/src/memory/schema/conversations.ts +3 -0
- package/src/memory/schema/index.ts +2 -0
- package/src/memory/schema/memory-archive.ts +121 -0
- package/src/memory/schema/memory-brief.ts +55 -0
- package/src/memory/search/semantic.ts +17 -4
- package/src/oauth/oauth-store.ts +3 -1
- package/src/permissions/checker.ts +89 -6
- package/src/permissions/defaults.ts +14 -0
- package/src/runtime/routes/conversation-management-routes.ts +6 -0
- package/src/runtime/routes/conversation-query-routes.ts +7 -0
- package/src/runtime/routes/conversation-routes.ts +52 -5
- package/src/runtime/routes/identity-routes.ts +2 -35
- package/src/runtime/routes/llm-context-normalization.ts +14 -1
- package/src/runtime/routes/memory-item-routes.ts +90 -5
- package/src/runtime/routes/secret-routes.ts +2 -0
- package/src/runtime/routes/surface-action-routes.ts +68 -1
- package/src/schedule/schedule-store.ts +21 -0
- package/src/skills/inline-command-expansions.ts +204 -0
- package/src/skills/inline-command-render.ts +127 -0
- package/src/skills/inline-command-runner.ts +242 -0
- package/src/skills/transitive-version-hash.ts +88 -0
- package/src/tasks/task-store.ts +43 -1
- package/src/tools/permission-checker.ts +8 -1
- package/src/tools/skills/load.ts +140 -6
- package/src/util/platform.ts +18 -0
- package/src/workspace/migrations/{002-backfill-installation-id.ts → 011-backfill-installation-id.ts} +1 -1
- package/src/workspace/migrations/registry.ts +1 -1
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplified memory reducer — provider-backed conversation turn processor.
|
|
3
|
+
*
|
|
4
|
+
* This module owns:
|
|
5
|
+
* 1. ReducerPromptInput — structured input for the provider call
|
|
6
|
+
* 2. runReducer — send the transcript span to the LLM and return a typed result
|
|
7
|
+
* 3. parseReducerOutput — raw string -> validated ReducerResult
|
|
8
|
+
* 4. Fallback to EMPTY_REDUCER_RESULT on any invalid output
|
|
9
|
+
*
|
|
10
|
+
* The reducer is intentionally side-effect-free: it never writes to the
|
|
11
|
+
* database. Callers are responsible for applying the returned ReducerResult.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
createTimeout,
|
|
16
|
+
extractText,
|
|
17
|
+
getConfiguredProvider,
|
|
18
|
+
} from "../providers/provider-send-message.js";
|
|
19
|
+
import { getLogger } from "../util/logger.js";
|
|
20
|
+
import {
|
|
21
|
+
type ArchiveEpisodeCandidate,
|
|
22
|
+
type ArchiveObservationCandidate,
|
|
23
|
+
EMPTY_REDUCER_RESULT,
|
|
24
|
+
type OpenLoopCreate,
|
|
25
|
+
type OpenLoopOp,
|
|
26
|
+
type OpenLoopUpdate,
|
|
27
|
+
type ReducerResult,
|
|
28
|
+
type TimeContextOp,
|
|
29
|
+
type TimeContextUpdate,
|
|
30
|
+
} from "./reducer-types.js";
|
|
31
|
+
|
|
32
|
+
const log = getLogger("memory-reducer");
|
|
33
|
+
|
|
34
|
+
/** Timeout for the reducer provider call (ms). */
|
|
35
|
+
const REDUCER_TIMEOUT_MS = 30_000;
|
|
36
|
+
|
|
37
|
+
// ── Prompt input type ──────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/** The structured input that will be fed to the reducer provider call. */
|
|
40
|
+
export interface ReducerPromptInput {
|
|
41
|
+
/** Conversation ID being reduced. */
|
|
42
|
+
conversationId: string;
|
|
43
|
+
/** New messages since the last reduction checkpoint (role + content). */
|
|
44
|
+
newMessages: Array<{ role: string; content: string }>;
|
|
45
|
+
/** Current time-context rows the model can reference for updates. */
|
|
46
|
+
existingTimeContexts: Array<{ id: string; summary: string }>;
|
|
47
|
+
/** Current open-loop rows the model can reference for updates. */
|
|
48
|
+
existingOpenLoops: Array<{ id: string; summary: string; status: string }>;
|
|
49
|
+
/** Current time as epoch ms — injected for deterministic tests. */
|
|
50
|
+
nowMs: number;
|
|
51
|
+
/** Memory scope identifier (e.g. assistant instance ID). */
|
|
52
|
+
scopeId: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── System prompt ─────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Build the reducer system prompt. Extracted as a named function so tests can
|
|
59
|
+
* assert on prompt content without coupling to string literals.
|
|
60
|
+
*/
|
|
61
|
+
export function buildReducerSystemPrompt(): string {
|
|
62
|
+
return [
|
|
63
|
+
"You are a memory reducer for a personal assistant. Your job is to analyze",
|
|
64
|
+
"a span of new conversation messages and produce structured JSON output that",
|
|
65
|
+
"captures important information for the assistant's long-term memory.",
|
|
66
|
+
"",
|
|
67
|
+
"You output a single JSON object with four optional arrays:",
|
|
68
|
+
"",
|
|
69
|
+
"1. `timeContexts` — time-bounded situational context (e.g. 'user traveling next week').",
|
|
70
|
+
" Each entry has: action ('create'|'update'|'resolve'), and fields depending on the action.",
|
|
71
|
+
" - create: summary (string), source (string), activeFrom (epoch ms), activeUntil (epoch ms)",
|
|
72
|
+
" - update: id (string), and at least one of: summary, activeFrom, activeUntil",
|
|
73
|
+
" - resolve: id (string)",
|
|
74
|
+
"",
|
|
75
|
+
"2. `openLoops` — unresolved items to track (e.g. 'waiting for Bob's reply').",
|
|
76
|
+
" Each entry has: action ('create'|'update'|'resolve'), and fields depending on the action.",
|
|
77
|
+
" - create: summary (string), source (string), optional dueAt (epoch ms)",
|
|
78
|
+
" - update: id (string), and at least one of: summary, dueAt",
|
|
79
|
+
" - resolve: id (string), status ('resolved'|'expired')",
|
|
80
|
+
"",
|
|
81
|
+
"3. `archiveObservations` — factual statements extracted from the conversation.",
|
|
82
|
+
" Each entry has: content (string), role (string), optional modality (string), optional source (string)",
|
|
83
|
+
"",
|
|
84
|
+
"4. `archiveEpisodes` — coherent narrative summaries of interaction spans.",
|
|
85
|
+
" Each entry has: title (string), summary (string), optional source (string)",
|
|
86
|
+
"",
|
|
87
|
+
"Rules:",
|
|
88
|
+
"- Output ONLY valid JSON. No markdown, no explanation, no wrapping.",
|
|
89
|
+
"- Omit arrays that would be empty rather than including empty arrays.",
|
|
90
|
+
"- For updates and resolves, reference existing IDs from the provided context.",
|
|
91
|
+
"- Be selective: only extract genuinely important or actionable information.",
|
|
92
|
+
"- Timestamps are in epoch milliseconds.",
|
|
93
|
+
"- If there is nothing meaningful to extract, output: {}",
|
|
94
|
+
].join("\n");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Build the user-message content for the reducer prompt from the structured input.
|
|
99
|
+
*/
|
|
100
|
+
export function buildReducerUserMessage(input: ReducerPromptInput): string {
|
|
101
|
+
const parts: string[] = [];
|
|
102
|
+
|
|
103
|
+
parts.push(
|
|
104
|
+
`Current time: ${new Date(input.nowMs).toISOString()} (${input.nowMs}ms)`,
|
|
105
|
+
);
|
|
106
|
+
parts.push(`Conversation: ${input.conversationId}`);
|
|
107
|
+
parts.push(`Scope: ${input.scopeId}`);
|
|
108
|
+
parts.push("");
|
|
109
|
+
|
|
110
|
+
// Existing state the model can reference for updates/resolves
|
|
111
|
+
if (input.existingTimeContexts.length > 0) {
|
|
112
|
+
parts.push("## Active time contexts");
|
|
113
|
+
for (const tc of input.existingTimeContexts) {
|
|
114
|
+
parts.push(`- [${tc.id}] ${tc.summary}`);
|
|
115
|
+
}
|
|
116
|
+
parts.push("");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (input.existingOpenLoops.length > 0) {
|
|
120
|
+
parts.push("## Active open loops");
|
|
121
|
+
for (const ol of input.existingOpenLoops) {
|
|
122
|
+
parts.push(`- [${ol.id}] (${ol.status}) ${ol.summary}`);
|
|
123
|
+
}
|
|
124
|
+
parts.push("");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// The unreduced transcript span
|
|
128
|
+
parts.push("## New messages to process");
|
|
129
|
+
for (const msg of input.newMessages) {
|
|
130
|
+
parts.push(`[${msg.role}]: ${msg.content}`);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return parts.join("\n");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ── Provider-backed reducer call ──────────────────────────────────────
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Run the memory reducer against a transcript span.
|
|
140
|
+
*
|
|
141
|
+
* Sends the unreduced messages, active time contexts, active open loops,
|
|
142
|
+
* current time, and scope metadata to the configured LLM provider. Parses
|
|
143
|
+
* the response into a typed {@link ReducerResult}.
|
|
144
|
+
*
|
|
145
|
+
* This function is **side-effect-free**: it never writes to the database.
|
|
146
|
+
* The caller is responsible for applying the returned result.
|
|
147
|
+
*
|
|
148
|
+
* Returns {@link EMPTY_REDUCER_RESULT} when:
|
|
149
|
+
* - No provider is configured/available
|
|
150
|
+
* - The provider call fails or times out
|
|
151
|
+
* - The model output is unparseable
|
|
152
|
+
*
|
|
153
|
+
* @param input Structured reducer input
|
|
154
|
+
* @param signal Optional external abort signal
|
|
155
|
+
*/
|
|
156
|
+
export async function runReducer(
|
|
157
|
+
input: ReducerPromptInput,
|
|
158
|
+
signal?: AbortSignal,
|
|
159
|
+
): Promise<ReducerResult> {
|
|
160
|
+
const provider = await getConfiguredProvider();
|
|
161
|
+
if (!provider) {
|
|
162
|
+
log.warn(
|
|
163
|
+
"No provider available for memory reducer — returning empty result",
|
|
164
|
+
);
|
|
165
|
+
return EMPTY_REDUCER_RESULT;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const systemPrompt = buildReducerSystemPrompt();
|
|
169
|
+
const userText = buildReducerUserMessage(input);
|
|
170
|
+
|
|
171
|
+
const { signal: timeoutSignal, cleanup } = createTimeout(REDUCER_TIMEOUT_MS);
|
|
172
|
+
const combinedSignal = signal
|
|
173
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
174
|
+
: timeoutSignal;
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
const response = await provider.sendMessage(
|
|
178
|
+
[{ role: "user", content: [{ type: "text", text: userText }] }],
|
|
179
|
+
undefined,
|
|
180
|
+
systemPrompt,
|
|
181
|
+
{
|
|
182
|
+
signal: combinedSignal,
|
|
183
|
+
config: {
|
|
184
|
+
modelIntent: "latency-optimized" as const,
|
|
185
|
+
max_tokens: 4096,
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const rawText = extractText(response);
|
|
191
|
+
if (!rawText) {
|
|
192
|
+
log.warn("Reducer provider returned empty text — returning empty result");
|
|
193
|
+
return EMPTY_REDUCER_RESULT;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return parseReducerOutput(rawText);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (combinedSignal.aborted) {
|
|
199
|
+
log.warn("Memory reducer provider call timed out or was aborted");
|
|
200
|
+
} else {
|
|
201
|
+
log.warn({ err }, "Memory reducer provider call failed");
|
|
202
|
+
}
|
|
203
|
+
return EMPTY_REDUCER_RESULT;
|
|
204
|
+
} finally {
|
|
205
|
+
cleanup();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ── Validation helpers ─────────────────────────────────────────────────
|
|
210
|
+
|
|
211
|
+
const VALID_TIME_CONTEXT_ACTIONS = new Set(["create", "update", "resolve"]);
|
|
212
|
+
const VALID_OPEN_LOOP_ACTIONS = new Set(["create", "update", "resolve"]);
|
|
213
|
+
const VALID_OPEN_LOOP_RESOLVE_STATUSES = new Set(["resolved", "expired"]);
|
|
214
|
+
|
|
215
|
+
function isNonEmptyString(v: unknown): v is string {
|
|
216
|
+
return typeof v === "string" && v.length > 0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function isPositiveNumber(v: unknown): v is number {
|
|
220
|
+
return typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function isNonNegativeNumber(v: unknown): v is number {
|
|
224
|
+
return typeof v === "number" && Number.isFinite(v) && v >= 0;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function validateTimeContextOp(raw: unknown): TimeContextOp | null {
|
|
228
|
+
if (raw == null || typeof raw !== "object") return null;
|
|
229
|
+
const obj = raw as Record<string, unknown>;
|
|
230
|
+
const action = obj.action;
|
|
231
|
+
|
|
232
|
+
if (!isNonEmptyString(action) || !VALID_TIME_CONTEXT_ACTIONS.has(action)) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (action === "create") {
|
|
237
|
+
if (
|
|
238
|
+
!isNonEmptyString(obj.summary) ||
|
|
239
|
+
!isNonEmptyString(obj.source) ||
|
|
240
|
+
!isNonNegativeNumber(obj.activeFrom) ||
|
|
241
|
+
!isPositiveNumber(obj.activeUntil)
|
|
242
|
+
) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
action: "create",
|
|
247
|
+
summary: obj.summary,
|
|
248
|
+
source: obj.source,
|
|
249
|
+
activeFrom: obj.activeFrom,
|
|
250
|
+
activeUntil: obj.activeUntil,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (action === "update") {
|
|
255
|
+
if (!isNonEmptyString(obj.id)) return null;
|
|
256
|
+
// Extract and narrow optional fields
|
|
257
|
+
const summary = isNonEmptyString(obj.summary) ? obj.summary : undefined;
|
|
258
|
+
const activeFrom = isNonNegativeNumber(obj.activeFrom)
|
|
259
|
+
? obj.activeFrom
|
|
260
|
+
: undefined;
|
|
261
|
+
const activeUntil = isPositiveNumber(obj.activeUntil)
|
|
262
|
+
? obj.activeUntil
|
|
263
|
+
: undefined;
|
|
264
|
+
// At least one field must be provided for the update to be meaningful
|
|
265
|
+
if (
|
|
266
|
+
summary === undefined &&
|
|
267
|
+
activeFrom === undefined &&
|
|
268
|
+
activeUntil === undefined
|
|
269
|
+
) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const result: TimeContextUpdate = {
|
|
273
|
+
action: "update",
|
|
274
|
+
id: obj.id,
|
|
275
|
+
};
|
|
276
|
+
if (summary !== undefined) result.summary = summary;
|
|
277
|
+
if (activeFrom !== undefined) result.activeFrom = activeFrom;
|
|
278
|
+
if (activeUntil !== undefined) result.activeUntil = activeUntil;
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// resolve
|
|
283
|
+
if (!isNonEmptyString(obj.id)) return null;
|
|
284
|
+
return { action: "resolve", id: obj.id };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function validateOpenLoopOp(raw: unknown): OpenLoopOp | null {
|
|
288
|
+
if (raw == null || typeof raw !== "object") return null;
|
|
289
|
+
const obj = raw as Record<string, unknown>;
|
|
290
|
+
const action = obj.action;
|
|
291
|
+
|
|
292
|
+
if (!isNonEmptyString(action) || !VALID_OPEN_LOOP_ACTIONS.has(action)) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (action === "create") {
|
|
297
|
+
if (!isNonEmptyString(obj.summary) || !isNonEmptyString(obj.source)) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const result: OpenLoopCreate = {
|
|
301
|
+
action: "create",
|
|
302
|
+
summary: obj.summary,
|
|
303
|
+
source: obj.source,
|
|
304
|
+
};
|
|
305
|
+
const dueAt = isNonNegativeNumber(obj.dueAt) ? obj.dueAt : undefined;
|
|
306
|
+
if (dueAt !== undefined) result.dueAt = dueAt;
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (action === "update") {
|
|
311
|
+
if (!isNonEmptyString(obj.id)) return null;
|
|
312
|
+
const summary = isNonEmptyString(obj.summary) ? obj.summary : undefined;
|
|
313
|
+
const dueAt = isNonNegativeNumber(obj.dueAt) ? obj.dueAt : undefined;
|
|
314
|
+
if (summary === undefined && dueAt === undefined) return null;
|
|
315
|
+
|
|
316
|
+
const result: OpenLoopUpdate = {
|
|
317
|
+
action: "update",
|
|
318
|
+
id: obj.id,
|
|
319
|
+
};
|
|
320
|
+
if (summary !== undefined) result.summary = summary;
|
|
321
|
+
if (dueAt !== undefined) result.dueAt = dueAt;
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// resolve
|
|
326
|
+
if (!isNonEmptyString(obj.id)) return null;
|
|
327
|
+
if (
|
|
328
|
+
!isNonEmptyString(obj.status) ||
|
|
329
|
+
!VALID_OPEN_LOOP_RESOLVE_STATUSES.has(obj.status)
|
|
330
|
+
) {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
action: "resolve",
|
|
335
|
+
id: obj.id,
|
|
336
|
+
status: obj.status as "resolved" | "expired",
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function validateArchiveObservation(
|
|
341
|
+
raw: unknown,
|
|
342
|
+
): ArchiveObservationCandidate | null {
|
|
343
|
+
if (raw == null || typeof raw !== "object") return null;
|
|
344
|
+
const obj = raw as Record<string, unknown>;
|
|
345
|
+
if (!isNonEmptyString(obj.content) || !isNonEmptyString(obj.role)) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
const result: ArchiveObservationCandidate = {
|
|
349
|
+
content: obj.content,
|
|
350
|
+
role: obj.role,
|
|
351
|
+
};
|
|
352
|
+
if (isNonEmptyString(obj.modality)) result.modality = obj.modality;
|
|
353
|
+
if (isNonEmptyString(obj.source)) result.source = obj.source;
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function validateArchiveEpisode(raw: unknown): ArchiveEpisodeCandidate | null {
|
|
358
|
+
if (raw == null || typeof raw !== "object") return null;
|
|
359
|
+
const obj = raw as Record<string, unknown>;
|
|
360
|
+
if (!isNonEmptyString(obj.title) || !isNonEmptyString(obj.summary)) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
const result: ArchiveEpisodeCandidate = {
|
|
364
|
+
title: obj.title,
|
|
365
|
+
summary: obj.summary,
|
|
366
|
+
};
|
|
367
|
+
if (isNonEmptyString(obj.source)) result.source = obj.source;
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ── Public API ─────────────────────────────────────────────────────────
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Parse raw model output into a validated ReducerResult.
|
|
375
|
+
*
|
|
376
|
+
* On any structural error (non-JSON, missing top-level keys, wrong types)
|
|
377
|
+
* the function returns EMPTY_REDUCER_RESULT rather than throwing. Individual
|
|
378
|
+
* invalid operations within an otherwise valid structure are silently dropped
|
|
379
|
+
* to preserve the rest of the result.
|
|
380
|
+
*
|
|
381
|
+
* However, if **all four** top-level arrays are absent or not arrays, the
|
|
382
|
+
* entire output is treated as invalid and returns the empty result.
|
|
383
|
+
*/
|
|
384
|
+
export function parseReducerOutput(raw: string): ReducerResult {
|
|
385
|
+
let parsed: unknown;
|
|
386
|
+
try {
|
|
387
|
+
parsed = JSON.parse(raw);
|
|
388
|
+
} catch {
|
|
389
|
+
log.warn("reducer output is not valid JSON — falling back to empty result");
|
|
390
|
+
return EMPTY_REDUCER_RESULT;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
394
|
+
log.warn(
|
|
395
|
+
"reducer output is not a JSON object — falling back to empty result",
|
|
396
|
+
);
|
|
397
|
+
return EMPTY_REDUCER_RESULT;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const obj = parsed as Record<string, unknown>;
|
|
401
|
+
|
|
402
|
+
// Check that at least one top-level array key exists
|
|
403
|
+
const hasTimeContexts = Array.isArray(obj.timeContexts);
|
|
404
|
+
const hasOpenLoops = Array.isArray(obj.openLoops);
|
|
405
|
+
const hasArchiveObservations = Array.isArray(obj.archiveObservations);
|
|
406
|
+
const hasArchiveEpisodes = Array.isArray(obj.archiveEpisodes);
|
|
407
|
+
|
|
408
|
+
if (
|
|
409
|
+
!hasTimeContexts &&
|
|
410
|
+
!hasOpenLoops &&
|
|
411
|
+
!hasArchiveObservations &&
|
|
412
|
+
!hasArchiveEpisodes
|
|
413
|
+
) {
|
|
414
|
+
log.warn(
|
|
415
|
+
"reducer output has no recognized top-level arrays — falling back to empty result",
|
|
416
|
+
);
|
|
417
|
+
return EMPTY_REDUCER_RESULT;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const timeContexts: TimeContextOp[] = [];
|
|
421
|
+
if (hasTimeContexts) {
|
|
422
|
+
for (const item of obj.timeContexts as unknown[]) {
|
|
423
|
+
const validated = validateTimeContextOp(item);
|
|
424
|
+
if (validated) timeContexts.push(validated);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const openLoops: OpenLoopOp[] = [];
|
|
429
|
+
if (hasOpenLoops) {
|
|
430
|
+
for (const item of obj.openLoops as unknown[]) {
|
|
431
|
+
const validated = validateOpenLoopOp(item);
|
|
432
|
+
if (validated) openLoops.push(validated);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const archiveObservations: ArchiveObservationCandidate[] = [];
|
|
437
|
+
if (hasArchiveObservations) {
|
|
438
|
+
for (const item of obj.archiveObservations as unknown[]) {
|
|
439
|
+
const validated = validateArchiveObservation(item);
|
|
440
|
+
if (validated) archiveObservations.push(validated);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const archiveEpisodes: ArchiveEpisodeCandidate[] = [];
|
|
445
|
+
if (hasArchiveEpisodes) {
|
|
446
|
+
for (const item of obj.archiveEpisodes as unknown[]) {
|
|
447
|
+
const validated = validateArchiveEpisode(item);
|
|
448
|
+
if (validated) archiveEpisodes.push(validated);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return { timeContexts, openLoops, archiveObservations, archiveEpisodes };
|
|
453
|
+
}
|
|
@@ -30,6 +30,9 @@ export const conversations = sqliteTable(
|
|
|
30
30
|
forkParentMessageId: text("fork_parent_message_id"),
|
|
31
31
|
isAutoTitle: integer("is_auto_title").notNull().default(1),
|
|
32
32
|
scheduleJobId: text("schedule_job_id"),
|
|
33
|
+
memoryReducedThroughMessageId: text("memory_reduced_through_message_id"),
|
|
34
|
+
memoryDirtyTailSinceMessageId: text("memory_dirty_tail_since_message_id"),
|
|
35
|
+
memoryLastReducedAt: integer("memory_last_reduced_at"),
|
|
33
36
|
},
|
|
34
37
|
(table) => [
|
|
35
38
|
index("idx_conversations_updated_at").on(table.updatedAt),
|
|
@@ -3,6 +3,8 @@ export * from "./contacts.js";
|
|
|
3
3
|
export * from "./conversations.js";
|
|
4
4
|
export * from "./guardian.js";
|
|
5
5
|
export * from "./infrastructure.js";
|
|
6
|
+
export * from "./memory-archive.js";
|
|
7
|
+
export * from "./memory-brief.js";
|
|
6
8
|
export * from "./memory-core.js";
|
|
7
9
|
export * from "./notifications.js";
|
|
8
10
|
export * from "./oauth.js";
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
index,
|
|
3
|
+
integer,
|
|
4
|
+
sqliteTable,
|
|
5
|
+
text,
|
|
6
|
+
uniqueIndex,
|
|
7
|
+
} from "drizzle-orm/sqlite-core";
|
|
8
|
+
|
|
9
|
+
import { conversations, messages } from "./conversations.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Raw observation records captured from conversation turns. Each observation
|
|
13
|
+
* is a single factual statement extracted from user or assistant messages,
|
|
14
|
+
* annotated with modality and source metadata for downstream recall.
|
|
15
|
+
*/
|
|
16
|
+
export const memoryObservations = sqliteTable(
|
|
17
|
+
"memory_observations",
|
|
18
|
+
{
|
|
19
|
+
id: text("id").primaryKey(),
|
|
20
|
+
scopeId: text("scope_id").notNull().default("default"),
|
|
21
|
+
conversationId: text("conversation_id")
|
|
22
|
+
.notNull()
|
|
23
|
+
.references(() => conversations.id, { onDelete: "cascade" }),
|
|
24
|
+
messageId: text("message_id").references(() => messages.id, {
|
|
25
|
+
onDelete: "set null",
|
|
26
|
+
}),
|
|
27
|
+
/** The role that produced the observation (e.g. "user", "assistant"). */
|
|
28
|
+
role: text("role").notNull(),
|
|
29
|
+
/** Free-text statement capturing the observed fact. */
|
|
30
|
+
content: text("content").notNull(),
|
|
31
|
+
/**
|
|
32
|
+
* Modality of the source material: "text", "voice", "image", etc.
|
|
33
|
+
* Enables downstream filters for recall relevance.
|
|
34
|
+
*/
|
|
35
|
+
modality: text("modality").notNull().default("text"),
|
|
36
|
+
/**
|
|
37
|
+
* Source channel or interface that produced the observation
|
|
38
|
+
* (e.g. "vellum", "telegram", "phone").
|
|
39
|
+
*/
|
|
40
|
+
source: text("source"),
|
|
41
|
+
createdAt: integer("created_at").notNull(),
|
|
42
|
+
},
|
|
43
|
+
(table) => [
|
|
44
|
+
index("idx_memory_observations_scope_id").on(table.scopeId),
|
|
45
|
+
index("idx_memory_observations_conversation_id").on(table.conversationId),
|
|
46
|
+
index("idx_memory_observations_created_at").on(table.createdAt),
|
|
47
|
+
],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Deduplicated content chunks derived from observations. Chunks are the unit
|
|
52
|
+
* of embedding and recall — each chunk carries a contentHash for idempotent
|
|
53
|
+
* dual-write safety so the same content is never stored twice.
|
|
54
|
+
*/
|
|
55
|
+
export const memoryChunks = sqliteTable(
|
|
56
|
+
"memory_chunks",
|
|
57
|
+
{
|
|
58
|
+
id: text("id").primaryKey(),
|
|
59
|
+
scopeId: text("scope_id").notNull().default("default"),
|
|
60
|
+
observationId: text("observation_id")
|
|
61
|
+
.notNull()
|
|
62
|
+
.references(() => memoryObservations.id, { onDelete: "cascade" }),
|
|
63
|
+
/** The chunk text used for embedding and recall. */
|
|
64
|
+
content: text("content").notNull(),
|
|
65
|
+
/** Token count estimate for context-window budgeting. */
|
|
66
|
+
tokenEstimate: integer("token_estimate").notNull(),
|
|
67
|
+
/**
|
|
68
|
+
* SHA-256 hash of the normalized content, used to skip duplicate inserts
|
|
69
|
+
* during dual-write windows.
|
|
70
|
+
*/
|
|
71
|
+
contentHash: text("content_hash").notNull(),
|
|
72
|
+
createdAt: integer("created_at").notNull(),
|
|
73
|
+
},
|
|
74
|
+
(table) => [
|
|
75
|
+
index("idx_memory_chunks_scope_id").on(table.scopeId),
|
|
76
|
+
index("idx_memory_chunks_observation_id").on(table.observationId),
|
|
77
|
+
uniqueIndex("idx_memory_chunks_content_hash").on(
|
|
78
|
+
table.scopeId,
|
|
79
|
+
table.contentHash,
|
|
80
|
+
),
|
|
81
|
+
index("idx_memory_chunks_created_at").on(table.createdAt),
|
|
82
|
+
],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Episode records that group related observations into coherent narrative
|
|
87
|
+
* units. An episode represents a meaningful interaction or topic span,
|
|
88
|
+
* with source-link metadata for provenance tracking.
|
|
89
|
+
*/
|
|
90
|
+
export const memoryEpisodes = sqliteTable(
|
|
91
|
+
"memory_episodes",
|
|
92
|
+
{
|
|
93
|
+
id: text("id").primaryKey(),
|
|
94
|
+
scopeId: text("scope_id").notNull().default("default"),
|
|
95
|
+
conversationId: text("conversation_id")
|
|
96
|
+
.notNull()
|
|
97
|
+
.references(() => conversations.id, { onDelete: "cascade" }),
|
|
98
|
+
/** Human-readable title summarizing the episode. */
|
|
99
|
+
title: text("title").notNull(),
|
|
100
|
+
/** Longer narrative summary of the episode content. */
|
|
101
|
+
summary: text("summary").notNull(),
|
|
102
|
+
/** Token count estimate for the summary. */
|
|
103
|
+
tokenEstimate: integer("token_estimate").notNull(),
|
|
104
|
+
/**
|
|
105
|
+
* Source channel or interface that produced the episode
|
|
106
|
+
* (mirrors observation.source for episode-level filtering).
|
|
107
|
+
*/
|
|
108
|
+
source: text("source"),
|
|
109
|
+
/** Epoch-ms timestamp of the earliest observation in the episode. */
|
|
110
|
+
startAt: integer("start_at").notNull(),
|
|
111
|
+
/** Epoch-ms timestamp of the latest observation in the episode. */
|
|
112
|
+
endAt: integer("end_at").notNull(),
|
|
113
|
+
createdAt: integer("created_at").notNull(),
|
|
114
|
+
updatedAt: integer("updated_at").notNull(),
|
|
115
|
+
},
|
|
116
|
+
(table) => [
|
|
117
|
+
index("idx_memory_episodes_scope_id").on(table.scopeId),
|
|
118
|
+
index("idx_memory_episodes_conversation_id").on(table.conversationId),
|
|
119
|
+
index("idx_memory_episodes_created_at").on(table.createdAt),
|
|
120
|
+
],
|
|
121
|
+
);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { index, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Time contexts represent bounded temporal windows that are relevant to the
|
|
5
|
+
* assistant's current awareness — e.g. "user is traveling next week",
|
|
6
|
+
* "quarterly planning period ends Friday". Each row captures one window
|
|
7
|
+
* with an activation range and a human-readable summary the brief can surface.
|
|
8
|
+
*/
|
|
9
|
+
export const timeContexts = sqliteTable(
|
|
10
|
+
"time_contexts",
|
|
11
|
+
{
|
|
12
|
+
id: text("id").primaryKey(),
|
|
13
|
+
scopeId: text("scope_id").notNull(),
|
|
14
|
+
summary: text("summary").notNull(),
|
|
15
|
+
source: text("source").notNull(), // e.g. 'conversation', 'schedule', 'manual'
|
|
16
|
+
activeFrom: integer("active_from").notNull(), // epoch ms — window start
|
|
17
|
+
activeUntil: integer("active_until").notNull(), // epoch ms — window end
|
|
18
|
+
createdAt: integer("created_at").notNull(),
|
|
19
|
+
updatedAt: integer("updated_at").notNull(),
|
|
20
|
+
},
|
|
21
|
+
(table) => [
|
|
22
|
+
index("idx_time_contexts_scope_active_until").on(
|
|
23
|
+
table.scopeId,
|
|
24
|
+
table.activeUntil,
|
|
25
|
+
),
|
|
26
|
+
],
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Open loops track unresolved items the assistant should follow up on —
|
|
31
|
+
* e.g. "waiting for Bob's reply", "need to file taxes before April 15".
|
|
32
|
+
* Each row carries a status and an optional due date so the brief can
|
|
33
|
+
* prioritise which loops to surface.
|
|
34
|
+
*/
|
|
35
|
+
export const openLoops = sqliteTable(
|
|
36
|
+
"open_loops",
|
|
37
|
+
{
|
|
38
|
+
id: text("id").primaryKey(),
|
|
39
|
+
scopeId: text("scope_id").notNull(),
|
|
40
|
+
summary: text("summary").notNull(),
|
|
41
|
+
status: text("status").notNull().default("open"), // 'open' | 'resolved' | 'expired'
|
|
42
|
+
source: text("source").notNull(), // e.g. 'conversation', 'followup', 'manual'
|
|
43
|
+
dueAt: integer("due_at"), // epoch ms — optional deadline
|
|
44
|
+
surfacedAt: integer("surfaced_at"), // epoch ms — last time shown in brief
|
|
45
|
+
createdAt: integer("created_at").notNull(),
|
|
46
|
+
updatedAt: integer("updated_at").notNull(),
|
|
47
|
+
},
|
|
48
|
+
(table) => [
|
|
49
|
+
index("idx_open_loops_scope_status_due").on(
|
|
50
|
+
table.scopeId,
|
|
51
|
+
table.status,
|
|
52
|
+
table.dueAt,
|
|
53
|
+
),
|
|
54
|
+
],
|
|
55
|
+
);
|
|
@@ -61,6 +61,7 @@ export async function semanticSearch(
|
|
|
61
61
|
fetchLimit,
|
|
62
62
|
["item", "summary", "segment", "media"],
|
|
63
63
|
excludedMessageIds,
|
|
64
|
+
scopeIds,
|
|
64
65
|
),
|
|
65
66
|
);
|
|
66
67
|
}
|
|
@@ -277,13 +278,13 @@ export async function semanticSearch(
|
|
|
277
278
|
* Build a Qdrant filter for hybrid search. Mirrors the logic in
|
|
278
279
|
* `searchWithFilter` but as a standalone object for the query API.
|
|
279
280
|
*
|
|
280
|
-
* Scope filtering:
|
|
281
|
-
*
|
|
282
|
-
*
|
|
281
|
+
* Scope filtering: points with a `memory_scope_id` payload field are
|
|
282
|
+
* filtered at the Qdrant level. Legacy points without the field pass
|
|
283
|
+
* through and are caught by post-query DB filtering.
|
|
283
284
|
*/
|
|
284
285
|
function buildHybridFilter(
|
|
285
286
|
excludeMessageIds: string[],
|
|
286
|
-
|
|
287
|
+
scopeIds?: string[],
|
|
287
288
|
): Record<string, unknown> {
|
|
288
289
|
const mustConditions: Array<Record<string, unknown>> = [
|
|
289
290
|
{
|
|
@@ -310,6 +311,18 @@ function buildHybridFilter(
|
|
|
310
311
|
});
|
|
311
312
|
}
|
|
312
313
|
|
|
314
|
+
// Scope filtering: accept points whose memory_scope_id matches one of the
|
|
315
|
+
// allowed scopes, OR points that lack the field entirely (legacy data).
|
|
316
|
+
// Post-query DB filtering remains as defense-in-depth for legacy points.
|
|
317
|
+
if (scopeIds && scopeIds.length > 0) {
|
|
318
|
+
mustConditions.push({
|
|
319
|
+
should: [
|
|
320
|
+
{ key: "memory_scope_id", match: { any: scopeIds } },
|
|
321
|
+
{ is_empty: { key: "memory_scope_id" } },
|
|
322
|
+
],
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
313
326
|
const mustNotConditions: Array<Record<string, unknown>> = [
|
|
314
327
|
{ key: "_meta", match: { value: true } },
|
|
315
328
|
];
|