> For the complete documentation index, see [llms.txt](https://docs.gitloom.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gitloom.cloud/documentation/typescript.md).

# TypeScript

```bash
npm install @gitloomhq/sdk
```

A **drop-in beside the OpenAI and Anthropic SDKs**: wrap the client you already use, your call sites stay exactly as they are — one field richer — and the conversation manages itself.

## Drop-in

```ts
import OpenAI from 'openai'
import { Gitloom, withMemory } from '@gitloomhq/sdk'

const memory = new Gitloom()                        // reads GITLOOM_API_KEY
const openai = withMemory(new OpenAI(), { memory }) // ← the only setup

const res = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What camera do I own?' }],
  conversation: 'chat-42',                          // ← the only change per call
})
```

That's the whole loop — you pass **only the new message, never append anything**. Behind the call: the stored conversation supplies the earlier turns, memory is retrieved and injected as background, both turns are stored with the response's real token usage, compaction runs on cadence (default every 5 exchanges) or window pressure, and every compaction feeds the summarized turns to memory ingestion. Untitled conversations get a title automatically. Anthropic clients (`client.messages.create`) wrap identically. Calls without `conversation:` pass through untouched.

Configure the conversations the wrapper opens:

```ts
const openai = withMemory(new OpenAI(), {
  memory,
  conversations: {
    summarize: 'server',      // GitLoom's model compacts…
    // summarize: myFn,       // …or yours, locally
    compactEvery: 5,
    namespace: userId,
  },
})
```

## Added features, on the same client

```ts
const conv = await openai.gitloom.conversation('chat-42')

await conv.rewind(6)                                              // fork after seq 6
await conv.edit(4, { role: 'user', content: 'ask differently' })  // fork at same seq
await conv.editInPlace(4, { content: '[redacted]' })              // destroy the original (PII)
await conv.setTitle('Camera shopping')
await conv.branches()
```

These act on the **same managed conversation** the completions flow through — a rewind here is what the next `create({ conversation })` continues from. Direct memory: `openai.gitloom.memory.recall(...)` / `.remember(...)` — every hit carries per-arm scores, git history with the last diff, and relation snippets.

## Multimodal

```ts
import { textPart, imageData } from '@gitloomhq/sdk'

await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: [
    textPart("what's in this photo?"),
    imageData(b64, 'image/png'), // uploaded transparently; stored by reference
  ] }],
  conversation: 'chat-42',
})
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gitloom.cloud/documentation/typescript.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
