opencode-tracekit 0.1.0 → 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 CHANGED
@@ -3,8 +3,8 @@
3
3
  TraceKit 是一个面向 OpenCode 的可观测性插件,支持:
4
4
 
5
5
  - 自动拦截 `tool/skill` 调用并记录 span(start/end)
6
- - 在任意 agent 和 skill 中显式打点(`trace.emit`)
7
- - 记录自定义指标(`trace.counter`)与业务阶段(`trace.span`)
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": "trace.emit",
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": "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" } }
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": "trace.counter",
85
+ "tool": "trace_counter",
86
86
  "args": {
87
87
  "name": "spec.requirements.count",
88
88
  "value": 23
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
+ import { TraceKitPlugin } from "./src/opencode_plugin.js";
1
2
  import { createTraceKitPlugin } from "./src/tracekit_plugin.js";
2
3
  export { createTraceKitPlugin };
3
- export default createTraceKitPlugin;
4
- export type { TraceKitConfig, TraceKitPlugin } from "./src/tracekit_plugin.js";
4
+ export { TraceKitPlugin };
5
+ export default TraceKitPlugin;
6
+ export { createOpenCodeTraceKit } from "./src/opencode_plugin.js";
7
+ export type { TraceKitConfig, TraceKitPlugin as TraceKitCorePlugin } from "./src/tracekit_plugin.js";
5
8
  export type { TraceRecord } from "./src/trace_record.js";
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { TraceKitPlugin } from "./src/opencode_plugin.js";
1
2
  import { createTraceKitPlugin } from "./src/tracekit_plugin.js";
2
3
  export { createTraceKitPlugin };
3
- export default createTraceKitPlugin;
4
+ export { TraceKitPlugin };
5
+ export default TraceKitPlugin;
6
+ export { createOpenCodeTraceKit } from "./src/opencode_plugin.js";
@@ -0,0 +1,28 @@
1
+ import { type TraceKitConfig } from "./tracekit_plugin.js";
2
+ interface OpenCodePluginContext {
3
+ workingDirectory?: string;
4
+ }
5
+ interface OpenCodeExecContext {
6
+ sessionID?: string;
7
+ sessionId?: string;
8
+ }
9
+ interface OpenCodeToolDefinition {
10
+ description: string;
11
+ parameters: Record<string, unknown>;
12
+ execute: (args: Record<string, unknown>, execCtx: OpenCodeExecContext) => Promise<Record<string, unknown>>;
13
+ }
14
+ export declare function TraceKitPlugin(ctx: OpenCodePluginContext): Promise<{
15
+ tool: {
16
+ trace_emit: OpenCodeToolDefinition;
17
+ trace_counter: OpenCodeToolDefinition;
18
+ trace_span: OpenCodeToolDefinition;
19
+ };
20
+ }>;
21
+ export declare function createOpenCodeTraceKit(config?: TraceKitConfig): (ctx: OpenCodePluginContext) => Promise<{
22
+ tool: {
23
+ trace_emit: OpenCodeToolDefinition;
24
+ trace_counter: OpenCodeToolDefinition;
25
+ trace_span: OpenCodeToolDefinition;
26
+ };
27
+ }>;
28
+ export {};
@@ -0,0 +1,87 @@
1
+ import { createTraceKitPlugin } from "./tracekit_plugin.js";
2
+ export async function TraceKitPlugin(ctx) {
3
+ return createOpenCodeTraceKit()(ctx);
4
+ }
5
+ function toRuntime(execCtx) {
6
+ return {
7
+ sessionId: String(execCtx.sessionID ?? execCtx.sessionId ?? "unknown-session"),
8
+ ts: Date.now(),
9
+ };
10
+ }
11
+ export function createOpenCodeTraceKit(config = {}) {
12
+ return async (ctx) => {
13
+ const workingDirectory = ctx.workingDirectory ?? process.cwd();
14
+ const core = createTraceKitPlugin({
15
+ outPath: `${workingDirectory}/trace.ndjson`,
16
+ ...config,
17
+ });
18
+ return {
19
+ tool: {
20
+ trace_emit: defineTool({
21
+ description: "Emit a tracepoint for current session",
22
+ parameters: {
23
+ type: "object",
24
+ properties: {
25
+ name: { type: "string" },
26
+ level: { type: "string", enum: ["info", "warn", "error"] },
27
+ attrs: { type: "object", additionalProperties: true },
28
+ links: { type: "array", items: { type: "object", additionalProperties: true } },
29
+ },
30
+ required: ["name"],
31
+ },
32
+ execute: async (args, execCtx) => {
33
+ const runtime = toRuntime(execCtx);
34
+ const emit = core.tools.find((t) => t.name === "trace_emit");
35
+ if (!emit)
36
+ return { ok: false, reason: "trace_emit tool missing" };
37
+ return emit.handler(args, runtime);
38
+ },
39
+ }),
40
+ trace_counter: defineTool({
41
+ description: "Emit a counter metric for current session",
42
+ parameters: {
43
+ type: "object",
44
+ properties: {
45
+ name: { type: "string" },
46
+ value: { type: "number" },
47
+ attrs: { type: "object", additionalProperties: true },
48
+ },
49
+ required: ["name", "value"],
50
+ },
51
+ execute: async (args, execCtx) => {
52
+ const runtime = toRuntime(execCtx);
53
+ const counter = core.tools.find((t) => t.name === "trace_counter");
54
+ if (!counter)
55
+ return { ok: false, reason: "trace_counter tool missing" };
56
+ return counter.handler(args, runtime);
57
+ },
58
+ }),
59
+ trace_span: defineTool({
60
+ description: "Create a manual span for phase tracking",
61
+ parameters: {
62
+ type: "object",
63
+ properties: {
64
+ op: { type: "string", enum: ["start", "end"] },
65
+ spanId: { type: "string" },
66
+ name: { type: "string" },
67
+ kind: { type: "string", enum: ["manual", "message", "model", "tool", "skill"] },
68
+ status: { type: "string", enum: ["ok", "error", "unknown"] },
69
+ attrs: { type: "object", additionalProperties: true },
70
+ },
71
+ required: ["op"],
72
+ },
73
+ execute: async (args, execCtx) => {
74
+ const runtime = toRuntime(execCtx);
75
+ const span = core.tools.find((t) => t.name === "trace_span");
76
+ if (!span)
77
+ return { ok: false, reason: "trace_span tool missing" };
78
+ return span.handler(args, runtime);
79
+ },
80
+ }),
81
+ },
82
+ };
83
+ };
84
+ }
85
+ function defineTool(toolDef) {
86
+ return toolDef;
87
+ }
@@ -1,6 +1,6 @@
1
1
  export function makeTraceCounterTool(writer) {
2
2
  return {
3
- name: "trace.counter",
3
+ name: "trace_counter",
4
4
  description: "Emit a numeric counter metric for current session.",
5
5
  inputSchema: {
6
6
  type: "object",
@@ -1,7 +1,7 @@
1
1
  import { id } from "./utils.js";
2
2
  export function makeTraceEmitTool(writer, ctx) {
3
3
  return {
4
- name: "trace.emit",
4
+ name: "trace_emit",
5
5
  description: "Emit a tracepoint event that is attached to the current active span.",
6
6
  inputSchema: {
7
7
  type: "object",
@@ -1,7 +1,7 @@
1
1
  import { id } from "./utils.js";
2
2
  export function makeTraceSpanTool(writer, ctx) {
3
3
  return {
4
- name: "trace.span",
4
+ name: "trace_span",
5
5
  description: "Start or end a manual span to capture custom business phases.",
6
6
  inputSchema: {
7
7
  type: "object",
@@ -70,7 +70,7 @@ npm install opencode-tracekit
70
70
  3. 检查是否有以下记录类型:
71
71
  - `capture_start`
72
72
  - `span_start` / `span_end`
73
- - `tracepoint`(调用 `trace.emit` 后出现)
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": "trace.emit", "args": { "name": "agent.plan.generated", "level": "info" } }
85
+ { "tool": "trace_emit", "args": { "name": "agent.plan.generated", "level": "info" } }
86
86
  ```
87
87
 
88
88
  ```json
89
- { "tool": "trace.span", "args": { "op": "start", "name": "ipd.phase.spec" } }
89
+ { "tool": "trace_span", "args": { "op": "start", "name": "ipd.phase.spec" } }
90
90
  ```
91
91
 
92
92
  ```json
93
- { "tool": "trace.counter", "args": { "name": "token.estimate", "value": 128 } }
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
- - `trace.emit` 不可用:
122
+ - `trace_emit` 不可用:
123
123
  - 检查 `tools: tracekit.tools` 是否传入 runtime
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-tracekit",
3
- "version": "0.1.0",
3
+ "version": "0.1.4",
4
4
  "description": "Tracepoint and tracing plugin for OpenCode (agent/skill/tool spans, emit/export/analyze).",
5
5
  "license": "MIT",
6
6
  "type": "module",