mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 05:12:11 +03:00
Release v3.8.42 — full CHANGELOG in CHANGELOG.md. CI: 103 checks green incl. CodeQL (all languages), Semgrep, all 8 unit shards, coverage, Node 24 compat, and integration tests. Full unit suite validated locally: 19437 pass / 0 fail. The 3 red checks are advisory and do not gate main (no required status checks): SonarCloud/SonarQube new-code coverage gate, and PR Test Policy (test-masking detector flagging the legitimate dead-Phind provider removal in #5530 — reviewed, correct). Includes cycle-close reconciliation + repair of inherited base-red tests from #5480/#5527/#5427/#5521 that the PR->release fast-path did not exercise.
82 lines
2.1 KiB
TypeScript
82 lines
2.1 KiB
TypeScript
import crypto from "crypto";
|
|
|
|
interface Message {
|
|
role: string;
|
|
content: string | unknown[];
|
|
}
|
|
|
|
interface PrefixAnalysis {
|
|
prefixEndIdx: number;
|
|
prefixHash: string;
|
|
prefixTokens: number;
|
|
prefixType: "system_only" | "system_and_tools" | "system_tools_history";
|
|
confidence: number;
|
|
}
|
|
|
|
function normalizeContent(content: string | unknown[]): string {
|
|
if (typeof content === "string") return content;
|
|
return JSON.stringify(content);
|
|
}
|
|
|
|
function estimateTokens(text: string): number {
|
|
return Math.ceil(text.length / 4);
|
|
}
|
|
|
|
export function analyzePrefix(messages: Message[]): PrefixAnalysis {
|
|
if (!Array.isArray(messages) || messages.length === 0) {
|
|
return {
|
|
prefixEndIdx: -1,
|
|
prefixHash: "",
|
|
prefixTokens: 0,
|
|
prefixType: "system_only",
|
|
confidence: 0,
|
|
};
|
|
}
|
|
|
|
let prefixEndIdx = -1;
|
|
let prefixType: PrefixAnalysis["prefixType"] = "system_only";
|
|
let confidence = 0.5;
|
|
|
|
for (let i = 0; i < messages.length; i++) {
|
|
const msg = messages[i];
|
|
const role = msg.role || "user";
|
|
|
|
if (role === "system") {
|
|
prefixEndIdx = i;
|
|
prefixType = "system_only";
|
|
confidence = 0.9;
|
|
} else if (role === "tool" || (role === "assistant" && Array.isArray(msg.content))) {
|
|
prefixEndIdx = i;
|
|
prefixType = "system_and_tools";
|
|
confidence = 0.8;
|
|
} else if (role === "assistant") {
|
|
prefixEndIdx = i;
|
|
prefixType = "system_tools_history";
|
|
confidence = 0.7;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
const prefixMessages = messages.slice(0, prefixEndIdx + 1);
|
|
const prefixText = prefixMessages.map((m) => normalizeContent(m.content)).join("\n");
|
|
const prefixHash = crypto.createHash("sha256").update(prefixText).digest("hex");
|
|
const prefixTokens = estimateTokens(prefixText);
|
|
|
|
return {
|
|
prefixEndIdx,
|
|
prefixHash,
|
|
prefixTokens,
|
|
prefixType,
|
|
confidence,
|
|
};
|
|
}
|
|
|
|
export function generatePromptCacheKey(messages: Message[]): string {
|
|
const analysis = analyzePrefix(messages);
|
|
if (analysis.prefixHash) {
|
|
return `omni-${analysis.prefixHash.slice(0, 32)}`;
|
|
}
|
|
return "";
|
|
}
|