foliko 1.0.86 → 1.0.87
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/.agent/data/default.json +25 -4227
- package/.agent/plugins/marknative/index.js +2 -7
- package/.claude/settings.local.json +179 -171
- package/examples/test-chat-debug.js +102 -0
- package/examples/test-chat-result.js +76 -0
- package/examples/test-chat-stream-diff.js +63 -0
- package/examples/test-concurrent-chat.js +60 -0
- package/examples/test-long-chat.js +77 -0
- package/examples/test-session-chat.js +93 -0
- package/package.json +1 -1
- package/plugins/session-plugin.js +21 -0
- package/src/core/agent-chat.js +102 -55
- package/src/core/agent.js +3 -61
- package/src/utils/index.js +1 -1
package/src/core/agent.js
CHANGED
|
@@ -53,10 +53,6 @@ class Agent extends EventEmitter {
|
|
|
53
53
|
// 子Agent管理
|
|
54
54
|
this._subAgents = new Map();
|
|
55
55
|
|
|
56
|
-
// 消息队列(用于 pushMessage)
|
|
57
|
-
this._messageQueue = [];
|
|
58
|
-
this._isProcessingQueue = false;
|
|
59
|
-
|
|
60
56
|
// System prompt 构建器
|
|
61
57
|
this._systemPromptBuilder = new SystemPromptBuilder();
|
|
62
58
|
this._registerDefaultPromptParts();
|
|
@@ -734,10 +730,6 @@ class Agent extends EventEmitter {
|
|
|
734
730
|
* 发送消息
|
|
735
731
|
*/
|
|
736
732
|
async chat(message, options = {}) {
|
|
737
|
-
if (this._status === 'busy') {
|
|
738
|
-
throw new Error('Agent is busy');
|
|
739
|
-
}
|
|
740
|
-
|
|
741
733
|
if (this._status === 'error') {
|
|
742
734
|
this._status = 'idle';
|
|
743
735
|
}
|
|
@@ -755,21 +747,16 @@ class Agent extends EventEmitter {
|
|
|
755
747
|
|
|
756
748
|
this._syncTools();
|
|
757
749
|
|
|
750
|
+
// 通过 handler 的 enqueue 处理(自动排队)
|
|
758
751
|
const result = await this._chatHandler.chat(enhancedMessage, options);
|
|
752
|
+
|
|
759
753
|
this._status = 'idle';
|
|
760
754
|
this.emit('status', { status: 'idle' });
|
|
761
755
|
|
|
762
|
-
// 处理队列中的下一条消息
|
|
763
|
-
setImmediate(() => this._processQueue());
|
|
764
|
-
|
|
765
756
|
return result;
|
|
766
757
|
} catch (err) {
|
|
767
758
|
this._status = 'error';
|
|
768
759
|
this.emit('status', { status: 'error', error: err.message });
|
|
769
|
-
|
|
770
|
-
// 发生错误时也要处理队列
|
|
771
|
-
setImmediate(() => this._processQueue());
|
|
772
|
-
|
|
773
760
|
throw err;
|
|
774
761
|
}
|
|
775
762
|
}
|
|
@@ -789,49 +776,10 @@ class Agent extends EventEmitter {
|
|
|
789
776
|
options.sessionId = ctx.sessionId;
|
|
790
777
|
}
|
|
791
778
|
|
|
792
|
-
//
|
|
793
|
-
if (this._status === 'busy') {
|
|
794
|
-
const queuedItem = { message, options, resolve: null, reject: null };
|
|
795
|
-
const promise = new Promise((resolve, reject) => {
|
|
796
|
-
queuedItem.resolve = resolve;
|
|
797
|
-
queuedItem.reject = reject;
|
|
798
|
-
});
|
|
799
|
-
this._messageQueue.push(queuedItem);
|
|
800
|
-
// Agent busy, message queued
|
|
801
|
-
return promise;
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
// 空闲则直接处理
|
|
779
|
+
// 直接调用 chat,由 handler 的 enqueue 处理排队
|
|
805
780
|
return this.chat(message, options);
|
|
806
781
|
}
|
|
807
782
|
|
|
808
|
-
/**
|
|
809
|
-
* 处理消息队列
|
|
810
|
-
* @private
|
|
811
|
-
*/
|
|
812
|
-
async _processQueue() {
|
|
813
|
-
if (this._isProcessingQueue || this._messageQueue.length === 0) {
|
|
814
|
-
return;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
this._isProcessingQueue = true;
|
|
818
|
-
|
|
819
|
-
// 强制设置为 idle,确保 this.chat() 能执行
|
|
820
|
-
this._status = 'idle';
|
|
821
|
-
|
|
822
|
-
while (this._messageQueue.length > 0) {
|
|
823
|
-
const item = this._messageQueue.shift();
|
|
824
|
-
try {
|
|
825
|
-
const result = await this.chat(item.message, item.options);
|
|
826
|
-
item.resolve(result);
|
|
827
|
-
} catch (err) {
|
|
828
|
-
item.reject(err);
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
this._isProcessingQueue = false;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
783
|
/**
|
|
836
784
|
* 发送消息(流式)
|
|
837
785
|
*/
|
|
@@ -861,16 +809,10 @@ class Agent extends EventEmitter {
|
|
|
861
809
|
yield* this._chatHandler.chatStream(enhancedMessage, options);
|
|
862
810
|
this._status = 'idle';
|
|
863
811
|
this.emit('status', { status: 'idle' });
|
|
864
|
-
|
|
865
|
-
// 处理队列中的下一条消息
|
|
866
|
-
setImmediate(() => this._processQueue());
|
|
867
812
|
} catch (err) {
|
|
868
813
|
this._status = 'error';
|
|
869
814
|
this.emit('status', { status: 'error', error: err.message });
|
|
870
815
|
|
|
871
|
-
// 发生错误时也要处理队列
|
|
872
|
-
setImmediate(() => this._processQueue());
|
|
873
|
-
|
|
874
816
|
throw err;
|
|
875
817
|
}
|
|
876
818
|
}
|
package/src/utils/index.js
CHANGED