tnega 0.1.0 → 0.3.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 (110) hide show
  1. package/README.md +115 -189
  2. package/dist/agent.js +2907 -0
  3. package/dist/agent.js.map +7 -0
  4. package/dist/bin.js +32512 -4419
  5. package/dist/bin.js.map +4 -4
  6. package/dist/cli-runtime.js +33404 -0
  7. package/dist/cli-runtime.js.map +7 -0
  8. package/dist/coding-agent.js +19088 -0
  9. package/dist/coding-agent.js.map +7 -0
  10. package/dist/core.js +1398 -0
  11. package/dist/core.js.map +7 -0
  12. package/dist/eval.js +23905 -0
  13. package/dist/eval.js.map +7 -0
  14. package/dist/events.js +1215 -0
  15. package/dist/events.js.map +7 -0
  16. package/dist/evolve.js +18303 -0
  17. package/dist/evolve.js.map +7 -0
  18. package/dist/index.js +33087 -4450
  19. package/dist/index.js.map +4 -4
  20. package/dist/llm.js +1016 -0
  21. package/dist/llm.js.map +7 -0
  22. package/dist/services.js +1142 -0
  23. package/dist/services.js.map +7 -0
  24. package/dist/session.js +1045 -0
  25. package/dist/session.js.map +7 -0
  26. package/dist/tools.js +2577 -0
  27. package/dist/tools.js.map +7 -0
  28. package/dist/types/packages/agent/src/definition.d.ts +17 -0
  29. package/dist/types/packages/agent/src/inbox-durable.d.ts +54 -0
  30. package/dist/types/packages/agent/src/index.d.ts +7 -0
  31. package/dist/types/packages/agent/src/live.d.ts +115 -0
  32. package/dist/types/packages/agent/src/llm-service.d.ts +32 -0
  33. package/dist/types/packages/agent/src/prompt.d.ts +55 -0
  34. package/dist/types/packages/agent/src/service.d.ts +68 -0
  35. package/dist/types/packages/agent/src/types.d.ts +215 -0
  36. package/dist/types/packages/benchmark/src/bigcodebench.d.ts +6 -0
  37. package/dist/types/packages/benchmark/src/humaneval.d.ts +11 -0
  38. package/dist/types/packages/benchmark/src/index.d.ts +9 -0
  39. package/dist/types/packages/benchmark/src/manifest.d.ts +10 -0
  40. package/dist/types/packages/benchmark/src/mbpp.d.ts +10 -0
  41. package/dist/types/packages/benchmark/src/parquet.d.ts +2 -0
  42. package/dist/types/packages/benchmark/src/stdlib.d.ts +3 -0
  43. package/dist/types/packages/benchmark/src/swebench.d.ts +2 -0
  44. package/dist/types/packages/benchmark/src/types.d.ts +58 -0
  45. package/dist/types/packages/cli/src/bin.d.ts +2 -0
  46. package/dist/types/packages/cli/src/commands.d.ts +146 -0
  47. package/dist/types/packages/cli/src/config.d.ts +28 -0
  48. package/dist/types/packages/cli/src/index.d.ts +9 -0
  49. package/dist/types/packages/cli/src/profile-file.d.ts +26 -0
  50. package/dist/types/packages/cli/src/profile.d.ts +9 -0
  51. package/dist/types/packages/cli/src/server.d.ts +14 -0
  52. package/dist/types/packages/cli/src/store.d.ts +60 -0
  53. package/dist/types/packages/cli/src/yaml.d.ts +1 -0
  54. package/dist/types/packages/coding-agent/src/codingAgent.d.ts +25 -0
  55. package/dist/types/packages/coding-agent/src/index.d.ts +6 -0
  56. package/dist/types/packages/coding-agent/src/mcp.d.ts +24 -0
  57. package/dist/types/packages/coding-agent/src/plan.d.ts +13 -0
  58. package/dist/types/packages/coding-agent/src/skills.d.ts +12 -0
  59. package/dist/types/packages/coding-agent/src/slash.d.ts +28 -0
  60. package/dist/types/packages/coding-agent/src/types.d.ts +41 -0
  61. package/dist/types/packages/core/src/context.d.ts +28 -0
  62. package/dist/types/packages/core/src/events.d.ts +57 -0
  63. package/dist/types/packages/core/src/fiber.d.ts +79 -0
  64. package/dist/types/packages/core/src/index.d.ts +10 -0
  65. package/dist/types/packages/core/src/invariant.d.ts +45 -0
  66. package/dist/types/packages/core/src/logger.d.ts +48 -0
  67. package/dist/types/packages/core/src/reflect.d.ts +42 -0
  68. package/dist/types/packages/core/src/registry.d.ts +54 -0
  69. package/dist/types/packages/core/src/service.d.ts +19 -0
  70. package/dist/types/packages/core/src/utils.d.ts +54 -0
  71. package/dist/types/packages/eval/src/codingRuntime.d.ts +18 -0
  72. package/dist/types/packages/eval/src/index.d.ts +5 -0
  73. package/dist/types/packages/eval/src/policy.d.ts +8 -0
  74. package/dist/types/packages/eval/src/runner.d.ts +31 -0
  75. package/dist/types/packages/eval/src/service.d.ts +23 -0
  76. package/dist/types/packages/eval/src/strategies.d.ts +58 -0
  77. package/dist/types/packages/eval/src/trace.d.ts +7 -0
  78. package/dist/types/packages/eval/src/types.d.ts +281 -0
  79. package/dist/types/packages/eval/src/utils.d.ts +5 -0
  80. package/dist/types/packages/eval/src/workspace.d.ts +17 -0
  81. package/dist/types/packages/evolve/src/diagnose.d.ts +9 -0
  82. package/dist/types/packages/evolve/src/index.d.ts +7 -0
  83. package/dist/types/packages/evolve/src/llm.d.ts +39 -0
  84. package/dist/types/packages/evolve/src/log.d.ts +26 -0
  85. package/dist/types/packages/evolve/src/select.d.ts +3 -0
  86. package/dist/types/packages/evolve/src/service.d.ts +29 -0
  87. package/dist/types/packages/evolve/src/types.d.ts +176 -0
  88. package/dist/types/packages/llm/src/anthropic-messages.d.ts +4 -0
  89. package/dist/types/packages/llm/src/errors.d.ts +6 -0
  90. package/dist/types/packages/llm/src/index.d.ts +7 -0
  91. package/dist/types/packages/llm/src/models.d.ts +29 -0
  92. package/dist/types/packages/llm/src/openai.d.ts +4 -0
  93. package/dist/types/packages/llm/src/provider.d.ts +3 -0
  94. package/dist/types/packages/llm/src/shared.d.ts +15 -0
  95. package/dist/types/packages/llm/src/types.d.ts +13 -0
  96. package/dist/types/packages/session/src/index.d.ts +500 -0
  97. package/dist/types/packages/session/src/invariant.d.ts +33 -0
  98. package/dist/types/packages/tools/src/builtins.d.ts +25 -0
  99. package/dist/types/packages/tools/src/calc.d.ts +4 -0
  100. package/dist/types/packages/tools/src/execution.d.ts +31 -0
  101. package/dist/types/packages/tools/src/index.d.ts +107 -0
  102. package/dist/types/packages/tools/src/path.d.ts +4 -0
  103. package/dist/types/packages/tools/src/policy.d.ts +14 -0
  104. package/dist/types/src/events.d.ts +1 -0
  105. package/dist/types/src/index.d.ts +15 -0
  106. package/dist/types/src/services.d.ts +4 -0
  107. package/dist/web/assets/index-45pokiBH.js +270 -0
  108. package/dist/web/assets/index-DCLL69Kz.css +1 -0
  109. package/dist/web/index.html +13 -0
  110. package/package.json +92 -35
