query_headhaul_document_list.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from tools.headhaul_document_common import (
  2. NUMBER_FIELDS,
  3. bounded_integer,
  4. number_list,
  5. number_properties,
  6. )
  7. class QueryHeadhaulDocumentListTool:
  8. name = 'query_headhaul_document_list'
  9. route_path = '/mcp/tools/queryHeadhaulDocumentList'
  10. def __init__(self, api_client=None):
  11. self.api_client = api_client
  12. def metadata(self):
  13. properties = {
  14. 'document_type_id': {
  15. 'type': 'integer',
  16. 'minimum': 1,
  17. 'description': (
  18. '文档类型。必须先调用list_headhaul_document_filter_options'
  19. '并选择“查询文档类型”取得可传值。显示名含文件维度。'
  20. '不止一条对得上类型名称时,先请用户按完整显示名选择。'
  21. ),
  22. },
  23. 'original_name': {
  24. 'type': 'string',
  25. 'minLength': 1,
  26. 'maxLength': 255,
  27. 'description': '完整原始文件名,精确匹配,不是模糊搜索。',
  28. },
  29. }
  30. properties.update(number_properties())
  31. properties['page'] = {
  32. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 1,
  33. }
  34. properties['limit'] = {
  35. 'type': 'integer', 'minimum': 1, 'maximum': 100, 'default': 20,
  36. }
  37. return {
  38. 'name': self.name,
  39. 'description': (
  40. '使用场景:用户要看头程文档列表时,必须先有文档类型、完整文件名'
  41. '或已经标明种类的录入单号之一,再查询当前页。对照后台'
  42. 'admin/document/index,一行一份文件或外链。号码类型不明确时必须先询问用户,'
  43. '按文档类型查询时,显示名是「文档类型名称 - 文件维度」。'
  44. '用户只说了类型名称且不止一条对得上时,先列出完整显示名请用户选择,选定前不要查询。'
  45. '只有一条对得上时使用该条 value。'
  46. '不得根据号码格式猜测,不得跨字段或跨工具试查,也不得改走'
  47. 'query_customs_declaration_files或订单附件。'
  48. '禁止使用:空扫全表、混查单号框、行内编辑、把文件放进工具参数。'
  49. '参数只用于工具内部调用;最终回答只能展示中文业务名称;'
  50. '描述筛选条件时不得展示筛选字段的英文参数名。'
  51. ),
  52. 'input_schema': {
  53. 'type': 'object',
  54. 'properties': properties,
  55. 'additionalProperties': False,
  56. },
  57. }
  58. def call(
  59. self,
  60. document_type_id=None,
  61. original_name=None,
  62. page=1,
  63. limit=20,
  64. request_id='rq_query_headhaul_document_list',
  65. **numbers
  66. ):
  67. if self.api_client is None:
  68. raise RuntimeError(
  69. 'api client is required for query_headhaul_document_list'
  70. )
  71. payload = {}
  72. if document_type_id is not None:
  73. if isinstance(document_type_id, bool) or not isinstance(
  74. document_type_id, int
  75. ) or document_type_id < 1:
  76. raise ValueError('document_type_id is invalid')
  77. payload['document_type_id'] = document_type_id
  78. if original_name is not None:
  79. name = str(original_name).strip()
  80. if not name or len(name) > 255:
  81. raise ValueError('original_name is invalid')
  82. payload['original_name'] = name
  83. for field in NUMBER_FIELDS:
  84. if field in numbers and numbers[field] is not None:
  85. payload[field] = number_list(numbers[field], field)
  86. if (
  87. 'document_type_id' not in payload
  88. and 'original_name' not in payload
  89. and not any(field in payload for field in NUMBER_FIELDS)
  90. ):
  91. raise ValueError('locator is required')
  92. payload['page'] = bounded_integer(page, 'page')
  93. payload['limit'] = bounded_integer(limit, 'limit')
  94. return self.api_client.call_tool(
  95. self.name, self.route_path, payload, request_id,
  96. )