@lowire/loop 0.0.23 → 0.0.24
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.d.ts +1 -0
- package/lib/cache.js +7 -2
- package/lib/loop.d.ts +1 -0
- package/lib/loop.js +2 -2
- package/lib/providers/anthropic.js +10 -14
- package/lib/providers/google.js +8 -19
- package/lib/providers/openai.js +3 -11
- package/lib/providers/openaiCompatible.js +3 -9
- package/lib/types.d.ts +3 -1
- package/lib/types.js +15 -0
- package/package.json +1 -1
package/lib/cache.d.ts
CHANGED
|
@@ -20,4 +20,5 @@ export type ReplayCaches = {
|
|
|
20
20
|
};
|
|
21
21
|
export declare function cachedComplete(provider: types.Provider, conversation: types.Conversation, caches: ReplayCaches | undefined, options: types.CompletionOptions & {
|
|
22
22
|
secrets?: Record<string, string>;
|
|
23
|
+
cacheMode?: 'strict' | 'lax';
|
|
23
24
|
}): ReturnType<types.Provider['complete']>;
|
package/lib/cache.js
CHANGED
|
@@ -28,8 +28,13 @@ async function cachedComplete(provider, conversation, caches, options) {
|
|
|
28
28
|
async function cachedCompleteNoSecrets(provider, conversation, caches, options) {
|
|
29
29
|
if (!caches)
|
|
30
30
|
return await provider.complete(conversation, options);
|
|
31
|
-
const
|
|
32
|
-
|
|
31
|
+
const keyObject = {
|
|
32
|
+
conversation: options.cacheMode === 'lax' ? { ...conversation, tools: [] } : conversation,
|
|
33
|
+
maxTokens: options.maxTokens,
|
|
34
|
+
reasoning: options.reasoning,
|
|
35
|
+
temperature: options.temperature,
|
|
36
|
+
};
|
|
37
|
+
const key = calculateSha1(JSON.stringify(keyObject));
|
|
33
38
|
if (!process.env.LOWIRE_NO_CACHE && caches.input[key]) {
|
|
34
39
|
caches.output[key] = caches.input[key];
|
|
35
40
|
return caches.input[key] ?? caches.output[key];
|
package/lib/loop.d.ts
CHANGED
package/lib/loop.js
CHANGED
|
@@ -68,10 +68,10 @@ class Loop {
|
|
|
68
68
|
maxTokens: budget.tokens !== undefined ? budget.tokens - tokenEstimate : undefined,
|
|
69
69
|
signal: abortController?.signal,
|
|
70
70
|
});
|
|
71
|
+
if (assistantMessage.stopReason.code === 'error')
|
|
72
|
+
return { status: 'error', error: assistantMessage.stopReason.message, usage: totalUsage, turns };
|
|
71
73
|
if (assistantMessage.stopReason.code === 'max_tokens')
|
|
72
74
|
return { status: 'error', error: `Max tokens exhausted`, usage: totalUsage, turns };
|
|
73
|
-
if (assistantMessage.stopReason.code === 'other')
|
|
74
|
-
return { status: 'error', error: assistantMessage.stopReason.message, usage: totalUsage, turns };
|
|
75
75
|
const intent = assistantMessage.content.filter(part => part.type === 'text').map(part => part.text).join('\n');
|
|
76
76
|
totalUsage.input += usage.input;
|
|
77
77
|
totalUsage.output += usage.output;
|
|
@@ -17,11 +17,12 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.Anthropic = void 0;
|
|
19
19
|
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
20
|
+
const types_1 = require("../types");
|
|
20
21
|
class Anthropic {
|
|
21
22
|
name = 'anthropic';
|
|
22
23
|
async complete(conversation, options) {
|
|
23
24
|
const maxTokens = Math.min(options.maxTokens ?? 32_768, 32_768);
|
|
24
|
-
const response = await create({
|
|
25
|
+
const { response, error } = await create({
|
|
25
26
|
model: options.model,
|
|
26
27
|
max_tokens: maxTokens,
|
|
27
28
|
temperature: options.temperature,
|
|
@@ -33,6 +34,8 @@ class Anthropic {
|
|
|
33
34
|
budget_tokens: options.maxTokens ? Math.round(maxTokens / 10) : 1024,
|
|
34
35
|
} : undefined,
|
|
35
36
|
}, options);
|
|
37
|
+
if (error || !response)
|
|
38
|
+
return { result: (0, types_1.assistantMessageFromError)(error ?? 'No response from Anthropic API'), usage: (0, types_1.emptyUsage)() };
|
|
36
39
|
const result = toAssistantMessage(response);
|
|
37
40
|
const usage = {
|
|
38
41
|
input: response.usage.input_tokens,
|
|
@@ -57,13 +60,14 @@ async function create(createParams, options) {
|
|
|
57
60
|
signal: options.signal,
|
|
58
61
|
timeout: options.apiTimeout
|
|
59
62
|
});
|
|
63
|
+
const responseText = await response.text();
|
|
64
|
+
const responseBody = JSON.parse(responseText);
|
|
65
|
+
options.debug?.('lowire:anthropic')('Response:', responseText);
|
|
60
66
|
if (!response.ok) {
|
|
61
67
|
options.debug?.('lowire:anthropic')('Response:', response.status);
|
|
62
|
-
|
|
68
|
+
return { error: `API error: ${response.status} ${response.statusText} ${responseText}` };
|
|
63
69
|
}
|
|
64
|
-
|
|
65
|
-
options.debug?.('lowire:anthropic')('Response:', JSON.stringify(responseBody, null, 2));
|
|
66
|
-
return responseBody;
|
|
70
|
+
return { response: responseBody };
|
|
67
71
|
}
|
|
68
72
|
function toContentPart(block) {
|
|
69
73
|
if (block.type === 'text') {
|
|
@@ -110,16 +114,8 @@ function toAnthropicResultParam(part) {
|
|
|
110
114
|
}
|
|
111
115
|
function toAssistantMessage(message) {
|
|
112
116
|
const stopReason = { code: 'ok' };
|
|
113
|
-
if (message.stop_reason === 'max_tokens')
|
|
117
|
+
if (message.stop_reason === 'max_tokens')
|
|
114
118
|
stopReason.code = 'max_tokens';
|
|
115
|
-
}
|
|
116
|
-
else if (message.stop_reason === 'tool_use') {
|
|
117
|
-
stopReason.code = 'ok';
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
stopReason.code = 'other';
|
|
121
|
-
stopReason.message = `Unexpected stop reason: ${message.stop_reason}`;
|
|
122
|
-
}
|
|
123
119
|
return {
|
|
124
120
|
role: 'assistant',
|
|
125
121
|
content: message.content.map(toContentPart).filter(Boolean),
|
package/lib/providers/google.js
CHANGED
|
@@ -17,11 +17,12 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.Google = void 0;
|
|
19
19
|
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
20
|
+
const types_1 = require("../types");
|
|
20
21
|
class Google {
|
|
21
22
|
name = 'google';
|
|
22
23
|
async complete(conversation, options) {
|
|
23
24
|
const contents = conversation.messages.map(toGeminiContent).flat();
|
|
24
|
-
const response = await create(options.model ?? 'gemini-2.5-pro', {
|
|
25
|
+
const { response, error } = await create(options.model ?? 'gemini-2.5-pro', {
|
|
25
26
|
systemInstruction: {
|
|
26
27
|
role: 'system',
|
|
27
28
|
parts: [
|
|
@@ -35,9 +36,9 @@ class Google {
|
|
|
35
36
|
maxOutputTokens: options.maxTokens
|
|
36
37
|
},
|
|
37
38
|
}, options);
|
|
38
|
-
const [candidate] = response
|
|
39
|
-
if (!candidate)
|
|
40
|
-
|
|
39
|
+
const [candidate] = response?.candidates ?? [];
|
|
40
|
+
if (error || !response || !candidate)
|
|
41
|
+
return { result: (0, types_1.assistantMessageFromError)(error ?? 'No response from Google API'), usage: (0, types_1.emptyUsage)() };
|
|
41
42
|
const usage = {
|
|
42
43
|
input: response.usageMetadata?.promptTokenCount ?? 0,
|
|
43
44
|
output: response.usageMetadata?.candidatesTokenCount ?? 0,
|
|
@@ -62,11 +63,11 @@ async function create(model, createParams, options) {
|
|
|
62
63
|
});
|
|
63
64
|
if (!response.ok) {
|
|
64
65
|
options.debug?.('lowire:google')('Response:', response.status);
|
|
65
|
-
|
|
66
|
+
return { error: `API error: ${response.status} ${response.statusText} ${await response.text()}` };
|
|
66
67
|
}
|
|
67
68
|
const responseBody = await response.json();
|
|
68
69
|
options.debug?.('lowire:google')('Response:', JSON.stringify(responseBody, null, 2));
|
|
69
|
-
return responseBody;
|
|
70
|
+
return { response: responseBody };
|
|
70
71
|
}
|
|
71
72
|
function toGeminiTool(tool) {
|
|
72
73
|
return {
|
|
@@ -89,20 +90,8 @@ function stripUnsupportedSchemaFields(schema) {
|
|
|
89
90
|
}
|
|
90
91
|
function toAssistantMessage(candidate) {
|
|
91
92
|
const stopReason = { code: 'ok' };
|
|
92
|
-
|
|
93
|
-
if (finishReason === 'MAX_TOKENS') {
|
|
93
|
+
if (candidate.finishReason === 'MAX_TOKENS')
|
|
94
94
|
stopReason.code = 'max_tokens';
|
|
95
|
-
}
|
|
96
|
-
else if (!finishReason || finishReason === 'STOP') {
|
|
97
|
-
stopReason.code = 'ok';
|
|
98
|
-
}
|
|
99
|
-
else if (finishReason.includes('FUNCTION') || finishReason.includes('TOOL')) {
|
|
100
|
-
stopReason.code = 'ok';
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
stopReason.code = 'other';
|
|
104
|
-
stopReason.message = `Unexpected finish reason: ${finishReason}`;
|
|
105
|
-
}
|
|
106
95
|
return {
|
|
107
96
|
role: 'assistant',
|
|
108
97
|
content: (candidate.content.parts || []).map(toContentPart).filter(Boolean),
|
package/lib/providers/openai.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.OpenAI = void 0;
|
|
19
19
|
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
20
|
+
const types_1 = require("../types");
|
|
20
21
|
class OpenAI {
|
|
21
22
|
name = 'openai';
|
|
22
23
|
async complete(conversation, options) {
|
|
@@ -39,20 +40,11 @@ async function complete(conversation, options) {
|
|
|
39
40
|
reasoning: toOpenAIReasoning(options.reasoning),
|
|
40
41
|
}, options);
|
|
41
42
|
if (!response || error)
|
|
42
|
-
return { result:
|
|
43
|
+
return { result: (0, types_1.assistantMessageFromError)(error ?? 'No response from OpenAI API'), usage: (0, types_1.emptyUsage)() };
|
|
43
44
|
// Parse response output items
|
|
44
45
|
const stopReason = { code: 'ok' };
|
|
45
|
-
if (response.incomplete_details?.reason === 'max_output_tokens')
|
|
46
|
+
if (response.incomplete_details?.reason === 'max_output_tokens')
|
|
46
47
|
stopReason.code = 'max_tokens';
|
|
47
|
-
}
|
|
48
|
-
else if (response.incomplete_details?.reason === 'content_filter') {
|
|
49
|
-
stopReason.code = 'other';
|
|
50
|
-
stopReason.message = 'Content filter triggered';
|
|
51
|
-
}
|
|
52
|
-
else if (response.incomplete_details?.reason) {
|
|
53
|
-
stopReason.code = 'other';
|
|
54
|
-
stopReason.message = `Unexpected incomplete reason: ${response.incomplete_details.reason}`;
|
|
55
|
-
}
|
|
56
48
|
const result = { role: 'assistant', content: [], stopReason };
|
|
57
49
|
const usage = {
|
|
58
50
|
input: response.usage?.input_tokens ?? 0,
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.OpenAICompatible = void 0;
|
|
19
19
|
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
20
|
+
const types_1 = require("../types");
|
|
20
21
|
class OpenAICompatible {
|
|
21
22
|
name = 'openai-compatible';
|
|
22
23
|
async complete(conversation, options) {
|
|
@@ -42,13 +43,8 @@ async function complete(conversation, options) {
|
|
|
42
43
|
reasoning_effort: toCompletionsReasoning(options.reasoning),
|
|
43
44
|
parallel_tool_calls: false,
|
|
44
45
|
}, options);
|
|
45
|
-
if (error)
|
|
46
|
-
|
|
47
|
-
return { result: { role: 'assistant', content: [], stopReason: { code: 'max_tokens' } }, usage: { input: 0, output: 0 } };
|
|
48
|
-
return { result: { role: 'assistant', content: [], stopReason: { code: 'other', message: response.error.message } }, usage: { input: 0, output: 0 } };
|
|
49
|
-
}
|
|
50
|
-
if (!response || !response.choices.length)
|
|
51
|
-
return { result: { role: 'assistant', content: [], stopReason: { code: 'other', message: 'Failed to get response from OpenAI completions' } }, usage: { input: 0, output: 0 } };
|
|
46
|
+
if (error || !response)
|
|
47
|
+
return { result: (0, types_1.assistantMessageFromError)(error?.message ?? 'No response from OpenAI compatible API'), usage: (0, types_1.emptyUsage)() };
|
|
52
48
|
const result = { role: 'assistant', content: [], stopReason: { code: 'ok' } };
|
|
53
49
|
const finishReason = response.choices[0]?.finish_reason;
|
|
54
50
|
for (const choice of response.choices) {
|
|
@@ -63,8 +59,6 @@ async function complete(conversation, options) {
|
|
|
63
59
|
}
|
|
64
60
|
if (finishReason === 'length')
|
|
65
61
|
result.stopReason = { code: 'max_tokens' };
|
|
66
|
-
else if (finishReason !== 'tool_calls' && finishReason !== 'function_call' && finishReason !== 'stop')
|
|
67
|
-
result.stopReason = { code: 'other', message: `Unexpected finish reason: ${finishReason}` };
|
|
68
62
|
const usage = {
|
|
69
63
|
input: response.usage?.prompt_tokens ?? 0,
|
|
70
64
|
output: response.usage?.completion_tokens ?? 0,
|
package/lib/types.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export type AssistantMessage = BaseMessage & {
|
|
|
39
39
|
role: 'assistant';
|
|
40
40
|
content: (TextContentPart | ToolCallContentPart | ThinkingContentPart)[];
|
|
41
41
|
stopReason: {
|
|
42
|
-
code: '
|
|
42
|
+
code: 'ok' | 'max_tokens' | 'error';
|
|
43
43
|
message?: string;
|
|
44
44
|
};
|
|
45
45
|
openaiId?: string;
|
|
@@ -120,3 +120,5 @@ export type ReplayCache = Record<string, {
|
|
|
120
120
|
result: AssistantMessage;
|
|
121
121
|
usage: Usage;
|
|
122
122
|
}>;
|
|
123
|
+
export declare function assistantMessageFromError(error: string): AssistantMessage;
|
|
124
|
+
export declare function emptyUsage(): Usage;
|
package/lib/types.js
CHANGED
|
@@ -15,3 +15,18 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.assistantMessageFromError = assistantMessageFromError;
|
|
19
|
+
exports.emptyUsage = emptyUsage;
|
|
20
|
+
function assistantMessageFromError(error) {
|
|
21
|
+
return {
|
|
22
|
+
role: 'assistant',
|
|
23
|
+
content: [],
|
|
24
|
+
stopReason: { code: 'error', message: error },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function emptyUsage() {
|
|
28
|
+
return {
|
|
29
|
+
input: 0,
|
|
30
|
+
output: 0,
|
|
31
|
+
};
|
|
32
|
+
}
|