docs(agent): draft machine-aware resource registry - #140
Open
Yuqing (mydmdm) wants to merge 11 commits into
Open
docs(agent): draft machine-aware resource registry#140Yuqing (mydmdm) wants to merge 11 commits into
Yuqing (mydmdm) wants to merge 11 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines
+453
to
+546
| }>('/:canvasId/resources/local/receipts', async (request, reply) => { | ||
| const body = Buffer.isBuffer(request.body) | ||
| ? request.body.toString('utf8') | ||
| : ''; | ||
| let json: unknown; | ||
| try { | ||
| json = JSON.parse(body || '{}'); | ||
| } catch { | ||
| return reply | ||
| .code(400) | ||
| .send(rfsError('Request body is not valid JSON.', 'invalid_json')); | ||
| } | ||
| const parsed = localResourceReceiptRequestSchema.safeParse(json); | ||
| if (!parsed.success) { | ||
| return reply | ||
| .code(400) | ||
| .send( | ||
| rfsError( | ||
| parsed.error.issues[0]?.message ?? 'Invalid resource receipt.', | ||
| 'invalid_input', | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| const grantHeader = request.headers[RESOURCE_GRANT_HEADER]; | ||
| const grantToken = Array.isArray(grantHeader) | ||
| ? grantHeader[0] | ||
| : grantHeader; | ||
| let release: (() => void) | undefined; | ||
| try { | ||
| const grant = authorizeResourceGrant( | ||
| grantToken, | ||
| request.params.canvasId, | ||
| 'local-resource-management', | ||
| ); | ||
| if (grant.agentletId !== getSupervisedAgentletId()) { | ||
| throw new HostedCapabilityError( | ||
| 'forbidden', | ||
| 'Local resource management is available only on the supervised Agentlet.', | ||
| ); | ||
| } | ||
| release = acquireInvocation( | ||
| grantToken ?? '', | ||
| 'local-resource-management', | ||
| ); | ||
| const root = resolveResourceRoot(); | ||
| assertLocalResourceIdAvailable(parsed.data.id, grant.agentletId); | ||
| try { | ||
| writeReceipt(root, { | ||
| ...parsed.data, | ||
| provider: grant.agentletId, | ||
| installedAt: new Date().toISOString(), | ||
| }); | ||
| } catch (error) { | ||
| request.log.warn( | ||
| { err: error, resourceId: parsed.data.id }, | ||
| 'Local resource receipt validation failed', | ||
| ); | ||
| throw new HostedCapabilityError( | ||
| 'invalid_input', | ||
| 'The local resource receipt or entrypoint is invalid.', | ||
| ); | ||
| } | ||
| const refreshed = refreshLocalAgentResources(); | ||
| const resource = refreshed.records.find( | ||
| (record) => record.id === parsed.data.id, | ||
| ); | ||
| if (!resource) { | ||
| throw new HostedCapabilityError( | ||
| 'internal_error', | ||
| 'The validated local resource was not published.', | ||
| ); | ||
| } | ||
| const response: LocalResourceReceiptResponse = { resource }; | ||
| return reply.send(response); | ||
| } catch (cause) { | ||
| const error = | ||
| cause instanceof HostedCapabilityError ? cause : toInternalError(cause); | ||
| request.log.warn( | ||
| { | ||
| err: cause, | ||
| resourceId: parsed.data.id, | ||
| canvasId: request.params.canvasId, | ||
| outcome: error.code, | ||
| }, | ||
| 'Local resource receipt write failed', | ||
| ); | ||
| return reply | ||
| .code(hostedCapabilityStatus(error)) | ||
| .send(rfsError(publicHostedCapabilityMessage(error), error.code)); | ||
| } finally { | ||
| release?.(); | ||
| } | ||
| }); |
Comment on lines
+552
to
+595
| async (request, reply) => { | ||
| const grantHeader = request.headers[RESOURCE_GRANT_HEADER]; | ||
| const grantToken = Array.isArray(grantHeader) | ||
| ? grantHeader[0] | ||
| : grantHeader; | ||
| let release: (() => void) | undefined; | ||
| try { | ||
| authorizeResourceGrant( | ||
| grantToken, | ||
| request.params.canvasId, | ||
| 'local-resource-management', | ||
| ); | ||
| release = acquireInvocation( | ||
| grantToken ?? '', | ||
| 'local-resource-management', | ||
| ); | ||
| const root = resolveResourceRoot(); | ||
| const removed = | ||
| readReceipt(root, request.params.resourceId) !== undefined; | ||
| removeReceipt(root, request.params.resourceId); | ||
| refreshLocalAgentResources(); | ||
| const response: LocalResourceRemovalResponse = { removed }; | ||
| return reply.send(response); | ||
| } catch (cause) { | ||
| const error = | ||
| cause instanceof HostedCapabilityError | ||
| ? cause | ||
| : toInternalError(cause); | ||
| request.log.warn( | ||
| { | ||
| err: cause, | ||
| resourceId: request.params.resourceId, | ||
| canvasId: request.params.canvasId, | ||
| outcome: error.code, | ||
| }, | ||
| 'Local resource receipt removal failed', | ||
| ); | ||
| return reply | ||
| .code(hostedCapabilityStatus(error)) | ||
| .send(rfsError(publicHostedCapabilityMessage(error), error.code)); | ||
| } finally { | ||
| release?.(); | ||
| } | ||
| }, |
Comment on lines
+1025
to
+1173
| }>('/:canvasId/resources/:resourceId/invoke', async (request, reply) => { | ||
| const { canvasId, resourceId } = request.params; | ||
| if (resourceId !== 'web-search' && resourceId !== 'generate-image') { | ||
| return reply | ||
| .code(404) | ||
| .send( | ||
| rfsError( | ||
| `Hosted resource not found: ${resourceId}`, | ||
| 'resource_not_found', | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| const body = Buffer.isBuffer(request.body) | ||
| ? request.body.toString('utf8') | ||
| : ''; | ||
| let json: unknown; | ||
| try { | ||
| json = JSON.parse(body || '{}'); | ||
| } catch { | ||
| return reply | ||
| .code(400) | ||
| .send(rfsError('Request body is not valid JSON.', 'invalid_json')); | ||
| } | ||
| const envelope = hostedCapabilityInvokeRequestSchema.safeParse(json); | ||
| if (!envelope.success) { | ||
| return reply | ||
| .code(400) | ||
| .send( | ||
| rfsError( | ||
| envelope.error.issues[0]?.message ?? 'Invalid invocation request.', | ||
| 'invalid_input', | ||
| ), | ||
| ); | ||
| } | ||
| const webInput = | ||
| resourceId === 'web-search' | ||
| ? webSearchInvocationInputSchema.safeParse(envelope.data.input) | ||
| : undefined; | ||
| const imageInput = | ||
| resourceId === 'generate-image' | ||
| ? imageGenerationInvocationInputSchema.safeParse(envelope.data.input) | ||
| : undefined; | ||
| const invalidInput = | ||
| webInput?.success === false | ||
| ? webInput.error | ||
| : imageInput?.success === false | ||
| ? imageInput.error | ||
| : undefined; | ||
| if (invalidInput) { | ||
| return reply | ||
| .code(400) | ||
| .send( | ||
| rfsError( | ||
| invalidInput.issues[0]?.message ?? 'Invalid capability input.', | ||
| 'invalid_input', | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| const grantHeader = request.headers[RESOURCE_GRANT_HEADER]; | ||
| const grantToken = Array.isArray(grantHeader) | ||
| ? grantHeader[0] | ||
| : grantHeader; | ||
| const startedAt = Date.now(); | ||
| const abortController = new AbortController(); | ||
| const abortInvocation = () => abortController.abort(); | ||
| request.raw.once('aborted', abortInvocation); | ||
| let grant; | ||
| let release: (() => void) | undefined; | ||
| try { | ||
| grant = authorizeResourceGrant(grantToken, canvasId, resourceId); | ||
| release = acquireInvocation(grantToken ?? '', resourceId); | ||
| let result: unknown; | ||
| if (resourceId === 'web-search') { | ||
| if (!webInput?.success) { | ||
| throw new HostedCapabilityError( | ||
| 'internal_error', | ||
| 'Validated web search input is unavailable.', | ||
| ); | ||
| } | ||
| result = await invokeWebSearch(webInput.data, { | ||
| signal: abortController.signal, | ||
| }); | ||
| } else { | ||
| if (!imageInput?.success) { | ||
| throw new HostedCapabilityError( | ||
| 'internal_error', | ||
| 'Validated image input is unavailable.', | ||
| ); | ||
| } | ||
| result = await invokeImageGeneration( | ||
| imageInput.data, | ||
| { | ||
| canvasId: grant.canvasId, | ||
| }, | ||
| { | ||
| signal: abortController.signal, | ||
| }, | ||
| ); | ||
| } | ||
| request.log.info( | ||
| { | ||
| resourceId, | ||
| profileId: grant.profileId, | ||
| agentletId: grant.agentletId, | ||
| canvasId: grant.canvasId, | ||
| threadId: grant.threadId, | ||
| correlationId: envelope.data.correlationId, | ||
| outcome: 'success', | ||
| latencyMs: Date.now() - startedAt, | ||
| policyVersion: grant.policyVersion, | ||
| }, | ||
| 'Hosted capability invocation', | ||
| ); | ||
| return reply.send({ | ||
| schemaVersion: 1, | ||
| resourceId, | ||
| ...(envelope.data.correlationId | ||
| ? { correlationId: envelope.data.correlationId } | ||
| : {}), | ||
| result, | ||
| }); | ||
| } catch (cause) { | ||
| const error = | ||
| cause instanceof HostedCapabilityError ? cause : toInternalError(cause); | ||
| request.log.warn( | ||
| { | ||
| err: cause, | ||
| resourceId, | ||
| profileId: grant?.profileId, | ||
| agentletId: grant?.agentletId, | ||
| canvasId: grant?.canvasId ?? canvasId, | ||
| threadId: grant?.threadId, | ||
| correlationId: envelope.data.correlationId, | ||
| outcome: error.code, | ||
| latencyMs: Date.now() - startedAt, | ||
| policyVersion: grant?.policyVersion, | ||
| }, | ||
| 'Hosted capability invocation failed', | ||
| ); | ||
| return reply | ||
| .code(hostedCapabilityStatus(error)) | ||
| .send(rfsError(publicHostedCapabilityMessage(error), error.code)); | ||
| } finally { | ||
| request.raw.removeListener('aborted', abortInvocation); | ||
| release?.(); | ||
| } | ||
| }); |
| /** Read and validate one persisted receipt by resource ID. Returns `undefined` if absent. */ | ||
| export function readReceipt(root: string, id: string): ResourceReceipt | undefined { | ||
| const path = receiptFilePath(root, id); | ||
| if (!existsSync(path)) return undefined; |
| export function readReceipt(root: string, id: string): ResourceReceipt | undefined { | ||
| const path = receiptFilePath(root, id); | ||
| if (!existsSync(path)) return undefined; | ||
| const raw = readFileSync(path, 'utf8'); |
| /** Remove a persisted receipt, if present. Idempotent. */ | ||
| export function removeReceipt(root: string, id: string): void { | ||
| const path = receiptFilePath(root, id); | ||
| if (existsSync(path)) { |
| export function removeReceipt(root: string, id: string): void { | ||
| const path = receiptFilePath(root, id); | ||
| if (existsSync(path)) { | ||
| unlinkSync(path); |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Scope
This PR is documentation-only. It deliberately defers Agentlet probes, verified machine observations, desired-installation reconciliation, and implementation schemas.
Relates to #120
Relates to #110