Fixes to evenhub extensions library

This commit is contained in:
James Babcock committed 2026-10-24 01:51:34 -07:00
1 parent 4bf2769d24
commit 07859c95ad
8 files changed
+203 -34

No files matched your search

+28 -6
View File
@@ -34,6 +34,11 @@ export type AssistantBackendConfig =
/** Trim history from the head once it grows past this many messages. */
const MAX_HISTORY_MESSAGES = 40;
/** The longest tool name every provider accepts (OpenAI allows 64; Anthropic, 128). */
const MAX_API_TOOL_NAME_LENGTH = 64;
/** Characters of the sanitized name kept at the start when shortening it. */
const SHORTENED_TOOL_NAME_HEAD = 16;
/** Hermes brings its own persona and tools; it only needs to know where the reply is shown. */
const HERMES_SYSTEM_PROMPT =
@@ -237,13 +242,20 @@ export class AssistantSession {
/** Convert a canonical registry name to a provider-legal tool name. */
private toApiToolName(name: string): string {
let base = name.replace(/[^a-zA-Z0-9_-]/g, "_");
// Disambiguate the rare case where two canonical names sanitize alike.
if (this.toolNameMap.has(base)) {
let i = 2;
while (this.toolNameMap.has(`${base}_${i}`)) i++;
base = `${base}_${i}`;
if (base.length > MAX_API_TOOL_NAME_LENGTH) {
// Keep the start (the namespace) and the end (the tool's own name). The
// hash of the whole name keeps shortened names distinct and stable.
const hash = fnv32(name).toString(16).padStart(8, "0");
const tail = MAX_API_TOOL_NAME_LENGTH - SHORTENED_TOOL_NAME_HEAD - hash.length - 2;
base = `${base.slice(0, SHORTENED_TOOL_NAME_HEAD)}_${hash}_${base.slice(-tail)}`;
}
return base;
// Disambiguate the rare case where two canonical names sanitize alike.
let apiName = base;
for (let i = 2; this.toolNameMap.has(apiName); i++) {
const suffix = `_${i}`;
apiName = base.slice(0, MAX_API_TOOL_NAME_LENGTH - suffix.length) + suffix;
}
return apiName;
}
private trimHistory(): void {
@@ -266,3 +278,13 @@ function startsWithToolResult(message: LlmMessage): boolean {
message.content[0]!.type === "tool_result"
);
}
/** FNV-1a over UTF-16 code units: a small stable hash for shortened tool names. */
function fnv32(text: string): number {
let hash = 2166136261;
for (let i = 0; i < text.length; i++) {
hash ^= text.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}