This commit is contained in:
seb
2026-10-05 12:03:05 +02:00
parent 9b24e8ece4
commit 8609a8f26d
3 changed files with 150 additions and 155 deletions

View File

@@ -45,13 +45,12 @@ Exports: `LLMOrbiter`, `OpenRouterProvider`, `defineTool`, `ToolError`, `DirectR
### `orbiter.run(opts)`
`{ prompt, messages, model, tools, maxIterations=10, tool_choice, extraBody, signal }`.
`{ prompt, messages, model, tools, maxIterations=10, extraBody, signal }`.
- `prompt`: user message string to append (omit or pass `null` to continue without adding one).
- `messages`: transcript array to seed from; a leading `{ role: 'system' }` is the system prompt.
- `tools`: array of tool objects created with `defineTool()`.
- `maxIterations`: safety limit on tool-calling loop iterations (default: `10`).
- `tool_choice`: tool choice setting (defaults to `'auto'` when tools are present).
- `extraBody`: extra parameters merged into provider request payload.
- `signal`: external `AbortSignal`.
@@ -80,7 +79,7 @@ Returns:
- `sessionId` sets session ID / cache key (defaults to a process-lifetime UUID created when the app starts; sent via `x-session-id` HTTP header and `session_id` body parameter).
Method:
`chatCompletions({ model, messages, tools, tool_choice, signal, extraBody, onStatus, onContent, onReasoning, sessionId })`
`chatCompletions({ model, messages, tools, signal, extraBody, onStatus, onContent, onReasoning, sessionId })`
— streams SSE, assembles `{ content, tool_calls, finish_reason, usage, reasoning_details, reasoning }`.
- If reasoning was returned, automatically appends `{ role: 'assistant', content: result.reasoning }` to `messages`.
- Delta handling:

View File

@@ -32,7 +32,7 @@ function ensureAbortError(reason) {
*/
class LLMOrbiter {
/**
* @param {Object} provider — must implement chatCompletions({ model, messages, tools, tool_choice, signal, onStatus, onContent, onReasoning })
* @param {Object} provider — must implement chatCompletions({ model, messages, tools, signal, onStatus, onContent, onReasoning })
* @param {Object} [opts]
* @param {Function} [opts.onLLMCall] — called before LLM request ({ model, toolCount, promptSize, messageCount, iteration })
* @param {Function} [opts.onUsage] — called with usage info ({ cached, input, output, cost })
@@ -71,7 +71,6 @@ class LLMOrbiter {
* @param {string} [opts.model] — model identifier (provider-specific default if omitted)
* @param {Array} [opts.tools] — array of Tool objects from defineTool()
* @param {number} [opts.maxIterations=10] — safety limit for tool-call loops
* @param {Object} [opts.tool_choice] — force a specific tool or "auto"
* @param {Object} [opts.extraBody] — extra fields merged into the request body (e.g. reasoning)
* @param {AbortSignal} [opts.signal] — optional external abort signal
* @returns {Promise<RunResult>}
@@ -83,7 +82,6 @@ class LLMOrbiter {
model,
tools = [],
maxIterations = 10,
tool_choice,
extraBody = {},
signal,
} = opts;
@@ -105,9 +103,9 @@ class LLMOrbiter {
this._activeController = controller;
const runSignal = controller.signal;
const call = (fn, ...args) => {
const call = async (fn, ...args) => {
if (fn) {
try { fn(...args); } catch (_) {}
try { await fn(...args); } catch (_) { }
}
};
@@ -169,7 +167,7 @@ class LLMOrbiter {
return [m.role];
}).join(',');
call(onLLMCall, {
await call(onLLMCall, {
model,
toolCount,
tools: toolCount,
@@ -183,7 +181,6 @@ class LLMOrbiter {
model,
messages,
tools: toolSpecs.length ? toolSpecs : undefined,
tool_choice: toolSpecs.length ? (tool_choice || 'auto') : undefined,
signal: runSignal,
extraBody,
onStatus: (s) => call(onStatus, s),
@@ -196,7 +193,7 @@ class LLMOrbiter {
if (completion.usage) {
rawUsages.push(completion.usage);
}
call(onUsage, callUsage);
await call(onUsage, callUsage);
const toolCalls = completion.tool_calls;
const content = completion.content;
@@ -236,7 +233,7 @@ class LLMOrbiter {
isError = true;
errorMsg = `Unknown tool: ${fnName}`;
result = { error: errorMsg };
call(onToolError, { name: fnName, error: errorMsg });
await call(onToolError, { name: fnName, error: errorMsg });
} else {
let args;
try {
@@ -245,11 +242,11 @@ class LLMOrbiter {
isError = true;
errorMsg = `Invalid JSON arguments: ${e.message}`;
result = { error: errorMsg };
call(onToolError, { name: fnName, error: errorMsg });
await call(onToolError, { name: fnName, error: errorMsg });
}
if (!isError) {
call(onToolCall, { name: fnName, args, id: callItem.id, callItem, tool_calls: toolCalls, reasoning_details: completion.reasoning_details, reasoning: completion.reasoning, content: content || null });
await call(onToolCall, { name: fnName, args, id: callItem.id, callItem, tool_calls: toolCalls, reasoning_details: completion.reasoning_details, reasoning: completion.reasoning, content: content || null });
try {
result = await invokeFn(args, {
@@ -289,14 +286,14 @@ class LLMOrbiter {
errorMsg = 'Tool crashed due to an unspecified internal error';
}
result = { error: errorMsg };
call(onToolError, { name: fnName, error: errorMsg, args, rawError: err, id: callItem.id, callItem });
await call(onToolError, { name: fnName, error: errorMsg, args, rawError: err, id: callItem.id, callItem });
}
// Also handle tools that return { error: ... } instead of throwing
if (!isError && result && typeof result === 'object' && 'error' in result && result.error) {
isError = true;
errorMsg = typeof result.error === 'string' ? result.error : JSON.stringify(result.error);
call(onToolError, { name: fnName, error: errorMsg, args });
await call(onToolError, { name: fnName, error: errorMsg, args });
}
if (result && typeof result === 'object' && Array.isArray(result.appendMessages)) {
@@ -314,7 +311,7 @@ class LLMOrbiter {
allToolResults[fnName].push(result);
if (!isError) {
call(onToolResult, { name: fnName, result, id: callItem.id, callItem });
await call(onToolResult, { name: fnName, result, id: callItem.id, callItem });
}
// Feed result back to conversation

View File

@@ -41,7 +41,7 @@ class OpenRouterProvider {
* Streaming chat completion against OpenRouter.
* Returns assembled { content, tool_calls, finish_reason, usage, reasoning_details }.
*/
async chatCompletions({ model, messages, tools, tool_choice, signal, extraBody = {}, onStatus, onContent, onReasoning, sessionId }) {
async chatCompletions({ model, messages, tools, signal, extraBody = {}, onStatus, onContent, onReasoning, sessionId }) {
if (!model) throw new Error('model is required');
const effectiveSessionId = sessionId || extraBody.session_id || this.sessionId;
const body = {
@@ -55,7 +55,6 @@ class OpenRouterProvider {
...extraBody,
};
if (tools?.length) body.tools = tools;
if (tool_choice) body.tool_choice = tool_choice;
const headers = {
'Content-Type': 'application/json',