@proteos/sdk 0.86.0 → 0.87.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.
@@ -14,7 +14,7 @@
14
14
  */
15
15
 
16
16
  import type { AuditFields, ListOptions, UserRef } from '../types/common.js'
17
- import type { ToolKind } from './types.js'
17
+ import type { AgentRuntime, ToolKind } from './types.js'
18
18
 
19
19
  export type { ToolKind } from './types.js'
20
20
 
@@ -60,6 +60,15 @@ export interface Session extends AuditFields {
60
60
  org_id: string
61
61
  id: string
62
62
  agent_key: string
63
+ /** What runs this session: the agent's runtime when the session was created. */
64
+ runtime: AgentRuntime
65
+ /**
66
+ * Set on a delegated session: the session whose agent handed this one its work
67
+ * with the `delegate` tool, and whose log gets a `delegated_session.updated`
68
+ * event when a turn of this one ends. Null for a session a person or a surface
69
+ * opened.
70
+ */
71
+ parent_session_id: string | null
63
72
  title: string
64
73
  status: SessionStatus
65
74
  usage: SessionUsage
@@ -75,8 +84,8 @@ export interface Session extends AuditFields {
75
84
 
76
85
  /**
77
86
  * The discriminant of a {@link SessionEvent} — it selects which payload shape the
78
- * event carries. v1 carries WHOLE events only; the delta/chunk variants
79
- * (`agent.message.delta`, …) are intentionally absent.
87
+ * event carries. These are the WHOLE events of the stored log; the chunk frames
88
+ * of an event still being written are {@link DeltaEventType}.
80
89
  */
81
90
  export type EventType =
82
91
  // User-originated (posted via the API or echoed back by the provider).
@@ -84,8 +93,12 @@ export type EventType =
84
93
  | 'user.interrupt'
85
94
  | 'user.tool_confirmation'
86
95
  | 'user.tool_result'
96
+ // Voice: what was said aloud, and the voice assistant handing a request over.
97
+ | 'user.speech'
98
+ | 'user.voice_request'
87
99
  // Agent-originated.
88
100
  | 'agent.message'
101
+ | 'agent.speech'
89
102
  | 'agent.reasoning'
90
103
  | 'agent.tool_use'
91
104
  | 'agent.tool_result'
@@ -98,11 +111,18 @@ export type EventType =
98
111
  | 'session.status_ended'
99
112
  | 'session.error'
100
113
  | 'session.updated'
114
+ | 'session.voice_started'
115
+ | 'session.voice_ended'
116
+ // A report on a session this one delegated to.
117
+ | 'delegated_session.updated'
101
118
  // Span (observability).
102
119
  | 'span.model_request_start'
103
120
  | 'span.model_request_end'
104
121
 
105
- /** The client-postable subset (everything else is produced by the runtime). */
122
+ /**
123
+ * The client-postable subset (everything else is produced by the runtime). The
124
+ * voice events are written by the voice routes, never posted as events.
125
+ */
106
126
  export type ClientEventType =
107
127
  | 'user.message'
108
128
  | 'user.interrupt'
@@ -159,8 +179,10 @@ export interface ResultBlock {
159
179
  * A user message body (text + file blocks). `context` is what the launching
160
180
  * surface hands the model alongside the message (a call's summary and linked
161
181
  * records, say): stored with the event, sent to the model as an extra text
162
- * block, never rendered as part of the person's message. Not for secrets —
163
- * the session owner can read it back through the events API.
182
+ * block, never rendered as part of the person's message. On the first message
183
+ * of a session whose agent has memory, the service appends the owner's memory
184
+ * block to it. Not for secrets: everyone who can read the session reads it back
185
+ * through the events API.
164
186
  */
165
187
  export interface UserMessagePayload {
166
188
  content: ContentBlock[]
@@ -183,11 +205,13 @@ export interface AgentArtifactsPayload {
183
205
 
184
206
  /**
185
207
  * An agent reasoning ("thinking") block. v1 whole-events carry no text/signature
186
- * from the provider, so both are typically empty.
208
+ * from the provider, so both are typically empty. `signature` is opaque: it
209
+ * means something only to the model `provider` that wrote the block.
187
210
  */
188
211
  export interface ReasoningPayload {
189
212
  text: string
190
213
  signature?: string
214
+ provider?: string
191
215
  }
192
216
 
193
217
  /**
@@ -199,9 +223,19 @@ export interface ToolUsePayload {
199
223
  id: string
200
224
  name: string
201
225
  input: unknown
202
- tool_kind: ToolKind
226
+ tool_kind: SessionToolKind
203
227
  }
204
228
 
229
+ /**
230
+ * Where a tool a session's agent called is run. Besides the kinds a registered
231
+ * tool can have ({@link ToolKind}), a session whose agent can delegate has the
232
+ * `delegation` tools (`delegate`, `list_delegated_sessions`, …), which the agent
233
+ * service runs itself and which cannot be registered. Every agent also has the
234
+ * `system` tools (`new_phase`), which the agent service answers itself and which
235
+ * cannot be registered either.
236
+ */
237
+ export type SessionToolKind = ToolKind | 'delegation' | 'system'
238
+
205
239
  /** The result of a tool invocation, linked to its use by `tool_use_id`. */
206
240
  export interface ToolResultPayload {
207
241
  tool_use_id: string
@@ -217,9 +251,15 @@ export interface ToolConfirmationPayload {
217
251
  decision: string
218
252
  }
219
253
 
220
- /** Announces that earlier context was summarized. */
254
+ /**
255
+ * Announces that earlier context was summarized. On the embedded runtime the
256
+ * agent continues from `summary` in place of the events up to and including seq
257
+ * `through_seq`; the events themselves stay in the log. A durable session's
258
+ * provider compacts on its own and sets neither.
259
+ */
221
260
  export interface ContextCompactedPayload {
222
261
  summary: string
262
+ through_seq?: number
223
263
  }
224
264
 
225
265
  /** A session-level error. */
@@ -230,7 +270,9 @@ export interface ErrorPayload {
230
270
 
231
271
  /**
232
272
  * Why a turn went idle. `type` is an open string ("turn_ended" |
233
- * "user_action_required" | …) so new provider reasons don't break decoding.
273
+ * "user_action_required" | "budget_reached" | …) so new provider reasons don't
274
+ * break decoding. "budget_reached": the session used up the spend ceiling it was
275
+ * created with; a message sent to it is refused (409 `session_budget_reached`).
234
276
  */
235
277
  export interface StopReason {
236
278
  type: string
@@ -260,6 +302,92 @@ export interface SessionUpdatedPayload {
260
302
  title?: string
261
303
  }
262
304
 
305
+ /** A tool call a session is waiting on: what it asks, for whoever answers it. */
306
+ export interface AwaitingToolUse {
307
+ tool_use_id: string
308
+ name: string
309
+ input: unknown
310
+ }
311
+
312
+ /**
313
+ * Where a delegated session stands at the end of one of its turns, written to the
314
+ * log of the session that delegated to it (see {@link Session.parent_session_id}).
315
+ *
316
+ * `status` is the delegated session's status at that moment. `stop_reason` is set
317
+ * only with status `idle`: `turn_ended`, or `user_action_required` when it waits
318
+ * for an answer, in which case `awaiting_tool_uses` lists the calls it waits on.
319
+ * `summary` is the last thing its agent wrote, cut to a bound; the whole
320
+ * conversation is in the delegated session. `is_stopped_by_parent` is true when
321
+ * the delegating agent interrupted the turn itself. `is_handed_back` is true when
322
+ * the person was talking to the delegated agent in its session and that agent
323
+ * handed them back (the `hand_back_session` client tool); `summary` is then what
324
+ * the agent gave the delegating agent with that call, when it gave one.
325
+ */
326
+ export interface DelegatedSessionUpdatedPayload {
327
+ session_id: string
328
+ agent_key: string
329
+ title: string
330
+ status: SessionStatus
331
+ stop_reason?: StopReason
332
+ awaiting_tool_uses?: AwaitingToolUse[]
333
+ is_stopped_by_parent?: boolean
334
+ is_handed_back?: boolean
335
+ summary: string
336
+ }
337
+
338
+ /**
339
+ * One utterance of a voice session, of the person (`user.speech`) or of the voice
340
+ * assistant (`agent.speech`): the text as the voice provider transcribed it, and
341
+ * when it was said, in milliseconds from the start of the voice session. Speech
342
+ * starts no turn; the agent reads it with what opens the next one.
343
+ */
344
+ export interface SpeechPayload {
345
+ voice_session_id: string
346
+ text: string
347
+ start_ms: number
348
+ end_ms: number
349
+ }
350
+
351
+ /**
352
+ * The voice assistant handing what the person asked for to the agent: it starts a
353
+ * turn, or is queued behind the one that runs, like a `user.message`. The request
354
+ * itself is the speech before the event.
355
+ */
356
+ export interface VoiceRequestPayload {
357
+ voice_session_id: string
358
+ /** The voice model's delegation the request is of, as the page reported it.
359
+ * The page speaks the turn's answer as that delegation's result. */
360
+ delegation_id?: string
361
+ /** Model-only text the page sent with the request; never rendered. */
362
+ context?: string
363
+ }
364
+
365
+ /** Voice was entered on the session. */
366
+ export interface VoiceStartedPayload {
367
+ voice_session_id: string
368
+ }
369
+
370
+ /** Why a voice session ended. `superseded` is never reported by a page. */
371
+ export type VoiceEndReason =
372
+ | 'user'
373
+ | 'idle'
374
+ | 'max_duration'
375
+ | 'session_ended'
376
+ | 'browser_left'
377
+ | 'provider_closed'
378
+ | 'superseded'
379
+
380
+ /**
381
+ * Voice was left: why, and how long the voice session lasted: what the voice
382
+ * provider says, or the time since voice was entered when the page went away
383
+ * without it (null when nobody learned it).
384
+ */
385
+ export interface VoiceEndedPayload {
386
+ voice_session_id: string
387
+ end_reason: VoiceEndReason
388
+ duration_seconds: number | null
389
+ }
390
+
263
391
  /** Opens a model-request span. */
264
392
  export interface ModelRequestStartPayload {
265
393
  model_id: string
@@ -278,9 +406,18 @@ export interface ModelRequestEndPayload {
278
406
  model_usage: ModelUsage
279
407
  }
280
408
 
409
+ /**
410
+ * Opens a turn. `acting_user_id` is the person the turn's tools run as, which is
411
+ * not always the author of the message that opened it (a message an agent posts
412
+ * to a session it delegated to). Absent on turns started before it was recorded.
413
+ */
414
+ export interface SessionRunningPayload {
415
+ acting_user_id?: string
416
+ }
417
+
281
418
  /**
282
419
  * The body of events that carry no fields (`user.interrupt`,
283
- * `session.status_running`, `session.status_terminated`, `session.status_ended`).
420
+ * `session.status_terminated`, `session.status_ended`).
284
421
  */
285
422
  export type EmptyPayload = Record<string, never>
286
423
 
@@ -315,18 +452,24 @@ export type SessionEvent = SessionEventEnvelope &
315
452
  | { type: 'user.interrupt'; payload: EmptyPayload }
316
453
  | { type: 'user.tool_confirmation'; payload: ToolConfirmationPayload }
317
454
  | { type: 'user.tool_result'; payload: ToolResultPayload }
455
+ | { type: 'user.speech'; payload: SpeechPayload }
456
+ | { type: 'user.voice_request'; payload: VoiceRequestPayload }
318
457
  | { type: 'agent.message'; payload: AgentMessagePayload }
458
+ | { type: 'agent.speech'; payload: SpeechPayload }
319
459
  | { type: 'agent.reasoning'; payload: ReasoningPayload }
320
460
  | { type: 'agent.tool_use'; payload: ToolUsePayload }
321
461
  | { type: 'agent.tool_result'; payload: ToolResultPayload }
322
462
  | { type: 'agent.context_compacted'; payload: ContextCompactedPayload }
323
463
  | { type: 'agent.artifacts'; payload: AgentArtifactsPayload }
324
- | { type: 'session.status_running'; payload: EmptyPayload }
464
+ | { type: 'session.status_running'; payload: SessionRunningPayload }
325
465
  | { type: 'session.status_idle'; payload: SessionIdlePayload }
326
466
  | { type: 'session.status_terminated'; payload: EmptyPayload }
327
467
  | { type: 'session.status_ended'; payload: EmptyPayload }
328
468
  | { type: 'session.error'; payload: ErrorPayload }
329
469
  | { type: 'session.updated'; payload: SessionUpdatedPayload }
470
+ | { type: 'session.voice_started'; payload: VoiceStartedPayload }
471
+ | { type: 'session.voice_ended'; payload: VoiceEndedPayload }
472
+ | { type: 'delegated_session.updated'; payload: DelegatedSessionUpdatedPayload }
330
473
  | { type: 'span.model_request_start'; payload: ModelRequestStartPayload }
331
474
  | { type: 'span.model_request_end'; payload: ModelRequestEndPayload }
332
475
  )
@@ -395,6 +538,103 @@ export interface AppendEventRequest {
395
538
  * Omitted, the session's tools are left as they are.
396
539
  */
397
540
  client_tools?: ClientToolSchema[]
541
+ /**
542
+ * Refuse a `user.message` posted while a turn runs with 409 `session_running`
543
+ * instead of queueing it. Queueing is the default: the running turn reads the
544
+ * message with its next tool results where the runtime allows, and it opens the
545
+ * session's next turn otherwise. For a caller that pairs each message with the
546
+ * turn it starts and would rather retry later.
547
+ */
548
+ reject_if_running?: boolean
549
+ }
550
+
551
+ // ---------------------------------------------------------------------------
552
+ // Voice
553
+ // ---------------------------------------------------------------------------
554
+
555
+ /**
556
+ * One time a person talked to a session's agent through the voice assistant. The
557
+ * assistant listens and speaks; what was said is in the session's event log
558
+ * (`user.speech`, `agent.speech`) and everything asked for runs as turns of the
559
+ * session. A session has at most one active voice session.
560
+ */
561
+ export interface VoiceSession {
562
+ id: string
563
+ org_id: string
564
+ session_id: string
565
+ status: 'active' | 'ended'
566
+ /** Null while it is active. */
567
+ end_reason: VoiceEndReason | null
568
+ /** How long it lasted: what the voice provider says, or the time since voice
569
+ * was entered when the page went away without it. Null when not known. */
570
+ duration_seconds: number | null
571
+ created_by: UserRef
572
+ created_at: string
573
+ ended_at: string | null
574
+ }
575
+
576
+ /** Enters voice on a session with the WebRTC offer of the page's peer connection. */
577
+ export interface StartVoiceRequest {
578
+ sdp_offer: string
579
+ }
580
+
581
+ /**
582
+ * The voice session that started, the voice provider's answer to the page's offer,
583
+ * and the limits the page enforces by leaving voice.
584
+ */
585
+ export interface StartVoiceResponse {
586
+ voice_session: VoiceSession
587
+ sdp_answer: string
588
+ /** How long nobody may speak before the page leaves voice. */
589
+ idle_timeout_seconds: number
590
+ /** How long a voice session may last at most. */
591
+ max_duration_seconds: number
592
+ }
593
+
594
+ /** One finished utterance as the page heard it from the voice provider. */
595
+ export interface SpeechSegment {
596
+ speaker: 'user' | 'agent'
597
+ text: string
598
+ /** Milliseconds from the start of the voice session. */
599
+ start_ms: number
600
+ end_ms: number
601
+ }
602
+
603
+ /**
604
+ * Reports what was said in the session's active voice session since the last
605
+ * report, in the order said. `starts_turn` says the voice assistant handed a
606
+ * request over: after the segments a `user.voice_request` starts a turn, or is
607
+ * queued behind the one that runs. `client_tools` is honoured with `starts_turn`
608
+ * only (see {@link AppendEventRequest.client_tools}). `voice_session_id` is the
609
+ * voice session the page started: a report for one that was replaced since (the
610
+ * same person entered voice again, in another tab) is refused with 409
611
+ * `voice_session_not_active`.
612
+ */
613
+ export interface AppendSpeechRequest {
614
+ voice_session_id: string
615
+ segments: SpeechSegment[]
616
+ starts_turn?: boolean
617
+ /** The voice model's delegation the request is of, with `starts_turn` only.
618
+ * A request is written once per delegation, however often it is reported. */
619
+ delegation_id?: string
620
+ client_tools?: ClientToolSchema[]
621
+ /** Model-only text for the request, with `starts_turn` only (see
622
+ * {@link UserMessagePayload.context}). */
623
+ context?: string
624
+ }
625
+
626
+ /**
627
+ * Leaves voice: why, and how long the voice provider says the session lasted (it
628
+ * tells the page when the session closes). `voice_session_id` is the voice
629
+ * session the page started (see {@link AppendSpeechRequest}).
630
+ */
631
+ export interface EndVoiceRequest {
632
+ voice_session_id: string
633
+ end_reason: Exclude<VoiceEndReason, 'superseded'>
634
+ duration_seconds?: number | null
635
+ /** What was said since the last speech report: a page that is being closed
636
+ * has one request left to send. */
637
+ segments?: SpeechSegment[]
398
638
  }
399
639
 
400
640
  /** List filter for sessions (in addition to pagination/sorting). */
@@ -410,6 +650,8 @@ export interface ListSessionsOptions extends ListOptions {
410
650
  * It narrows what the caller may see and never widens it.
411
651
  */
412
652
  created_by?: string
653
+ /** Keep the sessions one session delegated to. */
654
+ parent_session_id?: string
413
655
  }
414
656
 
415
657
  /**
@@ -432,3 +674,100 @@ export interface SessionStreamOptions {
432
674
  /** Abort to tear the stream down. */
433
675
  signal?: AbortSignal
434
676
  }
677
+
678
+ // ---------------------------------------------------------------------------
679
+ // The live delta channel
680
+ // ---------------------------------------------------------------------------
681
+
682
+ /**
683
+ * The chunk frames of the three long agent events, sent on the delta channel
684
+ * while the model writes the event: the whole event's type plus `.start`,
685
+ * `.delta` or `.end`. Frames are never stored and never posted by a client.
686
+ */
687
+ export type DeltaEventType =
688
+ | 'agent.message.start'
689
+ | 'agent.message.delta'
690
+ | 'agent.message.end'
691
+ | 'agent.reasoning.start'
692
+ | 'agent.reasoning.delta'
693
+ | 'agent.reasoning.end'
694
+ | 'agent.tool_use.start'
695
+ | 'agent.tool_use.delta'
696
+ | 'agent.tool_use.end'
697
+
698
+ /** Opens or closes the frames of one content part. */
699
+ export interface DeltaBoundaryPayload {
700
+ part_index: number
701
+ }
702
+
703
+ /** The next piece of a message's text. Append the pieces in order. */
704
+ export interface MessageDeltaPayload {
705
+ part_index: number
706
+ text: string
707
+ }
708
+
709
+ /** The next piece of the reasoning text, or the signature that closes it. */
710
+ export interface ReasoningDeltaPayload {
711
+ part_index: number
712
+ text?: string
713
+ signature?: string
714
+ }
715
+
716
+ /** A tool call is known by id, name and kind before its input has been written. */
717
+ export interface ToolUseStartPayload {
718
+ part_index: number
719
+ id: string
720
+ name: string
721
+ tool_kind: SessionToolKind
722
+ }
723
+
724
+ /** The next piece of a tool call's input, as JSON text. */
725
+ export interface ToolUseDeltaPayload {
726
+ part_index: number
727
+ partial_json: string
728
+ }
729
+
730
+ /**
731
+ * One chunk frame. It uses the envelope of the whole event it belongs to: `id`
732
+ * and `turn_id` are that event's, so a client builds a draft from the frames and
733
+ * drops it when the whole event with the same `id` arrives. `seq` is 0 — a frame
734
+ * has no place in the log.
735
+ */
736
+ export type SessionDeltaFrame = SessionEventEnvelope &
737
+ (
738
+ | { type: 'agent.message.start'; payload: DeltaBoundaryPayload }
739
+ | { type: 'agent.message.delta'; payload: MessageDeltaPayload }
740
+ | { type: 'agent.message.end'; payload: DeltaBoundaryPayload }
741
+ | { type: 'agent.reasoning.start'; payload: DeltaBoundaryPayload }
742
+ | { type: 'agent.reasoning.delta'; payload: ReasoningDeltaPayload }
743
+ | { type: 'agent.reasoning.end'; payload: DeltaBoundaryPayload }
744
+ | { type: 'agent.tool_use.start'; payload: ToolUseStartPayload }
745
+ | { type: 'agent.tool_use.delta'; payload: ToolUseDeltaPayload }
746
+ | { type: 'agent.tool_use.end'; payload: DeltaBoundaryPayload }
747
+ )
748
+
749
+ /**
750
+ * One entry of the delta channel: a whole event of the log, or a chunk frame of
751
+ * the event being written after it. `position` is the entry's place on the
752
+ * channel, `<seq>-<n>`: a whole event is `<seq>-0`, the frames after it count up
753
+ * from 1. Pass it back as {@link SessionDeltaStreamOptions.after} to resume.
754
+ */
755
+ export interface SessionDeltaEntry {
756
+ position: string
757
+ event: SessionEvent | SessionDeltaFrame
758
+ }
759
+
760
+ /** Options for opening the live delta stream. */
761
+ export interface SessionDeltaStreamOptions {
762
+ /** Replay from here (exclusive) before following live: a position, or a bare seq. */
763
+ after?: string | number
764
+ /** Abort to tear the stream down. */
765
+ signal?: AbortSignal
766
+ }
767
+
768
+ const DELTA_EVENT_TYPE = /^agent\.(message|reasoning|tool_use)\.(start|delta|end)$/
769
+
770
+ /** Whether an entry of the delta channel is a chunk frame, not a whole event. */
771
+ export function isDeltaFrame(event: SessionEvent | SessionDeltaFrame): event is SessionDeltaFrame {
772
+ return DELTA_EVENT_TYPE.test(event.type)
773
+ }
@@ -1,19 +1,27 @@
1
1
  import type { ProteosClient } from '../client.js'
2
2
  import { PageIterator } from '../iterator.js'
3
3
  import type { ListResult } from '../types/common.js'
4
+ import type { RequestOptions } from '../types/options.js'
4
5
  import type {
5
6
  AppendEventRequest,
7
+ AppendSpeechRequest,
6
8
  ClientToolSchema,
7
9
  ContentBlock,
8
10
  CreateSessionRequest,
11
+ EndVoiceRequest,
9
12
  FileBlock,
10
13
  ListSessionsOptions,
11
14
  ResultBlock,
12
15
  Session,
16
+ SessionDeltaEntry,
17
+ SessionDeltaStreamOptions,
13
18
  SessionEvent,
14
19
  SessionEventsOptions,
15
20
  SessionStreamOptions,
21
+ StartVoiceRequest,
22
+ StartVoiceResponse,
16
23
  Turn,
24
+ VoiceSession,
17
25
  } from './session-types.js'
18
26
 
19
27
  const SESSIONS_BASE_PATH = '/agents/v1/sessions'
@@ -50,6 +58,18 @@ export interface SessionService {
50
58
  * follows live; reconnects resume gap-free. Runs until the `AbortSignal` fires.
51
59
  */
52
60
  stream(id: string, options?: SessionStreamOptions): AsyncGenerator<SessionEvent, void, unknown>
61
+ /**
62
+ * Live-tails the session's delta channel over SSE: every whole event
63
+ * {@link stream} carries, and between them the chunk frames of the message,
64
+ * reasoning or tool call the agent is writing — what a chat renders from while
65
+ * a reply is in progress. Replays from `after` (exclusive) then follows live;
66
+ * reconnects resume from the last entry received. Runs until the `AbortSignal`
67
+ * fires. Tell the two kinds of entry apart with `isDeltaFrame`.
68
+ */
69
+ streamDelta(
70
+ id: string,
71
+ options?: SessionDeltaStreamOptions,
72
+ ): AsyncGenerator<SessionDeltaEntry, void, unknown>
53
73
 
54
74
  /**
55
75
  * Convenience: appends a `user.message` with a single text block. Pass
@@ -65,7 +85,9 @@ export interface SessionService {
65
85
  * `text` / `files` should be present. `context` rides the payload for the model
66
86
  * only (see {@link UserMessagePayload.context}). `clientTools` attaches the
67
87
  * sending surface's client tools to a session it did not create (see
68
- * {@link AppendEventRequest.client_tools}).
88
+ * {@link AppendEventRequest.client_tools}). A message to a session whose turn
89
+ * runs is queued for the model; `rejectIfRunning` asks for the 409 instead (see
90
+ * {@link AppendEventRequest.reject_if_running}).
69
91
  */
70
92
  sendUserMessage(
71
93
  id: string,
@@ -75,6 +97,7 @@ export interface SessionService {
75
97
  turnId?: string
76
98
  context?: string
77
99
  clientTools?: ClientToolSchema[]
100
+ rejectIfRunning?: boolean
78
101
  },
79
102
  ): Promise<SessionEvent>
80
103
  /** Convenience: appends a `user.interrupt` to halt the running turn. */
@@ -96,6 +119,39 @@ export interface SessionService {
96
119
  id: string,
97
120
  input: { toolUseId: string; result: ResultBlock[]; outcome?: 'success' | 'error' },
98
121
  ): Promise<SessionEvent>
122
+
123
+ /**
124
+ * Enters voice on a session: sends the WebRTC offer of the page's peer
125
+ * connection and returns the voice provider's answer, so the page never holds
126
+ * the provider's key. The page drives the voice session from then on, reports
127
+ * what is said with {@link appendSpeech}, and leaves with {@link endVoice}. A
128
+ * voice session still active on the session is ended first.
129
+ * @throws {ProteosError} 409 `voice_not_supported` when the session cannot be
130
+ * talked to by voice; 501 `voice_disabled`; 502 `voice_provider_failed`.
131
+ */
132
+ startVoice(id: string, request: StartVoiceRequest): Promise<StartVoiceResponse>
133
+ /**
134
+ * Reports what was said in the session's active voice session and returns the
135
+ * events written. With `starts_turn` the last of them is the
136
+ * `user.voice_request` that starts a turn.
137
+ * @throws {ProteosError} 409 `voice_session_not_active`.
138
+ */
139
+ appendSpeech(
140
+ id: string,
141
+ request: AppendSpeechRequest,
142
+ requestOptions?: RequestOptions,
143
+ ): Promise<SessionEvent[]>
144
+ /**
145
+ * Leaves voice. Closing the connection to the voice provider is the page's job.
146
+ * A page that is being closed passes `keepalive` so its last report and its
147
+ * end still arrive.
148
+ * @throws {ProteosError} 409 `voice_session_not_active`.
149
+ */
150
+ endVoice(
151
+ id: string,
152
+ request: EndVoiceRequest,
153
+ requestOptions?: RequestOptions,
154
+ ): Promise<VoiceSession>
99
155
  }
100
156
 
101
157
  export class SessionServiceImpl implements SessionService {
@@ -164,6 +220,20 @@ export class SessionServiceImpl implements SessionService {
164
220
  }
165
221
  }
166
222
 
223
+ async *streamDelta(
224
+ id: string,
225
+ options: SessionDeltaStreamOptions = {},
226
+ ): AsyncGenerator<SessionDeltaEntry, void, unknown> {
227
+ const after = options.after === undefined || options.after === 0 ? '' : String(options.after)
228
+ const query = after ? `?after_seq=${encodeURIComponent(after)}` : ''
229
+ const path = `${SESSIONS_BASE_PATH}/${id}/events/delta${query}`
230
+ const streamOptions = options.signal ? { signal: options.signal } : {}
231
+ for await (const frame of this.client.streamEvents(path, streamOptions)) {
232
+ const event = parseEvent(frame.data) as SessionDeltaEntry['event'] | null
233
+ if (event && frame.id) yield { position: frame.id, event }
234
+ }
235
+ }
236
+
167
237
  async sendMessage(id: string, text: string, turnId?: string): Promise<SessionEvent> {
168
238
  return this.sendUserMessage(id, { text, ...(turnId ? { turnId } : {}) })
169
239
  }
@@ -176,6 +246,7 @@ export class SessionServiceImpl implements SessionService {
176
246
  turnId?: string
177
247
  context?: string
178
248
  clientTools?: ClientToolSchema[]
249
+ rejectIfRunning?: boolean
179
250
  },
180
251
  ): Promise<SessionEvent> {
181
252
  const content: ContentBlock[] = []
@@ -190,6 +261,7 @@ export class SessionServiceImpl implements SessionService {
190
261
  ...(input.turnId ? { turn_id: input.turnId } : {}),
191
262
  payload: { content, ...(context ? { context } : {}) },
192
263
  ...(input.clientTools?.length ? { client_tools: input.clientTools } : {}),
264
+ ...(input.rejectIfRunning ? { reject_if_running: true } : {}),
193
265
  })
194
266
  }
195
267
 
@@ -214,6 +286,41 @@ export class SessionServiceImpl implements SessionService {
214
286
  },
215
287
  })
216
288
  }
289
+
290
+ async startVoice(id: string, request: StartVoiceRequest): Promise<StartVoiceResponse> {
291
+ return this.client.request<StartVoiceResponse>(
292
+ 'POST',
293
+ `${SESSIONS_BASE_PATH}/${id}/voice`,
294
+ request,
295
+ )
296
+ }
297
+
298
+ async appendSpeech(
299
+ id: string,
300
+ request: AppendSpeechRequest,
301
+ requestOptions?: RequestOptions,
302
+ ): Promise<SessionEvent[]> {
303
+ const response = await this.client.request<{ data: SessionEvent[] }>(
304
+ 'POST',
305
+ `${SESSIONS_BASE_PATH}/${id}/voice/speech`,
306
+ request,
307
+ requestOptions,
308
+ )
309
+ return response.data
310
+ }
311
+
312
+ async endVoice(
313
+ id: string,
314
+ request: EndVoiceRequest,
315
+ requestOptions?: RequestOptions,
316
+ ): Promise<VoiceSession> {
317
+ return this.client.request<VoiceSession>(
318
+ 'DELETE',
319
+ `${SESSIONS_BASE_PATH}/${id}/voice`,
320
+ request,
321
+ requestOptions,
322
+ )
323
+ }
217
324
  }
218
325
 
219
326
  /**