@xpert-ai/chatkit-types 0.6.1 → 0.6.3

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.js CHANGED
@@ -273,6 +273,26 @@ function normalizeRequestLanguage(locale) {
273
273
  }
274
274
  return normalized;
275
275
  }
276
+ function normalizeThreadReference(value) {
277
+ if (!value || typeof value !== "object" || !("type" in value) || value.type !== "thread")
278
+ return null;
279
+ if (!("conversationId" in value) || !isLocator(value.conversationId) || !("threadId" in value) || !isLocator(value.threadId))
280
+ return null;
281
+ if ("label" in value && value.label !== void 0 && (typeof value.label !== "string" || value.label.length > 300))
282
+ return null;
283
+ if ("id" in value && value.id !== void 0 && !isLocator(value.id))
284
+ return null;
285
+ return {
286
+ type: "thread",
287
+ conversationId: value.conversationId.trim(),
288
+ threadId: value.threadId.trim(),
289
+ ..."label" in value && typeof value.label === "string" ? { label: value.label.trim() } : {},
290
+ ..."id" in value && typeof value.id === "string" ? { id: value.id.trim() } : {}
291
+ };
292
+ }
293
+ function isLocator(value) {
294
+ return typeof value === "string" && value.trim().length > 0 && value.length <= 128;
295
+ }
276
296
  const PET_ANIMATION_NAMES = [
277
297
  "idle",
278
298
  "running-right",
@@ -524,8 +544,281 @@ function getPinnedPetPosition(pin, size, viewport, padding) {
524
544
  }
525
545
  }
526
546
  const CHATKIT_TASK_SUMMARY_OPEN_RESOURCE_EFFECT = "task_summary.open_resource";
547
+ function chatSkillUsageKey(usage) {
548
+ return JSON.stringify([
549
+ usage.source.type,
550
+ usage.source.id,
551
+ usage.skillId,
552
+ usage.version
553
+ ]);
554
+ }
555
+ function mergeChatSkillUsages(...groups) {
556
+ const usages = /* @__PURE__ */ new Map();
557
+ for (const group of groups) {
558
+ for (const usage of group ?? []) {
559
+ const key = chatSkillUsageKey(usage);
560
+ if (!usages.has(key)) usages.set(key, usage);
561
+ }
562
+ }
563
+ return [...usages.values()];
564
+ }
565
+ function normalizeChatSkillUsages(value) {
566
+ if (!Array.isArray(value)) return [];
567
+ const usages = [];
568
+ for (const item of value) {
569
+ if (!item || typeof item !== "object" || item.activation !== "read")
570
+ continue;
571
+ const { source } = item;
572
+ if (!source || typeof source !== "object" || !["workspace", "project", "assistant", "plugin"].includes(source.type) || typeof source.id !== "string" || !source.id.trim())
573
+ continue;
574
+ if (typeof item.skillId !== "string" || !item.skillId.trim() || typeof item.name !== "string" || !item.name.trim() || typeof item.version !== "string" || typeof item.toolCallId !== "string" || !item.toolCallId.trim() || typeof item.loadedAt !== "string" || !Number.isFinite(Date.parse(item.loadedAt)))
575
+ continue;
576
+ usages.push({
577
+ skillId: item.skillId,
578
+ name: item.name,
579
+ version: item.version,
580
+ source: { type: source.type, id: source.id },
581
+ activation: "read",
582
+ toolCallId: item.toolCallId,
583
+ ...typeof item.executionId === "string" && item.executionId ? { executionId: item.executionId } : {},
584
+ loadedAt: item.loadedAt
585
+ });
586
+ }
587
+ return mergeChatSkillUsages(usages);
588
+ }
589
+ function summarySkillUsages(summary) {
590
+ return summary && typeof summary === "object" && "version" in summary && summary.version === 1 && "skillUsages" in summary ? normalizeChatSkillUsages(summary.skillUsages) : [];
591
+ }
592
+ function getMessageSkillUsages(message) {
593
+ var _a;
594
+ const groups = [summarySkillUsages(message.taskSummary)];
595
+ if (Array.isArray(message.content)) {
596
+ for (const part of message.content) {
597
+ if ((part == null ? void 0 : part.type) === "component" && ((_a = part.data) == null ? void 0 : _a.status) === "success") {
598
+ groups.push(summarySkillUsages(part.data.taskSummary));
599
+ }
600
+ }
601
+ }
602
+ return mergeChatSkillUsages(...groups);
603
+ }
527
604
  const TOOL_OUTPUT_PRESENTATION_TYPE = "xpert.tool-output";
