@photon-ai/advanced-imessage-kit 1.7.0 → 1.8.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/dist/index.js CHANGED
@@ -105,6 +105,48 @@ var getLogger = (tag) => {
105
105
  }
106
106
  return logger;
107
107
  };
108
+
109
+ // lib/auto-create-chat.ts
110
+ function isChatNotExistError(error) {
111
+ const axiosError = error;
112
+ const errorMsg = axiosError?.response?.data?.error?.message || axiosError?.response?.data?.message || "";
113
+ const lowerMsg = errorMsg.toLowerCase();
114
+ return lowerMsg.includes("chat does not exist") || lowerMsg.includes("chat not found");
115
+ }
116
+ function extractAddress(chatGuid) {
117
+ const parts = chatGuid.split(";-;");
118
+ if (parts.length !== 2 || !parts[1]) {
119
+ return void 0;
120
+ }
121
+ return parts[1];
122
+ }
123
+ function extractService(chatGuid) {
124
+ if (!chatGuid) return void 0;
125
+ const prefix = chatGuid.split(";")[0]?.toLowerCase();
126
+ if (prefix === "imessage") return "iMessage";
127
+ if (prefix === "sms") return "SMS";
128
+ return void 0;
129
+ }
130
+ async function createChatWithMessage(options) {
131
+ const { http, address, message, tempGuid, subject, effectId, service } = options;
132
+ try {
133
+ const response = await http.post("/api/v1/chat/new", {
134
+ addresses: [address],
135
+ message,
136
+ tempGuid,
137
+ subject,
138
+ effectId,
139
+ ...service && { service }
140
+ });
141
+ return response.data.data?.guid;
142
+ } catch (error) {
143
+ throw new Error(
144
+ `Failed to create chat with address "${address}": ${error instanceof Error ? error.message : String(error)}`
145
+ );
146
+ }
147
+ }
148
+
149
+ // modules/attachment.ts
108
150
  var AttachmentModule = class {
109
151
  constructor(http, enqueueSend = (task) => task()) {
110
152
  this.http = http;
@@ -147,52 +189,94 @@ var AttachmentModule = class {
147
189
  });
148
190
  return response.data.data.blurhash;
149
191
  }
