@dotlabo/agent-chat-relay 0.0.0-stage → 0.2.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/CHANGELOG.md +109 -0
- package/LICENSE +21 -0
- package/README.md +154 -2
- package/bin/agent-chat-relay +22 -0
- package/package.json +69 -4
- package/src/connectors/chatwork.ts +289 -0
- package/src/connectors/common.ts +106 -0
- package/src/connectors/discord.ts +335 -0
- package/src/connectors/http.ts +137 -0
- package/src/connectors/line-media.ts +63 -0
- package/src/connectors/line.ts +444 -0
- package/src/connectors/terminal.ts +367 -0
- package/src/core/access.ts +250 -0
- package/src/core/instructions.ts +21 -0
- package/src/core/mcp.ts +160 -0
- package/src/core/relay.ts +1063 -0
- package/src/core/retention.ts +44 -0
- package/src/harness/config.ts +137 -0
- package/src/harness/harness.ts +327 -0
- package/src/harness/log-file.ts +34 -0
- package/src/harness/pi/config.ts +51 -0
- package/src/harness/pi/state.ts +97 -0
- package/src/harness/pi/tool-approval.ts +128 -0
- package/src/harness/protect.ts +37 -0
- package/src/home.ts +67 -0
- package/src/jev.ts +201 -0
- package/src/main.ts +275 -0
- package/src/prompt.ts +12 -0
- package/src/serve-args.ts +56 -0
- package/src/shutdown.ts +52 -0
- package/src/terminal.ts +172 -0
- package/src/vendor/pi/adapter.js +3238 -0
- package/src/vendor/pi/managed-extension.ts +94 -0
- package/src/vendor/pi/managed-pi-rpc.ts +102 -0
- package/src/vendor/pi/managed-read.ts +99 -0
- package/src/vendor/pi/package.json +7 -0
- package/src/vendor/pi/pi-LICENSE +21 -0
- package/src/vendor/pi/pi-acp-LICENSE +21 -0
- package/src/version.ts +4 -0
- package/src/workspace.ts +117 -0
package/src/core/mcp.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// エージェントに渡すチャット用ツール(MCP)。ツールの名前と形は、公式の Claude Code Discord プラグインに揃えた。
|
|
2
|
+
// ハーネスとは 127.0.0.1 の HTTP でつなぎ、起動ごとに作る token で守る。
|
|
3
|
+
|
|
4
|
+
import { timingSafeEqual } from "node:crypto"
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
|
6
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js"
|
|
7
|
+
import { z } from "zod"
|
|
8
|
+
import { VERSION } from "../version.ts"
|
|
9
|
+
import type { Relay } from "./relay.ts"
|
|
10
|
+
|
|
11
|
+
type ToolResult = { content: { type: "text"; text: string }[]; isError?: boolean }
|
|
12
|
+
|
|
13
|
+
function ok(text: string): ToolResult {
|
|
14
|
+
return { content: [{ type: "text", text }] }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function run(name: string, op: () => Promise<string>): Promise<ToolResult> {
|
|
18
|
+
try {
|
|
19
|
+
return ok(await op())
|
|
20
|
+
} catch (err) {
|
|
21
|
+
return { content: [{ type: "text", text: `${name} failed: ${err instanceof Error ? err.message : String(err)}` }], isError: true }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function buildTools(relay: Relay, inboxDir: string): McpServer {
|
|
26
|
+
const server = new McpServer({ name: "chat", version: VERSION }, { instructions: relay.instructions() })
|
|
27
|
+
|
|
28
|
+
server.registerTool(
|
|
29
|
+
"reply",
|
|
30
|
+
{
|
|
31
|
+
description:
|
|
32
|
+
`チャットのルームへ送る。chat_id は届いた発言のもの、または list_rooms のもの。reply_to に message_id を渡すと、その発言を引用して返す(いちばん新しい発言への返事には必要ない)。files で、作業ディレクトリの中のファイルを添えられる。送れるものと本文の長さは、チャットサービス(chat_id の先頭)による:\n${relay.sendingGuide()}`,
|
|
33
|
+
inputSchema: {
|
|
34
|
+
chat_id: z.string(),
|
|
35
|
+
text: z.string().describe("本文。files があれば空でもよい"),
|
|
36
|
+
reply_to: z.string().optional().describe("引用して返す発言の message_id"),
|
|
37
|
+
files: z.array(z.string()).optional().describe("添えるファイルのパス(作業ディレクトリの中だけ。相対パスは作業ディレクトリから)"),
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
({ chat_id, text, reply_to, files }) =>
|
|
41
|
+
run("reply", async () => {
|
|
42
|
+
const ids = await relay.reply(chat_id, text, reply_to, files)
|
|
43
|
+
return ids.length <= 1 ? `sent (id: ${ids[0] ?? "-"})` : `sent ${ids.length} parts (ids: ${ids.join(", ")})`
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
server.registerTool(
|
|
48
|
+
"fetch_messages",
|
|
49
|
+
{
|
|
50
|
+
description: `ルームの最近の発言を、古い順に取得する。Bot に向けられていない発言も含む。${historyNote(relay)}`,
|
|
51
|
+
inputSchema: { chat_id: z.string(), limit: z.number().int().optional().describe("最大件数(既定 20、最大 100)") },
|
|
52
|
+
},
|
|
53
|
+
({ chat_id, limit }) =>
|
|
54
|
+
run("fetch_messages", async () => {
|
|
55
|
+
const msgs = await relay.fetchMessages(chat_id, limit)
|
|
56
|
+
if (msgs.length === 0) return "(no messages)"
|
|
57
|
+
return msgs
|
|
58
|
+
.map((m) => {
|
|
59
|
+
const who = m.fromMe ? "me" : (m.senderName ?? m.sender)
|
|
60
|
+
const atts = m.attachments.length > 0 ? ` +${m.attachments.length}att` : ""
|
|
61
|
+
// 改行で行を偽装されないよう、1 行にまとめる
|
|
62
|
+
return `[${new Date(m.ts).toISOString()}] ${who}: ${m.text.replace(/[\r\n]+/g, " ⏎ ")} (id: ${m.id}${atts})`
|
|
63
|
+
})
|
|
64
|
+
.join("\n")
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
server.registerTool(
|
|
69
|
+
"download_attachment",
|
|
70
|
+
{
|
|
71
|
+
description: "発言の添付(画像・動画・音声・ファイル)を取得し、読めるパスを返す。attachments 属性のある発言や、fetch_messages で +Natt と出た発言に使う。",
|
|
72
|
+
inputSchema: { chat_id: z.string(), message_id: z.string() },
|
|
73
|
+
},
|
|
74
|
+
({ chat_id, message_id }) =>
|
|
75
|
+
run("download_attachment", async () => {
|
|
76
|
+
const paths = await relay.download(chat_id, message_id, inboxDir)
|
|
77
|
+
return `downloaded ${paths.length} attachment(s):\n${paths.map((p) => ` ${p}`).join("\n")}`
|
|
78
|
+
}),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
server.registerTool(
|
|
82
|
+
"list_rooms",
|
|
83
|
+
{ description: "送れるルームと DM の一覧。", inputSchema: {} },
|
|
84
|
+
() => run("list_rooms", async () => JSON.stringify(await relay.listRooms(), null, 2)),
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
// 使えるサービスがなければ、ツールを出さない
|
|
88
|
+
const react = relay.servicesFor("react")
|
|
89
|
+
if (react.supported.length > 0) {
|
|
90
|
+
server.registerTool(
|
|
91
|
+
"react",
|
|
92
|
+
{ description: `発言に絵文字のリアクションを付ける。${onlyIn(react)}`, inputSchema: { chat_id: z.string(), message_id: z.string(), emoji: z.string() } },
|
|
93
|
+
({ chat_id, message_id, emoji }) =>
|
|
94
|
+
run("react", async () => {
|
|
95
|
+
await relay.react(chat_id, message_id, emoji)
|
|
96
|
+
return "reacted"
|
|
97
|
+
}),
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const edit = relay.servicesFor("edit")
|
|
102
|
+
if (edit.supported.length > 0) {
|
|
103
|
+
server.registerTool(
|
|
104
|
+
"edit_message",
|
|
105
|
+
{
|
|
106
|
+
description: `Bot が送った発言を編集する(途中経過の更新などに)。${onlyIn(edit)}編集では通知が鳴らないので、作業が終わったら新しく reply する。`,
|
|
107
|
+
inputSchema: { chat_id: z.string(), message_id: z.string(), text: z.string() },
|
|
108
|
+
},
|
|
109
|
+
({ chat_id, message_id, text }) =>
|
|
110
|
+
run("edit_message", async () => {
|
|
111
|
+
await relay.edit(chat_id, message_id, text)
|
|
112
|
+
return "edited"
|
|
113
|
+
}),
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const thread = relay.servicesFor("createThread")
|
|
118
|
+
if (thread.supported.length > 0) {
|
|
119
|
+
server.registerTool(
|
|
120
|
+
"create_thread",
|
|
121
|
+
{
|
|
122
|
+
description: `発言からスレッドを作り、スレッドの chat_id を返す。続けて reply でそこに書ける。${onlyIn(thread)}作れるのは、チャンネルの発言からだけ(スレッドの中や DM では作れない)。その発言にスレッドがあれば、その chat_id を返す。`,
|
|
123
|
+
inputSchema: {
|
|
124
|
+
chat_id: z.string(),
|
|
125
|
+
message_id: z.string().describe("スレッドを作る発言"),
|
|
126
|
+
name: z.string().describe("スレッドの名前"),
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
({ chat_id, message_id, name }) =>
|
|
130
|
+
run("create_thread", async () => `thread chat_id: ${await relay.createThread(chat_id, message_id, name)}`),
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return server
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 使えないサービスがあるときだけ、使えるサービスを書く */
|
|
138
|
+
function onlyIn(s: { supported: string[]; unsupported: string[] }): string {
|
|
139
|
+
return s.unsupported.length > 0 ? `使えるのは ${s.supported.join("、")} のルームだけ。` : ""
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function historyNote(relay: Relay): string {
|
|
143
|
+
const { unsupported } = relay.servicesFor("history")
|
|
144
|
+
return unsupported.length > 0 ? `${unsupported.join("、")} では、中継が起動してからの分だけ。` : ""
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** /mcp のエンドポイント。リクエストごとにツールの一式を作る(状態を持たない形) */
|
|
148
|
+
export function mcpHandler(relay: Relay, inboxDir: string, token: string): (req: Request) => Promise<Response> {
|
|
149
|
+
const expected = Buffer.from(`Bearer ${token}`)
|
|
150
|
+
return async (req) => {
|
|
151
|
+
const given = Buffer.from(req.headers.get("authorization") ?? "")
|
|
152
|
+
if (given.length !== expected.length || !timingSafeEqual(given, expected)) {
|
|
153
|
+
return new Response("unauthorized", { status: 401 })
|
|
154
|
+
}
|
|
155
|
+
const server = buildTools(relay, inboxDir)
|
|
156
|
+
const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true })
|
|
157
|
+
await server.connect(transport)
|
|
158
|
+
return transport.handleRequest(req)
|
|
159
|
+
}
|
|
160
|
+
}
|