feat: refactor sgclaw around zeroclaw compat runtime
This commit is contained in:
229
tools/browser_smoke/fake_deepseek_server.mjs
Normal file
229
tools/browser_smoke/fake_deepseek_server.mjs
Normal file
@@ -0,0 +1,229 @@
|
||||
import http from 'node:http'
|
||||
|
||||
const DEFAULT_DEEPSEEK_MODEL = 'deepseek-chat'
|
||||
const DEFAULT_DEEPSEEK_API_KEY = 'sgclaw-smoke-test-key'
|
||||
|
||||
const BAIDU_URL = 'https://www.baidu.com'
|
||||
const BAIDU_DOMAIN = 'www.baidu.com'
|
||||
const BAIDU_INPUT_SELECTOR = '#kw'
|
||||
const BAIDU_BUTTON_SELECTOR = '#su'
|
||||
const ZHIHU_URL = 'https://www.zhihu.com/search'
|
||||
const ZHIHU_DOMAIN = 'www.zhihu.com'
|
||||
|
||||
export function buildSmokeEnv(baseUrl, baseEnv = process.env) {
|
||||
return {
|
||||
...baseEnv,
|
||||
DEEPSEEK_API_KEY: DEFAULT_DEEPSEEK_API_KEY,
|
||||
DEEPSEEK_BASE_URL: baseUrl,
|
||||
DEEPSEEK_MODEL: DEFAULT_DEEPSEEK_MODEL,
|
||||
}
|
||||
}
|
||||
|
||||
export function buildChatCompletionResponse(requestBody) {
|
||||
const instruction = latestUserInstruction(requestBody?.messages ?? [])
|
||||
const plan = instructionPlan(instruction)
|
||||
const isFinalRound = (requestBody?.messages ?? []).some((message) => message?.role === 'tool')
|
||||
|
||||
if (isFinalRound) {
|
||||
return {
|
||||
id: `chatcmpl-smoke-final-${plan.key}`,
|
||||
object: 'chat.completion',
|
||||
created: unixTimeSeconds(),
|
||||
model: DEFAULT_DEEPSEEK_MODEL,
|
||||
choices: [{
|
||||
index: 0,
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: plan.summary,
|
||||
},
|
||||
finish_reason: 'stop',
|
||||
}],
|
||||
usage: {
|
||||
prompt_tokens: 32,
|
||||
completion_tokens: 8,
|
||||
total_tokens: 40,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: `chatcmpl-smoke-tools-${plan.key}`,
|
||||
object: 'chat.completion',
|
||||
created: unixTimeSeconds(),
|
||||
model: DEFAULT_DEEPSEEK_MODEL,
|
||||
choices: [{
|
||||
index: 0,
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
tool_calls: plan.toolCalls,
|
||||
},
|
||||
finish_reason: 'tool_calls',
|
||||
}],
|
||||
usage: {
|
||||
prompt_tokens: 24,
|
||||
completion_tokens: 12,
|
||||
total_tokens: 36,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function startFakeDeepSeekServer() {
|
||||
const requests = []
|
||||
const server = http.createServer(async (req, res) => {
|
||||
try {
|
||||
if (req.method !== 'POST') {
|
||||
res.writeHead(405, {'content-type': 'application/json'})
|
||||
res.end(JSON.stringify({error: 'method not allowed'}))
|
||||
return
|
||||
}
|
||||
|
||||
const body = await readJsonBody(req)
|
||||
requests.push({
|
||||
method: req.method,
|
||||
url: req.url || '/',
|
||||
body,
|
||||
})
|
||||
|
||||
const response = buildChatCompletionResponse(body)
|
||||
res.writeHead(200, {'content-type': 'application/json'})
|
||||
res.end(JSON.stringify(response))
|
||||
} catch (error) {
|
||||
res.writeHead(500, {'content-type': 'application/json'})
|
||||
res.end(JSON.stringify({
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
}))
|
||||
}
|
||||
})
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
server.once('error', reject)
|
||||
server.listen(0, '127.0.0.1', resolve)
|
||||
})
|
||||
|
||||
const address = server.address()
|
||||
if (!address || typeof address === 'string') {
|
||||
server.close()
|
||||
throw new Error('failed to bind fake DeepSeek server')
|
||||
}
|
||||
|
||||
return {
|
||||
baseUrl: `http://127.0.0.1:${address.port}`,
|
||||
requests,
|
||||
close: () => new Promise((resolve, reject) => {
|
||||
server.close((error) => error ? reject(error) : resolve())
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
function latestUserInstruction(messages) {
|
||||
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
||||
const message = messages[index]
|
||||
if (message?.role === 'user' && typeof message.content === 'string' && message.content.trim()) {
|
||||
return normalizeSmokeInstruction(message.content)
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('missing user instruction in DeepSeek smoke request')
|
||||
}
|
||||
|
||||
export function normalizeSmokeInstruction(rawInstruction) {
|
||||
return rawInstruction.trim().replace(/^(?:\[[^\]]+\]\s*)+/, '')
|
||||
}
|
||||
|
||||
function instructionPlan(instruction) {
|
||||
const baiduQuery = extractQuery(instruction, ['打开百度搜索', '打开百度并搜索'])
|
||||
if (baiduQuery) {
|
||||
return {
|
||||
key: 'baidu',
|
||||
summary: `已在百度搜索${baiduQuery}`,
|
||||
toolCalls: [
|
||||
browserToolCall('call_baidu_1', {
|
||||
action: 'navigate',
|
||||
expected_domain: BAIDU_DOMAIN,
|
||||
url: BAIDU_URL,
|
||||
}),
|
||||
browserToolCall('call_baidu_2', {
|
||||
action: 'type',
|
||||
expected_domain: BAIDU_DOMAIN,
|
||||
selector: BAIDU_INPUT_SELECTOR,
|
||||
text: baiduQuery,
|
||||
clear_first: true,
|
||||
}),
|
||||
browserToolCall('call_baidu_3', {
|
||||
action: 'click',
|
||||
expected_domain: BAIDU_DOMAIN,
|
||||
selector: BAIDU_BUTTON_SELECTOR,
|
||||
}),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
const zhihuQuery = extractQuery(instruction, ['打开知乎搜索', '打开知乎并搜索'])
|
||||
if (zhihuQuery) {
|
||||
const url = new URL(ZHIHU_URL)
|
||||
url.searchParams.set('type', 'content')
|
||||
url.searchParams.set('q', zhihuQuery)
|
||||
|
||||
return {
|
||||
key: 'zhihu',
|
||||
summary: `已在知乎搜索${zhihuQuery}`,
|
||||
toolCalls: [
|
||||
browserToolCall('call_zhihu_1', {
|
||||
action: 'navigate',
|
||||
expected_domain: ZHIHU_DOMAIN,
|
||||
url: url.toString(),
|
||||
}),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`unsupported smoke instruction: ${instruction}`)
|
||||
}
|
||||
|
||||
function extractQuery(instruction, prefixes) {
|
||||
for (const prefix of prefixes) {
|
||||
if (instruction.startsWith(prefix)) {
|
||||
const query = instruction.slice(prefix.length).trim()
|
||||
if (!query) {
|
||||
throw new Error(`missing search query in instruction: ${instruction}`)
|
||||
}
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function browserToolCall(id, args) {
|
||||
return {
|
||||
id,
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'browser_action',
|
||||
arguments: JSON.stringify(args),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function readJsonBody(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let raw = ''
|
||||
req.setEncoding('utf8')
|
||||
req.on('data', (chunk) => {
|
||||
raw += chunk
|
||||
})
|
||||
req.on('end', () => {
|
||||
try {
|
||||
resolve(JSON.parse(raw || '{}'))
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
req.on('error', reject)
|
||||
})
|
||||
}
|
||||
|
||||
function unixTimeSeconds() {
|
||||
return Math.floor(Date.now() / 1000)
|
||||
}
|
||||
98
tools/browser_smoke/fake_deepseek_server.test.mjs
Normal file
98
tools/browser_smoke/fake_deepseek_server.test.mjs
Normal file
@@ -0,0 +1,98 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
buildChatCompletionResponse,
|
||||
buildSmokeEnv,
|
||||
} from './fake_deepseek_server.mjs'
|
||||
|
||||
test('buildChatCompletionResponse returns Baidu tool calls for the first round', () => {
|
||||
const response = buildChatCompletionResponse({
|
||||
messages: [
|
||||
{role: 'system', content: 'You are sgClaw'},
|
||||
{role: 'user', content: '打开百度搜索天气'},
|
||||
],
|
||||
})
|
||||
|
||||
const message = response.choices[0].message
|
||||
assert.equal(message.content, '')
|
||||
assert.equal(message.tool_calls.length, 3)
|
||||
assert.equal(message.tool_calls[0].function.name, 'browser_action')
|
||||
assert.match(message.tool_calls[0].function.arguments, /"action":"navigate"/)
|
||||
assert.match(message.tool_calls[1].function.arguments, /"action":"type"/)
|
||||
assert.match(message.tool_calls[2].function.arguments, /"action":"click"/)
|
||||
})
|
||||
|
||||
test('buildChatCompletionResponse returns Zhihu navigate tool call for the first round', () => {
|
||||
const response = buildChatCompletionResponse({
|
||||
messages: [
|
||||
{role: 'system', content: 'You are sgClaw'},
|
||||
{role: 'user', content: '打开知乎搜索天气'},
|
||||
],
|
||||
})
|
||||
|
||||
const message = response.choices[0].message
|
||||
assert.equal(message.content, '')
|
||||
assert.equal(message.tool_calls.length, 1)
|
||||
assert.match(
|
||||
message.tool_calls[0].function.arguments,
|
||||
/https:\/\/www\.zhihu\.com\/search\?type=content&q=%E5%A4%A9%E6%B0%94/,
|
||||
)
|
||||
})
|
||||
|
||||
test('buildChatCompletionResponse returns final summaries expected by the existing smoke script', () => {
|
||||
const baiduResponse = buildChatCompletionResponse({
|
||||
messages: [
|
||||
{role: 'system', content: 'You are sgClaw'},
|
||||
{role: 'user', content: '打开百度搜索天气'},
|
||||
{role: 'assistant', content: '', tool_calls: [{id: 'call_baidu_1'}]},
|
||||
{role: 'tool', tool_call_id: 'call_baidu_1', content: '{"ok":true}'},
|
||||
],
|
||||
})
|
||||
assert.equal(baiduResponse.choices[0].message.content, '已在百度搜索天气')
|
||||
|
||||
const zhihuResponse = buildChatCompletionResponse({
|
||||
messages: [
|
||||
{role: 'system', content: 'You are sgClaw'},
|
||||
{role: 'user', content: '打开知乎搜索天气'},
|
||||
{role: 'assistant', content: '', tool_calls: [{id: 'call_zhihu_1'}]},
|
||||
{role: 'tool', tool_call_id: 'call_zhihu_1', content: '{"ok":true}'},
|
||||
],
|
||||
})
|
||||
assert.equal(zhihuResponse.choices[0].message.content, '已在知乎搜索天气')
|
||||
})
|
||||
|
||||
test('buildChatCompletionResponse rejects unsupported instructions clearly', () => {
|
||||
assert.throws(
|
||||
() =>
|
||||
buildChatCompletionResponse({
|
||||
messages: [{role: 'user', content: '打开豆瓣搜索天气'}],
|
||||
}),
|
||||
/unsupported smoke instruction: 打开豆瓣搜索天气/,
|
||||
)
|
||||
})
|
||||
|
||||
test('buildChatCompletionResponse accepts ZeroClaw timestamp-prefixed instructions', () => {
|
||||
const response = buildChatCompletionResponse({
|
||||
messages: [
|
||||
{role: 'user', content: '[2026-03-26 16:11:26 +08:00] 打开百度搜索天气'},
|
||||
],
|
||||
})
|
||||
|
||||
const message = response.choices[0].message
|
||||
assert.equal(message.tool_calls.length, 3)
|
||||
assert.match(message.tool_calls[0].function.arguments, /https:\/\/www\.baidu\.com/)
|
||||
})
|
||||
|
||||
test('buildSmokeEnv injects DeepSeek environment variables for the local fake server', () => {
|
||||
const env = buildSmokeEnv('http://127.0.0.1:32123', {
|
||||
PATH: '/usr/bin',
|
||||
HOME: '/tmp/home',
|
||||
})
|
||||
|
||||
assert.equal(env.PATH, '/usr/bin')
|
||||
assert.equal(env.HOME, '/tmp/home')
|
||||
assert.equal(env.DEEPSEEK_API_KEY, 'sgclaw-smoke-test-key')
|
||||
assert.equal(env.DEEPSEEK_BASE_URL, 'http://127.0.0.1:32123')
|
||||
assert.equal(env.DEEPSEEK_MODEL, 'deepseek-chat')
|
||||
})
|
||||
75
tools/browser_smoke/run_deepseek_browser_smoke.mjs
Normal file
75
tools/browser_smoke/run_deepseek_browser_smoke.mjs
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import {spawn} from 'node:child_process'
|
||||
import process from 'node:process'
|
||||
|
||||
import {
|
||||
buildSmokeEnv,
|
||||
normalizeSmokeInstruction,
|
||||
startFakeDeepSeekServer,
|
||||
} from './fake_deepseek_server.mjs'
|
||||
|
||||
const SUPER_RPA_SMOKE_SCRIPT =
|
||||
'/home/zyl/projects/superRpa/src/chrome/browser/superrpa/sgclaw/sgclaw_chat_smoke.mjs'
|
||||
|
||||
async function main() {
|
||||
const server = await startFakeDeepSeekServer()
|
||||
console.log(`Started fake DeepSeek server at ${server.baseUrl}`)
|
||||
|
||||
try {
|
||||
await runSmoke(server.baseUrl)
|
||||
assertCompatRuntimeTraffic(server.requests)
|
||||
console.log(`Verified DeepSeek compat runtime via ${server.requests.length} provider requests.`)
|
||||
} finally {
|
||||
await server.close()
|
||||
console.log('Stopped fake DeepSeek server.')
|
||||
}
|
||||
}
|
||||
|
||||
function runSmoke(baseUrl) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('node', [SUPER_RPA_SMOKE_SCRIPT], {
|
||||
stdio: 'inherit',
|
||||
env: buildSmokeEnv(baseUrl),
|
||||
})
|
||||
|
||||
child.once('error', reject)
|
||||
child.once('exit', (code, signal) => {
|
||||
if (code === 0) {
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
|
||||
if (signal) {
|
||||
reject(new Error(`browser smoke exited via signal: ${signal}`))
|
||||
return
|
||||
}
|
||||
|
||||
reject(new Error(`browser smoke exited with code: ${code}`))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function assertCompatRuntimeTraffic(requests) {
|
||||
if (requests.length < 4) {
|
||||
throw new Error(`expected at least 4 provider requests, got ${requests.length}`)
|
||||
}
|
||||
|
||||
const instructions = requests
|
||||
.flatMap((entry) => entry.body?.messages ?? [])
|
||||
.filter((message) => message?.role === 'user')
|
||||
.map((message) => normalizeSmokeInstruction(message.content))
|
||||
|
||||
if (!instructions.includes('打开百度搜索天气')) {
|
||||
throw new Error('fake DeepSeek server did not receive the Baidu smoke instruction')
|
||||
}
|
||||
|
||||
if (!instructions.includes('打开知乎搜索天气')) {
|
||||
throw new Error('fake DeepSeek server did not receive the Zhihu smoke instruction')
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.message : String(error))
|
||||
process.exitCode = 1
|
||||
})
|
||||
Reference in New Issue
Block a user