Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

8.6 KB
Raw Blame
import { describe, it, expect, beforeEach, vi } from 'vitest'
import {
  precomputeToolEmbeddings,
  rankToolsByEmbedding,
  selectToolsByEmbedding,
  clearEmbeddingCache,
  __internal,
} from '../embedding-router'

// Deterministic fake embeddings: character-frequency vectors of length 26
// ('a'..'z'). Two similar-topic strings will share character distribution
// and hence a high cosine similarity without needing real nomic-embed.
const fakeEmbed = async (texts: string[]): Promise<number[][]> =>
  texts.map((t) => {
    const v = new Array(26).fill(0)
    const s = t.toLowerCase()
    for (let i = 0; i < s.length; i++) {
      const code = s.charCodeAt(i)
      if (code >= 97 && code <= 122) v[code - 97] += 1
    }
    return v
  })

describe('embedding-router — precompute / cache', () => {
  beforeEach(() => clearEmbeddingCache())

  it('embeds tools on first pass and caches them', async () => {
    const tools = [
      { name: 'a', description: 'apple' },
      { name: 'b', description: 'banana' },
    ]
    const added = await precomputeToolEmbeddings(tools, fakeEmbed)
    expect(added).toBe(2)
    expect(__internal.CACHE.size).toBe(2)
  })

  it('skips tools whose description has not changed', async () => {
    const tools = [{ name: 'a', description: 'apple' }]
    await precomputeToolEmbeddings(tools, fakeEmbed)
    const embedSpy = vi.fn(fakeEmbed)
    const added = await precomputeToolEmbeddings(tools, embedSpy)
    expect(added).toBe(0)
    expect(embedSpy).not.toHaveBeenCalled()
  })

  it('re-embeds a tool whose description changed', async () => {
    await precomputeToolEmbeddings([{ name: 'a', description: 'apple' }], fakeEmbed)
    const embedSpy = vi.fn(fakeEmbed)
    const added = await precomputeToolEmbeddings(
      [{ name: 'a', description: 'apple banana' }],
      embedSpy
    )
    expect(added).toBe(1)
    expect(embedSpy).toHaveBeenCalledOnce()
  })
})

describe('embedding-router — ranking', () => {
  beforeEach(() => clearEmbeddingCache())

  it('ranks the most similar-text tool first', async () => {
    const tools = [
      { name: 'search_web', description: 'find things on the internet and return results' },
      { name: 'read_file', description: 'open a file on disk and return contents' },
      { name: 'run_shell', description: 'execute a terminal command and return output' },
    ]
    await precomputeToolEmbeddings(tools, fakeEmbed)
    const ranked = await rankToolsByEmbedding(
      'please open the readme file on my disk',
      tools,
      fakeEmbed,
      { topN: 3 }
    )
    expect(ranked[0].tool.name).toBe('read_file')
  })

  it('respects topN truncation', async () => {
    const tools = Array.from({ length: 10 }, (_, i) => ({
      name: `t${i}`,
      description: `description ${i}`,
    }))
    await precomputeToolEmbeddings(tools, fakeEmbed)
    const ranked = await rankToolsByEmbedding('query', tools, fakeEmbed, { topN: 3 })
    expect(ranked).toHaveLength(3)
  })

  it('skips uncached tools silently', async () => {
    const tools = [{ name: 'a', description: 'apple' }, { name: 'b', description: 'banana' }]
    // Only embed "a" first
    await precomputeToolEmbeddings([tools[0]], fakeEmbed)
    const ranked = await rankToolsByEmbedding('apple', tools, fakeEmbed, { topN: 5 })
    expect(ranked.map((r) => r.tool.name)).toEqual(['a'])
  })

  it('returns [] on empty query embedding', async () => {
    const tools = [{ name: 'a', description: 'apple' }]
    await precomputeToolEmbeddings(tools, fakeEmbed)
    const ranked = await rankToolsByEmbedding(
      'x',
      tools,
      async () => [] as number[][], // pathological — returns no vector
      { topN: 3 }
    )
    expect(ranked).toEqual([])
  })
})

