@senselab-ai/amfs 0.3.1 → 0.4.5

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.
@@ -9,16 +9,56 @@ export interface HttpAdapterOptions {
9
9
  agentId?: string;
10
10
  headers?: Record<string, string>;
11
11
  }
12
+ /**
13
+ * Request header naming the agent the calling session acts as. Read routes
14
+ * carry no agent in their query or body, so without it a hosted server cannot
15
+ * attribute a read or look up the agent's live repair canary.
16
+ */
17
+ export declare const AGENT_ID_HEADER = "X-AMFS-Agent-Id";
18
+ /**
19
+ * Request header carrying the session id the SDK stamps on its trace — the key
20
+ * a hosted server hashes to keep one session on one arm of a live canary. A
21
+ * session that does not send it is never routed.
22
+ */
23
+ export declare const SESSION_HEADER = "X-AMFS-Session";
12
24
  export declare class HttpAdapter implements AmfsAdapter {
13
25
  private readonly baseUrl;
14
26
  private readonly headers;
15
27
  private readonly agentId;
16
28
  constructor(opts: HttpAdapterOptions);
29
+ private readonly opts;
30
+ /**
31
+ * An adapter on the same server that identifies itself as `agentId` in
32
+ * `sessionId` on every request. `AgentMemory` calls this when it is built, so
33
+ * two memories sharing one adapter never share an identity. Either id may be
34
+ * omitted; the header is then simply not sent.
35
+ */
36
+ bind(agentId: string | undefined, sessionId: string | undefined): HttpAdapter;
37
+ /** The identity headers this adapter adds to every request (a copy). */
38
+ get identityHeaders(): Record<string, string>;
17
39
  private fetch;
40
+ /** Like `fetch` but returns the server's JSON untouched (snake_case keys). */
41
+ private fetchRaw;
42
+ /**
43
+ * Call a route this adapter has no method for, and get the server's JSON back
44
+ * untouched (snake_case keys).
45
+ *
46
+ * The request is this adapter's own: the same base URL, API key and identity
47
+ * headers as every other call, and the same error, an `Error` whose message
48
+ * carries the status and the response body. `AgentMemory`'s room and
49
+ * negotiation methods go through it. `query` values that are `undefined` or
50
+ * `null` are left out; the rest are URL-encoded.
51
+ */
52
+ requestRaw(method: string, path: string, options?: {
53
+ body?: unknown;
54
+ query?: Record<string, string | number | boolean | undefined | null>;
55
+ }): Promise<unknown>;
18
56
  read(entityPath: string, key: string, options?: {
19
57
  minConfidence?: number;
20
58
  }): MemoryEntry | null;
21
- readAsync(entityPath: string, key: string): Promise<MemoryEntry | null>;
59
+ readAsync(entityPath: string, key: string, options?: {
60
+ branch?: string;
61
+ }): Promise<MemoryEntry | null>;
22
62
  write(entry: MemoryEntry): MemoryEntry;
23
63
  writeAsync(entry: {
24
64
  entityPath: string;
@@ -36,14 +76,20 @@ export declare class HttpAdapter implements AmfsAdapter {
36
76
  }): MemoryEntry[];
37
77
  listAsync(entityPath?: string, options?: {
38
78
  includeSuperseded?: boolean;
79
+ branch?: string;
39
80
  }): Promise<MemoryEntry[]>;
40
81
  searchAsync(query: {
41
82
  query?: string;
42
83
  entityPath?: string;
43
84
  minConfidence?: number;
85
+ maxConfidence?: number;
44
86
  agentId?: string;
87
+ since?: string;
88
+ patternRef?: string;
45
89
  sortBy?: string;
46
90
  limit?: number;
91
+ depth?: number;
92
+ branch?: string;
47
93
  }): Promise<MemoryEntry[]>;
48
94
  watch(entityPath: string, callback: (entry: MemoryEntry) => void): WatchHandle;
49
95
  commitOutcome(record: OutcomeRecord): MemoryEntry[];
@@ -52,71 +98,226 @@ export declare class HttpAdapter implements AmfsAdapter {
52
98
  outcomeType: string;
53
99
  entityPath?: string;
54
100
  causalEntryKeys?: string[];
101
+ causalConfidence?: number;
102
+ agentId?: string;
103
+ /**
104
+ * Already in wire shape (`attributes`, `llm_calls`, ...); the server lifts
105
+ * these onto the trace it seals for this outcome.
106
+ */
107
+ sessionMetadata?: Record<string, unknown>;
108
+ /** The request the run answered — what a judge and training read the trace through. */
109
+ taskInput?: string | null;
110
+ /** The agent's answer, in full. */
111
+ responseText?: string | null;
112
+ /** The actions taken, as `{tool_name, arguments}` rows. */
113
+ toolCalls?: Array<Record<string, unknown>>;
55
114
  }): Promise<unknown>;
56
115
  statsAsync(): Promise<Record<string, unknown>>;
57
116
  healthAsync(): Promise<Record<string, unknown>>;
58
- listTracesAsync(options?: {
117
+ briefingAsync(options?: {
59
118
  entityPath?: string;
60
119
  agentId?: string;
61
- outcomeType?: string;
62
120
  limit?: number;
63
- }): Promise<DecisionTraceSummary[]>;
121
+ /** Lead digest only, with its evidence sections; a fraction of the tokens. */
122
+ compact?: boolean;
123
+ /** Only what changed after this ISO-8601 moment in the list sections. */
124
+ since?: string;
125
+ /** Brief from a memory branch other than `main` (a repair branch, a canary). */
126
+ branch?: string;
127
+ }): Promise<unknown>;
128
+ consolidationStatusAsync(): Promise<unknown>;
129
+ consolidationProposalsAsync(options?: {
130
+ status?: string;
131
+ limit?: number;
132
+ }): Promise<unknown>;
133
+ consolidationCandidatesAsync(options?: {
134
+ limit?: number;
135
+ }): Promise<unknown>;
136
+ consolidateAsync(options?: {
137
+ dryRun?: boolean;
138
+ }): Promise<unknown>;
139
+ /**
140
+ * One page of decision traces, newest first, exactly as the server pages them.
141
+ *
142
+ * Mirrors GET /api/v1/traces: pass `cursor` (the previous page's `nextCursor`)
143
+ * to continue; `offset` is honoured only when no cursor is given. `since`
144
+ * (inclusive) and `until` (exclusive) are sent to the server and bound the
145
+ * query itself, so a window that excludes the newest traces still returns
146
+ * the older matches rather than an empty first page.
147
+ */
148
+ listTracesPageAsync(options?: ListTracesOptions): Promise<DecisionTracePage>;
149
+ /**
150
+ * Decision traces as a flat array. Accepts the same paging options as
151
+ * {@link listTracesPageAsync}; use that method when you need `nextCursor` /
152
+ * `hasMore` to walk beyond one page.
153
+ */
154
+ listTracesAsync(options?: ListTracesOptions): Promise<DecisionTraceSummary[]>;
64
155
  getTraceAsync(traceId: string): Promise<DecisionTrace | null>;
156
+ /** Server-side semantic retrieval (embedding + similarity + blend happen on the server). */
157
+ retrieveAsync(query: string, options?: {
158
+ entityPath?: string;
159
+ minConfidence?: number;
160
+ limit?: number;
161
+ semanticWeight?: number;
162
+ recencyWeight?: number;
163
+ confidenceWeight?: number;
164
+ includeArtifacts?: boolean;
165
+ branch?: string;
166
+ }): Promise<Array<{
167
+ entry: MemoryEntry;
168
+ score: number;
169
+ }>>;
170
+ /** Full version history of a key, ordered by version. */
171
+ historyAsync(entityPath: string, key: string, options?: {
172
+ since?: string;
173
+ until?: string;
174
+ }): Promise<MemoryEntry[]>;
175
+ /** Recent timeline events for an agent (writes, outcomes, cross-agent reads). */
176
+ timelineAsync(agentId: string, options?: {
177
+ eventType?: string;
178
+ since?: string;
179
+ limit?: number;
180
+ branch?: string;
181
+ }): Promise<unknown[]>;
182
+ /** Related entities in the knowledge graph (Pro). */
183
+ graphNeighborsAsync(options: {
184
+ entity: string;
185
+ relation?: string;
186
+ direction?: string;
187
+ minConfidence?: number;
188
+ depth?: number;
189
+ limit?: number;
190
+ }): Promise<unknown[]>;
191
+ /** Export outcome-validated knowledge as a fine-tuning dataset (Pro). */
192
+ exportTrainingDataAsync(options?: {
193
+ entityPath?: string;
194
+ minConfidence?: number;
195
+ format?: "sft" | "dpo" | "reward_model";
196
+ limit?: number;
197
+ }): Promise<unknown>;
65
198
  }
