@deckasoft/waify 0.3.7 → 0.3.9

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.
Files changed (2) hide show
  1. package/dist/cli/index.js +35 -11
  2. package/package.json +1 -1
package/dist/cli/index.js CHANGED
@@ -969,6 +969,7 @@ var SessionResponseSchema = z5.object({
969
969
  id: z5.string().optional(),
970
970
  name: z5.string().optional()
971
971
  });
972
+ var SessionListSchema = z5.array(SessionResponseSchema);
972
973
  var QrResponseSchema = z5.object({
973
974
  qrCode: z5.string().optional()
974
975
  });
@@ -1019,11 +1020,6 @@ var registerSetup = (program2) => {
1019
1020
  console.warn("Creating config directory...");
1020
1021
  mkdirSync4(join2(homedir2(), ".config", "waify"), { recursive: true });
1021
1022
  const baseUrl = loadConfig().openwaBaseUrl;
1022
- const rawApiKey = await promptLine(
1023
- rl,
1024
- 'OpenWA API key (press Enter to use default "dev-admin-key"):\n> '
1025
- );
1026
- const openwaApiKey = rawApiKey.trim() || "dev-admin-key";
1027
1023
  let geminiKey = "";
1028
1024
  while (!geminiKey.trim()) {
1029
1025
  geminiKey = await promptLine(
@@ -1046,8 +1042,8 @@ var registerSetup = (program2) => {
1046
1042
  }
1047
1043
  }
1048
1044
  const chatId = `${recipientNumber.trim()}@c.us`;
1049
- saveSecrets({ GEMINI_API_KEY: geminiKey.trim(), OPENWA_API_KEY: openwaApiKey });
1050
- saveConfig({ ...loadConfig(), openwaApiKey, recipients: [{ chatId }] });
1045
+ saveSecrets({ GEMINI_API_KEY: geminiKey.trim(), OPENWA_API_KEY: "" });
1046
+ saveConfig({ ...loadConfig(), recipients: [{ chatId }] });
1051
1047
  console.warn("Writing docker-compose.yml...");
1052
1048
  writeFileSync5(composePath(), composeTemplate(), "utf-8");
1053
1049
  console.warn("Starting OpenWA containers (this may take a minute on first run)...");
@@ -1079,6 +1075,19 @@ var registerSetup = (program2) => {
1079
1075
  process.exitCode = 1;
1080
1076
  return;
1081
1077
  }
1078
+ console.warn("Reading API key from container...");
1079
+ const keyResult = spawnSync(
1080
+ "docker",
1081
+ ["compose", "-f", composePath(), "exec", "-T", "openwa-api", "cat", "/app/data/.api-key"],
1082
+ { encoding: "utf-8" }
1083
+ );
1084
+ const openwaApiKey = keyResult.stdout?.trim();
1085
+ if (keyResult.status !== 0 || !openwaApiKey) {
1086
+ const errorMsg = keyResult.stderr?.trim() || "Could not read API key from container.";
1087
+ throw new Error(`${errorMsg} Check logs with: docker compose -f ${composePath()} logs openwa-api`);
1088
+ }
1089
+ saveSecrets({ GEMINI_API_KEY: geminiKey.trim(), OPENWA_API_KEY: openwaApiKey });
1090
+ saveConfig({ ...loadConfig(), openwaApiKey, recipients: [{ chatId }] });
1082
1091
  console.warn("Creating WhatsApp session...");
1083
1092
  const sessionRes = await fetchWithTimeout(`${baseUrl}/api/sessions`, {
1084
1093
  method: "POST",
@@ -1088,17 +1097,32 @@ var registerSetup = (program2) => {
1088
1097
  },
1089
1098
  body: JSON.stringify({ name: "waify" })
1090
1099
  }, 1e4);
1091
- if (!sessionRes.ok) {
1100
+ let sessionId;
1101
+ if (sessionRes.status === 409) {
1102
+ const listRes = await fetchWithTimeout(`${baseUrl}/api/sessions`, {
1103
+ headers: { "X-API-Key": openwaApiKey }
1104
+ }, 1e4);
1105
+ if (!listRes.ok) {
1106
+ throw new Error(`Failed to list sessions: ${listRes.status} ${listRes.statusText}`);
1107
+ }
1108
+ const sessions = SessionListSchema.parse(await listRes.json());
1109
+ const existing = sessions.find((s) => s.name === "waify");
1110
+ if (!existing?.id) {
1111
+ throw new Error('Session "waify" already exists but could not be retrieved');
1112
+ }
1113
+ sessionId = existing.id;
1114
+ } else if (!sessionRes.ok) {
1092
1115
  throw new Error(`Failed to create session: ${sessionRes.status} ${sessionRes.statusText}`);
1116
+ } else {
1117
+ const sessionData = SessionResponseSchema.parse(await sessionRes.json());
1118
+ sessionId = sessionData.id ?? sessionData.name ?? "waify";
1093
1119
  }
1094
- const sessionData = SessionResponseSchema.parse(await sessionRes.json());
1095
- const sessionId = sessionData.id ?? sessionData.name ?? "waify";
1096
1120
  console.warn("Starting WhatsApp engine...");
1097
1121
  const startRes = await fetchWithTimeout(`${baseUrl}/api/sessions/${sessionId}/start`, {
1098
1122
  method: "POST",
1099
1123
  headers: { "X-API-Key": openwaApiKey }
1100
1124
  }, 1e4);
1101
- if (!startRes.ok) {
1125
+ if (!startRes.ok && startRes.status !== 400) {
1102
1126
  throw new Error(`Failed to start session: ${startRes.status} ${startRes.statusText}`);
1103
1127
  }
1104
1128
  console.warn("Waiting for QR code...");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deckasoft/waify",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "description": "AI-powered daily message sender for WhatsApp, powered by OpenWA",
5
5
  "keywords": [
6
6
  "whatsapp",