@canonmsg/backend-contracts 8.3.0 → 8.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/dist/cjs/index.js CHANGED
@@ -36,3 +36,4 @@ __exportStar(require("./moderation.js"), exports);
36
36
  __exportStar(require("./selfContext.js"), exports);
37
37
  __exportStar(require("./replyAuthority.js"), exports);
38
38
  __exportStar(require("./runtimeDescriptor.js"), exports);
39
+ __exportStar(require("./workSessions.js"), exports);
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WORK_SESSION_REQUEST_TTL_MS = exports.WORK_SESSION_LEASE_MS = exports.WORK_SESSION_SCHEMA = void 0;
4
+ exports.workSessionId = workSessionId;
5
+ exports.parseWorkSessionSettings = parseWorkSessionSettings;
6
+ exports.parseWorkSessionCatalog = parseWorkSessionCatalog;
7
+ exports.parseWorkSessionSelection = parseWorkSessionSelection;
8
+ exports.assertWorkSessionSelectionAvailable = assertWorkSessionSelectionAvailable;
9
+ exports.parseWorkSessionRuntimeRegistration = parseWorkSessionRuntimeRegistration;
10
+ exports.parseWorkSessionRuntimeAuth = parseWorkSessionRuntimeAuth;
11
+ exports.parseRequestWorkSessionInput = parseRequestWorkSessionInput;
12
+ exports.parseWorkSessionCompletion = parseWorkSessionCompletion;
13
+ /** Private owner/operator workflow. None of this catalog belongs in public runtime descriptors. */
14
+ exports.WORK_SESSION_SCHEMA = 'canon.work-sessions.v1';
15
+ exports.WORK_SESSION_LEASE_MS = 90_000;
16
+ exports.WORK_SESSION_REQUEST_TTL_MS = 5 * 60_000;
17
+ const SETTINGS_KEYS = ['projectId', 'modelId', 'reasoningEffort', 'permissionMode', 'executionMode'];
18
+ function object(value, keys, label) {
19
+ if (!value || typeof value !== 'object' || Array.isArray(value))
20
+ throw new Error(`Invalid ${label}`);
21
+ const result = value;
22
+ if (Object.keys(result).some((key) => !keys.includes(key)))
23
+ throw new Error(`Unsupported ${label} field`);
24
+ return result;
25
+ }
26
+ function workSessionId(value, label = 'identifier') {
27
+ if (typeof value !== 'string' || !/^[A-Za-z0-9_.:-]{1,160}$/.test(value))
28
+ throw new Error(`Invalid ${label}`);
29
+ return value;
30
+ }
31
+ function label(value, name, max = 160) {
32
+ if (typeof value !== 'string' || !value.trim() || value.length > max)
33
+ throw new Error(`Invalid ${name}`);
34
+ return value.trim();
35
+ }
36
+ function parseWorkSessionSettings(value) {
37
+ const input = object(value, SETTINGS_KEYS, 'settings');
38
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [key, workSessionId(value, key)]));
39
+ }
40
+ function choices(value, models = false) {
41
+ if (!Array.isArray(value) || value.length > 128)
42
+ throw new Error('Invalid choices');
43
+ const ids = new Set();
44
+ return value.map((entry) => {
45
+ const input = object(entry, ['id', 'label', 'description', ...(models ? ['reasoningEfforts', 'defaultReasoningEffort'] : [])], 'choice');
46
+ const id = workSessionId(input.id);
47
+ if (ids.has(id))
48
+ throw new Error('Duplicate choice identifier');
49
+ ids.add(id);
50
+ const result = { id, label: label(input.label, 'choice label') };
51
+ if (input.description !== undefined)
52
+ result.description = label(input.description, 'choice description', 512);
53
+ if (input.reasoningEfforts !== undefined)
54
+ result.reasoningEfforts = choices(input.reasoningEfforts);
55
+ if (input.defaultReasoningEffort !== undefined) {
56
+ result.defaultReasoningEffort = workSessionId(input.defaultReasoningEffort);
57
+ if (!result.reasoningEfforts?.some((choice) => choice.id === result.defaultReasoningEffort))
58
+ throw new Error('Invalid default reasoning effort');
59
+ }
60
+ return result;
61
+ });
62
+ }
63
+ function parseWorkSessionCatalog(value) {
64
+ const input = object(value, ['revision', 'provider', 'canCreate', 'canAttach', 'projects', 'models', 'reasoningEfforts', 'permissionModes', 'executionModes', 'defaults', 'sessions'], 'catalog');
65
+ if (typeof input.canCreate !== 'boolean' || typeof input.canAttach !== 'boolean')
66
+ throw new Error('Invalid catalog capabilities');
67
+ if (!Array.isArray(input.sessions) || input.sessions.length > 128)
68
+ throw new Error('Invalid loaded sessions');
69
+ const ids = new Set();
70
+ const result = {
71
+ revision: workSessionId(input.revision, 'catalog revision'), provider: workSessionId(input.provider, 'provider'),
72
+ canCreate: input.canCreate, canAttach: input.canAttach, projects: choices(input.projects),
73
+ sessions: input.sessions.map((entry) => {
74
+ const session = object(entry, ['id', 'title', 'status', 'settings', 'projectLabel', 'modelLabel'], 'loaded session');
75
+ const id = workSessionId(session.id, 'session identifier');
76
+ if (ids.has(id))
77
+ throw new Error('Duplicate loaded session');
78
+ ids.add(id);
79
+ if (session.status !== 'idle' && session.status !== 'running')
80
+ throw new Error('Invalid session status');
81
+ return {
82
+ id, title: label(session.title, 'session title'), status: session.status,
83
+ settings: parseWorkSessionSettings(session.settings),
84
+ ...(session.projectLabel !== undefined ? { projectLabel: label(session.projectLabel, 'project label') } : {}),
85
+ ...(session.modelLabel !== undefined ? { modelLabel: label(session.modelLabel, 'model label') } : {}),
86
+ };
87
+ }),
88
+ };
89
+ for (const key of ['models', 'reasoningEfforts', 'permissionModes', 'executionModes']) {
90
+ if (input[key] !== undefined)
91
+ result[key] = choices(input[key], key === 'models');
92
+ }
93
+ if (input.defaults !== undefined)
94
+ result.defaults = parseWorkSessionSettings(input.defaults);
95
+ if (result.canCreate && !result.projects.length)
96
+ throw new Error('Creating sessions requires an advertised project');
97
+ if (JSON.stringify(result).length > 65_536)
98
+ throw new Error('Work session catalog is too large');
99
+ return result;
100
+ }
101
+ function parseWorkSessionSelection(value) {
102
+ const mode = value?.mode;
103
+ if (mode === 'attach') {
104
+ const input = object(value, ['mode', 'sessionId'], 'attach selection');
105
+ return { mode, sessionId: workSessionId(input.sessionId, 'session identifier') };
106
+ }
107
+ if (mode === 'create') {
108
+ const { mode: _mode, ...settings } = object(value, ['mode', ...SETTINGS_KEYS], 'create selection');
109
+ return { mode, ...parseWorkSessionSettings(settings), projectId: workSessionId(settings.projectId, 'project identifier') };
110
+ }
111
+ throw new Error('Invalid work session selection');
112
+ }
113
+ function assertWorkSessionSelectionAvailable(selection, catalog) {
114
+ if (selection.mode === 'attach') {
115
+ if (!catalog.canAttach || !catalog.sessions.some((session) => session.id === selection.sessionId))
116
+ throw new Error('Selected loaded session is unavailable');
117
+ return;
118
+ }
119
+ if (!catalog.canCreate || !catalog.projects.some((project) => project.id === selection.projectId))
120
+ throw new Error('Selected project is unavailable');
121
+ const modelId = selection.modelId ?? catalog.defaults?.modelId;
122
+ const model = catalog.models?.find((choice) => choice.id === modelId);
123
+ const available = {
124
+ modelId: catalog.models, reasoningEffort: model?.reasoningEfforts ?? catalog.reasoningEfforts,
125
+ permissionMode: catalog.permissionModes, executionMode: catalog.executionModes,
126
+ };
127
+ for (const key of ['modelId', 'reasoningEffort', 'permissionMode', 'executionMode']) {
128
+ if (selection[key] !== undefined && !available[key]?.some((choice) => choice.id === selection[key]))
129
+ throw new Error(`Selected ${key} is unavailable`);
130
+ }
131
+ }
132
+ function parseWorkSessionRuntimeRegistration(value) {
133
+ const input = object(value, ['hostId', 'runtimeEpoch', 'displayName', 'catalog'], 'runtime registration');
134
+ return { hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), displayName: label(input.displayName, 'host name'), catalog: parseWorkSessionCatalog(input.catalog) };
135
+ }
136
+ function parseWorkSessionRuntimeAuth(value, extraKeys = []) {
137
+ const input = object(value, ['hostId', 'runtimeEpoch', 'leaseToken', ...extraKeys], 'runtime request');
138
+ return { hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), leaseToken: workSessionId(input.leaseToken, 'lease token') };
139
+ }
140
+ function parseRequestWorkSessionInput(value) {
141
+ const input = object(value, ['agentId', 'conversationId', 'requestId', 'hostId', 'runtimeEpoch', 'catalogRevision', 'selection'], 'work session request');
142
+ if (typeof input.requestId !== 'string' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input.requestId))
143
+ throw new Error('requestId must be a UUID');
144
+ return {
145
+ agentId: workSessionId(input.agentId), conversationId: workSessionId(input.conversationId), requestId: input.requestId,
146
+ hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), catalogRevision: workSessionId(input.catalogRevision),
147
+ selection: parseWorkSessionSelection(input.selection),
148
+ };
149
+ }
150
+ function parseWorkSessionCompletion(value) {
151
+ const status = value?.status;
152
+ if (status === 'attached') {
153
+ const input = object(value, ['status', 'nativeSessionId', 'settings'], 'attached result');
154
+ return { status, nativeSessionId: workSessionId(input.nativeSessionId), settings: parseWorkSessionSettings(input.settings) };
155
+ }
156
+ if (status === 'failed' || status === 'uncertain') {
157
+ const input = object(value, ['status', 'error'], 'failure result');
158
+ const error = object(input.error, ['code', 'message'], 'failure');
159
+ return { status, error: { code: workSessionId(error.code), message: label(error.message, 'failure message', 512) } };
160
+ }
161
+ throw new Error('Invalid work session result');
162
+ }
package/dist/index.d.ts CHANGED
@@ -20,3 +20,4 @@ export * from './moderation.js';
20
20
  export * from './selfContext.js';
