@kabobcompany/lift 0.3.0 → 0.5.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/README.md CHANGED
@@ -34,6 +34,14 @@ and `storage` are the rest. An app reaches only its own files.
34
34
  `sendSms({ to, text })` a text message to one phone, both on Kabob's account and within
35
35
  the limits Lift sets; `email()` and `sms()` say what those are and what was sent today.
36
36
 
37
+ `channelTicket({ channels, client })` lets one client of the app listen to channels over a
38
+ long connection to Lift, and `publish(channel, event, data)` says something to everyone
39
+ listening; `listeners` and `channels` show who is connected.
40
+
41
+ `startVoiceCall(sdp, session)` sets up a live voice conversation between a browser and
42
+ OpenAI's Realtime models (the sound never passes through the app), and `endVoiceCall(id)`
43
+ hangs it up; calls are limited and counted by their length.
44
+
37
45
  A refusal is thrown as a `LiftError` with `status`, `code`, `message` and, on a 422,
38
46
  `issues`.
39
47
 
package/dist/index.d.ts CHANGED
@@ -496,4 +496,125 @@ export declare const sms: (options?: Paid) => Promise<{
496
496
  countries: string[];
497
497
  };
498
498
  }>;
499
+ export type ChannelTicket = {
500
+ /** What the client presents. */
501
+ ticket: string;
502
+ /** Where the client connects with it: `new EventSource(url)`. */
503
+ url: string;
504
+ expires_at: string;
505
+ };
506
+ /**
507
+ * A ticket for one client, naming the channels it may listen to:
508
+ *
509
+ * app.get("/api/ticket", requireCompany, async (c) =>
510
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
511
+ *
512
+ * Give one only to a client the app has recognised (a session, a paired
513
+ * device), and only for channels that client should hear: whoever holds the
514
+ * ticket hears them. `client` is the app's own word for who this is and
515
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
516
+ * seconds (an hour unless said, a day at most); the stream ends then with a
517
+ * `lift.expired` event and the client asks for a new one. 20 channels a
518
+ * ticket at most.
519
+ */
520
+ export declare const channelTicket: (ask: {
521
+ channels: string[];
522
+ client?: string | number;
523
+ meta?: Record<string, unknown>;
524
+ ttl?: number;
525
+ }) => Promise<ChannelTicket>;
526
+ /**
527
+ * Say something to everyone listening to a channel:
528
+ *
529
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
530
+ *
531
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
532
+ * what changed and let the client fetch it. With `retain` the message is
533
+ * also kept as the channel's latest, and whoever connects later gets it
534
+ * first. Answers with the message's id and how many were listening.
535
+ *
536
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
537
+ * colons and underscores. A message reaches who is connected now; it is not
538
+ * a queue, so what must not be lost also goes in the app's database.
539
+ */
540
+ export declare const publish: (channel: string, event: string, data?: unknown, options?: {
541
+ retain?: boolean;
542
+ }) => Promise<{
543
+ id: number;
544
+ listeners: number;
545
+ }>;
546
+ export type Listener = {
547
+ client: string | null;
548
+ meta: Record<string, unknown>;
549
+ since: string;
550
+ };
551
+ /** Who is connected to a channel right now, longest first. */
552
+ export declare const listeners: (channel: string) => Promise<Listener[]>;
553
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
554
+ export declare const channels: () => Promise<{
555
+ connections: number;
556
+ channels: {
557
+ name: string;
558
+ listeners: number;
559
+ kept: {
560
+ event: string;
561
+ at: string;
562
+ } | null;
563
+ }[];
564
+ limits: {
565
+ connections_per_app: number;
566
+ channels_per_ticket: number;
567
+ message_bytes: number;
568
+ publishes_per_minute: number;
569
+ };
570
+ }>;
571
+ /** Forget what a channel keeps as its latest. */
572
+ export declare const forgetKept: (channel: string) => Promise<void>;
573
+ export type VoiceCall = {
574
+ /** OpenAI's id for the call: keep it to hang up. */
575
+ id: string;
576
+ /** OpenAI's SDP answer: give it to the browser's `setRemoteDescription`. */
577
+ sdp: string;
578
+ model: string;
579
+ /** When Lift hangs the call up if nobody did. */
580
+ ends_at: string;
581
+ };
582
+ /**
583
+ * Start a voice call for a browser:
584
+ *
585
+ * voice.post("/call", requireCompany, async (c) => {
586
+ * const offer = await c.req.text(); // the browser's SDP offer
587
+ * const call = await startVoiceCall(offer, { instructions: PROMPT, audio: { output: { voice: "marin" } } },
588
+ * { who: c.get("session"), tag: `brand-${brand.id}` });
589
+ * remember(call.id, c.get("session").user.id); // to hang up later
590
+ * return c.json({ id: call.id, sdp: call.sdp, endsAt: call.ends_at });
591
+ * });
592
+ *
593
+ * `session` is OpenAI's session object (`model`, `instructions`, `tools`,
594
+ * `audio`), chosen by this server, never by the browser. Lift offers the
595
+ * models `voice()` lists and uses the small one when none is named.
596
+ *
597
+ * A call is cut off after ten minutes. Lift counts its length and the tokens
598
+ * its answers used. Hang up (`endVoiceCall`) as soon as the conversation is over. Throws a
599
+ * `LiftError`: 429 `too_many_calls` or `daily_limit`, 422 `refused` with
600
+ * OpenAI's reason.
601
+ */
602
+ export declare function startVoiceCall(sdp: string, session?: Record<string, unknown>, options?: Paid): Promise<VoiceCall>;
603
+ /** Hang a call up, and learn how many seconds it is counted as. Safe to call twice. */
604
+ export declare function endVoiceCall(id: string, options?: Paid): Promise<{
605
+ id: string;
606
+ seconds: number | null;
607
+ }>;
608
+ /** Calls going now, seconds of voice used today, and the limits (models, seconds a call, calls at once, seconds a day). */
609
+ export declare function voice(options?: Paid): Promise<{
610
+ active: number;
611
+ seconds_today: number;
612
+ limits: {
613
+ models: string[];
614
+ default_model: string;
615
+ max_seconds: number;
616
+ concurrent_per_app: number;
617
+ seconds_per_app_day: number;
618
+ };
619
+ }>;
499
620
  export {};
