Skip to content

Add missing timestamps to agents #11

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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
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
28 changes: 16 additions & 12 deletions app/api/plugins/[pluginId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SwaggerParser from "@apidevtools/swagger-parser";
import {
getPlugin,
Prisma,
Agent,
type Prisma,
type Agent,
prismaClient,
getAgent,
} from "@bitte-ai/data";
Expand Down Expand Up @@ -143,6 +143,7 @@ export const POST = withUnkey(
(tool) => ({
...tool,
function: tool.function as unknown as Prisma.InputJsonValue,
createdAt: new Date(),
})
);
} catch (e: unknown) {
Expand Down Expand Up @@ -200,12 +201,13 @@ export const POST = withUnkey(
createdAt: new Date(),
updatedAt: null,
};
const pluginData = transformPlugin(pluginId, spec);

// 13. Prepare and Execute Batch Write
try {
await prismaClient.$transaction([
prismaClient.plugin.create({
data: transformPlugin(pluginId, spec),
data: { ...pluginData, createdAt: new Date() },
}),
prismaClient.agent.create({ data: agent }),
prismaClient.tool.createMany({ data: pluginTools }),
Expand Down Expand Up @@ -304,6 +306,7 @@ export const PUT = withUnkey(
}).map((tool) => ({
...tool,
function: tool.function as unknown as Prisma.InputJsonValue,
createdAt: new Date(),
}));

const assistantDefinition =
Expand Down Expand Up @@ -340,13 +343,14 @@ export const PUT = withUnkey(
.map((t) => t.type) || [],
updatedAt: new Date(),
};
const pluginData = transformPlugin(pluginId, plugin);

try {
// TODO: update using data package functions?
await prismaClient.$transaction([
prismaClient.plugin.update({
where: { id: pluginId },
data: transformPlugin(pluginId, plugin),
data: { ...pluginData, updatedAt: new Date() },
}),
prismaClient.agent.update({ where: { id: pluginId }, data: agent }),
prismaClient.tool.deleteMany({
Expand Down Expand Up @@ -543,7 +547,8 @@ const sanitizeSpec = async (
return sanitizedArray.map((item, index) =>
Array.isArray(item) ? { [`item_${index}`]: item } : item
);
} else if (typeof value === "object" && value !== null) {
}
if (typeof value === "object" && value !== null) {
// Handle objects
const sanitizedObj: Record<string, JSONValue> = {};
for (const [key, val] of Object.entries(value)) {
Expand All @@ -553,12 +558,11 @@ const sanitizeSpec = async (
}
}
return Object.keys(sanitizedObj).length > 0 ? sanitizedObj : null;
} else {
// Handle primitive values
return value === "" || value === null || value === undefined
? null
: value;
}
// Handle primitive values
return value === "" || value === null || value === undefined
? null
: value;
};

return sanitize(validatedSpec);
Expand All @@ -578,11 +582,11 @@ const transformPlugin = (id: string, spec: BitteOpenAPISpec) => {
extra: {},
};

Object.entries(spec).forEach(([k, v]) => {
for (const [k, v] of Object.entries(spec)) {
if (!Object.keys(transformed).includes(k)) {
(transformed.extra as Record<string, unknown>)[k] = v;
}
});
}

return transformed;
};
Expand Down