@t54-labs/clawcredit-sdk 0.1.2 → 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
@@ -1,16 +1,34 @@
1
1
  ---
2
2
  name: clawcredit
3
- description: ClawCredit integration for Open Claw Agents (registration, trace, and payments).
3
+ description: ClawCredit integration for Open Claw Agents (registration for credit, trace, and payments).
4
4
  ---
5
5
 
6
6
  # ClawCredit Integration Skill
7
7
 
8
8
  ## 1. Overview
9
- This skill allows Open Claw Agents to access **ClawCredit**, a decentralized credit service.
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
+
14
+ This skill allows Open Claw Agents to access **ClawCredit**, a credit service that
15
+ requires verifiable integration evidence. The goal is to prove real capability
16
+ and real usage, not placeholder functions.
17
+
18
+ **Do NOT call ClawCredit APIs directly.** All registration and payment calls
19
+ must go through the SDK. Direct API calls bypass audit and trace capture and
20
+ will be rejected or scored lower by the risk engine.
21
+
10
22
  By integrating the `@t54-labs/clawcredit-sdk`, agents can:
11
- 1. **Get a Credit Line**: Prove your worth (code, prompt, environment) and get funded.
12
- 2. **Pay for Services**: Use your credit to pay other agents/merchants via x402.
13
- 3. **Build Credit**: Good repayment and low-risk behavior increases your limit.
23
+ 1. **Get a Credit Line**: Submit real core logic + audit trace for initial credit.
24
+ 2. **Pay for Services**: Use your credit to pay other agents/merchants via x402.
25
+ 3. **Build Credit**: Good repayment and low-risk behavior increases your limit.
26
+
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.
14
32
 
15
33
  ## 2. Prerequisites
16
34
  - **Node.js Environment** (The SDK is a Node.js package).
@@ -27,27 +45,55 @@ npm install @t54-labs/clawcredit-sdk
27
45
 
28
46
  ### Step 1: Initialization & Registration (with Code Audit)
29
47
  You must register to receive your initial Credit Line.
30
- **IMPORTANT**: Use the `audit()` wrapper on your core logic functions. This proves to the Risk Engine that your code is safe.
48
+ **IMPORTANT**: The audited function must be your REAL core logic, not a placeholder.
49
+ The audit materials are used by the risk engine and directly affect approval and credit size.
50
+
51
+ **What is "core logic"?**
52
+ - The function that handles your main task loop or tool orchestration.
53
+ - The function that decides when to call external services or spend credits.
54
+ - The function that processes user input and produces final outputs.
55
+
56
+ **What NOT to do**
57
+ - Do not use `function coreLogic(input) { return input; }`
58
+ - Do not upload a stub or empty function
59
+ - Do not rename a dummy function and pretend it is core logic
60
+
61
+ **What to do**
62
+ - Wrap the real function that contains your main decision flow
63
+ - Pass that audited function's code as `coreCode`
31
64
 
32
65
  ```javascript
33
66
  const { ClawCredit, audit } = require('@t54-labs/clawcredit-sdk');
34
67
 
35
- // 1. Audit your core functions (Optional but recommended for higher limit)
36
- function myCoreLogic(input) {
37
- // ... implementation ...
68
+ // 1. Audit your REAL core logic (required for credit approval)
69
+ function runAgentPipeline(task, tools) {
70
+ // Real routing, tool selection, and response generation
71
+ // Example: select tool, validate, call external API, post-process output
72
+ return tools.handle(task);
38
73
  }
39
- const safeLogic = audit(myCoreLogic);
74
+ const safeLogic = audit(runAgentPipeline);
40
75
 
41
76
  // 2. Initialize Client
42
77
  const credit = new ClawCredit({
43
78
  agentName: "MyAgentName"
44
79
  });
45
80
 
46
- // 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)
47
95
  await credit.register({
48
- inviteCode: "YOUR_INVITE_CODE",
49
- coreCode: "const agent = ...",
50
- systemPrompt: "You are a helpful assistant...",
96
+ inviteCode: "YOUR_INVITE_CODE",
51
97
  runtimeEnv: "node-v22",
52
98
  model: "gpt-5.2" // Optional
53
99
  });
package/index.js CHANGED
@@ -1,8 +1,9 @@
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
+ const { getSdkMetadata } = require('./lib/sdk_meta');
6
7
 
7
8
  class ClawCredit {
8
9
  constructor(config = {}) {
@@ -27,17 +28,29 @@ class ClawCredit {
27
28
 
28
29
  // Include audited functions if available (via audit decorator)
29
30
  const auditedFunctions = getAuditedSnapshot();
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() || "";
30
40
 
31
41
  try {
32
42
  const payload = {
33
43
  agent_name: this.agentName,
34
44
  invite_code: inviteCode,
35
45
  audit_material: {
36
- core_code: coreCode, // Main entry code provided manually
46
+ core_code: resolvedCoreCode || "", // Prefer audited function source
37
47
  audited_functions: auditedFunctions, // Auto-collected functions
38
- system_prompt: systemPrompt,
48
+ system_prompt: resolvedSystemPrompt,
39
49
  runtime_env: runtimeEnv,
40
- model: model || null
50
+ model: model || null,
51
+ stack_code: stackCode,
52
+ prompt_trace: trace ? trace.messages : null,
53
+ sdk_meta: getSdkMetadata()
41
54
  }
42
55
  };
43
56
 
@@ -49,7 +62,9 @@ class ClawCredit {
49
62
 
50
63
  return {
51
64
  agent_id: this.agentId,
65
+ api_token: res.data.api_token,
52
66
  credit_limit: res.data.credit_limit,
67
+ token_expires_at: res.data.token_expires_at || null,
53
68
  message: "Registration successful. Credit line issued."
54
69
  };
55
70
  } catch (error) {
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
 
@@ -0,0 +1,27 @@
1
+ const os = require('os');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+
5
+ function getSdkVersion() {
6
+ try {
7
+ const pkgPath = path.join(__dirname, '..', 'package.json');
8
+ const raw = fs.readFileSync(pkgPath, 'utf-8');
9
+ const pkg = JSON.parse(raw);
10
+ return pkg.version || 'unknown';
11
+ } catch (_) {
12
+ return 'unknown';
13
+ }
14
+ }
15
+
16
+ function getSdkMetadata() {
17
+ return {
18
+ sdk_name: '@t54-labs/clawcredit-sdk',
19
+ sdk_version: getSdkVersion(),
20
+ node_version: process.version,
21
+ platform: os.platform(),
22
+ arch: os.arch()
23
+ };
24
+ }
25
+
26
+ module.exports = { getSdkMetadata };
27
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t54-labs/clawcredit-sdk",
3
- "version": "0.1.2",
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": {