Merge branch 'main' of https://git.nianxx.cn/wangxuming/makelore
# Conflicts: # electron/coding-runtime/pi/extensions/makelore-runtime.ts
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import { atomicWriteText } from '../../../coding-projects/atomic-json';
|
||||
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 3;
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 4;
|
||||
export const MAKELORE_PI_EXTENSION_FILENAME = `makelore-runtime-v${MAKELORE_PI_EXTENSION_VERSION}.mjs`;
|
||||
|
||||
const BUNDLE_SOURCE = String.raw`
|
||||
@@ -13,167 +13,217 @@ const MUTATION_TOOLS = new Set([
|
||||
'edit',
|
||||
'write',
|
||||
]);
|
||||
const WORKER_ROLE = process.env.MAKELORE_PI_WORKER_ROLE || 'parent';
|
||||
const leases = new Map();
|
||||
const touchedPaths = new Map();
|
||||
let dynamicLeaseTools = new Set();
|
||||
const RUNTIME_FLAGS = {
|
||||
bridgeUrl: 'makelore-bridge-url',
|
||||
workerToken: 'makelore-worker-token',
|
||||
contextFile: 'makelore-context-file',
|
||||
workerRole: 'makelore-worker-role',
|
||||
projectPath: 'makelore-project-path',
|
||||
};
|
||||
|
||||
async function readWorkerContext() {
|
||||
const value = JSON.parse(await readFile(process.env.MAKELORE_PI_CONTEXT_FILE, 'utf8'));
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new Error('Makelore worker context is invalid');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
async function runtimeContext() {
|
||||
const value = await readWorkerContext();
|
||||
if (!value.runId) throw new Error('Makelore run context is unavailable');
|
||||
return value;
|
||||
}
|
||||
|
||||
async function bridge(action, body, signal) {
|
||||
const context = await runtimeContext();
|
||||
const response = await fetch(process.env.MAKELORE_PI_BRIDGE_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + process.env.MAKELORE_PI_WORKER_TOKEN,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ ...context, action, ...body }),
|
||||
signal,
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(result.error || 'Makelore runtime bridge rejected the request');
|
||||
return result;
|
||||
}
|
||||
|
||||
async function bridgeStream(action, body, signal, onUpdate) {
|
||||
const context = await runtimeContext();
|
||||
const response = await fetch(process.env.MAKELORE_PI_BRIDGE_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + process.env.MAKELORE_PI_WORKER_TOKEN,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ ...context, action, ...body }),
|
||||
signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
const result = await response.json().catch(() => ({}));
|
||||
throw new Error(result.error || 'Makelore runtime bridge rejected the request');
|
||||
}
|
||||
if (!response.body) throw new Error('Makelore runtime bridge returned no stream');
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffered = '';
|
||||
let details;
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
buffered += decoder.decode(value || new Uint8Array(), { stream: !done });
|
||||
let newline = buffered.indexOf('\n');
|
||||
while (newline >= 0) {
|
||||
const line = buffered.slice(0, newline);
|
||||
buffered = buffered.slice(newline + 1);
|
||||
if (line) {
|
||||
const item = JSON.parse(line);
|
||||
if (item.details?.schema === 'subagent.v1') {
|
||||
details = item.details;
|
||||
onUpdate?.({ content: [], details });
|
||||
}
|
||||
}
|
||||
newline = buffered.indexOf('\n');
|
||||
export function createMakeloreRuntime(runtimeDefaults = {}) {
|
||||
return async function makeloreRuntime(pi) {
|
||||
const fallbackFlagValues = new Map();
|
||||
const registerRuntimeFlag = (name, defaultValue) => {
|
||||
fallbackFlagValues.set(name, defaultValue);
|
||||
if (typeof pi.registerFlag === 'function') {
|
||||
pi.registerFlag(name, { type: 'string', default: defaultValue });
|
||||
}
|
||||
if (done) break;
|
||||
};
|
||||
registerRuntimeFlag(
|
||||
RUNTIME_FLAGS.bridgeUrl,
|
||||
runtimeDefaults.bridgeUrl || process.env.MAKELORE_PI_BRIDGE_URL || '',
|
||||
);
|
||||
registerRuntimeFlag(
|
||||
RUNTIME_FLAGS.workerToken,
|
||||
runtimeDefaults.workerToken || process.env.MAKELORE_PI_WORKER_TOKEN || '',
|
||||
);
|
||||
registerRuntimeFlag(
|
||||
RUNTIME_FLAGS.contextFile,
|
||||
runtimeDefaults.contextFile || process.env.MAKELORE_PI_CONTEXT_FILE || '',
|
||||
);
|
||||
registerRuntimeFlag(
|
||||
RUNTIME_FLAGS.workerRole,
|
||||
runtimeDefaults.workerRole || process.env.MAKELORE_PI_WORKER_ROLE || 'parent',
|
||||
);
|
||||
registerRuntimeFlag(
|
||||
RUNTIME_FLAGS.projectPath,
|
||||
runtimeDefaults.projectPath || process.env.MAKELORE_PI_PROJECT_PATH || process.cwd(),
|
||||
);
|
||||
|
||||
const runtimeValue = (name) => {
|
||||
const value = typeof pi.getFlag === 'function'
|
||||
? pi.getFlag(name)
|
||||
: fallbackFlagValues.get(name);
|
||||
return typeof value === 'string' ? value : '';
|
||||
};
|
||||
const workerRole = runtimeValue(RUNTIME_FLAGS.workerRole) || 'parent';
|
||||
const leases = new Map();
|
||||
const touchedPaths = new Map();
|
||||
let dynamicLeaseTools = new Set();
|
||||
|
||||
async function readWorkerContext() {
|
||||
const contextFile = runtimeValue(RUNTIME_FLAGS.contextFile);
|
||||
if (!contextFile) throw new Error('Makelore runtime context file is unavailable');
|
||||
const value = JSON.parse(await readFile(contextFile, 'utf8'));
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new Error('Makelore worker context is invalid');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
if (!details) throw new Error('Makelore subagent stream returned no details');
|
||||
return details;
|
||||
}
|
||||
|
||||
async function releaseLease(toolCallId) {
|
||||
const leaseId = leases.get(toolCallId);
|
||||
if (!leaseId) return;
|
||||
leases.delete(toolCallId);
|
||||
await bridge('lease.release', { leaseId, resourceId: toolCallId }).catch(() => undefined);
|
||||
}
|
||||
async function runtimeContext() {
|
||||
const value = await readWorkerContext();
|
||||
if (!value.runId) throw new Error('Makelore run context is unavailable');
|
||||
return value;
|
||||
}
|
||||
|
||||
async function releaseAll() {
|
||||
await Promise.all([...leases.keys()].map(releaseLease));
|
||||
touchedPaths.clear();
|
||||
}
|
||||
async function bridge(action, body, signal) {
|
||||
const context = await runtimeContext();
|
||||
const bridgeUrl = runtimeValue(RUNTIME_FLAGS.bridgeUrl);
|
||||
const workerToken = runtimeValue(RUNTIME_FLAGS.workerToken);
|
||||
if (!bridgeUrl || !workerToken) throw new Error('Makelore runtime bridge is unavailable');
|
||||
const response = await fetch(bridgeUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + workerToken,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ ...context, action, ...body }),
|
||||
signal,
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(result.error || 'Makelore runtime bridge rejected the request');
|
||||
return result;
|
||||
}
|
||||
|
||||
function projectRelativePath(value) {
|
||||
if (typeof value !== 'string' || !value.trim()) return undefined;
|
||||
const projectPath = path.resolve(process.cwd());
|
||||
const targetPath = path.resolve(projectPath, value);
|
||||
const relative = path.relative(projectPath, targetPath);
|
||||
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) return undefined;
|
||||
return relative.split(path.sep).join('/');
|
||||
}
|
||||
async function bridgeStream(action, body, signal, onUpdate) {
|
||||
const context = await runtimeContext();
|
||||
const bridgeUrl = runtimeValue(RUNTIME_FLAGS.bridgeUrl);
|
||||
const workerToken = runtimeValue(RUNTIME_FLAGS.workerToken);
|
||||
if (!bridgeUrl || !workerToken) throw new Error('Makelore runtime bridge is unavailable');
|
||||
const response = await fetch(bridgeUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + workerToken,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ ...context, action, ...body }),
|
||||
signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
const result = await response.json().catch(() => ({}));
|
||||
throw new Error(result.error || 'Makelore runtime bridge rejected the request');
|
||||
}
|
||||
if (!response.body) throw new Error('Makelore subagent stream returned no body');
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffered = '';
|
||||
let details;
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
buffered += decoder.decode(value || new Uint8Array(), { stream: !done });
|
||||
let newline = buffered.indexOf('\n');
|
||||
while (newline >= 0) {
|
||||
const line = buffered.slice(0, newline);
|
||||
buffered = buffered.slice(newline + 1);
|
||||
if (line) {
|
||||
const item = JSON.parse(line);
|
||||
if (item.details?.schema === 'subagent.v1') {
|
||||
details = item.details;
|
||||
onUpdate?.({ content: [], details });
|
||||
}
|
||||
}
|
||||
newline = buffered.indexOf('\n');
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
if (!details) throw new Error('Makelore subagent stream returned no details');
|
||||
return details;
|
||||
}
|
||||
|
||||
async function invokeProduct(toolCallId, toolName, input, signal) {
|
||||
const response = await bridge('product.invoke', {
|
||||
resourceId: toolCallId,
|
||||
toolName,
|
||||
input,
|
||||
}, signal);
|
||||
return response.result;
|
||||
}
|
||||
async function releaseLease(toolCallId) {
|
||||
const leaseId = leases.get(toolCallId);
|
||||
if (!leaseId) return;
|
||||
leases.delete(toolCallId);
|
||||
await bridge('lease.release', { leaseId, resourceId: toolCallId }).catch(() => undefined);
|
||||
}
|
||||
|
||||
function registerProductTool(pi, name, label, description, parameters) {
|
||||
pi.registerTool({
|
||||
name,
|
||||
label,
|
||||
description,
|
||||
parameters,
|
||||
async execute(toolCallId, params, signal) {
|
||||
return await invokeProduct(toolCallId, name, params, signal);
|
||||
},
|
||||
});
|
||||
}
|
||||
async function releaseAll() {
|
||||
await Promise.all([...leases.keys()].map(releaseLease));
|
||||
touchedPaths.clear();
|
||||
}
|
||||
|
||||
function isRecord(value) {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
function projectRelativePath(value) {
|
||||
if (typeof value !== 'string' || !value.trim()) return undefined;
|
||||
const projectPath = path.resolve(runtimeValue(RUNTIME_FLAGS.projectPath) || process.cwd());
|
||||
const targetPath = path.resolve(projectPath, value);
|
||||
const relative = path.relative(projectPath, targetPath);
|
||||
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) return undefined;
|
||||
return relative.split(path.sep).join('/');
|
||||
}
|
||||
|
||||
function isToolDeclaration(value) {
|
||||
return isRecord(value)
|
||||
&& typeof value.name === 'string'
|
||||
&& typeof value.label === 'string'
|
||||
&& typeof value.description === 'string'
|
||||
&& isRecord(value.inputSchema);
|
||||
}
|
||||
async function invokeProduct(toolCallId, toolName, input, signal) {
|
||||
const response = await bridge('product.invoke', {
|
||||
resourceId: toolCallId,
|
||||
toolName,
|
||||
input,
|
||||
}, signal);
|
||||
return response.result;
|
||||
}
|
||||
|
||||
async function registerDynamicProductTools(pi) {
|
||||
if (WORKER_ROLE !== 'parent') return;
|
||||
const context = await readWorkerContext();
|
||||
const allowedToolNames = new Set(
|
||||
Array.isArray(context.allowedToolNames)
|
||||
? context.allowedToolNames.filter((name) => typeof name === 'string')
|
||||
: [],
|
||||
);
|
||||
const declarations = Array.isArray(context.tools) ? context.tools : [];
|
||||
dynamicLeaseTools = new Set(
|
||||
declarations
|
||||
.filter((declaration) => isToolDeclaration(declaration)
|
||||
&& declaration.projectWriteLease === true
|
||||
&& allowedToolNames.has(declaration.name))
|
||||
.map((declaration) => declaration.name),
|
||||
);
|
||||
for (const declaration of declarations) {
|
||||
if (!isToolDeclaration(declaration) || !allowedToolNames.has(declaration.name)) continue;
|
||||
registerProductTool(
|
||||
pi,
|
||||
declaration.name,
|
||||
declaration.label,
|
||||
declaration.description,
|
||||
declaration.inputSchema,
|
||||
function registerProductTool(name, label, description, parameters) {
|
||||
pi.registerTool({
|
||||
name,
|
||||
label,
|
||||
description,
|
||||
parameters,
|
||||
async execute(toolCallId, params, signal) {
|
||||
return await invokeProduct(toolCallId, name, params, signal);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function isRecord(value) {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function isToolDeclaration(value) {
|
||||
return isRecord(value)
|
||||
&& typeof value.name === 'string'
|
||||
&& typeof value.label === 'string'
|
||||
&& typeof value.description === 'string'
|
||||
&& isRecord(value.inputSchema);
|
||||
}
|
||||
|
||||
async function registerDynamicProductTools() {
|
||||
if (workerRole !== 'parent') return;
|
||||
const context = await readWorkerContext();
|
||||
const allowedToolNames = new Set(
|
||||
Array.isArray(context.allowedToolNames)
|
||||
? context.allowedToolNames.filter((name) => typeof name === 'string')
|
||||
: [],
|
||||
);
|
||||
const declarations = Array.isArray(context.tools) ? context.tools : [];
|
||||
dynamicLeaseTools = new Set(
|
||||
declarations
|
||||
.filter((declaration) => isToolDeclaration(declaration)
|
||||
&& declaration.projectWriteLease === true
|
||||
&& allowedToolNames.has(declaration.name))
|
||||
.map((declaration) => declaration.name),
|
||||
);
|
||||
for (const declaration of declarations) {
|
||||
if (!isToolDeclaration(declaration) || !allowedToolNames.has(declaration.name)) continue;
|
||||
registerProductTool(
|
||||
declaration.name,
|
||||
declaration.label,
|
||||
declaration.description,
|
||||
declaration.inputSchema,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default async function makeloreRuntime(pi) {
|
||||
if (WORKER_ROLE === 'parent') pi.registerTool({
|
||||
if (workerRole === 'parent') pi.registerTool({
|
||||
name: 'ask_user',
|
||||
label: 'Ask user',
|
||||
description: 'Ask the user for a selection, confirmation, short input, or editor text.',
|
||||
@@ -206,7 +256,7 @@ export default async function makeloreRuntime(pi) {
|
||||
},
|
||||
});
|
||||
|
||||
if (WORKER_ROLE === 'parent') pi.registerTool({
|
||||
if (workerRole === 'parent') pi.registerTool({
|
||||
name: 'subagent',
|
||||
label: 'Subagent',
|
||||
description: 'Dispatch one or more managed project Agents in single, parallel, or chain mode.',
|
||||
@@ -252,8 +302,7 @@ export default async function makeloreRuntime(pi) {
|
||||
},
|
||||
});
|
||||
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
if (workerRole === 'parent') registerProductTool(
|
||||
'agent_browser',
|
||||
'Agent browser',
|
||||
'Open, navigate, inspect, or close the Main-owned Makelore development browser.',
|
||||
@@ -270,8 +319,7 @@ export default async function makeloreRuntime(pi) {
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
if (workerRole === 'parent') registerProductTool(
|
||||
'task_state',
|
||||
'Task state',
|
||||
'Publish versioned task steps and progress to the Makelore product timeline.',
|
||||
@@ -291,8 +339,7 @@ export default async function makeloreRuntime(pi) {
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
if (workerRole === 'parent') registerProductTool(
|
||||
'changed_file',
|
||||
'Changed file',
|
||||
'Report project-relative paths touched by managed tools and refresh Conversation changes.',
|
||||
@@ -304,15 +351,14 @@ export default async function makeloreRuntime(pi) {
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
if (workerRole === 'parent') registerProductTool(
|
||||
'runtime_context',
|
||||
'Runtime context',
|
||||
'Read the safe selected-skill and command catalog for this managed worker.',
|
||||
{ type: 'object', additionalProperties: false, properties: {} },
|
||||
);
|
||||
|
||||
await registerDynamicProductTools(pi);
|
||||
await registerDynamicProductTools();
|
||||
|
||||
pi.on('tool_call', async (event, ctx) => {
|
||||
if (!MUTATION_TOOLS.has(event.toolName) && !dynamicLeaseTools.has(event.toolName)) return;
|
||||
@@ -344,7 +390,10 @@ export default async function makeloreRuntime(pi) {
|
||||
});
|
||||
pi.on('agent_end', releaseAll);
|
||||
pi.on('session_shutdown', releaseAll);
|
||||
};
|
||||
}
|
||||
|
||||
export default createMakeloreRuntime();
|
||||
`;
|
||||
|
||||
export async function materializeMakelorePiExtension(extensionsDir: string): Promise<string> {
|
||||
|
||||
Reference in New Issue
Block a user