@micdrop/openai 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,30 +1,13 @@
1
1
  # @micdrop/openai
2
2
 
3
- OpenAI implementation for [@micdrop/server](../server/README.md).
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/ai-integration/provided-integrations/openai)
4
4
 
5
- This package provides AI agent and speech-to-text implementations using OpenAI's API.
6
-
7
- ## Features
8
-
9
- - 🤖 **OpenAI Agent** - GPT-powered conversational AI with advanced features:
10
- - Streaming responses for real-time conversation
11
- - Auto end call detection when user requests to end conversation
12
- - Semantic turn detection to handle incomplete sentences
13
- - User noise filtering to ignore meaningless interjections
14
- - Tool calling support for enhanced interactions
15
- - 🎙️ **OpenAI STT** - Whisper-powered speech-to-text with:
16
- - High-quality transcription using Whisper models
17
- - Multi-language support
18
- - Configurable model selection
5
+ OpenAI implementation for [@micdrop/server](https://micdrop.dev/docs/server).
19
6
 
20
7
  ## Installation
21
8
 
22
9
  ```bash
23
10
  npm install @micdrop/openai
24
- # or
25
- yarn add @micdrop/openai
26
- # or
27
- pnpm add @micdrop/openai
28
11
  ```
29
12
 
30
13
  ## OpenAI Agent
@@ -59,18 +42,6 @@ new MicdropServer(socket, {
59
42
  })
60
43
  ```
61
44
 
62
- ### Options
63
-
64
- | Option | Type | Default | Description |
65
- | --------------------- | ------------------- | ---------- | --------------------------------------- |
66
- | `apiKey` | `string` | Required | Your OpenAI API key |
67
- | `model` | `string` | `'gpt-4o'` | OpenAI model to use |
68
- | `systemPrompt` | `string` | Required | System prompt for the agent |
69
- | `autoEndCall` | `boolean \| string` | `false` | Auto-detect when user wants to end call |
70
- | `autoSemanticTurn` | `boolean \| string` | `false` | Handle incomplete user sentences |
71
- | `autoIgnoreUserNoise` | `boolean \| string` | `false` | Filter meaningless user sounds |
72
- | `settings` | `object` | `{}` | Additional OpenAI API parameters |
73
-
74
45
  ## OpenAI STT (Speech-to-Text)
75
46
 
76
47
  ### Usage
@@ -92,124 +63,9 @@ new MicdropServer(socket, {
92
63
  })
93
64
  ```
94
65
 
95
- ### Options
96
-
97
- | Option | Type | Default | Description |
98
- | ---------- | -------- | ------------- | ------------------------------- |
99
- | `apiKey` | `string` | Required | Your OpenAI API key |
100
- | `model` | `string` | `'whisper-1'` | Whisper model to use |
101
- | `language` | `string` | Optional | Language code for transcription |
102
-
103
- ## Tool Management
104
-
105
- The OpenAI Agent supports adding and removing custom tools to extend its capabilities.
106
-
107
- ### Adding Tools
108
-
109
- Use `addTool()` to add custom functions that the agent can call during conversations:
110
-
111
- ```typescript
112
- import { z } from 'zod'
113
-
114
- const agent = new OpenaiAgent({
115
- apiKey: process.env.OPENAI_API_KEY || '',
116
- systemPrompt: 'You are a helpful assistant that can manage user information.',
117
- })
118
-
119
- // Add a simple tool without parameters
120
- agent.addTool({
121
- name: 'get_time',
122
- description: 'Get the current time',
123
- callback: () => new Date().toLocaleTimeString(),
124
- })
125
-
126
- // Add a tool with typed parameters using Zod schema
127
- agent.addTool({
128
- name: 'set_user_info',
129
- description: 'Save user information to the database',
130
- parameters: z.object({
131
- city: z.string().describe('City'),
132
- jobTitle: z.string().describe('Job title').nullable(),
133
- experience: z
134
- .number()
135
- .describe('Number of years of experience of the user')
136
- .nullable(),
137
- }),
138
- callback: ({ city, jobTitle, experience }) => {
139
- // Your implementation here
140
- console.log('Saving user:', { city, jobTitle, experience })
141
- return { success: true, message: 'User information saved' }
142
- },
143
- })
144
-
145
- // Add a tool that returns data for the conversation
146
- agent.addTool({
147
- name: 'search_database',
148
- description: 'Search for items in the database',
149
- parameters: z.object({
150
- query: z.string().describe('Search query'),
151
- limit: z.number().default(10).describe('Maximum number of results'),
152
- }),
153
- callback: async ({ query, limit }) => {
154
- // Your search implementation
155
- const results = await searchDatabase(query, limit)
156
- return { results, total: results.length }
157
- },
158
- })
159
- ```
160
-
161
- ### Removing Tools
162
-
163
- Use `removeTool()` to remove tools by name:
164
-
165
- ```typescript
166
- // Remove a specific tool
167
- agent.removeTool('get_time')
168
- ```
169
-
170
- ## Advanced Features
171
-
172
- ### Auto End Call
173
-
174
- When enabled, the agent automatically detects when the user wants to end the conversation and triggers the call termination:
175
-
176
- ```typescript
177
- const agent = new OpenaiAgent({
178
- apiKey: process.env.OPENAI_API_KEY || '',
179
- systemPrompt: 'You are a helpful assistant',
180
- autoEndCall: true, // Use default detection
181
- // or provide custom prompt:
182
- // autoEndCall: 'User is saying goodbye or wants to hang up',
183
- })
184
- ```
185
-
186
- ### Semantic Turn Detection
187
-
188
- Handles cases where users speak incomplete sentences, allowing for more natural conversation flow:
189
-
190
- ```typescript
191
- const agent = new OpenaiAgent({
192
- apiKey: process.env.OPENAI_API_KEY || '',
193
- systemPrompt: 'You are a helpful assistant',
194
- autoSemanticTurn: true, // Wait for complete thoughts
195
- // or provide custom prompt:
196
- // autoSemanticTurn: 'Last user message is an incomplete sentence',
197
- })
198
- ```
199
-
200
- ### User Noise Filtering
66
+ ## Documentation
201
67
 
202
- Filters out meaningless sounds like "uh", "hmm", "ahem" that don't carry conversational meaning:
203
-
204
- ```typescript
205
- const agent = new OpenaiAgent({
206
- apiKey: process.env.OPENAI_API_KEY || '',
207
- systemPrompt: 'You are a helpful assistant',
208
- autoIgnoreUserNoise: true, // Ignore filler sounds
209
- // or provide custom prompt:
210
- // autoIgnoreUserNoise: 'Last user message is just an interjection',
211
- })
212
- ```
68
+ Read full [documentation of the OpenAI integration for Micdrop](https://micdrop.dev/docs/ai-integration/provided-integrations/openai) on the [website](https://micdrop.dev).
213
69
 
214
70
  ## License
215
71
 
package/dist/index.d.mts CHANGED
@@ -8,6 +8,7 @@ interface Tool<Schema extends z.ZodObject = z.ZodObject> {
8
8
  description: string;
9
9
  parameters?: Schema;
10
10
  callback: (input: z.infer<Schema>) => any | Promise<any>;
11
+ skipAnswer?: boolean;
11
12
  }
12
13
 
13
14
  interface OpenaiAgentOptions extends AgentOptions {
@@ -24,13 +25,11 @@ declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
24
25
  private tools;
25
26
  private abortController?;
26
27
  constructor(options: OpenaiAgentOptions);
27
- answer(): {
28
- message: Promise<string>;
29
- stream: PassThrough;
30
- };
28
+ answer(): PassThrough;
31
29
  private generateAnswer;
32
30
  addTool<Schema extends z.ZodObject>(tool: Tool<Schema>): void;
33
31
  removeTool(name: string): void;
32
+ getTool(name: string): Tool | undefined;
34
33
  private getDefaultTools;
35
34
  private executeTool;
36
35
  cancel(): void;
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ interface Tool<Schema extends z.ZodObject = z.ZodObject> {
8
8
  description: string;
9
9
  parameters?: Schema;
10
10
  callback: (input: z.infer<Schema>) => any | Promise<any>;
11
+ skipAnswer?: boolean;
11
12
  }
12
13
 
13
14
  interface OpenaiAgentOptions extends AgentOptions {
@@ -24,13 +25,11 @@ declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
24
25
  private tools;
25
26
  private abortController?;
26
27
  constructor(options: OpenaiAgentOptions);
27
- answer(): {
28
- message: Promise<string>;
29
- stream: PassThrough;
30
- };
28
+ answer(): PassThrough;
31
29
  private generateAnswer;
32
30
  addTool<Schema extends z.ZodObject>(tool: Tool<Schema>): void;
33
31
  removeTool(name: string): void;
32
+ getTool(name: string): Tool | undefined;
34
33
  private getDefaultTools;
35
34
  private executeTool;
36
35
  cancel(): void;
package/dist/index.js CHANGED
@@ -63,11 +63,11 @@ function createToolCallOutput(toolCall, args) {
63
63
  var DEFAULT_MODEL = "gpt-4o";
64
64
  var DEFAULT_MAX_CALLS = 5;
65
65
  var AUTO_END_CALL_TOOL_NAME = "end_call";
66
- var AUTO_END_CALL_PROMPT = "User asks to end the call";
66
+ var AUTO_END_CALL_PROMPT = "Call this tool only if user asks to end the call";
67
67
  var AUTO_SEMANTIC_TURN_TOOL_NAME = "semantic_turn";
68
- var AUTO_SEMANTIC_TURN_PROMPT = "Last user message is an incomplete sentence";
68
+ var AUTO_SEMANTIC_TURN_PROMPT = "Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering";
69
69
  var AUTO_IGNORE_USER_NOISE_TOOL_NAME = "ignore_user_noise";
70
- var AUTO_IGNORE_USER_NOISE_PROMPT = `Last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: "Uh", "Ahem", "Hmm", "Ah") but doesn't carry any clear meaning like agreeing, refusing, or commanding`;
70
+ var AUTO_IGNORE_USER_NOISE_PROMPT = `Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: "Uh", "Ahem", "Hmm", "Ah") but doesn't carry any clear meaning like agreeing, refusing, or commanding`;
71
71
  var OpenaiAgent = class extends import_server.Agent {
72
72
  constructor(options) {
73
73
  super(options);
@@ -78,19 +78,17 @@ var OpenaiAgent = class extends import_server.Agent {
78
78
  this.cancel();
79
79
  this.log("Start answering");
80
80
  const stream = new import_stream.PassThrough();
81
- const textPromise = this.createTextPromise();
82
- this.generateAnswer(stream, textPromise).finally(() => {
81
+ this.generateAnswer(stream).finally(() => {
83
82
  this.abortController = void 0;
84
83
  if (stream.writable) {
85
84
  stream.end();
86
85
  }
87
86
  });
88
- return { message: textPromise.promise, stream };
87
+ return stream;
89
88
  }
90
- async generateAnswer(stream, textPromise, callCount = 0, toolInputs) {
89
+ async generateAnswer(stream, callCount = 0, toolInputs) {
91
90
  if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {
92
91
  console.error("[OpenaiAgent] Max calls reached");
93
- textPromise.reject(new Error("Max calls reached"));
94
92
  return;
95
93
  }
96
94
  this.abortController = new AbortController();
@@ -119,6 +117,8 @@ var OpenaiAgent = class extends import_server.Agent {
119
117
  { signal }
120
118
  );
121
119
  const toolOutputs = [];
120
+ let hasAnswer = false;
121
+ let skipAnswer = false;
122
122
  for await (const event of response) {
123
123
  switch (event.type) {
124
124
  case "response.output_text.delta":
@@ -128,29 +128,25 @@ var OpenaiAgent = class extends import_server.Agent {
128
128
  case "response.output_text.done":
129
129
  this.addAssistantMessage(event.text);
130
130
  stream.end();
131
- textPromise.resolve(event.text);
131
+ hasAnswer = true;
132
132
  break;
133
133
  case "response.output_item.done":
134
134
  if (event.item.type === "function_call") {
135
- toolOutputs.push(event.item);
136
- const toolOutput = await this.executeTool(event.item);
137
- toolOutputs.push(toolOutput);
135
+ const result = await this.executeTool(event.item);
136
+ toolOutputs.push(result.toolCall, result.toolCallOutput);
137
+ if (result.skipAnswer) {
138
+ skipAnswer = true;
139
+ }
138
140
  }
139
141
  break;
140
142
  default:
141
143
  break;
142
144
  }
143
145
  }
144
- if (toolOutputs.length > 0) {
145
- await this.generateAnswer(
146
- stream,
147
- textPromise,
148
- callCount + 1,
149
- toolOutputs
150
- );
146
+ if (toolOutputs.length > 0 && !hasAnswer && !skipAnswer) {
147
+ await this.generateAnswer(stream, callCount + 1, toolOutputs);
151
148
  }
152
149
  } catch (error) {
153
- textPromise.reject(error);
154
150
  if (!(error instanceof import_openai.default.APIUserAbortError)) {
155
151
  console.error("[OpenaiAgent] Error answering:", error);
156
152
  }
@@ -162,6 +158,9 @@ var OpenaiAgent = class extends import_server.Agent {
162
158
  removeTool(name) {
163
159
  this.tools = this.tools.filter((tool) => tool.name !== name);
164
160
  }
161
+ getTool(name) {
162
+ return this.tools.find((tool) => tool.name === name);
163
+ }
165
164
  getDefaultTools() {
166
165
  const tools = [];
167
166
  if (this.options.autoEndCall) {
@@ -175,6 +174,7 @@ var OpenaiAgent = class extends import_server.Agent {
175
174
  tools.push({
176
175
  name: AUTO_SEMANTIC_TURN_TOOL_NAME,
177
176
  description: typeof this.options.autoSemanticTurn === "string" ? this.options.autoSemanticTurn : AUTO_SEMANTIC_TURN_PROMPT,
177
+ skipAnswer: true,
178
178
  callback: () => this.skipAnswer()
179
179
  });
180
180
  }
@@ -182,6 +182,7 @@ var OpenaiAgent = class extends import_server.Agent {
182
182
  tools.push({
183
183
  name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,
184
184
  description: typeof this.options.autoIgnoreUserNoise === "string" ? this.options.autoIgnoreUserNoise : AUTO_IGNORE_USER_NOISE_PROMPT,
185
+ skipAnswer: true,
185
186
  callback: () => this.cancelLastUserMessage()
186
187
  });
187
188
  }
@@ -189,17 +190,26 @@ var OpenaiAgent = class extends import_server.Agent {
189
190
  }
190
191
  async executeTool(toolCall) {
191
192
  try {
192
- const tool = this.tools.find((tool2) => tool2.name === toolCall.name);
193
+ const tool = this.getTool(toolCall.name);
193
194
  if (!tool) {
194
195
  throw new Error(`Tool not found "${toolCall.name}"`);
195
196
  }
196
197
  this.log("Executing tool:", toolCall.name, toolCall.arguments);
197
198
  const args = JSON.parse(toolCall.arguments);
198
199
  const result = await tool.callback(args);
199
- return createToolCallOutput(toolCall, result || {});
200
+ return {
201
+ toolCall,
202
+ toolCallOutput: createToolCallOutput(toolCall, result || {}),
203
+ skipAnswer: tool.skipAnswer
204
+ };
200
205
  } catch (error) {
201
206
  console.error("[OpenaiAgent] Error executing tool:", error);
202
- return createToolCallOutput(toolCall, { error: error.message });
207
+ return {
208
+ toolCall,
209
+ toolCallOutput: createToolCallOutput(toolCall, {
210
+ error: error.message
211
+ })
212
+ };
203
213
  }
204
214
  }
205
215
  cancel() {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/OpenaiAgent.ts","../src/tools.ts","../src/OpenaiSTT.ts"],"sourcesContent":["export * from './OpenaiAgent'\nexport * from './OpenaiSTT'\n","import { Agent, AgentOptions, TextPromise } from '@micdrop/server'\nimport OpenAI from 'openai'\nimport { PassThrough } from 'stream'\nimport { z } from 'zod'\nimport {\n createToolCallOutput,\n createToolSchema,\n Tool,\n ToolCall,\n ToolCallOutput,\n} from './tools'\n\nexport interface OpenaiAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n maxCalls?: number\n settings?: Omit<\n OpenAI.Responses.ResponseCreateParamsStreaming,\n 'input' | 'model'\n >\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n}\n\nconst DEFAULT_MODEL = 'gpt-4o'\nconst DEFAULT_MAX_CALLS = 5\n\nconst AUTO_END_CALL_TOOL_NAME = 'end_call'\nconst AUTO_END_CALL_PROMPT = 'User asks to end the call'\n\nconst AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nconst AUTO_SEMANTIC_TURN_PROMPT = 'Last user message is an incomplete sentence'\n\nconst AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nconst AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n\nexport class OpenaiAgent extends Agent<OpenaiAgentOptions> {\n private openai: OpenAI\n private tools: Tool[]\n private abortController?: AbortController\n\n constructor(options: OpenaiAgentOptions) {\n super(options)\n this.openai = new OpenAI({ apiKey: options.apiKey })\n this.tools = this.getDefaultTools()\n }\n\n answer() {\n this.cancel()\n this.log('Start answering')\n const stream = new PassThrough()\n const textPromise = this.createTextPromise()\n\n this.generateAnswer(stream, textPromise).finally(() => {\n this.abortController = undefined\n if (stream.writable) {\n stream.end()\n }\n })\n\n return { message: textPromise.promise, stream }\n }\n\n private async generateAnswer(\n stream: PassThrough,\n textPromise: TextPromise,\n callCount = 0,\n toolInputs?: OpenAI.Responses.ResponseInputItem[]\n ): Promise<void> {\n if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {\n console.error('[OpenaiAgent] Max calls reached')\n textPromise.reject(new Error('Max calls reached'))\n return\n }\n\n this.abortController = new AbortController()\n const signal = this.abortController.signal\n\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n\n // Build input\n const input: OpenAI.Responses.ResponseInput = this.conversation.map(\n (message) => ({\n role: message.role,\n content: message.content,\n })\n )\n if (toolInputs) {\n input.push(...toolInputs)\n }\n\n try {\n // Generate answer\n const response = await this.openai.responses.create(\n {\n model: this.options.model || DEFAULT_MODEL,\n input,\n temperature: 0.5,\n max_output_tokens: 250,\n stream: true,\n tools: enableTools ? this.tools.map(createToolSchema) : undefined,\n ...this.options.settings,\n },\n { signal }\n )\n const toolOutputs: OpenAI.Responses.ResponseInputItem[] = []\n\n // Handle response events\n for await (const event of response) {\n switch (event.type) {\n case 'response.output_text.delta':\n this.log(`Answer chunk: \"${event.delta}\"`)\n stream.write(event.delta)\n break\n\n case 'response.output_text.done':\n this.addAssistantMessage(event.text)\n stream.end()\n textPromise.resolve(event.text)\n break\n\n case 'response.output_item.done':\n if (event.item.type === 'function_call') {\n toolOutputs.push(event.item)\n const toolOutput = await this.executeTool(event.item)\n toolOutputs.push(toolOutput)\n }\n break\n\n default:\n break\n }\n }\n\n // Query again in case of tool call\n if (toolOutputs.length > 0) {\n await this.generateAnswer(\n stream,\n textPromise,\n callCount + 1,\n toolOutputs\n )\n }\n } catch (error) {\n textPromise.reject(error)\n if (!(error instanceof OpenAI.APIUserAbortError)) {\n console.error('[OpenaiAgent] Error answering:', error)\n }\n }\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n this.tools = this.tools.filter((tool) => tool.name !== name)\n }\n\n private getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n callback: () => this.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n callback: () => this.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n callback: () => this.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n private async executeTool(toolCall: ToolCall): Promise<ToolCallOutput> {\n try {\n const tool = this.tools.find((tool) => tool.name === toolCall.name)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.name}\"`)\n }\n\n this.log('Executing tool:', toolCall.name, toolCall.arguments)\n const args = JSON.parse(toolCall.arguments)\n const result = await tool.callback(args)\n return createToolCallOutput(toolCall, result || {})\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return createToolCallOutput(toolCall, { error: error.message })\n }\n }\n\n cancel() {\n if (!this.abortController) return\n this.log('Cancel')\n this.abortController.abort()\n this.abortController = undefined\n }\n}\n","import {\n FunctionTool,\n ResponseInputItem,\n Responses,\n} from 'openai/resources/responses/responses'\nimport { toJSONSchema, z } from 'zod'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n parameters?: Schema\n callback: (input: z.infer<Schema>) => any | Promise<any>\n}\n\nexport type ToolCall = Responses.ResponseFunctionToolCall\nexport type ToolCallOutput = ResponseInputItem.FunctionCallOutput\n\nexport function createToolSchema<Schema extends z.ZodObject>(\n tool: Tool<Schema>\n): FunctionTool {\n return {\n type: 'function',\n name: tool.name,\n description: tool.description,\n strict: true,\n parameters: toJSONSchema(tool.parameters || z.object()),\n }\n}\n\nexport function createToolCallOutput(\n toolCall: ToolCall,\n args: any\n): ToolCallOutput {\n return {\n type: 'function_call_output',\n call_id: toolCall.call_id,\n output: JSON.stringify(args),\n }\n}\n","import { FileSTT } from '@micdrop/server'\n\n/**\n * OpenAI Whisper STT\n *\n * @see https://platform.openai.com/docs/guides/speech-to-text?lang=curl\n */\n\nexport interface OpenaiSTTOptions {\n apiKey: string\n model?: string\n language?: string\n}\n\nconst DEFAULT_MODEL = 'whisper-1'\n\nexport class OpenaiSTT extends FileSTT {\n constructor(private options: OpenaiSTTOptions) {\n super()\n }\n\n async transcribeFile(file: File) {\n // Prepare form data\n const formData = new FormData()\n formData.append('file', file)\n formData.append('model', this.options.model || DEFAULT_MODEL)\n formData.append('response_format', 'text')\n\n if (this.options.language) {\n formData.append('language', this.options.language)\n }\n\n const response = await fetch(\n 'https://api.openai.com/v1/audio/transcriptions',\n {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n body: formData,\n }\n )\n\n if (!response.ok) {\n this.log('Failed to transcribe audio', await response.json())\n throw new Error(`Failed to transcribe audio: ${response.statusText}`)\n }\n\n const transcript = await response.text()\n this.log(`Received transcript: \"${transcript}\"`)\n\n return transcript\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAiD;AACjD,oBAAmB;AACnB,oBAA4B;;;ACG5B,iBAAgC;AAYzB,SAAS,iBACd,MACc;AACd,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,QAAQ;AAAA,IACR,gBAAY,yBAAa,KAAK,cAAc,aAAE,OAAO,CAAC;AAAA,EACxD;AACF;AAEO,SAAS,qBACd,UACA,MACgB;AAChB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS,SAAS;AAAA,IAClB,QAAQ,KAAK,UAAU,IAAI;AAAA,EAC7B;AACF;;;ADJA,IAAM,gBAAgB;AACtB,IAAM,oBAAoB;AAE1B,IAAM,0BAA0B;AAChC,IAAM,uBAAuB;AAE7B,I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wB;AAcxB,IAAMC,iBAAgB;AAEf,IAAM,YAAN,cAAwB,uBAAQ;AAAA,EACrC,YAAoB,SAA2B;AAC7C,UAAM;AADY;AAAA,EAEpB;AAAA,EAEA,MAAM,eAAe,MAAY;AAE/B,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,QAAQ,IAAI;AAC5B,aAAS,OAAO,SAAS,KAAK,QAAQ,SAASA,cAAa;AAC5D,aAAS,OAAO,mBAAmB,MAAM;AAEzC,QAAI,KAAK,QAAQ,UAAU;AACzB,eAAS,OAAO,YAAY,KAAK,QAAQ,QAAQ;AAAA,IACnD;AAEA,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,eAAe,UAAU,KAAK,QAAQ,MAAM;AAAA,QAC9C;AAAA,QACA,MAAM;AAAA,MACR;AAAA,IACF;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,WAAK,IAAI,8BAA8B,MAAM,SAAS,KAAK,CAAC;AAC5D,YAAM,IAAI,MAAM,+BAA+B,SAAS,UAAU,EAAE;AAAA,IACtE;AAEA,UAAM,aAAa,MAAM,SAAS,KAAK;AACvC,SAAK,IAAI,yBAAyB,UAAU,GAAG;AAE/C,WAAO;AAAA,EACT;AACF;","names":["OpenAI","tool","import_server","DEFAULT_MODEL"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/OpenaiAgent.ts","../src/tools.ts","../src/OpenaiSTT.ts"],"sourcesContent":["export * from './OpenaiAgent'\nexport * from './OpenaiSTT'\n","import { Agent, AgentOptions } from '@micdrop/server'\nimport OpenAI from 'openai'\nimport { PassThrough } from 'stream'\nimport { z } from 'zod'\nimport {\n createToolCallOutput,\n createToolSchema,\n Tool,\n ToolCall,\n ToolCallOutput,\n} from './tools'\n\nexport interface OpenaiAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n maxCalls?: number\n settings?: Omit<\n OpenAI.Responses.ResponseCreateParamsStreaming,\n 'input' | 'model'\n >\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n}\n\nconst DEFAULT_MODEL = 'gpt-4o'\nconst DEFAULT_MAX_CALLS = 5\n\nconst AUTO_END_CALL_TOOL_NAME = 'end_call'\nconst AUTO_END_CALL_PROMPT = 'Call this tool only if user asks to end the call'\n\nconst AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nconst AUTO_SEMANTIC_TURN_PROMPT =\n 'Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering'\n\nconst AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nconst AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n\nexport class OpenaiAgent extends Agent<OpenaiAgentOptions> {\n private openai: OpenAI\n private tools: Tool[]\n private abortController?: AbortController\n\n constructor(options: OpenaiAgentOptions) {\n super(options)\n this.openai = new OpenAI({ apiKey: options.apiKey })\n this.tools = this.getDefaultTools()\n }\n\n answer() {\n this.cancel()\n this.log('Start answering')\n const stream = new PassThrough()\n\n this.generateAnswer(stream).finally(() => {\n this.abortController = undefined\n if (stream.writable) {\n stream.end()\n }\n })\n\n return stream\n }\n\n private async generateAnswer(\n stream: PassThrough,\n callCount = 0,\n toolInputs?: Array<ToolCall | ToolCallOutput>\n ): Promise<void> {\n if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {\n console.error('[OpenaiAgent] Max calls reached')\n return\n }\n\n this.abortController = new AbortController()\n const signal = this.abortController.signal\n\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n\n // Build input\n const input: OpenAI.Responses.ResponseInput = this.conversation.map(\n (message) => ({\n role: message.role,\n content: message.content,\n })\n )\n if (toolInputs) {\n input.push(...toolInputs)\n }\n\n try {\n // Generate answer\n const response = await this.openai.responses.create(\n {\n model: this.options.model || DEFAULT_MODEL,\n input,\n temperature: 0.5,\n max_output_tokens: 250,\n stream: true,\n tools: enableTools ? this.tools.map(createToolSchema) : undefined,\n ...this.options.settings,\n },\n { signal }\n )\n const toolOutputs: Array<ToolCall | ToolCallOutput> = []\n let hasAnswer = false\n let skipAnswer = false\n\n // Handle response events\n for await (const event of response) {\n switch (event.type) {\n case 'response.output_text.delta':\n this.log(`Answer chunk: \"${event.delta}\"`)\n stream.write(event.delta)\n break\n\n case 'response.output_text.done':\n this.addAssistantMessage(event.text)\n stream.end()\n hasAnswer = true\n break\n\n case 'response.output_item.done':\n if (event.item.type === 'function_call') {\n const result = await this.executeTool(event.item)\n toolOutputs.push(result.toolCall, result.toolCallOutput)\n if (result.skipAnswer) {\n skipAnswer = true\n }\n }\n break\n\n default:\n break\n }\n }\n\n // Query again in case of tool call\n if (toolOutputs.length > 0 && !hasAnswer && !skipAnswer) {\n await this.generateAnswer(stream, callCount + 1, toolOutputs)\n }\n } catch (error) {\n if (!(error instanceof OpenAI.APIUserAbortError)) {\n console.error('[OpenaiAgent] Error answering:', error)\n }\n }\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n this.tools = this.tools.filter((tool) => tool.name !== name)\n }\n\n getTool(name: string): Tool | undefined {\n return this.tools.find((tool) => tool.name === name)\n }\n\n private getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n callback: () => this.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n skipAnswer: true,\n callback: () => this.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n skipAnswer: true,\n callback: () => this.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n private async executeTool(toolCall: ToolCall) {\n try {\n const tool = this.getTool(toolCall.name)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.name}\"`)\n }\n\n this.log('Executing tool:', toolCall.name, toolCall.arguments)\n const args = JSON.parse(toolCall.arguments)\n const result = await tool.callback(args)\n\n return {\n toolCall,\n toolCallOutput: createToolCallOutput(toolCall, result || {}),\n skipAnswer: tool.skipAnswer,\n }\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return {\n toolCall,\n toolCallOutput: createToolCallOutput(toolCall, {\n error: error.message,\n }),\n }\n }\n }\n\n cancel() {\n if (!this.abortController) return\n this.log('Cancel')\n this.abortController.abort()\n this.abortController = undefined\n }\n}\n","import {\n FunctionTool,\n ResponseInputItem,\n Responses,\n} from 'openai/resources/responses/responses'\nimport { toJSONSchema, z } from 'zod'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n parameters?: Schema\n callback: (input: z.infer<Schema>) => any | Promise<any>\n skipAnswer?: boolean\n}\n\nexport type ToolCall = Responses.ResponseFunctionToolCall\nexport type ToolCallOutput = ResponseInputItem.FunctionCallOutput\n\nexport function createToolSchema<Schema extends z.ZodObject>(\n tool: Tool<Schema>\n): FunctionTool {\n return {\n type: 'function',\n name: tool.name,\n description: tool.description,\n strict: true,\n parameters: toJSONSchema(tool.parameters || z.object()),\n }\n}\n\nexport function createToolCallOutput(\n toolCall: ToolCall,\n args: any\n): ToolCallOutput {\n return {\n type: 'function_call_output',\n call_id: toolCall.call_id,\n output: JSON.stringify(args),\n }\n}\n","import { FileSTT } from '@micdrop/server'\n\n/**\n * OpenAI Whisper STT\n *\n * @see https://platform.openai.com/docs/guides/speech-to-text?lang=curl\n */\n\nexport interface OpenaiSTTOptions {\n apiKey: string\n model?: string\n language?: string\n}\n\nconst DEFAULT_MODEL = 'whisper-1'\n\nexport class OpenaiSTT extends FileSTT {\n constructor(private options: OpenaiSTTOptions) {\n super()\n }\n\n async transcribeFile(file: File) {\n // Prepare form data\n const formData = new FormData()\n formData.append('file', file)\n formData.append('model', this.options.model || DEFAULT_MODEL)\n formData.append('response_format', 'text')\n\n if (this.options.language) {\n formData.append('language', this.options.language)\n }\n\n const response = await fetch(\n 'https://api.openai.com/v1/audio/transcriptions',\n {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n body: formData,\n }\n )\n\n if (!response.ok) {\n this.log('Failed to transcribe audio', await response.json())\n throw new Error(`Failed to transcribe audio: ${response.statusText}`)\n }\n\n const transcript = await response.text()\n this.log(`Received transcript: \"${transcript}\"`)\n\n return transcript\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoC;AACpC,oBAAmB;AACnB,oBAA4B;;;ACG5B,iBAAgC;AAazB,SAAS,iBACd,MACc;AACd,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,QAAQ;AAAA,IACR,gBAAY,yBAAa,KAAK,cAAc,aAAE,OAAO,CAAC;AAAA,EACxD;AACF;AAEO,SAAS,qBACd,UACA,MACgB;AAChB,SAAO;AAAA,IACL,MAA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AAC1D,aAAO;AAAA,QACL;AAAA,QACA,gBAAgB,qBAAqB,UAAU;AAAA,UAC7C,OAAO,MAAM;AAAA,QACf,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA,EAEA,SAAS;AACP,QAAI,CAAC,KAAK,gBAAiB;AAC3B,SAAK,IAAI,QAAQ;AACjB,SAAK,gBAAgB,MAAM;AAC3B,SAAK,kBAAkB;AAAA,EACzB;AACF;;;AEjPA,IAAAC,iBAAwB;AAcxB,IAAMC,iBAAgB;AAEf,IAAM,YAAN,cAAwB,uBAAQ;AAAA,EACrC,YAAoB,SAA2B;AAC7C,UAAM;AADY;AAAA,EAEpB;AAAA,EAEA,MAAM,eAAe,MAAY;AAE/B,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,QAAQ,IAAI;AAC5B,aAAS,OAAO,SAAS,KAAK,QAAQ,SAASA,cAAa;AAC5D,aAAS,OAAO,mBAAmB,MAAM;AAEzC,QAAI,KAAK,QAAQ,UAAU;AACzB,eAAS,OAAO,YAAY,KAAK,QAAQ,QAAQ;AAAA,IACnD;AAEA,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,eAAe,UAAU,KAAK,QAAQ,MAAM;AAAA,QAC9C;AAAA,QACA,MAAM;AAAA,MACR;AAAA,IACF;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,WAAK,IAAI,8BAA8B,MAAM,SAAS,KAAK,CAAC;AAC5D,YAAM,IAAI,MAAM,+BAA+B,SAAS,UAAU,EAAE;AAAA,IACtE;AAEA,UAAM,aAAa,MAAM,SAAS,KAAK;AACvC,SAAK,IAAI,yBAAyB,UAAU,GAAG;AAE/C,WAAO;AAAA,EACT;AACF;","names":["OpenAI","import_server","DEFAULT_MODEL"]}
package/dist/index.mjs CHANGED
@@ -26,11 +26,11 @@ function createToolCallOutput(toolCall, args) {
26
26
  var DEFAULT_MODEL = "gpt-4o";
27
27
  var DEFAULT_MAX_CALLS = 5;
28
28
  var AUTO_END_CALL_TOOL_NAME = "end_call";
29
- var AUTO_END_CALL_PROMPT = "User asks to end the call";
29
+ var AUTO_END_CALL_PROMPT = "Call this tool only if user asks to end the call";
30
30
  var AUTO_SEMANTIC_TURN_TOOL_NAME = "semantic_turn";
31
- var AUTO_SEMANTIC_TURN_PROMPT = "Last user message is an incomplete sentence";
31
+ var AUTO_SEMANTIC_TURN_PROMPT = "Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering";
32
32
  var AUTO_IGNORE_USER_NOISE_TOOL_NAME = "ignore_user_noise";
33
- var AUTO_IGNORE_USER_NOISE_PROMPT = `Last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: "Uh", "Ahem", "Hmm", "Ah") but doesn't carry any clear meaning like agreeing, refusing, or commanding`;
33
+ var AUTO_IGNORE_USER_NOISE_PROMPT = `Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: "Uh", "Ahem", "Hmm", "Ah") but doesn't carry any clear meaning like agreeing, refusing, or commanding`;
34
34
  var OpenaiAgent = class extends Agent {
35
35
  constructor(options) {
36
36
  super(options);
@@ -41,19 +41,17 @@ var OpenaiAgent = class extends Agent {
41
41
  this.cancel();
42
42
  this.log("Start answering");
43
43
  const stream = new PassThrough();
44
- const textPromise = this.createTextPromise();
45
- this.generateAnswer(stream, textPromise).finally(() => {
44
+ this.generateAnswer(stream).finally(() => {
46
45
  this.abortController = void 0;
47
46
  if (stream.writable) {
48
47
  stream.end();
49
48
  }
50
49
  });
51
- return { message: textPromise.promise, stream };
50
+ return stream;
52
51
  }
53
- async generateAnswer(stream, textPromise, callCount = 0, toolInputs) {
52
+ async generateAnswer(stream, callCount = 0, toolInputs) {
54
53
  if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {
55
54
  console.error("[OpenaiAgent] Max calls reached");
56
- textPromise.reject(new Error("Max calls reached"));
57
55
  return;
58
56
  }
59
57
  this.abortController = new AbortController();
@@ -82,6 +80,8 @@ var OpenaiAgent = class extends Agent {
82
80
  { signal }
83
81
  );
84
82
  const toolOutputs = [];
83
+ let hasAnswer = false;
84
+ let skipAnswer = false;
85
85
  for await (const event of response) {
86
86
  switch (event.type) {
87
87
  case "response.output_text.delta":
@@ -91,29 +91,25 @@ var OpenaiAgent = class extends Agent {
91
91
  case "response.output_text.done":
92
92
  this.addAssistantMessage(event.text);
93
93
  stream.end();
94
- textPromise.resolve(event.text);
94
+ hasAnswer = true;
95
95
  break;
96
96
  case "response.output_item.done":
97
97
  if (event.item.type === "function_call") {
98
- toolOutputs.push(event.item);
99
- const toolOutput = await this.executeTool(event.item);
100
- toolOutputs.push(toolOutput);
98
+ const result = await this.executeTool(event.item);
99
+ toolOutputs.push(result.toolCall, result.toolCallOutput);
100
+ if (result.skipAnswer) {
101
+ skipAnswer = true;
102
+ }
101
103
  }
102
104
  break;
103
105
  default:
104
106
  break;
105
107
  }
106
108
  }
107
- if (toolOutputs.length > 0) {
108
- await this.generateAnswer(
109
- stream,
110
- textPromise,
111
- callCount + 1,
112
- toolOutputs
113
- );
109
+ if (toolOutputs.length > 0 && !hasAnswer && !skipAnswer) {
110
+ await this.generateAnswer(stream, callCount + 1, toolOutputs);
114
111
  }
115
112
  } catch (error) {
116
- textPromise.reject(error);
117
113
  if (!(error instanceof OpenAI.APIUserAbortError)) {
118
114
  console.error("[OpenaiAgent] Error answering:", error);
119
115
  }
@@ -125,6 +121,9 @@ var OpenaiAgent = class extends Agent {
125
121
  removeTool(name) {
126
122
  this.tools = this.tools.filter((tool) => tool.name !== name);
127
123
  }
124
+ getTool(name) {
125
+ return this.tools.find((tool) => tool.name === name);
126
+ }
128
127
  getDefaultTools() {
129
128
  const tools = [];
130
129
  if (this.options.autoEndCall) {
@@ -138,6 +137,7 @@ var OpenaiAgent = class extends Agent {
138
137
  tools.push({
139
138
  name: AUTO_SEMANTIC_TURN_TOOL_NAME,
140
139
  description: typeof this.options.autoSemanticTurn === "string" ? this.options.autoSemanticTurn : AUTO_SEMANTIC_TURN_PROMPT,
140
+ skipAnswer: true,
141
141
  callback: () => this.skipAnswer()
142
142
  });
143
143
  }
@@ -145,6 +145,7 @@ var OpenaiAgent = class extends Agent {
145
145
  tools.push({
146
146
  name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,
147
147
  description: typeof this.options.autoIgnoreUserNoise === "string" ? this.options.autoIgnoreUserNoise : AUTO_IGNORE_USER_NOISE_PROMPT,
148
+ skipAnswer: true,
148
149
  callback: () => this.cancelLastUserMessage()
149
150
  });
150
151
  }
@@ -152,17 +153,26 @@ var OpenaiAgent = class extends Agent {
152
153
  }
153
154
  async executeTool(toolCall) {
154
155
  try {
155
- const tool = this.tools.find((tool2) => tool2.name === toolCall.name);
156
+ const tool = this.getTool(toolCall.name);
156
157
  if (!tool) {
157
158
  throw new Error(`Tool not found "${toolCall.name}"`);
158
159
  }
159
160
  this.log("Executing tool:", toolCall.name, toolCall.arguments);
160
161
  const args = JSON.parse(toolCall.arguments);
161
162
  const result = await tool.callback(args);
162
- return createToolCallOutput(toolCall, result || {});
163
+ return {
164
+ toolCall,
165
+ toolCallOutput: createToolCallOutput(toolCall, result || {}),
166
+ skipAnswer: tool.skipAnswer
167
+ };
163
168
  } catch (error) {
164
169
  console.error("[OpenaiAgent] Error executing tool:", error);
165
- return createToolCallOutput(toolCall, { error: error.message });
170
+ return {
171
+ toolCall,
172
+ toolCallOutput: createToolCallOutput(toolCall, {
173
+ error: error.message
174
+ })
175
+ };
166
176
  }
167
177
  }
168
178
  cancel() {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/OpenaiAgent.ts","../src/tools.ts","../src/OpenaiSTT.ts"],"sourcesContent":["import { Agent, AgentOptions, TextPromise } from '@micdrop/server'\nimport OpenAI from 'openai'\nimport { PassThrough } from 'stream'\nimport { z } from 'zod'\nimport {\n createToolCallOutput,\n createToolSchema,\n Tool,\n ToolCall,\n ToolCallOutput,\n} from './tools'\n\nexport interface OpenaiAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n maxCalls?: number\n settings?: Omit<\n OpenAI.Responses.ResponseCreateParamsStreaming,\n 'input' | 'model'\n >\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n}\n\nconst DEFAULT_MODEL = 'gpt-4o'\nconst DEFAULT_MAX_CALLS = 5\n\nconst AUTO_END_CALL_TOOL_NAME = 'end_call'\nconst AUTO_END_CALL_PROMPT = 'User asks to end the call'\n\nconst AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nconst AUTO_SEMANTIC_TURN_PROMPT = 'Last user message is an incomplete sentence'\n\nconst AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nconst AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n\nexport class OpenaiAgent extends Agent<OpenaiAgentOptions> {\n private openai: OpenAI\n private tools: Tool[]\n private abortController?: AbortController\n\n constructor(options: OpenaiAgentOptions) {\n super(options)\n this.openai = new OpenAI({ apiKey: options.apiKey })\n this.tools = this.getDefaultTools()\n }\n\n answer() {\n this.cancel()\n this.log('Start answering')\n const stream = new PassThrough()\n const textPromise = this.createTextPromise()\n\n this.generateAnswer(stream, textPromise).finally(() => {\n this.abortController = undefined\n if (stream.writable) {\n stream.end()\n }\n })\n\n return { message: textPromise.promise, stream }\n }\n\n private async generateAnswer(\n stream: PassThrough,\n textPromise: TextPromise,\n callCount = 0,\n toolInputs?: OpenAI.Responses.ResponseInputItem[]\n ): Promise<void> {\n if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {\n console.error('[OpenaiAgent] Max calls reached')\n textPromise.reject(new Error('Max calls reached'))\n return\n }\n\n this.abortController = new AbortController()\n const signal = this.abortController.signal\n\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n\n // Build input\n const input: OpenAI.Responses.ResponseInput = this.conversation.map(\n (message) => ({\n role: message.role,\n content: message.content,\n })\n )\n if (toolInputs) {\n input.push(...toolInputs)\n }\n\n try {\n // Generate answer\n const response = await this.openai.responses.create(\n {\n model: this.options.model || DEFAULT_MODEL,\n input,\n temperature: 0.5,\n max_output_tokens: 250,\n stream: true,\n tools: enableTools ? this.tools.map(createToolSchema) : undefined,\n ...this.options.settings,\n },\n { signal }\n )\n const toolOutputs: OpenAI.Responses.ResponseInputItem[] = []\n\n // Handle response events\n for await (const event of response) {\n switch (event.type) {\n case 'response.output_text.delta':\n this.log(`Answer chunk: \"${event.delta}\"`)\n stream.write(event.delta)\n break\n\n case 'response.output_text.done':\n this.addAssistantMessage(event.text)\n stream.end()\n textPromise.resolve(event.text)\n break\n\n case 'response.output_item.done':\n if (event.item.type === 'function_call') {\n toolOutputs.push(event.item)\n const toolOutput = await this.executeTool(event.item)\n toolOutputs.push(toolOutput)\n }\n break\n\n default:\n break\n }\n }\n\n // Query again in case of tool call\n if (toolOutputs.length > 0) {\n await this.generateAnswer(\n stream,\n textPromise,\n callCount + 1,\n toolOutputs\n )\n }\n } catch (error) {\n textPromise.reject(error)\n if (!(error instanceof OpenAI.APIUserAbortError)) {\n console.error('[OpenaiAgent] Error answering:', error)\n }\n }\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n this.tools = this.tools.filter((tool) => tool.name !== name)\n }\n\n private getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n callback: () => this.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n callback: () => this.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n callback: () => this.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n private async executeTool(toolCall: ToolCall): Promise<ToolCallOutput> {\n try {\n const tool = this.tools.find((tool) => tool.name === toolCall.name)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.name}\"`)\n }\n\n this.log('Executing tool:', toolCall.name, toolCall.arguments)\n const args = JSON.parse(toolCall.arguments)\n const result = await tool.callback(args)\n return createToolCallOutput(toolCall, result || {})\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return createToolCallOutput(toolCall, { error: error.message })\n }\n }\n\n cancel() {\n if (!this.abortController) return\n this.log('Cancel')\n this.abortController.abort()\n this.abortController = undefined\n }\n}\n","import {\n FunctionTool,\n ResponseInputItem,\n Responses,\n} from 'openai/resources/responses/responses'\nimport { toJSONSchema, z } from 'zod'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n parameters?: Schema\n callback: (input: z.infer<Schema>) => any | Promise<any>\n}\n\nexport type ToolCall = Responses.ResponseFunctionToolCall\nexport type ToolCallOutput = ResponseInputItem.FunctionCallOutput\n\nexport function createToolSchema<Schema extends z.ZodObject>(\n tool: Tool<Schema>\n): FunctionTool {\n return {\n type: 'function',\n name: tool.name,\n description: tool.description,\n strict: true,\n parameters: toJSONSchema(tool.parameters || z.object()),\n }\n}\n\nexport function createToolCallOutput(\n toolCall: ToolCall,\n args: any\n): ToolCallOutput {\n return {\n type: 'function_call_output',\n call_id: toolCall.call_id,\n output: JSON.stringify(args),\n }\n}\n","import { FileSTT } from '@micdrop/server'\n\n/**\n * OpenAI Whisper STT\n *\n * @see https://platform.openai.com/docs/guides/speech-to-text?lang=curl\n */\n\nexport interface OpenaiSTTOptions {\n apiKey: string\n model?: string\n language?: string\n}\n\nconst DEFAULT_MODEL = 'whisper-1'\n\nexport class OpenaiSTT extends FileSTT {\n constructor(private options: OpenaiSTTOptions) {\n super()\n }\n\n async transcribeFile(file: File) {\n // Prepare form data\n const formData = new FormData()\n formData.append('file', file)\n formData.append('model', this.options.model || DEFAULT_MODEL)\n formData.append('response_format', 'text')\n\n if (this.options.language) {\n formData.append('language', this.options.language)\n }\n\n const response = await fetch(\n 'https://api.openai.com/v1/audio/transcriptions',\n {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n body: formData,\n }\n )\n\n if (!response.ok) {\n this.log('Failed to transcribe audio', await response.json())\n throw new Error(`Failed to transcribe audio: ${response.statusText}`)\n }\n\n const transcript = await response.text()\n this.log(`Received transcript: \"${transcript}\"`)\n\n return transcript\n }\n}\n"],"mappings":";AAAA,SAAS,aAAwC;AACjD,OAAO,YAAY;AACnB,SAAS,mBAAmB;;;ACG5B,SAAS,cAAc,SAAS;AAYzB,SAAS,iBACd,MACc;AACd,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,QAAQ;AAAA,IACR,YAAY,aAAa,KAAK,cAAc,EAAE,OAAO,CAAC;AAAA,EACxD;AACF;AAEO,SAAS,qBACd,UACA,MACgB;AAChB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS,SAAS;AAAA,IAClB,QAAQ,KAAK,UAAU,IAAI;AAAA,EAC7B;AACF;;;ADJA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AgB,MAAM;AAC3B,SAAK,kBAAkB;AAAA,EACzB;AACF;;;AErOA,SAAS,eAAe;AAcxB,IAAMC,iBAAgB;AAEf,IAAM,YAAN,cAAwB,QAAQ;AAAA,EACrC,YAAoB,SAA2B;AAC7C,UAAM;AADY;AAAA,EAEpB;AAAA,EAEA,MAAM,eAAe,MAAY;AAE/B,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,QAAQ,IAAI;AAC5B,aAAS,OAAO,SAAS,KAAK,QAAQ,SAASA,cAAa;AAC5D,aAAS,OAAO,mBAAmB,MAAM;AAEzC,QAAI,KAAK,QAAQ,UAAU;AACzB,eAAS,OAAO,YAAY,KAAK,QAAQ,QAAQ;AAAA,IACnD;AAEA,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,eAAe,UAAU,KAAK,QAAQ,MAAM;AAAA,QAC9C;AAAA,QACA,MAAM;AAAA,MACR;AAAA,IACF;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,WAAK,IAAI,8BAA8B,MAAM,SAAS,KAAK,CAAC;AAC5D,YAAM,IAAI,MAAM,+BAA+B,SAAS,UAAU,EAAE;AAAA,IACtE;AAEA,UAAM,aAAa,MAAM,SAAS,KAAK;AACvC,SAAK,IAAI,yBAAyB,UAAU,GAAG;AAE/C,WAAO;AAAA,EACT;AACF;","names":["tool","DEFAULT_MODEL"]}
1
+ {"version":3,"sources":["../src/OpenaiAgent.ts","../src/tools.ts","../src/OpenaiSTT.ts"],"sourcesContent":["import { Agent, AgentOptions } from '@micdrop/server'\nimport OpenAI from 'openai'\nimport { PassThrough } from 'stream'\nimport { z } from 'zod'\nimport {\n createToolCallOutput,\n createToolSchema,\n Tool,\n ToolCall,\n ToolCallOutput,\n} from './tools'\n\nexport interface OpenaiAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n maxCalls?: number\n settings?: Omit<\n OpenAI.Responses.ResponseCreateParamsStreaming,\n 'input' | 'model'\n >\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n}\n\nconst DEFAULT_MODEL = 'gpt-4o'\nconst DEFAULT_MAX_CALLS = 5\n\nconst AUTO_END_CALL_TOOL_NAME = 'end_call'\nconst AUTO_END_CALL_PROMPT = 'Call this tool only if user asks to end the call'\n\nconst AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nconst AUTO_SEMANTIC_TURN_PROMPT =\n 'Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering'\n\nconst AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nconst AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n\nexport class OpenaiAgent extends Agent<OpenaiAgentOptions> {\n private openai: OpenAI\n private tools: Tool[]\n private abortController?: AbortController\n\n constructor(options: OpenaiAgentOptions) {\n super(options)\n this.openai = new OpenAI({ apiKey: options.apiKey })\n this.tools = this.getDefaultTools()\n }\n\n answer() {\n this.cancel()\n this.log('Start answering')\n const stream = new PassThrough()\n\n this.generateAnswer(stream).finally(() => {\n this.abortController = undefined\n if (stream.writable) {\n stream.end()\n }\n })\n\n return stream\n }\n\n private async generateAnswer(\n stream: PassThrough,\n callCount = 0,\n toolInputs?: Array<ToolCall | ToolCallOutput>\n ): Promise<void> {\n if (callCount >= (this.options.maxCalls || DEFAULT_MAX_CALLS)) {\n console.error('[OpenaiAgent] Max calls reached')\n return\n }\n\n this.abortController = new AbortController()\n const signal = this.abortController.signal\n\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n\n // Build input\n const input: OpenAI.Responses.ResponseInput = this.conversation.map(\n (message) => ({\n role: message.role,\n content: message.content,\n })\n )\n if (toolInputs) {\n input.push(...toolInputs)\n }\n\n try {\n // Generate answer\n const response = await this.openai.responses.create(\n {\n model: this.options.model || DEFAULT_MODEL,\n input,\n temperature: 0.5,\n max_output_tokens: 250,\n stream: true,\n tools: enableTools ? this.tools.map(createToolSchema) : undefined,\n ...this.options.settings,\n },\n { signal }\n )\n const toolOutputs: Array<ToolCall | ToolCallOutput> = []\n let hasAnswer = false\n let skipAnswer = false\n\n // Handle response events\n for await (const event of response) {\n switch (event.type) {\n case 'response.output_text.delta':\n this.log(`Answer chunk: \"${event.delta}\"`)\n stream.write(event.delta)\n break\n\n case 'response.output_text.done':\n this.addAssistantMessage(event.text)\n stream.end()\n hasAnswer = true\n break\n\n case 'response.output_item.done':\n if (event.item.type === 'function_call') {\n const result = await this.executeTool(event.item)\n toolOutputs.push(result.toolCall, result.toolCallOutput)\n if (result.skipAnswer) {\n skipAnswer = true\n }\n }\n break\n\n default:\n break\n }\n }\n\n // Query again in case of tool call\n if (toolOutputs.length > 0 && !hasAnswer && !skipAnswer) {\n await this.generateAnswer(stream, callCount + 1, toolOutputs)\n }\n } catch (error) {\n if (!(error instanceof OpenAI.APIUserAbortError)) {\n console.error('[OpenaiAgent] Error answering:', error)\n }\n }\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n this.tools = this.tools.filter((tool) => tool.name !== name)\n }\n\n getTool(name: string): Tool | undefined {\n return this.tools.find((tool) => tool.name === name)\n }\n\n private getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n callback: () => this.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n skipAnswer: true,\n callback: () => this.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n skipAnswer: true,\n callback: () => this.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n private async executeTool(toolCall: ToolCall) {\n try {\n const tool = this.getTool(toolCall.name)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.name}\"`)\n }\n\n this.log('Executing tool:', toolCall.name, toolCall.arguments)\n const args = JSON.parse(toolCall.arguments)\n const result = await tool.callback(args)\n\n return {\n toolCall,\n toolCallOutput: createToolCallOutput(toolCall, result || {}),\n skipAnswer: tool.skipAnswer,\n }\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return {\n toolCall,\n toolCallOutput: createToolCallOutput(toolCall, {\n error: error.message,\n }),\n }\n }\n }\n\n cancel() {\n if (!this.abortController) return\n this.log('Cancel')\n this.abortController.abort()\n this.abortController = undefined\n }\n}\n","import {\n FunctionTool,\n ResponseInputItem,\n Responses,\n} from 'openai/resources/responses/responses'\nimport { toJSONSchema, z } from 'zod'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n parameters?: Schema\n callback: (input: z.infer<Schema>) => any | Promise<any>\n skipAnswer?: boolean\n}\n\nexport type ToolCall = Responses.ResponseFunctionToolCall\nexport type ToolCallOutput = ResponseInputItem.FunctionCallOutput\n\nexport function createToolSchema<Schema extends z.ZodObject>(\n tool: Tool<Schema>\n): FunctionTool {\n return {\n type: 'function',\n name: tool.name,\n description: tool.description,\n strict: true,\n parameters: toJSONSchema(tool.parameters || z.object()),\n }\n}\n\nexport function createToolCallOutput(\n toolCall: ToolCall,\n args: any\n): ToolCallOutput {\n return {\n type: 'function_call_output',\n call_id: toolCall.call_id,\n output: JSON.stringify(args),\n }\n}\n","import { FileSTT } from '@micdrop/server'\n\n/**\n * OpenAI Whisper STT\n *\n * @see https://platform.openai.com/docs/guides/speech-to-text?lang=curl\n */\n\nexport interface OpenaiSTTOptions {\n apiKey: string\n model?: string\n language?: string\n}\n\nconst DEFAULT_MODEL = 'whisper-1'\n\nexport class OpenaiSTT extends FileSTT {\n constructor(private options: OpenaiSTTOptions) {\n super()\n }\n\n async transcribeFile(file: File) {\n // Prepare form data\n const formData = new FormData()\n formData.append('file', file)\n formData.append('model', this.options.model || DEFAULT_MODEL)\n formData.append('response_format', 'text')\n\n if (this.options.language) {\n formData.append('language', this.options.language)\n }\n\n const response = await fetch(\n 'https://api.openai.com/v1/audio/transcriptions',\n {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n body: formData,\n }\n )\n\n if (!response.ok) {\n this.log('Failed to transcribe audio', await response.json())\n throw new Error(`Failed to transcribe audio: ${response.statusText}`)\n }\n\n const transcript = await response.text()\n this.log(`Received transcript: \"${transcript}\"`)\n\n return transcript\n }\n}\n"],"mappings":";AAAA,SAAS,aAA2B;AACpC,OAAO,YAAY;AACnB,SAAS,mBAAmB;;;ACG5B,SAAS,cAAc,SAAS;AAazB,SAAS,iBACd,MACc;AACd,SAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM,KAAK;AAAA,IACX,aAAa,KAAK;AAAA,IAClB,QAAQ;AAAA,IACR,YAAY,aAAa,KAAK,cAAc,EAAE,OAAO,CAAC;AAAA,EACxD;AACF;AAEO,SAAS,qBACd,UACA,MACgB;AAChB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS,SAAS;AAAA,IAClB,QAAQ,KA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UAC7C,OAAO,MAAM;AAAA,QACf,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAAA,EAEA,SAAS;AACP,QAAI,CAAC,KAAK,gBAAiB;AAC3B,SAAK,IAAI,QAAQ;AACjB,SAAK,gBAAgB,MAAM;AAC3B,SAAK,kBAAkB;AAAA,EACzB;AACF;;;AEjPA,SAAS,eAAe;AAcxB,IAAMA,iBAAgB;AAEf,IAAM,YAAN,cAAwB,QAAQ;AAAA,EACrC,YAAoB,SAA2B;AAC7C,UAAM;AADY;AAAA,EAEpB;AAAA,EAEA,MAAM,eAAe,MAAY;AAE/B,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,QAAQ,IAAI;AAC5B,aAAS,OAAO,SAAS,KAAK,QAAQ,SAASA,cAAa;AAC5D,aAAS,OAAO,mBAAmB,MAAM;AAEzC,QAAI,KAAK,QAAQ,UAAU;AACzB,eAAS,OAAO,YAAY,KAAK,QAAQ,QAAQ;AAAA,IACnD;AAEA,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,eAAe,UAAU,KAAK,QAAQ,MAAM;AAAA,QAC9C;AAAA,QACA,MAAM;AAAA,MACR;AAAA,IACF;AAEA,QAAI,CAAC,SAAS,IAAI;AAChB,WAAK,IAAI,8BAA8B,MAAM,SAAS,KAAK,CAAC;AAC5D,YAAM,IAAI,MAAM,+BAA+B,SAAS,UAAU,EAAE;AAAA,IACtE;AAEA,UAAM,aAAa,MAAM,SAAS,KAAK;AACvC,SAAK,IAAI,yBAAyB,UAAU,GAAG;AAE/C,WAAO;AAAA,EACT;AACF;","names":["DEFAULT_MODEL"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/openai",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "OpenAI implementation for @micdrop/server",
5
5
  "author": "Lonestone",
6
6
  "license": "MIT",