/** An object tracked by Steamship */
type IsSteamshipModel = {
    id: string;
    createdAt: string;
    updatedAt?: string;
};
/** An object contained within a workspace */
type IsWorkspaceContained = {
    workspaceId: string;
};
/** An object owned by a user */
type IsUserOwned = {
    userId: string;
};
/** An object owned by a file */
type IsFileOwned = {
    fileId: string;
};
/** Has Handle */
type HasHandle = {
    handle: string;
};

/**
 * Tag
 *
 * A complex key-valued object that annotates a block or file.
 *
 */
type Tag = IsSteamshipModel & IsWorkspaceContained & IsUserOwned & IsFileOwned & {
    blockId?: string;
    startIdx?: number;
    endIdx?: number;
    kind: string;
    name?: string;
    value?: Record<string, any>;
    text?: string;
};

/**
 * Block
 *
 * Blocks store the content of a file.
 *
 *  Content of a block can either be in the `text` field or stored remotely in S3.
 *  Metadata of a block is added in an unordered list of Tags.
 */
type Block = IsSteamshipModel & IsWorkspaceContained & IsUserOwned & IsFileOwned & {
    text?: string;
    tags?: Tag[];
    contentUrl?: string;
    mimeType?: string;
    url?: string;
    index: number;
    publicData: boolean;
};
interface IBlockClient {
    raw(params: {
        id: string;
    }): Promise<Response>;
    get(params: {
        id: string;
    }): Promise<Block>;
}

type ServerSentEvent<T> = {
    id: string;
    event: string;
    data: T;
};
type BlockCreatedPayload = {
    blockId: string;
    createdAt: string;
};
interface FileEvent extends ServerSentEvent<BlockCreatedPayload> {
    id: string;
    event: "blockCreated";
    data: BlockCreatedPayload;
}

/**
 * File
 *
 * Files are one of the core unit of data representation in Steamship.
 *
 * Videos, PDFs, ChatHistories, Text Blobs, and JSON objects are all Files.
 *
 * Content of a file is arranged in an ordered list of Blocks.
 * Metadata of a file is added in an unordered list of Tags.
 */
type File = IsSteamshipModel & IsWorkspaceContained & IsUserOwned & HasHandle & {
    tags: Tag[];
    blocks: Block[];
    mimeType?: string;
    filename?: string;
    publicData?: boolean;
};
interface IFileClient {
    stream(params: {
        id: string;
    }): Promise<ReadableStream<FileEvent>>;
    raw(params: {
        id: string;
    }): Promise<Response>;
    get(params: {
        id: string;
    }): Promise<File>;
}

/**
 * Task
 *
 * All work in Steamship can be expressed as an async Task running on the server. This enables scheduling
 * and chaining of user & agent workflows.
 */
type Task = IsWorkspaceContained & IsUserOwned & {
    taskId: string;
    requestId?: string;
    input?: string;
    output?: string;
    state: "waiting" | "running" | "suceeded" | "failed";
    statusMessage?: string;
    statusSuggestion?: string;
    statusCode?: string;
    statusCreatedOn?: string;
    taskType?: "internalApi" | "train" | "infer";
    taskExecutor?: string;
    taskCreatedOn?: string;
    taskLastModifiedOn?: string;
    remoteStatusInput?: string;
    remoteStatusOutput?: string;
    remoteStatusMessage?: string;
    assignedWorker?: string;
    startedAt?: string;
    maxRetries?: number;
    retries?: number;
};

/**
 * Workspace
 *
 * An isolated data container in the cloud.
 */
type Workspace = IsSteamshipModel & IsUserOwned & HasHandle & {};
interface IWorkspaceClient {
    create(params: {
        handle?: string;
        fetch_if_exists?: boolean;
    }): Promise<Workspace>;
}

/**
 * Parameters for creating an instance of an agent.
 *
 */

type CreatePackageInstanceParams = {
    /**
    * Handle of the agent you are creating an instance of. This is a remote service deployed to Steamship.
    */
    package: string;
    /**
     * Optional Version of the agent you are creating.
     */
    version?: string;
    /**
     * Optional handle identifying this particular agent instance. This uniquely identifies the data and chat history
     * that will be maintained by the instance.
     */
    handle?: string;
    /**
     * Optional configuration to pass to the agent upon construction.
     */
    config?: Record<string, any>;
    /**
     * Fetch if exists
     */
    fetchIfExists?: boolean;
};

/**
 * Steamship Package Instance.
 *
 * Package Instances are copies of a Package or Agent running in the cloud.
 *
 * They maintain their own task queue, set of web endpoints, chat histories, and data.
 */
type PackageInstance = IsSteamshipModel & IsWorkspaceContained & IsUserOwned & HasHandle & {
    /**
     * The invocation URL base of the package.
     */
    invocationUrl: string;
    /**
     * The handle of the package tied to this instance.
     */
    packageHandle: string;
    /**
     * The ID of the package tied to this instance.
     */
    packageId: string;
    /**
     * The handle of the package version tied to this instance.
     */
    packageVersionHandle: string;
    /**
     * The ID of the package version tied to this instance.
     */
    packageVersionId: string;
    /**
     * The ID of the workspace this instance resides within.
     */
    workspaceId: string;
};
interface IPackageClient {
    createInstance(params: CreatePackageInstanceParams): Promise<PackageInstance>;
    invoke(params: {
        base_url: string;
        method: string;
        payload?: Record<string, any>;
        verb?: "GET" | "POST";
    }): Promise<Response>;
}

/**
 * Agent Instance.
 *
 * Agent Instances are instances of AI agents running in the cloud.
 *
 */
