@almadar/agent 2.0.5 → 3.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.
@@ -47,9 +47,9 @@ export declare class LocalMemoryBackend {
47
47
  getAllErrorResolutions(): Promise<ErrorResolution[]>;
48
48
  getUserPreferences(_userId: string): Promise<UserPreference | null>;
49
49
  updateUserPreferences(_userId: string, _preferences: Partial<Omit<UserPreference, 'id' | 'userId' | 'learnedAt'>>): Promise<void>;
50
- recordGeneration(_userId: string, _session: Omit<GenerationSession, 'id' | 'userId' | 'createdAt'>): Promise<string>;
51
- getGenerationSession(_sessionId: string): Promise<GenerationSession | null>;
52
- getUserGenerationHistory(_userId: string, _limit?: number): Promise<GenerationSession[]>;
50
+ recordGeneration(_userId: string, session: Omit<GenerationSession, 'id' | 'userId' | 'createdAt'>): Promise<string>;
51
+ getGenerationSession(sessionId: string): Promise<GenerationSession | null>;
52
+ getUserGenerationHistory(_userId: string, limit?: number): Promise<GenerationSession[]>;
53
53
  getProjectContext(_appId: string): Promise<ProjectContext | null>;
54
54
  updateProjectContext(_appId: string, _update: Partial<Omit<ProjectContext, 'id' | 'appId'>>): Promise<void>;
55
55
  updatePatternAffinity(_userId: string, _patternId: string, _outcome: 'success' | 'failure' | 'partial', _context?: {
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Neural Pipeline Module
3
+ *
4
+ * Schema generation via the Masar server. The neural pipeline (GFlowNet +
5
+ * LLM fix loop) now runs server-side in the Masar container. This module
6
+ * provides the HTTP client and shared types.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export type { GoalSpec, NeuralGenerateResult, NeuralPipelineResult, NeuralPipelineOptions, CommandExecutor, PythonExecutor, InferOptions, InferResult, ValidationError, ValidationResult, CompileResult, } from './types.js';
11
+ export { MasarClient } from './masar-client.js';
12
+ export type { MasarClientOptions } from './masar-client.js';
@@ -0,0 +1,86 @@
1
+ // src/neural/masar-client.ts
2
+ var MasarClient = class {
3
+ constructor(options) {
4
+ this.baseUrl = (options?.baseUrl ?? process.env["MASAR_URL"] ?? "https://masar-345008351456.europe-west4.run.app").replace(/\/$/, "");
5
+ this.timeoutMs = options?.timeoutMs ?? 12e4;
6
+ this.onProgress = options?.onProgress ?? null;
7
+ }
8
+ /**
9
+ * Generate an .orb schema from a natural language prompt.
10
+ *
11
+ * Sends the prompt to the Masar server which runs the full neural pipeline
12
+ * (goal parsing, GFlowNet generation, validation, LLM fix loop) and returns
13
+ * the result.
14
+ */
15
+ async generate(prompt) {
16
+ this.onProgress?.("request", `Sending prompt to Masar at ${this.baseUrl}`);
17
+ const url = `${this.baseUrl}/generate`;
18
+ const controller = new AbortController();
19
+ const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
20
+ try {
21
+ const response = await fetch(url, {
22
+ method: "POST",
23
+ headers: { "Content-Type": "application/json" },
24
+ body: JSON.stringify({ prompt }),
25
+ signal: controller.signal
26
+ });
27
+ if (!response.ok) {
28
+ const body = await response.text().catch(() => "");
29
+ return makeErrorResult(
30
+ `Masar server returned ${response.status} ${response.statusText}${body ? `: ${body}` : ""}`
31
+ );
32
+ }
33
+ const data = await response.json();
34
+ this.onProgress?.("response", data.success ? "Generation succeeded" : `Generation failed: ${data.error ?? "unknown"}`);
35
+ return toNeuralPipelineResult(data);
36
+ } catch (err) {
37
+ if (err instanceof DOMException && err.name === "AbortError") {
38
+ return makeErrorResult(`Masar request timed out after ${this.timeoutMs}ms`);
39
+ }
40
+ const message = err instanceof Error ? err.message : String(err);
41
+ return makeErrorResult(`Masar request failed: ${message}`);
42
+ } finally {
43
+ clearTimeout(timeout);
44
+ }
45
+ }
46
+ };
47
+ function toNeuralPipelineResult(data) {
48
+ return {
49
+ success: data.success,
50
+ validationPass: data.validation_pass,
51
+ compilationPass: false,
52
+ goalMatch: data.goal_match ?? 0,
53
+ totalSteps: data.total_steps ?? 0,
54
+ fixRounds: data.fix_rounds ?? 0,
55
+ llmCalls: data.llm_calls ?? 0,
56
+ durationMs: data.duration_ms ?? 0,
57
+ schema: data.schema ?? null,
58
+ schemaPath: null,
59
+ actions: data.actions ?? [],
60
+ validationErrors: data.validation_errors ?? [],
61
+ compilationErrors: [],
62
+ error: data.error ?? null
63
+ };
64
+ }
65
+ function makeErrorResult(error) {
66
+ return {
67
+ success: false,
68
+ validationPass: false,
69
+ compilationPass: false,
70
+ goalMatch: 0,
71
+ totalSteps: 0,
72
+ fixRounds: 0,
73
+ llmCalls: 0,
74
+ durationMs: 0,
75
+ schema: null,
76
+ schemaPath: null,
77
+ actions: [],
78
+ validationErrors: [],
79
+ compilationErrors: [],
80
+ error
81
+ };
82
+ }
83
+
84
+ export { MasarClient };
85
+ //# sourceMappingURL=index.js.map
86
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/neural/masar-client.ts"],"names":[],"mappings":";AA8CO,IAAM,cAAN,MAAkB;AAAA,EAKvB,YAAY,OAAA,EAA8B;AACxC,IAAA,IAAA,CAAK,OAAA,GAAA,CAAW,OAAA,EAAS,OAAA,IAAW,OAAA,CAAQ,GAAA,CAAI,WAAW,CAAA,IAAK,iDAAA,EAAmD,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AACpI,IAAA,IAAA,CAAK,SAAA,GAAY,SAAS,SAAA,IAAa,IAAA;AACvC,IAAA,IAAA,CAAK,UAAA,GAAa,SAAS,UAAA,IAAc,IAAA;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,SAAS,MAAA,EAA+C;AAC5D,IAAA,IAAA,CAAK,UAAA,GAAa,SAAA,EAAW,CAAA,2BAAA,EAA8B,IAAA,CAAK,OAAO,CAAA,CAAE,CAAA;AAEzE,IAAA,MAAM,GAAA,GAAM,CAAA,EAAG,IAAA,CAAK,OAAO,CAAA,SAAA,CAAA;AAC3B,IAAA,MAAM,UAAA,GAAa,IAAI,eAAA,EAAgB;AACvC,IAAA,MAAM,UAAU,UA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AkB,EAAC;AAAA,IACnB,mBAAmB,EAAC;AAAA,IACpB;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * Masar HTTP Client\n *\n * Calls the Masar server's /generate endpoint via HTTP instead of\n * orchestrating the neural pipeline (GFlowNet + LLM fix loop) directly.\n *\n * The Masar server runs the full pipeline server-side and returns\n * a NeuralPipelineResult-compatible response.\n */\n\nimport type { NeuralPipelineResult } from './types.js';\n\n/** Response shape returned by the Masar /generate endpoint (snake_case from Python). */\ninterface MasarGenerateResponse {\n success: boolean;\n schema: string | null;\n validation_pass: boolean;\n goal_match: number;\n total_steps: number;\n fix_rounds: number;\n llm_calls: number;\n duration_ms: number;\n actions: string[];\n validation_errors: string[];\n error: string | null;\n}\n\n/** Options for constructing a MasarClient. */\nexport interface MasarClientOptions {\n /** Base URL of the Masar server. Defaults to MASAR_URL env var or http://localhost:8080 */\n baseUrl?: string;\n /** Request timeout in milliseconds. Defaults to 120000 (2 minutes). */\n timeoutMs?: number;\n /** Optional callback for progress updates. */\n onProgress?: (phase: string, detail: string) => void;\n}\n\n/**\n * HTTP client for the Masar schema generation server.\n *\n * Usage:\n * ```ts\n * const client = new MasarClient();\n * const result = await client.generate('Build a todo app with projects and tasks');\n * ```\n */\nexport class MasarClient {\n private readonly baseUrl: string;\n private readonly timeoutMs: number;\n private readonly onProgress: ((phase: string, detail: string) => void) | null;\n\n constructor(options?: MasarClientOptions) {\n this.baseUrl = (options?.baseUrl ?? process.env['MASAR_URL'] ?? 'https://masar-345008351456.europe-west4.run.app').replace(/\\/$/, '');\n this.timeoutMs = options?.timeoutMs ?? 120_000;\n this.onProgress = options?.onProgress ?? null;\n }\n\n /**\n * Generate an .orb schema from a natural language prompt.\n *\n * Sends the prompt to the Masar server which runs the full neural pipeline\n * (goal parsing, GFlowNet generation, validation, LLM fix loop) and returns\n * the result.\n */\n async generate(prompt: string): Promise<NeuralPipelineResult> {\n this.onProgress?.('request', `Sending prompt to Masar at ${this.baseUrl}`);\n\n const url = `${this.baseUrl}/generate`;\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), this.timeoutMs);\n\n try {\n const response = await fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ prompt }),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const body = await response.text().catch(() => '');\n return makeErrorResult(\n `Masar server returned ${response.status} ${response.statusText}${body ? `: ${body}` : ''}`,\n );\n }\n\n const data: MasarGenerateResponse = await response.json() as MasarGenerateResponse;\n this.onProgress?.('response', data.success ? 'Generation succeeded' : `Generation failed: ${data.error ?? 'unknown'}`);\n\n return toNeuralPipelineResult(data);\n } catch (err: unknown) {\n if (err instanceof DOMException && err.name === 'AbortError') {\n return makeErrorResult(`Masar request timed out after ${this.timeoutMs}ms`);\n }\n const message = err instanceof Error ? err.message : String(err);\n return makeErrorResult(`Masar request failed: ${message}`);\n } finally {\n clearTimeout(timeout);\n }\n }\n}\n\n/**\n * Map the Masar server response to a NeuralPipelineResult.\n * Provides defaults for any missing fields so the result always satisfies the full interface.\n */\nfunction toNeuralPipelineResult(data: MasarGenerateResponse): NeuralPipelineResult {\n return {\n success: data.success,\n validationPass: data.validation_pass,\n compilationPass: false,\n goalMatch: data.goal_match ?? 0,\n totalSteps: data.total_steps ?? 0,\n fixRounds: data.fix_rounds ?? 0,\n llmCalls: data.llm_calls ?? 0,\n durationMs: data.duration_ms ?? 0,\n schema: data.schema ?? null,\n schemaPath: null,\n actions: data.actions ?? [],\n validationErrors: data.validation_errors ?? [],\n compilationErrors: [],\n error: data.error ?? null,\n };\n}\n\nfunction makeErrorResult(error: string): NeuralPipelineResult {\n return {\n success: false,\n validationPass: false,\n compilationPass: false,\n goalMatch: 0,\n totalSteps: 0,\n fixRounds: 0,\n llmCalls: 0,\n durationMs: 0,\n schema: null,\n schemaPath: null,\n actions: [],\n validationErrors: [],\n compilationErrors: [],\n error,\n };\n}\n"]}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Masar HTTP Client
3
+ *
4
+ * Calls the Masar server's /generate endpoint via HTTP instead of
5
+ * orchestrating the neural pipeline (GFlowNet + LLM fix loop) directly.
6
+ *
7
+ * The Masar server runs the full pipeline server-side and returns
8
+ * a NeuralPipelineResult-compatible response.
9
+ */
10
+ import type { NeuralPipelineResult } from './types.js';
11
+ /** Options for constructing a MasarClient. */
12
+ export interface MasarClientOptions {
13
+ /** Base URL of the Masar server. Defaults to MASAR_URL env var or http://localhost:8080 */
14
+ baseUrl?: string;
15
+ /** Request timeout in milliseconds. Defaults to 120000 (2 minutes). */
16
+ timeoutMs?: number;
17
+ /** Optional callback for progress updates. */
18
+ onProgress?: (phase: string, detail: string) => void;
19
+ }
20
+ /**
21
+ * HTTP client for the Masar schema generation server.
22
+ *
23
+ * Usage:
24
+ * ```ts
25
+ * const client = new MasarClient();
26
+ * const result = await client.generate('Build a todo app with projects and tasks');
27
+ * ```
28
+ */
29
+ export declare class MasarClient {
30
+ private readonly baseUrl;
31
+ private readonly timeoutMs;
32
+ private readonly onProgress;
33
+ constructor(options?: MasarClientOptions);
34
+ /**
35
+ * Generate an .orb schema from a natural language prompt.
36
+ *
37
+ * Sends the prompt to the Masar server which runs the full neural pipeline
38
+ * (goal parsing, GFlowNet generation, validation, LLM fix loop) and returns
39
+ * the result.
40
+ */
41
+ generate(prompt: string): Promise<NeuralPipelineResult>;
42
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Neural Pipeline Types
3
+ *
4
+ * Shared types for the GFlowNet-based schema generation pipeline.
5
+ */
6
+ /**
7
+ * Structural goal vector extracted from a natural language prompt.
8
+ * Used by the GFlowNet to generate schema skeletons.
9
+ */
10
+ export interface GoalSpec {
11
+ entities: number;
12
+ fields: number;
13
+ traits: number;
14
+ states: number;
15
+ transitions: number;
16
+ pages: number;
17
+ events: number;
18
+ effects: number;
19
+ entityNames: string[];
20
+ fieldNames: string[];
21
+ projectName: string;
22
+ }
23
+ export interface NeuralGenerateResult {
24
+ success: boolean;
25
+ schema?: Record<string, unknown>;
26
+ actions?: string[];
27
+ steps?: number;
28
+ goalMatch?: number;
29
+ error?: string;
30
+ }
31
+ export interface NeuralPipelineResult {
32
+ success: boolean;
33
+ validationPass: boolean;
34
+ compilationPass: boolean;
35
+ goalMatch: number;
36
+ totalSteps: number;
37
+ fixRounds: number;
38
+ llmCalls: number;
39
+ durationMs: number;
40
+ /** Generated .orb schema JSON string (returned by Masar server). */
41
+ schema: string | null;
42
+ /** @deprecated Use `schema` instead. Legacy field for file-based pipeline. */
43
+ schemaPath: string | null;
44
+ actions: string[];
45
+ validationErrors: string[];
46
+ compilationErrors: string[];
47
+ error: string | null;
48
+ }
49
+ export interface NeuralPipelineOptions {
50
+ provider: string;
51
+ model: string;
52
+ /** Working directory for writing schemas */
53
+ workDir: string;
54
+ maxFixRounds?: number;
55
+ samples?: number;
56
+ temperature?: number;
57
+ onProgress?: (phase: string, detail: string) => void;
58
+ }
59
+ /**
60
+ * Abstraction for running shell commands.
61
+ * Server uses temp files, CLI uses real paths, tests use mocks.
62
+ */
63
+ export interface CommandExecutor {
64
+ /**
65
+ * Run `orbital validate` on a schema string or file path.
66
+ */
67
+ validateSchema(schema: string): Promise<ValidationResult>;
68
+ /**
69
+ * Run `orbital compile` on a schema file.
70
+ */
71
+ compileSchema?(schemaPath: string): Promise<CompileResult>;
72
+ }
73
+ /**
74
+ * Abstraction for running GFlowNet inference (Python subprocess).
75
+ */
76
+ export interface PythonExecutor {
77
+ /**
78
+ * Run GFlowNet inference on a goal spec.
79
+ */
80
+ infer(goal: GoalSpec, options: InferOptions): Promise<InferResult>;
81
+ }
82
+ export interface InferOptions {
83
+ samples?: number;
84
+ temperature?: number;
85
+ bestOf?: boolean;
86
+ }
87
+ export interface InferResult {
88
+ samples: Array<{
89
+ schema: Record<string, unknown>;
90
+ actions: string[];
91
+ steps: number;
92
+ goal_match: number;
93
+ sample_idx: number;
94
+ }>;
95
+ goal: GoalSpec;
96
+ temperature: number;
97
+ }
98
+ export interface ValidationError {
99
+ code: string;
100
+ message: string;
101
+ path?: string;
102
+ }
103
+ export interface ValidationResult {
104
+ success: boolean;
105
+ errors: ValidationError[];
106
+ warnings: ValidationError[];
107
+ }
108
+ export interface CompileResult {
109
+ success: boolean;
110
+ errors: string[];
111
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@almadar/agent",
3
- "version": "2.0.5",
3
+ "version": "3.1.0",
4
4
  "description": "AI agent infrastructure for Almadar orbital schema generation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -34,6 +34,14 @@
34
34
  "./workspace": {
35
35
  "import": "./dist/workspace/index.js",
36
36
  "types": "./dist/workspace/index.d.ts"
37
+ },
38
+ "./neural": {
39
+ "import": "./dist/neural/index.js",
40
+ "types": "./dist/neural/index.d.ts"
41
+ },
42
+ "./builder": {
43
+ "import": "./dist/builder/index.js",
44
+ "types": "./dist/builder/index.d.ts"
37
45
  }
38
46
  },
39
47
  "files": [
@@ -45,7 +53,7 @@
45
53
  "dependencies": {
46
54
  "@almadar/core": ">=2.1.0",
47
55
  "@almadar/integrations": ">=2.0.1",
48
- "@almadar/llm": ">=2.0.1",
56
+ "@almadar/llm": ">=2.2.0",
49
57
  "@almadar/patterns": ">=2.0.0",
50
58
  "@almadar/skills": ">=2.0.1",
51
59
  "langsmith": "^0.1.0",
@@ -84,6 +92,7 @@
84
92
  "@types/uuid": "^9.0.0",
85
93
  "@typescript-eslint/parser": "8.56.0",
86
94
  "deepagents": "^1.7.3",
95
+ "dotenv": "17.3.1",
87
96
  "eslint": "10.0.0",
88
97
  "tsup": "^8.0.0",
89
98
  "typescript": "^5.4.0",