mycto_agent 0.1.5 → 0.1.6

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/dist/index.d.mts CHANGED
@@ -696,6 +696,8 @@ type LLMStreamEvent = {
696
696
  } | {
697
697
  type: 'tool-result';
698
698
  toolCallId: string;
699
+ toolName: string;
700
+ args: Record<string, unknown>;
699
701
  result: unknown;
700
702
  } | {
701
703
  type: 'finish';
@@ -957,13 +959,13 @@ declare class OpenAgent {
957
959
  */
958
960
  private executeTool;
959
961
  /**
960
- * 非流式对话(带 Agent Loop)
962
+ * 非流式对话(AI SDK 自动多步)
961
963
  */
962
964
  chat(message: string, options?: {
963
965
  sessionId?: string;
964
966
  }): Promise<ChatResult>;
965
967
  /**
966
- * 流式对话
968
+ * 流式对话(AI SDK 自动多步)
967
969
  */
968
970
  stream(message: string, options?: {
969
971
  sessionId?: string;
package/dist/index.d.ts CHANGED
@@ -696,6 +696,8 @@ type LLMStreamEvent = {
696
696
  } | {
697
697
  type: 'tool-result';
698
698
  toolCallId: string;
699
+ toolName: string;
700
+ args: Record<string, unknown>;
699
701
  result: unknown;
700
702
  } | {
701
703
  type: 'finish';
@@ -957,13 +959,13 @@ declare class OpenAgent {
957
959
  */
958
960
  private executeTool;
959
961
  /**
960
- * 非流式对话(带 Agent Loop)
962
+ * 非流式对话(AI SDK 自动多步)
961
963
  */
962
964
  chat(message: string, options?: {
963
965
  sessionId?: string;
964
966
  }): Promise<ChatResult>;
965
967
  /**
966
- * 流式对话
968
+ * 流式对话(AI SDK 自动多步)
967
969
  */
968
970
  stream(message: string, options?: {
969
971
  sessionId?: string;
package/dist/index.js CHANGED
@@ -1313,6 +1313,8 @@ async function stream(input) {
1313
1313
  yield {
1314
1314
  type: "tool-result",
1315
1315
  toolCallId: e.toolCallId,
1316
+ toolName: e.toolName,
1317
+ args: e.args ?? e.input ?? {},
1316
1318
  result: e.result ?? e.output
1317
1319
  };
1318
1320
  break;
@@ -3483,7 +3485,7 @@ You can read, write, and edit files, search code, and execute commands.`
3483
3485
  }
3484
3486
  }
3485
3487
  /**
3486
- * 非流式对话(带 Agent Loop)
3488
+ * 非流式对话(AI SDK 自动多步)
3487
3489
  */
3488
3490
  async chat(message, options) {
3489
3491
  await this.ensureInitialized();
@@ -3496,90 +3498,61 @@ You can read, write, and edit files, search code, and execute commands.`
3496
3498
  const msgId = `msg_${Date.now()}`;
3497
3499
  const ctx = this.createToolContext(sessionId2, msgId);
3498
3500
  const tools = getAIToolsForAgent(this.agent, ctx, this.mode);
3501
+ const messages = await getSessionMessages(sessionId2);
3502
+ const modelMessages = await toModelMessages(messages);
3503
+ const result = await stream({
3504
+ providerId: this.providerId,
3505
+ modelId: this.modelId,
3506
+ messages: modelMessages,
3507
+ system: buildSystemPrompt(this.agent),
3508
+ tools,
3509
+ temperature: this.temperature,
3510
+ maxOutputTokens: this.maxOutputTokens,
3511
+ maxSteps: this.maxSteps
3512
+ // AI SDK 自动处理多步
3513
+ });
3499
3514
  let fullText = "";
3500
3515
  const toolCalls = [];
3501
3516
  let totalUsage = { input: 0, output: 0 };
3502
3517
  let totalCost = 0;
3503
3518
  let finishReason = "unknown";
3504
- let step = 0;
3505
- while (step < this.maxSteps) {
3506
- step++;
3507
- log6.debug(`Agent loop step ${step}/${this.maxSteps}`);
3508
- const messages = await getSessionMessages(sessionId2);
3509
- const modelMessages = await toModelMessages(messages);
3510
- const result = await stream({
3511
- providerId: this.providerId,
3512
- modelId: this.modelId,
3513
- messages: modelMessages,
3514
- system: buildSystemPrompt(this.agent),
3515
- tools,
3516
- temperature: this.temperature,
3517
- maxOutputTokens: this.maxOutputTokens,
3518
- maxSteps: 1
3519
- // 每次只执行一步,由我们控制循环
3520
- });
3521
- let stepText = "";
3522
- const stepToolCalls = [];
3523
- for await (const event of result.fullStream) {
3524
- switch (event.type) {
3525
- case "text-delta":
3526
- stepText += event.text;
3527
- break;
3528
- case "tool-call":
3529
- stepToolCalls.push({
3530
- id: event.toolCallId,
3531
- name: event.toolName,
3532
- args: event.args,
3533
- status: "PENDING",
3534
- providerMetadata: event.providerMetadata
3535
- });
3536
- break;
3537
- case "finish":
3538
- finishReason = event.finishReason;
3539
- totalUsage.input += event.usage.tokens.input;
3540
- totalUsage.output += event.usage.tokens.output;
3541
- totalCost += event.usage.cost;
3542
- break;
3543
- }
3544
- }
3545
- if (stepText) {
3546
- fullText += (fullText && stepText ? "\n" : "") + stepText;
3547
- }
3548
- if (stepToolCalls.length === 0) {
3549
- if (stepText) {
3550
- await batchSaveMessage({
3551
- sessionId: sessionId2,
3552
- role: "ASSISTANT",
3553
- parts: [{ type: "TEXT", text: stepText }]
3519
+ const assistantParts = [];
3520
+ for await (const event of result.fullStream) {
3521
+ switch (event.type) {
3522
+ case "text-delta":
3523
+ fullText += event.text;
3524
+ break;
3525
+ case "tool-call":
3526
+ log6.debug(`Tool call: ${event.toolName}`, { args: event.args });
3527
+ break;
3528
+ case "tool-result":
3529
+ toolCalls.push({
3530
+ name: event.toolName,
3531
+ input: event.args,
3532
+ output: String(event.result),
3533
+ status: "COMPLETED"
3554
3534
  });
3555
- }
3556
- break;
3557
- }
3558
- const assistantParts = [];
3559
- if (stepText) {
3560
- assistantParts.push({ type: "TEXT", text: stepText });
3561
- }
3562
- for (const call of stepToolCalls) {
3563
- log6.debug(`Executing tool: ${call.name}`, { args: call.args });
3564
- const result2 = await this.executeTool(call.name, call.args, ctx);
3565
- call.output = result2.output;
3566
- call.status = "COMPLETED";
3567
- toolCalls.push({
3568
- name: call.name,
3569
- input: call.args,
3570
- output: result2.output,
3571
- status: "COMPLETED"
3572
- });
3573
- assistantParts.push({
3574
- type: "TOOL",
3575
- toolName: call.name,
3576
- toolCallId: call.id,
3577
- toolInput: call.args,
3578
- toolOutput: result2.output,
3579
- toolStatus: "COMPLETED",
3580
- toolMeta: call.providerMetadata
3581
- });
3535
+ assistantParts.push({
3536
+ type: "TOOL",
3537
+ toolName: event.toolName,
3538
+ toolCallId: event.toolCallId,
3539
+ toolInput: event.args,
3540
+ toolOutput: String(event.result),
3541
+ toolStatus: "COMPLETED"
3542
+ });
3543
+ break;
3544
+ case "finish":
3545
+ finishReason = event.finishReason;
3546
+ totalUsage.input += event.usage.tokens.input;
3547
+ totalUsage.output += event.usage.tokens.output;
3548
+ totalCost += event.usage.cost;
3549
+ break;
3582
3550
  }
3551
+ }
3552
+ if (fullText) {
3553
+ assistantParts.unshift({ type: "TEXT", text: fullText });
3554
+ }
3555
+ if (assistantParts.length > 0) {
3583
3556
  await batchSaveMessage({
3584
3557
  sessionId: sessionId2,
3585
3558
  role: "ASSISTANT",
@@ -3596,7 +3569,7 @@ You can read, write, and edit files, search code, and execute commands.`
3596
3569
  };
3597
3570
  }
3598
3571
  /**
3599
- * 流式对话
3572
+ * 流式对话(AI SDK 自动多步)
3600
3573
  */
3601
3574
  async *stream(message, options) {
3602
3575
  await this.ensureInitialized();
@@ -3609,105 +3582,78 @@ You can read, write, and edit files, search code, and execute commands.`
3609
3582
  const msgId = `msg_${Date.now()}`;
3610
3583
  const ctx = this.createToolContext(sessionId2, msgId);
3611
3584
  const tools = getAIToolsForAgent(this.agent, ctx, this.mode);
3585
+ const messages = await getSessionMessages(sessionId2);
3586
+ const modelMessages = await toModelMessages(messages);
3612
3587
  let fullText = "";
3613
3588
  const toolCalls = [];
3614
3589
  let totalUsage = { input: 0, output: 0 };
3615
3590
  let totalCost = 0;
3616
3591
  let finishReason = "unknown";
3617
- let step = 0;
3592
+ const assistantParts = [];
3618
3593
  try {
3619
- while (step < this.maxSteps) {
3620
- step++;
3621
- const messages = await getSessionMessages(sessionId2);
3622
- const modelMessages = await toModelMessages(messages);
3623
- const result = await stream({
3624
- providerId: this.providerId,
3625
- modelId: this.modelId,
3626
- messages: modelMessages,
3627
- system: buildSystemPrompt(this.agent),
3628
- tools,
3629
- temperature: this.temperature,
3630
- maxOutputTokens: this.maxOutputTokens,
3631
- maxSteps: 1
3632
- });
3633
- let stepText = "";
3634
- const stepToolCalls = [];
3635
- for await (const event of result.fullStream) {
3636
- switch (event.type) {
3637
- case "text-delta":
3638
- stepText += event.text;
3639
- yield { type: "text", text: event.text };
3640
- break;
3641
- case "reasoning-delta":
3642
- yield { type: "reasoning", text: event.text };
3643
- break;
3644
- case "tool-call":
3645
- stepToolCalls.push({
3646
- id: event.toolCallId,
3647
- name: event.toolName,
3648
- args: event.args,
3649
- status: "PENDING",
3650
- providerMetadata: event.providerMetadata
3651
- });
3652
- yield {
3653
- type: "tool-start",
3654
- name: event.toolName,
3655
- input: event.args
3656
- };
3657
- break;
3658
- case "finish":
3659
- finishReason = event.finishReason;
3660
- totalUsage.input += event.usage.tokens.input;
3661
- totalUsage.output += event.usage.tokens.output;
3662
- totalCost += event.usage.cost;
3663
- break;
3664
- case "error":
3665
- yield { type: "error", error: event.error };
3666
- return;
3667
- }
3668
- }
3669
- if (stepText) {
3670
- fullText += (fullText && stepText ? "\n" : "") + stepText;
3671
- }
3672
- if (stepToolCalls.length === 0) {
3673
- if (stepText) {
3674
- await batchSaveMessage({
3675
- sessionId: sessionId2,
3676
- role: "ASSISTANT",
3677
- parts: [{ type: "TEXT", text: stepText }]
3594
+ const result = await stream({
3595
+ providerId: this.providerId,
3596
+ modelId: this.modelId,
3597
+ messages: modelMessages,
3598
+ system: buildSystemPrompt(this.agent),
3599
+ tools,
3600
+ temperature: this.temperature,
3601
+ maxOutputTokens: this.maxOutputTokens,
3602
+ maxSteps: this.maxSteps
3603
+ // AI SDK 自动处理多步
3604
+ });
3605
+ for await (const event of result.fullStream) {
3606
+ switch (event.type) {
3607
+ case "text-delta":
3608
+ fullText += event.text;
3609
+ yield { type: "text", text: event.text };
3610
+ break;
3611
+ case "reasoning-delta":
3612
+ yield { type: "reasoning", text: event.text };
3613
+ break;
3614
+ case "tool-call":
3615
+ yield {
3616
+ type: "tool-start",
3617
+ name: event.toolName,
3618
+ input: event.args
3619
+ };
3620
+ break;
3621
+ case "tool-result":
3622
+ toolCalls.push({
3623
+ name: event.toolName,
3624
+ input: event.args,
3625
+ output: String(event.result),
3626
+ status: "COMPLETED"
3678
3627
  });
3679
- }
3680
- break;
3681
- }
3682
- const assistantParts = [];
3683
- if (stepText) {
3684
- assistantParts.push({ type: "TEXT", text: stepText });
3685
- }
3686
- for (const call of stepToolCalls) {
3687
- const toolResult = await this.executeTool(call.name, call.args, ctx);
3688
- call.output = toolResult.output;
3689
- call.status = "COMPLETED";
3690
- toolCalls.push({
3691
- name: call.name,
3692
- input: call.args,
3693
- output: toolResult.output,
3694
- status: "COMPLETED"
3695
- });
3696
- yield {
3697
- type: "tool-end",
3698
- name: call.name,
3699
- output: toolResult.output
3700
- };
3701
- assistantParts.push({
3702
- type: "TOOL",
3703
- toolName: call.name,
3704
- toolCallId: call.id,
3705
- toolInput: call.args,
3706
- toolOutput: toolResult.output,
3707
- toolStatus: "COMPLETED",
3708
- toolMeta: call.providerMetadata
3709
- });
3628
+ assistantParts.push({
3629
+ type: "TOOL",
3630
+ toolName: event.toolName,
3631
+ toolCallId: event.toolCallId,
3632
+ toolInput: event.args,
3633
+ toolOutput: String(event.result),
3634
+ toolStatus: "COMPLETED"
3635
+ });
3636
+ yield {
3637
+ type: "tool-end",
3638
+ name: event.toolName,
3639
+ output: String(event.result)
3640
+ };
3641
+ break;
3642
+ case "finish":
3643
+ finishReason = event.finishReason;
3644
+ totalUsage.input += event.usage.tokens.input;
3645
+ totalUsage.output += event.usage.tokens.output;
3646
+ totalCost += event.usage.cost;
3647
+ break;
3648
+ case "error":
3649
+ yield { type: "error", error: event.error };
3650
+ return;
3710
3651
  }
3652
+ }
3653
+ if (fullText) {
3654
+ assistantParts.unshift({ type: "TEXT", text: fullText });
3655
+ }
3656
+ if (assistantParts.length > 0) {
3711
3657
  await batchSaveMessage({
3712
3658
  sessionId: sessionId2,
3713
3659
  role: "ASSISTANT",