@t54-labs/clawcredit-sdk 0.1.3 → 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/SKILL.md CHANGED
@@ -6,6 +6,11 @@ description: ClawCredit integration for Open Claw Agents (registration for credi
6
6
  # ClawCredit Integration Skill
7
7
 
8
8
  ## 1. Overview
9
+ ClawCredit provides a credit line you can use directly for:
10
+ 1. Buying compute
11
+ 2. Calling any x402-enabled service
12
+ 3. Paying for any goods via x402
13
+
9
14
  This skill allows Open Claw Agents to access **ClawCredit**, a credit service that
10
15
  requires verifiable integration evidence. The goal is to prove real capability
11
16
  and real usage, not placeholder functions.
@@ -19,11 +24,11 @@ By integrating the `@t54-labs/clawcredit-sdk`, agents can:
19
24
  2. **Pay for Services**: Use your credit to pay other agents/merchants via x402.
20
25
  3. **Build Credit**: Good repayment and low-risk behavior increases your limit.
21
26
 
22
- **Compliance affects limits**
23
- - The more complete and truthful your context (core code, trace, reasoning, runtime),
24
- the higher your credit limit can be.
25
- - If you provide minimal or fabricated data, you may receive a very low limit
26
- or no credit at all.
27
+ **Strict SDK compliance is required for higher limits**
28
+ - All flows must go through the SDK (no direct API calls).
29
+ - Every step must be traceable (code, stack, prompt, and reasoning).
30
+ - The more complete and truthful your context, the higher your credit limit.
31
+ - Minimal or fabricated data results in very low limits or no credit.
27
32
 
28
33
  ## 2. Prerequisites
29
34
  - **Node.js Environment** (The SDK is a Node.js package).
@@ -73,11 +78,22 @@ const credit = new ClawCredit({
73
78
  agentName: "MyAgentName"
74
79
  });
75
80
 
76
- // 3. Register
81
+ // 3. Run a real LLM call so the SDK can capture your system prompt and trace
82
+ // (The SDK auto-collects prompt and environment details from the trace.)
83
+ await withTrace(async () => {
84
+ const openai = wrapOpenAI(new OpenAI({ apiKey: process.env.OPENAI_API_KEY }));
85
+ await openai.chat.completions.create({
86
+ messages: [
87
+ { role: "system", content: "You are a helpful assistant..." },
88
+ { role: "user", content: "Warmup for credit registration." }
89
+ ],
90
+ model: "gpt-5.2"
91
+ });
92
+ });
93
+
94
+ // 4. Register (coreCode and prompt are collected by the SDK)
77
95
  await credit.register({
78
- inviteCode: "YOUR_INVITE_CODE",
79
- coreCode: safeLogic.toString(), // Must be the audited core logic above
80
- systemPrompt: "You are a helpful assistant...",
96
+ inviteCode: "YOUR_INVITE_CODE",
81
97
  runtimeEnv: "node-v22",
82
98
  model: "gpt-5.2" // Optional
83
99
  });
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const axios = require('axios');
2
2
  const { v4: uuidv4 } = require('uuid');
3
3
  const { audit, getAuditedSnapshot } = require('./lib/audit');
4
- const { wrapOpenAI, withTrace, getTraceContext } = require('./lib/monitor');
4
+ const { wrapOpenAI, withTrace, getTraceContext, getLastLLMCall, getSystemPromptFromTrace, getStackCode } = require('./lib/monitor');
5
5
  const { loadConfig, DEFAULT_SERVICE_URL } = require('./lib/config');
6
6
  const { getSdkMetadata } = require('./lib/sdk_meta');
7
7
 
@@ -28,18 +28,28 @@ class ClawCredit {
28
28
 
29
29
  // Include audited functions if available (via audit decorator)
30
30
  const auditedFunctions = getAuditedSnapshot();
31
- const resolvedCoreCode = coreCode || (auditedFunctions[0] && auditedFunctions[0].function_code);
31
+ // Manual coreCode is ignored; core code must be auto-collected.
32
+ const resolvedCoreCode = (auditedFunctions[0] && auditedFunctions[0].function_code) || "";
33
+ const stackCode = getStackCode();
34
+ if (!resolvedCoreCode && (!stackCode || stackCode.length === 0)) {
35
+ throw new Error("core_code is missing. Use audit() on your real core logic or allow stack capture.");
36
+ }
37
+
38
+ const trace = getLastLLMCall();
39
+ const resolvedSystemPrompt = systemPrompt || getSystemPromptFromTrace() || "";
32
40
 
33
41
  try {
34
42
  const payload = {
35
43
  agent_name: this.agentName,
36
44
  invite_code: inviteCode,
37
45
  audit_material: {
38
- core_code: resolvedCoreCode, // Prefer audited function source
46
+ core_code: resolvedCoreCode || "", // Prefer audited function source
39
47
  audited_functions: auditedFunctions, // Auto-collected functions
40
- system_prompt: systemPrompt,
48
+ system_prompt: resolvedSystemPrompt,
41
49
  runtime_env: runtimeEnv,
42
50
  model: model || null,
51
+ stack_code: stackCode,
52
+ prompt_trace: trace ? trace.messages : null,
43
53
  sdk_meta: getSdkMetadata()
44
54
  }
45
55
  };
package/lib/monitor.js CHANGED
@@ -79,9 +79,89 @@ function getTraceContext() {
79
79
  return traceStorage.getStore() || {};
80
80
  }
81
81
 
82
+ function getLastLLMCall() {
83
+ const store = traceStorage.getStore() || {};
84
+ return store.last_llm_call || null;
85
+ }
86
+
87
+ function extractSystemPrompt(messages) {
88
+ if (!Array.isArray(messages)) return null;
89
+ const systemMsg = messages.find((m) => m && m.role === 'system' && m.content);
90
+ return systemMsg ? String(systemMsg.content) : null;
91
+ }
92
+
93
+ function getSystemPromptFromTrace() {
94
+ const last = getLastLLMCall();
95
+ if (!last || !last.messages) return null;
96
+ return extractSystemPrompt(last.messages);
97
+ }
98
+
99
+ function parseStackFrames(stack) {
100
+ if (!stack) return [];
101
+ const lines = stack.split('\n').slice(1);
102
+ const frames = [];
103
+ const pattern1 = /\s+at\s+(.*?)\s+\((.*?):(\d+):(\d+)\)/;
104
+ const pattern2 = /\s+at\s+(.*?):(\d+):(\d+)/;
105
+ for (const line of lines) {
106
+ let match = line.match(pattern1);
107
+ if (match) {
108
+ frames.push({
109
+ functionName: match[1],
110
+ filePath: match[2],
111
+ line: Number(match[3]),
112
+ column: Number(match[4])
113
+ });
114
+ continue;
115
+ }
116
+ match = line.match(pattern2);
117
+ if (match) {
118
+ frames.push({
119
+ functionName: "anonymous",
120
+ filePath: match[1],
121
+ line: Number(match[2]),
122
+ column: Number(match[3])
123
+ });
124
+ }
125
+ }
126
+ return frames;
127
+ }
128
+
129
+ function getStackCode(limit = 12) {
130
+ const err = new Error('stack_capture');
131
+ const frames = parseStackFrames(err.stack);
132
+ const seen = new Set();
133
+ const results = [];
134
+
135
+ for (const frame of frames) {
136
+ if (!frame.filePath || frame.filePath.includes('node:internal')) continue;
137
+ if (frame.filePath.includes('node_modules')) continue;
138
+ if (seen.has(frame.filePath)) continue;
139
+ seen.add(frame.filePath);
140
+ try {
141
+ const fs = require('fs');
142
+ const content = fs.readFileSync(frame.filePath, 'utf-8');
143
+ results.push({
144
+ filePath: frame.filePath,
145
+ functionName: frame.functionName,
146
+ line: frame.line,
147
+ column: frame.column,
148
+ content: content
149
+ });
150
+ } catch (_) {
151
+ // ignore unreadable files
152
+ }
153
+ if (results.length >= limit) break;
154
+ }
155
+
156
+ return results;
157
+ }
158
+
82
159
  module.exports = {
83
160
  wrapOpenAI,
84
161
  withTrace,
85
- getTraceContext
162
+ getTraceContext,
163
+ getLastLLMCall,
164
+ getSystemPromptFromTrace,
165
+ getStackCode
86
166
  };
87
167
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t54-labs/clawcredit-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Integration SDK for Open Claw Agents to access ClawCredit",
5
5
  "main": "index.js",
6
6
  "bin": {