@effect/ai 0.10.1 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/AiModel/package.json +6 -0
  2. package/AiModels/package.json +6 -0
  3. package/AiPlan/package.json +6 -0
  4. package/dist/cjs/AiModel.js +44 -0
  5. package/dist/cjs/AiModel.js.map +1 -0
  6. package/dist/cjs/AiModels.js +54 -0
  7. package/dist/cjs/AiModels.js.map +1 -0
  8. package/dist/cjs/AiPlan.js +30 -0
  9. package/dist/cjs/AiPlan.js.map +1 -0
  10. package/dist/cjs/AiTelemetry.js.map +1 -1
  11. package/dist/cjs/index.js +7 -1
  12. package/dist/cjs/internal/aiPlan.js +91 -0
  13. package/dist/cjs/internal/aiPlan.js.map +1 -0
  14. package/dist/dts/AiError.d.ts +1 -1
  15. package/dist/dts/AiModel.d.ts +61 -0
  16. package/dist/dts/AiModel.d.ts.map +1 -0
  17. package/dist/dts/AiModels.d.ts +34 -0
  18. package/dist/dts/AiModels.d.ts.map +1 -0
  19. package/dist/dts/AiPlan.d.ts +126 -0
  20. package/dist/dts/AiPlan.d.ts.map +1 -0
  21. package/dist/dts/AiTelemetry.d.ts +20 -1
  22. package/dist/dts/AiTelemetry.d.ts.map +1 -1
  23. package/dist/dts/index.d.ts +12 -0
  24. package/dist/dts/index.d.ts.map +1 -1
  25. package/dist/dts/internal/aiPlan.d.ts +2 -0
  26. package/dist/dts/internal/aiPlan.d.ts.map +1 -0
  27. package/dist/esm/AiModel.js +35 -0
  28. package/dist/esm/AiModel.js.map +1 -0
  29. package/dist/esm/AiModels.js +44 -0
  30. package/dist/esm/AiModels.js.map +1 -0
  31. package/dist/esm/AiPlan.js +22 -0
  32. package/dist/esm/AiPlan.js.map +1 -0
  33. package/dist/esm/AiTelemetry.js.map +1 -1
  34. package/dist/esm/index.js +12 -0
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/internal/aiPlan.js +82 -0
  37. package/dist/esm/internal/aiPlan.js.map +1 -0
  38. package/package.json +28 -4
  39. package/src/AiModel.ts +91 -0
  40. package/src/AiModels.ts +77 -0
  41. package/src/AiPlan.ts +152 -0
  42. package/src/AiTelemetry.ts +20 -1
  43. package/src/index.ts +15 -0
  44. package/src/internal/aiPlan.ts +163 -0
@@ -10,6 +10,18 @@ export * as AiError from "./AiError.js";
10
10
  * @since 1.0.0
11
11
  */
12
12
  export * as AiInput from "./AiInput.js";
