@nebutra/agent-runtime 0.2.0 → 0.2.2

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 (137) hide show
  1. package/LICENSE +21 -676
  2. package/README.md +2 -0
  3. package/dist/adapters/dispatcher-sse.js +1 -0
  4. package/dist/adapters/index.d.ts +24 -5
  5. package/dist/adapters/index.js +28 -0
  6. package/dist/adapters/index.js.map +1 -1
  7. package/dist/adapters/mcp-catalog.js +1 -0
  8. package/dist/adapters/prisma-rollout.js +1 -0
  9. package/dist/chunk-424PT5DM.js +23 -0
  10. package/dist/chunk-424PT5DM.js.map +1 -0
  11. package/dist/{chunk-NN7DATXA.js → chunk-4Y25ZTKI.js} +3 -3
  12. package/dist/chunk-4Y25ZTKI.js.map +1 -0
  13. package/dist/{chunk-BJBBR3QA.js → chunk-D4YAPLOW.js} +4 -4
  14. package/dist/chunk-D4YAPLOW.js.map +1 -0
  15. package/dist/{chunk-ZMYX5VBU.js → chunk-GQZKYWFT.js} +23 -7
  16. package/dist/chunk-GQZKYWFT.js.map +1 -0
  17. package/dist/chunk-KCNN4QUQ.js +255 -0
  18. package/dist/chunk-KCNN4QUQ.js.map +1 -0
  19. package/dist/{chunk-PGGWSUTM.js → chunk-NI4EDT4T.js} +2 -2
  20. package/dist/chunk-NI4EDT4T.js.map +1 -0
  21. package/dist/chunk-Q62VKHIT.js +178 -0
  22. package/dist/chunk-Q62VKHIT.js.map +1 -0
  23. package/dist/{chunk-MUF7ZZTO.js → chunk-R5HOSQUW.js} +2 -1
  24. package/dist/{chunk-MUF7ZZTO.js.map → chunk-R5HOSQUW.js.map} +1 -1
  25. package/dist/{chunk-5N4644PB.js → chunk-SD2ZJ7XG.js} +4 -4
  26. package/dist/cli.d.ts +2 -0
  27. package/dist/cli.js +52 -0
  28. package/dist/cli.js.map +1 -0
  29. package/dist/commands.js +1 -0
  30. package/dist/definitions.js +1 -0
  31. package/dist/dispatcher.js +1 -0
  32. package/dist/durable-turn.js +3 -2
  33. package/dist/hook-pipeline.js +1 -0
  34. package/dist/index.d.ts +8 -85
  35. package/dist/index.js +249 -134
  36. package/dist/index.js.map +1 -1
  37. package/dist/loop.d.ts +2 -2
  38. package/dist/loop.js +3 -2
  39. package/dist/mcp-bridge.d.ts +3 -3
  40. package/dist/mcp-bridge.js +3 -2
  41. package/dist/model.js +1 -0
  42. package/dist/orchestration.d.ts +84 -0
  43. package/dist/orchestration.js +16 -0
  44. package/dist/orchestration.js.map +1 -0
  45. package/dist/policy.js +1 -0
  46. package/dist/protocol.js +1 -0
  47. package/dist/pulsar.d.ts +78 -0
  48. package/dist/pulsar.js +17 -0
  49. package/dist/pulsar.js.map +1 -0
  50. package/dist/rollout-store-persistent.js +1 -0
  51. package/dist/rollout.js +1 -0
  52. package/dist/sandbox.js +2 -1
  53. package/dist/skills.js +2 -1
  54. package/dist/subagents.js +1 -0
  55. package/dist/tools.d.ts +4 -3
  56. package/dist/tools.js +5 -3
  57. package/package.json +84 -27
  58. package/.turbo/turbo-build.log +0 -115
  59. package/.turbo/turbo-test.log +0 -44
  60. package/.turbo/turbo-typecheck.log +0 -4
  61. package/CHANGELOG.md +0 -253
  62. package/dist/chunk-BJBBR3QA.js.map +0 -1
  63. package/dist/chunk-NN7DATXA.js.map +0 -1
  64. package/dist/chunk-PGGWSUTM.js.map +0 -1
  65. package/dist/chunk-ZMYX5VBU.js.map +0 -1
  66. package/src/adapters/dispatcher-sse.test.ts +0 -218
  67. package/src/adapters/dispatcher-sse.ts +0 -222
  68. package/src/adapters/index.ts +0 -18
  69. package/src/adapters/mcp-catalog.test.ts +0 -213
  70. package/src/adapters/mcp-catalog.ts +0 -188
  71. package/src/adapters/prisma-rollout.test.ts +0 -153
  72. package/src/adapters/prisma-rollout.ts +0 -104
  73. package/src/agent-runtime.test.ts +0 -176
  74. package/src/artifact-stream.test.ts +0 -330
  75. package/src/artifact-stream.ts +0 -453
  76. package/src/channel-gateway.test.ts +0 -432
  77. package/src/channel-gateway.ts +0 -357
  78. package/src/code-review.test.ts +0 -501
  79. package/src/code-review.ts +0 -495
  80. package/src/command-suggestions.test.ts +0 -251
  81. package/src/command-suggestions.ts +0 -338
  82. package/src/commands.test.ts +0 -184
  83. package/src/commands.ts +0 -140
  84. package/src/commit-message.test.ts +0 -249
  85. package/src/commit-message.ts +0 -180
  86. package/src/context-compaction.test.ts +0 -522
  87. package/src/context-compaction.ts +0 -434
  88. package/src/definitions.test.ts +0 -78
  89. package/src/definitions.ts +0 -190
  90. package/src/deployment-status.test.ts +0 -215
  91. package/src/deployment-status.ts +0 -227
  92. package/src/design-context.test.ts +0 -195
  93. package/src/design-context.ts +0 -198
  94. package/src/dispatcher.test.ts +0 -234
  95. package/src/dispatcher.ts +0 -189
  96. package/src/durable-turn.test.ts +0 -209
  97. package/src/durable-turn.ts +0 -135
  98. package/src/edit-planner.test.ts +0 -204
  99. package/src/edit-planner.ts +0 -325
  100. package/src/fuzzy-match.test.ts +0 -311
  101. package/src/fuzzy-match.ts +0 -444
  102. package/src/hook-pipeline.test.ts +0 -279
  103. package/src/hook-pipeline.ts +0 -373
  104. package/src/inbound-admission.test.ts +0 -394
  105. package/src/inbound-admission.ts +0 -246
  106. package/src/index.ts +0 -47
  107. package/src/loop.test.ts +0 -161
  108. package/src/loop.ts +0 -211
  109. package/src/mcp-bridge.test.ts +0 -165
  110. package/src/mcp-bridge.ts +0 -76
  111. package/src/memory-provider.test.ts +0 -232
  112. package/src/memory-provider.ts +0 -257
  113. package/src/model.ts +0 -168
  114. package/src/permission-ruleset.test.ts +0 -301
  115. package/src/permission-ruleset.ts +0 -200
  116. package/src/policy.ts +0 -151
  117. package/src/project-repo.test.ts +0 -232
  118. package/src/project-repo.ts +0 -311
  119. package/src/protocol.ts +0 -159
  120. package/src/rollout-store-persistent.test.ts +0 -217
  121. package/src/rollout-store-persistent.ts +0 -166
  122. package/src/rollout.ts +0 -150
  123. package/src/sandbox.ts +0 -113
  124. package/src/session-share.test.ts +0 -360
  125. package/src/session-share.ts +0 -310
  126. package/src/skill-distillation.test.ts +0 -177
  127. package/src/skill-distillation.ts +0 -369
  128. package/src/skills.test.ts +0 -277
  129. package/src/skills.ts +0 -255
  130. package/src/subagents.test.ts +0 -290
  131. package/src/subagents.ts +0 -332
  132. package/src/tools.ts +0 -126
  133. package/src/workbench.test.ts +0 -0
  134. package/src/workbench.ts +0 -0
  135. package/tsconfig.json +0 -12
  136. package/tsup.config.ts +0 -33
  137. /package/dist/{chunk-5N4644PB.js.map → chunk-SD2ZJ7XG.js.map} +0 -0
