omp-channel 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 +54 -0
- package/LICENSE +201 -0
- package/NOTICE +8 -0
- package/README.md +318 -2
- package/dist/adapter.d.ts +46 -0
- package/dist/adapter.js +18 -0
- package/dist/adapter.js.map +1 -0
- package/dist/config.d.ts +58 -0
- package/dist/config.js +140 -0
- package/dist/config.js.map +1 -0
- package/dist/dispatcher.d.ts +53 -0
- package/dist/dispatcher.js +242 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/extension.d.ts +27 -0
- package/dist/extension.js +151 -0
- package/dist/extension.js.map +1 -0
- package/dist/http-adapter.d.ts +20 -0
- package/dist/http-adapter.js +140 -0
- package/dist/http-adapter.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/module-adapter.d.ts +3 -0
- package/dist/module-adapter.js +21 -0
- package/dist/module-adapter.js.map +1 -0
- package/dist/protocol.d.ts +113 -0
- package/dist/protocol.js +222 -0
- package/dist/protocol.js.map +1 -0
- package/dist/redact.d.ts +11 -0
- package/dist/redact.js +65 -0
- package/dist/redact.js.map +1 -0
- package/dist/tool-schema.d.ts +13 -0
- package/dist/tool-schema.js +13 -0
- package/dist/tool-schema.js.map +1 -0
- package/dist/turn.d.ts +24 -0
- package/dist/turn.js +60 -0
- package/dist/turn.js.map +1 -0
- package/docs/protocol.md +334 -0
- package/package.json +76 -4
- package/schema/config.schema.json +159 -0
- package/schema/failure.schema.json +34 -0
- package/schema/inbound-event.schema.json +82 -0
- package/schema/outbound-reply.schema.json +34 -0
- package/schema/status.schema.json +22 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { ACKNOWLEDGED_OUTCOMES, Dispatcher } from "./dispatcher.js";
|
|
2
|
+
import { parseMcpNotification, parseReplyInput } from "./protocol.js";
|
|
3
|
+
import { jsonSchemaParameters } from "./tool-schema.js";
|
|
4
|
+
import { failedRun } from "./turn.js";
|
|
5
|
+
export const REPLY_TOOL_NAME = "reply";
|
|
6
|
+
/**
|
|
7
|
+
* Creates the extension factory. It registers:
|
|
8
|
+
* - `mcp_notification`: messages pushed by the configured MCP server enter the agent;
|
|
9
|
+
* - the `reply` tool: the model answers a message by its messageId;
|
|
10
|
+
* - lifecycle hooks: agent state and failed turns are reported to the service, and the adapter
|
|
11
|
+
* stops on shutdown;
|
|
12
|
+
* - the adapter receive loop, when an adapter is given.
|
|
13
|
+
*/
|
|
14
|
+
export function createChannelExtension(options) {
|
|
15
|
+
return (pi) => {
|
|
16
|
+
const { config } = options;
|
|
17
|
+
const adapter = options.adapter;
|
|
18
|
+
const log = options.log ?? (() => undefined);
|
|
19
|
+
let context;
|
|
20
|
+
const session = {
|
|
21
|
+
send: (content, sendOptions) => pi.sendUserMessage(content, sendOptions),
|
|
22
|
+
abort: () => context?.abort(),
|
|
23
|
+
isIdle: () => context?.isIdle() ?? true,
|
|
24
|
+
};
|
|
25
|
+
const dispatcher = new Dispatcher({ config, session, adapter, log });
|
|
26
|
+
pi.on("session_start", (_event, ctx) => {
|
|
27
|
+
context = ctx;
|
|
28
|
+
});
|
|
29
|
+
pi.on("mcp_notification", (event, ctx) => {
|
|
30
|
+
context = ctx;
|
|
31
|
+
const parsed = parseMcpNotification(event, config.mcpServer, config.limits);
|
|
32
|
+
if (parsed === null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!parsed.ok) {
|
|
36
|
+
log(`dropped invalid MCP channel notification: ${parsed.reason}`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
void dispatcher.submit(parsed.value).then((outcome) => {
|
|
40
|
+
if (!ACKNOWLEDGED_OUTCOMES.has(outcome)) {
|
|
41
|
+
log(`MCP channel event not admitted: ${outcome}`);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
46
|
+
context = ctx;
|
|
47
|
+
dispatcher.setStatus("working");
|
|
48
|
+
});
|
|
49
|
+
pi.on("agent_end", (event, ctx) => {
|
|
50
|
+
context = ctx;
|
|
51
|
+
// A run that Oh My Pi continues (a retry, for example) has not ended: still working.
|
|
52
|
+
if (event.willContinue === true) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const failed = failedRun(event);
|
|
56
|
+
if (failed !== undefined) {
|
|
57
|
+
void dispatcher.reportFailure(failed.messageIds, failed.reason);
|
|
58
|
+
}
|
|
59
|
+
dispatcher.setStatus("free");
|
|
60
|
+
});
|
|
61
|
+
if (adapter !== undefined) {
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
pi.on("session_shutdown", () => {
|
|
64
|
+
controller.abort();
|
|
65
|
+
});
|
|
66
|
+
void runReceiveLoop(adapter, dispatcher, controller.signal, log);
|
|
67
|
+
}
|
|
68
|
+
pi.registerTool({
|
|
69
|
+
name: REPLY_TOOL_NAME,
|
|
70
|
+
label: "Reply to service",
|
|
71
|
+
description: "Send a reply to an external message. Pass the messageId shown with that message. Only messageIds received in this session are accepted.",
|
|
72
|
+
parameters: jsonSchemaParameters({
|
|
73
|
+
type: "object",
|
|
74
|
+
properties: {
|
|
75
|
+
messageId: { type: "string", minLength: 1, maxLength: 128 },
|
|
76
|
+
text: { type: "string", minLength: 1, maxLength: config.limits.maxTextLength },
|
|
77
|
+
},
|
|
78
|
+
required: ["messageId", "text"],
|
|
79
|
+
additionalProperties: false,
|
|
80
|
+
}),
|
|
81
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
82
|
+
context = ctx;
|
|
83
|
+
const parsed = parseReplyInput(params, config.limits);
|
|
84
|
+
if (!parsed.ok) {
|
|
85
|
+
return textResult(`Reply rejected: ${parsed.reason}.`);
|
|
86
|
+
}
|
|
87
|
+
const result = await dispatcher.reply(parsed.value.messageId, parsed.value.text);
|
|
88
|
+
return textResult(result.ok ? "Reply sent." : `Reply not sent: ${result.reason}.`);
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
pi.registerCommand(STATUS_COMMAND_NAME, {
|
|
92
|
+
description: "Show how the channel is connected and what its adapter reports",
|
|
93
|
+
handler: async (_args, ctx) => {
|
|
94
|
+
ctx.ui.notify(await channelStatusText(config, adapter, log), "info");
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Name of the command that shows the channel state (`/channel-status`). */
|
|
100
|
+
export const STATUS_COMMAND_NAME = "channel-status";
|
|
101
|
+
/**
|
|
102
|
+
* The text of `/channel-status`: the transport in use, then the adapter's own description when it
|
|
103
|
+
* has one. A failing description is reported in one line and never shown raw.
|
|
104
|
+
*/
|
|
105
|
+
export async function channelStatusText(config, adapter, log) {
|
|
106
|
+
const transport = config.service !== undefined
|
|
107
|
+
? "HTTP service"
|
|
108
|
+
: adapter !== undefined
|
|
109
|
+
? "adapter module"
|
|
110
|
+
: "MCP push only (replies are not configured)";
|
|
111
|
+
const lines = [`Channel: ${transport}, MCP server ${config.mcpServer}.`];
|
|
112
|
+
if (adapter?.describe !== undefined) {
|
|
113
|
+
try {
|
|
114
|
+
lines.push(await adapter.describe());
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
log("the adapter could not describe its state");
|
|
118
|
+
lines.push("The adapter could not report its state.");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return lines.join("\n");
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Takes deliveries from the adapter one at a time. A delivery is acknowledged only after the agent
|
|
125
|
+
* admitted it, so a crash before that point leads to redelivery rather than loss.
|
|
126
|
+
*/
|
|
127
|
+
async function runReceiveLoop(adapter, dispatcher, signal, log) {
|
|
128
|
+
try {
|
|
129
|
+
for await (const delivery of adapter.receive(signal)) {
|
|
130
|
+
const outcome = await dispatcher.submit(delivery.event, delivery.deliveryId);
|
|
131
|
+
if (!ACKNOWLEDGED_OUTCOMES.has(outcome)) {
|
|
132
|
+
log(`delivery not admitted (${outcome}); the service will deliver it again`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (delivery.deliveryId !== undefined && adapter.ack !== undefined) {
|
|
136
|
+
await adapter.ack(delivery.deliveryId).catch(() => {
|
|
137
|
+
log("could not acknowledge a delivery; the service may deliver it again");
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (!signal.aborted) {
|
|
144
|
+
log(`receive loop stopped (${error instanceof Error ? error.name : "unknown"})`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function textResult(text) {
|
|
149
|
+
return { content: [{ type: "text", text }] };
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAoB,MAAM,iBAAiB,CAAC;AACtF,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC;AAUvC;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAgC;IACrE,OAAO,CAAC,EAAgB,EAAQ,EAAE;QAChC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAChC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC7C,IAAI,OAAqC,CAAC;QAE1C,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C;gBACnE,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;oBAChD,GAAG,CAAC,oEAAoE,CAAC,CAAC;gBAC5E,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,GAAG,CAAC,yBAAyB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC;QACnF,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC9B,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ChannelAdapter } from "./adapter.js";
|
|
2
|
+
import type { PollConfig, ServiceConfig } from "./config.js";
|
|
3
|
+
import { type Limits } from "./protocol.js";
|
|
4
|
+
export interface HttpAdapterOptions {
|
|
5
|
+
readonly service: ServiceConfig;
|
|
6
|
+
readonly poll: PollConfig;
|
|
7
|
+
readonly limits: Limits;
|
|
8
|
+
readonly log: (line: string) => void;
|
|
9
|
+
/** Injected for tests. Defaults to the global fetch. */
|
|
10
|
+
readonly fetch?: typeof fetch;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Transport over plain HTTP with leases (see docs/protocol.md, "HTTP transport"):
|
|
14
|
+
* - GET paths.messages returns { deliveries: [{ deliveryId, leaseExpiresAt, event }] } in order;
|
|
15
|
+
* - POST paths.ack with { deliveryIds: [...] } after the agent admitted a delivery;
|
|
16
|
+
* - POST paths.replies with a reply; POST paths.status with an agent status;
|
|
17
|
+
* - POST paths.failures when a turn ended in an error and a message got no answer.
|
|
18
|
+
* A delivery that is not acknowledged before its lease ends is delivered again by the service.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createHttpAdapter(options: HttpAdapterOptions): ChannelAdapter;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { AdapterError } from "./adapter.js";
|
|
2
|
+
import { parseInboundEvent, } from "./protocol.js";
|
|
3
|
+
const DELIVERY_ID_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
4
|
+
/**
|
|
5
|
+
* Transport over plain HTTP with leases (see docs/protocol.md, "HTTP transport"):
|
|
6
|
+
* - GET paths.messages returns { deliveries: [{ deliveryId, leaseExpiresAt, event }] } in order;
|
|
7
|
+
* - POST paths.ack with { deliveryIds: [...] } after the agent admitted a delivery;
|
|
8
|
+
* - POST paths.replies with a reply; POST paths.status with an agent status;
|
|
9
|
+
* - POST paths.failures when a turn ended in an error and a message got no answer.
|
|
10
|
+
* A delivery that is not acknowledged before its lease ends is delivered again by the service.
|
|
11
|
+
*/
|
|
12
|
+
export function createHttpAdapter(options) {
|
|
13
|
+
const { service, poll, limits, log } = options;
|
|
14
|
+
const fetchFn = options.fetch ?? fetch;
|
|
15
|
+
const request = async (method, path, body, signal) => {
|
|
16
|
+
const url = new URL(`${service.url.pathname.replace(/\/+$/, "")}${path}`, service.url.origin);
|
|
17
|
+
const timeout = AbortSignal.timeout(service.requestTimeoutMs);
|
|
18
|
+
const headers = {
|
|
19
|
+
authorization: `Bearer ${service.key}`,
|
|
20
|
+
accept: "application/json",
|
|
21
|
+
};
|
|
22
|
+
if (body !== undefined) {
|
|
23
|
+
headers["content-type"] = "application/json";
|
|
24
|
+
}
|
|
25
|
+
const requestSignal = signal === undefined ? timeout : AbortSignal.any([timeout, signal]);
|
|
26
|
+
let response;
|
|
27
|
+
try {
|
|
28
|
+
response = await fetchFn(url, {
|
|
29
|
+
method,
|
|
30
|
+
headers,
|
|
31
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
32
|
+
signal: requestSignal,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (timeout.aborted) {
|
|
37
|
+
throw new AdapterError("the request timed out");
|
|
38
|
+
}
|
|
39
|
+
if (signal?.aborted) {
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
throw new AdapterError(`the request failed (${error instanceof Error ? error.name : "unknown"})`);
|
|
43
|
+
}
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
throw new AdapterError(`the service answered with HTTP ${response.status}`);
|
|
46
|
+
}
|
|
47
|
+
return response;
|
|
48
|
+
};
|
|
49
|
+
const ack = async (deliveryId) => {
|
|
50
|
+
await request("POST", service.paths.ack, { deliveryIds: [deliveryId] }, undefined);
|
|
51
|
+
};
|
|
52
|
+
const poll_ = async (signal) => {
|
|
53
|
+
const response = await request("GET", `${service.paths.messages}?limit=${poll.batchSize}`, undefined, signal);
|
|
54
|
+
const body = await response.json().catch(() => undefined);
|
|
55
|
+
if (typeof body !== "object" ||
|
|
56
|
+
body === null ||
|
|
57
|
+
!Array.isArray(body.deliveries)) {
|
|
58
|
+
throw new AdapterError("the service returned an unexpected response");
|
|
59
|
+
}
|
|
60
|
+
const deliveries = [];
|
|
61
|
+
for (const item of body.deliveries) {
|
|
62
|
+
const deliveryId = typeof item === "object" && item !== null
|
|
63
|
+
? item.deliveryId
|
|
64
|
+
: undefined;
|
|
65
|
+
if (typeof deliveryId !== "string" || !DELIVERY_ID_PATTERN.test(deliveryId)) {
|
|
66
|
+
log("discarded a delivery without a valid deliveryId");
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const parsed = parseInboundEvent(item.event, limits);
|
|
70
|
+
if (!parsed.ok) {
|
|
71
|
+
// An invalid delivery is acknowledged and dropped, so it cannot block the queue forever.
|
|
72
|
+
log(`discarded invalid delivery ${deliveryId}: ${parsed.reason}`);
|
|
73
|
+
await ack(deliveryId).catch(() => log(`could not acknowledge delivery ${deliveryId}`));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
deliveries.push({ deliveryId, event: parsed.value });
|
|
77
|
+
}
|
|
78
|
+
return deliveries;
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
async *receive(signal) {
|
|
82
|
+
let failures = 0;
|
|
83
|
+
while (!signal.aborted) {
|
|
84
|
+
let batch;
|
|
85
|
+
try {
|
|
86
|
+
batch = await poll_(signal);
|
|
87
|
+
failures = 0;
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (signal.aborted) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
failures += 1;
|
|
94
|
+
const wait = Math.min(poll.intervalMs * 2 ** Math.min(failures, 16), poll.maxBackoffMs);
|
|
95
|
+
log(`polling failed (${error instanceof Error ? error.name : "unknown"}), retrying in ${wait} ms`);
|
|
96
|
+
await sleep(wait, signal);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (batch.length === 0) {
|
|
100
|
+
await sleep(poll.intervalMs, signal);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
for (const delivery of batch) {
|
|
104
|
+
if (signal.aborted) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
yield delivery;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
async reply(reply) {
|
|
112
|
+
await request("POST", service.paths.replies, reply, undefined);
|
|
113
|
+
},
|
|
114
|
+
async ack(deliveryId) {
|
|
115
|
+
await ack(deliveryId);
|
|
116
|
+
},
|
|
117
|
+
async status(status) {
|
|
118
|
+
await request("POST", service.paths.status, status, undefined);
|
|
119
|
+
},
|
|
120
|
+
async failure(failure) {
|
|
121
|
+
await request("POST", service.paths.failures, failure, undefined);
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function sleep(ms, signal) {
|
|
126
|
+
return new Promise((resolve) => {
|
|
127
|
+
if (signal.aborted) {
|
|
128
|
+
resolve();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const timer = setTimeout(done, ms);
|
|
132
|
+
signal.addEventListener("abort", done, { once: true });
|
|
133
|
+
function done() {
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
signal.removeEventListener("abort", done);
|
|
136
|
+
resolve();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=http-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-adapter.js","sourceRoot":"","sources":["../src/http-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsC,MAAM,cAAc,CAAC;AAEhF,OAAO,EAML,iBAAiB,GAClB,MAAM,eAAe,CAAC;AAWvB,MAAM,mBAAmB,GAAG,0BAA0B,CAAC;AAEvD;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA2B;IAC3D,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAC/C,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC;IAEvC,MAAM,OAAO,GAAG,KAAK,EACnB,MAAsB,EACtB,IAAY,EACZ,IAAa,EACb,MAA+B,EACZ,EAAE;QACrB,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC9F,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QAC9D,MAAM,OAAO,GAA2B;YACtC,aAAa,EAAE,UAAU,OAAO,CAAC,GAAG,EAAE;YACtC,MAAM,EAAE,kBAAkB;SAC3B,CAAC;QACF,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,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AAC;AACL,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types";
|
|
2
|
+
export { type ChannelAdapter, type Delivery, AdapterError, isChannelAdapter } from "./adapter.js";
|
|
3
|
+
export { type ChannelConfig, ConfigError, loadConfig, parseServiceUrl } from "./config.js";
|
|
4
|
+
export { createChannelExtension, REPLY_TOOL_NAME } from "./extension.js";
|
|
5
|
+
export { createHttpAdapter } from "./http-adapter.js";
|
|
6
|
+
export { DEFAULT_LIMITS, MCP_NOTIFICATION_METHOD, PROTOCOL_VERSION, formatForSession, parseInboundEvent, parseMcpNotification, parseReplyInput, type InboundEvent, type MessageEvent, type OutboundReply, type OutboundStatus, } from "./protocol.js";
|
|
7
|
+
/**
|
|
8
|
+
* Entry point loaded by Oh My Pi. A configuration problem disables the channel and is written to
|
|
9
|
+
* stderr (stdout belongs to the terminal interface). The message never contains a key.
|
|
10
|
+
*/
|
|
11
|
+
export default function ompChannel(pi: ExtensionAPI): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { ConfigError, loadConfig } from "./config.js";
|
|
3
|
+
import { createChannelExtension } from "./extension.js";
|
|
4
|
+
import { createHttpAdapter } from "./http-adapter.js";
|
|
5
|
+
import { loadAdapterModule } from "./module-adapter.js";
|
|
6
|
+
export { AdapterError, isChannelAdapter } from "./adapter.js";
|
|
7
|
+
export { ConfigError, loadConfig, parseServiceUrl } from "./config.js";
|
|
8
|
+
export { createChannelExtension, REPLY_TOOL_NAME } from "./extension.js";
|
|
9
|
+
export { createHttpAdapter } from "./http-adapter.js";
|
|
10
|
+
export { DEFAULT_LIMITS, MCP_NOTIFICATION_METHOD, PROTOCOL_VERSION, formatForSession, parseInboundEvent, parseMcpNotification, parseReplyInput, } from "./protocol.js";
|
|
11
|
+
const readText = (path) => {
|
|
12
|
+
try {
|
|
13
|
+
return readFileSync(path, "utf8");
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Entry point loaded by Oh My Pi. A configuration problem disables the channel and is written to
|
|
24
|
+
* stderr (stdout belongs to the terminal interface). The message never contains a key.
|
|
25
|
+
*/
|
|
26
|
+
export default async function ompChannel(pi) {
|
|
27
|
+
const log = (line) => {
|
|
28
|
+
process.stderr.write(`[omp-channel] ${line}\n`);
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
const cwd = process.cwd();
|
|
32
|
+
const config = loadConfig({ env: process.env, cwd, readText });
|
|
33
|
+
const adapter = config.adapterModule !== undefined
|
|
34
|
+
? await loadAdapterModule(config.adapterModule, cwd)
|
|
35
|
+
: config.service !== undefined
|
|
36
|
+
? createHttpAdapter({
|
|
37
|
+
service: config.service,
|
|
38
|
+
poll: config.poll,
|
|
39
|
+
limits: config.limits,
|
|
40
|
+
log,
|
|
41
|
+
})
|
|
42
|
+
: undefined;
|
|
43
|
+
createChannelExtension({ config, adapter, log })(pi);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
const reason = error instanceof ConfigError ? error.message : "unexpected error";
|
|
47
|
+
log(`disabled: ${reason}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,WAAW,EAAiB,UAAU,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,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AAQ,EAAE,CAAC,CAAC;QAC/D,MAAM,OAAO,GACX,MAAM,CAAC,aAAa,KAAK,SAAS;YAChC,CAAC,CAAC,MAAM,iBAAiB,CAAC,MAAM,CAAC,aAAa,EAAE,GAAG,CAAC;YACpD,CAAC,CAAC,MAAM,CAAC,OAAO,KAAK,SAAS;gBAC5B,CAAC,CAAC,iBAAiB,CAAC;oBAChB,OAAO,EAAE,MAAM,CAAC,OAAO;oBACvB,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,MAAM,EAAE,MAAM,CAAC,MAAM;oBACrB,GAAG;iBACJ,CAAC;gBACJ,CAAC,CAAC,SAAS,CAAC;QAClB,sBAAsB,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IACvD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,GAAG,KAAK,YAAY,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB,CAAC;QACjF,GAAG,CAAC,aAAa,MAAM,EAAE,CAAC,CAAC;IAC7B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { isChannelAdapter } from "./adapter.js";
|
|
4
|
+
import { ConfigError } from "./config.js";
|
|
5
|
+
/** Loads a ChannelAdapter from a JavaScript module (default export, or named `adapter`). */
|
|
6
|
+
export async function loadAdapterModule(specifier, cwd) {
|
|
7
|
+
const href = pathToFileURL(resolve(cwd, specifier)).href;
|
|
8
|
+
let loaded;
|
|
9
|
+
try {
|
|
10
|
+
loaded = (await import(href));
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
throw new ConfigError(`adapterModule cannot be loaded (${error instanceof Error ? error.name : "unknown"})`);
|
|
14
|
+
}
|
|
15
|
+
const candidate = loaded.default ?? loaded.adapter;
|
|
16
|
+
if (!isChannelAdapter(candidate)) {
|
|
17
|
+
throw new ConfigError("adapterModule must export a ChannelAdapter as default export");
|
|
18
|
+
}
|
|
19
|
+
return candidate;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=module-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-adapter.js","sourceRoot":"","sources":["../src/module-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAuB,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,4FAA4F;AAC5F,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,SAAiB,EAAE,GAAW;IACpE,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;IACzD,IAAI,MAA+B,CAAC;IACpC,IAAI,CAAC;QACH,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,IAAI,CAAC,CAA4B,CAAC;IAC3D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,WAAW,CACnB,mCAAmC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,GAAG,CACtF,CAAC;IACJ,CAAC;IACD,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC;IACnD,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,WAAW,CAAC,8DAA8D,CAAC,CAAC;IACxF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration contract: the messages a service and omp-channel exchange.
|
|
3
|
+
*
|
|
4
|
+
* Pure module: no I/O and no Oh My Pi imports. Every value that arrives from a service or an MCP
|
|
5
|
+
* server is untrusted and is validated here. The same contract is published as JSON Schema in
|
|
6
|
+
* schema/ and described in docs/protocol.md. Change both together.
|
|
7
|
+
*/
|
|
8
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
9
|
+
/** JSON-RPC notification method used by the MCP transport. */
|
|
10
|
+
export declare const MCP_NOTIFICATION_METHOD = "notifications/omp/channel";
|
|
11
|
+
export interface Limits {
|
|
12
|
+
readonly maxTextLength: number;
|
|
13
|
+
readonly maxSenderLength: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const DEFAULT_LIMITS: Limits;
|
|
16
|
+
export declare const MAX_ID_LENGTH = 128;
|
|
17
|
+
export type Attribution = "user" | "agent";
|
|
18
|
+
export type AgentStatus = "working" | "free";
|
|
19
|
+
/** A message typed by a person or sent by another agent, to be given to the agent. */
|
|
20
|
+
export interface MessageEvent {
|
|
21
|
+
readonly type: "message";
|
|
22
|
+
readonly conversationId: string;
|
|
23
|
+
readonly messageId: string;
|
|
24
|
+
readonly sender: string;
|
|
25
|
+
readonly text: string;
|
|
26
|
+
readonly sentAt?: string;
|
|
27
|
+
/** messageId of an earlier message this one answers, when the service sets it. */
|
|
28
|
+
readonly replyTo?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Who wrote the message. Required: the value is passed to Oh My Pi unchanged. Which value
|
|
31
|
+
* Oh My Pi needs for each kind of message is an open point (docs/protocol.md, section 3.1).
|
|
32
|
+
*/
|
|
33
|
+
readonly attribution: Attribution;
|
|
34
|
+
}
|
|
35
|
+
/** Stops the agent's current turn. Tool results already produced are kept. */
|
|
36
|
+
export interface InterruptEvent {
|
|
37
|
+
readonly type: "interrupt";
|
|
38
|
+
readonly conversationId: string;
|
|
39
|
+
}
|
|
40
|
+
/** Ends the open conversation so that another one can start. */
|
|
41
|
+
export interface CloseEvent {
|
|
42
|
+
readonly type: "close";
|
|
43
|
+
readonly conversationId: string;
|
|
44
|
+
}
|
|
45
|
+
export type InboundEvent = MessageEvent | InterruptEvent | CloseEvent;
|
|
46
|
+
/** Outbound reply, sent to the service. `replyTo` is the messageId being answered. */
|
|
47
|
+
export interface OutboundReply {
|
|
48
|
+
readonly type: "reply";
|
|
49
|
+
readonly conversationId: string;
|
|
50
|
+
/** Unique id of this reply, so a retried POST can be recognized. */
|
|
51
|
+
readonly messageId: string;
|
|
52
|
+
readonly replyTo: string;
|
|
53
|
+
readonly text: string;
|
|
54
|
+
readonly at: string;
|
|
55
|
+
}
|
|
56
|
+
/** Agent state for one conversation, reported to the service. */
|
|
57
|
+
export interface OutboundStatus {
|
|
58
|
+
readonly type: "status";
|
|
59
|
+
readonly conversationId: string;
|
|
60
|
+
readonly status: AgentStatus;
|
|
61
|
+
readonly at: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A turn ended in an error and a message got no answer. `reason` is the cleaned error text, absent
|
|
65
|
+
* when it is turned off or there is none.
|
|
66
|
+
*/
|
|
67
|
+
export interface OutboundFailure {
|
|
68
|
+
readonly type: "failure";
|
|
69
|
+
readonly conversationId: string;
|
|
70
|
+
/** Unique id of this report, so a retried POST can be recognized. */
|
|
71
|
+
readonly messageId: string;
|
|
72
|
+
/** The inbound message that got no answer. */
|
|
73
|
+
readonly replyTo: string;
|
|
74
|
+
readonly reason?: string;
|
|
75
|
+
readonly at: string;
|
|
76
|
+
}
|
|
77
|
+
export type ParseResult<T> = {
|
|
78
|
+
ok: true;
|
|
79
|
+
value: T;
|
|
80
|
+
} | {
|
|
81
|
+
ok: false;
|
|
82
|
+
reason: string;
|
|
83
|
+
};
|
|
84
|
+
export declare function parseInboundEvent(raw: unknown, limits?: Limits): ParseResult<InboundEvent>;
|
|
85
|
+
/**
|
|
86
|
+
* Reads an MCP notification. Returns null when it is not for this channel: another server or
|
|
87
|
+
* another method. Returns a failed result when it is for this channel but invalid.
|
|
88
|
+
*/
|
|
89
|
+
export declare function parseMcpNotification(notification: {
|
|
90
|
+
server: string;
|
|
91
|
+
method: string;
|
|
92
|
+
params: unknown;
|
|
93
|
+
}, expectedServer: string, limits?: Limits): ParseResult<InboundEvent> | null;
|
|
94
|
+
export declare function parseReplyInput(raw: unknown, limits?: Limits): ParseResult<{
|
|
95
|
+
messageId: string;
|
|
96
|
+
text: string;
|
|
97
|
+
}>;
|
|
98
|
+
export declare function buildReply(conversationId: string, replyTo: string, text: string, reply: {
|
|
99
|
+
messageId: string;
|
|
100
|
+
at: Date;
|
|
101
|
+
}): OutboundReply;
|
|
102
|
+
export declare function buildFailure(conversationId: string, replyTo: string, reason: string | undefined, report: {
|
|
103
|
+
messageId: string;
|
|
104
|
+
at: Date;
|
|
105
|
+
}): OutboundFailure;
|
|
106
|
+
export declare function buildStatus(conversationId: string, status: AgentStatus, at: Date): OutboundStatus;
|
|
107
|
+
/** The message ids of the envelopes in a text handed to the agent (see `formatForSession`). */
|
|
108
|
+
export declare function messageIdsIn(text: string): string[];
|
|
109
|
+
/**
|
|
110
|
+
* Text handed to the agent for a message. It is marked as data written by an outside person.
|
|
111
|
+
* Angle brackets in the sender and the body are escaped, so the body cannot close the envelope.
|
|
112
|
+
*/
|
|
113
|
+
export declare function formatForSession(message: MessageEvent): string;
|