@itpay/cli 2.0.3 → 2.0.5

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.
Files changed (70) hide show
  1. package/README.md +92 -142
  2. package/dist/src/client/backend.js +26 -8
  3. package/dist/src/commands/buy.js +84 -132
  4. package/dist/src/commands/cart.js +274 -169
  5. package/dist/src/commands/catalog.js +63 -38
  6. package/dist/src/commands/checkout.js +117 -78
  7. package/dist/src/commands/docs.js +97 -51
  8. package/dist/src/commands/guidance.js +87 -15
  9. package/dist/src/commands/install.js +50 -87
  10. package/dist/src/commands/next.js +45 -0
  11. package/dist/src/commands/order.js +44 -69
  12. package/dist/src/commands/orders.js +43 -15
  13. package/dist/src/commands/pay.js +51 -22
  14. package/dist/src/commands/readyz.js +8 -4
  15. package/dist/src/commands/refund.js +132 -11
  16. package/dist/src/commands/services.js +719 -148
  17. package/dist/src/main.js +711 -193
  18. package/dist/src/render/output.js +2 -3
  19. package/dist/src/state/cart_session.js +13 -17
  20. package/dist/src/state/client_context.js +4 -2
  21. package/dist/src/state/config.js +2 -4
  22. package/dist/src/state/device_authority.js +1 -1
  23. package/docs/agent/buyer/cart-checkout.json +27 -83
  24. package/docs/agent/buyer/install-and-setup.json +23 -67
  25. package/docs/agent/buyer/orders-refunds.json +31 -53
  26. package/docs/agent/buyer/payment-flow.json +24 -57
  27. package/docs/agent/buyer/quickstart.json +39 -162
  28. package/docs/agent/buyer/render-hosts.json +43 -57
  29. package/docs/cli-reference/agent-types.md +45 -0
  30. package/docs/cli-reference/commands/buy.md +167 -0
  31. package/docs/cli-reference/commands/cart/add.md +86 -0
  32. package/docs/cli-reference/commands/cart/clear.md +53 -0
  33. package/docs/cli-reference/commands/cart/index.md +30 -0
  34. package/docs/cli-reference/commands/cart/next.md +71 -0
  35. package/docs/cli-reference/commands/cart/remove.md +53 -0
  36. package/docs/cli-reference/commands/cart/show.md +65 -0
  37. package/docs/cli-reference/commands/catalog/index.md +26 -0
  38. package/docs/cli-reference/commands/catalog/list.md +45 -0
  39. package/docs/cli-reference/commands/checkout.md +74 -0
  40. package/docs/cli-reference/commands/docs/index.md +28 -0
  41. package/docs/cli-reference/commands/docs/list.md +51 -0
  42. package/docs/cli-reference/commands/docs/search.md +69 -0
  43. package/docs/cli-reference/commands/docs/show.md +68 -0
  44. package/docs/cli-reference/commands/install.md +112 -0
  45. package/docs/cli-reference/commands/next.md +87 -0
  46. package/docs/cli-reference/commands/order.md +92 -0
  47. package/docs/cli-reference/commands/orders.md +83 -0
  48. package/docs/cli-reference/commands/pay.md +103 -0
  49. package/docs/cli-reference/commands/readyz.md +39 -0
  50. package/docs/cli-reference/commands/refund/cancel.md +62 -0
  51. package/docs/cli-reference/commands/refund/create.md +85 -0
  52. package/docs/cli-reference/commands/refund/get.md +60 -0
  53. package/docs/cli-reference/commands/refund/index.md +33 -0
  54. package/docs/cli-reference/commands/refund/list.md +68 -0
  55. package/docs/cli-reference/commands/refund/watch.md +73 -0
  56. package/docs/cli-reference/commands/services/action.md +43 -0
  57. package/docs/cli-reference/commands/services/checkout.md +82 -0
  58. package/docs/cli-reference/commands/services/events.md +73 -0
  59. package/docs/cli-reference/commands/services/get.md +66 -0
  60. package/docs/cli-reference/commands/services/index.md +45 -0
  61. package/docs/cli-reference/commands/services/invoke.md +67 -0
  62. package/docs/cli-reference/commands/services/list.md +61 -0
  63. package/docs/cli-reference/commands/services/next.md +162 -0
  64. package/docs/cli-reference/commands/services/quote.md +59 -0
  65. package/docs/cli-reference/commands/services/read-result.md +98 -0
  66. package/docs/cli-reference/commands/services/start.md +53 -0
  67. package/docs/cli-reference/conventions.md +94 -0
  68. package/docs/cli-reference/index.md +64 -0
  69. package/package.json +1 -1
  70. package/skills/itpay-buyer/SKILL.md +47 -113