13
+ /**
14
+ * @since 1.0.0
15
+ */
16
+ export * as AiModel from "./AiModel.js";
17
+ /**
18
+ * @since 1.0.0
19
+ */
20
+ export * as AiModels from "./AiModels.js";
21
+ /**
22
+ * @since 1.0.0
23
+ */
24
+ export * as AiPlan from "./AiPlan.js";
13
25
  /**
14
26
  * @since 1.0.0
15
27
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC;;GAEG;AACH,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAA;AAE7C;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;GAEG;AACH,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAE3C;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;GAEG;AACH,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAA;AAE7C;;GAEG;AACH,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC;;GAEG;AACH,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AAEzC;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAA;AAE7C;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;GAEG;AACH,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAE3C;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;GAEG;AACH,OAAO,KAAK,UAAU,MAAM,iBAAiB,CAAA;AAE7C;;GAEG;AACH,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=aiPlan.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiPlan.d.ts","sourceRoot":"","sources":["../../../src/internal/aiPlan.ts"],"names":[],"mappings":""}
@@ -0,0 +1,35 @@
1
+ import * as Option from "effect/Option";
2
+ import * as InternalAiPlan from "./internal/aiPlan.js";
3
+ /**
4
+ * @since 1.0.0
5
+ * @category type ids
6
+ */
7
+ export const TypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiModel");
8
+ /**
9
+ * @since 1.0.0
10
+ * @category type ids
11
+ */
12
+ export const PlanTypeId = /*#__PURE__*/Symbol.for("@effect/ai/Plan");
13
+ const AiModelProto = {
14
+ ...InternalAiPlan.PlanPrototype,
15
+ [TypeId]: TypeId
16
+ };
17
+ /**
18
+ * @since 1.0.0
19
+ * @category constructors
20
+ */
21
+ export const make = options => {
22
+ const self = Object.create(AiModelProto);
23
+ self.cacheKey = options.cacheKey;
24
+ self.model = options.model;
25
+ self.provides = options.provides;
26
+ self.requires = options.requires;
27
+ self.updateContext = options.updateContext;
28
+ self.steps = [{
29
+ model: self,
30
+ check: Option.none(),
31
+ schedule: Option.none()
32
+ }];
33
+ return self;
34
+ };
35
+ //# sourceMappingURL=AiModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AiModel.js","names":["Option","InternalAiPlan","TypeId","Symbol","for","PlanTypeId","AiModelProto","PlanPrototype","make","options","self","Object","create","cacheKey","model","provides","requires","updateContext","steps","check","none","schedule"],"sources":["../../src/AiModel.ts"],"sourcesContent":[null],"mappings":"AAKA,OAAO,KAAKA,MAAM,MAAM,eAAe;AAGvC,OAAO,KAAKC,cAAc,MAAM,sBAAsB;AAEtD;;;;AAIA,OAAO,MAAMC,MAAM,gBAAkBC,MAAM,CAACC,GAAG,CAAC,oBAAoB,CAAC;AAQrE;;;;AAIA,OAAO,MAAMC,UAAU,gBAAkBF,MAAM,CAACC,GAAG,CAAC,iBAAiB,CAAC;AAoCtE,MAAME,YAAY,GAAG;EACnB,GAAGL,cAAc,CAACM,aAAa;EAC/B,CAACL,MAAM,GAAGA;CACX;AAED;;;;AAIA,OAAO,MAAMM,IAAI,GAAwBC,OAMxC,IAAiC;EAChC,MAAMC,IAAI,GAAGC,MAAM,CAACC,MAAM,CAACN,YAAY,CAAC;EACxCI,IAAI,CAACG,QAAQ,GAAGJ,OAAO,CAACI,QAAQ;EAChCH,IAAI,CAACI,KAAK,GAAGL,OAAO,CAACK,KAAK;EAC1BJ,IAAI,CAACK,QAAQ,GAAGN,OAAO,CAACM,QAAQ;EAChCL,IAAI,CAACM,QAAQ,GAAGP,OAAO,CAACO,QAAQ;EAChCN,IAAI,CAACO,aAAa,GAAGR,OAAO,CAACQ,aAAa;EAC1CP,IAAI,CAACQ,KAAK,GAAG,CAAC;IACZJ,KAAK,EAAEJ,IAAI;IACXS,KAAK,EAAEnB,MAAM,CAACoB,IAAI,EAAE;IACpBC,QAAQ,EAAErB,MAAM,CAACoB,IAAI;GACtB,CAAC;EACF,OAAOV,IAAI;AACb,CAAC","ignoreList":[]}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Context from "effect/Context";
5
+ import * as Effect from "effect/Effect";
6
+ import * as Equal from "effect/Equal";
7
+ import * as Hash from "effect/Hash";
8
+ import * as Layer from "effect/Layer";
9
+ import * as RcMap from "effect/RcMap";
10
+ /**
11
+ * @since 1.0.0
12
+ * @category tags
13
+ */
14
+ export class AiModels extends /*#__PURE__*/Context.Tag("@effect/ai/AiModels")() {}
15
+ class AiModelsKey {
16
+ model;
17
+ service;
18
+ constructor(model, service) {
19
+ this.model = model;
20
+ this.service = service;
21
+ }
22
+ [Equal.symbol](that) {
23
+ return this.service === that.service && this.model.cacheKey === that.model.cacheKey;
24
+ }
25
+ [Hash.symbol]() {
26
+ return Hash.combine(Hash.hash(this.service))(Hash.hash(this.model.cacheKey));
27
+ }
28
+ }
29
+ const make = /*#__PURE__*/Effect.gen(function* () {
30
+ const services = yield* RcMap.make({
31
+ idleTimeToLive: "1 minute",
32
+ lookup: key => Effect.provideService(key.model.provides, key.model.requires, key.service)
33
+ });
34
+ const build = (model, context) => Effect.map(RcMap.get(services, new AiModelsKey(model, Context.get(context, model.requires))), context => model.updateContext(context));
35
+ return {
36
+ build
37
+ };
38
+ });
39
+ /**
40
+ * @since 1.0.0
41
+ * @category layers
42
+ */
43
+ export const layer = /*#__PURE__*/Layer.scoped(AiModels, make);
44
+ //# sourceMappingURL=AiModels.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AiModels.js","names":["Context","Effect","Equal","Hash","Layer","RcMap","AiModels","Tag","AiModelsKey","model","service","constructor","symbol","that","cacheKey","combine","hash","make","gen","services","idleTimeToLive","lookup","key","provideService","provides","requires","build","context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;GACxG,CAAC;EAEF,MAAMgB,KAAK,GAAGA,CACZjB,KAAkC,EAClCkB,OAAkC,KAElC1B,MAAM,CAAC2B,GAAG,CACRvB,KAAK,CAACwB,GAAG,CACPV,QAAQ,EACR,IAAIX,WAAW,CAACC,KAAK,EAAET,OAAO,CAAC6B,GAAG,CAACF,OAAO,EAAElB,KAAK,CAACgB,QAAe,CAAC,CAAC,CACpE,EACAE,OAAO,IAAKlB,KAAK,CAACqB,aAAa,CAACH,OAAO,CAAC,CAC1C;EAEH,OAAO;IAAED;EAAK,CAAW;AAC3B,CAAC,CAAC;AAEF;;;;AAIA,OAAO,MAAMK,KAAK,gBAA0B3B,KAAK,CAAC4B,MAAM,CAAC1B,QAAQ,EAAEW,IAAI,CAAC","ignoreList":[]}
@@ -0,0 +1,22 @@
1
+ import * as Internal from "./internal/aiPlan.js";
2
+ /**
3
+ * @since 1.0.0
4
+ * @category type ids
5
+ */
6
+ export const TypeId = Internal.TypeId;
7
+ /**
8
+ * @since 1.0.0
9
+ * @category constructors
10
+ */
11
+ export const fromModel = Internal.fromModel;
12
+ /**
13
+ * @since 1.0.0
14
+ * @category error handling
15
+ */
16
+ export const withFallback = Internal.withFallback;
17
+ /**
18
+ * @since 1.0.0
19
+ * @category combination
20
+ */
21
+ export const concatSteps = Internal.concatSteps;
22
+ //# sourceMappingURL=AiPlan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AiPlan.js","names":["Internal","TypeId","fromModel","withFallback","concatSteps"],"sources":["../../src/AiPlan.ts"],"sourcesContent":[null],"mappings":"AAWA,OAAO,KAAKA,QAAQ,MAAM,sBAAsB;AAEhD;;;;AAIA,OAAO,MAAMC,MAAM,GAAkBD,QAAQ,CAACC,MAAM;AAmEpD;;;;AAIA,OAAO,MAAMC,SAAS,GAO+BF,QAAQ,CAACE,SAAS;AAEvE;;;;AAIA,OAAO,MAAMC,YAAY,GA4BrBH,QAAQ,CAACG,YAAY;AAEzB;;;;AAIA,OAAO,MAAMC,WAAW,GAgBpBJ,QAAQ,CAACI,WAAW","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"AiTelemetry.js","names":["dual","Predicate","String","addSpanAttributes","keyPrefix","transformKey","span","attributes","key","value","Object","entries","isNotNullable","attribute","addSpanBaseAttributes","camelToSnake","addSpanOperationAttributes","addSpanRequestAttributes","addSpanResponseAttributes","addSpanTokenAttributes","addSpanUsageAttributes","addGenAIAnnotations","options","system","operation","request","response","token","usage"],"sources":["../../src/AiTelemetry.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA,SAASA,IAAI,QAAQ,iBAAiB;AACtC,OAAO,KAAKC,SAAS,MAAM,kBAAkB;AAC7C,OAAO,KAAKC,MAAM,MAAM,eAAe;AA6NvC;;;;AAIA,OAAO,MAAMC,iBAAiB,GAAGA,CAC/BC,SAAiB,EACjBC,YAAqC,KAEvC,CAAyCC,IAAU,EAAEC,UAAsB,KAAU;EACnF,KAAK,MAAM,CAACC,GAAG,EAAEC,KAAK,CAAC,IAAIC,MAAM,CAACC,OAAO,CAACJ,UAAU,CAAC,EAAE;IACrD,IAAIN,SAAS,CAACW,aAAa,CAACH,KAAK,CAAC,EAAE;MAClCH,IAAI,CAACO,SAAS,CAAC,GAAGT,SAAS,IAAIC,YAAY,CAACG,GAAG,CAAC,EAAE,EAAEC,KAAK,CAAC;IAC5D;EACF;AACF,CAAC;AAED,MAAMK,qBAAqB,gBAAGX,iBAAiB,CAAC,QAAQ,EAAED,MAAM,CAACa,YAAY,CAAuB;AACpG,MAAMC,0BAA0B,gBAAGb,iBAAiB,CAAC,kBAAkB,EAAED,MAAM,CAACa,YAAY,CAA4B;AACxH,MAAME,wBAAwB,gBAAGd,iBAAiB,CAAC,gBAAgB,EAAED,MAAM,CAACa,YAAY,CAA0B;AAClH,MAAMG,yBAAyB,gBAAGf,iBAAiB,CAAC,iBAAiB,EAAED,MAAM,CAACa,YAAY,CAA2B;AACrH,MAAMI,sBAAsB,gBAAGhB,iBAAiB,CAAC,cAAc,EAAED,MAAM,CAACa,YAAY,CAAwB;AAC5G,MAAMK,sBAAsB,gBAAGjB,iBAAiB,CAAC,cAAc,EAAED,MAAM,CAACa,YAAY,CAAwB;AAc5G;;;;;;;;AAQA,OAAO,MAAMM,mBAAmB,gBAAGrB,IAAI,CAmBrC,CAAC,EAAE,CAACM,IAAI,EAAEgB,OAAO,KAAI;EACrBR,qBAAqB,CAACR,IAAI,EAAE;IAAEiB,MAAM,EAAED,OAAO,CAACC;EAAM,CAAE,CAAC;EACvD,IAAItB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACE,SAAS,CAAC,EAAER,0BAA0B,CAACV,IAAI,EAAEgB,OAAO,CAACE,SAAS,CAAC;EACnG,IAAIvB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACG,OAAO,CAAC,EAAER,wBAAwB,CAACX,IAAI,EAAEgB,OAAO,CAACG,OAAO,CAAC;EAC7F,IAAIxB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACI,QAAQ,CAAC,EAAER,yBAAyB,CAACZ,IAAI,EAAEgB,OAAO,CAACI,QAAQ,CAAC;EAChG,IAAIzB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACK,KAAK,CAAC,EAAER,sBAAsB,CAACb,IAAI,EAAEgB,OAAO,CAACK,KAAK,CAAC;EACvF,IAAI1B,SAAS,CAACW,aAAa,CAACU,OAAO,CAACM,KAAK,CAAC,EAAER,sBAAsB,CAACd,IAAI,EAAEgB,OAAO,CAACM,KAAK,CAAC;AACzF,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"AiTelemetry.js","names":["dual","Predicate","String","addSpanAttributes","keyPrefix","transformKey","span","attributes","key","value","Object","entries","isNotNullable","attribute","addSpanBaseAttributes","camelToSnake","addSpanOperationAttributes","addSpanRequestAttributes","addSpanResponseAttributes","addSpanTokenAttributes","addSpanUsageAttributes","addGenAIAnnotations","options","system","operation","request","response","token","usage"],"sources":["../../src/AiTelemetry.ts"],"sourcesContent":[null],"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ItB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACE,SAAS,CAAC,EAAER,0BAA0B,CAACV,IAAI,EAAEgB,OAAO,CAACE,SAAS,CAAC;EACnG,IAAIvB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACG,OAAO,CAAC,EAAER,wBAAwB,CAACX,IAAI,EAAEgB,OAAO,CAACG,OAAO,CAAC;EAC7F,IAAIxB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACI,QAAQ,CAAC,EAAER,yBAAyB,CAACZ,IAAI,EAAEgB,OAAO,CAACI,QAAQ,CAAC;EAChG,IAAIzB,SAAS,CAACW,aAAa,CAACU,OAAO,CAACK,KAAK,CAAC,EAAER,sBAAsB,CAACb,IAAI,EAAEgB,OAAO,CAACK,KAAK,CAAC;EACvF,IAAI1B,SAAS,CAACW,aAAa,CAACU,OAAO,CAACM,KAAK,CAAC,EAAER,sBAAsB,CAACd,IAAI,EAAEgB,OAAO,CAACM,KAAK,CAAC;AACzF,CAAC,CAAC","ignoreList":[]}
package/dist/esm/index.js CHANGED
@@ -10,6 +10,18 @@ export * as AiError from "./AiError.js";
10
10
  * @since 1.0.0
