@sap-ai-sdk/foundation-models 1.5.1-20250112013117.0 → 1.5.1-20250114013043.0
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 +3 -3
- package/dist/azure-openai/azure-openai-chat-completion-stream.d.ts +1 -1
- package/dist/azure-openai/azure-openai-chat-completion-stream.d.ts.map +1 -1
- package/dist/azure-openai/azure-openai-chat-completion-stream.js +1 -1
- package/dist/azure-openai/azure-openai-chat-completion-stream.js.map +1 -1
- package/package.json +3 -3
- package/dist/azure-openai/stream/index.d.ts +0 -2
- package/dist/azure-openai/stream/index.d.ts.map +0 -1
- package/dist/azure-openai/stream/index.js +0 -2
- package/dist/azure-openai/stream/index.js.map +0 -1
- package/dist/azure-openai/stream/line-decoder.d.ts +0 -21
- package/dist/azure-openai/stream/line-decoder.d.ts.map +0 -1
- package/dist/azure-openai/stream/line-decoder.js +0 -81
- package/dist/azure-openai/stream/line-decoder.js.map +0 -1
- package/dist/azure-openai/stream/sse-decoder.d.ts +0 -35
- package/dist/azure-openai/stream/sse-decoder.d.ts.map +0 -1
- package/dist/azure-openai/stream/sse-decoder.js +0 -67
- package/dist/azure-openai/stream/sse-decoder.js.map +0 -1
- package/dist/azure-openai/stream/sse-stream.d.ts +0 -25
- package/dist/azure-openai/stream/sse-stream.d.ts.map +0 -1
- package/dist/azure-openai/stream/sse-stream.js +0 -172
- package/dist/azure-openai/stream/sse-stream.js.map +0 -1
package/README.md
CHANGED
|
@@ -152,7 +152,7 @@ Refer to `AzureOpenAiChatCompletionParameters` interface for other parameters th
|
|
|
152
152
|
The `AzureOpenAiChatClient` supports streaming response for chat completion requests based on the [Server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) standard.
|
|
153
153
|
|
|
154
154
|
Use the `stream()` method to receive a stream of chunk responses from the model.
|
|
155
|
-
After consuming the stream, call the helper methods to get the finish reason and token usage information
|
|
155
|
+
After consuming the stream, call the helper methods to get the finish reason and token usage information.
|
|
156
156
|
|
|
157
157
|
```ts
|
|
158
158
|
const chatClient = new AzureOpenAiChatClient('gpt-4o');
|
|
@@ -178,7 +178,7 @@ console.log(`Token usage: ${JSON.stringify(tokenUsage)}\n`);
|
|
|
178
178
|
|
|
179
179
|
##### Streaming the Delta Content
|
|
180
180
|
|
|
181
|
-
The client provides a helper method to extract
|
|
181
|
+
The client provides a helper method to extract the text chunks as strings:
|
|
182
182
|
|
|
183
183
|
```ts
|
|
184
184
|
for await (const chunk of response.stream.toContentStream()) {
|
|
@@ -198,7 +198,7 @@ Additionally, it can be aborted manually by calling the `stream()` method with a
|
|
|
198
198
|
```ts
|
|
199
199
|
const chatClient = new AzureOpenAiChatClient('gpt-4o');
|
|
200
200
|
const controller = new AbortController();
|
|
201
|
-
const response = await
|
|
201
|
+
const response = await chatClient.stream(
|
|
202
202
|
{
|
|
203
203
|
messages: [
|
|
204
204
|
{
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SseStream } from '
|
|
1
|
+
import { SseStream } from '@sap-ai-sdk/core';
|
|
2
2
|
import { AzureOpenAiChatCompletionStreamChunkResponse } from './azure-openai-chat-completion-stream-chunk-response.js';
|
|
3
3
|
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
|
|
4
4
|
import type { AzureOpenAiChatCompletionStreamResponse } from './azure-openai-chat-completion-stream-response.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-chat-completion-stream.d.ts","sourceRoot":"","sources":["../../src/azure-openai/azure-openai-chat-completion-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"azure-openai-chat-completion-stream.d.ts","sourceRoot":"","sources":["../../src/azure-openai/azure-openai-chat-completion-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,4CAA4C,EAAE,MAAM,yDAAyD,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CAAC,GACtD,+BAA+B,CAAC,OAAO,CAAC;IAapC,eAAe,CACpB,IAAI,EAAE,+BAA+B,CAAC,4CAA4C,CAAC,EACnF,WAAW,CAAC,EAAE,MAAM,GACnB,+BAA+B,CAAC,MAAM,CAAC;CAU3C"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createLogger } from '@sap-cloud-sdk/util';
|
|
2
|
-
import { SseStream } from '
|
|
2
|
+
import { SseStream } from '@sap-ai-sdk/core';
|
|
3
3
|
import { AzureOpenAiChatCompletionStreamChunkResponse } from './azure-openai-chat-completion-stream-chunk-response.js';
|
|
4
4
|
const logger = createLogger({
|
|
5
5
|
package: 'foundation-models',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"azure-openai-chat-completion-stream.js","sourceRoot":"","sources":["../../src/azure-openai/azure-openai-chat-completion-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"azure-openai-chat-completion-stream.js","sourceRoot":"","sources":["../../src/azure-openai/azure-openai-chat-completion-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,4CAA4C,EAAE,MAAM,yDAAyD,CAAC;AAIvH,MAAM,MAAM,GAAG,YAAY,CAAC;IAC1B,OAAO,EAAE,mBAAmB;IAC5B,cAAc,EAAE,qCAAqC;CACtD,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,OAAO,+BAAsC,SAAQ,SAAe;IA8G/D;IA7GT;;;;;OAKG;IACI,MAAM,CAAC,OAAO,CACnB,QAAsB,EACtB,UAA2B;QAE3B,mGAAmG;QACnG,MAAM,MAAM,GAAG,SAAS,CAAC,oBAAoB,CAAM,QAAQ,EAAE,UAAU,CA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AAQ,CAAC,EAC/B,IAAI,CAAC,UAAU,CAChB,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,+BAA+B,CACxC,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,EACrB,IAAI,CAAC,UAAU,CAChB,CAAC;IACJ,CAAC;IAEM,eAAe,CAEpB,WAAoB;QAEpB,OAAO,IAAI,+BAA+B,CACxC,GAAG,EAAE,CACH,+BAA+B,CAAC,qBAAqB,CACnD,IAAI,EACJ,WAAW,CACZ,EACH,IAAI,CAAC,UAAU,CAChB,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sap-ai-sdk/foundation-models",
|
|
3
|
-
"version": "1.5.1-
|
|
3
|
+
"version": "1.5.1-20250114013043.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"@sap-cloud-sdk/http-client": "^3.25.0",
|
|
24
24
|
"@sap-cloud-sdk/util": "^3.25.0",
|
|
25
25
|
"@sap-cloud-sdk/connectivity": "^3.25.0",
|
|
26
|
-
"@sap-ai-sdk/ai-api": "^1.5.1-
|
|
27
|
-
"@sap-ai-sdk/core": "^1.5.1-
|
|
26
|
+
"@sap-ai-sdk/ai-api": "^1.5.1-20250114013043.0",
|
|
27
|
+
"@sap-ai-sdk/core": "^1.5.1-20250114013043.0"
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"compile": "tsc",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/azure-openai/stream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/azure-openai/stream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
|
|
2
|
-
/**
|
|
3
|
-
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
4
|
-
* reading lines from text.
|
|
5
|
-
*
|
|
6
|
-
* Https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258.
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export declare class LineDecoder {
|
|
10
|
-
static NEWLINE_CHARS: Set<string>;
|
|
11
|
-
static NEWLINE_REGEXP: RegExp;
|
|
12
|
-
buffer: string[];
|
|
13
|
-
trailingCR: boolean;
|
|
14
|
-
textDecoder: any;
|
|
15
|
-
constructor();
|
|
16
|
-
decode(chunk: Bytes): string[];
|
|
17
|
-
decodeText(bytes: Bytes): string;
|
|
18
|
-
flush(): string[];
|
|
19
|
-
}
|
|
20
|
-
export {};
|
|
21
|
-
//# sourceMappingURL=line-decoder.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"line-decoder.d.ts","sourceRoot":"","sources":["../../../src/azure-openai/stream/line-decoder.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AAE3E;;;;;;GAMG;AACH,qBAAa,WAAW;IAEtB,MAAM,CAAC,aAAa,cAAyB;IAC7C,MAAM,CAAC,cAAc,SAAkB;IAEvC,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;IACpB,WAAW,EAAE,GAAG,CAAC;;IAOjB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,EAAE;IA4C9B,UAAU,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM;IA2BhC,KAAK,IAAI,MAAM,EAAE;CAUlB"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
3
|
-
* reading lines from text.
|
|
4
|
-
*
|
|
5
|
-
* Https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258.
|
|
6
|
-
* @internal
|
|
7
|
-
*/
|
|
8
|
-
export class LineDecoder {
|
|
9
|
-
// prettier-ignore
|
|
10
|
-
static NEWLINE_CHARS = new Set(['\n', '\r']);
|
|
11
|
-
static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
12
|
-
buffer;
|
|
13
|
-
trailingCR;
|
|
14
|
-
textDecoder; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
|
|
15
|
-
constructor() {
|
|
16
|
-
this.buffer = [];
|
|
17
|
-
this.trailingCR = false;
|
|
18
|
-
}
|
|
19
|
-
decode(chunk) {
|
|
20
|
-
let text = this.decodeText(chunk);
|
|
21
|
-
if (this.trailingCR) {
|
|
22
|
-
text = '\r' + text;
|
|
23
|
-
this.trailingCR = false;
|
|
24
|
-
}
|
|
25
|
-
if (text.endsWith('\r')) {
|
|
26
|
-
this.trailingCR = true;
|
|
27
|
-
text = text.slice(0, -1);
|
|
28
|
-
}
|
|
29
|
-
if (!text) {
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
|
|
33
|
-
let lines = text.split(LineDecoder.NEWLINE_REGEXP);
|
|
34
|
-
// if there is a trailing new line then the last entry will be an empty
|
|
35
|
-
// string which we don't care about
|
|
36
|
-
if (trailingNewline) {
|
|
37
|
-
lines.pop();
|
|
38
|
-
}
|
|
39
|
-
if (lines.length === 1 && !trailingNewline) {
|
|
40
|
-
this.buffer.push(lines[0]);
|
|
41
|
-
return [];
|
|
42
|
-
}
|
|
43
|
-
if (this.buffer.length) {
|
|
44
|
-
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
|
|
45
|
-
this.buffer = [];
|
|
46
|
-
}
|
|
47
|
-
if (!trailingNewline) {
|
|
48
|
-
this.buffer = [lines.pop() || ''];
|
|
49
|
-
}
|
|
50
|
-
return lines;
|
|
51
|
-
}
|
|
52
|
-
decodeText(bytes) {
|
|
53
|
-
if (bytes == null) {
|
|
54
|
-
return '';
|
|
55
|
-
}
|
|
56
|
-
if (typeof bytes === 'string') {
|
|
57
|
-
return bytes;
|
|
58
|
-
}
|
|
59
|
-
// Node:
|
|
60
|
-
if (typeof Buffer !== 'undefined') {
|
|
61
|
-
if (bytes instanceof Buffer) {
|
|
62
|
-
return bytes.toString();
|
|
63
|
-
}
|
|
64
|
-
if (bytes instanceof Uint8Array) {
|
|
65
|
-
return Buffer.from(bytes).toString();
|
|
66
|
-
}
|
|
67
|
-
throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`);
|
|
68
|
-
}
|
|
69
|
-
throw new Error('Unexpected: Buffer is not available as globals. Please report this error.');
|
|
70
|
-
}
|
|
71
|
-
flush() {
|
|
72
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
73
|
-
return [];
|
|
74
|
-
}
|
|
75
|
-
const lines = [this.buffer.join('')];
|
|
76
|
-
this.buffer = [];
|
|
77
|
-
this.trailingCR = false;
|
|
78
|
-
return lines;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
//# sourceMappingURL=line-decoder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"line-decoder.js","sourceRoot":"","sources":["../../../src/azure-openai/stream/line-decoder.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IACtB,kBAAkB;IAClB,MAAM,CAAC,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,E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GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
* Server-Sent Event, see {@link https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events}.
|
|
4
|
-
*/
|
|
5
|
-
export interface ServerSentEvent {
|
|
6
|
-
/**
|
|
7
|
-
* Name of the event if field name is `event`.
|
|
8
|
-
*/
|
|
9
|
-
event: string | null;
|
|
10
|
-
/**
|
|
11
|
-
* Value of the data if field name is `data`.
|
|
12
|
-
*/
|
|
13
|
-
data: string;
|
|
14
|
-
/**
|
|
15
|
-
* Raw string chunks. Each line is either in format `data: ...` or `event: ...`.
|
|
16
|
-
*/
|
|
17
|
-
raw: string[];
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Server-Sent Event decoder.
|
|
21
|
-
* @internal
|
|
22
|
-
*/
|
|
23
|
-
export declare class SSEDecoder {
|
|
24
|
-
private data;
|
|
25
|
-
private event;
|
|
26
|
-
private chunks;
|
|
27
|
-
constructor();
|
|
28
|
-
/**
|
|
29
|
-
* Decode the line into structured server sent event.
|
|
30
|
-
* @param line - Line to decode.
|
|
31
|
-
* @returns Server sent event if the line is empty meaning the end of the received event, or null if there are more lines to come.
|
|
32
|
-
*/
|
|
33
|
-
decode(line: string): ServerSentEvent | null;
|
|
34
|
-
}
|
|
35
|
-
//# sourceMappingURL=sse-decoder.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sse-decoder.d.ts","sourceRoot":"","sources":["../../../src/azure-openai/stream/sse-decoder.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,GAAG,EAAE,MAAM,EAAE,CAAC;CACf;AAED;;;GAGG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,IAAI,CAAW;IACvB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,MAAM,CAAW;;IAQzB;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI;CA2C7C"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Server-Sent Event decoder.
|
|
3
|
-
* @internal
|
|
4
|
-
*/
|
|
5
|
-
export class SSEDecoder {
|
|
6
|
-
data;
|
|
7
|
-
event;
|
|
8
|
-
chunks;
|
|
9
|
-
constructor() {
|
|
10
|
-
this.event = null;
|
|
11
|
-
this.data = [];
|
|
12
|
-
this.chunks = [];
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Decode the line into structured server sent event.
|
|
16
|
-
* @param line - Line to decode.
|
|
17
|
-
* @returns Server sent event if the line is empty meaning the end of the received event, or null if there are more lines to come.
|
|
18
|
-
*/
|
|
19
|
-
decode(line) {
|
|
20
|
-
if (line.endsWith('\r')) {
|
|
21
|
-
line = line.substring(0, line.length - 1);
|
|
22
|
-
}
|
|
23
|
-
if (!line) {
|
|
24
|
-
// empty line and we didn't previously encounter any messages
|
|
25
|
-
if (!this.event && !this.data.length) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
const sse = {
|
|
29
|
-
event: this.event,
|
|
30
|
-
data: this.data.join('\n'),
|
|
31
|
-
raw: this.chunks
|
|
32
|
-
};
|
|
33
|
-
this.event = null;
|
|
34
|
-
this.data = [];
|
|
35
|
-
this.chunks = [];
|
|
36
|
-
return sse;
|
|
37
|
-
}
|
|
38
|
-
this.chunks.push(line);
|
|
39
|
-
if (line.startsWith(':')) {
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
const [fieldname, _, value] = partition(line, ':');
|
|
43
|
-
const trimedValue = value.startsWith(' ') ? value.substring(1) : value;
|
|
44
|
-
if (fieldname === 'event') {
|
|
45
|
-
this.event = trimedValue;
|
|
46
|
-
}
|
|
47
|
-
else if (fieldname === 'data') {
|
|
48
|
-
this.data.push(trimedValue);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
throw new Error(`Invalid SSE payload: ${line}`);
|
|
52
|
-
}
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function partition(str, delimiter) {
|
|
57
|
-
const index = str.indexOf(delimiter);
|
|
58
|
-
if (index !== -1) {
|
|
59
|
-
return [
|
|
60
|
-
str.substring(0, index),
|
|
61
|
-
delimiter,
|
|
62
|
-
str.substring(index + delimiter.length)
|
|
63
|
-
];
|
|
64
|
-
}
|
|
65
|
-
return [str, '', ''];
|
|
66
|
-
}
|
|
67
|
-
//# sourceMappingURL=sse-decoder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sse-decoder.js","sourceRoot":"","sources":["../../../src/azure-openai/stream/sse-decoder.ts"],"names":[],"mappings":"AAmBA;;;GAGG;AACH,MAAM,OAAO,UAAU;IACb,IAAI,CAAW;IACf,KAAK,CAAgB;IACrB,MAAM,CAAW;IAEzB;QACE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,IAAY;QACjB,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACxB,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC5C,CAAC;QAED,IAAI,CAAC,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AC;CACF;AAED,SAAS,SAAS,CAAC,GAAW,EAAE,SAAiB;IAC/C,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACrC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,OAAO;YACL,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC;YACvB,SAAS;YACT,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC;SACxC,CAAC;IACJ,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;AACvB,CAAC"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ServerSentEvent } from './sse-decoder.js';
|
|
2
|
-
import type { HttpResponse } from '@sap-cloud-sdk/http-client';
|
|
3
|
-
/**
|
|
4
|
-
* Stream implemented as an async iterable.
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
export declare class SseStream<Item> implements AsyncIterable<Item> {
|
|
8
|
-
iterator: () => AsyncIterator<Item>;
|
|
9
|
-
protected static transformToSseStream<Item>(response: HttpResponse, controller: AbortController): SseStream<Item>;
|
|
10
|
-
controller: AbortController;
|
|
11
|
-
constructor(iterator: () => AsyncIterator<Item>, controller: AbortController);
|
|
12
|
-
[Symbol.asyncIterator](): AsyncIterator<Item>;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
export declare function _iterSseMessages(response: HttpResponse, controller: AbortController): AsyncGenerator<ServerSentEvent, void, unknown>;
|
|
18
|
-
/**
|
|
19
|
-
* This is an internal helper function that's just used for testing.
|
|
20
|
-
* @param chunks - The chunks to decode.
|
|
21
|
-
* @returns The decoded lines.
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
export declare function _decodeChunks(chunks: string[]): string[];
|
|
25
|
-
//# sourceMappingURL=sse-stream.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sse-stream.d.ts","sourceRoot":"","sources":["../../../src/azure-openai/stream/sse-stream.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAS/D;;;GAGG;AACH,qBAAa,SAAS,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;IA6DhD,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC;IA5D5C,SAAS,CAAC,MAAM,CAAC,oBAAoB,CAAC,IAAI,EACxC,QAAQ,EAAE,YAAY,EACtB,UAAU,EAAE,eAAe,GAC1B,SAAS,CAAC,IAAI,CAAC;IAsDlB,UAAU,EAAE,eAAe,CAAC;gBAGnB,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAC1C,UAAU,EAAE,eAAe;IAK7B,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;CAG9C;AAED;;GAEG;AACH,wBAAuB,gBAAgB,CACrC,QAAQ,EAAE,YAAY,EACtB,UAAU,EAAE,eAAe,GAC1B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,OAAO,CAAC,CAyBhD;AA0ED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAQxD"}
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { createLogger } from '@sap-cloud-sdk/util';
|
|
2
|
-
import { LineDecoder } from './line-decoder.js';
|
|
3
|
-
import { SSEDecoder } from './sse-decoder.js';
|
|
4
|
-
const logger = createLogger({
|
|
5
|
-
package: 'foundation-models',
|
|
6
|
-
messageContext: 'azure-openai-sse-stream'
|
|
7
|
-
});
|
|
8
|
-
/**
|
|
9
|
-
* Stream implemented as an async iterable.
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
export class SseStream {
|
|
13
|
-
iterator;
|
|
14
|
-
static transformToSseStream(response, controller) {
|
|
15
|
-
let consumed = false;
|
|
16
|
-
async function* iterator() {
|
|
17
|
-
if (consumed) {
|
|
18
|
-
throw new Error('Cannot iterate over a consumed stream.');
|
|
19
|
-
}
|
|
20
|
-
consumed = true;
|
|
21
|
-
let done = false;
|
|
22
|
-
try {
|
|
23
|
-
for await (const sse of _iterSseMessages(response, controller)) {
|
|
24
|
-
if (done) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
if (sse.data.startsWith('[DONE]')) {
|
|
28
|
-
done = true;
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
const data = JSON.parse(sse.data);
|
|
33
|
-
if (data?.error) {
|
|
34
|
-
throw new Error(data.error);
|
|
35
|
-
}
|
|
36
|
-
// Yield also the event if it exists, otherwise just the data
|
|
37
|
-
yield sse.event === null
|
|
38
|
-
? data
|
|
39
|
-
: { event: sse.event, data };
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
logger.error(`Could not parse message into JSON: ${sse.data}`);
|
|
43
|
-
logger.error(`From chunk: ${sse.raw}`);
|
|
44
|
-
throw e;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
done = true;
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
// Ignore the error if controller was aborted
|
|
51
|
-
if (e instanceof Error && e.name === 'CanceledError') {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
logger.error('Error while iterating over SSE stream:', e);
|
|
55
|
-
}
|
|
56
|
-
finally {
|
|
57
|
-
// Make sure that the controller is aborted if the stream was not fully consumed
|
|
58
|
-
if (!done) {
|
|
59
|
-
controller.abort();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return new SseStream(iterator, controller);
|
|
64
|
-
}
|
|
65
|
-
controller;
|
|
66
|
-
constructor(iterator, controller) {
|
|
67
|
-
this.iterator = iterator;
|
|
68
|
-
this.controller = controller;
|
|
69
|
-
}
|
|
70
|
-
[Symbol.asyncIterator]() {
|
|
71
|
-
return this.iterator();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* @internal
|
|
76
|
-
*/
|
|
77
|
-
export async function* _iterSseMessages(response, controller) {
|
|
78
|
-
if (!response.data) {
|
|
79
|
-
controller.abort();
|
|
80
|
-
throw new Error('Attempted to iterate over a response with no body');
|
|
81
|
-
}
|
|
82
|
-
const sseDecoder = new SSEDecoder();
|
|
83
|
-
const lineDecoder = new LineDecoder();
|
|
84
|
-
const iter = response.data;
|
|
85
|
-
for await (const sseChunk of iterSseChunks(iter)) {
|
|
86
|
-
for (const line of lineDecoder.decode(sseChunk)) {
|
|
87
|
-
const sse = sseDecoder.decode(line);
|
|
88
|
-
if (sse) {
|
|
89
|
-
yield sse;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
for (const line of lineDecoder.flush()) {
|
|
94
|
-
const sse = sseDecoder.decode(line);
|
|
95
|
-
if (sse) {
|
|
96
|
-
yield sse;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Given an async iterable iterator, iterates over it and yields full
|
|
102
|
-
* SSE chunks, i.e. yields when a double new-line is encountered.
|
|
103
|
-
* @param iterator - Async iterable iterator.
|
|
104
|
-
* @returns Async generator of Uint8Array.
|
|
105
|
-
* @internal
|
|
106
|
-
*/
|
|
107
|
-
async function* iterSseChunks(iterator) {
|
|
108
|
-
let data = new Uint8Array();
|
|
109
|
-
for await (const chunk of iterator) {
|
|
110
|
-
if (chunk == null) {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const binaryChunk = chunk instanceof ArrayBuffer
|
|
114
|
-
? new Uint8Array(chunk)
|
|
115
|
-
: typeof chunk === 'string'
|
|
116
|
-
? new TextEncoder().encode(chunk)
|
|
117
|
-
: chunk;
|
|
118
|
-
const newData = new Uint8Array(data.length + binaryChunk.length);
|
|
119
|
-
newData.set(data);
|
|
120
|
-
newData.set(binaryChunk, data.length);
|
|
121
|
-
data = newData;
|
|
122
|
-
let patternIndex;
|
|
123
|
-
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
124
|
-
yield data.slice(0, patternIndex);
|
|
125
|
-
data = data.slice(patternIndex);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (data.length) {
|
|
129
|
-
yield data;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
function findDoubleNewlineIndex(buffer) {
|
|
133
|
-
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
134
|
-
// and returns the index right after the first occurrence of any pattern,
|
|
135
|
-
// or -1 if none of the patterns are found.
|
|
136
|
-
const newline = 0x0a; // \n
|
|
137
|
-
const carriage = 0x0d; // \r
|
|
138
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
139
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
140
|
-
// \n\n
|
|
141
|
-
return i + 2;
|
|
142
|
-
}
|
|
143
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
144
|
-
// \r\r
|
|
145
|
-
return i + 2;
|
|
146
|
-
}
|
|
147
|
-
if (buffer[i] === carriage &&
|
|
148
|
-
buffer[i + 1] === newline &&
|
|
149
|
-
i + 3 < buffer.length &&
|
|
150
|
-
buffer[i + 2] === carriage &&
|
|
151
|
-
buffer[i + 3] === newline) {
|
|
152
|
-
// \r\n\r\n
|
|
153
|
-
return i + 4;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return -1;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* This is an internal helper function that's just used for testing.
|
|
160
|
-
* @param chunks - The chunks to decode.
|
|
161
|
-
* @returns The decoded lines.
|
|
162
|
-
* @internal
|
|
163
|
-
*/
|
|
164
|
-
export function _decodeChunks(chunks) {
|
|
165
|
-
const decoder = new LineDecoder();
|
|
166
|
-
const lines = [];
|
|
167
|
-
for (const chunk of chunks) {
|
|
168
|
-
lines.push(...decoder.decode(chunk));
|
|
169
|
-
}
|
|
170
|
-
return lines;
|
|
171
|
-
}
|
|
172
|
-
//# sourceMappingURL=sse-stream.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sse-stream.js","sourceRoot":"","sources":["../../../src/azure-openai/stream/sse-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAI9C,MAAM,MAAM,GAAG,YAAY,CAAC;IAC1B,OAAO,EAAE,mBAAmB;IAC5B,cAAc,EAAE,yBAAyB;CAC1C,CAAC,CAAC;AAIH;;;GAGG;AACH,MAAM,OAAO,SAAS;IA6DX;IA5DC,MAAM,CAAC,oBAAoB,CACnC,QAAsB,EACtB,UAA2B;QAE3B,IAAI,QAAQ,GAAG,KAAK,CAAC;QAErB,KAAK,SAAS,CAAC,CAAC,QAAQ;YACtB,IAAI,QAAQ,EAAE,CAAC;gBACb,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;YAC5D,CAAC;YACD,QAAQ,GAAG,IAAI,CAAC;YAChB,IAAI,IAAI,GAAG,KAAK,CAAC;YAEjB,IAAI,CAAC;gBACH,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,gBAAgB,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE,CAAC;oBAC/D,IAAI,IAAI,EAAE,CAAC;wBACT,SAAS;oBACX,CAAC;oBAED,IAAI,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAClC,IAAI,GAAG,IAAI,CAAC;wBACZ,SAAS;o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