返回 CodeWhale
session-prompt.ts
根目录 / crates / engine / extension-host / src / dsh / session-prompt.ts
1 import { Service } from '@deepseek-ai/cordis'
2 import { ErrorCode } from '../protocol.ts'
3 import { RpcError } from '../rpc.ts'
4 import type { OwnerRecord, ToolInvocation } from '../root.ts'
5 import type { AdapterHost, DshAdapter } from './adapters.ts'
6
7 const CHILD_ONLY = "system prompt sections are only available on a child agent's agent/created context"
8
9 function checkpointCall(host: AdapterHost, ctx: unknown, session: unknown): ToolInvocation | undefined {
10 const invocation = host.liveInvocation(ctx)
11 const ticket = invocation?.ticket
12 return invocation !== undefined && typeof ticket === 'string' && ticket.length > 0
13 && session !== undefined && session === invocation.session
14 ? invocation
15 : undefined
16 }
17
18 export const sessionsAdapter: DshAdapter = {
19 service: 'sessions',
20 define(host) {
21 class SessionsService extends Service {
22 constructor(ctx: any) { super(ctx, 'sessions') }
23
24 async flush(session: unknown): Promise<boolean> {
25 const ctx = this.ctx
26 const call = checkpointCall(host, ctx, session)
27 if (call === undefined) throw new RpcError(ErrorCode.NotAvailable, 'sessions.flush requires this live tool invocation’s session')
28 const owner: OwnerRecord = call.owner
29 const live = () => checkpointCall(host, ctx, session) === call && !call.signal.aborted
30 const run = async () => {
31 if (!live()) throw new RpcError(ErrorCode.NotAvailable, 'session checkpoint invocation ended')
32 const result = await host.rpc.request<unknown>('session/checkpoint', { owner: owner.ref, ticket: call.ticket }, call.signal)
33 if (!live()) throw new RpcError(ErrorCode.NotAvailable, 'session checkpoint invocation ended')
34 if (typeof result !== 'boolean') throw new RpcError(ErrorCode.Internal, 'invalid session checkpoint receipt')
35 return result
36 }
37 const result = call.pending.then(run)
38 call.pending = result.then(() => undefined, () => undefined)
39 return result
40 }
41 }
42 Object.freeze(SessionsService.prototype)
43 return SessionsService
44 },
45 }
46
47 export function createBundledSystemPromptView(host: AdapterHost, ctx: any): Record<string, unknown> {
48 return {
49 getSectionOrder() {
50 return 117
51 },
52 section(definition: unknown) {
53 if (definition === null || typeof definition !== 'object' || Array.isArray(definition)) {
54 throw new RpcError(ErrorCode.InvalidParams, 'expected a system prompt section object')
55 }
56 const { name, text } = definition as { name?: unknown; text?: unknown }
57 if (typeof name !== 'string' || name.length === 0) {
58 throw new RpcError(ErrorCode.InvalidParams, 'system prompt section name must be a non-empty string')
59 }
60 const rawText = typeof text === 'function' ? text({ agent: undefined }) : text
61 if (typeof rawText !== 'string' || rawText.trim().length === 0) {
62 throw new RpcError(ErrorCode.InvalidParams, 'system prompt section text must be a non-empty string')
63 }
64 const baseId = name
65 .toLowerCase()
66 .replace(/[^a-z0-9_-]+/g, '_')
67 .replace(/^[^a-z]+/, 's_')
68 .slice(0, 56) || 'section'
69 const register = host.registerPromptSection
70 if (!register) throw new RpcError(ErrorCode.NotAvailable, 'prompt section registration is unavailable')
71 const disposers: Array<() => void> = []
72 const bytes = Buffer.byteLength(rawText, 'utf8')
73 if (bytes <= 4096) {
74 disposers.push(register(ctx, { id: baseId, text: rawText }))
75 } else {
76 const lines = rawText.split('\n')
77 let chunk = ''
78 let part = 1
79 for (const line of lines) {
80 const candidate = chunk.length === 0 ? line : `${chunk}\n${line}`
81 if (Buffer.byteLength(candidate, 'utf8') > 4096 && chunk.length > 0) {
82 disposers.push(register(ctx, { id: `${baseId}_${part++}`, text: chunk }))
83 chunk = line
84 } else {
85 chunk = candidate
86 }
87 }
88 if (chunk.length > 0) {
89 disposers.push(register(ctx, { id: `${baseId}_${part}`, text: chunk }))
90 }
91 }
92 return () => {
93 for (const dispose of disposers) dispose()
94 }
95 },
96 }
97 }
98
99 export const systemPromptAdapter: DshAdapter = {
100 service: 'systemPrompt',
101 define() {
102 class SystemPromptService extends Service {
103 constructor(ctx: any) { super(ctx, 'systemPrompt') }
104 section() { throw new RpcError(ErrorCode.NotAvailable, CHILD_ONLY) }
105 getSectionOrder() { throw new RpcError(ErrorCode.NotAvailable, CHILD_ONLY) }
106 }
107 Object.freeze(SystemPromptService.prototype)
108 return SystemPromptService
109 },
110 }
111
111 lines TYPESCRIPT