@jerome-benoit/sap-ai-provider-v2 5.0.0 → 5.0.2
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 +90 -48
- package/dist/chunk-77JSAAPE.js +778 -0
- package/dist/chunk-77JSAAPE.js.map +1 -0
- package/dist/{chunk-Q5JV4I6D.js → chunk-LEME2MFD.js} +59 -387
- package/dist/chunk-LEME2MFD.js.map +1 -0
- package/dist/{chunk-TXZZURBA.js → chunk-NLMWIVG7.js} +17 -7
- package/dist/chunk-NLMWIVG7.js.map +1 -0
- package/dist/chunk-QZOZ3PZT.js +634 -0
- package/dist/chunk-QZOZ3PZT.js.map +1 -0
- package/dist/{chunk-FY2HO62O.js → chunk-U6WJSLR7.js} +19 -11
- package/dist/chunk-U6WJSLR7.js.map +1 -0
- package/dist/{chunk-QDUZRLMI.js → chunk-YP3AADWD.js} +47 -40
- package/dist/chunk-YP3AADWD.js.map +1 -0
- package/dist/{foundation-models-embedding-model-strategy-JJBN3TQI.js → foundation-models-embedding-model-strategy-A3WDDAYK.js} +8 -9
- package/dist/foundation-models-embedding-model-strategy-A3WDDAYK.js.map +1 -0
- package/dist/{foundation-models-language-model-strategy-6ELKE34K.js → foundation-models-language-model-strategy-6U6KK6KH.js} +18 -21
- package/dist/foundation-models-language-model-strategy-6U6KK6KH.js.map +1 -0
- package/dist/index.cjs +1325 -31669
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +60 -55
- package/dist/index.d.ts +60 -55
- package/dist/index.js +48 -194
- package/dist/index.js.map +1 -1
- package/dist/{orchestration-embedding-model-strategy-QI72OB3E.js → orchestration-embedding-model-strategy-JO3QH5MR.js} +7 -11
- package/dist/orchestration-embedding-model-strategy-JO3QH5MR.js.map +1 -0
- package/dist/{orchestration-language-model-strategy-X4FZNLSA.js → orchestration-language-model-strategy-MBVA7BYX.js} +27 -28
- package/dist/orchestration-language-model-strategy-MBVA7BYX.js.map +1 -0
- package/package.json +15 -12
- package/dist/chunk-BIBSYTMU.js +0 -31085
- package/dist/chunk-BIBSYTMU.js.map +0 -1
- package/dist/chunk-FY2HO62O.js.map +0 -1
- package/dist/chunk-Q5JV4I6D.js.map +0 -1
- package/dist/chunk-QDUZRLMI.js.map +0 -1
- package/dist/chunk-TXZZURBA.js.map +0 -1
- package/dist/chunk-WNR7DC4V.js +0 -251
- package/dist/chunk-WNR7DC4V.js.map +0 -1
- package/dist/foundation-models-embedding-model-strategy-JJBN3TQI.js.map +0 -1
- package/dist/foundation-models-language-model-strategy-6ELKE34K.js.map +0 -1
- package/dist/orchestration-embedding-model-strategy-QI72OB3E.js.map +0 -1
- package/dist/orchestration-language-model-strategy-X4FZNLSA.js.map +0 -1
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import {createRequire as __createRequire} from 'module';var require=__createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
convertToSAPMessages
|
|
4
|
+
} from "./chunk-YP3AADWD.js";
|
|
5
|
+
import {
|
|
6
|
+
buildAnthropicCacheMetadata,
|
|
7
|
+
buildGenerateResult,
|
|
8
|
+
buildModelParams,
|
|
9
|
+
createAISDKRequestBodySummary,
|
|
10
|
+
extractCompletionMetadata,
|
|
11
|
+
extractResponseMetadata,
|
|
12
|
+
mapFinishReason,
|
|
13
|
+
mapTokenUsage,
|
|
14
|
+
mapToolChoice,
|
|
15
|
+
mergeRequestConfig,
|
|
16
|
+
sanitizeAsJSONArray
|
|
17
|
+
} from "./chunk-LEME2MFD.js";
|
|
18
|
+
import {
|
|
19
|
+
VERSION,
|
|
20
|
+
convertToAISDKError,
|
|
21
|
+
getProviderName,
|
|
22
|
+
normalizeHeaders
|
|
23
|
+
} from "./chunk-77JSAAPE.js";
|
|
24
|
+
|
|
25
|
+
// src/stream-transformer.ts
|
|
26
|
+
var StreamIdGenerator = class {
|
|
27
|
+
/**
|
|
28
|
+
* @returns A UUID string for identifying the response.
|
|
29
|
+
*/
|
|
30
|
+
generateResponseId() {
|
|
31
|
+
return crypto.randomUUID();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @returns A UUID string for identifying a text block.
|
|
35
|
+
*/
|
|
36
|
+
generateTextBlockId() {
|
|
37
|
+
return crypto.randomUUID();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @returns A UUID string for identifying a tool call when the API does not provide one.
|
|
41
|
+
*/
|
|
42
|
+
generateToolCallId() {
|
|
43
|
+
return crypto.randomUUID();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
function createAbortError(reason) {
|
|
47
|
+
const error = new Error("The operation was aborted.", { cause: reason });
|
|
48
|
+
error.name = "AbortError";
|
|
49
|
+
return error;
|
|
50
|
+
}
|
|
51
|
+
function createInitialStreamState() {
|
|
52
|
+
return {
|
|
53
|
+
activeText: false,
|
|
54
|
+
finishReason: {
|
|
55
|
+
raw: void 0,
|
|
56
|
+
unified: "other"
|
|
57
|
+
},
|
|
58
|
+
isFirstChunk: true,
|
|
59
|
+
usage: {
|
|
60
|
+
inputTokens: {
|
|
61
|
+
cacheRead: void 0,
|
|
62
|
+
cacheWrite: void 0,
|
|
63
|
+
noCache: void 0,
|
|
64
|
+
total: void 0
|
|
65
|
+
},
|
|
66
|
+
outputTokens: {
|
|
67
|
+
reasoning: void 0,
|
|
68
|
+
text: void 0,
|
|
69
|
+
total: void 0
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function createStreamTransformer(config) {
|
|
75
|
+
const {
|
|
76
|
+
cancel,
|
|
77
|
+
convertToAISDKError: convertToAISDKError2,
|
|
78
|
+
extractChunkMetadata,
|
|
79
|
+
idGenerator,
|
|
80
|
+
includeRawChunks,
|
|
81
|
+
modelId,
|
|
82
|
+
options,
|
|
83
|
+
providerName,
|
|
84
|
+
requestId,
|
|
85
|
+
responseMetadata,
|
|
86
|
+
sdkStream,
|
|
87
|
+
streamResponseGetCitations,
|
|
88
|
+
streamResponseGetFinishReason,
|
|
89
|
+
streamResponseGetIntermediateFailures,
|
|
90
|
+
url,
|
|
91
|
+
version,
|
|
92
|
+
warnings
|
|
93
|
+
} = config;
|
|
94
|
+
let textBlockId = null;
|
|
95
|
+
const streamState = createInitialStreamState();
|
|
96
|
+
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
97
|
+
const resolvedResponseMetadata = { ...responseMetadata };
|
|
98
|
+
let resolvedRequestId = requestId;
|
|
99
|
+
let tokenUsage;
|
|
100
|
+
let citations;
|
|
101
|
+
function collectCitations(incoming) {
|
|
102
|
+
if (!incoming?.length) return;
|
|
103
|
+
citations ??= /* @__PURE__ */ new Map();
|
|
104
|
+
for (const citation of incoming) {
|
|
105
|
+
citations.set(String(citation.ref_id ?? citation.url), citation);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function collectUsage(incoming) {
|
|
109
|
+
if (!incoming) return;
|
|
110
|
+
const promptDetails = tokenUsage?.prompt_tokens_details;
|
|
111
|
+
const incomingPromptDetails = incoming.prompt_tokens_details;
|
|
112
|
+
const completionDetails = tokenUsage?.completion_tokens_details;
|
|
113
|
+
const incomingCompletionDetails = incoming.completion_tokens_details;
|
|
114
|
+
const cacheDetails = promptDetails?.cache_creation_token_details;
|
|
115
|
+
const incomingCacheDetails = incomingPromptDetails?.cache_creation_token_details;
|
|
116
|
+
tokenUsage = {
|
|
117
|
+
...tokenUsage,
|
|
118
|
+
...incoming,
|
|
119
|
+
...promptDetails || incomingPromptDetails ? {
|
|
120
|
+
prompt_tokens_details: {
|
|
121
|
+
...promptDetails,
|
|
122
|
+
...incomingPromptDetails,
|
|
123
|
+
...cacheDetails || incomingCacheDetails ? {
|
|
124
|
+
cache_creation_token_details: { ...cacheDetails, ...incomingCacheDetails }
|
|
125
|
+
} : {}
|
|
126
|
+
}
|
|
127
|
+
} : {},
|
|
128
|
+
...completionDetails || incomingCompletionDetails ? {
|
|
129
|
+
completion_tokens_details: { ...completionDetails, ...incomingCompletionDetails }
|
|
130
|
+
} : {}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function emitToolInputStart(tc, controller) {
|
|
134
|
+
if (tc.didEmitInputStart || tc.id == null) return;
|
|
135
|
+
tc.didEmitInputStart = true;
|
|
136
|
+
controller.enqueue({
|
|
137
|
+
id: tc.id,
|
|
138
|
+
toolName: tc.toolName ?? "",
|
|
139
|
+
type: "tool-input-start"
|
|
140
|
+
});
|
|
141
|
+
if (tc.arguments.length > 0) {
|
|
142
|
+
controller.enqueue({
|
|
143
|
+
delta: tc.arguments,
|
|
144
|
+
id: tc.id,
|
|
145
|
+
type: "tool-input-delta"
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const iterator = safeIterate(sdkStream, options.abortSignal)[Symbol.asyncIterator]();
|
|
150
|
+
let canceled = false;
|
|
151
|
+
return new ReadableStream({
|
|
152
|
+
async cancel() {
|
|
153
|
+
canceled = true;
|
|
154
|
+
cancel();
|
|
155
|
+
await iterator.return(void 0);
|
|
156
|
+
},
|
|
157
|
+
async pull(controller) {
|
|
158
|
+
const { done, value } = await iterator.next();
|
|
159
|
+
if (canceled) return;
|
|
160
|
+
if (done) controller.close();
|
|
161
|
+
else controller.enqueue(value);
|
|
162
|
+
}
|
|
163
|
+
}).pipeThrough(
|
|
164
|
+
new TransformStream({
|
|
165
|
+
flush(controller) {
|
|
166
|
+
if (options.abortSignal?.aborted) {
|
|
167
|
+
handleStreamError(
|
|
168
|
+
createAbortError(options.abortSignal.reason),
|
|
169
|
+
controller,
|
|
170
|
+
convertToAISDKError2,
|
|
171
|
+
options,
|
|
172
|
+
modelId,
|
|
173
|
+
url
|
|
174
|
+
);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const didEmitAnyToolCalls = finalizeToolCalls(
|
|
178
|
+
controller,
|
|
179
|
+
toolCallsInProgress,
|
|
180
|
+
idGenerator,
|
|
181
|
+
emitToolInputStart
|
|
182
|
+
);
|
|
183
|
+
if (streamState.activeText && textBlockId) {
|
|
184
|
+
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
185
|
+
}
|
|
186
|
+
const finalFinishReason = streamResponseGetFinishReason();
|
|
187
|
+
if (finalFinishReason) {
|
|
188
|
+
streamState.finishReason = mapFinishReason(finalFinishReason);
|
|
189
|
+
} else if (didEmitAnyToolCalls) {
|
|
190
|
+
streamState.finishReason = {
|
|
191
|
+
raw: void 0,
|
|
192
|
+
unified: "tool-calls"
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
streamState.usage = mapTokenUsage(tokenUsage);
|
|
196
|
+
collectCitations(streamResponseGetCitations?.());
|
|
197
|
+
if (citations) {
|
|
198
|
+
for (const citation of citations.values()) {
|
|
199
|
+
controller.enqueue({
|
|
200
|
+
id: String(citation.ref_id ?? citation.url),
|
|
201
|
+
sourceType: "url",
|
|
202
|
+
title: citation.title,
|
|
203
|
+
type: "source",
|
|
204
|
+
url: citation.url
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const streamIntermediateFailures = streamResponseGetIntermediateFailures?.();
|
|
209
|
+
controller.enqueue({
|
|
210
|
+
finishReason: streamState.finishReason,
|
|
211
|
+
providerMetadata: {
|
|
212
|
+
[providerName]: {
|
|
213
|
+
...buildAnthropicCacheMetadata(tokenUsage),
|
|
214
|
+
finishReason: streamState.finishReason.raw ?? "unknown",
|
|
215
|
+
finishReasonMapped: streamState.finishReason,
|
|
216
|
+
...streamIntermediateFailures?.length ? {
|
|
217
|
+
intermediateFailures: sanitizeAsJSONArray(streamIntermediateFailures)
|
|
218
|
+
} : {},
|
|
219
|
+
...resolvedRequestId ? { requestId: resolvedRequestId } : {},
|
|
220
|
+
responseId: resolvedResponseMetadata.id,
|
|
221
|
+
version
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
type: "finish",
|
|
225
|
+
usage: streamState.usage
|
|
226
|
+
});
|
|
227
|
+
},
|
|
228
|
+
start(controller) {
|
|
229
|
+
controller.enqueue({
|
|
230
|
+
type: "stream-start",
|
|
231
|
+
warnings: [...warnings]
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
transform(chunk, controller) {
|
|
235
|
+
if (options.abortSignal?.aborted && !(chunk instanceof Error)) {
|
|
236
|
+
chunk = createAbortError(options.abortSignal.reason);
|
|
237
|
+
}
|
|
238
|
+
if (chunk instanceof Error) {
|
|
239
|
+
handleStreamError(chunk, controller, convertToAISDKError2, options, modelId, url);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (includeRawChunks) {
|
|
243
|
+
controller.enqueue({
|
|
244
|
+
rawValue: chunk._data ?? chunk,
|
|
245
|
+
type: "raw"
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const metadata = extractChunkMetadata(chunk);
|
|
249
|
+
if (metadata.requestId) resolvedRequestId = metadata.requestId;
|
|
250
|
+
const incomingMetadata = metadata.responseMetadata;
|
|
251
|
+
const metadataChanged = incomingMetadata.id !== void 0 && incomingMetadata.id !== resolvedResponseMetadata.id || incomingMetadata.modelId !== void 0 && incomingMetadata.modelId !== resolvedResponseMetadata.modelId || incomingMetadata.timestamp !== void 0 && incomingMetadata.timestamp.getTime() !== resolvedResponseMetadata.timestamp?.getTime();
|
|
252
|
+
Object.assign(resolvedResponseMetadata, incomingMetadata);
|
|
253
|
+
collectUsage(chunk.getTokenUsage?.());
|
|
254
|
+
collectCitations(chunk.getCitations?.());
|
|
255
|
+
if (streamState.isFirstChunk || metadataChanged) {
|
|
256
|
+
streamState.isFirstChunk = false;
|
|
257
|
+
controller.enqueue({
|
|
258
|
+
...resolvedResponseMetadata,
|
|
259
|
+
type: "response-metadata"
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const deltaToolCalls = chunk.getDeltaToolCalls();
|
|
263
|
+
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
264
|
+
streamState.finishReason = {
|
|
265
|
+
raw: void 0,
|
|
266
|
+
unified: "tool-calls"
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
const deltaContent = chunk.getDeltaContent();
|
|
270
|
+
if (typeof deltaContent === "string" && deltaContent.length > 0) {
|
|
271
|
+
textBlockId = handleTextDelta(
|
|
272
|
+
deltaContent,
|
|
273
|
+
controller,
|
|
274
|
+
streamState,
|
|
275
|
+
textBlockId,
|
|
276
|
+
idGenerator
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
if (Array.isArray(deltaToolCalls) && deltaToolCalls.length > 0) {
|
|
280
|
+
handleToolCallDeltas(deltaToolCalls, controller, toolCallsInProgress, emitToolInputStart);
|
|
281
|
+
}
|
|
282
|
+
const chunkFinishReason = chunk.getFinishReason();
|
|
283
|
+
if (chunkFinishReason) {
|
|
284
|
+
streamState.finishReason = mapFinishReason(chunkFinishReason);
|
|
285
|
+
if (streamState.finishReason.unified === "tool-calls") {
|
|
286
|
+
finalizeToolCalls(controller, toolCallsInProgress, idGenerator, emitToolInputStart);
|
|
287
|
+
if (streamState.activeText && textBlockId) {
|
|
288
|
+
controller.enqueue({ id: textBlockId, type: "text-end" });
|
|
289
|
+
streamState.activeText = false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function finalizeToolCalls(controller, toolCallsInProgress, idGenerator, emitToolInputStart) {
|
|
298
|
+
let didEmitAnyToolCalls = false;
|
|
299
|
+
for (const tc of toolCallsInProgress.values()) {
|
|
300
|
+
if (tc.didEmitCall) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
tc.id ??= idGenerator.generateToolCallId();
|
|
304
|
+
emitToolInputStart(tc, controller);
|
|
305
|
+
didEmitAnyToolCalls = true;
|
|
306
|
+
tc.didEmitCall = true;
|
|
307
|
+
controller.enqueue({ id: tc.id, type: "tool-input-end" });
|
|
308
|
+
controller.enqueue({
|
|
309
|
+
input: tc.arguments,
|
|
310
|
+
toolCallId: tc.id,
|
|
311
|
+
toolName: tc.toolName ?? "",
|
|
312
|
+
type: "tool-call"
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
return didEmitAnyToolCalls;
|
|
316
|
+
}
|
|
317
|
+
function handleStreamError(error, controller, convertToAISDKError2, options, modelId, url) {
|
|
318
|
+
const aiError = convertToAISDKError2(error, {
|
|
319
|
+
modelId,
|
|
320
|
+
modelType: "languageModel",
|
|
321
|
+
operation: "doStream",
|
|
322
|
+
requestBody: createAISDKRequestBodySummary(options),
|
|
323
|
+
url
|
|
324
|
+
});
|
|
325
|
+
controller.enqueue({
|
|
326
|
+
error: aiError instanceof Error ? aiError : new Error(String(aiError)),
|
|
327
|
+
type: "error"
|
|
328
|
+
});
|
|
329
|
+
controller.terminate();
|
|
330
|
+
}
|
|
331
|
+
function handleTextDelta(deltaContent, controller, streamState, currentTextBlockId, idGenerator) {
|
|
332
|
+
let textBlockId = currentTextBlockId;
|
|
333
|
+
if (!streamState.activeText) {
|
|
334
|
+
textBlockId = idGenerator.generateTextBlockId();
|
|
335
|
+
controller.enqueue({ id: textBlockId, type: "text-start" });
|
|
336
|
+
streamState.activeText = true;
|
|
337
|
+
}
|
|
338
|
+
if (textBlockId) {
|
|
339
|
+
controller.enqueue({
|
|
340
|
+
delta: deltaContent,
|
|
341
|
+
id: textBlockId,
|
|
342
|
+
type: "text-delta"
|
|
343
|
+
});
|
|
344
|
+
return textBlockId;
|
|
345
|
+
}
|
|
346
|
+
return currentTextBlockId ?? idGenerator.generateTextBlockId();
|
|
347
|
+
}
|
|
348
|
+
function handleToolCallDeltas(deltaToolCalls, controller, toolCallsInProgress, emitToolInputStart) {
|
|
349
|
+
for (const toolCallChunk of deltaToolCalls) {
|
|
350
|
+
const index = toolCallChunk.index;
|
|
351
|
+
if (typeof index !== "number" || !Number.isFinite(index)) {
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
if (!toolCallsInProgress.has(index)) {
|
|
355
|
+
toolCallsInProgress.set(index, {
|
|
356
|
+
arguments: "",
|
|
357
|
+
didEmitCall: false,
|
|
358
|
+
didEmitInputStart: false,
|
|
359
|
+
id: typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 ? toolCallChunk.id : void 0,
|
|
360
|
+
toolName: toolCallChunk.function?.name
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
const tc = toolCallsInProgress.get(index);
|
|
364
|
+
if (!tc) continue;
|
|
365
|
+
if (typeof toolCallChunk.id === "string" && toolCallChunk.id.length > 0 && tc.id === void 0) {
|
|
366
|
+
tc.id = toolCallChunk.id;
|
|
367
|
+
}
|
|
368
|
+
const nextToolName = toolCallChunk.function?.name;
|
|
369
|
+
if (typeof nextToolName === "string" && nextToolName.length > 0) {
|
|
370
|
+
tc.toolName = nextToolName;
|
|
371
|
+
}
|
|
372
|
+
if (!tc.didEmitInputStart && tc.toolName != null && tc.id != null) {
|
|
373
|
+
emitToolInputStart(tc, controller);
|
|
374
|
+
}
|
|
375
|
+
const argumentsDelta = toolCallChunk.function?.arguments;
|
|
376
|
+
if (typeof argumentsDelta === "string" && argumentsDelta.length > 0) {
|
|
377
|
+
tc.arguments += argumentsDelta;
|
|
378
|
+
if (tc.didEmitInputStart && tc.id != null) {
|
|
379
|
+
controller.enqueue({
|
|
380
|
+
delta: argumentsDelta,
|
|
381
|
+
id: tc.id,
|
|
382
|
+
type: "tool-input-delta"
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
async function* safeIterate(iterable, abortSignal) {
|
|
389
|
+
try {
|
|
390
|
+
for await (const chunk of iterable) {
|
|
391
|
+
abortSignal?.throwIfAborted();
|
|
392
|
+
yield chunk;
|
|
393
|
+
}
|
|
394
|
+
abortSignal?.throwIfAborted();
|
|
395
|
+
} catch (error) {
|
|
396
|
+
if (abortSignal?.aborted) {
|
|
397
|
+
yield createAbortError(abortSignal.reason);
|
|
398
|
+
} else {
|
|
399
|
+
yield error instanceof Error ? error : new Error(String(error));
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/base-language-model-strategy.ts
|
|
405
|
+
var BaseLanguageModelStrategy = class {
|
|
406
|
+
/**
|
|
407
|
+
* Common parameter mappings for language model APIs.
|
|
408
|
+
* @internal
|
|
409
|
+
*/
|
|
410
|
+
static COMMON_PARAM_MAPPINGS = [
|
|
411
|
+
{ camelCaseKey: "maxTokens", optionKey: "maxOutputTokens", outputKey: "max_completion_tokens" },
|
|
412
|
+
{ camelCaseKey: "temperature", optionKey: "temperature", outputKey: "temperature" },
|
|
413
|
+
{ camelCaseKey: "topP", optionKey: "topP", outputKey: "top_p" },
|
|
414
|
+
{
|
|
415
|
+
camelCaseKey: "frequencyPenalty",
|
|
416
|
+
optionKey: "frequencyPenalty",
|
|
417
|
+
outputKey: "frequency_penalty"
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
camelCaseKey: "presencePenalty",
|
|
421
|
+
optionKey: "presencePenalty",
|
|
422
|
+
outputKey: "presence_penalty"
|
|
423
|
+
},
|
|
424
|
+
{ camelCaseKey: "seed", optionKey: "seed", outputKey: "seed" },
|
|
425
|
+
{ optionKey: "reasoning", outputKey: "reasoning_effort" },
|
|
426
|
+
{ camelCaseKey: "parallel_tool_calls", outputKey: "parallel_tool_calls" }
|
|
427
|
+
];
|
|
428
|
+
async doGenerate(config, settings, options) {
|
|
429
|
+
try {
|
|
430
|
+
if (options.abortSignal?.aborted) throw createAbortError(options.abortSignal.reason);
|
|
431
|
+
const commonParts = this.buildCommonParts(config, settings, options);
|
|
432
|
+
const { request, warnings } = this.buildRequest(config, settings, options, commonParts);
|
|
433
|
+
const client = this.createClient(config, settings, commonParts);
|
|
434
|
+
const response = await this.executeApiCall(
|
|
435
|
+
client,
|
|
436
|
+
request,
|
|
437
|
+
mergeRequestConfig(config.requestConfig, options.abortSignal, options.headers)
|
|
438
|
+
);
|
|
439
|
+
return buildGenerateResult({
|
|
440
|
+
providerName: commonParts.providerName,
|
|
441
|
+
requestBody: request,
|
|
442
|
+
requestId: response.requestId,
|
|
443
|
+
response,
|
|
444
|
+
responseHeaders: normalizeHeaders(response.rawResponse.headers),
|
|
445
|
+
version: VERSION,
|
|
446
|
+
warnings: [...commonParts.warnings, ...warnings]
|
|
447
|
+
});
|
|
448
|
+
} catch (error) {
|
|
449
|
+
throw convertToAISDKError(error, {
|
|
450
|
+
modelId: config.modelId,
|
|
451
|
+
modelType: "languageModel",
|
|
452
|
+
operation: "doGenerate",
|
|
453
|
+
requestBody: createAISDKRequestBodySummary(options),
|
|
454
|
+
url: this.getUrl()
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async doStream(config, settings, options) {
|
|
459
|
+
try {
|
|
460
|
+
if (options.abortSignal?.aborted) throw createAbortError(options.abortSignal.reason);
|
|
461
|
+
const commonParts = this.buildCommonParts(config, settings, options);
|
|
462
|
+
const { request, warnings } = this.buildRequest(config, settings, options, commonParts);
|
|
463
|
+
const client = this.createClient(config, settings, commonParts);
|
|
464
|
+
const streamResponse = await this.executeStreamCall(
|
|
465
|
+
client,
|
|
466
|
+
request,
|
|
467
|
+
options.abortSignal ?? void 0,
|
|
468
|
+
settings,
|
|
469
|
+
mergeRequestConfig(config.requestConfig, void 0, options.headers)
|
|
470
|
+
);
|
|
471
|
+
const idGenerator = new StreamIdGenerator();
|
|
472
|
+
const streamWarnings = this.collectStreamWarnings(settings, commonParts.sapOptions);
|
|
473
|
+
const transformedStream = createStreamTransformer({
|
|
474
|
+
cancel: streamResponse.cancel,
|
|
475
|
+
convertToAISDKError,
|
|
476
|
+
extractChunkMetadata: (chunk) => this.extractMetadata(chunk),
|
|
477
|
+
idGenerator,
|
|
478
|
+
includeRawChunks: options.includeRawChunks ?? false,
|
|
479
|
+
modelId: config.modelId,
|
|
480
|
+
options,
|
|
481
|
+
providerName: commonParts.providerName,
|
|
482
|
+
requestId: streamResponse.requestId,
|
|
483
|
+
responseMetadata: {
|
|
484
|
+
...streamResponse.responseMetadata,
|
|
485
|
+
id: streamResponse.responseMetadata?.id ?? idGenerator.generateResponseId()
|
|
486
|
+
},
|
|
487
|
+
sdkStream: streamResponse.stream,
|
|
488
|
+
streamResponseGetCitations: streamResponse.getCitations,
|
|
489
|
+
streamResponseGetFinishReason: streamResponse.getFinishReason,
|
|
490
|
+
streamResponseGetIntermediateFailures: streamResponse.getIntermediateFailures,
|
|
491
|
+
url: this.getUrl(),
|
|
492
|
+
version: VERSION,
|
|
493
|
+
warnings: [...commonParts.warnings, ...warnings, ...streamWarnings]
|
|
494
|
+
});
|
|
495
|
+
return {
|
|
496
|
+
request: {
|
|
497
|
+
body: request
|
|
498
|
+
},
|
|
499
|
+
response: {
|
|
500
|
+
headers: streamResponse.responseHeaders
|
|
501
|
+
},
|
|
502
|
+
stream: transformedStream
|
|
503
|
+
};
|
|
504
|
+
} catch (error) {
|
|
505
|
+
throw convertToAISDKError(error, {
|
|
506
|
+
modelId: config.modelId,
|
|
507
|
+
modelType: "languageModel",
|
|
508
|
+
operation: "doStream",
|
|
509
|
+
requestBody: createAISDKRequestBodySummary(options),
|
|
510
|
+
url: this.getUrl()
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Builds common parts shared between doGenerate and doStream.
|
|
516
|
+
* @param config - Strategy configuration.
|
|
517
|
+
* @param settings - Model settings.
|
|
518
|
+
* @param options - AI SDK call options.
|
|
519
|
+
* @returns Common build result with typed messages and tool choice.
|
|
520
|
+
* @internal
|
|
521
|
+
*/
|
|
522
|
+
buildCommonParts(config, settings, options) {
|
|
523
|
+
const providerName = getProviderName(config.provider);
|
|
524
|
+
const sapOptions = config.parsedProviderOptions;
|
|
525
|
+
const warnings = [];
|
|
526
|
+
const resolvedState = this.resolveAdditionalState(
|
|
527
|
+
config,
|
|
528
|
+
settings,
|
|
529
|
+
sapOptions,
|
|
530
|
+
options,
|
|
531
|
+
warnings
|
|
532
|
+
);
|
|
533
|
+
const messages = convertToSAPMessages(options.prompt, {
|
|
534
|
+
escapeTemplatePlaceholders: this.getEscapeTemplatePlaceholders(sapOptions, settings),
|
|
535
|
+
includeReasoning: this.getIncludeReasoning(sapOptions, settings),
|
|
536
|
+
parsePartProviderOptions: this.getPartProviderOptionsParser(),
|
|
537
|
+
warnings
|
|
538
|
+
});
|
|
539
|
+
const { modelParams, warnings: paramWarnings } = buildModelParams({
|
|
540
|
+
options,
|
|
541
|
+
paramMappings: this.getParamMappings(),
|
|
542
|
+
providerModelParams: sapOptions?.modelParams,
|
|
543
|
+
settingsModelParams: settings.modelParams
|
|
544
|
+
});
|
|
545
|
+
warnings.push(...paramWarnings);
|
|
546
|
+
const toolChoice = mapToolChoice(options.toolChoice);
|
|
547
|
+
return {
|
|
548
|
+
messages,
|
|
549
|
+
modelParams,
|
|
550
|
+
providerName,
|
|
551
|
+
resolvedState,
|
|
552
|
+
sapOptions,
|
|
553
|
+
toolChoice,
|
|
554
|
+
warnings
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Collects stream-specific warnings.
|
|
559
|
+
* Override in subclasses to add API-specific streaming warnings.
|
|
560
|
+
* @param _settings - Model settings (unused in base implementation).
|
|
561
|
+
* @param _sapOptions - Provider options (unused in base implementation).
|
|
562
|
+
* @returns Array of warnings for streaming operations.
|
|
563
|
+
* @internal
|
|
564
|
+
*/
|
|
565
|
+
collectStreamWarnings(_settings, _sapOptions) {
|
|
566
|
+
return [];
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Resolves completion metadata, request ID, and normalised headers from an SDK response.
|
|
570
|
+
* @param response - Raw SDK response or stream response.
|
|
571
|
+
* @returns Combined metadata fragment.
|
|
572
|
+
* @internal
|
|
573
|
+
*/
|
|
574
|
+
extractMetadata(response) {
|
|
575
|
+
const responseMetadata = extractCompletionMetadata(
|
|
576
|
+
response,
|
|
577
|
+
this.getCompletionDataPath()
|
|
578
|
+
);
|
|
579
|
+
const { headers, requestId } = extractResponseMetadata(response, "rawResponse");
|
|
580
|
+
return { requestId, responseHeaders: headers, responseMetadata };
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Returns whether to escape template placeholders for this API.
|
|
584
|
+
* @param _sapOptions - Parsed provider options (unused in base implementation).
|
|
585
|
+
* @param _settings - Model settings (unused in base implementation).
|
|
586
|
+
* @returns false by default; the Orchestration override defaults to true unless disabled.
|
|
587
|
+
* @internal
|
|
588
|
+
*/
|
|
589
|
+
getEscapeTemplatePlaceholders(_sapOptions, _settings) {
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Returns whether assistant reasoning in the input history is forwarded as text.
|
|
594
|
+
* @param sapOptions - Parsed provider options.
|
|
595
|
+
* @param settings - Model settings.
|
|
596
|
+
* @returns Whether to forward input reasoning (checks sapOptions then settings, defaults to false).
|
|
597
|
+
* @internal
|
|
598
|
+
*/
|
|
599
|
+
getIncludeReasoning(sapOptions, settings) {
|
|
600
|
+
return sapOptions?.includeReasoning ?? settings.includeReasoning ?? false;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Returns a parser for per-message-part `providerOptions['sap-ai']`.
|
|
604
|
+
*
|
|
605
|
+
* Default returns `undefined`; strategies honouring part-level directives
|
|
606
|
+
* (e.g. Anthropic `cacheControl` on orchestration) override to return a parser.
|
|
607
|
+
* @returns Parser callback, or undefined to disable per-part plumbing.
|
|
608
|
+
* @internal
|
|
609
|
+
*/
|
|
610
|
+
getPartProviderOptionsParser() {
|
|
611
|
+
return void 0;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Resolves API-specific extra state shared between doGenerate and doStream
|
|
615
|
+
* (e.g. orchestration `configRef` / `promptTemplateRef` / `tools`). Override
|
|
616
|
+
* to add API-specific deprecation checks or pre-resolve auxiliary inputs.
|
|
617
|
+
* Pushed warnings flow into the shared sink.
|
|
618
|
+
* @param _config - Strategy configuration.
|
|
619
|
+
* @param _settings - Model settings.
|
|
620
|
+
* @param _sapOptions - Parsed provider options, or `undefined`.
|
|
621
|
+
* @param _options - AI SDK call options.
|
|
622
|
+
* @param _warnings - Shared warnings sink for the current call.
|
|
623
|
+
* @returns Strategy-specific resolved state, or `undefined` (default).
|
|
624
|
+
* @internal
|
|
625
|
+
*/
|
|
626
|
+
resolveAdditionalState(_config, _settings, _sapOptions, _options, _warnings) {
|
|
627
|
+
return void 0;
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
export {
|
|
632
|
+
BaseLanguageModelStrategy
|
|
633
|
+
};
|
|
634
|
+
//# sourceMappingURL=chunk-QZOZ3PZT.js.map
|