hebbrix 2.1.0 → 2.2.0
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/dist/index.d.mts +191 -9
- package/dist/index.d.ts +191 -9
- package/dist/index.js +188 -23
- package/dist/index.mjs +184 -22
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -57,8 +57,27 @@ interface SearchResponse {
|
|
|
57
57
|
total: number;
|
|
58
58
|
search_type: string;
|
|
59
59
|
processing_time_ms: number;
|
|
60
|
+
no_match: boolean;
|
|
61
|
+
abstain_recommended: boolean;
|
|
62
|
+
query_confidence: number;
|
|
63
|
+
grounding: GroundingReceipt;
|
|
64
|
+
evidence_ids: string[];
|
|
65
|
+
evidence_claims?: EvidenceClaim[];
|
|
66
|
+
safety_contract_version: string;
|
|
67
|
+
degraded?: boolean;
|
|
68
|
+
sdk_safety_reason?: string;
|
|
60
69
|
proof_context?: ProofContext;
|
|
61
70
|
}
|
|
71
|
+
interface GroundingReceipt {
|
|
72
|
+
status: string;
|
|
73
|
+
reason?: string;
|
|
74
|
+
contract_version?: string;
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
}
|
|
77
|
+
interface EvidenceClaim {
|
|
78
|
+
memory_id: string;
|
|
79
|
+
claims: Record<string, unknown>;
|
|
80
|
+
}
|
|
62
81
|
interface ProofContext {
|
|
63
82
|
schema_version: "proofloop-context-v1";
|
|
64
83
|
context_id: string;
|
|
@@ -88,9 +107,18 @@ interface ReasoningSource {
|
|
|
88
107
|
score: number;
|
|
89
108
|
}
|
|
90
109
|
interface ReasoningResponse {
|
|
91
|
-
answer: string;
|
|
110
|
+
answer: string | null;
|
|
92
111
|
sources: ReasoningSource[];
|
|
93
112
|
metadata: Record<string, any>;
|
|
113
|
+
no_match: boolean;
|
|
114
|
+
abstain_recommended: boolean;
|
|
115
|
+
query_confidence: number;
|
|
116
|
+
grounding: GroundingReceipt;
|
|
117
|
+
evidence_ids: string[];
|
|
118
|
+
evidence_claims?: EvidenceClaim[];
|
|
119
|
+
safety_contract_version: string;
|
|
120
|
+
degraded?: boolean;
|
|
121
|
+
sdk_safety_reason?: string;
|
|
94
122
|
reasoning_context?: Record<string, any>;
|
|
95
123
|
}
|
|
96
124
|
interface CreateCollectionParams {
|
|
@@ -104,17 +132,54 @@ interface UpdateCollectionParams {
|
|
|
104
132
|
metadata?: Record<string, any>;
|
|
105
133
|
}
|
|
106
134
|
interface CreateMemoryParams {
|
|
107
|
-
collection_id
|
|
108
|
-
content
|
|
135
|
+
collection_id?: string;
|
|
136
|
+
content?: string;
|
|
137
|
+
messages?: Array<{
|
|
138
|
+
role: string;
|
|
139
|
+
content: string;
|
|
140
|
+
}>;
|
|
109
141
|
importance?: number;
|
|
110
142
|
source_type?: string;
|
|
111
143
|
source_reference?: string;
|
|
112
144
|
metadata?: Record<string, any>;
|
|
145
|
+
infer?: boolean;
|
|
146
|
+
user_id?: string;
|
|
147
|
+
agent_id?: string;
|
|
148
|
+
run_id?: string;
|
|
149
|
+
app_id?: string;
|
|
150
|
+
namespace?: string;
|
|
151
|
+
wait_for_index?: boolean;
|
|
152
|
+
async_dispatch?: boolean;
|
|
153
|
+
title?: string;
|
|
154
|
+
tags?: string[];
|
|
155
|
+
source?: string;
|
|
156
|
+
/** Stable retry key sent as the Idempotency-Key transport header. */
|
|
157
|
+
idempotency_key?: string;
|
|
158
|
+
}
|
|
159
|
+
interface MemoryAddResult {
|
|
160
|
+
id: string;
|
|
161
|
+
memory_id?: string;
|
|
162
|
+
event: "ADD" | "UPDATE" | "NOOP" | string;
|
|
163
|
+
memory?: string;
|
|
164
|
+
reason?: string;
|
|
165
|
+
}
|
|
166
|
+
interface MemoryAddResponse {
|
|
167
|
+
results: MemoryAddResult[];
|
|
168
|
+
collection_id?: string;
|
|
169
|
+
processing_status: string;
|
|
170
|
+
searchable: boolean;
|
|
171
|
+
outbox_event_id?: string;
|
|
172
|
+
status_url?: string;
|
|
173
|
+
job_id?: string;
|
|
174
|
+
created_count: number;
|
|
175
|
+
updated_count: number;
|
|
176
|
+
skipped_count: number;
|
|
113
177
|
}
|
|
114
178
|
interface UpdateMemoryParams {
|
|
115
179
|
content?: string;
|
|
116
180
|
importance?: number;
|
|
117
181
|
metadata?: Record<string, any>;
|
|
182
|
+
wait_for_index?: boolean;
|
|
118
183
|
}
|
|
119
184
|
interface SearchParams {
|
|
120
185
|
query: string;
|
|
@@ -123,17 +188,81 @@ interface SearchParams {
|
|
|
123
188
|
search_type?: "hybrid" | "vector" | "bm25" | "graph";
|
|
124
189
|
filters?: Record<string, any>;
|
|
125
190
|
user_id?: string;
|
|
191
|
+
agent_id?: string;
|
|
192
|
+
run_id?: string;
|
|
193
|
+
fast?: boolean;
|
|
194
|
+
threshold?: number;
|
|
195
|
+
include_low_confidence?: boolean;
|
|
196
|
+
group_by_source?: boolean;
|
|
197
|
+
debug?: boolean;
|
|
126
198
|
}
|
|
127
199
|
interface ReasonParams {
|
|
128
200
|
query: string;
|
|
129
201
|
collection_id?: string;
|
|
130
202
|
provider?: "gemini" | "openai" | "anthropic";
|
|
131
203
|
include_steps?: boolean;
|
|
204
|
+
user_id?: string;
|
|
205
|
+
agent_id?: string;
|
|
206
|
+
run_id?: string;
|
|
207
|
+
facets?: string[];
|
|
132
208
|
}
|
|
133
209
|
interface ListParams {
|
|
134
210
|
skip?: number;
|
|
135
211
|
limit?: number;
|
|
136
212
|
}
|
|
213
|
+
interface MemoryListParams {
|
|
214
|
+
collection_id?: string;
|
|
215
|
+
user_id?: string;
|
|
216
|
+
agent_id?: string;
|
|
217
|
+
run_id?: string;
|
|
218
|
+
scope?: "all";
|
|
219
|
+
cursor?: string;
|
|
220
|
+
limit?: number;
|
|
221
|
+
include_superseded?: boolean;
|
|
222
|
+
}
|
|
223
|
+
interface CursorPage<T> {
|
|
224
|
+
items: T[];
|
|
225
|
+
next_cursor?: string | null;
|
|
226
|
+
has_more: boolean;
|
|
227
|
+
total_count: number;
|
|
228
|
+
}
|
|
229
|
+
interface MemoryJobReceipt {
|
|
230
|
+
job_id?: string;
|
|
231
|
+
status: string;
|
|
232
|
+
[key: string]: any;
|
|
233
|
+
}
|
|
234
|
+
interface CorrectionCreateParams {
|
|
235
|
+
corrected_content: string;
|
|
236
|
+
correction_type?: "preference" | "factual" | "procedural" | string;
|
|
237
|
+
original_content?: string;
|
|
238
|
+
context?: string;
|
|
239
|
+
memory_id?: string;
|
|
240
|
+
collection_id?: string;
|
|
241
|
+
user_id?: string;
|
|
242
|
+
agent_id?: string;
|
|
243
|
+
confidence?: number;
|
|
244
|
+
metadata?: Record<string, any>;
|
|
245
|
+
idempotency_key?: string;
|
|
246
|
+
}
|
|
247
|
+
interface CorrectionSearchParams {
|
|
248
|
+
query: string;
|
|
249
|
+
correction_type?: string;
|
|
250
|
+
collection_id?: string;
|
|
251
|
+
user_id?: string;
|
|
252
|
+
agent_id?: string;
|
|
253
|
+
include_global?: boolean;
|
|
254
|
+
limit?: number;
|
|
255
|
+
}
|
|
256
|
+
interface ProofLoopMetricParams {
|
|
257
|
+
policy_key: string;
|
|
258
|
+
metric_key: string;
|
|
259
|
+
name: string;
|
|
260
|
+
min_value: number;
|
|
261
|
+
max_value: number;
|
|
262
|
+
collection_id?: string;
|
|
263
|
+
user_id?: string;
|
|
264
|
+
[key: string]: any;
|
|
265
|
+
}
|
|
137
266
|
interface APIKeyResponse {
|
|
138
267
|
id: string;
|
|
139
268
|
name: string;
|
|
@@ -194,13 +323,13 @@ declare class MemoriesResource extends BaseResource {
|
|
|
194
323
|
/**
|
|
195
324
|
* Create a new memory
|
|
196
325
|
*/
|
|
197
|
-
create(params: CreateMemoryParams): Promise<
|
|
326
|
+
create(params: CreateMemoryParams): Promise<MemoryAddResponse>;
|
|
198
327
|
/**
|
|
199
328
|
* List memories
|
|
200
329
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
330
|
+
listPage(params?: MemoryListParams): Promise<CursorPage<Memory>>;
|
|
331
|
+
/** Back-compatible one-page convenience; use listPage for cursor metadata. */
|
|
332
|
+
list(params?: MemoryListParams): Promise<Memory[]>;
|
|
204
333
|
/**
|
|
205
334
|
* Get a specific memory
|
|
206
335
|
*/
|
|
@@ -214,6 +343,19 @@ declare class MemoriesResource extends BaseResource {
|
|
|
214
343
|
*/
|
|
215
344
|
delete(memoryId: string): Promise<void>;
|
|
216
345
|
}
|
|
346
|
+
declare class MemoryJobsResource extends BaseResource {
|
|
347
|
+
get(jobId: string): Promise<MemoryJobReceipt>;
|
|
348
|
+
wait(jobId: string, options?: {
|
|
349
|
+
timeoutMs?: number;
|
|
350
|
+
pollIntervalMs?: number;
|
|
351
|
+
}): Promise<MemoryJobReceipt>;
|
|
352
|
+
}
|
|
353
|
+
declare class CorrectionsResource extends BaseResource {
|
|
354
|
+
create(params: CorrectionCreateParams): Promise<Record<string, any>>;
|
|
355
|
+
relevant(params: CorrectionSearchParams): Promise<Array<Record<string, any>>>;
|
|
356
|
+
get(correctionId: string): Promise<Record<string, any>>;
|
|
357
|
+
delete(correctionId: string): Promise<Record<string, any>>;
|
|
358
|
+
}
|
|
217
359
|
declare class SearchResource extends BaseResource {
|
|
218
360
|
/**
|
|
219
361
|
* Search memories
|
|
@@ -239,8 +381,26 @@ declare class ProofLoopResource extends BaseResource {
|
|
|
239
381
|
success?: boolean;
|
|
240
382
|
idempotency_key?: string;
|
|
241
383
|
}): Promise<Record<string, any>>;
|
|
384
|
+
getDecision(decisionId: string): Promise<Record<string, any>>;
|
|
385
|
+
defineMetric(params: ProofLoopMetricParams): Promise<Record<string, any>>;
|
|
386
|
+
listMetrics(params?: {
|
|
387
|
+
policy_key?: string;
|
|
388
|
+
collection_id?: string;
|
|
389
|
+
user_id?: string;
|
|
390
|
+
}): Promise<Record<string, any>>;
|
|
391
|
+
policyInsights(policyKey: string, params?: {
|
|
392
|
+
collection_id?: string;
|
|
393
|
+
user_id?: string;
|
|
394
|
+
action_keys?: string[];
|
|
395
|
+
context?: Record<string, any>;
|
|
396
|
+
}): Promise<Record<string, any>>;
|
|
397
|
+
evaluatePolicy(policyKey: string, params?: {
|
|
398
|
+
collection_id?: string;
|
|
399
|
+
user_id?: string;
|
|
400
|
+
limit?: number;
|
|
401
|
+
}): Promise<Record<string, any>>;
|
|
242
402
|
proof(decisionId: string): Promise<Record<string, any>>;
|
|
243
|
-
publicKey(): Promise<Record<string, any>>;
|
|
403
|
+
publicKey(keyId?: string): Promise<Record<string, any>>;
|
|
244
404
|
}
|
|
245
405
|
declare class RLResource extends BaseResource {
|
|
246
406
|
/**
|
|
@@ -418,6 +578,8 @@ declare class MemoryClient {
|
|
|
418
578
|
auth: AuthResource;
|
|
419
579
|
collections: CollectionsResource;
|
|
420
580
|
memories: MemoriesResource;
|
|
581
|
+
memoryJobs: MemoryJobsResource;
|
|
582
|
+
corrections: CorrectionsResource;
|
|
421
583
|
private searchResource;
|
|
422
584
|
rl: RLResource;
|
|
423
585
|
procedural: ProceduralResource;
|
|
@@ -464,4 +626,24 @@ declare class ServerError extends HebbrixError {
|
|
|
464
626
|
constructor(message?: string);
|
|
465
627
|
}
|
|
466
628
|
|
|
467
|
-
|
|
629
|
+
interface SafetyEnvelope {
|
|
630
|
+
no_match: boolean;
|
|
631
|
+
abstain_recommended: boolean;
|
|
632
|
+
query_confidence: number;
|
|
633
|
+
grounding: GroundingReceipt;
|
|
634
|
+
evidence_ids: string[];
|
|
635
|
+
evidence_claims?: unknown[];
|
|
636
|
+
safety_contract_version: string;
|
|
637
|
+
degraded?: boolean;
|
|
638
|
+
sdk_safety_reason?: string;
|
|
639
|
+
[key: string]: unknown;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Validate the API-owned grounding receipt before exposing evidence to an agent.
|
|
643
|
+
* A malformed, degraded, abstaining, or ungrounded response is converted into
|
|
644
|
+
* one deterministic no-match envelope. This prevents SDK/API contract drift
|
|
645
|
+
* from turning a nearest neighbour into verified evidence.
|
|
646
|
+
*/
|
|
647
|
+
declare function enforceSearchSafety<T extends object>(response: T, rowsKey?: "results" | "sources"): T & SafetyEnvelope;
|
|
648
|
+
|
|
649
|
+
export { type APIKeyResponse, AuthResource, type AuthResponse, AuthenticationError, type ClientConfig, type Collection, CollectionsResource, ConsolidationResource, type CorrectionCreateParams, type CorrectionSearchParams, CorrectionsResource, type CreateCollectionParams, type CreateMemoryParams, type CursorPage, type EvidenceClaim, type GroundingReceipt, HebbrixError, type ListParams, MemoriesResource, type Memory, type MemoryAddResponse, type MemoryAddResult, MemoryClient, type MemoryJobReceipt, MemoryJobsResource, type MemoryListParams, MemoryToolsResource, type MemoryWithMetadata, NotFoundError, ProceduralResource, type ProofContext, type ProofLoopCandidate, type ProofLoopDecisionParams, type ProofLoopMetricParams, ProofLoopResource, RLResource, RateLimitError, type ReasonParams, type ReasoningResponse, type ReasoningSource, type SafetyEnvelope, type SearchParams, SearchResource, type SearchResponse, type SearchResult, ServerError, TemporalResource, type UpdateCollectionParams, type UpdateMemoryParams, type User, ValidationError, WorkingMemoryResource, WorldModelResource, enforceSearchSafety };
|
package/dist/index.d.ts
CHANGED
|
@@ -57,8 +57,27 @@ interface SearchResponse {
|
|
|
57
57
|
total: number;
|
|
58
58
|
search_type: string;
|
|
59
59
|
processing_time_ms: number;
|
|
60
|
+
no_match: boolean;
|
|
61
|
+
abstain_recommended: boolean;
|
|
62
|
+
query_confidence: number;
|
|
63
|
+
grounding: GroundingReceipt;
|
|
64
|
+
evidence_ids: string[];
|
|
65
|
+
evidence_claims?: EvidenceClaim[];
|
|
66
|
+
safety_contract_version: string;
|
|
67
|
+
degraded?: boolean;
|
|
68
|
+
sdk_safety_reason?: string;
|
|
60
69
|
proof_context?: ProofContext;
|
|
61
70
|
}
|
|
71
|
+
interface GroundingReceipt {
|
|
72
|
+
status: string;
|
|
73
|
+
reason?: string;
|
|
74
|
+
contract_version?: string;
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
}
|
|
77
|
+
interface EvidenceClaim {
|
|
78
|
+
memory_id: string;
|
|
79
|
+
claims: Record<string, unknown>;
|
|
80
|
+
}
|
|
62
81
|
interface ProofContext {
|
|
63
82
|
schema_version: "proofloop-context-v1";
|
|
64
83
|
context_id: string;
|
|
@@ -88,9 +107,18 @@ interface ReasoningSource {
|
|
|
88
107
|
score: number;
|
|
89
108
|
}
|
|
90
109
|
interface ReasoningResponse {
|
|
91
|
-
answer: string;
|
|
110
|
+
answer: string | null;
|
|
92
111
|
sources: ReasoningSource[];
|
|
93
112
|
metadata: Record<string, any>;
|
|
113
|
+
no_match: boolean;
|
|
114
|
+
abstain_recommended: boolean;
|
|
115
|
+
query_confidence: number;
|
|
116
|
+
grounding: GroundingReceipt;
|
|
117
|
+
evidence_ids: string[];
|
|
118
|
+
evidence_claims?: EvidenceClaim[];
|
|
119
|
+
safety_contract_version: string;
|
|
120
|
+
degraded?: boolean;
|
|
121
|
+
sdk_safety_reason?: string;
|
|
94
122
|
reasoning_context?: Record<string, any>;
|
|
95
123
|
}
|
|
96
124
|
interface CreateCollectionParams {
|
|
@@ -104,17 +132,54 @@ interface UpdateCollectionParams {
|
|
|
104
132
|
metadata?: Record<string, any>;
|
|
105
133
|
}
|
|
106
134
|
interface CreateMemoryParams {
|
|
107
|
-
collection_id
|
|
108
|
-
content
|
|
135
|
+
collection_id?: string;
|
|
136
|
+
content?: string;
|
|
137
|
+
messages?: Array<{
|
|
138
|
+
role: string;
|
|
139
|
+
content: string;
|
|
140
|
+
}>;
|
|
109
141
|
importance?: number;
|
|
110
142
|
source_type?: string;
|
|
111
143
|
source_reference?: string;
|
|
112
144
|
metadata?: Record<string, any>;
|
|
145
|
+
infer?: boolean;
|
|
146
|
+
user_id?: string;
|
|
147
|
+
agent_id?: string;
|
|
148
|
+
run_id?: string;
|
|
149
|
+
app_id?: string;
|
|
150
|
+
namespace?: string;
|
|
151
|
+
wait_for_index?: boolean;
|
|
152
|
+
async_dispatch?: boolean;
|
|
153
|
+
title?: string;
|
|
154
|
+
tags?: string[];
|
|
155
|
+
source?: string;
|
|
156
|
+
/** Stable retry key sent as the Idempotency-Key transport header. */
|
|
157
|
+
idempotency_key?: string;
|
|
158
|
+
}
|
|
159
|
+
interface MemoryAddResult {
|
|
160
|
+
id: string;
|
|
161
|
+
memory_id?: string;
|
|
162
|
+
event: "ADD" | "UPDATE" | "NOOP" | string;
|
|
163
|
+
memory?: string;
|
|
164
|
+
reason?: string;
|
|
165
|
+
}
|
|
166
|
+
interface MemoryAddResponse {
|
|
167
|
+
results: MemoryAddResult[];
|
|
168
|
+
collection_id?: string;
|
|
169
|
+
processing_status: string;
|
|
170
|
+
searchable: boolean;
|
|
171
|
+
outbox_event_id?: string;
|
|
172
|
+
status_url?: string;
|
|
173
|
+
job_id?: string;
|
|
174
|
+
created_count: number;
|
|
175
|
+
updated_count: number;
|
|
176
|
+
skipped_count: number;
|
|
113
177
|
}
|
|
114
178
|
interface UpdateMemoryParams {
|
|
115
179
|
content?: string;
|
|
116
180
|
importance?: number;
|
|
117
181
|
metadata?: Record<string, any>;
|
|
182
|
+
wait_for_index?: boolean;
|
|
118
183
|
}
|
|
119
184
|
interface SearchParams {
|
|
120
185
|
query: string;
|
|
@@ -123,17 +188,81 @@ interface SearchParams {
|
|
|
123
188
|
search_type?: "hybrid" | "vector" | "bm25" | "graph";
|
|
124
189
|
filters?: Record<string, any>;
|
|
125
190
|
user_id?: string;
|
|
191
|
+
agent_id?: string;
|
|
192
|
+
run_id?: string;
|
|
193
|
+
fast?: boolean;
|
|
194
|
+
threshold?: number;
|
|
195
|
+
include_low_confidence?: boolean;
|
|
196
|
+
group_by_source?: boolean;
|
|
197
|
+
debug?: boolean;
|
|
126
198
|
}
|
|
127
199
|
interface ReasonParams {
|
|
128
200
|
query: string;
|
|
129
201
|
collection_id?: string;
|
|
130
202
|
provider?: "gemini" | "openai" | "anthropic";
|
|
131
203
|
include_steps?: boolean;
|
|
204
|
+
user_id?: string;
|
|
205
|
+
agent_id?: string;
|
|
206
|
+
run_id?: string;
|
|
207
|
+
facets?: string[];
|
|
132
208
|
}
|
|
133
209
|
interface ListParams {
|
|
134
210
|
skip?: number;
|
|
135
211
|
limit?: number;
|
|
136
212
|
}
|
|
213
|
+
interface MemoryListParams {
|
|
214
|
+
collection_id?: string;
|
|
215
|
+
user_id?: string;
|
|
216
|
+
agent_id?: string;
|
|
217
|
+
run_id?: string;
|
|
218
|
+
scope?: "all";
|
|
219
|
+
cursor?: string;
|
|
220
|
+
limit?: number;
|
|
221
|
+
include_superseded?: boolean;
|
|
222
|
+
}
|
|
223
|
+
interface CursorPage<T> {
|
|
224
|
+
items: T[];
|
|
225
|
+
next_cursor?: string | null;
|
|
226
|
+
has_more: boolean;
|
|
227
|
+
total_count: number;
|
|
228
|
+
}
|
|
229
|
+
interface MemoryJobReceipt {
|
|
230
|
+
job_id?: string;
|
|
231
|
+
status: string;
|
|
232
|
+
[key: string]: any;
|
|
233
|
+
}
|
|
234
|
+
interface CorrectionCreateParams {
|
|
235
|
+
corrected_content: string;
|
|
236
|
+
correction_type?: "preference" | "factual" | "procedural" | string;
|
|
237
|
+
original_content?: string;
|
|
238
|
+
context?: string;
|
|
239
|
+
memory_id?: string;
|
|
240
|
+
collection_id?: string;
|
|
241
|
+
user_id?: string;
|
|
242
|
+
agent_id?: string;
|
|
243
|
+
confidence?: number;
|
|
244
|
+
metadata?: Record<string, any>;
|
|
245
|
+
idempotency_key?: string;
|
|
246
|
+
}
|
|
247
|
+
interface CorrectionSearchParams {
|
|
248
|
+
query: string;
|
|
249
|
+
correction_type?: string;
|
|
250
|
+
collection_id?: string;
|
|
251
|
+
user_id?: string;
|
|
252
|
+
agent_id?: string;
|
|
253
|
+
include_global?: boolean;
|
|
254
|
+
limit?: number;
|
|
255
|
+
}
|
|
256
|
+
interface ProofLoopMetricParams {
|
|
257
|
+
policy_key: string;
|
|
258
|
+
metric_key: string;
|
|
259
|
+
name: string;
|
|
260
|
+
min_value: number;
|
|
261
|
+
max_value: number;
|
|
262
|
+
collection_id?: string;
|
|
263
|
+
user_id?: string;
|
|
264
|
+
[key: string]: any;
|
|
265
|
+
}
|
|
137
266
|
interface APIKeyResponse {
|
|
138
267
|
id: string;
|
|
139
268
|
name: string;
|
|
@@ -194,13 +323,13 @@ declare class MemoriesResource extends BaseResource {
|
|
|
194
323
|
/**
|
|
195
324
|
* Create a new memory
|
|
196
325
|
*/
|
|
197
|
-
create(params: CreateMemoryParams): Promise<
|
|
326
|
+
create(params: CreateMemoryParams): Promise<MemoryAddResponse>;
|
|
198
327
|
/**
|
|
199
328
|
* List memories
|
|
200
329
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
330
|
+
listPage(params?: MemoryListParams): Promise<CursorPage<Memory>>;
|
|
331
|
+
/** Back-compatible one-page convenience; use listPage for cursor metadata. */
|
|
332
|
+
list(params?: MemoryListParams): Promise<Memory[]>;
|
|
204
333
|
/**
|
|
205
334
|
* Get a specific memory
|
|
206
335
|
*/
|
|
@@ -214,6 +343,19 @@ declare class MemoriesResource extends BaseResource {
|
|
|
214
343
|
*/
|
|
215
344
|
delete(memoryId: string): Promise<void>;
|
|
216
345
|
}
|
|
346
|
+
declare class MemoryJobsResource extends BaseResource {
|
|
347
|
+
get(jobId: string): Promise<MemoryJobReceipt>;
|
|
348
|
+
wait(jobId: string, options?: {
|
|
349
|
+
timeoutMs?: number;
|
|
350
|
+
pollIntervalMs?: number;
|
|
351
|
+
}): Promise<MemoryJobReceipt>;
|
|
352
|
+
}
|
|
353
|
+
declare class CorrectionsResource extends BaseResource {
|
|
354
|
+
create(params: CorrectionCreateParams): Promise<Record<string, any>>;
|
|
355
|
+
relevant(params: CorrectionSearchParams): Promise<Array<Record<string, any>>>;
|
|
356
|
+
get(correctionId: string): Promise<Record<string, any>>;
|
|
357
|
+
delete(correctionId: string): Promise<Record<string, any>>;
|
|
358
|
+
}
|
|
217
359
|
declare class SearchResource extends BaseResource {
|
|
218
360
|
/**
|
|
219
361
|
* Search memories
|
|
@@ -239,8 +381,26 @@ declare class ProofLoopResource extends BaseResource {
|
|
|
239
381
|
success?: boolean;
|
|
240
382
|
idempotency_key?: string;
|
|
241
383
|
}): Promise<Record<string, any>>;
|
|
384
|
+
getDecision(decisionId: string): Promise<Record<string, any>>;
|
|
385
|
+
defineMetric(params: ProofLoopMetricParams): Promise<Record<string, any>>;
|
|
386
|
+
listMetrics(params?: {
|
|
387
|
+
policy_key?: string;
|
|
388
|
+
collection_id?: string;
|
|
389
|
+
user_id?: string;
|
|
390
|
+
}): Promise<Record<string, any>>;
|
|
391
|
+
policyInsights(policyKey: string, params?: {
|
|
392
|
+
collection_id?: string;
|
|
393
|
+
user_id?: string;
|
|
394
|
+
action_keys?: string[];
|
|
395
|
+
context?: Record<string, any>;
|
|
396
|
+
}): Promise<Record<string, any>>;
|
|
397
|
+
evaluatePolicy(policyKey: string, params?: {
|
|
398
|
+
collection_id?: string;
|
|
399
|
+
user_id?: string;
|
|
400
|
+
limit?: number;
|
|
401
|
+
}): Promise<Record<string, any>>;
|
|
242
402
|
proof(decisionId: string): Promise<Record<string, any>>;
|
|
243
|
-
publicKey(): Promise<Record<string, any>>;
|
|
403
|
+
publicKey(keyId?: string): Promise<Record<string, any>>;
|
|
244
404
|
}
|
|
245
405
|
declare class RLResource extends BaseResource {
|
|
246
406
|
/**
|
|
@@ -418,6 +578,8 @@ declare class MemoryClient {
|
|
|
418
578
|
auth: AuthResource;
|
|
419
579
|
collections: CollectionsResource;
|
|
420
580
|
memories: MemoriesResource;
|
|
581
|
+
memoryJobs: MemoryJobsResource;
|
|
582
|
+
corrections: CorrectionsResource;
|
|
421
583
|
private searchResource;
|
|
422
584
|
rl: RLResource;
|
|
423
585
|
procedural: ProceduralResource;
|
|
@@ -464,4 +626,24 @@ declare class ServerError extends HebbrixError {
|
|
|
464
626
|
constructor(message?: string);
|
|
465
627
|
}
|
|
466
628
|
|
|
467
|
-
|
|
629
|
+
interface SafetyEnvelope {
|
|
630
|
+
no_match: boolean;
|
|
631
|
+
abstain_recommended: boolean;
|
|
632
|
+
query_confidence: number;
|
|
633
|
+
grounding: GroundingReceipt;
|
|
634
|
+
evidence_ids: string[];
|
|
635
|
+
evidence_claims?: unknown[];
|
|
636
|
+
safety_contract_version: string;
|
|
637
|
+
degraded?: boolean;
|
|
638
|
+
sdk_safety_reason?: string;
|
|
639
|
+
[key: string]: unknown;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Validate the API-owned grounding receipt before exposing evidence to an agent.
|
|
643
|
+
* A malformed, degraded, abstaining, or ungrounded response is converted into
|
|
644
|
+
* one deterministic no-match envelope. This prevents SDK/API contract drift
|
|
645
|
+
* from turning a nearest neighbour into verified evidence.
|
|
646
|
+
*/
|
|
647
|
+
declare function enforceSearchSafety<T extends object>(response: T, rowsKey?: "results" | "sources"): T & SafetyEnvelope;
|
|
648
|
+
|
|
649
|
+
export { type APIKeyResponse, AuthResource, type AuthResponse, AuthenticationError, type ClientConfig, type Collection, CollectionsResource, ConsolidationResource, type CorrectionCreateParams, type CorrectionSearchParams, CorrectionsResource, type CreateCollectionParams, type CreateMemoryParams, type CursorPage, type EvidenceClaim, type GroundingReceipt, HebbrixError, type ListParams, MemoriesResource, type Memory, type MemoryAddResponse, type MemoryAddResult, MemoryClient, type MemoryJobReceipt, MemoryJobsResource, type MemoryListParams, MemoryToolsResource, type MemoryWithMetadata, NotFoundError, ProceduralResource, type ProofContext, type ProofLoopCandidate, type ProofLoopDecisionParams, type ProofLoopMetricParams, ProofLoopResource, RLResource, RateLimitError, type ReasonParams, type ReasoningResponse, type ReasoningSource, type SafetyEnvelope, type SearchParams, SearchResource, type SearchResponse, type SearchResult, ServerError, TemporalResource, type UpdateCollectionParams, type UpdateMemoryParams, type User, ValidationError, WorkingMemoryResource, WorldModelResource, enforceSearchSafety };
|
package/dist/index.js
CHANGED
|
@@ -24,9 +24,11 @@ __export(index_exports, {
|
|
|
24
24
|
AuthenticationError: () => AuthenticationError,
|
|
25
25
|
CollectionsResource: () => CollectionsResource,
|
|
26
26
|
ConsolidationResource: () => ConsolidationResource,
|
|
27
|
+
CorrectionsResource: () => CorrectionsResource,
|
|
27
28
|
HebbrixError: () => HebbrixError,
|
|
28
29
|
MemoriesResource: () => MemoriesResource,
|
|
29
30
|
MemoryClient: () => MemoryClient,
|
|
31
|
+
MemoryJobsResource: () => MemoryJobsResource,
|
|
30
32
|
MemoryToolsResource: () => MemoryToolsResource,
|
|
31
33
|
NotFoundError: () => NotFoundError,
|
|
32
34
|
ProceduralResource: () => ProceduralResource,
|
|
@@ -38,10 +40,63 @@ __export(index_exports, {
|
|
|
38
40
|
TemporalResource: () => TemporalResource,
|
|
39
41
|
ValidationError: () => ValidationError,
|
|
40
42
|
WorkingMemoryResource: () => WorkingMemoryResource,
|
|
41
|
-
WorldModelResource: () => WorldModelResource
|
|
43
|
+
WorldModelResource: () => WorldModelResource,
|
|
44
|
+
enforceSearchSafety: () => enforceSearchSafety
|
|
42
45
|
});
|
|
43
46
|
module.exports = __toCommonJS(index_exports);
|
|
44
47
|
|
|
48
|
+
// src/safety.ts
|
|
49
|
+
var REQUIRED_FIELDS = [
|
|
50
|
+
"no_match",
|
|
51
|
+
"abstain_recommended",
|
|
52
|
+
"query_confidence",
|
|
53
|
+
"grounding",
|
|
54
|
+
"evidence_ids",
|
|
55
|
+
"safety_contract_version"
|
|
56
|
+
];
|
|
57
|
+
function enforceSearchSafety(response, rowsKey = "results") {
|
|
58
|
+
const data = { ...response };
|
|
59
|
+
const rawRows = data[rowsKey];
|
|
60
|
+
const rows = Array.isArray(rawRows) ? rawRows.filter((row) => typeof row === "object" && row !== null) : [];
|
|
61
|
+
const missing = REQUIRED_FIELDS.filter(
|
|
62
|
+
(field) => !Object.prototype.hasOwnProperty.call(data, field)
|
|
63
|
+
);
|
|
64
|
+
let reason;
|
|
65
|
+
if (missing.length > 0) {
|
|
66
|
+
reason = `missing_safety_fields:${missing.join(",")}`;
|
|
67
|
+
} else if (typeof data.no_match !== "boolean" || typeof data.abstain_recommended !== "boolean") {
|
|
68
|
+
reason = "invalid_abstention_fields";
|
|
69
|
+
} else if (typeof data.query_confidence !== "number" || !Number.isFinite(data.query_confidence) || data.query_confidence < 0 || data.query_confidence > 1) {
|
|
70
|
+
reason = "invalid_query_confidence";
|
|
71
|
+
} else if (typeof data.grounding !== "object" || data.grounding === null || Array.isArray(data.grounding)) {
|
|
72
|
+
reason = "invalid_grounding_receipt";
|
|
73
|
+
} else if (!Array.isArray(data.evidence_ids)) {
|
|
74
|
+
reason = "invalid_evidence_ids";
|
|
75
|
+
} else {
|
|
76
|
+
const evidenceIds = new Set(data.evidence_ids.map(String));
|
|
77
|
+
const rowIds = rows.map((row) => row.memory_id ?? row.id).filter((value) => typeof value === "string" && value.length > 0);
|
|
78
|
+
if (rowIds.some((value) => !evidenceIds.has(value))) {
|
|
79
|
+
reason = "rows_not_bound_to_evidence_ids";
|
|
80
|
+
} else if (data.no_match && (rowIds.length > 0 || evidenceIds.size > 0)) {
|
|
81
|
+
reason = "no_match_contains_evidence";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (reason || data.degraded === true || data.no_match === true || data.abstain_recommended === true) {
|
|
85
|
+
data[rowsKey] = [];
|
|
86
|
+
if (rowsKey === "results") data.total = 0;
|
|
87
|
+
data.no_match = true;
|
|
88
|
+
data.abstain_recommended = true;
|
|
89
|
+
data.query_confidence = 0;
|
|
90
|
+
data.evidence_ids = [];
|
|
91
|
+
data.evidence_claims = [];
|
|
92
|
+
if (reason) {
|
|
93
|
+
data.sdk_safety_reason = reason;
|
|
94
|
+
data.grounding = { status: "no_grounded_match", reason };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return data;
|
|
98
|
+
}
|
|
99
|
+
|
|
45
100
|
// src/resources.ts
|
|
46
101
|
var BaseResource = class {
|
|
47
102
|
constructor(client) {
|
|
@@ -121,14 +176,32 @@ var MemoriesResource = class extends BaseResource {
|
|
|
121
176
|
* Create a new memory
|
|
122
177
|
*/
|
|
123
178
|
async create(params) {
|
|
124
|
-
|
|
179
|
+
if (!params.content?.trim() && !params.messages?.length) {
|
|
180
|
+
throw new TypeError("content or messages must be provided");
|
|
181
|
+
}
|
|
182
|
+
const { idempotency_key, ...input } = params;
|
|
183
|
+
return this.client.request("POST", "/v1/memories", {
|
|
184
|
+
headers: idempotency_key ? { "Idempotency-Key": idempotency_key } : void 0,
|
|
185
|
+
body: JSON.stringify({
|
|
186
|
+
source_type: "text",
|
|
187
|
+
metadata: {},
|
|
188
|
+
infer: false,
|
|
189
|
+
wait_for_index: false,
|
|
190
|
+
...input
|
|
191
|
+
})
|
|
192
|
+
});
|
|
125
193
|
}
|
|
126
194
|
/**
|
|
127
195
|
* List memories
|
|
128
196
|
*/
|
|
129
|
-
async
|
|
197
|
+
async listPage(params = {}) {
|
|
130
198
|
return this.client.get("/v1/memories", params);
|
|
131
199
|
}
|
|
200
|
+
/** Back-compatible one-page convenience; use listPage for cursor metadata. */
|
|
201
|
+
async list(params = {}) {
|
|
202
|
+
const page = await this.listPage(params);
|
|
203
|
+
return page.items;
|
|
204
|
+
}
|
|
132
205
|
/**
|
|
133
206
|
* Get a specific memory
|
|
134
207
|
*/
|
|
@@ -148,30 +221,79 @@ var MemoriesResource = class extends BaseResource {
|
|
|
148
221
|
await this.client.delete(`/v1/memories/${memoryId}`);
|
|
149
222
|
}
|
|
150
223
|
};
|
|
224
|
+
var MemoryJobsResource = class extends BaseResource {
|
|
225
|
+
async get(jobId) {
|
|
226
|
+
return this.client.get(`/v1/memory-jobs/${jobId}`);
|
|
227
|
+
}
|
|
228
|
+
async wait(jobId, options = {}) {
|
|
229
|
+
const timeoutMs = Math.max(0, options.timeoutMs ?? 6e4);
|
|
230
|
+
const pollIntervalMs = Math.max(50, options.pollIntervalMs ?? 500);
|
|
231
|
+
const deadline = Date.now() + timeoutMs;
|
|
232
|
+
while (true) {
|
|
233
|
+
const receipt = await this.get(jobId);
|
|
234
|
+
const status = String(receipt.status || "").toLowerCase();
|
|
235
|
+
if (["completed", "failed", "cancelled", "canceled"].includes(status)) {
|
|
236
|
+
return receipt;
|
|
237
|
+
}
|
|
238
|
+
if (Date.now() >= deadline) {
|
|
239
|
+
throw new Error(`memory job ${jobId} did not finish in ${timeoutMs}ms`);
|
|
240
|
+
}
|
|
241
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
var CorrectionsResource = class extends BaseResource {
|
|
246
|
+
async create(params) {
|
|
247
|
+
const { idempotency_key, ...body } = params;
|
|
248
|
+
return this.client.request("POST", "/v1/corrections", {
|
|
249
|
+
headers: idempotency_key ? { "Idempotency-Key": idempotency_key } : void 0,
|
|
250
|
+
body: JSON.stringify({
|
|
251
|
+
correction_type: "preference",
|
|
252
|
+
confidence: 1,
|
|
253
|
+
...body
|
|
254
|
+
})
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
async relevant(params) {
|
|
258
|
+
return this.client.get("/v1/corrections/relevant", {
|
|
259
|
+
include_global: false,
|
|
260
|
+
limit: 10,
|
|
261
|
+
...params
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async get(correctionId) {
|
|
265
|
+
return this.client.get(`/v1/corrections/${correctionId}`);
|
|
266
|
+
}
|
|
267
|
+
async delete(correctionId) {
|
|
268
|
+
return this.client.delete(`/v1/corrections/${correctionId}`);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
151
271
|
var SearchResource = class extends BaseResource {
|
|
152
272
|
/**
|
|
153
273
|
* Search memories
|
|
154
274
|
*/
|
|
155
275
|
async search(params) {
|
|
156
|
-
const response = await this.
|
|
157
|
-
query: params.query,
|
|
158
|
-
collection_id: params.collection_id,
|
|
159
|
-
limit: params.limit || 10,
|
|
160
|
-
search_type: params.search_type || "hybrid",
|
|
161
|
-
filters: params.filters || {}
|
|
162
|
-
});
|
|
276
|
+
const response = await this.searchWithProof(params);
|
|
163
277
|
return response.results;
|
|
164
278
|
}
|
|
165
279
|
/** Search while preserving the automatic ProofLoop evidence context. */
|
|
166
280
|
async searchWithProof(params) {
|
|
167
|
-
|
|
281
|
+
const response = await this.client.post("/v1/search", {
|
|
168
282
|
query: params.query,
|
|
169
283
|
collection_id: params.collection_id,
|
|
170
284
|
user_id: params.user_id,
|
|
285
|
+
agent_id: params.agent_id,
|
|
286
|
+
run_id: params.run_id,
|
|
171
287
|
limit: params.limit || 10,
|
|
172
288
|
search_type: params.search_type || "hybrid",
|
|
173
|
-
filters: params.filters || {}
|
|
289
|
+
filters: params.filters || {},
|
|
290
|
+
fast: params.fast,
|
|
291
|
+
threshold: params.threshold,
|
|
292
|
+
include_low_confidence: params.include_low_confidence ?? false,
|
|
293
|
+
group_by_source: params.group_by_source ?? true,
|
|
294
|
+
debug: params.debug ?? false
|
|
174
295
|
});
|
|
296
|
+
return enforceSearchSafety(response);
|
|
175
297
|
}
|
|
176
298
|
/**
|
|
177
299
|
* Find similar memories
|
|
@@ -187,12 +309,20 @@ var SearchResource = class extends BaseResource {
|
|
|
187
309
|
* Perform reasoning over memories
|
|
188
310
|
*/
|
|
189
311
|
async reason(params) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
312
|
+
const response = await this.client.post(
|
|
313
|
+
"/v1/search/reason",
|
|
314
|
+
{
|
|
315
|
+
query: params.query,
|
|
316
|
+
collection_id: params.collection_id,
|
|
317
|
+
provider: params.provider,
|
|
318
|
+
include_steps: params.include_steps ?? false,
|
|
319
|
+
user_id: params.user_id,
|
|
320
|
+
agent_id: params.agent_id,
|
|
321
|
+
run_id: params.run_id,
|
|
322
|
+
facets: params.facets ?? []
|
|
323
|
+
}
|
|
324
|
+
);
|
|
325
|
+
return enforceSearchSafety(response, "sources");
|
|
196
326
|
}
|
|
197
327
|
};
|
|
198
328
|
var ProofLoopResource = class extends BaseResource {
|
|
@@ -212,11 +342,37 @@ var ProofLoopResource = class extends BaseResource {
|
|
|
212
342
|
...body
|
|
213
343
|
});
|
|
214
344
|
}
|
|
345
|
+
async getDecision(decisionId) {
|
|
346
|
+
return this.client.get(`/v1/learning/decisions/${decisionId}`);
|
|
347
|
+
}
|
|
348
|
+
async defineMetric(params) {
|
|
349
|
+
return this.client.post("/v1/learning/metrics", params);
|
|
350
|
+
}
|
|
351
|
+
async listMetrics(params = {}) {
|
|
352
|
+
return this.client.get("/v1/learning/metrics", params);
|
|
353
|
+
}
|
|
354
|
+
async policyInsights(policyKey, params = {}) {
|
|
355
|
+
return this.client.get(`/v1/learning/policies/${policyKey}/insights`, {
|
|
356
|
+
collection_id: params.collection_id,
|
|
357
|
+
user_id: params.user_id,
|
|
358
|
+
action_key: params.action_keys,
|
|
359
|
+
context: params.context ? JSON.stringify(params.context) : void 0
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
async evaluatePolicy(policyKey, params = {}) {
|
|
363
|
+
return this.client.post(`/v1/learning/policies/${policyKey}/evaluate`, {
|
|
364
|
+
collection_id: params.collection_id,
|
|
365
|
+
user_id: params.user_id,
|
|
366
|
+
limit: params.limit ?? 500
|
|
367
|
+
});
|
|
368
|
+
}
|
|
215
369
|
async proof(decisionId) {
|
|
216
370
|
return this.client.get(`/v1/learning/decisions/${decisionId}/proof`);
|
|
217
371
|
}
|
|
218
|
-
async publicKey() {
|
|
219
|
-
return this.client.get("/v1/learning/proof-key"
|
|
372
|
+
async publicKey(keyId) {
|
|
373
|
+
return this.client.get("/v1/learning/proof-key", {
|
|
374
|
+
key_id: keyId
|
|
375
|
+
});
|
|
220
376
|
}
|
|
221
377
|
};
|
|
222
378
|
var RLResource = class extends BaseResource {
|
|
@@ -513,6 +669,8 @@ var MemoryClient = class {
|
|
|
513
669
|
this.auth = new AuthResource(this);
|
|
514
670
|
this.collections = new CollectionsResource(this);
|
|
515
671
|
this.memories = new MemoriesResource(this);
|
|
672
|
+
this.memoryJobs = new MemoryJobsResource(this);
|
|
673
|
+
this.corrections = new CorrectionsResource(this);
|
|
516
674
|
this.searchResource = new SearchResource(this);
|
|
517
675
|
this.rl = new RLResource(this);
|
|
518
676
|
this.procedural = new ProceduralResource(this);
|
|
@@ -526,7 +684,7 @@ var MemoryClient = class {
|
|
|
526
684
|
getHeaders() {
|
|
527
685
|
const headers = {
|
|
528
686
|
"Content-Type": "application/json",
|
|
529
|
-
"User-Agent": "hebbrix-typescript/2.
|
|
687
|
+
"User-Agent": "hebbrix-typescript/2.2.0"
|
|
530
688
|
};
|
|
531
689
|
if (this.apiKey) {
|
|
532
690
|
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
@@ -580,7 +738,11 @@ var MemoryClient = class {
|
|
|
580
738
|
const searchParams = new URLSearchParams();
|
|
581
739
|
Object.entries(params).forEach(([key, value]) => {
|
|
582
740
|
if (value !== void 0 && value !== null) {
|
|
583
|
-
|
|
741
|
+
if (Array.isArray(value)) {
|
|
742
|
+
value.forEach((item) => searchParams.append(key, String(item)));
|
|
743
|
+
} else {
|
|
744
|
+
searchParams.append(key, String(value));
|
|
745
|
+
}
|
|
584
746
|
}
|
|
585
747
|
});
|
|
586
748
|
url += `?${searchParams.toString()}`;
|
|
@@ -617,9 +779,11 @@ var MemoryClient = class {
|
|
|
617
779
|
AuthenticationError,
|
|
618
780
|
CollectionsResource,
|
|
619
781
|
ConsolidationResource,
|
|
782
|
+
CorrectionsResource,
|
|
620
783
|
HebbrixError,
|
|
621
784
|
MemoriesResource,
|
|
622
785
|
MemoryClient,
|
|
786
|
+
MemoryJobsResource,
|
|
623
787
|
MemoryToolsResource,
|
|
624
788
|
NotFoundError,
|
|
625
789
|
ProceduralResource,
|
|
@@ -631,5 +795,6 @@ var MemoryClient = class {
|
|
|
631
795
|
TemporalResource,
|
|
632
796
|
ValidationError,
|
|
633
797
|
WorkingMemoryResource,
|
|
634
|
-
WorldModelResource
|
|
798
|
+
WorldModelResource,
|
|
799
|
+
enforceSearchSafety
|
|
635
800
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
// src/safety.ts
|
|
2
|
+
var REQUIRED_FIELDS = [
|
|
3
|
+
"no_match",
|
|
4
|
+
"abstain_recommended",
|
|
5
|
+
"query_confidence",
|
|
6
|
+
"grounding",
|
|
7
|
+
"evidence_ids",
|
|
8
|
+
"safety_contract_version"
|
|
9
|
+
];
|
|
10
|
+
function enforceSearchSafety(response, rowsKey = "results") {
|
|
11
|
+
const data = { ...response };
|
|
12
|
+
const rawRows = data[rowsKey];
|
|
13
|
+
const rows = Array.isArray(rawRows) ? rawRows.filter((row) => typeof row === "object" && row !== null) : [];
|
|
14
|
+
const missing = REQUIRED_FIELDS.filter(
|
|
15
|
+
(field) => !Object.prototype.hasOwnProperty.call(data, field)
|
|
16
|
+
);
|
|
17
|
+
let reason;
|
|
18
|
+
if (missing.length > 0) {
|
|
19
|
+
reason = `missing_safety_fields:${missing.join(",")}`;
|
|
20
|
+
} else if (typeof data.no_match !== "boolean" || typeof data.abstain_recommended !== "boolean") {
|
|
21
|
+
reason = "invalid_abstention_fields";
|
|
22
|
+
} else if (typeof data.query_confidence !== "number" || !Number.isFinite(data.query_confidence) || data.query_confidence < 0 || data.query_confidence > 1) {
|
|
23
|
+
reason = "invalid_query_confidence";
|
|
24
|
+
} else if (typeof data.grounding !== "object" || data.grounding === null || Array.isArray(data.grounding)) {
|
|
25
|
+
reason = "invalid_grounding_receipt";
|
|
26
|
+
} else if (!Array.isArray(data.evidence_ids)) {
|
|
27
|
+
reason = "invalid_evidence_ids";
|
|
28
|
+
} else {
|
|
29
|
+
const evidenceIds = new Set(data.evidence_ids.map(String));
|
|
30
|
+
const rowIds = rows.map((row) => row.memory_id ?? row.id).filter((value) => typeof value === "string" && value.length > 0);
|
|
31
|
+
if (rowIds.some((value) => !evidenceIds.has(value))) {
|
|
32
|
+
reason = "rows_not_bound_to_evidence_ids";
|
|
33
|
+
} else if (data.no_match && (rowIds.length > 0 || evidenceIds.size > 0)) {
|
|
34
|
+
reason = "no_match_contains_evidence";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (reason || data.degraded === true || data.no_match === true || data.abstain_recommended === true) {
|
|
38
|
+
data[rowsKey] = [];
|
|
39
|
+
if (rowsKey === "results") data.total = 0;
|
|
40
|
+
data.no_match = true;
|
|
41
|
+
data.abstain_recommended = true;
|
|
42
|
+
data.query_confidence = 0;
|
|
43
|
+
data.evidence_ids = [];
|
|
44
|
+
data.evidence_claims = [];
|
|
45
|
+
if (reason) {
|
|
46
|
+
data.sdk_safety_reason = reason;
|
|
47
|
+
data.grounding = { status: "no_grounded_match", reason };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
|
|
1
53
|
// src/resources.ts
|
|
2
54
|
var BaseResource = class {
|
|
3
55
|
constructor(client) {
|
|
@@ -77,14 +129,32 @@ var MemoriesResource = class extends BaseResource {
|
|
|
77
129
|
* Create a new memory
|
|
78
130
|
*/
|
|
79
131
|
async create(params) {
|
|
80
|
-
|
|
132
|
+
if (!params.content?.trim() && !params.messages?.length) {
|
|
133
|
+
throw new TypeError("content or messages must be provided");
|
|
134
|
+
}
|
|
135
|
+
const { idempotency_key, ...input } = params;
|
|
136
|
+
return this.client.request("POST", "/v1/memories", {
|
|
137
|
+
headers: idempotency_key ? { "Idempotency-Key": idempotency_key } : void 0,
|
|
138
|
+
body: JSON.stringify({
|
|
139
|
+
source_type: "text",
|
|
140
|
+
metadata: {},
|
|
141
|
+
infer: false,
|
|
142
|
+
wait_for_index: false,
|
|
143
|
+
...input
|
|
144
|
+
})
|
|
145
|
+
});
|
|
81
146
|
}
|
|
82
147
|
/**
|
|
83
148
|
* List memories
|
|
84
149
|
*/
|
|
85
|
-
async
|
|
150
|
+
async listPage(params = {}) {
|
|
86
151
|
return this.client.get("/v1/memories", params);
|
|
87
152
|
}
|
|
153
|
+
/** Back-compatible one-page convenience; use listPage for cursor metadata. */
|
|
154
|
+
async list(params = {}) {
|
|
155
|
+
const page = await this.listPage(params);
|
|
156
|
+
return page.items;
|
|
157
|
+
}
|
|
88
158
|
/**
|
|
89
159
|
* Get a specific memory
|
|
90
160
|
*/
|
|
@@ -104,30 +174,79 @@ var MemoriesResource = class extends BaseResource {
|
|
|
104
174
|
await this.client.delete(`/v1/memories/${memoryId}`);
|
|
105
175
|
}
|
|
106
176
|
};
|
|
177
|
+
var MemoryJobsResource = class extends BaseResource {
|
|
178
|
+
async get(jobId) {
|
|
179
|
+
return this.client.get(`/v1/memory-jobs/${jobId}`);
|
|
180
|
+
}
|
|
181
|
+
async wait(jobId, options = {}) {
|
|
182
|
+
const timeoutMs = Math.max(0, options.timeoutMs ?? 6e4);
|
|
183
|
+
const pollIntervalMs = Math.max(50, options.pollIntervalMs ?? 500);
|
|
184
|
+
const deadline = Date.now() + timeoutMs;
|
|
185
|
+
while (true) {
|
|
186
|
+
const receipt = await this.get(jobId);
|
|
187
|
+
const status = String(receipt.status || "").toLowerCase();
|
|
188
|
+
if (["completed", "failed", "cancelled", "canceled"].includes(status)) {
|
|
189
|
+
return receipt;
|
|
190
|
+
}
|
|
191
|
+
if (Date.now() >= deadline) {
|
|
192
|
+
throw new Error(`memory job ${jobId} did not finish in ${timeoutMs}ms`);
|
|
193
|
+
}
|
|
194
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
var CorrectionsResource = class extends BaseResource {
|
|
199
|
+
async create(params) {
|
|
200
|
+
const { idempotency_key, ...body } = params;
|
|
201
|
+
return this.client.request("POST", "/v1/corrections", {
|
|
202
|
+
headers: idempotency_key ? { "Idempotency-Key": idempotency_key } : void 0,
|
|
203
|
+
body: JSON.stringify({
|
|
204
|
+
correction_type: "preference",
|
|
205
|
+
confidence: 1,
|
|
206
|
+
...body
|
|
207
|
+
})
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
async relevant(params) {
|
|
211
|
+
return this.client.get("/v1/corrections/relevant", {
|
|
212
|
+
include_global: false,
|
|
213
|
+
limit: 10,
|
|
214
|
+
...params
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
async get(correctionId) {
|
|
218
|
+
return this.client.get(`/v1/corrections/${correctionId}`);
|
|
219
|
+
}
|
|
220
|
+
async delete(correctionId) {
|
|
221
|
+
return this.client.delete(`/v1/corrections/${correctionId}`);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
107
224
|
var SearchResource = class extends BaseResource {
|
|
108
225
|
/**
|
|
109
226
|
* Search memories
|
|
110
227
|
*/
|
|
111
228
|
async search(params) {
|
|
112
|
-
const response = await this.
|
|
113
|
-
query: params.query,
|
|
114
|
-
collection_id: params.collection_id,
|
|
115
|
-
limit: params.limit || 10,
|
|
116
|
-
search_type: params.search_type || "hybrid",
|
|
117
|
-
filters: params.filters || {}
|
|
118
|
-
});
|
|
229
|
+
const response = await this.searchWithProof(params);
|
|
119
230
|
return response.results;
|
|
120
231
|
}
|
|
121
232
|
/** Search while preserving the automatic ProofLoop evidence context. */
|
|
122
233
|
async searchWithProof(params) {
|
|
123
|
-
|
|
234
|
+
const response = await this.client.post("/v1/search", {
|
|
124
235
|
query: params.query,
|
|
125
236
|
collection_id: params.collection_id,
|
|
126
237
|
user_id: params.user_id,
|
|
238
|
+
agent_id: params.agent_id,
|
|
239
|
+
run_id: params.run_id,
|
|
127
240
|
limit: params.limit || 10,
|
|
128
241
|
search_type: params.search_type || "hybrid",
|
|
129
|
-
filters: params.filters || {}
|
|
242
|
+
filters: params.filters || {},
|
|
243
|
+
fast: params.fast,
|
|
244
|
+
threshold: params.threshold,
|
|
245
|
+
include_low_confidence: params.include_low_confidence ?? false,
|
|
246
|
+
group_by_source: params.group_by_source ?? true,
|
|
247
|
+
debug: params.debug ?? false
|
|
130
248
|
});
|
|
249
|
+
return enforceSearchSafety(response);
|
|
131
250
|
}
|
|
132
251
|
/**
|
|
133
252
|
* Find similar memories
|
|
@@ -143,12 +262,20 @@ var SearchResource = class extends BaseResource {
|
|
|
143
262
|
* Perform reasoning over memories
|
|
144
263
|
*/
|
|
145
264
|
async reason(params) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
265
|
+
const response = await this.client.post(
|
|
266
|
+
"/v1/search/reason",
|
|
267
|
+
{
|
|
268
|
+
query: params.query,
|
|
269
|
+
collection_id: params.collection_id,
|
|
270
|
+
provider: params.provider,
|
|
271
|
+
include_steps: params.include_steps ?? false,
|
|
272
|
+
user_id: params.user_id,
|
|
273
|
+
agent_id: params.agent_id,
|
|
274
|
+
run_id: params.run_id,
|
|
275
|
+
facets: params.facets ?? []
|
|
276
|
+
}
|
|
277
|
+
);
|
|
278
|
+
return enforceSearchSafety(response, "sources");
|
|
152
279
|
}
|
|
153
280
|
};
|
|
154
281
|
var ProofLoopResource = class extends BaseResource {
|
|
@@ -168,11 +295,37 @@ var ProofLoopResource = class extends BaseResource {
|
|
|
168
295
|
...body
|
|
169
296
|
});
|
|
170
297
|
}
|
|
298
|
+
async getDecision(decisionId) {
|
|
299
|
+
return this.client.get(`/v1/learning/decisions/${decisionId}`);
|
|
300
|
+
}
|
|
301
|
+
async defineMetric(params) {
|
|
302
|
+
return this.client.post("/v1/learning/metrics", params);
|
|
303
|
+
}
|
|
304
|
+
async listMetrics(params = {}) {
|
|
305
|
+
return this.client.get("/v1/learning/metrics", params);
|
|
306
|
+
}
|
|
307
|
+
async policyInsights(policyKey, params = {}) {
|
|
308
|
+
return this.client.get(`/v1/learning/policies/${policyKey}/insights`, {
|
|
309
|
+
collection_id: params.collection_id,
|
|
310
|
+
user_id: params.user_id,
|
|
311
|
+
action_key: params.action_keys,
|
|
312
|
+
context: params.context ? JSON.stringify(params.context) : void 0
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
async evaluatePolicy(policyKey, params = {}) {
|
|
316
|
+
return this.client.post(`/v1/learning/policies/${policyKey}/evaluate`, {
|
|
317
|
+
collection_id: params.collection_id,
|
|
318
|
+
user_id: params.user_id,
|
|
319
|
+
limit: params.limit ?? 500
|
|
320
|
+
});
|
|
321
|
+
}
|
|
171
322
|
async proof(decisionId) {
|
|
172
323
|
return this.client.get(`/v1/learning/decisions/${decisionId}/proof`);
|
|
173
324
|
}
|
|
174
|
-
async publicKey() {
|
|
175
|
-
return this.client.get("/v1/learning/proof-key"
|
|
325
|
+
async publicKey(keyId) {
|
|
326
|
+
return this.client.get("/v1/learning/proof-key", {
|
|
327
|
+
key_id: keyId
|
|
328
|
+
});
|
|
176
329
|
}
|
|
177
330
|
};
|
|
178
331
|
var RLResource = class extends BaseResource {
|
|
@@ -469,6 +622,8 @@ var MemoryClient = class {
|
|
|
469
622
|
this.auth = new AuthResource(this);
|
|
470
623
|
this.collections = new CollectionsResource(this);
|
|
471
624
|
this.memories = new MemoriesResource(this);
|
|
625
|
+
this.memoryJobs = new MemoryJobsResource(this);
|
|
626
|
+
this.corrections = new CorrectionsResource(this);
|
|
472
627
|
this.searchResource = new SearchResource(this);
|
|
473
628
|
this.rl = new RLResource(this);
|
|
474
629
|
this.procedural = new ProceduralResource(this);
|
|
@@ -482,7 +637,7 @@ var MemoryClient = class {
|
|
|
482
637
|
getHeaders() {
|
|
483
638
|
const headers = {
|
|
484
639
|
"Content-Type": "application/json",
|
|
485
|
-
"User-Agent": "hebbrix-typescript/2.
|
|
640
|
+
"User-Agent": "hebbrix-typescript/2.2.0"
|
|
486
641
|
};
|
|
487
642
|
if (this.apiKey) {
|
|
488
643
|
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
@@ -536,7 +691,11 @@ var MemoryClient = class {
|
|
|
536
691
|
const searchParams = new URLSearchParams();
|
|
537
692
|
Object.entries(params).forEach(([key, value]) => {
|
|
538
693
|
if (value !== void 0 && value !== null) {
|
|
539
|
-
|
|
694
|
+
if (Array.isArray(value)) {
|
|
695
|
+
value.forEach((item) => searchParams.append(key, String(item)));
|
|
696
|
+
} else {
|
|
697
|
+
searchParams.append(key, String(value));
|
|
698
|
+
}
|
|
540
699
|
}
|
|
541
700
|
});
|
|
542
701
|
url += `?${searchParams.toString()}`;
|
|
@@ -572,9 +731,11 @@ export {
|
|
|
572
731
|
AuthenticationError,
|
|
573
732
|
CollectionsResource,
|
|
574
733
|
ConsolidationResource,
|
|
734
|
+
CorrectionsResource,
|
|
575
735
|
HebbrixError,
|
|
576
736
|
MemoriesResource,
|
|
577
737
|
MemoryClient,
|
|
738
|
+
MemoryJobsResource,
|
|
578
739
|
MemoryToolsResource,
|
|
579
740
|
NotFoundError,
|
|
580
741
|
ProceduralResource,
|
|
@@ -586,5 +747,6 @@ export {
|
|
|
586
747
|
TemporalResource,
|
|
587
748
|
ValidationError,
|
|
588
749
|
WorkingMemoryResource,
|
|
589
|
-
WorldModelResource
|
|
750
|
+
WorldModelResource,
|
|
751
|
+
enforceSearchSafety
|
|
590
752
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hebbrix",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Advanced Memory API for AI Agents with Reinforcement Learning - TypeScript/JavaScript SDK",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"scripts": {
|
|
20
20
|
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
21
21
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
22
|
-
"test": "
|
|
22
|
+
"test": "npm run build && node --test tests/*.test.mjs",
|
|
23
23
|
"lint": "eslint src --ext .ts",
|
|
24
24
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
25
25
|
"prepublishOnly": "npm run build"
|