@runtypelabs/sdk 10.4.0 → 10.5.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/dist/index.mjs CHANGED
@@ -13199,6 +13199,196 @@ var SurfacesNamespace = class {
13199
13199
  }
13200
13200
  };
13201
13201
 
13202
+ // src/client-tokens-namespace.ts
13203
+ var ClientTokenEnsureError = class extends Error {
13204
+ constructor(message) {
13205
+ super(message);
13206
+ this.name = "ClientTokenEnsureError";
13207
+ }
13208
+ };
13209
+ var ClientTokenDriftError = class extends Error {
13210
+ constructor(name, plan) {
13211
+ super(
13212
+ `Client token "${name}" drifted: plan is '${plan.changes}' (changed: ${plan.changedKeys.join(", ") || "n/a"}). Re-run ensure to converge.`
13213
+ );
13214
+ this.name = "ClientTokenDriftError";
13215
+ this.plan = plan;
13216
+ }
13217
+ };
13218
+ function canonicalJson(value) {
13219
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
13220
+ if (value !== null && typeof value === "object") {
13221
+ const entries = Object.entries(value).filter(([, entry]) => entry !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
13222
+ return `{${entries.map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`).join(",")}}`;
13223
+ }
13224
+ return JSON.stringify(value ?? null);
13225
+ }
13226
+ var sameSet = (a, b) => canonicalJson([...a].sort()) === canonicalJson([...b].sort());
13227
+ function planUpdate(definition, existing) {
13228
+ const environment = definition.environment ?? "live";
13229
+ if (environment !== existing.environment) {
13230
+ throw new ClientTokenEnsureError(
13231
+ `Client token "${definition.name}" is a ${existing.environment} token; the definition asks for ${environment}. A token environment cannot change. Rename the definition or delete the existing token.`
13232
+ );
13233
+ }
13234
+ const productSurfaceId = definition.productSurfaceId ?? null;
13235
+ if (productSurfaceId !== (existing.productSurfaceId ?? null)) {
13236
+ throw new ClientTokenEnsureError(
13237
+ `Client token "${definition.name}" belongs to surface ${existing.productSurfaceId ?? "(none)"}; the definition names ${productSurfaceId ?? "(none)"}. A token's surface cannot change.`
13238
+ );
13239
+ }
13240
+ const flowIds = definition.flowIds ?? [];
13241
+ if (flowIds.length === 0 && existing.flowIds.length > 0) {
13242
+ throw new ClientTokenEnsureError(
13243
+ `Client token "${definition.name}" carries flows (${existing.flowIds.join(", ")}); the API cannot remove every flow from a token. Delete the token and re-run ensure.`
13244
+ );
13245
+ }
13246
+ const patch = {};
13247
+ const agentIds = definition.agentIds ?? [];
13248
+ if (!sameSet(agentIds, existing.agentIds)) patch.agentIds = agentIds;
13249
+ if (flowIds.length > 0 && !sameSet(flowIds, existing.flowIds)) patch.flowIds = flowIds;
13250
+ if (definition.defaultFlowId !== void 0 && definition.defaultFlowId !== existing.defaultFlowId) {
13251
+ patch.defaultFlowId = definition.defaultFlowId;
13252
+ }
13253
+ if (!sameSet(definition.allowedOrigins, existing.allowedOrigins)) {
13254
+ patch.allowedOrigins = definition.allowedOrigins;
13255
+ }
13256
+ const targetAlias = definition.targetAlias ?? null;
13257
+ if (targetAlias !== (existing.targetAlias ?? null)) patch.targetAlias = targetAlias;
13258
+ const targetVersionId = definition.targetVersionId ?? null;
13259
+ if (targetVersionId !== (existing.targetVersionId ?? null)) {
13260
+ patch.targetVersionId = targetVersionId;
13261
+ }
13262
+ const optionalKeys = [
13263
+ "rateLimitPerMinute",
13264
+ "rateLimitPerHour",
13265
+ "maxMessagesPerSession",
13266
+ "sessionIdleTimeoutMinutes"
13267
+ ];
13268
+ for (const key of optionalKeys) {
13269
+ const desired = definition[key];
13270
+ if (desired !== void 0 && desired !== existing[key]) patch[key] = desired;
13271
+ }
13272
+ if (definition.config !== void 0 && canonicalJson(definition.config) !== canonicalJson(existing.config ?? {})) {
13273
+ patch.config = definition.config;
13274
+ }
13275
+ return patch;
13276
+ }
13277
+ var ClientTokensNamespace = class {
13278
+ constructor(getClient) {
13279
+ this.getClient = getClient;
13280
+ }
13281
+ async list() {
13282
+ const response = await this.getClient().get("/client-tokens");
13283
+ return response.clientTokens;
13284
+ }
13285
+ async get(id) {
13286
+ const response = await this.getClient().get(
13287
+ `/client-tokens/${id}`
13288
+ );
13289
+ return response.clientToken;
13290
+ }
13291
+ /** The response's `token` is the publishable value. */
13292
+ async create(data) {
13293
+ return this.getClient().post("/client-tokens", data);
13294
+ }
13295
+ async update(id, data) {
13296
+ return this.getClient().put(
13297
+ `/client-tokens/${id}`,
13298
+ data
13299
+ );
13300
+ }
13301
+ async delete(id) {
13302
+ await this.getClient().delete(`/client-tokens/${id}`);
13303
+ }
13304
+ /** Issue a new value; the previous value stops working immediately. */
13305
+ async regenerate(id) {
13306
+ return this.getClient().post(`/client-tokens/${id}/regenerate`);
13307
+ }
13308
+ /** Read a stored token value. Fails for a reveal-once live token. */
13309
+ async getToken(id) {
13310
+ return this.getClient().get(`/client-tokens/${id}/token`);
13311
+ }
13312
+ async listConversations(id, params) {
13313
+ return this.getClient().get(
13314
+ `/client-tokens/${id}/conversations`,
13315
+ params
13316
+ );
13317
+ }
13318
+ /**
13319
+ * Find the token named `definition.name`, create it when missing, and update
13320
+ * it when it differs. Never deletes or regenerates. Throws when two tokens
13321
+ * share the name, or when the environment or surface differs.
13322
+ *
13323
+ * @example
13324
+ * ```typescript
13325
+ * const agent = await Runtype.agents.ensure(docsAgent, { deploy: { alias: 'live' } })
13326
+ * const { token } = await Runtype.clientTokens.ensure({
13327
+ * name: 'Docs site widget',
13328
+ * agentIds: [agent.agentId],
13329
+ * allowedOrigins: ['https://docs.example.com'],
13330
+ * })
13331
+ * ```
13332
+ */
13333
+ async ensure(definition, options = {}) {
13334
+ if (typeof definition?.name !== "string" || definition.name.length === 0) {
13335
+ throw new ClientTokenEnsureError('clientTokens.ensure requires a non-empty string "name"');
13336
+ }
13337
+ const dryRun = options.dryRun === true || options.expectNoChanges === true;
13338
+ const matches = (await this.list()).filter((token) => token.name === definition.name);
13339
+ if (matches.length > 1) {
13340
+ throw new ClientTokenEnsureError(
13341
+ `${matches.length} client tokens are named "${definition.name}" (${matches.map((t) => t.id).join(", ")}). Rename or delete all but one so ensure has a single identity to converge.`
13342
+ );
13343
+ }
13344
+ const existing = matches[0];
13345
+ if (!existing) {
13346
+ const plan = { result: "plan", changes: "create", changedKeys: [] };
13347
+ if (options.expectNoChanges) throw new ClientTokenDriftError(definition.name, plan);
13348
+ if (dryRun) return plan;
13349
+ const created = await this.create({ ...definition });
13350
+ return {
13351
+ result: "created",
13352
+ clientToken: created.clientToken,
13353
+ token: created.token,
13354
+ changedKeys: [],
13355
+ warnings: created.warnings ?? []
13356
+ };
13357
+ }
13358
+ const patch = planUpdate(definition, existing);
13359
+ const changedKeys = Object.keys(patch);
13360
+ if (dryRun) {
13361
+ const plan = {
13362
+ result: "plan",
13363
+ changes: changedKeys.length > 0 ? "update" : "none",
13364
+ changedKeys,
13365
+ clientToken: existing
13366
+ };
13367
+ if (options.expectNoChanges && plan.changes !== "none") {
13368
+ throw new ClientTokenDriftError(definition.name, plan);
13369
+ }
13370
+ return plan;
13371
+ }
13372
+ if (changedKeys.length === 0) {
13373
+ return {
13374
+ result: "unchanged",
13375
+ clientToken: existing,
13376
+ token: existing.tokenValue,
13377
+ changedKeys,
13378
+ warnings: []
13379
+ };
13380
+ }
13381
+ const updated = await this.update(existing.id, patch);
13382
+ return {
13383
+ result: "updated",
13384
+ clientToken: updated.clientToken,
13385
+ token: updated.clientToken.tokenValue,
13386
+ changedKeys,
13387
+ warnings: updated.warnings ?? []
13388
+ };
13389
+ }
13390
+ };
13391
+
13202
13392
  // src/runtype.ts
