omp-channel 0.1.0 → 0.2.1
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 +31 -0
- package/README.md +44 -33
- package/dist/adapter.d.ts +6 -1
- package/dist/adapter.js.map +1 -1
- package/dist/config.d.ts +19 -6
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -1
- package/dist/dispatcher.d.ts +21 -0
- package/dist/dispatcher.js +147 -1
- package/dist/dispatcher.js.map +1 -1
- package/dist/extension.d.ts +4 -1
- package/dist/extension.js +38 -6
- package/dist/extension.js.map +1 -1
- package/dist/http-adapter.d.ts +2 -1
- package/dist/http-adapter.js +5 -1
- package/dist/http-adapter.js.map +1 -1
- package/dist/protocol.d.ts +20 -0
- package/dist/protocol.js +15 -0
- package/dist/protocol.js.map +1 -1
- package/dist/redact.d.ts +16 -0
- package/dist/redact.js +79 -0
- package/dist/redact.js.map +1 -0
- package/dist/turn.d.ts +37 -0
- package/dist/turn.js +79 -0
- package/dist/turn.js.map +1 -0
- package/docs/protocol.md +75 -9
- package/package.json +3 -3
- package/schema/config.schema.json +31 -0
- package/schema/failure.schema.json +34 -0
package/dist/extension.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { ACKNOWLEDGED_OUTCOMES, Dispatcher } from "./dispatcher.js";
|
|
1
|
+
import { ACKNOWLEDGED_OUTCOMES, CLOSED_REASON, Dispatcher, SWITCHED_REASON, } from "./dispatcher.js";
|
|
2
2
|
import { parseMcpNotification, parseReplyInput } from "./protocol.js";
|
|
3
3
|
import { jsonSchemaParameters } from "./tool-schema.js";
|
|
4
|
+
import { segmentsOf } from "./turn.js";
|
|
4
5
|
export const REPLY_TOOL_NAME = "reply";
|
|
5
6
|
/**
|
|
6
7
|
* Creates the extension factory. It registers:
|
|
7
8
|
* - `mcp_notification`: messages pushed by the configured MCP server enter the agent;
|
|
8
9
|
* - the `reply` tool: the model answers a message by its messageId;
|
|
9
|
-
* - lifecycle hooks: agent state is reported
|
|
10
|
+
* - lifecycle hooks: the agent state is reported, no message is left without an answer or a
|
|
11
|
+
* report when a run ends or the session goes away, and the adapter stops on shutdown;
|
|
10
12
|
* - the adapter receive loop, when an adapter is given.
|
|
11
13
|
*/
|
|
12
14
|
export function createChannelExtension(options) {
|
|
@@ -20,7 +22,15 @@ export function createChannelExtension(options) {
|
|
|
20
22
|
abort: () => context?.abort(),
|
|
21
23
|
isIdle: () => context?.isIdle() ?? true,
|
|
22
24
|
};
|
|
23
|
-
const dispatcher = new Dispatcher({
|
|
25
|
+
const dispatcher = new Dispatcher({
|
|
26
|
+
config,
|
|
27
|
+
session,
|
|
28
|
+
adapter,
|
|
29
|
+
log,
|
|
30
|
+
...(options.wait === undefined ? {} : { wait: options.wait }),
|
|
31
|
+
});
|
|
32
|
+
// Counts the runs, so the end of an old one never marks free a newer one.
|
|
33
|
+
let runs = 0;
|
|
24
34
|
pi.on("session_start", (_event, ctx) => {
|
|
25
35
|
context = ctx;
|
|
26
36
|
});
|
|
@@ -42,15 +52,35 @@ export function createChannelExtension(options) {
|
|
|
42
52
|
});
|
|
43
53
|
pi.on("agent_start", (_event, ctx) => {
|
|
44
54
|
context = ctx;
|
|
55
|
+
runs += 1;
|
|
45
56
|
dispatcher.setStatus("working");
|
|
46
57
|
});
|
|
47
|
-
pi.on("agent_end", (
|
|
58
|
+
pi.on("agent_end", (event, ctx) => {
|
|
59
|
+
context = ctx;
|
|
60
|
+
// A run that Oh My Pi continues (a retry, for example) has not ended: still working.
|
|
61
|
+
if (event.willContinue === true) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const run = runs;
|
|
65
|
+
// The answers go first: a service that hears "free" has already received them.
|
|
66
|
+
void dispatcher.settle(segmentsOf(event)).finally(() => {
|
|
67
|
+
if (run === runs) {
|
|
68
|
+
dispatcher.setStatus("free");
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
// A new, resumed or forked session no longer holds the messages that were waiting.
|
|
73
|
+
pi.on("session_switch", async (_event, ctx) => {
|
|
48
74
|
context = ctx;
|
|
49
|
-
dispatcher.
|
|
75
|
+
await dispatcher.abandon(SWITCHED_REASON);
|
|
50
76
|
});
|
|
51
77
|
if (adapter !== undefined) {
|
|
52
78
|
const controller = new AbortController();
|
|
53
|
-
pi.on("session_shutdown", () => {
|
|
79
|
+
pi.on("session_shutdown", async () => {
|
|
80
|
+
// Oh My Pi waits a short time for this handler: report first, then stop the transport.
|
|
81
|
+
if (dispatcher.hasOpenMessages) {
|
|
82
|
+
await dispatcher.abandon(CLOSED_REASON);
|
|
83
|
+
}
|
|
54
84
|
controller.abort();
|
|
55
85
|
});
|
|
56
86
|
void runReceiveLoop(adapter, dispatcher, controller.signal, log);
|
|
@@ -58,6 +88,8 @@ export function createChannelExtension(options) {
|
|
|
58
88
|
pi.registerTool({
|
|
59
89
|
name: REPLY_TOOL_NAME,
|
|
60
90
|
label: "Reply to service",
|
|
91
|
+
// Without this Oh My Pi leaves an extension tool out of the tools it sends to the model.
|
|
92
|
+
loadMode: "essential",
|
|
61
93
|
description: "Send a reply to an external message. Pass the messageId shown with that message. Only messageIds received in this session are accepted.",
|
|
62
94
|
parameters: jsonSchemaParameters({
|
|
63
95
|
type: "object",
|
package/dist/extension.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAQA,OAAO,
|
|
1
|
+
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,qBAAqB,EACrB,aAAa,EACb,UAAU,EACV,eAAe,GAEhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MA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C;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"}
|
package/dist/http-adapter.d.ts
CHANGED
|
@@ -13,7 +13,8 @@ export interface HttpAdapterOptions {
|
|
|
13
13
|
* Transport over plain HTTP with leases (see docs/protocol.md, "HTTP transport"):
|
|
14
14
|
* - GET paths.messages returns { deliveries: [{ deliveryId, leaseExpiresAt, event }] } in order;
|
|
15
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
|
|
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.
|
|
17
18
|
* A delivery that is not acknowledged before its lease ends is delivered again by the service.
|
|
18
19
|
*/
|
|
19
20
|
export declare function createHttpAdapter(options: HttpAdapterOptions): ChannelAdapter;
|
package/dist/http-adapter.js
CHANGED
|
@@ -5,7 +5,8 @@ const DELIVERY_ID_PATTERN = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
|
5
5
|
* Transport over plain HTTP with leases (see docs/protocol.md, "HTTP transport"):
|
|
6
6
|
* - GET paths.messages returns { deliveries: [{ deliveryId, leaseExpiresAt, event }] } in order;
|
|
7
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
|
|
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.
|
|
9
10
|
* A delivery that is not acknowledged before its lease ends is delivered again by the service.
|
|
10
11
|
*/
|
|
11
12
|
export function createHttpAdapter(options) {
|
|
@@ -116,6 +117,9 @@ export function createHttpAdapter(options) {
|
|
|
116
117
|
async status(status) {
|
|
117
118
|
await request("POST", service.paths.status, status, undefined);
|
|
118
119
|
},
|
|
120
|
+
async failure(failure) {
|
|
121
|
+
await request("POST", service.paths.failures, failure, undefined);
|
|
122
|
+
},
|
|
119
123
|
};
|
|
120
124
|
}
|
|
121
125
|
function sleep(ms, signal) {
|
package/dist/http-adapter.js.map
CHANGED
|
@@ -1 +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,
|
|
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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ACnC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,SAAS,IAAI;YACX,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1C,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/protocol.d.ts
CHANGED
|
@@ -60,6 +60,20 @@ export interface OutboundStatus {
|
|
|
60
60
|
readonly status: AgentStatus;
|
|
61
61
|
readonly at: string;
|
|
62
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
|
+
}
|
|
63
77
|
export type ParseResult<T> = {
|
|
64
78
|
ok: true;
|
|
65
79
|
value: T;
|
|
@@ -85,7 +99,13 @@ export declare function buildReply(conversationId: string, replyTo: string, text
|
|
|
85
99
|
messageId: string;
|
|
86
100
|
at: Date;
|
|
87
101
|
}): OutboundReply;
|
|
102
|
+
export declare function buildFailure(conversationId: string, replyTo: string, reason: string | undefined, report: {
|
|
103
|
+
messageId: string;
|
|
104
|
+
at: Date;
|
|
105
|
+
}): OutboundFailure;
|
|
88
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[];
|
|
89
109
|
/**
|
|
90
110
|
* Text handed to the agent for a message. It is marked as data written by an outside person.
|
|
91
111
|
* Angle brackets in the sender and the body are escaped, so the body cannot close the envelope.
|
package/dist/protocol.js
CHANGED
|
@@ -132,9 +132,24 @@ export function buildReply(conversationId, replyTo, text, reply) {
|
|
|
132
132
|
at: reply.at.toISOString(),
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
|
+
export function buildFailure(conversationId, replyTo, reason, report) {
|
|
136
|
+
return {
|
|
137
|
+
type: "failure",
|
|
138
|
+
conversationId,
|
|
139
|
+
messageId: report.messageId,
|
|
140
|
+
replyTo,
|
|
141
|
+
...(reason === undefined ? {} : { reason }),
|
|
142
|
+
at: report.at.toISOString(),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
135
145
|
export function buildStatus(conversationId, status, at) {
|
|
136
146
|
return { type: "status", conversationId, status, at: at.toISOString() };
|
|
137
147
|
}
|
|
148
|
+
const ENVELOPE = /<channel-message messageId="([A-Za-z0-9._:-]{1,128})">/g;
|
|
149
|
+
/** The message ids of the envelopes in a text handed to the agent (see `formatForSession`). */
|
|
150
|
+
export function messageIdsIn(text) {
|
|
151
|
+
return [...text.matchAll(ENVELOPE)].flatMap((match) => match[1] === undefined ? [] : [match[1]]);
|
|
152
|
+
}
|
|
138
153
|
/**
|
|
139
154
|
* Text handed to the agent for a message. It is marked as data written by an outside person.
|
|
140
155
|
* Angle brackets in the sender and the body are escaped, so the body cannot close the envelope.
|
package/dist/protocol.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAElC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,uBAAuB,GAAG,2BAA2B,CAAC;AAOnE,MAAM,CAAC,MAAM,cAAc,GAAW,EAAE,aAAa,EAAE,IAAI,EA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SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,cAAc,CAAC,KAAa;IACnC,OAAO,CACL,kEAAkE,CAAC,IAAI,CAAC,KAAK,CAAC;QAC9E,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CACjC,CAAC;AACJ,CAAC"}
|
package/dist/redact.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes an error message safe to hand to a service. The text comes from a model provider and can
|
|
3
|
+
* quote a request: secrets are hidden before it leaves the machine. Known shapes, not a guarantee;
|
|
4
|
+
* `failures.includeReason: false` keeps the reason on the machine.
|
|
5
|
+
*/
|
|
6
|
+
export declare const MAX_REASON_LENGTH = 500;
|
|
7
|
+
/**
|
|
8
|
+
* One line, secrets hidden, at most `MAX_REASON_LENGTH` characters. Undefined when nothing is
|
|
9
|
+
* left. Secrets are hidden before the cut, so a cut never leaves part of one.
|
|
10
|
+
*/
|
|
11
|
+
export declare function cleanReason(raw: string): string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* The same hiding for a longer text that keeps its lines: the answer a model wrote without using
|
|
14
|
+
* the reply tool. At most `maxLength` characters. Undefined when nothing is left.
|
|
15
|
+
*/
|
|
16
|
+
export declare function cleanText(raw: string, maxLength: number): string | undefined;
|
package/dist/redact.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes an error message safe to hand to a service. The text comes from a model provider and can
|
|
3
|
+
* quote a request: secrets are hidden before it leaves the machine. Known shapes, not a guarantee;
|
|
4
|
+
* `failures.includeReason: false` keeps the reason on the machine.
|
|
5
|
+
*/
|
|
6
|
+
export const MAX_REASON_LENGTH = 500;
|
|
7
|
+
const HIDDEN = "[hidden]";
|
|
8
|
+
/** A private key block, from its first line to its last one, or to the end of the text. */
|
|
9
|
+
const KEY_BLOCK = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$)/g;
|
|
10
|
+
const SECRET_SHAPES = [
|
|
11
|
+
// The credentials of an address (scheme://user:password@host): the host stays.
|
|
12
|
+
[/\b([a-z][a-z0-9+.-]*:\/\/)[^\s/:@]*:[^\s/@]+@/gi, `$1${HIDDEN}@`],
|
|
13
|
+
// The query of an address can carry keys.
|
|
14
|
+
[/\b(https?:\/\/[^\s?#]+)\?[^\s"')]+/gi, `$1?${HIDDEN}`],
|
|
15
|
+
[
|
|
16
|
+
/\b(authorization["']?\s*[:=]\s*["']?)(?:(?:bearer|basic|token)\s+)?[^\s"',;]+/gi,
|
|
17
|
+
`$1${HIDDEN}`,
|
|
18
|
+
],
|
|
19
|
+
[/\b(bearer\s+)[A-Za-z0-9._~+/=-]{8,}/gi, `$1${HIDDEN}`],
|
|
20
|
+
// key=value and key: value for names that hold secrets.
|
|
21
|
+
[
|
|
22
|
+
/\b([A-Za-z0-9_-]*(?:password|passwd|secret|token|api[_-]?key|access[_-]?key)["']?\s*[:=]\s*["']?)[^\s"',;]{3,}/gi,
|
|
23
|
+
`$1${HIDDEN}`,
|
|
24
|
+
],
|
|
25
|
+
// Tokens of known services.
|
|
26
|
+
[/\b(?:sk|rk|pk)[-_][A-Za-z0-9_*-]{8,}/g, HIDDEN],
|
|
27
|
+
[/\b(?:gh[pousr]_|github_pat_|npm_|hf_)[A-Za-z0-9_]{16,}/g, HIDDEN],
|
|
28
|
+
[/\bxox[abposr]-[A-Za-z0-9-]{10,}/g, HIDDEN],
|
|
29
|
+
[/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, HIDDEN],
|
|
30
|
+
[/\bAIza[0-9A-Za-z_-]{30,}/g, HIDDEN],
|
|
31
|
+
[/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, HIDDEN],
|
|
32
|
+
// The home folder names the person.
|
|
33
|
+
[/(?:\/Users|\/home)\/[^\s/"':]+/g, "~"],
|
|
34
|
+
[/[A-Za-z]:\\Users\\[^\s\\"':]+/g, "~"],
|
|
35
|
+
];
|
|
36
|
+
/** A long token of random look: 32 or more characters with upper case, lower case and digits. */
|
|
37
|
+
const LONG_TOKEN = /[A-Za-z0-9_+=-]{32,}/g;
|
|
38
|
+
/**
|
|
39
|
+
* One line, secrets hidden, at most `MAX_REASON_LENGTH` characters. Undefined when nothing is
|
|
40
|
+
* left. Secrets are hidden before the cut, so a cut never leaves part of one.
|
|
41
|
+
*/
|
|
42
|
+
export function cleanReason(raw) {
|
|
43
|
+
return cut(hideSecrets(oneLine(raw)), MAX_REASON_LENGTH);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The same hiding for a longer text that keeps its lines: the answer a model wrote without using
|
|
47
|
+
* the reply tool. At most `maxLength` characters. Undefined when nothing is left.
|
|
48
|
+
*/
|
|
49
|
+
export function cleanText(raw, maxLength) {
|
|
50
|
+
return cut(hideSecrets(withoutControls(raw, "\n\t")).trim(), maxLength);
|
|
51
|
+
}
|
|
52
|
+
function hideSecrets(raw) {
|
|
53
|
+
let text = raw.replace(KEY_BLOCK, HIDDEN);
|
|
54
|
+
for (const [pattern, replacement] of SECRET_SHAPES) {
|
|
55
|
+
text = text.replace(pattern, replacement);
|
|
56
|
+
}
|
|
57
|
+
return text.replace(LONG_TOKEN, (token) => /[A-Z]/.test(token) && /[a-z]/.test(token) && /\d/.test(token) ? HIDDEN : token);
|
|
58
|
+
}
|
|
59
|
+
function cut(text, maxLength) {
|
|
60
|
+
const symbols = [...text];
|
|
61
|
+
if (symbols.length === 0) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
return symbols.length <= maxLength ? text : `${symbols.slice(0, maxLength - 1).join("")}…`;
|
|
65
|
+
}
|
|
66
|
+
function oneLine(text) {
|
|
67
|
+
return withoutControls(text, "").replace(/\s+/g, " ").trim();
|
|
68
|
+
}
|
|
69
|
+
/** Control characters become spaces, except those in `keep`. */
|
|
70
|
+
function withoutControls(text, keep) {
|
|
71
|
+
let result = "";
|
|
72
|
+
for (const symbol of text) {
|
|
73
|
+
const code = symbol.codePointAt(0) ?? 0;
|
|
74
|
+
const control = code < 32 || (code >= 127 && code <= 159) || code === 0x2028 || code === 0x2029;
|
|
75
|
+
result += control && !keep.includes(symbol) ? " " : symbol;
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=redact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact.js","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAErC,MAAM,MAAM,GAAG,UAAU,CAAC;AAE1B,2FAA2F;AAC3F,MAAM,SAAS,GACb,yFAAyF,CAAC;AAE5F,MAAM,aAAa,GAA6C;IAC9D,+EAA+E;IAC/E,CAAC,iDAAiD,EAAE,KAAK,MAAM,GAAG,CAAC;IACnE,0CAA0C;IAC1C,CAAC,sCAAsC,EAAE,MAAM,MAAM,EAAE,CAAC;IACxD;QACE,iFAAiF;QACjF,KAAK,MAAM,EAAE;KACd;IACD,CAAC,uCAAuC,EAAE,KAAK,MAAM,EAAE,CAAC;IACxD,wDAAwD;IACxD;QACE,kHAAkH;QAClH,KAAK,MAAM,EAAE;KACd;IACD,4BAA4B;IAC5B,CAAC,uCAAuC,EAAE,MAAM,CAAC;IACjD,CAAC,yDAAyD,EAAE,MAAM,CAAC;IACnE,CAAC,kCAAkC,EAAE,MAAM,CAAC;IAC5C,CAAC,gCAAgC,EAAE,MAAM,CAAC;IAC1C,CAAC,2BAA2B,EAAE,MAAM,CAAC;IACrC,CAAC,+DAA+D,EAAE,MAAM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B,CAAC;IACD,OAAO,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC;AAC7F,CAAC;AAED,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,eAAe,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAC/D,CAAC;AAED,gEAAgE;AAChE,SAAS,eAAe,CAAC,IAAY,EAAE,IAAY;IACjD,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,MAAM,MAAM,IAAI,IAAI,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACxC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAE,IAAI,CAAC,IAAI,IAAI,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,MAAM,CAAC;QAChG,MAAM,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;IAC7D,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/turn.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the end of an Oh My Pi run. The shapes are declared loosely on purpose: only the fields
|
|
3
|
+
* used here are required, so an unexpected message is skipped instead of breaking the session.
|
|
4
|
+
*/
|
|
5
|
+
export interface RunEnd {
|
|
6
|
+
/** True when Oh My Pi continues the run on its own (a retry, for example). */
|
|
7
|
+
readonly willContinue?: boolean | undefined;
|
|
8
|
+
/** True when background work that will continue the run is still going. */
|
|
9
|
+
readonly awaitingAsyncWork?: boolean | undefined;
|
|
10
|
+
/** The session messages when the run ended. */
|
|
11
|
+
readonly messages?: readonly unknown[] | undefined;
|
|
12
|
+
}
|
|
13
|
+
/** Fixed texts a service receives when there is no answer of the model to send. */
|
|
14
|
+
export declare const NO_REPLY_REASON = "The agent finished without sending a reply";
|
|
15
|
+
export declare const STOPPED_REASON = "The turn was stopped before the agent answered";
|
|
16
|
+
export declare const INCOMPLETE_REASON = "The agent stopped before finishing its answer";
|
|
17
|
+
/**
|
|
18
|
+
* A stretch of a run that ends with one outcome, and the channel messages it was answering.
|
|
19
|
+
* `completed`: the model finished an answer (`text`, which may be missing). `failed`: a model
|
|
20
|
+
* error. `stopped`: aborted, or ended before an answer.
|
|
21
|
+
*/
|
|
22
|
+
export interface Segment {
|
|
23
|
+
/** Ids of the channel messages this stretch was answering, oldest first. */
|
|
24
|
+
readonly messageIds: readonly string[];
|
|
25
|
+
readonly outcome: "completed" | "failed" | "stopped";
|
|
26
|
+
/** Why there is no answer. A model error comes as it is: clean it before it is sent. */
|
|
27
|
+
readonly reason: string | undefined;
|
|
28
|
+
/** The text the model ended its answer with, as it came: clean it before it is sent. */
|
|
29
|
+
readonly text: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The channel messages of a finished run, grouped by the answer that closed them. Empty for a run
|
|
33
|
+
* that Oh My Pi continues (a retry, or background work that has not ended in an error) and for a
|
|
34
|
+
* run without channel messages. The messages are the whole session, so the caller drops those
|
|
35
|
+
* that already have a reply or a report.
|
|
36
|
+
*/
|
|
37
|
+
export declare function segmentsOf(end: RunEnd): Segment[];
|
package/dist/turn.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { messageIdsIn } from "./protocol.js";
|
|
2
|
+
/** Fixed texts a service receives when there is no answer of the model to send. */
|
|
3
|
+
export const NO_REPLY_REASON = "The agent finished without sending a reply";
|
|
4
|
+
export const STOPPED_REASON = "The turn was stopped before the agent answered";
|
|
5
|
+
export const INCOMPLETE_REASON = "The agent stopped before finishing its answer";
|
|
6
|
+
/**
|
|
7
|
+
* The channel messages of a finished run, grouped by the answer that closed them. Empty for a run
|
|
8
|
+
* that Oh My Pi continues (a retry, or background work that has not ended in an error) and for a
|
|
9
|
+
* run without channel messages. The messages are the whole session, so the caller drops those
|
|
10
|
+
* that already have a reply or a report.
|
|
11
|
+
*/
|
|
12
|
+
export function segmentsOf(end) {
|
|
13
|
+
if (end.willContinue === true) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
const segments = [];
|
|
17
|
+
let ids = [];
|
|
18
|
+
// The last assistant message after the last completed answer and the last message to answer.
|
|
19
|
+
let last;
|
|
20
|
+
for (const message of (end.messages ?? []).filter(isRecord)) {
|
|
21
|
+
if (message.role === "user" || message.role === "developer") {
|
|
22
|
+
ids.push(...textsOf(message.content).flatMap(messageIdsIn));
|
|
23
|
+
// An end seen before this message says nothing about it.
|
|
24
|
+
last = undefined;
|
|
25
|
+
}
|
|
26
|
+
else if (message.role === "assistant") {
|
|
27
|
+
if (message.stopReason === "stop" || message.stopReason === "length") {
|
|
28
|
+
const text = textsOf(message.content).join("\n").trim();
|
|
29
|
+
add(segments, ids, "completed", undefined, text);
|
|
30
|
+
ids = [];
|
|
31
|
+
last = undefined;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
last = message;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (last?.stopReason === "error") {
|
|
39
|
+
const reason = typeof last.errorMessage === "string" ? last.errorMessage : undefined;
|
|
40
|
+
add(segments, ids, "failed", reason, "");
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const aborted = last === undefined || last.stopReason === "aborted";
|
|
44
|
+
add(segments, ids, "stopped", aborted ? STOPPED_REASON : INCOMPLETE_REASON, "");
|
|
45
|
+
}
|
|
46
|
+
// Background work will continue the run: only an error is already final.
|
|
47
|
+
if (end.awaitingAsyncWork === true && segments.at(-1)?.outcome !== "failed") {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
return segments;
|
|
51
|
+
}
|
|
52
|
+
function add(segments, ids, outcome, reason, text) {
|
|
53
|
+
if (ids.length > 0) {
|
|
54
|
+
segments.push({
|
|
55
|
+
messageIds: [...new Set(ids)],
|
|
56
|
+
outcome,
|
|
57
|
+
reason,
|
|
58
|
+
text: text === "" ? undefined : text,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** The text parts of a message. Thinking and tool calls are not text. */
|
|
63
|
+
function textsOf(content) {
|
|
64
|
+
if (typeof content === "string") {
|
|
65
|
+
return [content];
|
|
66
|
+
}
|
|
67
|
+
if (!Array.isArray(content)) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
return content
|
|
71
|
+
.filter(isRecord)
|
|
72
|
+
.flatMap((part) => (part.type === undefined || part.type === "text") && typeof part.text === "string"
|
|
73
|
+
? [part.text]
|
|
74
|
+
: []);
|
|
75
|
+
}
|
|
76
|
+
function isRecord(value) {
|
|
77
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=turn.js.map
|
package/dist/turn.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"turn.js","sourceRoot":"","sources":["../src/turn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAe7C,mFAAmF;AACnF,MAAM,CAAC,MAAM,eAAe,GAAG,4CAA4C,CAAC;AAC5E,MAAM,CAAC,MAAM,cAAc,GAAG,gDAAgD,CAAC;AAC/E,MAAM,CAAC,MAAM,iBAAiB,GAAG,+CAA+C,CAAC;AAiBjF;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,IAAI,GAAG,CAAC,YAAY,KAAK,IAAI,EAAE,CAAC;QAC9B,OAAO,E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EAAE,EAAE,CAChB,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ;QAChF,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;QACb,CAAC,CAAC,EAAE,CACP,CAAC;AACN,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
|
package/docs/protocol.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
---
|
|
2
|
+
|
|
3
|
+
title: Integration protocol
|
|
4
|
+
description: The public contract between a service and an Oh My Pi agent session.
|
|
5
|
+
---
|
|
6
|
+
|
|
1
7
|
# Integration protocol (version 1)
|
|
2
8
|
|
|
3
9
|
This document is the public contract between an external **service** (any system that exchanges
|
|
@@ -5,13 +11,14 @@ messages with a person or with another agent) and an agent
|
|
|
5
11
|
session of [Oh My Pi](https://github.com/can1357/oh-my-pi) running omp-channel. Any service can
|
|
6
12
|
implement it. The contract is independent of any product.
|
|
7
13
|
|
|
8
|
-
The machine-readable schemas are in [`schema/`](
|
|
14
|
+
The machine-readable schemas are in [`schema/`](https://github.com/tommasomarchionni/omp-channel/tree/main/schema):
|
|
9
15
|
|
|
10
16
|
| File | Describes |
|
|
11
17
|
| ---------------------------- | ----------------------------------------------------------------------- |
|
|
12
18
|
| `inbound-event.schema.json` | events the service sends to the agent (`message`, `interrupt`, `close`) |
|
|
13
19
|
| `outbound-reply.schema.json` | a reply the agent posts to the service |
|
|
14
20
|
| `status.schema.json` | agent state the agent reports to the service |
|
|
21
|
+
| `failure.schema.json` | a turn that ended in an error and left a message unanswered |
|
|
15
22
|
| `config.schema.json` | the omp-channel configuration file |
|
|
16
23
|
|
|
17
24
|
The TypeScript module `src/protocol.ts` implements the same rules. Both must change together.
|
|
@@ -122,6 +129,7 @@ Paths are relative to `service.url` and configurable in `service.paths`. Default
|
|
|
122
129
|
| Acknowledge | `POST /messages/ack` | `{ "deliveryIds": ["d1"] }` |
|
|
123
130
|
| Reply | `POST /replies` | outbound reply (section 7) |
|
|
124
131
|
| Status | `POST /status` | agent status (section 8) |
|
|
132
|
+
| Failure | `POST /failures` | failed turn (section 9) |
|
|
125
133
|
|
|
126
134
|
All calls carry `Authorization: Bearer <key>`. A non-2xx answer is an error. The key is never
|
|
127
135
|
included in a log line, an error message or a tool result.
|
|
@@ -207,6 +215,7 @@ interface ChannelAdapter {
|
|
|
207
215
|
reply(reply: OutboundReply): Promise<void>; // required
|
|
208
216
|
ack?(deliveryId: string): Promise<void>; // after admission
|
|
209
217
|
status?(status: OutboundStatus): Promise<void>; // best effort
|
|
218
|
+
failure?(failure: OutboundFailure): Promise<void>; // a message got no answer (section 9)
|
|
210
219
|
interrupt?(conversationId: string): Promise<void>; // after an interrupt is applied
|
|
211
220
|
close?(conversationId: string): Promise<void>; // after a close is applied
|
|
212
221
|
describe?(): Promise<string> | string; // plain-text state for the /channel-status command
|
|
@@ -244,20 +253,76 @@ message text. A `describe` that throws is reported as one line; its error text i
|
|
|
244
253
|
## 8. Agent status (agent to service)
|
|
245
254
|
|
|
246
255
|
```json
|
|
247
|
-
{
|
|
256
|
+
{
|
|
257
|
+
"type": "status",
|
|
258
|
+
"conversationId": "c1",
|
|
259
|
+
"status": "working",
|
|
260
|
+
"at": "2026-11-26T10:00:00.000Z"
|
|
261
|
+
}
|
|
248
262
|
```
|
|
249
263
|
|
|
250
|
-
`status` is `working` when a turn starts and `free` when it ends, for the open conversation.
|
|
251
|
-
|
|
252
|
-
|
|
264
|
+
`status` is `working` when a turn starts and `free` when it ends, for the open conversation. A run
|
|
265
|
+
that Oh My Pi continues on its own (a retry of a model request, for example) has not ended: the
|
|
266
|
+
state stays `working`. Status is best effort: a failed report is logged and not retried. Status is
|
|
267
|
+
not part of the acknowledgement logic.
|
|
268
|
+
|
|
269
|
+
## 9. No message is left waiting
|
|
270
|
+
|
|
271
|
+
Every message the agent took ends with exactly one outcome that the service receives: a `reply`
|
|
272
|
+
(section 6) or a `failure`.
|
|
273
|
+
|
|
274
|
+
| What happens | What the service receives |
|
|
275
|
+
| ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
|
276
|
+
| The model calls the `reply` tool | that reply |
|
|
277
|
+
| The run ends with an answer and no `reply` call | the final text of the model, as a reply |
|
|
278
|
+
| The same, with `replies.fromFinalText: false` or no text | `failure`: `The agent finished without sending a reply` |
|
|
279
|
+
| A model error | `failure` with the error text |
|
|
280
|
+
| The turn is aborted, or ends before an answer | `failure`: `The turn was stopped before the agent answered` or `The agent stopped before finishing its answer` |
|
|
281
|
+
| The service refuses the final text, also after two more tries | `failure`: `The agent answered, but the answer could not be delivered` |
|
|
282
|
+
| One answer covers several messages | the answer for the newest, `Answered together with a later message.` as the reply of each older one |
|
|
283
|
+
| A new, resumed or forked session while a message is open | `failure`: `The agent moved to another session before it answered` |
|
|
284
|
+
| Oh My Pi closes while a message is open | `failure`: `The agent was closed before it answered` (one attempt) |
|
|
285
|
+
| Oh My Pi retries, or background work will continue the run | nothing yet |
|
|
286
|
+
|
|
287
|
+
A process that is killed, or a machine that loses power or network for good, sends nothing: a
|
|
288
|
+
service should show that the agent is not connected.
|
|
289
|
+
|
|
290
|
+
The `failure` event:
|
|
291
|
+
|
|
292
|
+
```json
|
|
293
|
+
{
|
|
294
|
+
"type": "failure",
|
|
295
|
+
"conversationId": "c1",
|
|
296
|
+
"messageId": "f-41c0b9e2",
|
|
297
|
+
"replyTo": "m1",
|
|
298
|
+
"reason": "404 The model is downloaded but not loaded",
|
|
299
|
+
"at": "2026-11-26T10:00:00.000Z"
|
|
300
|
+
}
|
|
301
|
+
```
|
|
253
302
|
|
|
254
|
-
|
|
303
|
+
- One report for each unanswered message: `replyTo` is its `messageId`. `messageId` is generated
|
|
304
|
+
by the agent for each report.
|
|
305
|
+
- A message that already got a reply is never reported.
|
|
306
|
+
- `reason` is optional text for a person: one line, at most 500 characters.
|
|
307
|
+
`failures.includeReason: false` sends no reason.
|
|
308
|
+
- The final text sent as a reply is the text of the last answer only, never the thinking or a tool
|
|
309
|
+
result. It keeps its lines and is cut at `limits.maxTextLength`.
|
|
310
|
+
- In both, common shapes of secrets (tokens, credentials in headers and addresses) and the name of
|
|
311
|
+
the home folder are hidden before sending. The hiding covers known shapes and is no guarantee.
|
|
312
|
+
- A refused reply or report is tried again after 1 and 5 seconds. A message that still could not
|
|
313
|
+
be closed is tried again when the next run ends.
|
|
314
|
+
- A reply for the same message is still accepted afterwards. A service that has shown the failure
|
|
315
|
+
decides what to do with it.
|
|
316
|
+
- A transport without the `failure` hook only logs the event. Over MCP push there is no way back
|
|
317
|
+
to the service, so nothing is sent.
|
|
318
|
+
|
|
319
|
+
## 10. Activity (optional, not emitted in version 1)
|
|
255
320
|
|
|
256
321
|
The protocol reserves an optional `activity` report (for example, a tool started or finished)
|
|
257
322
|
for showing progress to the service. Version 1 of omp-channel does not emit it. A service must
|
|
258
323
|
not require it.
|
|
259
324
|
|
|
260
|
-
##
|
|
325
|
+
## 11. Security rules
|
|
261
326
|
|
|
262
327
|
- The service key is read from an environment variable named in the configuration
|
|
263
328
|
(`service.keyEnv`), or from a file named by `<keyEnv>_FILE`. It is never written in the
|
|
@@ -268,14 +333,15 @@ not require it.
|
|
|
268
333
|
- Inbound text is data from an outside party. It is handed to the model inside an envelope that
|
|
269
334
|
says so, and angle brackets are escaped so the text cannot close the envelope.
|
|
270
335
|
- Inbound events are validated before use. Unknown fields and out-of-range values are rejected.
|
|
336
|
+
- The error text of a failed turn is cleaned of known secret shapes before it is sent (section 9).
|
|
271
337
|
|
|
272
|
-
##
|
|
338
|
+
## 12. Versioning
|
|
273
339
|
|
|
274
340
|
`PROTOCOL_VERSION` is `1`. Version 1 is alpha: fields may still change before the first stable
|
|
275
341
|
release, and each change is recorded in CHANGELOG.md. A future incompatible change will use a
|
|
276
342
|
new version number.
|
|
277
343
|
|
|
278
|
-
##
|
|
344
|
+
## 13. Not in this contract
|
|
279
345
|
|
|
280
346
|
- Delivery of messages from the agent to a person without a reply to a message.
|
|
281
347
|
- Attachments and other non-text content.
|