@x402sentinel/x402 0.1.3 → 0.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.js CHANGED
@@ -922,76 +922,6 @@ function wrapWithSentinel(fetchWithPayment, config) {
922
922
  };
923
923
  return sentinelFetch;
924
924
  }
925
- var FileStorage = class {
926
- filePath;
927
- buffer = [];
928
- flushThreshold;
929
- flushTimer = null;
930
- constructor(filePath = ".valeo/audit.jsonl", flushThreshold = 100) {
931
- this.filePath = filePath;
932
- this.flushThreshold = flushThreshold;
933
- this.ensureDir();
934
- this.startAutoFlush();
935
- }
936
- async write(record) {
937
- this.buffer.push(record);
938
- if (this.buffer.length >= this.flushThreshold) {
939
- await this.flush();
940
- }
941
- }
942
- async query(query) {
943
- await this.flush();
944
- const all = this.readAll();
945
- let results = all.filter((r) => matchesQuery(r, query));
946
- const offset = query.offset ?? 0;
947
- const limit = query.limit ?? results.length;
948
- return results.slice(offset, offset + limit);
949
- }
950
- async summarize(query) {
951
- await this.flush();
952
- const records = this.readAll().filter((r) => matchesQuery(r, query));
953
- return buildSummary(records, query);
954
- }
955
- async count(query) {
956
- await this.flush();
957
- return this.readAll().filter((r) => matchesQuery(r, query)).length;
958
- }
959
- async getById(id) {
960
- await this.flush();
961
- return this.readAll().find((r) => r.id === id) ?? null;
962
- }
963
- /** Write buffered records to disk */
964
- async flush() {
965
- if (this.buffer.length === 0) return;
966
- const lines = this.buffer.map((r) => JSON.stringify(r)).join("\n") + "\n";
967
- appendFileSync(this.filePath, lines, "utf-8");
968
- this.buffer = [];
969
- }
970
- /** Stop the auto-flush timer (for clean shutdown) */
971
- destroy() {
972
- if (this.flushTimer) {
973
- clearInterval(this.flushTimer);
974
- this.flushTimer = null;
975
- }
976
- }
977
- readAll() {
978
- if (!existsSync(this.filePath)) return [];
979
- const content = readFileSync(this.filePath, "utf-8");
980
- return content.split("\n").filter((line) => line.trim().length > 0).map((line) => JSON.parse(line));
981
- }
982
- ensureDir() {
983
- const dir = dirname(this.filePath);
984
- if (!existsSync(dir)) {
985
- mkdirSync(dir, { recursive: true });
986
- }
987
- }
988
- startAutoFlush() {
989
- this.flushTimer = setInterval(() => {
990
- void this.flush();
991
- }, 5e3);
992
- this.flushTimer.unref();
993
- }
994
- };
995
925
 
996
926
  // src/audit/storage/api.ts