@@ -2,191 +2,68 @@
2
2
  "schema_version": "itp.agent_doc.v1",
3
3
  "role": "buyer",
4
4
  "topic": "quickstart",
5
- "title": "ItPay V3 CLI Buyer Quickstart",
6
- "purpose": "Teach an external agent the minimal safe path for using the current V3 itpay CLI package.",
5
+ "title": "ItPay CLI Buyer Quickstart",
6
+ "purpose": "Run the shortest safe discovery, Service Execution, Checkout, delivery, and refund flow using server-returned next commands.",
7
7
  "when_to_use": [
8
- "The agent has just loaded the V3 npm package.",
9
- "The user wants the agent to add a catalog item to the canonical server cart or create a checkout through the current V3 CLI.",
10
- "The agent needs to know which commands exist right now."
8
+ "The agent has just installed this CLI version.",
9
+ "The user asks what ItPay can do or asks to use a published service."
11
10
  ],
12
11
  "required_state": {
13
12
  "needs": [
14
- "itpay CLI installed",
15
- "production app at https://app.itpay.ai, or an intentional ITPAY_BACKEND_URL override",
16
- "catalog item id, variant id, and offer id from `itpay catalog list`",
17
- "real agent runtime type supplied through --agent-type or ITPAY_AGENT_TYPE"
13
+ "the real supported Agent Type",
14
+ "the default API or an intentional ITPAY_BACKEND_URL override"
18
15
  ],
19
- "must_not_need": [
20
- "admin token",
21
- "claim token",
22
- "provider raw payload"
23
- ]
16
+ "must_not_need": ["Admin access", "Provider secrets", "raw payload access"]
24
17
  },
25
18
  "commands": [
26
19
  {
27
- "intent": "check backend readiness",
28
- "command": "itpay readyz",
29
- "success_signal": "the production backend reports ready and accepts this CLI contract"
20
+ "intent": "verify compatibility",
21
+ "command": "itpay --agent-type <agent_type> readyz --json",
22
+ "success_signal": "status is ready and next points to catalog list"
30
23
  },
31
24
  {
32
- "intent": "browse available services",
25
+ "intent": "discover published services",
33
26
  "command": "itpay --agent-type <agent_type> catalog list --json",
34
- "success_signal": "catalog items with variants and prices are displayed"
27
+ "success_signal": "a compact service journey, service_id, variants and prices are returned"
35
28
  },
36
29
  {
37
- "intent": "start a catalog service without hardcoding its workflow",
38
- "command": "itpay --agent-type <agent_type> services start <service_id>",
39
- "success_signal": "a service_execution_id and server-derived next actions are displayed"
30
+ "intent": "start a generic service",
31
+ "command": "itpay --agent-type <agent_type> services start <service_id> --json",
32
+ "success_signal": "one service_execution_id, one preferred capability summary and one executable next command are returned"
40
33
  },
41
34
  {
42
- "intent": "recover the next step from remembered server handles",
43
- "command": "itpay next --json",
44
- "success_signal": "JSON includes next_actions derived from server cart, service execution, or checkout handles"
35
+ "intent": "continue after every step or interruption",
36
+ "command": "<execute the exact next.command returned by the previous CLI envelope>",
37
+ "success_signal": "the next envelope reports one current state and at most one preferred action"
45
38
  },
46
39
  {
47
- "intent": "add one line to the canonical server cart",
48
- "command": "itpay cart add --item <catalog_item_id> --variant <catalog_variant_id> --offer <offer_id> --quantity 1 --host <client> --json",
49
- "success_signal": "JSON includes cart_id, cart_item_id, service_execution_id when the line is service-backed, and top-level next_actions"
50
- },
51
- {
52
- "intent": "read the next cart or service-backed action",
53
- "command": "itpay cart next --json",
54
- "success_signal": "JSON includes next_actions such as services invoke, services action, services checkout, or buy"
55
- },
56
- {
57
- "intent": "show the current canonical server cart",
58
- "command": "itpay cart show",
59
- "success_signal": "the CLI prints persisted server cart items and service refs when present"
60
- },
61
- {
62
- "intent": "remove a pre-checkout line if the human changes their mind",
63
- "command": "itpay cart remove --line <cart_item_id>",
64
- "success_signal": "the server cart is updated; quote-locked or checkout-bound lines return a conflict"
65
- },
66
- {
67
- "intent": "invoke an agent-visible service capability after a service-backed cart or service start",
68
- "command": "itpay services invoke <service_execution_id> --capability <capability_id> --input key=value",
69
- "success_signal": "JSON with redacted execution status, safe result items, and next_actions"
70
- },
71
- {
72
- "intent": "submit a human/agent service action when required by the service contract",
73
- "command": "itpay services action <service_execution_id> --action <action_type> --result-item <result_item_id>",
74
- "success_signal": "JSON with redacted execution status, accepted action, and next_actions"
75
- },
76
- {
77
- "intent": "read the next Service Execution action",
78
- "command": "itpay services next <service_execution_id> --json",
79
- "success_signal": "JSON includes next_actions derived from the Service Execution read model and capabilities"
80
- },
81
- {
82
- "intent": "create checkout from a confirmed service execution and render human handoff",
83
- "command": "itpay services checkout <service_execution_id> --capability <paid_capability_id> [--email <email>] --json",
84
- "success_signal": "quote lock is bound to the server cart item when present; JSON includes checkout_id, display_token, checkout_url, qr_png_url, and brand_qr_local_path"
85
- },
86
- {
87
- "intent": "recover a lost or expired service checkout handoff",
88
- "command": "itpay services checkout <service_execution_id> --resume --json",
89
- "success_signal": "the same checkout_id is returned with a fresh short-lived display token and QR path"
90
- },
91
- {
92
- "intent": "create checkout from a server cart that is ready for checkout",
93
- "command": "itpay buy --cart <cart_id> --host terminal --contact-email <email>",
94
- "success_signal": "checkout QR/URL rendered for the host"
95
- },
96
- {
97
- "intent": "inspect checkout state",
98
- "command": "itpay checkout --id <checkout_id> --token <display_token>",
99
- "success_signal": "checkout presentation with status, items, payment intents"
100
- },
101
- {
102
- "intent": "read an order",
103
- "command": "itpay order <order_id>",
104
- "success_signal": "order details with items and delivery artifacts"
105
- },
106
- {
107
- "intent": "read a protected result after the human grants temporary access",
108
- "command": "itpay --agent-type <agent_type> services read-result <service_execution_id>",
109
- "success_signal": "the payload for this service execution is returned while its human grant is active"
110
- },
111
- {
112
- "intent": "list account orders (needs ITPAY_BEARER_TOKEN)",
113
- "command": "itpay orders --limit 20",
114
- "success_signal": "list of orders"
115
- },
116
- {
117
- "intent": "show install instructions",
118
- "command": "itpay install [claude-code|codex|terminal|telegram|feishu]",
119
- "success_signal": "step-by-step instructions for the target host"
120
- },
121
- {
122
- "intent": "browse agent documentation",
123
- "command": "itpay docs list",
124
- "success_signal": "list of available doc topics"
40
+ "intent": "recover from local handles",
41
+ "command": "itpay --agent-type <agent_type> next --json",
42
+ "success_signal": "one Service Execution, Checkout, Cart, or Catalog recovery command is returned"
125
43
  }
126
44
  ],
