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

Fix upload script #68

Merged
merged 1 commit into from
Jan 28, 2025
Merged
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
6 changes: 3 additions & 3 deletions scripts/build_tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export async function start() {
const tools_raw = await processToolsDirectory();
console.log(`Found ${tools_raw.length} tools to process`);

console.log("\nSaving tools to node and generating images...");
console.log("\nSaving tools to node and generating zip files...");
const tools_saved = await saveToolsInNode(tools_raw);
console.log(`Successfully processed ${tools_saved.length} tools`);

console.log("\nUploading tools...");
console.log("\nUploading tools to Shinkai Store...");
await uploadTools(tools_saved);
console.log("Tool processing complete!");
console.log("Tool uploading complete!");
}

if (import.meta.main) {
Expand Down
10 changes: 1 addition & 9 deletions scripts/build_tools/save_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from "https://deno.land/std/path/mod.ts";
import { exists } from "https://deno.land/std/fs/mod.ts";
import { encodeBase64 } from "jsr:@std/encoding/base64";
import { generateToolRouterKey, systemTools, stripVersion, author, uploadAsset } from "./system.ts";
import { getCategories, Category } from "./fetch_categories.ts";
import { getCategories } from "./fetch_categories.ts";

// deno-lint-ignore require-await
async function getToolType(file: string): Promise<string> {
Expand Down Expand Up @@ -188,7 +188,6 @@ export async function processToolsDirectory(): Promise<DirectoryEntry[]> {

// Validate tool has a category mapping
const routerKey = generateToolRouterKey(author, toolName);
console.log(`Debug - Tool: ${toolName}, Generated Router Key: ${routerKey}`);
const localEntry = toolCategories.find((tc: { routerKey: string; categoryId: string }) => tc.routerKey === routerKey);
if (!localEntry) {
throw new Error(`No category mapping found for tool ${toolName}. Please add a mapping in tool_categories.json.`);
Expand Down Expand Up @@ -370,13 +369,6 @@ export async function saveToolsInNode(toolsOriginal: DirectoryEntry[]): Promise<
// Check for .default file
tool.isDefault = await exists(join(tool.dir, ".default"));

// Upload tool assets to store
console.log(`Uploading tool assets for ${tool.name}...`);
tool.icon_url = await uploadAsset(tool.routerKey, join(tool.dir, "icon.png"), 'icon', `${tool.name}_icon.png`);
tool.banner_url = await uploadAsset(tool.routerKey, join(tool.dir, "banner.png"), 'banner', `${tool.name}_banner.png`);
tool.storeFile = await uploadAsset(tool.routerKey, zipPath, 'tool', `${tool.hash}.zip`);
console.log(`Tool assets for ${tool.name} uploaded`);

toolsSaved.push(tool);
}

Expand Down
35 changes: 20 additions & 15 deletions scripts/build_tools/upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from "https://deno.land/std/path/mod.ts";
import { DirectoryEntry } from "./interfaces.ts";

import { uploadAsset } from "./system.ts";
// Upload tools to Shinkai Store
export async function uploadTools(tools: DirectoryEntry[]) {
const store_addr = Deno.env.get("SHINKAI_STORE_ADDR");
Expand Down Expand Up @@ -31,20 +32,24 @@ export async function uploadTools(tools: DirectoryEntry[]) {
body: JSON.stringify(store_entry),
});

if (response.status === 409) {
const responseBody = await response.text();
if (responseBody.includes("already exists")) {
// Product exists, use PUT endpoint instead
const putResponse = await fetch(`${store_addr}/store/products/${entry.routerKey}`, {
method: "PUT",
headers: {
"Authorization": `Bearer ${store_token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(store_entry),
});
response = putResponse;
}
if (response.status === 409 || response.status === 200) {
// Upload tool assets to store
console.log(`Uploading tool assets for ${entry.name}...`);
store_entry.icon_url = await uploadAsset(entry.routerKey, join(entry.dir, "icon.png"), 'icon', `${entry.name}_icon.png`);
store_entry.banner_url = await uploadAsset(entry.routerKey, join(entry.dir, "banner.png"), 'banner', `${entry.name}_banner.png`);
store_entry.file = await uploadAsset(entry.routerKey, join("packages", `${entry.name}.zip`.toLowerCase().replace(/[^a-z0-9_.-]/g, '_')), 'tool', `${entry.hash}.zip`);
console.log(`Tool assets for ${entry.name} uploaded`);

// Product exists, use PUT endpoint instead
const putResponse = await fetch(`${store_addr}/store/products/${entry.routerKey}`, {
method: "PUT",
headers: {
"Authorization": `Bearer ${store_token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(store_entry),
});
response = putResponse;
}

if (response.status !== 200) {
Expand Down
Loading