@mantyx/sdk 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -1
- package/README.md +28 -0
- package/dist/a2a-server.cjs +2 -0
- package/dist/a2a-server.cjs.map +1 -1
- package/dist/a2a-server.d.cts +7 -1
- package/dist/a2a-server.d.ts +7 -1
- package/dist/a2a-server.js +3 -1
- package/dist/a2a-server.js.map +1 -1
- package/dist/{chunk-DR625E6B.js → chunk-36QXU6NV.js} +79 -4
- package/dist/chunk-36QXU6NV.js.map +1 -0
- package/dist/{client-Byb0Zdo7.d.cts → client-C-jl6_nH.d.cts} +129 -4
- package/dist/{client-Byb0Zdo7.d.ts → client-C-jl6_nH.d.ts} +129 -4
- package/dist/index.cjs +79 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/docs/agent-runs-protocol.md +169 -21
- package/docs/wire-protocol.md +126 -26
- package/package.json +1 -1
- package/dist/chunk-DR625E6B.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as A2AToolRef, a as AgentSession, b as AgentSpecBase, c as AssistantDeltaEvent, d as AssistantMessageEvent, C as CancelledEvent, D as DEFAULT_BASE_URL, e as DEFAULT_OAUTH_BASE_URL, f as DEFAULT_REFRESH_SKEW_MS, g as DefineLocalA2AOptions, h as DefineLocalMcpOptions, i as DefineLocalToolOptions, E as ErrorEvent, L as LocalA2ATool, j as LocalHandlers, k as LocalMcpHttpTransport, l as LocalMcpServer, m as LocalMcpStdioTransport, n as LocalTool, o as LocalToolCallEvent, p as LocalToolResultInEvent, q as LoopDetectedEvent, r as LoopDetection, s as MantyxA2AOptions, t as MantyxAuthError, M as MantyxClient, u as MantyxClientOptions, v as MantyxError, w as MantyxMcpOptions, x as MantyxNetworkError, y as MantyxOAuthClient, z as MantyxOAuthClientOptions, B as MantyxOAuthError, F as MantyxParseError, G as MantyxPluginToolRef, H as MantyxRunError, I as MantyxRunErrorInit, J as MantyxRunErrorModel, K as MantyxRunErrorTokens, N as MantyxScopeError,
|
|
1
|
+
export { A as A2AToolRef, a as AgentSession, b as AgentSpecBase, c as AssistantDeltaEvent, d as AssistantMessageEvent, C as CancelledEvent, D as DEFAULT_BASE_URL, e as DEFAULT_OAUTH_BASE_URL, f as DEFAULT_REFRESH_SKEW_MS, g as DefineLocalA2AOptions, h as DefineLocalMcpOptions, i as DefineLocalToolOptions, E as ErrorEvent, L as LocalA2ATool, j as LocalHandlers, k as LocalMcpHttpTransport, l as LocalMcpServer, m as LocalMcpStdioTransport, n as LocalTool, o as LocalToolCallEvent, p as LocalToolResultInEvent, q as LoopDetectedEvent, r as LoopDetection, s as MantyxA2AOptions, t as MantyxAuthError, M as MantyxClient, u as MantyxClientOptions, v as MantyxError, w as MantyxMcpOptions, x as MantyxNetworkError, y as MantyxOAuthClient, z as MantyxOAuthClientOptions, B as MantyxOAuthError, F as MantyxParseError, G as MantyxPluginToolRef, H as MantyxRunError, I as MantyxRunErrorInit, J as MantyxRunErrorModel, K as MantyxRunErrorTokens, N as MantyxScopeError, P as MantyxToolError, Q as MantyxToolRef, S as McpToolRef, U as ModelCatalog, V as ModelInfo, W as OAuthToken, O as OutputSchema, X as OutputSchemaValue, R as ReasoningLevel, Y as RefreshOptions, Z as RefreshTokenSourceOptions, _ as ResultEvent, $ as RevokeOptions, a0 as RunEvent, a1 as RunEventBase, a2 as RunModelInfo, a3 as RunResult, a4 as RunSpec, a5 as RunTokenUsage, a6 as ServerToolResultEvent, a7 as SessionInfo, a8 as SessionListResult, a9 as SessionSpec, aa as SessionSummary, ab as Supervisor, ac as SupervisorAction, ad as SupervisorEvent, ae as ThinkingDeltaEvent, af as TokenRequestReason, ag as TokenSource, ah as ToolBudget, ai as ToolBudgetExceededEvent, aj as ToolBudgets, T as ToolRef, ak as UserMessageEvent, al as ZodLikeObject, am as defineLocalA2A, an as defineLocalMcp, ao as defineLocalTool, ap as isLocalA2ATool, aq as isLocalMcpServer, ar as isLocalTool, as as mantyxA2A, at as mantyxMcp, au as mantyxPluginTool, av as mantyxTool, aw as parseRunOutput } from './client-C-jl6_nH.cjs';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -52,6 +52,6 @@ declare function readSseStream(body: ReadableStream<Uint8Array> | null, opts?: S
|
|
|
52
52
|
/**
|
|
53
53
|
* Release version — synced from repo root VERSION (`npm run sync-version`).
|
|
54
54
|
*/
|
|
55
|
-
declare const SDK_VERSION = "0.
|
|
55
|
+
declare const SDK_VERSION = "0.13.0";
|
|
56
56
|
|
|
57
57
|
export { SDK_VERSION, type SseEvent, type SseStreamOptions, readSseStream, toToolParametersWire, zodToJsonSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as A2AToolRef, a as AgentSession, b as AgentSpecBase, c as AssistantDeltaEvent, d as AssistantMessageEvent, C as CancelledEvent, D as DEFAULT_BASE_URL, e as DEFAULT_OAUTH_BASE_URL, f as DEFAULT_REFRESH_SKEW_MS, g as DefineLocalA2AOptions, h as DefineLocalMcpOptions, i as DefineLocalToolOptions, E as ErrorEvent, L as LocalA2ATool, j as LocalHandlers, k as LocalMcpHttpTransport, l as LocalMcpServer, m as LocalMcpStdioTransport, n as LocalTool, o as LocalToolCallEvent, p as LocalToolResultInEvent, q as LoopDetectedEvent, r as LoopDetection, s as MantyxA2AOptions, t as MantyxAuthError, M as MantyxClient, u as MantyxClientOptions, v as MantyxError, w as MantyxMcpOptions, x as MantyxNetworkError, y as MantyxOAuthClient, z as MantyxOAuthClientOptions, B as MantyxOAuthError, F as MantyxParseError, G as MantyxPluginToolRef, H as MantyxRunError, I as MantyxRunErrorInit, J as MantyxRunErrorModel, K as MantyxRunErrorTokens, N as MantyxScopeError,
|
|
1
|
+
export { A as A2AToolRef, a as AgentSession, b as AgentSpecBase, c as AssistantDeltaEvent, d as AssistantMessageEvent, C as CancelledEvent, D as DEFAULT_BASE_URL, e as DEFAULT_OAUTH_BASE_URL, f as DEFAULT_REFRESH_SKEW_MS, g as DefineLocalA2AOptions, h as DefineLocalMcpOptions, i as DefineLocalToolOptions, E as ErrorEvent, L as LocalA2ATool, j as LocalHandlers, k as LocalMcpHttpTransport, l as LocalMcpServer, m as LocalMcpStdioTransport, n as LocalTool, o as LocalToolCallEvent, p as LocalToolResultInEvent, q as LoopDetectedEvent, r as LoopDetection, s as MantyxA2AOptions, t as MantyxAuthError, M as MantyxClient, u as MantyxClientOptions, v as MantyxError, w as MantyxMcpOptions, x as MantyxNetworkError, y as MantyxOAuthClient, z as MantyxOAuthClientOptions, B as MantyxOAuthError, F as MantyxParseError, G as MantyxPluginToolRef, H as MantyxRunError, I as MantyxRunErrorInit, J as MantyxRunErrorModel, K as MantyxRunErrorTokens, N as MantyxScopeError, P as MantyxToolError, Q as MantyxToolRef, S as McpToolRef, U as ModelCatalog, V as ModelInfo, W as OAuthToken, O as OutputSchema, X as OutputSchemaValue, R as ReasoningLevel, Y as RefreshOptions, Z as RefreshTokenSourceOptions, _ as ResultEvent, $ as RevokeOptions, a0 as RunEvent, a1 as RunEventBase, a2 as RunModelInfo, a3 as RunResult, a4 as RunSpec, a5 as RunTokenUsage, a6 as ServerToolResultEvent, a7 as SessionInfo, a8 as SessionListResult, a9 as SessionSpec, aa as SessionSummary, ab as Supervisor, ac as SupervisorAction, ad as SupervisorEvent, ae as ThinkingDeltaEvent, af as TokenRequestReason, ag as TokenSource, ah as ToolBudget, ai as ToolBudgetExceededEvent, aj as ToolBudgets, T as ToolRef, ak as UserMessageEvent, al as ZodLikeObject, am as defineLocalA2A, an as defineLocalMcp, ao as defineLocalTool, ap as isLocalA2ATool, aq as isLocalMcpServer, ar as isLocalTool, as as mantyxA2A, at as mantyxMcp, au as mantyxPluginTool, av as mantyxTool, aw as parseRunOutput } from './client-C-jl6_nH.js';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -52,6 +52,6 @@ declare function readSseStream(body: ReadableStream<Uint8Array> | null, opts?: S
|
|
|
52
52
|
/**
|
|
53
53
|
* Release version — synced from repo root VERSION (`npm run sync-version`).
|
|
54
54
|
*/
|
|
55
|
-
declare const SDK_VERSION = "0.
|
|
55
|
+
declare const SDK_VERSION = "0.13.0";
|
|
56
56
|
|
|
57
57
|
export { SDK_VERSION, type SseEvent, type SseStreamOptions, readSseStream, toToolParametersWire, zodToJsonSchema };
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
readSseStream,
|
|
24
24
|
toToolParametersWire,
|
|
25
25
|
zodToJsonSchema
|
|
26
|
-
} from "./chunk-
|
|
26
|
+
} from "./chunk-36QXU6NV.js";
|
|
27
27
|
|
|
28
28
|
// src/oauth.ts
|
|
29
29
|
var DEFAULT_OAUTH_BASE_URL = "https://app.mantyx.io";
|
|
@@ -245,7 +245,7 @@ function normalizeScope(scope) {
|
|
|
245
245
|
}
|
|
246
246
|
|
|
247
247
|
// src/version.ts
|
|
248
|
-
var SDK_VERSION = "0.
|
|
248
|
+
var SDK_VERSION = "0.13.0";
|
|
249
249
|
export {
|
|
250
250
|
AgentSession,
|
|
251
251
|
DEFAULT_BASE_URL,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/oauth.ts","../src/version.ts"],"sourcesContent":["/**\n * MANTYX OAuth 2.0 refresh client: trade a stored refresh token for\n * short-lived access tokens, revoke tokens at sign-out, and expose\n * a {@link TokenSource} the {@link MantyxClient} HTTP layer calls\n * before every request (and again on 401).\n *\n * The library is intentionally **refresh-only**. It assumes the caller\n * already obtained the refresh token through their own sign-in flow\n * (Authorization Code + PKCE in a browser, native redirect, server-\n * side exchange — whatever fits the host application). The SDK does\n * not drive consent, does not initiate auth-code exchanges, and does\n * not bundle PKCE helpers.\n *\n * Wire contract (`docs/oauth.md`):\n *\n * - Token endpoint: `POST <baseUrl>/api/oauth/token`, form-encoded,\n * `grant_type=refresh_token`. Echoes back the same `refresh_token`\n * the client sent (refresh tokens are persistent and non-rotating).\n * - Revoke endpoint: `POST <baseUrl>/api/oauth/revoke`, form-encoded.\n * - Access tokens (`mantyx_at_…`) live 1 hour (`expires_in: 3600`).\n * - Refresh tokens (`mantyx_rt_…`) are long-lived; the caller persists\n * them once at first sign-in (encrypted at rest) and the SDK re-mints\n * access tokens from the same value on demand.\n */\n\nimport { MantyxError, MantyxNetworkError } from \"./errors.js\";\n\nexport const DEFAULT_OAUTH_BASE_URL = \"https://app.mantyx.io\";\n\n/** Skew (ms) before `expiresAt` at which a TokenSource will pre-emptively refresh. Default 60s. */\nexport const DEFAULT_REFRESH_SKEW_MS = 60_000;\n\n/**\n * Raised on a non-2xx response from `POST /api/oauth/token` or\n * `POST /api/oauth/revoke`. Carries the RFC 6749 `error` discriminator\n * (`\"invalid_grant\"`, `\"invalid_client\"`, `\"unsupported_grant_type\"`,\n * …) and the optional `error_description` so callers can branch on\n * machine-readable values without parsing the human message.\n *\n * `invalid_grant` from the refresh path specifically signals that the\n * refresh token has been revoked (or the OAuth grant / application\n * was deleted). The SDK never loops on this — callers should route\n * the user back to a fresh sign-in.\n */\nexport class MantyxOAuthError extends MantyxError {\n readonly oauthError: string;\n readonly oauthErrorDescription: string | undefined;\n\n constructor(\n oauthError: string,\n oauthErrorDescription: string | undefined,\n status: number,\n ) {\n const message = oauthErrorDescription\n ? `OAuth ${oauthError}: ${oauthErrorDescription}`\n : `OAuth ${oauthError}`;\n super(message, { code: oauthError, status });\n this.name = \"MantyxOAuthError\";\n this.oauthError = oauthError;\n this.oauthErrorDescription = oauthErrorDescription;\n }\n}\n\n/**\n * Decoded `POST /api/oauth/token` response, augmented with an absolute\n * `expiresAt` timestamp the SDK uses to decide when to refresh.\n *\n * On the refresh grant the response's `refreshToken` is identical to\n * the value the client just sent (refresh tokens never rotate). The\n * field is surfaced for symmetry with whatever the calling app's\n * sign-in flow already does.\n */\nexport interface OAuthToken {\n readonly accessToken: string;\n readonly refreshToken: string | undefined;\n readonly tokenType: string;\n readonly expiresIn: number;\n /** Absolute Unix-ms timestamp set when the SDK parsed the response. */\n readonly expiresAt: number;\n readonly scope: string | undefined;\n}\n\n/** Why the SDK asked the {@link TokenSource} for the current access token. */\nexport type TokenRequestReason = \"initial\" | \"expired\" | \"unauthorized\";\n\n/**\n * A `TokenSource` produces the current access token on demand. The\n * {@link MantyxClient} HTTP layer calls it before every request. When\n * called with `reason: \"unauthorized\"` the source MUST force a refresh\n * (do not return a cached value); this is how the SDK recovers from\n * 401s caused by a token that the server already invalidated.\n *\n * Implementations should be safe to call from many concurrent requests.\n */\nexport type TokenSource = (reason?: TokenRequestReason) => Promise<string>;\n\n/** Caller-supplied options for `MantyxOAuthClient`. */\nexport interface MantyxOAuthClientOptions {\n /**\n * OAuth `client_id` issued at app registration (token prefix\n * `mantyx_oa_`).\n */\n clientId: string;\n /**\n * OAuth `client_secret` issued at app registration (token prefix\n * `mantyx_oas_`). Every MANTYX OAuth app is a confidential client,\n * so this is always required for token + revoke calls. Treat as a\n * deployment secret — do not bundle into browser builds.\n */\n clientSecret: string;\n /**\n * Origin of the MANTYX deployment. Defaults to `https://app.mantyx.io`.\n * The OAuth endpoints are mounted at `<baseUrl>/api/oauth/...`.\n */\n baseUrl?: string;\n /** Optional `fetch` override (e.g. node-fetch wrapper). Default: global `fetch`. */\n fetch?: typeof fetch;\n /** Default per-request timeout in milliseconds. Default: 30s. */\n timeoutMs?: number;\n}\n\nexport interface RefreshOptions {\n refreshToken: string;\n /**\n * Optional scope narrowing. Must be a subset of the scopes already\n * granted to the refresh token (server enforces this). Useful when\n * an SDK consumer wants a short-scope access token for a specific\n * sub-operation.\n */\n scope?: string | readonly string[];\n}\n\nexport interface RevokeOptions {\n token: string;\n}\n\nexport interface RefreshTokenSourceOptions {\n refreshToken: string;\n /** Optional scope narrowing applied on every refresh. */\n scope?: string | readonly string[];\n /**\n * How many ms before `expiresAt` the source proactively refreshes.\n * Defaults to {@link DEFAULT_REFRESH_SKEW_MS} (60s).\n */\n refreshSkewMs?: number;\n /**\n * Optional initial access token + expiry to seed the source's cache\n * with (e.g. the token already in hand from the host application's\n * sign-in flow). When omitted, the source mints one on the first\n * call.\n */\n initialToken?: OAuthToken;\n}\n\n/**\n * Refresh-only wrapper around the MANTYX OAuth 2.0 authorization-server\n * endpoints. App-scoped (one per `{clientId, clientSecret}` pair);\n * construct independently of {@link MantyxClient}, then either call\n * {@link refresh} / {@link revoke} directly or hand a `TokenSource`\n * produced by {@link refreshTokenSource} to `MantyxClient` for fully\n * transparent refresh on every request.\n *\n * The client deliberately does **not** drive the authorization-code\n * exchange or any other \"initiate sign-in\" grant. The caller is\n * expected to obtain the refresh token through their own consent flow\n * and persist it before constructing this client.\n */\nexport class MantyxOAuthClient {\n readonly clientId: string;\n readonly baseUrl: string;\n private readonly clientSecret: string;\n private readonly fetchImpl: typeof fetch;\n private readonly timeoutMs: number;\n\n constructor(opts: MantyxOAuthClientOptions) {\n if (!opts.clientId) {\n throw new MantyxError(\"`clientId` is required for MantyxOAuthClient\");\n }\n if (!opts.clientSecret) {\n throw new MantyxError(\"`clientSecret` is required for MantyxOAuthClient\");\n }\n const f = opts.fetch ?? globalThis.fetch;\n if (typeof f !== \"function\") {\n throw new MantyxError(\n \"Global fetch is not available; pass a custom `fetch` implementation in MantyxOAuthClientOptions.\",\n );\n }\n this.clientId = opts.clientId;\n this.clientSecret = opts.clientSecret;\n this.baseUrl = (opts.baseUrl ?? DEFAULT_OAUTH_BASE_URL).replace(/\\/+$/, \"\");\n this.fetchImpl = f;\n this.timeoutMs = opts.timeoutMs ?? 30_000;\n }\n\n /**\n * Mint a fresh access token from a stored refresh token. The\n * returned `refreshToken` is identical to the input — refresh\n * tokens are persistent and non-rotating, so the field is\n * surfaced only for symmetry with the response shape.\n *\n * On `400 invalid_grant` the refresh token has been revoked (or its\n * grant / app was deleted); the SDK surfaces a\n * {@link MantyxOAuthError} and callers must drive a fresh sign-in.\n */\n async refresh(opts: RefreshOptions): Promise<OAuthToken> {\n if (!opts.refreshToken) {\n throw new MantyxError(\"`refreshToken` is required for MantyxOAuthClient.refresh\");\n }\n const body: Record<string, string> = {\n grant_type: \"refresh_token\",\n refresh_token: opts.refreshToken,\n };\n const scope = normalizeScope(opts.scope);\n if (scope !== undefined) body.scope = scope;\n return this.token(body);\n }\n\n /**\n * Revoke an access or refresh token (RFC 7009). The server always\n * returns 200, even for unknown tokens. Revoking a **refresh**\n * token kills the refresh and every live access token tied to its\n * grant; revoking an **access** token kills only that one.\n */\n async revoke(opts: RevokeOptions): Promise<void> {\n if (!opts.token) {\n throw new MantyxError(\"`token` is required for MantyxOAuthClient.revoke\");\n }\n await this.formPost(\"/api/oauth/revoke\", {\n token: opts.token,\n });\n }\n\n /**\n * Build a long-lived {@link TokenSource} that re-mints access\n * tokens from the supplied refresh token. Pass the returned source\n * to `new MantyxClient({ tokenSource, workspaceSlug, ... })`. The\n * source caches the access token in-memory and refreshes\n * proactively when the cached value is within `refreshSkewMs` of\n * `expiresAt`, or eagerly when `MantyxClient` reports a 401.\n *\n * Pass `initialToken` if the calling app already has a non-expired\n * access token in hand (e.g. straight out of the sign-in flow) to\n * avoid an extra round-trip on the first request.\n */\n refreshTokenSource(opts: RefreshTokenSourceOptions): TokenSource {\n if (!opts.refreshToken) {\n throw new MantyxError(\"`refreshToken` is required for MantyxOAuthClient.refreshTokenSource\");\n }\n const skew = opts.refreshSkewMs ?? DEFAULT_REFRESH_SKEW_MS;\n const cache: TokenCache = { token: opts.initialToken, inflight: null };\n const refreshToken = opts.refreshToken;\n return makeTokenSource(cache, skew, async () => {\n return this.refresh({ refreshToken, scope: opts.scope });\n });\n }\n\n // -------------------------------------------------------------- internals\n\n /**\n * POST `application/x-www-form-urlencoded` to `/api/oauth/token` and\n * decode the {@link OAuthToken} response. Always injects `client_id`\n * + `client_secret` from the constructor.\n */\n private async token(body: Record<string, string>): Promise<OAuthToken> {\n const res = await this.formPost(\"/api/oauth/token\", body);\n let parsed: Record<string, unknown> = {};\n try {\n parsed = (await res.json()) as Record<string, unknown>;\n } catch {\n throw new MantyxOAuthError(\n \"invalid_response\",\n \"Token endpoint returned a non-JSON response\",\n res.status,\n );\n }\n const accessToken = typeof parsed.access_token === \"string\" ? parsed.access_token : \"\";\n if (!accessToken) {\n throw new MantyxOAuthError(\n \"invalid_response\",\n \"Token endpoint response is missing `access_token`\",\n res.status,\n );\n }\n const expiresIn = typeof parsed.expires_in === \"number\" ? parsed.expires_in : 3600;\n return {\n accessToken,\n refreshToken: typeof parsed.refresh_token === \"string\" ? parsed.refresh_token : undefined,\n tokenType: typeof parsed.token_type === \"string\" ? parsed.token_type : \"Bearer\",\n expiresIn,\n expiresAt: Date.now() + expiresIn * 1000,\n scope: typeof parsed.scope === \"string\" ? parsed.scope : undefined,\n };\n }\n\n private async formPost(path: string, body: Record<string, string>): Promise<Response> {\n const url = `${this.baseUrl}${path}`;\n const params = new URLSearchParams({\n ...body,\n client_id: this.clientId,\n client_secret: this.clientSecret,\n });\n const ctrl = new AbortController();\n const t = setTimeout(() => ctrl.abort(), this.timeoutMs);\n let res: Response;\n try {\n res = await this.fetchImpl(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/x-www-form-urlencoded\",\n Accept: \"application/json\",\n },\n body: params.toString(),\n signal: ctrl.signal,\n });\n } catch (err) {\n if (ctrl.signal.aborted) {\n throw new MantyxNetworkError(`OAuth request timed out after ${this.timeoutMs}ms`);\n }\n throw new MantyxNetworkError(`OAuth network error: ${(err as Error).message}`, {\n cause: err,\n });\n } finally {\n clearTimeout(t);\n }\n if (!res.ok) {\n let errBody: { error?: unknown; error_description?: unknown } = {};\n try {\n errBody = (await res.json()) as typeof errBody;\n } catch {\n // ignore\n }\n const oauthError = typeof errBody.error === \"string\" ? errBody.error : `http_${res.status}`;\n const desc =\n typeof errBody.error_description === \"string\" ? errBody.error_description : undefined;\n throw new MantyxOAuthError(oauthError, desc, res.status);\n }\n return res;\n }\n}\n\n// -------------------------------------------------------------- internals\n\ninterface TokenCache {\n token: OAuthToken | undefined;\n inflight: Promise<OAuthToken> | null;\n}\n\n/**\n * Wrap a `mintToken` thunk into a single-flight {@link TokenSource}\n * with a cache + proactive-refresh skew. The cache is overwritten\n * atomically on every successful mint; the in-flight promise\n * collapses N concurrent expired-token observers into one mint call.\n *\n * Single-flight is an efficiency, not a correctness requirement —\n * `docs/oauth.md` explicitly allows multiple concurrent refreshes\n * against the same refresh token — but it keeps the token-endpoint\n * QPS reasonable when an SDK consumer fans out work in parallel.\n */\nfunction makeTokenSource(\n cache: TokenCache,\n skewMs: number,\n mint: () => Promise<OAuthToken>,\n): TokenSource {\n return async (reason: TokenRequestReason = \"initial\"): Promise<string> => {\n if (reason !== \"unauthorized\" && cache.token && !isExpiring(cache.token, skewMs)) {\n return cache.token.accessToken;\n }\n if (cache.inflight) {\n const t = await cache.inflight;\n if (reason === \"unauthorized\" && t === cache.token) {\n // If the inflight refresh was triggered by a benign cache miss\n // and we observed an unauthorized hint after it started, fall\n // through and mint again so the caller never gets a stale token.\n } else {\n return t.accessToken;\n }\n }\n cache.inflight = mint().then(\n (t) => {\n cache.token = t;\n return t;\n },\n (err: unknown) => {\n throw err;\n },\n );\n try {\n const t = await cache.inflight;\n return t.accessToken;\n } finally {\n cache.inflight = null;\n }\n };\n}\n\nfunction isExpiring(token: OAuthToken, skewMs: number): boolean {\n return token.expiresAt - Date.now() <= skewMs;\n}\n\nfunction normalizeScope(scope: string | readonly string[] | undefined): string | undefined {\n if (scope === undefined) return undefined;\n if (typeof scope === \"string\") {\n const trimmed = scope.trim();\n return trimmed.length > 0 ? trimmed : undefined;\n }\n const joined = scope.filter((s) => typeof s === \"string\" && s.length > 0).join(\" \");\n return joined.length > 0 ? joined : undefined;\n}\n","/**\n * Release version — synced from repo root VERS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.0 refresh client: trade a stored refresh token for\n * short-lived access tokens, revoke tokens at sign-out, and expose\n * a {@link TokenSource} the {@link MantyxClient} HTTP layer calls\n * before every request (and again on 401).\n *\n * The library is intentionally **refresh-only**. It assumes the caller\n * already obtained the refresh token through their own sign-in flow\n * (Authorization Code + PKCE in a browser, native redirect, server-\n * side exchange — whatever fits the host application). The SDK does\n * not drive consent, does not initiate auth-code exchanges, and does\n * not bundle PKCE helpers.\n *\n * Wire contract (`docs/oauth.md`):\n *\n * - Token endpoint: `POST <baseUrl>/api/oauth/token`, form-encoded,\n * `grant_type=refresh_token`. Echoes back the same `refresh_token`\n * the client sent (refresh tokens are persistent and non-rotating).\n * - Revoke endpoint: `POST <baseUrl>/api/oauth/revoke`, form-encoded.\n * - Access tokens (`mantyx_at_…`) live 1 hour (`expires_in: 3600`).\n * - Refresh tokens (`mantyx_rt_…`) are long-lived; the caller persists\n * them once at first sign-in (encrypted at rest) and the SDK re-mints\n * access tokens from the same value on demand.\n */\n\nimport { MantyxError, MantyxNetworkError } from \"./errors.js\";\n\nexport const DEFAULT_OAUTH_BASE_URL = \"https://app.mantyx.io\";\n\n/** Skew (ms) before `expiresAt` at which a TokenSource will pre-emptively refresh. Default 60s. */\nexport const DEFAULT_REFRESH_SKEW_MS = 60_000;\n\n/**\n * Raised on a non-2xx response from `POST /api/oauth/token` or\n * `POST /api/oauth/revoke`. Carries the RFC 6749 `error` discriminator\n * (`\"invalid_grant\"`, `\"invalid_client\"`, `\"unsupported_grant_type\"`,\n * …) and the optional `error_description` so callers can branch on\n * machine-readable values without parsing the human message.\n *\n * `invalid_grant` from the refresh path specifically signals that the\n * refresh token has been revoked (or the OAuth grant / application\n * was deleted). The SDK never loops on this — callers should route\n * the user back to a fresh sign-in.\n */\nexport class MantyxOAuthError extends MantyxError {\n readonly oauthError: string;\n readonly oauthErrorDescription: string | undefined;\n\n constructor(\n oauthError: string,\n oauthErrorDescription: string | undefined,\n status: number,\n ) {\n const message = oauthErrorDescription\n ? `OAuth ${oauthError}: ${oauthErrorDescription}`\n : `OAuth ${oauthError}`;\n super(message, { code: oauthError, status });\n this.name = \"MantyxOAuthError\";\n this.oauthError = oauthError;\n this.oauthErrorDescription = oauthErrorDescription;\n }\n}\n\n/**\n * Decoded `POST /api/oauth/token` response, augmented with an absolute\n * `expiresAt` timestamp the SDK uses to decide when to refresh.\n *\n * On the refresh grant the response's `refreshToken` is identical to\n * the value the client just sent (refresh tokens never rotate). The\n * field is surfaced for symmetry with whatever the calling app's\n * sign-in flow already does.\n */\nexport interface OAuthToken {\n readonly accessToken: string;\n readonly refreshToken: string | undefined;\n readonly tokenType: string;\n readonly expiresIn: number;\n /** Absolute Unix-ms timestamp set when the SDK parsed the response. */\n readonly expiresAt: number;\n readonly scope: string | undefined;\n}\n\n/** Why the SDK asked the {@link TokenSource} for the current access token. */\nexport type TokenRequestReason = \"initial\" | \"expired\" | \"unauthorized\";\n\n/**\n * A `TokenSource` produces the current access token on demand. The\n * {@link MantyxClient} HTTP layer calls it before every request. When\n * called with `reason: \"unauthorized\"` the source MUST force a refresh\n * (do not return a cached value); this is how the SDK recovers from\n * 401s caused by a token that the server already invalidated.\n *\n * Implementations should be safe to call from many concurrent requests.\n */\nexport type TokenSource = (reason?: TokenRequestReason) => Promise<string>;\n\n/** Caller-supplied options for `MantyxOAuthClient`. */\nexport interface MantyxOAuthClientOptions {\n /**\n * OAuth `client_id` issued at app registration (token prefix\n * `mantyx_oa_`).\n */\n clientId: string;\n /**\n * OAuth `client_secret` issued at app registration (token prefix\n * `mantyx_oas_`). Every MANTYX OAuth app is a confidential client,\n * so this is always required for token + revoke calls. Treat as a\n * deployment secret — do not bundle into browser builds.\n */\n clientSecret: string;\n /**\n * Origin of the MANTYX deployment. Defaults to `https://app.mantyx.io`.\n * The OAuth endpoints are mounted at `<baseUrl>/api/oauth/...`.\n */\n baseUrl?: string;\n /** Optional `fetch` override (e.g. node-fetch wrapper). Default: global `fetch`. */\n fetch?: typeof fetch;\n /** Default per-request timeout in milliseconds. Default: 30s. */\n timeoutMs?: number;\n}\n\nexport interface RefreshOptions {\n refreshToken: string;\n /**\n * Optional scope narrowing. Must be a subset of the scopes already\n * granted to the refresh token (server enforces this). Useful when\n * an SDK consumer wants a short-scope access token for a specific\n * sub-operation.\n */\n scope?: string | readonly string[];\n}\n\nexport interface RevokeOptions {\n token: string;\n}\n\nexport interface RefreshTokenSourceOptions {\n refreshToken: string;\n /** Optional scope narrowing applied on every refresh. */\n scope?: string | readonly string[];\n /**\n * How many ms before `expiresAt` the source proactively refreshes.\n * Defaults to {@link DEFAULT_REFRESH_SKEW_MS} (60s).\n */\n refreshSkewMs?: number;\n /**\n * Optional initial access token + expiry to seed the source's cache\n * with (e.g. the token already in hand from the host application's\n * sign-in flow). When omitted, the source mints one on the first\n * call.\n */\n initialToken?: OAuthToken;\n}\n\n/**\n * Refresh-only wrapper around the MANTYX OAuth 2.0 authorization-server\n * endpoints. App-scoped (one per `{clientId, clientSecret}` pair);\n * construct independently of {@link MantyxClient}, then either call\n * {@link refresh} / {@link revoke} directly or hand a `TokenSource`\n * produced by {@link refreshTokenSource} to `MantyxClient` for fully\n * transparent refresh on every request.\n *\n * The client deliberately does **not** drive the authorization-code\n * exchange or any other \"initiate sign-in\" grant. The caller is\n * expected to obtain the refresh token through their own consent flow\n * and persist it before constructing this client.\n */\nexport class MantyxOAuthClient {\n readonly clientId: string;\n readonly baseUrl: string;\n private readonly clientSecret: string;\n private readonly fetchImpl: typeof fetch;\n private readonly timeoutMs: number;\n\n constructor(opts: MantyxOAuthClientOptions) {\n if (!opts.clientId) {\n throw new MantyxError(\"`clientId` is required for MantyxOAuthClient\");\n }\n if (!opts.clientSecret) {\n throw new MantyxError(\"`clientSecret` is required for MantyxOAuthClient\");\n }\n const f = opts.fetch ?? globalThis.fetch;\n if (typeof f !== \"function\") {\n throw new MantyxError(\n \"Global fetch is not available; pass a custom `fetch` implementation in MantyxOAuthClientOptions.\",\n );\n }\n this.clientId = opts.clientId;\n this.clientSecret = opts.clientSecret;\n this.baseUrl = (opts.baseUrl ?? DEFAULT_OAUTH_BASE_URL).replace(/\\/+$/, \"\");\n this.fetchImpl = f;\n this.timeoutMs = opts.timeoutMs ?? 30_000;\n }\n\n /**\n * Mint a fresh access token from a stored refresh token. The\n * returned `refreshToken` is identical to the input — refresh\n * tokens are persistent and non-rotating, so the field is\n * surfaced only for symmetry with the response shape.\n *\n * On `400 invalid_grant` the refresh token has been revoked (or its\n * grant / app was deleted); the SDK surfaces a\n * {@link MantyxOAuthError} and callers must drive a fresh sign-in.\n */\n async refresh(opts: RefreshOptions): Promise<OAuthToken> {\n if (!opts.refreshToken) {\n throw new MantyxError(\"`refreshToken` is required for MantyxOAuthClient.refresh\");\n }\n const body: Record<string, string> = {\n grant_type: \"refresh_token\",\n refresh_token: opts.refreshToken,\n };\n const scope = normalizeScope(opts.scope);\n if (scope !== undefined) body.scope = scope;\n return this.token(body);\n }\n\n /**\n * Revoke an access or refresh token (RFC 7009). The server always\n * returns 200, even for unknown tokens. Revoking a **refresh**\n * token kills the refresh and every live access token tied to its\n * grant; revoking an **access** token kills only that one.\n */\n async revoke(opts: RevokeOptions): Promise<void> {\n if (!opts.token) {\n throw new MantyxError(\"`token` is required for MantyxOAuthClient.revoke\");\n }\n await this.formPost(\"/api/oauth/revoke\", {\n token: opts.token,\n });\n }\n\n /**\n * Build a long-lived {@link TokenSource} that re-mints access\n * tokens from the supplied refresh token. Pass the returned source\n * to `new MantyxClient({ tokenSource, workspaceSlug, ... })`. The\n * source caches the access token in-memory and refreshes\n * proactively when the cached value is within `refreshSkewMs` of\n * `expiresAt`, or eagerly when `MantyxClient` reports a 401.\n *\n * Pass `initialToken` if the calling app already has a non-expired\n * access token in hand (e.g. straight out of the sign-in flow) to\n * avoid an extra round-trip on the first request.\n */\n refreshTokenSource(opts: RefreshTokenSourceOptions): TokenSource {\n if (!opts.refreshToken) {\n throw new MantyxError(\"`refreshToken` is required for MantyxOAuthClient.refreshTokenSource\");\n }\n const skew = opts.refreshSkewMs ?? DEFAULT_REFRESH_SKEW_MS;\n const cache: TokenCache = { token: opts.initialToken, inflight: null };\n const refreshToken = opts.refreshToken;\n return makeTokenSource(cache, skew, async () => {\n return this.refresh({ refreshToken, scope: opts.scope });\n });\n }\n\n // -------------------------------------------------------------- internals\n\n /**\n * POST `application/x-www-form-urlencoded` to `/api/oauth/token` and\n * decode the {@link OAuthToken} response. Always injects `client_id`\n * + `client_secret` from the constructor.\n */\n private async token(body: Record<string, string>): Promise<OAuthToken> {\n const res = await this.formPost(\"/api/oauth/token\", body);\n let parsed: Record<string, unknown> = {};\n try {\n parsed = (await res.json()) as Record<string, unknown>;\n } catch {\n throw new MantyxOAuthError(\n \"invalid_response\",\n \"Token endpoint returned a non-JSON response\",\n res.status,\n );\n }\n const accessToken = typeof parsed.access_token === \"string\" ? parsed.access_token : \"\";\n if (!accessToken) {\n throw new MantyxOAuthError(\n \"invalid_response\",\n \"Token endpoint response is missing `access_token`\",\n res.status,\n );\n }\n const expiresIn = typeof parsed.expires_in === \"number\" ? parsed.expires_in : 3600;\n return {\n accessToken,\n refreshToken: typeof parsed.refresh_token === \"string\" ? parsed.refresh_token : undefined,\n tokenType: typeof parsed.token_type === \"string\" ? parsed.token_type : \"Bearer\",\n expiresIn,\n expiresAt: Date.now() + expiresIn * 1000,\n scope: typeof parsed.scope === \"string\" ? parsed.scope : undefined,\n };\n }\n\n private async formPost(path: string, body: Record<string, string>): Promise<Response> {\n const url = `${this.baseUrl}${path}`;\n const params = new URLSearchParams({\n ...body,\n client_id: this.clientId,\n client_secret: this.clientSecret,\n });\n const ctrl = new AbortController();\n const t = setTimeout(() => ctrl.abort(), this.timeoutMs);\n let res: Response;\n try {\n res = await this.fetchImpl(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/x-www-form-urlencoded\",\n Accept: \"application/json\",\n },\n body: params.toString(),\n signal: ctrl.signal,\n });\n } catch (err) {\n if (ctrl.signal.aborted) {\n throw new MantyxNetworkError(`OAuth request timed out after ${this.timeoutMs}ms`);\n }\n throw new MantyxNetworkError(`OAuth network error: ${(err as Error).message}`, {\n cause: err,\n });\n } finally {\n clearTimeout(t);\n }\n if (!res.ok) {\n let errBody: { error?: unknown; error_description?: unknown } = {};\n try {\n errBody = (await res.json()) as typeof errBody;\n } catch {\n // ignore\n }\n const oauthError = typeof errBody.error === \"string\" ? errBody.error : `http_${res.status}`;\n const desc =\n typeof errBody.error_description === \"string\" ? errBody.error_description : undefined;\n throw new MantyxOAuthError(oauthError, desc, res.status);\n }\n return res;\n }\n}\n\n// -------------------------------------------------------------- internals\n\ninterface TokenCache {\n token: OAuthToken | undefined;\n inflight: Promise<OAuthToken> | null;\n}\n\n/**\n * Wrap a `mintToken` thunk into a single-flight {@link TokenSource}\n * with a cache + proactive-refresh skew. The cache is overwritten\n * atomically on every successful mint; the in-flight promise\n * collapses N concurrent expired-token observers into one mint call.\n *\n * Single-flight is an efficiency, not a correctness requirement —\n * `docs/oauth.md` explicitly allows multiple concurrent refreshes\n * against the same refresh token — but it keeps the token-endpoint\n * QPS reasonable when an SDK consumer fans out work in parallel.\n */\nfunction makeTokenSource(\n cache: TokenCache,\n skewMs: number,\n mint: () => Promise<OAuthToken>,\n): TokenSource {\n return async (reason: TokenRequestReason = \"initial\"): Promise<string> => {\n if (reason !== \"unauthorized\" && cache.token && !isExpiring(cache.token, skewMs)) {\n return cache.token.accessToken;\n }\n if (cache.inflight) {\n const t = await cache.inflight;\n if (reason === \"unauthorized\" && t === cache.token) {\n // If the inflight refresh was triggered by a benign cache miss\n // and we observed an unauthorized hint after it started, fall\n // through and mint again so the caller never gets a stale token.\n } else {\n return t.accessToken;\n }\n }\n cache.inflight = mint().then(\n (t) => {\n cache.token = t;\n return t;\n },\n (err: unknown) => {\n throw err;\n },\n );\n try {\n const t = await cache.inflight;\n return t.accessToken;\n } finally {\n cache.inflight = null;\n }\n };\n}\n\nfunction isExpiring(token: OAuthToken, skewMs: number): boolean {\n return token.expiresAt - Date.now() <= skewMs;\n}\n\nfunction normalizeScope(scope: string | readonly string[] | undefined): string | undefined {\n if (scope === undefined) return undefined;\n if (typeof scope === \"string\") {\n const trimmed = scope.trim();\n return trimmed.length > 0 ? trimmed : undefined;\n }\n const joined = scope.filter((s) => typeof s === \"string\" && s.length > 0).join(\" \");\n return joined.length > 0 ? joined : undefined;\n}\n","/**\n * Release version — synced from repo root VERSION (`npm run sync-version`).\n */\nexport const SDK_VERSION = \"0.13.0\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BO,IAAM,yBAAyB;AAG/B,IAAM,0BAA0B;AAchC,IAAM,mBAAN,cAA+B,YAAY;AAAA,EACvC;AAAA,EACA;AAAA,EAET,YACE,YACA,uBACA,QACA;AACA,UAAM,UAAU,wBACZ,SAAS,UAAU,KAAK,qBAAqB,KAC7C,SAAS,UAAU;AACvB,UAAM,SAAS,EAAE,MAAM,YAAY,OAAO,CAAC;AAC3C,SAAK,OAAO;AACZ,SAAK,aAAa;AAClB,SAAK,wBAAwB;AAAA,EAC/B;AACF;AA0GO,IAAM,oBAAN,MAAwB;AAAA,EACpB;AAAA,EACA;AAAA,EACQ;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YAAY,MAAgC;AAC1C,QAAI,CAAC,KAAK,UAAU;AAClB,YAAM,IAAI,YAAY,8CAA8C;AAAA,IACtE;AACA,QAAI,CAAC,KAAK,cAAc;AACtB,YAAM,IAAI,YAAY,kDAAkD;AAAA,IAC1E;AACA,UAAM,IAAI,KAAK,SAAS,WAAW;AACnC,QAAI,OAAO,MAAM,YAAY;AAC3B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACA,SAAK,WAAW,KAAK;AACrB,SAAK,eAAe,KAAK;AACzB,SAAK,WAAW,KAAK,WAAW,wBAAwB,QAAQ,QAAQ,EAAE;AAC1E,SAAK,YAAY;AACjB,SAAK,YAAY,KAAK,aAAa;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA;A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C;AACA,QAAM,SAAS,MAAM,OAAO,CAAC,MAAM,OAAO,MAAM,YAAY,EAAE,SAAS,CAAC,EAAE,KAAK,GAAG;AAClF,SAAO,OAAO,SAAS,IAAI,SAAS;AACtC;;;ACpZO,IAAM,cAAc;","names":[]}
|
|
@@ -361,8 +361,12 @@ The agent spec is the body shape used by `POST /agent-runs` and `POST
|
|
|
361
361
|
"hive_consult_ontology": { "maxCalls": 4 },
|
|
362
362
|
"scary_tool": { "maxCalls": 0 },
|
|
363
363
|
},
|
|
364
|
+
"supervisor": {
|
|
365
|
+
// optional, see §4.8 — platform LLM judge; pass false to disable
|
|
366
|
+
"interval": 5,
|
|
367
|
+
},
|
|
364
368
|
"metadata": {
|
|
365
|
-
// optional, see §4.
|
|
369
|
+
// optional, see §4.9
|
|
366
370
|
"customer": "acme",
|
|
367
371
|
"env": "prod",
|
|
368
372
|
},
|
|
@@ -844,7 +848,64 @@ during normal multi-entity reads. The loop-detection guard catches the
|
|
|
844
848
|
pathological "same `(name, args)` batch over and over" case for that
|
|
845
849
|
family without needing per-tool caps.
|
|
846
850
|
|
|
847
|
-
### 4.8 `
|
|
851
|
+
### 4.8 `supervisor` (run judge)
|
|
852
|
+
|
|
853
|
+
`supervisor` controls the optional **run supervisor** — an LLM judge that
|
|
854
|
+
periodically reviews the agent's transcript (reasoning, tool calls, tool
|
|
855
|
+
results, visible text) and may steer the run:
|
|
856
|
+
|
|
857
|
+
- **`on_track`** — no-op; the run continues.
|
|
858
|
+
- **`redirect`** — a steering user message is injected; tools stay available.
|
|
859
|
+
- **`finalize`** — the next turn is forced tools-disabled so the run lands a
|
|
860
|
+
clean final answer.
|
|
861
|
+
|
|
862
|
+
Reviews fire every **`interval` LLM calls** (`completeTurn` invocations) at
|
|
863
|
+
the bottom of tool-emitting rounds. Default interval is **5** when enabled.
|
|
864
|
+
|
|
865
|
+
```jsonc
|
|
866
|
+
"supervisor": {
|
|
867
|
+
"interval": 5 // optional — LLM calls between reviews; default 5
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// or:
|
|
871
|
+
"supervisor": false // explicitly disable the platform judge for this run
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
| Field | Type | Required | Notes |
|
|
875
|
+
| ----------------- | --------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
876
|
+
| `interval` | integer ≥ 1 | no | Defaults to **5** when the supervisor is enabled and `interval` is omitted. Capped at **100** server-side. |
|
|
877
|
+
| (literal `false`) | `false` | no | Disables the run supervisor for this run. `loopDetection` and `toolBudgets` still apply. |
|
|
878
|
+
|
|
879
|
+
**Defaults.** When `supervisor` is **omitted**, MANTYX enables the platform
|
|
880
|
+
LLM judge on ephemeral runs. Pass `"supervisor": false` to opt out.
|
|
881
|
+
|
|
882
|
+
**SDK-only usage.** When calling `@mantyx/ts-sdk` directly (not via
|
|
883
|
+
`POST /agent-runs`), the supervisor is **off unless explicitly configured**:
|
|
884
|
+
pass `supervisor: { review, interval? }` on `RunAgentOptions` to enable a
|
|
885
|
+
caller-supplied judge, or pass `supervisor: false` (or omit the field) to
|
|
886
|
+
keep it disabled. The wire field above controls the **platform-hosted** judge
|
|
887
|
+
on API/ephemeral runs only.
|
|
888
|
+
|
|
889
|
+
Validation (server-side, `400 invalid_request` on violation):
|
|
890
|
+
|
|
891
|
+
| Constraint | Limit |
|
|
892
|
+
| ----------------- | ----- |
|
|
893
|
+
| `interval` upper bound | `100` |
|
|
894
|
+
|
|
895
|
+
**Inheritance for sessions.**
|
|
896
|
+
|
|
897
|
+
- `POST /agent-sessions { supervisor }` — sets the session-default, applied
|
|
898
|
+
to every subsequent message run.
|
|
899
|
+
- `POST /agent-sessions/:id/messages { supervisor }` — optional per-message
|
|
900
|
+
override; applies to that one run only and does not mutate the session's
|
|
901
|
+
stored value.
|
|
902
|
+
|
|
903
|
+
**Observability.** Each review emits a SSE `supervisor` event (see §7) —
|
|
904
|
+
including `on_track` checks — so SDK clients can render supervisor activity.
|
|
905
|
+
When `action` is `redirect` or `finalize`, the pipeline has already applied
|
|
906
|
+
the verdict by the time the event arrives.
|
|
907
|
+
|
|
908
|
+
### 4.9 `metadata` (developer-supplied KV for filtering)
|
|
848
909
|
|
|
849
910
|
`metadata` is a flat string→string KV that is **persisted alongside the run /
|
|
850
911
|
session** and surfaced in the MANTYX dashboard. Use it to tag runs with your
|
|
@@ -871,9 +932,11 @@ For session-scoped runs the inheritance rules are:
|
|
|
871
932
|
metadata do not retroactively rewrite past runs.
|
|
872
933
|
|
|
873
934
|
Metadata is returned on every read: `GET /agent-runs/:id`,
|
|
874
|
-
`GET /agent-sessions/:id`,
|
|
875
|
-
the admin list endpoints uses repeated
|
|
876
|
-
AND-combined
|
|
935
|
+
`GET /agent-sessions/:id`, the public session list (`GET /agent-sessions`,
|
|
936
|
+
see §6.1), and the admin list/detail endpoints. Filtering uses repeated
|
|
937
|
+
`?metadata=key:value` query params, AND-combined — supported on both the
|
|
938
|
+
public `GET /agent-sessions` endpoint and the admin list endpoints; see
|
|
939
|
+
`docs/agent-runs.md` §"Web UI" for the admin surface.
|
|
877
940
|
|
|
878
941
|
## 5. One-shot runs
|
|
879
942
|
|
|
@@ -905,7 +968,9 @@ runs or attributing spend after the SSE stream was already consumed.
|
|
|
905
968
|
|
|
906
969
|
```
|
|
907
970
|
POST /api/v1/workspaces/{slug}/agent-sessions
|
|
971
|
+
GET /api/v1/workspaces/{slug}/agent-sessions
|
|
908
972
|
GET /api/v1/workspaces/{slug}/agent-sessions/{sessionId}
|
|
973
|
+
GET /api/v1/workspaces/{slug}/agent-sessions/{sessionId}/events
|
|
909
974
|
POST /api/v1/workspaces/{slug}/agent-sessions/{sessionId}/messages
|
|
910
975
|
DELETE /api/v1/workspaces/{slug}/agent-sessions/{sessionId}
|
|
911
976
|
```
|
|
@@ -918,6 +983,87 @@ Returns:
|
|
|
918
983
|
{ "sessionId": "ses_abc" }
|
|
919
984
|
```
|
|
920
985
|
|
|
986
|
+
### 6.1 Listing sessions (`GET /agent-sessions`)
|
|
987
|
+
|
|
988
|
+
Lists the workspace's sessions, most-recently-used first. Supports paging
|
|
989
|
+
and metadata filtering so callers can find earlier sessions by the
|
|
990
|
+
application identifiers they attached at create time (see §4.9).
|
|
991
|
+
|
|
992
|
+
| Query param | Notes |
|
|
993
|
+
| ----------- | ------------------------------------------------------------------------------------------- |
|
|
994
|
+
| `metadata` | Repeatable `key:value` filter; AND-combined. Malformed entries return `400 invalid_request`. |
|
|
995
|
+
| `status` | Optional exact match (`active` / `ended`). |
|
|
996
|
+
| `limit` | Default `50`, max `200`. |
|
|
997
|
+
| `offset` | Default `0`. |
|
|
998
|
+
|
|
999
|
+
```http
|
|
1000
|
+
GET /api/v1/workspaces/acme/agent-sessions?metadata=customer:acme&metadata=env:prod&limit=20
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
```jsonc
|
|
1004
|
+
{
|
|
1005
|
+
"total": 1,
|
|
1006
|
+
"limit": 20,
|
|
1007
|
+
"offset": 0,
|
|
1008
|
+
"sessions": [
|
|
1009
|
+
{
|
|
1010
|
+
"sessionId": "ses_abc",
|
|
1011
|
+
"creationDate": "2026-06-08T17:00:00.000Z", // ISO 8601 — when created
|
|
1012
|
+
"lastInteractionDate": "2026-06-08T17:42:10.000Z", // ISO 8601 — last message run
|
|
1013
|
+
"summary": "How do I reset my password?", // derived from the first user prompt
|
|
1014
|
+
"metadata": { "customer": "acme", "env": "prod" },
|
|
1015
|
+
"status": "active"
|
|
1016
|
+
}
|
|
1017
|
+
]
|
|
1018
|
+
}
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
`summary` is a best-effort label derived from the conversation's first user
|
|
1022
|
+
prompt (sessions have no title); when the opener is very short the following
|
|
1023
|
+
messages are appended until it is informative.
|
|
1024
|
+
|
|
1025
|
+
### 6.2 Replaying a session as events (`GET /agent-sessions/{sessionId}/events`)
|
|
1026
|
+
|
|
1027
|
+
Returns the session's stored conversation as realtime-style event frames so a
|
|
1028
|
+
UI can restore the thread through the same handler it uses for the live SSE
|
|
1029
|
+
stream (§7). Unknown session ids return `404 not_found`.
|
|
1030
|
+
|
|
1031
|
+
| Query param | Notes |
|
|
1032
|
+
| -------------- | ---------------------------------------------------------------------------------------------- |
|
|
1033
|
+
| `full` | `1` / `true` returns every message (the default when no paging param is given). |
|
|
1034
|
+
| `lastMessages` | Return only the last `N` messages. Ignored when `full` is set. |
|
|
1035
|
+
|
|
1036
|
+
```http
|
|
1037
|
+
GET /api/v1/workspaces/acme/agent-sessions/ses_abc/events?lastMessages=2
|
|
1038
|
+
```
|
|
1039
|
+
|
|
1040
|
+
```jsonc
|
|
1041
|
+
{
|
|
1042
|
+
"sessionId": "ses_abc",
|
|
1043
|
+
"total": 4, // total messages in the session (not the page size)
|
|
1044
|
+
"events": [
|
|
1045
|
+
{ "seq": 3, "type": "user_message", "text": "three" },
|
|
1046
|
+
{ "seq": 4, "type": "assistant_message", "text": "four" }
|
|
1047
|
+
]
|
|
1048
|
+
}
|
|
1049
|
+
```
|
|
1050
|
+
|
|
1051
|
+
Frames use the flattened wire shape clients already consume from SSE
|
|
1052
|
+
(`{ seq, type, ...payload }`). `seq` is the message's position in the full
|
|
1053
|
+
conversation (stable across paging). Reconstructed turns map as:
|
|
1054
|
+
|
|
1055
|
+
| Stored role | Frame `type` |
|
|
1056
|
+
| ----------- | ------------------- |
|
|
1057
|
+
| `user` | `user_message` |
|
|
1058
|
+
| `assistant` | `assistant_message` |
|
|
1059
|
+
| other | `message` (carries a `role` field) |
|
|
1060
|
+
|
|
1061
|
+
`user_message` is a **replay/restore-only** frame — the live SSE stream never
|
|
1062
|
+
emits it (the client already knows the prompt it sent), but history
|
|
1063
|
+
restoration needs it to rebuild the user side of the conversation. This first
|
|
1064
|
+
cut reconstructs from the durable transcript (`role`/`content` only); tool
|
|
1065
|
+
calls are not replayed here.
|
|
1066
|
+
|
|
921
1067
|
`POST /agent-sessions/{id}/messages` queues a new run scoped to the session
|
|
922
1068
|
and returns `{ runId, streamUrl }` just like a one-shot run. Body:
|
|
923
1069
|
|
|
@@ -962,9 +1108,6 @@ data: <utf-8 JSON>
|
|
|
962
1108
|
`<type>` and `<data>` shapes:
|
|
963
1109
|
|
|
964
1110
|
```jsonc
|
|
965
|
-
// running message
|
|
966
|
-
{ "seq": 1, "type": "started", "data": {} }
|
|
967
|
-
|
|
968
1111
|
// streamed assistant tokens (zero or more per turn)
|
|
969
1112
|
{ "seq": 2, "type": "assistant_delta", "data": { "text": "Hello" } }
|
|
970
1113
|
|
|
@@ -1001,6 +1144,11 @@ data: <utf-8 JSON>
|
|
|
1001
1144
|
// is observability so SDK clients can render "memory budget exhausted" status notes.
|
|
1002
1145
|
{ "seq": 7, "type": "tool_budget_exceeded", "data": { "tool": "recall", "maxCalls": 4, "callIndex": 5 } }
|
|
1003
1146
|
|
|
1147
|
+
// run-supervisor check (see §4.8). Fired on every review — on_track included.
|
|
1148
|
+
{ "seq": 7, "type": "supervisor", "data": { "action": "on_track", "reason": "Agent is making progress.", "llmCalls": 5 } }
|
|
1149
|
+
{ "seq": 8, "type": "supervisor", "data": { "action": "redirect", "reason": "Stuck re-querying.", "redirect": "Answer from the data you already have.", "llmCalls": 10 } }
|
|
1150
|
+
{ "seq": 9, "type": "supervisor", "data": { "action": "finalize", "reason": "Enough to answer.", "llmCalls": 15 } }
|
|
1151
|
+
|
|
1004
1152
|
// terminal event
|
|
1005
1153
|
// Every terminal `result` event also carries `tokens`, `turns`, and `model`
|
|
1006
1154
|
// for cost attribution and dashboards — see §7.1. Older platforms (pre-
|
|
@@ -1241,18 +1389,18 @@ A reference SDK should:
|
|
|
1241
1389
|
- Treat `thinking_delta` events as opt-in callback fodder; many UIs hide
|
|
1242
1390
|
them by default. Their presence depends on `reasoningLevel > 0` and
|
|
1243
1391
|
on the active model exposing thought parts.
|
|
1244
|
-
- Accept `loopDetection` and `
|
|
1245
|
-
them through unchanged (see §4.6 / §4.7).
|
|
1246
|
-
omitting them keeps MANTYX's runtime defaults; passing
|
|
1247
|
-
`loopDetection: false`
|
|
1248
|
-
defaults; passing entries layers caller
|
|
1249
|
-
defaults.
|
|
1250
|
-
- Treat `loop_detected` and `
|
|
1251
|
-
observability-only — the server already substituted
|
|
1252
|
-
tool-results / steering nudges
|
|
1253
|
-
the event to the caller (status banner,
|
|
1254
|
-
**not** abort the run on these events; the run
|
|
1255
|
-
`result` / `error` / `cancelled` as usual.
|
|
1392
|
+
- Accept `loopDetection`, `toolBudgets`, and `supervisor` from the caller
|
|
1393
|
+
and pass them through unchanged (see §4.6 / §4.7 / §4.8). All three are
|
|
1394
|
+
_additive_: omitting them keeps MANTYX's runtime defaults; passing
|
|
1395
|
+
`loopDetection: false` or `supervisor: false` opts out; passing
|
|
1396
|
+
`toolBudgets: {}` clears the defaults; passing entries layers caller
|
|
1397
|
+
overrides on top of the defaults.
|
|
1398
|
+
- Treat `loop_detected`, `tool_budget_exceeded`, and `supervisor` SSE
|
|
1399
|
+
events as observability-only — the server already substituted synthetic
|
|
1400
|
+
tool-results / steering nudges / supervisor verdicts where applicable, so
|
|
1401
|
+
the SDK's job is just to surface the event to the caller (status banner,
|
|
1402
|
+
log line, telemetry). Do **not** abort the run on these events; the run
|
|
1403
|
+
continues through `result` / `error` / `cancelled` as usual.
|
|
1256
1404
|
- On terminal `result`, resolve the call. On `error` subtype, throw.
|
|
1257
1405
|
4. Re-emit assistant deltas/events as a stream/iterator for callers who care
|
|
1258
1406
|
about live output.
|
|
@@ -1269,4 +1417,4 @@ A reference SDK should:
|
|
|
1269
1417
|
|
|
1270
1418
|
The npm package [`@mantyx/sdk`](https://www.npmjs.com/package/@mantyx/sdk) and the Go module
|
|
1271
1419
|
[`github.com/mantyx/mantyx-go-sdk`](https://github.com/mantyx/mantyx-go-sdk) are reference implementations of this protocol
|
|
1272
|
-
(maintained in the official **mantyx-sdk** repositories).
|
|
1420
|
+
(maintained in the official **mantyx-sdk** repositories).
|
package/docs/wire-protocol.md
CHANGED
|
@@ -20,7 +20,7 @@ looks like for `mcp_local`, you're in the right place.
|
|
|
20
20
|
> **scopes** (`runs:read`, `runs:write`, `sessions:read`, `sessions:write`,
|
|
21
21
|
> `models:read`, `mantyx.identity:read`); see §2 of
|
|
22
22
|
> `agent-runs-protocol.md` for the per-endpoint scope table and
|
|
23
|
-
>
|
|
23
|
+
> `docs/oauth.md` for the registration / Authorization Code
|
|
24
24
|
>
|
|
25
25
|
> - PKCE flow.
|
|
26
26
|
|
|
@@ -132,6 +132,11 @@ short-circuit, etc.) see `agent-runs-protocol.md` §4.
|
|
|
132
132
|
"recall": { "maxCalls": 4 },
|
|
133
133
|
"hive_consult_ontology": { "maxCalls": 4 },
|
|
134
134
|
},
|
|
135
|
+
"supervisor": {
|
|
136
|
+
// optional; see §8.4 — platform LLM judge on ephemeral runs
|
|
137
|
+
"interval": 5,
|
|
138
|
+
"modelId": "platform:demo",
|
|
139
|
+
},
|
|
135
140
|
"metadata": { "customer": "acme" }, // optional, free-form k/v
|
|
136
141
|
}
|
|
137
142
|
```
|
|
@@ -140,9 +145,9 @@ short-circuit, etc.) see `agent-runs-protocol.md` §4.
|
|
|
140
145
|
|
|
141
146
|
Same body shape, posted to `POST /agent-sessions/:id/messages`. The session
|
|
142
147
|
keeps the conversation history; per-message `tools`, `reasoningLevel`,
|
|
143
|
-
`outputSchema`, `loopDetection`, and `
|
|
144
|
-
defaults for that single run only — the next run falls back to
|
|
145
|
-
the session was created with.
|
|
148
|
+
`outputSchema`, `loopDetection`, `toolBudgets`, and `supervisor` _replace_
|
|
149
|
+
the session's defaults for that single run only — the next run falls back to
|
|
150
|
+
whatever the session was created with.
|
|
146
151
|
|
|
147
152
|
---
|
|
148
153
|
|
|
@@ -391,6 +396,7 @@ The vocabulary (`EphemeralEventType` in `bus.ts`):
|
|
|
391
396
|
| `local_tool_result_in` | M → SDK | Per client-resolved tool call | Informational mirror of the tool-result the SDK just posted, persisted for observability. Re-emitted to late subscribers so they can replay the conversation. |
|
|
392
397
|
| `loop_detected` | M → SDK | 0–2× per run (soft nudge + optional hard cutoff) | Observability for the loop-detection guard (see §8). The server already substituted the synthetic skip + steering nudge — SDK clients render a status note (`looping — nudged` / `looping — gave up`) and otherwise leave the run alone. |
|
|
393
398
|
| `tool_budget_exceeded` | M → SDK | Per intercepted tool call | Observability for per-tool call budgets (see §8). The synthetic `tool_result` carrying the "budget exceeded — pivot or finalize" body lands on the normal tool-result channel; this event is purely so SDK clients can surface a UI banner. |
|
|
399
|
+
| `supervisor` | M → SDK | 0–N× per run (every `interval` LLM calls) | Run-supervisor check (see §4.7 / §8.4). Fired on **every** review — including `on_track` — so SDK clients can render supervisor activity. When the judge steers the run (`redirect` / `finalize`), the pipeline has already injected the steering message or forced a tools-disabled finalize turn. |
|
|
394
400
|
| `assistant_message` | M → SDK | 1× per turn | Final assistant message for the turn (concatenated, persistence-ready). |
|
|
395
401
|
| `result` | M → SDK | 1× terminal | Successful completion. Carries the final assistant text and run summary. |
|
|
396
402
|
| `error` | M → SDK | 1× terminal | Failure. Carries `error` (message), `code` / `errorClass` (category), `finishReason`, and an optional `partialText` salvage payload. See §4.7. |
|
|
@@ -640,7 +646,46 @@ re-parsing tool-result bodies.
|
|
|
640
646
|
|
|
641
647
|
See §8 for the wire-spec field that defines budgets.
|
|
642
648
|
|
|
643
|
-
### 4.7
|
|
649
|
+
### 4.7 `supervisor`
|
|
650
|
+
|
|
651
|
+
```jsonc
|
|
652
|
+
// on_track — the judge reviewed the run and decided not to intervene
|
|
653
|
+
{ "seq": 15, "type": "supervisor",
|
|
654
|
+
"data": { "action": "on_track", "reason": "Agent is gathering context via search before answering.", "llmCalls": 5 } }
|
|
655
|
+
|
|
656
|
+
// redirect — a steering user message was injected; the agent keeps its tools
|
|
657
|
+
{ "seq": 20, "type": "supervisor",
|
|
658
|
+
"data": { "action": "redirect", "reason": "Repeating the same search with identical args.", "redirect": "Stop re-querying; synthesize an answer from the results you already have.", "llmCalls": 10 } }
|
|
659
|
+
|
|
660
|
+
// finalize — the run was forced to wrap up on a tools-disabled turn
|
|
661
|
+
{ "seq": 25, "type": "supervisor",
|
|
662
|
+
"data": { "action": "finalize", "reason": "Enough evidence to answer; further tool use is unlikely to help.", "llmCalls": 15 } }
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
| Field | Type | Notes |
|
|
666
|
+
| ---------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
667
|
+
| `action` | string | One of `"on_track"`, `"redirect"`, `"finalize"`. |
|
|
668
|
+
| `reason` | string | One- or two-sentence explanation from the judge. |
|
|
669
|
+
| `redirect` | string | Present when `action === "redirect"`: the steering message injected into the conversation (same text the agent sees as a user message). Omitted for `on_track` / `finalize`. |
|
|
670
|
+
| `llmCalls` | integer | Number of LLM calls (`completeTurn` invocations) completed when this review fired. Matches the pipeline's `modelInvocations` counter at the check boundary. |
|
|
671
|
+
| `model` | object | Optional. Resolved judge model (`{ id, provider, vendorModelId }`) — same shape as terminal `result.model` (§4.7.1). Present on platform-hosted runs for cost attribution and debug UIs. |
|
|
672
|
+
|
|
673
|
+
Observability for the run-supervisor guard (see §8.4). The event fires on
|
|
674
|
+
**every** check, not only when the judge intervenes — `on_track` reviews are
|
|
675
|
+
included so SDK clients can show "supervisor reviewed" activity without
|
|
676
|
+
inferring it from missing events.
|
|
677
|
+
|
|
678
|
+
When `action` is `redirect` or `finalize`, the pipeline has already applied
|
|
679
|
+
the verdict by the time this event arrives: a steering user message was
|
|
680
|
+
appended (`redirect`) or the next turn was forced tools-disabled
|
|
681
|
+
(`finalize`). SDK clients should render a status note and **not** try to
|
|
682
|
+
steer the run themselves.
|
|
683
|
+
|
|
684
|
+
Pass `"supervisor": false` in the spec (§8.4) to disable the platform judge
|
|
685
|
+
for a run. Omission keeps the runtime default (supervisor **enabled** on
|
|
686
|
+
ephemeral runs).
|
|
687
|
+
|
|
688
|
+
### 4.8 Terminal events
|
|
644
689
|
|
|
645
690
|
```jsonc
|
|
646
691
|
// Every terminal `result` and `error` event also carries `tokens`, `turns`,
|
|
@@ -692,7 +737,7 @@ SDK can re-fetch via `GET /agent-runs/:runId` will have:
|
|
|
692
737
|
| `error` | Same string as `data.error`. |
|
|
693
738
|
| `failureReason` | `{ "errorClass": "truncation", "finishReason": "max_tokens" }` (JSON object, future-proof for additional triage fields). |
|
|
694
739
|
|
|
695
|
-
### 4.
|
|
740
|
+
### 4.8.1 Cost-attribution fields (`tokens`, `turns`, `model`)
|
|
696
741
|
|
|
697
742
|
Every terminal `result` and `error` event carries three additional
|
|
698
743
|
fields so callers can drive cost dashboards, per-turn budgets, and
|
|
@@ -1030,20 +1075,75 @@ banners without re-parsing tool-result bodies:
|
|
|
1030
1075
|
- `loop_detected` — fired on the soft nudge and again on the hard cutoff
|
|
1031
1076
|
if reached. See §4.5.
|
|
1032
1077
|
- `tool_budget_exceeded` — fired each time a call is intercepted. See §4.6.
|
|
1078
|
+
- `supervisor` — fired on every run-supervisor review (`on_track`,
|
|
1079
|
+
`redirect`, or `finalize`). See §4.7.
|
|
1080
|
+
|
|
1081
|
+
Both guard events (`loop_detected`, `tool_budget_exceeded`) are
|
|
1082
|
+
observability-only: the server has already substituted the synthetic
|
|
1083
|
+
tool-result / steering nudge by the time the SDK sees the event. The
|
|
1084
|
+
`supervisor` event is also observability-only when `action` is
|
|
1085
|
+
`redirect` / `finalize` — the pipeline already applied the verdict. The
|
|
1086
|
+
run continues to its terminal `result` / `error` / `cancelled` as usual.
|
|
1087
|
+
|
|
1088
|
+
### 8.4 `supervisor` (run judge)
|
|
1089
|
+
|
|
1090
|
+
An optional LLM **run supervisor** periodically reviews the agent's
|
|
1091
|
+
transcript (reasoning, tool calls, tool results, visible text) and may
|
|
1092
|
+
steer the run:
|
|
1093
|
+
|
|
1094
|
+
| Verdict | Server action |
|
|
1095
|
+
| ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
1096
|
+
| `on_track` | No-op — the run continues unchanged. |
|
|
1097
|
+
| `redirect` | A steering **user message** is injected; tools stay available on the next turn. |
|
|
1098
|
+
| `finalize` | The next turn is forced **tools-disabled** so the run lands a clean final answer (optionally prefaced by the supervisor's message). |
|
|
1099
|
+
|
|
1100
|
+
Reviews fire every **`interval` LLM calls** (`completeTurn` invocations),
|
|
1101
|
+
measured at the bottom of tool-emitting rounds. Default interval is **5**
|
|
1102
|
+
when the field is omitted.
|
|
1103
|
+
|
|
1104
|
+
```jsonc
|
|
1105
|
+
"supervisor": {
|
|
1106
|
+
"interval": 5, // optional — LLM calls between reviews; default 5
|
|
1107
|
+
"modelId": "platform:demo" // optional — judge model; see resolution below
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
// or:
|
|
1111
|
+
"supervisor": false // explicitly disable the platform judge for this run
|
|
1112
|
+
```
|
|
1113
|
+
|
|
1114
|
+
| Field | Type | Notes |
|
|
1115
|
+
| ---------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
|
1116
|
+
| `interval` | integer ≥ 1 | Optional. Default **5** when omitted. Capped at **100** server-side. |
|
|
1117
|
+
| `modelId` | string | Optional. Same selector grammar as top-level `modelId` (§2). When omitted, the platform resolves the judge model from the workspace default supervisor model, then the workspace default model. |
|
|
1118
|
+
| (literal `false`) | `false` | Disables the run supervisor for this run. Loop detection and tool budgets still apply. |
|
|
1119
|
+
|
|
1120
|
+
**Model resolution.** When the supervisor is enabled, the judge model is chosen in order:
|
|
1121
|
+
|
|
1122
|
+
1. `supervisor.modelId` on this run (if set)
|
|
1123
|
+
2. Workspace `defaultSupervisorModelId` (Settings → Workspace → General)
|
|
1124
|
+
3. Workspace default model (same as omitting top-level `modelId`)
|
|
1125
|
+
|
|
1126
|
+
**Defaults.** When `supervisor` is **omitted**, MANTYX enables the platform
|
|
1127
|
+
LLM judge on ephemeral runs (web chat enables it separately via the chat
|
|
1128
|
+
runner). Pass `"supervisor": false` to opt out.
|
|
1129
|
+
|
|
1130
|
+
**SDK-only runs.** When a caller uses `@mantyx/ts-sdk` directly (not via
|
|
1131
|
+
`POST /agent-runs`), the supervisor is **off unless explicitly configured**:
|
|
1132
|
+
pass a `RunAgentSupervisor` object with a `review` callback to enable it, or
|
|
1133
|
+
pass `supervisor: false` (or omit the field) to keep it disabled. The wire
|
|
1134
|
+
field above controls the **platform-hosted** judge on ephemeral API runs only.
|
|
1033
1135
|
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
event. The run continues to its terminal `result` / `error` / `cancelled`
|
|
1037
|
-
as usual.
|
|
1136
|
+
Each review emits a SSE `supervisor` event (§4.7). Supervisor LLM usage is
|
|
1137
|
+
recorded under the `supervisor` usage surface for cost attribution.
|
|
1038
1138
|
|
|
1039
|
-
### 8.
|
|
1139
|
+
### 8.5 Session inheritance
|
|
1040
1140
|
|
|
1041
|
-
Like `reasoningLevel` and `outputSchema`,
|
|
1141
|
+
Like `reasoningLevel` and `outputSchema`, the run-guard fields support
|
|
1042
1142
|
session-default + per-message override:
|
|
1043
1143
|
|
|
1044
|
-
- `POST /agent-sessions { loopDetection, toolBudgets }` — sets the
|
|
1144
|
+
- `POST /agent-sessions { loopDetection, toolBudgets, supervisor }` — sets the
|
|
1045
1145
|
session-default applied to every subsequent message run.
|
|
1046
|
-
- `POST /agent-sessions/:id/messages { loopDetection, toolBudgets }` —
|
|
1146
|
+
- `POST /agent-sessions/:id/messages { loopDetection, toolBudgets, supervisor }` —
|
|
1047
1147
|
optional per-message override. Applies to that one run only and does
|
|
1048
1148
|
not mutate the session's stored value.
|
|
1049
1149
|
|
|
@@ -1215,17 +1315,17 @@ A reference SDK should:
|
|
|
1215
1315
|
source-of-truth schema (Zod / Pydantic / etc.) — the server enforces
|
|
1216
1316
|
JSON shape via the provider, but transient model errors can still
|
|
1217
1317
|
produce strings that fail to parse in rare cases.
|
|
1218
|
-
- [ ] Accept `loopDetection` and `
|
|
1219
|
-
them through unchanged (see §8).
|
|
1220
|
-
them keeps the runtime defaults; passing `loopDetection: false`
|
|
1221
|
-
out; passing `toolBudgets: {}` clears the
|
|
1222
|
-
layers caller overrides on top of the defaults.
|
|
1223
|
-
to vendor-specific knobs.
|
|
1224
|
-
- [ ] Treat `loop_detected` and `
|
|
1225
|
-
observability-only (see §4.5 / §4.6). Surface them as
|
|
1226
|
-
/ log lines / telemetry — the server already substituted
|
|
1227
|
-
synthetic tool-results / steering nudges, so the
|
|
1228
|
-
consuming the stream until the terminal event lands.
|
|
1318
|
+
- [ ] Accept `loopDetection`, `toolBudgets`, and `supervisor` from the caller
|
|
1319
|
+
and pass them through unchanged (see §8). All three are _additive_ —
|
|
1320
|
+
omitting them keeps the runtime defaults; passing `loopDetection: false`
|
|
1321
|
+
or `supervisor: false` opts out; passing `toolBudgets: {}` clears the
|
|
1322
|
+
defaults; passing entries layers caller overrides on top of the defaults.
|
|
1323
|
+
Do **not** translate to vendor-specific knobs.
|
|
1324
|
+
- [ ] Treat `loop_detected`, `tool_budget_exceeded`, and `supervisor` SSE
|
|
1325
|
+
events as observability-only (see §4.5 / §4.6 / §4.7). Surface them as
|
|
1326
|
+
status notes / log lines / telemetry — the server already substituted
|
|
1327
|
+
synthetic tool-results / steering nudges / supervisor verdicts, so the
|
|
1328
|
+
SDK should keep consuming the stream until the terminal event lands.
|
|
1229
1329
|
- [ ] Maintain three local-callback registries (or one tagged-union
|
|
1230
1330
|
registry), keyed by `name`: - generic local tools (`kind: "local"`), - local A2A peers (`kind: "a2a_local"`, indexed by some Agent Card
|
|
1231
1331
|
field — typically `agentCard.url`), - local MCP servers (`kind: "mcp_local"`, indexed by the SDK-side
|
|
@@ -1262,4 +1362,4 @@ A reference SDK should:
|
|
|
1262
1362
|
- [A2A spec](https://google.github.io/A2A/specification/) — canonical
|
|
1263
1363
|
Agent Card schema.
|
|
1264
1364
|
- [MCP spec](https://spec.modelcontextprotocol.io/) — canonical `Tool` and
|
|
1265
|
-
`Implementation` shapes.
|
|
1365
|
+
`Implementation` shapes.
|