127
45
  "agent_rules": [
128
- "Every human-facing command should include the correct --host value.",
129
- "For Telegram, Feishu, and Lark, provide --target or the CLI will reject.",
130
- "Use --json for machine-readable output in agent flows.",
131
- "Supply the real agent runtime type on commerce commands; the CLI persists one signed device identity and registered agent instances under ~/.itpay-v3/device.",
132
- "Prefer top-level next_actions over hardcoded service-specific command sequences.",
133
- "Use itpay next or itpay cart next to recover after interruption; these commands read server handles and do not treat local cart files as business truth.",
134
- "For service-backed catalog items, read service_execution_id from `itpay cart add --json` before invoking service capabilities.",
135
- "Do not use --pay or itpay pay for the normal buyer flow; first show the ItPay checkout QR/URL so the human enters the checkout page.",
136
- "If buy emits an interaction request for missing contact fields, collect values and rerun with --contact-email/--contact-phone.",
137
- "For services checkout, include --email only when next_actions includes it. Explain that protected delivery uses it to send the claim link; agent-visible paid results do not require email.",
138
- "Do not assume checkout implies payment succeeded; it only creates a handoff.",
139
- "Payment success is confirmed by backend checkout/order state, not by QR display or buyer claim.",
140
- "A human grant applies only to its service execution and expires after 15 minutes; do not claim access to other orders or executions.",
141
- "Use `itpay catalog list` to browse available services before buying.",
142
- "Use `itpay docs show <topic>` to get detailed instructions on a specific topic.",
143
- "Server cart, quota, execution, quote, checkout, and delivery facts persist in backend Postgres. ~/.itpay-v3/cart.json only caches last ids and explicit local drafts."
46
+ "Use the CLI as the control surface; do not call ItPay APIs directly or hardcode a service workflow.",
47
+ "Treat result as current facts, instruction as how to use those facts, next as the preferred executable action, and recovery as exceptional paths.",
48
+ "Run one state-changing command at a time and execute the exact next.command unchanged after filling only required user data.",
49
+ "Start a separate Service Execution for each independent service intent; quota remains shared according to Backend identity policy.",
50
+ "Ask the user for required email or contact values and explain their purpose; never invent them.",
51
+ "Agent-visible delivery is returned by services next. Protected delivery is read only by services read-result while a human grant is active.",
52
+ "A human grant is scoped to one delivery, its frozen authorized Agent audience, approved fields, and a 15-minute expiry.",
53
+ "Normal payment uses the ItPay Checkout handoff, not itpay pay."
144
54
  ],
