@creedspace/sdk 1.0.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 ADDED
@@ -0,0 +1,195 @@
1
+ # @creed-space/sdk
2
+
3
+ Official TypeScript SDK for Creed Space - governance infrastructure for AI agents.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @creed-space/sdk
9
+ # or
10
+ yarn add @creed-space/sdk
11
+ # or
12
+ pnpm add @creed-space/sdk
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { createClient } from '@creed-space/sdk';
19
+
20
+ const client = createClient({
21
+ apiKey: process.env.CREED_API_KEY!,
22
+ });
23
+
24
+ // Get a governance decision for a tool call
25
+ const result = await client.decide({
26
+ toolName: 'send_email',
27
+ arguments: {
28
+ to: 'user@example.com',
29
+ subject: 'Hello',
30
+ },
31
+ onAllow: (decision) => {
32
+ console.log('Authorized:', decision.decisionToken);
33
+ // Execute the tool with the signed token
34
+ },
35
+ onDeny: (decision) => {
36
+ console.log('Denied:', decision.reasons);
37
+ },
38
+ });
39
+ ```
40
+
41
+ ## Features
42
+
43
+ - **Governance Decisions** - Get ALLOW/DENY decisions for tool calls
44
+ - **Cryptographic Tokens** - Signed JWT tokens for authorization proof
45
+ - **Callback Flow Control** - `onAllow`, `onDeny`, `onRequireHuman` callbacks
46
+ - **Audit Trail** - Query tamper-evident hash-chain audit logs
47
+ - **TypeScript Native** - Full type definitions included
48
+
49
+ ## API Reference
50
+
51
+ ### `createClient(options)`
52
+
53
+ Create a new Creed Space client.
54
+
55
+ ```typescript
56
+ const client = createClient({
57
+ apiKey: 'crd_live_...', // Required: Your API key
58
+ baseUrl: 'https://api.creed.space', // Optional: Custom API URL
59
+ timeoutMs: 30000, // Optional: Request timeout (default: 30s)
60
+ fetch: customFetch, // Optional: Custom fetch implementation
61
+ });
62
+ ```
63
+
64
+ ### `client.decide(request)`
65
+
66
+ Get a governance decision for a tool call.
67
+
68
+ ```typescript
69
+ interface DecideRequest {
70
+ toolName: string; // Tool to execute
71
+ arguments: Record<string, unknown>; // Tool arguments
72
+ constitutionId?: string; // Constitution ID (default: 'default')
73
+ context?: {
74
+ tenantId?: string;
75
+ projectId?: string;
76
+ userId?: string;
77
+ sessionId?: string;
78
+ };
79
+ onAllow?: (decision: AllowDecision) => void | Promise<void>;
80
+ onDeny?: (decision: DenyDecision) => void | Promise<void>;
81
+ onRequireHuman?: (decision: RequireHumanDecision) => void | Promise<void>;
82
+ }
83
+
84
+ const result = await client.decide({
85
+ toolName: 'send_email',
86
+ arguments: { to: 'user@example.com' },
87
+ onAllow: async (decision) => {
88
+ // Execute the tool
89
+ await sendEmail(decision.decisionToken);
90
+ },
91
+ });
92
+ ```
93
+
94
+ ### `client.authorize(request)`
95
+
96
+ Verify a decision token before execution.
97
+
98
+ ```typescript
99
+ const auth = await client.authorize({
100
+ decisionToken: result.decisionToken,
101
+ toolName: 'send_email', // Optional: verify tool name matches
102
+ });
103
+
104
+ if (auth.authorized) {
105
+ console.log('Token valid until:', auth.claims?.expiresAt);
106
+ }
107
+ ```
108
+
109
+ ### `client.audit(request)`
110
+
111
+ Query the audit trail for a run.
112
+
113
+ ```typescript
114
+ const audit = await client.audit({
115
+ runId: 'run_123',
116
+ limit: 50,
117
+ });
118
+
119
+ console.log('Events:', audit.events);
120
+ console.log('Integrity verified:', audit.integrity.verified);
121
+ ```
122
+
123
+ ### `client.status()`
124
+
125
+ Get service status and feature availability.
126
+
127
+ ```typescript
128
+ const status = await client.status();
129
+ console.log('Service:', status.service);
130
+ console.log('Features:', status.features);
131
+ ```
132
+
133
+ ## Utilities
134
+
135
+ ### `computeArgsHash(args)`
136
+
137
+ Compute SHA-256 hash of arguments for verification.
138
+
139
+ ```typescript
140
+ import { computeArgsHash } from '@creed-space/sdk';
141
+
142
+ const hash = await computeArgsHash({ to: 'user@example.com' });
143
+ // 'sha256:...'
144
+ ```
145
+
146
+ ### `isTokenExpired(token)`
147
+
148
+ Check if a decision token is expired.
149
+
150
+ ```typescript
151
+ import { isTokenExpired } from '@creed-space/sdk';
152
+
153
+ if (isTokenExpired(token)) {
154
+ // Request a new decision
155
+ }
156
+ ```
157
+
158
+ ## Error Handling
159
+
160
+ ```typescript
161
+ import { createClient, CreedError } from '@creed-space/sdk';
162
+
163
+ try {
164
+ await client.decide({ ... });
165
+ } catch (error) {
166
+ if (error instanceof CreedError) {
167
+ console.error('Code:', error.code);
168
+ console.error('Status:', error.statusCode);
169
+ }
170
+ }
171
+ ```
172
+
173
+ Error codes:
174
+ - `MISSING_API_KEY` - API key not provided
175
+ - `REQUEST_FAILED` - API request failed
176
+ - `NETWORK_ERROR` - Network connectivity issue
177
+ - `TIMEOUT` - Request timed out
178
+
179
+ ## Decision Types
180
+
181
+ | Decision | Status | Description |
182
+ |----------|--------|-------------|
183
+ | `ALLOW` | Active | Tool execution authorized |
184
+ | `DENY` | Active | Tool execution blocked |
185
+ | `REQUIRE_HUMAN` | Planned | Human review required |
186
+ | `REQUIRE_STEPUP` | Planned | Step-up authentication required |
187
+
188
+ ## Requirements
189
+
190
+ - Node.js 18+
191
+ - TypeScript 5.0+ (for type definitions)
192
+
193
+ ## License
194
+
195
+ MIT
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Creed Space SDK for TypeScript
3
+ *
4
+ * Provides governance infrastructure for AI agents with:
5
+ * - Tool call authorization with cryptographic proof
6
+ * - Callback-based flow control (onAllow, onDeny, onRequireHuman)
7
+ * - Hash-chain audit trails
8
+ *
9
+ * Design inspired by Superagent's clean SDK patterns.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { createClient } from '@creed-space/sdk';
14
+ *
15
+ * const client = createClient({ apiKey: 'crd_live_...' });
16
+ *
17
+ * const result = await client.decide({
18
+ * toolName: 'send_email',
19
+ * arguments: { to: 'user@example.com', subject: 'Hello' },
20
+ * onAllow: (decision) => {
21
+ * console.log('Authorized:', decision.token);
22
+ * // Execute the tool
23
+ * },
24
+ * onDeny: (decision) => {
25
+ * console.log('Denied:', decision.reasons);
26
+ * },
27
+ * onRequireHuman: (decision) => {
28
+ * // Feature planned - will trigger human review workflow
29
+ * console.log('Human review required (planned feature)');
30
+ * },
31
+ * });
32
+ * ```
33
+ */
34
+ type DecisionType = 'ALLOW' | 'DENY' | 'REQUIRE_HUMAN';
35
+ interface Risk {
36
+ score: number;
37
+ labels: string[];
38
+ }
39
+ interface DecideRequest {
40
+ /** Name of the tool to execute */
41
+ toolName: string;
42
+ /** Tool arguments */
43
+ arguments: Record<string, unknown>;
44
+ /** Constitution ID (default: 'default') */
45
+ constitutionId?: string;
46
+ /** Optional context */
47
+ context?: {
48
+ tenantId?: string;
49
+ projectId?: string;
50
+ userId?: string;
51
+ sessionId?: string;
52
+ };
53
+ /** Callback when tool is allowed */
54
+ onAllow?: (decision: AllowDecision) => void | Promise<void>;
55
+ /** Callback when tool is denied */
56
+ onDeny?: (decision: DenyDecision) => void | Promise<void>;
57
+ /** Callback when human review is required (planned feature) */
58
+ onRequireHuman?: (decision: RequireHumanDecision) => void | Promise<void>;
59
+ }
60
+ interface AllowDecision {
61
+ decision: 'ALLOW';
62
+ runId: string;
63
+ actionId: string;
64
+ toolCallId: string;
65
+ argsHash: string;
66
+ risk: Risk;
67
+ /** Signed JWT decision token */
68
+ decisionToken: string;
69
+ /** Token expiry */
70
+ expiresAt: string;
71
+ }
72
+ interface DenyDecision {
73
+ decision: 'DENY';
74
+ runId: string;
75
+ actionId: string;
76
+ toolCallId: string;
77
+ argsHash: string;
78
+ risk: Risk;
79
+ reasons: string[];
80
+ guidance: {
81
+ message: string;
82
+ suggestion?: string;
83
+ };
84
+ }
85
+ interface RequireHumanDecision {
86
+ decision: 'REQUIRE_HUMAN';
87
+ runId: string;
88
+ actionId: string;
89
+ toolCallId: string;
90
+ argsHash: string;
91
+ risk: Risk;
92
+ /** Review ID for tracking (planned) */
93
+ reviewId?: string;
94
+ /** Status - always 'planned' until feature is released */
95
+ featureStatus: 'planned';
96
+ }
97
+ type DecideResult = AllowDecision | DenyDecision | RequireHumanDecision;
98
+ interface AuthorizeRequest {
99
+ /** The decision token to verify */
100
+ decisionToken: string;
101
+ /** Tool name (must match token) */
102
+ toolName?: string;
103
+ /** Args hash (must match token) */
104
+ argsHash?: string;
105
+ }
106
+ interface AuthorizeResult {
107
+ authorized: boolean;
108
+ claims?: {
109
+ actionId: string;
110
+ toolName: string;
111
+ toolCallId: string;
112
+ argsHash: string;
113
+ runId: string;
114
+ decision: string;
115
+ issuedAt: string;
116
+ expiresAt: string;
117
+ };
118
+ error?: string;
119
+ message: string;
120
+ }
121
+ interface AuditRequest {
122
+ /** Run ID to query */
123
+ runId: string;
124
+ /** Specific action ID (optional) */
125
+ actionId?: string;
126
+ /** Max events to return */
127
+ limit?: number;
128
+ }
129
+ interface AuditEvent {
130
+ seq: number;
131
+ type: string;
132
+ timestamp: string;
133
+ data: Record<string, unknown>;
134
+ hash: string;
135
+ }
136
+ interface AuditResult {
137
+ runId: string;
138
+ actionId?: string;
139
+ eventCount: number;
140
+ events: AuditEvent[];
141
+ integrity: {
142
+ chain: string;
143
+ verified: boolean;
144
+ };
145
+ }
146
+ interface CreedClientOptions {
147
+ /** API key (crd_live_... or crd_test_...) */
148
+ apiKey: string;
149
+ /** Base URL (default: https://api.creed.space) */
150
+ baseUrl?: string;
151
+ /** Custom fetch implementation */
152
+ fetch?: typeof fetch;
153
+ /** Request timeout in ms (default: 30000) */
154
+ timeoutMs?: number;
155
+ }
156
+ interface CreedClient {
157
+ /** Get a governance decision for a tool call */
158
+ decide(request: DecideRequest): Promise<DecideResult>;
159
+ /** Verify a decision token before execution */
160
+ authorize(request: AuthorizeRequest): Promise<AuthorizeResult>;
161
+ /** Query the audit trail */
162
+ audit(request: AuditRequest): Promise<AuditResult>;
163
+ /** Get service status */
164
+ status(): Promise<StatusResult>;
165
+ }
166
+ interface StatusResult {
167
+ service: string;
168
+ version: string;
169
+ features: Record<string, {
170
+ status: string;
171
+ description: string;
172
+ }>;
173
+ decisionTypes: Record<string, string>;
174
+ }
175
+ declare class CreedError extends Error {
176
+ code: string;
177
+ statusCode?: number | undefined;
178
+ constructor(message: string, code: string, statusCode?: number | undefined);
179
+ }
180
+ /**
181
+ * Create a Creed Space client.
182
+ *
183
+ * @param options - Client configuration
184
+ * @returns CreedClient instance
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * const client = createClient({
189
+ * apiKey: process.env.CREED_API_KEY!,
190
+ * baseUrl: 'https://api.creed.space',
191
+ * });
192
+ * ```
193
+ */
194
+ declare function createClient(options: CreedClientOptions): CreedClient;
195
+ /**
196
+ * Compute SHA-256 hash of arguments (for verification).
197
+ * Uses the same canonical JSON format as the server.
198
+ */
199
+ declare function computeArgsHash(args: Record<string, unknown>): Promise<string>;
200
+ /**
201
+ * Check if a decision token is expired (without verification).
202
+ */
203
+ declare function isTokenExpired(token: string): boolean;
204
+
205
+ export { type AllowDecision, type AuditEvent, type AuditRequest, type AuditResult, type AuthorizeRequest, type AuthorizeResult, type CreedClient, type CreedClientOptions, CreedError, type DecideRequest, type DecideResult, type DecisionType, type DenyDecision, type RequireHumanDecision, type Risk, type StatusResult, computeArgsHash, createClient, createClient as default, isTokenExpired };
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Creed Space SDK for TypeScript
3
+ *
4
+ * Provides governance infrastructure for AI agents with:
5
+ * - Tool call authorization with cryptographic proof
6
+ * - Callback-based flow control (onAllow, onDeny, onRequireHuman)
7
+ * - Hash-chain audit trails
8
+ *
9
+ * Design inspired by Superagent's clean SDK patterns.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { createClient } from '@creed-space/sdk';
14
+ *
15
+ * const client = createClient({ apiKey: 'crd_live_...' });
16
+ *
17
+ * const result = await client.decide({
18
+ * toolName: 'send_email',
19
+ * arguments: { to: 'user@example.com', subject: 'Hello' },
20
+ * onAllow: (decision) => {
21
+ * console.log('Authorized:', decision.token);
22
+ * // Execute the tool
23
+ * },
24
+ * onDeny: (decision) => {
25
+ * console.log('Denied:', decision.reasons);
26
+ * },
27
+ * onRequireHuman: (decision) => {
28
+ * // Feature planned - will trigger human review workflow
29
+ * console.log('Human review required (planned feature)');
30
+ * },
31
+ * });
32
+ * ```
33
+ */
34
+ type DecisionType = 'ALLOW' | 'DENY' | 'REQUIRE_HUMAN';
35
+ interface Risk {
36
+ score: number;
37
+ labels: string[];
38
+ }
39
+ interface DecideRequest {
40
+ /** Name of the tool to execute */
41
+ toolName: string;
42
+ /** Tool arguments */
43
+ arguments: Record<string, unknown>;
44
+ /** Constitution ID (default: 'default') */
45
+ constitutionId?: string;
46
+ /** Optional context */
47
+ context?: {
48
+ tenantId?: string;
49
+ projectId?: string;
50
+ userId?: string;
51
+ sessionId?: string;
52
+ };
53
+ /** Callback when tool is allowed */
54
+ onAllow?: (decision: AllowDecision) => void | Promise<void>;
55
+ /** Callback when tool is denied */
56
+ onDeny?: (decision: DenyDecision) => void | Promise<void>;
57
+ /** Callback when human review is required (planned feature) */
58
+ onRequireHuman?: (decision: RequireHumanDecision) => void | Promise<void>;
59
+ }
60
+ interface AllowDecision {
61
+ decision: 'ALLOW';
62
+ runId: string;
63
+ actionId: string;
64
+ toolCallId: string;
65
+ argsHash: string;
66
+ risk: Risk;
67
+ /** Signed JWT decision token */
68
+ decisionToken: string;
69
+ /** Token expiry */
70
+ expiresAt: string;
71
+ }
72
+ interface DenyDecision {
73
+ decision: 'DENY';
74
+ runId: string;
75
+ actionId: string;
76
+ toolCallId: string;
77
+ argsHash: string;
78
+ risk: Risk;
79
+ reasons: string[];
80
+ guidance: {
81
+ message: string;
82
+ suggestion?: string;
83
+ };
84
+ }
85
+ interface RequireHumanDecision {
86
+ decision: 'REQUIRE_HUMAN';
87
+ runId: string;
88
+ actionId: string;
89
+ toolCallId: string;
90
+ argsHash: string;
91
+ risk: Risk;
92
+ /** Review ID for tracking (planned) */
93
+ reviewId?: string;
94
+ /** Status - always 'planned' until feature is released */
95
+ featureStatus: 'planned';
96
+ }
97
+ type DecideResult = AllowDecision | DenyDecision | RequireHumanDecision;
98
+ interface AuthorizeRequest {
99
+ /** The decision token to verify */
100
+ decisionToken: string;
101
+ /** Tool name (must match token) */
102
+ toolName?: string;
103
+ /** Args hash (must match token) */
104
+ argsHash?: string;
105
+ }
106
+ interface AuthorizeResult {
107
+ authorized: boolean;
108
+ claims?: {
109
+ actionId: string;
110
+ toolName: string;
111
+ toolCallId: string;
112
+ argsHash: string;
113
+ runId: string;
114
+ decision: string;
115
+ issuedAt: string;
116
+ expiresAt: string;
117
+ };
118
+ error?: string;
119
+ message: string;
120
+ }
121
+ interface AuditRequest {
122
+ /** Run ID to query */
123
+ runId: string;
124
+ /** Specific action ID (optional) */
125
+ actionId?: string;
126
+ /** Max events to return */
127
+ limit?: number;
128
+ }
129
+ interface AuditEvent {
130
+ seq: number;
131
+ type: string;
132
+ timestamp: string;
133
+ data: Record<string, unknown>;
134
+ hash: string;
135
+ }
136
+ interface AuditResult {
137
+ runId: string;
138
+ actionId?: string;
139
+ eventCount: number;
140
+ events: AuditEvent[];
141
+ integrity: {
142
+ chain: string;
143
+ verified: boolean;
144
+ };
145
+ }
146
+ interface CreedClientOptions {
147
+ /** API key (crd_live_... or crd_test_...) */
148
+ apiKey: string;
149
+ /** Base URL (default: https://api.creed.space) */
150
+ baseUrl?: string;
151
+ /** Custom fetch implementation */
152
+ fetch?: typeof fetch;
153
+ /** Request timeout in ms (default: 30000) */
154
+ timeoutMs?: number;
155
+ }
156
+ interface CreedClient {
157
+ /** Get a governance decision for a tool call */
158
+ decide(request: DecideRequest): Promise<DecideResult>;
159
+ /** Verify a decision token before execution */
160
+ authorize(request: AuthorizeRequest): Promise<AuthorizeResult>;
161
+ /** Query the audit trail */
162
+ audit(request: AuditRequest): Promise<AuditResult>;
163
+ /** Get service status */
164
+ status(): Promise<StatusResult>;
165
+ }
166
+ interface StatusResult {
167
+ service: string;
168
+ version: string;
169
+ features: Record<string, {
170
+ status: string;
171
+ description: string;
172
+ }>;
173
+ decisionTypes: Record<string, string>;
174
+ }
175
+ declare class CreedError extends Error {
176
+ code: string;
177
+ statusCode?: number | undefined;
178
+ constructor(message: string, code: string, statusCode?: number | undefined);
179
+ }
180
+ /**
181
+ * Create a Creed Space client.
182
+ *
183
+ * @param options - Client configuration
184
+ * @returns CreedClient instance
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * const client = createClient({
189
+ * apiKey: process.env.CREED_API_KEY!,
190
+ * baseUrl: 'https://api.creed.space',
191
+ * });
192
+ * ```
193
+ */
194
+ declare function createClient(options: CreedClientOptions): CreedClient;
195
+ /**
196
+ * Compute SHA-256 hash of arguments (for verification).
197
+ * Uses the same canonical JSON format as the server.
198
+ */
199
+ declare function computeArgsHash(args: Record<string, unknown>): Promise<string>;
200
+ /**
201
+ * Check if a decision token is expired (without verification).
202
+ */
203
+ declare function isTokenExpired(token: string): boolean;
204
+
205
+ export { type AllowDecision, type AuditEvent, type AuditRequest, type AuditResult, type AuthorizeRequest, type AuthorizeResult, type CreedClient, type CreedClientOptions, CreedError, type DecideRequest, type DecideResult, type DecisionType, type DenyDecision, type RequireHumanDecision, type Risk, type StatusResult, computeArgsHash, createClient, createClient as default, isTokenExpired };