@mapier/imsg-sdk 0.1.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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -0
  3. package/dist/gateway/external-ids.d.ts +3 -0
  4. package/dist/gateway/external-ids.js +37 -0
  5. package/dist/gateway/external-ids.js.map +1 -0
  6. package/dist/gateway/fake.d.ts +161 -0
  7. package/dist/gateway/fake.js +870 -0
  8. package/dist/gateway/fake.js.map +1 -0
  9. package/dist/gateway/imsg.d.ts +77 -0
  10. package/dist/gateway/imsg.js +676 -0
  11. package/dist/gateway/imsg.js.map +1 -0
  12. package/dist/gateway/portable-chat.d.ts +65 -0
  13. package/dist/gateway/portable-chat.js +118 -0
  14. package/dist/gateway/portable-chat.js.map +1 -0
  15. package/dist/gateway/types.d.ts +169 -0
  16. package/dist/gateway/types.js +16 -0
  17. package/dist/gateway/types.js.map +1 -0
  18. package/dist/imsg/binary.d.ts +1 -0
  19. package/dist/imsg/binary.js +4 -0
  20. package/dist/imsg/binary.js.map +1 -0
  21. package/dist/imsg/react.d.ts +2 -0
  22. package/dist/imsg/react.js +24 -0
  23. package/dist/imsg/react.js.map +1 -0
  24. package/dist/imsg/rpc.d.ts +145 -0
  25. package/dist/imsg/rpc.js +227 -0
  26. package/dist/imsg/rpc.js.map +1 -0
  27. package/dist/imsg/status.d.ts +9 -0
  28. package/dist/imsg/status.js +37 -0
  29. package/dist/imsg/status.js.map +1 -0
  30. package/dist/index.d.ts +9 -0
  31. package/dist/index.js +9 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/interactions/catalog.d.ts +2 -0
  34. package/dist/interactions/catalog.js +98 -0
  35. package/dist/interactions/catalog.js.map +1 -0
  36. package/dist/interactions/harness.d.ts +25 -0
  37. package/dist/interactions/harness.js +69 -0
  38. package/dist/interactions/harness.js.map +1 -0
  39. package/dist/interactions/index.d.ts +5 -0
  40. package/dist/interactions/index.js +5 -0
  41. package/dist/interactions/index.js.map +1 -0
  42. package/dist/interactions/registry.d.ts +9 -0
  43. package/dist/interactions/registry.js +33 -0
  44. package/dist/interactions/registry.js.map +1 -0
  45. package/dist/interactions/types.d.ts +28 -0
  46. package/dist/interactions/types.js +5 -0
  47. package/dist/interactions/types.js.map +1 -0
  48. package/dist/polls.d.ts +12 -0
  49. package/dist/polls.js +102 -0
  50. package/dist/polls.js.map +1 -0
  51. package/dist/runtime-report.d.ts +3 -0
  52. package/dist/runtime-report.js +10 -0
  53. package/dist/runtime-report.js.map +1 -0
  54. package/dist/types.d.ts +73 -0
  55. package/dist/types.js +2 -0
  56. package/dist/types.js.map +1 -0
  57. package/package.json +59 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mapier Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @mapier/imsg-sdk
