@blazingagents/sdk 0.46.0 → 0.47.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.
@@ -28,7 +28,7 @@ const mcpConnectionNameSchema = z
28
28
  const mcpConnectionNameInputSchema = mcpConnectionNameSchema.describe("Display name of the connection, unique within your tenant.");
29
29
  const bearerTokenSchema = z
30
30
  .string()
31
- .min(1)
31
+ .min(8)
32
32
  .max(MAX_MCP_BEARER_TOKEN_LENGTH)
33
33
  .regex(/^[!-~]+$/, "Bearer token must contain visible ASCII characters only")
34
34
  .describe("Token sent as `Authorization: Bearer <token>` to the MCP server. Required when `authType` is `bearer`. Never returned.");
@@ -40,7 +40,7 @@ const oauthClientIdSchema = z
40
40
  .describe("OAuth client ID. Optional, but send it together with `clientSecret`.");
41
41
  const oauthClientSecretSchema = z
42
42
  .string()
43
- .min(1)
43
+ .min(8)
44
44
  .max(MAX_MCP_OAUTH_CLIENT_SECRET_LENGTH)
45
45
  .regex(/^[!-~]+$/, "Client secret must contain visible ASCII characters only")
46
46
  .describe("OAuth client secret. Optional, but send it together with `clientId`. Never returned.");
@@ -47,7 +47,7 @@ export const createMerchantConnectionBodySchema = z
47
47
  environment: merchantEnvironmentSchema.describe("The provider environment to send usage to: `sandbox` or `live`."),
48
48
  credential: z
49
49
  .string()
50
- .min(1)
50
+ .min(8)
51
51
  .describe("A credential for your provider account: a Polar Organization Access Token, or a Dodo API key in the mode that matches `environment`. It is never returned."),
52
52
  guard: merchantGuardSchema
53
53
  .optional()
@@ -58,7 +58,7 @@ export const updateMerchantConnectionBodySchema = z
58
58
  .object({
59
59
  credential: z
60
60
  .string()
61
- .min(1)
61
+ .min(8)
62
62
  .optional()
63
63
  .describe("A replacement credential. It must belong to the same merchant account as the current one."),
64
64
  guard: merchantGuardSchema
@@ -71,7 +71,7 @@ export const createProviderBodySchema = z
71
71
  apiKey: z
72
72
  .string()
73
73
  .trim()
74
- .min(1)
74
+ .min(8)
75
75
  .describe("The vendor API key. It is stored encrypted, never returned, and cannot be changed later."),
76
76
  })
77
77
  .strict()
package/dist/functions.js CHANGED
@@ -10,6 +10,32 @@ import { apiPath, requestJson } from "./http.js";
10
10
  export function defineFunction(definition) {
11
11
  return definition;
12
12
  }
