@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/CHANGELOG.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Changelog — @sentisec/sdk
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are documented here.
|
|
4
|
+
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
5
|
+
Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## [0.1.0] — 2026-04-22
|
|
10
|
+
|
|
11
|
+
First public release of `@sentisec/sdk`. Ships the full Phase-1.5 TS SDK:
|
|
12
|
+
Monitor + OpenAI wrap + Anthropic wrap + SSE streaming adapter.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- **`Monitor`** — configuration object; validates policy id against built-in
|
|
17
|
+
registry (`research_agent`, `customer_support`, `coding_agent`) and `.yaml`
|
|
18
|
+
path shape. Throws `UnknownPolicyError` for unrecognised ids.
|
|
19
|
+
- **`Monitor.wrap(client)`** — polymorphic vendor-dispatch:
|
|
20
|
+
- **OpenAI** (`wrap-openai.ts`, T-151): rewrites `baseURL` to
|
|
21
|
+
`https://ingress.sentisec.ch/ws/<workspace>/v1`, injects
|
|
22
|
+
`x-sentisec-session-id` header, translates proxy HALT body to
|
|
23
|
+
`SentisecHijackError`.
|
|
24
|
+
- **Anthropic** (`wrap-anthropic.ts`, T-152): same URL/header
|
|
25
|
+
rewrite; translates HALT body to a synthetic `tool_use` content
|
|
26
|
+
block (`sentisec_block`) by default. Opt-in exception surface via
|
|
27
|
+
`{ onBlock: "throw" }` → `SentisecAnthropicBlockError`.
|
|
28
|
+
- **`wrapOpenAIStream` / `wrapAnthropicStream`** — SSE streaming adapters
|
|
29
|
+
(`streaming.ts`, T-153):
|
|
30
|
+
- Consume `fetch` + `ReadableStream`; browser-compatible and
|
|
31
|
+
Node-compatible (Node 20+ built-in fetch).
|
|
32
|
+
- `AsyncIterator<OpenAIStreamChunk>` / `AsyncIterator<AnthropicStreamEvent>`.
|
|
33
|
+
- Attaches `sentisec: {verdict, session_id}` to the final clean-exit chunk.
|
|
34
|
+
- Mid-stream HALT: yields a synthetic HALT sentinel and terminates.
|
|
35
|
+
- `AbortSignal` support: abort mid-stream yields a `session_closed`
|
|
36
|
+
sentinel and stops iteration.
|
|
37
|
+
- **Error surface** (pinned at `sdk_error_surface_version: v1.0-phase1`):
|
|
38
|
+
- `SentisecError` (base)
|
|
39
|
+
- `UnknownPolicyError`
|
|
40
|
+
- `VerifierBackendNotConfigured`
|
|
41
|
+
- `TaskTamperError`
|
|
42
|
+
- `SentisecHijackError` (OpenAI HALT)
|
|
43
|
+
- `SentisecAnthropicBlockError` (Anthropic HALT opt-in)
|
|
44
|
+
- **Type exports**: `ResponsePolicy`, `SessionStatus`, `BuiltinPolicy`,
|
|
45
|
+
`MonitorConfig`, `OpenAILike`, `AnthropicLike`, `WrapOptions`,
|
|
46
|
+
`OpenAIStreamChunk`, `AnthropicStreamEvent`, `Verdict`, `VerdictValue`,
|
|
47
|
+
`StreamOptions`, `StreamCloseReason`, and the full Anthropic
|
|
48
|
+
message-shape family.
|
|
49
|
+
- **Apache-2.0 license** (Sentisec defensive-patent posture; ADR D-007).
|
|
50
|
+
- **Dual CJS/ESM exports**: `dist/index.js` (ESM) + `dist/index.cjs` (CJS)
|
|
51
|
+
+ `dist/index.d.ts` types.
|
|
52
|
+
- **SLSA provenance attestation** via `.github/workflows/publish-sdk.yml`
|
|
53
|
+
(manual-trigger, `npm publish --provenance`).
|
|
54
|
+
|
|
55
|
+
### Implementation cards
|
|
56
|
+
|
|
57
|
+
T-150 (scaffold), T-151 (OpenAI wrap), T-152 (Anthropic wrap),
|
|
58
|
+
T-153 (streaming adapter), T-154 (publish prep + dry-run).
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## [0.1.0-rc.1] — 2026-04-22
|
|
63
|
+
|
|
64
|
+
Release candidate. Functionally identical to `0.1.0`; published to npm
|
|
65
|
+
for final integration check before the GA tag.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
*Earlier pre-release versions (`0.1.0-alpha.0`) were workspace-private and
|
|
70
|
+
never published to the npm registry.*
|
package/LICENSE
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or
|
|
82
|
+
contributory patent infringement, then any patent licenses granted to
|
|
83
|
+
You under this License for that Work shall terminate as of the date
|
|
84
|
+
such litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or as an addendum to
|
|
115
|
+
the NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the
|
|
117
|
+
License.
|
|
118
|
+
|
|
119
|
+
You may add Your own license statement for Your modifications and
|
|
120
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
121
|
+
publish, distribute, sublicense, and/or sell copies of the
|
|
122
|
+
Contribution, either on an unrestricted basis or subject only to
|
|
123
|
+
certain additional terms described in writing by the copyright owner
|
|
124
|
+
as described in Sections 4(b) and 4(d).
|
|
125
|
+
|
|
126
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
127
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
128
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
129
|
+
this License, without any additional terms or conditions.
|
|
130
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
131
|
+
the terms of any separate license agreement you may have executed
|
|
132
|
+
with Licensor regarding such Contributions.
|
|
133
|
+
|
|
134
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
135
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
136
|
+
except as required for reasonable and customary use in describing the
|
|
137
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
138
|
+
|
|
139
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
140
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
141
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
142
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
143
|
+
implied, including, without limitation, any warranties or conditions
|
|
144
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
145
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
146
|
+
appropriateness of using or reproducing the Work and assume any
|
|
147
|
+
risks associated with Your exercise of permissions under this License.
|
|
148
|
+
|
|
149
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
150
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
151
|
+
unless required by applicable law (such as deliberate and grossly
|
|
152
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
153
|
+
liable to You for damages, including any direct, indirect, special,
|
|
154
|
+
incidental, or exemplary damages of any character arising as a
|
|
155
|
+
result of this License or out of the use or inability to use the
|
|
156
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
157
|
+
work stoppage, computer failure or malfunction, or all other
|
|
158
|
+
commercial damages or losses), even if such Contributor has been
|
|
159
|
+
advised of the possibility of such damages.
|
|
160
|
+
|
|
161
|
+
9. Accepting Warranty or Liability. While redistributing the Work or
|
|
162
|
+
Derivative Works thereof, You may choose to offer, and charge a fee
|
|
163
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
164
|
+
obligations and/or rights consistent with this License. However, in
|
|
165
|
+
accepting such obligations, You may offer such conditions only on
|
|
166
|
+
Your own behalf and on Your sole responsibility, not on behalf of
|
|
167
|
+
any other Contributor, and only if You agree to indemnify, defend,
|
|
168
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
169
|
+
or claims asserted against, such Contributor by reason of your
|
|
170
|
+
accepting any warranty or additional liability.
|
|
171
|
+
|
|
172
|
+
END OF TERMS AND CONDITIONS
|
|
173
|
+
|
|
174
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
175
|
+
|
|
176
|
+
To apply the Apache License to your work, attach the following
|
|
177
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
178
|
+
replaced with your own identifying information. (Don't include
|
|
179
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
180
|
+
comment syntax for the syntax of the file format. Please also
|
|
181
|
+
include the word "NOTICE" in any file that you include in the
|
|
182
|
+
distribution alongside this License.
|
|
183
|
+
|
|
184
|
+
Copyright 2026 Sentisec
|
|
185
|
+
|
|
186
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
187
|
+
you may not use this file except in compliance with the License.
|
|
188
|
+
You may obtain a copy of the License at
|
|
189
|
+
|
|
190
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
191
|
+
|
|
192
|
+
Unless required by applicable law or agreed to in writing, software
|
|
193
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
194
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
195
|
+
See the License for the specific language governing permissions and
|
|
196
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# @sentisec/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Sentisec — cognitive integrity monitoring for autonomous
|
|
4
|
+
agents. Wraps OpenAI and Anthropic clients so every agent request flows
|
|
5
|
+
through Sentisec's staged monitoring pipeline, with streaming support via
|
|
6
|
+
SSE passthrough.
|
|
7
|
+
|
|
8
|
+
[](https://www.npmjs.com/package/@sentisec/sdk)
|
|
9
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @sentisec/sdk
|
|
17
|
+
# or
|
|
18
|
+
pnpm add @sentisec/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires Node 20+.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Quick start — OpenAI (non-streaming)
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import OpenAI from "openai";
|
|
29
|
+
import { Monitor, SentisecHijackError } from "@sentisec/sdk";
|
|
30
|
+
|
|
31
|
+
const monitor = new Monitor({
|
|
32
|
+
apiKey: process.env.SENTISEC_API_KEY!,
|
|
33
|
+
model: "gpt-4o",
|
|
34
|
+
task: "summarise today's security advisories",
|
|
35
|
+
policy: "research_agent",
|
|
36
|
+
responsePolicy: "gated",
|
|
37
|
+
customerId: "ws_acme",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Wraps the OpenAI client in place — type is preserved.
|
|
41
|
+
const openai = monitor.wrap(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const completion = await openai.chat.completions.create({
|
|
45
|
+
model: "gpt-4o",
|
|
46
|
+
messages: [{ role: "user", content: "Summarise CVE-2024-12345." }],
|
|
47
|
+
});
|
|
48
|
+
console.log(completion.choices[0]?.message.content);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
if (err instanceof SentisecHijackError) {
|
|
51
|
+
// Proxy issued a HALT verdict — the agent was attempting something
|
|
52
|
+
// outside its declared task scope.
|
|
53
|
+
console.error("HALT", err.sessionId, err.detail);
|
|
54
|
+
} else {
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Quick start — Anthropic (non-streaming)
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
66
|
+
import { Monitor, SentisecAnthropicBlockError } from "@sentisec/sdk";
|
|
67
|
+
|
|
68
|
+
const monitor = new Monitor({
|
|
69
|
+
apiKey: process.env.SENTISEC_API_KEY!,
|
|
70
|
+
model: "claude-3-5-sonnet-20241022",
|
|
71
|
+
task: "answer customer support tickets",
|
|
72
|
+
policy: "customer_support",
|
|
73
|
+
customerId: "ws_support",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const anthropic = monitor.wrap(new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }));
|
|
77
|
+
|
|
78
|
+
const message = await anthropic.messages.create({
|
|
79
|
+
model: "claude-3-5-sonnet-20241022",
|
|
80
|
+
max_tokens: 1024,
|
|
81
|
+
messages: [{ role: "user", content: "How do I reset my password?" }],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Default (gated-return): on HALT the response contains a synthetic
|
|
85
|
+
// tool_use block with sentisec_blocked: true.
|
|
86
|
+
const lastBlock = message.content[message.content.length - 1];
|
|
87
|
+
if (lastBlock?.type === "tool_use" && lastBlock.name === "sentisec_block") {
|
|
88
|
+
console.warn("Sentisec blocked this response:", lastBlock.input);
|
|
89
|
+
} else {
|
|
90
|
+
console.log(message.content);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Opt-in exception surface:
|
|
94
|
+
const anthropicThrow = monitor.wrap(
|
|
95
|
+
new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }),
|
|
96
|
+
{ onBlock: "throw" },
|
|
97
|
+
);
|
|
98
|
+
try {
|
|
99
|
+
await anthropicThrow.messages.create({ ... });
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (err instanceof SentisecAnthropicBlockError) {
|
|
102
|
+
console.error("HALT", err.sessionId, err.row, err.detail);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Streaming — OpenAI
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import OpenAI from "openai";
|
|
113
|
+
import { Monitor, wrapOpenAIStream } from "@sentisec/sdk";
|
|
114
|
+
|
|
115
|
+
const monitor = new Monitor({
|
|
116
|
+
apiKey: process.env.SENTISEC_API_KEY!,
|
|
117
|
+
model: "gpt-4o",
|
|
118
|
+
task: "code review",
|
|
119
|
+
policy: "coding_agent",
|
|
120
|
+
customerId: "ws_eng",
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const openai = monitor.wrap(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));
|
|
124
|
+
|
|
125
|
+
// Apply the streaming wrapper (separate from the non-streaming wrap).
|
|
126
|
+
const sessionId = "ses_0000000000000001"; // use your session id
|
|
127
|
+
const streamingClient = wrapOpenAIStream(openai, sessionId);
|
|
128
|
+
|
|
129
|
+
const stream = await streamingClient.chat.completions.create({
|
|
130
|
+
model: "gpt-4o",
|
|
131
|
+
messages: [{ role: "user", content: "Review this diff..." }],
|
|
132
|
+
stream: true,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
for await (const chunk of stream) {
|
|
136
|
+
const content = chunk.choices[0]?.delta.content ?? "";
|
|
137
|
+
process.stdout.write(content);
|
|
138
|
+
|
|
139
|
+
if (chunk.sentisec) {
|
|
140
|
+
// Final chunk: verdict is attached.
|
|
141
|
+
console.log("\nVerdict:", chunk.sentisec.verdict, chunk.sentisec.session_id);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
AbortSignal support:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
const controller = new AbortController();
|
|
150
|
+
setTimeout(() => controller.abort(), 5000); // 5 s timeout
|
|
151
|
+
|
|
152
|
+
const streamingClient = wrapOpenAIStream(openai, sessionId, {
|
|
153
|
+
signal: controller.signal,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
for await (const chunk of await streamingClient.chat.completions.create({ ... })) {
|
|
157
|
+
// On abort, last chunk has finish_reason: "session_closed"
|
|
158
|
+
// and chunk.sentisec.verdict === "ABORT".
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Streaming — Anthropic
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
168
|
+
import { Monitor, wrapAnthropicStream } from "@sentisec/sdk";
|
|
169
|
+
|
|
170
|
+
const monitor = new Monitor({ ... });
|
|
171
|
+
const anthropic = monitor.wrap(new Anthropic({ ... }));
|
|
172
|
+
const streamingClient = wrapAnthropicStream(anthropic, sessionId);
|
|
173
|
+
|
|
174
|
+
const stream = await streamingClient.messages.create({
|
|
175
|
+
model: "claude-3-5-sonnet-20241022",
|
|
176
|
+
max_tokens: 1024,
|
|
177
|
+
messages: [{ role: "user", content: "..." }],
|
|
178
|
+
stream: true,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
for await (const event of stream) {
|
|
182
|
+
if (event.type === "content_block_delta") {
|
|
183
|
+
const delta = event.delta as { type: string; text?: string };
|
|
184
|
+
if (delta.type === "text_delta") process.stdout.write(delta.text ?? "");
|
|
185
|
+
if (delta.type === "sentisec_halted") {
|
|
186
|
+
console.warn("Mid-stream HALT:", delta);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (event.type === "message_stop" && event.sentisec) {
|
|
190
|
+
console.log("Verdict:", event.sentisec.verdict);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Policies
|
|
198
|
+
|
|
199
|
+
Three built-in policy bundles ship with Phase 1:
|
|
200
|
+
|
|
201
|
+
| ID | Use case |
|
|
202
|
+
|---|---|
|
|
203
|
+
| `research_agent` | Web-access agents doing research and summarisation |
|
|
204
|
+
| `customer_support` | Support-ticket agents with constrained tool access |
|
|
205
|
+
| `coding_agent` | Code-review and generation agents |
|
|
206
|
+
|
|
207
|
+
Custom policies:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
const monitor = new Monitor({
|
|
211
|
+
// ...
|
|
212
|
+
policy: "./policies/my-custom-policy.yaml",
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Python parity
|
|
219
|
+
|
|
220
|
+
Every public symbol in this package has a 1:1 counterpart in
|
|
221
|
+
`platform/sentisec/sdk/`. The error-surface is a cross-language contract
|
|
222
|
+
pinned at `sdk_error_surface_version: v1.0-phase1`.
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## Develop
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
pnpm install # from repo root
|
|
230
|
+
pnpm --filter @sentisec/sdk build # tsup → dist/
|
|
231
|
+
pnpm --filter @sentisec/sdk test # vitest (23 tests)
|
|
232
|
+
pnpm --filter @sentisec/sdk typecheck
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Node 20+ and pnpm 9+ required.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## License
|
|
240
|
+
|
|
241
|
+
Apache-2.0. See [LICENSE](./LICENSE).
|