@nebutra/agent-runtime 0.2.1 → 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 (101) hide show
  1. package/LICENSE +21 -676
  2. package/dist/adapters/index.d.ts +24 -5
  3. package/dist/adapters/index.js +27 -0
  4. package/dist/adapters/index.js.map +1 -1
  5. package/dist/chunk-Q62VKHIT.js +178 -0
  6. package/dist/chunk-Q62VKHIT.js.map +1 -0
  7. package/dist/{chunk-MUF7ZZTO.js → chunk-R5HOSQUW.js} +2 -1
  8. package/dist/{chunk-MUF7ZZTO.js.map → chunk-R5HOSQUW.js.map} +1 -1
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +14 -17
  11. package/dist/index.js.map +1 -1
  12. package/dist/orchestration.d.ts +42 -1
  13. package/dist/orchestration.js +7 -3
  14. package/dist/sandbox.js +1 -1
  15. package/package.json +80 -30
  16. package/.turbo/turbo-build.log +0 -130
  17. package/.turbo/turbo-test.log +0 -47
  18. package/.turbo/turbo-typecheck.log +0 -4
  19. package/CHANGELOG.md +0 -250
  20. package/dist/chunk-MX2WL43P.js +0 -90
  21. package/dist/chunk-MX2WL43P.js.map +0 -1
  22. package/examples/pulsar-quickstart.ts +0 -35
  23. package/examples/resume-branch.ts +0 -45
  24. package/examples/subagent-fanout.ts +0 -20
  25. package/src/adapters/dispatcher-sse.test.ts +0 -218
  26. package/src/adapters/dispatcher-sse.ts +0 -222
  27. package/src/adapters/index.ts +0 -18
  28. package/src/adapters/mcp-catalog.test.ts +0 -213
  29. package/src/adapters/mcp-catalog.ts +0 -188
  30. package/src/adapters/prisma-rollout.test.ts +0 -153
  31. package/src/adapters/prisma-rollout.ts +0 -104
  32. package/src/agent-runtime.test.ts +0 -176
  33. package/src/artifact-stream.test.ts +0 -330
  34. package/src/artifact-stream.ts +0 -453
  35. package/src/channel-gateway.test.ts +0 -432
  36. package/src/channel-gateway.ts +0 -357
  37. package/src/cli.ts +0 -49
  38. package/src/code-review.test.ts +0 -501
  39. package/src/code-review.ts +0 -495
  40. package/src/command-suggestions.test.ts +0 -251
  41. package/src/command-suggestions.ts +0 -338
  42. package/src/commands.test.ts +0 -184
  43. package/src/commands.ts +0 -140
  44. package/src/commit-message.test.ts +0 -249
  45. package/src/commit-message.ts +0 -180
  46. package/src/context-compaction.test.ts +0 -522
  47. package/src/context-compaction.ts +0 -438
  48. package/src/definitions.test.ts +0 -78
  49. package/src/definitions.ts +0 -190
  50. package/src/deployment-status.test.ts +0 -215
  51. package/src/deployment-status.ts +0 -227
  52. package/src/design-context.test.ts +0 -195
  53. package/src/design-context.ts +0 -198
  54. package/src/dispatcher.test.ts +0 -234
  55. package/src/dispatcher.ts +0 -189
  56. package/src/durable-turn.test.ts +0 -209
  57. package/src/durable-turn.ts +0 -135
  58. package/src/edit-planner.test.ts +0 -204
  59. package/src/edit-planner.ts +0 -325
  60. package/src/fuzzy-match.test.ts +0 -311
  61. package/src/fuzzy-match.ts +0 -444
  62. package/src/hook-pipeline.test.ts +0 -279
  63. package/src/hook-pipeline.ts +0 -373
  64. package/src/inbound-admission.test.ts +0 -394
  65. package/src/inbound-admission.ts +0 -246
  66. package/src/index.ts +0 -49
  67. package/src/loop.test.ts +0 -161
  68. package/src/loop.ts +0 -211
  69. package/src/mcp-bridge.test.ts +0 -165
  70. package/src/mcp-bridge.ts +0 -81
  71. package/src/memory-provider.test.ts +0 -232
  72. package/src/memory-provider.ts +0 -257
  73. package/src/model.ts +0 -168
  74. package/src/orchestration.test.ts +0 -53
  75. package/src/orchestration.ts +0 -146
  76. package/src/permission-ruleset.test.ts +0 -301
  77. package/src/permission-ruleset.ts +0 -1
  78. package/src/policy.ts +0 -151
  79. package/src/project-repo.test.ts +0 -232
  80. package/src/project-repo.ts +0 -311
  81. package/src/protocol.ts +0 -159
  82. package/src/pulsar.test.ts +0 -156
  83. package/src/pulsar.ts +0 -322
  84. package/src/rollout-store-persistent.test.ts +0 -217
  85. package/src/rollout-store-persistent.ts +0 -166
  86. package/src/rollout.ts +0 -150
  87. package/src/sandbox.ts +0 -113
  88. package/src/session-share.test.ts +0 -360
  89. package/src/session-share.ts +0 -310
  90. package/src/skill-distillation.test.ts +0 -177
  91. package/src/skill-distillation.ts +0 -369
  92. package/src/skills.test.ts +0 -277
  93. package/src/skills.ts +0 -277
  94. package/src/subagents.test.ts +0 -290
  95. package/src/subagents.ts +0 -332
  96. package/src/tools.test.ts +0 -12
  97. package/src/tools.ts +0 -129
  98. package/src/workbench.test.ts +0 -0
  99. package/src/workbench.ts +0 -0
  100. package/tsconfig.json +0 -12
  101. package/tsup.config.ts +0 -36
@@ -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
- }
package/src/cli.ts DELETED
@@ -1,49 +0,0 @@
1
- import { readCapabilityDebug } from "@nebutra/capability-kit/debug";
2
-
3
- const command = process.argv[2] ?? "doctor";
4
-
5
- if (command === "doctor") {
6
- process.stdout.write(
7
- `${JSON.stringify(
8
- {
9
- capability: "agent-runtime",
10
- ok: true,
11
- checks: ["thread-turn-item", "rollout-store", "pulsar-facade", "subagent-briefs"],
12
- },
13
- null,
14
- 2,
15
- )}\n`,
16
- );
17
- } else if (command === "debug" || command === "replay") {
18
- const threadId = process.argv[3];
19
- process.stdout.write(
20
- `${JSON.stringify(
21
- {
22
- capability: "agent-runtime",
23
- threadId,
24
- entries: await readCapabilityDebug("agent-runtime", { limit: 20 }),
25
- suggestion:
26
- "Use a RolloutStore-backed host to replay full thread events; this CLI reports local debug lines.",
27
- },
28
- null,
29
- 2,
30
- )}\n`,
31
- );
32
- } else if (command === "subagent-trace" || command === "subagent-cost") {
33
- const threadId = process.argv[3];
34
- process.stdout.write(
35
- `${JSON.stringify(
36
- {
37
- capability: "agent-runtime",
38
- command,
39
- threadId,
40
- entries: await readCapabilityDebug("agent-runtime", { limit: 20 }),
41
- },
42
- null,
43
- 2,
44
- )}\n`,
45
- );
46
- } else {
47
- process.stderr.write(`Unknown agent-runtime command: ${command}\n`);
48
- process.exitCode = 1;
49
- }