冲突解决:保留 #885 的路由抽取(src/changes-ops.ts + 'changes.ops': changesOps), 把 dev 的现役内容搬进新模块:PTC 四种事件类型、-1 游标下限、会话/持久化双读与 4000 窗口; index.ts 的 import 块取 dev(只补 ServerResponse),订阅回调按仓内惯例restate 最小结构切片。 测试:路由 spec 加一条 PTC 事件穿透断言,用例名与注释统一英文。
81 lines
3.2 KiB
TypeScript
81 lines
3.2 KiB
TypeScript
import { Context as CordisContext } from '@deepseek-ai/cordis'
|
|
import { SessionStore } from '@deepseek-ai/dsh-session'
|
|
import { ToolCallId } from '@deepseek-ai/dsh-llm'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { createChangesOpsApi } from '../src/changes-ops.ts'
|
|
import type { Context } from '../src/context-types.ts'
|
|
|
|
describe('changes.ops waiting form', () => {
|
|
it('parks an empty window and answers a real seq-0 event promptly', async () => {
|
|
const runtime = new CordisContext()
|
|
const fiber = await runtime.plugin(SessionStore)
|
|
const session = runtime.sessions.create()
|
|
const changesOps = createChangesOpsApi(runtime as Context)
|
|
try {
|
|
const baseline = await changesOps({ sessionId: session.id })
|
|
expect(baseline).toEqual({ events: [], lastSeq: -1 })
|
|
|
|
let settled = false
|
|
const waiting = changesOps({ sessionId: session.id, afterSeq: -1, wait: true }).then(result => {
|
|
settled = true
|
|
return result
|
|
})
|
|
await new Promise(resolve => setTimeout(resolve, 2_600))
|
|
expect(settled).toBe(false)
|
|
|
|
const appendedAt = Date.now()
|
|
session.append('tool/call', {
|
|
turn: 0,
|
|
step: 0,
|
|
callId: ToolCallId('first-call'),
|
|
name: 'write',
|
|
arguments: JSON.stringify({ file_path: 'hello.txt', content: 'hello' }),
|
|
})
|
|
const answer = await waiting
|
|
expect(Date.now() - appendedAt).toBeLessThan(1_000)
|
|
expect(answer.events.map(event => [event.seq, event.type])).toEqual([[0, 'tool/call']])
|
|
expect(answer.lastSeq).toBe(0)
|
|
expect(await changesOps({ sessionId: session.id, afterSeq: 0 })).toEqual({ events: [], lastSeq: 0 })
|
|
} finally {
|
|
await fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('ships the PTC sub-call pair, not just the plain tool pair', async () => {
|
|
const runtime = new CordisContext()
|
|
const fiber = await runtime.plugin(SessionStore)
|
|
const session = runtime.sessions.create()
|
|
const changesOps = createChangesOpsApi(runtime as Context)
|
|
try {
|
|
const at = () => ({ turn: 0, step: 0, rootCallId: ToolCallId('root'), parentCallId: ToolCallId('root'), subCallId: 'root:ptc:1', name: 'write' })
|
|
session.append('tool/ptc-dispatch-start', { ...at(), arguments: { file_path: 'notes.txt', content: 'x' } })
|
|
session.append('tool/ptc-dispatch', {
|
|
...at(),
|
|
arguments: { file_path: 'notes.txt', content: 'x' },
|
|
isError: false,
|
|
content: [{ type: 'text', text: 'written' }],
|
|
})
|
|
const answer = await changesOps({ sessionId: session.id })
|
|
expect(answer.events.map(event => event.type))
|
|
.toEqual(['tool/ptc-dispatch-start', 'tool/ptc-dispatch'])
|
|
} finally {
|
|
await fiber.dispose()
|
|
}
|
|
})
|
|
|
|
it('ends the empty-window wait when the client disconnects', async () => {
|
|
const runtime = new CordisContext()
|
|
const fiber = await runtime.plugin(SessionStore)
|
|
const session = runtime.sessions.create()
|
|
const changesOps = createChangesOpsApi(runtime as Context)
|
|
const controller = new AbortController()
|
|
try {
|
|
const waiting = changesOps({ sessionId: session.id, afterSeq: -1, wait: true }, controller.signal)
|
|
controller.abort()
|
|
expect(await waiting).toEqual({ events: [], lastSeq: -1 })
|
|
} finally {
|
|
await fiber.dispose()
|
|
}
|
|
})
|
|
})
|