@siduri-x/brain 1.0.1 → 1.0.3
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/dist/index.d.ts +2 -0
- package/dist/index.js +51 -32
- package/dist/index.test.js +20 -0
- package/organ-manifest.json +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -3,10 +3,12 @@ export interface OpenAICompatibleBrainConfig {
|
|
|
3
3
|
apiKey: string;
|
|
4
4
|
model: string;
|
|
5
5
|
baseUrl: string;
|
|
6
|
+
timeoutMs?: number;
|
|
6
7
|
}
|
|
7
8
|
export interface OpenRouterBrainConfig {
|
|
8
9
|
apiKey: string;
|
|
9
10
|
model: string;
|
|
11
|
+
timeoutMs?: number;
|
|
10
12
|
}
|
|
11
13
|
export declare class OpenAICompatibleBrain implements BrainOrgan {
|
|
12
14
|
private config;
|
package/dist/index.js
CHANGED
|
@@ -104,44 +104,63 @@ class OpenAICompatibleBrain {
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
];
|
|
107
|
+
const overallTimeoutMs = this.config.timeoutMs ?? 30000;
|
|
108
|
+
const overallController = new AbortController();
|
|
109
|
+
const overallTimer = setTimeout(() => {
|
|
110
|
+
overallController.abort(new Error(`Brain provider exceeded overall wall-clock deadline of ${overallTimeoutMs}ms`));
|
|
111
|
+
}, overallTimeoutMs);
|
|
107
112
|
let retries = 3;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
"Authorization": `Bearer ${this.config.apiKey}`,
|
|
114
|
-
"Content-Type": "application/json",
|
|
115
|
-
},
|
|
116
|
-
body: JSON.stringify({
|
|
117
|
-
model: this.config.model,
|
|
118
|
-
messages,
|
|
119
|
-
tools,
|
|
120
|
-
tool_choice: { type: "function", function: { name: "submitResponsePlan" } }
|
|
121
|
-
})
|
|
122
|
-
});
|
|
123
|
-
if (!response.ok) {
|
|
124
|
-
throw new Error(`OpenRouter API error: ${response.statusText}`);
|
|
113
|
+
let lastError;
|
|
114
|
+
try {
|
|
115
|
+
while (retries > 0) {
|
|
116
|
+
if (overallController.signal.aborted) {
|
|
117
|
+
throw new Error(`Brain request aborted: overall deadline of ${overallTimeoutMs}ms exceeded`);
|
|
125
118
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
try {
|
|
120
|
+
const response = await fetch(`${this.config.baseUrl.replace(/\/+$/, '')}/chat/completions`, {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: {
|
|
123
|
+
"Authorization": `Bearer ${this.config.apiKey}`,
|
|
124
|
+
"Content-Type": "application/json",
|
|
125
|
+
},
|
|
126
|
+
body: JSON.stringify({
|
|
127
|
+
model: this.config.model,
|
|
128
|
+
messages,
|
|
129
|
+
tools,
|
|
130
|
+
tool_choice: { type: "function", function: { name: "submitResponsePlan" } }
|
|
131
|
+
}),
|
|
132
|
+
signal: overallController.signal,
|
|
133
|
+
});
|
|
134
|
+
if (!response.ok) {
|
|
135
|
+
throw new Error(`OpenRouter API error: ${response.statusText}`);
|
|
136
|
+
}
|
|
137
|
+
const data = await response.json();
|
|
138
|
+
const toolCall = data.choices?.[0]?.message?.tool_calls?.[0];
|
|
139
|
+
if (toolCall && toolCall.function.name === "submitResponsePlan") {
|
|
140
|
+
const rawArgs = JSON.parse(toolCall.function.arguments);
|
|
141
|
+
const parsed = ResponsePlanSchema.parse(rawArgs);
|
|
142
|
+
return parsed;
|
|
143
|
+
}
|
|
144
|
+
throw new Error("No valid tool call returned from OpenRouter");
|
|
132
145
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
146
|
+
catch (e) {
|
|
147
|
+
lastError = e;
|
|
148
|
+
if (overallController.signal.aborted) {
|
|
149
|
+
throw new Error(`Brain request timed out after overall deadline of ${overallTimeoutMs}ms: ${e.message}`);
|
|
150
|
+
}
|
|
151
|
+
retries--;
|
|
152
|
+
if (retries === 0) {
|
|
153
|
+
throw new Error("Failed to generate plan after retries: " + e.message);
|
|
154
|
+
}
|
|
155
|
+
// backoff respecting remaining deadline
|
|
156
|
+
await new Promise(r => setTimeout(r, 10));
|
|
139
157
|
}
|
|
140
|
-
// backoff
|
|
141
|
-
await new Promise(r => setTimeout(r, 10)); // keep test fast
|
|
142
158
|
}
|
|
159
|
+
throw new Error(`Failed to generate plan after retries: ${lastError?.message || 'unknown error'}`);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
clearTimeout(overallTimer);
|
|
143
163
|
}
|
|
144
|
-
throw new Error("Failed to generate plan after retries");
|
|
145
164
|
}
|
|
146
165
|
}
|
|
147
166
|
exports.OpenAICompatibleBrain = OpenAICompatibleBrain;
|
package/dist/index.test.js
CHANGED
|
@@ -88,6 +88,26 @@ describe('OpenRouterBrain', () => {
|
|
|
88
88
|
await expect(brain.generatePlan(mockContext)).rejects.toThrow("Failed to generate plan after retries");
|
|
89
89
|
expect(global.fetch).toHaveBeenCalledTimes(3);
|
|
90
90
|
});
|
|
91
|
+
test('overall wall-clock deadline aborts slow / hanging requests', async () => {
|
|
92
|
+
const fastTimeoutBrain = new index_1.OpenRouterBrain({
|
|
93
|
+
apiKey: 'test-key',
|
|
94
|
+
model: 'test-model',
|
|
95
|
+
timeoutMs: 50,
|
|
96
|
+
});
|
|
97
|
+
// Mock fetch that hangs until aborted
|
|
98
|
+
global.fetch.mockImplementation((_url, init) => {
|
|
99
|
+
return new Promise((_resolve, reject) => {
|
|
100
|
+
if (init?.signal) {
|
|
101
|
+
init.signal.addEventListener('abort', () => {
|
|
102
|
+
const err = new Error('The operation was aborted');
|
|
103
|
+
err.name = 'AbortError';
|
|
104
|
+
reject(err);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
await expect(fastTimeoutBrain.generatePlan(mockContext)).rejects.toThrow(/deadline/i);
|
|
110
|
+
});
|
|
91
111
|
});
|
|
92
112
|
describe('OpenAICompatibleBrain', () => {
|
|
93
113
|
test('uses a configurable OpenAI-compatible endpoint', async () => {
|
package/organ-manifest.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siduri-x/brain",
|
|
3
3
|
"organType": "brain",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"displayName": "Brain (Cognition & Planning)",
|
|
6
6
|
"description": "Provider-neutral LLM reasoning, response planning, and proposal generation",
|
|
7
7
|
"entrypoint": "./dist/index.js",
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siduri-x/brain",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"zod": "^4.4.3",
|
|
8
|
-
"@siduri-x/core": "1.0.
|
|
8
|
+
"@siduri-x/core": "1.0.3"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@types/jest": "^29.5.14",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"license": "Apache-2.0",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/
|
|
21
|
+
"url": "https://github.com/vxnus-studio/siduri-x",
|
|
22
22
|
"directory": "packages/organs/brain"
|
|
23
23
|
},
|
|
24
24
|
"publishConfig": {
|