chitin-openclaw-plugin 0.4.1 → 0.4.3

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.
Files changed (2) hide show
  1. package/dist/index.js +28 -12
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ import { privateKeyToAccount } from "viem/accounts";
12
12
  import { baseSepolia } from "viem/chains";
13
13
  import WebSocket from "ws";
14
14
  import { execFile } from "child_process";
15
- import { trace, SpanStatusCode } from "@opentelemetry/api";
15
+ import { trace, context, SpanStatusCode } from "@opentelemetry/api";
16
16
  import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
17
17
  import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-node";
18
18
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
@@ -46,11 +46,13 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
46
46
  logger.info(`[chitin] WS connecting to ${wsUrl}`);
47
47
  ws = new WebSocket(wsUrl);
48
48
  ws.on("open", () => {
49
+ const span = moduleTracer.startSpan("plugin.ws.connect", { attributes: { "ws.room": roomCode, "ws.player": playerId } });
49
50
  logger.info(`[chitin] WS connected to ${roomCode}`);
50
51
  wsStore.set(roomCode, ws);
51
52
  const enterMsg = { type: "enter", playerId, name, walletAddress };
52
53
  logger.info(`[chitin] WS sending enter: ${JSON.stringify(enterMsg)}`);
53
54
  ws.send(JSON.stringify(enterMsg));
55
+ span.end();
54
56
  });
