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.agent;
008
009import com.oracle.database.selectai.model.agent.ExecutionStateRequest;
010import com.oracle.database.selectai.model.agent.ExecutionStateParams;
011import com.oracle.database.selectai.model.agent.ExportTeamParams;
012import com.oracle.database.selectai.model.agent.ExportTeamRequest;
013import com.oracle.database.selectai.model.agent.RunTeamRequest;
014import com.oracle.database.selectai.model.agent.TeamAttributes;
015import com.oracle.database.selectai.model.agent.TeamDescription;
016import com.oracle.database.selectai.model.agent.TeamExecutionState;
017import com.oracle.database.selectai.model.agent.TeamRunResult;
018import com.oracle.database.selectai.model.agent.TeamStatus;
019import com.oracle.database.selectai.model.SelectAIException;
020
021/**
022 * Contract for Select AI agent team lifecycle, orchestration, and definition
023 * management operations.
024 * <p>
025 * A team is a database-side resource managed by the
026 * {@code DBMS_CLOUD_AI_AGENT} PL/SQL package. Teams coordinate agents and can
027 * run multi-agent workflows using their configured members and attributes.
028 * <p>
029 * A {@code Team} instance can represent an existing database team or be
030 * configured for creation. The name-only {@link SelectAIAgent#team(String)}
031 * factory verifies that the team exists and loads its metadata and attributes
032 * before returning. {@link SelectAIAgent#listTeams()} also returns handles with
033 * loaded metadata and attributes. A handle configured for creation is obtained with
034 * {@link SelectAIAgent#team(String, TeamAttributes, String, TeamStatus)}.
035 * A granted team owned by another user can be run by its qualified name via
036 * {@link SelectAIAgent#runTeam(String, RunTeamRequest)}; that path does not
037 * create a {@code Team} handle or expose owner-only metadata.
038 * <p>
039 * A creation-configured handle retains its supplied attributes, description,
040 * and status without querying the database. Only such a handle can call
041 * {@link #create()}; after creation succeeds, it becomes database-backed.
042 * {@link #enable()}, {@link #disable()}, {@link #drop(boolean)},
043 * orchestration, state, and export operations require an existing database-side
044 * team.
045 * Existing teams can update individual attributes with
046 * {@link #setAttribute(String, String)} or multiple attributes with
047 * {@link #setAttributes(TeamAttributes)}; both operations reload the typed
048 * attributes after the database update succeeds.
049 * <p>
050 * Before creation, {@link #getStatus()} and {@link #getTeamAttributes()} return
051 * the configured values. For a database-backed handle, each call to either
052 * method queries the database. {@code getStatus()} does not change the
053 * description returned by {@link #getDescription()}. The description is
054 * supplied or loaded when the handle is initialized; {@code getDescription()}
055 * does not query the database. Methods such as
056 * {@link #describe()} and {@link #getStateByConversationId(String)} retrieve
057 * other database-side information.
058 * <p>
059 * Names and required Java arguments are checked locally. Loading metadata and
060 * executing team operations require database access; the database checks
061 * privileges and validates team attributes when they are submitted. Team
062 * execution and state-management payloads must conform to the formats
063 * accepted by the database package.
064 * <p>
065 * For a complete runnable example, see
066 * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/CreateTeamSample.html">
067 * CreateTeamSample source</a>.
068 * For an end-to-end example that creates the resources needed by a team, runs
069 * it, and continues the same database conversation, see
070 * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/AgentWorkflowSample.html">
071 * AgentWorkflowSample source</a>.
072 * For a supervisor agent coordinating separate worker-task mappings, see
073 * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SupervisorTeamSample.html">
074 * SupervisorTeamSample source</a>.
075 *
076 * @see SelectAIAgent
077 * @see <a href="https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/dbms-cloud-ai-agent-package.html">
078 *      DBMS_CLOUD_AI_AGENT package documentation</a>
079 * @since 1.1.0
080 */
081public interface Team {
082    /**
083     * Creates this creation-configured team in the database.
084     * <p>
085     * For a runnable example, see
086     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/CreateTeamSample.html">
087     * CreateTeamSample source</a>.
088     *
089     * @return {@code true} when the database creates the team
090     * @throws IllegalStateException when this handle was loaded from an existing team
091     * @throws SelectAIException when the database rejects or cannot complete the operation
092     */
093    boolean create() throws SelectAIException;
094    /**
095     * Enables this database-backed team.
096     * <p>
097     * For a runnable example, see
098     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/EnableTeamSample.html">
099     * EnableTeamSample source</a>.
100     *
101     * @return {@code true} when the database enables the team
102     * @throws IllegalStateException when this team has not been created
103     * @throws SelectAIException when the database operation fails
104     */
105    boolean enable() throws SelectAIException;
106    /**
107     * Disables this database-backed team.
108     * <p>
109     * For a runnable example, see
110     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/DisableTeamSample.html">
111     * DisableTeamSample source</a>.
112     *
113     * @return {@code true} when the database disables the team
114     * @throws IllegalStateException when this team has not been created
115     * @throws SelectAIException when the database operation fails
116     */
117    boolean disable() throws SelectAIException;
118    /**
119     * Drops this database-backed team owned by the connected user. When
120     * {@code force} is {@code false}, dropping a missing team fails. When
121     * {@code force} is {@code true}, a missing-team error is ignored and the
122     * operation succeeds. The flag does not bypass authorization or perform a
123     * cascading delete.
124     * <p>
125     * For a runnable example, see
126     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/DropTeamSample.html">
127     * DropTeamSample source</a>.
128     *
129     * @param force database force-drop option
130     * @return {@code true} when the drop completes
131     * @throws IllegalStateException when {@code force} is {@code false} and
132     *         this team has not been created
133     * @throws SelectAIException when the database operation fails
134     */
135    boolean drop(boolean force) throws SelectAIException;
136
137    /**
138     * Updates one team attribute in the database.
139     * <p>
140     * For a runnable example, see
141     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetTeamAttributeSample.html">
142     * SetTeamAttributeSample source</a>.
143     *
144     * @param attributeName database team attribute name
145     * @param attributeValue string value, or {@code null} where supported
146     * @return {@code true} when the update succeeds
147     * @throws IllegalArgumentException when {@code attributeName} is null or blank
148     * @throws IllegalStateException when this team has not been created
149     * @throws SelectAIException when the database update fails
150     */
151    boolean setAttribute(String attributeName, String attributeValue) throws SelectAIException;
152
153    /**
154     * Updates one boolean team attribute in the database.
155     * <p>
156     * For a runnable example, see
157     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetTeamAttributeSample.html">
158     * SetTeamAttributeSample source</a>.
159     *
160     * @param attributeName database team attribute name
161     * @param attributeValue boolean value
162     * @return {@code true} when the update succeeds
163     * @throws IllegalArgumentException when {@code attributeName} is null or blank
164     * @throws IllegalStateException when this team has not been created
165     * @throws SelectAIException when the database update fails
166     */
167    boolean setAttribute(String attributeName, boolean attributeValue) throws SelectAIException;
168
169    /**
170     * Updates one integer team attribute in the database.
171     * <p>
172     * For a runnable example, see
173     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetTeamAttributeSample.html">
174     * SetTeamAttributeSample source</a>.
175     *
176     * @param attributeName database team attribute name
177     * @param attributeValue integer value
178     * @return {@code true} when the update succeeds
179     * @throws IllegalArgumentException when {@code attributeName} is null or blank,
180     *         or {@code attributeValue} is null
181     * @throws IllegalStateException when this team has not been created
182     * @throws SelectAIException when the database update fails
183     */
184    boolean setAttribute(String attributeName, Integer attributeValue) throws SelectAIException;
185
186    /**
187     * Updates one floating-point team attribute in the database.
188     * <p>
189     * For a runnable example, see
190     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetTeamAttributeSample.html">
191     * SetTeamAttributeSample source</a>.
192     *
193     * @param attributeName database team attribute name
194     * @param attributeValue floating-point value
195     * @return {@code true} when the update succeeds
196     * @throws IllegalArgumentException when {@code attributeName} is null or blank,
197     *         or {@code attributeValue} is null
198     * @throws IllegalStateException when this team has not been created
199     * @throws SelectAIException when the database update fails
200     */
201    boolean setAttribute(String attributeName, Float attributeValue) throws SelectAIException;
202
203    /**
204     * Updates multiple team attributes in one database operation.
205     * <p>
206     * For a runnable example, see
207     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetTeamAttributesSample.html">
208     * SetTeamAttributesSample source</a>.
209     *
210     * @param teamAttributes team attributes to update
211     * @return {@code true} when the update succeeds
212     * @throws IllegalArgumentException when {@code teamAttributes} is null
213     * @throws IllegalStateException when this team has not been created
214     * @throws SelectAIException when the database update fails
215     */
216    boolean setAttributes(TeamAttributes teamAttributes) throws SelectAIException;
217
218    /**
219     * Grants a database user or role permission to run this owner-managed team.
220     * The connected database user must own the team. This operation grants
221     * execution access to the team as a unit; it does not grant permission to
222     * modify the team or independently run its agents, tasks, or tools.
223     * <p>
224     * For a runnable example, see
225     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/GrantTeamHandleAccessSample.html">
226     * GrantTeamHandleAccessSample source</a>.
227     *
228     * @param userOrRoleName database user or role receiving execution access
229     * @return {@code true} when the database accepts the grant
230     * @throws IllegalArgumentException when the name is null, blank, invalid,
231     *         or {@code PUBLIC}
232     * @throws IllegalStateException when this team has not been created
233     * @throws SelectAIException when the database rejects or cannot complete the grant
234     * @since 1.1.0
235     */
236    boolean grantAccess(String userOrRoleName) throws SelectAIException;
237
238    /**
239     * Revokes a database user or role permission to run this owner-managed team.
240     * Revocation prevents new runs and resumes but does not terminate a run
241     * that is already in progress.
242     * <p>
243     * For a runnable example, see
244     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/RevokeTeamHandleAccessSample.html">
245     * RevokeTeamHandleAccessSample source</a>.
246     *
247     * @param userOrRoleName database user or role losing execution access
248     * @return {@code true} when the database accepts the revoke
249     * @throws IllegalArgumentException when the name is null, blank, invalid,
250     *         or {@code PUBLIC}
251     * @throws IllegalStateException when this team has not been created
252     * @throws SelectAIException when the database rejects or cannot complete the revoke
253     * @since 1.1.0
254     */
255    boolean revokeAccess(String userOrRoleName) throws SelectAIException;
256
257    /**
258     * Starts or resumes a synchronous team run using the supplied prompt and
259     * runtime parameters. Reuse the request's conversation identifier to resume
260     * a conversation across database sessions. This method uses an existing
261     * database-backed handle; use {@link SelectAIAgent#runTeam(String, RunTeamRequest)}
262     * to run a granted team owned by another user. In {@code DataSource} mode,
263     * the borrowed JDBC connection can remain occupied for the duration of the
264     * workflow. The SDK does not provide cancellation, and an interrupted or
265     * timed-out call may not stop server-side work; retry only when the workflow
266     * is known to be safe to repeat.
267     * Without established session context, provide a database-created
268     * conversation identifier in the request; otherwise the database may
269     * reject the run with ORA-20053.
270     * <p>
271     * For a runnable example, see
272     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/RunTeamSample.html">
273     * RunTeamSample source</a>.
274     * For a human-input workflow resumed through a pooled {@code DataSource}
275     * using the client-level run API, see
276     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/PooledTeamResumeSample.html">
277     * PooledTeamResumeSample source</a>.
278     *
279     * @param request run request; its prompt and parameters may be unset
280     * @return typed team result retaining the database response
281     * @throws IllegalArgumentException when {@code request} is null
282     * @throws SelectAIException when the database operation fails
283     */
284    TeamRunResult run(RunTeamRequest request) throws SelectAIException;
285    /**
286     * Returns the database-generated team definition and aggregated capabilities.
287     * <p>
288     * For a runnable example, see
289     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/DescribeTeamSample.html">
290     * DescribeTeamSample source</a>.
291     *
292     * Additional JSON fields are tolerated; malformed or non-object responses
293     * are reported as mapping failures instead of empty metadata.
294     *
295     * @return typed team description with skills retained as JSON, or
296     *         {@code null} when the database returns no value
297     * @throws IllegalStateException when this team has not been created
298     * @throws SelectAIException when the database operation or response mapping fails
299     */
300    TeamDescription describe() throws SelectAIException;
301    /**
302     * Returns the latest run state for a conversation associated with this team.
303     *
304     * <p>This convenience method creates the database {@code conversation_id}
305     * parameter and is suitable for resuming or inspecting a run across pooled
306     * database connections.</p>
307     * <p>
308     * For a runnable example, see
309     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/GetOwnedTeamStateSample.html">
310     * GetOwnedTeamStateSample source</a>.
311     *
312     * @param conversationId existing database conversation identifier
313     * @return typed team execution state retaining the original database value
314     * @throws IllegalArgumentException when {@code conversationId} is null or blank
315     * @throws IllegalStateException when this team has not been created
316     * @throws SelectAIException when the database operation fails
317     * @since 1.1.0
318     */
319    TeamExecutionState getStateByConversationId(String conversationId) throws SelectAIException;
320
321    /**
322     * Updates the state of a waiting team execution. Use
323     * {@link ExecutionStateRequest.Builder#params(ExecutionStateParams)} when
324     * the request should expose the typed parameter object explicitly. The
325     * {@link ExecutionStateRequest.Builder#conversationId(String)} method is
326     * a convenience for the common case.
327     * <p>
328     * For a runnable example, see
329     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/SetOwnedTeamExecutionStateSample.html">
330     * SetOwnedTeamExecutionStateSample source</a>.
331     *
332     * @param request execution state and a typed conversation identifier
333     * @return {@code true} when the database accepts the update
334     * @throws IllegalArgumentException when {@code request} is null or does not
335     *         identify a conversation
336     * @throws IllegalStateException when this team has not been created
337     * @throws SelectAIException when the database rejects or cannot complete the operation
338     */
339    boolean setExecutionState(ExecutionStateRequest request) throws SelectAIException;
340    /**
341     * Returns the team definition as JSON using the database defaults.
342     * <p>
343     * For a runnable example, see
344     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/ExportTeamSample.html">
345     * ExportTeamSample source</a>.
346     *
347     * @return exported team definition returned by the database
348     * @throws IllegalStateException when this team has not been created
349     * @throws SelectAIException when the database operation fails
350     */
351    String exportDefinition() throws SelectAIException;
352
353    /**
354     * Returns the team definition as JSON using typed export parameters.
355     *
356     * <p>The currently supported parameter is {@code overwrite}. The database
357     * returns the complete exported definition as a JSON string.</p>
358     * <p>
359     * For a runnable example, see
360     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/ExportTeamSample.html">
361     * ExportTeamSample source</a>.
362     *
363     * @param params typed export parameters, or {@code null} to use database defaults
364     * @return exported team definition returned by the database
365     * @throws IllegalStateException when this team has not been created
366     * @throws SelectAIException when the database operation fails
367     */
368    String exportDefinition(ExportTeamParams params) throws SelectAIException;
369    /**
370     * Exports this team definition to object storage or a database directory file.
371     * Use {@link ExportTeamParams} through {@link ExportTeamRequest} to provide
372     * the typed export options supported by the database.
373     * <p>
374     * For a runnable example, see
375     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/ExportTeamSample.html">
376     * ExportTeamSample source</a>.
377     *
378     * @param request export destination and optional overwrite configuration
379     * @return {@code true} when the export succeeds
380     * @throws IllegalArgumentException when {@code request} is null
381     * @throws IllegalStateException when this team has not been created
382     * @throws SelectAIException when the database operation fails
383     */
384    boolean exportDefinition(ExportTeamRequest request) throws SelectAIException;
385    /**
386     * Returns the configured or database-reported team name without database access.
387     * <p>
388     * For a runnable example, see
389     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/LoadTeamSample.html">
390     * LoadTeamSample source</a>.
391     *
392     * @return configured or database-reported team name
393     */
394    String getTeamName();
395    /**
396     * Returns the description supplied or loaded when this handle was initialized.
397     * This method does not access the database and the value is not changed by
398     * {@link #getStatus()}.
399     * <p>
400     * For a runnable example, see
401     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/LoadTeamSample.html">
402     * LoadTeamSample source</a>.
403     *
404     * @return supplied or loaded team description, or {@code null} when absent
405     */
406    String getDescription();
407    /**
408     * Returns the configured status before creation or the current database
409     * status for an existing team. A pending team with no configured status
410     * returns {@code null}. This method does not change the description.
411     * <p>
412     * For a runnable example, see
413     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/LoadTeamSample.html">
414     * LoadTeamSample source</a>.
415     *
416     * @return configured or current team status
417     * @throws SelectAIException when current team metadata cannot be fetched
418     */
419    TeamStatus getStatus() throws SelectAIException;
420    /**
421     * Returns team attributes for this handle.
422     * <p>
423     * For a database-backed team, this method reloads the attributes from the
424     * database on each call and therefore can fail if the resource was deleted.
425     * For a creation-configured team, it returns the configured attributes
426     * without querying the database.
427     * <p>
428     * For a runnable example, see
429     * <a href="{@docRoot}/src-html/com/oracle/database/selectai/samples/agent/LoadTeamSample.html">
430     * LoadTeamSample source</a>.
431     *
432     * @return current team attributes for an existing team, or configured
433     *         attributes for a team pending creation
434     * @throws SelectAIException when the team no longer exists or current attributes cannot be fetched
435     */
436    TeamAttributes getTeamAttributes() throws SelectAIException;
437}