OpenAIProvider

@composio/core


@composio/core / provider/OpenAIProvider / OpenAIProvider

Class: OpenAIProvider

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:22

Base class for all non-agentic toolsets. This class is not meant to be used directly, but rather to be extended by concrete provider implementations.

Extends

Constructors

Constructor

new OpenAIProvider(): OpenAIProvider

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:45

Creates a new instance of the OpenAIProvider.

This is the default provider for the Composio SDK and is automatically available without additional installation.

Returns

OpenAIProvider

Example

1// The OpenAIProvider is used by default when initializing Composio
2const composio = new Composio({
3 apiKey: 'your-api-key'
4});
5
6// You can also explicitly specify it
7const composio = new Composio({
8 apiKey: 'your-api-key',
9 provider: new OpenAIProvider()
10});

Overrides

BaseNonAgenticProvider.constructor

Properties

_isAgentic

readonly _isAgentic: false = false

Defined in: ts/packages/core/src/provider/BaseProvider.ts:93

Internal

Whether the provider is agentic. This is set automatically set by the core SDK implementation for different provider types.

Inherited from

BaseNonAgenticProvider._isAgentic


name

readonly name: "openai" = 'openai'

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:23

The name of the provider. Used to identify the provider in the telemetry.

Overrides

BaseNonAgenticProvider.name

Methods

_setExecuteToolFn()

_setExecuteToolFn(executeToolFn): void

Defined in: ts/packages/core/src/provider/BaseProvider.ts:36

Internal

Set the function to execute a tool. This is set automatically and injected by the core SDK.

Parameters

executeToolFn

GlobalExecuteToolFn

Returns

void

Inherited from

BaseNonAgenticProvider._setExecuteToolFn


executeTool()

executeTool(toolSlug, body, modifers?): Promise<{ }>

Defined in: ts/packages/core/src/provider/BaseProvider.ts:50

Global function to execute a tool. This function is used by providerds to implement helper functions to execute tools. This is a 1:1 mapping of the execute method in the Tools class.

Parameters

toolSlug

string

The slug of the tool to execute.

body

The body of the tool execution.

modifers?

ExecuteToolModifiers

The modifiers of the tool execution.

Returns

Promise<{ }>

The result of the tool execution.

Inherited from

BaseNonAgenticProvider.executeTool


executeToolCall()

executeToolCall(userId, tool, options?, modifiers?): Promise<string>

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:200

Executes a tool call from OpenAI’s chat completion.

This method processes a tool call from OpenAI’s chat completion API, executes the corresponding Composio tool, and returns the result.

Parameters

userId

string

The user ID for authentication and tracking

tool

ChatCompletionMessageToolCall

The tool call from OpenAI

options?

ExecuteToolFnOptions

Optional execution options

modifiers?

ExecuteToolModifiers

Optional execution modifiers

Returns

Promise<string>

The result of the tool call as a JSON string

Example

1// Execute a tool call from OpenAI
2const toolCall = {
3 id: 'call_abc123',
4 type: 'function',
5 function: {
6 name: 'SEARCH_TOOL',
7 arguments: '{"query":"composio documentation"}'
8 }
9};
10
11const result = await provider.executeToolCall(
12 'user123',
13 toolCall,
14 { connectedAccountId: 'conn_xyz456' }
15);
16console.log(JSON.parse(result));

handleAssistantMessage()

handleAssistantMessage(userId, run, options?, modifiers?): Promise<ToolOutput[]>

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:322

Handles all the tool calls from the OpenAI Assistant API.

This method processes tool calls from an OpenAI Assistant run, executes each tool call, and returns the tool outputs for submission.

Parameters

userId

string

The user ID for authentication and tracking

run

Run

The Assistant run object containing tool calls

options?

ExecuteToolFnOptions

Optional execution options

modifiers?

ExecuteToolModifiers

Optional execution modifiers

Returns

Promise<ToolOutput[]>

Array of tool outputs for submission

Example

1// Handle tool calls from an OpenAI Assistant run
2const run = {
3 id: 'run_abc123',
4 required_action: {
5 submit_tool_outputs: {
6 tool_calls: [
7 {
8 id: 'call_xyz789',
9 type: 'function',
10 function: {
11 name: 'SEARCH_TOOL',
12 arguments: '{"query":"composio documentation"}'
13 }
14 }
15 ]
16 }
17 }
18};
19
20const toolOutputs = await provider.handleAssistantMessage(
21 'user123',
22 run,
23 { connectedAccountId: 'conn_xyz456' }
24);
25
26// Submit tool outputs back to OpenAI
27await openai.beta.threads.runs.submitToolOutputs(
28 thread.id,
29 run.id,
30 { tool_outputs: toolOutputs }
31);

handleToolCalls()

handleToolCalls(userId, chatCompletion, options?, modifiers?): Promise<string[]>

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:258

Handles tool calls from OpenAI’s chat completion response.

This method processes tool calls from an OpenAI chat completion response, executes each tool call, and returns the results.

Parameters

userId

string

The user ID for authentication and tracking

chatCompletion

ChatCompletion

The chat completion response from OpenAI

options?

ExecuteToolFnOptions

Optional execution options

modifiers?

ExecuteToolModifiers

Optional execution modifiers

Returns

Promise<string[]>

Array of tool execution results as JSON strings

Example

1// Handle tool calls from a chat completion response
2const chatCompletion = {
3 choices: [
4 {
5 message: {
6 tool_calls: [
7 {
8 id: 'call_abc123',
9 type: 'function',
10 function: {
11 name: 'SEARCH_TOOL',
12 arguments: '{"query":"composio documentation"}'
13 }
14 }
15 ]
16 }
17 }
18 ]
19};
20
21const results = await provider.handleToolCalls(
22 'user123',
23 chatCompletion,
24 { connectedAccountId: 'conn_xyz456' }
25);
26console.log(results); // Array of tool execution results

