embeddedGateway.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
  2. import { createServer, type Server } from 'node:http'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { createEmbeddedHermes, type EmbeddedHermes } from '../../src/server/hermes/embedded.js'
  7. import { kernelPaths } from '../../src/server/hermes/paths.js'
  8. import type { GatewayFrame } from '../../src/server/workspaceGateway.js'
  9. const kernel = existsSync(kernelPaths().python)
  10. const env = { ...process.env, YAOVI_HERMES_TEST_OFFLINE: '1' }
  11. type Step = { content?: string; call?: { name: string; arguments: Record<string, unknown> } }
  12. /** Deterministic OpenAI-compatible endpoint: each request answers with the next scripted step. */
  13. function fakeModel(script: (messages: any[]) => Step) {
  14. const requests: any[] = []
  15. const server = createServer((req, res) => {
  16. let raw = ''
  17. req.on('data', chunk => { raw += chunk })
  18. req.on('end', () => {
  19. let body: any
  20. try { body = JSON.parse(raw) } catch { body = undefined }
  21. if (req.method !== 'POST' || !Array.isArray(body?.messages)) {
  22. // Model listing / capability probes.
  23. res.writeHead(200, { 'content-type': 'application/json' })
  24. res.end(JSON.stringify({ object: 'list', data: [{ id: 'test-model', object: 'model' }] }))
  25. return
  26. }
  27. requests.push(body)
  28. const step = script(body.messages)
  29. const delta = step.call
  30. ? { role: 'assistant', content: null, tool_calls: [{ index: 0, id: `call-${requests.length}`, type: 'function', function: { name: step.call.name, arguments: JSON.stringify(step.call.arguments) } }] }
  31. : { role: 'assistant', content: step.content ?? '' }
  32. res.writeHead(200, { 'content-type': 'text/event-stream' })
  33. for (const part of [
  34. { id: 't', object: 'chat.completion.chunk', model: 'test-model', choices: [{ index: 0, delta, finish_reason: null }] },
  35. { id: 't', object: 'chat.completion.chunk', model: 'test-model', choices: [{ index: 0, delta: {}, finish_reason: step.call ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } },
  36. ]) res.write(`data: ${JSON.stringify(part)}\n\n`)
  37. res.end('data: [DONE]\n\n')
  38. })
  39. })
  40. return { server, requests }
  41. }
  42. async function listen(server: Server): Promise<number> {
  43. await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
  44. return (server.address() as { port: number }).port
  45. }
  46. function waitFor(frames: GatewayFrame[], predicate: (frame: GatewayFrame) => boolean, timeout = 90_000): Promise<GatewayFrame> {
  47. return new Promise((resolve, reject) => {
  48. const started = Date.now()
  49. const tick = () => {
  50. const found = frames.find(predicate)
  51. if (found) return resolve(found)
  52. if (Date.now() - started > timeout) return reject(new Error(`timed out; saw ${frames.map(f => f.type).join(',')}`))
  53. setTimeout(tick, 50)
  54. }
  55. tick()
  56. })
  57. }
  58. describe.skipIf(!kernel)('embedded Hermes gateway', () => {
  59. let home = ''
  60. let hermes: EmbeddedHermes | undefined
  61. let model: Server | undefined
  62. afterEach(() => {
  63. hermes?.close(); hermes = undefined
  64. model?.close(); model = undefined
  65. if (home) rmSync(home, { recursive: true, force: true })
  66. })
  67. async function setup(script: (messages: any[]) => Step, approval: 'ask' | 'allow' | 'deny' = 'ask') {
  68. const fake = fakeModel(script)
  69. model = fake.server
  70. const port = await listen(fake.server)
  71. home = mkdtempSync(join(tmpdir(), 'yaoyao-gateway-'))
  72. mkdirSync(join(home, 'hermes'), { recursive: true })
  73. writeFileSync(join(home, 'hermes', 'config.yaml'), `model:\n provider: custom\n default: test-model\n base_url: http://127.0.0.1:${port}/v1\n api_key: test-key\n`)
  74. const cwd = join(home, 'work')
  75. hermes = createEmbeddedHermes(home, { env, defaultCwd: cwd, policy: () => ({ approval, denyHostTools: false }) })
  76. const frames: GatewayFrame[] = []
  77. const channel = hermes.gateway.open(frame => frames.push(frame))
  78. return { channel, frames, requests: fake.requests, cwd }
  79. }
  80. it('runs a native chat turn and streams gateway events', async () => {
  81. const { channel, frames, requests } = await setup(() => ({ content: '你好,我是内置内核。' }))
  82. const created = await channel.rpc('session.create', { profile: 'default', source: 'web' })
  83. expect(created.session_id).toBe(created.stored_session_id)
  84. expect(created.info).toMatchObject({ model: 'test-model', provider: 'custom' })
  85. expect(await channel.rpc('prompt.submit', { session_id: created.session_id, text: '你好' })).toEqual({ status: 'streaming' })
  86. const done = await waitFor(frames, f => f.type === 'message.complete')
  87. expect(done.payload).toMatchObject({ status: 'complete', text: '你好,我是内置内核。' })
  88. expect(frames.some(f => f.type === 'message.delta')).toBe(true)
  89. expect(requests[0].messages.at(-1).content).toContain('你好')
  90. // Hermes owns the transcript: the dashboard read path returns the stored messages.
  91. const page = await hermes!.api.handle({ method: 'GET', path: `/api/sessions/${created.session_id}/messages` })
  92. expect(page.status).toBe(200)
  93. expect(page.body.toString()).toContain('你好,我是内置内核。')
  94. await waitFor(frames, f => f.type === 'session.title')
  95. // A second turn continues the same Hermes session.
  96. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '再说一次' })
  97. await waitFor(frames, f => f.type === 'message.complete' && frames.filter(x => x.type === 'message.complete').length === 2)
  98. expect(requests.at(-1).messages.some((m: any) => m.role === 'assistant' && String(m.content).includes('内置内核'))).toBe(true)
  99. }, 180_000)
  100. it('executes granted application tools in the host and returns their results to the model', async () => {
  101. const calls: unknown[] = []
  102. const { channel, frames, requests } = await setup(messages => messages.some(m => m.role === 'tool')
  103. ? { content: '已读取:' + messages.find(m => m.role === 'tool').content }
  104. : { call: { name: 'team_lookup', arguments: { name: '小夭' } } })
  105. const created = await channel.rpc('session.create', { profile: 'default', bot_id: 'bot-1', hidden: true })
  106. const revoke = hermes!.gateway.grant(created.session_id, {
  107. catalog: () => [{ id: 'team_lookup', name: '查找成员', description: '按名字查找成员', inputSchema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] } }],
  108. call: async (id, args) => { calls.push([id, args]); return { content: [{ type: 'text', text: '小夭在线' }] } },
  109. })
  110. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '查一下小夭' })
  111. const done = await waitFor(frames, f => f.type === 'message.complete')
  112. revoke()
  113. expect(done.payload).toMatchObject({ status: 'complete' })
  114. expect(String(done.payload!.text)).toContain('小夭在线')
  115. expect(calls).toEqual([['team_lookup', { name: '小夭' }]])
  116. expect(requests[0].tools.map((t: any) => t.function.name)).toContain('team_lookup')
  117. expect(frames.some(f => f.type === 'tool.start' && f.payload?.name === 'team_lookup')).toBe(true)
  118. // Bot sessions live in the Bot's own runtime home.
  119. expect(existsSync(join(home, 'bots', 'bot-1', 'runtime', 'state.db'))).toBe(true)
  120. }, 180_000)
  121. it('asks for approval before a Host terminal command and honours a denial', async () => {
  122. const { channel, frames, cwd } = await setup(messages => messages.some(m => m.role === 'tool')
  123. ? { content: '结果:' + messages.find(m => m.role === 'tool').content.slice(0, 200) }
  124. : { call: { name: 'terminal', arguments: { command: 'echo embedded > proof.txt' } } })
  125. const created = await channel.rpc('session.create', { profile: 'default' })
  126. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '写文件' })
  127. const request = await waitFor(frames, f => f.type === 'approval.request')
  128. expect(String(request.payload!.command)).toContain('echo embedded')
  129. await channel.rpc('approval.respond', { session_id: created.session_id, request_id: request.payload!.request_id, choice: 'deny' })
  130. const done = await waitFor(frames, f => f.type === 'message.complete')
  131. expect(done.payload!.status).toBe('complete')
  132. expect(existsSync(join(cwd, 'proof.txt'))).toBe(false)
  133. }, 180_000)
  134. it('runs an approved Host terminal command in the session working directory', async () => {
  135. const { channel, frames, cwd } = await setup(messages => messages.some(m => m.role === 'tool')
  136. ? { content: '完成' }
  137. : { call: { name: 'terminal', arguments: { command: 'echo embedded > proof.txt' } } })
  138. const created = await channel.rpc('session.create', { profile: 'default' })
  139. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '写文件' })
  140. const request = await waitFor(frames, f => f.type === 'approval.request')
  141. await channel.rpc('approval.respond', { session_id: created.session_id, request_id: request.payload!.request_id, choice: 'once' })
  142. await waitFor(frames, f => f.type === 'message.complete')
  143. expect(existsSync(join(cwd, 'proof.txt'))).toBe(true)
  144. }, 180_000)
  145. it('interrupts a turn waiting on approval', async () => {
  146. const { channel, frames } = await setup(() => ({ call: { name: 'terminal', arguments: { command: 'sleep 1' } } }))
  147. const created = await channel.rpc('session.create', { profile: 'default' })
  148. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '执行' })
  149. await waitFor(frames, f => f.type === 'approval.request')
  150. expect((await channel.rpc('session.active_list', {})).sessions).toEqual([{ id: created.session_id, status: 'running' }])
  151. await channel.rpc('session.interrupt', { session_id: created.session_id })
  152. const done = await waitFor(frames, f => f.type === 'message.complete')
  153. expect(done.payload!.status).toBe('interrupted')
  154. await waitFor(frames, () => hermes!.gateway.idle, 10_000).catch(() => undefined)
  155. expect(hermes!.gateway.running(created.session_id)).toBe(false)
  156. }, 180_000)
  157. it('admits Profile skills and commits a skill created in a native chat', async () => {
  158. let step = 0
  159. const { channel, frames } = await setup(messages => {
  160. step++
  161. if (step === 1) return { call: { name: 'skills_list', arguments: {} } }
  162. if (step === 2) return { call: { name: 'skill_manage', arguments: { action: 'create', name: 'release-notes', content: '---\nname: release-notes\ndescription: Write release notes.\n---\n# Release notes\nList user-visible changes.\n' } } }
  163. return { content: '技能:' + messages.filter(m => m.role === 'tool').map(m => String(m.content).slice(0, 300)).join(' | ') }
  164. })
  165. const skill = join(home, 'hermes', 'skills', 'writing', 'weekly-report')
  166. mkdirSync(skill, { recursive: true })
  167. writeFileSync(join(skill, 'SKILL.md'), '---\nname: weekly-report\ndescription: Draft a weekly report.\n---\nUse the template.\n')
  168. writeFileSync(join(skill, 'template.md'), '# Week')
  169. const created = await channel.rpc('session.create', { profile: 'default' })
  170. await channel.rpc('prompt.submit', { session_id: created.session_id, text: '有哪些技能?再建一个发布说明技能' })
  171. const done = await waitFor(frames, f => f.type === 'message.complete')
  172. expect(done.payload!.status).toBe('complete')
  173. expect(String(done.payload!.text)).toContain('weekly-report')
  174. expect(frames.some(f => f.type === 'approval.request')).toBe(false)
  175. const written = join(home, 'hermes', 'skills')
  176. expect(readdirSync(written, { recursive: true }).some(path => String(path).endsWith('release-notes/SKILL.md'))).toBe(true)
  177. }, 180_000)
  178. })