Skip to content

Commit 4e31c3f

Browse files
committed
fix type export error, rfk functions, add docs
1 parent f68fca8 commit 4e31c3f

File tree

9 files changed

+69
-125
lines changed

9 files changed

+69
-125
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ for (const validationRaw of validations) {
230230
}
231231
```
232232

233+
### Reading Task Events
234+
235+
If you want to fetch `taskId`s for a given protocol, you can filter contract events with:
236+
237+
```ts
238+
const events = await oracle.getTaskEvents({
239+
protocol: stringToBytes32("my-protocol/0.1.0"),
240+
status: TaskStatus.Completed,
241+
});
242+
```
243+
244+
You can then use other functions with the returned `taskId`s.
245+
233246
## Examples
234247

235248
We have several examples that demonstrate making a request, following it up with another request, and view an existing request!
@@ -312,4 +325,4 @@ pnpm test
312325

313326
## License
314327

315-
We are using MIT license.
328+
We are using [MIT](./LICENSE) license.

package.json

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{
22
"name": "dria-oracle-sdk",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "An on-chain LLM Oracle SDK for Dria",
55
"license": "MIT",
66
"author": "FirstBatch Team <[email protected]>",
77
"homepage": "https://github.com/firstbatchxyz/dria-oracle-sdk#readme",
88
"contributors": [
99
"Erhan Tezcan <[email protected]> (https://github.com/erhant)"
1010
],
11+
"scripts": {
12+
"dev": "tsx src/index.ts",
13+
"start": "pnpm build && node dist/index.cjs",
14+
"lint": "eslint '**/*.ts'",
15+
"build": "pkgroll",
16+
"test": "NODE_ENV=test jest "
17+
},
1118
"files": [
1219
"dist/",
1320
"LICENSE",
@@ -45,13 +52,6 @@
4552
"peerDependencies": {
4653
"@irys/sdk": "^0.2.11"
4754
},
48-
"scripts": {
49-
"dev": "tsx src/index.ts",
50-
"start": "pnpm build && node dist/index.cjs",
51-
"lint": "eslint '**/*.ts'",
52-
"build": "pkgroll",
53-
"test": "NODE_ENV=test jest"
54-
},
5555
"prettier": {
5656
"printWidth": 120
5757
},
@@ -63,5 +63,6 @@
6363
"arweave",
6464
"llm",
6565
"ai"
66-
]
66+
],
67+
"packageManager": "[email protected]+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
6768
}

src/client.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DecentralizedStorage } from "./storage";
44
import type {
55
ChatHistoryRequest,
66
ChatHistoryResponse,
7-
OracleModels,
7+
Models,
88
TaskRequestOptions,
99
NewRequestReturnType,
1010
TaskParameters,
@@ -50,7 +50,7 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
5050

5151
// defaults
5252
public taskParameters: TaskParameters = { difficulty: 2, numGenerations: 2, numValidations: 2 };
53-
public protocol = "dria-oracle-sdk/0.x.x";
53+
public protocol = "dria-oracle-sdk/0.0.x";
5454

5555
constructor(
5656
readonly client: {
@@ -158,15 +158,15 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
158158
* @param opts optional request arguments, such as `protocol` and `taskParameters`
159159
* @returns task transaction hash
160160
*/
161-
async request(input: string, models: OracleModels, opts?: TaskRequestOptions): Promise<NewRequestReturnType>;
161+
async request(input: string, models: Models, opts?: TaskRequestOptions): Promise<NewRequestReturnType>;
162162
async request(
163163
input: Prettify<ChatHistoryRequest>,
164-
models: OracleModels,
164+
models: Models,
165165
opts?: TaskRequestOptions
166166
): Promise<NewRequestReturnType>;
167167
async request(
168168
input: string | Prettify<ChatHistoryRequest>,
169-
models: OracleModels = "*",
169+
models: Models = "*",
170170
opts: TaskRequestOptions = {}
171171
): Promise<NewRequestReturnType> {
172172
if (this.coordinator === undefined) {
@@ -255,9 +255,9 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
255255
}
256256

257257
/**
258-
*
258+
* Returns the task request for a given task id.
259259
* @param taskId task id
260-
* @returns test request
260+
* @returns task request
261261
*/
262262
async getRequest(taskId: bigint | number): Promise<TaskRequest> {
263263
if (this.coordinator === undefined) {
@@ -361,16 +361,23 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
361361
}
362362

363363
/**
364-
* Fetches the events from the coordinator contract.
364+
* Fetches the task events from the coordinator contract and returns its args.
365+
* A returned task event has the taskId, protocol, statusBefore, and statusAfter.
366+
* For a completed task, we are looking for the statusAfter to be `Completed` which is `3`.
365367
*
366368
* @param opts options for fetching tasks
367369
* - `protocol`: protocol name
368370
* - `from`: block to start from
369371
* - `to`: block to end at
370-
*
371-
* @returns array of events, you can read the topic values for `i`th event with `events[i].args`
372+
* - `status`: task status to filter by
373+
* @returns array of task events
372374
*/
373-
async getEvents(opts: { protocol?: string; from?: bigint | BlockTag; to?: bigint | BlockTag }) {
375+
async getTaskEvents(opts: {
376+
protocol?: string;
377+
from?: bigint | BlockTag;
378+
to?: bigint | BlockTag;
379+
status?: TaskStatus;
380+
}) {
374381
if (this.coordinator === undefined) {
375382
throw new Error("SDK not initialized.");
376383
}
@@ -381,7 +388,13 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
381388
const toBlock = opts.to ?? "latest";
382389

383390
const events = await this.coordinator!.getEvents.StatusUpdate({ protocol }, { fromBlock, toBlock });
384-
return events;
391+
const taskEvents = events.map((event) => event.args);
392+
393+
if (opts.status) {
394+
return taskEvents.filter((e) => e.statusAfter === opts.status);
395+
} else {
396+
return taskEvents;
397+
}
385398
}
386399

387400
/**

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// sdk
22
export { Oracle } from "./client";
3-
export type { OracleModels } from "./types";
3+
export type { Models as OracleModels } from "./types";
44

55
// storage
66
export type { DecentralizedStorage } from "./storage";
77
export { ArweaveStorage, type ArweaveWallet } from "./storage/";
88

99
// utilities
1010
export { contractBytesToStringWithStorage, stringToContractBytesWithStorage } from "./utils";
11+
export { TaskStatus } from "./types";

src/types/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
export * from "./model";
21
export * from "./request";
32
export * from "./task";
3+
4+
/**
5+
* Allowed Oracle models.
6+
*
7+
* The requested model(s) can be any of the following:
8+
*
9+
* - An array of model names, such as `["gemini-1.5-pro", "gpt-4o-mini"]`.
10+
* - `*` for any model randomly (of the responder).
11+
* - `!` for the first model (of the responder).
12+
*
13+
* You can look at the available models from [this repository](https://github.com/andthattoo/ollama-workflows/blob/main/src/program/models.rs#L14).
14+
*/
15+
export type Models = string[] | "*" | "!";

src/types/model.ts

-96
This file was deleted.

src/types/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Hex } from "viem";
2-
import type { OracleModels } from "./model";
2+
import type { Models } from "./";
33
import type { TaskParameters, TaskStatus } from "./task";
44

55
/** A chat history entry. */
@@ -23,7 +23,7 @@ export type NewRequestReturnType = {
2323
txHash: Hex;
2424
protocol: string;
2525
input: string;
26-
models: OracleModels;
26+
models: Models;
2727
taskParameters: TaskParameters;
2828
};
2929

tests/events.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ describe("events", () => {
3636
const [from, to] = [24660077n, 24670077n]; // 10k blocks
3737

3838
// there are 14 tasks specifically for the purchase requests
39-
const purchaseTasks = await oracle.getEvents({ protocol: "swan-agent-purchase/0.1.0", from, to });
39+
const purchaseTasks = await oracle.getTaskEvents({ protocol: "swan-agent-purchase/0.1.0", from, to });
4040
expect(purchaseTasks.length).toBe(2);
4141

4242
// there are 84 tasks specifically for the state updates
43-
const stateTasks = await oracle.getEvents({ protocol: "swan-agent-state/0.1.0", from, to });
43+
const stateTasks = await oracle.getTaskEvents({ protocol: "swan-agent-state/0.1.0", from, to });
4444
expect(stateTasks.length).toBe(19);
4545
});
4646
});

tests/oracle.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { baseSepolia } from "viem/chains";
77
import { TaskStatus } from "../src/types";
88

99
// this test uses the live testnet, NOT a forked local node!
10-
describe("oracle", () => {
10+
describe.skip("oracle", () => {
1111
let oracle: Oracle<HttpTransport, typeof baseSepolia>;
1212
let requestTxHash: Hex;
1313

0 commit comments

Comments
 (0)