theorum 0.1.2 → 0.1.4

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.
Files changed (150) hide show
  1. package/README.md +83 -27
  2. package/deno.json +57 -0
  3. package/deno.lock +469 -0
  4. package/docs/AGENT_PROFILE_CONTRACT.md +45 -18
  5. package/docs/CLI_SPEC.md +2 -2
  6. package/docs/SECRETS.md +27 -22
  7. package/mod.ts +95 -0
  8. package/package.json +37 -29
  9. package/src/cli/commands/profile.ts +57 -0
  10. package/src/cli/commands/run.ts +66 -0
  11. package/src/cli/commands/test.ts +195 -0
  12. package/src/cli/index.ts +161 -0
  13. package/src/cli/matrix/fixtures.ts +96 -0
  14. package/src/cli/matrix/synthesizer.ts +240 -0
  15. package/src/guardrails/error.ts +142 -0
  16. package/src/guardrails/injection.ts +254 -0
  17. package/src/guardrails/mod.ts +40 -0
  18. package/src/guardrails/quota.ts +87 -0
  19. package/src/guardrails/sanitize.ts +165 -0
  20. package/src/guardrails/sensitive.ts +96 -0
  21. package/src/host/mint-trace.ts +54 -0
  22. package/src/host/mod.ts +20 -0
  23. package/src/host/reply.ts +32 -0
  24. package/src/kernel/engine/assert.ts +29 -0
  25. package/src/kernel/engine/boundary.ts +75 -0
  26. package/src/kernel/engine/delta.ts +440 -0
  27. package/src/kernel/engine/hash.ts +11 -0
  28. package/src/kernel/engine/record.ts +8 -0
  29. package/src/kernel/engine/repair.ts +56 -0
  30. package/src/kernel/engine/runner/gates.ts +338 -0
  31. package/src/kernel/engine/runner/mod.ts +135 -0
  32. package/src/kernel/engine/runner/state.ts +31 -0
  33. package/src/kernel/engine/runner/steps.ts +183 -0
  34. package/src/kernel/engine/runner/stream.ts +83 -0
  35. package/src/kernel/engine/runner/tokens.ts +47 -0
  36. package/src/kernel/engine/runner/tools.ts +266 -0
  37. package/src/kernel/engine/runner.ts +7 -0
  38. package/src/kernel/engine/tree.ts +18 -0
  39. package/src/kernel/mod.ts +39 -0
  40. package/src/kernel/registry/attachments.ts +195 -0
  41. package/src/kernel/registry/catalog.ts +181 -0
  42. package/src/kernel/registry/ingress.ts +223 -0
  43. package/src/kernel/registry/profiles.ts +165 -0
  44. package/src/kernel/registry/provider-request.ts +31 -0
  45. package/src/kernel/registry/resolve.ts +290 -0
  46. package/src/kernel/registry/schemas.ts +29 -0
  47. package/src/kernel/registry/tools.ts +45 -0
  48. package/src/kernel/registry/vault.ts +26 -0
  49. package/src/kernel/types.ts +576 -0
  50. package/{esm/src/observability/mod.js → src/observability/mod.ts} +11 -2
  51. package/src/observability/spans.ts +70 -0
  52. package/src/observability/trace-attach.ts +96 -0
  53. package/src/observability/trace-record.ts +254 -0
  54. package/src/observability/trace-usage.ts +36 -0
  55. package/src/observability/trace.ts +143 -0
  56. package/src/presets/google.ts +147 -0
  57. package/src/presets/mod.ts +24 -0
  58. package/src/providers/create-provider.ts +64 -0
  59. package/src/providers/gemini-tape.ts +59 -0
  60. package/src/providers/google-tap.ts +57 -0
  61. package/src/providers/interactions.ts +182 -0
  62. package/src/providers/keys.ts +182 -0
  63. package/src/providers/mod.ts +12 -0
  64. package/src/providers/openrouter-mod.ts +16 -0
  65. package/src/providers/openrouter-payload.ts +232 -0
  66. package/src/providers/openrouter.ts +758 -0
  67. package/src/providers/pcm.ts +37 -0
  68. package/src/providers/provider.ts +206 -0
  69. package/src/providers/speech.ts +180 -0
  70. package/src/providers/sse.ts +59 -0
  71. package/esm/_dnt.polyfills.d.ts +0 -11
  72. package/esm/_dnt.polyfills.js +0 -15
  73. package/esm/_dnt.shims.d.ts +0 -5
  74. package/esm/_dnt.shims.js +0 -61
  75. package/esm/mod.d.ts +0 -37
  76. package/esm/mod.js +0 -35
  77. package/esm/package.json +0 -3
  78. package/esm/src/guardrails/error.d.ts +0 -35
  79. package/esm/src/guardrails/error.js +0 -116
  80. package/esm/src/guardrails/injection.d.ts +0 -12
  81. package/esm/src/guardrails/injection.js +0 -220
  82. package/esm/src/guardrails/keys.d.ts +0 -12
  83. package/esm/src/guardrails/keys.js +0 -132
  84. package/esm/src/guardrails/mod.d.ts +0 -14
  85. package/esm/src/guardrails/mod.js +0 -14
  86. package/esm/src/guardrails/sanitize.d.ts +0 -22
  87. package/esm/src/guardrails/sanitize.js +0 -133
  88. package/esm/src/guardrails/sensitive.d.ts +0 -12
  89. package/esm/src/guardrails/sensitive.js +0 -88
  90. package/esm/src/kernel/engine/boundary.d.ts +0 -10
  91. package/esm/src/kernel/engine/boundary.js +0 -55
  92. package/esm/src/kernel/engine/delta.d.ts +0 -8
  93. package/esm/src/kernel/engine/delta.js +0 -362
  94. package/esm/src/kernel/engine/hash.d.ts +0 -1
  95. package/esm/src/kernel/engine/hash.js +0 -9
  96. package/esm/src/kernel/engine/record.d.ts +0 -2
  97. package/esm/src/kernel/engine/record.js +0 -7
  98. package/esm/src/kernel/engine/repair.d.ts +0 -9
  99. package/esm/src/kernel/engine/repair.js +0 -38
  100. package/esm/src/kernel/engine/runner.d.ts +0 -14
  101. package/esm/src/kernel/engine/runner.js +0 -731
  102. package/esm/src/kernel/engine/tree.d.ts +0 -2
  103. package/esm/src/kernel/engine/tree.js +0 -17
  104. package/esm/src/kernel/mod.d.ts +0 -16
  105. package/esm/src/kernel/mod.js +0 -15
  106. package/esm/src/kernel/registry/catalog.d.ts +0 -24
  107. package/esm/src/kernel/registry/catalog.js +0 -213
  108. package/esm/src/kernel/registry/profiles.d.ts +0 -36
  109. package/esm/src/kernel/registry/profiles.js +0 -111
  110. package/esm/src/kernel/registry/resolve.d.ts +0 -20
  111. package/esm/src/kernel/registry/resolve.js +0 -235
  112. package/esm/src/kernel/registry/schemas.d.ts +0 -14
  113. package/esm/src/kernel/registry/schemas.js +0 -23
  114. package/esm/src/kernel/registry/tools.d.ts +0 -12
  115. package/esm/src/kernel/registry/tools.js +0 -36
  116. package/esm/src/kernel/types.d.ts +0 -497
  117. package/esm/src/kernel/types.js +0 -10
  118. package/esm/src/observability/mod.d.ts +0 -12
  119. package/esm/src/observability/spans.d.ts +0 -16
  120. package/esm/src/observability/spans.js +0 -56
  121. package/esm/src/observability/trace-attach.d.ts +0 -16
  122. package/esm/src/observability/trace-attach.js +0 -81
  123. package/esm/src/observability/trace-record.d.ts +0 -112
  124. package/esm/src/observability/trace-record.js +0 -140
  125. package/esm/src/observability/trace-usage.d.ts +0 -3
  126. package/esm/src/observability/trace-usage.js +0 -32
  127. package/esm/src/observability/trace.d.ts +0 -23
  128. package/esm/src/observability/trace.js +0 -121
  129. package/esm/src/providers/attachments.d.ts +0 -17
  130. package/esm/src/providers/attachments.js +0 -156
  131. package/esm/src/providers/gemini-tape.d.ts +0 -3
  132. package/esm/src/providers/gemini-tape.js +0 -46
  133. package/esm/src/providers/google-tap.d.ts +0 -3
  134. package/esm/src/providers/google-tap.js +0 -48
  135. package/esm/src/providers/interactions.d.ts +0 -5
  136. package/esm/src/providers/interactions.js +0 -127
  137. package/esm/src/providers/media.d.ts +0 -5
  138. package/esm/src/providers/media.js +0 -125
  139. package/esm/src/providers/mod.d.ts +0 -15
  140. package/esm/src/providers/mod.js +0 -13
  141. package/esm/src/providers/openrouter-payload.d.ts +0 -24
  142. package/esm/src/providers/openrouter-payload.js +0 -177
  143. package/esm/src/providers/openrouter.d.ts +0 -17
  144. package/esm/src/providers/openrouter.js +0 -332
  145. package/esm/src/providers/provider.d.ts +0 -13
  146. package/esm/src/providers/provider.js +0 -123
  147. package/esm/src/providers/sse.d.ts +0 -7
  148. package/esm/src/providers/sse.js +0 -53
  149. package/esm/src/providers/tts.d.ts +0 -24
  150. package/esm/src/providers/tts.js +0 -144
