@hasna/connectors 1.3.23 → 1.3.25
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/bin/index.js +492 -153
- package/bin/mcp.js +1078 -595
- package/bin/serve.js +774 -436
- package/connectors/connect-cloudflare/bun.lock +32 -0
- package/connectors/connect-gmail/bin/index.js +7027 -0
- package/connectors/connect-googledrive/bin/index.js +5958 -0
- package/connectors/connect-imessage/bin/.gitkeep +2 -0
- package/connectors/connect-imessage/bin/index.js +3016 -0
- package/connectors/connect-imessage/bun.lock +32 -0
- package/connectors/connect-imessage/package.json +46 -0
- package/connectors/connect-imessage/src/api/client.ts +139 -0
- package/connectors/connect-imessage/src/api/conversations.ts +49 -0
- package/connectors/connect-imessage/src/api/health.ts +16 -0
- package/connectors/connect-imessage/src/api/index.ts +62 -0
- package/connectors/connect-imessage/src/api/messages.ts +119 -0
- package/connectors/connect-imessage/src/cli/index.ts +258 -0
- package/connectors/connect-imessage/src/index.ts +26 -0
- package/connectors/connect-imessage/src/types/index.ts +134 -0
- package/connectors/connect-imessage/src/utils/config.ts +219 -0
- package/connectors/connect-imessage/src/utils/output.ts +121 -0
- package/connectors/connect-imessage/tsconfig.json +16 -0
- package/dist/core/builtins.test.d.ts +1 -0
- package/dist/core/connectors/googledrive.d.ts +6 -0
- package/dist/core/connectors/internal-operations.test.d.ts +1 -0
- package/dist/core/connectors/internal-runtime.test.d.ts +1 -0
- package/dist/core/errors.test.d.ts +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +640 -86
- package/dist/lib/runner-auth.test.d.ts +1 -0
- package/dist/lib/runner.d.ts +31 -0
- package/dist/lib/test-endpoints.test.d.ts +1 -0
- package/dist/lib/workflow-runner.d.ts +7 -0
- package/dist/server/auth-exchange.test.d.ts +1 -0
- package/dist/server/auth-refresh.test.d.ts +1 -0
- package/dist/server/auth.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { IMessage } from '../api/index';
|
|
5
|
+
import {
|
|
6
|
+
getBridgeUrl,
|
|
7
|
+
setBridgeUrl,
|
|
8
|
+
setApiKey,
|
|
9
|
+
setDeviceId,
|
|
10
|
+
getCurrentProfile,
|
|
11
|
+
setCurrentProfile,
|
|
12
|
+
listProfiles,
|
|
13
|
+
createProfile,
|
|
14
|
+
deleteProfile,
|
|
15
|
+
loadProfile,
|
|
16
|
+
getConfigDir,
|
|
17
|
+
} from '../utils/config';
|
|
18
|
+
import { output } from '../utils/output';
|
|
19
|
+
|
|
20
|
+
const program = new Command();
|
|
21
|
+
|
|
22
|
+
program
|
|
23
|
+
.name('connect-imessage')
|
|
24
|
+
.description('iMessage connector CLI - bridge-first iMessage transport')
|
|
25
|
+
.version('0.0.1')
|
|
26
|
+
.option('-p, --profile <name>', 'Profile to use')
|
|
27
|
+
.option('-f, --format <format>', 'Output format: json, pretty', 'pretty');
|
|
28
|
+
|
|
29
|
+
// ============================================
|
|
30
|
+
// Profile
|
|
31
|
+
// ============================================
|
|
32
|
+
|
|
33
|
+
const profileCmd = program.command('profile').description('Manage profiles');
|
|
34
|
+
|
|
35
|
+
profileCmd
|
|
36
|
+
.command('list')
|
|
37
|
+
.description('List all profiles')
|
|
38
|
+
.action(() => {
|
|
39
|
+
const profiles = listProfiles();
|
|
40
|
+
const current = getCurrentProfile();
|
|
41
|
+
for (const name of profiles) {
|
|
42
|
+
const marker = name === current ? chalk.green('* ') : ' ';
|
|
43
|
+
console.log(marker + name);
|
|
44
|
+
}
|
|
45
|
+
if (profiles.length === 0) {
|
|
46
|
+
console.log(chalk.dim('No profiles found'));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
profileCmd
|
|
51
|
+
.command('create <name>')
|
|
52
|
+
.description('Create a new profile')
|
|
53
|
+
.action((name: string) => {
|
|
54
|
+
createProfile(name);
|
|
55
|
+
console.log(chalk.green('Created profile: ' + name));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
profileCmd
|
|
59
|
+
.command('delete <name>')
|
|
60
|
+
.description('Delete a profile')
|
|
61
|
+
.action((name: string) => {
|
|
62
|
+
if (deleteProfile(name)) {
|
|
63
|
+
console.log(chalk.green('Deleted profile: ' + name));
|
|
64
|
+
} else {
|
|
65
|
+
console.log(chalk.red('Failed to delete profile: ' + name));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
profileCmd
|
|
70
|
+
.command('use <name>')
|
|
71
|
+
.description('Switch to a profile')
|
|
72
|
+
.action((name: string) => {
|
|
73
|
+
setCurrentProfile(name);
|
|
74
|
+
console.log(chalk.green('Now using profile: ' + name));
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// ============================================
|
|
78
|
+
// Config
|
|
79
|
+
// ============================================
|
|
80
|
+
|
|
81
|
+
const configCmd = program.command('config').description('Configure settings');
|
|
82
|
+
|
|
83
|
+
configCmd
|
|
84
|
+
.command('set <key> <value>')
|
|
85
|
+
.description('Set a config value (bridgeUrl, apiKey, deviceId)')
|
|
86
|
+
.action((key: string, value: string) => {
|
|
87
|
+
if (key === 'bridgeUrl') setBridgeUrl(value);
|
|
88
|
+
else if (key === 'apiKey') setApiKey(value);
|
|
89
|
+
else if (key === 'deviceId') setDeviceId(value);
|
|
90
|
+
else {
|
|
91
|
+
console.log(chalk.red('Unknown key: ' + key));
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
console.log(chalk.green('Set ' + key));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
configCmd
|
|
98
|
+
.command('show')
|
|
99
|
+
.description('Show current configuration')
|
|
100
|
+
.action(() => {
|
|
101
|
+
const config = loadProfile();
|
|
102
|
+
const profile = getCurrentProfile();
|
|
103
|
+
console.log(chalk.bold('Profile:'), profile);
|
|
104
|
+
console.log(chalk.bold('Config dir:'), getConfigDir());
|
|
105
|
+
console.log('');
|
|
106
|
+
console.log(chalk.bold('Settings:'));
|
|
107
|
+
console.log(' bridgeUrl:', config.bridgeUrl || '(not set)');
|
|
108
|
+
console.log(' apiKey:', config.apiKey ? '****' : '(not set)');
|
|
109
|
+
console.log(' deviceId:', config.deviceId || '(not set)');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// ============================================
|
|
113
|
+
// Health
|
|
114
|
+
// ============================================
|
|
115
|
+
|
|
116
|
+
program
|
|
117
|
+
.command('health')
|
|
118
|
+
.description('Check bridge health')
|
|
119
|
+
.action(async () => {
|
|
120
|
+
const client = createClient();
|
|
121
|
+
try {
|
|
122
|
+
const health = await client.health.check();
|
|
123
|
+
output(health, getFormat());
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.log(chalk.red('Health check failed: ' + (error as Error).message));
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ============================================
|
|
131
|
+
// Conversations
|
|
132
|
+
// ============================================
|
|
133
|
+
|
|
134
|
+
const convCmd = program.command('conversation').description('Manage conversations');
|
|
135
|
+
|
|
136
|
+
convCmd
|
|
137
|
+
.command('list')
|
|
138
|
+
.description('List conversations')
|
|
139
|
+
.option('-l, --limit <n>', 'Max conversations to return')
|
|
140
|
+
.action(async (opts: { limit?: string }) => {
|
|
141
|
+
const client = createClient();
|
|
142
|
+
try {
|
|
143
|
+
const convs = await client.conversations.list({
|
|
144
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
145
|
+
});
|
|
146
|
+
output(convs, getFormat());
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.log(chalk.red('Failed to list conversations: ' + (error as Error).message));
|
|
149
|
+
process.exit(1);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
convCmd
|
|
154
|
+
.command('get <chatGuid>')
|
|
155
|
+
.description('Get a conversation by GUID')
|
|
156
|
+
.action(async (chatGuid: string) => {
|
|
157
|
+
const client = createClient();
|
|
158
|
+
try {
|
|
159
|
+
const conv = await client.conversations.get(chatGuid);
|
|
160
|
+
output(conv, getFormat());
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.log(chalk.red('Failed to get conversation: ' + (error as Error).message));
|
|
163
|
+
process.exit(1);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ============================================
|
|
168
|
+
// Messages
|
|
169
|
+
// ============================================
|
|
170
|
+
|
|
171
|
+
const msgCmd = program.command('message').description('Manage messages');
|
|
172
|
+
|
|
173
|
+
msgCmd
|
|
174
|
+
.command('list <chatGuid>')
|
|
175
|
+
.description('List messages in a conversation')
|
|
176
|
+
.option('-l, --limit <n>', 'Max messages to return')
|
|
177
|
+
.action(async (chatGuid: string, opts: { limit?: string }) => {
|
|
178
|
+
const client = createClient();
|
|
179
|
+
try {
|
|
180
|
+
const msgs = await client.messages.list({
|
|
181
|
+
chatGuid,
|
|
182
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
183
|
+
});
|
|
184
|
+
output(msgs, getFormat());
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.log(chalk.red('Failed to list messages: ' + (error as Error).message));
|
|
187
|
+
process.exit(1);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
msgCmd
|
|
192
|
+
.command('send')
|
|
193
|
+
.description('Send a message')
|
|
194
|
+
.requiredOption('-r, --recipient <handle>', 'Recipient handle or chat GUID')
|
|
195
|
+
.requiredOption('-t, --text <text>', 'Message text')
|
|
196
|
+
.action(async (opts: { recipient: string; text: string }) => {
|
|
197
|
+
const client = createClient();
|
|
198
|
+
try {
|
|
199
|
+
const msg = await client.messages.send({
|
|
200
|
+
chatGuid: opts.recipient.includes('chat=') ? opts.recipient : undefined,
|
|
201
|
+
recipient: opts.recipient.includes('chat=') ? undefined : opts.recipient,
|
|
202
|
+
text: opts.text,
|
|
203
|
+
});
|
|
204
|
+
output(msg, getFormat());
|
|
205
|
+
} catch (error) {
|
|
206
|
+
console.log(chalk.red('Failed to send message: ' + (error as Error).message));
|
|
207
|
+
process.exit(1);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
msgCmd
|
|
212
|
+
.command('reply <messageGuid>')
|
|
213
|
+
.description('Reply to a specific message')
|
|
214
|
+
.requiredOption('-t, --text <text>', 'Reply text')
|
|
215
|
+
.action(async (messageGuid: string, opts: { text: string }) => {
|
|
216
|
+
const client = createClient();
|
|
217
|
+
try {
|
|
218
|
+
const msg = await client.messages.reply(messageGuid, {
|
|
219
|
+
text: opts.text,
|
|
220
|
+
selectedMessageGuid: messageGuid,
|
|
221
|
+
});
|
|
222
|
+
output(msg, getFormat());
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.log(chalk.red('Failed to reply: ' + (error as Error).message));
|
|
225
|
+
process.exit(1);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// ============================================
|
|
230
|
+
// Helpers
|
|
231
|
+
// ============================================
|
|
232
|
+
|
|
233
|
+
function createClient(): IMessage {
|
|
234
|
+
const bridgeUrl = getBridgeUrl();
|
|
235
|
+
if (!bridgeUrl) {
|
|
236
|
+
console.log(chalk.red('Error: bridgeUrl not configured'));
|
|
237
|
+
console.log(' Set it with: connect-imessage config set bridgeUrl <url>');
|
|
238
|
+
console.log(' Or set IMESSAGE_BRIDGE_URL env var');
|
|
239
|
+
process.exit(1);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return new IMessage({
|
|
243
|
+
bridgeUrl,
|
|
244
|
+
apiKey: process.env.IMESSAGE_API_KEY,
|
|
245
|
+
deviceId: process.env.IMESSAGE_DEVICE_ID,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function getFormat(): 'json' | 'pretty' {
|
|
250
|
+
const format = program.opts().format;
|
|
251
|
+
return format === 'json' ? 'json' : 'pretty';
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Parse async
|
|
255
|
+
program.parseAsync().catch((err) => {
|
|
256
|
+
console.error(err);
|
|
257
|
+
process.exit(1);
|
|
258
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// iMessage Connector
|
|
2
|
+
// TypeScript wrapper for iMessage via bridge API
|
|
3
|
+
|
|
4
|
+
export { IMessage } from './api';
|
|
5
|
+
export * from './types';
|
|
6
|
+
|
|
7
|
+
// Re-export individual API classes for advanced usage
|
|
8
|
+
export { ImessageClient, HealthApi, ConversationsApi, MessagesApi } from './api';
|
|
9
|
+
|
|
10
|
+
// Export config utilities
|
|
11
|
+
export {
|
|
12
|
+
getBridgeUrl,
|
|
13
|
+
getApiKey,
|
|
14
|
+
getDeviceId,
|
|
15
|
+
setBridgeUrl,
|
|
16
|
+
setApiKey,
|
|
17
|
+
setDeviceId,
|
|
18
|
+
getCurrentProfile,
|
|
19
|
+
setCurrentProfile,
|
|
20
|
+
listProfiles,
|
|
21
|
+
createProfile,
|
|
22
|
+
deleteProfile,
|
|
23
|
+
loadProfile,
|
|
24
|
+
saveProfile,
|
|
25
|
+
clearConfig,
|
|
26
|
+
} from './utils/config';
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// iMessage Connector Types
|
|
2
|
+
|
|
3
|
+
// ============================================
|
|
4
|
+
// Configuration
|
|
5
|
+
// ============================================
|
|
6
|
+
|
|
7
|
+
export interface IMessageConfig {
|
|
8
|
+
bridgeUrl: string;
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
deviceId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// ============================================
|
|
14
|
+
// Common Types
|
|
15
|
+
// ============================================
|
|
16
|
+
|
|
17
|
+
export type OutputFormat = 'json' | 'pretty';
|
|
18
|
+
|
|
19
|
+
// ============================================
|
|
20
|
+
// Health Types
|
|
21
|
+
// ============================================
|
|
22
|
+
|
|
23
|
+
export interface IMessageHealth {
|
|
24
|
+
status: 'healthy' | 'degraded' | 'unhealthy';
|
|
25
|
+
bridge: {
|
|
26
|
+
reachable: boolean;
|
|
27
|
+
version?: string;
|
|
28
|
+
platform?: string;
|
|
29
|
+
lastSeen?: string;
|
|
30
|
+
};
|
|
31
|
+
imessage: {
|
|
32
|
+
signedIn: boolean;
|
|
33
|
+
account?: string;
|
|
34
|
+
};
|
|
35
|
+
timestamp: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ============================================
|
|
39
|
+
// Conversation Types
|
|
40
|
+
// ============================================
|
|
41
|
+
|
|
42
|
+
export interface IMessageConversation {
|
|
43
|
+
id: string;
|
|
44
|
+
chatIdentifier: string;
|
|
45
|
+
displayName: string;
|
|
46
|
+
type: 'single' | 'group';
|
|
47
|
+
participants: IMessageParticipant[];
|
|
48
|
+
lastMessage?: IMessagePreview;
|
|
49
|
+
unreadCount?: number;
|
|
50
|
+
createdDate?: string;
|
|
51
|
+
lastMessageDate?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface IMessageParticipant {
|
|
55
|
+
handle: string;
|
|
56
|
+
name?: string;
|
|
57
|
+
isMe: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface IMessagePreview {
|
|
61
|
+
text: string;
|
|
62
|
+
fromMe: boolean;
|
|
63
|
+
date: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ============================================
|
|
67
|
+
// Message Types
|
|
68
|
+
// ============================================
|
|
69
|
+
|
|
70
|
+
export interface IMessage {
|
|
71
|
+
guid: string;
|
|
72
|
+
chatGuid: string;
|
|
73
|
+
text?: string;
|
|
74
|
+
attachments?: IMessageAttachment[];
|
|
75
|
+
fromMe: boolean;
|
|
76
|
+
handle?: string;
|
|
77
|
+
displayName?: string;
|
|
78
|
+
date: string;
|
|
79
|
+
dateRead?: string;
|
|
80
|
+
dateDelivered?: string;
|
|
81
|
+
error?: number;
|
|
82
|
+
isForward: boolean;
|
|
83
|
+
subject?: string;
|
|
84
|
+
threadOriginatorGuid?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface IMessageAttachment {
|
|
88
|
+
guid: string;
|
|
89
|
+
mimeType: string;
|
|
90
|
+
fileName?: string;
|
|
91
|
+
transferState?: string;
|
|
92
|
+
totalBytes?: number;
|
|
93
|
+
transferredBytes?: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ============================================
|
|
97
|
+
// Contact Types
|
|
98
|
+
// ============================================
|
|
99
|
+
|
|
100
|
+
export interface IMessageContact {
|
|
101
|
+
handle: string;
|
|
102
|
+
name?: string;
|
|
103
|
+
firstName?: string;
|
|
104
|
+
lastName?: string;
|
|
105
|
+
phoneNumbers?: string[];
|
|
106
|
+
emails?: string[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ============================================
|
|
110
|
+
// API Response Types
|
|
111
|
+
// ============================================
|
|
112
|
+
|
|
113
|
+
export interface IMessageApiResponse<T = unknown> {
|
|
114
|
+
ok: boolean;
|
|
115
|
+
data?: T;
|
|
116
|
+
error?: string;
|
|
117
|
+
code?: number;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ============================================
|
|
121
|
+
// API Error Types
|
|
122
|
+
// ============================================
|
|
123
|
+
|
|
124
|
+
export class IMessageApiError extends Error {
|
|
125
|
+
public readonly statusCode: number;
|
|
126
|
+
public readonly errorCode?: number;
|
|
127
|
+
|
|
128
|
+
constructor(message: string, statusCode: number, errorCode?: number) {
|
|
129
|
+
super(message);
|
|
130
|
+
this.name = 'IMessageApiError';
|
|
131
|
+
this.statusCode = statusCode;
|
|
132
|
+
this.errorCode = errorCode;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync } from 'fs';
|
|
2
|
+
import { homedir } from 'os';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
const CONNECTOR_NAME = 'connect-imessage';
|
|
6
|
+
const DEFAULT_PROFILE = 'default';
|
|
7
|
+
|
|
8
|
+
export interface ProfileConfig {
|
|
9
|
+
bridgeUrl?: string;
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
deviceId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Store for --profile flag override (set by CLI before commands run)
|
|
15
|
+
let profileOverride: string | undefined;
|
|
16
|
+
|
|
17
|
+
// Config directory: ~/.hasna/connectors/{connector-name}/
|
|
18
|
+
const CONFIG_DIR = join(homedir(), '.hasna', 'connectors', CONNECTOR_NAME);
|
|
19
|
+
const PROFILES_DIR = join(CONFIG_DIR, 'profiles');
|
|
20
|
+
const CURRENT_PROFILE_FILE = join(CONFIG_DIR, 'current_profile');
|
|
21
|
+
|
|
22
|
+
// ============================================
|
|
23
|
+
// Profile Management
|
|
24
|
+
// ============================================
|
|
25
|
+
|
|
26
|
+
export function setProfileOverride(profile: string | undefined): void {
|
|
27
|
+
profileOverride = profile;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function ensureConfigDir(): void {
|
|
31
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
32
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
33
|
+
}
|
|
34
|
+
if (!existsSync(PROFILES_DIR)) {
|
|
35
|
+
mkdirSync(PROFILES_DIR, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getProfilePath(profileName: string): string {
|
|
40
|
+
return join(PROFILES_DIR, profileName + '.json');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get the current active profile name
|
|
45
|
+
*/
|
|
46
|
+
export function getCurrentProfile(): string {
|
|
47
|
+
if (profileOverride) {
|
|
48
|
+
return profileOverride;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
ensureConfigDir();
|
|
52
|
+
|
|
53
|
+
if (existsSync(CURRENT_PROFILE_FILE)) {
|
|
54
|
+
try {
|
|
55
|
+
const profile = readFileSync(CURRENT_PROFILE_FILE, 'utf-8').trim();
|
|
56
|
+
if (profile && profileExists(profile)) {
|
|
57
|
+
return profile;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// Fall through to default
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return DEFAULT_PROFILE;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Set the current active profile
|
|
69
|
+
*/
|
|
70
|
+
export function setCurrentProfile(profile: string): void {
|
|
71
|
+
ensureConfigDir();
|
|
72
|
+
|
|
73
|
+
if (!profileExists(profile) && profile !== DEFAULT_PROFILE) {
|
|
74
|
+
throw new Error('Profile "' + profile + '" does not exist');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
writeFileSync(CURRENT_PROFILE_FILE, profile);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if a profile exists
|
|
82
|
+
*/
|
|
83
|
+
export function profileExists(profile: string): boolean {
|
|
84
|
+
return existsSync(getProfilePath(profile));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* List all available profiles
|
|
89
|
+
*/
|
|
90
|
+
export function listProfiles(): string[] {
|
|
91
|
+
ensureConfigDir();
|
|
92
|
+
|
|
93
|
+
if (!existsSync(PROFILES_DIR)) {
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return readdirSync(PROFILES_DIR)
|
|
98
|
+
.filter((f) => f.endsWith('.json'))
|
|
99
|
+
.map((f) => f.replace('.json', ''))
|
|
100
|
+
.sort();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create a new profile
|
|
105
|
+
*/
|
|
106
|
+
export function createProfile(profile: string, config: ProfileConfig = {}): boolean {
|
|
107
|
+
ensureConfigDir();
|
|
108
|
+
|
|
109
|
+
if (profileExists(profile)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Validate profile name
|
|
114
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(profile)) {
|
|
115
|
+
throw new Error('Profile name can only contain letters, numbers, hyphens, and underscores');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
writeFileSync(getProfilePath(profile), JSON.stringify(config, null, 2));
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Delete a profile
|
|
124
|
+
*/
|
|
125
|
+
export function deleteProfile(profile: string): boolean {
|
|
126
|
+
if (profile === DEFAULT_PROFILE) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!profileExists(profile)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Switch to default if deleting current profile
|
|
135
|
+
if (getCurrentProfile() === profile) {
|
|
136
|
+
setCurrentProfile(DEFAULT_PROFILE);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
rmSync(getProfilePath(profile));
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Load profile config
|
|
145
|
+
*/
|
|
146
|
+
export function loadProfile(profile?: string): ProfileConfig {
|
|
147
|
+
ensureConfigDir();
|
|
148
|
+
const profileName = profile || getCurrentProfile();
|
|
149
|
+
const profilePath = getProfilePath(profileName);
|
|
150
|
+
|
|
151
|
+
if (!existsSync(profilePath)) {
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
return JSON.parse(readFileSync(profilePath, 'utf-8')) as ProfileConfig;
|
|
157
|
+
} catch {
|
|
158
|
+
return {};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Save profile config
|
|
164
|
+
*/
|
|
165
|
+
export function saveProfile(config: ProfileConfig, profile?: string): void {
|
|
166
|
+
ensureConfigDir();
|
|
167
|
+
const profileName = profile || getCurrentProfile();
|
|
168
|
+
writeFileSync(getProfilePath(profileName), JSON.stringify(config, null, 2));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ============================================
|
|
172
|
+
// Config Accessors
|
|
173
|
+
// ============================================
|
|
174
|
+
|
|
175
|
+
export function getBridgeUrl(): string | undefined {
|
|
176
|
+
return process.env.IMESSAGE_BRIDGE_URL || loadProfile().bridgeUrl;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function getApiKey(): string | undefined {
|
|
180
|
+
return process.env.IMESSAGE_API_KEY || loadProfile().apiKey;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function getDeviceId(): string | undefined {
|
|
184
|
+
return process.env.IMESSAGE_DEVICE_ID || loadProfile().deviceId;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function setBridgeUrl(bridgeUrl: string): void {
|
|
188
|
+
const config = loadProfile();
|
|
189
|
+
config.bridgeUrl = bridgeUrl;
|
|
190
|
+
saveProfile(config);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function setApiKey(apiKey: string): void {
|
|
194
|
+
const config = loadProfile();
|
|
195
|
+
config.apiKey = apiKey;
|
|
196
|
+
saveProfile(config);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function setDeviceId(deviceId: string): void {
|
|
200
|
+
const config = loadProfile();
|
|
201
|
+
config.deviceId = deviceId;
|
|
202
|
+
saveProfile(config);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ============================================
|
|
206
|
+
// Utility Functions
|
|
207
|
+
// ============================================
|
|
208
|
+
|
|
209
|
+
export function clearConfig(): void {
|
|
210
|
+
saveProfile({});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function getConfigDir(): string {
|
|
214
|
+
return CONFIG_DIR;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function getActiveProfileName(): string {
|
|
218
|
+
return getCurrentProfile();
|
|
219
|
+
}
|