@openfiles-ai/sdk
Version:
OpenFiles SDK - AI-native file storage for your AI agents
151 lines • 5.53 kB
TypeScript
/**
* @openfiles-ai/sdk/openai
*
* OpenAI client with OpenFiles tool integration
* Follows OpenAI 2025 best practices for tool calling with structured outputs
*/
import { OpenAI as OriginalOpenAI } from 'openai';
import type { ClientOptions as OriginalClientOptions } from 'openai';
type OriginalCreateParams = OriginalOpenAI.Chat.ChatCompletionCreateParams;
type ChatCompletion = OriginalOpenAI.Chat.ChatCompletion;
import { OpenFilesClient } from '../core';
import type { FileMetadata, FileVersionsResponse } from '../core/generated/types.gen';
type FileOperationResult = FileMetadata | string | {
files: FileMetadata[];
total: number;
} | {
versions: NonNullable<FileVersionsResponse['data']['versions']>;
total: number;
};
type ToolMessage = {
role: 'tool';
tool_call_id: string;
content: string;
};
export interface FileOperation {
action: string;
path?: string;
version?: number;
success: boolean;
error?: string;
data?: FileOperationResult;
}
export interface ToolExecution {
toolCallId: string;
function: string;
duration?: number;
success: boolean;
result?: FileOperationResult;
error?: string;
}
export interface ClientOptions extends OriginalClientOptions {
/** OpenFiles API key (required for file operations) */
openFilesApiKey: string;
/** Base URL for OpenFiles API (optional) */
openFilesBaseUrl?: string;
/** Base path prefix for all file operations (optional) */
basePath?: string;
onFileOperation?: (operation: FileOperation) => void;
onToolExecution?: (execution: ToolExecution) => void;
onError?: (error: Error) => void;
}
export type ChatCompletionCreateParams = OriginalCreateParams;
export type { ChatCompletion };
export type ChatCompletionMessage = OriginalOpenAI.Chat.ChatCompletionMessage;
export type ChatCompletionMessageParam = OriginalOpenAI.Chat.ChatCompletionMessageParam;
export type ChatCompletionTool = OriginalOpenAI.Chat.ChatCompletionTool;
export type ChatCompletionCreateParamsNonStreaming = OriginalOpenAI.Chat.ChatCompletionCreateParamsNonStreaming;
export type ChatCompletionCreateParamsStreaming = OriginalOpenAI.Chat.ChatCompletionCreateParamsStreaming;
/**
* Drop-in replacement for OpenAI client with automatic file operations
*
* Simply replace your OpenAI import and add openFilesApiKey to get
* automatic file operation capabilities with zero code changes.
*
* @example
* ```typescript
* // Before: import OpenAI from 'openai'
* // After: import OpenAI from '@openfiles-ai/sdk/openai'
*
* const ai = new OpenAI({
* apiKey: 'sk_your_openai_key', // Same as before
* openFilesApiKey: 'oa_your_key', // Add this
* basePath: 'projects/website', // Optional: organize files
* onFileOperation: (op) => console.log(`${op.action}: ${op.path}`) // Optional
* })
*
* // Create scoped clients for different areas
* const configAI = ai.withBasePath('config')
* const docsAI = ai.withBasePath('documentation')
*
* // Everything else works exactly the same!
* const response = await configAI.chat.completions.create({
* model: 'gpt-4',
* messages: [{ role: 'user', content: 'Generate app configuration file' }],
* tools: [...myCustomTools] // Your tools + OpenFiles tools (auto-injected)
* })
*
* // File operations happen automatically, response ready to use!
* console.log(response.choices[0].message.content)
* ```
*/
export declare class OpenAI extends OriginalOpenAI {
private artifacts;
private toolsInstance;
private config;
constructor(config: ClientOptions);
/**
* Create a new OpenAI client instance with a base path prefix
* All file operations will automatically prefix paths with the base path
*
* @param basePath - The base path to prefix to all operations
* @returns New OpenAI client instance with the specified base path
*
* @example
* ```typescript
* const ai = new OpenAI({ apiKey: 'sk_...', openFilesApiKey: 'oa_...' })
* const projectAI = ai.withBasePath('projects/website')
*
* // AI operations will create files under 'projects/website/'
* const response = await projectAI.chat.completions.create({
* model: 'gpt-4',
* messages: [{ role: 'user', content: 'Create config.json' }]
* })
* ```
*/
withBasePath(basePath: string): OpenAI;
/**
* Create enhanced method with proper typing for OpenAI API overloads
*/
private createEnhancedMethod;
/**
* Enhanced create method that auto-handles OpenFiles tools
* True drop-in replacement - user doesn't need to manage tool flow
*/
private enhancedCreate;
/**
* Execute OpenFiles tools from a completion response
* Returns tool messages that should be added to the conversation
*
* @param response - OpenAI completion response containing tool calls
* @returns Array of tool messages to add to conversation
*/
executeTools(response: ChatCompletion): Promise<ToolMessage[]>;
/**
* Generate descriptive message for tool operation result
*/
private getOperationMessage;
/**
* Get OpenFiles tool definitions for use in chat completions
*/
get tools(): {
definitions: import("..").ToolDefinition[];
};
/**
* Access to the underlying OpenFiles client
* For direct API calls when needed
*/
get openfiles(): OpenFilesClient;
}
export default OpenAI;
//# sourceMappingURL=client.d.ts.map