Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Take a Bot's photo only from a local file or a data URL

Fei2411df885ba49e57
diff --git a/ds-bot/README.md b/ds-bot/README.md
index aa423003..671f1d5f 100644
--- a/ds-bot/README.md
+++ b/ds-bot/README.md
@@ -11,7 +11,7 @@ Tested with dsh `0.2.1-rc.2` (Web) and `0.2.0-rc.2` (Web and Desktop).
- **Long-lived Bots**: each Bot is a dsh Session in the "DS Bot" workspace, with its own name, role, instructions and model.
- **Working folders**: a Bot can work in a folder you choose instead of the team workspace, and a group chat can set one folder for all its members there. Pick it in the Bot's settings or details panel or in the group's settings, or ask a Bot (`update_bot`, `update_group`). After a change, the chat goes on in a fresh part that starts in the new folder. When a Bot sets a folder outside the team workspace, or adds a Bot to a group that works in one, it asks for your approval if those Bots can only write in their folder. Folders that hold dsh data (`$DSH_HOME`, the team's data) are refused. The team workspace itself moves in Settings > General > Locations; Bots and groups that work there go on in a fresh part in the new folder, and the old folder keeps its files.
- **Permission presets**: each Bot runs with its own dsh permission preset (Read only, Workspace or Full access), set in its details panel. A group chat can run all its members with one preset there, in place of their own: turn on **Same for every member** under Permissions in the group's settings. Only you set permissions; Bots cannot change them.
-- **Bots work together**: `message_bot` sends a message to another Bot, `create_bot` and `update_bot` add and set up Bots (including a profile photo from a local file, an http(s) link, or a data URL), and `ask_user` asks you a question without blocking the other Bots.
+- **Bots work together**: `message_bot` sends a message to another Bot, `create_bot` and `update_bot` add and set up Bots (including a profile photo from a local image file or a data URL), and `ask_user` asks you a question without blocking the other Bots.
- **Group chats**: members speak one at a time, and each one reads the latest messages first. A message can `@Name` a member to pass the turn on; type `@` to pick one from a list. **+ → Reference a chat** adds another Bot's chat to a message as context, and in a group the members who answer get it. A member with nothing new to add stays quiet (`[PASS]`). Each member has its own hidden Session per group, so a group never reads a Bot's private chat.
- **Roles and permissions**:
diff --git a/ds-bot/src/host/avatar.js b/ds-bot/src/host/avatar.js
index 06f949a9..0841c6bf 100644
--- a/ds-bot/src/host/avatar.js
+++ b/ds-bot/src/host/avatar.js
@@ -1,13 +1,12 @@
-// Avatar input for create_bot and update_bot: a local image path, a data URL, or an
-// http(s) link, read into the data URL a Bot keeps in avatar.image. There is no image
-// library in the host, so images are not resized; a small square image is asked for,
-// and every kind of input is capped at what the stored format holds.
+// Avatar input for create_bot and update_bot: a local image path or a data URL, read
+// into the data URL a Bot keeps in avatar.image. There is no image library in the
+// host, so images are not resized; a small square image is asked for, and every kind
+// of input is capped at what the stored format holds.
import { readFile, stat } from 'node:fs/promises'
import { homedir } from 'node:os'
import { join, resolve } from 'node:path'
import { MAX_AVATAR_IMAGE } from './constants.js'
-const TIMEOUT_MS = 15_000
// Base64 is four characters for every three bytes, and the data URL adds a mime
// prefix, so a raw image must stay under this to fit MAX_AVATAR_IMAGE once stored.
const LONGEST_PREFIX = 'data:image/jpeg;base64,'
@@ -29,7 +28,6 @@ function mimeOf(bytes) {
const tooLarge = size => `The avatar image is too large: ${size} bytes, and the limit is ${MAX_BYTES} bytes (about 120 KB). ${HINT}`
const NOT_IMAGE = `The avatar must be a PNG, JPEG, WebP, or GIF image. ${HINT}`
-const CHECK_LINK = 'Check the link, or download the image and pass its path.'
function toDataUrl(bytes) {
if (bytes.length > MAX_BYTES) throw new Error(tooLarge(bytes.length))
@@ -81,60 +79,14 @@ function readDataUrl(value) {
return toDataUrl(Buffer.from(payload, 'base64'))
}
-const secondsOf = timeoutMs => {
- const seconds = Math.max(1, Math.round(timeoutMs / 1000))
- return `${seconds} second${seconds === 1 ? '' : 's'}`
-}
-const timedOut = timeoutMs => `The image link did not answer within ${secondsOf(timeoutMs)}. ${CHECK_LINK}`
-const isTimeout = error => error?.name === 'TimeoutError' || error?.name === 'AbortError' || error?.cause?.name === 'TimeoutError'
-const discard = async (response) => { try { await response.body?.cancel() } catch {} }
-
-async function readUrl(url, timeoutMs) {
- let response
- try {
- response = await fetch(url, { redirect: 'follow', signal: AbortSignal.timeout(timeoutMs) })
- } catch (error) {
- if (isTimeout(error)) throw new Error(timedOut(timeoutMs))
- throw new Error(`Could not download the image: ${error?.cause?.message ?? error?.message ?? 'network error'}. ${CHECK_LINK}`)
- }
- if (!response.ok) {
- await discard(response)
- throw new Error(`The image link answered ${response.status}${response.statusText ? ` ${response.statusText}` : ''}. ${CHECK_LINK}`)
- }
- const length = Number(response.headers.get('content-length'))
- if (Number.isFinite(length) && length > MAX_BYTES) {
- await discard(response)
- throw new Error(tooLarge(length))
- }
- const reader = response.body?.getReader()
- if (reader === undefined) return toDataUrl(Buffer.alloc(0))
- const chunks = []
- let size = 0
- try {
- for (;;) {
- const { done, value } = await reader.read()
- if (done) break
- size += value.length
- if (size > MAX_BYTES) {
- await reader.cancel().catch(() => {})
- throw new Error(tooLarge(size))
- }
- chunks.push(Buffer.from(value))
- }
- } catch (error) {
- if (error?.name === 'AbortError' || error?.name === 'TimeoutError') throw new Error(timedOut(timeoutMs))
- throw error
- }
- return toDataUrl(Buffer.concat(chunks))
-}
-
// Returns the data URL for avatar.image, or throws with a line the user can read.
-// `baseDir` is where a relative local path starts, `timeoutMs` covers a whole download.
-export async function readAvatar(input, { baseDir, timeoutMs = TIMEOUT_MS } = {}) {
+// `baseDir` is where a relative local path starts. Links are not fetched: the host
+// would reach addresses, such as local services, that dsh's own web fetch refuses.
+export async function readAvatar(input, { baseDir } = {}) {
const value = typeof input === 'string' ? input.trim() : ''
- if (value === '') throw new Error(`Give an image path, an http(s) link, or a data URL. ${HINT}`)
+ if (value === '') throw new Error(`Give an image path or a data URL. ${HINT}`)
if (/^data:/i.test(value)) return readDataUrl(value)
- if (/^https?:\/\//i.test(value)) return readUrl(value, timeoutMs)
- if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) throw new Error(`The avatar must be a local image path, an http(s) link, or a data URL. ${HINT}`)
+ if (/^https?:\/\//i.test(value)) throw new Error('An http(s) link is not downloaded here. Save the image to a file and pass its path, or pass a data URL.')
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) throw new Error(`The avatar must be a local image path or a data URL. ${HINT}`)
return readLocal(value, baseDir)
}
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index c40b41e4..19ddb8c1 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -24,7 +24,7 @@ const MODE_PARAM = {
const WORK_DIR_PARAM = { type: 'string', description: 'Full path of the folder to work in, where commands and file tools start. Only when the user asks for it' }
const AVATAR_PARAM = {
type: 'string',
- description: 'Profile picture: a local image path (a relative path starts in your working folder), an http(s) link, or a data URL. PNG, JPEG, WebP, or GIF; square, 256x256 recommended, up to 120 KB. Images are not resized.',
+ description: 'Profile picture: a local image path (a relative path starts in your working folder) or a data URL. PNG, JPEG, WebP, or GIF; square, 256x256 recommended, up to 120 KB. Images are not resized.',
}
const WORKING_DIRECTORY_TOOL = 'working_directory'
@@ -306,19 +306,18 @@ export function install(rt) {
if (bot.id !== caller.id && !rt.isMain(self)) return 'Only a Main Bot can change another Bot. Leave out bot to change yourself, or ask a Main Bot with message_bot.'
try {
const { name: newName, role, instructions, color, model, effort } = args
- let avatar
+ let image
if (args.avatar !== undefined) {
const input = typeof args.avatar === 'string' ? args.avatar.trim() : args.avatar
- // The photo rides on the Bot's other avatar fields, as the settings page's
- // upload does: setting one keeps a custom character, and clearing removes
- // only the image so the character shows again.
- avatar = input === null || input === ''
- ? { ...bot.avatar, image: null }
- : { ...bot.avatar, image: await readAvatar(String(input), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent) }) }
+ image = input === null || input === '' ? null : await readAvatar(String(input), { baseDir: rt.sessionFolder?.(exec.agent?.session ?? exec.agent) })
}
const moving = typeof args.work_dir === 'string'
const workDir = moving ? await rt.cleanWorkDir(args.work_dir) : undefined
if (moving && workDir !== bot.workDir) await rt.approveFolder(exec, { path: workDir, sessions: rt.sessionsOfBot(bot.id), who: { en: bot.name, zh: ` ${bot.name} ` } })
+ // The photo rides on the Bot's other avatar fields, as the settings page's upload
+ // does: setting one keeps a custom character, and clearing removes only the image.
+ // They are read here, after the waits above, so a change made meanwhile stays.
+ const avatar = image === undefined ? undefined : { ...rt.botOf(bot.id)?.avatar, image }
const next = await rt.updateBot(bot.id, { name: newName, role, instructions, color, ...(avatar !== undefined ? { avatar } : {}), ...(model ? { model } : {}), ...(effort === undefined ? {} : { effort }), ...(moving ? { workDir: workDir ?? '' } : {}) })
if (!moving) return `Updated ${next.name}.`
const folder = workDir ?? 'the team workspace'
diff --git a/ds-bot/test/avatar.test.js b/ds-bot/test/avatar.test.js
index 75ae54f1..1ca0c483 100644
--- a/ds-bot/test/avatar.test.js
+++ b/ds-bot/test/avatar.test.js
@@ -1,6 +1,5 @@
import assert from 'node:assert/strict'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
-import { createServer } from 'node:http'
import { homedir, tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -23,22 +22,6 @@ const tempDir = (t) => {
return dir
}
-async function serve(handler) {
- const server = createServer(handler)
- await new Promise((resolve, reject) => {
- server.once('error', reject)
- server.listen(0, '127.0.0.1', resolve)
- })
- const { port } = server.address()
- return {
- url: `http://127.0.0.1:${port}`,
- close: () => new Promise((resolve) => {
- server.closeAllConnections()
- server.close(resolve)
- }),
- }
-}
-
test('a data URL is stored under the type its bytes really are', async () => {
assert.equal(await readAvatar(pngUrl), pngUrl)
assert.equal(await readAvatar(jpegUrl), jpegUrl)
@@ -113,46 +96,12 @@ test('a malformed data URL is refused', async () => {
await assert.rejects(readAvatar('data:image/png;base64,abcd'), /must be a PNG, JPEG, WebP, or GIF image/)
})
-test('an empty value or a link that is not http(s) is refused', async () => {
- await assert.rejects(readAvatar(''), /Give an image path, an http\(s\) link, or a data URL/)
+test('an empty value or a link is refused', async () => {
+ await assert.rejects(readAvatar(''), /Give an image path or a data URL/)
await assert.rejects(readAvatar(' '), /Give an image path/)
- await assert.rejects(readAvatar('ftp://example.com/photo.png'), /must be a local image path, an http\(s\) link, or a data URL/)
- await assert.rejects(readAvatar('file:///tmp/photo.png'), /must be a local image path, an http\(s\) link, or a data URL/)
-})
-
-test('an http link is downloaded, and its content type is not trusted', async (t) => {
- const server = await serve((request, response) => {
- if (request.url === '/photo') {
- response.writeHead(200, { 'content-type': 'text/plain' })
- response.end(JPEG)
- return
- }
- response.writeHead(404, { 'content-type': 'text/plain' })
- response.end('no such image')
- })
- t.after(server.close)
- assert.equal(await readAvatar(`${server.url}/photo`), jpegUrl)
- await assert.rejects(readAvatar(`${server.url}/missing`), /answered 404/)
-})
-
-test('an oversized or slow download is refused', async (t) => {
- const huge = Buffer.concat([PNG.subarray(0, 8), Buffer.alloc(200_000)])
- const server = await serve((request, response) => {
- if (request.url === '/length') {
- response.writeHead(200, { 'content-length': String(huge.length) })
- response.end(huge)
- return
- }
- if (request.url === '/chunks') {
- response.writeHead(200, { 'content-type': 'image/png' })
- for (let at = 0; at < huge.length; at += 25_000) response.write(huge.subarray(at, at + 25_000))
- response.end()
- return
- }
- // /slow: keep the request open until the caller's timeout fires.
- })
- t.after(server.close)
- await assert.rejects(readAvatar(`${server.url}/length`), /too large: 200008 bytes/)
- await assert.rejects(readAvatar(`${server.url}/chunks`), /The avatar image is too large/)
- await assert.rejects(readAvatar(`${server.url}/slow`, { timeoutMs: 200 }), /did not answer within 0\.2 seconds|did not answer within 1 second/)
+ await assert.rejects(readAvatar('ftp://example.com/photo.png'), /must be a local image path or a data URL/)
+ await assert.rejects(readAvatar('file:///tmp/photo.png'), /must be a local image path or a data URL/)
+ // A link is never fetched, so it reaches nothing on this machine or its network.
+ await assert.rejects(readAvatar('http://127.0.0.1:1/photo.png'), /not downloaded here.*pass its path, or pass a data URL/)
+ await assert.rejects(readAvatar('HTTPS://example.com/photo.png'), /not downloaded here/)
})
diff --git a/ds-bot/test/roster.test.js b/ds-bot/test/roster.test.js
index 4c03fd31..aca17c01 100644
--- a/ds-bot/test/roster.test.js
+++ b/ds-bot/test/roster.test.js
@@ -764,8 +764,8 @@ test('list_models names the thinking levels of the models a Bot runs on', async
assert.doesNotMatch(result.split('\n').find(line => line.includes('deepseek/plain')), /thinking/)
})
-// #320: create_bot and update_bot take a photo from a local path, a data URL, or an
-// http(s) link, and store it in the same data URL the settings page writes.
+// #320: create_bot and update_bot take a photo from a local path or a data URL, and
+// store it in the same data URL the settings page writes.
const PNG_BYTES = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), Buffer.alloc(24, 1)])
const PNG_DATA_URL = `data:image/png;base64,${PNG_BYTES.toString('base64')}`
const tempAvatarDir = (t) => {
@@ -781,7 +781,8 @@ test('create_bot and update_bot advertise an optional photo', async (t) => {
const schema = ctx.registeredTools.get(name).parameters
assert.equal(schema.properties.avatar?.type, 'string')
assert.equal(schema.required?.includes('avatar') ?? false, false)
- assert.match(schema.properties.avatar.description, /local image path.*http\(s\) link.*data URL/)
+ assert.match(schema.properties.avatar.description, /local image path.*data URL/)
+ assert.doesNotMatch(schema.properties.avatar.description, /http/)
assert.match(schema.properties.avatar.description, /256x256/)
}
})