dialai 0.1.0 → 1.0.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 (95) hide show
  1. package/LICENSE +18 -52
  2. package/README.md +32 -38
  3. package/dist/dialai/alignment.d.ts +51 -0
  4. package/dist/dialai/alignment.d.ts.map +1 -0
  5. package/dist/dialai/alignment.js +125 -0
  6. package/dist/dialai/alignment.js.map +1 -0
  7. package/dist/dialai/api.d.ts +131 -0
  8. package/dist/dialai/api.d.ts.map +1 -0
  9. package/dist/dialai/api.js +780 -0
  10. package/dist/dialai/api.js.map +1 -0
  11. package/dist/dialai/cli.d.ts +14 -0
  12. package/dist/dialai/cli.d.ts.map +1 -0
  13. package/dist/dialai/cli.js +83 -0
  14. package/dist/dialai/cli.js.map +1 -0
  15. package/dist/dialai/config.d.ts +42 -0
  16. package/dist/dialai/config.d.ts.map +1 -0
  17. package/dist/dialai/config.js +46 -0
  18. package/dist/dialai/config.js.map +1 -0
  19. package/dist/dialai/engine.d.ts +38 -0
  20. package/dist/dialai/engine.d.ts.map +1 -0
  21. package/dist/dialai/engine.js +276 -0
  22. package/dist/dialai/engine.js.map +1 -0
  23. package/dist/dialai/evaluation.d.ts +42 -0
  24. package/dist/dialai/evaluation.d.ts.map +1 -0
  25. package/dist/dialai/evaluation.js +106 -0
  26. package/dist/dialai/evaluation.js.map +1 -0
  27. package/dist/dialai/exemplars.d.ts +17 -0
  28. package/dist/dialai/exemplars.d.ts.map +1 -0
  29. package/dist/dialai/exemplars.js +33 -0
  30. package/dist/dialai/exemplars.js.map +1 -0
  31. package/dist/dialai/http-server.d.ts +11 -0
  32. package/dist/dialai/http-server.d.ts.map +1 -0
  33. package/dist/dialai/http-server.js +479 -0
  34. package/dist/dialai/http-server.js.map +1 -0
  35. package/dist/dialai/index.d.ts +22 -6
  36. package/dist/dialai/index.d.ts.map +1 -1
  37. package/dist/dialai/index.js +56 -6
  38. package/dist/dialai/index.js.map +1 -1
  39. package/dist/dialai/llm.d.ts +49 -0
  40. package/dist/dialai/llm.d.ts.map +1 -0
  41. package/dist/dialai/llm.js +157 -0
  42. package/dist/dialai/llm.js.map +1 -0
  43. package/dist/dialai/mcp.d.ts +16 -0
  44. package/dist/dialai/mcp.d.ts.map +1 -0
  45. package/dist/dialai/mcp.js +437 -0
  46. package/dist/dialai/mcp.js.map +1 -0
  47. package/dist/dialai/migrations/001-initial-schema.d.ts +8 -0
  48. package/dist/dialai/migrations/001-initial-schema.d.ts.map +1 -0
  49. package/dist/dialai/migrations/001-initial-schema.js +176 -0
  50. package/dist/dialai/migrations/001-initial-schema.js.map +1 -0
  51. package/dist/dialai/migrations/migrate.d.ts +11 -0
  52. package/dist/dialai/migrations/migrate.d.ts.map +1 -0
  53. package/dist/dialai/migrations/migrate.js +80 -0
  54. package/dist/dialai/migrations/migrate.js.map +1 -0
  55. package/dist/dialai/monitoring.d.ts +17 -0
  56. package/dist/dialai/monitoring.d.ts.map +1 -0
  57. package/dist/dialai/monitoring.js +173 -0
  58. package/dist/dialai/monitoring.js.map +1 -0
  59. package/dist/dialai/proxy-client.d.ts +23 -0
  60. package/dist/dialai/proxy-client.d.ts.map +1 -0
  61. package/dist/dialai/proxy-client.js +150 -0
  62. package/dist/dialai/proxy-client.js.map +1 -0
  63. package/dist/dialai/store-memory.d.ts +9 -0
  64. package/dist/dialai/store-memory.d.ts.map +1 -0
  65. package/dist/dialai/store-memory.js +113 -0
  66. package/dist/dialai/store-memory.js.map +1 -0
  67. package/dist/dialai/store-postgres.d.ts +9 -0
  68. package/dist/dialai/store-postgres.d.ts.map +1 -0
  69. package/dist/dialai/store-postgres.js +461 -0
  70. package/dist/dialai/store-postgres.js.map +1 -0
  71. package/dist/dialai/store.d.ts +44 -0
  72. package/dist/dialai/store.d.ts.map +1 -0
  73. package/dist/dialai/store.js +32 -0
  74. package/dist/dialai/store.js.map +1 -0
  75. package/dist/dialai/strategies.d.ts +43 -0
  76. package/dist/dialai/strategies.d.ts.map +1 -0
  77. package/dist/dialai/strategies.js +171 -0
  78. package/dist/dialai/strategies.js.map +1 -0
  79. package/dist/dialai/test-db.d.ts +15 -0
  80. package/dist/dialai/test-db.d.ts.map +1 -0
  81. package/dist/dialai/test-db.js +50 -0
  82. package/dist/dialai/test-db.js.map +1 -0
  83. package/dist/dialai/types.d.ts +564 -0
  84. package/dist/dialai/types.d.ts.map +1 -0
  85. package/dist/dialai/types.js +7 -0
  86. package/dist/dialai/types.js.map +1 -0
  87. package/dist/dialai/utils.d.ts +31 -0
  88. package/dist/dialai/utils.d.ts.map +1 -0
  89. package/dist/dialai/utils.js +89 -0
  90. package/dist/dialai/utils.js.map +1 -0
  91. package/dist/index.d.ts +7 -0
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +7 -0
  94. package/dist/index.js.map +1 -0
  95. package/package.json +29 -6
