@emilia-protocol/openai-agents 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 +190 -0
- package/README.md +115 -0
- package/index.d.ts +67 -0
- package/index.js +247 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2024–2026 EMILIA Protocol, Inc. and contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @emilia-protocol/openai-agents
|
|
2
|
+
|
|
3
|
+
## Add cryptographic approval receipts to OpenAI Agents in 10 minutes.
|
|
4
|
+
|
|
5
|
+
> **OpenAI pauses an agent and asks for approval. EMILIA makes that approval portable, offline-verifiable evidence** — proof that a *named* human accountably authorized *this exact* tool call, that anyone can check later without trusting OpenAI, your app, or a mutable log.
|
|
6
|
+
|
|
7
|
+
The OpenAI Agents SDK already pauses consequential tool calls and asks a human to approve them. That approval is a transient in-process boolean — the moment the run resumes, it is gone. This adapter turns each approval into an **EMILIA authorization receipt** (EP-RECEIPT-v1): a tamper-evident, Ed25519-signed artifact bound to the exact tool call, verifiable offline. It **composes with** OpenAI's approval primitive; it does not replace it.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## The confirmed OpenAI Agents SDK human-in-the-loop API
|
|
12
|
+
|
|
13
|
+
Grounded against the official docs (JavaScript/TypeScript, package `@openai/agents`):
|
|
14
|
+
|
|
15
|
+
- A tool requires approval when defined with **`needsApproval: true`** (or an async `needsApproval(context, args) => boolean`) on `tool({...})`.
|
|
16
|
+
- When such a tool is called, the run **pauses** and the pending approvals surface as **`result.interruptions`** — an array of **`RunToolApprovalItem`**, each with **`type: "tool_approval_item"`**.
|
|
17
|
+
- Each interruption exposes the **tool name** (`interruption.name` / `interruption.rawItem.name`), the **arguments** (`interruption.arguments` / `rawItem.arguments`, a JSON string), and the **call id** (`rawItem.callId` for `function_call`, or `rawItem.id` for hosted tools).
|
|
18
|
+
- You resolve each one with **`result.state.approve(interruption)`** or **`result.state.reject(interruption, { message })`**, then **resume by re-running** `run(agent, result.state)` with the same state.
|
|
19
|
+
|
|
20
|
+
Docs: https://openai.github.io/openai-agents-js/guides/human-in-the-loop (see also `.../classes/runstate` and `.../classes/streamedrunresult`).
|
|
21
|
+
|
|
22
|
+
This adapter is **framework-faithful**: it reads that exact interruption shape and drives those exact `state.approve` / `state.reject` calls, so a real integration is a thin wrapper — and the adapter is unit-testable **without** calling OpenAI.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @emilia-protocol/openai-agents @emilia-protocol/require-receipt
|
|
30
|
+
# @openai/agents is a peer dependency (you already have it in an Agents app)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Copy-paste integration
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { Agent, run, tool } from '@openai/agents';
|
|
37
|
+
import { requireReceiptForOpenAIAgent } from '@emilia-protocol/openai-agents';
|
|
38
|
+
import z from 'zod';
|
|
39
|
+
|
|
40
|
+
const cancelOrder = tool({
|
|
41
|
+
name: 'cancelOrder',
|
|
42
|
+
description: 'Cancel an order',
|
|
43
|
+
parameters: z.object({ orderId: z.number() }),
|
|
44
|
+
needsApproval: true, // <- OpenAI pauses the run here
|
|
45
|
+
execute: async ({ orderId }) => { /* ... */ },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const agent = new Agent({ name: 'Ops', tools: [cancelOrder] });
|
|
49
|
+
|
|
50
|
+
// One gate, configured once. actionFor maps a tool call -> canonical EP action_type.
|
|
51
|
+
const gate = requireReceiptForOpenAIAgent({
|
|
52
|
+
trustedKeys: [process.env.EMILIA_ISSUER_KEY], // base64url SPKI-DER issuer key(s) you trust
|
|
53
|
+
maxAgeSec: 900,
|
|
54
|
+
actionFor: (toolName /*, args */) => `openai.tool.${toolName}`,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
let result = await run(agent, 'Cancel order 4242');
|
|
58
|
+
|
|
59
|
+
while (result.interruptions?.length) {
|
|
60
|
+
// `receipts` is whatever your app collected for these calls — keyed by callId
|
|
61
|
+
// or tool name, or an array matched by action_type. A missing/invalid/replayed
|
|
62
|
+
// receipt is REJECTED; only a valid action-bound receipt is APPROVED.
|
|
63
|
+
const { approved, rejected, decisions } = await gate.resolve(result, { receipts });
|
|
64
|
+
|
|
65
|
+
console.log(decisions); // audit trail: decision + reason + action + subject per call
|
|
66
|
+
|
|
67
|
+
// gate.resolve already drove result.state.approve()/reject(); just resume:
|
|
68
|
+
result = await run(agent, result.state);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
console.log(result.finalOutput);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Per-interruption form, if you drive approvals yourself:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
const decision = gate.decide(interruption, receiptForThisCall);
|
|
78
|
+
if (decision.decision === 'approve') result.state.approve(interruption);
|
|
79
|
+
else result.state.reject(interruption, { message: `EMILIA: ${decision.reason}` });
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## The four checks it enforces
|
|
83
|
+
|
|
84
|
+
For every pending tool-approval interruption:
|
|
85
|
+
|
|
86
|
+
| Situation | Decision |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| **No receipt** for that interruption | **REJECT** — the tool stays blocked |
|
|
89
|
+
| **Valid** EP-RECEIPT-v1, action-bound to that exact tool call | **APPROVE** — the tool runs |
|
|
90
|
+
| **Replayed** receipt (`receipt_id` already consumed this process) | **REJECT** |
|
|
91
|
+
| **Tampered / invalid** receipt (signature or action mismatch, untrusted issuer, expired) | **REJECT** |
|
|
92
|
+
|
|
93
|
+
## The six audit questions a receipt answers
|
|
94
|
+
|
|
95
|
+
1. **Who approved it?** — `payload.subject` / `claim.approver` (a named, accountable human).
|
|
96
|
+
2. **What exact action?** — `claim.action_type`, bound via `actionFor` to this specific tool call.
|
|
97
|
+
3. **Was it altered after approval?** — no: the receipt is Ed25519-signed over sorted-key canonical JSON; any change breaks the signature.
|
|
98
|
+
4. **Was it replayed?** — no: one-time consumption by `receipt_id` (per-process consumed set).
|
|
99
|
+
5. **Was it authorized for the right org / policy?** — pin `trustedKeys` to the issuers your policy accepts; only those verify.
|
|
100
|
+
6. **Verifiable without trusting OpenAI, your app, or mutable logs?** — yes: verification is offline Ed25519 over canonical JSON; anyone holding the issuer's public key can re-check the artifact.
|
|
101
|
+
|
|
102
|
+
## Production note
|
|
103
|
+
|
|
104
|
+
- **Pin `trustedKeys`** to your real issuer key(s). **Drop `allowInlineKey`** (it only proves integrity, never trust).
|
|
105
|
+
- The **consumed-receipt set is per-process** (replay defense across restarts/instances needs a shared store — see `@emilia-protocol/gate`).
|
|
106
|
+
- This is **necessary, not sufficient**. It composes with — and never substitutes for — the resource owner's own authorization and policy checks. It makes the human approval that OpenAI already asks for into auditable, portable evidence; it does not decide whether the action *should* be allowed.
|
|
107
|
+
|
|
108
|
+
## References
|
|
109
|
+
|
|
110
|
+
- `draft-schrock-ep-authorization-receipts` — EP authorization-receipt format (individual Internet-Draft, **not** an RFC).
|
|
111
|
+
- `draft-schrock-ep-enforcement-point` — EP enforcement-point profile (individual Internet-Draft, **not** an RFC).
|
|
112
|
+
- `@emilia-protocol/require-receipt` — the underlying offline verifier (`verifyEmiliaReceipt`).
|
|
113
|
+
- `@emilia-protocol/gate` — productized enforcement point with a shared consumed-store + assurance tiers.
|
|
114
|
+
|
|
115
|
+
Apache-2.0. Reference implementation, experimental.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @emilia-protocol/openai-agents — type declarations.
|
|
3
|
+
* @license Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** A single per-interruption decision. */
|
|
7
|
+
export interface ReceiptDecision {
|
|
8
|
+
decision: 'approve' | 'reject';
|
|
9
|
+
/** Canonical EP action_type this tool call maps to (via actionFor), or null. */
|
|
10
|
+
action: string | null;
|
|
11
|
+
/** The OpenAI tool name from the interruption, or null. */
|
|
12
|
+
toolName: string | null;
|
|
13
|
+
/** The tool-call id (function_call.callId or hosted id), or null. */
|
|
14
|
+
callId: string | null;
|
|
15
|
+
/** Machine-readable reason for the decision. */
|
|
16
|
+
reason: string;
|
|
17
|
+
/** Present when a valid receipt was consumed. */
|
|
18
|
+
receipt_id?: string;
|
|
19
|
+
/** The receipt subject (the named accountable human), when valid. */
|
|
20
|
+
subject?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ResolveResult {
|
|
24
|
+
/** Interruptions that were approved (and state.approve called). */
|
|
25
|
+
approved: unknown[];
|
|
26
|
+
/** Interruptions that were rejected (and state.reject called). */
|
|
27
|
+
rejected: unknown[];
|
|
28
|
+
/** All per-interruption decisions, in order. */
|
|
29
|
+
decisions: ReceiptDecision[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RequireReceiptForOpenAIAgentOptions {
|
|
33
|
+
/** base64url SPKI-DER issuer public keys you trust. */
|
|
34
|
+
trustedKeys?: string[];
|
|
35
|
+
/** Also accept a receipt's own inline key (integrity, NOT trust). Default false. */
|
|
36
|
+
allowInlineKey?: boolean;
|
|
37
|
+
/** Reject receipts older than this many seconds. Default 900. */
|
|
38
|
+
maxAgeSec?: number;
|
|
39
|
+
/** REQUIRED. Map a tool call to the canonical EP action_type the receipt must bind. */
|
|
40
|
+
actionFor: (toolName: string, args: unknown) => string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface OpenAIAgentReceiptGate {
|
|
44
|
+
/** Decide a single interruption against a single receipt. */
|
|
45
|
+
decide(interruption: unknown, receipt: object | null | undefined): ReceiptDecision;
|
|
46
|
+
/** Resolve all pending tool-approval interruptions on a run result. */
|
|
47
|
+
resolve(
|
|
48
|
+
runResult: { interruptions?: unknown[]; state?: unknown },
|
|
49
|
+
ctx?: {
|
|
50
|
+
receipts?: Record<string, object> | Map<string, object> | object[];
|
|
51
|
+
state?: unknown;
|
|
52
|
+
},
|
|
53
|
+
): Promise<ResolveResult>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function requireReceiptForOpenAIAgent(
|
|
57
|
+
opts: RequireReceiptForOpenAIAgentOptions,
|
|
58
|
+
): OpenAIAgentReceiptGate;
|
|
59
|
+
|
|
60
|
+
/** Reset the process-local consumed-receipt set. Test/ops helper. */
|
|
61
|
+
export function _resetConsumed(): void;
|
|
62
|
+
|
|
63
|
+
declare const _default: {
|
|
64
|
+
requireReceiptForOpenAIAgent: typeof requireReceiptForOpenAIAgent;
|
|
65
|
+
_resetConsumed: typeof _resetConsumed;
|
|
66
|
+
};
|
|
67
|
+
export default _default;
|
package/index.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @emilia-protocol/openai-agents — make an OpenAI agent's approval portable.
|
|
3
|
+
* @license Apache-2.0
|
|
4
|
+
*
|
|
5
|
+
* The OpenAI Agents SDK already has a human-in-the-loop primitive: a tool marked
|
|
6
|
+
* `needsApproval` pauses the run, and the pending approvals surface as
|
|
7
|
+
* `RunToolApprovalItem`s in `result.interruptions`. You resolve each one with
|
|
8
|
+
* `result.state.approve(item)` / `result.state.reject(item)` and re-run the agent
|
|
9
|
+
* with the same state.
|
|
10
|
+
*
|
|
11
|
+
* That approval is a transient, in-process boolean. The moment the run resumes it
|
|
12
|
+
* is gone — there is no portable evidence that a *named* human accountably
|
|
13
|
+
* authorized *this exact* tool call, nothing an auditor (or an insurer, or a
|
|
14
|
+
* counterparty) can verify later without trusting OpenAI, your app, or a mutable
|
|
15
|
+
* log.
|
|
16
|
+
*
|
|
17
|
+
* This adapter closes that gap. It does NOT replace OpenAI's approval step — it
|
|
18
|
+
* drives it. For each pending interruption it requires a valid EMILIA
|
|
19
|
+
* authorization receipt (EP-RECEIPT-v1) cryptographically bound (via action_type)
|
|
20
|
+
* to that exact tool call:
|
|
21
|
+
*
|
|
22
|
+
* - no receipt for the interruption -> state.reject(item) (the tool stays blocked)
|
|
23
|
+
* - valid, action-bound receipt -> state.approve(item) (the tool runs)
|
|
24
|
+
* - replayed receipt (already used) -> state.reject(item)
|
|
25
|
+
* - tampered / invalid receipt -> state.reject(item)
|
|
26
|
+
*
|
|
27
|
+
* Verification is offline Ed25519 over canonical JSON via
|
|
28
|
+
* @emilia-protocol/require-receipt's verifyEmiliaReceipt. Zero network. The
|
|
29
|
+
* decision is necessary-not-sufficient: it composes with — never substitutes for —
|
|
30
|
+
* the resource owner's own checks.
|
|
31
|
+
*
|
|
32
|
+
* The adapter is unit-testable WITHOUT @openai/agents: it reads the documented
|
|
33
|
+
* interruption shape and calls the documented state methods, both passed in.
|
|
34
|
+
*
|
|
35
|
+
* See: draft-schrock-ep-authorization-receipts, draft-schrock-ep-enforcement-point
|
|
36
|
+
* (individual Internet-Drafts, not RFCs).
|
|
37
|
+
*/
|
|
38
|
+
// Declared dependency: @emilia-protocol/require-receipt (see package.json). In
|
|
39
|
+
// this monorepo there is no workspace symlink, so we import the sibling package
|
|
40
|
+
// by relative path — the same convention @emilia-protocol/gate uses. When this
|
|
41
|
+
// package is installed from npm, the published build resolves the bare
|
|
42
|
+
// "@emilia-protocol/require-receipt" specifier; both point at the same module.
|
|
43
|
+
import { verifyEmiliaReceipt } from '../require-receipt/index.js';
|
|
44
|
+
|
|
45
|
+
/** Process-local set of consumed receipt_ids (replay defense). Per-process only. */
|
|
46
|
+
const consumed = new Set();
|
|
47
|
+
|
|
48
|
+
/** Reset the consumed-receipt set. Test/ops helper — not a production control. */
|
|
49
|
+
export function _resetConsumed() {
|
|
50
|
+
consumed.clear();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Read the tool name from an OpenAI Agents RunToolApprovalItem.
|
|
55
|
+
* The SDK exposes `.name` on the item and also `.rawItem.name`.
|
|
56
|
+
*/
|
|
57
|
+
function interruptionToolName(interruption) {
|
|
58
|
+
return (
|
|
59
|
+
interruption?.name ??
|
|
60
|
+
interruption?.rawItem?.name ??
|
|
61
|
+
null
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Read the tool-call arguments from an interruption.
|
|
67
|
+
* The Agents SDK stores arguments as a JSON *string* on function_call rawItems
|
|
68
|
+
* (`interruption.arguments` / `rawItem.arguments`). We parse when possible and
|
|
69
|
+
* otherwise return the raw value so `actionFor` can decide.
|
|
70
|
+
*/
|
|
71
|
+
function interruptionArgs(interruption) {
|
|
72
|
+
const raw =
|
|
73
|
+
interruption?.arguments ??
|
|
74
|
+
interruption?.rawItem?.arguments ??
|
|
75
|
+
undefined;
|
|
76
|
+
if (raw === undefined || raw === null) return {};
|
|
77
|
+
if (typeof raw !== 'string') return raw;
|
|
78
|
+
try {
|
|
79
|
+
return JSON.parse(raw);
|
|
80
|
+
} catch {
|
|
81
|
+
return raw;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Stable id for a tool call: function_call uses callId; hosted tools use id. */
|
|
86
|
+
function interruptionCallId(interruption) {
|
|
87
|
+
return (
|
|
88
|
+
interruption?.rawItem?.callId ??
|
|
89
|
+
interruption?.rawItem?.id ??
|
|
90
|
+
interruption?.callId ??
|
|
91
|
+
interruption?.id ??
|
|
92
|
+
null
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isApprovalInterruption(interruption) {
|
|
97
|
+
// Faithful to the SDK: items are of type "tool_approval_item". Be lenient for
|
|
98
|
+
// synthetic / minimal items in tests that still carry a tool name.
|
|
99
|
+
if (!interruption || typeof interruption !== 'object') return false;
|
|
100
|
+
if (interruption.type && interruption.type !== 'tool_approval_item') return false;
|
|
101
|
+
return interruptionToolName(interruption) != null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build a receipt gate for OpenAI Agents human-in-the-loop tool approvals.
|
|
106
|
+
*
|
|
107
|
+
* @param {object} opts
|
|
108
|
+
* @param {string[]} [opts.trustedKeys] base64url SPKI-DER issuer public keys you trust.
|
|
109
|
+
* @param {boolean} [opts.allowInlineKey=false] also accept a receipt's own inline
|
|
110
|
+
* key (proves integrity, NOT trust — leave OFF in production).
|
|
111
|
+
* @param {number} [opts.maxAgeSec=900] reject receipts older than this.
|
|
112
|
+
* @param {(toolName:string, args:any)=>string} opts.actionFor REQUIRED — maps a
|
|
113
|
+
* tool call to the canonical EP action_type the receipt must be bound to.
|
|
114
|
+
* @returns {{
|
|
115
|
+
* decide: (interruption:any, receipt:object|null|undefined) => {decision:'approve'|'reject', action:string|null, toolName:string|null, callId:string|null, reason:string, receipt_id?:string, subject?:string},
|
|
116
|
+
* resolve: (runResult:any, ctx:{receipts?:object|Map|Array, state?:any}) => Promise<{approved:Array, rejected:Array, decisions:Array}>
|
|
117
|
+
* }}
|
|
118
|
+
*/
|
|
119
|
+
export function requireReceiptForOpenAIAgent(opts = {}) {
|
|
120
|
+
const {
|
|
121
|
+
trustedKeys = [],
|
|
122
|
+
allowInlineKey = false,
|
|
123
|
+
maxAgeSec = 900,
|
|
124
|
+
actionFor,
|
|
125
|
+
} = opts;
|
|
126
|
+
|
|
127
|
+
if (typeof actionFor !== 'function') {
|
|
128
|
+
throw new TypeError('requireReceiptForOpenAIAgent: opts.actionFor (toolName, args) => action_type is required');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Decide a single interruption against a single receipt. Pure: no side effects
|
|
133
|
+
* EXCEPT marking a receipt_id consumed when (and only when) it is the valid
|
|
134
|
+
* receipt that earns an approve.
|
|
135
|
+
*/
|
|
136
|
+
function decide(interruption, receipt) {
|
|
137
|
+
const toolName = interruptionToolName(interruption);
|
|
138
|
+
const callId = interruptionCallId(interruption);
|
|
139
|
+
const args = interruptionArgs(interruption);
|
|
140
|
+
const action = toolName != null ? actionFor(toolName, args) : null;
|
|
141
|
+
|
|
142
|
+
const base = { action, toolName, callId };
|
|
143
|
+
|
|
144
|
+
if (!isApprovalInterruption(interruption)) {
|
|
145
|
+
return { decision: 'reject', ...base, reason: 'not_a_tool_approval_interruption' };
|
|
146
|
+
}
|
|
147
|
+
if (!receipt) {
|
|
148
|
+
return { decision: 'reject', ...base, reason: 'no_receipt_for_interruption' };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const v = verifyEmiliaReceipt(receipt, {
|
|
152
|
+
trustedKeys,
|
|
153
|
+
allowInlineKey,
|
|
154
|
+
maxAgeSec,
|
|
155
|
+
action, // binds the receipt's claim.action_type to THIS tool call
|
|
156
|
+
});
|
|
157
|
+
if (!v.ok) {
|
|
158
|
+
return { decision: 'reject', ...base, reason: v.reason || 'invalid_receipt' };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Replay defense: a receipt_id may earn an approval only once per process.
|
|
162
|
+
if (v.receipt_id && consumed.has(v.receipt_id)) {
|
|
163
|
+
return { decision: 'reject', ...base, reason: 'receipt_replayed', receipt_id: v.receipt_id };
|
|
164
|
+
}
|
|
165
|
+
if (v.receipt_id) consumed.add(v.receipt_id);
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
decision: 'approve',
|
|
169
|
+
...base,
|
|
170
|
+
reason: 'valid_action_bound_receipt',
|
|
171
|
+
receipt_id: v.receipt_id,
|
|
172
|
+
subject: v.subject,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Look up the receipt a caller supplied for a given interruption. Accepts:
|
|
178
|
+
* - a Map / plain object keyed by callId OR tool name
|
|
179
|
+
* - an array of receipts (matched by claim.action_type for this tool call)
|
|
180
|
+
*/
|
|
181
|
+
function lookupReceipt(receipts, interruption) {
|
|
182
|
+
if (receipts == null) return null;
|
|
183
|
+
const callId = interruptionCallId(interruption);
|
|
184
|
+
const toolName = interruptionToolName(interruption);
|
|
185
|
+
|
|
186
|
+
if (receipts instanceof Map) {
|
|
187
|
+
if (callId != null && receipts.has(callId)) return receipts.get(callId);
|
|
188
|
+
if (toolName != null && receipts.has(toolName)) return receipts.get(toolName);
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
if (Array.isArray(receipts)) {
|
|
192
|
+
const args = interruptionArgs(interruption);
|
|
193
|
+
const action = toolName != null ? actionFor(toolName, args) : null;
|
|
194
|
+
return receipts.find((r) => r?.payload?.claim?.action_type === action) ?? null;
|
|
195
|
+
}
|
|
196
|
+
if (typeof receipts === 'object') {
|
|
197
|
+
if (callId != null && callId in receipts) return receipts[callId];
|
|
198
|
+
if (toolName != null && toolName in receipts) return receipts[toolName];
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Resolve EVERY pending tool-approval interruption on a run result, driving the
|
|
206
|
+
* SDK's own approve/reject. Returns the decisions; the run is then resumed by
|
|
207
|
+
* the caller via `run(agent, state)` (kept out of this adapter so it stays
|
|
208
|
+
* testable without OpenAI).
|
|
209
|
+
*
|
|
210
|
+
* @param {object} runResult an Agents-SDK RunResult / StreamedRunResult with
|
|
211
|
+
* `.interruptions` (RunToolApprovalItem[]) and `.state` (RunState).
|
|
212
|
+
* @param {object} ctx
|
|
213
|
+
* @param {object|Map|Array} [ctx.receipts] caller-supplied receipts (see lookupReceipt).
|
|
214
|
+
* @param {object} [ctx.state] override the state object to drive (defaults to runResult.state).
|
|
215
|
+
*/
|
|
216
|
+
async function resolve(runResult, ctx = {}) {
|
|
217
|
+
const interruptions = runResult?.interruptions ?? [];
|
|
218
|
+
const state = ctx.state ?? runResult?.state ?? null;
|
|
219
|
+
const receipts = ctx.receipts;
|
|
220
|
+
|
|
221
|
+
const approved = [];
|
|
222
|
+
const rejected = [];
|
|
223
|
+
const decisions = [];
|
|
224
|
+
|
|
225
|
+
for (const interruption of interruptions) {
|
|
226
|
+
const receipt = lookupReceipt(receipts, interruption);
|
|
227
|
+
const d = decide(interruption, receipt);
|
|
228
|
+
decisions.push(d);
|
|
229
|
+
if (d.decision === 'approve') {
|
|
230
|
+
approved.push(interruption);
|
|
231
|
+
if (state && typeof state.approve === 'function') state.approve(interruption);
|
|
232
|
+
} else {
|
|
233
|
+
rejected.push(interruption);
|
|
234
|
+
if (state && typeof state.reject === 'function') {
|
|
235
|
+
state.reject(interruption, { message: `EMILIA: ${d.reason}` });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return { approved, rejected, decisions };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return { decide, resolve };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const openaiAgentsExports = { requireReceiptForOpenAIAgent, _resetConsumed };
|
|
247
|
+
export default openaiAgentsExports;
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@emilia-protocol/openai-agents",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Turn the OpenAI Agents SDK human-in-the-loop tool-approval step into a verifiable, offline-checkable EMILIA authorization receipt. OpenAI pauses an agent and asks for approval; EMILIA makes that approval portable, tamper-evident evidence — a named human accountably authorized this exact tool call. Composes WITH OpenAI's approval primitive (needsApproval / interruptions / state.approve|reject); does not replace it. Reference implementation, experimental.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["index.js", "index.d.ts", "README.md", "LICENSE"],
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"emilia-protocol",
|
|
21
|
+
"openai-agents",
|
|
22
|
+
"ai-agent",
|
|
23
|
+
"human-in-the-loop",
|
|
24
|
+
"tool-approval",
|
|
25
|
+
"needs-approval",
|
|
26
|
+
"trust-receipt",
|
|
27
|
+
"agent-authorization",
|
|
28
|
+
"agent-accountability",
|
|
29
|
+
"receipt-required",
|
|
30
|
+
"verifiable-credential",
|
|
31
|
+
"ai-safety"
|
|
32
|
+
],
|
|
33
|
+
"engines": { "node": ">=18" },
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@emilia-protocol/require-receipt": "^0.3.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@openai/agents": ">=0.0.1"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"@openai/agents": { "optional": true }
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/emiliaprotocol/emilia-protocol.git",
|
|
46
|
+
"directory": "packages/openai-agents"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://www.emiliaprotocol.ai/agent-guard",
|
|
49
|
+
"publishConfig": { "access": "public" }
|
|
50
|
+
}
|