query_pending_goods_list.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import re
  2. class QueryPendingGoodsListTool:
  3. name = 'query_pending_goods_list'
  4. route_path = '/mcp/tools/queryPendingGoodsList'
  5. SHIPPING_METHODS = ('空运', '海运', '陆运')
  6. GOODS_ATTRS = ('普货', '带电', '带磁', '玩具', '敏感货', '食品')
  7. def __init__(self, api_client=None):
  8. self.api_client = api_client
  9. def metadata(self):
  10. return {
  11. 'name': self.name,
  12. 'description': (
  13. '使用场景:用户要看待审核商品时调用。只查商品审核页待审核页签,'
  14. '可以不带筛选直接看第一页。客户、起运国、目的国必须先调用'
  15. 'list_pending_goods_filter_options取得可传值;同名先让用户选定。'
  16. '第一列是商品编号,也就是审核页上的 SKU。要审核时用这个编号调用 open_goods_audit。'
  17. '禁止使用:在本工具里审核、驳回或导出,不得改走query_goods_info。'
  18. '参数只用于工具内部调用;最终回答只能展示中文业务名称;'
  19. '描述筛选条件时不得展示筛选字段的英文参数名。'
  20. ),
  21. 'input_schema': {
  22. 'type': 'object',
  23. 'properties': {
  24. 'skus': {
  25. 'type': 'array',
  26. 'maxItems': 50,
  27. 'items': {
  28. 'type': 'string',
  29. 'minLength': 1,
  30. 'maxLength': 200,
  31. },
  32. 'description': 'SKU,精确匹配,最多50个。多项表示命中任意一个。',
  33. },
  34. 'customer_id': {
  35. 'type': 'integer',
  36. 'minimum': 1,
  37. 'description': '客户ID。必须先调用筛选项选择「客户名称」取得value。',
  38. },
  39. 'departure_country': {
  40. 'type': 'string',
  41. 'pattern': '^[A-Za-z]{2,3}$',
  42. 'description': '起运国代码。必须先调用筛选项选择「起运国」取得value。',
  43. },
  44. 'destination_country': {
  45. 'type': 'string',
  46. 'pattern': '^[A-Za-z]{2,3}$',
  47. 'description': '目的国代码。必须先调用筛选项选择「目的国」取得value。',
  48. },
  49. 'shipping_method': {
  50. 'type': 'string',
  51. 'enum': list(self.SHIPPING_METHODS),
  52. 'description': '运输方式,只能是空运、海运、陆运。',
  53. },
  54. 'goods_attrs': {
  55. 'type': 'array',
  56. 'maxItems': 6,
  57. 'items': {
  58. 'type': 'string',
  59. 'enum': list(self.GOODS_ATTRS),
  60. },
  61. 'description': '商品属性。多项表示这条商品同时具备。普货是商品分类。',
  62. },
  63. 'page': {
  64. 'type': 'integer',
  65. 'minimum': 1,
  66. 'maximum': 100,
  67. 'default': 1,
  68. },
  69. 'limit': {
  70. 'type': 'integer',
  71. 'minimum': 1,
  72. 'maximum': 100,
  73. 'default': 20,
  74. },
  75. },
  76. 'additionalProperties': False,
  77. },
  78. }
  79. def call(
  80. self,
  81. skus=None,
  82. customer_id=None,
  83. departure_country=None,
  84. destination_country=None,
  85. shipping_method=None,
  86. goods_attrs=None,
  87. page=1,
  88. limit=20,
  89. request_id='rq_query_pending_goods_list',
  90. ):
  91. if self.api_client is None:
  92. raise RuntimeError(
  93. 'api client is required for query_pending_goods_list'
  94. )
  95. payload = {
  96. 'page': self._bounded_integer(page, 'page'),
  97. 'limit': self._bounded_integer(limit, 'limit'),
  98. }
  99. if skus is not None:
  100. payload['skus'] = self._skus(skus)
  101. if customer_id is not None:
  102. payload['customer_id'] = self._customer_id(customer_id)
  103. if departure_country is not None:
  104. payload['departure_country'] = self._country(
  105. departure_country, 'departure_country'
  106. )
  107. if destination_country is not None:
  108. payload['destination_country'] = self._country(
  109. destination_country, 'destination_country'
  110. )
  111. if shipping_method is not None:
  112. text = str(shipping_method).strip()
  113. if text not in self.SHIPPING_METHODS:
  114. raise ValueError('shipping_method is invalid')
  115. payload['shipping_method'] = text
  116. if goods_attrs is not None:
  117. payload['goods_attrs'] = self._attrs(goods_attrs)
  118. return self.api_client.call_tool(
  119. self.name, self.route_path, payload, request_id,
  120. )
  121. def _skus(self, values):
  122. if isinstance(values, str):
  123. values = re.split(r'[\r\n,,;;、]+', values)
  124. if not isinstance(values, list):
  125. raise ValueError('skus is invalid')
  126. result = []
  127. for item in values:
  128. for piece in re.split(r'[\r\n,,;;、]+', str(item)):
  129. sku = piece.strip()
  130. if not sku:
  131. continue
  132. if len(sku) > 200:
  133. raise ValueError('skus is invalid')
  134. if sku not in result:
  135. result.append(sku)
  136. if not result or len(result) > 50:
  137. raise ValueError('skus is invalid')
  138. return result
  139. @staticmethod
  140. def _customer_id(value):
  141. if isinstance(value, bool) or not isinstance(value, int) or value < 1:
  142. raise ValueError('customer_id is invalid')
  143. return value
  144. @staticmethod
  145. def _country(value, field):
  146. text = str(value or '').strip().upper()
  147. if not re.match(r'^[A-Z]{2,3}$', text):
  148. raise ValueError(field + ' is invalid')
  149. return text
  150. def _attrs(self, values):
  151. if not isinstance(values, list) or not values:
  152. raise ValueError('goods_attrs is invalid')
  153. result = []
  154. for item in values:
  155. name = str(item).strip()
  156. if name not in self.GOODS_ATTRS:
  157. raise ValueError('goods_attrs is invalid')
  158. if name not in result:
  159. result.append(name)
  160. return result
  161. @staticmethod
  162. def _bounded_integer(value, field):
  163. if isinstance(value, bool) or not isinstance(value, int):
  164. raise ValueError(field + ' is invalid')
  165. if value < 1 or value > 100:
  166. raise ValueError(field + ' is invalid')
  167. return value