@sap-ai-sdk/orchestration
Version:
SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.
91 lines • 2.5 kB
JavaScript
import { isMessageToolCall } from './internal.js';
/**
* Orchestration stream response.
*/
export class OrchestrationStreamResponse {
_usage;
/**
* Finish reasons for all choices.
*/
_finishReasons = new Map();
_toolCallsAccumulators = new Map();
_stream;
/**
* Gets the token usage for the response.
* @returns The token usage for the response.
*/
getTokenUsage() {
return this._usage;
}
/**
* @internal
*/
_setTokenUsage(usage) {
this._usage = usage;
}
/**
* Gets the finish reason for a specific choice index.
* @param choiceIndex - The index of the choice to get the finish reason for.
* @returns The finish reason for the specified choice index.
*/
getFinishReason(choiceIndex = 0) {
return this._finishReasons.get(choiceIndex);
}
/**
* @internal
*/
_getFinishReasons() {
return this._finishReasons;
}
/**
* @internal
*/
_setFinishReasons(finishReasons) {
this._finishReasons = finishReasons;
}
/**
* Gets the tool calls for a specific choice index.
* @param choiceIndex - The index of the choice to get the tool calls for.
* @returns The tool calls for the specified choice index.
*/
getToolCalls(choiceIndex = 0) {
try {
const toolCallsAccumulators = this._toolCallsAccumulators.get(choiceIndex);
if (!toolCallsAccumulators) {
return undefined;
}
const toolCalls = [];
for (const [id, acc] of toolCallsAccumulators.entries()) {
if (isMessageToolCall(acc)) {
toolCalls.push(acc);
}
else {
throw new Error(`Tool call with id ${id} was incomplete.`);
}
}
return toolCalls;
}
catch (error) {
throw new Error(`Error while getting tool calls for choice index ${choiceIndex}: ${error}`);
}
}
/**
* @internal
*/
_getToolCallsAccumulators() {
return this._toolCallsAccumulators;
}
get stream() {
if (!this._stream) {
throw new Error('Response stream is undefined.');
}
return this._stream;
}
/**
* @internal
*/
set stream(stream) {
this._stream = stream;
}
}
//# sourceMappingURL=orchestration-stream-response.js.map