@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.
package/README.md CHANGED
@@ -37,7 +37,8 @@ const { frequency } = await agent.frequencies.tuneByNumber(118025);
37
37
  const session = await agent.session(frequency!.id);
38
38
  await agent.speaker(session).speakWav("hello.wav");
39
39
 
40
- // Hear the reply for ten seconds. Receiving is off until you listen to a channel.
40
+ // Hear the reply for ten seconds. Receiving is off until you listen to a channel. Your own
41
+ // stream is not recorded (pass { includeOwn: true } to the listener if you want it).
41
42
  for (const c of await agent.frequencies.channels(frequency!.id)) session.listen(c.id);
42
43
  const listener = agent.listener(session);
43
44
  await listener.start();
@@ -48,6 +49,15 @@ const heard = listener.stop();
48
49
  const message = await agent.chat.waitForMessage();
49
50
  await agent.chat.send(message.fromUserId, "Heard you.");
50
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
+
51
61
  await agent.close();
52
62
  ```
53
63
 
@@ -58,8 +68,8 @@ npx @frequencytalk/agent mcp
58
68
  ```
59
69
 
60
70
  runs an MCP server over stdio with tools named `frequency_*`, `contacts_*`, `chat_*`,
61
- `call_*`, `audio_*` and `credit_*`, and a `frequency://inbox` resource with the socket's recent
62
- 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.
63
73
 
64
74
  ## Paying
65
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,
@@ -5115,18 +5198,21 @@ var Speaker = class {
5115
5198
 
5116
5199
  // src/listener.ts
5117
5200
  var Listener = class {
5118
- constructor(session) {
5201
+ constructor(session, options = {}) {
5119
5202
  this.session = session;
5203
+ this.options = options;
5120
5204
  }
5121
5205
  session;
5206
+ options;
5122
5207
  codec = null;
5123
5208
  streams = /* @__PURE__ */ new Map();
5124
5209
  detach = null;
5125
- /** Start recording everything the session hears. */
5210
+ /** Start recording everything the session hears (its own stream excepted, see `ListenerOptions`). */
5126
5211
  async start() {
5127
5212
  this.codec ??= await createOpus();
5128
5213
  const codec = this.codec;
5129
5214
  const handler = (frame) => {
5215
+ if (!this.options.includeOwn && frame.connectionId === this.session.connectionId) return;
5130
5216
  let s = this.streams.get(frame.streamId);
5131
5217
  if (!s) {
5132
5218
  s = { connectionId: frame.connectionId, chunks: [], lastTimestamp: null, first: frame.timestamp };
@@ -5186,6 +5272,7 @@ var Agent = class _Agent {
5186
5272
  contacts;
5187
5273
  chat;
5188
5274
  calls;
5275
+ phone;
5189
5276
  sessions = /* @__PURE__ */ new Map();
5190
5277
  constructor(options) {
5191
5278
  this.client = new FrequencyClient(options);
@@ -5194,6 +5281,7 @@ var Agent = class _Agent {
5194
5281
  this.contacts = new Contacts(this.client);
5195
5282
  this.chat = new Chat(this.client, this.realtime);
5196
5283
  this.calls = new Calls(this.client, this.realtime);
5284
+ this.phone = new Phone(this.client, this.realtime);
5197
5285
  }
5198
5286
  /** Sign in with the key and open the socket. */
5199
5287
  static async connect(options) {
@@ -5221,8 +5309,8 @@ var Agent = class _Agent {
5221
5309
  speaker(session) {
5222
5310
  return new Speaker(session);
5223
5311
  }
5224
- listener(session) {
5225
- return new Listener(session);
5312
+ listener(session, options) {
5313
+ return new Listener(session, options);
5226
5314
  }
5227
5315
  async close() {
5228
5316
  for (const id of [...this.sessions.keys()]) await this.closeSession(id);
@@ -5336,6 +5424,11 @@ function registerTools(server, agent) {
5336
5424
  server.tool("call_accept", "Answer a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.accept(callId)));
5337
5425
  server.tool("call_decline", "Decline a ringing call.", { callId: z18.string() }, async ({ callId }) => run(() => agent.calls.decline(callId).then(() => "declined")));
5338
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)));
5339
5432
  server.tool(
5340
5433
  "audio_speak",
5341
5434
  "Speak a WAV file (16-bit PCM) on a frequency's default channel over the native voice transport. Opens the session if needed.",