@cogitator-ai/core 0.12.0 → 0.14.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.
Files changed (60) hide show
  1. package/README.md +93 -0
  2. package/dist/cache/cache-key.d.ts.map +1 -1
  3. package/dist/cache/cache-key.js +1 -4
  4. package/dist/cache/cache-key.js.map +1 -1
  5. package/dist/cache/tool-cache.d.ts.map +1 -1
  6. package/dist/cache/tool-cache.js +3 -6
  7. package/dist/cache/tool-cache.js.map +1 -1
  8. package/dist/cogitator/initializers.d.ts +4 -0
  9. package/dist/cogitator/initializers.d.ts.map +1 -1
  10. package/dist/cogitator/initializers.js +14 -0
  11. package/dist/cogitator/initializers.js.map +1 -1
  12. package/dist/cogitator.d.ts +38 -1
  13. package/dist/cogitator.d.ts.map +1 -1
  14. package/dist/cogitator.js +56 -1
  15. package/dist/cogitator.js.map +1 -1
  16. package/dist/cost-routing/cost-estimator.d.ts +18 -0
  17. package/dist/cost-routing/cost-estimator.d.ts.map +1 -0
  18. package/dist/cost-routing/cost-estimator.js +149 -0
  19. package/dist/cost-routing/cost-estimator.js.map +1 -0
  20. package/dist/cost-routing/index.d.ts +2 -0
  21. package/dist/cost-routing/index.d.ts.map +1 -1
  22. package/dist/cost-routing/index.js +2 -0
  23. package/dist/cost-routing/index.js.map +1 -1
  24. package/dist/cost-routing/token-estimator.d.ts +22 -0
  25. package/dist/cost-routing/token-estimator.d.ts.map +1 -0
  26. package/dist/cost-routing/token-estimator.js +88 -0
  27. package/dist/cost-routing/token-estimator.js.map +1 -0
  28. package/dist/index.d.ts +4 -0
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +2 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/security/classifiers/index.d.ts +3 -0
  33. package/dist/security/classifiers/index.d.ts.map +1 -0
  34. package/dist/security/classifiers/index.js +3 -0
  35. package/dist/security/classifiers/index.js.map +1 -0
  36. package/dist/security/classifiers/llm-classifier.d.ts +10 -0
  37. package/dist/security/classifiers/llm-classifier.d.ts.map +1 -0
  38. package/dist/security/classifiers/llm-classifier.js +110 -0
  39. package/dist/security/classifiers/llm-classifier.js.map +1 -0
  40. package/dist/security/classifiers/local-classifier.d.ts +8 -0
  41. package/dist/security/classifiers/local-classifier.d.ts.map +1 -0
  42. package/dist/security/classifiers/local-classifier.js +130 -0
  43. package/dist/security/classifiers/local-classifier.js.map +1 -0
  44. package/dist/security/index.d.ts +5 -0
  45. package/dist/security/index.d.ts.map +1 -0
  46. package/dist/security/index.js +4 -0
  47. package/dist/security/index.js.map +1 -0
  48. package/dist/security/patterns.d.ts +6 -0
  49. package/dist/security/patterns.d.ts.map +1 -0
  50. package/dist/security/patterns.js +338 -0
  51. package/dist/security/patterns.js.map +1 -0
  52. package/dist/security/prompt-injection-detector.d.ts +28 -0
  53. package/dist/security/prompt-injection-detector.d.ts.map +1 -0
  54. package/dist/security/prompt-injection-detector.js +134 -0
  55. package/dist/security/prompt-injection-detector.js.map +1 -0
  56. package/dist/tools/hash.d.ts +1 -1
  57. package/dist/tools/index.d.ts +3 -3
  58. package/dist/tools/random.d.ts +1 -1
  59. package/dist/tools/vector-search.d.ts +1 -1
  60. package/package.json +5 -5
