@providerprotocol/ai 0.0.39 → 0.0.40
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 +269 -34
- package/dist/anthropic/index.d.ts +3 -3
- package/dist/anthropic/index.js +7 -5
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +3 -3
- package/dist/cerebras/index.js +7 -5
- package/dist/cerebras/index.js.map +1 -1
- package/dist/{chunk-WU4U6IHF.js → chunk-6QCV4WXF.js} +4 -13
- package/dist/chunk-6QCV4WXF.js.map +1 -0
- package/dist/{chunk-5XPRVUOK.js → chunk-AC3VHSZJ.js} +2 -2
- package/dist/{chunk-5XPRVUOK.js.map → chunk-AC3VHSZJ.js.map} +1 -1
- package/dist/{chunk-ZDYEDI2A.js → chunk-CWGTARDE.js} +2 -2
- package/dist/{chunk-KNBODIQU.js → chunk-DI47UY2H.js} +2 -2
- package/dist/{chunk-KNBODIQU.js.map → chunk-DI47UY2H.js.map} +1 -1
- package/dist/{chunk-IDZR4ROP.js → chunk-EHR3LIPS.js} +2 -2
- package/dist/{chunk-IDZR4ROP.js.map → chunk-EHR3LIPS.js.map} +1 -1
- package/dist/chunk-EY2LLDGY.js +94 -0
- package/dist/chunk-EY2LLDGY.js.map +1 -0
- package/dist/{chunk-MJI74VEJ.js → chunk-F5ENANMJ.js} +18 -2
- package/dist/chunk-F5ENANMJ.js.map +1 -0
- package/dist/chunk-IKJH5ZSJ.js +1 -0
- package/dist/chunk-IKJH5ZSJ.js.map +1 -0
- package/dist/{chunk-IIMTP3XC.js → chunk-KBI45OXI.js} +2 -2
- package/dist/{chunk-SAMIK4WZ.js → chunk-KVUOTFYZ.js} +2 -2
- package/dist/{chunk-U6M3MXNI.js → chunk-L6QWKFGE.js} +3 -2
- package/dist/chunk-L6QWKFGE.js.map +1 -0
- package/dist/{chunk-RDC5GYST.js → chunk-N4LAFGLX.js} +7 -7
- package/dist/{chunk-ZKNPQBIE.js → chunk-R3T2IYOU.js} +5 -3
- package/dist/{chunk-ZKNPQBIE.js.map → chunk-R3T2IYOU.js.map} +1 -1
- package/dist/chunk-U2G5PHHL.js +25 -0
- package/dist/chunk-U2G5PHHL.js.map +1 -0
- package/dist/{chunk-SBGZJVTJ.js → chunk-VQZPADW6.js} +100 -33
- package/dist/chunk-VQZPADW6.js.map +1 -0
- package/dist/{chunk-O32SBS6S.js → chunk-XTWBAL42.js} +2 -2
- package/dist/{chunk-O32SBS6S.js.map → chunk-XTWBAL42.js.map} +1 -1
- package/dist/{chunk-WNB5PSY6.js → chunk-ZMESKGUY.js} +2 -2
- package/dist/{chunk-7ULSRWDH.js → chunk-ZSZVWLGE.js} +2 -2
- package/dist/{embedding-iNQCeXfk.d.ts → embedding-ts1npsDg.d.ts} +1 -1
- package/dist/google/index.d.ts +38 -4
- package/dist/google/index.js +5 -4
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +3 -3
- package/dist/groq/index.js +7 -5
- package/dist/groq/index.js.map +1 -1
- package/dist/http/index.d.ts +5 -5
- package/dist/http/index.js +19 -22
- package/dist/{image-stream-ARno6XlS.d.ts → image-stream-BPml2YZZ.d.ts} +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +306 -112
- package/dist/index.js.map +1 -1
- package/dist/{llm-CZqlijjK.d.ts → llm-BWLaTzzY.d.ts} +75 -29
- package/dist/middleware/logging/index.d.ts +3 -3
- package/dist/middleware/logging/index.js +3 -0
- package/dist/middleware/logging/index.js.map +1 -1
- package/dist/middleware/parsed-object/index.d.ts +3 -3
- package/dist/middleware/parsed-object/index.js +5 -1
- package/dist/middleware/parsed-object/index.js.map +1 -1
- package/dist/middleware/persistence/index.d.ts +3 -3
- package/dist/middleware/persistence/index.js +3 -2
- package/dist/middleware/persistence/index.js.map +1 -1
- package/dist/middleware/pipeline/index.d.ts +195 -0
- package/dist/middleware/pipeline/index.js +61 -0
- package/dist/middleware/pipeline/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +13 -11
- package/dist/middleware/pubsub/index.js +31 -5
- package/dist/middleware/pubsub/index.js.map +1 -1
- package/dist/middleware/pubsub/server/express/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/express/index.js +2 -2
- package/dist/middleware/pubsub/server/fastify/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/fastify/index.js +2 -2
- package/dist/middleware/pubsub/server/h3/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/h3/index.js +2 -2
- package/dist/middleware/pubsub/server/index.d.ts +50 -9
- package/dist/middleware/pubsub/server/index.js +5 -5
- package/dist/middleware/pubsub/server/index.js.map +1 -1
- package/dist/middleware/pubsub/server/webapi/index.d.ts +3 -3
- package/dist/middleware/pubsub/server/webapi/index.js +2 -2
- package/dist/moonshot/index.d.ts +3 -3
- package/dist/moonshot/index.js +7 -5
- package/dist/moonshot/index.js.map +1 -1
- package/dist/ollama/index.d.ts +24 -4
- package/dist/ollama/index.js +5 -4
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +65 -4
- package/dist/openai/index.js +7 -5
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +4 -4
- package/dist/openrouter/index.js +7 -5
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +5 -5
- package/dist/proxy/index.js +16 -15
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +8 -9
- package/dist/proxy/server/express/index.js +4 -3
- package/dist/proxy/server/fastify/index.d.ts +8 -9
- package/dist/proxy/server/fastify/index.js +4 -3
- package/dist/proxy/server/h3/index.d.ts +8 -9
- package/dist/proxy/server/h3/index.js +4 -3
- package/dist/proxy/server/index.d.ts +5 -5
- package/dist/proxy/server/index.js +14 -13
- package/dist/proxy/server/webapi/index.d.ts +8 -9
- package/dist/proxy/server/webapi/index.js +4 -3
- package/dist/responses/index.d.ts +3 -3
- package/dist/responses/index.js +7 -5
- package/dist/responses/index.js.map +1 -1
- package/dist/retry-DVfdPLIB.d.ts +322 -0
- package/dist/{stream-DVVUIKpz.d.ts → stream-bBd_4Ipu.d.ts} +27 -4
- package/dist/{tool-D22EhP5F.d.ts → tool-BmAfKNBq.d.ts} +1 -1
- package/dist/{types-CyXF0J7C.d.ts → types-nTwlpyJE.d.ts} +13 -1
- package/dist/utils/index.d.ts +66 -2
- package/dist/utils/index.js +13 -0
- package/dist/xai/index.d.ts +3 -3
- package/dist/xai/index.js +7 -5
- package/dist/xai/index.js.map +1 -1
- package/package.json +6 -1
- package/dist/chunk-ARVM24K2.js +0 -128
- package/dist/chunk-ARVM24K2.js.map +0 -1
- package/dist/chunk-MJI74VEJ.js.map +0 -1
- package/dist/chunk-SBGZJVTJ.js.map +0 -1
- package/dist/chunk-U6M3MXNI.js.map +0 -1
- package/dist/chunk-WU4U6IHF.js.map +0 -1
- package/dist/chunk-Y5H7C5J4.js +0 -263
- package/dist/chunk-Y5H7C5J4.js.map +0 -1
- package/dist/retry-C1eJbEMV.d.ts +0 -531
- /package/dist/{chunk-ZDYEDI2A.js.map → chunk-CWGTARDE.js.map} +0 -0
- /package/dist/{chunk-IIMTP3XC.js.map → chunk-KBI45OXI.js.map} +0 -0
- /package/dist/{chunk-SAMIK4WZ.js.map → chunk-KVUOTFYZ.js.map} +0 -0
- /package/dist/{chunk-RDC5GYST.js.map → chunk-N4LAFGLX.js.map} +0 -0
- /package/dist/{chunk-WNB5PSY6.js.map → chunk-ZMESKGUY.js.map} +0 -0
- /package/dist/{chunk-7ULSRWDH.js.map → chunk-ZSZVWLGE.js.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -2,13 +2,7 @@ import {
|
|
|
2
2
|
resolveStructure,
|
|
3
3
|
resolveTools
|
|
4
4
|
} from "./chunk-3Q5VELKG.js";
|
|
5
|
-
import
|
|
6
|
-
ExponentialBackoff,
|
|
7
|
-
LinearBackoff,
|
|
8
|
-
NoRetry,
|
|
9
|
-
RetryAfterStrategy,
|
|
10
|
-
TokenBucket
|
|
11
|
-
} from "./chunk-Y5H7C5J4.js";
|
|
5
|
+
import "./chunk-IKJH5ZSJ.js";
|
|
12
6
|
import {
|
|
13
7
|
aggregateUsage,
|
|
14
8
|
createTurn,
|
|
@@ -16,23 +10,28 @@ import {
|
|
|
16
10
|
} from "./chunk-NSE7QN3P.js";
|
|
17
11
|
import {
|
|
18
12
|
Thread
|
|
19
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-R3T2IYOU.js";
|
|
20
14
|
import {
|
|
21
15
|
Image
|
|
22
16
|
} from "./chunk-N5DX5JW3.js";
|
|
23
17
|
import "./chunk-PMK5LZ5Z.js";
|
|
24
18
|
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} from "./chunk-
|
|
19
|
+
dynamicKey,
|
|
20
|
+
roundRobinKeys,
|
|
21
|
+
weightedKeys
|
|
22
|
+
} from "./chunk-EY2LLDGY.js";
|
|
29
23
|
import {
|
|
30
24
|
createProvider,
|
|
31
25
|
resolveEmbeddingHandler,
|
|
32
26
|
resolveImageHandler,
|
|
33
27
|
resolveLLMHandler
|
|
34
28
|
} from "./chunk-JA3UZALR.js";
|
|
35
|
-
import
|
|
29
|
+
import {
|
|
30
|
+
exponentialBackoff,
|
|
31
|
+
linearBackoff,
|
|
32
|
+
noRetry,
|
|
33
|
+
retryAfterStrategy
|
|
34
|
+
} from "./chunk-VQZPADW6.js";
|
|
36
35
|
import {
|
|
37
36
|
StreamEventType,
|
|
38
37
|
contentBlockStart,
|
|
@@ -41,11 +40,12 @@ import {
|
|
|
41
40
|
messageStart,
|
|
42
41
|
messageStop,
|
|
43
42
|
objectDelta,
|
|
43
|
+
streamRetry,
|
|
44
44
|
textDelta,
|
|
45
45
|
toolCallDelta,
|
|
46
46
|
toolExecutionEnd,
|
|
47
47
|
toolExecutionStart
|
|
48
|
-
} from "./chunk-
|
|
48
|
+
} from "./chunk-F5ENANMJ.js";
|
|
49
49
|
import {
|
|
50
50
|
AssistantMessage,
|
|
51
51
|
Message,
|
|
@@ -55,7 +55,8 @@ import {
|
|
|
55
55
|
isAssistantMessage,
|
|
56
56
|
isToolResultMessage,
|
|
57
57
|
isUserMessage
|
|
58
|
-
} from "./chunk-
|
|
58
|
+
} from "./chunk-6QCV4WXF.js";
|
|
59
|
+
import "./chunk-U2G5PHHL.js";
|
|
59
60
|
import {
|
|
60
61
|
isCancelledError,
|
|
61
62
|
toError
|
|
@@ -159,6 +160,17 @@ async function runAbortHook(middlewares, error, ctx) {
|
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
162
|
}
|
|
163
|
+
async function runRetryHook(middlewares, attempt, error, ctx) {
|
|
164
|
+
for (const mw of middlewares) {
|
|
165
|
+
if (mw.onRetry) {
|
|
166
|
+
try {
|
|
167
|
+
await mw.onRetry(attempt, error, ctx);
|
|
168
|
+
} catch (hookError) {
|
|
169
|
+
console.error(`[${mw.name}] Error in onRetry hook:`, hookError);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
162
174
|
async function runToolHook(middlewares, hook, tool, data, ctx) {
|
|
163
175
|
for (const mw of middlewares) {
|
|
164
176
|
const fn = mw[hook];
|
|
@@ -169,11 +181,21 @@ async function runToolHook(middlewares, hook, tool, data, ctx) {
|
|
|
169
181
|
}
|
|
170
182
|
async function runTurnHook(middlewares, turn, ctx) {
|
|
171
183
|
const ordered = [...middlewares].reverse();
|
|
184
|
+
let firstError = null;
|
|
172
185
|
for (const mw of ordered) {
|
|
173
186
|
if (mw.onTurn) {
|
|
174
|
-
|
|
187
|
+
try {
|
|
188
|
+
await mw.onTurn.call(mw, turn, ctx);
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (!firstError) {
|
|
191
|
+
firstError = err instanceof Error ? err : new Error(String(err));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
175
194
|
}
|
|
176
195
|
}
|
|
196
|
+
if (firstError) {
|
|
197
|
+
throw firstError;
|
|
198
|
+
}
|
|
177
199
|
}
|
|
178
200
|
function createStreamTransformer(middlewares, ctx) {
|
|
179
201
|
const streamMiddlewares = middlewares.filter((mw) => mw.onStreamEvent);
|
|
@@ -214,7 +236,12 @@ async function runStreamEndHook(middlewares, ctx) {
|
|
|
214
236
|
}
|
|
215
237
|
}
|
|
216
238
|
}
|
|
217
|
-
|
|
239
|
+
var noopEmit = () => {
|
|
240
|
+
};
|
|
241
|
+
function createEmitHolder() {
|
|
242
|
+
return { fn: noopEmit };
|
|
243
|
+
}
|
|
244
|
+
function createMiddlewareContext(modality, modelId, provider, streaming, request, emitHolder = { fn: noopEmit }) {
|
|
218
245
|
return {
|
|
219
246
|
modality,
|
|
220
247
|
modelId,
|
|
@@ -224,7 +251,8 @@ function createMiddlewareContext(modality, modelId, provider, streaming, request
|
|
|
224
251
|
response: void 0,
|
|
225
252
|
state: /* @__PURE__ */ new Map(),
|
|
226
253
|
startTime: Date.now(),
|
|
227
|
-
endTime: void 0
|
|
254
|
+
endTime: void 0,
|
|
255
|
+
emit: (event) => emitHolder.fn(event)
|
|
228
256
|
};
|
|
229
257
|
}
|
|
230
258
|
function createStreamContext(state) {
|
|
@@ -632,23 +660,79 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
632
660
|
structure,
|
|
633
661
|
config
|
|
634
662
|
};
|
|
663
|
+
const emitHolder = createEmitHolder();
|
|
635
664
|
const ctx = createMiddlewareContext(
|
|
636
665
|
"llm",
|
|
637
666
|
model.modelId,
|
|
638
667
|
model.provider.name,
|
|
639
668
|
true,
|
|
640
|
-
initialRequest
|
|
669
|
+
initialRequest,
|
|
670
|
+
emitHolder
|
|
641
671
|
);
|
|
642
672
|
const streamCtx = createStreamContext(ctx.state);
|
|
643
673
|
const transformer = createStreamTransformer(middleware, streamCtx);
|
|
674
|
+
const lateEventQueue = [];
|
|
675
|
+
let lateEventResolver = null;
|
|
676
|
+
let lateEventsActive = false;
|
|
677
|
+
let lateEventsClosed = false;
|
|
678
|
+
const pushLateEvent = (event) => {
|
|
679
|
+
if (lateEventsClosed) return;
|
|
680
|
+
lateEventQueue.push(event);
|
|
681
|
+
if (lateEventResolver) {
|
|
682
|
+
const resolver = lateEventResolver;
|
|
683
|
+
lateEventResolver = null;
|
|
684
|
+
resolver(false);
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
const closeLateEvents = () => {
|
|
688
|
+
lateEventsClosed = true;
|
|
689
|
+
if (lateEventResolver) {
|
|
690
|
+
lateEventResolver(true);
|
|
691
|
+
lateEventResolver = null;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
const waitForLateEvent = () => {
|
|
695
|
+
if (lateEventQueue.length > 0) {
|
|
696
|
+
return Promise.resolve(false);
|
|
697
|
+
}
|
|
698
|
+
if (lateEventsClosed) {
|
|
699
|
+
return Promise.resolve(true);
|
|
700
|
+
}
|
|
701
|
+
return new Promise((resolve) => {
|
|
702
|
+
lateEventResolver = resolve;
|
|
703
|
+
});
|
|
704
|
+
};
|
|
705
|
+
const inlineEventQueue = [];
|
|
706
|
+
emitHolder.fn = (event) => {
|
|
707
|
+
const result = transformer(event);
|
|
708
|
+
if (result === null) return;
|
|
709
|
+
if (lateEventsActive) {
|
|
710
|
+
if (Array.isArray(result)) {
|
|
711
|
+
for (const e of result) pushLateEvent(e);
|
|
712
|
+
} else {
|
|
713
|
+
pushLateEvent(result);
|
|
714
|
+
}
|
|
715
|
+
} else {
|
|
716
|
+
if (Array.isArray(result)) {
|
|
717
|
+
inlineEventQueue.push(...result);
|
|
718
|
+
} else {
|
|
719
|
+
inlineEventQueue.push(result);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
function* drainInlineEvents() {
|
|
724
|
+
while (inlineEventQueue.length > 0) {
|
|
725
|
+
yield inlineEventQueue.shift();
|
|
726
|
+
}
|
|
727
|
+
}
|
|
644
728
|
let resolveGenerator;
|
|
645
729
|
let rejectGenerator;
|
|
646
730
|
let generatorSettled = false;
|
|
647
731
|
const generatorDone = new Promise((resolve, reject) => {
|
|
648
|
-
resolveGenerator = () => {
|
|
732
|
+
resolveGenerator = (turn) => {
|
|
649
733
|
if (!generatorSettled) {
|
|
650
734
|
generatorSettled = true;
|
|
651
|
-
resolve();
|
|
735
|
+
resolve(turn);
|
|
652
736
|
}
|
|
653
737
|
};
|
|
654
738
|
rejectGenerator = (error) => {
|
|
@@ -664,6 +748,10 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
664
748
|
}
|
|
665
749
|
});
|
|
666
750
|
const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;
|
|
751
|
+
const strategy = (config.retryStrategy ?? noRetry())();
|
|
752
|
+
const strategyMaxAttempts = strategy.maxAttempts ?? 0;
|
|
753
|
+
let attempt = 1;
|
|
754
|
+
let retryMessages = [];
|
|
667
755
|
const onAbort = () => {
|
|
668
756
|
const error = new UPPError("Stream cancelled", ErrorCode.Cancelled, model.provider.name, ModalityType.LLM);
|
|
669
757
|
generatorError = error;
|
|
@@ -679,7 +767,9 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
679
767
|
try {
|
|
680
768
|
ensureNotAborted();
|
|
681
769
|
await runHook(middleware, "onStart", ctx);
|
|
770
|
+
yield* drainInlineEvents();
|
|
682
771
|
await runHook(middleware, "onRequest", ctx);
|
|
772
|
+
yield* drainInlineEvents();
|
|
683
773
|
allMessages = ctx.request.messages;
|
|
684
774
|
const requestMessageCount = allMessages.length;
|
|
685
775
|
const overrideIndex = ctx.state.get(TURN_START_INDEX_KEY);
|
|
@@ -691,64 +781,121 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
691
781
|
typeof overrideIndex === "number" ? overrideIndex : void 0
|
|
692
782
|
);
|
|
693
783
|
validateMediaCapabilities(allMessages, model.capabilities, model.provider.name);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
784
|
+
const retryTurnStartIndex = turnStartIndex;
|
|
785
|
+
let lastStreamResult;
|
|
786
|
+
retryLoop: while (true) {
|
|
787
|
+
try {
|
|
788
|
+
while (cycles < maxIterations + 1) {
|
|
789
|
+
cycles++;
|
|
790
|
+
ensureNotAborted();
|
|
791
|
+
retryMessages = [...allMessages];
|
|
792
|
+
if (strategy.beforeRequest) {
|
|
793
|
+
const beforeDelay = await strategy.beforeRequest();
|
|
794
|
+
if (beforeDelay > 0) {
|
|
795
|
+
await new Promise((resolve, reject) => {
|
|
796
|
+
const timeoutId = setTimeout(resolve, beforeDelay);
|
|
797
|
+
const handleBeforeAbort = () => {
|
|
798
|
+
clearTimeout(timeoutId);
|
|
799
|
+
reject(new UPPError("Stream cancelled", ErrorCode.Cancelled, model.provider.name, ModalityType.LLM));
|
|
800
|
+
};
|
|
801
|
+
if (abortController.signal.aborted) {
|
|
802
|
+
clearTimeout(timeoutId);
|
|
803
|
+
reject(new UPPError("Stream cancelled", ErrorCode.Cancelled, model.provider.name, ModalityType.LLM));
|
|
804
|
+
} else {
|
|
805
|
+
abortController.signal.addEventListener("abort", handleBeforeAbort, { once: true });
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
const request = {
|
|
811
|
+
messages: allMessages,
|
|
812
|
+
system,
|
|
813
|
+
params,
|
|
814
|
+
tools,
|
|
815
|
+
structure,
|
|
816
|
+
config,
|
|
817
|
+
signal: abortController.signal
|
|
818
|
+
};
|
|
819
|
+
const streamResult = model.stream(request);
|
|
820
|
+
lastStreamResult = streamResult;
|
|
821
|
+
for await (const event of streamResult) {
|
|
822
|
+
ensureNotAborted();
|
|
823
|
+
const transformed = transformer(event);
|
|
824
|
+
if (transformed === null) continue;
|
|
825
|
+
if (Array.isArray(transformed)) {
|
|
826
|
+
for (const e of transformed) {
|
|
827
|
+
yield e;
|
|
828
|
+
}
|
|
829
|
+
} else {
|
|
830
|
+
yield transformed;
|
|
831
|
+
}
|
|
832
|
+
yield* drainInlineEvents();
|
|
833
|
+
}
|
|
834
|
+
const response = await streamResult.response;
|
|
835
|
+
attempt = 1;
|
|
836
|
+
strategy.reset?.();
|
|
837
|
+
usages.push(response.usage);
|
|
838
|
+
allMessages.push(response.message);
|
|
839
|
+
if (response.data !== void 0) {
|
|
840
|
+
structuredData = response.data;
|
|
841
|
+
}
|
|
842
|
+
if (response.message.hasToolCalls && tools && tools.length > 0) {
|
|
843
|
+
if (response.data !== void 0) {
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
if (cycles >= maxIterations) {
|
|
847
|
+
await toolStrategy?.onMaxIterations?.(maxIterations);
|
|
848
|
+
throw new UPPError(
|
|
849
|
+
`Tool execution exceeded maximum iterations (${maxIterations})`,
|
|
850
|
+
ErrorCode.InvalidRequest,
|
|
851
|
+
model.provider.name,
|
|
852
|
+
ModalityType.LLM
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
const toolEvents = [];
|
|
856
|
+
const results = await executeTools(
|
|
857
|
+
response.message,
|
|
858
|
+
tools,
|
|
859
|
+
toolStrategy,
|
|
860
|
+
toolExecutions,
|
|
861
|
+
(event) => toolEvents.push(event),
|
|
862
|
+
middleware,
|
|
863
|
+
ctx
|
|
864
|
+
);
|
|
865
|
+
for (const event of toolEvents) {
|
|
866
|
+
ensureNotAborted();
|
|
867
|
+
const transformed = transformer(event);
|
|
868
|
+
if (transformed === null) continue;
|
|
869
|
+
if (Array.isArray(transformed)) {
|
|
870
|
+
for (const e of transformed) {
|
|
871
|
+
yield e;
|
|
872
|
+
}
|
|
873
|
+
} else {
|
|
874
|
+
yield transformed;
|
|
875
|
+
}
|
|
876
|
+
yield* drainInlineEvents();
|
|
877
|
+
}
|
|
878
|
+
allMessages.push(new ToolResultMessage(results));
|
|
879
|
+
continue;
|
|
714
880
|
}
|
|
715
|
-
} else {
|
|
716
|
-
yield transformed;
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
const response = await streamResult.response;
|
|
720
|
-
usages.push(response.usage);
|
|
721
|
-
allMessages.push(response.message);
|
|
722
|
-
if (response.data !== void 0) {
|
|
723
|
-
structuredData = response.data;
|
|
724
|
-
}
|
|
725
|
-
if (response.message.hasToolCalls && tools && tools.length > 0) {
|
|
726
|
-
if (response.data !== void 0) {
|
|
727
881
|
break;
|
|
728
882
|
}
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
ModalityType.LLM
|
|
736
|
-
);
|
|
883
|
+
break retryLoop;
|
|
884
|
+
} catch (streamError) {
|
|
885
|
+
const err = toError(streamError);
|
|
886
|
+
const uppErr = err instanceof UPPError ? err : null;
|
|
887
|
+
if (!uppErr || isCancelledError(err)) {
|
|
888
|
+
throw err;
|
|
737
889
|
}
|
|
738
|
-
const
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
);
|
|
748
|
-
for (const event of toolEvents) {
|
|
749
|
-
ensureNotAborted();
|
|
750
|
-
const transformed = transformer(event);
|
|
751
|
-
if (transformed === null) continue;
|
|
890
|
+
const delay = await strategy.onRetry(uppErr, attempt);
|
|
891
|
+
if (delay === null) {
|
|
892
|
+
throw err;
|
|
893
|
+
}
|
|
894
|
+
lastStreamResult?.response.catch(() => {
|
|
895
|
+
});
|
|
896
|
+
const retryEvent = streamRetry(attempt, strategyMaxAttempts, err, Date.now());
|
|
897
|
+
const transformed = transformer(retryEvent);
|
|
898
|
+
if (transformed !== null) {
|
|
752
899
|
if (Array.isArray(transformed)) {
|
|
753
900
|
for (const e of transformed) {
|
|
754
901
|
yield e;
|
|
@@ -757,14 +904,84 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
757
904
|
yield transformed;
|
|
758
905
|
}
|
|
759
906
|
}
|
|
760
|
-
|
|
761
|
-
|
|
907
|
+
await runRetryHook(middleware, attempt, err, ctx);
|
|
908
|
+
yield* drainInlineEvents();
|
|
909
|
+
allMessages = [...retryMessages];
|
|
910
|
+
turnStartIndex = retryTurnStartIndex;
|
|
911
|
+
cycles--;
|
|
912
|
+
structuredData = void 0;
|
|
913
|
+
ctx.request = {
|
|
914
|
+
messages: allMessages,
|
|
915
|
+
system,
|
|
916
|
+
params,
|
|
917
|
+
tools,
|
|
918
|
+
structure,
|
|
919
|
+
config
|
|
920
|
+
};
|
|
921
|
+
attempt += 1;
|
|
922
|
+
if (delay > 0) {
|
|
923
|
+
await new Promise((resolve, reject) => {
|
|
924
|
+
const timeoutId = setTimeout(resolve, delay);
|
|
925
|
+
const handleDelayAbort = () => {
|
|
926
|
+
clearTimeout(timeoutId);
|
|
927
|
+
reject(new UPPError("Stream cancelled", ErrorCode.Cancelled, model.provider.name, ModalityType.LLM));
|
|
928
|
+
};
|
|
929
|
+
if (abortController.signal.aborted) {
|
|
930
|
+
clearTimeout(timeoutId);
|
|
931
|
+
reject(new UPPError("Stream cancelled", ErrorCode.Cancelled, model.provider.name, ModalityType.LLM));
|
|
932
|
+
} else {
|
|
933
|
+
abortController.signal.addEventListener("abort", handleDelayAbort, { once: true });
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
continue retryLoop;
|
|
762
938
|
}
|
|
763
|
-
break;
|
|
764
939
|
}
|
|
765
|
-
|
|
940
|
+
lateEventsActive = true;
|
|
941
|
+
let hooksTurn = null;
|
|
942
|
+
let hooksError = null;
|
|
943
|
+
const hooksPromise = (async () => {
|
|
944
|
+
try {
|
|
945
|
+
await runStreamEndHook(middleware, streamCtx);
|
|
946
|
+
const data = structure ? structuredData : void 0;
|
|
947
|
+
const turn = createTurn(
|
|
948
|
+
allMessages.slice(turnStartIndex),
|
|
949
|
+
toolExecutions,
|
|
950
|
+
aggregateUsage(usages),
|
|
951
|
+
cycles,
|
|
952
|
+
data
|
|
953
|
+
);
|
|
954
|
+
ctx.response = {
|
|
955
|
+
message: turn.response,
|
|
956
|
+
usage: turn.usage,
|
|
957
|
+
stopReason: "end_turn",
|
|
958
|
+
data
|
|
959
|
+
};
|
|
960
|
+
ctx.endTime = Date.now();
|
|
961
|
+
await runHook(middleware, "onResponse", ctx, true);
|
|
962
|
+
await runTurnHook(middleware, turn, ctx);
|
|
963
|
+
await runHook(middleware, "onEnd", ctx, true);
|
|
964
|
+
hooksTurn = turn;
|
|
965
|
+
} catch (err) {
|
|
966
|
+
hooksError = err instanceof Error ? err : new Error(String(err));
|
|
967
|
+
} finally {
|
|
968
|
+
lateEventsActive = false;
|
|
969
|
+
closeLateEvents();
|
|
970
|
+
}
|
|
971
|
+
})();
|
|
972
|
+
while (true) {
|
|
973
|
+
const done = await waitForLateEvent();
|
|
974
|
+
if (done) break;
|
|
975
|
+
while (lateEventQueue.length > 0) {
|
|
976
|
+
yield lateEventQueue.shift();
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
await hooksPromise;
|
|
980
|
+
if (hooksError !== null) {
|
|
981
|
+
throw toError(hooksError);
|
|
982
|
+
}
|
|
766
983
|
generatorCompleted = true;
|
|
767
|
-
resolveGenerator();
|
|
984
|
+
resolveGenerator(hooksTurn);
|
|
768
985
|
} catch (error) {
|
|
769
986
|
const err = toError(error);
|
|
770
987
|
generatorError = err;
|
|
@@ -789,29 +1006,7 @@ function executeStream(model, config, system, params, tools, toolStrategy, struc
|
|
|
789
1006
|
}
|
|
790
1007
|
}
|
|
791
1008
|
const createTurnPromise = async () => {
|
|
792
|
-
|
|
793
|
-
if (generatorError) {
|
|
794
|
-
throw generatorError;
|
|
795
|
-
}
|
|
796
|
-
const data = structure ? structuredData : void 0;
|
|
797
|
-
const turn = createTurn(
|
|
798
|
-
allMessages.slice(turnStartIndex),
|
|
799
|
-
toolExecutions,
|
|
800
|
-
aggregateUsage(usages),
|
|
801
|
-
cycles,
|
|
802
|
-
data
|
|
803
|
-
);
|
|
804
|
-
ctx.response = {
|
|
805
|
-
message: turn.response,
|
|
806
|
-
usage: turn.usage,
|
|
807
|
-
stopReason: "end_turn",
|
|
808
|
-
data
|
|
809
|
-
};
|
|
810
|
-
ctx.endTime = Date.now();
|
|
811
|
-
await runHook(middleware, "onResponse", ctx, true);
|
|
812
|
-
await runTurnHook(middleware, turn, ctx);
|
|
813
|
-
await runHook(middleware, "onEnd", ctx, true);
|
|
814
|
-
return turn;
|
|
1009
|
+
return generatorDone;
|
|
815
1010
|
};
|
|
816
1011
|
return createStreamResult(generateStream(), createTurnPromise, abortController);
|
|
817
1012
|
}
|
|
@@ -1939,27 +2134,19 @@ export {
|
|
|
1939
2134
|
ContentBlockType,
|
|
1940
2135
|
Document,
|
|
1941
2136
|
DocumentSourceType,
|
|
1942
|
-
DynamicKey,
|
|
1943
2137
|
EmbeddingInputType,
|
|
1944
2138
|
ErrorCode,
|
|
1945
|
-
ExponentialBackoff,
|
|
1946
2139
|
Image,
|
|
1947
2140
|
ImageSourceType,
|
|
1948
|
-
LinearBackoff,
|
|
1949
2141
|
Message,
|
|
1950
2142
|
MessageRole,
|
|
1951
2143
|
ModalityType,
|
|
1952
|
-
NoRetry,
|
|
1953
|
-
RetryAfterStrategy,
|
|
1954
|
-
RoundRobinKeys,
|
|
1955
2144
|
StreamEventType,
|
|
1956
2145
|
Thread,
|
|
1957
|
-
TokenBucket,
|
|
1958
2146
|
ToolResultMessage,
|
|
1959
2147
|
UPPError,
|
|
1960
2148
|
UserMessage,
|
|
1961
2149
|
Video,
|
|
1962
|
-
WeightedKeys,
|
|
1963
2150
|
aggregateUsage,
|
|
1964
2151
|
ai,
|
|
1965
2152
|
contentBlockStart,
|
|
@@ -1967,8 +2154,10 @@ export {
|
|
|
1967
2154
|
createProvider,
|
|
1968
2155
|
createStreamResult,
|
|
1969
2156
|
createTurn,
|
|
2157
|
+
dynamicKey,
|
|
1970
2158
|
embedding,
|
|
1971
2159
|
emptyUsage,
|
|
2160
|
+
exponentialBackoff,
|
|
1972
2161
|
image,
|
|
1973
2162
|
isAssistantMessage,
|
|
1974
2163
|
isAudioBlock,
|
|
@@ -1980,15 +2169,20 @@ export {
|
|
|
1980
2169
|
isToolResultMessage,
|
|
1981
2170
|
isUserMessage,
|
|
1982
2171
|
isVideoBlock,
|
|
2172
|
+
linearBackoff,
|
|
1983
2173
|
llm,
|
|
1984
2174
|
messageStart,
|
|
1985
2175
|
messageStop,
|
|
2176
|
+
noRetry,
|
|
1986
2177
|
objectDelta,
|
|
1987
2178
|
reasoning,
|
|
2179
|
+
retryAfterStrategy,
|
|
2180
|
+
roundRobinKeys,
|
|
1988
2181
|
text,
|
|
1989
2182
|
textDelta,
|
|
1990
2183
|
toolCallDelta,
|
|
1991
2184
|
toolExecutionEnd,
|
|
1992
|
-
toolExecutionStart
|
|
2185
|
+
toolExecutionStart,
|
|
2186
|
+
weightedKeys
|
|
1993
2187
|
};
|
|
1994
2188
|
//# sourceMappingURL=index.js.map
|