@puku-ai/sdk 4.0.3 → 4.0.4
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/README.md +24 -7
- package/dist/index.cjs +85 -0
- package/dist/vendor/_vendor/partial-json-parser/parser.cjs +225 -0
- package/dist/vendor/api-promise.cjs +18 -0
- package/dist/vendor/client.cjs +1013 -0
- package/dist/vendor/core/api-promise.cjs +80 -0
- package/dist/vendor/core/api.cjs +2 -0
- package/dist/vendor/core/credentials.cjs +313 -0
- package/dist/vendor/core/error.cjs +156 -0
- package/dist/vendor/core/middleware.cjs +173 -0
- package/dist/vendor/core/pagination.cjs +215 -0
- package/dist/vendor/core/resource.cjs +11 -0
- package/dist/vendor/core/streaming.cjs +346 -0
- package/dist/vendor/core/uploads.cjs +5 -0
- package/dist/vendor/error.cjs +18 -0
- package/dist/vendor/helpers/beta/environments.cjs +30 -0
- package/dist/vendor/helpers/beta/json-schema.cjs +56 -0
- package/dist/vendor/helpers/beta/mcp.cjs +418 -0
- package/dist/vendor/helpers/beta/memory.cjs +17 -0
- package/dist/vendor/helpers/beta/standard-schema.cjs +57 -0
- package/dist/vendor/helpers/beta/zod.cjs +88 -0
- package/dist/vendor/helpers/index.cjs +7 -0
- package/dist/vendor/helpers/json-schema.cjs +36 -0
- package/dist/vendor/helpers/zod.cjs +77 -0
- package/dist/vendor/index.cjs +54 -0
- package/dist/vendor/internal/builtin-types.cjs +3 -0
- package/dist/vendor/internal/constants.cjs +12 -0
- package/dist/vendor/internal/decoders/jsonl.cjs +40 -0
- package/dist/vendor/internal/decoders/line.cjs +109 -0
- package/dist/vendor/internal/detect-platform.cjs +159 -0
- package/dist/vendor/internal/errors.cjs +42 -0
- package/dist/vendor/internal/file-store.cjs +535 -0
- package/dist/vendor/internal/headers.cjs +119 -0
- package/dist/vendor/internal/message-stream-utils.cjs +33 -0
- package/dist/vendor/internal/node.browser.cjs +21 -0
- package/dist/vendor/internal/node.cjs +55 -0
- package/dist/vendor/internal/parse.cjs +65 -0
- package/dist/vendor/internal/qs/formats.cjs +12 -0
- package/dist/vendor/internal/qs/index.cjs +13 -0
- package/dist/vendor/internal/qs/stringify.cjs +276 -0
- package/dist/vendor/internal/qs/types.cjs +2 -0
- package/dist/vendor/internal/qs/utils.cjs +229 -0
- package/dist/vendor/internal/request-options.cjs +31 -0
- package/dist/vendor/internal/request-signal.cjs +49 -0
- package/dist/vendor/internal/shim-types.cjs +3 -0
- package/dist/vendor/internal/shims.cjs +91 -0
- package/dist/vendor/internal/stainless-helper-header.cjs +90 -0
- package/dist/vendor/internal/stream-utils.cjs +37 -0
- package/dist/vendor/internal/to-file.cjs +95 -0
- package/dist/vendor/internal/types.cjs +3 -0
- package/dist/vendor/internal/uploads.cjs +148 -0
- package/dist/vendor/internal/utils/abort.cjs +24 -0
- package/dist/vendor/internal/utils/async-queue.cjs +69 -0
- package/dist/vendor/internal/utils/backoff.cjs +43 -0
- package/dist/vendor/internal/utils/base64.cjs +37 -0
- package/dist/vendor/internal/utils/bytes.cjs +30 -0
- package/dist/vendor/internal/utils/env.cjs +21 -0
- package/dist/vendor/internal/utils/log.cjs +111 -0
- package/dist/vendor/internal/utils/path.cjs +78 -0
- package/dist/vendor/internal/utils/promise.cjs +17 -0
- package/dist/vendor/internal/utils/query.cjs +41 -0
- package/dist/vendor/internal/utils/sleep.cjs +27 -0
- package/dist/vendor/internal/utils/time.cjs +7 -0
- package/dist/vendor/internal/utils/uuid.cjs +18 -0
- package/dist/vendor/internal/utils/values.cjs +125 -0
- package/dist/vendor/internal/utils.cjs +24 -0
- package/dist/vendor/lib/BetaMessageStream.cjs +665 -0
- package/dist/vendor/lib/MessageStream.cjs +612 -0
- package/dist/vendor/lib/beta-parser.cjs +78 -0
- package/dist/vendor/lib/credentials/credential-chain.cjs +244 -0
- package/dist/vendor/lib/credentials/identity-token.cjs +71 -0
- package/dist/vendor/lib/credentials/oidc-federation.cjs +81 -0
- package/dist/vendor/lib/credentials/token-cache.cjs +113 -0
- package/dist/vendor/lib/credentials/types.cjs +267 -0
- package/dist/vendor/lib/credentials/user-oauth.cjs +129 -0
- package/dist/vendor/lib/credentials.cjs +7 -0
- package/dist/vendor/lib/environments/index.cjs +17 -0
- package/dist/vendor/lib/environments/poller.cjs +226 -0
- package/dist/vendor/lib/environments/worker.cjs +609 -0
- package/dist/vendor/lib/helper-client.cjs +57 -0
- package/dist/vendor/lib/middleware.cjs +779 -0
- package/dist/vendor/lib/parser.cjs +65 -0
- package/dist/vendor/lib/sessions/accumulate.cjs +44 -0
- package/dist/vendor/lib/standard-schema.cjs +44 -0
- package/dist/vendor/lib/tools/BetaRunnableTool.cjs +30 -0
- package/dist/vendor/lib/tools/BetaToolRunner.cjs +500 -0
- package/dist/vendor/lib/tools/CompactionControl.cjs +27 -0
- package/dist/vendor/lib/tools/SessionToolRunner.cjs +740 -0
- package/dist/vendor/lib/tools/ToolError.cjs +46 -0
- package/dist/vendor/lib/transform-json-schema.cjs +113 -0
- package/dist/vendor/pagination.cjs +18 -0
- package/dist/vendor/resource.cjs +18 -0
- package/dist/vendor/resources/beta/agents/agents.cjs +159 -0
- package/dist/vendor/resources/beta/agents/index.cjs +8 -0
- package/dist/vendor/resources/beta/agents/versions.cjs +35 -0
- package/dist/vendor/resources/beta/agents.cjs +18 -0
- package/dist/vendor/resources/beta/beta.cjs +105 -0
- package/dist/vendor/resources/beta/deployment-runs.cjs +54 -0
- package/dist/vendor/resources/beta/deployments.cjs +195 -0
- package/dist/vendor/resources/beta/dreams.cjs +115 -0
- package/dist/vendor/resources/beta/environments/environments.cjs +178 -0
- package/dist/vendor/resources/beta/environments/index.cjs +8 -0
- package/dist/vendor/resources/beta/environments/work.cjs +256 -0
- package/dist/vendor/resources/beta/environments.cjs +18 -0
- package/dist/vendor/resources/beta/files.cjs +123 -0
- package/dist/vendor/resources/beta/index.cjs +38 -0
- package/dist/vendor/resources/beta/memory-stores/index.cjs +10 -0
- package/dist/vendor/resources/beta/memory-stores/memories.cjs +129 -0
- package/dist/vendor/resources/beta/memory-stores/memory-stores.cjs +173 -0
- package/dist/vendor/resources/beta/memory-stores/memory-versions.cjs +80 -0
- package/dist/vendor/resources/beta/memory-stores.cjs +18 -0
- package/dist/vendor/resources/beta/messages/batches.cjs +206 -0
- package/dist/vendor/resources/beta/messages/index.cjs +11 -0
- package/dist/vendor/resources/beta/messages/messages.cjs +189 -0
- package/dist/vendor/resources/beta/messages.cjs +18 -0
- package/dist/vendor/resources/beta/models.cjs +59 -0
- package/dist/vendor/resources/beta/organization/api-keys.cjs +55 -0
- package/dist/vendor/resources/beta/organization/compliance-settings.cjs +51 -0
- package/dist/vendor/resources/beta/organization/external-keys.cjs +118 -0
- package/dist/vendor/resources/beta/organization/federation/federation.cjs +49 -0
- package/dist/vendor/resources/beta/organization/federation/index.cjs +10 -0
- package/dist/vendor/resources/beta/organization/federation/issuers.cjs +168 -0
- package/dist/vendor/resources/beta/organization/federation/rules/index.cjs +8 -0
- package/dist/vendor/resources/beta/organization/federation/rules/rules.cjs +226 -0
- package/dist/vendor/resources/beta/organization/federation/rules/workspaces.cjs +113 -0
- package/dist/vendor/resources/beta/organization/federation/rules.cjs +18 -0
- package/dist/vendor/resources/beta/organization/federation.cjs +18 -0
- package/dist/vendor/resources/beta/organization/index.cjs +24 -0
- package/dist/vendor/resources/beta/organization/invites.cjs +75 -0
- package/dist/vendor/resources/beta/organization/organization.cjs +90 -0
- package/dist/vendor/resources/beta/organization/rate-limits.cjs +30 -0
- package/dist/vendor/resources/beta/organization/service-accounts/index.cjs +8 -0
- package/dist/vendor/resources/beta/organization/service-accounts/service-accounts.cjs +202 -0
- package/dist/vendor/resources/beta/organization/service-accounts/workspaces.cjs +124 -0
- package/dist/vendor/resources/beta/organization/service-accounts.cjs +18 -0
- package/dist/vendor/resources/beta/organization/users.cjs +66 -0
- package/dist/vendor/resources/beta/organization/workspaces/index.cjs +12 -0
- package/dist/vendor/resources/beta/organization/workspaces/members.cjs +101 -0
- package/dist/vendor/resources/beta/organization/workspaces/rate-limits.cjs +33 -0
- package/dist/vendor/resources/beta/organization/workspaces/service-accounts.cjs +188 -0
- package/dist/vendor/resources/beta/organization/workspaces/workspaces.cjs +140 -0
- package/dist/vendor/resources/beta/organization/workspaces.cjs +18 -0
- package/dist/vendor/resources/beta/organization.cjs +18 -0
- package/dist/vendor/resources/beta/sessions/events.cjs +119 -0
- package/dist/vendor/resources/beta/sessions/index.cjs +12 -0
- package/dist/vendor/resources/beta/sessions/resources.cjs +131 -0
- package/dist/vendor/resources/beta/sessions/sessions.cjs +186 -0
- package/dist/vendor/resources/beta/sessions/threads/events.cjs +60 -0
- package/dist/vendor/resources/beta/sessions/threads/index.cjs +8 -0
- package/dist/vendor/resources/beta/sessions/threads/threads.cjs +116 -0
- package/dist/vendor/resources/beta/sessions/threads.cjs +18 -0
- package/dist/vendor/resources/beta/sessions.cjs +18 -0
- package/dist/vendor/resources/beta/skills/index.cjs +8 -0
- package/dist/vendor/resources/beta/skills/skills.cjs +132 -0
- package/dist/vendor/resources/beta/skills/versions.cjs +128 -0
- package/dist/vendor/resources/beta/skills.cjs +18 -0
- package/dist/vendor/resources/beta/tunnels/certificates.cjs +128 -0
- package/dist/vendor/resources/beta/tunnels/index.cjs +8 -0
- package/dist/vendor/resources/beta/tunnels/tunnels.cjs +209 -0
- package/dist/vendor/resources/beta/tunnels.cjs +18 -0
- package/dist/vendor/resources/beta/user-profiles.cjs +117 -0
- package/dist/vendor/resources/beta/vaults/credentials.cjs +176 -0
- package/dist/vendor/resources/beta/vaults/index.cjs +8 -0
- package/dist/vendor/resources/beta/vaults/vaults.cjs +177 -0
- package/dist/vendor/resources/beta/vaults.cjs +18 -0
- package/dist/vendor/resources/beta/webhooks.cjs +32 -0
- package/dist/vendor/resources/beta.cjs +18 -0
- package/dist/vendor/resources/completions.cjs +22 -0
- package/dist/vendor/resources/files.cjs +51 -0
- package/dist/vendor/resources/index.cjs +31 -0
- package/dist/vendor/resources/messages/batches.cjs +160 -0
- package/dist/vendor/resources/messages/index.cjs +8 -0
- package/dist/vendor/resources/messages/messages.cjs +159 -0
- package/dist/vendor/resources/messages.cjs +18 -0
- package/dist/vendor/resources/models.cjs +44 -0
- package/dist/vendor/resources/shared.cjs +3 -0
- package/dist/vendor/resources/skills/index.cjs +8 -0
- package/dist/vendor/resources/skills/skills.cjs +72 -0
- package/dist/vendor/resources/skills/versions.cjs +40 -0
- package/dist/vendor/resources/skills.cjs +18 -0
- package/dist/vendor/resources/top-level.cjs +3 -0
- package/dist/vendor/resources.cjs +17 -0
- package/dist/vendor/streaming.cjs +18 -0
- package/dist/vendor/tools/agent-toolset/fs-util.cjs +193 -0
- package/dist/vendor/tools/agent-toolset/memories.cjs +916 -0
- package/dist/vendor/tools/agent-toolset/node.browser.cjs +147 -0
- package/dist/vendor/tools/agent-toolset/node.cjs +939 -0
- package/dist/vendor/tools/agent-toolset/skills.cjs +325 -0
- package/dist/vendor/tools/agent-toolset/sync-interval.cjs +28 -0
- package/dist/vendor/tools/memory/node.browser.cjs +48 -0
- package/dist/vendor/tools/memory/node.cjs +364 -0
- package/dist/vendor/uploads.cjs +18 -0
- package/dist/vendor/version.cjs +4 -0
- package/package.json +12 -13
- package/sdk.cjs +0 -14271
- package/sdk.cjs.map +0 -149
- package/sdk.d.ts +0 -10866
- package/sdk.mjs +0 -68
- package/sdk.mjs.map +0 -149
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.resolveCredentialsFromConfig = resolveCredentialsFromConfig;
|
|
37
|
+
exports.defaultCredentials = defaultCredentials;
|
|
38
|
+
const env_1 = require("../../internal/utils/env");
|
|
39
|
+
const credentials_1 = require("../../core/credentials");
|
|
40
|
+
const types_1 = require("./types");
|
|
41
|
+
const time_1 = require("../../internal/utils/time");
|
|
42
|
+
const identity_token_1 = require("./identity-token");
|
|
43
|
+
const oidc_federation_1 = require("./oidc-federation");
|
|
44
|
+
const user_oauth_1 = require("./user-oauth");
|
|
45
|
+
function resolveCredentialsFromConfig(config, options) {
|
|
46
|
+
const credentialsPath = config.authentication.credentials_path ?? null;
|
|
47
|
+
const effectiveBaseURL = (config.base_url || options.baseURL).replace(/\/+$/, '');
|
|
48
|
+
const provider = buildProvider(config, credentialsPath, effectiveBaseURL, options);
|
|
49
|
+
const extraHeaders = {};
|
|
50
|
+
// For federation profiles workspace_id is sent in the jwt-bearer exchange
|
|
51
|
+
// body, not as a request header (the minted token is already
|
|
52
|
+
// workspace-scoped, so the header would be ignored).
|
|
53
|
+
if (config.workspace_id && config.authentication.type === 'user_oauth') {
|
|
54
|
+
extraHeaders['puku-workspace-id'] = config.workspace_id;
|
|
55
|
+
}
|
|
56
|
+
// Surface the profile's own base_url (not the options.baseURL fallback) so
|
|
57
|
+
// the client can adopt it for outbound API requests when the caller didn't
|
|
58
|
+
// pin one explicitly. Echoing options.baseURL back would defeat precedence.
|
|
59
|
+
return { provider, extraHeaders, baseURL: config.base_url || undefined };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Resolves a {@link CredentialResult} from the environment. Returns `null`
|
|
63
|
+
* when no credentials can be resolved.
|
|
64
|
+
*
|
|
65
|
+
* Resolution order:
|
|
66
|
+
*
|
|
67
|
+
* 1. Config file for the active profile (or the explicit `profile` argument)
|
|
68
|
+
* → dispatch on `authentication.type` (`oidc_federation`, `user_oauth`)
|
|
69
|
+
* 2. Environment variables `PUKU_FEDERATION_RULE_ID` +
|
|
70
|
+
* `PUKU_ORGANIZATION_ID` (+ identity token) → OIDC federation
|
|
71
|
+
* 3. Nothing matches → `null`
|
|
72
|
+
*
|
|
73
|
+
* Passing `profile` selects `<config_dir>/configs/<profile>.json` directly,
|
|
74
|
+
* skipping `PUKU_PROFILE` / `active_config` resolution.
|
|
75
|
+
*/
|
|
76
|
+
async function defaultCredentials(options, profile) {
|
|
77
|
+
const loaded = await (0, credentials_1.loadConfigWithSource)(profile);
|
|
78
|
+
if (!loaded) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const { config, fromFile } = loaded;
|
|
82
|
+
// For file-loaded configs, default credentials_path to the per-profile
|
|
83
|
+
// location so user_oauth and federation caching work. Shallow-clone first
|
|
84
|
+
// so callers that retain a reference to the loaded config don't observe the
|
|
85
|
+
// patched-in default.
|
|
86
|
+
//
|
|
87
|
+
// Env-only credentials (no profile file on disk) skip the disk cache —
|
|
88
|
+
// matching the other SDKs. A disk cache keyed by profile path would
|
|
89
|
+
// re-serve a stale token after a change to PUKU_WORKSPACE_ID (or
|
|
90
|
+
// PUKU_ORGANIZATION_ID / PUKU_FEDERATION_RULE_ID) until the
|
|
91
|
+
// cached token expired, so the env-only chain stays in-memory only.
|
|
92
|
+
const withPath = config.authentication.credentials_path || !fromFile ?
|
|
93
|
+
config
|
|
94
|
+
: {
|
|
95
|
+
...config,
|
|
96
|
+
authentication: {
|
|
97
|
+
...config.authentication,
|
|
98
|
+
credentials_path: (await (0, credentials_1.getCredentialsPath)(config, profile)) ?? undefined,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
return resolveCredentialsFromConfig(withPath, options);
|
|
102
|
+
}
|
|
103
|
+
function buildProvider(config, credentialsPath, baseURL, options) {
|
|
104
|
+
switch (config.authentication.type) {
|
|
105
|
+
case 'oidc_federation': {
|
|
106
|
+
const auth = config.authentication;
|
|
107
|
+
const identityProvider = resolveIdentityTokenProvider(auth);
|
|
108
|
+
if (!identityProvider) {
|
|
109
|
+
throw new types_1.WorkloadIdentityError('oidc_federation config requires an identity token (set authentication.identity_token, ' +
|
|
110
|
+
'PUKU_IDENTITY_TOKEN_FILE, or PUKU_IDENTITY_TOKEN)');
|
|
111
|
+
}
|
|
112
|
+
if (!auth.federation_rule_id) {
|
|
113
|
+
throw new types_1.WorkloadIdentityError("oidc_federation config requires 'federation_rule_id'. Set it in authentication.federation_rule_id in your profile, or via PUKU_FEDERATION_RULE_ID (profile takes precedence).");
|
|
114
|
+
}
|
|
115
|
+
if (!config.organization_id) {
|
|
116
|
+
throw new types_1.WorkloadIdentityError('oidc_federation config requires organization_id (set PUKU_ORGANIZATION_ID or config.organization_id)');
|
|
117
|
+
}
|
|
118
|
+
const exchange = (0, oidc_federation_1.oidcFederationProvider)({
|
|
119
|
+
identityTokenProvider: identityProvider,
|
|
120
|
+
federationRuleId: auth.federation_rule_id,
|
|
121
|
+
organizationId: config.organization_id,
|
|
122
|
+
serviceAccountId: auth.service_account_id,
|
|
123
|
+
workspaceId: config.workspace_id,
|
|
124
|
+
baseURL,
|
|
125
|
+
fetch: options.fetch,
|
|
126
|
+
userAgent: options.userAgent,
|
|
127
|
+
});
|
|
128
|
+
// If there's a credentials file path, wrap the exchange with file caching
|
|
129
|
+
// (check file for fresh token before exchanging, write back after).
|
|
130
|
+
if (credentialsPath) {
|
|
131
|
+
return cachedExchangeProvider(exchange, credentialsPath, options.onCacheWriteError, options.onSafetyWarning);
|
|
132
|
+
}
|
|
133
|
+
return exchange;
|
|
134
|
+
}
|
|
135
|
+
case 'user_oauth': {
|
|
136
|
+
if (!credentialsPath) {
|
|
137
|
+
throw new types_1.WorkloadIdentityError('user_oauth config requires authentication.credentials_path ' +
|
|
138
|
+
'(or load via a profile so it defaults to <config_dir>/credentials/<profile>.json)');
|
|
139
|
+
}
|
|
140
|
+
return (0, user_oauth_1.userOAuthProvider)({
|
|
141
|
+
credentialsPath,
|
|
142
|
+
clientId: config.authentication.client_id,
|
|
143
|
+
baseURL,
|
|
144
|
+
fetch: options.fetch,
|
|
145
|
+
userAgent: options.userAgent,
|
|
146
|
+
onSafetyWarning: options.onSafetyWarning,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
default: {
|
|
150
|
+
const t = config.authentication.type;
|
|
151
|
+
throw new types_1.WorkloadIdentityError(`authentication.type "${t}" is not a known authentication type`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Resolves the identity token provider from config fields or environment variables.
|
|
157
|
+
*
|
|
158
|
+
* Resolution order:
|
|
159
|
+
* 1. `identity_token.path` from the config (source: "file")
|
|
160
|
+
* 2. `PUKU_IDENTITY_TOKEN_FILE` env var
|
|
161
|
+
* 3. `PUKU_IDENTITY_TOKEN` env var (static value)
|
|
162
|
+
*/
|
|
163
|
+
function resolveIdentityTokenProvider(auth) {
|
|
164
|
+
if (auth.identity_token) {
|
|
165
|
+
// Cast needed to stringify an unknown source value for the error message:
|
|
166
|
+
// the on-disk JSON may contain a source this SDK version doesn't know about.
|
|
167
|
+
const source = auth.identity_token.source;
|
|
168
|
+
if (source !== 'file') {
|
|
169
|
+
throw new types_1.WorkloadIdentityError(`identity_token.source "${source}" is not supported by this SDK version (only "file")`);
|
|
170
|
+
}
|
|
171
|
+
if (!auth.identity_token.path) {
|
|
172
|
+
throw new types_1.WorkloadIdentityError(`identity_token.source "file" requires a non-empty path`);
|
|
173
|
+
}
|
|
174
|
+
return (0, identity_token_1.identityTokenFromFile)(auth.identity_token.path);
|
|
175
|
+
}
|
|
176
|
+
const tokenFile = (0, env_1.readEnv)('PUKU_IDENTITY_TOKEN_FILE');
|
|
177
|
+
if (tokenFile) {
|
|
178
|
+
return (0, identity_token_1.identityTokenFromFile)(tokenFile);
|
|
179
|
+
}
|
|
180
|
+
const tokenValue = (0, env_1.readEnv)('PUKU_IDENTITY_TOKEN');
|
|
181
|
+
if (tokenValue) {
|
|
182
|
+
return (0, identity_token_1.identityTokenFromValue)(tokenValue);
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Wraps a federation exchange provider with credential file caching.
|
|
188
|
+
* Checks the file for a fresh token before exchanging, and writes the
|
|
189
|
+
* result back after a successful exchange (best-effort, atomic replace).
|
|
190
|
+
*
|
|
191
|
+
* Note: this is not cross-process serialized — two SDK instances that
|
|
192
|
+
* miss the cache simultaneously will both perform a full exchange and
|
|
193
|
+
* the last writer wins. That is acceptable: federation exchanges are
|
|
194
|
+
* idempotent and the cache is an optimization, not a correctness gate.
|
|
195
|
+
*/
|
|
196
|
+
function cachedExchangeProvider(exchange, credentialsPath, onCacheWriteError, onSafetyWarning) {
|
|
197
|
+
return async (opts) => {
|
|
198
|
+
const { fs } = await Promise.resolve().then(() => __importStar(require('../../internal/node')));
|
|
199
|
+
await (0, types_1.checkCredentialsFileSafety)(credentialsPath, onSafetyWarning);
|
|
200
|
+
// Try cached credentials file
|
|
201
|
+
let existing;
|
|
202
|
+
try {
|
|
203
|
+
const raw = await fs.promises.readFile(credentialsPath, 'utf-8');
|
|
204
|
+
existing = JSON.parse(raw);
|
|
205
|
+
const token = existing?.['access_token'];
|
|
206
|
+
if (token && !opts?.forceRefresh) {
|
|
207
|
+
const expiresAt = existing?.['expires_at'];
|
|
208
|
+
if (expiresAt == null || (0, time_1.nowAsSeconds)() < expiresAt - types_1.MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
|
|
209
|
+
return { token, expiresAt: expiresAt ?? null };
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch (err) {
|
|
214
|
+
// ENOENT or invalid-JSON → no usable cache, exchange fresh. Other
|
|
215
|
+
// errors (EACCES, EISDIR, …) indicate a broken cache path; surface to
|
|
216
|
+
// the optional hook so they're at least debuggable, then proceed.
|
|
217
|
+
const code = err?.code;
|
|
218
|
+
if (code !== 'ENOENT' && !(err instanceof SyntaxError)) {
|
|
219
|
+
onCacheWriteError?.(err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Exchange for a new token
|
|
223
|
+
const result = await exchange(opts);
|
|
224
|
+
// Write cache back (best-effort). Preserve any unknown keys from the
|
|
225
|
+
// existing file (notably refresh_token, in the unlikely case this path
|
|
226
|
+
// is shared with a user_oauth profile) so the federation cache writer
|
|
227
|
+
// doesn't clobber material it didn't own.
|
|
228
|
+
try {
|
|
229
|
+
await (0, types_1.writeCredentialsFileAtomic)(credentialsPath, {
|
|
230
|
+
...(existing ?? {}),
|
|
231
|
+
version: credentials_1.CREDENTIALS_FILE_VERSION,
|
|
232
|
+
type: 'oauth_token',
|
|
233
|
+
access_token: result.token,
|
|
234
|
+
expires_at: result.expiresAt,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
catch (err) {
|
|
238
|
+
// Best-effort caching: surface to the optional hook but never fail
|
|
239
|
+
// the exchange itself.
|
|
240
|
+
onCacheWriteError?.(err);
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
};
|
|
244
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.identityTokenFromFile = identityTokenFromFile;
|
|
37
|
+
exports.identityTokenFromValue = identityTokenFromValue;
|
|
38
|
+
const error_1 = require("../../core/error");
|
|
39
|
+
/**
|
|
40
|
+
* Reads a JWT from a file on every call. Supports automatic rotation
|
|
41
|
+
* (e.g. Kubernetes projected service-account tokens).
|
|
42
|
+
*/
|
|
43
|
+
function identityTokenFromFile(path) {
|
|
44
|
+
if (!path) {
|
|
45
|
+
throw new error_1.PukuError('Identity token file path is empty');
|
|
46
|
+
}
|
|
47
|
+
return async () => {
|
|
48
|
+
const { fs } = await Promise.resolve().then(() => __importStar(require('../../internal/node')));
|
|
49
|
+
let content;
|
|
50
|
+
try {
|
|
51
|
+
content = await fs.promises.readFile(path, 'utf-8');
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
throw new error_1.PukuError(`Failed to read identity token file at ${path}: ${err}`);
|
|
55
|
+
}
|
|
56
|
+
const token = content.trim();
|
|
57
|
+
if (!token) {
|
|
58
|
+
throw new error_1.PukuError(`Identity token file at ${path} is empty`);
|
|
59
|
+
}
|
|
60
|
+
return token;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Wraps a static JWT string as an {@link IdentityTokenProvider}.
|
|
65
|
+
*/
|
|
66
|
+
function identityTokenFromValue(token) {
|
|
67
|
+
if (!token) {
|
|
68
|
+
throw new error_1.PukuError('Identity token value is empty');
|
|
69
|
+
}
|
|
70
|
+
return () => token;
|
|
71
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oidcFederationProvider = oidcFederationProvider;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const time_1 = require("../../internal/utils/time");
|
|
6
|
+
const version_1 = require("../../version");
|
|
7
|
+
/**
|
|
8
|
+
* Exchanges an external OIDC JWT for an PukuAI access token via the
|
|
9
|
+
* RFC 7523 jwt-bearer grant.
|
|
10
|
+
*
|
|
11
|
+
* Each invocation performs a fresh token exchange. Wrap in a
|
|
12
|
+
* {@link TokenCache} to avoid exchanging on every request.
|
|
13
|
+
*
|
|
14
|
+
* Federation grants do not return a refresh token — callers re-exchange
|
|
15
|
+
* their assertion on expiry.
|
|
16
|
+
*/
|
|
17
|
+
function oidcFederationProvider(config) {
|
|
18
|
+
return async () => {
|
|
19
|
+
(0, types_1.requireSecureTokenEndpoint)(config.baseURL);
|
|
20
|
+
const jwt = await config.identityTokenProvider();
|
|
21
|
+
// The token endpoint enforces a 16 KiB assertion limit; surface a clear
|
|
22
|
+
// client-side error so misconfigured projected-token sources are
|
|
23
|
+
// diagnosable without a server round-trip.
|
|
24
|
+
if (jwt.length > 16 * 1024) {
|
|
25
|
+
throw new types_1.WorkloadIdentityError(`Identity token is ${Math.ceil(jwt.length / 1024)} KiB, exceeds the 16 KiB assertion limit`);
|
|
26
|
+
}
|
|
27
|
+
const body = {
|
|
28
|
+
grant_type: types_1.GRANT_TYPE_JWT_BEARER,
|
|
29
|
+
assertion: jwt,
|
|
30
|
+
federation_rule_id: config.federationRuleId,
|
|
31
|
+
organization_id: config.organizationId,
|
|
32
|
+
};
|
|
33
|
+
if (config.serviceAccountId) {
|
|
34
|
+
body['service_account_id'] = config.serviceAccountId;
|
|
35
|
+
}
|
|
36
|
+
if (config.workspaceId) {
|
|
37
|
+
body['workspace_id'] = config.workspaceId;
|
|
38
|
+
}
|
|
39
|
+
const url = `${config.baseURL}${types_1.TOKEN_ENDPOINT}`;
|
|
40
|
+
let resp;
|
|
41
|
+
try {
|
|
42
|
+
resp = await config.fetch(url, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: {
|
|
45
|
+
'Content-Type': 'application/json',
|
|
46
|
+
'puku-beta': `${types_1.OAUTH_API_BETA_HEADER},${types_1.FEDERATION_BETA_HEADER}`,
|
|
47
|
+
'User-Agent': config.userAgent || `puku-ai-sdk-typescript/${version_1.VERSION} oidcFederationProvider`,
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify(body),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
throw new types_1.WorkloadIdentityError(`Failed to reach token endpoint ${url}: ${err}`);
|
|
54
|
+
}
|
|
55
|
+
const requestId = resp.headers.get('Request-Id');
|
|
56
|
+
if (!resp.ok) {
|
|
57
|
+
const text = await resp.text().catch(() => '');
|
|
58
|
+
const redacted = (0, types_1.redactSensitive)(text);
|
|
59
|
+
// A 401 is hard to debug from the status code alone, so surface
|
|
60
|
+
// guidance: check the federation rule, optionally set a workspace ID
|
|
61
|
+
// (the most common fix when no workspaceId is configured), and point at
|
|
62
|
+
// the Workload identity page in Puku Console for the server-side
|
|
63
|
+
// authentication event log. Other statuses (5xx, 400, ...) get no hint.
|
|
64
|
+
let hint = '';
|
|
65
|
+
if (resp.status === 401) {
|
|
66
|
+
const hintMiddle = config.workspaceId ? '' : ("If your federation rule is scoped to multiple workspaces, set the PUKU_WORKSPACE_ID environment variable, the 'workspace_id' config key, or the `workspaceId` option. ");
|
|
67
|
+
hint = ` Ensure your federation rule matches your identity token. ${hintMiddle}View your authentication events in the Workload identity page of Puku Console for more details.`;
|
|
68
|
+
}
|
|
69
|
+
throw new types_1.WorkloadIdentityError(`Token exchange failed with status ${resp.status}${requestId ? ` (request-id ${requestId})` : ''}: ${redacted}${hint}`, resp.status, redacted, requestId);
|
|
70
|
+
}
|
|
71
|
+
const data = await (0, types_1.parseTokenResponse)(resp, requestId);
|
|
72
|
+
const expiresIn = Number(data.expires_in);
|
|
73
|
+
if (!Number.isFinite(expiresIn)) {
|
|
74
|
+
throw new types_1.WorkloadIdentityError(`Token endpoint response missing required fields: ${JSON.stringify((0, types_1.redactSensitive)(data))}`, resp.status, (0, types_1.redactSensitive)(data), requestId);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
token: data.access_token,
|
|
78
|
+
expiresAt: (0, time_1.nowAsSeconds)() + expiresIn,
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TokenCache = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const time_1 = require("../../internal/utils/time");
|
|
6
|
+
/**
|
|
7
|
+
* Wraps an {@link AccessTokenProvider} with two-tier proactive refresh
|
|
8
|
+
* and concurrent deduplication.
|
|
9
|
+
*
|
|
10
|
+
* Refresh policy on each {@link getToken} call:
|
|
11
|
+
*
|
|
12
|
+
* - No cached token → call provider (blocking), cache, return.
|
|
13
|
+
* - Cached with `expiresAt == null` → return cached forever.
|
|
14
|
+
* - More than 120s remaining → return cached.
|
|
15
|
+
* - 30–120s remaining (advisory window) → return stale token immediately,
|
|
16
|
+
* kick off background refresh. On failure, log and keep stale.
|
|
17
|
+
* - Less than 30s remaining or expired (mandatory) → block and refresh.
|
|
18
|
+
* On failure, throw.
|
|
19
|
+
*
|
|
20
|
+
* Concurrent mandatory callers coalesce into a single provider call.
|
|
21
|
+
*/
|
|
22
|
+
class TokenCache {
|
|
23
|
+
provider;
|
|
24
|
+
cached = null;
|
|
25
|
+
pendingRefresh = null;
|
|
26
|
+
nextForce = false;
|
|
27
|
+
lastAdvisoryError = 0;
|
|
28
|
+
onAdvisoryRefreshError;
|
|
29
|
+
constructor(provider, onAdvisoryRefreshError) {
|
|
30
|
+
this.provider = provider;
|
|
31
|
+
this.onAdvisoryRefreshError = onAdvisoryRefreshError;
|
|
32
|
+
}
|
|
33
|
+
async getToken() {
|
|
34
|
+
const force = this.nextForce;
|
|
35
|
+
this.nextForce = false;
|
|
36
|
+
const cached = this.cached;
|
|
37
|
+
if (force || cached == null) {
|
|
38
|
+
const token = await this.refresh(force);
|
|
39
|
+
return token.token;
|
|
40
|
+
}
|
|
41
|
+
if (cached.expiresAt == null) {
|
|
42
|
+
return cached.token;
|
|
43
|
+
}
|
|
44
|
+
const remaining = cached.expiresAt - (0, time_1.nowAsSeconds)();
|
|
45
|
+
if (remaining > types_1.ADVISORY_REFRESH_THRESHOLD_IN_SECONDS) {
|
|
46
|
+
return cached.token;
|
|
47
|
+
}
|
|
48
|
+
if (remaining > types_1.MANDATORY_REFRESH_THRESHOLD_IN_SECONDS) {
|
|
49
|
+
this.backgroundRefresh();
|
|
50
|
+
return cached.token;
|
|
51
|
+
}
|
|
52
|
+
const token = await this.refresh();
|
|
53
|
+
return token.token;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Clears the cached token and marks the next {@link getToken} as a forced
|
|
57
|
+
* refresh, so the underlying provider bypasses any on-disk freshness check.
|
|
58
|
+
* Called after a 401 — the server has just told us the token is bad even
|
|
59
|
+
* if its `expires_at` still looks fresh.
|
|
60
|
+
*/
|
|
61
|
+
invalidate() {
|
|
62
|
+
this.cached = null;
|
|
63
|
+
this.nextForce = true;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Mandatory refresh. Joins any in-flight refresh unless forced — a forced
|
|
67
|
+
* refresh must not coalesce into a non-forced one that may re-serve the
|
|
68
|
+
* same stale disk token.
|
|
69
|
+
*/
|
|
70
|
+
refresh(force = false) {
|
|
71
|
+
if (this.pendingRefresh && !force) {
|
|
72
|
+
return this.pendingRefresh;
|
|
73
|
+
}
|
|
74
|
+
return this.doRefresh(force);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Advisory background refresh. Shares the same in-flight promise as
|
|
78
|
+
* mandatory refreshes for deduplication, but swallows errors so the
|
|
79
|
+
* stale cached token keeps being served. Backs off for
|
|
80
|
+
* {@link ADVISORY_REFRESH_BACKOFF_IN_SECONDS} after a failure so an
|
|
81
|
+
* outage during the advisory window doesn't hammer the token endpoint.
|
|
82
|
+
*/
|
|
83
|
+
backgroundRefresh() {
|
|
84
|
+
if (this.pendingRefresh) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if ((0, time_1.nowAsSeconds)() - this.lastAdvisoryError < types_1.ADVISORY_REFRESH_BACKOFF_IN_SECONDS) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.doRefresh().catch((err) => {
|
|
91
|
+
this.lastAdvisoryError = (0, time_1.nowAsSeconds)();
|
|
92
|
+
// Advisory failure: keep serving the stale cached token, but surface
|
|
93
|
+
// the error to the caller-provided hook so it can be logged.
|
|
94
|
+
this.onAdvisoryRefreshError?.(err);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Core refresh. Sets {@link pendingRefresh} so concurrent callers
|
|
99
|
+
* (both advisory and mandatory) coalesce into a single provider call.
|
|
100
|
+
*/
|
|
101
|
+
doRefresh(force = false) {
|
|
102
|
+
this.pendingRefresh = this.provider(force ? { forceRefresh: true } : undefined).then((token) => {
|
|
103
|
+
this.cached = token;
|
|
104
|
+
this.pendingRefresh = null;
|
|
105
|
+
return token;
|
|
106
|
+
}, (err) => {
|
|
107
|
+
this.pendingRefresh = null;
|
|
108
|
+
throw err;
|
|
109
|
+
});
|
|
110
|
+
return this.pendingRefresh;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.TokenCache = TokenCache;
|