describe('embedding-router — selectToolsByEmbedding', () => {
  beforeEach(() => clearEmbeddingCache())

  it('precomputes, ranks topN tools, unions with alwaysInclude', async () => {
    const tools = [
      { name: 'search_web', description: 'find things on the internet' },
      { name: 'read_file', description: 'open a file on disk' },
      { name: 'get_current_time', description: 'return current local time' },
    ]
    const out = await selectToolsByEmbedding(
      'any query — topN is the contract being tested, not specific ranking',
      tools,
      fakeEmbed,
      { topN: 1, alwaysInclude: ['get_current_time'] }
    )
    // Exactly 1 ranked + get_current_time always appended = 2 (unless the
    // top-ranked happens to be get_current_time, then 1).
    expect(out.length).toBeGreaterThanOrEqual(1)
    expect(out).toContain('get_current_time')
  })

  it('does not duplicate when an always-include name is already ranked', async () => {
    const tools = [
      { name: 'get_current_time', description: 'return current local date and time now' },
    ]
    const out = await selectToolsByEmbedding(
      'what time is it',
      tools,
      fakeEmbed,
      { topN: 3, alwaysInclude: ['get_current_time'] }
    )
    expect(out.filter((n) => n === 'get_current_time')).toHaveLength(1)
  })
})

describe('embedding-router — cosine edge cases', () => {
  it('zero vector → similarity 0', () => {
    expect(__internal.cosine([0, 0, 0], [1, 1, 1])).toBe(0)
  })
  it('identical vector → 1', () => {
    expect(__internal.cosine([1, 2, 3], [1, 2, 3])).toBeCloseTo(1, 5)
  })
  it('opposite vector → -1', () => {
    expect(__internal.cosine([1, 2, 3], [-1, -2, -3])).toBeCloseTo(-1, 5)
  })
  it('different-length vectors return 0 (incompatible embedding spaces)', () => {
    // A mid-session embed-backend switch changes vector dimension; comparing
    // only the overlap yields a garbage similarity, so cosine refuses.
    expect(__internal.cosine([1, 2, 3], [1, 2])).toBe(0)
    expect(__internal.cosine([1, 2], [1, 2, 3])).toBe(0)
  })
})

describe('embedding-router — backend switch self-heals the cache (#14)', () => {
  it('drops stale-dimension vectors and re-embeds in the new space', async () => {
    clearEmbeddingCache()
    const tools = [{ name: 'a', description: 'apple' }]
    // First backend: 26-dim character-frequency vectors.
    await precomputeToolEmbeddings(tools, fakeEmbed)
    expect(__internal.CACHE.size).toBe(1)
    // Second backend: 4-dim vectors (a different embedding model). Ranking must
    // NOT compare across spaces — it drops the stale entry instead.
    const smallEmbed = async (texts: string[]) => texts.map(() => [1, 0, 0, 0])
    const ranked = await rankToolsByEmbedding('apple', tools, smallEmbed, { topN: 5 })
    expect(ranked).toEqual([])
    expect(__internal.CACHE.has('a')).toBe(false)
    // Next precompute with the new backend re-embeds in the new space.
    const added = await precomputeToolEmbeddings(tools, smallEmbed)
    expect(added).toBe(1)
  })
})

describe('embedding-router — hashDescription', () => {
  it('is stable', () => {
    expect(__internal.hashDescription('hello')).toBe(__internal.hashDescription('hello'))
  })
  it('differs for different descriptions', () => {
    expect(__internal.hashDescription('a')).not.toBe(__internal.hashDescription('b'))
  })
})

// Realtime pass 01.10.2026 (R3): the agent loop routes every step of a run on
// the same user instruction and asked the embedding server for its vector
// before every model call.
describe('embedding-router: the query vector is asked for once per query', () => {
  beforeEach(() => clearEmbeddingCache())

  it('a second step with the same instruction does not call the embedding server', async () => {
    const tools = [{ name: 'a', description: 'apple' }, { name: 'b', description: 'banana' }]
    const embed = vi.fn(fakeEmbed)
    await selectToolsByEmbedding('make an apple pie', tools, embed, { topN: 1 })
    const afterFirst = embed.mock.calls.length
    await selectToolsByEmbedding('make an apple pie', tools, embed, { topN: 1 })
    expect(afterFirst).toBe(2) // the tool descriptions, then the query
    expect(embed.mock.calls.length).toBe(afterFirst)
    // A new instruction is embedded, of course.
    await selectToolsByEmbedding('peel a banana', tools, embed, { topN: 1 })
    expect(embed.mock.calls.length).toBe(afterFirst + 1)
  })

  it('a query cached under another embedding backend is asked for again, not compared across spaces', async () => {
    const tools = [{ name: 'a', description: 'apple' }]
    await selectToolsByEmbedding('apple', tools, fakeEmbed, { topN: 1 })
    // Backend switch: the tools are re-embedded in the new 4-dim space.
    __internal.CACHE.clear()
    const smallEmbed = vi.fn(async (texts: string[]) => texts.map(() => [1, 0, 0, 0]))
    const out = await selectToolsByEmbedding('apple', tools, smallEmbed, { topN: 1 })
    expect(out).toEqual(['a'])
    expect(smallEmbed.mock.calls.some((c) => c[0][0] === 'apple' && c[0].length === 1)).toBe(true)
  })
})