001/*
002 * Copyright (c) 2026, Oracle and/or its affiliates.
003 *
004 * Licensed under the Universal Permissive License Version 1.0 as shown at
005 * https://oss.oracle.com/licenses/upl/
006 */
007package com.oracle.database.selectai;
008
009import com.oracle.database.selectai.impl.SelectAIFactory;
010import com.oracle.database.selectai.model.ConversationAttributes;
011import com.oracle.database.selectai.model.CredentialConfig;
012import com.oracle.database.selectai.model.DbConnectionConfig;
013import com.oracle.database.selectai.model.ProfileAttributes;
014import com.oracle.database.selectai.model.ProfileStatus;
015import com.oracle.database.selectai.model.SelectAIException;
016import com.oracle.database.selectai.model.SelectAIOptions;
017import com.oracle.database.selectai.model.VectorIndexConfig;
018
019import javax.sql.DataSource;
020import java.sql.Connection;
021import java.util.List;
022
023/**
024 * Top-level API contract for Oracle Select AI operations.
025 * <p>
026 * Select AI lets applications use natural-language prompts for SQL generation,
027 * SQL execution, SQL explanation, chat, summarization, synthetic data
028 * generation, and retrieval augmented generation (RAG) with vector indexes.
029 * This interface is the SDK entry point for creating or opening SDK objects for
030 * the database resources that support those features: credentials, profiles,
031 * conversations, and vector indexes.
032 * <p>
033 * {@code SelectAI} extends {@link AutoCloseable}. A {@code SelectAI} client may
034 * own SDK-managed JDBC resources depending on how it is created.
035 * <p>
036 * For clients created with {@link #create(DbConnectionConfig)}, the SDK
037 * creates and owns a single JDBC connection. Call {@link #close()} when the
038 * client is no longer needed to release that connection and its database
039 * session.
040 * <p>
041 * For clients created with {@link #create(DataSource)}, the application owns
042 * the {@code DataSource}. The SDK obtains a connection from the
043 * {@code DataSource} for each operation and closes that operation connection
044 * after use. Calling {@link #close()} does not close the caller-owned
045 * {@code DataSource}.
046 *
047 * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-about.html">
048 *      About Select AI</a>
049 * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/dbms-cloud-ai-package.html">
050 *      DBMS_CLOUD_AI package reference</a>
051 */
052public interface SelectAI extends AutoCloseable {
053    /**
054     * Creates a SelectAI client from database connection configuration.
055     * <p>
056     * This is the recommended public entry point for applications that want the
057     * SDK to create and use a single JDBC connection from {@link DbConnectionConfig}.
058     * For a complete runnable sample source, see
059     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/profile/ListProfilesSample.html">
060     * ListProfilesSample source</a>.
061     *
062     * @param dbConnectionConfig database connection configuration
063     * @return SelectAI client
064     * @throws IllegalArgumentException when {@code dbConnectionConfig} is null
065     * @throws SelectAIException when the database connection cannot be initialized
066     */
067    static SelectAI create(DbConnectionConfig dbConnectionConfig) throws SelectAIException {
068        return SelectAIFactory.create(dbConnectionConfig);
069    }
070
071    /**
072     * Creates a SelectAI client from database connection configuration and SDK
073     * execution options.
074     * <p>
075     * For a complete runnable sample source, see
076     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/CreateSelectAIWithOptionsSample.html">
077     * CreateSelectAIWithOptionsSample source</a>.
078     *
079     * @param dbConnectionConfig database connection configuration
080     * @param options SDK execution options; {@code null} uses {@link SelectAIOptions#defaults()}
081     * @return SelectAI client
082     * @throws IllegalArgumentException when {@code dbConnectionConfig} is null
083     * @throws SelectAIException when the database connection cannot be initialized
084     */
085    static SelectAI create(DbConnectionConfig dbConnectionConfig, SelectAIOptions options)
086            throws SelectAIException {
087        return SelectAIFactory.create(dbConnectionConfig, options);
088    }
089
090    /**
091     * Creates a SelectAI client backed by a {@link DataSource}.
092     * <p>
093     * This is the recommended public entry point for applications that manage
094     * JDBC connections through a DataSource or connection pool. Each SDK
095     * operation obtains a connection from the DataSource and closes it when the
096     * operation completes.
097     * For a complete runnable sample source, see
098     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/datasource/ListProfilesWithDataSourceSample.html">
099     * ListProfilesWithDataSourceSample source</a>.
100     *
101     * @param dataSource DataSource used to obtain JDBC connections
102     * @return SelectAI client
103     * @throws IllegalArgumentException when {@code dataSource} is null
104     */
105    static SelectAI create(DataSource dataSource) {
106        return SelectAIFactory.create(dataSource);
107    }
108
109    /**
110     * Creates a SelectAI client backed by a {@link DataSource} and SDK execution
111     * options.
112     * <p>
113     * For a complete runnable sample source, see
114     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/CreateSelectAIDataSourceWithOptionsSample.html">
115     * CreateSelectAIDataSourceWithOptionsSample source</a>.
116     *
117     * @param dataSource DataSource used to obtain JDBC connections
118     * @param options SDK execution options; {@code null} uses {@link SelectAIOptions#defaults()}
119     * @return SelectAI client
120     * @throws IllegalArgumentException when {@code dataSource} is null
121     */
122    static SelectAI create(DataSource dataSource, SelectAIOptions options) {
123        return SelectAIFactory.create(dataSource, options);
124    }
125
126    /**
127     * Releases resources owned by this SelectAI client.
128     * <p>
129     * For clients created with {@link #create(DbConnectionConfig)}, this closes
130     * the SDK-owned JDBC connection. For clients created with
131     * {@link #create(DataSource)}, this does not close the caller-owned
132     * DataSource; individual operation connections are already closed after
133     * each operation.
134     * For a complete runnable sample source, see
135     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/CloseSelectAISample.html">
136     * CloseSelectAISample source</a>.
137     *
138     * @throws SelectAIException when an owned JDBC connection cannot be closed
139     */
140    @Override
141    void close() throws SelectAIException;
142
143    /**
144     * Returns the JDBC connection retained by this SelectAI client.
145     * <p>
146     * This method is available only for clients created with
147     * {@link #create(DbConnectionConfig)}, where the SDK owns and reuses one
148     * JDBC connection. Use it when application code needs to execute custom SQL
149     * or PL/SQL in the same database session used by SDK operations.
150     * <p>
151     * For clients created with {@link #create(DataSource)}, there is no single
152     * retained connection. In that mode, this method throws
153     * {@link IllegalStateException}; callers should obtain custom JDBC
154     * connections from their own DataSource directly.
155     * <p>
156     * The returned connection is owned by this SelectAI client. Close the
157     * SelectAI client when finished; closing the returned connection directly
158     * also closes the SDK connection and can make later SDK operations fail.
159     * Do not use this method to share one SDK-owned JDBC connection across
160     * multiple application threads.
161     * For a complete runnable sample source, see
162     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/GetConnectionSample.html">
163     * GetConnectionSample source</a>.
164     *
165     * @return retained JDBC connection for DbConnectionConfig mode
166     * @throws IllegalStateException when this client is DataSource-backed
167     */
168    Connection getConnection();
169
170    /**
171     * Creates a {@link Credential} instance from database credential details.
172     * <p>
173     * A credential is a database object that stores the secret material needed
174     * by Autonomous Database to call an AI provider or object storage service.
175     * The returned object can create or drop that credential in the current
176     * schema.
177     * For a complete runnable sample source, see
178     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/CreateCredentialObjectSample.html">
179     * CreateCredentialObjectSample source</a>.
180     *
181     * @param credentialConfig credential definition used for create/drop operations
182     * @return credential object initialized with the supplied credential details
183     * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-manage-profiles.html">
184     *      Select AI prerequisites and credentials</a>
185     */
186    Credential credential(CredentialConfig credentialConfig);
187
188    /**
189     * Opens a {@link Profile} object for an existing AI profile in the current schema.
190     * <p>
191     * An AI profile stores the provider and AI model configuration and database object
192     * metadata that Select AI uses for natural-language SQL, chat, and RAG.
193     * "Existing" is not limited to profiles created by this Java SDK; profiles
194     * created through PL/SQL, Python, SQL tools, or another application are
195     * available when they exist in the same schema and privileges allow access.
196     * The returned object can run prompts, inspect metadata, update
197     * attributes, enable/disable the profile, or drop it.
198     * For a complete runnable sample source, see
199     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/OpenProfileSample.html">
200     * OpenProfileSample source</a>.
201     *
202     * @param profileName profile name
203     * @return profile object bound to the specified database profile
204     * @throws SelectAIException when the profile cannot be loaded
205     * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-manage-profiles.html">
206     *      Manage AI profiles</a>
207     */
208    Profile profile(String profileName) throws SelectAIException;
209
210    /**
211     * Creates a {@link Profile} object initialized with profile creation
212     * details.
213     * <p>
214     * This call does not create the profile in the database. Invoke
215     * {@link Profile#create()} on the returned object to persist the profile.
216     * Passing {@code null} for status keeps the database default behavior
217     * during creation.
218     * For a complete runnable sample source, see
219     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/CreateProfileWithSelectAISample.html">
220     * CreateProfileWithSelectAISample source</a>.
221     *
222     * @param profileName profile name
223     * @param profileAttributes profile attributes payload
224     * @param description profile description
225     * @param status optional initial profile status
226     * @return profile object initialized with the supplied create details
227     * @throws SelectAIException when the profile object cannot be initialized
228     * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-manage-profiles.html">
229     *      Create and set an AI profile</a>
230     */
231    Profile profile(String profileName, ProfileAttributes profileAttributes,
232                    String description, ProfileStatus status)
233            throws SelectAIException;
234
235    /**
236     * Lists AI profiles visible in the current schema.
237     * <p>
238     * Listing profiles is a collection-level operation, so it is exposed on the
239     * SelectAI client instead of requiring an unbound {@link Profile} object.
240     * Each returned object can inspect profile metadata, run prompts, update
241     * attributes, enable/disable the profile, or drop the profile.
242     * For a complete runnable sample source, see
243     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/profile/ListProfilesSample.html">
244     * ListProfilesSample source</a>.
245     *
246     * @return list of profile objects hydrated from database metadata
247     * @throws SelectAIException when listing profiles fails
248     */
249    List<Profile> listProfiles() throws SelectAIException;
250
251    /**
252     * Lists AI profiles visible in the current schema whose names match the
253     * supplied database regular-expression pattern.
254     * <p>
255     * Listing profiles is a collection-level operation, so it is exposed on the
256     * SelectAI client. The pattern is evaluated by the database metadata query.
257     * Use {@link #profile(String)} when opening one literal profile name.
258     * For a complete runnable sample source, see
259     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/profile/ListProfilesByPatternSample.html">
260     * ListProfilesByPatternSample source</a>.
261     *
262     * @param profileNamePattern regular-expression pattern matched against profile names
263     * @return list of matching profile objects hydrated from database metadata
264     * @throws IllegalArgumentException when {@code profileNamePattern} is null or blank
265     * @throws SelectAIException when listing profiles fails
266     */
267    List<Profile> listProfiles(String profileNamePattern) throws SelectAIException;
268
269    /**
270     * Opens a {@link VectorIndex} object for an existing vector index in the current schema.
271     * <p>
272     * A vector index stores embeddings for content that Select AI can retrieve
273     * for RAG. The index may have been created by Java, PL/SQL, Python, or
274     * another tool; this method only binds a Java object to the database object
275     * so it can be inspected, enabled, disabled, updated, or dropped.
276     * For a complete runnable sample source, see
277     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/OpenVectorIndexSample.html">
278     * OpenVectorIndexSample source</a>.
279     *
280     * @param indexName vector index name
281     * @return vector index object bound to the specified database index
282     * @throws SelectAIException when loading the index fails
283     * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-retrieval-augmented-generation.html">
284     *      Select AI with Retrieval Augmented Generation</a>
285     */
286    VectorIndex vectorIndex(String indexName) throws SelectAIException;
287
288    /**
289     * Creates a {@link VectorIndex} object initialized from configuration.
290     * <p>
291     * This call does not create/persist the vector index in the database. Invoke
292     * {@link VectorIndex#create()} on the returned object to create it.
293     * The configuration describes where source content is located, which
294     * credential can read it, and which profile/embedding settings Select AI
295     * should use to populate the vector store.
296     * For a complete runnable sample source, see
297     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/ConfigureVectorIndexSample.html">
298     * ConfigureVectorIndexSample source</a>.
299     *
300     * @param vectorIndexConfig vector index configuration
301     * @return vector index object initialized with the given configuration
302     * @throws SelectAIException when vector index object initialization fails
303     * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/select-ai-retrieval-augmented-generation.html">
304     *      Select AI RAG and vector indexes</a>
305     */
306    VectorIndex vectorIndex(VectorIndexConfig vectorIndexConfig) throws SelectAIException;
307
308    /**
309     * Lists vector indexes visible in the current schema.
310     * <p>
311     * Listing vector indexes is a collection-level operation, so it is exposed
312     * on the SelectAI client. This method is equivalent to
313     * {@link #listVectorIndexes(String)} with {@code .*}.
314     * For a complete runnable sample source, see
315     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/vectorindex/ListVectorIndexesSample.html">
316     * ListVectorIndexesSample source</a>.
317     *
318     * @return list of vector index objects hydrated from database metadata
319     * @throws SelectAIException when listing vector indexes fails
320     */
321    List<VectorIndex> listVectorIndexes() throws SelectAIException;
322
323    /**
324     * Lists vector indexes visible in the current schema whose names match the
325     * supplied database regular-expression pattern.
326     * <p>
327     * The pattern is evaluated by the database metadata query. Use
328     * {@link #vectorIndex(String)} when opening one literal vector-index name.
329     * For a complete runnable sample source, see
330     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/vectorindex/ListVectorIndexesSample.html">
331     * ListVectorIndexesSample source</a>.
332     *
333     * @param indexNamePattern regular-expression pattern matched against vector-index names
334     * @return list of matching vector index objects hydrated from database metadata
335     * @throws SelectAIException when listing vector indexes fails
336     */
337    List<VectorIndex> listVectorIndexes(String indexNamePattern) throws SelectAIException;
338
339    /**
340     * Lists conversations visible in the current schema.
341     * <p>
342     * Listing conversations is a collection-level operation, so it is exposed
343     * on the SelectAI client instead of requiring an unbound
344     * {@link Conversation} object.
345     * For a complete runnable sample source, see
346     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/conversation/ListConversationsSample.html">
347     * ListConversationsSample source</a>.
348     *
349     * @return list of conversation objects hydrated from database metadata
350     * @throws SelectAIException when listing conversations fails
351     */
352    List<Conversation> listConversations() throws SelectAIException;
353
354    /**
355     * Creates a {@link Conversation} object initialized with supplied attributes.
356     * <p>
357     * This call does not persist/create the conversation in the database. Invoke
358     * {@link Conversation#create()} on the returned object to create it.
359     * Conversations keep chat-style context so later prompts can refer to prior
360     * turns.
361     * For a complete runnable sample source, see
362     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/ConfigureConversationSample.html">
363     * ConfigureConversationSample source</a>.
364     *
365     * @param conversationAttributes conversation attributes
366     * @return conversation object initialized with the given attributes
367     * @throws SelectAIException when conversation object initialization fails
368     */
369    Conversation conversation(ConversationAttributes conversationAttributes) throws SelectAIException;
370
371    /**
372     * Opens a {@link Conversation} object for an existing conversation by ID.
373     * <p>
374     * The returned object can inspect attributes, update retention/title
375     * metadata, or drop the conversation.
376     * For a complete runnable sample source, see
377     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/selectai/OpenConversationSample.html">
378     * OpenConversationSample source</a>.
379     *
380     * @param conversationId conversation identifier
381     * @return conversation object bound to the given ID
382     * @throws SelectAIException when the conversation metadata cannot be loaded
383     */
384    Conversation conversation(String conversationId) throws SelectAIException;
385}