anthale 0.2.1 → 0.4.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/CHANGELOG.md +26 -0
- package/integrations/core.d.mts +138 -0
- package/integrations/core.d.mts.map +1 -0
- package/integrations/core.d.ts +138 -0
- package/integrations/core.d.ts.map +1 -0
- package/integrations/core.js +210 -0
- package/integrations/core.js.map +1 -0
- package/integrations/core.mjs +199 -0
- package/integrations/core.mjs.map +1 -0
- package/integrations/openai.d.mts +63 -0
- package/integrations/openai.d.mts.map +1 -0
- package/integrations/openai.d.ts +63 -0
- package/integrations/openai.d.ts.map +1 -0
- package/integrations/openai.js +296 -0
- package/integrations/openai.js.map +1 -0
- package/integrations/openai.mjs +292 -0
- package/integrations/openai.mjs.map +1 -0
- package/package.json +19 -1
- package/resources/organizations/policies.d.mts +3 -0
- package/resources/organizations/policies.d.mts.map +1 -1
- package/resources/organizations/policies.d.ts +3 -0
- package/resources/organizations/policies.d.ts.map +1 -1
- package/resources/organizations/policies.js +3 -0
- package/resources/organizations/policies.js.map +1 -1
- package/resources/organizations/policies.mjs +3 -0
- package/resources/organizations/policies.mjs.map +1 -1
- package/src/integrations/core.ts +301 -0
- package/src/integrations/openai.ts +428 -0
- package/src/resources/organizations/policies.ts +3 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.0 (2026-03-05)
|
|
4
|
+
|
|
5
|
+
Full Changelog: [v0.3.0...v0.4.0](https://github.com/anthalehq/anthale-node/compare/v0.3.0...v0.4.0)
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
* implement openai integration ([d2ec5d3](https://github.com/anthalehq/anthale-node/commit/d2ec5d348172878ebf240f46b85a0066211f86c0))
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Chores
|
|
13
|
+
|
|
14
|
+
* **internal:** codegen related update ([54dd025](https://github.com/anthalehq/anthale-node/commit/54dd0257688c1a0a6bf9102cc58e4724445f2b54))
|
|
15
|
+
|
|
16
|
+
## 0.3.0 (2026-03-05)
|
|
17
|
+
|
|
18
|
+
Full Changelog: [v0.2.1...v0.3.0](https://github.com/anthalehq/anthale-node/compare/v0.2.1...v0.3.0)
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* implement openai integration ([940380e](https://github.com/anthalehq/anthale-node/commit/940380efaa708fe71c992be0252b5033098e6a1e))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Chores
|
|
26
|
+
|
|
27
|
+
* **internal:** codegen related update ([09190dc](https://github.com/anthalehq/anthale-node/commit/09190dcdcf96eb0099007d75e996685de5b6dbc5))
|
|
28
|
+
|
|
3
29
|
## 0.2.1 (2026-03-02)
|
|
4
30
|
|
|
5
31
|
Full Changelog: [v0.2.0...v0.2.1](https://github.com/anthalehq/anthale-node/compare/v0.2.0...v0.2.1)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { AnthaleError } from "../core/error.mjs";
|
|
2
|
+
import type { PolicyEnforceParams, PolicyEnforceResponse } from "../resources/organizations/policies.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Minimal Anthale client contract required by {@link PolicyEnforcer}.
|
|
5
|
+
*
|
|
6
|
+
* Pass your own client when you need shared configuration (timeouts, retries,
|
|
7
|
+
* telemetry), or let the enforcer construct one with `apiKey`.
|
|
8
|
+
*/
|
|
9
|
+
export interface PolicyEnforcerClient {
|
|
10
|
+
organizations: {
|
|
11
|
+
policies: {
|
|
12
|
+
enforce: (policyIdentifier: string, body: PolicyEnforceParams) => PromiseLike<PolicyEnforceResponse> | PolicyEnforceResponse;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Options used to construct a {@link PolicyEnforcer}.
|
|
18
|
+
*/
|
|
19
|
+
export interface PolicyEnforcerOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Anthale policy identifier to enforce.
|
|
22
|
+
*/
|
|
23
|
+
policyId: string;
|
|
24
|
+
/**
|
|
25
|
+
* Anthale API key used only when `client` is not provided.
|
|
26
|
+
*/
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Optional pre-built Anthale client.
|
|
30
|
+
*/
|
|
31
|
+
client?: PolicyEnforcerClient;
|
|
32
|
+
/**
|
|
33
|
+
* Metadata merged into every enforcement call.
|
|
34
|
+
*/
|
|
35
|
+
metadata?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parameters for a single policy enforcement call.
|
|
39
|
+
*/
|
|
40
|
+
export interface EnforceOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Enforcement direction (`input` or `output`).
|
|
43
|
+
*/
|
|
44
|
+
direction: PolicyEnforceParams['direction'];
|
|
45
|
+
/**
|
|
46
|
+
* Conversation messages to evaluate.
|
|
47
|
+
*/
|
|
48
|
+
messages: PolicyEnforceParams['messages'];
|
|
49
|
+
/**
|
|
50
|
+
* Optional metadata merged over constructor metadata for this call.
|
|
51
|
+
*/
|
|
52
|
+
metadata?: Record<string, unknown>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Error raised when Anthale returns a blocking action.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { AnthalePolicyViolationError } from 'anthale/integrations/core';
|
|
60
|
+
*
|
|
61
|
+
* try {
|
|
62
|
+
* // ...
|
|
63
|
+
* } catch (error) {
|
|
64
|
+
* if (error instanceof AnthalePolicyViolationError) {
|
|
65
|
+
* console.error(error.enforcementIdentifier);
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare class AnthalePolicyViolationError extends AnthaleError {
|
|
71
|
+
readonly enforcementIdentifier: string;
|
|
72
|
+
constructor(enforcementIdentifier: string);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Lightweight policy enforcer used by provider integrations.
|
|
76
|
+
*
|
|
77
|
+
* It always calls Anthale with `includeEvaluations: false` and throws
|
|
78
|
+
* {@link AnthalePolicyViolationError} when the resulting action is `block`.
|
|
79
|
+
*/
|
|
80
|
+
export declare class PolicyEnforcer {
|
|
81
|
+
private readonly client;
|
|
82
|
+
private readonly policyId;
|
|
83
|
+
private readonly metadata;
|
|
84
|
+
/**
|
|
85
|
+
* Create a policy enforcer.
|
|
86
|
+
*
|
|
87
|
+
* @param options - Enforcer options.
|
|
88
|
+
*/
|
|
89
|
+
constructor(options: PolicyEnforcerOptions);
|
|
90
|
+
/**
|
|
91
|
+
* Enforce the configured Anthale policy on a message set.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Enforcement options.
|
|
94
|
+
* @returns The raw Anthale enforcement response.
|
|
95
|
+
* @throws {AnthalePolicyViolationError} When Anthale action is `block`.
|
|
96
|
+
*/
|
|
97
|
+
enforce(options: EnforceOptions): Promise<PolicyEnforceResponse>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convenience factory for {@link PolicyEnforcer}.
|
|
101
|
+
*/
|
|
102
|
+
export declare function buildPolicyEnforcer(options: PolicyEnforcerOptions): PolicyEnforcer;
|
|
103
|
+
type MessageRole = PolicyEnforceParams.Message['role'];
|
|
104
|
+
/**
|
|
105
|
+
* Normalize a role-like value to one of Anthale's supported roles.
|
|
106
|
+
*
|
|
107
|
+
* Unknown or non-string values fall back to `user`.
|
|
108
|
+
*/
|
|
109
|
+
export declare function normalizeRole(value: unknown): MessageRole;
|
|
110
|
+
/**
|
|
111
|
+
* Safely stringify arbitrary values for message content.
|
|
112
|
+
*/
|
|
113
|
+
export declare function stringify(value: unknown): string;
|
|
114
|
+
/**
|
|
115
|
+
* Normalize different message content shapes to a plain string.
|
|
116
|
+
*
|
|
117
|
+
* Handles raw strings, block arrays, and nested mappings commonly returned by
|
|
118
|
+
* OpenAI and LangChain payloads.
|
|
119
|
+
*/
|
|
120
|
+
export declare function extractContent(raw: unknown): string;
|
|
121
|
+
/**
|
|
122
|
+
* Convert a role/content pair into an Anthale message.
|
|
123
|
+
*
|
|
124
|
+
* Empty/blank content returns `null`.
|
|
125
|
+
*/
|
|
126
|
+
export declare function toAnthaleMessage(value: {
|
|
127
|
+
role: unknown;
|
|
128
|
+
content: unknown;
|
|
129
|
+
}): PolicyEnforceParams.Message | null;
|
|
130
|
+
/**
|
|
131
|
+
* Recursively convert unknown payload values into Anthale messages.
|
|
132
|
+
*
|
|
133
|
+
* This function is intentionally permissive so integrations can normalize
|
|
134
|
+
* heterogeneous provider payloads with a single utility.
|
|
135
|
+
*/
|
|
136
|
+
export declare function messagesFromValue(value: unknown, defaultRole: MessageRole): PolicyEnforceParams.Message[];
|
|
137
|
+
export {};
|
|
138
|
+
//# sourceMappingURL=core.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.mts","sourceRoot":"","sources":["../src/integrations/core.ts"],"names":[],"mappings":"OACO,EAAE,YAAY,EAAE;OAChB,KAAK,EAAE,mBAAmB,EAAE,qBAAqB,EAAE;AAE1D;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,aAAa,EAAE;QACb,QAAQ,EAAE;YACR,OAAO,EAAE,CACP,gBAAgB,EAAE,MAAM,EACxB,IAAI,EAAE,mBAAmB,KACtB,WAAW,CAAC,qBAAqB,CAAC,GAAG,qBAAqB,CAAC;SACjE,CAAC;KACH,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAC9B;;OAE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O,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,mBAAmB,CAAC,OAAO,GAAG,IAAI,CAUrC;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,GAAG,mBAAmB,CAAC,OAAO,EAAE,CAmCzG"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { AnthaleError } from "../core/error.js";
|
|
2
|
+
import type { PolicyEnforceParams, PolicyEnforceResponse } from "../resources/organizations/policies.js";
|
|
3
|
+
/**
|
|
4
|
+
* Minimal Anthale client contract required by {@link PolicyEnforcer}.
|
|
5
|
+
*
|
|
6
|
+
* Pass your own client when you need shared configuration (timeouts, retries,
|
|
7
|
+
* telemetry), or let the enforcer construct one with `apiKey`.
|
|
8
|
+
*/
|
|
9
|
+
export interface PolicyEnforcerClient {
|
|
10
|
+
organizations: {
|
|
11
|
+
policies: {
|
|
12
|
+
enforce: (policyIdentifier: string, body: PolicyEnforceParams) => PromiseLike<PolicyEnforceResponse> | PolicyEnforceResponse;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Options used to construct a {@link PolicyEnforcer}.
|
|
18
|
+
*/
|
|
19
|
+
export interface PolicyEnforcerOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Anthale policy identifier to enforce.
|
|
22
|
+
*/
|
|
23
|
+
policyId: string;
|
|
24
|
+
/**
|
|
25
|
+
* Anthale API key used only when `client` is not provided.
|
|
26
|
+
*/
|
|
27
|
+
apiKey?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Optional pre-built Anthale client.
|
|
30
|
+
*/
|
|
31
|
+
client?: PolicyEnforcerClient;
|
|
32
|
+
/**
|
|
33
|
+
* Metadata merged into every enforcement call.
|
|
34
|
+
*/
|
|
35
|
+
metadata?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parameters for a single policy enforcement call.
|
|
39
|
+
*/
|
|
40
|
+
export interface EnforceOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Enforcement direction (`input` or `output`).
|
|
43
|
+
*/
|
|
44
|
+
direction: PolicyEnforceParams['direction'];
|
|
45
|
+
/**
|
|
46
|
+
* Conversation messages to evaluate.
|
|
47
|
+
*/
|
|
48
|
+
messages: PolicyEnforceParams['messages'];
|
|
49
|
+
/**
|
|
50
|
+
* Optional metadata merged over constructor metadata for this call.
|
|
51
|
+
*/
|
|
52
|
+
metadata?: Record<string, unknown>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Error raised when Anthale returns a blocking action.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { AnthalePolicyViolationError } from 'anthale/integrations/core';
|
|
60
|
+
*
|
|
61
|
+
* try {
|
|
62
|
+
* // ...
|
|
63
|
+
* } catch (error) {
|
|
64
|
+
* if (error instanceof AnthalePolicyViolationError) {
|
|
65
|
+
* console.error(error.enforcementIdentifier);
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare class AnthalePolicyViolationError extends AnthaleError {
|
|
71
|
+
readonly enforcementIdentifier: string;
|
|
72
|
+
constructor(enforcementIdentifier: string);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Lightweight policy enforcer used by provider integrations.
|
|
76
|
+
*
|
|
77
|
+
* It always calls Anthale with `includeEvaluations: false` and throws
|
|
78
|
+
* {@link AnthalePolicyViolationError} when the resulting action is `block`.
|
|
79
|
+
*/
|
|
80
|
+
export declare class PolicyEnforcer {
|
|
81
|
+
private readonly client;
|
|
82
|
+
private readonly policyId;
|
|
83
|
+
private readonly metadata;
|
|
84
|
+
/**
|
|
85
|
+
* Create a policy enforcer.
|
|
86
|
+
*
|
|
87
|
+
* @param options - Enforcer options.
|
|
88
|
+
*/
|
|
89
|
+
constructor(options: PolicyEnforcerOptions);
|
|
90
|
+
/**
|
|
91
|
+
* Enforce the configured Anthale policy on a message set.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Enforcement options.
|
|
94
|
+
* @returns The raw Anthale enforcement response.
|
|
95
|
+
* @throws {AnthalePolicyViolationError} When Anthale action is `block`.
|
|
96
|
+
*/
|
|
97
|
+
enforce(options: EnforceOptions): Promise<PolicyEnforceResponse>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convenience factory for {@link PolicyEnforcer}.
|
|
101
|
+
*/
|
|
102
|
+
export declare function buildPolicyEnforcer(options: PolicyEnforcerOptions): PolicyEnforcer;
|
|
103
|
+
type MessageRole = PolicyEnforceParams.Message['role'];
|
|
104
|
+
/**
|
|
105
|
+
* Normalize a role-like value to one of Anthale's supported roles.
|
|
106
|
+
*
|
|
107
|
+
* Unknown or non-string values fall back to `user`.
|
|
108
|
+
*/
|
|
109
|
+
export declare function normalizeRole(value: unknown): MessageRole;
|
|
110
|
+
/**
|
|
111
|
+
* Safely stringify arbitrary values for message content.
|
|
112
|
+
*/
|
|
113
|
+
export declare function stringify(value: unknown): string;
|
|
114
|
+
/**
|
|
115
|
+
* Normalize different message content shapes to a plain string.
|
|
116
|
+
*
|
|
117
|
+
* Handles raw strings, block arrays, and nested mappings commonly returned by
|
|
118
|
+
* OpenAI and LangChain payloads.
|
|
119
|
+
*/
|
|
120
|
+
export declare function extractContent(raw: unknown): string;
|
|
121
|
+
/**
|
|
122
|
+
* Convert a role/content pair into an Anthale message.
|
|
123
|
+
*
|
|
124
|
+
* Empty/blank content returns `null`.
|
|
125
|
+
*/
|
|
126
|
+
export declare function toAnthaleMessage(value: {
|
|
127
|
+
role: unknown;
|
|
128
|
+
content: unknown;
|
|
129
|
+
}): PolicyEnforceParams.Message | null;
|
|
130
|
+
/**
|
|
131
|
+
* Recursively convert unknown payload values into Anthale messages.
|
|
132
|
+
*
|
|
133
|
+
* This function is intentionally permissive so integrations can normalize
|
|
134
|
+
* heterogeneous provider payloads with a single utility.
|
|
135
|
+
*/
|
|
136
|
+
export declare function messagesFromValue(value: unknown, defaultRole: MessageRole): PolicyEnforceParams.Message[];
|
|
137
|
+
export {};
|
|
138
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/integrations/core.ts"],"names":[],"mappings":"OACO,EAAE,YAAY,EAAE;OAChB,KAAK,EAAE,mBAAmB,EAAE,qBAAqB,EAAE;AAE1D;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,aAAa,EAAE;QACb,QAAQ,EAAE;YACR,OAAO,EAAE,CACP,gBAAgB,EAAE,MAAM,EACxB,IAAI,EAAE,mBAAmB,KACtB,WAAW,CAAC,qBAAqB,CAAC,GAAG,qBAAqB,CAAC;SACjE,CAAC;KACH,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAC9B;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,SAAS,EAAE,mBAAmB,CAAC,WAAW,CAAC,CAAC;IAC5C;;OAEG;IACH,QAAQ,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;IAC1C;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,2BAA4B,SAAQ,YAAY;IAC3D,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAC;gBAE3B,qBAAqB,EAAE,MAAM;CAI1C;AAED;;;;;GAKG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA0B;IAEnD;;;;OAIG;gBACS,OAAO,EAAE,qBAAqB;IAM1C;;;;;;OAMG;IACG,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAcvE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAElF;AAED,KAAK,WAAW,GAAG,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AAcvD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAMzD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAkBhD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CA2CnD;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE;IACtC,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,mBAAmB,CAAC,OAAO,GAAG,IAAI,CAUrC;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,WAAW,GAAG,mBAAmB,CAAC,OAAO,EAAE,CAmCzG"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PolicyEnforcer = exports.AnthalePolicyViolationError = void 0;
|
|
4
|
+
exports.buildPolicyEnforcer = buildPolicyEnforcer;
|
|
5
|
+
exports.normalizeRole = normalizeRole;
|
|
6
|
+
exports.stringify = stringify;
|
|
7
|
+
exports.extractContent = extractContent;
|
|
8
|
+
exports.toAnthaleMessage = toAnthaleMessage;
|
|
9
|
+
exports.messagesFromValue = messagesFromValue;
|
|
10
|
+
const client_1 = require("../client.js");
|
|
11
|
+
const error_1 = require("../core/error.js");
|
|
12
|
+
/**
|
|
13
|
+
* Error raised when Anthale returns a blocking action.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { AnthalePolicyViolationError } from 'anthale/integrations/core';
|
|
18
|
+
*
|
|
19
|
+
* try {
|
|
20
|
+
* // ...
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* if (error instanceof AnthalePolicyViolationError) {
|
|
23
|
+
* console.error(error.enforcementIdentifier);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
class AnthalePolicyViolationError extends error_1.AnthaleError {
|
|
29
|
+
constructor(enforcementIdentifier) {
|
|
30
|
+
super(`Policy enforcement '${enforcementIdentifier}' was blocked due to a policy violation.`);
|
|
31
|
+
this.enforcementIdentifier = enforcementIdentifier;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.AnthalePolicyViolationError = AnthalePolicyViolationError;
|
|
35
|
+
/**
|
|
36
|
+
* Lightweight policy enforcer used by provider integrations.
|
|
37
|
+
*
|
|
38
|
+
* It always calls Anthale with `includeEvaluations: false` and throws
|
|
39
|
+
* {@link AnthalePolicyViolationError} when the resulting action is `block`.
|
|
40
|
+
*/
|
|
41
|
+
class PolicyEnforcer {
|
|
42
|
+
/**
|
|
43
|
+
* Create a policy enforcer.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Enforcer options.
|
|
46
|
+
*/
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.client = options.client ?? new client_1.Anthale({ apiKey: options.apiKey });
|
|
49
|
+
this.policyId = options.policyId;
|
|
50
|
+
this.metadata = options.metadata ?? {};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Enforce the configured Anthale policy on a message set.
|
|
54
|
+
*
|
|
55
|
+
* @param options - Enforcement options.
|
|
56
|
+
* @returns The raw Anthale enforcement response.
|
|
57
|
+
* @throws {AnthalePolicyViolationError} When Anthale action is `block`.
|
|
58
|
+
*/
|
|
59
|
+
async enforce(options) {
|
|
60
|
+
const response = await this.client.organizations.policies.enforce(this.policyId, {
|
|
61
|
+
direction: options.direction,
|
|
62
|
+
messages: options.messages,
|
|
63
|
+
includeEvaluations: false,
|
|
64
|
+
metadata: { ...this.metadata, ...(options.metadata ?? {}) },
|
|
65
|
+
});
|
|
66
|
+
if (response.action === 'block') {
|
|
67
|
+
throw new AnthalePolicyViolationError(response.enforcerIdentifier);
|
|
68
|
+
}
|
|
69
|
+
return response;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.PolicyEnforcer = PolicyEnforcer;
|
|
73
|
+
/**
|
|
74
|
+
* Convenience factory for {@link PolicyEnforcer}.
|
|
75
|
+
*/
|
|
76
|
+
function buildPolicyEnforcer(options) {
|
|
77
|
+
return new PolicyEnforcer(options);
|
|
78
|
+
}
|
|
79
|
+
const ROLE_MAP = {
|
|
80
|
+
system: 'system',
|
|
81
|
+
developer: 'system',
|
|
82
|
+
machine: 'system',
|
|
83
|
+
user: 'user',
|
|
84
|
+
human: 'user',
|
|
85
|
+
assistant: 'assistant',
|
|
86
|
+
ai: 'assistant',
|
|
87
|
+
tool: 'tool',
|
|
88
|
+
function: 'tool',
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Normalize a role-like value to one of Anthale's supported roles.
|
|
92
|
+
*
|
|
93
|
+
* Unknown or non-string values fall back to `user`.
|
|
94
|
+
*/
|
|
95
|
+
function normalizeRole(value) {
|
|
96
|
+
if (typeof value !== 'string') {
|
|
97
|
+
return 'user';
|
|
98
|
+
}
|
|
99
|
+
return ROLE_MAP[value.toLowerCase()] ?? 'user';
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Safely stringify arbitrary values for message content.
|
|
103
|
+
*/
|
|
104
|
+
function stringify(value) {
|
|
105
|
+
if (typeof value === 'string') {
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
if (value == null) {
|
|
109
|
+
return String(value);
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === 'object') {
|
|
112
|
+
try {
|
|
113
|
+
return JSON.stringify(value);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return String(value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return String(value);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Normalize different message content shapes to a plain string.
|
|
123
|
+
*
|
|
124
|
+
* Handles raw strings, block arrays, and nested mappings commonly returned by
|
|
125
|
+
* OpenAI and LangChain payloads.
|
|
126
|
+
*/
|
|
127
|
+
function extractContent(raw) {
|
|
128
|
+
if (raw == null) {
|
|
129
|
+
return '';
|
|
130
|
+
}
|
|
131
|
+
if (typeof raw === 'string') {
|
|
132
|
+
return raw;
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(raw)) {
|
|
135
|
+
const parts = raw
|
|
136
|
+
.map((block) => {
|
|
137
|
+
if (typeof block === 'string') {
|
|
138
|
+
return block;
|
|
139
|
+
}
|
|
140
|
+
if (block && typeof block === 'object') {
|
|
141
|
+
const record = block;
|
|
142
|
+
return extractContent(record['text'] ??
|
|
143
|
+
record['output_text'] ??
|
|
144
|
+
record['input_text'] ??
|
|
145
|
+
record['content'] ??
|
|
146
|
+
record['arguments'] ??
|
|
147
|
+
block);
|
|
148
|
+
}
|
|
149
|
+
return stringify(block);
|
|
150
|
+
})
|
|
151
|
+
.filter((part) => part.length > 0);
|
|
152
|
+
return parts.join('\n');
|
|
153
|
+
}
|
|
154
|
+
if (typeof raw === 'object') {
|
|
155
|
+
const record = raw;
|
|
156
|
+
return extractContent(record['text'] ?? record['output_text'] ?? record['input_text'] ?? record['content'] ?? stringify(raw));
|
|
157
|
+
}
|
|
158
|
+
return stringify(raw);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Convert a role/content pair into an Anthale message.
|
|
162
|
+
*
|
|
163
|
+
* Empty/blank content returns `null`.
|
|
164
|
+
*/
|
|
165
|
+
function toAnthaleMessage(value) {
|
|
166
|
+
const content = extractContent(value.content);
|
|
167
|
+
if (!content || content.trim() === '' || content.trim() === 'None') {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
role: normalizeRole(value.role),
|
|
172
|
+
content,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Recursively convert unknown payload values into Anthale messages.
|
|
177
|
+
*
|
|
178
|
+
* This function is intentionally permissive so integrations can normalize
|
|
179
|
+
* heterogeneous provider payloads with a single utility.
|
|
180
|
+
*/
|
|
181
|
+
function messagesFromValue(value, defaultRole) {
|
|
182
|
+
if (value == null) {
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
if (Array.isArray(value)) {
|
|
186
|
+
const out = [];
|
|
187
|
+
for (const item of value) {
|
|
188
|
+
out.push(...messagesFromValue(item, defaultRole));
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
if (typeof value === 'object') {
|
|
193
|
+
const record = value;
|
|
194
|
+
const message = toAnthaleMessage({
|
|
195
|
+
role: record['role'] ?? defaultRole,
|
|
196
|
+
content: record['content'] ??
|
|
197
|
+
record['text'] ??
|
|
198
|
+
record['input_text'] ??
|
|
199
|
+
record['output_text'] ??
|
|
200
|
+
record['arguments'],
|
|
201
|
+
});
|
|
202
|
+
return message == null ? [] : [message];
|
|
203
|
+
}
|
|
204
|
+
const content = stringify(value);
|
|
205
|
+
if (!content || content.trim() === '' || content.trim() === 'None') {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
return [{ role: normalizeRole(defaultRole), content }];
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=core.js.map
|
|
@@ -0,0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