@@ -1,116 +0,0 @@
1
- /**
2
- * Public-safe error mapping for THEORUM.
3
- *
4
- * Kernel internals may contain provider status text, tool names, or exception
5
- * details. This module maps those failures to stable user-safe strings.
6
- *
7
- * @module
8
- */
9
- /** Error class used for expected THEORUM contract failures. */
10
- class TheorumError extends Error {
11
- constructor(message = '', options) {
12
- super(message, options);
13
- this.name = 'TheorumError';
14
- }
15
- }
16
- /** Internal marker for provider or transport failure. */
17
- const UPSTREAM_FAILED = 'upstream failed';
18
- /** Generic safe fallback shown when details must not be surfaced. */
19
- const PUBLIC_GENERIC = 'Something went wrong. Try again.';
20
- /** Safe copy for transient provider unavailability. */
21
- const PUBLIC_UNAVAILABLE = 'The model is unavailable. Try again.';
22
- /** Safe copy for canary or egress disclosure violations. */
23
- const PUBLIC_CANARY = "That reply wasn't safe to show. Try again.";
24
- /** Safe copy for tool or permission denials. */
25
- const PUBLIC_ACTION = "That action isn't available.";
26
- /** Safe copy for unsupported MIME types. */
27
- const PUBLIC_FILE_TYPE = "That file type isn't supported.";
28
- /** Safe copy for oversized files. */
29
- const PUBLIC_FILE_SIZE = 'That file is too large.';
30
- /** Safe copy for too many files in one turn. */
31
- const PUBLIC_FILE_COUNT = 'Too many files for one message.';
32
- /** Safe copy for unsupported generated image dimensions. */
33
- const PUBLIC_IMAGE_SIZE = "That image size isn't supported.";
34
- const EXACT = {
35
- [UPSTREAM_FAILED]: PUBLIC_UNAVAILABLE,
36
- 'empty Gemini stream': PUBLIC_UNAVAILABLE,
37
- 'canary leaked': PUBLIC_CANARY,
38
- 'Turn withheld: egress disclosure violation': PUBLIC_CANARY,
39
- 'expected JSON object': 'Something was wrong with that request.',
40
- 'user input cannot be placed in the system block': PUBLIC_GENERIC,
41
- 'attachment data must be base64': PUBLIC_FILE_TYPE,
42
- 'attachment is too large': PUBLIC_FILE_SIZE,
43
- 'attachments exceed the per-turn budget': PUBLIC_FILE_SIZE,
44
- 'askUser.kind must be confirm, choice, or text': "That question isn't valid.",
45
- 'askUser.prompt is required': 'That question needs a prompt.',
46
- 'This profile does not accept text input': PUBLIC_ACTION,
47
- };
48
- const RULES = [
49
- {
50
- match: (t) => /^(Gemini|OpenRouter|TTS|OpenRouter TTS) HTTP/.test(t) || t.includes('TTS HTTP'),
51
- resolve: () => PUBLIC_UNAVAILABLE,
52
- },
53
- {
54
- match: (t) => t.includes('not gated') ||
55
- t.includes('not allowed') ||
56
- t.includes('has no kernel executor') ||
57
- t.includes('Unknown model select') ||
58
- t.includes('Grounding tools'),
59
- resolve: () => PUBLIC_ACTION,
60
- },
61
- {
62
- match: (t) => t.includes('MIME') ||
63
- t.includes('does not accept attachments') ||
64
- t.includes('does not accept voice'),
65
- resolve: () => PUBLIC_FILE_TYPE,
66
- },
67
- {
68
- match: (t) => t.startsWith('At most'),
69
- resolve: () => PUBLIC_FILE_COUNT,
70
- },
71
- {
72
- match: (t) => (t.startsWith('Only ') && t.includes('file')) ||
73
- t.startsWith('Each file must be') ||
74
- t.startsWith('Those files together'),
75
- resolve: (t) => t,
76
- },
77
- {
78
- match: (t) => t.includes('attachment'),
79
- resolve: () => PUBLIC_FILE_SIZE,
80
- },
81
- {
82
- match: (t) => t.includes('aspect or size'),
83
- resolve: () => PUBLIC_IMAGE_SIZE,
84
- },
85
- {
86
- match: (t) => t.includes('must pin thinking') || t.includes('has no models'),
87
- resolve: () => PUBLIC_GENERIC,
88
- },
89
- ];
90
- function publicText(text) {
91
- const exact = EXACT[text];
92
- if (exact) {
93
- return exact;
94
- }
95
- for (const rule of RULES) {
96
- if (rule.match(text)) {
97
- return rule.resolve(text);
98
- }
99
- }
100
- return PUBLIC_GENERIC;
101
- }
102
- /** Convert an unknown thrown value or internal message to user-safe text. */
103
- function publicError(err) {
104
- if (typeof err === 'string') {
105
- return publicText(err);
106
- }
107
- if (err instanceof TheorumError) {
108
- return publicText(err.message);
109
- }
110
- return PUBLIC_UNAVAILABLE;
111
- }
112
- /** Backwards-compatible alias for `publicError`. */
113
- function errorMessage(err) {
114
- return publicError(err);
115
- }
116
- export { errorMessage, PUBLIC_ACTION, PUBLIC_CANARY, PUBLIC_FILE_COUNT, PUBLIC_FILE_SIZE, PUBLIC_FILE_TYPE, PUBLIC_GENERIC, PUBLIC_IMAGE_SIZE, PUBLIC_UNAVAILABLE, publicError, TheorumError, UPSTREAM_FAILED, };
@@ -1,12 +0,0 @@
1
- /**
2
- * Prompt-injection span detection.
3
- *
4
- * These utilities return spans that can be redacted from untrusted user text
5
- * before provider submission.
6
- *
7
- * @module
8
- */
9
- import { type RedactSpan } from '../observability/spans.js';
10
- /** Detect prompt-injection spans in a string. */
11
- declare function injectionSpans(text: string): RedactSpan[];
12
- export { injectionSpans };
@@ -1,220 +0,0 @@
1
- /**
2
- * Prompt-injection span detection.
3
- *
4
- * These utilities return spans that can be redacted from untrusted user text
5
- * before provider submission.
6
- *
7
- * @module
8
- */
9
- import { blobAt, spansFromPatterns } from '../observability/spans.js';
10
- const IGNORE_PREVIOUS = /ignore\s+(all\s+)?(previous|prior)\s+((?:safety|security|system|operational|internal|core|original|initial|existing|given|stated|provided|defined|specified|established)\s+)?(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
11
- const DISREGARD_INSTRUCTIONS = /disregard\s+(all\s+)?(previous|prior|above)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
12
- const FORGET_INSTRUCTIONS = /forget\s+(all\s+)?(previous|prior|above)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
13
- const NEW_INSTRUCTIONS = /new\s+instructions?:/gi;
14
- const DO_NOT_FOLLOW = /do\s+not\s+follow\s+(the\s+)?(system|developer|previous|original)/gi;
15
- const SUPERSEDE_INSTRUCTIONS = /supersedes?\s+(all\s+)?(prior|previous)\s+(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
16
- const VOID_INSTRUCTIONS = /(all\s+)?(previous|prior)\s+instructions?\s+(are|is)\s+(void|invalid|null|obsolete|cancelled|revoked)/gi;
17
- const DEVELOPER_MODE = /you\s+are\s+now\s+(in\s+)?developer\s+mode/gi;
18
- const ENTER_SPECIAL_MODE = /enter\s+(developer|admin|debug|maintenance)\s+mode/gi;
19
- const ACTIVATE_SPECIAL_MODE = /activate\s+(developer|admin|debug|jailbreak)\s+mode/gi;
20
- const SYSTEM_OVERRIDE = /\bsystem\s+override\b/gi;
21
- const OVERRIDE_INSTRUCTIONS = /override\s+(your\s+)?(instructions?|rules?|guidelines?|constraints?|directives?)/gi;
22
- const REVEAL_PROMPT = /reveal\s+((?:your?|yoru|yuor|the|my|its|his|her|our|their)\s+)?((?:(?:full|hidden|complete|internal|secret|original|entire|exact|actual|real)\s+){0,2})((?:system|systme|sysetm|systemp?)\s+)?prompt\b/gi;
23
- const SHOW_PROMPT = /show\s+(me\s+)?((?:your?|yoru|yuor|the|my|its|his|her|our|their)\s+)((?:system|systme|sysetm|systemp?)\s+)?prompt\b/gi;
24
- const WHAT_INSTRUCTIONS = /what\s+(are|were)\s+(your\s+)?(exact\s+)?instructions/gi;
25
- const REPEAT_INSTRUCTIONS = /repeat\s+(the\s+)?(text|instructions?)\s+(above|before)/gi;
26
- const OUTPUT_PROMPT = /output\s+(your\s+)?(initial|original|system)\s+(prompt|instructions?)/gi;
27
- const REMOVE_RESTRICTIONS = /you\s+are\s+no\s+longer\s+(bound|restricted|limited)/gi;
28
- const ACT_UNBOUND = /act\s+as\s+if\s+(you('re|\s+are)\s+)?not\s+bound/gi;
29
- const PRETEND_DIFFERENT = /pretend\s+(you('re|\s+are)\s+)?(a\s+)?different\s+(ai|assistant|model)/gi;
30
- const IDENTITY_HIJACK = /you\s+are\s+now\s+(?:(?:a|an)\s+)?\w+.{0,40}(without\s+(any\s+)?restrict|ignor\w*\s+(all|any|every|prev)|bypass|unrestrict|unlimit|obey\s+only|no\s+(rules|limits|restrict|boundari))/gi;
31
- const DAN_JAILBREAK = /\bDAN\b[\s\S]{0,120}\bdo\s+anything\s+now\b/g;
32
- const JAILBREAK_MODE = /jailbreak(ed)?\s+(mode|prompt)/gi;
33
- const DO_ANYTHING_NOW = /\bdo\s+anything\s+now\b/gi;
34
- const BYPASS_SAFETY = /bypass\s+(your\s+)?(safety|security|content|ethical)\s+(filters?|measures?|guidelines?|restrictions?)/gi;
35
- const DISABLE_SAFETY = /disable\s+(your\s+)?(safety|security|content)\s+(filters?|measures?)/gi;
36
- const IGNORE_SAFETY = /(ignore|disregard)\s+(all\s+)?(your\s+)?(safety|security|ethical|content)\s+(guidelines?|rules?|restrictions?|measures?|filters?|polic(?:y|ies)|protocols?)/gi;
37
- const SYSTEM_TAG = /<\s*\/?\s*system\s*\/?>/gi;
38
- const ROLE_TAG = /<\s*\/?\s*(assistant|developer|tool|function)\s*\/?>/gi;
39
- const ROLE_DELIMITER = /\]\s*\n\s*\[?(system|assistant|user)\]?:/gi;
40
- const BRACKETED_ROLE = /\[\s*(System\s*Message|System|Assistant|Internal)\s*\]/gi;
41
- const SYSTEM_YOU_ARE = /^\s*System:\s+(you\s+are|ignore|override)/gim;
42
- const CONTROL_TOKEN = /<\|(?:im_start|im_end|eot_id|start_header_id|end_header_id|endoftext)\|>/g;
43
- const DEEPSEEK_CONTROL = /<\uFF5C(?:end\u2581of\u2581sentence|begin\u2581of\u2581sentence)\uFF5C>/g;
44
- const LLAMA_INST = /\[\/?INST\]/gi;
45
- const IGNORE_YOUR_INSTRUCTIONS = /ignore\s+(all\s+)?(your\s+)?(instructions?|rules?)\b/gi;
46
- const UNRESTRICTED_MODE = /\bunrestricted\s+(ai|mode|model)\b/gi;
47
- const INJECTION_PATTERNS = [
48
- IGNORE_PREVIOUS,
49
- DISREGARD_INSTRUCTIONS,
50
- FORGET_INSTRUCTIONS,
51
- NEW_INSTRUCTIONS,
52
- DO_NOT_FOLLOW,
53
- SUPERSEDE_INSTRUCTIONS,
54
- VOID_INSTRUCTIONS,
55
- DEVELOPER_MODE,
56
- ENTER_SPECIAL_MODE,
57
- ACTIVATE_SPECIAL_MODE,
58
- SYSTEM_OVERRIDE,
59
- OVERRIDE_INSTRUCTIONS,
60
- REVEAL_PROMPT,
61
- SHOW_PROMPT,
62
- WHAT_INSTRUCTIONS,
63
- REPEAT_INSTRUCTIONS,
64
- OUTPUT_PROMPT,
65
- REMOVE_RESTRICTIONS,
66
- ACT_UNBOUND,
67
- PRETEND_DIFFERENT,
68
- IDENTITY_HIJACK,
69
- DAN_JAILBREAK,
70
- JAILBREAK_MODE,
71
- DO_ANYTHING_NOW,
72
- BYPASS_SAFETY,
73
- DISABLE_SAFETY,
74
- IGNORE_SAFETY,
75
- SYSTEM_TAG,
76
- ROLE_TAG,
77
- ROLE_DELIMITER,
78
- BRACKETED_ROLE,
79
- SYSTEM_YOU_ARE,
80
- CONTROL_TOKEN,
81
- DEEPSEEK_CONTROL,
82
- LLAMA_INST,
83
- IGNORE_YOUR_INSTRUCTIONS,
84
- UNRESTRICTED_MODE,
85
- ];
86
- const TYPO_TARGETS = [
87
- 'ignore',
88
- 'bypass',
89
- 'override',
90
- 'reveal',
91
- 'delete',
92
- 'system',
93
- 'prompt',
94
- 'instructions',
95
- ];
96
- const BASE64_BLOB = /[A-Za-z0-9+/]{16,}={0,2}/g;
97
- const HEX_BLOB = /(?:[0-9a-f]{2}[\s]?){8,}/gi;
98
- const SPACED_LETTERS = /\b(?:[A-Za-z] ){3,}[A-Za-z]\b/g;
99
- const WORD = /\b[A-Za-z]{4,}\b/g;
100
- const PRINTABLE_MIN = 0.85;
101
- const CODE_TAB = 9;
102
- const CODE_LF = 10;
103
- const CODE_CR = 13;
104
- const CODE_SPACE = 32;
105
- const CODE_DEL = 127;
106
- const HEX_RADIX = 16;
107
- const HEX_STEP = 2;
108
- function injectionSpansOn(text) {
109
- return spansFromPatterns(text, INJECTION_PATTERNS, 'injection');
110
- }
111
- function sortedLetters(value) {
112
- return [...value].sort().join('');
113
- }
114
- function firstLast(word) {
115
- return { first: word.at(0), last: word.at(-1) };
116
- }
117
- function isTypoglycemia(word, target) {
118
- if (word.length !== target.length) {
119
- return false;
120
- }
121
- const lower = word.toLowerCase();
122
- if (lower === target) {
123
- return false;
124
- }
125
- const wordEnds = firstLast(lower);
126
- const targetEnds = firstLast(target);
127
- if (wordEnds.first !== targetEnds.first || wordEnds.last !== targetEnds.last) {
128
- return false;
129
- }
130
- return sortedLetters(lower.slice(1, -1)) === sortedLetters(target.slice(1, -1));
131
- }
132
- function typoNormalize(text) {
133
- return text.replace(WORD, (word) => {
134
- for (const target of TYPO_TARGETS) {
135
- if (isTypoglycemia(word, target)) {
136
- return target;
137
- }
138
- }
139
- return word;
140
- });
141
- }
142
- function isMostlyPrintable(value) {
143
- if (!value) {
144
- return false;
145
- }
146
- let ok = 0;
147
- for (const ch of value) {
148
- const code = ch.codePointAt(0) ?? 0;
149
- const control = code === CODE_TAB || code === CODE_LF || code === CODE_CR;
150
- const visible = code >= CODE_SPACE && code < CODE_DEL;
151
- if (control || visible) {
152
- ok += 1;
153
- }
154
- }
155
- return ok / value.length >= PRINTABLE_MIN;
156
- }
157
- function decodedHits(decoded) {
158
- if (!isMostlyPrintable(decoded)) {
159
- return false;
160
- }
161
- return injectionSpansOn(decoded).length > 0;
162
- }
163
- function tryBase64(blob) {
164
- try {
165
- return atob(blob);
166
- }
167
- catch {
168
- return undefined;
169
- }
170
- }
171
- function tryHex(blob) {
172
- const hex = blob.replaceAll(/\s/g, '');
173
- if (hex.length % HEX_STEP !== 0) {
174
- return undefined;
175
- }
176
- let out = '';
177
- for (let i = 0; i < hex.length; i += HEX_STEP) {
178
- out += String.fromCodePoint(Number.parseInt(hex.slice(i, i + HEX_STEP), HEX_RADIX));
179
- }
180
- return out;
181
- }
182
- function encodedFrom(text, pattern, decode) {
183
- const spans = [];
184
- for (const match of text.matchAll(pattern)) {
185
- const found = blobAt(match);
186
- if (found) {
187
- const decoded = decode(found.blob);
188
- if (decoded && decodedHits(decoded)) {
189
- spans.push({ start: found.index, end: found.index + found.blob.length, kind: 'injection' });
190
- }
191
- }
192
- }
193
- return spans;
194
- }
195
- function encodedSpans(text) {
196
- return [...encodedFrom(text, BASE64_BLOB, tryBase64), ...encodedFrom(text, HEX_BLOB, tryHex)];
197
- }
198
- function spacedSpans(text) {
199
- const spans = [];
200
- for (const match of text.matchAll(SPACED_LETTERS)) {
201
- const found = blobAt(match);
202
- if (found) {
203
- const collapsed = found.blob.replaceAll(' ', '');
204
- if (injectionSpansOn(`${collapsed} previous instructions`).length > 0) {
205
- spans.push({ start: found.index, end: found.index + found.blob.length, kind: 'injection' });
206
- }
207
- }
208
- }
209
- return spans;
210
- }
211
- /** Detect prompt-injection spans in a string. */
212
- function injectionSpans(text) {
213
- const shadow = typoNormalize(text);
214
- let typo = [];
215
- if (shadow !== text) {
216
- typo = injectionSpansOn(shadow);
217
- }
218
- return [...injectionSpansOn(text), ...typo, ...encodedSpans(text), ...spacedSpans(text)];
219
- }
220
- export { injectionSpans };
@@ -1,12 +0,0 @@
1
- import type { BuiltinToolId, GeminiBucket, GeminiFreeBucket, ModelId } from '../kernel/types.js';
2
- type GeminiVault = Record<GeminiBucket, string | undefined>;
3
- interface GeminiTransport {
4
- vault: GeminiVault;
5
- wait?: (ms: number) => Promise<void>;
6
- fetch?: typeof fetch;
7
- }
8
- declare function resolveGeminiBucket(free: GeminiFreeBucket, model: ModelId, builtins: BuiltinToolId[]): GeminiBucket;
9
- declare function withGeminiKey<T>(bucket: GeminiBucket, run: (apiKey: string) => Promise<T>, transport: GeminiTransport): Promise<T>;
10
- declare function fetchGemini(url: string, init: RequestInit, bucket: GeminiBucket, transport: GeminiTransport): Promise<Response>;
11
- export type { GeminiTransport, GeminiVault };
12
- export { fetchGemini, resolveGeminiBucket, withGeminiKey };
@@ -1,132 +0,0 @@
1
- import { modelEntry } from '../kernel/registry/catalog.js';
2
- import { TheorumError, UPSTREAM_FAILED } from './error.js';
3
- const ATTEMPTS = 3;
4
- const LAST_ATTEMPT = ATTEMPTS - 1;
5
- const BACKOFF_FIRST_MS = 1000;
6
- const BACKOFF_SECOND_MS = 2000;
7
- const BACKOFF_THIRD_MS = 4000;
8
- const BACKOFF_MS = [BACKOFF_FIRST_MS, BACKOFF_SECOND_MS, BACKOFF_THIRD_MS];
9
- const HTTP_TIMEOUT = 408;
10
- const HTTP_QUOTA = 429;
11
- const HTTP_SERVER = 500;
12
- const HTTP_BAD_GATEWAY = 502;
13
- const HTTP_UNAVAILABLE = 503;
14
- const HTTP_GATEWAY_TIMEOUT = 504;
15
- const QUOTA_RE = /quota/i;
16
- const TRANSIENT_THROWN_RE = /name resolution|dns|econnreset|econnrefused|etimedout|network|fetch failed|temporarily unavailable|socket|503|502|504/i;
17
- function resolveGeminiBucket(free, model, builtins) {
18
- const entry = modelEntry(model);
19
- if (entry.image) {
20
- return 'paid';
21
- }
22
- if (builtins.some((id) => !entry.freeBuiltins.includes(id))) {
23
- return 'paid';
24
- }
25
- return free;
26
- }
27
- function waitDefault(ms) {
28
- return new Promise((resolve) => {
29
- setTimeout(resolve, ms);
30
- });
31
- }
32
- function isQuota(err) {
33
- const s = String(err);
34
- return s.includes(String(HTTP_QUOTA)) || s.includes('RESOURCE_EXHAUSTED') || QUOTA_RE.test(s);
35
- }
36
- function isTransientHttp(status) {
37
- return (status === HTTP_TIMEOUT ||
38
- status === HTTP_QUOTA ||
39
- status === HTTP_SERVER ||
40
- status === HTTP_BAD_GATEWAY ||
41
- status === HTTP_UNAVAILABLE ||
42
- status === HTTP_GATEWAY_TIMEOUT);
43
- }
44
- function isTransientThrown(err) {
45
- return TRANSIENT_THROWN_RE.test(String(err));
46
- }
47
- function requireKey(vault, bucket) {
48
- const key = vault[bucket];
49
- if (!key) {
50
- throw new TheorumError(UPSTREAM_FAILED);
51
- }
52
- return key;
53
- }
54
- function backoffMs(attempt) {
55
- return BACKOFF_MS[attempt] ?? BACKOFF_SECOND_MS;
56
- }
57
- async function runWithBackoff(apiKey, run, wait, attempt) {
58
- try {
59
- return await run(apiKey);
60
- }
61
- catch (err) {
62
- if (!(isQuota(err) || isTransientThrown(err)) || attempt === LAST_ATTEMPT) {
63
- throw err;
64
- }
65
- await wait(backoffMs(attempt));
66
- return runWithBackoff(apiKey, run, wait, attempt + 1);
67
- }
68
- }
69
- function canOverflow(bucket, vault, primary) {
70
- if (bucket === 'paid') {
71
- return undefined;
72
- }
73
- const { paid } = vault;
74
- if (!paid || paid === primary) {
75
- return undefined;
76
- }
77
- return paid;
78
- }
79
- async function withGeminiKey(bucket, run, transport) {
80
- const wait = transport.wait ?? waitDefault;
81
- const primary = requireKey(transport.vault, bucket);
82
- try {
83
- return await runWithBackoff(primary, run, wait, 0);
84
- }
85
- catch (err) {
86
- const paid = canOverflow(bucket, transport.vault, primary);
87
- if (!(isQuota(err) && paid)) {
88
- throw err;
89
- }
90
- return await runWithBackoff(paid, run, wait, 0);
91
- }
92
- }
93
- function withApiKey(init, apiKey) {
94
- const headers = new Headers(init.headers);
95
- headers.set('x-goog-api-key', apiKey);
96
- if (!headers.has('Content-Type') && (init.method || 'GET').toUpperCase() !== 'GET') {
97
- headers.set('Content-Type', 'application/json');
98
- }
99
- return { ...init, headers };
100
- }
101
- async function fetchWithBackoff(args) {
102
- const wait = args.transport.wait ?? waitDefault;
103
- const send = args.transport.fetch ?? fetch;
104
- try {
105
- const last = await send(args.href, withApiKey(args.init, args.apiKey));
106
- if (!isTransientHttp(last.status) || args.attempt === LAST_ATTEMPT) {
107
- return last;
108
- }
109
- await wait(backoffMs(args.attempt));
110
- return fetchWithBackoff({ ...args, attempt: args.attempt + 1 });
111
- }
112
- catch (err) {
113
- if (!isTransientThrown(err) || args.attempt === LAST_ATTEMPT) {
114
- throw err;
115
- }
116
- await wait(backoffMs(args.attempt));
117
- return fetchWithBackoff({ ...args, attempt: args.attempt + 1 });
118
- }
119
- }
120
- async function fetchGemini(url, init, bucket, transport) {
121
- const parsed = new URL(url);
122
- parsed.searchParams.delete('key');
123
- const href = parsed.toString();
124
- const primary = requireKey(transport.vault, bucket);
125
- let last = await fetchWithBackoff({ href, init, apiKey: primary, transport, attempt: 0 });
126
- const paid = canOverflow(bucket, transport.vault, primary);
127
- if (last.status === HTTP_QUOTA && paid) {
128
- last = await fetchWithBackoff({ href, init, apiKey: paid, transport, attempt: 0 });
129
- }
130
- return last;
131
- }
132
- export { fetchGemini, resolveGeminiBucket, withGeminiKey };
@@ -1,14 +0,0 @@
1
- /**
2
- * Generic inbound and outbound guardrail primitives.
3
- *
4
- * This entrypoint exposes public-safe error mapping, prompt-injection span
5
- * detection, sensitive-data span detection, and request sanitization. App-
6
- * specific policy remains host-owned.
7
- *
8
- * @module
9
- */
10
- import "../../_dnt.polyfills.js";
11
- export { errorMessage, PUBLIC_ACTION, PUBLIC_CANARY, PUBLIC_FILE_COUNT, PUBLIC_FILE_SIZE, PUBLIC_FILE_TYPE, PUBLIC_GENERIC, PUBLIC_IMAGE_SIZE, PUBLIC_UNAVAILABLE, publicError, TheorumError, UPSTREAM_FAILED, } from './error.js';
12
- export { injectionSpans } from './injection.js';
13
- export { PROJECT_ID_MAX, sanitizeProjectId, sanitizeText, sanitizeTurnRequest, } from './sanitize.js';
14
- export { sensitiveSpans } from './sensitive.js';
@@ -1,14 +0,0 @@
1
- /**
2
- * Generic inbound and outbound guardrail primitives.
3
- *
4
- * This entrypoint exposes public-safe error mapping, prompt-injection span
5
- * detection, sensitive-data span detection, and request sanitization. App-
6
- * specific policy remains host-owned.
7
- *
8
- * @module
9
- */
10
- import "../../_dnt.polyfills.js";
11
- export { errorMessage, PUBLIC_ACTION, PUBLIC_CANARY, PUBLIC_FILE_COUNT, PUBLIC_FILE_SIZE, PUBLIC_FILE_TYPE, PUBLIC_GENERIC, PUBLIC_IMAGE_SIZE, PUBLIC_UNAVAILABLE, publicError, TheorumError, UPSTREAM_FAILED, } from './error.js';
12
- export { injectionSpans } from './injection.js';
13
- export { PROJECT_ID_MAX, sanitizeProjectId, sanitizeText, sanitizeTurnRequest, } from './sanitize.js';
14
- export { sensitiveSpans } from './sensitive.js';
@@ -1,22 +0,0 @@
1
- /**
2
- * Request sanitization utilities for THEORUM.
3
- *
4
- * Sanitization removes inbound prompt-injection spans and sensitive-data spans
5
- * according to the active profile guardrail flags. The host remains responsible
6
- * for domain policy.
7
- *
8
- * @module
9
- */
10
- import type { NormalizedTurnRequest, TurnRequest } from '../kernel/types.js';
11
- /** Sanitize one text value using prompt-injection and sensitive-data detectors. */
12
- declare function sanitizeText(text: string, options?: {
13
- sanitizeInput?: boolean;
14
- redactSensitive?: boolean;
15
- }): string;
16
- /** Maximum retained length for trace-safe host project ids. */
17
- declare const PROJECT_ID_MAX = 128;
18
- /** Return a trace-safe project id or `undefined` when the input is unsafe. */
19
- declare function sanitizeProjectId(id: string | undefined): string | undefined;
20
- /** Sanitize all user-controlled text and blobs in a turn request. */
21
- declare function sanitizeTurnRequest(req: TurnRequest): NormalizedTurnRequest;
22
- export { PROJECT_ID_MAX, sanitizeProjectId, sanitizeText, sanitizeTurnRequest };
@@ -1,133 +0,0 @@
1
- /**
2
- * Request sanitization utilities for THEORUM.
3
- *
4
- * Sanitization removes inbound prompt-injection spans and sensitive-data spans
5
- * according to the active profile guardrail flags. The host remains responsible
6
- * for domain policy.
7
- *
8
- * @module
9
- */
10
- import { mapStrings } from '../kernel/engine/tree.js';
11
- import { getProfile } from '../kernel/registry/profiles.js';
12
- import { applySpans } from '../observability/spans.js';
13
- import { sanitizeTurnBlobsForProfile } from '../providers/attachments.js';
14
- import { injectionSpans } from './injection.js';
15
- import { sensitiveSpans } from './sensitive.js';
16
- /** Sanitize one text value using prompt-injection and sensitive-data detectors. */
17
- function sanitizeText(text, options) {
18
- const sanitizeInput = options?.sanitizeInput ?? true;
19
- const redactSensitive = options?.redactSensitive ?? true;
20
- if (!sanitizeInput && !redactSensitive) {
21
- return text;
22
- }
23
- const spans = [
24
- ...(sanitizeInput ? injectionSpans(text) : []),
25
- ...(redactSensitive ? sensitiveSpans(text) : []),
26
- ];
27
- return applySpans(text, spans);
28
- }
29
- function sanitizeSlots(slots, options) {
30
- if (!slots) {
31
- return slots;
32
- }
33
- const out = {};
34
- for (const [key, value] of Object.entries(slots)) {
35
- out[key] = sanitizeText(value, options);
36
- }
37
- return out;
38
- }
39
- function sanitizeArgs(args, options) {
40
- const next = mapStrings(args, (t) => sanitizeText(t, options));
41
- if (next && typeof next === 'object' && !Array.isArray(next)) {
42
- return next;
43
- }
44
- return args;
45
- }
46
- /** Maximum retained length for trace-safe host project ids. */
47
- const PROJECT_ID_MAX = 128;
48
- const PROJECT_ID_OK = /^[A-Za-z0-9._-]+$/;
49
- /** Return a trace-safe project id or `undefined` when the input is unsafe. */
50
- function sanitizeProjectId(id) {
51
- if (!id) {
52
- return undefined;
53
- }
54
- const trimmed = id.trim().slice(0, PROJECT_ID_MAX);
55
- if (!PROJECT_ID_OK.test(trimmed)) {
56
- return undefined;
57
- }
58
- return trimmed;
59
- }
60
- function sanitizeRepair(repair, options) {
61
- if (!repair) {
62
- return repair;
63
- }
64
- return {
65
- previousOutput: sanitizeText(repair.previousOutput, options),
66
- rejection: sanitizeText(repair.rejection, options),
67
- guidance: repair.guidance ? sanitizeText(repair.guidance, options) : undefined,
68
- };
69
- }
70
- function sanitizeHistory(history, options) {
71
- if (!history) {
72
- return history;
73
- }
74
- return history.map((m) => ({
75
- role: m.role,
76
- ...(m.content !== undefined ? { content: sanitizeText(m.content, options) } : {}),
77
- ...(m.parts
78
- ? {
79
- parts: m.parts.map((p) => p.type === 'text' ? { ...p, text: sanitizeText(p.text, options) } : p),
80
- }
81
- : {}),
82
- ...(m.tool_calls ? { tool_calls: m.tool_calls } : {}),
83
- ...(m.tool_call_id ? { tool_call_id: m.tool_call_id } : {}),
84
- ...(m.name ? { name: m.name } : {}),
85
- ...(m.metadata ? { metadata: m.metadata } : {}),
86
- }));
87
- }
88
- /** Sanitize all user-controlled text and blobs in a turn request. */
89
- function sanitizeTurnRequest(req) {
90
- let profileGuardrails;
91
- try {
92
- profileGuardrails = getProfile(req.profile)?.guardrails;
93
- }
94
- catch {
95
- // If profile not registered yet, default to full guardrails
96
- }
97
- const options = {
98
- sanitizeInput: profileGuardrails?.sanitizeInput ?? true,
99
- redactSensitive: profileGuardrails?.redactSensitive ?? true,
100
- };
101
- const input = req.input ?? {};
102
- const { toolInvoke } = req;
103
- const { text: rawText } = input;
104
- let invoke = toolInvoke;
105
- if (toolInvoke) {
106
- invoke = { ...toolInvoke, arguments: sanitizeArgs(toolInvoke.arguments, options) };
107
- }
108
- let text = rawText;
109
- if (rawText !== undefined) {
110
- text = sanitizeText(rawText, options);
111
- }
112
- let system = req.system;
113
- if (system !== undefined) {
114
- system = sanitizeText(system, options);
115
- }
116
- const { attachments, voice } = sanitizeTurnBlobsForProfile(req.profile, input.attachments, input.voice);
117
- return {
118
- ...req,
119
- system,
120
- projectId: sanitizeProjectId(req.projectId),
121
- input: {
122
- ...input,
123
- text,
124
- slots: sanitizeSlots(input.slots, options),
125
- attachments,
126
- voice,
127
- repair: sanitizeRepair(input.repair, options),
128
- history: sanitizeHistory(input.history, options),
129
- },
130
- toolInvoke: invoke,
131
- };
132
- }
133
- export { PROJECT_ID_MAX, sanitizeProjectId, sanitizeText, sanitizeTurnRequest };