opencode-codebase-index 0.15.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.
@@ -6,7 +6,13 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __commonJS = (cb, mod) => function __require() {
9
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
14
+ });
15
+ var __commonJS = (cb, mod) => function __require2() {
10
16
  try {
11
17
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12
18
  } catch (e) {
@@ -650,6 +656,467 @@ var require_ignore = __commonJS({
650
656
  }
651
657
  });
652
658
 
659
+ // node_modules/tiktoken/tiktoken_bg.cjs
660
+ var require_tiktoken_bg = __commonJS({
661
+ "node_modules/tiktoken/tiktoken_bg.cjs"(exports, module2) {
662
+ "use strict";
663
+ var wasm;
664
+ module2.exports.__wbg_set_wasm = function(val) {
665
+ wasm = val;
666
+ };
667
+ var lTextDecoder = typeof TextDecoder === "undefined" ? (0, module2.require)("util").TextDecoder : TextDecoder;
668
+ var cachedTextDecoder = new lTextDecoder("utf-8", { ignoreBOM: true, fatal: true });
669
+ cachedTextDecoder.decode();
670
+ var cachedUint8ArrayMemory0 = null;
671
+ function getUint8ArrayMemory0() {
672
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
673
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
674
+ }
675
+ return cachedUint8ArrayMemory0;
676
+ }
677
+ function getStringFromWasm0(ptr, len) {
678
+ ptr = ptr >>> 0;
679
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
680
+ }
681
+ var heap = new Array(128).fill(void 0);
682
+ heap.push(void 0, null, true, false);
683
+ var heap_next = heap.length;
684
+ function addHeapObject(obj) {
685
+ if (heap_next === heap.length) heap.push(heap.length + 1);
686
+ const idx = heap_next;
687
+ heap_next = heap[idx];
688
+ heap[idx] = obj;
689
+ return idx;
690
+ }
691
+ function handleError(f, args) {
692
+ try {
693
+ return f.apply(this, args);
694
+ } catch (e) {
695
+ wasm.__wbindgen_export_0(addHeapObject(e));
696
+ }
697
+ }
698
+ function getObject(idx) {
699
+ return heap[idx];
700
+ }
701
+ function dropObject(idx) {
702
+ if (idx < 132) return;
703
+ heap[idx] = heap_next;
704
+ heap_next = idx;
705
+ }
706
+ function takeObject(idx) {
707
+ const ret = getObject(idx);
708
+ dropObject(idx);
709
+ return ret;
710
+ }
711
+ var WASM_VECTOR_LEN = 0;
712
+ var lTextEncoder = typeof TextEncoder === "undefined" ? (0, module2.require)("util").TextEncoder : TextEncoder;
713
+ var cachedTextEncoder = new lTextEncoder("utf-8");
714
+ var encodeString = typeof cachedTextEncoder.encodeInto === "function" ? function(arg, view) {
715
+ return cachedTextEncoder.encodeInto(arg, view);
716
+ } : function(arg, view) {
717
+ const buf = cachedTextEncoder.encode(arg);
718
+ view.set(buf);
719
+ return {
720
+ read: arg.length,
721
+ written: buf.length
722
+ };
723
+ };
724
+ function passStringToWasm0(arg, malloc, realloc) {
725
+ if (realloc === void 0) {
726
+ const buf = cachedTextEncoder.encode(arg);
727
+ const ptr2 = malloc(buf.length, 1) >>> 0;
728
+ getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
729
+ WASM_VECTOR_LEN = buf.length;
730
+ return ptr2;
731
+ }
732
+ let len = arg.length;
733
+ let ptr = malloc(len, 1) >>> 0;
734
+ const mem = getUint8ArrayMemory0();
735
+ let offset = 0;
736
+ for (; offset < len; offset++) {
737
+ const code = arg.charCodeAt(offset);
738
+ if (code > 127) break;
739
+ mem[ptr + offset] = code;
740
+ }
741
+ if (offset !== len) {
742
+ if (offset !== 0) {
743
+ arg = arg.slice(offset);
744
+ }
745
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
746
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
747
+ const ret = encodeString(arg, view);
748
+ offset += ret.written;
749
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
750
+ }
751
+ WASM_VECTOR_LEN = offset;
752
+ return ptr;
753
+ }
754
+ function isLikeNone(x) {
755
+ return x === void 0 || x === null;
756
+ }
757
+ var cachedDataViewMemory0 = null;
758
+ function getDataViewMemory0() {
759
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === void 0 && cachedDataViewMemory0.buffer !== wasm.memory.buffer) {
760
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
761
+ }
762
+ return cachedDataViewMemory0;
763
+ }
764
+ var cachedUint32ArrayMemory0 = null;
765
+ function getUint32ArrayMemory0() {
766
+ if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
767
+ cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
768
+ }
769
+ return cachedUint32ArrayMemory0;
770
+ }
771
+ function getArrayU32FromWasm0(ptr, len) {
772
+ ptr = ptr >>> 0;
773
+ return getUint32ArrayMemory0().subarray(ptr / 4, ptr / 4 + len);
774
+ }
775
+ function passArray8ToWasm0(arg, malloc) {
776
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
777
+ getUint8ArrayMemory0().set(arg, ptr / 1);
778
+ WASM_VECTOR_LEN = arg.length;
779
+ return ptr;
780
+ }
781
+ function passArray32ToWasm0(arg, malloc) {
782
+ const ptr = malloc(arg.length * 4, 4) >>> 0;
783
+ getUint32ArrayMemory0().set(arg, ptr / 4);
784
+ WASM_VECTOR_LEN = arg.length;
785
+ return ptr;
786
+ }
787
+ function getArrayU8FromWasm0(ptr, len) {
788
+ ptr = ptr >>> 0;
789
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
790
+ }
791
+ module2.exports.get_encoding = function(encoding, extend_special_tokens) {
792
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
793
+ try {
794
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
795
+ const ptr0 = passStringToWasm0(encoding, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
796
+ const len0 = WASM_VECTOR_LEN;
797
+ wasm.get_encoding(retptr, ptr0, len0, addHeapObject(extend_special_tokens));
798
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
799
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
800
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
801
+ if (r2) {
802
+ throw takeObject(r1);
803
+ }
804
+ return Tiktoken.__wrap(r0);
805
+ } finally {
806
+ wasm.__wbindgen_add_to_stack_pointer(16);
807
+ }
808
+ };
809
+ module2.exports.encoding_for_model = function(model, extend_special_tokens) {
810
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
811
+ try {
812
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
813
+ const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
814
+ const len0 = WASM_VECTOR_LEN;
815
+ wasm.encoding_for_model(retptr, ptr0, len0, addHeapObject(extend_special_tokens));
816
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
817
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
818
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
819
+ if (r2) {
820
+ throw takeObject(r1);
821
+ }
822
+ return Tiktoken.__wrap(r0);
823
+ } finally {
824
+ wasm.__wbindgen_add_to_stack_pointer(16);
825
+ }
826
+ };
827
+ module2.exports.get_encoding_name_for_model = function(model) {
828
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
829
+ let deferred3_0;
830
+ let deferred3_1;
831
+ try {
832
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
833
+ const ptr0 = passStringToWasm0(model, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
834
+ const len0 = WASM_VECTOR_LEN;
835
+ wasm.get_encoding_name_for_model(retptr, ptr0, len0);
836
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
837
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
838
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
839
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
840
+ var ptr2 = r0;
841
+ var len2 = r1;
842
+ if (r3) {
843
+ ptr2 = 0;
844
+ len2 = 0;
845
+ throw takeObject(r2);
846
+ }
847
+ deferred3_0 = ptr2;
848
+ deferred3_1 = len2;
849
+ return getStringFromWasm0(ptr2, len2);
850
+ } finally {
851
+ wasm.__wbindgen_add_to_stack_pointer(16);
852
+ wasm.__wbindgen_export_3(deferred3_0, deferred3_1, 1);
853
+ }
854
+ };
855
+ var TiktokenFinalization = typeof FinalizationRegistry === "undefined" ? { register: () => {
856
+ }, unregister: () => {
857
+ } } : new FinalizationRegistry((ptr) => wasm.__wbg_tiktoken_free(ptr >>> 0, 1));
858
+ var Tiktoken = class _Tiktoken {
859
+ /**
860
+ * @param {string} tiktoken_bfe
861
+ * @param {any} special_tokens
862
+ * @param {string} pat_str
863
+ */
864
+ constructor(tiktoken_bfe, special_tokens, pat_str) {
865
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
866
+ const ptr0 = passStringToWasm0(tiktoken_bfe, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
867
+ const len0 = WASM_VECTOR_LEN;
868
+ const ptr1 = passStringToWasm0(pat_str, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
869
+ const len1 = WASM_VECTOR_LEN;
870
+ const ret = wasm.tiktoken_new(ptr0, len0, addHeapObject(special_tokens), ptr1, len1);
871
+ this.__wbg_ptr = ret >>> 0;
872
+ TiktokenFinalization.register(this, this.__wbg_ptr, this);
873
+ return this;
874
+ }
875
+ /** @returns {string | undefined} */
876
+ get name() {
877
+ try {
878
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
879
+ wasm.tiktoken_name(retptr, this.__wbg_ptr);
880
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
881
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
882
+ let v1;
883
+ if (r0 !== 0) {
884
+ v1 = getStringFromWasm0(r0, r1).slice();
885
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
886
+ }
887
+ return v1;
888
+ } finally {
889
+ wasm.__wbindgen_add_to_stack_pointer(16);
890
+ }
891
+ }
892
+ static __wrap(ptr) {
893
+ ptr = ptr >>> 0;
894
+ const obj = Object.create(_Tiktoken.prototype);
895
+ obj.__wbg_ptr = ptr;
896
+ TiktokenFinalization.register(obj, obj.__wbg_ptr, obj);
897
+ return obj;
898
+ }
899
+ __destroy_into_raw() {
900
+ const ptr = this.__wbg_ptr;
901
+ this.__wbg_ptr = 0;
902
+ TiktokenFinalization.unregister(this);
903
+ return ptr;
904
+ }
905
+ free() {
906
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
907
+ const ptr = this.__destroy_into_raw();
908
+ wasm.__wbg_tiktoken_free(ptr, 0);
909
+ }
910
+ /**
911
+ * @param {string} text
912
+ * @param {any} allowed_special
913
+ * @param {any} disallowed_special
914
+ * @returns {Uint32Array}
915
+ */
916
+ encode(text3, allowed_special, disallowed_special) {
917
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
918
+ try {
919
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
920
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
921
+ const len0 = WASM_VECTOR_LEN;
922
+ wasm.tiktoken_encode(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(allowed_special), addHeapObject(disallowed_special));
923
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
924
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
925
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
926
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
927
+ if (r3) {
928
+ throw takeObject(r2);
929
+ }
930
+ var v2 = getArrayU32FromWasm0(r0, r1).slice();
931
+ wasm.__wbindgen_export_3(r0, r1 * 4, 4);
932
+ return v2;
933
+ } finally {
934
+ wasm.__wbindgen_add_to_stack_pointer(16);
935
+ }
936
+ }
937
+ /**
938
+ * @param {string} text
939
+ * @returns {Uint32Array}
940
+ */
941
+ encode_ordinary(text3) {
942
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
943
+ try {
944
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
945
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
946
+ const len0 = WASM_VECTOR_LEN;
947
+ wasm.tiktoken_encode_ordinary(retptr, this.__wbg_ptr, ptr0, len0);
948
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
949
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
950
+ var v2 = getArrayU32FromWasm0(r0, r1).slice();
951
+ wasm.__wbindgen_export_3(r0, r1 * 4, 4);
952
+ return v2;
953
+ } finally {
954
+ wasm.__wbindgen_add_to_stack_pointer(16);
955
+ }
956
+ }
957
+ /**
958
+ * @param {string} text
959
+ * @param {any} allowed_special
960
+ * @param {any} disallowed_special
961
+ * @returns {any}
962
+ */
963
+ encode_with_unstable(text3, allowed_special, disallowed_special) {
964
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
965
+ try {
966
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
967
+ const ptr0 = passStringToWasm0(text3, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
968
+ const len0 = WASM_VECTOR_LEN;
969
+ wasm.tiktoken_encode_with_unstable(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(allowed_special), addHeapObject(disallowed_special));
970
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
971
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
972
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
973
+ if (r2) {
974
+ throw takeObject(r1);
975
+ }
976
+ return takeObject(r0);
977
+ } finally {
978
+ wasm.__wbindgen_add_to_stack_pointer(16);
979
+ }
980
+ }
981
+ /**
982
+ * @param {Uint8Array} bytes
983
+ * @returns {number}
984
+ */
985
+ encode_single_token(bytes) {
986
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
987
+ const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export_1);
988
+ const len0 = WASM_VECTOR_LEN;
989
+ const ret = wasm.tiktoken_encode_single_token(this.__wbg_ptr, ptr0, len0);
990
+ return ret >>> 0;
991
+ }
992
+ /**
993
+ * @param {Uint32Array} tokens
994
+ * @returns {Uint8Array}
995
+ */
996
+ decode(tokens) {
997
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
998
+ try {
999
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1000
+ const ptr0 = passArray32ToWasm0(tokens, wasm.__wbindgen_export_1);
1001
+ const len0 = WASM_VECTOR_LEN;
1002
+ wasm.tiktoken_decode(retptr, this.__wbg_ptr, ptr0, len0);
1003
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1004
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1005
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
1006
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
1007
+ return v2;
1008
+ } finally {
1009
+ wasm.__wbindgen_add_to_stack_pointer(16);
1010
+ }
1011
+ }
1012
+ /**
1013
+ * @param {number} token
1014
+ * @returns {Uint8Array}
1015
+ */
1016
+ decode_single_token_bytes(token) {
1017
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1018
+ try {
1019
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1020
+ wasm.tiktoken_decode_single_token_bytes(retptr, this.__wbg_ptr, token);
1021
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1022
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1023
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
1024
+ wasm.__wbindgen_export_3(r0, r1 * 1, 1);
1025
+ return v1;
1026
+ } finally {
1027
+ wasm.__wbindgen_add_to_stack_pointer(16);
1028
+ }
1029
+ }
1030
+ /** @returns {any} */
1031
+ token_byte_values() {
1032
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1033
+ const ret = wasm.tiktoken_token_byte_values(this.__wbg_ptr);
1034
+ return takeObject(ret);
1035
+ }
1036
+ };
1037
+ module2.exports.Tiktoken = Tiktoken;
1038
+ module2.exports.__wbg_parse_def2e24ef1252aff = function() {
1039
+ return handleError(function(arg0, arg1) {
1040
+ const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
1041
+ return addHeapObject(ret);
1042
+ }, arguments);
1043
+ };
1044
+ module2.exports.__wbg_stringify_f7ed6987935b4a24 = function() {
1045
+ return handleError(function(arg0) {
1046
+ const ret = JSON.stringify(getObject(arg0));
1047
+ return addHeapObject(ret);
1048
+ }, arguments);
1049
+ };
1050
+ module2.exports.__wbindgen_error_new = function(arg0, arg1) {
1051
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
1052
+ return addHeapObject(ret);
1053
+ };
1054
+ module2.exports.__wbindgen_is_undefined = function(arg0) {
1055
+ const ret = getObject(arg0) === void 0;
1056
+ return ret;
1057
+ };
1058
+ module2.exports.__wbindgen_object_drop_ref = function(arg0) {
1059
+ takeObject(arg0);
1060
+ };
1061
+ module2.exports.__wbindgen_string_get = function(arg0, arg1) {
1062
+ if (wasm == null) throw new Error("tiktoken: WASM binary has not been propery initialized.");
1063
+ const obj = getObject(arg1);
1064
+ const ret = typeof obj === "string" ? obj : void 0;
1065
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export_1, wasm.__wbindgen_export_2);
1066
+ var len1 = WASM_VECTOR_LEN;
1067
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
1068
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
1069
+ };
1070
+ module2.exports.__wbindgen_throw = function(arg0, arg1) {
1071
+ throw new Error(getStringFromWasm0(arg0, arg1));
1072
+ };
1073
+ }
1074
+ });
1075
+
1076
+ // node_modules/tiktoken/tiktoken.cjs
1077
+ var require_tiktoken = __commonJS({
1078
+ "node_modules/tiktoken/tiktoken.cjs"(exports) {
1079
+ "use strict";
1080
+ var wasm = require_tiktoken_bg();
1081
+ var imports = {};
1082
+ imports["./tiktoken_bg.js"] = wasm;
1083
+ var path17 = __require("path");
1084
+ var fs = __require("fs");
1085
+ var candidates = __dirname.split(path17.sep).reduce((memo, _, index, array) => {
1086
+ const prefix = array.slice(0, index + 1).join(path17.sep) + path17.sep;
1087
+ if (!prefix.includes("node_modules" + path17.sep)) {
1088
+ memo.unshift(
1089
+ path17.join(
1090
+ prefix,
1091
+ "node_modules",
1092
+ "tiktoken",
1093
+ "",
1094
+ "./tiktoken_bg.wasm"
1095
+ )
1096
+ );
1097
+ }
1098
+ return memo;
1099
+ }, []);
1100
+ candidates.unshift(path17.join(__dirname, "./tiktoken_bg.wasm"));
1101
+ var bytes = null;
1102
+ for (const candidate of candidates) {
1103
+ try {
1104
+ bytes = fs.readFileSync(candidate);
1105
+ break;
1106
+ } catch {
1107
+ }
1108
+ }
1109
+ if (bytes == null) throw new Error("Missing tiktoken_bg.wasm");
1110
+ var wasmModule = new WebAssembly.Module(bytes);
1111
+ var wasmInstance = new WebAssembly.Instance(wasmModule, imports);
1112
+ wasm.__wbg_set_wasm(wasmInstance.exports);
1113
+ exports["get_encoding"] = wasm["get_encoding"];
1114
+ exports["encoding_for_model"] = wasm["encoding_for_model"];
1115
+ exports["get_encoding_name_for_model"] = wasm["get_encoding_name_for_model"];
1116
+ exports["Tiktoken"] = wasm["Tiktoken"];
1117
+ }
1118
+ });
1119
+
653
1120
  // src/pi-extension.ts
654
1121
  import { Type as Type2 } from "typebox";
655
1122
 
@@ -4954,7 +5421,12 @@ function sameReclaimOwner(left, right) {
4954
5421
  }
4955
5422
  function publishJsonDirectory(finalPath, value) {
4956
5423
  const candidatePath = `${finalPath}.candidate.${process.pid}.${randomUUID()}`;
4957
- mkdirSync2(candidatePath, { mode: 448 });
5424
+ try {
5425
+ mkdirSync2(candidatePath, { mode: 448 });
5426
+ } catch (error) {
5427
+ if (getErrorCode(error) === "ENOENT") return false;
5428
+ throw error;
5429
+ }
4958
5430
  try {
4959
5431
  writeFileSync2(path12.join(candidatePath, OWNER_FILE_NAME), JSON.stringify(value), {
4960
5432
  encoding: "utf-8",
@@ -4967,8 +5439,12 @@ function publishJsonDirectory(finalPath, value) {
4967
5439
  return true;
4968
5440
  } catch (error) {
4969
5441
  if (existsSync7(finalPath)) return false;
5442
+ if (getErrorCode(error) === "ENOENT") return false;
4970
5443
  throw error;
4971
5444
  }
5445
+ } catch (error) {
5446
+ if (getErrorCode(error) === "ENOENT") return false;
5447
+ throw error;
4972
5448
  } finally {
4973
5449
  if (existsSync7(candidatePath)) rmSync(candidatePath, { recursive: true, force: true });
4974
5450
  }
@@ -6537,6 +7013,27 @@ function mergeTieredResults(symbolLane, hybridLane, limit) {
6537
7013
  }
6538
7014
  return out;
6539
7015
  }
7016
+ function selectChunksWithFileCoverage(chunks, limit) {
7017
+ if (limit <= 0 || chunks.length === 0) {
7018
+ return [];
7019
+ }
7020
+ if (chunks.length <= limit) {
7021
+ return chunks;
7022
+ }
7023
+ if (limit === 1) {
7024
+ return [chunks[Math.floor((chunks.length - 1) / 2)]];
7025
+ }
7026
+ const selected = [];
7027
+ for (let index = 0; index < limit; index++) {
7028
+ const sourceIndex = Math.round(index * (chunks.length - 1) / (limit - 1));
7029
+ selected.push(chunks[sourceIndex]);
7030
+ }
7031
+ return selected;
7032
+ }
7033
+ function selectIndexableChunks(chunks, limit, semanticOnly) {
7034
+ const indexableChunks = semanticOnly ? chunks.filter((chunk) => chunk.chunkType !== "other") : chunks;
7035
+ return selectChunksWithFileCoverage(indexableChunks, limit);
7036
+ }
6540
7037
  function matchesSearchFilters(candidate, options, minScore) {
6541
7038
  if (candidate.score < minScore) return false;
6542
7039
  if (options?.fileType) {
@@ -8275,7 +8772,6 @@ var Indexer = class {
8275
8772
  const relativePath = path13.relative(this.projectRoot, parsed.path);
8276
8773
  stats.parseFailures.push(relativePath);
8277
8774
  }
8278
- let fileChunkCount = 0;
8279
8775
  let chunksToProcess = parsed.chunks;
8280
8776
  if (this.config.indexing.fallbackToTextOnMaxChunks && chunksToProcess.length > this.config.indexing.maxChunksPerFile) {
8281
8777
  const changedFile = changedFiles.find((f) => f.path === parsed.path);
@@ -8284,13 +8780,12 @@ var Indexer = class {
8284
8780
  chunksToProcess = textChunks;
8285
8781
  }
8286
8782
  }
8783
+ chunksToProcess = selectIndexableChunks(
8784
+ chunksToProcess,
8785
+ this.config.indexing.maxChunksPerFile,
8786
+ this.config.indexing.semanticOnly
8787
+ );
8287
8788
  for (const chunk of chunksToProcess) {
8288
- if (fileChunkCount >= this.config.indexing.maxChunksPerFile) {
8289
- break;
8290
- }
8291
- if (this.config.indexing.semanticOnly && chunk.chunkType === "other") {
8292
- continue;
8293
- }
8294
8789
  const id = generateChunkId(parsed.path, chunk);
8295
8790
  const contentHash = generateChunkHash(chunk);
8296
8791
  const existingContentHash = existingChunks.get(id);
@@ -8314,7 +8809,6 @@ var Indexer = class {
8314
8809
  blameSummary: blameMetadata.blameSummary
8315
8810
  });
8316
8811
  if (existingContentHash === contentHash) {
8317
- fileChunkCount++;
8318
8812
  continue;
8319
8813
  }
8320
8814
  const texts = createEmbeddingTexts(chunk, parsed.path, getSafeEmbeddingChunkTokenLimit(configuredProviderInfo)).map((text3) => ({
@@ -8339,7 +8833,6 @@ var Indexer = class {
8339
8833
  contentHash,
8340
8834
  metadata
8341
8835
  });
8342
- fileChunkCount++;
8343
8836
  }
8344
8837
  }
8345
8838
  const retryableFailedChunks = this.collectRetryableFailedChunks(
@@ -8901,10 +9394,19 @@ var Indexer = class {
8901
9394
  });
8902
9395
  const embeddingStartTime = performance2.now();
8903
9396
  const embeddingQuery = stripFilePathHint(query);
8904
- const embedding = await this.getQueryEmbedding(embeddingQuery, provider);
9397
+ let embedding;
9398
+ try {
9399
+ embedding = await this.getQueryEmbedding(embeddingQuery, provider);
9400
+ } catch (error) {
9401
+ this.logger.warn("Query embedding failed; falling back to keyword-only search", {
9402
+ query,
9403
+ error: getErrorMessage2(error),
9404
+ action: "Check the embedding provider configuration and retry search after restoring provider health."
9405
+ });
9406
+ }
8905
9407
  const embeddingMs = performance2.now() - embeddingStartTime;
8906
9408
  const vectorStartTime = performance2.now();
8907
- const semanticResults = store.search(embedding, maxResults * 4);
9409
+ const semanticResults = embedding ? store.search(embedding, maxResults * 4) : [];
8908
9410
  const vectorMs = performance2.now() - vectorStartTime;
8909
9411
  const keywordStartTime = performance2.now();
8910
9412
  const keywordResults = await this.keywordSearch(query, maxResults * 4, store, invertedIndex);
@@ -8939,12 +9441,13 @@ var Indexer = class {
8939
9441
  });
8940
9442
  }
8941
9443
  const fusionStartTime = performance2.now();
9444
+ const rankingHybridWeight = embedding === void 0 && fusionStrategy === "weighted" ? 1 : effectiveHybridWeight;
8942
9445
  const combined = rankHybridResults(query, semanticCandidates, keywordCandidates, {
8943
9446
  fusionStrategy,
8944
9447
  rrfK,
8945
9448
  rerankTopN,
8946
9449
  limit: maxResults,
8947
- hybridWeight: effectiveHybridWeight,
9450
+ hybridWeight: rankingHybridWeight,
8948
9451
  prioritizeSourcePaths: sourceIntent
8949
9452
  });
8950
9453
  const rerankedCombined = await this.rerankCandidatesWithApi(query, combined, {
@@ -9920,6 +10423,159 @@ function findKnowledgeBasePathIndex(knowledgeBases, inputPath, projectRoot3) {
9920
10423
  }
9921
10424
 
9922
10425
  // src/tools/utils.ts
10426
+ var import_tiktoken = __toESM(require_tiktoken(), 1);
10427
+ var MIN_CONTEXT_PACK_TOKEN_BUDGET = 128;
10428
+ var MAX_CONTEXT_PACK_TOKEN_BUDGET = 4e3;
10429
+ var DEFAULT_CONTEXT_PACK_TOKEN_BUDGET = 1200;
10430
+ var CONTEXT_TOKENIZER = (0, import_tiktoken.get_encoding)("cl100k_base");
10431
+ function clampContextPackTokenBudget(tokenBudget) {
10432
+ if (tokenBudget === void 0 || !Number.isFinite(tokenBudget)) {
10433
+ return DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
10434
+ }
10435
+ return Math.min(
10436
+ MAX_CONTEXT_PACK_TOKEN_BUDGET,
10437
+ Math.max(MIN_CONTEXT_PACK_TOKEN_BUDGET, Math.floor(tokenBudget))
10438
+ );
10439
+ }
10440
+ function countContextTokens(text3) {
10441
+ return CONTEXT_TOKENIZER.encode(text3).length;
10442
+ }
10443
+ function fitTextToContextBudget(text3, tokenBudget) {
10444
+ const normalizedBudget = clampContextPackTokenBudget(tokenBudget);
10445
+ const tokenEstimate = countContextTokens(text3);
10446
+ if (tokenEstimate <= normalizedBudget) {
10447
+ return {
10448
+ text: text3,
10449
+ tokenBudget: normalizedBudget,
10450
+ tokenEstimate,
10451
+ truncated: false
10452
+ };
10453
+ }
10454
+ const suffix = "\n...[truncated to context token budget]";
10455
+ const codePoints = Array.from(text3);
10456
+ let low = 0;
10457
+ let high = codePoints.length;
10458
+ while (low < high) {
10459
+ const middle = Math.ceil((low + high) / 2);
10460
+ const candidate = `${codePoints.slice(0, middle).join("").trimEnd()}${suffix}`;
10461
+ if (countContextTokens(candidate) <= normalizedBudget) low = middle;
10462
+ else high = middle - 1;
10463
+ }
10464
+ const fitted = `${codePoints.slice(0, low).join("").trimEnd()}${suffix}`;
10465
+ return {
10466
+ text: fitted,
10467
+ tokenBudget: normalizedBudget,
10468
+ tokenEstimate: countContextTokens(fitted),
10469
+ truncated: true
10470
+ };
10471
+ }
10472
+ function normalizedLineRange(result) {
10473
+ return result.startLine <= result.endLine ? { start: result.startLine, end: result.endLine } : { start: result.endLine, end: result.startLine };
10474
+ }
10475
+ function rankContextCandidates(results) {
10476
+ return results.map((result, originalIndex) => ({ result, originalIndex })).sort((left, right) => right.result.score - left.result.score || left.originalIndex - right.originalIndex);
10477
+ }
10478
+ function deduplicateContextCandidates(candidates) {
10479
+ const acceptedByFile = /* @__PURE__ */ new Map();
10480
+ const deduplicated = [];
10481
+ for (const { result } of candidates) {
10482
+ const range = normalizedLineRange(result);
10483
+ const accepted = acceptedByFile.get(result.filePath) ?? [];
10484
+ if (accepted.some((item) => item.start <= range.end && range.start <= item.end)) {
10485
+ continue;
10486
+ }
10487
+ accepted.push(range);
10488
+ acceptedByFile.set(result.filePath, accepted);
10489
+ deduplicated.push(result);
10490
+ }
10491
+ return deduplicated;
10492
+ }
10493
+ function diversifyContextCandidates(results) {
10494
+ const byFile = /* @__PURE__ */ new Map();
10495
+ for (const result of results) {
10496
+ const bucket = byFile.get(result.filePath) ?? [];
10497
+ bucket.push(result);
10498
+ byFile.set(result.filePath, bucket);
10499
+ }
10500
+ const files = [...byFile.keys()];
10501
+ const diversified = [];
10502
+ for (let depth = 0; diversified.length < results.length; depth += 1) {
10503
+ for (const file of files) {
10504
+ const result = byFile.get(file)?.[depth];
10505
+ if (result) diversified.push(result);
10506
+ }
10507
+ }
10508
+ return diversified;
10509
+ }
10510
+ function compactEvidenceValue(value, maxChars) {
10511
+ const characters = [...value];
10512
+ if (characters.length <= maxChars) return value;
10513
+ return `\u2026${characters.slice(-(maxChars - 1)).join("")}`;
10514
+ }
10515
+ function formatContextEvidence(result, index) {
10516
+ const symbol = result.name ? ` ${JSON.stringify(compactEvidenceValue(result.name, 80))}` : "";
10517
+ const path17 = compactEvidenceValue(result.filePath, 120);
10518
+ return `[${index}] ${result.chunkType}${symbol} in ${path17}:${result.startLine}-${result.endLine} (score ${result.score.toFixed(2)})`;
10519
+ }
10520
+ function formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, budgetOmittedCount) {
10521
+ const lines = selected.map((result, index) => formatContextEvidence(result, index + 1));
10522
+ const notes = [];
10523
+ if (duplicateCount > 0) notes.push(`${duplicateCount} overlapping duplicate${duplicateCount === 1 ? "" : "s"} removed`);
10524
+ if (limitOmittedCount > 0) notes.push(`${limitOmittedCount} additional result${limitOmittedCount === 1 ? "" : "s"} excluded by result limit`);
10525
+ if (budgetOmittedCount > 0) notes.push(`${budgetOmittedCount} additional result${budgetOmittedCount === 1 ? "" : "s"} omitted by token budget`);
10526
+ const footer = notes.length > 0 ? `Selected ${selected.length} of ${candidateCount} candidates; ${notes.join("; ")}.` : `Selected ${selected.length} of ${candidateCount} candidates.`;
10527
+ return `${heading}
10528
+
10529
+ ${lines.join("\n")}
10530
+
10531
+ ${footer}`;
10532
+ }
10533
+ function buildContextPack(results, options = {}) {
10534
+ const requestedTokenBudget = options.tokenBudget ?? DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
10535
+ const tokenBudget = clampContextPackTokenBudget(options.tokenBudget);
10536
+ const heading = compactEvidenceValue(options.heading?.trim() || "Codebase evidence", 160);
10537
+ const maxResults = Math.max(0, Math.floor(options.maxResults ?? results.length));
10538
+ const candidateCount = results.length;
10539
+ const deduplicated = deduplicateContextCandidates(rankContextCandidates(results));
10540
+ const diversified = diversifyContextCandidates(deduplicated);
10541
+ const duplicateCount = candidateCount - deduplicated.length;
10542
+ const selectable = diversified.slice(0, maxResults);
10543
+ const limitOmittedCount = deduplicated.length - selectable.length;
10544
+ let selected = [];
10545
+ let text3 = formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, selectable.length);
10546
+ for (let count = 1; count <= selectable.length; count += 1) {
10547
+ const candidateSelection = selectable.slice(0, count);
10548
+ const budgetOmittedCount2 = selectable.length - candidateSelection.length;
10549
+ const candidateText = formatContextPack(
10550
+ heading,
10551
+ candidateSelection,
10552
+ candidateCount,
10553
+ duplicateCount,
10554
+ limitOmittedCount,
10555
+ budgetOmittedCount2
10556
+ );
10557
+ if (countContextTokens(candidateText) > tokenBudget) break;
10558
+ selected = candidateSelection;
10559
+ text3 = candidateText;
10560
+ }
10561
+ const fitted = fitTextToContextBudget(text3, tokenBudget);
10562
+ const budgetOmittedCount = selectable.length - selected.length;
10563
+ const omittedCount = candidateCount - selected.length;
10564
+ return {
10565
+ requestedTokenBudget,
10566
+ tokenBudget,
10567
+ text: fitted.text,
10568
+ tokenEstimate: fitted.tokenEstimate,
10569
+ results: selected,
10570
+ candidateCount,
10571
+ deduplicatedCount: deduplicated.length,
10572
+ selectedCount: selected.length,
10573
+ omittedCount,
10574
+ duplicateCount,
10575
+ limitOmittedCount,
10576
+ budgetOmittedCount
10577
+ };
10578
+ }
9923
10579
  var MAX_CONTENT_LINES = 30;
9924
10580
  function truncateContent(content) {
9925
10581
  const lines = content.split("\n");
@@ -10577,6 +11233,253 @@ Run /index to rebuild the index without the removed knowledge base.`;
10577
11233
  return result;
10578
11234
  }
10579
11235
 
11236
+ // src/tools/symbol-inference.ts
11237
+ var IDENTIFIER_RE = /[A-Za-z_$][A-Za-z0-9_$]*/g;
11238
+ var QUOTED_BACKTICK_RE = /`([^`]+)`/g;
11239
+ var QUOTED_SINGLE_RE = /'([^'\\]+)'/g;
11240
+ var QUOTED_DOUBLE_RE = /"([^"]+)"/g;
11241
+ var SYMBOL_LIKE_RE = /^(?:[A-Za-z_$][A-Za-z0-9_$]*)$/;
11242
+ var CAMEL_CASE_RE = /^[a-z_][A-Za-z0-9_$]*[A-Z][A-Za-z0-9_$]*$/;
11243
+ var PASCAL_CASE_RE = /^[A-Z][A-Za-z0-9_$]*$/;
11244
+ var SNAKE_CASE_RE = /^[a-z][a-z0-9_]*_[a-z0-9_]+$/;
11245
+ var DEFINITION_INTENT_RE = /\b(where|defined|definition|define|declaration|symbol|function|method|class|interface|type)\b/i;
11246
+ var STOP_WORDS = /* @__PURE__ */ new Set([
11247
+ "a",
11248
+ "an",
11249
+ "and",
11250
+ "are",
11251
+ "at",
11252
+ "for",
11253
+ "find",
11254
+ "how",
11255
+ "i",
11256
+ "in",
11257
+ "is",
11258
+ "it",
11259
+ "of",
11260
+ "on",
11261
+ "that",
11262
+ "the",
11263
+ "definition",
11264
+ "show",
11265
+ "to",
11266
+ "where",
11267
+ "which",
11268
+ "what",
11269
+ "you",
11270
+ "your",
11271
+ "with"
11272
+ ]);
11273
+ function stripCallSuffix(token) {
11274
+ return token.replace(/\(\s*\)$/, "");
11275
+ }
11276
+ function isLikelySymbolName(token) {
11277
+ if (!SYMBOL_LIKE_RE.test(token)) {
11278
+ return false;
11279
+ }
11280
+ if (STOP_WORDS.has(token.toLowerCase())) {
11281
+ return false;
11282
+ }
11283
+ return CAMEL_CASE_RE.test(token) || PASCAL_CASE_RE.test(token) || SNAKE_CASE_RE.test(token);
11284
+ }
11285
+ function extractQuotedIdentifiers(query) {
11286
+ const identifiers = /* @__PURE__ */ new Set();
11287
+ for (const match of query.matchAll(QUOTED_BACKTICK_RE)) {
11288
+ const candidate = stripCallSuffix(match[1].trim());
11289
+ if (candidate && isLikelySymbolName(candidate)) {
11290
+ identifiers.add(candidate);
11291
+ }
11292
+ }
11293
+ for (const match of query.matchAll(QUOTED_SINGLE_RE)) {
11294
+ const candidate = stripCallSuffix(match[1].trim());
11295
+ if (candidate && isLikelySymbolName(candidate)) {
11296
+ identifiers.add(candidate);
11297
+ }
11298
+ }
11299
+ for (const match of query.matchAll(QUOTED_DOUBLE_RE)) {
11300
+ const candidate = stripCallSuffix(match[1].trim());
11301
+ if (candidate && isLikelySymbolName(candidate)) {
11302
+ identifiers.add(candidate);
11303
+ }
11304
+ }
11305
+ return [...identifiers];
11306
+ }
11307
+ function extractBareIdentifiers(query) {
11308
+ const unquoted = query.replace(QUOTED_BACKTICK_RE, " ").replace(QUOTED_SINGLE_RE, " ").replace(QUOTED_DOUBLE_RE, " ");
11309
+ const identifiers = /* @__PURE__ */ new Set();
11310
+ for (const match of unquoted.matchAll(IDENTIFIER_RE)) {
11311
+ const candidate = stripCallSuffix(match[0]);
11312
+ if (isLikelySymbolName(candidate)) {
11313
+ identifiers.add(candidate);
11314
+ }
11315
+ }
11316
+ return [...identifiers];
11317
+ }
11318
+ function isSingleMeaningfulToken(query, symbol) {
11319
+ 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));
11320
+ return tokens.length === 1 && tokens[0] === symbol.toLowerCase();
11321
+ }
11322
+ function inferExactSymbolFromQuery(query) {
11323
+ const quoted = extractQuotedIdentifiers(query);
11324
+ if (quoted.length === 1) {
11325
+ return quoted[0];
11326
+ }
11327
+ if (quoted.length > 1) {
11328
+ return void 0;
11329
+ }
11330
+ const candidates = extractBareIdentifiers(query);
11331
+ if (candidates.length !== 1) {
11332
+ return void 0;
11333
+ }
11334
+ const candidate = candidates[0];
11335
+ if (DEFINITION_INTENT_RE.test(query)) {
11336
+ return candidate;
11337
+ }
11338
+ if (isSingleMeaningfulToken(query, candidate)) {
11339
+ return candidate;
11340
+ }
11341
+ return void 0;
11342
+ }
11343
+
11344
+ // src/tools/context.ts
11345
+ var MIN_CONTEXT_RESULT_LIMIT = 1;
11346
+ var MAX_CONTEXT_RESULT_LIMIT = 100;
11347
+ var MIN_CONTEXT_PATH_DEPTH = 1;
11348
+ var MAX_CONTEXT_PATH_DEPTH = 100;
11349
+ function locations(results) {
11350
+ return results.map((result) => ({
11351
+ filePath: result.filePath,
11352
+ startLine: result.startLine,
11353
+ endLine: result.endLine,
11354
+ score: result.score,
11355
+ chunkType: result.chunkType,
11356
+ name: result.name
11357
+ }));
11358
+ }
11359
+ function packedResult(route, routedQuery, pack) {
11360
+ return {
11361
+ text: pack.text,
11362
+ details: {
11363
+ route,
11364
+ routedQuery,
11365
+ tokenBudget: pack.tokenBudget,
11366
+ tokenEstimate: pack.tokenEstimate,
11367
+ candidateCount: pack.candidateCount,
11368
+ deduplicatedCount: pack.deduplicatedCount,
11369
+ selectedCount: pack.selectedCount,
11370
+ omittedCount: pack.omittedCount,
11371
+ duplicateCount: pack.duplicateCount,
11372
+ limitOmittedCount: pack.limitOmittedCount,
11373
+ budgetOmittedCount: pack.budgetOmittedCount,
11374
+ results: locations(pack.results)
11375
+ }
11376
+ };
11377
+ }
11378
+ async function resolveSearchContext(input, operations) {
11379
+ const explicitSymbol = input.symbol ?? void 0;
11380
+ const limit = input.limit ?? 10;
11381
+ const tokenBudget = input.tokenBudget ?? void 0;
11382
+ const lookupSymbol = explicitSymbol ?? inferExactSymbolFromQuery(input.query);
11383
+ if (lookupSymbol) {
11384
+ const definitions = await operations.lookup(lookupSymbol, MAX_CONTEXT_RESULT_LIMIT);
11385
+ if (definitions.length > 0) {
11386
+ return packedResult("definition", lookupSymbol, buildContextPack(definitions, {
11387
+ tokenBudget,
11388
+ maxResults: limit,
11389
+ heading: `Definition evidence for ${JSON.stringify(lookupSymbol)}`
11390
+ }));
11391
+ }
11392
+ if (explicitSymbol) {
11393
+ const fitted = fitTextToContextBudget(
11394
+ formatDefinitionLookup(definitions, lookupSymbol),
11395
+ tokenBudget
11396
+ );
11397
+ return { text: fitted.text };
11398
+ }
11399
+ }
11400
+ const results = await operations.search(input.query, MAX_CONTEXT_RESULT_LIMIT);
11401
+ if (results.length === 0) {
11402
+ const fitted = fitTextToContextBudget(
11403
+ "No matching code found. Try a different query or run index_codebase first.",
11404
+ tokenBudget
11405
+ );
11406
+ return { text: fitted.text };
11407
+ }
11408
+ return packedResult("conceptual", input.query, buildContextPack(results, {
11409
+ tokenBudget,
11410
+ maxResults: limit,
11411
+ heading: `Codebase evidence for ${JSON.stringify(input.query)}`
11412
+ }));
11413
+ }
11414
+ function fittedDetails(route, fitted) {
11415
+ return {
11416
+ route,
11417
+ tokenBudget: fitted.tokenBudget,
11418
+ tokenEstimate: fitted.tokenEstimate,
11419
+ truncated: fitted.truncated
11420
+ };
11421
+ }
11422
+ async function resolveCodebaseContext(projectRoot3, host, input) {
11423
+ const from = input.from ?? void 0;
11424
+ const to = input.to ?? void 0;
11425
+ const symbol = input.symbol ?? void 0;
11426
+ const limit = input.limit ?? 10;
11427
+ const maxDepth = input.maxDepth ?? 10;
11428
+ const fileType = input.fileType ?? void 0;
11429
+ const directory = input.directory ?? void 0;
11430
+ const tokenBudget = input.tokenBudget ?? void 0;
11431
+ if (from && to) {
11432
+ const path17 = await getCallGraphPath(projectRoot3, host, from, to, maxDepth);
11433
+ if (path17.length > 0) {
11434
+ const fitted2 = fitTextToContextBudget(
11435
+ formatCallGraphPath(from, to, path17),
11436
+ tokenBudget
11437
+ );
11438
+ return {
11439
+ text: fitted2.text,
11440
+ details: fittedDetails("path", fitted2)
11441
+ };
11442
+ }
11443
+ const { callers } = await getCallGraphData(projectRoot3, host, {
11444
+ name: to,
11445
+ direction: "callers"
11446
+ });
11447
+ const directEdge = callers.find((edge) => edge.fromSymbolName === from);
11448
+ if (directEdge) {
11449
+ const location = directEdge.fromSymbolFilePath ? ` at ${directEdge.fromSymbolFilePath}:${directEdge.line}` : "";
11450
+ const fitted2 = fitTextToContextBudget(
11451
+ `Direct path: ${from} --${directEdge.callType}--> ${to}${location} (edge is ${directEdge.isResolved ? "resolved" : "unresolved"}).`,
11452
+ tokenBudget
11453
+ );
11454
+ return {
11455
+ text: fitted2.text,
11456
+ details: fittedDetails("direct-edge", fitted2)
11457
+ };
11458
+ }
11459
+ const fitted = fitTextToContextBudget(
11460
+ formatCallGraphPath(from, to, path17),
11461
+ tokenBudget
11462
+ );
11463
+ return {
11464
+ text: fitted.text,
11465
+ details: fittedDetails("path", fitted)
11466
+ };
11467
+ }
11468
+ return resolveSearchContext({ query: input.query, symbol, limit, tokenBudget }, {
11469
+ lookup: (lookupSymbol, retrievalLimit) => implementationLookup(projectRoot3, host, lookupSymbol, {
11470
+ limit: retrievalLimit,
11471
+ fileType,
11472
+ directory
11473
+ }),
11474
+ search: (query, retrievalLimit) => searchCodebase(projectRoot3, host, query, {
11475
+ limit: retrievalLimit,
11476
+ fileType,
11477
+ directory,
11478
+ metadataOnly: true
11479
+ })
11480
+ });
11481
+ }
11482
+
10580
11483
  // src/pi-call-graph.ts
