coze_lab 0.1.11 → 0.1.13
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 +15 -7
- package/package.json +1 -1
- package/scripts/claude-code/cozeloop_hook.py +8 -6
package/index.js
CHANGED
|
@@ -4622,7 +4622,7 @@ function writeCodexHook(token, workspaceId, pythonCmd, codexHome, cloud) {
|
|
|
4622
4622
|
// agentId 非空时并入 plugins.entries[...].config.traceAgentIds allowlist —— 插件运行时
|
|
4623
4623
|
// 用 resolveAgentIdFromHookCtx 取当前 agentId,仅 allowlist 内的 agent 才上报 trace。
|
|
4624
4624
|
// allowlist 为空(本地全局模式)= 全部放行,向后兼容。
|
|
4625
|
-
function writeOpenClawHook(token, workspaceId, agentId) {
|
|
4625
|
+
function writeOpenClawHook(token, workspaceId, agentId, cloud) {
|
|
4626
4626
|
const configPath = path.join(os.homedir(), '.openclaw', 'openclaw.json');
|
|
4627
4627
|
const pluginDir = path.join(os.homedir(), '.cozeloop', 'openclaw-plugin');
|
|
4628
4628
|
|
|
@@ -4703,7 +4703,12 @@ function writeOpenClawHook(token, workspaceId, agentId) {
|
|
|
4703
4703
|
if (!existing.plugins.entries[PLUGIN].config) existing.plugins.entries[PLUGIN].config = {};
|
|
4704
4704
|
const pcfg = existing.plugins.entries[PLUGIN].config;
|
|
4705
4705
|
pcfg.authorization = `Bearer ${token}`;
|
|
4706
|
-
|
|
4706
|
+
// 云端:endpoint 走 sandbox 注入的 COZELOOP_API_BASE_URL 代理(token 经它鉴权);
|
|
4707
|
+
// 缺省回退 api.coze.cn 直连。插件 exporter 会在此基础上拼 /v1/traces。
|
|
4708
|
+
const ocBase = (cloud && process.env.COZELOOP_API_BASE_URL)
|
|
4709
|
+
? process.env.COZELOOP_API_BASE_URL.replace(/\/+$/, '') + '/v1/loop/opentelemetry'
|
|
4710
|
+
: 'https://api.coze.cn/v1/loop/opentelemetry';
|
|
4711
|
+
pcfg.endpoint = ocBase;
|
|
4707
4712
|
pcfg.workspaceId = workspaceId;
|
|
4708
4713
|
pcfg.debug = true;
|
|
4709
4714
|
// per-agent trace 放行:把当前 agentId 并入 traceAgentIds(去重、归一为小写,
|
|
@@ -4772,7 +4777,7 @@ function httpsPost(url, body, extraHeaders) {
|
|
|
4772
4777
|
// 只看 HTTP 状态码(2xx=通),不回查 trace 是否落库——回查由外部查询方完成。
|
|
4773
4778
|
// pairCode 写进 span 的 pair_code attribute,供查询方按该字段过滤回查;缺省自动生成。
|
|
4774
4779
|
// 不在函数内退出,退出行为交给调用方(主流程 Step 6 / 独立命令 --verify)。
|
|
4775
|
-
async function verifyTraceReport(token, workspaceId, pairCode) {
|
|
4780
|
+
async function verifyTraceReport(token, workspaceId, pairCode, baseUrl) {
|
|
4776
4781
|
const traceId = crypto.randomBytes(16).toString('hex'); // 32 hex chars
|
|
4777
4782
|
const spanId = crypto.randomBytes(8).toString('hex'); // 16 hex chars
|
|
4778
4783
|
const nowNs = String(Date.now() * 1_000_000); // OTLP 要求纳秒 unix 时间(字符串)
|
|
@@ -4805,9 +4810,12 @@ async function verifyTraceReport(token, workspaceId, pairCode) {
|
|
|
4805
4810
|
};
|
|
4806
4811
|
|
|
4807
4812
|
let res;
|
|
4813
|
+
// 云端:上报走 sandbox 注入的 COZELOOP_API_BASE_URL 代理(vefaas token 经它鉴权转换);
|
|
4814
|
+
// 缺省回退到 api.coze.cn 直连。base 已去尾部斜杠。
|
|
4815
|
+
const apiBase = (baseUrl || COZE_API).replace(/\/+$/, '');
|
|
4808
4816
|
try {
|
|
4809
4817
|
res = await httpsPost(
|
|
4810
|
-
`${
|
|
4818
|
+
`${apiBase}/v1/loop/opentelemetry/v1/traces`,
|
|
4811
4819
|
otlpBody,
|
|
4812
4820
|
{ Authorization: `Bearer ${token}`, 'cozeloop-workspace-id': workspaceId },
|
|
4813
4821
|
);
|
|
@@ -5158,7 +5166,7 @@ async function main() {
|
|
|
5158
5166
|
info('验证 trace 上报链路...');
|
|
5159
5167
|
const token = await getValidToken(); // 无凭证会自动走登录/刷新
|
|
5160
5168
|
console.log('');
|
|
5161
|
-
const result = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode);
|
|
5169
|
+
const result = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, process.env.COZELOOP_API_BASE_URL || undefined);
|
|
5162
5170
|
process.exit(result.success ? 0 : 1);
|
|
5163
5171
|
}
|
|
5164
5172
|
|
|
@@ -5239,7 +5247,7 @@ async function main() {
|
|
|
5239
5247
|
written = writeCodexHook(token, WORKSPACE_ID, pythonCmd, codexHome, args.cloud);
|
|
5240
5248
|
} else {
|
|
5241
5249
|
// openclaw:云端用 traceAgentIds allowlist 做 per-agent 放行。
|
|
5242
|
-
written = writeOpenClawHook(token, WORKSPACE_ID, args.agentId) || {};
|
|
5250
|
+
written = writeOpenClawHook(token, WORKSPACE_ID, args.agentId, args.cloud) || {};
|
|
5243
5251
|
}
|
|
5244
5252
|
// 走到这里说明 detectAgent / 环境检查 / 写 hook 配置全部成功 → 注入成功。
|
|
5245
5253
|
cloudResult.inject = 'ok';
|
|
@@ -5276,7 +5284,7 @@ async function main() {
|
|
|
5276
5284
|
|
|
5277
5285
|
// Step 5: Verify trace reporting end-to-end
|
|
5278
5286
|
info('Step 5/5: 验证 trace 上报链路...');
|
|
5279
|
-
const verifyResult = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode);
|
|
5287
|
+
const verifyResult = await verifyTraceReport(token, WORKSPACE_ID, args.pairCode, args.cloud ? process.env.COZELOOP_API_BASE_URL : undefined);
|
|
5280
5288
|
if (verifyResult.success) {
|
|
5281
5289
|
cloudResult.verify = 'ok';
|
|
5282
5290
|
} else if (CLOUD_MODE) {
|
package/package.json
CHANGED
|
@@ -1385,14 +1385,16 @@ def main():
|
|
|
1385
1385
|
# Group messages into turns and send to CozeLoop — only if coze-context present.
|
|
1386
1386
|
turns = group_messages_into_turns(new_messages)
|
|
1387
1387
|
if turns:
|
|
1388
|
-
|
|
1389
|
-
|
|
1388
|
+
# coze-context 只出现在首条用户消息里(cozelab 注入的 agent 才有)。增量 hook 触发时,
|
|
1389
|
+
# 新消息往往全是工具调用/结果(不带 coze-context),故必须连同历史 turns 一起判断——
|
|
1390
|
+
# 只要整个会话出现过 coze-context,就说明是被注入的 agent,后续增量都应上报。
|
|
1391
|
+
def _turn_has_ctx(turn):
|
|
1392
|
+
return bool(coze_context_tags(
|
|
1390
1393
|
(turn.get("user_message", {}).get("message", {}) or {}).get("content")
|
|
1391
|
-
)
|
|
1392
|
-
|
|
1393
|
-
)
|
|
1394
|
+
))
|
|
1395
|
+
has_coze_ctx = any(_turn_has_ctx(t) for t in turns) or any(_turn_has_ctx(t) for t in history_turns)
|
|
1394
1396
|
if not has_coze_ctx:
|
|
1395
|
-
debug_log("No coze-context found in any turn, skipping upload.")
|
|
1397
|
+
debug_log("No coze-context found in any turn (incl. history), skipping upload.")
|
|
1396
1398
|
return
|
|
1397
1399
|
print(f"[CozeLoop] 开始上报: session={session_id}, turns={len(turns)}", file=sys.stderr)
|
|
1398
1400
|
send_turns_to_cozeloop(turns, session_id, history_turns)
|