@satorijs/adapter-lark 3.11.0 → 3.11.2

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/lib/index.cjs CHANGED
@@ -769,8 +769,7 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
769
769
  textContent = "";
770
770
  richContent = [];
771
771
  card;
772
- noteElements;
773
- actionElements = [];
772
+ elements = [];
774
773
  isForm = false;
775
774
  editMessageIds;
776
775
  async post(data) {
@@ -832,32 +831,20 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
832
831
  this.errors.push(e);
833
832
  }
834
833
  }
835
- flushText(button = false) {
836
- if ((this.textContent || !button) && this.actionElements.length) {
837
- this.card.elements.push({ tag: "action", actions: this.actionElements, layout: "flow" });
838
- this.actionElements = [];
839
- }
840
- if (this.textContent) {
841
- this.richContent.push([{ tag: "md", text: this.textContent }]);
842
- if (this.noteElements) {
843
- this.noteElements.push({ tag: "plain_text", content: this.textContent });
844
- } else if (this.card) {
845
- this.card.elements.push({ tag: "markdown", content: this.textContent });
846
- }
847
- this.textContent = "";
848
- }
834
+ flushText() {
835
+ if (!this.textContent) return;
836
+ this.richContent.push([{ tag: "md", text: this.textContent }]);
837
+ this.elements.push({ tag: "markdown", content: this.textContent });
838
+ this.textContent = "";
849
839
  }
850
840
  async flush() {
851
841
  this.flushText();
852
842
  if (!this.card && !this.richContent.length) return;
853
843
  if (this.card) {
854
- this.bot.logger.debug("card", JSON.stringify(this.card.elements));
844
+ this.bot.logger.debug("card %o", JSON.parse(JSON.stringify(this.card)));
855
845
  await this.post({
856
846
  msg_type: "interactive",
857
- content: JSON.stringify({
858
- header: this.card.header,
859
- elements: this.card.elements
860
- })
847
+ content: JSON.stringify(this.card)
861
848
  });
862
849
  } else {
863
850
  await this.post({
@@ -921,14 +908,14 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
921
908
  content: JSON.stringify({ file_key })
922
909
  });
923
910
  }
924
- createBehavior(attrs) {
911
+ createBehaviors(attrs) {
925
912
  const behaviors = [];
926
913
  if (attrs.type === "link") {
927
914
  behaviors.push({
928
915
  type: "open_url",
929
916
  default_url: attrs.href
930
917
  });
931
- } else if (attrs.type === "input") {
918
+ } else if (attrs.type === "input" || attrs.type === "submit") {
932
919
  behaviors.push({
933
920
  type: "callback",
934
921
  value: {
@@ -985,26 +972,24 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
985
972
  } else if (type === "hr") {
986
973
  this.flushText();
987
974
  this.richContent.push([{ tag: "hr" }]);
988
- this.card?.elements.push({ tag: "hr" });
975
+ this.elements.push({ tag: "hr" });
989
976
  } else if (type === "form") {
990
977
  this.flushText();
991
- const length = this.card?.elements.length;
978
+ const parent = this.elements;
979
+ parent.push({
980
+ tag: "form",
981
+ name: attrs.name || "Form",
982
+ elements: this.elements = []
983
+ });
992
984
  this.isForm = true;
993
985
  await this.render(children);
994
986
  this.isForm = false;
995
- if (this.card?.elements.length > length) {
996
- const elements = this.card?.elements.splice(length);
997
- this.card.elements.push({
998
- tag: "form",
999
- name: attrs.name || "Form",
1000
- elements
1001
- });
1002
- }
987
+ this.elements = parent;
1003
988
  } else if (type === "input") {
1004
989
  if (attrs.type === "checkbox") {
1005
990
  this.flushText();
1006
991
  await this.render(children);
1007
- this.card?.elements.push({
992
+ this.elements.push({
1008
993
  tag: "checker",
1009
994
  name: (attrs.argument ? "@@" : attrs.option ? `@${attrs.option}=` : "") + attrs.name,
1010
995
  checked: attrs.value,
@@ -1015,20 +1000,17 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1015
1000
  });
1016
1001
  this.textContent = "";
1017
1002
  } else if (attrs.type === "submit") {
1018
- this.flushText(true);
1003
+ this.flushText();
1019
1004
  await this.render(children);
1020
- this.card?.elements.push({
1005
+ this.elements.push({
1021
1006
  tag: "button",
1022
1007
  name: attrs.name,
1023
1008
  text: {
1024
1009
  tag: "plain_text",
1025
1010
  content: this.textContent
1026
1011
  },
1027
- action_type: "form_submit",
1028
- value: {
1029
- _satori_type: "command",
1030
- content: attrs.text
1031
- }
1012
+ form_action_type: "submit",
1013
+ behaviors: this.createBehaviors(attrs)
1032
1014
  });
1033
1015
  this.textContent = "";
1034
1016
  } else {
@@ -1047,19 +1029,10 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1047
1029
  },
1048
1030
  default_value: attrs.value,
1049
1031
  disabled: attrs.disabled,
1050
- required: attrs.required
1032
+ required: attrs.required,
1033
+ behaviors: this.createBehaviors(attrs)
1051
1034
  };
1052
- if (this.isForm) {
1053
- this.card?.elements.push(input);
1054
- } else {
1055
- this.card?.elements.push({
1056
- tag: "action",
1057
- actions: [{
1058
- ...input,
1059
- behaviors: this.createBehavior(attrs)
1060
- }]
1061
- });
1062
- }
1035
+ this.elements.push(input);
1063
1036
  }
1064
1037
  } else if (type === "select") {
1065
1038
  this.flushText();
@@ -1074,7 +1047,8 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1074
1047
  tag: "plain_text",
1075
1048
  content: attrs.placeholder
1076
1049
  },
1077
- options: []
1050
+ options: [],
1051
+ behaviors: this.createBehaviors(attrs)
1078
1052
  };
1079
1053
  for (const child of children) {
1080
1054
  if (child.type !== "option") continue;
@@ -1088,29 +1062,17 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1088
1062
  });
1089
1063
  this.textContent = "";
1090
1064
  }
1091
- if (this.isForm) {
1092
- this.card?.elements.push(select);
1093
- } else {
1094
- this.card?.elements.push({
1095
- tag: "action",
1096
- actions: [{
1097
- ...select,
1098
- behaviors: this.createBehavior(attrs)
1099
- }]
1100
- });
1101
- }
1065
+ this.elements.push(select);
1102
1066
  } else if (type === "button") {
1103
- this.card ??= { elements: [] };
1104
- this.flushText(true);
1067
+ this.flushText();
1105
1068
  await this.render(children);
1106
- this.actionElements.push({
1069
+ this.elements.push({
1107
1070
  tag: "button",
1108
1071
  text: {
1109
1072
  tag: "plain_text",
1110
1073
  content: this.textContent
1111
1074
  },
1112
1075
  disabled: attrs.disabled,
1113
- behaviors: this.createBehavior(attrs),
1114
1076
  type: attrs["lark:type"],
1115
1077
  size: attrs["lark:size"],
1116
1078
  width: attrs["lark:width"],
@@ -1125,13 +1087,25 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1125
1087
  disabled_tips: attrs["lark:disabled-tips"] && {
1126
1088
  tag: "plain_text",
1127
1089
  content: attrs["lark:disabled-tips"]
1128
- }
1090
+ },
1091
+ behaviors: this.createBehaviors(attrs)
1129
1092
  });
1130
1093
  this.textContent = "";
1131
- } else if (type === "button-group") {
1094
+ } else if (type === "div") {
1132
1095
  this.flushText();
1133
1096
  await this.render(children);
1134
- this.flushText();
1097
+ this.elements.push({
1098
+ tag: "markdown",
1099
+ text_align: attrs.align,
1100
+ text_size: attrs.size,
1101
+ content: this.textContent,
1102
+ margin: attrs.margin,
1103
+ icon: attrs.icon && {
1104
+ tag: "standard_icon",
1105
+ token: attrs.icon
1106
+ }
1107
+ });
1108
+ this.textContent = "";
1135
1109
  } else if (type.startsWith("lark:") || type.startsWith("feishu:")) {
1136
1110
  const tag = type.slice(type.split(":", 1)[0].length + 1);
1137
1111
  if (tag === "share-chat") {
@@ -1161,10 +1135,10 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1161
1135
  } else if (tag === "card") {
1162
1136
  await this.flush();
1163
1137
  this.card = {
1164
- elements: [],
1138
+ schema: "2.0",
1165
1139
  header: attrs.title && {
1166
1140
  template: attrs.color,
1167
- ud_icon: attrs.icon && {
1141
+ icon: attrs.icon && {
1168
1142
  tag: "standard_icon",
1169
1143
  token: attrs.icon
1170
1144
  },
@@ -1176,40 +1150,16 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1176
1150
  tag: "plain_text",
1177
1151
  content: attrs.subtitle
1178
1152
  }
1153
+ },
1154
+ body: {
1155
+ elements: this.elements = []
1179
1156
  }
1180
1157
  };
1181
1158
  await this.render(children, true);
1182
- } else if (tag === "div") {
1183
- this.flushText();
1184
- await this.render(children);
1185
- this.card?.elements.push({
1186
- tag: "markdown",
1187
- text_align: attrs.align,
1188
- text_size: attrs.size,
1189
- content: this.textContent
1190
- });
1191
- this.textContent = "";
1192
- } else if (tag === "note") {
1193
- this.flushText();
1194
- this.noteElements = [];
1195
- await this.render(children);
1196
- this.flushText();
1197
- this.card?.elements.push({
1198
- tag: "note",
1199
- elements: this.noteElements
1200
- });
1201
- this.noteElements = void 0;
1202
- } else if (tag === "icon") {
1203
- this.flushText();
1204
- this.noteElements?.push({
1205
- tag: "standard_icon",
1206
- token: attrs.token
1207
- });
1208
1159
  } else if (tag === "column-set") {
1209
1160
  this.flushText();
1210
- const parent = this.card;
1211
1161
  const columns = [];
1212
- parent?.elements.push({
1162
+ this.elements.push({
1213
1163
  tag: "column_set",
1214
1164
  margin: attrs.margin,
1215
1165
  flex_mode: attrs.flexMode,
@@ -1218,11 +1168,12 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1218
1168
  background_style: attrs.backgroundStyle,
1219
1169
  columns
1220
1170
  });
1171
+ const parent = this.elements;
1221
1172
  for (const child of children) {
1222
1173
  if (child.type !== "lark:column" && child.type !== "feishu:column") {
1223
1174
  continue;
1224
1175
  }
1225
- this.card = { elements: [] };
1176
+ this.elements = [];
1226
1177
  await this.render(child.children);
1227
1178
  this.flushText();
1228
1179
  columns.push({
@@ -1233,11 +1184,30 @@ var LarkMessageEncoder = class extends import_core3.MessageEncoder {
1233
1184
  vertical_align: child.attrs.verticalAlign ?? "center",
1234
1185
  vertical_spacing: child.attrs.verticalSpacing ?? "0px",
1235
1186
  background_style: child.attrs.backgroundStyle,
1236
- elements: this.card.elements
1187
+ elements: this.elements
1237
1188
  });
1238
1189
  }
1239
- this.card = parent;
1190
+ this.elements = parent;
1240
1191
  }
1192
+ } else if (type === "button-group") {
1193
+ this.flushText();
1194
+ const parent = this.elements;
1195
+ this.elements = [];
1196
+ await this.render(children);
1197
+ this.flushText();
1198
+ parent.push({
1199
+ tag: "column_set",
1200
+ margin: attrs.margin,
1201
+ flex_mode: attrs.flexMode,
1202
+ horizontal_align: attrs.horizontalAlign,
1203
+ horizontal_spacing: attrs.horizontalSpacing,
1204
+ background_style: attrs.backgroundStyle,
1205
+ columns: this.elements.map((element2) => ({
1206
+ tag: "column",
1207
+ elements: [element2]
1208
+ }))
1209
+ });
1210
+ this.elements = parent;
1241
1211
  } else {
1242
1212
  await this.render(children);
1243
1213
  }
package/lib/message.d.ts CHANGED
@@ -7,8 +7,7 @@ export declare class LarkMessageEncoder<C extends Context = Context> extends Mes
7
7
  private textContent;
8
8
  private richContent;
9
9
  private card;
10
- private noteElements;
11
- private actionElements;
10
+ private elements;
12
11
  private isForm;
13
12
  editMessageIds: string[] | undefined;
14
13
  post(data?: any): Promise<void>;
@@ -16,7 +15,7 @@ export declare class LarkMessageEncoder<C extends Context = Context> extends Mes
16
15
  flush(): Promise<void>;
17
16
  createImage(url: string): Promise<string>;
18
17
  sendFile(_type: 'video' | 'audio' | 'file', attrs: any): Promise<void>;
19
- private createBehavior;
18
+ private createBehaviors;
20
19
  visit(element: h): Promise<void>;
21
20
  }
22
21
  export { LarkMessageEncoder as FeishuMessageEncoder };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@satorijs/adapter-lark",
3
3
  "description": "Lark (飞书) Adapter for Satorijs",
4
- "version": "3.11.0",
4
+ "version": "3.11.2",
5
5
  "type": "module",
6
6
  "main": "lib/index.cjs",
7
7
  "types": "lib/index.d.ts",