@frequencytalk/agent 0.1.2 → 0.2.1

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
@@ -49,6 +49,15 @@ const heard = listener.stop();
49
49
  const message = await agent.chat.waitForMessage();
50
50
  await agent.chat.send(message.fromUserId, "Heard you.");
51
51
 
52
+ // Answer the phone. A number bought for a frequency rings its phone channel; any member who
53
+ // may talk on that channel answers, an agent included. The caller is then a talker on the
54
+ // channel: listen to it, speak on the frequency, hang up.
55
+ const ring = await agent.phone.waitForRing(frequency!.id);
56
+ await agent.phone.answer(ring.callId);
57
+ session.listen(ring.channelId);
58
+ await agent.speaker(session).speakWav("greeting.wav");
59
+ await agent.phone.hangUp(ring.callId);
60
+
52
61
  await agent.close();
53
62
  ```
54
63
 
@@ -59,8 +68,8 @@ npx @frequencytalk/agent mcp
59
68
  ```
60
69
 
61
70
  runs an MCP server over stdio with tools named `frequency_*`, `contacts_*`, `chat_*`,
62
- `call_*`, `audio_*` and `credit_*`, and a `frequency://inbox` resource with the socket's recent
63
- events. The key is read as above.
71
+ `call_*`, `phone_*`, `audio_*` and `credit_*`, and a `frequency://inbox` resource with the
72
+ socket's recent events. The key is read as above.
64
73
 
65
74
  ## Paying
66
75
 
package/dist/cli.js CHANGED
@@ -432,6 +432,45 @@ var Calls = class {
432
432
  }
433
433
  };
434
434
 
435
+ // src/phone.ts
436
+ var Phone = class {
437
+ constructor(client, realtime) {
438
+ this.client = client;
439
+ this.realtime = realtime;
440
+ }
441
+ client;
442
+ realtime;
443
+ /** Calls ringing on phone channels right now, from the socket's recent events (one frequency, or all). */
444
+ ringing(frequencyId) {
445
+ if (!this.realtime) return [];
446
+ const latest = /* @__PURE__ */ new Map();
447
+ for (const e of this.realtime.recent((e2) => e2.type === "phone.call")) {
448
+ const ring = e;
449
+ if (frequencyId && ring.frequencyId !== frequencyId) continue;
450
+ latest.set(ring.callId, ring);
451
+ }
452
+ return [...latest.values()].filter((r) => r.state === "ringing");
453
+ }
454
+ /** Subscribes to the frequency and resolves with the next ring on one of its phone channels. */
455
+ waitForRing(frequencyId, timeoutMs = 12e4) {
456
+ if (!this.realtime) throw new Error("phone.waitForRing needs the realtime socket");
457
+ this.realtime.subscribe(frequencyId);
458
+ return this.realtime.waitFor((e) => e.type === "phone.call" && e.state === "ringing" && e.frequencyId === frequencyId, timeoutMs).then((e) => e);
459
+ }
460
+ /** Answer a ringing call: the caller joins the phone channel as the line. 409 when somebody else got there first. */
461
+ answer(callId) {
462
+ return this.client.request(`/v1/phone-calls/${encodeURIComponent(callId)}/answer`, { method: "POST" });
463
+ }
464
+ /** Hang up a live call, or refuse one still ringing. */
465
+ hangUp(callId) {
466
+ return this.client.request(`/v1/phone-calls/${encodeURIComponent(callId)}/hangup`, { method: "POST" });
467
+ }
468
+ /** The calls ringing or live on a frequency's phone channels (the server's list; use after a reconnect). */
469
+ live(frequencyId) {
470
+ return this.client.request(`/v1/frequencies/${encodeURIComponent(frequencyId)}/phone-calls`);
471
+ }
472
+ };
473
+
435
474
  // src/audio/native.ts
436
475
  import { createSocket } from "dgram";
437
476
  import WebSocket2 from "ws";
@@ -517,6 +556,30 @@ var PHONE_PLANS = {
517
556
  phone_pro: { key: "phone_pro", name: "Phone Line Pro", priceUsdCents: 3999, minutes: 3e3 }
518
557
  };
519
558
  var PHONE_PLAN_KEYS = Object.keys(PHONE_PLANS);
