-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
234 lines (217 loc) · 6.62 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import EventEmitter from 'events'
/**
* A run is a single conversation with an assistant.
* @fires Run#error - when an error occurs
* @fires Run#complete - when the run is complete
* @fires Run#cancelled - when the run is cancelled
*/
export default class Run extends EventEmitter {
#api
#openai
#threadId
#assistantId
#stream
#runId
#agentName
#status = 'queued'
#messages = []
/**
* Creates a new run
* @param {API} api The API instance
* @param {Object} openai The OpenAI instance
* @param {string} threadId The thread id on which to run
* @param {string} assistantId The assistant id to use for the run
* @param {string} agentName The agent name for debugging and messaging
* @emits Run#error
* @emits Run#complete
* @emits Run#cancelled
* @throws {Error} if the run cannot be created, started, or any other error occurs with the API.
*/
constructor({api, openai, threadId, assistantId, agentName}) {
super()
this.#api = api
this.#openai = openai
this.#threadId = threadId
this.#assistantId = assistantId
this.#agentName = agentName
}
/**
* The status of the run
* @type {string}
*/
get status() {
return this.#status
}
/**
* Adds a message to a run
* @param {Message} message
*/
async addMessage(message) {
let messagePrefix = ''
if (message.source !== 'user') {
messagePrefix = `From ${message.source}: `
}
const oaiMessage = await this.#openai.beta.threads.messages.create(this.#threadId, {
role: 'user',
content: messagePrefix + message.content,
metadata: {
source: message.source,
target: message.target,
id: message.id,
timestamp: message.timestamp.getTime(),
type: message.type.toString()
}
})
this.#api.log.trace('Created OpenAI message', oaiMessage, oaiMessage.content[0].text.value)
}
async start() {
this.#stream = await this.#openai.beta.threads.runs.create(this.#threadId, {
assistant_id: this.#assistantId,
stream: true
})
await this.#process()
}
async #process() {
let currentMessage
for await (const payload of this.#stream) {
switch(payload.event) {
case 'thread.run.created':
this.#runId = payload.data.id
break
case 'thread.run.queued':
case 'thread.run.in_progress':
case 'thread.run.cancelling':
case 'thread.run.step.created':
case 'thread.run.step.in_progress':
case 'thread.run.step.delta':
case 'thread.run.step.completed':
case 'thread.run.step.failed':
case 'thread.run.step.cancelled':
case 'thread.run.step.expired':
case 'thread.message.incomplete':
case 'thread.message.in_progress':
this.#api.log.trace('Unhandled/Ignored Streaming Event: ', payload.event)
break
case 'thread.run.completed':
await this.#onComplete()
break
case 'thread.run.cancelled':
this.#onCancel()
break
case 'thread.message.created':
currentMessage = this.#api.comms.createMessage('user', this.#agentName, '')
this.#messages.push(currentMessage)
break
case 'thread.message.delta':
if (payload.data.delta.content.length > 1) {
this.#api.log.warn('Unexpected content length', payload.data.delta.content)
}
if (!currentMessage) {
this.#api.log.error('Received message delta without a message')
break
}
currentMessage.append(payload.data.delta.content[0].text.value)
break
case 'thread.message.completed':
currentMessage = null
break
case 'thread.run.requires_action':
await this.#onActionRequired(payload.data)
break
case 'thread.run.failed':
case 'thread.run.expired':
case 'error':
this.#onError(payload.data)
return
case 'end':
await this.#onComplete()
return
default:
console.log('Event: ', payload)
}
}
}
async stop() {
await this.#openai.beta.threads.runs.cancel(this.#threadId, this.#runId)
}
/**
* Error event.
* @event Run#error
* @type {object}
* @property {string} message - The error message.
*/
/**
* Handles an error from the run
* @param {Object|Error} response
* @param {Object} [response.last_error]
* @param {string} [response.last_error.message]
* @fires Run#error
*/
#onError(response) {
if (response instanceof Error) {
this.emit('error', response.message, response)
} else if (response.last_error) {
this.emit('error', response.last_error.message, response)
} else {
this.emit('error', response, response)
}
}
/**
* Complete event.
* @event Run#complete
* @type {object}
* @property {Array} messages - The messages from the run.
*/
/**
* Handles the completion of a run
*/
async #onComplete() {
this.#api.log.trace('Run result:', this.#messages)
this.emit('complete', this.#messages)
this.#messages = []
}
#onCancel() {
this.#api.log.debug('Cancelled current OpenAI run')
this.emit('cancelled')
}
async #onActionRequired(runResult) {
if (runResult.required_action.type !== 'submit_tool_outputs') {
this.#api.log.debug('OpenAI run requires action but action type is unknown', runResult)
return
}
this.#api.log.debug('OpenAI run requires action', runResult)
const toolCalls = runResult.required_action.submit_tool_outputs.tool_calls
if (toolCalls.length === 0) {
this.#api.log.warn('OpenAI run requires action but no tool calls found', runResult)
return
}
const tool_outputs = []
for (const toolCall of toolCalls) {
this.#api.log.debug('OpenAI run requires action for tool call', toolCall)
switch (toolCall.type) {
case 'function':
tool_outputs.push({
tool_call_id: toolCall.id,
output: JSON.stringify(await this.#handleFunction(toolCall.function))
})
break
default:
this.#api.log.warn('OpenAI run requires action but tool call type is unknown', toolCall)
}
}
this.#stream = this.#openai.beta.threads.runs.submitToolOutputsStream(this.#threadId, runResult.id, {tool_outputs})
this.#api.log.debug('Submitted tool outputs for OpenAI run', runResult.id, tool_outputs)
await this.#process()
}
async #handleFunction(functionProperties) {
const name = functionProperties.name
const args = JSON.parse(functionProperties.arguments)
try {
return await this.#api.skills.execute(name, args, this.#agentName)
} catch (e) {
return {
error: e.message
}
}
}
}