-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmemory-core.ts
More file actions
409 lines (393 loc) · 14 KB
/
Copy pathmemory-core.ts
File metadata and controls
409 lines (393 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* The single facade exposed by the algorithm core.
*
* Adapters call these methods (TypeScript adapters import the implementation
* directly; non-TS adapters dispatch via JSON-RPC method names defined in
* `jsonrpc.ts`).
*
* Implementation lives in `core/pipeline/memory-core.ts`. Tests mock this
* interface; SDK consumers depend only on this file.
*/
import type {
AgentKind,
ApiLogDTO,
EpisodeId,
EpisodeListItemDTO,
FeedbackDTO,
PolicyDTO,
RetrievalQueryDTO,
RetrievalResultDTO,
SessionId,
SkillDTO,
SkillId,
ToolOutcomeDTO,
TraceDTO,
TurnInputDTO,
TurnResultDTO,
WorldModelDTO,
} from "./dto.js";
import type { CoreEvent } from "./events.js";
import type { LogRecord } from "./log-record.js";
// ─── Public lifecycle / status ────────────────────────────────────────────────
export interface CoreHealth {
ok: boolean;
version: string;
uptimeMs: number;
agent: AgentKind;
paths: {
home: string;
config: string;
db: string;
skills: string;
logs: string;
};
llm: ModelHealth;
embedder: ModelHealth & { dim: number };
/**
* Dedicated skill-crystallization model. When the operator leaves
* `skillEvolver.model` blank, we surface the main LLM model with
* `inherited: true` so the viewer can label it as "inherits from LLM".
* The health fields mirror the main LLM client when `inherited=true`.
*/
skillEvolver: ModelHealth & { inherited: boolean };
}
/**
* Per-model connectivity summary used by the viewer's Overview page.
* `available` reflects whether the client is configured (non-null);
* `lastOkAt` / `lastError` reflect the most recent **actual** call
* outcome tracked by the runtime. A green dot means "called successfully
* at least once and the last call was good"; red means "most recent
* call failed" + the error text to surface in a tooltip.
*/
export interface ModelHealth {
available: boolean;
provider: string;
model: string;
lastOkAt: number | null;
lastError: { at: number; message: string } | null;
}
// ─── Subscriptions ────────────────────────────────────────────────────────────
export type Unsubscribe = () => void;
export interface MemoryCore {
// ── lifecycle ──
init(): Promise<void>;
shutdown(): Promise<void>;
health(): Promise<CoreHealth>;
// ── session / episode ──
openSession(input: { agent: AgentKind; sessionId?: SessionId }): Promise<SessionId>;
closeSession(sessionId: SessionId): Promise<void>;
openEpisode(input: {
sessionId: SessionId;
episodeId?: EpisodeId;
/** Optional initial user text (for adapters that know it). */
userMessage?: string;
}): Promise<EpisodeId>;
closeEpisode(episodeId: EpisodeId): Promise<void>;
/** Hard-delete a closed episode by id (idempotent). Rejects if the episode is still open. */
deleteEpisode(id: string): Promise<{ deleted: boolean }>;
/** Bulk delete closed episodes — returns how many rows were actually removed. */
deleteEpisodes(ids: readonly string[]): Promise<{ deleted: number }>;
// ── pipeline (per turn) ──
/** Called *before* the agent acts. Returns the context to inject. */
onTurnStart(turn: TurnInputDTO): Promise<RetrievalResultDTO>;
/** Called *after* the agent acts. Persists the trace, schedules induction, etc. */
onTurnEnd(result: TurnResultDTO): Promise<{ traceId: string; episodeId: EpisodeId }>;
/** Called when the user gives task-level feedback (or implicit signals fire). */
submitFeedback(feedback: Omit<FeedbackDTO, "id" | "ts"> & { ts?: number }): Promise<FeedbackDTO>;
/**
* Record a tool outcome. Feeds decision-repair so failure bursts can
* trigger targeted re-injection on the *next* turn. Non-blocking: the
* call returns before repair runs and never throws on unknown sessions.
*/
recordToolOutcome(outcome: ToolOutcomeDTO): void;
// ── memory queries ──
searchMemory(query: RetrievalQueryDTO): Promise<RetrievalResultDTO>;
getTrace(id: string): Promise<TraceDTO | null>;
/**
* Mutate a single trace's user-facing fields (role / summary /
* body). Never touches algorithmic signals. Returns the updated
* DTO (or null if the id is unknown).
*/
updateTrace(
id: string,
patch: {
summary?: string | null;
userText?: string;
agentText?: string;
tags?: readonly string[];
},
): Promise<TraceDTO | null>;
/** Delete a trace by id (idempotent). Hard delete. */
deleteTrace(id: string): Promise<{ deleted: boolean }>;
/**
* Bulk delete — takes an id list and returns how many rows were
* actually removed. The viewer's "批量删除" uses this.
*/
deleteTraces(ids: readonly string[]): Promise<{ deleted: number }>;
/**
* Update the sharing state for a trace. `scope = null` clears the
* share. The core never talks to the Hub itself; the adapter /
* viewer are responsible for the network call, then call this to
* persist the resulting state.
*/
shareTrace(
id: string,
share: {
scope: "private" | "public" | "hub" | null;
target?: string | null;
sharedAt?: number | null;
},
): Promise<TraceDTO | null>;
getPolicy(id: string): Promise<PolicyDTO | null>;
getWorldModel(id: string): Promise<WorldModelDTO | null>;
/**
* List L2 policies ("经验") — newest-first. The viewer uses this
* for the Experiences panel.
*/
listPolicies(input?: {
status?: PolicyDTO["status"];
limit?: number;
offset?: number;
q?: string;
}): Promise<PolicyDTO[]>;
/**
* List L3 world models ("世界环境知识") — newest-first.
*/
listWorldModels(input?: {
limit?: number;
offset?: number;
q?: string;
}): Promise<WorldModelDTO[]>;
/** Transition a policy through candidate → active → archived. */
setPolicyStatus(
id: string,
status: PolicyDTO["status"],
): Promise<PolicyDTO | null>;
/** Hard-delete a policy row. */
deletePolicy(id: string): Promise<{ deleted: boolean }>;
/**
* Append decision guidance (preference / anti-pattern lines) to a
* policy's `@repair` block. Used by the viewer's PolicyDrawer for
* manual guidance entry — real agents land here via the feedback
* pipeline (`core/feedback/feedback.ts::attachRepairToPolicies`).
*
* Duplicates are de-duped; lines that were already present are a
* no-op. Returns the updated DTO or `null` if the policy id is
* unknown.
*/
editPolicyGuidance(
id: string,
patch: { preference?: string[]; antiPattern?: string[] },
): Promise<PolicyDTO | null>;
/** Hard-delete a world-model row. */
deleteWorldModel(id: string): Promise<{ deleted: boolean }>;
/**
* Apply a sharing transition to a policy. Same semantics as
* {@link shareTrace}: pass `scope=null` to clear.
*/
sharePolicy(
id: string,
share: {
scope: "private" | "public" | "hub" | null;
target?: string | null;
sharedAt?: number | null;
},
): Promise<PolicyDTO | null>;
/**
* Apply a sharing transition to a world model. Same semantics as
* {@link shareTrace}.
*/
shareWorldModel(
id: string,
share: {
scope: "private" | "public" | "hub" | null;
target?: string | null;
sharedAt?: number | null;
},
): Promise<WorldModelDTO | null>;
/**
* User-driven content patch from the viewer's edit modal. Only
* surfaces title / trigger / procedure / verification / boundary —
* status, support, gain, vec stay owned by the induction pipeline.
*/
updatePolicy(
id: string,
patch: {
title?: string;
trigger?: string;
procedure?: string;
verification?: string;
boundary?: string;
},
): Promise<PolicyDTO | null>;
/**
* User-driven world-model patch. Mutates only `title`, `body`, or
* `status` (active ↔ archived).
*/
updateWorldModel(
id: string,
patch: { title?: string; body?: string; status?: "active" | "archived" },
): Promise<WorldModelDTO | null>;
/** Soft-archive a world model so it can be un-archived later. */
archiveWorldModel(id: string): Promise<WorldModelDTO | null>;
/** Reverse of {@link archiveWorldModel}. */
unarchiveWorldModel(id: string): Promise<WorldModelDTO | null>;
listEpisodes(input: { sessionId?: SessionId; limit?: number; offset?: number }): Promise<EpisodeId[]>;
/**
* Like `listEpisodes` but returns rich per-row metadata the viewer
* needs to render its task list without a second round trip
* (session id, status, turn count, first-turn preview).
*/
listEpisodeRows(input?: {
sessionId?: SessionId;
limit?: number;
offset?: number;
}): Promise<EpisodeListItemDTO[]>;
timeline(input: { episodeId: EpisodeId }): Promise<TraceDTO[]>;
/**
* Reverse-chronological trace listing for the Memories viewer.
*
* Unlike `searchMemory`, this is a pure timeline query — no embedder,
* no ranking, no cold-start guards. The viewer uses it to show the
* user's recent memory entries (summary + metadata) even when
* retrieval would otherwise return zero results (e.g. brand-new
* installs, query mismatch, offline embedder).
*
* @param input.limit default 50
* @param input.offset default 0
* @param input.sessionId optional filter
* @param input.q optional case-insensitive substring filter
* applied to `summary ?? userText`
*/
listTraces(input?: {
limit?: number;
offset?: number;
sessionId?: SessionId;
q?: string;
}): Promise<TraceDTO[]>;
/**
* Paged listing of the rich api_logs table ({@link ApiLogDTO}).
* Fuels the viewer's Logs page — shows every memory_search and
* memory_add call with the full input/output JSON.
*/
listApiLogs(input?: {
toolName?: string;
limit?: number;
offset?: number;
}): Promise<{ logs: ApiLogDTO[]; total: number }>;
// ── skills ──
listSkills(input?: { status?: SkillDTO["status"]; limit?: number }): Promise<SkillDTO[]>;
getSkill(id: SkillId): Promise<SkillDTO | null>;
archiveSkill(id: SkillId, reason?: string): Promise<void>;
/**
* Hard-delete a skill row. Distinct from {@link archiveSkill}, which
* keeps the row but flips its status. Idempotent.
*/
deleteSkill(id: SkillId): Promise<{ deleted: boolean }>;
/**
* Re-activate a previously archived skill. Sets status back to
* `'active'` and stamps `updatedAt = now`.
*/
reactivateSkill(id: SkillId): Promise<SkillDTO | null>;
/**
* User-driven skill patch from the viewer's edit modal. Only the
* narrowly user-facing fields are mutable (`name`, `invocationGuide`).
*/
updateSkill(
id: SkillId,
patch: { name?: string; invocationGuide?: string },
): Promise<SkillDTO | null>;
/** Apply a sharing transition to a skill. */
shareSkill(
id: SkillId,
share: {
scope: "private" | "public" | "hub" | null;
target?: string | null;
sharedAt?: number | null;
},
): Promise<SkillDTO | null>;
// ── config (for viewer settings tab) ──
/**
* Return the current resolved `config.yaml`, with sensitive fields
* (api keys, tokens) masked as `"••••"`. Safe to hand to a browser.
*/
getConfig(): Promise<Record<string, unknown>>;
/**
* Apply a partial patch to `config.yaml` (deep merge, preserve
* comments). Returns the new masked config.
*/
patchConfig(patch: Record<string, unknown>): Promise<Record<string, unknown>>;
// ── analytics (viewer dashboard) ──
/**
* Aggregate counts for the viewer's Analytics tab. `days` controls
* the window the `dailyWrites` histogram covers.
*/
metrics(input?: { days?: number }): Promise<{
total: number;
writesToday: number;
sessions: number;
embeddings: number;
dailyWrites: Array<{ date: string; count: number }>;
/** Skill counts + "tasks → skills" evolution rate. */
skillStats: {
total: number;
active: number;
candidate: number;
archived: number;
/** episodes that directly minted a skill / total episodes */
evolutionRate: number;
};
/** L2 policy counts + mean gain across active rows. */
policyStats: {
total: number;
active: number;
candidate: number;
archived: number;
/** Arithmetic mean of `gain` across *active* policies. */
avgGain: number;
/** Average quality score; we reuse `policy.gain` as a proxy. */
avgQuality: number;
};
worldModelCount: number;
decisionRepairCount: number;
/** One bucket per day for the last `days`, newest-last. */
dailySkillEvolutions: Array<{ date: string; count: number }>;
/** Most recent crystallisations; viewer uses this for the "最近进化事件" table. */
recentEvolutions: Array<{
ts: number;
skillId: string;
skillName: string;
status: "candidate" | "active" | "archived";
sourcePolicyIds: string[];
}>;
}>;
// ── export / import (bundle) ──
exportBundle(): Promise<{
version: 1;
exportedAt: number;
traces: TraceDTO[];
policies: PolicyDTO[];
worldModels: WorldModelDTO[];
skills: SkillDTO[];
}>;
importBundle(bundle: {
version?: number;
traces?: unknown[];
policies?: unknown[];
worldModels?: unknown[];
skills?: unknown[];
}): Promise<{ imported: number; skipped: number }>;
// ── observability ──
/** Subscribe to every CoreEvent the algorithm emits. Returns unsubscribe. */
subscribeEvents(handler: (e: CoreEvent) => void): Unsubscribe;
/**
* Snapshot of the most-recent aggregated events (ring-buffered). The
* SSE route replays these to clients on connect so the Overview
* "实时活动" panel isn't empty on page reload.
*/
getRecentEvents(): readonly CoreEvent[];
/** Subscribe to every (post-redaction) LogRecord. Returns unsubscribe. */
subscribeLogs(handler: (r: LogRecord) => void): Unsubscribe;
/** Allow non-TS adapters to forward their own log records into our sinks. */
forwardLog(record: LogRecord): void;
}