export_out_of_province_port_data.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. class ExportOutOfProvincePortDataTool:
  2. name = 'export_out_of_province_port_data'
  3. route_path = '/mcp/tools/exportOutOfProvincePortData'
  4. def __init__(self, api_client=None):
  5. self.api_client = api_client
  6. def metadata(self):
  7. number_items = {
  8. 'type': 'string',
  9. 'minLength': 1,
  10. 'maxLength': 100,
  11. }
  12. return {
  13. 'name': self.name,
  14. 'description': (
  15. '使用场景:只有用户明确要求导出后台排舱单列表中的省外进港资料并需要'
  16. '下载文件时使用,返回可下载文件URL。'
  17. '只能在用户明确要求导出省外进港资料,并明确提供排舱单号、'
  18. '柜号、提单号或SO号中的一种,以及宁波、上海或美森资料类型时调用。'
  19. '号码类型不明确时必须先询问用户;用户没有明确号码类型时必须先询问:'
  20. '“请确认使用哪种单号导出:排舱单号、柜号、提单号还是SO号?”'
  21. '确认前不得调用。禁止使用:普通排舱查询、排舱详情、订单查询或普通文件'
  22. '查询不得调用本工具。不得根据号码格式猜测,不得跨字段或跨工具试查,'
  23. '也不得在查询失败后切换号码类型重试。一次只能选择一种号码类型。'
  24. '一次只能选择一种资料类型;用户要求多个类型时应分别调用。'
  25. '参数名仅用于工具调用;向用户回答时只能使用中文业务名称,'
  26. '不得展示内部参数名。'
  27. ),
  28. 'input_schema': {
  29. 'type': 'object',
  30. 'properties': {
  31. 'outbound_numbers': {
  32. 'type': 'array',
  33. 'items': dict(number_items),
  34. 'minItems': 1,
  35. 'maxItems': 100,
  36. 'uniqueItems': True,
  37. 'description': (
  38. '要导出的排舱单号数组。排舱单号是后台排舱单列表中的'
  39. '业务单号,不是系统订单号、客户参考号、快递单号、柜号、'
  40. 'SO号或Shipment ID。只能使用用户明确提供并确认类型为'
  41. '排舱单号的值,不得根据号码格式猜测。'
  42. ),
  43. 'examples': [[
  44. 'PC202607140001',
  45. 'PC202607140002',
  46. ]],
  47. },
  48. 'container_codes': {
  49. 'type': 'array',
  50. 'items': dict(number_items),
  51. 'minItems': 1,
  52. 'maxItems': 100,
  53. 'uniqueItems': True,
  54. 'description': (
  55. '要导出的柜号或集装箱号数组。仅当用户明确说明号码类型'
  56. '为“柜号”或“集装箱号”时使用,不得放入排舱单号或提单号。'
  57. ),
  58. 'examples': [[
  59. 'MSCU1234567',
  60. 'TGHU7654321',
  61. ]],
  62. },
  63. 'bl_numbers': {
  64. 'type': 'array',
  65. 'items': dict(number_items),
  66. 'minItems': 1,
  67. 'maxItems': 100,
  68. 'uniqueItems': True,
  69. 'description': (
  70. '要导出的提单号数组,对应后台排舱单列表中的“提单号”。'
  71. '不是系统提单号;仅当用户明确说明号码类型为“提单号”时使用。'
  72. ),
  73. 'examples': [[
  74. 'BL202607150001',
  75. 'BL202607150002',
  76. ]],
  77. },
  78. 'so_numbers': {
  79. 'type': 'array',
  80. 'items': dict(number_items),
  81. 'minItems': 1,
  82. 'maxItems': 100,
  83. 'uniqueItems': True,
  84. 'description': (
  85. '要导出的SO号数组,对应后台排舱单列表中的“SO号”,'
  86. '数据字段为fms_booking_detail.so_number。仅当用户'
  87. '明确说明号码类型为“SO号”时使用。'
  88. ),
  89. 'examples': [[
  90. 'SO202607160001',
  91. 'SO202607160002',
  92. ]],
  93. },
  94. 'file_type': {
  95. 'type': 'string',
  96. 'enum': ['NB', 'SH', 'MS'],
  97. 'description': (
  98. '进港资料类型:NB=宁波进港资料,SH=上海进港资料,'
  99. 'MS=美森进港资料。必须根据用户明确选择传值;'
  100. '用户未说明时先询问,不得默认选择。'
  101. ),
  102. 'examples': ['NB'],
  103. },
  104. },
  105. 'required': ['file_type'],
  106. 'oneOf': [
  107. {'required': ['outbound_numbers']},
  108. {'required': ['container_codes']},
  109. {'required': ['bl_numbers']},
  110. {'required': ['so_numbers']},
  111. ],
  112. 'additionalProperties': False,
  113. },
  114. }
  115. def call(
  116. self,
  117. outbound_numbers=None,
  118. file_type=None,
  119. request_id='rq_export_out_of_province_port_data',
  120. container_codes=None,
  121. bl_numbers=None,
  122. so_numbers=None,
  123. ):
  124. if self.api_client is None:
  125. raise RuntimeError(
  126. 'api client is required for export_out_of_province_port_data'
  127. )
  128. number_fields = {
  129. 'outbound_numbers': outbound_numbers,
  130. 'container_codes': container_codes,
  131. 'bl_numbers': bl_numbers,
  132. 'so_numbers': so_numbers,
  133. }
  134. provided = [
  135. field for field, values in number_fields.items()
  136. if values is not None
  137. ]
  138. if len(provided) != 1:
  139. raise ValueError('provide exactly one number type')
  140. number_field = provided[0]
  141. numbers = self._normalize_numbers(number_fields[number_field], number_field)
  142. normalized_type = str(file_type or '').strip().upper()
  143. if normalized_type not in ('NB', 'SH', 'MS'):
  144. raise ValueError('file_type must be NB, SH, or MS')
  145. return self.api_client.call_tool(
  146. self.name,
  147. self.route_path,
  148. {
  149. number_field: numbers,
  150. 'file_type': normalized_type,
  151. },
  152. request_id,
  153. )
  154. @staticmethod
  155. def _normalize_numbers(values, field):
  156. if not isinstance(values, list):
  157. raise ValueError('{0} must be an array'.format(field))
  158. if len(values) > 100:
  159. raise ValueError('{0} must contain 1 to 100 values'.format(field))
  160. numbers = []
  161. for value in values:
  162. if not isinstance(value, str):
  163. raise ValueError('{0} must contain strings'.format(field))
  164. value = value.strip()
  165. if not value:
  166. continue
  167. if len(value) > 100:
  168. raise ValueError('{0} contains a number that is too long'.format(field))
  169. if value not in numbers:
  170. numbers.append(value)
  171. if not numbers or len(numbers) > 100:
  172. raise ValueError('{0} must contain 1 to 100 values'.format(field))
  173. return numbers