Skip to content

espennilsen/pi / extensions/pi-a2a

pi-a2a

Flagged

FileKindSize
AGENTS.mdmarkdown12.0 KB
CHANGELOG.mdmarkdown1.3 KB
README.mdmarkdown11.0 KB
SKILL.mdskill md9.7 KB
agent-card-auth.test.tsscript1.7 KB
agent-card.tsscript6.1 KB
agent-executor.tsscript38.7 KB
auth-integration.test.tsscript4.6 KB
auth-selector.test.tsscript4.0 KB
auth-selector.tsscript2.6 KB
auth-types.tsscript3.7 KB
client.test.tsscript3.1 KB
client.tsscript14.9 KB
config-auth.test.tsscript4.0 KB
config.tsscript9.1 KB
cost.test.tsscript1.8 KB
hub.test.tsscript6.2 KB
hub.tsscript39.5 KB
inbound-auth.test.tsscript2.6 KB
inbound-auth.tsscript4.4 KB
index.tsscript136.5 KB
logger.tsscript1.5 KB
long-running-task-store.tsscript6.3 KB
outbound-auth.test.tsscript1.5 KB
outbound-auth.tsscript1.6 KB
package-lock.jsonmanifest149.9 KB
package.jsonmanifest1.5 KB
peer-metadata.test.tsscript3.7 KB
peer-metadata.tsscript4.6 KB
port-finder.tsscript742 B
server.tsscript12.6 KB
static-agents.tsscript5.8 KB
supervisor.test.tsscript6.1 KB
supervisor.tsscript8.4 KB
task-store.tsscript12.5 KB
token-provider.test.tsscript5.3 KB
token-provider.tsscript7.7 KB
tool-telemetry.test.tsscript2.8 KB
tool-telemetry.tsscript1.7 KB
tsconfig.jsonconfig274 B
types.tsscript11.3 KB

peer-metadata.ts

View on GitHub
/** Resolve a remote peer's authentication capabilities without handling credentials. */

import type { AuthMode, PeerAuthMetadata } from "./auth-types.ts";
import type { StaticAgentConfig } from "./types.ts";

export interface HubPeerDetail {
	id: string;
	auth?: Omit<PeerAuthMetadata, "source">;
}

export interface ResolvePeerMetadataInput {
	agentId: string;
	endpoint: string;
	/** Set only when the peer was selected from Hub discovery. */
	hubAgentId?: string;
	staticAgent?: StaticAgentConfig;
	/** A previously fetched static Agent Card. */
	cachedCard?: Record<string, unknown> | null;
}

export interface PeerMetadataDependencies {
	getHubAgent?: (agentId: string) => Promise<HubPeerDetail | null>;
	fetchAgentCard?: (endpoint: string, init?: RequestInit) => Promise<Record<string, unknown> | null>;
}

/**
 * Resolve capabilities in source precedence order. Credentials are deliberately
 * never passed to Agent Card discovery: a static apiKey is for later task auth.
 */
export async function resolvePeerMetadata(
	input: ResolvePeerMetadataInput,
	dependencies: PeerMetadataDependencies = {},
): Promise<PeerAuthMetadata> {
	if (input.hubAgentId && dependencies.getHubAgent) {
		const detail = await dependencies.getHubAgent(input.hubAgentId);
		if (detail?.auth) {
			return { ...detail.auth, agentId: detail.auth.agentId || detail.id, endpoint: detail.auth.endpoint || input.endpoint, supportedAuthModes: validModes(detail.auth.supportedAuthModes), source: "hub" };
		}
	}

	const override = input.staticAgent?.auth;
	if (override?.supportedAuthModes !== undefined) {
		return {
			agentId: input.agentId,
			endpoint: input.endpoint,
			supportedAuthModes: validModes(override.supportedAuthModes),
			source: "static-directory",
			...(override.authorizationServer ? { authorizationServer: override.authorizationServer } : {}),
			...(override.resource ? { resource: override.resource } : {}),
			...(override.transport ? { transport: override.transport } : {}),
		};
	}

	const card = input.cachedCard ?? await dependencies.fetchAgentCard?.(input.endpoint, { headers: { Accept: "application/json" } }) ?? null;
	return parseAgentCardAuthMetadata(card, input.agentId, input.endpoint);
}

/** Parse precisely the security requirement forms pi-a2a publishes. */
export function parseAgentCardAuthMetadata(
	card: Record<string, unknown> | null,
	agentId: string,
	endpoint: string,
): PeerAuthMetadata {
	const empty = (): PeerAuthMetadata => ({ agentId, endpoint, supportedAuthModes: [], source: "agent-card" });
	if (!card || !isRecord(card.securitySchemes) || !Array.isArray(card.security)) return empty();

	const schemes = card.securitySchemes;
	const modes = new Set<AuthMode>();
	let authorizationServer: string | undefined;
	for (const requirement of card.security) {
		if (!isRecord(requirement) || Object.keys(requirement).length === 0) return empty();
		let oauth = false;
		let mtls = false;
		let legacy = false;
		for (const name of Object.keys(requirement)) {
			const scheme = schemes[name];
			if (!isRecord(scheme)) return empty();
			if (name === "bearerAuth" && scheme.type === "http" && scheme.scheme === "bearer") {
				legacy = true;
				continue;
			}
			if (scheme.type === "oauth2") {
				oauth = true;
				authorizationServer ??= oauthServer(scheme);
				continue;
			}
			if (scheme.type === "mutualTLS") {
				mtls = true;
				continue;
			}
			return empty();
		}
		// A requirement is an AND set. Mixed legacy/modern or bare mTLS is
		// not a form we publish and therefore must not be guessed at.
		if (legacy && !oauth && !mtls) modes.add("legacy-api-key");
		else if (oauth && !legacy) modes.add(mtls ? "oauth2+mtls" : "oauth2");
		else return empty();
	}
	return {
		agentId,
		endpoint,
		supportedAuthModes: (['legacy-api-key', 'oauth2', 'oauth2+mtls'] as AuthMode[]).filter((mode) => modes.has(mode)),
		source: "agent-card",
		...(authorizationServer ? { authorizationServer } : {}),
		...(typeof card.resource === "string" ? { resource: card.resource } : {}),
	};
}

function oauthServer(scheme: Record<string, unknown>): string | undefined {
	if (typeof scheme.authorizationServer === "string") return scheme.authorizationServer;
	if (typeof scheme.openIdConnectUrl === "string") return scheme.openIdConnectUrl;
	return undefined;
}

function validModes(modes: readonly unknown[] | undefined): AuthMode[] {
	return (modes ?? []).filter(isAuthMode);
}
function isAuthMode(mode: unknown): mode is AuthMode {
	return mode === "legacy-api-key" || mode === "oauth2" || mode === "oauth2+mtls";
}
function isRecord(value: unknown): value is Record<string, unknown> {
	return value !== null && typeof value === "object" && !Array.isArray(value);
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall