Skip to content

Commit

Permalink
Fix upload script
Browse files Browse the repository at this point in the history
  • Loading branch information
guillevalin committed Jan 28, 2025
1 parent 52c346e commit 7eeb753
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
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

0 comments on commit 7eeb753

Please sign in to comment.