@canonmsg/backend-contracts 1.1.0 → 1.2.0

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/cjs/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./media.js"), exports);
18
18
  __exportStar(require("./message.js"), exports);
19
+ __exportStar(require("./runtimeCardStorage.js"), exports);
19
20
  __exportStar(require("./turnProtocol.js"), exports);
20
21
  __exportStar(require("./agentBehaviorPolicy.js"), exports);
21
22
  __exportStar(require("./contactRequest.js"), exports);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.serializeStoredMessage = serializeStoredMessage;
4
4
  const media_js_1 = require("./media.js");
5
+ const runtimeCardStorage_js_1 = require("./runtimeCardStorage.js");
5
6
  function isRecord(value) {
6
7
  return typeof value === 'object' && value !== null && !Array.isArray(value);
7
8
  }
@@ -10,6 +11,18 @@ function normalizeStringArray(value) {
10
11
  return [];
11
12
  return value.filter((entry) => typeof entry === 'string');
12
13
  }
14
+ function normalizeReactions(value) {
15
+ if (!isRecord(value))
16
+ return undefined;
17
+ const reactions = {};
18
+ for (const [reaction, userIds] of Object.entries(value)) {
19
+ const normalizedUserIds = normalizeStringArray(userIds);
20
+ if (reaction.length > 0 && normalizedUserIds.length > 0) {
21
+ reactions[reaction] = normalizedUserIds;
22
+ }
23
+ }
24
+ return Object.keys(reactions).length > 0 ? reactions : undefined;
25
+ }
13
26
  function normalizeTimestamp(value) {
14
27
  if (value instanceof Date)
15
28
  return value;
@@ -132,8 +145,13 @@ function serializeStoredMessage(input) {
132
145
  if (contactCard) {
133
146
  result.contactCard = contactCard;
134
147
  }
135
- if (isRecord(data.runtimeCard)) {
136
- result.runtimeCard = data.runtimeCard;
148
+ const runtimeCard = (0, runtimeCardStorage_js_1.decodeStoredRuntimeCard)(data);
149
+ if (runtimeCard) {
150
+ result.runtimeCard = runtimeCard;
151
+ }
152
+ const reactions = normalizeReactions(data.reactions);
153
+ if (reactions) {
154
+ result.reactions = reactions;
137
155
  }
138
156
  return result;
139
157
  }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeStoredRuntimeCard = decodeStoredRuntimeCard;
4
+ function isRecord(value) {
5
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
6
+ }
7
+ function decodeStoredRuntimeCard(input) {
8
+ if (isRecord(input.runtimeCard)) {
9
+ return input.runtimeCard;
10
+ }
11
+ if (typeof input.runtimeCardJson !== 'string' || !input.runtimeCardJson.trim()) {
12
+ return undefined;
13
+ }
14
+ try {
15
+ const parsed = JSON.parse(input.runtimeCardJson);
16
+ return isRecord(parsed) ? parsed : undefined;
17
+ }
18
+ catch {
19
+ return undefined;
20
+ }
21
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './media.js';
2
2
  export * from './message.js';
3
+ export * from './runtimeCardStorage.js';
3
4
  export * from './turnProtocol.js';
4
5
  export * from './agentBehaviorPolicy.js';
5
6
  export * from './contactRequest.js';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './media.js';
2
2
  export * from './message.js';
3
+ export * from './runtimeCardStorage.js';
3
4
  export * from './turnProtocol.js';
4
5
  export * from './agentBehaviorPolicy.js';
5
6
  export * from './contactRequest.js';
package/dist/message.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface ForwardedFrom {
6
6
  sourceConversationId: string;
7
7
  messageId: string;
8
8
  }
9
+ export type SerializedReactions = Record<string, string[]>;
9
10
  export interface SerializedContactCard {
10
11
  userId: string;
11
12
  displayName: string;
@@ -38,6 +39,7 @@ export interface SerializedMessage {
38
39
  contactCard?: SerializedContactCard;
39
40
  /** Durable canon.card.v1 runtime card content, passed through opaquely. */
40
41
  runtimeCard?: Record<string, unknown>;
42
+ reactions?: SerializedReactions;
41
43
  }
42
44
  export interface SerializeMessageInput {
43
45
  id: string;
package/dist/message.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { normalizeStoredAttachments } from './media.js';
2
+ import { decodeStoredRuntimeCard } from './runtimeCardStorage.js';
2
3
  function isRecord(value) {
3
4
  return typeof value === 'object' && value !== null && !Array.isArray(value);
4
5
  }
@@ -7,6 +8,18 @@ function normalizeStringArray(value) {
7
8
  return [];
8
9
  return value.filter((entry) => typeof entry === 'string');
9
10
  }
11
+ function normalizeReactions(value) {
12
+ if (!isRecord(value))
13
+ return undefined;
14
+ const reactions = {};
15
+ for (const [reaction, userIds] of Object.entries(value)) {
16
+ const normalizedUserIds = normalizeStringArray(userIds);
17
+ if (reaction.length > 0 && normalizedUserIds.length > 0) {
18
+ reactions[reaction] = normalizedUserIds;
19
+ }
20
+ }
21
+ return Object.keys(reactions).length > 0 ? reactions : undefined;
22
+ }
10
23
  function normalizeTimestamp(value) {
11
24
  if (value instanceof Date)
12
25
  return value;
@@ -129,8 +142,13 @@ export function serializeStoredMessage(input) {
129
142
  if (contactCard) {
130
143
  result.contactCard = contactCard;
131
144
  }
132
- if (isRecord(data.runtimeCard)) {
133
- result.runtimeCard = data.runtimeCard;
145
+ const runtimeCard = decodeStoredRuntimeCard(data);
146
+ if (runtimeCard) {
147
+ result.runtimeCard = runtimeCard;
148
+ }
149
+ const reactions = normalizeReactions(data.reactions);
150
+ if (reactions) {
151
+ result.reactions = reactions;
134
152
  }
135
153
  return result;
136
154
  }
@@ -0,0 +1,4 @@
1
+ export declare function decodeStoredRuntimeCard(input: {
2
+ runtimeCard?: unknown;
3
+ runtimeCardJson?: unknown;
4
+ }): Record<string, unknown> | undefined;
@@ -0,0 +1,18 @@
1
+ function isRecord(value) {
2
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
3
+ }
4
+ export function decodeStoredRuntimeCard(input) {
5
+ if (isRecord(input.runtimeCard)) {
6
+ return input.runtimeCard;
7
+ }
8
+ if (typeof input.runtimeCardJson !== 'string' || !input.runtimeCardJson.trim()) {
9
+ return undefined;
10
+ }
11
+ try {
12
+ const parsed = JSON.parse(input.runtimeCardJson);
13
+ return isRecord(parsed) ? parsed : undefined;
14
+ }
15
+ catch {
16
+ return undefined;
17
+ }
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonmsg/backend-contracts",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Canon backend contract helpers shared by Functions and stream-service",
5
5
  "type": "module",
6
6
  "main": "dist/cjs/index.js",