@hilbras/sdk 3.5.0 → 3.6.1
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/README.md +17 -10
- package/dist/core/execution/request-pipeline.js +138 -73
- package/dist/features/agent/plan-and-execute.js +1 -1
- package/dist/features/agent/react.js +1 -1
- package/dist/features/agent/tool-loop.js +1 -1
- package/dist/frameworks/angular/angular-shim.d.ts +1 -1
- package/dist/frameworks/angular/angular-shim.js +1 -1
- package/dist/frameworks/angular/chat.service.js +1 -1
- package/dist/frameworks/angular/completion.service.js +1 -1
- package/dist/frameworks/angular/object.service.js +1 -1
- package/dist/frameworks/nextjs/server.js +1 -1
- package/dist/frameworks/react-client/index.d.ts +35 -0
- package/dist/frameworks/react-client/index.js +31 -0
- package/dist/frameworks/react-client/provider.d.ts +45 -0
- package/dist/frameworks/react-client/provider.js +40 -0
- package/dist/frameworks/react-client/use-chat.d.ts +85 -0
- package/dist/frameworks/react-client/use-chat.js +202 -0
- package/dist/frameworks/react-client/use-completion.d.ts +66 -0
- package/dist/frameworks/react-client/use-completion.js +117 -0
- package/dist/frameworks/react-client/use-cost.d.ts +62 -0
- package/dist/frameworks/react-client/use-cost.js +68 -0
- package/dist/frameworks/svelte/use-chat.js +1 -1
- package/dist/frameworks/vue/use-chat.js +1 -1
- package/dist/security/audit-logger.d.ts +7 -1
- package/dist/security/audit-logger.js +7 -5
- package/dist/security/pii-guard.js +21 -1
- package/dist/security/prompt-injection-guard.js +1 -1
- package/dist/security/rate-limiter.d.ts +2 -0
- package/dist/security/rate-limiter.js +8 -1
- package/dist/security/rbac.js +2 -2
- package/dist/security/request-signer.d.ts +2 -0
- package/dist/security/request-signer.js +3 -1
- package/dist/security/url-guard.d.ts +1 -1
- package/dist/security/url-guard.js +4 -3
- package/dist/utils/sse-writer.js +1 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://img.shields.io/badge/version-3.
|
|
2
|
+
<img src="https://img.shields.io/badge/version-3.6.1-blue" alt="version">
|
|
3
3
|
<img src="https://img.shields.io/badge/license-MIT-green" alt="license">
|
|
4
4
|
<img src="https://img.shields.io/badge/node-%3E%3D18-brightgreen" alt="node">
|
|
5
5
|
<img src="https://img.shields.io/badge/types-strict-blueviolet" alt="types">
|
|
6
|
-
<img src="https://img.shields.io/badge/tests-
|
|
6
|
+
<img src="https://img.shields.io/badge/tests-1939%20passing-brightgreen" alt="tests">
|
|
7
7
|
<img src="https://img.shields.io/badge/runtime%20deps-zero-brightgreen" alt="zero deps">
|
|
8
8
|
</p>
|
|
9
9
|
|
|
@@ -62,7 +62,7 @@ npm install @hilbras/sdk
|
|
|
62
62
|
### React (optional)
|
|
63
63
|
|
|
64
64
|
```bash
|
|
65
|
-
npm install @hilbras/
|
|
65
|
+
npm install @hilbras/sdk
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
## Quick start
|
|
@@ -372,10 +372,17 @@ console.log(`Winner: ${result.winner.name}`);
|
|
|
372
372
|
|
|
373
373
|
## React Hooks
|
|
374
374
|
|
|
375
|
-
First-class React integration
|
|
375
|
+
First-class React integration. Zero runtime dependencies beyond React itself.
|
|
376
|
+
|
|
377
|
+
Two React subpaths, because they are different designs:
|
|
378
|
+
|
|
379
|
+
| Subpath | Transport | Use when |
|
|
380
|
+
|---------|-----------|----------|
|
|
381
|
+
| `@hilbras/sdk/react` | `fetch(api)` against your route | **Default.** Credentials stay on your server. |
|
|
382
|
+
| `@hilbras/sdk/react-client` | `client.stream()` in the browser | Trusted/internal apps only. A key passed here is bundled into client JS. |
|
|
376
383
|
|
|
377
384
|
```bash
|
|
378
|
-
npm install @hilbras/
|
|
385
|
+
npm install @hilbras/sdk
|
|
379
386
|
```
|
|
380
387
|
|
|
381
388
|
### useChat — Streaming chat
|
|
@@ -387,7 +394,7 @@ an API key in browser props or a client component.
|
|
|
387
394
|
```tsx
|
|
388
395
|
// app/page.tsx (server component)
|
|
389
396
|
import { HilbrasClient } from "@hilbras/sdk";
|
|
390
|
-
import { HilbrasProvider } from "@hilbras/react";
|
|
397
|
+
import { HilbrasProvider } from "@hilbras/sdk/react-client";
|
|
391
398
|
import { Chat } from "./chat";
|
|
392
399
|
|
|
393
400
|
export default function Page() {
|
|
@@ -399,7 +406,7 @@ export default function Page() {
|
|
|
399
406
|
|
|
400
407
|
```tsx
|
|
401
408
|
// app/chat.tsx (client component)
|
|
402
|
-
import { useChat } from "@hilbras/react";
|
|
409
|
+
import { useChat } from "@hilbras/sdk/react-client";
|
|
403
410
|
|
|
404
411
|
export function Chat() {
|
|
405
412
|
const { messages, input, setInput, handleSubmit, isLoading, stop } = useChat({
|
|
@@ -424,7 +431,7 @@ export function Chat() {
|
|
|
424
431
|
### useCompletion — Text completion
|
|
425
432
|
|
|
426
433
|
```tsx
|
|
427
|
-
import { useCompletion } from "@hilbras/react";
|
|
434
|
+
import { useCompletion } from "@hilbras/sdk/react-client";
|
|
428
435
|
|
|
429
436
|
function AutoComplete() {
|
|
430
437
|
const { completion, prompt, setPrompt, complete, isLoading } = useCompletion({
|
|
@@ -446,7 +453,7 @@ function AutoComplete() {
|
|
|
446
453
|
### useCost — Real-time cost tracking
|
|
447
454
|
|
|
448
455
|
```tsx
|
|
449
|
-
import { useCost } from "@hilbras/react";
|
|
456
|
+
import { useCost } from "@hilbras/sdk/react-client";
|
|
450
457
|
|
|
451
458
|
function CostDisplay() {
|
|
452
459
|
const { snapshot, isBudgetLow } = useCost();
|
|
@@ -500,7 +507,7 @@ import { CostAlertMonitor } from "@hilbras/sdk"; // Cost alerts
|
|
|
500
507
|
import { runABTest } from "@hilbras/sdk"; // A/B testing
|
|
501
508
|
|
|
502
509
|
// React package
|
|
503
|
-
import { useChat, useCompletion, useCost, HilbrasProvider } from "@hilbras/react";
|
|
510
|
+
import { useChat, useCompletion, useCost, HilbrasProvider } from "@hilbras/sdk/react-client";
|
|
504
511
|
|
|
505
512
|
// Framework subpaths from the root package
|
|
506
513
|
import { useChat } from "@hilbras/sdk/vue";
|
|
@@ -9,6 +9,33 @@ import { CircuitBreakerOpenError, ConfigurationError, ValidationError } from "..
|
|
|
9
9
|
import { calculateBackoff } from "../../reliability/backoff.js";
|
|
10
10
|
import { buildJsonSystemInstruction, buildRepairPrompt, extractJson } from "../../output/structured.js";
|
|
11
11
|
import { shouldRetry, shouldRetryNetworkError } from "../../reliability/retry.js";
|
|
12
|
+
const NETWORK_ERROR_CODES = new Set([
|
|
13
|
+
"ECONNREFUSED",
|
|
14
|
+
"ECONNRESET",
|
|
15
|
+
"ENOTFOUND",
|
|
16
|
+
"ENETUNREACH",
|
|
17
|
+
"ETIMEDOUT",
|
|
18
|
+
"EAI_AGAIN",
|
|
19
|
+
"EAI_FAIL",
|
|
20
|
+
"EAI_NONAME",
|
|
21
|
+
"EAI_SERVICE",
|
|
22
|
+
"ESOCKETTIMEDOUT",
|
|
23
|
+
]);
|
|
24
|
+
/**
|
|
25
|
+
* Best-effort detection of transport-level failures (DNS, connection,
|
|
26
|
+
* timeout, socket errors) so they are retried like network errors even
|
|
27
|
+
* when they surface as generic Errors rather than TypeErrors.
|
|
28
|
+
*/
|
|
29
|
+
function isNetworkFailure(error) {
|
|
30
|
+
if (error instanceof Error) {
|
|
31
|
+
const code = error.code;
|
|
32
|
+
if (typeof code === "string" && NETWORK_ERROR_CODES.has(code))
|
|
33
|
+
return true;
|
|
34
|
+
if (/network|unreachable|connection|reset|refused|timed out|dns|resolve|socket/i.test(error.message))
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
12
39
|
export class RequestPipeline {
|
|
13
40
|
ports;
|
|
14
41
|
constructor(ports) {
|
|
@@ -33,6 +60,13 @@ export class RequestPipeline {
|
|
|
33
60
|
}
|
|
34
61
|
let attempt = 0;
|
|
35
62
|
let primaryError;
|
|
63
|
+
let budgetReleased = false;
|
|
64
|
+
const releaseBudget = () => {
|
|
65
|
+
if (!budgetReleased) {
|
|
66
|
+
this.ports.budget.release(input.requestId);
|
|
67
|
+
budgetReleased = true;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
36
70
|
for (;;) {
|
|
37
71
|
const result = await this.ports.executor.executeComplete(prepared, input.params, { dispose: false, recordCircuitFailure: false });
|
|
38
72
|
if (result.ok) {
|
|
@@ -57,7 +91,8 @@ export class RequestPipeline {
|
|
|
57
91
|
}
|
|
58
92
|
const status = primaryError.status;
|
|
59
93
|
const isNetworkError = primaryError instanceof TypeError
|
|
60
|
-
|| (primaryError instanceof Error && primaryError.name === "AbortError")
|
|
94
|
+
|| (primaryError instanceof Error && primaryError.name === "AbortError")
|
|
95
|
+
|| isNetworkFailure(primaryError);
|
|
61
96
|
const canRetry = (isNetworkError && shouldRetryNetworkError(attempt, prepared.retryConfig))
|
|
62
97
|
|| (typeof status === "number" && shouldRetry(status, attempt, prepared.retryConfig));
|
|
63
98
|
if (canRetry) {
|
|
@@ -82,7 +117,7 @@ export class RequestPipeline {
|
|
|
82
117
|
continue;
|
|
83
118
|
}
|
|
84
119
|
if (prepared.context.policy.allowFallback) {
|
|
85
|
-
|
|
120
|
+
releaseBudget();
|
|
86
121
|
const attemptedModels = [input.model];
|
|
87
122
|
const fallbacks = input.fallbackCandidates(attemptedModels);
|
|
88
123
|
for (const fallback of fallbacks) {
|
|
@@ -128,8 +163,7 @@ export class RequestPipeline {
|
|
|
128
163
|
}
|
|
129
164
|
}
|
|
130
165
|
catch (error) {
|
|
131
|
-
|
|
132
|
-
primaryError = error;
|
|
166
|
+
primaryError = error;
|
|
133
167
|
}
|
|
134
168
|
finally {
|
|
135
169
|
fallbackPrepared?.dispose();
|
|
@@ -142,7 +176,7 @@ export class RequestPipeline {
|
|
|
142
176
|
break;
|
|
143
177
|
}
|
|
144
178
|
this.recordTerminalFailure(prepared, primaryError);
|
|
145
|
-
|
|
179
|
+
releaseBudget();
|
|
146
180
|
const error = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
147
181
|
this.ports.emit({
|
|
148
182
|
type: "request.failed",
|
|
@@ -154,14 +188,19 @@ export class RequestPipeline {
|
|
|
154
188
|
attempts: attempt + 1,
|
|
155
189
|
error: error.message,
|
|
156
190
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
191
|
+
try {
|
|
192
|
+
await this.ports.plugins.fireError({
|
|
193
|
+
requestId: input.requestId,
|
|
194
|
+
provider: input.provider,
|
|
195
|
+
model: input.model,
|
|
196
|
+
error,
|
|
197
|
+
durationMs: this.ports.now() - input.startTime,
|
|
198
|
+
attempts: attempt + 1,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// Swallow plugin hook failures so the original request error is still surfaced.
|
|
203
|
+
}
|
|
165
204
|
throw primaryError;
|
|
166
205
|
}
|
|
167
206
|
finally {
|
|
@@ -176,6 +215,14 @@ export class RequestPipeline {
|
|
|
176
215
|
let firstChunkEmitted = false;
|
|
177
216
|
let attempt = 0;
|
|
178
217
|
let primaryError;
|
|
218
|
+
let budgetReleased = false;
|
|
219
|
+
const releaseMainBudget = () => {
|
|
220
|
+
if (!budgetReleased) {
|
|
221
|
+
this.ports.budget.release(input.requestId);
|
|
222
|
+
budgetReleased = true;
|
|
223
|
+
}
|
|
224
|
+
reservationActive = false;
|
|
225
|
+
};
|
|
179
226
|
try {
|
|
180
227
|
await this.ports.plugins.fireRequest({
|
|
181
228
|
requestId: input.requestId,
|
|
@@ -255,10 +302,7 @@ export class RequestPipeline {
|
|
|
255
302
|
const callerAborted = input.callerSignal?.aborted === true;
|
|
256
303
|
const internalTimeout = prepared.signal?.aborted === true;
|
|
257
304
|
if (callerAborted || firstChunkEmitted || internalTimeout) {
|
|
258
|
-
|
|
259
|
-
this.ports.budget.release(input.requestId);
|
|
260
|
-
reservationActive = false;
|
|
261
|
-
}
|
|
305
|
+
releaseMainBudget();
|
|
262
306
|
const terminalError = error instanceof Error ? error : new Error(String(error));
|
|
263
307
|
this.recordTerminalFailure(prepared, terminalError);
|
|
264
308
|
this.ports.emit({
|
|
@@ -271,25 +315,30 @@ export class RequestPipeline {
|
|
|
271
315
|
attempts: attempt + 1,
|
|
272
316
|
error: terminalError.message,
|
|
273
317
|
});
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
318
|
+
try {
|
|
319
|
+
await this.ports.plugins.fireError({
|
|
320
|
+
requestId: input.requestId,
|
|
321
|
+
provider: input.provider,
|
|
322
|
+
model: input.model,
|
|
323
|
+
error: terminalError,
|
|
324
|
+
durationMs: this.ports.now() - input.startTime,
|
|
325
|
+
attempts: attempt + 1,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
// Swallow plugin hook failures so the original stream error is still surfaced.
|
|
330
|
+
}
|
|
282
331
|
throw error;
|
|
283
332
|
}
|
|
284
333
|
const status = error.status;
|
|
285
334
|
const isNetworkError = error instanceof TypeError
|
|
286
|
-
|| (error instanceof Error && error.name === "AbortError")
|
|
335
|
+
|| (error instanceof Error && error.name === "AbortError")
|
|
336
|
+
|| isNetworkFailure(error);
|
|
287
337
|
const canRetry = (isNetworkError && shouldRetryNetworkError(attempt, prepared.retryConfig))
|
|
288
338
|
|| (typeof status === "number" && shouldRetry(status, attempt, prepared.retryConfig));
|
|
289
339
|
if (!canRetry) {
|
|
290
340
|
if (prepared.context.policy.allowFallback) {
|
|
291
|
-
|
|
292
|
-
reservationActive = false;
|
|
341
|
+
releaseMainBudget();
|
|
293
342
|
const attemptedModels = [input.model];
|
|
294
343
|
const fallbacks = input.fallbackCandidates(attemptedModels);
|
|
295
344
|
for (const fallback of fallbacks) {
|
|
@@ -313,7 +362,6 @@ export class RequestPipeline {
|
|
|
313
362
|
if (!fallbackReservation)
|
|
314
363
|
continue;
|
|
315
364
|
let fallbackPrepared;
|
|
316
|
-
let fallbackVisible = false;
|
|
317
365
|
let fallbackInputTokens;
|
|
318
366
|
let fallbackOutputTokens;
|
|
319
367
|
let fallbackReservationActive = true;
|
|
@@ -327,7 +375,6 @@ export class RequestPipeline {
|
|
|
327
375
|
providerTimeoutMs: 0,
|
|
328
376
|
}, "stream");
|
|
329
377
|
for await (const chunk of this.ports.executor.executeStream(fallbackPrepared, { ...input.params, model: fallback.model }, { recordCircuitFailure: false })) {
|
|
330
|
-
fallbackVisible = true;
|
|
331
378
|
if (chunk.type === "usage") {
|
|
332
379
|
fallbackInputTokens = chunk.inputTokens;
|
|
333
380
|
fallbackOutputTokens = chunk.outputTokens;
|
|
@@ -375,10 +422,7 @@ export class RequestPipeline {
|
|
|
375
422
|
return;
|
|
376
423
|
}
|
|
377
424
|
catch (fallbackError) {
|
|
378
|
-
|
|
379
|
-
primaryError = fallbackError;
|
|
380
|
-
break;
|
|
381
|
-
}
|
|
425
|
+
primaryError = fallbackError;
|
|
382
426
|
}
|
|
383
427
|
finally {
|
|
384
428
|
fallbackPrepared?.dispose();
|
|
@@ -391,10 +435,7 @@ export class RequestPipeline {
|
|
|
391
435
|
}
|
|
392
436
|
}
|
|
393
437
|
if (!canRetry) {
|
|
394
|
-
|
|
395
|
-
this.ports.budget.release(input.requestId);
|
|
396
|
-
reservationActive = false;
|
|
397
|
-
}
|
|
438
|
+
releaseMainBudget();
|
|
398
439
|
const terminalError = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
399
440
|
this.recordTerminalFailure(prepared, terminalError);
|
|
400
441
|
this.ports.emit({
|
|
@@ -407,14 +448,19 @@ export class RequestPipeline {
|
|
|
407
448
|
attempts: attempt + 1,
|
|
408
449
|
error: terminalError.message,
|
|
409
450
|
});
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
451
|
+
try {
|
|
452
|
+
await this.ports.plugins.fireError({
|
|
453
|
+
requestId: input.requestId,
|
|
454
|
+
provider: input.provider,
|
|
455
|
+
model: input.model,
|
|
456
|
+
error: terminalError,
|
|
457
|
+
durationMs: this.ports.now() - input.startTime,
|
|
458
|
+
attempts: attempt + 1,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
catch {
|
|
462
|
+
// Swallow plugin hook failures so the original stream error is still surfaced.
|
|
463
|
+
}
|
|
418
464
|
throw primaryError;
|
|
419
465
|
}
|
|
420
466
|
const delay = calculateBackoff(attempt, prepared.context.policy.backoff);
|
|
@@ -431,10 +477,7 @@ export class RequestPipeline {
|
|
|
431
477
|
await this.ports.sleep(delay, input.callerSignal);
|
|
432
478
|
}
|
|
433
479
|
catch (sleepError) {
|
|
434
|
-
|
|
435
|
-
this.ports.budget.release(input.requestId);
|
|
436
|
-
reservationActive = false;
|
|
437
|
-
}
|
|
480
|
+
releaseMainBudget();
|
|
438
481
|
const terminalError = sleepError instanceof Error ? sleepError : new Error(String(sleepError));
|
|
439
482
|
this.recordTerminalFailure(prepared, terminalError);
|
|
440
483
|
this.ports.emit({
|
|
@@ -447,14 +490,19 @@ export class RequestPipeline {
|
|
|
447
490
|
attempts: attempt + 1,
|
|
448
491
|
error: terminalError.message,
|
|
449
492
|
});
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
493
|
+
try {
|
|
494
|
+
await this.ports.plugins.fireError({
|
|
495
|
+
requestId: input.requestId,
|
|
496
|
+
provider: input.provider,
|
|
497
|
+
model: input.model,
|
|
498
|
+
error: terminalError,
|
|
499
|
+
durationMs: this.ports.now() - input.startTime,
|
|
500
|
+
attempts: attempt + 1,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
// Swallow plugin hook failures so the original stream error is still surfaced.
|
|
505
|
+
}
|
|
458
506
|
throw sleepError;
|
|
459
507
|
}
|
|
460
508
|
attempt++;
|
|
@@ -462,8 +510,7 @@ export class RequestPipeline {
|
|
|
462
510
|
}
|
|
463
511
|
}
|
|
464
512
|
finally {
|
|
465
|
-
|
|
466
|
-
this.ports.budget.release(input.requestId);
|
|
513
|
+
releaseMainBudget();
|
|
467
514
|
prepared.dispose();
|
|
468
515
|
}
|
|
469
516
|
}
|
|
@@ -495,6 +542,13 @@ export class RequestPipeline {
|
|
|
495
542
|
};
|
|
496
543
|
let attempt = 0;
|
|
497
544
|
let primaryError;
|
|
545
|
+
let budgetReleased = false;
|
|
546
|
+
const releaseBudget = () => {
|
|
547
|
+
if (!budgetReleased) {
|
|
548
|
+
this.ports.budget.release(input.requestId);
|
|
549
|
+
budgetReleased = true;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
498
552
|
for (;;) {
|
|
499
553
|
const result = await this.ports.executor.executeComplete(prepared, { ...input.params, messages: structuredMessages, extra: structuredExtra }, { dispose: false, recordCircuitFailure: false });
|
|
500
554
|
if (result.ok) {
|
|
@@ -545,7 +599,7 @@ export class RequestPipeline {
|
|
|
545
599
|
continue;
|
|
546
600
|
}
|
|
547
601
|
const validationError = new ValidationError(maxRepairAttempts + 1, validation.error, result.value, { requestId: input.requestId, model: input.model });
|
|
548
|
-
|
|
602
|
+
releaseBudget();
|
|
549
603
|
this.ports.emit({
|
|
550
604
|
type: "request.failed",
|
|
551
605
|
requestId: input.requestId,
|
|
@@ -630,7 +684,14 @@ export class RequestPipeline {
|
|
|
630
684
|
primaryError = fallbackResult.error;
|
|
631
685
|
}
|
|
632
686
|
if (fallbackResult.ok) {
|
|
633
|
-
|
|
687
|
+
let parsed;
|
|
688
|
+
try {
|
|
689
|
+
parsed = JSON.parse(extractJson(fallbackResult.value));
|
|
690
|
+
}
|
|
691
|
+
catch (error) {
|
|
692
|
+
primaryError = error;
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
634
695
|
const validation = input.output.schema.safeParse(parsed);
|
|
635
696
|
if (validation.success) {
|
|
636
697
|
this.ports.budget.settle(fallbackId, fallbackCost, {
|
|
@@ -651,8 +712,7 @@ export class RequestPipeline {
|
|
|
651
712
|
}
|
|
652
713
|
}
|
|
653
714
|
catch (error) {
|
|
654
|
-
|
|
655
|
-
primaryError = error;
|
|
715
|
+
primaryError = error;
|
|
656
716
|
}
|
|
657
717
|
finally {
|
|
658
718
|
fallbackPrepared?.dispose();
|
|
@@ -665,7 +725,7 @@ export class RequestPipeline {
|
|
|
665
725
|
break;
|
|
666
726
|
}
|
|
667
727
|
this.recordTerminalFailure(prepared, primaryError);
|
|
668
|
-
|
|
728
|
+
releaseBudget();
|
|
669
729
|
const error = primaryError instanceof Error ? primaryError : new Error(String(primaryError));
|
|
670
730
|
this.ports.emit({
|
|
671
731
|
type: "request.failed",
|
|
@@ -677,14 +737,19 @@ export class RequestPipeline {
|
|
|
677
737
|
attempts: attempt + 1,
|
|
678
738
|
error: error.message,
|
|
679
739
|
});
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
740
|
+
try {
|
|
741
|
+
await this.ports.plugins.fireError({
|
|
742
|
+
requestId: input.requestId,
|
|
743
|
+
provider: input.provider,
|
|
744
|
+
model: input.model,
|
|
745
|
+
error,
|
|
746
|
+
durationMs: this.ports.now() - input.startTime,
|
|
747
|
+
attempts: attempt + 1,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
catch {
|
|
751
|
+
// Swallow plugin hook failures so the original request error is still surfaced.
|
|
752
|
+
}
|
|
688
753
|
throw primaryError;
|
|
689
754
|
}
|
|
690
755
|
finally {
|
|
@@ -33,7 +33,7 @@ export class PlanAndExecuteAgent {
|
|
|
33
33
|
* Run the plan-and-execute agent.
|
|
34
34
|
*/
|
|
35
35
|
async run(initialPrompt) {
|
|
36
|
-
const { tools, maxSteps = 10, budget = null, systemPrompt = PLAN_SYSTEM_PROMPT,
|
|
36
|
+
const { tools, maxSteps = 10, budget = null, systemPrompt = PLAN_SYSTEM_PROMPT, signal, llm, costEstimator, onApproval, onEvent, replanOnFailure = true, } = this._config;
|
|
37
37
|
const effectiveLLM = llm ?? (() => { throw new Error("No LLM function provided. Pass llm() in PlanAndExecuteAgentConfig."); });
|
|
38
38
|
const effectiveCost = costEstimator ?? (() => 0);
|
|
39
39
|
const steps = [];
|
|
@@ -31,7 +31,7 @@ export class ReActAgent {
|
|
|
31
31
|
* Run the ReAct agent loop.
|
|
32
32
|
*/
|
|
33
33
|
async run(initialPrompt) {
|
|
34
|
-
const { tools, maxSteps = 10, budget = null, systemPrompt = REACT_SYSTEM_PROMPT,
|
|
34
|
+
const { tools, maxSteps = 10, budget = null, systemPrompt = REACT_SYSTEM_PROMPT, signal, llm, costEstimator, onApproval, onEvent, } = this._config;
|
|
35
35
|
const effectiveLLM = llm ?? (() => { throw new Error("No LLM function provided. Pass llm() in ReActAgentConfig."); });
|
|
36
36
|
const effectiveCost = costEstimator ?? (() => 0);
|
|
37
37
|
const steps = [];
|
|
@@ -23,7 +23,7 @@ function defaultCostEstimator(usage, model) {
|
|
|
23
23
|
const p = pricing[model] ?? { input: 3, output: 15 };
|
|
24
24
|
return (usage.promptTokens * p.input + usage.completionTokens * p.output) / 1_000_000;
|
|
25
25
|
}
|
|
26
|
-
function defaultLLM(
|
|
26
|
+
function defaultLLM() {
|
|
27
27
|
throw new Error("No LLM function provided. Pass llm() in ToolLoopAgentConfig.");
|
|
28
28
|
}
|
|
29
29
|
export class ToolLoopAgent {
|
|
@@ -15,4 +15,4 @@ export interface Signal<T> {
|
|
|
15
15
|
}
|
|
16
16
|
export declare function signal<T>(initialValue: T): WritableSignal<T>;
|
|
17
17
|
export declare function computed<T>(fn: () => T): Signal<T>;
|
|
18
|
-
export declare function Injectable(
|
|
18
|
+
export declare function Injectable(): ClassDecorator;
|
|
@@ -66,7 +66,7 @@ export class ServerHilbrasClient {
|
|
|
66
66
|
* response. Removed in 4.0.0 — see the module deprecation notice.
|
|
67
67
|
*/
|
|
68
68
|
async streamResponse(params) {
|
|
69
|
-
const { messages,
|
|
69
|
+
const { messages, systemPrompt } = params;
|
|
70
70
|
const allMessages = systemPrompt
|
|
71
71
|
? [{ role: "system", content: systemPrompt }, ...messages]
|
|
72
72
|
: messages;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — React client-mode hooks
|
|
3
|
+
*
|
|
4
|
+
* Subpath: `@hilbras/sdk/react-client`
|
|
5
|
+
*
|
|
6
|
+
* These hooks hold a `HilbrasClient` and call `client.stream()` directly, so
|
|
7
|
+
* inference runs **in the browser**. They live under their own subpath rather
|
|
8
|
+
* than joining `@hilbras/sdk/react` for two reasons:
|
|
9
|
+
*
|
|
10
|
+
* 1. Name collision. `useChat` and `useCompletion` are exported by both, with
|
|
11
|
+
* different signatures and different transports. Merging them into one
|
|
12
|
+
* entry would make the import ambiguous.
|
|
13
|
+
* 2. Different design. `@hilbras/sdk/react`'s `useChat` takes an `api` route and
|
|
14
|
+
* calls `fetch(api)` — credentials stay on your server. These take a
|
|
15
|
+
* `provider` and `model` and reach the provider from the browser.
|
|
16
|
+
*
|
|
17
|
+
* ## Prefer the route-based hooks
|
|
18
|
+
*
|
|
19
|
+
* A client built from `config` with an API key will bundle that key into your
|
|
20
|
+
* client-side JavaScript, where anyone can read it. For a public app, use
|
|
21
|
+
* `@hilbras/sdk/react` and point `api` at a route handler from
|
|
22
|
+
* `@hilbras/sdk/nextjs/api` — the key never leaves the server.
|
|
23
|
+
*
|
|
24
|
+
* These hooks remain the supported path for a **trusted, authenticated
|
|
25
|
+
* environment** — an internal tool behind SSO, an Electron app, or a server
|
|
26
|
+
* component tree where the "client" is not really a browser.
|
|
27
|
+
*/
|
|
28
|
+
export { HilbrasProvider, useHilbrasClient } from "./provider.js";
|
|
29
|
+
export type { HilbrasProviderProps } from "./provider.js";
|
|
30
|
+
export { useChat } from "./use-chat.js";
|
|
31
|
+
export type { ChatMessage, UseChatOptions, UseChatReturn } from "./use-chat.js";
|
|
32
|
+
export { useCompletion } from "./use-completion.js";
|
|
33
|
+
export type { UseCompletionOptions, UseCompletionReturn } from "./use-completion.js";
|
|
34
|
+
export { useCost } from "./use-cost.js";
|
|
35
|
+
export type { CostSnapshot, UseCostOptions, UseCostReturn } from "./use-cost.js";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hilbras/sdk — React client-mode hooks
|
|
3
|
+
*
|
|
4
|
+
* Subpath: `@hilbras/sdk/react-client`
|
|
5
|
+
*
|
|
6
|
+
* These hooks hold a `HilbrasClient` and call `client.stream()` directly, so
|
|
7
|
+
* inference runs **in the browser**. They live under their own subpath rather
|
|
8
|
+
* than joining `@hilbras/sdk/react` for two reasons:
|
|
9
|
+
*
|
|
10
|
+
* 1. Name collision. `useChat` and `useCompletion` are exported by both, with
|
|
11
|
+
* different signatures and different transports. Merging them into one
|
|
12
|
+
* entry would make the import ambiguous.
|
|
13
|
+
* 2. Different design. `@hilbras/sdk/react`'s `useChat` takes an `api` route and
|
|
14
|
+
* calls `fetch(api)` — credentials stay on your server. These take a
|
|
15
|
+
* `provider` and `model` and reach the provider from the browser.
|
|
16
|
+
*
|
|
17
|
+
* ## Prefer the route-based hooks
|
|
18
|
+
*
|
|
19
|
+
* A client built from `config` with an API key will bundle that key into your
|
|
20
|
+
* client-side JavaScript, where anyone can read it. For a public app, use
|
|
21
|
+
* `@hilbras/sdk/react` and point `api` at a route handler from
|
|
22
|
+
* `@hilbras/sdk/nextjs/api` — the key never leaves the server.
|
|
23
|
+
*
|
|
24
|
+
* These hooks remain the supported path for a **trusted, authenticated
|
|
25
|
+
* environment** — an internal tool behind SSO, an Electron app, or a server
|
|
26
|
+
* component tree where the "client" is not really a browser.
|
|
27
|
+
*/
|
|
28
|
+
export { HilbrasProvider, useHilbrasClient } from "./provider.js";
|
|
29
|
+
export { useChat } from "./use-chat.js";
|
|
30
|
+
export { useCompletion } from "./use-completion.js";
|
|
31
|
+
export { useCost } from "./use-cost.js";
|