waitAndHandleAssistantStreamToolCalls()

waitAndHandleAssistantStreamToolCalls(userId, client, runStream, thread, options?, modifiers?): AsyncGenerator<AssistantStreamEvent, void, unknown>

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:394

Waits for the assistant stream and handles the tool calls.

This method processes an OpenAI Assistant stream, handles any tool calls that require action, and yields each event from the stream. It’s designed for streaming Assistant responses while handling tool calls in real-time.

Parameters

userId

string

The user ID for authentication and tracking

client

OpenAI

The OpenAI client instance

runStream

Stream<AssistantStreamEvent>

The Assistant run stream

thread

Thread

The thread object

options?

ExecuteToolFnOptions

Optional execution options

modifiers?

ExecuteToolModifiers

Optional execution modifiers

Returns

AsyncGenerator<AssistantStreamEvent, void, unknown>

Generator yielding stream events

Example

1// Process an OpenAI Assistant stream with tool calls
2const thread = await openai.beta.threads.create();
3const runStream = openai.beta.threads.runs.stream(thread.id, {
4 assistant_id: 'asst_abc123',
5 tools: provider.wrapTools(composioTools)
6});
7
8// Process the stream and handle tool calls
9const streamProcessor = provider.waitAndHandleAssistantStreamToolCalls(
10 'user123',
11 openai,
12 runStream,
13 thread,
14 { connectedAccountId: 'conn_xyz456' }
15);
16
17// Consume the stream events
18for await (const event of streamProcessor) {
19 if (event.event === 'thread.message.delta') {
20 console.log(event.data.delta.content);
21 }
22}

waitAndHandleAssistantToolCalls()

waitAndHandleAssistantToolCalls(userId, client, run, thread, options?, modifiers?): Promise<Run>

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:512

Waits for the assistant tool calls and handles them.

This method polls an OpenAI Assistant run until it completes or requires action, handles any tool calls, and returns the final run object. It’s designed for non-streaming Assistant interactions.

Parameters

userId

string

The user ID for authentication and tracking

client

OpenAI

The OpenAI client instance

run

Run

The initial run object

thread

Thread

The thread object

options?

ExecuteToolFnOptions

Optional execution options

modifiers?

ExecuteToolModifiers

Optional execution modifiers

Returns

Promise<Run>

The final run object after completion

Example

1// Process an OpenAI Assistant run with tool calls
2const thread = await openai.beta.threads.create();
3await openai.beta.threads.messages.create(thread.id, {
4 role: 'user',
5 content: 'Find information about Composio'
6});
7
8let run = await openai.beta.threads.runs.create(thread.id, {
9 assistant_id: 'asst_abc123',
10 tools: provider.wrapTools(composioTools)
11});
12
13// Wait for the run to complete, handling any tool calls
14run = await provider.waitAndHandleAssistantToolCalls(
15 'user123',
16 openai,
17 run,
18 thread,
19 { connectedAccountId: 'conn_xyz456' }
20);
21
22// Get the final messages after run completion
23const messages = await openai.beta.threads.messages.list(thread.id);
24console.log(messages.data[0].content);

wrapMcpServerResponse()

wrapMcpServerResponse(data, serverName, connectedAccountIds?, userIds?, toolkits?): McpServerGetResponse

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:60

Transform MCP URL response into OpenAI-specific format. OpenAI uses the standard format by default.

Parameters

data

McpUrlResponse

The MCP URL response data

serverName

string

Name of the MCP server

connectedAccountIds?

string[]

Optional array of connected account IDs

userIds?

string[]

Optional array of user IDs

toolkits?

string[]

Optional array of toolkit names

Returns

McpServerGetResponse

Standard MCP server response format

Overrides

BaseNonAgenticProvider.wrapMcpServerResponse


wrapTool()

wrapTool(tool): ChatCompletionTool

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:114

Wraps a Composio tool in the OpenAI function calling format.

This method transforms a Composio tool definition into the format expected by OpenAI’s function calling API.

Parameters

tool

The Composio tool to wrap

Returns

ChatCompletionTool

The wrapped tool in OpenAI format

Example

1// Wrap a single tool for use with OpenAI
2const composioTool = {
3 slug: 'SEARCH_TOOL',
4 description: 'Search for information',
5 inputParameters: {
6 type: 'object',
7 properties: {
8 query: { type: 'string' }
9 },
10 required: ['query']
11 }
12};
13
14const openAITool = provider.wrapTool(composioTool);

Overrides

BaseNonAgenticProvider.wrapTool


wrapTools()

wrapTools(tools): OpenAiToolCollection

Defined in: ts/packages/core/src/provider/OpenAIProvider.ts:164

Wraps multiple Composio tools in the OpenAI function calling format.

This method transforms a list of Composio tools into the format expected by OpenAI’s function calling API.

Parameters

tools

object[]

Array of Composio tools to wrap

Returns

OpenAiToolCollection

Array of wrapped tools in OpenAI format

Example

1// Wrap multiple tools for use with OpenAI
2const composioTools = [
3 {
4 slug: 'SEARCH_TOOL',
5 description: 'Search for information',
6 inputParameters: {
7 type: 'object',
8 properties: {
9 query: { type: 'string' }
10 }
11 }
12 },
13 {
14 slug: 'WEATHER_TOOL',
15 description: 'Get weather information',
16 inputParameters: {
17 type: 'object',
18 properties: {
19 location: { type: 'string' }
20 }
21 }
22 }
23];
24
25const openAITools = provider.wrapTools(composioTools);

Overrides

BaseNonAgenticProvider.wrapTools