2
+
3
+ TypeScript SDK for iMessage automation on macOS via the Mapier-patched `imsg` native layer, a fork of
4
+ [`openclaw/imsg`](https://github.com/openclaw/imsg) (MIT).
5
+
6
+ The package provides the gateway contract, the real `ImsgGateway` for a host Mac, and a deterministic `FakeGateway`
7
+ that runs anywhere.
8
+
9
+ Status: pre-release extraction. The gateway code was copied from `imsg-agent` at commit
10
+ `56b42c91f89fc3a2350f97c53f303aea6271d511` and last re-synced at `6e0b96b` (imsg-agent PR #161:
11
+ the full Tier-2 + read-verb surface, portable chat directory/backfill, and the 2026-07-14
12
+ host-verification ledger).
13
+
14
+ During the parallel-maintenance window, every gateway change must land in both repositories.
15
+
16
+ ## Quickstart
17
+
18
+ `FakeGateway` runs anywhere — no Mac, no device, no network:
19
+
20
+ ```ts
21
+ import { FakeGateway } from '@mapier/imsg-sdk';
22
+
23
+ const gateway = new FakeGateway();
24
+ const chatId = gateway.ensureDm('+15551234567');
25
+
26
+ gateway.injectInbound({ chatId, sender: '+15551234567', senderName: 'Ava', text: 'you free tonight?' });
27
+ await gateway.send({ chatId }, 'yep, 7pm works');
28
+
29
+ for await (const event of gateway.subscribe()) {
30
+ console.log(event.is_from_me ? `me: ${event.text}` : `${event.sender_name}: ${event.text}`);
31
+ if (event.is_from_me) break; // our send() echoing back confirms the round-trip
32
+ }
33
+ ```
34
+
35
+ See [`examples/quickstart.ts`](examples/quickstart.ts) for the runnable version:
36
+ `pnpm tsx examples/quickstart.ts`.
37
+
38
+ ## Real gateway
39
+
40
+ `ImsgGateway` drives real iMessage and requires a host Mac. It needs Mapier's pinned fork of
41
+ `openclaw/imsg`: `markxiong0122/imsg`, branch `mapier/macos26-tier2`, prebuilt binaries on release
42
+ [`v0.1.0`](https://github.com/markxiong0122/imsg/releases/tag/v0.1.0). Build it (`make build`) and
43
+ point the SDK at it via the `IMSG_BIN` environment variable. Caveat: rich-link delivery
44
+ (`sendRichLink`) requires a build carrying the fork's `mapier/richlink-sync-fix` patch, which
45
+ the `v0.1.0` release binaries predate — no capability marker gates it (see
46
+ `docs/interactions.md`, `rich-link-card`). See
47
+ [`docs/host-mac-control.md`](docs/host-mac-control.md) for host setup and
48
+ [`docs/gateway-contract.md`](docs/gateway-contract.md) for the full interface contract.
49
+
50
+ ## Interactions
51
+
52
+ `src/interactions/` catalogs iMessage interaction kinds across four tiers — web-link,
53
+ App Clip checkout, native balloons (tapback, poll, effect, rich-link card), and a future
54
+ MSMessages host-extension tier — with an honest capability/status per kind, plus a device-free
55
+ trial harness over `FakeGateway`. See [`docs/interactions.md`](docs/interactions.md).
56
+
57
+ ## Versioning
58
+
59
+ This package follows [SemVer](https://semver.org/) and is published to npm as
60
+ [`@mapier/imsg-sdk`](https://www.npmjs.com/package/@mapier/imsg-sdk) (`pnpm add @mapier/imsg-sdk`).
61
+ Pre-1.0: minor versions may break the API. See
62
+ [CONTRIBUTING.md](CONTRIBUTING.md#releasing) for the release process and
63
+ [CHANGELOG.md](CHANGELOG.md) for the version history.
64
+
65
+ ## License
66
+
67
+ MIT — see [LICENSE](LICENSE). The native layer is a fork of
68
+ [`openclaw/imsg`](https://github.com/openclaw/imsg), separately MIT-licensed upstream.
@@ -0,0 +1,3 @@
1
+ export declare function isImessageParticipantExternalId(value: string): boolean;
2
+ export declare function isImessageDmConversationExternalId(value: string): boolean;
3
+ export declare function isImessageGroupConversationExternalId(value: string): boolean;
@@ -0,0 +1,37 @@
1
+ // Portable iMessage identifier validation, mirroring the zod refinements in
2
+ // imsg-agent's connector protocol (shared/src/connector/protocol-primitives.ts)
3
+ // as plain predicates so the SDK stays dependency-free. The semantics must stay
4
+ // identical: at agent cutover these become the single source and the agent's
5
+ // schemas wrap them.
6
+ // Bounded-identifier envelope shared by every portable id: non-empty, capped,
7
+ // no control characters, no surrounding whitespace.
8
+ function isBoundedId(value) {
9
+ if (value.length < 1 || value.length > 512)
10
+ return false;
11
+ if (value.trim() !== value)
12
+ return false;
13
+ return [...value].every((character) => {
14
+ const codePoint = character.codePointAt(0);
15
+ return codePoint !== undefined && codePoint > 31 && codePoint !== 127;
16
+ });
17
+ }
18
+ // E.164 phone number, or an email that is already lowercase and NFC-normalized
19
+ // (the connector wire's canonical form — comparison happens after
20
+ // normalizeParticipant, but the portable id itself must arrive canonical).
21
+ export function isImessageParticipantExternalId(value) {
22
+ if (!isBoundedId(value))
23
+ return false;
24
+ return (/^\+[1-9][0-9]{6,14}$/.test(value) ||
25
+ (/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(value) && value === value.toLowerCase() && value === value.normalize('NFC')));
26
+ }
27
+ // DM GUID: `any;-;<participant>` — the portable id structurally carries its
28
+ // validated peer handle.
29
+ export function isImessageDmConversationExternalId(value) {
30
+ const prefix = 'any;-;';
31
+ return isBoundedId(value) && value.startsWith(prefix) && isImessageParticipantExternalId(value.slice(prefix.length));
32
+ }
33
+ // Group GUID: `any;+;<32 hex>` — opaque, carries no membership.
34
+ export function isImessageGroupConversationExternalId(value) {
35
+ return isBoundedId(value) && /^any;\+;[A-Fa-f0-9]{32}$/.test(value);
36
+ }
37
+ //# sourceMappingURL=external-ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"external-ids.js","sourceRoot":"","sources":["../../src/gateway/external-ids.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,gFAAgF;AAChF,gFAAgF;AAChF,6EAA6E;AAC7E,qBAAqB;AAErB,8EAA8E;AAC9E,oDAAoD;AACpD,SAAS,WAAW,CAAC,KAAa;IAChC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG;QAAE,OAAO,KAAK,CAAC;IACzD,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,KAAK;QAAE,OAAO,KAAK,CAAC;IACzC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,SAAS,EAAE,EAAE;QACpC,MAAM,SAAS,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;QAC3C,OAAO,SAAS,KAAK,SAAS,IAAI,SAAS,GAAG,EAAE,IAAI,SAAS,KAAK,GAAG,CAAC;IACxE,CAAC,CAAC,CAAC;AACL,CAAC;AAED,+EAA+E;AAC/E,kEAAkE;AAClE,2EAA2E;AAC3E,MAAM,UAAU,+BAA+B,CAAC,KAAa;IAC3D,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACtC,OAAO,CACL,sBAAsB,CAAC,IAAI,CAAC,KAAK,CAAC;QAClC,CAAC,4BAA4B,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,KAAK,CAAC,WAAW,EAAE,IAAI,KAAK,KAAK,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAChH,CAAC;AACJ,CAAC;AAED,4EAA4E;AAC5E,yBAAyB;AACzB,MAAM,UAAU,kCAAkC,CAAC,KAAa;IAC9D,MAAM,MAAM,GAAG,QAAQ,CAAC;IACxB,OAAO,WAAW,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,+BAA+B,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;AACvH,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,qCAAqC,CAAC,KAAa;IACjE,OAAO,WAAW,CAAC,KAAK,CAAC,IAAI,0BAA0B,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,161 @@
1
+ import type { MessageAttachment } from '../types.js';
2
+ import type { Gateway, GatewayCapabilities, GatewayEvent, GatewayChat, GatewayHistoryRange, GroupChatResolution, GroupChatResolutionRequest, HandleCheck, ImsgMessage, NamePhotoShareResult, Reaction, ReactionNote, ReactResult, SendResult, SendStatus, SendTarget } from './types.js';
3
+ export interface FakeGatewayOptions {
4
+ latencyMs?: number;
5
+ capabilities?: Partial<GatewayCapabilities>;
6
+ }
7
+ export declare class FakeGateway implements Gateway {
8
+ readonly capabilities: GatewayCapabilities;
9
+ private messages;
10
+ private chats;
11
+ private groupDirectoryComplete;
12
+ private groupParticipantsUnreported;
13
+ private namePhotoSharedChats;
14
+ private reachableAddresses;
15
+ private sentGuids;
16
+ private nextId;
17
+ private nextChatId;
18
+ private latencyMs;
19
+ private subscribers;
20
+ private activeSubscribers;
21
+ private forcedSendFailure;
22
+ constructor(opts?: FakeGatewayOptions);
23
+ private delay;
24
+ private allocId;
25
+ private emit;
26
+ private ensureDmChat;
27
+ private latestMessage;
28
+ private newestIncoming;
29
+ private buildMessage;
30
+ private buildOutboundMessage;
31
+ private toggleReaction;
32
+ subscribe(sinceId?: number): AsyncIterable<GatewayEvent>;
33
+ history(chatId: number, limit?: number): Promise<ImsgMessage[]>;
34
+ listChats(limit?: number): Promise<GatewayChat[]>;
35
+ historyRange(chatId: number, range: GatewayHistoryRange): Promise<ImsgMessage[]>;
36
+ send(target: SendTarget, text: string): Promise<SendResult>;
37
+ react(chatId: number, reaction: Reaction, expectedGuid?: string): Promise<ReactResult>;
38
+ createGroup(handles: string[], firstMessage: string): Promise<{
39
+ chatId: number;
40
+ }>;
41
+ recentReactions(chatId: number, limit?: number): Promise<ReactionNote[]>;
42
+ resolveDmChat(handle: string): Promise<number | null>;
43
+ resolveGroupChat(request: GroupChatResolutionRequest): Promise<GroupChatResolution>;
44
+ sendStatus(guid: string): Promise<SendStatus>;
45
+ checkHandle(address: string, opts?: {
46
+ aliasType?: 'phone' | 'email';
47
+ }): Promise<HandleCheck>;
48
+ private findInScanWindow;
49
+ tapback(chatId: number, targetGuid: string, reaction: Reaction, remove?: boolean): Promise<ReactResult>;
50
+ emojiTapback(chatId: number, targetGuid: string, emoji: string, remove?: boolean): Promise<ReactResult>;
51
+ sendRich(chatId: number, text: string, opts?: {
52
+ effect?: string;
53
+ replyToGuid?: string;
54
+ subject?: string;
55
+ }): Promise<SendResult>;
56
+ sendAttachment(chatId: number, _filePath: string, opts?: {
57
+ audio?: boolean;
58
+ replyToGuid?: string;
59
+ }): Promise<SendResult>;
60
+ sendSticker(chatId: number, _filePath: string, _opts?: {
61
+ attachToGuid?: string;
62
+ partIndex?: number;
63
+ }): Promise<SendResult>;
64
+ sendPoll(chatId: number, question: string, options: string[]): Promise<SendResult>;
65
+ sendRichLink(chatId: number, _url: string): Promise<SendResult>;
66
+ editMessage(chatId: number, targetGuid: string, text: string): Promise<{
67
+ ok: boolean;
68
+ }>;
69
+ unsendMessage(chatId: number, targetGuid: string): Promise<{
70
+ ok: boolean;
71
+ }>;
72
+ deleteMessage(chatId: number, targetGuid: string): Promise<{
73
+ ok: boolean;
74
+ }>;
75
+ setTyping(chatId: number, _on: boolean): Promise<{
76
+ ok: boolean;
77
+ }>;
78
+ markRead(chatId: number): Promise<{
79
+ ok: boolean;
80
+ }>;
81
+ renameGroup(chatId: number, name: string): Promise<{
82
+ ok: boolean;
83
+ }>;
84
+ setGroupPhoto(chatId: number, _filePath?: string): Promise<{
85
+ ok: boolean;
86
+ }>;
87
+ addParticipant(chatId: number, handle: string): Promise<{
88
+ ok: boolean;
89
+ }>;
90
+ removeParticipant(chatId: number, handle: string): Promise<{
91
+ ok: boolean;
92
+ }>;
93
+ leaveGroup(chatId: number): Promise<{
94
+ ok: boolean;
95
+ }>;
96
+ shareNamePhoto(chatId: number): Promise<NamePhotoShareResult>;
97
+ setGroupDirectoryComplete(complete: boolean): void;
98
+ setGroupParticipantsUnreported(chatId: number, unreported: boolean): void;
99
+ injectInbound(params: {
100
+ chatId?: number;
101
+ sender: string;
102
+ senderName?: string;
103
+ text?: string;
104
+ attachments?: MessageAttachment[];
105
+ }): ImsgMessage;
106
+ injectReaction(params: {
107
+ chatId: number;
108
+ sender: string;
109
+ senderName?: string;
110
+ reaction?: Reaction;
111
+ emoji?: string;
112
+ targetGuid?: string;
113
+ }): ImsgMessage;
114
+ injectPollCreate(params: {
115
+ chatId?: number;
116
+ sender?: string;
117
+ senderName?: string;
118
+ fromMe?: boolean;
119
+ question: string;
120
+ options: string[];
121
+ }): {
122
+ balloon: ImsgMessage;
123
+ caption: ImsgMessage;
124
+ };
125
+ private pollSnapshot;
126
+ injectPollVote(params: {
127
+ chatId: number;
128
+ sender: string;
129
+ senderName?: string;
130
+ pollGuid: string;
131
+ optionIds: string[];
132
+ }): ImsgMessage;
133
+ injectPollAddOption(params: {
134
+ chatId: number;
135
+ sender: string;
136
+ senderName?: string;
137
+ pollGuid: string;
138
+ optionText: string;
139
+ }): ImsgMessage;
140
+ failNextSend(): void;
141
+ setHandleReachable(address: string, reachable: boolean): void;
142
+ ensureDm(handle: string): number;
143
+ snapshot(): {
144
+ chats: Array<{
145
+ chatId: number;
146
+ chatGuid: string;
147
+ isGroup: boolean;
148
+ participants: string[];
149
+ }>;
150
+ messages: ImsgMessage[];
151
+ };
152
+ devRestoreSnapshot(input: {
153
+ readonly chats: ReadonlyArray<{
154
+ readonly chatId: number;
155
+ readonly isGroup: boolean;
156
+ readonly participants: readonly string[];
157
+ }>;
158
+ readonly messages: readonly ImsgMessage[];
159
+ }): void;
160
+ onEvent(listener: (msg: ImsgMessage) => void): () => void;
161
+ }