11
11
  */
12
12
  export * as AiInput from "./AiInput.js";
13
+ /**
14
+ * @since 1.0.0
15
+ */
16
+ export * as AiModel from "./AiModel.js";
17
+ /**
18
+ * @since 1.0.0
19
+ */
20
+ export * as AiModels from "./AiModels.js";
21
+ /**
22
+ * @since 1.0.0
23
+ */
24
+ export * as AiPlan from "./AiPlan.js";
13
25
  /**
14
26
  * @since 1.0.0
15
27
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["AiChat","AiError","AiInput","AiResponse","AiRole","AiTelemetry","AiToolkit","Completions","Embeddings","Tokenizer"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,OAAO,MAAM,cAAc;AAEvC;;;AAGA,OAAO,KAAKC,OAAO,MAAM,cAAc;AAEvC;;;AAGA,OAAO,KAAKC,UAAU,MAAM,iBAAiB;AAE7C;;;AAGA,OAAO,KAAKC,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;AAGA,OAAO,KAAKC,SAAS,MAAM,gBAAgB;AAE3C;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;AAGA,OAAO,KAAKC,UAAU,MAAM,iBAAiB;AAE7C;;;AAGA,OAAO,KAAKC,SAAS,MAAM,gBAAgB","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["AiChat","AiError","AiInput","AiModel","AiModels","AiPlan","AiResponse","AiRole","AiTelemetry","AiToolkit","Completions","Embeddings","Tokenizer"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAGA,OAAO,KAAKA,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,OAAO,MAAM,cAAc;AAEvC;;;AAGA,OAAO,KAAKC,OAAO,MAAM,cAAc;AAEvC;;;AAGA,OAAO,KAAKC,OAAO,MAAM,cAAc;AAEvC;;;AAGA,OAAO,KAAKC,QAAQ,MAAM,eAAe;AAEzC;;;AAGA,OAAO,KAAKC,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,UAAU,MAAM,iBAAiB;AAE7C;;;AAGA,OAAO,KAAKC,MAAM,MAAM,aAAa;AAErC;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;AAGA,OAAO,KAAKC,SAAS,MAAM,gBAAgB;AAE3C;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;AAGA,OAAO,KAAKC,UAAU,MAAM,iBAAiB;AAE7C;;;AAGA,OAAO,KAAKC,SAAS,MAAM,gBAAgB","ignoreList":[]}
@@ -0,0 +1,82 @@
1
+ import * as Cause from "effect/Cause";
2
+ import * as Context from "effect/Context";
3
+ import * as Effect from "effect/Effect";
4
+ import { CommitPrototype } from "effect/Effectable";
5
+ import * as Exit from "effect/Exit";
6
+ import { dual, identity } from "effect/Function";
7
+ import * as Option from "effect/Option";
8
+ import { pipeArguments } from "effect/Pipeable";
9
+ import * as Predicate from "effect/Predicate";
10
+ import * as Schedule from "effect/Schedule";
11
+ import * as Scope from "effect/Scope";
12
+ import { AiModels } from "../AiModels.js";
13
+ /** @internal */
14
+ export const TypeId = /*#__PURE__*/Symbol.for("@effect/ai/AiPlan");
15
+ /** @internal */
16
+ export const PlanPrototype = {
17
+ ...CommitPrototype,
18
+ [TypeId]: TypeId,
19
+ commit() {
20
+ return buildPlan(this);
21
+ },
22
+ pipe() {
23
+ return pipeArguments(this, arguments);
24
+ }
25
+ };
26
+ const makePlan = steps => {
27
+ const self = Object.create(PlanPrototype);
28
+ self.steps = steps;
29
+ return self;
30
+ };
31
+ const buildPlan = plan => Effect.map(Effect.context(), context => {
32
+ const models = Context.get(context, AiModels);
33
+ return Effect.fnUntraced(function* (effect) {
34
+ let exit = undefined;
35
+ for (const step of plan.steps) {
36
+ if (exit !== undefined && Exit.isFailure(exit) && Option.isSome(step.check)) {
37
+ const check = step.check.value(Cause.squash(exit.cause));
38
+ const isFatalError = !(Effect.isEffect(check) ? yield* check : check);
39
+ if (isFatalError) break;
40
+ }
41
+ const retryOptions = getRetryOptions(step);
42
+ exit = yield* Effect.scopedWith(scope => models.build(step.model, context).pipe(Scope.extend(scope), Effect.flatMap(context => effect.pipe(Option.isSome(retryOptions) ? Effect.retry(retryOptions.value) : identity, Effect.provide(context))), Effect.exit));
43
+ if (Exit.isSuccess(exit)) break;
44
+ }
45
+ return yield* exit;
46
+ });
47
+ });
48
+ const getRetryOptions = step => {
49
+ if (Option.isNone(step.schedule) && Option.isNone(step.check)) {
50
+ return Option.none();
51
+ }
52
+ return Option.some({
53
+ schedule: Option.getOrUndefined(step.schedule),
54
+ while: Option.getOrUndefined(step.check)
55
+ });
56
+ };
57
+ /** @internal */
58
+ export const fromModel = (model, options) => makePlan([{
59
+ model,
60
+ check: Option.fromNullable(options?.while),
61
+ schedule: resolveSchedule(options ?? {})
62
+ }]);
63
+ /** @internal */
64
+ export const withFallback = /*#__PURE__*/dual(2, (self, options) => makePlan([...self.steps, {
65
+ model: options.model,
66
+ check: Option.fromNullable(options.while),
67
+ schedule: resolveSchedule(options)
68
+ }]));
69
+ const resolveSchedule = options => {
70
+ if (Predicate.isUndefined(options.attempts) && Predicate.isUndefined(options.schedule)) return Option.none();
71
+ let schedule = options.schedule ?? Schedule.forever;
72
+ if (Predicate.isNotUndefined(options.attempts)) {
73
+ // In an `AiPlan`, the `attempts` represents the total number of times to
74
+ // attempt the call, not the number of retries, thus we subtract one from
75
+ // the total number of recurrences
76
+ schedule = Schedule.intersect(schedule, Schedule.recurs(options.attempts - 1));
77
+ }
78
+ return Option.some(schedule);
79
+ };
80
+ /** @internal */
81
+ export const concatSteps = /*#__PURE__*/dual(2, (self, other) => makePlan([...self.steps, ...other.steps]));
82
+ //# sourceMappingURL=aiPlan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aiPlan.js","names":["Cause","Context","Effect","CommitPrototype","Exit","dual","identity","Option","pipeArguments","Predicate","Schedule","Scope","AiModels","TypeId","Symbol","for","PlanPrototype","commit","buildPlan","pipe","arguments","makePlan","steps","self","Object","create","plan","map","context","models","get","fnUntraced","effect","exit","undefined","step","isFailure","isSome","check","value","squash","cause","isFatalError","isEffect","retryOptions","getRetryOptions","scopedWith","scope","build","model","extend","flatMap","retry","provide","isSuccess","isNone","schedule","none","some","getOrUndefined","while","fromModel","options","fromNullable","resolveSchedule","withFallback","isUndefined","attempts","forever","isNotUndefined","intersect","recurs","concatSteps","other"],"sources":["../../../src/internal/aiPlan.ts"],"sourcesContent":[null],"mappings":"AAAA,OAAO,KAAKA,KAAK,MAAM,cAAc;AACrC,OAAO,KAAKC,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC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,CAACK,QAAQ,GAAG,CAAC,CAAC,CAAC;EAChF;EACA,OAAO5D,MAAM,CAACmD,IAAI,CAACF,QAAQ,CAAC;AAC9B,CAAC;AAED;AACA,OAAO,MAAMgB,WAAW,gBAAGnE,IAAI,CAU7B,CAAC,EAAE,CAACkB,IAAI,EAAEkD,KAAK,KAAKpD,QAAQ,CAAC,CAAC,GAAGE,IAAI,CAACD,KAAK,EAAE,GAAGmD,KAAK,CAACnD,KAAK,CAAC,CAAC,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect/ai",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "Effect modules for working with AI apis",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,9 +11,9 @@
11
11
  "sideEffects": [],
