@kernl-sdk/xai 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.
@@ -1,212 +0,0 @@
1
- import type { JSONSchema7 } from "json-schema";
2
- /**
3
- * Grok (xAI) Realtime API wire types.
4
- *
5
- * Based on https://docs.x.ai/docs/guides/voice/agent
6
- */
7
- export type GrokClientEvent = GrokSessionUpdate | GrokInputAudioBufferAppend | GrokInputAudioBufferCommit | GrokInputAudioBufferClear | GrokConversationItemCreate | GrokResponseCreate;
8
- export interface GrokSessionUpdate {
9
- type: "session.update";
10
- session: GrokSessionConfig;
11
- }
12
- export interface GrokInputAudioBufferAppend {
13
- type: "input_audio_buffer.append";
14
- audio: string;
15
- }
16
- export interface GrokInputAudioBufferCommit {
17
- type: "input_audio_buffer.commit";
18
- }
19
- export interface GrokInputAudioBufferClear {
20
- type: "input_audio_buffer.clear";
21
- }
22
- export interface GrokConversationItemCreate {
23
- type: "conversation.item.create";
24
- item: GrokItem;
25
- previous_item_id?: string;
26
- }
27
- export interface GrokResponseCreate {
28
- type: "response.create";
29
- }
30
- export type GrokServerEvent = GrokConversationCreated | GrokSessionUpdated | GrokInputAudioBufferCommitted | GrokInputAudioBufferCleared | GrokInputAudioBufferSpeechStarted | GrokInputAudioBufferSpeechStopped | GrokConversationItemAdded | GrokConversationItemInputAudioTranscriptionCompleted | GrokResponseCreated | GrokResponseOutputItemAdded | GrokResponseDone | GrokResponseOutputAudioDelta | GrokResponseOutputAudioDone | GrokResponseOutputAudioTranscriptDelta | GrokResponseOutputAudioTranscriptDone | GrokResponseFunctionCallArgumentsDone;
31
- export interface GrokConversationCreated {
32
- type: "conversation.created";
33
- event_id: string;
34
- conversation: {
35
- id: string;
36
- object: "realtime.conversation";
37
- };
38
- }
39
- export interface GrokSessionUpdated {
40
- type: "session.updated";
41
- event_id: string;
42
- session: GrokSession;
43
- }
44
- export interface GrokInputAudioBufferCommitted {
45
- type: "input_audio_buffer.committed";
46
- event_id: string;
47
- previous_item_id?: string;
48
- item_id: string;
49
- }
50
- export interface GrokInputAudioBufferCleared {
51
- type: "input_audio_buffer.cleared";
52
- event_id: string;
53
- }
54
- export interface GrokInputAudioBufferSpeechStarted {
55
- type: "input_audio_buffer.speech_started";
56
- event_id: string;
57
- item_id: string;
58
- }
59
- export interface GrokInputAudioBufferSpeechStopped {
60
- type: "input_audio_buffer.speech_stopped";
61
- event_id: string;
62
- item_id: string;
63
- }
64
- export interface GrokConversationItemAdded {
65
- type: "conversation.item.added";
66
- event_id: string;
67
- previous_item_id?: string;
68
- item: GrokItemWithId;
69
- }
70
- export interface GrokConversationItemInputAudioTranscriptionCompleted {
71
- type: "conversation.item.input_audio_transcription.completed";
72
- event_id: string;
73
- item_id: string;
74
- transcript: string;
75
- }
76
- export interface GrokResponseCreated {
77
- type: "response.created";
78
- event_id: string;
79
- response: {
80
- id: string;
81
- object: "realtime.response";
82
- status: "in_progress";
83
- output: unknown[];
84
- };
85
- }
86
- export interface GrokResponseOutputItemAdded {
87
- type: "response.output_item.added";
88
- event_id: string;
89
- response_id: string;
90
- output_index: number;
91
- item: GrokItemWithId;
92
- }
93
- export interface GrokResponseDone {
94
- type: "response.done";
95
- event_id: string;
96
- response: {
97
- id: string;
98
- object: "realtime.response";
99
- status: "completed" | "cancelled" | "failed";
100
- };
101
- }
102
- export interface GrokResponseOutputAudioDelta {
103
- type: "response.output_audio.delta";
104
- event_id: string;
105
- response_id: string;
106
- item_id: string;
107
- output_index: number;
108
- content_index: number;
109
- delta: string;
110
- }
111
- export interface GrokResponseOutputAudioDone {
112
- type: "response.output_audio.done";
113
- event_id: string;
114
- response_id: string;
115
- item_id: string;
116
- }
117
- export interface GrokResponseOutputAudioTranscriptDelta {
118
- type: "response.output_audio_transcript.delta";
119
- event_id: string;
120
- response_id: string;
121
- item_id: string;
122
- delta: string;
123
- }
124
- export interface GrokResponseOutputAudioTranscriptDone {
125
- type: "response.output_audio_transcript.done";
126
- event_id: string;
127
- response_id: string;
128
- item_id: string;
129
- }
130
- export interface GrokResponseFunctionCallArgumentsDone {
131
- type: "response.function_call_arguments.done";
132
- event_id: string;
133
- response_id?: string;
134
- item_id?: string;
135
- call_id: string;
136
- name: string;
137
- arguments: string;
138
- }
139
- export interface GrokSession {
140
- instructions?: string;
141
- voice?: GrokVoice;
142
- turn_detection?: GrokTurnDetection;
143
- }
144
- export interface GrokSessionConfig {
145
- instructions?: string;
146
- voice?: GrokVoice;
147
- turn_detection?: GrokTurnDetection | null;
148
- audio?: GrokAudioConfig;
149
- tools?: GrokTool[];
150
- }
151
- /**
152
- * Available Grok voices.
153
- */
154
- export type GrokVoice = "Ara" | "Rex" | "Sal" | "Eve" | "Leo";
155
- export interface GrokTurnDetection {
156
- type: "server_vad" | null;
157
- }
158
- export interface GrokAudioConfig {
159
- input?: {
160
- format?: GrokAudioFormat;
161
- };
162
- output?: {
163
- format?: GrokAudioFormat;
164
- };
165
- }
166
- export interface GrokAudioFormat {
167
- type: "audio/pcm" | "audio/pcmu" | "audio/pcma";
168
- rate?: number;
169
- }
170
- export type GrokTool = GrokFunctionTool | GrokWebSearchTool | GrokXSearchTool | GrokFileSearchTool;
171
- export interface GrokFunctionTool {
172
- type: "function";
173
- name: string;
174
- description?: string;
175
- parameters?: JSONSchema7;
176
- }
177
- export interface GrokWebSearchTool {
178
- type: "web_search";
179
- }
180
- export interface GrokXSearchTool {
181
- type: "x_search";
182
- allowed_x_handles?: string[];
183
- }
184
- export interface GrokFileSearchTool {
185
- type: "file_search";
186
- vector_store_ids: string[];
187
- max_num_results?: number;
188
- }
189
- export type GrokItem = GrokMessageItem | GrokFunctionCallOutputItem;
190
- export interface GrokMessageItem {
191
- type: "message";
192
- role: "user" | "assistant";
193
- content: GrokContentPart[];
194
- }
195
- export interface GrokFunctionCallOutputItem {
196
- type: "function_call_output";
197
- call_id: string;
198
- output: string;
199
- }
200
- export type GrokItemWithId = GrokItem & {
201
- id: string;
202
- object: "realtime.item";
203
- status: "completed" | "in_progress";
204
- };
205
- export type GrokContentPart = {
206
- type: "input_text";
207
- text: string;
208
- } | {
209
- type: "input_audio";
210
- transcript?: string;
211
- };
212
- //# sourceMappingURL=protocol.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;GAIG;AAMH,MAAM,MAAM,eAAe,GACvB,iBAAiB,GACjB,0BAA0B,GAC1B,0BAA0B,GAC1B,yBAAyB,GACzB,0BAA0B,GAC1B,kBAAkB,CAAC;AAEvB,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,EAAE,iBAAiB,CAAC;CAC5B;AAED,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,2BAA2B,CAAC;IAClC,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,2BAA2B,CAAC;CACnC;AAED,MAAM,WAAW,yBAAyB;IACxC,IAAI,EAAE,0BAA0B,CAAC;CAClC;AAED,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,0BAA0B,CAAC;IACjC,IAAI,EAAE,QAAQ,CAAC;IACf,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,iBAAiB,CAAC;CACzB;AAMD,MAAM,MAAM,eAAe,GACvB,uBAAuB,GACvB,kBAAkB,GAClB,6BAA6B,GAC7B,2BAA2B,GAC3B,iCAAiC,GACjC,iCAAiC,GACjC,yBAAyB,GACzB,oDAAoD,GACpD,mBAAmB,GACnB,2BAA2B,GAC3B,gBAAgB,GAChB,4BAA4B,GAC5B,2BAA2B,GAC3B,sCAAsC,GACtC,qCAAqC,GACrC,qCAAqC,CAAC;AAE1C,MAAM,WAAW,uBAAuB;IACt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
1
- import type { RealtimeModel, RealtimeConnection, RealtimeConnectOptions, ClientCredential } from "@kernl-sdk/protocol";
2
- /**
3
- * Options for creating a Grok realtime model.
4
- */
5
- export interface GrokRealtimeOptions {
6
- /**
7
- * xAI API key. Defaults to XAI_API_KEY env var.
8
- */
9
- apiKey?: string;
10
- /**
11
- * Base URL for the realtime API.
12
- */
13
- baseUrl?: string;
14
- }
15
- /**
16
- * Grok (xAI) realtime model implementation.
17
- */
18
- export declare class GrokRealtimeModel implements RealtimeModel {
19
- readonly spec: "1.0";
20
- readonly provider = "xai";
21
- readonly modelId: string;
22
- private apiKey;
23
- private baseUrl;
24
- constructor(modelId: string, options?: GrokRealtimeOptions);
25
- /**
26
- * Create ephemeral credential for client-side connections.
27
- *
28
- * Must be called server-side where API key is available.
29
- */
30
- authenticate(): Promise<ClientCredential>;
31
- /**
32
- * Establish a WebSocket connection to the Grok realtime API.
33
- */
34
- connect(options?: RealtimeConnectOptions): Promise<RealtimeConnection>;
35
- }
36
- //# sourceMappingURL=realtime.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"realtime.d.ts","sourceRoot":"","sources":["../src/realtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,kBAAkB,EAElB,sBAAsB,EAGtB,gBAAgB,EAEjB,MAAM,qBAAqB,CAAC;AAQ7B;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,qBAAa,iBAAkB,YAAW,aAAa;IACrD,QAAQ,CAAC,IAAI,EAAG,KAAK,CAAU;IAC/B,QAAQ,CAAC,QAAQ,SAAS;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB;IAS1D;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAgC/C;;OAEG;IACG,OAAO,CAAC,OAAO,CAAC,EAAE,sBAAsB,GAAG,OAAO,CAAC,kBAAkB,CAAC;CA4E7E"}
package/dist/realtime.js DELETED
@@ -1,250 +0,0 @@
1
- import { Emitter } from "@kernl-sdk/shared";
2
- import { CLIENT_EVENT, SERVER_EVENT } from "./convert/event.js";
3
- const XAI_REALTIME_URL = "wss://api.x.ai/v1/realtime";
4
- const XAI_CLIENT_SECRETS_URL = "https://api.x.ai/v1/realtime/client_secrets";
5
- /**
6
- * Grok (xAI) realtime model implementation.
7
- */
8
- export class GrokRealtimeModel {
9
- spec = "1.0";
10
- provider = "xai";
11
- modelId;
12
- apiKey;
13
- baseUrl;
14
- constructor(modelId, options) {
15
- this.modelId = modelId;
16
- this.apiKey =
17
- options?.apiKey ??
18
- (typeof process !== "undefined" ? process.env?.XAI_API_KEY : null) ??
19
- null;
20
- this.baseUrl = options?.baseUrl ?? XAI_REALTIME_URL;
21
- }
22
- /**
23
- * Create ephemeral credential for client-side connections.
24
- *
25
- * Must be called server-side where API key is available.
26
- */
27
- async authenticate() {
28
- if (!this.apiKey) {
29
- throw new Error("API key required for authenticate(). " +
30
- "Call this server-side where XAI_API_KEY is available.");
31
- }
32
- const res = await fetch(XAI_CLIENT_SECRETS_URL, {
33
- method: "POST",
34
- headers: {
35
- Authorization: `Bearer ${this.apiKey}`,
36
- "Content-Type": "application/json",
37
- },
38
- body: JSON.stringify({
39
- expires_after: { seconds: 300 },
40
- }),
41
- });
42
- if (!res.ok) {
43
- const text = await res.text();
44
- throw new Error(`Failed to create credential: ${res.status} ${text}`);
45
- }
46
- const data = (await res.json());
47
- return {
48
- kind: "token",
49
- token: data.value,
50
- expiresAt: new Date(Date.now() + 300_000), // 5 min TTL
51
- };
52
- }
53
- /**
54
- * Establish a WebSocket connection to the Grok realtime API.
55
- */
56
- async connect(options) {
57
- const credential = options?.credential;
58
- if (credential && credential.kind !== "token") {
59
- throw new Error(`Grok requires token credentials, got "${credential.kind}".`);
60
- }
61
- const authToken = credential?.token ?? this.apiKey;
62
- if (!authToken) {
63
- throw new Error("No API key or credential provided. " +
64
- "Either set XAI_API_KEY or pass a credential from authenticate().");
65
- }
66
- // Use injectable WebSocket or globalThis.WebSocket
67
- const WS = options?.websocket ?? globalThis.WebSocket;
68
- if (!WS) {
69
- throw new Error("No WebSocket available. In Node.js <22, use WebSocketTransport with the 'ws' package:\n" +
70
- " import WebSocket from 'ws';\n" +
71
- " import { WebSocketTransport } from 'kernl';\n" +
72
- " new RealtimeSession(agent, { transport: new WebSocketTransport({ websocket: WebSocket }), ... })");
73
- }
74
- // Grok uses standard Authorization header via protocols
75
- // Browser WebSocket doesn't support custom headers, so we pass token in subprotocol
76
- const url = this.baseUrl;
77
- // For browser: use subprotocol to pass auth (similar pattern to OpenAI)
78
- // For Node.js with 'ws' package: headers can be passed directly
79
- const protocols = [`token.${authToken}`];
80
- const ws = new WS(url, protocols);
81
- const connection = new GrokRealtimeConnection(ws);
82
- await new Promise((resolve, reject) => {
83
- if (options?.abort?.aborted) {
84
- return reject(new Error("Connection aborted"));
85
- }
86
- const onOpen = () => {
87
- cleanup();
88
- resolve();
89
- };
90
- const onError = (event) => {
91
- cleanup();
92
- const err = event instanceof Error
93
- ? event
94
- : new Error("WebSocket connection failed");
95
- reject(err);
96
- };
97
- const onAbort = () => {
98
- cleanup();
99
- ws.close();
100
- reject(new Error("Connection aborted"));
101
- };
102
- const cleanup = () => {
103
- ws.removeEventListener("open", onOpen);
104
- ws.removeEventListener("error", onError);
105
- options?.abort?.removeEventListener("abort", onAbort);
106
- };
107
- ws.addEventListener("open", onOpen);
108
- ws.addEventListener("error", onError);
109
- options?.abort?.addEventListener("abort", onAbort);
110
- });
111
- return connection;
112
- }
113
- }
114
- // WebSocket readyState constants
115
- const WS_OPEN = 1;
116
- /**
117
- * Grok realtime connection implementation.
118
- */
119
- class GrokRealtimeConnection extends Emitter {
120
- ws;
121
- _status = "connecting";
122
- _muted = false;
123
- _sessionId = null;
124
- // Audio state tracking for interruption
125
- currid;
126
- faudtime;
127
- audlenms = 0;
128
- responding = false;
129
- constructor(socket) {
130
- super();
131
- this.ws = socket;
132
- socket.addEventListener("message", (event) => {
133
- try {
134
- const data = event && typeof event === "object" && "data" in event
135
- ? event.data
136
- : String(event);
137
- const raw = JSON.parse(data);
138
- // Track audio state for interruption handling
139
- if (raw.type === "response.output_audio.delta") {
140
- this.currid = raw.item_id;
141
- if (this.faudtime === undefined) {
142
- this.faudtime = Date.now();
143
- this.audlenms = 0;
144
- }
145
- // Calculate audio length assuming 24kHz PCM16
146
- const bytes = base64ByteLength(raw.delta);
147
- this.audlenms += (bytes / 2 / 24000) * 1000;
148
- }
149
- else if (raw.type === "response.created") {
150
- this.responding = true;
151
- }
152
- else if (raw.type === "response.done") {
153
- this.responding = false;
154
- this.reset();
155
- }
156
- else if (raw.type === "input_audio_buffer.speech_started") {
157
- this.interrupt();
158
- }
159
- const event_ = SERVER_EVENT.decode(raw);
160
- if (event_) {
161
- if (event_.kind === "session.created") {
162
- this._sessionId = event_.session.id;
163
- }
164
- this.emit("event", event_);
165
- }
166
- }
167
- catch (err) {
168
- this.emit("error", err instanceof Error ? err : new Error(String(err)));
169
- }
170
- });
171
- socket.addEventListener("open", () => {
172
- this._status = "connected";
173
- this.emit("status", this._status);
174
- });
175
- socket.addEventListener("close", () => {
176
- this._status = "closed";
177
- this.reset();
178
- this.emit("status", this._status);
179
- });
180
- socket.addEventListener("error", (event) => {
181
- const err = event instanceof Error ? event : new Error("WebSocket error");
182
- this.emit("error", err);
183
- });
184
- }
185
- get status() {
186
- return this._status;
187
- }
188
- get muted() {
189
- return this._muted;
190
- }
191
- get sessionId() {
192
- return this._sessionId;
193
- }
194
- /**
195
- * Send a client event to the Grok realtime API.
196
- */
197
- send(event) {
198
- const encoded = CLIENT_EVENT.encode(event);
199
- if (encoded && this.ws.readyState === WS_OPEN) {
200
- this.ws.send(JSON.stringify(encoded));
201
- }
202
- }
203
- /**
204
- * Close the WebSocket connection.
205
- */
206
- close() {
207
- this.reset();
208
- this.ws.close();
209
- }
210
- /**
211
- * Mute audio input.
212
- */
213
- mute() {
214
- this._muted = true;
215
- }
216
- /**
217
- * Unmute audio input.
218
- */
219
- unmute() {
220
- this._muted = false;
221
- }
222
- /**
223
- * Interrupt the current response.
224
- *
225
- * Note: Grok doesn't support response.cancel or item.truncate,
226
- * so we just reset local state and emit the interrupted event.
227
- */
228
- interrupt() {
229
- if (this.responding) {
230
- this.responding = false;
231
- }
232
- this.emit("interrupted");
233
- this.reset();
234
- }
235
- /**
236
- * Reset audio tracking state.
237
- */
238
- reset() {
239
- this.currid = undefined;
240
- this.faudtime = undefined;
241
- this.audlenms = 0;
242
- }
243
- }
244
- /**
245
- * Get byte length from base64 string without decoding.
246
- */
247
- function base64ByteLength(b64) {
248
- const padding = b64.endsWith("==") ? 2 : b64.endsWith("=") ? 1 : 0;
249
- return (b64.length * 3) / 4 - padding;
250
- }