@ai-sdk/provider-utils 5.0.4 → 5.0.6
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/CHANGELOG.md +14 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +34 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +7 -0
- package/src/streaming-tool-call-tracker.ts +4 -11
- package/src/websocket.ts +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/provider-utils",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@standard-schema/spec": "^1.1.0",
|
|
36
36
|
"@workflow/serde": "4.1.0",
|
|
37
37
|
"eventsource-parser": "^3.0.8",
|
|
38
|
-
"@ai-sdk/provider": "4.0.
|
|
38
|
+
"@ai-sdk/provider": "4.0.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "22.19.19",
|
package/src/index.ts
CHANGED
|
@@ -95,6 +95,13 @@ export * from './uint8-utils';
|
|
|
95
95
|
export { validateDownloadUrl } from './validate-download-url';
|
|
96
96
|
export * from './validate-types';
|
|
97
97
|
export { VERSION } from './version';
|
|
98
|
+
export {
|
|
99
|
+
getWebSocketConstructor,
|
|
100
|
+
readWebSocketMessageText,
|
|
101
|
+
toWebSocketUrl,
|
|
102
|
+
type WebSocketConstructor,
|
|
103
|
+
type WebSocketLike,
|
|
104
|
+
} from './websocket';
|
|
98
105
|
export { withUserAgentSuffix } from './with-user-agent-suffix';
|
|
99
106
|
export * from './without-trailing-slash';
|
|
100
107
|
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
type SharedV4ProviderMetadata,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import { generateId as defaultGenerateId } from './generate-id';
|
|
7
|
-
import { isParsableJson } from './parse-json';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Minimal interface for a streaming tool call delta from an OpenAI-compatible API.
|
|
@@ -188,11 +187,10 @@ export class StreamingToolCallTracker<
|
|
|
188
187
|
});
|
|
189
188
|
}
|
|
190
189
|
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
190
|
+
// Tool calls must not finalize before the stream ends: a parsable
|
|
191
|
+
// argument buffer can still be the prefix of a longer argument string,
|
|
192
|
+
// so acting on it early would use truncated inputs (see #13137).
|
|
193
|
+
// Finalization happens in flush().
|
|
196
194
|
}
|
|
197
195
|
|
|
198
196
|
private processExistingToolCall(index: number, toolCallDelta: DELTA): void {
|
|
@@ -211,11 +209,6 @@ export class StreamingToolCallTracker<
|
|
|
211
209
|
delta: toolCallDelta.function.arguments,
|
|
212
210
|
});
|
|
213
211
|
}
|
|
214
|
-
|
|
215
|
-
// Check if tool call is complete
|
|
216
|
-
if (isParsableJson(toolCall.function.arguments)) {
|
|
217
|
-
this.finishToolCall(toolCall);
|
|
218
|
-
}
|
|
219
212
|
}
|
|
220
213
|
|
|
221
214
|
private finishToolCall(toolCall: TrackedToolCall): void {
|
package/src/websocket.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type WebSocketLike = {
|
|
2
|
+
readyState: number;
|
|
3
|
+
send(data: string | Uint8Array | ArrayBuffer): void;
|
|
4
|
+
close(code?: number, reason?: string): void;
|
|
5
|
+
onopen: ((event: unknown) => void) | null;
|
|
6
|
+
onmessage: ((event: { data: unknown }) => void) | null;
|
|
7
|
+
onerror: ((event: unknown) => void) | null;
|
|
8
|
+
onclose: ((event: unknown) => void) | null;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type WebSocketConstructor = new (
|
|
12
|
+
url: string | URL,
|
|
13
|
+
protocols?: string | string[],
|
|
14
|
+
options?: {
|
|
15
|
+
headers?: Record<string, string | undefined>;
|
|
16
|
+
},
|
|
17
|
+
) => WebSocketLike;
|
|
18
|
+
|
|
19
|
+
export function getWebSocketConstructor(
|
|
20
|
+
webSocket: WebSocketConstructor | undefined,
|
|
21
|
+
): WebSocketConstructor {
|
|
22
|
+
const WebSocketConstructor =
|
|
23
|
+
webSocket ?? (globalThis.WebSocket as unknown as WebSocketConstructor);
|
|
24
|
+
|
|
25
|
+
if (WebSocketConstructor == null) {
|
|
26
|
+
throw new Error('No WebSocket implementation available.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return WebSocketConstructor;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Converts an http(s) URL to the corresponding ws(s) URL.
|
|
34
|
+
*/
|
|
35
|
+
export function toWebSocketUrl(url: string | URL): URL {
|
|
36
|
+
const wsUrl = new URL(url);
|
|
37
|
+
if (wsUrl.protocol === 'http:') {
|
|
38
|
+
wsUrl.protocol = 'ws:';
|
|
39
|
+
} else if (wsUrl.protocol === 'https:') {
|
|
40
|
+
wsUrl.protocol = 'wss:';
|
|
41
|
+
}
|
|
42
|
+
return wsUrl;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const textDecoder = new TextDecoder();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Reads WebSocket message data as text, handling string, binary,
|
|
49
|
+
* and Blob payloads.
|
|
50
|
+
*/
|
|
51
|
+
export async function readWebSocketMessageText(data: unknown): Promise<string> {
|
|
52
|
+
if (typeof data === 'string') return data;
|
|
53
|
+
if (data instanceof ArrayBuffer) return textDecoder.decode(data);
|
|
54
|
+
if (ArrayBuffer.isView(data)) {
|
|
55
|
+
return textDecoder.decode(data);
|
|
56
|
+
}
|
|
57
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) {
|
|
58
|
+
return data.text();
|
|
59
|
+
}
|
|
60
|
+
return String(data);
|
|
61
|
+
}
|