@toloka-platform/sdk 0.27.1 → 0.28.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.
Binary file
@@ -1 +1 @@
1
- cb304ddc77ab9aaab1ddb3281f9dbb928fcf3d7e714ba36473487b71f257cb67 toloka.js.jsc
1
+ b57537fed914343fa7ebef1b02691a9001f7fa12a75861ca53909d331c2534ab toloka.js.jsc
@@ -1 +1 @@
1
- f4af7967fea76fba203daa8572ff19599c8cfddbb7e7157c741a9ed22e2b8cb7 toloka.js
1
+ 30ee2bd68395d747b4af5e6d5a484bec485038ec9bccd23951dbd45ca759e317 toloka.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toloka-platform/sdk",
3
- "version": "0.27.1",
3
+ "version": "0.28.0",
4
4
  "private": false,
5
5
  "description": "Build, inspect, and operate Toloka pipeline projects as TypeScript source. The public API may change between minor versions before 1.0.",
6
6
  "license": "Apache-2.0",
@@ -78,6 +78,6 @@
78
78
  "bun": ">=1.4.0"
79
79
  },
80
80
  "tolokaBuild": {
81
- "commit": "eda5e8f0913b975d317c46a9102b71b681f1ec0b"
81
+ "commit": "dfa95a1d978ea42cc4d598c58f3655a99dd1a777"
82
82
  }
83
83
  }
@@ -11,7 +11,7 @@
11
11
  "testing",
12
12
  "checks"
13
13
  ],
14
- "version": "0.27.1",
14
+ "version": "0.28.0",
15
15
  "digest": "fecd55a4ddd06d4a37a54defa053b95bcd7f509117ea36640f5ca630d2f14bf1",
16
16
  "files": [
17
17
  "SKILL.md",
@@ -33,7 +33,7 @@
33
33
  "bug",
34
34
  "report"
35
35
  ],
36
- "version": "0.27.1",
36
+ "version": "0.28.0",
37
37
  "digest": "70f0bd2c80b32f87896aeb27c363b8a2d37f40e22424f57fe1aad142e125ad67",
38
38
  "files": [
39
39
  "SKILL.md",
@@ -50,7 +50,7 @@
50
50
  "getting-started",
51
51
  "agent"
52
52
  ],
53
- "version": "0.27.1",
53
+ "version": "0.28.0",
54
54
  "digest": "eb543135ef51533ecea48eacc137b80ca1ed8211d1eed0575d0ab24a26de136a",
55
55
  "files": [
56
56
  "SKILL.md"
package/src/cli/auth.ts CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  registrationUrlFor,
24
24
  resolveBaseUrl,
25
25
  resolveInternalBaseUrl,
26
+ storedApiKeyFor,
26
27
  } from "./config";
