@writ-agent/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/LICENSE +202 -0
- package/README.md +182 -0
- package/dist/cjs/claude-agent-sdk.d.ts +90 -0
- package/dist/cjs/claude-agent-sdk.js +312 -0
- package/dist/cjs/client.d.ts +107 -0
- package/dist/cjs/client.js +387 -0
- package/dist/cjs/errors.d.ts +35 -0
- package/dist/cjs/errors.js +60 -0
- package/dist/cjs/guard.d.ts +56 -0
- package/dist/cjs/guard.js +113 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/locate.d.ts +18 -0
- package/dist/cjs/locate.js +64 -0
- package/dist/cjs/output.d.ts +10 -0
- package/dist/cjs/output.js +38 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/protocol.d.ts +67 -0
- package/dist/cjs/protocol.js +9 -0
- package/dist/esm/claude-agent-sdk.d.ts +90 -0
- package/dist/esm/claude-agent-sdk.js +306 -0
- package/dist/esm/client.d.ts +107 -0
- package/dist/esm/client.js +382 -0
- package/dist/esm/errors.d.ts +35 -0
- package/dist/esm/errors.js +51 -0
- package/dist/esm/guard.d.ts +56 -0
- package/dist/esm/guard.js +109 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/locate.d.ts +18 -0
- package/dist/esm/locate.js +59 -0
- package/dist/esm/output.d.ts +10 -0
- package/dist/esm/output.js +33 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/protocol.d.ts +67 -0
- package/dist/esm/protocol.js +6 -0
- package/package.json +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# @writ-agent/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript / Node client for the [writ](https://github.com/writ-agent/writ/blob/main/README.md) hook gateway. Before an
|
|
4
|
+
agent's tool call runs, writ checks it against `writ.yaml` (allow / deny / ask /
|
|
5
|
+
redact) and records it to writ's hash-chained, tamper-evident ledger.
|
|
6
|
+
|
|
7
|
+
This package talks to writ over its gateway protocol (`writ check --stdio`,
|
|
8
|
+
[INTERFACES.md Contract 6](https://github.com/writ-agent/writ/blob/main/docs/INTERFACES.md)). It spawns one long-lived
|
|
9
|
+
`writ check` child process per `WritClient`, so there is no daemon to run.
|
|
10
|
+
|
|
11
|
+
- ESM and CommonJS builds, TypeScript types, Node >= 18, no runtime dependencies.
|
|
12
|
+
- `guard()` / `guardTools()`: wrap any tool function or `{ description, parameters, execute }` tool object.
|
|
13
|
+
- `@writ-agent/sdk/claude-agent-sdk`: `hooks` and `canUseTool` for the Claude Agent SDK.
|
|
14
|
+
- **Fails closed.** A missing binary, gateway crash, timeout, malformed line or
|
|
15
|
+
`error` response rejects with a `WritError`, and the tool does not run.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
> **You also need the `writ` binary.** Until prebuilt releases ship, install it
|
|
20
|
+
> from source: `cargo install --git https://github.com/writ-agent/writ writ-cli`
|
|
21
|
+
> (Rust stable), then check with `writ --version`.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @writ-agent/sdk
|
|
25
|
+
# optional, for the Claude Agent SDK integration
|
|
26
|
+
npm install @anthropic-ai/claude-agent-sdk
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You also need the `writ` binary. The client looks for it in this order:
|
|
30
|
+
|
|
31
|
+
1. the `bin` option (a path; `.js`/`.mjs`/`.cjs` paths run under Node),
|
|
32
|
+
2. the `WRIT_BIN` environment variable,
|
|
33
|
+
3. `writ` on `PATH` (`writ.exe` / `writ.com` on Windows; `.cmd` shims are not accepted).
|
|
34
|
+
|
|
35
|
+
Or pass `command` / `args` to spawn something else entirely (a wrapper, a test gateway).
|
|
36
|
+
|
|
37
|
+
## Wrap a tool
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { WritBlockedError, WritClient, guard } from "@writ-agent/sdk";
|
|
41
|
+
|
|
42
|
+
const writ = new WritClient({ policy: "writ.yaml", caller: { agent: "my-agent" } });
|
|
43
|
+
|
|
44
|
+
const runQuery = guard(async (input: { query: string }) => db.query(input.query), {
|
|
45
|
+
client: writ,
|
|
46
|
+
tool: "postgres.query", // the name your writ.yaml rules match on
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const rows = await runQuery({ query: "select email from users" });
|
|
51
|
+
// With a redact rule, `rows` is writ's redacted output, not the raw result.
|
|
52
|
+
} catch (err) {
|
|
53
|
+
if (err instanceof WritBlockedError) console.error(err.message); // names rule_id and writ.yaml:LINE
|
|
54
|
+
else throw err;
|
|
55
|
+
} finally {
|
|
56
|
+
await writ.close(); // or: await using writ = new WritClient(...)
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`guard` runs **decide, then (for a deferred ask) your approver, then the tool,
|
|
61
|
+
then complete**:
|
|
62
|
+
|
|
63
|
+
| writ verdict | what `guard` does |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| allow | runs the tool, records it, returns the result |
|
|
66
|
+
| deny | throws `WritBlockedError`; the tool never runs |
|
|
67
|
+
| ask (`ask: "deny"`, default) | throws `WritBlockedError` |
|
|
68
|
+
| ask (`ask: "defer"`) | calls `approver`; only an explicit `true` / `{ approved: true }` runs the tool. No approver, a throw, or no answer within the ask's `timeout_ms` is a rejection, and the rejection is sent to writ (`resolve`) |
|
|
69
|
+
| redact | runs the tool, sends the output to writ, returns writ's redacted output (structured results are re-parsed from the redacted JSON) |
|
|
70
|
+
|
|
71
|
+
If the gateway fails after the tool ran (the `complete` call), `guard` throws
|
|
72
|
+
instead of returning the result, so an unrecorded or unredacted result never
|
|
73
|
+
reaches the model.
|
|
74
|
+
|
|
75
|
+
### Tool objects (Vercel AI SDK and similar)
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { guardTools } from "@writ-agent/sdk";
|
|
79
|
+
|
|
80
|
+
const tools = guardTools(myTools, { client: writ, toolName: (key) => key });
|
|
81
|
+
// each tool's execute is wrapped; the AI SDK's toolCallId becomes writ's call_id
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Claude Agent SDK
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
88
|
+
import { WritClient } from "@writ-agent/sdk";
|
|
89
|
+
import { createWritIntegration } from "@writ-agent/sdk/claude-agent-sdk";
|
|
90
|
+
|
|
91
|
+
await using writ = new WritClient({ ask: "defer" });
|
|
92
|
+
const { hooks, canUseTool } = createWritIntegration({
|
|
93
|
+
client: writ,
|
|
94
|
+
approver: async ({ call, decision }) => askAHuman(call, decision), // optional
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
for await (const msg of query({ prompt: "...", options: { hooks, canUseTool } })) {
|
|
98
|
+
// ...
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
What the integration registers (checked against `@anthropic-ai/claude-agent-sdk` 0.3.280 types):
|
|
103
|
+
|
|
104
|
+
- **`PreToolUse`** sends `decide` and returns
|
|
105
|
+
`hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision, permissionDecisionReason }`:
|
|
106
|
+
`allow` for allow / redact, `deny` for deny (the reason names the rule and
|
|
107
|
+
`writ.yaml:LINE`). Any writ failure also returns `deny`: the hook never throws.
|
|
108
|
+
- **Deferred asks** (`ask: "defer"`): with `approver`, the hook asks it inline
|
|
109
|
+
and returns `allow` or `deny`. Without `approver` but with your own
|
|
110
|
+
`canUseTool`, the hook returns `permissionDecision: "ask"`; the SDK then calls
|
|
111
|
+
the returned `canUseTool`, which asks yours and sends the answer to writ.
|
|
112
|
+
An approval that edits the tool input is treated as a rejection, and
|
|
113
|
+
`updatedPermissions` are dropped so an SDK "always allow" rule cannot
|
|
114
|
+
bypass later writ asks. With neither, deferred asks are denied.
|
|
115
|
+
- **`PostToolUse`** sends `complete` with the tool response. For redact it
|
|
116
|
+
returns `hookSpecificOutput: { hookEventName: "PostToolUse", updatedToolOutput }`
|
|
117
|
+
with writ's redacted output. If redaction cannot be applied the output is
|
|
118
|
+
replaced with a withheld notice.
|
|
119
|
+
- **`PostToolUseFailure`** records the failed execution (`ok: false`).
|
|
120
|
+
|
|
121
|
+
Use `mergeHooks(hooks, yourHooks)` to combine with your own hooks, and
|
|
122
|
+
`onAllow: "passthrough"` if the SDK's own permission rules should still apply
|
|
123
|
+
after writ allows a call.
|
|
124
|
+
|
|
125
|
+
Tool names and inputs are mapped to writ's policy vocabulary, the same way as
|
|
126
|
+
`writ check --format claude-code`:
|
|
127
|
+
|
|
128
|
+
| Claude tool | writ `tool` | policy fields |
|
|
129
|
+
| --- | --- | --- |
|
|
130
|
+
| `Bash`, `PowerShell` | `bash` | `command` |
|
|
131
|
+
| `Read`, `Glob`, `Grep`, `LS`, `NotebookRead` | `fs.read` | `path` (kept, or from `file_path`) |
|
|
132
|
+
| `Write`, `Edit`, `MultiEdit`, `NotebookEdit` | `fs.write` | `path` (from `file_path` / `notebook_path`) |
|
|
133
|
+
| `WebFetch` | `http` | `url` (so `url.host`) |
|
|
134
|
+
| `WebSearch` | `web.search` | `query` |
|
|
135
|
+
| `mcp__<server>__<tool>` | `<tool>` | `server` = `<server>` (`mcp_server.name` if sent) |
|
|
136
|
+
| anything else | unchanged | |
|
|
137
|
+
|
|
138
|
+
The original input fields are kept alongside the normalized ones. For
|
|
139
|
+
subagent calls the SDK's `agent_id` becomes `caller.non_human_id`. Pass
|
|
140
|
+
`mapTool` to change the mapping.
|
|
141
|
+
|
|
142
|
+
## Client API
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const writ = new WritClient({
|
|
146
|
+
policy, ledger, // forwarded as --policy / --ledger
|
|
147
|
+
ask: "deny", // or "defer"
|
|
148
|
+
timeoutMs: 30_000, // per request; a timeout kills the gateway (fail closed)
|
|
149
|
+
caller, sessionId, // defaults for calls that do not set them
|
|
150
|
+
respawn: true, // start a fresh gateway after a crash (in-flight calls still fail)
|
|
151
|
+
bin, command, args, cwd, env, onStderr,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
await writ.decide(call); // -> Decision
|
|
155
|
+
await writ.resolve(ref, approved, who); // -> final Decision
|
|
156
|
+
await writ.complete(ref, { ok, exit, output }); // -> { recorded, output? }
|
|
157
|
+
await writ.authorize(call, { approver }); // decide + approver + resolve
|
|
158
|
+
await writ.close();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Only `shouldDispatch(decision)` (`dispatch === true` and not `deny`) means the
|
|
162
|
+
tool may run. Responses that contradict themselves (for example `deny` with
|
|
163
|
+
`dispatch: true`, or a dispatching decision without `ref`) are rejected as
|
|
164
|
+
`WritProtocolError`.
|
|
165
|
+
|
|
166
|
+
Never put credentials in tool `args`: args are recorded in the ledger and
|
|
167
|
+
visible to policy.
|
|
168
|
+
|
|
169
|
+
## Development
|
|
170
|
+
|
|
171
|
+
```sh
|
|
172
|
+
npm install
|
|
173
|
+
npm run build # dist/esm + dist/cjs
|
|
174
|
+
npm run typecheck
|
|
175
|
+
npm run lint
|
|
176
|
+
npm test # unit tests against a fake gateway (test/fixtures/fake-writ.mjs)
|
|
177
|
+
WRIT_E2E=1 WRIT_BIN=/path/to/writ npm test # also runs the e2e suite against the real binary
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
Apache-2.0
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* writ integration for the Claude Agent SDK (`@anthropic-ai/claude-agent-sdk`).
|
|
3
|
+
*
|
|
4
|
+
* Plugs into `query({ options: { hooks, canUseTool } })`:
|
|
5
|
+
* - `PreToolUse` asks writ for a verdict and returns the SDK's
|
|
6
|
+
* `permissionDecision` (`allow` / `deny` / `ask`).
|
|
7
|
+
* - `PostToolUse` / `PostToolUseFailure` record the execution (`complete`)
|
|
8
|
+
* and, for a redact verdict, replace the tool output via `updatedToolOutput`.
|
|
9
|
+
* - `canUseTool` resolves writ asks that were routed to the SDK's own
|
|
10
|
+
* permission flow.
|
|
11
|
+
*
|
|
12
|
+
* Only SDK *types* are imported; this module has no runtime dependency on the SDK.
|
|
13
|
+
*/
|
|
14
|
+
import type { CanUseTool, HookCallback, HookCallbackMatcher, HookEvent } from "@anthropic-ai/claude-agent-sdk";
|
|
15
|
+
import { type Approver, type WritClient } from "./client.js";
|
|
16
|
+
import type { CallerIdentity, ServerIdentity } from "./protocol.js";
|
|
17
|
+
/** A Claude tool use normalized to writ's vocabulary. */
|
|
18
|
+
export interface MappedToolCall {
|
|
19
|
+
tool: string;
|
|
20
|
+
args: Record<string, unknown>;
|
|
21
|
+
server?: ServerIdentity | null;
|
|
22
|
+
}
|
|
23
|
+
/** MCP server provenance as the SDK reports it (`mcp_server` / `mcpServer`). */
|
|
24
|
+
export interface McpProvenance {
|
|
25
|
+
name: string;
|
|
26
|
+
source: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Map a Claude tool name + input to writ's policy vocabulary. Kept identical
|
|
30
|
+
* to `writ check --format claude-code` (crates/writ-cli/src/hook.rs):
|
|
31
|
+
* - `Bash` / `PowerShell` → `bash` (with `command`)
|
|
32
|
+
* - `Read` / `Glob` / `Grep` / `LS` / `NotebookRead` → `fs.read`; `Write` / `Edit` / `MultiEdit` / `NotebookEdit` → `fs.write` (with `path`)
|
|
33
|
+
* - `WebFetch` → `http` (with `url`); `WebSearch` → `web.search` (with `query`)
|
|
34
|
+
* - `mcp__<server>__<tool>` → tool `<tool>` with `server: { name: <server> }`
|
|
35
|
+
* - anything else keeps its SDK name.
|
|
36
|
+
* The original input fields are kept; normalized keys are added alongside.
|
|
37
|
+
*/
|
|
38
|
+
export declare function mapClaudeTool(toolName: string, input: unknown, mcpServer?: McpProvenance): MappedToolCall;
|
|
39
|
+
export interface WritClaudeOptions {
|
|
40
|
+
client: WritClient;
|
|
41
|
+
/**
|
|
42
|
+
* Decides deferred asks inline, inside the PreToolUse hook (client must use
|
|
43
|
+
* `ask: "defer"`). Takes precedence over `canUseTool`.
|
|
44
|
+
*/
|
|
45
|
+
approver?: Approver;
|
|
46
|
+
/**
|
|
47
|
+
* Your own SDK permission callback. With `ask: "defer"` and no `approver`,
|
|
48
|
+
* writ asks are handed to the SDK's permission flow (`permissionDecision:
|
|
49
|
+
* "ask"`) and this callback decides them; its answer is sent to writ as
|
|
50
|
+
* `resolve`. It is also consulted for SDK permission prompts that writ did
|
|
51
|
+
* not raise. Without it, those are denied.
|
|
52
|
+
*/
|
|
53
|
+
canUseTool?: CanUseTool;
|
|
54
|
+
/** Default `{ agent: "claude-agent-sdk" }`. */
|
|
55
|
+
caller?: CallerIdentity;
|
|
56
|
+
/** Override the tool mapping (default `mapClaudeTool`). */
|
|
57
|
+
mapTool?: (toolName: string, input: unknown, mcpServer?: McpProvenance) => MappedToolCall;
|
|
58
|
+
/** Hook matcher (tool name regex); default: every tool. */
|
|
59
|
+
matcher?: string;
|
|
60
|
+
/** SDK hook timeout in seconds (covers inline approvals). */
|
|
61
|
+
hookTimeoutSec?: number;
|
|
62
|
+
approvalTimeoutMs?: number;
|
|
63
|
+
/**
|
|
64
|
+
* What an allow / redact verdict returns to the SDK: `"allow"` (default, as
|
|
65
|
+
* `writ check --format claude-code`) skips the SDK's own permission prompt;
|
|
66
|
+
* `"passthrough"` returns no decision, so the SDK's permission rules still
|
|
67
|
+
* apply on top of writ.
|
|
68
|
+
*/
|
|
69
|
+
onAllow?: "allow" | "passthrough";
|
|
70
|
+
}
|
|
71
|
+
/** The pieces to spread into the SDK's `query({ options })`. */
|
|
72
|
+
export interface WritClaudeIntegration {
|
|
73
|
+
hooks: Partial<Record<HookEvent, HookCallbackMatcher[]>>;
|
|
74
|
+
canUseTool: CanUseTool;
|
|
75
|
+
preToolUse: HookCallback;
|
|
76
|
+
postToolUse: HookCallback;
|
|
77
|
+
postToolUseFailure: HookCallback;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Build writ hooks and a `canUseTool` callback for the Claude Agent SDK.
|
|
81
|
+
* Every failure path denies the tool (fail closed): a hook never throws.
|
|
82
|
+
*/
|
|
83
|
+
export declare function createWritIntegration(options: WritClaudeOptions): WritClaudeIntegration;
|
|
84
|
+
/** Just the `hooks` option. Deferred asks need `approver` (or use `createWritIntegration` for `canUseTool`). */
|
|
85
|
+
export declare function writHooks(options: WritClaudeOptions): Partial<Record<HookEvent, HookCallbackMatcher[]>>;
|
|
86
|
+
/**
|
|
87
|
+
* Merge writ's hooks with your own `hooks` option. writ's matchers come first
|
|
88
|
+
* for each event; yours are kept.
|
|
89
|
+
*/
|
|
90
|
+
export declare function mergeHooks(...sets: Array<Partial<Record<HookEvent, HookCallbackMatcher[]>> | undefined>): Partial<Record<HookEvent, HookCallbackMatcher[]>>;
|