Files
deepseek-harness/packages/subagent/tool-subagent/src/model-selection-state.ts
T

61 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Durable per-session state for the user-controlled model-selection opt-in. */
import { z as zod } from 'zod'
import type { Session } from '@deepseek-ai/dsh-session'
import type { ProjectionHandle } from '@deepseek-ai/dsh-session-projection'
import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
import { assertAllowedModelRoutes, type AllowedModelRoute } from './model-selection.ts'
declare module '@deepseek-ai/dsh-session/types' {
interface SessionEventMap {
/**
* Records this Session’s authorized child provider/model routes. Appended
* before the first model request; absence restricts explicit selection
* to the current parent route. Log-only: it carries
* no `surfaceOp` and never enters model history.
*/
'subagent/model-selection-policy': {
/** Exact routes this Session may select explicitly for a child. */
allowedModels: AllowedModelRoute[]
}
}
}
const modelSelectionPolicySchema: zod.ZodType<AllowedModelRoute[] | null> = zod.array(zod.object({
provider: zod.string().min(1),
model: zod.string().min(1),
}).strict()).min(1).nullable()
/**
* Host-only projection of the durable model-selection policy.
* Each independently mounted consumer registers this shared definition; the registry reference-counts it.
*/
export const subagentModelSelectionProjectionDefinition = {
key: 'subagentModelSelectionPolicy',
stateVersion: 1,
stateSchema: modelSelectionPolicySchema,
init: () => null,
apply: (policy, event) => {
if (policy !== null || event.type !== 'subagent/model-selection-policy') return policy
const { allowedModels } = event.data
assertAllowedModelRoutes(allowedModels)
if (allowedModels.length === 0) {
throw new Error('subagent/model-selection-policy requires at least one route')
}
return allowedModels
},
} satisfies ProjectionDefinition<'subagentModelSelectionPolicy', AllowedModelRoute[] | null>
/**
* Read the exact route list authorized for a Session.
* @param subagentModelSelectionProjectionHandle - registered policy state reader.
* @param session - session whose durable decision is read.
* @returns a detached route list, or undefined to restrict selection to the parent route.
*/
export function subagentModelSelectionPolicy(
subagentModelSelectionProjectionHandle: Pick<ProjectionHandle<AllowedModelRoute[] | null>, 'stateOf'>,
session: Session,
): AllowedModelRoute[] | undefined {
return subagentModelSelectionProjectionHandle.stateOf(session)?.map(route => ({ ...route }))
}