opencode-codebase-index 0.16.0 → 0.17.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.
@@ -655,6 +655,467 @@ var require_ignore = __commonJS({
655
655
  }
656
656
  });
657
657
 
658
+ // node_modules/tiktoken/tiktoken_bg.cjs
659
+ var require_tiktoken_bg = __commonJS({
660
+ "node_modules/tiktoken/tiktoken_bg.cjs"(exports2, module3) {
661
+ "use strict";
662
+ var wasm;
663
+ module3.exports.__wbg_set_wasm = function(val) {
664
+ wasm = val;
665
+ };
666
+ var lTextDecoder = typeof TextDecoder === "undefined" ? (0, module3.require)("util").TextDecoder : TextDecoder;
667
+ var cachedTextDecoder = new lTextDecoder("utf-8", { ignoreBOM: true, fatal: true });
668
+ cachedTextDecoder.decode();
669
+ var cachedUint8ArrayMemory0 = null;
670
+ function getUint8ArrayMemory0() {
671
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
672
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
673
+ }
674
+ return cachedUint8ArrayMemory0;
675
+ }
676
+ function getStringFromWasm0(ptr, len) {
677
+ ptr = ptr >>> 0;
678
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
679
+ }
680
+ var heap = new Array(128).fill(void 0);
681
+ heap.push(void 0, null, true, false);
682
+ var heap_next = heap.length;
683
+ function addHeapObject(obj) {
684
+ if (heap_next === heap.length) heap.push(heap.length + 1);
685
+ const idx = heap_next;
686
+ heap_next = heap[idx];
687
+ heap[idx] = obj;
688
+ return idx;
689
+ }
690
+ function handleError(f, args) {
691
+ try {
692
+ return f.apply(this, args);
693
+ } catch (e) {
694
+ wasm.__wbindgen_export_0(addHeapObject(e));
695
+ }
696
+ }
697
+ function getObject(idx) {
698
+ return heap[idx];
699
+ }
700
+ function dropObject(idx) {
701
+ if (idx < 132) return;
702
+ heap[idx] = heap_next;
703
+ heap_next = idx;
704
+ }
705
+ function takeObject(idx) {
706
+ const ret = getObject(idx);
707
+ dropObject(idx);
708
+ return ret;
709
+ }
710
+ var WASM_VECTOR_LEN = 0;
711
+ var lTextEncoder = typeof TextEncoder === "undefined" ? (0, module3.require)("util").TextEncoder : TextEncoder;
712
+ var cachedTextEncoder = new lTextEncoder("utf-8");
713
+ var encodeString = typeof cachedTextEncoder.encodeInto === "function" ? function(arg, view) {
714
+ return cachedTextEncoder.encodeInto(arg, view);
715
+ } : function(arg, view) {
716
+ const buf = cachedTextEncoder.encode(arg);
717
+ view.set(buf);
718
+ return {
719
+ read: arg.length,
720
+ written: buf.length
721
+ };
722
+ };
723
+ function passStringToWasm0(arg, malloc, realloc) {
724
+ if (realloc === void 0) {
725
+ const buf = cachedTextEncoder.encode(arg);
726
+ const ptr2 = malloc(buf.length, 1) >>> 0;
727
+ getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
728
+ WASM_VECTOR_LEN = buf.length;
729
+ return ptr2;
730
+ }
731
+ let len = arg.length;
732
+ let ptr = malloc(len, 1) >>> 0;
733
+ const mem = getUint8ArrayMemory0();
734
+ let offset = 0;
735
+ for (; offset < len; offset++) {
736
+ const code = arg.charCodeAt(offset);
737
+ if (code > 127) break;
738
+ mem[ptr + offset] = code;
739
+ }
740
+ if (offset !== len) {
741
+ if (offset !== 0) {
742
+ arg = arg.slice(offset);
743
+ }
744
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
745
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
746
+ const ret = encodeString(arg, view);
747
+ offset += ret.written;
748
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
749
+ }
750
+ WASM_VECTOR_LEN = offset;
751
+ return ptr;
752
+ }
753
+ function isLikeNone(x) {
754
+ return x === void 0 || x === null;
755
+ }
756
+ var cachedDataViewMemory0 = null;
757
+ function getDataViewMemory0() {
758
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === void 0 && cachedDataViewMemory0.buffer !== wasm.memory.buffer) {
759
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
760
+ }
761
+ return cachedDataViewMemory0;
762
+ }
763
+ var cachedUint32ArrayMemory0 = null;
764
+ function getUint32ArrayMemory0() {
765
+ if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
766
+ cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
767
+ }
768
+ return cachedUint32ArrayMemory0;
769
+ }
770
+ function getArrayU32FromWasm0(ptr, len) {
771
+ ptr = ptr >>> 0;
772
+ return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
773
+ }
774
+ function passArray8ToWasm0(arg, malloc) {
775
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
776
+ getUint8ArrayMemory0().set(arg, ptr / 1);
777
+ WASM_VECTOR_LEN = arg.length;
778
+ return ptr;
779
+ }
780
+ function passArray32ToWasm0(arg, malloc) {
781
+ const ptr = malloc(arg.length * 4, 4) >>> 0;
782
+ getUint32ArrayMemory0().set(arg, ptr / 4);
783
+ WASM_VECTOR_LEN = arg.length;
784
+ return ptr;
785
+ }
786
+ function getArrayU8FromWasm0(ptr, len) {
787
+ ptr = ptr >>> 0;
788
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
789
+ }
790
+ module3.exports.get_encoding = function(encoding, extend_special_tokens) {
791
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
792
+ try {
793
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
794
+ const ptr0 = passStringToWasm0(encoding, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
795
+ const len0 = WASM_VECTOR_LEN;
796
+ wasm.get_encoding(retptr, ptr0, len0, addHeapObject(extend_special_tokens));
797
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
798
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
799
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
800
+ if (r2) {
801
+ throw takeObject(r1);
802
+ }
803
+ return Tiktoken.__wrap(r0);
804
+ } finally {
805
+ wasm.__wbindgen_add_to_stack_pointer(16);
806
+ }
807
+ };
808
+ module3.exports.encoding_for_model = function(model, extend_special_tokens) {
809
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
810
+ try {
811
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
812
+ const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
813
+ const len0 = WASM_VECTOR_LEN;
814
+ wasm.encoding_for_model(retptr, ptr0, len0, addHeapObject(extend_special_tokens));
815
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
816
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
817
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
818
+ if (r2) {
819
+ throw takeObject(r1);
820
+ }
821
+ return Tiktoken.__wrap(r0);
822
+ } finally {
823
+ wasm.__wbindgen_add_to_stack_pointer(16);
824
+ }
825
+ };
826
+ module3.exports.get_encoding_name_for_model = function(model) {
827
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
828
+ let deferred3_0;
829
+ let deferred3_1;
830
+ try {
831
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
832
+ const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
833
+ const len0 = WASM_VECTOR_LEN;
834
+ wasm.get_encoding_name_for_model(retptr, ptr0, len0);
835
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
836
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
837
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
838
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
839
+ var ptr2 = r0;
840
+ var len2 = r1;
841
+ if (r3) {
842
+ ptr2 = 0;
843
+ len2 = 0;
844
+ throw takeObject(r2);
845
+ }
846
+ deferred3_0 = ptr2;
847
+ deferred3_1 = len2;
848
+ return getStringFromWasm0(ptr2, len2);
849
+ } finally {
850
+ wasm.__wbindgen_add_to_stack_pointer(16);
851
+ wasm.__wbindgen_export_3(deferred3_0, deferred3_1, 1);
852
+ }
853
+ };
854
+ var TiktokenFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
855
+ }, unregister: () => {
856
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_tiktoken_free(ptr >>> 0, 1));
857
+ var Tiktoken = class _Tiktoken {
858
+ /**
859
+ * @param {string} tiktoken_bfe
860
+ * @param {any} special_tokens
861
+ * @param {string} pat_str
862
+ */
863
+ constructor(tiktoken_bfe, special_tokens, pat_str) {
864
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
865
+ const ptr0 = passStringToWasm0(tiktoken_bfe, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
866
+ const len0 = WASM_VECTOR_LEN;
867
+ const ptr1 = passStringToWasm0(pat_str, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
868
+ const len1 = WASM_VECTOR_LEN;
869
+ const ret = wasm.tiktoken_new(ptr0, len0, addHeapObject(special_tokens), ptr1, len1);
870
+ this.__wbg_ptr = ret >>> 0;
871
+ TiktokenFinalization.register(this, this.__wbg_ptr, this);
872
+ return this;
873
+ }
874
+ /** @returns {string | undefined} */
875
+ get name() {
876
+ try {
877
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
878
+ wasm.tiktoken_name(retptr, this.__wbg_ptr);
879
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
880
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
881
+ let v1;
882
+ if (r0 !== 0) {
883
+ v1 = getStringFromWasm0(r0, r1).slice();
884
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
885
+ }
886
+ return v1;
887
+ } finally {
888
+ wasm.__wbindgen_add_to_stack_pointer(16);
889
+ }
890
+ }
891
+ static __wrap(ptr) {
892
+ ptr = ptr >>> 0;
893
+ const obj = Object.create(_Tiktoken.prototype);
894
+ obj.__wbg_ptr = ptr;
895
+ TiktokenFinalization.register(obj, obj.__wbg_ptr, obj);
896
+ return obj;
897
+ }
898
+ __destroy_into_raw() {
899
+ const ptr = this.__wbg_ptr;
900
+ this.__wbg_ptr = 0;
901
+ TiktokenFinalization.unregister(this);
902
+ return ptr;
903
+ }
904
+ free() {
905
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
906
+ const ptr = this.__destroy_into_raw();
907
+ wasm.__wbg_tiktoken_free(ptr, 0);
908
+ }
909
+ /**
910
+ * @param {string} text
911
+ * @param {any} allowed_special
912
+ * @param {any} disallowed_special
913
+ * @returns {Uint32Array}
914
+ */
915
+ encode(text3, allowed_special, disallowed_special) {
916
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
917
+ try {
918
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
919
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
920
+ const len0 = WASM_VECTOR_LEN;
921
+ wasm.tiktoken_encode(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(allowed_special), addHeapObject(disallowed_special));
922
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
923
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
924
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
925
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
926
+ if (r3) {
927
+ throw takeObject(r2);
928
+ }
929
+ var v2 = getArrayU32FromWasm0(r0, r1).slice();
930
+ wasm.__wbindgen_export_3(r0, r1 * 4, 4);
931
+ return v2;
932
+ } finally {
933
+ wasm.__wbindgen_add_to_stack_pointer(16);
934
+ }
935
+ }
936
+ /**
937
+ * @param {string} text
938
+ * @returns {Uint32Array}
939
+ */
940
+ encode_ordinary(text3) {
941
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
942
+ try {
943
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
944
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
945
+ const len0 = WASM_VECTOR_LEN;
946
+ wasm.tiktoken_encode_ordinary(retptr, this.__wbg_ptr, ptr0, len0);
947
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
948
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
949
+ var v2 = getArrayU32FromWasm0(r0, r1).slice();
950
+ wasm.__wbindgen_export_3(r0, r1 * 4, 4);
951
+ return v2;
952
+ } finally {
953
+ wasm.__wbindgen_add_to_stack_pointer(16);
954
+ }
955
+ }
956
+ /**
957
+ * @param {string} text
958
+ * @param {any} allowed_special
959
+ * @param {any} disallowed_special
960
+ * @returns {any}
961
+ */
962
+ encode_with_unstable(text3, allowed_special, disallowed_special) {
963
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
964
+ try {
965
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
966
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
967
+ const len0 = WASM_VECTOR_LEN;
968
+ wasm.tiktoken_encode_with_unstable(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(allowed_special), addHeapObject(disallowed_special));
969
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
970
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
971
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
972
+ if (r2) {
973
+ throw takeObject(r1);
974
+ }
975
+ return takeObject(r0);
976
+ } finally {
977
+ wasm.__wbindgen_add_to_stack_pointer(16);
978
+ }
979
+ }
980
+ /**
981
+ * @param {Uint8Array} bytes
982
+ * @returns {number}
983
+ */
984
+ encode_single_token(bytes) {
985
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
986
+ const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export_1);
987
+ const len0 = WASM_VECTOR_LEN;
988
+ const ret = wasm.tiktoken_encode_single_token(this.__wbg_ptr, ptr0, len0);
989
+ return ret >>> 0;
990
+ }
991
+ /**
992
+ * @param {Uint32Array} tokens
993
+ * @returns {Uint8Array}
994
+ */
995
+ decode(tokens) {
996
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
997
+ try {
998
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
999
+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_export_1);
1000
+ const len0 = WASM_VECTOR_LEN;
1001
+ wasm.tiktoken_decode(retptr, this.__wbg_ptr, ptr0, len0);
1002
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1003
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1004
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
1005
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
1006
+ return v2;
1007
+ } finally {
1008
+ wasm.__wbindgen_add_to_stack_pointer(16);
1009
+ }
1010
+ }
1011
+ /**
1012
+ * @param {number} token
1013
+ * @returns {Uint8Array}
1014
+ */
1015
+ decode_single_token_bytes(token) {
1016
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1017
+ try {
1018
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1019
+ wasm.tiktoken_decode_single_token_bytes(retptr, this.__wbg_ptr, token);
1020
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1021
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1022
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
1023
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
1024
+ return v1;
1025
+ } finally {
1026
+ wasm.__wbindgen_add_to_stack_pointer(16);
1027
+ }
1028
+ }
1029
+ /** @returns {any} */
1030
+ token_byte_values() {
1031
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1032
+ const ret = wasm.tiktoken_token_byte_values(this.__wbg_ptr);
1033
+ return takeObject(ret);
1034
+ }
1035
+ };
1036
+ module3.exports.Tiktoken = Tiktoken;
1037
+ module3.exports.__wbg_parse_def2e24ef1252aff = function() {
1038
+ return handleError(function(arg0, arg1) {
1039
+ const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
1040
+ return addHeapObject(ret);
1041
+ }, arguments);
1042
+ };
1043
+ module3.exports.__wbg_stringify_f7ed6987935b4a24 = function() {
1044
+ return handleError(function(arg0) {
1045
+ const ret = JSON.stringify(getObject(arg0));
1046
+ return addHeapObject(ret);
1047
+ }, arguments);
1048
+ };
1049
+ module3.exports.__wbindgen_error_new = function(arg0, arg1) {
1050
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
1051
+ return addHeapObject(ret);
1052
+ };
1053
+ module3.exports.__wbindgen_is_undefined = function(arg0) {
1054
+ const ret = getObject(arg0) === void 0;
1055
+ return ret;
1056
+ };
1057
+ module3.exports.__wbindgen_object_drop_ref = function(arg0) {
1058
+ takeObject(arg0);
1059
+ };
1060
+ module3.exports.__wbindgen_string_get = function(arg0, arg1) {
1061
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1062
+ const obj = getObject(arg1);
1063
+ const ret = typeof obj === "string" ? obj : void 0;
1064
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
1065
+ var len1 = WASM_VECTOR_LEN;
1066
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1067
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1068
+ };
1069
+ module3.exports.__wbindgen_throw = function(arg0, arg1) {
1070
+ throw new Error(getStringFromWasm0(arg0, arg1));
1071
+ };
1072
+ }
1073
+ });
1074
+
1075
+ // node_modules/tiktoken/tiktoken.cjs
1076
+ var require_tiktoken = __commonJS({
1077
+ "node_modules/tiktoken/tiktoken.cjs"(exports2) {
1078
+ "use strict";
1079
+ var wasm = require_tiktoken_bg();
1080
+ var imports = {};
1081
+ imports["./tiktoken_bg.js"] = wasm;
1082
+ var path17 = require("path");
1083
+ var fs = require("fs");
1084
+ var candidates = __dirname.split(path17.sep).reduce((memo, _, index, array) => {
1085
+ const prefix = array.slice(0, index + 1).join(path17.sep) + path17.sep;
1086
+ if (!prefix.includes("node_modules" + path17.sep)) {
1087
+ memo.unshift(
1088
+ path17.join(
1089
+ prefix,
1090
+ "node_modules",
1091
+ "tiktoken",
1092
+ "",
1093
+ "./tiktoken_bg.wasm"
1094
+ )
1095
+ );
1096
+ }
1097
+ return memo;
1098
+ }, []);
1099
+ candidates.unshift(path17.join(__dirname, "./tiktoken_bg.wasm"));
1100
+ var bytes = null;
1101
+ for (const candidate of candidates) {
1102
+ try {
1103
+ bytes = fs.readFileSync(candidate);
1104
+ break;
1105
+ } catch {
1106
+ }
1107
+ }
1108
+ if (bytes == null) throw new Error("Missing tiktoken_bg.wasm");
1109
+ var wasmModule = new WebAssembly.Module(bytes);
1110
+ var wasmInstance = new WebAssembly.Instance(wasmModule, imports);
1111
+ wasm.__wbg_set_wasm(wasmInstance.exports);
1112
+ exports2["get_encoding"] = wasm["get_encoding"];
1113
+ exports2["encoding_for_model"] = wasm["encoding_for_model"];
1114
+ exports2["get_encoding_name_for_model"] = wasm["get_encoding_name_for_model"];
1115
+ exports2["Tiktoken"] = wasm["Tiktoken"];
1116
+ }
1117
+ });
1118
+
658
1119
  // src/pi-extension.ts
