@brunobracaioli/aibrain 0.4.0-beta.31 → 0.4.0-beta.32

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/mcp.mjs CHANGED
@@ -550,21 +550,47 @@ function responseError(response, fallback) {
550
550
  return fallback;
551
551
  }
552
552
 
553
- /** @param {JsonObject | null} result @returns {string | undefined} */
554
- function structuredErrorCode(result) {
555
- if (!result || result.isError !== true || !Array.isArray(result.content)) return undefined;
556
- const text = result.content.find((item) => isObject(item) && typeof item.text === 'string')?.text;
557
- const prefix =
558
- typeof text === 'string'
559
- ? /^(FEATURE_DISABLED|TOOL_UNAVAILABLE|PAID_PLAN_REQUIRED|INVALID_INPUT)(?::|$)/u.exec(
560
- text,
561
- )?.[1]
553
+ /** Server tool error codes the client acts on, mapped to its own codes. */
554
+ const CLIENT_ERROR_CODES = new Map([
555
+ ['FEATURE_DISABLED', 'mcp_feature_disabled'],
556
+ ['TOOL_UNAVAILABLE', 'mcp_tool_unavailable'],
557
+ ['PAID_PLAN_REQUIRED', 'mcp_paid_plan_required'],
558
+ ['INVALID_INPUT', 'mcp_invalid_input'],
559
+ ]);
560
+ /** A tool error text longer than this is not parsed as JSON. */
561
+ const MAX_ERROR_TEXT_JSON = 4_096;
562
+
563
+ /** @param {unknown} text @returns {string | undefined} */
564
+ function jsonErrorCode(text) {
565
+ if (typeof text !== 'string' || !text.startsWith('{') || text.length > MAX_ERROR_TEXT_JSON)
566
+ return undefined;
567
+ try {
568
+ const parsed = JSON.parse(text);
569
+ return isObject(parsed) && typeof parsed.error_code === 'string'
570
+ ? parsed.error_code
562
571
  : undefined;
563
- if (prefix === 'FEATURE_DISABLED') return 'mcp_feature_disabled';
564
- if (prefix === 'TOOL_UNAVAILABLE') return 'mcp_tool_unavailable';
565
- if (prefix === 'PAID_PLAN_REQUIRED') return 'mcp_paid_plan_required';
566
- if (prefix === 'INVALID_INPUT') return 'mcp_invalid_input';
567
- return undefined;
572
+ } catch {
573
+ return undefined;
574
+ }
575
+ }
576
+
577
+ /**
578
+ * The client code of a tool error, or undefined so the caller's fallback
579
+ * applies. The context tools put the server code in
580
+ * `structuredContent.error_code` and repeat that object as JSON text; older
581
+ * servers write `CODE: message` text. Only codes in CLIENT_ERROR_CODES map.
582
+ * @param {JsonObject | null} result @returns {string | undefined}
583
+ */
584
+ function structuredErrorCode(result) {
585
+ if (!result || result.isError !== true) return undefined;
586
+ const structured = isObject(result.structuredContent) ? result.structuredContent : null;
587
+ const content = Array.isArray(result.content) ? result.content : [];
588
+ const text = content.find((item) => isObject(item) && typeof item.text === 'string')?.text;
589
+ const serverCode =
590
+ (typeof structured?.error_code === 'string' ? structured.error_code : undefined) ??
591
+ jsonErrorCode(text) ??
592
+ (typeof text === 'string' ? /^([A-Z_]{1,64})(?::|$)/u.exec(text)?.[1] : undefined);
593
+ return serverCode === undefined ? undefined : CLIENT_ERROR_CODES.get(serverCode);
568
594
  }
569
595
 
570
596
  /** @param {string} endpoint @param {string} token @returns {Promise<{ok:true, providerBatches:0, providerModel:null}>} */
package/dist/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@brunobracaioli/aibrain",
3
- "version": "0.4.0-beta.31",
3
+ "version": "0.4.0-beta.32",
4
4
  "type": "module"
5
5
  }
package/dist/policy.mjs CHANGED
@@ -201,6 +201,22 @@ export function codexPolicyJson(policy) {
201
201
  return serialized;
202
202
  }
203
203
 
204
+ /**
205
+ * ASCII-only, any letter case: without the `u` flag a non-ASCII character never
206
+ * matches an ASCII letter, so look-alike characters are refused.
207
+ */
208
+ const DISCLOSURE_ACCEPT_PATTERN = /^accept$/i;
209
+
210
+ /**
211
+ * Whether a typed answer accepts the setup disclosure: the word ACCEPT in any
212
+ * letter case, with only whitespace around it. It stays one explicit word; a
213
+ * generic yes never accepts (spec aibrain-subscriber-beta, setup step 2).
214
+ * @param {string} answer
215
+ */
216
+ export function disclosureAnswerAccepted(answer) {
217
+ return DISCLOSURE_ACCEPT_PATTERN.test(answer.trim());
218
+ }
219
+
204
220
  /** @param {ContextPolicy} policy */
205
221
  export function publicPolicy(policy) {
206
222
  return {