// ask.mjs
import {
init,
getKeyring,
getGuardianAddress,
inferenceCompute,
getApi,
scanForBlockEvent,
fetchAndDecodeExtrinsic,
} from "@palliora.org/chainsdk";
init({ pallioraWs: "wss://staging.manas-rpc.palliora.org" });
const keyring = await getKeyring();
const account = keyring.addFromUri("//Bob"); // built-in test account, free to use here
const guardians = (await getGuardianAddress()).slice(0, 3).map((g) => g.address);
const result = await inferenceCompute(
{
input: JSON.stringify({
model: "gemma3:1b",
messages: [{ role: "user", content: process.argv[2] ?? "What is 2 + 2?" }],
stream: false,
}),
guardians,
fee: { amount: 0.05, computeRate: 0.00002 },
deadline: 0,
},
account,
);
const api = await getApi();
const match = await scanForBlockEvent(
api,
{
predicate: async (block, _event, phase) => {
if (!block || !phase.isApplyExtrinsic) return false;
const ext = block.block.extrinsics[phase.asApplyExtrinsic.toNumber()];
if (ext?.method?.section?.toLowerCase() !== "compute" || ext?.method?.method?.toLowerCase() !== "result") return false;
const emitted = "0x" + Array.from(ext.method.args[0]).map((b) => ("0" + (b & 0xff).toString(16)).slice(-2)).join("");
return emitted === result.agreementId;
},
},
result.blockNumber + 1,
300,
);
const { decoded } = await fetchAndDecodeExtrinsic(match.blockNumber, match.extrinsicIndex ?? 0);
const plainBytes = Buffer.from(decoded.method.args.contract.compute.input.Inline.data.slice(2), "hex");
console.log(new TextDecoder().decode(plainBytes));
process.exit(0);