claito-connect 1.0.1 → 1.0.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/lib/index.js +33 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -10,13 +10,23 @@ const HEARTBEAT_FILENAME = 'HEARTBEAT.md';
|
|
|
10
10
|
/**
|
|
11
11
|
* Fetch content from URL
|
|
12
12
|
*/
|
|
13
|
-
function fetchUrl(url) {
|
|
13
|
+
function fetchUrl(url, redirectsLeft = 5) {
|
|
14
14
|
return new Promise((resolve, reject) => {
|
|
15
15
|
const client = url.startsWith('https') ? https : http;
|
|
16
16
|
|
|
17
17
|
client.get(url, (res) => {
|
|
18
|
-
if (
|
|
19
|
-
|
|
18
|
+
if ([301, 302, 307, 308].includes(res.statusCode)) {
|
|
19
|
+
if (redirectsLeft <= 0) {
|
|
20
|
+
reject(new Error(`Too many redirects: ${url}`));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const location = res.headers.location;
|
|
24
|
+
if (!location) {
|
|
25
|
+
reject(new Error(`Redirect with no location: ${url}`));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const nextUrl = new URL(location, url).toString();
|
|
29
|
+
return fetchUrl(nextUrl, redirectsLeft - 1).then(resolve).catch(reject);
|
|
20
30
|
}
|
|
21
31
|
|
|
22
32
|
if (res.statusCode !== 200) {
|
|
@@ -35,7 +45,7 @@ function fetchUrl(url) {
|
|
|
35
45
|
/**
|
|
36
46
|
* POST JSON to URL
|
|
37
47
|
*/
|
|
38
|
-
function postJson(url, body) {
|
|
48
|
+
function postJson(url, body, redirectsLeft = 5) {
|
|
39
49
|
return new Promise((resolve, reject) => {
|
|
40
50
|
const urlObj = new URL(url);
|
|
41
51
|
const client = urlObj.protocol === 'https:' ? https : http;
|
|
@@ -54,6 +64,21 @@ function postJson(url, body) {
|
|
|
54
64
|
};
|
|
55
65
|
|
|
56
66
|
const req = client.request(options, (res) => {
|
|
67
|
+
if ([301, 302, 307, 308].includes(res.statusCode)) {
|
|
68
|
+
if (redirectsLeft <= 0) {
|
|
69
|
+
reject(new Error(`Too many redirects: ${url}`));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const location = res.headers.location;
|
|
73
|
+
if (!location) {
|
|
74
|
+
reject(new Error(`Redirect with no location: ${url}`));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const nextUrl = new URL(location, url).toString();
|
|
78
|
+
postJson(nextUrl, body, redirectsLeft - 1).then(resolve).catch(reject);
|
|
79
|
+
res.resume();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
57
82
|
let responseData = '';
|
|
58
83
|
res.on('data', chunk => responseData += chunk);
|
|
59
84
|
res.on('end', () => {
|
|
@@ -104,12 +129,14 @@ async function connect(options) {
|
|
|
104
129
|
ensureDir(skillDir);
|
|
105
130
|
fs.writeFileSync(path.join(skillDir, SKILL_FILENAME), skillContent);
|
|
106
131
|
console.log(` ✓ Saved to ${skillDir}/${SKILL_FILENAME}`);
|
|
132
|
+
console.log(` ▶ Action: Read and learn this SKILL.md before interacting.`);
|
|
107
133
|
} catch (err) {
|
|
108
134
|
console.log(` ⚠ Could not download (${err.message}), creating minimal version...`);
|
|
109
135
|
ensureDir(skillDir);
|
|
110
136
|
const minimalSkill = createMinimalSkill(baseUrl, name, track);
|
|
111
137
|
fs.writeFileSync(path.join(skillDir, SKILL_FILENAME), minimalSkill);
|
|
112
138
|
console.log(` ✓ Created minimal ${skillDir}/${SKILL_FILENAME}`);
|
|
139
|
+
console.log(` ▶ Action: Read and learn this SKILL.md before interacting.`);
|
|
113
140
|
}
|
|
114
141
|
|
|
115
142
|
// Step 2: Download HEARTBEAT.md
|
|
@@ -118,11 +145,13 @@ async function connect(options) {
|
|
|
118
145
|
const heartbeatContent = await fetchUrl(`${baseUrl}/heartbeat.md`);
|
|
119
146
|
fs.writeFileSync(path.join(skillDir, HEARTBEAT_FILENAME), heartbeatContent);
|
|
120
147
|
console.log(` ✓ Saved to ${skillDir}/${HEARTBEAT_FILENAME}`);
|
|
148
|
+
console.log(` ▶ Action: Register and run this HEARTBEAT routine.`);
|
|
121
149
|
} catch (err) {
|
|
122
150
|
console.log(` ⚠ Could not download (${err.message}), creating minimal version...`);
|
|
123
151
|
const minimalHeartbeat = createMinimalHeartbeat(baseUrl);
|
|
124
152
|
fs.writeFileSync(path.join(skillDir, HEARTBEAT_FILENAME), minimalHeartbeat);
|
|
125
153
|
console.log(` ✓ Created minimal ${skillDir}/${HEARTBEAT_FILENAME}`);
|
|
154
|
+
console.log(` ▶ Action: Register and run this HEARTBEAT routine.`);
|
|
126
155
|
}
|
|
127
156
|
|
|
128
157
|
// Step 3: Register agent
|