997
927
  var ApiStorage = class {
@@ -1113,6 +1043,115 @@ function sleep(ms) {
1113
1043
  return new Promise((resolve) => setTimeout(resolve, ms));
1114
1044
  }
1115
1045
 
1116
- export { ApiStorage, AuditLogger, BudgetManager, FileStorage, MemoryStorage, SentinelAuditError, SentinelBudgetError, SentinelConfigError, SentinelError, conservativePolicy, customPolicy, liberalPolicy, standardPolicy, unlimitedPolicy, validateConfig, wrapWithSentinel };
1046
+ // src/sentinel.ts
1047
+ function sentinel(fetchFn, options) {
1048
+ const agentId = options?.agentId || `agent-${Date.now().toString(36)}`;
1049
+ const apiKey = options?.apiKey || "anonymous";
1050
+ const baseUrl = options?.baseUrl || "https://sentinel.valeocash.com";
1051
+ const wrapped = wrapWithSentinel(fetchFn, {
1052
+ agentId,
1053
+ budget: {
1054
+ maxPerCall: "10.00",
1055
+ maxPerHour: "100.00",
1056
+ maxPerDay: "1000.00"
1057
+ },
1058
+ audit: {
1059
+ storage: new ApiStorage({
1060
+ baseUrl: `${baseUrl}/api/v1`,
1061
+ apiKey
1062
+ })
1063
+ }
1064
+ });
1065
+ if (apiKey === "anonymous") {
1066
+ let hasLoggedOnce = false;
1067
+ const sentinelFetch = async (input, init) => {
1068
+ const response = await wrapped(input, init);
1069
+ if (!hasLoggedOnce && (response.headers.has("payment-response") || response.headers.has("x-payment-response"))) {
1070
+ hasLoggedOnce = true;
1071
+ console.log(`
1072
+ \u2713 Payment tracked by Sentinel
1073
+ Agent: ${agentId}
1074
+ View: ${baseUrl}/agent/${agentId}
1075
+ Claim your data: ${baseUrl}/claim
1076
+ `);
1077
+ }
1078
+ return response;
1079
+ };
1080
+ return sentinelFetch;
1081
+ }
1082
+ return wrapped;
1083
+ }
1084
+ var FileStorage = class {
1085
+ filePath;
1086
+ buffer = [];
1087
+ flushThreshold;
1088
+ flushTimer = null;
1089
+ constructor(filePath = ".valeo/audit.jsonl", flushThreshold = 100) {
1090
+ this.filePath = filePath;
1091
+ this.flushThreshold = flushThreshold;
1092
+ this.ensureDir();
1093
+ this.startAutoFlush();
1094
+ }
1095
+ async write(record) {
1096
+ this.buffer.push(record);
1097
+ if (this.buffer.length >= this.flushThreshold) {
1098
+ await this.flush();
1099
+ }
1100
+ }
1101
+ async query(query) {
1102
+ await this.flush();
1103
+ const all = this.readAll();
1104
+ let results = all.filter((r) => matchesQuery(r, query));
1105
+ const offset = query.offset ?? 0;
1106
+ const limit = query.limit ?? results.length;
1107
+ return results.slice(offset, offset + limit);
1108
+ }
1109
+ async summarize(query) {
1110
+ await this.flush();
1111
+ const records = this.readAll().filter((r) => matchesQuery(r, query));
1112
+ return buildSummary(records, query);
1113
+ }
1114
+ async count(query) {
1115
+ await this.flush();
1116
+ return this.readAll().filter((r) => matchesQuery(r, query)).length;
1117
+ }
1118
+ async getById(id) {
1119
+ await this.flush();
1120
+ return this.readAll().find((r) => r.id === id) ?? null;
1121
+ }
1122
+ /** Write buffered records to disk */
1123
+ async flush() {
1124
+ if (this.buffer.length === 0) return;
1125
+ const lines = this.buffer.map((r) => JSON.stringify(r)).join("\n") + "\n";
1126
+ appendFileSync(this.filePath, lines, "utf-8");
1127
+ this.buffer = [];
1128
+ }
1129
+ /** Stop the auto-flush timer (for clean shutdown) */
1130
+ destroy() {
1131
+ if (this.flushTimer) {
1132
+ clearInterval(this.flushTimer);
1133
+ this.flushTimer = null;
1134
+ }
1135
+ }
1136
+ readAll() {
1137
+ if (!existsSync(this.filePath)) return [];
1138
+ const content = readFileSync(this.filePath, "utf-8");
1139
+ return content.split("\n").filter((line) => line.trim().length > 0).map((line) => JSON.parse(line));
1140
+ }
1141
+ ensureDir() {
1142
+ const dir = dirname(this.filePath);
1143
+ if (!existsSync(dir)) {
1144
+ mkdirSync(dir, { recursive: true });
1145
+ }
1146
+ }
1147
+ startAutoFlush() {
1148
+ this.flushTimer = setInterval(() => {
1149
+ void this.flush();
1150
+ }, 5e3);
1151
+ this.flushTimer.unref();
1152
+ }
1153
+ };
1154
+
1155
+ export { ApiStorage, AuditLogger, BudgetManager, FileStorage, MemoryStorage, SentinelAuditError, SentinelBudgetError, SentinelConfigError, SentinelError, conservativePolicy, customPolicy, liberalPolicy, sentinel, standardPolicy, unlimitedPolicy, validateConfig, wrapWithSentinel };
1117
1156
  //# sourceMappingURL=index.js.map
1118
1157
  //# sourceMappingURL=index.js.map