@canonmsg/core 0.21.0 → 0.22.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/approval-manager.js +62 -21
- package/dist/client.d.ts +57 -0
- package/dist/client.js +29 -0
- package/package.json +1 -1
package/dist/approval-manager.js
CHANGED
|
@@ -50,7 +50,7 @@ export class ApprovalManager {
|
|
|
50
50
|
}
|
|
51
51
|
const approvalId = generateApprovalId();
|
|
52
52
|
const expiresAt = new Date(Date.now() + this.config.timeoutSeconds * 1000).toISOString();
|
|
53
|
-
const {
|
|
53
|
+
const { metadata } = buildApprovalRequest(approvalId, toolName, toolInput, {
|
|
54
54
|
riskLevel: opts?.riskLevel,
|
|
55
55
|
risk: opts?.risk,
|
|
56
56
|
category: opts?.category,
|
|
@@ -63,29 +63,47 @@ export class ApprovalManager {
|
|
|
63
63
|
expiresAt,
|
|
64
64
|
redactPatterns: this.config.redactPatterns,
|
|
65
65
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
},
|
|
66
|
+
await this.client.createRuntimeApprovalRequest({
|
|
67
|
+
conversationId,
|
|
68
|
+
approvalId,
|
|
69
|
+
toolName,
|
|
70
|
+
toolSummary: metadata.toolSummary,
|
|
71
|
+
expiresAt: Date.parse(expiresAt),
|
|
72
|
+
responseUserId: this.ownerId,
|
|
73
|
+
...(opts?.riskLevel ? { riskLevel: opts.riskLevel } : {}),
|
|
74
|
+
...(opts?.risk ? { risk: opts.risk } : {}),
|
|
75
|
+
...(opts?.category ? { category: opts.category } : {}),
|
|
76
|
+
...(opts?.runtimeId ? { runtimeId: opts.runtimeId } : {}),
|
|
77
|
+
...(opts?.turnId ? { turnId: opts.turnId } : {}),
|
|
78
|
+
...(metadata.native ? { native: metadata.native } : {}),
|
|
79
|
+
...(metadata.details ? { details: metadata.details } : {}),
|
|
80
|
+
...(opts?.allowSessionRule === false ? { allowSessionRule: false } : {}),
|
|
74
81
|
});
|
|
75
82
|
// Wait for reply or timeout
|
|
76
83
|
return new Promise((resolve) => {
|
|
77
|
-
|
|
84
|
+
let settled = false;
|
|
85
|
+
let timer;
|
|
86
|
+
const finish = (result, reason) => {
|
|
87
|
+
if (settled)
|
|
88
|
+
return;
|
|
89
|
+
settled = true;
|
|
90
|
+
clearTimeout(timer);
|
|
78
91
|
this.pending.delete(approvalId);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
if (reason === 'timeout') {
|
|
93
|
+
const msg = buildApprovalOutcome(approvalId, toolName, metadata.toolSummary, 'deny', 'timeout');
|
|
94
|
+
this.client.sendMessage(conversationId, msg, {
|
|
95
|
+
metadata: {
|
|
96
|
+
type: 'approval_outcome',
|
|
97
|
+
approvalId,
|
|
98
|
+
decision: 'deny',
|
|
99
|
+
reason: 'timeout',
|
|
100
|
+
},
|
|
101
|
+
}).catch(() => { });
|
|
102
|
+
}
|
|
103
|
+
resolve(result);
|
|
104
|
+
};
|
|
105
|
+
timer = setTimeout(() => {
|
|
106
|
+
finish({ decision: 'deny' }, 'timeout');
|
|
89
107
|
}, this.config.timeoutSeconds * 1000);
|
|
90
108
|
this.pending.set(approvalId, {
|
|
91
109
|
approvalId,
|
|
@@ -93,9 +111,32 @@ export class ApprovalManager {
|
|
|
93
111
|
toolName,
|
|
94
112
|
toolSummary: metadata.toolSummary,
|
|
95
113
|
allowSessionRule: opts?.allowSessionRule !== false,
|
|
96
|
-
resolve,
|
|
114
|
+
resolve: (result) => finish(result, 'replied'),
|
|
97
115
|
timer,
|
|
98
116
|
});
|
|
117
|
+
const poll = async () => {
|
|
118
|
+
while (this.pending.has(approvalId)) {
|
|
119
|
+
try {
|
|
120
|
+
const response = await this.client.consumeRuntimeApprovalResponse({
|
|
121
|
+
conversationId,
|
|
122
|
+
approvalId,
|
|
123
|
+
});
|
|
124
|
+
if (response.status === 'allow' || response.status === 'deny') {
|
|
125
|
+
this.resolveApproval(approvalId, response.status, response.sessionRule, conversationId);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (response.status === 'timeout') {
|
|
129
|
+
finish({ decision: 'deny' }, 'timeout');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
// Keep polling through transient API errors until the local timeout.
|
|
135
|
+
}
|
|
136
|
+
await new Promise((resume) => setTimeout(resume, 1000));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
void poll();
|
|
99
140
|
});
|
|
100
141
|
}
|
|
101
142
|
/**
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { type CanonMessage, type CanonConversation, type CanonContact, type CanonContactRequest, type CanonMessagesPage, type CanonResolveAdmissionResult, type AgentContext, type AddMemberResult, type CreateContactRequestResult, type MediaAttachment, type SendMessageOptions, type CreateConversationOptions, type RegistrationStatus, type SetStreamingOptions } from './types.js';
|
|
2
2
|
import type { RuntimeInputKind } from './runtime-cards.js';
|
|
3
|
+
import type { ApprovalNativeRequestMetadata, ApprovalRequestCategory, ApprovalRequestDetail, ApprovalRisk, SessionRule } from './approval-types.js';
|
|
4
|
+
import type { RuntimeInputChoice, RuntimeInputNativeMetadata } from './runtime-cards.js';
|
|
3
5
|
import type { SendContextualMessageOptions, SendContextualMessageResult } from './self-context.js';
|
|
4
6
|
import type { InboundDisposition } from './turn-protocol.js';
|
|
5
7
|
/**
|
|
@@ -59,10 +61,19 @@ export declare class CanonClient {
|
|
|
59
61
|
inputId: string;
|
|
60
62
|
kind: RuntimeInputKind;
|
|
61
63
|
expiresAt: number;
|
|
64
|
+
title?: string;
|
|
65
|
+
prompt?: string;
|
|
66
|
+
choices?: RuntimeInputChoice[];
|
|
67
|
+
secretName?: string;
|
|
68
|
+
native?: RuntimeInputNativeMetadata;
|
|
69
|
+
sensitive?: boolean;
|
|
70
|
+
responseUserId?: string;
|
|
71
|
+
turnId?: string;
|
|
62
72
|
}): Promise<{
|
|
63
73
|
success: true;
|
|
64
74
|
inputId: string;
|
|
65
75
|
expiresAt: number;
|
|
76
|
+
messageId?: string;
|
|
66
77
|
}>;
|
|
67
78
|
consumeRuntimeInputResponse(options: {
|
|
68
79
|
conversationId: string;
|
|
@@ -86,6 +97,52 @@ export declare class CanonClient {
|
|
|
86
97
|
inputId: string;
|
|
87
98
|
kind: RuntimeInputKind;
|
|
88
99
|
}>;
|
|
100
|
+
createRuntimeApprovalRequest(options: {
|
|
101
|
+
conversationId: string;
|
|
102
|
+
approvalId?: string;
|
|
103
|
+
toolName: string;
|
|
104
|
+
toolSummary: string;
|
|
105
|
+
expiresAt: number;
|
|
106
|
+
responseUserId?: string;
|
|
107
|
+
riskLevel?: 'normal' | 'destructive';
|
|
108
|
+
risk?: ApprovalRisk;
|
|
109
|
+
category?: ApprovalRequestCategory;
|
|
110
|
+
runtimeId?: string;
|
|
111
|
+
turnId?: string;
|
|
112
|
+
native?: ApprovalNativeRequestMetadata;
|
|
113
|
+
details?: ApprovalRequestDetail[];
|
|
114
|
+
allowSessionRule?: boolean;
|
|
115
|
+
}): Promise<{
|
|
116
|
+
success: true;
|
|
117
|
+
approvalId: string;
|
|
118
|
+
expiresAt: number;
|
|
119
|
+
messageId?: string;
|
|
120
|
+
}>;
|
|
121
|
+
consumeRuntimeApprovalResponse(options: {
|
|
122
|
+
conversationId: string;
|
|
123
|
+
approvalId: string;
|
|
124
|
+
cancel?: boolean;
|
|
125
|
+
}): Promise<{
|
|
126
|
+
status: 'pending';
|
|
127
|
+
approvalId: string;
|
|
128
|
+
expiresAt?: number;
|
|
129
|
+
} | {
|
|
130
|
+
status: 'allow';
|
|
131
|
+
approvalId: string;
|
|
132
|
+
sessionRule?: SessionRule;
|
|
133
|
+
} | {
|
|
134
|
+
status: 'deny';
|
|
135
|
+
approvalId: string;
|
|
136
|
+
sessionRule?: SessionRule;
|
|
137
|
+
} | {
|
|
138
|
+
status: 'timeout';
|
|
139
|
+
approvalId: string;
|
|
140
|
+
}>;
|
|
141
|
+
updateRuntimeStatus(options: {
|
|
142
|
+
runtime: string;
|
|
143
|
+
hostMode?: boolean;
|
|
144
|
+
runtimeDescriptor?: Record<string, unknown>;
|
|
145
|
+
}): Promise<void>;
|
|
89
146
|
static register(baseUrl: string | undefined, body: {
|
|
90
147
|
name: string;
|
|
91
148
|
description: string;
|
package/dist/client.js
CHANGED
|
@@ -339,6 +339,35 @@ export class CanonClient {
|
|
|
339
339
|
throw new CanonApiError(res.status, await res.text());
|
|
340
340
|
return res.json();
|
|
341
341
|
}
|
|
342
|
+
async createRuntimeApprovalRequest(options) {
|
|
343
|
+
const res = await fetch(`${this.baseUrl}/runtime-approval/request`, {
|
|
344
|
+
method: 'POST',
|
|
345
|
+
headers: this.authHeaders(),
|
|
346
|
+
body: JSON.stringify(options),
|
|
347
|
+
});
|
|
348
|
+
if (!res.ok)
|
|
349
|
+
throw new CanonApiError(res.status, await res.text());
|
|
350
|
+
return res.json();
|
|
351
|
+
}
|
|
352
|
+
async consumeRuntimeApprovalResponse(options) {
|
|
353
|
+
const res = await fetch(`${this.baseUrl}/runtime-approval/consume`, {
|
|
354
|
+
method: 'POST',
|
|
355
|
+
headers: this.authHeaders(),
|
|
356
|
+
body: JSON.stringify(options),
|
|
357
|
+
});
|
|
358
|
+
if (!res.ok)
|
|
359
|
+
throw new CanonApiError(res.status, await res.text());
|
|
360
|
+
return res.json();
|
|
361
|
+
}
|
|
362
|
+
async updateRuntimeStatus(options) {
|
|
363
|
+
const res = await fetch(`${this.baseUrl}/runtime/status`, {
|
|
364
|
+
method: 'POST',
|
|
365
|
+
headers: this.authHeaders(),
|
|
366
|
+
body: JSON.stringify(options),
|
|
367
|
+
});
|
|
368
|
+
if (!res.ok)
|
|
369
|
+
throw new CanonApiError(res.status, await res.text());
|
|
370
|
+
}
|
|
342
371
|
// ── Static unauthenticated registration endpoints ────────────────────
|
|
343
372
|
static async register(baseUrl, body) {
|
|
344
373
|
const url = baseUrl || DEFAULT_BASE_URL;
|