@yolk-sdk/anthropic 0.0.1-canary.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yolk SDK contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @yolk-sdk/anthropic
2
+
3
+ Anthropic Claude OAuth mechanics and broker integration primitives.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @yolk-sdk/anthropic@canary @yolk-sdk/oauth@canary effect
9
+ ```
10
+
11
+ Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
12
+
13
+ ## Subpaths
14
+
15
+ | Subpath | Purpose |
16
+ | --- | --- |
17
+ | `@yolk-sdk/anthropic` | Anthropic Claude OAuth constants, auth URL helpers, broker helpers, and headers |
18
+
19
+ ## Imports
20
+
21
+ ```ts
22
+ import {
23
+ anthropicClaudeAuthorizationHeaders,
24
+ anthropicClaudeProviderId,
25
+ makeAnthropicClaudeAuthorizationUrl,
26
+ makeAnthropicClaudeBrokerRequest,
27
+ parseAnthropicClaudeAuthorizationCode
28
+ } from '@yolk-sdk/anthropic'
29
+ ```
30
+
31
+ ## OAuth URL
32
+
33
+ ```ts
34
+ const url = makeAnthropicClaudeAuthorizationUrl({
35
+ state: 'opaque-state',
36
+ codeChallenge: 'pkce-code-challenge'
37
+ })
38
+ ```
39
+
40
+ Hosts own PKCE verifier storage and callback routes.
41
+
42
+ ## Callback parse
43
+
44
+ ```ts
45
+ const parsed = parseAnthropicClaudeAuthorizationCode(callbackUrl)
46
+ ```
47
+
48
+ ## Broker request
49
+
50
+ ```ts
51
+ const request = makeAnthropicClaudeBrokerRequest({
52
+ subject: 'opaque-host-subject',
53
+ minimumTtlMs: 60_000
54
+ })
55
+ ```
56
+
57
+ ## Host responsibilities
58
+
59
+ - Own OAuth verifier/state storage, callback handling, and refresh-token storage.
60
+ - Provide hosted or local credential sources through `@yolk-sdk/oauth`.
61
+ - Own provider requests, telemetry, and product permissions.
62
+
63
+ ## Boundaries
64
+
65
+ - No app users, sessions, DB, Better Auth, routes, or Durable Objects.
66
+ - No Anthropic SDK dependency.
67
+ - API-key mode can be added later without changing OAuth contracts.
@@ -0,0 +1,53 @@
1
+ import * as Schema from "effect/Schema";
2
+ import { OAuthAccessToken, TokenBrokerClient, TokenBrokerRequest } from "@yolk-sdk/oauth";
3
+
4
+ //#region src/claude.d.ts
5
+ declare const anthropicClaudeProviderId = "anthropic-claude";
6
+ declare const anthropicClaudeClientId = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
7
+ declare const anthropicClaudeAuthorizeUrl = "https://claude.ai/oauth/authorize";
8
+ declare const anthropicClaudeTokenEndpoint = "https://console.anthropic.com/v1/oauth/token";
9
+ declare const anthropicClaudeRedirectUri = "https://console.anthropic.com/oauth/code/callback";
10
+ declare const anthropicClaudeScopes = "org:create_api_key user:profile user:inference";
11
+ declare const anthropicClaudeOAuthUserAgent = "claude-cli/2.1.2 (external, cli)";
12
+ declare const anthropicClaudeRefreshBufferMs: number;
13
+ declare const AnthropicClaudeOAuthToken_base: Schema.Class<AnthropicClaudeOAuthToken, Schema.Struct<{
14
+ readonly accessToken: Schema.String;
15
+ readonly refreshToken: Schema.String;
16
+ readonly expiresAt: Schema.Number;
17
+ readonly accountId: Schema.optional<Schema.String>;
18
+ }>, {}>;
19
+ declare class AnthropicClaudeOAuthToken extends AnthropicClaudeOAuthToken_base {}
20
+ declare const AnthropicClaudeTokenBrokerResponse_base: Schema.Class<AnthropicClaudeTokenBrokerResponse, Schema.Struct<{
21
+ readonly accessToken: Schema.String;
22
+ readonly expiresAt: Schema.Number;
23
+ readonly accountId: Schema.optional<Schema.String>;
24
+ }>, {}>;
25
+ declare class AnthropicClaudeTokenBrokerResponse extends AnthropicClaudeTokenBrokerResponse_base {}
26
+ declare const toAnthropicClaudeOAuthAccessToken: (token: AnthropicClaudeTokenBrokerResponse) => OAuthAccessToken;
27
+ declare const makeAnthropicClaudeBrokerRequest: (input: {
28
+ readonly subjectId: string;
29
+ readonly minTtlSeconds?: number;
30
+ readonly forceRefresh?: boolean;
31
+ }) => TokenBrokerRequest;
32
+ declare const anthropicClaudeAuthorizationHeaders: (token: OAuthAccessToken) => {
33
+ authorization: string;
34
+ };
35
+ declare const makeAnthropicClaudeTokenBrokerClient: (broker: TokenBrokerClient) => {
36
+ getAccessToken: (input: {
37
+ readonly subjectId: string;
38
+ readonly minTtlSeconds?: number;
39
+ readonly forceRefresh?: boolean;
40
+ }) => import("effect/Effect").Effect<OAuthAccessToken, import("@yolk-sdk/oauth").OAuthError, never>;
41
+ };
42
+ declare const makeAnthropicClaudeAuthorizationUrl: (input: {
43
+ readonly codeChallenge: string;
44
+ readonly state: string;
45
+ }) => string;
46
+ type ParsedAnthropicClaudeAuthorizationCode = {
47
+ readonly code: string;
48
+ readonly state: string;
49
+ };
50
+ declare const parseAnthropicClaudeAuthorizationCode: (value: string) => ParsedAnthropicClaudeAuthorizationCode | undefined;
51
+ //#endregion
52
+ export { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, ParsedAnthropicClaudeAuthorizationCode, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken };
53
+ //# sourceMappingURL=claude.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"claude.d.mts","names":[],"sources":["../src/claude.ts"],"mappings":";;;;cAGa,yBAAA;AAAA,cACA,uBAAA;AAAA,cACA,2BAAA;AAAA,cACA,4BAAA;AAAA,cACA,0BAAA;AAAA,cACA,qBAAA;AAAA,cACA,6BAAA;AAAA,cACA,8BAAA;AAAA,cAA8C,8BAAA;;;;;;cAE9C,yBAAA,SAAkC,8BAO7C;AAAA,cAAG,uCAAA;;;;;cAEQ,kCAAA,SAA2C,uCAMtD;AAAA,cAEW,iCAAA,GAAqC,KAAA,EAAO,kCAAA,KAAkC,gBAMvF;AAAA,cAES,gCAAA,GAAoC,KAAA;EAAA,SACtC,SAAA;EAAA,SACA,aAAA;EAAA,SACA,YAAA;AAAA,MACV,kBAMG;AAAA,cAES,mCAAA,GAAuC,KAAA,EAAO,gBAAgB;;;cAI9D,oCAAA,GAAwC,MAAA,EAAQ,iBAAA;;aAEhD,SAAA;IAAA,SACA,aAAA;IAAA,SACA,YAAA;EAAA,8BACV,MAAA,CAAA,gBAAA,4BAAA,UAAA;AAAA;AAAA,cAGU,mCAAA,GAAuC,KAAA;EAAA,SACzC,aAAA;EAAA,SACA,KAAA;AAAA;AAAA,KAeC,sCAAA;EAAA,SACD,IAAA;EAAA,SACA,KAAK;AAAA;AAAA,cAGH,qCAAA,GACX,KAAA,aACC,sCAAsC"}
@@ -0,0 +1,88 @@
1
+ import * as Schema from "effect/Schema";
2
+ import { OAuthAccessToken, TokenBrokerRequest } from "@yolk-sdk/oauth";
3
+ //#region src/claude.ts
4
+ const anthropicClaudeProviderId = "anthropic-claude";
5
+ const anthropicClaudeClientId = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
6
+ const anthropicClaudeAuthorizeUrl = "https://claude.ai/oauth/authorize";
7
+ const anthropicClaudeTokenEndpoint = "https://console.anthropic.com/v1/oauth/token";
8
+ const anthropicClaudeRedirectUri = "https://console.anthropic.com/oauth/code/callback";
9
+ const anthropicClaudeScopes = "org:create_api_key user:profile user:inference";
10
+ const anthropicClaudeOAuthUserAgent = "claude-cli/2.1.2 (external, cli)";
11
+ const anthropicClaudeRefreshBufferMs = 300 * 1e3;
12
+ var AnthropicClaudeOAuthToken = class extends Schema.Class("AnthropicClaudeOAuthToken")({
13
+ accessToken: Schema.String,
14
+ refreshToken: Schema.String,
15
+ expiresAt: Schema.Number,
16
+ accountId: Schema.optional(Schema.String)
17
+ }) {};
18
+ var AnthropicClaudeTokenBrokerResponse = class extends Schema.Class("AnthropicClaudeTokenBrokerResponse")({
19
+ accessToken: Schema.String,
20
+ expiresAt: Schema.Number,
21
+ accountId: Schema.optional(Schema.String)
22
+ }) {};
23
+ const toAnthropicClaudeOAuthAccessToken = (token) => new OAuthAccessToken({
24
+ provider: anthropicClaudeProviderId,
25
+ accessToken: token.accessToken,
26
+ expiresAt: token.expiresAt,
27
+ accountId: token.accountId
28
+ });
29
+ const makeAnthropicClaudeBrokerRequest = (input) => new TokenBrokerRequest({
30
+ provider: anthropicClaudeProviderId,
31
+ subjectId: input.subjectId,
32
+ minTtlSeconds: input.minTtlSeconds,
33
+ forceRefresh: input.forceRefresh
34
+ });
35
+ const anthropicClaudeAuthorizationHeaders = (token) => ({ authorization: `Bearer ${token.accessToken}` });
36
+ const makeAnthropicClaudeTokenBrokerClient = (broker) => ({ getAccessToken: (input) => broker.getAccessToken(makeAnthropicClaudeBrokerRequest(input)) });
37
+ const makeAnthropicClaudeAuthorizationUrl = (input) => {
38
+ const authUrl = new URL(anthropicClaudeAuthorizeUrl);
39
+ authUrl.searchParams.set("code", "true");
40
+ authUrl.searchParams.set("client_id", anthropicClaudeClientId);
41
+ authUrl.searchParams.set("response_type", "code");
42
+ authUrl.searchParams.set("redirect_uri", anthropicClaudeRedirectUri);
43
+ authUrl.searchParams.set("scope", anthropicClaudeScopes);
44
+ authUrl.searchParams.set("code_challenge", input.codeChallenge);
45
+ authUrl.searchParams.set("code_challenge_method", "S256");
46
+ authUrl.searchParams.set("state", input.state);
47
+ return authUrl.toString();
48
+ };
49
+ const parseAnthropicClaudeAuthorizationCode = (value) => {
50
+ const trimmed = value.trim();
51
+ if (trimmed.length === 0) return;
52
+ const parseFromUrl = () => {
53
+ try {
54
+ const url = new URL(trimmed);
55
+ const code = url.searchParams.get("code") ?? void 0;
56
+ const state = url.searchParams.get("state") ?? void 0;
57
+ if (code === void 0 || state === void 0) return;
58
+ return {
59
+ code,
60
+ state
61
+ };
62
+ } catch {
63
+ return;
64
+ }
65
+ };
66
+ const fromUrl = parseFromUrl();
67
+ if (fromUrl !== void 0) return fromUrl;
68
+ if (trimmed.includes("#")) {
69
+ const [code, state] = trimmed.split("#", 2);
70
+ if (code !== void 0 && code.length > 0 && state !== void 0 && state.length > 0) return {
71
+ code,
72
+ state
73
+ };
74
+ }
75
+ if (trimmed.includes("code=")) {
76
+ const params = new URLSearchParams(trimmed);
77
+ const code = params.get("code") ?? void 0;
78
+ const state = params.get("state") ?? void 0;
79
+ if (code !== void 0 && state !== void 0) return {
80
+ code,
81
+ state
82
+ };
83
+ }
84
+ };
85
+ //#endregion
86
+ export { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken };
87
+
88
+ //# sourceMappingURL=claude.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"claude.mjs","names":[],"sources":["../src/claude.ts"],"sourcesContent":["import * as Schema from 'effect/Schema'\nimport { OAuthAccessToken, TokenBrokerRequest, type TokenBrokerClient } from '@yolk-sdk/oauth'\n\nexport const anthropicClaudeProviderId = 'anthropic-claude'\nexport const anthropicClaudeClientId = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'\nexport const anthropicClaudeAuthorizeUrl = 'https://claude.ai/oauth/authorize'\nexport const anthropicClaudeTokenEndpoint = 'https://console.anthropic.com/v1/oauth/token'\nexport const anthropicClaudeRedirectUri = 'https://console.anthropic.com/oauth/code/callback'\nexport const anthropicClaudeScopes = 'org:create_api_key user:profile user:inference'\nexport const anthropicClaudeOAuthUserAgent = 'claude-cli/2.1.2 (external, cli)'\nexport const anthropicClaudeRefreshBufferMs = 5 * 60 * 1000\n\nexport class AnthropicClaudeOAuthToken extends Schema.Class<AnthropicClaudeOAuthToken>(\n 'AnthropicClaudeOAuthToken'\n)({\n accessToken: Schema.String,\n refreshToken: Schema.String,\n expiresAt: Schema.Number,\n accountId: Schema.optional(Schema.String)\n}) {}\n\nexport class AnthropicClaudeTokenBrokerResponse extends Schema.Class<AnthropicClaudeTokenBrokerResponse>(\n 'AnthropicClaudeTokenBrokerResponse'\n)({\n accessToken: Schema.String,\n expiresAt: Schema.Number,\n accountId: Schema.optional(Schema.String)\n}) {}\n\nexport const toAnthropicClaudeOAuthAccessToken = (token: AnthropicClaudeTokenBrokerResponse) =>\n new OAuthAccessToken({\n provider: anthropicClaudeProviderId,\n accessToken: token.accessToken,\n expiresAt: token.expiresAt,\n accountId: token.accountId\n })\n\nexport const makeAnthropicClaudeBrokerRequest = (input: {\n readonly subjectId: string\n readonly minTtlSeconds?: number\n readonly forceRefresh?: boolean\n}) =>\n new TokenBrokerRequest({\n provider: anthropicClaudeProviderId,\n subjectId: input.subjectId,\n minTtlSeconds: input.minTtlSeconds,\n forceRefresh: input.forceRefresh\n })\n\nexport const anthropicClaudeAuthorizationHeaders = (token: OAuthAccessToken) => ({\n authorization: `Bearer ${token.accessToken}`\n})\n\nexport const makeAnthropicClaudeTokenBrokerClient = (broker: TokenBrokerClient) => ({\n getAccessToken: (input: {\n readonly subjectId: string\n readonly minTtlSeconds?: number\n readonly forceRefresh?: boolean\n }) => broker.getAccessToken(makeAnthropicClaudeBrokerRequest(input))\n})\n\nexport const makeAnthropicClaudeAuthorizationUrl = (input: {\n readonly codeChallenge: string\n readonly state: string\n}) => {\n const authUrl = new URL(anthropicClaudeAuthorizeUrl)\n authUrl.searchParams.set('code', 'true')\n authUrl.searchParams.set('client_id', anthropicClaudeClientId)\n authUrl.searchParams.set('response_type', 'code')\n authUrl.searchParams.set('redirect_uri', anthropicClaudeRedirectUri)\n authUrl.searchParams.set('scope', anthropicClaudeScopes)\n authUrl.searchParams.set('code_challenge', input.codeChallenge)\n authUrl.searchParams.set('code_challenge_method', 'S256')\n authUrl.searchParams.set('state', input.state)\n\n return authUrl.toString()\n}\n\nexport type ParsedAnthropicClaudeAuthorizationCode = {\n readonly code: string\n readonly state: string\n}\n\nexport const parseAnthropicClaudeAuthorizationCode = (\n value: string\n): ParsedAnthropicClaudeAuthorizationCode | undefined => {\n const trimmed = value.trim()\n\n if (trimmed.length === 0) {\n return undefined\n }\n\n const parseFromUrl = () => {\n try {\n const url = new URL(trimmed)\n const code = url.searchParams.get('code') ?? undefined\n const state = url.searchParams.get('state') ?? undefined\n\n if (code === undefined || state === undefined) {\n return undefined\n }\n\n return { code, state }\n } catch {\n return undefined\n }\n }\n\n const fromUrl = parseFromUrl()\n\n if (fromUrl !== undefined) {\n return fromUrl\n }\n\n if (trimmed.includes('#')) {\n const [code, state] = trimmed.split('#', 2)\n\n if (code !== undefined && code.length > 0 && state !== undefined && state.length > 0) {\n return { code, state }\n }\n }\n\n if (trimmed.includes('code=')) {\n const params = new URLSearchParams(trimmed)\n const code = params.get('code') ?? undefined\n const state = params.get('state') ?? undefined\n\n if (code !== undefined && state !== undefined) {\n return { code, state }\n }\n }\n\n return undefined\n}\n"],"mappings":";;;AAGA,MAAa,4BAA4B;AACzC,MAAa,0BAA0B;AACvC,MAAa,8BAA8B;AAC3C,MAAa,+BAA+B;AAC5C,MAAa,6BAA6B;AAC1C,MAAa,wBAAwB;AACrC,MAAa,gCAAgC;AAC7C,MAAa,iCAAiC,MAAS;AAEvD,IAAa,4BAAb,cAA+C,OAAO,MACpD,2BACF,EAAE;CACA,aAAa,OAAO;CACpB,cAAc,OAAO;CACrB,WAAW,OAAO;CAClB,WAAW,OAAO,SAAS,OAAO,MAAM;AAC1C,CAAC,EAAE,CAAC;AAEJ,IAAa,qCAAb,cAAwD,OAAO,MAC7D,oCACF,EAAE;CACA,aAAa,OAAO;CACpB,WAAW,OAAO;CAClB,WAAW,OAAO,SAAS,OAAO,MAAM;AAC1C,CAAC,EAAE,CAAC;AAEJ,MAAa,qCAAqC,UAChD,IAAI,iBAAiB;CACnB,UAAU;CACV,aAAa,MAAM;CACnB,WAAW,MAAM;CACjB,WAAW,MAAM;AACnB,CAAC;AAEH,MAAa,oCAAoC,UAK/C,IAAI,mBAAmB;CACrB,UAAU;CACV,WAAW,MAAM;CACjB,eAAe,MAAM;CACrB,cAAc,MAAM;AACtB,CAAC;AAEH,MAAa,uCAAuC,WAA6B,EAC/E,eAAe,UAAU,MAAM,cACjC;AAEA,MAAa,wCAAwC,YAA+B,EAClF,iBAAiB,UAIX,OAAO,eAAe,iCAAiC,KAAK,CAAC,EACrE;AAEA,MAAa,uCAAuC,UAG9C;CACJ,MAAM,UAAU,IAAI,IAAI,2BAA2B;CACnD,QAAQ,aAAa,IAAI,QAAQ,MAAM;CACvC,QAAQ,aAAa,IAAI,aAAa,uBAAuB;CAC7D,QAAQ,aAAa,IAAI,iBAAiB,MAAM;CAChD,QAAQ,aAAa,IAAI,gBAAgB,0BAA0B;CACnE,QAAQ,aAAa,IAAI,SAAS,qBAAqB;CACvD,QAAQ,aAAa,IAAI,kBAAkB,MAAM,aAAa;CAC9D,QAAQ,aAAa,IAAI,yBAAyB,MAAM;CACxD,QAAQ,aAAa,IAAI,SAAS,MAAM,KAAK;CAE7C,OAAO,QAAQ,SAAS;AAC1B;AAOA,MAAa,yCACX,UACuD;CACvD,MAAM,UAAU,MAAM,KAAK;CAE3B,IAAI,QAAQ,WAAW,GACrB;CAGF,MAAM,qBAAqB;EACzB,IAAI;GACF,MAAM,MAAM,IAAI,IAAI,OAAO;GAC3B,MAAM,OAAO,IAAI,aAAa,IAAI,MAAM,KAAK,KAAA;GAC7C,MAAM,QAAQ,IAAI,aAAa,IAAI,OAAO,KAAK,KAAA;GAE/C,IAAI,SAAS,KAAA,KAAa,UAAU,KAAA,GAClC;GAGF,OAAO;IAAE;IAAM;GAAM;EACvB,QAAQ;GACN;EACF;CACF;CAEA,MAAM,UAAU,aAAa;CAE7B,IAAI,YAAY,KAAA,GACd,OAAO;CAGT,IAAI,QAAQ,SAAS,GAAG,GAAG;EACzB,MAAM,CAAC,MAAM,SAAS,QAAQ,MAAM,KAAK,CAAC;EAE1C,IAAI,SAAS,KAAA,KAAa,KAAK,SAAS,KAAK,UAAU,KAAA,KAAa,MAAM,SAAS,GACjF,OAAO;GAAE;GAAM;EAAM;CAEzB;CAEA,IAAI,QAAQ,SAAS,OAAO,GAAG;EAC7B,MAAM,SAAS,IAAI,gBAAgB,OAAO;EAC1C,MAAM,OAAO,OAAO,IAAI,MAAM,KAAK,KAAA;EACnC,MAAM,QAAQ,OAAO,IAAI,OAAO,KAAK,KAAA;EAErC,IAAI,SAAS,KAAA,KAAa,UAAU,KAAA,GAClC,OAAO;GAAE;GAAM;EAAM;CAEzB;AAGF"}
@@ -0,0 +1,2 @@
1
+ import { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, ParsedAnthropicClaudeAuthorizationCode, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken } from "./claude.mjs";
2
+ export { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, type ParsedAnthropicClaudeAuthorizationCode, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken } from "./claude.mjs";
2
+ export { AnthropicClaudeOAuthToken, AnthropicClaudeTokenBrokerResponse, anthropicClaudeAuthorizationHeaders, anthropicClaudeAuthorizeUrl, anthropicClaudeClientId, anthropicClaudeOAuthUserAgent, anthropicClaudeProviderId, anthropicClaudeRedirectUri, anthropicClaudeRefreshBufferMs, anthropicClaudeScopes, anthropicClaudeTokenEndpoint, makeAnthropicClaudeAuthorizationUrl, makeAnthropicClaudeBrokerRequest, makeAnthropicClaudeTokenBrokerClient, parseAnthropicClaudeAuthorizationCode, toAnthropicClaudeOAuthAccessToken };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@yolk-sdk/anthropic",
3
+ "version": "0.0.1-canary.0",
4
+ "description": "Anthropic Claude OAuth and provider mechanics for Yolk hosts.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/magoz/yolk-sdk.git",
11
+ "directory": "packages/anthropic"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/magoz/yolk-sdk/issues"
15
+ },
16
+ "homepage": "https://github.com/magoz/yolk-sdk#readme",
17
+ "keywords": [
18
+ "anthropic",
19
+ "claude",
20
+ "oauth",
21
+ "agent",
22
+ "effect"
23
+ ],
24
+ "engines": {
25
+ "node": ">=22"
26
+ },
27
+ "exports": {
28
+ "./package.json": "./package.json",
29
+ ".": {
30
+ "types": "./dist/index.d.mts",
31
+ "import": "./dist/index.mjs",
32
+ "default": "./dist/index.mjs"
33
+ }
34
+ },
35
+ "files": [
36
+ "src/**/*.ts",
37
+ "!src/**/*.test.ts",
38
+ "!src/**/*.test.tsx",
39
+ "dist/**/*",
40
+ "README.md"
41
+ ],
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "provenance": true
45
+ },
46
+ "dependencies": {
47
+ "effect": "4.0.0-beta.65",
48
+ "@yolk-sdk/oauth": "^0.0.1-canary.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsdown",
52
+ "check": "tsc -p tsconfig.json --noEmit",
53
+ "test": "vitest run --passWithNoTests",
54
+ "test:run": "vitest run --passWithNoTests"
55
+ }
56
+ }
package/src/claude.ts ADDED
@@ -0,0 +1,134 @@
1
+ import * as Schema from 'effect/Schema'
2
+ import { OAuthAccessToken, TokenBrokerRequest, type TokenBrokerClient } from '@yolk-sdk/oauth'
3
+
4
+ export const anthropicClaudeProviderId = 'anthropic-claude'
5
+ export const anthropicClaudeClientId = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'
6
+ export const anthropicClaudeAuthorizeUrl = 'https://claude.ai/oauth/authorize'
7
+ export const anthropicClaudeTokenEndpoint = 'https://console.anthropic.com/v1/oauth/token'
8
+ export const anthropicClaudeRedirectUri = 'https://console.anthropic.com/oauth/code/callback'
9
+ export const anthropicClaudeScopes = 'org:create_api_key user:profile user:inference'
10
+ export const anthropicClaudeOAuthUserAgent = 'claude-cli/2.1.2 (external, cli)'
11
+ export const anthropicClaudeRefreshBufferMs = 5 * 60 * 1000
12
+
13
+ export class AnthropicClaudeOAuthToken extends Schema.Class<AnthropicClaudeOAuthToken>(
14
+ 'AnthropicClaudeOAuthToken'
15
+ )({
16
+ accessToken: Schema.String,
17
+ refreshToken: Schema.String,
18
+ expiresAt: Schema.Number,
19
+ accountId: Schema.optional(Schema.String)
20
+ }) {}
21
+
22
+ export class AnthropicClaudeTokenBrokerResponse extends Schema.Class<AnthropicClaudeTokenBrokerResponse>(
23
+ 'AnthropicClaudeTokenBrokerResponse'
24
+ )({
25
+ accessToken: Schema.String,
26
+ expiresAt: Schema.Number,
27
+ accountId: Schema.optional(Schema.String)
28
+ }) {}
29
+
30
+ export const toAnthropicClaudeOAuthAccessToken = (token: AnthropicClaudeTokenBrokerResponse) =>
31
+ new OAuthAccessToken({
32
+ provider: anthropicClaudeProviderId,
33
+ accessToken: token.accessToken,
34
+ expiresAt: token.expiresAt,
35
+ accountId: token.accountId
36
+ })
37
+
38
+ export const makeAnthropicClaudeBrokerRequest = (input: {
39
+ readonly subjectId: string
40
+ readonly minTtlSeconds?: number
41
+ readonly forceRefresh?: boolean
42
+ }) =>
43
+ new TokenBrokerRequest({
44
+ provider: anthropicClaudeProviderId,
45
+ subjectId: input.subjectId,
46
+ minTtlSeconds: input.minTtlSeconds,
47
+ forceRefresh: input.forceRefresh
48
+ })
49
+
50
+ export const anthropicClaudeAuthorizationHeaders = (token: OAuthAccessToken) => ({
51
+ authorization: `Bearer ${token.accessToken}`
52
+ })
53
+
54
+ export const makeAnthropicClaudeTokenBrokerClient = (broker: TokenBrokerClient) => ({
55
+ getAccessToken: (input: {
56
+ readonly subjectId: string
57
+ readonly minTtlSeconds?: number
58
+ readonly forceRefresh?: boolean
59
+ }) => broker.getAccessToken(makeAnthropicClaudeBrokerRequest(input))
60
+ })
61
+
62
+ export const makeAnthropicClaudeAuthorizationUrl = (input: {
63
+ readonly codeChallenge: string
64
+ readonly state: string
65
+ }) => {
66
+ const authUrl = new URL(anthropicClaudeAuthorizeUrl)
67
+ authUrl.searchParams.set('code', 'true')
68
+ authUrl.searchParams.set('client_id', anthropicClaudeClientId)
69
+ authUrl.searchParams.set('response_type', 'code')
70
+ authUrl.searchParams.set('redirect_uri', anthropicClaudeRedirectUri)
71
+ authUrl.searchParams.set('scope', anthropicClaudeScopes)
72
+ authUrl.searchParams.set('code_challenge', input.codeChallenge)
73
+ authUrl.searchParams.set('code_challenge_method', 'S256')
74
+ authUrl.searchParams.set('state', input.state)
75
+
76
+ return authUrl.toString()
77
+ }
78
+
79
+ export type ParsedAnthropicClaudeAuthorizationCode = {
80
+ readonly code: string
81
+ readonly state: string
82
+ }
83
+
84
+ export const parseAnthropicClaudeAuthorizationCode = (
85
+ value: string
86
+ ): ParsedAnthropicClaudeAuthorizationCode | undefined => {
87
+ const trimmed = value.trim()
88
+
89
+ if (trimmed.length === 0) {
90
+ return undefined
91
+ }
92
+
93
+ const parseFromUrl = () => {
94
+ try {
95
+ const url = new URL(trimmed)
96
+ const code = url.searchParams.get('code') ?? undefined
97
+ const state = url.searchParams.get('state') ?? undefined
98
+
99
+ if (code === undefined || state === undefined) {
100
+ return undefined
101
+ }
102
+
103
+ return { code, state }
104
+ } catch {
105
+ return undefined
106
+ }
107
+ }
108
+
109
+ const fromUrl = parseFromUrl()
110
+
111
+ if (fromUrl !== undefined) {
112
+ return fromUrl
113
+ }
114
+
115
+ if (trimmed.includes('#')) {
116
+ const [code, state] = trimmed.split('#', 2)
117
+
118
+ if (code !== undefined && code.length > 0 && state !== undefined && state.length > 0) {
119
+ return { code, state }
120
+ }
121
+ }
122
+
123
+ if (trimmed.includes('code=')) {
124
+ const params = new URLSearchParams(trimmed)
125
+ const code = params.get('code') ?? undefined
126
+ const state = params.get('state') ?? undefined
127
+
128
+ if (code !== undefined && state !== undefined) {
129
+ return { code, state }
130
+ }
131
+ }
132
+
133
+ return undefined
134
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ export {
2
+ AnthropicClaudeOAuthToken,
3
+ AnthropicClaudeTokenBrokerResponse,
4
+ anthropicClaudeAuthorizationHeaders,
5
+ anthropicClaudeAuthorizeUrl,
6
+ anthropicClaudeClientId,
7
+ anthropicClaudeOAuthUserAgent,
8
+ anthropicClaudeProviderId,
9
+ anthropicClaudeRedirectUri,
10
+ anthropicClaudeRefreshBufferMs,
11
+ anthropicClaudeScopes,
12
+ anthropicClaudeTokenEndpoint,
13
+ makeAnthropicClaudeAuthorizationUrl,
14
+ makeAnthropicClaudeBrokerRequest,
15
+ makeAnthropicClaudeTokenBrokerClient,
16
+ parseAnthropicClaudeAuthorizationCode,
17
+ toAnthropicClaudeOAuthAccessToken,
18
+ type ParsedAnthropicClaudeAuthorizationCode
19
+ } from './claude.ts'