@lowire/loop 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cache.js +1 -1
- package/lib/loop.d.ts +2 -0
- package/lib/loop.js +19 -8
- package/package.json +1 -1
package/lib/cache.js
CHANGED
|
@@ -36,7 +36,7 @@ async function cachedCompleteNoSecrets(provider, conversation, caches, options)
|
|
|
36
36
|
if (!process.env.LOWIRE_NO_CACHE && caches.output[key])
|
|
37
37
|
return caches.output[key];
|
|
38
38
|
if (process.env.LOWIRE_FORCE_CACHE)
|
|
39
|
-
throw new Error('Cache missing but
|
|
39
|
+
throw new Error('Cache missing but LOWIRE_FORCE_CACHE is set' + JSON.stringify(conversation, null, 2));
|
|
40
40
|
const result = await provider.complete(conversation, options);
|
|
41
41
|
caches.output[key] = result;
|
|
42
42
|
return result;
|
package/lib/loop.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ export type LoopOptions = types.CompletionOptions & LoopEvents & {
|
|
|
45
45
|
tools?: types.Tool[];
|
|
46
46
|
callTool?: types.ToolCallback;
|
|
47
47
|
maxTurns?: number;
|
|
48
|
+
maxToolCalls?: number;
|
|
49
|
+
maxToolCallRetries?: number;
|
|
48
50
|
cache?: types.ReplayCache;
|
|
49
51
|
secrets?: Record<string, string>;
|
|
50
52
|
summarize?: boolean;
|
package/lib/loop.js
CHANGED
|
@@ -39,12 +39,16 @@ class Loop {
|
|
|
39
39
|
tools: allTools,
|
|
40
40
|
};
|
|
41
41
|
const debug = options.debug;
|
|
42
|
-
|
|
42
|
+
const budget = {
|
|
43
|
+
tokens: options.maxTokens,
|
|
44
|
+
toolCalls: options.maxToolCalls,
|
|
45
|
+
toolCallRetries: options.maxToolCallRetries,
|
|
46
|
+
};
|
|
43
47
|
const totalUsage = { input: 0, output: 0 };
|
|
44
|
-
debug?.('lowire:loop')(`Starting ${this._provider.name} loop
|
|
48
|
+
debug?.('lowire:loop')(`Starting ${this._provider.name} loop\n${task}`);
|
|
45
49
|
const maxTurns = options.maxTurns || 100;
|
|
46
50
|
for (let turns = 0; turns < maxTurns; ++turns) {
|
|
47
|
-
if (options.maxTokens &&
|
|
51
|
+
if (options.maxTokens && budget.tokens !== undefined && budget.tokens <= 0)
|
|
48
52
|
throw new Error(`Budget tokens ${options.maxTokens} exhausted`);
|
|
49
53
|
debug?.('lowire:loop')(`Turn ${turns + 1} of (max ${maxTurns})`);
|
|
50
54
|
const caches = options.cache ? {
|
|
@@ -52,23 +56,23 @@ class Loop {
|
|
|
52
56
|
output: this._cacheOutput,
|
|
53
57
|
} : undefined;
|
|
54
58
|
const summarizedConversation = options.summarize ? this._summarizeConversation(task, conversation, options) : conversation;
|
|
55
|
-
await options.onBeforeTurn?.({ conversation: summarizedConversation, totalUsage, budgetTokens });
|
|
59
|
+
await options.onBeforeTurn?.({ conversation: summarizedConversation, totalUsage, budgetTokens: budget.tokens });
|
|
56
60
|
if (abortController?.signal.aborted)
|
|
57
61
|
return { status: 'break', usage: totalUsage, turns };
|
|
58
62
|
debug?.('lowire:loop')(`Request`, JSON.stringify({ ...summarizedConversation, tools: `${summarizedConversation.tools.length} tools` }, null, 2));
|
|
59
63
|
const { result: assistantMessage, usage } = await (0, cache_1.cachedComplete)(this._provider, summarizedConversation, caches, {
|
|
60
64
|
...options,
|
|
61
|
-
maxTokens:
|
|
65
|
+
maxTokens: budget.tokens,
|
|
62
66
|
signal: abortController?.signal,
|
|
63
67
|
});
|
|
64
68
|
const intent = assistantMessage.content.filter(part => part.type === 'text').map(part => part.text).join('\n');
|
|
65
69
|
totalUsage.input += usage.input;
|
|
66
70
|
totalUsage.output += usage.output;
|
|
67
|
-
if (
|
|
68
|
-
|
|
71
|
+
if (budget.tokens !== undefined)
|
|
72
|
+
budget.tokens -= usage.input + usage.output;
|
|
69
73
|
debug?.('lowire:loop')('Usage', `input: ${usage.input}, output: ${usage.output}`);
|
|
70
74
|
debug?.('lowire:loop')('Assistant', intent, JSON.stringify(assistantMessage.content, null, 2));
|
|
71
|
-
await options.onAfterTurn?.({ assistantMessage, totalUsage, budgetTokens });
|
|
75
|
+
await options.onAfterTurn?.({ assistantMessage, totalUsage, budgetTokens: budget.tokens });
|
|
72
76
|
if (abortController?.signal.aborted)
|
|
73
77
|
return { status: 'break', usage: totalUsage, turns };
|
|
74
78
|
conversation.messages.push(assistantMessage);
|
|
@@ -78,6 +82,8 @@ class Loop {
|
|
|
78
82
|
continue;
|
|
79
83
|
}
|
|
80
84
|
for (const toolCall of toolCalls) {
|
|
85
|
+
if (budget.toolCalls !== undefined && --budget.toolCalls < 0)
|
|
86
|
+
throw new Error(`Failed to perform step, max tool calls (${options.maxToolCalls}) reached`);
|
|
81
87
|
const { name, arguments: args } = toolCall;
|
|
82
88
|
debug?.('lowire:loop')('Call tool', name, JSON.stringify(args, null, 2));
|
|
83
89
|
const status = await options.onBeforeToolCall?.({ assistantMessage, toolCall });
|
|
@@ -129,6 +135,11 @@ class Loop {
|
|
|
129
135
|
};
|
|
130
136
|
}
|
|
131
137
|
}
|
|
138
|
+
const hasErrors = toolCalls.some(toolCall => toolCall.result?.isError);
|
|
139
|
+
if (!hasErrors)
|
|
140
|
+
budget.toolCallRetries = options.maxToolCallRetries;
|
|
141
|
+
if (hasErrors && budget.toolCallRetries !== undefined && --budget.toolCallRetries < 0)
|
|
142
|
+
throw new Error(`Failed to perform action after ${options.maxToolCallRetries} tool call retries`);
|
|
132
143
|
}
|
|
133
144
|
throw new Error('Failed to perform step, max attempts reached');
|
|
134
145
|
}
|