fiuai-sdk-python 0.6.1__py3-none-any.whl → 0.6.4__py3-none-any.whl

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.
@@ -0,0 +1,38 @@
1
+ # -- coding: utf-8 --
2
+ # Project: fiuai_sdk_python
3
+ # Created Date: 2026 01 Su
4
+ # Author: liming
5
+ # Email: lmlala@aliyun.com
6
+ # Copyright (c) 2026 FiuAI
7
+
8
+
9
+ """各类枚举值"""
10
+
11
+ from pydantic import BaseModel, Field
12
+ from enum import Enum, StrEnum
13
+
14
+
15
+ class CompanyNetworkConnectionType(StrEnum):
16
+ """公司网络连接类型"""
17
+ Customer = "Customer"
18
+ Supplier = "Supplier"
19
+
20
+
21
+ class CompanySize(StrEnum):
22
+ """公司规模"""
23
+ One = "1"
24
+ TwoToFifteen = "2-15"
25
+ SixteenToFortyNine = "16-49"
26
+ FiftyToNinetyNine = "50-99"
27
+ MoreThanOneHundred = "100-249"
28
+ MoreThanTwoHundredFifty = "250-499"
29
+ MoreThanFiveHundred = "500-999"
30
+ MoreThanOneThousand = "1000+"
31
+
32
+
33
+
34
+ class CompanyEntityType(StrEnum):
35
+ """公司实体类型"""
36
+ Enterprise = "Enterprise"
37
+ Individual = "Individual"
38
+ Other = "Other"
fiuai_sdk_python/const.py CHANGED
@@ -12,4 +12,6 @@ V2_GET_LIST_URI = "/document"
12
12
  ASSISTANT_USER = "assistant@fiuai.com"
13
13
 
14
14
  DEFAULT_TENANT_ID = "8888888888888888888"