package/LICENSE CHANGED
@@ -1,55 +1,21 @@
1
- Business Source License 1.1
1
+ MIT License
2
2
 
3
3
  Copyright (c) 2025 DialAI Contributors
4
4
 
5
- Licensor: DialAI Contributors
6
-
7
- Licensed Work: DialAI
8
- The Licensed Work is (c) 2025 DialAI Contributors
9
-
10
- Additional Use Grant: You may not use the Licensed Work to provide a service to third parties, including but not limited to:
11
- - Offering the Licensed Work as a hosted or managed service
12
- - Using the Licensed Work in a production environment to provide services to third parties
13
- - Using the Licensed Work as part of a commercial offering that competes with the Licensed Work
14
-
15
- However, the following uses are permitted:
16
- - Using the Licensed Work for development, testing, or evaluation purposes
17
- - Using the Licensed Work in a non-production environment
18
- - Using the Licensed Work internally within your organization
19
- - Contributing to the Licensed Work
20
-
21
- Change Date: 2029-01-01
22
-
23
- Change License: Apache License 2.0
24
-
25
- On the Change Date specified above, the Licensed Work will automatically convert to the Change License, and You may use the Licensed Work under the terms of the Change License.
26
-
27
- NOTICE: The Business Source License is not an Open Source license. However, the Licensed Work will eventually be licensed under the Apache License 2.0 after the Change Date.
28
-
29
- ---
30
-
31
- Business Source License 1.1
32
-
33
- Terms and Conditions
34
-
35
- This Business Source License (this "License") applies to the Licensed Work. Each licensee is addressed as "you" or "Licensee."
36
-
37
- "Licensor" means the individual or entity that creates or contributes to the creation of the Licensed Work.
38
-
39
- "Licensed Work" means the work of authorship, whether in Source or Object form, made available under this License, as indicated by a copyright notice that is included in or attached to the work.
40
-
41
- "Derivative Works" means any work, whether in Source or Object form, that is based on (or derived from) the Licensed Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship.
42
-
43
- "Contribution" means any work of authorship, including the original version of the Licensed Work and any modifications or additions to that Licensed Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Licensed Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner.
44
-
45
- "Grant of Copyright License" means each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Licensed Work and such Derivative Works in Source or Object form.
46
-
47
- "Grant of Patent License" means each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Licensed Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Licensed Work to which such Contribution(s) was submitted.
48
-
49
- Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Licensed Work and such Derivative Works in Source or Object form.
50
-
51
- The above grant is subject to the Additional Use Grant specified in the header of this License.
52
-
53
- If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Licensed Work or a Contribution incorporated within the Licensed Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Licensed Work shall terminate as of the date such litigation is filed.
54
-
55
- THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.
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 CHANGED
@@ -4,15 +4,20 @@
4
4
  <p><strong>Dynamic Integration between AI and Labor</strong></p>