10581
11484
  import { Type } from "typebox";
10582
11485
  var HOST = "pi";
@@ -10650,10 +11553,42 @@ function projectRoot2(ctx) {
10650
11553
  return ctx?.cwd ?? process.cwd();
10651
11554
  }
10652
11555
  function codebaseIndexPiExtension(pi) {
11556
+ pi.registerTool({
11557
+ name: "codebase_context",
11558
+ label: "Codebase Context",
11559
+ 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.",
11560
+ parameters: Type2.Object({
11561
+ query: Type2.String({ description: "Natural language description of what code you're trying to locate" }),
11562
+ from: Type2.Optional(Type2.Union([Type2.String(), Type2.Null()], { description: "Source symbol when asking for a dependency path." })),
11563
+ to: Type2.Optional(Type2.Union([Type2.String(), Type2.Null()], { description: "Target symbol when asking for a dependency path." })),
11564
+ symbol: Type2.Optional(Type2.Union([Type2.String(), Type2.Null()], { description: "Exact symbol name for an authoritative definition lookup." })),
11565
+ limit: Type2.Optional(Type2.Union([
11566
+ Type2.Integer({ minimum: MIN_CONTEXT_RESULT_LIMIT, maximum: MAX_CONTEXT_RESULT_LIMIT }),
11567
+ Type2.Null()
11568
+ ], { default: 10, description: `Maximum results (${MIN_CONTEXT_RESULT_LIMIT}-${MAX_CONTEXT_RESULT_LIMIT})` })),
11569
+ maxDepth: Type2.Optional(Type2.Union([
11570
+ Type2.Integer({ minimum: MIN_CONTEXT_PATH_DEPTH, maximum: MAX_CONTEXT_PATH_DEPTH }),
11571
+ Type2.Null()
11572
+ ], { default: 10, description: `Maximum call-graph traversal depth (${MIN_CONTEXT_PATH_DEPTH}-${MAX_CONTEXT_PATH_DEPTH})` })),
11573
+ fileType: Type2.Optional(Type2.Union([Type2.String(), Type2.Null()], { description: "Filter by file extension, e.g., ts, py, rs" })),
11574
+ directory: Type2.Optional(Type2.Union([Type2.String(), Type2.Null()], { description: "Filter by directory path" })),
11575
+ tokenBudget: Type2.Optional(Type2.Union([
11576
+ Type2.Integer({ minimum: MIN_CONTEXT_PACK_TOKEN_BUDGET, maximum: MAX_CONTEXT_PACK_TOKEN_BUDGET }),
11577
+ Type2.Null()
11578
+ ], {
11579
+ default: DEFAULT_CONTEXT_PACK_TOKEN_BUDGET,
11580
+ description: `Maximum response tokens (${MIN_CONTEXT_PACK_TOKEN_BUDGET}-${MAX_CONTEXT_PACK_TOKEN_BUDGET})`
11581
+ }))
11582
+ }),
11583
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
11584
+ const result = await resolveCodebaseContext(projectRoot2(ctx), HOST2, params);
11585
+ return text2(result.text, result.details);
11586
+ }
11587
+ });
10653
11588
  pi.registerTool({
10654
11589
  name: "codebase_search",
10655
11590
  label: "Codebase Search",
10656
- description: "Semantic search over the indexed codebase. Describe behavior, not syntax.",
11591
+ description: "Use this after codebase_context when you need semantic content, not just locations. Describe behavior, not syntax.",
10657
11592
  parameters: Type2.Object({
10658
11593
  query: Type2.String({ description: "Natural language description of what code you're looking for" }),
10659
11594
  limit: Type2.Optional(Type2.Number({ description: "Maximum results (default: 10)" })),
@@ -10673,7 +11608,7 @@ function codebaseIndexPiExtension(pi) {
10673
11608
  pi.registerTool({
10674
11609
  name: "codebase_peek",
10675
11610
  label: "Codebase Peek",
10676
- description: "Semantic search returning only metadata (file, lines, symbol) to save tokens.",
11611
+ description: "LOW-TOKEN location-first retrieval. Prefer codebase_context first, then use this for cheap conceptual lookup.",
10677
11612
  parameters: Type2.Object({
10678
11613
  query: Type2.String(),
10679
11614
  limit: Type2.Optional(Type2.Number()),
@@ -10692,7 +11627,7 @@ function codebaseIndexPiExtension(pi) {
10692
11627
  pi.registerTool({
10693
11628
  name: "find_similar",
10694
11629
  label: "Find Similar Code",
10695
- description: "Find code similar to a snippet for duplicate detection and pattern discovery.",
11630
+ description: "Find code similar to a snippet for duplicate detection, pattern discovery, and refactor planning.",
10696
11631
  parameters: Type2.Object({
10697
11632
  code: Type2.String({ description: "Code snippet to compare" }),
10698
11633
  limit: Type2.Optional(Type2.Number()),
@@ -10709,7 +11644,7 @@ function codebaseIndexPiExtension(pi) {
10709
11644
  pi.registerTool({
10710
11645
  name: "implementation_lookup",
10711
11646
  label: "Implementation Lookup",
10712
- description: "Find likely symbol definitions or implementations by name or natural language.",
11647
+ description: "Find likely symbol definitions or implementations after codebase_context identifies a symbol.",
10713
11648
  parameters: Type2.Object({
10714
11649
  query: Type2.String(),
10715
11650
  limit: Type2.Optional(Type2.Number()),
@@ -10724,7 +11659,7 @@ function codebaseIndexPiExtension(pi) {
10724
11659
  pi.registerTool({
10725
11660
  name: "index_codebase",
10726
11661
  label: "Index Codebase",
10727
- description: "Build or refresh the semantic codebase index.",
11662
+ description: "Build or refresh the semantic codebase index. Run index_status when freshness is unknown.",
10728
11663
  parameters: Type2.Object({
10729
11664
  force: Type2.Optional(Type2.Boolean({ default: false })),
10730
11665
  estimateOnly: Type2.Optional(Type2.Boolean({ default: false })),
@@ -10750,7 +11685,7 @@ function codebaseIndexPiExtension(pi) {
10750
11685
  pi.registerTool({
10751
11686
  name: "index_health_check",
10752
11687
  label: "Index Health Check",
10753
- description: "Garbage collect orphaned embeddings/chunks and report health.",
11688
+ description: "Garbage collect orphaned embeddings/chunks and report index health status.",
10754
11689
  parameters: Type2.Object({}),
10755
11690
  async execute(_toolCallId, _params, _signal, _onUpdate, ctx) {
10756
11691
  const result = await runIndexHealthCheck(projectRoot2(ctx), HOST2);
@@ -10790,6 +11725,11 @@ function codebaseIndexPiExtension(pi) {
10790
11725
  }
10791
11726
  });
10792
11727
  registerPiCallGraphTools(pi);
11728
+ pi.on("before_agent_start", (event) => ({
11729
+ systemPrompt: `${event.systemPrompt}
11730
+
11731
+ Check index_status first when index readiness is unknown. For repository questions, call codebase_context before search/grep/bash/read-style broad reads. Use implementation_lookup for known symbols and call_graph/call_graph_path after endpoints are identified for dependency flow. Avoid broad tool calls until semantic locations are known.`
11732
+ }));
10793
11733
  pi.registerTool({
10794
11734
  name: "pr_impact",
10795
11735
  label: "PR Impact",