@egdesk/next-api-plugin 1.5.13 → 1.5.15

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.
@@ -508,9 +508,11 @@ export async function renameTable(
508
508
  // ==========================================
509
509
 
510
510
  /**
511
- * Upload a file to a user data table row.
512
- * Data must be base64-encoded. Files <10KB are stored as uncompressed blobs,
513
- * 10-100KB as gzip-compressed blobs, >100KB on the filesystem via BucketManager.
511
+ * Upload a file attachment for a table row.
512
+ * Files are NOT table columns — they attach via a virtual columnName (e.g. "file").
513
+ * Workflow: insertRows(...) → uploadFile(tableName, rowId, 'file', filename, base64).
514
+ * Data must be base64 (raw or data URL). Files <10KB store as uncompressed blobs,
515
+ * 10-100KB as gzip blobs, >100KB on disk via BucketManager.
514
516
  */
515
517
  export async function uploadFile(
516
518
  tableName: string,
@@ -520,7 +522,7 @@ export async function uploadFile(
520
522
  data: string,
521
523
  options: {
522
524
  mimeType?: string;
523
- forceStorageType?: 'blob' | 'file';
525
+ forceStorageType?: 'blob' | 'filesystem';
524
526
  compress?: boolean;
525
527
  } = {}
526
528
  ) {
@@ -2891,6 +2893,372 @@ export async function setGeminiApiKey(options: {
2891
2893
  export async function listGeminiApiKeys() {
2892
2894
  return listApiKeys({ provider: 'google' });
2893
2895
  }
2896
+
2897
+ // ==========================================
2898
+ // Phone / Google Messages (MCP)
2899
+ // ==========================================
2900
+
2901
+ /**
2902
+ * Call EGDesk Phone MCP tool (Messages Web devices, inbox, SMS queue, consent).
2903
+ *
2904
+ * - Server: \`POST {apiUrl}/phone/tools/call\`
2905
+ * - Client: \`POST /__phone_proxy\` (see proxy.ts / middleware)
2906
+ */
2907
+ export async function callPhoneTool(
2908
+ toolName: string,
2909
+ args: Record<string, any> = {}
2910
+ ): Promise<any> {
2911
+ const body = JSON.stringify({ tool: toolName, arguments: args });
2912
+ const isServer = typeof window === 'undefined';
2913
+
2914
+ let response: Response;
2915
+ if (isServer) {
2916
+ const apiUrl =
2917
+ (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_API_URL) ||
2918
+ EGDESK_CONFIG.apiUrl;
2919
+ response = await fetch(\`\${apiUrl}/phone/tools/call\`, {
2920
+ method: 'POST',
2921
+ headers: buildServerEgdeskHeaders(),
2922
+ body
2923
+ });
2924
+ } else {
2925
+ response = await apiFetch('/__phone_proxy', {
2926
+ method: 'POST',
2927
+ headers: { 'Content-Type': 'application/json' },
2928
+ body
2929
+ });
2930
+ }
2931
+
2932
+ return parseEgdeskMcpToolResponse(response);
2933
+ }
2934
+
2935
+ export async function listPhoneDevices() {
2936
+ return callPhoneTool('phone_list_devices', {});
2937
+ }
2938
+
2939
+ export async function createPhoneDevice(options: {
2940
+ label: string;
2941
+ id?: string;
2942
+ googleProfileName?: string;
2943
+ }) {
2944
+ return callPhoneTool('phone_create_device', options);
2945
+ }
2946
+
2947
+ export async function updatePhoneDevice(options: {
2948
+ deviceId: string;
2949
+ label?: string;
2950
+ googleProfileName?: string | null;
2951
+ }) {
2952
+ return callPhoneTool('phone_update_device', options);
2953
+ }
2954
+
2955
+ export async function deletePhoneDevice(deviceId: string) {
2956
+ return callPhoneTool('phone_delete_device', { deviceId });
2957
+ }
2958
+
2959
+ export async function connectPhoneDevice(deviceId: string) {
2960
+ return callPhoneTool('phone_connect', { deviceId });
2961
+ }
2962
+
2963
+ export async function checkPhoneDevice(deviceId: string) {
2964
+ return callPhoneTool('phone_check', { deviceId });
2965
+ }
2966
+
2967
+ export async function listPhoneConversations(deviceId: string) {
2968
+ return callPhoneTool('phone_list_conversations', { deviceId });
2969
+ }
2970
+
2971
+ export async function syncPhoneConversations(deviceId: string) {
2972
+ return callPhoneTool('phone_sync_conversations', { deviceId });
2973
+ }
2974
+
2975
+ export async function listPhoneConversationMessages(options: {
2976
+ deviceId: string;
2977
+ convKey: string;
2978
+ }) {
2979
+ return callPhoneTool('phone_list_conversation_messages', options);
2980
+ }
2981
+
2982
+ export async function syncPhoneConversationThread(options: {
2983
+ deviceId: string;
2984
+ convKey: string;
2985
+ title: string;
2986
+ }) {
2987
+ return callPhoneTool('phone_sync_conversation_thread', options);
2988
+ }
2989
+
2990
+ export async function listPhoneContacts(deviceId: string) {
2991
+ return callPhoneTool('phone_list_contacts', { deviceId });
2992
+ }
2993
+
2994
+ export async function syncPhoneContacts(deviceId: string) {
2995
+ return callPhoneTool('phone_sync_contacts', { deviceId });
2996
+ }
2997
+
2998
+ export async function sendPhoneSms(options: {
2999
+ deviceId: string;
3000
+ phoneNumber: string;
3001
+ message: string;
3002
+ snapshotId?: string;
3003
+ scheduledAt?: number;
3004
+ isMarketing?: boolean;
3005
+ brandName?: string;
3006
+ }) {
3007
+ return callPhoneTool('phone_send', options);
3008
+ }
3009
+
3010
+ export async function listPhoneQueue(options: {
3011
+ deviceId?: string;
3012
+ snapshotId?: string;
3013
+ limit?: number;
3014
+ } = {}) {
3015
+ return callPhoneTool('phone_list_queue', options);
3016
+ }
3017
+
3018
+ export async function cancelPhoneSend(jobId: string) {
3019
+ return callPhoneTool('phone_cancel_send', { jobId });
3020
+ }
3021
+
3022
+ export async function retryPhoneSend(jobId: string) {
3023
+ return callPhoneTool('phone_retry_send', { jobId });
3024
+ }
3025
+
3026
+ export async function listPhoneDevicesForSnapshot(snapshotId: string) {
3027
+ return callPhoneTool('phone_list_for_snapshot', { snapshotId });
3028
+ }
3029
+
3030
+ export async function linkPhoneSnapshot(options: {
3031
+ snapshotId: string;
3032
+ deviceId: string;
3033
+ }) {
3034
+ return callPhoneTool('phone_link_snapshot', options);
3035
+ }
3036
+
3037
+ export async function unlinkPhoneSnapshot(options: {
3038
+ snapshotId: string;
3039
+ deviceId: string;
3040
+ }) {
3041
+ return callPhoneTool('phone_unlink_snapshot', options);
3042
+ }
3043
+
3044
+ export async function getPhoneConsentLink(snapshotId: string) {
3045
+ return callPhoneTool('phone_consent_link_get', { snapshotId });
3046
+ }
3047
+
3048
+ export async function createPhoneConsentLink(options: {
3049
+ snapshotId: string;
3050
+ brandName?: string;
3051
+ optOutPhone?: string;
3052
+ }) {
3053
+ return callPhoneTool('phone_consent_link_create', options);
3054
+ }
3055
+
3056
+ export async function syncPhoneConsentEvents(snapshotId: string) {
3057
+ return callPhoneTool('phone_consent_events_sync', { snapshotId });
3058
+ }
3059
+
3060
+ // ==========================================
3061
+ // INSTAGRAM (MCP)
3062
+ // ==========================================
3063
+ //
3064
+ // Login model: each connection stores a login id (email/username) + password
3065
+ // (or a shared googleProfileName), plus an optional public @handle used for
3066
+ // profile URLs. Posting and stats sync drive real Instagram via Playwright,
3067
+ // so they are slower (~seconds to ~1-2 min) than plain data reads.
3068
+
3069
+ /**
3070
+ * Call EGDesk Instagram MCP tool.
3071
+ *
3072
+ * - Server: \`POST {apiUrl}/instagram/tools/call\`
3073
+ * - Client: \`POST /__instagram_proxy\` (see proxy.ts / middleware)
3074
+ */
3075
+ export async function callInstagramTool(
3076
+ toolName: string,
3077
+ args: Record<string, any> = {}
3078
+ ): Promise<any> {
3079
+ const body = JSON.stringify({ tool: toolName, arguments: args });
3080
+
3081
+ const isServer = typeof window === 'undefined';
3082
+
3083
+ let response: Response;
3084
+ if (isServer) {
3085
+ const apiUrl =
3086
+ (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_EGDESK_API_URL) ||
3087
+ EGDESK_CONFIG.apiUrl;
3088
+ response = await fetch(\`\${apiUrl}/instagram/tools/call\`, {
3089
+ method: 'POST',
3090
+ headers: buildServerEgdeskHeaders(),
3091
+ body
3092
+ });
3093
+ } else {
3094
+ response = await apiFetch('/__instagram_proxy', {
3095
+ method: 'POST',
3096
+ headers: { 'Content-Type': 'application/json' },
3097
+ body
3098
+ });
3099
+ }
3100
+
3101
+ return parseEgdeskMcpToolResponse(response);
3102
+ }
3103
+
3104
+ export type InstagramConnection = {
3105
+ id: string;
3106
+ name: string;
3107
+ username: string;
3108
+ /** Public @handle (no leading @) used for profile URLs, or null. */
3109
+ handle: string | null;
3110
+ hasPassword: boolean;
3111
+ hasAccessToken: boolean;
3112
+ createdAt: string;
3113
+ updatedAt: string;
3114
+ };
3115
+
3116
+ /** List Instagram connections saved in EGDesk. Passwords are never returned. */
3117
+ export async function listInstagramConnections(): Promise<{
3118
+ success: boolean;
3119
+ connections: InstagramConnection[];
3120
+ }> {
3121
+ return callInstagramTool('instagram_list_connections', {});
3122
+ }
3123
+
3124
+ /** Save a new Instagram connection, or update an existing one by (username or id). */
3125
+ export async function saveInstagramConnection(options: {
3126
+ name: string;
3127
+ /** Login id (email, phone, or username) */
3128
+ username: string;
3129
+ password: string;
3130
+ /** Public Instagram @handle (not email) */
3131
+ handle?: string;
3132
+ /** Optional existing connection id */
3133
+ id?: string;
3134
+ }) {
3135
+ return callInstagramTool('instagram_save_connection', options);
3136
+ }
3137
+
3138
+ /** Update fields on an Instagram connection (name, login, password, handle). */
3139
+ export async function updateInstagramConnection(options: {
3140
+ connectionId: string;
3141
+ name?: string;
3142
+ username?: string;
3143
+ password?: string;
3144
+ handle?: string;
3145
+ }) {
3146
+ return callInstagramTool('instagram_update_connection', options);
3147
+ }
3148
+
3149
+ /** Delete an Instagram connection by id. */
3150
+ export async function deleteInstagramConnection(connectionId: string) {
3151
+ return callInstagramTool('instagram_delete_connection', { connectionId });
3152
+ }
3153
+
3154
+ export type InstagramPublishHistorySource = 'schedule' | 'debug' | 'manual';
3155
+
3156
+ export type InstagramPublishHistoryEntry = {
3157
+ id: string;
3158
+ source: InstagramPublishHistorySource;
3159
+ status: 'success' | 'failure';
3160
+ connectionId?: string | null;
3161
+ connectionName?: string | null;
3162
+ username?: string | null;
3163
+ scheduleId?: string | null;
3164
+ scheduleTitle?: string | null;
3165
+ title?: string | null;
3166
+ caption?: string | null;
3167
+ imagePath?: string | null;
3168
+ postUrl?: string | null;
3169
+ shortcode?: string | null;
3170
+ likes?: number | null;
3171
+ comments?: number | null;
3172
+ views?: number | null;
3173
+ statsScrapedAt?: string | null;
3174
+ errorMessage?: string | null;
3175
+ startedAt: string;
3176
+ completedAt: string;
3177
+ durationMs?: number;
3178
+ };
3179
+
3180
+ /** List Instagram publish history (scheduled runs + debug/manual posts) with likes/comments when synced. */
3181
+ export async function listInstagramHistory(options: {
3182
+ connectionId?: string;
3183
+ /** 'success' | 'failure' */
3184
+ status?: string;
3185
+ /** Max rows (default 100, max 500) */
3186
+ limit?: number;
3187
+ } = {}): Promise<{ success: boolean; total: number; history: InstagramPublishHistoryEntry[] }> {
3188
+ return callInstagramTool('instagram_list_history', options);
3189
+ }
3190
+
3191
+ /**
3192
+ * Login (headed, background window) and scrape recent posts for likes/comments;
3193
+ * merge into publish history. Slow (~1-2 min).
3194
+ */
3195
+ export async function syncInstagramPostStats(options: {
3196
+ connectionId: string;
3197
+ /** Max posts to open (default 12, max 24) */
3198
+ limit?: number;
3199
+ /** Parallel tabs (default 8, max 8) */
3200
+ concurrency?: number;
3201
+ }) {
3202
+ return callInstagramTool('instagram_sync_post_stats', options);
3203
+ }
3204
+
3205
+ /** Alias of syncInstagramPostStats — scrape recent posts with engagement. */
3206
+ export async function fetchInstagramPosts(options: {
3207
+ connectionId: string;
3208
+ limit?: number;
3209
+ }) {
3210
+ return callInstagramTool('instagram_fetch_posts', options);
3211
+ }
3212
+
3213
+ /**
3214
+ * Publish an Instagram post via Playwright (login + create). Requires a local
3215
+ * imagePath (absolute path on the EGDesk machine) and caption. Records publish history.
3216
+ */
3217
+ export async function createInstagramPost(options: {
3218
+ connectionId: string;
3219
+ caption: string;
3220
+ /** Absolute path to an image on the EGDesk machine */
3221
+ imagePath: string;
3222
+ /** Ms to wait after Share (default 8000) */
3223
+ waitAfterShare?: number;
3224
+ }) {
3225
+ return callInstagramTool('instagram_create_post', options);
3226
+ }
3227
+
3228
+ /** Debug publish: post fixed/default caption with ~/Downloads/cat.png (or imagePath). No Gemini. */
3229
+ export async function debugInstagramPost(options: {
3230
+ connectionId: string;
3231
+ caption?: string;
3232
+ imagePath?: string;
3233
+ }) {
3234
+ return callInstagramTool('instagram_debug_post', options);
3235
+ }
3236
+
3237
+ export type InstagramSchedule = {
3238
+ id: string;
3239
+ title: string;
3240
+ connectionId: string;
3241
+ connectionName: string;
3242
+ connectionType: string;
3243
+ enabled: boolean;
3244
+ frequencyType: string;
3245
+ frequencyValue: unknown;
3246
+ scheduledTime: string;
3247
+ nextRun?: string | null;
3248
+ lastRun?: string | null;
3249
+ runCount?: number;
3250
+ successCount?: number;
3251
+ failureCount?: number;
3252
+ topics?: unknown;
3253
+ };
3254
+
3255
+ /** List Instagram scheduled posts for a connection, or all Instagram schedules when omitted. */
3256
+ export async function listInstagramSchedules(connectionId?: string): Promise<{
3257
+ success: boolean;
3258
+ schedules: InstagramSchedule[];
3259
+ }> {
3260
+ return callInstagramTool('instagram_list_schedules', connectionId ? { connectionId } : {});
3261
+ }
2894
3262
  `;
2895
3263
  fs.writeFileSync(helperPath, helperContent.replace(/\r?\n/g, os.EOL), 'utf-8');
2896
3264
  console.log(`✅ Generated ${helperPath}`);
@@ -75,7 +75,7 @@ import type { NextRequest } from 'next/server';
75
75
  /**
76
76
  * EGDesk Database Proxy (Next.js 16+)
77
77
  *
78
- * Intercepts __user_data_proxy, __browser_recording_proxy, __internal_knowledge_proxy, __company_research_proxy, __ai_center_proxy, __korean_law_proxy, __seo_proxy, __ssl_proxy, __local_agent_proxy, __pageindex_proxy, __drive_proxy, __knowledge_wiki_proxy, __egdesk_config_proxy, __ai_caller_proxy, and __kakao_proxy requests
78
+ * Intercepts __user_data_proxy, __browser_recording_proxy, __internal_knowledge_proxy, __company_research_proxy, __ai_center_proxy, __korean_law_proxy, __seo_proxy, __ssl_proxy, __local_agent_proxy, __pageindex_proxy, __drive_proxy, __knowledge_wiki_proxy, __egdesk_config_proxy, __ai_caller_proxy, __kakao_proxy, __phone_proxy, and __instagram_proxy requests
79
79
  * and forwards them to the EGDesk MCP HTTP server. This avoids CORS in local and tunneled environments.
80
80
  *
81
81
  * Generated by @egdesk/next-api-plugin
@@ -384,6 +384,50 @@ export async function proxy(request: NextRequest) {
384
384
  }
385
385
  }
386
386
 
387
+ if (pathname.includes('__phone_proxy')) {
388
+ try {
389
+ const body = await request.text();
390
+ const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
391
+
392
+ const response = await fetch(\`\${apiUrl}/phone/tools/call\`, {
393
+ method: 'POST',
394
+ headers: buildEgdeskHeaders(),
395
+ body,
396
+ });
397
+
398
+ const result = await response.json();
399
+
400
+ return NextResponse.json(result, { status: response.status });
401
+ } catch (error: any) {
402
+ return NextResponse.json(
403
+ { error: 'Proxy error', message: error.message },
404
+ { status: 500 }
405
+ );
406
+ }
407
+ }
408
+
409
+ if (pathname.includes('__instagram_proxy')) {
410
+ try {
411
+ const body = await request.text();
412
+ const apiUrl = process.env.NEXT_PUBLIC_EGDESK_API_URL || 'http://localhost:8080';
413
+
414
+ const response = await fetch(\`\${apiUrl}/instagram/tools/call\`, {
415
+ method: 'POST',
416
+ headers: buildEgdeskHeaders(),
417
+ body,
418
+ });
419
+
420
+ const result = await response.json();
421
+
422
+ return NextResponse.json(result, { status: response.status });
423
+ } catch (error: any) {
424
+ return NextResponse.json(
425
+ { error: 'Proxy error', message: error.message },
426
+ { status: 500 }
427
+ );
428
+ }
429
+ }
430
+
387
431
  if (pathname.includes('__egdesk_config_proxy')) {
388
432
  try {
389
433
  const body = await request.text();
@@ -436,7 +480,7 @@ export const config = {
436
480
  matcher: [
437
481
  /*
438
482
  * Match all paths except static assets
439
- * The proxy function filters for __user_data_proxy / __browser_recording_proxy / __internal_knowledge_proxy / __company_research_proxy / __ai_center_proxy / __korean_law_proxy / __seo_proxy / __ssl_proxy / __local_agent_proxy / __pageindex_proxy / __drive_proxy / __knowledge_wiki_proxy / __egdesk_config_proxy / __ai_caller_proxy / __kakao_proxy
483
+ * The proxy function filters for __user_data_proxy / __browser_recording_proxy / __internal_knowledge_proxy / __company_research_proxy / __ai_center_proxy / __korean_law_proxy / __seo_proxy / __ssl_proxy / __local_agent_proxy / __pageindex_proxy / __drive_proxy / __knowledge_wiki_proxy / __egdesk_config_proxy / __ai_caller_proxy / __kakao_proxy / __phone_proxy / __instagram_proxy
440
484
  */
441
485
  '/:path*',
442
486
  ],
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * The minimum EGDesk app version this plugin version is compatible with.
9
9
  * The app checks this at setup time and warns on mismatch.
10
10
  */
11
- export declare const COMPATIBLE_APP_VERSION = "1.4.24";
11
+ export declare const COMPATIBLE_APP_VERSION = "1.4.28";
12
12
  export interface SetupOptions {
13
13
  egdeskUrl?: string;
14
14
  /** Production EGDesk server URL — written to .env.production.local. Falls back to egdeskUrl. */
package/dist/index.js CHANGED
@@ -46,7 +46,7 @@ exports.setupNextApiPlugin = setupNextApiPlugin;
46
46
  * The minimum EGDesk app version this plugin version is compatible with.
47
47
  * The app checks this at setup time and warns on mismatch.
48
48
  */
49
- exports.COMPATIBLE_APP_VERSION = '1.4.24';
49
+ exports.COMPATIBLE_APP_VERSION = '1.4.28';
50
50
  const fs = __importStar(require("fs"));
51
51
  const path = __importStar(require("path"));
52
52
  const setup_userdata_1 = require("./setup-userdata");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@egdesk/next-api-plugin",
3
- "version": "1.5.13",
3
+ "version": "1.5.15",
4
4
  "description": "Next.js plugin for EGDesk database proxy integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",