@xpert-ai/chatkit-types 0.11.2 → 0.12.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/dist/index.cjs CHANGED
@@ -2,6 +2,89 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const constants = require("./constants.cjs");
4
4
  require("@langchain/core/messages/tool");
5
+ function nonempty$1(value) {
6
+ return typeof value === "string" && value.trim().length > 0;
7
+ }
8
+ function parseFileReference(value) {
9
+ if (!value || typeof value !== "object" || !("viewKey" in value) || !nonempty$1(value.viewKey) || !("fileKey" in value) || !nonempty$1(value.fileKey) || !("targetId" in value) || !nonempty$1(value.targetId) || Object.keys(value).some(
10
+ (key) => !["viewKey", "fileKey", "targetId"].includes(key)
11
+ ))
12
+ return null;
13
+ return {
14
+ viewKey: value.viewKey,
15
+ fileKey: value.fileKey,
16
+ targetId: value.targetId
17
+ };
18
+ }
19
+ function parseFile(value) {
20
+ if (!value || typeof value !== "object" || !("id" in value) || !nonempty$1(value.id) || !("title" in value) || !nonempty$1(value.title) || !("file" in value) || "description" in value && typeof value.description !== "string")
21
+ return null;
22
+ const file = parseFileReference(value.file);
23
+ if (!file) return null;
24
+ return {
25
+ id: value.id,
26
+ title: value.title,
27
+ file,
28
+ ..."description" in value && typeof value.description === "string" ? { description: value.description } : {}
29
+ };
30
+ }
31
+ function parseImage(value) {
32
+ const item = parseFile(value);
33
+ if (!item || !value || typeof value !== "object" || "alt" in value && typeof value.alt !== "string")
34
+ return null;
35
+ return {
36
+ id: item.id,
37
+ title: item.title,
38
+ file: item.file,
39
+ ..."alt" in value && typeof value.alt === "string" ? { alt: value.alt } : {}
40
+ };
41
+ }
42
+ function parseField(value) {
43
+ if (!value || typeof value !== "object" || !("label" in value) || !nonempty$1(value.label) || !("value" in value) || typeof value.value !== "string")
44
+ return null;
45
+ return { label: value.label, value: value.value };
46
+ }
47
+ function parseItems(value, parse) {
48
+ if (!Array.isArray(value) || value.length > 100) return null;
49
+ const items = [];
50
+ for (const raw of value) {
51
+ const item = parse(raw);
52
+ if (!item) return null;
53
+ items.push(item);
54
+ }
55
+ return items;
56
+ }
57
+ function distinctIds(items) {
58
+ return new Set(items.map((item) => item.id)).size === items.length;
59
+ }
60
+ function parseBlock(value) {
61
+ if (!value || typeof value !== "object" || !("kind" in value) || "title" in value && typeof value.title !== "string")
62
+ return null;
63
+ const title = "title" in value && typeof value.title === "string" ? { title: value.title } : {};
64
+ switch (value.kind) {
65
+ case "image-gallery": {
66
+ const images = "images" in value ? parseItems(value.images, parseImage) : null;
67
+ return (images == null ? void 0 : images.length) && distinctIds(images) ? { kind: "image-gallery", ...title, images } : null;
68
+ }
69
+ case "file-list": {
70
+ const files = "files" in value ? parseItems(value.files, parseFile) : null;
71
+ return (files == null ? void 0 : files.length) && distinctIds(files) ? { kind: "file-list", ...title, files } : null;
72
+ }
73
+ case "fields": {
74
+ const fields = "fields" in value ? parseItems(value.fields, parseField) : null;
75
+ return (fields == null ? void 0 : fields.length) ? { kind: "fields", ...title, fields } : null;
76
+ }
77
+ default:
78
+ return null;
79
+ }
80
+ }
81
+ function parseResourceCardBlocks(value) {
82
+ if (!Array.isArray(value)) return [];
83
+ return value.slice(0, 32).flatMap((item) => {
84
+ const block = parseBlock(item);
85
+ return block ? [block] : [];
86
+ });
87
+ }
5
88
  function resourceCardId(card) {
6
89
  return JSON.stringify([
7
90
  card.resource.namespace,
@@ -23,7 +106,7 @@ function parseResourceCard(value) {
23
106
  return null;
24
107
  if ("artifactId" in resource && !nonempty(resource.artifactId)) return null;
25
108
  const open = value.open;
26
- if (!open || typeof open !== "object" || !("target" in open) || open.target !== "workbench.view" && open.target !== "assistant.project" || !("viewKey" in open) || !nonempty(open.viewKey))
109
+ if (!open || typeof open !== "object" || !("target" in open) || open.target !== "workbench.view" && open.target !== "assistant.project" && open.target !== "workbench.file" || !("viewKey" in open) || !nonempty(open.viewKey))
27
110
  return null;
28
111
  if (Object.keys(open).some(
29
112
  (key) => ![
@@ -31,7 +114,10 @@ function parseResourceCard(value) {
31
114
  "viewKey",
32
115
  "selectionId",
33
116
  "parameters",
34
- "projectId"
117
+ "projectId",
118
+ "fileKey",
119
+ "targetId",
120
+ "previewFile"
35
121
  ].includes(key)
36
122
  ))
37
123
  return null;
@@ -54,7 +140,21 @@ function parseResourceCard(value) {
54
140
  ..."parameters" in open ? { parameters } : {}
55
141
  };
56
142
  let target;
57
- if (open.target === "assistant.project") {
143
+ if (open.target === "workbench.file") {
144
+ if ("projectId" in open || "selectionId" in open || "parameters" in open || !("fileKey" in open) || !nonempty(open.fileKey) || !("targetId" in open) || !nonempty(open.targetId))
145
+ return null;
146
+ const previewFile = "previewFile" in open ? parseFileReference(open.previewFile) : void 0;
147
+ if (previewFile === null) return null;
148
+ target = {
149
+ viewKey: open.viewKey,
150
+ target: "workbench.file",
151
+ fileKey: open.fileKey,
152
+ targetId: open.targetId,
153
+ ...previewFile ? { previewFile } : {}
154
+ };
155
+ } else if ("fileKey" in open || "targetId" in open || "previewFile" in open) {
156
+ return null;
157
+ } else if (open.target === "assistant.project") {
58
158
  if (!("projectId" in open) || !nonempty(open.projectId)) return null;
59
159
  target = {
60
160
  ...common,
@@ -67,6 +167,9 @@ function parseResourceCard(value) {
67
167
  }
68
168
  if ("description" in value && typeof value.description !== "string")
69
169
  return null;
170
+ const content = parseResourceCardBlocks(
171
+ "content" in value ? value.content : "images" in value ? [{ kind: "image-gallery", images: value.images }] : []
172
+ );
70
173
  let icon;
71
174
  if ("icon" in value) {
72
175
  const candidate = value.icon;
@@ -86,6 +189,7 @@ function parseResourceCard(value) {
86
189
  title: value.title,
87
190
  ..."description" in value ? { description: value.description } : {},
88
191
  ...icon ? { icon } : {},
192
+ ...content.length ? { content } : {},
89
193
  open: target
90
194
  };
91
195
  }