@ddse/acm-sdk 0.5.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 (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +221 -0
  3. package/dist/capability.d.ts +10 -0
  4. package/dist/capability.d.ts.map +1 -0
  5. package/dist/capability.js +3 -0
  6. package/dist/capability.js.map +1 -0
  7. package/dist/index.d.ts +8 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +9 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/policy.d.ts +5 -0
  12. package/dist/policy.d.ts.map +1 -0
  13. package/dist/policy.js +2 -0
  14. package/dist/policy.js.map +1 -0
  15. package/dist/registry.d.ts +6 -0
  16. package/dist/registry.d.ts.map +1 -0
  17. package/dist/registry.js +3 -0
  18. package/dist/registry.js.map +1 -0
  19. package/dist/src/capability-map.d.ts +6 -0
  20. package/dist/src/capability-map.d.ts.map +1 -0
  21. package/dist/src/capability-map.js +281 -0
  22. package/dist/src/capability-map.js.map +1 -0
  23. package/dist/src/capability.d.ts +10 -0
  24. package/dist/src/capability.d.ts.map +1 -0
  25. package/dist/src/capability.js +3 -0
  26. package/dist/src/capability.js.map +1 -0
  27. package/dist/src/context-provider.d.ts +36 -0
  28. package/dist/src/context-provider.d.ts.map +1 -0
  29. package/dist/src/context-provider.js +59 -0
  30. package/dist/src/context-provider.js.map +1 -0
  31. package/dist/src/context.d.ts +42 -0
  32. package/dist/src/context.d.ts.map +1 -0
  33. package/dist/src/context.js +148 -0
  34. package/dist/src/context.js.map +1 -0
  35. package/dist/src/index.d.ts +12 -0
  36. package/dist/src/index.d.ts.map +1 -0
  37. package/dist/src/index.js +13 -0
  38. package/dist/src/index.js.map +1 -0
  39. package/dist/src/nucleus.d.ts +105 -0
  40. package/dist/src/nucleus.d.ts.map +1 -0
  41. package/dist/src/nucleus.js +208 -0
  42. package/dist/src/nucleus.js.map +1 -0
  43. package/dist/src/policy.d.ts +5 -0
  44. package/dist/src/policy.d.ts.map +1 -0
  45. package/dist/src/policy.js +2 -0
  46. package/dist/src/policy.js.map +1 -0
  47. package/dist/src/registry.d.ts +6 -0
  48. package/dist/src/registry.d.ts.map +1 -0
  49. package/dist/src/registry.js +3 -0
  50. package/dist/src/registry.js.map +1 -0
  51. package/dist/src/stream.d.ts +8 -0
  52. package/dist/src/stream.d.ts.map +1 -0
  53. package/dist/src/stream.js +19 -0
  54. package/dist/src/stream.js.map +1 -0
  55. package/dist/src/task.d.ts +24 -0
  56. package/dist/src/task.d.ts.map +1 -0
  57. package/dist/src/task.js +11 -0
  58. package/dist/src/task.js.map +1 -0
  59. package/dist/src/tool.d.ts +5 -0
  60. package/dist/src/tool.d.ts.map +1 -0
  61. package/dist/src/tool.js +4 -0
  62. package/dist/src/tool.js.map +1 -0
  63. package/dist/src/types.d.ts +195 -0
  64. package/dist/src/types.d.ts.map +1 -0
  65. package/dist/src/types.js +2 -0
  66. package/dist/src/types.js.map +1 -0
  67. package/dist/src/utils.d.ts +22 -0
  68. package/dist/src/utils.d.ts.map +1 -0
  69. package/dist/src/utils.js +194 -0
  70. package/dist/src/utils.js.map +1 -0
  71. package/dist/src/validators.d.ts +20 -0
  72. package/dist/src/validators.d.ts.map +1 -0
  73. package/dist/src/validators.js +209 -0
  74. package/dist/src/validators.js.map +1 -0
  75. package/dist/stream.d.ts +8 -0
  76. package/dist/stream.d.ts.map +1 -0
  77. package/dist/stream.js +19 -0
  78. package/dist/stream.js.map +1 -0
  79. package/dist/task.d.ts +11 -0
  80. package/dist/task.d.ts.map +1 -0
  81. package/dist/task.js +9 -0
  82. package/dist/task.js.map +1 -0
  83. package/dist/tests/phase4-integration.test.d.ts +2 -0
  84. package/dist/tests/phase4-integration.test.d.ts.map +1 -0
  85. package/dist/tests/phase4-integration.test.js +115 -0
  86. package/dist/tests/phase4-integration.test.js.map +1 -0
  87. package/dist/tool.d.ts +5 -0
  88. package/dist/tool.d.ts.map +1 -0
  89. package/dist/tool.js +4 -0
  90. package/dist/tool.js.map +1 -0
  91. package/dist/tsconfig.tsbuildinfo +1 -0
  92. package/dist/types.d.ts +77 -0
  93. package/dist/types.d.ts.map +1 -0
  94. package/dist/types.js +3 -0
  95. package/dist/types.js.map +1 -0
  96. package/package.json +26 -0
  97. package/src/capability.ts +11 -0
  98. package/src/context-provider.ts +136 -0
  99. package/src/context.ts +190 -0
  100. package/src/index.ts +12 -0
  101. package/src/nucleus.ts +341 -0
  102. package/src/policy.ts +9 -0
  103. package/src/registry.ts +7 -0
  104. package/src/stream.ts +24 -0
  105. package/src/task.ts +38 -0
  106. package/src/tool.ts +6 -0
  107. package/src/types.ts +233 -0
  108. package/src/utils.ts +271 -0
  109. package/tests/phase4-integration.test.ts +138 -0
  110. package/tsconfig.json +8 -0
  111. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,36 @@
1
+ import type { Nucleus } from './nucleus.js';
2
+ import type { RunContext, InternalContextScope } from './types.js';
3
+ import type { Tool } from './tool.js';
4
+ export type ContextRetrievalArtifact = {
5
+ type: string;
6
+ content: any;
7
+ promote?: boolean;
8
+ provenance?: {
9
+ tool?: string;
10
+ rationale?: string;
11
+ [key: string]: any;
12
+ };
13
+ };
14
+ export type ContextRetrievalResult = ContextRetrievalArtifact | ContextRetrievalArtifact[] | null | undefined;
15
+ export type ContextRetrievalTool = Tool<any, ContextRetrievalResult>;
16
+ export type ContextRetrievalBindingOptions = {
17
+ match?: (directive: string) => boolean;
18
+ buildInput?: (directive: string, context: {
19
+ runContext?: RunContext;
20
+ nucleus?: Nucleus;
21
+ }) => any;
22
+ autoPromote?: boolean;
23
+ maxArtifacts?: number;
24
+ describe?: string;
25
+ };
26
+ export declare class ExternalContextProviderAdapter {
27
+ private bindings;
28
+ register(tool: ContextRetrievalTool, options?: ContextRetrievalBindingOptions): void;
29
+ fulfill(request: {
30
+ directives: string[];
31
+ scope: InternalContextScope;
32
+ runContext?: RunContext;
33
+ nucleus?: Nucleus;
34
+ }): Promise<void>;
35
+ }
36
+ //# sourceMappingURL=context-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-provider.d.ts","sourceRoot":"","sources":["../../src/context-provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AACnE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE;QACX,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAC9B,wBAAwB,GACxB,wBAAwB,EAAE,GAC1B,IAAI,GACJ,SAAS,CAAC;AAEd,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,GAAG,EAAE,sBAAsB,CAAC,CAAC;AAErE,MAAM,MAAM,8BAA8B,GAAG;IAC3C,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC;IACvC,UAAU,CAAC,EAAE,CACX,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE;QACP,UAAU,CAAC,EAAE,UAAU,CAAC;QACxB,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,KACE,GAAG,CAAC;IACT,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAiBF,qBAAa,8BAA8B;IACzC,OAAO,CAAC,QAAQ,CAAiB;IAEjC,QAAQ,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,8BAA8B,GAAG,IAAI;IA0B9E,OAAO,CAAC,OAAO,EAAE;QACrB,UAAU,EAAE,MAAM,EAAE,CAAC;QACrB,KAAK,EAAE,oBAAoB,CAAC;QAC5B,UAAU,CAAC,EAAE,UAAU,CAAC;QACxB,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,GAAG,OAAO,CAAC,IAAI,CAAC;CAiDlB"}
@@ -0,0 +1,59 @@
1
+ export class ExternalContextProviderAdapter {
2
+ bindings = [];
3
+ register(tool, options) {
4
+ const match = options?.match ??
5
+ ((directive) => directive.startsWith(`${tool.name()}:`) || directive.startsWith(`${tool.name()}::`));
6
+ const buildInput = options?.buildInput ?? ((directive) => {
7
+ const separatorIndex = directive.indexOf(':');
8
+ const payload = separatorIndex >= 0 ? directive.slice(separatorIndex + 1) : undefined;
9
+ return {
10
+ directive,
11
+ payload,
12
+ };
13
+ });
14
+ this.bindings.push({
15
+ tool,
16
+ match,
17
+ buildInput,
18
+ autoPromote: options?.autoPromote ?? true,
19
+ maxArtifacts: options?.maxArtifacts ?? 16,
20
+ describe: options?.describe,
21
+ });
22
+ }
23
+ async fulfill(request) {
24
+ const { directives, scope, runContext, nucleus } = request;
25
+ const unresolved = [];
26
+ for (const directive of directives) {
27
+ const binding = this.bindings.find(entry => entry.match(directive));
28
+ if (!binding) {
29
+ unresolved.push(directive);
30
+ continue;
31
+ }
32
+ const input = binding.buildInput(directive, { runContext, nucleus });
33
+ const result = await binding.tool.call(input);
34
+ if (!result) {
35
+ continue;
36
+ }
37
+ const artifacts = Array.isArray(result) ? result : [result];
38
+ if (artifacts.length > binding.maxArtifacts) {
39
+ throw new Error(`Tool ${binding.tool.name()} returned ${artifacts.length} artifacts for directive ${directive}, exceeding limit ${binding.maxArtifacts}`);
40
+ }
41
+ for (const artifact of artifacts) {
42
+ if (!artifact || typeof artifact.type !== 'string') {
43
+ throw new Error(`Tool ${binding.tool.name()} returned an invalid artifact for directive ${directive}`);
44
+ }
45
+ const provenance = artifact.provenance ?? {
46
+ tool: binding.tool.name(),
47
+ };
48
+ const artifactId = scope.addArtifact(artifact.type, artifact.content, provenance);
49
+ if (artifact.promote ?? binding.autoPromote) {
50
+ await scope.promote(artifactId);
51
+ }
52
+ }
53
+ }
54
+ if (unresolved.length > 0) {
55
+ throw new Error(`No context retrieval tool registered for directives: ${unresolved.join(', ')}`);
56
+ }
57
+ }
58
+ }
59
+ //# sourceMappingURL=context-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-provider.js","sourceRoot":"","sources":["../../src/context-provider.ts"],"names":[],"mappings":"AAoDA,MAAM,OAAO,8BAA8B;IACjC,QAAQ,GAAc,EAAE,CAAC;IAEjC,QAAQ,CAAC,IAA0B,EAAE,OAAwC;QAC3E,MAAM,KAAK,GACT,OAAO,EAAE,KAAK;YACd,CAAC,CAAC,SAAiB,EAAE,EAAE,CACrB,SAAS,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QAEzF,MAAM,UAAU,GACd,OAAO,EAAE,UAAU,IAAI,CAAC,CAAC,SAAiB,EAAE,EAAE;YAC5C,MAAM,cAAc,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC9C,MAAM,OAAO,GAAG,cAAc,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,cAAc,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YACtF,OAAO;gBACL,SAAS;gBACT,OAAO;aACR,CAAC;QACJ,CAAC,CAAC,CAAC;QAEL,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;YACjB,IAAI;YACJ,KAAK;YACL,UAAU;YACV,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,IAAI;YACzC,YA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QAAQ,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBACnD,MAAM,IAAI,KAAK,CACb,QAAQ,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,+CAA+C,SAAS,EAAE,CACtF,CAAC;gBACJ,CAAC;gBAED,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,IAAI;oBACxC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE;iBAC1B,CAAC;gBAEF,MAAM,UAAU,GAAG,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;gBAClF,IAAI,QAAQ,CAAC,OAAO,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;oBAC5C,MAAM,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CACb,wDAAwD,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAChF,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,42 @@
1
+ import type { ContextPacket, InternalContextScope, LedgerEntry } from './types.js';
2
+ export declare class ContextBuilder {
3
+ private sources;
4
+ private facts;
5
+ private assumptions;
6
+ private augmentations;
7
+ private provenance;
8
+ addSource(uri: string, type?: string, content?: any): this;
9
+ addFact(key: string, value: any): this;
10
+ addFacts(facts: Record<string, any>): this;
11
+ addAssumption(assumption: string): this;
12
+ addAugmentation(type: string, artifact: string): this;
13
+ setProvenance(provenance: ContextPacket['provenance']): this;
14
+ build(version?: string): ContextPacket;
15
+ private computePacketId;
16
+ private computeDigest;
17
+ static computeContextRef(context: ContextPacket): string;
18
+ }
19
+ export declare class InternalContextScopeImpl implements InternalContextScope {
20
+ artifacts: Array<{
21
+ id: string;
22
+ type: string;
23
+ content: any;
24
+ digest: string;
25
+ provenance?: {
26
+ retrievedAt: number;
27
+ tool?: string;
28
+ rationale?: string;
29
+ };
30
+ }>;
31
+ private ledgerAppend?;
32
+ private contextBuilder?;
33
+ constructor(ledgerAppend?: (entry: LedgerEntry) => void);
34
+ addArtifact(type: string, content: any, provenance?: {
35
+ tool?: string;
36
+ rationale?: string;
37
+ }): string;
38
+ promote(artifactId: string): Promise<void>;
39
+ getArtifact(id: string): any;
40
+ private computeDigest;
41
+ }
42
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEnF,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAgC;IAC/C,OAAO,CAAC,KAAK,CAA2B;IACxC,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,aAAa,CAAsC;IAC3D,OAAO,CAAC,UAAU,CAAmC;IAErD,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,GAAG,IAAI;IAM1D,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAKtC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAK1C,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAKvC,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAKrD,aAAa,CAAC,UAAU,EAAE,aAAa,CAAC,YAAY,CAAC,GAAG,IAAI;IAK5D,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,aAAa;IActC,OAAO,CAAC,eAAe;IAavB,OAAO,CAAC,aAAa;IAMrB,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM;CAYzD;AAED,qBAAa,wBAAyB,YAAW,oBAAoB;IACnE,SAAS,EAAE,KAAK,CAAC;QACf,EAAE,EAAE,MAAM,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,GAAG,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;QACf,UAAU,CAAC,EAAE;YACX,WAAW,EAAE,MAAM,CAAC;YACpB,IAAI,CAAC,EAAE,MAAM,CAAC;YACd,SAAS,CAAC,EAAE,MAAM,CAAC;SACpB,CAAC;KACH,CAAC,CAAM;IAER,OAAO,CAAC,YAAY,CAAC,CAA+B;IACpD,OAAO,CAAC,cAAc,CAAC,CAAiB;gBAE5B,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI;IAIvD,WAAW,CACT,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,GAAG,EACZ,UAAU,CAAC,EAAE;QACX,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,GACA,MAAM;IAmCH,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BhD,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,GAAG;IAK5B,OAAO,CAAC,aAAa;CAKtB"}
@@ -0,0 +1,148 @@
1
+ // Context orchestration utilities
2
+ import { createHash } from 'crypto';
3
+ export class ContextBuilder {
4
+ sources = [];
5
+ facts = {};
6
+ assumptions = [];
7
+ augmentations = [];
8
+ provenance = {};
9
+ addSource(uri, type, content) {
10
+ const digest = this.computeDigest(content ? JSON.stringify(content) : uri);
11
+ this.sources.push({ uri, digest, type });
12
+ return this;
13
+ }
14
+ addFact(key, value) {
15
+ this.facts[key] = value;
16
+ return this;
17
+ }
18
+ addFacts(facts) {
19
+ Object.assign(this.facts, facts);
20
+ return this;
21
+ }
22
+ addAssumption(assumption) {
23
+ this.assumptions.push(assumption);
24
+ return this;
25
+ }
26
+ addAugmentation(type, artifact) {
27
+ this.augmentations.push({ type, artifact });
28
+ return this;
29
+ }
30
+ setProvenance(provenance) {
31
+ this.provenance = provenance;
32
+ return this;
33
+ }
34
+ build(version) {
35
+ const packet = {
36
+ id: this.computePacketId(),
37
+ version,
38
+ sources: this.sources,
39
+ facts: this.facts,
40
+ assumptions: this.assumptions.length > 0 ? this.assumptions : undefined,
41
+ augmentations: this.augmentations,
42
+ provenance: this.provenance,
43
+ };
44
+ return packet;
45
+ }
46
+ computePacketId() {
47
+ // Compute SHA-256 hash of the normalized packet
48
+ const normalized = JSON.stringify({
49
+ sources: this.sources,
50
+ facts: this.facts,
51
+ assumptions: this.assumptions,
52
+ augmentations: this.augmentations,
53
+ });
54
+ const hash = createHash('sha256');
55
+ hash.update(normalized);
56
+ return `sha256-${hash.digest('hex')}`;
57
+ }
58
+ computeDigest(content) {
59
+ const hash = createHash('sha256');
60
+ hash.update(content);
61
+ return `sha256-${hash.digest('hex').substring(0, 32)}`;
62
+ }
63
+ static computeContextRef(context) {
64
+ // Compute full SHA-256 hash of the normalized context packet
65
+ const normalized = JSON.stringify({
66
+ sources: context.sources,
67
+ facts: context.facts,
68
+ assumptions: context.assumptions,
69
+ augmentations: context.augmentations,
70
+ });
71
+ const hash = createHash('sha256');
72
+ hash.update(normalized);
73
+ return hash.digest('hex');
74
+ }
75
+ }
76
+ export class InternalContextScopeImpl {
77
+ artifacts = [];
78
+ ledgerAppend;
79
+ contextBuilder;
80
+ constructor(ledgerAppend) {
81
+ this.ledgerAppend = ledgerAppend;
82
+ }
83
+ addArtifact(type, content, provenance) {
84
+ const id = `artifact-${Date.now()}-${Math.random().toString(36).substring(7)}`;
85
+ const digest = this.computeDigest(JSON.stringify(content));
86
+ this.artifacts.push({
87
+ id,
88
+ type,
89
+ content,
90
+ digest,
91
+ provenance: provenance
92
+ ? {
93
+ retrievedAt: Date.now(),
94
+ ...provenance,
95
+ }
96
+ : undefined,
97
+ });
98
+ // Record to ledger
99
+ if (this.ledgerAppend) {
100
+ this.ledgerAppend({
101
+ id: `context-internal-${Date.now()}-${Math.random().toString(36).substring(7)}`,
102
+ ts: Date.now(),
103
+ type: 'CONTEXT_INTERNALIZED',
104
+ details: {
105
+ artifactId: id,
106
+ type,
107
+ digest,
108
+ provenance,
109
+ },
110
+ });
111
+ }
112
+ return id;
113
+ }
114
+ async promote(artifactId) {
115
+ const artifact = this.artifacts.find(a => a.id === artifactId);
116
+ if (!artifact) {
117
+ throw new Error(`Artifact not found: ${artifactId}`);
118
+ }
119
+ // In a real implementation, this would create a new Context Packet version
120
+ // and append to the context builder
121
+ if (this.contextBuilder) {
122
+ this.contextBuilder.addAugmentation(artifact.type, artifact.id);
123
+ }
124
+ // Log promotion
125
+ if (this.ledgerAppend) {
126
+ this.ledgerAppend({
127
+ id: `context-promote-${Date.now()}-${Math.random().toString(36).substring(7)}`,
128
+ ts: Date.now(),
129
+ type: 'CONTEXT_INTERNALIZED',
130
+ details: {
131
+ action: 'promote',
132
+ artifactId,
133
+ digest: artifact.digest,
134
+ },
135
+ });
136
+ }
137
+ }
138
+ getArtifact(id) {
139
+ const artifact = this.artifacts.find(a => a.id === id);
140
+ return artifact?.content;
141
+ }
142
+ computeDigest(content) {
143
+ const hash = createHash('sha256');
144
+ hash.update(content);
145
+ return hash.digest('hex').substring(0, 32);
146
+ }
147
+ }
148
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGpC,MAAM,OAAO,cAAc;IACjB,OAAO,GAA6B,EAAE,CAAC;IACvC,KAAK,GAAwB,EAAE,CAAC;IAChC,WAAW,GAAa,EAAE,CAAC;IAC3B,aAAa,GAAmC,EAAE,CAAC;IACnD,UAAU,GAAgC,EAAE,CA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M,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,12 @@
1
+ export * from './types.js';
2
+ export * from './tool.js';
3
+ export * from './task.js';
4
+ export * from './capability.js';
5
+ export * from './registry.js';
6
+ export * from './policy.js';
7
+ export * from './stream.js';
8
+ export * from './nucleus.js';
9
+ export * from './context.js';
10
+ export * from './context-provider.js';
11
+ export * from './utils.js';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,13 @@
1
+ // SDK exports
2
+ export * from './types.js';
3
+ export * from './tool.js';
4
+ export * from './task.js';
5
+ export * from './capability.js';
6
+ export * from './registry.js';
7
+ export * from './policy.js';
8
+ export * from './stream.js';
9
+ export * from './nucleus.js';
10
+ export * from './context.js';
11
+ export * from './context-provider.js';
12
+ export * from './utils.js';
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc;AACd,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AACtC,cAAc,YAAY,CAAC"}
@@ -0,0 +1,105 @@
1
+ import type { Context, InternalContextScope, LedgerEntry } from './types.js';
2
+ export type NucleusToolDefinition = {
3
+ name: string;
4
+ description?: string;
5
+ inputSchema?: {
6
+ type: 'object';
7
+ properties: Record<string, any>;
8
+ required?: string[];
9
+ };
10
+ };
11
+ export type StructuredToolCall = {
12
+ id?: string;
13
+ name: string;
14
+ input: Record<string, any>;
15
+ output?: Record<string, any>;
16
+ error?: {
17
+ code: string;
18
+ message: string;
19
+ };
20
+ };
21
+ export type NucleusInvokeRequest = {
22
+ prompt?: string;
23
+ input?: any;
24
+ tools?: NucleusToolDefinition[];
25
+ };
26
+ export type NucleusInvokeResult = {
27
+ reasoning?: string;
28
+ toolCalls: StructuredToolCall[];
29
+ raw?: any;
30
+ };
31
+ export type NucleusConfig = {
32
+ goalId: string;
33
+ goalIntent: string;
34
+ planId?: string;
35
+ taskId?: string;
36
+ contextRef: string;
37
+ context?: Context;
38
+ llmCall: {
39
+ provider: string;
40
+ model: string;
41
+ temperature?: number;
42
+ seed?: number;
43
+ maxTokens?: number;
44
+ };
45
+ allowedTools?: string[];
46
+ hooks?: {
47
+ preflight?: boolean;
48
+ postcheck?: boolean;
49
+ };
50
+ promptTemplate?: string;
51
+ promptDigest?: string;
52
+ };
53
+ export type PreflightResult = {
54
+ status: 'OK';
55
+ } | {
56
+ status: 'NEEDS_CONTEXT';
57
+ retrievalDirectives: string[];
58
+ };
59
+ export type PostcheckResult = {
60
+ status: 'COMPLETE';
61
+ } | {
62
+ status: 'NEEDS_COMPENSATION';
63
+ reason: string;
64
+ } | {
65
+ status: 'ESCALATE';
66
+ reason: string;
67
+ };
68
+ export type LLMCallFn = (prompt: string, tools: NucleusToolDefinition[], config: NucleusConfig['llmCall']) => Promise<{
69
+ reasoning?: string;
70
+ toolCalls: StructuredToolCall[];
71
+ raw?: any;
72
+ }>;
73
+ export declare abstract class Nucleus {
74
+ protected config: NucleusConfig;
75
+ constructor(config: NucleusConfig);
76
+ abstract preflight(): Promise<PreflightResult>;
77
+ abstract invoke(request: NucleusInvokeRequest): Promise<NucleusInvokeResult>;
78
+ abstract postcheck(output: any): Promise<PostcheckResult>;
79
+ abstract recordInference(promptDigest: string, toolCalls: StructuredToolCall[], reasoning?: string): LedgerEntry;
80
+ abstract getInternalContext(): InternalContextScope | undefined;
81
+ abstract setInternalContext(scope: InternalContextScope): void;
82
+ getConfig(): NucleusConfig;
83
+ }
84
+ export declare class DeterministicNucleus extends Nucleus {
85
+ private llmCall;
86
+ private ledgerAppend;
87
+ private internalContext?;
88
+ private ledger;
89
+ constructor(config: NucleusConfig, llmCall: LLMCallFn, ledgerAppend: (entry: LedgerEntry) => void);
90
+ preflight(): Promise<PreflightResult>;
91
+ invoke(request: NucleusInvokeRequest): Promise<NucleusInvokeResult>;
92
+ postcheck(output: any): Promise<PostcheckResult>;
93
+ recordInference(promptDigest: string, toolCalls: StructuredToolCall[], reasoning?: string): LedgerEntry;
94
+ getInternalContext(): InternalContextScope | undefined;
95
+ setInternalContext(scope: InternalContextScope): void;
96
+ private callLLM;
97
+ private buildPreflightPrompt;
98
+ private buildInvokePrompt;
99
+ private buildPostcheckPrompt;
100
+ private computePromptDigest;
101
+ private computeDigest;
102
+ private renderContextSnapshot;
103
+ }
104
+ export type NucleusFactory = (config: NucleusConfig) => Nucleus;
105
+ //# sourceMappingURL=nucleus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nucleus.d.ts","sourceRoot":"","sources":["../../src/nucleus.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAoB,MAAM,YAAY,CAAC;AAE/F,MAAM,MAAM,qBAAqB,GAAG;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE;QACZ,IAAI,EAAE,QAA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B,EAAE,EAC/B,SAAS,CAAC,EAAE,MAAM,GACjB,WAAW;IAEd,QAAQ,CAAC,kBAAkB,IAAI,oBAAoB,GAAG,SAAS;IAC/D,QAAQ,CAAC,kBAAkB,CAAC,KAAK,EAAE,oBAAoB,GAAG,IAAI;IAE9D,SAAS,IAAI,aAAa;CAG3B;AAED,qBAAa,oBAAqB,SAAQ,OAAO;IAM7C,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,YAAY;IANtB,OAAO,CAAC,eAAe,CAAC,CAAuB;IAC/C,OAAO,CAAC,MAAM,CAAqB;gBAGjC,MAAM,EAAE,aAAa,EACb,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI;IAQ9C,SAAS,IAAI,OAAO,CAAC,eAAe,CAAC;IA0BrC,MAAM,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAKnE,SAAS,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,eAAe,CAAC;IA2BtD,eAAe,CACb,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,kBAAkB,EAAE,EAC/B,SAAS,CAAC,EAAE,MAAM,GACjB,WAAW;IA8Cd,kBAAkB,IAAI,oBAAoB,GAAG,SAAS;IAItD,kBAAkB,CAAC,KAAK,EAAE,oBAAoB,GAAG,IAAI;YAIvC,OAAO;IAsBrB,OAAO,CAAC,oBAAoB;IAa5B,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,mBAAmB;IAM3B,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,qBAAqB;CAiC9B;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,CAAC"}
@@ -0,0 +1,208 @@
1
+ // Nucleus contract for LLM-native task and planner execution
2
+ import { createHash } from 'crypto';
3
+ export class Nucleus {
4
+ config;
5
+ constructor(config) {
6
+ this.config = config;
7
+ }
8
+ getConfig() {
9
+ return this.config;
10
+ }
11
+ }
12
+ export class DeterministicNucleus extends Nucleus {
13
+ llmCall;
14
+ ledgerAppend;
15
+ internalContext;
16
+ ledger = [];
17
+ constructor(config, llmCall, ledgerAppend) {
18
+ super(config);
19
+ this.llmCall = llmCall;
20
+ this.ledgerAppend = ledgerAppend;
21
+ if (!config.goalIntent || config.goalIntent.trim().length === 0) {
22
+ throw new Error('NucleusConfig.goalIntent is required for nucleus execution.');
23
+ }
24
+ }
25
+ async preflight() {
26
+ if (!this.config.hooks?.preflight) {
27
+ return { status: 'OK' };
28
+ }
29
+ const prompt = this.buildPreflightPrompt();
30
+ const result = await this.callLLM(prompt);
31
+ const needsContext = result.toolCalls.some(tc => tc.name === 'request_context_retrieval');
32
+ if (needsContext) {
33
+ const directives = result.toolCalls
34
+ .filter(tc => tc.name === 'request_context_retrieval')
35
+ .map(tc => tc.input?.directive)
36
+ .filter(Boolean);
37
+ return {
38
+ status: 'NEEDS_CONTEXT',
39
+ retrievalDirectives: directives,
40
+ };
41
+ }
42
+ return { status: 'OK' };
43
+ }
44
+ async invoke(request) {
45
+ const prompt = request.prompt ?? this.buildInvokePrompt(request.input);
46
+ return this.callLLM(prompt, request.tools);
47
+ }
48
+ async postcheck(output) {
49
+ if (!this.config.hooks?.postcheck) {
50
+ return { status: 'COMPLETE' };
51
+ }
52
+ const prompt = this.buildPostcheckPrompt(output);
53
+ const result = await this.callLLM(prompt);
54
+ const compensation = result.toolCalls.find(tc => tc.name === 'request_compensation');
55
+ if (compensation) {
56
+ return {
57
+ status: 'NEEDS_COMPENSATION',
58
+ reason: compensation.input?.reason || 'Unknown',
59
+ };
60
+ }
61
+ const escalation = result.toolCalls.find(tc => tc.name === 'escalate_issue');
62
+ if (escalation) {
63
+ return {
64
+ status: 'ESCALATE',
65
+ reason: escalation.input?.reason || 'Unknown',
66
+ };
67
+ }
68
+ return { status: 'COMPLETE' };
69
+ }
70
+ recordInference(promptDigest, toolCalls, reasoning) {
71
+ const envelopes = toolCalls.map((tc, idx) => ({
72
+ id: tc.id ?? `tool-call-${Date.now()}-${idx}`,
73
+ name: tc.name,
74
+ input: tc.input ?? {},
75
+ output: tc.output,
76
+ error: tc.error,
77
+ metadata: {
78
+ timestamp: Date.now(),
79
+ digest: this.computeDigest(JSON.stringify(tc.input ?? {})),
80
+ },
81
+ }));
82
+ const entry = {
83
+ id: `nucleus-inference-${Date.now()}-${Math.random().toString(36).substring(7)}`,
84
+ ts: Date.now(),
85
+ type: 'NUCLEUS_INFERENCE',
86
+ details: {
87
+ nucleus: {
88
+ goalId: this.config.goalId,
89
+ planId: this.config.planId,
90
+ taskId: this.config.taskId,
91
+ contextRef: this.config.contextRef,
92
+ },
93
+ llmCall: {
94
+ provider: this.config.llmCall.provider,
95
+ model: this.config.llmCall.model,
96
+ seed: this.config.llmCall.seed,
97
+ },
98
+ promptDigest,
99
+ reasoning,
100
+ toolCalls: envelopes.map(env => ({
101
+ id: env.id,
102
+ name: env.name,
103
+ inputDigest: env.metadata?.digest,
104
+ outputDigest: env.output ? this.computeDigest(JSON.stringify(env.output)) : undefined,
105
+ })),
106
+ },
107
+ };
108
+ entry.digest = this.computeDigest(JSON.stringify(entry.details));
109
+ this.ledger.push(entry);
110
+ this.ledgerAppend(entry);
111
+ return entry;
112
+ }
113
+ getInternalContext() {
114
+ return this.internalContext;
115
+ }
116
+ setInternalContext(scope) {
117
+ this.internalContext = scope;
118
+ }
119
+ async callLLM(prompt, tools = []) {
120
+ const result = await this.llmCall(prompt, tools, this.config.llmCall);
121
+ const normalizedCalls = (result.toolCalls ?? []).map((tc, idx) => ({
122
+ id: tc.id ?? `tool-call-${Date.now()}-${idx}`,
123
+ name: tc.name,
124
+ input: tc.input ?? {},
125
+ output: tc.output,
126
+ error: tc.error,
127
+ }));
128
+ this.recordInference(this.computePromptDigest(prompt), normalizedCalls, result.reasoning);
129
+ return {
130
+ reasoning: result.reasoning,
131
+ toolCalls: normalizedCalls,
132
+ raw: result.raw,
133
+ };
134
+ }
135
+ buildPreflightPrompt() {
136
+ return `Assess whether the current context is sufficient for the task.
137
+ Goal: ${this.config.goalId}
138
+ Goal Intent: ${this.config.goalIntent}
139
+ Task: ${this.config.taskId || 'planner'}
140
+ Context Ref: ${this.config.contextRef}
141
+
142
+ Context Snapshot:
143
+ ${this.renderContextSnapshot()}
144
+
145
+ If additional context is needed, call request_context_retrieval with a directive.`;
146
+ }
147
+ buildInvokePrompt(input) {
148
+ return `Execute the task with the following input:
149
+ ${JSON.stringify(input, null, 2)}
150
+
151
+ Goal: ${this.config.goalId}
152
+ Goal Intent: ${this.config.goalIntent}
153
+ Task: ${this.config.taskId || 'planner'}
154
+
155
+ Relevant Context:
156
+ ${this.renderContextSnapshot()}`;
157
+ }
158
+ buildPostcheckPrompt(output) {
159
+ return `Validate the output and determine if any follow-up is needed:
160
+ ${JSON.stringify(output, null, 2)}
161
+
162
+ Goal: ${this.config.goalId}
163
+ Goal Intent: ${this.config.goalIntent}
164
+ Task: ${this.config.taskId || 'planner'}
165
+
166
+ Context Reference: ${this.config.contextRef}
167
+
168
+ If compensation is needed, call request_compensation.
169
+ If escalation is needed, call escalate_issue.`;
170
+ }
171
+ computePromptDigest(prompt) {
172
+ const hash = createHash('sha256');
173
+ hash.update(prompt);
174
+ return hash.digest('hex').substring(0, 16);
175
+ }
176
+ computeDigest(content) {
177
+ const hash = createHash('sha256');
178
+ hash.update(content);
179
+ return hash.digest('hex').substring(0, 16);
180
+ }
181
+ renderContextSnapshot() {
182
+ const context = this.config.context;
183
+ if (!context) {
184
+ return '(No context payload provided; consider supplying context.facts to the nucleus.)';
185
+ }
186
+ const pieces = [];
187
+ if (context.facts && Object.keys(context.facts).length > 0) {
188
+ pieces.push(`Facts:\n${JSON.stringify(context.facts, null, 2)}`);
189
+ }
190
+ else {
191
+ pieces.push('Facts: {}');
192
+ }
193
+ if (context.assumptions && context.assumptions.length > 0) {
194
+ pieces.push(`Assumptions:\n- ${context.assumptions.join('\n- ')}`);
195
+ }
196
+ if (context.constraints_inherited && Object.keys(context.constraints_inherited).length > 0) {
197
+ pieces.push(`Inherited Constraints:\n${JSON.stringify(context.constraints_inherited, null, 2)}`);
198
+ }
199
+ if (context.augmentations && context.augmentations.length > 0) {
200
+ const augmentations = context.augmentations
201
+ .map(aug => `- ${aug.type}: ${aug.artifact}`)
202
+ .join('\n');
203
+ pieces.push(`Augmentations:\n${augmentations}`);
204
+ }
205
+ return pieces.join('\n\n');
206
+ }
207
+ }
208
+ //# sourceMappingURL=nucleus.js.map