feat(session-title): add fallback and model providers

This commit is contained in:
Tianyi Cui
2026-07-21 01:53:24 +08:00
parent 9a6914d845
commit 58dc5f94de
41 changed files with 2900 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
# @deepseek-ai/dsh-session-title-first-message-llm
Optional `ctx.sessionTitle` provider that summarizes the first eligible human message through `ctx.llm`. It registers the `first-message` cadence, runs automatically only when a fresh non-fork session first creates its fallback, and attributes the result to that message's exact seq. An automatic failure retains the fallback and is retried only through `ctx.sessionTitle.refresh()`.
The plugin uses the complete required [shared LLM configuration](../session-title-llm/README.md#configuration). Omit both `provider` and `model` to inherit the exact route from the current logged main request, or set both to route title generation independently.
## Model Experience
### First-message title request
#### What the model sees
The title model receives the shared title instruction and a JSON array containing only the first eligible human message. Later prompts and inherited fork history do not trigger another automatic call.
#### Token effect
At most one automatic auxiliary request is made for a fresh session, bounded by `maxInputBytes` and `maxOutputTokens`; explicit refreshes may make additional calls. The main agent request gains zero tokens.
#### KV Cache effect
No main-request invalidation. The auxiliary request uses the configured or logged route and has provider-specific cache behavior.
## Known Limitations and Deferred Work
- The first message alone may cease to represent a long-running session; use the all-messages provider when later prompts should retitle it.
- A fork keeps its inherited title and never runs this provider automatically, even when its seeded first message came from the parent.

View File

@@ -0,0 +1,37 @@
{
"name": "@deepseek-ai/dsh-session-title-first-message-llm",
"description": "First-message LLM provider plugin for DeepSeek Harness session titles",
"version": "0.0.1",
"private": true,
"type": "module",
"main": "lib/index.js",
"types": "lib/types/index.d.ts",
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
},
"files": ["lib/index.js", "lib/types/**/*.d.ts", "lib/types/**/*.d.ts.map", "src"],
"license": "BSD-3-Clause",
"peerDependencies": {
"@deepseek-ai/dsh-llm": "^0.0.1",
"@deepseek-ai/dsh-session-title": "^0.0.1",
"@deepseek-ai/dsh-session-title-llm": "^0.0.1",
"cordis": "^4.0.0-rc.7"
},
"dependencies": {
"schemastery": "^3.18.0"
},
"devDependencies": {
"@cordisjs/plugin-include": "workspace:^",
"@cordisjs/plugin-loader": "workspace:^",
"@deepseek-ai/dsh-llm": "workspace:^",
"@deepseek-ai/dsh-llm-deepseek": "workspace:^",
"@deepseek-ai/dsh-session": "workspace:^",
"@deepseek-ai/dsh-session-title": "workspace:^",
"@deepseek-ai/dsh-session-title-llm": "workspace:^",
"cordis": "^4.0.0-rc.7"
}
}

View File

@@ -0,0 +1,40 @@
/** First-human-message model provider for `ctx.sessionTitle`. */
import type { Context } from 'cordis'
import z from 'schemastery'
import {
registerSessionTitleLlmProvider,
SessionTitleLlmConfigFields,
} from '@deepseek-ai/dsh-session-title-llm'
import type { SessionTitleLlmConfig } from '@deepseek-ai/dsh-session-title-llm'
export const name = 'session-title-first-message-llm'
export const inject = ['sessionTitle', 'llm']
/** Required LLM policy; this plugin adds no defaults. */
export type Config = SessionTitleLlmConfig
/** Loader schema shared with the all-messages provider. */
/* jscpd:ignore-start -- Loader requires each plugin to export its own statically walkable schema; the field validators remain shared. */
export const Config: z<Config> = z.object({
targetWords: SessionTitleLlmConfigFields.targetWords,
targetCjkCharacters: SessionTitleLlmConfigFields.targetCjkCharacters,
maxInputBytes: SessionTitleLlmConfigFields.maxInputBytes,
maxOutputTokens: SessionTitleLlmConfigFields.maxOutputTokens,
timeoutMs: SessionTitleLlmConfigFields.timeoutMs,
provider: SessionTitleLlmConfigFields.provider,
model: SessionTitleLlmConfigFields.model,
})
/* jscpd:ignore-end */
/**
* Register the first-message model provider.
* @param ctx - context exposing session-title and LLM services.
* @param config - required route, target, byte, token, and timeout policy.
*/
export function apply(ctx: Context, config: Config): void {
registerSessionTitleLlmProvider(ctx, config, name, 'first-message', (messages) => {
const first = messages[0]
if (first === undefined) throw new Error('first-message title provider requires one human message')
return [first]
})
}

