Skip to content
safran

Type to search. Enter opens the first result.

    Installing the SDK

    Safran is not published to a package registry. You clone the repository and import its packages straight from the checkout: every package exports its TypeScript source, and Bun runs it without a build step. This page shows the two ways of importing them that work, the one that looks obvious and does not, and a first script.

    What you need

    • Bun 1.3 or newer. The packages use Bun.file, bun:sqlite and Bun.spawn, so Node.js cannot run them.
    • git on PATH, for cloning the repositories of your code hosts.
    • A profile. The examples use the acme profile from the quickstart.
    git clone <repository-url> /work/safran
    cd /work/safran
    bun install

    The packages

    PackageImport it for
    @safran/corecreateSafran and everything a caller needs: the instance, its six sub-APIs, the error helpers, the domain types, defineProfile and the define* plugin helpers. Enough for most programs.
    @safran/kitThe contracts and helpers for plugin authors: Tracker, CodeHost, DocSource, Embedder, createHttpClient, cursor and JSON helpers. @safran/core re-exports the common part of it.
    @safran/kit/testingIn-memory fakes (FakeTracker, FakeCodeHost, FakeCodeIndex, FakeEmbedder, FakeDocSource) and mockFetch. See testing.
    @safran/profileLoading, validating and querying a profile without building an instance. See company and docs.
    @safran/mcpThe MCP server as a library. See the API reference.
    @safran/discoverDrafting groups, teams and glossary from merged pull requests. See the API reference.
    @safran/evalGolden sets and retrieval measurement. See the API reference.

    The other packages in packages/ (@safran/trackers, @safran/hosts, @safran/docsources, @safran/embedders, @safran/codeindex) hold the built-in connectors and the code index. @safran/core loads them for you.

    How the packages resolve

    Two facts decide how you import Safran:

    1. Each package's package.json points exports at TypeScript source ("exports": "./src/index.ts"). There is no compiled output.
    2. The root package.json declares "workspaces": ["packages/*"], and Bun links a workspace package only into the packages that list it as a dependency. There is no shared node_modules/@safran at the root.

    The second fact means that a loose script dropped anywhere in the checkout cannot import the packages:

    $ bun scripts/hello.ts
    error: Cannot find module '@safran/core' from '/work/safran/scripts/hello.ts'

    Your code has to live in a package that declares the dependency. Either of the next two sections does that.

    Option A: a package inside the checkout

    Add your own workspace package next to Safran's. This is the shortest path and needs nothing outside the checkout.

    1. Create packages/acme-scripts/package.json:

      {
        "name": "acme-scripts",
        "private": true,
        "type": "module",
        "dependencies": {
          "@safran/core": "workspace:*",
          "@safran/kit": "workspace:*",
          "zod": "^4.0.0"
        }
      }

      List @safran/kit only if you import @safran/kit or @safran/kit/testing, and zod only if you write plugins.

    2. Link it:

      cd /work/safran
      bun install

      bun install rewrites bun.lock, because your package is now part of the workspace. Keep that change local.

    3. Write scripts in packages/acme-scripts/ and run them with bun.

    Option B: your own project, linked to the checkout

    To keep your code in a separate repository, register the packages with bun link and depend on them through the link: protocol.

    1. Register each package you import. This writes nothing into the checkout:

      cd /work/safran/packages/core && bun link
      cd /work/safran/packages/kit && bun link
    2. In your project, declare them:

      {
        "name": "acme-tools",
        "private": true,
        "type": "module",
        "dependencies": {
          "@safran/core": "link:@safran/core",
          "@safran/kit": "link:@safran/kit",
          "zod": "^4.0.0"
        },
        "devDependencies": {
          "@types/bun": "latest",
          "typescript": "^5.9.3"
        }
      }
    3. Install:

      cd /work/acme-tools
      bun install

    node_modules/@safran/core is then a symbolic link to /work/safran/packages/core. Safran's own dependencies (@safran/kit, zod, the connectors) are found from there, so bun install must have been run in the checkout too. A link is per machine: every developer, and CI, repeats step 1.

    A first script

    The script builds an instance from ./safran.yaml, reads one ticket and searches the indexed code with the ticket's title.

    first.ts
    import { createSafran } from "@safran/core";
    
    await using safran = await createSafran();
    
    const ticket = await safran.tickets.get("PAY-1423");
    console.log(`${ticket.key} [${ticket.status}] ${ticket.title}`);
    
    const { hits, effectiveQuery } = await safran.code.search(ticket.title, { groups: ["backend"], limit: 3 });
    console.log(`searched for: ${effectiveQuery}`);
    for (const hit of hits) console.log(`${hit.repo}/${hit.path}:${hit.startLine}-${hit.endLine}  ${hit.score}`);

    Run it from the directory that holds safran.yaml, with the tokens the profile names in the environment (see secrets), after safran sync has cloned and indexed the repositories once:

    $ cd /work/acme
    $ bun /work/safran/packages/acme-scripts/first.ts
    PAY-1423 [Done] Kısmi iade işlemi İstanbul şubesinde başarısız oluyor
    searched for: Kısmi iade işlemi İstanbul şubesinde başarısız oluyor refund chargeback
    ledger/src/entry.ts:1-9  0.4852
    payments-api/src/refund/service.ts:1-19  0.4745
    payments-api/src/refund/policy.ts:1-11  0.474

    The second line shows the glossary at work: the ticket says iade, the code says refund. The scores come from two small example repositories and a local bge-m3 model; yours will differ. Before anything is indexed, the search returns no hits instead of failing.

    What the script used:

    • createSafran() with no options reads ./safran.yaml and keeps its data in ./data.
    • await using closes the instance when the scope ends. See lifecycle.
    • tickets.get accepts whatever a person would paste: a key or a URL.
    • code.search returns ranked snippets, not whole files.

    TypeScript settings

    Bun runs the scripts with no configuration. A tsconfig.json matters for your editor and for tsc --noEmit, and one detail is specific to Safran: because the packages export source, your compiler typechecks Safran's files with your options. Use the options the repository itself is checked with:

    {
      "compilerOptions": {
        "target": "ESNext",
        "module": "Preserve",
        "moduleResolution": "bundler",
        "lib": ["ESNext"],
        "types": ["bun"],
        "strict": true,
        "noUncheckedIndexedAccess": true,
        "exactOptionalPropertyTypes": true,
        "verbatimModuleSyntax": true,
        "skipLibCheck": true,
        "noEmit": true
      }
    }
    SettingWhy
    target and lib: ESNextawait using and Symbol.asyncDispose need the explicit resource management types. With an older lib the declaration await using safran = … does not compile.
    moduleResolution: bundlerThe packages are imported by their exports map and by extension-less relative paths.
    types: ["bun"]The sources use Bun globals. Install @types/bun.
    exactOptionalPropertyTypesThe public types are written for it: an optional field is left out, never set to undefined. The examples in this guide compile with it on.

    If your toolchain cannot use await using, call close() yourself:

    import { createSafran } from "@safran/core";
    
    const safran = await createSafran();
    try {
      console.log((await safran.tickets.get("PAY-1423")).title);
    } finally {
      await safran.close();
    }

    Common mistakes

    SymptomCause
    Cannot find module '@safran/core'The script is not inside a package that declares the dependency. Use option A or B.
    Cannot find package 'zod'Your package imports zod without listing it. Add it to your own dependencies.
    PROFILE_INVALID: Profile not foundcreateSafran() looks for safran.yaml in the current directory. Run from the profile's directory or pass profile.
    AUTH_MISSING on the first ticket callThe environment variable named in the profile is not set in the shell that runs the script.