@canonmsg/backend-contracts 4.0.0 → 5.0.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/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @canonmsg/backend-contracts
2
+
3
+ The wire contracts Canon's server and its clients must agree on, in one zero-dependency package.
4
+
5
+ Canon's Cloud Functions API, the SSE stream service, `@canonmsg/core`, and the shared chat domain all encode and decode the same messages, turn states, policies, and verbs. When those rules live in more than one place they drift silently. They live here.
6
+
7
+ This is a low-level package. If you are building an agent, you want [`@canonmsg/agent-sdk`](https://www.npmjs.com/package/@canonmsg/agent-sdk); if you are binding Canon's verbs into a model runtime, you want [`@canonmsg/agent-tools`](https://www.npmjs.com/package/@canonmsg/agent-tools). Reach for this package when you are writing a server, a serializer, or a second implementation of the wire.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install @canonmsg/backend-contracts
13
+ ```
14
+
15
+ Node.js 18+. Ships both ESM and CommonJS builds — Cloud Functions consume the CJS entry, everything else the ESM one — so the same contract runs on both sides of the wire.
16
+
17
+ ## What is in it
18
+
19
+ **The verb contract.** `canon.verbs.v1` (`verbContract.ts`) is the plaintext *intent* schema — sixteen verbs, their input and result schemas, byte limits, and rate limits. `canon.verb-wire.v1` (`verbWire.ts`) is what actually crosses the network: a server-readable envelope plus a body that is either `{ encoding: 'json', value }` or `{ encoding: 'mls', … }`. `projectVerbIntentToWire` and `mergeVerbWireToIntent` are the two halves of that split, so bindings and the server never disagree about which fields are envelope and which are content.
20
+
21
+ ```ts
22
+ import {
23
+ CANON_VERB_NAMES,
24
+ getVerbInputSchema,
25
+ projectVerbIntentToWire,
26
+ } from '@canonmsg/backend-contracts';
27
+ ```
28
+
29
+ The JSON Schemas are also emitted as files for non-JavaScript consumers: `@canonmsg/backend-contracts/canon-verbs.schema.json`, `canon-verb-wire.schema.json`, and `canon-verbs.limits.json`.
30
+
31
+ **The turn protocol.** Turn lifecycle states, message semantics, delivery intents, and the trigger rules that decide whether an inbound message starts an agent turn.
32
+
33
+ **Message and media serialization.** The canonical `SerializedContentType` union (`text | image | audio | video | file | contact_card | interaction`), attachment normalization, and the single runtime-card decoder that `@canonmsg/core` re-exports rather than reimplements.
34
+
35
+ **Behavior policy and contact requests.** The participation evaluator the stream service runs before dispatching a turn, and the contact-request serializer both sides validate against.
36
+
37
+ **Environments.** `CANON_ENVIRONMENT_CONTRACTS` binds `canon-dev-v1` and `canon-prod-v1` to their project and region; `getCanonEnvironmentContract` rejects anything else rather than defaulting.
38
+
39
+ **Diff redaction.** Approval diffs carry pre-image context into a message every conversation member can read, so secret-shaped paths and tokens are suppressed before send — by the emitting host, and again defensively by the server.
40
+
41
+ Agent-facing reference: [API contracts](https://canonmail.com/agents/contracts)
@@ -52,16 +52,9 @@ function resolveGroupMentionRequirement(input) {
52
52
  && typeof input.requireMentionForGroupReplies !== 'boolean') {
53
53
  throw new Error('requireMentionForGroupReplies must be a boolean');
54
54
  }
55
- if (input.requireMentionForGroupAgentReplies !== undefined
56
- && typeof input.requireMentionForGroupAgentReplies !== 'boolean') {
57
- throw new Error('requireMentionForGroupAgentReplies must be a boolean');
58
- }
59
55
  if (typeof input.requireMentionForGroupReplies === 'boolean') {
60
56
  return input.requireMentionForGroupReplies;
61
57
  }
62
- if (typeof input.requireMentionForGroupAgentReplies === 'boolean') {
63
- return input.requireMentionForGroupAgentReplies;
64
- }
65
58
  return undefined;
66
59
  }
67
60
  export function parseAgentBehaviorSettings(raw) {
@@ -104,9 +97,7 @@ export function normalizeStoredAgentBehaviorPolicy(raw) {
104
97
  : {}),
105
98
  ...(typeof raw.requireMentionForGroupReplies === 'boolean'
106
99
  ? { requireMentionForGroupReplies: raw.requireMentionForGroupReplies }
107
- : typeof raw.requireMentionForGroupAgentReplies === 'boolean'
108
- ? { requireMentionForGroupReplies: raw.requireMentionForGroupAgentReplies }
109
- : {}),
100
+ : {}),
110
101
  ...(typeof raw.maxConsecutiveAgentTurns === 'number' || raw.maxConsecutiveAgentTurns === null
111
102
  ? { maxConsecutiveAgentTurns: raw.maxConsecutiveAgentTurns }
112
103
  : {}),
@@ -153,7 +153,7 @@
153
153
  },
154
154
  "messageOptions": {
155
155
  "type": "object",
156
- "description": "Message composition options (mirrors POST /messages/send). Deliberately absent: contact_card contentType (use share_contact) and forwarded/forwardedFrom (forwarding stays host/REST-mediated; /messages/forward is not a v1 verb).",
156
+ "description": "Message composition options (mirrors POST /messages/send). Deliberately absent: contact_card contentType (use share_contact) and forwarded/forwardedFrom (forwarding has its own verb — use forward, not send_to).",
157
157
  "additionalProperties": false,
158
158
  "properties": {
159
159
  "messageId": {
@@ -661,7 +661,7 @@
661
661
  }
662
662
  },
663
663
  "send_to_result": {
664
- "description": "Canonical outcome vocabulary. Legacy binding drift documented in CANON_VERB_BINDINGS (hermes 'opened' -> messaged without messageId; hermes 'denied' -> blocked).",
664
+ "description": "Canonical outcome vocabulary. Legacy binding drift documented in docs/design/verb-bindings.md (hermes 'opened' -> messaged without messageId; hermes 'denied' -> blocked).",
665
665
  "oneOf": [
666
666
  {
667
667
  "type": "object",
@@ -62,16 +62,9 @@ function resolveGroupMentionRequirement(input) {
62
62
  && typeof input.requireMentionForGroupReplies !== 'boolean') {
63
63
  throw new Error('requireMentionForGroupReplies must be a boolean');
64
64
  }
65
- if (input.requireMentionForGroupAgentReplies !== undefined
66
- && typeof input.requireMentionForGroupAgentReplies !== 'boolean') {
67
- throw new Error('requireMentionForGroupAgentReplies must be a boolean');
68
- }
69
65
  if (typeof input.requireMentionForGroupReplies === 'boolean') {
70
66
  return input.requireMentionForGroupReplies;
71
67
  }
72
- if (typeof input.requireMentionForGroupAgentReplies === 'boolean') {
73
- return input.requireMentionForGroupAgentReplies;
74
- }
75
68
  return undefined;
76
69
  }
77
70
  function parseAgentBehaviorSettings(raw) {
@@ -114,9 +107,7 @@ function normalizeStoredAgentBehaviorPolicy(raw) {
114
107
  : {}),
115
108
  ...(typeof raw.requireMentionForGroupReplies === 'boolean'
116
109
  ? { requireMentionForGroupReplies: raw.requireMentionForGroupReplies }
117
- : typeof raw.requireMentionForGroupAgentReplies === 'boolean'
118
- ? { requireMentionForGroupReplies: raw.requireMentionForGroupAgentReplies }
119
- : {}),
110
+ : {}),
120
111
  ...(typeof raw.maxConsecutiveAgentTurns === 'number' || raw.maxConsecutiveAgentTurns === null
121
112
  ? { maxConsecutiveAgentTurns: raw.maxConsecutiveAgentTurns }
122
113
  : {}),
@@ -5,9 +5,6 @@ function isRecord(value) {
5
5
  return typeof value === 'object' && value !== null && !Array.isArray(value);
6
6
  }
7
7
  function decodeStoredRuntimeCard(input) {
8
- if (isRecord(input.runtimeCard)) {
9
- return input.runtimeCard;
10
- }
11
8
  if (typeof input.runtimeCardJson !== 'string' || !input.runtimeCardJson.trim()) {
12
9
  return undefined;
13
10
  }
@@ -23,8 +23,9 @@
23
23
  *
24
24
  * Scope note: this module DESCRIBES the contract (canonical shapes + the
25
25
  * server-enforced limits, with enforcement sites cited). Enforcement itself
26
- * stays where it runs today — functions/src. Server-side verb endpoints that
27
- * validate against these schemas are a planned follow-up.
26
+ * stays where it runs today — functions/src. The server-side verb endpoint that
27
+ * validates against these schemas is live: POST /agent/verbs/:verb
28
+ * (functions/src/index.ts, functions/src/api/agentVerbs.ts).
28
29
  *
29
30
  * Normativity: the JSON Schemas in `verbSchemas.ts` (and the emitted
30
31
  * canon-verbs.schema.json) are the normative contract. The TypeScript types
@@ -125,7 +125,7 @@ const messageOptionsDef = {
125
125
  type: 'object',
126
126
  description: 'Message composition options (mirrors POST /messages/send). Deliberately absent: '
127
127
  + 'contact_card contentType (use share_contact) and forwarded/forwardedFrom '
128
- + '(forwarding stays host/REST-mediated; /messages/forward is not a v1 verb).',
128
+ + '(forwarding has its own verb — use forward, not send_to).',
129
129
  additionalProperties: false,
130
130
  properties: {
131
131
  messageId: REF('messageId'),
@@ -404,7 +404,7 @@ const send_to_input = {
404
404
  dependentRequired: { selfContext: ['sourceConversationId'] },
405
405
  };
406
406
  const send_to_result = {
407
- description: 'Canonical outcome vocabulary. Legacy binding drift documented in CANON_VERB_BINDINGS '
407
+ description: 'Canonical outcome vocabulary. Legacy binding drift documented in docs/design/verb-bindings.md '
408
408
  + "(hermes 'opened' -> messaged without messageId; hermes 'denied' -> blocked).",
409
409
  oneOf: [
410
410
  {
@@ -1,4 +1,3 @@
1
1
  export declare function decodeStoredRuntimeCard(input: {
2
- runtimeCard?: unknown;
3
2
  runtimeCardJson?: unknown;
4
3
  }): Record<string, unknown> | undefined;
@@ -2,9 +2,6 @@ function isRecord(value) {
2
2
  return typeof value === 'object' && value !== null && !Array.isArray(value);
3
3
  }
4
4
  export function decodeStoredRuntimeCard(input) {
5
- if (isRecord(input.runtimeCard)) {
6
- return input.runtimeCard;
7
- }
8
5
  if (typeof input.runtimeCardJson !== 'string' || !input.runtimeCardJson.trim()) {
9
6
  return undefined;
10
7
  }
@@ -22,8 +22,9 @@
22
22
  *
23
23
  * Scope note: this module DESCRIBES the contract (canonical shapes + the
24
24
  * server-enforced limits, with enforcement sites cited). Enforcement itself
25
- * stays where it runs today — functions/src. Server-side verb endpoints that
26
- * validate against these schemas are a planned follow-up.
25
+ * stays where it runs today — functions/src. The server-side verb endpoint that
26
+ * validates against these schemas is live: POST /agent/verbs/:verb
27
+ * (functions/src/index.ts, functions/src/api/agentVerbs.ts).
27
28
  *
28
29
  * Normativity: the JSON Schemas in `verbSchemas.ts` (and the emitted
29
30
  * canon-verbs.schema.json) are the normative contract. The TypeScript types
@@ -22,8 +22,9 @@
22
22
  *
23
23
  * Scope note: this module DESCRIBES the contract (canonical shapes + the
24
24
  * server-enforced limits, with enforcement sites cited). Enforcement itself
25
- * stays where it runs today — functions/src. Server-side verb endpoints that
26
- * validate against these schemas are a planned follow-up.
25
+ * stays where it runs today — functions/src. The server-side verb endpoint that
26
+ * validates against these schemas is live: POST /agent/verbs/:verb
27
+ * (functions/src/index.ts, functions/src/api/agentVerbs.ts).
27
28
  *
28
29
  * Normativity: the JSON Schemas in `verbSchemas.ts` (and the emitted
29
30
  * canon-verbs.schema.json) are the normative contract. The TypeScript types
@@ -120,7 +120,7 @@ const messageOptionsDef = {
120
120
  type: 'object',
121
121
  description: 'Message composition options (mirrors POST /messages/send). Deliberately absent: '
122
122
  + 'contact_card contentType (use share_contact) and forwarded/forwardedFrom '
123
- + '(forwarding stays host/REST-mediated; /messages/forward is not a v1 verb).',
123
+ + '(forwarding has its own verb — use forward, not send_to).',
124
124
  additionalProperties: false,
125
125
  properties: {
126
126
  messageId: REF('messageId'),
@@ -399,7 +399,7 @@ const send_to_input = {
399
399
  dependentRequired: { selfContext: ['sourceConversationId'] },
400
400
  };
401
401
  const send_to_result = {
402
- description: 'Canonical outcome vocabulary. Legacy binding drift documented in CANON_VERB_BINDINGS '
402
+ description: 'Canonical outcome vocabulary. Legacy binding drift documented in docs/design/verb-bindings.md '
403
403
  + "(hermes 'opened' -> messaged without messageId; hermes 'denied' -> blocked).",
404
404
  oneOf: [
405
405
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonmsg/backend-contracts",
3
- "version": "4.0.0",
3
+ "version": "5.0.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",
@@ -43,7 +43,7 @@
43
43
  "access": "public"
44
44
  },
45
45
  "devDependencies": {
46
- "@canonmsg/rich-cards": "^0.8.5",
46
+ "@canonmsg/rich-cards": "^0.8.7",
47
47
  "@types/node": "^22.0.0",
48
48
  "ajv": "^8.20.0",
49
49
  "typescript": "~5.7.0",