@arbidocs/tui 0.4.109 → 0.4.111

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/dist/index.js CHANGED
@@ -1327,8 +1327,8 @@ var miscCommands = [
1327
1327
  run: async (ctx) => {
1328
1328
  const { arbi } = ctx;
1329
1329
  const contactList = await sdk.contacts.listContacts(arbi);
1330
- const { registered, pending } = sdk.contacts.groupContactsByStatus(contactList);
1331
- if (registered.length === 0 && pending.length === 0) {
1330
+ const { registered, pending, cards } = sdk.contacts.groupContactsByStatus(contactList);
1331
+ if (registered.length === 0 && pending.length === 0 && cards.length === 0) {
1332
1332
  return "No contacts. Use the web app to add contacts.";
1333
1333
  }
1334
1334
  const lines = [];
@@ -1347,6 +1347,14 @@ var miscCommands = [
1347
1347
  lines.push(` ${c.email} \u2014 ${colors.muted(c.status)}`);
1348
1348
  }
1349
1349
  }
1350
+ if (cards.length > 0) {
1351
+ if (lines.length > 0) lines.push("");
1352
+ lines.push(`Address book cards (${cards.length}):`);
1353
+ for (const c of cards) {
1354
+ const label = c.card?.name || c.card?.emails?.[0]?.email || colors.muted("(locked card)");
1355
+ lines.push(` ${label}`);
1356
+ }
1357
+ }
1350
1358
  lines.push("", colors.muted("Type @name to start a DM with a registered contact."));
1351
1359
  return lines;
1352
1360
  }
@@ -1362,9 +1370,12 @@ var miscCommands = [
1362
1370
  const email = args[0].trim();
1363
1371
  const { showMessage: showMessage2 } = await Promise.resolve().then(() => (init_tui_helpers(), tui_helpers_exports));
1364
1372
  showMessage2(tui, `Inviting ${email}...`);
1365
- const result = await sdk.contacts.addContacts(arbi, [email]);
1373
+ const result = await sdk.contacts.inviteContacts(arbi, [email]);
1366
1374
  const contact = result[0];
1367
- if (contact?.status === "registered") {
1375
+ if (!contact || contact.status === "failed") {
1376
+ return `${colors.error("Not invited")} ${email} \u2014 ${contact?.outcome_detail ?? "the invitation could not be sent"}`;
1377
+ }
1378
+ if (contact.status === "registered") {
1368
1379
  return `${colors.success("Added")} ${email} \u2014 already registered. You can DM them with @${email.split("@")[0]}`;
1369
1380
  }
1370
1381
  return `${colors.success("Invited")} ${email} \u2014 invitation sent. They'll appear in /contacts once they register.`;
@@ -2102,6 +2113,20 @@ async function streamResponse(tui, response, docNames) {
2102
2113
  }
2103
2114
  }
2104
2115
  init_tui_helpers();
2116
+ function chatCrypto(tui) {
2117
+ const userExtId = tui.authContext?.loginResult.userExtId;
2118
+ if (!tui.encryptionKeys || !userExtId) return null;
2119
+ return { encryptionKeypair: tui.encryptionKeys, userExtId };
2120
+ }
2121
+ async function ensureChannelChat(tui, channel) {
2122
+ if (channel.ring && channel.chatExtId) return channel.ring;
2123
+ const crypto = chatCrypto(tui);
2124
+ if (!tui.authContext || !crypto) throw new Error("Not authenticated");
2125
+ const chat = await sdk.chats.openDirectChat(tui.authContext.arbi, channel.user.external_id);
2126
+ channel.chatExtId = chat.external_id;
2127
+ channel.ring = await sdk.chats.openChatKeyRing(chat, crypto);
2128
+ return channel.ring;
2129
+ }
2105
2130
  function deriveEncryptionKeypair(signingPrivateKeyBase64) {
2106
2131
  const signingPrivateKey = client.base64ToBytes(signingPrivateKeyBase64);
2107
2132
  const ed25519PublicKey = signingPrivateKey.slice(32, 64);
@@ -2122,7 +2147,7 @@ async function resolveRecipient(tui, query) {
2122
2147
  (c) => c.status === "registered" && c.user?.encryption_public_key
2123
2148
  );
2124
2149
  const matches = registered.filter((c) => {
2125
- const emailPrefix = c.email.split("@")[0]?.toLowerCase();
2150
+ const emailPrefix = (c.email ?? "").split("@")[0]?.toLowerCase();
2126
2151
  const givenName = c.user?.given_name?.toLowerCase();
2127
2152
  return emailPrefix === queryLower || givenName === queryLower;
2128
2153
  });
@@ -2144,7 +2169,7 @@ ${names}`, "warning");
2144
2169
  const user = contact.user;
2145
2170
  return {
2146
2171
  external_id: user.external_id,
2147
- email: contact.email,
2172
+ email: user.email,
2148
2173
  encryption_public_key: user.encryption_public_key,
2149
2174
  given_name: user.given_name,
2150
2175
  family_name: user.family_name
@@ -2190,25 +2215,20 @@ function switchToArbi(tui) {
2190
2215
  showMessage(tui, "Switched back to AI chat.");
2191
2216
  }
2192
2217
  async function loadDmHistory(tui, user) {
2193
- if (!tui.authContext || !tui.encryptionKeys) return;
2218
+ const channel = tui.currentDmChannel;
2219
+ if (!tui.authContext || !channel) return;
2194
2220
  const myExtId = tui.authContext.loginResult.userExtId;
2195
2221
  try {
2196
- const allDms = await sdk.dm.listDMs(tui.authContext.arbi);
2197
- const conversation = allDms.filter(
2198
- (msg) => msg.type === "user_message" && (msg.sender.external_id === user.external_id && msg.recipient.external_id === myExtId || msg.sender.external_id === myExtId && msg.recipient.external_id === user.external_id)
2199
- );
2200
- conversation.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
2201
- const unreadIds = conversation.filter((msg) => !msg.read && msg.sender.external_id === user.external_id).map((msg) => msg.external_id);
2202
- if (unreadIds.length > 0) {
2203
- sdk.dm.markRead(tui.authContext.arbi, unreadIds).catch(() => {
2222
+ const ring = await ensureChannelChat(tui, channel);
2223
+ const raw = await sdk.chats.listChatMessages(tui.authContext.arbi, ring.chatExtId, { limit: 100 });
2224
+ const conversation = (await sdk.chats.decryptChatMessages(ring, raw)).reverse();
2225
+ if (conversation.length > 0) {
2226
+ sdk.chats.markChatRead(tui.authContext.arbi, ring.chatExtId).catch(() => {
2204
2227
  });
2205
2228
  }
2206
2229
  for (const msg of conversation) {
2207
- const isSent = msg.sender.external_id === myExtId;
2208
- const plaintext = await decryptNotification(msg, myExtId, tui.encryptionKeys, user);
2209
- if (plaintext) {
2210
- displayDmMessage(tui, plaintext, isSent, user.email);
2211
- }
2230
+ if (!msg.content) continue;
2231
+ displayDmMessage(tui, msg.content, msg.sender_ext_id === myExtId, user.email);
2212
2232
  }
2213
2233
  tui.requestRender();
2214
2234
  } catch (err) {
@@ -2216,61 +2236,42 @@ async function loadDmHistory(tui, user) {
2216
2236
  }
2217
2237
  }
2218
2238
  async function sendEncryptedDm(tui, plaintext) {
2219
- if (!tui.authContext || !tui.encryptionKeys || !tui.currentDmChannel) {
2239
+ const channel = tui.currentDmChannel;
2240
+ if (!tui.authContext || !tui.encryptionKeys || !channel) {
2220
2241
  showMessage(tui, "Cannot send DM \u2014 not in a DM channel.", "error");
2221
2242
  return;
2222
2243
  }
2223
- const recipient = tui.currentDmChannel.user;
2224
2244
  try {
2225
- const encrypted = await client.encryptMessage(
2226
- plaintext,
2227
- recipient.encryption_public_key,
2228
- tui.encryptionKeys.secretKey
2229
- );
2230
- await sdk.dm.sendDM(tui.authContext.arbi, [
2231
- { recipient_ext_id: recipient.external_id, content: encrypted }
2232
- ]);
2233
- displayDmMessage(tui, plaintext, true, recipient.email);
2245
+ const ring = await ensureChannelChat(tui, channel);
2246
+ await sdk.chats.sendChatMessage(tui.authContext.arbi, ring, plaintext);
2247
+ displayDmMessage(tui, plaintext, true, channel.user.email);
2234
2248
  tui.requestRender();
2235
2249
  } catch (err) {
2236
2250
  showMessage(tui, `Failed to send DM: ${sdk.getErrorMessage(err)}`, "error");
2237
2251
  }
2238
2252
  }
2239
- async function handleIncomingDm(tui, msg) {
2240
- if (!tui.currentDmChannel || !tui.encryptionKeys || !tui.authContext) {
2241
- return false;
2242
- }
2243
- const myExtId = tui.authContext.loginResult.userExtId;
2244
- const channelUserId = tui.currentDmChannel.user.external_id;
2245
- if (msg.sender.external_id !== channelUserId) {
2253
+ async function handleIncomingDm(tui, event) {
2254
+ const channel = tui.currentDmChannel;
2255
+ if (!channel || !tui.encryptionKeys || !tui.authContext) return false;
2256
+ const msg = event.message;
2257
+ if (msg.sender_ext_id !== channel.user.external_id) return false;
2258
+ let ring;
2259
+ try {
2260
+ ring = await ensureChannelChat(tui, channel);
2261
+ } catch {
2246
2262
  return false;
2247
2263
  }
2248
- const plaintext = await decryptNotification(
2249
- msg,
2250
- myExtId,
2251
- tui.encryptionKeys,
2252
- tui.currentDmChannel.user
2253
- );
2254
- if (plaintext) {
2255
- displayDmMessage(tui, plaintext, false, msg.sender.email);
2264
+ if (event.chat_ext_id !== ring.chatExtId) return false;
2265
+ const decrypted = await sdk.chats.decryptChatMessage(ring, msg);
2266
+ const text = decrypted.decryptFailed ? "[decryption failed]" : decrypted.content;
2267
+ if (text) {
2268
+ displayDmMessage(tui, text, false, channel.user.email);
2256
2269
  tui.requestRender();
2257
- sdk.dm.markRead(tui.authContext.arbi, [msg.external_id]).catch(() => {
2270
+ sdk.chats.markChatRead(tui.authContext.arbi, ring.chatExtId, msg.external_id).catch(() => {
2258
2271
  });
2259
2272
  }
2260
2273
  return true;
2261
2274
  }
2262
- async function decryptNotification(notification, _myExtId, encryptionKeyPair, otherUser) {
2263
- if (!notification.content) return null;
2264
- try {
2265
- return await client.decryptMessage(
2266
- notification.content,
2267
- otherUser.encryption_public_key,
2268
- encryptionKeyPair.secretKey
2269
- );
2270
- } catch {
2271
- return "[decryption failed]";
2272
- }
2273
- }
2274
2275
  function displayDmMessage(tui, text, isSent, senderEmail) {
2275
2276
  if (isSent) {
2276
2277
  tui.chatLog.addUser(text);
@@ -2289,8 +2290,8 @@ var DURATION_BY_LEVEL = {
2289
2290
  error: DURATION_LONG,
2290
2291
  warning: DURATION_DEFAULT
2291
2292
  };
2292
- function isNotification(msg) {
2293
- return "sender" in msg && "recipient" in msg;
2293
+ function isChatMessage(msg) {
2294
+ return msg.type === "chat.message";
2294
2295
  }
2295
2296
  function shouldToast(level, msgType) {
2296
2297
  if (level === "error" || level === "warning" || level === "success") return true;
@@ -2298,7 +2299,7 @@ function shouldToast(level, msgType) {
2298
2299
  return false;
2299
2300
  }
2300
2301
  async function handleMessage(msg, toasts, eventLog, tui) {
2301
- if (isNotification(msg) && msg.type === "user_message" && tui) {
2302
+ if (isChatMessage(msg) && tui) {
2302
2303
  const handled = await handleIncomingDm(tui, msg);
2303
2304
  if (handled) return;
2304
2305
  }