palpal-core 0.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/LICENSE +21 -0
- package/README.ja.md +114 -0
- package/README.md +118 -0
- package/dist/src/agent.d.ts +16 -0
- package/dist/src/agent.js +21 -0
- package/dist/src/approval.d.ts +14 -0
- package/dist/src/approval.js +67 -0
- package/dist/src/errors.d.ts +9 -0
- package/dist/src/errors.js +52 -0
- package/dist/src/guardrails.d.ts +11 -0
- package/dist/src/guardrails.js +29 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +27 -0
- package/dist/src/mcp.d.ts +15 -0
- package/dist/src/mcp.js +34 -0
- package/dist/src/providers.d.ts +2 -0
- package/dist/src/providers.js +413 -0
- package/dist/src/runner.d.ts +31 -0
- package/dist/src/runner.js +389 -0
- package/dist/src/safety.d.ts +37 -0
- package/dist/src/safety.js +348 -0
- package/dist/src/skills.d.ts +19 -0
- package/dist/src/skills.js +269 -0
- package/dist/src/tools.d.ts +21 -0
- package/dist/src/tools.js +53 -0
- package/dist/src/types.d.ts +227 -0
- package/dist/src/types.js +2 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Naruhide KITADA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ja.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# palpal-core (日本語)
|
|
2
|
+
|
|
3
|
+
`palpal-core` は、安全性を重視した OpenAI Agents SDK 互換(TypeScript)の npm ライブラリです。
|
|
4
|
+
Agents SDK の使い勝手を保ちながら、MCP / Skills / Tool 実行の実運用向け安全制御を追加します。
|
|
5
|
+
|
|
6
|
+
## 位置づけ
|
|
7
|
+
|
|
8
|
+
- OpenAI Agents SDK 互換I/Fを維持し、移行コストを最小化
|
|
9
|
+
- `SafetyAgent + Guardrails + Approval flow` による Safety-first 実行モデル
|
|
10
|
+
- MCP / Skills を「実行前」に防御しやすい設計
|
|
11
|
+
|
|
12
|
+
## 何を解決するか
|
|
13
|
+
|
|
14
|
+
現在のエージェント実装では、次の課題が起きやすいです。
|
|
15
|
+
|
|
16
|
+
- MCP/Tools の実行前安全判定が実装依存になりやすい
|
|
17
|
+
- Skills (`SKILL.md`) をそのまま実行可能な Tool として扱いにくい
|
|
18
|
+
- 人間承認が必要な処理の「中断 -> 再開」制御が分散しやすい
|
|
19
|
+
- Chat Completions 互換モデル(Ollama など)を統一I/Fで扱いにくい
|
|
20
|
+
|
|
21
|
+
`palpal-core` はこれを、`SafetyAgent + Guardrails + Approval flow` で最小差分に統合します。
|
|
22
|
+
|
|
23
|
+
## 設計のポイント
|
|
24
|
+
|
|
25
|
+
- Agents SDK 互換I/F:
|
|
26
|
+
- `new Agent(...)`
|
|
27
|
+
- `run(agent, input, options?)`
|
|
28
|
+
- `tool(...)`, `hostedMcpTool(...)`
|
|
29
|
+
- SafetyAgent:
|
|
30
|
+
- `runner.run(agent, ...)` 時に `Agent` から Skills/MCP/Tools を自動抽出
|
|
31
|
+
- Go/No-Go/needs_human を fail-closed で判定
|
|
32
|
+
- ModelSafetyAgent:
|
|
33
|
+
- model + rubric(箇条書き文字列)で判定器を構成
|
|
34
|
+
- 対象 `Agent` の tool/skill/MCP 文脈を入力に含める
|
|
35
|
+
- 構造化出力(`allow|deny|needs_human`)で安定判定
|
|
36
|
+
- 既定は `includeUserIntent: false`(生のユーザー入力は含めない)
|
|
37
|
+
- OpenAI Agents SDK の一般ガードレール併用:
|
|
38
|
+
- `agent.guardrails.input/tool/output` を追加
|
|
39
|
+
- `SafetyAgent` とは独立に deny できる二重防御
|
|
40
|
+
- Skills:
|
|
41
|
+
- `loadSkills(...) -> Skill[]`
|
|
42
|
+
- `toTools(skills)` で function tools 化
|
|
43
|
+
- `listSkills` / `describeSkill` / `toIntrospectionTools`
|
|
44
|
+
- Provider:
|
|
45
|
+
- `getProvider("ollama").getModel("gpt-oss-20b")`
|
|
46
|
+
- OpenAI / Ollama / LM Studio / Gemini / Anthropic / OpenRouter
|
|
47
|
+
|
|
48
|
+
## インストール
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install palpal-core
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 最小例
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import {
|
|
58
|
+
Agent,
|
|
59
|
+
SafetyAgent,
|
|
60
|
+
createRunner,
|
|
61
|
+
tool,
|
|
62
|
+
getProvider
|
|
63
|
+
} from "palpal-core";
|
|
64
|
+
|
|
65
|
+
const runner = createRunner({
|
|
66
|
+
safetyAgent: new SafetyAgent(async (_agent, request) => {
|
|
67
|
+
if (request.tool_kind === "mcp") {
|
|
68
|
+
return { decision: "needs_human", reason: "MCP review required", risk_level: 4 };
|
|
69
|
+
}
|
|
70
|
+
return { decision: "allow", reason: "safe", risk_level: 1 };
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const agent = new Agent({
|
|
75
|
+
name: "assistant",
|
|
76
|
+
instructions: "be helpful",
|
|
77
|
+
model: getProvider("ollama").getModel("gpt-oss-20b"),
|
|
78
|
+
tools: [
|
|
79
|
+
tool({
|
|
80
|
+
name: "echo",
|
|
81
|
+
description: "echo text",
|
|
82
|
+
execute: async (args) => args
|
|
83
|
+
})
|
|
84
|
+
],
|
|
85
|
+
guardrails: {
|
|
86
|
+
input: [
|
|
87
|
+
({ inputText }) => ({
|
|
88
|
+
allow: !inputText.includes("forbidden"),
|
|
89
|
+
reason: "forbidden input"
|
|
90
|
+
})
|
|
91
|
+
]
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const result = await runner.run(agent, "hello", {
|
|
96
|
+
extensions: {
|
|
97
|
+
toolCalls: [{ toolName: "echo", args: { text: "hello" } }]
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## MCP/Skills を防御しやすい理由
|
|
103
|
+
|
|
104
|
+
- SafetyAgent は実行対象 `Agent` から能力情報を自動取得するため、判定コンテキストの漏れを減らせる
|
|
105
|
+
- Guardrails を入力/ツール/出力で分離でき、禁止条件を短く保てる
|
|
106
|
+
- `needs_human` の承認トークンで、中断処理を安全に再開できる
|
|
107
|
+
|
|
108
|
+
## チュートリアル
|
|
109
|
+
|
|
110
|
+
- 日本語: [tutorials/ja/getting-started.md](./tutorials/ja/getting-started.md)
|
|
111
|
+
- 英語: [tutorials/en/getting-started.md](./tutorials/en/getting-started.md)
|
|
112
|
+
- Filesystem MCP + SafetyAgent サンプル: [tutorials/samples/filesystem-mcp-safety.ts](./tutorials/samples/filesystem-mcp-safety.ts)
|
|
113
|
+
- ModelSafetyAgent サンプル: [tutorials/samples/model-safety-agent.ts](./tutorials/samples/model-safety-agent.ts)
|
|
114
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# palpal-core
|
|
2
|
+
|
|
3
|
+
`palpal-core` is a high-safety, OpenAI Agents SDK-compatible npm library for TypeScript.
|
|
4
|
+
It keeps the familiar Agents SDK style while adding practical runtime safety controls for MCP, Skills, and tool execution.
|
|
5
|
+
|
|
6
|
+
## Positioning
|
|
7
|
+
|
|
8
|
+
- OpenAI Agents SDK-compatible interface with minimal migration cost
|
|
9
|
+
- Safety-first execution model (`SafetyAgent + Guardrails + Approval flow`)
|
|
10
|
+
- Designed to make MCP/Skills easier to defend before execution
|
|
11
|
+
|
|
12
|
+
## What problems it solves
|
|
13
|
+
|
|
14
|
+
In many current agent stacks:
|
|
15
|
+
|
|
16
|
+
- MCP/tool safety checks are inconsistent or ad-hoc
|
|
17
|
+
- Skills (`SKILL.md`) are hard to expose as executable tools
|
|
18
|
+
- Human approval flows (`pause -> resume`) are fragmented
|
|
19
|
+
- Chat Completions compatible backends are not unified
|
|
20
|
+
|
|
21
|
+
`palpal-core` addresses this with a single execution layer:
|
|
22
|
+
`SafetyAgent + Guardrails + Approval flow`.
|
|
23
|
+
|
|
24
|
+
## Design highlights
|
|
25
|
+
|
|
26
|
+
- Agents SDK-like interface:
|
|
27
|
+
- `new Agent(...)`
|
|
28
|
+
- `run(agent, input, options?)`
|
|
29
|
+
- `tool(...)`, `hostedMcpTool(...)`
|
|
30
|
+
- `SafetyAgent`:
|
|
31
|
+
- derives Skills/MCP/Tools from the `Agent` at `runner.run(...)`
|
|
32
|
+
- fail-closed Go/No-Go/needs_human decision
|
|
33
|
+
- `ModelSafetyAgent`:
|
|
34
|
+
- accepts a model + rubric (bullet-string rules)
|
|
35
|
+
- evaluates tool/skill/MCP context from the target `Agent`
|
|
36
|
+
- returns stable structured decision (`allow|deny|needs_human`)
|
|
37
|
+
- defaults to `includeUserIntent: false` (raw user input is excluded)
|
|
38
|
+
- MCP approval policy:
|
|
39
|
+
- `hostedMcpTool(..., { requireApproval: true })` forces `needs_human` before execution
|
|
40
|
+
- General guardrails (OpenAI Agents SDK style) are also supported:
|
|
41
|
+
- `agent.guardrails.input/tool/output`
|
|
42
|
+
- independent deny path in addition to `SafetyAgent`
|
|
43
|
+
- Skills:
|
|
44
|
+
- `loadSkills(...) -> Skill[]`
|
|
45
|
+
- `toTools(skills)` for function-tool conversion
|
|
46
|
+
- `listSkills` / `describeSkill` / `toIntrospectionTools`
|
|
47
|
+
- Providers:
|
|
48
|
+
- `getProvider("ollama").getModel("gpt-oss-20b")`
|
|
49
|
+
- OpenAI / Ollama / LM Studio / Gemini / Anthropic / OpenRouter
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install palpal-core
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Minimal example
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import {
|
|
61
|
+
Agent,
|
|
62
|
+
SafetyAgent,
|
|
63
|
+
createRunner,
|
|
64
|
+
tool,
|
|
65
|
+
getProvider
|
|
66
|
+
} from "palpal-core";
|
|
67
|
+
|
|
68
|
+
const runner = createRunner({
|
|
69
|
+
safetyAgent: new SafetyAgent(async (_agent, request) => {
|
|
70
|
+
if (request.tool_kind === "mcp") {
|
|
71
|
+
return { decision: "needs_human", reason: "MCP review required", risk_level: 4 };
|
|
72
|
+
}
|
|
73
|
+
return { decision: "allow", reason: "safe", risk_level: 1 };
|
|
74
|
+
})
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const agent = new Agent({
|
|
78
|
+
name: "assistant",
|
|
79
|
+
instructions: "be helpful",
|
|
80
|
+
model: getProvider("ollama").getModel("gpt-oss-20b"),
|
|
81
|
+
tools: [
|
|
82
|
+
tool({
|
|
83
|
+
name: "echo",
|
|
84
|
+
description: "echo text",
|
|
85
|
+
execute: async (args) => args
|
|
86
|
+
})
|
|
87
|
+
],
|
|
88
|
+
guardrails: {
|
|
89
|
+
input: [
|
|
90
|
+
({ inputText }) => ({
|
|
91
|
+
allow: !inputText.includes("forbidden"),
|
|
92
|
+
reason: "forbidden input"
|
|
93
|
+
})
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const result = await runner.run(agent, "hello", {
|
|
99
|
+
extensions: {
|
|
100
|
+
toolCalls: [{ toolName: "echo", args: { text: "hello" } }]
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Why MCP/Skills are easier to defend
|
|
106
|
+
|
|
107
|
+
- `SafetyAgent` evaluates with capability snapshot derived from the actual `Agent`
|
|
108
|
+
- guardrails are split by stage (`input/tool/output`) for concise policy rules
|
|
109
|
+
- `needs_human` supports explicit approval and safe resume
|
|
110
|
+
|
|
111
|
+
## Docs
|
|
112
|
+
|
|
113
|
+
- Japanese README: [README.ja.md](./README.ja.md)
|
|
114
|
+
- Japanese tutorial: [tutorials/ja/getting-started.md](./tutorials/ja/getting-started.md)
|
|
115
|
+
- English tutorial: [tutorials/en/getting-started.md](./tutorials/en/getting-started.md)
|
|
116
|
+
- Filesystem MCP + SafetyAgent sample: [tutorials/samples/filesystem-mcp-safety.ts](./tutorials/samples/filesystem-mcp-safety.ts)
|
|
117
|
+
- ModelSafetyAgent sample: [tutorials/samples/model-safety-agent.ts](./tutorials/samples/model-safety-agent.ts)
|
|
118
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AgentGuardrails, AgentLike, Model, Tool } from "./types";
|
|
2
|
+
export interface AgentOptions {
|
|
3
|
+
name: string;
|
|
4
|
+
instructions: string;
|
|
5
|
+
tools?: Tool[];
|
|
6
|
+
model?: Model;
|
|
7
|
+
guardrails?: AgentGuardrails;
|
|
8
|
+
}
|
|
9
|
+
export declare class Agent implements AgentLike {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly instructions: string;
|
|
12
|
+
readonly tools: Tool[];
|
|
13
|
+
readonly model?: Model;
|
|
14
|
+
readonly guardrails?: AgentGuardrails;
|
|
15
|
+
constructor(options: AgentOptions);
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Agent = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
class Agent {
|
|
6
|
+
name;
|
|
7
|
+
instructions;
|
|
8
|
+
tools;
|
|
9
|
+
model;
|
|
10
|
+
guardrails;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
(0, errors_1.ensure)(options.name?.trim(), "AGENTS-E-RUNNER-CONFIG", "Agent.name is required.");
|
|
13
|
+
(0, errors_1.ensure)(options.instructions?.trim(), "AGENTS-E-RUNNER-CONFIG", "Agent.instructions is required.");
|
|
14
|
+
this.name = options.name;
|
|
15
|
+
this.instructions = options.instructions;
|
|
16
|
+
this.tools = options.tools ? [...options.tools] : [];
|
|
17
|
+
this.model = options.model;
|
|
18
|
+
this.guardrails = options.guardrails;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.Agent = Agent;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { GateDecision, HumanApprovalRequest, ResumeToken } from "./types";
|
|
2
|
+
export declare class ApprovalController {
|
|
3
|
+
private readonly approvals;
|
|
4
|
+
private readonly tokens;
|
|
5
|
+
private readonly ttlSec;
|
|
6
|
+
constructor(ttlSec?: number);
|
|
7
|
+
createApprovalRequest(runId: string, gateDecision: GateDecision, prompt: string): Promise<HumanApprovalRequest>;
|
|
8
|
+
getPendingApprovals(runId?: string): Promise<HumanApprovalRequest[]>;
|
|
9
|
+
submitApproval(approvalId: string, decision: "approve" | "deny", comment?: string): Promise<ResumeToken>;
|
|
10
|
+
consumeResumeToken(runId: string, tokenValue: string): {
|
|
11
|
+
decision: "approve" | "deny";
|
|
12
|
+
approvalId: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApprovalController = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
class ApprovalController {
|
|
6
|
+
approvals = new Map();
|
|
7
|
+
tokens = new Map();
|
|
8
|
+
ttlSec;
|
|
9
|
+
constructor(ttlSec = 900) {
|
|
10
|
+
this.ttlSec = ttlSec;
|
|
11
|
+
}
|
|
12
|
+
async createApprovalRequest(runId, gateDecision, prompt) {
|
|
13
|
+
(0, errors_1.ensure)(runId, "AGENTS-E-APPROVAL-INVALID", "runId is required.");
|
|
14
|
+
(0, errors_1.ensure)(gateDecision.decision === "needs_human", "AGENTS-E-APPROVAL-INVALID", "createApprovalRequest requires decision=needs_human.");
|
|
15
|
+
const approval = {
|
|
16
|
+
approval_id: gateDecision.approval_id ?? (0, errors_1.createId)("approval"),
|
|
17
|
+
run_id: runId,
|
|
18
|
+
required_action: "human_review",
|
|
19
|
+
prompt,
|
|
20
|
+
status: "pending"
|
|
21
|
+
};
|
|
22
|
+
this.approvals.set(approval.approval_id, { request: approval });
|
|
23
|
+
return approval;
|
|
24
|
+
}
|
|
25
|
+
async getPendingApprovals(runId) {
|
|
26
|
+
const items = [...this.approvals.values()]
|
|
27
|
+
.map((record) => record.request)
|
|
28
|
+
.filter((request) => request.status === "pending");
|
|
29
|
+
if (!runId) {
|
|
30
|
+
return items;
|
|
31
|
+
}
|
|
32
|
+
return items.filter((item) => item.run_id === runId);
|
|
33
|
+
}
|
|
34
|
+
async submitApproval(approvalId, decision, comment) {
|
|
35
|
+
const record = this.approvals.get(approvalId);
|
|
36
|
+
(0, errors_1.ensure)(record, "AGENTS-E-APPROVAL-NOT-FOUND", `Approval not found: ${approvalId}`);
|
|
37
|
+
(0, errors_1.ensure)(record.request.status === "pending", "AGENTS-E-APPROVAL-INVALID", `Approval is not pending: ${approvalId}`);
|
|
38
|
+
record.request.status = decision === "approve" ? "approved" : "denied";
|
|
39
|
+
record.comment = comment;
|
|
40
|
+
const expiresAt = new Date(Date.now() + this.ttlSec * 1000).toISOString();
|
|
41
|
+
const token = {
|
|
42
|
+
token: (0, errors_1.createId)("resume"),
|
|
43
|
+
run_id: record.request.run_id,
|
|
44
|
+
expires_at: expiresAt,
|
|
45
|
+
status: "active"
|
|
46
|
+
};
|
|
47
|
+
this.tokens.set(token.token, {
|
|
48
|
+
token,
|
|
49
|
+
approvalId,
|
|
50
|
+
decision
|
|
51
|
+
});
|
|
52
|
+
return token;
|
|
53
|
+
}
|
|
54
|
+
consumeResumeToken(runId, tokenValue) {
|
|
55
|
+
const tokenRecord = this.tokens.get(tokenValue);
|
|
56
|
+
(0, errors_1.ensure)(tokenRecord, "AGENTS-E-RESUME-TOKEN", "Resume token is not found.");
|
|
57
|
+
(0, errors_1.ensure)(tokenRecord.token.run_id === runId, "AGENTS-E-RESUME-TOKEN", "runId mismatch.");
|
|
58
|
+
(0, errors_1.ensure)(tokenRecord.token.status === "active", "AGENTS-E-RESUME-TOKEN", "Token is not active.");
|
|
59
|
+
(0, errors_1.ensure)(new Date(tokenRecord.token.expires_at).getTime() > Date.now(), "AGENTS-E-RESUME-TOKEN", "Token has expired.");
|
|
60
|
+
tokenRecord.token.status = "used";
|
|
61
|
+
return {
|
|
62
|
+
decision: tokenRecord.decision,
|
|
63
|
+
approvalId: tokenRecord.approvalId
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.ApprovalController = ApprovalController;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type AgentsErrorCategory = "provider" | "safety" | "approval" | "skills" | "mcp" | "runner" | "policy" | "unknown";
|
|
2
|
+
export declare class AgentsError extends Error {
|
|
3
|
+
readonly code: string;
|
|
4
|
+
readonly category: AgentsErrorCategory;
|
|
5
|
+
readonly details?: unknown;
|
|
6
|
+
constructor(code: string, message: string, details?: unknown);
|
|
7
|
+
}
|
|
8
|
+
export declare function ensure(condition: unknown, code: string, message: string, details?: unknown): asserts condition;
|
|
9
|
+
export declare function createId(prefix: string): string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentsError = void 0;
|
|
4
|
+
exports.ensure = ensure;
|
|
5
|
+
exports.createId = createId;
|
|
6
|
+
class AgentsError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
category;
|
|
9
|
+
details;
|
|
10
|
+
constructor(code, message, details) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "AgentsError";
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.category = inferErrorCategory(code);
|
|
15
|
+
this.details = details;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.AgentsError = AgentsError;
|
|
19
|
+
function ensure(condition, code, message, details) {
|
|
20
|
+
if (!condition) {
|
|
21
|
+
throw new AgentsError(code, message, details);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function createId(prefix) {
|
|
25
|
+
const stamp = Date.now().toString(36);
|
|
26
|
+
const random = Math.random().toString(36).slice(2, 10);
|
|
27
|
+
return `${prefix}_${stamp}_${random}`;
|
|
28
|
+
}
|
|
29
|
+
function inferErrorCategory(code) {
|
|
30
|
+
if (code.includes("PROVIDER")) {
|
|
31
|
+
return "provider";
|
|
32
|
+
}
|
|
33
|
+
if (code.includes("GATE") || code.includes("GUARDRAIL") || code.includes("CAPABILITY")) {
|
|
34
|
+
return "safety";
|
|
35
|
+
}
|
|
36
|
+
if (code.includes("APPROVAL") || code.includes("RESUME")) {
|
|
37
|
+
return "approval";
|
|
38
|
+
}
|
|
39
|
+
if (code.includes("SKILL")) {
|
|
40
|
+
return "skills";
|
|
41
|
+
}
|
|
42
|
+
if (code.includes("MCP")) {
|
|
43
|
+
return "mcp";
|
|
44
|
+
}
|
|
45
|
+
if (code.includes("POLICY")) {
|
|
46
|
+
return "policy";
|
|
47
|
+
}
|
|
48
|
+
if (code.includes("RUNNER") || code.includes("RUN")) {
|
|
49
|
+
return "runner";
|
|
50
|
+
}
|
|
51
|
+
return "unknown";
|
|
52
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AgentGuardrails, GuardrailHandler } from "./types";
|
|
2
|
+
export interface StaticGuardrailRule {
|
|
3
|
+
denyWhen(inputText: string): boolean;
|
|
4
|
+
reason: string;
|
|
5
|
+
}
|
|
6
|
+
export interface GuardrailsTemplateOptions {
|
|
7
|
+
inputRules?: StaticGuardrailRule[];
|
|
8
|
+
outputRules?: StaticGuardrailRule[];
|
|
9
|
+
toolRules?: GuardrailHandler[];
|
|
10
|
+
}
|
|
11
|
+
export declare function createGuardrailsTemplate(options?: GuardrailsTemplateOptions): AgentGuardrails;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createGuardrailsTemplate = createGuardrailsTemplate;
|
|
4
|
+
function createGuardrailsTemplate(options) {
|
|
5
|
+
const input = createStaticHandlers(options?.inputRules);
|
|
6
|
+
const output = createStaticHandlers(options?.outputRules);
|
|
7
|
+
const tool = options?.toolRules ?? [];
|
|
8
|
+
return {
|
|
9
|
+
input: input.length > 0 ? input : undefined,
|
|
10
|
+
// Tool execution decision should primarily come from SafetyAgent.
|
|
11
|
+
tool: tool.length > 0 ? tool : undefined,
|
|
12
|
+
output: output.length > 0 ? output : undefined
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function createStaticHandlers(rules) {
|
|
16
|
+
if (!rules || rules.length === 0) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
return rules.map((rule) => {
|
|
20
|
+
return ({ stage, inputText, finalOutputText }) => {
|
|
21
|
+
const target = stage === "output" ? finalOutputText ?? "" : inputText;
|
|
22
|
+
const denied = rule.denyWhen(target);
|
|
23
|
+
return {
|
|
24
|
+
allow: !denied,
|
|
25
|
+
reason: denied ? rule.reason : undefined
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./agent";
|
|
2
|
+
export * from "./approval";
|
|
3
|
+
export * from "./errors";
|
|
4
|
+
export * from "./guardrails";
|
|
5
|
+
export * from "./mcp";
|
|
6
|
+
export * from "./providers";
|
|
7
|
+
export * from "./runner";
|
|
8
|
+
export * from "./safety";
|
|
9
|
+
export * from "./skills";
|
|
10
|
+
export * from "./tools";
|
|
11
|
+
export * from "./types";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./agent"), exports);
|
|
18
|
+
__exportStar(require("./approval"), exports);
|
|
19
|
+
__exportStar(require("./errors"), exports);
|
|
20
|
+
__exportStar(require("./guardrails"), exports);
|
|
21
|
+
__exportStar(require("./mcp"), exports);
|
|
22
|
+
__exportStar(require("./providers"), exports);
|
|
23
|
+
__exportStar(require("./runner"), exports);
|
|
24
|
+
__exportStar(require("./safety"), exports);
|
|
25
|
+
__exportStar(require("./skills"), exports);
|
|
26
|
+
__exportStar(require("./tools"), exports);
|
|
27
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { JsonObject, McpCapabilitySummary } from "./types";
|
|
2
|
+
import { HostedMcpServer } from "./tools";
|
|
3
|
+
export interface McpServerConfig extends HostedMcpServer {
|
|
4
|
+
server_id?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface McpServerHandle {
|
|
7
|
+
server_id: string;
|
|
8
|
+
capabilities: McpCapabilitySummary[];
|
|
9
|
+
}
|
|
10
|
+
export declare class McpGateway {
|
|
11
|
+
private readonly servers;
|
|
12
|
+
register(config: McpServerConfig): Promise<McpServerHandle>;
|
|
13
|
+
introspect(serverId: string): Promise<McpCapabilitySummary[]>;
|
|
14
|
+
call(serverId: string, toolName: string, args: JsonObject): Promise<unknown>;
|
|
15
|
+
}
|
package/dist/src/mcp.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpGateway = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
class McpGateway {
|
|
6
|
+
servers = new Map();
|
|
7
|
+
async register(config) {
|
|
8
|
+
(0, errors_1.ensure)(config.url, "AGENTS-E-MCP-UNREACHABLE", "MCP server url is required.");
|
|
9
|
+
const serverId = config.server_id ?? config.id ?? (0, errors_1.createId)("mcp");
|
|
10
|
+
this.servers.set(serverId, config);
|
|
11
|
+
const capabilities = await this.introspect(serverId).catch(() => []);
|
|
12
|
+
return {
|
|
13
|
+
server_id: serverId,
|
|
14
|
+
capabilities
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async introspect(serverId) {
|
|
18
|
+
const server = this.servers.get(serverId);
|
|
19
|
+
(0, errors_1.ensure)(server, "AGENTS-E-MCP-UNREACHABLE", `MCP server is not registered: ${serverId}`);
|
|
20
|
+
if (!server.listTools) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
const capabilities = await server.listTools();
|
|
24
|
+
(0, errors_1.ensure)(Array.isArray(capabilities), "AGENTS-E-MCP-SCHEMA", "MCP tools/list must return an array.");
|
|
25
|
+
return capabilities;
|
|
26
|
+
}
|
|
27
|
+
async call(serverId, toolName, args) {
|
|
28
|
+
const server = this.servers.get(serverId);
|
|
29
|
+
(0, errors_1.ensure)(server, "AGENTS-E-MCP-UNREACHABLE", `MCP server is not registered: ${serverId}`);
|
|
30
|
+
(0, errors_1.ensure)(toolName, "AGENTS-E-MCP-EXEC", "toolName is required.");
|
|
31
|
+
return server.callTool(toolName, args);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.McpGateway = McpGateway;
|