145
55
  "forbidden": [
146
- "Do not invent variant ids, offer ids, or catalog ids.",
147
- "Do not invent admin, account-management, or grant-creation commands; the human grants access in the web UI and the CLI only reads an active grant.",
148
- "Do not ask users to paste secrets into chat."
56
+ "Do not invent catalog, variant, offer, capability, result item, order or refund IDs.",
57
+ "Do not dump Backend DTOs, diagnostic events, renderer internals or secrets into user chat.",
58
+ "Do not bypass a grant, refund lock, ownership error or compatibility gate."
149
59
  ],
150
60
  "next_docs": [
151
- {
152
- "condition": "Need to understand cart-first checkout in detail",
153
- "topic": "cart-checkout",
154
- "file": "docs/agent/buyer/cart-checkout.json"
155
- },
156
- {
157
- "condition": "Need to browse available services",
158
- "topic": "catalog-list",
159
- "file": "docs/agent/buyer/catalog-list.json"
160
- },
161
- {
162
- "condition": "Need the checkout-to-payment handoff and verification rules",
163
- "topic": "payment-flow",
164
- "file": "docs/agent/buyer/payment-flow.json"
165
- },
166
- {
167
- "condition": "Need to understand host rendering and interaction",
168
- "topic": "render-hosts",
169
- "file": "docs/agent/buyer/render-hosts.json"
170
- },
171
- {
172
- "condition": "Need to inspect orders or create refunds",
173
- "topic": "orders-refunds",
174
- "file": "docs/agent/buyer/orders-refunds.json"
175
- },
176
- {
177
- "condition": "Need to install or configure for a specific host",
178
- "topic": "install-and-setup",
179
- "file": "docs/agent/buyer/install-and-setup.json"
180
- }
61
+ { "condition": "Need service catalog language", "topic": "catalog-list" },
62
+ { "condition": "Need Cart and Checkout routing", "topic": "cart-checkout" },
63
+ { "condition": "Need payment recovery", "topic": "payment-flow" },
64
+ { "condition": "Need Host-specific presentation", "topic": "render-hosts" },
65
+ { "condition": "Need order or refund recovery", "topic": "orders-refunds" },
66
+ { "condition": "Need install details", "topic": "install-and-setup" }
181
67
  ],
182
- "search_terms": [
183
- "quickstart",
184
- "start",
185
- "buyer flow",
186
- "v3 cli",
187
- "how to use",
188
- "怎么用",
189
- "开始",
190
- "快速开始"
191
- ]
68
+ "search_terms": ["quickstart", "service", "next", "recovery", "delivery", "refund"]
192
69
  }
@@ -2,78 +2,64 @@
2
2
  "schema_version": "itp.agent_doc.v1",
3
3
  "role": "buyer",
4
4
  "topic": "render-hosts",
5
- "title": "Host Rendering And Interaction Requests",
6
- "purpose": "Teach the agent how the current V3 CLI renders checkout handoff and missing-data interaction prompts for each host.",
5
+ "title": "Agent Type And Checkout Handoff Rendering",
6
+ "purpose": "Use only the handoff fields returned for the current Agent Type and make the human Checkout visible on that surface.",
7
7
  "when_to_use": [
8
- "The agent needs to choose the right --host value.",
9
- "The agent needs to explain what the user will see in terminal, Trae/Codex, Telegram, or Feishu/Lark.",
10
- "The flow emits an interaction request instead of immediate checkout."
8
+ "buy, services checkout, checkout, or pay returned a human handoff.",
9
+ "The human cannot see the QR image or Checkout link."
11
10
  ],
