Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/src/approval-review/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ export const isInputCut = (input: unknown): boolean => serializedInput(input).le
export const isConfirmationOnly = (call: Pick<ProposedCall, 'cls' | 'kind' | 'input'>): boolean =>
call.cls === 'destructive' || call.kind === 'grant' || isInputCut(call.input)

// field '' (json-layout's item guard): keys and values the item itself must hold, not one property of it
const checkedText = (check: CheckedArgument): string => check.field.split('/').filter(Boolean).length
? `checked by the server before the call: the ${check.field.split('/').filter(Boolean).at(-1)} of what "${check.against}" names; the call is refused, nothing done, if it differs`
: `checked by the server before the call: the item "${check.against}" names (the list item it is in, or removes or moves) holds these keys and values; the call is refused, nothing done, if it does not`

const renderCall = (call: ProposedCall): string => {
// The title once: the gate takes it from annotations.title, where the MCP client puts it.
const { title: annotatedTitle, ...annotations } = call.annotations ?? {}
Expand All @@ -538,7 +543,7 @@ const renderCall = (call: ProposedCall): string => {
const given = (call.input && typeof call.input === 'object' ? call.input : {}) as Record<string, unknown>
const checked = Object.entries(call.checked ?? {})
.filter(([name, check]) => given[name] !== undefined && identifier(name) && identifier(check.against))
.map(([name, check]) => [name, `checked by the server before the call: the ${check.field.split('/').filter(Boolean).at(-1) ?? 'label'} of what "${check.against}" names; the call is refused, nothing done, if it differs`])
.map(([name, check]) => [name, checkedText(check)])
const head = JSON.stringify({
tool: call.toolName,
...(call.server ? { server: call.server } : {}),
Expand Down
3 changes: 2 additions & 1 deletion api/src/conversations/model-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const isMedia = (item: unknown): item is MediaItem =>
const sizeKb = (item: MediaItem) => Math.round(String(item.data).length * 3 / 4 / 1024)

export const imagePlaceholder = (item: MediaItem, reason: 'earlier' | 'unsupported') => reason === 'earlier'
? `[image ${item.mediaType} ~${sizeKb(item)}kB no longer shown: a later image replaced it, call the tool again to see the current state]`
// "call again to see it" read as "I could not see it": the agent told a person it had not seen a render it had described
? `[image ${item.mediaType} ~${sizeKb(item)}kB, which you saw when it came, no longer shown: a later image replaced it. What you saw then still holds; call the tool again only if what it shows may have changed since]`
: `[image ${item.mediaType} ~${sizeKb(item)}kB not shown: this model does not read images]`

const isContentResult = (part: any): part is { type: 'tool-result', output: { type: 'content', value: ContentItem[] } } =>
Expand Down
11 changes: 8 additions & 3 deletions api/src/conversations/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,14 +779,19 @@ export const titleInput = (messages: { role: string, parts?: Array<{ type: strin
const asked = textOf('user')
if (!asked) return undefined
const answered = textOf('assistant')
return `Person: ${asked}${answered ? `\n\nAssistant: ${answered}` : ''}`
// wrapped and followed by the instruction: given as a bare "Person: … Assistant: …", small models went on with the
// conversation (« Bonjour ! Je regarde le portail… ») instead of titling it
return `<conversation>\nPerson: ${asked}${answered ? `\n\nAssistant: ${answered}` : ''}\n</conversation>\n\nThe title of this conversation, and nothing else:`
}

/** A model's title, made safe to show: one line, no wrapping quotes or trailing period, bounded. */
export const cleanTitle = (text: string): string | undefined => {
const line = text.split('\n').map(l => l.trim()).find(Boolean) ?? ''
// a label before the title (« Titre de la conversation : … », "Title: …") is dropped; a line that only introduces the
// next one ends with a colon (« Titre sans citation ni point final, … : ») and is skipped
const lines = text.split('\n').map(l => l.trim().replace(/^(title|titre)( de la conversation| of the conversation| demandé| proposé| suggéré)?\s*:\s*/i, '')).filter(Boolean)
const line = lines.find(l => !l.endsWith(':')) ?? ''
// eslint-disable-next-line no-control-regex
const title = line.replace(/^(title|titre)\s*:\s*/i, '').replace(/^["'«“\s]+|["'»”\s.]+$/g, '').replace(/[\u0000-\u001F\u007F]/g, '')
const title = line.replace(/^["'«“\s]+|["'»”\s.]+$/g, '').replace(/[\u0000-\u001F\u007F]/g, '')
if (!title) return undefined
return title.length > TITLE_MAX_CHARS ? title.slice(0, TITLE_MAX_CHARS - 1).trimEnd() + '…' : title
}
7 changes: 7 additions & 0 deletions tests/features/approval-review/approval-review.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ test.describe('Checked arguments', () => {
const without = reviewMessage({ earlierCalls: [], intent: intentFromConversation(['attach it'], ''), call: { ...checkedCall, input: { id: 'b51f' } }, exposure: noExposure, nonce: 'abcdef012345' }, 2)
assert.doesNotMatch(without, /"checkedArguments"/)
})

test('a checked item guard (json-layout, field \'\') says the item holds the given keys', () => {
const formSchema = { type: 'object', properties: { path: { type: 'string' }, value: {}, item: { type: 'object', 'x-checked': { against: 'path', field: '' } } } }
const formCall: ProposedCall = { ...call, toolName: 'page_config_setFieldValue', checked: checkedArgumentsOf(formSchema), input: { path: '/elements/1/link', item: { type: 'button' }, value: { type: 'standard' } } }
const message = reviewMessage({ earlierCalls: [], intent: intentFromConversation(['add a button'], ''), call: formCall, exposure: noExposure, nonce: 'abcdef012345' }, 2)
assert.match(message, /"item": "checked by the server before the call: the item \\"path\\" names \(the list item it is in, or removes or moves\) holds these keys and values; the call is refused, nothing done, if it does not"/)
})
})

test.describe('Earlier calls', () => {
Expand Down
9 changes: 8 additions & 1 deletion tests/features/conversations/title.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test.describe('conversation titles', () => {
{ role: 'user', parts: [{ type: 'text', text: 'list the road closures ' + 'x'.repeat(3000) }] },
{ role: 'assistant', parts: [{ type: 'reasoning', text: 'hidden' }, { type: 'text', text: 'Here they are.' }] }
])!
assert.match(input, /^Person: list the road closures x+…\n\nAssistant: Here they are\.$/)
assert.match(input, /^<conversation>\nPerson: list the road closures x+…\n\nAssistant: Here they are\.\n<\/conversation>\n\nThe title of this conversation, and nothing else:$/)
assert.ok(input.length < 2200)
assert.equal(titleInput([{ role: 'assistant', parts: [{ type: 'text', text: 'hi' }] }]), undefined)
})
Expand All @@ -20,6 +20,13 @@ test.describe('conversation titles', () => {
assert.equal(cleanTitle('"Fermetures de rues du centre-ville."\nextra'), 'Fermetures de rues du centre-ville')
assert.equal(cleanTitle('Title: Road closures'), 'Road closures')
assert.equal(cleanTitle(' \n '), undefined)
// what a small model answered in portals' simulations
assert.equal(cleanTitle('Titre de la conversation : Page « Nos actions pour la jeunesse » vide sur mobile'), 'Page « Nos actions pour la jeunesse » vide sur mobile')
assert.equal(cleanTitle('Titre demandé : Ajout d\'un bouton vers le catalogue'), 'Ajout d\'un bouton vers le catalogue')
assert.equal(cleanTitle('Titre sans citation ni point final, en français, sujet et contexte :\nAperçu mobile de la page Jeunesse'), 'Aperçu mobile de la page Jeunesse')
assert.equal(cleanTitle('Titre sans citation ni point final, en français, sujet et contexte :'), undefined)
// a title that merely starts with the word stays whole
assert.equal(cleanTitle('Titre de séjour : renouvellement'), 'Titre de séjour : renouvellement')
assert.equal(cleanTitle('a'.repeat(200))!.length, 80)
})
})
2 changes: 1 addition & 1 deletion tests/features/tool-result/2.model-images.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test.describe('limitModelImages', () => {
assert.equal(outputOf(out, 'c2').type, 'content', 'the latest is sent as an image')
const older = outputOf(out, 'c0')
assert.equal(older.type, 'text', 'a result left with no image takes the shape every provider accepts')
assert.match(older.value, /^report c0\n\n\[image image\/png ~3kB no longer shown/)
assert.match(older.value, /^report c0\n\n\[image image\/png ~3kB, which you saw when it came, no longer shown/)
})

test('a model that does not read images gets none, and is told why', () => {
Expand Down
Loading