@inkibra/voice-sdk 0.1.0 → 0.2.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.
@@ -0,0 +1,75 @@
1
+ import { ControlClient } from "./control.js";
2
+ import type { LiveVoiceConversationOptions, LiveVoiceEventMap, LiveVoiceSnapshot, LiveVoiceTask, LiveVoiceTools } from "./session-protocol.js";
3
+ export declare const defaultEndpoint = "wss://inkibra-voice.inkibra.workers.dev/v1/live";
4
+ export type ParticipantOptions = {
5
+ token: string;
6
+ endpoint?: string;
7
+ tools?: LiveVoiceTools;
8
+ prompt?: string;
9
+ conversation?: LiveVoiceConversationOptions;
10
+ };
11
+ export type Reconciliation = {
12
+ status: "succeeded" | "failed" | "cancelled";
13
+ output?: unknown;
14
+ error?: string;
15
+ };
16
+ export type LiveVoiceRating = {
17
+ overall: number;
18
+ scores?: {
19
+ waiting?: number;
20
+ interruptions?: number;
21
+ backchannels?: number;
22
+ voice?: number;
23
+ tools?: number;
24
+ };
25
+ note?: string;
26
+ moments?: {
27
+ atSeconds: number;
28
+ note: string;
29
+ }[];
30
+ };
31
+ export type LiveVoiceRatingResult = {
32
+ rating: LiveVoiceRating & Record<string, unknown>;
33
+ storedFields: string[];
34
+ };
35
+ /** Internal shared participant surface; owners and attachments expose different lifecycle operations. */
36
+ export declare class LiveVoiceParticipant {
37
+ protected readonly client: ControlClient;
38
+ protected readonly tools: LiveVoiceTools;
39
+ protected readonly developer: {
40
+ prompt: string;
41
+ conversation?: LiveVoiceConversationOptions;
42
+ };
43
+ protected connectedToken: string;
44
+ private listeners;
45
+ private readonly lifetime;
46
+ readonly closed: Promise<void>;
47
+ constructor(options: ParticipantOptions, client?: ControlClient);
48
+ get snapshot(): LiveVoiceSnapshot;
49
+ get sessionId(): string;
50
+ get participantId(): string;
51
+ get connectionId(): string;
52
+ get phase(): string | undefined;
53
+ on<K extends keyof LiveVoiceEventMap>(event: K, handler: (value: LiveVoiceEventMap[K]) => void): () => void;
54
+ protected rateSession(rating: LiveVoiceRating): Promise<LiveVoiceRatingResult>;
55
+ reconnect(options?: {
56
+ token?: string;
57
+ }): Promise<LiveVoiceSnapshot>;
58
+ dispose(): void;
59
+ protected finish(): void;
60
+ protected connectionChanged(state: LiveVoiceEventMap["connection"]["state"], detail?: string): void;
61
+ protected unknownMessage(message: Record<string, unknown>): void;
62
+ protected emit<K extends keyof LiveVoiceEventMap>(event: K, value: LiveVoiceEventMap[K]): void;
63
+ protected taskApi(): {
64
+ list: () => Promise<LiveVoiceTask[]>;
65
+ get: (taskId: string) => Promise<LiveVoiceTask | undefined>;
66
+ cancel: (taskId: string) => Promise<LiveVoiceTask | undefined>;
67
+ };
68
+ protected attachmentTaskApi(): {
69
+ reconcile: (taskId: string, resolution: Reconciliation) => Promise<LiveVoiceTask | undefined>;
70
+ list: () => Promise<LiveVoiceTask[]>;
71
+ get: (taskId: string) => Promise<LiveVoiceTask | undefined>;
72
+ cancel: (taskId: string) => Promise<LiveVoiceTask | undefined>;
73
+ };
74
+ private acceptEvent;
75
+ }
package/dist/public.d.ts CHANGED
@@ -1,109 +1,43 @@
1
- export type LiveVoiceTool<TInput extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = {
2
- description: string;
3
- parameters: Record<string, unknown>;
4
- execute(input: TInput): TResult | Promise<TResult>;
5
- };
6
- export type LiveVoiceTools = Record<string, LiveVoiceTool>;
7
- export type LiveVoicePlatformVoice = "eve" | "ara" | "rex" | "sal" | "leo";
8
- export type LiveVoiceConversationOptions = {
9
- /** Instructions for the rhythm and social behavior of the conversation. */
10
- prompt?: string;
11
- /** Selects which participant opens the session. Defaults to the user. */
12
- starts?: "user" | "assistant";
13
- phases?: {
14
- /** The phase active when the session begins. */
15
- initial: string;
16
- /** Instructions governing when the agent should move between phases. */
17
- prompt?: string;
18
- /** Developer-defined phases and the behavior appropriate to each one. */
19
- definitions: Record<string, string>;
20
- };
21
- };
22
- export type LiveVoiceConnectOptions = {
1
+ import { LiveVoiceParticipant, type LiveVoiceRating, type LiveVoiceRatingResult, type ParticipantOptions } from "./participant.js";
2
+ export type * from "./session-protocol.js";
3
+ import type { LiveVoiceSnapshot, LiveVoiceTools } from "./session-protocol.js";
4
+ export type { LiveVoiceRating, LiveVoiceRatingResult } from "./participant.js";
5
+ export type LiveVoiceCreateOptions = ParticipantOptions & {
6
+ mediaMode?: "browser";
7
+ };
8
+ export type LiveVoiceAttachOptions = {
23
9
  token: string;
24
- /** Task, domain, persona, and tool-use instructions for the agent. */
25
- prompt?: string;
26
- /** Provider-neutral control over conversational behavior and phases. */
27
- conversation?: LiveVoiceConversationOptions;
10
+ sessionId: string;
11
+ participantId: string;
28
12
  tools?: LiveVoiceTools;
29
13
  endpoint?: string;
30
14
  };
31
- export type LiveVoiceCanonicalTurn = {
32
- id: string;
33
- role: "user" | "assistant";
34
- text: string;
35
- truncated?: boolean;
36
- };
37
- export type LiveVoiceConnectionState = "connecting" | "live" | "closed" | "error";
38
- export type LiveVoiceFloorState = {
39
- owner: "USER" | "ASSISTANT";
40
- assistantAudible: boolean;
41
- };
42
- export type LiveVoiceToolCall = {
43
- callId: string;
44
- name: string;
45
- input: Record<string, unknown>;
46
- };
47
- export type LiveVoicePhaseEvent = {
48
- phase: string;
49
- previousPhase?: string;
50
- source: "initial" | "floor";
51
- };
52
- export type LiveVoiceEventMap = {
53
- "connection": {
54
- state: LiveVoiceConnectionState;
55
- detail?: string;
56
- };
57
- "transcript.instantaneous": {
58
- text: string;
59
- };
60
- "transcript.canonical": {
61
- turns: LiveVoiceCanonicalTurn[];
62
- };
63
- "floor": LiveVoiceFloorState;
64
- "phase": LiveVoicePhaseEvent;
65
- "quick-reaction": {
66
- reaction: string;
67
- status: "prepared" | "playing" | "completed" | "cancelled";
68
- };
69
- "tool.call": LiveVoiceToolCall;
70
- "tool.result": LiveVoiceToolCall & {
71
- ok: boolean;
72
- output?: unknown;
73
- error?: string;
74
- };
75
- "limit": {
76
- reason: string;
77
- durationSeconds?: number;
78
- };
79
- "error": {
80
- code: string;
81
- message: string;
82
- };
83
- };
84
- type EventName = keyof LiveVoiceEventMap;
85
- type EventHandler<K extends EventName> = (event: LiveVoiceEventMap[K]) => void;
86
- export declare class LiveVoiceSession {
87
- private client;
88
- private tools;
89
- private listeners;
90
- private previousPartial;
91
- private previousCanonical;
92
- private previousFloor;
93
- private reactionStates;
94
- private currentPhase?;
95
- private constructor();
96
- static connect(options: LiveVoiceConnectOptions): Promise<LiveVoiceSession>;
97
- on<K extends EventName>(event: K, handler: EventHandler<K>): () => void;
98
- get phase(): string | undefined;
99
- stop(): void;
100
- dispose(): void;
101
- private emit;
102
- private acceptApplicationEvent;
103
- private acceptState;
104
- private executeTool;
15
+ export type LiveVoiceReconnectOptions = {
16
+ token?: string;
17
+ };
18
+ export declare class LiveVoiceOwner extends LiveVoiceParticipant {
19
+ readonly tasks: {
20
+ list: () => Promise<import("./session-protocol.js").LiveVoiceTask[]>;
21
+ get: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
22
+ cancel: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
23
+ };
24
+ private started;
25
+ private starting?;
26
+ start(): Promise<void>;
27
+ rate(rating: LiveVoiceRating): Promise<LiveVoiceRatingResult>;
28
+ stop(): Promise<void>;
29
+ reconnect(options?: LiveVoiceReconnectOptions): Promise<LiveVoiceSnapshot>;
30
+ }
31
+ export declare class LiveVoiceAttachment extends LiveVoiceParticipant {
32
+ readonly tasks: {
33
+ reconcile: (taskId: string, resolution: import("./participant.js").Reconciliation) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
34
+ list: () => Promise<import("./session-protocol.js").LiveVoiceTask[]>;
35
+ get: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
36
+ cancel: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
37
+ };
38
+ detach(): Promise<void>;
105
39
  }
106
40
  export default class LiveVoice {
107
- static connect(options: LiveVoiceConnectOptions): Promise<LiveVoiceSession>;
41
+ static create(options: LiveVoiceCreateOptions): Promise<LiveVoiceOwner>;
42
+ static attach(options: LiveVoiceAttachOptions): Promise<LiveVoiceAttachment>;
108
43
  }
109
- export {};
package/dist/server.d.ts CHANGED
@@ -1,120 +1,120 @@
1
- import type { LiveVoiceConversationOptions, LiveVoiceEventMap, LiveVoicePlatformVoice, LiveVoiceTools } from "./public";
2
- export type { LiveVoiceCanonicalTurn, LiveVoiceConversationOptions, LiveVoiceEventMap, LiveVoiceFloorState, LiveVoicePhaseEvent, LiveVoicePlatformVoice, LiveVoiceTool, LiveVoiceToolCall, LiveVoiceTools, } from "./public";
1
+ import { ControlClient } from "./control.js";
2
+ import { LiveVoiceParticipant, type LiveVoiceRating, type LiveVoiceRatingResult } from "./participant.js";
3
+ export type { LiveVoiceRating, LiveVoiceRatingResult } from "./participant.js";
4
+ import type { LiveVoiceConversationOptions, LiveVoiceEventMap, LiveVoicePlatformVoice, LiveVoiceSnapshot, LiveVoiceTools } from "./session-protocol.js";
5
+ export type * from "./session-protocol.js";
3
6
  export type LiveVoicePcmFormat = {
4
7
  encoding: "pcm_s16le";
5
8
  sampleRate: 24_000;
6
9
  channels: 1;
7
10
  };
8
11
  export declare const liveVoicePcmFormat: LiveVoicePcmFormat;
9
- export type LiveVoiceAudioOutputCommand =
10
- /** Append PCM in arrival order. `buffer` is inaudible until release; `play` is immediately eligible for playback. */
11
- {
12
+ export type LiveVoiceAudioOutputCommand = {
12
13
  type: "enqueue";
13
14
  generationId: string;
14
15
  lane: "FOREGROUND" | "REACTION";
15
16
  pcm: Uint8Array;
16
17
  mode: "buffer" | "play";
17
18
  gain: number;
18
- }
19
- /** Prevent this foreground generation from becoming audible while its speculative audio arrives. */
20
- | {
19
+ } | {
21
20
  type: "block-foreground";
22
21
  generationId: string;
23
- }
24
- /** Make the buffered generation eligible for playback at the supplied gain. */
25
- | {
22
+ } | {
26
23
  type: "release";
27
24
  generationId: string;
28
25
  gain: number;
29
- }
30
- /** Stop this generation and discard all of its queued PCM. */
31
- | {
26
+ } | {
32
27
  type: "cancel";
33
28
  generationId: string;
34
- }
35
- /** No more PCM will arrive for this generation; completion still waits for the playback queue to drain. */
36
- | {
29
+ } | {
37
30
  type: "done";
38
31
  generationId: string;
39
32
  lane: "FOREGROUND" | "REACTION";
40
33
  };
41
34
  export type LiveVoicePlaybackReceipt = {
42
- event: "started" | "completed" | "cancelled";
43
- lane: "FOREGROUND" | "REACTION";
44
35
  generationId: string;
45
- /** Samples actually played, expressed at the native 24 kHz format. */
46
36
  playedSamples: number;
37
+ bufferedSamples: number;
38
+ completed: boolean;
39
+ cancelled?: boolean;
47
40
  };
48
41
  export type LiveVoiceAudioTransportHandlers = {
49
- /** Supply mono PCM16LE at 24 kHz. Frame boundaries do not imply conversational boundaries. */
50
42
  input(pcm: Uint8Array): void;
51
- /** Report the first audible sample and exactly one terminal outcome for each audible generation. */
52
43
  receipt(receipt: LiveVoicePlaybackReceipt): void;
53
- /** End the Live Voice session because the caller-side transport closed. */
54
44
  close(reason?: string): void;
55
45
  error(error: Error): void;
56
46
  };
57
47
  export interface LiveVoiceAudioTransport {
58
- /** SDK-facing format after any provider-specific decoding or resampling performed by the adapter. */
59
48
  readonly format: LiveVoicePcmFormat;
60
- /** Attach the session callbacks and make the transport ready to accept output commands. */
61
49
  start(handlers: LiveVoiceAudioTransportHandlers): void | Promise<void>;
62
- /** Apply commands in call order; the transport owns only PCM playback, never conversational state. */
63
50
  output(command: LiveVoiceAudioOutputCommand): void | Promise<void>;
64
51
  stop(): void | Promise<void>;
65
52
  }
66
- type LiveVoiceServerAuthorization = {
53
+ type Authorization = {
67
54
  token: string;
68
55
  apiKey?: never;
69
56
  } | {
70
57
  apiKey: string;
71
58
  token?: never;
72
59
  };
73
- export type LiveVoiceServerConnectOptions = LiveVoiceServerAuthorization & {
60
+ export type LiveVoiceServerCreateOptions = Authorization & {
74
61
  audio: LiveVoiceAudioTransport;
75
62
  prompt?: string;
76
63
  conversation?: LiveVoiceConversationOptions;
77
64
  tools?: LiveVoiceTools;
78
- /** Built-in voice selected when this SDK creates a session with `apiKey`. */
79
65
  voice?: LiveVoicePlatformVoice;
80
- /** Override the Live Voice WebSocket URL. */
81
66
  endpoint?: string;
82
- /** Override the HTTP API origin used with apiKey. */
83
67
  apiBaseUrl?: string;
84
68
  };
85
- type EventName = keyof LiveVoiceEventMap;
86
- type EventHandler<K extends EventName> = (event: LiveVoiceEventMap[K]) => void;
87
- export declare class LiveVoiceServerSession {
88
- private socket;
89
- private audio;
90
- private tools;
91
- readonly closed: Promise<void>;
92
- private resolveClosed;
93
- private listeners;
94
- private previousPartial;
95
- private previousCanonical;
96
- private previousFloor;
97
- private reactionStates;
98
- private state?;
99
- private stopped;
100
- private prepared?;
101
- private constructor();
102
- static connect(options: LiveVoiceServerConnectOptions): Promise<LiveVoiceServerSession>;
103
- on<K extends EventName>(event: K, handler: EventHandler<K>): () => void;
104
- get phase(): string | undefined;
105
- stop(): void;
69
+ export type LiveVoiceServerAttachOptions = Authorization & {
70
+ sessionId: string;
71
+ participantId: string;
72
+ audio?: never;
73
+ tools?: LiveVoiceTools;
74
+ endpoint?: string;
75
+ apiBaseUrl?: string;
76
+ };
77
+ export declare class LiveVoiceServerSession extends LiveVoiceParticipant {
78
+ private readonly audio;
79
+ readonly tasks: {
80
+ list: () => Promise<import("./session-protocol.js").LiveVoiceTask[]>;
81
+ get: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
82
+ cancel: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
83
+ };
84
+ private mediaWanted;
85
+ private audioActive;
86
+ private audioEpoch;
87
+ private starting?;
88
+ private audioStopping?;
89
+ constructor(options: {
90
+ token: string;
91
+ endpoint?: string;
92
+ tools?: LiveVoiceTools;
93
+ prompt?: string;
94
+ conversation?: LiveVoiceConversationOptions;
95
+ }, client: ControlClient, audio: LiveVoiceAudioTransport);
96
+ start(): Promise<void>;
97
+ private startMedia;
98
+ stop(): Promise<void>;
99
+ rate(rating: LiveVoiceRating): Promise<LiveVoiceRatingResult>;
100
+ reconnect(options?: {
101
+ token?: string;
102
+ }): Promise<LiveVoiceSnapshot>;
106
103
  dispose(): void;
107
- private bindSocket;
108
- private prepare;
109
- private onMessage;
110
- private writeInput;
111
- private sendReceipt;
112
- private executeTool;
113
- private acceptState;
114
- private acceptApplicationEvent;
115
- private emit;
116
- private finish;
104
+ protected connectionChanged(state: LiveVoiceEventMap["connection"]["state"], detail?: string): void;
105
+ private stopAudio;
106
+ protected unknownMessage(message: Record<string, unknown>): void;
107
+ }
108
+ export declare class LiveVoiceServerAttachment extends LiveVoiceParticipant {
109
+ readonly tasks: {
110
+ reconcile: (taskId: string, resolution: import("./participant.js").Reconciliation) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
111
+ list: () => Promise<import("./session-protocol.js").LiveVoiceTask[]>;
112
+ get: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
113
+ cancel: (taskId: string) => Promise<import("./session-protocol.js").LiveVoiceTask | undefined>;
114
+ };
115
+ detach(): Promise<void>;
117
116
  }
118
117
  export default class LiveVoiceServer {
119
- static connect(options: LiveVoiceServerConnectOptions): Promise<LiveVoiceServerSession>;
118
+ static create(options: LiveVoiceServerCreateOptions): Promise<LiveVoiceServerSession>;
119
+ static attach(options: LiveVoiceServerAttachOptions): Promise<LiveVoiceServerAttachment>;
120
120
  }