Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d67ebb0c77 |
@@ -130,25 +130,6 @@ export function scrubUserMessageImageAttachments(message) {
|
||||
};
|
||||
}
|
||||
|
||||
export function messageContentHasImageUrl(content) {
|
||||
if (!Array.isArray(content)) return false;
|
||||
return content.some((item) => item?.type === 'image_url' && item?.image_url?.url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Any persisted image_url part will break DeepSeek / other text-only providers.
|
||||
* User metadata.imageUrls alone is not enough — Goose may have expanded them into
|
||||
* content parts on assistant or user turns.
|
||||
*/
|
||||
export function conversationHasImageUrlContent(conversation, { excludeMessageId = null } = {}) {
|
||||
if (!Array.isArray(conversation)) return false;
|
||||
const excluded = String(excludeMessageId ?? '').trim();
|
||||
return conversation.some((message) => {
|
||||
if (excluded && String(message?.id ?? '').trim() === excluded) return false;
|
||||
return messageContentHasImageUrl(message?.content);
|
||||
});
|
||||
}
|
||||
|
||||
export function scrubConversationHistoricalImageAttachments(conversation, activeMessageId) {
|
||||
const activeId = String(activeMessageId ?? '').trim();
|
||||
if (!Array.isArray(conversation) || !activeId) {
|
||||
|
||||
@@ -2,7 +2,6 @@ import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
buildCurrentTurnImageScopeNote,
|
||||
conversationHasImageUrlContent,
|
||||
dedupeImageUrlsByAssetKey,
|
||||
extractCurrentTurnImageUrls,
|
||||
scrubConversationHistoricalImageAttachments,
|
||||
@@ -114,46 +113,3 @@ test('buildCurrentTurnImageScopeNote states one independent topic per upload', (
|
||||
assert.match(note, /不得与历史轮次混用/);
|
||||
assert.match(note, /asset=asset-9/);
|
||||
});
|
||||
|
||||
test('conversationHasImageUrlContent detects historical poison and ignores active turn', () => {
|
||||
const conversation = [
|
||||
{
|
||||
id: 'assistant-old',
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: '看图' },
|
||||
{ type: 'image_url', image_url: { url: 'https://example.com/old.png' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'user-new',
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: '这是什么' },
|
||||
{ type: 'image_url', image_url: { url: 'https://example.com/new.png' } },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
assert.equal(conversationHasImageUrlContent(conversation), true);
|
||||
assert.equal(
|
||||
conversationHasImageUrlContent(conversation, { excludeMessageId: 'user-new' }),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
conversationHasImageUrlContent(
|
||||
[
|
||||
{
|
||||
id: 'user-new',
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: '这是什么' },
|
||||
{ type: 'image_url', image_url: { url: 'https://example.com/new.png' } },
|
||||
],
|
||||
},
|
||||
],
|
||||
{ excludeMessageId: 'user-new' },
|
||||
),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -164,7 +164,7 @@ test('buildAutoChatSkillPrefix routes an uploaded xlsx only when Excel Analyst i
|
||||
|
||||
test('manifest enhanced search route is opt-in and uses the MindSearch skill prompt', () => {
|
||||
const routes = [{ skillName: 'search-enhanced', promptKey: 'search-enhanced', keywords: ['最新资料'], priority: 30 }];
|
||||
assert.match(buildAutoChatSkillPrefix('请搜索最新资料:Goose MCP', ['search-enhanced'], { skillRouterV2: true, manifestRoutes: routes }), /tkmind-search/);
|
||||
assert.match(buildAutoChatSkillPrefix('请搜索最新资料:Goose MCP', ['search-enhanced'], { skillRouterV2: true, manifestRoutes: routes }), /tkmind_search/);
|
||||
assert.equal(buildAutoChatSkillPrefix('请搜索最新资料:Goose MCP', [], { skillRouterV2: true, manifestRoutes: routes }), '');
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ const BLOCKED_ACTIVE_PATTERNS = [
|
||||
const SCRIPT_TAG_PATTERN = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
|
||||
const SCRIPT_SRC_PATTERN = /\bsrc\s*=\s*(['"])([^'"]+)\1/i;
|
||||
const TRUSTED_SCRIPT_SRC_PATTERNS = [
|
||||
/^\/assets\/page-data-client\.js(?:[?#].*)?$/i,
|
||||
/^\/assets\/chart\.umd\.min\.js(?:[?#].*)?$/i,
|
||||
/^https:\/\/cdn\.jsdelivr\.net\/npm\/chart\.js(?:@[^/]+)?\/dist\/chart\.(?:umd\.)?min\.js(?:[?#].*)?$/i,
|
||||
/^https:\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/Chart\.js\/[^/]+\/chart\.(?:umd\.)?min\.js(?:[?#].*)?$/i,
|
||||
|
||||
@@ -35,21 +35,6 @@ test('runBasicFileScan warns for sandboxed html with inline script and trusted C
|
||||
assert.deepEqual(result.findings, ['trusted_html_active_content']);
|
||||
});
|
||||
|
||||
test('runBasicFileScan warns for sandboxed html with page-data-client and inline script', () => {
|
||||
const result = runBasicFileScan(
|
||||
Buffer.from(`<!doctype html>
|
||||
<script src="/assets/page-data-client.js"></script>
|
||||
<script>MindSpacePageData.createClient({ apiBase: "/api" });</script>`),
|
||||
{
|
||||
filename: 'survey.html',
|
||||
mimeType: 'text/html',
|
||||
htmlActiveContentPolicy: 'sandbox_warn',
|
||||
},
|
||||
);
|
||||
assert.equal(result.scanStatus, 'warned');
|
||||
assert.deepEqual(result.findings, ['trusted_html_active_content']);
|
||||
});
|
||||
|
||||
test('runBasicFileScan still blocks unsafe html active content in sandbox mode', () => {
|
||||
const javascriptUrl = runBasicFileScan(Buffer.from('<a href="javascript:alert(1)">go</a>'), {
|
||||
filename: 'dashboard.html',
|
||||
|
||||
@@ -88,9 +88,6 @@ export function createPageDataBrowserClient({
|
||||
async listRows(dataset, query = {}) {
|
||||
return request('GET', buildPageDataPublicPath(apiBase, pageId, dataset), { query });
|
||||
},
|
||||
async readRows(dataset, query = {}) {
|
||||
return request('GET', buildPageDataPublicPath(apiBase, pageId, dataset), { query });
|
||||
},
|
||||
async getSchema(dataset) {
|
||||
return request('GET', buildPageDataPublicPath(apiBase, pageId, dataset, 'schema'));
|
||||
},
|
||||
|
||||
@@ -106,9 +106,6 @@
|
||||
listRows: function (dataset, query) {
|
||||
return request('GET', buildDataPath(apiBase, pageId, dataset), { query: query || {} });
|
||||
},
|
||||
readRows: function (dataset, query) {
|
||||
return request('GET', buildDataPath(apiBase, pageId, dataset), { query: query || {} });
|
||||
},
|
||||
getSchema: function (dataset) {
|
||||
return request('GET', buildDataPath(apiBase, pageId, dataset, 'schema'));
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import fsp from 'node:fs/promises';
|
||||
import net from 'node:net';
|
||||
import path from 'node:path';
|
||||
|
||||
import mysql from 'mysql2/promise';
|
||||
@@ -13,6 +14,37 @@ import { assertNonProductionTarget } from './safety.mjs';
|
||||
const { Client: PgClient } = pg;
|
||||
const SAFE_DB_NAME = /^[a-z][a-z0-9_]{5,62}$/;
|
||||
|
||||
const ISOLATED_GATE_REMOTE_ENV_KEYS = [
|
||||
'MINDSPACE_REMOTE_BASE_URL',
|
||||
'MINDSPACE_REMOTE_AUTH_TOKEN',
|
||||
'MINDSPACE_MCP_BASE_URL',
|
||||
'MINDSPACE_MCP_TOKEN_SECRET',
|
||||
];
|
||||
|
||||
/**
|
||||
* Release gate stacks must not inherit split-service MindSpace MCP routing from
|
||||
* the developer .env. Scoped MCP tokens would target the standalone 8082 service
|
||||
* and resolve workspace paths against the host H5 root instead of the isolated
|
||||
* gate sandbox, causing sandbox-fs write_file/publish_page ENOENT failures.
|
||||
*/
|
||||
export function sanitizeIsolatedGatePortalEnv(
|
||||
env,
|
||||
{ port, runtimeProfile = 'local' } = {},
|
||||
) {
|
||||
const sanitized = { ...env };
|
||||
for (const key of ISOLATED_GATE_REMOTE_ENV_KEYS) {
|
||||
delete sanitized[key];
|
||||
}
|
||||
sanitized.MEMIND_RUNTIME_PROFILE = runtimeProfile;
|
||||
sanitized.MINDSPACE_SERVER_ADAPTER = 'local';
|
||||
if (port != null) {
|
||||
const portalBase = `http://127.0.0.1:${port}`;
|
||||
sanitized.H5_PORTAL_BASE_URL = portalBase;
|
||||
sanitized.MINDSPACE_AGENT_API_BASE_URL = `${portalBase}/api`;
|
||||
}
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
function assertLoopbackHost(host, label) {
|
||||
const normalized = String(host ?? '').toLowerCase();
|
||||
if (!['localhost', '127.0.0.1', '::1', '/tmp'].includes(normalized)) {
|
||||
@@ -173,14 +205,44 @@ export async function selectBackendLlmProvider({
|
||||
}
|
||||
}
|
||||
|
||||
export function assertGatePortAvailable(port, host = '127.0.0.1') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = net.createServer();
|
||||
server.once('error', (error) => {
|
||||
if (error?.code === 'EADDRINUSE') {
|
||||
reject(new Error(
|
||||
`Release gate port ${host}:${port} is already in use; stop the stale local gate process before retrying`,
|
||||
));
|
||||
return;
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
server.once('listening', () => {
|
||||
server.close((closeError) => {
|
||||
if (closeError) reject(closeError);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
server.listen(port, host);
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForPortal(baseUrl, child, timeoutMs = 60_000) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (child.exitCode !== null) throw new Error(`isolated Portal exited with code ${child.exitCode}`);
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/auth/status`, { signal: AbortSignal.timeout(1_000) });
|
||||
if (response.ok) return;
|
||||
} catch {
|
||||
if (response.ok) {
|
||||
if (child.exitCode !== null) {
|
||||
throw new Error(`isolated Portal exited with code ${child.exitCode} after health check`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('isolated Portal exited')) {
|
||||
throw error;
|
||||
}
|
||||
// Startup can take several seconds while the isolated schema is initialized.
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||
@@ -188,6 +250,42 @@ async function waitForPortal(baseUrl, child, timeoutMs = 60_000) {
|
||||
throw new Error(`isolated Portal did not become ready: ${baseUrl}`);
|
||||
}
|
||||
|
||||
export async function grantGateUserSkillsByUsername({
|
||||
targetUrl,
|
||||
username,
|
||||
skillNames,
|
||||
}) {
|
||||
assertNonProductionTarget(targetUrl, 'isolated gate database');
|
||||
const normalizedUsername = String(username ?? '').trim();
|
||||
const skills = [...new Set(
|
||||
(Array.isArray(skillNames) ? skillNames : [])
|
||||
.map((name) => String(name ?? '').trim())
|
||||
.filter(Boolean),
|
||||
)];
|
||||
if (!normalizedUsername || skills.length === 0) return { userId: null, granted: [] };
|
||||
const target = await mysql.createConnection(targetUrl);
|
||||
try {
|
||||
const [rows] = await target.query(
|
||||
'SELECT id FROM h5_users WHERE username = ? LIMIT 1',
|
||||
[normalizedUsername],
|
||||
);
|
||||
const userId = rows[0]?.id ?? null;
|
||||
if (!userId) throw new Error(`Release gate user not found for skill grant: ${normalizedUsername}`);
|
||||
const now = Date.now();
|
||||
for (const skillName of skills) {
|
||||
await target.execute(
|
||||
`INSERT INTO h5_user_skill_grants (subject_type, subject_id, skill_name, enabled, updated_at)
|
||||
VALUES ('user', ?, ?, 1, ?)
|
||||
ON DUPLICATE KEY UPDATE enabled = VALUES(enabled), updated_at = VALUES(updated_at)`,
|
||||
[userId, skillName, now],
|
||||
);
|
||||
}
|
||||
return { userId, granted: skills };
|
||||
} finally {
|
||||
await target.end();
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForHttpHealth(baseUrl, child, label, timeoutMs = 30_000) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
@@ -377,6 +475,7 @@ export async function createLocalGateStack({
|
||||
async function startPortal() {
|
||||
if (!childEnv || !baseUrl) throw new Error('isolated Portal environment is not initialized');
|
||||
if (child && child.exitCode === null) throw new Error('isolated Portal is already running');
|
||||
await assertGatePortAvailable(port);
|
||||
deepseekProxyLogFd = fs.openSync(deepseekProxyLogPath, 'a');
|
||||
deepseekProxyChild = spawn(
|
||||
process.execPath,
|
||||
@@ -408,10 +507,10 @@ export async function createLocalGateStack({
|
||||
try {
|
||||
mysqlUrl = await createMysqlDatabase(baseEnv.DATABASE_URL, mysqlDatabase);
|
||||
pgUrl = await createPgDatabase(baseEnv.MINDSPACE_USERDATA_PG_URL, pgDatabase);
|
||||
childEnv = sanitizeIsolatedGatePortalEnv(baseEnv, { port, runtimeProfile });
|
||||
childEnv = {
|
||||
...baseEnv,
|
||||
...childEnv,
|
||||
NODE_ENV: nodeEnv,
|
||||
...(runtimeProfile ? { MEMIND_RUNTIME_PROFILE: runtimeProfile } : {}),
|
||||
H5_HOST: '127.0.0.1',
|
||||
H5_PORT: String(port),
|
||||
H5_PUBLIC_BASE_URL: `http://127.0.0.1:${port}`,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import net from 'node:net';
|
||||
import test from 'node:test';
|
||||
|
||||
import {
|
||||
assertGatePortAvailable,
|
||||
buildContainerPgConnectionString,
|
||||
buildPgConnectionString,
|
||||
makeIsolatedDatabaseName,
|
||||
sanitizeIsolatedGatePortalEnv,
|
||||
} from './local-stack.mjs';
|
||||
|
||||
test('isolated database names are bounded and identifier-safe', () => {
|
||||
@@ -41,3 +44,40 @@ test('container PostgreSQL DSN keeps credentials and host while isolating databa
|
||||
test('isolated database names reject empty or unsafe prefixes', () => {
|
||||
assert.throws(() => makeIsolatedDatabaseName('abc', '../bad'), /Unsafe/);
|
||||
});
|
||||
|
||||
test('sanitizeIsolatedGatePortalEnv drops split-service MCP routing', () => {
|
||||
const sanitized = sanitizeIsolatedGatePortalEnv({
|
||||
MINDSPACE_SERVER_ADAPTER: 'remote',
|
||||
MINDSPACE_REMOTE_BASE_URL: 'http://127.0.0.1:8082',
|
||||
MINDSPACE_REMOTE_AUTH_TOKEN: 'local-dev-secret',
|
||||
MINDSPACE_MCP_BASE_URL: 'http://127.0.0.1:8082',
|
||||
MINDSPACE_MCP_TOKEN_SECRET: 'local-dev-secret',
|
||||
MINDSPACE_AGENT_API_BASE_URL: 'http://127.0.0.1:8081/api',
|
||||
}, { port: 19087, runtimeProfile: 'local' });
|
||||
|
||||
assert.equal(sanitized.MINDSPACE_SERVER_ADAPTER, 'local');
|
||||
assert.equal(sanitized.MEMIND_RUNTIME_PROFILE, 'local');
|
||||
assert.equal(sanitized.H5_PORTAL_BASE_URL, 'http://127.0.0.1:19087');
|
||||
assert.equal(sanitized.MINDSPACE_AGENT_API_BASE_URL, 'http://127.0.0.1:19087/api');
|
||||
assert.equal(sanitized.MINDSPACE_REMOTE_BASE_URL, undefined);
|
||||
assert.equal(sanitized.MINDSPACE_MCP_BASE_URL, undefined);
|
||||
assert.equal(sanitized.MINDSPACE_MCP_TOKEN_SECRET, undefined);
|
||||
});
|
||||
|
||||
test('assertGatePortAvailable rejects occupied loopback ports', async () => {
|
||||
const server = net.createServer();
|
||||
await new Promise((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.listen(0, '127.0.0.1', resolve);
|
||||
});
|
||||
const address = server.address();
|
||||
assert.ok(address && typeof address === 'object');
|
||||
await assert.rejects(
|
||||
() => assertGatePortAvailable(address.port),
|
||||
/already in use/,
|
||||
);
|
||||
await new Promise((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
});
|
||||
await assertGatePortAvailable(address.port);
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import path from 'node:path';
|
||||
|
||||
import {
|
||||
createLocalGateStack,
|
||||
grantGateUserSkillsByUsername,
|
||||
selectBackendLlmProvider,
|
||||
seedSelectedProviderKeys,
|
||||
} from '../release-gate/local-stack.mjs';
|
||||
@@ -16,8 +17,12 @@ const scenarioIds = [
|
||||
|
||||
async function runScenario(scenarioId, port) {
|
||||
const childTimeoutMs = Math.max(
|
||||
30_000,
|
||||
Number(process.env.RELEASE_GATE_SCENARIO_PROCESS_TIMEOUT_MS ?? 360_000) || 360_000,
|
||||
60_000,
|
||||
Number(process.env.RELEASE_GATE_SCENARIO_PROCESS_TIMEOUT_MS ?? 900_000) || 900_000,
|
||||
);
|
||||
const stepTimeoutMs = Math.max(
|
||||
60_000,
|
||||
Number(process.env.RELEASE_GATE_SCENARIO_TIMEOUT_MS ?? 900_000) || 900_000,
|
||||
);
|
||||
const code = await new Promise((resolve, reject) => {
|
||||
const child = spawn(
|
||||
@@ -28,9 +33,7 @@ async function runScenario(scenarioId, port) {
|
||||
env: {
|
||||
...process.env,
|
||||
JOHN_PASSWORD: '888888',
|
||||
RELEASE_GATE_SCENARIO_TIMEOUT_MS: String(
|
||||
Number(process.env.RELEASE_GATE_SCENARIO_TIMEOUT_MS ?? 300_000) || 300_000,
|
||||
),
|
||||
RELEASE_GATE_SCENARIO_TIMEOUT_MS: String(stepTimeoutMs),
|
||||
},
|
||||
stdio: 'inherit',
|
||||
},
|
||||
@@ -77,10 +80,18 @@ async function register(username) {
|
||||
|
||||
try {
|
||||
await register('john2');
|
||||
const results = await Promise.all(scenarioIds.map(async (scenarioId) => ({
|
||||
scenarioId,
|
||||
code: await runScenario(scenarioId, new URL(stack.baseUrl).port),
|
||||
})));
|
||||
await grantGateUserSkillsByUsername({
|
||||
targetUrl: stack.mysqlUrl,
|
||||
username: 'john2',
|
||||
skillNames: ['static-page-publish'],
|
||||
});
|
||||
const results = [];
|
||||
for (const scenarioId of scenarioIds) {
|
||||
results.push({
|
||||
scenarioId,
|
||||
code: await runScenario(scenarioId, new URL(stack.baseUrl).port),
|
||||
});
|
||||
}
|
||||
for (const result of results) {
|
||||
console.log(`${result.code === 0 ? 'PASS' : 'FAIL'} ${result.scenarioId}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Direct goosed page smoke on TKMIND_API_TARGET (default https://127.0.0.1:18006).
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { Readable } from 'node:stream';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { Agent, fetch } from 'undici';
|
||||
|
||||
import { buildChatSkillPrompt } from '../chat-skills.mjs';
|
||||
|
||||
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
|
||||
function loadEnvFile(filePath) {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith('#')) continue;
|
||||
const eq = trimmed.indexOf('=');
|
||||
if (eq < 0) continue;
|
||||
const key = trimmed.slice(0, eq).trim();
|
||||
const value = trimmed.slice(eq + 1).trim();
|
||||
if (!process.env[key]) process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
loadEnvFile(path.join(root, '.env'));
|
||||
|
||||
const secret = process.env.TKMIND_SERVER__SECRET_KEY ?? 'local-dev-secret';
|
||||
const base = process.env.TKMIND_API_TARGET ?? 'https://127.0.0.1:18006';
|
||||
const workingDir = path.resolve(
|
||||
process.argv[2] ?? path.join(root, '.release-gate/local/goosed-direct-page'),
|
||||
);
|
||||
const targetHtml = process.argv[3] ?? 'public/suzhou-goosed-direct.html';
|
||||
const provider = process.argv[4] ?? process.env.GOOSED_PAGE_TEST_PROVIDER ?? 'custom_tkmind_relay_deepseek';
|
||||
const model = process.argv[5] ?? process.env.GOOSED_PAGE_TEST_MODEL ?? 'deepseek-chat';
|
||||
const timeoutMs = Number(process.env.GOOSED_PAGE_TEST_TIMEOUT_MS ?? 600_000);
|
||||
|
||||
const dispatcher = new Agent({ connect: { rejectUnauthorized: false } });
|
||||
|
||||
async function apiFetch(pathname, init = {}) {
|
||||
const headers = {
|
||||
...(init.headers ?? {}),
|
||||
'X-Secret-Key': secret,
|
||||
};
|
||||
if (init.body && !headers['Content-Type']) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
return fetch(`${base}${pathname}`, {
|
||||
...init,
|
||||
headers,
|
||||
dispatcher,
|
||||
});
|
||||
}
|
||||
|
||||
async function apiJson(pathname, body) {
|
||||
const response = await apiFetch(pathname, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const text = await response.text();
|
||||
if (!response.ok) throw new Error(`${pathname} ${response.status}: ${text.slice(0, 800)}`);
|
||||
if (!text.trim()) return {};
|
||||
return JSON.parse(text);
|
||||
}
|
||||
|
||||
function messageVisibleText(message) {
|
||||
return (message?.content ?? [])
|
||||
.filter((item) => item?.type === 'text')
|
||||
.map((item) => String(item.text ?? ''))
|
||||
.join('\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
async function executeSessionReply(sessionId, requestId, prompt) {
|
||||
const eventsResponse = await apiFetch(`/sessions/${sessionId}/events`, {
|
||||
method: 'GET',
|
||||
headers: { Accept: 'text/event-stream' },
|
||||
});
|
||||
if (!eventsResponse.ok || !eventsResponse.body) {
|
||||
const text = await eventsResponse.text().catch(() => '');
|
||||
throw new Error(text || '无法建立 goosed 事件流');
|
||||
}
|
||||
|
||||
const replyResponse = await apiFetch(`/sessions/${sessionId}/reply`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
request_id: requestId,
|
||||
user_message: {
|
||||
role: 'user',
|
||||
created: Date.now(),
|
||||
content: [{ type: 'text', text: prompt }],
|
||||
metadata: { userVisible: true, agentVisible: true, displayText: prompt },
|
||||
},
|
||||
}),
|
||||
});
|
||||
if (!replyResponse.ok) {
|
||||
const text = await replyResponse.text().catch(() => '');
|
||||
throw new Error(text || 'reply 失败');
|
||||
}
|
||||
replyResponse.body?.cancel?.();
|
||||
|
||||
const reader = Readable.fromWeb(eventsResponse.body);
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
let messages = [];
|
||||
let finishSeen = false;
|
||||
let errorText = '';
|
||||
|
||||
const pushMessage = (list, message) => {
|
||||
const index = list.findIndex((item) => item.id === message.id);
|
||||
if (index >= 0) {
|
||||
const next = [...list];
|
||||
next[index] = message;
|
||||
return next;
|
||||
}
|
||||
return [...list, message];
|
||||
};
|
||||
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
for await (const chunk of reader) {
|
||||
if (Date.now() > deadline) throw new Error(`goosed 事件流超时 ${timeoutMs}ms`);
|
||||
buffer += decoder.decode(chunk, { stream: true });
|
||||
const frames = buffer.split('\n\n');
|
||||
buffer = frames.pop() ?? '';
|
||||
for (const frame of frames) {
|
||||
let data = '';
|
||||
for (const line of frame.split('\n')) {
|
||||
if (line.startsWith('data:')) data += line.slice(5).trim();
|
||||
}
|
||||
if (!data) continue;
|
||||
let event;
|
||||
try {
|
||||
event = JSON.parse(data);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const routingId = event.chat_request_id ?? event.request_id;
|
||||
if (routingId && routingId !== requestId) continue;
|
||||
|
||||
if (event.type === 'Message' && event.message?.metadata?.userVisible !== false) {
|
||||
messages = pushMessage(messages, event.message);
|
||||
} else if (event.type === 'UpdateConversation') {
|
||||
messages = (event.conversation ?? []).filter((item) => item.metadata?.userVisible !== false);
|
||||
} else if (event.type === 'Error') {
|
||||
errorText = String(event.error ?? event.message ?? 'goose 执行失败');
|
||||
throw new Error(errorText);
|
||||
} else if (event.type === 'Finish') {
|
||||
finishSeen = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (finishSeen) break;
|
||||
}
|
||||
|
||||
const assistantTexts = messages
|
||||
.filter((item) => item.role === 'assistant')
|
||||
.map((item) => messageVisibleText(item))
|
||||
.filter(Boolean);
|
||||
return {
|
||||
finishSeen,
|
||||
combined: assistantTexts.join('\n\n').trim(),
|
||||
toolCalls: messages.flatMap((item) => (item.content ?? [])
|
||||
.filter((part) => part?.type === 'toolRequest' || part?.type === 'toolResponse')
|
||||
.map((part) => ({
|
||||
role: item.role,
|
||||
type: part.type,
|
||||
name: part.toolCall?.value?.name ?? part.toolResponse?.value?.name ?? part.name ?? null,
|
||||
}))),
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
fs.mkdirSync(path.join(workingDir, 'public'), { recursive: true });
|
||||
console.log(`goosed base: ${base}`);
|
||||
console.log(`working_dir: ${workingDir}`);
|
||||
console.log(`target: ${targetHtml}`);
|
||||
console.log(`provider: ${provider} / ${model}`);
|
||||
|
||||
const start = await apiJson('/agent/start', { working_dir: workingDir });
|
||||
const sessionId = start.id;
|
||||
console.log(`session: ${sessionId}`);
|
||||
|
||||
await apiJson('/agent/update_provider', {
|
||||
session_id: sessionId,
|
||||
provider,
|
||||
model,
|
||||
});
|
||||
|
||||
const skillPrefix = buildChatSkillPrompt('generate-page', 'static-page-publish');
|
||||
const userText = `${skillPrefix}请帮我做一个全新的苏州一日游攻略页面,保存为 ${targetHtml},不要修改或复用已有页面,做完直接给我链接。`;
|
||||
const requestId = crypto.randomUUID();
|
||||
|
||||
const reply = await executeSessionReply(sessionId, requestId, userText);
|
||||
const htmlPath = path.join(workingDir, targetHtml);
|
||||
const htmlExists = fs.existsSync(htmlPath);
|
||||
const htmlSize = htmlExists ? fs.statSync(htmlPath).size : 0;
|
||||
|
||||
console.log('\n=== result ===');
|
||||
console.log(`finish_seen: ${reply.finishSeen}`);
|
||||
console.log(`html_exists: ${htmlExists}`);
|
||||
console.log(`html_bytes: ${htmlSize}`);
|
||||
console.log(`tool_calls: ${reply.toolCalls.length}`);
|
||||
for (const call of reply.toolCalls.slice(-12)) {
|
||||
console.log(` - ${call.role} ${call.type} ${call.name ?? ''}`);
|
||||
}
|
||||
if (reply.combined) {
|
||||
console.log('\n--- assistant excerpt ---');
|
||||
console.log(reply.combined.slice(-1500));
|
||||
}
|
||||
|
||||
if (!htmlExists || htmlSize <= 0) {
|
||||
console.error('\nFAIL: goosed did not materialize target HTML in working_dir');
|
||||
process.exit(1);
|
||||
}
|
||||
const html = fs.readFileSync(htmlPath, 'utf8');
|
||||
if (!/苏州/u.test(html)) {
|
||||
console.error('\nFAIL: generated HTML missing expected keyword 苏州');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('\nPASS: goosed wrote deliverable HTML on 18006');
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack ?? error.message : error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -140,35 +140,3 @@ test('buildVisionPayload does not mark billable usage when vision analysis fails
|
||||
assert.equal(result?.billableImageCount, 0);
|
||||
assert.doesNotMatch(result?.userMessage?.content?.[0]?.text ?? '', /Qwen VL 图片描述/);
|
||||
});
|
||||
|
||||
test('buildVisionPayload strips image_url content parts for text-only Goose providers', async () => {
|
||||
const result = await buildVisionPayload({
|
||||
userId: 'user-1',
|
||||
publishLayout: { publicUrl: 'https://m.tkmind.cn/MindSpace/user-1' },
|
||||
userMessage: {
|
||||
content: [
|
||||
{ type: 'text', text: '这是什么' },
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: { url: '/api/mindspace/v1/assets/asset-7/download?inline=1' },
|
||||
},
|
||||
],
|
||||
metadata: {
|
||||
imageUrls: ['/api/mindspace/v1/assets/asset-7/download?inline=1'],
|
||||
},
|
||||
},
|
||||
localFetchAsset: async () => ({
|
||||
buffer: Buffer.from('fake-image'),
|
||||
mimeType: 'image/png',
|
||||
}),
|
||||
llmProviderService: {
|
||||
analyzeImagesWithVision: async () => '蓝色方块',
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(
|
||||
(result?.userMessage?.content ?? []).some((item) => item?.type === 'image_url'),
|
||||
false,
|
||||
);
|
||||
assert.match(result?.userMessage?.content?.[0]?.text ?? '', /蓝色方块/);
|
||||
});
|
||||
|
||||
+7
-53
@@ -34,7 +34,6 @@ import {
|
||||
import { extractAttachmentText } from './mindspace-attachment-text.mjs';
|
||||
import {
|
||||
buildCurrentTurnImageScopeNote,
|
||||
conversationHasImageUrlContent,
|
||||
extractCurrentTurnImageUrls,
|
||||
scrubConversationHistoricalImageAttachments,
|
||||
} from './chat-image-turn-scope.mjs';
|
||||
@@ -975,9 +974,6 @@ export async function buildVisionPayload({
|
||||
'不要向用户展示 HTML 代码块。';
|
||||
|
||||
let updatedContent = Array.isArray(userMessage?.content) ? [...userMessage.content] : [];
|
||||
// Text-only Goose providers cannot accept image_url parts. After VL analysis,
|
||||
// keep only text (with the injected vision note) for the agent turn.
|
||||
updatedContent = updatedContent.filter((item) => item?.type !== 'image_url');
|
||||
for (const item of imageItems) {
|
||||
updatedContent = updatedContent.map((c) => {
|
||||
if (c?.type !== 'text' || typeof c.text !== 'string') return c;
|
||||
@@ -1702,59 +1698,27 @@ export function createTkmindProxy({
|
||||
return { changed: false, updated: false, status: upstream.status };
|
||||
}
|
||||
const session = await upstream.json().catch(() => null);
|
||||
const conversation = Array.isArray(session?.conversation) ? session.conversation : [];
|
||||
const hasImageUrlContent = conversationHasImageUrlContent(conversation, {
|
||||
excludeMessageId: activeId,
|
||||
});
|
||||
const { conversation: scrubbedConversation, changed } = scrubConversationHistoricalImageAttachments(
|
||||
conversation,
|
||||
const { conversation, changed } = scrubConversationHistoricalImageAttachments(
|
||||
session?.conversation ?? [],
|
||||
activeId,
|
||||
);
|
||||
// Text-only providers (DeepSeek) reject any lingering image_url parts. If Goose
|
||||
// cannot persist a scrub (405/404), callers must rotate to a fresh session.
|
||||
if (!changed && !hasImageUrlContent) {
|
||||
return { changed: false, updated: false, hasImageUrlContent: false };
|
||||
}
|
||||
if (!changed && hasImageUrlContent) {
|
||||
return {
|
||||
changed: true,
|
||||
updated: false,
|
||||
status: 'image_url_content_present',
|
||||
hasImageUrlContent: true,
|
||||
};
|
||||
}
|
||||
if (!changed) return { changed: false, updated: false };
|
||||
const update = await apiFetch(
|
||||
target,
|
||||
apiSecret,
|
||||
`/sessions/${encodeURIComponent(sessionId)}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ conversation: scrubbedConversation }),
|
||||
body: JSON.stringify({ conversation }),
|
||||
},
|
||||
);
|
||||
if (!update.ok) {
|
||||
console.warn(
|
||||
`Historical image scrub skipped for session ${sessionId}: upstream ${update.status}`,
|
||||
);
|
||||
return {
|
||||
changed: true,
|
||||
updated: false,
|
||||
status: update.status,
|
||||
hasImageUrlContent:
|
||||
hasImageUrlContent
|
||||
|| conversationHasImageUrlContent(scrubbedConversation, {
|
||||
excludeMessageId: activeId,
|
||||
}),
|
||||
};
|
||||
return { changed: true, updated: false, status: update.status };
|
||||
}
|
||||
return {
|
||||
changed: true,
|
||||
updated: true,
|
||||
status: update.status,
|
||||
hasImageUrlContent: conversationHasImageUrlContent(scrubbedConversation, {
|
||||
excludeMessageId: activeId,
|
||||
}),
|
||||
};
|
||||
return { changed: true, updated: true, status: update.status };
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
'Historical image scrub skipped:',
|
||||
@@ -1974,17 +1938,7 @@ export function createTkmindProxy({
|
||||
if (!user) throw new Error('用户不存在');
|
||||
if (requireHistoricalImageIsolation || messageHasImages(userMessage)) {
|
||||
const imageIsolation = await syncHistoricalImageTurnIsolation(sessionId, userMessage?.id);
|
||||
const scrubUnsupported =
|
||||
imageIsolation.changed
|
||||
&& !imageIsolation.updated
|
||||
&& (
|
||||
requireHistoricalImageIsolation
|
||||
|| imageIsolation.hasImageUrlContent
|
||||
|| Number(imageIsolation.status) === 404
|
||||
|| Number(imageIsolation.status) === 405
|
||||
|| imageIsolation.status === 'image_url_content_present'
|
||||
);
|
||||
if (scrubUnsupported) {
|
||||
if (requireHistoricalImageIsolation && imageIsolation.changed && !imageIsolation.updated) {
|
||||
const error = new Error(
|
||||
`historical_image_session_update_unsupported:${imageIsolation.status ?? 'unknown'}`,
|
||||
);
|
||||
|
||||
@@ -1534,58 +1534,6 @@ test('submitSessionReplyForUser fails closed when historical image scrub is unsu
|
||||
});
|
||||
});
|
||||
|
||||
test('submitSessionReplyForUser rotates when assistant history still has image_url', async () => {
|
||||
await withFakeGoosedSession(async ({ apiTarget, workingDir, replyBodies }) => {
|
||||
const proxy = createTkmindProxy({
|
||||
apiTarget,
|
||||
apiSecret: 'test-secret',
|
||||
userAuth: {
|
||||
...createMemoryTestUserAuth(workingDir),
|
||||
async ownsSession() {
|
||||
return true;
|
||||
},
|
||||
async canUseChat() {
|
||||
return { ok: true };
|
||||
},
|
||||
async getUserById() {
|
||||
return { id: 'user-1' };
|
||||
},
|
||||
async resolveUserPolicies() {
|
||||
return { unrestricted: true, policies: {} };
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
proxy.submitSessionReplyForUser(
|
||||
'user-1',
|
||||
'session-1',
|
||||
'request-after-assistant-image',
|
||||
{
|
||||
id: 'message-current',
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: '这张图是什么' }],
|
||||
metadata: { imageUrls: ['https://example.com/new.png'] },
|
||||
},
|
||||
{ requireHistoricalImageIsolation: true },
|
||||
),
|
||||
/historical_image_session_update_unsupported:image_url_content_present/,
|
||||
);
|
||||
assert.equal(replyBodies.length, 0);
|
||||
}, {
|
||||
conversation: [
|
||||
{
|
||||
id: 'assistant-old-image',
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: '我看到了图片' },
|
||||
{ type: 'image_url', image_url: { url: 'https://example.com/old.png' } },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('visual fallback session removes read_image while preserving the text task path', async () => {
|
||||
await withFakeGoosedSession(async ({
|
||||
apiTarget,
|
||||
|
||||
+5
-6
@@ -3288,8 +3288,8 @@ test('wechat mp service forwards H5 agent text when page generation produced no
|
||||
assert.equal(fs.existsSync(htmlPath), false);
|
||||
const sendCall = wechatCalls.find(([url]) => String(url).includes('/cgi-bin/message/custom/send'));
|
||||
const payload = JSON.parse(sendCall[2]);
|
||||
assert.doesNotMatch(payload.text.content, /🌴 泰国简易攻略/);
|
||||
assert.match(payload.text.content, /没有按服务号页面技能真正生成成功/);
|
||||
assert.match(payload.text.content, /🌴 泰国简易攻略/);
|
||||
assert.doesNotMatch(payload.text.content, /没有按 H5 里的页面技能真正生成成功/);
|
||||
assert.doesNotMatch(payload.text.content, /https:\/\/m\.tkmind\.cn\/MindSpace\/.+\/public\/thailand-guide\.html/);
|
||||
});
|
||||
|
||||
@@ -3631,8 +3631,7 @@ test('wechat mp service recreates dedicated session when tool_calls error arrive
|
||||
assert.equal(started, true);
|
||||
const sendCall = wechatCalls.find(([url]) => String(url).includes('/cgi-bin/message/custom/send'));
|
||||
const payload = JSON.parse(sendCall[2]);
|
||||
assert.doesNotMatch(payload.text.content, /已恢复,可以继续对话/);
|
||||
assert.match(payload.text.content, /没有按服务号页面技能真正生成成功/);
|
||||
assert.match(payload.text.content, /已恢复,可以继续对话/);
|
||||
assert.doesNotMatch(payload.text.content, /Bad request/);
|
||||
assert.doesNotMatch(payload.text.content, /tool_calls/);
|
||||
});
|
||||
@@ -3966,8 +3965,8 @@ test('wechat mp service retries poisoned publish claims before forwarding H5 ret
|
||||
const sendCall = wechatCalls.find(([url]) => String(url).includes('/cgi-bin/message/custom/send'));
|
||||
const payload = JSON.parse(sendCall[2]);
|
||||
assert.doesNotMatch(payload.text.content, /页面都已经成功发布了|主题页面已发布/);
|
||||
assert.doesNotMatch(payload.text.content, /🌴 夏日主题页面/);
|
||||
assert.match(payload.text.content, /没有按服务号页面技能真正生成成功/);
|
||||
assert.match(payload.text.content, /🌴 夏日主题页面/);
|
||||
assert.doesNotMatch(payload.text.content, /没有按 H5 里的页面技能真正生成成功/);
|
||||
assert.doesNotMatch(payload.text.content, /summer-breeze-journal\.html/);
|
||||
});
|
||||
|
||||
|
||||
@@ -69,11 +69,5 @@ export function resolvePageGenerateOutcome({
|
||||
};
|
||||
}
|
||||
|
||||
// Fail closed: page.generate must deliver a verified public HTML artifact.
|
||||
// Text-only planning replies ("我先搜索…") must not mark WeChat delivery done.
|
||||
return {
|
||||
action: 'fail',
|
||||
failureText: buildPagePublishFailureText(),
|
||||
reason: 'missing_page_artifact',
|
||||
};
|
||||
return { action: 'send', artifacts: sendable };
|
||||
}
|
||||
|
||||
@@ -218,29 +218,3 @@ test('resolvePageGenerateOutcome sends when share preview meta is present', () =
|
||||
assert.equal(outcome.action, 'send');
|
||||
assert.equal(outcome.artifacts.length, 1);
|
||||
});
|
||||
|
||||
test('resolvePageGenerateOutcome fails closed on planning text without html artifact', () => {
|
||||
const outcome = resolvePageGenerateOutcome({
|
||||
reply: {
|
||||
text: '找到了之前的新闻页面。让我先读取最新的页面格式作为参考,同时并行搜索今日新闻和天气。',
|
||||
},
|
||||
confirmedArtifacts: [],
|
||||
verifiedArtifacts: [],
|
||||
});
|
||||
assert.equal(outcome.action, 'fail');
|
||||
assert.equal(outcome.reason, 'missing_page_artifact');
|
||||
assert.match(outcome.failureText, /没有按服务号页面技能真正生成成功|static-page-publish/);
|
||||
});
|
||||
|
||||
test('resolvePageGenerateOutcome fails closed when reply links an old page but no artifact confirmed', () => {
|
||||
const outcome = resolvePageGenerateOutcome({
|
||||
reply: {
|
||||
text: '[每日新闻](https://m.tkmind.cn/MindSpace/u/public/daily-news-0728.html)',
|
||||
},
|
||||
confirmedArtifacts: [],
|
||||
verifiedArtifacts: [],
|
||||
replyHasPublicLinks: true,
|
||||
});
|
||||
assert.equal(outcome.action, 'fail');
|
||||
assert.equal(outcome.reason, 'missing_page_artifact');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user