coze_lab 0.1.19 → 0.1.20

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.
package/index.js CHANGED
@@ -4669,15 +4669,38 @@ function normalizeTraceAgentIds(ids) {
4669
4669
  .filter(Boolean);
4670
4670
  }
4671
4671
 
4672
- function getOpenClawEndpoint(cloud) {
4673
- return (cloud && process.env.COZELOOP_API_BASE_URL)
4674
- ? process.env.COZELOOP_API_BASE_URL.replace(/\/+$/, '') + '/v1/loop/opentelemetry'
4675
- : 'https://api.coze.cn/v1/loop/opentelemetry';
4672
+ function getCloudCozeloopApiBaseUrl() {
4673
+ const raw = process.env.COZELOOP_API_BASE_URL || process.env.OTEL_ENDPOINT || '';
4674
+ const base = raw.trim().replace(/\/+$/, '');
4675
+ if (!base) return '';
4676
+ if (base.endsWith('/v1/loop/opentelemetry/v1/traces')) {
4677
+ return base.slice(0, -'/v1/loop/opentelemetry/v1/traces'.length).replace(/\/+$/, '');
4678
+ }
4679
+ if (base.endsWith('/api/v1/loop/opentelemetry/v1/traces')) {
4680
+ return base.slice(0, -'/v1/loop/opentelemetry/v1/traces'.length).replace(/\/+$/, '');
4681
+ }
4682
+ if (base.endsWith('/v1/loop/opentelemetry')) {
4683
+ return base.slice(0, -'/v1/loop/opentelemetry'.length).replace(/\/+$/, '');
4684
+ }
4685
+ if (base.endsWith('/api/v1/loop/opentelemetry')) {
4686
+ return base.slice(0, -'/loop/opentelemetry'.length).replace(/\/+$/, '');
4687
+ }
4688
+ if (base.endsWith('/api/v1')) {
4689
+ return base.slice(0, -'/v1'.length).replace(/\/+$/, '');
4690
+ }
4691
+ return base;
4692
+ }
4693
+
4694
+ function getCozeloopApiBaseUrl(cloud) {
4695
+ return cloud ? (getCloudCozeloopApiBaseUrl() || COZE_API) : COZE_API;
4696
+ }
4697
+
4698
+ function getOtelEndpointBase(cloud) {
4699
+ return `${getCozeloopApiBaseUrl(cloud).replace(/\/+$/, '')}/v1/loop/opentelemetry`;
4676
4700
  }
4677
4701
 
4678
- function getCloudCozeloopBaseUrl() {
4679
- const endpoint = process.env.COZELOOP_API_BASE_URL || process.env.OTEL_ENDPOINT || '';
4680
- return endpoint.replace(/\/v1\/loop\/opentelemetry\/?$/, '').replace(/\/+$/, '') || undefined;
4702
+ function getOtelTracesUrl(cloud) {
4703
+ return `${getOtelEndpointBase(cloud).replace(/\/+$/, '')}/v1/traces`;
4681
4704
  }
4682
4705
 
4683
4706
  function applyOpenClawPluginConfig(existing, token, workspaceId, agentId, cloud) {
@@ -4699,7 +4722,7 @@ function applyOpenClawPluginConfig(existing, token, workspaceId, agentId, cloud)
4699
4722
  if (!existing.plugins.entries[PLUGIN].config) existing.plugins.entries[PLUGIN].config = {};
4700
4723
  const pcfg = existing.plugins.entries[PLUGIN].config;
4701
4724
  pcfg.authorization = `Bearer ${token}`;
4702
- pcfg.endpoint = getOpenClawEndpoint(cloud);
4725
+ pcfg.endpoint = getOtelEndpointBase(cloud);
4703
4726
  pcfg.workspaceId = workspaceId;
4704
4727
  pcfg.debug = true;
4705
4728
  // per-agent trace 放行:把当前 agentId 并入 traceAgentIds(去重、归一为小写,
@@ -4855,7 +4878,7 @@ function httpsPost(url, body, extraHeaders) {
4855
4878
  // 只看 HTTP 状态码(2xx=通),不回查 trace 是否落库——回查由外部查询方完成。
4856
4879
  // pairCode 写进 span 的 pair_code attribute,供查询方按该字段过滤回查;缺省自动生成。
4857
4880
  // 不在函数内退出,退出行为交给调用方(主流程 Step 6 / 独立命令 --verify)。
4858
- async function verifyTraceReport(token, workspaceId, pairCode, baseUrl) {
4881
+ async function verifyTraceReport(token, workspaceId, pairCode, tracesUrl) {
4859
4882
  const traceId = crypto.randomBytes(16).toString('hex'); // 32 hex chars
4860
4883
  const spanId = crypto.randomBytes(8).toString('hex'); // 16 hex chars
4861
4884
  const nowNs = String(Date.now() * 1_000_000); // OTLP 要求纳秒 unix 时间(字符串)
@@ -4888,12 +4911,10 @@ async function verifyTraceReport(token, workspaceId, pairCode, baseUrl) {
4888
4911
  };
4889
4912
 
4890
4913
  let res;
4891
- // 云端:上报走 sandbox 注入的 COZELOOP_API_BASE_URL 代理(vefaas token 经它鉴权转换);
4892
- // 缺省回退到 api.coze.cn 直连。base 已去尾部斜杠。
4893
- const apiBase = (baseUrl || COZE_API).replace(/\/+$/, '');
4914
+ const url = tracesUrl || getOtelTracesUrl(false);
4894
4915
  try {
4895
4916
  res = await httpsPost(
4896
- `${apiBase}/v1/loop/opentelemetry/v1/traces`,
4917
+ url,
4897
4918
  otlpBody,
4898
4919
  { Authorization: `Bearer ${token}`, 'cozeloop-workspace-id': workspaceId },
4899
4920
  );
@@ -5244,7 +5265,7 @@ async function main() {
5244
5265
  info('验证 trace 上报链路...');
5245
5266
  const token = await getValidToken(); // 无凭证会自动走登录/刷新
5246
5267
  console.log('');
5247
- const result = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, getCloudCozeloopBaseUrl());
5268
+ const result = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, getOtelTracesUrl(false));
5248
5269
  process.exit(result.success ? 0 : 1);
5249
5270
  }
5250
5271
 
@@ -5366,7 +5387,7 @@ async function main() {
5366
5387
 
5367
5388
  // Step 5: Verify trace reporting end-to-end
5368
5389
  info('Step 5/5: 验证 trace 上报链路...');
5369
- const verifyResult = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, args.cloud ? getCloudCozeloopBaseUrl() : undefined);
5390
+ const verifyResult = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, getOtelTracesUrl(args.cloud));
5370
5391
  if (verifyResult.success) {
5371
5392
  cloudResult.verify = 'ok';
5372
5393
  } else if (CLOUD_MODE) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coze_lab",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "description": "Configure local AI agents (Claude Code, Codex, OpenClaw) to report traces to CozeLoop",
5
5
  "keywords": [
6
6
  "cozeloop",
@@ -210,8 +210,16 @@ def _refresh_token(refresh_tok: str):
210
210
 
211
211
  def _normalize_api_base_url(url: str) -> str:
212
212
  base = (url or "").strip().rstrip("/")
213
+ if base.endswith(_OTEL_SUFFIX + "/v1/traces"):
214
+ return base[:-len(_OTEL_SUFFIX + "/v1/traces")].rstrip("/")
215
+ if base.endswith("/api/v1/loop/opentelemetry/v1/traces"):
216
+ return base[:-len("/v1/loop/opentelemetry/v1/traces")].rstrip("/")
213
217
  if base.endswith(_OTEL_SUFFIX):
214
218
  return base[:-len(_OTEL_SUFFIX)].rstrip("/")
219
+ if base.endswith("/api/v1/loop/opentelemetry"):
220
+ return base[:-len("/v1/loop/opentelemetry")].rstrip("/")
221
+ if base.endswith("/api/v1"):
222
+ return base[:-len("/v1")].rstrip("/")
215
223
  return base
216
224
 
217
225