@lincyaw/dsh 0.2.0-rc.6 → 0.2.0-rc.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/node/tools.ts CHANGED
@@ -1,27 +1,116 @@
1
- import type { ListedTool } from '@lincyaw/mcp/client'
2
- import type { DshAgent, DshContext, DshToolDefinition } from './dsh-types.ts'
1
+ import { errorMessage, isRecord } from '@lincyaw/kernel'
2
+ import { compactToolCatalog, isOrdinaryMediaCall, mediaResult, prepareMediaArguments, queryToolCatalog, resolveCatalogCall } from '@lincyaw/mcp/client'
3
+ import type { CalledImage, CatalogCall, ListedTool } from '@lincyaw/mcp/client'
4
+ import type { DshAgent, DshAttachments, DshContentBlock, DshContext, DshImageMediaType, DshLlm, DshToolDefinition, DshToolExecution } from './dsh-types.ts'
3
5
  import type { DocumentBinding } from './bindings.ts'
4
6
 
5
7
  export const name = 'presmium-tools'
6
8
  export const inject = ['presmium', 'tools']
7
9
 
8
10
  export const PREFIX = 'presmium__'
11
+ export const CANDIDATE_TOOLS = new Set([
12
+ 'get_authoring_capabilities', 'get_document', 'get_document_outline', 'read_document_part',
13
+ 'plan_document_change', 'apply_document_change', 'plan_page_change', 'apply_page_change',
14
+ 'plan_page_duplication', 'apply_page_duplication', 'plan_authoring_action', 'apply_authoring_action', 'read_authoring_action_impact',
15
+ 'check_document', 'get_section', 'get_step', 'list_sections', 'deck_info', 'list_documents',
16
+ 'run_code', 'capture_slides', 'capture_reading', 'capture_overview',
17
+ ])
9
18
 