659
1120
  var pi_extension_exports = {};
660
1121
  __export(pi_extension_exports, {
@@ -4955,7 +5416,12 @@ function sameReclaimOwner(left, right) {
4955
5416
  }
4956
5417
  function publishJsonDirectory(finalPath, value) {
4957
5418
  const candidatePath = `${finalPath}.candidate.${process.pid}.${(0, import_crypto.randomUUID)()}`;
4958
- (0, import_fs7.mkdirSync)(candidatePath, { mode: 448 });
5419
+ try {
5420
+ (0, import_fs7.mkdirSync)(candidatePath, { mode: 448 });
5421
+ } catch (error) {
5422
+ if (getErrorCode(error) === "ENOENT") return false;
5423
+ throw error;
5424
+ }
4959
5425
  try {
4960
5426
  (0, import_fs7.writeFileSync)(path12.join(candidatePath, OWNER_FILE_NAME), JSON.stringify(value), {
4961
5427
  encoding: "utf-8",
@@ -4968,8 +5434,12 @@ function publishJsonDirectory(finalPath, value) {
4968
5434
  return true;
4969
5435
  } catch (error) {
4970
5436
  if ((0, import_fs7.existsSync)(finalPath)) return false;
5437
+ if (getErrorCode(error) === "ENOENT") return false;
4971
5438
  throw error;
4972
5439
  }
5440
+ } catch (error) {
5441
+ if (getErrorCode(error) === "ENOENT") return false;
5442
+ throw error;
4973
5443
  } finally {
4974
5444
  if ((0, import_fs7.existsSync)(candidatePath)) (0, import_fs7.rmSync)(candidatePath, { recursive: true, force: true });
4975
5445
  }
@@ -6538,6 +7008,27 @@ function mergeTieredResults(symbolLane, hybridLane, limit) {
6538
7008
  }
6539
7009
  return out;
6540
7010
  }
7011
+ function selectChunksWithFileCoverage(chunks, limit) {
7012
+ if (limit <= 0 || chunks.length === 0) {
7013
+ return [];
7014
+ }
7015
+ if (chunks.length <= limit) {
7016
+ return chunks;
7017
+ }
7018
+ if (limit === 1) {
7019
+ return [chunks[Math.floor((chunks.length - 1) / 2)]];
7020
+ }
7021
+ const selected = [];
7022
+ for (let index = 0; index < limit; index++) {
7023
+ const sourceIndex = Math.round(index * (chunks.length - 1) / (limit - 1));
7024
+ selected.push(chunks[sourceIndex]);
7025
+ }
7026
+ return selected;
7027
+ }
7028
+ function selectIndexableChunks(chunks, limit, semanticOnly) {
7029
+ const indexableChunks = semanticOnly ? chunks.filter((chunk) => chunk.chunkType !== "other") : chunks;
7030
+ return selectChunksWithFileCoverage(indexableChunks, limit);
7031
+ }
6541
7032
  function matchesSearchFilters(candidate, options, minScore) {
6542
7033
  if (candidate.score < minScore) return false;
6543
7034
  if (options?.fileType) {
@@ -8276,7 +8767,6 @@ var Indexer = class {
8276
8767
  const relativePath = path13.relative(this.projectRoot, parsed.path);
8277
8768
  stats.parseFailures.push(relativePath);
8278
8769
  }
8279
- let fileChunkCount = 0;
8280
8770
  let chunksToProcess = parsed.chunks;
8281
8771
  if (this.config.indexing.fallbackToTextOnMaxChunks && chunksToProcess.length > this.config.indexing.maxChunksPerFile) {
8282
8772
  const changedFile = changedFiles.find((f) => f.path === parsed.path);
@@ -8285,13 +8775,12 @@ var Indexer = class {
8285
8775
  chunksToProcess = textChunks;
8286
8776
  }
8287
8777
  }
8778
+ chunksToProcess = selectIndexableChunks(
8779
+ chunksToProcess,
8780
+ this.config.indexing.maxChunksPerFile,
8781
+ this.config.indexing.semanticOnly
8782
+ );
8288
8783
  for (const chunk of chunksToProcess) {
8289
- if (fileChunkCount >= this.config.indexing.maxChunksPerFile) {
8290
- break;
8291
- }
8292
- if (this.config.indexing.semanticOnly && chunk.chunkType === "other") {
8293
- continue;
8294
- }
8295
8784
  const id = generateChunkId(parsed.path, chunk);
8296
8785
  const contentHash = generateChunkHash(chunk);
8297
8786
  const existingContentHash = existingChunks.get(id);
@@ -8315,7 +8804,6 @@ var Indexer = class {
8315
8804
  blameSummary: blameMetadata.blameSummary
8316
8805
  });
8317
8806
  if (existingContentHash === contentHash) {
8318
- fileChunkCount++;
8319
8807
  continue;
8320
8808
  }
8321
8809
  const texts = createEmbeddingTexts(chunk, parsed.path, getSafeEmbeddingChunkTokenLimit(configuredProviderInfo)).map((text3) => ({
@@ -8340,7 +8828,6 @@ var Indexer = class {
8340
8828
  contentHash,
8341
8829
  metadata
8342
8830
  });
8343
- fileChunkCount++;
8344
8831
  }
8345
8832
  }
8346
8833
  const retryableFailedChunks = this.collectRetryableFailedChunks(
@@ -9931,6 +10418,159 @@ function findKnowledgeBasePathIndex(knowledgeBases, inputPath, projectRoot3) {
9931
10418
  }
9932
10419
 
9933
10420
  // src/tools/utils.ts
10421
+ var import_tiktoken = __toESM(require_tiktoken(), 1);
10422
+ var MIN_CONTEXT_PACK_TOKEN_BUDGET = 128;
10423
+ var MAX_CONTEXT_PACK_TOKEN_BUDGET = 4e3;
10424
+ var DEFAULT_CONTEXT_PACK_TOKEN_BUDGET = 1200;
10425
+ var CONTEXT_TOKENIZER = (0, import_tiktoken.get_encoding)("cl100k_base");
10426
+ function clampContextPackTokenBudget(tokenBudget) {
10427
+ if (tokenBudget === void 0 || !Number.isFinite(tokenBudget)) {
10428
+ return DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
10429
+ }
10430
+ return Math.min(
10431
+ MAX_CONTEXT_PACK_TOKEN_BUDGET,
10432
+ Math.max(MIN_CONTEXT_PACK_TOKEN_BUDGET, Math.floor(tokenBudget))
10433
+ );
10434
+ }
10435
+ function countContextTokens(text3) {
10436
+ return CONTEXT_TOKENIZER.encode(text3).length;
10437
+ }
10438
+ function fitTextToContextBudget(text3, tokenBudget) {
10439
+ const normalizedBudget = clampContextPackTokenBudget(tokenBudget);
10440
+ const tokenEstimate = countContextTokens(text3);
10441
+ if (tokenEstimate <= normalizedBudget) {
10442
+ return {
10443
+ text: text3,
10444
+ tokenBudget: normalizedBudget,
10445
+ tokenEstimate,
10446
+ truncated: false
10447
+ };
10448
+ }
10449
+ const suffix = "\n...[truncated to context token budget]";
10450
+ const codePoints = Array.from(text3);
10451
+ let low = 0;
10452
+ let high = codePoints.length;
10453
+ while (low < high) {
10454
+ const middle = Math.ceil((low + high) / 2);
10455
+ const candidate = `${codePoints.slice(0, middle).join("").trimEnd()}${suffix}`;
10456
+ if (countContextTokens(candidate) <= normalizedBudget) low = middle;
10457
+ else high = middle - 1;
10458
+ }
10459
+ const fitted = `${codePoints.slice(0, low).join("").trimEnd()}${suffix}`;
10460
+ return {
10461
+ text: fitted,
10462
+ tokenBudget: normalizedBudget,
10463
+ tokenEstimate: countContextTokens(fitted),
10464
+ truncated: true
10465
+ };
10466
+ }
10467
+ function normalizedLineRange(result) {
10468
+ return result.startLine <= result.endLine ? { start: result.startLine, end: result.endLine } : { start: result.endLine, end: result.startLine };
10469
+ }
10470
+ function rankContextCandidates(results) {
10471
+ return results.map((result, originalIndex) => ({ result, originalIndex })).sort((left, right) => right.result.score - left.result.score || left.originalIndex - right.originalIndex);
10472
+ }
10473
+ function deduplicateContextCandidates(candidates) {
10474
+ const acceptedByFile = /* @__PURE__ */ new Map();
10475
+ const deduplicated = [];
10476
+ for (const { result } of candidates) {
10477
+ const range = normalizedLineRange(result);
10478
+ const accepted = acceptedByFile.get(result.filePath) ?? [];
10479
+ if (accepted.some((item) => item.start <= range.end && range.start <= item.end)) {
10480
+ continue;
10481
+ }
10482
+ accepted.push(range);
10483
+ acceptedByFile.set(result.filePath, accepted);
10484
+ deduplicated.push(result);
10485
+ }
10486
+ return deduplicated;
10487
+ }
10488
+ function diversifyContextCandidates(results) {
10489
+ const byFile = /* @__PURE__ */ new Map();
10490
+ for (const result of results) {
10491
+ const bucket = byFile.get(result.filePath) ?? [];
10492
+ bucket.push(result);
10493
+ byFile.set(result.filePath, bucket);
10494
+ }
10495
+ const files = [...byFile.keys()];
10496
+ const diversified = [];
10497
+ for (let depth = 0; diversified.length < results.length; depth += 1) {
10498
+ for (const file of files) {
10499
+ const result = byFile.get(file)?.[depth];
10500
+ if (result) diversified.push(result);
10501
+ }
10502
+ }
10503
+ return diversified;
10504
+ }
10505
+ function compactEvidenceValue(value, maxChars) {
10506
+ const characters = [...value];
10507
+ if (characters.length <= maxChars) return value;
10508
+ return `\u2026${characters.slice(-(maxChars - 1)).join("")}`;
10509
+ }
10510
+ function formatContextEvidence(result, index) {
10511
+ const symbol = result.name ? ` ${JSON.stringify(compactEvidenceValue(result.name, 80))}` : "";
10512
+ const path17 = compactEvidenceValue(result.filePath, 120);
10513
+ return `[${index}] ${result.chunkType}${symbol} in ${path17}:${result.startLine}-${result.endLine} (score ${result.score.toFixed(2)})`;
10514
+ }
10515
+ function formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, budgetOmittedCount) {
10516
+ const lines = selected.map((result, index) => formatContextEvidence(result, index + 1));
10517
+ const notes = [];
10518
+ if (duplicateCount > 0) notes.push(`${duplicateCount} overlapping duplicate${duplicateCount === 1 ? "" : "s"} removed`);
10519
+ if (limitOmittedCount > 0) notes.push(`${limitOmittedCount} additional result${limitOmittedCount === 1 ? "" : "s"} excluded by result limit`);
10520
+ if (budgetOmittedCount > 0) notes.push(`${budgetOmittedCount} additional result${budgetOmittedCount === 1 ? "" : "s"} omitted by token budget`);
10521
+ const footer = notes.length > 0 ? `Selected ${selected.length} of ${candidateCount} candidates; ${notes.join("; ")}.` : `Selected ${selected.length} of ${candidateCount} candidates.`;
10522
+ return `${heading}
10523
+
10524
+ ${lines.join("\n")}
10525
+
10526
+ ${footer}`;
10527
+ }
10528
+ function buildContextPack(results, options = {}) {
10529
+ const requestedTokenBudget = options.tokenBudget ?? DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
10530
+ const tokenBudget = clampContextPackTokenBudget(options.tokenBudget);
10531
+ const heading = compactEvidenceValue(options.heading?.trim() || "Codebase evidence", 160);
10532
+ const maxResults = Math.max(0, Math.floor(options.maxResults ?? results.length));
10533
+ const candidateCount = results.length;
10534
+ const deduplicated = deduplicateContextCandidates(rankContextCandidates(results));
10535
+ const diversified = diversifyContextCandidates(deduplicated);
10536
+ const duplicateCount = candidateCount - deduplicated.length;
10537
+ const selectable = diversified.slice(0, maxResults);
10538
+ const limitOmittedCount = deduplicated.length - selectable.length;
10539
+ let selected = [];
10540
+ let text3 = formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, selectable.length);
10541
+ for (let count = 1; count <= selectable.length; count += 1) {
10542
+ const candidateSelection = selectable.slice(0, count);
10543
+ const budgetOmittedCount2 = selectable.length - candidateSelection.length;
10544
+ const candidateText = formatContextPack(
10545
+ heading,
10546
+ candidateSelection,
10547
+ candidateCount,
10548
+ duplicateCount,
10549
+ limitOmittedCount,
10550
+ budgetOmittedCount2
10551
+ );
10552
+ if (countContextTokens(candidateText) > tokenBudget) break;
10553
+ selected = candidateSelection;
10554
+ text3 = candidateText;
10555
+ }
10556
+ const fitted = fitTextToContextBudget(text3, tokenBudget);
10557
+ const budgetOmittedCount = selectable.length - selected.length;
10558
+ const omittedCount = candidateCount - selected.length;
10559
+ return {
10560
+ requestedTokenBudget,
10561
+ tokenBudget,
10562
+ text: fitted.text,
10563
+ tokenEstimate: fitted.tokenEstimate,
10564
+ results: selected,
10565
+ candidateCount,
10566
+ deduplicatedCount: deduplicated.length,
10567
+ selectedCount: selected.length,
10568
+ omittedCount,
10569
+ duplicateCount,
10570
+ limitOmittedCount,
10571
+ budgetOmittedCount
10572
+ };
10573
+ }
9934
10574
  var MAX_CONTENT_LINES = 30;
9935
10575
  function truncateContent(content) {
9936
10576
  const lines = content.split("\n");
@@ -10075,17 +10715,6 @@ function calculatePercentage(progress) {
10075
10715
  if (progress.phase === "storing") return 95;
10076
10716
  return 0;
10077
10717
  }
10078
- function formatCodebasePeek(results) {
10079
- if (results.length === 0) {
10080
- return "No matching code found. Try a different query or run index_codebase first.";
10081
- }
10082
- const formatted = results.map((r, idx) => {
10083
- const location = `${r.filePath}:${r.startLine}-${r.endLine}`;
10084
- const name = r.name ? `"${r.name}"` : "(anonymous)";
10085
- return `[${idx + 1}] ${r.chunkType} ${name} at ${location} (score: ${r.score.toFixed(2)})${formatBlame(r)}`;
10086
- });
10087
- return formatted.join("\n");
10088
- }
10089
10718
  function formatHealthCheck(result) {
10090
10719
  if (result.resetCorruptedIndex) {
10091
10720
  return result.warning ?? "Detected a corrupted local index and reset it. Run index_codebase to rebuild search data.";
@@ -10599,6 +11228,253 @@ Run /index to rebuild the index without the removed knowledge base.`;
10599
11228
  return result;
10600
11229
  }
10601
11230
 
11231
+ // src/tools/symbol-inference.ts
11232
+ var IDENTIFIER_RE = /[A-Za-z_$][A-Za-z0-9_$]*/g;
11233
+ var QUOTED_BACKTICK_RE = /`([^`]+)`/g;
11234
+ var QUOTED_SINGLE_RE = /'([^'\\]+)'/g;
11235
+ var QUOTED_DOUBLE_RE = /"([^"]+)"/g;
11236
+ var SYMBOL_LIKE_RE = /^(?:[A-Za-z_$][A-Za-z0-9_$]*)$/;
11237
+ var CAMEL_CASE_RE = /^[a-z_][A-Za-z0-9_$]*[A-Z][A-Za-z0-9_$]*$/;
11238
+ var PASCAL_CASE_RE = /^[A-Z][A-Za-z0-9_$]*$/;
11239
+ var SNAKE_CASE_RE = /^[a-z][a-z0-9_]*_[a-z0-9_]+$/;
11240
+ var DEFINITION_INTENT_RE = /\b(where|defined|definition|define|declaration|symbol|function|method|class|interface|type)\b/i;
11241
+ var STOP_WORDS = /* @__PURE__ */ new Set([
11242
+ "a",
11243
+ "an",
11244
+ "and",
11245
+ "are",
11246
+ "at",
11247
+ "for",
11248
+ "find",
11249
+ "how",
11250
+ "i",
11251
+ "in",
11252
+ "is",
11253
+ "it",
11254
+ "of",
11255
+ "on",
11256
+ "that",
11257
+ "the",
11258
+ "definition",
11259
+ "show",
11260
+ "to",
11261
+ "where",
11262
+ "which",
11263
+ "what",
11264
+ "you",
11265
+ "your",
11266
+ "with"
11267
+ ]);
11268
+ function stripCallSuffix(token) {
11269
+ return token.replace(/\(\s*\)$/, "");
11270
+ }
11271
+ function isLikelySymbolName(token) {
11272
+ if (!SYMBOL_LIKE_RE.test(token)) {
11273
+ return false;
11274
+ }
11275
+ if (STOP_WORDS.has(token.toLowerCase())) {
11276
+ return false;
11277
+ }
11278
+ return CAMEL_CASE_RE.test(token) || PASCAL_CASE_RE.test(token) || SNAKE_CASE_RE.test(token);
11279
+ }
11280
+ function extractQuotedIdentifiers(query) {
11281
+ const identifiers = /* @__PURE__ */ new Set();
11282
+ for (const match of query.matchAll(QUOTED_BACKTICK_RE)) {
11283
+ const candidate = stripCallSuffix(match[1].trim());
11284
+ if (candidate && isLikelySymbolName(candidate)) {
11285
+ identifiers.add(candidate);
11286
+ }
11287
+ }
11288
+ for (const match of query.matchAll(QUOTED_SINGLE_RE)) {
11289
+ const candidate = stripCallSuffix(match[1].trim());
11290
+ if (candidate && isLikelySymbolName(candidate)) {
11291
+ identifiers.add(candidate);
11292
+ }
11293
+ }
11294
+ for (const match of query.matchAll(QUOTED_DOUBLE_RE)) {
11295
+ const candidate = stripCallSuffix(match[1].trim());
11296
+ if (candidate && isLikelySymbolName(candidate)) {
11297
+ identifiers.add(candidate);
11298
+ }
11299
+ }
11300
+ return [...identifiers];
11301
+ }
11302
+ function extractBareIdentifiers(query) {
11303
+ const unquoted = query.replace(QUOTED_BACKTICK_RE, " ").replace(QUOTED_SINGLE_RE, " ").replace(QUOTED_DOUBLE_RE, " ");
11304
+ const identifiers = /* @__PURE__ */ new Set();
11305
+ for (const match of unquoted.matchAll(IDENTIFIER_RE)) {
11306
+ const candidate = stripCallSuffix(match[0]);
11307
+ if (isLikelySymbolName(candidate)) {
11308
+ identifiers.add(candidate);
11309
+ }
11310
+ }
11311
+ return [...identifiers];
11312
+ }
11313
+ function isSingleMeaningfulToken(query, symbol) {
11314
+ const tokens = query.replace(/[`'"()]/g, " ").split(/[^A-Za-z0-9_$]+/).map((token) => token.trim().toLowerCase()).filter((token) => token.length > 0).filter((token) => !STOP_WORDS.has(token));
11315
+ return tokens.length === 1 && tokens[0] === symbol.toLowerCase();
11316
+ }
11317
+ function inferExactSymbolFromQuery(query) {
11318
+ const quoted = extractQuotedIdentifiers(query);
11319
+ if (quoted.length === 1) {
11320
+ return quoted[0];
11321
+ }
11322
+ if (quoted.length > 1) {
11323
+ return void 0;
11324
+ }
11325
+ const candidates = extractBareIdentifiers(query);
11326
+ if (candidates.length !== 1) {
11327
+ return void 0;
11328
+ }
11329
+ const candidate = candidates[0];
11330
+ if (DEFINITION_INTENT_RE.test(query)) {
11331
+ return candidate;
11332
+ }
11333
+ if (isSingleMeaningfulToken(query, candidate)) {
11334
+ return candidate;
11335
+ }
11336
+ return void 0;
11337
+ }
11338
+
11339
+ // src/tools/context.ts
11340
+ var MIN_CONTEXT_RESULT_LIMIT = 1;
11341
+ var MAX_CONTEXT_RESULT_LIMIT = 100;
11342
+ var MIN_CONTEXT_PATH_DEPTH = 1;
11343
+ var MAX_CONTEXT_PATH_DEPTH = 100;
11344
+ function locations(results) {
11345
+ return results.map((result) => ({
11346
+ filePath: result.filePath,
11347
+ startLine: result.startLine,
11348
+ endLine: result.endLine,
11349
+ score: result.score,
11350
+ chunkType: result.chunkType,
11351
+ name: result.name
11352
+ }));
11353
+ }
11354
+ function packedResult(route, routedQuery, pack) {
11355
+ return {
11356
+ text: pack.text,
11357
+ details: {
11358
+ route,
11359
+ routedQuery,
11360
+ tokenBudget: pack.tokenBudget,
11361
+ tokenEstimate: pack.tokenEstimate,
11362
+ candidateCount: pack.candidateCount,
11363
+ deduplicatedCount: pack.deduplicatedCount,
11364
+ selectedCount: pack.selectedCount,
11365
+ omittedCount: pack.omittedCount,
11366
+ duplicateCount: pack.duplicateCount,
11367
+ limitOmittedCount: pack.limitOmittedCount,
11368
+ budgetOmittedCount: pack.budgetOmittedCount,
11369
+ results: locations(pack.results)
11370
+ }
11371
+ };
11372
+ }
11373
+ async function resolveSearchContext(input, operations) {
11374
+ const explicitSymbol = input.symbol ?? void 0;
11375
+ const limit = input.limit ?? 10;
11376
+ const tokenBudget = input.tokenBudget ?? void 0;
11377
+ const lookupSymbol = explicitSymbol ?? inferExactSymbolFromQuery(input.query);
11378
+ if (lookupSymbol) {
11379
+ const definitions = await operations.lookup(lookupSymbol, MAX_CONTEXT_RESULT_LIMIT);
11380
+ if (definitions.length > 0) {
11381
+ return packedResult("definition", lookupSymbol, buildContextPack(definitions, {
11382
+ tokenBudget,
11383
+ maxResults: limit,
11384
+ heading: `Definition evidence for ${JSON.stringify(lookupSymbol)}`
11385
+ }));
11386
+ }
11387
+ if (explicitSymbol) {
11388
+ const fitted = fitTextToContextBudget(
11389
+ formatDefinitionLookup(definitions, lookupSymbol),
11390
+ tokenBudget
11391
+ );
11392
+ return { text: fitted.text };
11393
+ }
11394
+ }
11395
+ const results = await operations.search(input.query, MAX_CONTEXT_RESULT_LIMIT);
11396
+ if (results.length === 0) {
11397
+ const fitted = fitTextToContextBudget(
11398
+ "No matching code found. Try a different query or run index_codebase first.",
11399
+ tokenBudget
11400
+ );
11401
+ return { text: fitted.text };
11402
+ }
11403
+ return packedResult("conceptual", input.query, buildContextPack(results, {
11404
+ tokenBudget,
11405
+ maxResults: limit,
11406
+ heading: `Codebase evidence for ${JSON.stringify(input.query)}`
11407
+ }));
11408
+ }
11409
+ function fittedDetails(route, fitted) {
11410
+ return {
11411
+ route,
11412
+ tokenBudget: fitted.tokenBudget,
11413
+ tokenEstimate: fitted.tokenEstimate,
11414
+ truncated: fitted.truncated
11415
+ };
11416
+ }
11417
+ async function resolveCodebaseContext(projectRoot3, host, input) {
11418
+ const from = input.from ?? void 0;
11419
+ const to = input.to ?? void 0;
11420
+ const symbol = input.symbol ?? void 0;
11421
+ const limit = input.limit ?? 10;
11422
+ const maxDepth = input.maxDepth ?? 10;
11423
+ const fileType = input.fileType ?? void 0;
11424
+ const directory = input.directory ?? void 0;
11425
+ const tokenBudget = input.tokenBudget ?? void 0;
11426
+ if (from && to) {
11427
+ const path17 = await getCallGraphPath(projectRoot3, host, from, to, maxDepth);
11428
+ if (path17.length > 0) {
11429
+ const fitted2 = fitTextToContextBudget(
11430
+ formatCallGraphPath(from, to, path17),
11431
+ tokenBudget
11432
+ );
11433
+ return {
11434
+ text: fitted2.text,
11435
+ details: fittedDetails("path", fitted2)
11436
+ };
11437
+ }
11438
+ const { callers } = await getCallGraphData(projectRoot3, host, {
11439
+ name: to,
11440
+ direction: "callers"
11441
+ });
11442
+ const directEdge = callers.find((edge) => edge.fromSymbolName === from);
11443
+ if (directEdge) {
11444
+ const location = directEdge.fromSymbolFilePath ? ` at ${directEdge.fromSymbolFilePath}:${directEdge.line}` : "";
11445
+ const fitted2 = fitTextToContextBudget(
11446
+ `Direct path: ${from} --${directEdge.callType}--> ${to}${location} (edge is ${directEdge.isResolved ? "resolved" : "unresolved"}).`,
11447
+ tokenBudget
11448
+ );
11449
+ return {
11450
+ text: fitted2.text,
11451
+ details: fittedDetails("direct-edge", fitted2)
11452
+ };
11453
+ }
11454
+ const fitted = fitTextToContextBudget(
11455
+ formatCallGraphPath(from, to, path17),
11456
+ tokenBudget
11457
+ );
11458
+ return {
11459
+ text: fitted.text,
11460
+ details: fittedDetails("path", fitted)
11461
+ };
11462
+ }
11463
+ return resolveSearchContext({ query: input.query, symbol, limit, tokenBudget }, {
11464
+ lookup: (lookupSymbol, retrievalLimit) => implementationLookup(projectRoot3, host, lookupSymbol, {
11465
+ limit: retrievalLimit,
11466
+ fileType,
11467
+ directory
11468
+ }),
11469
+ search: (query, retrievalLimit) => searchCodebase(projectRoot3, host, query, {
11470
+ limit: retrievalLimit,
11471
+ fileType,
11472
+ directory,
11473
+ metadataOnly: true
11474
+ })
11475
+ });
11476
+ }
11477
+
10602
11478
  // src/pi-call-graph.ts
10603
11479
  var import_typebox = require("typebox");
10604
11480
  var HOST = "pi";
@@ -10675,58 +11551,33 @@ function codebaseIndexPiExtension(pi) {
10675
11551
  pi.registerTool({
10676
11552
  name: "codebase_context",
10677
11553
  label: "Codebase Context",
10678
- description: "PREFERRED FIRST TOOL for any repository question. Check index_status when freshness is unknown, then use this tool for low-token location discovery and dependency flow.",
11554
+ description: "PREFERRED FIRST TOOL for any repository question. Returns a deduplicated, file-diverse evidence pack within tokenBudget. Check index_status when freshness is unknown, then use this tool for low-token location discovery and dependency flow.",
10679
11555
  parameters: import_typebox2.Type.Object({
10680
11556
  query: import_typebox2.Type.String({ description: "Natural language description of what code you're trying to locate" }),
10681
- from: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Source symbol when asking for a dependency path." })),
10682
- to: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Target symbol when asking for a dependency path." })),
10683
- symbol: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Exact symbol name for an authoritative definition lookup." })),
10684
- limit: import_typebox2.Type.Optional(import_typebox2.Type.Number({ description: "Maximum results (default: 10)" })),
10685
- maxDepth: import_typebox2.Type.Optional(import_typebox2.Type.Number({ description: "Maximum call-graph traversal depth for from/to paths" })),
10686
- fileType: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Filter by file extension, e.g., ts, py, rs" })),
10687
- directory: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Filter by directory path" }))
11557
+ from: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Source symbol when asking for a dependency path." })),
11558
+ to: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Target symbol when asking for a dependency path." })),
11559
+ symbol: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Exact symbol name for an authoritative definition lookup." })),
11560
+ limit: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11561
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_RESULT_LIMIT, maximum: MAX_CONTEXT_RESULT_LIMIT }),
11562
+ import_typebox2.Type.Null()
11563
+ ], { default: 10, description: `Maximum results (${MIN_CONTEXT_RESULT_LIMIT}-${MAX_CONTEXT_RESULT_LIMIT})` })),
11564
+ maxDepth: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11565
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_PATH_DEPTH, maximum: MAX_CONTEXT_PATH_DEPTH }),
11566
+ import_typebox2.Type.Null()
11567
+ ], { default: 10, description: `Maximum call-graph traversal depth (${MIN_CONTEXT_PATH_DEPTH}-${MAX_CONTEXT_PATH_DEPTH})` })),
11568
+ fileType: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Filter by file extension, e.g., ts, py, rs" })),
11569
+ directory: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Filter by directory path" })),
11570
+ tokenBudget: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11571
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_PACK_TOKEN_BUDGET, maximum: MAX_CONTEXT_PACK_TOKEN_BUDGET }),
11572
+ import_typebox2.Type.Null()
11573
+ ], {
11574
+ default: DEFAULT_CONTEXT_PACK_TOKEN_BUDGET,
11575
+ description: `Maximum response tokens (${MIN_CONTEXT_PACK_TOKEN_BUDGET}-${MAX_CONTEXT_PACK_TOKEN_BUDGET})`
11576
+ }))
10688
11577
  }),
10689
11578
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
10690
- const root = projectRoot2(ctx);
10691
- if (params.from && params.to) {
10692
- const path17 = await getCallGraphPath(root, HOST2, params.from, params.to, params.maxDepth);
10693
- if (path17.length > 0) {
10694
- return text2(formatCallGraphPath(params.from, params.to, path17), path17);
10695
- }
10696
- const { callers } = await getCallGraphData(root, HOST2, {
10697
- name: params.to,
10698
- direction: "callers"
10699
- });
10700
- const directEdge = callers.find((edge) => edge.fromSymbolName === params.from);
10701
- if (directEdge) {
10702
- const location = directEdge.fromSymbolFilePath ? ` at ${directEdge.fromSymbolFilePath}:${directEdge.line}` : "";
10703
- return text2(
10704
- `Direct path: ${params.from} --${directEdge.callType}--> ${params.to}${location} (edge is ${directEdge.isResolved ? "resolved" : "unresolved"}).`,
10705
- path17
10706
- );
10707
- }
10708
- return text2(formatCallGraphPath(params.from, params.to, path17), path17);
10709
- }
10710
- if (params.symbol) {
10711
- const results2 = await implementationLookup(root, HOST2, params.symbol, {
10712
- limit: params.limit ?? 10,
10713
- fileType: params.fileType,
10714
- directory: params.directory
10715
- });
10716
- return text2(formatDefinitionLookup(results2, params.symbol), results2);
10717
- }
10718
- const results = await searchCodebase(root, HOST2, params.query, {
10719
- limit: params.limit ?? 10,
10720
- fileType: params.fileType,
10721
- directory: params.directory,
10722
- metadataOnly: true
10723
- });
10724
- if (results.length === 0) {
10725
- return text2("No matching code found. Try a different query or run index_status/index_codebase first.");
10726
- }
10727
- return text2(`Found ${results.length} locations for "${params.query}":
10728
-
10729
- ${formatCodebasePeek(results)}`, results);
11579
+ const result = await resolveCodebaseContext(projectRoot2(ctx), HOST2, params);
11580
+ return text2(result.text, result.details);
10730
11581
  }
10731
11582
  });
10732
11583
  pi.registerTool({