@inixiative/integrations 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +50 -2
- package/package.json +56 -4
- package/src/catalog/declaredOperations.ts +9 -0
- package/src/catalog/eventLeaves.ts +5 -0
- package/src/catalog/familyProducts.ts +5 -0
- package/src/catalog/findProvider.ts +5 -0
- package/src/catalog/integrationEventResource.ts +20 -0
- package/src/catalog/integrationKeyedKinds.ts +4 -0
- package/src/catalog/isAccountSuppliedByHost.ts +6 -0
- package/src/catalog/isEventLeaf.ts +1 -0
- package/src/catalog/isFamilyManaged.ts +7 -0
- package/src/catalog/isUncalledLeaf.ts +6 -0
- package/src/catalog/operationLeaves.ts +6 -0
- package/src/catalog/providerEvent.ts +3 -0
- package/src/catalog/providerEventSchema.ts +3 -0
- package/src/catalog/providerFamily.ts +4 -0
- package/src/catalog/providerHasEvents.ts +8 -0
- package/src/catalog/providerModel.ts +12 -0
- package/src/catalog/providers.ts +54 -0
- package/src/contracts.ts +107 -0
- package/src/credentials/codexClientId.ts +2 -0
- package/src/credentials/codexTokenUrl.ts +1 -0
- package/src/credentials/consentReason.ts +4 -0
- package/src/credentials/consentReasonSchema.ts +21 -0
- package/src/credentials/credentialClient.ts +4 -0
- package/src/credentials/exchangeOauthCode.ts +61 -0
- package/src/credentials/fetchRenewalResponse.ts +41 -0
- package/src/credentials/mintProviderCredential.ts +68 -0
- package/src/credentials/providerExchangeTimeoutMs.ts +1 -0
- package/src/credentials/refreshProviderCredential.ts +172 -0
- package/src/credentials/renewalRequest.ts +1 -0
- package/src/credentials/subscriptionAccountId.ts +12 -0
- package/src/credentials/validateCredentialSecret.ts +18 -0
- package/src/index.ts +3 -0
- package/src/operations/buildProviderRequest.ts +54 -0
- package/src/operations/executeProvider.ts +26 -0
- package/src/operations/findOperationHandler.ts +6 -0
- package/src/operations/operationHandler.ts +8 -0
- package/src/operations/operationHandlers.ts +41 -0
- package/src/operations/operationRequestInput.ts +12 -0
- package/src/operations/providerRequestSpec.ts +1 -0
- package/src/providers/anthropic/anthropicProvider.ts +16 -0
- package/src/providers/archive/archiveCandidate.ts +1 -0
- package/src/providers/archive/archiveConfigurationSchema.ts +3 -0
- package/src/providers/archive/archiveProvider.ts +21 -0
- package/src/providers/archive/isLocalArchive.ts +5 -0
- package/src/providers/archive/localArchiveConfigurationSchema.ts +5 -0
- package/src/providers/archive/localArchiveSourceId.ts +8 -0
- package/src/providers/chorus/chorusProvider.ts +22 -0
- package/src/providers/claude/claudeProvider.ts +17 -0
- package/src/providers/codex/codexProvider.ts +17 -0
- package/src/providers/confluence/confluenceOperationHandler.ts +14 -0
- package/src/providers/confluence/confluenceProvider.ts +16 -0
- package/src/providers/discord/discordOperationHandler.ts +30 -0
- package/src/providers/discord/discordProvider.ts +22 -0
- package/src/providers/drive/driveOperationHandler.ts +21 -0
- package/src/providers/drive/driveProvider.ts +18 -0
- package/src/providers/foundry/foundryProvider.ts +33 -0
- package/src/providers/geminiApi/geminiApiProvider.ts +16 -0
- package/src/providers/github/auth/fetchGithubAppInstallation.ts +21 -0
- package/src/providers/github/auth/fetchGithubOrganizationRole.ts +27 -0
- package/src/providers/github/auth/fetchGithubUser.ts +11 -0
- package/src/providers/github/auth/fetchGithubUserInstallation.ts +21 -0
- package/src/providers/github/auth/githubAppClient.ts +4 -0
- package/src/providers/github/auth/githubAppInstallUrl.ts +20 -0
- package/src/providers/github/auth/githubAppJwt.ts +16 -0
- package/src/providers/github/auth/githubClientForApp.ts +11 -0
- package/src/providers/github/auth/githubInstallation.ts +4 -0
- package/src/providers/github/auth/githubInstallationAccountId.ts +4 -0
- package/src/providers/github/auth/githubInstallationFromAccountId.ts +4 -0
- package/src/providers/github/auth/githubInstallationSchema.ts +12 -0
- package/src/providers/github/auth/githubPermissionCovers.ts +14 -0
- package/src/providers/github/auth/githubPermissionScopes.ts +4 -0
- package/src/providers/github/auth/githubProviderForOwner.ts +6 -0
- package/src/providers/github/auth/githubUserAuthorizeUrl.ts +10 -0
- package/src/providers/github/events/githubEventRepositoryId.ts +10 -0
- package/src/providers/github/events/githubNotification.ts +4 -0
- package/src/providers/github/events/githubNotificationSchema.ts +9 -0
- package/src/providers/github/events/listGithubNotifications.ts +27 -0
- package/src/providers/github/events/normalizeGithubEvent.ts +200 -0
- package/src/providers/github/events/normalizeGithubNotification.ts +56 -0
- package/src/providers/github/events/verifyGithubSignature.ts +7 -0
- package/src/providers/github/fetchAllGithubPages.ts +12 -0
- package/src/providers/github/githubApiVersion.ts +1 -0
- package/src/providers/github/githubOrganizationProvider.ts +13 -0
- package/src/providers/github/githubPagination.ts +7 -0
- package/src/providers/github/githubProvider.ts +13 -0
- package/src/providers/github/githubRequestHeaders.ts +9 -0
- package/src/providers/github/githubUserProvider.ts +13 -0
- package/src/providers/github/iterateGithubPages.ts +12 -0
- package/src/providers/github/models/account/githubAccount.ts +17 -0
- package/src/providers/github/models/repository/githubOperationHandler.ts +36 -0
- package/src/providers/github/models/repository/githubRepository.ts +40 -0
- package/src/providers/github/models/repository/githubRepositoryIdSchema.ts +3 -0
- package/src/providers/github/models/repository/githubRepositoryMetadataSchema.ts +6 -0
- package/src/providers/github/models/repository/githubRepositoryName.ts +4 -0
- package/src/providers/github/models/repository/githubRepositoryNameSchema.ts +9 -0
- package/src/providers/github/models/repository/githubTreeEntryModeSchema.ts +3 -0
- package/src/providers/github/models/repository/githubTreeEntryTypeForMode.ts +14 -0
- package/src/providers/github/models/repository/githubTreeEntryTypeSchema.ts +3 -0
- package/src/providers/github/models/repository/listInstallationRepositories.ts +31 -0
- package/src/providers/github/models/repository/listUserRepositories.ts +36 -0
- package/src/providers/github/models/repository/readGithubObject.ts +85 -0
- package/src/providers/github/models/repository/readGithubRepositoryName.ts +4 -0
- package/src/providers/github/requestGithub.ts +18 -0
- package/src/providers/gmail/buildGmailMessageRequest.ts +22 -0
- package/src/providers/gmail/fetchGmailMessages.ts +81 -0
- package/src/providers/gmail/gmailOperationHandler.ts +12 -0
- package/src/providers/gmail/gmailProvider.ts +19 -0
- package/src/providers/google/auth/buildGoogleAuthorizationUrl.ts +33 -0
- package/src/providers/google/auth/verifyGoogleIdToken.ts +22 -0
- package/src/providers/google/googleProvider.ts +13 -0
- package/src/providers/jira/jiraOperationHandler.ts +14 -0
- package/src/providers/jira/jiraProvider.ts +16 -0
- package/src/providers/linear/auth/linearIntegrationSecret.ts +6 -0
- package/src/providers/linear/events/createLinearWebhook.ts +30 -0
- package/src/providers/linear/events/deleteLinearWebhook.ts +13 -0
- package/src/providers/linear/events/linearUserSchema.ts +3 -0
- package/src/providers/linear/events/linearWebhook.ts +3 -0
- package/src/providers/linear/events/linearWebhookPayload.ts +4 -0
- package/src/providers/linear/events/linearWebhookPayloadSchema.ts +13 -0
- package/src/providers/linear/events/listLinearWebhooks.ts +44 -0
- package/src/providers/linear/events/normalizeLinearEvent.ts +127 -0
- package/src/providers/linear/events/repairLinearWebhook.ts +17 -0
- package/src/providers/linear/events/verifyLinearSignature.ts +25 -0
- package/src/providers/linear/fetchAllLinearPages.ts +40 -0
- package/src/providers/linear/linearClient.ts +5 -0
- package/src/providers/linear/linearPagination.ts +13 -0
- package/src/providers/linear/linearProvider.ts +12 -0
- package/src/providers/linear/linearRequestInput.ts +1 -0
- package/src/providers/linear/models/issue/linearIssue.ts +11 -0
- package/src/providers/linear/models/issue/linearOperationHandler.ts +30 -0
- package/src/providers/linear/models/team/fetchLinearWorkspace.ts +30 -0
- package/src/providers/linear/models/team/linearTeam.ts +18 -0
- package/src/providers/linear/requestLinear.ts +51 -0
- package/src/providers/muse/museProvider.ts +17 -0
- package/src/providers/notion/notionOperationHandler.ts +32 -0
- package/src/providers/notion/notionProvider.ts +22 -0
- package/src/providers/openai/openaiProvider.ts +16 -0
- package/src/providers/oracle/oracleProvider.ts +10 -0
- package/src/providers/railway/railwayProvider.ts +9 -0
- package/src/providers/render/renderProvider.ts +9 -0
- package/src/providers/slack/auth/fetchSlackIdentity.ts +10 -0
- package/src/providers/slack/events/normalizeSlackEvent.ts +64 -0
- package/src/providers/slack/events/slackAccountIdOf.ts +1 -0
- package/src/providers/slack/events/slackEventEnvelope.ts +4 -0
- package/src/providers/slack/events/slackEventEnvelopeSchema.ts +16 -0
- package/src/providers/slack/events/slackTeamPrefix.ts +1 -0
- package/src/providers/slack/events/slackTokensRevokedSchema.ts +13 -0
- package/src/providers/slack/events/slackUserOfAccountId.ts +2 -0
- package/src/providers/slack/events/verifySlackSignature.ts +17 -0
- package/src/providers/slack/fetchAllSlackPages.ts +12 -0
- package/src/providers/slack/models/account/slackAccount.ts +13 -0
- package/src/providers/slack/models/channel/listSlackChannels.ts +26 -0
- package/src/providers/slack/models/channel/slackChannel.ts +31 -0
- package/src/providers/slack/models/channel/slackOperationHandler.ts +30 -0
- package/src/providers/slack/requestSlack.ts +22 -0
- package/src/providers/slack/slackPagination.ts +12 -0
- package/src/providers/slack/slackProvider.ts +12 -0
- package/src/providers/slack/slackProviderError.ts +12 -0
- package/src/providers/teams/teamsOperationHandler.ts +24 -0
- package/src/providers/teams/teamsProvider.ts +17 -0
- package/src/resources/integrationConfigurationSchemas.ts +11 -0
- package/src/resources/perProvider.ts +20 -0
- package/src/resources/resourceExternalIdSchema.ts +4 -0
- package/src/resources/resourceExternalIdSchemas.ts +18 -0
- package/src/resources/resourceMetadataSchema.ts +4 -0
- package/src/resources/resourceMetadataSchemas.ts +9 -0
- package/src/utils/capacityWindowSchema.ts +7 -0
- package/src/utils/headerNumber.ts +6 -0
- package/src/utils/jwtClaims.ts +14 -0
- package/src/utils/matchesHmacSha256.ts +11 -0
- package/src/utils/objectSchema.ts +3 -0
- package/src/utils/pagination/applyCaps.ts +18 -0
- package/src/utils/pagination/byCursorKeys.ts +7 -0
- package/src/utils/pagination/byCursorOptions.ts +7 -0
- package/src/utils/pagination/byPageKeys.ts +7 -0
- package/src/utils/pagination/byPageOptions.ts +8 -0
- package/src/utils/pagination/collectPages.ts +27 -0
- package/src/utils/pagination/compactRequest.ts +4 -0
- package/src/utils/pagination/fetchAllByCursor.ts +6 -0
- package/src/utils/pagination/fetchAllByPage.ts +6 -0
- package/src/utils/pagination/fetchAllRequestPages.ts +16 -0
- package/src/utils/pagination/incompletePageWalkError.ts +8 -0
- package/src/utils/pagination/isAnotherPage.ts +20 -0
- package/src/utils/pagination/iterateByCursor.ts +49 -0
- package/src/utils/pagination/iterateByPage.ts +59 -0
- package/src/utils/pagination/iterateProviderPages.ts +27 -0
- package/src/utils/pagination/iterateRequestPages.ts +18 -0
- package/src/utils/pagination/pageWalkError.ts +6 -0
- package/src/utils/pagination/paginationOptions.ts +9 -0
- package/src/utils/pagination/providerPagesOptions.ts +8 -0
- package/src/utils/pagination/providerPagination.ts +6 -0
- package/src/utils/pagination/readPage.ts +29 -0
- package/src/utils/pagination/readPath.ts +12 -0
- package/src/utils/pagination/repeatedCursorError.ts +10 -0
- package/src/utils/providerClient.ts +3 -0
- package/src/utils/providerCooldown.ts +23 -0
- package/src/utils/providerCooldownState.ts +4 -0
- package/src/utils/providerError.ts +10 -0
- package/src/utils/providerReading.ts +4 -0
- package/src/utils/providerReadingSchema.ts +8 -0
- package/src/utils/providerRequest.ts +22 -0
- package/src/utils/providerRequestInput.ts +7 -0
- package/src/utils/providerResponseLimit.ts +1 -0
- package/src/utils/providerSend.ts +1 -0
- package/src/utils/readProviderJson.ts +38 -0
- package/src/utils/readProviderLimits.ts +66 -0
- package/src/utils/readRateLimit.ts +29 -0
- package/src/utils/requestProviderJson.ts +13 -0
- package/src/utils/responseBudget.ts +1 -0
- package/src/utils/select.ts +6 -0
- package/src/utils/sendProviderRequest.ts +33 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { providers } from '../catalog/providers';
|
|
4
|
+
import { type CredentialSecret, credentialSecretSchema, type OAuthClient } from '../contracts';
|
|
5
|
+
import { jwtClaims } from '../utils/jwtClaims';
|
|
6
|
+
import { ProviderError } from '../utils/providerError';
|
|
7
|
+
import { CODEX_TOKEN_URL } from './codexTokenUrl';
|
|
8
|
+
import type { CredentialClient } from './credentialClient';
|
|
9
|
+
import { fetchRenewalResponse } from './fetchRenewalResponse';
|
|
10
|
+
import { mintProviderCredential } from './mintProviderCredential';
|
|
11
|
+
import type { RenewalRequest } from './renewalRequest';
|
|
12
|
+
|
|
13
|
+
const secondsUntilJwtExpiry = (token: string) => {
|
|
14
|
+
const exp = z.number().int().positive().safeParse(jwtClaims(token)?.exp);
|
|
15
|
+
if (!exp.success) throw new ProviderError('renewal_outcome_unknown', 409);
|
|
16
|
+
return z
|
|
17
|
+
.number()
|
|
18
|
+
.int()
|
|
19
|
+
.positive()
|
|
20
|
+
.max(31536000)
|
|
21
|
+
.parse(exp.data - Math.floor(Date.now() / 1000));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const refreshRequest = (
|
|
25
|
+
provider: string,
|
|
26
|
+
secret: CredentialSecret,
|
|
27
|
+
client?: OAuthClient,
|
|
28
|
+
): RenewalRequest => {
|
|
29
|
+
if (!secret.oauth) throw new ProviderError('renewal_unsupported', 409);
|
|
30
|
+
const { refreshToken, tenantId } = secret.oauth;
|
|
31
|
+
const clientId = client?.clientId ?? secret.oauth.clientId;
|
|
32
|
+
const clientSecret = client?.clientSecret ?? secret.oauth.clientSecret;
|
|
33
|
+
const publicClient = provider === 'codex';
|
|
34
|
+
if (!clientId || (!clientSecret && !publicClient))
|
|
35
|
+
throw new ProviderError('renewal_unsupported', 409);
|
|
36
|
+
const familyOAuth = providers.find((definition) => definition.key === provider)?.oauth;
|
|
37
|
+
const fields = {
|
|
38
|
+
grant_type: 'refresh_token',
|
|
39
|
+
refresh_token: refreshToken,
|
|
40
|
+
client_id: clientId,
|
|
41
|
+
client_secret: clientSecret ?? '',
|
|
42
|
+
};
|
|
43
|
+
const headers = new Headers({
|
|
44
|
+
Accept: 'application/json',
|
|
45
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
46
|
+
});
|
|
47
|
+
switch (provider) {
|
|
48
|
+
case 'slack':
|
|
49
|
+
return {
|
|
50
|
+
url: 'https://slack.com/api/oauth.v2.access',
|
|
51
|
+
headers,
|
|
52
|
+
body: new URLSearchParams(fields).toString(),
|
|
53
|
+
};
|
|
54
|
+
case 'githubUser':
|
|
55
|
+
return {
|
|
56
|
+
url: z
|
|
57
|
+
.url()
|
|
58
|
+
.parse(providers.find((definition) => definition.key === 'github')?.oauth?.tokenUrl),
|
|
59
|
+
headers,
|
|
60
|
+
body: new URLSearchParams(fields).toString(),
|
|
61
|
+
};
|
|
62
|
+
case 'linear':
|
|
63
|
+
return {
|
|
64
|
+
url: 'https://api.linear.app/oauth/token',
|
|
65
|
+
headers,
|
|
66
|
+
body: new URLSearchParams(fields).toString(),
|
|
67
|
+
};
|
|
68
|
+
case 'google':
|
|
69
|
+
return {
|
|
70
|
+
url: z.url().parse(familyOAuth?.tokenUrl),
|
|
71
|
+
headers,
|
|
72
|
+
body: new URLSearchParams(fields).toString(),
|
|
73
|
+
};
|
|
74
|
+
case 'teams':
|
|
75
|
+
return {
|
|
76
|
+
url: `https://login.microsoftonline.com/${z.uuid().parse(tenantId)}/oauth2/v2.0/token`,
|
|
77
|
+
headers,
|
|
78
|
+
body: new URLSearchParams(fields).toString(),
|
|
79
|
+
};
|
|
80
|
+
case 'jira':
|
|
81
|
+
case 'confluence':
|
|
82
|
+
headers.set('Content-Type', 'application/json');
|
|
83
|
+
return {
|
|
84
|
+
url: 'https://auth.atlassian.com/oauth/token',
|
|
85
|
+
headers,
|
|
86
|
+
body: JSON.stringify(fields),
|
|
87
|
+
};
|
|
88
|
+
case 'notion':
|
|
89
|
+
headers.set('Content-Type', 'application/json');
|
|
90
|
+
headers.set('Notion-Version', '2026-03-11');
|
|
91
|
+
headers.set(
|
|
92
|
+
'Authorization',
|
|
93
|
+
`Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
|
|
94
|
+
);
|
|
95
|
+
return {
|
|
96
|
+
url: 'https://api.notion.com/v1/oauth/token',
|
|
97
|
+
headers,
|
|
98
|
+
body: JSON.stringify({ grant_type: 'refresh_token', refresh_token: refreshToken }),
|
|
99
|
+
};
|
|
100
|
+
case 'codex':
|
|
101
|
+
headers.set('Content-Type', 'application/json');
|
|
102
|
+
return {
|
|
103
|
+
url: CODEX_TOKEN_URL,
|
|
104
|
+
headers,
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
client_id: clientId,
|
|
107
|
+
grant_type: 'refresh_token',
|
|
108
|
+
refresh_token: refreshToken,
|
|
109
|
+
scope: 'openid profile email',
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
default:
|
|
113
|
+
throw new ProviderError('renewal_unsupported', 409);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const refreshedSecret = (
|
|
118
|
+
provider: string,
|
|
119
|
+
secret: CredentialSecret,
|
|
120
|
+
data: Record<string, unknown>,
|
|
121
|
+
) => {
|
|
122
|
+
if (data.error || (provider === 'slack' && data.ok !== true))
|
|
123
|
+
throw new ProviderError(
|
|
124
|
+
data.error === 'ratelimited' ? 'renewal_temporarily_unavailable' : 'renewal_rejected',
|
|
125
|
+
data.error === 'ratelimited' ? 503 : 409,
|
|
126
|
+
);
|
|
127
|
+
const refreshToken = secret.oauth?.refreshToken;
|
|
128
|
+
const refreshed = credentialSecretSchema.safeParse({
|
|
129
|
+
token: data.access_token,
|
|
130
|
+
oauth: {
|
|
131
|
+
...secret.oauth,
|
|
132
|
+
refreshToken: data.refresh_token ?? refreshToken,
|
|
133
|
+
...(typeof data.id_token === 'string' ? { idToken: data.id_token } : {}),
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
if (!refreshed.success) throw new ProviderError('renewal_outcome_unknown', 409);
|
|
137
|
+
if (
|
|
138
|
+
['slack', 'linear', 'jira', 'confluence', 'notion', 'codex', 'githubUser'].includes(provider) &&
|
|
139
|
+
!data.refresh_token
|
|
140
|
+
)
|
|
141
|
+
throw new ProviderError('renewal_outcome_unknown', 409);
|
|
142
|
+
const expiresIn =
|
|
143
|
+
provider === 'notion' && data.expires_in === undefined
|
|
144
|
+
? null
|
|
145
|
+
: provider === 'codex' && data.expires_in === undefined
|
|
146
|
+
? secondsUntilJwtExpiry(refreshed.data.token)
|
|
147
|
+
: z.number().int().positive().max(31536000).safeParse(data.expires_in).data;
|
|
148
|
+
if (expiresIn === undefined) throw new ProviderError('renewal_outcome_unknown', 409);
|
|
149
|
+
return { secret: refreshed.data, expiresIn };
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const refreshProviderCredential = async (
|
|
153
|
+
client: CredentialClient,
|
|
154
|
+
{ provider, secret: value }: { provider: string; secret: unknown },
|
|
155
|
+
) => {
|
|
156
|
+
const secret = credentialSecretSchema.parse(value);
|
|
157
|
+
if (secret.clientCredentials)
|
|
158
|
+
return await mintProviderCredential(client, {
|
|
159
|
+
provider,
|
|
160
|
+
clientCredentials: secret.clientCredentials,
|
|
161
|
+
});
|
|
162
|
+
const data = await fetchRenewalResponse(
|
|
163
|
+
client.send ?? fetch,
|
|
164
|
+
refreshRequest(provider, secret, client.app),
|
|
165
|
+
);
|
|
166
|
+
try {
|
|
167
|
+
return refreshedSecret(provider, secret, data);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error instanceof ProviderError) throw error;
|
|
170
|
+
throw new ProviderError('renewal_outcome_unknown', 409);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type RenewalRequest = { url: string; headers: Headers; body: string };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SubscriptionRuntime } from '../contracts';
|
|
2
|
+
import { jwtClaims } from '../utils/jwtClaims';
|
|
3
|
+
|
|
4
|
+
export const subscriptionAccountId = (runtime: SubscriptionRuntime, accessToken: string) => {
|
|
5
|
+
if (runtime !== 'codex') return null;
|
|
6
|
+
const auth = jwtClaims(accessToken)?.['https://api.openai.com/auth'];
|
|
7
|
+
const accountId =
|
|
8
|
+
auth && typeof auth === 'object' && !Array.isArray(auth)
|
|
9
|
+
? (auth as Record<string, unknown>).chatgpt_account_id
|
|
10
|
+
: undefined;
|
|
11
|
+
return typeof accountId === 'string' ? accountId : null;
|
|
12
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { credentialSecretSchema } from '../contracts';
|
|
2
|
+
import { ProviderError } from '../utils/providerError';
|
|
3
|
+
|
|
4
|
+
export const validateCredentialSecret = (method: string, secret: unknown, provider: string) => {
|
|
5
|
+
const parsed = credentialSecretSchema.parse(secret);
|
|
6
|
+
if ((method === 'oauth2') !== Boolean(parsed.oauth))
|
|
7
|
+
throw new ProviderError('Credential method and OAuth bundle must match', 400);
|
|
8
|
+
if ((method === 'clientCredentials') !== Boolean(parsed.clientCredentials))
|
|
9
|
+
throw new ProviderError('Credential method and client credentials must match', 400);
|
|
10
|
+
if (
|
|
11
|
+
method === 'oauth2' &&
|
|
12
|
+
(!parsed.oauth?.clientId || (!parsed.oauth.clientSecret && provider !== 'codex'))
|
|
13
|
+
)
|
|
14
|
+
throw new ProviderError('OAuth requires the application client ID and secret', 400);
|
|
15
|
+
if (method === 'oauth2' && provider === 'teams' && !parsed.oauth?.tenantId)
|
|
16
|
+
throw new ProviderError('Microsoft OAuth requires a tenant UUID', 400);
|
|
17
|
+
return parsed;
|
|
18
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { isUncalledLeaf } from '../catalog/isUncalledLeaf';
|
|
3
|
+
import { providers } from '../catalog/providers';
|
|
4
|
+
import type { ProviderContext } from '../contracts';
|
|
5
|
+
import { credentialSecretSchema } from '../contracts';
|
|
6
|
+
import { integrationConfigurationSchemas } from '../resources/integrationConfigurationSchemas';
|
|
7
|
+
import { resourceExternalIdSchemas } from '../resources/resourceExternalIdSchemas';
|
|
8
|
+
import { resourceMetadataSchemas } from '../resources/resourceMetadataSchemas';
|
|
9
|
+
import { ProviderError } from '../utils/providerError';
|
|
10
|
+
import { findOperationHandler } from './findOperationHandler';
|
|
11
|
+
|
|
12
|
+
export const buildProviderRequest = (
|
|
13
|
+
context: ProviderContext,
|
|
14
|
+
signal: AbortSignal = AbortSignal.timeout(15000),
|
|
15
|
+
): Request => {
|
|
16
|
+
const definition = providers.find((item) => item.key === context.provider);
|
|
17
|
+
const operation = definition?.operations.find((item) => item.key === context.operation);
|
|
18
|
+
if (!definition || !operation || operation.resourceKind !== context.resource.kind)
|
|
19
|
+
throw new ProviderError('unsupported_operation', 400);
|
|
20
|
+
if (isUncalledLeaf(operation.key)) throw new ProviderError('unsupported_integration', 400);
|
|
21
|
+
if (!definition.authMethods.includes(context.method))
|
|
22
|
+
throw new ProviderError('unsupported_authentication', 400);
|
|
23
|
+
const configuration = integrationConfigurationSchemas[definition.key].parse(
|
|
24
|
+
context.configuration,
|
|
25
|
+
);
|
|
26
|
+
const metadata = resourceMetadataSchemas[definition.key].parse(context.resource.metadata);
|
|
27
|
+
const externalId = resourceExternalIdSchemas[definition.key].parse(context.resource.externalId);
|
|
28
|
+
const id = encodeURIComponent(externalId);
|
|
29
|
+
const limit = z.number().int().min(1).max(50).parse(context.limit);
|
|
30
|
+
const secret = credentialSecretSchema.parse(context.secret);
|
|
31
|
+
const headers = new Headers({
|
|
32
|
+
Accept: 'application/json',
|
|
33
|
+
Authorization: context.method === 'apiKey' ? secret.token : `Bearer ${secret.token}`,
|
|
34
|
+
});
|
|
35
|
+
const client = findOperationHandler(definition.key);
|
|
36
|
+
if (!client) throw new ProviderError('unsupported_integration', 400);
|
|
37
|
+
const { url, body } = client.request({
|
|
38
|
+
context,
|
|
39
|
+
configuration: configuration as Record<string, unknown>,
|
|
40
|
+
metadata: metadata as Record<string, unknown>,
|
|
41
|
+
externalId,
|
|
42
|
+
id,
|
|
43
|
+
limit,
|
|
44
|
+
token: secret.token,
|
|
45
|
+
headers,
|
|
46
|
+
});
|
|
47
|
+
return new Request(url.toString(), {
|
|
48
|
+
method: body ? 'POST' : 'GET',
|
|
49
|
+
headers,
|
|
50
|
+
body,
|
|
51
|
+
redirect: 'error',
|
|
52
|
+
signal,
|
|
53
|
+
});
|
|
54
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ProviderContext } from '../contracts';
|
|
2
|
+
import { fetchGmailMessages } from '../providers/gmail/fetchGmailMessages';
|
|
3
|
+
import { ProviderError } from '../utils/providerError';
|
|
4
|
+
import type { ProviderSend } from '../utils/providerSend';
|
|
5
|
+
import { requestProviderJson } from '../utils/requestProviderJson';
|
|
6
|
+
import { buildProviderRequest } from './buildProviderRequest';
|
|
7
|
+
import { findOperationHandler } from './findOperationHandler';
|
|
8
|
+
|
|
9
|
+
export const executeProvider = async (
|
|
10
|
+
{ send = fetch }: { send?: ProviderSend },
|
|
11
|
+
context: ProviderContext,
|
|
12
|
+
{ signal = AbortSignal.timeout(15000) }: { signal?: AbortSignal } = {},
|
|
13
|
+
): Promise<unknown> => {
|
|
14
|
+
const request = buildProviderRequest(context, signal);
|
|
15
|
+
const handler = findOperationHandler(context.provider);
|
|
16
|
+
if (!handler) throw new ProviderError('unsupported_integration', 400);
|
|
17
|
+
try {
|
|
18
|
+
// why: Gmail lists a label and then fetches each message under one byte budget.
|
|
19
|
+
if (context.provider === 'gmail')
|
|
20
|
+
return await fetchGmailMessages({ send }, { listRequest: request, context });
|
|
21
|
+
return handler.parse(await requestProviderJson(send, request), context);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error instanceof ProviderError) throw error;
|
|
24
|
+
throw new ProviderError('provider_invalid_response');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Provider } from '../contracts';
|
|
2
|
+
import type { OperationHandler } from './operationHandler';
|
|
3
|
+
import { operationHandlers } from './operationHandlers';
|
|
4
|
+
|
|
5
|
+
export const findOperationHandler = (key: string): OperationHandler | null =>
|
|
6
|
+
operationHandlers[key as Provider] ?? null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ProviderContext } from '../contracts';
|
|
2
|
+
import type { OperationRequestInput } from './operationRequestInput';
|
|
3
|
+
import type { ProviderRequestSpec } from './providerRequestSpec';
|
|
4
|
+
|
|
5
|
+
export type OperationHandler = {
|
|
6
|
+
request(input: OperationRequestInput): ProviderRequestSpec;
|
|
7
|
+
parse(body: unknown, context: ProviderContext): unknown;
|
|
8
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Provider } from '../contracts';
|
|
2
|
+
import { confluenceOperationHandler } from '../providers/confluence/confluenceOperationHandler';
|
|
3
|
+
import { discordOperationHandler } from '../providers/discord/discordOperationHandler';
|
|
4
|
+
import { driveOperationHandler } from '../providers/drive/driveOperationHandler';
|
|
5
|
+
import { githubOperationHandler } from '../providers/github/models/repository/githubOperationHandler';
|
|
6
|
+
import { gmailOperationHandler } from '../providers/gmail/gmailOperationHandler';
|
|
7
|
+
import { jiraOperationHandler } from '../providers/jira/jiraOperationHandler';
|
|
8
|
+
import { linearOperationHandler } from '../providers/linear/models/issue/linearOperationHandler';
|
|
9
|
+
import { notionOperationHandler } from '../providers/notion/notionOperationHandler';
|
|
10
|
+
import { slackOperationHandler } from '../providers/slack/models/channel/slackOperationHandler';
|
|
11
|
+
import { teamsOperationHandler } from '../providers/teams/teamsOperationHandler';
|
|
12
|
+
import type { OperationHandler } from './operationHandler';
|
|
13
|
+
|
|
14
|
+
// why: null marks a provider reached outside the generic HTTP path (own client, Signet, family, or admitted elsewhere).
|
|
15
|
+
export const operationHandlers: Record<Provider, OperationHandler | null> = {
|
|
16
|
+
slack: slackOperationHandler,
|
|
17
|
+
discord: discordOperationHandler,
|
|
18
|
+
teams: teamsOperationHandler,
|
|
19
|
+
linear: linearOperationHandler,
|
|
20
|
+
notion: notionOperationHandler,
|
|
21
|
+
jira: jiraOperationHandler,
|
|
22
|
+
drive: driveOperationHandler,
|
|
23
|
+
confluence: confluenceOperationHandler,
|
|
24
|
+
github: null,
|
|
25
|
+
githubOrganization: githubOperationHandler,
|
|
26
|
+
githubUser: githubOperationHandler,
|
|
27
|
+
google: null,
|
|
28
|
+
anthropic: null,
|
|
29
|
+
openai: null,
|
|
30
|
+
geminiApi: null,
|
|
31
|
+
archive: null,
|
|
32
|
+
gmail: gmailOperationHandler,
|
|
33
|
+
foundry: null,
|
|
34
|
+
oracle: null,
|
|
35
|
+
chorus: null,
|
|
36
|
+
railway: null,
|
|
37
|
+
render: null,
|
|
38
|
+
claude: null,
|
|
39
|
+
codex: null,
|
|
40
|
+
muse: null,
|
|
41
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProviderContext } from '../contracts';
|
|
2
|
+
|
|
3
|
+
export type OperationRequestInput = {
|
|
4
|
+
context: ProviderContext;
|
|
5
|
+
configuration: Record<string, unknown>;
|
|
6
|
+
metadata: Record<string, unknown>;
|
|
7
|
+
externalId: string;
|
|
8
|
+
id: string;
|
|
9
|
+
limit: number;
|
|
10
|
+
token: string;
|
|
11
|
+
headers: Headers;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ProviderRequestSpec = { url: URL; body?: string };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const anthropicProvider: ProviderDefinition = {
|
|
4
|
+
key: 'anthropic',
|
|
5
|
+
name: 'Anthropic',
|
|
6
|
+
documentationUrl: 'https://platform.claude.com/docs/en/api/messages',
|
|
7
|
+
authMethods: ['apiKey', 'bearer'],
|
|
8
|
+
operations: [
|
|
9
|
+
{
|
|
10
|
+
key: 'inference.create',
|
|
11
|
+
name: 'Run inference',
|
|
12
|
+
resourceKind: 'model',
|
|
13
|
+
requiredScopes: ['inference'],
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ArchiveCandidate = { provider: string | null; configuration?: unknown };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
archiveLibraryResourceKind,
|
|
3
|
+
archiveOperationNames,
|
|
4
|
+
archiveOperationSchema,
|
|
5
|
+
} from '@inixiative/signet/contracts';
|
|
6
|
+
import { declaredOperations } from '../../catalog/declaredOperations';
|
|
7
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
8
|
+
|
|
9
|
+
export const archiveProvider: ProviderDefinition = {
|
|
10
|
+
key: 'archive',
|
|
11
|
+
name: 'Archive',
|
|
12
|
+
documentationUrl: 'https://github.com/inixiative/archive',
|
|
13
|
+
authMethods: ['bearer'],
|
|
14
|
+
inbound: true,
|
|
15
|
+
operations: declaredOperations(
|
|
16
|
+
archiveOperationNames,
|
|
17
|
+
archiveLibraryResourceKind,
|
|
18
|
+
[],
|
|
19
|
+
archiveOperationSchema.options,
|
|
20
|
+
),
|
|
21
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ArchiveCandidate } from './archiveCandidate';
|
|
2
|
+
import { localArchiveConfigurationSchema } from './localArchiveConfigurationSchema';
|
|
3
|
+
|
|
4
|
+
export const localArchiveSourceId = (integration: ArchiveCandidate) =>
|
|
5
|
+
integration.provider === 'archive'
|
|
6
|
+
? (localArchiveConfigurationSchema.safeParse(integration.configuration).data?.local.sourceId ??
|
|
7
|
+
null)
|
|
8
|
+
: null;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chorusOperationNames,
|
|
3
|
+
chorusOperationSchema,
|
|
4
|
+
chorusResourceKind,
|
|
5
|
+
} from '@inixiative/signet/contracts';
|
|
6
|
+
import { declaredOperations } from '../../catalog/declaredOperations';
|
|
7
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
8
|
+
|
|
9
|
+
export const chorusProvider: ProviderDefinition = {
|
|
10
|
+
key: 'chorus',
|
|
11
|
+
name: 'Chorus',
|
|
12
|
+
documentationUrl: 'https://github.com/inixiative/chorus',
|
|
13
|
+
authMethods: [],
|
|
14
|
+
inbound: true,
|
|
15
|
+
delivery: 'installationSocket',
|
|
16
|
+
operations: declaredOperations(
|
|
17
|
+
chorusOperationNames,
|
|
18
|
+
chorusResourceKind,
|
|
19
|
+
[],
|
|
20
|
+
chorusOperationSchema.options,
|
|
21
|
+
),
|
|
22
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const claudeProvider: ProviderDefinition = {
|
|
4
|
+
key: 'claude',
|
|
5
|
+
name: 'Claude subscription',
|
|
6
|
+
documentationUrl: 'https://code.claude.com/docs/en/authentication',
|
|
7
|
+
authMethods: ['bearer'],
|
|
8
|
+
subscription: true,
|
|
9
|
+
operations: [
|
|
10
|
+
{
|
|
11
|
+
key: 'subscription.use',
|
|
12
|
+
name: 'Run Claude Code on this login',
|
|
13
|
+
resourceKind: 'subscription',
|
|
14
|
+
requiredScopes: ['user:inference'],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const codexProvider: ProviderDefinition = {
|
|
4
|
+
key: 'codex',
|
|
5
|
+
name: 'ChatGPT subscription',
|
|
6
|
+
documentationUrl: 'https://developers.openai.com/codex/auth',
|
|
7
|
+
authMethods: ['oauth2'],
|
|
8
|
+
subscription: true,
|
|
9
|
+
operations: [
|
|
10
|
+
{
|
|
11
|
+
key: 'subscription.use',
|
|
12
|
+
name: 'Run Codex on this login',
|
|
13
|
+
resourceKind: 'subscription',
|
|
14
|
+
requiredScopes: ['openid', 'offline_access'],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OperationHandler } from '../../operations/operationHandler';
|
|
2
|
+
import { objectSchema } from '../../utils/objectSchema';
|
|
3
|
+
import { select } from '../../utils/select';
|
|
4
|
+
|
|
5
|
+
export const confluenceOperationHandler: OperationHandler = {
|
|
6
|
+
request: ({ configuration, id }) => {
|
|
7
|
+
const url = new URL(
|
|
8
|
+
`https://api.atlassian.com/ex/confluence/${'cloudId' in configuration ? configuration.cloudId : ''}/wiki/api/v2/pages/${id}`,
|
|
9
|
+
);
|
|
10
|
+
url.searchParams.set('body-format', 'storage');
|
|
11
|
+
return { url };
|
|
12
|
+
},
|
|
13
|
+
parse: (body) => select(objectSchema.parse(body), ['id', 'title', 'status', 'body', 'version']),
|
|
14
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const confluenceProvider: ProviderDefinition = {
|
|
4
|
+
key: 'confluence',
|
|
5
|
+
name: 'Confluence',
|
|
6
|
+
documentationUrl: 'https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/',
|
|
7
|
+
authMethods: ['bearer', 'oauth2'],
|
|
8
|
+
operations: [
|
|
9
|
+
{
|
|
10
|
+
key: 'pages.read',
|
|
11
|
+
name: 'Read a page',
|
|
12
|
+
resourceKind: 'page',
|
|
13
|
+
requiredScopes: ['read:page:confluence'],
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { OperationHandler } from '../../operations/operationHandler';
|
|
3
|
+
import { objectSchema } from '../../utils/objectSchema';
|
|
4
|
+
import { select } from '../../utils/select';
|
|
5
|
+
|
|
6
|
+
export const discordOperationHandler: OperationHandler = {
|
|
7
|
+
request: ({ context, id, limit, headers, token }) => {
|
|
8
|
+
headers.set('Authorization', `Bot ${token}`);
|
|
9
|
+
const url = new URL(
|
|
10
|
+
`https://discord.com/api/v10/channels/${id}${context.operation === 'messages.read' ? '/messages' : ''}`,
|
|
11
|
+
);
|
|
12
|
+
if (context.operation === 'messages.read') url.searchParams.set('limit', String(limit));
|
|
13
|
+
return { url };
|
|
14
|
+
},
|
|
15
|
+
parse: (body, context) => {
|
|
16
|
+
if (context.operation !== 'messages.read')
|
|
17
|
+
return select(objectSchema.parse(body), ['id', 'name', 'type', 'guild_id', 'topic']);
|
|
18
|
+
const messages = z.array(z.unknown()).parse(body);
|
|
19
|
+
return {
|
|
20
|
+
messages: messages.map((item) => {
|
|
21
|
+
const message = select(item, ['id', 'content', 'timestamp', 'type', 'author']);
|
|
22
|
+
return {
|
|
23
|
+
...message,
|
|
24
|
+
author: select(message.author ?? {}, ['id', 'username', 'global_name']),
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
hasMore: messages.length >= context.limit,
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const discordProvider: ProviderDefinition = {
|
|
4
|
+
key: 'discord',
|
|
5
|
+
name: 'Discord',
|
|
6
|
+
documentationUrl: 'https://discord.com/developers/docs/resources/message#get-channel-messages',
|
|
7
|
+
authMethods: ['apiKey'],
|
|
8
|
+
operations: [
|
|
9
|
+
{
|
|
10
|
+
key: 'channels.read',
|
|
11
|
+
name: 'Read channel details',
|
|
12
|
+
resourceKind: 'channel',
|
|
13
|
+
requiredScopes: ['VIEW_CHANNEL'],
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
key: 'messages.read',
|
|
17
|
+
name: 'Read channel messages',
|
|
18
|
+
resourceKind: 'channel',
|
|
19
|
+
requiredScopes: ['VIEW_CHANNEL and READ_MESSAGE_HISTORY'],
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { OperationHandler } from '../../operations/operationHandler';
|
|
2
|
+
import { objectSchema } from '../../utils/objectSchema';
|
|
3
|
+
import { select } from '../../utils/select';
|
|
4
|
+
|
|
5
|
+
export const driveOperationHandler: OperationHandler = {
|
|
6
|
+
request: ({ id }) => {
|
|
7
|
+
const url = new URL(`https://www.googleapis.com/drive/v3/files/${id}`);
|
|
8
|
+
url.searchParams.set('fields', 'id,name,mimeType,description,modifiedTime,size');
|
|
9
|
+
url.searchParams.set('supportsAllDrives', 'true');
|
|
10
|
+
return { url };
|
|
11
|
+
},
|
|
12
|
+
parse: (body) =>
|
|
13
|
+
select(objectSchema.parse(body), [
|
|
14
|
+
'id',
|
|
15
|
+
'name',
|
|
16
|
+
'mimeType',
|
|
17
|
+
'description',
|
|
18
|
+
'modifiedTime',
|
|
19
|
+
'size',
|
|
20
|
+
]),
|
|
21
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ProviderDefinition } from '../../contracts';
|
|
2
|
+
|
|
3
|
+
export const driveProvider: ProviderDefinition = {
|
|
4
|
+
key: 'drive',
|
|
5
|
+
name: 'Google Drive',
|
|
6
|
+
documentationUrl: 'https://developers.google.com/workspace/drive/api/reference/rest/v3/files/get',
|
|
7
|
+
authMethods: ['oauth2'],
|
|
8
|
+
family: 'google',
|
|
9
|
+
oauthScopes: ['https://www.googleapis.com/auth/drive.metadata.readonly'],
|
|
10
|
+
operations: [
|
|
11
|
+
{
|
|
12
|
+
key: 'files.read',
|
|
13
|
+
name: 'Read file metadata',
|
|
14
|
+
resourceKind: 'file',
|
|
15
|
+
requiredScopes: ['https://www.googleapis.com/auth/drive.metadata.readonly'],
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
};
|