@celljs/ai-agent 0.0.0-stage → 4.1.0
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 +68 -2
- package/cell.yml +5 -0
- package/esm/node/agent/agent-factory.d.ts +10 -0
- package/esm/node/agent/agent-factory.js +76 -0
- package/esm/node/agent/agent-factory.js.map +1 -0
- package/esm/node/agent/index.d.ts +1 -0
- package/esm/node/agent/index.js +2 -0
- package/esm/node/agent/index.js.map +1 -0
- package/esm/node/agent-protocol.d.ts +32 -0
- package/esm/node/agent-protocol.js +5 -0
- package/esm/node/agent-protocol.js.map +1 -0
- package/esm/node/error/errors.d.ts +11 -0
- package/esm/node/error/errors.js +16 -0
- package/esm/node/error/errors.js.map +1 -0
- package/esm/node/error/index.d.ts +1 -0
- package/esm/node/error/index.js +2 -0
- package/esm/node/error/index.js.map +1 -0
- package/esm/node/event/agent-event-util.d.ts +37 -0
- package/esm/node/event/agent-event-util.js +60 -0
- package/esm/node/event/agent-event-util.js.map +1 -0
- package/esm/node/event/index.d.ts +1 -0
- package/esm/node/event/index.js +2 -0
- package/esm/node/event/index.js.map +1 -0
- package/esm/node/index.d.ts +5 -0
- package/esm/node/index.js +6 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/pi/index.d.ts +2 -0
- package/esm/node/pi/index.js +3 -0
- package/esm/node/pi/index.js.map +1 -0
- package/esm/node/pi/pi-agent-core-util.d.ts +6 -0
- package/esm/node/pi/pi-agent-core-util.js +17 -0
- package/esm/node/pi/pi-agent-core-util.js.map +1 -0
- package/esm/node/pi/pi-types.d.ts +6 -0
- package/esm/node/pi/pi-types.js +2 -0
- package/esm/node/pi/pi-types.js.map +1 -0
- package/esm/node/test/index.d.ts +2 -0
- package/esm/node/test/index.js +3 -0
- package/esm/node/test/index.js.map +1 -0
- package/esm/node/test/test-container.d.ts +7 -0
- package/esm/node/test/test-container.js +11 -0
- package/esm/node/test/test-container.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/agent/agent-factory.d.ts +11 -0
- package/lib/node/agent/agent-factory.d.ts.map +1 -0
- package/lib/node/agent/agent-factory.js +79 -0
- package/lib/node/agent/agent-factory.js.map +1 -0
- package/lib/node/agent/agent-factory.spec.d.ts +47 -0
- package/lib/node/agent/agent-factory.spec.d.ts.map +1 -0
- package/lib/node/agent/agent-factory.spec.js +109 -0
- package/lib/node/agent/agent-factory.spec.js.map +1 -0
- package/lib/node/agent/index.d.ts +2 -0
- package/lib/node/agent/index.d.ts.map +1 -0
- package/lib/node/agent/index.js +18 -0
- package/lib/node/agent/index.js.map +1 -0
- package/lib/node/agent-protocol.d.ts +33 -0
- package/lib/node/agent-protocol.d.ts.map +1 -0
- package/lib/node/agent-protocol.js +8 -0
- package/lib/node/agent-protocol.js.map +1 -0
- package/lib/node/error/errors.d.ts +12 -0
- package/lib/node/error/errors.d.ts.map +1 -0
- package/lib/node/error/errors.js +22 -0
- package/lib/node/error/errors.js.map +1 -0
- package/lib/node/error/index.d.ts +2 -0
- package/lib/node/error/index.d.ts.map +1 -0
- package/lib/node/error/index.js +18 -0
- package/lib/node/error/index.js.map +1 -0
- package/lib/node/event/agent-event-util.d.ts +38 -0
- package/lib/node/event/agent-event-util.d.ts.map +1 -0
- package/lib/node/event/agent-event-util.js +63 -0
- package/lib/node/event/agent-event-util.js.map +1 -0
- package/lib/node/event/agent-event-util.spec.d.ts +2 -0
- package/lib/node/event/agent-event-util.spec.d.ts.map +1 -0
- package/lib/node/event/agent-event-util.spec.js +81 -0
- package/lib/node/event/agent-event-util.spec.js.map +1 -0
- package/lib/node/event/index.d.ts +2 -0
- package/lib/node/event/index.d.ts.map +1 -0
- package/lib/node/event/index.js +18 -0
- package/lib/node/event/index.js.map +1 -0
- package/lib/node/index.d.ts +6 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +22 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/pi/index.d.ts +3 -0
- package/lib/node/pi/index.d.ts.map +1 -0
- package/lib/node/pi/index.js +19 -0
- package/lib/node/pi/index.js.map +1 -0
- package/lib/node/pi/pi-agent-core-util.d.ts +7 -0
- package/lib/node/pi/pi-agent-core-util.d.ts.map +1 -0
- package/lib/node/pi/pi-agent-core-util.js +20 -0
- package/lib/node/pi/pi-agent-core-util.js.map +1 -0
- package/lib/node/pi/pi-types.d.ts +7 -0
- package/lib/node/pi/pi-types.d.ts.map +1 -0
- package/lib/node/pi/pi-types.js +3 -0
- package/lib/node/pi/pi-types.js.map +1 -0
- package/lib/node/test/index.d.ts +3 -0
- package/lib/node/test/index.d.ts.map +1 -0
- package/lib/node/test/index.js +21 -0
- package/lib/node/test/index.js.map +1 -0
- package/lib/node/test/test-container.d.ts +8 -0
- package/lib/node/test/test-container.d.ts.map +1 -0
- package/lib/node/test/test-container.js +14 -0
- package/lib/node/test/test-container.js.map +1 -0
- package/package.json +93 -4
- package/src/node/agent/agent-factory.spec.ts +97 -0
- package/src/node/agent/agent-factory.ts +60 -0
- package/src/node/agent/index.ts +1 -0
- package/src/node/agent-protocol.ts +39 -0
- package/src/node/error/errors.ts +20 -0
- package/src/node/error/index.ts +1 -0
- package/src/node/event/agent-event-util.spec.ts +87 -0
- package/src/node/event/agent-event-util.ts +79 -0
- package/src/node/event/index.ts +1 -0
- package/src/node/index.ts +5 -0
- package/src/node/module.ts +4 -0
- package/src/node/pi/index.ts +2 -0
- package/src/node/pi/pi-agent-core-util.ts +19 -0
- package/src/node/pi/pi-types.ts +8 -0
- package/src/node/test/index.ts +2 -0
- package/src/node/test/test-container.ts +15 -0
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - AI Agent Component
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
基于 [pi-agent-core](https://www.npmjs.com/package/@earendil-works/pi-agent-core) 的通用 agent:agent 循环、并行工具调用、事件流,不带编码工具和文件系统权限,适合在业务里做助手。模型来自 [@celljs/ai-pi](../ai-pi)。需要读写代码、执行命令的场景用 [@celljs/ai-coding-agent](../ai-coding-agent)。
|
|
4
|
+
|
|
5
|
+
- 工具写成 `AgentTool` 组件,可以直接用注入的服务;同样的组件也是编码会话的工具;
|
|
6
|
+
- `AgentFactory` 按 `cell.ai.agent` 创建 pi-agent-core 的 `Agent`;
|
|
7
|
+
- `AgentEventUtil.stream()` 把 agent(或编码会话)的事件写成 Server-Sent Events。
|
|
8
|
+
|
|
9
|
+
pi-agent-core 用动态 `import()` 加载,需要 Node.js >= 22.19 或 Bun。
|
|
10
|
+
|
|
11
|
+
## 配置
|
|
12
|
+
|
|
13
|
+
```yaml
|
|
14
|
+
cell:
|
|
15
|
+
ai:
|
|
16
|
+
agent:
|
|
17
|
+
model: anthropic/claude-sonnet-4-5 # pi-ai 的 <provider>/<model id>;默认第一个有凭据的模型
|
|
18
|
+
systemPrompt: You are the support assistant of ACME.
|
|
19
|
+
thinkingLevel: low
|
|
20
|
+
tools: [find_order, refund] # AgentTool 组件名;默认全部
|
|
21
|
+
toolExecution: parallel # 或 sequential
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
API key 在 `cell.ai.pi.apiKeys` 或服务商的环境变量里配置。
|
|
25
|
+
|
|
26
|
+
## 使用
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { AgentEventUtil, AgentFactory, AgentTool } from '@celljs/ai-agent';
|
|
30
|
+
import { Autowired, Component } from '@celljs/core';
|
|
31
|
+
import { Body, Controller, Post, Res } from '@celljs/mvc/lib/node';
|
|
32
|
+
import { OrderService } from './order-service';
|
|
33
|
+
|
|
34
|
+
@Component(AgentTool)
|
|
35
|
+
export class FindOrderTool implements AgentTool {
|
|
36
|
+
readonly name = 'find_order';
|
|
37
|
+
readonly label = 'Find order';
|
|
38
|
+
readonly description = 'Finds an order by its number.';
|
|
39
|
+
readonly parameters = { type: 'object', properties: { number: { type: 'string' } }, required: [ 'number' ] };
|
|
40
|
+
|
|
41
|
+
@Autowired(OrderService)
|
|
42
|
+
protected readonly orderService: OrderService;
|
|
43
|
+
|
|
44
|
+
async execute(_toolCallId: string, params: any) {
|
|
45
|
+
const order = await this.orderService.find(params.number);
|
|
46
|
+
return { content: [ { type: 'text' as const, text: JSON.stringify(order) } ], details: {} };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Controller('assistant')
|
|
51
|
+
export class AssistantController {
|
|
52
|
+
@Autowired(AgentFactory)
|
|
53
|
+
protected readonly agentFactory: AgentFactory;
|
|
54
|
+
|
|
55
|
+
@Post()
|
|
56
|
+
async ask(@Body('text') text: string, @Res() response: any): Promise<void> {
|
|
57
|
+
const agent = await this.agentFactory.create();
|
|
58
|
+
await AgentEventUtil.stream(agent, response, () => agent.prompt(text));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`create()` 的参数(`model`、`systemPrompt`、`thinkingLevel`、`tools`)覆盖配置;传 `messages`(之前保存的 `agent.state.messages`)可以继续一段对话。工具名不存在时报 `AgentToolNotFoundError`,没有可用模型时报 `AgentModelMissingError`。拿到的 `Agent` 是 pi-agent-core 原生对象:`prompt`、`steer`、`followUp`、`abort`、`subscribe`、`state` 见其文档。
|
|
64
|
+
|
|
65
|
+
SSE 每帧是 `event: <type>\ndata: <JSON>`,`message_update` 只带增量和用量,与 pi coding agent 的 JSON 模式一致;失败时发 `{ "type": "error", "message": "..." }`。
|
|
66
|
+
|
|
67
|
+
## 测试
|
|
68
|
+
|
|
69
|
+
`@celljs/ai-agent/lib/node/test` 的 `createAgentTestContainer(options, ...modules)` 用 `FauxPiModels` 作默认模型:`faux.reply('文本' | { tool, args } | { error })` 编排回复,`faux.contexts` 记录每次请求。
|
package/cell.yml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PiModelsProvider } from '@celljs/ai-pi';
|
|
2
|
+
import { type AgentCreateOptions, AgentFactory, type AgentOptions, AgentTool } from '../agent-protocol.js';
|
|
3
|
+
import { type Agent } from '../pi/index.js';
|
|
4
|
+
export declare class DefaultAgentFactory implements AgentFactory {
|
|
5
|
+
protected readonly options?: AgentOptions;
|
|
6
|
+
protected readonly modelsProvider: PiModelsProvider;
|
|
7
|
+
protected readonly tools: AgentTool[];
|
|
8
|
+
create(createOptions?: AgentCreateOptions): Promise<Agent>;
|
|
9
|
+
protected selectTools(names?: string[]): AgentTool[];
|
|
10
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { PiModelsProvider } from '@celljs/ai-pi';
|
|
11
|
+
import { Autowired, Component, Optional, Value } from '@celljs/core';
|
|
12
|
+
import { AGENT_OPTIONS, AgentFactory, AgentTool } from '../agent-protocol.js';
|
|
13
|
+
import { AgentModelMissingError, AgentToolNotFoundError } from '../error/index.js';
|
|
14
|
+
import { PiAgentCoreUtil } from '../pi/index.js';
|
|
15
|
+
let DefaultAgentFactory = class DefaultAgentFactory {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.tools = [];
|
|
18
|
+
}
|
|
19
|
+
async create(createOptions = {}) {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
const options = { ...this.options };
|
|
22
|
+
for (const [key, value] of Object.entries(createOptions)) {
|
|
23
|
+
if (value !== undefined && key !== 'messages') {
|
|
24
|
+
options[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const [core, models] = await Promise.all([PiAgentCoreUtil.load(), this.modelsProvider.provide()]);
|
|
28
|
+
const model = options.model ? await this.modelsProvider.resolve(options.model) : (await models.getAvailable())[0];
|
|
29
|
+
if (!model) {
|
|
30
|
+
throw new AgentModelMissingError();
|
|
31
|
+
}
|
|
32
|
+
return new core.Agent({
|
|
33
|
+
initialState: {
|
|
34
|
+
systemPrompt: (_a = options.systemPrompt) !== null && _a !== void 0 ? _a : '',
|
|
35
|
+
model,
|
|
36
|
+
thinkingLevel: (_b = options.thinkingLevel) !== null && _b !== void 0 ? _b : 'off',
|
|
37
|
+
tools: this.selectTools(options.tools),
|
|
38
|
+
messages: (_c = createOptions.messages) !== null && _c !== void 0 ? _c : [],
|
|
39
|
+
},
|
|
40
|
+
streamFn: models.streamSimple.bind(models),
|
|
41
|
+
toolExecution: options.toolExecution,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
selectTools(names) {
|
|
45
|
+
var _a;
|
|
46
|
+
const tools = (_a = this.tools) !== null && _a !== void 0 ? _a : [];
|
|
47
|
+
if (!names) {
|
|
48
|
+
return tools;
|
|
49
|
+
}
|
|
50
|
+
return names.map(name => {
|
|
51
|
+
const tool = tools.find(t => t.name === name);
|
|
52
|
+
if (!tool) {
|
|
53
|
+
throw new AgentToolNotFoundError(name, tools.map(t => t.name));
|
|
54
|
+
}
|
|
55
|
+
return tool;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__decorate([
|
|
60
|
+
Value(AGENT_OPTIONS),
|
|
61
|
+
__metadata("design:type", Object)
|
|
62
|
+
], DefaultAgentFactory.prototype, "options", void 0);
|
|
63
|
+
__decorate([
|
|
64
|
+
Autowired(PiModelsProvider),
|
|
65
|
+
__metadata("design:type", Object)
|
|
66
|
+
], DefaultAgentFactory.prototype, "modelsProvider", void 0);
|
|
67
|
+
__decorate([
|
|
68
|
+
Autowired(AgentTool),
|
|
69
|
+
Optional(),
|
|
70
|
+
__metadata("design:type", Array)
|
|
71
|
+
], DefaultAgentFactory.prototype, "tools", void 0);
|
|
72
|
+
DefaultAgentFactory = __decorate([
|
|
73
|
+
Component(AgentFactory)
|
|
74
|
+
], DefaultAgentFactory);
|
|
75
|
+
export { DefaultAgentFactory };
|
|
76
|
+
//# sourceMappingURL=agent-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-factory.js","sourceRoot":"","sources":["../../../src/node/agent/agent-factory.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './agent-factory.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/agent/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Agent, AgentMessage, ThinkingLevel } from './pi/index.js';
|
|
2
|
+
export declare const AgentTool: unique symbol;
|
|
3
|
+
export declare const AgentFactory: unique symbol;
|
|
4
|
+
/** Configuration key of {@link AgentOptions}. */
|
|
5
|
+
export declare const AGENT_OPTIONS = "cell.ai.agent";
|
|
6
|
+
/**
|
|
7
|
+
* A tool as a Cell component: pi-agent-core's `AgentTool` (`name`, `label`, `description`, JSON Schema
|
|
8
|
+
* `parameters`, `execute`). `execute` throws on failure. The same components are tools of coding agent
|
|
9
|
+
* sessions (@celljs/ai-coding-agent).
|
|
10
|
+
*/
|
|
11
|
+
export type AgentTool<TParameters extends object = object, TDetails = any> = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import'
|
|
12
|
+
} }).AgentTool<TParameters, TDetails>;
|
|
13
|
+
/** `cell.ai.agent`. */
|
|
14
|
+
export interface AgentOptions {
|
|
15
|
+
/** `<provider>/<model id>` of pi-ai (@celljs/ai-pi). Default: the first model with credentials. */
|
|
16
|
+
model?: string;
|
|
17
|
+
systemPrompt?: string;
|
|
18
|
+
thinkingLevel?: ThinkingLevel;
|
|
19
|
+
/** Names of the AgentTool components the agent gets. Default: all of them. */
|
|
20
|
+
tools?: string[];
|
|
21
|
+
/** How the tool calls of one model response run. Default: parallel. */
|
|
22
|
+
toolExecution?: 'parallel' | 'sequential';
|
|
23
|
+
}
|
|
24
|
+
/** Options of one agent; they override `cell.ai.agent`. */
|
|
25
|
+
export interface AgentCreateOptions extends Omit<AgentOptions, 'toolExecution'> {
|
|
26
|
+
/** Conversation to continue, e.g. `agent.state.messages` saved earlier. */
|
|
27
|
+
messages?: AgentMessage[];
|
|
28
|
+
}
|
|
29
|
+
/** Creates pi-agent-core `Agent`s with the models of @celljs/ai-pi and the {@link AgentTool} components. */
|
|
30
|
+
export interface AgentFactory {
|
|
31
|
+
create(options?: AgentCreateOptions): Promise<Agent>;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-protocol.js","sourceRoot":"","sources":["../../src/node/agent-protocol.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;AAC7C,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AAEnD,iDAAiD;AACjD,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CustomError } from '@celljs/core';
|
|
2
|
+
export declare class AgentError extends CustomError {
|
|
3
|
+
}
|
|
4
|
+
export declare class AgentToolNotFoundError extends AgentError {
|
|
5
|
+
readonly tool: string;
|
|
6
|
+
readonly available: string[];
|
|
7
|
+
constructor(tool: string, available: string[]);
|
|
8
|
+
}
|
|
9
|
+
export declare class AgentModelMissingError extends AgentError {
|
|
10
|
+
constructor();
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CustomError } from '@celljs/core';
|
|
2
|
+
export class AgentError extends CustomError {
|
|
3
|
+
}
|
|
4
|
+
export class AgentToolNotFoundError extends AgentError {
|
|
5
|
+
constructor(tool, available) {
|
|
6
|
+
super(`No AgentTool component is named ${tool}. Known tools: ${available.length ? available.join(', ') : 'none; add one with @Component(AgentTool)'}.`);
|
|
7
|
+
this.tool = tool;
|
|
8
|
+
this.available = available;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class AgentModelMissingError extends AgentError {
|
|
12
|
+
constructor() {
|
|
13
|
+
super('No model is available for the agent: set cell.ai.agent.model ("<provider>/<model id>") and the API key of its provider (e.g. ANTHROPIC_API_KEY or cell.ai.pi.apiKeys).');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/node/error/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,MAAM,OAAO,UAAW,SAAQ,WAAW;CAAG;AAE9C,MAAM,OAAO,sBAAuB,SAAQ,UAAU;IAClD,YACa,IAAY,EACZ,SAAmB;QAE5B,KAAK,CAAC,mCAAmC,IAAI,kBAAkB,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,0CAA0C,GAAG,CAAC,CAAC;oBAH/I,IAAI;yBACJ,SAAS;IAGtB,CAAC;CACJ;AAED,MAAM,OAAO,sBAAuB,SAAQ,UAAU;IAClD;QACI,KAAK,CACD,wKAAwK,CAC3K,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/error/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The part of a Node.js `ServerResponse` that {@link AgentEventUtil.stream} writes to. */
|
|
2
|
+
export interface EventStreamResponse {
|
|
3
|
+
setHeader(name: string, value: string): unknown;
|
|
4
|
+
write(chunk: string): unknown;
|
|
5
|
+
end(): unknown;
|
|
6
|
+
flushHeaders?(): void;
|
|
7
|
+
readonly writableEnded?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** What emits agent events: a pi-agent-core `Agent` or a pi coding agent `AgentSession`. */
|
|
10
|
+
export interface AgentEventSource {
|
|
11
|
+
subscribe(listener: (event: any) => unknown): () => void;
|
|
12
|
+
}
|
|
13
|
+
/** An agent event: pi-agent-core's `AgentEvent` or pi coding agent's `AgentSessionEvent`. */
|
|
14
|
+
export interface AgentWireEvent {
|
|
15
|
+
type: string;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}
|
|
18
|
+
/** Sent by {@link AgentEventUtil.stream} when the run fails; pi's own events never use this type. */
|
|
19
|
+
export interface AgentErrorEvent {
|
|
20
|
+
type: 'error';
|
|
21
|
+
message: string;
|
|
22
|
+
}
|
|
23
|
+
export declare namespace AgentEventUtil {
|
|
24
|
+
/**
|
|
25
|
+
* The event as pi's JSON and RPC modes write it (see pi coding agent's docs/json.md):
|
|
26
|
+
* `message_update` keeps only the delta and the usage, so a stream grows linearly. Other events are unchanged.
|
|
27
|
+
*/
|
|
28
|
+
function toWire<T extends AgentWireEvent>(event: T): AgentWireEvent;
|
|
29
|
+
/** One Server-Sent Events frame: the event type as `event`, the wire event as JSON `data`. */
|
|
30
|
+
function toSse(event: AgentWireEvent | AgentErrorEvent): string;
|
|
31
|
+
/**
|
|
32
|
+
* Streams the events of `source` to `response` as Server-Sent Events while `run` (usually
|
|
33
|
+
* `() => agent.prompt(text)`) is pending, then ends the response. A failure of `run` becomes an
|
|
34
|
+
* `error` event. The agent keeps running if the client goes away; abort it explicitly.
|
|
35
|
+
*/
|
|
36
|
+
function stream(source: AgentEventSource, response: EventStreamResponse, run: () => Promise<unknown>): Promise<void>;
|
|
37
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export var AgentEventUtil;
|
|
2
|
+
(function (AgentEventUtil) {
|
|
3
|
+
/**
|
|
4
|
+
* The event as pi's JSON and RPC modes write it (see pi coding agent's docs/json.md):
|
|
5
|
+
* `message_update` keeps only the delta and the usage, so a stream grows linearly. Other events are unchanged.
|
|
6
|
+
*/
|
|
7
|
+
function toWire(event) {
|
|
8
|
+
var _a;
|
|
9
|
+
if (event.type !== 'message_update') {
|
|
10
|
+
return event;
|
|
11
|
+
}
|
|
12
|
+
const { partial, ...delta } = event.assistantMessageEvent;
|
|
13
|
+
const wire = delta;
|
|
14
|
+
if (delta.type === 'toolcall_start' && partial) {
|
|
15
|
+
const toolCall = partial.content[delta.contentIndex];
|
|
16
|
+
wire.id = toolCall === null || toolCall === void 0 ? void 0 : toolCall.id;
|
|
17
|
+
wire.toolName = toolCall === null || toolCall === void 0 ? void 0 : toolCall.name;
|
|
18
|
+
}
|
|
19
|
+
return { type: 'message_update', usage: (_a = event.message) === null || _a === void 0 ? void 0 : _a.usage, assistantMessageEvent: wire };
|
|
20
|
+
}
|
|
21
|
+
AgentEventUtil.toWire = toWire;
|
|
22
|
+
/** One Server-Sent Events frame: the event type as `event`, the wire event as JSON `data`. */
|
|
23
|
+
function toSse(event) {
|
|
24
|
+
return `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`;
|
|
25
|
+
}
|
|
26
|
+
AgentEventUtil.toSse = toSse;
|
|
27
|
+
/**
|
|
28
|
+
* Streams the events of `source` to `response` as Server-Sent Events while `run` (usually
|
|
29
|
+
* `() => agent.prompt(text)`) is pending, then ends the response. A failure of `run` becomes an
|
|
30
|
+
* `error` event. The agent keeps running if the client goes away; abort it explicitly.
|
|
31
|
+
*/
|
|
32
|
+
async function stream(source, response, run) {
|
|
33
|
+
var _a;
|
|
34
|
+
response.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
|
35
|
+
response.setHeader('Cache-Control', 'no-cache, no-transform');
|
|
36
|
+
response.setHeader('Connection', 'keep-alive');
|
|
37
|
+
response.setHeader('X-Accel-Buffering', 'no');
|
|
38
|
+
(_a = response.flushHeaders) === null || _a === void 0 ? void 0 : _a.call(response);
|
|
39
|
+
const write = (chunk) => {
|
|
40
|
+
if (!response.writableEnded) {
|
|
41
|
+
response.write(chunk);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const unsubscribe = source.subscribe(event => write(toSse(toWire(event))));
|
|
45
|
+
try {
|
|
46
|
+
await run();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
write(toSse({ type: 'error', message: error instanceof Error ? error.message : String(error) }));
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
unsubscribe();
|
|
53
|
+
if (!response.writableEnded) {
|
|
54
|
+
response.end();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
AgentEventUtil.stream = stream;
|
|
59
|
+
})(AgentEventUtil || (AgentEventUtil = {}));
|
|
60
|
+
//# sourceMappingURL=agent-event-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-event-util.js","sourceRoot":"","sources":["../../../src/node/event/agent-event-util.ts"],"names":[],"mappings":"AA0BA,MAAM,KAAW,cAAc,CAoD9B;AApDD,WAAiB,cAAc;IAC3B;;;OAGG;IACH,SAAgB,MAAM,CAA2B,KAAQ;;QACrD,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YAClC,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,GAAG,KAAK,CAAC,qBAA6F,CAAC;QAClI,MAAM,IAAI,GAA4B,KAAK,CAAC;QAC5C,IAAI,KAAK,CAAC,IAAI,KAAK,gBAAgB,IAAI,OAAO,EAAE,CAAC;YAC7C,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACrD,IAAI,CAAC,EAAE,GAAG,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,EAAE,CAAC;YACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,CAAC;QACnC,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAA,KAAK,CAAC,OAAO,0CAAE,KAAK,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAAC;IAChG,CAAC;IAZe,eAAA,MAAM,SAYrB,CAAA;IAED,8FAA8F;IAC9F,SAAgB,KAAK,CAAC,KAAuC;QACzD,OAAO,UAAU,KAAK,CAAC,IAAI,WAAW,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;IACtE,CAAC;IAFe,eAAA,KAAK,QAEpB,CAAA;IAED;;;;OAIG;IACI,KAAK,UAAU,MAAM,CAAC,MAAwB,EAAE,QAA6B,EAAE,GAA2B;;QAC7G,QAAQ,CAAC,SAAS,CAAC,cAAc,EAAE,kCAAkC,CAAC,CAAC;QACvE,QAAQ,CAAC,SAAS,CAAC,eAAe,EAAE,wBAAwB,CAAC,CAAC;QAC9D,QAAQ,CAAC,SAAS,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;QAC/C,QAAQ,CAAC,SAAS,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC;QAC9C,MAAA,QAAQ,CAAC,YAAY,+CAArB,QAAQ,CAAiB,CAAC;QAC1B,MAAM,KAAK,GAAG,CAAC,KAAa,EAAE,EAAE;YAC5B,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC;gBAC1B,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC1B,CAAC;QACL,CAAC,CAAC;QACF,MAAM,WAAW,GAAG,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3E,IAAI,CAAC;YACD,MAAM,GAAG,EAAE,CAAC;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,KAAK,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;QACrG,CAAC;gBAAS,CAAC;YACP,WAAW,EAAE,CAAC;YACd,IAAI,CAAC,QAAQ,CAAC,aAAa,EAAE,CAAC;gBAC1B,QAAQ,CAAC,GAAG,EAAE,CAAC;YACnB,CAAC;QACL,CAAC;IACL,CAAC;IAtBqB,eAAA,MAAM,SAsB3B,CAAA;AACL,CAAC,EApDgB,cAAc,KAAd,cAAc,QAoD9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './agent-event-util.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/event/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../src/node/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,SAAS,CAAC;AAEjB,eAAe,QAAQ,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/pi/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The module namespace of `@earendil-works/pi-agent-core`. */
|
|
2
|
+
export type PiAgentCore = typeof import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } });
|
|
3
|
+
export declare namespace PiAgentCoreUtil {
|
|
4
|
+
/** Loads pi-agent-core once, with a dynamic `import()`: it is an ES module package. */
|
|
5
|
+
function load(): Promise<PiAgentCore>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AgentError } from '../error/index.js';
|
|
2
|
+
export var PiAgentCoreUtil;
|
|
3
|
+
(function (PiAgentCoreUtil) {
|
|
4
|
+
let core;
|
|
5
|
+
/** Loads pi-agent-core once, with a dynamic `import()`: it is an ES module package. */
|
|
6
|
+
function load() {
|
|
7
|
+
core !== null && core !== void 0 ? core : (core = import('@earendil-works/pi-agent-core').catch(cause => {
|
|
8
|
+
core = undefined;
|
|
9
|
+
throw new AgentError('Cannot load @earendil-works/pi-agent-core. Install it next to @celljs/ai-agent; it needs Node.js >= 22.19.0 or Bun.', {
|
|
10
|
+
cause,
|
|
11
|
+
});
|
|
12
|
+
}));
|
|
13
|
+
return core;
|
|
14
|
+
}
|
|
15
|
+
PiAgentCoreUtil.load = load;
|
|
16
|
+
})(PiAgentCoreUtil || (PiAgentCoreUtil = {}));
|
|
17
|
+
//# sourceMappingURL=pi-agent-core-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pi-agent-core-util.js","sourceRoot":"","sources":["../../../src/node/pi/pi-agent-core-util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAKtC,MAAM,KAAW,eAAe,CAa/B;AAbD,WAAiB,eAAe;IAC5B,IAAI,IAAsC,CAAC;IAE3C,uFAAuF;IACvF,SAAgB,IAAI;QAChB,IAAI,aAAJ,IAAI,cAAJ,IAAI,IAAJ,IAAI,GAAK,MAAM,CAAC,+BAA+B,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAC3D,IAAI,GAAG,SAAS,CAAC;YACjB,MAAM,IAAI,UAAU,CAAC,qHAAqH,EAAE;gBACxI,KAAK;aACR,CAAC,CAAC;QACP,CAAC,CAAC,EAAC;QACH,OAAO,IAAI,CAAC;IAChB,CAAC;IARe,gBAAA,IAAI,OAQnB,CAAA;AACL,CAAC,EAbgB,eAAe,KAAf,eAAe,QAa/B"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type Agent = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).Agent;
|
|
2
|
+
export type AgentEvent = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).AgentEvent;
|
|
3
|
+
export type AgentMessage = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).AgentMessage;
|
|
4
|
+
export type AgentToolResult<T = any> = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).AgentToolResult<T>;
|
|
5
|
+
export type AgentToolUpdateCallback<T = any> = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).AgentToolUpdateCallback<T>;
|
|
6
|
+
export type ThinkingLevel = import('@earendil-works/pi-agent-core', { with: { 'resolution-mode': 'import' } }).ThinkingLevel;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pi-types.js","sourceRoot":"","sources":["../../../src/node/pi/pi-types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/test/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAoB,MAAM,6BAA6B,CAAC;AAC7E,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createPiTestContainer, type PiTestContainer } from '@celljs/ai-pi/lib/node/test';
|
|
2
|
+
import type { AgentOptions } from '../agent-protocol.js';
|
|
3
|
+
import '../index.js';
|
|
4
|
+
type ContainerModule = Parameters<typeof createPiTestContainer>[1];
|
|
5
|
+
/** A container with ai-core, ai-pi, this package and the scripted `FauxPiModels` as the agents' model. */
|
|
6
|
+
export declare function createAgentTestContainer(options?: AgentOptions, ...modules: ContainerModule[]): Promise<PiTestContainer>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createPiTestContainer, FauxPiModels } from '@celljs/ai-pi/lib/node/test';
|
|
2
|
+
import { autoBind, currentThis } from '@celljs/core';
|
|
3
|
+
import '../index.js';
|
|
4
|
+
const agentModule = autoBind();
|
|
5
|
+
/** A container with ai-core, ai-pi, this package and the scripted `FauxPiModels` as the agents' model. */
|
|
6
|
+
export async function createAgentTestContainer(options = {}, ...modules) {
|
|
7
|
+
const test = await createPiTestContainer({}, agentModule, ...modules);
|
|
8
|
+
currentThis.cellProps.cell.ai.agent = { model: FauxPiModels.MODEL, ...options };
|
|
9
|
+
return test;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=test-container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-container.js","sourceRoot":"","sources":["../../../src/node/test/test-container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAwB,MAAM,6BAA6B,CAAC;AACxG,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,UAAU,CAAC;AAElB,MAAM,WAAW,GAAG,QAAQ,EAAE,CAAC;AAI/B,0GAA0G;AAC1G,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,OAAO,GAAiB,EAAE,EAAE,GAAG,OAA0B;IACpG,MAAM,IAAI,GAAG,MAAM,qBAAqB,CAAC,EAAE,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,CAAC;IACtE,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,GAAG,EAAE,KAAK,EAAE,YAAY,CAAC,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC;IAChF,OAAO,IAAI,CAAC;AAChB,CAAC"}
|
package/esm/package.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PiModelsProvider } from '@celljs/ai-pi';
|
|
2
|
+
import { type AgentCreateOptions, AgentFactory, type AgentOptions, AgentTool } from '../agent-protocol';
|
|
3
|
+
import { type Agent } from '../pi';
|
|
4
|
+
export declare class DefaultAgentFactory implements AgentFactory {
|
|
5
|
+
protected readonly options?: AgentOptions;
|
|
6
|
+
protected readonly modelsProvider: PiModelsProvider;
|
|
7
|
+
protected readonly tools: AgentTool[];
|
|
8
|
+
create(createOptions?: AgentCreateOptions): Promise<Agent>;
|
|
9
|
+
protected selectTools(names?: string[]): AgentTool[];
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=agent-factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-factory.d.ts","sourceRoot":"","sources":["../../../src/node/agent/agent-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAiB,KAAK,kBAAkB,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEvH,OAAO,EAAE,KAAK,KAAK,EAAmB,MAAM,OAAO,CAAC;AAEpD,qBACa,mBAAoB,YAAW,YAAY;IAEpD,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC;IAG1C,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,gBAAgB,CAAC;IAIpD,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,CAAM;IAErC,MAAM,CAAC,aAAa,GAAE,kBAAuB,GAAG,OAAO,CAAC,KAAK,CAAC,CAuBnE;IAED,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,CAenD;CACJ"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DefaultAgentFactory = void 0;
|
|
13
|
+
const ai_pi_1 = require("@celljs/ai-pi");
|
|
14
|
+
const core_1 = require("@celljs/core");
|
|
15
|
+
const agent_protocol_1 = require("../agent-protocol");
|
|
16
|
+
const error_1 = require("../error");
|
|
17
|
+
const pi_1 = require("../pi");
|
|
18
|
+
let DefaultAgentFactory = class DefaultAgentFactory {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.tools = [];
|
|
21
|
+
}
|
|
22
|
+
async create(createOptions = {}) {
|
|
23
|
+
var _a, _b, _c;
|
|
24
|
+
const options = { ...this.options };
|
|
25
|
+
for (const [key, value] of Object.entries(createOptions)) {
|
|
26
|
+
if (value !== undefined && key !== 'messages') {
|
|
27
|
+
options[key] = value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const [core, models] = await Promise.all([pi_1.PiAgentCoreUtil.load(), this.modelsProvider.provide()]);
|
|
31
|
+
const model = options.model ? await this.modelsProvider.resolve(options.model) : (await models.getAvailable())[0];
|
|
32
|
+
if (!model) {
|
|
33
|
+
throw new error_1.AgentModelMissingError();
|
|
34
|
+
}
|
|
35
|
+
return new core.Agent({
|
|
36
|
+
initialState: {
|
|
37
|
+
systemPrompt: (_a = options.systemPrompt) !== null && _a !== void 0 ? _a : '',
|
|
38
|
+
model,
|
|
39
|
+
thinkingLevel: (_b = options.thinkingLevel) !== null && _b !== void 0 ? _b : 'off',
|
|
40
|
+
tools: this.selectTools(options.tools),
|
|
41
|
+
messages: (_c = createOptions.messages) !== null && _c !== void 0 ? _c : [],
|
|
42
|
+
},
|
|
43
|
+
streamFn: models.streamSimple.bind(models),
|
|
44
|
+
toolExecution: options.toolExecution,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
selectTools(names) {
|
|
48
|
+
var _a;
|
|
49
|
+
const tools = (_a = this.tools) !== null && _a !== void 0 ? _a : [];
|
|
50
|
+
if (!names) {
|
|
51
|
+
return tools;
|
|
52
|
+
}
|
|
53
|
+
return names.map(name => {
|
|
54
|
+
const tool = tools.find(t => t.name === name);
|
|
55
|
+
if (!tool) {
|
|
56
|
+
throw new error_1.AgentToolNotFoundError(name, tools.map(t => t.name));
|
|
57
|
+
}
|
|
58
|
+
return tool;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.DefaultAgentFactory = DefaultAgentFactory;
|
|
63
|
+
__decorate([
|
|
64
|
+
(0, core_1.Value)(agent_protocol_1.AGENT_OPTIONS),
|
|
65
|
+
__metadata("design:type", Object)
|
|
66
|
+
], DefaultAgentFactory.prototype, "options", void 0);
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, core_1.Autowired)(ai_pi_1.PiModelsProvider),
|
|
69
|
+
__metadata("design:type", Object)
|
|
70
|
+
], DefaultAgentFactory.prototype, "modelsProvider", void 0);
|
|
71
|
+
__decorate([
|
|
72
|
+
(0, core_1.Autowired)(agent_protocol_1.AgentTool),
|
|
73
|
+
(0, core_1.Optional)(),
|
|
74
|
+
__metadata("design:type", Array)
|
|
75
|
+
], DefaultAgentFactory.prototype, "tools", void 0);
|
|
76
|
+
exports.DefaultAgentFactory = DefaultAgentFactory = __decorate([
|
|
77
|
+
(0, core_1.Component)(agent_protocol_1.AgentFactory)
|
|
78
|
+
], DefaultAgentFactory);
|
|
79
|
+
//# sourceMappingURL=agent-factory.js.map
|