diff options
Diffstat (limited to 'packages/tools/src/openai.ts')
| -rw-r--r-- | packages/tools/src/openai.ts | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/packages/tools/src/openai.ts b/packages/tools/src/openai.ts index 5c79a9c1..197a5829 100644 --- a/packages/tools/src/openai.ts +++ b/packages/tools/src/openai.ts @@ -1,27 +1,27 @@ -import type OpenAI from "openai" -import Supermemory from "supermemory" +import type OpenAI from "openai"; +import Supermemory from "supermemory"; import { DEFAULT_VALUES, + getContainerTags, PARAMETER_DESCRIPTIONS, TOOL_DESCRIPTIONS, - getContainerTags, -} from "./shared" -import type { SupermemoryToolsConfig } from "./types" +} from "./shared"; +import type { SupermemoryToolsConfig } from "./types"; /** * Result types for memory operations */ export interface MemorySearchResult { - success: boolean - results?: Awaited<ReturnType<Supermemory["search"]["execute"]>>["results"] - count?: number - error?: string + success: boolean; + results?: Awaited<ReturnType<Supermemory["search"]["execute"]>>["results"]; + count?: number; + error?: string; } export interface MemoryAddResult { - success: boolean - memory?: Awaited<ReturnType<Supermemory["memories"]["add"]>> - error?: string + success: boolean; + memory?: Awaited<ReturnType<Supermemory["memories"]["add"]>>; + error?: string; } /** @@ -67,7 +67,7 @@ export const memoryToolSchemas = { required: ["memory"], }, } satisfies OpenAI.FunctionDefinition, -} as const +} as const; /** * Create a Supermemory client with configuration @@ -76,11 +76,11 @@ function createClient(apiKey: string, config?: SupermemoryToolsConfig) { const client = new Supermemory({ apiKey, ...(config?.baseUrl && { baseURL: config.baseUrl }), - }) + }); - const containerTags = getContainerTags(config) + const containerTags = getContainerTags(config); - return { client, containerTags } + return { client, containerTags }; } /** @@ -90,16 +90,16 @@ export function createSearchMemoriesFunction( apiKey: string, config?: SupermemoryToolsConfig, ) { - const { client, containerTags } = createClient(apiKey, config) + const { client, containerTags } = createClient(apiKey, config); return async function searchMemories({ informationToGet, includeFullDocs = DEFAULT_VALUES.includeFullDocs, limit = DEFAULT_VALUES.limit, }: { - informationToGet: string - includeFullDocs?: boolean - limit?: number + informationToGet: string; + includeFullDocs?: boolean; + limit?: number; }): Promise<MemorySearchResult> { try { const response = await client.search.execute({ @@ -108,20 +108,20 @@ export function createSearchMemoriesFunction( limit, chunkThreshold: DEFAULT_VALUES.chunkThreshold, includeFullDocs, - }) + }); return { success: true, results: response.results, count: response.results?.length || 0, - } + }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error", - } + }; } - } + }; } /** @@ -131,33 +131,33 @@ export function createAddMemoryFunction( apiKey: string, config?: SupermemoryToolsConfig, ) { - const { client, containerTags } = createClient(apiKey, config) + const { client, containerTags } = createClient(apiKey, config); return async function addMemory({ memory, }: { - memory: string + memory: string; }): Promise<MemoryAddResult> { try { - const metadata: Record<string, string | number | boolean> = {} + const metadata: Record<string, string | number | boolean> = {}; const response = await client.memories.add({ content: memory, containerTags, ...(Object.keys(metadata).length > 0 && { metadata }), - }) + }); return { success: true, memory: response, - } + }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error", - } + }; } - } + }; } /** @@ -167,13 +167,13 @@ export function supermemoryTools( apiKey: string, config?: SupermemoryToolsConfig, ) { - const searchMemories = createSearchMemoriesFunction(apiKey, config) - const addMemory = createAddMemoryFunction(apiKey, config) + const searchMemories = createSearchMemoriesFunction(apiKey, config); + const addMemory = createAddMemoryFunction(apiKey, config); return { searchMemories, addMemory, - } + }; } /** @@ -183,7 +183,7 @@ export function getToolDefinitions(): OpenAI.Chat.Completions.ChatCompletionTool return [ { type: "function", function: memoryToolSchemas.searchMemories }, { type: "function", function: memoryToolSchemas.addMemory }, - ] + ]; } /** @@ -193,26 +193,26 @@ export function createToolCallExecutor( apiKey: string, config?: SupermemoryToolsConfig, ) { - const tools = supermemoryTools(apiKey, config) + const tools = supermemoryTools(apiKey, config); return async function executeToolCall( toolCall: OpenAI.Chat.Completions.ChatCompletionMessageToolCall, ): Promise<string> { - const functionName = toolCall.function.name - const args = JSON.parse(toolCall.function.arguments) + const functionName = toolCall.function.name; + const args = JSON.parse(toolCall.function.arguments); switch (functionName) { case "searchMemories": - return JSON.stringify(await tools.searchMemories(args)) + return JSON.stringify(await tools.searchMemories(args)); case "addMemory": - return JSON.stringify(await tools.addMemory(args)) + return JSON.stringify(await tools.addMemory(args)); default: return JSON.stringify({ success: false, error: `Unknown function: ${functionName}`, - }) + }); } - } + }; } /** @@ -222,24 +222,24 @@ export function createToolCallsExecutor( apiKey: string, config?: SupermemoryToolsConfig, ) { - const executeToolCall = createToolCallExecutor(apiKey, config) + const executeToolCall = createToolCallExecutor(apiKey, config); return async function executeToolCalls( toolCalls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[], ): Promise<OpenAI.Chat.Completions.ChatCompletionToolMessageParam[]> { const results = await Promise.all( toolCalls.map(async (toolCall) => { - const result = await executeToolCall(toolCall) + const result = await executeToolCall(toolCall); return { tool_call_id: toolCall.id, role: "tool" as const, content: result, - } + }; }), - ) + ); - return results - } + return results; + }; } /** @@ -249,7 +249,7 @@ export function createSearchMemoriesTool( apiKey: string, config?: SupermemoryToolsConfig, ) { - const searchMemories = createSearchMemoriesFunction(apiKey, config) + const searchMemories = createSearchMemoriesFunction(apiKey, config); return { definition: { @@ -257,14 +257,14 @@ export function createSearchMemoriesTool( function: memoryToolSchemas.searchMemories, }, execute: searchMemories, - } + }; } export function createAddMemoryTool( apiKey: string, config?: SupermemoryToolsConfig, ) { - const addMemory = createAddMemoryFunction(apiKey, config) + const addMemory = createAddMemoryFunction(apiKey, config); return { definition: { @@ -272,5 +272,5 @@ export function createAddMemoryTool( function: memoryToolSchemas.addMemory, }, execute: addMemory, - } + }; } |