@make-u-free/migi 0.2.8 → 0.2.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/agent.js +23 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@make-u-free/migi",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Your AI right-hand agent. Works anywhere, with any LLM API.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/agent.js CHANGED
@@ -65,7 +65,25 @@ ${userNameLine}
65
65
  (context ? `\n## ロードされたコンテキスト\n${context}` : '')
66
66
  }
67
67
 
68
+ // tool_calls に対応する tool 結果がない壊れた履歴を修復する
69
+ _sanitizeHistory() {
70
+ const cleaned = []
71
+ for (let i = 0; i < this.history.length; i++) {
72
+ const msg = this.history[i]
73
+ if (msg.role === 'assistant' && msg.tool_calls?.length) {
74
+ const toolIds = msg.tool_calls.map(t => t.id)
75
+ const hasAllResults = toolIds.every(id =>
76
+ this.history.slice(i + 1).some(m => m.role === 'tool' && m.tool_call_id === id)
77
+ )
78
+ if (!hasAllResults) continue // 対応する結果がなければ丸ごとスキップ
79
+ }
80
+ cleaned.push(msg)
81
+ }
82
+ this.history = cleaned
83
+ }
84
+
68
85
  async chat(userMessage) {
86
+ this._sanitizeHistory()
69
87
  this.history.push({ role: 'user', content: userMessage })
70
88
 
71
89
  const messages = [
@@ -104,7 +122,11 @@ ${userNameLine}
104
122
  let result
105
123
 
106
124
  if (approved) {
107
- result = await executeTool(name, args, { teamsWebhookUrl: this.teamsWebhookUrl })
125
+ try {
126
+ result = await executeTool(name, args, { teamsWebhookUrl: this.teamsWebhookUrl })
127
+ } catch (err) {
128
+ result = `エラー: ${err.message}`
129
+ }
108
130
  } else {
109
131
  result = 'ユーザーによりキャンセルされました'
110
132
  console.log(chalk.dim(' → キャンセル'))