Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
/research: web search and page reading with a structured answer and sources; http driver commands for plan, mode switch and long chats
Niko1221998e9df95976
diff --git a/apps/host/scripts/agent-http.mjs b/apps/host/scripts/agent-http.mjsindex 2f18b0bd1..cd111a301 100644--- a/apps/host/scripts/agent-http.mjs+++ b/apps/host/scripts/agent-http.mjs@@ -32,8 +32,8 @@ async function model() {}/** One chat stream; returns the frames. `print` shows a compact line per frame, deltas and reasoning as counters. */-async function stream(body, { quiet = false } = {}) {- const res = await fetch(BASE + '/api/chat/stream', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });+async function stream(body, { quiet = false, path = '/api/chat/stream', onFrame = null } = {}) {+ const res = await fetch(BASE + path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });if (!res.ok) throw new Error(`stream -> ${res.status} ${await res.text()}`);const frames = []; let buffer = '', text = 0, reasoning = 0, lastLine = '';const flushCounters = () => { if (text || reasoning) { console.log(`${stamp()} ... ${reasoning} reasoning chars, ${text} answer chars`); text = 0; reasoning = 0; } };@@ -45,6 +45,7 @@ async function stream(body, { quiet = false } = {}) {const lines = buffer.split('\n'); buffer = lines.pop() ?? '';for (const line of lines.filter(Boolean)) {const f = JSON.parse(line); f._at = Date.now(); frames.push(f);+ try { onFrame?.(f); } catch (e) { console.log('onFrame error', e.message); }if (f.type === 'delta') { text += (f.text ?? '').length; continue; }if (f.type === 'reasoning') { reasoning += (f.text ?? '').length; continue; }if (f.type === 'stats') continue;@@ -57,6 +58,10 @@ async function stream(body, { quiet = false } = {}) {// ANSWER=approve|deny answers every card that asks (the card is the real Approve/Deny card of Manual mode)if (f.tool.state === 'proposed' && process.env.ANSWER) void j('/api/chat/approval', { sessionId: body.sessionId, toolId: f.tool.id, decision: process.env.APPROVE_MATCH && new RegExp(process.env.APPROVE_MATCH, 'i').test(`${f.tool.target} ${f.tool.effect}`) ? 'approve' : process.env.ANSWER }).then(() => console.log(`${stamp()} -> ${process.env.ANSWER} ${f.tool.id}`), e => console.log(`${stamp()} answer failed: ${e.message}`));}+ else if (f.type === 'plan') out = `PLAN ${f.event} ${f.plan.state} "${f.plan.title}" steps=${f.plan.steps.map(x => x.state[0] + ':' + x.text.slice(0, 40)).join(' | ')}`;+ else if (f.type === 'plan-step') out = `PLAN-STEP ${f.line}${f.summary ? ' | summary: ' + f.summary : ''}`;+ else if (f.type === 'queued-message') out = `QUEUED-MESSAGE ${f.state} "${f.message.text.slice(0, 50)}" deliverAt=${f.message.deliverAt}${f.afterStep ? ' afterStep=' + f.afterStep : ''} queue=${f.queue.length}`;+ else if (f.type === 'queued-release') out = `QUEUED-RELEASE ${f.messages.map(m => m.text.slice(0, 40)).join(' | ')}`;else if (f.type === 'done') out = `done ${f.finishReason} tokensOut=${f.tokensOut}`;else if (f.type === 'error') out = `ERROR ${f.message}`;if (!quiet && out !== lastLine) console.log(`${stamp()} ${out}`);@@ -71,7 +76,7 @@ async function stream(body, { quiet = false } = {}) {if (command === 'connect') {const [baseUrl, modelId] = rest;- await j('/api/model-providers', { kind: 'custom', name: 'Cloud model', baseUrl, protocol: 'openai-chat', apiKey: '', models: [{ id: modelId, name: 'Test model' }] });+ await j('/api/model-providers', { kind: 'custom', name: 'Cloud model', baseUrl, protocol: 'openai-chat', apiKey: '', models: [{ id: modelId, name: 'Test model', ...(process.env.CTX ? { contextMax: Number(process.env.CTX) } : {}), ...(process.env.MAXOUT ? { maxOutput: Number(process.env.MAXOUT) } : {}) }] });console.log('saved');} else if (command === 'stream') {const [message, mode = 'bypass'] = rest;@@ -134,6 +139,57 @@ if (command === 'connect') {}await sleep(4000);}+} else if (command === 'plan') {+ // /plan <task>, then (unless STOP_AT_PLAN) Start as goal with an optional queued message: plan "<task>" ["<queued message>"]+ const [task, queued] = rest;+ const chat = process.env.CHAT ?? `pl-${Date.now()}`;+ const common = { sessionId: chat, mode: process.env.MODE ?? 'manual', remoteModel: await model(), workspaceId: process.env.WORKSPACE_ID };+ console.log(`${stamp()} -- /plan`);+ const frames = await stream({ ...common, message: `/plan ${task}`});+ const plan = frames.find(f => f.type === 'plan')?.plan;+ if (!plan) { console.log('NO PLAN FRAME (the reply stays plain text)'); process.exit(1); }+ console.log(JSON.stringify(plan.steps.map(x => x.text)));+ console.log(`${stamp()} -- GET /api/plan`, (await j(`/api/plan?sessionId=${chat}`)).plan?.state);+ if (process.env.EDIT_LAST) { const steps = plan.steps.map(x => x.text); steps.push(process.env.EDIT_LAST); console.log(`${stamp()} -- edit`, (await j('/api/plan/edit', { sessionId: chat, planId: plan.id, steps })).plan.steps.length, 'steps'); }+ if (process.env.STOP_AT_PLAN) { console.log(`${stamp()} -- ${process.env.STOP_AT_PLAN}`, JSON.stringify((await j('/api/plan/' + process.env.STOP_AT_PLAN, { sessionId: chat, planId: plan.id })).plan.state)); process.exit(0); }+ console.log(`${stamp()} -- start as goal`);+ let sent = false;+ const run = stream({ ...common, planId: plan.id }, { path: '/api/plan/start-goal', onFrame: f => {+ if (queued && !sent && f.type === 'plan-step' && f.state === 'running') { sent = true; setTimeout(async () => { try { console.log(`${stamp()} -- queue`, JSON.stringify((await j('/api/chat/queue', { sessionId: chat, message: queued })).message)); } catch (e) { console.log('queue failed', e.message); } }, Number(process.env.QUEUE_AFTER ?? 8) * 1000); }+ } });+ await run;+ console.log(`${stamp()} -- final plan`, JSON.stringify((await j(`/api/plan?sessionId=${chat}`)).plan?.steps.map(x => x.state + ':' + x.text.slice(0, 30))));+} else if (command === 'modeswitch') {+ // Manual run that needs several approvals; after the first card the chat is switched to Bypass: no further card may ask.+ const [message] = rest;+ const chat = process.env.CHAT ?? `ms-${Date.now()}`;+ const common = { sessionId: chat, mode: 'manual', remoteModel: await model(), workspaceId: process.env.WORKSPACE_ID };+ let asked = 0, switched = false;+ const frames = await stream({ ...common, message }, { onFrame: async f => {+ if (f.type === 'agent-tool' && f.tool.state === 'proposed') {+ asked++;+ if (!switched) { switched = true; console.log(`${stamp()} -- switching to Bypass`, JSON.stringify(await j('/api/chat/mode', { sessionId: chat, mode: 'bypass' }))); }+ }+ } });+ const done = frames.filter(f => f.type === 'agent-tool' && f.tool.state === 'done').length;+ console.log(`${stamp()} RESULT cards that asked=${asked} (expected 1), tools done=${done}`);+} else if (command === 'long') {+ // Fill the context with long pastes so the Harness has to compact, then ask about the first one: long <rounds> <chars>+ const [rounds = '6', chars = '16000'] = rest;+ const chat = process.env.CHAT ?? `lg-${Date.now()}`;+ const common = { sessionId: chat, mode: 'bypass', remoteModel: await model(), workspaceId: process.env.WORKSPACE_ID };+ const filler = n => Array.from({ length: Math.ceil(Number(chars) / 60) }, (_, i) => `Entry ${n}-${i}: the quick brown fox number ${i * 7 + n} jumps over the lazy dog.`).join('\n');+ let frames = await stream({ ...common, message: 'Remember this code word for later: PAGODA-7731. Reply with just OK.' }, { quiet: true });+ console.log(`${stamp()} round 0 done, ctx=${frames.at(-1).contextTokens ?? "?"}`);+ for (let n = 1; n <= Number(rounds); n++) {+ frames = await stream({ ...common, message: `Here is log chunk ${n}. Reply with just the word OK.++${filler(n)}`}, { quiet: true });+ const d = frames.at(-1);+ console.log(`${stamp()} round ${n} ${d.type} ${d.finishReason ?? d.message ?? ""} tokensIn=${d.tokensIn} ctx=${d.contextTokens ?? d.context ?? "?"}`);+ }+ frames = await stream({ ...common, message: 'What was the code word I asked you to remember at the very start? Answer with the code word only.' }, { quiet: true });+ console.log(`${stamp()} ANSWER: ${frames.filter(f => f.type === "delta").map(f => f.text).join("").trim().slice(0, 120)}`);} else {- console.error('commands: connect | stream | goal | timeskip'); process.exit(2);+ console.error('commands: connect | stream | goal | timeskip | plan | modeswitch | long'); process.exit(2);}diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjsindex 68ed4a5ab..01e82113e 100644--- a/apps/host/server/agent/agent-bridge.mjs+++ b/apps/host/server/agent/agent-bridge.mjs@@ -23,7 +23,7 @@ import { StatusReporter } from './status.mjs';import { buildProfile, routeFor, MODE_PRESETS, modeOf, effortOf } from './profile.mjs';import { TurnProjector, parseArguments as parseArgs } from './events.mjs';import { ChildProjector } from './children.mjs';-import { PlanStore, planPrompt, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, planAsPrompt, editedSteps, planView } from './plan.mjs';+import { PlanStore, planPrompt, parsePlan, parsePoints, stepObjective, stepLine, firstSentence, planAsPrompt, editedSteps, planView, researchPrompt, parseSources } from './plan.mjs';/*** A failure the person can read. `message` is a plain sentence with the next step; `details` keeps the technical@@ -246,7 +246,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =try { return await runTurnInner(c, options); } finally { c.starting = false; }}- async function runTurnInner(c, { chatId, message, workspacePath, choice = null, mode = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal, goal = null, thinking = null, planTask = null, planRun = null }) {+ async function runTurnInner(c, { chatId, message, workspacePath, choice = null, mode = null, history = [], onFrame, requestId = `req-${randomUUID()}`, signal, goal = null, thinking = null, planTask = null, planRun = null, researchTopic = null }) {let send = onFrame; let attached = true;signal?.addEventListener('abort', () => { send = () => {}; attached = false; }, { once: true });// What the assistant itself wrote in this run (not our own step lines): a plan turn reads its plan from it, a step reads its summary.@@ -306,6 +306,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =if (turn.stopping && !failure) projector.ended = { kind: 'aborted', reason: { kind: 'user' } };reporter.clear();const extra = [];+ if (researchTopic && !failure && !turn.stopping) { const sources = parseSources(written.all); if (sources.length) extra.push({ type: 'research', topic: researchTopic, sources }); }try { extra.push(...settlePlan(failure)); } catch { /* a plan frame must never break the end of a turn */ }for (const f of extra) emit(f);const frames = failure ? [{ type: 'error', message: plainAgentFailure(failure), details: failure, code: 'AGENT_STOPPED' }] : projector.finish({ model: route.name, contextMax: route.contextWindow });@@ -788,6 +789,17 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =return runTurn({ ...options, message: planPrompt(task), planTask: task, mode: 'plan' });}+ /**+ * "/research <topic>": the assistant searches the web, opens and reads pages, and writes a short structured answer (Summary, Key findings,+ * Sources). The host also sends the sources as a `research` frame before the end of the turn. Needs web search turned on.+ */+ async function researchCommand(options) {+ const topic = String(options.line ?? '').trim();+ if (!topic) return sayOnce(options, 'Type /research followed by what you want to know, for example: /research how do heat pumps compare with gas boilers. I will search the web, read the pages and answer with sources.');+ if (tools()?.off?.has('search')) return sayOnce(options, 'Research needs web search, which is turned off. Turn it on in Settings, Tools (DuckDuckGo needs no key), then try again.');+ return runTurn({ ...options, message: researchPrompt(topic), researchTopic: topic });+ }+const planGone = () => new AgentUnavailable('There is no plan waiting in this chat. Ask for one with /plan followed by what you want done.', 'plan-gone');function planOf(chatId, planId) {const plan = plans.get(chatId);@@ -976,7 +988,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir =/** Let go of a chat's Harness session mapping (a task starts every run in a session of its own). */function forget(chatId) { if (chats.get(chatId)?.turn) return; delete sessionMap[chatId]; chats.delete(chatId); persist(); }- return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, planCommand, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) };+ return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, planCommand, researchCommand, planGoal, planControl, planState, queueMessage, cancelQueued, takeQueue, forget, approve, setMode, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) };}/** A goal that stopped by itself, in words. */diff --git a/apps/host/server/agent/plan.mjs b/apps/host/server/agent/plan.mjsindex 9302f3dcb..aeca32484 100644--- a/apps/host/server/agent/plan.mjs+++ b/apps/host/server/agent/plan.mjs@@ -187,3 +187,44 @@ export class PlanStore {/** What the page gets: a copy with no host-only fields. */export const planView = plan => (plan ? JSON.parse(JSON.stringify(plan)) : null);++// ---- /research ------------------------------------------------------------------------------------------------------------------++/** The instruction a research turn gets in front of the person's topic. */+export function researchPrompt(topic) {+ return [+ `Research this topic for me using the web: ${topic}`,+ '',+ 'How to work:',+ '1. Search the web with the web_search tool (try two or three different searches).',+ '2. Open the most useful pages with the web_fetch tool and read them. Use at least three different sources when you can find them.',+ '3. Write only what the pages say. If sources disagree or you could not find something, say so plainly.',+ '',+ 'Then write your answer in exactly this shape, in plain words:',+ '## Summary',+ 'Three to five sentences.',+ '## Key findings',+ '- One finding per line, each ending with its source number in square brackets, like [1].',+ '## Sources',+ '1. Page title - web address',+ '2. Page title - web address',+ ].join('\n');+}++/** The numbered sources at the end of a research answer: [{ n, title, url }]. Empty when there are none. */+export function parseSources(text) {+ const lines = String(text ?? '').replace(/\r/g, '').split('\n');+ const at = lines.map(l => /^\s{0,3}#{1,6}\s*sources\b/i.test(l) || /^\s*\*{0,2}sources\*{0,2}:?\s*$/i.test(l)).lastIndexOf(true);+ if (at < 0) return [];+ const out = [];+ for (const line of lines.slice(at + 1)) {+ if (/^\s{0,3}#{1,6}\s/.test(line)) break;+ const url = /(https?:\/\/[^\s)>\]"']+)/.exec(line)?.[1]?.replace(/[.,;]+$/, '');+ if (!url) continue;+ const md = /\[([^\]]+)\]\(https?:\/\//.exec(line);+ const before = line.slice(0, line.indexOf(url)).replace(/^\s*(?:\d+[.)]|[-*•])\s*/, '').replace(/[\s\-–—:(<[]+$/, '').replace(/\*\*|`/g, '').trim();+ const title = clean(md?.[1] ?? before) || (() => { try { return new URL(url).hostname; } catch { return url; } })();+ if (!out.some(s => s.url === url)) out.push({ n: out.length + 1, title: clip(title, 120), url });+ }+ return out.slice(0, 20);+}diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjsindex 6688b7281..39144594e 100644--- a/apps/host/server/index.mjs+++ b/apps/host/server/index.mjs@@ -524,8 +524,10 @@ async function runAgentTurn({ res, controller, chatId, body, message, workspaceI// "/goal ..." is the Harness's goal command (set, show, pause, resume, clear); everything else is a message.const goalLine = /^\/goal(?:\s+([\s\S]*))?$/i.exec(message.trim());const planLine = /^\/plan(?:\s+([\s\S]*))?$/i.exec(message.trim());+ const researchLine = /^\/research(?:\s+([\s\S]*))?$/i.exec(message.trim());const turnOptions = { chatId, message, workspacePath: cwd, choice, mode: typeof body.mode === 'string' ? body.mode : null, thinking: typeof body.thinking === 'string' ? body.thinking : null, history: Array.isArray(body.history) ? body.history : [], onFrame: write, signal: controller.signal };const result = typeof body.planId === 'string' && body.message === '/plan start' ? await agent.planGoal({ ...turnOptions, planId: body.planId })+ : researchLine ? await agent.researchCommand({ ...turnOptions, line: researchLine[1] ?? '' }): planLine ? await agent.planCommand({ ...turnOptions, line: planLine[1] ?? '' }): goalLine ? await agent.goalCommand({ ...turnOptions, line: goalLine[1] ?? '' }) : await agent.runTurn(turnOptions);if (result?.detached) void result.ended.then(r => settle(r?.tokensOut ?? null), () => settle(null));diff --git a/apps/host/tests/agent-plan.test.mjs b/apps/host/tests/agent-plan.test.mjsindex 02418c578..9f9fd1646 100644--- a/apps/host/tests/agent-plan.test.mjs+++ b/apps/host/tests/agent-plan.test.mjs@@ -78,3 +78,23 @@ test('plans survive a restart', () => {b.clear('chat-1'); assert.equal(new PlanStore(file).get('chat-1'), null);} finally { rmSync(dir, { recursive: true, force: true }); }});++import { parseSources, researchPrompt } from '../server/agent/plan.mjs';++test('research: the answer shape is asked for, and the sources are read back as [{n, title, url}]', () => {+ assert.ok(researchPrompt('heat pumps').includes('web_search') && researchPrompt('heat pumps').includes('## Sources'));+ const answer = [+ '## Summary', 'Heat pumps move heat.', '## Key findings', '- They are efficient [1].', '- They cost more up front [2].',+ '## Sources',+ '1. Heat pump basics - https://energy.example.org/heat-pumps',+ '2. [Costs explained](https://costs.example.com/page?x=1).',+ '3. https://plain.example.net/a',+ '4. Heat pump basics - https://energy.example.org/heat-pumps',+ ].join('\n');+ assert.deepEqual(parseSources(answer), [+ { n: 1, title: 'Heat pump basics', url: 'https://energy.example.org/heat-pumps' },+ { n: 2, title: 'Costs explained', url: 'https://costs.example.com/page?x=1' },+ { n: 3, title: 'plain.example.net', url: 'https://plain.example.net/a' },+ ]);+ assert.deepEqual(parseSources('No sources section here, see https://x.example'), []);+});