opencode-tracekit 0.1.1 → 0.1.4
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/README.md +7 -7
- package/dist/src/opencode_plugin.d.ts +6 -61
- package/dist/src/opencode_plugin.js +9 -44
- package/dist/src/tools_trace_counter.js +1 -1
- package/dist/src/tools_trace_emit.js +1 -1
- package/dist/src/tools_trace_span.js +1 -1
- package/docs/installation.md +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
TraceKit 是一个面向 OpenCode 的可观测性插件,支持:
|
|
4
4
|
|
|
5
5
|
- 自动拦截 `tool/skill` 调用并记录 span(start/end)
|
|
6
|
-
- 在任意 agent 和 skill 中显式打点(`
|
|
7
|
-
- 记录自定义指标(`
|
|
6
|
+
- 在任意 agent 和 skill 中显式打点(`trace_emit`)
|
|
7
|
+
- 记录自定义指标(`trace_counter`)与业务阶段(`trace_span`)
|
|
8
8
|
- 将 trace 以 `ndjson` 落盘,并支持离线导出和分析
|
|
9
9
|
|
|
10
10
|
安装与接入步骤见:`plugins/tracekit/docs/installation.md`
|
|
@@ -61,7 +61,7 @@ opencode.registerPlugin({
|
|
|
61
61
|
|
|
62
62
|
```json
|
|
63
63
|
{
|
|
64
|
-
"tool": "
|
|
64
|
+
"tool": "trace_emit",
|
|
65
65
|
"args": {
|
|
66
66
|
"name": "agent.plan.generated",
|
|
67
67
|
"level": "info",
|
|
@@ -73,16 +73,16 @@ opencode.registerPlugin({
|
|
|
73
73
|
### 自定义 Span(业务阶段)
|
|
74
74
|
|
|
75
75
|
```json
|
|
76
|
-
{ "tool": "
|
|
77
|
-
{ "tool": "
|
|
78
|
-
{ "tool": "
|
|
76
|
+
{ "tool": "trace_span", "args": { "op": "start", "name": "ipd.phase.spec" } }
|
|
77
|
+
{ "tool": "trace_emit", "args": { "name": "spec.ready" } }
|
|
78
|
+
{ "tool": "trace_span", "args": { "op": "end", "status": "ok" } }
|
|
79
79
|
```
|
|
80
80
|
|
|
81
81
|
### Counter
|
|
82
82
|
|
|
83
83
|
```json
|
|
84
84
|
{
|
|
85
|
-
"tool": "
|
|
85
|
+
"tool": "trace_counter",
|
|
86
86
|
"args": {
|
|
87
87
|
"name": "spec.requirements.count",
|
|
88
88
|
"value": 23
|
|
@@ -2,37 +2,6 @@ import { type TraceKitConfig } from "./tracekit_plugin.js";
|
|
|
2
2
|
interface OpenCodePluginContext {
|
|
3
3
|
workingDirectory?: string;
|
|
4
4
|
}
|
|
5
|
-
interface OpenCodeToolBeforeInput {
|
|
6
|
-
sessionID?: string;
|
|
7
|
-
sessionId?: string;
|
|
8
|
-
tool?: string;
|
|
9
|
-
callID?: string;
|
|
10
|
-
callId?: string;
|
|
11
|
-
id?: string;
|
|
12
|
-
[key: string]: unknown;
|
|
13
|
-
}
|
|
14
|
-
interface OpenCodeToolBeforeOutput {
|
|
15
|
-
args?: Record<string, unknown>;
|
|
16
|
-
[key: string]: unknown;
|
|
17
|
-
}
|
|
18
|
-
interface OpenCodeToolAfterInput {
|
|
19
|
-
sessionID?: string;
|
|
20
|
-
sessionId?: string;
|
|
21
|
-
tool?: string;
|
|
22
|
-
callID?: string;
|
|
23
|
-
callId?: string;
|
|
24
|
-
id?: string;
|
|
25
|
-
[key: string]: unknown;
|
|
26
|
-
}
|
|
27
|
-
interface OpenCodeToolAfterOutput {
|
|
28
|
-
result?: unknown;
|
|
29
|
-
error?: unknown;
|
|
30
|
-
usage?: {
|
|
31
|
-
prompt_tokens?: number;
|
|
32
|
-
completion_tokens?: number;
|
|
33
|
-
};
|
|
34
|
-
[key: string]: unknown;
|
|
35
|
-
}
|
|
36
5
|
interface OpenCodeExecContext {
|
|
37
6
|
sessionID?: string;
|
|
38
7
|
sessionId?: string;
|
|
@@ -43,41 +12,17 @@ interface OpenCodeToolDefinition {
|
|
|
43
12
|
execute: (args: Record<string, unknown>, execCtx: OpenCodeExecContext) => Promise<Record<string, unknown>>;
|
|
44
13
|
}
|
|
45
14
|
export declare function TraceKitPlugin(ctx: OpenCodePluginContext): Promise<{
|
|
46
|
-
"session.created": (input: {
|
|
47
|
-
sessionID?: string;
|
|
48
|
-
sessionId?: string;
|
|
49
|
-
parentID?: string;
|
|
50
|
-
}) => Promise<void>;
|
|
51
|
-
"session.idle": (input: {
|
|
52
|
-
sessionID?: string;
|
|
53
|
-
sessionId?: string;
|
|
54
|
-
}) => Promise<void>;
|
|
55
|
-
"tool.execute.before": (input: OpenCodeToolBeforeInput, output: OpenCodeToolBeforeOutput) => Promise<void>;
|
|
56
|
-
"tool.execute.after": (input: OpenCodeToolAfterInput, output: OpenCodeToolAfterOutput) => Promise<void>;
|
|
57
|
-
"shutdown.before": () => Promise<void>;
|
|
58
15
|
tool: {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
16
|
+
trace_emit: OpenCodeToolDefinition;
|
|
17
|
+
trace_counter: OpenCodeToolDefinition;
|
|
18
|
+
trace_span: OpenCodeToolDefinition;
|
|
62
19
|
};
|
|
63
20
|
}>;
|
|
64
21
|
export declare function createOpenCodeTraceKit(config?: TraceKitConfig): (ctx: OpenCodePluginContext) => Promise<{
|
|
65
|
-
"session.created": (input: {
|
|
66
|
-
sessionID?: string;
|
|
67
|
-
sessionId?: string;
|
|
68
|
-
parentID?: string;
|
|
69
|
-
}) => Promise<void>;
|
|
70
|
-
"session.idle": (input: {
|
|
71
|
-
sessionID?: string;
|
|
72
|
-
sessionId?: string;
|
|
73
|
-
}) => Promise<void>;
|
|
74
|
-
"tool.execute.before": (input: OpenCodeToolBeforeInput, output: OpenCodeToolBeforeOutput) => Promise<void>;
|
|
75
|
-
"tool.execute.after": (input: OpenCodeToolAfterInput, output: OpenCodeToolAfterOutput) => Promise<void>;
|
|
76
|
-
"shutdown.before": () => Promise<void>;
|
|
77
22
|
tool: {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
23
|
+
trace_emit: OpenCodeToolDefinition;
|
|
24
|
+
trace_counter: OpenCodeToolDefinition;
|
|
25
|
+
trace_span: OpenCodeToolDefinition;
|
|
81
26
|
};
|
|
82
27
|
}>;
|
|
83
28
|
export {};
|
|
@@ -16,43 +16,8 @@ export function createOpenCodeTraceKit(config = {}) {
|
|
|
16
16
|
...config,
|
|
17
17
|
});
|
|
18
18
|
return {
|
|
19
|
-
"session.created": async (input) => {
|
|
20
|
-
core.hooks.onSessionStart({
|
|
21
|
-
sessionId: String(input.sessionID ?? input.sessionId ?? "unknown-session"),
|
|
22
|
-
parentSessionId: input.parentID ? String(input.parentID) : undefined,
|
|
23
|
-
});
|
|
24
|
-
},
|
|
25
|
-
"session.idle": async (input) => {
|
|
26
|
-
core.hooks.onSessionEnd({
|
|
27
|
-
sessionId: String(input.sessionID ?? input.sessionId ?? "unknown-session"),
|
|
28
|
-
});
|
|
29
|
-
},
|
|
30
|
-
"tool.execute.before": async (input, output) => {
|
|
31
|
-
core.hooks.onToolStart({
|
|
32
|
-
sessionId: String(input.sessionID ?? input.sessionId ?? "unknown-session"),
|
|
33
|
-
toolName: String(input.tool ?? "unknown-tool"),
|
|
34
|
-
toolCallId: String(input.callID ?? input.callId ?? input.id ?? `${Date.now()}`),
|
|
35
|
-
input: output.args ?? {},
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
"tool.execute.after": async (input, output) => {
|
|
39
|
-
core.hooks.onToolEnd({
|
|
40
|
-
sessionId: String(input.sessionID ?? input.sessionId ?? "unknown-session"),
|
|
41
|
-
toolName: String(input.tool ?? "unknown-tool"),
|
|
42
|
-
toolCallId: String(input.callID ?? input.callId ?? input.id ?? `${Date.now()}`),
|
|
43
|
-
output: output.result,
|
|
44
|
-
error: output.error,
|
|
45
|
-
usage: {
|
|
46
|
-
promptTokens: output.usage?.prompt_tokens,
|
|
47
|
-
completionTokens: output.usage?.completion_tokens,
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
"shutdown.before": async () => {
|
|
52
|
-
await core.shutdown();
|
|
53
|
-
},
|
|
54
19
|
tool: {
|
|
55
|
-
|
|
20
|
+
trace_emit: defineTool({
|
|
56
21
|
description: "Emit a tracepoint for current session",
|
|
57
22
|
parameters: {
|
|
58
23
|
type: "object",
|
|
@@ -66,13 +31,13 @@ export function createOpenCodeTraceKit(config = {}) {
|
|
|
66
31
|
},
|
|
67
32
|
execute: async (args, execCtx) => {
|
|
68
33
|
const runtime = toRuntime(execCtx);
|
|
69
|
-
const emit = core.tools.find((t) => t.name === "
|
|
34
|
+
const emit = core.tools.find((t) => t.name === "trace_emit");
|
|
70
35
|
if (!emit)
|
|
71
|
-
return { ok: false, reason: "
|
|
36
|
+
return { ok: false, reason: "trace_emit tool missing" };
|
|
72
37
|
return emit.handler(args, runtime);
|
|
73
38
|
},
|
|
74
39
|
}),
|
|
75
|
-
|
|
40
|
+
trace_counter: defineTool({
|
|
76
41
|
description: "Emit a counter metric for current session",
|
|
77
42
|
parameters: {
|
|
78
43
|
type: "object",
|
|
@@ -85,13 +50,13 @@ export function createOpenCodeTraceKit(config = {}) {
|
|
|
85
50
|
},
|
|
86
51
|
execute: async (args, execCtx) => {
|
|
87
52
|
const runtime = toRuntime(execCtx);
|
|
88
|
-
const counter = core.tools.find((t) => t.name === "
|
|
53
|
+
const counter = core.tools.find((t) => t.name === "trace_counter");
|
|
89
54
|
if (!counter)
|
|
90
|
-
return { ok: false, reason: "
|
|
55
|
+
return { ok: false, reason: "trace_counter tool missing" };
|
|
91
56
|
return counter.handler(args, runtime);
|
|
92
57
|
},
|
|
93
58
|
}),
|
|
94
|
-
|
|
59
|
+
trace_span: defineTool({
|
|
95
60
|
description: "Create a manual span for phase tracking",
|
|
96
61
|
parameters: {
|
|
97
62
|
type: "object",
|
|
@@ -107,9 +72,9 @@ export function createOpenCodeTraceKit(config = {}) {
|
|
|
107
72
|
},
|
|
108
73
|
execute: async (args, execCtx) => {
|
|
109
74
|
const runtime = toRuntime(execCtx);
|
|
110
|
-
const span = core.tools.find((t) => t.name === "
|
|
75
|
+
const span = core.tools.find((t) => t.name === "trace_span");
|
|
111
76
|
if (!span)
|
|
112
|
-
return { ok: false, reason: "
|
|
77
|
+
return { ok: false, reason: "trace_span tool missing" };
|
|
113
78
|
return span.handler(args, runtime);
|
|
114
79
|
},
|
|
115
80
|
}),
|
package/docs/installation.md
CHANGED
|
@@ -70,7 +70,7 @@ npm install opencode-tracekit
|
|
|
70
70
|
3. 检查是否有以下记录类型:
|
|
71
71
|
- `capture_start`
|
|
72
72
|
- `span_start` / `span_end`
|
|
73
|
-
- `tracepoint`(调用 `
|
|
73
|
+
- `tracepoint`(调用 `trace_emit` 后出现)
|
|
74
74
|
|
|
75
75
|
你也可以用内置 CLI 验证:
|
|
76
76
|
|
|
@@ -82,15 +82,15 @@ node dist/src/cli.js analyze ./trace.ndjson
|
|
|
82
82
|
## 4. 在 Agent/Skill 中使用打点工具
|
|
83
83
|
|
|
84
84
|
```json
|
|
85
|
-
{ "tool": "
|
|
85
|
+
{ "tool": "trace_emit", "args": { "name": "agent.plan.generated", "level": "info" } }
|
|
86
86
|
```
|
|
87
87
|
|
|
88
88
|
```json
|
|
89
|
-
{ "tool": "
|
|
89
|
+
{ "tool": "trace_span", "args": { "op": "start", "name": "ipd.phase.spec" } }
|
|
90
90
|
```
|
|
91
91
|
|
|
92
92
|
```json
|
|
93
|
-
{ "tool": "
|
|
93
|
+
{ "tool": "trace_counter", "args": { "name": "token.estimate", "value": 128 } }
|
|
94
94
|
```
|
|
95
95
|
|
|
96
96
|
## 5. 导出与分析
|
|
@@ -119,5 +119,5 @@ node dist/src/cli.js export ./trace.ndjson --out ./trace.csv --format csv
|
|
|
119
119
|
- 检查 runtime 是否正确触发了 `onToolEnd`
|
|
120
120
|
- 检查 `toolCallId` 是否在 start/end 事件里一致
|
|
121
121
|
|
|
122
|
-
- `
|
|
122
|
+
- `trace_emit` 不可用:
|
|
123
123
|
- 检查 `tools: tracekit.tools` 是否传入 runtime
|