66
199
  export interface DecisionTraceSummary {
67
200
  id: string;
68
- agent_id: string;
69
- outcome_ref: string;
70
- outcome_type: string;
71
- decision_summary?: string;
72
- causal_entries: number;
73
- external_contexts: number;
74
- session_duration_ms?: number;
75
- created_at: string;
201
+ agentId: string;
202
+ outcomeRef: string;
203
+ outcomeType: string;
204
+ decisionSummary?: string;
205
+ causalEntries: number;
206
+ externalContexts: number;
207
+ sessionDurationMs?: number;
208
+ createdAt: string;
209
+ }
210
+ /** Filters and paging for GET /api/v1/traces. */
211
+ export interface ListTracesOptions {
212
+ entityPath?: string;
213
+ agentId?: string;
214
+ outcomeType?: string;
215
+ limit?: number;
216
+ /** Rows to skip; honoured only when no `cursor` is given. */
217
+ offset?: number;
218
+ /** Opaque keyset position — the `nextCursor` of the previous page. */
219
+ cursor?: string;
220
+ /** Only traces created at or after this instant (ISO-8601 string or Date). */
221
+ since?: string | Date;
222
+ /** Only traces created before this instant (ISO-8601 string or Date). */
223
+ until?: string | Date;
224
+ }
225
+ /** One page of GET /api/v1/traces. Follow `nextCursor` while `hasMore` is true. */
226
+ export interface DecisionTracePage {
227
+ traces: DecisionTraceSummary[];
228
+ nextCursor: string | null;
229
+ hasMore: boolean;
230
+ }
231
+ /**
232
+ * An action the agent recorded during the session (`amfs_record_action`).
233
+ * `arguments` is passed through exactly as stored — its keys are the caller's,
234
+ * not the API's, so they are not converted to camelCase.
235
+ */
236
+ export interface ToolCall {
237
+ toolName: string;
238
+ arguments: Record<string, unknown>;
239
+ resultSummary: string;
240
+ resultHash: string;
241
+ startedAt?: string;
242
+ durationMs: number;
243
+ source?: string | null;
244
+ success: boolean;
245
+ }
246
+ /** Which model, client and toolset produced the session's decisions. */
247
+ export interface SessionMetadata {
248
+ model?: string | null;
249
+ clientName?: string | null;
250
+ platform?: string | null;
251
+ toolsAvailable: string[];
252
+ mcpClientId?: string | null;
253
+ mcpSessionId?: string | null;
254
+ /** The run's dimensions (`customer`, `task_type`, ...), when any were set. */
255
+ attributes: Record<string, string | number | boolean>;
256
+ /** LLM calls recorded for the run, in wire shape (snake_case keys). */
257
+ llmCalls: Array<Record<string, unknown>>;
76
258
  }
