spectrum-ts 0.5.0 → 0.6.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spectrum-ts",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@photon-ai/advanced-imessage": "^0.4.3",
23
- "@photon-ai/imessage-kit": "^3.0.0-rc.2",
23
+ "@photon-ai/imessage-kit": "^3.0.0",
24
24
  "@photon-ai/whatsapp-business": "^0.1.1",
25
25
  "@repeaterjs/repeater": "^3.0.6",
26
26
  "better-grpc": "^0.3.2",
@@ -1,199 +0,0 @@
1
- // src/content/text.ts
2
- import z from "zod";
3
- var textSchema = z.object({
4
- type: z.literal("text"),
5
- text: z.string().nonempty()
6
- });
7
- var asText = (text2) => textSchema.parse({ type: "text", text: text2 });
8
- function text(text2) {
9
- return {
10
- build: async () => asText(text2)
11
- };
12
- }
13
-
14
- // src/content/resolve.ts
15
- var resolveContents = (items) => Promise.all(
16
- items.map((c) => typeof c === "string" ? text(c).build() : c.build())
17
- );
18
-
19
- // src/platform/define.ts
20
- function createPlatformInstance(def, runtime) {
21
- const isPlatformUser = (value) => {
22
- return typeof value === "object" && value !== null && "__platform" in value && value.__platform === def.name;
23
- };
24
- const normalizeSpaceArgs = (args) => {
25
- if (args.length === 0) {
26
- return { users: [], params: void 0 };
27
- }
28
- const [first, ...rest] = args;
29
- if (Array.isArray(first)) {
30
- return {
31
- users: first,
32
- params: rest[0]
33
- };
34
- }
35
- if (!isPlatformUser(first)) {
36
- return {
37
- users: [],
38
- params: first
39
- };
40
- }
41
- const last = args.at(-1);
42
- if (last !== void 0 && !isPlatformUser(last)) {
43
- return {
44
- users: args.slice(0, -1),
45
- params: last
46
- };
47
- }
48
- return {
49
- users: args,
50
- params: void 0
51
- };
52
- };
53
- const base = {
54
- async user(userID) {
55
- const resolved = await def.user.resolve({
56
- input: { userID },
57
- client: runtime.client,
58
- config: runtime.config
59
- });
60
- return {
61
- ...resolved,
62
- __platform: def.name
63
- };
64
- },
65
- async space(...args) {
66
- const { users, params } = normalizeSpaceArgs(args);
67
- let parsedParams = params;
68
- if (params !== void 0 && def.space.params) {
69
- parsedParams = def.space.params.parse(params);
70
- }
71
- const resolved = await def.space.resolve({
72
- input: { users, params: parsedParams },
73
- client: runtime.client,
74
- config: runtime.config
75
- });
76
- const parsedSpace = def.space.schema ? def.space.schema.parse(resolved) : resolved;
77
- const spaceRef = {
78
- id: parsedSpace.id,
79
- __platform: def.name
80
- };
81
- const typingCtx = {
82
- space: spaceRef,
83
- client: runtime.client,
84
- config: runtime.config
85
- };
86
- return {
87
- ...parsedSpace,
88
- ...spaceRef,
89
- send: async (...content) => {
90
- const built = await resolveContents(content);
91
- for (const item of built) {
92
- await def.actions.send({
93
- ...typingCtx,
94
- content: item
95
- });
96
- }
97
- },
98
- startTyping: async () => {
99
- await def.actions.startTyping?.(typingCtx);
100
- },
101
- stopTyping: async () => {
102
- await def.actions.stopTyping?.(typingCtx);
103
- },
104
- responding: async (fn) => {
105
- await def.actions.startTyping?.(typingCtx);
106
- try {
107
- return await fn();
108
- } finally {
109
- await def.actions.stopTyping?.(typingCtx).catch(() => {
110
- });
111
- }
112
- }
113
- };
114
- }
115
- };
116
- const eventProperties = {};
117
- for (const eventName of Object.keys(def.events)) {
118
- if (eventName === "messages") {
119
- continue;
120
- }
121
- const producer = def.events[eventName];
122
- if (producer) {
123
- eventProperties[eventName] = producer({
124
- client: runtime.client,
125
- config: runtime.config
126
- });
127
- }
128
- }
129
- return Object.assign(base, eventProperties);
130
- }
131
- function definePlatform(name, def) {
132
- const fullDef = { name, ...def };
133
- const platformCache = /* @__PURE__ */ new WeakMap();
134
- const narrowSpectrum = (spectrum) => {
135
- const cached = platformCache.get(spectrum);
136
- if (cached) {
137
- return cached;
138
- }
139
- const runtime = spectrum.__internal.platforms.get(name);
140
- if (!runtime) {
141
- throw new Error(`Platform "${name}" is not registered`);
142
- }
143
- const instance = createPlatformInstance(
144
- fullDef,
145
- runtime
146
- );
147
- platformCache.set(spectrum, instance);
148
- return instance;
149
- };
150
- const narrowSpace = (input) => {
151
- if (input.__platform !== name) {
152
- throw new Error(
153
- `Expected space from "${name}", got "${input.__platform}"`
154
- );
155
- }
156
- return input;
157
- };
158
- const narrowMessage = (input) => {
159
- if (input.platform !== name) {
160
- throw new Error(
161
- `Expected message from "${name}", got "${input.platform}"`
162
- );
163
- }
164
- return input;
165
- };
166
- const narrower = ((input) => {
167
- if ("__providers" in input && "__internal" in input) {
168
- return narrowSpectrum(input);
169
- }
170
- if ("__platform" in input && "send" in input) {
171
- return narrowSpace(input);
172
- }
173
- if ("platform" in input && "sender" in input && "space" in input) {
174
- return narrowMessage(input);
175
- }
176
- throw new Error("Invalid input to platform narrowing function");
177
- });
178
- narrower.config = (config) => {
179
- const resolvedConfig = config ?? {};
180
- return {
181
- __tag: "PlatformProviderConfig",
182
- __def: void 0,
183
- __name: name,
184
- config: resolvedConfig,
185
- __definition: fullDef
186
- };
187
- };
188
- if (def.static) {
189
- Object.assign(narrower, def.static);
190
- }
191
- return narrower;
192
- }
193
-
194
- export {
195
- asText,
196
- text,
197
- resolveContents,
198
- definePlatform
199
- };