forked from Blendman974/faceclaw
Hermes integration survives restarts, fix error hadling, increase turn length limit
This commit is contained in:
1 parent
825559aa82
commit
de653f0fbb
6 files changed
+100
-9
No files matched your search
@@ -25,6 +25,13 @@ const MAX_ITERATIONS = 8;
|
||||
/** The on-phone model is slower per step and unreliable on long tool chains. */
|
||||
const MAX_ITERATIONS_LOCAL = 4;
|
||||
const MAX_TURN_MS = 2 * 60 * 1000;
|
||||
/**
|
||||
* Hermes runs whole multi-tool agent tasks in one request, and dropping the
|
||||
* stream interrupts the run on the server, so its cap is only a backstop. A
|
||||
* dead server is caught sooner: Hermes sends keepalives every 10s, and both
|
||||
* transports fail a stream that goes quiet for 180s.
|
||||
*/
|
||||
const MAX_TURN_MS_HERMES = 20 * 60 * 1000;
|
||||
|
||||
export type DirectTurnOptions = {
|
||||
provider: AssistantProvider;
|
||||
@@ -66,13 +73,14 @@ export class DirectAssistantBackend {
|
||||
};
|
||||
|
||||
const maxIterations = options.provider === "local" ? MAX_ITERATIONS_LOCAL : MAX_ITERATIONS;
|
||||
const maxTurnMs = options.provider === "hermes" ? MAX_TURN_MS_HERMES : MAX_TURN_MS;
|
||||
const runIteration = (iteration: number): void => {
|
||||
if (cancelled) return;
|
||||
if (iteration >= maxIterations) {
|
||||
finishError("Assistant stopped: too many tool steps");
|
||||
return;
|
||||
}
|
||||
if (Date.now() - startMs > MAX_TURN_MS) {
|
||||
if (Date.now() - startMs > maxTurnMs) {
|
||||
finishError("Assistant stopped: turn took too long");
|
||||
return;
|
||||
}
|
||||
@@ -142,7 +150,7 @@ export class DirectAssistantBackend {
|
||||
: streamAnthropicMessage(streamOptions);
|
||||
};
|
||||
|
||||
deadline = setTimeout(() => finishError("Assistant stopped: turn took too long"), MAX_TURN_MS);
|
||||
deadline = setTimeout(() => finishError("Assistant stopped: turn took too long"), maxTurnMs);
|
||||
runIteration(0);
|
||||
|
||||
return {
|
||||
|
||||
@@ -22,6 +22,8 @@ export type AssistantSessionHistory = {
|
||||
transcript: AssistantTranscriptEntry[];
|
||||
/** Model identity for safe replay after Auto resolves differently on restart. */
|
||||
engine?: string;
|
||||
/** The Hermes server-side session for this conversation (Hermes keeps the history). */
|
||||
hermesSessionId?: string;
|
||||
};
|
||||
|
||||
/** Which engine answers utterances, plus what it needs to do so. */
|
||||
@@ -48,14 +50,18 @@ export class AssistantSession {
|
||||
// API-safe tool name -> canonical registry name. Provider APIs restrict tool
|
||||
// names, so dotted registry names are sanitized and mapped back on calls.
|
||||
private readonly toolNameMap = new Map<string, string>();
|
||||
// The Hermes server-side session for this conversation (Hermes keeps the history).
|
||||
private readonly hermesSessionId = `faceclaw-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
// Saved with the history: Faceclaw only sends Hermes the latest message, so a
|
||||
// restored conversation must keep addressing the same server-side session.
|
||||
private readonly hermesSessionId: string;
|
||||
|
||||
constructor(
|
||||
private config: AssistantBackendConfig,
|
||||
private readonly registry: ToolRegistry = toolRegistry,
|
||||
history?: AssistantSessionHistory,
|
||||
) {
|
||||
this.hermesSessionId = typeof history?.hermesSessionId === "string" && history.hermesSessionId
|
||||
? history.hermesSessionId
|
||||
: `faceclaw-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
if (history) {
|
||||
this.messages.push(...history.messages);
|
||||
this.transcript.push(...history.transcript);
|
||||
@@ -75,7 +81,7 @@ export class AssistantSession {
|
||||
}
|
||||
|
||||
history(): AssistantSessionHistory {
|
||||
return { messages: this.messages, transcript: this.transcript, engine: this.engine() };
|
||||
return { messages: this.messages, transcript: this.transcript, engine: this.engine(), hermesSessionId: this.hermesSessionId };
|
||||
}
|
||||
|
||||
configure(config: AssistantBackendConfig): void {
|
||||
|
||||
+18
-3
@@ -36,9 +36,23 @@ export function streamHermesChat(options: HermesStreamOptions): LlmStreamHandle
|
||||
};
|
||||
const done = () => {
|
||||
if (settled) return;
|
||||
// A failed or interrupted run still ends with a finish chunk and [DONE]; the
|
||||
// chunk carries an error object only when Hermes has a message to show.
|
||||
if (finishReason === "error") {
|
||||
fail("Hermes: the agent run failed");
|
||||
return;
|
||||
}
|
||||
if (finishReason === "cancelled") {
|
||||
fail("Hermes: the agent run was interrupted");
|
||||
return;
|
||||
}
|
||||
if (!text) {
|
||||
fail("Hermes ended the turn without a reply");
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
request?.cancel();
|
||||
options.onDone({ text, content: text ? [{ type: "text", text }] : [], stopReason: finishReason === "length" ? "max_tokens" : "end_turn" });
|
||||
options.onDone({ text, content: [{ type: "text", text }], stopReason: finishReason === "length" ? "max_tokens" : "end_turn" });
|
||||
};
|
||||
|
||||
const baseUrl = normalizeHermesUrl(options.baseUrl);
|
||||
@@ -86,8 +100,9 @@ export function streamHermesChat(options: HermesStreamOptions): LlmStreamHandle
|
||||
if (choice?.finish_reason) finishReason = String(choice.finish_reason);
|
||||
},
|
||||
onHttpError: (code: number, errorBody: string) => fail(describeHttpError(Number(code), String(errorBody))),
|
||||
// Some servers close without [DONE] after the final chunk.
|
||||
onComplete: () => (finishReason || text ? done() : fail("Hermes stream ended without a reply")),
|
||||
// Some servers close without [DONE] after the final chunk. Hermes always sends
|
||||
// a finish_reason, so a stream closed without one was cut off.
|
||||
onComplete: () => (finishReason ? done() : fail(text ? "Hermes: the reply was cut off" : "Hermes stream ended without a reply")),
|
||||
onFailure: (message: string) => fail(`Hermes connection failed: ${String(message)}`),
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user