Skip to content

Commit

Permalink
chore: refactor utils, revert assistant ID
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Jan 6, 2025
1 parent f603dc3 commit 9d6cdc1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/app-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"accessibility": {
"keyboardShortcut": "ctrl+?"
},
"assistantId": "asst_v9QCHtVvhFqI5lmLjIZnSQUG",
"assistantId": "asst_xmAX5oxByssXrkBymMbcsVEm",
"dimensions": {
"height": 680,
"width": 380
Expand Down
4 changes: 2 additions & 2 deletions src/models/assistant-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { OpenAI } from "openai";
import { Message } from "openai/resources/beta/threads/messages";
import { codapInterface } from "@concord-consortium/codap-plugin-api";
import { DAVAI_SPEAKER, DEBUG_SPEAKER } from "../constants";
import { formatJsonMessage } from "../utils/utils";
import { convertBase64ToImage, requestThreadDeletion } from "../utils/openai-utils";
import { convertBase64ToImage, formatJsonMessage } from "../utils/utils";
import { requestThreadDeletion } from "../utils/openai-utils";
import { ChatTranscriptModel } from "./chat-transcript-model";

const OpenAIType = types.custom({
Expand Down
80 changes: 0 additions & 80 deletions src/utils/openai-utils.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,3 @@
import { AssistantTool } from "openai/resources/beta/assistants";

export const openAiTools: AssistantTool[] = [
{
type: "function",
function: {
name: "create_request",
description: "Create a request to get data from CODAP",
strict: false,
parameters: {
type: "object",
properties: {
action: {
type: "string",
description: "The action to perform"
},
resource: {
type: "string",
description: "The resource to act upon"
},
values: {
type: "object",
description: "The values to pass to the action"
}
},
additionalProperties: false,
required: [
"action",
"resource"
]
}
}
},
{
type: "function",
function: {
name: "convert_base64_to_image",
description: "Convert a base64 image to a file object",
strict: false,
parameters: {
type: "object",
properties: {
base64Data: {
type: "string",
description: "The base64 image data"
},
filename: {
type: "string",
description: "The filename to use for the image"
}
},
additionalProperties: false,
required: [
"base64Data"
]
}
}
}
];

export const requestThreadDeletion = async (threadId: string): Promise<Response> => {
const response = await fetch(`${process.env.REACT_APP_OPENAI_BASE_URL}threads/${threadId}`, {
method: "DELETE",
Expand All @@ -70,23 +10,3 @@ export const requestThreadDeletion = async (threadId: string): Promise<Response>

return response;
};

export async function convertBase64ToImage(base64Data: string, filename = "image.png") {
try {
const mimeType = base64Data.match(/data:(.*?);base64/)?.[1] || "image/png";
const base64 = base64Data.split(",")[1];
const binary = atob(base64);
const binaryLength = binary.length;
const arrayBuffer = new Uint8Array(binaryLength);
for (let i = 0; i < binaryLength; i++) {
arrayBuffer[i] = binary.charCodeAt(i);
}

const blob = new Blob([arrayBuffer], { type: mimeType });
const file = new File([blob], filename, { type: mimeType });
return file;
} catch (error) {
console.error("Error converting base64 to image:", error);
throw error;
}
}
20 changes: 20 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,23 @@ export const isShortcutPressed = (pressedKeys: Set<string>, shortcutKeys: string
});
});
};

export const convertBase64ToImage = async (base64Data: string, filename = "image.png") => {
try {
const mimeType = base64Data.match(/data:(.*?);base64/)?.[1] || "image/png";
const base64 = base64Data.split(",")[1];
const binary = atob(base64);
const binaryLength = binary.length;
const arrayBuffer = new Uint8Array(binaryLength);
for (let i = 0; i < binaryLength; i++) {
arrayBuffer[i] = binary.charCodeAt(i);
}

const blob = new Blob([arrayBuffer], { type: mimeType });
const file = new File([blob], filename, { type: mimeType });
return file;
} catch (error) {
console.error("Error converting base64 to image:", error);
throw error;
}
};

0 comments on commit 9d6cdc1

Please sign in to comment.