12
- "required_state": {
13
- "needs": [
14
- "correct host selection"
15
- ],
16
- "optional": [
17
- "target for Telegram / Feishu / Lark"
18
- ]
19
- },
20
- "commands": [
11
+ "host_contracts": [
12
+ {
13
+ "agent_type": "codex-desktop",
14
+ "default_host": "codex",
15
+ "responsibility": "attach handoff.qr_local_path and show handoff.url or the returned markdown in the current desktop conversation"
16
+ },
21
17
  {
22
- "intent": "render checkout for terminal",
23
- "command": "itpay buy --host terminal --item <catalog_item_id> --variant <catalog_variant_id> --offer <offer_id>",
24
- "success_signal": "terminal summary, links, Branded QR local path + mirrors, and an ATTACH block with status=downloaded are printed"
18
+ "agent_type": "codex-cli",
19
+ "default_host": "terminal",
20
+ "responsibility": "render only in a terminal the human is actually watching and show the Checkout URL"
25
21
  },
26
22
  {
27
- "intent": "render checkout or interaction request for Trae / Codex style chat",
28
- "command": "itpay buy --host trae --item <catalog_item_id> --variant <catalog_variant_id> --offer <offer_id> --require-contact email,phone",
29
- "success_signal": "markdown with one inline data: image, an [ATTACH] IDE image reference block, and a structured itpay_input_request block is printed"
23
+ "agent_type": "claude-code-desktop",
24
+ "default_host": "claude-code",
25
+ "responsibility": "attach handoff.qr_local_path and show handoff.url or the returned markdown in the current desktop conversation"
30
26
  },
31
27
  {
32
- "intent": "render chat-native buttons for Telegram",
33
- "command": "itpay buy --host telegram --target <chat_id> --item <catalog_item_id> --variant <catalog_variant_id> --offer <offer_id>",
34
- "success_signal": "openclaw_message and presentation payload plus presentation.ide_image_attach (status, local_path, mirrors, must_render_reason) are printed"
28
+ "agent_type": "claude-code-cli",
29
+ "default_host": "terminal",
30
+ "responsibility": "render only in a terminal the human is actually watching and show the Checkout URL"
35
31
  },
36
32
  {
37
- "intent": "render interactive card output for Feishu or Lark",
38
- "command": "itpay buy --host feishu --target <open_id_or_chat_id> --item <catalog_item_id> --variant <catalog_variant_id> --offer <offer_id>",
39
- "success_signal": "interactive card JSON plus message.ide_image_attach (status, local_path, mirrors, must_render_reason) is printed"
33
+ "agent_type": "workbuddy",
34
+ "default_host": "plain-chat",
35
+ "responsibility": "send the returned Checkout URL and any usable image attachment to the current chat"
36
+ }
37
+ ],
38
+ "commands": [
39
+ {
40
+ "intent": "confirm the current Agent Type contract",
41
+ "command": "itpay install <agent_type> --json",
42
+ "success_signal": "the exact default Host and presentation responsibility are returned"
43
+ },
44
+ {
45
+ "intent": "recover a Checkout handoff",
46
+ "command": "itpay checkout --id <checkout_id> --token <display_token> --json",
47
+ "success_signal": "only handoff fields usable by the current Host are returned"
40
48
  }
41
49
  ],
42
50
  "agent_rules": [
43
- "Use --host terminal only when the human is directly watching a terminal.",
44
- "Use --host trae in Trae chat; it normalizes to the codex-style markdown renderer.",
45
- "Telegram, Feishu, and Lark require --target.",
46
- "Current V3 CLI can emit structured interaction requests, but Trae itself does not show a native popup unless a host adapter consumes that block.",
47
- "Terminal can render images as ASCII when an image URL is available.",
48
- "Telegram and Feishu/Lark can emit selector or input metadata for host-native adapters.",
49
- "If an interaction request is emitted, collect the requested values and rerun the relevant command with those values rather than inventing them.",
50
- "After buy/checkout/order output, the IDE image attach contract must be honoured: read the file at brand_qr_local_path (or any of brand_qr_mirrors) with the IDE's Read tool so the human sees the QR in the chat window. Do not declare success unless brand_qr_status is \"downloaded\" and the image is visible."
51
+ "Agent Type is stable identity; Host is presentation and may be explicitly overridden.",
52
+ "A local QR path is not visible until the Agent attaches or renders that file on the human-facing surface.",
53
+ "Do not claim the handoff was shown until both a usable payment image or QR and the Checkout URL are visible.",
54
+ "IM Hosts that require a target must receive --target; the initial five Agent Types currently default to desktop, terminal, or plain-chat Hosts."
51
55
  ],
52
56
  "forbidden": [
53
- "Do not claim Trae currently has a built-in native popup if only markdown is printed.",
54
- "Do not handwrite Telegram buttons when the CLI already returns a native presentation payload.",
55
- "Do not drop required --target fields for IM hosts."
57
+ "Do not expose display tokens separately from the tokenized Checkout handoff.",
58
+ "Do not print base64 images, mirror lists, renderer debug state, or provider QR URLs.",
59
+ "Do not claim a desktop attachment exists when only a filesystem path was printed."
56
60
  ],
57
61
  "next_docs": [
58
- {
59
- "condition": "Need the overall start sequence",
60
- "topic": "quickstart",
61
- "file": "docs/agent/buyer/quickstart.json"
62
- },
63
- {
64
- "condition": "Need cart-first handoff details",
65
- "topic": "cart-checkout",
66
- "file": "docs/agent/buyer/cart-checkout.json"
67
- }
62
+ { "condition": "Need payment verification rules", "topic": "payment-flow" }
68
63
  ],
69
- "search_terms": [
70
- "render",
71
- "host",
72
- "telegram",
73
- "trae",
74
- "codex",
75
- "interaction request",
76
- "selector",
77
- "输入框"
78
- ]
64
+ "search_terms": ["host", "agent type", "QR", "markdown", "terminal", "desktop", "workbuddy"]
79
65
  }
