@agent-cards/commerce-sdk 0.5.0 → 0.8.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 +80 -9
- package/dist/ack.d.ts +22 -6
- package/dist/ack.js +59 -11
- package/dist/buy-tool.d.ts +47 -3
- package/dist/buy-tool.js +124 -21
- package/dist/client.d.ts +60 -6
- package/dist/client.js +144 -12
- package/dist/definition.js +1 -0
- package/dist/events.d.ts +12 -0
- package/dist/events.js +79 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -2
- package/dist/listen.d.ts +93 -0
- package/dist/listen.js +496 -0
- package/dist/model-view.d.ts +12 -4
- package/dist/model-view.js +36 -9
- package/package.json +2 -2
package/dist/events.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every event type Agentcard sends a company, the same list a webhook endpoint subscribes to by
|
|
3
|
+
* name. The stream (`agentcard.listen`) and a webhook endpoint carry the same types, with the same
|
|
4
|
+
* envelope and the same ids. A test holds this list to the backend's catalog.
|
|
5
|
+
*/
|
|
6
|
+
export const AGENTCARD_EVENT_TYPES = [
|
|
7
|
+
'card.created',
|
|
8
|
+
'card.updated',
|
|
9
|
+
'card.closed',
|
|
10
|
+
'cardholder.created',
|
|
11
|
+
'cardholder.updated',
|
|
12
|
+
'transaction.authorized',
|
|
13
|
+
'transaction.declined',
|
|
14
|
+
'transaction.cleared',
|
|
15
|
+
'transaction.voided',
|
|
16
|
+
'balance.low',
|
|
17
|
+
'cardholder_onboarding_session.completed',
|
|
18
|
+
'platform_connect_session.completed',
|
|
19
|
+
'merchant.connected',
|
|
20
|
+
'order.placed',
|
|
21
|
+
'order.confirmed',
|
|
22
|
+
'order.failed',
|
|
23
|
+
'order.updated',
|
|
24
|
+
'purchase.message',
|
|
25
|
+
'connection.created',
|
|
26
|
+
'connected_card.updated',
|
|
27
|
+
'vault.card_stored',
|
|
28
|
+
'vault.session_linked',
|
|
29
|
+
'vault.session_card_picked',
|
|
30
|
+
'vault.payment_permission.updated',
|
|
31
|
+
'action.confirmed',
|
|
32
|
+
'action.done',
|
|
33
|
+
'checkout_authorization.approved',
|
|
34
|
+
'checkout_authorization.submitted',
|
|
35
|
+
'checkout_authorization.declined',
|
|
36
|
+
'checkout_authorization.cancelled',
|
|
37
|
+
'checkout_authorization.expired',
|
|
38
|
+
'checkout_authorization.duplicate_charge',
|
|
39
|
+
'checkout_authorization.refused',
|
|
40
|
+
'checkout_authorization.watched',
|
|
41
|
+
'checkout_authorization.amount_mismatch',
|
|
42
|
+
'checkout_authorization.settled',
|
|
43
|
+
'checkout_authorization.settlement_unconfirmed',
|
|
44
|
+
'checkout_authorization.outcome_reported',
|
|
45
|
+
'card_flow.started',
|
|
46
|
+
'card_flow.failed',
|
|
47
|
+
'transfer.approved',
|
|
48
|
+
'transfer.initiated',
|
|
49
|
+
'transfer.completed',
|
|
50
|
+
'transfer.failed',
|
|
51
|
+
'transfer.released',
|
|
52
|
+
'wallet.funded',
|
|
53
|
+
'user_wallet.funding_detected',
|
|
54
|
+
'user_wallet.funded',
|
|
55
|
+
'wallet.balance.low',
|
|
56
|
+
'recovery.requested',
|
|
57
|
+
'recovery.completed',
|
|
58
|
+
'recovery.rejected',
|
|
59
|
+
'wallet.withdrawal.initiated',
|
|
60
|
+
'wallet.withdrawal.completed',
|
|
61
|
+
'wallet.withdrawal.failed',
|
|
62
|
+
'identity.verification.updated',
|
|
63
|
+
'approval.requested',
|
|
64
|
+
'reward.earned',
|
|
65
|
+
'reward.reversed',
|
|
66
|
+
'wallet_link.opened',
|
|
67
|
+
];
|
|
68
|
+
const KNOWN = new Set(AGENTCARD_EVENT_TYPES);
|
|
69
|
+
const FAMILIES = new Set(AGENTCARD_EVENT_TYPES.map((t) => `${t.split('.')[0]}.*`));
|
|
70
|
+
/** Whether a filter entry names something Agentcard sends: an event type, a family such as `order.*`, or `*`. */
|
|
71
|
+
export function isEventPattern(pattern) {
|
|
72
|
+
return pattern === '*' || KNOWN.has(pattern) || FAMILIES.has(pattern);
|
|
73
|
+
}
|
|
74
|
+
/** Whether an event type matches a filter entry, as a webhook subscription matches it. */
|
|
75
|
+
export function matchesEventPattern(type, pattern) {
|
|
76
|
+
if (pattern === '*' || pattern === type)
|
|
77
|
+
return true;
|
|
78
|
+
return pattern.endsWith('.*') && type.startsWith(pattern.slice(0, -1));
|
|
79
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,10 +2,12 @@ export { verifyAgentcardWebhook, AgentcardSignatureError, type AgentcardSignatur
|
|
|
2
2
|
export { readPurchaseMessage, type PurchaseMessage, type PurchaseMessageKind, type PurchaseFacts, } from './message.js';
|
|
3
3
|
export { readShopperMessage, isStale, SHOPPER_EVENT_TYPES, type ShopperMessage, type ShopperMessageKind, type ShopperFacts, type ShopperEventType, type OrderStatus, } from './shopper-message.js';
|
|
4
4
|
export { agentcardWebhook, purchaseAgentWebhook, type AgentcardWebhookOptions, type AgentcardWebhookHandler, type PurchaseAgentWebhookOptions, type WebhookFetchRequest, type WebhookRequest, type WebhookResponse, } from './handler.js';
|
|
5
|
-
export { AgentcardClient, AgentcardApiError, BUY_TURN_TIMEOUT_MS, DEFAULT_BASE_URL, type AgentcardClientOptions, type FetchLike, type Shopper, type BuyRequest, type BuyDeliveryAddress, type BuyTurn, type BuyStatus, type BuyCart, type BuyCartLine, type BuyStore, type BuyCatalog, type BuyCatalogItem, type BuyFlightItinerary, type BuyPaymentSource, type BuyConversation, type BuyLastCheckout, type BuyOrder, type VaultLink, type BuyNextAction, type BuyPick, } from './client.js';
|
|
5
|
+
export { AgentcardClient, AgentcardApiError, BUY_TURN_TIMEOUT_MS, DEFAULT_BASE_URL, type AgentcardClientOptions, type FetchLike, type FetchLikeResponse, type BuyProgress, type BuyProgressStage, type BuyCatalogUpdate, type BuyStreamHandlers, type Shopper, type BuyRequest, type BuyDeliveryAddress, type BuyTurn, type BuyStatus, type BuyCart, type BuyCartLine, type BuyStore, type BuyCatalog, type BuyCatalogItem, type BuyFlightItinerary, type BuyPaymentSource, type BuyConversation, type BuyLastCheckout, type BuyOrder, type VaultLink, type BuyNextAction, type BuyPick, } from './client.js';
|
|
6
6
|
export { BUY_TOOL_NAME, BUY_TOOL_DESCRIPTION, buyToolDefinition, parseBuyToolInput, type BuyToolDefinition, type BuyToolInput, type BuyToolInputSchema, type BuyToolInputCheck, type BuyToolStandardSchema, type OpenAIBuyTool, } from './definition.js';
|
|
7
7
|
export { toMessages, messageStream, type MessageStream, type MessageStreamOptions, } from './messages.js';
|
|
8
8
|
export { buyTool, type BuyTool, type BuyToolOptions, type BuyToolResult, type BuyToolStatus, type BuyToolLink, type BuyToolProduct, } from './buy-tool.js';
|
|
9
|
-
export { ackFor, type AckForOptions } from './ack.js';
|
|
10
|
-
export { forModel, stripLinks, type BuyToolModelView, type BuyToolModelCart, type BuyToolModelProduct, } from './model-view.js';
|
|
9
|
+
export { instantAck, ackFor, type AckForOptions } from './ack.js';
|
|
10
|
+
export { forModel, forModelError, stripLinks, type BuyToolModelView, type BuyToolModelCart, type BuyToolModelProduct, } from './model-view.js';
|
|
11
11
|
export { toAnthropicTool, toOpenRouterTool, type AnthropicBuyTool, type OpenRouterToolkit, type ZodLike, } from './adapters.js';
|
|
12
|
+
export { listenToAgentcard, type AgentcardListener, type ListenOptions, type ListenCursorStore, type StreamFetch, } from './listen.js';
|
|
13
|
+
export { AGENTCARD_EVENT_TYPES, type AgentcardEventType, } from './events.js';
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,8 @@ export { AgentcardClient, AgentcardApiError, BUY_TURN_TIMEOUT_MS, DEFAULT_BASE_U
|
|
|
6
6
|
export { BUY_TOOL_NAME, BUY_TOOL_DESCRIPTION, buyToolDefinition, parseBuyToolInput, } from './definition.js';
|
|
7
7
|
export { toMessages, messageStream, } from './messages.js';
|
|
8
8
|
export { buyTool, } from './buy-tool.js';
|
|
9
|
-
export { ackFor } from './ack.js';
|
|
10
|
-
export { forModel, stripLinks, } from './model-view.js';
|
|
9
|
+
export { instantAck, ackFor } from './ack.js';
|
|
10
|
+
export { forModel, forModelError, stripLinks, } from './model-view.js';
|
|
11
11
|
export { toAnthropicTool, toOpenRouterTool, } from './adapters.js';
|
|
12
|
+
export { listenToAgentcard, } from './listen.js';
|
|
13
|
+
export { AGENTCARD_EVENT_TYPES, } from './events.js';
|
package/dist/listen.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hear every Agentcard event without a webhook endpoint. Your server opens one connection to
|
|
3
|
+
* Agentcard with your company credentials, and Agentcard streams your company's events down it:
|
|
4
|
+
* no public URL, no webhook secret, no signature to check. Each event is the envelope a webhook
|
|
5
|
+
* endpoint would receive, with the same id.
|
|
6
|
+
*
|
|
7
|
+
* The listener reconnects on its own, resumes after the last event it handled, skips an event it
|
|
8
|
+
* already handled, and falls back to asking every few seconds when the stream is not available.
|
|
9
|
+
* Shopper messages go through the same reader as `agentcardWebhook` (`readShopperMessage`), so
|
|
10
|
+
* `onMessage` gets the same ready-to-send text.
|
|
11
|
+
*/
|
|
12
|
+
import { type AgentcardEvent } from './verify.js';
|
|
13
|
+
import { type ShopperMessage } from './shopper-message.js';
|
|
14
|
+
import { type AgentcardEventType } from './events.js';
|
|
15
|
+
import { type AgentcardClient } from './client.js';
|
|
16
|
+
/** Where the listener keeps the cursor of the last event it handled, so a restarted server resumes there. */
|
|
17
|
+
export interface ListenCursorStore {
|
|
18
|
+
/** The cursor saved last, or null to start from now. */
|
|
19
|
+
load(): Promise<string | null> | string | null;
|
|
20
|
+
/** Called after each handled event, and at most once a minute while no event arrives. */
|
|
21
|
+
save(cursor: string): Promise<void> | void;
|
|
22
|
+
}
|
|
23
|
+
/** The fetch the listener streams with: the global fetch of Node 18 and later. */
|
|
24
|
+
export type StreamFetch = (url: string, init: {
|
|
25
|
+
method: 'GET';
|
|
26
|
+
headers: Record<string, string>;
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}) => Promise<{
|
|
29
|
+
status: number;
|
|
30
|
+
ok: boolean;
|
|
31
|
+
headers: {
|
|
32
|
+
get(name: string): string | null;
|
|
33
|
+
};
|
|
34
|
+
body: {
|
|
35
|
+
getReader(): {
|
|
36
|
+
read(): Promise<{
|
|
37
|
+
done: boolean;
|
|
38
|
+
value?: Uint8Array;
|
|
39
|
+
}>;
|
|
40
|
+
cancel(reason?: unknown): Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
} | null;
|
|
43
|
+
text(): Promise<string>;
|
|
44
|
+
}>;
|
|
45
|
+
export interface ListenOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Receives each event for one of your shoppers, under your own id for them, with the message
|
|
48
|
+
* ready to post into that shopper's chat: the same as `agentcardWebhook`'s `onMessage`. A message
|
|
49
|
+
* whose link or window closed before it arrived reaches no shopper: its event goes to `onEvent`.
|
|
50
|
+
*/
|
|
51
|
+
onMessage?: (externalUserId: string, message: ShopperMessage, event: AgentcardEvent<AgentcardEventType>) => void | Promise<void>;
|
|
52
|
+
/** Receives every event `onMessage` does not take: every event, when you pass no `onMessage`. */
|
|
53
|
+
onEvent?: (event: AgentcardEvent<AgentcardEventType | (string & {})>) => void | Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Receives what went wrong: a callback that threw five times, after which the listener moves on,
|
|
56
|
+
* a lost connection it is about to retry, a cursor Agentcard no longer keeps. A reporter that
|
|
57
|
+
* throws is ignored. Default: `console.error`.
|
|
58
|
+
*/
|
|
59
|
+
onError?: (error: unknown, event?: AgentcardEvent) => void | Promise<void>;
|
|
60
|
+
/** Only these events: event types, families such as `order.*`, or `*`. Default: every event. */
|
|
61
|
+
events?: string[];
|
|
62
|
+
/** Keeps the cursor across restarts. Default: in memory, so a new process starts from now. */
|
|
63
|
+
cursor?: ListenCursorStore;
|
|
64
|
+
/** `auto` streams and falls back to polling when the stream is not available. Default `auto`. */
|
|
65
|
+
transport?: 'auto' | 'stream' | 'poll';
|
|
66
|
+
/** How long polling waits between reads. Default 2000. */
|
|
67
|
+
pollIntervalMs?: number;
|
|
68
|
+
/** How long the stream may stay silent before the listener reconnects. Agentcard sends a heartbeat every 15 seconds. Default 45000. */
|
|
69
|
+
heartbeatTimeoutMs?: number;
|
|
70
|
+
/** How long one polling read may take before the listener gives up on it and reads again. Default 30000. */
|
|
71
|
+
pollTimeoutMs?: number;
|
|
72
|
+
/** The fetch to stream with. Default: the global fetch. */
|
|
73
|
+
fetch?: StreamFetch;
|
|
74
|
+
}
|
|
75
|
+
/** A running listener. */
|
|
76
|
+
export interface AgentcardListener {
|
|
77
|
+
/** Resolves once the listener first reaches Agentcard, or once it closes. */
|
|
78
|
+
readonly ready: Promise<void>;
|
|
79
|
+
/** How the listener hears Agentcard right now. */
|
|
80
|
+
readonly transport: 'stream' | 'poll';
|
|
81
|
+
/** The cursor of the last event handled, or null before the first connection. */
|
|
82
|
+
readonly cursor: string | null;
|
|
83
|
+
/** Stops listening. Resolves once the callback that is running, if any, has returned. */
|
|
84
|
+
close(): Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Listen to every event Agentcard sends your company, over one connection your server opens.
|
|
88
|
+
* Build `agentcard` with your company's client credentials. Returns at once; `ready` resolves once
|
|
89
|
+
* the listener reaches Agentcard. Each event runs its callback once, in order, and the next event
|
|
90
|
+
* waits for it: a callback that throws runs again, up to five times, before the listener reports
|
|
91
|
+
* it and moves on. Run one listener per company and mode: two listeners each hear every event.
|
|
92
|
+
*/
|
|
93
|
+
export declare function listenToAgentcard(agentcard: AgentcardClient, opts: ListenOptions): AgentcardListener;
|