@sentisec/sdk 0.1.0-rc.1
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 +70 -0
- package/LICENSE +196 -0
- package/README.md +241 -0
- package/dist/index.cjs +757 -0
- package/dist/index.d.cts +654 -0
- package/dist/index.d.ts +654 -0
- package/dist/index.js +741 -0
- package/package.json +61 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sentisec/sdk · openai.ts — structural shape of the OpenAI client (T-151).
|
|
3
|
+
*
|
|
4
|
+
* Defines the minimum surface area `wrap(...)` reads + mutates on
|
|
5
|
+
* an OpenAI-shaped client. We deliberately do NOT import the
|
|
6
|
+
* `openai` npm package here:
|
|
7
|
+
*
|
|
8
|
+
* 1. `package.json` was frozen by T-150; adding `openai` (or
|
|
9
|
+
* `openai` as a peerDependency) is out of scope for T-151.
|
|
10
|
+
* 2. The wrap signature is generic — `wrap<T>(client: T): T` —
|
|
11
|
+
* so the SDK works against any client whose surface matches
|
|
12
|
+
* this structural type, including future provider SDKs that
|
|
13
|
+
* adopt the same chat.completions.create shape.
|
|
14
|
+
* 3. Customers who wire their own `openai` instance into
|
|
15
|
+
* `monitor.wrap(openai)` get full type-preservation via the
|
|
16
|
+
* generic; they never see this `OpenAILike` surface unless
|
|
17
|
+
* they explicitly cast.
|
|
18
|
+
*
|
|
19
|
+
* If the OpenAI package's public shape ever changes in a way that
|
|
20
|
+
* breaks the fields below, the fix is on the customer side
|
|
21
|
+
* (upgrade their openai version) or in the wrap implementation
|
|
22
|
+
* (adapt to the new shape) — never in this file's structural
|
|
23
|
+
* contract, which is pinned by the T-151 acceptance tests.
|
|
24
|
+
*
|
|
25
|
+
* Reference: public Monitor surface and OpenAI adapter wave-2 notes.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Structural shape of the OpenAI Node SDK's client surface that
|
|
29
|
+
* `wrap(...)` interacts with. Only the fields the wrap layer reads
|
|
30
|
+
* or mutates are listed:
|
|
31
|
+
*
|
|
32
|
+
* - `baseURL` — read + rewritten to the Sentisec proxy URL.
|
|
33
|
+
* - `defaultHeaders` — read + extended with `x-sentisec-session-id`.
|
|
34
|
+
* - `chat.completions.create` — the call site whose Promise return
|
|
35
|
+
* is post-processed to translate proxy HALT bodies into
|
|
36
|
+
* `SentisecHijackError` (see `wrap-openai.ts`).
|
|
37
|
+
*/
|
|
38
|
+
interface OpenAILike {
|
|
39
|
+
baseURL: string;
|
|
40
|
+
defaultHeaders: Record<string, string>;
|
|
41
|
+
chat: {
|
|
42
|
+
completions: {
|
|
43
|
+
create: (params: {
|
|
44
|
+
model: string;
|
|
45
|
+
messages: {
|
|
46
|
+
role: string;
|
|
47
|
+
content: string;
|
|
48
|
+
}[];
|
|
49
|
+
}) => Promise<unknown>;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Optional per-call overrides to `wrap(...)`. The default behaviour
|
|
55
|
+
* (no second arg) rewrites the client's baseURL to
|
|
56
|
+
* `https://ingress.sentisec.ch/ws/<workspace>/v1` per the T-151
|
|
57
|
+
* acceptance contract. Customers who run an on-prem proxy or who
|
|
58
|
+
* want to point the wrap at a staging ingress override `baseUrl`
|
|
59
|
+
* explicitly — the override is a literal string, not a template.
|
|
60
|
+
*/
|
|
61
|
+
interface WrapOptions {
|
|
62
|
+
baseUrl?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The Sentisec hosted-ingress base URL template. The `{workspace}`
|
|
66
|
+
* placeholder is substituted with `Monitor.customerId` at wrap
|
|
67
|
+
* time. Phase-2 may add region-pinned ingresses
|
|
68
|
+
* (`https://ingress-eu.sentisec.ch/...`); until then, this is the
|
|
69
|
+
* single canonical hostname.
|
|
70
|
+
*
|
|
71
|
+
* A change in this constant silently re-routes every wrapped client
|
|
72
|
+
* to the wrong ingress; the T-151 test
|
|
73
|
+
* `test_wrap_rewrites_base_url` grep-guards the literal.
|
|
74
|
+
*/
|
|
75
|
+
declare const DEFAULT_INGRESS_TEMPLATE = "https://ingress.sentisec.ch/ws/{workspace}/v1";
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @sentisec/sdk · monitor.ts — Monitor.wrap dispatcher (T-151).
|
|
79
|
+
*
|
|
80
|
+
* T-150 inlined the `Monitor` class in `index.ts`. T-151 extends
|
|
81
|
+
* that same class with a `wrap(...)` method via prototype
|
|
82
|
+
* augmentation in this dedicated file so the wave-2 sibling cards
|
|
83
|
+
* (T-152 Anthropic, T-153 streaming) can each pull their own
|
|
84
|
+
* `wrap-<vendor>.ts` without touching `index.ts` or each other's
|
|
85
|
+
* wrap files.
|
|
86
|
+
*
|
|
87
|
+
* Module-graph topology (acyclic by design):
|
|
88
|
+
*
|
|
89
|
+
* index.ts ──> monitor.ts ──> wrap-openai.ts ──> errors.ts
|
|
90
|
+
* └─> wrap-anthropic.ts (T-152) ─> errors.ts
|
|
91
|
+
* └─> openai.ts
|
|
92
|
+
*
|
|
93
|
+
* monitor.ts PULLS each `*VendorEntry` value from its sibling
|
|
94
|
+
* wrap-<vendor>.ts modules and appends them to a local registry.
|
|
95
|
+
* The wrap modules do NOT import from monitor.ts — that direction
|
|
96
|
+
* would re-introduce the ESM cycle the wave-1 architecture is
|
|
97
|
+
* structured to avoid.
|
|
98
|
+
*
|
|
99
|
+
* Why prototype augmentation rather than subclassing: customers
|
|
100
|
+
* already construct `Monitor` directly per the README ("`new
|
|
101
|
+
* Monitor({...})`") and the T-150 acceptance tests
|
|
102
|
+
* (`tests/scaffold.test.ts`). Subclassing would force them to
|
|
103
|
+
* import a different name. Augmentation keeps the public surface
|
|
104
|
+
* a single class while the per-vendor adapters live in their own
|
|
105
|
+
* files.
|
|
106
|
+
*
|
|
107
|
+
* Reference: public Monitor surface and T-151 acceptance tests.
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
declare module "./index.js" {
|
|
111
|
+
interface Monitor {
|
|
112
|
+
/**
|
|
113
|
+
* Wrap a provider SDK client so every request flows through
|
|
114
|
+
* the Sentisec proxy and every HALT verdict surfaces as a
|
|
115
|
+
* `SentisecHijackError`.
|
|
116
|
+
*
|
|
117
|
+
* - The client's `baseURL` is rewritten to the Sentisec proxy
|
|
118
|
+
* URL (default `https://ingress.sentisec.ch/ws/<workspace>/v1`,
|
|
119
|
+
* overridable via `opts.baseUrl`).
|
|
120
|
+
* - The client's `defaultHeaders` carries
|
|
121
|
+
* `x-sentisec-session-id` for the proxy to correlate
|
|
122
|
+
* requests against the session.
|
|
123
|
+
*
|
|
124
|
+
* Returns the same client reference, type-preserved via the
|
|
125
|
+
* generic so customer code keeps full IDE autocomplete on the
|
|
126
|
+
* underlying SDK's surface.
|
|
127
|
+
*/
|
|
128
|
+
wrap<T>(client: T, opts?: WrapOptions): T;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @sentisec/sdk · errors.ts — TypeScript-side error surface (T-151).
|
|
134
|
+
*
|
|
135
|
+
* T-150 inlined the four base/policy/verifier/tamper exceptions in
|
|
136
|
+
* `index.ts`. T-151 adds `SentisecHijackError` (the SDK-side
|
|
137
|
+
* surface for proxy-emitted HALT verdicts) in this dedicated file
|
|
138
|
+
* so the wave-2 sibling cards (T-152 Anthropic, T-153 streaming)
|
|
139
|
+
* extend the surface in the same module instead of growing
|
|
140
|
+
* `index.ts` further.
|
|
141
|
+
*
|
|
142
|
+
* `SentisecHijackError` deliberately mirrors the Python SDK's
|
|
143
|
+
* `TaskTamperError` constructor surface (`session_id` + optional
|
|
144
|
+
* `detail`, both retained as instance attributes; formatted message
|
|
145
|
+
* stable for grep-based alert rules) and the contract that
|
|
146
|
+
* `sdk_error_surface_version:
|
|
147
|
+
* v1.0-phase1` is a cross-language contract, not a per-language
|
|
148
|
+
* one.
|
|
149
|
+
*
|
|
150
|
+
* Reference: public error surface and HIJACK / HALT verdict handling.
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* Raised when the Sentisec proxy returns a HIJACK / HALT verdict.
|
|
154
|
+
* The wrap layer translates the proxy's HALT body into
|
|
155
|
+
* this exception **before** the underlying provider SDK can throw
|
|
156
|
+
* its own HTTP-status error so customer integration code catches
|
|
157
|
+
* Sentisec's typed surface, not a vendor-specific shape.
|
|
158
|
+
*
|
|
159
|
+
* Surface mirrors the Python SDK's `TaskTamperError`:
|
|
160
|
+
* - `sessionId: str` — the aborted session id (alerting key)
|
|
161
|
+
* - `detail: str | undefined` — short structured remediation hint
|
|
162
|
+
* - `args[0]` (i.e. `.message`) — `Sentisec HALT for session
|
|
163
|
+
* {sessionId!r}.` optionally extended with `detail`.
|
|
164
|
+
*
|
|
165
|
+
* The Python equivalent lives at `platform/sentisec/sdk/errors.py`
|
|
166
|
+
* (`TaskTamperError`); both are pinned at
|
|
167
|
+
* `sdk_error_surface_version: v1.0-phase1`.
|
|
168
|
+
*/
|
|
169
|
+
declare class SentisecHijackError extends Error {
|
|
170
|
+
readonly sessionId: string;
|
|
171
|
+
readonly detail: string | undefined;
|
|
172
|
+
constructor(sessionId: string, detail?: string);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @sentisec/sdk · anthropic.ts — structural shape of the Anthropic
|
|
177
|
+
* client + vendor-specific types and the opt-in
|
|
178
|
+
* `SentisecAnthropicBlockError` (T-152).
|
|
179
|
+
*
|
|
180
|
+
* Symmetric with `openai.ts` (T-151): defines the minimum surface area
|
|
181
|
+
* `wrap(...)` reads + mutates on an Anthropic-shaped client. We
|
|
182
|
+
* deliberately do NOT import the `@anthropic-ai/sdk` npm package here:
|
|
183
|
+
*
|
|
184
|
+
* 1. `package.json` was frozen by T-150; adding `@anthropic-ai/sdk`
|
|
185
|
+
* (or it as a peerDependency) is out of scope for T-152.
|
|
186
|
+
* 2. The wrap signature is generic — `wrap<T>(client: T): T` — so
|
|
187
|
+
* the SDK works against any client whose surface matches this
|
|
188
|
+
* structural type, including future provider SDKs that adopt the
|
|
189
|
+
* same `messages.create` shape.
|
|
190
|
+
* 3. Customers who wire their own `Anthropic` instance into
|
|
191
|
+
* `monitor.wrap(anthropic)` get full type-preservation via the
|
|
192
|
+
* generic; they never see this `AnthropicLike` surface unless
|
|
193
|
+
* they explicitly cast.
|
|
194
|
+
*
|
|
195
|
+
* Vendor-specific error: `SentisecAnthropicBlockError` lives here
|
|
196
|
+
* (NOT in `errors.ts`) per the T-152 scope fence — T-151 owns
|
|
197
|
+
* `errors.ts` (`SentisecHijackError`); Anthropic-specific errors ship
|
|
198
|
+
* in vendor files so the two cards do not contend for a single file.
|
|
199
|
+
*
|
|
200
|
+
* Reference: public Monitor surface and Anthropic adapter wave-2 notes.
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* Structural shape of `@anthropic-ai/sdk`'s `Anthropic` client surface
|
|
204
|
+
* that `wrap(...)` interacts with. Only the fields the wrap layer
|
|
205
|
+
* reads or mutates are listed:
|
|
206
|
+
*
|
|
207
|
+
* - `apiKey` — preserved verbatim. The wrap layer does not
|
|
208
|
+
* strip the customer's Anthropic key; the proxy receives it and
|
|
209
|
+
* forwards (or, in fully proxied deployments, swaps for a
|
|
210
|
+
* workspace-scoped key).
|
|
211
|
+
* - `baseURL` — read + rewritten to the Sentisec proxy URL.
|
|
212
|
+
* - `defaultHeaders` — read + extended with `x-sentisec-session-id`
|
|
213
|
+
* (matches the OpenAI adapter's header name verbatim per the
|
|
214
|
+
* T-151 / T-152 acceptance contract).
|
|
215
|
+
* - `messages.create` — the call site whose Promise return is
|
|
216
|
+
* post-processed. Unlike OpenAI (which throws on HALT), the
|
|
217
|
+
* Anthropic adapter rewrites the response into a synthetic
|
|
218
|
+
* `tool_use` block carrying `sentisec_blocked: true`.
|
|
219
|
+
*/
|
|
220
|
+
interface AnthropicLike {
|
|
221
|
+
apiKey: string;
|
|
222
|
+
baseURL: string;
|
|
223
|
+
defaultHeaders: Record<string, string>;
|
|
224
|
+
messages: {
|
|
225
|
+
create: (params: AnthropicMessageCreateParams) => Promise<AnthropicMessageResponse>;
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/** Subset of `MessageCreateParams` from `@anthropic-ai/sdk` we touch. */
|
|
229
|
+
interface AnthropicMessageCreateParams {
|
|
230
|
+
model: string;
|
|
231
|
+
max_tokens: number;
|
|
232
|
+
messages: AnthropicInputMessage[];
|
|
233
|
+
[key: string]: unknown;
|
|
234
|
+
}
|
|
235
|
+
interface AnthropicInputMessage {
|
|
236
|
+
role: "user" | "assistant";
|
|
237
|
+
content: string | AnthropicContentBlock[];
|
|
238
|
+
}
|
|
239
|
+
interface AnthropicTextBlock {
|
|
240
|
+
type: "text";
|
|
241
|
+
text: string;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* `tool_use` content block per Anthropic's Messages API. The wrap
|
|
245
|
+
* layer constructs one of these on every HALT response, naming the
|
|
246
|
+
* synthetic tool `sentisec_block` and putting the proxy's verdict
|
|
247
|
+
* fields under `input`. Customer agent loops treat it like any other
|
|
248
|
+
* tool-call: the standard `stop_reason === "tool_use"` branch fires.
|
|
249
|
+
*/
|
|
250
|
+
interface AnthropicToolUseBlock {
|
|
251
|
+
type: "tool_use";
|
|
252
|
+
id: string;
|
|
253
|
+
name: string;
|
|
254
|
+
input: Record<string, unknown> & {
|
|
255
|
+
sentisec_blocked?: boolean;
|
|
256
|
+
intervention?: string;
|
|
257
|
+
forensic_id?: string;
|
|
258
|
+
row?: string;
|
|
259
|
+
decision_table_version?: string;
|
|
260
|
+
detail?: string;
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
type AnthropicContentBlock = AnthropicTextBlock | AnthropicToolUseBlock;
|
|
264
|
+
/** Subset of Anthropic's `Message` response we read or rewrite. */
|
|
265
|
+
interface AnthropicMessageResponse {
|
|
266
|
+
id: string;
|
|
267
|
+
type: "message";
|
|
268
|
+
role: "assistant";
|
|
269
|
+
model: string;
|
|
270
|
+
content: AnthropicContentBlock[];
|
|
271
|
+
stop_reason: "end_turn" | "max_tokens" | "stop_sequence" | "tool_use";
|
|
272
|
+
[key: string]: unknown;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* The shape the Sentisec proxy returns when a verdict resolves to
|
|
276
|
+
* HALT. The wrap layer detects this shape
|
|
277
|
+
* by the presence of `verdict === "HIJACK"` OR `intervention === "HALT"`
|
|
278
|
+
* — either flag alone is sufficient (the proxy emits both).
|
|
279
|
+
*
|
|
280
|
+
* Exported so consumers writing a custom on-block handler can
|
|
281
|
+
* type-narrow the verdict on their side.
|
|
282
|
+
*/
|
|
283
|
+
interface SentisecHaltEnvelope {
|
|
284
|
+
verdict?: "HIJACK" | "ABORT";
|
|
285
|
+
intervention?: "HALT" | "ABORT";
|
|
286
|
+
session_id?: string;
|
|
287
|
+
decision_table_version?: string;
|
|
288
|
+
forensic_id?: string;
|
|
289
|
+
row?: string;
|
|
290
|
+
detail?: string;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Opt-in exception for consumers who want exception semantics on HALT
|
|
294
|
+
* instead of the default gated-return shape (acceptance test 3).
|
|
295
|
+
*
|
|
296
|
+
* The wrap layer does NOT throw this by default — Anthropic's agent
|
|
297
|
+
* loop pattern is "consume the model's content blocks, dispatch
|
|
298
|
+
* tool_use blocks, append tool_result blocks, repeat." Throwing
|
|
299
|
+
* mid-loop fights that pattern. Customers who want fail-closed
|
|
300
|
+
* semantics opt in via `Monitor.wrap(client, { onBlock: "throw" })`.
|
|
301
|
+
*
|
|
302
|
+
* The asymmetry with OpenAI's `SentisecHijackError` (T-151, which
|
|
303
|
+
* throws by default) is in the cards: the OpenAI completions API has
|
|
304
|
+
* no equivalent of the Anthropic `tool_use` content-block channel, so
|
|
305
|
+
* throwing is the only structured signal an OpenAI customer can
|
|
306
|
+
* observe. Anthropic gives us a richer return shape and the card
|
|
307
|
+
* pins us to use it.
|
|
308
|
+
*
|
|
309
|
+
* Surface mirrors `SentisecHijackError` (T-151) so consumer catch
|
|
310
|
+
* sites can type-discriminate on the vendor-specific class while
|
|
311
|
+
* sharing common fields:
|
|
312
|
+
* - `sessionId` — alerting key
|
|
313
|
+
* - `forensicId` — points into the §2.10 forensic pipeline
|
|
314
|
+
* - `row` — the decision-table row that fired
|
|
315
|
+
* - `intervention` — `"HALT"` (or `"ABORT"` if Stage-3 escalates)
|
|
316
|
+
*
|
|
317
|
+
* Pinned at `sdk_error_surface_version: v1.0-phase1`. Adding new fields
|
|
318
|
+
* here is a minor bump; renaming or removing is a major bump.
|
|
319
|
+
*/
|
|
320
|
+
declare class SentisecAnthropicBlockError extends Error {
|
|
321
|
+
readonly sessionId: string;
|
|
322
|
+
readonly forensicId: string;
|
|
323
|
+
readonly row: string;
|
|
324
|
+
readonly intervention: string;
|
|
325
|
+
readonly detail: string | undefined;
|
|
326
|
+
constructor(init: {
|
|
327
|
+
sessionId: string;
|
|
328
|
+
forensicId: string;
|
|
329
|
+
row: string;
|
|
330
|
+
intervention: string;
|
|
331
|
+
detail?: string;
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @sentisec/sdk · streaming.ts — SSE streaming adapter (T-153).
|
|
337
|
+
*
|
|
338
|
+
* Implements `AsyncIterator<StreamChunk>` consuming server-sent-events
|
|
339
|
+
* from the proxy SSE endpoint and provides streaming wrappers for both
|
|
340
|
+
* OpenAI and Anthropic clients.
|
|
341
|
+
*
|
|
342
|
+
* Design constraints:
|
|
343
|
+
*
|
|
344
|
+
* 1. Browser-compatible: uses `fetch` + `ReadableStream` with no
|
|
345
|
+
* browser-incompatible APIs.
|
|
346
|
+
* 2. Node-compatible: same `fetch` path works via Node 20's built-in
|
|
347
|
+
* fetch or undici (no import needed beyond globalThis.fetch).
|
|
348
|
+
* 3. No new runtime dependencies: no `eventsource-parser`, no `msw`.
|
|
349
|
+
* Pure fetch + ReadableStream + TextDecoder.
|
|
350
|
+
* 4. AbortSignal honoured: consumer calls `AbortController.abort()`;
|
|
351
|
+
* the stream closes and emits a `session_closed` event on the
|
|
352
|
+
* last chunk.
|
|
353
|
+
*
|
|
354
|
+
* Acceptance tests (T-153):
|
|
355
|
+
* - test_openai_stream_clean_passthrough
|
|
356
|
+
* - test_openai_stream_tool_call_halted_mid_stream
|
|
357
|
+
* - test_anthropic_stream_clean_passthrough
|
|
358
|
+
* - test_anthropic_stream_tool_use_halted_mid_stream
|
|
359
|
+
* - test_abort_signal_honored
|
|
360
|
+
*
|
|
361
|
+
* Reference: public Monitor surface and T-153 acceptance tests.
|
|
362
|
+
*/
|
|
363
|
+
/**
|
|
364
|
+
* A verdict emitted by the proxy SSE endpoint.
|
|
365
|
+
* Values mirror the proxy verdict output.
|
|
366
|
+
*/
|
|
367
|
+
type VerdictValue = "PASS" | "HIJACK" | "ABORT" | "SANITIZE" | "PENDING";
|
|
368
|
+
/**
|
|
369
|
+
* Sentisec verdict metadata attached to stream chunks.
|
|
370
|
+
*/
|
|
371
|
+
interface Verdict {
|
|
372
|
+
verdict: VerdictValue;
|
|
373
|
+
session_id: string;
|
|
374
|
+
row?: string;
|
|
375
|
+
forensic_id?: string;
|
|
376
|
+
detail?: string;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* OpenAI-flavoured streaming chunk carrying an optional Sentisec
|
|
380
|
+
* verdict in the final chunk. The `sentisec` field is absent on
|
|
381
|
+
* mid-stream chunks and present on the terminal chunk.
|
|
382
|
+
*/
|
|
383
|
+
interface OpenAIStreamChunk {
|
|
384
|
+
id: string;
|
|
385
|
+
object: "chat.completion.chunk";
|
|
386
|
+
choices: Array<{
|
|
387
|
+
index: number;
|
|
388
|
+
delta: {
|
|
389
|
+
role?: string;
|
|
390
|
+
content?: string | null;
|
|
391
|
+
tool_calls?: Array<{
|
|
392
|
+
index: number;
|
|
393
|
+
id?: string;
|
|
394
|
+
type?: string;
|
|
395
|
+
function?: {
|
|
396
|
+
name?: string;
|
|
397
|
+
arguments?: string;
|
|
398
|
+
};
|
|
399
|
+
}>;
|
|
400
|
+
};
|
|
401
|
+
finish_reason: string | null;
|
|
402
|
+
}>;
|
|
403
|
+
/** Present only on the final chunk; absent mid-stream. */
|
|
404
|
+
sentisec?: Verdict;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Anthropic-flavoured streaming event. The `type` field maps to
|
|
408
|
+
* Anthropic's SSE event type names. `sentisec` is injected on the
|
|
409
|
+
* terminal `message_stop` event.
|
|
410
|
+
*/
|
|
411
|
+
interface AnthropicStreamEvent {
|
|
412
|
+
type: "message_start" | "content_block_start" | "content_block_delta" | "content_block_stop" | "message_delta" | "message_stop" | "ping" | "error";
|
|
413
|
+
/** Present only on the final message_stop event; absent mid-stream. */
|
|
414
|
+
sentisec?: Verdict;
|
|
415
|
+
/** Pass-through for all provider event fields. */
|
|
416
|
+
[key: string]: unknown;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Reason a stream was closed. `sentisec_halted` means the proxy
|
|
420
|
+
* issued a HALT verdict mid-stream; `aborted` means the consumer
|
|
421
|
+
* called `AbortController.abort()`; `done` is the clean-exit.
|
|
422
|
+
*/
|
|
423
|
+
type StreamCloseReason = "done" | "sentisec_halted" | "aborted";
|
|
424
|
+
/**
|
|
425
|
+
* Options controlling stream behaviour.
|
|
426
|
+
*/
|
|
427
|
+
interface StreamOptions {
|
|
428
|
+
/** AbortSignal for consumer-side cancellation. */
|
|
429
|
+
signal?: AbortSignal;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Shape of the OpenAI streaming client surface that
|
|
433
|
+
* `wrapOpenAIStream` reads + replaces. Mirrors the non-streaming
|
|
434
|
+
* `OpenAILike` shape but the create call must accept `stream: true`.
|
|
435
|
+
*/
|
|
436
|
+
interface OpenAIStreamingLike {
|
|
437
|
+
baseURL: string;
|
|
438
|
+
defaultHeaders: Record<string, string>;
|
|
439
|
+
chat: {
|
|
440
|
+
completions: {
|
|
441
|
+
create: (params: {
|
|
442
|
+
model: string;
|
|
443
|
+
messages: {
|
|
444
|
+
role: string;
|
|
445
|
+
content: string;
|
|
446
|
+
}[];
|
|
447
|
+
stream?: boolean;
|
|
448
|
+
[key: string]: unknown;
|
|
449
|
+
}) => Promise<unknown> | AsyncIterable<OpenAIStreamChunk>;
|
|
450
|
+
};
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Wrap an OpenAI-shaped client's `chat.completions.create` so that
|
|
455
|
+
* streaming calls (`{stream: true}`) return an `AsyncIterator` that:
|
|
456
|
+
*
|
|
457
|
+
* 1. Passes through upstream chunks unchanged.
|
|
458
|
+
* 2. Attaches `sentisec: {verdict, session_id}` metadata to the
|
|
459
|
+
* final clean-exit chunk.
|
|
460
|
+
* 3. On mid-stream HALT: yields chunks up to and including the HALT
|
|
461
|
+
* chunk with `finish_reason: "sentisec_halted"` and terminates.
|
|
462
|
+
* 4. Honours AbortSignal — yields a `session_closed` sentinel and
|
|
463
|
+
* stops iteration.
|
|
464
|
+
*
|
|
465
|
+
* Non-streaming calls (`stream` absent or `false`) are passed through
|
|
466
|
+
* to the original `create` unchanged; this wrapper is additive.
|
|
467
|
+
*/
|
|
468
|
+
declare function wrapOpenAIStream<T extends OpenAIStreamingLike>(client: T, sessionId: string, opts?: StreamOptions): T;
|
|
469
|
+
/**
|
|
470
|
+
* Anthropic-shaped streaming client surface.
|
|
471
|
+
*/
|
|
472
|
+
interface AnthropicStreamingLike {
|
|
473
|
+
baseURL: string;
|
|
474
|
+
defaultHeaders: Record<string, string>;
|
|
475
|
+
messages: {
|
|
476
|
+
create: (params: {
|
|
477
|
+
model: string;
|
|
478
|
+
max_tokens: number;
|
|
479
|
+
messages: {
|
|
480
|
+
role: string;
|
|
481
|
+
content: string | unknown[];
|
|
482
|
+
}[];
|
|
483
|
+
stream?: boolean;
|
|
484
|
+
[key: string]: unknown;
|
|
485
|
+
}) => Promise<unknown> | AsyncIterable<AnthropicStreamEvent>;
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Wrap an Anthropic-shaped client's `messages.create` so that
|
|
490
|
+
* streaming calls (`{stream: true}`) return an `AsyncIterator` that:
|
|
491
|
+
*
|
|
492
|
+
* 1. Passes through upstream events unchanged.
|
|
493
|
+
* 2. Attaches `sentisec: {verdict, session_id}` to the terminal
|
|
494
|
+
* `message_stop` event.
|
|
495
|
+
* 3. On mid-stream HALT: yields events up to and including the HALT
|
|
496
|
+
* `content_block_delta` and terminates.
|
|
497
|
+
* 4. Honours AbortSignal — yields a `message_stop` with
|
|
498
|
+
* `sentisec.verdict: "ABORT"` and stops.
|
|
499
|
+
*/
|
|
500
|
+
declare function wrapAnthropicStream<T extends AnthropicStreamingLike>(client: T, sessionId: string, opts?: StreamOptions): T;
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* @sentisec/sdk — TypeScript SDK public surface (T-150 skeleton).
|
|
504
|
+
*
|
|
505
|
+
* This file mirrors the Python SDK's public shape (see
|
|
506
|
+
* `platform/sentisec/sdk/__init__.py` + `platform/sentisec/sdk/monitor.py`
|
|
507
|
+
* + `platform/sentisec/sdk/session.py` + `platform/sentisec/sdk/errors.py`)
|
|
508
|
+
* so the wave-2 cards (T-151/T-152/T-153/T-154) can fill in the
|
|
509
|
+
* adapter bodies against a frozen surface.
|
|
510
|
+
*
|
|
511
|
+
* T-150 deliberately does NOT implement session lifecycle / wrap /
|
|
512
|
+
* task-integrity checks / forensic export — those responsibilities
|
|
513
|
+
* land with T-151 (OpenAI wrap), T-152 (Anthropic wrap), T-153
|
|
514
|
+
* (Session lifecycle parity), T-154 (error-surface parity). This
|
|
515
|
+
* file pins the types + class shape so those cards do not each
|
|
516
|
+
* re-invent it.
|
|
517
|
+
*
|
|
518
|
+
* Public SDK surface, frozen at `sdk_error_surface_version: v1.0-phase1`.
|
|
519
|
+
*/
|
|
520
|
+
/**
|
|
521
|
+
* Base class for all Sentisec SDK exceptions. Frozen at
|
|
522
|
+
* `sdk_error_surface_version: v1.0-phase1`.
|
|
523
|
+
*/
|
|
524
|
+
declare class SentisecError extends Error {
|
|
525
|
+
constructor(message?: string);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Raised when `Monitor` is constructed with a policy id that does
|
|
529
|
+
* not resolve against the built-in registry or the `.yaml` path
|
|
530
|
+
* shape.
|
|
531
|
+
*/
|
|
532
|
+
declare class UnknownPolicyError extends SentisecError {
|
|
533
|
+
readonly policyName: string;
|
|
534
|
+
constructor(policyName: string, message?: string);
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Raised when the verifier is invoked with no configured backend.
|
|
538
|
+
* The class-name suffix deliberately omits
|
|
539
|
+
* `Error` to match the Python surface (the Python class is
|
|
540
|
+
* `VerifierBackendNotConfigured`, frozen at v1.0-phase1).
|
|
541
|
+
*/
|
|
542
|
+
declare class VerifierBackendNotConfigured extends SentisecError {
|
|
543
|
+
readonly hint: string | undefined;
|
|
544
|
+
constructor(hint?: string);
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Raised when the signed user intent differs from its immutable
|
|
548
|
+
* sha256 hash mid-session.
|
|
549
|
+
*/
|
|
550
|
+
declare class TaskTamperError extends SentisecError {
|
|
551
|
+
readonly sessionId: string;
|
|
552
|
+
readonly detail: string | undefined;
|
|
553
|
+
constructor(sessionId: string, detail?: string);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* The four-tuple of gating modes used by the proxy. Changes here
|
|
557
|
+
* silently reshape enforcement on the proxy side.
|
|
558
|
+
*/
|
|
559
|
+
type ResponsePolicy = "shadow" | "gated" | "block" | "sanitize_then_resume";
|
|
560
|
+
/**
|
|
561
|
+
* Session lifecycle states. `ACTIVE` → `COMPLETED` is the clean-exit
|
|
562
|
+
* transition. `HALTED` is set by the orchestrator on a Stage-3 block.
|
|
563
|
+
* `ABORTED` is set by `_check_task_integrity()` on tamper.
|
|
564
|
+
*/
|
|
565
|
+
type SessionStatus = "ACTIVE" | "HALTED" | "COMPLETED" | "ABORTED";
|
|
566
|
+
/**
|
|
567
|
+
* The three Phase-1 built-in policy bundles. T-150 mirrors the Python
|
|
568
|
+
* registry exactly (`_BUILTIN_POLICIES` in `platform/sentisec/sdk/monitor.py`).
|
|
569
|
+
* Phase-1.5 adds the `*_enterprise.yaml` + agent-specific bundles
|
|
570
|
+
* (Track 4C, T-115..T-129) — those ship as `.yaml` paths, not new ids.
|
|
571
|
+
*/
|
|
572
|
+
declare const BUILTIN_POLICIES: readonly ["research_agent", "customer_support", "coding_agent"];
|
|
573
|
+
type BuiltinPolicy = (typeof BUILTIN_POLICIES)[number];
|
|
574
|
+
/**
|
|
575
|
+
* Developer-facing config for a `Monitor`. Mirrors the Python
|
|
576
|
+
* `Monitor` dataclass constructor in `platform/sentisec/sdk/monitor.py`.
|
|
577
|
+
*/
|
|
578
|
+
interface MonitorConfig {
|
|
579
|
+
apiKey: string;
|
|
580
|
+
model: string;
|
|
581
|
+
task: string;
|
|
582
|
+
policy?: string;
|
|
583
|
+
responsePolicy?: ResponsePolicy;
|
|
584
|
+
customerId?: string;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* A monitored session handle. T-150 pins the shape; the wave-2 cards
|
|
588
|
+
* add `recordToolCall`, `wrap`, `exportForensicBundle`, and the task-
|
|
589
|
+
* integrity check.
|
|
590
|
+
*
|
|
591
|
+
* NOT constructed directly — obtain via `monitor.session()`.
|
|
592
|
+
*/
|
|
593
|
+
declare class Session {
|
|
594
|
+
readonly sessionId: string;
|
|
595
|
+
readonly customerId: string;
|
|
596
|
+
readonly model: string;
|
|
597
|
+
readonly task: string;
|
|
598
|
+
readonly taskImmutableHash: string;
|
|
599
|
+
readonly startedAt: Date;
|
|
600
|
+
status: SessionStatus;
|
|
601
|
+
constructor(init: {
|
|
602
|
+
sessionId: string;
|
|
603
|
+
customerId: string;
|
|
604
|
+
model: string;
|
|
605
|
+
task: string;
|
|
606
|
+
taskImmutableHash: string;
|
|
607
|
+
});
|
|
608
|
+
/**
|
|
609
|
+
* Wrap a provider SDK client so every request/response is monitored.
|
|
610
|
+
*
|
|
611
|
+
* T-151 (OpenAI) and T-152 (Anthropic) fill this in. Calling it at
|
|
612
|
+
* T-150 throws so downstream code can't accidentally ship against
|
|
613
|
+
* a silently-passthrough stub.
|
|
614
|
+
*/
|
|
615
|
+
wrap<T>(_client: T): T;
|
|
616
|
+
/**
|
|
617
|
+
* Emit the hash-chained forensic bundle for this session. Lands with
|
|
618
|
+
* T-153 alongside the Python `export_forensic_bundle` parity work.
|
|
619
|
+
*/
|
|
620
|
+
exportForensicBundle(): string;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Cheap, side-effect-free (beyond policy-id validation) configuration
|
|
624
|
+
* object. Mirrors the Python `Monitor` dataclass.
|
|
625
|
+
*
|
|
626
|
+
* Construction validates the `policy` id against the built-in registry
|
|
627
|
+
* and the `.yaml`-path shape. Unknown ids throw `UnknownPolicyError`
|
|
628
|
+
* with `.policyName` populated — matches Python behaviour so the
|
|
629
|
+
* control-plane alert grouping works cross-language.
|
|
630
|
+
*
|
|
631
|
+
* Real bundle YAML loading is deferred to the policy-bundle cards
|
|
632
|
+
* (Track 3, T-070..T-072); T-150 only pins the registry gate.
|
|
633
|
+
*/
|
|
634
|
+
declare class Monitor {
|
|
635
|
+
readonly apiKey: string;
|
|
636
|
+
readonly model: string;
|
|
637
|
+
readonly task: string;
|
|
638
|
+
readonly policy: string;
|
|
639
|
+
readonly responsePolicy: ResponsePolicy;
|
|
640
|
+
readonly customerId: string;
|
|
641
|
+
constructor(config: MonitorConfig);
|
|
642
|
+
private validatePolicy;
|
|
643
|
+
/**
|
|
644
|
+
* Start a new monitored session.
|
|
645
|
+
*
|
|
646
|
+
* T-153 fills in the sha256 task-hash computation + `_checkTaskIntegrity`
|
|
647
|
+
* parity. T-150 throws a `NotImplementedError`-shaped exception so
|
|
648
|
+
* accidental usage during wave-2 development does not silently
|
|
649
|
+
* produce a session with an empty hash.
|
|
650
|
+
*/
|
|
651
|
+
session(): Session;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
export { type AnthropicContentBlock, type AnthropicInputMessage, type AnthropicLike, type AnthropicMessageCreateParams, type AnthropicMessageResponse, type AnthropicStreamEvent, type AnthropicStreamingLike, type AnthropicTextBlock, type AnthropicToolUseBlock, BUILTIN_POLICIES, type BuiltinPolicy, DEFAULT_INGRESS_TEMPLATE, Monitor, type MonitorConfig, type OpenAILike, type OpenAIStreamChunk, type OpenAIStreamingLike, type ResponsePolicy, SentisecAnthropicBlockError, SentisecError, type SentisecHaltEnvelope, SentisecHijackError, Session, type SessionStatus, type StreamCloseReason, type StreamOptions, TaskTamperError, UnknownPolicyError, type Verdict, type VerdictValue, VerifierBackendNotConfigured, type WrapOptions, Monitor as default, wrapAnthropicStream, wrapOpenAIStream };
|