View File

@@ -0,0 +1,120 @@
import { afterEach, describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import Loader from '@cordisjs/plugin-loader'
import Include from '@cordisjs/plugin-include'
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import LlmService, { LlmAdapter } from '@deepseek-ai/dsh-llm'
import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService from '@deepseek-ai/dsh-session-title'
import * as providerPlugin from '@deepseek-ai/dsh-session-title-first-message-llm'
let root: string | undefined
let context: Context | undefined
class LoaderAdapter extends LlmAdapter {
readonly requests: GenerateOptions[] = []
override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
this.requests.push(options)
yield { type: 'text-delta', index: 0, text: 'Loader composed title' }
yield { type: 'finish', reason: { kind: 'stop' } }
}
}
afterEach(async () => {
await context?.fiber.dispose()
context = undefined
if (root !== undefined) await rm(root, { recursive: true, force: true })
root = undefined
})
async function loadComposition(): Promise<Context> {
root = await mkdtemp(join(tmpdir(), 'dsh-title-loader-'))
const configPath = join(root, 'cordis.yml')
await writeFile(configPath, [
"- name: '@deepseek-ai/dsh-llm'",
"- name: '@deepseek-ai/dsh-session'",
"- name: '@deepseek-ai/dsh-session-title'",
' config:',
' fallbackMaxWords: 5',
' fallbackMaxBytes: 40',
' maxTitleBytes: 80',
"- name: '@deepseek-ai/dsh-session-title-first-message-llm'",
' config:',
' targetWords: 5',
' targetCjkCharacters: 10',
' maxInputBytes: 1000',
' maxOutputTokens: 32',
' timeoutMs: 1000',
" provider: 'title-route'",
" model: 'title-model'",
'',
].join('\n'))
context = new Context()
context.baseUrl = pathToFileURL(root).href + '/'
await context.plugin(Loader)
context.loader.builtins.include = Include
const modules = new Map<string, unknown>([
['@deepseek-ai/dsh-llm', LlmService],
['@deepseek-ai/dsh-session', SessionStore],
['@deepseek-ai/dsh-session-title', SessionTitleService],
['@deepseek-ai/dsh-session-title-first-message-llm', providerPlugin],
])
context.loader.internal = {
version: 'v2',
async import(specifier: string) {
if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
return modules.get(specifier)
},
} as unknown as NonNullable<typeof context.loader.internal>
await context.loader.create({
name: 'cordis:include',
config: { path: pathToFileURL(configPath).href },
})
await context.loader.await()
return context
}
describe('session-title Loader composition', () => {
it('loads the service and one model provider with required deployment policy', async () => {
const ctx = await loadComposition()
const unloaded = [...ctx.loader.entries()]
.filter(entry => entry.fiber === undefined && !entry.disabled)
.map(entry => entry.options.name)
expect(unloaded).toEqual([])
const adapter = new LoaderAdapter()
ctx.llm.registerAdapter(['title-route'], adapter)
const session = ctx.sessions.create(SessionId('loader-title'))
session.append('turn/start', {
turn: 1,
trigger: { kind: 'message', source: { kind: 'user' } },
})
const message = session.append('user/message', {
content: [{ type: 'text', text: 'Compose a title through Loader' }],
source: { kind: 'user' },
}, { surfaceOp: 'append' })
await new Promise(resolve => setTimeout(resolve, 0))
session.append('request/header', {
header: { config: { provider: 'main-route', model: 'main-model' } },
reason: 'initial',
})
await new Promise(resolve => setTimeout(resolve, 0))
expect(adapter.requests[0]).toMatchObject({ provider: 'title-route', model: 'title-model' })
expect(ctx.sessionTitle.get(session)).toMatchObject({
title: 'Loader composed title',
messageSeqs: [message.seq],
source: {
kind: 'provider',
provider: 'session-title-first-message-llm',
model: { provider: 'title-route', model: 'title-model' },
},
})
})
})

View File

