Merge pull request #2 from arkhai-io/refactor

Refactor
This commit is contained in:
疒奀 2026-01-03 23:20:06 +08:00 committed by GitHub
commit 1774345c48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 23 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}`);
@ -218,21 +218,19 @@ async function main() {
obligationAbi,
attestation,
);
console.log(` Obligation: "${obligation[0].item}"`);
const obligationItem = obligation[0].item;
console.log(` Obligation: "${obligationItem}"`);
// Get demand data
const [, demand] = await client.getEscrowAndDemand(
llmClient.llm.LLMAbi,
attestation,
);
console.log(` Demand: "${demand[0].demand}"`);
console.log(` Model: ${demand[0].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(
demand[0],
obligation[0].item
nlaDemandData,
obligationItem
);
console.log(` ✨ Arbitration result: ${result ? "✅ APPROVED" : "❌ REJECTED"}`);

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;
},