Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] convert milliseconds to seconds. #331

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 49 additions & 46 deletions src/ai/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../types';
import { Path } from '../utils/path';
import { buildSetValueOp, buildSetTxBody, sendTx } from '../utils/transaction';
import { getAssistant, getValue } from '../utils/util';
import { getAssistant, getValue, isMillisecond, toSecond } from '../utils/util';
import {
validateAssistant,
validateObject,
Expand Down Expand Up @@ -203,14 +203,13 @@ export class Threads extends FactoryBase {
.history(address)
.thread(threadId)
.value();
const data = await this.ain.db.ref(threadPath).getValue();
const thread = {
id: data.id,
metadata: data.metadata || {},
createdAt: data.createdAt,
};
const thread = await this.ain.db.ref(threadPath).getValue();

return thread;
return {
id: thread.id,
createdAt: isMillisecond(thread.createdAt) ? toSecond(thread.createdAt) : thread.createdAt,
metadata: thread.metadata || {},
};
}

/**
Expand Down Expand Up @@ -307,44 +306,48 @@ export class Threads extends FactoryBase {

private async flattenThreads(objectId: string, tokens: any) {
const flatten: any = [];
await Promise.all(_.map(tokens, async (token, tokenId) => {
if (!token.ai) {
return;
}
const assistantId = token.ai.id;
const assistant = await getAssistant(this.ain, objectId, tokenId, assistantId);
const histories = token.ai.history;
if (typeof histories !== 'object' || histories === true) {
return;
}
_.forEach(histories, (history, address) => {
const threads = _.get(history, 'threads');
_.forEach(threads, (thread) => {
let updatedAt = thread.createdAt;
if (typeof thread.messages === 'object' && thread.messages !== null) {
const keys = Object.keys(thread.messages);
updatedAt = Number(keys[keys.length - 1]);
}
flatten.push({
id: thread.id,
metadata: thread.metadata || {},
createdAt: thread.createdAt,
updatedAt,
assistant: {
id: assistant.id,
createdAt: assistant.createdAt,
objectId,
tokenId,
tokenOwner: token.owner,
model: assistant.model,
name: assistant.name,
description: assistant.description || null,
instructions: assistant.instructions || null,
metadata: assistant.metadata || {},
metrics: assistant.metrics || {},
},
author: { address },
});
await Promise.all(
_.map(tokens, async (token, tokenId) => {
if (!token.ai) {
return;
}
const assistantId = token.ai.id;
const assistant = await getAssistant(this.ain, objectId, tokenId, assistantId);
const histories = token.ai.history;
if (typeof histories !== 'object' || histories === true) {
return;
}
_.forEach(histories, (history, address) => {
const threads = _.get(history, 'threads');
_.forEach(threads, (thread) => {
const createdAt = isMillisecond(thread.createdAt)
? toSecond(thread.createdAt)
: thread.createdAt;
let updatedAt = createdAt;
if (typeof thread.messages === 'object' && thread.messages !== null) {
const keys = Object.keys(thread.messages);
updatedAt = Number(keys[keys.length - 1]);
}
flatten.push({
id: thread.id,
metadata: thread.metadata || {},
createdAt: createdAt,
updatedAt,
assistant: {
id: assistant.id,
createdAt: assistant.createdAt,
objectId,
tokenId,
tokenOwner: token.owner,
model: assistant.model,
name: assistant.name,
description: assistant.description || null,
instructions: assistant.instructions || null,
metadata: assistant.metadata || {},
metrics: assistant.metrics || {},
},
author: { address },
});
});
});
})
Expand Down
16 changes: 15 additions & 1 deletion src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export const getAssistant = async (
if (!assistant) {
return null;
}
return { ...assistant, tokenOwner: token.owner };
return {
...assistant,
createdAt: isMillisecond(assistant.createdAt)
? toSecond(assistant.createdAt)
: assistant.createdAt,
tokenOwner: token.owner,
};
};

export const getToken = async (ain: Ain, objectId: string, tokenId: string) => {
Expand Down Expand Up @@ -99,3 +105,11 @@ export const arrayToObject = <T>(array: T[]): { [key: string]: T } => {
});
return result;
};

export const isMillisecond = (timestamp: number) => {
jiyoung-an marked this conversation as resolved.
Show resolved Hide resolved
return timestamp.toString().length === 13;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: maybe >= 13 would be better?

};

export const toSecond = (millisecond: number) => {
return Math.floor(millisecond / 1000);
};
Loading