coze-coding-utils 0.1.2__py3-none-any.whl → 0.1.5__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.
@@ -1,11 +1,23 @@
1
1
  import os
2
2
  import uuid
3
3
  from dataclasses import dataclass
4
- from typing import Optional, Dict
4
+ from typing import Optional, Dict, Mapping
5
5
 
6
+ # Header keys
7
+ HEADER_X_TT_LOGID = "x-tt-logid"
8
+ HEADER_X_TT_ENV = "x-tt-env"
9
+ HEADER_X_USE_PPE = "x-use-ppe"
10
+ HEADER_X_TT_ENV_FE = "x-tt-env-fe"
11
+ HEADER_BENEFIT_BIZ_SCENE = "benefit-biz-scene"
6
12
 
7
- @dataclass
13
+ # Environment variable keys
14
+ ENV_SPACE_ID = "COZE_PROJECT_SPACE_ID"
15
+ ENV_PROJECT_ID = "COZE_PROJECT_ID"
16
+
17
+
18
+ @dataclass(slots=True)
8
19
  class Context:
20
+ """运行时上下文,封装请求关联的标识与环境信息。"""
9
21
  run_id: str
10
22
  space_id: str
11
23
  project_id: str
@@ -17,18 +29,47 @@ class Context:
17
29
  benefit_biz_scene: Optional[str] = None
18
30
 
19
31
 
20
- def new_context(method: str, headers: Optional[Dict[str, str]] = None) -> Context:
32
+ def new_context(method: str, headers: Optional[Mapping[str, str]] = None) -> Context:
33
+ """创建上下文对象,读取必要环境变量并可从请求头补充可选字段。"""
21
34
  ctx = Context(
22
35
  run_id=str(uuid.uuid4()),
23
- space_id=os.getenv("COZE_PROJECT_SPACE_ID", ""),
24
- project_id=os.getenv("COZE_PROJECT_ID", ""),
36
+ space_id=os.getenv(ENV_SPACE_ID, ""),
37
+ project_id=os.getenv(ENV_PROJECT_ID, ""),
25
38
  method=method,
26
39
  )
27
40
  if headers:
28
- ctx.logid = headers.get("x-tt-logid", "")
29
- ctx.x_tt_env = headers.get("x-tt-env")
30
- ctx.x_use_ppe = headers.get("x-use-ppe")
31
- ctx.x_tt_env_fe = headers.get("x-tt-env-fe")
32
- ctx.benefit_biz_scene = headers.get("benefit-biz-scene")
41
+ if HEADER_X_TT_LOGID in headers:
42
+ ctx.logid = headers[HEADER_X_TT_LOGID]
43
+ if HEADER_X_TT_ENV in headers:
44
+ ctx.x_tt_env = headers[HEADER_X_TT_ENV]
45
+ if HEADER_X_USE_PPE in headers:
46
+ ctx.x_use_ppe = headers[HEADER_X_USE_PPE]
47
+ if HEADER_X_TT_ENV_FE in headers:
48
+ ctx.x_tt_env_fe = headers[HEADER_X_TT_ENV_FE]
49
+ if HEADER_BENEFIT_BIZ_SCENE in headers:
50
+ ctx.benefit_biz_scene = headers[HEADER_BENEFIT_BIZ_SCENE]
33
51
  return ctx
34
52
 
53
+
54
+ def default_headers(ctx: Context | None) -> Dict[str, str]:
55
+ """从上下文生成请求头字典,仅包含已设置的字段。"""
56
+ if not ctx:
57
+ return {}
58
+ headers: Dict[str, str] = {}
59
+ if ctx.logid:
60
+ headers[HEADER_X_TT_LOGID] = ctx.logid
61
+ if ctx.x_tt_env:
62
+ headers[HEADER_X_TT_ENV] = ctx.x_tt_env
63
+ if ctx.x_use_ppe:
64
+ headers[HEADER_X_USE_PPE] = ctx.x_use_ppe
65
+ if ctx.x_tt_env_fe:
66
+ headers[HEADER_X_TT_ENV_FE] = ctx.x_tt_env_fe
67
+ if ctx.benefit_biz_scene:
68
+ headers[HEADER_BENEFIT_BIZ_SCENE] = ctx.benefit_biz_scene
69
+ return headers
70
+
71
+ __all__ = [
72
+ "Context",
73
+ "new_context",
74
+ "default_headers",
75
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coze-coding-utils
3
- Version: 0.1.2
3
+ Version: 0.1.5
4
4
  Summary: Utilities for Coze coding client runtime context and helpers.
5
5
  Project-URL: Homepage, https://code.byted.org/stone/coze-coding-client
6
6
  Author: Bytedance Stone Team
@@ -0,0 +1,7 @@
1
+ coze_coding_utils/__init__.py,sha256=vJ5SRiIxcBwcGt0tBF9QingGIl8ICxQuTnAMisQs34A,36
2
+ coze_coding_utils/runtime_ctx/__init__.py,sha256=4W8VliAYUP1KY2gLJ_YDy2TmcXYVm-PY7XikQD_bFwA,2
3
+ coze_coding_utils/runtime_ctx/context.py,sha256=oU9LlYIhQsOl07vSxOWhOSJvh5_jQwIllP_oDERLEu8,2324
4
+ coze_coding_utils-0.1.5.dist-info/METADATA,sha256=l7jTX5A3KdMqDWH6c9SBRiehhvUrDPXLeyUtTKi4hBc,978
5
+ coze_coding_utils-0.1.5.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ coze_coding_utils-0.1.5.dist-info/licenses/LICENSE,sha256=lzckZhAjHlpSJcWvppoST095IHFpBwKiB2pKcBv7vP4,1078
7
+ coze_coding_utils-0.1.5.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- coze_coding_utils/__init__.py,sha256=vJ5SRiIxcBwcGt0tBF9QingGIl8ICxQuTnAMisQs34A,36
2
- coze_coding_utils/runtime_ctx/__init__.py,sha256=4W8VliAYUP1KY2gLJ_YDy2TmcXYVm-PY7XikQD_bFwA,2
3
- coze_coding_utils/runtime_ctx/context.py,sha256=gvQnDJ9YSuOLrSpw_a36IKbMeMC4yR3PAfkLSpr45fc,939
4
- coze_coding_utils-0.1.2.dist-info/METADATA,sha256=GvIBIaxO94Sf2FVuMqI6Z0y_WIUhK2bzxL254t6PqOI,978
5
- coze_coding_utils-0.1.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
- coze_coding_utils-0.1.2.dist-info/licenses/LICENSE,sha256=lzckZhAjHlpSJcWvppoST095IHFpBwKiB2pKcBv7vP4,1078
7
- coze_coding_utils-0.1.2.dist-info/RECORD,,