@frequencytalk/agent 0.1.1 → 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.
@@ -430,6 +430,45 @@ var Calls = class {
430
430
  }
431
431
  };
432
432
 
433
+ // src/phone.ts
434
+ var Phone = class {
435
+ constructor(client, realtime) {
436
+ this.client = client;
437
+ this.realtime = realtime;
438
+ }
439
+ client;
440
+ realtime;
441
+ /** Calls ringing on phone channels right now, from the socket's recent events (one frequency, or all). */
442
+ ringing(frequencyId) {
443
+ if (!this.realtime) return [];
444
+ const latest = /* @__PURE__ */ new Map();
445
+ for (const e of this.realtime.recent((e2) => e2.type === "phone.call")) {
446
+ const ring = e;
447
+ if (frequencyId && ring.frequencyId !== frequencyId) continue;
448
+ latest.set(ring.callId, ring);
449
+ }
450
+ return [...latest.values()].filter((r) => r.state === "ringing");
451
+ }
452
+ /** Subscribes to the frequency and resolves with the next ring on one of its phone channels. */
453
+ waitForRing(frequencyId, timeoutMs = 12e4) {
454
+ if (!this.realtime) throw new Error("phone.waitForRing needs the realtime socket");
455
+ this.realtime.subscribe(frequencyId);
456
+ return this.realtime.waitFor((e) => e.type === "phone.call" && e.state === "ringing" && e.frequencyId === frequencyId, timeoutMs).then((e) => e);
457
+ }
458
+ /** Answer a ringing call: the caller joins the phone channel as the line. 409 when somebody else got there first. */
459
+ answer(callId) {
460
+ return this.client.request(`/v1/phone-calls/${encodeURIComponent(callId)}/answer`, { method: "POST" });
461
+ }
462
+ /** Hang up a live call, or refuse one still ringing. */
463
+ hangUp(callId) {
464
+ return this.client.request(`/v1/phone-calls/${encodeURIComponent(callId)}/hangup`, { method: "POST" });
465
+ }
466
+ /** The calls ringing or live on a frequency's phone channels (the server's list; use after a reconnect). */
467
+ live(frequencyId) {
468
+ return this.client.request(`/v1/frequencies/${encodeURIComponent(frequencyId)}/phone-calls`);
469
+ }
470
+ };
471
+
433
472
  // src/audio/native.ts
434
473
  import { createSocket } from "dgram";
435
474
  import WebSocket2 from "ws";
@@ -515,6 +554,30 @@ var PHONE_PLANS = {
515
554
  phone_pro: { key: "phone_pro", name: "Phone Line Pro", priceUsdCents: 3999, minutes: 3e3 }
516
555
  };
517
556
  var PHONE_PLAN_KEYS = Object.keys(PHONE_PLANS);
557
+ var PhoneCallStateSchema = z2.enum(["ringing", "answering", "active", "ended"]);
558
+ var PhoneCallDirectionSchema = z2.enum(["inbound", "outbound"]);
559
+ var PhoneCallPartySchema = z2.object({
560
+ e164: z2.string(),
561
+ display: z2.string()
562
+ });
563
+ var PhoneCallSchema = z2.object({
564
+ id: z2.string(),
565
+ phoneNumberId: z2.string(),
566
+ frequencyId: z2.string(),
567
+ channelId: z2.string(),
568
+ direction: PhoneCallDirectionSchema,
569
+ state: PhoneCallStateSchema,
570
+ from: PhoneCallPartySchema,
571
+ to: PhoneCallPartySchema,
572
+ answeredByUserId: z2.string().nullable(),
573
+ startedAt: z2.string(),
574
+ answeredAt: z2.string().nullable(),
575
+ endedAt: z2.string().nullable(),
576
+ /** `no_answer` when the ring ran out, else the carrier's own cause word; null while live. */
577
+ hangupCause: z2.string().nullable(),
578
+ /** When the ring ends if nobody answers (ISO 8601); null once the call is past ringing. */
579
+ expiresAt: z2.string().nullable()
580
+ });
518
581
 
519
582
  // ../shared/dist/contacts.js
520
583
  import { z as z4 } from "zod";
@@ -2693,6 +2756,23 @@ var ServerToClientEventSchema = z10.discriminatedUnion("type", [
2693
2756
  muted: z10.boolean(),
2694
2757
  byUserId: z10.string()
2695
2758
  }),
2759
+ // A call on a phone channel (capability phone-calls, contract §24). Delivered to the
2760
+ // channel's audience (its roster and the frequency's staff): `ringing` is the moment to ring
2761
+ // and offer Answer, `answering` names who took it (stop ringing everywhere else), `active`
2762
+ // means both directions carry audio, `ended` clears the row. One call per line at a time,
2763
+ // so (channelId) identifies the live call as well as callId does; callId is what the
2764
+ // answer and hangup routes take. `expiresAt` rides on `ringing` only.
2765
+ z10.object({
2766
+ type: z10.literal("phone.call"),
2767
+ frequencyId: z10.string(),
2768
+ channelId: z10.string(),
2769
+ phoneNumberId: z10.string(),
2770
+ callId: z10.string(),
2771
+ state: z10.enum(["ringing", "answering", "active", "ended"]),
2772
+ from: z10.object({ e164: z10.string(), display: z10.string() }),
2773
+ answeredByUserId: z10.string().nullable().optional(),
2774
+ expiresAt: z10.string().optional()
2775
+ }),
2696
2776
  // Covers group create/rename/delete/membership changes and channel<->group link
2697
2777
  // changes — clients just refetch groups + channels for the frequency on any of these,
