feat: implement PI-090 product tools
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
import path from 'node:path';
|
||||
import { atomicWriteText } from '../../../coding-projects/atomic-json';
|
||||
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 2;
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 3;
|
||||
export const MAKELORE_PI_EXTENSION_FILENAME = `makelore-runtime-v${MAKELORE_PI_EXTENSION_VERSION}.mjs`;
|
||||
|
||||
const BUNDLE_SOURCE = String.raw`
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
const MUTATION_TOOLS = new Set(['bash', 'edit', 'write']);
|
||||
const MUTATION_TOOLS = new Set([
|
||||
'bash',
|
||||
'edit',
|
||||
'write',
|
||||
'game_asset_browser',
|
||||
'game_asset_review',
|
||||
]);
|
||||
const WORKER_ROLE = process.env.MAKELORE_PI_WORKER_ROLE || 'parent';
|
||||
const leases = new Map();
|
||||
const touchedPaths = new Map();
|
||||
|
||||
async function runtimeContext() {
|
||||
const value = JSON.parse(await readFile(process.env.MAKELORE_PI_CONTEXT_FILE, 'utf8'));
|
||||
@@ -84,6 +92,37 @@ async function releaseLease(toolCallId) {
|
||||
|
||||
async function releaseAll() {
|
||||
await Promise.all([...leases.keys()].map(releaseLease));
|
||||
touchedPaths.clear();
|
||||
}
|
||||
|
||||
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 invokeProduct(toolCallId, toolName, input, signal) {
|
||||
const response = await bridge('product.invoke', {
|
||||
resourceId: toolCallId,
|
||||
toolName,
|
||||
input,
|
||||
}, signal);
|
||||
return response.result;
|
||||
}
|
||||
|
||||
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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export default function makeloreRuntime(pi) {
|
||||
@@ -166,8 +205,94 @@ export default function makeloreRuntime(pi) {
|
||||
},
|
||||
});
|
||||
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'agent_browser',
|
||||
'Agent browser',
|
||||
'Open, navigate, inspect, or close the Main-owned Makelore development browser.',
|
||||
{
|
||||
type: 'object', additionalProperties: true, required: ['action'],
|
||||
properties: {
|
||||
action: { type: 'string', enum: ['open', 'status', 'close', 'reset_profile', 'navigate', 'send_cdp', 'read_events', 'read_payload'] },
|
||||
url: { type: 'string' }, navigation: { type: 'string', enum: ['url', 'back', 'forward', 'reload'] },
|
||||
method: { type: 'string' }, params: { type: 'object' }, sessionRef: { type: 'string' },
|
||||
timeoutMs: { type: 'number' }, after: { type: 'number' }, methods: { type: 'array', items: { type: 'string' } },
|
||||
limit: { type: 'number' }, waitMs: { type: 'number' }, handle: { type: 'string' },
|
||||
offset: { type: 'number' }, maxBytes: { type: 'number' },
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'game_asset_browser',
|
||||
'Game assets',
|
||||
'Load product-owned game asset candidates and their current review state.',
|
||||
{ type: 'object', additionalProperties: false, properties: { invocationId: { type: 'string' } } },
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'game_asset_review',
|
||||
'Game asset review',
|
||||
'Load one versioned game asset review interaction without encoding decisions in message text.',
|
||||
{
|
||||
type: 'object', additionalProperties: false,
|
||||
properties: {
|
||||
invocationId: { type: 'string' },
|
||||
candidateIds: { type: 'array', maxItems: 200, items: { type: 'string' } },
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'task_state',
|
||||
'Task state',
|
||||
'Publish versioned task steps and progress to the Makelore product timeline.',
|
||||
{
|
||||
type: 'object', additionalProperties: false, required: ['tasks'],
|
||||
properties: {
|
||||
tasks: {
|
||||
type: 'array', minItems: 1, maxItems: 100,
|
||||
items: {
|
||||
type: 'object', additionalProperties: false, required: ['id', 'title', 'status'],
|
||||
properties: {
|
||||
id: { type: 'string' }, title: { type: 'string' },
|
||||
status: { type: 'string', enum: ['pending', 'running', 'complete', 'error'] },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'changed_file',
|
||||
'Changed file',
|
||||
'Report project-relative paths touched by managed tools and refresh Conversation changes.',
|
||||
{
|
||||
type: 'object', additionalProperties: false,
|
||||
properties: {
|
||||
path: { type: 'string' }, paths: { type: 'array', maxItems: 200, items: { type: 'string' } },
|
||||
refresh: { type: 'boolean' },
|
||||
},
|
||||
},
|
||||
);
|
||||
if (WORKER_ROLE === 'parent') registerProductTool(
|
||||
pi,
|
||||
'runtime_context',
|
||||
'Runtime context',
|
||||
'Read the safe selected-skill and command catalog for this managed worker.',
|
||||
{ type: 'object', additionalProperties: false, properties: {} },
|
||||
);
|
||||
|
||||
pi.on('tool_call', async (event, ctx) => {
|
||||
if (!MUTATION_TOOLS.has(event.toolName)) return;
|
||||
const input = event.input || event.arguments || event.args || {};
|
||||
const touchedPath = (event.toolName === 'write' || event.toolName === 'edit')
|
||||
? projectRelativePath(input.path) : undefined;
|
||||
if (touchedPath) touchedPaths.set(event.toolCallId, [touchedPath]);
|
||||
if (event.toolName === 'bash') {
|
||||
await bridge('changes.bash', { resourceId: event.toolCallId }, ctx.signal).catch(() => undefined);
|
||||
}
|
||||
ctx.ui.setStatus('makelore.write-lease', '等待项目写入');
|
||||
try {
|
||||
const result = await bridge('lease.acquire', { resourceId: event.toolCallId }, ctx.signal);
|
||||
@@ -176,7 +301,14 @@ export default function makeloreRuntime(pi) {
|
||||
ctx.ui.setStatus('makelore.write-lease', undefined);
|
||||
}
|
||||
});
|
||||
pi.on('tool_result', async (event) => releaseLease(event.toolCallId));
|
||||
pi.on('tool_result', async (event) => {
|
||||
await releaseLease(event.toolCallId);
|
||||
const paths = touchedPaths.get(event.toolCallId);
|
||||
touchedPaths.delete(event.toolCallId);
|
||||
if (paths) {
|
||||
await invokeProduct(event.toolCallId, 'changed_file', { paths, refresh: true }).catch(() => undefined);
|
||||
}
|
||||
});
|
||||
pi.on('agent_end', releaseAll);
|
||||
pi.on('session_shutdown', releaseAll);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user