21
21
  export * from './replyAuthority.js';
22
22
  export * from './runtimeDescriptor.js';
23
+ export * from './workSessions.js';
package/dist/index.js CHANGED
@@ -20,3 +20,4 @@ export * from './moderation.js';
20
20
  export * from './selfContext.js';
21
21
  export * from './replyAuthority.js';
22
22
  export * from './runtimeDescriptor.js';
23
+ export * from './workSessions.js';
@@ -0,0 +1,156 @@
1
+ /** Private owner/operator workflow. None of this catalog belongs in public runtime descriptors. */
2
+ export declare const WORK_SESSION_SCHEMA: "canon.work-sessions.v1";
3
+ export declare const WORK_SESSION_LEASE_MS = 90000;
4
+ export declare const WORK_SESSION_REQUEST_TTL_MS: number;
5
+ export interface WorkSessionChoice {
6
+ id: string;
7
+ label: string;
8
+ description?: string;
9
+ }
10
+ export interface WorkSessionModelChoice extends WorkSessionChoice {
11
+ reasoningEfforts?: WorkSessionChoice[];
12
+ defaultReasoningEffort?: string;
13
+ }
14
+ export interface WorkSessionSettings {
15
+ projectId?: string;
16
+ modelId?: string;
17
+ reasoningEffort?: string;
18
+ permissionMode?: string;
19
+ executionMode?: string;
20
+ }
21
+ export type WorkSessionSelection = ({
22
+ mode: 'create';
23
+ projectId: string;
24
+ } & Omit<WorkSessionSettings, 'projectId'>) | {
25
+ mode: 'attach';
26
+ sessionId: string;
27
+ };
28
+ export interface WorkSessionLoadedSession {
29
+ id: string;
30
+ title: string;
31
+ status: 'idle' | 'running';
32
+ settings: WorkSessionSettings;
33
+ projectLabel?: string;
34
+ modelLabel?: string;
35
+ }
36
+ export interface WorkSessionCatalog {
37
+ revision: string;
38
+ provider: string;
39
+ canCreate: boolean;
40
+ canAttach: boolean;
41
+ projects: WorkSessionChoice[];
42
+ models?: WorkSessionModelChoice[];
43
+ reasoningEfforts?: WorkSessionChoice[];
44
+ permissionModes?: WorkSessionChoice[];
45
+ executionModes?: WorkSessionChoice[];
46
+ defaults?: WorkSessionSettings;
47
+ sessions: WorkSessionLoadedSession[];
48
+ }
49
+ export interface WorkSessionRuntimeIdentity {
50
+ hostId: string;
51
+ runtimeEpoch: string;
52
+ }
53
+ export interface WorkSessionRuntimeLease extends WorkSessionRuntimeIdentity {
54
+ /** Agent-only fencing credential. Never expose in the owner catalog or request status. */
55
+ leaseToken: string;
56
+ expiresAt: number;
57
+ }
58
+ export interface WorkSessionRuntimeRegistration extends WorkSessionRuntimeIdentity {
59
+ displayName: string;
60
+ catalog: WorkSessionCatalog;
61
+ }
62
+ export interface WorkSessionBinding {
63
+ workSessionId: string;
64
+ conversationId: string;
65
+ hostId: string;
66
+ provider: string;
67
+ nativeSessionId: string;
68
+ settings: WorkSessionSettings;
69
+ createdAt: number;
70
+ }
71
+ export interface WorkSessionRuntimeState {
72
+ ownerId: string;
73
+ lease: WorkSessionRuntimeLease;
74
+ bindings: WorkSessionBinding[];
75
+ }
76
+ export interface WorkSessionCatalogResult {
77
+ status: 'available' | 'offline' | 'unavailable';
78
+ agentId: string;
79
+ hostId?: string;
80
+ runtimeEpoch?: string;
81
+ displayName?: string;
82
+ expiresAt?: number;
83
+ catalog?: WorkSessionCatalog;
84
+ bindings: WorkSessionBinding[];
85
+ }
86
+ export interface RequestWorkSessionInput extends WorkSessionRuntimeIdentity {
87
+ agentId: string;
88
+ conversationId: string;
89
+ requestId: string;
90
+ catalogRevision: string;
91
+ selection: WorkSessionSelection;
92
+ }
93
+ export type WorkSessionRequestStatus = 'pending' | 'claimed' | 'attached' | 'failed' | 'uncertain' | 'expired';
94
+ export interface WorkSessionRequest extends RequestWorkSessionInput {
95
+ schema: typeof WORK_SESSION_SCHEMA;
96
+ requestedBy: string;
97
+ status: WorkSessionRequestStatus;
98
+ createdAt: number;
99
+ updatedAt: number;
100
+ expiresAt: number;
101
+ binding?: WorkSessionBinding;
102
+ error?: {
103
+ code: string;
104
+ message: string;
105
+ };
106
+ }
107
+ export type WorkSessionCompletion = {
108
+ status: 'attached';
109
+ nativeSessionId: string;
110
+ settings: WorkSessionSettings;
111
+ } | {
112
+ status: 'failed' | 'uncertain';
113
+ error: {
114
+ code: string;
115
+ message: string;
116
+ };
117
+ };
118
+ export type WorkSessionRuntimeAuth = Pick<WorkSessionRuntimeLease, 'hostId' | 'runtimeEpoch' | 'leaseToken'>;
119
+ export interface WorkSessionClaimInput extends WorkSessionRuntimeAuth {
120
+ requestId?: string;
121
+ }
122
+ export interface WorkSessionClaimResult {
123
+ request: WorkSessionRequest | null;
124
+ /** Previously claimed: reconcile the durable host journal, never blindly execute again. */
125
+ replayed: boolean;
126
+ }
127
+ export interface CompleteWorkSessionInput extends WorkSessionRuntimeAuth {
128
+ requestId: string;
129
+ result: WorkSessionCompletion;
130
+ }
131
+ export interface WorkSessionHeartbeatInput extends WorkSessionRuntimeAuth {
132
+ catalog?: WorkSessionCatalog;
133
+ }
134
+ export interface ReleaseWorkSessionBindingInput extends WorkSessionRuntimeAuth {
135
+ conversationId: string;
136
+ workSessionId: string;
137
+ }
138
+ export interface ResolveWorkSessionRequestInput {
139
+ agentId: string;
140
+ requestId: string;
141
+ }
142
+ export interface GetWorkSessionRequestInput {
143
+ agentId: string;
144
+ /** Exactly one selector. Conversation lookup recovers the latest operation after reload. */
145
+ requestId?: string;
146
+ conversationId?: string;
147
+ }
148
+ export declare function workSessionId(value: unknown, label?: string): string;
149
+ export declare function parseWorkSessionSettings(value: unknown): WorkSessionSettings;
150
+ export declare function parseWorkSessionCatalog(value: unknown): WorkSessionCatalog;
151
+ export declare function parseWorkSessionSelection(value: unknown): WorkSessionSelection;
152
+ export declare function assertWorkSessionSelectionAvailable(selection: WorkSessionSelection, catalog: WorkSessionCatalog): void;
153
+ export declare function parseWorkSessionRuntimeRegistration(value: unknown): WorkSessionRuntimeRegistration;
154
+ export declare function parseWorkSessionRuntimeAuth(value: unknown, extraKeys?: string[]): WorkSessionRuntimeAuth;
155
+ export declare function parseRequestWorkSessionInput(value: unknown): RequestWorkSessionInput;
156
+ export declare function parseWorkSessionCompletion(value: unknown): WorkSessionCompletion;
@@ -0,0 +1,150 @@
1
+ /** Private owner/operator workflow. None of this catalog belongs in public runtime descriptors. */
2
+ export const WORK_SESSION_SCHEMA = 'canon.work-sessions.v1';
3
+ export const WORK_SESSION_LEASE_MS = 90_000;
4
+ export const WORK_SESSION_REQUEST_TTL_MS = 5 * 60_000;
5
+ const SETTINGS_KEYS = ['projectId', 'modelId', 'reasoningEffort', 'permissionMode', 'executionMode'];
6
+ function object(value, keys, label) {
7
+ if (!value || typeof value !== 'object' || Array.isArray(value))
8
+ throw new Error(`Invalid ${label}`);
9
+ const result = value;
10
+ if (Object.keys(result).some((key) => !keys.includes(key)))
11
+ throw new Error(`Unsupported ${label} field`);
12
+ return result;
13
+ }
14
+ export function workSessionId(value, label = 'identifier') {
15
+ if (typeof value !== 'string' || !/^[A-Za-z0-9_.:-]{1,160}$/.test(value))
16
+ throw new Error(`Invalid ${label}`);
17
+ return value;
18
+ }
19
+ function label(value, name, max = 160) {
20
+ if (typeof value !== 'string' || !value.trim() || value.length > max)
21
+ throw new Error(`Invalid ${name}`);
22
+ return value.trim();
23
+ }
24
+ export function parseWorkSessionSettings(value) {
25
+ const input = object(value, SETTINGS_KEYS, 'settings');
26
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [key, workSessionId(value, key)]));
27
+ }
28
+ function choices(value, models = false) {
29
+ if (!Array.isArray(value) || value.length > 128)
30
+ throw new Error('Invalid choices');
31
+ const ids = new Set();
32
+ return value.map((entry) => {
33
+ const input = object(entry, ['id', 'label', 'description', ...(models ? ['reasoningEfforts', 'defaultReasoningEffort'] : [])], 'choice');
34
+ const id = workSessionId(input.id);
35
+ if (ids.has(id))
36
+ throw new Error('Duplicate choice identifier');
37
+ ids.add(id);
38
+ const result = { id, label: label(input.label, 'choice label') };
39
+ if (input.description !== undefined)
40
+ result.description = label(input.description, 'choice description', 512);
41
+ if (input.reasoningEfforts !== undefined)
42
+ result.reasoningEfforts = choices(input.reasoningEfforts);
43
+ if (input.defaultReasoningEffort !== undefined) {
44
+ result.defaultReasoningEffort = workSessionId(input.defaultReasoningEffort);
45
+ if (!result.reasoningEfforts?.some((choice) => choice.id === result.defaultReasoningEffort))
46
+ throw new Error('Invalid default reasoning effort');
47
+ }
48
+ return result;
49
+ });
50
+ }
51
+ export function parseWorkSessionCatalog(value) {
52
+ const input = object(value, ['revision', 'provider', 'canCreate', 'canAttach', 'projects', 'models', 'reasoningEfforts', 'permissionModes', 'executionModes', 'defaults', 'sessions'], 'catalog');
53
+ if (typeof input.canCreate !== 'boolean' || typeof input.canAttach !== 'boolean')
54
+ throw new Error('Invalid catalog capabilities');
55
+ if (!Array.isArray(input.sessions) || input.sessions.length > 128)
56
+ throw new Error('Invalid loaded sessions');
57
+ const ids = new Set();
58
+ const result = {
59
+ revision: workSessionId(input.revision, 'catalog revision'), provider: workSessionId(input.provider, 'provider'),
60
+ canCreate: input.canCreate, canAttach: input.canAttach, projects: choices(input.projects),
61
+ sessions: input.sessions.map((entry) => {
62
+ const session = object(entry, ['id', 'title', 'status', 'settings', 'projectLabel', 'modelLabel'], 'loaded session');
63
+ const id = workSessionId(session.id, 'session identifier');
64
+ if (ids.has(id))
65
+ throw new Error('Duplicate loaded session');
66
+ ids.add(id);
67
+ if (session.status !== 'idle' && session.status !== 'running')
68
+ throw new Error('Invalid session status');
69
+ return {
70
+ id, title: label(session.title, 'session title'), status: session.status,
71
+ settings: parseWorkSessionSettings(session.settings),
72
+ ...(session.projectLabel !== undefined ? { projectLabel: label(session.projectLabel, 'project label') } : {}),
73
+ ...(session.modelLabel !== undefined ? { modelLabel: label(session.modelLabel, 'model label') } : {}),
74
+ };
75
+ }),
76
+ };
77
+ for (const key of ['models', 'reasoningEfforts', 'permissionModes', 'executionModes']) {
78
+ if (input[key] !== undefined)
79
+ result[key] = choices(input[key], key === 'models');
80
+ }
81
+ if (input.defaults !== undefined)
82
+ result.defaults = parseWorkSessionSettings(input.defaults);
83
+ if (result.canCreate && !result.projects.length)
84
+ throw new Error('Creating sessions requires an advertised project');
85
+ if (JSON.stringify(result).length > 65_536)
86
+ throw new Error('Work session catalog is too large');
87
+ return result;
88
+ }
89
+ export function parseWorkSessionSelection(value) {
90
+ const mode = value?.mode;
91
+ if (mode === 'attach') {
92
+ const input = object(value, ['mode', 'sessionId'], 'attach selection');
93
+ return { mode, sessionId: workSessionId(input.sessionId, 'session identifier') };
94
+ }
95
+ if (mode === 'create') {
96
+ const { mode: _mode, ...settings } = object(value, ['mode', ...SETTINGS_KEYS], 'create selection');
97
+ return { mode, ...parseWorkSessionSettings(settings), projectId: workSessionId(settings.projectId, 'project identifier') };
98
+ }
99
+ throw new Error('Invalid work session selection');
100
+ }
101
+ export function assertWorkSessionSelectionAvailable(selection, catalog) {
102
+ if (selection.mode === 'attach') {
103
+ if (!catalog.canAttach || !catalog.sessions.some((session) => session.id === selection.sessionId))
104
+ throw new Error('Selected loaded session is unavailable');
105
+ return;
106
+ }
107
+ if (!catalog.canCreate || !catalog.projects.some((project) => project.id === selection.projectId))
108
+ throw new Error('Selected project is unavailable');
109
+ const modelId = selection.modelId ?? catalog.defaults?.modelId;
110
+ const model = catalog.models?.find((choice) => choice.id === modelId);
111
+ const available = {
112
+ modelId: catalog.models, reasoningEffort: model?.reasoningEfforts ?? catalog.reasoningEfforts,
113
+ permissionMode: catalog.permissionModes, executionMode: catalog.executionModes,
114
+ };
115
+ for (const key of ['modelId', 'reasoningEffort', 'permissionMode', 'executionMode']) {
116
+ if (selection[key] !== undefined && !available[key]?.some((choice) => choice.id === selection[key]))
117
+ throw new Error(`Selected ${key} is unavailable`);
118
+ }
119
+ }
120
+ export function parseWorkSessionRuntimeRegistration(value) {
121
+ const input = object(value, ['hostId', 'runtimeEpoch', 'displayName', 'catalog'], 'runtime registration');
122
+ return { hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), displayName: label(input.displayName, 'host name'), catalog: parseWorkSessionCatalog(input.catalog) };
123
+ }
124
+ export function parseWorkSessionRuntimeAuth(value, extraKeys = []) {
125
+ const input = object(value, ['hostId', 'runtimeEpoch', 'leaseToken', ...extraKeys], 'runtime request');
126
+ return { hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), leaseToken: workSessionId(input.leaseToken, 'lease token') };
127
+ }
128
+ export function parseRequestWorkSessionInput(value) {
129
+ const input = object(value, ['agentId', 'conversationId', 'requestId', 'hostId', 'runtimeEpoch', 'catalogRevision', 'selection'], 'work session request');
130
+ if (typeof input.requestId !== 'string' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input.requestId))
131
+ throw new Error('requestId must be a UUID');
132
+ return {
133
+ agentId: workSessionId(input.agentId), conversationId: workSessionId(input.conversationId), requestId: input.requestId,
134
+ hostId: workSessionId(input.hostId), runtimeEpoch: workSessionId(input.runtimeEpoch), catalogRevision: workSessionId(input.catalogRevision),
135
+ selection: parseWorkSessionSelection(input.selection),
136
+ };
137
+ }
138
+ export function parseWorkSessionCompletion(value) {
139
+ const status = value?.status;
140
+ if (status === 'attached') {
141
+ const input = object(value, ['status', 'nativeSessionId', 'settings'], 'attached result');
142
+ return { status, nativeSessionId: workSessionId(input.nativeSessionId), settings: parseWorkSessionSettings(input.settings) };
143
+ }
144
+ if (status === 'failed' || status === 'uncertain') {
145
+ const input = object(value, ['status', 'error'], 'failure result');
146
+ const error = object(input.error, ['code', 'message'], 'failure');
147
+ return { status, error: { code: workSessionId(error.code), message: label(error.message, 'failure message', 512) } };
148
+ }
149
+ throw new Error('Invalid work session result');
150
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonmsg/backend-contracts",
3
- "version": "8.3.0",
3
+ "version": "8.4.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",
@@ -33,21 +33,19 @@
33
33
  "contracts",
34
34
  "wire"
35
35
  ],
36
- "repository": {
37
- "type": "git",
38
- "url": "https://github.com/HeyBobChan/canon",
39
- "directory": "packages/backend-contracts"
40
- },
41
- "homepage": "https://github.com/HeyBobChan/canon/tree/main/packages/backend-contracts",
36
+ "homepage": "https://canonmail.com/agents/contracts",
42
37
  "publishConfig": {
43
38
  "access": "public"
44
39
  },
45
40
  "devDependencies": {
46
- "@canonmsg/rich-cards": "^0.10.3",
41
+ "@canonmsg/rich-cards": "^0.10.5",
47
42
  "@types/node": "^22.0.0",
48
43
  "ajv": "^8.20.0",
49
44
  "typescript": "~5.7.0",
50
45
  "vitest": "^4.1.8"
51
46
  },
52
- "license": "MIT"
47
+ "license": "MIT",
48
+ "bugs": {
49
+ "url": "https://canonmail.com/support"
50
+ }
53
51
  }