Update listenAndArbitrate

This commit is contained in:
ngoc 2026-01-02 18:17:47 +07:00
parent 3c804bbb5b
commit 7b50984ee0
No known key found for this signature in database
GPG Key ID: 51FE6110113A5C32
4 changed files with 24 additions and 22 deletions

View File

@ -9,8 +9,8 @@
"@ai-sdk/openai": "^3.0.2",
"@viem/anvil": "^0.0.10",
"ai": "^6.0.5",
"arktype": "^2.1.23",
"alkahest-ts": "github:arkhai-io/alkahest",
"arktype": "^2.1.23",
"viem": "^2.42.1",
"zod": "^3.25.76",
},
@ -67,7 +67,7 @@
"ai": ["ai@6.0.5", "", { "dependencies": { "@ai-sdk/gateway": "3.0.4", "@ai-sdk/provider": "3.0.1", "@ai-sdk/provider-utils": "4.0.2", "@opentelemetry/api": "1.9.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-CKL3dDHedWskC6EY67LrULonZBU9vL+Bwa+xQEcprBhJfxpogntG3utjiAkYuy5ZQatyWk+SmWG8HLvcnhvbRg=="],
"alkahest-ts": ["alkahest-ts@github:arkhai-io/alkahest#5df4180", {}, "arkhai-io-alkahest-5df4180"],
"alkahest-ts": ["alkahest-ts@github:arkhai-io/alkahest#3c53bc9", {}, "arkhai-io-alkahest-3c53bc9"],
"arkregex": ["arkregex@0.0.5", "", { "dependencies": { "@ark/util": "0.56.0" } }, "sha512-ncYjBdLlh5/QnVsAA8De16Tc9EqmYM7y/WU9j+236KcyYNUXogpz3sC4ATIZYzzLxwI+0sEOaQLEmLmRleaEXw=="],

View File

@ -208,7 +208,7 @@ async function main() {
// Start listening and arbitrating
const { unwatch } = await client.arbiters.general.trustedOracle.listenAndArbitrate(
async (attestation: any) => {
async ({ attestation, demand }) => {
console.log(`\n📨 New arbitration request received!`);
console.log(` Attestation UID: ${attestation.uid}`);
@ -221,19 +221,15 @@ async function main() {
const obligationItem = obligation[0].item;
console.log(` Obligation: "${obligationItem}"`);
// Get demand data
const [, demand] = await client.getEscrowAndDemand(
llmClient.llm.LLMAbi,
attestation,
);
const demandData = demand[0];
console.log(` Demand: "${demandData.demand}"`);
console.log(` Model: ${demandData.arbitrationModel}`);
const trustedOracleDemandData = client.arbiters.general.trustedOracle.decodeDemand(demand);
const nlaDemandData = llmClient.llm.decodeDemand(trustedOracleDemandData.data);
console.log(` Demand: "${nlaDemandData.demand}"`);
console.log(` Model: ${nlaDemandData.arbitrationModel}`);
// Perform arbitration using LLM
console.log(` 🤔 Arbitrating with AI...`);
const result = await llmClient.llm.arbitrate(
demandData,
nlaDemandData,
obligationItem
);

6
nla.ts
View File

@ -81,6 +81,11 @@ export const makeLLMClient = (
);
};
const decodeDemand = (data: `0x${string}`): LLMDemand => {
const decoded = decodeAbiParameters(LLMAbi, data);
return decoded[0];
};
const arbitrate = async (demand: LLMDemand, obligation: string): Promise<boolean> => {
try {
const matchingProvider = providers.find(provider =>
@ -176,6 +181,7 @@ Answer ONLY with 'true' or 'false' - no explanations or additional text.`;
LLMAbi,
arbitrate,
encodeDemand,
decodeDemand,
addProvider,
getProvider,
providers,

View File

@ -1,5 +1,5 @@
import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
import { encodeAbiParameters, parseAbiParameters } from "viem";
import { decodeAbiParameters, encodeAbiParameters, parseAbiParameters } from "viem";
import { generateText } from "ai"
import { openai } from "@ai-sdk/openai"
@ -9,6 +9,7 @@ import {
type TestContext,
} from "alkahest-ts";
import { makeLLMClient } from "..";
import { de } from "zod/v4/locales";
let testContext: TestContext;
let charlieClient: ReturnType<typeof testContext.charlie.client.extend<{ llm: ReturnType<typeof makeLLMClient> }>>;
@ -67,18 +68,17 @@ Fulfillment: {{obligation}}`,
const obligationAbi = parseAbiParameters("(string item)");
const { decisions, unwatch } =
await testContext.bob.client.arbiters.general.trustedOracle.listenAndArbitrate(
async (attestation) => {
console.log("arbitrating");
const obligation = testContext.bob.client.extractObligationData(
async ({ attestation, demand }) => {
console.log("Arbitrating ", attestation, demand);
const obligation = charlieClient.extractObligationData(
obligationAbi,
attestation,
);
const [, demand] = await testContext.bob.client.getEscrowAndDemand(
charlieClient.llm.LLMAbi,
attestation,
);
const result = await charlieClient.llm.arbitrate(demand[0], obligation[0].item);
console.log("Obligation:", obligation);
const trustedOracleDemandData = testContext.bob.client.arbiters.general.trustedOracle.decodeDemand(demand);
const nlaDemandData = charlieClient.llm.decodeDemand(trustedOracleDemandData.data);
const result = await charlieClient.llm.arbitrate(nlaDemandData, obligation[0].item);
console.log("response", result);
return result;
},