@@ -0,0 +1,134 @@
1
+ import { LocalInjectionClassifier } from './classifiers/local-classifier';
2
+ import { LLMInjectionClassifier } from './classifiers/llm-classifier';
3
+ export class PromptInjectionDetector {
4
+ classifier;
5
+ config;
6
+ customPatterns = [];
7
+ allowlistSet = new Set();
8
+ stats = { analyzed: 0, blocked: 0, warned: 0 };
9
+ constructor(options = {}) {
10
+ this.config = {
11
+ detectInjection: true,
12
+ detectJailbreak: true,
13
+ detectRoleplay: true,
14
+ detectEncoding: true,
15
+ detectContextManipulation: true,
16
+ classifier: 'local',
17
+ action: 'block',
18
+ threshold: 0.7,
19
+ ...options,
20
+ };
21
+ if (options.patterns) {
22
+ this.customPatterns = [...options.patterns];
23
+ }
24
+ if (options.allowlist) {
25
+ for (const phrase of options.allowlist) {
26
+ this.allowlistSet.add(phrase.toLowerCase());
27
+ }
28
+ }
29
+ if (this.config.classifier === 'llm' && this.config.llmBackend) {
30
+ this.classifier = new LLMInjectionClassifier(this.config.llmBackend);
31
+ }
32
+ else {
33
+ this.classifier = new LocalInjectionClassifier();
34
+ }
35
+ }
36
+ async analyze(input) {
37
+ const start = Date.now();
38
+ this.stats.analyzed++;
39
+ if (this.isAllowlisted(input)) {
40
+ return {
41
+ safe: true,
42
+ threats: [],
43
+ action: 'allowed',
44
+ analysisTime: Date.now() - start,
45
+ };
46
+ }
47
+ const configWithPatterns = {
48
+ ...this.config,
49
+ patterns: [...(this.config.patterns ?? []), ...this.customPatterns],
50
+ };
51
+ const threats = await this.classifier.analyze(input, configWithPatterns);
52
+ const safe = threats.length === 0;
53
+ let action = 'allowed';
54
+ if (!safe) {
55
+ switch (this.config.action) {
56
+ case 'block':
57
+ action = 'blocked';
58
+ this.stats.blocked++;
59
+ break;
60
+ case 'warn':
61
+ action = 'warned';
62
+ this.stats.warned++;
63
+ break;
64
+ case 'log':
65
+ action = 'allowed';
66
+ break;
67
+ }
68
+ }
69
+ const result = {
70
+ safe,
71
+ threats,
72
+ action,
73
+ analysisTime: Date.now() - start,
74
+ };
75
+ if (!safe && this.config.onThreat) {
76
+ this.config.onThreat(result, input);
77
+ }
78
+ return result;
79
+ }
80
+ isAllowlisted(input) {
81
+ const lowered = input.toLowerCase();
82
+ for (const phrase of this.allowlistSet) {
83
+ if (lowered.includes(phrase)) {
84
+ return true;
85
+ }
86
+ }
87
+ return false;
88
+ }
89
+ addPattern(pattern) {
90
+ this.customPatterns.push(pattern);
91
+ }
92
+ removePattern(pattern) {
93
+ const source = pattern.source;
94
+ const idx = this.customPatterns.findIndex((p) => p.source === source);
95
+ if (idx !== -1) {
96
+ this.customPatterns.splice(idx, 1);
97
+ return true;
98
+ }
99
+ return false;
100
+ }
101
+ addToAllowlist(phrase) {
102
+ this.allowlistSet.add(phrase.toLowerCase());
103
+ }
104
+ removeFromAllowlist(phrase) {
105
+ return this.allowlistSet.delete(phrase.toLowerCase());
106
+ }
107
+ clearAllowlist() {
108
+ this.allowlistSet.clear();
109
+ }
110
+ getConfig() {
111
+ return { ...this.config };
112
+ }
113
+ updateConfig(updates) {
114
+ this.config = { ...this.config, ...updates };
115
+ if (updates.classifier !== undefined || updates.llmBackend !== undefined) {
116
+ if (this.config.classifier === 'llm' && this.config.llmBackend) {
117
+ this.classifier = new LLMInjectionClassifier(this.config.llmBackend);
118
+ }
119
+ else {
120
+ this.classifier = new LocalInjectionClassifier();
121
+ }
122
+ }
123
+ }
124
+ getStats() {
125
+ const allowRate = this.stats.analyzed > 0
126
+ ? (this.stats.analyzed - this.stats.blocked - this.stats.warned) / this.stats.analyzed
127
+ : 1;
128
+ return { ...this.stats, allowRate };
129
+ }
130
+ resetStats() {
131
+ this.stats = { analyzed: 0, blocked: 0, warned: 0 };
132
+ }
133
+ }
134
+ //# sourceMappingURL=prompt-injection-detector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt-injection-detector.js","sourceRoot":"","sources":["../../src/security/prompt-injection-detector.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AAItE,MAAM,OAAO,uBAAuB;IAC1B,UAAU,CAAsB;IAChC,MAAM,CAAwB;IAC9B,cAAc,GAAa,EAAE,CAAC;IAC9B,YAAY,GAAgB,IAAI,GAAG,EAAE,CAAC;IACtC,KAAK,GAAG,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;IAEvD,YAAY,UAA0C,EAAE;QACtD,IAAI,CAAC,MAAM,GAAG;YACZ,eAAe,EAAE,IAAI;YACrB,eAAe,EAAE,IAAI;YACrB,cAAc,EAAE,IAAI;YACpB,cAAc,EAAE,IAAI;YACpB,yBAAyB,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AAU,GAAG,IAAI,sBAAsB,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACvE,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,UAAU,GAAG,IAAI,wBAAwB,EAAE,CAAC;YACnD,CAAC;QACH,CAAC;IACH,CAAC;IAED,QAAQ;QACN,MAAM,SAAS,GACb,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC;YACrB,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ;YACtF,CAAC,CAAC,CAAC,CAAC;QAER,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,CAAC;IACtC,CAAC;IAED,UAAU;QACR,IAAI,CAAC,KAAK,GAAG,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;IACtD,CAAC;CACF"}
@@ -3,8 +3,8 @@
3
3
  */
