@canonmsg/core 0.12.0 → 0.14.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.
@@ -10,7 +10,12 @@ const POLL_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
10
10
  * 3. On approval, returns the API key
11
11
  */
12
12
  export async function registerAndWaitForApproval(input, callbacks) {
13
- const { requestId } = await CanonClient.register(input.baseUrl, {
13
+ const submitted = await submitRegistrationRequest(input);
14
+ callbacks?.onSubmitted?.(submitted.requestId, submitted.pollToken);
15
+ return waitForRegistrationApproval(input.baseUrl, submitted.requestId, submitted.pollToken, callbacks);
16
+ }
17
+ export async function submitRegistrationRequest(input) {
18
+ return CanonClient.register(input.baseUrl, {
14
19
  name: input.name,
15
20
  description: input.description,
16
21
  ownerPhone: input.ownerPhone,
@@ -18,29 +23,47 @@ export async function registerAndWaitForApproval(input, callbacks) {
18
23
  avatarUrl: input.avatarUrl,
19
24
  clientType: input.clientType,
20
25
  requestedAgentId: input.requestedAgentId,
26
+ localRegistrationId: input.localRegistrationId,
21
27
  });
22
- callbacks?.onSubmitted?.(requestId);
28
+ }
29
+ export async function waitForRegistrationApproval(baseUrl, requestId, pollToken, callbacks) {
23
30
  const deadline = Date.now() + POLL_TIMEOUT_MS;
24
31
  while (Date.now() < deadline) {
25
32
  await sleep(POLL_INTERVAL_MS);
26
- const result = await CanonClient.checkStatus(input.baseUrl, requestId);
33
+ const result = await CanonClient.checkStatus(baseUrl, requestId, pollToken);
27
34
  callbacks?.onPollUpdate?.(result);
28
35
  if (result.status === 'approved') {
36
+ if (!result.apiKey) {
37
+ return {
38
+ status: 'timeout',
39
+ requestId,
40
+ pollToken,
41
+ agentId: result.agentId,
42
+ agentName: result.agentName,
43
+ };
44
+ }
29
45
  return {
30
46
  status: 'approved',
31
47
  apiKey: result.apiKey,
32
48
  agentId: result.agentId,
33
49
  agentName: result.agentName,
50
+ requestId,
51
+ pollToken,
34
52
  };
35
53
  }
36
54
  if (result.status === 'rejected') {
37
55
  return {
38
56
  status: 'rejected',
39
57
  agentName: result.agentName,
58
+ requestId,
59
+ pollToken,
40
60
  };
41
61
  }
42
62
  }
43
- return { status: 'timeout' };
63
+ return { status: 'timeout', requestId, pollToken };
64
+ }
65
+ export async function ackRegistrationApproval(baseUrl, requestId, pollToken) {
66
+ await CanonClient.ackRegistrationStatus(baseUrl, requestId, pollToken);
44
67
  }
45
68
  function sleep(ms) {
46
69
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -114,7 +114,7 @@ interface RTDBAuthOptions {
114
114
  rtdbUrl?: string;
115
115
  firebaseApiKey?: string;
116
116
  }
117
- interface RTDBClientHandle {
117
+ export interface RTDBClientHandle {
118
118
  read(path: string): Promise<unknown>;
119
119
  write(path: string, data: unknown): Promise<void>;
120
120
  patch(path: string, data: unknown): Promise<void>;
@@ -0,0 +1,50 @@
1
+ import type { ExecutionEnvironmentMode } from './execution-environment-mode.js';
2
+ import { type AgentClientType, type CanonControlDescriptor, type CanonRuntimeActionDescriptor, type CanonRuntimeDescriptor, type CanonRuntimeStreamingMode, type CanonWorkspaceRootMetadata, type ModelOption, type PermissionModeOption, type WorkspaceOption } from './types.js';
3
+ import type { HostAdmissionActionCapabilities } from './turn-protocol.js';
4
+ export declare const CLAUDE_EFFORT_OPTIONS: readonly [{
5
+ readonly value: "low";
6
+ readonly label: "Low";
7
+ }, {
8
+ readonly value: "medium";
9
+ readonly label: "Medium";
10
+ }, {
11
+ readonly value: "high";
12
+ readonly label: "High";
13
+ }];
14
+ export declare const EXECUTION_MODE_CONTROL_OPTIONS: ReadonlyArray<{
15
+ value: ExecutionEnvironmentMode;
16
+ label: string;
17
+ description: string;
18
+ }>;
19
+ export declare function buildRuntimeWorkspaceControlOptions(workspaces: ReadonlyArray<WorkspaceOption>): ModelOption[];
20
+ export declare function buildRuntimeExecutionModeOptions(modes: ReadonlyArray<ExecutionEnvironmentMode>): ModelOption[];
21
+ export declare function buildRuntimeModelControl(input: {
22
+ models: ReadonlyArray<ModelOption>;
23
+ liveBehavior: 'immediate' | 'next_turn';
24
+ defaultValue?: string | null;
25
+ }): CanonControlDescriptor;
26
+ export declare function buildRuntimeWorkspaceControl(input: {
27
+ workspaces: ReadonlyArray<WorkspaceOption>;
28
+ workspaceRoots?: ReadonlyArray<CanonWorkspaceRootMetadata>;
29
+ label?: string;
30
+ defaultValue?: string | null;
31
+ }): CanonControlDescriptor;
32
+ export declare function buildRuntimeExecutionModeControl(executionModes: ReadonlyArray<ExecutionEnvironmentMode>): CanonControlDescriptor;
33
+ export declare function buildRuntimePermissionModeControl(input: {
34
+ clientType: 'claude-code' | 'codex';
35
+ options: ReadonlyArray<PermissionModeOption>;
36
+ defaultValue?: string | null;
37
+ }): CanonControlDescriptor;
38
+ export declare function buildRuntimeEffortControl(): CanonControlDescriptor;
39
+ export declare function buildFirstPartyCodingRuntimeDescriptor(input: {
40
+ clientType: Extract<AgentClientType, 'claude-code' | 'codex'>;
41
+ models: ReadonlyArray<ModelOption>;
42
+ workspaces: ReadonlyArray<WorkspaceOption>;
43
+ workspaceRoots?: ReadonlyArray<CanonWorkspaceRootMetadata>;
44
+ executionModes: ReadonlyArray<ExecutionEnvironmentMode>;
45
+ permissionModes?: ReadonlyArray<PermissionModeOption>;
46
+ defaultPermissionMode?: string | null;
47
+ actions?: ReadonlyArray<CanonRuntimeActionDescriptor>;
48
+ streamingTextMode: CanonRuntimeStreamingMode;
49
+ admissionActions?: HostAdmissionActionCapabilities;
50
+ }): CanonRuntimeDescriptor;
@@ -0,0 +1,128 @@
1
+ import { CLAUDE_PERMISSION_MODE_OPTIONS, } from './types.js';
2
+ export const CLAUDE_EFFORT_OPTIONS = [
3
+ { value: 'low', label: 'Low' },
4
+ { value: 'medium', label: 'Medium' },
5
+ { value: 'high', label: 'High' },
6
+ ];
7
+ export const EXECUTION_MODE_CONTROL_OPTIONS = [
8
+ {
9
+ value: 'worktree',
10
+ label: 'Isolated worktree',
11
+ description: 'Creates or reuses a per-conversation git worktree under ~/.canon/conversation-worktrees when the selected project is a git repo.',
12
+ },
13
+ {
14
+ value: 'locked',
15
+ label: 'Use shared project',
16
+ description: 'Runs directly in the selected project folder. Changes happen there.',
17
+ },
18
+ ];
19
+ export function buildRuntimeWorkspaceControlOptions(workspaces) {
20
+ return workspaces.map((workspace) => ({
21
+ value: workspace.id,
22
+ label: workspace.label,
23
+ ...(workspace.description ? { description: workspace.description } : {}),
24
+ ...(workspace.workspaceRootId ? { workspaceRootId: workspace.workspaceRootId } : {}),
25
+ ...(workspace.workspaceRelativePath ? { workspaceRelativePath: workspace.workspaceRelativePath } : {}),
26
+ ...(workspace.source ? { source: workspace.source } : {}),
27
+ }));
28
+ }
29
+ export function buildRuntimeExecutionModeOptions(modes) {
30
+ return modes.map((mode) => {
31
+ const option = EXECUTION_MODE_CONTROL_OPTIONS.find((candidate) => candidate.value === mode);
32
+ return option
33
+ ? { ...option }
34
+ : { value: mode, label: mode, description: mode };
35
+ });
36
+ }
37
+ export function buildRuntimeModelControl(input) {
38
+ return {
39
+ id: 'model',
40
+ label: 'Model',
41
+ options: input.models,
42
+ defaultValue: input.defaultValue ?? input.models[0]?.value ?? null,
43
+ availability: 'setup_and_live',
44
+ liveBehavior: input.liveBehavior,
45
+ selectionPolicy: 'inherit',
46
+ };
47
+ }
48
+ export function buildRuntimeWorkspaceControl(input) {
49
+ return {
50
+ id: 'workspace',
51
+ label: input.label ?? 'Project',
52
+ options: buildRuntimeWorkspaceControlOptions(input.workspaces),
53
+ defaultValue: input.defaultValue ?? input.workspaces[0]?.id ?? null,
54
+ availability: 'setup',
55
+ liveBehavior: 'none',
56
+ selectionPolicy: 'inherit',
57
+ description: input.workspaceRoots?.length
58
+ ? 'Choose one of the projects discovered inside the approved local roots for this host.'
59
+ : 'Choose one of the local projects advertised by this host.',
60
+ };
61
+ }
62
+ export function buildRuntimeExecutionModeControl(executionModes) {
63
+ return {
64
+ id: 'executionMode',
65
+ label: 'Execution mode',
66
+ options: buildRuntimeExecutionModeOptions(executionModes),
67
+ defaultValue: null,
68
+ availability: 'setup',
69
+ liveBehavior: 'none',
70
+ selectionPolicy: 'required_explicit',
71
+ };
72
+ }
73
+ export function buildRuntimePermissionModeControl(input) {
74
+ const isCodex = input.clientType === 'codex';
75
+ return {
76
+ id: 'permissionMode',
77
+ label: isCodex ? 'Execution policy' : 'Permission mode',
78
+ options: input.options,
79
+ defaultValue: input.defaultValue ?? null,
80
+ availability: isCodex ? 'setup' : 'setup_and_live',
81
+ liveBehavior: isCodex ? 'none' : 'immediate',
82
+ selectionPolicy: 'inherit',
83
+ };
84
+ }
85
+ export function buildRuntimeEffortControl() {
86
+ return {
87
+ id: 'effort',
88
+ label: 'Thinking level',
89
+ options: [...CLAUDE_EFFORT_OPTIONS],
90
+ defaultValue: 'medium',
91
+ availability: 'setup_and_live',
92
+ liveBehavior: 'immediate',
93
+ selectionPolicy: 'inherit',
94
+ };
95
+ }
96
+ export function buildFirstPartyCodingRuntimeDescriptor(input) {
97
+ const permissionModes = input.permissionModes
98
+ ?? (input.clientType === 'claude-code' ? CLAUDE_PERMISSION_MODE_OPTIONS : []);
99
+ return {
100
+ coreControls: [
101
+ buildRuntimeModelControl({
102
+ models: input.models,
103
+ liveBehavior: input.clientType === 'codex' ? 'next_turn' : 'immediate',
104
+ }),
105
+ buildRuntimeWorkspaceControl({
106
+ workspaces: input.workspaces,
107
+ workspaceRoots: input.workspaceRoots,
108
+ }),
109
+ buildRuntimeExecutionModeControl(input.executionModes),
110
+ ],
111
+ runtimeControls: [
112
+ ...(permissionModes.length > 0
113
+ ? [buildRuntimePermissionModeControl({
114
+ clientType: input.clientType,
115
+ options: permissionModes,
116
+ defaultValue: input.defaultPermissionMode
117
+ ?? (input.clientType === 'claude-code' ? 'default' : null),
118
+ })]
119
+ : []),
120
+ ...(input.clientType === 'claude-code' ? [buildRuntimeEffortControl()] : []),
121
+ ],
122
+ actions: input.actions,
123
+ workspaceRoots: input.workspaceRoots,
124
+ supportsInterrupt: true,
125
+ streamingTextMode: input.streamingTextMode,
126
+ admissionActions: input.admissionActions,
127
+ };
128
+ }
@@ -0,0 +1,31 @@
1
+ import type { AgentClientType, AgentRuntime, RuntimeInfoPayload } from './types.js';
2
+ import { type AgentSessionSnapshotPatch, type RTDBClientHandle, type SessionStatePayload, type TurnStatePayload } from './rtdb-rest.js';
3
+ export interface RuntimeStatePublisherOptions {
4
+ agentId: string;
5
+ clientType: AgentClientType;
6
+ hostMode: boolean;
7
+ rtdb?: RTDBClientHandle;
8
+ }
9
+ export interface RuntimeStreamingPayload {
10
+ text: string;
11
+ status: 'thinking' | 'streaming' | 'tool';
12
+ messageId?: string;
13
+ updatedAt?: number | {
14
+ '.sv': 'timestamp';
15
+ };
16
+ }
17
+ export interface RuntimeStatePublisher {
18
+ publishAgentRuntime(runtime: AgentRuntime): Promise<void>;
19
+ clearAgentRuntime(): Promise<void>;
20
+ writeSessionState(conversationId: string, state: Omit<SessionStatePayload, 'updatedAt'>): Promise<void>;
21
+ clearSessionState(conversationId: string): Promise<void>;
22
+ writeTurnState(conversationId: string, state: Omit<TurnStatePayload, 'updatedAt'>): Promise<void>;
23
+ clearTurnState(conversationId: string): Promise<void>;
24
+ patchAgentSessionSnapshot(conversationId: string, snapshot: Omit<AgentSessionSnapshotPatch, 'updatedAt'>): Promise<void>;
25
+ writeRuntimeInfo(conversationId: string, payload: Omit<RuntimeInfoPayload, 'updatedAt'>): Promise<void>;
26
+ patchRuntimeInfo(conversationId: string, payload: Partial<Omit<RuntimeInfoPayload, 'updatedAt'>>): Promise<void>;
27
+ clearRuntimeInfo(conversationId: string): Promise<void>;
28
+ writeStreaming(conversationId: string, payload: RuntimeStreamingPayload): Promise<void>;
29
+ clearStreaming(conversationId: string): Promise<void>;
30
+ }
31
+ export declare function createRuntimeStatePublisher(options: RuntimeStatePublisherOptions): RuntimeStatePublisher;
@@ -0,0 +1,68 @@
1
+ import { clearRuntimeInfo, clearSessionState, clearTurnState, patchAgentSessionSnapshot, patchRuntimeInfo, rtdbWrite, writeRuntimeInfo, writeSessionState, writeTurnState, } from './rtdb-rest.js';
2
+ const SERVER_TIMESTAMP = { '.sv': 'timestamp' };
3
+ export function createRuntimeStatePublisher(options) {
4
+ const { agentId, clientType, hostMode, rtdb } = options;
5
+ const writePath = (path, data) => (rtdb ? rtdb.write(path, data) : rtdbWrite(path, data));
6
+ return {
7
+ async publishAgentRuntime(runtime) {
8
+ await writePath(`/agent-runtime/${agentId}`, {
9
+ clientType,
10
+ hostMode,
11
+ ...runtime,
12
+ updatedAt: SERVER_TIMESTAMP,
13
+ });
14
+ },
15
+ async clearAgentRuntime() {
16
+ await writePath(`/agent-runtime/${agentId}`, null);
17
+ },
18
+ async writeSessionState(conversationId, state) {
19
+ await (rtdb
20
+ ? rtdb.writeSessionState(conversationId, agentId, state)
21
+ : writeSessionState(conversationId, agentId, state));
22
+ },
23
+ async clearSessionState(conversationId) {
24
+ await (rtdb
25
+ ? rtdb.clearSessionState(conversationId, agentId)
26
+ : clearSessionState(conversationId, agentId));
27
+ },
28
+ async writeTurnState(conversationId, state) {
29
+ await (rtdb
30
+ ? rtdb.writeTurnState(conversationId, agentId, state)
31
+ : writeTurnState(conversationId, agentId, state));
32
+ },
33
+ async clearTurnState(conversationId) {
34
+ await (rtdb
35
+ ? rtdb.clearTurnState(conversationId, agentId)
36
+ : clearTurnState(conversationId, agentId));
37
+ },
38
+ async patchAgentSessionSnapshot(conversationId, snapshot) {
39
+ await (rtdb
40
+ ? rtdb.patchAgentSessionSnapshot(conversationId, agentId, snapshot)
41
+ : patchAgentSessionSnapshot(conversationId, agentId, snapshot));
42
+ },
43
+ async writeRuntimeInfo(conversationId, payload) {
44
+ await (rtdb
45
+ ? rtdb.writeRuntimeInfo(conversationId, agentId, payload)
46
+ : writeRuntimeInfo(conversationId, agentId, payload));
47
+ },
48
+ async patchRuntimeInfo(conversationId, payload) {
49
+ await (rtdb
50
+ ? rtdb.patchRuntimeInfo(conversationId, agentId, payload)
51
+ : patchRuntimeInfo(conversationId, agentId, payload));
52
+ },
53
+ async clearRuntimeInfo(conversationId) {
54
+ await (rtdb
55
+ ? rtdb.clearRuntimeInfo(conversationId, agentId)
56
+ : clearRuntimeInfo(conversationId, agentId));
57
+ },
58
+ async writeStreaming(conversationId, payload) {
59
+ await writePath(`/streaming/${conversationId}/${agentId}`, {
60
+ ...payload,
61
+ updatedAt: payload.updatedAt ?? SERVER_TIMESTAMP,
62
+ });
63
+ },
64
+ async clearStreaming(conversationId) {
65
+ await writePath(`/streaming/${conversationId}/${agentId}`, null);
66
+ },
67
+ };
68
+ }
package/dist/types.d.ts CHANGED
@@ -605,15 +605,18 @@ export interface RegistrationInput {
605
605
  baseUrl?: string;
606
606
  clientType?: AgentClientType;
607
607
  requestedAgentId?: string;
608
+ localRegistrationId?: string;
608
609
  }
609
610
  export interface RegistrationResult {
610
611
  status: 'approved' | 'rejected' | 'timeout';
611
612
  apiKey?: string;
612
613
  agentId?: string;
613
614
  agentName?: string;
615
+ requestId?: string;
616
+ pollToken?: string;
614
617
  }
615
618
  export interface RegistrationStatus {
616
- status: 'pending' | 'approved' | 'rejected';
619
+ status: 'pending' | 'approving' | 'approved' | 'rejected';
617
620
  agentName: string;
618
621
  agentId?: string;
619
622
  apiKey?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonmsg/core",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Canon core — shared types, REST client, SSE stream, and registration for Canon messaging",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",