5
5
  <p>A coordination framework for AI and human specialists making<br>decisions together within state machines.</p>
6
6
  <br>
7
+
8
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
9
+ [![npm version](https://img.shields.io/npm/v/dialai.svg)](https://www.npmjs.com/package/dialai)
10
+ [![CI](https://github.com/eloquentanalytics/dialai/actions/workflows/ci.yml/badge.svg)](https://github.com/eloquentanalytics/dialai/actions/workflows/ci.yml)
11
+
7
12
  <a href="https://eloquentanalytics.github.io/dialai/"><strong>Documentation</strong></a> · <a href="https://eloquentanalytics.github.io/dialai/docs/getting-started/installation"><strong>Get Started</strong></a> · <a href="https://github.com/eloquentanalytics/dialai/issues"><strong>Issues</strong></a>
8
13
  <br><br>
9
14
  </div>
10
15
 
11
16
  ## Overview
12
17
 
13
- DialAI provides a framework for answering a fundamental question: *Given any task modeled as a state machine, how do you know — in dollars, time, and quality — exactly what it would cost to turn that task over to a minimally competent AI decision-maker?*
18
+ DIAL provides a framework for answering a fundamental question: *Given any task modeled as a state machine, how do you know — in dollars, time, and quality — exactly what it would cost to turn that task over to a minimally competent AI decision-maker?*
14
19
 
15
- DialAI starts from a deliberately pessimistic assumption: **AI has no role.** LLM specialists begin with weight 0.0. The default assumption is that the task is too difficult for AI and only humans can navigate it. DialAI then provides the mechanism to prove otherwise, one decision at a time.
20
+ DIAL starts from a deliberately pessimistic assumption: **AI has no role.** The default is that the task is too difficult for AI and only humans can navigate it. DIAL then provides the mechanism to prove otherwise, one decision at a time.
16
21
 
17
22
  ## Key Principles
18
23
 
@@ -20,30 +25,40 @@ DialAI starts from a deliberately pessimistic assumption: **AI has no role.** LL
20
25
  - **Progressive Collapse**: Over repeated decision cycles, measuring how well AI predicts human choices causes the multi-agent deliberation structure to progressively collapse into deterministic execution.
21
26
  - **Empirical Trust**: Trust is earned through demonstrated alignment with human decisions, not assumed.
22
27
 
23
- ## Quick Start
28
+ ## Install
24
29
 
25
30
  ```bash
26
31
  npm install dialai
27
32
  ```
28
33
 
34
+ ## Quick Start
35
+
29
36
  ```typescript
30
37
  import { createSession, registerSpecialist } from "dialai";
31
38
 
32
39
  // Create a session with a state machine
33
40
  const session = createSession({
34
- sessionTypeName: "my-task",
35
- initialState: "idle"
41
+ machineName: "my-task",
42
+ initialState: "idle",
36
43
  });
37
44
 
38
45
  // Register an AI specialist
39
46
  await registerSpecialist({
40
47
  specialistId: "specialist.my-task.proposer.gpt-4",
41
- sessionTypeName: "my-task",
48
+ machineName: "my-task",
42
49
  specialistRole: "proposer",
43
- modelId: "gpt-4"
50
+ modelId: "gpt-4",
44
51
  });
45
52
  ```
46
53
 
54
+ ## Packages
55
+
56
+ | Package | Description |
57
+ |---|---|
58
+ | `dialai` | Core library — engine, types, in-memory store, CLI, MCP server |
59
+ | `dialai/store-postgres` | PostgreSQL store implementation (Kysely) |
60
+ | `dialai/migrations` | Database migration runner for PostgreSQL |
61
+
47
62
  ## Documentation
48
63
 
49
64
  Full documentation is available at [https://eloquentanalytics.github.io/dialai/](https://eloquentanalytics.github.io/dialai/).
@@ -51,39 +66,18 @@ Full documentation is available at [https://eloquentanalytics.github.io/dialai/]
51
66
  ## Development
52
67
 
53
68
  ```bash
54
- # Install dependencies
55
- npm install
56
-
57
- # Run tests
58
- npm test
59
-
60
- # Build
61
- npm run build
62
-
63
- # Type check
64
- npm run typecheck
65
-
66
- # Lint
67
- npm run lint
69
+ npm install # Install dependencies
70
+ npm test # Run tests
71
+ npm run build # Build
72
+ npm run typecheck # Type check
73
+ npm run lint # Lint
74
+ npm run ci # Full CI pipeline
68
75
  ```
69
76
 
70
- ## License
71
-
72
- DialAI is licensed under the Business Source License 1.1 (BUSL-1.1). This license allows free use for development, testing, evaluation, and internal use, but restricts commercial use, particularly by cloud providers offering DialAI as a hosted or managed service.
73
-
74
- **Key restrictions:**
75
- - You may not use DialAI to provide a service to third parties
76
- - You may not offer DialAI as a hosted or managed service
77
- - You may not use DialAI in a production environment to provide services to third parties
77
+ ## Contributing
78
78
 
79
- **Permitted uses:**
80
- - Development, testing, and evaluation
81
- - Internal use within your organization
82
- - Contributing to the project
79
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development workflow and guidelines.
83
80
 
84
- **Change Date:** January 1, 2029
85
- **Change License:** Apache License 2.0
86
-
87
- On the Change Date, DialAI will automatically convert to the Apache License 2.0, making it fully open source. This approach protects the project from cloud provider exploitation while ensuring eventual open source availability.
81
+ ## License
88
82
 
89
- See [LICENSE](LICENSE) for full terms.
83
+ [MIT](LICENSE)
@@ -0,0 +1,51 @@
1
+ /**
2
+ * DIAL AI Alignment System
3
+ *
4
+ * Tracks how well AI specialists align with human decisions.
5
+ * Alignment scores are used for weighted consensus and progressive collapse.
6
+ */
7
+ import type { AlignmentRecord, Proposal } from "./types.js";
8
+ /**
9
+ * Wilson score lower bound for a binomial proportion.
10
+ * Returns the lower bound of a confidence interval, naturally penalizing
11
+ * small sample sizes. Used instead of naive `matches/total` so that
12
+ * 1/1 ≈ 0.21 (not 1.0) and confidence grows with evidence.
13
+ *
14
+ * @param matches - Number of successes
15
+ * @param total - Total number of trials
16
+ * @param z - Z-score for confidence level (default 1.96 = 95%)
17
+ */
18
+ export declare function wilsonLowerBound(matches: number, total: number, z?: number): number;
19
+ /**
20
+ * Checks whether a specialist is human by looking at the isHuman flag
21
+ * on the registered specialist. No string matching on specialistId.
22
+ */
23
+ export declare function isHumanSpecialist(specialistId: string): Promise<boolean>;
24
+ /**
25
+ * Returns the alignment score for a specialist on a machine.
26
+ * Returns 1.0 for human specialists (always perfectly aligned).
27
+ * Returns 0 for unknown specialists.
28
+ */
29
+ export declare function getAlignmentScore(specialistId: string, machineName: string, state?: string): Promise<number>;
30
+ /**
31
+ * Updates the alignment record for a specialist after a single comparison.
32
+ */
33
+ export declare function updateAlignment(specialistId: string, machineName: string, matched: boolean, state?: string): Promise<void>;
34
+ /**
35
+ * Pure function: computes alignment updates for all specialists given a human decision.
36
+ * Returns an array of { specialistId, matched } without touching the store.
37
+ */
38
+ export declare function computeAlignmentUpdates(humanTransitionName: string, proposals: Proposal[], humanSpecialistIds: Set<string>): Array<{
39
+ specialistId: string;
40
+ matched: boolean;
41
+ }>;
42
+ /**
43
+ * Updates alignment for all specialists after a human forces a decision.
44
+ * Thin wrapper: builds the human IDs set, calls the pure function, applies results.
45
+ */
46
+ export declare function updateAlignmentAfterHumanDecision(machineName: string, humanTransitionName: string, proposals: Proposal[], state?: string): Promise<void>;
47
+ /**
48
+ * Returns all alignment records for a machine.
49
+ */
50
+ export declare function getAllAlignmentRecords(machineName: string, state?: string): Promise<AlignmentRecord[]>;
51
+ //# sourceMappingURL=alignment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alignment.d.ts","sourceRoot":"","sources":["../../src/dialai/alignment.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE5D;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,SAAO,GAAG,MAAM,CAOjF;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAI9E;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED;;GAEG;AACH,wBAAsB,eAAe,CACnC,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,OAAO,EAChB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CA2Bf;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,mBAAmB,EAAE,MAAM,EAC3B,SAAS,EAAE,QAAQ,EAAE,EACrB,kBAAkB,EAAE,GAAG,CAAC,MAAM,CAAC,GAC9B,KAAK,CAAC;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAWnD;AAED;;;GAGG;AACH,wBAAsB,iCAAiC,CACrD,WAAW,EAAE,MAAM,EACnB,mBAAmB,EAAE,MAAM,EAC3B,SAAS,EAAE,QAAQ,EAAE,EACrB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC,CAmBf;AAED;;GAEG;AACH,wBAAsB,sBAAsB,CAC1C,WAAW,EAAE,MAAM,EACnB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,eAAe,EAAE,CAAC,CAE5B"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * DIAL AI Alignment System
3
+ *
4
+ * Tracks how well AI specialists align with human decisions.
5
+ * Alignment scores are used for weighted consensus and progressive collapse.
6
+ */
7
+ import { getStore } from "./store.js";
8
+ /**
9
+ * Wilson score lower bound for a binomial proportion.
10
+ * Returns the lower bound of a confidence interval, naturally penalizing
11
+ * small sample sizes. Used instead of naive `matches/total` so that
12
+ * 1/1 ≈ 0.21 (not 1.0) and confidence grows with evidence.
13
+ *
14
+ * @param matches - Number of successes
15
+ * @param total - Total number of trials
16
+ * @param z - Z-score for confidence level (default 1.96 = 95%)
17
+ */
18
+ export function wilsonLowerBound(matches, total, z = 1.96) {
19
+ if (total === 0)
20
+ return 0;
21
+ const phat = matches / total;
22
+ const z2 = z * z;
23
+ const numerator = phat + z2 / (2 * total) - z * Math.sqrt((phat * (1 - phat) + z2 / (4 * total)) / total);
24
+ const denominator = 1 + z2 / total;
25
+ return Math.max(0, numerator / denominator);
26
+ }
27
+ /**
28
+ * Checks whether a specialist is human by looking at the isHuman flag
29
+ * on the registered specialist. No string matching on specialistId.
30
+ */
31
+ export async function isHumanSpecialist(specialistId) {
32
+ const specialist = await getStore().getSpecialist(specialistId);
33
+ if (!specialist)
34
+ return false;
35
+ return "isHuman" in specialist && specialist.isHuman === true;
36
+ }
37
+ /**
38
+ * Returns the alignment score for a specialist on a machine.
39
+ * Returns 1.0 for human specialists (always perfectly aligned).
40
+ * Returns 0 for unknown specialists.
41
+ */
42
+ export async function getAlignmentScore(specialistId, machineName, state) {
43
+ if (await isHumanSpecialist(specialistId)) {
44
+ return 1.0;
45
+ }
46
+ const key = state
47
+ ? `${specialistId}:${machineName}:${state}`
48
+ : `${specialistId}:${machineName}`;
49
+ const record = await getStore().getAlignmentRecord(key);
50
+ if (!record)
51
+ return 0;
52
+ return record.alignmentScore;
53
+ }
54
+ /**
55
+ * Updates the alignment record for a specialist after a single comparison.
56
+ */
57
+ export async function updateAlignment(specialistId, machineName, matched, state) {
58
+ // Don't track alignment for human specialists
59
+ if (await isHumanSpecialist(specialistId))
60
+ return;
61
+ const key = state
62
+ ? `${specialistId}:${machineName}:${state}`
63
+ : `${specialistId}:${machineName}`;
64
+ const existing = await getStore().getAlignmentRecord(key);
65
+ if (existing) {
66
+ existing.matchingChoices += matched ? 1 : 0;
67
+ existing.totalComparisons += 1;
68
+ existing.alignmentScore =
69
+ wilsonLowerBound(existing.matchingChoices, existing.totalComparisons);
70
+ existing.lastUpdated = new Date();
71
+ await getStore().setAlignmentRecord(key, existing);
72
+ }
73
+ else {
74
+ await getStore().setAlignmentRecord(key, {
75
+ specialistId,
76
+ machineName,
77
+ state,
78
+ matchingChoices: matched ? 1 : 0,
79
+ totalComparisons: 1,
80
+ alignmentScore: wilsonLowerBound(matched ? 1 : 0, 1),
81
+ lastUpdated: new Date(),
82
+ });
83
+ }
84
+ }
85
+ /**
86
+ * Pure function: computes alignment updates for all specialists given a human decision.
87
+ * Returns an array of { specialistId, matched } without touching the store.
88
+ */
89
+ export function computeAlignmentUpdates(humanTransitionName, proposals, humanSpecialistIds) {
90
+ const results = [];
91
+ // Check proposers: did they propose the same transition?
92
+ for (const proposal of proposals) {
93
+ if (humanSpecialistIds.has(proposal.specialistId))
94
+ continue;
95
+ const matched = proposal.transitionName === humanTransitionName;
96
+ results.push({ specialistId: proposal.specialistId, matched });
97
+ }
98
+ return results;
99
+ }
100
+ /**
101
+ * Updates alignment for all specialists after a human forces a decision.
102
+ * Thin wrapper: builds the human IDs set, calls the pure function, applies results.
103
+ */
104
+ export async function updateAlignmentAfterHumanDecision(machineName, humanTransitionName, proposals, state) {
105
+ // Build set of human specialist IDs
106
+ const allSpecialistIds = new Set();
107
+ for (const p of proposals)
108
+ allSpecialistIds.add(p.specialistId);
109
+ const humanSpecialistIds = new Set();
110
+ for (const id of allSpecialistIds) {
111
+ if (await isHumanSpecialist(id))
112
+ humanSpecialistIds.add(id);
113
+ }
114
+ const updates = computeAlignmentUpdates(humanTransitionName, proposals, humanSpecialistIds);
115
+ for (const { specialistId, matched } of updates) {
116
+ await updateAlignment(specialistId, machineName, matched, state);
117
+ }
118
+ }
119
+ /**
120
+ * Returns all alignment records for a machine.
121
+ */
122
+ export async function getAllAlignmentRecords(machineName, state) {
123
+ return getStore().getAlignmentRecordsByMachine(machineName, state);
124
+ }
125
+ //# sourceMappingURL=alignment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alignment.js","sourceRoot":"","sources":["../../src/dialai/alignment.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe,EAAE,KAAa,EAAE,CAAC,GAAG,IAAI;IACvE,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAC1B,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAC;IAC7B,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACjB,MAAM,SAAS,GAAG,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;IAC1G,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;IACnC,OAAO,IA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,EAAE,CAAC,CAAC;IACjE,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iCAAiC,CACrD,WAAmB,EACnB,mBAA2B,EAC3B,SAAqB,EACrB,KAAc;IAEd,oCAAoC;IACpC,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAU,CAAC;IAC3C,KAAK,MAAM,CAAC,IAAI,SAAS;QAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;IAEhE,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAU,CAAC;IAC7C,KAAK,MAAM,EAAE,IAAI,gBAAgB,EAAE,CAAC;QAClC,IAAI,MAAM,iBAAiB,CAAC,EAAE,CAAC;YAAE,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,OAAO,GAAG,uBAAuB,CACrC,mBAAmB,EACnB,SAAS,EACT,kBAAkB,CACnB,CAAC;IAEF,KAAK,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,IAAI,OAAO,EAAE,CAAC;QAChD,MAAM,eAAe,CAAC,YAAY,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,WAAmB,EACnB,KAAc;IAEd,OAAO,QAAQ,EAAE,CAAC,4BAA4B,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AACrE,CAAC"}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * DIAL AI Core API
3
+ *
4
+ * Core API functions for session management, specialist registration,
5
+ * and the decision cycle.
6
+ */
7
+ import type { MachineDefinition, Session, Proposal, Proposer, Arbiter, Specialist, RegisterProposerOptions, RegisterArbiterOptions, ProposerContext, ArbiterContext, ConsensusResult, ArbitrationResult, ArbitrationPath, SubmitProposalOptions, SubmitArbitrationOptions } from "./types.js";
8
+ /**
9
+ * Creates a new session instance from a machine definition.
10
+ *
11
+ * @param machine - The machine definition to instantiate
12
+ * @returns The created session
13
+ */
14
+ export declare function createSession(machine: MachineDefinition, metaJson?: Record<string, unknown>): Promise<Session>;
15
+ /**
16
+ * Retrieves a session by its ID.
17
+ *
18
+ * @param sessionId - The session ID to look up
19
+ * @returns The session
20
+ * @throws If session not found
21
+ */
22
+ export declare function getSession(sessionId: string): Promise<Session>;
23
+ /**
24
+ * Returns all stored sessions.
25
+ *
26
+ * @returns Array of all sessions
27
+ */
28
+ export declare function getSessions(): Promise<Session[]>;
29
+ /**
30
+ * Registers a proposer specialist for a machine.
31
+ *
32
+ * @param opts - Registration options
33
+ * @returns The registered proposer
34
+ */
35
+ export declare function registerProposer(opts: RegisterProposerOptions): Promise<Proposer>;
36
+ /**
37
+ * Registers an arbiter specialist for a machine.
38
+ *
39
+ * @param opts - Registration options
40
+ * @returns The registered arbiter
41
+ */
42
+ export declare function registerArbiter(opts: RegisterArbiterOptions): Promise<Arbiter>;
43
+ /**
44
+ * Gets a specialist by ID.
45
+ */
46
+ export declare function getSpecialist(specialistId: string): Promise<(Specialist | Arbiter) | undefined>;
47
+ /**
48
+ * Gets all proposers for a machine.
49
+ */
50
+ export declare function getProposers(machineName: string): Promise<Proposer[]>;
51
+ /**
52
+ * Gets the arbiter for a machine.
53
+ */
54
+ export declare function getArbiter(machineName: string): Promise<Arbiter | undefined>;
55
+ /**
56
+ * Enables a specialist (sets enabled = true).
57
+ */
58
+ export declare function enableSpecialist(specialistId: string): Promise<void>;
59
+ /**
60
+ * Disables a specialist (sets enabled = false).
61
+ */
62
+ export declare function disableSpecialist(specialistId: string): Promise<void>;
63
+ /**
64
+ * Gets enabled proposers for a machine (enabled is true or undefined).
65
+ */
66
+ export declare function getEnabledProposers(machineName: string): Promise<Proposer[]>;
67
+ /**
68
+ * Gets the enabled arbiter for a machine.
69
+ */
70
+ export declare function getEnabledArbiter(machineName: string): Promise<Arbiter | undefined>;
71
+ /**
72
+ * Gets proposers declared in the current state's specialists array.
73
+ * Falls back to getProposers(machineName) if state has no specialists.
74
+ * Respects both state-level `disabled` and global `enabled` flags.
75
+ */
76
+ export declare function getProposersForState(session: Session): Promise<Proposer[]>;
77
+ /**
78
+ * Gets enabled proposers for the current state.
79
+ * Filters out specialists that are disabled at the state level or globally.
80
+ */
81
+ export declare function getEnabledProposersForState(session: Session): Promise<Proposer[]>;
82
+ /**
83
+ * Gets the arbiter declared in the current state's specialists array.
84
+ * Falls back to getArbiter(machineName).
85
+ */
86
+ export declare function getArbiterForState(session: Session): Promise<Arbiter | undefined>;
87
+ /**
88
+ * Gets the effective consensus threshold for a session's current state.
89
+ * Priority: state > machine > arbiter. Returns undefined if none configured.
90
+ */
91
+ export declare function getEffectiveThreshold(session: Session): Promise<number | undefined>;
92
+ /**
93
+ * Resolves a proposer's local strategy function, or null for side-effectful modes.
94
+ */
95
+ export declare function resolveProposerStrategy(proposer: Proposer): ((ctx: ProposerContext) => Promise<{
96
+ transitionName: string;
97
+ toState: string;
98
+ reasoning: string;
99
+ }>) | null;
100
+ /**
101
+ * Resolves an arbiter's local strategy function, or null for side-effectful modes.
102
+ */
103
+ export declare function resolveArbiterStrategy(arbiter: Arbiter): ((ctx: ArbiterContext) => Promise<ConsensusResult>) | null;
104
+ /**
105
+ * Creates and stores a proposal.
106
+ * If transitionName is omitted, invokes the specialist's registered strategy.
107
+ */
108
+ export declare function submitProposal(opts: SubmitProposalOptions): Promise<Proposal>;
109
+ /**
110
+ * Gets all proposals for a session's current round.
111
+ */
112
+ export declare function getProposalsForRound(sessionId: string, roundId: string): Promise<Proposal[]>;
113
+ /**
114
+ * Evaluates whether consensus has been reached for a session.
115
+ * Read-only operation - does not execute any transition.
116
+ */
117
+ export declare function evaluateConsensus(sessionId: string): Promise<ConsensusResult>;
118
+ /**
119
+ * Pure function: classifies which path submitArbitration should take
120
+ * based on round staleness, human status, transition validity, and proposal count.
121
+ */
122
+ export declare function classifyArbitration(currentRoundId: string, effectiveRoundId: string, isHuman: boolean, transitionName: string | undefined, currentStateTransitions: Record<string, string> | undefined, proposalCount: number, currentState: string): ArbitrationPath;
123
+ /**
124
+ * Evaluates consensus and optionally executes the winning transition.
125
+ */
126
+ export declare function submitArbitration(opts: SubmitArbitrationOptions): Promise<ArbitrationResult>;
127
+ /**
128
+ * Executes a state transition on a session.
129
+ */
130
+ export declare function executeTransition(sessionId: string, transitionName: string, toState: string, reasoning?: string): Promise<Session>;
131
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/dialai/api.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EACV,iBAAiB,EACjB,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,UAAU,EACV,uBAAuB,EACvB,sBAAsB,EACtB,eAAe,EACf,cAAc,EACd,eAAe,EACf,iBAAiB,EACjB,eAAe,EAGf,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,YAAY,CAAC;AAMpB;;;;;GAKG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,iBAAiB,EAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,OAAO,CAAC,OAAO,CAAC,CAgDlB;AAED;;;;;;GAMG;AACH,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAMpE;AAED;;;;GAIG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC,CAEtD;AAyID;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,uBAAuB,GAC5B,OAAO,CAAC,QAAQ,CAAC,CA4BnB;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,sBAAsB,GAC3B,OAAO,CAAC,OAAO,CAAC,CAwBlB;AAED;;GAEG;AACH,wBAAsB,a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