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.
Files changed (44) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +8 -0
  4. package/README.md +318 -2
  5. package/dist/adapter.d.ts +46 -0
  6. package/dist/adapter.js +18 -0
  7. package/dist/adapter.js.map +1 -0
  8. package/dist/config.d.ts +58 -0
  9. package/dist/config.js +140 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/dispatcher.d.ts +53 -0
  12. package/dist/dispatcher.js +242 -0
  13. package/dist/dispatcher.js.map +1 -0
  14. package/dist/extension.d.ts +27 -0
  15. package/dist/extension.js +151 -0
  16. package/dist/extension.js.map +1 -0
  17. package/dist/http-adapter.d.ts +20 -0
  18. package/dist/http-adapter.js +140 -0
  19. package/dist/http-adapter.js.map +1 -0
  20. package/dist/index.d.ts +11 -0
  21. package/dist/index.js +50 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/module-adapter.d.ts +3 -0
  24. package/dist/module-adapter.js +21 -0
  25. package/dist/module-adapter.js.map +1 -0
  26. package/dist/protocol.d.ts +113 -0
  27. package/dist/protocol.js +222 -0
  28. package/dist/protocol.js.map +1 -0
  29. package/dist/redact.d.ts +11 -0
  30. package/dist/redact.js +65 -0
  31. package/dist/redact.js.map +1 -0
  32. package/dist/tool-schema.d.ts +13 -0
  33. package/dist/tool-schema.js +13 -0
  34. package/dist/tool-schema.js.map +1 -0
  35. package/dist/turn.d.ts +24 -0
  36. package/dist/turn.js +60 -0
  37. package/dist/turn.js.map +1 -0
  38. package/docs/protocol.md +334 -0
  39. package/package.json +76 -4
  40. package/schema/config.schema.json +159 -0
  41. package/schema/failure.schema.json +34 -0
  42. package/schema/inbound-event.schema.json +82 -0
  43. package/schema/outbound-reply.schema.json +34 -0
  44. package/schema/status.schema.json +22 -0
