traw 0.2.3 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "traw",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "module": "src/index.ts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -72,9 +72,13 @@ export class Agent {
72
72
  log.step(step + 1, this.config.maxSteps, state.url)
73
73
 
74
74
  const thinkStart = Date.now()
75
+ let decision: { thought: string; action: Action }
75
76
  log.receiveStart()
76
- const decision = await this.think(state)
77
- log.receiveStop()
77
+ try {
78
+ decision = await this.think(state)
79
+ } finally {
80
+ log.receiveStop()
81
+ }
78
82
  this.aiTime += Date.now() - thinkStart
79
83
 
80
84
  log.thought(decision.thought)
@@ -22,7 +22,7 @@ export class MoClient {
22
22
  this.thinking = opts.thinking
23
23
  }
24
24
 
25
- async chat(messages: ChatMessage[]): Promise<string> {
25
+ async chat(messages: ChatMessage[], timeoutMs = 300000): Promise<string> {
26
26
  const headers: Record<string, string> = {
27
27
  "Content-Type": "application/json",
28
28
  }
@@ -31,16 +31,30 @@ export class MoClient {
31
31
  headers["Authorization"] = `Bearer ${this.apiKey}`
32
32
  }
33
33
 
34
- const resp = await fetch(`${this.url}/v1/chat/completions`, {
35
- method: "POST",
36
- headers,
37
- body: JSON.stringify({
38
- model: this.model,
39
- messages,
40
- stream: false,
41
- thinking: this.thinking,
42
- }),
43
- })
34
+ const controller = new AbortController()
35
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs)
36
+
37
+ let resp: Response
38
+ try {
39
+ resp = await fetch(`${this.url}/v1/chat/completions`, {
40
+ method: "POST",
41
+ headers,
42
+ signal: controller.signal,
43
+ body: JSON.stringify({
44
+ model: this.model,
45
+ messages,
46
+ stream: false,
47
+ thinking: this.thinking,
48
+ }),
49
+ })
50
+ } catch (err: any) {
51
+ clearTimeout(timeoutId)
52
+ if (err.name === "AbortError") {
53
+ throw new Error(`mo timeout: no response in ${timeoutMs / 1000}s`)
54
+ }
55
+ throw err
56
+ }
57
+ clearTimeout(timeoutId)
44
58
 
45
59
  if (!resp.ok) {
46
60
  const body = await resp.text()