type AgentInstance = PackageInstance;
type StreamingResponse = {
    task: Task;
    file: File;
};
type AgentRespondParams = {
    /**
     * The base URL of the running agent instance.
     */
    url: string;
    /**
     * Input for the agent.
     */
    input: {
        /**
         * The new user message to respond to.
         */
        prompt: string;
        /**
         * The ID of the conversation context for conversation history retrieval.
         */
        context_id: string;
    };
};
type AgentPostGetParams = {
    /**
     * The base URL of the running agent instance.
     */
    url: string;
    /**
     * The method to invoke
     */
    path: string;
    /**
     * Input for the agent.
     */
    arguments: Record<string, any>;
};

interface IAgentClient {
    post(params: AgentPostGetParams): Promise<Response>;
    respond(params: AgentRespondParams): Promise<Block[]>;
    respondAsync(params: AgentRespondParams): Promise<StreamingResponse>;
}

type Configuration = {
    apiBase?: string;
    appBase?: string;
    apiKey?: string;
    workspace?: string;
    workspaceId?: string;
};
type RequestOptions = {
    workspace?: string;
    workspaceId?: string;
    verb?: "GET" | "POST";
    body?: any;
    baseUrl?: string;
};
/**
 * Interface for a Steamship client.
 */
interface Client {
    url(path: string): string;
    get(path: string, options?: RequestOptions): Promise<Response>;
    post(path: string, payload: any, options?: RequestOptions): Promise<Response>;
    invokePackageMethod(url_base: string, path: string, payload: any, options?: RequestOptions): Promise<Response>;
    eventStream<T>(path: string, options?: RequestOptions): Promise<ReadableStream<T>>;
    switchWorkspace({ workspace, workspaceId }: {
        workspace?: string;
        workspaceId?: string;
    }): Client;
    package: IPackageClient;
    workspace: IWorkspaceClient;
    agent: IAgentClient;
    block: IBlockClient;
    file: IFileClient;
}

declare abstract class ClientBase implements Client {
    package: IPackageClient;
    workspace: IWorkspaceClient;
    agent: IAgentClient;
    block: IBlockClient;
    file: IFileClient;
    constructor();
    abstract url(path: string): string;
    abstract get(path: string, options?: RequestOptions): Promise<Response>;
    abstract post(path: string, payload: any, options?: RequestOptions): Promise<Response>;
    abstract invokePackageMethod(url_base: string, path: string, payload: any, options?: RequestOptions): Promise<Response>;
    abstract eventStream<T>(path: string, options?: RequestOptions): Promise<ReadableStream<T>>;
    abstract switchWorkspace({ workspace, workspaceId }: {
        workspace?: string;
        workspaceId?: string;
    }): Client;
}

/**
 * Steamship API client.
 *
 * Intended for use consuming Steamship's API.
 */
declare class Steamship extends ClientBase implements Client {
    config: Configuration;
    /**
     * Create a new Steamship API client.
     *
     * @param config
     */
    constructor(config: Configuration);
    private workspaceHandleFromBaseUrl;
    private makeHeaders;
    /**
     * Return the fully-specified URL for an API path.
     *
     * @param path
     */
    url(path: string): string;
    /**
     * Invoke an API method on a Steamship package.
     *
     * @param apiBase
     * @param path API Path rooted in apiBase provided in the configuration object.
     * @param opts Javascript `fetch` options. API Key and Content-Type are auto-applied.
     */
    invokePackageMethod(apiBase: string, path: string, opts?: any): Promise<Response>;
    /**
     * Invoke an API method on Steamship.
     *
     * @param path API Path rooted in apiBase provided in the configuration object.
     * @param opts Javascript `fetch` options. API Key and Content-Type are auto-applied.
     */
    invokeApi(path: string, opts: any): Promise<Response>;
    /**
     * Perform a `get` against the Steamship API.
     * @param path API Path rooted in apiBase provided in the configuration object.
     */
    get(path: string): Promise<Response>;
    /**
     * Perform a 'post' against the Steamship API
     * @param path API Path rooted in apiBase provided in the configuration object.
     * @param payload Payload, as a JSON object, to be provided as JSON to Steamship.
     */
    post(path: string, payload: any): Promise<Response>;
    switchWorkspace({ workspace, workspaceId }: {
        workspace?: string;
        workspaceId?: string;
    }): Client;
    eventStream<T>(path: string, opts: any): Promise<ReadableStream<T>>;
}

declare function BlockStreamToMarkdownStream(client: Client): TransformStream<Block, string>;

declare function FileEventStreamToBlockStream(client: Client): TransformStream<FileEvent, Block>;

type SteamshipStreamOptions = {
    streamTimeoutSeconds?: number;
    tagKindFilter?: string;
    tagNameFilter?: string;
    minDatetime?: string;
};
/**
 * Stream a Steamship Agent response as multi-modal Markdown.
 *
 * Steamship Agents act like LLMs, but they are stateful, remembering your conversation history.
 *
 * @example
 * const response = await steamship.agent.respond({
 *  url: "https://username.steamship.com/workspace-name/agent-name",
 *  input: {
 *    prompt: messages.join('\n'),
 *    context_id: "my-chat-session-id"
 *  },
 * })
 *
 * const stream = await SteamshipStream(response)
 * return new StreamingTextResponse(stream)
 *
 */
declare function SteamshipMarkdownStream(res: StreamingResponse, client: Client, opts?: SteamshipStreamOptions): Promise<ReadableStream>;

export { AgentInstance, Block, BlockCreatedPayload, BlockStreamToMarkdownStream, Client, File, FileEvent, FileEventStreamToBlockStream, HasHandle, IsSteamshipModel, IsUserOwned, IsWorkspaceContained, PackageInstance, ServerSentEvent, Steamship, SteamshipMarkdownStream, SteamshipStreamOptions, Tag, Task, Workspace, Steamship as default };