77
259
  export interface DecisionTrace {
78
260
  id: string;
79
- agent_id: string;
80
- session_id: string;
81
- outcome_ref: string;
82
- outcome_type: string;
83
- decision_summary?: string;
84
- causal_entries: Array<{
85
- entity_path: string;
261
+ agentId: string;
262
+ sessionId: string;
263
+ outcomeRef: string;
264
+ outcomeType: string;
265
+ decisionSummary?: string;
266
+ /** The request that started the work, as it arrived (may be redacted). */
267
+ taskInput?: string | null;
268
+ /** The agent's answer, when it was captured. */
269
+ responseText?: string | null;
270
+ /** Actions the agent recorded; empty for read/write-only sessions. */
271
+ toolCalls: ToolCall[];
272
+ sessionMetadata?: SessionMetadata | null;
273
+ causalEntries: Array<{
274
+ entityPath: string;
86
275
  key: string;
87
276
  version: number;
88
277
  confidence: number;
89
278
  value?: unknown;
90
- memory_type?: string;
91
- written_by?: string;
92
- read_at?: string;
279
+ memoryType?: string;
280
+ writtenBy?: string;
281
+ readAt?: string;
93
282
  }>;
94
- external_contexts: Array<{
283
+ externalContexts: Array<{
95
284
  label: string;
96
285
  summary: string;
97
286
  source?: string;
98
- recorded_at?: string;
287
+ recordedAt?: string;
99
288
  }>;
100
- query_events: Array<{
289
+ queryEvents: Array<{
101
290
  operation: string;
102
291
  parameters: Record<string, unknown>;
103
- result_count: number;
104
- duration_ms?: number;
105
- occurred_at?: string;
292
+ resultCount: number;
293
+ durationMs?: number;
294
+ occurredAt?: string;
106
295
  }>;
107
- error_events: Array<{
296
+ errorEvents: Array<{
108
297
  operation: string;
109
- error_type: string;
298
+ errorType: string;
110
299
  message: string;
111
- stack_trace?: string;
112
- occurred_at?: string;
300
+ stackTrace?: string;
301
+ occurredAt?: string;
113
302
  }>;
114
- state_diff?: {
115
- entries_created: number;
116
- entries_updated: number;
303
+ stateDiff?: {
304
+ entriesCreated: number;
305
+ entriesUpdated: number;
117
306
  };
118
- session_started_at?: string;
119
- session_ended_at?: string;
120
- session_duration_ms?: number;
121
- created_at: string;
307
+ sessionStartedAt?: string;
308
+ sessionEndedAt?: string;
309
+ sessionDurationMs?: number;
310
+ createdAt: string;
122
311
  }
312
+ /**
313
+ * Map a raw (snake_case) trace from the server to a `DecisionTrace`.
314
+ *
315
+ * The bulk of the object goes through the generic key converter, as before;
316
+ * the fields the converter would get wrong are set explicitly afterwards:
317
+ * `toolCalls[].arguments` must keep the caller's own keys, and
318
+ * `taskInput` / `responseText` / `sessionMetadata` must exist (null, not
319
+ * undefined) so consumers can distinguish "not captured" from "old server".
320
+ */
321
+ export declare function toDecisionTrace(raw: unknown): DecisionTrace;
322
+ /** Map a raw GET /api/v1/traces body (new page shape or legacy array) to a page. */
323
+ export declare function toDecisionTracePage(raw: unknown): DecisionTracePage;