@process.co/sdk 0.0.0-stage → 0.1.0-next.2
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/REACT.md +97 -0
- package/README.md +99 -2
- package/conformance/v1.json +33 -0
- package/dist/app.d.ts +14 -0
- package/dist/app.js +21 -0
- package/dist/browser.d.ts +16 -0
- package/dist/browser.js +41 -0
- package/dist/connection-form.d.ts +7 -0
- package/dist/connection-form.js +28 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +22 -0
- package/dist/methods.d.ts +63 -0
- package/dist/methods.js +74 -0
- package/dist/react/action-card.d.ts +54 -0
- package/dist/react/action-card.js +42 -0
- package/dist/react/connection-form.d.ts +10 -0
- package/dist/react/connection-form.js +47 -0
- package/dist/react/index.d.ts +99 -0
- package/dist/react/index.js +33 -0
- package/dist/react/use-method.d.ts +39 -0
- package/dist/react/use-method.js +50 -0
- package/dist/server-api.d.ts +5 -0
- package/dist/server-api.js +24 -0
- package/dist/server.d.ts +11 -0
- package/dist/server.js +110 -0
- package/dist/url.d.ts +1 -0
- package/dist/url.js +12 -0
- package/dist/workflow.d.ts +103 -0
- package/dist/workflow.js +250 -0
- package/package.json +44 -4
package/dist/workflow.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InstalledWorkflowError = void 0;
|
|
4
|
+
exports.createInstalledWorkflow = createInstalledWorkflow;
|
|
5
|
+
const url_1 = require("./url");
|
|
6
|
+
const setupIssues = ['provider-not-oauth', 'provider-not-personal', 'workflow-scopes-missing', 'provider-scopes-mismatch'];
|
|
7
|
+
const invocationErrorCodes = ['AUTHENTICATION_REQUIRED', 'NOT_FOUND', 'SCOPE_DENIED', 'APPROVAL_REQUIRED',
|
|
8
|
+
'VERSION_UNAVAILABLE', 'INVALID_ARGUMENTS', 'LIMIT_EXCEEDED', 'EXECUTION_REJECTED', 'EXECUTION_FAILED',
|
|
9
|
+
'TIMEOUT', 'CANCELED', 'CREDENTIAL_REQUIRED', 'UNAVAILABLE'];
|
|
10
|
+
class InstalledWorkflowError extends Error {
|
|
11
|
+
status;
|
|
12
|
+
code;
|
|
13
|
+
settlement;
|
|
14
|
+
responseNotRetained;
|
|
15
|
+
constructor(status, code, settlement, responseNotRetained = false) {
|
|
16
|
+
super('Installed workflow request failed with HTTP ' + status);
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.code = code;
|
|
19
|
+
this.settlement = settlement;
|
|
20
|
+
this.responseNotRetained = responseNotRetained;
|
|
21
|
+
this.name = 'InstalledWorkflowError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.InstalledWorkflowError = InstalledWorkflowError;
|
|
25
|
+
/** Forward only published diagnostic fields. An authored error body can contain provider data. */
|
|
26
|
+
async function invocationError(response) {
|
|
27
|
+
if (!response.headers.get('content-type')?.toLowerCase().includes('application/json'))
|
|
28
|
+
return new InstalledWorkflowError(response.status);
|
|
29
|
+
const body = await response.json().catch(() => undefined);
|
|
30
|
+
const error = body?.error;
|
|
31
|
+
const code = invocationErrorCodes.find(candidate => candidate === error?.code);
|
|
32
|
+
const details = error?.details;
|
|
33
|
+
if (response.status === 409 && details?.reason === 'RESPONSE_NOT_RETAINED') {
|
|
34
|
+
return new InstalledWorkflowError(response.status, code, undefined, true);
|
|
35
|
+
}
|
|
36
|
+
if (code === 'UNAVAILABLE' && details?.phase === 'settlement-validation' &&
|
|
37
|
+
(details.reason === 'invalid-error-settlement' || details.reason === 'invalid-success-settlement') &&
|
|
38
|
+
Number.isInteger(details.upstreamStatus) && details.upstreamStatus >= 100 && details.upstreamStatus <= 599) {
|
|
39
|
+
const executionId = details.executionId;
|
|
40
|
+
return new InstalledWorkflowError(response.status, code, { reason: details.reason, upstreamStatus: details.upstreamStatus,
|
|
41
|
+
...(typeof executionId === 'string' && /^project-user-installed-(?:[0-9a-f]{64}|[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})$/i.test(executionId) && { executionId }) });
|
|
42
|
+
}
|
|
43
|
+
return new InstalledWorkflowError(response.status, code);
|
|
44
|
+
}
|
|
45
|
+
function origin(value) {
|
|
46
|
+
const url = (0, url_1.trustedUrl)(value);
|
|
47
|
+
if (url.pathname !== '/')
|
|
48
|
+
throw new Error('Expected a configured origin');
|
|
49
|
+
return url.origin;
|
|
50
|
+
}
|
|
51
|
+
function record(value) {
|
|
52
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
53
|
+
throw new Error('Invalid installed workflow response');
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function connectionStatus(value) {
|
|
57
|
+
const { state, revision, reasonCode } = record(value);
|
|
58
|
+
if (typeof revision === 'number' && Number.isSafeInteger(revision) && revision >= 0) {
|
|
59
|
+
if (state === 'ready')
|
|
60
|
+
return { state, revision };
|
|
61
|
+
if (state === 'credential-required' && typeof reasonCode === 'string' && reasonCode) {
|
|
62
|
+
return { state, revision, reasonCode };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw new Error('Invalid installed connection status');
|
|
66
|
+
}
|
|
67
|
+
function connectionRequired(body, consentOrigin, readOperation) {
|
|
68
|
+
const error = record(record(body).error);
|
|
69
|
+
if (error.code !== 'CREDENTIAL_REQUIRED')
|
|
70
|
+
throw new InstalledWorkflowError(428);
|
|
71
|
+
const { reasonCode, installUrl, confirmationCode, expiresAt, operation, setupIssue } = record(error.details);
|
|
72
|
+
const retained = operation === undefined ? {} : { operation: readOperation(operation) };
|
|
73
|
+
if (typeof reasonCode !== 'string' || !reasonCode)
|
|
74
|
+
throw new Error('Invalid connection response');
|
|
75
|
+
if (installUrl === undefined && confirmationCode === undefined && expiresAt === undefined) {
|
|
76
|
+
const knownIssue = setupIssues.find(code => code === setupIssue);
|
|
77
|
+
return { state: 'connection-required', reason: reasonCode, ...retained, ...(knownIssue && { setupIssue: knownIssue }) };
|
|
78
|
+
}
|
|
79
|
+
return { state: 'connection-required', reason: reasonCode, ...retained,
|
|
80
|
+
consent: consentHandoff({ installUrl, confirmationCode, expiresAt }, consentOrigin) };
|
|
81
|
+
}
|
|
82
|
+
function consentHandoff(value, consentOrigin) {
|
|
83
|
+
const { installUrl, confirmationCode, expiresAt } = record(value);
|
|
84
|
+
const url = typeof installUrl === 'string' && (0, url_1.trustedUrl)(installUrl);
|
|
85
|
+
if (!url || url.origin !== consentOrigin ||
|
|
86
|
+
!/^\/grant\/installed-account\/[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$/i.test(url.pathname) ||
|
|
87
|
+
typeof confirmationCode !== 'string' || !/^[A-F\d]{4}(?:-[A-F\d]{4}){4}$/.test(confirmationCode) ||
|
|
88
|
+
typeof expiresAt !== 'string' || !(Date.parse(expiresAt) > Date.now())) {
|
|
89
|
+
throw new Error('Invalid connection handoff');
|
|
90
|
+
}
|
|
91
|
+
return { installUrl: url.href, confirmationCode, expiresAt };
|
|
92
|
+
}
|
|
93
|
+
function createInstalledWorkflow(options) {
|
|
94
|
+
const invokeUrl = (0, url_1.trustedUrl)(options.invokeUrl);
|
|
95
|
+
const route = /^\/(?:api\/)?v1\/organizations\/([^/]+)\/app-installations\/([^/]+)\/capabilities\/([^/]+)\/invoke$/.exec(invokeUrl.pathname);
|
|
96
|
+
if (!route)
|
|
97
|
+
throw new Error('Expected an installed workflow invocation URL');
|
|
98
|
+
const org = encodeURIComponent(decodeURIComponent(route[1]));
|
|
99
|
+
const installation = encodeURIComponent(decodeURIComponent(route[2]));
|
|
100
|
+
const capabilityId = decodeURIComponent(route[3]);
|
|
101
|
+
const capability = encodeURIComponent(capabilityId);
|
|
102
|
+
const operationPath = (surface) => '/api/v1/organizations/' + org + '/app-installations/' + installation + '/' + surface + '/capabilities/' + capability + '/';
|
|
103
|
+
const consentOrigin = origin(options.consentOrigin);
|
|
104
|
+
const operationOrigin = origin(options.operationOrigin ?? invokeUrl.origin);
|
|
105
|
+
const { externalUserId, getAccessToken } = options;
|
|
106
|
+
if (externalUserId !== undefined && (!externalUserId || externalUserId.length > 500 ||
|
|
107
|
+
externalUserId.trim() !== externalUserId || /[\x00-\x1f\x7f,]/u.test(externalUserId))) {
|
|
108
|
+
throw new Error('Invalid external user ID');
|
|
109
|
+
}
|
|
110
|
+
async function request(url, init) {
|
|
111
|
+
const headers = new Headers(init.headers);
|
|
112
|
+
const token = await getAccessToken();
|
|
113
|
+
if (!token || /\s/.test(token))
|
|
114
|
+
throw new Error('Expected an access token');
|
|
115
|
+
headers.set('authorization', 'Bearer ' + token);
|
|
116
|
+
if (externalUserId !== undefined)
|
|
117
|
+
headers.set('x-process-external-user-id', externalUserId);
|
|
118
|
+
return fetch(url, { ...init, headers, cache: 'no-store', redirect: 'error' });
|
|
119
|
+
}
|
|
120
|
+
function operation(body, surface = 'rest') {
|
|
121
|
+
const { kind, operationId, statusUrl, streamUrl } = record(body);
|
|
122
|
+
if (kind !== 'operation' || typeof operationId !== 'string' || !/^[\w-]+$/.test(operationId) || typeof statusUrl !== 'string') {
|
|
123
|
+
throw new Error('Invalid installed workflow operation');
|
|
124
|
+
}
|
|
125
|
+
const url = (0, url_1.trustedUrl)(statusUrl);
|
|
126
|
+
const paths = ['operations/', 'setup-operations/'].map(collection => operationPath(surface) + collection + operationId);
|
|
127
|
+
if (url.origin !== operationOrigin || !paths.includes(url.pathname) || (streamUrl !== undefined && streamUrl !== url.href + '/events')) {
|
|
128
|
+
throw new Error('Invalid installed workflow operation URL');
|
|
129
|
+
}
|
|
130
|
+
async function read(endpoint, signal, headers) {
|
|
131
|
+
const response = await request(endpoint, { method: 'GET', signal, headers });
|
|
132
|
+
if (!response.ok)
|
|
133
|
+
throw new InstalledWorkflowError(response.status);
|
|
134
|
+
return response;
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
id: operationId, statusUrl: url.href,
|
|
138
|
+
status: async (signal) => (await read(url.href, signal)).json(),
|
|
139
|
+
...(typeof streamUrl === 'string' && { events: ({ lastEventId, signal } = {}) => read(streamUrl, signal, { accept: 'text/event-stream', ...(lastEventId && { 'last-event-id': lastEventId }) }) }),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
/** Provision the website user independently of provider consent or workflow execution.
|
|
144
|
+
* A supplied profile replaces this website's previous display profile. */
|
|
145
|
+
async ensureUser(profile, signal) {
|
|
146
|
+
if (!externalUserId)
|
|
147
|
+
throw new Error('A verified website user ID is required to provision an app user');
|
|
148
|
+
const response = await request(new URL('../../user-instance', invokeUrl).href, {
|
|
149
|
+
method: 'POST', signal, headers: { 'content-type': 'application/json' }, body: JSON.stringify({ profile }),
|
|
150
|
+
});
|
|
151
|
+
if (!response.ok)
|
|
152
|
+
throw new InstalledWorkflowError(response.status);
|
|
153
|
+
const { instanceId } = record(await response.json());
|
|
154
|
+
if (typeof instanceId !== 'string' || !/^[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$/i.test(instanceId)) {
|
|
155
|
+
throw new Error('Invalid installed app user response');
|
|
156
|
+
}
|
|
157
|
+
return { instanceId };
|
|
158
|
+
},
|
|
159
|
+
/** Manage the signed-in user's account for one credential requirement in this publication. */
|
|
160
|
+
connection(requirementId) {
|
|
161
|
+
if (typeof requirementId !== 'string' || !requirementId || requirementId.trim() !== requirementId || requirementId.length > 512) {
|
|
162
|
+
throw new Error('Invalid installed connection requirement');
|
|
163
|
+
}
|
|
164
|
+
const base = new URL('../../user-instance/connections/', invokeUrl);
|
|
165
|
+
const slot = { capabilityId, requirementId, surface: 'rest' };
|
|
166
|
+
async function post(action, signal, expectedRevision) {
|
|
167
|
+
if (action !== 'status' && (typeof expectedRevision !== 'number' || !Number.isSafeInteger(expectedRevision) || expectedRevision < 0)) {
|
|
168
|
+
throw new Error('Invalid installed connection revision');
|
|
169
|
+
}
|
|
170
|
+
const response = await request(new URL(action, base).href, {
|
|
171
|
+
method: 'POST', signal, headers: { 'content-type': 'application/json' },
|
|
172
|
+
body: JSON.stringify({ ...slot, expectedRevision }),
|
|
173
|
+
});
|
|
174
|
+
if (!response.ok)
|
|
175
|
+
throw new InstalledWorkflowError(response.status);
|
|
176
|
+
return response.json();
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
status: async (signal) => connectionStatus(await post('status', signal)),
|
|
180
|
+
consent: async (expectedRevision, signal) => consentHandoff(await post('consent', signal, expectedRevision), consentOrigin),
|
|
181
|
+
disconnect: async (expectedRevision, signal) => connectionStatus(await post('disconnect', signal, expectedRevision)),
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
/** Read an existing execution using the current installation and caller. */
|
|
185
|
+
operation(id) {
|
|
186
|
+
if (typeof id !== 'string' || !/^[\w-]{1,512}$/.test(id))
|
|
187
|
+
throw new Error('Invalid operation ID');
|
|
188
|
+
const statusUrl = operationOrigin + operationPath('rest') + 'operations/' + id;
|
|
189
|
+
return operation({ kind: 'operation', operationId: id, statusUrl });
|
|
190
|
+
},
|
|
191
|
+
/** Reattach from a server-side route after the browser returns from hosted consent. */
|
|
192
|
+
setupOperation(id, surface = 'rest') {
|
|
193
|
+
if (surface !== 'rest' && surface !== 'mcp')
|
|
194
|
+
throw new Error('Invalid setup operation surface');
|
|
195
|
+
if (!/^[a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12}$/i.test(id))
|
|
196
|
+
throw new Error('Invalid setup operation ID');
|
|
197
|
+
const statusUrl = operationOrigin + operationPath(surface) + 'setup-operations/' + id;
|
|
198
|
+
return {
|
|
199
|
+
...operation({ kind: 'operation', operationId: id, statusUrl, streamUrl: statusUrl + '/events' }, surface),
|
|
200
|
+
async consent(signal) {
|
|
201
|
+
const response = await request(statusUrl + '/consent', { method: 'POST', signal });
|
|
202
|
+
if (!response.ok)
|
|
203
|
+
throw new InstalledWorkflowError(response.status);
|
|
204
|
+
return consentHandoff(await response.json(), consentOrigin);
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
/** Decode an installed MCP tool result in the host. Only structuredContent is intended for the model. */
|
|
209
|
+
readMcpResult(reply) {
|
|
210
|
+
const tool = record(reply);
|
|
211
|
+
const body = record(tool.structuredContent);
|
|
212
|
+
if (tool.isError === true) {
|
|
213
|
+
if (body.code === 'CREDENTIAL_REQUIRED') {
|
|
214
|
+
const meta = tool._meta ? record(tool._meta) : {};
|
|
215
|
+
const handoff = meta['process.co/connection-setup'];
|
|
216
|
+
const { reasonCode, operation: pending, setupIssue } = record(body.details);
|
|
217
|
+
const launch = handoff === undefined ? undefined : consentHandoff(handoff, consentOrigin);
|
|
218
|
+
return connectionRequired({ error: { code: body.code, details: {
|
|
219
|
+
reasonCode, operation: pending, setupIssue, ...launch,
|
|
220
|
+
} } }, consentOrigin, value => operation(value, 'mcp'));
|
|
221
|
+
}
|
|
222
|
+
const code = invocationErrorCodes.find(candidate => candidate === body.code);
|
|
223
|
+
throw new InstalledWorkflowError(code === 'NOT_FOUND' ? 404 : code === 'SCOPE_DENIED' ? 403 : 502, code);
|
|
224
|
+
}
|
|
225
|
+
if (body.kind === 'operation')
|
|
226
|
+
return { state: 'accepted', operation: operation(body, 'mcp') };
|
|
227
|
+
return { state: 'completed', response: Response.json(body) };
|
|
228
|
+
},
|
|
229
|
+
async invoke(args, call = {}) {
|
|
230
|
+
const responseMode = call.responseMode ?? 'direct';
|
|
231
|
+
const response = await request(invokeUrl.href, {
|
|
232
|
+
method: 'POST', signal: call.signal, body: JSON.stringify(args),
|
|
233
|
+
headers: { 'content-type': 'application/json', 'x-process-response-mode': responseMode,
|
|
234
|
+
...(call.idempotencyKey !== undefined && { 'idempotency-key': call.idempotencyKey }) },
|
|
235
|
+
});
|
|
236
|
+
if (response.status === 428)
|
|
237
|
+
return connectionRequired(await response.json(), consentOrigin, operation);
|
|
238
|
+
if (!response.ok)
|
|
239
|
+
throw await invocationError(response);
|
|
240
|
+
const setupId = response.headers.get('x-process-setup-operation');
|
|
241
|
+
if (response.status === 202 && (responseMode === 'async' || setupId)) {
|
|
242
|
+
const observed = operation(await response.json());
|
|
243
|
+
if (setupId && setupId !== observed.id)
|
|
244
|
+
throw new Error('Invalid setup operation acknowledgement');
|
|
245
|
+
return { state: 'accepted', operation: observed };
|
|
246
|
+
}
|
|
247
|
+
return { state: 'completed', response };
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@process.co/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-next.2",
|
|
4
|
+
"description": "Call installed Process app methods with JavaScript or React.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./react": {
|
|
14
|
+
"types": "./dist/react/index.d.ts",
|
|
15
|
+
"default": "./dist/react/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./server": {
|
|
18
|
+
"types": "./dist/server-api.d.ts",
|
|
19
|
+
"default": "./dist/server-api.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"conformance",
|
|
25
|
+
"REACT.md"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"react": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"tag": "next"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/process-co/npm-installed-app-sdks.git"
|
|
45
|
+
}
|
|
46
|
+
}
|