@@ -0,0 +1,59 @@
import { afterEach, describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import LlmService from '@deepseek-ai/dsh-llm'
import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService from '@deepseek-ai/dsh-session-title'
import * as FirstMessageTitleProvider from '@deepseek-ai/dsh-session-title-first-message-llm'
const contexts: Context[] = []
afterEach(async () => {
await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
})
describe.skipIf(!process.env.DEEPSEEK_API_KEY)('first-message title provider with real DeepSeek API', () => {
it('replaces the fallback with a short model title', async () => {
const ctx = new Context()
contexts.push(ctx)
await ctx.plugin(LlmService)
await ctx.plugin(LlmDeepSeek, { thinking: 'disabled' })
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, {
fallbackMaxWords: 5,
fallbackMaxBytes: 40,
maxTitleBytes: 80,
})
await ctx.plugin(FirstMessageTitleProvider, {
targetWords: 5,
targetCjkCharacters: 10,
maxInputBytes: 4_096,
maxOutputTokens: 64,
timeoutMs: 60_000,
provider: 'deepseek',
model: 'deepseek-v4-flash',
})
const session = ctx.sessions.create(SessionId('real-title-provider'))
session.append('turn/start', {
turn: 1,
trigger: { kind: 'message', source: { kind: 'user' } },
})
const message = session.append('user/message', {
content: [{ type: 'text', text: 'Explain why append-only logs make session titles durable.' }],
source: { kind: 'user' },
}, { surfaceOp: 'append' })
const title = await ctx.sessionTitle.refresh(session)
expect(title).toMatchObject({
messageSeqs: [message.seq],
source: {
kind: 'provider',
provider: 'session-title-first-message-llm',
model: { provider: 'deepseek', model: 'deepseek-v4-flash' },
},
})
expect(title?.title.length).toBeGreaterThan(0)
expect(Buffer.byteLength(title?.title ?? '', 'utf8')).toBeLessThanOrEqual(80)
})
})

View File

@@ -0,0 +1,86 @@
import { Context } from 'cordis'
import { describe, expect, it, vi } from 'vitest'
import LlmService, { LlmAdapter } from '@deepseek-ai/dsh-llm'
import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
import SessionTitleService, { type SessionTitleProvider } from '@deepseek-ai/dsh-session-title'
import * as providerPlugin from '@deepseek-ai/dsh-session-title-first-message-llm'
class RecordingAdapter extends LlmAdapter {
readonly requests: GenerateOptions[] = []
override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
this.requests.push(options)
yield { type: 'text-delta', index: 0, text: 'First-message model title' }
yield { type: 'finish', reason: { kind: 'stop' } }
}
}
const TITLE_CONFIG = { fallbackMaxWords: 5, fallbackMaxBytes: 40, maxTitleBytes: 80 } as const
const LLM_CONFIG = {
targetWords: 5,
targetCjkCharacters: 10,
maxInputBytes: 1_000,
maxOutputTokens: 32,
timeoutMs: 1_000,
provider: 'title-route',
model: 'title-model',
} as const
async function settle(): Promise<void> {
await new Promise(resolve => setTimeout(resolve, 0))
}
describe('first-message LLM title provider', () => {
it('rejects an impossible empty provider request at its own boundary', async () => {
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, TITLE_CONFIG)
let registered: SessionTitleProvider | undefined
vi.spyOn(ctx.sessionTitle, 'register').mockImplementation((provider) => {
registered = provider
return () => undefined
})
providerPlugin.apply(ctx, LLM_CONFIG)
await expect(registered!.generate({
session: new Session(SessionId('empty-first-provider')),
messages: [],
signal: new AbortController().signal,
})).rejects.toThrow(/requires one human message/)
})
it('always selects only the first eligible human message, including explicit refresh', async () => {
const ctx = new Context()
await ctx.plugin(LlmService)
await ctx.plugin(SessionStore)
await ctx.plugin(SessionTitleService, TITLE_CONFIG)
const adapter = new RecordingAdapter()
ctx.llm.registerAdapter(['title-route'], adapter)
await ctx.plugin(providerPlugin, LLM_CONFIG)
const session = ctx.sessions.create(SessionId('first-plugin'))
session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
const first = session.append('user/message', {
content: [{ type: 'text', text: 'first input' }], source: { kind: 'user' },
}, { surfaceOp: 'append' })
await settle()
session.append('request/header', {
header: { config: { provider: 'main', model: 'main-model' } }, reason: 'initial',
})
await settle()
session.append('user/message', {
content: [{ type: 'text', text: 'second input must be ignored' }], source: { kind: 'user' },
}, { surfaceOp: 'append' })
await ctx.sessionTitle.refresh(session)
expect(adapter.requests).toHaveLength(2)
for (const options of adapter.requests) {
const content = options.messages[0]?.content[0]
expect(content?.type === 'text' && content.text).toContain('first input')
expect(content?.type === 'text' && content.text).not.toContain('second input must be ignored')
}
expect(ctx.sessionTitle.get(session)).toMatchObject({ messageSeqs: [first.seq] })
})
})

View File

@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": { "rootDir": "src", "outDir": "lib/types" },
"include": ["src"],
"references": [
{ "path": "../../../vendor/cosmokit" },
{ "path": "../../../vendor/cordis" },
{ "path": "../../../vendor/schemastery" },
{ "path": "../../llm/llm" },
{ "path": "../session-title" },
{ "path": "../session-title-llm" }
]
}