@swapai/core 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SwapAI contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # SwapAI
2
+
3
+ SwapAI learns from an existing classifier, trains a local Needle 2 classifier,
4
+ and only uses it after it has passed a held-out test.
5
+
6
+ ```sh
7
+ npm install @swapai/core
8
+ ```
9
+
10
+ ## Use it
11
+
12
+ ```ts
13
+ import { init } from "@swapai/core";
14
+
15
+ const relevance = init({
16
+ name: "accountant-relevance",
17
+ result: { type: "number", min: 0, max: 1 },
18
+ retrainOnCount: 50,
19
+ acceptableError: "10%",
20
+ retestInterval: 100,
21
+ retestRevertOn: 3,
22
+ model: "needle2",
23
+ maxTrainingSet: 10000,
24
+ });
25
+
26
+ const score = await relevance.classify(input, async (value) => {
27
+ const result = await callExistingClassifier(value);
28
+ return result.score;
29
+ });
30
+ ```
31
+
32
+ Before the local classifier is trained, `classify` calls the supplied reference
33
+ classifier and records its answer. Once the local classifier passes its
34
+ held-out test, it takes over. SwapAI periodically calls both classifiers to
35
+ check that the local result remains within the allowed error.
36
+
37
+ `input` must be the complete input sent to the reference classifier, including
38
+ the instruction that defines the classification task. Do not log only the raw
39
+ text being classified: Needle needs the same task and context the reference saw.
40
+
41
+ ## Manual control
42
+
43
+ The callback is optional. You can keep control of the switch yourself:
44
+
45
+ ```ts
46
+ if (relevance.isTrained()) {
47
+ return relevance.classify(input);
48
+ }
49
+
50
+ const score = await callExistingClassifier(input);
51
+ relevance.logClassification(input, score);
52
+ return score;
53
+ ```
54
+
55
+ `isTrained()` and `logClassification()` are synchronous. Logging validates the
56
+ result immediately and queues storage work. Use `await relevance.flush()` when
57
+ you need to know all accepted examples are durable, such as during shutdown.
58
+
59
+ ## Result types
60
+
61
+ SwapAI supports bounded numbers, booleans, and a closed list of strings:
62
+
63
+ ```ts
64
+ init({
65
+ // other options
66
+ result: { type: "boolean" },
67
+ });
68
+
69
+ init({
70
+ // other options
71
+ result: { type: "string", values: ["rabbit", "fish", "pig"] },
72
+ });
73
+ ```
74
+
75
+ The string result is inferred as `"rabbit" | "fish" | "pig"`.
76
+
77
+ ## Effect
78
+
79
+ Effect is optional and lives in a separate import:
80
+
81
+ ```ts
82
+ import { classifyWithReference } from "@swapai/core/effect";
83
+
84
+ const program = classifyWithReference(relevance, input, callReferenceEffect);
85
+ ```
86
+
87
+ ## Runtime
88
+
89
+ Needle runs locally. On first use SwapAI creates its private runtime under
90
+ `.swapai`, installs the pinned training package, and downloads Needle's model
91
+ files. No Python setup is part of the TypeScript API. Set `dataDirectory` to
92
+ store this state elsewhere.
93
+
94
+ Full documentation: [stuartmccamley.com/swapai](https://stuartmccamley.com/swapai)
@@ -0,0 +1,6 @@
1
+ # Third-party notices
2
+
3
+ SwapAI installs and runs [Cactus Needle](https://github.com/cactus-compute/needle),
4
+ which is licensed under the Apache License 2.0. Needle is downloaded into the
5
+ configured SwapAI data directory on first use; it is not bundled in this npm
6
+ package.
@@ -0,0 +1,15 @@
1
+ // src/errors.ts
2
+ var SwapAIError = class extends Error {
3
+ _tag = "SwapAIError";
4
+ code;
5
+ constructor(code, message, options) {
6
+ super(message, options);
7
+ this.name = "SwapAIError";
8
+ this.code = code;
9
+ }
10
+ };
11
+
12
+ export {
13
+ SwapAIError
14
+ };
15
+ //# sourceMappingURL=chunk-SXECHZ3L.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts"],"sourcesContent":["export type SwapAIErrorCode =\n | \"invalid_configuration\"\n | \"invalid_result\"\n | \"not_trained\"\n | \"service_unavailable\"\n | \"classification_failed\"\n | \"storage_failed\";\n\nexport class SwapAIError extends Error {\n readonly _tag = \"SwapAIError\" as const;\n readonly code: SwapAIErrorCode;\n\n constructor(\n code: SwapAIErrorCode,\n message: string,\n options?: ErrorOptions,\n ) {\n super(message, options);\n this.name = \"SwapAIError\";\n this.code = code;\n }\n}\n"],"mappings":";AAQO,IAAM,cAAN,cAA0B,MAAM;AAAA,EAC5B,OAAO;AAAA,EACP;AAAA,EAET,YACE,MACA,SACA,SACA;AACA,UAAM,SAAS,OAAO;AACtB,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,7 @@
1
+ import { Effect } from 'effect';
2
+ import { R as ResultValue, C as Classifier, S as SwapAIError } from './types-DTbU1MEi.js';
3
+
4
+ declare function classify<Result extends ResultValue>(classifier: Classifier<Result>, input: string): Effect.Effect<Result, SwapAIError>;
5
+ declare function classifyWithReference<Result extends ResultValue, ReferenceError, Requirements>(classifier: Classifier<Result>, input: string, reference: (input: string) => Effect.Effect<Result, ReferenceError, Requirements>): Effect.Effect<Result, SwapAIError | ReferenceError, Requirements>;
6
+
7
+ export { classify, classifyWithReference };
package/dist/effect.js ADDED
@@ -0,0 +1,55 @@
1
+ import {
2
+ SwapAIError
3
+ } from "./chunk-SXECHZ3L.js";
4
+
5
+ // src/effect.ts
6
+ import { Effect, Either, Runtime } from "effect";
7
+ var ReferenceEffectFailure = class {
8
+ error;
9
+ constructor(error) {
10
+ this.error = error;
11
+ }
12
+ };
13
+ function classificationFailure(error) {
14
+ if (error instanceof SwapAIError) {
15
+ return error;
16
+ }
17
+ return new SwapAIError("classification_failed", "Classification failed", {
18
+ cause: error
19
+ });
20
+ }
21
+ function classify(classifier, input) {
22
+ return Effect.tryPromise({
23
+ try: () => classifier.classify(input),
24
+ catch: classificationFailure
25
+ });
26
+ }
27
+ function classifyWithReference(classifier, input, reference) {
28
+ return Effect.runtime().pipe(
29
+ Effect.flatMap(
30
+ (runtime) => Effect.tryPromise({
31
+ try: (signal) => classifier.classify(input, async (referenceInput) => {
32
+ const result = await Runtime.runPromise(runtime)(
33
+ Effect.either(reference(referenceInput)),
34
+ { signal }
35
+ );
36
+ if (Either.isLeft(result)) {
37
+ throw new ReferenceEffectFailure(result.left);
38
+ }
39
+ return result.right;
40
+ }),
41
+ catch: (error) => {
42
+ if (error instanceof ReferenceEffectFailure) {
43
+ return error.error;
44
+ }
45
+ return classificationFailure(error);
46
+ }
47
+ })
48
+ )
49
+ );
50
+ }
51
+ export {
52
+ classify,
53
+ classifyWithReference
54
+ };
55
+ //# sourceMappingURL=effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/effect.ts"],"sourcesContent":["import { Effect, Either, Runtime } from \"effect\";\n\nimport { SwapAIError } from \"./errors.js\";\nimport type { Classifier, ResultValue } from \"./types.js\";\n\nclass ReferenceEffectFailure<ReferenceError> {\n readonly error: ReferenceError;\n\n constructor(error: ReferenceError) {\n this.error = error;\n }\n}\n\nfunction classificationFailure(error: unknown): SwapAIError {\n if (error instanceof SwapAIError) {\n return error;\n }\n\n return new SwapAIError(\"classification_failed\", \"Classification failed\", {\n cause: error,\n });\n}\n\nexport function classify<Result extends ResultValue>(\n classifier: Classifier<Result>,\n input: string,\n): Effect.Effect<Result, SwapAIError> {\n return Effect.tryPromise({\n try: () => classifier.classify(input),\n catch: classificationFailure,\n });\n}\n\nexport function classifyWithReference<\n Result extends ResultValue,\n ReferenceError,\n Requirements,\n>(\n classifier: Classifier<Result>,\n input: string,\n reference: (\n input: string,\n ) => Effect.Effect<Result, ReferenceError, Requirements>,\n): Effect.Effect<Result, SwapAIError | ReferenceError, Requirements> {\n return Effect.runtime<Requirements>().pipe(\n Effect.flatMap((runtime) =>\n Effect.tryPromise({\n try: (signal) =>\n classifier.classify(input, async (referenceInput) => {\n const result = await Runtime.runPromise(runtime)(\n Effect.either(reference(referenceInput)),\n { signal },\n );\n\n if (Either.isLeft(result)) {\n throw new ReferenceEffectFailure(result.left);\n }\n\n return result.right;\n }),\n catch: (error): SwapAIError | ReferenceError => {\n if (error instanceof ReferenceEffectFailure) {\n return error.error as ReferenceError;\n }\n\n return classificationFailure(error);\n },\n }),\n ),\n );\n}\n"],"mappings":";;;;;AAAA,SAAS,QAAQ,QAAQ,eAAe;AAKxC,IAAM,yBAAN,MAA6C;AAAA,EAClC;AAAA,EAET,YAAY,OAAuB;AACjC,SAAK,QAAQ;AAAA,EACf;AACF;AAEA,SAAS,sBAAsB,OAA6B;AAC1D,MAAI,iBAAiB,aAAa;AAChC,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,YAAY,yBAAyB,yBAAyB;AAAA,IACvE,OAAO;AAAA,EACT,CAAC;AACH;AAEO,SAAS,SACd,YACA,OACoC;AACpC,SAAO,OAAO,WAAW;AAAA,IACvB,KAAK,MAAM,WAAW,SAAS,KAAK;AAAA,IACpC,OAAO;AAAA,EACT,CAAC;AACH;AAEO,SAAS,sBAKd,YACA,OACA,WAGmE;AACnE,SAAO,OAAO,QAAsB,EAAE;AAAA,IACpC,OAAO;AAAA,MAAQ,CAAC,YACd,OAAO,WAAW;AAAA,QAChB,KAAK,CAAC,WACJ,WAAW,SAAS,OAAO,OAAO,mBAAmB;AACnD,gBAAM,SAAS,MAAM,QAAQ,WAAW,OAAO;AAAA,YAC7C,OAAO,OAAO,UAAU,cAAc,CAAC;AAAA,YACvC,EAAE,OAAO;AAAA,UACX;AAEA,cAAI,OAAO,OAAO,MAAM,GAAG;AACzB,kBAAM,IAAI,uBAAuB,OAAO,IAAI;AAAA,UAC9C;AAEA,iBAAO,OAAO;AAAA,QAChB,CAAC;AAAA,QACH,OAAO,CAAC,UAAwC;AAC9C,cAAI,iBAAiB,wBAAwB;AAC3C,mBAAO,MAAM;AAAA,UACf;AAEA,iBAAO,sBAAsB,KAAK;AAAA,QACpC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,6 @@
1
+ import { a as ResultConfig, I as InitConfig, C as Classifier, b as ResultFor } from './types-DTbU1MEi.js';
2
+ export { B as BooleanResultConfig, N as NormalizedInitConfig, c as NumberResultConfig, d as ReferenceClassifier, e as ResultComparison, R as ResultValue, f as StringResultConfig, S as SwapAIError } from './types-DTbU1MEi.js';
3
+
4
+ declare function init<const Config extends ResultConfig>(inputConfig: InitConfig<Config>): Classifier<ResultFor<Config>>;
5
+
6
+ export { Classifier, InitConfig, ResultConfig, ResultFor, init };