@absolutejs/voice 0.0.10 → 0.0.12

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.
@@ -422,6 +422,12 @@ var createVoiceStream = (path, options = {}) => {
422
422
  get error() {
423
423
  return store.getSnapshot().error;
424
424
  },
425
+ getServerSnapshot() {
426
+ return store.getServerSnapshot();
427
+ },
428
+ getSnapshot() {
429
+ return store.getSnapshot();
430
+ },
425
431
  get isConnected() {
426
432
  return store.getSnapshot().isConnected;
427
433
  },
@@ -418,6 +418,12 @@ var createVoiceStream = (path, options = {}) => {
418
418
  get error() {
419
419
  return store.getSnapshot().error;
420
420
  },
421
+ getServerSnapshot() {
422
+ return store.getServerSnapshot();
423
+ },
424
+ getSnapshot() {
425
+ return store.getSnapshot();
426
+ },
421
427
  get isConnected() {
422
428
  return store.getSnapshot().isConnected;
423
429
  },
package/dist/index.js CHANGED
@@ -675,12 +675,20 @@ var createSocketAdapter = (ws) => ({
675
675
  ws.send(data);
676
676
  }
677
677
  });
678
+ var normalizeOnTurn = (handler) => {
679
+ if (handler.length > 1) {
680
+ const directHandler = handler;
681
+ return async ({ context, session, turn, api }) => directHandler(session, turn, api, context);
682
+ }
683
+ return handler;
684
+ };
678
685
  var voice = (config) => {
679
686
  const runtime = {
680
687
  activeSessions: new Map,
681
688
  logger: resolveLogger(config.logger),
682
689
  socketSessions: new WeakMap
683
690
  };
691
+ const onTurn = normalizeOnTurn(config.onTurn);
684
692
  const htmxOptions = config.htmx && typeof config.htmx === "object" ? config.htmx : undefined;
685
693
  const htmxRoute = htmxOptions?.route ?? `${config.path}/htmx/session`;
686
694
  const htmxRenderers = resolveVoiceHTMXRenderers(config.htmx && config.htmx !== true ? config.htmx : undefined);
@@ -762,7 +770,7 @@ var voice = (config) => {
762
770
  onComplete: config.onComplete,
763
771
  onError: config.onError,
764
772
  onSession: config.onSession,
765
- onTurn: config.onTurn
773
+ onTurn
766
774
  },
767
775
  socket: createSocketAdapter(ws),
768
776
  store: config.session,
@@ -797,7 +805,7 @@ var voice = (config) => {
797
805
  onComplete: config.onComplete,
798
806
  onError: config.onError,
799
807
  onSession: config.onSession,
800
- onTurn: config.onTurn
808
+ onTurn
801
809
  },
802
810
  socket: createSocketAdapter(ws),
803
811
  store: config.session,
@@ -422,6 +422,12 @@ var createVoiceStream = (path, options = {}) => {
422
422
  get error() {
423
423
  return store.getSnapshot().error;
424
424
  },
425
+ getServerSnapshot() {
426
+ return store.getServerSnapshot();
427
+ },
428
+ getSnapshot() {
429
+ return store.getSnapshot();
430
+ },
425
431
  get isConnected() {
426
432
  return store.getSnapshot().isConnected;
427
433
  },
@@ -453,6 +459,15 @@ var createVoiceStream = (path, options = {}) => {
453
459
  };
454
460
 
455
461
  // src/react/useVoiceStream.tsx
462
+ var EMPTY_SNAPSHOT = {
463
+ assistantTexts: [],
464
+ error: null,
465
+ isConnected: false,
466
+ partial: "",
467
+ sessionId: "",
468
+ status: "idle",
469
+ turns: []
470
+ };
456
471
  var useVoiceStream = (path, options = {}) => {
457
472
  const streamRef = useRef(null);
458
473
  if (!streamRef.current) {
@@ -460,23 +475,7 @@ var useVoiceStream = (path, options = {}) => {
460
475
  }
461
476
  const stream = streamRef.current;
462
477
  useEffect(() => () => stream.close(), [stream]);
463
- const snapshot = useSyncExternalStore(stream.subscribe, () => ({
464
- assistantTexts: stream.assistantTexts,
465
- error: stream.error,
466
- isConnected: stream.isConnected,
467
- partial: stream.partial,
468
- sessionId: stream.sessionId,
469
- status: stream.status,
470
- turns: stream.turns
471
- }), () => ({
472
- assistantTexts: [],
473
- error: null,
474
- isConnected: false,
475
- partial: "",
476
- sessionId: "",
477
- status: "idle",
478
- turns: []
479
- }));
478
+ const snapshot = useSyncExternalStore(stream.subscribe, stream.getSnapshot, stream.getServerSnapshot) ?? EMPTY_SNAPSHOT;
480
479
  return {
481
480
  ...snapshot,
482
481
  close: () => stream.close(),
@@ -3,11 +3,11 @@ export declare const useVoiceStream: <TResult = unknown>(path: string, options?:
3
3
  close: () => void;
4
4
  endTurn: () => void;
5
5
  sendAudio: (audio: Uint8Array | ArrayBuffer) => void;
6
- assistantTexts: string[];
7
- error: string | null;
8
- isConnected: boolean;
9
- partial: string;
10
6
  sessionId: string | null;
11
7
  status: import("..").VoiceSessionStatus | "idle";
8
+ partial: string;
12
9
  turns: import("..").VoiceTurnRecord<TResult>[];
10
+ assistantTexts: string[];
11
+ error: string | null;
12
+ isConnected: boolean;
13
13
  };
@@ -419,6 +419,12 @@ var createVoiceStream = (path, options = {}) => {
419
419
  get error() {
420
420
  return store.getSnapshot().error;
421
421
  },
422
+ getServerSnapshot() {
423
+ return store.getServerSnapshot();
424
+ },
425
+ getSnapshot() {
426
+ return store.getSnapshot();
427
+ },
422
428
  get isConnected() {
423
429
  return store.getSnapshot().isConnected;
424
430
  },
package/dist/types.d.ts CHANGED
@@ -173,18 +173,20 @@ export type VoiceRouteResult<TResult = unknown> = {
173
173
  result?: TResult;
174
174
  assistantText?: string;
175
175
  };
176
+ export type VoiceOnTurnObjectHandler<TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown> = (input: {
177
+ context: TContext;
178
+ session: TSession;
179
+ turn: VoiceTurnRecord;
180
+ api: VoiceSessionHandle<TContext, TSession, TResult>;
181
+ }) => Promise<VoiceRouteResult<TResult> | void> | VoiceRouteResult<TResult> | void;
182
+ export type VoiceOnTurnHandler<TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown> = VoiceOnTurnObjectHandler<TContext, TSession, TResult> | ((session: TSession, turn: VoiceTurnRecord, api: VoiceSessionHandle<TContext, TSession, TResult>, context: TContext) => Promise<VoiceRouteResult<TResult> | void> | VoiceRouteResult<TResult> | void);
176
183
  export type VoiceRouteConfig<TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown> = {
177
184
  onSession?: (input: {
178
185
  context: TContext;
179
186
  session: TSession;
180
187
  api: VoiceSessionHandle<TContext, TSession, TResult>;
181
188
  }) => Promise<void> | void;
182
- onTurn: (input: {
183
- context: TContext;
184
- session: TSession;
185
- turn: VoiceTurnRecord;
186
- api: VoiceSessionHandle<TContext, TSession, TResult>;
187
- }) => Promise<VoiceRouteResult<TResult> | void> | VoiceRouteResult<TResult> | void;
189
+ onTurn: VoiceOnTurnHandler<TContext, TSession, TResult>;
188
190
  onComplete: (input: {
189
191
  context: TContext;
190
192
  session: TSession;
@@ -198,6 +200,9 @@ export type VoiceRouteConfig<TContext = unknown, TSession extends VoiceSessionRe
198
200
  api?: VoiceSessionHandle<TContext, TSession, TResult>;
199
201
  }) => Promise<void> | void;
200
202
  };
203
+ export type VoiceNormalizedRouteConfig<TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown> = Omit<VoiceRouteConfig<TContext, TSession, TResult>, 'onTurn'> & {
204
+ onTurn: VoiceOnTurnObjectHandler<TContext, TSession, TResult>;
205
+ };
201
206
  export type VoicePluginConfig<TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown> = {
202
207
  path: string;
203
208
  stt: STTAdapter;
@@ -220,7 +225,7 @@ export type CreateVoiceSessionOptions<TContext = unknown, TSession extends Voice
220
225
  turnDetection: {
221
226
  silenceMs: number;
222
227
  };
223
- route: VoiceRouteConfig<TContext, TSession, TResult>;
228
+ route: VoiceNormalizedRouteConfig<TContext, TSession, TResult>;
224
229
  logger?: VoiceLogger;
225
230
  };
226
231
  export type CreateVoiceSession = <TContext = unknown, TSession extends VoiceSessionRecord = VoiceSessionRecord, TResult = unknown>(options: CreateVoiceSessionOptions<TContext, TSession, TResult>) => VoiceSessionHandle<TContext, TSession, TResult>;
@@ -325,6 +330,8 @@ export type VoiceStream<TResult = unknown> = {
325
330
  close: () => void;
326
331
  endTurn: () => void;
327
332
  error: string | null;
333
+ getServerSnapshot: () => VoiceStreamState<TResult>;
334
+ getSnapshot: () => VoiceStreamState<TResult>;
328
335
  isConnected: boolean;
329
336
  partial: string;
330
337
  sendAudio: (audio: Uint8Array | ArrayBuffer) => void;
package/dist/vue/index.js CHANGED
@@ -422,6 +422,12 @@ var createVoiceStream = (path, options = {}) => {
422
422
  get error() {
423
423
  return store.getSnapshot().error;
424
424
  },
425
+ getServerSnapshot() {
426
+ return store.getServerSnapshot();
427
+ },
428
+ getSnapshot() {
429
+ return store.getSnapshot();
430
+ },
425
431
  get isConnected() {
426
432
  return store.getSnapshot().isConnected;
427
433
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/voice",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "Voice primitives and Elysia plugin for AbsoluteJS",
5
5
  "repository": {
6
6
  "type": "git",