opencode-tracekit 0.1.0 → 0.1.1

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/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,83 @@
1
+ import { type TraceKitConfig } from "./tracekit_plugin.js";
2
+ interface OpenCodePluginContext {
3
+ workingDirectory?: string;
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
+ interface OpenCodeExecContext {
37
+ sessionID?: string;
38
+ sessionId?: string;
39
+ }
40
+ interface OpenCodeToolDefinition {
41
+ description: string;
42
+ parameters: Record<string, unknown>;
43
+ execute: (args: Record<string, unknown>, execCtx: OpenCodeExecContext) => Promise<Record<string, unknown>>;
44
+ }
45
+ 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
+ tool: {
59
+ "trace.emit": OpenCodeToolDefinition;
60
+ "trace.counter": OpenCodeToolDefinition;
61
+ "trace.span": OpenCodeToolDefinition;
62
+ };
63
+ }>;
64
+ 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
+ tool: {
78
+ "trace.emit": OpenCodeToolDefinition;
79
+ "trace.counter": OpenCodeToolDefinition;
80
+ "trace.span": OpenCodeToolDefinition;
81
+ };
82
+ }>;
83
+ export {};
@@ -0,0 +1,122 @@
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
+ "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
+ tool: {
55
+ "trace.emit": defineTool({
56
+ description: "Emit a tracepoint for current session",
57
+ parameters: {
58
+ type: "object",
59
+ properties: {
60
+ name: { type: "string" },
61
+ level: { type: "string", enum: ["info", "warn", "error"] },
62
+ attrs: { type: "object", additionalProperties: true },
63
+ links: { type: "array", items: { type: "object", additionalProperties: true } },
64
+ },
65
+ required: ["name"],
66
+ },
67
+ execute: async (args, execCtx) => {
68
+ const runtime = toRuntime(execCtx);
69
+ const emit = core.tools.find((t) => t.name === "trace.emit");
70
+ if (!emit)
71
+ return { ok: false, reason: "trace.emit tool missing" };
72
+ return emit.handler(args, runtime);
73
+ },
74
+ }),
75
+ "trace.counter": defineTool({
76
+ description: "Emit a counter metric for current session",
77
+ parameters: {
78
+ type: "object",
79
+ properties: {
80
+ name: { type: "string" },
81
+ value: { type: "number" },
82
+ attrs: { type: "object", additionalProperties: true },
83
+ },
84
+ required: ["name", "value"],
85
+ },
86
+ execute: async (args, execCtx) => {
87
+ const runtime = toRuntime(execCtx);
88
+ const counter = core.tools.find((t) => t.name === "trace.counter");
89
+ if (!counter)
90
+ return { ok: false, reason: "trace.counter tool missing" };
91
+ return counter.handler(args, runtime);
92
+ },
93
+ }),
94
+ "trace.span": defineTool({
95
+ description: "Create a manual span for phase tracking",
96
+ parameters: {
97
+ type: "object",
98
+ properties: {
99
+ op: { type: "string", enum: ["start", "end"] },
100
+ spanId: { type: "string" },
101
+ name: { type: "string" },
102
+ kind: { type: "string", enum: ["manual", "message", "model", "tool", "skill"] },
103
+ status: { type: "string", enum: ["ok", "error", "unknown"] },
104
+ attrs: { type: "object", additionalProperties: true },
105
+ },
106
+ required: ["op"],
107
+ },
108
+ execute: async (args, execCtx) => {
109
+ const runtime = toRuntime(execCtx);
110
+ const span = core.tools.find((t) => t.name === "trace.span");
111
+ if (!span)
112
+ return { ok: false, reason: "trace.span tool missing" };
113
+ return span.handler(args, runtime);
114
+ },
115
+ }),
116
+ },
117
+ };
118
+ };
119
+ }
120
+ function defineTool(toolDef) {
121
+ return toolDef;
122
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-tracekit",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Tracepoint and tracing plugin for OpenCode (agent/skill/tool spans, emit/export/analyze).",
5
5
  "license": "MIT",
6
6
  "type": "module",