@jackle.dev/zalox-plugin 1.0.27 → 1.0.28
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/package.json +1 -1
- package/src/client.ts +42 -62
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -60,72 +60,52 @@ export async function getOrCreateApi(profile: string): Promise<CachedApiEntry> {
|
|
|
60
60
|
const zalo = new Zalo({ logging: false, imageMetadataGetter } as any);
|
|
61
61
|
const api = await zalo.login(credentials);
|
|
62
62
|
|
|
63
|
-
// Get own user info
|
|
64
|
-
let ownId = '';
|
|
63
|
+
// Get own user info with robust fallback
|
|
64
|
+
let ownId = String((credentials as any).uid || (credentials as any).id || (credentials as any).userId || '');
|
|
65
65
|
let displayName: string | undefined;
|
|
66
66
|
let avatar: string | undefined;
|
|
67
|
-
try {
|
|
68
|
-
const info = await api.fetchAccountInfo();
|
|
69
|
-
ownId = String((info as any).userId || '');
|
|
70
|
-
displayName = (info as any).displayName || (info as any).zaloName;
|
|
71
|
-
avatar = (info as any).avatar;
|
|
72
|
-
} catch {
|
|
73
|
-
// Try to get from context
|
|
74
|
-
try {
|
|
75
|
-
const ctx = await api.getContext();
|
|
76
|
-
ownId = String((ctx as any).uid || '');
|
|
77
|
-
} catch {}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const entry: CachedApiEntry = {
|
|
81
|
-
api,
|
|
82
|
-
ownId,
|
|
83
|
-
displayName,
|
|
84
|
-
avatar,
|
|
85
|
-
connectedAt: Date.now(),
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
apiCache.set(profile, entry);
|
|
89
|
-
return entry;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Get cached API instance (no login if not cached)
|
|
94
|
-
*/
|
|
95
|
-
export function getCachedApi(profile: string): CachedApiEntry | undefined {
|
|
96
|
-
return apiCache.get(profile);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Check if credentials exist for a profile (offline, no login)
|
|
101
|
-
*/
|
|
102
|
-
export function hasCredentials(profile: string): boolean {
|
|
103
|
-
return existsSync(resolveCredentialsPath(profile));
|
|
104
|
-
}
|
|
105
67
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
68
|
+
// Try API if credentials didn't have ID
|
|
69
|
+
if (!ownId || ownId === 'undefined') {
|
|
70
|
+
try {
|
|
71
|
+
if (typeof (api as any).getOwnId === 'function') {
|
|
72
|
+
const id = await (api as any).getOwnId();
|
|
73
|
+
if (id) ownId = String(id);
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
console.warn('[ZaloX] Failed to getOwnId from API:', e);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
116
79
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
80
|
+
// Try Context if still missing
|
|
81
|
+
if (!ownId || ownId === 'undefined') {
|
|
82
|
+
try {
|
|
83
|
+
const ctx = (api as any).context || (api as any).ctx || (zalo as any).context;
|
|
84
|
+
if (ctx && (ctx.uid || ctx.userId)) {
|
|
85
|
+
ownId = String(ctx.uid || ctx.userId);
|
|
86
|
+
}
|
|
87
|
+
} catch {}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Try to parse from Cookie as last resort
|
|
91
|
+
if ((!ownId || ownId === 'undefined') && credentials.cookie) {
|
|
92
|
+
const c = typeof credentials.cookie === 'string' ? credentials.cookie : JSON.stringify(credentials.cookie);
|
|
93
|
+
const match = c.match(/uid=([0-9]+)/) || c.match(/z_uuid=([a-zA-Z0-9]+)/);
|
|
94
|
+
if (match) ownId = match[1];
|
|
95
|
+
}
|
|
121
96
|
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
97
|
+
// Get display name
|
|
98
|
+
try {
|
|
99
|
+
if (typeof (api as any).fetchAccountInfo === 'function') {
|
|
100
|
+
const info = await (api as any).fetchAccountInfo();
|
|
101
|
+
if (info) {
|
|
102
|
+
displayName = (info as any).displayName || (info as any).zaloName;
|
|
103
|
+
avatar = (info as any).avatar;
|
|
104
|
+
if (!ownId) ownId = String((info as any).userId || '');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
} catch {}
|
|
125
108
|
|
|
126
|
-
|
|
127
|
-
const zcaPath = join(homedir(), '.config', 'zca-cli-nodejs', 'profiles', `${profile}.json`);
|
|
128
|
-
if (existsSync(zcaPath)) return zcaPath;
|
|
109
|
+
console.log(`[ZaloX] Logged in profile=${profile} ownId=${ownId}`);
|
|
129
110
|
|
|
130
|
-
return openclawPath; // Default to persistent .openclaw path
|
|
131
|
-
}
|
|
111
|
+
const entry: CachedApiEntry = {\n api,\n ownId,\n displayName,\n avatar,\n connectedAt: Date.now(),\n };\n\n apiCache.set(profile, entry);\n return entry;\n}\n\n/**\n * Get cached API instance (no login if not cached)\n */\nexport function getCachedApi(profile: string): CachedApiEntry | undefined {\n return apiCache.get(profile);\n}\n\n/**\n * Check if credentials exist for a profile (offline, no login)\n */\nexport function hasCredentials(profile: string): boolean {\n return existsSync(resolveCredentialsPath(profile));\n}\n\n/**\n * Clear cached API instance\n */\nexport function clearApi(profile: string): void {\n apiCache.delete(profile);\n}\n\nfunction resolveOpenClawDir(): string {\n return process.env.OPENCLAW_DIR || join(homedir(), '.openclaw');\n}\n\nexport function resolveCredentialsPath(profile: string): string {\n // 1. Persistent path inside .openclaw (survives container restarts)\n const openclawPath = join(resolveOpenClawDir(), 'zalox', 'profiles', `${profile}.json`);\n if (existsSync(openclawPath)) return openclawPath;\n\n // 2. Legacy ZaloX config path\n const zaloxPath = join(homedir(), '.config', 'zalox', 'profiles', `${profile}.json`);\n if (existsSync(zaloxPath)) return zaloxPath;\n\n // 3. Legacy zca-cli path\n const zcaPath = join(homedir(), '.config', 'zca-cli-nodejs', 'profiles', `${profile}.json`);\n if (existsSync(zcaPath)) return zcaPath;\n\n return openclawPath; // Default to persistent .openclaw path\n}\n
|