package/dist/index.js CHANGED
@@ -466,3 +466,82 @@ export async function sendSms(message, options = {}) {
466
466
  }
467
467
  /** How many segments the app sent today, its limits, and the countries Lift texts. */
468
468
  export const sms = (options = {}) => sending("sms", "GET", "", undefined, options);
469
+ // Channels: telling many clients something at once. Clients (screens,
470
+ // kiosks, phones, open pages) each hold one long connection to Lift and
471
+ // listen; the app's server says who may listen to what (`channelTicket`) and
472
+ // what to say (`publish`). The app holds no connection itself, so it can
473
+ // sleep and be deployed without anyone dropping. Needs the app's Channels
474
+ // switch on Lift.
475
+ const channelsCall = async (method, path, body) => paidAnswer(await paid("channels", method, path, body, { timeoutMs: 25_000 }), "Lift");
476
+ /**
477
+ * A ticket for one client, naming the channels it may listen to:
478
+ *
479
+ * app.get("/api/ticket", requireCompany, async (c) =>
480
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
481
+ *
482
+ * Give one only to a client the app has recognised (a session, a paired
483
+ * device), and only for channels that client should hear: whoever holds the
484
+ * ticket hears them. `client` is the app's own word for who this is and
485
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
486
+ * seconds (an hour unless said, a day at most); the stream ends then with a
487
+ * `lift.expired` event and the client asks for a new one. 20 channels a
488
+ * ticket at most.
489
+ */
490
+ export const channelTicket = (ask) => channelsCall("POST", "tickets", ask);
491
+ /**
492
+ * Say something to everyone listening to a channel:
493
+ *
494
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
495
+ *
496
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
497
+ * what changed and let the client fetch it. With `retain` the message is
498
+ * also kept as the channel's latest, and whoever connects later gets it
499
+ * first. Answers with the message's id and how many were listening.
500
+ *
501
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
502
+ * colons and underscores. A message reaches who is connected now; it is not
503
+ * a queue, so what must not be lost also goes in the app's database.
504
+ */
505
+ export const publish = (channel, event, data, options = {}) => channelsCall("POST", "publish", {
506
+ channel,
507
+ event,
508
+ ...(data !== undefined && { data }),
509
+ ...(options.retain && { retain: true }),
510
+ });
511
+ /** Who is connected to a channel right now, longest first. */
512
+ export const listeners = async (channel) => (await channelsCall("GET", `${encodeURIComponent(channel)}/listeners`)).listeners;
513
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
514
+ export const channels = () => channelsCall("GET", "");
515
+ /** Forget what a channel keeps as its latest. */
516
+ export const forgetKept = (channel) => channelsCall("DELETE", `${encodeURIComponent(channel)}/kept`);
517
+ /**
518
+ * Start a voice call for a browser:
519
+ *
520
+ * voice.post("/call", requireCompany, async (c) => {
521
+ * const offer = await c.req.text(); // the browser's SDP offer
522
+ * const call = await startVoiceCall(offer, { instructions: PROMPT, audio: { output: { voice: "marin" } } },
523
+ * { who: c.get("session"), tag: `brand-${brand.id}` });
524
+ * remember(call.id, c.get("session").user.id); // to hang up later
525
+ * return c.json({ id: call.id, sdp: call.sdp, endsAt: call.ends_at });
526
+ * });
527
+ *
528
+ * `session` is OpenAI's session object (`model`, `instructions`, `tools`,
529
+ * `audio`), chosen by this server, never by the browser. Lift offers the
530
+ * models `voice()` lists and uses the small one when none is named.
531
+ *
532
+ * A call is cut off after ten minutes. Lift counts its length and the tokens
533
+ * its answers used. Hang up (`endVoiceCall`) as soon as the conversation is over. Throws a
534
+ * `LiftError`: 429 `too_many_calls` or `daily_limit`, 422 `refused` with
535
+ * OpenAI's reason.
536
+ */
537
+ export async function startVoiceCall(sdp, session = {}, options = {}) {
538
+ return paidAnswer(await paid("openai", "POST", "realtime/calls", { sdp, session }, { timeoutMs: 30_000, ...options }), "OpenAI");
539
+ }
540
+ /** Hang a call up, and learn how many seconds it is counted as. Safe to call twice. */
541
+ export async function endVoiceCall(id, options = {}) {
542
+ return paidAnswer(await paid("openai", "POST", `realtime/calls/${encodeURIComponent(id)}/hangup`, undefined, { timeoutMs: 20_000, ...options }), "OpenAI");
543
+ }
544
+ /** Calls going now, seconds of voice used today, and the limits (models, seconds a call, calls at once, seconds a day). */
545
+ export async function voice(options = {}) {
546
+ return paidAnswer(await paid("openai", "GET", "realtime", undefined, { timeoutMs: 20_000, ...options }), "Lift");
547
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kabobcompany/lift",
3
- "version": "0.3.0",
4
- "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, run background jobs, and send email and text messages.",
3
+ "version": "0.5.0",
4
+ "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, run background jobs, send email and text messages, and publish to long-lived channels.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",