spectrum-ts 1.0.1 → 1.1.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.
@@ -903,8 +903,54 @@ function buildMessage(params) {
903
903
  throw err;
904
904
  }
905
905
  };
906
+ const requireBuiltMessage = (action) => {
907
+ if (!self) {
908
+ throw new Error(
909
+ `${action}() called before message construction completed (internal bug)`
910
+ );
911
+ }
912
+ return self;
913
+ };
914
+ const dispatchReplyItem = async (item, target, replyToMessage) => {
915
+ let sendResult;
916
+ try {
917
+ sendResult = await replyToMessage({
918
+ space: spaceRef,
919
+ messageId: params.id,
920
+ target,
921
+ content: item,
922
+ client,
923
+ config
924
+ });
925
+ } catch (err) {
926
+ if (err instanceof UnsupportedError) {
927
+ warnUnsupported(err, definition.name);
928
+ return;
929
+ }
930
+ throw err;
931
+ }
932
+ if (!sendResult?.id) {
933
+ throw new Error(
934
+ `Platform "${definition.name}" reply did not return a message id`
935
+ );
936
+ }
937
+ return buildMessage({
938
+ id: sendResult.id,
939
+ content: item,
940
+ sender: sendResult.sender,
941
+ timestamp: sendResult.timestamp ?? /* @__PURE__ */ new Date(),
942
+ extras: {},
943
+ spaceRef,
944
+ space,
945
+ definition,
946
+ client,
947
+ config,
948
+ direction: "outbound"
949
+ });
950
+ };
906
951
  async function reply(...content) {
907
- if (!definition.actions.replyToMessage) {
952
+ const replyToMessage = definition.actions.replyToMessage;
953
+ if (!replyToMessage) {
908
954
  warnUnsupported(
909
955
  UnsupportedError.action("reply", definition.name),
910
956
  definition.name
@@ -912,44 +958,13 @@ function buildMessage(params) {
912
958
  return content.length === 1 ? void 0 : [];
913
959
  }
914
960
  const resolved = await resolveContents(content);
961
+ const target = requireBuiltMessage("reply");
915
962
  const results = [];
916
963
  for (const item of resolved) {
917
- let sendResult;
918
- try {
919
- sendResult = await definition.actions.replyToMessage({
920
- space: spaceRef,
921
- messageId: params.id,
922
- content: item,
923
- client,
924
- config
925
- });
926
- } catch (err) {
927
- if (err instanceof UnsupportedError) {
928
- warnUnsupported(err, definition.name);
929
- continue;
930
- }
931
- throw err;
932
- }
933
- if (!sendResult?.id) {
934
- throw new Error(
935
- `Platform "${definition.name}" reply did not return a message id`
936
- );
964
+ const sent = await dispatchReplyItem(item, target, replyToMessage);
965
+ if (sent) {
966
+ results.push(sent);
937
967
  }
938
- results.push(
939
- buildMessage({
940
- id: sendResult.id,
941
- content: item,
942
- sender: sendResult.sender,
943
- timestamp: sendResult.timestamp ?? /* @__PURE__ */ new Date(),
944
- extras: {},
945
- spaceRef,
946
- space,
947
- definition,
948
- client,
949
- config,
950
- direction: "outbound"
951
- })
952
- );
953
968
  }
954
969
  if (content.length === 1) {
955
970
  return results[0];
@@ -2,7 +2,7 @@ import {
2
2
  bufferToStream,
3
3
  readSchema,
4
4
  streamSchema
5
- } from "./chunk-XMAI2AAN.js";
5
+ } from "./chunk-7D6FHYKT.js";
6
6
 
7
7
  // src/content/voice.ts
8
8
  import { createReadStream } from "fs";
@@ -1,3 +1,58 @@
1
+ // src/content/poll.ts
2
+ import z from "zod";
3
+ var pollChoiceSchema = z.object({
4
+ title: z.string().nonempty()
5
+ });
6
+ var pollSchema = z.object({
7
+ type: z.literal("poll"),
8
+ title: z.string().nonempty().max(300),
9
+ options: z.array(pollChoiceSchema).min(2).max(10)
10
+ });
11
+ var pollOptionSchema = z.object({
12
+ type: z.literal("poll_option"),
13
+ option: pollChoiceSchema,
14
+ poll: pollSchema,
15
+ selected: z.boolean(),
16
+ title: z.string().nonempty()
17
+ }).superRefine((value, ctx) => {
18
+ if (value.title !== value.option.title) {
19
+ ctx.addIssue({
20
+ code: "custom",
21
+ message: "poll_option title must match option.title",
22
+ path: ["title"]
23
+ });
24
+ }
25
+ if (!value.poll.options.some(
26
+ (pollOption) => pollOption.title === value.option.title
27
+ )) {
28
+ ctx.addIssue({
29
+ code: "custom",
30
+ message: "poll_option option must exist in poll.options",
31
+ path: ["option"]
32
+ });
33
+ }
34
+ });
35
+ var asPoll = (input) => pollSchema.parse({ type: "poll", ...input });
36
+ var asPollOption = (input) => pollOptionSchema.parse({
37
+ type: "poll_option",
38
+ ...input,
39
+ title: input.option.title
40
+ });
41
+ var option = (title) => ({ title });
42
+ var normalize = (raw) => typeof raw === "string" ? { title: raw } : { title: raw.title };
43
+ var collectOptions = (args) => {
44
+ const [first] = args;
45
+ if (args.length === 1 && Array.isArray(first)) {
46
+ return first;
47
+ }
48
+ return args;
49
+ };
50
+ function poll(title, ...rest) {
51
+ return {
52
+ build: async () => asPoll({ title, options: collectOptions(rest).map(normalize) })
53
+ };
54
+ }
55
+
1
56
  // src/utils/stream.ts
2
57
  import { Repeater } from "@repeaterjs/repeater";
3
58
  function stream(setup) {
@@ -122,6 +177,10 @@ var cloud = {
122
177
  };
123
178
 
124
179
  export {
180
+ asPoll,
181
+ asPollOption,
182
+ option,
183
+ poll,
125
184
  stream,
126
185
  mergeStreams,
127
186
  SpectrumCloudError,
@@ -3,7 +3,7 @@ import {
3
3
  readSchema,
4
4
  resolveContents,
5
5
  streamSchema
6
- } from "./chunk-XMAI2AAN.js";
6
+ } from "./chunk-7D6FHYKT.js";
7
7
 
8
8
  // src/content/group.ts
9
9
  import z from "zod";
@@ -162,7 +162,7 @@ function richlink(url) {
162
162
  }
163
163
 
164
164
  export {
165
- asGroup,
165
+ groupSchema,
166
166
  group,
167
167
  asRichlink,
168
168
  richlink
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { C as ContentBuilder, U as User, M as Message, a as ContentInput, b as Content, P as ProviderMessage, c as PlatformDef, d as Platform, e as PlatformProviderConfig, S as SpectrumLike, f as CustomEventStreams, g as Space, I as InboundMessage, O as OutboundMessage } from './types-D5KhSXLy.js';
2
- export { A as AnyPlatformDef, E as EventProducer, h as PlatformInstance, i as PlatformMessage, j as PlatformSpace, k as PlatformUser, l as SchemaMessage } from './types-D5KhSXLy.js';
1
+ import { C as ContentBuilder, U as User, M as Message, a as ContentInput, b as Content, P as ProviderMessage, c as PlatformDef, d as Platform, e as PlatformProviderConfig, S as SpectrumLike, f as CustomEventStreams, g as Space, I as InboundMessage, O as OutboundMessage } from './types-BZhWdyLk.js';
2
+ export { A as AnyPlatformDef, E as EventProducer, h as PlatformInstance, i as PlatformMessage, j as PlatformSpace, k as PlatformUser, l as SchemaMessage } from './types-BZhWdyLk.js';
3
3
  import vCard from 'vcf';
4
4
  import z__default from 'zod';
5
5
  export { M as ManagedStream, m as mergeStreams, s as stream } from './stream-B55k7W8-.js';
@@ -138,6 +138,41 @@ declare const groupSchema: z__default.ZodObject<{
138
138
  type Group = z__default.infer<typeof groupSchema>;
139
139
  declare function group(...items: [ContentInput, ContentInput, ...ContentInput[]]): ContentBuilder;
140
140
 
141
+ declare const pollChoiceSchema: z__default.ZodObject<{
142
+ title: z__default.ZodString;
143
+ }, z__default.core.$strip>;
144
+ declare const pollSchema: z__default.ZodObject<{
145
+ type: z__default.ZodLiteral<"poll">;
146
+ title: z__default.ZodString;
147
+ options: z__default.ZodArray<z__default.ZodObject<{
148
+ title: z__default.ZodString;
149
+ }, z__default.core.$strip>>;
150
+ }, z__default.core.$strip>;
151
+ declare const pollOptionSchema: z__default.ZodObject<{
152
+ type: z__default.ZodLiteral<"poll_option">;
153
+ option: z__default.ZodObject<{
154
+ title: z__default.ZodString;
155
+ }, z__default.core.$strip>;
156
+ poll: z__default.ZodObject<{
157
+ type: z__default.ZodLiteral<"poll">;
158
+ title: z__default.ZodString;
159
+ options: z__default.ZodArray<z__default.ZodObject<{
160
+ title: z__default.ZodString;
161
+ }, z__default.core.$strip>>;
162
+ }, z__default.core.$strip>;
163
+ selected: z__default.ZodBoolean;
164
+ title: z__default.ZodString;
165
+ }, z__default.core.$strip>;
166
+ type Poll = z__default.infer<typeof pollSchema>;
167
+ type PollChoice = z__default.infer<typeof pollChoiceSchema>;
168
+ type PollOption = z__default.infer<typeof pollOptionSchema>;
169
+ type PollChoiceInput = string | {
170
+ title: string;
171
+ };
172
+ declare const option: (title: string) => PollChoice;
173
+ declare function poll(title: string, options: PollChoiceInput[]): ContentBuilder;
174
+ declare function poll(title: string, ...options: PollChoiceInput[]): ContentBuilder;
175
+
141
176
  declare const reactionSchema: z__default.ZodObject<{
142
177
  type: z__default.ZodLiteral<"reaction">;
143
178
  emoji: z__default.ZodString;
@@ -2234,4 +2269,4 @@ declare class UnsupportedError extends Error {
2234
2269
  declare const fromVCard: (vcf: string) => ContactInput;
2235
2270
  declare const toVCard: (contact: Contact) => Promise<string>;
2236
2271
 
2237
- export { type CloudPlatform, type Contact, type ContactAddress, type ContactDetails, type ContactEmail, type ContactInput, type ContactName, type ContactOrg, type ContactPhone, Content, ContentBuilder, ContentInput, type DedicatedTokenData, Emoji, type EmojiKey, type Group, type ImessageInfoData, Message, Platform, PlatformDef, PlatformProviderConfig, type PlatformStatus, type PlatformsData, type Reaction, type Richlink, type SharedTokenData, Space, Spectrum, SpectrumCloudError, type SpectrumInstance, type SubscriptionData, type SubscriptionStatus, type TokenData, UnsupportedError, type UnsupportedKind, User, type Voice, attachment, cloud, contact, custom, definePlatform, fromVCard, group, reaction, resolveContents, richlink, text, toVCard, voice };
2272
+ export { type CloudPlatform, type Contact, type ContactAddress, type ContactDetails, type ContactEmail, type ContactInput, type ContactName, type ContactOrg, type ContactPhone, Content, ContentBuilder, ContentInput, type DedicatedTokenData, Emoji, type EmojiKey, type Group, type ImessageInfoData, Message, Platform, PlatformDef, PlatformProviderConfig, type PlatformStatus, type PlatformsData, type Poll, type PollChoice, type PollChoiceInput, type PollOption, type Reaction, type Richlink, type SharedTokenData, Space, Spectrum, SpectrumCloudError, type SpectrumInstance, type SubscriptionData, type SubscriptionStatus, type TokenData, UnsupportedError, type UnsupportedKind, User, type Voice, attachment, cloud, contact, custom, definePlatform, fromVCard, group, option, poll, reaction, resolveContents, richlink, text, toVCard, voice };
package/dist/index.js CHANGED
@@ -1,16 +1,18 @@
1
1
  import {
2
2
  group,
3
3
  richlink
4
- } from "./chunk-LAGNM6I7.js";
4
+ } from "./chunk-TY3RT4OB.js";
5
5
  import {
6
6
  voice
7
- } from "./chunk-7Q7KJKGL.js";
7
+ } from "./chunk-7VSE6V3Q.js";
8
8
  import {
9
9
  SpectrumCloudError,
10
10
  cloud,
11
11
  mergeStreams,
12
+ option,
13
+ poll,
12
14
  stream
13
- } from "./chunk-2Y5GBI6W.js";
15
+ } from "./chunk-PXX7ISZ6.js";
14
16
  import {
15
17
  UnsupportedError,
16
18
  attachment,
@@ -24,7 +26,7 @@ import {
24
26
  text,
25
27
  toVCard,
26
28
  wrapProviderMessage
27
- } from "./chunk-XMAI2AAN.js";
29
+ } from "./chunk-7D6FHYKT.js";
28
30
 
29
31
  // src/emoji/generated.ts
30
32
  var GeneratedEmoji = {
@@ -2205,6 +2207,8 @@ export {
2205
2207
  fromVCard,
2206
2208
  group,
2207
2209
  mergeStreams,
2210
+ option,
2211
+ poll,
2208
2212
  reaction,
2209
2213
  resolveContents,
2210
2214
  richlink,
@@ -1,5 +1,5 @@
1
1
  import { M as ManagedStream } from '../../stream-B55k7W8-.js';
2
- import { l as SchemaMessage, d as Platform, c as PlatformDef, P as ProviderMessage } from '../../types-D5KhSXLy.js';
2
+ import { l as SchemaMessage, d as Platform, c as PlatformDef, P as ProviderMessage } from '../../types-BZhWdyLk.js';
3
3
  import * as zod_v4_core from 'zod/v4/core';
4
4
  import * as z from 'zod';
5
5
  import z__default from 'zod';