@@ -0,0 +1,45 @@
1
+ # Agent Type And Host Contract
2
+
3
+ `--agent-type` 表示谁在运行 CLI,用于设备登记、Agent 实例归属和定制 instruction。`--host` 表示输出展示在哪里,用于选择二维码、链接或消息的渲染方式。两者不可混用。
4
+
5
+ ## 首批支持类型
6
+
7
+ | Agent Type | 默认 Host | 初始 instruction 差异 |
8
+ |---|---|---|
9
+ | `codex-desktop` | `codex` | 返回可在 Codex 桌面对话中展示的本地二维码图片和付款链接,要求 Agent 将图片实际发到当前对话。 |
10
+ | `codex-cli` | `terminal` | 在用户可见终端渲染二维码并输出付款链接;若用户不看该终端,要求改用正确 Host。 |
11
+ | `claude-code-desktop` | `claude-code` | 返回桌面对话可展示的 Markdown 图片和付款链接,要求先展示再等待。 |
12
+ | `claude-code-cli` | `terminal` | 在用户可见终端输出二维码和链接,不声称已在桌面对话展示。 |
13
+ | `workbuddy` | `plain-chat` | 初期返回清晰付款链接和通用图片附件信息;原生卡片细节后续补充。 |
14
+
15
+ ## 通用规则
16
+
17
+ - commerce 命令必须传 `--agent-type` 或设置 `ITPAY_AGENT_TYPE`。
18
+ - Agent Type 必须稳定;同一运行时不得临时换名。
19
+ - 显式 `--host` 覆盖默认 Host,但不改变已登记的 Agent Type。
20
+ - Host 只影响 `instruction` 和 `handoff`,不得改变金额、订单、权限、quota 或交付状态。
21
+ - 非展示命令在五种 Agent Type 下返回相同业务结果,只允许 instruction 措辞不同。
22
+
23
+ ## Checkout Handoff 最小合同
24
+
25
+ ```json
26
+ {
27
+ "status": "human_checkout_required",
28
+ "result": {
29
+ "checkout_id": "<checkout_id>",
30
+ "amount": "<amount> <currency>"
31
+ },
32
+ "handoff": {
33
+ "url": "<checkout_url>",
34
+ "qr_local_path": "<optional_local_path>",
35
+ "markdown": "<optional_host_ready_markdown>"
36
+ },
37
+ "instruction": "<agent-type-specific instruction>",
38
+ "next": {
39
+ "command": "itpay checkout --id <checkout_id> --token <display_token> --json"
40
+ },
41
+ "recovery": []
42
+ }
43
+ ```
44
+
45
+ 只返回当前 Host 能使用的 `handoff` 字段,不返回镜像路径列表、渲染器内部状态或重复的 action 描述。
@@ -0,0 +1,167 @@
1
+ # `itpay buy`
2
+
3
+ ## 范围与意义
4
+
5
+ 为普通 Catalog 项目或已绑定 Service Quote 的 canonical Cart 创建一个 ItPay Checkout,并按当前 Agent Type 把付款入口交给用户。命令只创建或恢复 ItPay Checkout,不把“用户说已付款”当作付款成功。
6
+
7
+ 未报价的 service-backed line 不可购买;命令停止并转向 `services next`。已通过 `cart add --quote` 锁定输入和价格的服务行可以合并结算,每个 Order Item 仍映射回独立 Execution、Capability 和交付路径。
8
+
9
+ **直接上游:**
10
+
11
+ - `cart add` 创建的 canonical Cart;
12
+ - `cart add --local` 创建的本地兼容草稿;
13
+ - Catalog 返回的一组完整 `item / variant / offer`;
14
+ - 已知的普通 canonical `cart_id`。
15
+
16
+ **直接下游:** `checkout` 查询同一 Checkout 的权威状态。付款确认后,再按其返回的 `next` 进入订单或服务履约。
17
+
18
+ ## 语法
19
+
20
+ ```bash
21
+ itpay buy [--json]
22
+
23
+ itpay buy --cart <cart_id> [--json]
24
+
25
+ itpay buy \
26
+ --item <catalog_item_id> \
27
+ --variant <catalog_variant_id> \
28
+ --offer <offer_id> \
29
+ [--quantity <positive_integer>] [--json]
30
+ ```
31
+
32
+ 通用选项:
33
+
34
+ ```text
35
+ --ref <client_reference_id>
36
+ --contact-email <email>
37
+ --contact-phone <phone>
38
+ --require-contact <email,phone>
39
+ --host <host>
40
+ --target <target>
41
+ --qr-format <unicode|utf8|ansi|terminal>
42
+ --qr-file <path>
43
+ --pay
44
+ --method <alipay|wechatpay>
45
+ --no-wait
46
+ --timeout <positive_seconds>
47
+ --json
48
+ ```
49
+
50
+ ## 参数合同
51
+
52
+ | 参数 | 必填 | 规则 |
53
+ |---|---:|---|
54
+ | `--cart` | 条件必填 | 使用一个已存在的普通或全量已报价 canonical Cart。不能和 inline 三元组同时使用。 |
55
+ | `--item` | 条件必填 | Inline 购买时必须与 `--variant`、`--offer` 一起提供。只能使用 Catalog 返回值。 |
56
+ | `--variant` | 条件必填 | 同上。 |
57
+ | `--offer` | 条件必填 | 同上。 |
58
+ | `--quantity` | 否 | 正整数,默认 `1`;只用于 inline 项目。 |
59
+ | `--ref` | 否 | 调用方自己的业务引用;不承担幂等职责。 |
60
+ | `--contact-email` | 条件必填 | 普通 Cart 或兼容旧 Quote 缺少所需邮箱时使用,值必须来自用户;新版 Service Quote 会自动携带已确认邮箱。 |
61
+ | `--contact-phone` | 条件必填 | `--require-contact` 包含 `phone` 时必填,值必须来自用户。 |
62
+ | `--require-contact` | 否 | 只接受 `email`、`phone`;缺失时先询问用户,禁止 Agent 编造。 |
63
+ | `--host` | 否 | 默认由 `--agent-type` 推导;只改变 handoff 展示,不改变交易事实。 |
64
+ | `--target` | 条件必填 | 只有要求目标会话的 IM Host 才需要;当前首批五种 Agent Type 无需提供。 |
65
+ | `--qr-format` | 否 | 非 JSON 的终端渲染选项。 |
66
+ | `--qr-file` | 否 | 非 JSON handoff 的明确二维码文件路径。 |
67
+ | `--pay` | 否 | 创建 Payment Intent 的集成/运维入口;普通 Agent 流程只展示 Checkout。 |
68
+ | `--method` | 否 | 只接受 `alipay` 或 `wechatpay`,默认 `alipay`;仅随 `--pay` 生效。 |
69
+ | `--no-wait` | 否 | 只能与 `--pay` 一起使用。 |
70
+ | `--timeout` | 否 | 等待付款事件的正整数秒数,默认 `120`。 |
71
+ | `--json` | 否 | 输出本文定义的紧凑机器合同,不输出字符二维码或图片二进制。 |
72
+
73
+ 不传 `--cart` 或 inline 三元组时,命令使用本机保存的 active canonical Cart;若没有,则使用本地普通草稿创建 canonical Cart。两者都没有时返回 `cart_empty`。
74
+
75
+ ## 标准 JSON 输出
76
+
77
+ ### 等待用户付款
78
+
79
+ ```json
80
+ {
81
+ "status": "human_checkout_required",
82
+ "result": {
83
+ "checkout_id": "<checkout_id>",
84
+ "payment": "pending",
85
+ "amount": "<amount> <currency>",
86
+ "item_count": 1
87
+ },
88
+ "handoff": {
89
+ "url": "<tokenized_checkout_url>",
90
+ "qr_local_path": "<optional_host_ready_file>",
91
+ "markdown": "<desktop_only_optional_markdown>",
92
+ "qr_image_url": "<plain_chat_only_optional_url>"
93
+ },
94
+ "instruction": "<current_host_instruction>",
95
+ "next": {
96
+ "command": "itpay checkout --id <checkout_id> --token <display_token> --json",
97
+ "reason": "稍后查询同一笔 Checkout 状态"
98
+ },
99
+ "recovery": []
100
+ }
101
+ ```
102
+
103
+ `handoff` 只保留当前 Host 可以使用的字段。不得返回二维码 base64、镜像路径数组、renderer 状态、原始后端 DTO 或重复的 `agent_action`。
104
+
105
+ **Instruction:** 先把 handoff 中的付款入口实际展示给用户,再等待用户操作;不要创建第二个 Checkout。
106
+
107
+ ### `--pay` 已观察到付款事件
108
+
109
+ ```json
110
+ {
111
+ "status": "payment_event_observed",
112
+ "result": {
113
+ "checkout_id": "<checkout_id>",
114
+ "payment": "verified",
115
+ "amount": "<amount> <currency>",
116
+ "item_count": 1,
117
+ "payment_intent_id": "<payment_intent_id>",
118
+ "payment_intent_status": "<provider_status>"
119
+ },
120
+ "instruction": "已观察到付款确认事件;读取同一 Checkout 的权威完成状态,不要再次付款。",
121
+ "next": {
122
+ "command": "itpay checkout --id <checkout_id> --token <display_token> --json",
123
+ "reason": "读取订单和履约句柄"
124
+ },
125
+ "recovery": []
126
+ }
127
+ ```
128
+
129
+ 事件观察不是订单 DTO。Agent 必须执行 `next.command`,不能自行宣称履约完成。
130
+
131
+ ## 幂等与中断恢复
132
+
133
+ - CLI 在本机 operation journal 中为 `checkout.create:<cart_id>` 保存稳定幂等键。
134
+ - HTTP 请求通过 `Idempotency-Key` 提交该键。
135
+ - Checkout 响应丢失时,重跑同一命令会复用已保存的 canonical Cart 和同一幂等键。
136
+ - 后端返回同一个待处理 Checkout,并轮换新的交接 token;不会创建第二笔订单。
137
+ - Service Quote Cart 的联系信息由 Quote Lock 汇总;正常流程无需在 `buy` 重复传邮箱,旧 Quote 可显式补充。
138
+ - Checkout 成功保存后,active Cart 句柄被清除,只保留 Checkout(以及可选 Service Execution)恢复句柄。
139
+ - 不确定命令执行到哪一步时,先运行 `itpay next --json`,不得重新拼一笔购买。
140
+
141
+ ## 异常处理
142
+
143
+ | 错误码 | 含义 | Agent 处理 |
144
+ |---|---|---|
145
+ | `buy_source_invalid` | Inline 三元组不完整,或与 `--cart` 混用。 | 从 Catalog 重新读取完整 ID,或只保留 `--cart`。 |
146
+ | `cart_empty` | 没有本地草稿或 active canonical Cart。 | 执行 `catalog list`,不要猜 ID。 |
147
+ | `missing_contact` | 缺少调用方声明的 required contact。 | 向用户询问后重跑同一命令;禁止编造。 |
148
+ | `contact_field_invalid` | `--require-contact` 包含不支持字段。 | 只使用 `email`、`phone`。 |
149
+ | `payment_method_invalid` | 付款方式不是允许值。 | 使用 `alipay` 或 `wechatpay`。 |
150
+ | `buy_parameter_invalid` | quantity/timeout 非正整数,或 `--no-wait` 未配 `--pay`。 | 修正参数;本次不创建资源。 |
151
+ | `service_quote_required` | Cart 含尚未绑定 Quote 的 Service Execution。 | 原样执行返回的 `services next <id> --json`;不要绕过 Quote 输入校验。 |
152
+ | `idempotency_conflict` | 同一幂等操作被用于不同请求。 | 保留句柄并执行 `itpay next --json`,不要换键重建。 |
153
+ | `buy_failed` | 网络或未知后端错误。 | 先执行 `itpay next --json` 或 `cart next --json` 恢复现有资源。 |
154
+
155
+ 所有参数错误都必须在 HTTP 和本地 Cart 变更前被拒绝。服务 Cart 只有每条 service-backed line 都绑定有效 Quote 时才能创建 Checkout;付款后 Backend 按 Order Item 分别推进 Execution。
156
+
157
+ ## Agent Type / Host
158
+
159
+ | Agent Type | 默认 Host | JSON handoff | Instruction |
160
+ |---|---|---|---|
161
+ | `codex-desktop` | `codex` | `url`、可用时 `qr_local_path` 和 `markdown` | 把 `handoff.markdown` 原样发到当前 Codex 对话,确认二维码和链接可见后等待。 |
162
+ | `codex-cli` | `terminal` | `url`、可用时 `qr_local_path` | 非 JSON 模式在用户可见终端渲染二维码;始终保留付款链接。 |
163
+ | `claude-code-desktop` | `claude-code` | `url`、可用时 `qr_local_path` 和 `markdown` | 把 Markdown handoff 发到当前桌面对话,不能只输出本地路径。 |
164
+ | `claude-code-cli` | `terminal` | `url`、可用时 `qr_local_path` | 在用户可见终端展示;不能声称桌面对话已收到图片。 |
165
+ | `workbuddy` | `plain-chat` | `url`、可用时 `qr_local_path` 和 `qr_image_url` | 发送链接和可用图片附件,不虚构原生卡片。 |
166
+
167
+ 显式 `--host` 可以覆盖展示方式,但不会改变 Agent Type、设备身份、金额、权限或交易状态。