@oked/claude-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 +21 -0
- package/README.md +96 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +125 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OKed
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @oked/claude-agent-sdk
|
|
2
|
+
|
|
3
|
+
OKed for the [Claude Agent SDK](https://code.claude.com/docs/en/agent-sdk).
|
|
4
|
+
A ready-made `PreToolUse` hook callback — sensitive tool calls freeze the
|
|
5
|
+
agent and wait for a human approval (push to your phone) before running.
|
|
6
|
+
|
|
7
|
+
> Building with the **Claude Code CLI** instead? Use
|
|
8
|
+
> [`@oked/claude-code`](../claude-code) (`oked init`, zero code).
|
|
9
|
+
|
|
10
|
+
## Why this exists
|
|
11
|
+
|
|
12
|
+
The Claude Agent SDK does **not** read `.claude/settings.json` hooks by
|
|
13
|
+
default — hooks are passed programmatically via `options.hooks`. So unlike
|
|
14
|
+
the Claude Code CLI there's no zero-code install: you wire OKed's hook into
|
|
15
|
+
your agent's options yourself. (If you *do* load project settings via
|
|
16
|
+
`settingSources: ["project"]`, the `@oked/claude-code` hook would also fire —
|
|
17
|
+
this package is for the common programmatic case.)
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @oked/claude-agent-sdk
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`@anthropic-ai/claude-agent-sdk` is an optional peer dependency — you already
|
|
26
|
+
have it if you're building an agent.
|
|
27
|
+
|
|
28
|
+
## Use
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
32
|
+
import { okedHooks } from "@oked/claude-agent-sdk";
|
|
33
|
+
|
|
34
|
+
for await (const message of query({
|
|
35
|
+
prompt: "…",
|
|
36
|
+
options: { hooks: okedHooks() },
|
|
37
|
+
})) {
|
|
38
|
+
// …
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or wire the callback yourself for full control over matchers/timeout:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { okedPreToolUseHook } from "@oked/claude-agent-sdk";
|
|
46
|
+
|
|
47
|
+
const options = {
|
|
48
|
+
hooks: {
|
|
49
|
+
PreToolUse: [{ hooks: [okedPreToolUseHook] }],
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Auth
|
|
55
|
+
|
|
56
|
+
Bring your own API key (this package has no pairing CLI):
|
|
57
|
+
|
|
58
|
+
- `OKED_API_KEY=ok_…` environment variable, or
|
|
59
|
+
- `~/.oked/config.json` (`{ "apiKey": "ok_…" }`)
|
|
60
|
+
|
|
61
|
+
Create a key from the OKed dashboard. Optionally set `OKED_BACKEND_URL` to
|
|
62
|
+
target a non-default backend.
|
|
63
|
+
|
|
64
|
+
## Behavior
|
|
65
|
+
|
|
66
|
+
Same tier model and fail-safe semantics as the other OKed integrations:
|
|
67
|
+
|
|
68
|
+
| Tier | What happens |
|
|
69
|
+
|------|--------------|
|
|
70
|
+
| `safe` | Allow immediately, no network call |
|
|
71
|
+
| `warning` | Allow, log to stderr only, no network call |
|
|
72
|
+
| `review` / `high_stakes` | Request approval; **Approve** → run, otherwise the tool call is denied with a reason |
|
|
73
|
+
|
|
74
|
+
Failure handling: missing API key → defer to the agent's built-in
|
|
75
|
+
permission flow (`ask`); invalid key → deny; backend unreachable → degrade
|
|
76
|
+
per tier (`degradedDecision` — high-stakes / strict fail-closed deny, others
|
|
77
|
+
may proceed); any unexpected error → deny. Set `OKED_STRICT_FAIL_CLOSED=1`
|
|
78
|
+
to deny every sensitive action during backend outages.
|
|
79
|
+
|
|
80
|
+
## Attribution
|
|
81
|
+
|
|
82
|
+
Approvals are attributed by the **API key's pairing record** — not a
|
|
83
|
+
per-request field. Pair a device with `client_type: "claude-agent-sdk"`
|
|
84
|
+
and that clientType plus the host/device name are bonded to the issued API
|
|
85
|
+
key (`device_codes` ↔ `api_keys`). Every approval made with that key is
|
|
86
|
+
then attributable to `claude-agent-sdk` in the dashboard, via the same
|
|
87
|
+
mechanism used for the other integrations.
|
|
88
|
+
|
|
89
|
+
This package ships no pairing CLI (callback-only, BYO key), so obtain a
|
|
90
|
+
`claude-agent-sdk`-typed key through your pairing flow. A key minted from
|
|
91
|
+
the dashboard "create key" UI has no device record and falls back to the
|
|
92
|
+
generic `sdk` bucket.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { HookCallback } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* A `PreToolUse` hook callback for the Claude Agent SDK. Pass it via
|
|
4
|
+
* `options.hooks` (or use {@link okedHooks}).
|
|
5
|
+
*/
|
|
6
|
+
export declare const okedPreToolUseHook: HookCallback;
|
|
7
|
+
/**
|
|
8
|
+
* Convenience: spread into the Claude Agent SDK `options.hooks`.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
12
|
+
* import { okedHooks } from "@oked/claude-agent-sdk";
|
|
13
|
+
*
|
|
14
|
+
* for await (const m of query({ prompt, options: { hooks: okedHooks() } })) { }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function okedHooks(): {
|
|
18
|
+
PreToolUse: {
|
|
19
|
+
hooks: HookCallback[];
|
|
20
|
+
}[];
|
|
21
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @oked/claude-agent-sdk — OKed for the Claude Agent SDK.
|
|
3
|
+
*
|
|
4
|
+
* Exports a ready-made `PreToolUse` hook callback. Wire it into your agent's
|
|
5
|
+
* `options.hooks` and every tool call the model makes is classified; calls
|
|
6
|
+
* classified as sensitive (review / high_stakes) freeze the agent and wait
|
|
7
|
+
* for a human approval via the OKed backend (push to your phone). The tool
|
|
8
|
+
* only runs on Approve.
|
|
9
|
+
*
|
|
10
|
+
* Unlike `@oked/claude-code` there is no `.claude/settings.json` install —
|
|
11
|
+
* the Claude Agent SDK takes hooks programmatically. Bring your own API key
|
|
12
|
+
* via `OKED_API_KEY` (or `~/.oked/config.json`); see README.
|
|
13
|
+
*
|
|
14
|
+
* Failure semantics mirror the other OKed integrations: fail safe. Auth
|
|
15
|
+
* errors deny; backend-unreachable degrades per tier (`degradedDecision`);
|
|
16
|
+
* any unexpected error denies. Never let an agent proceed when in doubt.
|
|
17
|
+
*/
|
|
18
|
+
import { OKedClient, classify, describe, describeFields, degradedDecision, OKedAuthError, OKedBackendUnreachableError, } from "@oked/sdk";
|
|
19
|
+
function log(msg) {
|
|
20
|
+
process.stderr.write(`[OKed] ${msg}\n`);
|
|
21
|
+
}
|
|
22
|
+
const ALLOW = {};
|
|
23
|
+
function deny(reason) {
|
|
24
|
+
return {
|
|
25
|
+
systemMessage: reason,
|
|
26
|
+
hookSpecificOutput: {
|
|
27
|
+
hookEventName: "PreToolUse",
|
|
28
|
+
permissionDecision: "deny",
|
|
29
|
+
permissionDecisionReason: reason,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function ask() {
|
|
34
|
+
return {
|
|
35
|
+
hookSpecificOutput: {
|
|
36
|
+
hookEventName: "PreToolUse",
|
|
37
|
+
permissionDecision: "ask",
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A `PreToolUse` hook callback for the Claude Agent SDK. Pass it via
|
|
43
|
+
* `options.hooks` (or use {@link okedHooks}).
|
|
44
|
+
*/
|
|
45
|
+
export const okedPreToolUseHook = async (rawInput) => {
|
|
46
|
+
const input = rawInput;
|
|
47
|
+
const toolName = input.tool_name;
|
|
48
|
+
const toolInput = (input.tool_input ?? {});
|
|
49
|
+
const tier = classify(toolName, toolInput);
|
|
50
|
+
const client = new OKedClient();
|
|
51
|
+
// Presence ping (throttled to once/day on disk, never throws). Fired before
|
|
52
|
+
// the safe/warning return so installs that only run safe actions still
|
|
53
|
+
// register for retention. Fire-and-forget: this is a long-lived process, so
|
|
54
|
+
// we never add latency to the tool call.
|
|
55
|
+
if (client.apiKey)
|
|
56
|
+
void client.heartbeat().catch(() => { });
|
|
57
|
+
// Safe — allow immediately, no network call.
|
|
58
|
+
if (tier === "safe")
|
|
59
|
+
return ALLOW;
|
|
60
|
+
// Warning — allow, log only, no network call (mirrors the Claude Code hook).
|
|
61
|
+
if (tier === "warning") {
|
|
62
|
+
const summary = (toolInput.file_path ?? toolInput.command ?? toolName);
|
|
63
|
+
log(`WARNING ${toolName} ${summary} - allowed (inside project)`);
|
|
64
|
+
return ALLOW;
|
|
65
|
+
}
|
|
66
|
+
if (!client.apiKey) {
|
|
67
|
+
log("OKED_API_KEY not set - deferring to the agent's built-in permission flow.");
|
|
68
|
+
return ask();
|
|
69
|
+
}
|
|
70
|
+
const description = describe(toolName, toolInput);
|
|
71
|
+
const fields = describeFields(toolName, toolInput) ?? undefined;
|
|
72
|
+
log(`${toolName}: "${description}" - ${tier}`);
|
|
73
|
+
log("Requesting approval... (check your phone)");
|
|
74
|
+
try {
|
|
75
|
+
const result = await client.approve({
|
|
76
|
+
action: toolName,
|
|
77
|
+
description,
|
|
78
|
+
tier,
|
|
79
|
+
fields,
|
|
80
|
+
tool_input: toolInput,
|
|
81
|
+
session_id: input.session_id,
|
|
82
|
+
cwd: input.cwd,
|
|
83
|
+
});
|
|
84
|
+
if (result.approved) {
|
|
85
|
+
log(`Approved (${result.approval_id})`);
|
|
86
|
+
return ALLOW;
|
|
87
|
+
}
|
|
88
|
+
log(`Denied (${result.approval_id})`);
|
|
89
|
+
return deny(`Denied via OKed: ${description}`);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
if (err instanceof OKedAuthError) {
|
|
93
|
+
// Auth misconfig is not an outage — always deny.
|
|
94
|
+
log("Invalid API key - action denied");
|
|
95
|
+
return deny("OKed: invalid API key");
|
|
96
|
+
}
|
|
97
|
+
if (err instanceof OKedBackendUnreachableError) {
|
|
98
|
+
const decision = degradedDecision(tier, {
|
|
99
|
+
strictFailClosed: client.strictFailClosed,
|
|
100
|
+
});
|
|
101
|
+
if (decision === "allow") {
|
|
102
|
+
log(`Backend unreachable - allowed (degraded; ${tier}, non-high-stakes)`);
|
|
103
|
+
return ALLOW;
|
|
104
|
+
}
|
|
105
|
+
const why = client.strictFailClosed ? "strict fail-closed" : "high-stakes";
|
|
106
|
+
log(`Backend unreachable - ${why} denied (fail-safe)`);
|
|
107
|
+
return deny(`OKed backend unreachable - ${why} denied (fail-safe)`);
|
|
108
|
+
}
|
|
109
|
+
log("Unexpected error - action denied (fail-safe)");
|
|
110
|
+
return deny("OKed: unexpected error (fail-safe)");
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Convenience: spread into the Claude Agent SDK `options.hooks`.
|
|
115
|
+
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
118
|
+
* import { okedHooks } from "@oked/claude-agent-sdk";
|
|
119
|
+
*
|
|
120
|
+
* for await (const m of query({ prompt, options: { hooks: okedHooks() } })) { }
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export function okedHooks() {
|
|
124
|
+
return { PreToolUse: [{ hooks: [okedPreToolUseHook] }] };
|
|
125
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oked/claude-agent-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OKed for the Claude Agent SDK. A ready-made PreToolUse hook that gates sensitive tool calls behind a human approval push to your phone.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"prebuild": "npm run build --workspace=@oked/sdk",
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"dev": "tsc --watch",
|
|
23
|
+
"test": "npm run build && node src/smoke.test.mjs",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@anthropic-ai/claude-agent-sdk": ">=0.1.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependenciesMeta": {
|
|
30
|
+
"@anthropic-ai/claude-agent-sdk": {
|
|
31
|
+
"optional": true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@oked/sdk": "^0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@anthropic-ai/claude-agent-sdk": "^0.1.0",
|
|
39
|
+
"@types/node": "^22.13.10",
|
|
40
|
+
"typescript": "^5.6.0"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"claude-agent-sdk",
|
|
44
|
+
"claude",
|
|
45
|
+
"anthropic",
|
|
46
|
+
"approval",
|
|
47
|
+
"hook",
|
|
48
|
+
"agents",
|
|
49
|
+
"ai",
|
|
50
|
+
"human-in-the-loop",
|
|
51
|
+
"oked"
|
|
52
|
+
],
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/oked-ai/oked-sdk.git",
|
|
56
|
+
"directory": "packages/claude-agent-sdk"
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/oked-ai/oked-sdk/issues"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/oked-ai/oked-sdk/tree/main/packages/claude-agent-sdk#readme",
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=18"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
}
|
|
69
|
+
}
|