27
28
  import {
28
29
  clearStoredCredentials,
@@ -197,7 +198,7 @@ const registryFor = async (
197
198
  environment: CliEnvironment,
198
199
  baseUrl: string,
199
200
  ): Promise<Pick<StoredCredentials, "current" | "projects">> => {
200
- const existing = await inspectCredentialsFile(credentialsPath(environment));
201
+ const existing = inspectCredentialsFile(credentialsPath(environment));
201
202
  if (typeof existing !== "object") return {};
202
203
  if (normalizeBaseUrl(existing.credentials.baseUrl) !== normalizeBaseUrl(baseUrl)) return {};
203
204
  return {
@@ -345,7 +346,7 @@ const loginInBrowser = async (
345
346
 
346
347
  const readValidStoredCredentials = async (environment: CliEnvironment): Promise<StoredCredentials | undefined> => {
347
348
  for (const path of storedCredentialsPaths(environment)) {
348
- const inspected = await inspectCredentialsFile(path);
349
+ const inspected = inspectCredentialsFile(path);
349
350
  if (inspected === "missing") continue;
350
351
  return typeof inspected === "object" ? inspected.credentials : undefined;
351
352
  }
@@ -462,7 +463,7 @@ const firstDefined = <T>(candidates: ReadonlyArray<T | undefined>): T | undefine
462
463
  const resolveStatusCredential = (
463
464
  options: AuthStatusOptions,
464
465
  environment: CliEnvironment,
465
- found: FoundCredentials | undefined,
466
+ storedApiKey: string | undefined,
466
467
  storedPath: string,
467
468
  ): ResolvedCredential | undefined =>
468
469
  firstDefined<ResolvedCredential>([
@@ -485,11 +486,11 @@ const resolveStatusCredential = (
485
486
  source: "from TOLOKA_API_KEY",
486
487
  environmentVariable: "TOLOKA_API_KEY",
487
488
  },
488
- found?.credentials.apiKey === undefined
489
+ storedApiKey === undefined
489
490
  ? undefined
490
491
  : {
491
492
  type: "apiKey",
492
- value: found.credentials.apiKey,
493
+ value: storedApiKey,
493
494
  source: `stored in ${storedPath}`,
494
495
  },
495
496
  ]);
@@ -604,13 +605,20 @@ export async function authStatus(options: AuthStatusOptions = {}): Promise<AuthS
604
605
  ));
605
606
  const { found, warning } = credentialLookup;
606
607
  const storedPath = found?.path ?? credentialsPath(environment);
607
- const credential = resolveStatusCredential(options, environment, found, storedPath);
608
608
  const platform = resolvePlatform(options, environment, found);
609
+ const storedApiKey = storedApiKeyFor(platform.value, found?.credentials);
610
+ const credential = resolveStatusCredential(options, environment, storedApiKey, storedPath);
609
611
  const browser =
610
612
  credential === undefined ? await findBrowserLogin(platform.value, environment) : undefined;
611
613
  const projectLine = formatProjectContextLine(options.project, found?.credentials);
612
614
 
613
615
  const lines: string[] = warning === undefined ? [] : [warning, ""];
616
+ if (found?.credentials.apiKey !== undefined && storedApiKey === undefined && credential === undefined) {
617
+ lines.push(
618
+ `The key stored in ${found.path} is for ${found.credentials.baseUrl}, so it is not sent to ${platform.value}.`,
619
+ "",
620
+ );
621
+ }
614
622
 
615
623
  if (credential === undefined && browser === undefined) {
616
624
  lines.push(
@@ -647,7 +655,7 @@ export async function authStatus(options: AuthStatusOptions = {}): Promise<AuthS
647
655
  }
648
656
  lines.push(projectLine);
649
657
 
650
- if (found?.credentials.apiKey !== undefined && credential?.environmentVariable !== undefined) {
658
+ if (found !== undefined && storedApiKey !== undefined && credential?.environmentVariable !== undefined) {
651
659
  lines.push(
652
660
  "",
653
661
  `${credential.environmentVariable} is set, so the key stored in ${found.path} is ignored.`,
package/src/cli/config.ts CHANGED
@@ -1,5 +1,10 @@
1
+ import { existsSync } from "node:fs";
2
+ import type { ClientConfig } from "../client";
1
3
  import type { TolokaState } from "../state";
2
- import type { StoredCredentials } from "./credentials";
4
+ import { browserSessionPath, createBrowserSessionAuth } from "./browser-session";
5
+ import { CredentialsError, findStoredCredentials, normalizeBaseUrl } from "./credentials";
6
+ import type { CliEnvironment, StoredCredentials } from "./credentials";
7
+ import { browserLoginProviderFor } from "./keycloak";
3
8
  import type { BrowserLoginProvider } from "./keycloak";
4
9
  import { resolveProjectContext } from "./project-context";
5
10
  import { assertCli } from "./cli-error";
@@ -66,6 +71,20 @@ export const MISSING_CREDENTIALS_MESSAGE = [
66
71
  "Run toloka auth-status to see what is in effect.",
67
72
  ].join("\n");
68
73
 
74
+ export const storedApiKeyFor = (baseUrl: string, stored: StoredCredentials | undefined): string | undefined =>
75
+ stored !== undefined && normalizeBaseUrl(baseUrl) === normalizeBaseUrl(stored.baseUrl)
76
+ ? stored.apiKey
77
+ : undefined;
78
+
79
+ const missingCredentialMessage = (
80
+ baseUrl: string,
81
+ stored: StoredCredentials | undefined,
82
+ notLoggedIn: string,
83
+ ): string =>
84
+ stored?.apiKey !== undefined && storedApiKeyFor(baseUrl, stored) === undefined
85
+ ? `The API key saved by toloka login is for ${stored.baseUrl}, so it is not sent to ${baseUrl}. Run toloka login for ${baseUrl}, or set TOLOKA_API_KEY.`
86
+ : notLoggedIn;
87
+
69
88
  export const resolveConfiguredCredential = (
70
89
  flags: TolokaCliFlags,
71
90
  environment: TolokaEnvironment,
@@ -75,7 +94,7 @@ export const resolveConfiguredCredential = (
75
94
  if (environment.TOLOKA_SCOPED_TOKEN !== undefined) {
76
95
  return { type: "scopedToken", scopedToken: environment.TOLOKA_SCOPED_TOKEN };
77
96
  }
78
- const apiKey = environment.TOLOKA_API_KEY ?? stored?.apiKey;
97
+ const apiKey = environment.TOLOKA_API_KEY ?? storedApiKeyFor(resolveBaseUrl(flags, environment, stored), stored);
79
98
  return apiKey === undefined ? undefined : { type: "apiKey", apiKey };
80
99
  };
81
100
 
@@ -109,6 +128,51 @@ export function resolveTolokaConfig(
109
128
  const credential: CliCredential | undefined =
110
129
  configuredCredential ??
111
130
  (browserLogin === undefined ? undefined : { type: "browser", provider: browserLogin });
112
- assertCli(credential, MISSING_CREDENTIALS_MESSAGE, { hint: { disabled: true } });
131
+ assertCli(credential, missingCredentialMessage(baseUrl, stored, MISSING_CREDENTIALS_MESSAGE), {
132
+ hint: { disabled: true },
133
+ });
113
134
  return { baseUrl, credential, ...(project === undefined ? {} : { projectId: project.id }) };
114
135
  }
136
+
137
+ export const clientConfigFor = (
138
+ baseUrl: string,
139
+ credential: CliCredential,
140
+ environment: CliEnvironment,
141
+ ): ClientConfig =>
142
+ credential.type === "apiKey"
143
+ ? { baseUrl, apiKey: credential.apiKey }
144
+ : credential.type === "scopedToken"
145
+ ? { baseUrl, scopedToken: credential.scopedToken }
146
+ : {
147
+ baseUrl,
148
+ accessToken: createBrowserSessionAuth({ provider: credential.provider, baseUrl, environment }),
149
+ };
150
+
151
+ export const SDK_NOT_LOGGED_IN_MESSAGE =
152
+ "Not logged in. Run toloka login, or set TOLOKA_API_KEY for a script that runs without you.";
153
+
154
+ const readLoginCredentials = (environment: TolokaEnvironment): StoredCredentials | undefined => {
155
+ try {
156
+ return findStoredCredentials(environment)?.credentials;
157
+ } catch (error) {
158
+ if (error instanceof CredentialsError && resolveConfiguredCredential({}, environment) !== undefined) {
159
+ return undefined;
160
+ }
161
+ throw error;
162
+ }
163
+ };
164
+
165
+ export const resolveLoginClientConfig = (environment: TolokaEnvironment): ClientConfig => {
166
+ const stored = readLoginCredentials(environment);
167
+ const baseUrl = resolveBaseUrl({}, environment, stored);
168
+ const provider = browserLoginProviderFor(baseUrl);
169
+ const browserCredential: CliCredential | undefined =
170
+ provider !== undefined && existsSync(browserSessionPath(provider, environment))
171
+ ? { type: "browser", provider }
172
+ : undefined;
173
+ const credential = resolveConfiguredCredential({}, environment, stored) ?? browserCredential;
174
+ assertCli(credential, missingCredentialMessage(baseUrl, stored, SDK_NOT_LOGGED_IN_MESSAGE), {
175
+ hint: { disabled: true },
176
+ });
177
+ return clientConfigFor(baseUrl, credential, environment);
178
+ };
@@ -8,10 +8,11 @@ import type { TolokaState } from "../state";
8
8
  import type { AuthChoose, AuthPrompt } from "./auth";
9
9
  import type { CliAnalytics } from "./analytics";
10
10
  import type { BrowserLoginFlow } from "./browser-login";
11
- import { createBrowserSessionAuth, findBrowserLogin } from "./browser-session";
11
+ import { findBrowserLogin } from "./browser-session";
12
12
  import type { checkProject } from "./check";
13
13
  import type { DescribeTaskUiComponents } from "../testing/task-ui-browser";
14
14
  import {
15
+ clientConfigFor,
15
16
  PRODUCTION_BASE_URL,
16
17
  resolveBaseUrl,
17
18
  resolveConfiguredCredential,
@@ -234,20 +235,7 @@ export const resolveClient = async (
234
235
  const { baseUrl, credential } = config;
235
236
  const client =
236
237
  context.injectedClient ??
237
- createClient(
238
- credential.type === "apiKey"
239
- ? { baseUrl, apiKey: credential.apiKey }
240
- : credential.type === "scopedToken"
241
- ? { baseUrl, scopedToken: credential.scopedToken }
242
- : {
243
- baseUrl,
244
- accessToken: createBrowserSessionAuth({
245
- provider: credential.provider,
246
- baseUrl,
247
- environment,
248
- }),
249
- },
250
- );
238
+ createClient(clientConfigFor(baseUrl, credential, environment));
251
239
  return { config, client };
252
240
  };
253
241
 
@@ -1,4 +1,5 @@
1
- import { chmod, lstat, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
1
+ import { readFileSync } from "node:fs";
2
+ import { chmod, lstat, mkdir, rm, stat, writeFile } from "node:fs/promises";
2
3
  import { homedir } from "node:os";
3
4
  import { dirname, join } from "node:path";
4
5
  import { assertCli, TolokaCliError } from "./cli-error";
@@ -144,10 +145,10 @@ const asCredentials = (payload: unknown): StoredCredentials | undefined => {
144
145
  return { ...identity, ...registry };
145
146
  };
146
147
 
147
- export async function inspectCredentialsFile(path: string): Promise<InspectedCredentialsFile> {
148
+ export function inspectCredentialsFile(path: string): InspectedCredentialsFile {
148
149
  let raw: string;
149
150
  try {
150
- raw = await readFile(path, "utf8");
151
+ raw = readFileSync(path, "utf8");
151
152
  } catch (error) {
152
153
  if (error instanceof Error && "code" in error && error.code === "ENOENT") return "missing";
153
154
  return "invalid";
@@ -161,11 +162,11 @@ export async function inspectCredentialsFile(path: string): Promise<InspectedCre
161
162
  }
162
163
  }
163
164
 
164
- export async function findStoredCredentials(
165
+ export function findStoredCredentials(
165
166
  environment: CliEnvironment = process.env,
166
- ): Promise<FoundCredentials | undefined> {
167
+ ): FoundCredentials | undefined {
167
168
  const path = credentialsPath(environment);
168
- const primary = await inspectCredentialsFile(path);
169
+ const primary = inspectCredentialsFile(path);
169
170
  if (primary !== "missing") {
170
171
  if (primary === "invalid") {
171
172
  throw new CredentialsError(
@@ -179,7 +180,7 @@ export async function findStoredCredentials(
179
180
  if (environment.TOLOKA_CREDENTIALS_FILE !== undefined) return undefined;
180
181
 
181
182
  const legacyPath = legacyCredentialsPath(environment);
182
- const legacy = await inspectCredentialsFile(legacyPath);
183
+ const legacy = inspectCredentialsFile(legacyPath);
183
184
  if (legacy === "missing") return undefined;
184
185
  if (legacy === "invalid") {
185
186
  throw new CredentialsError(
@@ -195,7 +196,7 @@ export async function findCommandCredentials(
195
196
  overrideCredential: string | undefined,
196
197
  ): Promise<CommandCredentialLookup> {
197
198
  try {
198
- return { found: await findStoredCredentials(environment) };
199
+ return { found: findStoredCredentials(environment) };
199
200
  } catch (error) {
200
201
  if (!(error instanceof CredentialsError) || overrideCredential === undefined) throw error;
201
202
  return { found: undefined, warning: error.message };
@@ -205,13 +206,13 @@ export async function findCommandCredentials(
205
206
  export async function readStoredCredentials(
206
207
  environment: CliEnvironment = process.env,
207
208
  ): Promise<StoredCredentials | undefined> {
208
- return (await findStoredCredentials(environment))?.credentials;
209
+ return findStoredCredentials(environment)?.credentials;
209
210
  }
210
211
 
211
212
  export async function requireStoredCredentials(
212
213
  environment: CliEnvironment = process.env,
213
214
  ): Promise<FoundCredentials> {
214
- const found = await findStoredCredentials(environment);
215
+ const found = findStoredCredentials(environment);
215
216
  if (found !== undefined) return found;
216
217
  throw new CredentialsError(
217
218
  `No stored credentials found at ${credentialsPath(environment)}. Run toloka login.`,
package/src/client.ts CHANGED
@@ -66,6 +66,7 @@ import type {
66
66
  UploadJob as GeneratedUploadJob,
67
67
  ValidatePipelineGraphBody,
68
68
  } from "../openapi/gen/api.schemas";
69
+ import { resolveLoginClientConfig } from "./cli/config";
69
70
  import { listAllPages, readAllPagesFirst } from "./pagination";
70
71
  import { createRequestOptions } from "./transport";
71
72
  import type { ClientCredential } from "./transport";
@@ -740,7 +741,7 @@ type PublicClient<T extends (...args: never[]) => unknown> = T extends (
740
741
 
741
742
  export type PotatoClient = PublicClient<typeof createClientImplementation>;
742
743
 
743
- export function createClient(config: ClientConfig): PotatoClient;
744
- export function createClient(config: ClientConfig) {
744
+ export function createClient(config?: ClientConfig): PotatoClient;
745
+ export function createClient(config: ClientConfig = resolveLoginClientConfig(process.env)) {
745
746
  return createClientImplementation(config);
746
747
  }