@keemakr/operator-sdk 0.8.0 → 0.10.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/package.json +1 -1
- package/src/conformance/index.ts +11 -0
- package/src/index.ts +46 -8
- package/src/manifest.ts +274 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keemakr/operator-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "The keemakr connector contract: defineProvider + ConnectionProvider/ConnectionOperation types + OperationContext (governed fetch) + the connector manifest schema, shared by keemakr-core's built-in adapters and external connector repos. One shape, two consumers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/conformance/index.ts
CHANGED
|
@@ -110,11 +110,14 @@ export async function runConformance(
|
|
|
110
110
|
// 9/10 — per-op access + idempotency match the reviewed declarations.
|
|
111
111
|
const accessMismatch: string[] = [];
|
|
112
112
|
const idemMismatch: string[] = [];
|
|
113
|
+
const decisionMismatch: string[] = [];
|
|
113
114
|
for (const decl of manifest.operations) {
|
|
114
115
|
const op = provider.operations?.[decl.name];
|
|
115
116
|
if (!op) continue;
|
|
116
117
|
if ((op.access ?? 'write') !== decl.readWrite) accessMismatch.push(decl.name);
|
|
117
118
|
if ((op.idempotent ?? false) !== decl.idempotent) idemMismatch.push(decl.name);
|
|
119
|
+
if ((op.decisionOnly ?? false) !== (decl.decision_only ?? false))
|
|
120
|
+
decisionMismatch.push(decl.name);
|
|
118
121
|
}
|
|
119
122
|
add(
|
|
120
123
|
'ops-access',
|
|
@@ -128,6 +131,14 @@ export async function runConformance(
|
|
|
128
131
|
idemMismatch.length === 0,
|
|
129
132
|
idemMismatch.length ? idemMismatch.join(', ') : undefined,
|
|
130
133
|
);
|
|
134
|
+
// Core reads the flag from the manifest for a remote or MCP connector and from the code for a
|
|
135
|
+
// vendored one, so a connector whose two disagree is a person's decision on one lane only.
|
|
136
|
+
add(
|
|
137
|
+
'ops-decision-only',
|
|
138
|
+
'op decision-only flags match the manifest',
|
|
139
|
+
decisionMismatch.length === 0,
|
|
140
|
+
decisionMismatch.length ? decisionMismatch.join(', ') : undefined,
|
|
141
|
+
);
|
|
131
142
|
|
|
132
143
|
// 11/12 — schemas parse safely; call signatures accept (cred, args).
|
|
133
144
|
const badSchema: string[] = [];
|
package/src/index.ts
CHANGED
|
@@ -134,6 +134,14 @@ export interface OperationContext {
|
|
|
134
134
|
// needs attribution must treat an absent actor the same as an unattended one and refuse, rather
|
|
135
135
|
// than falling back to an args value. Frozen: read it, never write it.
|
|
136
136
|
actor?: Readonly<OperationActor>;
|
|
137
|
+
// A key for this one write, derived by the platform from the caller's Idempotency-Key, when the
|
|
138
|
+
// caller sent one. The same retried call carries the same key; another call never does. Pass it
|
|
139
|
+
// to a provider that accepts its own idempotency key (Stripe's `Idempotency-Key`, for example)
|
|
140
|
+
// so a retry the platform cannot see, such as your own retry after a timeout, is still applied
|
|
141
|
+
// once. It is not the caller's raw key: a value derived per tenant and install, safe to send to
|
|
142
|
+
// the provider. Absent on reads, on calls made without a key, and outside an in-process
|
|
143
|
+
// connector's ctx.
|
|
144
|
+
idempotencyKey?: string;
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
// Slim context for the lifecycle hooks (buildAuthUrl/exchangeCode/refresh, and
|
|
@@ -325,6 +333,19 @@ export interface ConnectionOperation {
|
|
|
325
333
|
// Safe-to-retry marker. Omitted ⇒ false. The platform NEVER auto-retries a
|
|
326
334
|
// non-idempotent operation; callers (agents) read this to know what is safe.
|
|
327
335
|
idempotent?: boolean;
|
|
336
|
+
// How long this one operation may run, in milliseconds, end to end. Omitted ⇒ the platform's
|
|
337
|
+
// default (30 s). The platform caps it at 120 s: work that needs longer must not hold a request
|
|
338
|
+
// open. When the caller's own deadline is nearer, the platform uses that instead, so a call
|
|
339
|
+
// never outlives the request that asked for it. A write that runs out of time answers
|
|
340
|
+
// `retry: 'unknown'`: it may have reached the provider. Read for in-process operations (built-in
|
|
341
|
+
// and local-runtime connectors); a remote or MCP runtime keeps its own clock for now.
|
|
342
|
+
timeoutMs?: number;
|
|
343
|
+
// A decision a person makes (approve a task, start a paid render). The platform runs it only
|
|
344
|
+
// from a person's approval of these exact arguments (an Approvals card), and names that approver
|
|
345
|
+
// to the connector as `ctx.actor` with `via: 'approval'`; a direct call from an agent, a routine,
|
|
346
|
+
// chat or the MCP server is refused once the company's gate enforces. Declare it on a write.
|
|
347
|
+
// Omitted ⇒ false. Mirror it in the manifest as `decision_only: true`.
|
|
348
|
+
decisionOnly?: boolean;
|
|
328
349
|
// The fields of the RESULT that are files a person should see: generated
|
|
329
350
|
// images, synthesized audio. `path` names them with dots and `[]` for every
|
|
330
351
|
// item of a list, e.g. `images[].url` or `audio.url`. When the Operator calls
|
|
@@ -332,6 +353,11 @@ export interface ConnectionOperation {
|
|
|
332
353
|
// core keeps its own copy, so the card still works after the provider's link
|
|
333
354
|
// expires. Omitted ⇒ the result holds no media.
|
|
334
355
|
media?: ReadonlyArray<{ kind: 'image' | 'audio'; path: string }>;
|
|
356
|
+
// The operation takes one long text argument in ordered parts: `text` is that argument,
|
|
357
|
+
// `index` the part number, `last` (when the operation has one) true on the final part, and
|
|
358
|
+
// `maxChars` the most one part may hold. Lets the Operator pass a chat attachment by id and
|
|
359
|
+
// have core make the calls, one approval for the whole file. Omitted ⇒ no such argument.
|
|
360
|
+
textChunks?: { text: string; index: string; last?: string; maxChars: number };
|
|
335
361
|
// A Standard Schema (zod/valibot/…) validating the agent-supplied args. Typed
|
|
336
362
|
// loosely here so the connector contract doesn't hard-depend on zod; adapters
|
|
337
363
|
// pass a zod schema and the route validates with `.parse`/`.safeParse`.
|
|
@@ -396,25 +422,37 @@ export type ProviderDefinition<Ops extends Record<string, OperationInputSchema>>
|
|
|
396
422
|
// can be the connector's own client credentials.
|
|
397
423
|
// egress_blocked → the governed fetch refused a non-allowlisted host
|
|
398
424
|
// rate_limited → upstream 429 (platform applies backoff)
|
|
399
|
-
export
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
425
|
+
export const OPERATION_ERROR_CODES = [
|
|
426
|
+
'provider_unavailable',
|
|
427
|
+
'connector_error',
|
|
428
|
+
'auth_revoked',
|
|
429
|
+
'egress_blocked',
|
|
430
|
+
'rate_limited',
|
|
405
431
|
// The CONNECTOR refused the call on policy — typically an action that must be attributable
|
|
406
432
|
// reached it unattended. A denial, not a failure: it does not count against the connector's
|
|
407
433
|
// health or the provider's circuit breaker, so refusing correctly cannot degrade you.
|
|
408
|
-
|
|
434
|
+
'forbidden',
|
|
409
435
|
// The provider ANSWERED and rejected what the caller sent — bad args, an unknown record, a
|
|
410
436
|
// failed validation. The caller's fault, not the provider's: like `forbidden` it does not count
|
|
411
437
|
// against health or the circuit breaker, so one client's bad input cannot shed the connector
|
|
412
438
|
// for every tenant.
|
|
413
|
-
|
|
439
|
+
'invalid_request',
|
|
440
|
+
] as const;
|
|
441
|
+
|
|
442
|
+
/** One of {@link OPERATION_ERROR_CODES}. Core drops any other code a connector sends. */
|
|
443
|
+
export type OperationErrorCode = (typeof OPERATION_ERROR_CODES)[number];
|
|
444
|
+
|
|
445
|
+
// How core recognises an OperationError. A vendored connector can load its own copy of this
|
|
446
|
+
// package, and then `instanceof OperationError` is false in core for every error it throws: the
|
|
447
|
+
// two classes are different objects with the same shape. `Symbol.for` returns the same symbol from
|
|
448
|
+
// the global registry in every copy and every realm, so the brand survives where the class does
|
|
449
|
+
// not. Core reads the brand first, then checks `code` against OPERATION_ERROR_CODES.
|
|
450
|
+
export const OPERATION_ERROR_BRAND: unique symbol = Symbol.for('keemakr.operation-error');
|
|
414
451
|
|
|
415
452
|
// Throw from a ConnectionOperation.call to surface a typed, agent-safe failure
|
|
416
453
|
// (maps to a 4xx with a clean message — never leaks the credential).
|
|
417
454
|
export class OperationError extends Error {
|
|
455
|
+
readonly [OPERATION_ERROR_BRAND] = 1 as const;
|
|
418
456
|
status: number;
|
|
419
457
|
code?: OperationErrorCode;
|
|
420
458
|
constructor(message: string, status = 400, code?: OperationErrorCode) {
|
package/src/manifest.ts
CHANGED
|
@@ -124,9 +124,43 @@ export const connectorOperationDeclaration = z.object({
|
|
|
124
124
|
readWrite: z.enum(['read', 'write']),
|
|
125
125
|
idempotent: z.boolean(),
|
|
126
126
|
description: z.string().max(500).optional(),
|
|
127
|
+
// A decision a person makes (approve a task, start a paid render): core runs it only from a
|
|
128
|
+
// person's approval of the exact call and refuses every other caller once the company's gate
|
|
129
|
+
// enforces. Optional for the same reason as input_schema below.
|
|
130
|
+
decision_only: z.boolean().optional(),
|
|
127
131
|
// OPTIONAL, and it must stay optional: stored manifests of already-published versions are
|
|
128
132
|
// re-parsed with the current schema at every publish, and they do not carry it.
|
|
129
133
|
input_schema: operationInputJsonSchema.optional(),
|
|
134
|
+
// OPTIONAL, for the same reason. The operation takes one long text argument in ordered parts
|
|
135
|
+
// (Kee CRM's crm-csv-import-append: importId, chunkIndex, text, last). Declaring it lets the
|
|
136
|
+
// Operator hand the operation a chat attachment by id: core reads the file and calls the
|
|
137
|
+
// operation once per part, `index` counting up from 0 (a call that sets it is refused: core
|
|
138
|
+
// always sends the whole file, and a retry resends it from 0), `last` true on the final part,
|
|
139
|
+
// all under the person's one approval (KEE-1279). `text`,
|
|
140
|
+
// `index` and `last` name properties of `input_schema`, which must then be declared.
|
|
141
|
+
text_chunks: z
|
|
142
|
+
.object({
|
|
143
|
+
text: z.string().min(1).max(64),
|
|
144
|
+
index: z.string().min(1).max(64),
|
|
145
|
+
last: z.string().min(1).max(64).optional(),
|
|
146
|
+
max_chars: z.number().int().min(1_000).max(1_000_000),
|
|
147
|
+
})
|
|
148
|
+
.optional(),
|
|
149
|
+
// OPTIONAL, for the same reason. The fields this operation RETURNS to the platform, by name and
|
|
150
|
+
// type ('string', 'date', 'number', or a type of the connector's own). PHI never reaches the
|
|
151
|
+
// platform: it stays in the EHR/EMR behind its connector (D-PHI), so this list is what a reviewer
|
|
152
|
+
// reads to see that. Publish warns on a name or type that reads as a patient-record field
|
|
153
|
+
// (patientRecordOutputWarnings); staff review refuses such a connector. Nothing checks call
|
|
154
|
+
// results against it at run time.
|
|
155
|
+
outputs: z
|
|
156
|
+
.array(
|
|
157
|
+
z.object({
|
|
158
|
+
name: z.string().min(1).max(100),
|
|
159
|
+
type: z.string().min(1).max(64),
|
|
160
|
+
}),
|
|
161
|
+
)
|
|
162
|
+
.max(200)
|
|
163
|
+
.optional(),
|
|
130
164
|
});
|
|
131
165
|
|
|
132
166
|
// A name inside a push scheme's envelope (`page`, `leadgen`): lowercase, underscores allowed.
|
|
@@ -138,6 +172,16 @@ const pushAccountOp = z.object({
|
|
|
138
172
|
account_ref_arg: z.string().min(1).max(100),
|
|
139
173
|
});
|
|
140
174
|
|
|
175
|
+
/**
|
|
176
|
+
* The subscribe op of a scheme signed per account (`hmac_sha256`) may also name the argument core
|
|
177
|
+
* hands the account's signing secret in. Core mints that secret for the workspace's account and
|
|
178
|
+
* fills the argument itself (a value the caller sends there is dropped), so the connector can give
|
|
179
|
+
* it to its provider; the door then verifies that account's deliveries with it.
|
|
180
|
+
*/
|
|
181
|
+
const pushRegisterOp = pushAccountOp.extend({
|
|
182
|
+
secret_arg: z.string().min(1).max(100).optional(),
|
|
183
|
+
});
|
|
184
|
+
|
|
141
185
|
/**
|
|
142
186
|
* PUSHED UPDATES: the provider calls core when something happens, instead of core asking. Core
|
|
143
187
|
* owns the door (`/api/webhooks/connections/<provider>`): it checks the provider's signature with
|
|
@@ -146,7 +190,13 @@ const pushAccountOp = z.object({
|
|
|
146
190
|
*
|
|
147
191
|
* `scheme` is the provider's protocol, implemented once in core. `websub_graph` is the WebSub
|
|
148
192
|
* verification handshake (`hub.challenge`) plus an `X-Hub-Signature-256` HMAC of the raw body and
|
|
149
|
-
* the `{ object, entry: [{ id, changes: [{ field, value }] }] }` envelope.
|
|
193
|
+
* the `{ object, entry: [{ id, changes: [{ field, value }] }] }` envelope. `hmac_sha256` is a
|
|
194
|
+
* generic signed callback with no handshake: an `X-Kee-Signature: t=<unix seconds>,v1=<hex>`
|
|
195
|
+
* header, the HMAC-SHA256 of `<t>.<raw body>`, refused outside a five-minute window, and the
|
|
196
|
+
* `{ account, events: [{ id, object, field, value }] }` envelope, one account per delivery. An
|
|
197
|
+
* event's `id` is the provider's own: a resend of the same id for the same account is one event.
|
|
198
|
+
* Its secret is the connector's config secret (`secret_key`) or, once core signs per account, the
|
|
199
|
+
* one core minted for that account and handed to `register.secret_arg`.
|
|
150
200
|
*
|
|
151
201
|
* `fields` is the REVIEWED allowlist of what may come through: an update whose (object, field) is
|
|
152
202
|
* not listed is dropped at the door. What a listed field carries reaches a handler under the
|
|
@@ -164,9 +214,13 @@ const pushAccountOp = z.object({
|
|
|
164
214
|
*/
|
|
165
215
|
export const connectorPushDeclaration = z
|
|
166
216
|
.object({
|
|
167
|
-
scheme: z.enum(['websub_graph']),
|
|
217
|
+
scheme: z.enum(['websub_graph', 'hmac_sha256']),
|
|
168
218
|
secret_key: z.string().regex(/^[A-Z][A-Z0-9_]*$/, 'config keys are SCREAMING_SNAKE'),
|
|
169
|
-
|
|
219
|
+
// `websub_graph` only: the token of the provider's handshake. `hmac_sha256` has no handshake.
|
|
220
|
+
verify_token_key: z
|
|
221
|
+
.string()
|
|
222
|
+
.regex(/^[A-Z][A-Z0-9_]*$/, 'config keys are SCREAMING_SNAKE')
|
|
223
|
+
.optional(),
|
|
170
224
|
fields: z
|
|
171
225
|
.array(
|
|
172
226
|
z.object({
|
|
@@ -176,13 +230,35 @@ export const connectorPushDeclaration = z
|
|
|
176
230
|
)
|
|
177
231
|
.min(1, 'declare at least one (object, field) the provider may push')
|
|
178
232
|
.max(50),
|
|
179
|
-
register:
|
|
233
|
+
register: pushRegisterOp,
|
|
180
234
|
unregister: pushAccountOp.optional(),
|
|
181
235
|
})
|
|
182
236
|
.superRefine((p, ctx) => {
|
|
237
|
+
if (p.scheme === 'websub_graph' && !p.verify_token_key) {
|
|
238
|
+
ctx.addIssue({
|
|
239
|
+
code: 'custom',
|
|
240
|
+
path: ['verify_token_key'],
|
|
241
|
+
message: 'websub_graph needs verify_token_key: the provider verifies the address with it',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (p.register.secret_arg !== undefined) {
|
|
245
|
+
if (p.scheme !== 'hmac_sha256') {
|
|
246
|
+
ctx.addIssue({
|
|
247
|
+
code: 'custom',
|
|
248
|
+
path: ['register', 'secret_arg'],
|
|
249
|
+
message: 'register.secret_arg is only for hmac_sha256: its provider signs per account',
|
|
250
|
+
});
|
|
251
|
+
} else if (p.register.secret_arg === p.register.account_ref_arg) {
|
|
252
|
+
ctx.addIssue({
|
|
253
|
+
code: 'custom',
|
|
254
|
+
path: ['register', 'secret_arg'],
|
|
255
|
+
message: 'register.secret_arg must be a different argument from account_ref_arg',
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
183
259
|
// The verify token is sent in the clear (a URL query) during the provider's handshake; the secret
|
|
184
260
|
// signs every delivery for every customer. One key for both would put the secret in access logs.
|
|
185
|
-
if (p.secret_key === p.verify_token_key) {
|
|
261
|
+
if (p.verify_token_key !== undefined && p.secret_key === p.verify_token_key) {
|
|
186
262
|
ctx.addIssue({
|
|
187
263
|
code: 'custom',
|
|
188
264
|
path: ['verify_token_key'],
|
|
@@ -212,17 +288,25 @@ export const connectorPushDeclaration = z
|
|
|
212
288
|
|
|
213
289
|
/**
|
|
214
290
|
* What a push declaration must agree on with the rest of its manifest: the secret and the verify
|
|
215
|
-
* token are declared config keys (staff can only set a declared key),
|
|
216
|
-
*
|
|
217
|
-
*
|
|
291
|
+
* token are declared config keys (staff can only set a declared key), the register and unregister
|
|
292
|
+
* operations are declared operations, and `register.secret_arg` is an argument the register
|
|
293
|
+
* operation declares but does not require (a caller never has it to send: core fills it). Paths are
|
|
294
|
+
* relative to `push`. Pure. Core runs it again when it reads a pinned manifest, so a block that no
|
|
295
|
+
* longer agrees opens no door.
|
|
218
296
|
*/
|
|
219
297
|
export function pushCrossCheckIssues(
|
|
220
298
|
push: ConnectorPushDeclaration,
|
|
221
|
-
manifest: {
|
|
299
|
+
manifest: {
|
|
300
|
+
env: readonly string[];
|
|
301
|
+
operations: ReadonlyArray<{
|
|
302
|
+
name: string;
|
|
303
|
+
input_schema?: { properties?: Record<string, unknown>; required?: readonly string[] };
|
|
304
|
+
}>;
|
|
305
|
+
},
|
|
222
306
|
): Array<{ path: (string | number)[]; message: string }> {
|
|
223
307
|
const out: Array<{ path: (string | number)[]; message: string }> = [];
|
|
224
308
|
for (const key of ['secret_key', 'verify_token_key'] as const) {
|
|
225
|
-
if (!manifest.env.includes(push[key])) {
|
|
309
|
+
if (push[key] !== undefined && !manifest.env.includes(push[key])) {
|
|
226
310
|
out.push({
|
|
227
311
|
path: [key],
|
|
228
312
|
message: `push.${key} '${push[key]}' must be one of the config keys declared in env`,
|
|
@@ -239,6 +323,33 @@ export function pushCrossCheckIssues(
|
|
|
239
323
|
});
|
|
240
324
|
}
|
|
241
325
|
}
|
|
326
|
+
const secretArg = push.register.secret_arg;
|
|
327
|
+
if (secretArg !== undefined) {
|
|
328
|
+
const schema = manifest.operations.find((o) => o.name === push.register.op)?.input_schema;
|
|
329
|
+
// Read defensively: core runs this on a stored manifest it reads back as plain JSON.
|
|
330
|
+
const properties = schema?.properties;
|
|
331
|
+
if (!properties || typeof properties !== 'object' || !Object.hasOwn(properties, secretArg)) {
|
|
332
|
+
out.push({
|
|
333
|
+
path: ['register', 'secret_arg'],
|
|
334
|
+
message: `push.register.secret_arg '${secretArg}' must be one of the register operation's input_schema properties`,
|
|
335
|
+
});
|
|
336
|
+
} else if (Array.isArray(schema?.required) && schema.required.includes(secretArg)) {
|
|
337
|
+
out.push({
|
|
338
|
+
path: ['register', 'secret_arg'],
|
|
339
|
+
message: `push.register.secret_arg '${secretArg}' must not be required: core fills it, a caller never sends it`,
|
|
340
|
+
});
|
|
341
|
+
} else {
|
|
342
|
+
// Core sends the secret as a string: a property of another type fails every subscribe.
|
|
343
|
+
const declared = (properties as Record<string, { type?: unknown } | undefined>)[secretArg]
|
|
344
|
+
?.type;
|
|
345
|
+
if (declared !== 'string' && !(Array.isArray(declared) && declared.includes('string'))) {
|
|
346
|
+
out.push({
|
|
347
|
+
path: ['register', 'secret_arg'],
|
|
348
|
+
message: `push.register.secret_arg '${secretArg}' must be declared type 'string': core sends the secret as one`,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
242
353
|
return out;
|
|
243
354
|
}
|
|
244
355
|
|
|
@@ -353,6 +464,75 @@ export const connectorManifestSchema = z
|
|
|
353
464
|
'declare exactly one lane — entrypoint, runtime_url, and mcp are mutually exclusive',
|
|
354
465
|
});
|
|
355
466
|
}
|
|
467
|
+
m.operations.forEach((op, i) => {
|
|
468
|
+
// A person's decision runs only from an Approvals card, and only a write is held for one: a
|
|
469
|
+
// decision-only read would be refused on every lane once a company enforces (WS8-S18).
|
|
470
|
+
if (op.decision_only === true && op.readWrite !== 'write') {
|
|
471
|
+
ctx.addIssue({
|
|
472
|
+
code: 'custom',
|
|
473
|
+
path: ['operations', i, 'decision_only'],
|
|
474
|
+
message:
|
|
475
|
+
'decision_only is only for an operation with readWrite: "write" — a person approves a write, and a read is never held for approval',
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
if (!op.text_chunks) return;
|
|
479
|
+
const path = ['operations', i, 'text_chunks'];
|
|
480
|
+
// Core sends a file's parts under one approval and a retry resends every part from 0, which
|
|
481
|
+
// is only safe for a write whose parts are idempotent: a read is never held for approval,
|
|
482
|
+
// and a non-idempotent part would land twice on the retry.
|
|
483
|
+
if (op.readWrite !== 'write' || op.idempotent !== true) {
|
|
484
|
+
ctx.addIssue({
|
|
485
|
+
code: 'custom',
|
|
486
|
+
path,
|
|
487
|
+
message:
|
|
488
|
+
'text_chunks is only for an operation with readWrite: "write" and idempotent: true — a retry resends every part from the start',
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
const properties = op.input_schema?.properties;
|
|
492
|
+
if (!properties) {
|
|
493
|
+
ctx.addIssue({
|
|
494
|
+
code: 'custom',
|
|
495
|
+
path,
|
|
496
|
+
message: 'text_chunks needs an input_schema that declares its arguments',
|
|
497
|
+
});
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const { text, index, last } = op.text_chunks;
|
|
501
|
+
const names = [text, index, ...(last ? [last] : [])];
|
|
502
|
+
if (new Set(names).size !== names.length) {
|
|
503
|
+
ctx.addIssue({
|
|
504
|
+
code: 'custom',
|
|
505
|
+
path,
|
|
506
|
+
message: 'text_chunks must name a different argument for each of text, index and last',
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
// Each argument core fills must be declared with the type core sends, or every part fails
|
|
510
|
+
// with a vague "invalid arguments" (a string-typed index, say).
|
|
511
|
+
const expected: [string, string][] = [
|
|
512
|
+
[text, 'string'],
|
|
513
|
+
[index, 'integer'],
|
|
514
|
+
...(last ? ([[last, 'boolean']] as [string, string][]) : []),
|
|
515
|
+
];
|
|
516
|
+
for (const [name, type] of expected) {
|
|
517
|
+
const property = properties[name];
|
|
518
|
+
if (!property) {
|
|
519
|
+
ctx.addIssue({
|
|
520
|
+
code: 'custom',
|
|
521
|
+
path,
|
|
522
|
+
message: `text_chunks names '${name}', which is not one of input_schema's properties`,
|
|
523
|
+
});
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const declared = property.type;
|
|
527
|
+
if (declared !== type && !(Array.isArray(declared) && declared.includes(type))) {
|
|
528
|
+
ctx.addIssue({
|
|
529
|
+
code: 'custom',
|
|
530
|
+
path,
|
|
531
|
+
message: `text_chunks needs input_schema's '${name}' to be type '${type}' — core sends it as one`,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
});
|
|
356
536
|
if (m.runtime_url && !m.runtime_url.startsWith('https://')) {
|
|
357
537
|
ctx.addIssue({ code: 'custom', path: ['runtime_url'], message: 'runtime_url must be https' });
|
|
358
538
|
}
|
|
@@ -398,6 +578,90 @@ export const connectorManifestSchema = z
|
|
|
398
578
|
}
|
|
399
579
|
});
|
|
400
580
|
|
|
581
|
+
/**
|
|
582
|
+
* The category a connector to a health-record system (an EHR or EMR) declares. Its operations are
|
|
583
|
+
* expected to list their `outputs`, because that boundary is where PHI must stop (D-PHI).
|
|
584
|
+
*/
|
|
585
|
+
export const HEALTH_RECORD_CATEGORY = 'health-record';
|
|
586
|
+
|
|
587
|
+
// Category is free text, so an EHR connector filed as `ehr` or `emr` is read the same way.
|
|
588
|
+
const HEALTH_RECORD_CATEGORIES: ReadonlySet<string> = new Set([
|
|
589
|
+
HEALTH_RECORD_CATEGORY,
|
|
590
|
+
'ehr',
|
|
591
|
+
'emr',
|
|
592
|
+
]);
|
|
593
|
+
|
|
594
|
+
/** True for a category that names a health-record system: `health-record`, `ehr` or `emr`. Pure. */
|
|
595
|
+
export function isHealthRecordCategory(category: string): boolean {
|
|
596
|
+
return HEALTH_RECORD_CATEGORIES.has(category);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// Patient-record fields, matched on a field's name or declared type after splitting camelCase and
|
|
600
|
+
// punctuation into words: "patientDOB", "date-of-birth" and "Lab Results" all match. A short list on
|
|
601
|
+
// purpose: a lint a reviewer reads, not a classifier.
|
|
602
|
+
const PATIENT_RECORD_FIELDS: ReadonlyArray<[label: string, pattern: RegExp]> = [
|
|
603
|
+
['diagnosis', /(^|_)(diagnos[a-z]*|dx|icd(_?10)?)(_|$)/],
|
|
604
|
+
['medication', /(^|_)(medications?|meds|prescriptions?)(_|$)/],
|
|
605
|
+
['MRN', /(^|_)(mrns?|medical_records?(_numbers?)?)(_|$)/],
|
|
606
|
+
['date of birth', /(^|_)(dobs?|dates?_of_birth|birth_?(date|dt)s?)(_|$)/],
|
|
607
|
+
['clinical note', /(^|_)clinical_notes?(_|$)/],
|
|
608
|
+
// Not an accounting connector's chart of accounts.
|
|
609
|
+
['chart', /(^|_)charts?(?!_of_accounts)(_|$)/],
|
|
610
|
+
['lab result', /(^|_)lab_results?(_|$)/],
|
|
611
|
+
['insurance member id', /(^|_)insurance_(member_)?(id|number)(_|$)/],
|
|
612
|
+
];
|
|
613
|
+
|
|
614
|
+
const words = (text: string) =>
|
|
615
|
+
text
|
|
616
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
617
|
+
// An acronym ends where a capitalised word starts ("HTTPServer"), but its plural stays one
|
|
618
|
+
// word ("MRNs", "DOBs").
|
|
619
|
+
.replace(/([A-Z]+)([A-Z][a-z]{2,})/g, '$1_$2')
|
|
620
|
+
.toLowerCase()
|
|
621
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
622
|
+
.replace(/^_+|_+$/g, '');
|
|
623
|
+
|
|
624
|
+
/** The patient-record field a name or type reads as, or null. Pure. */
|
|
625
|
+
export function patientRecordField(text: string): string | null {
|
|
626
|
+
const normal = words(text);
|
|
627
|
+
for (const [label, pattern] of PATIENT_RECORD_FIELDS) if (pattern.test(normal)) return label;
|
|
628
|
+
return null;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* The D-PHI publish lint: one warning per declared output whose name or type reads as a
|
|
633
|
+
* patient-record field, and, for a connector in a health-record category, one per operation
|
|
634
|
+
* that declares no outputs. Warnings, never errors: staff decide at review. Pure.
|
|
635
|
+
*/
|
|
636
|
+
export function patientRecordOutputWarnings(manifest: {
|
|
637
|
+
category: string;
|
|
638
|
+
operations: ReadonlyArray<Pick<ConnectorOperationDeclaration, 'name' | 'outputs'>>;
|
|
639
|
+
}): string[] {
|
|
640
|
+
const out: string[] = [];
|
|
641
|
+
for (const op of manifest.operations) {
|
|
642
|
+
if (!op.outputs) {
|
|
643
|
+
if (isHealthRecordCategory(manifest.category)) {
|
|
644
|
+
out.push(
|
|
645
|
+
`operation '${op.name}' declares no outputs, and this is a health-record connector: ` +
|
|
646
|
+
`list the fields it returns so review can see no patient record reaches the platform (D-PHI)`,
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
for (const field of op.outputs) {
|
|
652
|
+
const hit = patientRecordField(field.name) ?? patientRecordField(field.type);
|
|
653
|
+
if (hit) {
|
|
654
|
+
out.push(
|
|
655
|
+
`operation '${op.name}' returns '${field.name}' (${field.type}), which reads as a ` +
|
|
656
|
+
`patient record (${hit}): PHI stays in the EMR behind its connector (D-PHI), and ` +
|
|
657
|
+
`review refuses a connector whose outputs carry patient-record fields`,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return out;
|
|
663
|
+
}
|
|
664
|
+
|
|
401
665
|
export type ConnectorOperationDeclaration = z.infer<typeof connectorOperationDeclaration>;
|
|
402
666
|
export type OperationInputJsonSchema = z.infer<typeof operationInputJsonSchema>;
|
|
403
667
|
export type ConnectorManifest = z.infer<typeof connectorManifestSchema>;
|