2698
2778
  // same as the channel.* events above, rather than diffing individual fields over the wire.
@@ -3985,6 +4065,9 @@ var ERROR_CODES = {
3985
4065
  PHONE_NOT_CONFIGURED: { status: 503, summary: "This deployment has no carrier configured, so no phone number can be searched, bought or released." },
3986
4066
  PHONE_NUMBER_UNAVAILABLE: { status: 409, summary: "The number was taken between the search and the purchase, or is already ours. Search again." },
3987
4067
  PHONE_SUBSCRIPTION_REQUIRED: { status: 402, summary: "Buying a number needs a paid, unclaimed phone line subscription (capability phone-numbers)." },
4068
+ PHONE_CALL_NOT_RINGING: { status: 409, summary: "The phone call is not ringing any more: it was answered, hung up, or rang out (capability phone-calls)." },
4069
+ PHONE_CALL_TAKEN: { status: 409, summary: "Somebody else answered this phone call first (capability phone-calls)." },
4070
+ PHONE_CALL_ENDED: { status: 409, summary: "The phone call has already ended (capability phone-calls)." },
3988
4071
  CARRIER_ERROR: { status: 502, summary: "The carrier refused or did not answer. Nothing was changed on our side unless the message says otherwise; retry later." },
3989
4072
  PROTECTED_MEMBER: {
3990
4073
  status: 403,
@@ -5113,18 +5196,21 @@ var Speaker = class {
5113
5196
 
5114
5197
  // src/listener.ts
5115
5198
  var Listener = class {
5116
- constructor(session) {
5199
+ constructor(session, options = {}) {
5117
5200
  this.session = session;
5201
+ this.options = options;
5118
5202
  }
5119
5203
  session;
5204
+ options;
5120
5205
  codec = null;
5121
5206
  streams = /* @__PURE__ */ new Map();
5122
5207
  detach = null;
5123
- /** Start recording everything the session hears. */
5208
+ /** Start recording everything the session hears (its own stream excepted, see `ListenerOptions`). */
5124
5209
  async start() {
5125
5210
  this.codec ??= await createOpus();
5126
5211
  const codec = this.codec;
5127
5212
  const handler = (frame) => {
5213
+ if (!this.options.includeOwn && frame.connectionId === this.session.connectionId) return;
5128
5214
  let s = this.streams.get(frame.streamId);
5129
5215
  if (!s) {
5130
5216
  s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, first: frame.timestamp };
@@ -5184,6 +5270,7 @@ var Agent = class _Agent {
5184
5270
  contacts;
5185
5271
  chat;
5186
5272
  calls;
5273
+ phone;
5187
5274
  sessions = /* @__PURE__ */ new Map();
5188
5275
  constructor(options) {
5189
5276
  this.client = new FrequencyClient(options);
@@ -5192,6 +5279,7 @@ var Agent = class _Agent {
5192
5279
  this.contacts = new Contacts(this.client);
5193
5280
  this.chat = new Chat(this.client, this.realtime);
5194
5281
  this.calls = new Calls(this.client, this.realtime);
5282
+ this.phone = new Phone(this.client, this.realtime);
5195
5283
  }
5196
5284
  /** Sign in with the key and open the socket. */
5197
5285
  static async connect(options) {
@@ -5219,8 +5307,8 @@ var Agent = class _Agent {
5219
5307
  speaker(session) {
5220
5308
  return new Speaker(session);
5221
5309
  }
5222
- listener(session) {
5223
- return new Listener(session);
5310
+ listener(session, options) {
5311
+ return new Listener(session, options);
5224
5312
  }
5225
5313
  async close() {
5226
5314
  for (const id of [...this.sessions.keys()]) await this.closeSession(id);
@@ -5334,6 +5422,11 @@ function registerTools(server, agent) {
5334
5422
  server.tool("call_accept", "Answer a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.accept(callId)));
5335
5423
  server.tool("call_decline", "Decline a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.decline(callId).then(() => "declined")));
5336
5424
  server.tool("call_hang_up", "Hang up a call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.hangUp(callId).then(() => "hung up")));
5425
+ server.tool("phone_ringing", "Calls ringing on phone channels right now (a frequency's, or every subscribed one). Answer with phone_answer.", { frequencyId: z18.string().optional() }, async ({ frequencyId }) => run(async () => agent.phone.ringing(frequencyId)));
5426
+ server.tool("phone_wait", "Wait up to `seconds` for a call to ring a phone channel on a frequency (subscribes to it).", { frequencyId: z18.string(), seconds: z18.number().int().min(1).max(600).default(120) }, async ({ frequencyId, seconds }) => run(() => agent.phone.waitForRing(frequencyId, seconds * 1e3)));
5427
+ server.tool("phone_answer", "Answer a call ringing on a phone channel. The caller then speaks on that channel: listen with audio_listen, reply with audio_speak on the frequency.", { callId: z18.string() }, async ({ callId }) => run(() => agent.phone.answer(callId)));
5428
+ server.tool("phone_hang_up", "Hang up a phone call, or refuse one still ringing.", { callId: z18.string() }, async ({ callId }) => run(() => agent.phone.hangUp(callId)));
5429
+ server.tool("phone_live", "The calls ringing or live on a frequency's phone channels, from the server.", { frequencyId: z18.string() }, async ({ frequencyId }) => run(() => agent.phone.live(frequencyId)));
5337
5430
  server.tool(
5338
5431
  "audio_speak",
5339
5432
  "Speak a WAV file (16-bit PCM) on a frequency's default channel over the native voice transport. Opens the session if needed.",