@valence-ai/sdk 0.1.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/README.md +194 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +41 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/wrap-tool.d.ts +12 -0
- package/dist/wrap-tool.d.ts.map +1 -0
- package/dist/wrap-tool.js +54 -0
- package/dist/wrap-tool.js.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Valence SDK
|
|
2
|
+
|
|
3
|
+
The Valence SDK adds pre-execution policy enforcement to tool-using applications.
|
|
4
|
+
|
|
5
|
+
It sends tool execution requests to Valence before the tool runs and returns one of three outcomes:
|
|
6
|
+
|
|
7
|
+
- `ALLOW`
|
|
8
|
+
- `BLOCK`
|
|
9
|
+
- `REQUIRES_APPROVAL`
|
|
10
|
+
|
|
11
|
+
Use it at the tool execution boundary in your application or agent runtime.
|
|
12
|
+
|
|
13
|
+
## Package
|
|
14
|
+
|
|
15
|
+
- Package name: `@valence-ai/sdk`
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @valence-ai/sdk
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Environment variables
|
|
24
|
+
|
|
25
|
+
```env
|
|
26
|
+
VALENCE_API_BASE_URL=http://localhost:3000
|
|
27
|
+
VALENCE_API_KEY=valence_pk_...
|
|
28
|
+
VALENCE_WORKSPACE_ID=workspace-id
|
|
29
|
+
VALENCE_PROJECT_ID=project-id
|
|
30
|
+
VALENCE_AGENT_NAME=incidentos-agent
|
|
31
|
+
VALENCE_ENVIRONMENT=staging
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Create a client
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { createValenceClient } from '@valence-ai/sdk';
|
|
38
|
+
|
|
39
|
+
const client = createValenceClient({
|
|
40
|
+
baseUrl: process.env.VALENCE_API_BASE_URL!,
|
|
41
|
+
apiKey: process.env.VALENCE_API_KEY!,
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Guard a tool dispatcher
|
|
46
|
+
|
|
47
|
+
This is the recommended integration model for real applications.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createToolExecutionGuard } from '@valence-ai/sdk';
|
|
51
|
+
|
|
52
|
+
const guard = createToolExecutionGuard(client, {
|
|
53
|
+
workspaceId: process.env.VALENCE_WORKSPACE_ID!,
|
|
54
|
+
projectId: process.env.VALENCE_PROJECT_ID!,
|
|
55
|
+
agentName: process.env.VALENCE_AGENT_NAME!,
|
|
56
|
+
environment: process.env.VALENCE_ENVIRONMENT as 'production' | 'staging' | 'sandbox',
|
|
57
|
+
goal: 'resolve_incident',
|
|
58
|
+
sessionId: 'session-123',
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Run a guarded tool
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const result = await guard.runTool(
|
|
66
|
+
{
|
|
67
|
+
tool: 'restart_service',
|
|
68
|
+
action: 'write',
|
|
69
|
+
parameters: { service: 'payments-api' },
|
|
70
|
+
},
|
|
71
|
+
async () => {
|
|
72
|
+
return restartService('payments-api');
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Use `withValenceGuard` for a single tool
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { withValenceGuard } from '@valence-ai/sdk';
|
|
81
|
+
|
|
82
|
+
const guardedReadIncident = withValenceGuard(client, {
|
|
83
|
+
workspaceId: process.env.VALENCE_WORKSPACE_ID!,
|
|
84
|
+
projectId: process.env.VALENCE_PROJECT_ID!,
|
|
85
|
+
agentName: process.env.VALENCE_AGENT_NAME!,
|
|
86
|
+
tool: 'read_incident',
|
|
87
|
+
action: 'read',
|
|
88
|
+
environment: 'staging',
|
|
89
|
+
goal: 'resolve_incident',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const incident = await guardedReadIncident(async () => {
|
|
93
|
+
return readIncident('INC-1421');
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Request contract
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
type ToolEvent = {
|
|
101
|
+
agentId?: string;
|
|
102
|
+
agentName: string;
|
|
103
|
+
projectId?: string;
|
|
104
|
+
tool: string;
|
|
105
|
+
action: string;
|
|
106
|
+
environment: 'production' | 'staging' | 'sandbox';
|
|
107
|
+
goal: string;
|
|
108
|
+
sessionId?: string;
|
|
109
|
+
workspaceId?: string;
|
|
110
|
+
parameters?: Record<string, unknown>;
|
|
111
|
+
dryRun?: boolean;
|
|
112
|
+
timestamp?: string;
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Decision response
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
type DecisionResponse = {
|
|
120
|
+
decision: 'ALLOW' | 'BLOCK' | 'REQUIRES_APPROVAL';
|
|
121
|
+
reason: string;
|
|
122
|
+
risk: 'LOW' | 'MEDIUM' | 'HIGH';
|
|
123
|
+
policyId: string | null;
|
|
124
|
+
approvalId: string | null;
|
|
125
|
+
latencyMs: number;
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Runtime behavior
|
|
130
|
+
|
|
131
|
+
- normal requests go to `/api/decide`
|
|
132
|
+
- `dryRun: true` goes to `/api/simulate`
|
|
133
|
+
- `ALLOW` executes the wrapped tool
|
|
134
|
+
- `BLOCK` throws `ValenceBlockError` before execution
|
|
135
|
+
- `REQUIRES_APPROVAL` throws `ValenceApprovalRequiredError` before execution
|
|
136
|
+
|
|
137
|
+
Approval-required decisions are returned before execution so applications can route the request into an explicit review and retry workflow.
|
|
138
|
+
|
|
139
|
+
## Error handling
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import {
|
|
143
|
+
ValenceApprovalRequiredError,
|
|
144
|
+
ValenceBlockError,
|
|
145
|
+
} from '@valence-ai/sdk';
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
await guard.runTool(
|
|
149
|
+
{
|
|
150
|
+
tool: 'restart_service',
|
|
151
|
+
action: 'write',
|
|
152
|
+
parameters: { service: 'payments-api' },
|
|
153
|
+
},
|
|
154
|
+
async () => restartService('payments-api')
|
|
155
|
+
);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (error instanceof ValenceApprovalRequiredError) {
|
|
158
|
+
console.log('Approval required:', error.message, error.approvalId);
|
|
159
|
+
} else if (error instanceof ValenceBlockError) {
|
|
160
|
+
console.log('Blocked:', error.message);
|
|
161
|
+
} else {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Dry-run example
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
const decision = await guard.runTool(
|
|
171
|
+
{
|
|
172
|
+
tool: 'rollback_deployment',
|
|
173
|
+
action: 'write',
|
|
174
|
+
parameters: { service: 'checkout-api' },
|
|
175
|
+
dryRun: true,
|
|
176
|
+
},
|
|
177
|
+
async () => ({ simulated: true })
|
|
178
|
+
);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Use dry-run when you want to preview the policy outcome without committing to a real operation.
|
|
182
|
+
|
|
183
|
+
## Exported API
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
createValenceClient
|
|
187
|
+
createToolExecutionGuard
|
|
188
|
+
guardToolExecution
|
|
189
|
+
withValenceGuard
|
|
190
|
+
ValenceDecisionError
|
|
191
|
+
ValenceBlockError
|
|
192
|
+
ValenceApprovalRequiredError
|
|
193
|
+
isValenceDecisionError
|
|
194
|
+
```
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEpF,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,oBAAoB;kBAMrC,SAAS,GAAG,OAAO,CAAC,gBAAgB,CAAC;EAwChE"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function createValenceClient(options) {
|
|
2
|
+
const request = options.fetch ?? fetch;
|
|
3
|
+
const decidePath = options.decidePath ?? '/api/decide';
|
|
4
|
+
const simulatePath = options.simulatePath ?? '/api/simulate';
|
|
5
|
+
return {
|
|
6
|
+
async decide(event) {
|
|
7
|
+
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
|
|
8
|
+
const timeout = controller && options.timeoutMs
|
|
9
|
+
? setTimeout(() => controller.abort(), options.timeoutMs)
|
|
10
|
+
: null;
|
|
11
|
+
try {
|
|
12
|
+
const path = event.dryRun ? simulatePath : decidePath;
|
|
13
|
+
const response = await request(`${options.baseUrl}${path}`, {
|
|
14
|
+
method: 'POST',
|
|
15
|
+
headers: {
|
|
16
|
+
'Content-Type': 'application/json',
|
|
17
|
+
'x-api-key': options.apiKey,
|
|
18
|
+
...options.headers,
|
|
19
|
+
},
|
|
20
|
+
signal: controller?.signal,
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
...event,
|
|
23
|
+
timestamp: event.timestamp ?? new Date().toISOString(),
|
|
24
|
+
dryRun: event.dryRun ?? false,
|
|
25
|
+
parameters: event.parameters ?? {},
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error(`Valence decision request failed with ${response.status}`);
|
|
30
|
+
}
|
|
31
|
+
return (await response.json());
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
if (timeout) {
|
|
35
|
+
clearTimeout(timeout);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,mBAAmB,CAAC,OAA6B;IAC7D,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC;IACvC,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,aAAa,CAAC;IACvD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,eAAe,CAAC;IAE7D,OAAO;QACH,KAAK,CAAC,MAAM,CAAC,KAAgB;YACzB,MAAM,UAAU,GACZ,OAAO,eAAe,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,eAAe,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;YAC1E,MAAM,OAAO,GACT,UAAU,IAAI,OAAO,CAAC,SAAS;gBAC3B,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,OAAO,CAAC,SAAS,CAAC;gBACzD,CAAC,CAAC,IAAI,CAAC;YAEf,IAAI,CAAC;gBACD,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,CAAC;gBACtD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,EAAE,EAAE;oBACxD,MAAM,EAAE,MAAM;oBACd,OAAO,EAAE;wBACL,cAAc,EAAE,kBAAkB;wBAClC,WAAW,EAAE,OAAO,CAAC,MAAM;wBAC3B,GAAG,OAAO,CAAC,OAAO;qBACrB;oBACD,MAAM,EAAE,UAAU,EAAE,MAAM;oBAC1B,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;wBACjB,GAAG,KAAK;wBACR,SAAS,EAAE,KAAK,CAAC,SAAS,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;wBACtD,MAAM,EAAE,KAAK,CAAC,MAAM,IAAI,KAAK;wBAC7B,UAAU,EAAE,KAAK,CAAC,UAAU,IAAI,EAAE;qBACrC,CAAC;iBACL,CAAC,CAAC;gBAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACf,MAAM,IAAI,KAAK,CACX,wCAAwC,QAAQ,CAAC,MAAM,EAAE,CAC5D,CAAC;gBACN,CAAC;gBAED,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAqB,CAAC;YACvD,CAAC;oBAAS,CAAC;gBACP,IAAI,OAAO,EAAE,CAAC;oBACV,YAAY,CAAC,OAAO,CAAC,CAAC;gBAC1B,CAAC;YACL,CAAC;QACL,CAAC;KACJ,CAAC;AACN,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DecisionResponse, ToolEvent } from './types.js';
|
|
2
|
+
export declare class ValenceDecisionError extends Error {
|
|
3
|
+
readonly decision: DecisionResponse['decision'];
|
|
4
|
+
readonly risk: DecisionResponse['risk'];
|
|
5
|
+
readonly policyId: string | null;
|
|
6
|
+
readonly approvalId: string | null;
|
|
7
|
+
readonly latencyMs: number;
|
|
8
|
+
readonly event: ToolEvent;
|
|
9
|
+
constructor(message: string, decision: DecisionResponse, event: ToolEvent);
|
|
10
|
+
}
|
|
11
|
+
export declare class ValenceBlockError extends ValenceDecisionError {
|
|
12
|
+
constructor(decision: DecisionResponse, event: ToolEvent);
|
|
13
|
+
}
|
|
14
|
+
export declare class ValenceApprovalRequiredError extends ValenceDecisionError {
|
|
15
|
+
constructor(decision: DecisionResponse, event: ToolEvent);
|
|
16
|
+
}
|
|
17
|
+
export declare function isValenceDecisionError(error: unknown): error is ValenceDecisionError;
|
|
18
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE9D,qBAAa,oBAAqB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAChD,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACxC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;gBAEd,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,KAAK,EAAE,SAAS;CAU5E;AAED,qBAAa,iBAAkB,SAAQ,oBAAoB;gBAC3C,QAAQ,EAAE,gBAAgB,EAAE,KAAK,EAAE,SAAS;CAI3D;AAED,qBAAa,4BAA6B,SAAQ,oBAAoB;gBACtD,QAAQ,EAAE,gBAAgB,EAAE,KAAK,EAAE,SAAS;CAQ3D;AAED,wBAAgB,sBAAsB,CAClC,KAAK,EAAE,OAAO,GACf,KAAK,IAAI,oBAAoB,CAE/B"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class ValenceDecisionError extends Error {
|
|
2
|
+
constructor(message, decision, event) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'ValenceDecisionError';
|
|
5
|
+
this.decision = decision.decision;
|
|
6
|
+
this.risk = decision.risk;
|
|
7
|
+
this.policyId = decision.policyId;
|
|
8
|
+
this.approvalId = decision.approvalId;
|
|
9
|
+
this.latencyMs = decision.latencyMs;
|
|
10
|
+
this.event = event;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class ValenceBlockError extends ValenceDecisionError {
|
|
14
|
+
constructor(decision, event) {
|
|
15
|
+
super(decision.reason, decision, event);
|
|
16
|
+
this.name = 'ValenceBlockError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class ValenceApprovalRequiredError extends ValenceDecisionError {
|
|
20
|
+
constructor(decision, event) {
|
|
21
|
+
super(`${decision.reason} Approval id: ${decision.approvalId ?? 'pending'}`, decision, event);
|
|
22
|
+
this.name = 'ValenceApprovalRequiredError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function isValenceDecisionError(error) {
|
|
26
|
+
return error instanceof ValenceDecisionError;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAQ3C,YAAY,OAAe,EAAE,QAA0B,EAAE,KAAgB;QACrE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC;QAClC,IAAI,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC;QACtC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;CACJ;AAED,MAAM,OAAO,iBAAkB,SAAQ,oBAAoB;IACvD,YAAY,QAA0B,EAAE,KAAgB;QACpD,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IACpC,CAAC;CACJ;AAED,MAAM,OAAO,4BAA6B,SAAQ,oBAAoB;IAClE,YAAY,QAA0B,EAAE,KAAgB;QACpD,KAAK,CACD,GAAG,QAAQ,CAAC,MAAM,iBAAiB,QAAQ,CAAC,UAAU,IAAI,SAAS,EAAE,EACrE,QAAQ,EACR,KAAK,CACR,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,8BAA8B,CAAC;IAC/C,CAAC;CACJ;AAED,MAAM,UAAU,sBAAsB,CAClC,KAAc;IAEd,OAAO,KAAK,YAAY,oBAAoB,CAAC;AACjD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createValenceClient } from './client.js';
|
|
2
|
+
export { createToolExecutionGuard, guardToolExecution, withValenceGuard, } from './wrap-tool.js';
|
|
3
|
+
export { ValenceApprovalRequiredError, ValenceBlockError, ValenceDecisionError, isValenceDecisionError, } from './errors.js';
|
|
4
|
+
export type { DecisionResponse, ToolDefinition, ToolEvent, ToolExecutionContext, ToolExecutionHooks, ToolExecutionRequest, ToolParameters, ValenceClientOptions, ValenceDecision, ValenceEnvironment, ValenceRisk, } from './types.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EACH,wBAAwB,EACxB,kBAAkB,EAClB,gBAAgB,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,4BAA4B,EAC5B,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,GACzB,MAAM,aAAa,CAAC;AACrB,YAAY,EACR,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,cAAc,EACd,oBAAoB,EACpB,eAAe,EACf,kBAAkB,EAClB,WAAW,GACd,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createValenceClient } from './client.js';
|
|
2
|
+
export { createToolExecutionGuard, guardToolExecution, withValenceGuard, } from './wrap-tool.js';
|
|
3
|
+
export { ValenceApprovalRequiredError, ValenceBlockError, ValenceDecisionError, isValenceDecisionError, } from './errors.js';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EACH,wBAAwB,EACxB,kBAAkB,EAClB,gBAAgB,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACH,4BAA4B,EAC5B,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,GACzB,MAAM,aAAa,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type ValenceDecision = 'ALLOW' | 'BLOCK' | 'REQUIRES_APPROVAL';
|
|
2
|
+
export type ValenceRisk = 'LOW' | 'MEDIUM' | 'HIGH';
|
|
3
|
+
export type ValenceEnvironment = 'production' | 'staging' | 'sandbox';
|
|
4
|
+
export type ToolParameters = Record<string, unknown>;
|
|
5
|
+
export type ToolEvent = {
|
|
6
|
+
agentId?: string;
|
|
7
|
+
agentName: string;
|
|
8
|
+
projectId?: string;
|
|
9
|
+
tool: string;
|
|
10
|
+
action: string;
|
|
11
|
+
environment: ValenceEnvironment;
|
|
12
|
+
goal: string;
|
|
13
|
+
sessionId?: string;
|
|
14
|
+
workspaceId?: string;
|
|
15
|
+
parameters?: ToolParameters;
|
|
16
|
+
dryRun?: boolean;
|
|
17
|
+
timestamp?: string;
|
|
18
|
+
};
|
|
19
|
+
export type DecisionResponse = {
|
|
20
|
+
decision: ValenceDecision;
|
|
21
|
+
reason: string;
|
|
22
|
+
risk: ValenceRisk;
|
|
23
|
+
policyId: string | null;
|
|
24
|
+
approvalId: string | null;
|
|
25
|
+
latencyMs: number;
|
|
26
|
+
};
|
|
27
|
+
export type ToolExecutionContext = Omit<ToolEvent, 'tool' | 'action' | 'parameters' | 'timestamp'>;
|
|
28
|
+
export type ToolDefinition = {
|
|
29
|
+
tool: string;
|
|
30
|
+
action: string;
|
|
31
|
+
parameters?: ToolParameters;
|
|
32
|
+
};
|
|
33
|
+
export type ToolExecutionRequest = ToolExecutionContext & ToolDefinition;
|
|
34
|
+
export type ToolExecutionHooks<T> = {
|
|
35
|
+
onDecision?: (decision: DecisionResponse, event: ToolEvent) => void | Promise<void>;
|
|
36
|
+
onAllow?: (decision: DecisionResponse, event: ToolEvent) => void | Promise<void>;
|
|
37
|
+
onBlock?: (decision: DecisionResponse, event: ToolEvent) => void | Promise<void>;
|
|
38
|
+
onApprovalRequired?: (decision: DecisionResponse, event: ToolEvent) => void | Promise<void>;
|
|
39
|
+
onExecuted?: (result: T, decision: DecisionResponse, event: ToolEvent) => void | Promise<void>;
|
|
40
|
+
};
|
|
41
|
+
export type ValenceFetch = typeof fetch;
|
|
42
|
+
export type ValenceClientOptions = {
|
|
43
|
+
baseUrl: string;
|
|
44
|
+
apiKey: string;
|
|
45
|
+
fetch?: ValenceFetch;
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
headers?: Record<string, string>;
|
|
48
|
+
decidePath?: string;
|
|
49
|
+
simulatePath?: string;
|
|
50
|
+
};
|
|
51
|
+
//# sourceMapp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|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ToolEvent, ToolExecutionContext, ToolExecutionHooks, ToolExecutionRequest } from './types.js';
|
|
2
|
+
import { createValenceClient } from './client.js';
|
|
3
|
+
type ValenceClient = ReturnType<typeof createValenceClient>;
|
|
4
|
+
export declare function guardToolExecution<T>(client: ValenceClient, request: ToolExecutionRequest, execute: () => Promise<T>, hooks?: ToolExecutionHooks<T>): Promise<T>;
|
|
5
|
+
export declare function createToolExecutionGuard(client: ValenceClient, context: ToolExecutionContext, hooks?: ToolExecutionHooks<unknown>): {
|
|
6
|
+
runTool: <T>(request: Omit<ToolExecutionRequest, keyof ToolExecutionContext>, execute: () => Promise<T>, runtimeHooks?: ToolExecutionHooks<T>) => Promise<T>;
|
|
7
|
+
wrapTool<T>(request: Omit<ToolExecutionRequest, keyof ToolExecutionContext>, execute: () => Promise<T>, runtimeHooks?: ToolExecutionHooks<T>): () => Promise<T>;
|
|
8
|
+
};
|
|
9
|
+
export type ValenceGuardOptions = ToolEvent;
|
|
10
|
+
export declare function withValenceGuard(client: ValenceClient, event: ValenceGuardOptions): <T>(toolFn: () => Promise<T>, hooks?: ToolExecutionHooks<T>) => Promise<T>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=wrap-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrap-tool.d.ts","sourceRoot":"","sources":["../src/wrap-tool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAER,SAAS,EACT,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACvB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,KAAK,aAAa,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAsB5D,wBAAsB,kBAAkB,CAAC,CAAC,EACtC,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACzB,KAAK,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,cAsBhC;AAED,wBAAgB,wBAAwB,CACpC,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,oBAAoB,EAC7B,KAAK,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC;cAEZ,CAAC,WACX,IAAI,CAAC,oBAAoB,EAAE,MAAM,oBAAoB,CAAC,WACtD,MAAM,OAAO,CAAC,CAAC,CAAC,iBACV,kBAAkB,CAAC,CAAC,CAAC;aAmB3B,CAAC,WACG,IAAI,CAAC,oBAAoB,EAAE,MAAM,oBAAoB,CAAC,WACtD,MAAM,OAAO,CAAC,CAAC,CAAC,iBACV,kBAAkB,CAAC,CAAC,CAAC;EAK/C;AAED,MAAM,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAE5C,wBAAgB,gBAAgB,CAC5B,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,mBAAmB,IAEW,CAAC,EAClC,QAAQ,MAAM,OAAO,CAAC,CAAC,CAAC,EACxB,QAAQ,kBAAkB,CAAC,CAAC,CAAC,gBAIpC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ValenceApprovalRequiredError, ValenceBlockError } from './errors.js';
|
|
2
|
+
async function applyHooks(hooks, decision, event) {
|
|
3
|
+
await hooks?.onDecision?.(decision, event);
|
|
4
|
+
if (decision.decision === 'ALLOW') {
|
|
5
|
+
await hooks?.onAllow?.(decision, event);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (decision.decision === 'REQUIRES_APPROVAL') {
|
|
9
|
+
await hooks?.onApprovalRequired?.(decision, event);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
await hooks?.onBlock?.(decision, event);
|
|
13
|
+
}
|
|
14
|
+
export async function guardToolExecution(client, request, execute, hooks) {
|
|
15
|
+
const event = {
|
|
16
|
+
...request,
|
|
17
|
+
parameters: request.parameters ?? {},
|
|
18
|
+
dryRun: request.dryRun ?? false,
|
|
19
|
+
};
|
|
20
|
+
const decision = await client.decide(event);
|
|
21
|
+
await applyHooks(hooks, decision, event);
|
|
22
|
+
if (decision.decision === 'ALLOW') {
|
|
23
|
+
const result = await execute();
|
|
24
|
+
await hooks?.onExecuted?.(result, decision, event);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
if (decision.decision === 'REQUIRES_APPROVAL') {
|
|
28
|
+
throw new ValenceApprovalRequiredError(decision, event);
|
|
29
|
+
}
|
|
30
|
+
throw new ValenceBlockError(decision, event);
|
|
31
|
+
}
|
|
32
|
+
export function createToolExecutionGuard(client, context, hooks) {
|
|
33
|
+
const runTool = async (request, execute, runtimeHooks) => {
|
|
34
|
+
return guardToolExecution(client, {
|
|
35
|
+
...context,
|
|
36
|
+
...request,
|
|
37
|
+
}, execute, {
|
|
38
|
+
...hooks,
|
|
39
|
+
...runtimeHooks,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
runTool,
|
|
44
|
+
wrapTool(request, execute, runtimeHooks) {
|
|
45
|
+
return () => runTool(request, execute, runtimeHooks);
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export function withValenceGuard(client, event) {
|
|
50
|
+
return async function runGuardedTool(toolFn, hooks) {
|
|
51
|
+
return guardToolExecution(client, event, toolFn, hooks);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=wrap-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrap-tool.js","sourceRoot":"","sources":["../src/wrap-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAY9E,KAAK,UAAU,U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E,KAAK,CAAC,CAAC;IAC5D,CAAC;IAED,MAAM,IAAI,iBAAiB,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AACjD,CAAC;AAED,MAAM,UAAU,wBAAwB,CACpC,MAAqB,EACrB,OAA6B,EAC7B,KAAmC;IAEnC,MAAM,OAAO,GAAG,KAAK,EACjB,OAA+D,EAC/D,OAAyB,EACzB,YAAoC,EACtC,EAAE;QACA,OAAO,kBAAkB,CACrB,MAAM,EACN;YACI,GAAG,OAAO;YACV,GAAG,OAAO;SACb,EACD,OAAO,EACP;YACI,GAAI,KAA2C;YAC/C,GAAG,YAAY;SAClB,CACJ,CAAC;IACN,CAAC,CAAC;IAEF,OAAO;QACH,OAAO;QAEP,QAAQ,CACJ,OAA+D,EAC/D,OAAyB,EACzB,YAAoC;YAEpC,OAAO,GAAG,EAAE,CAAC,OAAO,CAAI,OAAO,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;QAC5D,CAAC;KACJ,CAAC;AACN,CAAC;AAID,MAAM,UAAU,gBAAgB,CAC5B,MAAqB,EACrB,KAA0B;IAE1B,OAAO,KAAK,UAAU,cAAc,CAChC,MAAwB,EACxB,KAA6B;QAE7B,OAAO,kBAAkB,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAC5D,CAAC,CAAC;AACN,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@valence-ai/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pre-execution policy enforcement SDK for tool-using AI agents.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/praveensahu-dev/Valence-AI.git",
|
|
8
|
+
"directory": "sdk"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://www.valenceai.co",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/praveensahu-dev/Valence-AI/issues"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.json",
|
|
33
|
+
"clean": "if exist dist rmdir /s /q dist",
|
|
34
|
+
"prepack": "npm run clean && npm run build"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"ai",
|
|
44
|
+
"agent",
|
|
45
|
+
"sdk",
|
|
46
|
+
"security",
|
|
47
|
+
"tool-calling",
|
|
48
|
+
"guardrails",
|
|
49
|
+
"policy"
|
|
50
|
+
]
|
|
51
|
+
}
|