4
4
  export declare const hash: import("@cogitator-ai/types").Tool<{
5
5
  data: string;
6
- algorithm?: "sha256" | "md5" | "sha1" | "sha512" | undefined;
7
6
  encoding?: "base64" | "hex" | undefined;
7
+ algorithm?: "sha256" | "md5" | "sha1" | "sha512" | undefined;
8
8
  }, {
9
9
  hash: string;
10
10
  algorithm: "sha256" | "md5" | "sha1" | "sha512";
@@ -55,9 +55,9 @@ export declare const builtinTools: readonly [import("@cogitator-ai/types").Tool<
55
55
  count: number;
56
56
  uuid?: undefined;
57
57
  }>, import("@cogitator-ai/types").Tool<{
58
- integer?: boolean | undefined;
59
58
  min?: number | undefined;
60
59
  max?: number | undefined;
60
+ integer?: boolean | undefined;
61
61
  }, {
62
62
  error: string;
63
63
  min: number;
@@ -79,8 +79,8 @@ export declare const builtinTools: readonly [import("@cogitator-ai/types").Tool<
79
79
  charset: "numeric" | "alphanumeric" | "alpha" | "hex";
80
80
  }>, import("@cogitator-ai/types").Tool<{
81
81
  data: string;
82
- algorithm?: "sha256" | "md5" | "sha1" | "sha512" | undefined;
83
82
  encoding?: "base64" | "hex" | undefined;
83
+ algorithm?: "sha256" | "md5" | "sha1" | "sha512" | undefined;
84
84
  }, {
85
85
  hash: string;
86
86
  algorithm: "sha256" | "md5" | "sha1" | "sha512";
@@ -363,10 +363,10 @@ export declare const builtinTools: readonly [import("@cogitator-ai/types").Tool<
363
363
  }>, import("@cogitator-ai/types").Tool<{
364
364
  query: string;
365
365
  filter?: Record<string, unknown> | undefined;
366
+ threshold?: number | undefined;
366
367
  connectionString?: string | undefined;
367
368
  collection?: string | undefined;
368
369
  topK?: number | undefined;
369
- threshold?: number | undefined;
370
370
  embeddingProvider?: "ollama" | "openai" | "google" | undefined;
371
371
  embeddingModel?: string | undefined;
372
372
  }, import("./vector-search").VectorSearchResponse | {
@@ -2,9 +2,9 @@
2
2
  * Random tools - generate random numbers and strings
3
3
  */
4
4
  export declare const randomNumber: import("@cogitator-ai/types").Tool<{
5
- integer?: boolean | undefined;
6
5
  min?: number | undefined;
7
6
  max?: number | undefined;
7
+ integer?: boolean | undefined;
8
8
  }, {
9
9
  error: string;
10
10
  min: number;
@@ -13,10 +13,10 @@ export interface VectorSearchResponse {
13
13
  export declare const vectorSearch: import("@cogitator-ai/types").Tool<{
14
14
  query: string;
15
15
  filter?: Record<string, unknown> | undefined;
16
+ threshold?: number | undefined;
16
17
  connectionString?: string | undefined;
17
18
  collection?: string | undefined;
18
19
  topK?: number | undefined;
19
- threshold?: number | undefined;
20
20
  embeddingProvider?: "ollama" | "openai" | "google" | undefined;
21
21
  embeddingModel?: string | undefined;
22
22
  }, VectorSearchResponse | {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogitator-ai/core",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Core runtime for Cogitator AI agents",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,9 +22,9 @@
22
22
  "openai": "^4.24.0",
23
23
  "zod": "^3.22.4",
24
24
  "zod-to-json-schema": "^3.22.4",
25
- "@cogitator-ai/memory": "0.6.3",
26
- "@cogitator-ai/models": "11.0.0",
27
- "@cogitator-ai/types": "0.13.0"
25
+ "@cogitator-ai/memory": "0.6.5",
26
+ "@cogitator-ai/types": "0.15.0",
27
+ "@cogitator-ai/models": "13.0.0"
28
28
  },
29
29
  "optionalDependencies": {
30
30
  "pg": "^8.11.3"
@@ -37,7 +37,7 @@
37
37
  "peerDependencies": {
38
38
  "@aws-sdk/client-bedrock-runtime": "^3.0.0",
39
39
  "pg": "^8.0.0",
40
- "@cogitator-ai/sandbox": "0.2.13"
40
+ "@cogitator-ai/sandbox": "0.2.15"
41
41
  },
42
42
  "peerDependenciesMeta": {
43
43
  "@aws-sdk/client-bedrock-runtime": {