@harrylabsj/kiwi 0.6.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 +201 -0
- package/README.md +150 -0
- package/contracts/candidate-agent-dto-1.0.README.md +39 -0
- package/contracts/candidate-agent-dto-1.0.schema.json +172 -0
- package/contracts/interop/kiwi-buyer-message-send.json +43 -0
- package/contracts/interop/knp-data-part-examples.json +243 -0
- package/contracts/kiwi-catalog/1.0/agent-record.schema.json +78 -0
- package/contracts/kiwi-catalog/1.0/listing-record.schema.json +88 -0
- package/contracts/kiwi-catalog/1.0/listing-search-result.schema.json +60 -0
- package/contracts/negotiation/1.0/schema.json +501 -0
- package/contracts/shopping.negotiation/0.1/capabilities.schema.json +47 -0
- package/contracts/shopping.negotiation/0.1/decision.schema.json +120 -0
- package/contracts/shopping.negotiation/0.1/policy-result.schema.json +52 -0
- package/contracts/shopping.negotiation/0.1/snapshot.schema.json +172 -0
- package/dist/a2a/client/client.d.ts +33 -0
- package/dist/a2a/client/client.js +161 -0
- package/dist/a2a/client/client.js.map +1 -0
- package/dist/a2a/client/error.d.ts +35 -0
- package/dist/a2a/client/error.js +34 -0
- package/dist/a2a/client/error.js.map +1 -0
- package/dist/a2a/client/index.d.ts +29 -0
- package/dist/a2a/client/index.js +26 -0
- package/dist/a2a/client/index.js.map +1 -0
- package/dist/a2a/client/jsonrpc.d.ts +49 -0
- package/dist/a2a/client/jsonrpc.js +85 -0
- package/dist/a2a/client/jsonrpc.js.map +1 -0
- package/dist/a2a/client/parse.d.ts +18 -0
- package/dist/a2a/client/parse.js +134 -0
- package/dist/a2a/client/parse.js.map +1 -0
- package/dist/a2a/client/types.d.ts +102 -0
- package/dist/a2a/client/types.js +26 -0
- package/dist/a2a/client/types.js.map +1 -0
- package/dist/a2a/client/url-policy.d.ts +44 -0
- package/dist/a2a/client/url-policy.js +242 -0
- package/dist/a2a/client/url-policy.js.map +1 -0
- package/dist/a2a/negotiate.d.ts +68 -0
- package/dist/a2a/negotiate.js +288 -0
- package/dist/a2a/negotiate.js.map +1 -0
- package/dist/a2a/node.d.ts +37 -0
- package/dist/a2a/node.js +155 -0
- package/dist/a2a/node.js.map +1 -0
- package/dist/a2a/server/auth.d.ts +38 -0
- package/dist/a2a/server/auth.js +112 -0
- package/dist/a2a/server/auth.js.map +1 -0
- package/dist/a2a/server/card.d.ts +19 -0
- package/dist/a2a/server/card.js +81 -0
- package/dist/a2a/server/card.js.map +1 -0
- package/dist/a2a/server/errors.d.ts +109 -0
- package/dist/a2a/server/errors.js +178 -0
- package/dist/a2a/server/errors.js.map +1 -0
- package/dist/a2a/server/handler.d.ts +22 -0
- package/dist/a2a/server/handler.js +57 -0
- package/dist/a2a/server/handler.js.map +1 -0
- package/dist/a2a/server/inbound-message.d.ts +37 -0
- package/dist/a2a/server/inbound-message.js +104 -0
- package/dist/a2a/server/inbound-message.js.map +1 -0
- package/dist/a2a/server/index.d.ts +43 -0
- package/dist/a2a/server/index.js +42 -0
- package/dist/a2a/server/index.js.map +1 -0
- package/dist/a2a/server/merchant-handler.d.ts +69 -0
- package/dist/a2a/server/merchant-handler.js +316 -0
- package/dist/a2a/server/merchant-handler.js.map +1 -0
- package/dist/a2a/server/pipeline.d.ts +69 -0
- package/dist/a2a/server/pipeline.js +329 -0
- package/dist/a2a/server/pipeline.js.map +1 -0
- package/dist/a2a/server/server.d.ts +64 -0
- package/dist/a2a/server/server.js +420 -0
- package/dist/a2a/server/server.js.map +1 -0
- package/dist/a2a/server/task-registry.d.ts +31 -0
- package/dist/a2a/server/task-registry.js +67 -0
- package/dist/a2a/server/task-registry.js.map +1 -0
- package/dist/a2a/server/throttle.d.ts +154 -0
- package/dist/a2a/server/throttle.js +289 -0
- package/dist/a2a/server/throttle.js.map +1 -0
- package/dist/a2a/server/types.d.ts +191 -0
- package/dist/a2a/server/types.js +17 -0
- package/dist/a2a/server/types.js.map +1 -0
- package/dist/a2a/server/ucp.d.ts +49 -0
- package/dist/a2a/server/ucp.js +85 -0
- package/dist/a2a/server/ucp.js.map +1 -0
- package/dist/a2a/task/index.d.ts +24 -0
- package/dist/a2a/task/index.js +22 -0
- package/dist/a2a/task/index.js.map +1 -0
- package/dist/a2a/task/ledger.d.ts +48 -0
- package/dist/a2a/task/ledger.js +65 -0
- package/dist/a2a/task/ledger.js.map +1 -0
- package/dist/a2a/task/poller.d.ts +93 -0
- package/dist/a2a/task/poller.js +184 -0
- package/dist/a2a/task/poller.js.map +1 -0
- package/dist/a2a/task/state.d.ts +72 -0
- package/dist/a2a/task/state.js +119 -0
- package/dist/a2a/task/state.js.map +1 -0
- package/dist/a2a/ucp-agent.d.ts +49 -0
- package/dist/a2a/ucp-agent.js +78 -0
- package/dist/a2a/ucp-agent.js.map +1 -0
- package/dist/agent/agent-db.d.ts +31 -0
- package/dist/agent/agent-db.js +91 -0
- package/dist/agent/agent-db.js.map +1 -0
- package/dist/agent/buyer/buyer-tools.d.ts +70 -0
- package/dist/agent/buyer/buyer-tools.js +1353 -0
- package/dist/agent/buyer/buyer-tools.js.map +1 -0
- package/dist/agent/buyer/ranker.d.ts +53 -0
- package/dist/agent/buyer/ranker.js +153 -0
- package/dist/agent/buyer/ranker.js.map +1 -0
- package/dist/agent/buyer/scheduler.d.ts +58 -0
- package/dist/agent/buyer/scheduler.js +317 -0
- package/dist/agent/buyer/scheduler.js.map +1 -0
- package/dist/agent/buyer/search-loop.d.ts +42 -0
- package/dist/agent/buyer/search-loop.js +280 -0
- package/dist/agent/buyer/search-loop.js.map +1 -0
- package/dist/agent/buyer/task-store.d.ts +178 -0
- package/dist/agent/buyer/task-store.js +637 -0
- package/dist/agent/buyer/task-store.js.map +1 -0
- package/dist/agent/buyer/types.d.ts +222 -0
- package/dist/agent/buyer/types.js +124 -0
- package/dist/agent/buyer/types.js.map +1 -0
- package/dist/agent/chat-tools.d.ts +41 -0
- package/dist/agent/chat-tools.js +193 -0
- package/dist/agent/chat-tools.js.map +1 -0
- package/dist/agent/chat-tui.d.ts +49 -0
- package/dist/agent/chat-tui.js +314 -0
- package/dist/agent/chat-tui.js.map +1 -0
- package/dist/agent/connector/fake-connector.d.ts +50 -0
- package/dist/agent/connector/fake-connector.js +138 -0
- package/dist/agent/connector/fake-connector.js.map +1 -0
- package/dist/agent/connector/http-connector.d.ts +49 -0
- package/dist/agent/connector/http-connector.js +208 -0
- package/dist/agent/connector/http-connector.js.map +1 -0
- package/dist/agent/connector/types.d.ts +103 -0
- package/dist/agent/connector/types.js +97 -0
- package/dist/agent/connector/types.js.map +1 -0
- package/dist/agent/fake-chat-model.d.ts +22 -0
- package/dist/agent/fake-chat-model.js +134 -0
- package/dist/agent/fake-chat-model.js.map +1 -0
- package/dist/agent/kernel.d.ts +258 -0
- package/dist/agent/kernel.js +1099 -0
- package/dist/agent/kernel.js.map +1 -0
- package/dist/agent/memory/schema.d.ts +32 -0
- package/dist/agent/memory/schema.js +291 -0
- package/dist/agent/memory/schema.js.map +1 -0
- package/dist/agent/memory/store.d.ts +193 -0
- package/dist/agent/memory/store.js +881 -0
- package/dist/agent/memory/store.js.map +1 -0
- package/dist/agent/memory/types.d.ts +139 -0
- package/dist/agent/memory/types.js +119 -0
- package/dist/agent/memory/types.js.map +1 -0
- package/dist/agent/memory/vault.d.ts +49 -0
- package/dist/agent/memory/vault.js +125 -0
- package/dist/agent/memory/vault.js.map +1 -0
- package/dist/agent/merchant/action-candidate.d.ts +106 -0
- package/dist/agent/merchant/action-candidate.js +241 -0
- package/dist/agent/merchant/action-candidate.js.map +1 -0
- package/dist/agent/merchant/credential-broker.d.ts +67 -0
- package/dist/agent/merchant/credential-broker.js +76 -0
- package/dist/agent/merchant/credential-broker.js.map +1 -0
- package/dist/agent/merchant/fake-merchant-client.d.ts +47 -0
- package/dist/agent/merchant/fake-merchant-client.js +143 -0
- package/dist/agent/merchant/fake-merchant-client.js.map +1 -0
- package/dist/agent/merchant/merchant-client.d.ts +49 -0
- package/dist/agent/merchant/merchant-client.js +186 -0
- package/dist/agent/merchant/merchant-client.js.map +1 -0
- package/dist/agent/merchant/merchant-tools.d.ts +74 -0
- package/dist/agent/merchant/merchant-tools.js +582 -0
- package/dist/agent/merchant/merchant-tools.js.map +1 -0
- package/dist/agent/merchant/types.d.ts +119 -0
- package/dist/agent/merchant/types.js +133 -0
- package/dist/agent/merchant/types.js.map +1 -0
- package/dist/agent/mode.d.ts +32 -0
- package/dist/agent/mode.js +34 -0
- package/dist/agent/mode.js.map +1 -0
- package/dist/agent/negotiation-chat.d.ts +81 -0
- package/dist/agent/negotiation-chat.js +330 -0
- package/dist/agent/negotiation-chat.js.map +1 -0
- package/dist/agent/session.d.ts +22 -0
- package/dist/agent/session.js +164 -0
- package/dist/agent/session.js.map +1 -0
- package/dist/agent/system-prompt.d.ts +26 -0
- package/dist/agent/system-prompt.js +77 -0
- package/dist/agent/system-prompt.js.map +1 -0
- package/dist/agent/write-gate.d.ts +98 -0
- package/dist/agent/write-gate.js +76 -0
- package/dist/agent/write-gate.js.map +1 -0
- package/dist/cli.d.ts +18 -0
- package/dist/cli.js +1139 -0
- package/dist/cli.js.map +1 -0
- package/dist/commerce/data-source.d.ts +88 -0
- package/dist/commerce/data-source.js +25 -0
- package/dist/commerce/data-source.js.map +1 -0
- package/dist/commerce/erp-source.d.ts +51 -0
- package/dist/commerce/erp-source.js +184 -0
- package/dist/commerce/erp-source.js.map +1 -0
- package/dist/commerce/fake-client.d.ts +167 -0
- package/dist/commerce/fake-client.js +549 -0
- package/dist/commerce/fake-client.js.map +1 -0
- package/dist/commerce/http-client.d.ts +73 -0
- package/dist/commerce/http-client.js +284 -0
- package/dist/commerce/http-client.js.map +1 -0
- package/dist/commerce/local-db-source.d.ts +50 -0
- package/dist/commerce/local-db-source.js +162 -0
- package/dist/commerce/local-db-source.js.map +1 -0
- package/dist/commerce/shopping-cli-source.d.ts +57 -0
- package/dist/commerce/shopping-cli-source.js +206 -0
- package/dist/commerce/shopping-cli-source.js.map +1 -0
- package/dist/commerce/types.d.ts +146 -0
- package/dist/commerce/types.js +31 -0
- package/dist/commerce/types.js.map +1 -0
- package/dist/config/profile.d.ts +122 -0
- package/dist/config/profile.js +386 -0
- package/dist/config/profile.js.map +1 -0
- package/dist/contracts/negotiation-schema.d.ts +28 -0
- package/dist/contracts/negotiation-schema.js +101 -0
- package/dist/contracts/negotiation-schema.js.map +1 -0
- package/dist/contracts/schemas.d.ts +31 -0
- package/dist/contracts/schemas.js +122 -0
- package/dist/contracts/schemas.js.map +1 -0
- package/dist/counterparty/a2a-direct/index.d.ts +41 -0
- package/dist/counterparty/a2a-direct/index.js +330 -0
- package/dist/counterparty/a2a-direct/index.js.map +1 -0
- package/dist/counterparty/channel.d.ts +200 -0
- package/dist/counterparty/channel.js +61 -0
- package/dist/counterparty/channel.js.map +1 -0
- package/dist/counterparty/index.d.ts +28 -0
- package/dist/counterparty/index.js +24 -0
- package/dist/counterparty/index.js.map +1 -0
- package/dist/counterparty/platform-api/index.d.ts +41 -0
- package/dist/counterparty/platform-api/index.js +65 -0
- package/dist/counterparty/platform-api/index.js.map +1 -0
- package/dist/counterparty/registry.d.ts +51 -0
- package/dist/counterparty/registry.js +71 -0
- package/dist/counterparty/registry.js.map +1 -0
- package/dist/counterparty/shopping-cli-hosted/index.d.ts +36 -0
- package/dist/counterparty/shopping-cli-hosted/index.js +337 -0
- package/dist/counterparty/shopping-cli-hosted/index.js.map +1 -0
- package/dist/discovery/agent-card/error.d.ts +28 -0
- package/dist/discovery/agent-card/error.js +31 -0
- package/dist/discovery/agent-card/error.js.map +1 -0
- package/dist/discovery/agent-card/extensions.d.ts +20 -0
- package/dist/discovery/agent-card/extensions.js +59 -0
- package/dist/discovery/agent-card/extensions.js.map +1 -0
- package/dist/discovery/agent-card/index.d.ts +29 -0
- package/dist/discovery/agent-card/index.js +38 -0
- package/dist/discovery/agent-card/index.js.map +1 -0
- package/dist/discovery/agent-card/secrets.d.ts +30 -0
- package/dist/discovery/agent-card/secrets.js +240 -0
- package/dist/discovery/agent-card/secrets.js.map +1 -0
- package/dist/discovery/agent-card/types.d.ts +110 -0
- package/dist/discovery/agent-card/types.js +37 -0
- package/dist/discovery/agent-card/types.js.map +1 -0
- package/dist/discovery/agent-card/validate.d.ts +31 -0
- package/dist/discovery/agent-card/validate.js +210 -0
- package/dist/discovery/agent-card/validate.js.map +1 -0
- package/dist/discovery/capability/error.d.ts +23 -0
- package/dist/discovery/capability/error.js +28 -0
- package/dist/discovery/capability/error.js.map +1 -0
- package/dist/discovery/capability/index.d.ts +21 -0
- package/dist/discovery/capability/index.js +21 -0
- package/dist/discovery/capability/index.js.map +1 -0
- package/dist/discovery/capability/intersect.d.ts +56 -0
- package/dist/discovery/capability/intersect.js +137 -0
- package/dist/discovery/capability/intersect.js.map +1 -0
- package/dist/discovery/catalog-source/errors.d.ts +32 -0
- package/dist/discovery/catalog-source/errors.js +41 -0
- package/dist/discovery/catalog-source/errors.js.map +1 -0
- package/dist/discovery/catalog-source/index.d.ts +30 -0
- package/dist/discovery/catalog-source/index.js +29 -0
- package/dist/discovery/catalog-source/index.js.map +1 -0
- package/dist/discovery/catalog-source/kiwi-record.d.ts +183 -0
- package/dist/discovery/catalog-source/kiwi-record.js +72 -0
- package/dist/discovery/catalog-source/kiwi-record.js.map +1 -0
- package/dist/discovery/catalog-source/kiwi-schema.d.ts +33 -0
- package/dist/discovery/catalog-source/kiwi-schema.js +106 -0
- package/dist/discovery/catalog-source/kiwi-schema.js.map +1 -0
- package/dist/discovery/catalog-source/kiwi-source.d.ts +38 -0
- package/dist/discovery/catalog-source/kiwi-source.js +318 -0
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -0
- package/dist/discovery/catalog-source/register.d.ts +48 -0
- package/dist/discovery/catalog-source/register.js +83 -0
- package/dist/discovery/catalog-source/register.js.map +1 -0
- package/dist/discovery/catalog-source/schema.d.ts +21 -0
- package/dist/discovery/catalog-source/schema.js +63 -0
- package/dist/discovery/catalog-source/schema.js.map +1 -0
- package/dist/discovery/catalog-source/source.d.ts +45 -0
- package/dist/discovery/catalog-source/source.js +197 -0
- package/dist/discovery/catalog-source/source.js.map +1 -0
- package/dist/discovery/catalog-source/types.d.ts +122 -0
- package/dist/discovery/catalog-source/types.js +30 -0
- package/dist/discovery/catalog-source/types.js.map +1 -0
- package/dist/discovery/index.d.ts +25 -0
- package/dist/discovery/index.js +25 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/resolve.d.ts +175 -0
- package/dist/discovery/resolve.js +367 -0
- package/dist/discovery/resolve.js.map +1 -0
- package/dist/discovery/ucp/error.d.ts +25 -0
- package/dist/discovery/ucp/error.js +27 -0
- package/dist/discovery/ucp/error.js.map +1 -0
- package/dist/discovery/ucp/index.d.ts +33 -0
- package/dist/discovery/ucp/index.js +28 -0
- package/dist/discovery/ucp/index.js.map +1 -0
- package/dist/discovery/ucp/intersect.d.ts +148 -0
- package/dist/discovery/ucp/intersect.js +312 -0
- package/dist/discovery/ucp/intersect.js.map +1 -0
- package/dist/discovery/ucp/resolver.d.ts +62 -0
- package/dist/discovery/ucp/resolver.js +214 -0
- package/dist/discovery/ucp/resolver.js.map +1 -0
- package/dist/discovery/ucp/types.d.ts +97 -0
- package/dist/discovery/ucp/types.js +103 -0
- package/dist/discovery/ucp/types.js.map +1 -0
- package/dist/discovery/ucp/validate.d.ts +38 -0
- package/dist/discovery/ucp/validate.js +232 -0
- package/dist/discovery/ucp/validate.js.map +1 -0
- package/dist/discovery/ucp/vendor.d.ts +60 -0
- package/dist/discovery/ucp/vendor.js +61 -0
- package/dist/discovery/ucp/vendor.js.map +1 -0
- package/dist/doctor.d.ts +26 -0
- package/dist/doctor.js +113 -0
- package/dist/doctor.js.map +1 -0
- package/dist/exit-codes.d.ts +26 -0
- package/dist/exit-codes.js +27 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/fanout/disclosure.d.ts +138 -0
- package/dist/fanout/disclosure.js +188 -0
- package/dist/fanout/disclosure.js.map +1 -0
- package/dist/fanout/index.d.ts +32 -0
- package/dist/fanout/index.js +30 -0
- package/dist/fanout/index.js.map +1 -0
- package/dist/fanout/orchestrator.d.ts +153 -0
- package/dist/fanout/orchestrator.js +439 -0
- package/dist/fanout/orchestrator.js.map +1 -0
- package/dist/fanout/policy.d.ts +128 -0
- package/dist/fanout/policy.js +130 -0
- package/dist/fanout/policy.js.map +1 -0
- package/dist/handoff/acp-commerce.d.ts +58 -0
- package/dist/handoff/acp-commerce.js +69 -0
- package/dist/handoff/acp-commerce.js.map +1 -0
- package/dist/handoff/authorization.d.ts +127 -0
- package/dist/handoff/authorization.js +107 -0
- package/dist/handoff/authorization.js.map +1 -0
- package/dist/handoff/candidate.d.ts +94 -0
- package/dist/handoff/candidate.js +172 -0
- package/dist/handoff/candidate.js.map +1 -0
- package/dist/handoff/channel.d.ts +88 -0
- package/dist/handoff/channel.js +189 -0
- package/dist/handoff/channel.js.map +1 -0
- package/dist/handoff/completion.d.ts +44 -0
- package/dist/handoff/completion.js +136 -0
- package/dist/handoff/completion.js.map +1 -0
- package/dist/handoff/delivery.d.ts +59 -0
- package/dist/handoff/delivery.js +185 -0
- package/dist/handoff/delivery.js.map +1 -0
- package/dist/handoff/destination.d.ts +39 -0
- package/dist/handoff/destination.js +99 -0
- package/dist/handoff/destination.js.map +1 -0
- package/dist/handoff/errors.d.ts +34 -0
- package/dist/handoff/errors.js +47 -0
- package/dist/handoff/errors.js.map +1 -0
- package/dist/handoff/idempotency.d.ts +51 -0
- package/dist/handoff/idempotency.js +168 -0
- package/dist/handoff/idempotency.js.map +1 -0
- package/dist/handoff/index.d.ts +46 -0
- package/dist/handoff/index.js +48 -0
- package/dist/handoff/index.js.map +1 -0
- package/dist/handoff/ledger.d.ts +94 -0
- package/dist/handoff/ledger.js +195 -0
- package/dist/handoff/ledger.js.map +1 -0
- package/dist/handoff/lifecycle.d.ts +29 -0
- package/dist/handoff/lifecycle.js +102 -0
- package/dist/handoff/lifecycle.js.map +1 -0
- package/dist/handoff/metrics.d.ts +56 -0
- package/dist/handoff/metrics.js +78 -0
- package/dist/handoff/metrics.js.map +1 -0
- package/dist/handoff/operator-approval.d.ts +143 -0
- package/dist/handoff/operator-approval.js +287 -0
- package/dist/handoff/operator-approval.js.map +1 -0
- package/dist/handoff/order-record.d.ts +104 -0
- package/dist/handoff/order-record.js +235 -0
- package/dist/handoff/order-record.js.map +1 -0
- package/dist/handoff/package.d.ts +102 -0
- package/dist/handoff/package.js +132 -0
- package/dist/handoff/package.js.map +1 -0
- package/dist/handoff/transaction.d.ts +96 -0
- package/dist/handoff/transaction.js +263 -0
- package/dist/handoff/transaction.js.map +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.d.ts +111 -0
- package/dist/handoff/ucp-checkout/cart-channel.js +370 -0
- package/dist/handoff/ucp-checkout/cart-channel.js.map +1 -0
- package/dist/handoff/ucp-checkout/cart-parse.d.ts +22 -0
- package/dist/handoff/ucp-checkout/cart-parse.js +102 -0
- package/dist/handoff/ucp-checkout/cart-parse.js.map +1 -0
- package/dist/handoff/ucp-checkout/cart-types.d.ts +93 -0
- package/dist/handoff/ucp-checkout/cart-types.js +38 -0
- package/dist/handoff/ucp-checkout/cart-types.js.map +1 -0
- package/dist/handoff/ucp-checkout/channel.d.ts +159 -0
- package/dist/handoff/ucp-checkout/channel.js +636 -0
- package/dist/handoff/ucp-checkout/channel.js.map +1 -0
- package/dist/handoff/ucp-checkout/client.d.ts +88 -0
- package/dist/handoff/ucp-checkout/client.js +228 -0
- package/dist/handoff/ucp-checkout/client.js.map +1 -0
- package/dist/handoff/ucp-checkout/endpoint.d.ts +36 -0
- package/dist/handoff/ucp-checkout/endpoint.js +145 -0
- package/dist/handoff/ucp-checkout/endpoint.js.map +1 -0
- package/dist/handoff/ucp-checkout/index.d.ts +35 -0
- package/dist/handoff/ucp-checkout/index.js +36 -0
- package/dist/handoff/ucp-checkout/index.js.map +1 -0
- package/dist/handoff/ucp-checkout/parse.d.ts +71 -0
- package/dist/handoff/ucp-checkout/parse.js +349 -0
- package/dist/handoff/ucp-checkout/parse.js.map +1 -0
- package/dist/handoff/ucp-checkout/totals.d.ts +41 -0
- package/dist/handoff/ucp-checkout/totals.js +36 -0
- package/dist/handoff/ucp-checkout/totals.js.map +1 -0
- package/dist/handoff/ucp-checkout/types.d.ts +149 -0
- package/dist/handoff/ucp-checkout/types.js +58 -0
- package/dist/handoff/ucp-checkout/types.js.map +1 -0
- package/dist/handoff/url-safety.d.ts +44 -0
- package/dist/handoff/url-safety.js +121 -0
- package/dist/handoff/url-safety.js.map +1 -0
- package/dist/i18n.d.ts +24 -0
- package/dist/i18n.js +27 -0
- package/dist/i18n.js.map +1 -0
- package/dist/negotiation/action-candidate.d.ts +97 -0
- package/dist/negotiation/action-candidate.js +160 -0
- package/dist/negotiation/action-candidate.js.map +1 -0
- package/dist/negotiation/condition/evaluator.d.ts +36 -0
- package/dist/negotiation/condition/evaluator.js +123 -0
- package/dist/negotiation/condition/evaluator.js.map +1 -0
- package/dist/negotiation/context-map/index.d.ts +23 -0
- package/dist/negotiation/context-map/index.js +22 -0
- package/dist/negotiation/context-map/index.js.map +1 -0
- package/dist/negotiation/context-map/store.d.ts +51 -0
- package/dist/negotiation/context-map/store.js +186 -0
- package/dist/negotiation/context-map/store.js.map +1 -0
- package/dist/negotiation/context-map/types.d.ts +29 -0
- package/dist/negotiation/context-map/types.js +57 -0
- package/dist/negotiation/context-map/types.js.map +1 -0
- package/dist/negotiation/domain/common.d.ts +95 -0
- package/dist/negotiation/domain/common.js +216 -0
- package/dist/negotiation/domain/common.js.map +1 -0
- package/dist/negotiation/domain/envelope.d.ts +55 -0
- package/dist/negotiation/domain/envelope.js +110 -0
- package/dist/negotiation/domain/envelope.js.map +1 -0
- package/dist/negotiation/domain/identifiers.d.ts +65 -0
- package/dist/negotiation/domain/identifiers.js +130 -0
- package/dist/negotiation/domain/identifiers.js.map +1 -0
- package/dist/negotiation/domain/objects.d.ts +149 -0
- package/dist/negotiation/domain/objects.js +335 -0
- package/dist/negotiation/domain/objects.js.map +1 -0
- package/dist/negotiation/idempotency/index.d.ts +26 -0
- package/dist/negotiation/idempotency/index.js +25 -0
- package/dist/negotiation/idempotency/index.js.map +1 -0
- package/dist/negotiation/idempotency/store.d.ts +52 -0
- package/dist/negotiation/idempotency/store.js +189 -0
- package/dist/negotiation/idempotency/store.js.map +1 -0
- package/dist/negotiation/idempotency/types.d.ts +102 -0
- package/dist/negotiation/idempotency/types.js +47 -0
- package/dist/negotiation/idempotency/types.js.map +1 -0
- package/dist/negotiation/jcs.d.ts +21 -0
- package/dist/negotiation/jcs.js +114 -0
- package/dist/negotiation/jcs.js.map +1 -0
- package/dist/negotiation/ledger/event.d.ts +142 -0
- package/dist/negotiation/ledger/event.js +206 -0
- package/dist/negotiation/ledger/event.js.map +1 -0
- package/dist/negotiation/ledger/index.d.ts +26 -0
- package/dist/negotiation/ledger/index.js +25 -0
- package/dist/negotiation/ledger/index.js.map +1 -0
- package/dist/negotiation/ledger/store.d.ts +97 -0
- package/dist/negotiation/ledger/store.js +338 -0
- package/dist/negotiation/ledger/store.js.map +1 -0
- package/dist/negotiation/prompt.d.ts +34 -0
- package/dist/negotiation/prompt.js +113 -0
- package/dist/negotiation/prompt.js.map +1 -0
- package/dist/negotiation/recovery/index.d.ts +26 -0
- package/dist/negotiation/recovery/index.js +24 -0
- package/dist/negotiation/recovery/index.js.map +1 -0
- package/dist/negotiation/recovery/outbound.d.ts +49 -0
- package/dist/negotiation/recovery/outbound.js +34 -0
- package/dist/negotiation/recovery/outbound.js.map +1 -0
- package/dist/negotiation/recovery/recover.d.ts +59 -0
- package/dist/negotiation/recovery/recover.js +486 -0
- package/dist/negotiation/recovery/recover.js.map +1 -0
- package/dist/negotiation/recovery/types.d.ts +85 -0
- package/dist/negotiation/recovery/types.js +17 -0
- package/dist/negotiation/recovery/types.js.map +1 -0
- package/dist/negotiation/state/phase.d.ts +70 -0
- package/dist/negotiation/state/phase.js +200 -0
- package/dist/negotiation/state/phase.js.map +1 -0
- package/dist/negotiation/types.d.ts +139 -0
- package/dist/negotiation/types.js +24 -0
- package/dist/negotiation/types.js.map +1 -0
- package/dist/net/safe-http.d.ts +54 -0
- package/dist/net/safe-http.js +116 -0
- package/dist/net/safe-http.js.map +1 -0
- package/dist/operator/controller.d.ts +181 -0
- package/dist/operator/controller.js +784 -0
- package/dist/operator/controller.js.map +1 -0
- package/dist/operator/runner.d.ts +128 -0
- package/dist/operator/runner.js +248 -0
- package/dist/operator/runner.js.map +1 -0
- package/dist/operator/store.d.ts +39 -0
- package/dist/operator/store.js +169 -0
- package/dist/operator/store.js.map +1 -0
- package/dist/operator/strategy.d.ts +54 -0
- package/dist/operator/strategy.js +249 -0
- package/dist/operator/strategy.js.map +1 -0
- package/dist/operator/tui.d.ts +27 -0
- package/dist/operator/tui.js +395 -0
- package/dist/operator/tui.js.map +1 -0
- package/dist/operator/types.d.ts +224 -0
- package/dist/operator/types.js +17 -0
- package/dist/operator/types.js.map +1 -0
- package/dist/product-buyer.d.ts +74 -0
- package/dist/product-buyer.js +161 -0
- package/dist/product-buyer.js.map +1 -0
- package/dist/product-cli.d.ts +56 -0
- package/dist/product-cli.js +175 -0
- package/dist/product-cli.js.map +1 -0
- package/dist/product-compat.d.ts +50 -0
- package/dist/product-compat.js +63 -0
- package/dist/product-compat.js.map +1 -0
- package/dist/product-init.d.ts +73 -0
- package/dist/product-init.js +203 -0
- package/dist/product-init.js.map +1 -0
- package/dist/product-publish.d.ts +85 -0
- package/dist/product-publish.js +216 -0
- package/dist/product-publish.js.map +1 -0
- package/dist/protocol/legacy-shopping-negotiation/adapter.d.ts +42 -0
- package/dist/protocol/legacy-shopping-negotiation/adapter.js +561 -0
- package/dist/protocol/legacy-shopping-negotiation/adapter.js.map +1 -0
- package/dist/protocol/legacy-shopping-negotiation/mapping.d.ts +78 -0
- package/dist/protocol/legacy-shopping-negotiation/mapping.js +321 -0
- package/dist/protocol/legacy-shopping-negotiation/mapping.js.map +1 -0
- package/dist/protocol/legacy-shopping-negotiation/money.d.ts +49 -0
- package/dist/protocol/legacy-shopping-negotiation/money.js +70 -0
- package/dist/protocol/legacy-shopping-negotiation/money.js.map +1 -0
- package/dist/protocol/legacy-shopping-negotiation/types.d.ts +107 -0
- package/dist/protocol/legacy-shopping-negotiation/types.js +34 -0
- package/dist/protocol/legacy-shopping-negotiation/types.js.map +1 -0
- package/dist/runtime/buyer-policy.d.ts +58 -0
- package/dist/runtime/buyer-policy.js +89 -0
- package/dist/runtime/buyer-policy.js.map +1 -0
- package/dist/runtime/fake-model.d.ts +77 -0
- package/dist/runtime/fake-model.js +307 -0
- package/dist/runtime/fake-model.js.map +1 -0
- package/dist/runtime/foreground.d.ts +56 -0
- package/dist/runtime/foreground.js +97 -0
- package/dist/runtime/foreground.js.map +1 -0
- package/dist/runtime/heartbeat.d.ts +36 -0
- package/dist/runtime/heartbeat.js +49 -0
- package/dist/runtime/heartbeat.js.map +1 -0
- package/dist/runtime/merchant-turn.d.ts +35 -0
- package/dist/runtime/merchant-turn.js +20 -0
- package/dist/runtime/merchant-turn.js.map +1 -0
- package/dist/runtime/model.d.ts +40 -0
- package/dist/runtime/model.js +86 -0
- package/dist/runtime/model.js.map +1 -0
- package/dist/runtime/negotiation-turn.d.ts +101 -0
- package/dist/runtime/negotiation-turn.js +317 -0
- package/dist/runtime/negotiation-turn.js.map +1 -0
- package/dist/runtime/tools.d.ts +64 -0
- package/dist/runtime/tools.js +236 -0
- package/dist/runtime/tools.js.map +1 -0
- package/dist/supervisor/init.d.ts +31 -0
- package/dist/supervisor/init.js +222 -0
- package/dist/supervisor/init.js.map +1 -0
- package/dist/supervisor/logs.d.ts +28 -0
- package/dist/supervisor/logs.js +101 -0
- package/dist/supervisor/logs.js.map +1 -0
- package/dist/supervisor/manage.d.ts +72 -0
- package/dist/supervisor/manage.js +419 -0
- package/dist/supervisor/manage.js.map +1 -0
- package/dist/supervisor/manifest.d.ts +69 -0
- package/dist/supervisor/manifest.js +209 -0
- package/dist/supervisor/manifest.js.map +1 -0
- package/dist/supervisor/stack-config.d.ts +79 -0
- package/dist/supervisor/stack-config.js +262 -0
- package/dist/supervisor/stack-config.js.map +1 -0
- package/dist/supervisor/wrapper.d.ts +17 -0
- package/dist/supervisor/wrapper.js +138 -0
- package/dist/supervisor/wrapper.js.map +1 -0
- package/dist/trust/identity/auth-verifier.d.ts +96 -0
- package/dist/trust/identity/auth-verifier.js +166 -0
- package/dist/trust/identity/auth-verifier.js.map +1 -0
- package/dist/trust/identity/identity-binding.d.ts +63 -0
- package/dist/trust/identity/identity-binding.js +59 -0
- package/dist/trust/identity/identity-binding.js.map +1 -0
- package/dist/trust/identity/index.d.ts +38 -0
- package/dist/trust/identity/index.js +31 -0
- package/dist/trust/identity/index.js.map +1 -0
- package/dist/trust/identity/jws.d.ts +64 -0
- package/dist/trust/identity/jws.js +178 -0
- package/dist/trust/identity/jws.js.map +1 -0
- package/dist/trust/identity/keys.d.ts +71 -0
- package/dist/trust/identity/keys.js +118 -0
- package/dist/trust/identity/keys.js.map +1 -0
- package/dist/trust/identity/message-signature.d.ts +105 -0
- package/dist/trust/identity/message-signature.js +297 -0
- package/dist/trust/identity/message-signature.js.map +1 -0
- package/dist/trust/identity/nonce.d.ts +45 -0
- package/dist/trust/identity/nonce.js +50 -0
- package/dist/trust/identity/nonce.js.map +1 -0
- package/dist/trust/identity/signature-base.d.ts +84 -0
- package/dist/trust/identity/signature-base.js +261 -0
- package/dist/trust/identity/signature-base.js.map +1 -0
- package/dist/trust/identity/trust-policy.d.ts +92 -0
- package/dist/trust/identity/trust-policy.js +95 -0
- package/dist/trust/identity/trust-policy.js.map +1 -0
- package/dist/trust/index.d.ts +21 -0
- package/dist/trust/index.js +22 -0
- package/dist/trust/index.js.map +1 -0
- package/dist/trust/records/evaluator.d.ts +76 -0
- package/dist/trust/records/evaluator.js +122 -0
- package/dist/trust/records/evaluator.js.map +1 -0
- package/dist/trust/records/index.d.ts +28 -0
- package/dist/trust/records/index.js +26 -0
- package/dist/trust/records/index.js.map +1 -0
- package/dist/trust/records/store.d.ts +75 -0
- package/dist/trust/records/store.js +339 -0
- package/dist/trust/records/store.js.map +1 -0
- package/dist/trust/records/types.d.ts +159 -0
- package/dist/trust/records/types.js +40 -0
- package/dist/trust/records/types.js.map +1 -0
- package/dist/tui/banner.d.ts +46 -0
- package/dist/tui/banner.js +99 -0
- package/dist/tui/banner.js.map +1 -0
- package/dist/tui/styles.d.ts +70 -0
- package/dist/tui/styles.js +197 -0
- package/dist/tui/styles.js.map +1 -0
- package/dist/tui/theme.d.ts +63 -0
- package/dist/tui/theme.js +136 -0
- package/dist/tui/theme.js.map +1 -0
- package/examples/profiles/buyer.deepseek.yaml +34 -0
- package/examples/profiles/buyer.fake.yaml +36 -0
- package/examples/profiles/buyer.local.yaml +40 -0
- package/examples/profiles/merchant.deepseek.yaml +32 -0
- package/examples/profiles/merchant.fake.yaml +31 -0
- package/examples/profiles/merchant.local.yaml +34 -0
- package/package.json +55 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { ConnectorError } from "../connector/types.js";
|
|
17
|
+
import { observationHash, runSearchCycle } from "./search-loop.js";
|
|
18
|
+
import { BuyerTaskError } from "./types.js";
|
|
19
|
+
const DEFAULT_BUDGET = { max_requests: 20, max_rules: 50, max_tasks: 5 };
|
|
20
|
+
export class TaskScheduler {
|
|
21
|
+
store;
|
|
22
|
+
connectors;
|
|
23
|
+
now;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.store = options.store;
|
|
26
|
+
this.connectors = new Map(options.connectors.map((c) => [c.connector_id, c]));
|
|
27
|
+
const clock = options.now ?? (() => new Date().toISOString());
|
|
28
|
+
this.now = () => new Date(Date.parse(clock())).toISOString();
|
|
29
|
+
}
|
|
30
|
+
connectorFor(candidateConnectorId) {
|
|
31
|
+
return this.connectors.get(candidateConnectorId);
|
|
32
|
+
}
|
|
33
|
+
async tick(budget = {}) {
|
|
34
|
+
const b = { ...DEFAULT_BUDGET, ...budget };
|
|
35
|
+
const now = this.now();
|
|
36
|
+
const result = {
|
|
37
|
+
checked_rules: 0,
|
|
38
|
+
notifications: [],
|
|
39
|
+
tasks_searched: [],
|
|
40
|
+
tasks_expired: [],
|
|
41
|
+
errors: [],
|
|
42
|
+
};
|
|
43
|
+
let requests = 0;
|
|
44
|
+
// 1. Expire tracking tasks past their deadline (§11.3: tracking -> expired).
|
|
45
|
+
for (const task of this.store.expirableTasks(now)) {
|
|
46
|
+
try {
|
|
47
|
+
this.store.transitionTask({
|
|
48
|
+
task_id: task.task_id,
|
|
49
|
+
to: "expired",
|
|
50
|
+
expected_version: task.version,
|
|
51
|
+
event_type: "expired",
|
|
52
|
+
origin: "scheduler",
|
|
53
|
+
idempotency_key: `expire:${task.task_id}:${task.expires_at ?? now}`,
|
|
54
|
+
});
|
|
55
|
+
result.tasks_expired.push(task.task_id);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
result.errors.push(`expire ${task.task_id}: ${err instanceof Error ? err.message : String(err)}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// 2. Due tracking rules, grouped by candidate (merged observe+notify).
|
|
62
|
+
const due = this.store.dueRules(now, b.max_rules);
|
|
63
|
+
const byCandidate = new Map();
|
|
64
|
+
const taskLevel = [];
|
|
65
|
+
for (const rule of due) {
|
|
66
|
+
if (rule.candidate_id === undefined)
|
|
67
|
+
taskLevel.push(rule);
|
|
68
|
+
else {
|
|
69
|
+
const group = byCandidate.get(rule.candidate_id) ?? [];
|
|
70
|
+
group.push(rule);
|
|
71
|
+
byCandidate.set(rule.candidate_id, group);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const [candidateId, rules] of byCandidate) {
|
|
75
|
+
if (requests >= b.max_requests)
|
|
76
|
+
break;
|
|
77
|
+
const candidate = this.store.getCandidate(candidateId);
|
|
78
|
+
if (candidate === undefined || candidate.sku === undefined) {
|
|
79
|
+
for (const rule of rules)
|
|
80
|
+
this.store.markRuleChecked(rule.rule_id, false, now);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const connector = this.connectorFor(candidate.connector_id);
|
|
84
|
+
if (connector === undefined) {
|
|
85
|
+
result.errors.push(`no connector ${candidate.connector_id} for candidate ${candidateId}`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let observation;
|
|
89
|
+
requests += 1;
|
|
90
|
+
// §11.7: observation freshness TTL comes from the task's tracking
|
|
91
|
+
// policy, not a hardcoded 30 minutes.
|
|
92
|
+
const task = this.store.getTask(candidate.task_id);
|
|
93
|
+
const ttlSeconds = task?.tracking_policy.observation_ttl_seconds ?? 1800;
|
|
94
|
+
try {
|
|
95
|
+
const product = await connector.getProduct(candidate.sku);
|
|
96
|
+
const added = this.store.addObservation({
|
|
97
|
+
candidate_id: candidateId,
|
|
98
|
+
observed_at: now,
|
|
99
|
+
source_url_or_ref: `shopping-cli:/products/${product.sku}`,
|
|
100
|
+
title: product.title,
|
|
101
|
+
price: {
|
|
102
|
+
list: product.price,
|
|
103
|
+
currency: product.currency,
|
|
104
|
+
delivery_fee: product.delivery.fee,
|
|
105
|
+
},
|
|
106
|
+
promotion: {},
|
|
107
|
+
stock: { quantity: product.stock, observed_at: now },
|
|
108
|
+
delivery: { ...product.delivery },
|
|
109
|
+
after_sales: {},
|
|
110
|
+
merchant: {
|
|
111
|
+
id: product.merchant.id,
|
|
112
|
+
name: product.merchant.name,
|
|
113
|
+
city: product.merchant.city,
|
|
114
|
+
warnings: product.warnings,
|
|
115
|
+
},
|
|
116
|
+
content_hash: observationHash(product),
|
|
117
|
+
fresh_until: new Date(Date.parse(now) + ttlSeconds * 1000).toISOString(),
|
|
118
|
+
});
|
|
119
|
+
if (added.added) {
|
|
120
|
+
this.store.updateCandidate(candidateId, { latest_observation_id: added.observation_id });
|
|
121
|
+
}
|
|
122
|
+
observation = this.store.latestObservation(candidateId);
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
// §18.2: a failed read never reuses stale facts; reschedule and report.
|
|
126
|
+
result.errors.push(`observe ${candidateId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
127
|
+
for (const rule of rules)
|
|
128
|
+
this.store.markRuleChecked(rule.rule_id, false, now);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (observation === undefined)
|
|
132
|
+
continue;
|
|
133
|
+
const previous = this.store.observations(candidateId, 2)[1];
|
|
134
|
+
const triggered = [];
|
|
135
|
+
for (const rule of rules) {
|
|
136
|
+
result.checked_rules += 1;
|
|
137
|
+
const inCooldown = rule.cooldown_seconds > 0 &&
|
|
138
|
+
rule.last_triggered_at !== undefined &&
|
|
139
|
+
Date.parse(rule.last_triggered_at) + rule.cooldown_seconds * 1000 > Date.parse(now);
|
|
140
|
+
const reason = inCooldown ? undefined : evaluateRule(rule, observation, previous, now);
|
|
141
|
+
if (reason !== undefined) {
|
|
142
|
+
triggered.push({ rule, reason });
|
|
143
|
+
this.store.markRuleChecked(rule.rule_id, true, now);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.store.markRuleChecked(rule.rule_id, false, now);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (triggered.length > 0) {
|
|
150
|
+
// Merged: one notification per candidate per observation (§11.7). The
|
|
151
|
+
// event is keyed by observation_id, so an unchanged fact re-triggering
|
|
152
|
+
// after cooldown does NOT surface a duplicate notification.
|
|
153
|
+
const summary = triggered.map((t) => t.reason).join(";");
|
|
154
|
+
const inserted = this.store.appendEvent(rules[0]?.task_id ?? "", "notification", {
|
|
155
|
+
candidate_id: candidateId,
|
|
156
|
+
rule_ids: triggered.map((t) => t.rule.rule_id),
|
|
157
|
+
summary,
|
|
158
|
+
}, "scheduler", `notify:${candidateId}:${observation.observation_id}`);
|
|
159
|
+
if (inserted) {
|
|
160
|
+
result.notifications.push({
|
|
161
|
+
task_id: rules[0]?.task_id ?? "",
|
|
162
|
+
candidate_id: candidateId,
|
|
163
|
+
summary,
|
|
164
|
+
rule_ids: triggered.map((t) => t.rule.rule_id),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// 3. Task-level rules (periodic_review / price_below / stock_available)
|
|
170
|
+
// queue a re-search. 求值在重搜索之后(第 5 步)——任务级规则针对的是
|
|
171
|
+
// "任务任一候选满足条件"(如默认 price_below 阈值),必须基于本 tick
|
|
172
|
+
// 重搜索产生的新鲜观察,否则条件形同虚设(评审项 H5:此前只
|
|
173
|
+
// markRuleChecked,条件从未被求值,承诺的"降价/到货通知"静默失效)。
|
|
174
|
+
const searchQueue = new Map();
|
|
175
|
+
for (const rule of taskLevel) {
|
|
176
|
+
const task = this.store.getTask(rule.task_id);
|
|
177
|
+
if (task !== undefined && task.status === "tracking") {
|
|
178
|
+
searchQueue.set(task.task_id, task);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// 不重搜的任务:仅推进检查时间,避免每 tick 重复入队。
|
|
182
|
+
result.checked_rules += 1;
|
|
183
|
+
this.store.markRuleChecked(rule.rule_id, true, now);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// 4. Due task wakeups (restart recovery: everything comes from the DB).
|
|
187
|
+
for (const task of this.store.dueTasks(now, b.max_tasks)) {
|
|
188
|
+
searchQueue.set(task.task_id, task);
|
|
189
|
+
}
|
|
190
|
+
let searched = 0;
|
|
191
|
+
for (const task of searchQueue.values()) {
|
|
192
|
+
if (searched >= b.max_tasks)
|
|
193
|
+
break;
|
|
194
|
+
const connector = this.connectorFor(task.connector_scope.connectors[0] ?? "shopping-cli");
|
|
195
|
+
if (connector === undefined) {
|
|
196
|
+
result.errors.push(`no connector for task ${task.task_id}`);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
searched += 1;
|
|
200
|
+
try {
|
|
201
|
+
const before = new Set(this.store.listCandidates(task.task_id).map((c) => c.canonical_key));
|
|
202
|
+
const cycle = await runSearchCycle({ store: this.store, connector, now: this.now }, task.task_id, `tick:${task.task_id}:${now}`);
|
|
203
|
+
result.tasks_searched.push(task.task_id);
|
|
204
|
+
if (cycle.outcome === "retry" && cycle.error !== undefined) {
|
|
205
|
+
// §18.1 transient failure: runSearchCycle parked the task with a
|
|
206
|
+
// scheduled retry but did NOT throw. Surface it in tick errors so
|
|
207
|
+
// the chat/model can see the task is retrying — otherwise a
|
|
208
|
+
// persistently down gateway looks like a silently stalled task.
|
|
209
|
+
result.errors.push(`search ${task.task_id}: ${cycle.error}(已安排自动重试)`);
|
|
210
|
+
}
|
|
211
|
+
const newOnes = this.store
|
|
212
|
+
.listCandidates(task.task_id)
|
|
213
|
+
.filter((c) => !before.has(c.canonical_key));
|
|
214
|
+
if (newOnes.length > 0) {
|
|
215
|
+
const inserted = this.store.appendEvent(task.task_id, "notification", {
|
|
216
|
+
summary: `发现 ${newOnes.length} 个新候选`,
|
|
217
|
+
new_candidates: newOnes.map((c) => c.candidate_id),
|
|
218
|
+
}, "scheduler", `notify:${task.task_id}:new:${now}`);
|
|
219
|
+
if (inserted) {
|
|
220
|
+
result.notifications.push({
|
|
221
|
+
task_id: task.task_id,
|
|
222
|
+
summary: `发现 ${newOnes.length} 个新候选`,
|
|
223
|
+
rule_ids: [],
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
void cycle;
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
if (err instanceof BuyerTaskError || err instanceof ConnectorError) {
|
|
231
|
+
result.errors.push(`search ${task.task_id}: ${err.message}`);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
result.errors.push(`search ${task.task_id}: ${err instanceof Error ? err.message : String(err)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// 5. Task-level rules evaluated against this tick's fresh observations
|
|
239
|
+
// (评审项 H5 修复):只对本 tick 实际重搜过的任务求值——重搜写入了
|
|
240
|
+
// 新观察,规则看到的是当前事实。任一候选满足条件即触发(任务级
|
|
241
|
+
// price_below 的阈值是全任务预算;stock_available 同理)。通知按
|
|
242
|
+
// (task_id, rule_id, 触发观察) 去重——观察未变时不重复打扰用户。
|
|
243
|
+
const searchedTasks = new Set(result.tasks_searched);
|
|
244
|
+
for (const rule of taskLevel) {
|
|
245
|
+
if (!searchedTasks.has(rule.task_id))
|
|
246
|
+
continue;
|
|
247
|
+
const task = this.store.getTask(rule.task_id);
|
|
248
|
+
if (task === undefined)
|
|
249
|
+
continue;
|
|
250
|
+
result.checked_rules += 1;
|
|
251
|
+
let triggeredReason;
|
|
252
|
+
let triggerObservationId;
|
|
253
|
+
for (const candidate of this.store.listCandidates(rule.task_id)) {
|
|
254
|
+
if (candidate.sku === undefined)
|
|
255
|
+
continue;
|
|
256
|
+
const observation = this.store.latestObservation(candidate.candidate_id);
|
|
257
|
+
if (observation === undefined)
|
|
258
|
+
continue;
|
|
259
|
+
const previous = this.store.observations(candidate.candidate_id, 2)[1];
|
|
260
|
+
const inCooldown = rule.cooldown_seconds > 0 &&
|
|
261
|
+
rule.last_triggered_at !== undefined &&
|
|
262
|
+
Date.parse(rule.last_triggered_at) + rule.cooldown_seconds * 1000 > Date.parse(now);
|
|
263
|
+
const reason = inCooldown ? undefined : evaluateRule(rule, observation, previous, now);
|
|
264
|
+
if (reason !== undefined) {
|
|
265
|
+
triggeredReason = reason;
|
|
266
|
+
triggerObservationId = observation.observation_id;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (triggeredReason === undefined || triggerObservationId === undefined) {
|
|
271
|
+
this.store.markRuleChecked(rule.rule_id, false, now);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
this.store.markRuleChecked(rule.rule_id, true, now);
|
|
275
|
+
const inserted = this.store.appendEvent(rule.task_id, "notification", {
|
|
276
|
+
rule_ids: [rule.rule_id],
|
|
277
|
+
summary: triggeredReason,
|
|
278
|
+
}, "scheduler", `notify:${rule.task_id}:${rule.rule_id}:${triggerObservationId}`);
|
|
279
|
+
if (inserted) {
|
|
280
|
+
result.notifications.push({
|
|
281
|
+
task_id: rule.task_id,
|
|
282
|
+
summary: triggeredReason,
|
|
283
|
+
rule_ids: [rule.rule_id],
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** Evaluate one rule against the latest facts. Returns a reason when triggered. */
|
|
291
|
+
function evaluateRule(rule, latest, previous, now) {
|
|
292
|
+
switch (rule.rule_type) {
|
|
293
|
+
case "price_below": {
|
|
294
|
+
const threshold = Number(rule.condition.threshold);
|
|
295
|
+
if (!Number.isFinite(threshold))
|
|
296
|
+
return undefined;
|
|
297
|
+
const total = latest.price.list + latest.price.delivery_fee;
|
|
298
|
+
return total <= threshold
|
|
299
|
+
? `到手价 ${total} ${latest.price.currency} 已低于 ${threshold}`
|
|
300
|
+
: undefined;
|
|
301
|
+
}
|
|
302
|
+
case "stock_available":
|
|
303
|
+
return latest.stock.quantity > 0 ? `已到货(库存 ${latest.stock.quantity})` : undefined;
|
|
304
|
+
case "delivery_before": {
|
|
305
|
+
const before = typeof rule.condition.eta_before === "string" ? rule.condition.eta_before : "";
|
|
306
|
+
if (before === "")
|
|
307
|
+
return undefined;
|
|
308
|
+
const etaMinutes = Number(latest.delivery.eta_minutes ?? 0);
|
|
309
|
+
return Date.parse(now) + etaMinutes * 60_000 <= Date.parse(before)
|
|
310
|
+
? `可在 ${before} 前送达`
|
|
311
|
+
: undefined;
|
|
312
|
+
}
|
|
313
|
+
default:
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
//# sourceMappingURL=scheduler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../../../src/agent/buyer/scheduler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAcH,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGnE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA6B5C,MAAM,cAAc,GAAyB,EAAE,YAAY,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;AAE/F,MAAM,OAAO,aAAa;IACP,KAAK,CAAiB;IACtB,UAAU,CAAiC;IAC3C,GAAG,CAAe;IAEnC,YAAY,OAAyB;QACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9E,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;QAC9D,IAAI,CAAC,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,WA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 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { CommerceConnector, ConnectorProduct } from "../connector/types.js";
|
|
17
|
+
import type { BuyerTaskStore } from "./task-store.js";
|
|
18
|
+
import type { BuyerTask, ProductCandidate, ProductObservation } from "./types.js";
|
|
19
|
+
export interface SearchCycleDeps {
|
|
20
|
+
store: BuyerTaskStore;
|
|
21
|
+
connector: CommerceConnector;
|
|
22
|
+
now: () => string;
|
|
23
|
+
}
|
|
24
|
+
export interface ShortlistEntry {
|
|
25
|
+
candidate: ProductCandidate;
|
|
26
|
+
observation?: ProductObservation;
|
|
27
|
+
}
|
|
28
|
+
export interface SearchCycleResult {
|
|
29
|
+
task: BuyerTask;
|
|
30
|
+
/** `retry` = transient connector error, task parked for a scheduled retry. */
|
|
31
|
+
outcome: "shortlist_ready" | "tracking" | "failed" | "retry";
|
|
32
|
+
shortlist: ShortlistEntry[];
|
|
33
|
+
error?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Content-hash over the assertion-relevant facts (§11.6 dedup). Includes
|
|
37
|
+
* delivery ETA so a changed delivery promise produces a NEW observation
|
|
38
|
+
* instead of being silently deduped away (which would keep `delivery_before`
|
|
39
|
+
* rules and the delivery dimension evaluating stale ETAs).
|
|
40
|
+
*/
|
|
41
|
+
export declare function observationHash(product: ConnectorProduct): string;
|
|
42
|
+
export declare function runSearchCycle(deps: SearchCycleDeps, taskId: string, runId: string): Promise<SearchCycleResult>;
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Buyer search execution loop (design §13):
|
|
18
|
+
* ready -> searching -> (shortlist_ready -> awaiting_user | tracking).
|
|
19
|
+
*
|
|
20
|
+
* Every run has a run_id; every event carries an idempotency key derived
|
|
21
|
+
* from it, so a retried run never double-writes. Transitions are
|
|
22
|
+
* version-guarded. Connector failures classify: transient => the task
|
|
23
|
+
* moves to `failed` with a retriable note (never silently keeps stale
|
|
24
|
+
* facts, §18.2).
|
|
25
|
+
*/
|
|
26
|
+
import { createHash } from "node:crypto";
|
|
27
|
+
import { ConnectorError } from "../connector/types.js";
|
|
28
|
+
import { hardFilter, scoreCandidate } from "./ranker.js";
|
|
29
|
+
import { BuyerTaskError } from "./types.js";
|
|
30
|
+
/**
|
|
31
|
+
* Content-hash over the assertion-relevant facts (§11.6 dedup). Includes
|
|
32
|
+
* delivery ETA so a changed delivery promise produces a NEW observation
|
|
33
|
+
* instead of being silently deduped away (which would keep `delivery_before`
|
|
34
|
+
* rules and the delivery dimension evaluating stale ETAs).
|
|
35
|
+
*/
|
|
36
|
+
export function observationHash(product) {
|
|
37
|
+
return createHash("sha256")
|
|
38
|
+
.update(JSON.stringify({
|
|
39
|
+
title: product.title,
|
|
40
|
+
price: product.price,
|
|
41
|
+
fee: product.delivery.fee,
|
|
42
|
+
stock: product.stock,
|
|
43
|
+
eta_minutes: product.delivery.eta_minutes,
|
|
44
|
+
warnings: product.warnings,
|
|
45
|
+
}))
|
|
46
|
+
.digest("hex");
|
|
47
|
+
}
|
|
48
|
+
function toObservation(candidateId, product, now, ttlSeconds) {
|
|
49
|
+
return {
|
|
50
|
+
candidate_id: candidateId,
|
|
51
|
+
observed_at: now,
|
|
52
|
+
source_url_or_ref: `shopping-cli:/products/${product.sku}`,
|
|
53
|
+
title: product.title,
|
|
54
|
+
price: { list: product.price, currency: product.currency, delivery_fee: product.delivery.fee },
|
|
55
|
+
promotion: {},
|
|
56
|
+
stock: { quantity: product.stock, observed_at: now },
|
|
57
|
+
delivery: { ...product.delivery },
|
|
58
|
+
after_sales: {},
|
|
59
|
+
merchant: {
|
|
60
|
+
id: product.merchant.id,
|
|
61
|
+
name: product.merchant.name,
|
|
62
|
+
city: product.merchant.city,
|
|
63
|
+
warnings: product.warnings,
|
|
64
|
+
},
|
|
65
|
+
content_hash: observationHash(product),
|
|
66
|
+
fresh_until: new Date(Date.parse(now) + ttlSeconds * 1000).toISOString(),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Install default tracking rules from constraints when none are active (§13). */
|
|
70
|
+
function installDefaultRules(deps, task) {
|
|
71
|
+
const { store } = deps;
|
|
72
|
+
const active = store.rulesForTask(task.task_id).filter((r) => r.status === "active");
|
|
73
|
+
if (active.length > 0)
|
|
74
|
+
return active;
|
|
75
|
+
const installed = [];
|
|
76
|
+
const interval = task.tracking_policy.default_interval_seconds;
|
|
77
|
+
const cooldown = task.tracking_policy.default_cooldown_seconds;
|
|
78
|
+
if (task.constraints.max_total_price !== undefined) {
|
|
79
|
+
installed.push(store.addTrackingRule({
|
|
80
|
+
task_id: task.task_id,
|
|
81
|
+
rule_type: "price_below",
|
|
82
|
+
condition: { threshold: task.constraints.max_total_price },
|
|
83
|
+
interval_seconds: interval,
|
|
84
|
+
cooldown_seconds: cooldown,
|
|
85
|
+
idempotency_key: `${task.task_id}:default:price_below`,
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
installed.push(store.addTrackingRule({
|
|
89
|
+
task_id: task.task_id,
|
|
90
|
+
rule_type: "stock_available",
|
|
91
|
+
condition: {},
|
|
92
|
+
interval_seconds: interval,
|
|
93
|
+
cooldown_seconds: cooldown,
|
|
94
|
+
idempotency_key: `${task.task_id}:default:stock_available`,
|
|
95
|
+
}));
|
|
96
|
+
installed.push(store.addTrackingRule({
|
|
97
|
+
task_id: task.task_id,
|
|
98
|
+
rule_type: "periodic_review",
|
|
99
|
+
condition: {},
|
|
100
|
+
interval_seconds: interval,
|
|
101
|
+
cooldown_seconds: cooldown,
|
|
102
|
+
idempotency_key: `${task.task_id}:default:periodic_review`,
|
|
103
|
+
}));
|
|
104
|
+
return installed;
|
|
105
|
+
}
|
|
106
|
+
export async function runSearchCycle(deps, taskId, runId) {
|
|
107
|
+
const { store, connector } = deps;
|
|
108
|
+
// Normalize to UTC ISO (lexicographic timestamp comparisons downstream).
|
|
109
|
+
const now = () => new Date(Date.parse(deps.now())).toISOString();
|
|
110
|
+
let task = store.getTask(taskId);
|
|
111
|
+
if (task === undefined)
|
|
112
|
+
throw new BuyerTaskError("not_found", `no task ${taskId}`);
|
|
113
|
+
if (!["ready", "searching", "tracking"].includes(task.status)) {
|
|
114
|
+
throw new BuyerTaskError("illegal_transition", `task ${taskId} is ${task.status}; search requires ready/searching/tracking`);
|
|
115
|
+
}
|
|
116
|
+
// A private budget may be vaulted (max_total_price_vault_ref); resolve it
|
|
117
|
+
// here so filtering and default rules never see the plaintext value and
|
|
118
|
+
// never skip the constraint when it lives in the Vault (§11.2).
|
|
119
|
+
const budget = store.resolveBudget(task.constraints);
|
|
120
|
+
const effectiveConstraints = budget !== undefined ? { ...task.constraints, max_total_price: budget } : task.constraints;
|
|
121
|
+
const startedAt = now();
|
|
122
|
+
// ready/tracking -> searching (idempotent; already-searching = crash resume).
|
|
123
|
+
if (task.status !== "searching") {
|
|
124
|
+
task = store.transitionTask({
|
|
125
|
+
task_id: taskId,
|
|
126
|
+
to: "searching",
|
|
127
|
+
expected_version: task.version,
|
|
128
|
+
event_type: "search_started",
|
|
129
|
+
origin: "scheduler",
|
|
130
|
+
idempotency_key: `${runId}:search_started`,
|
|
131
|
+
// 崩溃恢复 + 防并发重复执行(评审项 H4):新激活任务的 next_run_at 为
|
|
132
|
+
// NULL(createTask 时置空、激活 ready 不设置)——searching 转移若保留
|
|
133
|
+
// NULL,崩溃后 dueTasks(要求 NOT NULL)永不选中该任务,永久卡死且
|
|
134
|
+
// 无恢复路径("already-searching = crash resume" 注释此前不成立)。
|
|
135
|
+
// 置为未来锁定期:正常完成由 tracking/shortlist 转移覆盖该值;崩溃
|
|
136
|
+
// 后到期即被 dueTasks 重新选中恢复;锁定期内下一 tick 不会重复选中
|
|
137
|
+
// 同一任务(防并发搜索)。
|
|
138
|
+
next_run_at: new Date(Date.parse(startedAt) +
|
|
139
|
+
Math.max(task.tracking_policy.default_interval_seconds, 60) * 1000).toISOString(),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
let products;
|
|
143
|
+
try {
|
|
144
|
+
products = await connector.searchProducts({
|
|
145
|
+
...(task.intent.query_text !== undefined ? { query: task.intent.query_text } : {}),
|
|
146
|
+
...(task.intent.city !== undefined ? { city: task.intent.city } : {}),
|
|
147
|
+
...(task.intent.area !== undefined ? { area: task.intent.area } : {}),
|
|
148
|
+
include_out_of_stock: task.constraints.exclude_out_of_stock === false,
|
|
149
|
+
limit: task.search_budget.max_candidates,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
154
|
+
const retriable = err instanceof ConnectorError && err.kind === "transient";
|
|
155
|
+
if (retriable) {
|
|
156
|
+
// §18.1/§13: a transient connector error must not kill the task. Park it
|
|
157
|
+
// in `tracking` with a scheduled retry (exponential backoff, capped); the
|
|
158
|
+
// scheduler's dueTasks re-runs it when next_run_at is due.
|
|
159
|
+
const attempts = store.taskEvents(taskId).filter((e) => e.type === "connector_retry").length + 1;
|
|
160
|
+
const backoffMs = Math.min(task.tracking_policy.default_interval_seconds * 1000 * 2 ** Math.min(attempts - 1, 5), 6 * 3600 * 1000);
|
|
161
|
+
const nextRunAt = new Date(Date.parse(startedAt) + backoffMs).toISOString();
|
|
162
|
+
task = store.transitionTask({
|
|
163
|
+
task_id: taskId,
|
|
164
|
+
to: "tracking",
|
|
165
|
+
expected_version: task.version,
|
|
166
|
+
event_type: "connector_retry",
|
|
167
|
+
payload: { error: message, retriable: true, attempts, next_run_at: nextRunAt },
|
|
168
|
+
origin: "connector",
|
|
169
|
+
idempotency_key: `${runId}:retry`,
|
|
170
|
+
next_run_at: nextRunAt,
|
|
171
|
+
});
|
|
172
|
+
return { task, outcome: "retry", shortlist: [], error: message };
|
|
173
|
+
}
|
|
174
|
+
task = store.transitionTask({
|
|
175
|
+
task_id: taskId,
|
|
176
|
+
to: "failed",
|
|
177
|
+
expected_version: task.version,
|
|
178
|
+
event_type: "failed",
|
|
179
|
+
payload: { error: message, retriable: false },
|
|
180
|
+
origin: "connector",
|
|
181
|
+
idempotency_key: `${runId}:failed`,
|
|
182
|
+
});
|
|
183
|
+
return { task, outcome: "failed", shortlist: [], error: message };
|
|
184
|
+
}
|
|
185
|
+
const observations = new Map();
|
|
186
|
+
const eligible = [];
|
|
187
|
+
for (const product of products.slice(0, task.search_budget.max_candidates)) {
|
|
188
|
+
const candidate = store.upsertCandidate({
|
|
189
|
+
task_id: taskId,
|
|
190
|
+
connector_id: connector.connector_id,
|
|
191
|
+
platform: connector.platform,
|
|
192
|
+
external_product_id: product.sku,
|
|
193
|
+
sku: product.sku,
|
|
194
|
+
merchant_id: product.merchant_id,
|
|
195
|
+
});
|
|
196
|
+
const obs = store.addObservation(toObservation(candidate.candidate_id, product, startedAt, task.tracking_policy.observation_ttl_seconds));
|
|
197
|
+
const filter = hardFilter(product, effectiveConstraints, startedAt);
|
|
198
|
+
const observation = store.latestObservation(candidate.candidate_id);
|
|
199
|
+
observations.set(product.sku, observation);
|
|
200
|
+
if (filter.eligible === "ineligible") {
|
|
201
|
+
store.updateCandidate(candidate.candidate_id, {
|
|
202
|
+
eligibility: "ineligible",
|
|
203
|
+
candidate_status: "rejected",
|
|
204
|
+
rejection_reasons: filter.reasons,
|
|
205
|
+
...(obs.added ? { latest_observation_id: obs.observation_id } : {}),
|
|
206
|
+
});
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const { score, explanation } = scoreCandidate(product, {
|
|
210
|
+
intent: task.intent,
|
|
211
|
+
policy: task.ranking_policy,
|
|
212
|
+
now: startedAt,
|
|
213
|
+
observations,
|
|
214
|
+
});
|
|
215
|
+
if (filter.unknowns.length > 0)
|
|
216
|
+
explanation.stale_facts.push(...filter.unknowns);
|
|
217
|
+
const updated = store.updateCandidate(candidate.candidate_id, {
|
|
218
|
+
eligibility: filter.eligible,
|
|
219
|
+
candidate_status: "tracked",
|
|
220
|
+
score,
|
|
221
|
+
score_explanation: explanation,
|
|
222
|
+
...(obs.added ? { latest_observation_id: obs.observation_id } : {}),
|
|
223
|
+
});
|
|
224
|
+
eligible.push({ candidate: updated, product });
|
|
225
|
+
}
|
|
226
|
+
store.appendEvent(taskId, "observation_added", { products: products.length, eligible: eligible.length }, "connector", `${runId}:observations`);
|
|
227
|
+
eligible.sort((a, b) => (b.candidate.score ?? 0) - (a.candidate.score ?? 0));
|
|
228
|
+
if (eligible.length > 0) {
|
|
229
|
+
const top = eligible.slice(0, 3);
|
|
230
|
+
for (const { candidate } of top) {
|
|
231
|
+
store.updateCandidate(candidate.candidate_id, { candidate_status: "shortlisted" });
|
|
232
|
+
}
|
|
233
|
+
task = store.transitionTask({
|
|
234
|
+
task_id: taskId,
|
|
235
|
+
to: "shortlist_ready",
|
|
236
|
+
expected_version: task.version,
|
|
237
|
+
event_type: "shortlisted",
|
|
238
|
+
payload: {
|
|
239
|
+
candidates: top.map(({ candidate }) => ({
|
|
240
|
+
candidate_id: candidate.candidate_id,
|
|
241
|
+
sku: candidate.sku,
|
|
242
|
+
score: candidate.score,
|
|
243
|
+
})),
|
|
244
|
+
},
|
|
245
|
+
origin: "scheduler",
|
|
246
|
+
idempotency_key: `${runId}:shortlisted`,
|
|
247
|
+
});
|
|
248
|
+
task = store.transitionTask({
|
|
249
|
+
task_id: taskId,
|
|
250
|
+
to: "awaiting_user",
|
|
251
|
+
expected_version: task.version,
|
|
252
|
+
event_type: "status_changed",
|
|
253
|
+
origin: "scheduler",
|
|
254
|
+
idempotency_key: `${runId}:awaiting_user`,
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
task,
|
|
258
|
+
outcome: "shortlist_ready",
|
|
259
|
+
shortlist: top.map(({ candidate }) => ({
|
|
260
|
+
candidate,
|
|
261
|
+
observation: observations.get(candidate.sku ?? ""),
|
|
262
|
+
})),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
// Nothing eligible yet: install tracking rules and wait for wakeups.
|
|
266
|
+
const rules = installDefaultRules(deps, { ...task, constraints: effectiveConstraints });
|
|
267
|
+
const nextRun = new Date(Date.parse(startedAt) + task.tracking_policy.default_interval_seconds * 1000).toISOString();
|
|
268
|
+
task = store.transitionTask({
|
|
269
|
+
task_id: taskId,
|
|
270
|
+
to: "tracking",
|
|
271
|
+
expected_version: task.version,
|
|
272
|
+
event_type: "tracking_installed",
|
|
273
|
+
payload: { rule_ids: rules.map((r) => r.rule_id) },
|
|
274
|
+
origin: "scheduler",
|
|
275
|
+
idempotency_key: `${runId}:tracking`,
|
|
276
|
+
next_run_at: nextRun,
|
|
277
|
+
});
|
|
278
|
+
return { task, outcome: "tracking", shortlist: [] };
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=search-loop.js.map
|