@superinterface/react 1.1.8 → 2.0.0

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.
@@ -1,164 +1,439 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/mutationFns.ts
31
- var mutationFns_exports = {};
32
- __export(mutationFns_exports, {
33
- createMessageMutationFn: () => mutationFn,
34
- createRunMutationFn: () => mutationFn3,
35
- handleActionMutationFn: () => mutationFn2
36
- });
37
- module.exports = __toCommonJS(mutationFns_exports);
38
-
39
1
  // src/lib/ai/index.ts
40
- var import_openai = __toESM(require("openai"));
41
- var defaultClient = new import_openai.default({
42
- apiKey: process.env.OPENAI_API_KEY
43
- });
44
-
45
- // src/lib/runSteps/getRunSteps/index.ts
46
- var getRunSteps = async ({
47
- threadId,
48
- runId,
49
- client
50
- }) => {
51
- const runStepsResponse = await client.beta.threads.runs.steps.list(
52
- threadId,
53
- runId
54
- );
55
- return runStepsResponse.data;
56
- };
57
-
58
- // src/lib/messages/extendMessage.ts
59
- var extendMessage = async ({
60
- message,
61
- client
62
- }) => {
63
- if (!message.run_id) {
64
- return {
65
- ...message,
66
- runSteps: []
2
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
+ try {
4
+ var info = gen[key](arg);
5
+ var value = info.value;
6
+ } catch (error) {
7
+ reject(error);
8
+ return;
9
+ }
10
+ if (info.done) {
11
+ resolve(value);
12
+ } else {
13
+ Promise.resolve(value).then(_next, _throw);
14
+ }
15
+ }
16
+ function _async_to_generator(fn) {
17
+ return function() {
18
+ var self = this, args = arguments;
19
+ return new Promise(function(resolve, reject) {
20
+ var gen = fn.apply(self, args);
21
+ function _next(value) {
22
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
23
+ }
24
+ function _throw(err) {
25
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
26
+ }
27
+ _next(undefined);
28
+ });
29
+ };
30
+ }
31
+ function _define_property(obj, key, value) {
32
+ if (key in obj) {
33
+ Object.defineProperty(obj, key, {
34
+ value: value,
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true
38
+ });
39
+ } else {
40
+ obj[key] = value;
41
+ }
42
+ return obj;
43
+ }
44
+ function _object_spread(target) {
45
+ for(var i = 1; i < arguments.length; i++){
46
+ var source = arguments[i] != null ? arguments[i] : {};
47
+ var ownKeys = Object.keys(source);
48
+ if (typeof Object.getOwnPropertySymbols === "function") {
49
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
50
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
51
+ }));
52
+ }
53
+ ownKeys.forEach(function(key) {
54
+ _define_property(target, key, source[key]);
55
+ });
56
+ }
57
+ return target;
58
+ }
59
+ function ownKeys(object, enumerableOnly) {
60
+ var keys = Object.keys(object);
61
+ if (Object.getOwnPropertySymbols) {
62
+ var symbols = Object.getOwnPropertySymbols(object);
63
+ if (enumerableOnly) {
64
+ symbols = symbols.filter(function(sym) {
65
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
66
+ });
67
+ }
68
+ keys.push.apply(keys, symbols);
69
+ }
70
+ return keys;
71
+ }
72
+ function _object_spread_props(target, source) {
73
+ source = source != null ? source : {};
74
+ if (Object.getOwnPropertyDescriptors) {
75
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
76
+ } else {
77
+ ownKeys(Object(source)).forEach(function(key) {
78
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
79
+ });
80
+ }
81
+ return target;
82
+ }
83
+ function _object_without_properties(source, excluded) {
84
+ if (source == null) return {};
85
+ var target = _object_without_properties_loose(source, excluded);
86
+ var key, i;
87
+ if (Object.getOwnPropertySymbols) {
88
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
89
+ for(i = 0; i < sourceSymbolKeys.length; i++){
90
+ key = sourceSymbolKeys[i];
91
+ if (excluded.indexOf(key) >= 0) continue;
92
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
93
+ target[key] = source[key];
94
+ }
95
+ }
96
+ return target;
97
+ }
98
+ function _object_without_properties_loose(source, excluded) {
99
+ if (source == null) return {};
100
+ var target = {};
101
+ var sourceKeys = Object.keys(source);
102
+ var key, i;
103
+ for(i = 0; i < sourceKeys.length; i++){
104
+ key = sourceKeys[i];
105
+ if (excluded.indexOf(key) >= 0) continue;
106
+ target[key] = source[key];
107
+ }
108
+ return target;
109
+ }
110
+ function _ts_generator(thisArg, body) {
111
+ var f, y, t, g, _ = {
112
+ label: 0,
113
+ sent: function() {
114
+ if (t[0] & 1) throw t[1];
115
+ return t[1];
116
+ },
117
+ trys: [],
118
+ ops: []
67
119
  };
68
- }
69
- return {
70
- ...message,
71
- runSteps: await getRunSteps({
72
- threadId: message.thread_id,
73
- runId: message.run_id,
74
- client
75
- })
76
- };
77
- };
78
-
79
- // src/hooks/messages/useCreateMessage/lib/mutationOptions/mutationFn.ts
80
- var mutationFn = async ({
81
- client = defaultClient,
82
- content,
83
- threadId
84
- }) => {
85
- const message = await client.beta.threads.messages.create(threadId, {
86
- content,
87
- role: "user"
88
- });
89
- return {
90
- message: await extendMessage({
91
- message,
92
- client
93
- })
94
- };
95
- };
96
-
97
- // src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/index.ts
98
- var import_p_map = __toESM(require("p-map"));
99
-
100
- // src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/toolOutput/index.ts
101
- var toolOutput = async ({
102
- toolCall,
103
- latestRun,
104
- functions
105
- }) => {
106
- const fn = functions[toolCall.function.name];
107
- if (!fn) {
108
- console.log({ toolCall });
109
- throw new Error(`No function for ${toolCall.function.name}`);
110
- }
111
- return {
112
- tool_call_id: toolCall.id,
113
- output: await fn({
114
- toolCall,
115
- latestRun
116
- })
117
- };
118
- };
119
-
120
- // src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/index.ts
121
- var mutationFn2 = async ({
122
- client = defaultClient,
123
- latestRun,
124
- functions = {}
125
- }) => {
126
- if (!latestRun.required_action) {
127
- throw new Error("No required_action for run ${latestRun.id} with status ${latestRun.status}");
128
- }
129
- console.log("handleActions", { latestRun });
130
- const toolCalls = latestRun.required_action.submit_tool_outputs.tool_calls;
131
- const run = await client.beta.threads.runs.submitToolOutputs(
132
- latestRun.thread_id,
133
- latestRun.id,
134
- {
135
- tool_outputs: await (0, import_p_map.default)(toolCalls, (toolCall) => toolOutput({ toolCall, latestRun, functions }))
120
+ return g = {
121
+ next: verb(0),
122
+ "throw": verb(1),
123
+ "return": verb(2)
124
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
125
+ return this;
126
+ }), g;
127
+ function verb(n) {
128
+ return function(v) {
129
+ return step([
130
+ n,
131
+ v
132
+ ]);
133
+ };
134
+ }
135
+ function step(op) {
136
+ if (f) throw new TypeError("Generator is already executing.");
137
+ while(_)try {
138
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
139
+ if (y = 0, t) op = [
140
+ op[0] & 2,
141
+ t.value
142
+ ];
143
+ switch(op[0]){
144
+ case 0:
145
+ case 1:
146
+ t = op;
147
+ break;
148
+ case 4:
149
+ _.label++;
150
+ return {
151
+ value: op[1],
152
+ done: false
153
+ };
154
+ case 5:
155
+ _.label++;
156
+ y = op[1];
157
+ op = [
158
+ 0
159
+ ];
160
+ continue;
161
+ case 7:
162
+ op = _.ops.pop();
163
+ _.trys.pop();
164
+ continue;
165
+ default:
166
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
167
+ _ = 0;
168
+ continue;
169
+ }
170
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
171
+ _.label = op[1];
172
+ break;
173
+ }
174
+ if (op[0] === 6 && _.label < t[1]) {
175
+ _.label = t[1];
176
+ t = op;
177
+ break;
178
+ }
179
+ if (t && _.label < t[2]) {
180
+ _.label = t[2];
181
+ _.ops.push(op);
182
+ break;
183
+ }
184
+ if (t[2]) _.ops.pop();
185
+ _.trys.pop();
186
+ continue;
187
+ }
188
+ op = body.call(thisArg, _);
189
+ } catch (e) {
190
+ op = [
191
+ 6,
192
+ e
193
+ ];
194
+ y = 0;
195
+ } finally{
196
+ f = t = 0;
197
+ }
198
+ if (op[0] & 5) throw op[1];
199
+ return {
200
+ value: op[0] ? op[1] : void 0,
201
+ done: true
202
+ };
136
203
  }
