ai 7.0.181 → 7.0.183
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 +21 -0
- package/dist/{default-stop-condition-D8PwkeWe.js → default-stop-condition-CKhd4H3S.js} +2 -2
- package/dist/{default-stop-condition-D8PwkeWe.js.map → default-stop-condition-CKhd4H3S.js.map} +1 -1
- package/dist/{index-CKEJqCzY.d.ts → index-D1zb0Eh5.d.ts} +1 -2
- package/dist/{index-CKEJqCzY.d.ts.map → index-D1zb0Eh5.d.ts.map} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +51 -23
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +3 -3
- package/docs/03-ai-sdk-harnesses/02-harness-agent.mdx +22 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-events.mdx +3 -2
- package/package.json +11 -11
- package/src/realtime/browser-realtime-audio.ts +76 -17
- package/src/realtime/realtime-session.ts +5 -9
package/dist/internal/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Al as LanguageModelUsage, Ao as ToolApprovalStatus, Ba as GenerateTextOnEndCallback, Bc as Instructions, Ca as TelemetryDispatcher, Dl as Warning, Eo as ResolvedToolCallers, Fc as ToolModelMessage$1, Ga as GenerateTextOnStepFinishCallback, Go as StoppedBy, Gs as DefaultStepResult, Ha as GenerateTextOnStartCallback, Hl as ResponseMessage, Jl as LanguageModel, Jo as InferCompleteOutput, Ka as GenerateTextOnStepStartCallback, Ko as StaticToolOutputDenied, Ml as asLanguageModelUsage, Nl as createNullLanguageModelUsage, Qo as createAsyncIterableStream, Qs as TypedToolResult, So as ToolCallRepairFunction, Tc as AssistantModelMessage$1, Tl as logWarnings, Ua as GenerateTextOnStepEndCallback, Ul as LanguageModelRequestMetadata, Vl as LanguageModelResponseMetadata, Vo as ToolOrder, Wa as GenerateTextOnStepErrorCallback, Xs as DynamicToolResult, Yl as ToolChoice, Ys as ContentPart, Zs as StaticToolResult, _a as LanguageModelStreamPart, _c as Callback, _o as OnToolExecutionEndCallback, ac as StaticToolCall, cc as ReasoningOutput, dc as GeneratedFile, el as LanguageModelCallOptions, fc as IncludedContext, ga as GenerateTextResult, ic as DynamicToolCall, jl as addLanguageModelUsage, ko as ToolApprovalConfiguration, mc as TelemetryOptions, mo as ToolInputRefinement, nc as ToolApprovalResponseOutput, oc as TypedToolCall, pc as IncludedToolsContext, po as ToolsContextSettings, ql as FinishReason, qs as StepResult, rc as ToolApprovalRequestOutput, sc as ReasoningFileOutput, to as Output, vc as StandardizedPrompt, vl as DownloadFunction, vo as OnToolExecutionStartCallback, wl as LogWarningsFunction, wo as InvalidToolInputError, xo as ToolOutput, yc as standardizePrompt, yl as createDefaultDownloadFunction, za as GenerateTextOnAbortCallback } from "../index-
|
|
1
|
+
import { Al as LanguageModelUsage, Ao as ToolApprovalStatus, Ba as GenerateTextOnEndCallback, Bc as Instructions, Ca as TelemetryDispatcher, Dl as Warning, Eo as ResolvedToolCallers, Fc as ToolModelMessage$1, Ga as GenerateTextOnStepFinishCallback, Go as StoppedBy, Gs as DefaultStepResult, Ha as GenerateTextOnStartCallback, Hl as ResponseMessage, Jl as LanguageModel, Jo as InferCompleteOutput, Ka as GenerateTextOnStepStartCallback, Ko as StaticToolOutputDenied, Ml as asLanguageModelUsage, Nl as createNullLanguageModelUsage, Qo as createAsyncIterableStream, Qs as TypedToolResult, So as ToolCallRepairFunction, Tc as AssistantModelMessage$1, Tl as logWarnings, Ua as GenerateTextOnStepEndCallback, Ul as LanguageModelRequestMetadata, Vl as LanguageModelResponseMetadata, Vo as ToolOrder, Wa as GenerateTextOnStepErrorCallback, Xs as DynamicToolResult, Yl as ToolChoice, Ys as ContentPart, Zs as StaticToolResult, _a as LanguageModelStreamPart, _c as Callback, _o as OnToolExecutionEndCallback, ac as StaticToolCall, cc as ReasoningOutput, dc as GeneratedFile, el as LanguageModelCallOptions, fc as IncludedContext, ga as GenerateTextResult, ic as DynamicToolCall, jl as addLanguageModelUsage, ko as ToolApprovalConfiguration, mc as TelemetryOptions, mo as ToolInputRefinement, nc as ToolApprovalResponseOutput, oc as TypedToolCall, pc as IncludedToolsContext, po as ToolsContextSettings, ql as FinishReason, qs as StepResult, rc as ToolApprovalRequestOutput, sc as ReasoningFileOutput, to as Output, vc as StandardizedPrompt, vl as DownloadFunction, vo as OnToolExecutionStartCallback, wl as LogWarningsFunction, wo as InvalidToolInputError, xo as ToolOutput, yc as standardizePrompt, yl as createDefaultDownloadFunction, za as GenerateTextOnAbortCallback } from "../index-D1zb0Eh5.js";
|
|
2
2
|
import { Arrayable, Context, Experimental_SandboxSession, FlexibleSchema, IdGenerator, InferToolSetContext, ModelMessage, RetryFunction, ShouldRetryFunction, Tool, ToolApprovalRequest, ToolApprovalResponse, ToolResultOutput, ToolResultPart, ToolSet, convertAsyncIteratorToReadableStream } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { EmbeddingModelV4, Experimental_DecisionModelV4, Experimental_ExtractionModelV4, Experimental_SpeechTranslationModelV4, Experimental_VideoModelV4, ImageModelV4, LanguageModelV4, LanguageModelV4Content, LanguageModelV4FunctionTool, LanguageModelV4Prompt, LanguageModelV4ProviderTool, LanguageModelV4ToolCall, LanguageModelV4ToolChoice, LanguageModelV4ToolResultOutput, RerankingModelV4, SharedV4Warning, SpeechModelV4, TranscriptionModelV4 } from "@ai-sdk/provider";
|
|
4
4
|
//#region src/prompt/convert-to-language-model-prompt.d.ts
|
package/dist/internal/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { $ as convertToLanguageModelPrompt, B as setAbortTimeout, C as resolveToolApproval, Dt as logWarnings, E as DefaultGenerateTextResult, G as addLanguageModelUsage, H as mergeAbortSignals, J as standardizePrompt, K as asLanguageModelUsage, O as validateToolContext, P as convertLanguageModelContent, Q as createToolModelOutput, R as collectToolApprovals, S as filterIncludedContext, V as prepareRetries, X as prepareToolChoice, Y as prepareTools, Z as prepareLanguageModelCallOptions, _ as createTelemetryDispatcher, a as createAsyncIterableStream, c as validateApprovedToolApprovals, d as verifyToolApprovalSignature, et as downloadAssets, g as DefaultStepResult, h as createRestrictedTelemetryDispatcher, ht as resolveLanguageModel, k as createToolSearchState, n as logDefaultStopConditionWarning, o as mapWithConcurrency, ot as mergeObjects, p as toResponseMessages, q as createNullLanguageModelUsage, r as isOutputChunk, s as prepareToolCallConcurrency, st as createDefaultDownloadFunction, tt as mapToolResultOutput, u as signToolApproval, w as parseToolCall, x as mergeCallbacks, z as calculateTokensPerSecond } from "../default-stop-condition-
|
|
1
|
+
import { $ as convertToLanguageModelPrompt, B as setAbortTimeout, C as resolveToolApproval, Dt as logWarnings, E as DefaultGenerateTextResult, G as addLanguageModelUsage, H as mergeAbortSignals, J as standardizePrompt, K as asLanguageModelUsage, O as validateToolContext, P as convertLanguageModelContent, Q as createToolModelOutput, R as collectToolApprovals, S as filterIncludedContext, V as prepareRetries, X as prepareToolChoice, Y as prepareTools, Z as prepareLanguageModelCallOptions, _ as createTelemetryDispatcher, a as createAsyncIterableStream, c as validateApprovedToolApprovals, d as verifyToolApprovalSignature, et as downloadAssets, g as DefaultStepResult, h as createRestrictedTelemetryDispatcher, ht as resolveLanguageModel, k as createToolSearchState, n as logDefaultStopConditionWarning, o as mapWithConcurrency, ot as mergeObjects, p as toResponseMessages, q as createNullLanguageModelUsage, r as isOutputChunk, s as prepareToolCallConcurrency, st as createDefaultDownloadFunction, tt as mapToolResultOutput, u as signToolApproval, w as parseToolCall, x as mergeCallbacks, z as calculateTokensPerSecond } from "../default-stop-condition-CKhd4H3S.js";
|
|
2
2
|
import { convertAsyncIteratorToReadableStream } from "@ai-sdk/provider-utils";
|
|
3
3
|
export { DefaultGenerateTextResult, DefaultStepResult, addLanguageModelUsage, asLanguageModelUsage, calculateTokensPerSecond, collectToolApprovals, convertAsyncIteratorToReadableStream, convertLanguageModelContent, convertToLanguageModelPrompt, createAsyncIterableStream, createDefaultDownloadFunction, createNullLanguageModelUsage, createRestrictedTelemetryDispatcher, createTelemetryDispatcher, createToolModelOutput, createToolSearchState, downloadAssets, filterIncludedContext, isOutputChunk, logDefaultStopConditionWarning, logWarnings, mapToolResultOutput, mapWithConcurrency, mergeAbortSignals, mergeCallbacks, mergeObjects, parseToolCall, prepareLanguageModelCallOptions as prepareCallSettings, prepareLanguageModelCallOptions, prepareRetries, prepareToolCallConcurrency, prepareToolChoice, prepareTools, resolveLanguageModel, resolveToolApproval, setAbortTimeout, signToolApproval, standardizePrompt, toResponseMessages, validateApprovedToolApprovals, validateToolContext, verifyToolApprovalSignature };
|
|
@@ -133,9 +133,9 @@ Here are the capabilities of popular models:
|
|
|
133
133
|
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-4-fast-reasoning` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
134
134
|
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-4-fast-non-reasoning` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
135
135
|
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-code-fast-1` | <Cross /> | <Check /> | <Check /> | <Check /> |
|
|
136
|
-
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-4` | <
|
|
137
|
-
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-3` | <
|
|
138
|
-
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-3-mini` | <
|
|
136
|
+
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-4` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
137
|
+
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-3` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
138
|
+
| [xAI Grok](/providers/ai-sdk-providers/xai) | `grok-3-mini` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
139
139
|
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-6.2-astra` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
140
140
|
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-6.2-sol` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
141
141
|
| [OpenAI](/providers/ai-sdk-providers/openai) | `gpt-6-astra` | <Check /> | <Check /> | <Check /> | <Check /> |
|
|
@@ -689,6 +689,28 @@ templates. `sandboxId` names the live sandbox, not its reusable template.
|
|
|
689
689
|
The existing native `name` option still names the live sandbox; if both are
|
|
690
690
|
supplied, their values must match.
|
|
691
691
|
|
|
692
|
+
Set `templateName` to choose the name of the reusable template, for example to
|
|
693
|
+
keep production and preview deployments on separate templates, or to list your
|
|
694
|
+
templates by name prefix:
|
|
695
|
+
|
|
696
|
+
```ts
|
|
697
|
+
const template = await agent.getSandboxTemplate();
|
|
698
|
+
|
|
699
|
+
const sandboxSession = await createVercelNetworkSandboxSession({
|
|
700
|
+
runtime: 'node24',
|
|
701
|
+
ports: [4000],
|
|
702
|
+
template,
|
|
703
|
+
templateName: `my-app-${process.env.VERCEL_ENV}-${template?.identity}`,
|
|
704
|
+
});
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
`templateName` replaces the derived name, which changes with the template's
|
|
708
|
+
identity, the sandbox source, and SDK upgrades that invalidate templates. A
|
|
709
|
+
template that already exists under your name is reused as is, so change the name
|
|
710
|
+
when any of those change. The example above covers identity changes. If the
|
|
711
|
+
source can change, add whatever identifies it to the name. Without a template,
|
|
712
|
+
`templateName` has no effect.
|
|
713
|
+
|
|
692
714
|
## Settings
|
|
693
715
|
|
|
694
716
|
`HarnessAgent` accepts these main settings:
|
|
@@ -656,7 +656,8 @@ The listing method takes `{ params, options }`; the other methods include
|
|
|
656
656
|
|
|
657
657
|
## Errors
|
|
658
658
|
|
|
659
|
-
The client rejects requests to servers
|
|
659
|
+
The client rejects requests to servers that declare neither the
|
|
660
|
+
`io.modelcontextprotocol/events` extension nor the legacy `events` capability.
|
|
660
661
|
Subscription lifecycle methods require `experimental_events.store`; refresh and unsubscribe
|
|
661
662
|
also require a known subscription ID or storage key. Subscribe rejects unknown
|
|
662
663
|
event names, unsupported delivery modes, invalid callback URLs, signing secrets,
|
|
@@ -823,7 +824,7 @@ the body before passing it to this handler.
|
|
|
823
824
|
The server must include `webhook-id`, `webhook-timestamp`, `webhook-signature`,
|
|
824
825
|
and `X-MCP-Subscription-Id`. The helper verifies Standard Webhooks v1 HMAC-SHA256
|
|
825
826
|
signatures and accepts signing timestamps within five minutes of the current time.
|
|
826
|
-
An event's
|
|
827
|
+
An event's name must match the subscription.
|
|
827
828
|
|
|
828
829
|
## Event Types
|
|
829
830
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.183",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -43,20 +43,20 @@
|
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@ai-sdk/gateway": "4.0.
|
|
46
|
+
"@ai-sdk/gateway": "4.0.156",
|
|
47
47
|
"@ai-sdk/provider": "4.0.39",
|
|
48
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
48
|
+
"@ai-sdk/provider-utils": "5.0.85"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@ai-sdk/amazon-bedrock": "5.0.
|
|
52
|
-
"@ai-sdk/deepseek": "3.0.
|
|
53
|
-
"@ai-sdk/google": "4.0.
|
|
54
|
-
"@ai-sdk/groq": "4.0.
|
|
55
|
-
"@ai-sdk/huggingface": "2.0.
|
|
56
|
-
"@ai-sdk/moonshotai": "3.0.
|
|
57
|
-
"@ai-sdk/openai": "4.0.
|
|
51
|
+
"@ai-sdk/amazon-bedrock": "5.0.149",
|
|
52
|
+
"@ai-sdk/deepseek": "3.0.91",
|
|
53
|
+
"@ai-sdk/google": "4.0.128",
|
|
54
|
+
"@ai-sdk/groq": "4.0.88",
|
|
55
|
+
"@ai-sdk/huggingface": "2.0.97",
|
|
56
|
+
"@ai-sdk/moonshotai": "3.0.97",
|
|
57
|
+
"@ai-sdk/openai": "4.0.125",
|
|
58
58
|
"@ai-sdk/test-server": "2.0.4",
|
|
59
|
-
"@ai-sdk/xai": "5.0.
|
|
59
|
+
"@ai-sdk/xai": "5.0.52",
|
|
60
60
|
"@edge-runtime/vm": "^5.0.0",
|
|
61
61
|
"@smithy/eventstream-codec": "^4.3.3",
|
|
62
62
|
"@smithy/util-utf8": "^4.3.3",
|
|
@@ -14,6 +14,17 @@ export type BrowserRealtimeAudioOptions = {
|
|
|
14
14
|
onError?: (error: Error) => void;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* One scheduled chunk of output audio. `itemOffset` is how many samples of the
|
|
19
|
+
* same item were scheduled before this chunk.
|
|
20
|
+
*/
|
|
21
|
+
type ScheduledPlayback = {
|
|
22
|
+
itemId: string | undefined;
|
|
23
|
+
startTime: number;
|
|
24
|
+
sampleCount: number;
|
|
25
|
+
itemOffset: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
17
28
|
export class BrowserRealtimeAudio {
|
|
18
29
|
private readonly captureSampleRate: number;
|
|
19
30
|
private readonly playbackSampleRate: number;
|
|
@@ -30,10 +41,13 @@ export class BrowserRealtimeAudio {
|
|
|
30
41
|
private captureGeneration = 0;
|
|
31
42
|
|
|
32
43
|
private playbackContext: AudioContext | null = null;
|
|
33
|
-
private playbackQueue:
|
|
44
|
+
private playbackQueue: Array<{
|
|
45
|
+
samples: Float32Array;
|
|
46
|
+
itemId: string | undefined;
|
|
47
|
+
}> = [];
|
|
34
48
|
private playbackTime = 0;
|
|
35
|
-
private
|
|
36
|
-
private
|
|
49
|
+
private activeSources = new Map<AudioBufferSourceNode, ScheduledPlayback>();
|
|
50
|
+
private scheduledItem: { id: string; samples: number } | undefined;
|
|
37
51
|
private isPlaying = false;
|
|
38
52
|
private playbackPaused = false;
|
|
39
53
|
private readonly maxPlaybackSeconds: number;
|
|
@@ -157,7 +171,7 @@ export class BrowserRealtimeAudio {
|
|
|
157
171
|
this.onCapturingChange(false);
|
|
158
172
|
}
|
|
159
173
|
|
|
160
|
-
playAudio(base64Audio: string): void {
|
|
174
|
+
playAudio(base64Audio: string, itemId?: string): void {
|
|
161
175
|
if (this.playbackPaused) return;
|
|
162
176
|
this.ensurePlaybackContext();
|
|
163
177
|
const samples = decodeRealtimeAudio(base64Audio);
|
|
@@ -178,14 +192,14 @@ export class BrowserRealtimeAudio {
|
|
|
178
192
|
);
|
|
179
193
|
return;
|
|
180
194
|
}
|
|
181
|
-
this.playbackQueue.push(samples);
|
|
195
|
+
this.playbackQueue.push({ samples, itemId });
|
|
182
196
|
this.schedulePlayback();
|
|
183
197
|
}
|
|
184
198
|
|
|
185
199
|
stopPlayback(): void {
|
|
186
200
|
this.playbackQueue = [];
|
|
187
201
|
|
|
188
|
-
for (const source of this.activeSources) {
|
|
202
|
+
for (const source of this.activeSources.keys()) {
|
|
189
203
|
try {
|
|
190
204
|
source.onended = null;
|
|
191
205
|
source.stop();
|
|
@@ -202,10 +216,42 @@ export class BrowserRealtimeAudio {
|
|
|
202
216
|
this.setPlaying(false);
|
|
203
217
|
}
|
|
204
218
|
|
|
205
|
-
|
|
219
|
+
/**
|
|
220
|
+
* The item the user is currently hearing and how far into that item
|
|
221
|
+
* playback is, in whole milliseconds. Queued items play back to back, so
|
|
222
|
+
* this is resolved from the scheduled chunk under the playback clock, not
|
|
223
|
+
* from the most recently received item.
|
|
224
|
+
*/
|
|
225
|
+
getPlaybackPosition(): { itemId: string; audioEndMs: number } | undefined {
|
|
206
226
|
const ctx = this.playbackContext;
|
|
207
|
-
if (ctx == null) return
|
|
208
|
-
|
|
227
|
+
if (ctx == null) return undefined;
|
|
228
|
+
const now = ctx.currentTime;
|
|
229
|
+
|
|
230
|
+
// Chunks are scheduled back to back, in insertion order. Take the latest
|
|
231
|
+
// chunk that has started, or the first pending one if none has (an
|
|
232
|
+
// `ended` event can fire marginally before the next chunk's start time).
|
|
233
|
+
let current: ScheduledPlayback | undefined;
|
|
234
|
+
for (const playback of this.activeSources.values()) {
|
|
235
|
+
if (current != null && playback.startTime > now) break;
|
|
236
|
+
current = playback;
|
|
237
|
+
}
|
|
238
|
+
if (current?.itemId == null) return undefined;
|
|
239
|
+
|
|
240
|
+
const rate = this.playbackSampleRate;
|
|
241
|
+
const played = Math.min(
|
|
242
|
+
Math.max(now - current.startTime, 0),
|
|
243
|
+
current.sampleCount / rate,
|
|
244
|
+
);
|
|
245
|
+
const heardMs = Math.round((current.itemOffset / rate + played) * 1000);
|
|
246
|
+
// Never report more than the item's scheduled audio, rounded down:
|
|
247
|
+
// providers reject a truncation past the end of the item.
|
|
248
|
+
const scheduledMs = Math.floor(
|
|
249
|
+
((current.itemOffset + current.sampleCount) * 1000) / rate,
|
|
250
|
+
);
|
|
251
|
+
return {
|
|
252
|
+
itemId: current.itemId,
|
|
253
|
+
audioEndMs: Math.min(heardMs, scheduledMs),
|
|
254
|
+
};
|
|
209
255
|
}
|
|
210
256
|
|
|
211
257
|
dispose(): void {
|
|
@@ -215,13 +261,11 @@ export class BrowserRealtimeAudio {
|
|
|
215
261
|
void this.playbackContext?.close().catch(() => {});
|
|
216
262
|
this.playbackContext = null;
|
|
217
263
|
this.playbackTime = 0;
|
|
264
|
+
this.scheduledItem = undefined;
|
|
218
265
|
this.playbackPaused = false;
|
|
219
266
|
}
|
|
220
267
|
|
|
221
268
|
private setPlaying(isPlaying: boolean): void {
|
|
222
|
-
if (isPlaying && !this.isPlaying) {
|
|
223
|
-
this.playbackStartTime = this.playbackContext?.currentTime ?? 0;
|
|
224
|
-
}
|
|
225
269
|
if (this.isPlaying !== isPlaying) {
|
|
226
270
|
this.isPlaying = isPlaying;
|
|
227
271
|
this.onPlayingChange(isPlaying);
|
|
@@ -233,7 +277,7 @@ export class BrowserRealtimeAudio {
|
|
|
233
277
|
if (ctx == null || this.playbackQueue.length === 0) return;
|
|
234
278
|
|
|
235
279
|
while (this.playbackQueue.length > 0) {
|
|
236
|
-
const samples = this.playbackQueue.shift()!;
|
|
280
|
+
const { samples, itemId } = this.playbackQueue.shift()!;
|
|
237
281
|
const buffer = ctx.createBuffer(
|
|
238
282
|
1,
|
|
239
283
|
samples.length,
|
|
@@ -249,10 +293,23 @@ export class BrowserRealtimeAudio {
|
|
|
249
293
|
source.start(startTime);
|
|
250
294
|
this.playbackTime = startTime + buffer.duration;
|
|
251
295
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
296
|
+
const itemOffset =
|
|
297
|
+
itemId != null && this.scheduledItem?.id === itemId
|
|
298
|
+
? this.scheduledItem.samples
|
|
299
|
+
: 0;
|
|
300
|
+
if (itemId != null)
|
|
301
|
+
this.scheduledItem = {
|
|
302
|
+
id: itemId,
|
|
303
|
+
samples: itemOffset + samples.length,
|
|
304
|
+
};
|
|
305
|
+
this.activeSources.set(source, {
|
|
306
|
+
itemId,
|
|
307
|
+
startTime,
|
|
308
|
+
sampleCount: samples.length,
|
|
309
|
+
itemOffset,
|
|
310
|
+
});
|
|
311
|
+
// Attach before publishing the playing state: a listener that stops
|
|
312
|
+
// playback must find this source fully set up so it is detached.
|
|
256
313
|
source.onended = () => {
|
|
257
314
|
source.disconnect();
|
|
258
315
|
this.activeSources.delete(source);
|
|
@@ -260,6 +317,8 @@ export class BrowserRealtimeAudio {
|
|
|
260
317
|
this.setPlaying(false);
|
|
261
318
|
}
|
|
262
319
|
};
|
|
320
|
+
this.setPlaying(ctx.state === 'running');
|
|
321
|
+
if (this.playbackContext !== ctx) return;
|
|
263
322
|
}
|
|
264
323
|
}
|
|
265
324
|
}
|
|
@@ -99,7 +99,6 @@ export abstract class AbstractRealtimeSession {
|
|
|
99
99
|
private suppliedStream?: MediaStream;
|
|
100
100
|
private captureGeneration = 0;
|
|
101
101
|
private captureRequested = false;
|
|
102
|
-
private currentResponseItemId: string | null = null;
|
|
103
102
|
private readonly toolCallsInResponse = new Set<string>();
|
|
104
103
|
private readonly submittedToolOutputs = new Set<string>();
|
|
105
104
|
private responseToolCallsClosed = false;
|
|
@@ -224,7 +223,6 @@ export abstract class AbstractRealtimeSession {
|
|
|
224
223
|
}
|
|
225
224
|
if (!current()) return;
|
|
226
225
|
this.reducer = new RealtimeEventReducer(this.maxEvents);
|
|
227
|
-
this.currentResponseItemId = null;
|
|
228
226
|
this.toolCallsInResponse.clear();
|
|
229
227
|
this.submittedToolOutputs.clear();
|
|
230
228
|
this.responseToolCallsClosed = false;
|
|
@@ -1036,21 +1034,19 @@ export abstract class AbstractRealtimeSession {
|
|
|
1036
1034
|
): void {
|
|
1037
1035
|
switch (effect.type) {
|
|
1038
1036
|
case 'play-audio':
|
|
1039
|
-
this.
|
|
1040
|
-
this.audio?.playAudio(effect.delta);
|
|
1037
|
+
this.audio?.playAudio(effect.delta, effect.itemId);
|
|
1041
1038
|
break;
|
|
1042
1039
|
case 'speech-started':
|
|
1043
1040
|
if (!this.continuous && this.state.isPlaying) {
|
|
1044
|
-
const
|
|
1045
|
-
const itemId = this.currentResponseItemId;
|
|
1041
|
+
const position = this.audio?.getPlaybackPosition();
|
|
1046
1042
|
this.audio?.stopPlayback();
|
|
1047
1043
|
if (this.attempt !== attempt || !attempt.active) return;
|
|
1048
|
-
if (
|
|
1044
|
+
if (position != null && !attempt.closing && attempt.cause == null)
|
|
1049
1045
|
this.sendEvent({
|
|
1050
1046
|
type: 'conversation-item-truncate',
|
|
1051
|
-
itemId,
|
|
1047
|
+
itemId: position.itemId,
|
|
1052
1048
|
contentIndex: 0,
|
|
1053
|
-
audioEndMs:
|
|
1049
|
+
audioEndMs: position.audioEndMs,
|
|
1054
1050
|
});
|
|
1055
1051
|
}
|
|
1056
1052
|
break;
|