Skip to content
New issue

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

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

Already on GitHub? # to your account

SQL migration part 1 #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": false
}
19 changes: 9 additions & 10 deletions app/api/agents/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Agent } from "@/lib/types";
import { NextRequest, NextResponse } from "next/server";
import { write, queryAgents } from "@/lib/firestore";
import { COLLECTIONS } from "@/lib/constants";
import { queryAgents } from "@/lib/firestore";
import { kv } from "@vercel/kv";
import { listAiAgents, createAiAgent, ai_agent } from "@bitte-ai/data";

export async function GET(request: NextRequest) {
try {
Expand All @@ -13,13 +12,17 @@ export async function GET(request: NextRequest) {
const verifiedOnly = searchParams.get("verifiedOnly") !== "false";
const category = searchParams.get("category") || undefined;

const agents = await queryAgents<Agent>({
// TODO: remove firestore after migration
const firestoreAgents = await queryAgents({
verified: verifiedOnly,
chainIds,
offset,
limit,
category: category === "" ? undefined : category,
});
// FIXME: wait for version that is camel case
const sqlAgents = await listAiAgents();
const agents = [...firestoreAgents, ...sqlAgents];

const agentIds = agents.map((agent) => agent.id);
const pingsByAgent = await getTotalPingsByAgentIds(agentIds);
Expand Down Expand Up @@ -47,7 +50,7 @@ export async function POST(request: NextRequest) {
try {
const body = await request.json();

const newAgent: Agent = {
const newAgent: ai_agent = {
...body,
verified: false,
id: crypto.randomUUID(),
Expand All @@ -73,11 +76,7 @@ export async function POST(request: NextRequest) {
);
}

const result = await write(COLLECTIONS.AGENTS, newAgent.id, newAgent);

if (!result.success) {
throw result.error;
}
await createAiAgent(newAgent);

return NextResponse.json(newAgent, { status: 201 });
} catch (error) {
Expand Down
81 changes: 47 additions & 34 deletions lib/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,32 @@ export const catchDocumentNotFound = (err: Error): null => {
throw err;
};

export const queryAgents = async <T>(options: {
verified?: boolean;
withTools?: boolean;
chainIds?: string[];
offset?: number;
limit?: number;
category?: string | null;
} = {}): Promise<T[]> => {
export const queryAgents = async (
options: {
verified?: boolean;
withTools?: boolean;
chainIds?: string[];
offset?: number;
limit?: number;
category?: string | null;
} = {}
): Promise<Agent[]> => {
let query: FirebaseFirestore.Query = db.collection(COLLECTIONS.AGENTS);

if (options.verified) {
query = query.where('verified', '==', true);
query = query.where("verified", "==", true);
}

if (options.chainIds?.length) {
query = query.where('chainIds', 'array-contains-any', options.chainIds.map(id => parseInt(id)));
query = query.where(
"chainIds",
"array-contains-any",
options.chainIds.map((id) => parseInt(id))
);
}

if (options.category) {
query = query.where('category', '==', options.category);
query = query.where("category", "==", options.category);
}

if (options.limit) {
Expand All @@ -166,59 +172,66 @@ export const queryAgents = async <T>(options: {
}

const snapshot = await query.get();
const agents = snapshot.docs.map(doc => doc.data());
const agents = snapshot.docs.map((doc) => doc.data());

if (!options.withTools) {
return agents.map(agent => {
return agents.map((agent) => {
const { ...rest } = agent;
return rest as T;
return rest as Agent;
});
}
return agents as T[];

return agents as Agent[];
};

export const queryTools = async <T>(options: {
verified?: boolean;
functionName?: string;
offset?: number;
chainId?: string;
} = {}): Promise<T[]> => {
let query: FirebaseFirestore.Query = db.collection(COLLECTIONS.AGENTS)
.select('tools', 'image', 'chainIds');

export const queryTools = async <T>(
options: {
verified?: boolean;
functionName?: string;
offset?: number;
chainId?: string;
} = {}
): Promise<T[]> => {
let query: FirebaseFirestore.Query = db
.collection(COLLECTIONS.AGENTS)
.select("tools", "image", "chainIds");

if (options.verified) {
query = query.where('verified', '==', true);
query = query.where("verified", "==", true);
}

if (options.chainId) {
query = query.where('chainIds', 'array-contains', options.chainId);
query = query.where("chainIds", "array-contains", options.chainId);
}

const limit = 100;
query = query.limit(limit);

const snapshot = await query.get();
const tools: Tool[] = [];

for (const doc of snapshot.docs) {
const agent = doc.data();
if (!agent.tools?.length) continue;

const baseToolData = {
image: agent.image,
chainIds: agent.chainIds || []
chainIds: agent.chainIds || [],
};

for (const tool of agent.tools) {
if (options.functionName &&
!tool.function.name.toLowerCase().includes(options.functionName.toLowerCase())) {
if (
options.functionName &&
!tool.function.name
.toLowerCase()
.includes(options.functionName.toLowerCase())
) {
continue;
}

tools.push({
...tool,
...baseToolData
...baseToolData,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@ai-sdk/openai": "^1.1.11",
"@bitte-ai/data": "0.1.0-fix-cicd2-460c6c4",
"@google-cloud/firestore": "^7.11.0",
"@jest/globals": "^29.7.0",
"@radix-ui/react-dialog": "^1.1.6",
Expand Down
33 changes: 31 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.