@m4trix/core 0.10.0 → 0.11.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.
@@ -401,6 +401,77 @@ declare class AgentNetwork {
401
401
  private runScoped;
402
402
  }
403
403
 
404
+ /**
405
+ * Branded type for layer/dependency names. Enforces camelCase at runtime via refinement.
406
+ * Used internally for parsing, validation, and uniqueness enforcement across layers.
407
+ */
408
+ type LayerName = string & Brand.Brand<'LayerName'>;
409
+ declare const LayerName: Brand.Brand.Constructor<LayerName>;
410
+ /** Definition of a single skill dependency with a branded name and schema shape */
411
+ type SkillDependencyDef<N extends string, PS extends Schema.Schema.Any> = {
412
+ readonly _tag: 'SkillDependencyDef';
413
+ readonly name: LayerName;
414
+ readonly _name: N;
415
+ readonly shape: PS;
416
+ readonly decode: (u: unknown) => Effect.Effect<Schema.Schema.Type<PS>, ParseError>;
417
+ };
418
+ /** Build layers object type from a tuple of dependency definitions */
419
+ type DependenciesToLayers<T> = T extends SkillDependencyDef<infer N, infer PS> ? {
420
+ [K in N]: Schema.Schema.Type<PS>;
421
+ } : never;
422
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
423
+ /** Build layers object from union of dependency types */
424
+ type LayersFromDeps<T extends SkillDependencyDef<string, Schema.Schema.Any>> = [T] extends [never] ? Record<string, never> : UnionToIntersection<DependenciesToLayers<T>>;
425
+ declare const SkillDependency: {
426
+ of<const N extends string, PS extends Schema.Schema.Any>(config: {
427
+ name: N;
428
+ shape: PS;
429
+ }): SkillDependencyDef<N, PS>;
430
+ };
431
+ /** Minimal runtime options placeholder (logger, trace, etc. can be extended later) */
432
+ type SkillRuntimeOptions = Record<string, unknown>;
433
+ /** Context passed to the define callback */
434
+ type SkillDefineContext<TIn, TChunk, TLayers> = {
435
+ input: TIn;
436
+ emit: (chunk: TChunk) => void;
437
+ layers: TLayers;
438
+ };
439
+ /** Define function signature */
440
+ type DefineFn<TIn, TChunk, TDone, TLayers> = (ctx: SkillDefineContext<TIn, TChunk, TLayers>) => TDone | Promise<TDone>;
441
+ /** Final executable skill instance */
442
+ type SkillInstance<TInput, TChunk, TDone, TLayers> = {
443
+ invokeStream: (input: unknown, runtime?: {
444
+ layers: TLayers;
445
+ } & SkillRuntimeOptions) => AsyncIterable<TChunk | {
446
+ _tag: 'Done';
447
+ done: TDone;
448
+ }>;
449
+ /** Input is decoded to TInput before being passed to the skill logic */
450
+ invoke: (input: unknown, runtime?: {
451
+ layers: TLayers;
452
+ } & SkillRuntimeOptions) => Promise<{
453
+ chunks: TChunk[];
454
+ done: TDone;
455
+ }>;
456
+ } & {
457
+ readonly _input?: TInput;
458
+ };
459
+ declare class Skill<TInput = unknown, TChunk = unknown, TDone = unknown, TDeps extends SkillDependencyDef<string, Schema.Schema.Any> = never> {
460
+ private _inputSchema;
461
+ private _chunkSchema;
462
+ private _doneSchema;
463
+ private _layers;
464
+ private _defineFn;
465
+ private constructor();
466
+ private getState;
467
+ static of(_options?: SkillRuntimeOptions): Skill<unknown, unknown, unknown, never>;
468
+ input<ISchema extends Schema.Schema.Any>(schema: ISchema): Skill<Schema.Schema.Type<ISchema>, TChunk, TDone, TDeps>;
469
+ chunk<CSchema extends Schema.Schema.Any>(schema: CSchema): Skill<TInput, Schema.Schema.Type<CSchema>, TDone, TDeps>;
470
+ done<DSchema extends Schema.Schema.Any>(schema: DSchema): Skill<TInput, TChunk, Schema.Schema.Type<DSchema>, TDeps>;
471
+ use<D extends SkillDependencyDef<string, Schema.Schema.Any>>(...layers: [D, ...D[]] | [ReadonlyArray<D>]): Skill<TInput, TChunk, TDone, TDeps | D>;
472
+ define(fn: DefineFn<TInput, TChunk, TDone, LayersFromDeps<TDeps>>): SkillInstance<TInput, TChunk, TDone, LayersFromDeps<TDeps>>;
473
+ }
474
+
404
475
  /** Thrown when auth denies the request */
