@agentrun/sdk 0.0.1-test.eab07a8 → 0.0.2
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/README.md +0 -55
- package/dist/index.cjs +80 -1171
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -516
- package/dist/index.d.ts +3 -516
- package/dist/index.js +81 -1160
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -13,7 +13,6 @@ var _2 = require('lodash');
|
|
|
13
13
|
var YAML = require('js-yaml');
|
|
14
14
|
require('crc64-ecma182.js');
|
|
15
15
|
var $Devs = require('@alicloud/devs20230714');
|
|
16
|
-
var uuid = require('uuid');
|
|
17
16
|
|
|
18
17
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
18
|
|
|
@@ -4243,7 +4242,7 @@ var init_model_proxy = __esm({
|
|
|
4243
4242
|
*/
|
|
4244
4243
|
static async create(params) {
|
|
4245
4244
|
const { input, config: config2 } = params;
|
|
4246
|
-
return await this.getClient().create(input, config2);
|
|
4245
|
+
return await this.getClient().create({ input, config: config2 });
|
|
4247
4246
|
}
|
|
4248
4247
|
/**
|
|
4249
4248
|
* 根据名称删除模型代理 / Delete model proxy by name
|
|
@@ -4253,11 +4252,11 @@ var init_model_proxy = __esm({
|
|
|
4253
4252
|
*/
|
|
4254
4253
|
static async delete(params) {
|
|
4255
4254
|
const { name, config: config2 } = params;
|
|
4256
|
-
return await this.getClient().delete(
|
|
4255
|
+
return await this.getClient().delete({
|
|
4257
4256
|
name,
|
|
4258
|
-
exports.BackendType.PROXY,
|
|
4259
|
-
config2
|
|
4260
|
-
);
|
|
4257
|
+
backendType: exports.BackendType.PROXY,
|
|
4258
|
+
config: config2
|
|
4259
|
+
});
|
|
4261
4260
|
}
|
|
4262
4261
|
/**
|
|
4263
4262
|
* 根据名称更新模型代理 / Update model proxy by name
|
|
@@ -4267,7 +4266,7 @@ var init_model_proxy = __esm({
|
|
|
4267
4266
|
*/
|
|
4268
4267
|
static async update(params) {
|
|
4269
4268
|
const { name, input, config: config2 } = params;
|
|
4270
|
-
return await this.getClient().update(name, input, config2);
|
|
4269
|
+
return await this.getClient().update({ name, input, config: config2 });
|
|
4271
4270
|
}
|
|
4272
4271
|
/**
|
|
4273
4272
|
* 根据名称获取模型代理 / Get model proxy by name
|
|
@@ -4277,11 +4276,11 @@ var init_model_proxy = __esm({
|
|
|
4277
4276
|
*/
|
|
4278
4277
|
static async get(params) {
|
|
4279
4278
|
const { name, config: config2 } = params;
|
|
4280
|
-
return await this.getClient().get(
|
|
4279
|
+
return await this.getClient().get({
|
|
4281
4280
|
name,
|
|
4282
|
-
exports.BackendType.PROXY,
|
|
4283
|
-
config2
|
|
4284
|
-
);
|
|
4281
|
+
backendType: exports.BackendType.PROXY,
|
|
4282
|
+
config: config2
|
|
4283
|
+
});
|
|
4285
4284
|
}
|
|
4286
4285
|
/**
|
|
4287
4286
|
* 列出模型代理(分页)/ List model proxies (paginated)
|
|
@@ -4292,13 +4291,15 @@ var init_model_proxy = __esm({
|
|
|
4292
4291
|
* @returns 模型代理列表 / Model proxy list
|
|
4293
4292
|
*/
|
|
4294
4293
|
static async listPage(pageInput, config2, kwargs) {
|
|
4295
|
-
return await this.getClient().list(
|
|
4296
|
-
{
|
|
4294
|
+
return await this.getClient().list({
|
|
4295
|
+
input: {
|
|
4296
|
+
modelProxyName: void 0,
|
|
4297
|
+
// 标识这是 ModelProxyListInput
|
|
4297
4298
|
...kwargs,
|
|
4298
4299
|
...pageInput
|
|
4299
4300
|
},
|
|
4300
|
-
config2
|
|
4301
|
-
);
|
|
4301
|
+
config: config2
|
|
4302
|
+
});
|
|
4302
4303
|
}
|
|
4303
4304
|
/**
|
|
4304
4305
|
* 列出所有模型代理 / List all model proxies
|
|
@@ -4308,14 +4309,32 @@ var init_model_proxy = __esm({
|
|
|
4308
4309
|
* @returns 模型代理列表 / Model proxy list
|
|
4309
4310
|
*/
|
|
4310
4311
|
static async listAll(options) {
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4312
|
+
const allResults = [];
|
|
4313
|
+
let page = 1;
|
|
4314
|
+
const pageSize = 50;
|
|
4315
|
+
while (true) {
|
|
4316
|
+
const pageResults = await this.listPage(
|
|
4317
|
+
{ pageNumber: page, pageSize },
|
|
4318
|
+
options?.config,
|
|
4319
|
+
{
|
|
4320
|
+
proxyMode: options?.proxyMode,
|
|
4321
|
+
status: options?.status
|
|
4322
|
+
}
|
|
4323
|
+
);
|
|
4324
|
+
page += 1;
|
|
4325
|
+
allResults.push(...pageResults);
|
|
4326
|
+
if (pageResults.length < pageSize) break;
|
|
4327
|
+
}
|
|
4328
|
+
const resultSet = /* @__PURE__ */ new Set();
|
|
4329
|
+
const results = [];
|
|
4330
|
+
for (const item of allResults) {
|
|
4331
|
+
const uniqId = item.modelProxyId || "";
|
|
4332
|
+
if (!resultSet.has(uniqId)) {
|
|
4333
|
+
resultSet.add(uniqId);
|
|
4334
|
+
results.push(item);
|
|
4317
4335
|
}
|
|
4318
|
-
|
|
4336
|
+
}
|
|
4337
|
+
return results;
|
|
4319
4338
|
}
|
|
4320
4339
|
/**
|
|
4321
4340
|
* 更新模型代理 / Update model proxy
|
|
@@ -4540,7 +4559,7 @@ var init_model_service = __esm({
|
|
|
4540
4559
|
*/
|
|
4541
4560
|
static async create(params) {
|
|
4542
4561
|
const { input, config: config2 } = params;
|
|
4543
|
-
return await this.getClient().create(input, config2);
|
|
4562
|
+
return await this.getClient().create({ input, config: config2 });
|
|
4544
4563
|
}
|
|
4545
4564
|
/**
|
|
4546
4565
|
* 根据名称删除模型服务 / Delete model service by name
|
|
@@ -4550,11 +4569,11 @@ var init_model_service = __esm({
|
|
|
4550
4569
|
*/
|
|
4551
4570
|
static async delete(params) {
|
|
4552
4571
|
const { name, config: config2 } = params;
|
|
4553
|
-
return await this.getClient().delete(
|
|
4572
|
+
return await this.getClient().delete({
|
|
4554
4573
|
name,
|
|
4555
|
-
exports.BackendType.SERVICE,
|
|
4556
|
-
config2
|
|
4557
|
-
);
|
|
4574
|
+
backendType: exports.BackendType.SERVICE,
|
|
4575
|
+
config: config2
|
|
4576
|
+
});
|
|
4558
4577
|
}
|
|
4559
4578
|
/**
|
|
4560
4579
|
* 根据名称更新模型服务 / Update model service by name
|
|
@@ -4564,7 +4583,7 @@ var init_model_service = __esm({
|
|
|
4564
4583
|
*/
|
|
4565
4584
|
static async update(params) {
|
|
4566
4585
|
const { name, input, config: config2 } = params;
|
|
4567
|
-
return await this.getClient().update(name, input, config2);
|
|
4586
|
+
return await this.getClient().update({ name, input, config: config2 });
|
|
4568
4587
|
}
|
|
4569
4588
|
/**
|
|
4570
4589
|
* 根据名称获取模型服务 / Get model service by name
|
|
@@ -4574,11 +4593,11 @@ var init_model_service = __esm({
|
|
|
4574
4593
|
*/
|
|
4575
4594
|
static async get(params) {
|
|
4576
4595
|
const { name, config: config2 } = params;
|
|
4577
|
-
return await this.getClient().get(
|
|
4596
|
+
return await this.getClient().get({
|
|
4578
4597
|
name,
|
|
4579
|
-
exports.BackendType.SERVICE,
|
|
4580
|
-
config2
|
|
4581
|
-
);
|
|
4598
|
+
backendType: exports.BackendType.SERVICE,
|
|
4599
|
+
config: config2
|
|
4600
|
+
});
|
|
4582
4601
|
}
|
|
4583
4602
|
/**
|
|
4584
4603
|
* 列出模型服务(分页)/ List model services (paginated)
|
|
@@ -4589,13 +4608,13 @@ var init_model_service = __esm({
|
|
|
4589
4608
|
* @returns 模型服务列表 / Model service list
|
|
4590
4609
|
*/
|
|
4591
4610
|
static async listPage(pageInput, config2, kwargs) {
|
|
4592
|
-
return await this.getClient().list(
|
|
4593
|
-
{
|
|
4611
|
+
return await this.getClient().list({
|
|
4612
|
+
input: {
|
|
4594
4613
|
...kwargs,
|
|
4595
4614
|
...pageInput
|
|
4596
4615
|
},
|
|
4597
|
-
config2
|
|
4598
|
-
);
|
|
4616
|
+
config: config2
|
|
4617
|
+
});
|
|
4599
4618
|
}
|
|
4600
4619
|
/**
|
|
4601
4620
|
* 列出所有模型服务 / List all model services
|
|
@@ -4605,14 +4624,32 @@ var init_model_service = __esm({
|
|
|
4605
4624
|
* @returns 模型服务列表 / Model service list
|
|
4606
4625
|
*/
|
|
4607
4626
|
static async listAll(options) {
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
|
|
4611
|
-
|
|
4612
|
-
|
|
4613
|
-
|
|
4627
|
+
const allResults = [];
|
|
4628
|
+
let page = 1;
|
|
4629
|
+
const pageSize = 50;
|
|
4630
|
+
while (true) {
|
|
4631
|
+
const pageResults = await this.listPage(
|
|
4632
|
+
{ pageNumber: page, pageSize },
|
|
4633
|
+
options?.config,
|
|
4634
|
+
{
|
|
4635
|
+
modelType: options?.modelType,
|
|
4636
|
+
provider: options?.provider
|
|
4637
|
+
}
|
|
4638
|
+
);
|
|
4639
|
+
page += 1;
|
|
4640
|
+
allResults.push(...pageResults);
|
|
4641
|
+
if (pageResults.length < pageSize) break;
|
|
4642
|
+
}
|
|
4643
|
+
const resultSet = /* @__PURE__ */ new Set();
|
|
4644
|
+
const results = [];
|
|
4645
|
+
for (const item of allResults) {
|
|
4646
|
+
const uniqId = item.modelServiceId || "";
|
|
4647
|
+
if (!resultSet.has(uniqId)) {
|
|
4648
|
+
resultSet.add(uniqId);
|
|
4649
|
+
results.push(item);
|
|
4614
4650
|
}
|
|
4615
|
-
|
|
4651
|
+
}
|
|
4652
|
+
return results;
|
|
4616
4653
|
}
|
|
4617
4654
|
/**
|
|
4618
4655
|
* 更新模型服务 / Update model service
|
|
@@ -8619,1126 +8656,9 @@ init_log();
|
|
|
8619
8656
|
// src/toolset/index.ts
|
|
8620
8657
|
init_mcp();
|
|
8621
8658
|
|
|
8622
|
-
// src/server/server.ts
|
|
8623
|
-
init_log();
|
|
8624
|
-
|
|
8625
|
-
// src/server/model.ts
|
|
8626
|
-
var MessageRole = /* @__PURE__ */ ((MessageRole3) => {
|
|
8627
|
-
MessageRole3["SYSTEM"] = "system";
|
|
8628
|
-
MessageRole3["USER"] = "user";
|
|
8629
|
-
MessageRole3["ASSISTANT"] = "assistant";
|
|
8630
|
-
MessageRole3["TOOL"] = "tool";
|
|
8631
|
-
return MessageRole3;
|
|
8632
|
-
})(MessageRole || {});
|
|
8633
|
-
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
8634
|
-
EventType2["TEXT"] = "TEXT";
|
|
8635
|
-
EventType2["TOOL_CALL"] = "TOOL_CALL";
|
|
8636
|
-
EventType2["TOOL_CALL_CHUNK"] = "TOOL_CALL_CHUNK";
|
|
8637
|
-
EventType2["TOOL_RESULT"] = "TOOL_RESULT";
|
|
8638
|
-
EventType2["TOOL_RESULT_CHUNK"] = "TOOL_RESULT_CHUNK";
|
|
8639
|
-
EventType2["ERROR"] = "ERROR";
|
|
8640
|
-
EventType2["STATE"] = "STATE";
|
|
8641
|
-
EventType2["HITL"] = "HITL";
|
|
8642
|
-
EventType2["CUSTOM"] = "CUSTOM";
|
|
8643
|
-
EventType2["RAW"] = "RAW";
|
|
8644
|
-
return EventType2;
|
|
8645
|
-
})(EventType || {});
|
|
8646
|
-
|
|
8647
|
-
// src/server/agui-protocol.ts
|
|
8648
|
-
var AGUI_EVENT_TYPES = {
|
|
8649
|
-
RUN_STARTED: "RUN_STARTED",
|
|
8650
|
-
RUN_FINISHED: "RUN_FINISHED",
|
|
8651
|
-
RUN_ERROR: "RUN_ERROR",
|
|
8652
|
-
TEXT_MESSAGE_START: "TEXT_MESSAGE_START",
|
|
8653
|
-
TEXT_MESSAGE_CONTENT: "TEXT_MESSAGE_CONTENT",
|
|
8654
|
-
TEXT_MESSAGE_END: "TEXT_MESSAGE_END",
|
|
8655
|
-
TOOL_CALL_START: "TOOL_CALL_START",
|
|
8656
|
-
TOOL_CALL_ARGS: "TOOL_CALL_ARGS",
|
|
8657
|
-
TOOL_CALL_END: "TOOL_CALL_END",
|
|
8658
|
-
TOOL_CALL_RESULT: "TOOL_CALL_RESULT",
|
|
8659
|
-
STATE_SNAPSHOT: "STATE_SNAPSHOT",
|
|
8660
|
-
STATE_DELTA: "STATE_DELTA",
|
|
8661
|
-
CUSTOM: "CUSTOM"};
|
|
8662
|
-
var EventEncoder = class {
|
|
8663
|
-
getContentType() {
|
|
8664
|
-
return "text/event-stream";
|
|
8665
|
-
}
|
|
8666
|
-
encode(event) {
|
|
8667
|
-
return `data: ${JSON.stringify(event)}
|
|
8668
|
-
|
|
8669
|
-
`;
|
|
8670
|
-
}
|
|
8671
|
-
};
|
|
8672
|
-
var StreamStateMachine = class {
|
|
8673
|
-
text = {
|
|
8674
|
-
started: false,
|
|
8675
|
-
ended: false,
|
|
8676
|
-
messageId: uuid.v4()
|
|
8677
|
-
};
|
|
8678
|
-
toolCallStates = /* @__PURE__ */ new Map();
|
|
8679
|
-
toolResultChunks = /* @__PURE__ */ new Map();
|
|
8680
|
-
runErrored = false;
|
|
8681
|
-
/**
|
|
8682
|
-
* End all open tool calls
|
|
8683
|
-
*/
|
|
8684
|
-
*endAllTools(encoder, exclude) {
|
|
8685
|
-
for (const [toolId, state] of this.toolCallStates) {
|
|
8686
|
-
if (exclude && toolId === exclude) {
|
|
8687
|
-
continue;
|
|
8688
|
-
}
|
|
8689
|
-
if (state.started && !state.ended) {
|
|
8690
|
-
yield encoder.encode({
|
|
8691
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_END,
|
|
8692
|
-
toolCallId: toolId
|
|
8693
|
-
});
|
|
8694
|
-
state.ended = true;
|
|
8695
|
-
}
|
|
8696
|
-
}
|
|
8697
|
-
}
|
|
8698
|
-
/**
|
|
8699
|
-
* Ensure text message has started
|
|
8700
|
-
*/
|
|
8701
|
-
*ensureTextStarted(encoder) {
|
|
8702
|
-
if (!this.text.started || this.text.ended) {
|
|
8703
|
-
if (this.text.ended) {
|
|
8704
|
-
this.text = {
|
|
8705
|
-
started: false,
|
|
8706
|
-
ended: false,
|
|
8707
|
-
messageId: uuid.v4()
|
|
8708
|
-
};
|
|
8709
|
-
}
|
|
8710
|
-
yield encoder.encode({
|
|
8711
|
-
type: AGUI_EVENT_TYPES.TEXT_MESSAGE_START,
|
|
8712
|
-
messageId: this.text.messageId,
|
|
8713
|
-
role: "assistant"
|
|
8714
|
-
});
|
|
8715
|
-
this.text.started = true;
|
|
8716
|
-
this.text.ended = false;
|
|
8717
|
-
}
|
|
8718
|
-
}
|
|
8719
|
-
/**
|
|
8720
|
-
* End text message if open
|
|
8721
|
-
*/
|
|
8722
|
-
*endTextIfOpen(encoder) {
|
|
8723
|
-
if (this.text.started && !this.text.ended) {
|
|
8724
|
-
yield encoder.encode({
|
|
8725
|
-
type: AGUI_EVENT_TYPES.TEXT_MESSAGE_END,
|
|
8726
|
-
messageId: this.text.messageId
|
|
8727
|
-
});
|
|
8728
|
-
this.text.ended = true;
|
|
8729
|
-
}
|
|
8730
|
-
}
|
|
8731
|
-
/**
|
|
8732
|
-
* Cache tool result chunk
|
|
8733
|
-
*/
|
|
8734
|
-
cacheToolResultChunk(toolId, delta) {
|
|
8735
|
-
if (!toolId || delta === null || delta === void 0) {
|
|
8736
|
-
return;
|
|
8737
|
-
}
|
|
8738
|
-
if (delta) {
|
|
8739
|
-
const chunks = this.toolResultChunks.get(toolId) || [];
|
|
8740
|
-
chunks.push(delta);
|
|
8741
|
-
this.toolResultChunks.set(toolId, chunks);
|
|
8742
|
-
}
|
|
8743
|
-
}
|
|
8744
|
-
/**
|
|
8745
|
-
* Pop and concatenate cached tool result chunks
|
|
8746
|
-
*/
|
|
8747
|
-
popToolResultChunks(toolId) {
|
|
8748
|
-
const chunks = this.toolResultChunks.get(toolId) || [];
|
|
8749
|
-
this.toolResultChunks.delete(toolId);
|
|
8750
|
-
return chunks.join("");
|
|
8751
|
-
}
|
|
8752
|
-
};
|
|
8753
|
-
var DEFAULT_PREFIX = "/ag-ui/agent";
|
|
8754
|
-
var AGUIProtocolHandler = class {
|
|
8755
|
-
name = "ag-ui";
|
|
8756
|
-
config;
|
|
8757
|
-
encoder = new EventEncoder();
|
|
8758
|
-
constructor(config2) {
|
|
8759
|
-
this.config = config2?.agui;
|
|
8760
|
-
}
|
|
8761
|
-
/**
|
|
8762
|
-
* Get the route prefix
|
|
8763
|
-
*/
|
|
8764
|
-
getPrefix() {
|
|
8765
|
-
return this.config?.prefix ?? DEFAULT_PREFIX;
|
|
8766
|
-
}
|
|
8767
|
-
/**
|
|
8768
|
-
* Get the encoder
|
|
8769
|
-
*/
|
|
8770
|
-
getEncoder() {
|
|
8771
|
-
return this.encoder;
|
|
8772
|
-
}
|
|
8773
|
-
/**
|
|
8774
|
-
* Parse AG-UI request
|
|
8775
|
-
*/
|
|
8776
|
-
parseRequest(requestData) {
|
|
8777
|
-
const context = {
|
|
8778
|
-
threadId: requestData.threadId || uuid.v4(),
|
|
8779
|
-
runId: requestData.runId || uuid.v4()
|
|
8780
|
-
};
|
|
8781
|
-
const messages = this.parseMessages(
|
|
8782
|
-
requestData.messages || []
|
|
8783
|
-
);
|
|
8784
|
-
const tools = this.parseTools(
|
|
8785
|
-
requestData.tools
|
|
8786
|
-
);
|
|
8787
|
-
const agentRequest = {
|
|
8788
|
-
protocol: "agui",
|
|
8789
|
-
messages,
|
|
8790
|
-
stream: true,
|
|
8791
|
-
// AG-UI always streams
|
|
8792
|
-
tools: tools || void 0,
|
|
8793
|
-
model: requestData.model,
|
|
8794
|
-
metadata: requestData.metadata
|
|
8795
|
-
};
|
|
8796
|
-
return { agentRequest, context };
|
|
8797
|
-
}
|
|
8798
|
-
/**
|
|
8799
|
-
* Parse messages list
|
|
8800
|
-
*/
|
|
8801
|
-
parseMessages(rawMessages) {
|
|
8802
|
-
const messages = [];
|
|
8803
|
-
for (const msgData of rawMessages) {
|
|
8804
|
-
if (typeof msgData !== "object" || msgData === null) {
|
|
8805
|
-
continue;
|
|
8806
|
-
}
|
|
8807
|
-
const roleStr = msgData.role || "user";
|
|
8808
|
-
let role;
|
|
8809
|
-
try {
|
|
8810
|
-
role = roleStr;
|
|
8811
|
-
if (!Object.values(MessageRole).includes(role)) {
|
|
8812
|
-
role = "user" /* USER */;
|
|
8813
|
-
}
|
|
8814
|
-
} catch {
|
|
8815
|
-
role = "user" /* USER */;
|
|
8816
|
-
}
|
|
8817
|
-
let toolCalls;
|
|
8818
|
-
const rawToolCalls = msgData.toolCalls;
|
|
8819
|
-
if (rawToolCalls && Array.isArray(rawToolCalls)) {
|
|
8820
|
-
toolCalls = rawToolCalls.map((tc) => ({
|
|
8821
|
-
id: tc.id || "",
|
|
8822
|
-
type: tc.type || "function",
|
|
8823
|
-
function: tc.function || {}
|
|
8824
|
-
}));
|
|
8825
|
-
}
|
|
8826
|
-
messages.push({
|
|
8827
|
-
id: msgData.id,
|
|
8828
|
-
role,
|
|
8829
|
-
content: msgData.content,
|
|
8830
|
-
name: msgData.name,
|
|
8831
|
-
toolCalls,
|
|
8832
|
-
toolCallId: msgData.toolCallId
|
|
8833
|
-
});
|
|
8834
|
-
}
|
|
8835
|
-
return messages;
|
|
8836
|
-
}
|
|
8837
|
-
/**
|
|
8838
|
-
* Parse tools list
|
|
8839
|
-
*/
|
|
8840
|
-
parseTools(rawTools) {
|
|
8841
|
-
if (!rawTools || !Array.isArray(rawTools)) {
|
|
8842
|
-
return null;
|
|
8843
|
-
}
|
|
8844
|
-
const tools = [];
|
|
8845
|
-
for (const toolData of rawTools) {
|
|
8846
|
-
if (typeof toolData !== "object" || toolData === null) {
|
|
8847
|
-
continue;
|
|
8848
|
-
}
|
|
8849
|
-
tools.push({
|
|
8850
|
-
type: toolData.type || "function",
|
|
8851
|
-
function: toolData.function || { name: "" }
|
|
8852
|
-
});
|
|
8853
|
-
}
|
|
8854
|
-
return tools.length > 0 ? tools : null;
|
|
8855
|
-
}
|
|
8856
|
-
/**
|
|
8857
|
-
* Format event stream as AG-UI SSE format
|
|
8858
|
-
*/
|
|
8859
|
-
async *formatStream(eventStream, context) {
|
|
8860
|
-
const state = new StreamStateMachine();
|
|
8861
|
-
yield this.encoder.encode({
|
|
8862
|
-
type: AGUI_EVENT_TYPES.RUN_STARTED,
|
|
8863
|
-
threadId: context.threadId,
|
|
8864
|
-
runId: context.runId
|
|
8865
|
-
});
|
|
8866
|
-
for await (const rawEvent of eventStream) {
|
|
8867
|
-
if (state.runErrored) {
|
|
8868
|
-
continue;
|
|
8869
|
-
}
|
|
8870
|
-
const event = typeof rawEvent === "string" ? { event: "TEXT" /* TEXT */, data: { delta: rawEvent } } : rawEvent;
|
|
8871
|
-
if (event.event === "ERROR" /* ERROR */) {
|
|
8872
|
-
state.runErrored = true;
|
|
8873
|
-
}
|
|
8874
|
-
for (const sseData of this.processEventWithBoundaries(
|
|
8875
|
-
event,
|
|
8876
|
-
context,
|
|
8877
|
-
state
|
|
8878
|
-
)) {
|
|
8879
|
-
if (sseData) {
|
|
8880
|
-
yield sseData;
|
|
8881
|
-
}
|
|
8882
|
-
}
|
|
8883
|
-
}
|
|
8884
|
-
if (state.runErrored) {
|
|
8885
|
-
return;
|
|
8886
|
-
}
|
|
8887
|
-
for (const sseData of state.endAllTools(this.encoder)) {
|
|
8888
|
-
yield sseData;
|
|
8889
|
-
}
|
|
8890
|
-
for (const sseData of state.endTextIfOpen(this.encoder)) {
|
|
8891
|
-
yield sseData;
|
|
8892
|
-
}
|
|
8893
|
-
yield this.encoder.encode({
|
|
8894
|
-
type: AGUI_EVENT_TYPES.RUN_FINISHED,
|
|
8895
|
-
threadId: context.threadId,
|
|
8896
|
-
runId: context.runId
|
|
8897
|
-
});
|
|
8898
|
-
}
|
|
8899
|
-
/**
|
|
8900
|
-
* Process event and inject boundary events
|
|
8901
|
-
*/
|
|
8902
|
-
*processEventWithBoundaries(event, context, state) {
|
|
8903
|
-
if (event.event === "RAW" /* RAW */) {
|
|
8904
|
-
let rawData = event.data?.raw || "";
|
|
8905
|
-
if (rawData) {
|
|
8906
|
-
if (!rawData.endsWith("\n\n")) {
|
|
8907
|
-
rawData = rawData.replace(/\n+$/, "") + "\n\n";
|
|
8908
|
-
}
|
|
8909
|
-
yield rawData;
|
|
8910
|
-
}
|
|
8911
|
-
return;
|
|
8912
|
-
}
|
|
8913
|
-
if (event.event === "TEXT" /* TEXT */) {
|
|
8914
|
-
for (const sseData of state.endAllTools(this.encoder)) {
|
|
8915
|
-
yield sseData;
|
|
8916
|
-
}
|
|
8917
|
-
for (const sseData of state.ensureTextStarted(this.encoder)) {
|
|
8918
|
-
yield sseData;
|
|
8919
|
-
}
|
|
8920
|
-
const aguiEvent = {
|
|
8921
|
-
type: AGUI_EVENT_TYPES.TEXT_MESSAGE_CONTENT,
|
|
8922
|
-
messageId: state.text.messageId,
|
|
8923
|
-
delta: event.data?.delta || ""
|
|
8924
|
-
};
|
|
8925
|
-
if (event.addition) {
|
|
8926
|
-
const merged = this.applyAddition(
|
|
8927
|
-
aguiEvent,
|
|
8928
|
-
event.addition,
|
|
8929
|
-
event.additionMergeOptions
|
|
8930
|
-
);
|
|
8931
|
-
yield `data: ${JSON.stringify(merged)}
|
|
8932
|
-
|
|
8933
|
-
`;
|
|
8934
|
-
} else {
|
|
8935
|
-
yield this.encoder.encode(aguiEvent);
|
|
8936
|
-
}
|
|
8937
|
-
return;
|
|
8938
|
-
}
|
|
8939
|
-
if (event.event === "TOOL_CALL_CHUNK" /* TOOL_CALL_CHUNK */) {
|
|
8940
|
-
const toolId = event.data?.id || "";
|
|
8941
|
-
const toolName = event.data?.name || "";
|
|
8942
|
-
for (const sseData of state.endTextIfOpen(this.encoder)) {
|
|
8943
|
-
yield sseData;
|
|
8944
|
-
}
|
|
8945
|
-
let needStart = false;
|
|
8946
|
-
const currentState = state.toolCallStates.get(toolId);
|
|
8947
|
-
if (toolId) {
|
|
8948
|
-
if (!currentState || currentState.ended) {
|
|
8949
|
-
needStart = true;
|
|
8950
|
-
}
|
|
8951
|
-
}
|
|
8952
|
-
if (needStart) {
|
|
8953
|
-
yield this.encoder.encode({
|
|
8954
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_START,
|
|
8955
|
-
toolCallId: toolId,
|
|
8956
|
-
toolCallName: toolName
|
|
8957
|
-
});
|
|
8958
|
-
state.toolCallStates.set(toolId, {
|
|
8959
|
-
name: toolName,
|
|
8960
|
-
started: true,
|
|
8961
|
-
ended: false,
|
|
8962
|
-
hasResult: false,
|
|
8963
|
-
isHitl: false
|
|
8964
|
-
});
|
|
8965
|
-
}
|
|
8966
|
-
yield this.encoder.encode({
|
|
8967
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_ARGS,
|
|
8968
|
-
toolCallId: toolId,
|
|
8969
|
-
delta: event.data?.args_delta || event.data?.argsDelta || ""
|
|
8970
|
-
});
|
|
8971
|
-
return;
|
|
8972
|
-
}
|
|
8973
|
-
if (event.event === "TOOL_CALL" /* TOOL_CALL */) {
|
|
8974
|
-
const toolId = event.data?.id || "";
|
|
8975
|
-
const toolName = event.data?.name || "";
|
|
8976
|
-
const toolArgs = event.data?.args || "";
|
|
8977
|
-
for (const sseData of state.endTextIfOpen(this.encoder)) {
|
|
8978
|
-
yield sseData;
|
|
8979
|
-
}
|
|
8980
|
-
let needStart = false;
|
|
8981
|
-
const currentState = state.toolCallStates.get(toolId);
|
|
8982
|
-
if (toolId) {
|
|
8983
|
-
if (!currentState || currentState.ended) {
|
|
8984
|
-
needStart = true;
|
|
8985
|
-
}
|
|
8986
|
-
}
|
|
8987
|
-
if (needStart) {
|
|
8988
|
-
yield this.encoder.encode({
|
|
8989
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_START,
|
|
8990
|
-
toolCallId: toolId,
|
|
8991
|
-
toolCallName: toolName
|
|
8992
|
-
});
|
|
8993
|
-
state.toolCallStates.set(toolId, {
|
|
8994
|
-
name: toolName,
|
|
8995
|
-
started: true,
|
|
8996
|
-
ended: false,
|
|
8997
|
-
hasResult: false,
|
|
8998
|
-
isHitl: false
|
|
8999
|
-
});
|
|
9000
|
-
}
|
|
9001
|
-
if (toolArgs) {
|
|
9002
|
-
yield this.encoder.encode({
|
|
9003
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_ARGS,
|
|
9004
|
-
toolCallId: toolId,
|
|
9005
|
-
delta: toolArgs
|
|
9006
|
-
});
|
|
9007
|
-
}
|
|
9008
|
-
return;
|
|
9009
|
-
}
|
|
9010
|
-
if (event.event === "TOOL_RESULT_CHUNK" /* TOOL_RESULT_CHUNK */) {
|
|
9011
|
-
const toolId = event.data?.id || "";
|
|
9012
|
-
const delta = event.data?.delta || "";
|
|
9013
|
-
state.cacheToolResultChunk(toolId, delta);
|
|
9014
|
-
return;
|
|
9015
|
-
}
|
|
9016
|
-
if (event.event === "HITL" /* HITL */) {
|
|
9017
|
-
const hitlId = event.data?.id || "";
|
|
9018
|
-
const toolCallId = event.data?.tool_call_id || event.data?.toolCallId || "";
|
|
9019
|
-
const hitlType = event.data?.type || "confirmation";
|
|
9020
|
-
const prompt = event.data?.prompt || "";
|
|
9021
|
-
const options = event.data?.options;
|
|
9022
|
-
const defaultValue = event.data?.default;
|
|
9023
|
-
const timeout = event.data?.timeout;
|
|
9024
|
-
const schema = event.data?.schema;
|
|
9025
|
-
for (const sseData of state.endTextIfOpen(this.encoder)) {
|
|
9026
|
-
yield sseData;
|
|
9027
|
-
}
|
|
9028
|
-
if (toolCallId && state.toolCallStates.has(toolCallId)) {
|
|
9029
|
-
const toolState = state.toolCallStates.get(toolCallId);
|
|
9030
|
-
if (toolState.started && !toolState.ended) {
|
|
9031
|
-
yield this.encoder.encode({
|
|
9032
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_END,
|
|
9033
|
-
toolCallId
|
|
9034
|
-
});
|
|
9035
|
-
toolState.ended = true;
|
|
9036
|
-
}
|
|
9037
|
-
toolState.isHitl = true;
|
|
9038
|
-
toolState.hasResult = false;
|
|
9039
|
-
return;
|
|
9040
|
-
}
|
|
9041
|
-
const argsDict = {
|
|
9042
|
-
type: hitlType,
|
|
9043
|
-
prompt
|
|
9044
|
-
};
|
|
9045
|
-
if (options) argsDict.options = options;
|
|
9046
|
-
if (defaultValue !== void 0) argsDict.default = defaultValue;
|
|
9047
|
-
if (timeout !== void 0) argsDict.timeout = timeout;
|
|
9048
|
-
if (schema) argsDict.schema = schema;
|
|
9049
|
-
const argsJson = JSON.stringify(argsDict);
|
|
9050
|
-
const actualId = toolCallId || hitlId;
|
|
9051
|
-
yield this.encoder.encode({
|
|
9052
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_START,
|
|
9053
|
-
toolCallId: actualId,
|
|
9054
|
-
toolCallName: `hitl_${hitlType}`
|
|
9055
|
-
});
|
|
9056
|
-
yield this.encoder.encode({
|
|
9057
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_ARGS,
|
|
9058
|
-
toolCallId: actualId,
|
|
9059
|
-
delta: argsJson
|
|
9060
|
-
});
|
|
9061
|
-
yield this.encoder.encode({
|
|
9062
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_END,
|
|
9063
|
-
toolCallId: actualId
|
|
9064
|
-
});
|
|
9065
|
-
state.toolCallStates.set(actualId, {
|
|
9066
|
-
name: `hitl_${hitlType}`,
|
|
9067
|
-
started: true,
|
|
9068
|
-
ended: true,
|
|
9069
|
-
hasResult: false,
|
|
9070
|
-
isHitl: true
|
|
9071
|
-
});
|
|
9072
|
-
return;
|
|
9073
|
-
}
|
|
9074
|
-
if (event.event === "TOOL_RESULT" /* TOOL_RESULT */) {
|
|
9075
|
-
const toolId = event.data?.id || "";
|
|
9076
|
-
const toolName = event.data?.name || "";
|
|
9077
|
-
for (const sseData of state.endTextIfOpen(this.encoder)) {
|
|
9078
|
-
yield sseData;
|
|
9079
|
-
}
|
|
9080
|
-
let toolState = state.toolCallStates.get(toolId);
|
|
9081
|
-
if (toolId && !toolState) {
|
|
9082
|
-
yield this.encoder.encode({
|
|
9083
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_START,
|
|
9084
|
-
toolCallId: toolId,
|
|
9085
|
-
toolCallName: toolName
|
|
9086
|
-
});
|
|
9087
|
-
toolState = {
|
|
9088
|
-
name: toolName,
|
|
9089
|
-
started: true,
|
|
9090
|
-
ended: false,
|
|
9091
|
-
hasResult: false,
|
|
9092
|
-
isHitl: false
|
|
9093
|
-
};
|
|
9094
|
-
state.toolCallStates.set(toolId, toolState);
|
|
9095
|
-
}
|
|
9096
|
-
if (toolState && toolState.started && !toolState.ended) {
|
|
9097
|
-
yield this.encoder.encode({
|
|
9098
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_END,
|
|
9099
|
-
toolCallId: toolId
|
|
9100
|
-
});
|
|
9101
|
-
toolState.ended = true;
|
|
9102
|
-
}
|
|
9103
|
-
let finalResult = (event.data?.content || event.data?.result) ?? "";
|
|
9104
|
-
if (toolId) {
|
|
9105
|
-
const cachedChunks = state.popToolResultChunks(toolId);
|
|
9106
|
-
if (cachedChunks) {
|
|
9107
|
-
finalResult = cachedChunks + finalResult;
|
|
9108
|
-
}
|
|
9109
|
-
}
|
|
9110
|
-
yield this.encoder.encode({
|
|
9111
|
-
type: AGUI_EVENT_TYPES.TOOL_CALL_RESULT,
|
|
9112
|
-
messageId: event.data?.message_id || event.data?.messageId || `tool-result-${toolId}`,
|
|
9113
|
-
toolCallId: toolId,
|
|
9114
|
-
content: finalResult,
|
|
9115
|
-
role: "tool"
|
|
9116
|
-
});
|
|
9117
|
-
return;
|
|
9118
|
-
}
|
|
9119
|
-
if (event.event === "ERROR" /* ERROR */) {
|
|
9120
|
-
yield this.encoder.encode({
|
|
9121
|
-
type: AGUI_EVENT_TYPES.RUN_ERROR,
|
|
9122
|
-
message: event.data?.message || "",
|
|
9123
|
-
code: event.data?.code
|
|
9124
|
-
});
|
|
9125
|
-
return;
|
|
9126
|
-
}
|
|
9127
|
-
if (event.event === "STATE" /* STATE */) {
|
|
9128
|
-
if ("snapshot" in (event.data || {})) {
|
|
9129
|
-
yield this.encoder.encode({
|
|
9130
|
-
type: AGUI_EVENT_TYPES.STATE_SNAPSHOT,
|
|
9131
|
-
snapshot: event.data?.snapshot || {}
|
|
9132
|
-
});
|
|
9133
|
-
} else if ("delta" in (event.data || {})) {
|
|
9134
|
-
yield this.encoder.encode({
|
|
9135
|
-
type: AGUI_EVENT_TYPES.STATE_DELTA,
|
|
9136
|
-
delta: event.data?.delta || []
|
|
9137
|
-
});
|
|
9138
|
-
} else {
|
|
9139
|
-
yield this.encoder.encode({
|
|
9140
|
-
type: AGUI_EVENT_TYPES.STATE_SNAPSHOT,
|
|
9141
|
-
snapshot: event.data || {}
|
|
9142
|
-
});
|
|
9143
|
-
}
|
|
9144
|
-
return;
|
|
9145
|
-
}
|
|
9146
|
-
if (event.event === "CUSTOM" /* CUSTOM */) {
|
|
9147
|
-
yield this.encoder.encode({
|
|
9148
|
-
type: AGUI_EVENT_TYPES.CUSTOM,
|
|
9149
|
-
name: event.data?.name || "custom",
|
|
9150
|
-
value: event.data?.value
|
|
9151
|
-
});
|
|
9152
|
-
return;
|
|
9153
|
-
}
|
|
9154
|
-
const eventName = event.event || "unknown";
|
|
9155
|
-
yield this.encoder.encode({
|
|
9156
|
-
type: AGUI_EVENT_TYPES.CUSTOM,
|
|
9157
|
-
name: eventName,
|
|
9158
|
-
value: event.data
|
|
9159
|
-
});
|
|
9160
|
-
}
|
|
9161
|
-
/**
|
|
9162
|
-
* Apply addition fields
|
|
9163
|
-
*/
|
|
9164
|
-
applyAddition(eventData, addition, mergeOptions) {
|
|
9165
|
-
if (!addition) {
|
|
9166
|
-
return eventData;
|
|
9167
|
-
}
|
|
9168
|
-
const result = { ...eventData };
|
|
9169
|
-
for (const [key, value] of Object.entries(addition)) {
|
|
9170
|
-
if (mergeOptions?.noNewField && !(key in eventData)) {
|
|
9171
|
-
continue;
|
|
9172
|
-
}
|
|
9173
|
-
result[key] = value;
|
|
9174
|
-
}
|
|
9175
|
-
return result;
|
|
9176
|
-
}
|
|
9177
|
-
/**
|
|
9178
|
-
* Generate error stream
|
|
9179
|
-
*/
|
|
9180
|
-
async *errorStream(message) {
|
|
9181
|
-
const threadId = uuid.v4();
|
|
9182
|
-
const runId = uuid.v4();
|
|
9183
|
-
yield this.encoder.encode({
|
|
9184
|
-
type: AGUI_EVENT_TYPES.RUN_STARTED,
|
|
9185
|
-
threadId,
|
|
9186
|
-
runId
|
|
9187
|
-
});
|
|
9188
|
-
yield this.encoder.encode({
|
|
9189
|
-
type: AGUI_EVENT_TYPES.RUN_ERROR,
|
|
9190
|
-
message,
|
|
9191
|
-
code: "REQUEST_ERROR"
|
|
9192
|
-
});
|
|
9193
|
-
}
|
|
9194
|
-
};
|
|
9195
|
-
|
|
9196
|
-
// src/server/server.ts
|
|
9197
|
-
var AgentRunServer = class {
|
|
9198
|
-
invokeAgent;
|
|
9199
|
-
config;
|
|
9200
|
-
server;
|
|
9201
|
-
aguiHandler;
|
|
9202
|
-
constructor(options) {
|
|
9203
|
-
this.invokeAgent = options.invokeAgent;
|
|
9204
|
-
this.config = options.config ?? {};
|
|
9205
|
-
this.aguiHandler = new AGUIProtocolHandler(this.config);
|
|
9206
|
-
}
|
|
9207
|
-
/**
|
|
9208
|
-
* Start the HTTP server
|
|
9209
|
-
*/
|
|
9210
|
-
start(options) {
|
|
9211
|
-
const host = options?.host ?? this.config.host ?? "0.0.0.0";
|
|
9212
|
-
const port = options?.port ?? this.config.port ?? 9e3;
|
|
9213
|
-
this.server = http__namespace.createServer(async (req, res) => {
|
|
9214
|
-
this.handleCors(req, res);
|
|
9215
|
-
if (req.method === "OPTIONS") {
|
|
9216
|
-
res.writeHead(200);
|
|
9217
|
-
res.end();
|
|
9218
|
-
return;
|
|
9219
|
-
}
|
|
9220
|
-
try {
|
|
9221
|
-
await this.handleRequest(req, res);
|
|
9222
|
-
} catch (error) {
|
|
9223
|
-
logger.error("Request error:", error);
|
|
9224
|
-
res.writeHead(500, { "Content-Type": "application/json" });
|
|
9225
|
-
res.end(JSON.stringify({ error: "Internal server error" }));
|
|
9226
|
-
}
|
|
9227
|
-
});
|
|
9228
|
-
this.server.listen(port, host, () => {
|
|
9229
|
-
logger.info(`AgentRun Server started: http://${host}:${port}`);
|
|
9230
|
-
logger.info(
|
|
9231
|
-
` POST /openai/v1/chat/completions - OpenAI compatible endpoint`
|
|
9232
|
-
);
|
|
9233
|
-
logger.info(
|
|
9234
|
-
` POST ${this.aguiHandler.getPrefix()} - AG-UI compatible endpoint`
|
|
9235
|
-
);
|
|
9236
|
-
});
|
|
9237
|
-
}
|
|
9238
|
-
/**
|
|
9239
|
-
* Stop the HTTP server
|
|
9240
|
-
*/
|
|
9241
|
-
stop() {
|
|
9242
|
-
return new Promise((resolve, reject) => {
|
|
9243
|
-
if (!this.server) {
|
|
9244
|
-
resolve();
|
|
9245
|
-
return;
|
|
9246
|
-
}
|
|
9247
|
-
this.server.close((err) => {
|
|
9248
|
-
if (err) {
|
|
9249
|
-
reject(err);
|
|
9250
|
-
} else {
|
|
9251
|
-
logger.info("AgentRun Server stopped");
|
|
9252
|
-
resolve();
|
|
9253
|
-
}
|
|
9254
|
-
});
|
|
9255
|
-
});
|
|
9256
|
-
}
|
|
9257
|
-
/**
|
|
9258
|
-
* Handle CORS headers
|
|
9259
|
-
*/
|
|
9260
|
-
handleCors(req, res) {
|
|
9261
|
-
const origins = this.config.corsOrigins ?? ["*"];
|
|
9262
|
-
const origin = req.headers.origin;
|
|
9263
|
-
if (origins.includes("*") || origin && origins.includes(origin)) {
|
|
9264
|
-
res.setHeader("Access-Control-Allow-Origin", origin || "*");
|
|
9265
|
-
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
9266
|
-
res.setHeader(
|
|
9267
|
-
"Access-Control-Allow-Headers",
|
|
9268
|
-
"Content-Type, Authorization"
|
|
9269
|
-
);
|
|
9270
|
-
res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
9271
|
-
}
|
|
9272
|
-
}
|
|
9273
|
-
/**
|
|
9274
|
-
* Handle HTTP request
|
|
9275
|
-
*/
|
|
9276
|
-
async handleRequest(req, res) {
|
|
9277
|
-
const url = req.url || "/";
|
|
9278
|
-
const aguiPrefix = this.aguiHandler.getPrefix();
|
|
9279
|
-
if (url.endsWith("/v1/chat/completions") && req.method === "POST") {
|
|
9280
|
-
await this.handleChatCompletions(req, res);
|
|
9281
|
-
return;
|
|
9282
|
-
}
|
|
9283
|
-
if ((url === aguiPrefix || url.startsWith(aguiPrefix + "/")) && req.method === "POST") {
|
|
9284
|
-
await this.handleAGUI(req, res);
|
|
9285
|
-
return;
|
|
9286
|
-
}
|
|
9287
|
-
if (url === aguiPrefix + "/health" && req.method === "GET") {
|
|
9288
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
9289
|
-
res.end(
|
|
9290
|
-
JSON.stringify({ status: "ok", protocol: "ag-ui", version: "1.0" })
|
|
9291
|
-
);
|
|
9292
|
-
return;
|
|
9293
|
-
}
|
|
9294
|
-
if (url === "/health" && req.method === "GET") {
|
|
9295
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
9296
|
-
res.end(JSON.stringify({ status: "ok" }));
|
|
9297
|
-
return;
|
|
9298
|
-
}
|
|
9299
|
-
res.writeHead(404, { "Content-Type": "application/json" });
|
|
9300
|
-
res.end(JSON.stringify({ error: "Not found" }));
|
|
9301
|
-
}
|
|
9302
|
-
/**
|
|
9303
|
-
* Handle AG-UI endpoint
|
|
9304
|
-
*/
|
|
9305
|
-
async handleAGUI(req, res) {
|
|
9306
|
-
const encoder = this.aguiHandler.getEncoder();
|
|
9307
|
-
const sseHeaders = {
|
|
9308
|
-
"Content-Type": encoder.getContentType(),
|
|
9309
|
-
"Cache-Control": "no-cache",
|
|
9310
|
-
Connection: "keep-alive",
|
|
9311
|
-
"X-Accel-Buffering": "no"
|
|
9312
|
-
};
|
|
9313
|
-
try {
|
|
9314
|
-
const body = await this.parseBody(req);
|
|
9315
|
-
const { agentRequest, context } = this.aguiHandler.parseRequest(body);
|
|
9316
|
-
agentRequest.rawRequest = req;
|
|
9317
|
-
let result;
|
|
9318
|
-
try {
|
|
9319
|
-
result = await Promise.resolve(this.invokeAgent(agentRequest));
|
|
9320
|
-
} catch (error) {
|
|
9321
|
-
res.writeHead(200, sseHeaders);
|
|
9322
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
9323
|
-
for await (const chunk of this.aguiHandler.errorStream(
|
|
9324
|
-
`Internal error: ${errorMessage}`
|
|
9325
|
-
)) {
|
|
9326
|
-
res.write(chunk);
|
|
9327
|
-
}
|
|
9328
|
-
res.end();
|
|
9329
|
-
return;
|
|
9330
|
-
}
|
|
9331
|
-
res.writeHead(200, sseHeaders);
|
|
9332
|
-
const eventStream = this.toAsyncIterable(result);
|
|
9333
|
-
for await (const chunk of this.aguiHandler.formatStream(
|
|
9334
|
-
eventStream,
|
|
9335
|
-
context
|
|
9336
|
-
)) {
|
|
9337
|
-
res.write(chunk);
|
|
9338
|
-
}
|
|
9339
|
-
res.end();
|
|
9340
|
-
} catch (error) {
|
|
9341
|
-
res.writeHead(200, sseHeaders);
|
|
9342
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
9343
|
-
for await (const chunk of this.aguiHandler.errorStream(
|
|
9344
|
-
`Internal error: ${errorMessage}`
|
|
9345
|
-
)) {
|
|
9346
|
-
res.write(chunk);
|
|
9347
|
-
}
|
|
9348
|
-
res.end();
|
|
9349
|
-
}
|
|
9350
|
-
}
|
|
9351
|
-
/**
|
|
9352
|
-
* Handle OpenAI chat completions endpoint
|
|
9353
|
-
*/
|
|
9354
|
-
async handleChatCompletions(req, res) {
|
|
9355
|
-
const body = await this.parseBody(req);
|
|
9356
|
-
const messages = (body.messages || []).map((m) => ({
|
|
9357
|
-
role: m.role,
|
|
9358
|
-
content: m.content,
|
|
9359
|
-
name: m.name,
|
|
9360
|
-
toolCallId: m.tool_call_id,
|
|
9361
|
-
toolCalls: m.tool_calls?.map((tc) => ({
|
|
9362
|
-
id: tc.id,
|
|
9363
|
-
type: tc.type || "function",
|
|
9364
|
-
function: tc.function
|
|
9365
|
-
}))
|
|
9366
|
-
}));
|
|
9367
|
-
const agentRequest = {
|
|
9368
|
-
protocol: "openai",
|
|
9369
|
-
messages,
|
|
9370
|
-
stream: body.stream ?? false,
|
|
9371
|
-
model: body.model,
|
|
9372
|
-
metadata: body.metadata,
|
|
9373
|
-
rawRequest: req
|
|
9374
|
-
};
|
|
9375
|
-
let result;
|
|
9376
|
-
try {
|
|
9377
|
-
result = await Promise.resolve(this.invokeAgent(agentRequest));
|
|
9378
|
-
} catch (error) {
|
|
9379
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
9380
|
-
res.writeHead(500, { "Content-Type": "application/json" });
|
|
9381
|
-
res.end(
|
|
9382
|
-
JSON.stringify({
|
|
9383
|
-
error: { message: errorMessage, type: "server_error" }
|
|
9384
|
-
})
|
|
9385
|
-
);
|
|
9386
|
-
return;
|
|
9387
|
-
}
|
|
9388
|
-
if (agentRequest.stream && this.isAsyncIterable(result)) {
|
|
9389
|
-
res.writeHead(200, {
|
|
9390
|
-
"Content-Type": "text/event-stream",
|
|
9391
|
-
"Cache-Control": "no-cache",
|
|
9392
|
-
Connection: "keep-alive"
|
|
9393
|
-
});
|
|
9394
|
-
const id = `chatcmpl-${Date.now()}`;
|
|
9395
|
-
const created = Math.floor(Date.now() / 1e3);
|
|
9396
|
-
for await (const chunk of result) {
|
|
9397
|
-
let content = "";
|
|
9398
|
-
let toolCalls;
|
|
9399
|
-
if (typeof chunk === "string") {
|
|
9400
|
-
content = chunk;
|
|
9401
|
-
} else {
|
|
9402
|
-
const event = chunk;
|
|
9403
|
-
if (event.event === "TEXT" /* TEXT */) {
|
|
9404
|
-
content = event.data?.delta || "";
|
|
9405
|
-
} else if (event.event === "TOOL_CALL" /* TOOL_CALL */) {
|
|
9406
|
-
toolCalls = [
|
|
9407
|
-
{
|
|
9408
|
-
id: event.data?.id || "",
|
|
9409
|
-
type: "function",
|
|
9410
|
-
function: {
|
|
9411
|
-
name: event.data?.name || "",
|
|
9412
|
-
arguments: event.data?.args || ""
|
|
9413
|
-
}
|
|
9414
|
-
}
|
|
9415
|
-
];
|
|
9416
|
-
} else if (event.event === "ERROR" /* ERROR */) {
|
|
9417
|
-
const errorData = {
|
|
9418
|
-
id,
|
|
9419
|
-
object: "chat.completion.chunk",
|
|
9420
|
-
created,
|
|
9421
|
-
model: agentRequest.model || "agent",
|
|
9422
|
-
choices: [
|
|
9423
|
-
{
|
|
9424
|
-
index: 0,
|
|
9425
|
-
delta: {},
|
|
9426
|
-
finish_reason: "error"
|
|
9427
|
-
}
|
|
9428
|
-
],
|
|
9429
|
-
error: {
|
|
9430
|
-
message: event.data?.message || "Unknown error",
|
|
9431
|
-
code: event.data?.code
|
|
9432
|
-
}
|
|
9433
|
-
};
|
|
9434
|
-
res.write(`data: ${JSON.stringify(errorData)}
|
|
9435
|
-
|
|
9436
|
-
`);
|
|
9437
|
-
continue;
|
|
9438
|
-
} else {
|
|
9439
|
-
content = event.data?.content || "";
|
|
9440
|
-
}
|
|
9441
|
-
}
|
|
9442
|
-
const data = {
|
|
9443
|
-
id,
|
|
9444
|
-
object: "chat.completion.chunk",
|
|
9445
|
-
created,
|
|
9446
|
-
model: agentRequest.model || "agent",
|
|
9447
|
-
choices: [
|
|
9448
|
-
{
|
|
9449
|
-
index: 0,
|
|
9450
|
-
delta: content ? { content } : toolCalls ? {
|
|
9451
|
-
tool_calls: toolCalls.map((tc, idx) => ({
|
|
9452
|
-
index: idx,
|
|
9453
|
-
id: tc.id,
|
|
9454
|
-
type: tc.type,
|
|
9455
|
-
function: tc.function
|
|
9456
|
-
}))
|
|
9457
|
-
} : {},
|
|
9458
|
-
finish_reason: null
|
|
9459
|
-
}
|
|
9460
|
-
]
|
|
9461
|
-
};
|
|
9462
|
-
res.write(`data: ${JSON.stringify(data)}
|
|
9463
|
-
|
|
9464
|
-
`);
|
|
9465
|
-
}
|
|
9466
|
-
res.write("data: [DONE]\n\n");
|
|
9467
|
-
res.end();
|
|
9468
|
-
} else {
|
|
9469
|
-
let content = "";
|
|
9470
|
-
let toolCalls;
|
|
9471
|
-
if (typeof result === "string") {
|
|
9472
|
-
content = result;
|
|
9473
|
-
} else if (this.isAsyncIterable(result)) {
|
|
9474
|
-
for await (const chunk of result) {
|
|
9475
|
-
if (typeof chunk === "string") {
|
|
9476
|
-
content += chunk;
|
|
9477
|
-
} else {
|
|
9478
|
-
const event = chunk;
|
|
9479
|
-
if (event.event === "TEXT" /* TEXT */) {
|
|
9480
|
-
content += event.data?.delta || "";
|
|
9481
|
-
} else if (event.event === "TOOL_CALL" /* TOOL_CALL */) {
|
|
9482
|
-
if (!toolCalls) toolCalls = [];
|
|
9483
|
-
toolCalls.push({
|
|
9484
|
-
id: event.data?.id || "",
|
|
9485
|
-
type: "function",
|
|
9486
|
-
function: {
|
|
9487
|
-
name: event.data?.name || "",
|
|
9488
|
-
arguments: event.data?.args || ""
|
|
9489
|
-
}
|
|
9490
|
-
});
|
|
9491
|
-
}
|
|
9492
|
-
}
|
|
9493
|
-
}
|
|
9494
|
-
} else {
|
|
9495
|
-
const event = result;
|
|
9496
|
-
if (event.event === "TEXT" /* TEXT */) {
|
|
9497
|
-
content = event.data?.delta || event.data?.content || "";
|
|
9498
|
-
} else if (event.event) {
|
|
9499
|
-
content = event.data?.content || "";
|
|
9500
|
-
} else {
|
|
9501
|
-
content = result.content || "";
|
|
9502
|
-
}
|
|
9503
|
-
}
|
|
9504
|
-
const response = {
|
|
9505
|
-
id: `chatcmpl-${Date.now()}`,
|
|
9506
|
-
object: "chat.completion",
|
|
9507
|
-
created: Math.floor(Date.now() / 1e3),
|
|
9508
|
-
model: agentRequest.model || "agent",
|
|
9509
|
-
choices: [
|
|
9510
|
-
{
|
|
9511
|
-
index: 0,
|
|
9512
|
-
message: {
|
|
9513
|
-
role: "assistant",
|
|
9514
|
-
content,
|
|
9515
|
-
tool_calls: toolCalls?.map((tc, idx) => ({
|
|
9516
|
-
index: idx,
|
|
9517
|
-
id: tc.id,
|
|
9518
|
-
type: tc.type,
|
|
9519
|
-
function: tc.function
|
|
9520
|
-
}))
|
|
9521
|
-
},
|
|
9522
|
-
finish_reason: "stop"
|
|
9523
|
-
}
|
|
9524
|
-
],
|
|
9525
|
-
usage: {
|
|
9526
|
-
prompt_tokens: 0,
|
|
9527
|
-
completion_tokens: 0,
|
|
9528
|
-
total_tokens: 0
|
|
9529
|
-
}
|
|
9530
|
-
};
|
|
9531
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
9532
|
-
res.end(JSON.stringify(response));
|
|
9533
|
-
}
|
|
9534
|
-
}
|
|
9535
|
-
/**
|
|
9536
|
-
* Parse request body
|
|
9537
|
-
*/
|
|
9538
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9539
|
-
parseBody(req) {
|
|
9540
|
-
return new Promise((resolve, reject) => {
|
|
9541
|
-
let body = "";
|
|
9542
|
-
req.on("data", (chunk) => body += chunk);
|
|
9543
|
-
req.on("end", () => {
|
|
9544
|
-
try {
|
|
9545
|
-
resolve(JSON.parse(body || "{}"));
|
|
9546
|
-
} catch (e) {
|
|
9547
|
-
reject(new Error("Invalid JSON"));
|
|
9548
|
-
}
|
|
9549
|
-
});
|
|
9550
|
-
req.on("error", reject);
|
|
9551
|
-
});
|
|
9552
|
-
}
|
|
9553
|
-
/**
|
|
9554
|
-
* Check if value is async iterable
|
|
9555
|
-
*/
|
|
9556
|
-
isAsyncIterable(value) {
|
|
9557
|
-
return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
|
|
9558
|
-
}
|
|
9559
|
-
/**
|
|
9560
|
-
* Convert result to async iterable
|
|
9561
|
-
*/
|
|
9562
|
-
async *toAsyncIterable(result) {
|
|
9563
|
-
if (typeof result === "string") {
|
|
9564
|
-
yield result;
|
|
9565
|
-
} else if (this.isAsyncIterable(result)) {
|
|
9566
|
-
for await (const chunk of result) {
|
|
9567
|
-
yield chunk;
|
|
9568
|
-
}
|
|
9569
|
-
} else {
|
|
9570
|
-
yield result;
|
|
9571
|
-
}
|
|
9572
|
-
}
|
|
9573
|
-
};
|
|
9574
|
-
|
|
9575
|
-
// src/integration/adapter.ts
|
|
9576
|
-
function schemaToType(schema) {
|
|
9577
|
-
if (!schema || typeof schema !== "object") {
|
|
9578
|
-
return "unknown";
|
|
9579
|
-
}
|
|
9580
|
-
const schemaType = schema["type"];
|
|
9581
|
-
switch (schemaType) {
|
|
9582
|
-
case "string":
|
|
9583
|
-
return "string";
|
|
9584
|
-
case "number":
|
|
9585
|
-
case "integer":
|
|
9586
|
-
return "number";
|
|
9587
|
-
case "boolean":
|
|
9588
|
-
return "boolean";
|
|
9589
|
-
case "array": {
|
|
9590
|
-
const items = schema["items"];
|
|
9591
|
-
if (items) {
|
|
9592
|
-
return `${schemaToType(items)}[]`;
|
|
9593
|
-
}
|
|
9594
|
-
return "unknown[]";
|
|
9595
|
-
}
|
|
9596
|
-
case "object":
|
|
9597
|
-
return "Record<string, unknown>";
|
|
9598
|
-
case "null":
|
|
9599
|
-
return "null";
|
|
9600
|
-
default:
|
|
9601
|
-
return "unknown";
|
|
9602
|
-
}
|
|
9603
|
-
}
|
|
9604
|
-
|
|
9605
|
-
// src/integration/mastra/adapter.ts
|
|
9606
|
-
var MastraMessageAdapter = class {
|
|
9607
|
-
/**
|
|
9608
|
-
* 将 Mastra 消息转换为标准格式
|
|
9609
|
-
*/
|
|
9610
|
-
toCanonical(messages) {
|
|
9611
|
-
return messages.map((msg) => ({
|
|
9612
|
-
role: msg.role,
|
|
9613
|
-
content: msg.content,
|
|
9614
|
-
name: msg.name,
|
|
9615
|
-
toolCallId: msg.tool_call_id,
|
|
9616
|
-
toolCalls: msg.tool_calls?.map((tc) => ({
|
|
9617
|
-
id: tc.id,
|
|
9618
|
-
type: tc.type,
|
|
9619
|
-
function: tc.function
|
|
9620
|
-
}))
|
|
9621
|
-
}));
|
|
9622
|
-
}
|
|
9623
|
-
/**
|
|
9624
|
-
* 将标准消息转换为 Mastra 格式
|
|
9625
|
-
*/
|
|
9626
|
-
fromCanonical(messages) {
|
|
9627
|
-
return messages.map((msg) => ({
|
|
9628
|
-
role: msg.role,
|
|
9629
|
-
content: msg.content,
|
|
9630
|
-
name: msg.name,
|
|
9631
|
-
tool_call_id: msg.toolCallId,
|
|
9632
|
-
tool_calls: msg.toolCalls?.map(
|
|
9633
|
-
(tc) => ({
|
|
9634
|
-
id: tc.id,
|
|
9635
|
-
type: tc.type,
|
|
9636
|
-
function: tc.function
|
|
9637
|
-
})
|
|
9638
|
-
)
|
|
9639
|
-
}));
|
|
9640
|
-
}
|
|
9641
|
-
};
|
|
9642
|
-
var MastraToolAdapter = class {
|
|
9643
|
-
/**
|
|
9644
|
-
* 将标准工具转换为 Mastra 格式
|
|
9645
|
-
*/
|
|
9646
|
-
fromCanonical(tools) {
|
|
9647
|
-
return tools.map((tool) => ({
|
|
9648
|
-
name: tool.name,
|
|
9649
|
-
description: tool.description,
|
|
9650
|
-
inputSchema: tool.parameters,
|
|
9651
|
-
execute: tool.func
|
|
9652
|
-
}));
|
|
9653
|
-
}
|
|
9654
|
-
/**
|
|
9655
|
-
* 将 Mastra 工具转换为标准格式
|
|
9656
|
-
*/
|
|
9657
|
-
toCanonical(tools) {
|
|
9658
|
-
return tools.map((tool) => ({
|
|
9659
|
-
name: tool.name,
|
|
9660
|
-
description: tool.description,
|
|
9661
|
-
parameters: tool.inputSchema,
|
|
9662
|
-
func: tool.execute
|
|
9663
|
-
}));
|
|
9664
|
-
}
|
|
9665
|
-
};
|
|
9666
|
-
var MastraModelAdapter = class {
|
|
9667
|
-
/**
|
|
9668
|
-
* 使用 AgentRun 配置创建 Mastra 模型配置
|
|
9669
|
-
*/
|
|
9670
|
-
createModel(config2) {
|
|
9671
|
-
const provider = this.detectProvider(config2.endpoint);
|
|
9672
|
-
return {
|
|
9673
|
-
provider,
|
|
9674
|
-
modelId: config2.modelName || "gpt-4",
|
|
9675
|
-
apiKey: config2.apiKey,
|
|
9676
|
-
baseUrl: config2.endpoint,
|
|
9677
|
-
temperature: config2.temperature,
|
|
9678
|
-
maxTokens: config2.maxTokens
|
|
9679
|
-
};
|
|
9680
|
-
}
|
|
9681
|
-
/**
|
|
9682
|
-
* 检测模型提供商
|
|
9683
|
-
*/
|
|
9684
|
-
detectProvider(endpoint) {
|
|
9685
|
-
if (!endpoint) {
|
|
9686
|
-
return "openai";
|
|
9687
|
-
}
|
|
9688
|
-
const lowerEndpoint = endpoint.toLowerCase();
|
|
9689
|
-
if (lowerEndpoint.includes("openai")) {
|
|
9690
|
-
return "openai";
|
|
9691
|
-
}
|
|
9692
|
-
if (lowerEndpoint.includes("anthropic")) {
|
|
9693
|
-
return "anthropic";
|
|
9694
|
-
}
|
|
9695
|
-
if (lowerEndpoint.includes("dashscope")) {
|
|
9696
|
-
return "dashscope";
|
|
9697
|
-
}
|
|
9698
|
-
if (lowerEndpoint.includes("google") || lowerEndpoint.includes("gemini")) {
|
|
9699
|
-
return "google";
|
|
9700
|
-
}
|
|
9701
|
-
return "openai-compatible";
|
|
9702
|
-
}
|
|
9703
|
-
};
|
|
9704
|
-
var MastraAdapter = class {
|
|
9705
|
-
name = "mastra";
|
|
9706
|
-
message = new MastraMessageAdapter();
|
|
9707
|
-
tool = new MastraToolAdapter();
|
|
9708
|
-
model = new MastraModelAdapter();
|
|
9709
|
-
};
|
|
9710
|
-
function createMastraAdapter() {
|
|
9711
|
-
return new MastraAdapter();
|
|
9712
|
-
}
|
|
9713
|
-
function wrapTools(tools) {
|
|
9714
|
-
const adapter = new MastraToolAdapter();
|
|
9715
|
-
return adapter.fromCanonical(tools);
|
|
9716
|
-
}
|
|
9717
|
-
function wrapModel(config2) {
|
|
9718
|
-
const adapter = new MastraModelAdapter();
|
|
9719
|
-
return adapter.createModel(config2);
|
|
9720
|
-
}
|
|
9721
|
-
function wrapAgent(params) {
|
|
9722
|
-
const { name, instructions, modelConfig, tools } = params;
|
|
9723
|
-
const modelAdapter = new MastraModelAdapter();
|
|
9724
|
-
const mastraModel = modelAdapter.createModel(modelConfig);
|
|
9725
|
-
let mastraTools;
|
|
9726
|
-
if (tools && tools.length > 0) {
|
|
9727
|
-
const toolAdapter = new MastraToolAdapter();
|
|
9728
|
-
mastraTools = toolAdapter.fromCanonical(tools);
|
|
9729
|
-
}
|
|
9730
|
-
return {
|
|
9731
|
-
name,
|
|
9732
|
-
instructions,
|
|
9733
|
-
model: mastraModel,
|
|
9734
|
-
tools: mastraTools
|
|
9735
|
-
};
|
|
9736
|
-
}
|
|
9737
|
-
|
|
9738
8659
|
// src/index.ts
|
|
9739
8660
|
var VERSION = "0.0.1";
|
|
9740
8661
|
|
|
9741
|
-
exports.AgentRunServer = AgentRunServer;
|
|
9742
8662
|
exports.AgentRuntime = AgentRuntime;
|
|
9743
8663
|
exports.AgentRuntimeArtifact = AgentRuntimeArtifact;
|
|
9744
8664
|
exports.AgentRuntimeClient = AgentRuntimeClient;
|
|
@@ -9746,21 +8666,10 @@ exports.AgentRuntimeControlAPI = AgentRuntimeControlAPI;
|
|
|
9746
8666
|
exports.AgentRuntimeEndpoint = AgentRuntimeEndpoint;
|
|
9747
8667
|
exports.AgentRuntimeLanguage = AgentRuntimeLanguage;
|
|
9748
8668
|
exports.AgentRuntimeProtocolType = AgentRuntimeProtocolType;
|
|
9749
|
-
exports.EventType = EventType;
|
|
9750
|
-
exports.MastraAdapter = MastraAdapter;
|
|
9751
|
-
exports.MastraMessageAdapter = MastraMessageAdapter;
|
|
9752
|
-
exports.MastraModelAdapter = MastraModelAdapter;
|
|
9753
|
-
exports.MastraToolAdapter = MastraToolAdapter;
|
|
9754
|
-
exports.MessageRole = MessageRole;
|
|
9755
8669
|
exports.SandboxClient = SandboxClient;
|
|
9756
8670
|
exports.Template = Template;
|
|
9757
8671
|
exports.ToolSet = ToolSet;
|
|
9758
8672
|
exports.ToolSetClient = ToolSetClient;
|
|
9759
8673
|
exports.VERSION = VERSION;
|
|
9760
|
-
exports.createMastraAdapter = createMastraAdapter;
|
|
9761
|
-
exports.schemaToType = schemaToType;
|
|
9762
|
-
exports.wrapAgent = wrapAgent;
|
|
9763
|
-
exports.wrapModel = wrapModel;
|
|
9764
|
-
exports.wrapTools = wrapTools;
|
|
9765
8674
|
//# sourceMappingURL=index.cjs.map
|
|
9766
8675
|
//# sourceMappingURL=index.cjs.map
|