@gemies/sdk 10.14.0 → 10.16.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @gemies/sdk
2
2
 
3
+ ## 10.16.0
4
+
5
+ ### Phone calls a person approves
6
+
7
+ `@gemies/sdk/calls` gains the `proposed` and `expired` statuses and their events,
8
+ `proposalExpiresAt`, the `remote_hangup` and `busy` end reasons, the phone error codes, and the
9
+ shapes of the channel and caller ID routes (`CallChannelsState`, `CallerIdState`,
10
+ `StartedCallerIdVerification`). The contract and what a client still has to draw are in Gemies
11
+ `docs/calls-api.md`.
12
+
13
+ ## 10.15.0
14
+
15
+ ### A gemie can call
16
+
17
+ A gemie's call rings as a `call` frame on the realtime connection, and is answered by opening the
18
+ voice session with its id: `useVoiceRoom({ callId, conversationId, agentId })`. The new
19
+ `@gemies/sdk/calls` entry carries the shared types (`CallWire`, `CallEventData`, statuses, end
20
+ reasons and error codes). The voice protocol gains `assistant.speaking`, `endedBy: 'gemie'` on
21
+ `session.closed` when the gemie hung up, and the `call-unavailable` error. The contract is Gemies
22
+ `docs/calls-api.md`.
23
+
3
24
  ## 10.14.0
4
25
 
5
26
  ### What a coding agent's turn changed, and the way back
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gemies/sdk",
3
- "version": "10.14.0",
3
+ "version": "10.16.0",
4
4
  "description": "The Gemies SDK: the conversation runtime, chat UI, voice calls and dictation for Oxy apps that talk to a gemie",
5
5
  "main": "src/index.ts",
6
6
  "react-native": "src/index.ts",
@@ -14,6 +14,7 @@
14
14
  "./ask/lean": "./src/ask-lean.ts",
15
15
  "./ask/text": "./src/ask-text.ts",
16
16
  "./avatar": "./src/avatar.ts",
17
+ "./calls": "./src/calls.ts",
17
18
  "./character": "./src/character.ts",
18
19
  "./connectors": "./src/connectors.ts",
19
20
  "./content": "./src/lib/content-utils.ts",