137
- );
138
- return {
139
- run
140
- };
141
- };
142
-
143
- // src/hooks/runs/useCreateRun/lib/mutationOptions/mutationFn.ts
144
- var mutationFn3 = async ({
145
- client = defaultClient,
146
- threadId,
147
- assistantId,
148
- ...rest
149
- }) => {
150
- const run = await client.beta.threads.runs.create(threadId, {
151
- ...rest,
152
- assistant_id: assistantId
153
- });
154
- return {
155
- run
156
- };
157
- };
158
- // Annotate the CommonJS export names for ESM import in node:
159
- 0 && (module.exports = {
160
- createMessageMutationFn,
161
- createRunMutationFn,
162
- handleActionMutationFn
204
+ }
205
+ import OpenAI from "openai";
206
+ var defaultClient = new OpenAI({
207
+ apiKey: process.env.OPENAI_API_KEY
163
208
  });
209
+ // src/lib/runSteps/getRunSteps/index.ts
210
+ var getRunSteps = function() {
211
+ var _ref = _async_to_generator(function(param) {
212
+ var threadId, runId, client, runStepsResponse;
213
+ return _ts_generator(this, function(_state) {
214
+ switch(_state.label){
215
+ case 0:
216
+ threadId = param.threadId, runId = param.runId, client = param.client;
217
+ return [
218
+ 4,
219
+ client.beta.threads.runs.steps.list(threadId, runId)
220
+ ];
221
+ case 1:
222
+ runStepsResponse = _state.sent();
223
+ return [
224
+ 2,
225
+ runStepsResponse.data
226
+ ];
227
+ }
228
+ });
229
+ });
230
+ return function getRunSteps(_) {
231
+ return _ref.apply(this, arguments);
232
+ };
233
+ }();
234
+ // src/lib/threadMessages/extendThreadMessage.ts
235
+ var extendThreadMessage = function() {
236
+ var _ref = _async_to_generator(function(param) {
237
+ var threadMessage, client, _tmp, _tmp1;
238
+ return _ts_generator(this, function(_state) {
239
+ switch(_state.label){
240
+ case 0:
241
+ threadMessage = param.threadMessage, client = param.client;
242
+ if (!threadMessage.run_id) {
243
+ return [
244
+ 2,
245
+ _object_spread_props(_object_spread({}, threadMessage), {
246
+ runSteps: []
247
+ })
248
+ ];
249
+ }
250
+ _tmp = [
251
+ _object_spread({}, threadMessage)
252
+ ];
253
+ _tmp1 = {};
254
+ return [
255
+ 4,
256
+ getRunSteps({
257
+ threadId: threadMessage.thread_id,
258
+ runId: threadMessage.run_id,
259
+ client: client
260
+ })
261
+ ];
262
+ case 1:
263
+ return [
264
+ 2,
265
+ _object_spread_props.apply(void 0, _tmp.concat([
266
+ (_tmp1.runSteps = _state.sent(), _tmp1)
267
+ ]))
268
+ ];
269
+ }
270
+ });
271
+ });
272
+ return function extendThreadMessage(_) {
273
+ return _ref.apply(this, arguments);
274
+ };
275
+ }();
276
+ // src/lib/threadMessages/createThreadMessageMutationFn.ts
277
+ var createThreadMessageMutationFn = function() {
278
+ var _ref = _async_to_generator(function(param) {
279
+ var _param_client, client, content, threadId, threadMessage, _tmp;
280
+ return _ts_generator(this, function(_state) {
281
+ switch(_state.label){
282
+ case 0:
283
+ _param_client = param.client, client = _param_client === void 0 ? defaultClient : _param_client, content = param.content, threadId = param.threadId;
284
+ return [
285
+ 4,
286
+ client.beta.threads.messages.create(threadId, {
287
+ content: content,
288
+ role: "user"
289
+ })
290
+ ];
291
+ case 1:
292
+ threadMessage = _state.sent();
293
+ _tmp = {};
294
+ return [
295
+ 4,
296
+ extendThreadMessage({
297
+ threadMessage: threadMessage,
298
+ client: client
299
+ })
300
+ ];
301
+ case 2:
302
+ return [
303
+ 2,
304
+ (_tmp.threadMessage = _state.sent(), _tmp)
305
+ ];
306
+ }
307
+ });
308
+ });
309
+ return function createThreadMessageMutationFn(_) {
310
+ return _ref.apply(this, arguments);
311
+ };
312
+ }();
313
+ // src/lib/runs/createRunMutationFn.ts
314
+ var createRunMutationFn = function() {
315
+ var _ref = _async_to_generator(function(_param) {
316
+ var _param_client, client, threadId, assistantId, rest, run;
317
+ return _ts_generator(this, function(_state) {
318
+ switch(_state.label){
319
+ case 0:
320
+ _param_client = _param.client, client = _param_client === void 0 ? defaultClient : _param_client, threadId = _param.threadId, assistantId = _param.assistantId, rest = _object_without_properties(_param, [
321
+ "client",
322
+ "threadId",
323
+ "assistantId"
324
+ ]);
325
+ return [
326
+ 4,
327
+ client.beta.threads.runs.create(threadId, _object_spread_props(_object_spread({}, rest), {
328
+ assistant_id: assistantId
329
+ }))
330
+ ];
331
+ case 1:
332
+ run = _state.sent();
333
+ return [
334
+ 2,
335
+ {
336
+ run: run
337
+ }
338
+ ];
339
+ }
340
+ });
341
+ });
342
+ return function createRunMutationFn(_param) {
343
+ return _ref.apply(this, arguments);
344
+ };
345
+ }();
346
+ // src/lib/actions/handleActionMutationFn/index.ts
347
+ import pMap from "p-map";
348
+ // src/lib/actions/handleActionMutationFn/toolOutput/index.ts
349
+ var toolOutput = function() {
350
+ var _ref = _async_to_generator(function(param) {
351
+ var toolCall, latestRun, functions, fn, _tmp;
352
+ return _ts_generator(this, function(_state) {
353
+ switch(_state.label){
354
+ case 0:
355
+ toolCall = param.toolCall, latestRun = param.latestRun, functions = param.functions;
356
+ fn = functions[toolCall.function.name];
357
+ if (!fn) {
358
+ console.log({
359
+ toolCall: toolCall
360
+ });
361
+ throw new Error("No function for ".concat(toolCall.function.name));
362
+ }
363
+ _tmp = {
364
+ tool_call_id: toolCall.id
365
+ };
366
+ return [
367
+ 4,
368
+ fn({
369
+ toolCall: toolCall,
370
+ latestRun: latestRun
371
+ })
372
+ ];
373
+ case 1:
374
+ return [
375
+ 2,
376
+ (_tmp.output = _state.sent(), _tmp)
377
+ ];
378
+ }
379
+ });
380
+ });
381
+ return function toolOutput(_) {
382
+ return _ref.apply(this, arguments);
383
+ };
384
+ }();
385
+ // src/lib/actions/handleActionMutationFn/index.ts
386
+ var handleActionMutationFn = function() {
387
+ var _ref = _async_to_generator(function(param) {
388
+ var _param_client, client, latestRun, _param_functions, functions, toolCalls, run, _, _1, _tmp, _tmp1;
389
+ return _ts_generator(this, function(_state) {
390
+ switch(_state.label){
391
+ case 0:
392
+ _param_client = param.client, client = _param_client === void 0 ? defaultClient : _param_client, latestRun = param.latestRun, _param_functions = param.functions, functions = _param_functions === void 0 ? {} : _param_functions;
393
+ if (!latestRun.required_action) {
394
+ throw new Error("No required_action for run ${latestRun.id} with status ${latestRun.status}");
395
+ }
396
+ console.log("handleActions", {
397
+ latestRun: latestRun
398
+ });
399
+ toolCalls = latestRun.required_action.submit_tool_outputs.tool_calls;
400
+ _1 = (_ = client.beta.threads.runs).submitToolOutputs;
401
+ _tmp = [
402
+ latestRun.thread_id,
403
+ latestRun.id
404
+ ];
405
+ _tmp1 = {};
406
+ return [
407
+ 4,
408
+ pMap(toolCalls, function(toolCall) {
409
+ return toolOutput({
410
+ toolCall: toolCall,
411
+ latestRun: latestRun,
412
+ functions: functions
413
+ });
414
+ })
415
+ ];
416
+ case 1:
417
+ return [
418
+ 4,
419
+ _1.apply(_, _tmp.concat([
420
+ (_tmp1.tool_outputs = _state.sent(), _tmp1)
421
+ ]))
422
+ ];
423
+ case 2:
424
+ run = _state.sent();
425
+ return [
426
+ 2,
427
+ {
428
+ run: run
429
+ }
430
+ ];
431
+ }
432
+ });
433
+ });
434
+ return function handleActionMutationFn(_) {
435
+ return _ref.apply(this, arguments);
436
+ };
437
+ }();
438
+ export { createRunMutationFn, createThreadMessageMutationFn, handleActionMutationFn };
164
439
  //# sourceMappingURL=mutationFns.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mutationFns.ts","../src/lib/ai/index.ts","../src/lib/runSteps/getRunSteps/index.ts","../src/lib/messages/extendMessage.ts","../src/hooks/messages/useCreateMessage/lib/mutationOptions/mutationFn.ts","../src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/index.ts","../src/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn/toolOutput/index.ts","../src/hooks/runs/useCreateRun/lib/mutationOptions/mutationFn.ts"],"sourcesContent":["export {\n mutationFn as createMessageMutationFn,\n Response as CreateMessageResponse,\n} from '@/hooks/messages/useCreateMessage/lib/mutationOptions/mutationFn'\nexport { mutationFn as handleActionMutationFn } from '@/hooks/actions/useHandleAction/lib/mutationOptions/mutationFn'\nexport { mutationFn as createRunMutationFn } from '@/hooks/runs/useCreateRun/lib/mutationOptions/mutationFn'\n","import OpenAI from 'openai'\n\nexport const defaultClient = new OpenAI({\n apiKey: process.env.OPENAI_API_KEY!,\n})\n","import OpenAI from 'openai'\n\ntype Args = {\n threadId: string\n runId: string\n client: OpenAI\n}\n\nexport const getRunSteps = async ({\n threadId,\n runId,\n client,\n}: Args) => {\n const runStepsResponse = await client.beta.threads.runs.steps.list(\n threadId,\n runId,\n )\n\n return runStepsResponse.data\n}\n","import OpenAI from 'openai'\nimport { getRunSteps } from '@/lib/runSteps/getRunSteps'\n\ntype Args = {\n message: OpenAI.Beta.Threads.Messages.ThreadMessage\n client: OpenAI\n}\n\nexport const extendMessage = async ({\n message,\n client,\n}: Args) => {\n if (!message.run_id) {\n return {\n ...message,\n runSteps: [],\n }\n }\n\n return {\n ...message,\n runSteps: await getRunSteps({\n threadId: message.thread_id,\n runId: message.run_id,\n client,\n }),\n }\n}\n","import { defaultClient } from '@/lib/ai'\nimport { Message } from '@/types'\nimport { extendMessage } from '@/lib/messages/extendMessage'\n\nexport type Args = {\n client?: typeof defaultClient\n content: string\n threadId: string\n}\n\nexport type Response = {\n message: Message\n}\n\nexport const mutationFn = async ({\n client = defaultClient,\n content,\n threadId,\n}: Args): Promise<Response> => {\n const message = await client.beta.threads.messages.create(threadId, {\n content: content,\n role: 'user',\n })\n\n return {\n message: await extendMessage({\n message,\n client,\n }),\n }\n}\n","import { Run, Functions } from '@/types'\nimport pMap from 'p-map'\nimport { defaultClient } from '@/lib/ai'\nimport { toolOutput } from './toolOutput'\n\nexport type Args = {\n client?: typeof defaultClient\n latestRun: Run\n functions?: Functions\n}\n\nexport type Response = {\n run: Run\n}\n\nexport const mutationFn = async ({\n client = defaultClient,\n latestRun,\n functions = {},\n}: Args): Promise<Response> => {\n if (!latestRun.required_action) {\n throw new Error('No required_action for run ${latestRun.id} with status ${latestRun.status}')\n }\n\n console.log('handleActions', { latestRun })\n\n const toolCalls = latestRun.required_action.submit_tool_outputs.tool_calls\n\n const run = await client.beta.threads.runs.submitToolOutputs(\n latestRun.thread_id,\n latestRun.id,\n {\n tool_outputs: await pMap(toolCalls, (toolCall) => toolOutput({ toolCall, latestRun, functions })),\n },\n )\n\n return {\n run,\n }\n}\n","import OpenAI from 'openai'\nimport { Functions } from '@/types'\n\ntype Args = {\n toolCall: OpenAI.Beta.Threads.Runs.RequiredActionFunctionToolCall\n latestRun: OpenAI.Beta.Threads.Runs.Run\n functions: Functions\n}\n\nexport const toolOutput = async ({\n toolCall,\n latestRun,\n functions,\n}: Args) => {\n const fn = functions[toolCall.function.name]\n\n if (!fn) {\n console.log({ toolCall })\n throw new Error(`No function for ${toolCall.function.name}`)\n }\n\n return {\n tool_call_id: toolCall.id,\n output: await fn({\n toolCall,\n latestRun,\n }),\n }\n}\n","import OpenAI from 'openai'\nimport { Run } from '@/types'\nimport { defaultClient } from '@/lib/ai'\n\nexport type Args = {\n client?: typeof defaultClient\n threadId: string\n assistantId: string\n} & (OpenAI.Beta.Threads.Runs.RunCreateParams | {})\n\nexport type Response = {\n run: Run\n}\n\nexport const mutationFn = async ({\n client = defaultClient,\n threadId,\n assistantId,\n ...rest\n}: Args): Promise<Response> => {\n const run = await client.beta.threads.runs.create(threadId, {\n ...rest,\n assistant_id: assistantId,\n })\n\n return {\n run,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,6BAAAA;AAAA,EAAA,8BAAAA;AAAA;AAAA;;;ACAA,oBAAmB;AAEZ,IAAM,gBAAgB,IAAI,cAAAC,QAAO;AAAA,EACtC,QAAQ,QAAQ,IAAI;AACtB,CAAC;;;ACIM,IAAM,cAAc,OAAO;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AACF,MAAY;AACV,QAAM,mBAAmB,MAAM,OAAO,KAAK,QAAQ,KAAK,MAAM;AAAA,IAC5D;AAAA,IACA;AAAA,EACF;AAEA,SAAO,iBAAiB;AAC1B;;;ACXO,IAAM,gBAAgB,OAAO;AAAA,EAClC;AAAA,EACA;AACF,MAAY;AACV,MAAI,CAAC,QAAQ,QAAQ;AACnB,WAAO;AAAA,MACL,GAAG;AAAA,MACH,UAAU,CAAC;AAAA,IACb;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,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CAAC;AAAA,IAClG;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,EACF;AACF;;;AEzBO,IAAMC,cAAa,OAAO;AAAA,EAC/B,SAAS;AAAA,EACT;AAAA,EACA;AAAA,EACA,GAAG;AACL,MAA+B;AAC7B,QAAM,MAAM,MAAM,OAAO,KAAK,QAAQ,KAAK,OAAO,UAAU;AAAA,IAC1D,GAAG;AAAA,IACH,cAAc;AAAA,EAChB,CAAC;AAED,SAAO;AAAA,IACL;AAAA,EACF;AACF;","names":["mutationFn","OpenAI","mutationFn","pMap","mutationFn"]}
1
+ {"version":3,"sources":["../src/lib/ai/index.ts","../src/lib/runSteps/getRunSteps/index.ts","../src/lib/threadMessages/extendThreadMessage.ts","../src/lib/threadMessages/createThreadMessageMutationFn.ts","../src/lib/runs/createRunMutationFn.ts","../src/lib/actions/handleActionMutationFn/index.ts","../src/lib/actions/handleActionMutationFn/toolOutput/index.ts"],"names":[],"mappings":";AAAA,OAAO,YAAY;AAEZ,IAAM,gBAAgB,IAAI,OAAO;AAAA,EACtC,QAAQ,QAAQ,IAAI;AACtB,CAAC;;;ACIM,IAAM,cAAc,OAAO;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AACF,MAAY;AACV,QAAM,mBAAmB,MAAM,OAAO,KAAK,QAAQ,KAAK,MAAM;AAAA,IAC5D;AAAA,IACA;AAAA,EACF;AAEA,SAAO,iBAAiB;AAC1B;;;ACXO,IAAM,sBAAsB,OAAO;AAAA,EACxC;AAAA,EACA;AACF,MAAY;AACV,MAAI,CAAC,cAAc,QAAQ;AACzB,WAAO;AAAA,MACL,GAAG;AAAA,MACH,UAAU,CAAC;AAAA,IACb;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG;AAAA,IACH,UAAU,MAAM,YAAY;AAAA,MAC1B,UAAU,cAAc;AAAA,MACxB,OAAO,cAAc;AAAA,MACrB;AAAA,IACF,CAAC;AAAA,EACH;AACF;;;ACbO,IAAM,gCAAgC,OAAO;AAAA,E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UAAU;AAAA,IACV;AAAA,MACE,cAAc,MAAM,KAAK,WAAW,CAAC,aAAa,WAAW,EAAE,UAAU,WAAW,UAAU,CAAC,CAAC;AAAA,IAClG;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,EACF;AACF","sourcesContent":["import OpenAI from 'openai'\n\nexport const defaultClient = new OpenAI({\n apiKey: process.env.OPENAI_API_KEY!,\n})\n","import OpenAI from 'openai'\n\ntype Args = {\n threadId: string\n runId: string\n client: OpenAI\n}\n\nexport const getRunSteps = async ({\n threadId,\n runId,\n client,\n}: Args) => {\n const runStepsResponse = await client.beta.threads.runs.steps.list(\n threadId,\n runId,\n )\n\n return runStepsResponse.data\n}\n","import OpenAI from 'openai'\nimport { getRunSteps } from '@/lib/runSteps/getRunSteps'\n\ntype Args = {\n threadMessage: OpenAI.Beta.Threads.Messages.ThreadMessage\n client: OpenAI\n}\n\nexport const extendThreadMessage = async ({\n threadMessage,\n client,\n}: Args) => {\n if (!threadMessage.run_id) {\n return {\n ...threadMessage,\n runSteps: [],\n }\n }\n\n return {\n ...threadMessage,\n runSteps: await getRunSteps({\n threadId: threadMessage.thread_id,\n runId: threadMessage.run_id,\n client,\n }),\n }\n}\n","import { defaultClient } from '@/lib/ai'\nimport { ThreadMessage } from '@/types'\nimport { extendThreadMessage } from '@/lib/threadMessages/extendThreadMessage'\n\nexport type Args = {\n client?: typeof defaultClient\n content: string\n threadId: string\n}\n\nexport type Response = {\n threadMessage: ThreadMessage\n}\n\nexport const createThreadMessageMutationFn = async ({\n client = defaultClient,\n content,\n threadId,\n}: Args): Promise<Response> => {\n const threadMessage = await client.beta.threads.messages.create(threadId, {\n content: content,\n role: 'user',\n })\n\n return {\n threadMessage: await extendThreadMessage({\n threadMessage,\n client,\n }),\n }\n}\n","import OpenAI from 'openai'\nimport { Run } from '@/types'\nimport { defaultClient } from '@/lib/ai'\n\nexport type Args = {\n client?: typeof defaultClient\n threadId: string\n assistantId: string\n} & (OpenAI.Beta.Threads.Runs.RunCreateParams | {})\n\nexport type Response = {\n run: Run\n}\n\nexport const createRunMutationFn = async ({\n client = defaultClient,\n threadId,\n assistantId,\n ...rest\n}: Args): Promise<Response> => {\n const run = await client.beta.threads.runs.create(threadId, {\n ...rest,\n assistant_id: assistantId,\n })\n\n return {\n run,\n }\n}\n","import { Run, Functions } from '@/types'\nimport pMap from 'p-map'\nimport { defaultClient } from '@/lib/ai'\nimport { toolOutput } from './toolOutput'\n\nexport type Args = {\n client?: typeof defaultClient\n latestRun: Run\n functions?: Functions\n}\n\nexport type Response = {\n run: Run\n}\n\nexport const handleActionMutationFn = async ({\n client = defaultClient,\n latestRun,\n functions = {},\n}: Args): Promise<Response> => {\n if (!latestRun.required_action) {\n throw new Error('No required_action for run ${latestRun.id} with status ${latestRun.status}')\n }\n\n console.log('handleActions', { latestRun })\n\n const toolCalls = latestRun.required_action.submit_tool_outputs.tool_calls\n\n const run = await client.beta.threads.runs.submitToolOutputs(\n latestRun.thread_id,\n latestRun.id,\n {\n tool_outputs: await pMap(toolCalls, (toolCall) => toolOutput({ toolCall, latestRun, functions })),\n },\n )\n\n return {\n run,\n }\n}\n","import OpenAI from 'openai'\nimport { Functions } from '@/types'\n\ntype Args = {\n toolCall: OpenAI.Beta.Threads.Runs.RequiredActionFunctionToolCall\n latestRun: OpenAI.Beta.Threads.Runs.Run\n functions: Functions\n}\n\nexport const toolOutput = async ({\n toolCall,\n latestRun,\n functions,\n}: Args) => {\n const fn = functions[toolCall.function.name]\n\n if (!fn) {\n console.log({ toolCall })\n throw new Error(`No function for ${toolCall.function.name}`)\n }\n\n return {\n tool_call_id: toolCall.id,\n output: await fn({\n toolCall,\n latestRun,\n }),\n }\n}\n"]}