hebbrix 2.2.1 → 2.3.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.js CHANGED
@@ -25,7 +25,9 @@ __export(index_exports, {
25
25
  CollectionsResource: () => CollectionsResource,
26
26
  ConsolidationResource: () => ConsolidationResource,
27
27
  CorrectionsResource: () => CorrectionsResource,
28
+ EntitlementError: () => EntitlementError,
28
29
  HebbrixError: () => HebbrixError,
30
+ IndexingTimeoutError: () => IndexingTimeoutError,
29
31
  MemoriesResource: () => MemoriesResource,
30
32
  MemoryClient: () => MemoryClient,
31
33
  MemoryJobsResource: () => MemoryJobsResource,
@@ -40,7 +42,6 @@ __export(index_exports, {
40
42
  TemporalResource: () => TemporalResource,
41
43
  ValidationError: () => ValidationError,
42
44
  WorkingMemoryResource: () => WorkingMemoryResource,
43
- WorldModelResource: () => WorldModelResource,
44
45
  enforceSearchSafety: () => enforceSearchSafety
45
46
  });
46
47
  module.exports = __toCommonJS(index_exports);
@@ -99,6 +100,72 @@ function enforceSearchSafety(response, rowsKey = "results") {
99
100
  return data;
100
101
  }
101
102
 
103
+ // src/errors.ts
104
+ var HebbrixError = class _HebbrixError extends Error {
105
+ constructor(message, statusCode, options = {}) {
106
+ super(message);
107
+ this.name = "HebbrixError";
108
+ this.statusCode = statusCode;
109
+ this.code = options.code;
110
+ this.requestId = options.requestId;
111
+ this.details = options.details;
112
+ Object.setPrototypeOf(this, _HebbrixError.prototype);
113
+ }
114
+ };
115
+ var EntitlementError = class _EntitlementError extends HebbrixError {
116
+ constructor(message, statusCode, options = {}) {
117
+ super(message, statusCode, options);
118
+ this.name = "EntitlementError";
119
+ Object.setPrototypeOf(this, _EntitlementError.prototype);
120
+ }
121
+ };
122
+ var IndexingTimeoutError = class _IndexingTimeoutError extends Error {
123
+ constructor(message, receipt) {
124
+ super(message);
125
+ this.name = "IndexingTimeoutError";
126
+ this.receipt = { ...receipt };
127
+ this.memoryIds = [...receipt.memory_ids || []];
128
+ this.statusUrl = receipt.status_url;
129
+ Object.setPrototypeOf(this, _IndexingTimeoutError.prototype);
130
+ }
131
+ };
132
+ var AuthenticationError = class _AuthenticationError extends HebbrixError {
133
+ constructor(message = "Authentication failed", options = {}) {
134
+ super(message, 401, options);
135
+ this.name = "AuthenticationError";
136
+ Object.setPrototypeOf(this, _AuthenticationError.prototype);
137
+ }
138
+ };
139
+ var ValidationError = class _ValidationError extends HebbrixError {
140
+ constructor(message, errors, options = {}) {
141
+ super(message, 422, options);
142
+ this.name = "ValidationError";
143
+ this.errors = errors;
144
+ Object.setPrototypeOf(this, _ValidationError.prototype);
145
+ }
146
+ };
147
+ var NotFoundError = class _NotFoundError extends HebbrixError {
148
+ constructor(message = "Resource not found", options = {}) {
149
+ super(message, 404, options);
150
+ this.name = "NotFoundError";
151
+ Object.setPrototypeOf(this, _NotFoundError.prototype);
152
+ }
153
+ };
154
+ var RateLimitError = class _RateLimitError extends HebbrixError {
155
+ constructor(message = "Rate limit exceeded", options = {}) {
156
+ super(message, 429, options);
157
+ this.name = "RateLimitError";
158
+ Object.setPrototypeOf(this, _RateLimitError.prototype);
159
+ }
160
+ };
161
+ var ServerError = class _ServerError extends HebbrixError {
162
+ constructor(message = "Internal server error", options = {}) {
163
+ super(message, 500, options);
164
+ this.name = "ServerError";
165
+ Object.setPrototypeOf(this, _ServerError.prototype);
166
+ }
167
+ };
168
+
102
169
  // src/resources.ts
103
170
  var BaseResource = class {
104
171
  constructor(client) {
@@ -195,9 +262,9 @@ var MemoriesResource = class extends BaseResource {
195
262
  }
196
263
  /**
197
264
  * Create up to 100 memories with one unambiguous readiness contract.
198
- * `wait_for_index=true` resolves only for a fully searchable batch; a server
199
- * deadline or indexing failure rejects the request instead of returning a
200
- * successful processing receipt.
265
+ * `wait_for_index=true` resolves only for a fully searchable batch. A durable
266
+ * server-side 202 is polled to the caller's deadline; expiry raises a typed
267
+ * `IndexingTimeoutError` carrying the original durable receipt.
201
268
  */
202
269
  async createBatch(params) {
203
270
  if (!params.memories?.length || params.memories.length > 100) {
@@ -206,7 +273,13 @@ var MemoriesResource = class extends BaseResource {
206
273
  if (params.memories.some((item) => !item.content?.trim())) {
207
274
  throw new TypeError("every batch memory must contain non-empty content");
208
275
  }
209
- const { idempotency_key, signal, ...body } = params;
276
+ const {
277
+ idempotency_key,
278
+ signal,
279
+ index_timeout_ms,
280
+ index_poll_interval_ms,
281
+ ...body
282
+ } = params;
210
283
  const receipt = await this.client.request(
211
284
  "POST",
212
285
  "/v1/memories/batch",
@@ -216,10 +289,12 @@ var MemoriesResource = class extends BaseResource {
216
289
  signal
217
290
  }
218
291
  );
219
- if (params.wait_for_index && !(receipt.searchable === true && receipt.processing_status === "completed")) {
220
- throw new Error(
221
- "wait_for_index batch response was not fully searchable; retry with the same Idempotency-Key"
222
- );
292
+ if (params.wait_for_index && !receipt.searchable) {
293
+ return this.waitForBatchSearchable(receipt, {
294
+ timeoutMs: index_timeout_ms,
295
+ pollIntervalMs: index_poll_interval_ms,
296
+ signal
297
+ });
223
298
  }
224
299
  return receipt;
225
300
  }
@@ -260,7 +335,10 @@ var MemoriesResource = class extends BaseResource {
260
335
  };
261
336
  }
262
337
  if (Date.now() >= deadline) {
263
- throw new Error(`batch was not searchable within ${timeoutMs}ms`);
338
+ throw new IndexingTimeoutError(
339
+ `batch was not searchable within ${timeoutMs}ms; the write is durable`,
340
+ receipt
341
+ );
264
342
  }
265
343
  await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
266
344
  }
@@ -454,8 +532,7 @@ var RLResource = class extends BaseResource {
454
532
  * Train the Memory Manager agent using RL
455
533
  */
456
534
  async trainMemoryManager(params) {
457
- return this.client.post("/rl/train/memory-manager", {
458
- collection_id: params.collection_id,
535
+ return this.client.post("/v1/rl/train/memory-manager", {
459
536
  num_episodes: params.num_episodes || 100,
460
537
  ...params
461
538
  });
@@ -464,8 +541,7 @@ var RLResource = class extends BaseResource {
464
541
  * Train the Answer Agent using RL
465
542
  */
466
543
  async trainAnswerAgent(params) {
467
- return this.client.post("/rl/train/answer-agent", {
468
- collection_id: params.collection_id,
544
+ return this.client.post("/v1/rl/train/answer-agent", {
469
545
  num_episodes: params.num_episodes || 100,
470
546
  ...params
471
547
  });
@@ -474,13 +550,13 @@ var RLResource = class extends BaseResource {
474
550
  * Get RL training metrics
475
551
  */
476
552
  async getMetrics() {
477
- return this.client.get("/rl/metrics");
553
+ return this.client.get("/v1/rl/metrics");
478
554
  }
479
555
  /**
480
556
  * Evaluate a trained RL agent
481
557
  */
482
558
  async evaluate(agentType, collectionId) {
483
- return this.client.post("/rl/evaluate", {
559
+ return this.client.post("/v1/rl/evaluate", {
484
560
  agent_type: agentType,
485
561
  collection_id: collectionId
486
562
  });
@@ -566,12 +642,15 @@ var TemporalResource = class extends BaseResource {
566
642
  * Add a temporal fact to the knowledge graph
567
643
  */
568
644
  async addFact(params) {
569
- return this.client.post("/temporal/facts", {
645
+ return this.client.post("/v1/temporal/facts", {
570
646
  subject: params.subject,
647
+ subject_type: params.subject_type || "ENTITY",
571
648
  predicate: params.predicate,
572
649
  object: params.object,
650
+ object_type: params.object_type || "ENTITY",
573
651
  valid_from: params.valid_from,
574
652
  valid_until: params.valid_until,
653
+ observed_at: params.observed_at,
575
654
  confidence: params.confidence || 1,
576
655
  source_memory_id: params.source_memory_id,
577
656
  metadata: params.metadata || {}
@@ -581,33 +660,79 @@ var TemporalResource = class extends BaseResource {
581
660
  * Query temporal facts
582
661
  */
583
662
  async queryFacts(params) {
584
- return this.client.get("/temporal/facts", {
585
- subject: params?.subject,
586
- predicate: params?.predicate,
587
- object: params?.object,
588
- at_time: params?.at_time
663
+ if (!params.subject) {
664
+ throw new TypeError("subject is required by the temporal query contract");
665
+ }
666
+ let rows;
667
+ if (params.at_time) {
668
+ const response = await this.queryAtTime({
669
+ subject: params.subject,
670
+ predicate: params.predicate,
671
+ timestamp: params.at_time
672
+ });
673
+ rows = response.facts || [];
674
+ } else if (params.predicate) {
675
+ const response = await this.history(
676
+ params.subject,
677
+ params.predicate
678
+ );
679
+ rows = response.history || [];
680
+ } else {
681
+ throw new TypeError("predicate or at_time is required");
682
+ }
683
+ return params.object === void 0 ? rows : rows.filter((row) => row.object === params.object);
684
+ }
685
+ async queryAtTime(params) {
686
+ return this.client.post("/v1/temporal/facts/query-at-time", params);
687
+ }
688
+ async history(subject, predicate, limit = 50) {
689
+ return this.client.get("/v1/temporal/facts/history", {
690
+ subject,
691
+ predicate,
692
+ limit
693
+ });
694
+ }
695
+ async conflicts(subject, predicate) {
696
+ return this.client.get("/v1/temporal/facts/conflicts", {
697
+ subject,
698
+ predicate
699
+ });
700
+ }
701
+ async invalidate(subject, predicate, object) {
702
+ return this.client.post("/v1/temporal/facts/invalidate", {
703
+ subject,
704
+ predicate,
705
+ object
589
706
  });
590
707
  }
591
708
  /**
592
709
  * Query knowledge state at a specific point in time
593
710
  */
594
711
  async pointInTime(timestamp, entity) {
595
- return this.client.post("/temporal/point-in-time", {
712
+ if (!entity) {
713
+ throw new TypeError("entity is required and maps to the canonical subject");
714
+ }
715
+ return this.queryAtTime({
596
716
  timestamp,
597
- entity
717
+ subject: entity
598
718
  });
599
719
  }
600
720
  /** Permanently delete a tenant-scoped temporal fact by stable ID. */
601
721
  async deleteFact(factId) {
602
- return this.client.delete(`/temporal/facts/${factId}`);
722
+ return this.client.delete(`/v1/temporal/facts/${factId}`);
603
723
  }
604
724
  };
605
725
  var WorkingMemoryResource = class extends BaseResource {
726
+ constructor(client) {
727
+ super(client);
728
+ this.sessionId = `sdk-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
729
+ }
606
730
  /**
607
731
  * Add item to working memory buffer
608
732
  */
609
733
  async add(params) {
610
- return this.client.post("/working-memory", {
734
+ return this.client.post("/v1/working-memory/add", {
735
+ session_id: params.session_id || this.sessionId,
611
736
  role: params.role,
612
737
  content: params.content,
613
738
  metadata: params.metadata || {}
@@ -616,48 +741,40 @@ var WorkingMemoryResource = class extends BaseResource {
616
741
  /**
617
742
  * Get current working memory context
618
743
  */
619
- async getContext() {
620
- return this.client.get("/working-memory/context");
744
+ async getContext(sessionId = this.sessionId, includeCompressed = false) {
745
+ return this.client.get(`/v1/working-memory/context/${sessionId}`, {
746
+ include_compressed: includeCompressed
747
+ });
621
748
  }
622
749
  /**
623
750
  * Compress working memory buffer
624
751
  */
625
- async compress() {
626
- return this.client.post("/working-memory/compress");
752
+ async compress(sessionId = this.sessionId) {
753
+ return this.client.post(`/v1/working-memory/compress/${sessionId}`);
627
754
  }
628
755
  /**
629
756
  * Clear working memory buffer
630
757
  */
631
- async clear() {
632
- return this.client.delete("/working-memory");
758
+ async clear(sessionId = this.sessionId) {
759
+ return this.client.delete(`/v1/working-memory/clear/${sessionId}`);
633
760
  }
634
761
  };
635
762
  var ConsolidationResource = class extends BaseResource {
636
763
  /**
637
764
  * Trigger memory consolidation
638
765
  */
639
- async consolidate(collectionId, threshold = 100) {
640
- return this.client.post("/consolidation/consolidate", {
766
+ async consolidate(collectionId, lookbackDays = 7, utilityThreshold = 0.3) {
767
+ return this.client.post("/v1/consolidation/consolidate", {
641
768
  collection_id: collectionId,
642
- threshold
769
+ lookback_days: lookbackDays,
770
+ utility_threshold: utilityThreshold
643
771
  });
644
772
  }
645
773
  /**
646
774
  * Get consolidation statistics
647
775
  */
648
776
  async getStats(collectionId) {
649
- return this.client.get("/consolidation/stats", {
650
- collection_id: collectionId
651
- });
652
- }
653
- /**
654
- * Archive old memories
655
- */
656
- async archive(collectionId, beforeDate) {
657
- return this.client.post("/consolidation/archive", {
658
- collection_id: collectionId,
659
- before_date: beforeDate
660
- });
777
+ return this.client.get(`/v1/consolidation/stats/${collectionId}`);
661
778
  }
662
779
  };
663
780
  var MemoryToolsResource = class extends BaseResource {
@@ -665,100 +782,38 @@ var MemoryToolsResource = class extends BaseResource {
665
782
  * Replace memory content
666
783
  */
667
784
  async replace(params) {
668
- return this.client.post("/memory-tools/replace", {
785
+ return this.client.post("/v1/memory-tools/replace", {
669
786
  memory_id: params.memory_id,
787
+ old_content: params.old_content,
670
788
  new_content: params.new_content,
671
- reason: params.reason
789
+ collection_id: params.collection_id
672
790
  });
673
791
  }
674
792
  /**
675
793
  * Insert new memory at position
676
794
  */
677
795
  async insert(params) {
678
- return this.client.post("/memory-tools/insert", {
796
+ const metadata = { ...params.metadata || {} };
797
+ if (params.position !== void 0) metadata.requested_position = params.position;
798
+ if (params.reason !== void 0) metadata.reason = params.reason;
799
+ return this.client.post("/v1/memory-tools/insert", {
679
800
  collection_id: params.collection_id,
680
801
  content: params.content,
681
- position: params.position,
682
- reason: params.reason
802
+ importance: params.importance ?? 0.5,
803
+ metadata
683
804
  });
684
805
  }
685
806
  /**
686
807
  * Re-evaluate memory in light of new information
687
808
  */
688
- async rethink(memoryId, query) {
689
- return this.client.post("/memory-tools/rethink", {
809
+ async rethink(memoryId, collectionId) {
810
+ return this.client.post("/v1/memory-tools/rethink", {
690
811
  memory_id: memoryId,
691
- query
692
- });
693
- }
694
- };
695
- var WorldModelResource = class extends BaseResource {
696
- /**
697
- * Simulate retrieval without actually retrieving
698
- */
699
- async imagineRetrieval(query, collectionId) {
700
- return this.client.post("/world-model/imagine-retrieval", {
701
- query,
702
- collection_id: collectionId
703
- });
704
- }
705
- /**
706
- * Plan memory operations to achieve goal
707
- */
708
- async plan(goal, collectionId) {
709
- return this.client.post("/world-model/plan", {
710
- goal,
711
812
  collection_id: collectionId
712
813
  });
713
814
  }
714
815
  };
715
816
 
716
- // src/errors.ts
717
- var HebbrixError = class _HebbrixError extends Error {
718
- constructor(message, statusCode) {
719
- super(message);
720
- this.name = "HebbrixError";
721
- this.statusCode = statusCode;
722
- Object.setPrototypeOf(this, _HebbrixError.prototype);
723
- }
724
- };
725
- var AuthenticationError = class _AuthenticationError extends HebbrixError {
726
- constructor(message = "Authentication failed") {
727
- super(message, 401);
728
- this.name = "AuthenticationError";
729
- Object.setPrototypeOf(this, _AuthenticationError.prototype);
730
- }
731
- };
732
- var ValidationError = class _ValidationError extends HebbrixError {
733
- constructor(message, errors) {
734
- super(message, 422);
735
- this.name = "ValidationError";
736
- this.errors = errors;
737
- Object.setPrototypeOf(this, _ValidationError.prototype);
738
- }
739
- };
740
- var NotFoundError = class _NotFoundError extends HebbrixError {
741
- constructor(message = "Resource not found") {
742
- super(message, 404);
743
- this.name = "NotFoundError";
744
- Object.setPrototypeOf(this, _NotFoundError.prototype);
745
- }
746
- };
747
- var RateLimitError = class _RateLimitError extends HebbrixError {
748
- constructor(message = "Rate limit exceeded") {
749
- super(message, 429);
750
- this.name = "RateLimitError";
751
- Object.setPrototypeOf(this, _RateLimitError.prototype);
752
- }
753
- };
754
- var ServerError = class _ServerError extends HebbrixError {
755
- constructor(message = "Internal server error") {
756
- super(message, 500);
757
- this.name = "ServerError";
758
- Object.setPrototypeOf(this, _ServerError.prototype);
759
- }
760
- };
761
-
762
817
  // src/client.ts
763
818
  var MemoryClient = class {
764
819
  constructor(config = {}) {
@@ -778,13 +833,12 @@ var MemoryClient = class {
778
833
  this.workingMemory = new WorkingMemoryResource(this);
779
834
  this.consolidation = new ConsolidationResource(this);
780
835
  this.memoryTools = new MemoryToolsResource(this);
781
- this.worldModel = new WorldModelResource(this);
782
836
  this.proofloop = new ProofLoopResource(this);
783
837
  }
784
838
  getHeaders() {
785
839
  const headers = {
786
840
  "Content-Type": "application/json",
787
- "User-Agent": "hebbrix-typescript/2.2.1"
841
+ "User-Agent": "hebbrix-typescript/2.3.0"
788
842
  };
789
843
  if (this.apiKey) {
790
844
  headers["Authorization"] = `Bearer ${this.apiKey}`;
@@ -793,21 +847,31 @@ var MemoryClient = class {
793
847
  }
794
848
  handleError(response, data) {
795
849
  const statusCode = response.status;
796
- const detail = data?.detail;
797
- const message = data?.error?.message || (typeof detail === "string" ? detail : detail?.message) || response.statusText;
850
+ const envelope = data?.error || data?.detail || {};
851
+ const nested = envelope?.message;
852
+ const details = nested && typeof nested === "object" ? nested : envelope;
853
+ const message = details?.message || (typeof nested === "string" ? nested : void 0) || response.statusText;
854
+ const code = details?.code || envelope?.code;
855
+ const requestId = details?.request_id || envelope?.request_id || response.headers.get("X-Request-ID") || void 0;
798
856
  if (statusCode === 401) {
799
- throw new AuthenticationError(message);
857
+ throw new AuthenticationError(message, { code, requestId, details });
800
858
  } else if (statusCode === 404) {
801
- throw new NotFoundError(message);
859
+ throw new NotFoundError(message, { code, requestId, details });
802
860
  } else if (statusCode === 422) {
803
861
  const errors = data?.error?.details || [];
804
- throw new ValidationError(message, errors);
862
+ throw new ValidationError(message, errors, { code, requestId, details });
805
863
  } else if (statusCode === 429) {
806
- throw new RateLimitError(message);
864
+ throw new RateLimitError(message, { code, requestId, details });
807
865
  } else if (statusCode >= 500) {
808
- throw new ServerError(message);
866
+ throw new ServerError(message, { code, requestId, details });
867
+ } else if ((statusCode === 402 || statusCode === 403) && (String(code || "").includes("ENTITLEMENT") || ["feature_not_available", "tier_upgrade_required"].includes(details?.error))) {
868
+ throw new EntitlementError(message, statusCode, {
869
+ code,
870
+ requestId,
871
+ details
872
+ });
809
873
  } else {
810
- throw new HebbrixError(message, statusCode);
874
+ throw new HebbrixError(message, statusCode, { code, requestId, details });
811
875
  }
812
876
  }
813
877
  async request(method, path, options = {}) {
@@ -889,7 +953,9 @@ var MemoryClient = class {
889
953
  CollectionsResource,
890
954
  ConsolidationResource,
891
955
  CorrectionsResource,
956
+ EntitlementError,
892
957
  HebbrixError,
958
+ IndexingTimeoutError,
893
959
  MemoriesResource,
894
960
  MemoryClient,
895
961
  MemoryJobsResource,
@@ -904,6 +970,5 @@ var MemoryClient = class {
904
970
  TemporalResource,
905
971
  ValidationError,
906
972
  WorkingMemoryResource,
907
- WorldModelResource,
908
973
  enforceSearchSafety
909
974
  });