55
57
  ws.on("message", (data) => {
56
58
  try {
@@ -62,7 +64,9 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
62
64
  });
63
65
  ws.on("close", (code) => {
64
66
  if (aborted) return;
67
+ const span = moduleTracer.startSpan("plugin.ws.disconnect", { attributes: { "ws.room": roomCode, "ws.close_code": code } });
65
68
  logger.info(`[chitin] WS closed (code ${code}), reconnecting in 3s...`);
69
+ span.end();
66
70
  setTimeout(connect, 3e3);
67
71
  });
68
72
  ws.on("error", (err) => {
@@ -75,6 +79,7 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
75
79
  latestState.set(roomCode2, msg);
76
80
  logger2.info(`[chitin] game_state: players=${msg.players?.length || 0} hand=${msg.isHandInProgress ? "yes" : "no"}`);
77
81
  } else if (msg.type === "your_turn") {
82
+ const span = moduleTracer.startSpan("plugin.ws.your_turn", { attributes: { "ws.room": roomCode2, "ws.actions": msg.legalActions?.join(",") || "" } });
78
83
  const state = latestState.get(roomCode2);
79
84
  const legalActions = msg.legalActions?.join(", ") || "";
80
85
  const chipRange = msg.chipRange ? `${msg.chipRange.min}-${msg.chipRange.max}` : "";
@@ -98,20 +103,26 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
98
103
  parts.push(`Legal actions: ${legalActions}.`);
99
104
  if (chipRange) parts.push(`Bet/raise range: ${chipRange}.`);
100
105
  parts.push(`Use chitin_poker_action to respond with roomCode "${roomCode2}".`);
101
- promptAgent(parts.join(" "), logger2);
106
+ promptAgent(parts.join(" "), logger2, span);
102
107
  } else if (msg.type === "hand_result") {
108
+ const span = moduleTracer.startSpan("plugin.ws.hand_result", { attributes: { "ws.room": roomCode2 } });
103
109
  const winners = msg.result?.winners?.flat() || [];
104
110
  const winnerNames = winners.map((w) => `${w.playerId}(${w.amount})`).join(", ");
105
111
  logger2.info(`[chitin] HAND RESULT: winners=${winnerNames} rake=${msg.result?.rake || 0}`);
106
112
  const text = `POKER HAND RESULT at table ${roomCode2}: Winners: ${winnerNames}. Rake: ${msg.result?.rake || 0}.`;
107
- promptAgent(text, logger2);
113
+ promptAgent(text, logger2, span);
108
114
  } else {
109
115
  logger2.info(`[chitin] WS msg: type=${msg.type}`);
110
116
  }
111
117
  }
112
- function promptAgent(text, logger2) {
118
+ function promptAgent(text, logger2, parentSpan) {
119
+ const span = moduleTracer.startSpan(
120
+ "plugin.ws.prompt_agent",
121
+ {},
122
+ parentSpan ? trace.setSpan(context.active(), parentSpan) : void 0
123
+ );
113
124
  logger2.info(`[chitin] Prompting agent: ${text.slice(0, 100)}...`);
114
- const child = execFile("openclaw", ["agent", "--local", "--message", text], {
125
+ execFile("openclaw", ["agent", "--local", "--message", text], {
115
126
  timeout: 12e4,
116
127
  env: process.env
117
128
  // Inherit HOME pointing to workspace
@@ -119,11 +130,15 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
119
130
  if (err) {
120
131
  logger2.error(`[chitin] Agent prompt failed: ${err.message}`);
121
132
  if (stderr) logger2.error(`[chitin] Agent stderr: ${stderr.slice(0, 500)}`);
122
- return;
133
+ span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
134
+ } else {
135
+ if (stdout) logger2.info(`[chitin] Agent response: ${stdout.slice(0, 300)}`);
136
+ if (stderr) logger2.warn(`[chitin] Agent stderr: ${stderr.slice(0, 300)}`);
137
+ logger2.info(`[chitin] Agent prompted ok`);
138
+ span.setStatus({ code: SpanStatusCode.OK });
123
139
  }
124
- if (stdout) logger2.info(`[chitin] Agent response: ${stdout.slice(0, 300)}`);
125
- if (stderr) logger2.warn(`[chitin] Agent stderr: ${stderr.slice(0, 300)}`);
126
- logger2.info(`[chitin] Agent prompted ok`);
140
+ span.end();
141
+ parentSpan?.end();
127
142
  });
128
143
  }
129
144
  connect();
@@ -133,6 +148,7 @@ function connectToTable(gameServerUrl, roomCode, playerId, name, walletAddress,
133
148
  };
134
149
  }
135
150
  var otelInitialized = false;
151
+ var moduleTracer = trace.getTracer("chitin-plugin");
136
152
  var plugin = {
137
153
  id: "chitin-openclaw-plugin",
138
154
  name: "Chitin Casino",
@@ -144,7 +160,7 @@ var plugin = {
144
160
  },
145
161
  register(api) {
146
162
  api.logger.info("[chitin] Chitin Casino plugin loaded");
147
- let tracer = trace.getTracer("chitin-plugin");
163
+ let tracer = moduleTracer;
148
164
  if (!otelInitialized) try {
149
165
  otelInitialized = true;
150
166
  const endpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
@@ -161,7 +177,7 @@ var plugin = {
161
177
  const agentId = process.env.AGENT_ID || "unknown";
162
178
  const pluginResource = resourceFromAttributes({
163
179
  [ATTR_SERVICE_NAME]: "chitin-plugin",
164
- "service.namespace": process.env.HOSTNAME || "unknown",
180
+ "service.namespace": "clawbot-runner",
165
181
  "service.instance.id": `${agentName}-${agentId.slice(0, 8)}`,
166
182
  "agent.name": agentName,
167
183
  "agent.id": agentId
@@ -187,7 +203,7 @@ var plugin = {
187
203
  ]
188
204
  });
189
205
  logs.setGlobalLoggerProvider(logProvider);
190
- tracer = trace.getTracer("chitin-plugin");
206
+ tracer = moduleTracer = trace.getTracer("chitin-plugin");
191
207
  api.logger.info(`[chitin] OTel initialized \u2014 endpoint=${endpoint}`);
192
208
  const span = tracer.startSpan("plugin.loaded");
193
209
  span.setAttributes({ "plugin.name": "chitin-openclaw-plugin" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chitin-openclaw-plugin",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "OpenClaw plugin for Chitin Casino — wallet management and poker game connection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",