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