@coolclaw/coolclaw 0.2.9 → 0.3.0

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.
@@ -0,0 +1,105 @@
1
+ // src/binding.ts
2
+ import { mkdir, readFile, rename, writeFile, chmod } from "fs/promises";
3
+ import { homedir } from "os";
4
+ import path from "path";
5
+ import { fileURLToPath } from "url";
6
+ var RIDDLE_BINDING_VERSION = 1;
7
+ function defaultBindingFile(home = homedir()) {
8
+ return path.join(home, ".config", "coolclaw", "agent_binding.json");
9
+ }
10
+ function defaultOpenClawConfigFile(home = homedir()) {
11
+ return path.join(home, ".openclaw", "openclaw.json");
12
+ }
13
+ function defaultTokenFile(bindingFile, agentId) {
14
+ return path.join(path.dirname(bindingFile), `agent_token_${agentId}.txt`);
15
+ }
16
+ async function loadBinding(bindingFile) {
17
+ try {
18
+ const raw = JSON.parse(await readFile(bindingFile, "utf8"));
19
+ return {
20
+ agentId: String(raw.agentId ?? ""),
21
+ tokenRef: typeof raw.tokenRef === "string" ? raw.tokenRef : null,
22
+ runtimeType: String(raw.runtimeType ?? "unknown"),
23
+ lastAckedSeq: Number(raw.lastAckedSeq ?? 0),
24
+ bindingVersion: Number(raw.bindingVersion ?? RIDDLE_BINDING_VERSION),
25
+ updatedAt: typeof raw.updatedAt === "string" ? raw.updatedAt : void 0
26
+ };
27
+ } catch {
28
+ return {
29
+ agentId: "",
30
+ tokenRef: null,
31
+ runtimeType: "unknown",
32
+ lastAckedSeq: 0,
33
+ bindingVersion: RIDDLE_BINDING_VERSION
34
+ };
35
+ }
36
+ }
37
+ function touchBinding(binding) {
38
+ return {
39
+ ...binding,
40
+ bindingVersion: RIDDLE_BINDING_VERSION,
41
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString().replace(/\.\d{3}Z$/, "Z")
42
+ };
43
+ }
44
+ async function saveBinding(bindingFile, binding) {
45
+ await mkdir(path.dirname(bindingFile), { recursive: true });
46
+ const tmpFile = `${bindingFile}.${process.pid}.tmp`;
47
+ await writeFile(tmpFile, `${JSON.stringify(touchBinding(binding), null, 2)}
48
+ `, { mode: 384 });
49
+ await chmod(tmpFile, 384);
50
+ await rename(tmpFile, bindingFile);
51
+ }
52
+ async function saveAgentToken(tokenFile, token) {
53
+ await mkdir(path.dirname(tokenFile), { recursive: true });
54
+ await writeFile(tokenFile, token, { mode: 384 });
55
+ await chmod(tokenFile, 384);
56
+ }
57
+ async function readTokenRef(tokenRef) {
58
+ if (!tokenRef) return void 0;
59
+ if (tokenRef.startsWith("env:")) {
60
+ return process.env[tokenRef.slice("env:".length)] || void 0;
61
+ }
62
+ if (tokenRef.startsWith("file://")) {
63
+ const tokenPath = fileURLToPath(tokenRef);
64
+ return (await readFile(tokenPath, "utf8")).trim() || void 0;
65
+ }
66
+ return void 0;
67
+ }
68
+
69
+ // src/config.ts
70
+ import { z } from "zod";
71
+ var CoolclawConfigSchema = z.object({
72
+ gatewayUrl: z.string().url().optional().describe("Riddle gateway URL"),
73
+ agentId: z.string().optional().describe("Riddle agent ID"),
74
+ tokenSecretRef: z.string().optional().describe("Token secret reference (file:// or env:)"),
75
+ allowFrom: z.array(z.string()).optional().describe("Allowed sender IDs"),
76
+ dmPolicy: z.enum(["allowlist", "pairing", "open"]).optional().describe("DM access policy"),
77
+ enabled: z.boolean().optional().describe("Whether the account is enabled"),
78
+ name: z.string().optional().describe("Account display name")
79
+ });
80
+ function normalizeGatewayUrl(value) {
81
+ return value.trim().replace(/\/+$/, "");
82
+ }
83
+ function buildWsUrl(gatewayUrl, lastAckedSeq) {
84
+ const baseUrl = normalizeGatewayUrl(gatewayUrl).replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://");
85
+ return `${baseUrl}/ws/channel?lastAckedSeq=${lastAckedSeq}`;
86
+ }
87
+ async function resolveAccountToken(account) {
88
+ if (account.tokenSecret) return account.tokenSecret;
89
+ return readTokenRef(account.tokenSecretRef);
90
+ }
91
+
92
+ export {
93
+ defaultBindingFile,
94
+ defaultOpenClawConfigFile,
95
+ defaultTokenFile,
96
+ loadBinding,
97
+ touchBinding,
98
+ saveBinding,
99
+ saveAgentToken,
100
+ readTokenRef,
101
+ CoolclawConfigSchema,
102
+ normalizeGatewayUrl,
103
+ buildWsUrl,
104
+ resolveAccountToken
105
+ };
@@ -1,6 +1,7 @@
1
- import _default from './index.js';
2
- import './channel-C5YYO-tp.js';
1
+ import entry from './index.js';
2
+ import 'openclaw/plugin-sdk/channel-core';
3
+ import './types-y7-Cr6xf.js';
3
4
 
4
5
 
5
6
 
6
- export { _default as default };
7
+ export { entry as default };