@@ -0,0 +1,1142 @@
1
+ import { createRequire as __tnegaCreateRequire } from 'node:module'; const require = __tnegaCreateRequire(import.meta.url);
2
+
3
+ // packages/core/src/utils.ts
4
+ var DisposableList = class {
5
+ sn = 0;
6
+ map = /* @__PURE__ */ new Map();
7
+ weak = /* @__PURE__ */ new WeakMap();
8
+ get length() {
9
+ return this.map.size;
10
+ }
11
+ push(value) {
12
+ const sn = ++this.sn;
13
+ this.map.set(sn, value);
14
+ this.weak.set(value, sn);
15
+ return () => this.map.delete(sn);
16
+ }
17
+ unshift(value) {
18
+ const sn = ++this.sn;
19
+ const entries = [...this.map.entries()];
20
+ this.map.clear();
21
+ this.map.set(sn, value);
22
+ this.weak.set(value, sn);
23
+ for (const [oldSn, oldValue] of entries) {
24
+ this.map.set(oldSn, oldValue);
25
+ }
26
+ return () => this.map.delete(sn);
27
+ }
28
+ delete(value) {
29
+ const sn = this.weak.get(value);
30
+ if (!sn) return false;
31
+ return this.map.delete(sn);
32
+ }
33
+ clear() {
34
+ const values = [...this.map.values()];
35
+ this.map.clear();
36
+ return values.reverse();
37
+ }
38
+ [Symbol.iterator]() {
39
+ return this.map.values();
40
+ }
41
+ [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
42
+ return [...this];
43
+ }
44
+ };
45
+ var symbols = {
46
+ shadow: /* @__PURE__ */ Symbol.for("tnega.shadow"),
47
+ caller: /* @__PURE__ */ Symbol.for("tnega.caller"),
48
+ receiver: /* @__PURE__ */ Symbol.for("tnega.receiver"),
49
+ original: /* @__PURE__ */ Symbol.for("tnega.original"),
50
+ metadata: /* @__PURE__ */ Symbol.for("tnega.metadata"),
51
+ initHooks: /* @__PURE__ */ Symbol.for("tnega.initHooks"),
52
+ checkProto: /* @__PURE__ */ Symbol.for("tnega.checkProto"),
53
+ effect: /* @__PURE__ */ Symbol.for("tnega.effect"),
54
+ filter: /* @__PURE__ */ Symbol.for("tnega.filter"),
55
+ isolate: /* @__PURE__ */ Symbol.for("tnega.isolate"),
56
+ intercept: /* @__PURE__ */ Symbol.for("tnega.intercept"),
57
+ init: /* @__PURE__ */ Symbol.for("tnega.init"),
58
+ check: /* @__PURE__ */ Symbol.for("tnega.check"),
59
+ config: /* @__PURE__ */ Symbol.for("tnega.config"),
60
+ invoke: /* @__PURE__ */ Symbol.for("tnega.invoke"),
61
+ extend: /* @__PURE__ */ Symbol.for("tnega.extend"),
62
+ tracker: /* @__PURE__ */ Symbol.for("tnega.tracker"),
63
+ resolveConfig: /* @__PURE__ */ Symbol.for("tnega.resolveConfig")
64
+ };
65
+ var GeneratorFunction = function* () {
66
+ }.constructor;
67
+ var AsyncGeneratorFunction = async function* () {
68
+ }.constructor;
69
+ function isConstructor(func) {
70
+ if (typeof func !== "function") return false;
71
+ if (func instanceof GeneratorFunction) return false;
72
+ if (AsyncGeneratorFunction !== Function && func instanceof AsyncGeneratorFunction) return false;
73
+ return /^class\s/.test(Function.prototype.toString.call(func));
74
+ }
75
+ function isObject(value) {
76
+ return !!value && (typeof value === "object" || typeof value === "function");
77
+ }
78
+ function isNullable(value) {
79
+ return value == null;
80
+ }
81
+ function joinPrototype(proto1, proto2) {
82
+ if (proto1 === Object.prototype) return proto2;
83
+ const result = Object.create(joinPrototype(Object.getPrototypeOf(proto1), proto2));
84
+ for (const key of Reflect.ownKeys(proto1)) {
85
+ Object.defineProperty(result, key, Reflect.getOwnPropertyDescriptor(proto1, key));
86
+ }
87
+ return result;
88
+ }
89
+ function getPropertyDescriptor(target, prop) {
90
+ let proto = target;
91
+ while (proto) {
92
+ const desc = Reflect.getOwnPropertyDescriptor(proto, prop);
93
+ if (desc) return desc;
94
+ proto = Object.getPrototypeOf(proto);
95
+ }
96
+ }
97
+ function getTraceable(ctx, value) {
98
+ if (!isObject(value)) return value;
99
+ if (Object.hasOwn(value, symbols.shadow)) {
100
+ return Object.getPrototypeOf(value);
101
+ }
102
+ const tracker = value[symbols.tracker];
103
+ if (!tracker) return value;
104
+ return createTraceable(ctx, value, tracker);
105
+ }
106
+ function withProps(target, props) {
107
+ if (!props) return target;
108
+ return new Proxy(target, {
109
+ get: (target2, prop, receiver) => {
110
+ if (prop in props && prop !== "constructor") return Reflect.get(props, prop, receiver);
111
+ return Reflect.get(target2, prop, receiver);
112
+ },
113
+ set: (target2, prop, value, receiver) => {
114
+ if (prop in props && prop !== "constructor") return Reflect.set(props, prop, value, receiver);
115
+ return Reflect.set(target2, prop, value, receiver);
116
+ }
117
+ });
118
+ }
119
+ function withProp(target, prop, value) {
120
+ return withProps(target, Object.defineProperty(/* @__PURE__ */ Object.create(null), prop, {
121
+ value,
122
+ writable: false
123
+ }));
124
+ }
125
+ function createShadow(ctx, target, property, receiver) {
126
+ if (!property) return receiver;
127
+ const origin = getPropertyDescriptor(target, property)?.value;
128
+ if (!origin) return receiver;
129
+ return withProp(receiver, property, ctx.extend({ [symbols.shadow]: origin }));
130
+ }
131
+ function createShadowMethod(ctx, value, outer, shadow) {
132
+ const proxy = new Proxy(value, {
133
+ apply: (target, thisArg, args) => {
134
+ if (thisArg === outer) thisArg = shadow;
135
+ return getTraceable(ctx, Reflect.apply(target, thisArg, args));
136
+ }
137
+ });
138
+ return proxy;
139
+ }
140
+ function createTraceable(ctx, value, tracker) {
141
+ const caller = ctx[symbols.shadow] ?? ctx;
142
+ if (ctx[symbols.shadow]) {
143
+ ctx = Object.getPrototypeOf(ctx);
144
+ }
145
+ const proxy = new Proxy(value, {
146
+ get: (target, prop, receiver) => {
147
+ if (prop === symbols.original) return target;
148
+ if (prop === symbols.caller) return caller;
149
+ if (prop === tracker.property) return ctx;
150
+ if (typeof prop === "symbol") {
151
+ return Reflect.get(target, prop, receiver);
152
+ }
153
+ if (tracker.associate && ctx.reflect.props[`${tracker.associate}.${prop}`]) {
154
+ return Reflect.get(ctx, `${tracker.associate}.${prop}`, withProp(ctx, symbols.receiver, receiver));
155
+ }
156
+ let shadow;
157
+ let innerValue;
158
+ const desc = getPropertyDescriptor(target, prop);
159
+ if (desc?.value !== void 0) {
160
+ innerValue = desc.value;
161
+ } else {
162
+ shadow = createShadow(ctx, target, tracker.property, receiver);
163
+ innerValue = Reflect.get(target, prop, shadow);
164
+ }
165
+ const innerTracker = innerValue?.[symbols.tracker];
166
+ if (innerTracker) {
167
+ return createTraceable(ctx, innerValue, innerTracker);
168
+ } else if (!tracker.noShadow && typeof innerValue === "function") {
169
+ shadow ??= createShadow(ctx, target, tracker.property, receiver);
170
+ return createShadowMethod(ctx, innerValue, receiver, shadow);
171
+ } else {
172
+ return innerValue;
173
+ }
174
+ },
175
+ set: (target, prop, value2, receiver) => {
176
+ if (prop === symbols.original || prop === symbols.caller || prop === tracker.property) return false;
177
+ if (typeof prop === "symbol") {
178
+ return Reflect.set(target, prop, value2, receiver);
179
+ }
180
+ if (tracker.associate && ctx.reflect.props[`${tracker.associate}.${prop}`]) {
181
+ return Reflect.set(ctx, `${tracker.associate}.${prop}`, value2, withProp(ctx, symbols.receiver, receiver));
182
+ }
183
+ const shadow = createShadow(ctx, target, tracker.property, receiver);
184
+ return Reflect.set(target, prop, value2, shadow);
185
+ },
186
+ apply: (target, thisArg, args) => {
187
+ const receiver = tracker.noShadow ? proxy : createShadow(ctx, target, tracker.property, proxy);
188
+ return applyTraceable(receiver, target, thisArg, args);
189
+ }
190
+ });
191
+ return proxy;
192
+ }
193
+ function applyTraceable(proxy, value, thisArg, args) {
194
+ if (!value[symbols.invoke]) return Reflect.apply(value, thisArg, args);
195
+ return value[symbols.invoke].apply(proxy, args);
196
+ }
197
+ function createCallable(name, proto, tracker) {
198
+ const self = function(...args) {
199
+ const proxy = createTraceable(self.ctx, self, tracker);
200
+ return Reflect.apply(proxy, this, args);
201
+ };
202
+ Object.defineProperty(self, "name", { value: name, configurable: true, writable: true });
203
+ return Object.setPrototypeOf(self, proto);
204
+ }
205
+ function handleError(info, reason, getOuterStack) {
206
+ const innerLines = info.error.stack.split("\n");
207
+ if (typeof reason?.stack !== "string") {
208
+ const outerError = new Error(reason);
209
+ const lines2 = outerError.stack.split("\n");
210
+ lines2.splice(1, Infinity, ...getOuterStack());
211
+ outerError.stack = lines2.join("\n");
212
+ throw outerError;
213
+ }
214
+ const lines = reason.stack.split("\n");
215
+ const index = lines.indexOf(innerLines[2]);
216
+ if (index === -1) throw reason;
217
+ lines.splice(index - info.offset, Infinity, ...getOuterStack());
218
+ reason.stack = lines.join("\n");
219
+ throw reason;
220
+ }
221
+ function composeError(callback, getOuterStack = buildOuterStack()) {
222
+ const info = { offset: 1, error: new Error() };
223
+ try {
224
+ const result = callback(info);
225
+ if (isObject(result) && "then" in result) {
226
+ return result.then(void 0, (reason) => handleError(info, reason, getOuterStack));
227
+ }
228
+ return result;
229
+ } catch (reason) {
230
+ handleError(info, reason, getOuterStack);
231
+ }
232
+ }
233
+ function buildOuterStack(offset = 0) {
234
+ const outerError = new Error();
235
+ return () => outerError.stack.split("\n").slice(3 + offset);
236
+ }
237
+
238
+ // packages/core/src/service.ts
239
+ var Service = class _Service {
240
+ constructor(ctx, name) {
241
+ this.ctx = ctx;
242
+ name ??= this.constructor.provide;
243
+ const tracker = {
244
+ associate: name,
245
+ property: "ctx"
246
+ };
247
+ const self = this[symbols.invoke] ? createCallable(name, joinPrototype(Object.getPrototypeOf(this), Function.prototype), tracker) : this;
248
+ self.ctx = ctx;
249
+ self.name = name;
250
+ Object.defineProperty(self, symbols.tracker, { value: tracker });
251
+ self.ctx.reflect.provide(name, self, this[symbols.check]);
252
+ return self;
253
+ }
254
+ ctx;
255
+ static init = symbols.init;
256
+ static check = symbols.check;
257
+ static config = symbols.config;
258
+ static invoke = symbols.invoke;
259
+ static extend = symbols.extend;
260
+ static tracker = symbols.tracker;
261
+ static resolveConfig = symbols.resolveConfig;
262
+ name;
263
+ [symbols.filter](ctx) {
264
+ return ctx[symbols.isolate][this.name] === this.ctx[symbols.isolate][this.name];
265
+ }
266
+ [symbols.extend](props) {
267
+ let self;
268
+ if (this[_Service.invoke]) {
269
+ self = createCallable(this.name, this, this[symbols.tracker]);
270
+ } else {
271
+ self = Object.create(this);
272
+ }
273
+ return Object.assign(self, props);
274
+ }
275
+ [symbols.resolveConfig](base, head) {
276
+ let intercept = this.ctx[symbols.intercept];
277
+ const configs = [];
278
+ while (this.name in intercept) {
279
+ if (Object.hasOwn(intercept, this.name)) {
280
+ configs.unshift(intercept[this.name]);
281
+ }
282
+ intercept = Object.getPrototypeOf(intercept);
283
+ }
284
+ if (base) configs.unshift(base);
285
+ if (head) configs.push(head);
286
+ if (this["Config"]?.merge) {
287
+ return this["Config"].merge(...configs);
288
+ } else {
289
+ return Object.assign({}, ...configs);
290
+ }
291
+ }
292
+ static [Symbol.hasInstance](instance) {
293
+ if (!instance) return false;
294
+ let constructor = instance.constructor;
295
+ while (constructor) {
296
+ constructor = constructor.prototype?.constructor;
297
+ if (constructor === this) return true;
298
+ constructor &&= Object.getPrototypeOf(constructor);
299
+ }
300
+ return false;
301
+ }
302
+ };
303
+
304
+ // packages/core/src/fiber.ts
305
+ var INACTIVE = "__INACTIVE__";
306
+ var ValidationError = class extends TypeError {
307
+ name = "ValidationError";
308
+ constructor(issues) {
309
+ super("invalid config:\n" + issues.map((issue) => {
310
+ if (issue.path) {
311
+ return ` - ${issue.message} (at ${issue.path.join(".")})`;
312
+ }
313
+ return ` - ${issue.message}`;
314
+ }).join("\n"));
315
+ }
316
+ };
317
+ function resolveConfig(runtime, config) {
318
+ if (!runtime.Config) return config;
319
+ const schema = runtime.Config;
320
+ if (typeof schema === "function") return schema(config);
321
+ const validate = schema?.["~standard"]?.validate;
322
+ if (typeof validate === "function") {
323
+ const result = validate(config);
324
+ if ("then" in result) {
325
+ throw new TypeError("Async config validation is not supported");
326
+ }
327
+ if (result.issues) {
328
+ throw new ValidationError(result.issues);
329
+ }
330
+ return result.value;
331
+ }
332
+ return config;
333
+ }
334
+ var CordisErrorCode = {
335
+ INACTIVE_EFFECT: "cannot create effect on inactive context"
336
+ };
337
+ var CordisError = class extends Error {
338
+ constructor(code, message) {
339
+ super(message ?? CordisErrorCode[code]);
340
+ this.code = code;
341
+ }
342
+ code;
343
+ };
344
+ var Fiber = class {
345
+ constructor(parent, config, inject, runtime, getOuterStack) {
346
+ this.parent = parent;
347
+ this.inject = inject;
348
+ this.runtime = runtime;
349
+ const collect = (dispose) => {
350
+ this._disposables.push(dispose);
351
+ };
352
+ if (runtime) {
353
+ this.uid = parent.registry.counter;
354
+ this.ctx = this.context = parent.extend({ fiber: this });
355
+ const injectEntries = Object.entries(this.inject);
356
+ if (injectEntries.length) {
357
+ this.ctx[symbols.intercept] = Object.create(parent[symbols.intercept]);
358
+ for (const [name, config2] of injectEntries) {
359
+ if (isNullable(config2)) continue;
360
+ this.ctx[symbols.intercept][name] = config2;
361
+ }
362
+ }
363
+ this._runner = {
364
+ epoch: INACTIVE,
365
+ getOuterStack,
366
+ execute: () => {
367
+ if (isConstructor(runtime.callback)) {
368
+ const instance = new runtime.callback(this.ctx, this.config);
369
+ for (const hook of instance?.[symbols.initHooks] ?? []) {
370
+ hook();
371
+ }
372
+ return instance?.[symbols.init]?.();
373
+ } else {
374
+ return runtime.callback(this.ctx, this.config);
375
+ }
376
+ },
377
+ collect
378
+ };
379
+ this.context.emit(this, "internal/plugin");
380
+ for (const name of Object.keys(this.inject)) {
381
+ this._checkImpl(name);
382
+ }
383
+ this.dispose = parent.fiber.effect(() => {
384
+ const remove = runtime.fibers.push(this);
385
+ try {
386
+ this.config = resolveConfig(runtime, config);
387
+ this._refresh();
388
+ } catch (error) {
389
+ this.ctx.logger.error(error);
390
+ this._error = error;
391
+ }
392
+ return async () => {
393
+ this.uid = null;
394
+ this.context.emit(this, "internal/plugin");
395
+ if (this.ctx.registry.has(runtime.callback)) {
396
+ remove();
397
+ if (!runtime.fibers.length) {
398
+ this.ctx.registry.delete(runtime.callback);
399
+ }
400
+ }
401
+ this._setEpoch(INACTIVE);
402
+ while (this.inertia) {
403
+ await this.inertia;
404
+ }
405
+ };
406
+ }, "ctx.plugin()");
407
+ } else {
408
+ this.uid = 0;
409
+ this.ctx = this.context = parent;
410
+ this.state = 2 /* ACTIVE */;
411
+ this.store = /* @__PURE__ */ Object.create(null);
412
+ this._runner = {
413
+ epoch: "",
414
+ getOuterStack,
415
+ execute: () => {
416
+ },
417
+ collect
418
+ };
419
+ this.dispose = () => this.restart();
420
+ }
421
+ }
422
+ parent;
423
+ inject;
424
+ runtime;
425
+ uid;
426
+ ctx;
427
+ config;
428
+ state = 0 /* PENDING */;
429
+ dispose;
430
+ store;
431
+ inertia;
432
+ _hooks = /* @__PURE__ */ Object.create(null);
433
+ _disposables = new DisposableList();
434
+ context;
435
+ _error = void 0;
436
+ _runner;
437
+ _store = /* @__PURE__ */ Object.create(null);
438
+ _restarting;
439
+ get name() {
440
+ if (this.runtime?.name) return this.runtime.name;
441
+ let fiber = this.parent.fiber;
442
+ while (fiber !== fiber.parent.fiber) {
443
+ if (fiber.runtime?.name) return fiber.runtime.name;
444
+ fiber = fiber.parent.fiber;
445
+ }
446
+ return "root";
447
+ }
448
+ assertActive() {
449
+ if (this.uid !== null) return;
450
+ throw new CordisError("INACTIVE_EFFECT");
451
+ }
452
+ _execute(runner) {
453
+ const oldEpoch = runner.epoch;
454
+ return composeError((info) => {
455
+ const safeCollect = (dispose) => {
456
+ if (typeof dispose === "function") {
457
+ runner.collect(dispose);
458
+ } else if (!isNullable(dispose)) {
459
+ throw new TypeError("Invalid effect");
460
+ }
461
+ };
462
+ const effect = runner.execute.call(this);
463
+ if (typeof effect === "function") {
464
+ return runner.collect(effect);
465
+ } else if (isNullable(effect)) {
466
+ return;
467
+ } else if (!isObject(effect)) {
468
+ throw new TypeError("Invalid effect");
469
+ } else if ("then" in effect) {
470
+ return effect.then(safeCollect);
471
+ } else if (Symbol.iterator in effect) {
472
+ info.error = new Error();
473
+ const iter = effect[Symbol.iterator]();
474
+ while (true) {
475
+ const result = iter.next();
476
+ safeCollect(result.value);
477
+ if (result.done) return;
478
+ }
479
+ } else if (Symbol.asyncIterator in effect) {
480
+ const iter = effect[Symbol.asyncIterator]();
481
+ return (async () => {
482
+ await Promise.resolve();
483
+ info.error = new Error();
484
+ while (true) {
485
+ if (runner.epoch !== oldEpoch) return;
486
+ const result = await iter.next();
487
+ safeCollect(result.value);
488
+ if (result.done) return;
489
+ }
490
+ })();
491
+ } else {
492
+ throw new TypeError("Invalid effect");
493
+ }
494
+ }, runner.getOuterStack);
495
+ }
496
+ effect(execute, label = "anonymous") {
497
+ this.assertActive();
498
+ const disposables = [];
499
+ const dispose = () => {
500
+ let task2;
501
+ for (const dispose2 of disposables.splice(0).reverse()) {
502
+ if (task2) {
503
+ task2 = task2.then(dispose2);
504
+ } else {
505
+ const result = dispose2();
506
+ if (isObject(result) && "then" in result) {
507
+ task2 = result;
508
+ }
509
+ }
510
+ }
511
+ return task2;
512
+ };
513
+ const meta = { label, children: [] };
514
+ const runner = {
515
+ execute,
516
+ epoch: true,
517
+ collect: (dispose2) => {
518
+ disposables.push(dispose2);
519
+ this._disposables.delete(dispose2);
520
+ if (dispose2[symbols.effect]) {
521
+ meta.children.push(dispose2[symbols.effect]);
522
+ }
523
+ },
524
+ getOuterStack: buildOuterStack()
525
+ };
526
+ let task;
527
+ try {
528
+ task = this._execute(runner);
529
+ } catch (reason) {
530
+ dispose();
531
+ throw reason;
532
+ }
533
+ task?.catch(dispose).catch((error) => this.ctx.logger.error(error));
534
+ const wrapper = Object.defineProperty(() => {
535
+ if (!runner.epoch) return;
536
+ runner.epoch = false;
537
+ return task ? task.then(dispose) : dispose();
538
+ }, symbols.effect, { value: meta });
539
+ const disposeAsync = () => {
540
+ if (!runner.epoch) return;
541
+ runner.epoch = false;
542
+ return dispose();
543
+ };
544
+ wrapper.then = async (onFulfilled, onRejected) => {
545
+ return Promise.resolve(task).then(() => disposeAsync).then(onFulfilled, onRejected);
546
+ };
547
+ disposables.push(this._disposables.push(wrapper));
548
+ return wrapper;
549
+ }
550
+ getEffects() {
551
+ return [...this._disposables].map((dispose) => dispose[symbols.effect]).filter(Boolean);
552
+ }
553
+ _getState() {
554
+ if (this.uid === null) return 4 /* DISPOSED */;
555
+ if (this._error) return 3 /* FAILED */;
556
+ if (this._runner.epoch !== INACTIVE) return 2 /* ACTIVE */;
557
+ return 0 /* PENDING */;
558
+ }
559
+ _updateState(callback) {
560
+ const oldState = this.state;
561
+ this.state = callback?.() ?? this._getState();
562
+ if (oldState === this.state) return;
563
+ this.context.emit(this, "internal/status", oldState);
564
+ if (oldState !== 2 /* ACTIVE */ && this.state !== 2 /* ACTIVE */) return;
565
+ for (const key of Reflect.ownKeys(this.ctx.reflect.store)) {
566
+ const impl = this.ctx.reflect.store[key];
567
+ if (!impl) continue;
568
+ if (impl.fiber !== this) continue;
569
+ this.ctx.reflect.notify([impl.name]);
570
+ }
571
+ }
572
+ _checkImpl(name) {
573
+ const impl = this.ctx.reflect._getImpl(name, true);
574
+ if (!impl) return delete this._store[name];
575
+ try {
576
+ if (impl.check && !impl.check.call(getTraceable(this.ctx, impl.value))) {
577
+ return delete this._store[name];
578
+ }
579
+ } catch (error) {
580
+ impl.fiber.ctx.logger.error(error);
581
+ return delete this._store[name];
582
+ }
583
+ this._store[name] = impl;
584
+ }
585
+ _refresh() {
586
+ let epoch = "";
587
+ for (const name of Object.keys(this.inject)) {
588
+ const impl = this._store[name];
589
+ if (!impl) {
590
+ epoch = INACTIVE;
591
+ break;
592
+ }
593
+ epoch += ":" + impl.fiber.uid;
594
+ }
595
+ this._setEpoch(epoch);
596
+ }
597
+ _setEpoch(epoch) {
598
+ const oldEpoch = this._runner.epoch;
599
+ if (epoch === oldEpoch) return;
600
+ this._runner.epoch = epoch;
601
+ if (this.inertia) return;
602
+ this._updateState(() => {
603
+ if (epoch !== INACTIVE && oldEpoch === INACTIVE) {
604
+ this.inertia = this._reload();
605
+ return 1 /* LOADING */;
606
+ } else {
607
+ this.inertia = this._unload();
608
+ return 5 /* UNLOADING */;
609
+ }
610
+ });
611
+ }
612
+ async _reload() {
613
+ this.store = { ...this._store };
614
+ const oldEpoch = this._runner.epoch;
615
+ try {
616
+ await Promise.resolve();
617
+ await this._execute(this._runner);
618
+ } catch (reason) {
619
+ this.ctx.logger.error(reason);
620
+ this._error = reason;
621
+ this._runner.epoch = INACTIVE;
622
+ }
623
+ this._updateState(() => {
624
+ if (this._runner.epoch === oldEpoch) {
625
+ this.inertia = void 0;
626
+ } else {
627
+ this.inertia = this._unload();
628
+ return 5 /* UNLOADING */;
629
+ }
630
+ });
631
+ }
632
+ async _unload() {
633
+ await Promise.all(this._disposables.clear().map(async (dispose) => {
634
+ try {
635
+ await composeError(async (info) => {
636
+ await Promise.resolve();
637
+ info.error = new Error();
638
+ await dispose();
639
+ }, this._runner.getOuterStack);
640
+ } catch (reason) {
641
+ this.ctx.logger.error(reason);
642
+ }
643
+ }));
644
+ this.store = void 0;
645
+ this._updateState(() => {
646
+ if (this._runner.epoch === INACTIVE) {
647
+ this.inertia = void 0;
648
+ } else {
649
+ this.inertia = this._reload();
650
+ return 1 /* LOADING */;
651
+ }
652
+ });
653
+ }
654
+ async await() {
655
+ while (this.inertia || this._restarting) {
656
+ await Promise.all([this.inertia, this._restarting].filter((task) => !!task));
657
+ }
658
+ if (this._error) throw this._error;
659
+ return this;
660
+ }
661
+ async restart() {
662
+ const fiber = this.ctx.fiber;
663
+ fiber.assertActive();
664
+ if (fiber._restarting) return fiber._restarting.then(() => void 0);
665
+ let resolve;
666
+ const restarting = new Promise((resolvePromise) => {
667
+ resolve = resolvePromise;
668
+ });
669
+ fiber._restarting = restarting;
670
+ try {
671
+ await this._runRestart(fiber);
672
+ } finally {
673
+ fiber._restarting = void 0;
674
+ resolve();
675
+ }
676
+ }
677
+ async _runRestart(fiber) {
678
+ if (fiber.inertia) {
679
+ fiber._setEpoch(INACTIVE);
680
+ while (fiber.inertia) await fiber.inertia;
681
+ }
682
+ fiber._setEpoch(INACTIVE);
683
+ fiber._refresh();
684
+ while (fiber.inertia) await fiber.inertia;
685
+ }
686
+ update(config, noSave = false) {
687
+ const fiber = this.ctx.fiber;
688
+ fiber.assertActive();
689
+ config = resolveConfig(fiber.runtime, config);
690
+ fiber.context.waterfall(fiber.context, "internal/update", config, noSave, () => {
691
+ fiber.config = config;
692
+ fiber._error = void 0;
693
+ return fiber.restart();
694
+ });
695
+ }
696
+ };
697
+
698
+ // packages/core/src/registry.ts
699
+ function isApplicable(object) {
700
+ return object && typeof object === "object" && typeof object.apply === "function";
701
+ }
702
+ function Inject(name, config) {
703
+ return function(value, decorator) {
704
+ if (decorator.kind === "class") {
705
+ if (!Object.hasOwn(value, "inject")) {
706
+ Object.defineProperty(value, "inject", {
707
+ value: Object.create(Object.getPrototypeOf(value).inject ?? null)
708
+ });
709
+ Object.defineProperty(value.inject, symbols.checkProto, { value: true });
710
+ }
711
+ value.inject[name] = config;
712
+ } else if (decorator.kind === "method") {
713
+ const inject = (value[symbols.metadata] ??= {}).inject ??= /* @__PURE__ */ Object.create(null);
714
+ inject[name] = config;
715
+ decorator.addInitializer(function() {
716
+ const property = this[symbols.tracker]?.property;
717
+ (this[symbols.initHooks] ??= []).push(() => {
718
+ this.ctx.inject(inject, (ctx) => {
719
+ return value.call(property ? withProps(this, { [property]: ctx }) : this);
720
+ });
721
+ });
722
+ });
723
+ } else {
724
+ throw new Error("@Inject() can only be used on class or class methods");
725
+ }
726
+ };
727
+ }
728
+ function resolveInject(inject, result = /* @__PURE__ */ Object.create(null)) {
729
+ if (!inject) return result;
730
+ if (Array.isArray(inject)) {
731
+ for (const name of inject) {
732
+ result[name] = null;
733
+ }
734
+ } else if (Reflect.has(inject, symbols.checkProto)) {
735
+ Object.assign(result, resolveInject(Object.getPrototypeOf(inject)));
736
+ for (const name of Object.keys(inject)) {
737
+ result[name] = inject[name] ?? null;
738
+ }
739
+ } else {
740
+ for (const name of Object.keys(inject)) {
741
+ result[name] = inject[name] ?? null;
742
+ }
743
+ }
744
+ return result;
745
+ }
746
+ var RegistryService = class {
747
+ constructor(ctx) {
748
+ this.ctx = ctx;
749
+ Object.defineProperty(this, symbols.tracker, {
750
+ value: { property: "ctx", noShadow: true }
751
+ });
752
+ }
753
+ ctx;
754
+ _counter = 0;
755
+ _internal = /* @__PURE__ */ new Map();
756
+ get counter() {
757
+ return ++this._counter;
758
+ }
759
+ get size() {
760
+ return this._internal.size;
761
+ }
762
+ resolve(plugin) {
763
+ try {
764
+ if (typeof plugin === "function") return plugin;
765
+ if (isApplicable(plugin)) return plugin.apply;
766
+ } catch {
767
+ }
768
+ }
769
+ get(plugin) {
770
+ const key = this.resolve(plugin);
771
+ return key && this._internal.get(key);
772
+ }
773
+ has(plugin) {
774
+ const key = this.resolve(plugin);
775
+ return !!key && this._internal.has(key);
776
+ }
777
+ delete(plugin) {
778
+ const key = this.resolve(plugin);
779
+ const runtime = key && this._internal.get(key);
780
+ if (!runtime) return;
781
+ this._internal.delete(key);
782
+ for (const fiber of [...runtime.fibers]) {
783
+ fiber.dispose();
784
+ }
785
+ return runtime;
786
+ }
787
+ keys() {
788
+ return this._internal.keys();
789
+ }
790
+ values() {
791
+ return this._internal.values();
792
+ }
793
+ entries() {
794
+ return this._internal.entries();
795
+ }
796
+ forEach(callback) {
797
+ return this._internal.forEach(callback);
798
+ }
799
+ inject(inject, callback) {
800
+ const name = callback.name || void 0;
801
+ return this.plugin(Object.assign(
802
+ { inject, apply: callback },
803
+ name ? { name } : {}
804
+ ));
805
+ }
806
+ plugin(plugin, config, getOuterStack = buildOuterStack()) {
807
+ const callback = this.resolve(plugin);
808
+ if (!callback) throw new Error('invalid plugin, expect function or object with an "apply" method, received ' + typeof plugin);
809
+ this.ctx.fiber.assertActive();
810
+ let runtime = this._internal.get(callback);
811
+ if (!runtime) {
812
+ let name = plugin.name;
813
+ if (name === "apply") name = void 0;
814
+ runtime = {
815
+ callback,
816
+ fibers: new DisposableList(),
817
+ ...name ? { name } : {},
818
+ ...plugin.Config ? { Config: plugin.Config } : {}
819
+ };
820
+ this._internal.set(callback, runtime);
821
+ }
822
+ const fiber = new Fiber(this.ctx, config, resolveInject(plugin.inject), runtime, getOuterStack);
823
+ const wrapped = Object.create(fiber);
824
+ wrapped.then = (onFulfilled, onRejected) => {
825
+ return fiber.await().then(onFulfilled, onRejected);
826
+ };
827
+ return wrapped;
828
+ }
829
+ };
830
+
831
+ // packages/core/src/reflect.ts
832
+ function enhanceError(error) {
833
+ const lines = error.stack.split("\n");
834
+ lines.splice(0, 2, `Error: ${error.message}`);
835
+ error.stack = lines.join("\n");
836
+ return error;
837
+ }
838
+ var RESERVED_WORDS = ["prototype", "then"];
839
+ function isSpecialProperty(prop) {
840
+ return typeof prop === "symbol" || RESERVED_WORDS.includes(prop) || parseInt(prop).toString() === prop || prop.startsWith("_");
841
+ }
842
+ var ReflectService = class {
843
+ constructor(ctx) {
844
+ this.ctx = ctx;
845
+ Object.defineProperty(this, symbols.tracker, {
846
+ value: { property: "ctx", noShadow: true }
847
+ });
848
+ this.mixin("reflect", ["get", "set", "provide", "accessor", "mixin"]);
849
+ this.mixin("fiber", ["runtime", "effect"]);
850
+ this.mixin("registry", ["inject", "plugin"]);
851
+ this.mixin("events", [
852
+ "on",
853
+ "once",
854
+ "parallel",
855
+ "emit",
856
+ "serial",
857
+ "bail",
858
+ "waterfall",
859
+ "waterfallAsync"
860
+ ]);
861
+ }
862
+ ctx;
863
+ static handler = {
864
+ get: (target, prop, ctx) => {
865
+ if (isSpecialProperty(prop)) {
866
+ return Reflect.get(target, prop, ctx);
867
+ }
868
+ if (Reflect.has(target, prop)) {
869
+ return getTraceable(ctx, Reflect.get(target, prop, ctx));
870
+ }
871
+ const error = new Error(`cannot get property "${String(prop)}" without inject`);
872
+ try {
873
+ const def = target.reflect.props[prop];
874
+ if (def?.type === "accessor") {
875
+ return def.get.call(ctx, ctx[symbols.receiver], error);
876
+ }
877
+ if (!ctx.fiber.runtime) return ctx.reflect.get(prop, false);
878
+ return ctx.events.waterfall(ctx, "internal/get", prop, error, () => {
879
+ const key = target[symbols.isolate][prop];
880
+ let fiber = (ctx[symbols.shadow] ?? ctx).fiber;
881
+ while (true) {
882
+ const impl = fiber.store?.[prop];
883
+ if (impl) return getTraceable(ctx, impl.value);
884
+ if (prop in fiber.inject) {
885
+ error.message = `cannot get required service "${String(prop)}" in inactive context`;
886
+ throw error;
887
+ }
888
+ if (!fiber.runtime) throw error;
889
+ if (fiber.parent[symbols.isolate][prop] !== key) throw error;
890
+ fiber = fiber.parent.fiber;
891
+ }
892
+ });
893
+ } catch (e) {
894
+ throw e === error ? enhanceError(e) : e;
895
+ }
896
+ },
897
+ set: (target, prop, value, ctx) => {
898
+ if (isSpecialProperty(prop)) {
899
+ return Reflect.set(target, prop, value, ctx);
900
+ }
901
+ const error = new Error(`cannot set property "${String(prop)}" without provide`);
902
+ const def = target.reflect.props[prop];
903
+ try {
904
+ if (def?.type === "accessor") {
905
+ if (!def.set) return false;
906
+ return def.set.call(ctx, value, ctx[symbols.receiver], error);
907
+ }
908
+ if (!def && !ctx.fiber.runtime) {
909
+ return Reflect.set(target, prop, value, ctx);
910
+ }
911
+ return ctx.events.waterfall(ctx, "internal/set", prop, value, error, () => {
912
+ if (!def) throw error;
913
+ return ctx.reflect.set(prop, value);
914
+ });
915
+ } catch (e) {
916
+ throw e === error ? enhanceError(e) : e;
917
+ }
918
+ },
919
+ has: (target, prop) => {
920
+ if (isSpecialProperty(prop)) {
921
+ return Reflect.has(target, prop);
922
+ }
923
+ if (Reflect.has(target, prop)) return true;
924
+ return !!target.reflect.props[prop];
925
+ }
926
+ };
927
+ store = /* @__PURE__ */ Object.create(null);
928
+ props = /* @__PURE__ */ Object.create(null);
929
+ get(name, strict = true) {
930
+ return getTraceable(this.ctx, this._getImpl(name, strict)?.value);
931
+ }
932
+ _getImpl(name, strict = true) {
933
+ const key = this.ctx[symbols.isolate][name];
934
+ const impl = key && this.store[key];
935
+ if (!impl) return;
936
+ if (strict && impl.fiber.state !== 2 /* ACTIVE */) return;
937
+ return impl;
938
+ }
939
+ set(name, value) {
940
+ const key = this.ctx[symbols.isolate][name];
941
+ const impl = key && this.store[key];
942
+ if (!impl) {
943
+ throw new Error(`cannot set property "${name}" without provide`);
944
+ }
945
+ if (impl.fiber !== this.ctx.fiber) {
946
+ throw new Error(`cannot set property "${name}" in multiple fibers`);
947
+ }
948
+ impl.value = value;
949
+ return true;
950
+ }
951
+ provide(name, value, check) {
952
+ return this.ctx.fiber.effect(() => {
953
+ if (!this.props[name]) {
954
+ this.props[name] = { type: "service" };
955
+ } else if (this.props[name].type !== "service") {
956
+ throw new Error(`property "${name}" is already declared as ${this.props[name].type}`);
957
+ }
958
+ this.props[name] = { type: "service" };
959
+ this.ctx.root[symbols.isolate][name] ??= Symbol(name);
960
+ const key = this.ctx[symbols.isolate][name];
961
+ const impl = {
962
+ name,
963
+ value,
964
+ fiber: this.ctx.fiber,
965
+ ...check ? { check } : {}
966
+ };
967
+ if (key && this.store[key]) {
968
+ throw new Error(`service "${name}" has been registered at <${this.store[key].fiber.name}>`);
969
+ }
970
+ this.store[key] = impl;
971
+ this.ctx.fiber.store[name] = impl;
972
+ if (this.ctx.fiber.state === 2 /* ACTIVE */) {
973
+ this.notify([name]);
974
+ }
975
+ return async () => {
976
+ delete this.store[key];
977
+ const fibers = this.notify([name]);
978
+ await Promise.allSettled(fibers.map((fiber) => fiber.await()));
979
+ delete this.ctx.fiber.store[name];
980
+ };
981
+ }, `ctx.provide(${JSON.stringify(name)})`);
982
+ }
983
+ notify(names, filter = (ctx, name) => ctx[symbols.isolate][name] === this.ctx[symbols.isolate][name]) {
984
+ const fibers = [];
985
+ for (const runtime of this.ctx.registry.values()) {
986
+ for (const fiber of runtime.fibers) {
987
+ let hasUpdate = false;
988
+ for (const name of names) {
989
+ if (!(name in fiber.inject)) continue;
990
+ if (!filter(fiber.ctx, name)) continue;
991
+ hasUpdate = true;
992
+ fiber._checkImpl(name);
993
+ }
994
+ if (!hasUpdate) continue;
995
+ fiber._refresh();
996
+ fibers.push(fiber);
997
+ }
998
+ }
999
+ for (const name of names) {
1000
+ const self = Object.create(this.ctx);
1001
+ self[symbols.filter] = (target) => filter(target, name);
1002
+ this.ctx.events.emit(self, "internal/service", name, this._getImpl(name, false)?.value);
1003
+ }
1004
+ return fibers;
1005
+ }
1006
+ accessor(name, options) {
1007
+ return this.ctx.fiber.effect(() => {
1008
+ if (name in this.props) {
1009
+ throw new Error(`property "${name}" is already declared as ${this.props[name].type}`);
1010
+ }
1011
+ this.props[name] = { type: "accessor", ...options };
1012
+ return () => delete this.props[name];
1013
+ }, `ctx.accessor(${JSON.stringify(name)})`);
1014
+ }
1015
+ mixin(source, mixins) {
1016
+ return this.ctx.fiber.effect(() => {
1017
+ const entries = Array.isArray(mixins) ? mixins.map((key) => [key, key]) : Object.entries(mixins);
1018
+ const getTarget = (target) => target[source];
1019
+ return entries.map(([key, value]) => {
1020
+ return this.accessor(value, {
1021
+ get(receiver) {
1022
+ const service = getTarget(this);
1023
+ if (service == null) return service;
1024
+ const mixin = receiver ? withProps(receiver, service) : service;
1025
+ const result = Reflect.get(service, key, mixin);
1026
+ if (typeof result !== "function") return result;
1027
+ return result.bind(mixin ?? service);
1028
+ },
1029
+ set(value2, receiver) {
1030
+ const service = getTarget(this);
1031
+ const mixin = receiver ? withProps(receiver, service) : service;
1032
+ return Reflect.set(service, key, value2, mixin);
1033
+ }
1034
+ });
1035
+ });
1036
+ }, `ctx.mixin(${JSON.stringify(source)})`);
1037
+ }
1038
+ trace(value) {
1039
+ return getTraceable(this.ctx, value);
1040
+ }
1041
+ bind(callback) {
1042
+ return new Proxy(callback, {
1043
+ apply: (target, thisArg, args) => {
1044
+ return Reflect.apply(target, this.trace(thisArg), args.map((arg) => this.trace(arg)));
1045
+ },
1046
+ construct: (target, args, newTarget) => {
1047
+ return Reflect.construct(target, args.map((arg) => this.trace(arg)), newTarget);
1048
+ }
1049
+ });
1050
+ }
1051
+ };
1052
+
1053
+ // packages/core/src/logger.ts
1054
+ var Logger = class {
1055
+ constructor(options, service) {
1056
+ this.service = service;
1057
+ this.name = options.name;
1058
+ this.level = options.level;
1059
+ this.error = this._method("error");
1060
+ this.info = this._method("info");
1061
+ this.warn = this._method("warn");
1062
+ this.debug = this._method("debug");
1063
+ }
1064
+ service;
1065
+ name;
1066
+ level;
1067
+ error;
1068
+ info;
1069
+ warn;
1070
+ debug;
1071
+ _method(type) {
1072
+ return (...args) => this.service._log({ name: this.name, type, args });
1073
+ }
1074
+ };
1075
+ var LoggerService = class {
1076
+ buffer = [];
1077
+ ctx;
1078
+ _snMessage = 0;
1079
+ _snExporter = 0;
1080
+ exporters = /* @__PURE__ */ new Map();
1081
+ constructor(ctx) {
1082
+ const tracker = {
1083
+ property: "ctx",
1084
+ noShadow: true
1085
+ };
1086
+ const self = createCallable("logger", joinPrototype(Object.getPrototypeOf(this), Function.prototype), tracker);
1087
+ Object.assign(self, this);
1088
+ self.ctx = ctx;
1089
+ Object.defineProperty(self, symbols.tracker, { value: tracker });
1090
+ self.exporter({
1091
+ export: (message) => {
1092
+ self.buffer.push(message);
1093
+ }
1094
+ });
1095
+ ctx.reflect.provide("logger", self);
1096
+ return self;
1097
+ }
1098
+ exporter(exporter) {
1099
+ return this.ctx.fiber.effect(() => {
1100
+ const id = ++this._snExporter;
1101
+ this.exporters.set(id, exporter);
1102
+ return () => this.exporters.delete(id);
1103
+ }, "ctx.logger.exporter()");
1104
+ }
1105
+ [symbols.invoke](name) {
1106
+ const caller = this[symbols.caller];
1107
+ const fiber = (caller ?? this.ctx).fiber;
1108
+ return new Logger({
1109
+ name: name ?? fiber.name
1110
+ }, this);
1111
+ }
1112
+ _log(message) {
1113
+ const sn = ++this._snMessage;
1114
+ const ts = Date.now();
1115
+ for (const exporter of this.exporters.values()) {
1116
+ if (exporter.level != null && exporter.level < 0) continue;
1117
+ exporter.export({ sn, ts, ...message });
1118
+ }
1119
+ }
1120
+ error(...args) {
1121
+ return this().error(...args);
1122
+ }
1123
+ info(...args) {
1124
+ return this().info(...args);
1125
+ }
1126
+ warn(...args) {
1127
+ return this().warn(...args);
1128
+ }
1129
+ debug(...args) {
1130
+ return this().debug(...args);
1131
+ }
1132
+ };
1133
+ export {
1134
+ Inject,
1135
+ Logger,
1136
+ LoggerService,
1137
+ ReflectService,
1138
+ RegistryService,
1139
+ Service,
1140
+ resolveInject
1141
+ };
1142
+ //# sourceMappingURL=services.js.map