12
12
  "dependencies": {},
13
13
  "peerDependencies": {
14
- "@effect/experimental": "^0.41.1",
15
- "@effect/platform": "^0.77.1",
16
- "effect": "^3.13.1"
14
+ "@effect/experimental": "^0.41.3",
15
+ "@effect/platform": "^0.77.3",
16
+ "effect": "^3.13.3"
17
17
  },
18
18
  "publishConfig": {
19
19
  "provenance": true
@@ -43,6 +43,21 @@
43
43
  "import": "./dist/esm/AiInput.js",
44
44
  "default": "./dist/cjs/AiInput.js"
45
45
  },
46
+ "./AiModel": {
47
+ "types": "./dist/dts/AiModel.d.ts",
48
+ "import": "./dist/esm/AiModel.js",
49
+ "default": "./dist/cjs/AiModel.js"
50
+ },
51
+ "./AiModels": {
52
+ "types": "./dist/dts/AiModels.d.ts",
53
+ "import": "./dist/esm/AiModels.js",
54
+ "default": "./dist/cjs/AiModels.js"
55
+ },
56
+ "./AiPlan": {
57
+ "types": "./dist/dts/AiPlan.d.ts",
58
+ "import": "./dist/esm/AiPlan.js",
59
+ "default": "./dist/cjs/AiPlan.js"
60
+ },
46
61
  "./AiResponse": {
47
62
  "types": "./dist/dts/AiResponse.d.ts",
48
63
  "import": "./dist/esm/AiResponse.js",
@@ -90,6 +105,15 @@
90
105
  "AiInput": [
91
106
  "./dist/dts/AiInput.d.ts"
92
107
  ],
108
+ "AiModel": [
109
+ "./dist/dts/AiModel.d.ts"
110
+ ],
111
+ "AiModels": [
112
+ "./dist/dts/AiModels.d.ts"
113
+ ],
114
+ "AiPlan": [
115
+ "./dist/dts/AiPlan.d.ts"
116
+ ],
93
117
  "AiResponse": [
94
118
  "./dist/dts/AiResponse.d.ts"
95
119
  ],
package/src/AiModel.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import type * as Context from "effect/Context"
5
+ import type * as Effect from "effect/Effect"
6
+ import * as Option from "effect/Option"
7
+ import type * as Scope from "effect/Scope"
8
+ import type * as AiPlan from "./AiPlan.js"
9
+ import * as InternalAiPlan from "./internal/aiPlan.js"
10
+
11
+ /**
12
+ * @since 1.0.0
13
+ * @category type ids
14
+ */
15
+ export const TypeId: unique symbol = Symbol.for("@effect/ai/AiModel")
16
+
17
+ /**
18
+ * @since 1.0.0
19
+ * @category type ids
20
+ */
21
+ export type TypeId = typeof TypeId
22
+
23
+ /**
24
+ * @since 1.0.0
25
+ * @category type ids
26
+ */
27
+ export const PlanTypeId: unique symbol = Symbol.for("@effect/ai/Plan")
28
+
29
+ /**
30
+ * @since 1.0.0
31
+ * @category type ids
32
+ */
33
+ export type PlanTypeId = typeof TypeId
34
+
35
+ /**
36
+ * @since 1.0.0
37
+ * @category models
38
+ */
39
+ export interface AiModel<in out Provides, in out Requires> extends AiPlan.AiPlan<unknown, Provides, Requires> {
40
+ readonly [TypeId]: TypeId
41
+ readonly model: string
42
+ readonly cacheKey: symbol
43
+ readonly requires: Context.Tag<Requires, any>
44
+ readonly provides: AiModel.ContextBuilder<Provides, Requires>
45
+ readonly updateContext: (context: Context.Context<Provides>) => Context.Context<Provides>
46
+ }
47
+
48
+ /**
49
+ * @since 1.0.0
50
+ */
51
+ export declare namespace AiModel {
52
+ /**
53
+ * @since 1.0.0
54
+ * @category AiModel
55
+ */
56
+ export type ContextBuilder<Provides, Requires> = Effect.Effect<
57
+ Context.Context<Provides>,
58
+ never,
59
+ Requires | Scope.Scope
60
+ >
61
+ }
62
+
63
+ const AiModelProto = {
64
+ ...InternalAiPlan.PlanPrototype,
65
+ [TypeId]: TypeId
66
+ }
67
+
68
+ /**
69
+ * @since 1.0.0
70
+ * @category constructors
71
+ */
72
+ export const make = <Provides, Requires>(options: {
73
+ readonly model: string
74
+ readonly cacheKey: symbol
75
+ readonly requires: Context.Tag<Requires, any>
76
+ readonly provides: AiModel.ContextBuilder<Provides, Requires>
77
+ readonly updateContext: (context: Context.Context<Provides>) => Context.Context<Provides>
78
+ }): AiModel<Provides, Requires> => {
79
+ const self = Object.create(AiModelProto)
80
+ self.cacheKey = options.cacheKey
81
+ self.model = options.model
82
+ self.provides = options.provides
83
+ self.requires = options.requires
84
+ self.updateContext = options.updateContext
85
+ self.steps = [{
86
+ model: self,
87
+ check: Option.none(),
88
+ schedule: Option.none()
89
+ }]
90
+ return self
91
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Context from "effect/Context"
5
+ import * as Effect from "effect/Effect"
6
+ import * as Equal from "effect/Equal"
7
+ import * as Hash from "effect/Hash"
8
+ import * as Layer from "effect/Layer"
9
+ import * as RcMap from "effect/RcMap"
10
+ import type * as Scope from "effect/Scope"
11
+ import type { AiModel } from "./AiModel.js"
12
+
13
+ /**
14
+ * @since 1.0.0
15
+ * @category tags
16
+ */
17
+ export class AiModels extends Context.Tag("@effect/ai/AiModels")<
18
+ AiModels,
19
+ AiModels.Service
20
+ >() {}
21
+
22
+ /**
23
+ * @since 1.0.0
24
+ */
25
+ export declare namespace AiModels {
26
+ /**
27
+ * @since 1.0.0
28
+ * @category models
29
+ */
30
+ export interface Service {
31
+ readonly build: <Provides, Requires>(
32
+ model: AiModel<Provides, Requires>,
33
+ context: Context.Context<Requires>
34
+ ) => Effect.Effect<Context.Context<Provides>, never, Scope.Scope>
35
+ }
36
+ }
37
+
38
+ class AiModelsKey {
39
+ constructor(
40
+ readonly model: AiModel<any, any>,
41
+ readonly service: unknown
42
+ ) {}
43
+
44
+ [Equal.symbol](that: AiModelsKey): boolean {
45
+ return this.service === that.service && this.model.cacheKey === that.model.cacheKey
46
+ }
47
+ [Hash.symbol](): number {
48
+ return Hash.combine(Hash.hash(this.service))(Hash.hash(this.model.cacheKey))
49
+ }
50
+ }
51
+
52
+ const make = Effect.gen(function*() {
53
+ const services = yield* RcMap.make({
54
+ idleTimeToLive: "1 minute",
55
+ lookup: (key: AiModelsKey) => Effect.provideService(key.model.provides, key.model.requires, key.service)
56
+ })
57
+
58
+ const build = <Provides, Requires>(
59
+ model: AiModel<Provides, Requires>,
60
+ context: Context.Context<Requires>
61
+ ): Effect.Effect<Context.Context<Provides>, never, Scope.Scope> =>
62
+ Effect.map(
63
+ RcMap.get(
64
+ services,
65
+ new AiModelsKey(model, Context.get(context, model.requires as any))
66
+ ),
67
+ (context) => model.updateContext(context)
68
+ )
69
+
70
+ return { build } as const
71
+ })
72
+
73
+ /**
74
+ * @since 1.0.0
75
+ * @category layers
76
+ */
77
+ export const layer: Layer.Layer<AiModels> = Layer.scoped(AiModels, make)
package/src/AiPlan.ts ADDED
@@ -0,0 +1,152 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import type { NonEmptyReadonlyArray } from "effect/Array"
5
+ import type * as Effect from "effect/Effect"
6
+ import type * as Option from "effect/Option"
7
+ import type { Pipeable } from "effect/Pipeable"
8
+ import type * as Schedule from "effect/Schedule"
9
+ import type * as Unify from "effect/Unify"
10
+ import type * as AiModel from "./AiModel.js"
11
+ import type { AiModels } from "./AiModels.js"
12
+ import * as Internal from "./internal/aiPlan.js"
13
+
14
+ /**
15
+ * @since 1.0.0
16
+ * @category type ids
17
+ */
18
+ export const TypeId: unique symbol = Internal.TypeId
19
+
20
+ /**
21
+ * @since 1.0.0
22
+ * @category type ids
23
+ */
24
+ export type TypeId = typeof TypeId
25
+
26
+ /**
27
+ * @since 1.0.0
28
+ * @category AiPlan
29
+ */
30
+ export interface AiPlan<in Error, in out Provides, in out Requires>
31
+ extends Pipeable, AiPlan.Builder<Provides, Requires>
32
+ {
33
+ readonly [TypeId]: TypeId
34
+ readonly [Unify.typeSymbol]?: unknown
35
+ readonly [Unify.unifySymbol]?: AiPlanUnify<this>
36
+ readonly [Unify.ignoreSymbol]?: AiPlanUnifyIgnore
37
+ readonly steps: NonEmptyReadonlyArray<AiPlan.Step<Error, Provides, Requires>>
38
+ }
39
+
40
+ /**
41
+ * @since 1.0.0
42
+ * @category Plan
43
+ */
44
+ export interface AiPlanUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
45
+ AiPlan?: () => Extract<A[Unify.typeSymbol], AiPlan<any, any, any>>
46
+ }
47
+
48
+ /**
49
+ * @since 1.0.0
50
+ * @category Plan
51
+ */
52
+ export interface AiPlanUnifyIgnore extends Effect.EffectUnifyIgnore {
53
+ Effect?: true
54
+ }
55
+
56
+ /**
57
+ * @since 1.0.0
58
+ */
59
+ export declare namespace AiPlan {
60
+ /**
61
+ * @since 1.0.0
62
+ * @category AiPlan
63
+ */
64
+ export type Builder<Provides, Requires> = Effect.Effect<Provider<Provides>, never, AiModels | Requires>
65
+
66
+ /**
67
+ * @since 1.0.0
68
+ * @category AiPlan
69
+ */
70
+ export interface Provider<Provides> {
71
+ readonly provide: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Provides>>
72
+ }
73
+
74
+ /**
75
+ * @since 1.0.0
76
+ * @category AiPlan
77
+ */
78
+ export interface Step<Error, Provides, Requires> {
79
+ readonly model: AiModel.AiModel<Provides, Requires>
80
+ readonly check: Option.Option<(error: Error) => boolean | Effect.Effect<boolean>>
81
+ readonly schedule: Option.Option<Schedule.Schedule<any, Error, Requires>>
82
+ }
83
+ }
84
+
85
+ /**
86
+ * @since 1.0.0
87
+ * @category constructors
88
+ */
89
+ export const fromModel: <Provides, Requires, EW, Out, ES, RW = never, RS = never>(
90
+ model: AiModel.AiModel<Provides, Requires>,
91
+ options?: {
92
+ readonly attempts?: number | undefined
93
+ readonly while?: ((error: EW) => boolean | Effect.Effect<boolean, never, RW>) | undefined
94
+ readonly schedule?: Schedule.Schedule<Out, ES, RS> | undefined
95
+ }
96
+ ) => AiPlan<EW & ES, Provides, RW | RS | Requires> = Internal.fromModel
97
+
98
+ /**
99
+ * @since 1.0.0
100
+ * @category error handling
101
+ */
102
+ export const withFallback: {
103
+ /**
104
+ * @since 1.0.0
105
+ * @category error handling
106
+ */
107
+ <Provides, Provides2 extends Provides, Requires2, Out, EW, ES, RW = never, RS = never>(
108
+ options: {
109
+ readonly model: AiModel.AiModel<Provides2, Requires2>
110
+ readonly attempts?: number | undefined
111
+ readonly while?: ((error: EW) => boolean | Effect.Effect<boolean, never, RW>) | undefined
112
+ readonly schedule?: Schedule.Schedule<Out, ES, RS> | undefined
113
+ }
114
+ ): <E, Requires>(
115
+ self: AiPlan<E, Provides, Requires>
116
+ ) => AiPlan<E & EW & ES, Provides & Provides2, Requires | Requires2 | RW | RS>
117
+ /**
118
+ * @since 1.0.0
119
+ * @category error handling
120
+ */
121
+ <E, Provides, Requires, Provides2 extends Provides, Requires2, Out, EW, ES, RW = never, RS = never>(
122
+ self: AiPlan<E, Provides, Requires>,
123
+ options: {
124
+ readonly model: AiModel.AiModel<Provides2, Requires2>
125
+ readonly attempts?: number | undefined
126
+ readonly while?: ((error: EW) => boolean | Effect.Effect<boolean, never, RW>) | undefined
127
+ readonly schedule?: Schedule.Schedule<Out, ES, RS> | undefined
128
+ }
129
+ ): AiPlan<E & EW & ES, Provides & Provides2, Requires | Requires2 | RW | RS>
130
+ } = Internal.withFallback
131
+
132
+ /**
133
+ * @since 1.0.0
134
+ * @category combination
135
+ */
136
+ export const concatSteps: {
137
+ /**
138
+ * @since 1.0.0
139
+ * @category combination
140
+ */
141
+ <Error2, Provides2, Requires2>(other: AiPlan<Error2, Provides2, Requires2>): <Error, Provides, Requires>(
142
+ self: AiPlan<Error, Provides, Requires>
143
+ ) => AiPlan<Error & Error2, Provides & Provides2, Requires | Requires2>
144
+ /**
145
+ * @since 1.0.0
146
+ * @category combination
147
+ */
148
+ <Error, Provides, Requires, Error2, Provides2, Requires2>(
149
+ self: AiPlan<Error, Provides, Requires>,
150
+ other: AiPlan<Error2, Provides2, Requires2>
151
+ ): AiPlan<Error & Error2, Provides & Provides2, Requires | Requires2>
152
+ } = Internal.concatSteps