Commit Graph

3872 Commits

Author SHA1 Message Date
Debanjum
6ac1530816 More robustly default to searching all content type 2025-06-06 15:09:39 -07:00
Debanjum
b21706aa45 Drop help, summarize and automation /slash commands from chat api
Clean non useful slash commands to make chat API more maintanable.
- App version, chat model via /help is visible in other parts of the
  UX. Asking help questions with site:docs.khoj.dev filter isn't used
  or known to folks
- /summarize is esoterically tuned. Should be rewritten if add back.
  It wasn't being used by /research already
- Automations can be configured via UX. It wasn't being shown in UX
  already
2025-06-06 15:09:39 -07:00
Debanjum
7f6db526c3 Enforce json for non reasoning anthropic models even in deepthought 2025-06-06 13:28:18 -07:00
Debanjum
2f4160e24b Use single extract questions method across all LLMs for doc search
Using model specific extract questions was an artifact from older
times, with less guidable models.

New changes collate and reuse logic
- Rely on send_message_to_model_wrapper for model specific formatting.
- Use same prompt, context for all LLMs as can handle prompt variation.
- Use response schema enforcer to ensure response consistency across models.

Extract questions (because of its age) was the only tool directly within
each provider code. Put it into helpers to have all the (mini) tools
in one place.
2025-06-06 13:28:18 -07:00
Debanjum
c2cd92a454 [Breaking] Move automation api into new router with consistent routes
- Rename GET /api/automations to GET /api/automation
- Rename POST /api/trigger/automation to POST /api/automation/trigger
- Update calls to the automations API from the web app.
2025-06-06 13:28:18 -07:00
Debanjum
7dfa710cb4 Log invalid automation ids for investigation and clean-up 2025-06-06 13:28:18 -07:00
Debanjum
7d59688729 Move document search tool into helpers module with other tools
Document search (because of its age) was the only tool directly within
an api router. Put it into helpers to have all the (mini) tools in one
place.
2025-06-06 13:28:18 -07:00
Debanjum
1dbe60a8a2 Give more readable name to document search tool 2025-06-06 13:28:18 -07:00
Debanjum
38fa34a861 Simplify ai provider converse methods
- Add context based on information provided rather than conversation
  commands. Let caller handle passing appropriate context to ai
  provider converse methods
2025-06-06 13:28:18 -07:00
Debanjum
bfd4695705 Save conversation in common chat api func instead of each ai provider 2025-06-06 13:28:18 -07:00
Debanjum
e7584bc29d Remove old "Notes" stop keyword for openai api based models
They were when passing notes context to dumber models. Not required
for most models now.
2025-06-06 13:28:18 -07:00
Debanjum
d16f9f272b Add ability to retry a query from the web app 2025-06-06 13:28:18 -07:00
Debanjum
05d4e19cb8 Pass deep typed chat history for more ergonomic, readable, safe code
The chat dictionary is an artifact from earlier non-db chat history
storage. We've been ensuring new chat messages have valid type before
being written to DB for more than 6 months now.

Move to using the deeply typed chat history helps avoids null refs,
makes code more readable and easier to reason about.

Next Steps:
The current update entangles chat_history written to DB
with any virtual chat history message generated for intermediate
steps. The chat message type written to DB should be decoupled from
type that can be passed to AI model APIs (maybe?).

For now we've made the ChatMessage.message type looser to allow
for list[dict] type (apart from string). But later maybe a good idea
to decouple the chat_history recieved by send_message_to_model from
the chat_history saved to DB (which can then have its stricter type check)
2025-06-04 00:03:14 -07:00
Debanjum
430459a338 Release Khoj version 1.42.1 2025-06-03 21:46:16 -07:00
Debanjum
d618f2d650 Raise value error if research pick next tool isn't a dictionary
This will give a better error message with response content than the
failed to get errors from non dictionary response we were getting
earlier.
2025-06-03 21:45:11 -07:00
Debanjum
65d9ad6cb2 Use tool calls to enforce response schema for anthropic models
- Converts response schema into a anthropic tool call definition.
- Works with simple enums without needing to rely on $defs, $refs as
  unsupported by Anthropic API
- Do not force specific tool use as not supported with deep thought

This puts anthropic models on parity with openai, gemini models for
response schema following. Reduces need for complex json response
parsing on khoj end.
2025-06-03 21:05:29 -07:00
Debanjum
d45d9d4cfb Fix malformed user uuids to fix automations [automations data loss]
- **Malformed automations will be dropped**
  They can't run with malformed user uuid anyway.
