forked from Blendman974/faceclaw
Fixes to evenhub extensions library
This commit is contained in:
1 parent
4bf2769d24
commit
07859c95ad
8 files changed
+203
-34
No files matched your search
@@ -1055,6 +1055,12 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
);
|
||||
}
|
||||
|
||||
/** Log a problem with the app's own calls where its developer will see it: the app's console. */
|
||||
private warnApp(message: string): void {
|
||||
this.log(`evenhub: ${message}`);
|
||||
this.webViewHandle?.evaluateJs(`window.console && console.warn(${JSON.stringify(`Faceclaw: ${message}`)})`);
|
||||
}
|
||||
|
||||
/** Deliver an extension event to the app (e.g. windowLifecycle). */
|
||||
private pushExtEvent(name: string, data: unknown): void {
|
||||
this.webViewHandle?.evaluateJs(
|
||||
@@ -1266,12 +1272,16 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
for (const raw of rawSpecs) {
|
||||
const record = asRecord(raw);
|
||||
const name = readString(record, "name", "");
|
||||
if (!name) continue;
|
||||
const problem = appToolProblem(name, record.parameters);
|
||||
if (problem) {
|
||||
this.warnApp(`setAssistantTools: skipped tool ${JSON.stringify(name)}: ${problem}`);
|
||||
continue;
|
||||
}
|
||||
const availability = record.availability === "open" ? "open" : "foreground";
|
||||
specs.push({
|
||||
name,
|
||||
description: readString(record, "description", ""),
|
||||
inputSchema: asRecord(record.parameters),
|
||||
inputSchema: record.parameters == null ? { type: "object", properties: {} } : asRecord(record.parameters),
|
||||
availability,
|
||||
});
|
||||
}
|
||||
@@ -1362,13 +1372,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
if (steps.length === 0) return;
|
||||
for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) {
|
||||
const chunk = steps.slice(i, i + CFW_SEQ_MAX);
|
||||
const payload = buildSoundSequencePayload(chunk);
|
||||
try {
|
||||
await this.sendBuzzerSequence(payload);
|
||||
} catch (error) {
|
||||
this.log(`evenhub: playBuzzer failed: ${error}`);
|
||||
return;
|
||||
}
|
||||
// A send failure rejects the app's promise (handleExtensionCall logs it).
|
||||
await this.sendBuzzerSequence(buildSoundSequencePayload(chunk));
|
||||
if (i + CFW_SEQ_MAX < steps.length) await delay(phraseDurationMs(chunk));
|
||||
}
|
||||
}
|
||||
@@ -1511,6 +1516,35 @@ function configuredApiKeyServices(): string[] {
|
||||
return API_KEY_SERVICES.filter((service) => service.setting.get().trim()).map((service) => service.id);
|
||||
}
|
||||
|
||||
/** App assistant-tool names: what every model provider accepts in a tool name. */
|
||||
const APP_TOOL_NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
||||
|
||||
/**
|
||||
* Why an app's assistant tool can't be offered, or null if it can. Provider
|
||||
* APIs reject a whole request over one bad tool definition, so offering it
|
||||
* would break every assistant turn while the app runs.
|
||||
*/
|
||||
function appToolProblem(name: string, parameters: unknown): string | null {
|
||||
if (!APP_TOOL_NAME.test(name)) return "name must be 1-64 letters, digits, underscores or hyphens";
|
||||
if (parameters == null) return null;
|
||||
const schema = parameters as Record<string, unknown>;
|
||||
if (typeof parameters !== "object" || Array.isArray(parameters) || schema.type !== "object") {
|
||||
return 'parameters must be a JSON Schema with type "object"';
|
||||
}
|
||||
if ("oneOf" in schema || "anyOf" in schema || "allOf" in schema) {
|
||||
return "parameters can't use oneOf, anyOf or allOf at the top level";
|
||||
}
|
||||
const properties = schema.properties;
|
||||
if (properties !== undefined && (!properties || typeof properties !== "object" || Array.isArray(properties))) {
|
||||
return "parameters.properties must be an object";
|
||||
}
|
||||
const required = schema.required;
|
||||
if (required !== undefined && !(Array.isArray(required) && required.every((key) => typeof key === "string"))) {
|
||||
return "parameters.required must be an array of property names";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Coerce an app-supplied buzzer step list into validated {freq, ms, duty?}. */
|
||||
function normalizeBuzzerSteps(raw: unknown[]): Step[] {
|
||||
const out: Step[] = [];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user