|
| 1 | +import type { |
| 2 | + OcxProviderContinuationOwner, |
| 3 | + OcxReasoningReplayIdentity, |
| 4 | +} from "../types"; |
| 5 | + |
| 6 | +const bounded = (value: unknown, max: number): value is string => |
| 7 | + typeof value === "string" && value.length > 0 && value.length <= max; |
| 8 | + |
| 9 | +/** Validate proxy-authored continuation ownership before trusting persisted state. */ |
| 10 | +export function isValidProviderContinuationOwner( |
| 11 | + value: unknown, |
| 12 | +): value is OcxProviderContinuationOwner { |
| 13 | + if (!value || typeof value !== "object" || Array.isArray(value)) return false; |
| 14 | + const owner = value as Record<string, unknown>; |
| 15 | + return owner.version === 1 |
| 16 | + && bounded(owner.providerName, 256) |
| 17 | + && typeof owner.providerDestinationIdentity === "string" |
| 18 | + && /^destination:[0-9a-f]{64}$/.test(owner.providerDestinationIdentity) |
| 19 | + && bounded(owner.adapterName, 128) |
| 20 | + && bounded(owner.modelId, 512) |
| 21 | + && typeof owner.credentialIdentity === "string" |
| 22 | + && /^(key|oauth|codex):[0-9a-f]{64}$/.test(owner.credentialIdentity); |
| 23 | +} |
| 24 | + |
| 25 | +/** Reuse the exact process-local route identity already established for reasoning replay. */ |
| 26 | +export function providerContinuationOwnerFromReplayIdentity( |
| 27 | + identity: Readonly<OcxReasoningReplayIdentity> | undefined, |
| 28 | +): OcxProviderContinuationOwner | undefined { |
| 29 | + if (!identity) return undefined; |
| 30 | + const owner: OcxProviderContinuationOwner = { |
| 31 | + version: 1, |
| 32 | + providerName: identity.providerName, |
| 33 | + providerDestinationIdentity: identity.providerDestinationIdentity, |
| 34 | + adapterName: identity.adapterName, |
| 35 | + modelId: identity.modelId, |
| 36 | + credentialIdentity: identity.credentialIdentity, |
| 37 | + }; |
| 38 | + return isValidProviderContinuationOwner(owner) ? owner : undefined; |
| 39 | +} |
| 40 | + |
| 41 | +export function sameProviderContinuationOwner( |
| 42 | + left: OcxProviderContinuationOwner, |
| 43 | + right: OcxProviderContinuationOwner, |
| 44 | +): boolean { |
| 45 | + return left.version === right.version |
| 46 | + && left.providerName === right.providerName |
| 47 | + && left.providerDestinationIdentity === right.providerDestinationIdentity |
| 48 | + && left.adapterName === right.adapterName |
| 49 | + && left.modelId === right.modelId |
| 50 | + && left.credentialIdentity === right.credentialIdentity; |
| 51 | +} |
| 52 | + |
| 53 | +/** Cursor hashes this non-secret namespace with the client thread id. */ |
| 54 | +export function providerContinuationRouteScope(owner: OcxProviderContinuationOwner): string { |
| 55 | + return JSON.stringify([ |
| 56 | + "provider-continuation-v1", |
| 57 | + owner.providerName, |
| 58 | + owner.providerDestinationIdentity, |
| 59 | + owner.adapterName, |
| 60 | + owner.modelId, |
| 61 | + owner.credentialIdentity, |
| 62 | + ]); |
| 63 | +} |
0 commit comments