@coderule/clients 1.2.0 → 1.5.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/README.md +173 -96
- package/dist/index.cjs +333 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +100 -13
- package/dist/index.d.ts +100 -13
- package/dist/index.js +332 -55
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -237,10 +237,26 @@ init_esm_shims();
|
|
|
237
237
|
var SyncHttpClient = class {
|
|
238
238
|
/**
|
|
239
239
|
* Initialize the Sync HTTP client
|
|
240
|
-
* @param
|
|
241
|
-
* @param timeout - Request timeout in milliseconds (default: 60000)
|
|
240
|
+
* @param config.baseUrl - URI/URL for connecting to the HTTP server (e.g., "http://localhost:8002")
|
|
241
|
+
* @param config.timeout - Request timeout in milliseconds (default: 60000)
|
|
242
|
+
* @param config.jwtProvider - Provider for obtaining JWT tokens
|
|
242
243
|
*/
|
|
243
|
-
constructor(
|
|
244
|
+
constructor({
|
|
245
|
+
baseUrl = "http://localhost:8002",
|
|
246
|
+
timeout = 6e4,
|
|
247
|
+
jwtProvider
|
|
248
|
+
}) {
|
|
249
|
+
if (!jwtProvider) {
|
|
250
|
+
throw new Error("SyncHttpClient requires a JWT provider");
|
|
251
|
+
}
|
|
252
|
+
this.timeout = timeout;
|
|
253
|
+
this.jwtProvider = jwtProvider;
|
|
254
|
+
this.configureBase(baseUrl);
|
|
255
|
+
}
|
|
256
|
+
updateBaseUrl(baseUrl) {
|
|
257
|
+
this.configureBase(baseUrl);
|
|
258
|
+
}
|
|
259
|
+
configureBase(uri) {
|
|
244
260
|
let processedUri = uri;
|
|
245
261
|
if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
|
|
246
262
|
processedUri = `http://${uri}`;
|
|
@@ -251,7 +267,6 @@ var SyncHttpClient = class {
|
|
|
251
267
|
} else {
|
|
252
268
|
this.baseUrl = `${url.protocol}//${url.host}`;
|
|
253
269
|
}
|
|
254
|
-
this.timeout = timeout;
|
|
255
270
|
if (this.baseUrl.endsWith("/")) {
|
|
256
271
|
this.apiBase = `${this.baseUrl}sync/v1/`;
|
|
257
272
|
} else {
|
|
@@ -261,14 +276,14 @@ var SyncHttpClient = class {
|
|
|
261
276
|
/**
|
|
262
277
|
* Check the status of a snapshot
|
|
263
278
|
* @param snapshotHash - SHA256 hash of the snapshot
|
|
264
|
-
* @param jwt - JWT token for authorization (required)
|
|
265
279
|
* @returns Snapshot status information
|
|
266
280
|
* @throws Error on HTTP errors or connection errors
|
|
267
281
|
*/
|
|
268
|
-
async checkSnapshotStatus(snapshotHash
|
|
269
|
-
if (!
|
|
270
|
-
throw new Error("
|
|
282
|
+
async checkSnapshotStatus(snapshotHash) {
|
|
283
|
+
if (!snapshotHash) {
|
|
284
|
+
throw new Error("Snapshot hash must be provided");
|
|
271
285
|
}
|
|
286
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
272
287
|
const url = `${this.apiBase}snapshots`;
|
|
273
288
|
try {
|
|
274
289
|
const controller = new AbortController2();
|
|
@@ -306,14 +321,14 @@ var SyncHttpClient = class {
|
|
|
306
321
|
* Create a new snapshot or get its status if it exists
|
|
307
322
|
* @param snapshotHash - SHA256 hash of the snapshot
|
|
308
323
|
* @param files - List of file information with 'file_path' and 'file_hash'
|
|
309
|
-
* @param jwt - JWT token for authorization (required)
|
|
310
324
|
* @returns Snapshot creation result or status
|
|
311
325
|
* @throws Error on HTTP errors or connection errors
|
|
312
326
|
*/
|
|
313
|
-
async createSnapshot(snapshotHash, files
|
|
314
|
-
if (!
|
|
315
|
-
throw new Error("
|
|
327
|
+
async createSnapshot(snapshotHash, files) {
|
|
328
|
+
if (!snapshotHash) {
|
|
329
|
+
throw new Error("Snapshot hash must be provided");
|
|
316
330
|
}
|
|
331
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
317
332
|
const url = `${this.apiBase}snapshots`;
|
|
318
333
|
try {
|
|
319
334
|
const controller = new AbortController2();
|
|
@@ -360,14 +375,11 @@ var SyncHttpClient = class {
|
|
|
360
375
|
/**
|
|
361
376
|
* Upload file content to the service
|
|
362
377
|
* @param filesContent - Map of file_hash to object with 'path' and 'content'
|
|
363
|
-
* @param jwt - JWT token for authorization (required)
|
|
364
378
|
* @returns Upload result with counts
|
|
365
379
|
* @throws Error on HTTP errors or connection errors
|
|
366
380
|
*/
|
|
367
|
-
async uploadFileContent(filesContent
|
|
368
|
-
|
|
369
|
-
throw new Error("JWT must be provided");
|
|
370
|
-
}
|
|
381
|
+
async uploadFileContent(filesContent) {
|
|
382
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
371
383
|
const url = `${this.apiBase}files/content`;
|
|
372
384
|
try {
|
|
373
385
|
const controller = new AbortController2();
|
|
@@ -475,10 +487,28 @@ init_esm_shims();
|
|
|
475
487
|
var RetrievalHttpClient = class {
|
|
476
488
|
/**
|
|
477
489
|
* Initialize the Retrieval HTTP client
|
|
478
|
-
* @param
|
|
479
|
-
* @param timeout - Request timeout in milliseconds (default: 60000)
|
|
490
|
+
* @param config.baseUrl - URI/URL for connecting to the HTTP server (e.g., "http://localhost:8004")
|
|
491
|
+
* @param config.timeout - Request timeout in milliseconds (default: 60000)
|
|
492
|
+
* @param config.jwtProvider - Provider for obtaining JWT tokens
|
|
480
493
|
*/
|
|
481
|
-
constructor(
|
|
494
|
+
constructor({
|
|
495
|
+
baseUrl = "http://localhost:8004",
|
|
496
|
+
timeout = 6e4,
|
|
497
|
+
jwtProvider
|
|
498
|
+
}) {
|
|
499
|
+
if (!jwtProvider) {
|
|
500
|
+
throw new Error("RetrievalHttpClient requires a JWT provider");
|
|
501
|
+
}
|
|
502
|
+
this.timeout = timeout;
|
|
503
|
+
this.jwtProvider = jwtProvider;
|
|
504
|
+
this.configureBase(baseUrl);
|
|
505
|
+
console.debug(`Initialized HTTP client for ${this.baseUrl}`);
|
|
506
|
+
console.debug(`API base: ${this.apiBase}`);
|
|
507
|
+
}
|
|
508
|
+
updateBaseUrl(baseUrl) {
|
|
509
|
+
this.configureBase(baseUrl);
|
|
510
|
+
}
|
|
511
|
+
configureBase(uri) {
|
|
482
512
|
let processedUri = uri;
|
|
483
513
|
if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
|
|
484
514
|
processedUri = `http://${uri}`;
|
|
@@ -489,14 +519,11 @@ var RetrievalHttpClient = class {
|
|
|
489
519
|
} else {
|
|
490
520
|
this.baseUrl = `${url.protocol}//${url.host}`;
|
|
491
521
|
}
|
|
492
|
-
this.timeout = timeout;
|
|
493
522
|
if (this.baseUrl.endsWith("/")) {
|
|
494
523
|
this.apiBase = `${this.baseUrl}api/retrieval/`;
|
|
495
524
|
} else {
|
|
496
525
|
this.apiBase = `${this.baseUrl}/api/retrieval/`;
|
|
497
526
|
}
|
|
498
|
-
console.debug(`Initialized HTTP client for ${this.baseUrl}`);
|
|
499
|
-
console.debug(`API base: ${this.apiBase}`);
|
|
500
527
|
}
|
|
501
528
|
/**
|
|
502
529
|
* Check the health status of the Retrieval service
|
|
@@ -545,25 +572,22 @@ var RetrievalHttpClient = class {
|
|
|
545
572
|
* @param snapshotHash - SHA256 hash of the codebase snapshot
|
|
546
573
|
* @param queryText - Natural language query for retrieval
|
|
547
574
|
* @param budgetTokens - Maximum token budget for results (default: 3000)
|
|
548
|
-
* @param jwt - JWT token for authorization (required)
|
|
549
575
|
* @param options - Optional retrieval parameters
|
|
550
576
|
* @returns Retrieval results with formatted output
|
|
551
577
|
* @throws Error on query failures
|
|
552
578
|
*/
|
|
553
|
-
async query(snapshotHash, queryText, budgetTokens = 3e3,
|
|
579
|
+
async query(snapshotHash, queryText, budgetTokens = 3e3, options) {
|
|
554
580
|
if (!snapshotHash) {
|
|
555
581
|
throw new Error("Snapshot hash must be provided");
|
|
556
582
|
}
|
|
557
583
|
if (!queryText) {
|
|
558
584
|
throw new Error("Query text must be provided");
|
|
559
585
|
}
|
|
560
|
-
if (!jwt) {
|
|
561
|
-
throw new Error("JWT must be provided");
|
|
562
|
-
}
|
|
563
586
|
if (budgetTokens < 100) {
|
|
564
587
|
throw new Error("Budget tokens must be at least 100");
|
|
565
588
|
}
|
|
566
589
|
const startTime = Date.now();
|
|
590
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
567
591
|
const queryEndpoint = `${this.apiBase}query`;
|
|
568
592
|
try {
|
|
569
593
|
const controller = new AbortController2();
|
|
@@ -620,13 +644,11 @@ var RetrievalHttpClient = class {
|
|
|
620
644
|
* @returns Snapshot status information
|
|
621
645
|
* @throws Error on status check failures
|
|
622
646
|
*/
|
|
623
|
-
async checkSnapshotStatus(snapshotHash
|
|
647
|
+
async checkSnapshotStatus(snapshotHash) {
|
|
624
648
|
if (!snapshotHash) {
|
|
625
649
|
throw new Error("Snapshot hash must be provided");
|
|
626
650
|
}
|
|
627
|
-
|
|
628
|
-
throw new Error("JWT must be provided");
|
|
629
|
-
}
|
|
651
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
630
652
|
const statusEndpoint = `${this.apiBase}snapshots/${snapshotHash}/status`;
|
|
631
653
|
try {
|
|
632
654
|
const controller = new AbortController2();
|
|
@@ -671,10 +693,8 @@ var RetrievalHttpClient = class {
|
|
|
671
693
|
* @returns true if cache cleared successfully
|
|
672
694
|
* @throws Error on cache clear failures
|
|
673
695
|
*/
|
|
674
|
-
async clearCache(
|
|
675
|
-
|
|
676
|
-
throw new Error("JWT must be provided");
|
|
677
|
-
}
|
|
696
|
+
async clearCache() {
|
|
697
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
678
698
|
const cacheEndpoint = `${this.apiBase}cache`;
|
|
679
699
|
try {
|
|
680
700
|
const controller = new AbortController2();
|
|
@@ -709,10 +729,8 @@ var RetrievalHttpClient = class {
|
|
|
709
729
|
* @returns Cache statistics
|
|
710
730
|
* @throws Error on stats retrieval failures
|
|
711
731
|
*/
|
|
712
|
-
async getCacheStats(
|
|
713
|
-
|
|
714
|
-
throw new Error("JWT must be provided");
|
|
715
|
-
}
|
|
732
|
+
async getCacheStats() {
|
|
733
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
716
734
|
const statsEndpoint = `${this.apiBase}cache/stats`;
|
|
717
735
|
try {
|
|
718
736
|
const controller = new AbortController2();
|
|
@@ -758,7 +776,7 @@ var RetrievalHttpClient = class {
|
|
|
758
776
|
* @param formatter - Output format "standard" or "compact"
|
|
759
777
|
* @returns Retrieval results
|
|
760
778
|
*/
|
|
761
|
-
async queryWithOptions(snapshotHash, queryText,
|
|
779
|
+
async queryWithOptions(snapshotHash, queryText, budgetTokens = 3e3, flowStrength = 1.5, blendAlpha = 0.8, hopDepth = 2, maxIterations = 12, split = 0.8, formatter = "standard") {
|
|
762
780
|
const options = {
|
|
763
781
|
flow_strength: flowStrength,
|
|
764
782
|
blend_alpha: blendAlpha,
|
|
@@ -767,7 +785,7 @@ var RetrievalHttpClient = class {
|
|
|
767
785
|
split,
|
|
768
786
|
formatter
|
|
769
787
|
};
|
|
770
|
-
return this.query(snapshotHash, queryText, budgetTokens,
|
|
788
|
+
return this.query(snapshotHash, queryText, budgetTokens, options);
|
|
771
789
|
}
|
|
772
790
|
/**
|
|
773
791
|
* Close the HTTP client connection (no-op for fetch)
|
|
@@ -781,10 +799,26 @@ init_esm_shims();
|
|
|
781
799
|
var ASTHttpClient = class {
|
|
782
800
|
/**
|
|
783
801
|
* Initialize the AST HTTP client
|
|
784
|
-
* @param
|
|
785
|
-
* @param timeout - Request timeout in milliseconds (default: 60000)
|
|
802
|
+
* @param config.baseUrl - URI/URL for connecting to the HTTP server (e.g., "http://localhost:8003")
|
|
803
|
+
* @param config.timeout - Request timeout in milliseconds (default: 60000)
|
|
804
|
+
* @param config.jwtProvider - Provider for obtaining JWT tokens
|
|
786
805
|
*/
|
|
787
|
-
constructor(
|
|
806
|
+
constructor({
|
|
807
|
+
baseUrl = "http://localhost:8003",
|
|
808
|
+
timeout = 6e4,
|
|
809
|
+
jwtProvider
|
|
810
|
+
}) {
|
|
811
|
+
if (!jwtProvider) {
|
|
812
|
+
throw new Error("ASTHttpClient requires a JWT provider");
|
|
813
|
+
}
|
|
814
|
+
this.timeout = timeout;
|
|
815
|
+
this.jwtProvider = jwtProvider;
|
|
816
|
+
this.configureBase(baseUrl);
|
|
817
|
+
}
|
|
818
|
+
updateBaseUrl(baseUrl) {
|
|
819
|
+
this.configureBase(baseUrl);
|
|
820
|
+
}
|
|
821
|
+
configureBase(uri) {
|
|
788
822
|
let processedUri = uri;
|
|
789
823
|
if (!uri.startsWith("http://") && !uri.startsWith("https://")) {
|
|
790
824
|
processedUri = `http://${uri}`;
|
|
@@ -795,7 +829,6 @@ var ASTHttpClient = class {
|
|
|
795
829
|
} else {
|
|
796
830
|
this.baseUrl = `${url.protocol}//${url.host}`;
|
|
797
831
|
}
|
|
798
|
-
this.timeout = timeout;
|
|
799
832
|
if (this.baseUrl.endsWith("/")) {
|
|
800
833
|
this.apiBase = `${this.baseUrl}api/ast/`;
|
|
801
834
|
} else {
|
|
@@ -804,14 +837,11 @@ var ASTHttpClient = class {
|
|
|
804
837
|
}
|
|
805
838
|
/**
|
|
806
839
|
* Get file visitor rules in v2 format optimized for Chokidar v4
|
|
807
|
-
* @param jwt - JWT token for authorization (required)
|
|
808
840
|
* @returns Visitor rules with format, include_extensions, include_filenames, exclude_dirnames
|
|
809
841
|
* @throws Error on HTTP errors or connection errors
|
|
810
842
|
*/
|
|
811
|
-
async getVisitorRulesV2(
|
|
812
|
-
|
|
813
|
-
throw new Error("JWT must be provided");
|
|
814
|
-
}
|
|
843
|
+
async getVisitorRulesV2() {
|
|
844
|
+
const jwt = await this.jwtProvider.getJWT();
|
|
815
845
|
const url = `${this.apiBase}visitor-rules`;
|
|
816
846
|
try {
|
|
817
847
|
const controller = new AbortController2();
|
|
@@ -894,7 +924,7 @@ var ASTHttpClient = class {
|
|
|
894
924
|
`(?:^|[\\/])(?:${[...dirs].map(escapeRe).join("|")})(?:[\\/]|$)`,
|
|
895
925
|
"i"
|
|
896
926
|
);
|
|
897
|
-
return { exts, names, dirRe };
|
|
927
|
+
return { exts, names, dirRe, dirs };
|
|
898
928
|
}
|
|
899
929
|
/**
|
|
900
930
|
* Build Chokidar v4 `ignored` predicate from compiled rules
|
|
@@ -904,8 +934,19 @@ var ASTHttpClient = class {
|
|
|
904
934
|
static buildIgnoredPredicate(compiled) {
|
|
905
935
|
return (p, stats) => {
|
|
906
936
|
const posix = p.replace(/\\/g, "/");
|
|
907
|
-
|
|
908
|
-
|
|
937
|
+
const parts = posix.split("/").map((segment) => segment.toLowerCase());
|
|
938
|
+
const isDirectory = stats?.isDirectory?.() ?? false;
|
|
939
|
+
if (isDirectory) {
|
|
940
|
+
const current = parts[parts.length - 1];
|
|
941
|
+
if (compiled.dirs.has(current)) {
|
|
942
|
+
return true;
|
|
943
|
+
}
|
|
944
|
+
return false;
|
|
945
|
+
}
|
|
946
|
+
const parentDirs = parts.slice(0, -1);
|
|
947
|
+
if (parentDirs.some((segment) => compiled.dirs.has(segment))) {
|
|
948
|
+
return true;
|
|
949
|
+
}
|
|
909
950
|
const base = posix.split("/").pop()?.toLowerCase() || "";
|
|
910
951
|
if (compiled.names.has(base)) return false;
|
|
911
952
|
const extIndex = base.lastIndexOf(".");
|
|
@@ -921,6 +962,242 @@ var ASTHttpClient = class {
|
|
|
921
962
|
}
|
|
922
963
|
};
|
|
923
964
|
|
|
924
|
-
|
|
965
|
+
// src/utils/jwt-factory.ts
|
|
966
|
+
init_esm_shims();
|
|
967
|
+
var DEFAULT_MIN_TTL_MS = 5e3;
|
|
968
|
+
var JWTFactory = class {
|
|
969
|
+
constructor(authClient, sourceToken, options = {}) {
|
|
970
|
+
this.authClient = authClient;
|
|
971
|
+
this.sourceToken = sourceToken;
|
|
972
|
+
if (!sourceToken) {
|
|
973
|
+
throw new Error("JWTFactory requires a non-empty source token");
|
|
974
|
+
}
|
|
975
|
+
this.minTtlMs = options.minTtlMs ?? DEFAULT_MIN_TTL_MS;
|
|
976
|
+
this.onTokenRefreshed = options.onTokenRefreshed;
|
|
977
|
+
}
|
|
978
|
+
setSourceToken(token) {
|
|
979
|
+
if (!token) {
|
|
980
|
+
throw new Error("JWTFactory requires a non-empty source token");
|
|
981
|
+
}
|
|
982
|
+
if (token !== this.sourceToken) {
|
|
983
|
+
this.sourceToken = token;
|
|
984
|
+
this.cache = void 0;
|
|
985
|
+
this.currentServerUrl = void 0;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
invalidate() {
|
|
989
|
+
this.cache = void 0;
|
|
990
|
+
this.currentServerUrl = void 0;
|
|
991
|
+
}
|
|
992
|
+
async getJWT(forceRefresh = false) {
|
|
993
|
+
const now = Date.now();
|
|
994
|
+
if (!forceRefresh && this.cache) {
|
|
995
|
+
if (now < this.cache.expiresAt) {
|
|
996
|
+
if (now < this.cache.refreshAt) {
|
|
997
|
+
return this.cache.token;
|
|
998
|
+
}
|
|
999
|
+
} else {
|
|
1000
|
+
this.cache = void 0;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
return this.refresh(now, forceRefresh);
|
|
1004
|
+
}
|
|
1005
|
+
async refresh(now, forceRefresh) {
|
|
1006
|
+
if (!this.refreshPromise) {
|
|
1007
|
+
this.refreshPromise = this.fetchNewToken().finally(() => {
|
|
1008
|
+
this.refreshPromise = void 0;
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
try {
|
|
1012
|
+
return await this.refreshPromise;
|
|
1013
|
+
} catch (error) {
|
|
1014
|
+
if (!forceRefresh && this.cache && now < this.cache.expiresAt) {
|
|
1015
|
+
console.warn(
|
|
1016
|
+
"Failed to refresh JWT, using cached token until expiry. Reason:",
|
|
1017
|
+
error
|
|
1018
|
+
);
|
|
1019
|
+
return this.cache.token;
|
|
1020
|
+
}
|
|
1021
|
+
throw error;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
async fetchNewToken() {
|
|
1025
|
+
const response = await this.authClient.authenticate(this.sourceToken);
|
|
1026
|
+
const fetchedAt = Date.now();
|
|
1027
|
+
const expiresAt = this.resolveExpiryMs(
|
|
1028
|
+
response.jwt,
|
|
1029
|
+
response.expires_at,
|
|
1030
|
+
fetchedAt
|
|
1031
|
+
);
|
|
1032
|
+
const payload = decodeJWT(response.jwt);
|
|
1033
|
+
const serverUrl = this.extractServerUrl(payload);
|
|
1034
|
+
const halfLife = Math.max((expiresAt - fetchedAt) / 2, this.minTtlMs);
|
|
1035
|
+
const refreshAt = fetchedAt + halfLife;
|
|
1036
|
+
this.cache = {
|
|
1037
|
+
token: response.jwt,
|
|
1038
|
+
expiresAt,
|
|
1039
|
+
refreshAt,
|
|
1040
|
+
fetchedAt,
|
|
1041
|
+
serverUrl
|
|
1042
|
+
};
|
|
1043
|
+
this.currentServerUrl = serverUrl;
|
|
1044
|
+
this.emitTokenRefreshed({
|
|
1045
|
+
token: response.jwt,
|
|
1046
|
+
expiresAt,
|
|
1047
|
+
serverUrl
|
|
1048
|
+
});
|
|
1049
|
+
return response.jwt;
|
|
1050
|
+
}
|
|
1051
|
+
resolveExpiryMs(jwt, expiresAt, referenceTime) {
|
|
1052
|
+
if (expiresAt) {
|
|
1053
|
+
const parsed = Date.parse(expiresAt);
|
|
1054
|
+
if (!Number.isNaN(parsed) && parsed > referenceTime + this.minTtlMs) {
|
|
1055
|
+
return parsed;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
const decoded = decodeJWT(jwt);
|
|
1059
|
+
if (decoded?.exp) {
|
|
1060
|
+
const expMs = decoded.exp * 1e3;
|
|
1061
|
+
if (expMs > referenceTime + this.minTtlMs) {
|
|
1062
|
+
return expMs;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
return referenceTime + Math.max(this.minTtlMs, 6e4);
|
|
1066
|
+
}
|
|
1067
|
+
extractServerUrl(payload) {
|
|
1068
|
+
const possibleUrl = payload?.server_url;
|
|
1069
|
+
if (typeof possibleUrl === "string" && possibleUrl.trim()) {
|
|
1070
|
+
return possibleUrl.trim();
|
|
1071
|
+
}
|
|
1072
|
+
return void 0;
|
|
1073
|
+
}
|
|
1074
|
+
emitTokenRefreshed(info) {
|
|
1075
|
+
if (this.onTokenRefreshed) {
|
|
1076
|
+
try {
|
|
1077
|
+
this.onTokenRefreshed(info);
|
|
1078
|
+
} catch (error) {
|
|
1079
|
+
console.warn("JWTFactory onTokenRefreshed callback failed:", error);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
getServerUrl() {
|
|
1084
|
+
return this.currentServerUrl ?? this.cache?.serverUrl;
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
// src/coderule-clients.ts
|
|
1089
|
+
init_esm_shims();
|
|
1090
|
+
var DEFAULT_AUTH_BASE_URL = "https://r.coderule.ai:16803";
|
|
1091
|
+
var DEFAULT_SERVICE_BASE_URL = "https://s1.coderule.ai:16803";
|
|
1092
|
+
var DEFAULT_TIMEOUTS = {
|
|
1093
|
+
auth: 3e4,
|
|
1094
|
+
ast: 6e4,
|
|
1095
|
+
retrieval: 6e4,
|
|
1096
|
+
sync: 6e4
|
|
1097
|
+
};
|
|
1098
|
+
function resolveOverrides(options) {
|
|
1099
|
+
return {
|
|
1100
|
+
auth: options.auth,
|
|
1101
|
+
ast: options.ast,
|
|
1102
|
+
retrieval: options.retrieval,
|
|
1103
|
+
sync: options.sync
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
function resolveTimeout(service, overrides) {
|
|
1107
|
+
return overrides[service]?.timeout ?? DEFAULT_TIMEOUTS[service];
|
|
1108
|
+
}
|
|
1109
|
+
var CoderuleClients = class {
|
|
1110
|
+
constructor(options) {
|
|
1111
|
+
if (!options?.token) {
|
|
1112
|
+
throw new Error("CoderuleClients requires a non-empty token");
|
|
1113
|
+
}
|
|
1114
|
+
const overrides = resolveOverrides(options);
|
|
1115
|
+
const baseUrl = options.baseUrl;
|
|
1116
|
+
const authBase = overrides.auth?.baseUrl ?? baseUrl ?? DEFAULT_AUTH_BASE_URL;
|
|
1117
|
+
const authTimeout = resolveTimeout("auth", overrides);
|
|
1118
|
+
this.auth = new AuthHttpClient(authBase, authTimeout);
|
|
1119
|
+
const userTokenCallback = options.jwtFactory?.onTokenRefreshed;
|
|
1120
|
+
const jwtOptions = {
|
|
1121
|
+
...options.jwtFactory,
|
|
1122
|
+
onTokenRefreshed: (info) => {
|
|
1123
|
+
userTokenCallback?.(info);
|
|
1124
|
+
if (info.serverUrl) {
|
|
1125
|
+
this.applyServerUrl(info.serverUrl);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1129
|
+
this.jwtFactory = new JWTFactory(this.auth, options.token, jwtOptions);
|
|
1130
|
+
this.serviceBaseLocked = {
|
|
1131
|
+
ast: Boolean(overrides.ast?.baseUrl),
|
|
1132
|
+
retrieval: Boolean(overrides.retrieval?.baseUrl),
|
|
1133
|
+
sync: Boolean(overrides.sync?.baseUrl)
|
|
1134
|
+
};
|
|
1135
|
+
const defaultServiceBase = baseUrl ?? DEFAULT_SERVICE_BASE_URL;
|
|
1136
|
+
this.ast = new ASTHttpClient({
|
|
1137
|
+
baseUrl: overrides.ast?.baseUrl ?? defaultServiceBase,
|
|
1138
|
+
timeout: resolveTimeout("ast", overrides),
|
|
1139
|
+
jwtProvider: this.jwtFactory
|
|
1140
|
+
});
|
|
1141
|
+
this.retrieval = new RetrievalHttpClient({
|
|
1142
|
+
baseUrl: overrides.retrieval?.baseUrl ?? defaultServiceBase,
|
|
1143
|
+
timeout: resolveTimeout("retrieval", overrides),
|
|
1144
|
+
jwtProvider: this.jwtFactory
|
|
1145
|
+
});
|
|
1146
|
+
this.sync = new SyncHttpClient({
|
|
1147
|
+
baseUrl: overrides.sync?.baseUrl ?? defaultServiceBase,
|
|
1148
|
+
timeout: resolveTimeout("sync", overrides),
|
|
1149
|
+
jwtProvider: this.jwtFactory
|
|
1150
|
+
});
|
|
1151
|
+
const initialServerUrl = this.jwtFactory.getServerUrl();
|
|
1152
|
+
if (initialServerUrl) {
|
|
1153
|
+
this.applyServerUrl(initialServerUrl);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
get jwt() {
|
|
1157
|
+
return this.jwtFactory;
|
|
1158
|
+
}
|
|
1159
|
+
async getJWT(forceRefresh = false) {
|
|
1160
|
+
return this.jwtFactory.getJWT(forceRefresh);
|
|
1161
|
+
}
|
|
1162
|
+
setToken(token) {
|
|
1163
|
+
this.jwtFactory.setSourceToken(token);
|
|
1164
|
+
}
|
|
1165
|
+
close() {
|
|
1166
|
+
this.ast.close();
|
|
1167
|
+
this.retrieval.close();
|
|
1168
|
+
this.sync.close();
|
|
1169
|
+
this.auth.close();
|
|
1170
|
+
}
|
|
1171
|
+
applyServerUrl(serverUrl) {
|
|
1172
|
+
const trimmed = serverUrl.trim();
|
|
1173
|
+
if (!trimmed || this.lastServerUrl === trimmed) {
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
this.lastServerUrl = trimmed;
|
|
1177
|
+
try {
|
|
1178
|
+
if (!this.serviceBaseLocked.ast) {
|
|
1179
|
+
this.ast.updateBaseUrl(trimmed);
|
|
1180
|
+
}
|
|
1181
|
+
} catch (error) {
|
|
1182
|
+
console.warn("Failed to update AST client base URL:", error);
|
|
1183
|
+
}
|
|
1184
|
+
try {
|
|
1185
|
+
if (!this.serviceBaseLocked.retrieval) {
|
|
1186
|
+
this.retrieval.updateBaseUrl(trimmed);
|
|
1187
|
+
}
|
|
1188
|
+
} catch (error) {
|
|
1189
|
+
console.warn("Failed to update Retrieval client base URL:", error);
|
|
1190
|
+
}
|
|
1191
|
+
try {
|
|
1192
|
+
if (!this.serviceBaseLocked.sync) {
|
|
1193
|
+
this.sync.updateBaseUrl(trimmed);
|
|
1194
|
+
}
|
|
1195
|
+
} catch (error) {
|
|
1196
|
+
console.warn("Failed to update Sync client base URL:", error);
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
export { ASTHttpClient, AuthHttpClient, CoderuleClients, JWTFactory, RetrievalHttpClient, SyncHttpClient, decodeJWT, fetch_wrapper_default as fetch };
|
|
925
1202
|
//# sourceMappingURL=index.js.map
|
|
926
1203
|
//# sourceMappingURL=index.js.map
|