threenative-engine-mcp 0.2.22 → 0.2.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +303 -206
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,8 @@ mechanics. Start with the installed `threenative-capabilities` skill, then `prd-
|
|
|
5
5
|
owns the discovery order and plan record.
|
|
6
6
|
|
|
7
7
|
The server exposes `engine_search_capabilities`, `engine_capability_detail`,
|
|
8
|
-
`engine_search_references`, `engine_reference_detail`,
|
|
8
|
+
`engine_search_references`, `engine_reference_detail`, `engine_save_reference` and
|
|
9
|
+
`engine_watch_reference`.
|
|
9
10
|
Capability search works offline and finds a capability by an exact exported symbol name as well as
|
|
10
11
|
by situation. Full requests use `scope: "request"` (up to 15 deduplicated
|
|
11
12
|
matches); mechanic searches use `scope: "mechanic"` (up to five). A row carries the answer —
|
package/dist/index.d.ts
CHANGED
|
@@ -111,7 +111,7 @@ declare function searchCapabilities(situation: string, manifestFile?: string, sc
|
|
|
111
111
|
/**
|
|
112
112
|
* Every manifest entry named `symbol` (optionally in one `importPath`); throws when there is none.
|
|
113
113
|
* Two packages can export one name (`createThreeObject` from raw-unreal and ueformat), so a lookup
|
|
114
|
-
* by name alone must return both
|
|
114
|
+
* by name alone must return both.
|
|
115
115
|
*/
|
|
116
116
|
declare function capabilityDetails(symbol: string, manifestFile?: string, importPath?: string): readonly ICapabilityDetail[];
|
|
117
117
|
/** One capability's detail; code callers must name `importPath` when the symbol is shared. */
|
package/dist/index.js
CHANGED
|
@@ -702,6 +702,7 @@ function undouble(stem2) {
|
|
|
702
702
|
return stem2.length > 3 && last !== void 0 && last === stem2.at(-2) && !"aeiou".includes(last) ? stem2.slice(0, -1) : stem2;
|
|
703
703
|
}
|
|
704
704
|
function stem(token) {
|
|
705
|
+
if (token === "leaves") return "leaf";
|
|
705
706
|
const stripped = suffixStripped(token);
|
|
706
707
|
return stripped.length > 3 && stripped.endsWith("e") ? stripped.slice(0, -1) : stripped;
|
|
707
708
|
}
|
|
@@ -730,192 +731,219 @@ function tokens(value) {
|
|
|
730
731
|
}
|
|
731
732
|
return cached;
|
|
732
733
|
}
|
|
733
|
-
function
|
|
734
|
+
function lexicalIndex(rows, fieldsOf, boosts) {
|
|
735
|
+
const fields = rows.map(
|
|
736
|
+
(row) => fieldsOf(row).map((phrases) => phrases.map((text2) => ({ text: text2, words: tokens(text2) })))
|
|
737
|
+
);
|
|
738
|
+
const lengths = boosts.map((_, field) => {
|
|
739
|
+
const phrases = fields.flatMap((row) => row[field] ?? []).filter(({ words }) => words.length);
|
|
740
|
+
return phrases.reduce((sum, { words }) => sum + words.length, 0) / (phrases.length || 1);
|
|
741
|
+
});
|
|
734
742
|
const frequency = /* @__PURE__ */ new Map();
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
743
|
+
const documents = fields.map((row) => {
|
|
744
|
+
const seen = /* @__PURE__ */ new Set();
|
|
745
|
+
const indexed = row.map(
|
|
746
|
+
(phrases, field) => phrases.map(({ text: text2, words }) => {
|
|
747
|
+
const terms = /* @__PURE__ */ new Map();
|
|
748
|
+
for (const word of words) {
|
|
749
|
+
seen.add(word);
|
|
750
|
+
terms.set(word, (terms.get(word) ?? 0) + 1);
|
|
751
|
+
}
|
|
752
|
+
const average = lengths[field] || 1;
|
|
753
|
+
const norm = 1 - BM25_B + BM25_B * (words.length / average);
|
|
754
|
+
return { boost: (boosts[field] ?? 0) / norm, terms, text: text2, words };
|
|
755
|
+
})
|
|
756
|
+
);
|
|
757
|
+
for (const word of seen) frequency.set(word, (frequency.get(word) ?? 0) + 1);
|
|
758
|
+
return indexed;
|
|
759
|
+
});
|
|
760
|
+
const idf = /* @__PURE__ */ new Map();
|
|
761
|
+
for (const [word, df] of frequency) {
|
|
762
|
+
idf.set(word, Math.log1p((rows.length - df + 0.5) / (df + 0.5)));
|
|
763
|
+
}
|
|
764
|
+
return { documents, idf };
|
|
765
|
+
}
|
|
766
|
+
var BM25_B = 0.45;
|
|
767
|
+
var BM25_K1 = 1.2;
|
|
768
|
+
var DISTINCTIVE_FLOOR = Math.log1p(1 / 0.02);
|
|
769
|
+
var CAPABILITY_IDF_LIMIT = 4;
|
|
770
|
+
function hasTokenSubsequence(haystack, needle) {
|
|
771
|
+
return needle.length > 0 && needle.length <= haystack.length && haystack.some(
|
|
772
|
+
(word, start) => word === needle[0] && needle.every((part, offset) => haystack[start + offset] === part)
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
function lexicalQuery(words, idf, maxWeight = Number.POSITIVE_INFINITY) {
|
|
776
|
+
const weights = new Map(
|
|
777
|
+
[...new Set(words)].map((word) => [word, Math.min(idf.get(word) ?? 2, maxWeight)])
|
|
778
|
+
);
|
|
779
|
+
const total = [...weights.values()].reduce((sum, weight) => sum + weight, 0);
|
|
780
|
+
return { total, weights, words };
|
|
781
|
+
}
|
|
782
|
+
function scoreField(phrase, query) {
|
|
783
|
+
let matched = 0;
|
|
784
|
+
let count = 0;
|
|
785
|
+
let score = 0;
|
|
786
|
+
for (const [word, tf] of phrase.terms) {
|
|
787
|
+
const weight = query.weights.get(word);
|
|
788
|
+
if (weight === void 0) continue;
|
|
789
|
+
matched += weight;
|
|
790
|
+
count += 1;
|
|
791
|
+
score += weight * saturate(tf * phrase.boost);
|
|
792
|
+
}
|
|
793
|
+
if (count === 0) return void 0;
|
|
794
|
+
const exact = hasTokenSubsequence(query.words, phrase.words) || hasTokenSubsequence(phrase.words, query.words);
|
|
795
|
+
const bonus = exact ? 1.6 : 1;
|
|
796
|
+
const distinctive = exact && (count > 1 || query.weights.size === 1 || matched >= DISTINCTIVE_FLOOR) || matched >= DISTINCTIVE_FLOOR && (count > 1 || phrase.words.length <= 3);
|
|
797
|
+
return { boost: phrase.boost * bonus, count, distinctive, exact, phrase, score: score * bonus };
|
|
798
|
+
}
|
|
799
|
+
function bestField(phrases, query, isAlias = false) {
|
|
800
|
+
let best;
|
|
801
|
+
for (const phrase of phrases ?? []) {
|
|
802
|
+
const match = scoreField(phrase, query);
|
|
803
|
+
if (match === void 0 || isAlias && match.count === 1 && phrase.words.length > 1) continue;
|
|
804
|
+
if (best === void 0 || match.score > best.score) best = match;
|
|
805
|
+
}
|
|
806
|
+
return best;
|
|
807
|
+
}
|
|
808
|
+
function saturate(tf) {
|
|
809
|
+
return (BM25_K1 + 1) * tf / (BM25_K1 + tf);
|
|
810
|
+
}
|
|
811
|
+
function bm25f(query, fields, clauses = []) {
|
|
812
|
+
if (fields.every((field) => field === void 0)) return 0;
|
|
813
|
+
let matched = 0;
|
|
814
|
+
let score = 0;
|
|
815
|
+
for (const [word, weight] of query.weights) {
|
|
816
|
+
let tf = 0;
|
|
817
|
+
for (const field of fields) {
|
|
818
|
+
if (field !== void 0) tf += field.boost * (field.phrase.terms.get(word) ?? 0);
|
|
741
819
|
}
|
|
820
|
+
if (tf === 0) continue;
|
|
821
|
+
matched += weight;
|
|
822
|
+
score += weight * saturate(tf);
|
|
742
823
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
824
|
+
if (matched === 0) return 0;
|
|
825
|
+
let coverage = matched / query.total;
|
|
826
|
+
for (const clause of clauses) {
|
|
827
|
+
let clauseMatched = 0;
|
|
828
|
+
let count = 0;
|
|
829
|
+
for (const [word, weight] of clause.weights) {
|
|
830
|
+
if (!fields.some((field) => field?.phrase.terms.has(word))) continue;
|
|
831
|
+
clauseMatched += weight;
|
|
832
|
+
count += 1;
|
|
833
|
+
}
|
|
834
|
+
if (count > 1) coverage = Math.max(coverage, 0.55 * (clauseMatched / clause.total));
|
|
835
|
+
}
|
|
836
|
+
const evidence = 1 - 0.25 * Math.exp(-matched / 5);
|
|
837
|
+
return score / matched * coverage ** 0.65 * evidence;
|
|
746
838
|
}
|
|
747
|
-
function
|
|
748
|
-
return
|
|
839
|
+
function weakMatch(score) {
|
|
840
|
+
return RELEVANCE_FLOOR * score / (1 + score);
|
|
749
841
|
}
|
|
750
842
|
function nameWords(value) {
|
|
751
|
-
return
|
|
843
|
+
return splitSymbolCase(value.replace(/([a-zA-Z])(\d+)/gu, "$1 $2")).toLowerCase().split(/[^a-z]+/u).filter((word) => word.length > 1 && !STOP_WORDS.has(word));
|
|
752
844
|
}
|
|
753
845
|
function normaliseName(value) {
|
|
754
|
-
return value.
|
|
755
|
-
}
|
|
756
|
-
function spelledWords(value) {
|
|
757
|
-
return value.toLocaleLowerCase().split(/[^a-z0-9]+/u).filter((word) => word.length > 0);
|
|
846
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/gu, "");
|
|
758
847
|
}
|
|
759
|
-
function
|
|
760
|
-
const words =
|
|
761
|
-
const
|
|
848
|
+
function spelledNames(value) {
|
|
849
|
+
const words = value.toLowerCase().match(/[a-z0-9]+/gu) ?? [];
|
|
850
|
+
const names = /* @__PURE__ */ new Set();
|
|
762
851
|
for (let start = 0; start < words.length; start += 1) {
|
|
763
|
-
let
|
|
764
|
-
for (
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
if (piece.length >= whole.length) break;
|
|
852
|
+
let name = "";
|
|
853
|
+
for (const word of words.slice(start, start + 4)) {
|
|
854
|
+
name += word;
|
|
855
|
+
names.add(name);
|
|
768
856
|
}
|
|
769
857
|
}
|
|
770
|
-
return
|
|
771
|
-
}
|
|
772
|
-
function
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
}
|
|
780
|
-
var DISTINCTIVE_SITUATION_SHARE = 0.02;
|
|
781
|
-
var DISTINCTIVE_FLOOR = Math.log(1 + 1 / DISTINCTIVE_SITUATION_SHARE);
|
|
782
|
-
var AGREEMENT_WEIGHT = 0.4;
|
|
783
|
-
var LONE_WORD_COVERAGE = 0.22;
|
|
784
|
-
function agreement(situation, query) {
|
|
785
|
-
const matched = situation.filter((token) => query.has(token)).length;
|
|
786
|
-
return 1 + AGREEMENT_WEIGHT * Math.max(0, matched - 1);
|
|
787
|
-
}
|
|
788
|
-
function scorePhrase(value, queryTokens, queryText, weights) {
|
|
789
|
-
const phrase = tokens(value);
|
|
790
|
-
const unique2 = [...new Set(phrase)];
|
|
791
|
-
const total = unique2.reduce((sum, token) => sum + weightOf(weights, token), 0);
|
|
792
|
-
const matched = unique2.filter((token) => queryTokens.has(token)).reduce((sum, token) => sum + weightOf(weights, token), 0);
|
|
793
|
-
const phraseBonus = phrase.join(" ").includes(queryText) || queryText.includes(phrase.join(" ")) ? 1 : 0;
|
|
794
|
-
if (total === 0 || matched === 0 && phraseBonus === 0) return void 0;
|
|
795
|
-
const coverage = matched / total;
|
|
796
|
-
const agreed = agreement(unique2, queryTokens);
|
|
797
|
-
return { agreed, coverage, matched, phraseBonus, score: coverage * agreed + phraseBonus };
|
|
798
|
-
}
|
|
799
|
-
function isDistinctivePhrase(candidate) {
|
|
800
|
-
if (candidate.phraseBonus > 0) return true;
|
|
801
|
-
if (candidate.matched < DISTINCTIVE_FLOOR) return false;
|
|
802
|
-
return !(candidate.agreed === 1 && candidate.coverage < LONE_WORD_COVERAGE);
|
|
803
|
-
}
|
|
804
|
-
function scoreReadableSituations(situations, queryTokens, queryText, weights) {
|
|
805
|
-
if (queryTokens.size === 0)
|
|
806
|
-
return {
|
|
807
|
-
best: 0,
|
|
808
|
-
bestReadableScore: 0,
|
|
809
|
-
bestReadableSituation: situations[0] ?? "",
|
|
810
|
-
matchedSituation: ""
|
|
811
|
-
};
|
|
812
|
-
let best = 0;
|
|
813
|
-
let matchedSituation = "";
|
|
814
|
-
let bestReadableScore = 0;
|
|
815
|
-
let bestReadableSituation = situations[0] ?? "";
|
|
816
|
-
for (const situation of situations) {
|
|
817
|
-
const candidate = scorePhrase(situation, queryTokens, queryText, weights);
|
|
818
|
-
if (candidate === void 0) continue;
|
|
819
|
-
if (candidate.score > bestReadableScore) {
|
|
820
|
-
bestReadableScore = candidate.score;
|
|
821
|
-
bestReadableSituation = situation;
|
|
822
|
-
}
|
|
823
|
-
if (isDistinctivePhrase(candidate) && candidate.score > best) {
|
|
824
|
-
best = candidate.score;
|
|
825
|
-
matchedSituation = situation;
|
|
826
|
-
}
|
|
858
|
+
return names;
|
|
859
|
+
}
|
|
860
|
+
function capabilityQuery(situation, idf, scope) {
|
|
861
|
+
const clauses = scope === "request" ? situation.split(/[,;:\n]|[.!?]+(?=\s|$)/u) : [situation];
|
|
862
|
+
const identifiers = situation.match(/[a-zA-Z0-9_$]+(?:\.[a-zA-Z0-9_$]+)*/gu) ?? [];
|
|
863
|
+
const verbatim = new Set(identifiers);
|
|
864
|
+
for (const identifier of identifiers) {
|
|
865
|
+
const qualified = /^([A-Z][\w$]*)\./u.exec(identifier)?.[1];
|
|
866
|
+
if (qualified !== void 0) verbatim.add(qualified);
|
|
827
867
|
}
|
|
828
|
-
return {
|
|
868
|
+
return {
|
|
869
|
+
...lexicalQuery(tokens(situation), idf, CAPABILITY_IDF_LIMIT),
|
|
870
|
+
clauses,
|
|
871
|
+
clauseQueries: clauses.length < 2 ? [] : clauses.map((clause) => lexicalQuery(tokens(clause), idf, CAPABILITY_IDF_LIMIT)).filter((clause) => clause.weights.size > 1),
|
|
872
|
+
names: spelledNames(situation),
|
|
873
|
+
nameWords: nameWords(situation),
|
|
874
|
+
normalised: normaliseName(situation),
|
|
875
|
+
verbatim
|
|
876
|
+
};
|
|
829
877
|
}
|
|
830
|
-
function
|
|
831
|
-
if (
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
const candidate = scorePhrase(alias, queryTokens, queryText, weights);
|
|
835
|
-
if (candidate !== void 0) best = Math.max(best, candidate.score);
|
|
878
|
+
function scoreSymbolName(entry, { name, specificity, words }, query) {
|
|
879
|
+
if (query.normalised === name) return { score: 5, specificity };
|
|
880
|
+
if (query.verbatim.has(entry.symbol) && /[A-Z0-9.]/u.test(entry.symbol)) {
|
|
881
|
+
return { score: 4, specificity };
|
|
836
882
|
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
const matchedSituation = readable.matchedSituation.length > 0 ? readable.matchedSituation : best >= RELEVANCE_FLOOR ? readable.bestReadableSituation : "";
|
|
842
|
-
return matchedSituation.length > 0 ? { matchedSituation, score: best } : { matchedSituation: "", score: 0 };
|
|
843
|
-
}
|
|
844
|
-
function situationScore(query, situations, aliases, weights) {
|
|
845
|
-
if (query.length === 0) return { matchedSituation: "", score: 0 };
|
|
846
|
-
const queryTokens = new Set(query);
|
|
847
|
-
const queryText = query.join(" ");
|
|
848
|
-
return answeredSituation(
|
|
849
|
-
scoreReadableSituations(situations, queryTokens, queryText, weights),
|
|
850
|
-
scoreAliases(aliases, queryTokens, queryText, weights)
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
var EXACT_SYMBOL_LEAD = 4;
|
|
854
|
-
var NAME_BASE_LEAD = 2;
|
|
855
|
-
var NAME_SHARE_LEAD = 1.4;
|
|
856
|
-
var SYMBOL_WEIGHT_FLOOR = 4.2;
|
|
857
|
-
var SINGLE_WORD_SHARE_FLOOR = 0.3;
|
|
858
|
-
function scoreSymbolName(symbol, situation, queryTokens, weights) {
|
|
859
|
-
const name = [...new Set(nameWords(symbol))];
|
|
860
|
-
if (name.length === 0) return void 0;
|
|
861
|
-
const nameWeights = name.map((token) => weightOf(weights, token));
|
|
862
|
-
const specificity = nameWeights.reduce((sum, weight) => sum + weight, 0);
|
|
863
|
-
const requestTotal = [...queryTokens].reduce((sum, token) => sum + weightOf(weights, token), 0);
|
|
864
|
-
const share = requestTotal === 0 ? 0 : specificity / requestTotal;
|
|
865
|
-
const nameScore = NAME_BASE_LEAD + NAME_SHARE_LEAD * Math.min(1, share);
|
|
866
|
-
if (spellsName(situation, symbol)) {
|
|
867
|
-
if (normaliseName(situation) === normaliseName(symbol)) {
|
|
868
|
-
return { score: EXACT_SYMBOL_LEAD, specificity };
|
|
869
|
-
}
|
|
870
|
-
if (name.length === 1) {
|
|
871
|
-
if (weightOf(weights, name[0] ?? "") < SYMBOL_WEIGHT_FLOOR) return void 0;
|
|
872
|
-
if (share < SINGLE_WORD_SHARE_FLOOR) return void 0;
|
|
873
|
-
return { score: (NAME_BASE_LEAD + NAME_SHARE_LEAD) * Math.min(1, share), specificity };
|
|
874
|
-
}
|
|
875
|
-
return { score: nameScore, specificity };
|
|
883
|
+
const share = Math.min(1, specificity / query.total);
|
|
884
|
+
if (query.names.has(name)) {
|
|
885
|
+
if (words.length > 1) return { score: 2 + share, specificity };
|
|
886
|
+
if (specificity >= 2.5 && share >= 0.3) return { score: 3.4 * share, specificity };
|
|
876
887
|
}
|
|
877
|
-
if (
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
if (
|
|
881
|
-
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
888
|
+
if (words.length > 1 && hasTokenSubsequence(query.nameWords, words)) {
|
|
889
|
+
return { score: 1.8 + share, specificity };
|
|
890
|
+
}
|
|
891
|
+
if (words.length > 1 && specificity >= DISTINCTIVE_FLOOR && words.every((word) => query.nameWords.includes(word))) {
|
|
892
|
+
return { score: 0.55 + share * 0.6, specificity };
|
|
893
|
+
}
|
|
894
|
+
return void 0;
|
|
895
|
+
}
|
|
896
|
+
var LIFECYCLE_INTENT = /* @__PURE__ */ new Map([
|
|
897
|
+
[
|
|
898
|
+
"ctx.quit",
|
|
899
|
+
/\bquit\b|\b(?:close|exit)\s+(?:the\s+)?(?:app(?:lication)?|game|window)\b|\b(?:app(?:lication)?|game|window)\s+(?:is\s+)?(?:closed|closes)\b/iu
|
|
900
|
+
],
|
|
901
|
+
[
|
|
902
|
+
"leaveScene",
|
|
903
|
+
/\b(?:leave|leaving|exit|exiting|unload)\s+(?:(?:the|a|current)\s+)*(?:scene|level)\b/iu
|
|
904
|
+
],
|
|
905
|
+
[
|
|
906
|
+
"onLaunchFailure",
|
|
907
|
+
/\b(?:launch|startup|boot)\s+(?:fails?|failed|failure|error|crash)\b|\b(?:fail(?:s|ed)?|error|crash)\s+(?:to\s+|during\s+)?(?:launch|start|boot)\b/iu
|
|
908
|
+
]
|
|
909
|
+
]);
|
|
910
|
+
function capabilityScore(query, entry, nameInfo, fields, scope) {
|
|
911
|
+
const symbol = scoreSymbolName(entry, nameInfo, query);
|
|
912
|
+
const intent = LIFECYCLE_INTENT.get(entry.symbol);
|
|
913
|
+
const explicit = intent === void 0 || scope === "mechanic" && entry.symbol !== "ctx.quit" || query.verbatim.has(entry.symbol) || query.normalised === nameInfo.name || query.clauses.some((clause) => intent.test(clause));
|
|
914
|
+
if (symbol !== void 0 && explicit && symbol.score >= 1.8) {
|
|
915
|
+
return { ...symbol, matchedSituation: entry.symbol };
|
|
916
|
+
}
|
|
917
|
+
const name = bestField(fields[0], query);
|
|
918
|
+
const alias = bestField(fields[1], query, true);
|
|
919
|
+
const situation = bestField(fields[2], query);
|
|
920
|
+
const summary = bestField(fields[3], query);
|
|
921
|
+
const anchoredName = name !== void 0 && (name.count > 1 || name.phrase.words[0] === query.words[0] && (situation?.count ?? 0) > 1);
|
|
922
|
+
const lexical = bm25f(
|
|
923
|
+
query,
|
|
924
|
+
[anchoredName ? name : void 0, alias, situation, summary],
|
|
925
|
+
query.clauseQueries
|
|
926
|
+
);
|
|
927
|
+
const distinctive = alias?.distinctive && entry.situations.length > 0 || situation?.distinctive || summary?.distinctive;
|
|
928
|
+
const score = distinctive && explicit ? lexical : weakMatch(lexical);
|
|
929
|
+
if (symbol !== void 0 && explicit && symbol.score > score) {
|
|
930
|
+
return { ...symbol, matchedSituation: entry.symbol };
|
|
902
931
|
}
|
|
903
932
|
return {
|
|
904
|
-
matchedSituation:
|
|
905
|
-
score
|
|
933
|
+
matchedSituation: lexical === 0 ? "" : situation?.phrase.text ?? entry.situations[0] ?? entry.summary,
|
|
934
|
+
score,
|
|
906
935
|
specificity: 0
|
|
907
936
|
};
|
|
908
937
|
}
|
|
909
938
|
function notOwnedSituationScore(query, situations) {
|
|
910
939
|
if (query.length === 0) return { matchedSituation: "", score: 0 };
|
|
911
|
-
const queryText = query.join(" ");
|
|
912
940
|
let best = 0;
|
|
913
941
|
let matchedSituation = "";
|
|
914
942
|
for (const situation of situations) {
|
|
915
943
|
const phrase = tokens(situation);
|
|
916
944
|
const overlap = new Set(phrase.filter((token) => query.includes(token))).size;
|
|
917
945
|
const score = overlap / Math.max(query.length, phrase.length);
|
|
918
|
-
const phraseBonus =
|
|
946
|
+
const phraseBonus = hasTokenSubsequence(query, phrase) || hasTokenSubsequence(phrase, query) ? 1 : 0;
|
|
919
947
|
if (overlap < (query.length >= 4 ? 2 : 1) && phraseBonus === 0) continue;
|
|
920
948
|
const candidate = score + phraseBonus;
|
|
921
949
|
if (candidate > best) {
|
|
@@ -1005,38 +1033,64 @@ function withReferenceTools(situation, response) {
|
|
|
1005
1033
|
guidance: `Reverse engineer the game with engine_search_references, then engine_reference_detail for its pinned cards. ${response.guidance}`
|
|
1006
1034
|
};
|
|
1007
1035
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1036
|
+
function indexCapabilities(entries) {
|
|
1037
|
+
const { documents, idf } = lexicalIndex(
|
|
1038
|
+
entries,
|
|
1039
|
+
(entry) => [
|
|
1040
|
+
[entry.symbol],
|
|
1041
|
+
entry.aliases,
|
|
1042
|
+
entry.situations,
|
|
1043
|
+
entry.summary.split(/(?<=[.!?])\s+/u)
|
|
1044
|
+
],
|
|
1045
|
+
[2.8, 2.1, 1.25, 0.4]
|
|
1046
|
+
);
|
|
1047
|
+
return {
|
|
1048
|
+
entries: entries.map((entry, index) => {
|
|
1049
|
+
const words = nameWords(entry.symbol);
|
|
1050
|
+
const specificity = words.reduce((sum, word) => sum + (idf.get(stem(word)) ?? 0), 0);
|
|
1051
|
+
return {
|
|
1052
|
+
entry,
|
|
1053
|
+
fields: documents[index] ?? [],
|
|
1054
|
+
symbol: { name: normaliseName(entry.symbol), specificity, words }
|
|
1055
|
+
};
|
|
1056
|
+
}),
|
|
1057
|
+
idf
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
var capabilityIndexes = /* @__PURE__ */ new WeakMap();
|
|
1061
|
+
function capabilityIndex(entries) {
|
|
1062
|
+
let index = capabilityIndexes.get(entries);
|
|
1063
|
+
if (index === void 0) {
|
|
1064
|
+
index = indexCapabilities(entries);
|
|
1065
|
+
capabilityIndexes.set(entries, index);
|
|
1014
1066
|
}
|
|
1015
|
-
return
|
|
1067
|
+
return index;
|
|
1016
1068
|
}
|
|
1017
1069
|
function searchMechanics(situation, manifest, scope, suggest = true) {
|
|
1018
|
-
const query = tokens(situation);
|
|
1019
1070
|
const limit = scope === "request" ? MAX_COMPLETE_REQUEST_RESULTS : MAX_SITUATION_RESULTS;
|
|
1020
|
-
const {
|
|
1021
|
-
const
|
|
1022
|
-
const
|
|
1071
|
+
const { entries, idf } = capabilityIndex(manifest.entries);
|
|
1072
|
+
const query = capabilityQuery(situation, idf, scope);
|
|
1073
|
+
const notOwned = notOwnedMatch(manifest, query.words);
|
|
1074
|
+
const specific = notOwned !== void 0 && isSpecificNotOwnedMatch(query.words, notOwned.matchedSituation);
|
|
1023
1075
|
const optionalPackage = specific ? `@threenative/${notOwned.entry.id}` : void 0;
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1026
|
-
);
|
|
1027
|
-
if (specific && candidates.length === 0) {
|
|
1076
|
+
const includesEntry = (entry) => optionalPackage === void 0 || entry.importPath === optionalPackage || entry.importPath.startsWith(`${optionalPackage}/`);
|
|
1077
|
+
if (specific && !manifest.entries.some(includesEntry)) {
|
|
1028
1078
|
return { guidance: notOwned.entry.guidance, results: [], verdict: "none" };
|
|
1029
1079
|
}
|
|
1030
1080
|
const floor = optionalPackage === void 0 ? RELEVANCE_FLOOR : Number.MIN_VALUE;
|
|
1031
|
-
const
|
|
1081
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1082
|
+
const scored = entries.flatMap(
|
|
1083
|
+
({ entry, fields, symbol }) => includesEntry(entry) ? [{ entry, ...capabilityScore(query, entry, symbol, fields, scope) }] : []
|
|
1084
|
+
).filter(({ matchedSituation }) => matchedSituation.length > 0).sort(
|
|
1032
1085
|
(left, right) => right.score - left.score || right.specificity - left.specificity || `${left.entry.importPath}:${left.entry.symbol}`.localeCompare(
|
|
1033
1086
|
`${right.entry.importPath}:${right.entry.symbol}`
|
|
1034
1087
|
)
|
|
1035
|
-
).filter(
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
)
|
|
1039
|
-
|
|
1088
|
+
).filter(({ entry }) => {
|
|
1089
|
+
const key = capabilitySearchKey(entry);
|
|
1090
|
+
if (seen.has(key)) return false;
|
|
1091
|
+
seen.add(key);
|
|
1092
|
+
return true;
|
|
1093
|
+
});
|
|
1040
1094
|
const results = scored.filter(({ score }) => score >= floor).slice(0, limit).map(({ entry, matchedSituation, score }) => ({
|
|
1041
1095
|
constraints: entry.constraints,
|
|
1042
1096
|
...entry.deprecated === void 0 ? {} : { deprecated: entry.deprecated },
|
|
@@ -1066,15 +1120,45 @@ function searchMechanics(situation, manifest, scope, suggest = true) {
|
|
|
1066
1120
|
...nearMatches.length > 0 ? { nearMatches } : {}
|
|
1067
1121
|
};
|
|
1068
1122
|
}
|
|
1123
|
+
function editDistance(a, b) {
|
|
1124
|
+
let costs = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
1125
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
1126
|
+
const next = [i + 1];
|
|
1127
|
+
for (let j = 0; j < b.length; j += 1) {
|
|
1128
|
+
next.push(
|
|
1129
|
+
Math.min(
|
|
1130
|
+
(next[j] ?? 0) + 1,
|
|
1131
|
+
(costs[j + 1] ?? 0) + 1,
|
|
1132
|
+
(costs[j] ?? 0) + (a[i] === b[j] ? 0 : 1)
|
|
1133
|
+
)
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
costs = next;
|
|
1137
|
+
}
|
|
1138
|
+
return costs[b.length] ?? a.length;
|
|
1139
|
+
}
|
|
1140
|
+
function nearbySymbols(symbol, entries) {
|
|
1141
|
+
const sought = normaliseName(symbol);
|
|
1142
|
+
if (sought.length < 3 || sought.length > 80) return "";
|
|
1143
|
+
const names = [...new Set(entries.map((entry) => entry.symbol))];
|
|
1144
|
+
const best = names.map((name) => {
|
|
1145
|
+
const full = normaliseName(name);
|
|
1146
|
+
const member = normaliseName(name.split(".").at(-1) ?? name);
|
|
1147
|
+
const distance = [full, member].filter((part) => Math.abs(part.length - sought.length) <= 2).reduce((lowest, part) => Math.min(lowest, editDistance(part, sought)), 3);
|
|
1148
|
+
return { distance, name };
|
|
1149
|
+
}).filter(({ distance }) => distance <= (sought.length >= 7 ? 2 : 1)).sort((a, b) => a.distance - b.distance || a.name.localeCompare(b.name)).slice(0, 3);
|
|
1150
|
+
return best.length ? ` Did you mean ${best.map(({ name }) => `'${name}'`).join(" or ")}?` : "";
|
|
1151
|
+
}
|
|
1069
1152
|
function capabilityDetails(symbol, manifestFile = defaultManifestPath(), importPath) {
|
|
1070
1153
|
if (typeof symbol !== "string" || symbol.trim().length === 0)
|
|
1071
1154
|
throw new Error("engine_capability_detail requires a non-empty symbol string.");
|
|
1072
|
-
const
|
|
1073
|
-
(candidate) =>
|
|
1155
|
+
const entries = loadCapabilityManifest(manifestFile).entries.filter(
|
|
1156
|
+
(candidate) => importPath === void 0 || candidate.importPath === importPath
|
|
1074
1157
|
);
|
|
1158
|
+
const matches = entries.filter((candidate) => candidate.symbol === symbol);
|
|
1075
1159
|
if (matches.length === 0)
|
|
1076
1160
|
throw new Error(
|
|
1077
|
-
`Unknown engine capability '${symbol}'${importPath === void 0 ? "" : ` in '${importPath}'`}
|
|
1161
|
+
`Unknown engine capability '${symbol}'${importPath === void 0 ? "" : ` in '${importPath}'`}.${nearbySymbols(symbol, entries)}`
|
|
1078
1162
|
);
|
|
1079
1163
|
return matches;
|
|
1080
1164
|
}
|
|
@@ -1271,22 +1355,29 @@ async function searchReferences(situation, limit = 5) {
|
|
|
1271
1355
|
guidance: "Explore this repository, then call engine_save_reference."
|
|
1272
1356
|
}))
|
|
1273
1357
|
];
|
|
1274
|
-
const
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
const query = tokens(situation);
|
|
1280
|
-
const querySet = new Set(query);
|
|
1281
|
-
const queryText = query.join(" ");
|
|
1358
|
+
const { documents, idf } = lexicalIndex(
|
|
1359
|
+
candidates,
|
|
1360
|
+
(stub) => [[stub.name], [stub.title], stub.tags, [stub.summary]],
|
|
1361
|
+
[2.4, 1.6, 1.2, 0.7]
|
|
1362
|
+
);
|
|
1363
|
+
const query = lexicalQuery(tokens(situation), idf);
|
|
1282
1364
|
const scored = candidates.map((stub, index) => {
|
|
1283
|
-
const
|
|
1284
|
-
const
|
|
1285
|
-
const
|
|
1286
|
-
const
|
|
1365
|
+
const matches = documents[index]?.map((field) => bestField(field, query)) ?? [];
|
|
1366
|
+
const [name, title, tag, summary] = matches;
|
|
1367
|
+
const score = bm25f(query, matches);
|
|
1368
|
+
const content = [name, tag, summary].reduce(
|
|
1369
|
+
(best, match) => match !== void 0 && (best === void 0 || match.score > best.score) ? match : best,
|
|
1370
|
+
void 0
|
|
1371
|
+
);
|
|
1372
|
+
const matched = content ?? title;
|
|
1373
|
+
const relevant = matches.some((match) => match?.distinctive);
|
|
1287
1374
|
return {
|
|
1288
|
-
near: { score
|
|
1289
|
-
stub: {
|
|
1375
|
+
near: { score, situation: matched?.phrase.text ?? "" },
|
|
1376
|
+
stub: {
|
|
1377
|
+
...stub,
|
|
1378
|
+
matchedSituation: relevant ? matched?.phrase.text ?? "" : "",
|
|
1379
|
+
score: relevant ? score : 0
|
|
1380
|
+
}
|
|
1290
1381
|
};
|
|
1291
1382
|
});
|
|
1292
1383
|
const results = scored.filter(({ stub }) => stub.score >= RELEVANCE_FLOOR).sort((a, b) => b.stub.score - a.stub.score || a.stub.id.localeCompare(b.stub.id)).slice(0, limit).map(({ stub }) => stub);
|
|
@@ -1316,26 +1407,31 @@ function referenceSuggestions(scored) {
|
|
|
1316
1407
|
title: stub.title
|
|
1317
1408
|
}));
|
|
1318
1409
|
}
|
|
1410
|
+
function searchSituation(tool, argumentsValue) {
|
|
1411
|
+
const { situation, query } = argumentsValue;
|
|
1412
|
+
if (typeof situation === "string" && situation.trim().length > 0) return situation;
|
|
1413
|
+
const hint = query === void 0 ? "" : " Pass 'situation' instead of 'query'.";
|
|
1414
|
+
throw new Error(`${tool} requires a string 'situation' argument with text.${hint}`);
|
|
1415
|
+
}
|
|
1319
1416
|
async function handleToolCall(params, manifestFile) {
|
|
1320
1417
|
const name = params.name;
|
|
1321
1418
|
const argumentsValue = isRecord(params.arguments) ? params.arguments : {};
|
|
1322
1419
|
if (name === "engine_search_capabilities") {
|
|
1323
|
-
|
|
1324
|
-
throw new Error("engine_search_capabilities requires a string 'situation' argument.");
|
|
1420
|
+
const situation = searchSituation(name, argumentsValue);
|
|
1325
1421
|
const scope = argumentsValue.scope === void 0 ? "mechanic" : argumentsValue.scope;
|
|
1326
1422
|
if (scope !== "request" && scope !== "mechanic")
|
|
1327
1423
|
throw new Error("engine_search_capabilities scope must be 'request' or 'mechanic'.");
|
|
1328
|
-
const response = searchCapabilities(
|
|
1424
|
+
const response = searchCapabilities(situation, manifestFile, scope);
|
|
1329
1425
|
logToolCall({
|
|
1330
1426
|
results: response.results.map((result) => result.symbol),
|
|
1331
1427
|
scope,
|
|
1332
|
-
situation
|
|
1428
|
+
situation,
|
|
1333
1429
|
tool: name,
|
|
1334
1430
|
verdict: response.verdict
|
|
1335
1431
|
});
|
|
1336
1432
|
return toolText({
|
|
1337
1433
|
...response,
|
|
1338
|
-
references: await searchReferences(
|
|
1434
|
+
references: await searchReferences(situation, 3).catch((error) => ({
|
|
1339
1435
|
results: [],
|
|
1340
1436
|
guidance: `references unavailable: ${String(error)}`
|
|
1341
1437
|
}))
|
|
@@ -1363,19 +1459,20 @@ async function handleToolCall(params, manifestFile) {
|
|
|
1363
1459
|
return toolText(detail);
|
|
1364
1460
|
}
|
|
1365
1461
|
if (name === "engine_search_references") {
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
const response = await searchReferences(argumentsValue.situation);
|
|
1462
|
+
const situation = searchSituation(name, argumentsValue);
|
|
1463
|
+
const response = await searchReferences(situation);
|
|
1369
1464
|
logToolCall({
|
|
1370
1465
|
tool: name,
|
|
1371
|
-
situation
|
|
1466
|
+
situation,
|
|
1372
1467
|
results: response.results.map((entry) => entry.id)
|
|
1373
1468
|
});
|
|
1374
1469
|
return toolText(response);
|
|
1375
1470
|
}
|
|
1376
1471
|
if (name === "engine_reference_detail") {
|
|
1377
1472
|
if (typeof argumentsValue.game !== "string" || typeof argumentsValue.id !== "string" || argumentsValue.kind !== "node" && argumentsValue.kind !== "card")
|
|
1378
|
-
throw new Error(
|
|
1473
|
+
throw new Error(
|
|
1474
|
+
'engine_reference_detail requires "game" and "id" strings and "kind":"node" or "kind":"card".'
|
|
1475
|
+
);
|
|
1379
1476
|
let game = await loadReferenceGame(argumentsValue.game);
|
|
1380
1477
|
const { id: id2, kind } = argumentsValue;
|
|
1381
1478
|
let entry = kind === "node" ? game.nodes.find((node) => node.id === id2) : game.cards.find((card) => card.id === id2);
|
|
@@ -1437,7 +1534,7 @@ async function handleLine(line2, manifestFile) {
|
|
|
1437
1534
|
capabilities: { tools: { listChanged: false } },
|
|
1438
1535
|
instructions: AUTHORING_INSTRUCTIONS,
|
|
1439
1536
|
protocolVersion: "2025-06-18",
|
|
1440
|
-
serverInfo: { name: "threenative-engine-mcp", version: "0.2.
|
|
1537
|
+
serverInfo: { name: "threenative-engine-mcp", version: "0.2.24" }
|
|
1441
1538
|
});
|
|
1442
1539
|
}
|
|
1443
1540
|
if (request.method === "tools/list") {
|