13203
13393
  var globalConfig = {};
13204
13394
  var globalClient = null;
@@ -13745,6 +13935,24 @@ var Runtype = class {
13745
13935
  static get surfaces() {
13746
13936
  return new SurfacesNamespace(() => this.getClient());
13747
13937
  }
13938
+ /**
13939
+ * Client tokens — publishable credentials for widgets. `ensure` finds a token
13940
+ * by name, creates it when missing, and converges its agents, flows, origins,
13941
+ * and target; it returns the token value when the token stores one.
13942
+ *
13943
+ * @example
13944
+ * ```typescript
13945
+ * const agent = await Runtype.agents.ensure(docsAgent, { deploy: { alias: 'live' } })
13946
+ * const { token } = await Runtype.clientTokens.ensure({
13947
+ * name: 'Docs site widget',
13948
+ * agentIds: [agent.agentId],
13949
+ * allowedOrigins: ['https://docs.example.com'],
13950
+ * })
13951
+ * ```
13952
+ */
13953
+ static get clientTokens() {
13954
+ return new ClientTokensNamespace(() => this.getClient());
13955
+ }
13748
13956
  };
13749
13957
 
13750
13958
  // src/agent-promotion.ts
@@ -13938,7 +14146,7 @@ function transformQueryParams(params) {
13938
14146
 
13939
14147
  // src/version.ts
13940
14148
  var FALLBACK_VERSION = "0.0.0";
13941
- var SDK_VERSION = "10.4.0".length > 0 ? "10.4.0" : FALLBACK_VERSION;
14149
+ var SDK_VERSION = "10.5.0".length > 0 ? "10.5.0" : FALLBACK_VERSION;
13942
14150
  var RUNTYPE_CLIENT_KIND = "sdk";
13943
14151
  var SDK_USER_AGENT = `runtype-sdk/${SDK_VERSION} (typescript)`;
13944
14152
 
@@ -15229,7 +15437,10 @@ export {
15229
15437
  ClientBatchBuilder,
15230
15438
  ClientEvalBuilder,
15231
15439
  ClientFlowBuilder,
15440
+ ClientTokenDriftError,
15441
+ ClientTokenEnsureError,
15232
15442
  ClientTokensEndpoint,
15443
+ ClientTokensNamespace,
15233
15444
  CollectionsEndpoint,
15234
15445
  ContextTemplatesEndpoint,
15235
15446
  ConversationsEndpoint,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtypelabs/sdk",
3
- "version": "10.4.0",
3
+ "version": "10.5.0",
4
4
  "type": "module",
5
5
  "description": "TypeScript SDK for the Runtype API with fluent methods. Use it to quickly realize AI products, agents, and workflows.",
6
6
  "main": "dist/index.cjs",
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dependencies": {},
26
26
  "devDependencies": {
27
- "@runtypelabs/shared": "3.62.0",
27
+ "@runtypelabs/shared": "3.63.0",
28
28
  "openapi-typescript": "^7.13.0",
29
29
  "tsup": "^8.0.2",
30
30
  "typescript": "^6.0.3",