codemie-sdk 0.1.426 → 0.1.428

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/dist/index.cjs CHANGED
@@ -102,8 +102,10 @@ module.exports = __toCommonJS(index_exports);
102
102
  var import_node_https = require("https");
103
103
  var import_axios = __toESM(require("axios"), 1);
104
104
  var DEFAULT_CLIENT_ID = "codemie-sdk";
105
- var KeycloakCredentials = class {
105
+ var _KeycloakCredentials = class _KeycloakCredentials {
106
106
  constructor(config) {
107
+ this.cachedToken = null;
108
+ this.tokenExpiresAt = 0;
107
109
  this.validateConfigCredentials(config);
108
110
  this.config = config;
109
111
  this.httpAgent = new import_node_https.Agent({
@@ -111,11 +113,17 @@ var KeycloakCredentials = class {
111
113
  });
112
114
  }
113
115
  async getToken() {
116
+ const now = Date.now();
117
+ if (this.cachedToken && now < this.tokenExpiresAt) {
118
+ return this.cachedToken;
119
+ }
114
120
  const { serverUrl, realmName } = this.config;
115
121
  const url = `${serverUrl}/realms/${realmName}/protocol/openid-connect/token`;
116
122
  let payload;
117
123
  if (this.config.externalToken && this.config.externalIdp) {
118
- payload = this.createTokenExchangePayload();
124
+ const raw = this.config.externalToken;
125
+ const subjectToken = typeof raw === "function" ? await raw() : raw;
126
+ payload = this.createTokenExchangePayload(subjectToken);
119
127
  } else if (this.config.username && this.config.password) {
120
128
  payload = this.createResourceOwnerCredentialsPayload();
121
129
  } else {
@@ -125,15 +133,23 @@ var KeycloakCredentials = class {
125
133
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
126
134
  httpsAgent: this.httpAgent
127
135
  });
128
- return data.access_token;
136
+ this.cachedToken = data.access_token;
137
+ const expiresIn = data.expires_in ?? 300;
138
+ this.tokenExpiresAt = now + expiresIn * 1e3 - _KeycloakCredentials.TOKEN_EXPIRY_BUFFER_MS;
139
+ return this.cachedToken;
140
+ }
141
+ clearCache() {
142
+ this.cachedToken = null;
143
+ this.tokenExpiresAt = 0;
129
144
  }
130
- createTokenExchangePayload() {
131
- const { clientId, clientSecret, externalToken, externalIdp } = this.config;
145
+ createTokenExchangePayload(subjectToken) {
146
+ const { clientId, clientSecret, externalIdp } = this.config;
132
147
  return {
133
148
  grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
134
149
  client_id: clientId || DEFAULT_CLIENT_ID,
135
150
  client_secret: clientSecret,
136
- subject_token: externalToken,
151
+ subject_token: subjectToken,
152
+ subject_token_type: "urn:ietf:params:oauth:token-type:access_token",
137
153
  subject_issuer: externalIdp,
138
154
  requested_token_type: "urn:ietf:params:oauth:token-type:access_token"
139
155
  };
@@ -171,6 +187,8 @@ var KeycloakCredentials = class {
171
187
  }
172
188
  }
173
189
  };
190
+ _KeycloakCredentials.TOKEN_EXPIRY_BUFFER_MS = 3e4;
191
+ var KeycloakCredentials = _KeycloakCredentials;
174
192
 
175
193
  // src/schemas/analytics.ts
176
194
  var import_zod = require("zod");
@@ -229,7 +247,7 @@ function formatCookies(cookies) {
229
247
  }
230
248
  var ApiRequestHandler = class {
231
249
  constructor(config) {
232
- const { apiDomain, token, verifySSL = true, cookies = {} } = config;
250
+ const { apiDomain, token, verifySSL = true, cookies = {}, tokenGetter } = config;
233
251
  const headers = {
234
252
  "Content-Type": "application/json"
235
253
  };
@@ -251,6 +269,13 @@ var ApiRequestHandler = class {
251
269
  (response) => response,
252
270
  (error) => this.processFailedResponse(error)
253
271
  );
272
+ if (tokenGetter) {
273
+ this.client.interceptors.request.use(async (requestConfig) => {
274
+ const freshToken = await tokenGetter();
275
+ requestConfig.headers.Authorization = `Bearer ${freshToken}`;
276
+ return requestConfig;
277
+ });
278
+ }
254
279
  }
255
280
  async request(method, path, config = {}) {
256
281
  const response = await this.client.request({
@@ -434,10 +459,10 @@ var import_zod3 = require("zod");
434
459
  // src/schemas/assistant.ts
435
460
  var import_zod2 = require("zod");
436
461
  var AssistantListParamsSchema = import_zod2.z.object({
437
- minimal_response: import_zod2.z.boolean().prefault(true),
438
- scope: import_zod2.z.enum(["visible_to_user", "marketplace"]).prefault("visible_to_user"),
439
- page: import_zod2.z.number().prefault(0),
440
- per_page: import_zod2.z.number().prefault(12),
462
+ minimal_response: import_zod2.z.boolean().default(true),
463
+ scope: import_zod2.z.enum(["visible_to_user", "marketplace"]).default("visible_to_user"),
464
+ page: import_zod2.z.number().default(0),
465
+ per_page: import_zod2.z.number().default(12),
441
466
  filters: import_zod2.z.record(import_zod2.z.string(), import_zod2.z.unknown()).optional()
442
467
  }).readonly();
443
468
  var PromptVariableSchema = import_zod2.z.object({
@@ -503,9 +528,7 @@ var AssistantCreateParamsSchema = import_zod2.z.object({
503
528
  const hasCommand = !!config.command;
504
529
  return hasUrl !== hasCommand;
505
530
  },
506
- {
507
- error: "Either 'url' or 'command' must be provided in config, but not both"
508
- }
531
+ "Either 'url' or 'command' must be provided in config, but not both"
509
532
  ),
510
533
  mcp_connect_url: import_zod2.z.string().optional(),
511
534
  tools_tokens_size_limit: import_zod2.z.number().optional(),
@@ -516,9 +539,9 @@ var AssistantCreateParamsSchema = import_zod2.z.object({
516
539
  })
517
540
  ),
518
541
  assistant_ids: import_zod2.z.array(import_zod2.z.string()),
519
- prompt_variables: import_zod2.z.array(PromptVariableSchema).optional().prefault([]),
542
+ prompt_variables: import_zod2.z.array(PromptVariableSchema).optional().default([]),
520
543
  categories: import_zod2.z.array(import_zod2.z.string()).max(3).optional(),
521
- skip_integration_validation: import_zod2.z.boolean().optional().prefault(false)
544
+ skip_integration_validation: import_zod2.z.boolean().optional().default(false)
522
545
  }).readonly();
523
546
  var AssistantUpdateParamsSchema = AssistantCreateParamsSchema;
524
547
  var AssistantChatParamsSchema = import_zod2.z.object({
@@ -568,7 +591,7 @@ var AssistantService = class {
568
591
  "/v1/assistants",
569
592
  {
570
593
  ...params,
571
- ...params.filters && { filters: JSON.stringify(params.filters) }
594
+ ...params.filters ? { filters: JSON.stringify(params.filters) } : {}
572
595
  }
573
596
  );
574
597
  return response.data;
@@ -582,7 +605,7 @@ var AssistantService = class {
582
605
  "/v1/assistants",
583
606
  {
584
607
  ...params,
585
- ...params.filters && { filters: JSON.stringify(params.filters) }
608
+ ...params.filters ? { filters: JSON.stringify(params.filters) } : {}
586
609
  }
587
610
  );
588
611
  return response;
@@ -776,7 +799,7 @@ var import_zod4 = require("zod");
776
799
  var ConversationCreateParamsSchema = import_zod4.z.object({
777
800
  initial_assistant_id: import_zod4.z.string().optional(),
778
801
  folder: import_zod4.z.string().optional(),
779
- mcp_server_single_usage: import_zod4.z.boolean().prefault(false).optional()
802
+ mcp_server_single_usage: import_zod4.z.boolean().optional().default(false)
780
803
  }).readonly();
781
804
 
782
805
  // src/services/conversation.ts
@@ -1332,18 +1355,18 @@ var IntegrationType = {
1332
1355
  // src/schemas/integration.ts
1333
1356
  var import_zod5 = require("zod");
1334
1357
  var IntegrationListParamsSchema = import_zod5.z.object({
1335
- setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).prefault(IntegrationType.USER),
1336
- page: import_zod5.z.number().prefault(0),
1337
- per_page: import_zod5.z.number().prefault(10),
1358
+ setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).default(IntegrationType.USER),
1359
+ page: import_zod5.z.number().default(0),
1360
+ per_page: import_zod5.z.number().default(10),
1338
1361
  filters: import_zod5.z.record(import_zod5.z.string(), import_zod5.z.unknown()).optional()
1339
1362
  }).readonly();
1340
1363
  var IntegrationGetParamsSchema = import_zod5.z.object({
1341
1364
  integration_id: import_zod5.z.string(),
1342
- setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).prefault(IntegrationType.USER)
1365
+ setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).default(IntegrationType.USER)
1343
1366
  }).readonly();
1344
1367
  var IntegrationGetByAliasParamsSchema = import_zod5.z.object({
1345
1368
  alias: import_zod5.z.string(),
1346
- setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).prefault(IntegrationType.USER)
1369
+ setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).default(IntegrationType.USER)
1347
1370
  }).readonly();
1348
1371
  var CredentialValueSchema = import_zod5.z.object({
1349
1372
  key: import_zod5.z.string(),
@@ -1353,11 +1376,11 @@ var IntegrationCreateParamsSchema = import_zod5.z.object({
1353
1376
  credential_type: import_zod5.z.enum(Object.values(CredentialTypes)),
1354
1377
  credential_values: import_zod5.z.array(CredentialValueSchema),
1355
1378
  project_name: import_zod5.z.string(),
1356
- setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).prefault(IntegrationType.USER),
1379
+ setting_type: import_zod5.z.enum([IntegrationType.USER, IntegrationType.PROJECT]).default(IntegrationType.USER),
1357
1380
  alias: import_zod5.z.string().optional(),
1358
- default: import_zod5.z.boolean().optional().prefault(false),
1381
+ default: import_zod5.z.boolean().optional().default(false),
1359
1382
  project: import_zod5.z.string().optional(),
1360
- enabled: import_zod5.z.boolean().optional().prefault(true),
1383
+ enabled: import_zod5.z.boolean().optional().default(true),
1361
1384
  external_id: import_zod5.z.string().optional()
1362
1385
  }).readonly();
1363
1386
  var IntegrationUpdateParamsSchema = IntegrationCreateParamsSchema;
@@ -1382,7 +1405,7 @@ var IntegrationService = class {
1382
1405
  const queryParams = {
1383
1406
  page: params.page,
1384
1407
  per_page: params.per_page,
1385
- ...params.filters && { filters: JSON.stringify(params.filters) }
1408
+ ...params.filters ? { filters: JSON.stringify(params.filters) } : {}
1386
1409
  };
1387
1410
  const response = await this.api.get(
1388
1411
  this.getBasePath(settingType),
@@ -1827,8 +1850,8 @@ var ExecutionStatus = {
1827
1850
 
1828
1851
  // src/schemas/workflow.ts
1829
1852
  var WorkflowListParamsSchema = import_zod7.z.object({
1830
- page: import_zod7.z.number().prefault(0),
1831
- per_page: import_zod7.z.number().prefault(10),
1853
+ page: import_zod7.z.number().default(0),
1854
+ per_page: import_zod7.z.number().default(10),
1832
1855
  projects: import_zod7.z.array(import_zod7.z.string()).optional(),
1833
1856
  search: import_zod7.z.string().optional()
1834
1857
  }).readonly();
@@ -1851,8 +1874,8 @@ var WorkflowUpdateParamsSchema = import_zod7.z.object({
1851
1874
  icon_url: import_zod7.z.string().optional()
1852
1875
  }).readonly();
1853
1876
  var WorkflowExecutionListParamsSchema = import_zod7.z.object({
1854
- page: import_zod7.z.number().prefault(0),
1855
- per_page: import_zod7.z.number().prefault(10)
1877
+ page: import_zod7.z.number().default(0),
1878
+ per_page: import_zod7.z.number().default(10)
1856
1879
  }).readonly();
1857
1880
  var WorkflowExecutionCreateParamsSchema = import_zod7.z.object({
1858
1881
  user_input: import_zod7.z.union([
@@ -1868,8 +1891,8 @@ var WorkflowExecutionCreateParamsSchema = import_zod7.z.object({
1868
1891
  tags: import_zod7.z.array(import_zod7.z.string()).optional()
1869
1892
  }).readonly();
1870
1893
  var WorkflowExecutionStateListParamsSchema = import_zod7.z.object({
1871
- page: import_zod7.z.number().prefault(0),
1872
- per_page: import_zod7.z.number().prefault(10)
1894
+ page: import_zod7.z.number().default(0),
1895
+ per_page: import_zod7.z.number().default(10)
1873
1896
  }).readonly();
1874
1897
 
1875
1898
  // src/services/workflow_execution_state.ts
@@ -2104,7 +2127,9 @@ var CodeMieClient = class {
2104
2127
  apiDomain: this.apiDomain,
2105
2128
  token: this.token,
2106
2129
  verifySSL: this.verifySSL,
2107
- cookies: this.cookies
2130
+ cookies: this.cookies,
2131
+ // biome-ignore lint/style/noNonNullAssertion: auth is non-null here, guarded by ternary
2132
+ tokenGetter: this.auth ? () => this.auth.getToken() : null
2108
2133
  };
2109
2134
  this._analytics = new AnalyticsService(authConfig);
2110
2135
  this._assistants = new AssistantService(authConfig);
@@ -2234,6 +2259,7 @@ var CodeMieClient = class {
2234
2259
  if (!this.auth) {
2235
2260
  throw new Error("Authentication not configured");
2236
2261
  }
2262
+ this.auth.clearCache();
2237
2263
  this.token = await this.auth.getToken();
2238
2264
  await this.refreshServices();
2239
2265
  return this.token;
@@ -2252,7 +2278,9 @@ var CodeMieClient = class {
2252
2278
  apiDomain: this.apiDomain,
2253
2279
  token: this.token,
2254
2280
  verifySSL: this.verifySSL,
2255
- cookies: this.cookies
2281
+ cookies: this.cookies,
2282
+ // biome-ignore lint/style/noNonNullAssertion: auth is non-null here, guarded by ternary
2283
+ tokenGetter: this.auth ? () => this.auth.getToken() : null
2256
2284
  };
2257
2285
  this._analytics = new AnalyticsService(authConfig);
2258
2286
  this._assistants = new AssistantService(authConfig);