@@ -0,0 +1,58 @@
1
+ import type { Limits } from "./protocol.js";
2
+ export declare const DEFAULT_CONFIG_RELATIVE_PATH: string;
3
+ export declare class ConfigError extends Error {
4
+ constructor(message: string);
5
+ }
6
+ /** Reads text, or returns undefined when the file does not exist. Other errors are thrown. */
7
+ export type ReadText = (path: string) => string | undefined;
8
+ export interface ServiceConfig {
9
+ readonly url: URL;
10
+ readonly key: string;
11
+ readonly requestTimeoutMs: number;
12
+ readonly paths: ServicePaths;
13
+ }
14
+ export interface PollConfig {
15
+ readonly intervalMs: number;
16
+ readonly maxBackoffMs: number;
17
+ readonly batchSize: number;
18
+ }
19
+ export interface DeliveryConfig {
20
+ readonly deliverAs: "steer" | "followUp" | "aside";
21
+ }
22
+ export interface FailuresConfig {
23
+ /** Whether the cleaned error text is sent with a failure report. */
24
+ readonly includeReason: boolean;
25
+ }
26
+ export interface ChannelConfig {
27
+ readonly mcpServer: string;
28
+ readonly service: ServiceConfig | undefined;
29
+ readonly adapterModule: string | undefined;
30
+ readonly poll: PollConfig;
31
+ readonly limits: Limits & {
32
+ maxPending: number;
33
+ };
34
+ readonly delivery: DeliveryConfig;
35
+ readonly failures: FailuresConfig;
36
+ }
37
+ export interface ConfigSources {
38
+ readonly env: Readonly<Record<string, string | undefined>>;
39
+ readonly cwd: string;
40
+ readonly readText: ReadText;
41
+ }
42
+ /**
43
+ * Loads the configuration. The file is OMP_CHANNEL_CONFIG when set, otherwise
44
+ * <cwd>/.omp/omp-channel.json when it exists, otherwise built-in defaults.
45
+ * The key is read from the environment variable named by service.keyEnv, or from the file named
46
+ * by <keyEnv>_FILE. It is never read from the configuration file.
47
+ */
48
+ export declare function loadConfig(sources: ConfigSources): ChannelConfig;
49
+ interface ServicePaths {
50
+ messages: string;
51
+ ack: string;
52
+ replies: string;
53
+ status: string;
54
+ failures: string;
55
+ }
56
+ /** https is required. Plain http is accepted only for loopback, and credentials are refused. */
57
+ export declare function parseServiceUrl(raw: string): URL;
58
+ export {};
package/dist/config.js ADDED
@@ -0,0 +1,140 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { Ajv2020 } from "ajv/dist/2020.js";
4
+ export const DEFAULT_CONFIG_RELATIVE_PATH = join(".omp", "omp-channel.json");
5
+ export class ConfigError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = "ConfigError";
9
+ }
10
+ }
11
+ const schemaUrl = new URL("../schema/config.schema.json", import.meta.url);
12
+ const ajv = new Ajv2020({ allErrors: false, strict: true, useDefaults: true, verbose: true });
13
+ const validateConfigFile = ajv.compile(JSON.parse(readFileSync(schemaUrl, "utf8")));
14
+ /**
15
+ * Loads the configuration. The file is OMP_CHANNEL_CONFIG when set, otherwise
16
+ * <cwd>/.omp/omp-channel.json when it exists, otherwise built-in defaults.
17
+ * The key is read from the environment variable named by service.keyEnv, or from the file named
18
+ * by <keyEnv>_FILE. It is never read from the configuration file.
19
+ */
20
+ export function loadConfig(sources) {
21
+ const path = nonEmpty(sources.env.OMP_CHANNEL_CONFIG) ?? join(sources.cwd, DEFAULT_CONFIG_RELATIVE_PATH);
22
+ const explicit = nonEmpty(sources.env.OMP_CHANNEL_CONFIG) !== undefined;
23
+ const text = sources.readText(path);
24
+ if (text === undefined && explicit) {
25
+ throw new ConfigError("OMP_CHANNEL_CONFIG points to a file that does not exist");
26
+ }
27
+ let raw = {};
28
+ if (text !== undefined) {
29
+ try {
30
+ raw = JSON.parse(text);
31
+ }
32
+ catch {
33
+ throw new ConfigError("the configuration file is not valid JSON");
34
+ }
35
+ }
36
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
37
+ throw new ConfigError("the configuration file must be a JSON object");
38
+ }
39
+ const data = { ...raw };
40
+ if (!validateConfigFile(data)) {
41
+ const first = validateConfigFile.errors?.[0];
42
+ throw new ConfigError(`invalid configuration: ${first === undefined ? "unknown error" : describeSchemaError(first)}`);
43
+ }
44
+ const parsed = data;
45
+ const service = parsed.service === undefined ? undefined : loadService(parsed.service, sources);
46
+ return {
47
+ mcpServer: parsed.mcpServer,
48
+ service,
49
+ adapterModule: parsed.adapterModule,
50
+ poll: parsed.poll,
51
+ limits: parsed.limits,
52
+ delivery: parsed.delivery,
53
+ failures: parsed.failures,
54
+ };
55
+ }
56
+ /**
57
+ * Turns a schema error into one sentence that names the place and what is allowed. It never
58
+ * prints the rejected value, so a mistyped key cannot reach a log through this message.
59
+ */
60
+ function describeSchemaError(error) {
61
+ const params = error.params;
62
+ const place = error.instancePath === "" ? "the file" : error.instancePath;
63
+ const allowed = error.parentSchema
64
+ ?.properties;
65
+ switch (error.keyword) {
66
+ case "additionalProperties": {
67
+ const keys = Object.keys(allowed ?? {}).filter((key) => key !== "$schema");
68
+ const list = keys.length === 0 ? "" : ` (allowed: ${keys.join(", ")})`;
69
+ return `unknown key "${String(params.additionalProperty)}" at ${place}${list}`;
70
+ }
71
+ case "required":
72
+ return `missing required key "${String(params.missingProperty)}" at ${place}`;
73
+ case "enum":
74
+ return `${place} must be one of: ${params.allowedValues.join(", ")}`;
75
+ case "minimum":
76
+ return `${place} must be at least ${String(params.limit)}`;
77
+ case "maximum":
78
+ return `${place} must be at most ${String(params.limit)}`;
79
+ case "type":
80
+ return `${place} must be ${article(String(params.type))}`;
81
+ case "pattern":
82
+ return `${place} has an invalid format`;
83
+ case "minLength":
84
+ return `${place} is too short`;
85
+ case "maxLength":
86
+ return `${place} is too long`;
87
+ default:
88
+ return `${place} ${error.message ?? "is invalid"}`;
89
+ }
90
+ }
91
+ function article(type) {
92
+ return type === "integer" || type === "object" ? `an ${type}` : `a ${type}`;
93
+ }
94
+ function loadService(service, sources) {
95
+ const url = parseServiceUrl(service.url);
96
+ const key = readKey(service.keyEnv, sources);
97
+ return { url, key, requestTimeoutMs: service.requestTimeoutMs, paths: service.paths };
98
+ }
99
+ /** https is required. Plain http is accepted only for loopback, and credentials are refused. */
100
+ export function parseServiceUrl(raw) {
101
+ let url;
102
+ try {
103
+ url = new URL(raw);
104
+ }
105
+ catch {
106
+ throw new ConfigError("service.url is not a valid URL");
107
+ }
108
+ if (url.username !== "" || url.password !== "") {
109
+ throw new ConfigError("service.url must not contain credentials");
110
+ }
111
+ const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]";
112
+ if (url.protocol === "https:" || (url.protocol === "http:" && loopback)) {
113
+ return url;
114
+ }
115
+ throw new ConfigError("service.url must use https (http is allowed only for loopback)");
116
+ }
117
+ function readKey(keyEnv, sources) {
118
+ const direct = nonEmpty(sources.env[keyEnv]);
119
+ if (direct !== undefined) {
120
+ return direct;
121
+ }
122
+ const filePath = nonEmpty(sources.env[`${keyEnv}_FILE`]);
123
+ if (filePath !== undefined) {
124
+ const text = sources.readText(filePath);
125
+ const key = text === undefined ? undefined : nonEmpty(text);
126
+ if (key !== undefined) {
127
+ return key;
128
+ }
129
+ throw new ConfigError(`${keyEnv}_FILE cannot be read or is empty`);
130
+ }
131
+ throw new ConfigError(`set ${keyEnv} (or ${keyEnv}_FILE) to the service key`);
132
+ }
133
+ function nonEmpty(value) {
134
+ if (value === undefined) {
135
+ return undefined;
136
+ }
137
+ const trimmed = value.trim();
138
+ return trimmed.length === 0 ? undefined : trimmed;
139
+ }
140
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAoB,MAAM,kBAAkB,CAAC;AAG7D,MAAM,CAAC,MAAM,4BAA4B,GAAG,IAAI,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;AAE7E,MAAM,OAAO,WAAY,SAAQ,KAAK;IACpC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AA2CD,MAAM,SAAS,GAAG,IAAI,G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G,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,OAAO,CAAC,CAAC,CAAC;IACzD,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACxC,MAAM,GAAG,GAAG,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC5D,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,OAAO,GAAG,CAAC;QACb,CAAC;QACD,MAAM,IAAI,WAAW,CAAC,GAAG,MAAM,kCAAkC,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,IAAI,WAAW,CAAC,OAAO,MAAM,QAAQ,MAAM,2BAA2B,CAAC,CAAC;AAChF,CAAC;AAED,SAAS,QAAQ,CAAC,KAAyB;IACzC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,OAAO,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;AACpD,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { type ChannelAdapter } from "./adapter.js";
2
+ import type { ChannelConfig } from "./config.js";
3
+ import type { SendUserMessageOptions } from "@oh-my-pi/pi-coding-agent/session/agent-session-types";
4
+ import { type AgentStatus, type InboundEvent } from "./protocol.js";
5
+ /** What happened to an inbound event. The outcomes marked as acknowledged may be acked. */
6
+ export type AdmitOutcome = "admitted" | "duplicate" | "ignored" | "refused" | "rejected" | "dropped";
7
+ export declare const ACKNOWLEDGED_OUTCOMES: ReadonlySet<AdmitOutcome>;
8
+ /** The part of the Oh My Pi session that the dispatcher uses. */
9
+ export interface SessionPort {
10
+ send(content: string, options: SendUserMessageOptions): Promise<void>;
11
+ abort(): void;
12
+ isIdle(): boolean;
13
+ }
14
+ export interface DispatcherOptions {
15
+ readonly config: ChannelConfig;
16
+ readonly session: SessionPort;
17
+ readonly adapter: ChannelAdapter | undefined;
18
+ readonly log: (line: string) => void;
19
+ readonly now?: () => Date;
20
+ readonly newId?: () => string;
21
+ }
22
+ export type ReplyResult = {
23
+ ok: true;
24
+ } | {
25
+ ok: false;
26
+ reason: string;
27
+ };
28
+ /**
29
+ * Applies inbound events to the agent session, one at a time and in arrival order.
30
+ *
31
+ * - One conversation is open at a time. Messages of another conversation are refused and are not
32
+ * acknowledged, so the service delivers them again once the open conversation closes.
33
+ * - A message is admitted once. Duplicates (same messageId, or same delivery) are acknowledged
34
+ * without running again.
35
+ * - Interrupt stops the current turn only. Tool results already produced stay in the session.
36
+ * - Agent state is reported to the service for the open conversation.
37
+ * - A message that a failed turn left unanswered is reported once.
38
+ */
39
+ export declare class Dispatcher {
40
+ #private;
41
+ constructor(options: DispatcherOptions);
42
+ get activeConversation(): string | undefined;
43
+ submit(event: InboundEvent, deliveryId?: string): Promise<AdmitOutcome>;
44
+ /** Reports the agent state for the open conversation. Best effort: errors are logged. */
45
+ setStatus(status: AgentStatus): void;
46
+ reply(messageId: string, text: string): Promise<ReplyResult>;
47
+ /**
48
+ * Tells the service that a failed turn left these messages unanswered. Messages that were not
49
+ * admitted here, already answered or already reported are skipped. Best effort: a report that
50
+ * is refused is logged, and a later failure of the same message tries again.
51
+ */
52
+ reportFailure(messageIds: readonly string[], reason: string | undefined): Promise<void>;
53
+ }
@@ -0,0 +1,242 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { AdapterError } from "./adapter.js";
3
+ import { cleanReason } from "./redact.js";
4
+ import { buildFailure, buildReply, buildStatus, formatForSession, } from "./protocol.js";
5
+ export const ACKNOWLEDGED_OUTCOMES = new Set([
6
+ "admitted",
7
+ "duplicate",
8
+ "ignored",
9
+ ]);
10
+ const MAX_REMEMBERED = 1000;
11
+ /**
12
+ * Applies inbound events to the agent session, one at a time and in arrival order.
13
+ *
14
+ * - One conversation is open at a time. Messages of another conversation are refused and are not
15
+ * acknowledged, so the service delivers them again once the open conversation closes.
16
+ * - A message is admitted once. Duplicates (same messageId, or same delivery) are acknowledged
17
+ * without running again.
18
+ * - Interrupt stops the current turn only. Tool results already produced stay in the session.
19
+ * - Agent state is reported to the service for the open conversation.
20
+ * - A message that a failed turn left unanswered is reported once.
21
+ */
22
+ export class Dispatcher {
23
+ #options;
24
+ #queue = Promise.resolve();
25
+ #pending = 0;
26
+ #active;
27
+ #remembered = new RecentSet(MAX_REMEMBERED);
28
+ #conversationOf = new RecentMap(MAX_REMEMBERED);
29
+ #answered = new RecentSet(MAX_REMEMBERED);
30
+ #reportedFailed = new RecentSet(MAX_REMEMBERED);
31
+ constructor(options) {
32
+ this.#options = options;
33
+ }
34
+ get activeConversation() {
35
+ return this.#active;
36
+ }
37
+ submit(event, deliveryId) {
38
+ if (this.#pending >= this.#options.config.limits.maxPending) {
39
+ return Promise.resolve("dropped");
40
+ }
41
+ this.#pending += 1;
42
+ const run = this.#queue.then(async () => {
43
+ try {
44
+ return await this.#apply(event, deliveryId);
45
+ }
46
+ finally {
47
+ this.#pending -= 1;
48
+ }
49
+ });
50
+ this.#queue = run.catch(() => undefined);
51
+ return run;
52
+ }
53
+ /** Reports the agent state for the open conversation. Best effort: errors are logged. */
54
+ setStatus(status) {
55
+ const conversation = this.#active;
56
+ const adapter = this.#options.adapter;
57
+ if (conversation === undefined || adapter?.status === undefined) {
58
+ return;
59
+ }
60
+ adapter.status(buildStatus(conversation, status, this.#now())).catch((error) => {
61
+ this.#options.log(`status was not reported (${errorName(error)})`);
62
+ });
63
+ }
64
+ async reply(messageId, text) {
65
+ const conversation = this.#conversationOf.get(messageId);
66
+ if (conversation === undefined) {
67
+ return { ok: false, reason: "unknown messageId" };
68
+ }
69
+ const adapter = this.#options.adapter;
70
+ if (adapter === undefined) {
71
+ return { ok: false, reason: "replies are not configured" };
72
+ }
73
+ const reply = buildReply(conversation, messageId, text, {
74
+ messageId: this.#newId(),
75
+ at: this.#now(),
76
+ });
77
+ try {
78
+ await adapter.reply(reply);
79
+ this.#answered.add(messageId);
80
+ return { ok: true };
81
+ }
82
+ catch (error) {
83
+ const detail = error instanceof AdapterError ? error.message : `the request failed (${errorName(error)})`;
84
+ return { ok: false, reason: `the service did not accept the reply: ${detail}` };
85
+ }
86
+ }
87
+ /**
88
+ * Tells the service that a failed turn left these messages unanswered. Messages that were not
89
+ * admitted here, already answered or already reported are skipped. Best effort: a report that
90
+ * is refused is logged, and a later failure of the same message tries again.
91
+ */
92
+ async reportFailure(messageIds, reason) {
93
+ const waiting = messageIds.flatMap((messageId) => {
94
+ const conversation = this.#conversationOf.get(messageId);
95
+ return conversation === undefined ||
96
+ this.#answered.has(messageId) ||
97
+ this.#reportedFailed.has(messageId)
98
+ ? []
99
+ : [{ messageId, conversation }];
100
+ });
101
+ if (waiting.length === 0) {
102
+ return;
103
+ }
104
+ const adapter = this.#options.adapter;
105
+ if (adapter?.failure === undefined) {
106
+ this.#options.log("a turn failed and the adapter does not report failures");
107
+ return;
108
+ }
109
+ const shown = this.#options.config.failures.includeReason && reason !== undefined
110
+ ? cleanReason(reason)
111
+ : undefined;
112
+ // Marked before the first wait, so a second end of the same run does not report twice.
113
+ for (const { messageId } of waiting) {
114
+ this.#reportedFailed.add(messageId);
115
+ }
116
+ for (const { messageId, conversation } of waiting) {
117
+ const report = buildFailure(conversation, messageId, shown, {
118
+ messageId: this.#newId(),
119
+ at: this.#now(),
120
+ });
121
+ try {
122
+ await adapter.failure(report);
123
+ }
124
+ catch (error) {
125
+ this.#reportedFailed.delete(messageId);
126
+ this.#options.log(`failure was not reported (${errorName(error)})`);
127
+ }
128
+ }
129
+ }
130
+ async #apply(event, deliveryId) {
131
+ const { config, session } = this.#options;
132
+ if (event.type === "message") {
133
+ return this.#applyMessage(event, config);
134
+ }
135
+ const key = deliveryId === undefined ? undefined : `delivery:${deliveryId}`;
136
+ if (key !== undefined && this.#remembered.has(key)) {
137
+ return "duplicate";
138
+ }
139
+ if (event.conversationId !== this.#active) {
140
+ return "ignored";
141
+ }
142
+ if (event.type === "interrupt") {
143
+ if (!session.isIdle()) {
144
+ session.abort();
145
+ }
146
+ if (key !== undefined) {
147
+ this.#remembered.add(key);
148
+ }
149
+ await this.#notify(() => this.#options.adapter?.interrupt?.(event.conversationId), "interrupt");
150
+ return "admitted";
151
+ }
152
+ this.#active = undefined;
153
+ if (key !== undefined) {
154
+ this.#remembered.add(key);
155
+ }
156
+ await this.#notify(() => this.#options.adapter?.close?.(event.conversationId), "close");
157
+ return "admitted";
158
+ }
159
+ async #applyMessage(event, config) {
160
+ const key = `message:${event.messageId}`;
161
+ if (this.#remembered.has(key)) {
162
+ return "duplicate";
163
+ }
164
+ if (this.#active !== undefined && this.#active !== event.conversationId) {
165
+ return "refused";
166
+ }
167
+ const options = {
168
+ deliverAs: config.delivery.deliverAs,
169
+ attribution: event.attribution,
170
+ };
171
+ try {
172
+ await this.#options.session.send(formatForSession(event), options);
173
+ }
174
+ catch (error) {
175
+ this.#options.log(`message ${event.messageId} was not admitted (${errorName(error)})`);
176
+ return "rejected";
177
+ }
178
+ this.#active = event.conversationId;
179
+ this.#remembered.add(key);
180
+ this.#conversationOf.set(event.messageId, event.conversationId);
181
+ return "admitted";
182
+ }
183
+ async #notify(call, label) {
184
+ try {
185
+ await call();
186
+ }
187
+ catch (error) {
188
+ this.#options.log(`${label} was not reported (${errorName(error)})`);
189
+ }
190
+ }
191
+ #now() {
192
+ return this.#options.now?.() ?? new Date();
193
+ }
194
+ #newId() {
195
+ return this.#options.newId?.() ?? randomUUID();
196
+ }
197
+ }
198
+ function errorName(error) {
199
+ return error instanceof Error ? error.name : "unknown";
200
+ }
201
+ class RecentSet {
202
+ #values = new Set();
203
+ #capacity;
204
+ constructor(capacity) {
205
+ this.#capacity = capacity;
206
+ }
207
+ has(value) {
208
+ return this.#values.has(value);
209
+ }
210
+ delete(value) {
211
+ this.#values.delete(value);
212
+ }
213
+ add(value) {
214
+ this.#values.add(value);
215
+ if (this.#values.size > this.#capacity) {
216
+ const oldest = this.#values.values().next();
217
+ if (oldest.value !== undefined) {
218
+ this.#values.delete(oldest.value);
219
+ }
220
+ }
221
+ }
222
+ }
223
+ class RecentMap {
224
+ #values = new Map();
225
+ #capacity;
226
+ constructor(capacity) {
227
+ this.#capacity = capacity;
228
+ }
229
+ get(key) {
230
+ return this.#values.get(key);
231
+ }
232
+ set(key, value) {
233
+ this.#values.set(key, value);
234
+ if (this.#values.size > this.#capacity) {
235
+ const oldest = this.#values.keys().next();
236
+ if (oldest.value !== undefined) {
237
+ this.#values.delete(oldest.value);
238
+ }
239
+ }
240
+ }
241
+ }
242
+ //# sourceMappingURL=dispatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispatcher.js","sourceRoot":"","sources":["../src/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,YAAY,EAAuB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAEL,YAAY,EACZ,UAAU,EACV,WAAW,EACX,gBAAgB,GAGjB,MAAM,eAAe,CAAC;AAWvB,MAAM,CAAC,MAAM,qBAAqB,GAA8B,IAAI,GAAG,CAAC;IACtE,UAAU;IACV,WAAW;IACX,SAAS;CACV,CAAC,CAAC;AAoBH,MAAM,cAAc,GAAG,IAAI,CAAC;AAE5B;;;;;;;;;;GAUG;AACH,MAAM,OAAO,UAAU;IACZ,QAAQ,CAAoB;IACrC,MAAM,GAAqB,OAAO,CAAC,OAA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,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC;YAC1C,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;gBAC/B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,27 @@
1
+ import type { ExtensionFactory } from "@oh-my-pi/pi-coding-agent/extensibility/extensions/types";
2
+ import type { ChannelAdapter } from "./adapter.js";
3
+ import type { ChannelConfig } from "./config.js";
4
+ export declare const REPLY_TOOL_NAME = "reply";
5
+ export interface ChannelExtensionOptions {
6
+ readonly config: ChannelConfig;
7
+ /** Transport adapter (HTTP service or a custom module). Undefined: MCP push only, no replies. */
8
+ readonly adapter?: ChannelAdapter | undefined;
9
+ /** Receives short diagnostic lines. They never contain message text or keys. */
10
+ readonly log?: (line: string) => void;
11
+ }
12
+ /**
13
+ * Creates the extension factory. It registers:
14
+ * - `mcp_notification`: messages pushed by the configured MCP server enter the agent;
15
+ * - the `reply` tool: the model answers a message by its messageId;
16
+ * - lifecycle hooks: agent state and failed turns are reported to the service, and the adapter
17
+ * stops on shutdown;
18
+ * - the adapter receive loop, when an adapter is given.
19
+ */
20
+ export declare function createChannelExtension(options: ChannelExtensionOptions): ExtensionFactory;
21
+ /** Name of the command that shows the channel state (`/channel-status`). */
22
+ export declare const STATUS_COMMAND_NAME = "channel-status";
23
+ /**
24
+ * The text of `/channel-status`: the transport in use, then the adapter's own description when it
25
+ * has one. A failing description is reported in one line and never shown raw.
26
+ */
27
+ export declare function channelStatusText(config: ChannelConfig, adapter: ChannelAdapter | undefined, log: (line: string) => void): Promise<string>;