@@ -6,56 +6,31 @@ const assitantId = "asst_sxL8Gxy8meOwaf0vySOnegmu";
6
6
async function streamRun (
7
7
threadId : string ,
8
8
assistantId : string ,
9
- send : ( chunk : string ) => void
9
+ send : ( chunk : string ) => void ,
10
10
) {
11
- console . log ( "Streaming" ) ;
12
11
const stream = openai . beta . threads . runs . stream ( threadId , {
13
12
assistant_id : assistantId ,
14
13
} ) ;
15
14
16
15
for await ( const chunk of stream ) {
17
- // console.log("Event:", chunk.event);
18
16
if ( chunk . event === "thread.message.delta" ) {
19
17
if ( chunk . data . delta . content ?. [ 0 ] ?. type === "text" ) {
20
18
send ( chunk . data . delta . content [ 0 ] . text ?. value ! ) ;
21
19
// process.stdout.write(chunk.data.delta.content[0].text?.value);
22
20
}
21
+ } else {
22
+ console . log ( `Used event: ${ chunk . event } ` ) ;
23
23
}
24
24
}
25
25
}
26
26
27
- // .on("textCreated", (text) => process.stdout.write("\nassistant > "))
28
- // .on("textDelta", (textDelta, snapshot) =>
29
- // process.stdout.write(textDelta.value)
30
- // )
31
- // .on("toolCallCreated", (toolCall) =>
32
- // process.stdout.write(`\nassistant > ${toolCall.type}\n\n`)
33
- // )
34
- // .on("toolCallDelta", (toolCallDelta, snapshot) => {
35
- // if (toolCallDelta.type === "code_interpreter") {
36
- // if (toolCallDelta.code_interpreter.input) {
37
- // process.stdout.write(toolCallDelta.code_interpreter.input);
38
- // }
39
- // if (toolCallDelta.code_interpreter.outputs) {
40
- // process.stdout.write("\noutput >\n");
41
- // toolCallDelta.code_interpreter.outputs.forEach((output) => {
42
- // if (output.type === "logs") {
43
- // process.stdout.write(`\n${output.logs}\n`);
44
- // }
45
- // });
46
- // }
47
- // }
48
- // });
49
-
50
27
export default defineWebSocketHandler ( {
51
28
open ( peer ) {
52
29
console . log ( "[ws] open" , peer ) ;
53
30
} ,
54
31
55
32
async message ( peer , message ) {
56
- console . log ( "[ws] message" , peer , message ) ;
57
33
const parsed = JSON . parse ( message . text ( ) ) ;
58
- console . log ( parsed ) ;
59
34
60
35
const dbthread = await db ( "threads" ) . where ( { id : parsed . threadId } ) . first ( ) ;
61
36
if ( ! dbthread ) {
@@ -67,16 +42,9 @@ export default defineWebSocketHandler({
67
42
JSON . stringify ( {
68
43
type : "text-chunk" ,
69
44
text : textChunk ,
70
- } )
71
- )
45
+ } ) ,
46
+ ) ,
72
47
) ;
73
- // if (message.text().includes("ping")) {
74
- // peer.send("pong");
75
- // }
76
-
77
- // if (message.text().includes("dorun")) {
78
- // streamRun()
79
- // }
80
48
} ,
81
49
82
50
close ( peer , event ) {
0 commit comments