feat: integrate automatic game resource delivery
This commit is contained in:
@@ -93,7 +93,7 @@ interface SubagentBridgeRequest {
|
||||
}
|
||||
|
||||
interface ProductToolBridgeRequest {
|
||||
action: 'product.invoke';
|
||||
action: 'product.invoke' | 'product.invoke.stream';
|
||||
conversationId: string;
|
||||
workerGeneration: number;
|
||||
runId: string;
|
||||
@@ -142,7 +142,7 @@ function bridgeRequest(value: unknown): value is BridgeRequest {
|
||||
&& typeof value.resourceId === 'string';
|
||||
if (!common) return false;
|
||||
if (value.action === 'subagent.dispatch') return 'request' in value;
|
||||
if (value.action === 'product.invoke') {
|
||||
if (value.action === 'product.invoke' || value.action === 'product.invoke.stream') {
|
||||
return typeof value.toolName === 'string'
|
||||
&& PRODUCT_TOOL_NAME_PATTERN.test(value.toolName)
|
||||
&& 'input' in value;
|
||||
@@ -417,7 +417,7 @@ export class PiManagedExtensionHost {
|
||||
this.respond(response, 200, { recorded: true });
|
||||
return;
|
||||
}
|
||||
if (value.action === 'product.invoke') {
|
||||
if (value.action === 'product.invoke' || value.action === 'product.invoke.stream') {
|
||||
if (record.role !== 'parent') {
|
||||
this.respond(response, 403, { error: 'Child workers cannot invoke parent product tools' });
|
||||
return;
|
||||
@@ -431,7 +431,7 @@ export class PiManagedExtensionHost {
|
||||
this.respond(response, 503, { error: 'Product tools are unavailable' });
|
||||
return;
|
||||
}
|
||||
const productResult = await this.productTools.execute(value.toolName, {
|
||||
const context = {
|
||||
conversationId: record.conversationId,
|
||||
workerGeneration: record.generation,
|
||||
runId: value.runId,
|
||||
@@ -440,7 +440,38 @@ export class PiManagedExtensionHost {
|
||||
projectPath: record.projectPath,
|
||||
skillIds: record.skillIds,
|
||||
...(record.effectiveSnapshot ? { effectiveSnapshot: record.effectiveSnapshot } : {}),
|
||||
}, value.input);
|
||||
};
|
||||
if (value.action === 'product.invoke.stream') {
|
||||
response.writeHead(200, {
|
||||
'content-type': 'application/x-ndjson; charset=utf-8',
|
||||
...(this.closing ? { connection: 'close' } : {}),
|
||||
});
|
||||
try {
|
||||
const productResult = await this.productTools.execute(
|
||||
value.toolName,
|
||||
context,
|
||||
value.input,
|
||||
(result) => {
|
||||
if (!response.writableEnded && !response.destroyed) {
|
||||
response.write(`${JSON.stringify({ result })}\n`);
|
||||
}
|
||||
},
|
||||
);
|
||||
if (!response.writableEnded && !response.destroyed) {
|
||||
response.end(`${JSON.stringify({ result: productResult, done: true })}\n`);
|
||||
}
|
||||
} catch {
|
||||
if (!response.writableEnded && !response.destroyed) {
|
||||
response.end(`${JSON.stringify({ error: 'Product tool invocation failed' })}\n`);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const productResult = await this.productTools.execute(
|
||||
value.toolName,
|
||||
context,
|
||||
value.input,
|
||||
);
|
||||
this.respond(response, 200, { result: productResult });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import path from 'node:path';
|
||||
import { atomicWriteText } from '../../../coding-projects/atomic-json';
|
||||
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 5;
|
||||
export const MAKELORE_PI_EXTENSION_VERSION = 6;
|
||||
export const MAKELORE_PI_EXTENSION_FILENAME = `makelore-runtime-v${MAKELORE_PI_EXTENSION_VERSION}.mjs`;
|
||||
|
||||
const BUNDLE_SOURCE = String.raw`
|
||||
@@ -180,15 +180,78 @@ export function createMakeloreRuntime(runtimeDefaults = {}) {
|
||||
return response.result;
|
||||
}
|
||||
|
||||
function registerProductTool(name, label, description, parameters, projectWriteLease = false) {
|
||||
async function invokeProductStream(toolCallId, toolName, input, 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: 'product.invoke.stream',
|
||||
resourceId: toolCallId,
|
||||
toolName,
|
||||
input,
|
||||
}),
|
||||
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 product stream returned no body');
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffered = '';
|
||||
let finalResult;
|
||||
let completed = false;
|
||||
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 (typeof item.error === 'string') throw new Error(item.error);
|
||||
if (item.result) {
|
||||
finalResult = item.result;
|
||||
if (item.done) completed = true;
|
||||
else onUpdate?.(item.result);
|
||||
}
|
||||
}
|
||||
newline = buffered.indexOf('\n');
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
if (!completed || !finalResult) throw new Error('Makelore product stream ended before completion');
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
function registerProductTool(
|
||||
name,
|
||||
label,
|
||||
description,
|
||||
parameters,
|
||||
projectWriteLease = false,
|
||||
streamsProgress = false,
|
||||
) {
|
||||
pi.registerTool({
|
||||
name,
|
||||
label,
|
||||
description,
|
||||
parameters,
|
||||
...(projectWriteLease ? { executionMode: 'sequential' } : {}),
|
||||
async execute(toolCallId, params, signal) {
|
||||
return await invokeProduct(toolCallId, name, params, signal);
|
||||
async execute(toolCallId, params, signal, onUpdate) {
|
||||
return streamsProgress
|
||||
? await invokeProductStream(toolCallId, name, params, signal, onUpdate)
|
||||
: await invokeProduct(toolCallId, name, params, signal);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -229,6 +292,7 @@ export function createMakeloreRuntime(runtimeDefaults = {}) {
|
||||
declaration.description,
|
||||
declaration.inputSchema,
|
||||
dynamicLeaseTools.has(declaration.name),
|
||||
declaration.executionMode === 'job',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,7 @@ export class PiProductTools {
|
||||
toolName: PiProductToolName | string,
|
||||
context: PiProductToolContext,
|
||||
input: unknown,
|
||||
onUpdate?: (result: PiProductToolResult) => void,
|
||||
): Promise<PiProductToolResult> {
|
||||
if (DEVICE_PACKAGE_TOOL_NAMES.includes(toolName as typeof DEVICE_PACKAGE_TOOL_NAMES[number])) {
|
||||
if (!this.options.devicePackageTools) throw new Error('Device package management is unavailable');
|
||||
@@ -176,6 +177,7 @@ export class PiProductTools {
|
||||
workerRole: 'parent',
|
||||
effectiveSkillIds: context.skillIds,
|
||||
value: input,
|
||||
...(onUpdate ? { onUpdate } : {}),
|
||||
});
|
||||
}
|
||||
if (toolName !== 'runtime_context') throw new Error('Product tool is unavailable');
|
||||
|
||||
@@ -1416,7 +1416,7 @@ export async function runFinalAsarExtensionProof(): Promise<PiReleaseExtensionPr
|
||||
providerRequests: requestCounts,
|
||||
subagentStatus: 'complete',
|
||||
subagentSummary: 'REAL_CHILD_COMPLETE',
|
||||
materializedExtension: 'makelore-runtime-v5.mjs',
|
||||
materializedExtension: 'makelore-runtime-v6.mjs',
|
||||
providerFirstEventDelayMs: PROOF_PROVIDER_FIRST_EVENT_DELAY_MS,
|
||||
managedTurns,
|
||||
managedWorkerMilestones: composition.telemetry,
|
||||
|
||||
Reference in New Issue
Block a user