package/src/calls.ts ADDED
@@ -0,0 +1,150 @@
1
+ /**
2
+ * A gemie's calls, as a client sees them (Gemies `docs/calls-api.md`).
3
+ *
4
+ * The server's copy is `packages/api/src/domain/call.ts`; the SDK does not depend on the API
5
+ * package, so the shapes are restated here and `call-contract.test.ts` holds the two equal.
6
+ *
7
+ * Pure data: no React, no socket. A ringing call arrives as a `call` frame on the realtime
8
+ * connection (`@gemies/sdk/realtime`) and is listed by `GET /calls?live=true`; it is answered by
9
+ * opening the voice session with its id (`useVoiceRoom({ callId, conversationId, agentId })`).
10
+ */
11
+
12
+ export const CALL_CHANNELS = ['app', 'phone', 'own_number'] as const;
13
+ export type CallChannel = (typeof CALL_CHANNELS)[number];
14
+
15
+ export const CALL_DIRECTIONS = ['outbound', 'inbound'] as const;
16
+ export type CallDirection = (typeof CALL_DIRECTIONS)[number];
17
+
18
+ export const CALL_STATUSES = [
19
+ 'proposed',
20
+ 'ringing',
21
+ 'active',
22
+ 'ended',
23
+ 'failed',
24
+ 'declined',
25
+ 'missed',
26
+ 'expired',
27
+ ] as const;
28
+ export type CallStatus = (typeof CALL_STATUSES)[number];
29
+
30
+ export const CALL_END_REASONS = [
31
+ 'gemie_hangup',
32
+ 'conversation_ended',
33
+ 'user_hangup',
34
+ 'remote_hangup',
35
+ 'declined',
36
+ 'busy',
37
+ 'missed',
38
+ 'cancelled',
39
+ 'expired',
40
+ 'max_duration',
41
+ 'idle_timeout',
42
+ 'credits_exhausted',
43
+ 'moved_elsewhere',
44
+ 'server_shutdown',
45
+ 'error',
46
+ ] as const;
47
+ export type CallEndReason = (typeof CALL_END_REASONS)[number];
48
+
49
+ export const CALL_SPEAKERS = ['gemie', 'user', 'third_party'] as const;
50
+ export type CallSpeaker = (typeof CALL_SPEAKERS)[number];
51
+
52
+ export const CALL_ERROR_CODES = [
53
+ 'invalid_request',
54
+ 'not_found',
55
+ 'not_ringing',
56
+ 'not_live',
57
+ 'not_pending',
58
+ 'call_in_progress',
59
+ 'rate_limited',
60
+ 'voice_plan_required',
61
+ 'not_configured',
62
+ 'not_enabled',
63
+ 'caller_id_required',
64
+ 'number_not_allowed',
65
+ 'number_unavailable',
66
+ 'credits_exhausted',
67
+ 'provider_error',
68
+ ] as const;
69
+ export type CallErrorCode = (typeof CALL_ERROR_CODES)[number];
70
+
71
+ export const CALL_EVENTS = [
72
+ 'call.proposed',
73
+ 'call.ringing',
74
+ 'call.accepted',
75
+ 'call.declined',
76
+ 'call.missed',
77
+ 'call.ended',
78
+ 'call.failed',
79
+ 'call.expired',
80
+ ] as const;
81
+ export type CallEventName = (typeof CALL_EVENTS)[number];
82
+
83
+ /** A call, as `GET /calls` and the `call` realtime frame carry it. */
84
+ export interface CallWire {
85
+ readonly id: string;
86
+ readonly channel: CallChannel;
87
+ readonly direction: CallDirection;
88
+ readonly status: CallStatus;
89
+ readonly endReason: CallEndReason | null;
90
+ /** The gemie; `null` is Alia. */
91
+ readonly agentId: string | null;
92
+ readonly agentName: string | null;
93
+ readonly conversationId: string | null;
94
+ readonly purpose: string | null;
95
+ readonly to: string | null;
96
+ readonly from: string | null;
97
+ readonly createdAt: string;
98
+ /** When a `proposed` call becomes `expired`. */
99
+ readonly proposalExpiresAt: string | null;
100
+ readonly ringExpiresAt: string | null;
101
+ readonly answeredAt: string | null;
102
+ readonly endedAt: string | null;
103
+ readonly durationSeconds: number | null;
104
+ }
105
+
106
+ export interface CallTranscriptEntry {
107
+ readonly id: string;
108
+ readonly speaker: CallSpeaker;
109
+ readonly text: string;
110
+ readonly ts: string;
111
+ }
112
+
113
+ /** The `data` of a `call` realtime frame. */
114
+ export interface CallEventData {
115
+ readonly event: CallEventName;
116
+ readonly call: CallWire;
117
+ }
118
+
119
+ /** The body of a refused `/calls` request. */
120
+ export interface CallErrorBody {
121
+ readonly error: { readonly code: CallErrorCode; readonly message: string };
122
+ }
123
+
124
+ /** `GET /calls/channels`. */
125
+ export interface CallChannelsState {
126
+ readonly app: { readonly status: 'available' };
127
+ readonly phone: {
128
+ readonly status: 'available' | 'caller_id_required' | 'not_configured';
129
+ readonly phoneNumber: string | null;
130
+ };
131
+ readonly own_number: { readonly status: 'not_enabled' };
132
+ }
133
+
134
+ /** `GET /calls/caller-id`'s `callerId`: the number the person calls from. */
135
+ export interface CallerIdState {
136
+ readonly configured: boolean;
137
+ readonly phoneNumber: string | null;
138
+ readonly pendingPhoneNumber: string | null;
139
+ readonly pendingExpiresAt: string | null;
140
+ }
141
+
142
+ /**
143
+ * `POST /calls/caller-id { phoneNumber }`: the provider rings the number, and whoever answers types
144
+ * `validationCode` on its keypad. Show the code.
145
+ */
146
+ export interface StartedCallerIdVerification {
147
+ readonly validationCode: string;
148
+ readonly phoneNumber: string;
149
+ readonly expiresAt: string;
150
+ }
@@ -37,6 +37,11 @@ export interface UseVoiceRoomOptions {
37
37
  conversationId?: string;
38
38
  /** The agent this call talks to; omitted, Alia. */
39
39
  agentId?: string;
40
+ /**
41
+ * Answer this ringing call from the gemie (`@gemies/sdk/calls`). `conversationId` and `agentId`
42
+ * must be the call's own.
43
+ */
44
+ callId?: string;
40
45
  /** Which of the product's two voices answers. */
41
46
  voicePreference?: ProductVoice;
42
47
  /** Bearer for the call; defaults to the surrounding Oxy session's. */
@@ -126,6 +131,7 @@ export function useVoiceRoom(options: UseVoiceRoomOptions = {}) {
126
131
  getAppAssertion,
127
132
  conversationId: current.conversationId ?? newConversationId(),
128
133
  ...(current.agentId === undefined ? {} : { agentId: current.agentId }),
134
+ ...(current.callId === undefined ? {} : { callId: current.callId }),
129
135
  voice: current.voicePreference ?? 'female',
130
136
  locale: current.lang ?? defaultSpeechLanguage(),
131
137
  createEngine: current.createAudioEngine ?? createVoiceAudioEngine,
@@ -9,6 +9,7 @@ const VOICE_ERRORS: Record<VoiceErrorCode, string> = {
9
9
  'plan-required': 'El modo voz necesita un plan de pago',
10
10
  'credits-exhausted': 'Te has quedado sin créditos para esta llamada',
11
11
  'agent-unavailable': 'Este Gemie no está disponible para una llamada',
12
+ 'call-unavailable': 'Esa llamada ya no está sonando',
12
13
  'invalid-request': 'No se pudo iniciar la llamada',
13
14
  'resume-unavailable': 'No se pudo retomar la llamada: empieza una nueva',
14
15
  'realtime-unavailable': 'La voz no está disponible ahora mismo: inténtalo más tarde',
@@ -36,6 +36,7 @@ export const VOICE_ERROR_MESSAGES: Readonly<Record<VoiceErrorCode, string>> = {
36
36
  'plan-required': 'Voice mode needs a paid plan',
37
37
  'credits-exhausted': "You've run out of credits for this call",
38
38
  'agent-unavailable': 'This agent is not available for a call',
39
+ 'call-unavailable': 'That call is not ringing any more',
39
40
  'invalid-request': 'The call could not be started',
40
41
  'resume-unavailable': 'The call could not be picked up again — start a new one',
41
42
  'realtime-unavailable': 'Voice is not available right now — try again later',
@@ -87,6 +87,8 @@ export interface VoiceCallOptions {
87
87
  readonly getAppAssertion?: () => Promise<string | null>;
88
88
  readonly conversationId: string;
89
89
  readonly agentId?: string;
90
+ /** The ringing call from the gemie that this call answers (`@gemies/sdk/calls`). */
91
+ readonly callId?: string;
90
92
  readonly voice: ProductVoice;
91
93
  /** BCP-47. */
92
94
  readonly locale: string;
@@ -376,6 +378,7 @@ export class VoiceCall {
376
378
  protocol: VOICE_PROTOCOL_VERSION,
377
379
  conversationId: this.options.conversationId,
378
380
  ...(this.options.agentId === undefined ? {} : { agentId: this.options.agentId }),
381
+ ...(this.options.callId === undefined ? {} : { callId: this.options.callId }),
379
382
  voice: this.options.voice,
380
383
  locale: this.options.locale,
381
384
  });
@@ -417,6 +420,8 @@ export class VoiceCall {
417
420
  case 'transcript.done':
418
421
  this.upsertMessage(event, () => event.text, false);
419
422
  return;
423
+ // Playback, not this event, is what moves the state to `speaking`.
424
+ case 'assistant.speaking':
420
425
  case 'assistant.audio_done':
421
426
  return;
422
427
  case 'assistant.done':
@@ -41,6 +41,7 @@ export type VoiceServerErrorCode =
41
41
  | 'plan-required'
42
42
  | 'credits-exhausted'
43
43
  | 'agent-unavailable'
44
+ | 'call-unavailable'
44
45
  | 'invalid-request'
45
46
  | 'resume-unavailable'
46
47
  | 'realtime-unavailable'
@@ -54,6 +55,7 @@ const SERVER_ERROR_CODES: ReadonlySet<string> = new Set<VoiceServerErrorCode>([
54
55
  'plan-required',
55
56
  'credits-exhausted',
56
57
  'agent-unavailable',
58
+ 'call-unavailable',
57
59
  'invalid-request',
58
60
  'resume-unavailable',
59
61
  'realtime-unavailable',
@@ -94,6 +96,8 @@ export type VoiceClientEvent =
94
96
  agentId?: string;
95
97
  voice: ProductVoice;
96
98
  locale: string;
99
+ /** Answer this ringing call from a gemie (`@gemies/sdk/calls`). */
100
+ callId?: string;
97
101
  }
98
102
  | { type: 'session.resume'; protocol: number; voiceSessionId: string; afterSeq: number }
99
103
  | { type: 'playback.interrupted'; itemId: string; playedMs: number }
@@ -118,6 +122,7 @@ export type VoiceServerEvent =
118
122
  */
119
123
  | { type: 'transcript.delta'; role: VoiceRole; messageId: string; itemId: string; text: string }
120
124
  | { type: 'transcript.done'; role: VoiceRole; messageId: string; itemId: string; text: string }
125
+ | { type: 'assistant.speaking'; itemId: string }
121
126
  | { type: 'assistant.audio_done'; itemId: string }
122
127
  | {
123
128
  type: 'assistant.done';
@@ -137,7 +142,8 @@ export type VoiceServerEvent =
137
142
  retryable: boolean;
138
143
  reference: string;
139
144
  }
140
- | { type: 'session.closed'; reason: VoiceCloseReason };
145
+ /** `endedBy: 'gemie'`: the gemie hung up. */
146
+ | { type: 'session.closed'; reason: VoiceCloseReason; endedBy?: 'gemie' };
141
147
 
142
148
  /** A server event as received: numbered once a session exists. */
143
149
  export type ReceivedVoiceEvent = VoiceServerEvent & { seq?: number };
@@ -199,6 +205,7 @@ export function parseVoiceServerEvent(raw: string): ReceivedVoiceEvent | null {
199
205
  }
200
206
  case 'user.speech_started':
201
207
  case 'user.speech_stopped':
208
+ case 'assistant.speaking':
202
209
  case 'assistant.audio_done':
203
210
  return isString(event.itemId) ? { type: event.type, itemId: event.itemId, ...seq } : null;
204
211
  case 'transcript.delta':
@@ -288,6 +295,7 @@ export function parseVoiceServerEvent(raw: string): ReceivedVoiceEvent | null {
288
295
  isString(event.reason) && CLOSE_REASONS.has(event.reason)
289
296
  ? (event.reason as VoiceCloseReason)
290
297
  : 'error',
298
+ ...(event.endedBy === 'gemie' ? { endedBy: 'gemie' as const } : {}),
291
299
  ...seq,
292
300
  };
293
301
  default:
@@ -6,6 +6,7 @@
6
6
  * Pure data: no React, no socket, so a test on either side can import it.
7
7
  */
8
8
 
9
+ import type { CallEventData } from '../calls';
9
10
  import type { Message } from '../runtime/types';
10
11
 
11
12
  /** The one server → client event. */
@@ -32,6 +33,7 @@ export const REALTIME_DURABLE_TYPES = [
32
33
  'notification',
33
34
  'connections',
34
35
  'collections',
36
+ 'call',
35
37
  ] as const;
36
38
 
37
39
  /** Signals: only true while they happen, never replayed. */
@@ -143,6 +145,8 @@ export interface RealtimeData {
143
145
  notification: { readonly notification: Record<string, unknown> & { readonly id: string } };
144
146
  connections: Record<string, never>;
145
147
  collections: CollectionsSnapshotWire;
148
+ /** A gemie's call rang, was answered or declined, or ended (`@gemies/sdk/calls`). */
149
+ call: CallEventData;
146
150
  activity: RealtimeActivity;
147
151
  room: {
148
152
  readonly conversationId: string;