192
+ /**
193
+ * Ensures the chat exists by creating it if it doesn't already exist.
194
+ */
195
+ async ensureChatExists(chatGuid) {
196
+ const address = extractAddress(chatGuid);
197
+ if (!address) return;
198
+ const service = extractService(chatGuid);
199
+ await this.http.post("/api/v1/chat/new", {
200
+ addresses: [address],
201
+ ...service && { service }
202
+ });
203
+ }
150
204
  async sendAttachment(options) {
151
205
  return this.enqueueSend(async () => {
152
206
  const fileBuffer = await readFile(options.filePath);
153
207
  const fileName = options.fileName || path__default.basename(options.filePath);
154
- const form = new FormData();
155
- form.append("chatGuid", options.chatGuid);
156
- form.append("attachment", fileBuffer, fileName);
157
- form.append("name", fileName);
158
- form.append("tempGuid", randomUUID());
159
- if (options.isAudioMessage !== void 0) {
160
- form.append("isAudioMessage", options.isAudioMessage.toString());
161
- if (options.isAudioMessage) {
162
- form.append("method", "private-api");
208
+ const tempGuid = randomUUID();
209
+ const buildForm = () => {
210
+ const form = new FormData();
211
+ form.append("chatGuid", options.chatGuid);
212
+ form.append("attachment", fileBuffer, fileName);
213
+ form.append("name", fileName);
214
+ form.append("tempGuid", tempGuid);
215
+ if (options.isAudioMessage !== void 0) {
216
+ form.append("isAudioMessage", options.isAudioMessage.toString());
217
+ if (options.isAudioMessage) {
218
+ form.append("method", "private-api");
219
+ }
163
220
  }
221
+ if (options.selectedMessageGuid) {
222
+ form.append("selectedMessageGuid", options.selectedMessageGuid);
223
+ }
224
+ return form;
225
+ };
226
+ try {
227
+ const form = buildForm();
228
+ const response = await this.http.post("/api/v1/message/attachment", form, {
229
+ headers: form.getHeaders()
230
+ });
231
+ return response.data.data;
232
+ } catch (error) {
233
+ if (!isChatNotExistError(error)) throw error;
234
+ await this.ensureChatExists(options.chatGuid);
235
+ const form = buildForm();
236
+ const response = await this.http.post("/api/v1/message/attachment", form, {
237
+ headers: form.getHeaders()
238
+ });
239
+ return response.data.data;
164
240
  }
165
- if (options.selectedMessageGuid) {
166
- form.append("selectedMessageGuid", options.selectedMessageGuid);
167
- }
168
- const response = await this.http.post("/api/v1/message/attachment", form, {
169
- headers: form.getHeaders()
170
- });
171
- return response.data.data;
172
241
  });
173
242
  }
174
243
  async sendSticker(options) {
175
244
  return this.enqueueSend(async () => {
176
245
  const fileName = options.fileName || path__default.basename(options.filePath);
177
- const form = new FormData();
178
- form.append("attachment", await readFile(options.filePath), fileName);
179
- form.append("name", fileName);
180
- form.append("chatGuid", options.chatGuid);
181
- form.append("isSticker", "true");
182
- form.append("method", "private-api");
183
- if (options.selectedMessageGuid) {
184
- form.append("selectedMessageGuid", options.selectedMessageGuid);
185
- form.append("partIndex", "0");
186
- form.append("stickerX", String(options.stickerX ?? 0.5));
187
- form.append("stickerY", String(options.stickerY ?? 0.5));
188
- form.append("stickerScale", String(options.stickerScale ?? 0.75));
189
- form.append("stickerRotation", String(options.stickerRotation ?? 0));
190
- form.append("stickerWidth", String(options.stickerWidth ?? 300));
246
+ const fileBuffer = await readFile(options.filePath);
247
+ const buildForm = () => {
248
+ const form = new FormData();
249
+ form.append("attachment", fileBuffer, fileName);
250
+ form.append("name", fileName);
251
+ form.append("chatGuid", options.chatGuid);
252
+ form.append("isSticker", "true");
253
+ form.append("method", "private-api");
254
+ if (options.selectedMessageGuid) {
255
+ form.append("selectedMessageGuid", options.selectedMessageGuid);
256
+ form.append("partIndex", "0");
257
+ form.append("stickerX", String(options.stickerX ?? 0.5));
258
+ form.append("stickerY", String(options.stickerY ?? 0.5));
259
+ form.append("stickerScale", String(options.stickerScale ?? 0.75));
260
+ form.append("stickerRotation", String(options.stickerRotation ?? 0));
261
+ form.append("stickerWidth", String(options.stickerWidth ?? 300));
262
+ }
263
+ return form;
264
+ };
265
+ try {
266
+ const form = buildForm();
267
+ const { data } = await this.http.post("/api/v1/message/attachment", form, {
268
+ headers: form.getHeaders()
269
+ });
270
+ return data.data;
271
+ } catch (error) {
272
+ if (!isChatNotExistError(error)) throw error;
273
+ await this.ensureChatExists(options.chatGuid);
274
+ const form = buildForm();
275
+ const { data } = await this.http.post("/api/v1/message/attachment", form, {
276
+ headers: form.getHeaders()
277
+ });
278
+ return data.data;
191
279
  }
192
- const { data } = await this.http.post("/api/v1/message/attachment", form, {
193
- headers: form.getHeaders()
194
- });
195
- return data.data;
196
280
  });
197
281
  }
198
282
  };
@@ -237,8 +321,8 @@ var ChatModule = class {
237
321
  return response.data.data;
238
322
  }
239
323
  async removeParticipant(chatGuid, address) {
240
- const response = await this.http.delete(`/api/v1/chat/${encodeURIComponent(chatGuid)}/participant`, {
241
- data: { address }
324
+ const response = await this.http.post(`/api/v1/chat/${encodeURIComponent(chatGuid)}/participant/remove`, {
325
+ address
242
326
  });
243
327
  return response.data.data;
244
328
  }
@@ -392,8 +476,25 @@ var MessageModule = class {
392
476
  return this.enqueueSend(async () => {
393
477
  const tempGuid = options.tempGuid || randomUUID();
394
478
  const payload = { ...options, tempGuid };
395
- const response = await this.http.post("/api/v1/message/text", payload);
396
- return response.data.data;
479
+ try {
480
+ const response = await this.http.post("/api/v1/message/text", payload);
481
+ return response.data.data;
482
+ } catch (error) {
483
+ if (!isChatNotExistError(error)) throw error;
484
+ const address = extractAddress(options.chatGuid);
485
+ if (!address) throw error;
486
+ const service = extractService(options.chatGuid);
487
+ await createChatWithMessage({
488
+ http: this.http,
489
+ address,
490
+ message: options.message,
491
+ tempGuid,
492
+ subject: options.subject,
493
+ effectId: options.effectId,
494
+ service
495
+ });
496
+ return { guid: tempGuid, text: options.message, dateCreated: Date.now() };
497
+ }
397
498
  });
398
499
  }
399
500
  async getMessage(guid, options) {