10
- function definition(binding: DocumentBinding, tool: ListedTool): DshToolDefinition {
19
+ /** New tools carry their routing with their schema; the names retain compatibility with older servers. */
20
+ export const isCandidateTool = (tool: ListedTool): boolean => tool.workspace ? tool.workspace === 'candidate' : CANDIDATE_TOOLS.has(tool.name)
21
+
22
+ export async function resolvePresmiumCall(binding: DocumentBinding | undefined, exec: Pick<DshToolExecution, 'name' | 'arguments'>) {
23
+ if (!binding || !exec.name.startsWith(PREFIX)) throw new Error('operation-unavailable')
24
+ const target = await resolveCatalogCall(binding.registeredTools ?? binding.tools, exec.name.slice(PREFIX.length), exec.arguments)
25
+ if (!target.discovery && JSON.stringify(binding.tools.find(tool => tool.name === target.tool.name)) !== JSON.stringify(target.tool)) throw new Error('operation-definition-changed')
26
+ return target
27
+ }
28
+
29
+ export async function preparePresmiumCall(binding: DocumentBinding, exec: DshToolExecution, target: CatalogCall) {
30
+ if (target.discovery || !isOrdinaryMediaCall(target.tool.name, target.arguments)) return
31
+ if (!isRecord(exec.arguments)) throw new Error('operation-arguments-invalid')
32
+ const preparedCalls = binding.preparedCalls ??= new Map()
33
+ const signature = JSON.stringify([target.tool, target.arguments]), previous = preparedCalls.get(exec.token)
34
+ if (previous) {
35
+ if (previous.signature !== signature) throw new Error('operation-definition-changed')
36
+ return
37
+ }
38
+ if (preparedCalls.size >= 64) throw new Error('operation-preparation-busy')
39
+ const client = isCandidateTool(target.tool) ? binding.client : binding.authorClient
40
+ const args = await prepareMediaArguments(target.tool.name, target.arguments, async (name, args) => {
41
+ exec.signal.throwIfAborted()
42
+ const result = await client.call(name, args, exec.signal)
43
+ exec.signal.throwIfAborted()
44
+ if (result.isError) throw new Error(result.text)
45
+ return result.structured ?? JSON.parse(result.text)
46
+ })
47
+ // Validate the fixed native request using the same definition; never rewrite logged arguments.
48
+ const prepared = await resolveCatalogCall(binding.tools, target.tool.name, args)
49
+ exec.signal.throwIfAborted()
50
+ if (preparedCalls.size >= 64) throw new Error('operation-preparation-busy')
51
+ preparedCalls.set(exec.token, { signature, arguments: prepared.arguments })
52
+ }
53
+
54
+ const IMAGE_TYPES = new Set<string>(['image/png', 'image/jpeg', 'image/webp', 'image/gif'])
55
+
56
+ /**
57
+ * A tool result's images as dsh's durable image blocks, which reach the model beside the text; only when the model of
58
+ * the session's latest request declares image input, as dsh's own MCP client does. Otherwise, and when the attachment
59
+ * store refuses them, one line says they were left out (the text names their files where the tool has any) and the call
60
+ * still succeeds.
61
+ */
62
+ async function imageBlocks(ctx: DshContext, tool: string, images: CalledImage[], exec: DshToolExecution): Promise<DshContentBlock[]> {
63
+ const leftOut = (why: string): DshContentBlock[] => [{ type: 'text', text: `[${images.length} image${images.length === 1 ? '' : 's'} not attached: ${why}]` }]
64
+ const attachments = ctx.get('attachments') as DshAttachments | undefined
65
+ const llm = ctx.get('llm') as DshLlm | undefined
66
+ const { provider, model } = exec.agent?.session.requestContext() ?? {}
67
+ if (!attachments || !llm || !provider || !model) return leftOut('this session takes no images')
68
+ const info = await llm.resolveModelInfo(provider, model, exec.signal).catch(() => undefined)
69
+ if (!info?.inputModalities?.includes('image')) return leftOut(`the model ${model} does not take images`)
70
+ if (images.some(image => !IMAGE_TYPES.has(image.mimeType))) return leftOut('not PNG, JPEG, WebP or GIF')
71
+ try {
72
+ const refs = await attachments.saveImages(images.map((image, n) => ({ data: Buffer.from(image.data, 'base64'), mediaType: image.mimeType as DshImageMediaType, name: `${tool}-${n + 1}.${image.mimeType.slice('image/'.length)}` })))
73
+ return refs.map(attachment => ({ type: 'image', attachment }))
74
+ } catch (error) {
75
+ return leftOut(errorMessage(error))
76
+ }
77
+ }
78
+
79
+ function definition(ctx: DshContext, binding: DocumentBinding, tool: ListedTool): DshToolDefinition {
80
+ const prepared = new Map<symbol, DshContentBlock[]>()
11
81
  return {
12
82
  name: `${PREFIX}${tool.name}`,
13
83
  description: tool.description,
14
84
  parameters: tool.inputSchema,
15
85
  output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: String(value) }] },
16
86
  async execute(args, exec) {
17
- const result = await binding.client.call(tool.name, (args ?? {}) as Record<string, unknown>, exec.signal)
87
+ if (exec.agent && ctx.presmium.bindings.bindingOf(exec.agent) !== binding) throw new Error('operation-binding-changed')
88
+ const target = await resolvePresmiumCall(binding, { name: PREFIX + tool.name, arguments: args })
89
+ if (target.discovery) return JSON.stringify(queryToolCatalog(binding.registeredTools ?? binding.tools, target.arguments))
90
+ const ordinaryMedia = isOrdinaryMediaCall(target.tool.name, target.arguments)
91
+ let argumentsToSend = target.arguments
92
+ if (ordinaryMedia) {
93
+ const fixed = binding.preparedCalls?.get(exec.token)
94
+ binding.preparedCalls?.delete(exec.token)
95
+ if (!fixed || fixed.signature !== JSON.stringify([target.tool, target.arguments])) throw new Error('operation-not-prepared')
96
+ argumentsToSend = fixed.arguments
97
+ }
98
+ const result = await (isCandidateTool(target.tool) ? binding.client : binding.authorClient).call(target.tool.name, argumentsToSend, exec.signal)
18
99
  if (result.isError) throw new Error(result.text)
100
+ if (ordinaryMedia) return JSON.stringify(mediaResult(target.tool.name, result.structured ?? JSON.parse(result.text), target.arguments))
101
+ if (result.images.length > 0) prepared.set(exec.token, [...result.text ? [{ type: 'text' as const, text: result.text }] : [], ...await imageBlocks(ctx, tool.name, result.images, exec)])
19
102
  return result.text
20
103
  },
104
+ projectContent(exec, result) {
105
+ const content = prepared.get(exec.token)
106
+ prepared.delete(exec.token)
107
+ return result.isError ? undefined : content
108
+ },
21
109
  }