559
+ var PhoneCallStateSchema = z2.enum(["ringing", "answering", "active", "ended"]);
560
+ var PhoneCallDirectionSchema = z2.enum(["inbound", "outbound"]);
561
+ var PhoneCallPartySchema = z2.object({
562
+ e164: z2.string(),
563
+ display: z2.string()
564
+ });
565
+ var PhoneCallSchema = z2.object({
566
+ id: z2.string(),
567
+ phoneNumberId: z2.string(),
568
+ frequencyId: z2.string(),
569
+ channelId: z2.string(),
570
+ direction: PhoneCallDirectionSchema,
571
+ state: PhoneCallStateSchema,
572
+ from: PhoneCallPartySchema,
573
+ to: PhoneCallPartySchema,
574
+ answeredByUserId: z2.string().nullable(),
575
+ startedAt: z2.string(),
576
+ answeredAt: z2.string().nullable(),
577
+ endedAt: z2.string().nullable(),
578
+ /** `no_answer` when the ring ran out, else the carrier's own cause word; null while live. */
579
+ hangupCause: z2.string().nullable(),
580
+ /** When the ring ends if nobody answers (ISO 8601); null once the call is past ringing. */
581
+ expiresAt: z2.string().nullable()
582
+ });
520
583
 
521
584
  // ../shared/dist/contacts.js
522
585
  import { z as z4 } from "zod";
@@ -2695,6 +2758,23 @@ var ServerToClientEventSchema = z10.discriminatedUnion("type", [
2695
2758
  muted: z10.boolean(),
2696
2759
  byUserId: z10.string()
2697
2760
  }),
2761
+ // A call on a phone channel (capability phone-calls, contract §24). Delivered to the
2762
+ // channel's audience (its roster and the frequency's staff): `ringing` is the moment to ring
2763
+ // and offer Answer, `answering` names who took it (stop ringing everywhere else), `active`
2764
+ // means both directions carry audio, `ended` clears the row. One call per line at a time,
2765
+ // so (channelId) identifies the live call as well as callId does; callId is what the
2766
+ // answer and hangup routes take. `expiresAt` rides on `ringing` only.
2767
+ z10.object({
2768
+ type: z10.literal("phone.call"),
2769
+ frequencyId: z10.string(),
2770
+ channelId: z10.string(),
2771
+ phoneNumberId: z10.string(),
2772
+ callId: z10.string(),
2773
+ state: z10.enum(["ringing", "answering", "active", "ended"]),
2774
+ from: z10.object({ e164: z10.string(), display: z10.string() }),
2775
+ answeredByUserId: z10.string().nullable().optional(),
2776
+ expiresAt: z10.string().optional()
2777
+ }),
2698
2778
  // Covers group create/rename/delete/membership changes and channel<->group link
2699
2779
  // changes — clients just refetch groups + channels for the frequency on any of these,
2700
2780
  // same as the channel.* events above, rather than diffing individual fields over the wire.
@@ -3987,6 +4067,9 @@ var ERROR_CODES = {
3987
4067
  PHONE_NOT_CONFIGURED: { status: 503, summary: "This deployment has no carrier configured, so no phone number can be searched, bought or released." },
3988
4068
  PHONE_NUMBER_UNAVAILABLE: { status: 409, summary: "The number was taken between the search and the purchase, or is already ours. Search again." },
3989
4069
  PHONE_SUBSCRIPTION_REQUIRED: { status: 402, summary: "Buying a number needs a paid, unclaimed phone line subscription (capability phone-numbers)." },
4070
+ 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)." },
4071
+ PHONE_CALL_TAKEN: { status: 409, summary: "Somebody else answered this phone call first (capability phone-calls)." },
4072
+ PHONE_CALL_ENDED: { status: 409, summary: "The phone call has already ended (capability phone-calls)." },
3990
4073
  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." },
3991
4074
  PROTECTED_MEMBER: {
3992
4075
  status: 403,
@@ -5189,6 +5272,7 @@ var Agent = class _Agent {
5189
5272
  contacts;
5190
5273
  chat;
5191
5274
  calls;
5275
+ phone;
5192
5276
  sessions = /* @__PURE__ */ new Map();
5193
5277
  constructor(options) {
5194
5278
  this.client = new FrequencyClient(options);
@@ -5197,6 +5281,7 @@ var Agent = class _Agent {
5197
5281
  this.contacts = new Contacts(this.client);
5198
5282
  this.chat = new Chat(this.client, this.realtime);
5199
5283
  this.calls = new Calls(this.client, this.realtime);
5284
+ this.phone = new Phone(this.client, this.realtime);
5200
5285
  }
5201
5286
  /** Sign in with the key and open the socket. */
5202
5287
  static async connect(options) {
@@ -5339,6 +5424,11 @@ function registerTools(server, agent) {
5339
5424
  server.tool("call_accept", "Answer a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.accept(callId)));
5340
5425
  server.tool("call_decline", "Decline a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.decline(callId).then(() => "declined")));
5341
5426
  server.tool("call_hang_up", "Hang up a call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.hangUp(callId).then(() => "hung up")));
5427
+ 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)));
5428
+ 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)));
5429
+ 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)));
5430
+ 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)));
5431
+ 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)));
5342
5432
  server.tool(
5343
5433
  "audio_speak",
5344
5434
  "Speak a WAV file (16-bit PCM) on a frequency's default channel over the native voice transport. Opens the session if needed.",