@mnexium/sdk 0.1.0 → 0.2.1
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/README.md +61 -0
- package/dist/index.d.mts +194 -1
- package/dist/index.d.ts +194 -1
- package/dist/index.js +210 -28
- package/dist/index.mjs +210 -28
- package/package.json +4 -2
package/dist/index.mjs
CHANGED
|
@@ -61,7 +61,9 @@ var Chat = class {
|
|
|
61
61
|
temperature: options.temperature ?? this.options.temperature,
|
|
62
62
|
stream: options.stream,
|
|
63
63
|
metadata: options.metadata ?? this.options.metadata,
|
|
64
|
-
regenerateKey: options.regenerateKey
|
|
64
|
+
regenerateKey: options.regenerateKey,
|
|
65
|
+
memoryPolicy: options.memoryPolicy ?? this.options.memoryPolicy,
|
|
66
|
+
records: options.records ?? this.options.records
|
|
65
67
|
});
|
|
66
68
|
}
|
|
67
69
|
};
|
|
@@ -81,7 +83,8 @@ var EventStream = class {
|
|
|
81
83
|
}
|
|
82
84
|
async *[Symbol.asyncIterator]() {
|
|
83
85
|
const response = await this.client._requestRaw("GET", "/events/memories", {
|
|
84
|
-
query: { subject_id: this.subjectId }
|
|
86
|
+
query: { subject_id: this.subjectId },
|
|
87
|
+
signal: this.abortController.signal
|
|
85
88
|
});
|
|
86
89
|
if (!response.body) {
|
|
87
90
|
throw new Error("Response body is null \u2014 SSE not supported");
|
|
@@ -170,7 +173,8 @@ var SubjectMemoriesResource = class {
|
|
|
170
173
|
text,
|
|
171
174
|
source: options?.source,
|
|
172
175
|
visibility: options?.visibility,
|
|
173
|
-
metadata: options?.metadata
|
|
176
|
+
metadata: options?.metadata,
|
|
177
|
+
no_supersede: options?.no_supersede
|
|
174
178
|
}
|
|
175
179
|
});
|
|
176
180
|
}
|
|
@@ -566,6 +570,11 @@ var ChatCompletionsResource = class {
|
|
|
566
570
|
} else if (options.googleKey) {
|
|
567
571
|
headers["x-google-key"] = options.googleKey;
|
|
568
572
|
}
|
|
573
|
+
if (options.memoryPolicy === false) {
|
|
574
|
+
headers["x-mnx-memory-policy"] = "false";
|
|
575
|
+
} else if (typeof options.memoryPolicy === "string" && options.memoryPolicy.trim()) {
|
|
576
|
+
headers["x-mnx-memory-policy"] = options.memoryPolicy;
|
|
577
|
+
}
|
|
569
578
|
const body = {
|
|
570
579
|
model: options.model,
|
|
571
580
|
messages: options.messages,
|
|
@@ -584,9 +593,24 @@ var ChatCompletionsResource = class {
|
|
|
584
593
|
state: options.state,
|
|
585
594
|
system_prompt: options.systemPrompt,
|
|
586
595
|
metadata: options.metadata,
|
|
587
|
-
regenerate_key: options.regenerateKey
|
|
596
|
+
regenerate_key: options.regenerateKey,
|
|
597
|
+
memory_policy: options.memoryPolicy,
|
|
598
|
+
records: options.records
|
|
588
599
|
}
|
|
589
600
|
};
|
|
601
|
+
if (options.stream) {
|
|
602
|
+
const response = await this.client._requestRaw("POST", "/chat/completions", {
|
|
603
|
+
body,
|
|
604
|
+
headers
|
|
605
|
+
});
|
|
606
|
+
return new StreamResponse(response, {
|
|
607
|
+
chatId: response.headers.get("X-Mnx-Chat-Id") || options.chatId || "",
|
|
608
|
+
subjectId: response.headers.get("X-Mnx-Subject-Id") || options.subjectId || "",
|
|
609
|
+
model: options.model,
|
|
610
|
+
provisionedKey: response.headers.get("X-Mnx-Key-Provisioned") || void 0,
|
|
611
|
+
claimUrl: response.headers.get("X-Mnx-Claim-Url") || void 0
|
|
612
|
+
});
|
|
613
|
+
}
|
|
590
614
|
return this.client._request("POST", "/chat/completions", {
|
|
591
615
|
body,
|
|
592
616
|
headers
|
|
@@ -621,20 +645,18 @@ var MemoriesResource = class {
|
|
|
621
645
|
offset: options?.offset
|
|
622
646
|
}
|
|
623
647
|
});
|
|
624
|
-
return response.
|
|
648
|
+
return response.data || [];
|
|
625
649
|
}
|
|
626
650
|
async search(options) {
|
|
627
|
-
const response = await this.client._request("
|
|
628
|
-
|
|
651
|
+
const response = await this.client._request("GET", "/memories/search", {
|
|
652
|
+
query: {
|
|
629
653
|
subject_id: options.subjectId,
|
|
630
|
-
|
|
654
|
+
q: options.query,
|
|
631
655
|
limit: options.limit,
|
|
632
|
-
min_score: options.minScore
|
|
633
|
-
include_deleted: options.includeDeleted,
|
|
634
|
-
include_superseded: options.includeSuperseded
|
|
656
|
+
min_score: options.minScore
|
|
635
657
|
}
|
|
636
658
|
});
|
|
637
|
-
return response.
|
|
659
|
+
return response.data || [];
|
|
638
660
|
}
|
|
639
661
|
async delete(id) {
|
|
640
662
|
await this.client._request("DELETE", `/memories/${id}`);
|
|
@@ -699,7 +721,7 @@ var StateResource = class {
|
|
|
699
721
|
async get(key, subjectId) {
|
|
700
722
|
try {
|
|
701
723
|
return await this.client._request("GET", `/state/${key}`, {
|
|
702
|
-
|
|
724
|
+
headers: subjectId ? { "x-subject-id": subjectId } : void 0
|
|
703
725
|
});
|
|
704
726
|
} catch (error) {
|
|
705
727
|
if (error instanceof NotFoundError) {
|
|
@@ -709,17 +731,21 @@ var StateResource = class {
|
|
|
709
731
|
}
|
|
710
732
|
}
|
|
711
733
|
async set(options) {
|
|
712
|
-
|
|
734
|
+
const headers = {};
|
|
735
|
+
if (options.subjectId) {
|
|
736
|
+
headers["x-subject-id"] = options.subjectId;
|
|
737
|
+
}
|
|
738
|
+
return this.client._request("PUT", `/state/${options.key}`, {
|
|
739
|
+
headers,
|
|
713
740
|
body: {
|
|
714
741
|
value: options.value,
|
|
715
|
-
subject_id: options.subjectId,
|
|
716
742
|
ttl_seconds: options.ttlSeconds
|
|
717
743
|
}
|
|
718
744
|
});
|
|
719
745
|
}
|
|
720
746
|
async delete(key, subjectId) {
|
|
721
747
|
await this.client._request("DELETE", `/state/${key}`, {
|
|
722
|
-
|
|
748
|
+
headers: subjectId ? { "x-subject-id": subjectId } : void 0
|
|
723
749
|
});
|
|
724
750
|
}
|
|
725
751
|
};
|
|
@@ -769,6 +795,136 @@ var PromptsResource = class {
|
|
|
769
795
|
}
|
|
770
796
|
};
|
|
771
797
|
|
|
798
|
+
// src/resources/records.ts
|
|
799
|
+
var RecordsResource = class {
|
|
800
|
+
constructor(client) {
|
|
801
|
+
this.client = client;
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Define or update a record schema
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* await mnx.records.defineSchema('deal', {
|
|
808
|
+
* title: { type: 'string', required: true },
|
|
809
|
+
* value: { type: 'number' },
|
|
810
|
+
* stage: { type: 'string' },
|
|
811
|
+
* account_id: { type: 'ref:account' },
|
|
812
|
+
* }, { displayName: 'Deal', description: 'Sales deals' });
|
|
813
|
+
*/
|
|
814
|
+
async defineSchema(typeName, fields, opts) {
|
|
815
|
+
return this.client._request("POST", "/records/schemas", {
|
|
816
|
+
body: {
|
|
817
|
+
type_name: typeName,
|
|
818
|
+
fields,
|
|
819
|
+
display_name: opts?.displayName,
|
|
820
|
+
description: opts?.description
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Get a schema by type name
|
|
826
|
+
*/
|
|
827
|
+
async getSchema(typeName) {
|
|
828
|
+
try {
|
|
829
|
+
return await this.client._request("GET", `/records/schemas/${typeName}`);
|
|
830
|
+
} catch (error) {
|
|
831
|
+
if (error instanceof NotFoundError) return null;
|
|
832
|
+
throw error;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* List all schemas for the project
|
|
837
|
+
*/
|
|
838
|
+
async listSchemas() {
|
|
839
|
+
const response = await this.client._request("GET", "/records/schemas");
|
|
840
|
+
return response.schemas;
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Insert a new record
|
|
844
|
+
*
|
|
845
|
+
* @example
|
|
846
|
+
* const deal = await mnx.records.insert('deal', {
|
|
847
|
+
* title: 'Acme Renewal',
|
|
848
|
+
* value: 500000,
|
|
849
|
+
* stage: 'negotiation',
|
|
850
|
+
* });
|
|
851
|
+
*/
|
|
852
|
+
async insert(typeName, data, opts) {
|
|
853
|
+
return this.client._request("POST", `/records/${typeName}`, {
|
|
854
|
+
body: {
|
|
855
|
+
data,
|
|
856
|
+
owner_id: opts?.ownerId,
|
|
857
|
+
visibility: opts?.visibility,
|
|
858
|
+
collaborators: opts?.collaborators
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Get a record by ID
|
|
864
|
+
*/
|
|
865
|
+
async get(typeName, recordId) {
|
|
866
|
+
try {
|
|
867
|
+
return await this.client._request("GET", `/records/${typeName}/${recordId}`);
|
|
868
|
+
} catch (error) {
|
|
869
|
+
if (error instanceof NotFoundError) return null;
|
|
870
|
+
throw error;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Update a record (partial merge)
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* await mnx.records.update('deal', 'rec_abc', { stage: 'closed_won', value: 550000 });
|
|
878
|
+
*/
|
|
879
|
+
async update(typeName, recordId, data) {
|
|
880
|
+
return this.client._request("PUT", `/records/${typeName}/${recordId}`, {
|
|
881
|
+
body: { data }
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Soft-delete a record
|
|
886
|
+
*/
|
|
887
|
+
async delete(typeName, recordId) {
|
|
888
|
+
await this.client._request("DELETE", `/records/${typeName}/${recordId}`);
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Query records with JSONB filters
|
|
892
|
+
*
|
|
893
|
+
* @example
|
|
894
|
+
* const deals = await mnx.records.query('deal', {
|
|
895
|
+
* where: { stage: 'closed_won' },
|
|
896
|
+
* orderBy: '-value',
|
|
897
|
+
* limit: 10,
|
|
898
|
+
* });
|
|
899
|
+
*/
|
|
900
|
+
async query(typeName, opts) {
|
|
901
|
+
const response = await this.client._request("POST", `/records/${typeName}/query`, {
|
|
902
|
+
body: {
|
|
903
|
+
where: opts?.where,
|
|
904
|
+
order_by: opts?.orderBy,
|
|
905
|
+
limit: opts?.limit,
|
|
906
|
+
offset: opts?.offset
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
return response.records;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Semantic search across records
|
|
913
|
+
*
|
|
914
|
+
* @example
|
|
915
|
+
* const results = await mnx.records.search('deal', 'large enterprise renewal');
|
|
916
|
+
*/
|
|
917
|
+
async search(typeName, query, opts) {
|
|
918
|
+
const response = await this.client._request("POST", `/records/${typeName}/search`, {
|
|
919
|
+
body: {
|
|
920
|
+
query,
|
|
921
|
+
limit: opts?.limit
|
|
922
|
+
}
|
|
923
|
+
});
|
|
924
|
+
return response.records;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
|
|
772
928
|
// src/client.ts
|
|
773
929
|
var DEFAULT_BASE_URL = "https://mnexium.com/api/v1";
|
|
774
930
|
var DEFAULT_TIMEOUT = 3e4;
|
|
@@ -798,6 +954,7 @@ var Mnexium = class {
|
|
|
798
954
|
this.profiles = new ProfilesResource(this);
|
|
799
955
|
this.state = new StateResource(this);
|
|
800
956
|
this.prompts = new PromptsResource(this);
|
|
957
|
+
this.records = new RecordsResource(this);
|
|
801
958
|
}
|
|
802
959
|
async process(input) {
|
|
803
960
|
const options = typeof input === "string" ? { content: input } : input;
|
|
@@ -815,6 +972,8 @@ var Mnexium = class {
|
|
|
815
972
|
const maxTokens = options.maxTokens || this.defaults.maxTokens;
|
|
816
973
|
const temperature = options.temperature ?? this.defaults.temperature;
|
|
817
974
|
const regenerateKey = options.regenerateKey ?? this.defaults.regenerateKey ?? false;
|
|
975
|
+
const memoryPolicy = options.memoryPolicy ?? this.defaults.memoryPolicy;
|
|
976
|
+
const records = options.records ?? this.defaults.records;
|
|
818
977
|
const headers = {};
|
|
819
978
|
const provider = detectProvider(model);
|
|
820
979
|
if (provider === "anthropic" && this.anthropicConfig?.apiKey) {
|
|
@@ -828,6 +987,11 @@ var Mnexium = class {
|
|
|
828
987
|
} else if (this.googleConfig?.apiKey) {
|
|
829
988
|
headers["x-google-key"] = this.googleConfig.apiKey;
|
|
830
989
|
}
|
|
990
|
+
if (memoryPolicy === false) {
|
|
991
|
+
headers["x-mnx-memory-policy"] = "false";
|
|
992
|
+
} else if (typeof memoryPolicy === "string" && memoryPolicy.trim()) {
|
|
993
|
+
headers["x-mnx-memory-policy"] = memoryPolicy;
|
|
994
|
+
}
|
|
831
995
|
const body = {
|
|
832
996
|
model,
|
|
833
997
|
messages: [{ role: "user", content: options.content }],
|
|
@@ -845,7 +1009,9 @@ var Mnexium = class {
|
|
|
845
1009
|
summarize,
|
|
846
1010
|
system_prompt: systemPrompt,
|
|
847
1011
|
metadata,
|
|
848
|
-
regenerate_key: regenerateKey
|
|
1012
|
+
regenerate_key: regenerateKey,
|
|
1013
|
+
memory_policy: memoryPolicy,
|
|
1014
|
+
records
|
|
849
1015
|
}
|
|
850
1016
|
};
|
|
851
1017
|
if (options.stream) {
|
|
@@ -878,6 +1044,7 @@ var Mnexium = class {
|
|
|
878
1044
|
} : void 0,
|
|
879
1045
|
provisionedKey: raw.mnx.provisioned_key,
|
|
880
1046
|
claimUrl: raw.mnx.claim_url,
|
|
1047
|
+
records: raw.mnx.records,
|
|
881
1048
|
raw
|
|
882
1049
|
};
|
|
883
1050
|
}
|
|
@@ -968,13 +1135,20 @@ var Mnexium = class {
|
|
|
968
1135
|
}
|
|
969
1136
|
const controller = new AbortController();
|
|
970
1137
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1138
|
+
if (options.signal) {
|
|
1139
|
+
options.signal.addEventListener("abort", () => controller.abort(), { once: true });
|
|
1140
|
+
}
|
|
1141
|
+
let response;
|
|
1142
|
+
try {
|
|
1143
|
+
response = await fetch(url.toString(), {
|
|
1144
|
+
method,
|
|
1145
|
+
headers,
|
|
1146
|
+
body: options.body ? JSON.stringify(options.body) : void 0,
|
|
1147
|
+
signal: controller.signal
|
|
1148
|
+
});
|
|
1149
|
+
} finally {
|
|
1150
|
+
clearTimeout(timeoutId);
|
|
1151
|
+
}
|
|
978
1152
|
const provisionedKey = response.headers.get("x-mnx-key-provisioned");
|
|
979
1153
|
if (provisionedKey) {
|
|
980
1154
|
this._setProvisionedKey(provisionedKey);
|
|
@@ -1007,16 +1181,15 @@ var Mnexium = class {
|
|
|
1007
1181
|
}
|
|
1008
1182
|
let lastError;
|
|
1009
1183
|
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
1184
|
+
const controller = new AbortController();
|
|
1185
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
1010
1186
|
try {
|
|
1011
|
-
const controller = new AbortController();
|
|
1012
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
1013
1187
|
const response = await fetch(url.toString(), {
|
|
1014
1188
|
method,
|
|
1015
1189
|
headers,
|
|
1016
1190
|
body: options.body ? JSON.stringify(options.body) : void 0,
|
|
1017
1191
|
signal: controller.signal
|
|
1018
1192
|
});
|
|
1019
|
-
clearTimeout(timeoutId);
|
|
1020
1193
|
const provisionedKey = response.headers.get("x-mnx-key-provisioned");
|
|
1021
1194
|
if (provisionedKey) {
|
|
1022
1195
|
this._setProvisionedKey(provisionedKey);
|
|
@@ -1025,7 +1198,14 @@ var Mnexium = class {
|
|
|
1025
1198
|
const errorBody = await response.json().catch(() => ({}));
|
|
1026
1199
|
throw this._handleErrorResponse(response.status, errorBody);
|
|
1027
1200
|
}
|
|
1028
|
-
|
|
1201
|
+
if (response.status === 204) {
|
|
1202
|
+
return void 0;
|
|
1203
|
+
}
|
|
1204
|
+
const text = await response.text();
|
|
1205
|
+
if (!text) {
|
|
1206
|
+
return void 0;
|
|
1207
|
+
}
|
|
1208
|
+
return JSON.parse(text);
|
|
1029
1209
|
} catch (error) {
|
|
1030
1210
|
lastError = error;
|
|
1031
1211
|
if (error instanceof APIError && error.status < 500 && !(error instanceof RateLimitError)) {
|
|
@@ -1035,6 +1215,8 @@ var Mnexium = class {
|
|
|
1035
1215
|
throw error;
|
|
1036
1216
|
}
|
|
1037
1217
|
await this._sleep(Math.pow(2, attempt) * 1e3);
|
|
1218
|
+
} finally {
|
|
1219
|
+
clearTimeout(timeoutId);
|
|
1038
1220
|
}
|
|
1039
1221
|
}
|
|
1040
1222
|
throw lastError || new MnexiumError("Request failed");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mnexium/sdk",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Official Mnexium SDK for JavaScript/TypeScript - Add memory to your AI applications",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
20
20
|
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
21
21
|
"test": "vitest",
|
|
22
|
-
"lint": "eslint src/",
|
|
22
|
+
"lint": "eslint --ext .ts src/",
|
|
23
23
|
"prepublishOnly": "npm run build",
|
|
24
24
|
"examples:install": "npm run build && cd examples && npm pkg set dependencies.@mnexium/sdk=file:.. && npm install",
|
|
25
25
|
"example:hello": "cd examples && npm run hello",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"example:claims": "cd examples && npm run claims",
|
|
29
29
|
"example:state": "cd examples && npm run state",
|
|
30
30
|
"example:profile": "cd examples && npm run profile",
|
|
31
|
+
"example:records": "cd examples && npm run records",
|
|
31
32
|
"example:prompts": "cd examples && npm run prompts",
|
|
32
33
|
"example:streaming": "cd examples && npm run streaming",
|
|
33
34
|
"example:events": "cd examples && npm run events",
|
|
@@ -56,6 +57,7 @@
|
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"@types/node": "^20.0.0",
|
|
60
|
+
"@typescript-eslint/parser": "^6.0.0",
|
|
59
61
|
"eslint": "^8.0.0",
|
|
60
62
|
"tsup": "^8.0.0",
|
|
61
63
|
"typescript": "^5.0.0",
|