22
110
  }
23
111
 
24
112
  export function apply(ctx: DshContext): void {
113
+ ctx.on('tools/result', (exec: Readonly<DshToolExecution>) => { ctx.presmium.bindings.bindingOf(exec.agent)?.preparedCalls?.delete(exec.token) })
25
114
  const live = new WeakSet<DshAgent>()
26
115
  const selections = new WeakMap<DshAgent, () => void>()
27
116
  const registered = new WeakMap<DshAgent, { binding?: DocumentBinding, tools?: ListedTool[], stop?: () => void, dispose: (() => void)[] }>()
@@ -31,10 +120,11 @@ export function apply(ctx: DshContext): void {
31
120
  if (previous && previous.binding === binding && previous.tools === binding?.tools) return
32
121
  previous?.stop?.()
33
122
  for (const dispose of previous?.dispose ?? []) dispose()
123
+ if (binding) binding.registeredTools = binding.tools
34
124
  registered.set(agent, {
35
125
  binding, tools: binding?.tools,
36
126
  stop: binding?.toolChanges.watch(() => { if (agent.status === 'idle') install(agent, binding) }),
37
- dispose: binding?.tools.map(tool => agent.ctx.tools.register(definition(binding, tool))) ?? [],
127
+ dispose: binding ? compactToolCatalog(binding.tools).map(tool => agent.ctx.tools.register(definition(ctx, binding, tool))) : [],
38
128
  })
39
129
  }
40
130
  const sync = async (agent: DshAgent) => {
@@ -57,6 +147,7 @@ export function apply(ctx: DshContext): void {
57
147
  for (const dispose of previous?.dispose ?? []) dispose()
58
148
  })
59
149
  ctx.on('agent/pre-step', async ({ agent }: { agent: DshAgent }, next: () => Promise<unknown>) => {
150
+ await ctx.presmium.bindings.refreshCandidate(agent)
60
151
  await sync(agent)
61
152
  return next()
62
153
  })
package/src/node/turns.ts CHANGED
@@ -3,6 +3,7 @@ import { errorMessage } from '@lincyaw/kernel'
3
3
  import { isInside } from '@lincyaw/kernel/node'
4
4
  import type { DshAgent, DshContext, DshToolExecution } from './dsh-types.ts'
5
5
  import { WORKSPACE_ROUTES } from './server-routes.ts'
6
+ import { isCandidateTool, PREFIX, resolvePresmiumCall } from './tools.ts'
6
7
 
7
8
  // What a writing tool answers when the change cannot be recorded. The AI reads it and tells the author in their own
8
9
  // language, so it names what the author sees (Version History) and none of the internals behind it.
@@ -83,7 +84,8 @@ export function apply(ctx: DshContext): void {
83
84
  if (path === 'open' && response.status === 404) throw new Error(NOT_RECORDED)
84
85
  let reason = text
85
86
  try {
86
- reason = (JSON.parse(text) as { error?: string, detail?: string }).error ?? reason
87
+ const failure = JSON.parse(text) as { error?: string, next?: string, paths?: string[] }
88
+ reason = [failure.error, failure.paths?.join(', '), failure.next].filter(Boolean).join(' ') || reason
87
89
  } catch {
88
90
  }
89
91
  throw new Error(reason || `the review answered ${response.status}`)
@@ -100,9 +102,14 @@ export function apply(ctx: DshContext): void {
100
102
  })
101
103
 
102
104
  ctx.on('tools/execute', async (exec: DshToolExecution, next: () => Promise<unknown>) => {
103
- if (!WRITING_TOOLS.has(exec.name) || !exec.agent) return next()
105
+ if (!exec.agent) return next()
104
106
  const { root } = await ctx.presmium.server.ready
105
107
  if (!isInside(ctx.presmium.bindings.workingDirectoryOf(exec.agent), root)) return next()
108
+ await ctx.presmium.bindings.candidateOf(exec.agent)
109
+ const binding = ctx.presmium.bindings.bindingOf(exec.agent)
110
+ const target = exec.name.startsWith(PREFIX) ? await resolvePresmiumCall(binding, exec) : undefined
111
+ const tool = target?.tool
112
+ if (!WRITING_TOOLS.has(exec.name) && (!tool || tool.kind === 'read' || !isCandidateTool(tool))) return next()
106
113
  const document = ctx.presmium.bindings.bindingOf(exec.agent)?.document.dir ?? ''
107
114
  const agentSessionId = rootAgentSessionOf(exec.agent.session.id)
108
115
  const state = turnStateOf(agentSessionId)
@@ -113,12 +120,9 @@ export function apply(ctx: DshContext): void {
113
120
  }
114
121
  state.document = document
115
122
  state.wrote = true
116
- try {
117
- return await next()
118
- } finally {
119
- await postReview(document, agentSessionId, 'step', { ...state.turn !== undefined ? { turn: state.turn } : {}, label: labelOf(exec), tool: exec.name })
120
- .catch((error: unknown) => log.warn(`presmium-turns: no checkpoint after ${exec.name} in ${agentSessionId}: ${errorMessage(error)}`))
121
- }
123
+ const result = await next()
124
+ await postReview(document, agentSessionId, 'step', { ...state.turn !== undefined ? { turn: state.turn } : {}, label: labelOf(target ? { ...exec, name: PREFIX + target.tool.name, arguments: target.arguments } : exec), tool: target ? PREFIX + target.tool.name : exec.name })
125
+ return result
122
126
  })
123
127
 
124
128
  ctx.on('agent/created', ({ agent }: { agent: LiveAgent }) => {
@@ -94,13 +94,15 @@ export function apply(ctx: EventContext): void {
94
94
  const current = () => !closed && !controller.signal.aborted && subscriptions.get(command.id) === entry
95
95
  void ctx.presmium.server.ready.then(endpoint => {
96
96
  if (!current()) return
97
- if (command.topic === 'authoring-client') {
97
+ if (command.topic === 'authoring-client' || command.topic === 'authoring-window') {
98
+ const resource = command.topic === 'authoring-window' ? 'authoring-windows' : 'authoring-clients'
99
+ const documentQuery: Record<string, string> = command.topic === 'authoring-client' ? { document: command.document } : {}
98
100
  // Keep the client credential on the trusted host, off browser messages and MCP reads.
99
101
  let lease: AuthoringClientLease | undefined
100
102
  let publication: Promise<void> = Promise.resolve()
101
103
  let publications = 0
102
104
  const post = async (kind: string, value: unknown) => {
103
- const path = `${WORKSPACE_ROUTES}authoring-clients/${kind}?${new URLSearchParams({ document: command.document })}`
105
+ const path = `${WORKSPACE_ROUTES}${resource}/${kind}?${new URLSearchParams(documentQuery)}`
104
106
  const response = await ctx.presmium.server.request(path, { method: 'POST', body: value, signal: controller.signal })
105
107
  if (!response.ok) throw new Error('client-publication-refused')
106
108
  return response.json() as Promise<unknown>
@@ -120,8 +122,8 @@ export function apply(ctx: EventContext): void {
120
122
  if (!current()) return
121
123
  if (!validateAuthoringClientLease(value)) throw new Error('client-registration-invalid')
122
124
  lease = value
123
- const query = new URLSearchParams({ document: command.document, clientId: lease.clientId, epoch: lease.epoch, connectionId: lease.connectionId })
124
- const path = `${WORKSPACE_ROUTES}authoring-clients/events?${query}`
125
+ const query = new URLSearchParams({ ...documentQuery, clientId: lease.clientId, epoch: lease.epoch, connectionId: lease.connectionId })
126
+ const path = `${WORKSPACE_ROUTES}${resource}/events?${query}`
125
127
  source = new EventSource(new URL(path, endpoint.url), {
126
128
  fetch: (_address, init) => ctx.presmium.server.request(path, { headers: { ...Object.fromEntries(new Headers(init?.headers)), 'X-Presmium-Authoring-Client': value.credential }, signal: init?.signal ? AbortSignal.any([controller.signal, init.signal]) : controller.signal }),
127
129
  })
@@ -12,6 +12,7 @@ const approvals = z.strictObject({ type: z.literal('subscribe'), id: subscriptio
12
12
 
13
13
  export const workspaceEventCommand = z.union([
14
14
  z.strictObject({ type: z.literal('subscribe'), id: subscriptionId, topic: z.literal('authoring-client'), document: z.string().max(1024), clientId: subscriptionId, epoch: subscriptionId, snapshot: z.unknown() }),
15
+ z.strictObject({ type: z.literal('subscribe'), id: subscriptionId, topic: z.literal('authoring-window'), clientId: subscriptionId, epoch: subscriptionId, snapshot: z.unknown() }),
15
16
  z.strictObject({ type: z.literal('authoring-client-publish'), id: subscriptionId, connectionId: subscriptionId, sequence: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), kind: z.enum(['state', 'ack']), value: z.unknown() }),
16
17
  viewerSite, viewerControl, approvals,
17
18
  z.strictObject({ type: z.literal('subscribe'), id: subscriptionId, topic: z.enum(WORKSPACE_EVENT_TOPICS), document: z.string().max(1024) }),
@@ -26,6 +27,6 @@ export const workspaceEventMessage = z.discriminatedUnion('type', [
26
27
  z.strictObject({ type: z.literal('event'), id: subscriptionId, event: z.string().max(40).optional(), data: z.string() }),
27
28
  ])
28
29
 
29
- export type WorkspaceEventTopic = typeof WORKSPACE_EVENT_TOPICS[number] | 'viewer-site' | 'viewer-control' | 'approvals' | 'authoring-client'
30
+ export type WorkspaceEventTopic = typeof WORKSPACE_EVENT_TOPICS[number] | 'viewer-site' | 'viewer-control' | 'approvals' | 'authoring-client' | 'authoring-window'
30
31
  export type WorkspaceEventCommand = z.infer<typeof workspaceEventCommand>
31
32
  export type WorkspaceEventMessage = z.infer<typeof workspaceEventMessage>
@@ -1,37 +0,0 @@
1
- import { PmMascot, PmSegmented, setMascotEnabled, useAssistantMascot, useMascotEnabled, WorkbenchProvider } from '@lincyaw/workbench'
2
- import type { Workbench } from '@lincyaw/workbench'
3
- import { MASCOT_SETTING, MASCOT_SLOT } from './contract.ts'
4
- import { useClientMessages } from './messages.ts'
5
-
6
- /** The session header's standard prop: a selector over the bound session's snapshot. */
7
- type UseSession = <T>(select: (snapshot: { running?: boolean }) => T) => T
8
-
9
- function Kitten({ running }: { running: boolean }) {
10
- return <span className="pmw-conversation-mascot"><PmMascot size="small" action={useAssistantMascot(running)} /></span>
11
- }
12
-
13
- function MascotSetting({ workbench }: { workbench: Workbench }) {
14
- const t = useClientMessages(workbench)
15
- const on = useMascotEnabled()
16
- return (
17
- <div className="pmw-mascot-setting pm-ui" data-pmw-setting="mascot">
18
- <div className="pmw-mascot-setting__copy">
19
- <p className="pmw-mascot-setting__title">{t.mascot}</p>
20
- <p className="pmw-mascot-setting__hint">{t.mascotHint}</p>
21
- </div>
22
- <PmSegmented label={t.mascot} value={on ? 'show' : 'hide'} options={[{ value: 'show', label: t.mascotShow }, { value: 'hide', label: t.mascotHide }]} onChange={value => setMascotEnabled(value === 'show')} />
23
- </div>
24
- )
25
- }
26
-
27
- /** Puts the kitten in the conversation's header, where it acts out what the assistant is doing, and its switch in
28
- * Settings → General. */
29
- export function registerMascot(ctx: DshClientContext, workbench: Workbench): void {
30
- ctx.effect(() => ctx.slots.inject(MASCOT_SLOT.name, () => ctx.slots.register({ name: MASCOT_SLOT.name, id: MASCOT_SLOT.id, order: -100 }, ({ useSession }: { useSession: UseSession }) => {
31
- const running = useSession(snapshot => Boolean(snapshot.running))
32
- return <WorkbenchProvider workbench={workbench}><Kitten running={running} /></WorkbenchProvider>
33
- })), 'presmium: the kitten beside the conversation')
34
- ctx.effect(() => ctx.slots.inject(MASCOT_SETTING.name, () => ctx.slots.register({ name: MASCOT_SETTING.name, id: MASCOT_SETTING.id, order: 1000 }, () => (
35
- <WorkbenchProvider workbench={workbench}><MascotSetting workbench={workbench} /></WorkbenchProvider>
36
- ))), 'presmium: the kitten\'s switch in Settings')
37
- }