2025-06-03 21:05:29 -07:00
Debanjum
4892e73323 Remove unsuppported NUL char from file, chat before save to DB 2025-06-03 21:05:29 -07:00
Debanjum
27534f6533 Make query field in context optional
Query field isn't set for all context. The current change was
preventing save to conversation errors when query unset in context.
2025-06-03 19:59:05 -07:00
Debanjum
6e48f4de84 Fix to switch text to speech model via API 2025-06-03 19:59:05 -07:00
Debanjum
f3a5fe1ae8 Release Khoj version 1.42.0 2025-06-01 20:52:25 -07:00
Debanjum
a236288ca9 Fixes to enable dockerized khoj to operate its computer 2025-06-01 19:19:01 -07:00
Debanjum
f95d352eb9 Ensure profile is right border aligned on khoj obsidian settings page
On wide screens it wasn't taking up the header wasn't taking up the
full width, so profile picture could hang out in the middle somewhere.
2025-06-01 17:02:08 -07:00
Debanjum
759ffc46b0 Default to read currently open file when chat with Khoj from Obsidian
Vault is already indexed, this should ease engaging with current
context more easily.
2025-06-01 16:56:19 -07:00
Debanjum
3fb8f77cd5 Fix terminal tool passed to claude 3.7 sonnet as anthropic operator 2025-06-01 16:55:17 -07:00
sabaimran
e9a107cc06 fix spelling of development 2025-06-01 13:41:39 -07:00
Henri Jamet
dbfac89a0c Major updates to Obsidian Khoj plugin chat interface and editing features (#1109)
## Description
This PR introduces significant improvements to the Obsidian Khoj
plugin's chat interface and editing capabilities, enhancing the overall
user experience and content management functionality.

## Features

### 🔍 Enhanced Communication Mode
I've implemented radio buttons below the chat window for easier
communication mode selection. The modes are now displayed as emojis in
the conversation for a cleaner interface, replacing the previous
text-based system (e.g., /default, /research). I've also documented the
search mode functionality in the help command.

#### Screenshots
- Radio buttons for mode selection
- Emoji display in conversations
![Recording 2025-02-11 at 18 56
10](https://github.com/user-attachments/assets/798d15df-ad32-45bd-b03f-581f6093575a)

### 💬 Revamped Message Interaction
I've redesigned the message buttons with improved spacing and color
coding for better visual differentiation. The new edit button allows
quick message modifications - clicking it removes the conversation up to
that point and copies the message to the input field for easy editing or
retrying questions.

#### Screenshots
- New message styling and color scheme
![Recording 2025-02-11 at 18 44
48](https://github.com/user-attachments/assets/159ece3d-2d80-4583-a7a8-2ef1f253adcc)
- Edit button functionality
![Recording 2025-02-11 at 18 47
52](https://github.com/user-attachments/assets/82ee7221-bc49-4088-9a98-744ef74d1e58)

### 🤖 Advanced Agent Selection System
I've added a new chat creation button with agent selection capability.
Users can now choose from their available agents when starting a new
chat. While agents can't be switched mid-conversation to maintain
context, users can easily start fresh conversations with different
agents.

#### Screenshots
- Agent selection dropdown
![Recording 2025-02-11 at 18 51
27](https://github.com/user-attachments/assets/be4208df-224c-45bf-a5b4-cf0a8068b102)

### 👁️ Real-Time Context Awareness
I've added a button that gives Khoj access to read Obsidian opened tabs.
This allows Khoj to read open notes and track changes in real-time,
maintaining a history of previous versions to provide more contextual
assistance.

#### Screenshots
- Window access toggle
![Recording 2025-02-11 at 18 59
01](https://github.com/user-attachments/assets/b596bfca-f622-41b7-b826-25a8e254d4a2)

### ✏️ Smart Document Editing
Inspired by Cursor IDE's intelligent editing and ChatGPT's Canvas
functionality, I've implemented a first version of a content creation
system we've been discussing. Using a JSON-based modification system,
Khoj can now make precise changes to specific parts of files, with
changes previewed in yellow highlighting before application.
Modification code blocks are neatly organized in collapsible sections
with clear action summaries. While this is just a first step, it's
working remarkably well and I have several ideas for expanding this
functionality to make Khoj an even more powerful content creation
assistant.

#### Screenshots
- JSON modification preview
- Change highlighting system
- Collapsible code blocks
- Accept/cancel controls
![Recording 2025-02-11 at 19 02
32](https://github.com/user-attachments/assets/88826c9e-d0c9-40da-ab78-9976c786aa9e)

---------

Co-authored-by: Debanjum <debanjum@gmail.com>
2025-06-01 10:42:36 +05:30
Debanjum
fa2e370ce6 Document how to enable and use computer operator in operator readme 2025-05-31 21:41:23 -07:00
Debanjum
68f7aae71c Install claude 4 sonnet, latest gemini 2.5s when configure on first run 2025-05-31 20:52:27 -07:00
Debanjum
b90b724f9a Disable openai, binary operator agents until they become useful 2025-05-31 20:51:08 -07:00
Debanjum
830a1af69e Render operator train of thought as video on web app to ease viewing
- You can seek through the train of thought video of computer operation or
  follow it in live mode.
- Interleaves video with normal text thoughts.
- Video available of old interactions and currently streaming message.
2025-05-31 20:51:08 -07:00
Debanjum
6821bd38ed Fix mypy typing errors in operator environment files
- Add type guards for action.path in drag vs text editor actions
- Added type guards for Union type attribute access
- Fixed variable naming conflicts between drag and text editor cases
- Resolved remaining typing issues in OpenAI, Anthropic agents
- Type guard without requiring another code indent level
2025-05-31 20:51:08 -07:00
Debanjum
c5c06a086e Fix, improve openai operator agent for interrupts, computer environment
- Create reusable method to call model
- Fix to summarize messages on operator run.
- Mark assistant tool calls with role = assistant, not environment

- Try fix message format when load after interrupts.
  Does not work well yet
2025-05-31 20:51:08 -07:00
Debanjum
f517566560 Improve invoking keybindings on computer always using lowercase keys
Previously CTRL+A would get triggered instead of ctrl+a. CTRL+A is
equivalent to ctrl+shift+a. This isn't intended and should be
called directly when required.

Now key combos like ctrl+a on computer firefox etc. work as expected
2025-05-31 20:51:08 -07:00
Debanjum
2558ac7f18 Show thinking and engage deep thought for gemini 2.5 model series
Gemini models now show (a summary of) their thoughts. Stream this in
research mode, similar to how it is done already for claude, deepseek,
qwen etc.
2025-05-31 20:51:08 -07:00
Debanjum
cecbfe35e2 Rename compile response into a private operator agents function 2025-05-31 20:51:08 -07:00
Debanjum
ded1db642c Get max context for user, operator model pair for context compression 2025-05-31 20:51:08 -07:00
Debanjum
7eaf0e80c5 Get max prompt size for given user, model via reusable functions 2025-05-31 20:51:08 -07:00
Debanjum
3797f03625 Log ai model usage on every call to get_chat_usage_metrics in debug mode 2025-05-31 20:51:08 -07:00
Debanjum
4cb900658d Cache system prompt, tools of anthropic operator agent for efficiency 2025-05-31 20:51:08 -07:00
Debanjum
928e5ee8ad Cache messages to anthropic models from chat actors for efficiency 2025-05-31 20:51:08 -07:00
Debanjum
0d1e6b0d53 Do not overwrite system_prompt for idempotent AI API calls retry
Previously on tenacity retry the system_prompt could get overwritten
2025-05-31 20:51:08 -07:00
Debanjum
e0ea151f20 Implement file editor and terminal tools, in-built in claude
This should improve viewing, editing files and viewing terminal
command outputs by anthropic operator
2025-05-31 20:51:08 -07:00
Debanjum
21bf7f1d6d Continue interrupted operator run with new query and previous context
Track research and operator results at each nested iteration step
using python object references + async events bubbled up from nested
iterators.

Instantiates operator with interrupted operator messages from research
or normal mode.

Reflects actual interaction trajectory as closely as possible to agent
including conversation history, partial operator trajectory and new
query for fine grained, corrigible steerability.

Research mode continues with operator tool directly if previous
iteration was an interrupted operator run.
2025-05-31 20:51:08 -07:00
Debanjum
de35d91e1d Pass previous trajectory to operator agents for context 2025-05-31 20:51:08 -07:00
Debanjum
864e0ac8b5 Simplify research iteration and main research function names 2025-05-31 20:51:08 -07:00
Debanjum
6c9d569a22 Fix to get user questions in chat history from user not khoj message
Since partial state reload after interrupt drops Khoj messages. The
assumption that there will always be a Khoj message after a user
message is broken. That is, there can now be multiple user messages
preceding a Khoj user message now.

This change allow for user queries to still be extracted for chat
history even if no khoj message follow.
2025-05-31 20:51:08 -07:00
Debanjum
b6aa77a6f5 Lookback 3 previous turns to select next tool, for questions history 2025-05-31 20:50:03 -07:00
Debanjum
d511cbfa34 Extract constructing question history into shared function for reuse
Minor logic update to only include non image inferred queries for
gemini, anthropic models as well instead of just for openai models.

Apart from that the extracted function should be functionally same.
2025-05-31 16:50:26 -07:00
Debanjum
da663e184c Type operator results. Enable storing, loading operator trajectories.
We were passing operator results as a simple dictionary. Strongly
typing it makes sense as operator results becomes more complex.

Storing operator results with trajectory on interrupts will allow
restarting interrupted operator run with agent messages of interrupted
trajectory loaded into operator agents
2025-05-31 16:50:26 -07:00