15
- PASSIVE_TENANT_ID = "7777777777777777777"
15
+ PASSIVE_TENANT_ID = "7777777777777777777"
16
+
17
+
@@ -0,0 +1,226 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Project: FiuAI
5
+ Created Date: 2024-03-21
6
+ Author: liming
7
+ Email: lmlala@aliyun.com
8
+ Copyright (c) 2025 FiuAI
9
+ """
10
+
11
+ import threading
12
+ import time
13
+ import os
14
+ import hashlib
15
+ from typing import Optional
16
+ from snowflake import SnowflakeGenerator
17
+
18
+
19
+ class SnowflakeIdGenerator:
20
+ """
21
+ 雪花算法ID生成器
22
+ 使用 snowflake 包实现,支持多进程环境
23
+ """
24
+ _instance: Optional['SnowflakeIdGenerator'] = None
25
+ _lock = threading.Lock()
26
+
27
+ def __new__(cls, *args, **kwargs):
28
+ if cls._instance is None:
29
+ with cls._lock:
30
+ if cls._instance is None:
31
+ cls._instance = super().__new__(cls)
32
+ return cls._instance
33
+
34
+ def __init__(self, instance: int = None):
35
+ """
36
+ 初始化雪花算法ID生成器
37
+
38
+ Args:
39
+ instance: 实例ID (0-1023),如果为None则自动生成
40
+ """
41
+ if hasattr(self, '_initialized'):
42
+ return
43
+
44
+ # 在初始化时获取一次进程信息并缓存
45
+ self._process_id = os.getpid()
46
+ self._start_time = self._get_process_start_time()
47
+ self._object_id = id(self)
48
+
49
+ # 自动生成实例ID,确保多进程环境下唯一性
50
+ if instance is None:
51
+ instance = self._generate_process_instance_id()
52
+ elif not 0 <= instance <= 1023:
53
+ raise ValueError("实例ID必须在0-1023之间")
54
+
55
+ self.generator = SnowflakeGenerator(
56
+ instance=instance
57
+ )
58
+ self._last_timestamp = 0
59
+ self._instance_id = instance
60
+ self._initialized = True
61
+
62
+ def _get_process_start_time(self) -> float:
63
+ """
64
+ 获取进程启动时间
65
+
66
+ Returns:
67
+ float: 进程启动时间
68
+ """
69
+ try:
70
+ # 在Linux/macOS上使用更精确的时间
71
+ if hasattr(os, 'times'):
72
+ return os.times().elapsed
73
+ else:
74
+ return time.time()
75
+ except:
76
+ return time.time()
77
+
78
+ def _generate_process_instance_id(self) -> int:
79
+ """
80
+ 基于进程信息生成唯一的实例ID
81
+
82
+ Returns:
83
+ int: 0-1023之间的唯一实例ID
84
+ """
85
+ # 使用缓存的进程信息,避免重复调用
86
+ process_info = f"{self._process_id}_{self._start_time}_{self._object_id}"
87
+ hash_obj = hashlib.md5(process_info.encode())
88
+ hash_int = int(hash_obj.hexdigest()[:8], 16)
89
+
90
+ # 确保在0-1023范围内
91
+ instance_id = hash_int % 1024
92
+
93
+ return instance_id
94
+
95
+ def next_id(self) -> int:
96
+ """
97
+ 生成下一个ID
98
+
99
+ Returns:
100
+ int: 64位唯一ID
101
+
102
+ Raises:
103
+ OverflowError: 当时间戳达到最大值时抛出
104
+ RuntimeError: 当检测到时间回退时抛出
105
+ """
106
+ try:
107
+ result = next(self.generator)
108
+ if result is None:
109
+ raise ValueError("Generator returned None")
110
+
111
+ # 检查时间回退(简单的时间戳检查)
112
+ current_timestamp = int(time.time() * 1000)
113
+ if current_timestamp < self._last_timestamp:
114
+ raise RuntimeError(f"检测到时间回退: 当前时间 {current_timestamp} < 上次时间 {self._last_timestamp}")
115
+
116
+ self._last_timestamp = current_timestamp
117
+ return result
118
+
119
+ except StopIteration:
120
+ # 如果序列号达到最大值,重新初始化生成器
121
+ with self._lock:
122
+ self.generator = SnowflakeGenerator(instance=self._instance_id)
123
+ result = next(self.generator)
124
+ if result is None:
125
+ raise ValueError("Generator returned None")
126
+ return result
127
+
128
+ @property
129
+ def instance_id(self) -> int:
130
+ """获取当前实例ID"""
131
+ return self._instance_id
132
+
133
+ @property
134
+ def process_id(self) -> int:
135
+ """获取当前进程ID"""
136
+ return self._process_id
137
+
138
+ @property
139
+ def start_time(self) -> float:
140
+ """获取进程启动时间"""
141
+ return self._start_time
142
+
143
+
144
+ # 全局单例实例和锁
145
+ _id_generator = None
146
+ _id_generator_lock = threading.Lock()
147
+
148
+
149
+ def gen_id() -> str:
150
+ """
151
+ 获取下一个唯一ID
152
+
153
+ Returns:
154
+ str: 64位唯一ID字符串
155
+
156
+ Raises:
157
+ RuntimeError: 当ID生成失败时抛出
158
+ """
159
+ global _id_generator
160
+
161
+ # 双重检查锁定模式,确保线程安全
162
+ if _id_generator is None:
163
+ with _id_generator_lock:
164
+ if _id_generator is None:
165
+ _id_generator = SnowflakeIdGenerator()
166
+
167
+ try:
168
+ return str(_id_generator.next_id())
169
+ except Exception as e:
170
+ # 记录错误并重新抛出
171
+ raise RuntimeError(f"ID生成失败: {str(e)}") from e
172
+
173
+
174
+ def get_instance_info() -> dict:
175
+ """
176
+ 获取当前ID生成器的实例信息
177
+
178
+ Returns:
179
+ dict: 包含进程ID和实例ID的信息
180
+ """
181
+ global _id_generator
182
+
183
+ if _id_generator is None:
184
+ return {"process_id": os.getpid(), "instance_id": None, "status": "not_initialized"}
185
+
186
+ return {
187
+ "process_id": _id_generator.process_id,
188
+ "instance_id": _id_generator.instance_id,
189
+ "start_time": _id_generator.start_time,
190
+ "status": "initialized"
191
+ }
192
+
193
+
194
+ def reset_id_generator():
195
+ """
196
+ 重置ID生成器(主要用于测试或异常恢复)
197
+ """
198
+ global _id_generator
199
+ with _id_generator_lock:
200
+ _id_generator = None
201
+
202
+
203
+ def set_custom_instance_id(instance_id: int):
204
+ """
205
+ 设置自定义实例ID(谨慎使用,确保多进程环境下唯一性)
206
+
207
+ Args:
208
+ instance_id: 自定义实例ID (0-1023)
209
+
210
+ Raises:
211
+ ValueError: 实例ID超出范围
212
+ RuntimeError: 生成器已初始化
213
+ """
214
+ global _id_generator
215
+
216
+ if not 0 <= instance_id <= 1023:
217
+ raise ValueError("实例ID必须在0-1023之间")
218
+
219
+ if _id_generator is not None:
220
+ raise RuntimeError("生成器已初始化,无法修改实例ID")
221
+
222
+ with _id_generator_lock:
223
+ if _id_generator is None:
224
+ _id_generator = SnowflakeIdGenerator(instance=instance_id)
225
+ else:
226
+ raise RuntimeError("生成器已初始化,无法修改实例ID")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fiuai_sdk_python
3
- Version: 0.6.1
3
+ Version: 0.6.4
4
4
  Summary: FiuAI Python SDK - 企业级AI服务集成开发工具包
5
5
  Project-URL: Homepage, https://github.com/fiuai/fiuai-sdk-python
6
6
  Project-URL: Documentation, https://github.com/fiuai/fiuai-sdk-python#readme
@@ -28,6 +28,7 @@ Requires-Dist: langgraph-checkpoint-redis>=0.3.1
28
28
  Requires-Dist: pydantic>=2.11.7
29
29
  Requires-Dist: pytest>=8.4.1
30
30
  Requires-Dist: redis>=7.1.0
31
+ Requires-Dist: snowflake-id>=1.0.2
31
32
  Requires-Dist: tenacity>=9.1.2
32
33
  Description-Content-Type: text/markdown
33
34
 
@@ -2,7 +2,8 @@ fiuai_sdk_python/__init__.py,sha256=TBQNujrI8Y7Weuvw3zih22CipKUd0nyfeVGOGySA5zA,
2
2
  fiuai_sdk_python/bank.py,sha256=GRAmkI33rRZw9UE67SpkYsqPN-kdLm-iuJuuBLxSvrE,5438
3
3
  fiuai_sdk_python/client.py,sha256=rDc0l4nhcv6sT_T_5lbcfkKbk4GT0WibStUH7C0YVtA,18917
4
4
  fiuai_sdk_python/company.py,sha256=ZELOAxCTIA-F9cUWakvq7hv6SflM19Rq3XMDkV0mtbg,4771
5
- fiuai_sdk_python/const.py,sha256=S1sXurb9yE1fS3mWYw0qYRJDwJ4UlmqqWwpatt5lh-0,300
5
+ fiuai_sdk_python/config.py,sha256=vCD-aW2Y9OpJitfTDB3Acjw_PCurYT69vQjZ_wY950I,825
6
+ fiuai_sdk_python/const.py,sha256=zxYOMpdkTOd-Xwr-ImnhlFItpQNydFQUdJQICJNA9HU,303
6
7
  fiuai_sdk_python/context.py,sha256=Gu77o_tQN9eEh42RYC9KoJQcJxlmi4i8LH50IFfFH4I,9319
7
8
  fiuai_sdk_python/datatype.py,sha256=WQjsoJgpkkw0GtAQLzTrsem4joT8e3cnhcfy3dKN0bA,8919
8
9
  fiuai_sdk_python/doctype.py,sha256=5o6WsZDeKuCWfTiQa0H_BNh4jb96EpZt9WaDh8TgKZo,5013
@@ -29,8 +30,9 @@ fiuai_sdk_python/pkg/db/utils.py,sha256=qqZ14eV7fy9x_mwcEDKJDz1W54xPndpJ1QfM_D0E
29
30
  fiuai_sdk_python/pkg/vector/__init__.py,sha256=fvbetFxbHiFeojnTfKT3roA1hOcoNHF_eCRuG6HwMpI,404
30
31
  fiuai_sdk_python/pkg/vector/vector.py,sha256=gC8CQTZm56zXbhM3g_hFa7SGtAo9D1atNs3HwuEbJHQ,28994
31
32
  fiuai_sdk_python/utils/__init__.py,sha256=UwwsvqBsaRCHbWdx-wvM48szT3j50h95k9MZdbfawRQ,72
33
+ fiuai_sdk_python/utils/ids.py,sha256=ZDtEqt_Woth8ytPB2tdnnTIv7noWr8XYhSsUvkZ7Hc0,6448
32
34
  fiuai_sdk_python/utils/text.py,sha256=bnob_W0nj_Vj8Hp93B0cYmFOY8IhUWF0C8UedOYCNvs,1667
33
- fiuai_sdk_python-0.6.1.dist-info/METADATA,sha256=0FAbgudXGsl0ZG4ANaFLpGCEUcOZvPFev9O_o2xPbcE,1488
34
- fiuai_sdk_python-0.6.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
35
- fiuai_sdk_python-0.6.1.dist-info/licenses/LICENSE,sha256=PFMF0dFErrBFqU-rryEby0yW8GBagYqrdbyZQHMUCJg,1062
36
- fiuai_sdk_python-0.6.1.dist-info/RECORD,,
35
+ fiuai_sdk_python-0.6.4.dist-info/METADATA,sha256=ltSIEs8tgMsUwqCLYJAzSvQw5zgNwNMfLfqMHeDW1RI,1523
36
+ fiuai_sdk_python-0.6.4.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
37
+ fiuai_sdk_python-0.6.4.dist-info/licenses/LICENSE,sha256=PFMF0dFErrBFqU-rryEby0yW8GBagYqrdbyZQHMUCJg,1062
38
+ fiuai_sdk_python-0.6.4.dist-info/RECORD,,