agents-relay 1.0.93 → 1.0.94
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/dist/decision.js +33 -21
- package/package.json +1 -1
package/dist/decision.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
2
5
|
function validateContract(contract) {
|
|
3
6
|
if (contract.type !== 'choice')
|
|
4
7
|
throw new Error(`Unsupported decision contract ${contract.type}`);
|
|
@@ -69,8 +72,9 @@ export function parseChoiceDecision(raw, contract) {
|
|
|
69
72
|
return result;
|
|
70
73
|
}
|
|
71
74
|
/**
|
|
72
|
-
* Stateless Laya invocation. The
|
|
73
|
-
*
|
|
75
|
+
* Stateless Laya invocation. The adapter materializes the caller's JSON state
|
|
76
|
+
* and question contract as temporary files for `uvx laya-mlx predict`. It is
|
|
77
|
+
* intentionally a subprocess per
|
|
74
78
|
* decision: no daemon, resident model, socket, or PM2 lifecycle is introduced.
|
|
75
79
|
*/
|
|
76
80
|
export class LayaCliDecisionEngine {
|
|
@@ -80,36 +84,45 @@ export class LayaCliDecisionEngine {
|
|
|
80
84
|
timeoutMs;
|
|
81
85
|
env;
|
|
82
86
|
constructor(options = {}) {
|
|
83
|
-
this.command = options.command ?? process.env.AGENTS_RELAY_LAYA_COMMAND ?? '
|
|
87
|
+
this.command = options.command ?? process.env.AGENTS_RELAY_LAYA_COMMAND ?? 'uvx';
|
|
84
88
|
this.args = options.args ?? layaArgsFromEnv();
|
|
85
89
|
this.timeoutMs = options.timeoutMs ?? Number(process.env.AGENTS_RELAY_LAYA_TIMEOUT_MS ?? 30_000);
|
|
86
90
|
this.env = options.env;
|
|
87
91
|
}
|
|
88
92
|
async decide(request, contract) {
|
|
89
93
|
validateDecisionRequest(request, contract);
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
instructions: contract.question,
|
|
96
|
-
criteria: [...contract.choices],
|
|
97
|
-
},
|
|
94
|
+
const questions = {
|
|
95
|
+
decision: {
|
|
96
|
+
type: contract.type,
|
|
97
|
+
instructions: contract.question,
|
|
98
|
+
criteria: [...contract.choices],
|
|
98
99
|
},
|
|
99
100
|
};
|
|
100
|
-
const
|
|
101
|
-
let parsed;
|
|
101
|
+
const dir = await mkdtemp(join(tmpdir(), 'agents-relay-laya-'));
|
|
102
102
|
try {
|
|
103
|
-
|
|
103
|
+
const statePath = join(dir, 'state.json');
|
|
104
|
+
const questionsPath = join(dir, 'questions.json');
|
|
105
|
+
await Promise.all([
|
|
106
|
+
writeFile(statePath, JSON.stringify(request.facts), 'utf8'),
|
|
107
|
+
writeFile(questionsPath, JSON.stringify(questions), 'utf8'),
|
|
108
|
+
]);
|
|
109
|
+
const stdout = await this.run(['--state-file', statePath, '--questions', questionsPath]);
|
|
110
|
+
let parsed;
|
|
111
|
+
try {
|
|
112
|
+
parsed = JSON.parse(stdout);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
throw new Error('Laya CLI returned invalid JSON');
|
|
116
|
+
}
|
|
117
|
+
return parseChoiceDecision(parsed, contract);
|
|
104
118
|
}
|
|
105
|
-
|
|
106
|
-
|
|
119
|
+
finally {
|
|
120
|
+
await rm(dir, { recursive: true, force: true });
|
|
107
121
|
}
|
|
108
|
-
return parseChoiceDecision(parsed, contract);
|
|
109
122
|
}
|
|
110
|
-
async run(
|
|
123
|
+
async run(extraArgs) {
|
|
111
124
|
return await new Promise((resolve, reject) => {
|
|
112
|
-
const child = spawn(this.command, [...this.args], { stdio: ['
|
|
125
|
+
const child = spawn(this.command, [...this.args, ...extraArgs], { stdio: ['ignore', 'pipe', 'pipe'], env: this.env ?? process.env });
|
|
113
126
|
const stdout = [];
|
|
114
127
|
const stderr = [];
|
|
115
128
|
let settled = false;
|
|
@@ -138,14 +151,13 @@ export class LayaCliDecisionEngine {
|
|
|
138
151
|
}
|
|
139
152
|
finish(undefined, Buffer.concat(stdout).toString('utf8').trim());
|
|
140
153
|
});
|
|
141
|
-
child.stdin.end(`${stdin}\n`);
|
|
142
154
|
});
|
|
143
155
|
}
|
|
144
156
|
}
|
|
145
157
|
function layaArgsFromEnv() {
|
|
146
158
|
const configured = process.env.AGENTS_RELAY_LAYA_ARGS;
|
|
147
159
|
if (!configured)
|
|
148
|
-
return ['-
|
|
160
|
+
return ['laya-mlx', 'predict'];
|
|
149
161
|
try {
|
|
150
162
|
const parsed = JSON.parse(configured);
|
|
151
163
|
if (!Array.isArray(parsed) || parsed.some(item => typeof item !== 'string'))
|