@make-u-free/migi 0.4.6 → 0.4.7

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/bin/migi.js CHANGED
@@ -249,8 +249,7 @@ async function prompt() {
249
249
  console.log('\n' + sepWithLabel(chalk.bold.cyan(agentName) + chalk.dim(` [スキル: ${parsed.name}]`)))
250
250
  const expanded = expandSkill(skill.content, parsed.args)
251
251
  try {
252
- const reply = await agent.chat(expanded)
253
- console.log('\n' + reply + '\n')
252
+ await agent.chat(expanded)
254
253
  } catch (err) {
255
254
  console.error(chalk.red('\n エラー: ' + err.message + '\n'))
256
255
  }
@@ -265,8 +264,7 @@ async function prompt() {
265
264
  // --- 通常チャット ---
266
265
  console.log('\n' + sepWithLabel(chalk.bold.cyan(agentName)))
267
266
  try {
268
- const reply = await agent.chat(input)
269
- console.log('\n' + reply + '\n')
267
+ await agent.chat(input)
270
268
  } catch (err) {
271
269
  console.error(chalk.red('\n エラー: ' + err.message + '\n'))
272
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@make-u-free/migi",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
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
@@ -97,28 +97,69 @@ ${userNameLine}
97
97
 
98
98
  while (true) {
99
99
  spinner.start('考え中…')
100
- const response = await this.client.chat.completions.create({
100
+
101
+ const stream = await this.client.chat.completions.create({
101
102
  model: this.model,
102
103
  messages,
103
104
  tools: this.tools,
104
- tool_choice: 'auto'
105
+ tool_choice: 'auto',
106
+ stream: true
105
107
  })
106
- spinner.stop()
107
108
 
108
- const choice = response.choices[0]
109
- messages.push(choice.message)
110
- this.history.push(choice.message)
109
+ let content = ''
110
+ const tcMap = {} // tool_calls をインデックスで蓄積
111
+ let finishReason = null
112
+ let streaming = false // 最初のコンテンツが届いたか
113
+
114
+ for await (const chunk of stream) {
115
+ const choice = chunk.choices[0]
116
+ if (!choice) continue
117
+ const delta = choice.delta
118
+ if (choice.finish_reason) finishReason = choice.finish_reason
119
+
120
+ // テキストチャンク
121
+ if (delta?.content) {
122
+ if (!streaming) {
123
+ spinner.stop()
124
+ process.stdout.write('\n')
125
+ streaming = true
126
+ }
127
+ content += delta.content
128
+ process.stdout.write(delta.content)
129
+ }
130
+
131
+ // tool_calls チャンク(引数はストリームで分割されて届く)
132
+ if (delta?.tool_calls) {
133
+ for (const tc of delta.tool_calls) {
134
+ if (!tcMap[tc.index]) tcMap[tc.index] = { id: '', type: 'function', function: { name: '', arguments: '' } }
135
+ if (tc.id) tcMap[tc.index].id += tc.id
136
+ if (tc.function?.name) tcMap[tc.index].function.name += tc.function.name
137
+ if (tc.function?.arguments) tcMap[tc.index].function.arguments += tc.function.arguments
138
+ }
139
+ }
140
+ }
141
+
142
+ spinner.stop()
111
143
 
112
144
  // 通常の返答
113
- if (choice.finish_reason === 'stop') {
114
- return choice.message.content
145
+ if (finishReason === 'stop') {
146
+ process.stdout.write('\n\n')
147
+ const assistantMsg = { role: 'assistant', content }
148
+ messages.push(assistantMsg)
149
+ this.history.push(assistantMsg)
150
+ return content
115
151
  }
116
152
 
117
153
  // ツール呼び出し
118
- if (choice.finish_reason === 'tool_calls') {
119
- const toolResults = []
154
+ if (finishReason === 'tool_calls') {
155
+ if (streaming) process.stdout.write('\n')
156
+ const toolCalls = Object.values(tcMap)
157
+ const assistantMsg = { role: 'assistant', content: content || null, tool_calls: toolCalls }
158
+ messages.push(assistantMsg)
159
+ this.history.push(assistantMsg)
120
160
 
121
- for (const toolCall of choice.message.tool_calls) {
161
+ const toolResults = []
162
+ for (const toolCall of toolCalls) {
122
163
  const args = JSON.parse(toolCall.function.arguments)
123
164
  const name = toolCall.function.name
124
165