purser-client 0.0.0-stage → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +175 -2
- package/dist/adapters/ai-sdk.d.ts +37 -0
- package/dist/adapters/ai-sdk.js +39 -0
- package/dist/adapters/guard.d.ts +137 -0
- package/dist/adapters/guard.js +251 -0
- package/dist/adapters/langchain.d.ts +38 -0
- package/dist/adapters/langchain.js +27 -0
- package/dist/adapters/openai-agents.d.ts +46 -0
- package/dist/adapters/openai-agents.js +42 -0
- package/dist/index.d.ts +262 -0
- package/dist/index.js +221 -0
- package/package.json +47 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Max Geurtsen
|
|
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
CHANGED
|
@@ -1,3 +1,176 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<a href="../../README.md"><picture><source media="(prefers-color-scheme: dark)" srcset="../../docs/assets/brand/purser-mark-dark.svg"><img src="../../docs/assets/brand/purser-mark.svg" alt="Purser" width="64" height="64"></picture></a>
|
|
4
|
+
|
|
5
|
+
# Purser TypeScript client
|
|
6
|
+
|
|
7
|
+
**List invoices, propose a payment and read its status from TypeScript; no dependencies, and no method approves.**
|
|
8
|
+
|
|
9
|
+
[](https://github.com/agamemnon-coder/x402-business-agents/actions/workflows/ci.yml?query=branch%3Amain) [](https://www.npmjs.com/package/purser-client) [](package.json) [](../../openapi/purser.yaml) [](#it-cannot-approve) [](#it-cannot-approve)
|
|
10
|
+
|
|
11
|
+
[Install](#install) · [Quickstart](#quickstart) · [Agent frameworks](#use-with-agent-frameworks) · [It cannot approve](#it-cannot-approve) · [Notes](#notes)
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<picture><source media="(prefers-color-scheme: dark)" srcset="../../docs/assets/pages/sdk-typescript-dark.svg"><img src="../../docs/assets/pages/sdk-typescript-light.svg" alt="Purser's TypeScript client in four steps: listInvoices, proposePayment to PENDING_APPROVAL with terms_sha256, the CFO signs the SHA-256 of the exact terms in Purser Cloud, getProposal to APPROVED and EXECUTED; no method approves (animated)" width="100%"></picture>
|
|
16
|
+
|
|
17
|
+
A small, typed client for Purser Cloud's API. It lists invoices, proposes an invoice's payment for the CFO's approval
|
|
18
|
+
and reads a proposal's status. It has no dependencies (the platform's `fetch`, Node 22.13 or later) and calls only the
|
|
19
|
+
operations of [`openapi/purser.yaml`](../../openapi/purser.yaml), under their `operationId`s
|
|
20
|
+
(`test/sdk-typescript.test.ts` checks every one).
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
**On npm** as [`purser-client`](https://www.npmjs.com/package/purser-client) (this repository's client; not the Go
|
|
25
|
+
project `go-steer/purser`) since 1 December 2026 ([docs/releasing.md](../../docs/releasing.md), "Packages"):
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install purser-client
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or build it from a clone, or use it from this repository (`sdk/typescript/src/index.ts`):
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
cd sdk/typescript && npm pack # builds dist/ and writes purser-client-<version>.tgz (needs the repo's npm ci)
|
|
35
|
+
cd your-project && npm install /path/to/purser-client-<version>.tgz
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Status: tested locally against Purser Cloud in-process. Packaged as `purser-client` (ESM with type declarations):
|
|
39
|
+
`test/sdk-packages.test.ts` packs it with `npm pack`, installs the tarball into an empty project and imports it with
|
|
40
|
+
plain `node`.
|
|
41
|
+
|
|
42
|
+
## Quickstart
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
export PURSER_BASE_URL=https://purser.example.com # your Purser Cloud (PURSER_URL works too)
|
|
46
|
+
export PURSER_API_KEY=pck_... # an owner makes it: scopes read and propose; never in code
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { Purser, PurserError } from "purser-client"; // or "./sdk/typescript/src/index.js" in a clone
|
|
51
|
+
|
|
52
|
+
const purser = Purser.fromEnv();
|
|
53
|
+
|
|
54
|
+
// 1. List invoices (one page; purser.invoices() walks every page)
|
|
55
|
+
const page = await purser.listInvoices({ status: "ready", limit: 20 });
|
|
56
|
+
for (const inv of page.items) console.log(inv.id, inv.vendor, inv.currency, inv.total);
|
|
57
|
+
|
|
58
|
+
// 2. Propose a payment for the CFO's approval (a key with the propose scope)
|
|
59
|
+
const payable = (await purser.listPayableInvoices()).find((i) => i.payable && !i.proposal);
|
|
60
|
+
if (payable) {
|
|
61
|
+
try {
|
|
62
|
+
const { proposal } = await purser.proposePayment(payable.id);
|
|
63
|
+
console.log(`${proposal.id} waits for the CFO's passkey`); // nothing is paid yet
|
|
64
|
+
} catch (e) {
|
|
65
|
+
if (e instanceof PurserError && e.status === 409) console.log("Purser did not file it:", e.message);
|
|
66
|
+
else throw e;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 3. Read a proposal's status
|
|
71
|
+
const p = await purser.getProposal("P-0009");
|
|
72
|
+
console.log(p.status); // PENDING_APPROVAL, then APPROVED and EXECUTED, or REJECTED or EXPIRED
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Since 1.7.1 the client also reads a payment's authorisation dossier and back-tests past invoices (a read key):
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const dossier = await purser.getPaymentDossier("P-0009"); // or the invoice's id; who signed which exact terms, when
|
|
79
|
+
const report = await purser.runShadowMode(csvText, { aloneLimit: "2000.00" }); // shadow mode: nothing is written
|
|
80
|
+
console.log(report.summary);
|
|
81
|
+
// purser.getBankFile("P-0009"): the SEPA file an approved payment was executed as; the CFO's own sign-in only (403 to every key)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Use with agent frameworks
|
|
85
|
+
|
|
86
|
+
`withPurserGuard` turns an agent's payment tool call into a Purser **proposal**: the call is parsed (an invoice id,
|
|
87
|
+
an amount with exactly two decimals, a currency), checked against the invoice Purser holds, and filed for the CFO's
|
|
88
|
+
approval. It never pays and never approves; the CFO's passkey (Face ID) in Purser Cloud is the only approval. The tool
|
|
89
|
+
answers `{ outcome, proposal_id, status, terms_sha256, message, ... }`: `pending`, `approved`, `paid`, `refused` (by
|
|
90
|
+
the guard before filing, by Purser, or by the CFO) or `failed`. With `wait`, it polls the proposal with backoff until
|
|
91
|
+
Purser answers or `timeoutMs` passes (then `timed_out: true`; the proposal stays filed). Its key needs the read and
|
|
92
|
+
propose scopes; it refuses a person's sign-in. No framework is a dependency: each adapter is typed by the framework's
|
|
93
|
+
shape, and you pass its `tool()` where the framework needs its own.
|
|
94
|
+
|
|
95
|
+
**Vercel AI SDK** (`purser-client/ai-sdk`):
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { generateText } from "ai";
|
|
99
|
+
import { Purser } from "purser-client";
|
|
100
|
+
import { withPurserGuard } from "purser-client/ai-sdk";
|
|
101
|
+
|
|
102
|
+
const tools = { propose_invoice_payment: withPurserGuard({ purser: Purser.fromEnv(), wait: { timeoutMs: 60_000 } }) };
|
|
103
|
+
const { text } = await generateText({ model, tools, prompt: "Pay Bergtex's invoice AP-0004 if it is due." });
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**OpenAI Agents SDK** (`purser-client/openai-agents`):
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { Agent, run, tool } from "@openai/agents";
|
|
110
|
+
import { Purser } from "purser-client";
|
|
111
|
+
import { withPurserGuard } from "purser-client/openai-agents";
|
|
112
|
+
|
|
113
|
+
const agent = new Agent({ name: "Payables", instructions: "Propose due invoices.", tools: [withPurserGuard({ purser: Purser.fromEnv(), tool })] });
|
|
114
|
+
await run(agent, "Pay Bergtex's invoice AP-0004 if it is due.");
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**LangChain and LangGraph** (`purser-client/langchain`):
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { tool } from "@langchain/core/tools";
|
|
121
|
+
import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
122
|
+
import { Purser } from "purser-client";
|
|
123
|
+
import { withPurserGuard } from "purser-client/langchain";
|
|
124
|
+
|
|
125
|
+
const agent = createReactAgent({ llm, tools: [withPurserGuard({ purser: Purser.fromEnv(), tool })] });
|
|
126
|
+
// To call it yourself in TypeScript, name the tool's type: withPurserGuard<DynamicStructuredTool>({ ... }).invoke(...)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
To keep your own payment tool's name and schema, wrap it: `withPurserGuard(myPayTool, { purser, toPayment })`, where
|
|
130
|
+
`toPayment` maps its input to `{ invoice_id, amount }` (or `amount_cents`) and `currency`. Its own `execute` is never
|
|
131
|
+
called. `purser-client/guard` has the framework-free `createPaymentGuard` the three are built on. Tested offline
|
|
132
|
+
against a stand-in API and against Purser Cloud in-process (`test/sdk-adapters.test.ts`), not against the frameworks'
|
|
133
|
+
own packages.
|
|
134
|
+
|
|
135
|
+
## It cannot approve
|
|
136
|
+
|
|
137
|
+
Approving a payment is the CFO's passkey over the exact terms, in Purser Cloud. The API refuses every key there, and
|
|
138
|
+
this client has no method for it. Test money only until a company goes live. The picture at the top shows this flow;
|
|
139
|
+
the same as a sequence diagram:
|
|
140
|
+
|
|
141
|
+
```mermaid
|
|
142
|
+
sequenceDiagram
|
|
143
|
+
participant Y as Your code (this client)
|
|
144
|
+
participant P as Purser Cloud API
|
|
145
|
+
participant C as CFO (passkey, in Purser Cloud)
|
|
146
|
+
Y->>P: listInvoices / listPayableInvoices (read scope)
|
|
147
|
+
P-->>Y: invoices
|
|
148
|
+
Y->>P: proposePayment(id) (propose scope)
|
|
149
|
+
P-->>Y: proposal PENDING_APPROVAL, with terms_sha256
|
|
150
|
+
Note over Y,P: no method approves, and the API refuses every key there
|
|
151
|
+
C->>P: signs the SHA-256 of the exact terms
|
|
152
|
+
Y->>P: getProposal(id)
|
|
153
|
+
P-->>Y: APPROVED, then EXECUTED (or REJECTED, EXPIRED)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Notes
|
|
157
|
+
|
|
158
|
+
- `baseUrl` must be https (http only for localhost). The key is sent as `Authorization: Bearer` and never logged.
|
|
159
|
+
- Errors are `PurserError` with `status` and `code`: `forbidden` for a scope the key lacks, `conflict` when Purser
|
|
160
|
+
will not file (held, waiting or paid; the message says why), and `not_found`.
|
|
161
|
+
- `invoices()` follows `next_cursor` page by page. If the server sends a cursor it already gave, or the same page
|
|
162
|
+
again, it throws `PurserError` (`repeated_cursor`, `repeated_page`) before yielding that page, instead of reading
|
|
163
|
+
the same invoices for ever.
|
|
164
|
+
- If the CFO set a pay-alone limit (off unless set), a proposal within it is paid under the CFO's own policy, and the
|
|
165
|
+
answer says so in `alone`.
|
|
166
|
+
- Pass `fetch` to use your own (a proxy, a test).
|
|
167
|
+
- Each request is given up after `timeoutMs` (30,000 by default; 0 for none), and `signal` (an `AbortSignal`) stops
|
|
168
|
+
every request of the client, and any wait before a retry, when it fires.
|
|
169
|
+
- Every POST carries an `Idempotency-Key`: a new UUID, or yours (`proposePayment(id, { idempotencyKey })`). Purser
|
|
170
|
+
Cloud answers the same key and request within 24 hours with the first answer, so the payment is filed once.
|
|
171
|
+
- A read (GET), or a POST with its `Idempotency-Key`, answered 429 or 5xx is tried again up to `retries` times (2 by
|
|
172
|
+
default; every try the same key), waiting as the answer's `Retry-After` says (at most a minute), else 0.5 s, 1 s, 2 s
|
|
173
|
+
with jitter. With `idempotencyKeys: false` a POST carries no key and is never tried again.
|
|
174
|
+
- Requests carry `User-Agent: purser-client/<version>`. A proposal's `status` is a `ProposalStatus`
|
|
175
|
+
(`PENDING_APPROVAL`, `APPROVED`, `EXECUTED`, `FAILED`, `REJECTED` or `EXPIRED`). `Purser.fromEnv()` reads
|
|
176
|
+
`PURSER_BASE_URL` (or `PURSER_URL`) and `PURSER_API_KEY` where the platform has `process.env`; elsewhere pass the values.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type GuardOptions, type GuardResult, type ToPayment } from "./guard.js";
|
|
2
|
+
export { PAYMENT_INPUT_SCHEMA, type GuardOptions, type GuardResult, type WaitOptions } from "./guard.js";
|
|
3
|
+
/** What the AI SDK passes an execute as its second argument (the part this adapter reads). */
|
|
4
|
+
export interface AiSdkToolCallOptions {
|
|
5
|
+
toolCallId?: string;
|
|
6
|
+
abortSignal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The AI SDK's tool shape: inputSchema (AI SDK 5 and later) and parameters (AI SDK 4) carry the same schema object.
|
|
10
|
+
* It is opaque, and typed `never` because the AI SDK keys its schema type by a symbol private to its module: so typed,
|
|
11
|
+
* it is accepted as the SDK's FlexibleSchema whatever its version (at run time it is the object jsonSchema() makes).
|
|
12
|
+
*/
|
|
13
|
+
export interface AiSdkTool<INPUT = unknown> {
|
|
14
|
+
description: string;
|
|
15
|
+
inputSchema: never;
|
|
16
|
+
parameters: never;
|
|
17
|
+
execute: (input: INPUT, options?: AiSdkToolCallOptions) => Promise<GuardResult>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* "ai"'s jsonSchema(), when you pass it. It is called as jsonSchema(PAYMENT_INPUT_SCHEMA, { validate }); its parameters
|
|
21
|
+
* are typed `never` so that "ai"'s own signature (JSONSchema7, its generics) is accepted whatever its version.
|
|
22
|
+
*/
|
|
23
|
+
export type JsonSchemaFactory = (schema: never, options?: never) => unknown;
|
|
24
|
+
export interface AiSdkGuardOptions extends GuardOptions {
|
|
25
|
+
/** jsonSchema from "ai", to make the input schema with your installed version; a structurally equal one without it. */
|
|
26
|
+
jsonSchema?: JsonSchemaFactory;
|
|
27
|
+
/** When wrapping your own tool: maps its input to the guard's. */
|
|
28
|
+
toPayment?: ToPayment;
|
|
29
|
+
}
|
|
30
|
+
/** A new tool that proposes an invoice's payment in Purser. */
|
|
31
|
+
export declare function withPurserGuard(options: AiSdkGuardOptions): AiSdkTool;
|
|
32
|
+
/** Your payment tool, wrapped: the same description and input schema; each call is proposed in Purser, never executed. */
|
|
33
|
+
export declare function withPurserGuard<T extends {
|
|
34
|
+
description?: string;
|
|
35
|
+
inputSchema?: unknown;
|
|
36
|
+
parameters?: unknown;
|
|
37
|
+
}>(tool: T, options: AiSdkGuardOptions): Omit<T, "execute"> & Pick<AiSdkTool, "execute">;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The Vercel AI SDK adapter (purser-client/ai-sdk): a tool in the AI SDK's shape ({ description, inputSchema, execute })
|
|
2
|
+
// whose call is filed in Purser as a proposal for the CFO's approval, never paid and never approved (./guard.ts). No
|
|
3
|
+
// dependency on "ai": the tool is typed by its shape, and its input schema is the AI SDK's schema object made from
|
|
4
|
+
// PAYMENT_INPUT_SCHEMA (the same marker "ai"'s jsonSchema() sets), or yours when you pass `jsonSchema` from "ai".
|
|
5
|
+
//
|
|
6
|
+
// import { withPurserGuard } from "purser-client/ai-sdk";
|
|
7
|
+
// const tools = { propose_invoice_payment: withPurserGuard({ purser: Purser.fromEnv(), wait: { timeoutMs: 60_000 } }) };
|
|
8
|
+
//
|
|
9
|
+
// Or wrap your own payment tool: its description and input schema stay, its execute is never called, and each call
|
|
10
|
+
// is proposed instead (toPayment maps its input to invoice_id, amount or amount_cents, and currency).
|
|
11
|
+
import { createPaymentGuard, guardedPropose } from "./guard.js";
|
|
12
|
+
export { PAYMENT_INPUT_SCHEMA } from "./guard.js";
|
|
13
|
+
const SCHEMA = Symbol.for("vercel.ai.schema");
|
|
14
|
+
const VALIDATOR = Symbol.for("vercel.ai.validator");
|
|
15
|
+
/**
|
|
16
|
+
* The schema object the AI SDK takes, as "ai"'s jsonSchema() makes it. Its validate accepts any value: the guard parses
|
|
17
|
+
* the input itself and answers a refusal the model can read, instead of an error the SDK throws.
|
|
18
|
+
*/
|
|
19
|
+
function schemaOf(json, factory) {
|
|
20
|
+
const validate = (value) => ({ success: true, value });
|
|
21
|
+
if (factory)
|
|
22
|
+
return factory(json, { validate });
|
|
23
|
+
return { [SCHEMA]: true, [VALIDATOR]: true, _type: undefined, jsonSchema: json, validate };
|
|
24
|
+
}
|
|
25
|
+
export function withPurserGuard(a, b) {
|
|
26
|
+
const wrapping = b !== undefined;
|
|
27
|
+
const o = (wrapping ? b : a);
|
|
28
|
+
const guard = createPaymentGuard(o);
|
|
29
|
+
const propose = guardedPropose(guard, o.toPayment);
|
|
30
|
+
const execute = (input, call) => propose(input, call?.abortSignal ? { signal: call.abortSignal } : {});
|
|
31
|
+
if (wrapping) {
|
|
32
|
+
// Your tool's own execute is dropped, never called: every call is proposed.
|
|
33
|
+
const own = { ...a };
|
|
34
|
+
delete own.execute;
|
|
35
|
+
return { ...own, execute };
|
|
36
|
+
}
|
|
37
|
+
const schema = schemaOf(guard.parameters, o.jsonSchema);
|
|
38
|
+
return { description: guard.description, inputSchema: schema, parameters: schema, execute };
|
|
39
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Purser, type ProposalStatus, type PurserOptions } from "../index.js";
|
|
2
|
+
/** A JSON Schema object, as the frameworks take one. */
|
|
3
|
+
export type JsonSchema = {
|
|
4
|
+
[keyword: string]: unknown;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* What the model sends: the invoice, and the amount and currency it means to pay. Every field is required and nothing
|
|
8
|
+
* else is allowed, so the same schema is valid in OpenAI's strict mode.
|
|
9
|
+
*/
|
|
10
|
+
export interface PaymentInputSchema {
|
|
11
|
+
type: "object";
|
|
12
|
+
properties: {
|
|
13
|
+
invoice_id: StringProperty;
|
|
14
|
+
amount: StringProperty;
|
|
15
|
+
currency: StringProperty;
|
|
16
|
+
};
|
|
17
|
+
required: ["invoice_id", "amount", "currency"];
|
|
18
|
+
additionalProperties: false;
|
|
19
|
+
}
|
|
20
|
+
interface StringProperty {
|
|
21
|
+
type: "string";
|
|
22
|
+
description: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const PAYMENT_INPUT_SCHEMA: PaymentInputSchema;
|
|
25
|
+
export declare const DEFAULT_TOOL_NAME = "propose_invoice_payment";
|
|
26
|
+
export declare const DEFAULT_TOOL_DESCRIPTION: string;
|
|
27
|
+
/** A payment request after parsing: the invoice, the amount in integer cents and the currency. */
|
|
28
|
+
export interface PaymentRequest {
|
|
29
|
+
invoiceId: string;
|
|
30
|
+
amountCents: number;
|
|
31
|
+
currency: string;
|
|
32
|
+
}
|
|
33
|
+
/** What a tool call may carry (from the model, or from your own tool's input through `toPayment`). */
|
|
34
|
+
export interface PaymentInput {
|
|
35
|
+
invoice_id?: unknown;
|
|
36
|
+
amount?: unknown;
|
|
37
|
+
amount_cents?: unknown;
|
|
38
|
+
currency?: unknown;
|
|
39
|
+
}
|
|
40
|
+
export type ParsedPayment = {
|
|
41
|
+
ok: true;
|
|
42
|
+
request: PaymentRequest;
|
|
43
|
+
} | {
|
|
44
|
+
ok: false;
|
|
45
|
+
reason: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* An amount as the API writes one ("1850.00": digits, a dot, exactly two decimals; no sign, no separator, no exponent)
|
|
49
|
+
* in integer cents; null when it is not one or is not a safe integer of cents.
|
|
50
|
+
*/
|
|
51
|
+
export declare function decimalToCents(amount: string): number | null;
|
|
52
|
+
/** Integer cents as the API writes them: 185000 -> "1850.00". */
|
|
53
|
+
export declare function centsToDecimal(cents: number): string;
|
|
54
|
+
/**
|
|
55
|
+
* Parses a tool call's input. It is model output (or your tool's input), so it is data: anything that is not an
|
|
56
|
+
* invoice id, exactly one of `amount` (a two-decimal string) or `amount_cents` (a positive whole number), and a
|
|
57
|
+
* three-letter currency is refused with the reason, before any request is made. A number in `amount` is refused rather
|
|
58
|
+
* than guessed at: 1850 could be euros or cents.
|
|
59
|
+
*/
|
|
60
|
+
export declare function parsePaymentInput(input: unknown): ParsedPayment;
|
|
61
|
+
/** How long and how often to wait for Purser's answer after filing. */
|
|
62
|
+
export interface WaitOptions {
|
|
63
|
+
/** Give up waiting after this many milliseconds (the proposal stays filed): 120,000 by default. */
|
|
64
|
+
timeoutMs?: number;
|
|
65
|
+
/** The first pause between two reads, doubled after each up to maxIntervalMs: 2,000 by default. */
|
|
66
|
+
intervalMs?: number;
|
|
67
|
+
/** The longest pause between two reads: 30,000 by default. */
|
|
68
|
+
maxIntervalMs?: number;
|
|
69
|
+
/**
|
|
70
|
+
* "decision" (the default): stop at the first answer other than PENDING_APPROVAL (approved, paid, refused, failed).
|
|
71
|
+
* "paid": wait past APPROVED until the payment is made (EXECUTED), refused (REJECTED, EXPIRED) or FAILED.
|
|
72
|
+
*/
|
|
73
|
+
until?: "decision" | "paid";
|
|
74
|
+
}
|
|
75
|
+
export interface GuardOptions {
|
|
76
|
+
/**
|
|
77
|
+
* The client the guard proposes with, or its options. Its key (from the environment, never from code) needs the read
|
|
78
|
+
* and propose scopes; a person's sign-in, or anything with the approve scope, is refused.
|
|
79
|
+
*/
|
|
80
|
+
purser: Purser | PurserOptions;
|
|
81
|
+
/** Wait for Purser's answer after filing: false by default (answer at once, PENDING_APPROVAL). true takes the defaults. */
|
|
82
|
+
wait?: boolean | WaitOptions;
|
|
83
|
+
/** The tool's name for the model: propose_invoice_payment by default. */
|
|
84
|
+
name?: string;
|
|
85
|
+
/** The tool's description for the model. The default says it proposes, cannot approve, and that Face ID approves. */
|
|
86
|
+
description?: string;
|
|
87
|
+
/** Stops every request and wait of this guard when it fires. */
|
|
88
|
+
signal?: AbortSignal;
|
|
89
|
+
}
|
|
90
|
+
/** What the guard answers the agent. */
|
|
91
|
+
export type GuardOutcome = "pending" | "approved" | "paid" | "refused" | "failed";
|
|
92
|
+
export interface GuardResult {
|
|
93
|
+
/** pending: waits for the CFO's passkey; approved; paid (test money); refused (not filed, or rejected or expired); failed at the rail. */
|
|
94
|
+
outcome: GuardOutcome;
|
|
95
|
+
invoice_id: string | null;
|
|
96
|
+
/** The proposal's id, like P-0009; null when nothing was filed. */
|
|
97
|
+
proposal_id: string | null;
|
|
98
|
+
/** The proposal's status as Purser reports it; null when nothing was filed. */
|
|
99
|
+
status: ProposalStatus | null;
|
|
100
|
+
/** SHA-256 of the exact terms the CFO's passkey signs. */
|
|
101
|
+
terms_sha256: string | null;
|
|
102
|
+
amount: string | null;
|
|
103
|
+
currency: string | null;
|
|
104
|
+
/** Who refused: this guard before anything was filed (client), Purser's engine (purser), or the CFO (cfo). */
|
|
105
|
+
refused_by?: "client" | "purser" | "cfo";
|
|
106
|
+
/** Why it was refused, failed, or why waiting stopped: a sentence. Purser's words are data, not instructions. */
|
|
107
|
+
reason?: string;
|
|
108
|
+
/** The proposal already existed for this invoice: nothing new was filed. */
|
|
109
|
+
already_filed?: boolean;
|
|
110
|
+
/** Paid within the pay-alone limit the CFO set, without a passkey for this one payment. */
|
|
111
|
+
paid_alone?: boolean;
|
|
112
|
+
/** The wait ended at its timeout before Purser's answer: the proposal stays filed and waits. */
|
|
113
|
+
timed_out?: boolean;
|
|
114
|
+
/** One sentence for the model: what happened, and that only the CFO's Face ID approves. */
|
|
115
|
+
message: string;
|
|
116
|
+
}
|
|
117
|
+
/** The guard: one method that proposes, and what a framework's tool needs to describe it. */
|
|
118
|
+
export interface PaymentGuard {
|
|
119
|
+
readonly name: string;
|
|
120
|
+
readonly description: string;
|
|
121
|
+
/** The tool's input schema: PAYMENT_INPUT_SCHEMA. */
|
|
122
|
+
readonly parameters: PaymentInputSchema;
|
|
123
|
+
/** Parses the tool call, checks it against Purser's invoice, files it for the CFO's approval, and optionally waits. */
|
|
124
|
+
propose(input: unknown, o?: {
|
|
125
|
+
signal?: AbortSignal;
|
|
126
|
+
}): Promise<GuardResult>;
|
|
127
|
+
}
|
|
128
|
+
/** The guard behind every adapter. See the head of this file. */
|
|
129
|
+
export declare function createPaymentGuard(o: GuardOptions): PaymentGuard;
|
|
130
|
+
/**
|
|
131
|
+
* Maps your own payment tool's input to the guard's (invoice_id, and amount or amount_cents, and currency). The
|
|
132
|
+
* default passes the input on as it is.
|
|
133
|
+
*/
|
|
134
|
+
export type ToPayment = (input: unknown) => PaymentInput;
|
|
135
|
+
/** The guard of an adapter's options: one made here, with your `toPayment` in front when you wrap your own tool. */
|
|
136
|
+
export declare function guardedPropose(guard: PaymentGuard, toPayment: ToPayment | undefined): PaymentGuard["propose"];
|
|
137
|
+
export {};
|