coze-coding-utils 0.1.7__tar.gz → 0.1.8__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coze-coding-utils
3
- Version: 0.1.7
3
+ Version: 0.1.8
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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "coze-coding-utils"
7
- version = "0.1.7"
7
+ version = "0.1.8"
8
8
  description = "Utilities for Coze coding client runtime context and helpers."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,3 @@
1
+ __all__ = []
2
+
3
+ __version__ = "0.1.8"
@@ -12,6 +12,7 @@ HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE = "rpc-persist-rec-rec-biz-scene"
12
12
  HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID = "rpc-persist-coze-record-root-id" # root_id,串联一次完整请求,通常标识一次完整对话
13
13
  HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE = "rpc-persist-rec-rec-root-entity-type" # 对应最顶层实体类型,用于标识资源消耗的来源归属实体
14
14
  HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID = "rpc-persist-rec-rec-root-entity-id" #对应最顶层实体ID
15
+ HEADER_RPC_PERSIST_REC_REC_EXT_INFO = "rpc-persist-rec-rec-ext-info" # 扩展信息,json字符串格式
15
16
 
16
17
  # Environment variable keys
17
18
  ENV_SPACE_ID = "COZE_PROJECT_SPACE_ID"
@@ -26,14 +27,16 @@ class Context:
26
27
  project_id: str
27
28
  logid: str = ""
28
29
  method: str = ""
30
+
29
31
  x_tt_env: Optional[str] = None
30
32
  x_use_ppe: Optional[str] = None
31
33
  x_tt_env_fe: Optional[str] = None
32
-
34
+
33
35
  rpc_persist_rec_rec_biz_scene: Optional[str] = None
34
36
  rpc_persist_coze_record_root_id: Optional[str] = None
35
37
  rpc_persist_rec_root_entity_type: Optional[str] = None
36
38
  rpc_persist_rec_root_entity_id: Optional[str] = None
39
+ rpc_persist_rec_rec_ext_info: Optional[str] = None
37
40
 
38
41
 
39
42
  def new_context(method: str, headers: Optional[Mapping[str, str]] = None) -> Context:
@@ -44,23 +47,24 @@ def new_context(method: str, headers: Optional[Mapping[str, str]] = None) -> Con
44
47
  project_id=os.getenv(ENV_PROJECT_ID, ""),
45
48
  method=method,
46
49
  )
50
+ # logid 与 method 保持一致,不读取头部覆盖
51
+ ctx.logid = method
47
52
  if headers:
48
- if HEADER_X_TT_LOGID in headers:
49
- ctx.logid = headers[HEADER_X_TT_LOGID]
50
- if HEADER_X_TT_ENV in headers:
51
- ctx.x_tt_env = headers[HEADER_X_TT_ENV]
52
- if HEADER_X_USE_PPE in headers:
53
- ctx.x_use_ppe = headers[HEADER_X_USE_PPE]
54
- if HEADER_X_TT_ENV_FE in headers:
55
- ctx.x_tt_env_fe = headers[HEADER_X_TT_ENV_FE]
56
- if HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE in headers:
57
- ctx.rpc_persist_rec_rec_biz_scene = headers[HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE]
58
- if HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID in headers:
59
- ctx.rpc_persist_coze_record_root_id = headers[HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID]
60
- if HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE in headers:
61
- ctx.rpc_persist_rec_root_entity_type = headers[HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE]
62
- if HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID in headers:
63
- ctx.rpc_persist_rec_root_entity_id = headers[HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID]
53
+ norm = {k.casefold(): v for k, v in headers.items()}
54
+ HEADERS_TO_ATTR = {
55
+ HEADER_X_TT_ENV: "x_tt_env",
56
+ HEADER_X_USE_PPE: "x_use_ppe",
57
+ HEADER_X_TT_ENV_FE: "x_tt_env_fe",
58
+ HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE: "rpc_persist_rec_rec_biz_scene",
59
+ HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID: "rpc_persist_coze_record_root_id",
60
+ HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE: "rpc_persist_rec_root_entity_type",
61
+ HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID: "rpc_persist_rec_root_entity_id",
62
+ HEADER_RPC_PERSIST_REC_REC_EXT_INFO: "rpc_persist_rec_rec_ext_info",
63
+ }
64
+ for hk, attr in HEADERS_TO_ATTR.items():
65
+ val = norm.get(hk)
66
+ if val:
67
+ setattr(ctx, attr, val)
64
68
  return ctx
65
69
 
66
70
 
@@ -69,22 +73,21 @@ def default_headers(ctx: Context | None) -> Dict[str, str]:
69
73
  if not ctx:
70
74
  return {}
71
75
  headers: Dict[str, str] = {}
72
- if ctx.logid:
73
- headers[HEADER_X_TT_LOGID] = ctx.logid
74
- if ctx.x_tt_env:
75
- headers[HEADER_X_TT_ENV] = ctx.x_tt_env
76
- if ctx.x_use_ppe:
77
- headers[HEADER_X_USE_PPE] = ctx.x_use_ppe
78
- if ctx.x_tt_env_fe:
79
- headers[HEADER_X_TT_ENV_FE] = ctx.x_tt_env_fe
80
- if ctx.rpc_persist_rec_rec_biz_scene:
81
- headers[HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE] = ctx.rpc_persist_rec_rec_biz_scene
82
- if ctx.rpc_persist_coze_record_root_id:
83
- headers[HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID] = ctx.rpc_persist_coze_record_root_id
84
- if ctx.rpc_persist_rec_root_entity_type:
85
- headers[HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE] = ctx.rpc_persist_rec_root_entity_type
86
- if ctx.rpc_persist_rec_root_entity_id:
87
- headers[HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID] = ctx.rpc_persist_rec_root_entity_id
76
+ ATTR_TO_HEADER = {
77
+ "logid": HEADER_X_TT_LOGID,
78
+ "x_tt_env": HEADER_X_TT_ENV,
79
+ "x_use_ppe": HEADER_X_USE_PPE,
80
+ "x_tt_env_fe": HEADER_X_TT_ENV_FE,
81
+ "rpc_persist_rec_rec_biz_scene": HEADER_RPC_PERSIST_REC_REC_BIZ_SCENE,
82
+ "rpc_persist_coze_record_root_id": HEADER_RPC_PERSIST_COZE_RECORD_ROOT_ID,
83
+ "rpc_persist_rec_root_entity_type": HEADER_RPC_PERSIST_REC_ROOT_ENTITY_TYPE,
84
+ "rpc_persist_rec_root_entity_id": HEADER_RPC_PERSIST_REC_ROOT_ENTITY_ID,
85
+ "rpc_persist_rec_rec_ext_info": HEADER_RPC_PERSIST_REC_REC_EXT_INFO,
86
+ }
87
+ for attr, hk in ATTR_TO_HEADER.items():
88
+ v = getattr(ctx, attr)
89
+ if v:
90
+ headers[hk] = v
88
91
  return headers
89
92
 
90
93
  __all__ = [
@@ -1,3 +0,0 @@
1
- __all__ = []
2
-
3
- __version__ = "0.1.7"