13
+ /**
14
+ * Inlines a root `$ref` to an object `$defs` entry. Zod extracts a schema
15
+ * with an id and leaves that reference, which is not `type: "object"`.
16
+ */
17
+ function inlineRootObjectDefinition(schema) {
18
+ const { $ref: ref, $defs: defs, ...rest } = schema;
19
+ if (ref === undefined || !ref.startsWith("#/$defs/") || defs === undefined) {
20
+ return schema;
21
+ }
22
+ const name = ref
23
+ .slice("#/$defs/".length)
24
+ .replaceAll("~1", "/")
25
+ .replaceAll("~0", "~");
26
+ const target = defs[name];
27
+ if (target === undefined || target.type !== "object") {
28
+ return schema;
29
+ }
30
+ const inlined = { ...target, ...rest };
31
+ const json = JSON.stringify(inlined);
32
+ if (json.includes('"$ref":"#/$defs/')) {
33
+ const { [name]: _root, ...others } = defs;
34
+ const rootReferenced = `${json}${JSON.stringify(others)}`.includes(JSON.stringify(ref));
35
+ inlined.$defs = rootReferenced ? defs : others;
36
+ }
37
+ return inlined;
38
+ }
13
39
  /**
14
40
  * Converts caller-local Zod input schemas to validated JSON Schema definitions.
15
41
  * @param functions - Caller-local function handlers keyed by name.
@@ -20,7 +46,13 @@ export function toChatFunctionDefinitions(functions) {
20
46
  try {
21
47
  return chatFunctionDefinitionsSchema.parse(Object.fromEntries(Object.entries(functions).map(([name, fn]) => {
22
48
  const { $schema: _, ...inputSchema } = z.toJSONSchema(fn.inputSchema, { io: "input" });
23
- return [name, { description: fn.description, inputSchema }];
49
+ return [
50
+ name,
51
+ {
52
+ description: fn.description,
53
+ inputSchema: inlineRootObjectDefinition(inputSchema),
54
+ },
55
+ ];
24
56
  })));
25
57
  }
26
58
  catch (cause) {
@@ -255,7 +287,11 @@ export function stripFunctionEvents(body, { onCall, onCancel, onEnd } = {}) {
255
287
  buffer = buffer.slice(end);
256
288
  }
257
289
  }
258
- handleBlock("", buffer + decoder.decode());
290
+ /** An unterminated block is not an SSE event: forward public bytes, never dispatch. */
291
+ const rest = buffer + decoder.decode();
292
+ if (functionCallFrom(rest) === undefined) {
293
+ handleBlock("", rest);
294
+ }
259
295
  if (!finished) {
260
296
  finished = true;
261
297
  output.close();
package/dist/http.js CHANGED
@@ -20,6 +20,8 @@ const CREDENTIAL_KEYS = new Set([
20
20
  "client_secret",
21
21
  "credential",
22
22
  ]);
23
+ /** Submitted secrets at least this long match as a substring; shorter ones only as a whole string. */
24
+ const MIN_SUBSTRING_SECRET_LENGTH = 8;
23
25
  const DOT_OR_EMPTY_SEGMENTS = new Set(["", ".", ".."]);
24
26
  /**
25
27
  * Builds an endpoint path, encoding each interpolated value so it stays inside
@@ -106,6 +108,16 @@ export async function requestJson(config, path, options = {}, schema) {
106
108
  status: response.status,
107
109
  });
108
110
  }
111
+ if (options.credentialBearing &&
112
+ containsSubmittedSecret(json, collectSecrets(options.json))) {
113
+ throw new BlazingAgentsError({
114
+ code: "credential_echo",
115
+ headers,
116
+ message: "The server echoed a submitted secret; the response body was discarded.",
117
+ requestId: headers.get("x-request-id") ?? undefined,
118
+ status: response.status,
119
+ });
120
+ }
109
121
  try {
110
122
  return schema === undefined ? json : schema.parse(json);
111
123
  }
@@ -139,6 +151,38 @@ function findCredentialKey(value) {
139
151
  }
140
152
  }
141
153
  }
154
+ /** Credential values collected from a request body. */
155
+ function collectSecrets(value) {
156
+ if (value === null || typeof value !== "object") {
157
+ return [];
158
+ }
159
+ return Object.entries(value).flatMap(([key, item]) => CREDENTIAL_KEYS.has(key) && typeof item === "string"
160
+ ? [item]
161
+ : collectSecrets(item));
162
+ }
163
+ /**
164
+ * Reports whether `text` holds `secret`: anywhere for a secret of
165
+ * `MIN_SUBSTRING_SECRET_LENGTH` characters or more, else only as the whole
166
+ * string. An empty secret is never found.
167
+ */
168
+ function secretIn(text, secret) {
169
+ if (secret === "") {
170
+ return false;
171
+ }
172
+ return secret.length >= MIN_SUBSTRING_SECRET_LENGTH
173
+ ? text.includes(secret)
174
+ : text === secret;
175
+ }
176
+ /** Reports whether any string value in a decoded JSON value holds a submitted secret. */
177
+ function containsSubmittedSecret(value, secrets) {
178
+ if (typeof value === "string") {
179
+ return secrets.some((secret) => secretIn(value, secret));
180
+ }
181
+ if (value === null || typeof value !== "object") {
182
+ return false;
183
+ }
184
+ return Object.values(value).some((item) => containsSubmittedSecret(item, secrets));
185
+ }
142
186
  /**
143
187
  * Performs a request and returns the raw `Response` for streaming paths.
144
188
  * Non-2xx bodies are decoded before a caller can observe a streaming body.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blazingagents/sdk",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "TypeScript SDK for the Blazing Agents API",
5
5
  "homepage": "https://docs.blazingagents.com",
6
6
  "bugs": {