@@ -1,357 +0,0 @@
1
- /**
2
- * Multi-channel gateway (WRAP — channel-adapter layer).
3
- *
4
- * A faithful, brand-neutral re-expression of a multi-channel AI assistant's
5
- * channel-adapter seam: every transport (chat platform, email, SMS, …) is
6
- * reduced to a single `ChannelAdapter` port that produces/consumes
7
- * channel-agnostic normalized contracts. Platform SDKs are NOT ported —
8
- * transport is injected through the port so this package stays dependency-free.
9
- *
10
- * Multi-tenant by construction: `tenantId` is mandatory on every operation and
11
- * fails closed (empty/blank/foreign tenant → typed error, never a silent pass).
12
- * Boundaries are validated with zod; inputs are never mutated.
13
- */
14
-
15
- import { z } from "zod";
16
-
17
- /* -------------------------------------------------------------------------- */
18
- /* Branded ChannelId */
19
- /* -------------------------------------------------------------------------- */
20
-
21
- declare const channelIdBrand: unique symbol;
22
-
23
- /** A validated, non-empty channel identifier. Construct via {@link asChannelId}. */
24
- export type ChannelId = string & { readonly [channelIdBrand]: "ChannelId" };
25
-
26
- const channelIdSchema = z.string().trim().min(1, "channelId is required (fail-closed)");
27
-
28
- /** Brand a string as a {@link ChannelId}; throws on empty/blank input. */
29
- export function asChannelId(value: string): ChannelId {
30
- return channelIdSchema.parse(value) as ChannelId;
31
- }
32
-
33
- /* -------------------------------------------------------------------------- */
34
- /* Errors */
35
- /* -------------------------------------------------------------------------- */
36
-
37
- /** Lookup/route against a channel that is not registered for the tenant. */
38
- export class UnknownChannelError extends Error {
39
- override readonly name = "UnknownChannelError";
40
- constructor(channelId: string) {
41
- super(`Unknown channel "${channelId}" for the current tenant`);
42
- }
43
- }
44
-
45
- /** A channelId is registered twice within the same tenant scope. */
46
- export class DuplicateChannelError extends Error {
47
- override readonly name = "DuplicateChannelError";
48
- constructor(channelId: string) {
49
- super(`Channel "${channelId}" is already registered for this tenant`);
50
- }
51
- }
52
-
53
- /** A tenantId on a contract does not match the resolved adapter scope. */
54
- export class TenantMismatchError extends Error {
55
- override readonly name = "TenantMismatchError";
56
- constructor(detail: string) {
57
- super(`Tenant mismatch: ${detail}`);
58
- }
59
- }
60
-
61
- /* -------------------------------------------------------------------------- */
62
- /* Tenant guard (fail-closed) */
63
- /* -------------------------------------------------------------------------- */
64
-
65
- const tenantIdSchema = z.string().trim().min(1, "tenantId is required (fail-closed)");
66
-
67
- /** Returns a trimmed, non-empty tenantId or throws (fail-closed). */
68
- function requireTenant(tenantId: string): string {
69
- return tenantIdSchema.parse(tenantId);
70
- }
71
-
72
- /* -------------------------------------------------------------------------- */
73
- /* Normalized contracts */
74
- /* -------------------------------------------------------------------------- */
75
-
76
- /** A channel-agnostic attachment reference. */
77
- export interface InboundAttachment {
78
- readonly kind: "image" | "file" | "audio";
79
- readonly ref: string;
80
- }
81
-
82
- /** A normalized, channel-agnostic inbound user message. */
83
- export interface InboundMessage {
84
- readonly tenantId: string;
85
- readonly channelId: ChannelId;
86
- readonly chatId: string;
87
- readonly threadId?: string | undefined;
88
- readonly senderId: string;
89
- readonly senderLabel?: string | undefined;
90
- readonly text: string;
91
- readonly attachments?: readonly InboundAttachment[] | undefined;
92
- readonly receivedAt: string;
93
- readonly raw: unknown;
94
- }
95
-
96
- /** A normalized, channel-agnostic outbound message. */
97
- export interface OutboundMessage {
98
- readonly tenantId: string;
99
- readonly channelId: ChannelId;
100
- readonly chatId: string;
101
- readonly threadId?: string | undefined;
102
- readonly text: string;
103
- readonly replyToMessageId?: string | undefined;
104
- }
105
-
106
- /** Capability surface a channel exposes. */
107
- export interface ChannelCapabilities {
108
- readonly threads: boolean;
109
- readonly reactions: boolean;
110
- readonly streaming: boolean;
111
- }
112
-
113
- /** Static description of a channel. */
114
- export interface ChannelMeta {
115
- readonly id: ChannelId;
116
- readonly displayName: string;
117
- readonly capabilities: ChannelCapabilities;
118
- }
119
-
120
- /* -------------------------------------------------------------------------- */
121
- /* Boundary schemas */
122
- /* -------------------------------------------------------------------------- */
123
-
124
- const attachmentSchema = z.object({
125
- kind: z.enum(["image", "file", "audio"]),
126
- ref: z.string().min(1),
127
- });
128
-
129
- const inboundSchema = z.object({
130
- tenantId: tenantIdSchema,
131
- channelId: channelIdSchema,
132
- chatId: z.string().min(1, "chatId is required"),
133
- threadId: z.string().min(1).optional(),
134
- senderId: z.string().min(1, "senderId is required"),
135
- senderLabel: z.string().min(1).optional(),
136
- text: z.string(),
137
- attachments: z.array(attachmentSchema).optional(),
138
- receivedAt: z.string().min(1, "receivedAt is required"),
139
- raw: z.unknown(),
140
- });
141
-
142
- const outboundSchema = z.object({
143
- tenantId: tenantIdSchema,
144
- channelId: channelIdSchema,
145
- chatId: z.string().min(1, "chatId is required"),
146
- threadId: z.string().min(1).optional(),
147
- text: z.string().min(1, "text is required"),
148
- replyToMessageId: z.string().min(1).optional(),
149
- });
150
-
151
- /**
152
- * Validate an adapter-produced inbound message. Throws a clearly-labelled
153
- * error (never a raw TypeError) when an adapter returns a malformed shape.
154
- */
155
- function validateInbound(candidate: unknown): InboundMessage {
156
- const parsed = inboundSchema.safeParse(candidate);
157
- if (!parsed.success) {
158
- throw new Error(
159
- `Adapter produced an invalid InboundMessage: ${parsed.error.issues
160
- .map((i) => `${i.path.join(".") || "(root)"} ${i.message}`)
161
- .join("; ")}`,
162
- );
163
- }
164
- const d = parsed.data;
165
- return {
166
- tenantId: d.tenantId,
167
- channelId: d.channelId as ChannelId,
168
- chatId: d.chatId,
169
- senderId: d.senderId,
170
- text: d.text,
171
- receivedAt: d.receivedAt,
172
- raw: d.raw,
173
- ...(d.threadId !== undefined ? { threadId: d.threadId } : {}),
174
- ...(d.senderLabel !== undefined ? { senderLabel: d.senderLabel } : {}),
175
- ...(d.attachments !== undefined ? { attachments: d.attachments } : {}),
176
- };
177
- }
178
-
179
- /** Validate an outbound message at the route boundary. */
180
- function validateOutbound(candidate: unknown): OutboundMessage {
181
- const parsed = outboundSchema.safeParse(candidate);
182
- if (!parsed.success) {
183
- throw new Error(
184
- `Invalid OutboundMessage: ${parsed.error.issues
185
- .map((i) => `${i.path.join(".") || "(root)"} ${i.message}`)
186
- .join("; ")}`,
187
- );
188
- }
189
- const d = parsed.data;
190
- return {
191
- tenantId: d.tenantId,
192
- channelId: d.channelId as ChannelId,
193
- chatId: d.chatId,
194
- text: d.text,
195
- ...(d.threadId !== undefined ? { threadId: d.threadId } : {}),
196
- ...(d.replyToMessageId !== undefined ? { replyToMessageId: d.replyToMessageId } : {}),
197
- };
198
- }
199
-
200
- /* -------------------------------------------------------------------------- */
201
- /* ChannelAdapter port */
202
- /* -------------------------------------------------------------------------- */
203
-
204
- /**
205
- * A transport adapter. Implementations wrap a concrete platform SDK (not
206
- * shipped here) and translate to/from the normalized contracts.
207
- *
208
- * - `parseInbound` MUST return `null` for events that are not user messages
209
- * (joins, typing, presence, …) and MUST NOT throw on junk payloads. The
210
- * resolved `tenantId` is stamped onto the result by the adapter.
211
- * - `sendOutbound` performs the actual delivery and returns the platform's
212
- * message id.
213
- */
214
- export interface ChannelAdapter {
215
- readonly meta: ChannelMeta;
216
- parseInbound(tenantId: string, raw: unknown): InboundMessage | null;
217
- sendOutbound(msg: OutboundMessage): Promise<{ messageId: string }>;
218
- }
219
-
220
- /* -------------------------------------------------------------------------- */
221
- /* ChannelRegistry (per-op tenantId, fail-closed) */
222
- /* -------------------------------------------------------------------------- */
223
-
224
- /**
225
- * Tenant-scoped registry of {@link ChannelAdapter}s. Every operation takes an
226
- * explicit `tenantId`; a tenant only ever sees adapters it registered. Empty,
227
- * blank, or foreign tenants fail closed.
228
- */
229
- export class ChannelRegistry {
230
- /** tenantId → (channelId → adapter). */
231
- readonly #byTenant = new Map<string, Map<string, ChannelAdapter>>();
232
-
233
- /** Register an adapter for a tenant. Duplicate channelId → throws. */
234
- register(tenantId: string, adapter: ChannelAdapter): void {
235
- const tid = requireTenant(tenantId);
236
- const channelId = asChannelId(adapter.meta.id);
237
- const tenantMap = this.#byTenant.get(tid) ?? new Map<string, ChannelAdapter>();
238
- if (tenantMap.has(channelId)) {
239
- throw new DuplicateChannelError(channelId);
240
- }
241
- const next = new Map(tenantMap);
242
- next.set(channelId, adapter);
243
- this.#byTenant.set(tid, next);
244
- }
245
-
246
- /** Resolve an adapter for a tenant. Unknown/foreign → UnknownChannelError. */
247
- lookup(tenantId: string, channelId: ChannelId): ChannelAdapter {
248
- const tid = requireTenant(tenantId);
249
- const adapter = this.#byTenant.get(tid)?.get(channelId);
250
- if (adapter === undefined) {
251
- throw new UnknownChannelError(channelId);
252
- }
253
- return adapter;
254
- }
255
-
256
- /** List the channel ids registered for a tenant (empty array if none). */
257
- list(tenantId: string): readonly string[] {
258
- const tid = requireTenant(tenantId);
259
- return [...(this.#byTenant.get(tid)?.keys() ?? [])];
260
- }
261
- }
262
-
263
- /* -------------------------------------------------------------------------- */
264
- /* replyTo (pure helper) */
265
- /* -------------------------------------------------------------------------- */
266
-
267
- export interface ReplyOptions {
268
- readonly replyToMessageId?: string | undefined;
269
- }
270
-
271
- /**
272
- * Derive a correctly-addressed {@link OutboundMessage} from an inbound message:
273
- * same channel, chat, and thread. Pure — never mutates `inbound`. Fails closed
274
- * on an empty inbound tenantId.
275
- */
276
- export function replyTo(
277
- inbound: InboundMessage,
278
- text: string,
279
- opts?: ReplyOptions,
280
- ): OutboundMessage {
281
- const tenantId = requireTenant(inbound.tenantId);
282
- return {
283
- tenantId,
284
- channelId: inbound.channelId,
285
- chatId: inbound.chatId,
286
- text,
287
- ...(inbound.threadId !== undefined ? { threadId: inbound.threadId } : {}),
288
- ...(opts?.replyToMessageId !== undefined ? { replyToMessageId: opts.replyToMessageId } : {}),
289
- };
290
- }
291
-
292
- /* -------------------------------------------------------------------------- */
293
- /* ChannelGateway */
294
- /* -------------------------------------------------------------------------- */
295
-
296
- /**
297
- * Facade over a {@link ChannelRegistry}: ingest inbound payloads into
298
- * normalized messages and route outbound replies back through the originating
299
- * channel. Junk is swallowed (null); malformed adapter output and invalid
300
- * replies surface as typed validation errors rather than crashes.
301
- */
302
- export class ChannelGateway {
303
- readonly #registry: ChannelRegistry;
304
-
305
- constructor(registry: ChannelRegistry) {
306
- this.#registry = registry;
307
- }
308
-
309
- /**
310
- * Resolve the adapter, parse the raw payload, and return the normalized
311
- * message (or `null` for non-message events). Never throws on junk; a
312
- * structurally-invalid adapter result is rejected via zod (typed error).
313
- */
314
- ingest(tenantId: string, channelId: ChannelId, raw: unknown): InboundMessage | null {
315
- const tid = requireTenant(tenantId);
316
- const adapter = this.#registry.lookup(tid, channelId);
317
- const parsed = adapter.parseInbound(tid, raw);
318
- if (parsed === null) return null;
319
- const validated = validateInbound(parsed);
320
- if (validated.tenantId !== tid) {
321
- throw new TenantMismatchError(
322
- `adapter stamped "${validated.tenantId}" but request scope is "${tid}"`,
323
- );
324
- }
325
- if (validated.channelId !== channelId) {
326
- throw new TenantMismatchError(
327
- `adapter stamped channel "${validated.channelId}" but request channel is "${channelId}"`,
328
- );
329
- }
330
- return validated;
331
- }
332
-
333
- /**
334
- * Send a reply back through the channel the conversation belongs to
335
- * (`reply.channelId`). Tenant must be non-empty and resolve within scope.
336
- */
337
- async route(reply: OutboundMessage): Promise<{ messageId: string }> {
338
- const validated = validateOutbound(reply);
339
- const adapter = this.#registry.lookup(validated.tenantId, validated.channelId);
340
- if (adapter.meta.id !== validated.channelId) {
341
- throw new TenantMismatchError(
342
- `resolved adapter "${adapter.meta.id}" does not match reply channel "${validated.channelId}"`,
343
- );
344
- }
345
- return adapter.sendOutbound(validated);
346
- }
347
-
348
- /** Full {@link ChannelMeta} for a registered channel. */
349
- describe(tenantId: string, channelId: ChannelId): ChannelMeta {
350
- return this.#registry.lookup(tenantId, channelId).meta;
351
- }
352
-
353
- /** {@link ChannelCapabilities} surfaced from the adapter meta. */
354
- capabilities(tenantId: string, channelId: ChannelId): ChannelCapabilities {
355
- return this.describe(tenantId, channelId).capabilities;
356
- }
357
- }