@atlasauth/pca-anthropic 0.0.0-stage → 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/README.md +38 -2
- package/dist/index.d.ts +68 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -3
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @atlasauth/pca-anthropic
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Anthropic (Claude) adapter for Proof-Carrying Authority: turn a tool_use block into a PCActn and build the tool_result with the proof attached.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i @atlasauth/pca-anthropic
|
|
9
|
+
# peer dependency, install alongside:
|
|
10
|
+
npm i @anthropic-ai/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@anthropic-ai/sdk` is an optional peer dependency.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { handleToolUse, extractToolUses } from '@atlasauth/pca-anthropic';
|
|
19
|
+
|
|
20
|
+
// For each tool_use block in the assistant message:
|
|
21
|
+
for (const block of extractToolUses(message.content)) {
|
|
22
|
+
const toolResult = await handleToolUse(
|
|
23
|
+
agent, // core @atlasauth/pca Agent
|
|
24
|
+
block, // { type: 'tool_use', id, name, input }
|
|
25
|
+
({ name, input }) => // map tool → catalog { verb, resource }; null declines
|
|
26
|
+
name === 'refund' ? { verb: 'stripe.refund', resource: `ch_${input.chargeId}` } : null,
|
|
27
|
+
{
|
|
28
|
+
refund: async (input, pca) => { // pca carries the signed proof to forward downstream
|
|
29
|
+
return doRefund(input);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
// → { type: 'tool_result', tool_use_id, content } (is_error:true on a declined/failed tool)
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`handleToolUse` builds a PCActn and attaches the proof; it does not authorize on its own — the resource server's verifier decides.
|
|
38
|
+
|
|
39
|
+
Part of Proof-Carrying Authority — see `@atlasauth/pca`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic (Claude) adapter for Proof-Carrying Authority.
|
|
3
|
+
*
|
|
4
|
+
* Claude's tool use is a conversation: an assistant message carries content blocks, and a
|
|
5
|
+
* `tool_use` block (`{ type, id, name, input }`) asks your app to run a tool. Your app runs it and
|
|
6
|
+
* replies with a user message containing a matching `tool_result` block (`{ tool_use_id, content }`).
|
|
7
|
+
* This adapter sits in that gap: it turns a `tool_use` block into a PCActn (via the core
|
|
8
|
+
* `bridgeToolCall`), runs your handler with the proof in hand, and hands back the `tool_result` to
|
|
9
|
+
* append to the next message — including the error-shaped result Claude expects when a tool refuses.
|
|
10
|
+
*
|
|
11
|
+
* Structurally compatible with the `@anthropic-ai/sdk` package (declared as an OPTIONAL peer
|
|
12
|
+
* dependency). We model only the block shapes we touch, so this works across SDK minor versions
|
|
13
|
+
* without importing its runtime — nothing here requires the SDK to be installed.
|
|
14
|
+
*
|
|
15
|
+
* HONEST: this does not authorize — it attaches a proof-carrying action; the resource server's
|
|
16
|
+
* verifier decides. A handler that forgets to forward the proof just means the downstream action
|
|
17
|
+
* lacks a valid PCActn and fails server-side.
|
|
18
|
+
*/
|
|
19
|
+
import { type Agent, type PcaCall, pcaHeaders } from '@atlasauth/pca';
|
|
20
|
+
/** A Claude `tool_use` content block (what the model emits to call a tool). */
|
|
21
|
+
export interface ToolUseBlock {
|
|
22
|
+
type: 'tool_use';
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
input: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
/** A Claude `tool_result` content block (what your app replies with). */
|
|
28
|
+
export interface ToolResultBlock {
|
|
29
|
+
type: 'tool_result';
|
|
30
|
+
tool_use_id: string;
|
|
31
|
+
content: string;
|
|
32
|
+
is_error?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** How a Claude tool name + input maps onto a PCA (verb, resource). Return null to decline the tool. */
|
|
35
|
+
export type ToolMapping = (block: {
|
|
36
|
+
name: string;
|
|
37
|
+
input: Record<string, unknown>;
|
|
38
|
+
}) => {
|
|
39
|
+
verb: string;
|
|
40
|
+
resource: string;
|
|
41
|
+
} | null;
|
|
42
|
+
/** Your tool implementation: gets the model's input and the PCActn so it can carry the proof downstream. */
|
|
43
|
+
export type ToolHandler = (input: Record<string, unknown>, pca: import('@atlasauth/pca').PcaCall) => Promise<unknown> | unknown;
|
|
44
|
+
/**
|
|
45
|
+
* Handle one `tool_use` block: build its PCActn, run the mapped handler, and return the `tool_result`.
|
|
46
|
+
*
|
|
47
|
+
* If the mapping declines the tool (returns null), we never run the handler — we return a
|
|
48
|
+
* `tool_result` with `is_error: true` so Claude sees the refusal. If the handler throws, we likewise
|
|
49
|
+
* return an error-shaped `tool_result`. On success, `content` is `JSON.stringify(result)`, falling
|
|
50
|
+
* back to `{ pca: <encoded> }` when the handler returns nothing.
|
|
51
|
+
*/
|
|
52
|
+
export declare function handleToolUse(agent: Agent, block: ToolUseBlock, map: ToolMapping, handlers: Record<string, ToolHandler>, opts?: {
|
|
53
|
+
aud?: string;
|
|
54
|
+
counter?: number;
|
|
55
|
+
now?: number;
|
|
56
|
+
}): Promise<ToolResultBlock>;
|
|
57
|
+
/** Handle many `tool_use` blocks (e.g. all of them from one assistant turn), preserving order. */
|
|
58
|
+
export declare function handleToolUses(agent: Agent, blocks: ToolUseBlock[], map: ToolMapping, handlers: Record<string, ToolHandler>, opts?: {
|
|
59
|
+
aud?: string;
|
|
60
|
+
counter?: number;
|
|
61
|
+
now?: number;
|
|
62
|
+
}): Promise<ToolResultBlock[]>;
|
|
63
|
+
/** Pull the `tool_use` blocks out of an assistant message's `content` array (ignoring text/other blocks). */
|
|
64
|
+
export declare function extractToolUses(content: Array<{
|
|
65
|
+
type: string;
|
|
66
|
+
[k: string]: unknown;
|
|
67
|
+
}>): ToolUseBlock[];
|
|
68
|
+
export { pcaHeaders, type PcaCall };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Anthropic (Claude) adapter for Proof-Carrying Authority.
|
|
4
|
+
*
|
|
5
|
+
* Claude's tool use is a conversation: an assistant message carries content blocks, and a
|
|
6
|
+
* `tool_use` block (`{ type, id, name, input }`) asks your app to run a tool. Your app runs it and
|
|
7
|
+
* replies with a user message containing a matching `tool_result` block (`{ tool_use_id, content }`).
|
|
8
|
+
* This adapter sits in that gap: it turns a `tool_use` block into a PCActn (via the core
|
|
9
|
+
* `bridgeToolCall`), runs your handler with the proof in hand, and hands back the `tool_result` to
|
|
10
|
+
* append to the next message — including the error-shaped result Claude expects when a tool refuses.
|
|
11
|
+
*
|
|
12
|
+
* Structurally compatible with the `@anthropic-ai/sdk` package (declared as an OPTIONAL peer
|
|
13
|
+
* dependency). We model only the block shapes we touch, so this works across SDK minor versions
|
|
14
|
+
* without importing its runtime — nothing here requires the SDK to be installed.
|
|
15
|
+
*
|
|
16
|
+
* HONEST: this does not authorize — it attaches a proof-carrying action; the resource server's
|
|
17
|
+
* verifier decides. A handler that forgets to forward the proof just means the downstream action
|
|
18
|
+
* lacks a valid PCActn and fails server-side.
|
|
19
|
+
*/
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.pcaHeaders = void 0;
|
|
22
|
+
exports.handleToolUse = handleToolUse;
|
|
23
|
+
exports.handleToolUses = handleToolUses;
|
|
24
|
+
exports.extractToolUses = extractToolUses;
|
|
25
|
+
const pca_1 = require("@atlasauth/pca");
|
|
26
|
+
Object.defineProperty(exports, "pcaHeaders", { enumerable: true, get: function () { return pca_1.pcaHeaders; } });
|
|
27
|
+
/**
|
|
28
|
+
* Handle one `tool_use` block: build its PCActn, run the mapped handler, and return the `tool_result`.
|
|
29
|
+
*
|
|
30
|
+
* If the mapping declines the tool (returns null), we never run the handler — we return a
|
|
31
|
+
* `tool_result` with `is_error: true` so Claude sees the refusal. If the handler throws, we likewise
|
|
32
|
+
* return an error-shaped `tool_result`. On success, `content` is `JSON.stringify(result)`, falling
|
|
33
|
+
* back to `{ pca: <encoded> }` when the handler returns nothing.
|
|
34
|
+
*/
|
|
35
|
+
async function handleToolUse(agent, block, map, handlers, opts) {
|
|
36
|
+
const pca = (0, pca_1.bridgeToolCall)(agent, { name: block.name, arguments: block.input }, (b) => map({ name: b.name, input: b.arguments }), opts);
|
|
37
|
+
if (!pca) {
|
|
38
|
+
return {
|
|
39
|
+
type: 'tool_result',
|
|
40
|
+
tool_use_id: block.id,
|
|
41
|
+
content: `No proof-carrying authority is mapped for tool "${block.name}".`,
|
|
42
|
+
is_error: true,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const handler = handlers[block.name];
|
|
46
|
+
if (!handler) {
|
|
47
|
+
return {
|
|
48
|
+
type: 'tool_result',
|
|
49
|
+
tool_use_id: block.id,
|
|
50
|
+
content: `No handler registered for tool "${block.name}".`,
|
|
51
|
+
is_error: true,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const result = await handler(block.input, pca);
|
|
56
|
+
return {
|
|
57
|
+
type: 'tool_result',
|
|
58
|
+
tool_use_id: block.id,
|
|
59
|
+
content: JSON.stringify(result ?? { pca: pca.encoded }),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
return {
|
|
64
|
+
type: 'tool_result',
|
|
65
|
+
tool_use_id: block.id,
|
|
66
|
+
content: err instanceof Error ? err.message : String(err),
|
|
67
|
+
is_error: true,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Handle many `tool_use` blocks (e.g. all of them from one assistant turn), preserving order. */
|
|
72
|
+
async function handleToolUses(agent, blocks, map, handlers, opts) {
|
|
73
|
+
const uses = blocks.filter((b) => b.type === 'tool_use');
|
|
74
|
+
return Promise.all(uses.map((b) => handleToolUse(agent, b, map, handlers, opts)));
|
|
75
|
+
}
|
|
76
|
+
/** Pull the `tool_use` blocks out of an assistant message's `content` array (ignoring text/other blocks). */
|
|
77
|
+
function extractToolUses(content) {
|
|
78
|
+
return content.filter((b) => b.type === 'tool_use');
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AAwCH,sCA6CC;AAGD,wCASC;AAGD,0CAEC;AApGD,wCAAsF;AAsG7E,2FAtG0C,gBAAU,OAsG1C;AAxEnB;;;;;;;GAOG;AACI,KAAK,UAAU,aAAa,CACjC,KAAY,EACZ,KAAmB,EACnB,GAAgB,EAChB,QAAqC,EACrC,IAAuD;IAEvD,MAAM,GAAG,GAAG,IAAA,oBAAc,EACxB,KAAK,EACL,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,KAAK,EAAE,EAC5C,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC,EAChD,IAAI,CACL,CAAC;IACF,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO;YACL,IAAI,EAAE,aAAa;YACnB,WAAW,EAAE,KAAK,CAAC,EAAE;YACrB,OAAO,EAAE,mDAAmD,KAAK,CAAC,IAAI,IAAI;YAC1E,QAAQ,EAAE,IAAI;SACf,CAAC;IACJ,CAAC;IACD,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,CAAC,OAAO,EAAE,CAAC;QAC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AAM,CAAC,CAAC,CAAC,EAA+C,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AACnG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlasauth/pca-anthropic",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Anthropic (Claude) adapter for Proof-Carrying Authority: turn a tool_use block into a PCActn and build the tool_result with the proof attached.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@atlasauth/pca": "0.1.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@anthropic-ai/sdk": "^0.30 || ^0.40 || ^0.50"
|
|
19
|
+
},
|
|
20
|
+
"peerDependenciesMeta": {
|
|
21
|
+
"@anthropic-ai/sdk": {
|
|
22
|
+
"optional": true
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"typescript": "^5.6.3",
|
|
27
|
+
"vitest": "^2.1.4"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.build.json",
|
|
38
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
39
|
+
"test": "vitest run"
|
|
40
|
+
}
|
|
6
41
|
}
|