528
605
  const TOOL_OUTPUT_PRESENTATION_VERSION = 1;
606
+ function parseFileActivityContent(value) {
607
+ if (!value || typeof value !== "object") return null;
608
+ const part = value;
609
+ if (part.type !== "file_activity" || typeof part.id !== "string" || !part.id || !part.data || typeof part.data !== "object") return null;
610
+ const data = part.data;
611
+ if (data.version !== 1 || data.receiptId !== part.id || typeof data.toolCallId !== "string" || !data.toolCallId || typeof data.updatedAt !== "string" || !Number.isFinite(Date.parse(data.updatedAt))) return null;
612
+ const base = { version: 1, receiptId: part.id, toolCallId: data.toolCallId, updatedAt: data.updatedAt };
613
+ let receipt;
614
+ if (data.kind === "changes" && ["bounded", "partial", "unavailable"].includes(data.coverage) && Array.isArray(data.fileChanges)) {
615
+ const fileChanges = normalizeFileChanges(data.fileChanges);
616
+ if (fileChanges.length !== data.fileChanges.length) return null;
617
+ receipt = { ...base, kind: "changes", coverage: data.coverage, fileChanges };
618
+ } else if (data.kind === "delivery" && data.status === "success" && Array.isArray(data.outputs)) {
619
+ const outputs = normalizeFileOutputs(data.outputs);
620
+ if (outputs.length !== data.outputs.length) return null;
621
+ receipt = { ...base, kind: "delivery", status: "success", outputs };
622
+ } else if (data.kind === "delivery" && data.status === "error" && typeof data.error === "string") {
623
+ receipt = { ...base, kind: "delivery", status: "error", error: data.error };
624
+ } else return null;
625
+ return {
626
+ id: part.id,
627
+ type: "file_activity",
628
+ data: receipt,
629
+ ...typeof part.messageId === "string" ? { messageId: part.messageId } : {},
630
+ ...typeof part.executionId === "string" ? { executionId: part.executionId } : {},
631
+ ...typeof part.agentKey === "string" ? { agentKey: part.agentKey } : {},
632
+ ...typeof part.parentExecutionId === "string" ? { parentExecutionId: part.parentExecutionId } : {},
633
+ ...typeof part.runId === "string" ? { runId: part.runId } : {}
634
+ };
635
+ }
636
+ function getLegacyFileActivityContribution(value) {
637
+ var _a;
638
+ if (!value || typeof value !== "object") return null;
639
+ const part = value;
640
+ const step = part.type === "component" ? part.data : value;
641
+ if (!step || typeof step !== "object") return null;
642
+ const summary = step.taskSummary ?? ((_a = step._meta) == null ? void 0 : _a["xpertai/taskSummary"]);
643
+ const id = part.id ?? step.id;
644
+ return (summary == null ? void 0 : summary.version) === 1 && summary.fileActivityVersion === 1 && typeof summary.fileActivityToolCallId === "string" && Boolean(summary.fileActivityToolCallId) && typeof id === "string" && id !== summary.fileActivityToolCallId ? summary : null;
645
+ }
646
+ function isFileActivityContent(value) {
647
+ return Boolean(value && typeof value === "object" && "type" in value && value.type === "file_activity") || getLegacyFileActivityContribution(value) !== null;
648
+ }
649
+ function getFileActivityContribution(value) {
650
+ const part = parseFileActivityContent(value);
651
+ if (!part) return getLegacyFileActivityContribution(value);
652
+ const receipt = part.data;
653
+ return {
654
+ version: 1,
655
+ fileActivityVersion: 1,
656
+ fileActivityToolCallId: receipt.toolCallId,
657
+ ...receipt.kind === "changes" ? { fileChanges: receipt.fileChanges, fileChangeCoverage: receipt.coverage } : receipt.status === "success" ? { outputs: receipt.outputs } : {}
658
+ };
659
+ }
660
+ function upsertFileActivityContent(content, receipt) {
661
+ const index = content.findIndex((item) => {
662
+ var _a;
663
+ return ((_a = parseFileActivityContent(item)) == null ? void 0 : _a.id) === receipt.id;
664
+ });
665
+ if (index < 0) return [...content, receipt];
666
+ const previous = parseFileActivityContent(content[index]);
667
+ if (previous && Date.parse(previous.data.updatedAt) >= Date.parse(receipt.data.updatedAt)) return content;
668
+ return content.map((item, position) => position === index ? receipt : item);
669
+ }
670
+ function fileChangeRangeKey(resource) {
671
+ return JSON.stringify([resource.first.artifactId, resource.first.artifactVersionId, resource.last.artifactId, resource.last.artifactVersionId]);
672
+ }
673
+ const writeTools = /* @__PURE__ */ new Set(["sandbox_write_file", "sandbox_append_file", "sandbox_edit_file", "sandbox_multi_edit_file"]);
674
+ function normalizeFileChanges(value) {
675
+ if (!Array.isArray(value)) return [];
676
+ return value.filter((item) => {
677
+ if (!item || typeof item !== "object") return false;
678
+ const change = item;
679
+ return typeof change.id === "string" && typeof change.workspacePath === "string" && typeof change.title === "string" && ["added", "modified", "deleted", "unknown"].includes(change.operation ?? "") && ["observed", "legacy"].includes(change.coverage ?? "") && (!change.resource || change.resource.type === "file_change" && validVersion(change.resource.first) && validVersion(change.resource.last)) && validRevision(change.before) && validRevision(change.after);
680
+ }).map(({ id, title, workspacePath, operation, before, after, resource, coverage, messageId, startedAt, updatedAt }) => ({
681
+ id,
682
+ title,
683
+ workspacePath,
684
+ operation,
685
+ before: compactRevision(before),
686
+ after: compactRevision(after),
687
+ resource,
688
+ coverage,
689
+ messageId,
690
+ startedAt,
691
+ updatedAt
692
+ }));
693
+ }
694
+ function compactRevision(value) {
695
+ return value ? { sha256: value.sha256, size: value.size } : value;
696
+ }
697
+ function normalizeFileOutputs(value) {
698
+ if (!Array.isArray(value)) return [];
699
+ return value.flatMap((item) => {
700
+ if (!item || typeof item !== "object") return [];
701
+ const output = compactOutput(item);
702
+ return output ? [output] : [];
703
+ });
704
+ }
705
+ function compactOutput(value) {
706
+ if (!value || typeof value.id !== "string" || typeof value.title !== "string" || !["file", "image", "document", "spreadsheet", "presentation", "site", "url", "mcp_app"].includes(value.kind) || value.status !== void 0 && value.status !== "success") return null;
707
+ const ref = value.resource;
708
+ const resource = (ref == null ? void 0 : ref.type) === "artifact" && typeof ref.artifactId === "string" ? { type: "artifact", artifactId: ref.artifactId, ...typeof ref.artifactVersionId === "string" ? { artifactVersionId: ref.artifactVersionId } : {} } : (ref == null ? void 0 : ref.type) === "workspace_file" && typeof ref.workspacePath === "string" ? { type: "workspace_file", workspacePath: ref.workspacePath, fileAssetId: ref.fileAssetId, storageFileId: ref.storageFileId } : (ref == null ? void 0 : ref.type) === "url" && typeof ref.url === "string" ? { type: "url", url: ref.url } : void 0;
709
+ if (!resource) return null;
710
+ const { id, kind, title, description, status, origin, workspacePath, mimeType, size, sha256, messageId, updatedAt } = value;
711
+ return { id, kind, title, description, status, resource, origin, workspacePath, mimeType, size, sha256, messageId, updatedAt };
712
+ }
713
+ function validVersion(value) {
714
+ return Boolean(value && typeof value.artifactId === "string" && typeof value.artifactVersionId === "string");
715
+ }
716
+ function validRevision(value) {
717
+ return value == null || typeof value.sha256 === "string" && /^[a-f0-9]{64}$/.test(value.sha256) && Number.isSafeInteger(value.size) && value.size >= 0;
718
+ }
719
+ function mergeFileChanges(items) {
720
+ const byPath = /* @__PURE__ */ new Map();
721
+ for (const item of [...items].sort((a, b) => (a.startedAt ?? a.updatedAt ?? "").localeCompare(b.startedAt ?? b.updatedAt ?? ""))) {
722
+ const previous = byPath.get(item.workspacePath);
723
+ if (!previous || previous.coverage === "legacy") {
724
+ byPath.set(item.workspacePath, item);
725
+ continue;
726
+ }
727
+ if (item.coverage === "legacy") continue;
728
+ const latest = (item.updatedAt ?? "") >= (previous.updatedAt ?? "") ? item : previous;
729
+ const earliest = (item.startedAt ?? item.updatedAt ?? "") < (previous.startedAt ?? previous.updatedAt ?? "") ? item : previous;
730
+ const before = earliest.before;
731
+ const after = latest.after;
732
+ byPath.set(item.workspacePath, {
733
+ ...latest,
734
+ before,
735
+ after,
736
+ startedAt: earliest.startedAt ?? earliest.updatedAt,
737
+ operation: before === null ? "added" : after === null ? "deleted" : "modified",
738
+ resource: earliest.resource && latest.resource ? { type: "file_change", first: earliest.resource.first, last: latest.resource.last } : latest.resource
739
+ });
740
+ }
741
+ return [...byPath.values()].filter((item) => {
742
+ var _a, _b;
743
+ return item.coverage === "legacy" || ((_a = item.before) == null ? void 0 : _a.sha256) !== ((_b = item.after) == null ? void 0 : _b.sha256);
744
+ }).sort((a, b) => (b.updatedAt ?? "").localeCompare(a.updatedAt ?? ""));
745
+ }
746
+ function projectMessageFileActivity(message, contribution) {
747
+ var _a, _b, _c, _d, _e;
748
+ const parts = Array.isArray(message.content) ? message.content : [];
749
+ const observed = [];
750
+ const legacyChanges = [];
751
+ const legacyOutputIds = /* @__PURE__ */ new Set();
752
+ const date = message.updatedAt ?? message.createdAt;
753
+ const updatedAt = date instanceof Date ? date.toISOString() : date;
754
+ const observedToolCalls = new Set(parts.flatMap((part) => {
755
+ var _a2, _b2, _c2;
756
+ const summary = getFileActivityContribution(part) ?? ((_a2 = part.data) == null ? void 0 : _a2.taskSummary) ?? ((_c2 = (_b2 = part.data) == null ? void 0 : _b2._meta) == null ? void 0 : _c2["xpertai/taskSummary"]);
757
+ return (summary == null ? void 0 : summary.fileActivityToolCallId) ? [summary.fileActivityToolCallId] : [];
758
+ }));
759
+ for (const part of parts) {
760
+ const receipt = getFileActivityContribution(part);
761
+ if (receipt) {
762
+ observed.push(receipt);
763
+ continue;
764
+ }
765
+ if ((part == null ? void 0 : part.type) !== "component" || !part.data) continue;
766
+ const data = part.data;
767
+ const explicit = data.taskSummary ?? ((_a = data._meta) == null ? void 0 : _a["xpertai/taskSummary"]);
768
+ if ((explicit == null ? void 0 : explicit.version) === 1) observed.push(explicit);
769
+ if (!writeTools.has(data.tool ?? "") || data.status !== "success") continue;
770
+ const path = (_b = data.input) == null ? void 0 : _b.file_path;
771
+ if (typeof path !== "string" || !path || path.startsWith("/") || path.split("/").includes("..")) continue;
772
+ legacyOutputIds.add(`workspace-file:${path}`);
773
+ if ((explicit == null ? void 0 : explicit.fileActivityVersion) === 1 || part.id && observedToolCalls.has(part.id)) continue;
774
+ legacyChanges.push({ id: `file-change:${path}`, title: path, workspacePath: path, operation: "unknown", coverage: "legacy", messageId: message.id, updatedAt });
775
+ }
776
+ if (!observed.length && !legacyOutputIds.size && !((_c = contribution.outputs) == null ? void 0 : _c.length) && !((_d = contribution.fileChanges) == null ? void 0 : _d.length)) return contribution;
777
+ const outputs = [...contribution.outputs ?? [], ...observed.flatMap((item) => item.outputs ?? [])];
778
+ const byId = /* @__PURE__ */ new Map();
779
+ for (const raw of outputs) {
780
+ const output = compactOutput(raw);
781
+ if (!output) continue;
782
+ if (legacyOutputIds.has(output.id) && output.origin !== "tool" && output.origin !== "integration") continue;
783
+ if ((((_e = byId.get(output.id)) == null ? void 0 : _e.updatedAt) ?? "") > (output.updatedAt ?? updatedAt ?? "")) continue;
784
+ byId.set(output.id, { ...output, origin: output.origin ?? "legacy", messageId: message.id ?? output.messageId, updatedAt: output.updatedAt ?? updatedAt });
785
+ }
786
+ const changes = mergeFileChanges([
787
+ ...legacyChanges,
788
+ ...normalizeFileChanges(contribution.fileChanges),
789
+ ...observed.flatMap((item) => normalizeFileChanges(item.fileChanges))
790
+ ].map((item) => ({ ...item, messageId: message.id ?? item.messageId })));
791
+ const coverage = [contribution.fileChangeCoverage, ...observed.map((item) => item.fileChangeCoverage)];
792
+ const fileChangeCoverage = coverage.includes("unavailable") ? "unavailable" : coverage.includes("partial") ? "partial" : coverage.includes("bounded") ? "bounded" : void 0;
793
+ return { ...contribution, fileActivityVersion: 1, fileChangeCoverage, outputs: [...byId.values()], fileChanges: changes };
794
+ }
795
+ function parseFileChangeReport(value) {
796
+ if (!value || typeof value !== "object") return null;
797
+ const report = value;
798
+ const revision = (item) => item === null || Boolean(item && validRevision(item) && (item.text === void 0 || typeof item.text === "string" && item.text.length <= 32768));
799
+ return report.schema === "xpert.file-change.v1" && typeof report.workspacePath === "string" && revision(report.before) && revision(report.after) ? report : null;
800
+ }
801
+ const MAX_DIFF_CELLS = 4e6;
802
+ function countFileChangeLines(report) {
803
+ var _a, _b;
804
+ if (report.before && report.before.text === void 0 || report.after && report.after.text === void 0) return { status: "unavailable" };
805
+ const lines = (text) => text ? text.replace(/\r\n/g, "\n").replace(/\n$/, "").split("\n") : [];
806
+ const before = lines(((_a = report.before) == null ? void 0 : _a.text) ?? ""), after = lines(((_b = report.after) == null ? void 0 : _b.text) ?? "");
807
+ let start = 0, endBefore = before.length, endAfter = after.length;
808
+ while (start < endBefore && start < endAfter && before[start] === after[start]) start++;
809
+ while (endBefore > start && endAfter > start && before[endBefore - 1] === after[endAfter - 1]) {
810
+ endBefore--;
811
+ endAfter--;
812
+ }
813
+ const n = endBefore - start, m = endAfter - start;
814
+ if (n * m > MAX_DIFF_CELLS) return { status: "unavailable" };
815
+ let previous = new Uint32Array(m + 1), current = new Uint32Array(m + 1);
816
+ for (let i = 0; i < n; i++) {
817
+ for (let j = 0; j < m; j++) current[j + 1] = before[start + i] === after[start + j] ? previous[j] + 1 : Math.max(previous[j + 1], current[j]);
818
+ [previous, current] = [current, previous];
819
+ }
820
+ return { status: "ready", added: m - previous[m], removed: n - previous[m] };
821
+ }
529
822
  export {
530
823
  CHATKIT_TASK_SUMMARY_OPEN_RESOURCE_EFFECT,
531
824
  CHAT_EVENT_TYPE_FOLLOW_UP_CONSUMED,
@@ -545,12 +838,19 @@ export {
545
838
  STATE_VARIABLE_HUMAN,
546
839
  TOOL_OUTPUT_PRESENTATION_TYPE,
547
840
  TOOL_OUTPUT_PRESENTATION_VERSION,
841
+ chatSkillUsageKey,
548
842
  clampPetPosition,
843
+ countFileChangeLines,
844
+ fileChangeRangeKey,
845
+ getFileActivityContribution,
846
+ getLegacyFileActivityContribution,
847
+ getMessageSkillUsages,
549
848
  getPinnedPetPosition,
550
849
  isClientToolInterrupt,
551
850
  isClientToolInterruptPayload,
552
851
  isClientToolInterruptTask,
553
852
  isClientToolRequest,
853
+ isFileActivityContent,
554
854
  isHITLDecisionType,
555
855
  isHITLInterrupt,
556
856
  isHITLInterruptPayload,
@@ -565,13 +865,23 @@ export {
565
865
  isToolCallInterruptPayload,
566
866
  isToolCallInterruptTask,
567
867
  isToolCallRequest,
868
+ mergeChatSkillUsages,
869
+ mergeFileChanges,
568
870
  mergePetSpriteAtlas,
569
871
  normalizeBoundsPadding,
872
+ normalizeChatSkillUsages,
873
+ normalizeFileChanges,
874
+ normalizeFileOutputs,
570
875
  normalizeHITLRequest,
571
876
  normalizePetOptions,
572
877
  normalizeRequestLanguage,
878
+ normalizeThreadReference,
879
+ parseFileActivityContent,
880
+ parseFileChangeReport,
573
881
  petSpriteAtlas,
882
+ projectMessageFileActivity,
574
883
  resolveLocalizedText,
575
- resolvePetCharacter
884
+ resolvePetCharacter,
885
+ upsertFileActivityContent
576
886
  };
577
887
  //# sourceMappingURL=index.js.map