405
476
  declare class ExposeAuthError extends Error {
406
477
  readonly status: number;
@@ -501,4 +572,4 @@ declare const consoleTracer: Tracer.Tracer;
501
572
  */
502
573
  declare const consoleTracerLayer: Layer.Layer<never>;
503
574
 
504
- export { Agent, AgentBinding, AgentFactory, AgentNetwork, AgentNetworkEvent, AgentNetworkEventDef, AnyAgent, AuthResult, Channel, ChannelDef, ChannelName, ConfiguredChannel, ContextEvents, EmitPayload, EnvelopeLike, EventEnvelope, EventMeta, EventMetaSchema, EventPlane, ExposeAuthError, ExposeOptions, ExposeRequest, ExposeSelect, ExposedAPI, ExposedStream, ExpressEndpoint, ExpressEndpointOptions, ExpressHandler, ExpressRequest, ExpressResponse, NextEndpoint, NextEndpointOptions, NextGetHandler, OnRequestContext, RunEvents, SetupContext, Sink, SinkDef, SpawnCallbackContext, SpawnFn, SpawnerBuilder, StreamFactory, UnboundEvent, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
575
+ export { Agent, AgentBinding, AgentFactory, AgentNetwork, AgentNetworkEvent, AgentNetworkEventDef, AnyAgent, AuthResult, Channel, ChannelDef, ChannelName, ConfiguredChannel, ContextEvents, EmitPayload, EnvelopeLike, EventEnvelope, EventMeta, EventMetaSchema, EventPlane, ExposeAuthError, ExposeOptions, ExposeRequest, ExposeSelect, ExposedAPI, ExposedStream, ExpressEndpoint, ExpressEndpointOptions, ExpressHandler, ExpressRequest, ExpressResponse, LayerName, LayersFromDeps, NextEndpoint, NextEndpointOptions, NextGetHandler, OnRequestContext, RunEvents, SetupContext, Sink, SinkDef, Skill, SkillDefineContext, SkillDependency, SkillDependencyDef, SkillInstance, SkillRuntimeOptions, SpawnCallbackContext, SpawnFn, SpawnerBuilder, StreamFactory, UnboundEvent, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
@@ -766,6 +766,157 @@ var AgentFactory = class _AgentFactory {
766
766
  );
767
767
  }
768
768
  };
769
+ var CAMEL_CASE_REGEX = /^[a-z][a-zA-Z0-9]*$/;
770
+ var LayerName = Brand.refined(
771
+ (s) => typeof s === "string" && CAMEL_CASE_REGEX.test(s),
772
+ (s) => Brand.error(`Expected camelCase (e.g. myLayerFoo), got: ${s}`)
773
+ );
774
+ var SkillDependency = {
775
+ of(config) {
776
+ const name = LayerName(config.name);
777
+ const decode = Schema.decodeUnknown(config.shape);
778
+ return {
779
+ _tag: "SkillDependencyDef",
780
+ name,
781
+ _name: config.name,
782
+ shape: config.shape,
783
+ decode
784
+ };
785
+ }
786
+ };
787
+ function toLayerArray(layers) {
788
+ if (layers.length === 1 && Array.isArray(layers[0])) {
789
+ return layers[0];
790
+ }
791
+ return [...layers];
792
+ }
793
+ function assertUniqueLayerNames(layers) {
794
+ const seen = /* @__PURE__ */ new Set();
795
+ for (const dep of layers) {
796
+ const key = dep.name;
797
+ if (seen.has(key)) {
798
+ throw new Error(`Duplicate layer name: ${key}`);
799
+ }
800
+ seen.add(key);
801
+ }
802
+ }
803
+ var Skill = class _Skill {
804
+ constructor(params) {
805
+ this._inputSchema = params.inputSchema;
806
+ this._chunkSchema = params.chunkSchema;
807
+ this._doneSchema = params.doneSchema;
808
+ this._layers = params.layers;
809
+ this._defineFn = params.defineFn;
810
+ }
811
+ getState() {
812
+ return {
813
+ inputSchema: this._inputSchema,
814
+ chunkSchema: this._chunkSchema,
815
+ doneSchema: this._doneSchema,
816
+ layers: this._layers,
817
+ defineFn: this._defineFn
818
+ };
819
+ }
820
+ static of(_options) {
821
+ return new _Skill({
822
+ layers: []
823
+ });
824
+ }
825
+ input(schema) {
826
+ return new _Skill({
827
+ ...this.getState(),
828
+ inputSchema: schema
829
+ });
830
+ }
831
+ chunk(schema) {
832
+ return new _Skill({
833
+ ...this.getState(),
834
+ chunkSchema: schema
835
+ });
836
+ }
837
+ done(schema) {
838
+ return new _Skill({
839
+ ...this.getState(),
840
+ doneSchema: schema
841
+ });
842
+ }
843
+ use(...layers) {
844
+ const normalized = toLayerArray(layers);
845
+ const allLayers = [...this._layers, ...normalized];
846
+ assertUniqueLayerNames(allLayers);
847
+ return new _Skill({
848
+ ...this.getState(),
849
+ layers: allLayers
850
+ });
851
+ }
852
+ define(fn) {
853
+ const state = this.getState();
854
+ const inputSchema = state.inputSchema;
855
+ const chunkSchema = state.chunkSchema;
856
+ const doneSchema = state.doneSchema;
857
+ const defineFn = fn;
858
+ if (!inputSchema || !chunkSchema || !doneSchema || !defineFn) {
859
+ throw new Error(
860
+ "Skill.define requires input(), chunk(), and done() to be called before define()"
861
+ );
862
+ }
863
+ const decodeInput = Schema.decodeUnknown(inputSchema);
864
+ const decodeChunk = Schema.decodeUnknown(chunkSchema);
865
+ const decodeDone = Schema.decodeUnknown(doneSchema);
866
+ const runDefine = async (input, runtime) => {
867
+ const layersObj = runtime?.layers ?? {};
868
+ const chunks = [];
869
+ const emit = (chunk) => {
870
+ const decoded = Effect.runSync(
871
+ decodeChunk(chunk)
872
+ );
873
+ chunks.push(decoded);
874
+ };
875
+ const done = await defineFn({
876
+ input,
877
+ emit,
878
+ layers: layersObj
879
+ });
880
+ const decodedDone = Effect.runSync(
881
+ decodeDone(done)
882
+ );
883
+ return { chunks, done: decodedDone };
884
+ };
885
+ return {
886
+ invokeStream: async function* (input, runtime) {
887
+ const decodedInput = Effect.runSync(
888
+ decodeInput(input)
889
+ );
890
+ const layersObj = runtime?.layers ?? {};
891
+ const chunks = [];
892
+ const emit = (chunk) => {
893
+ const decoded = Effect.runSync(
894
+ decodeChunk(chunk)
895
+ );
896
+ chunks.push(decoded);
897
+ };
898
+ const done = await defineFn({
899
+ input: decodedInput,
900
+ emit,
901
+ layers: layersObj
902
+ });
903
+ const decodedDone = Effect.runSync(
904
+ decodeDone(done)
905
+ );
906
+ for (const c of chunks) {
907
+ yield c;
908
+ }
909
+ yield { _tag: "Done", done: decodedDone };
910
+ },
911
+ invoke: async (input, runtime) => {
912
+ const decodedInput = Effect.runSync(
913
+ decodeInput(input)
914
+ );
915
+ return runDefine(decodedInput, runtime);
916
+ }
917
+ };
918
+ }
919
+ };
769
920
 
770
921
  // src/matrix/io/protocols/sse.ts
771
922
  function formatSSE(envelope) {
@@ -976,6 +1127,6 @@ var consoleTracerLayer = Layer.setTracer(
976
1127
  consoleTracer
977
1128
  );
978
1129
 
979
- export { Agent, AgentFactory, AgentNetwork, AgentNetworkEvent, Channel, ChannelName, ConfiguredChannel, EventMetaSchema, ExposeAuthError, ExpressEndpoint, NextEndpoint, Sink, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
1130
+ export { Agent, AgentFactory, AgentNetwork, AgentNetworkEvent, Channel, ChannelName, ConfiguredChannel, EventMetaSchema, ExposeAuthError, ExpressEndpoint, LayerName, NextEndpoint, Sink, Skill, SkillDependency, consoleTracer, consoleTracerLayer, formatSSE, isHttpStreamSink, toSSEStream };
980
1131
  //# sourceMappingURL=out.js.map
981
1132
  //# sourceMappingURL=index.js.map