chore: some retry logic
This commit is contained in:
+27
-8
@@ -2,6 +2,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
||||
import { Ollama, type Message, type ToolCall } from "ollama";
|
||||
import { log } from "../utils/cli.ts";
|
||||
import { retry } from "../utils/retry.ts";
|
||||
import { agent, type AgentResult, type AgentRunContext } from "./shared.ts";
|
||||
|
||||
const DEFAULT_MODEL = "qwen3.6:35b";
|
||||
@@ -113,20 +114,38 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise<AgentResult> {
|
||||
|
||||
let response: Awaited<ReturnType<typeof ollama.chat>>;
|
||||
try {
|
||||
response = await ollama.chat({
|
||||
model,
|
||||
messages,
|
||||
tools,
|
||||
keep_alive: -1,
|
||||
think: false,
|
||||
options: { num_ctx: 262144 },
|
||||
});
|
||||
response = await retry(
|
||||
() => ollama.chat({
|
||||
model,
|
||||
messages,
|
||||
tools,
|
||||
keep_alive: -1,
|
||||
think: false,
|
||||
options: { num_ctx: 262144 },
|
||||
}),
|
||||
{
|
||||
delaysMs: [3_000, 8_000],
|
||||
shouldRetry: (err) => {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
return /unexpected EOF|XML syntax error|ECONNRESET|ETIMEDOUT|fetch failed/i.test(msg);
|
||||
},
|
||||
label: `Ollama turn ${iterations}`,
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
const lastError = err instanceof Error ? err.message : String(err);
|
||||
log.error(`Ollama error: ${lastError}`);
|
||||
return { success: false, error: `Ollama request failed: ${lastError}` };
|
||||
}
|
||||
|
||||
const promptTokens = (response as Record<string, unknown>).prompt_eval_count as number | undefined;
|
||||
const evalTokens = (response as Record<string, unknown>).eval_count as number | undefined;
|
||||
if (promptTokens !== undefined) {
|
||||
const total = promptTokens + (evalTokens ?? 0);
|
||||
const pct = Math.round((total / 262144) * 100);
|
||||
log.info(` context: ${promptTokens} prompt + ${evalTokens ?? 0} eval = ${total} tokens (${pct}% of limit)`);
|
||||
}
|
||||
|
||||
const assistantMessage = response.message;
|
||||
messages.push(assistantMessage);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user