@forbocai/core 0.5.4 → 0.5.7
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/dist/ans104-GLQVFKBA.mjs +224 -0
- package/dist/chunk-7P6ASYW6.mjs +9 -0
- package/dist/index.d.mts +96 -70
- package/dist/index.d.ts +96 -70
- package/dist/index.js +383 -89
- package/dist/index.mjs +149 -93
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-7P6ASYW6.mjs";
|
|
6
4
|
|
|
7
5
|
// src/agent.ts
|
|
8
6
|
var createInitialState = (partial) => {
|
|
@@ -14,17 +12,6 @@ var updateAgentState = (currentState, updates) => {
|
|
|
14
12
|
...updates
|
|
15
13
|
};
|
|
16
14
|
};
|
|
17
|
-
var processAgentInput = (currentState, input, context = {}) => {
|
|
18
|
-
const stateKeys = Object.keys(currentState);
|
|
19
|
-
const stateSummary = stateKeys.length > 0 ? stateKeys.map((k) => `${k}=${JSON.stringify(currentState[k])}`).join(", ") : "empty";
|
|
20
|
-
return {
|
|
21
|
-
dialogue: `Processing: "${input}" (State: ${stateSummary})`,
|
|
22
|
-
action: {
|
|
23
|
-
type: "respond",
|
|
24
|
-
reason: "Default processing"
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
15
|
var exportToSoul = (agentId, name, persona, state, memories) => {
|
|
29
16
|
return {
|
|
30
17
|
id: agentId,
|
|
@@ -37,10 +24,10 @@ var exportToSoul = (agentId, name, persona, state, memories) => {
|
|
|
37
24
|
};
|
|
38
25
|
var createAgent = (config) => {
|
|
39
26
|
let state = createInitialState(config.initialState);
|
|
40
|
-
let memories = [];
|
|
41
27
|
const cortex = config.cortex;
|
|
42
|
-
const apiUrl = config.apiUrl || "
|
|
28
|
+
const apiUrl = config.apiUrl || "https://api.forboc.ai";
|
|
43
29
|
const agentId = config.id || "agent-" + Math.random().toString(36).substring(7);
|
|
30
|
+
const authHeaders = config.apiKey ? { "Content-Type": "application/json", "Authorization": `Bearer ${config.apiKey}` } : { "Content-Type": "application/json" };
|
|
44
31
|
const getAgentState = () => {
|
|
45
32
|
return { ...state };
|
|
46
33
|
};
|
|
@@ -56,7 +43,7 @@ var createAgent = (config) => {
|
|
|
56
43
|
};
|
|
57
44
|
const dirRes = await fetch(`${apiUrl}/agents/${agentId}/directive`, {
|
|
58
45
|
method: "POST",
|
|
59
|
-
headers:
|
|
46
|
+
headers: authHeaders,
|
|
60
47
|
body: JSON.stringify(directiveBody)
|
|
61
48
|
});
|
|
62
49
|
if (!dirRes.ok) {
|
|
@@ -68,7 +55,8 @@ var createAgent = (config) => {
|
|
|
68
55
|
try {
|
|
69
56
|
const rawMemories = await config.memory.recall(
|
|
70
57
|
directiveData.memoryRecall.query,
|
|
71
|
-
directiveData.memoryRecall.limit
|
|
58
|
+
directiveData.memoryRecall.limit,
|
|
59
|
+
directiveData.memoryRecall.threshold
|
|
72
60
|
);
|
|
73
61
|
recalledMemories = rawMemories.map((m) => ({
|
|
74
62
|
text: m.text,
|
|
@@ -76,7 +64,8 @@ var createAgent = (config) => {
|
|
|
76
64
|
importance: m.importance,
|
|
77
65
|
similarity: void 0
|
|
78
66
|
}));
|
|
79
|
-
} catch {
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.warn("Memory recall failed, continuing with empty memories:", e);
|
|
80
69
|
}
|
|
81
70
|
}
|
|
82
71
|
const contextBody = {
|
|
@@ -86,7 +75,7 @@ var createAgent = (config) => {
|
|
|
86
75
|
};
|
|
87
76
|
const ctxRes = await fetch(`${apiUrl}/agents/${agentId}/context`, {
|
|
88
77
|
method: "POST",
|
|
89
|
-
headers:
|
|
78
|
+
headers: authHeaders,
|
|
90
79
|
body: JSON.stringify(contextBody)
|
|
91
80
|
});
|
|
92
81
|
if (!ctxRes.ok) {
|
|
@@ -105,7 +94,7 @@ var createAgent = (config) => {
|
|
|
105
94
|
};
|
|
106
95
|
const verRes = await fetch(`${apiUrl}/agents/${agentId}/verdict`, {
|
|
107
96
|
method: "POST",
|
|
108
|
-
headers:
|
|
97
|
+
headers: authHeaders,
|
|
109
98
|
body: JSON.stringify(verdictBody)
|
|
110
99
|
});
|
|
111
100
|
if (!verRes.ok) {
|
|
@@ -120,8 +109,7 @@ var createAgent = (config) => {
|
|
|
120
109
|
}
|
|
121
110
|
if (config.memory && typeof config.memory.store === "function" && verdictData.memoryStore) {
|
|
122
111
|
for (const instruction of verdictData.memoryStore) {
|
|
123
|
-
config.memory.store(instruction.text, instruction.type, instruction.importance).catch(() =>
|
|
124
|
-
});
|
|
112
|
+
config.memory.store(instruction.text, instruction.type, instruction.importance).catch((e) => console.warn("Memory store failed:", e));
|
|
125
113
|
}
|
|
126
114
|
}
|
|
127
115
|
if (verdictData.stateDelta && Object.keys(verdictData.stateDelta).length > 0) {
|
|
@@ -136,18 +124,43 @@ var createAgent = (config) => {
|
|
|
136
124
|
thought: generatedText
|
|
137
125
|
};
|
|
138
126
|
};
|
|
127
|
+
const speak = async (message, context = {}) => {
|
|
128
|
+
const currentState = getAgentState();
|
|
129
|
+
const speakBody = {
|
|
130
|
+
speakMessage: message,
|
|
131
|
+
speakContext: Object.keys(context).length > 0 ? context : void 0,
|
|
132
|
+
speakAgentState: currentState
|
|
133
|
+
};
|
|
134
|
+
const res = await fetch(`${apiUrl}/agents/${agentId}/speak`, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
headers: authHeaders,
|
|
137
|
+
body: JSON.stringify(speakBody)
|
|
138
|
+
});
|
|
139
|
+
if (!res.ok) {
|
|
140
|
+
throw new Error(`API Speak Error: ${res.status}`);
|
|
141
|
+
}
|
|
142
|
+
const data = await res.json();
|
|
143
|
+
if (data.speakHistory) {
|
|
144
|
+
state = updateAgentState(state, { conversationHistory: data.speakHistory });
|
|
145
|
+
}
|
|
146
|
+
return data.speakReply;
|
|
147
|
+
};
|
|
148
|
+
const reply = speak;
|
|
139
149
|
const exportSoul2 = async () => {
|
|
140
|
-
let exportedMemories = [
|
|
150
|
+
let exportedMemories = [];
|
|
141
151
|
if (config.memory && typeof config.memory.export === "function") {
|
|
142
152
|
try {
|
|
143
153
|
exportedMemories = await config.memory.export();
|
|
144
|
-
} catch {
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.warn("Memory export failed, exporting with empty memories:", e);
|
|
145
156
|
}
|
|
146
157
|
}
|
|
147
158
|
return exportToSoul(agentId, "Agent", config.persona, state, exportedMemories);
|
|
148
159
|
};
|
|
149
160
|
return {
|
|
150
161
|
process,
|
|
162
|
+
speak,
|
|
163
|
+
reply,
|
|
151
164
|
getState: getAgentState,
|
|
152
165
|
setState: setAgentState,
|
|
153
166
|
export: exportSoul2
|
|
@@ -164,7 +177,8 @@ var fromSoul = async (soul, cortex, memory) => {
|
|
|
164
177
|
if (memory && soul.memories && soul.memories.length > 0 && typeof memory.import === "function") {
|
|
165
178
|
try {
|
|
166
179
|
await memory.import(soul.memories);
|
|
167
|
-
} catch {
|
|
180
|
+
} catch (e) {
|
|
181
|
+
console.warn("Memory hydration failed, continuing without memories:", e);
|
|
168
182
|
}
|
|
169
183
|
}
|
|
170
184
|
return agent;
|
|
@@ -218,10 +232,6 @@ var createBridge = (config = {}) => {
|
|
|
218
232
|
for (const rule of applicableRules) {
|
|
219
233
|
const result = rule.validate(currentAction, context);
|
|
220
234
|
if (!result.valid) {
|
|
221
|
-
if (effectiveConfig.apiUrl) {
|
|
222
|
-
const apiResult = await validateRemote(action);
|
|
223
|
-
console.debug("API validation result:", apiResult);
|
|
224
|
-
}
|
|
225
235
|
return result;
|
|
226
236
|
}
|
|
227
237
|
if (result.correctedAction) {
|
|
@@ -235,8 +245,6 @@ var createBridge = (config = {}) => {
|
|
|
235
245
|
};
|
|
236
246
|
const registerRule = (rule) => {
|
|
237
247
|
rules.set(rule.id, rule);
|
|
238
|
-
if (effectiveConfig.apiUrl) {
|
|
239
|
-
}
|
|
240
248
|
};
|
|
241
249
|
const listRules = () => {
|
|
242
250
|
return Array.from(rules.values());
|
|
@@ -264,7 +272,6 @@ var validateAction = (action, rules, context = {}) => {
|
|
|
264
272
|
};
|
|
265
273
|
|
|
266
274
|
// src/soul.ts
|
|
267
|
-
import Irys from "@irys/sdk";
|
|
268
275
|
var createSoul = (id, name, persona, state, memories = []) => {
|
|
269
276
|
return {
|
|
270
277
|
id,
|
|
@@ -294,63 +301,69 @@ var deserializeSoul = (json) => {
|
|
|
294
301
|
};
|
|
295
302
|
};
|
|
296
303
|
var uploadToArweave = async (soul, config) => {
|
|
297
|
-
|
|
298
|
-
|
|
304
|
+
const wallet = config.walletJwk || config.privateKey;
|
|
305
|
+
if (!wallet) {
|
|
306
|
+
throw new Error("Arweave wallet JWK required for direct upload");
|
|
299
307
|
}
|
|
300
|
-
|
|
301
|
-
url: config.irysUrl || "https://node1.irys.xyz",
|
|
302
|
-
token: config.token || "solana",
|
|
303
|
-
key: config.privateKey
|
|
304
|
-
});
|
|
305
|
-
const dataToUpload = serializeSoul(soul);
|
|
308
|
+
let createArweaveDataItem;
|
|
306
309
|
try {
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
txId: receipt.id,
|
|
310
|
-
url: `https://gateway.irys.xyz/${receipt.id}`,
|
|
311
|
-
soul
|
|
312
|
-
};
|
|
310
|
+
const imported = await import("./ans104-GLQVFKBA.mjs");
|
|
311
|
+
createArweaveDataItem = imported.createArweaveDataItem;
|
|
313
312
|
} catch (e) {
|
|
314
|
-
throw new Error(
|
|
313
|
+
throw new Error("Missing ANS-104 Arweave module. Rebuild the SDK to enable direct uploads.");
|
|
315
314
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
315
|
+
const jwk = typeof wallet === "string" ? JSON.parse(wallet) : wallet;
|
|
316
|
+
const dataToUpload = serializeSoul(soul);
|
|
317
|
+
const tags = [
|
|
318
|
+
{ name: "Content-Type", value: "application/json" },
|
|
319
|
+
{ name: "App-Name", value: "ForbocAI" },
|
|
320
|
+
{ name: "App-Version", value: "soul-1" }
|
|
321
|
+
];
|
|
322
|
+
const dataItem = await createArweaveDataItem(dataToUpload, jwk, { tags });
|
|
323
|
+
const rawData = dataItem.raw;
|
|
324
|
+
if (!rawData) {
|
|
325
|
+
throw new Error("Failed to build ANS-104 data item");
|
|
324
326
|
}
|
|
325
|
-
const
|
|
327
|
+
const bundlerUrl = config.bundlerUrl || "https://upload.ardrive.io/v1/tx";
|
|
328
|
+
const gatewayUrl = config.gatewayUrl || "https://arweave.net";
|
|
326
329
|
try {
|
|
327
|
-
const response = await fetch(
|
|
330
|
+
const response = await fetch(bundlerUrl, {
|
|
328
331
|
method: "POST",
|
|
329
|
-
headers: { "Content-Type": "application/
|
|
330
|
-
body:
|
|
331
|
-
// Sending full soul for server to sign/upload
|
|
332
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
333
|
+
body: rawData
|
|
332
334
|
});
|
|
333
335
|
if (!response.ok) {
|
|
334
|
-
|
|
336
|
+
const message = await response.text().catch(() => response.statusText);
|
|
337
|
+
throw new Error(message || response.statusText);
|
|
338
|
+
}
|
|
339
|
+
const responseText = await response.text();
|
|
340
|
+
let responseJson = null;
|
|
341
|
+
try {
|
|
342
|
+
responseJson = JSON.parse(responseText);
|
|
343
|
+
} catch {
|
|
344
|
+
responseJson = null;
|
|
345
|
+
}
|
|
346
|
+
const txId = responseJson?.id || responseJson?.dataItemId || responseJson?.txId || (typeof responseText === "string" && responseText.trim().length > 0 ? responseText.trim() : null) || dataItem.id;
|
|
347
|
+
if (!txId) {
|
|
348
|
+
throw new Error("Bundler response did not include a data item id");
|
|
335
349
|
}
|
|
336
|
-
const data = await response.json();
|
|
337
350
|
return {
|
|
338
|
-
txId
|
|
339
|
-
url:
|
|
351
|
+
txId,
|
|
352
|
+
url: `${gatewayUrl}/${txId}`,
|
|
340
353
|
soul
|
|
341
354
|
};
|
|
342
355
|
} catch (e) {
|
|
343
|
-
|
|
344
|
-
const mockTxId = `mock_ar_${Date.now()}`;
|
|
345
|
-
return {
|
|
346
|
-
txId: mockTxId,
|
|
347
|
-
url: `https://arweave.net/${mockTxId}`,
|
|
348
|
-
soul
|
|
349
|
-
};
|
|
356
|
+
throw new Error(`Failed to upload to Arweave: ${e}`);
|
|
350
357
|
}
|
|
351
358
|
};
|
|
359
|
+
var exportSoul = async (_agentId, soul, config = {}) => {
|
|
360
|
+
if (!config.walletJwk && !config.privateKey) {
|
|
361
|
+
throw new Error("walletJwk required for Arweave ANS-104 export");
|
|
362
|
+
}
|
|
363
|
+
return uploadToArweave(soul, config);
|
|
364
|
+
};
|
|
352
365
|
var importSoulFromArweave = async (txId, config = {}) => {
|
|
353
|
-
const gateway = config.gatewayUrl || "https://
|
|
366
|
+
const gateway = config.gatewayUrl || "https://arweave.net";
|
|
354
367
|
try {
|
|
355
368
|
const response = await fetch(`${gateway}/${txId}`, {
|
|
356
369
|
method: "GET",
|
|
@@ -374,25 +387,22 @@ var getSoulList = async (limit = 50, apiUrl) => {
|
|
|
374
387
|
const response = await fetch(`${url}/souls?limit=${limit}`);
|
|
375
388
|
if (!response.ok) throw new Error(response.statusText);
|
|
376
389
|
const data = await response.json();
|
|
377
|
-
return data.souls.map((s) =>
|
|
378
|
-
txId
|
|
379
|
-
name
|
|
380
|
-
agentId
|
|
381
|
-
exportedAt
|
|
382
|
-
|
|
383
|
-
|
|
390
|
+
return data.souls.map((s) => {
|
|
391
|
+
const txId = s.txId || s.soulEntryTxId || s.cid;
|
|
392
|
+
const name = s.name || s.soulEntryName;
|
|
393
|
+
const agentId = s.agentId || s.soulEntryAgentId;
|
|
394
|
+
const exportedAt = s.exportedAt || s.soulEntryExportedAt;
|
|
395
|
+
const url2 = s.url || s.soulEntryArweaveUrl || `https://arweave.net/${txId}`;
|
|
396
|
+
return { txId, name, agentId, exportedAt, url: url2 };
|
|
397
|
+
});
|
|
384
398
|
} catch (e) {
|
|
385
399
|
return [];
|
|
386
400
|
}
|
|
387
401
|
};
|
|
388
|
-
var createSoulInstance = (id, name, persona, state, memories = []
|
|
402
|
+
var createSoulInstance = (id, name, persona, state, memories = []) => {
|
|
389
403
|
const soulData = createSoul(id, name, persona, state, memories);
|
|
390
|
-
const defaultApiUrl = initialApiUrl || "https://api.forboc.ai";
|
|
391
404
|
const performExport = async (config) => {
|
|
392
|
-
return exportSoul(soulData.id, soulData, {
|
|
393
|
-
...config,
|
|
394
|
-
apiUrl: config?.apiUrl || defaultApiUrl
|
|
395
|
-
});
|
|
405
|
+
return exportSoul(soulData.id, soulData, { ...config });
|
|
396
406
|
};
|
|
397
407
|
return {
|
|
398
408
|
export: performExport,
|
|
@@ -604,6 +614,9 @@ var createGhost = (config) => {
|
|
|
604
614
|
return getGhostResults(sessionId, apiUrl);
|
|
605
615
|
};
|
|
606
616
|
const stop = async () => {
|
|
617
|
+
if (sessionId) {
|
|
618
|
+
await stopGhostSession(sessionId, apiUrl);
|
|
619
|
+
}
|
|
607
620
|
sessionId = null;
|
|
608
621
|
};
|
|
609
622
|
const waitForCompletion = async (pollIntervalMs, timeoutMs, onProgress) => {
|
|
@@ -628,7 +641,11 @@ var createGhost = (config) => {
|
|
|
628
641
|
};
|
|
629
642
|
|
|
630
643
|
// src/cortex-remote.ts
|
|
631
|
-
var createRemoteCortex = (apiUrl) => {
|
|
644
|
+
var createRemoteCortex = (apiUrl, cortexId = "local", apiKey) => {
|
|
645
|
+
const authHeaders = {
|
|
646
|
+
"Content-Type": "application/json",
|
|
647
|
+
...apiKey ? { "Authorization": `Bearer ${apiKey}` } : {}
|
|
648
|
+
};
|
|
632
649
|
const init = async () => ({
|
|
633
650
|
id: `remote_${Date.now()}`,
|
|
634
651
|
model: "api-integrated",
|
|
@@ -636,9 +653,9 @@ var createRemoteCortex = (apiUrl) => {
|
|
|
636
653
|
engine: "remote"
|
|
637
654
|
});
|
|
638
655
|
const complete = async (prompt, options) => {
|
|
639
|
-
const response = await fetch(`${apiUrl}/cortex/complete`, {
|
|
656
|
+
const response = await fetch(`${apiUrl}/cortex/${cortexId}/complete`, {
|
|
640
657
|
method: "POST",
|
|
641
|
-
headers:
|
|
658
|
+
headers: authHeaders,
|
|
642
659
|
body: JSON.stringify({ prompt, ...options })
|
|
643
660
|
});
|
|
644
661
|
if (!response.ok) throw new Error(`Remote Cortex failed: ${response.statusText}`);
|
|
@@ -652,6 +669,42 @@ var createRemoteCortex = (apiUrl) => {
|
|
|
652
669
|
return { init, complete, completeStream };
|
|
653
670
|
};
|
|
654
671
|
|
|
672
|
+
// src/stream.ts
|
|
673
|
+
async function streamToCallback(stream, onChunk) {
|
|
674
|
+
for await (const chunk of stream) {
|
|
675
|
+
onChunk(chunk);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
async function streamToString(stream) {
|
|
679
|
+
let fullText = "";
|
|
680
|
+
for await (const chunk of stream) {
|
|
681
|
+
fullText += chunk;
|
|
682
|
+
}
|
|
683
|
+
return fullText;
|
|
684
|
+
}
|
|
685
|
+
async function streamFromCortex(cortex, prompt, onChunk, options) {
|
|
686
|
+
const stream = cortex.completeStream(prompt, options);
|
|
687
|
+
let fullText = "";
|
|
688
|
+
for await (const chunk of stream) {
|
|
689
|
+
fullText += chunk;
|
|
690
|
+
onChunk(chunk);
|
|
691
|
+
}
|
|
692
|
+
return fullText;
|
|
693
|
+
}
|
|
694
|
+
async function streamFromCortexWithDelay(cortex, prompt, onChunk, options) {
|
|
695
|
+
const { delayMs = 30, ...completionOptions } = options ?? {};
|
|
696
|
+
const stream = cortex.completeStream(prompt, completionOptions);
|
|
697
|
+
let fullText = "";
|
|
698
|
+
for await (const chunk of stream) {
|
|
699
|
+
fullText += chunk;
|
|
700
|
+
onChunk(chunk);
|
|
701
|
+
if (delayMs > 0) {
|
|
702
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return fullText;
|
|
706
|
+
}
|
|
707
|
+
|
|
655
708
|
// src/presets/index.ts
|
|
656
709
|
var presets_exports = {};
|
|
657
710
|
__export(presets_exports, {
|
|
@@ -859,7 +912,7 @@ var socialRules = [speakRule, interactRule];
|
|
|
859
912
|
var puzzleRules = [movementRule, interactRule];
|
|
860
913
|
|
|
861
914
|
// src/index.ts
|
|
862
|
-
var SDK_VERSION = "0.5.
|
|
915
|
+
var SDK_VERSION = "0.5.7";
|
|
863
916
|
export {
|
|
864
917
|
SDK_VERSION,
|
|
865
918
|
createAgent,
|
|
@@ -879,10 +932,13 @@ export {
|
|
|
879
932
|
getSoulList,
|
|
880
933
|
importSoulFromArweave,
|
|
881
934
|
presets_exports as presets,
|
|
882
|
-
processAgentInput,
|
|
883
935
|
serializeSoul,
|
|
884
936
|
startGhostSession,
|
|
885
937
|
stopGhostSession,
|
|
938
|
+
streamFromCortex,
|
|
939
|
+
streamFromCortexWithDelay,
|
|
940
|
+
streamToCallback,
|
|
941
|
+
streamToString,
|
|
886
942
|
updateAgentState,
|
|
887
943
|
uploadToArweave,
|
|
888
944
|
validateAction,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forbocai/core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"description": "Core agnostic logic for ForbocAI SDK",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"test": "vitest"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"
|
|
15
|
+
"arweave": "^1.15.7",
|
|
16
|
+
"base64url": "^3.0.1",
|
|
16
17
|
"axios": "^1.6.2",
|
|
17
18
|
"zod": "^3.22.4"
|
|
18
19
|
},
|