@make-u-free/migi 0.2.7 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@make-u-free/migi",
3
- "version": "0.2.7",
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(' → キャンセル'))
package/src/tools.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } from 'fs'
2
2
  import { execSync } from 'child_process'
3
3
  import { dirname, extname } from 'path'
4
+ import { request } from 'https'
4
5
  import { glob } from 'glob'
5
6
  import xlsxPkg from 'xlsx'
7
+ import { httpsAgent } from './tls.js'
6
8
  const { readFile: xlsxReadFile, utils: xlsxUtils } = xlsxPkg
7
9
 
8
10
  // ---- OpenAI ツールスキーマ定義 ----
@@ -179,13 +181,29 @@ export async function executeTool(name, args, opts = {}) {
179
181
  const url = opts.teamsWebhookUrl
180
182
  if (!url) return 'エラー: Teams Webhook URL が設定されていません'
181
183
  const body = JSON.stringify({ text: args.message })
182
- const res = await fetch(url, {
183
- method: 'POST',
184
- headers: { 'Content-Type': 'application/json' },
185
- body
184
+ return new Promise((resolve) => {
185
+ const parsed = new URL(url)
186
+ const options = {
187
+ hostname: parsed.hostname,
188
+ path: parsed.pathname + parsed.search,
189
+ method: 'POST',
190
+ headers: {
191
+ 'Content-Type': 'application/json',
192
+ 'Content-Length': Buffer.byteLength(body)
193
+ },
194
+ ...(httpsAgent ? { agent: httpsAgent } : {})
195
+ }
196
+ const req = request(options, (res) => {
197
+ if (res.statusCode >= 200 && res.statusCode < 300) {
198
+ resolve('Teams に通知しました')
199
+ } else {
200
+ resolve(`エラー: Teams への送信に失敗しました (${res.statusCode})`)
201
+ }
202
+ })
203
+ req.on('error', (err) => resolve(`エラー: ${err.message}`))
204
+ req.write(body)
205
+ req.end()
186
206
  })
187
- if (!res.ok) return `エラー: Teams への送信に失敗しました (${res.status})`
188
- return 'Teams に通知しました'
189
207
  }
190
208
 
191
209
  default: