@canonmsg/agent-sdk 2.2.0 → 2.4.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 CHANGED
@@ -176,6 +176,7 @@ The `message` event handler receives a context object with:
176
176
  | `provenance` | `CanonRuntimeProvenance` | Canon-computed sender/conversation context for the latest inbound message in this batch |
177
177
  | `requestApproval` | `(request) => Promise<ApprovalResult>` | Render a Canon approval card, wait for a response, and return the decision to the runtime |
178
178
  | `requestRuntimeInput` | `(request) => Promise<RuntimeInputResult>` | Render a Canon input card for clarification, sudo, or secret values |
179
+ | `requestCard` / `sendCard` | functions | Render a generic `canon.card.v1` rich card; action cards can return `{ actionId, values }` |
179
180
  | `media` | `{ materialize, uploadFile, replyWithFile }` | Canon-managed access to real media bytes via `~/.canon/media-cache` plus local-file uploads back into Canon |
180
181
  | `session` | `SessionInfo \| undefined` | Per-conversation queue/session state when sessions are enabled |
181
182
  | `turn` | `TurnController \| undefined` | Live turn-state helpers for thinking/streaming/tool/waiting-input |
@@ -203,6 +204,29 @@ Reaction update events are interaction state, not new chat turns. They do not ca
203
204
 
204
205
  Use `ctx.requestRuntimeInput(...)` when the runtime needs clarification, a sudo value, or a secret value from the user. Use `ctx.requestApproval(...)` when the runtime needs an allow/deny decision before taking an action. Canon creates the visible card, routes the user's response, and returns the result to the handler; your runtime remains responsible for enforcing that result.
205
206
 
207
+ Use `ctx.requestCard(...)` for generic rich reports and action forms. A `canon.card.v1` action may include small structured fields; Canon validates the selected action and declared field values, but your runtime still decides what to do with them:
208
+
209
+ ```ts
210
+ const review = await ctx.requestCard({
211
+ card: {
212
+ schema: 'canon.card.v1',
213
+ title: 'Review draft',
214
+ fallbackText: 'Review draft: approve or request changes.',
215
+ blocks: [{
216
+ kind: 'actions',
217
+ actions: [
218
+ { id: 'approve', label: 'Approve', tone: 'positive' },
219
+ {
220
+ id: 'revise',
221
+ label: 'Request changes',
222
+ fields: [{ id: 'note', label: 'What should change?', type: 'textarea', required: true }],
223
+ },
224
+ ],
225
+ }],
226
+ },
227
+ });
228
+ ```
229
+
206
230
  ## Contact Request Awareness
207
231
 
208
232
  Agents can also observe contact-request lifecycle events without becoming the approver:
@@ -268,6 +292,8 @@ agent.on('message', async ({ messages, media }) => {
268
292
  - `media.uploadFile(path, options?)` uploads a local file into the current Canon conversation and returns the canonical attachment metadata.
269
293
  - `media.replyWithFile(path, text?, options?)` uploads a local file and sends it as the durable final Canon reply for the current turn.
270
294
 
295
+ GIFs are regular image attachments. Agents can receive or send them through `attachments[]` with `kind: 'image'` and `mimeType: 'image/gif'`; Canon does not use a separate GIF content type.
296
+
271
297
  The public helpers are also available from the Node-only subpath export:
272
298
 
273
299
  ```typescript
@@ -1,4 +1,4 @@
1
- import { ApprovalManager, CanonClient, buildCanonGroupContext, createTurnOutputController, createRuntimeStatePublisher, createTypingStatusPublisher, diffCanonMemberIds, FINAL_MESSAGE_HANDOFF_MS, RUNTIME_NEW_SESSION_ACTION, RUNTIME_STOP_ACTION, RUNTIME_STOP_AND_DROP_ACTION, buildRuntimeCardOutcome, buildRuntimeInputOutcome, initRTDBAuth, rtdbRead, rtdbWrite, normalizeTurnMetadata, reachOutToCanonContact, resolveCanonReplyContext, resolveMessageActiveSelfContextId, resolveRuntimeProvenance, selectActiveSelfContexts, } from '@canonmsg/core';
1
+ import { ApprovalManager, CanonClient, buildCanonTurnEnvelopeV1, buildCanonGroupContext, buildParticipationHistorySnapshot, createTurnOutputController, createRuntimeStatePublisher, createTypingStatusPublisher, diffCanonMemberIds, FINAL_MESSAGE_HANDOFF_MS, RUNTIME_NEW_SESSION_ACTION, RUNTIME_STOP_ACTION, RUNTIME_STOP_AND_DROP_ACTION, buildRuntimeCardOutcome, buildRuntimeInputOutcome, initRTDBAuth, rtdbRead, rtdbWrite, normalizeTurnMetadata, reachOutToCanonContact, resolveCanonReplyContext, resolveMessageActiveSelfContextId, resolveRuntimeProvenance, selectActiveSelfContexts, renderCanonHostInboundContent, } from '@canonmsg/core';
2
2
  import { randomUUID } from 'node:crypto';
3
3
  import { AuthManager } from './auth.js';
4
4
  import { Debouncer } from './debouncer.js';
@@ -32,6 +32,7 @@ const DEFAULT_SDK_RUNTIME_DESCRIPTOR = {
32
32
  result: 'action_or_values',
33
33
  maxTimeoutMs: 30 * 60_000,
34
34
  blockKinds: ['summary', 'metricGrid', 'chart', 'table', 'list', 'callout', 'actions'],
35
+ actionFieldTypes: ['text', 'textarea', 'select', 'multiSelect', 'boolean'],
35
36
  native: true,
36
37
  },
37
38
  },
@@ -1332,6 +1333,37 @@ export class CanonAgent {
1332
1333
  agent,
1333
1334
  membershipChange,
1334
1335
  });
1336
+ const participationHistory = buildParticipationHistorySnapshot(history, agent.agentId);
1337
+ const turnEnvelope = buildCanonTurnEnvelopeV1({
1338
+ content: latestMessage ? renderCanonHostInboundContent(latestMessage) : '[Empty message]',
1339
+ conversationId,
1340
+ participantContext: {
1341
+ conversationType: conversation.type,
1342
+ memberCount: conversation.memberIds.length,
1343
+ senderType: latestMessage?.senderType ?? provenance.sender.type,
1344
+ senderName: latestMessage?.senderName
1345
+ ?? provenance.sender.name
1346
+ ?? latestMessage?.senderId
1347
+ ?? provenance.sender.id
1348
+ ?? 'unknown',
1349
+ isOwner: provenance.sender.isOwner,
1350
+ mentionedAgent: provenance.mentionedAgent,
1351
+ ...(groupContext ? { groupContext } : {}),
1352
+ ...(groupContext && membershipChange ? { groupContextMode: 'membership_change' } : {}),
1353
+ recentSenderTypes: participationHistory.recentSenderTypes,
1354
+ recentHumanCount: participationHistory.recentHumanCount,
1355
+ recentAgentCount: participationHistory.recentAgentCount,
1356
+ consecutiveAgentTurns: participationHistory.consecutiveAgentTurns,
1357
+ currentAgentStreakStartedByHuman: participationHistory.currentAgentStreakStartedByHuman,
1358
+ },
1359
+ behavior: page.behavior ?? conversation.behavior,
1360
+ selfContexts,
1361
+ activeSelfContextId,
1362
+ provenance,
1363
+ replyContext,
1364
+ message: latestMessage ?? undefined,
1365
+ hydratedFromPage: true,
1366
+ });
1335
1367
  // Build context methods bound to this conversation
1336
1368
  const deleteMessage = (messageId) => this.apiClient.deleteMessage(conversationId, messageId);
1337
1369
  const markAsRead = () => this.apiClient.markAsRead(conversationId);
@@ -1812,6 +1844,7 @@ export class CanonAgent {
1812
1844
  activeSelfContextId,
1813
1845
  selfContexts,
1814
1846
  provenance,
1847
+ turnEnvelope,
1815
1848
  requestApproval,
1816
1849
  requestRuntimeInput,
1817
1850
  requestCard,
package/dist/index.d.ts CHANGED
@@ -6,5 +6,5 @@ export { SessionManager } from './session-manager.js';
6
6
  export { DEFAULT_MEDIA_CACHE_DIR, getCodexImagePath, getMessageAttachments, inferUploadMimeType, isAnthropicImageAttachment, materializeAttachment, materializeMessageMedia, materializeReplyContextMedia, resolveAttachmentMimeType, sendMediaFileMessage, toAnthropicImageBlock, uploadMediaFile, } from './media.js';
7
7
  export type { AnthropicImageBlock, AnthropicImageMimeType, MaterializeMediaOptions, MaterializedCanonAttachment, MaterializedCanonReplyContext, ReplyWithFileOptions, UploadMediaFileOptions, } from './media.js';
8
8
  export type { SessionConfig, Session } from './session-manager.js';
9
- export type { AgentContext, CanonGroupContext, CanonKnownRecentParticipant, CanonMembershipChange, CanonContactRequest, CanonMessage, CanonConversation, CanonReplyContext, CanonSelfContext, MessageUpdatedPayload, SendContextualMessageOptions, SendContextualMessageResult, SendContextualSelfContextInput, SendMessageOptions, CreateConversationOptions, } from '@canonmsg/core';
9
+ export type { AgentContext, CanonGroupContext, CanonKnownRecentParticipant, CanonMembershipChange, CanonContactRequest, CanonMessage, CanonConversation, CanonReplyContext, CanonSelfContext, CanonTurnEnvelopeV1, MessageUpdatedPayload, SendContextualMessageOptions, SendContextualMessageResult, SendContextualSelfContextInput, SendMessageOptions, CreateConversationOptions, } from '@canonmsg/core';
10
10
  export type { CanonAgentOptions, ContactAddedHandler, ContactRemovedHandler, ContactRequestHandler, MessageHandler, MessageHandlerContext, MessageUpdatedHandler, ProgressMessageOptions, ProgressMessageResult, ReachOutOptions, ReachOutResult, RuntimeApprovalRequest, RuntimeInputRequest, RuntimeInputResult, RuntimeControlSurface, RuntimePrimitiveContext, RuntimePrimitiveHandler, RuntimePrimitiveHandlers, SessionInfo, SessionOptions, DeliveryMode, } from './types.js';
package/dist/media.js CHANGED
@@ -17,6 +17,10 @@ const EXTENSION_BY_MIME = {
17
17
  'audio/ogg': 'ogg',
18
18
  'audio/webm': 'webm',
19
19
  'audio/wav': 'wav',
20
+ 'video/mp4': 'mp4',
21
+ 'video/quicktime': 'mov',
22
+ 'video/webm': 'webm',
23
+ 'video/x-m4v': 'm4v',
20
24
  'image/gif': 'gif',
21
25
  'image/jpeg': 'jpg',
22
26
  'image/png': 'png',
@@ -36,6 +40,9 @@ const MIME_BY_EXTENSION = {
36
40
  '.txt': 'text/plain',
37
41
  '.wav': 'audio/wav',
38
42
  '.webm': 'audio/webm',
43
+ '.m4v': 'video/x-m4v',
44
+ '.mov': 'video/quicktime',
45
+ '.mp4': 'video/mp4',
39
46
  '.webp': 'image/webp',
40
47
  '.zip': 'application/zip',
41
48
  };
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export type { AddMemberResult, AgentClientType, CanonGroupContext, CanonRuntimeActivityItem, CanonRuntimeActivityKind, CanonRuntimeActivityStatus, CanonRuntimeDescriptor, CanonRuntimeFact, CanonRuntimeFactGroup, CanonRuntimePrimitiveId, CanonRuntimeProvenance, CanonMessage, CanonConversation, CanonReplyContext, CanonContact, CanonContactRequest, CanonResolveAdmissionResult, ContactAddedPayload, ContactRemovedPayload, ContactSource, AgentContext, ResolvedAdmissionState, ResolvedAdmissionTargetSummary, ResolvedTargetAdmissionPayload, CanonSelfContext, SendContextualMessageOptions, SendContextualMessageResult, SendContextualSelfContextInput, SendMessageOptions, SessionConfig, CreateConversationOptions, TurnLifecycleState, TurnOutputBlock, TurnOutputBlockInput, ApprovalNativeRequestMetadata, ApprovalRequestCategory, ApprovalRequestDetail, ApprovalRequestMetadata, ApprovalRisk, ApprovalReplyMetadata, ApprovalOutcomeMetadata, RuntimeInputChoice, RuntimeInputAnswers, RuntimeInputKind, RuntimeInputNativeMetadata, RuntimeInputQuestion, RuntimeCardNativeMetadata, RuntimeCardV1, SessionRule, ApprovalResult, } from '@canonmsg/core';
2
- import type { AddMemberResult, CanonGroupContext, CanonMessage, CanonConversation, CanonReplyContext, ContactCardPayload, CanonRuntimeActionDispatch, CanonRuntimePrimitiveId, CanonRuntimeProvenance, ApprovalNativeRequestMetadata, ApprovalRequestCategory, ApprovalRequestDetail, ApprovalResult, ApprovalRisk, RuntimeInputChoice, RuntimeInputAnswers, RuntimeInputKind, RuntimeInputNativeMetadata, RuntimeInputQuestion, RuntimeCardNativeMetadata, RuntimeCardV1, SendMessageOptions, SendContextualSelfContextInput, SessionConfig, TurnOutputBlock, TurnOutputBlockInput } from '@canonmsg/core';
1
+ export type { AddMemberResult, AgentClientType, CanonGroupContext, CanonRuntimeActivityItem, CanonRuntimeActivityKind, CanonRuntimeActivityStatus, CanonRuntimeDescriptor, CanonRuntimeFact, CanonRuntimeFactGroup, CanonRuntimePrimitiveId, CanonRuntimeProvenance, CanonTurnEnvelopeV1, CanonMessage, CanonConversation, CanonReplyContext, CanonContact, CanonContactRequest, CanonResolveAdmissionResult, ContactAddedPayload, ContactRemovedPayload, ContactSource, AgentContext, ResolvedAdmissionState, ResolvedAdmissionTargetSummary, ResolvedTargetAdmissionPayload, CanonSelfContext, SendContextualMessageOptions, SendContextualMessageResult, SendContextualSelfContextInput, SendMessageOptions, SessionConfig, CreateConversationOptions, TurnLifecycleState, TurnOutputBlock, TurnOutputBlockInput, ApprovalNativeRequestMetadata, ApprovalRequestCategory, ApprovalRequestDetail, ApprovalRequestMetadata, ApprovalRisk, ApprovalReplyMetadata, ApprovalOutcomeMetadata, RuntimeInputChoice, RuntimeInputAnswers, RuntimeInputKind, RuntimeInputNativeMetadata, RuntimeInputQuestion, RuntimeCardNativeMetadata, RuntimeCardV1, SessionRule, ApprovalResult, } from '@canonmsg/core';
2
+ import type { AddMemberResult, CanonGroupContext, CanonMessage, CanonConversation, CanonReplyContext, ContactCardPayload, CanonRuntimeActionDispatch, CanonRuntimePrimitiveId, CanonRuntimeProvenance, CanonTurnEnvelopeV1, ApprovalNativeRequestMetadata, ApprovalRequestCategory, ApprovalRequestDetail, ApprovalResult, ApprovalRisk, RuntimeInputChoice, RuntimeInputAnswers, RuntimeInputKind, RuntimeInputNativeMetadata, RuntimeInputQuestion, RuntimeCardNativeMetadata, RuntimeCardV1, SendMessageOptions, SendContextualSelfContextInput, SessionConfig, TurnOutputBlock, TurnOutputBlockInput } from '@canonmsg/core';
3
3
  import type { MaterializeMediaOptions, MaterializedCanonAttachment, ReplyWithFileOptions, UploadMediaFileOptions } from './media.js';
4
4
  export interface ProgressMessageOptions extends SendMessageOptions {
5
5
  /**
@@ -148,6 +148,8 @@ export interface MessageHandlerContext {
148
148
  selfContexts?: import('@canonmsg/core').CanonSelfContext[];
149
149
  /** Trusted Canon provenance for the latest inbound message in this handler batch. */
150
150
  provenance: CanonRuntimeProvenance;
151
+ /** Compact canonical turn envelope for the latest inbound message in this handler batch. */
152
+ turnEnvelope: CanonTurnEnvelopeV1;
151
153
  /**
152
154
  * Ask the conversation owner to approve a native runtime action. This only
153
155
  * renders Canon's inline approval card; runtimes must explicitly wait for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonmsg/agent-sdk",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Canon Agent SDK — build AI agents that participate in Canon conversations",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -28,7 +28,7 @@
28
28
  "node": ">=18.0.0"
29
29
  },
30
30
  "dependencies": {
31
- "@canonmsg/core": "^1.4.0"
31
+ "@canonmsg/core": "^1.7.0"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"