Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
454bb57484
|
|||
|
00cf6f8747
|
|||
|
6bd1e81a51
|
|||
|
a5a718b3e4
|
|||
|
7b65b62f58
|
|||
|
97c751c585
|
|||
|
e2f87481d6
|
|||
|
d6c2d57fd9
|
|||
|
71638fcaed
|
|||
|
767e929bf9
|
|||
|
3b6e3c34d5
|
+15
-1
@@ -1,10 +1,24 @@
|
|||||||
|
MODEL_PROVIDER=ollama
|
||||||
OLLAMA_BASE_URL=http://localhost:11434
|
OLLAMA_BASE_URL=http://localhost:11434
|
||||||
OLLAMA_MODEL=qwen3.5:9b
|
OLLAMA_MODEL=qwen3.5:9b
|
||||||
OLLAMA_NUM_CTX=64512
|
OLLAMA_NUM_CTX=64512
|
||||||
|
OPENAI_BASE_URL=https://api.openai.com/v1
|
||||||
|
OPENAI_MODEL=gpt-5.4-mini
|
||||||
|
OPENAI_API_KEY=
|
||||||
|
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
||||||
|
DEEPSEEK_MODEL=deepseek-v4-flash
|
||||||
|
DEEPSEEK_API_KEY=
|
||||||
|
MODEL_REASONING_EFFORT=medium
|
||||||
|
CODEX_COMMAND=codex
|
||||||
|
CODEX_MODEL=gpt-5.4
|
||||||
UEX_BASE_URL=https://api.uexcorp.space/2.0
|
UEX_BASE_URL=https://api.uexcorp.space/2.0
|
||||||
|
SCMDB_BASE_URL=https://scmdb.net
|
||||||
|
CORNERSTONE_BASE_URL=https://finder.cstone.space
|
||||||
|
SCWIKI_BASE_URL=https://starcitizen.tools
|
||||||
|
SCWIKI_API_BASE_URL=https://api.star-citizen.wiki
|
||||||
UEX_SECRET_KEY=
|
UEX_SECRET_KEY=
|
||||||
UEX_BEARER_TOKEN=
|
UEX_BEARER_TOKEN=
|
||||||
TRADERAI_USER_NAME=
|
TRADERAI_USER_NAME=
|
||||||
TRADERAI_MEMORY_PATH=
|
TRADERAI_MEMORY_PATH=
|
||||||
UEX_NOTIFICATION_POLL_SECONDS=60
|
UEX_NOTIFICATION_POLL_SECONDS=300
|
||||||
REQUIRE_WRITE_APPROVAL=true
|
REQUIRE_WRITE_APPROVAL=true
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
# TraderAI
|
# TraderAI
|
||||||
|
|
||||||
Local Ollama-powered chat for UEX marketplace workflows.
|
Local Ollama-, DeepSeek-, OpenAI-, or Codex-powered chat for UEX marketplace workflows.
|
||||||
|
|
||||||
## What It Does
|
## What It Does
|
||||||
|
|
||||||
- Searches active/current UEX marketplace listings through `GET /marketplace_listings/`.
|
- Searches active/current UEX marketplace listings through `GET /marketplace_listings/`.
|
||||||
|
- Searches SCMDB mission data so the assistant can answer what Star Citizen missions pay or reward, including UEC, reputation, item rewards, blueprint rewards, partial payouts, and hauling cargo.
|
||||||
|
- Searches Cornerstone Universal Item Finder data so the assistant can find where in-game items are sold, including store/location, base price, and verified date.
|
||||||
- Reads authenticated marketplace negotiations and negotiation messages when `UEX_SECRET_KEY` or `UEX_BEARER_TOKEN` is set.
|
- Reads authenticated marketplace negotiations and negotiation messages when `UEX_SECRET_KEY` or `UEX_BEARER_TOKEN` is set.
|
||||||
- Drafts negotiation messages and marketplace listings as pending actions.
|
- Drafts negotiation messages and marketplace listings as pending actions.
|
||||||
- Requires browser approval before sending authenticated write requests to UEX.
|
- Requires browser approval before sending authenticated write requests to UEX.
|
||||||
@@ -23,6 +25,12 @@ Local Ollama-powered chat for UEX marketplace workflows.
|
|||||||
```
|
```
|
||||||
|
|
||||||
3. Create `.env` from `.env.example` and set `UEX_SECRET_KEY` and/or `UEX_BEARER_TOKEN` if you want authenticated actions.
|
3. Create `.env` from `.env.example` and set `UEX_SECRET_KEY` and/or `UEX_BEARER_TOKEN` if you want authenticated actions.
|
||||||
|
If you want the cheapest hosted default, set `MODEL_PROVIDER=deepseek`, set `DEEPSEEK_API_KEY`, and keep `DEEPSEEK_MODEL=deepseek-v4-flash` unless you specifically want `deepseek-v4-pro`.
|
||||||
|
If you want to use OpenAI instead of Ollama, set `MODEL_PROVIDER=openai`, set `OPENAI_API_KEY`, and optionally change `OPENAI_MODEL` from the default `gpt-5.4-mini`.
|
||||||
|
If you want to use Codex models with ChatGPT/Codex OAuth, install the Codex CLI, set `MODEL_PROVIDER=codex`, and optionally change `CODEX_MODEL` from the default `gpt-5.4`. TraderAI uses the local `codex app-server` JSON-RPC interface for both authentication and chat turns.
|
||||||
|
`MODEL_REASONING_EFFORT` controls reasoning depth for DeepSeek, OpenAI, and Codex and defaults to `medium`.
|
||||||
|
`SCMDB_BASE_URL` defaults to `https://scmdb.net`.
|
||||||
|
`CORNERSTONE_BASE_URL` defaults to `https://finder.cstone.space`.
|
||||||
4. Install and run:
|
4. Install and run:
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
@@ -34,7 +42,7 @@ Local Ollama-powered chat for UEX marketplace workflows.
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
Ollama runs locally at `http://localhost:11434` by default. This app talks to Ollama's native chat API with tool schemas, then executes approved UEX calls in the FastAPI backend. `OLLAMA_NUM_CTX` controls the per-request Ollama context window; `64512` is the default because Ollama recommends at least 64k tokens for agent-style workflows when hardware allows it.
|
Ollama runs locally at `http://localhost:11434` by default. This app can talk to Ollama's native chat API, DeepSeek's OpenAI-compatible Chat Completions API, OpenAI's Chat Completions API, or the local Codex App Server authenticated through ChatGPT/Codex OAuth, then executes approved UEX calls in the FastAPI backend. `OLLAMA_NUM_CTX` controls the per-request Ollama context window; `64512` is the default because Ollama recommends at least 64k tokens for agent-style workflows when hardware allows it. DeepSeek context caching is provider-side and automatic when repeated prompt prefixes line up.
|
||||||
|
|
||||||
## Releases And Updates
|
## Releases And Updates
|
||||||
|
|
||||||
@@ -71,6 +79,8 @@ UEX notifications are checked every `UEX_NOTIFICATION_POLL_SECONDS` seconds by d
|
|||||||
## Sources Used
|
## Sources Used
|
||||||
|
|
||||||
- UEX SwaggerHub OpenAPI v2.1: https://app.swaggerhub.com/apis-docs/dolejska-daniel/UEX-API/v2.1
|
- UEX SwaggerHub OpenAPI v2.1: https://app.swaggerhub.com/apis-docs/dolejska-daniel/UEX-API/v2.1
|
||||||
|
- SCMDB mission data: https://scmdb.net/
|
||||||
|
- Cornerstone Universal Item Finder: https://finder.cstone.space/
|
||||||
- UEX marketplace listings docs: https://uexcorp.space/api/documentation/id/get_marketplace_listings/?is_kiosk=1
|
- UEX marketplace listings docs: https://uexcorp.space/api/documentation/id/get_marketplace_listings/?is_kiosk=1
|
||||||
- UEX negotiation message docs: https://uexcorp.space/api/documentation/id/post_marketplace_negotiations_messages/?is_kiosk=1
|
- UEX negotiation message docs: https://uexcorp.space/api/documentation/id/post_marketplace_negotiations_messages/?is_kiosk=1
|
||||||
- Ollama tool calling docs: https://docs.ollama.com/capabilities/tool-calling
|
- Ollama tool calling docs: https://docs.ollama.com/capabilities/tool-calling
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
Add an option to use API: marketplace_trends as a tool for the AI from UEX
|
||||||
|
Closed negotiations (not open): All other negotiations show date_closed > 0 or were marked as closed by client or Riggs0. find a more relaiable way to mark them as closed/open negotations for the AI
|
||||||
|
some times when having a long tool call the assiant will not reply what it did/an awnser see ---
|
||||||
|
|
||||||
|
2026-05-06T19:41:17.786390+00:00 assistant
|
||||||
|
Looking at the historical price data for **Wikelo Favor**, here's what happened recently:
|
||||||
|
|
||||||
|
## Recent Historical Price Activity (Last Few Hours)
|
||||||
|
|
||||||
|
**Sell Orders (asking prices):**
|
||||||
|
- **1,500,000,000 UEC** (two listings at the same time)
|
||||||
|
- **1,000,000,000 UEC** (one listing)
|
||||||
|
|
||||||
|
**Buy Orders (bidding prices):**
|
||||||
|
- **300,000,000 UEC** (multiple buy orders)
|
||||||
|
|
||||||
|
**Current Active Listings:**
|
||||||
|
- **500,000,000 UEC** per unit (9 in stock)
|
||||||
|
- **1,000,000,000 UEC** per stack (5 in stock)
|
||||||
|
|
||||||
|
## Price Range Analysis
|
||||||
|
|
||||||
|
The market has been fluctuating significantly:
|
||||||
|
- **Lowest recent price:** 300M UEC (buy orders)
|
||||||
|
- **Highest recent price:** 1.5B UEC (sell orders)
|
||||||
|
- **Current active listings:** 500M - 1B UEC
|
||||||
|
|
||||||
|
**What This Means for Pricing:**
|
||||||
|
If you're looking to sell your Wikelo Favors competitively, I'd recommend pricing between **500M - 800M UEC** to match the current market, which is more realistic than the high-end 1.5B UEC listings. The 300M UEC buy orders suggest that's where many players are willing to bid, so anything between 300M-500M UEC might actually sell quickly.
|
||||||
|
|
||||||
|
Would you like me to search for more specific listings or help you create a listing at a particular price point?
|
||||||
|
2026-05-06T19:40:51.966297+00:00 user
|
||||||
|
What happened
|
||||||
|
2026-05-06T19:38:27.178306+00:00 assistant
|
||||||
|
2026-05-06T19:37:52.825713+00:00 user
|
||||||
|
I want you to use the history tools and show me what they were selling for recently so I know what to price mine at
|
||||||
|
|
||||||
|
|
||||||
|
----
|
||||||
|
I want the user to beable to see and directly respond to negotations in the app seperate the ai so it should work like this, when a negotation is refrenced the user should be able to click on it and a floating block wiht the chat messages and an area for the user to type one to the other party,
|
||||||
|
|
||||||
|
I want the message box for the user to be forced to the bottom of the viewport, right now when their are no messages if moves up to the top then its sticky on the bottom when messages happen
|
||||||
|
|
||||||
|
I want the users messages to right aligned and not have abunch of empty space to the right or left of it.
|
||||||
|
|
||||||
|
when I approve messages they dont send I see
|
||||||
|
|
||||||
|
Approval result:
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
"status": "ok",
|
||||||
|
|
||||||
|
"http_code": 200,
|
||||||
|
|
||||||
|
"data": {
|
||||||
|
|
||||||
|
"id_message": "0"
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
"message": "Sandbox mode"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
and an error that a python dll is missing from the exe.
|
||||||
|
|
||||||
|
|
||||||
|
I want a default collapsed sidebar that has chats, they are to be like chat gpt chats where memories persist across them and the chat history is confied to them. I want the users to beable to delete them with a trash can icon, and I want them saved to disk.
|
||||||
|
|
||||||
|
|
||||||
|
the wake jobs may not be working properly, I scehudled one and
|
||||||
|
I see I'll check your UEX notifications and handle your open negotiations now!
|
||||||
|
but I dont see an output for this job. to fix this I want to have an Inbox that the AI can send messages to. It should be the output of the wake job and have the ablity to continue the coversation in a new chat.
|
||||||
|
|
||||||
|
make a popup that prompts a user to update on open when a new update is avaible.
|
||||||
+9
-1
@@ -1,14 +1,22 @@
|
|||||||
# -*- mode: python ; coding: utf-8 -*-
|
# -*- mode: python ; coding: utf-8 -*-
|
||||||
|
|
||||||
from PyInstaller.utils.hooks import collect_all
|
from PyInstaller.utils.hooks import collect_all
|
||||||
|
import sysconfig
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
webview_datas, webview_binaries, webview_hiddenimports = collect_all("webview")
|
webview_datas, webview_binaries, webview_hiddenimports = collect_all("webview")
|
||||||
|
|
||||||
|
python_binaries = []
|
||||||
|
for dll_name in ("python3.dll", f"python{sysconfig.get_python_version().replace('.', '')}.dll"):
|
||||||
|
dll_path = Path(sysconfig.get_config_var("BINDIR") or "") / dll_name
|
||||||
|
if dll_path.exists():
|
||||||
|
python_binaries.append((str(dll_path), "."))
|
||||||
|
|
||||||
|
|
||||||
a = Analysis(
|
a = Analysis(
|
||||||
["traderai\\desktop.py"],
|
["traderai\\desktop.py"],
|
||||||
pathex=[],
|
pathex=[],
|
||||||
binaries=webview_binaries,
|
binaries=[*webview_binaries, *python_binaries],
|
||||||
datas=[("web", "web"), *webview_datas],
|
datas=[("web", "web"), *webview_datas],
|
||||||
hiddenimports=[
|
hiddenimports=[
|
||||||
*webview_hiddenimports,
|
*webview_hiddenimports,
|
||||||
|
|||||||
+9
-1
@@ -1,14 +1,22 @@
|
|||||||
# -*- mode: python ; coding: utf-8 -*-
|
# -*- mode: python ; coding: utf-8 -*-
|
||||||
|
|
||||||
from PyInstaller.utils.hooks import collect_all
|
from PyInstaller.utils.hooks import collect_all
|
||||||
|
import sysconfig
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
webview_datas, webview_binaries, webview_hiddenimports = collect_all("webview")
|
webview_datas, webview_binaries, webview_hiddenimports = collect_all("webview")
|
||||||
|
|
||||||
|
python_binaries = []
|
||||||
|
for dll_name in ("python3.dll", f"python{sysconfig.get_python_version().replace('.', '')}.dll"):
|
||||||
|
dll_path = Path(sysconfig.get_config_var("BINDIR") or "") / dll_name
|
||||||
|
if dll_path.exists():
|
||||||
|
python_binaries.append((str(dll_path), "."))
|
||||||
|
|
||||||
|
|
||||||
a = Analysis(
|
a = Analysis(
|
||||||
["traderai\\desktop.py"],
|
["traderai\\desktop.py"],
|
||||||
pathex=[],
|
pathex=[],
|
||||||
binaries=webview_binaries,
|
binaries=[*webview_binaries, *python_binaries],
|
||||||
datas=[("web", "web"), *webview_datas],
|
datas=[("web", "web"), *webview_datas],
|
||||||
hiddenimports=[
|
hiddenimports=[
|
||||||
*webview_hiddenimports,
|
*webview_hiddenimports,
|
||||||
|
|||||||
+9
-2
@@ -1,7 +1,7 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "traderai"
|
name = "traderai"
|
||||||
version = "0.0.2"
|
version = "0.0.8"
|
||||||
description = "Local Ollama-powered assistant for UEX marketplace workflows."
|
description = "Local Ollama, OpenAI, or Codex assistant for UEX marketplace workflows."
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"apscheduler>=3.10.4",
|
"apscheduler>=3.10.4",
|
||||||
@@ -36,3 +36,10 @@ include = ["traderai*"]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
import asyncio
|
||||||
|
import itertools
|
||||||
|
|
||||||
from traderai.agent import OllamaAgent, SYSTEM_PROMPT
|
from traderai.agent import OllamaAgent, SYSTEM_PROMPT
|
||||||
from traderai.memory import MemoryStore
|
from traderai.memory import MemoryStore
|
||||||
@@ -12,6 +14,130 @@ class EmptyTools:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
class WakeTools(EmptyTools):
|
||||||
|
def __init__(self):
|
||||||
|
self.calls = []
|
||||||
|
|
||||||
|
async def execute(self, name, arguments):
|
||||||
|
self.calls.append((name, arguments))
|
||||||
|
return {"count": 1, "notifications": [{"message": "Buyer replied"}]}
|
||||||
|
|
||||||
|
|
||||||
|
class WakeAgent(OllamaAgent):
|
||||||
|
def __init__(self, memory):
|
||||||
|
super().__init__("http://127.0.0.1:1", "missing-model", WakeTools(), memory=memory)
|
||||||
|
self.responses = [
|
||||||
|
{
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"function": {
|
||||||
|
"name": "check_uex_notifications",
|
||||||
|
"arguments": {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{"message": {"role": "assistant", "content": "I checked notifications: Buyer replied."}},
|
||||||
|
]
|
||||||
|
|
||||||
|
async def ensure_available(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _ollama_chat(self, *args, **kwargs):
|
||||||
|
return self.responses.pop(0)
|
||||||
|
|
||||||
|
|
||||||
|
class TitleAgent(OllamaAgent):
|
||||||
|
def __init__(self, memory):
|
||||||
|
super().__init__("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory)
|
||||||
|
|
||||||
|
async def ensure_available(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _generate_chat_title(self, first_message):
|
||||||
|
return "UEX Market Check"
|
||||||
|
|
||||||
|
async def _ollama_chat(self, *args, **kwargs):
|
||||||
|
return {"message": {"role": "assistant", "content": "Done"}}
|
||||||
|
|
||||||
|
|
||||||
|
class ImageCaptureAgent(OllamaAgent):
|
||||||
|
def __init__(self, memory):
|
||||||
|
super().__init__("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory)
|
||||||
|
self.last_messages = None
|
||||||
|
|
||||||
|
async def ensure_available(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _chat_once(self, query="", messages=None, **kwargs):
|
||||||
|
self.last_messages = messages
|
||||||
|
return {"message": {"role": "assistant", "content": "Seen"}}
|
||||||
|
|
||||||
|
|
||||||
|
class SlowToolTools(EmptyTools):
|
||||||
|
schemas = [
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "slow_tool",
|
||||||
|
"description": "Slow fake tool.",
|
||||||
|
"parameters": {"type": "object", "properties": {}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.calls = 0
|
||||||
|
|
||||||
|
async def execute(self, name, arguments):
|
||||||
|
self.calls += 1
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
|
return {"status": "ok", "value": "slow result"}
|
||||||
|
|
||||||
|
|
||||||
|
class SlowStreamingAgent(OllamaAgent):
|
||||||
|
def __init__(self, memory):
|
||||||
|
super().__init__("http://127.0.0.1:1", "missing-model", SlowToolTools(), memory=memory)
|
||||||
|
self.stream_calls = 0
|
||||||
|
|
||||||
|
async def health(self):
|
||||||
|
return {"online": True, "model": "test", "base_url": self.base_url}
|
||||||
|
|
||||||
|
async def _ollama_chat_stream(self, *args, **kwargs):
|
||||||
|
self.stream_calls += 1
|
||||||
|
if self.stream_calls == 1:
|
||||||
|
yield {
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"tool_calls": [{"function": {"name": "slow_tool", "arguments": {}}}],
|
||||||
|
},
|
||||||
|
"done": True,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
yield {"message": {"role": "assistant", "content": ""}, "done": True}
|
||||||
|
|
||||||
|
|
||||||
|
class FailingAfterToolAgent(SlowStreamingAgent):
|
||||||
|
async def _ollama_chat_stream(self, *args, **kwargs):
|
||||||
|
self.stream_calls += 1
|
||||||
|
if self.stream_calls == 1:
|
||||||
|
yield {
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"tool_calls": [{"function": {"name": "slow_tool", "arguments": {}}}],
|
||||||
|
},
|
||||||
|
"done": True,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
raise RuntimeError("ollama timed out")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_chat_events_warns_when_ollama_offline():
|
async def test_chat_events_warns_when_ollama_offline():
|
||||||
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools())
|
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools())
|
||||||
@@ -90,3 +216,217 @@ def test_ollama_options_include_num_ctx():
|
|||||||
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), num_ctx=64000)
|
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), num_ctx=64000)
|
||||||
|
|
||||||
assert agent._ollama_options() == {"num_ctx": 64000}
|
assert agent._ollama_options() == {"num_ctx": 64000}
|
||||||
|
|
||||||
|
|
||||||
|
def test_deepseek_tool_rounds_are_not_capped_at_ten():
|
||||||
|
agent = OllamaAgent("https://api.deepseek.com", "deepseek-v4-flash", EmptyTools(), provider="deepseek", api_key="test")
|
||||||
|
|
||||||
|
rounds = list(itertools.islice(agent._tool_rounds(), 12))
|
||||||
|
|
||||||
|
assert len(rounds) == 12
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_draft_normalization_extracts_json_and_defaults():
|
||||||
|
seed = {"title": "Wikelo Polaris", "objective": "Find parts", "kind": "buying", "constraints": {}, "items": []}
|
||||||
|
raw = 'draft:\n{"title":"Wikelo Polaris Parts","objective":"Find and draft deals for the parts below","kind":"buying","cadence":"0 */3 * * *","constraints":{"message_tone":"casual","instructions":"Prioritize cheap listings first."},"items":[{"item_name":"RCMBNT-RGL-1","desired_quantity":2}]}'
|
||||||
|
|
||||||
|
draft = OllamaAgent._normalize_plan_draft(raw, seed)
|
||||||
|
|
||||||
|
assert draft["title"] == "Wikelo Polaris Parts"
|
||||||
|
assert draft["cadence"] == "0 */3 * * *"
|
||||||
|
assert draft["constraints"]["message_tone"] == "casual"
|
||||||
|
assert draft["items"][0]["item_name"] == "RCMBNT-RGL-1"
|
||||||
|
assert draft["items"][0]["desired_quantity"] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_draft_heuristic_fills_in_basic_instructions():
|
||||||
|
seed = {"title": "Watch open negotiations", "objective": "", "kind": "custom", "constraints": {}, "items": []}
|
||||||
|
|
||||||
|
draft = OllamaAgent._heuristic_plan_draft(seed)
|
||||||
|
|
||||||
|
assert draft["kind"] == "custom"
|
||||||
|
assert draft["cadence"] == "0 */4 * * *"
|
||||||
|
assert "summarize" in draft["constraints"]["instructions"].casefold()
|
||||||
|
assert draft["constraints"]["message_tone"] == "friendly and direct"
|
||||||
|
|
||||||
|
|
||||||
|
def test_codex_prompt_mentions_tools_and_images(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
agent = OllamaAgent("codex", "gpt-5.3-codex", EmptyTools(), memory=memory, provider="codex")
|
||||||
|
|
||||||
|
prompt = agent._codex_cli_prompt(
|
||||||
|
"check listing",
|
||||||
|
[
|
||||||
|
{"role": "system", "content": SYSTEM_PROMPT},
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "Look at this",
|
||||||
|
"images": ["ZmFrZQ=="],
|
||||||
|
"image_content_types": ["image/png"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_123",
|
||||||
|
"type": "function",
|
||||||
|
"function": {"name": "search_marketplace_listings", "arguments": "{\"commodity\":\"gold\"}"},
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "tool",
|
||||||
|
"tool_name": "search_marketplace_listings",
|
||||||
|
"tool_call_id": "call_123",
|
||||||
|
"content": "{\"ok\":true}",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "Available tools" in prompt
|
||||||
|
assert "attached images: 1" in prompt
|
||||||
|
assert "search_marketplace_listings" in prompt
|
||||||
|
assert "tool search_marketplace_listings" in prompt
|
||||||
|
|
||||||
|
|
||||||
|
def test_deepseek_openai_messages_include_reasoning_content_for_tool_turns():
|
||||||
|
agent = OllamaAgent("https://api.deepseek.com", "deepseek-v4-flash", EmptyTools(), provider="deepseek", api_key="test")
|
||||||
|
|
||||||
|
messages = agent._openai_messages(
|
||||||
|
"check listing",
|
||||||
|
[
|
||||||
|
{"role": "system", "content": SYSTEM_PROMPT},
|
||||||
|
{"role": "user", "content": "Check this listing"},
|
||||||
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"reasoning_content": "I should check the current listing first.",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"id": "call_123",
|
||||||
|
"type": "function",
|
||||||
|
"function": {"name": "search_marketplace_listings", "arguments": "{\"query\":\"panel\"}"},
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{"role": "tool", "tool_name": "search_marketplace_listings", "tool_call_id": "call_123", "content": "{\"ok\":true}"},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
assistant_turn = next(message for message in messages if message["role"] == "assistant")
|
||||||
|
assert assistant_turn["reasoning_content"] == "I should check the current listing first."
|
||||||
|
|
||||||
|
|
||||||
|
def test_codex_structured_response_extracts_text_and_tool_calls():
|
||||||
|
agent = OllamaAgent("codex", "gpt-5.3-codex", EmptyTools(), provider="codex")
|
||||||
|
|
||||||
|
result = agent._codex_structured_response(
|
||||||
|
{
|
||||||
|
"kind": "tool_call",
|
||||||
|
"message": "",
|
||||||
|
"tool_name": "search_marketplace_listings",
|
||||||
|
"arguments_json": "{\"commodity\":\"gold\"}",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["message"]["content"] == ""
|
||||||
|
assert result["message"]["tool_calls"] == [
|
||||||
|
{
|
||||||
|
"id": result["message"]["tool_calls"][0]["id"],
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "search_marketplace_listings",
|
||||||
|
"arguments": "{\"commodity\":\"gold\"}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_codex_exec_output_reads_final_json():
|
||||||
|
agent = OllamaAgent("codex", "gpt-5.3-codex", EmptyTools(), provider="codex")
|
||||||
|
|
||||||
|
result = agent._parse_codex_exec_output(
|
||||||
|
{
|
||||||
|
"returncode": 0,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": "",
|
||||||
|
"events": [
|
||||||
|
{"type": "thread.started", "thread_id": "abc"},
|
||||||
|
{"type": "item.completed", "item": {"type": "agent_message", "text": "{\"kind\":\"final\",\"message\":\"hello\",\"tool_name\":\"\",\"arguments_json\":\"{}\"}"}},
|
||||||
|
{"type": "turn.completed"},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result == {"kind": "final", "message": "hello", "tool_name": "", "arguments_json": "{}"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_wake_response_executes_tool_calls(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
agent = WakeAgent(memory)
|
||||||
|
|
||||||
|
response = await agent.generate_wake_response("Scheduled wake job fired. Check notifications.")
|
||||||
|
|
||||||
|
assert response == "I checked notifications: Buyer replied."
|
||||||
|
assert agent.tools.calls == [("check_uex_notifications", {})]
|
||||||
|
wake_rows = memory.recent_conversation(thread_id="wake")
|
||||||
|
assert wake_rows[-1]["content"] == "I checked notifications: Buyer replied."
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_first_chat_message_generates_thread_title(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
thread = memory.create_thread()
|
||||||
|
agent = TitleAgent(memory)
|
||||||
|
|
||||||
|
result = await agent.chat("Check UEX market listings", thread_id=thread["id"])
|
||||||
|
|
||||||
|
assert result["message"] == "Done"
|
||||||
|
assert memory.get_thread(thread["id"])["title"] == "UEX Market Check"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_chat_includes_pasted_images_and_memory_note(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
agent = ImageCaptureAgent(memory)
|
||||||
|
|
||||||
|
result = await agent.chat(
|
||||||
|
"",
|
||||||
|
images=[{"name": "listing.png", "content_type": "image/png", "image_data": "ZmFrZS1pbWFnZQ=="}],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["message"] == "Seen"
|
||||||
|
user_message = next(message for message in reversed(agent.last_messages) if message.get("role") == "user")
|
||||||
|
assert user_message["images"] == ["ZmFrZS1pbWFnZQ=="]
|
||||||
|
assert user_message["content"] == "Please analyze the attached image."
|
||||||
|
assert "[Attached 1 pasted image]" in memory.recent_conversation()[-2]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_chat_events_returns_fallback_after_slow_tool_and_empty_final_response(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
agent = SlowStreamingAgent(memory)
|
||||||
|
|
||||||
|
events = [event async for event in agent.chat_events("run a slow tool")]
|
||||||
|
text = "".join(event.get("content", "") for event in events if event["type"] == "token")
|
||||||
|
|
||||||
|
assert agent.tools.calls == 1
|
||||||
|
assert "I completed the tool call" in text
|
||||||
|
assert "slow result" in text
|
||||||
|
assert events[-1]["type"] == "done"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_chat_events_returns_tool_result_when_model_fails_after_slow_tool(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
agent = FailingAfterToolAgent(memory)
|
||||||
|
|
||||||
|
events = [event async for event in agent.chat_events("run a slow tool")]
|
||||||
|
text = "".join(event.get("content", "") for event in events if event["type"] == "token")
|
||||||
|
|
||||||
|
assert agent.tools.calls == 1
|
||||||
|
assert "local model stopped after the tool call" in text
|
||||||
|
assert "slow result" in text
|
||||||
|
assert events[-1]["type"] == "done"
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
from traderai.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_provider_codex_falls_back_to_ollama():
|
||||||
|
settings = Settings(model_provider="codex")
|
||||||
|
|
||||||
|
assert settings.model_provider == "ollama"
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_provider_openai_falls_back_to_ollama():
|
||||||
|
settings = Settings(model_provider="openai")
|
||||||
|
|
||||||
|
assert settings.model_provider == "ollama"
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_provider_accepts_deepseek():
|
||||||
|
settings = Settings(model_provider="deepseek")
|
||||||
|
|
||||||
|
assert settings.model_provider == "deepseek"
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_provider_invalid_value_falls_back_to_ollama():
|
||||||
|
settings = Settings(model_provider="something-else")
|
||||||
|
|
||||||
|
assert settings.model_provider == "ollama"
|
||||||
|
|
||||||
|
|
||||||
|
def test_reasoning_effort_normalizes_invalid_values():
|
||||||
|
settings = Settings(model_reasoning_effort="whatever")
|
||||||
|
|
||||||
|
assert settings.model_reasoning_effort == "medium"
|
||||||
|
|
||||||
|
|
||||||
|
def test_reasoning_effort_accepts_supported_values():
|
||||||
|
settings = Settings(model_reasoning_effort="high")
|
||||||
|
|
||||||
|
assert settings.model_reasoning_effort == "high"
|
||||||
|
|
||||||
|
|
||||||
|
def test_reasoning_effort_accepts_max():
|
||||||
|
settings = Settings(model_reasoning_effort="max")
|
||||||
|
|
||||||
|
assert settings.model_reasoning_effort == "max"
|
||||||
@@ -25,3 +25,52 @@ def test_memory_store_clear_selected_sections(tmp_path):
|
|||||||
assert snapshot["memories"] == []
|
assert snapshot["memories"] == []
|
||||||
assert snapshot["conversations"] == []
|
assert snapshot["conversations"] == []
|
||||||
assert snapshot["profile"][0]["key"] == "configured_name"
|
assert snapshot["profile"][0]["key"] == "configured_name"
|
||||||
|
|
||||||
|
|
||||||
|
def test_memory_store_separates_chat_threads_but_keeps_shared_memories(tmp_path):
|
||||||
|
store = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
first = store.create_thread("First")
|
||||||
|
second = store.create_thread("Second")
|
||||||
|
store.add_conversation("user", "first thread message", first["id"])
|
||||||
|
store.add_conversation("user", "second thread message", second["id"])
|
||||||
|
store.remember("preference", "Shared trading preference", importance=5)
|
||||||
|
|
||||||
|
first_rows = store.recent_conversation(thread_id=first["id"])
|
||||||
|
second_rows = store.recent_conversation(thread_id=second["id"])
|
||||||
|
|
||||||
|
assert [row["content"] for row in first_rows] == ["first thread message"]
|
||||||
|
assert [row["content"] for row in second_rows] == ["second thread message"]
|
||||||
|
assert store.recall("trading preference")[0]["content"] == "Shared trading preference"
|
||||||
|
|
||||||
|
|
||||||
|
def test_memory_store_renames_threads_and_deletes_outbox_items(tmp_path):
|
||||||
|
store = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
thread = store.create_thread("New chat")
|
||||||
|
store.add_outbox("Wake job result")
|
||||||
|
inbox_id = store.list_outbox()[0]["id"]
|
||||||
|
|
||||||
|
renamed = store.rename_thread(thread["id"], " Market Check ")
|
||||||
|
deleted = store.delete_outbox(inbox_id)
|
||||||
|
|
||||||
|
assert renamed["title"] == "Market Check"
|
||||||
|
assert deleted is True
|
||||||
|
assert store.list_outbox() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_memory_store_uses_absolute_path_across_working_directory_changes(tmp_path, monkeypatch):
|
||||||
|
original_cwd = tmp_path / "start"
|
||||||
|
original_cwd.mkdir()
|
||||||
|
monkeypatch.chdir(original_cwd)
|
||||||
|
|
||||||
|
store = MemoryStore("data/memory.sqlite3")
|
||||||
|
|
||||||
|
moved_cwd = tmp_path / "moved"
|
||||||
|
moved_cwd.mkdir()
|
||||||
|
monkeypatch.chdir(moved_cwd)
|
||||||
|
|
||||||
|
store.add_outbox("Notification survived cwd change")
|
||||||
|
|
||||||
|
snapshot = store.inspect()
|
||||||
|
|
||||||
|
assert store.path.is_absolute()
|
||||||
|
assert snapshot["outbox"][0]["content"] == "Notification survived cwd change"
|
||||||
|
|||||||
@@ -0,0 +1,256 @@
|
|||||||
|
import pytest
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from traderai.memory import MemoryStore, utc_now
|
||||||
|
from traderai.plans import ContinualPlanRunner, ContinualPlanStore
|
||||||
|
from traderai.scheduler import WakeScheduler
|
||||||
|
from traderai.tools import ToolRegistry
|
||||||
|
|
||||||
|
|
||||||
|
class BuyingUEX:
|
||||||
|
def __init__(self):
|
||||||
|
self.posts = []
|
||||||
|
|
||||||
|
async def get(self, path, params=None, authenticated=False):
|
||||||
|
if path == "marketplace_listings":
|
||||||
|
return {
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id": 501,
|
||||||
|
"slug": "wikelo-panel-good",
|
||||||
|
"title": "Wikelo Idris panel",
|
||||||
|
"operation": "sell",
|
||||||
|
"type": "item",
|
||||||
|
"price": 450_000,
|
||||||
|
"currency": "UEC",
|
||||||
|
"in_stock": 2,
|
||||||
|
"location": "Orison",
|
||||||
|
"user_username": "seller_a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 502,
|
||||||
|
"slug": "wikelo-panel-expensive",
|
||||||
|
"title": "Wikelo Idris panel premium",
|
||||||
|
"operation": "sell",
|
||||||
|
"type": "item",
|
||||||
|
"price": 900_000,
|
||||||
|
"currency": "UEC",
|
||||||
|
"in_stock": 1,
|
||||||
|
"location": "Area18",
|
||||||
|
"user_username": "seller_b",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
return {"data": []}
|
||||||
|
|
||||||
|
async def post(self, path, payload, authenticated=True):
|
||||||
|
self.posts.append({"path": path, "payload": payload, "authenticated": authenticated})
|
||||||
|
return {"status": "ok", "posted": self.posts[-1]}
|
||||||
|
|
||||||
|
async def delete(self, path, params=None, authenticated=True):
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
class FakePlanAgent:
|
||||||
|
def __init__(self):
|
||||||
|
self.prompts = []
|
||||||
|
|
||||||
|
async def generate_wake_response(self, wake_message):
|
||||||
|
self.prompts.append(wake_message)
|
||||||
|
return "Custom plan checked notifications and found no blockers."
|
||||||
|
|
||||||
|
|
||||||
|
def plan_stack(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
store = ContinualPlanStore(memory)
|
||||||
|
scheduler = WakeScheduler(memory)
|
||||||
|
tools = ToolRegistry(BuyingUEX(), memory=memory, scheduler=scheduler, plan_store=store)
|
||||||
|
runner = ContinualPlanRunner(store, tools, memory)
|
||||||
|
tools.plan_runner = runner
|
||||||
|
scheduler.bind_plan_runner(runner)
|
||||||
|
return memory, store, tools, runner, scheduler
|
||||||
|
|
||||||
|
|
||||||
|
def test_continual_plan_store_creates_needs_input_plan(tmp_path):
|
||||||
|
_, store, _, _, _ = plan_stack(tmp_path)
|
||||||
|
|
||||||
|
plan = store.create_plan("Wikelo Idris", objective="Get all parts", items=[])
|
||||||
|
|
||||||
|
assert plan["status"] == "needs_input"
|
||||||
|
assert plan["items"] == []
|
||||||
|
assert plan["events"][0]["kind"] == "needs_input"
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_plan_without_items_is_active(tmp_path):
|
||||||
|
_, store, _, _, _ = plan_stack(tmp_path)
|
||||||
|
|
||||||
|
plan = store.create_plan("Watch negotiations", kind="custom", objective="Check replies and summarize next steps", items=[])
|
||||||
|
|
||||||
|
assert plan["status"] == "active"
|
||||||
|
assert plan["items"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_continual_plan_store_creates_buying_checklist(tmp_path):
|
||||||
|
_, store, _, _, _ = plan_stack(tmp_path)
|
||||||
|
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Wikelo Idris",
|
||||||
|
objective="Get all listed parts",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel", "desired_quantity": 2, "max_unit_price": 500_000}],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert plan["status"] == "active"
|
||||||
|
assert plan["items"][0]["item_name"] == "Wikelo Idris panel"
|
||||||
|
assert plan["items"][0]["desired_quantity"] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_continual_plan_store_deletes_plan_and_related_records(tmp_path):
|
||||||
|
_, store, _, _, _ = plan_stack(tmp_path)
|
||||||
|
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Delete me",
|
||||||
|
objective="Remove everything",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel", "desired_quantity": 1}],
|
||||||
|
)
|
||||||
|
item_id = int(plan["items"][0]["id"])
|
||||||
|
candidate = store.upsert_candidate(plan["id"], item_id, {"id": "listing-1", "title": "Panel", "price": 10}, 0.9)
|
||||||
|
store.add_negotiation(plan["id"], item_id, int(candidate["id"]), {"listing_id": "listing-1", "listing_slug": "panel", "id_negotiation": "neg-1", "hash": "hash-1"})
|
||||||
|
|
||||||
|
assert store.delete_plan(plan["id"]) is True
|
||||||
|
assert store.get_plan(plan["id"]) is None
|
||||||
|
assert store.list_items(plan["id"]) == []
|
||||||
|
assert store.list_candidates(plan["id"]) == []
|
||||||
|
assert store.list_negotiations(plan["id"]) == []
|
||||||
|
assert store.list_events(plan["id"]) == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_buying_runner_tracks_candidates_and_drafts_only(tmp_path):
|
||||||
|
memory, store, tools, runner, _ = plan_stack(tmp_path)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Wikelo Idris",
|
||||||
|
objective="Get all listed parts",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel", "desired_quantity": 1, "max_unit_price": 500_000}],
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await runner.run_plan(plan["id"])
|
||||||
|
snapshot = store.get_plan(plan["id"])
|
||||||
|
|
||||||
|
assert result["drafted"] == 1
|
||||||
|
assert any(candidate["listing_id"] == "501" and candidate["status"] == "drafted" for candidate in snapshot["candidates"])
|
||||||
|
assert snapshot["negotiations"][0]["status"] == "drafted"
|
||||||
|
assert len(tools.pending_actions) == 1
|
||||||
|
assert not tools.uex.posts
|
||||||
|
assert "Drafted 1 negotiation" in memory.list_outbox()[0]["content"]
|
||||||
|
pending = next(iter(tools.pending_actions.values()))
|
||||||
|
assert "Tone note" not in pending.payload["message"]
|
||||||
|
assert "Polaris build" not in pending.payload["message"] or "putting together parts for a Polaris build" in pending.payload["message"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_plan_approval_logs_back_to_plan(tmp_path):
|
||||||
|
_, store, tools, runner, _ = plan_stack(tmp_path)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Wikelo Idris",
|
||||||
|
objective="Get all listed parts",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel", "max_unit_price": 500_000}],
|
||||||
|
)
|
||||||
|
await runner.run_plan(plan["id"])
|
||||||
|
action_id = next(iter(tools.pending_actions))
|
||||||
|
|
||||||
|
approved = await tools.approve(action_id)
|
||||||
|
snapshot = store.get_plan(plan["id"])
|
||||||
|
|
||||||
|
assert approved["posted"]["path"] == "marketplace_negotiations_messages"
|
||||||
|
assert any(event["kind"] == "approved" for event in snapshot["events"])
|
||||||
|
assert any(negotiation["status"] == "approved" for negotiation in snapshot["negotiations"])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_custom_runner_continues_plan_through_agent(tmp_path):
|
||||||
|
memory, store, tools, runner, _ = plan_stack(tmp_path)
|
||||||
|
agent = FakePlanAgent()
|
||||||
|
runner.bind_agent(agent)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Watch open negotiations",
|
||||||
|
kind="custom",
|
||||||
|
objective="Check UEX replies and recommend next action",
|
||||||
|
constraints={"instructions": "Pay attention to stale buyer replies."},
|
||||||
|
items=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await runner.run_plan(plan["id"])
|
||||||
|
snapshot = store.get_plan(plan["id"])
|
||||||
|
|
||||||
|
assert result["status"] == "ok"
|
||||||
|
assert "Custom plan checked notifications" in result["summary"]
|
||||||
|
assert plan["id"] in agent.prompts[0]
|
||||||
|
assert any(event["kind"] == "run" for event in snapshot["events"])
|
||||||
|
assert "Custom plan checked notifications" in memory.list_outbox()[0]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_scheduler_plan_run_survives_runner_error(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
store = ContinualPlanStore(memory)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Broken plan",
|
||||||
|
objective="Test failure handling",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel"}],
|
||||||
|
)
|
||||||
|
|
||||||
|
class FailingRunner:
|
||||||
|
def __init__(self, store):
|
||||||
|
self.store = store
|
||||||
|
|
||||||
|
async def run_plan(self, plan_id):
|
||||||
|
self.store.add_event(plan_id, "error", "boom")
|
||||||
|
memory.add_outbox("Broken plan: boom")
|
||||||
|
return {"error": "boom", "plan": self.store.get_plan(plan_id)}
|
||||||
|
|
||||||
|
scheduler = WakeScheduler(memory)
|
||||||
|
scheduler.bind_plan_runner(FailingRunner(store))
|
||||||
|
|
||||||
|
await scheduler._run_plan(plan["id"])
|
||||||
|
|
||||||
|
snapshot = store.get_plan(plan["id"])
|
||||||
|
assert snapshot["status"] == "active"
|
||||||
|
assert snapshot["events"][0]["kind"] == "error"
|
||||||
|
assert "boom" in memory.list_outbox()[0]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_scheduler_schedules_overdue_plan_catchup_on_start(tmp_path):
|
||||||
|
memory, store, _, runner, scheduler = plan_stack(tmp_path)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Overdue plan",
|
||||||
|
objective="Check after restart",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel"}],
|
||||||
|
)
|
||||||
|
store.update_schedule(plan["id"], (utc_now() - timedelta(minutes=5)).isoformat())
|
||||||
|
|
||||||
|
scheduler.start()
|
||||||
|
try:
|
||||||
|
catchup = scheduler.scheduler.get_job(scheduler._plan_catchup_job_id(plan["id"]))
|
||||||
|
snapshot = store.get_plan(plan["id"])
|
||||||
|
finally:
|
||||||
|
scheduler.shutdown()
|
||||||
|
|
||||||
|
assert catchup is not None
|
||||||
|
assert any(event["kind"] == "catchup_scheduled" for event in snapshot["events"])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_tools_delete_continual_plan_removes_it(tmp_path):
|
||||||
|
_, store, tools, _, _ = plan_stack(tmp_path)
|
||||||
|
plan = store.create_plan(
|
||||||
|
"Delete through tools",
|
||||||
|
objective="Remove via registry",
|
||||||
|
items=[{"item_name": "Wikelo Idris panel"}],
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await tools.delete_continual_plan(plan["id"])
|
||||||
|
|
||||||
|
assert result["deleted"] is True
|
||||||
|
assert result["plan_id"] == plan["id"]
|
||||||
|
assert store.get_plan(plan["id"]) is None
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from traderai.agent import OllamaAgent
|
||||||
from traderai.memory import MemoryStore
|
from traderai.memory import MemoryStore
|
||||||
from traderai.scheduler import WakeScheduler
|
from traderai.scheduler import WakeScheduler
|
||||||
|
|
||||||
@@ -31,6 +32,102 @@ class FakeUEXNotifications:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FailingUEXNotifications:
|
||||||
|
async def get_user_notifications(self):
|
||||||
|
raise RuntimeError("bad token")
|
||||||
|
|
||||||
|
|
||||||
|
class FakeWakeAgent:
|
||||||
|
async def generate_wake_response(self, wake_message):
|
||||||
|
return f"Wake output: {wake_message}"
|
||||||
|
|
||||||
|
|
||||||
|
class ListingWakeTools:
|
||||||
|
schemas = [
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "search_marketplace_listings",
|
||||||
|
"description": "Search active UEX marketplace listings.",
|
||||||
|
"parameters": {"type": "object", "properties": {}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.calls = []
|
||||||
|
self.pending_actions = {}
|
||||||
|
|
||||||
|
async def execute(self, name, arguments):
|
||||||
|
self.calls.append((name, arguments))
|
||||||
|
return {
|
||||||
|
"count": 2,
|
||||||
|
"listings": [
|
||||||
|
{
|
||||||
|
"id": 100,
|
||||||
|
"title": "Wikelo Favor",
|
||||||
|
"operation": "sell",
|
||||||
|
"price": 500_000_000,
|
||||||
|
"currency": "UEC",
|
||||||
|
"in_stock": 9,
|
||||||
|
"advertiser": "pilot_a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 101,
|
||||||
|
"title": "Wikelo Favor stack",
|
||||||
|
"operation": "sell",
|
||||||
|
"price": 1_000_000_000,
|
||||||
|
"currency": "UEC",
|
||||||
|
"in_stock": 5,
|
||||||
|
"advertiser": "pilot_b",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ListingWakeAgent(OllamaAgent):
|
||||||
|
def __init__(self, memory):
|
||||||
|
self.listing_tools = ListingWakeTools()
|
||||||
|
super().__init__("http://127.0.0.1:1", "missing-model", self.listing_tools, memory=memory)
|
||||||
|
self.responses = [
|
||||||
|
{
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "",
|
||||||
|
"tool_calls": [
|
||||||
|
{
|
||||||
|
"function": {
|
||||||
|
"name": "search_marketplace_listings",
|
||||||
|
"arguments": {
|
||||||
|
"query": "Wikelo Favor",
|
||||||
|
"operation": "sell",
|
||||||
|
"limit": 5,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"message": {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": (
|
||||||
|
"Listing check complete: found 2 active Wikelo Favor sell listings. "
|
||||||
|
"Cheapest listing is 500,000,000 UEC with 9 in stock; the next listing is "
|
||||||
|
"1,000,000,000 UEC. Suggested next action: price near 500,000,000 UEC "
|
||||||
|
"if you want to move yours quickly."
|
||||||
|
),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
async def ensure_available(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _ollama_chat(self, *args, **kwargs):
|
||||||
|
return self.responses.pop(0)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_poll_uex_notifications_adds_unread_once(tmp_path):
|
async def test_poll_uex_notifications_adds_unread_once(tmp_path):
|
||||||
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
@@ -45,3 +142,52 @@ async def test_poll_uex_notifications_adds_unread_once(tmp_path):
|
|||||||
assert second == []
|
assert second == []
|
||||||
assert len(outbox) == 1
|
assert len(outbox) == 1
|
||||||
assert "A buyer replied to your listing." in outbox[0]["content"]
|
assert "A buyer replied to your listing." in outbox[0]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_poll_uex_notifications_reports_failures_to_outbox(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
scheduler = WakeScheduler(memory)
|
||||||
|
scheduler.bind_uex_notifications(FailingUEXNotifications())
|
||||||
|
|
||||||
|
result = await scheduler.poll_uex_notifications()
|
||||||
|
|
||||||
|
assert result == []
|
||||||
|
assert "bad token" in memory.inspect()["outbox"][0]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_wake_job_writes_agent_output_to_outbox_and_disables_one_shot(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
scheduler = WakeScheduler(memory)
|
||||||
|
scheduler.bind_agent(FakeWakeAgent())
|
||||||
|
memory.add_job("wake-test", "check notifications", "date", "2099-01-01T00:00:00+00:00")
|
||||||
|
|
||||||
|
await scheduler._run_job("wake-test", "check notifications")
|
||||||
|
snapshot = memory.inspect()
|
||||||
|
|
||||||
|
assert "Wake output:" in snapshot["outbox"][0]["content"]
|
||||||
|
assert snapshot["scheduled_jobs"][0]["enabled"] == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_wake_job_checks_listings_and_writes_analysis_to_outbox(tmp_path):
|
||||||
|
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||||
|
scheduler = WakeScheduler(memory)
|
||||||
|
agent = ListingWakeAgent(memory)
|
||||||
|
scheduler.bind_agent(agent)
|
||||||
|
memory.add_job("wake-listings", "check Wikelo Favor listings and analyze the market", "date", "2099-01-01T00:00:00+00:00")
|
||||||
|
|
||||||
|
await scheduler._run_job("wake-listings", "check Wikelo Favor listings and analyze the market")
|
||||||
|
snapshot = memory.inspect()
|
||||||
|
content = snapshot["outbox"][0]["content"]
|
||||||
|
|
||||||
|
assert agent.listing_tools.calls == [
|
||||||
|
(
|
||||||
|
"search_marketplace_listings",
|
||||||
|
{"query": "Wikelo Favor", "operation": "sell", "limit": 5},
|
||||||
|
)
|
||||||
|
]
|
||||||
|
assert "Listing check complete" in content
|
||||||
|
assert "500,000,000 UEC" in content
|
||||||
|
assert "Suggested next action" in content
|
||||||
|
|||||||
@@ -0,0 +1,257 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
import traderai.server as server
|
||||||
|
|
||||||
|
|
||||||
|
def test_config_update_rebuilds_runtime_without_restart(monkeypatch, tmp_path):
|
||||||
|
state = {"settings": make_settings(tmp_path, model_provider="ollama", ollama_model="qwen3.5:9b")}
|
||||||
|
|
||||||
|
class FakeScheduler:
|
||||||
|
def __init__(self, memory):
|
||||||
|
self.memory = memory
|
||||||
|
|
||||||
|
def bind_agent(self, agent):
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_plan_runner(self, plan_runner):
|
||||||
|
self.plan_runner = plan_runner
|
||||||
|
|
||||||
|
def bind_uex_notifications(self, uex, poll_seconds=60):
|
||||||
|
self.uex = uex
|
||||||
|
self.poll_seconds = poll_seconds
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def list_jobs(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
class FakeUEXClient:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def get_user(self, username=None, authenticated=False):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
class FakeToolRegistry:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.pending_actions = {}
|
||||||
|
self.plan_runner = None
|
||||||
|
|
||||||
|
async def approve(self, action_id):
|
||||||
|
return {"approved": action_id}
|
||||||
|
|
||||||
|
async def decline(self, action_id):
|
||||||
|
return {"declined": action_id}
|
||||||
|
|
||||||
|
class FakePlanRunner:
|
||||||
|
def __init__(self, store, tools, memory, agent=None):
|
||||||
|
self.store = store
|
||||||
|
self.tools = tools
|
||||||
|
self.memory = memory
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_agent(self, agent):
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
class FakeClient:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def fake_health(self):
|
||||||
|
return {
|
||||||
|
"online": True,
|
||||||
|
"provider": self.provider,
|
||||||
|
"model": self.model,
|
||||||
|
"model_available": True,
|
||||||
|
"message": f"{self.provider} ready",
|
||||||
|
}
|
||||||
|
|
||||||
|
async def fake_chat(self, content, thread_id=None, images=None):
|
||||||
|
return {"message": f"{self.provider}:{self.model}", "pending_actions": [], "thread_id": thread_id}
|
||||||
|
|
||||||
|
def fake_get_settings():
|
||||||
|
return state["settings"]
|
||||||
|
|
||||||
|
def fake_save_settings(values):
|
||||||
|
state["settings"] = make_settings(
|
||||||
|
tmp_path,
|
||||||
|
model_provider=values.get("model_provider", state["settings"].model_provider),
|
||||||
|
ollama_model=values.get("ollama_model", state["settings"].ollama_model),
|
||||||
|
codex_model=values.get("codex_model", state["settings"].codex_model),
|
||||||
|
deepseek_model=values.get("deepseek_model", state["settings"].deepseek_model),
|
||||||
|
)
|
||||||
|
return {"values": values, "fields": {}, "secrets_configured": {}, "app_data_dir": str(tmp_path)}
|
||||||
|
|
||||||
|
monkeypatch.setattr(server, "WakeScheduler", FakeScheduler)
|
||||||
|
monkeypatch.setattr(server, "UEXClient", FakeUEXClient)
|
||||||
|
monkeypatch.setattr(server, "ToolRegistry", FakeToolRegistry)
|
||||||
|
monkeypatch.setattr(server, "ContinualPlanRunner", FakePlanRunner)
|
||||||
|
monkeypatch.setattr(server, "SCMDBClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "CornerstoneClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "StarCitizenWikiClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "get_settings", fake_get_settings)
|
||||||
|
monkeypatch.setattr(server, "save_settings", fake_save_settings)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
server,
|
||||||
|
"settings_payload",
|
||||||
|
lambda settings=None: {"app_data_dir": str(tmp_path), "values": {}, "fields": {}, "secrets_configured": {}},
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(server.OllamaAgent, "health", fake_health)
|
||||||
|
monkeypatch.setattr(server.OllamaAgent, "chat", fake_chat)
|
||||||
|
|
||||||
|
app = server.create_app()
|
||||||
|
with TestClient(app) as client:
|
||||||
|
before = client.get("/api/health").json()
|
||||||
|
assert before["model_provider"] == "ollama"
|
||||||
|
assert before["inference"]["provider"] == "ollama"
|
||||||
|
|
||||||
|
updated = client.post(
|
||||||
|
"/api/config",
|
||||||
|
json={"values": {"model_provider": "deepseek", "deepseek_model": "deepseek-v4-flash"}},
|
||||||
|
).json()
|
||||||
|
assert updated["restart_required"] is False
|
||||||
|
|
||||||
|
after = client.get("/api/health").json()
|
||||||
|
assert after["model_provider"] == "deepseek"
|
||||||
|
assert after["inference"]["provider"] == "deepseek"
|
||||||
|
|
||||||
|
chat = client.post("/api/chat", json={"message": "hi", "thread_id": "thread-1", "images": []}).json()
|
||||||
|
assert chat["message"] == "deepseek:deepseek-v4-flash"
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_draft_endpoint_returns_agent_draft(monkeypatch, tmp_path):
|
||||||
|
state = {"settings": make_settings(tmp_path)}
|
||||||
|
|
||||||
|
class FakeScheduler:
|
||||||
|
def __init__(self, memory):
|
||||||
|
self.memory = memory
|
||||||
|
|
||||||
|
def bind_agent(self, agent):
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_plan_runner(self, plan_runner):
|
||||||
|
self.plan_runner = plan_runner
|
||||||
|
|
||||||
|
def bind_uex_notifications(self, uex, poll_seconds=60):
|
||||||
|
self.uex = uex
|
||||||
|
self.poll_seconds = poll_seconds
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def list_jobs(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
class FakeUEXClient:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def get_user(self, username=None, authenticated=False):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
class FakeToolRegistry:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.pending_actions = {}
|
||||||
|
self.plan_runner = None
|
||||||
|
|
||||||
|
async def approve(self, action_id):
|
||||||
|
return {"approved": action_id}
|
||||||
|
|
||||||
|
async def decline(self, action_id):
|
||||||
|
return {"declined": action_id}
|
||||||
|
|
||||||
|
class FakePlanRunner:
|
||||||
|
def __init__(self, store, tools, memory, agent=None):
|
||||||
|
self.store = store
|
||||||
|
self.tools = tools
|
||||||
|
self.memory = memory
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_agent(self, agent):
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
class FakeClient:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def fake_get_settings():
|
||||||
|
return state["settings"]
|
||||||
|
|
||||||
|
monkeypatch.setattr(server, "WakeScheduler", FakeScheduler)
|
||||||
|
monkeypatch.setattr(server, "UEXClient", FakeUEXClient)
|
||||||
|
monkeypatch.setattr(server, "ToolRegistry", FakeToolRegistry)
|
||||||
|
monkeypatch.setattr(server, "ContinualPlanRunner", FakePlanRunner)
|
||||||
|
monkeypatch.setattr(server, "SCMDBClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "CornerstoneClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "StarCitizenWikiClient", FakeClient)
|
||||||
|
monkeypatch.setattr(server, "get_settings", fake_get_settings)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
server,
|
||||||
|
"settings_payload",
|
||||||
|
lambda settings=None: {"app_data_dir": str(tmp_path), "values": {}, "fields": {}, "secrets_configured": {}},
|
||||||
|
)
|
||||||
|
|
||||||
|
async def fake_generate_plan_draft(self, title="", objective="", kind="buying", constraints=None, items=None):
|
||||||
|
return {
|
||||||
|
"title": title or "Draft title",
|
||||||
|
"objective": objective or "Draft objective",
|
||||||
|
"kind": kind,
|
||||||
|
"cadence": "0 */3 * * *",
|
||||||
|
"constraints": {"message_tone": "friendly and direct", "instructions": "Start with the best listings."},
|
||||||
|
"items": [{"item_name": "RCMBNT-RGL-1", "desired_quantity": 1, "max_unit_price": None}],
|
||||||
|
}
|
||||||
|
|
||||||
|
monkeypatch.setattr(server.OllamaAgent, "generate_plan_draft", fake_generate_plan_draft)
|
||||||
|
|
||||||
|
app = server.create_app()
|
||||||
|
with TestClient(app) as client:
|
||||||
|
response = client.post(
|
||||||
|
"/api/plans/draft",
|
||||||
|
json={"title": "Polaris parts", "objective": "Find the required parts", "kind": "buying", "constraints": {}, "items": []},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
draft = response.json()["draft"]
|
||||||
|
assert draft["cadence"] == "0 */3 * * *"
|
||||||
|
assert draft["constraints"]["instructions"] == "Start with the best listings."
|
||||||
|
assert draft["items"][0]["item_name"] == "RCMBNT-RGL-1"
|
||||||
|
|
||||||
|
|
||||||
|
def make_settings(tmp_path, model_provider="ollama", ollama_model="qwen3.5:9b", codex_model="gpt-5.4", deepseek_model="deepseek-v4-flash"):
|
||||||
|
return SimpleNamespace(
|
||||||
|
traderai_memory_path=str(tmp_path / "memory.sqlite3"),
|
||||||
|
model_provider=model_provider,
|
||||||
|
ollama_base_url="http://localhost:11434",
|
||||||
|
ollama_model=ollama_model,
|
||||||
|
ollama_num_ctx=64512,
|
||||||
|
openai_base_url="https://api.openai.com/v1",
|
||||||
|
openai_api_key=None,
|
||||||
|
openai_model="gpt-5.4-mini",
|
||||||
|
deepseek_base_url="https://api.deepseek.com",
|
||||||
|
deepseek_api_key=None,
|
||||||
|
deepseek_model=deepseek_model,
|
||||||
|
model_reasoning_effort="medium",
|
||||||
|
codex_command="codex",
|
||||||
|
codex_model=codex_model,
|
||||||
|
uex_base_url="https://api.uexcorp.space/2.0",
|
||||||
|
uex_secret_key=None,
|
||||||
|
uex_bearer_token=None,
|
||||||
|
traderai_user_name=None,
|
||||||
|
uex_notification_poll_seconds=60,
|
||||||
|
require_write_approval=True,
|
||||||
|
scmdb_base_url="https://scmdb.net",
|
||||||
|
cornerstone_base_url="https://finder.cstone.space",
|
||||||
|
scwiki_base_url="https://starcitizen.tools",
|
||||||
|
scwiki_api_base_url="https://api.star-citizen.wiki",
|
||||||
|
)
|
||||||
@@ -2,12 +2,18 @@ import pytest
|
|||||||
import respx
|
import respx
|
||||||
from httpx import Response
|
from httpx import Response
|
||||||
|
|
||||||
|
from traderai.cornerstone_client import CornerstoneClient, parse_cornerstone_item_page
|
||||||
from traderai.tools import ToolRegistry
|
from traderai.tools import ToolRegistry
|
||||||
from traderai.uex_client import UEXClient
|
from traderai.uex_client import UEXClient
|
||||||
|
|
||||||
|
|
||||||
class FakeUEX:
|
class FakeUEX:
|
||||||
|
def __init__(self):
|
||||||
|
self.posts = []
|
||||||
|
self.get_calls = []
|
||||||
|
|
||||||
async def get(self, path, params=None, authenticated=False):
|
async def get(self, path, params=None, authenticated=False):
|
||||||
|
self.get_calls.append({"path": path, "params": params, "authenticated": authenticated})
|
||||||
if path == "commodities_prices_history":
|
if path == "commodities_prices_history":
|
||||||
return {
|
return {
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
@@ -76,6 +82,34 @@ class FakeUEX:
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
if path == "marketplace_trends":
|
||||||
|
return {
|
||||||
|
"status": "ok",
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"id_item": 2791,
|
||||||
|
"item_name": "\"Quantanium\" Water Bottle",
|
||||||
|
"item_slug": "quantanium-water-bottle",
|
||||||
|
"currency": "UEC",
|
||||||
|
"price_avg_sell": "937500",
|
||||||
|
"price_avg_month_sell": "1072222",
|
||||||
|
"price_min_sell": "750000",
|
||||||
|
"price_max_sell": "1200000",
|
||||||
|
"listings_count_sell": 4,
|
||||||
|
"price_avg_buy": "500000",
|
||||||
|
"price_avg_month_buy": "525000",
|
||||||
|
"price_min_buy": "450000",
|
||||||
|
"price_max_buy": "550000",
|
||||||
|
"listings_count_buy": 2,
|
||||||
|
"total_listings_count": 6,
|
||||||
|
"negotiations_count": 18,
|
||||||
|
"negotiations_open": 7,
|
||||||
|
"negotiations_success": 9,
|
||||||
|
"link_prices": "https://uexcorp.space/marketplace/home/?id_item=2791&mode=list",
|
||||||
|
"link_prices_history": "https://uexcorp.space/marketplace/averages/?id_item=2791&quality_tier=q0&unit=unit",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
assert path == "marketplace_listings"
|
assert path == "marketplace_listings"
|
||||||
return {
|
return {
|
||||||
"data": [
|
"data": [
|
||||||
@@ -113,6 +147,257 @@ class FakeUEX:
|
|||||||
async def delete(self, path, params=None, authenticated=True):
|
async def delete(self, path, params=None, authenticated=True):
|
||||||
return {"status": "ok", "deleted": {"path": path, "params": params, "authenticated": authenticated}}
|
return {"status": "ok", "deleted": {"path": path, "params": params, "authenticated": authenticated}}
|
||||||
|
|
||||||
|
async def post(self, path, payload, authenticated=True):
|
||||||
|
self.posts.append({"path": path, "payload": payload, "authenticated": authenticated})
|
||||||
|
return {"status": "ok", "posted": self.posts[-1]}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeSCMDB:
|
||||||
|
base_url = "https://scmdb.test"
|
||||||
|
|
||||||
|
async def list_versions(self):
|
||||||
|
return [
|
||||||
|
{"version": "4.8.0-ptu.1", "file": "merged-4.8.0-ptu.1.json"},
|
||||||
|
{"version": "4.7.2-live.1", "file": "merged-4.7.2-live.1.json"},
|
||||||
|
]
|
||||||
|
|
||||||
|
async def get_data(self, version=None, channel="live"):
|
||||||
|
return {
|
||||||
|
"version": version or "4.7.2-live.1",
|
||||||
|
"factions": {
|
||||||
|
"fac-haul": {"name": "Covalex"},
|
||||||
|
"fac-bounty": {"name": "Bounty Hunters Guild"},
|
||||||
|
},
|
||||||
|
"scopes": {
|
||||||
|
"scope-rep": {"scopeName": "FactionReputation"},
|
||||||
|
},
|
||||||
|
"factionRewardsPools": [
|
||||||
|
[{"factionGuid": "fac-haul", "scopeGuid": "scope-rep", "amount": 125}],
|
||||||
|
[{"factionGuid": "fac-bounty", "scopeGuid": "scope-rep", "amount": 250}],
|
||||||
|
],
|
||||||
|
"partialRewardPayoutPools": [
|
||||||
|
[],
|
||||||
|
[{"minPercentage": 50, "maxPercentage": 99, "currencyRewardMultiplier": 0.75, "reputationMultipliers": None}],
|
||||||
|
],
|
||||||
|
"resourcePools": {
|
||||||
|
"res-tungsten": {"name": "Tungsten"},
|
||||||
|
},
|
||||||
|
"blueprintPools": {
|
||||||
|
"bp-pool": {
|
||||||
|
"name": "Ship Salvage Rewards",
|
||||||
|
"blueprints": [{"name": "Abrade Scraper Module"}],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"locationPools": {
|
||||||
|
"loc-a18": {"name": "Area18"},
|
||||||
|
"loc-baijini": {"name": "Baijini Point"},
|
||||||
|
},
|
||||||
|
"contracts": [
|
||||||
|
{
|
||||||
|
"id": "mission-haul",
|
||||||
|
"debugName": "Haul_Tungsten_Test",
|
||||||
|
"title": "Move Tungsten",
|
||||||
|
"description": "Move Tungsten to Baijini Point.",
|
||||||
|
"missionType": "Hauling",
|
||||||
|
"category": "career",
|
||||||
|
"factionGuid": "fac-haul",
|
||||||
|
"rewardUEC": 50250,
|
||||||
|
"factionRewardsIndex": 0,
|
||||||
|
"partialRewardPayoutIndex": 1,
|
||||||
|
"haulingOrders": [{"resource": "res-tungsten", "minSCU": 6, "maxSCU": 6, "maxContainerSize": 1}],
|
||||||
|
"locations": ["loc-a18"],
|
||||||
|
"destinations": ["loc-baijini"],
|
||||||
|
"systems": ["Stanton"],
|
||||||
|
"illegal": False,
|
||||||
|
"canBeShared": False,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "mission-bounty",
|
||||||
|
"debugName": "Bounty_Blueprint_Test",
|
||||||
|
"title": "Ambush Op",
|
||||||
|
"description": "Clean out targets.",
|
||||||
|
"missionType": "Bounty Hunter",
|
||||||
|
"factionGuid": "fac-bounty",
|
||||||
|
"rewardUEC": 120000,
|
||||||
|
"factionRewardsIndex": 1,
|
||||||
|
"partialRewardPayoutIndex": 0,
|
||||||
|
"itemRewards": [{"name": "Council Scrip", "amount": 5}],
|
||||||
|
"blueprintRewards": [{"blueprintPool": "bp-pool", "chance": 1, "trigger": "complete"}],
|
||||||
|
"systems": ["Pyro"],
|
||||||
|
"illegal": True,
|
||||||
|
"canBeShared": True,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"legacyContracts": [
|
||||||
|
{
|
||||||
|
"id": "legacy-delivery",
|
||||||
|
"debugName": "Legacy_Delivery_Test",
|
||||||
|
"title": "Old Box Run",
|
||||||
|
"missionType": "Delivery",
|
||||||
|
"factionGuid": "fac-haul",
|
||||||
|
"rewardUEC": 1000,
|
||||||
|
"factionRewardsIndex": 0,
|
||||||
|
"partialRewardPayoutIndex": 0,
|
||||||
|
"systems": ["Stanton"],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeCornerstone:
|
||||||
|
base_url = "https://finder.cstone.test"
|
||||||
|
|
||||||
|
async def list_items(self):
|
||||||
|
return [
|
||||||
|
{"id": "item-abrade", "name": "Abrade Scraper Module", "sold": True},
|
||||||
|
{"id": "item-cinch", "name": "Cinch Scraper Module", "sold": True},
|
||||||
|
{"id": "item-poster", "name": "Zeus 2955 Ship Showdown Poster", "sold": False},
|
||||||
|
]
|
||||||
|
|
||||||
|
async def get_item_page(self, item_id):
|
||||||
|
assert item_id == "item-abrade"
|
||||||
|
return {
|
||||||
|
"url": f"{self.base_url}/ShipSalvageMods1/{item_id}",
|
||||||
|
"html": """
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Star Citizen - Salvage modifier - Abrade Scraper Module</title>
|
||||||
|
<meta property="og:image" content="/images/abrade.png">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<table>
|
||||||
|
<tr><td>NAME</td><td>Abrade Scraper Module</td></tr>
|
||||||
|
<tr><td>MANUFACTURER</td><td>Greycat Industrial</td></tr>
|
||||||
|
</table>
|
||||||
|
<table>
|
||||||
|
<tr><th>LOCATION</th><th>BASE PRICE</th><th>VERIFIED</th></tr>
|
||||||
|
<tr><td>Stanton - ArcCorp - Area18 - Dumper's Depot</td><td>21 250</td><td>2956-01-29</td></tr>
|
||||||
|
<tr><td>Stanton - microTech - Port Tressler - Platinum Bay</td><td>21 250</td><td>2956-01-04</td></tr>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""",
|
||||||
|
}
|
||||||
|
|
||||||
|
async def get_image_data(self, url, max_bytes=10_000_000):
|
||||||
|
assert url == f"{self.base_url}/images/abrade.png"
|
||||||
|
return {
|
||||||
|
"url": url,
|
||||||
|
"content_type": "image/png",
|
||||||
|
"size_bytes": 12,
|
||||||
|
"image_data": "ZmFrZS1pbWFnZQ==",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeSCWiki:
|
||||||
|
base_url = "https://starcitizen.tools"
|
||||||
|
api_base_url = "https://api.star-citizen.wiki"
|
||||||
|
|
||||||
|
async def search_pages(self, query, limit=5):
|
||||||
|
assert query == "Carrack"
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"pageid": 415,
|
||||||
|
"title": "Carrack",
|
||||||
|
"description": "Deep-space multi-crew explorer manufactured by Anvil Aerospace",
|
||||||
|
"extract": "The Anvil Carrack is a multi-crew explorer.",
|
||||||
|
"thumbnail": "https://media.starcitizen.tools/carrack.webp",
|
||||||
|
"url": "https://starcitizen.tools/Carrack",
|
||||||
|
}
|
||||||
|
][:limit]
|
||||||
|
|
||||||
|
async def get_page_summary(self, title=None, pageid=None, chars=700):
|
||||||
|
assert title == "Carrack" or pageid == 415
|
||||||
|
return {
|
||||||
|
"pageid": 415,
|
||||||
|
"title": "Carrack",
|
||||||
|
"description": "Deep-space multi-crew explorer manufactured by Anvil Aerospace",
|
||||||
|
"extract": "The Anvil Carrack is a multi-crew explorer.",
|
||||||
|
"thumbnail": "https://media.starcitizen.tools/carrack.webp",
|
||||||
|
"url": "https://starcitizen.tools/Carrack",
|
||||||
|
}
|
||||||
|
|
||||||
|
async def search_verse(self, query):
|
||||||
|
assert query == "Carrack"
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"type": "vehicles",
|
||||||
|
"label": "Vehicles",
|
||||||
|
"results": [
|
||||||
|
{
|
||||||
|
"name": "Anvil Carrack",
|
||||||
|
"class_name": "ANVL_Carrack",
|
||||||
|
"extra_label": "Exploration",
|
||||||
|
"web_url": "https://api.star-citizen.wiki/vehicles/anvl-carrack",
|
||||||
|
"api_url": "https://api.star-citizen.wiki/api/vehicles/anvl-carrack",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
async def get_vehicle(self, slug):
|
||||||
|
assert slug == "anvl-carrack"
|
||||||
|
return {
|
||||||
|
"name": "Carrack",
|
||||||
|
"game_name": "Anvil Carrack",
|
||||||
|
"slug": "anvl-carrack",
|
||||||
|
"manufacturer": {"name": "Anvil Aerospace"},
|
||||||
|
"career": "Exploration",
|
||||||
|
"role": "Expedition",
|
||||||
|
"size_class": 5,
|
||||||
|
"cargo_capacity": 456,
|
||||||
|
"crew": {"min": 6, "max": 6},
|
||||||
|
"msrp": 600,
|
||||||
|
"pledge_url": "https://robertsspaceindustries.com/pledge/ships/carrack/Carrack",
|
||||||
|
"uex_prices": {
|
||||||
|
"purchase": [
|
||||||
|
{
|
||||||
|
"price_buy": 34398000,
|
||||||
|
"terminal_name": "Astro Armada - Area 18",
|
||||||
|
"starmap_location": {"name": "Area18", "parent_name": "ArcCorp", "star_system_name": "Stanton"},
|
||||||
|
"game_version": "4.8.1-LIVE.11952564",
|
||||||
|
"date_updated": "2026-05-20T18:39:37-04:00",
|
||||||
|
"uex_link": "https://uexcorp.space/vehicles/home/list/in_game_sell/?id_terminal=148",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"description": {"en_EN": "The Anvil Carrack features reinforced fuel tanks for long-duration flight."},
|
||||||
|
"web_url": "https://api.star-citizen.wiki/vehicles/anvl-carrack",
|
||||||
|
"updated_at": "2026-06-08T00:34:00Z",
|
||||||
|
"version": "4.8.1-LIVE.11952564",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeWikelo:
|
||||||
|
base_url = "https://wikelo-projects.test"
|
||||||
|
|
||||||
|
async def list_ship_projects(self):
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"id": "ship-1",
|
||||||
|
"ship_name": "Polaris Wikelo Special",
|
||||||
|
"description": "Now make Polaris. Short Time Deal",
|
||||||
|
"status": "planning",
|
||||||
|
"privacy": "public",
|
||||||
|
"owner_name": "Chimpanz33",
|
||||||
|
"required_materials": [
|
||||||
|
{"material_name": "Wikelo Favor", "quantity_needed": 50.0, "quantity_collected": 0.0},
|
||||||
|
{"material_name": "Polaris Bit", "quantity_needed": 15.0, "quantity_collected": 2.0},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ship-2",
|
||||||
|
"ship_name": "Guardian",
|
||||||
|
"description": "Guardian Fight Mod",
|
||||||
|
"status": "planning",
|
||||||
|
"privacy": "public",
|
||||||
|
"owner_name": "Chimpanz33",
|
||||||
|
"required_materials": [
|
||||||
|
{"material_name": "Wikelo Favor", "quantity_needed": 20.0, "quantity_collected": 0.0},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_search_marketplace_listings_filters_locally():
|
async def test_search_marketplace_listings_filters_locally():
|
||||||
@@ -145,6 +430,19 @@ async def test_decline_pending_action_removes_without_sending():
|
|||||||
assert action_id not in registry.pending_actions
|
assert action_id not in registry.pending_actions
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_approve_negotiation_message_forces_production_send():
|
||||||
|
fake = FakeUEX()
|
||||||
|
registry = ToolRegistry(fake)
|
||||||
|
result = await registry.draft_negotiation_message(hash="abc", message="Ready to close", is_production=0)
|
||||||
|
action_id = result["pending_action"]["id"]
|
||||||
|
|
||||||
|
approved = await registry.approve(action_id)
|
||||||
|
|
||||||
|
assert approved["posted"]["path"] == "marketplace_negotiations_messages"
|
||||||
|
assert approved["posted"]["payload"]["is_production"] == 1
|
||||||
|
|
||||||
|
|
||||||
def test_uex_client_uses_bearer_and_secret_headers():
|
def test_uex_client_uses_bearer_and_secret_headers():
|
||||||
client = UEXClient("https://api.uexcorp.space/2.0", secret_key="secret", bearer_token="bearer")
|
client = UEXClient("https://api.uexcorp.space/2.0", secret_key="secret", bearer_token="bearer")
|
||||||
|
|
||||||
@@ -175,6 +473,65 @@ async def test_uex_get_projects_and_limits_results():
|
|||||||
assert result["items"] == [{"id": 10, "commodity_name": "Gold", "price_buy": 4120}]
|
assert result["items"] == [{"id": 10, "commodity_name": "Gold", "price_buy": 4120}]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_uex_get_marketplace_listings_accepts_item_and_operation_filters():
|
||||||
|
fake = FakeUEX()
|
||||||
|
registry = ToolRegistry(fake)
|
||||||
|
|
||||||
|
result = await registry.execute(
|
||||||
|
"get_uex_marketplace_listings",
|
||||||
|
{
|
||||||
|
"id_item": 2791,
|
||||||
|
"operation": "sell",
|
||||||
|
"fields": ["id", "slug", "operation"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["params"] == {"id_item": 2791, "operation": "sell"}
|
||||||
|
assert fake.get_calls[-1]["path"] == "marketplace_listings"
|
||||||
|
assert fake.get_calls[-1]["params"] == {"id_item": 2791, "operation": "sell"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_marketplace_trends_returns_compact_wts_wtb_and_negotiation_metrics():
|
||||||
|
fake = FakeUEX()
|
||||||
|
registry = ToolRegistry(fake)
|
||||||
|
|
||||||
|
result = await registry.get_marketplace_trends(item_name="Quantanium", currency="UEC", quality_tier=0)
|
||||||
|
|
||||||
|
assert result["status"] == "ok"
|
||||||
|
assert result["count"] == 1
|
||||||
|
assert result["filters"] == {"item_name": "Quantanium", "currency": "UEC", "quality_tier": 0}
|
||||||
|
assert fake.get_calls[-1]["path"] == "marketplace_trends"
|
||||||
|
assert fake.get_calls[-1]["params"] == {"id_item": None, "item_name": "Quantanium", "item_slug": None, "id_category": None, "currency": "UEC", "quality_tier": 0}
|
||||||
|
assert result["trends"][0] == {
|
||||||
|
"id_item": 2791,
|
||||||
|
"item_name": "\"Quantanium\" Water Bottle",
|
||||||
|
"item_slug": "quantanium-water-bottle",
|
||||||
|
"currency": "UEC",
|
||||||
|
"sell": {
|
||||||
|
"avg_price": "937500",
|
||||||
|
"avg_price_month": "1072222",
|
||||||
|
"min_price": "750000",
|
||||||
|
"max_price": "1200000",
|
||||||
|
"listings_count": 4,
|
||||||
|
},
|
||||||
|
"buy": {
|
||||||
|
"avg_price": "500000",
|
||||||
|
"avg_price_month": "525000",
|
||||||
|
"min_price": "450000",
|
||||||
|
"max_price": "550000",
|
||||||
|
"listings_count": 2,
|
||||||
|
},
|
||||||
|
"total_listings_count": 6,
|
||||||
|
"negotiations_count": 18,
|
||||||
|
"negotiations_open": 7,
|
||||||
|
"negotiations_success": 9,
|
||||||
|
"link_prices": "https://uexcorp.space/marketplace/home/?id_item=2791&mode=list",
|
||||||
|
"link_prices_history": "https://uexcorp.space/marketplace/averages/?id_item=2791&quality_tier=q0&unit=unit",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_uex_api_catalog_exposes_resources_without_live_call():
|
async def test_uex_api_catalog_exposes_resources_without_live_call():
|
||||||
registry = ToolRegistry(FakeUEX())
|
registry = ToolRegistry(FakeUEX())
|
||||||
@@ -210,12 +567,271 @@ def test_schemas_expose_specific_uex_tools_instead_of_generic_api_tool():
|
|||||||
|
|
||||||
assert "get_uex_commodities_prices" in names
|
assert "get_uex_commodities_prices" in names
|
||||||
assert "get_uex_vehicles" in names
|
assert "get_uex_vehicles" in names
|
||||||
|
assert "get_marketplace_trends" in names
|
||||||
assert "draft_uex_marketplace_advertise" in names
|
assert "draft_uex_marketplace_advertise" in names
|
||||||
assert "delete_uex_marketplace_listings" in names
|
assert "delete_uex_marketplace_listings" in names
|
||||||
assert "uex_get" not in names
|
assert "uex_get" not in names
|
||||||
assert "uex_draft_post" not in names
|
assert "uex_draft_post" not in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_schemas_expose_scmdb_mission_tools():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scmdb=FakeSCMDB())
|
||||||
|
|
||||||
|
names = {schema["function"]["name"] for schema in registry.schemas}
|
||||||
|
|
||||||
|
assert "list_scmdb_versions" in names
|
||||||
|
assert "search_scmdb_missions" in names
|
||||||
|
assert "get_scmdb_mission_rewards" in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_schemas_expose_cornerstone_item_tools():
|
||||||
|
registry = ToolRegistry(FakeUEX(), cornerstone=FakeCornerstone())
|
||||||
|
|
||||||
|
names = {schema["function"]["name"] for schema in registry.schemas}
|
||||||
|
|
||||||
|
assert "search_cornerstone_items" in names
|
||||||
|
assert "get_cornerstone_item_locations" in names
|
||||||
|
assert "get_cornerstone_item_media" in names
|
||||||
|
assert "draft_marketplace_listing_with_cornerstone_image" in names
|
||||||
|
|
||||||
|
|
||||||
|
def test_schemas_expose_scwiki_tools():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scwiki=FakeSCWiki())
|
||||||
|
|
||||||
|
names = {schema["function"]["name"] for schema in registry.schemas}
|
||||||
|
|
||||||
|
assert "search_scwiki_pages" in names
|
||||||
|
assert "get_scwiki_page" in names
|
||||||
|
assert "search_scwiki_vehicles" in names
|
||||||
|
assert "get_scwiki_vehicle" in names
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_scmdb_missions_returns_reward_summary():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scmdb=FakeSCMDB())
|
||||||
|
|
||||||
|
result = await registry.search_scmdb_missions(query="tungsten", mission_type="hauling")
|
||||||
|
|
||||||
|
assert result["version"] == "4.7.2-live.1"
|
||||||
|
assert result["matched"] == 1
|
||||||
|
mission = result["missions"][0]
|
||||||
|
assert mission["title"] == "Move Tungsten"
|
||||||
|
assert mission["rewards"]["uec"] == 50250
|
||||||
|
assert mission["rewards"]["reputation"] == [{"faction": "Covalex", "scope": "FactionReputation", "amount": 125}]
|
||||||
|
assert mission["rewards"]["hauling"] == [
|
||||||
|
{"resource": "Tungsten", "min_scu": 6, "max_scu": 6, "max_container_size_scu": 1}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_scmdb_mission_rewards_enriches_items_blueprints_and_locations():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scmdb=FakeSCMDB())
|
||||||
|
|
||||||
|
result = await registry.get_scmdb_mission_rewards(debug_name="Bounty_Blueprint_Test")
|
||||||
|
|
||||||
|
mission = result["mission"]
|
||||||
|
assert mission["title"] == "Ambush Op"
|
||||||
|
assert mission["faction"] == "Bounty Hunters Guild"
|
||||||
|
assert mission["rewards"]["items"] == [{"name": "Council Scrip", "amount": 5}]
|
||||||
|
assert mission["rewards"]["blueprints"][0]["blueprints"] == ["Abrade Scraper Module"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_cornerstone_items_filters_sold_items():
|
||||||
|
registry = ToolRegistry(FakeUEX(), cornerstone=FakeCornerstone())
|
||||||
|
|
||||||
|
result = await registry.search_cornerstone_items(query="scraper", sold_only=True)
|
||||||
|
|
||||||
|
assert result["matched"] == 2
|
||||||
|
assert {item["name"] for item in result["items"]} == {"Abrade Scraper Module", "Cinch Scraper Module"}
|
||||||
|
assert result["items"][0]["url"].startswith("https://finder.cstone.test/Search/item-")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_cornerstone_item_locations_parses_store_prices():
|
||||||
|
registry = ToolRegistry(FakeUEX(), cornerstone=FakeCornerstone())
|
||||||
|
|
||||||
|
result = await registry.get_cornerstone_item_locations(query="abrade", location="Area18")
|
||||||
|
|
||||||
|
assert result["item"]["name"] == "Abrade Scraper Module"
|
||||||
|
assert result["item"]["general"]["manufacturer"] == "Greycat Industrial"
|
||||||
|
assert result["matched_locations"] == 1
|
||||||
|
assert result["locations"] == [
|
||||||
|
{
|
||||||
|
"location": "Stanton - ArcCorp - Area18 - Dumper's Depot",
|
||||||
|
"base_price": 21250,
|
||||||
|
"base_price_display": "21 250",
|
||||||
|
"verified": "2956-01-29",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_cornerstone_item_media_returns_absolute_image_urls():
|
||||||
|
registry = ToolRegistry(FakeUEX(), cornerstone=FakeCornerstone())
|
||||||
|
|
||||||
|
result = await registry.get_cornerstone_item_media(query="abrade")
|
||||||
|
|
||||||
|
assert result["media"] == [
|
||||||
|
{
|
||||||
|
"url": "https://finder.cstone.test/images/abrade.png",
|
||||||
|
"source": "og:image",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_scwiki_pages_returns_general_knowledge_matches():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scwiki=FakeSCWiki())
|
||||||
|
|
||||||
|
result = await registry.search_scwiki_pages(query="Carrack")
|
||||||
|
|
||||||
|
assert result["source"] == "https://starcitizen.tools"
|
||||||
|
assert result["matched"] == 1
|
||||||
|
assert result["pages"][0]["title"] == "Carrack"
|
||||||
|
assert result["pages"][0]["url"] == "https://starcitizen.tools/Carrack"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_scwiki_vehicle_returns_ship_prices_and_store_context():
|
||||||
|
registry = ToolRegistry(FakeUEX(), scwiki=FakeSCWiki())
|
||||||
|
|
||||||
|
result = await registry.get_scwiki_vehicle(query="Carrack")
|
||||||
|
|
||||||
|
assert result["source"] == "https://api.star-citizen.wiki"
|
||||||
|
vehicle = result["vehicle"]
|
||||||
|
assert vehicle["name"] == "Carrack"
|
||||||
|
assert vehicle["manufacturer"] == "Anvil Aerospace"
|
||||||
|
assert vehicle["msrp"] == 600
|
||||||
|
assert vehicle["purchase_locations"] == [
|
||||||
|
{
|
||||||
|
"price_buy": 34398000,
|
||||||
|
"terminal_name": "Astro Armada - Area 18",
|
||||||
|
"location": "Area18",
|
||||||
|
"parent_location": "ArcCorp",
|
||||||
|
"star_system": "Stanton",
|
||||||
|
"game_version": "4.8.1-LIVE.11952564",
|
||||||
|
"date_updated": "2026-05-20T18:39:37-04:00",
|
||||||
|
"uex_link": "https://uexcorp.space/vehicles/home/list/in_game_sell/?id_terminal=148",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_wikelo_ship_projects_returns_material_matches():
|
||||||
|
registry = ToolRegistry(FakeUEX(), wikelo=FakeWikelo())
|
||||||
|
|
||||||
|
result = await registry.search_wikelo_ship_projects(query="Polaris")
|
||||||
|
|
||||||
|
assert result["source"] == "https://wikelo-projects.test/Ships"
|
||||||
|
assert result["matched"] == 1
|
||||||
|
assert result["projects"][0]["ship_name"] == "Polaris Wikelo Special"
|
||||||
|
assert result["projects"][0]["required_materials"][0]["material_name"] == "Wikelo Favor"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_wikelo_ship_project_returns_full_requirements():
|
||||||
|
registry = ToolRegistry(FakeUEX(), wikelo=FakeWikelo())
|
||||||
|
|
||||||
|
result = await registry.get_wikelo_ship_project(ship_name="Guardian")
|
||||||
|
|
||||||
|
assert result["project"]["ship_name"] == "Guardian"
|
||||||
|
assert result["project"]["materials_count"] == 1
|
||||||
|
assert result["project"]["required_materials"] == [
|
||||||
|
{
|
||||||
|
"material_name": "Wikelo Favor",
|
||||||
|
"quantity_needed": 20,
|
||||||
|
"quantity_collected": 0,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_draft_marketplace_listing_with_cornerstone_image_adds_image_data_and_redacts_display():
|
||||||
|
registry = ToolRegistry(FakeUEX(), cornerstone=FakeCornerstone())
|
||||||
|
|
||||||
|
result = await registry.draft_marketplace_listing_with_cornerstone_image(
|
||||||
|
item_query="abrade",
|
||||||
|
id_category=3,
|
||||||
|
operation="sell",
|
||||||
|
type="item",
|
||||||
|
unit="unit",
|
||||||
|
title="Abrade Scraper Module",
|
||||||
|
description="Clean module, ready for pickup.",
|
||||||
|
price=21250,
|
||||||
|
currency="UEC",
|
||||||
|
language="en_US",
|
||||||
|
source="purchased_in_game",
|
||||||
|
in_stock=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
pending = result["pending_action"]
|
||||||
|
stored = registry.pending_actions[pending["id"]]
|
||||||
|
|
||||||
|
assert pending["endpoint"] == "marketplace_advertise"
|
||||||
|
assert pending["payload"]["image_data"].startswith("<base64 image data redacted")
|
||||||
|
assert stored.payload["image_data"] == "ZmFrZS1pbWFnZQ=="
|
||||||
|
assert pending["metadata"]["cornerstone_image_status"] == "included"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_draft_marketplace_listing_can_reuse_pasted_chat_image():
|
||||||
|
registry = ToolRegistry(FakeUEX())
|
||||||
|
|
||||||
|
with registry.chat_image_scope([{"name": "listing.png", "content_type": "image/png", "image_data": "ZmFrZS1pbWFnZQ=="}]):
|
||||||
|
result = await registry.draft_marketplace_listing(
|
||||||
|
id_category=3,
|
||||||
|
operation="sell",
|
||||||
|
type="item",
|
||||||
|
unit="unit",
|
||||||
|
title="Abrade Scraper Module",
|
||||||
|
description="Clean module, ready for pickup.",
|
||||||
|
price=21250,
|
||||||
|
currency="UEC",
|
||||||
|
language="en_US",
|
||||||
|
use_attached_image=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
pending = result["pending_action"]
|
||||||
|
stored = registry.pending_actions[pending["id"]]
|
||||||
|
assert pending["payload"]["image_data"].startswith("<base64 image data redacted")
|
||||||
|
assert stored.payload["image_data"] == "ZmFrZS1pbWFnZQ=="
|
||||||
|
assert pending["metadata"]["attached_chat_image_name"] == "listing.png"
|
||||||
|
assert pending["metadata"]["attached_chat_image_status"] == "included"
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_cornerstone_item_page_extracts_locations():
|
||||||
|
parsed = parse_cornerstone_item_page(
|
||||||
|
"""
|
||||||
|
<html><head><title>Star Citizen - Food - Whamburger</title><meta property="og:image" content="/img/wham.png"></head>
|
||||||
|
<body><table><tr><td>NAME</td><td>Whamburger</td></tr></table>
|
||||||
|
<img src="https://example.test/extra.png" alt="Whamburger">
|
||||||
|
<table><tr><th>LOCATION</th><th>BASE PRICE</th><th>VERIFIED</th></tr>
|
||||||
|
<tr><td>Stanton - Area18 - Cubby Blast</td><td>9</td><td>2956-01-01</td></tr></table></body></html>
|
||||||
|
""",
|
||||||
|
"https://finder.cstone.test/Search/item-wham",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert parsed["name"] == "Whamburger"
|
||||||
|
assert parsed["locations"][0]["base_price"] == 9
|
||||||
|
assert parsed["media"][0]["url"] == "https://finder.cstone.test/img/wham.png"
|
||||||
|
assert parsed["media"][1]["url"] == "https://example.test/extra.png"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@respx.mock
|
||||||
|
async def test_cornerstone_client_accepts_json_encoded_string_payload():
|
||||||
|
respx.get("https://finder.cstone.space/GetSearch").mock(
|
||||||
|
return_value=Response(
|
||||||
|
200,
|
||||||
|
json='[{"id":"item-1","name":"Abrade Scraper Module","Sold":1}]',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
client = CornerstoneClient("https://finder.cstone.space")
|
||||||
|
|
||||||
|
assert await client.list_items() == [{"id": "item-1", "name": "Abrade Scraper Module", "sold": True}]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_search_uex_api_index_finds_history_tools():
|
async def test_search_uex_api_index_finds_history_tools():
|
||||||
registry = ToolRegistry(FakeUEX())
|
registry = ToolRegistry(FakeUEX())
|
||||||
|
|||||||
+453
File diff suppressed because one or more lines are too long
+1579
-72
File diff suppressed because it is too large
Load Diff
+43
-3
@@ -11,10 +11,24 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
|||||||
|
|
||||||
|
|
||||||
CONFIG_FIELDS: dict[str, dict[str, Any]] = {
|
CONFIG_FIELDS: dict[str, dict[str, Any]] = {
|
||||||
|
"model_provider": {"env": "MODEL_PROVIDER", "type": "string", "secret": False},
|
||||||
"ollama_base_url": {"env": "OLLAMA_BASE_URL", "type": "string", "secret": False},
|
"ollama_base_url": {"env": "OLLAMA_BASE_URL", "type": "string", "secret": False},
|
||||||
"ollama_model": {"env": "OLLAMA_MODEL", "type": "string", "secret": False},
|
"ollama_model": {"env": "OLLAMA_MODEL", "type": "string", "secret": False},
|
||||||
"ollama_num_ctx": {"env": "OLLAMA_NUM_CTX", "type": "integer", "secret": False},
|
"ollama_num_ctx": {"env": "OLLAMA_NUM_CTX", "type": "integer", "secret": False},
|
||||||
|
"openai_base_url": {"env": "OPENAI_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"openai_model": {"env": "OPENAI_MODEL", "type": "string", "secret": False},
|
||||||
|
"deepseek_base_url": {"env": "DEEPSEEK_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"deepseek_model": {"env": "DEEPSEEK_MODEL", "type": "string", "secret": False},
|
||||||
|
"model_reasoning_effort": {"env": "MODEL_REASONING_EFFORT", "type": "string", "secret": False},
|
||||||
|
"codex_command": {"env": "CODEX_COMMAND", "type": "string", "secret": False},
|
||||||
|
"codex_model": {"env": "CODEX_MODEL", "type": "string", "secret": False},
|
||||||
"uex_base_url": {"env": "UEX_BASE_URL", "type": "string", "secret": False},
|
"uex_base_url": {"env": "UEX_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"scmdb_base_url": {"env": "SCMDB_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"cornerstone_base_url": {"env": "CORNERSTONE_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"scwiki_base_url": {"env": "SCWIKI_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"scwiki_api_base_url": {"env": "SCWIKI_API_BASE_URL", "type": "string", "secret": False},
|
||||||
|
"openai_api_key": {"env": "OPENAI_API_KEY", "type": "string", "secret": True},
|
||||||
|
"deepseek_api_key": {"env": "DEEPSEEK_API_KEY", "type": "string", "secret": True},
|
||||||
"uex_secret_key": {"env": "UEX_SECRET_KEY", "type": "string", "secret": True},
|
"uex_secret_key": {"env": "UEX_SECRET_KEY", "type": "string", "secret": True},
|
||||||
"uex_bearer_token": {"env": "UEX_BEARER_TOKEN", "type": "string", "secret": True},
|
"uex_bearer_token": {"env": "UEX_BEARER_TOKEN", "type": "string", "secret": True},
|
||||||
"traderai_user_name": {"env": "TRADERAI_USER_NAME", "type": "string", "secret": False},
|
"traderai_user_name": {"env": "TRADERAI_USER_NAME", "type": "string", "secret": False},
|
||||||
@@ -60,22 +74,48 @@ class Settings(BaseSettings):
|
|||||||
env_file_encoding="utf-8",
|
env_file_encoding="utf-8",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
model_provider: str = "ollama"
|
||||||
ollama_base_url: str = "http://localhost:11434"
|
ollama_base_url: str = "http://localhost:11434"
|
||||||
ollama_model: str = "qwen3.5:9b"
|
ollama_model: str = "qwen3.5:9b"
|
||||||
ollama_num_ctx: int = 64512
|
ollama_num_ctx: int = 64512
|
||||||
|
openai_base_url: str = "https://api.openai.com/v1"
|
||||||
|
openai_model: str = "gpt-5.4-mini"
|
||||||
|
deepseek_base_url: str = "https://api.deepseek.com"
|
||||||
|
deepseek_model: str = "deepseek-v4-flash"
|
||||||
|
model_reasoning_effort: str = "medium"
|
||||||
|
codex_command: str = "codex"
|
||||||
|
codex_model: str = "gpt-5.4"
|
||||||
uex_base_url: str = "https://api.uexcorp.space/2.0"
|
uex_base_url: str = "https://api.uexcorp.space/2.0"
|
||||||
|
scmdb_base_url: str = "https://scmdb.net"
|
||||||
|
cornerstone_base_url: str = "https://finder.cstone.space"
|
||||||
|
scwiki_base_url: str = "https://starcitizen.tools"
|
||||||
|
scwiki_api_base_url: str = "https://api.star-citizen.wiki"
|
||||||
|
openai_api_key: str | None = Field(default=None)
|
||||||
|
deepseek_api_key: str | None = Field(default=None)
|
||||||
uex_secret_key: str | None = Field(default=None)
|
uex_secret_key: str | None = Field(default=None)
|
||||||
uex_bearer_token: str | None = Field(default=None)
|
uex_bearer_token: str | None = Field(default=None)
|
||||||
traderai_user_name: str | None = Field(default=None)
|
traderai_user_name: str | None = Field(default=None)
|
||||||
traderai_memory_path: str = Field(default_factory=lambda: str(default_memory_path()))
|
traderai_memory_path: str = Field(default_factory=lambda: str(default_memory_path()))
|
||||||
uex_notification_poll_seconds: int = 60
|
uex_notification_poll_seconds: int = 300
|
||||||
require_write_approval: bool = True
|
require_write_approval: bool = True
|
||||||
|
|
||||||
@field_validator("uex_secret_key", "uex_bearer_token", "traderai_user_name", mode="before")
|
@field_validator("openai_api_key", "deepseek_api_key", "uex_secret_key", "uex_bearer_token", "traderai_user_name", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def _blank_optional(cls, value: Any) -> Any:
|
def _blank_optional(cls, value: Any) -> Any:
|
||||||
return None if value == "" else value
|
return None if value == "" else value
|
||||||
|
|
||||||
|
@field_validator("model_provider", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def _normalize_model_provider(cls, value: Any) -> str:
|
||||||
|
text = str(value or "ollama").strip().casefold()
|
||||||
|
return text if text in {"ollama", "deepseek"} else "ollama"
|
||||||
|
|
||||||
|
@field_validator("model_reasoning_effort", mode="before")
|
||||||
|
@classmethod
|
||||||
|
def _normalize_reasoning_effort(cls, value: Any) -> str:
|
||||||
|
text = str(value or "medium").strip().casefold()
|
||||||
|
return text if text in {"none", "minimal", "low", "medium", "high", "xhigh", "max"} else "medium"
|
||||||
|
|
||||||
@field_validator("traderai_memory_path", mode="before")
|
@field_validator("traderai_memory_path", mode="before")
|
||||||
@classmethod
|
@classmethod
|
||||||
def _blank_memory_path(cls, value: Any) -> Any:
|
def _blank_memory_path(cls, value: Any) -> Any:
|
||||||
@@ -133,7 +173,7 @@ def save_settings(values: dict[str, Any]) -> dict[str, Any]:
|
|||||||
def _coerce_value(key: str, value: Any) -> Any:
|
def _coerce_value(key: str, value: Any) -> Any:
|
||||||
field_type = CONFIG_FIELDS[key]["type"]
|
field_type = CONFIG_FIELDS[key]["type"]
|
||||||
if value == "":
|
if value == "":
|
||||||
return None if key in {"uex_secret_key", "uex_bearer_token", "traderai_user_name"} else ""
|
return None if key in {"openai_api_key", "deepseek_api_key", "uex_secret_key", "uex_bearer_token", "traderai_user_name"} else ""
|
||||||
if field_type == "integer":
|
if field_type == "integer":
|
||||||
return int(value)
|
return int(value)
|
||||||
if field_type == "boolean":
|
if field_type == "boolean":
|
||||||
|
|||||||
@@ -0,0 +1,226 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from html.parser import HTMLParser
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
class CornerstoneError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class CornerstoneClient:
|
||||||
|
def __init__(self, base_url: str = "https://finder.cstone.space") -> None:
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
self._items: list[dict[str, Any]] | None = None
|
||||||
|
|
||||||
|
async def list_items(self) -> list[dict[str, Any]]:
|
||||||
|
if self._items is not None:
|
||||||
|
return self._items
|
||||||
|
body = await self._get_json("GetSearch")
|
||||||
|
if isinstance(body, str):
|
||||||
|
body = json.loads(body)
|
||||||
|
if not isinstance(body, list):
|
||||||
|
raise CornerstoneError("Cornerstone search response was not a list.")
|
||||||
|
self._items = [
|
||||||
|
{"id": item.get("id"), "name": item.get("name"), "sold": bool(item.get("Sold"))}
|
||||||
|
for item in body
|
||||||
|
if isinstance(item, dict) and item.get("id") and item.get("name")
|
||||||
|
]
|
||||||
|
return self._items
|
||||||
|
|
||||||
|
async def get_item_page(self, item_id: str) -> dict[str, Any]:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(
|
||||||
|
f"{self.base_url}/Search/{item_id.strip()}",
|
||||||
|
headers={"Accept": "text/html,application/xhtml+xml"},
|
||||||
|
)
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise CornerstoneError(f"Cornerstone HTTP {response.status_code}: {response.text[:240]}")
|
||||||
|
return {"url": str(response.url), "html": response.text}
|
||||||
|
|
||||||
|
async def get_image_data(self, url: str, max_bytes: int = 10_000_000) -> dict[str, Any]:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(url, headers={"Accept": "image/png,image/jpeg,image/*"})
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise CornerstoneError(f"Cornerstone image HTTP {response.status_code}: {response.text[:240]}")
|
||||||
|
content_type = response.headers.get("content-type", "").split(";")[0].strip().casefold()
|
||||||
|
if content_type not in {"image/jpeg", "image/jpg", "image/png"}:
|
||||||
|
raise CornerstoneError(f"Cornerstone image was not JPG or PNG: {content_type or 'unknown content type'}")
|
||||||
|
if len(response.content) > max_bytes:
|
||||||
|
raise CornerstoneError(f"Cornerstone image is larger than {max_bytes} bytes.")
|
||||||
|
return {
|
||||||
|
"url": str(response.url),
|
||||||
|
"content_type": content_type,
|
||||||
|
"size_bytes": len(response.content),
|
||||||
|
"image_data": base64.b64encode(response.content).decode("ascii"),
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _get_json(self, path: str) -> Any:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(f"{self.base_url}/{path.lstrip('/')}", headers={"Accept": "application/json"})
|
||||||
|
try:
|
||||||
|
body = response.json()
|
||||||
|
except ValueError as exc:
|
||||||
|
raise CornerstoneError(f"Cornerstone returned non-JSON response: HTTP {response.status_code}") from exc
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise CornerstoneError(f"Cornerstone HTTP {response.status_code}: {body}")
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
class CornerstonePageParser(HTMLParser):
|
||||||
|
def __init__(self) -> None:
|
||||||
|
super().__init__(convert_charrefs=True)
|
||||||
|
self.title = ""
|
||||||
|
self.tables: list[list[list[str]]] = []
|
||||||
|
self.images: list[dict[str, str]] = []
|
||||||
|
self._skip_depth = 0
|
||||||
|
self._in_title = False
|
||||||
|
self._current_table: list[list[str]] | None = None
|
||||||
|
self._current_row: list[str] | None = None
|
||||||
|
self._current_cell: list[str] | None = None
|
||||||
|
|
||||||
|
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
|
||||||
|
tag = tag.casefold()
|
||||||
|
if tag in {"script", "style"}:
|
||||||
|
self._skip_depth += 1
|
||||||
|
return
|
||||||
|
if self._skip_depth:
|
||||||
|
return
|
||||||
|
if tag == "title":
|
||||||
|
self._in_title = True
|
||||||
|
elif tag == "meta":
|
||||||
|
attr_map = self._attrs(attrs)
|
||||||
|
name = (attr_map.get("property") or attr_map.get("name") or "").casefold()
|
||||||
|
content = attr_map.get("content") or ""
|
||||||
|
if content and name in {"og:image", "twitter:image", "twitter:image:src"}:
|
||||||
|
self.images.append({"url": content, "source": name})
|
||||||
|
elif tag == "link":
|
||||||
|
attr_map = self._attrs(attrs)
|
||||||
|
rel = (attr_map.get("rel") or "").casefold()
|
||||||
|
href = attr_map.get("href") or ""
|
||||||
|
if href and "image_src" in rel:
|
||||||
|
self.images.append({"url": href, "source": "link:image_src"})
|
||||||
|
elif tag == "img":
|
||||||
|
attr_map = self._attrs(attrs)
|
||||||
|
url = attr_map.get("src") or attr_map.get("data-src") or attr_map.get("data-original") or ""
|
||||||
|
if url:
|
||||||
|
self.images.append(
|
||||||
|
{
|
||||||
|
"url": url,
|
||||||
|
"alt": attr_map.get("alt") or "",
|
||||||
|
"source": "img",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
elif tag == "table":
|
||||||
|
self._current_table = []
|
||||||
|
elif tag == "tr" and self._current_table is not None:
|
||||||
|
self._current_row = []
|
||||||
|
elif tag in {"td", "th"} and self._current_row is not None:
|
||||||
|
self._current_cell = []
|
||||||
|
|
||||||
|
def handle_endtag(self, tag: str) -> None:
|
||||||
|
tag = tag.casefold()
|
||||||
|
if tag in {"script", "style"} and self._skip_depth:
|
||||||
|
self._skip_depth -= 1
|
||||||
|
return
|
||||||
|
if self._skip_depth:
|
||||||
|
return
|
||||||
|
if tag == "title":
|
||||||
|
self._in_title = False
|
||||||
|
elif tag in {"td", "th"} and self._current_cell is not None and self._current_row is not None:
|
||||||
|
text = " ".join("".join(self._current_cell).split())
|
||||||
|
self._current_row.append(text)
|
||||||
|
self._current_cell = None
|
||||||
|
elif tag == "tr" and self._current_row is not None and self._current_table is not None:
|
||||||
|
if any(cell for cell in self._current_row):
|
||||||
|
self._current_table.append(self._current_row)
|
||||||
|
self._current_row = None
|
||||||
|
elif tag == "table" and self._current_table is not None:
|
||||||
|
if self._current_table:
|
||||||
|
self.tables.append(self._current_table)
|
||||||
|
self._current_table = None
|
||||||
|
|
||||||
|
def handle_data(self, data: str) -> None:
|
||||||
|
if self._skip_depth:
|
||||||
|
return
|
||||||
|
if self._in_title:
|
||||||
|
self.title += data
|
||||||
|
if self._current_cell is not None:
|
||||||
|
self._current_cell.append(data)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _attrs(attrs: list[tuple[str, str | None]]) -> dict[str, str]:
|
||||||
|
return {key.casefold(): value or "" for key, value in attrs}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_cornerstone_item_page(html: str, page_url: str | None = None) -> dict[str, Any]:
|
||||||
|
parser = CornerstonePageParser()
|
||||||
|
parser.feed(html)
|
||||||
|
info: dict[str, Any] = {"page_title": " ".join(parser.title.split())}
|
||||||
|
general: dict[str, str] = {}
|
||||||
|
locations = []
|
||||||
|
|
||||||
|
for table in parser.tables:
|
||||||
|
if not table:
|
||||||
|
continue
|
||||||
|
header = [cell.casefold() for cell in table[0]]
|
||||||
|
if len(header) >= 3 and "location" in header[0] and "price" in header[1] and "verified" in header[2]:
|
||||||
|
for row in table[1:]:
|
||||||
|
if len(row) < 3:
|
||||||
|
continue
|
||||||
|
locations.append(
|
||||||
|
{
|
||||||
|
"location": row[0],
|
||||||
|
"base_price": _parse_cornerstone_price(row[1]),
|
||||||
|
"base_price_display": row[1],
|
||||||
|
"verified": row[2],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
elif all(len(row) >= 2 for row in table):
|
||||||
|
for row in table:
|
||||||
|
key = row[0].strip().lower().replace(" ", "_")
|
||||||
|
value = row[1].strip()
|
||||||
|
if key and value and key not in general:
|
||||||
|
general[key] = value
|
||||||
|
|
||||||
|
info["name"] = general.get("name") or _name_from_title(info["page_title"])
|
||||||
|
media = _dedupe_media(parser.images, page_url)
|
||||||
|
if media:
|
||||||
|
info["media"] = media
|
||||||
|
if general:
|
||||||
|
info["general"] = general
|
||||||
|
info["locations"] = locations
|
||||||
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_cornerstone_price(value: str) -> int | None:
|
||||||
|
digits = "".join(char for char in value if char.isdigit())
|
||||||
|
return int(digits) if digits else None
|
||||||
|
|
||||||
|
|
||||||
|
def _name_from_title(title: str) -> str | None:
|
||||||
|
if " - " not in title:
|
||||||
|
return title or None
|
||||||
|
return title.rsplit(" - ", 1)[-1].strip() or None
|
||||||
|
|
||||||
|
|
||||||
|
def _dedupe_media(images: list[dict[str, str]], page_url: str | None = None) -> list[dict[str, str]]:
|
||||||
|
media = []
|
||||||
|
seen = set()
|
||||||
|
for image in images:
|
||||||
|
raw_url = (image.get("url") or "").strip()
|
||||||
|
if not raw_url or raw_url.startswith("data:"):
|
||||||
|
continue
|
||||||
|
url = urljoin(page_url or "", raw_url)
|
||||||
|
if url in seen:
|
||||||
|
continue
|
||||||
|
seen.add(url)
|
||||||
|
item = dict(image)
|
||||||
|
item["url"] = url
|
||||||
|
media.append(item)
|
||||||
|
return media
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
@@ -25,6 +26,10 @@ def resource_path(*parts: str) -> Path:
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
try:
|
try:
|
||||||
_chdir_to_app_dir()
|
_chdir_to_app_dir()
|
||||||
|
backend_port = _backend_port_from_args()
|
||||||
|
if backend_port is not None:
|
||||||
|
_run_server(backend_port)
|
||||||
|
return
|
||||||
_log("TraderAI desktop starting")
|
_log("TraderAI desktop starting")
|
||||||
_log(f"cwd={Path.cwd()}")
|
_log(f"cwd={Path.cwd()}")
|
||||||
_log(f"executable={sys.executable}")
|
_log(f"executable={sys.executable}")
|
||||||
@@ -36,6 +41,10 @@ def main() -> None:
|
|||||||
_log("existing TraderAI backend found; opening window")
|
_log("existing TraderAI backend found; opening window")
|
||||||
_open_window(url)
|
_open_window(url)
|
||||||
return
|
return
|
||||||
|
if getattr(sys, "frozen", False):
|
||||||
|
backend_process = _start_backend_process(port)
|
||||||
|
_log(f"backend process started pid={backend_process.pid}")
|
||||||
|
else:
|
||||||
server_thread = threading.Thread(target=_run_server, args=(port,), daemon=True)
|
server_thread = threading.Thread(target=_run_server, args=(port,), daemon=True)
|
||||||
server_thread.start()
|
server_thread.start()
|
||||||
_log("backend thread started")
|
_log("backend thread started")
|
||||||
@@ -62,6 +71,22 @@ def _select_port() -> int:
|
|||||||
return _free_port()
|
return _free_port()
|
||||||
|
|
||||||
|
|
||||||
|
def _backend_port_from_args() -> int | None:
|
||||||
|
args = sys.argv[1:]
|
||||||
|
if len(args) >= 2 and args[0] == "--backend-port":
|
||||||
|
return int(args[1])
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _start_backend_process(port: int) -> subprocess.Popen:
|
||||||
|
command = [sys.executable, "--backend-port", str(port)]
|
||||||
|
_log(f"starting backend subprocess: {' '.join(command)}")
|
||||||
|
kwargs: dict[str, object] = {}
|
||||||
|
if sys.platform == "win32":
|
||||||
|
kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW
|
||||||
|
return subprocess.Popen(command, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _port_available(port: int) -> bool:
|
def _port_available(port: int) -> bool:
|
||||||
try:
|
try:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||||
@@ -88,6 +113,9 @@ def _existing_server_ready(url: str) -> bool:
|
|||||||
def _run_server(port: int) -> NoReturn:
|
def _run_server(port: int) -> NoReturn:
|
||||||
try:
|
try:
|
||||||
_log(f"backend starting on port {port}")
|
_log(f"backend starting on port {port}")
|
||||||
|
if sys.platform == "win32" and hasattr(asyncio, "WindowsProactorEventLoopPolicy"):
|
||||||
|
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
|
||||||
|
_log("set Windows Proactor event loop policy for subprocess-compatible backend")
|
||||||
from traderai.server import app
|
from traderai.server import app
|
||||||
|
|
||||||
config = uvicorn.Config(
|
config = uvicorn.Config(
|
||||||
|
|||||||
+193
-14
@@ -8,6 +8,9 @@ from typing import Any
|
|||||||
from zoneinfo import ZoneInfo
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_THREAD_ID = "default"
|
||||||
|
|
||||||
|
|
||||||
def utc_now() -> datetime:
|
def utc_now() -> datetime:
|
||||||
return datetime.now(timezone.utc)
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
@@ -52,7 +55,7 @@ def _plural(value: int, unit: str) -> str:
|
|||||||
|
|
||||||
class MemoryStore:
|
class MemoryStore:
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: str) -> None:
|
||||||
self.path = Path(path)
|
self.path = Path(path).expanduser().resolve()
|
||||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
self._init_db()
|
self._init_db()
|
||||||
|
|
||||||
@@ -65,8 +68,16 @@ class MemoryStore:
|
|||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
db.executescript(
|
db.executescript(
|
||||||
"""
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS chat_threads (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
created_at TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS conversations (
|
CREATE TABLE IF NOT EXISTS conversations (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
thread_id TEXT,
|
||||||
role TEXT NOT NULL,
|
role TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
created_at TEXT NOT NULL
|
created_at TEXT NOT NULL
|
||||||
@@ -129,25 +140,150 @@ class MemoryStore:
|
|||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
self._ensure_column(db, "conversations", "thread_id", "TEXT")
|
||||||
def add_conversation(self, role: str, content: str) -> None:
|
now = iso_now()
|
||||||
with self._connect() as db:
|
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT INTO conversations(role, content, created_at) VALUES (?, ?, ?)",
|
"""
|
||||||
(role, content, iso_now()),
|
INSERT INTO chat_threads(id, title, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
ON CONFLICT(id) DO NOTHING
|
||||||
|
""",
|
||||||
|
(DEFAULT_THREAD_ID, "New chat", now, now),
|
||||||
|
)
|
||||||
|
db.execute(
|
||||||
|
"UPDATE conversations SET thread_id = ? WHERE thread_id IS NULL OR thread_id = ''",
|
||||||
|
(DEFAULT_THREAD_ID,),
|
||||||
)
|
)
|
||||||
|
|
||||||
def last_interaction(self) -> dict[str, Any] | None:
|
@staticmethod
|
||||||
|
def _ensure_column(db: sqlite3.Connection, table: str, column: str, definition: str) -> None:
|
||||||
|
columns = {row["name"] for row in db.execute(f"PRAGMA table_info({table})").fetchall()}
|
||||||
|
if column not in columns:
|
||||||
|
db.execute(f"ALTER TABLE {table} ADD COLUMN {column} {definition}")
|
||||||
|
|
||||||
|
def ensure_thread(self, thread_id: str | None = None, title: str | None = None) -> dict[str, Any]:
|
||||||
|
now = iso_now()
|
||||||
|
resolved_id = (thread_id or DEFAULT_THREAD_ID).strip() or DEFAULT_THREAD_ID
|
||||||
|
resolved_title = (title or "New chat").strip() or "New chat"
|
||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO chat_threads(id, title, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
ON CONFLICT(id) DO UPDATE SET updated_at=excluded.updated_at
|
||||||
|
""",
|
||||||
|
(resolved_id, resolved_title, now, now),
|
||||||
|
)
|
||||||
row = db.execute(
|
row = db.execute(
|
||||||
"SELECT role, content, created_at FROM conversations ORDER BY id DESC LIMIT 1"
|
"SELECT id, title, created_at, updated_at FROM chat_threads WHERE id = ?",
|
||||||
|
(resolved_id,),
|
||||||
|
).fetchone()
|
||||||
|
return dict(row)
|
||||||
|
|
||||||
|
def create_thread(self, title: str | None = None) -> dict[str, Any]:
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
thread_id = f"chat-{uuid.uuid4()}"
|
||||||
|
return self.ensure_thread(thread_id, title or "New chat")
|
||||||
|
|
||||||
|
def list_threads(self) -> list[dict[str, Any]]:
|
||||||
|
with self._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
t.id,
|
||||||
|
t.title,
|
||||||
|
t.created_at,
|
||||||
|
t.updated_at,
|
||||||
|
COUNT(c.id) AS message_count,
|
||||||
|
MAX(c.created_at) AS last_message_at
|
||||||
|
FROM chat_threads t
|
||||||
|
LEFT JOIN conversations c ON c.thread_id = t.id
|
||||||
|
GROUP BY t.id
|
||||||
|
ORDER BY COALESCE(MAX(c.created_at), t.updated_at) DESC
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
def delete_thread(self, thread_id: str) -> bool:
|
||||||
|
with self._connect() as db:
|
||||||
|
db.execute("DELETE FROM conversations WHERE thread_id = ?", (thread_id,))
|
||||||
|
cursor = db.execute("DELETE FROM chat_threads WHERE id = ?", (thread_id,))
|
||||||
|
return cursor.rowcount > 0
|
||||||
|
|
||||||
|
def rename_thread(self, thread_id: str, title: str) -> dict[str, Any] | None:
|
||||||
|
clean_title = self._clean_thread_title(title)
|
||||||
|
if not clean_title:
|
||||||
|
return None
|
||||||
|
now = iso_now()
|
||||||
|
with self._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"UPDATE chat_threads SET title = ?, updated_at = ? WHERE id = ?",
|
||||||
|
(clean_title, now, thread_id),
|
||||||
|
)
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT id, title, created_at, updated_at FROM chat_threads WHERE id = ?",
|
||||||
|
(thread_id,),
|
||||||
).fetchone()
|
).fetchone()
|
||||||
return dict(row) if row else None
|
return dict(row) if row else None
|
||||||
|
|
||||||
def recent_conversation(self, limit: int = 8) -> list[dict[str, Any]]:
|
def get_thread(self, thread_id: str) -> dict[str, Any] | None:
|
||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT id, title, created_at, updated_at FROM chat_threads WHERE id = ?",
|
||||||
|
(thread_id,),
|
||||||
|
).fetchone()
|
||||||
|
return dict(row) if row else None
|
||||||
|
|
||||||
|
def add_conversation(self, role: str, content: str, thread_id: str | None = DEFAULT_THREAD_ID) -> None:
|
||||||
|
resolved_thread_id = (thread_id or DEFAULT_THREAD_ID).strip() or DEFAULT_THREAD_ID
|
||||||
|
self.ensure_thread(resolved_thread_id)
|
||||||
|
now = iso_now()
|
||||||
|
with self._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO conversations(thread_id, role, content, created_at) VALUES (?, ?, ?, ?)",
|
||||||
|
(resolved_thread_id, role, content, now),
|
||||||
|
)
|
||||||
|
db.execute(
|
||||||
|
"UPDATE chat_threads SET updated_at = ? WHERE id = ?",
|
||||||
|
(now, resolved_thread_id),
|
||||||
|
)
|
||||||
|
|
||||||
|
def last_interaction(self, thread_id: str | None = None) -> dict[str, Any] | None:
|
||||||
|
with self._connect() as db:
|
||||||
|
if thread_id:
|
||||||
|
row = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT thread_id, role, content, created_at
|
||||||
|
FROM conversations
|
||||||
|
WHERE thread_id = ?
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(thread_id,),
|
||||||
|
).fetchone()
|
||||||
|
else:
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT thread_id, role, content, created_at FROM conversations ORDER BY id DESC LIMIT 1"
|
||||||
|
).fetchone()
|
||||||
|
return dict(row) if row else None
|
||||||
|
|
||||||
|
def recent_conversation(self, limit: int = 8, thread_id: str | None = None) -> list[dict[str, Any]]:
|
||||||
|
with self._connect() as db:
|
||||||
|
if thread_id:
|
||||||
rows = db.execute(
|
rows = db.execute(
|
||||||
"SELECT role, content, created_at FROM conversations ORDER BY id DESC LIMIT ?",
|
"""
|
||||||
|
SELECT id, thread_id, role, content, created_at
|
||||||
|
FROM conversations
|
||||||
|
WHERE thread_id = ?
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(thread_id, limit),
|
||||||
|
).fetchall()
|
||||||
|
else:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT id, thread_id, role, content, created_at FROM conversations ORDER BY id DESC LIMIT ?",
|
||||||
(limit,),
|
(limit,),
|
||||||
).fetchall()
|
).fetchall()
|
||||||
return [dict(row) for row in reversed(rows)]
|
return [dict(row) for row in reversed(rows)]
|
||||||
@@ -222,13 +358,22 @@ class MemoryStore:
|
|||||||
).fetchall()
|
).fetchall()
|
||||||
conversations = db.execute(
|
conversations = db.execute(
|
||||||
"""
|
"""
|
||||||
SELECT id, role, content, created_at
|
SELECT id, thread_id, role, content, created_at
|
||||||
FROM conversations
|
FROM conversations
|
||||||
ORDER BY id DESC
|
ORDER BY id DESC
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
""",
|
""",
|
||||||
(limit,),
|
(limit,),
|
||||||
).fetchall()
|
).fetchall()
|
||||||
|
threads = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, title, created_at, updated_at
|
||||||
|
FROM chat_threads
|
||||||
|
ORDER BY updated_at DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(limit,),
|
||||||
|
).fetchall()
|
||||||
profile_rows = db.execute(
|
profile_rows = db.execute(
|
||||||
"SELECT key, value, updated_at FROM user_profile ORDER BY key"
|
"SELECT key, value, updated_at FROM user_profile ORDER BY key"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
@@ -252,6 +397,7 @@ class MemoryStore:
|
|||||||
return {
|
return {
|
||||||
"path": str(self.path),
|
"path": str(self.path),
|
||||||
"memories": [self._memory_row(row) for row in memories],
|
"memories": [self._memory_row(row) for row in memories],
|
||||||
|
"chat_threads": [dict(row) for row in threads],
|
||||||
"conversations": [dict(row) for row in conversations],
|
"conversations": [dict(row) for row in conversations],
|
||||||
"profile": profile,
|
"profile": profile,
|
||||||
"scheduled_jobs": [dict(row) for row in jobs],
|
"scheduled_jobs": [dict(row) for row in jobs],
|
||||||
@@ -339,17 +485,38 @@ class MemoryStore:
|
|||||||
).fetchall()
|
).fetchall()
|
||||||
return [dict(row) for row in rows]
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
def mark_job_run(self, job_id: str, next_run_at: str | None = None) -> None:
|
def mark_job_run(self, job_id: str, next_run_at: str | None = None, enabled: bool = True) -> None:
|
||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
"UPDATE scheduled_jobs SET last_run_at = ?, next_run_at = ? WHERE id = ?",
|
"UPDATE scheduled_jobs SET last_run_at = ?, next_run_at = ?, enabled = ? WHERE id = ?",
|
||||||
(iso_now(), next_run_at, job_id),
|
(iso_now(), next_run_at, 1 if enabled else 0, job_id),
|
||||||
)
|
)
|
||||||
|
|
||||||
def add_outbox(self, content: str) -> None:
|
def add_outbox(self, content: str) -> None:
|
||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
db.execute("INSERT INTO outbox(content, created_at) VALUES (?, ?)", (content, iso_now()))
|
db.execute("INSERT INTO outbox(content, created_at) VALUES (?, ?)", (content, iso_now()))
|
||||||
|
|
||||||
|
def list_outbox(self, limit: int = 100) -> list[dict[str, Any]]:
|
||||||
|
with self._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT id, content, created_at, delivered_at FROM outbox ORDER BY id DESC LIMIT ?",
|
||||||
|
(limit,),
|
||||||
|
).fetchall()
|
||||||
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
def get_outbox(self, inbox_id: int) -> dict[str, Any] | None:
|
||||||
|
with self._connect() as db:
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT id, content, created_at, delivered_at FROM outbox WHERE id = ?",
|
||||||
|
(inbox_id,),
|
||||||
|
).fetchone()
|
||||||
|
return dict(row) if row else None
|
||||||
|
|
||||||
|
def delete_outbox(self, inbox_id: int) -> bool:
|
||||||
|
with self._connect() as db:
|
||||||
|
cursor = db.execute("DELETE FROM outbox WHERE id = ?", (inbox_id,))
|
||||||
|
return cursor.rowcount > 0
|
||||||
|
|
||||||
def undelivered_outbox(self) -> list[dict[str, Any]]:
|
def undelivered_outbox(self) -> list[dict[str, Any]]:
|
||||||
now = iso_now()
|
now = iso_now()
|
||||||
with self._connect() as db:
|
with self._connect() as db:
|
||||||
@@ -362,6 +529,18 @@ class MemoryStore:
|
|||||||
)
|
)
|
||||||
return [dict(row) for row in rows]
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _thread_title(content: str) -> str:
|
||||||
|
text = " ".join(content.strip().split())
|
||||||
|
if not text:
|
||||||
|
return "New chat"
|
||||||
|
return text[:42] + ("..." if len(text) > 42 else "")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _clean_thread_title(title: str) -> str:
|
||||||
|
text = " ".join(title.strip().strip('"').strip("'").split())
|
||||||
|
return text[:64]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _fts_query(query: str) -> str:
|
def _fts_query(query: str) -> str:
|
||||||
tokens = [token.replace('"', "") for token in query.split() if token.strip()]
|
tokens = [token.replace('"', "") for token in query.split() if token.strip()]
|
||||||
|
|||||||
@@ -0,0 +1,614 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from traderai.memory import MemoryStore, iso_now
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_PLAN_CADENCE = "0 */6 * * *"
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanStore:
|
||||||
|
def __init__(self, memory: MemoryStore) -> None:
|
||||||
|
self.memory = memory
|
||||||
|
self._init_db()
|
||||||
|
|
||||||
|
def _init_db(self) -> None:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.executescript(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS continual_plans (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
kind TEXT NOT NULL,
|
||||||
|
status TEXT NOT NULL,
|
||||||
|
objective TEXT NOT NULL,
|
||||||
|
constraints TEXT NOT NULL DEFAULT '{}',
|
||||||
|
cadence TEXT NOT NULL,
|
||||||
|
next_run_at TEXT,
|
||||||
|
last_run_at TEXT,
|
||||||
|
created_at TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS continual_plan_items (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
plan_id TEXT NOT NULL,
|
||||||
|
item_name TEXT NOT NULL,
|
||||||
|
desired_quantity INTEGER NOT NULL DEFAULT 1,
|
||||||
|
max_unit_price REAL,
|
||||||
|
status TEXT NOT NULL DEFAULT 'active',
|
||||||
|
acquired_quantity INTEGER NOT NULL DEFAULT 0,
|
||||||
|
metadata TEXT NOT NULL DEFAULT '{}',
|
||||||
|
created_at TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS continual_plan_candidates (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
plan_id TEXT NOT NULL,
|
||||||
|
plan_item_id INTEGER NOT NULL,
|
||||||
|
listing_id TEXT,
|
||||||
|
listing_slug TEXT,
|
||||||
|
title TEXT,
|
||||||
|
seller TEXT,
|
||||||
|
price REAL,
|
||||||
|
currency TEXT,
|
||||||
|
stock INTEGER,
|
||||||
|
location TEXT,
|
||||||
|
score REAL,
|
||||||
|
first_seen_at TEXT NOT NULL,
|
||||||
|
last_seen_at TEXT NOT NULL,
|
||||||
|
status TEXT NOT NULL DEFAULT 'current',
|
||||||
|
metadata TEXT NOT NULL DEFAULT '{}',
|
||||||
|
UNIQUE(plan_item_id, listing_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS continual_plan_events (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
plan_id TEXT NOT NULL,
|
||||||
|
kind TEXT NOT NULL,
|
||||||
|
message TEXT NOT NULL,
|
||||||
|
metadata TEXT NOT NULL DEFAULT '{}',
|
||||||
|
created_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS continual_plan_negotiations (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
plan_id TEXT NOT NULL,
|
||||||
|
plan_item_id INTEGER,
|
||||||
|
candidate_id INTEGER,
|
||||||
|
listing_id TEXT,
|
||||||
|
listing_slug TEXT,
|
||||||
|
negotiation_id TEXT,
|
||||||
|
negotiation_hash TEXT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'drafted',
|
||||||
|
created_at TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
def create_plan(
|
||||||
|
self,
|
||||||
|
title: str,
|
||||||
|
kind: str = "buying",
|
||||||
|
objective: str = "",
|
||||||
|
items: list[dict[str, Any]] | None = None,
|
||||||
|
constraints: dict[str, Any] | None = None,
|
||||||
|
cadence: str | None = None,
|
||||||
|
status: str | None = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
clean_items = [item for item in (items or []) if str(item.get("item_name") or item.get("name") or "").strip()]
|
||||||
|
plan_id = f"plan-{uuid.uuid4()}"
|
||||||
|
now = iso_now()
|
||||||
|
clean_kind = (kind.strip() or "buying").casefold()
|
||||||
|
resolved_status = status or ("needs_input" if clean_kind == "buying" and not clean_items else "active")
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO continual_plans(id, title, kind, status, objective, constraints, cadence, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
plan_id,
|
||||||
|
title.strip() or "Continual plan",
|
||||||
|
clean_kind,
|
||||||
|
resolved_status,
|
||||||
|
objective.strip() or title.strip(),
|
||||||
|
json.dumps(constraints or {}),
|
||||||
|
(cadence or DEFAULT_PLAN_CADENCE).strip() or DEFAULT_PLAN_CADENCE,
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
for item in clean_items:
|
||||||
|
db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO continual_plan_items(
|
||||||
|
plan_id, item_name, desired_quantity, max_unit_price, status,
|
||||||
|
acquired_quantity, metadata, created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, 'active', ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
plan_id,
|
||||||
|
str(item.get("item_name") or item.get("name")).strip(),
|
||||||
|
max(1, int(item.get("desired_quantity") or item.get("quantity") or 1)),
|
||||||
|
item.get("max_unit_price"),
|
||||||
|
max(0, int(item.get("acquired_quantity") or 0)),
|
||||||
|
json.dumps(item.get("metadata") or {}),
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if clean_kind == "buying" and not clean_items:
|
||||||
|
self.add_event(plan_id, "needs_input", "Created plan, but no item checklist was provided. Add the required parts before it can run.")
|
||||||
|
elif clean_items:
|
||||||
|
self.add_event(plan_id, "created", f"Created continual {clean_kind} plan with {len(clean_items)} checklist item(s).")
|
||||||
|
else:
|
||||||
|
self.add_event(plan_id, "created", f"Created continual {clean_kind} plan.")
|
||||||
|
return self.get_plan(plan_id) or {}
|
||||||
|
|
||||||
|
def list_plans(self, include_inactive: bool = True) -> list[dict[str, Any]]:
|
||||||
|
where = "" if include_inactive else "WHERE status = 'active'"
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
f"""
|
||||||
|
SELECT *
|
||||||
|
FROM continual_plans
|
||||||
|
{where}
|
||||||
|
ORDER BY
|
||||||
|
CASE status WHEN 'active' THEN 0 WHEN 'needs_input' THEN 1 WHEN 'paused' THEN 2 ELSE 3 END,
|
||||||
|
updated_at DESC
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
return [self._plan_row(row) for row in rows]
|
||||||
|
|
||||||
|
def get_plan(self, plan_id: str) -> dict[str, Any] | None:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
plan = db.execute("SELECT * FROM continual_plans WHERE id = ?", (plan_id,)).fetchone()
|
||||||
|
if not plan:
|
||||||
|
return None
|
||||||
|
data = self._plan_row(plan)
|
||||||
|
data["items"] = self.list_items(plan_id)
|
||||||
|
data["candidates"] = self.list_candidates(plan_id)
|
||||||
|
data["negotiations"] = self.list_negotiations(plan_id)
|
||||||
|
data["events"] = self.list_events(plan_id)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def list_items(self, plan_id: str) -> list[dict[str, Any]]:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT * FROM continual_plan_items WHERE plan_id = ? ORDER BY id",
|
||||||
|
(plan_id,),
|
||||||
|
).fetchall()
|
||||||
|
return [self._json_row(row, "metadata") for row in rows]
|
||||||
|
|
||||||
|
def list_candidates(self, plan_id: str, limit: int = 100) -> list[dict[str, Any]]:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT *
|
||||||
|
FROM continual_plan_candidates
|
||||||
|
WHERE plan_id = ?
|
||||||
|
ORDER BY status = 'current' DESC, score DESC, last_seen_at DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(plan_id, limit),
|
||||||
|
).fetchall()
|
||||||
|
return [self._json_row(row, "metadata") for row in rows]
|
||||||
|
|
||||||
|
def list_events(self, plan_id: str, limit: int = 50) -> list[dict[str, Any]]:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT *
|
||||||
|
FROM continual_plan_events
|
||||||
|
WHERE plan_id = ?
|
||||||
|
ORDER BY id DESC
|
||||||
|
LIMIT ?
|
||||||
|
""",
|
||||||
|
(plan_id, limit),
|
||||||
|
).fetchall()
|
||||||
|
return [self._json_row(row, "metadata") for row in rows]
|
||||||
|
|
||||||
|
def list_negotiations(self, plan_id: str) -> list[dict[str, Any]]:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT * FROM continual_plan_negotiations WHERE plan_id = ? ORDER BY updated_at DESC",
|
||||||
|
(plan_id,),
|
||||||
|
).fetchall()
|
||||||
|
return [dict(row) for row in rows]
|
||||||
|
|
||||||
|
def set_status(self, plan_id: str, status: str) -> dict[str, Any] | None:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"UPDATE continual_plans SET status = ?, updated_at = ? WHERE id = ?",
|
||||||
|
(status, iso_now(), plan_id),
|
||||||
|
)
|
||||||
|
self.add_event(plan_id, status, f"Plan status changed to {status}.")
|
||||||
|
return self.get_plan(plan_id)
|
||||||
|
|
||||||
|
def delete_plan(self, plan_id: str) -> bool:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
deleted = db.execute("DELETE FROM continual_plans WHERE id = ?", (plan_id,)).rowcount
|
||||||
|
if not deleted:
|
||||||
|
return False
|
||||||
|
db.execute("DELETE FROM continual_plan_items WHERE plan_id = ?", (plan_id,))
|
||||||
|
db.execute("DELETE FROM continual_plan_candidates WHERE plan_id = ?", (plan_id,))
|
||||||
|
db.execute("DELETE FROM continual_plan_events WHERE plan_id = ?", (plan_id,))
|
||||||
|
db.execute("DELETE FROM continual_plan_negotiations WHERE plan_id = ?", (plan_id,))
|
||||||
|
return True
|
||||||
|
|
||||||
|
def add_event(self, plan_id: str, kind: str, message: str, metadata: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||||
|
now = iso_now()
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
cursor = db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO continual_plan_events(plan_id, kind, message, metadata, created_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(plan_id, kind, message, json.dumps(metadata or {}), now),
|
||||||
|
)
|
||||||
|
return {"id": cursor.lastrowid, "plan_id": plan_id, "kind": kind, "message": message, "created_at": now}
|
||||||
|
|
||||||
|
def update_schedule(self, plan_id: str, next_run_at: str | None = None, last_run_at: str | None = None) -> None:
|
||||||
|
fields = ["next_run_at = ?", "updated_at = ?"]
|
||||||
|
values: list[Any] = [next_run_at, iso_now()]
|
||||||
|
if last_run_at is not None:
|
||||||
|
fields.insert(1, "last_run_at = ?")
|
||||||
|
values.insert(1, last_run_at)
|
||||||
|
values.append(plan_id)
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.execute(f"UPDATE continual_plans SET {', '.join(fields)} WHERE id = ?", values)
|
||||||
|
|
||||||
|
def upsert_candidate(self, plan_id: str, plan_item_id: int, listing: dict[str, Any], score: float) -> dict[str, Any]:
|
||||||
|
now = iso_now()
|
||||||
|
listing_id = str(listing.get("id") or listing.get("listing_id") or listing.get("slug") or uuid.uuid4())
|
||||||
|
metadata = dict(listing)
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO continual_plan_candidates(
|
||||||
|
plan_id, plan_item_id, listing_id, listing_slug, title, seller, price, currency,
|
||||||
|
stock, location, score, first_seen_at, last_seen_at, status, metadata
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'current', ?)
|
||||||
|
ON CONFLICT(plan_item_id, listing_id) DO UPDATE SET
|
||||||
|
listing_slug=excluded.listing_slug,
|
||||||
|
title=excluded.title,
|
||||||
|
seller=excluded.seller,
|
||||||
|
price=excluded.price,
|
||||||
|
currency=excluded.currency,
|
||||||
|
stock=excluded.stock,
|
||||||
|
location=excluded.location,
|
||||||
|
score=excluded.score,
|
||||||
|
last_seen_at=excluded.last_seen_at,
|
||||||
|
status='current',
|
||||||
|
metadata=excluded.metadata
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
plan_id,
|
||||||
|
plan_item_id,
|
||||||
|
listing_id,
|
||||||
|
listing.get("slug"),
|
||||||
|
listing.get("title"),
|
||||||
|
listing.get("advertiser") or listing.get("user_username") or listing.get("seller"),
|
||||||
|
listing.get("price"),
|
||||||
|
listing.get("currency"),
|
||||||
|
listing.get("in_stock") or listing.get("stock"),
|
||||||
|
listing.get("location"),
|
||||||
|
score,
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
json.dumps(metadata),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
row = db.execute(
|
||||||
|
"SELECT * FROM continual_plan_candidates WHERE plan_item_id = ? AND listing_id = ?",
|
||||||
|
(plan_item_id, listing_id),
|
||||||
|
).fetchone()
|
||||||
|
return self._json_row(row, "metadata")
|
||||||
|
|
||||||
|
def mark_stale_candidates(self, plan_item_id: int, seen_listing_ids: set[str]) -> int:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT id, listing_id FROM continual_plan_candidates WHERE plan_item_id = ? AND status = 'current'",
|
||||||
|
(plan_item_id,),
|
||||||
|
).fetchall()
|
||||||
|
stale_ids = [row["id"] for row in rows if str(row["listing_id"]) not in seen_listing_ids]
|
||||||
|
if stale_ids:
|
||||||
|
placeholders = ",".join("?" for _ in stale_ids)
|
||||||
|
db.execute(
|
||||||
|
f"UPDATE continual_plan_candidates SET status = 'stale', last_seen_at = ? WHERE id IN ({placeholders})",
|
||||||
|
(iso_now(), *stale_ids),
|
||||||
|
)
|
||||||
|
return len(stale_ids)
|
||||||
|
|
||||||
|
def mark_candidate_drafted(self, candidate_id: int) -> None:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
db.execute("UPDATE continual_plan_candidates SET status = 'drafted', last_seen_at = ? WHERE id = ?", (iso_now(), candidate_id))
|
||||||
|
|
||||||
|
def add_negotiation(self, plan_id: str, plan_item_id: int | None, candidate_id: int | None, metadata: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
now = iso_now()
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
cursor = db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO continual_plan_negotiations(
|
||||||
|
plan_id, plan_item_id, candidate_id, listing_id, listing_slug,
|
||||||
|
negotiation_id, negotiation_hash, status, created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
plan_id,
|
||||||
|
plan_item_id,
|
||||||
|
candidate_id,
|
||||||
|
metadata.get("listing_id"),
|
||||||
|
metadata.get("listing_slug"),
|
||||||
|
metadata.get("id_negotiation"),
|
||||||
|
metadata.get("hash"),
|
||||||
|
metadata.get("status") or "drafted",
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
row = db.execute("SELECT * FROM continual_plan_negotiations WHERE id = ?", (cursor.lastrowid,)).fetchone()
|
||||||
|
return dict(row)
|
||||||
|
|
||||||
|
def has_negotiation_for_candidate(self, plan_id: str, plan_item_id: int, candidate: dict[str, Any]) -> bool:
|
||||||
|
with self.memory._connect() as db:
|
||||||
|
row = db.execute(
|
||||||
|
"""
|
||||||
|
SELECT id
|
||||||
|
FROM continual_plan_negotiations
|
||||||
|
WHERE plan_id = ?
|
||||||
|
AND plan_item_id = ?
|
||||||
|
AND (
|
||||||
|
candidate_id = ?
|
||||||
|
OR (listing_id IS NOT NULL AND listing_id = ?)
|
||||||
|
OR (listing_slug IS NOT NULL AND listing_slug = ?)
|
||||||
|
)
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
plan_id,
|
||||||
|
plan_item_id,
|
||||||
|
candidate.get("id"),
|
||||||
|
candidate.get("listing_id"),
|
||||||
|
candidate.get("listing_slug"),
|
||||||
|
),
|
||||||
|
).fetchone()
|
||||||
|
return row is not None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _json_row(row: Any, *json_fields: str) -> dict[str, Any]:
|
||||||
|
data = dict(row)
|
||||||
|
for field in json_fields:
|
||||||
|
try:
|
||||||
|
data[field] = json.loads(data.get(field) or "{}")
|
||||||
|
except (TypeError, json.JSONDecodeError):
|
||||||
|
data[field] = {}
|
||||||
|
return data
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _plan_row(cls, row: Any) -> dict[str, Any]:
|
||||||
|
return cls._json_row(row, "constraints")
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanRunner:
|
||||||
|
def __init__(self, store: ContinualPlanStore, tools: Any, memory: MemoryStore, agent: Any | None = None) -> None:
|
||||||
|
self.store = store
|
||||||
|
self.tools = tools
|
||||||
|
self.memory = memory
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_agent(self, agent: Any) -> None:
|
||||||
|
self.agent = agent
|
||||||
|
|
||||||
|
async def run_plan(self, plan_id: str) -> dict[str, Any]:
|
||||||
|
plan = self.store.get_plan(plan_id)
|
||||||
|
if not plan:
|
||||||
|
return {"error": f"Plan not found: {plan_id}"}
|
||||||
|
if plan["status"] != "active":
|
||||||
|
message = f"Skipped {plan['title']} because status is {plan['status']}."
|
||||||
|
self.store.add_event(plan_id, "skipped", message)
|
||||||
|
return {"status": "skipped", "summary": message, "plan": self.store.get_plan(plan_id)}
|
||||||
|
try:
|
||||||
|
if plan["kind"] == "buying":
|
||||||
|
result = await self._run_buying_plan(plan)
|
||||||
|
else:
|
||||||
|
result = await self._run_agent_plan(plan)
|
||||||
|
self.store.update_schedule(plan_id, plan.get("next_run_at"), last_run_at=iso_now())
|
||||||
|
self.memory.add_outbox(result["summary"])
|
||||||
|
return {**result, "plan": self.store.get_plan(plan_id)}
|
||||||
|
except Exception as exc:
|
||||||
|
message = f"Continual plan failed: {exc}"
|
||||||
|
self.store.add_event(plan_id, "error", message)
|
||||||
|
self.memory.add_outbox(f"{plan['title']}: {message}")
|
||||||
|
self.store.update_schedule(plan_id, plan.get("next_run_at"), last_run_at=iso_now())
|
||||||
|
return {"error": str(exc), "summary": message, "plan": self.store.get_plan(plan_id)}
|
||||||
|
|
||||||
|
async def _run_agent_plan(self, plan: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
if self.agent is None:
|
||||||
|
raise RuntimeError("No agent is bound to run generic continual plans.")
|
||||||
|
prompt = self._agent_plan_prompt(plan)
|
||||||
|
response = await self.agent.generate_wake_response(prompt)
|
||||||
|
summary = f"{plan['title']}: {response}"
|
||||||
|
self.store.add_event(plan["id"], "run", "Ran generic continual plan through the agent.", {"response": response})
|
||||||
|
return {"status": "ok", "summary": summary, "checked": 0, "drafted": 0}
|
||||||
|
|
||||||
|
async def _run_buying_plan(self, plan: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
items = [item for item in plan.get("items") or [] if item.get("status") != "acquired"]
|
||||||
|
if not items:
|
||||||
|
self.store.set_status(plan["id"], "completed")
|
||||||
|
summary = f"{plan['title']}: all checklist items are marked acquired."
|
||||||
|
return {"status": "completed", "summary": summary, "drafted": 0, "checked": 0}
|
||||||
|
|
||||||
|
checked = 0
|
||||||
|
drafted = 0
|
||||||
|
best_lines = []
|
||||||
|
constraints = plan.get("constraints") or {}
|
||||||
|
excluded_sellers = {str(value).casefold() for value in constraints.get("excluded_sellers") or []}
|
||||||
|
preferred_locations = [str(value).casefold() for value in constraints.get("preferred_locations") or []]
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
response = await self.tools.search_marketplace_listings(
|
||||||
|
query=item["item_name"],
|
||||||
|
operation="sell",
|
||||||
|
type="item",
|
||||||
|
limit=25,
|
||||||
|
)
|
||||||
|
listings = response.get("listings") or response.get("data") or []
|
||||||
|
seen: set[str] = set()
|
||||||
|
candidates = []
|
||||||
|
for listing in listings:
|
||||||
|
if not isinstance(listing, dict):
|
||||||
|
continue
|
||||||
|
listing_id = str(listing.get("id") or listing.get("slug") or "")
|
||||||
|
if listing_id:
|
||||||
|
seen.add(listing_id)
|
||||||
|
if str(listing.get("advertiser") or listing.get("seller") or "").casefold() in excluded_sellers:
|
||||||
|
continue
|
||||||
|
score = self._candidate_score(listing, item, preferred_locations)
|
||||||
|
candidate = self.store.upsert_candidate(plan["id"], int(item["id"]), listing, score)
|
||||||
|
candidates.append(candidate)
|
||||||
|
stale = self.store.mark_stale_candidates(int(item["id"]), seen)
|
||||||
|
checked += 1
|
||||||
|
current_candidates = [candidate for candidate in candidates if candidate.get("status") == "current"]
|
||||||
|
current_candidates.sort(key=lambda candidate: (-float(candidate.get("score") or 0), float(candidate.get("price") or 10**18)))
|
||||||
|
best = current_candidates[0] if current_candidates else None
|
||||||
|
if not best:
|
||||||
|
best_lines.append(f"{item['item_name']}: no active matching sell listings found.")
|
||||||
|
self.store.add_event(plan["id"], "search", f"{item['item_name']}: no active candidates found.", {"stale": stale})
|
||||||
|
continue
|
||||||
|
|
||||||
|
best_lines.append(
|
||||||
|
f"{item['item_name']}: best candidate is {best.get('title') or best.get('listing_slug')} "
|
||||||
|
f"at {self._format_price(best.get('price'), best.get('currency'))} from {best.get('seller') or 'unknown seller'}."
|
||||||
|
)
|
||||||
|
self.store.add_event(
|
||||||
|
plan["id"],
|
||||||
|
"search",
|
||||||
|
f"{item['item_name']}: found {len(current_candidates)} current candidate(s); {stale} stale candidate(s) marked.",
|
||||||
|
{"best_candidate_id": best.get("id")},
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.store.has_negotiation_for_candidate(plan["id"], int(item["id"]), best) or not self._within_budget(best, item, constraints):
|
||||||
|
continue
|
||||||
|
draft = await self._draft_buying_message(plan, item, best)
|
||||||
|
if "pending_action" in draft:
|
||||||
|
drafted += 1
|
||||||
|
self.store.mark_candidate_drafted(int(best["id"]))
|
||||||
|
self.store.add_negotiation(
|
||||||
|
plan["id"],
|
||||||
|
int(item["id"]),
|
||||||
|
int(best["id"]),
|
||||||
|
{
|
||||||
|
"listing_id": best.get("listing_id"),
|
||||||
|
"listing_slug": best.get("listing_slug"),
|
||||||
|
"status": "drafted",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.store.add_event(
|
||||||
|
plan["id"],
|
||||||
|
"draft",
|
||||||
|
f"Drafted negotiation opener for {item['item_name']} candidate {best.get('listing_id')}.",
|
||||||
|
{"pending_action_id": draft["pending_action"].get("id"), "candidate_id": best.get("id")},
|
||||||
|
)
|
||||||
|
|
||||||
|
summary = f"{plan['title']}: checked {checked} item(s). " + " ".join(best_lines[:4])
|
||||||
|
if drafted:
|
||||||
|
summary += f" Drafted {drafted} negotiation message(s) for approval."
|
||||||
|
self.store.add_event(plan["id"], "run", summary, {"checked": checked, "drafted": drafted})
|
||||||
|
return {"status": "ok", "summary": summary, "checked": checked, "drafted": drafted}
|
||||||
|
|
||||||
|
async def _draft_buying_message(self, plan: dict[str, Any], item: dict[str, Any], candidate: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
tone = (plan.get("constraints") or {}).get("message_tone") or "polite and concise"
|
||||||
|
greeting = "Hi" if "professional" in str(tone).casefold() or "polite" in str(tone).casefold() else "Hey"
|
||||||
|
build_context = self._plan_build_context(plan["objective"])
|
||||||
|
message = (
|
||||||
|
f"{greeting}, is your {candidate.get('title') or item['item_name']} listing still available "
|
||||||
|
f"for {self._format_price(candidate.get('price'), candidate.get('currency'))}? "
|
||||||
|
f"{build_context}If you still have it, I can move quickly."
|
||||||
|
).strip()
|
||||||
|
return await self.tools.draft_negotiation_message(
|
||||||
|
message=message,
|
||||||
|
id_listing=self._int_or_none(candidate.get("listing_id")),
|
||||||
|
plan_id=plan["id"],
|
||||||
|
plan_item_id=int(item["id"]),
|
||||||
|
candidate_id=int(candidate["id"]),
|
||||||
|
listing_slug=candidate.get("listing_slug"),
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _plan_build_context(objective: str) -> str:
|
||||||
|
text = str(objective or "").strip().rstrip(".")
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
lowered = text.casefold()
|
||||||
|
if "polaris" in lowered:
|
||||||
|
return "I'm putting together parts for a Polaris build. "
|
||||||
|
if "mission" in lowered:
|
||||||
|
return "I'm trying to wrap up a mission build. "
|
||||||
|
return "I'm sourcing parts for a build. "
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _candidate_score(listing: dict[str, Any], item: dict[str, Any], preferred_locations: list[str]) -> float:
|
||||||
|
price = float(listing.get("price") or 10**12)
|
||||||
|
max_price = item.get("max_unit_price")
|
||||||
|
budget_bonus = 40.0 if max_price and price <= float(max_price) else 0.0
|
||||||
|
stock = float(listing.get("in_stock") or listing.get("stock") or 1)
|
||||||
|
location = str(listing.get("location") or "").casefold()
|
||||||
|
location_bonus = 8.0 if preferred_locations and any(place in location for place in preferred_locations) else 0.0
|
||||||
|
return round(max(0.0, 50.0 - (price / 10_000_000.0)) + min(stock, 20.0) + budget_bonus + location_bonus, 4)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _within_budget(candidate: dict[str, Any], item: dict[str, Any], constraints: dict[str, Any]) -> bool:
|
||||||
|
price = candidate.get("price")
|
||||||
|
if price is None:
|
||||||
|
return False
|
||||||
|
max_price = item.get("max_unit_price") or constraints.get("max_unit_price")
|
||||||
|
return max_price is None or float(price) <= float(max_price)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _format_price(price: Any, currency: Any) -> str:
|
||||||
|
if isinstance(price, (int, float)):
|
||||||
|
return f"{price:,.0f} {currency or 'UEC'}"
|
||||||
|
return f"unknown price {currency or 'UEC'}"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _int_or_none(value: Any) -> int | None:
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _agent_plan_prompt(plan: dict[str, Any]) -> str:
|
||||||
|
recent_events = [
|
||||||
|
{
|
||||||
|
"kind": event.get("kind"),
|
||||||
|
"message": event.get("message"),
|
||||||
|
"created_at": event.get("created_at"),
|
||||||
|
}
|
||||||
|
for event in (plan.get("events") or [])[:8]
|
||||||
|
]
|
||||||
|
payload = {
|
||||||
|
"plan_id": plan.get("id"),
|
||||||
|
"title": plan.get("title"),
|
||||||
|
"kind": plan.get("kind"),
|
||||||
|
"objective": plan.get("objective"),
|
||||||
|
"constraints": plan.get("constraints") or {},
|
||||||
|
"items": plan.get("items") or [],
|
||||||
|
"recent_events": recent_events,
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
"Continual plan wake run. Continue this durable plan and write an Inbox-ready summary. "
|
||||||
|
"Use tools as needed. For any account-affecting marketplace write, only draft a pending action for approval. "
|
||||||
|
"Do not claim a message, offer, listing, or negotiation was sent unless an approved action result says it was sent. "
|
||||||
|
f"Plan JSON: {json.dumps(payload, ensure_ascii=True)}"
|
||||||
|
)
|
||||||
+88
-3
@@ -1,6 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ from apscheduler.triggers.date import DateTrigger
|
|||||||
from apscheduler.triggers.interval import IntervalTrigger
|
from apscheduler.triggers.interval import IntervalTrigger
|
||||||
from tzlocal import get_localzone
|
from tzlocal import get_localzone
|
||||||
|
|
||||||
from traderai.memory import MemoryStore, iso_now, time_since
|
from traderai.memory import MemoryStore, iso_now, parse_iso, time_since, utc_now
|
||||||
|
|
||||||
|
|
||||||
UEX_NOTIFICATION_JOB_ID = "uex-notification-poll"
|
UEX_NOTIFICATION_JOB_ID = "uex-notification-poll"
|
||||||
@@ -22,11 +22,15 @@ class WakeScheduler:
|
|||||||
self.scheduler = AsyncIOScheduler(timezone=get_localzone())
|
self.scheduler = AsyncIOScheduler(timezone=get_localzone())
|
||||||
self.agent = None
|
self.agent = None
|
||||||
self.uex = None
|
self.uex = None
|
||||||
|
self.plan_runner = None
|
||||||
self.notification_poll_seconds = 60
|
self.notification_poll_seconds = 60
|
||||||
|
|
||||||
def bind_agent(self, agent: Any) -> None:
|
def bind_agent(self, agent: Any) -> None:
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
|
|
||||||
|
def bind_plan_runner(self, plan_runner: Any) -> None:
|
||||||
|
self.plan_runner = plan_runner
|
||||||
|
|
||||||
def bind_uex_notifications(self, uex: Any, poll_seconds: int = 60) -> None:
|
def bind_uex_notifications(self, uex: Any, poll_seconds: int = 60) -> None:
|
||||||
self.uex = uex
|
self.uex = uex
|
||||||
self.notification_poll_seconds = max(15, poll_seconds)
|
self.notification_poll_seconds = max(15, poll_seconds)
|
||||||
@@ -37,6 +41,9 @@ class WakeScheduler:
|
|||||||
self._schedule_notification_poll()
|
self._schedule_notification_poll()
|
||||||
for job in self.memory.list_jobs():
|
for job in self.memory.list_jobs():
|
||||||
self._schedule_existing(job)
|
self._schedule_existing(job)
|
||||||
|
if self.plan_runner is not None:
|
||||||
|
for plan in self.plan_runner.store.list_plans(include_inactive=False):
|
||||||
|
self.schedule_plan(plan)
|
||||||
|
|
||||||
def shutdown(self) -> None:
|
def shutdown(self) -> None:
|
||||||
if self.scheduler.running:
|
if self.scheduler.running:
|
||||||
@@ -59,6 +66,70 @@ class WakeScheduler:
|
|||||||
def list_jobs(self) -> list[dict[str, Any]]:
|
def list_jobs(self) -> list[dict[str, Any]]:
|
||||||
return self.memory.list_jobs()
|
return self.memory.list_jobs()
|
||||||
|
|
||||||
|
def schedule_plan(self, plan: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
if self.plan_runner is None or plan.get("status") != "active":
|
||||||
|
return plan
|
||||||
|
job_id = self._plan_job_id(plan["id"])
|
||||||
|
previous_next_run = plan.get("next_run_at")
|
||||||
|
trigger = CronTrigger.from_crontab(plan.get("cadence") or "0 */6 * * *")
|
||||||
|
self.scheduler.add_job(self._run_plan, trigger=trigger, id=job_id, args=[plan["id"]], replace_existing=True)
|
||||||
|
job = self.scheduler.get_job(job_id)
|
||||||
|
next_run = job.next_run_time if job else None
|
||||||
|
self.plan_runner.store.update_schedule(plan["id"], next_run.isoformat() if next_run else None)
|
||||||
|
if self._plan_is_overdue(previous_next_run):
|
||||||
|
catchup_id = self._plan_catchup_job_id(plan["id"])
|
||||||
|
self.scheduler.add_job(
|
||||||
|
self._run_plan,
|
||||||
|
trigger=DateTrigger(run_date=datetime.now() + timedelta(seconds=5)),
|
||||||
|
id=catchup_id,
|
||||||
|
args=[plan["id"]],
|
||||||
|
replace_existing=True,
|
||||||
|
)
|
||||||
|
self.plan_runner.store.add_event(
|
||||||
|
plan["id"],
|
||||||
|
"catchup_scheduled",
|
||||||
|
"Plan was overdue while the app was closed, so a one-time catch-up run was scheduled after startup.",
|
||||||
|
{"previous_next_run_at": previous_next_run},
|
||||||
|
)
|
||||||
|
return self.plan_runner.store.get_plan(plan["id"]) or plan
|
||||||
|
|
||||||
|
def unschedule_plan(self, plan_id: str) -> None:
|
||||||
|
job_id = self._plan_job_id(plan_id)
|
||||||
|
if self.scheduler.get_job(job_id):
|
||||||
|
self.scheduler.remove_job(job_id)
|
||||||
|
catchup_id = self._plan_catchup_job_id(plan_id)
|
||||||
|
if self.scheduler.get_job(catchup_id):
|
||||||
|
self.scheduler.remove_job(catchup_id)
|
||||||
|
if self.plan_runner is not None:
|
||||||
|
self.plan_runner.store.update_schedule(plan_id, None)
|
||||||
|
|
||||||
|
async def _run_plan(self, plan_id: str) -> None:
|
||||||
|
if self.plan_runner is None:
|
||||||
|
return
|
||||||
|
result = await self.plan_runner.run_plan(plan_id)
|
||||||
|
plan = result.get("plan") or self.plan_runner.store.get_plan(plan_id)
|
||||||
|
if plan and plan.get("status") == "active":
|
||||||
|
job = self.scheduler.get_job(self._plan_job_id(plan_id))
|
||||||
|
next_run = job.next_run_time if job else None
|
||||||
|
self.plan_runner.store.update_schedule(plan_id, next_run.isoformat() if next_run else None)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _plan_job_id(plan_id: str) -> str:
|
||||||
|
return f"continual-{plan_id}"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _plan_catchup_job_id(plan_id: str) -> str:
|
||||||
|
return f"continual-catchup-{plan_id}"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _plan_is_overdue(next_run_at: str | None) -> bool:
|
||||||
|
if not next_run_at:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
return parse_iso(next_run_at) <= utc_now()
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
def _schedule_existing(self, job: dict[str, Any]) -> None:
|
def _schedule_existing(self, job: dict[str, Any]) -> None:
|
||||||
if job["trigger_type"] == "cron":
|
if job["trigger_type"] == "cron":
|
||||||
trigger = CronTrigger.from_crontab(job["trigger_value"])
|
trigger = CronTrigger.from_crontab(job["trigger_value"])
|
||||||
@@ -83,11 +154,20 @@ class WakeScheduler:
|
|||||||
)
|
)
|
||||||
if self.agent is None:
|
if self.agent is None:
|
||||||
self.memory.add_outbox(wake_message)
|
self.memory.add_outbox(wake_message)
|
||||||
|
self._mark_job_finished(job_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
text = await self.agent.generate_wake_response(wake_message)
|
text = await self.agent.generate_wake_response(wake_message)
|
||||||
|
except Exception as exc:
|
||||||
|
text = f"Wake job failed: {exc}. Job instruction: {prompt}"
|
||||||
self.memory.add_outbox(text)
|
self.memory.add_outbox(text)
|
||||||
self.memory.mark_job_run(job_id)
|
self._mark_job_finished(job_id)
|
||||||
|
|
||||||
|
def _mark_job_finished(self, job_id: str) -> None:
|
||||||
|
job = self.scheduler.get_job(job_id)
|
||||||
|
next_run = job.next_run_time if job else None
|
||||||
|
self.memory.mark_job_run(job_id, next_run.isoformat() if next_run else None, enabled=bool(next_run))
|
||||||
|
|
||||||
def _schedule_notification_poll(self) -> None:
|
def _schedule_notification_poll(self) -> None:
|
||||||
if self.uex is None:
|
if self.uex is None:
|
||||||
@@ -104,7 +184,12 @@ class WakeScheduler:
|
|||||||
if self.uex is None:
|
if self.uex is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
try:
|
||||||
response = await self.uex.get_user_notifications()
|
response = await self.uex.get_user_notifications()
|
||||||
|
except Exception as exc:
|
||||||
|
self.memory.add_outbox(f"UEX notification poll failed: {exc}")
|
||||||
|
self.memory.set_profile("uex_last_notification_error", str(exc))
|
||||||
|
return []
|
||||||
notifications = response.get("notifications") or []
|
notifications = response.get("notifications") or []
|
||||||
pending = [item for item in notifications if not item.get("date_read")]
|
pending = [item for item in notifications if not item.get("date_read")]
|
||||||
profile = self.memory.get_profile()
|
profile = self.memory.get_profile()
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
class SCMDBError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class SCMDBClient:
|
||||||
|
def __init__(self, base_url: str = "https://scmdb.net") -> None:
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
self._versions: list[dict[str, Any]] | None = None
|
||||||
|
self._data_cache: dict[str, dict[str, Any]] = {}
|
||||||
|
|
||||||
|
async def list_versions(self) -> list[dict[str, Any]]:
|
||||||
|
if self._versions is not None:
|
||||||
|
return self._versions
|
||||||
|
body = await self._get_json("data/versions.json")
|
||||||
|
if not isinstance(body, list):
|
||||||
|
raise SCMDBError("SCMDB versions response was not a list.")
|
||||||
|
self._versions = [
|
||||||
|
item
|
||||||
|
for item in body
|
||||||
|
if isinstance(item, dict) and item.get("version") and item.get("file")
|
||||||
|
]
|
||||||
|
return self._versions
|
||||||
|
|
||||||
|
async def get_data(self, version: str | None = None, channel: str = "live") -> dict[str, Any]:
|
||||||
|
selected = await self.resolve_version(version=version, channel=channel)
|
||||||
|
cache_key = str(selected["version"])
|
||||||
|
if cache_key not in self._data_cache:
|
||||||
|
body = await self._get_json(f"data/{selected['file']}")
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise SCMDBError(f"SCMDB data for {cache_key} was not an object.")
|
||||||
|
self._data_cache[cache_key] = body
|
||||||
|
return self._data_cache[cache_key]
|
||||||
|
|
||||||
|
async def resolve_version(self, version: str | None = None, channel: str = "live") -> dict[str, Any]:
|
||||||
|
versions = await self.list_versions()
|
||||||
|
if not versions:
|
||||||
|
raise SCMDBError("SCMDB did not return any data versions.")
|
||||||
|
|
||||||
|
if version:
|
||||||
|
needle = version.casefold().strip()
|
||||||
|
for item in versions:
|
||||||
|
item_version = str(item["version"])
|
||||||
|
if item_version.casefold() == needle or needle in item_version.casefold():
|
||||||
|
return item
|
||||||
|
raise SCMDBError(f"SCMDB version not found: {version}")
|
||||||
|
|
||||||
|
channel = (channel or "live").casefold().strip()
|
||||||
|
if channel in {"latest", "any", "all"}:
|
||||||
|
return versions[0]
|
||||||
|
if channel not in {"live", "ptu"}:
|
||||||
|
raise SCMDBError("SCMDB channel must be live, ptu, or latest.")
|
||||||
|
for item in versions:
|
||||||
|
if f"-{channel}." in str(item["version"]).casefold():
|
||||||
|
return item
|
||||||
|
return versions[0]
|
||||||
|
|
||||||
|
async def _get_json(self, path: str) -> Any:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(f"{self.base_url}/{path.lstrip('/')}", headers={"Accept": "application/json"})
|
||||||
|
try:
|
||||||
|
body = response.json()
|
||||||
|
except ValueError as exc:
|
||||||
|
raise SCMDBError(f"SCMDB returned non-JSON response: HTTP {response.status_code}") from exc
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise SCMDBError(f"SCMDB HTTP {response.status_code}: {body}")
|
||||||
|
return body
|
||||||
+798
-21
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -21,11 +22,16 @@ from pydantic import BaseModel
|
|||||||
from traderai.agent import OllamaAgent, OllamaUnavailable
|
from traderai.agent import OllamaAgent, OllamaUnavailable
|
||||||
from traderai.config import save_settings, settings_payload
|
from traderai.config import save_settings, settings_payload
|
||||||
from traderai.config import get_settings
|
from traderai.config import get_settings
|
||||||
from traderai.memory import MemoryStore
|
from traderai.cornerstone_client import CornerstoneClient
|
||||||
|
from traderai.memory import DEFAULT_THREAD_ID, MemoryStore
|
||||||
|
from traderai.plans import ContinualPlanRunner, ContinualPlanStore
|
||||||
from traderai.scheduler import WakeScheduler
|
from traderai.scheduler import WakeScheduler
|
||||||
|
from traderai.scmdb_client import SCMDBClient
|
||||||
|
from traderai.starcitizen_wiki_client import StarCitizenWikiClient
|
||||||
from traderai.tools import ToolRegistry
|
from traderai.tools import ToolRegistry
|
||||||
from traderai.uex_client import UEXClient
|
from traderai.uex_client import UEXClient
|
||||||
from traderai.version import RELEASES_API_URL, RELEASES_URL, __version__
|
from traderai.version import RELEASES_API_URL, RELEASES_URL, __version__
|
||||||
|
from traderai.wikelo_projects_client import WikeloProjectsClient
|
||||||
|
|
||||||
|
|
||||||
def resource_path(*parts: str) -> Path:
|
def resource_path(*parts: str) -> Path:
|
||||||
@@ -35,6 +41,26 @@ def resource_path(*parts: str) -> Path:
|
|||||||
|
|
||||||
class ChatRequest(BaseModel):
|
class ChatRequest(BaseModel):
|
||||||
message: str
|
message: str
|
||||||
|
thread_id: str | None = DEFAULT_THREAD_ID
|
||||||
|
images: list["ChatImageRequest"] = []
|
||||||
|
|
||||||
|
|
||||||
|
class ChatImageRequest(BaseModel):
|
||||||
|
name: str = "pasted-image.png"
|
||||||
|
content_type: str = "image/png"
|
||||||
|
image_data: str
|
||||||
|
|
||||||
|
|
||||||
|
class ChatThreadRequest(BaseModel):
|
||||||
|
title: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class RenameChatThreadRequest(BaseModel):
|
||||||
|
title: str
|
||||||
|
|
||||||
|
|
||||||
|
class DirectNegotiationMessageRequest(BaseModel):
|
||||||
|
message: str
|
||||||
|
|
||||||
|
|
||||||
class ClearMemoryRequest(BaseModel):
|
class ClearMemoryRequest(BaseModel):
|
||||||
@@ -45,6 +71,35 @@ class ClearMemoryRequest(BaseModel):
|
|||||||
include_outbox: bool = True
|
include_outbox: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanItemRequest(BaseModel):
|
||||||
|
item_name: str
|
||||||
|
desired_quantity: int = 1
|
||||||
|
max_unit_price: float | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanCreateRequest(BaseModel):
|
||||||
|
title: str
|
||||||
|
objective: str
|
||||||
|
kind: str = "buying"
|
||||||
|
cadence: str | None = None
|
||||||
|
constraints: dict[str, Any] = {}
|
||||||
|
items: list[ContinualPlanItemRequest] = []
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanDraftRequest(BaseModel):
|
||||||
|
title: str = ""
|
||||||
|
objective: str = ""
|
||||||
|
kind: str = "buying"
|
||||||
|
constraints: dict[str, Any] = {}
|
||||||
|
items: list[ContinualPlanItemRequest] = []
|
||||||
|
|
||||||
|
|
||||||
|
class ContinualPlanEventRequest(BaseModel):
|
||||||
|
kind: str = "note"
|
||||||
|
message: str
|
||||||
|
metadata: dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
class ConfigUpdateRequest(BaseModel):
|
class ConfigUpdateRequest(BaseModel):
|
||||||
values: dict
|
values: dict
|
||||||
|
|
||||||
@@ -60,19 +115,56 @@ UPDATE_ASSET_NAME = "TraderAI.exe"
|
|||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
memory = MemoryStore(settings.traderai_memory_path)
|
memory = MemoryStore(settings.traderai_memory_path)
|
||||||
|
plan_store = ContinualPlanStore(memory)
|
||||||
scheduler = WakeScheduler(memory)
|
scheduler = WakeScheduler(memory)
|
||||||
uex = UEXClient(settings.uex_base_url, settings.uex_secret_key, settings.uex_bearer_token)
|
runtime: dict[str, Any] = {}
|
||||||
tools = ToolRegistry(uex, settings.require_write_approval, memory=memory, scheduler=scheduler)
|
|
||||||
|
def configure_runtime(current_settings: Any) -> None:
|
||||||
|
uex = UEXClient(current_settings.uex_base_url, current_settings.uex_secret_key, current_settings.uex_bearer_token)
|
||||||
|
scmdb = SCMDBClient(current_settings.scmdb_base_url)
|
||||||
|
cornerstone = CornerstoneClient(current_settings.cornerstone_base_url)
|
||||||
|
scwiki = StarCitizenWikiClient(current_settings.scwiki_base_url, current_settings.scwiki_api_base_url)
|
||||||
|
wikelo = WikeloProjectsClient()
|
||||||
|
tools = ToolRegistry(
|
||||||
|
uex,
|
||||||
|
current_settings.require_write_approval,
|
||||||
|
memory=memory,
|
||||||
|
scheduler=scheduler,
|
||||||
|
scmdb=scmdb,
|
||||||
|
cornerstone=cornerstone,
|
||||||
|
scwiki=scwiki,
|
||||||
|
wikelo=wikelo,
|
||||||
|
plan_store=plan_store,
|
||||||
|
)
|
||||||
|
plan_runner = ContinualPlanRunner(plan_store, tools, memory)
|
||||||
|
tools.plan_runner = plan_runner
|
||||||
|
provider_base_url, provider_model, provider_api_key = provider_settings(current_settings)
|
||||||
agent = OllamaAgent(
|
agent = OllamaAgent(
|
||||||
settings.ollama_base_url,
|
provider_base_url,
|
||||||
settings.ollama_model,
|
provider_model,
|
||||||
tools,
|
tools,
|
||||||
memory=memory,
|
memory=memory,
|
||||||
user_name=settings.traderai_user_name,
|
user_name=current_settings.traderai_user_name,
|
||||||
num_ctx=settings.ollama_num_ctx,
|
num_ctx=current_settings.ollama_num_ctx,
|
||||||
|
provider=current_settings.model_provider,
|
||||||
|
api_key=provider_api_key,
|
||||||
|
reasoning_effort=current_settings.model_reasoning_effort,
|
||||||
)
|
)
|
||||||
|
plan_runner.bind_agent(agent)
|
||||||
scheduler.bind_agent(agent)
|
scheduler.bind_agent(agent)
|
||||||
scheduler.bind_uex_notifications(uex, settings.uex_notification_poll_seconds)
|
scheduler.bind_plan_runner(plan_runner)
|
||||||
|
scheduler.bind_uex_notifications(uex, current_settings.uex_notification_poll_seconds)
|
||||||
|
runtime.update(
|
||||||
|
{
|
||||||
|
"settings": current_settings,
|
||||||
|
"uex": uex,
|
||||||
|
"tools": tools,
|
||||||
|
"plan_runner": plan_runner,
|
||||||
|
"agent": agent,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
configure_runtime(settings)
|
||||||
|
|
||||||
app = FastAPI(title="TraderAI")
|
app = FastAPI(title="TraderAI")
|
||||||
static_dir = resource_path("web")
|
static_dir = resource_path("web")
|
||||||
@@ -88,17 +180,20 @@ def create_app() -> FastAPI:
|
|||||||
scheduler.shutdown()
|
scheduler.shutdown()
|
||||||
|
|
||||||
async def refresh_user_profile() -> None:
|
async def refresh_user_profile() -> None:
|
||||||
if settings.traderai_user_name:
|
current_settings = get_settings()
|
||||||
memory.set_profile("configured_name", settings.traderai_user_name)
|
agent = runtime["agent"]
|
||||||
agent.user_name = agent.user_name or settings.traderai_user_name
|
uex = runtime["uex"]
|
||||||
|
if current_settings.traderai_user_name:
|
||||||
|
memory.set_profile("configured_name", current_settings.traderai_user_name)
|
||||||
|
agent.user_name = agent.user_name or current_settings.traderai_user_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = await uex.get_user(authenticated=True)
|
response = await uex.get_user(authenticated=True)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
memory.set_profile("uex_user_error", str(exc))
|
memory.set_profile("uex_user_error", str(exc))
|
||||||
if settings.traderai_user_name:
|
if current_settings.traderai_user_name:
|
||||||
try:
|
try:
|
||||||
response = await uex.get_user(username=settings.traderai_user_name)
|
response = await uex.get_user(username=current_settings.traderai_user_name)
|
||||||
except Exception:
|
except Exception:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
@@ -117,8 +212,13 @@ def create_app() -> FastAPI:
|
|||||||
|
|
||||||
@app.get("/api/health")
|
@app.get("/api/health")
|
||||||
async def health() -> dict:
|
async def health() -> dict:
|
||||||
|
agent = runtime["agent"]
|
||||||
|
current_settings = get_settings()
|
||||||
|
inference = await agent.health()
|
||||||
return {
|
return {
|
||||||
"ollama": await agent.health(),
|
"inference": inference,
|
||||||
|
"ollama": inference,
|
||||||
|
"model_provider": current_settings.model_provider,
|
||||||
"user": memory.get_profile(),
|
"user": memory.get_profile(),
|
||||||
"jobs": scheduler.list_jobs(),
|
"jobs": scheduler.list_jobs(),
|
||||||
"app_data_dir": settings_payload()["app_data_dir"],
|
"app_data_dir": settings_payload()["app_data_dir"],
|
||||||
@@ -131,14 +231,61 @@ def create_app() -> FastAPI:
|
|||||||
|
|
||||||
@app.post("/api/config")
|
@app.post("/api/config")
|
||||||
async def update_config(request: ConfigUpdateRequest) -> dict:
|
async def update_config(request: ConfigUpdateRequest) -> dict:
|
||||||
|
previous_settings = get_settings()
|
||||||
updated = save_settings(request.values)
|
updated = save_settings(request.values)
|
||||||
updated["restart_required"] = True
|
current_settings = get_settings()
|
||||||
updated["message"] = "Configuration saved. Restart TraderAI for all settings to take effect."
|
configure_runtime(current_settings)
|
||||||
|
await refresh_user_profile()
|
||||||
|
restart_required = (
|
||||||
|
"traderai_memory_path" in request.values
|
||||||
|
and str(request.values.get("traderai_memory_path") or "").strip() != str(previous_settings.traderai_memory_path)
|
||||||
|
)
|
||||||
|
updated["restart_required"] = restart_required
|
||||||
|
updated["message"] = (
|
||||||
|
"Configuration saved. Restart TraderAI to switch memory databases."
|
||||||
|
if restart_required
|
||||||
|
else "Configuration saved and applied."
|
||||||
|
)
|
||||||
return updated
|
return updated
|
||||||
|
|
||||||
@app.get("/api/ollama/status")
|
@app.get("/api/ollama/status")
|
||||||
async def ollama_status() -> dict:
|
async def ollama_status() -> dict:
|
||||||
return await inspect_ollama()
|
return await inspect_model_provider()
|
||||||
|
|
||||||
|
@app.get("/api/provider/models")
|
||||||
|
async def provider_models(provider: str | None = None) -> dict:
|
||||||
|
status = await inspect_provider_models(provider)
|
||||||
|
return {
|
||||||
|
"provider": status.get("provider", "openai"),
|
||||||
|
"configured_model": status.get("configured_model"),
|
||||||
|
"models": status.get("models", []),
|
||||||
|
"reasoning_efforts": status.get("reasoning_efforts", reasoning_effort_options()),
|
||||||
|
"configured_reasoning_effort": status.get("configured_reasoning_effort", get_settings().model_reasoning_effort),
|
||||||
|
"message": status.get("message", ""),
|
||||||
|
"detail": status.get("detail", ""),
|
||||||
|
"online": status.get("online", False),
|
||||||
|
}
|
||||||
|
|
||||||
|
@app.post("/api/codex/login")
|
||||||
|
async def launch_codex_login() -> dict:
|
||||||
|
current_settings = get_settings()
|
||||||
|
command = find_codex_cli(current_settings.codex_command)
|
||||||
|
if not command:
|
||||||
|
raise HTTPException(status_code=404, detail="Codex CLI was not found on PATH.")
|
||||||
|
try:
|
||||||
|
login = await start_codex_browser_login(command)
|
||||||
|
except Exception as exc:
|
||||||
|
raise HTTPException(status_code=500, detail=f"Codex App Server login failed: {exception_detail(exc)}") from exc
|
||||||
|
return {
|
||||||
|
"installed": True,
|
||||||
|
"running": False,
|
||||||
|
"online": False,
|
||||||
|
"provider": "codex",
|
||||||
|
"login_id": login.get("loginId"),
|
||||||
|
"auth_url": login.get("authUrl"),
|
||||||
|
"base_url": str(command),
|
||||||
|
"message": "Opened Codex App Server sign-in in your browser. Finish the flow, then TraderAI will detect the new login.",
|
||||||
|
}
|
||||||
|
|
||||||
@app.post("/api/ollama/launch")
|
@app.post("/api/ollama/launch")
|
||||||
async def launch_ollama() -> dict:
|
async def launch_ollama() -> dict:
|
||||||
@@ -149,7 +296,7 @@ def create_app() -> FastAPI:
|
|||||||
popen_hidden(command)
|
popen_hidden(command)
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
raise HTTPException(status_code=500, detail=f"Could not launch Ollama: {exc}") from exc
|
raise HTTPException(status_code=500, detail=f"Could not launch Ollama: {exc}") from exc
|
||||||
status = await inspect_ollama()
|
status = await inspect_model_provider()
|
||||||
status["message"] = "Ollama launch requested."
|
status["message"] = "Ollama launch requested."
|
||||||
return status
|
return status
|
||||||
|
|
||||||
@@ -166,7 +313,7 @@ def create_app() -> FastAPI:
|
|||||||
popen_hidden([str(cli), "pull", model])
|
popen_hidden([str(cli), "pull", model])
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
raise HTTPException(status_code=500, detail=f"Could not start model install: {exc}") from exc
|
raise HTTPException(status_code=500, detail=f"Could not start model install: {exc}") from exc
|
||||||
status = await inspect_ollama()
|
status = await inspect_model_provider()
|
||||||
status["message"] = f"Started installing model {model}."
|
status["message"] = f"Started installing model {model}."
|
||||||
return status
|
return status
|
||||||
|
|
||||||
@@ -245,31 +392,184 @@ def create_app() -> FastAPI:
|
|||||||
|
|
||||||
@app.post("/api/chat")
|
@app.post("/api/chat")
|
||||||
async def chat(request: ChatRequest) -> dict:
|
async def chat(request: ChatRequest) -> dict:
|
||||||
|
agent = runtime["agent"]
|
||||||
try:
|
try:
|
||||||
return await agent.chat(request.message)
|
return await agent.chat(
|
||||||
|
request.message,
|
||||||
|
thread_id=request.thread_id,
|
||||||
|
images=[image.model_dump() for image in request.images],
|
||||||
|
)
|
||||||
except OllamaUnavailable as exc:
|
except OllamaUnavailable as exc:
|
||||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||||
|
|
||||||
@app.post("/api/chat/stream")
|
@app.post("/api/chat/stream")
|
||||||
async def chat_stream(request: ChatRequest) -> StreamingResponse:
|
async def chat_stream(request: ChatRequest) -> StreamingResponse:
|
||||||
|
agent = runtime["agent"]
|
||||||
|
|
||||||
async def events():
|
async def events():
|
||||||
async for event in agent.chat_events(request.message):
|
async for event in agent.chat_events(
|
||||||
|
request.message,
|
||||||
|
thread_id=request.thread_id,
|
||||||
|
images=[image.model_dump() for image in request.images],
|
||||||
|
):
|
||||||
yield f"data: {json.dumps(event)}\n\n"
|
yield f"data: {json.dumps(event)}\n\n"
|
||||||
|
|
||||||
return StreamingResponse(events(), media_type="text/event-stream")
|
return StreamingResponse(events(), media_type="text/event-stream")
|
||||||
|
|
||||||
|
@app.get("/api/chats")
|
||||||
|
async def chats() -> dict:
|
||||||
|
return {"chats": memory.list_threads()}
|
||||||
|
|
||||||
|
@app.post("/api/chats")
|
||||||
|
async def create_chat(request: ChatThreadRequest) -> dict:
|
||||||
|
return {"chat": memory.create_thread(request.title)}
|
||||||
|
|
||||||
|
@app.get("/api/chats/{thread_id}/messages")
|
||||||
|
async def chat_messages(thread_id: str) -> dict:
|
||||||
|
memory.ensure_thread(thread_id)
|
||||||
|
return {"thread_id": thread_id, "messages": memory.recent_conversation(limit=200, thread_id=thread_id)}
|
||||||
|
|
||||||
|
@app.delete("/api/chats/{thread_id}")
|
||||||
|
async def delete_chat(thread_id: str) -> dict:
|
||||||
|
deleted = memory.delete_thread(thread_id)
|
||||||
|
return {"deleted": deleted, "chats": memory.list_threads()}
|
||||||
|
|
||||||
|
@app.patch("/api/chats/{thread_id}")
|
||||||
|
async def rename_chat(thread_id: str, request: RenameChatThreadRequest) -> dict:
|
||||||
|
chat = memory.rename_thread(thread_id, request.title)
|
||||||
|
if not chat:
|
||||||
|
raise HTTPException(status_code=400, detail="A non-empty chat title is required.")
|
||||||
|
return {"chat": chat, "chats": memory.list_threads()}
|
||||||
|
|
||||||
@app.get("/api/pending-actions")
|
@app.get("/api/pending-actions")
|
||||||
async def pending_actions() -> dict:
|
async def pending_actions() -> dict:
|
||||||
|
agent = runtime["agent"]
|
||||||
return {"pending_actions": agent._pending_payloads()}
|
return {"pending_actions": agent._pending_payloads()}
|
||||||
|
|
||||||
@app.get("/api/notifications")
|
@app.get("/api/notifications")
|
||||||
async def notifications() -> dict:
|
async def notifications() -> dict:
|
||||||
return {"notifications": memory.undelivered_outbox()}
|
return {"notifications": memory.undelivered_outbox()}
|
||||||
|
|
||||||
|
@app.get("/api/inbox")
|
||||||
|
async def inbox() -> dict:
|
||||||
|
return {"inbox": memory.list_outbox()}
|
||||||
|
|
||||||
|
@app.post("/api/inbox/{inbox_id}/continue")
|
||||||
|
async def continue_inbox(inbox_id: int) -> dict:
|
||||||
|
item = memory.get_outbox(inbox_id)
|
||||||
|
if not item:
|
||||||
|
raise HTTPException(status_code=404, detail="Inbox item not found.")
|
||||||
|
thread = memory.create_thread("Inbox follow-up")
|
||||||
|
memory.add_conversation("assistant", item["content"], thread["id"])
|
||||||
|
return {"chat": thread, "message": item}
|
||||||
|
|
||||||
|
@app.delete("/api/inbox/{inbox_id}")
|
||||||
|
async def delete_inbox(inbox_id: int) -> dict:
|
||||||
|
deleted = memory.delete_outbox(inbox_id)
|
||||||
|
return {"deleted": deleted, "inbox": memory.list_outbox()}
|
||||||
|
|
||||||
|
@app.get("/api/negotiations/{identifier}/messages")
|
||||||
|
async def negotiation_messages(identifier: str) -> dict:
|
||||||
|
uex = runtime["uex"]
|
||||||
|
params = negotiation_identifier_params(identifier)
|
||||||
|
return await uex.get("marketplace_negotiations_messages", params, authenticated=True)
|
||||||
|
|
||||||
|
@app.post("/api/negotiations/{identifier}/messages")
|
||||||
|
async def send_negotiation_message(identifier: str, request: DirectNegotiationMessageRequest) -> dict:
|
||||||
|
uex = runtime["uex"]
|
||||||
|
params = negotiation_identifier_params(identifier)
|
||||||
|
payload = {**params, "message": request.message, "is_production": 1}
|
||||||
|
return await uex.post("marketplace_negotiations_messages", payload, authenticated=True)
|
||||||
|
|
||||||
@app.get("/api/wake-jobs")
|
@app.get("/api/wake-jobs")
|
||||||
async def wake_jobs() -> dict:
|
async def wake_jobs() -> dict:
|
||||||
return {"scheduled_jobs": scheduler.list_jobs()}
|
return {"scheduled_jobs": scheduler.list_jobs()}
|
||||||
|
|
||||||
|
@app.get("/api/plans")
|
||||||
|
async def continual_plans(include_inactive: bool = True) -> dict:
|
||||||
|
return {"plans": plan_store.list_plans(include_inactive=include_inactive)}
|
||||||
|
|
||||||
|
@app.post("/api/plans")
|
||||||
|
async def create_continual_plan(request: ContinualPlanCreateRequest) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.create_continual_plan(
|
||||||
|
title=request.title,
|
||||||
|
objective=request.objective,
|
||||||
|
kind=request.kind,
|
||||||
|
items=[item.model_dump() for item in request.items],
|
||||||
|
constraints=request.constraints,
|
||||||
|
cadence=request.cadence,
|
||||||
|
)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=400, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.post("/api/plans/draft")
|
||||||
|
async def draft_continual_plan(request: ContinualPlanDraftRequest) -> dict:
|
||||||
|
agent = runtime["agent"]
|
||||||
|
draft = await agent.generate_plan_draft(
|
||||||
|
title=request.title,
|
||||||
|
objective=request.objective,
|
||||||
|
kind=request.kind,
|
||||||
|
constraints=request.constraints,
|
||||||
|
items=[item.model_dump() for item in request.items],
|
||||||
|
)
|
||||||
|
return {"draft": draft}
|
||||||
|
|
||||||
|
@app.get("/api/plans/{plan_id}")
|
||||||
|
async def continual_plan(plan_id: str) -> dict:
|
||||||
|
plan = plan_store.get_plan(plan_id)
|
||||||
|
if not plan:
|
||||||
|
raise HTTPException(status_code=404, detail="Plan not found.")
|
||||||
|
return {"plan": plan}
|
||||||
|
|
||||||
|
@app.post("/api/plans/{plan_id}/pause")
|
||||||
|
async def pause_continual_plan(plan_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.pause_continual_plan(plan_id)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=404, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.post("/api/plans/{plan_id}/resume")
|
||||||
|
async def resume_continual_plan(plan_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.resume_continual_plan(plan_id)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=404, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.post("/api/plans/{plan_id}/cancel")
|
||||||
|
async def cancel_continual_plan(plan_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.cancel_continual_plan(plan_id)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=404, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.delete("/api/plans/{plan_id}")
|
||||||
|
async def delete_continual_plan(plan_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.delete_continual_plan(plan_id)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=404, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.post("/api/plans/{plan_id}/run")
|
||||||
|
async def run_continual_plan(plan_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
|
result = await tools.run_continual_plan_now(plan_id)
|
||||||
|
if result.get("error"):
|
||||||
|
raise HTTPException(status_code=400, detail=result["error"])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@app.post("/api/plans/{plan_id}/events")
|
||||||
|
async def add_continual_plan_event(plan_id: str, request: ContinualPlanEventRequest) -> dict:
|
||||||
|
if not plan_store.get_plan(plan_id):
|
||||||
|
raise HTTPException(status_code=404, detail="Plan not found.")
|
||||||
|
event = plan_store.add_event(plan_id, request.kind, request.message, request.metadata)
|
||||||
|
return {"event": event, "plan": plan_store.get_plan(plan_id)}
|
||||||
|
|
||||||
@app.get("/api/memory")
|
@app.get("/api/memory")
|
||||||
async def inspect_memory(limit: int = 50) -> dict:
|
async def inspect_memory(limit: int = 50) -> dict:
|
||||||
return memory.inspect(max(1, min(limit, 200)))
|
return memory.inspect(max(1, min(limit, 200)))
|
||||||
@@ -291,15 +591,161 @@ def create_app() -> FastAPI:
|
|||||||
|
|
||||||
@app.post("/api/approve/{action_id}")
|
@app.post("/api/approve/{action_id}")
|
||||||
async def approve(action_id: str) -> dict:
|
async def approve(action_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
return await tools.approve(action_id)
|
return await tools.approve(action_id)
|
||||||
|
|
||||||
@app.post("/api/decline/{action_id}")
|
@app.post("/api/decline/{action_id}")
|
||||||
async def decline(action_id: str) -> dict:
|
async def decline(action_id: str) -> dict:
|
||||||
|
tools = runtime["tools"]
|
||||||
return await tools.decline(action_id)
|
return await tools.decline(action_id)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
def negotiation_identifier_params(identifier: str) -> dict[str, Any]:
|
||||||
|
value = identifier.strip()
|
||||||
|
if not value:
|
||||||
|
raise HTTPException(status_code=400, detail="Negotiation id or hash is required.")
|
||||||
|
if value.isdigit():
|
||||||
|
return {"id_negotiation": int(value)}
|
||||||
|
return {"hash": value}
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_model_provider() -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
if settings.model_provider == "openai":
|
||||||
|
return await inspect_openai()
|
||||||
|
if settings.model_provider == "deepseek":
|
||||||
|
return await inspect_deepseek()
|
||||||
|
if settings.model_provider == "codex":
|
||||||
|
return await inspect_codex()
|
||||||
|
return await inspect_ollama()
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_openai() -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
return await inspect_cloud_provider_config("openai", settings.openai_base_url, settings.openai_api_key, settings.openai_model)
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_deepseek() -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
return await inspect_cloud_provider_config(
|
||||||
|
"deepseek",
|
||||||
|
settings.deepseek_base_url,
|
||||||
|
settings.deepseek_api_key,
|
||||||
|
settings.deepseek_model,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_codex() -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
command = find_codex_cli(settings.codex_command)
|
||||||
|
detail = ""
|
||||||
|
online = False
|
||||||
|
models: list[str] = []
|
||||||
|
effort_map: dict[str, list[str]] = {}
|
||||||
|
if command:
|
||||||
|
try:
|
||||||
|
account, models, effort_map = await inspect_codex_app_server(command)
|
||||||
|
online = bool(account)
|
||||||
|
detail = f"Logged in as {account.get('email')}" if isinstance(account, dict) and account.get("email") else ""
|
||||||
|
except (OSError, RuntimeError, asyncio.TimeoutError) as exc:
|
||||||
|
detail = str(exc)
|
||||||
|
configured_model = settings.codex_model
|
||||||
|
model_available = configured_model in models if models else bool(configured_model)
|
||||||
|
return {
|
||||||
|
"installed": bool(command),
|
||||||
|
"running": online,
|
||||||
|
"online": online,
|
||||||
|
"provider": "codex",
|
||||||
|
"model_available": model_available,
|
||||||
|
"configured_model": configured_model,
|
||||||
|
"configured_reasoning_effort": settings.model_reasoning_effort,
|
||||||
|
"reasoning_efforts": codex_reasoning_efforts(configured_model, effort_map),
|
||||||
|
"base_url": str(command) if command else settings.codex_command,
|
||||||
|
"models": models,
|
||||||
|
"message": codex_status_message(bool(command), online, model_available, configured_model),
|
||||||
|
"detail": detail,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_cloud_provider() -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
if settings.model_provider == "codex":
|
||||||
|
return await inspect_codex()
|
||||||
|
if settings.model_provider == "deepseek":
|
||||||
|
return await inspect_deepseek()
|
||||||
|
return await inspect_openai()
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_provider_models(provider: str | None = None) -> dict[str, Any]:
|
||||||
|
normalized = str(provider or get_settings().model_provider).strip().casefold()
|
||||||
|
if normalized == "codex":
|
||||||
|
return await inspect_codex()
|
||||||
|
if normalized == "ollama":
|
||||||
|
return await inspect_ollama()
|
||||||
|
if normalized == "deepseek":
|
||||||
|
return await inspect_deepseek()
|
||||||
|
return await inspect_openai()
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_cloud_provider_config(
|
||||||
|
provider: str,
|
||||||
|
base_url: str,
|
||||||
|
api_key: str | None,
|
||||||
|
model: str,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
settings = get_settings()
|
||||||
|
models: list[str] = []
|
||||||
|
online = False
|
||||||
|
detail = ""
|
||||||
|
provider_name = provider_display_name(provider)
|
||||||
|
if not api_key:
|
||||||
|
return {
|
||||||
|
"installed": True,
|
||||||
|
"running": False,
|
||||||
|
"online": False,
|
||||||
|
"provider": provider,
|
||||||
|
"model_available": False,
|
||||||
|
"configured_model": model,
|
||||||
|
"configured_reasoning_effort": canonical_provider_reasoning_effort(provider, settings.model_reasoning_effort),
|
||||||
|
"reasoning_efforts": provider_reasoning_efforts(provider, model),
|
||||||
|
"base_url": base_url,
|
||||||
|
"models": [],
|
||||||
|
"message": f"{provider_name} is selected, but no API key is configured.",
|
||||||
|
"detail": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
response = await client.get(
|
||||||
|
f"{base_url.rstrip('/')}/models",
|
||||||
|
headers={"Authorization": f"Bearer {api_key}"},
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
body = response.json()
|
||||||
|
online = True
|
||||||
|
models = sorted(item.get("id") for item in body.get("data", []) if item.get("id"))
|
||||||
|
except (httpx.HTTPError, ValueError) as exc:
|
||||||
|
detail = str(exc)
|
||||||
|
|
||||||
|
model_available = model in models
|
||||||
|
return {
|
||||||
|
"installed": True,
|
||||||
|
"running": online,
|
||||||
|
"online": online,
|
||||||
|
"provider": provider,
|
||||||
|
"model_available": model_available,
|
||||||
|
"configured_model": model,
|
||||||
|
"configured_reasoning_effort": canonical_provider_reasoning_effort(provider, settings.model_reasoning_effort),
|
||||||
|
"reasoning_efforts": provider_reasoning_efforts(provider, model),
|
||||||
|
"base_url": base_url,
|
||||||
|
"models": models,
|
||||||
|
"message": cloud_status_message(provider, online, bool(api_key), model_available, model),
|
||||||
|
"detail": detail,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def inspect_ollama() -> dict[str, Any]:
|
async def inspect_ollama() -> dict[str, Any]:
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
executable = find_ollama_executable()
|
executable = find_ollama_executable()
|
||||||
@@ -325,8 +771,11 @@ async def inspect_ollama() -> dict[str, Any]:
|
|||||||
"installed": installed,
|
"installed": installed,
|
||||||
"running": online,
|
"running": online,
|
||||||
"online": online,
|
"online": online,
|
||||||
|
"provider": "ollama",
|
||||||
"model_available": model_available,
|
"model_available": model_available,
|
||||||
"configured_model": settings.ollama_model,
|
"configured_model": settings.ollama_model,
|
||||||
|
"configured_reasoning_effort": settings.model_reasoning_effort,
|
||||||
|
"reasoning_efforts": reasoning_effort_options(),
|
||||||
"base_url": settings.ollama_base_url,
|
"base_url": settings.ollama_base_url,
|
||||||
"num_ctx": settings.ollama_num_ctx,
|
"num_ctx": settings.ollama_num_ctx,
|
||||||
"models": models,
|
"models": models,
|
||||||
@@ -339,6 +788,17 @@ async def inspect_ollama() -> dict[str, Any]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def cloud_status_message(provider: str, running: bool, configured: bool, model_available: bool, model: str) -> str:
|
||||||
|
provider_name = provider_display_name(provider)
|
||||||
|
if not configured:
|
||||||
|
return f"{provider_name} API key is not configured."
|
||||||
|
if not running:
|
||||||
|
return f"{provider_name} is not reachable with the configured key."
|
||||||
|
if not model_available:
|
||||||
|
return f'{provider_name} is reachable, but model "{model}" was not returned by the API.'
|
||||||
|
return f"{provider_name} is ready."
|
||||||
|
|
||||||
|
|
||||||
def ollama_status_message(installed: bool, running: bool, model_available: bool, model: str) -> str:
|
def ollama_status_message(installed: bool, running: bool, model_available: bool, model: str) -> str:
|
||||||
if not installed:
|
if not installed:
|
||||||
return "Ollama is not installed."
|
return "Ollama is not installed."
|
||||||
@@ -349,6 +809,316 @@ def ollama_status_message(installed: bool, running: bool, model_available: bool,
|
|||||||
return "Ollama is ready."
|
return "Ollama is ready."
|
||||||
|
|
||||||
|
|
||||||
|
def codex_status_message(installed: bool, logged_in: bool, model_available: bool, model: str) -> str:
|
||||||
|
if not installed:
|
||||||
|
return "Codex CLI is not installed."
|
||||||
|
if not logged_in:
|
||||||
|
return "Codex CLI is installed, but the Codex App Server is not logged in with ChatGPT."
|
||||||
|
if not model_available:
|
||||||
|
return f'Codex App Server is logged in, but model "{model}" was not returned by the model list.'
|
||||||
|
return "Codex App Server is ready."
|
||||||
|
|
||||||
|
|
||||||
|
def provider_settings(settings: Any) -> tuple[str, str, str | None]:
|
||||||
|
if settings.model_provider == "openai":
|
||||||
|
return settings.openai_base_url, settings.openai_model, settings.openai_api_key
|
||||||
|
if settings.model_provider == "deepseek":
|
||||||
|
return settings.deepseek_base_url, settings.deepseek_model, settings.deepseek_api_key
|
||||||
|
if settings.model_provider == "codex":
|
||||||
|
return settings.codex_command, settings.codex_model, None
|
||||||
|
return settings.ollama_base_url, settings.ollama_model, None
|
||||||
|
|
||||||
|
|
||||||
|
def provider_display_name(provider: str) -> str:
|
||||||
|
return {"openai": "OpenAI", "deepseek": "DeepSeek", "codex": "Codex"}.get(provider, "Ollama")
|
||||||
|
|
||||||
|
|
||||||
|
def find_codex_cli(configured_command: str | None = None) -> Path | None:
|
||||||
|
candidates = [configured_command, shutil.which("codex"), os.path.join(os.environ.get("USERPROFILE", ""), ".codex", ".sandbox-bin", "codex.exe")]
|
||||||
|
for candidate in candidates:
|
||||||
|
if not candidate:
|
||||||
|
continue
|
||||||
|
resolved = shutil.which(candidate) if Path(candidate).name == candidate else candidate
|
||||||
|
if not resolved:
|
||||||
|
continue
|
||||||
|
path = Path(resolved)
|
||||||
|
if path.exists():
|
||||||
|
return path
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
_codex_login_tasks: set[asyncio.Task] = set()
|
||||||
|
|
||||||
|
|
||||||
|
async def start_codex_browser_login(command: Path) -> dict[str, Any]:
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
str(command),
|
||||||
|
"app-server",
|
||||||
|
stdin=asyncio.subprocess.PIPE,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
|
creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0,
|
||||||
|
)
|
||||||
|
request_id = 1
|
||||||
|
|
||||||
|
async def write(payload: dict[str, Any]) -> None:
|
||||||
|
if process.stdin is None:
|
||||||
|
raise RuntimeError("Codex App Server stdin is unavailable.")
|
||||||
|
process.stdin.write((json.dumps(payload, ensure_ascii=True) + "\n").encode("utf-8"))
|
||||||
|
await process.stdin.drain()
|
||||||
|
|
||||||
|
async def read(timeout: int = 30) -> dict[str, Any]:
|
||||||
|
if process.stdout is None:
|
||||||
|
raise RuntimeError("Codex App Server stdout is unavailable.")
|
||||||
|
try:
|
||||||
|
line = await asyncio.wait_for(process.stdout.readline(), timeout=timeout)
|
||||||
|
except asyncio.TimeoutError as exc:
|
||||||
|
raise RuntimeError("Codex App Server timed out while starting browser login.") from exc
|
||||||
|
if not line:
|
||||||
|
stderr = ""
|
||||||
|
if process.stderr is not None:
|
||||||
|
try:
|
||||||
|
stderr = (await asyncio.wait_for(process.stderr.read(), timeout=1)).decode("utf-8", errors="replace").strip()
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
stderr = ""
|
||||||
|
raise RuntimeError(stderr or "Codex App Server exited before login completed.")
|
||||||
|
return json.loads(line.decode("utf-8", errors="replace"))
|
||||||
|
|
||||||
|
async def send(method: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||||
|
nonlocal request_id
|
||||||
|
current_id = request_id
|
||||||
|
request_id += 1
|
||||||
|
payload: dict[str, Any] = {"jsonrpc": "2.0", "id": current_id, "method": method}
|
||||||
|
if params is not None:
|
||||||
|
payload["params"] = params
|
||||||
|
await write(payload)
|
||||||
|
while True:
|
||||||
|
message = await read()
|
||||||
|
if message.get("id") == current_id:
|
||||||
|
if message.get("error"):
|
||||||
|
error = message["error"]
|
||||||
|
raise RuntimeError(error.get("message") or f"Codex App Server request failed: {error}")
|
||||||
|
return message.get("result") or {}
|
||||||
|
await answer_codex_login_server_request(write, message)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await send(
|
||||||
|
"initialize",
|
||||||
|
{
|
||||||
|
"clientInfo": {"name": "TraderAI", "version": __version__},
|
||||||
|
"capabilities": {"experimentalApi": True},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await write({"jsonrpc": "2.0", "method": "initialized", "params": {}})
|
||||||
|
login = await send("account/login/start", {"type": "chatgpt"})
|
||||||
|
if login.get("type") != "chatgpt" or not login.get("authUrl"):
|
||||||
|
raise RuntimeError(f"Codex App Server did not return a browser login URL: {login!r}")
|
||||||
|
task = asyncio.create_task(watch_codex_browser_login(process, read, write, login.get("loginId")))
|
||||||
|
_codex_login_tasks.add(task)
|
||||||
|
task.add_done_callback(_codex_login_tasks.discard)
|
||||||
|
return login
|
||||||
|
except Exception:
|
||||||
|
await stop_process(process)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
async def answer_codex_login_server_request(write: Any, message: dict[str, Any]) -> None:
|
||||||
|
if "id" not in message or "method" not in message:
|
||||||
|
return
|
||||||
|
await write(
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": message["id"],
|
||||||
|
"error": {"code": -32601, "message": "TraderAI login does not handle server requests."},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def watch_codex_browser_login(process: asyncio.subprocess.Process, read: Any, write: Any, login_id: str | None) -> None:
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
message = await read(timeout=300)
|
||||||
|
if message.get("method") == "account/login/completed":
|
||||||
|
params = message.get("params") or {}
|
||||||
|
if login_id is None or params.get("loginId") == login_id:
|
||||||
|
return
|
||||||
|
await answer_codex_login_server_request(write, message)
|
||||||
|
except Exception:
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
await stop_process(process)
|
||||||
|
|
||||||
|
|
||||||
|
async def stop_process(process: asyncio.subprocess.Process) -> None:
|
||||||
|
if process.returncode is not None:
|
||||||
|
return
|
||||||
|
process.terminate()
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(process.wait(), timeout=3)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
process.kill()
|
||||||
|
await process.wait()
|
||||||
|
|
||||||
|
|
||||||
|
async def inspect_codex_app_server(command: Path) -> tuple[dict[str, Any] | None, list[str], dict[str, list[str]]]:
|
||||||
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
str(command),
|
||||||
|
"app-server",
|
||||||
|
stdin=asyncio.subprocess.PIPE,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
|
creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0,
|
||||||
|
)
|
||||||
|
request_id = 1
|
||||||
|
|
||||||
|
async def write(payload: dict[str, Any]) -> None:
|
||||||
|
if process.stdin is None:
|
||||||
|
raise RuntimeError("Codex App Server stdin is unavailable.")
|
||||||
|
process.stdin.write((json.dumps(payload, ensure_ascii=True) + "\n").encode("utf-8"))
|
||||||
|
await process.stdin.drain()
|
||||||
|
|
||||||
|
async def read(timeout: int = 30) -> dict[str, Any]:
|
||||||
|
if process.stdout is None:
|
||||||
|
raise RuntimeError("Codex App Server stdout is unavailable.")
|
||||||
|
line = await asyncio.wait_for(process.stdout.readline(), timeout=timeout)
|
||||||
|
if not line:
|
||||||
|
stderr = ""
|
||||||
|
if process.stderr is not None:
|
||||||
|
try:
|
||||||
|
stderr = (await asyncio.wait_for(process.stderr.read(), timeout=1)).decode("utf-8", errors="replace").strip()
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
stderr = ""
|
||||||
|
raise RuntimeError(stderr or "Codex App Server exited without a response.")
|
||||||
|
return json.loads(line.decode("utf-8", errors="replace"))
|
||||||
|
|
||||||
|
async def send(method: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||||
|
nonlocal request_id
|
||||||
|
current_id = request_id
|
||||||
|
request_id += 1
|
||||||
|
payload: dict[str, Any] = {"jsonrpc": "2.0", "id": current_id, "method": method}
|
||||||
|
if params is not None:
|
||||||
|
payload["params"] = params
|
||||||
|
await write(payload)
|
||||||
|
while True:
|
||||||
|
message = await read()
|
||||||
|
if message.get("id") == current_id:
|
||||||
|
if message.get("error"):
|
||||||
|
error = message["error"]
|
||||||
|
raise RuntimeError(error.get("message") or f"Codex App Server request failed: {error}")
|
||||||
|
return message.get("result") or {}
|
||||||
|
if "id" in message and "method" in message:
|
||||||
|
await write(
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": message["id"],
|
||||||
|
"error": {"code": -32601, "message": "TraderAI status checks do not handle server requests."},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await send(
|
||||||
|
"initialize",
|
||||||
|
{
|
||||||
|
"clientInfo": {"name": "TraderAI", "version": __version__},
|
||||||
|
"capabilities": {"experimentalApi": True},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await write({"jsonrpc": "2.0", "method": "initialized", "params": {}})
|
||||||
|
account_result = await send("account/read", {"refreshToken": False})
|
||||||
|
models: list[str] = []
|
||||||
|
effort_map: dict[str, list[str]] = {}
|
||||||
|
cursor: str | None = None
|
||||||
|
for _ in range(20):
|
||||||
|
params: dict[str, Any] = {"limit": 50, "includeHidden": False}
|
||||||
|
if cursor:
|
||||||
|
params["cursor"] = cursor
|
||||||
|
page = await send("model/list", params)
|
||||||
|
for item in page.get("data") or []:
|
||||||
|
model = item.get("id") or item.get("model")
|
||||||
|
if not model:
|
||||||
|
continue
|
||||||
|
models.append(model)
|
||||||
|
efforts = [
|
||||||
|
effort.get("reasoningEffort")
|
||||||
|
for effort in item.get("supportedReasoningEfforts", [])
|
||||||
|
if effort.get("reasoningEffort")
|
||||||
|
]
|
||||||
|
if efforts:
|
||||||
|
effort_map[model] = efforts
|
||||||
|
cursor = page.get("nextCursor")
|
||||||
|
if not cursor:
|
||||||
|
break
|
||||||
|
return account_result.get("account"), sorted(set(models)), effort_map
|
||||||
|
finally:
|
||||||
|
if process.returncode is None:
|
||||||
|
process.terminate()
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(process.wait(), timeout=3)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
process.kill()
|
||||||
|
await process.wait()
|
||||||
|
|
||||||
|
|
||||||
|
def codex_models() -> list[str]:
|
||||||
|
cache_path = Path.home() / ".codex" / "models_cache.json"
|
||||||
|
if not cache_path.exists():
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
body = json.loads(cache_path.read_text(encoding="utf-8"))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return []
|
||||||
|
models = []
|
||||||
|
for item in body.get("models", []):
|
||||||
|
slug = item.get("slug")
|
||||||
|
if slug:
|
||||||
|
models.append(slug)
|
||||||
|
return sorted(set(models))
|
||||||
|
|
||||||
|
|
||||||
|
def codex_reasoning_efforts(model: str, effort_map: dict[str, list[str]] | None = None) -> list[str]:
|
||||||
|
if effort_map and effort_map.get(model):
|
||||||
|
return effort_map[model]
|
||||||
|
cache_path = Path.home() / ".codex" / "models_cache.json"
|
||||||
|
if not cache_path.exists():
|
||||||
|
return reasoning_effort_options()
|
||||||
|
try:
|
||||||
|
body = json.loads(cache_path.read_text(encoding="utf-8"))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return reasoning_effort_options()
|
||||||
|
for item in body.get("models", []):
|
||||||
|
if item.get("slug") != model:
|
||||||
|
continue
|
||||||
|
efforts = [entry.get("effort") for entry in item.get("supported_reasoning_levels", []) if entry.get("effort")]
|
||||||
|
return efforts or reasoning_effort_options()
|
||||||
|
return reasoning_effort_options()
|
||||||
|
|
||||||
|
|
||||||
|
def reasoning_effort_options() -> list[str]:
|
||||||
|
return ["none", "minimal", "low", "medium", "high", "xhigh"]
|
||||||
|
|
||||||
|
|
||||||
|
def deepseek_reasoning_efforts(model: str) -> list[str]:
|
||||||
|
supported_models = {"deepseek-v4-flash", "deepseek-v4-pro", "deepseek-chat", "deepseek-reasoner"}
|
||||||
|
return ["none", "high", "max"] if model in supported_models else ["none", "high"]
|
||||||
|
|
||||||
|
|
||||||
|
def provider_reasoning_efforts(provider: str, model: str) -> list[str]:
|
||||||
|
if provider == "deepseek":
|
||||||
|
return deepseek_reasoning_efforts(model)
|
||||||
|
return reasoning_effort_options()
|
||||||
|
|
||||||
|
|
||||||
|
def canonical_provider_reasoning_effort(provider: str, effort: str) -> str:
|
||||||
|
normalized = str(effort or "medium").strip().casefold()
|
||||||
|
if provider != "deepseek":
|
||||||
|
return normalized
|
||||||
|
if normalized in {"none", "minimal"}:
|
||||||
|
return "none"
|
||||||
|
if normalized in {"xhigh", "max"}:
|
||||||
|
return "max"
|
||||||
|
return "high"
|
||||||
|
|
||||||
|
|
||||||
def find_ollama_executable() -> Path | None:
|
def find_ollama_executable() -> Path | None:
|
||||||
candidates = [
|
candidates = [
|
||||||
shutil.which("ollama"),
|
shutil.which("ollama"),
|
||||||
@@ -401,6 +1171,13 @@ def popen_hidden(command: list[str]) -> subprocess.Popen:
|
|||||||
return subprocess.Popen(command, **kwargs)
|
return subprocess.Popen(command, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def exception_detail(exc: BaseException) -> str:
|
||||||
|
text = str(exc).strip()
|
||||||
|
if text:
|
||||||
|
return text
|
||||||
|
return f"{type(exc).__name__}: {exc!r}"
|
||||||
|
|
||||||
|
|
||||||
async def inspect_update() -> dict[str, Any]:
|
async def inspect_update() -> dict[str, Any]:
|
||||||
try:
|
try:
|
||||||
latest = await latest_release()
|
latest = await latest_release()
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
class StarCitizenWikiError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class StarCitizenWikiClient:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
base_url: str = "https://starcitizen.tools",
|
||||||
|
api_base_url: str = "https://api.star-citizen.wiki",
|
||||||
|
) -> None:
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
self.api_base_url = api_base_url.rstrip("/")
|
||||||
|
|
||||||
|
async def search_pages(self, query: str, limit: int = 5) -> list[dict[str, Any]]:
|
||||||
|
body = await self._get_json(
|
||||||
|
f"{self.base_url}/api.php",
|
||||||
|
params={
|
||||||
|
"action": "query",
|
||||||
|
"generator": "prefixsearch",
|
||||||
|
"gpssearch": query,
|
||||||
|
"gpslimit": max(1, min(limit, 10)),
|
||||||
|
"prop": "description|pageimages|extracts",
|
||||||
|
"exintro": 1,
|
||||||
|
"explaintext": 1,
|
||||||
|
"exchars": 320,
|
||||||
|
"piprop": "thumbnail",
|
||||||
|
"pithumbsize": 240,
|
||||||
|
"format": "json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
pages = body.get("query", {}).get("pages", {})
|
||||||
|
ordered = sorted(
|
||||||
|
(item for item in pages.values() if isinstance(item, dict)),
|
||||||
|
key=lambda item: int(item.get("index") or 0),
|
||||||
|
)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"pageid": item.get("pageid"),
|
||||||
|
"title": item.get("title"),
|
||||||
|
"description": item.get("description"),
|
||||||
|
"extract": item.get("extract"),
|
||||||
|
"thumbnail": (item.get("thumbnail") or {}).get("source"),
|
||||||
|
"url": f"{self.base_url}/{quote(str(item.get('title') or '').replace(' ', '_'), safe=':/_')}",
|
||||||
|
}
|
||||||
|
for item in ordered
|
||||||
|
if item.get("title")
|
||||||
|
]
|
||||||
|
|
||||||
|
async def get_page_summary(self, title: str | None = None, pageid: int | None = None, chars: int = 700) -> dict[str, Any] | None:
|
||||||
|
params: dict[str, Any] = {
|
||||||
|
"action": "query",
|
||||||
|
"prop": "extracts|description|pageimages",
|
||||||
|
"exintro": 1,
|
||||||
|
"explaintext": 1,
|
||||||
|
"exchars": max(120, min(chars, 1200)),
|
||||||
|
"piprop": "thumbnail",
|
||||||
|
"pithumbsize": 320,
|
||||||
|
"format": "json",
|
||||||
|
}
|
||||||
|
if pageid is not None:
|
||||||
|
params["pageids"] = pageid
|
||||||
|
elif title:
|
||||||
|
params["titles"] = title
|
||||||
|
else:
|
||||||
|
raise StarCitizenWikiError("title or pageid is required")
|
||||||
|
|
||||||
|
body = await self._get_json(f"{self.base_url}/api.php", params=params)
|
||||||
|
pages = body.get("query", {}).get("pages", {})
|
||||||
|
for item in pages.values():
|
||||||
|
if isinstance(item, dict) and item.get("pageid") and item.get("title"):
|
||||||
|
return {
|
||||||
|
"pageid": item.get("pageid"),
|
||||||
|
"title": item.get("title"),
|
||||||
|
"description": item.get("description"),
|
||||||
|
"extract": item.get("extract"),
|
||||||
|
"thumbnail": (item.get("thumbnail") or {}).get("source"),
|
||||||
|
"url": f"{self.base_url}/{quote(str(item.get('title') or '').replace(' ', '_'), safe=':/_')}",
|
||||||
|
}
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def search_verse(self, query: str) -> list[dict[str, Any]]:
|
||||||
|
body = await self._get_json(
|
||||||
|
f"{self.api_base_url}/api/search",
|
||||||
|
params={"filter[query]": query},
|
||||||
|
)
|
||||||
|
data = body.get("data")
|
||||||
|
return data if isinstance(data, list) else []
|
||||||
|
|
||||||
|
async def get_vehicle(self, slug: str) -> dict[str, Any]:
|
||||||
|
body = await self._get_json(f"{self.api_base_url}/api/vehicles/{slug.strip('/')}")
|
||||||
|
data = body.get("data")
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise StarCitizenWikiError(f"Vehicle response for {slug} was not an object.")
|
||||||
|
return data
|
||||||
|
|
||||||
|
async def _get_json(self, url: str, params: dict[str, Any] | None = None) -> Any:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(url, params=params, headers={"Accept": "application/json"})
|
||||||
|
try:
|
||||||
|
body = response.json()
|
||||||
|
except ValueError as exc:
|
||||||
|
raise StarCitizenWikiError(f"Star Citizen Wiki returned non-JSON response: HTTP {response.status_code}") from exc
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise StarCitizenWikiError(f"Star Citizen Wiki HTTP {response.status_code}: {body}")
|
||||||
|
return body
|
||||||
+1535
-17
File diff suppressed because it is too large
Load Diff
+8
-1
@@ -1,6 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
__version__ = "0.0.2"
|
__version__ = "0.0.8"
|
||||||
|
|
||||||
RELEASES_URL = "https://git.hudsonriggs.systems/LambdaBankingConglomerate/TraderAI/releases"
|
RELEASES_URL = "https://git.hudsonriggs.systems/LambdaBankingConglomerate/TraderAI/releases"
|
||||||
RELEASES_API_URL = "https://git.hudsonriggs.systems/api/v1/repos/LambdaBankingConglomerate/TraderAI/releases"
|
RELEASES_API_URL = "https://git.hudsonriggs.systems/api/v1/repos/LambdaBankingConglomerate/TraderAI/releases"
|
||||||
@@ -8,3 +8,10 @@ RELEASES_API_URL = "https://git.hudsonriggs.systems/api/v1/repos/LambdaBankingCo
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
|
class WikeloProjectsError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class WikeloProjectsClient:
|
||||||
|
APP_ID = "695be2905c0b4866dfb21265"
|
||||||
|
|
||||||
|
def __init__(self, base_url: str = "https://wikelo-projects.com") -> None:
|
||||||
|
self.base_url = base_url.rstrip("/")
|
||||||
|
|
||||||
|
async def list_ship_projects(self) -> list[dict[str, Any]]:
|
||||||
|
body = await self._get_json(f"{self.base_url}/api/apps/{self.APP_ID}/entities/ShipProject")
|
||||||
|
if not isinstance(body, list):
|
||||||
|
raise WikeloProjectsError("Wikelo ship projects response was not a list.")
|
||||||
|
return [item for item in body if isinstance(item, dict)]
|
||||||
|
|
||||||
|
async def _get_json(self, url: str) -> Any:
|
||||||
|
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
||||||
|
response = await client.get(url, headers={"Accept": "application/json"})
|
||||||
|
try:
|
||||||
|
body = response.json()
|
||||||
|
except ValueError as exc:
|
||||||
|
raise WikeloProjectsError(f"Wikelo Projects returned non-JSON response: HTTP {response.status_code}") from exc
|
||||||
|
if response.status_code >= 400:
|
||||||
|
raise WikeloProjectsError(f"Wikelo Projects HTTP {response.status_code}: {body}")
|
||||||
|
return body
|
||||||
File diff suppressed because one or more lines are too long
@@ -755,7 +755,7 @@ wheels = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "traderai"
|
name = "traderai"
|
||||||
version = "0.0.2"
|
version = "0.0.8"
|
||||||
source = { virtual = "." }
|
source = { virtual = "." }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "apscheduler" },
|
{ name = "apscheduler" },
|
||||||
@@ -1048,3 +1048,10 @@ wheels = [
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1122
-40
File diff suppressed because it is too large
Load Diff
+156
-20
@@ -9,6 +9,37 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main class="shell">
|
<main class="shell">
|
||||||
|
<nav class="chat-rail collapsed" id="chat-rail" aria-label="Chats, plans, and inbox">
|
||||||
|
<div class="chat-rail-top">
|
||||||
|
<button class="icon-button" id="chat-sidebar-toggle" type="button" title="Chats" aria-expanded="false">
|
||||||
|
<i data-lucide="panel-left" aria-hidden="true"></i>
|
||||||
|
<span>Menu</span>
|
||||||
|
</button>
|
||||||
|
<button class="icon-button" id="new-chat" type="button" title="New chat">
|
||||||
|
<i data-lucide="square-pen" aria-hidden="true"></i>
|
||||||
|
<span>New chat</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="chat-rail-content">
|
||||||
|
<section class="chat-nav-section">
|
||||||
|
<div class="rail-heading">Chats</div>
|
||||||
|
<div class="chat-list" id="chat-list"></div>
|
||||||
|
</section>
|
||||||
|
<section class="chat-nav-section">
|
||||||
|
<div class="rail-heading-row">
|
||||||
|
<div class="rail-heading">Plans</div>
|
||||||
|
<button class="rail-icon-button" id="plans-toggle" type="button" title="Plans" aria-expanded="false" aria-controls="plans-panel">
|
||||||
|
<i data-lucide="list-checks" aria-hidden="true"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="plans-rail-list" id="plans-rail-list"></div>
|
||||||
|
</section>
|
||||||
|
<section class="chat-nav-section">
|
||||||
|
<div class="rail-heading">Inbox</div>
|
||||||
|
<div class="inbox-list" id="inbox-list"></div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
<section class="workspace">
|
<section class="workspace">
|
||||||
<header class="topbar">
|
<header class="topbar">
|
||||||
<div class="brand-block">
|
<div class="brand-block">
|
||||||
@@ -20,6 +51,7 @@
|
|||||||
<h1>TraderAI</h1>
|
<h1>TraderAI</h1>
|
||||||
<p>Institutional marketplace intelligence for UEX operations</p>
|
<p>Institutional marketplace intelligence for UEX operations</p>
|
||||||
</div>
|
</div>
|
||||||
|
<span class="brand-short" aria-hidden="true">LBC</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="status" id="status">Ready</div>
|
<div class="status" id="status">Ready</div>
|
||||||
</header>
|
</header>
|
||||||
@@ -27,7 +59,10 @@
|
|||||||
<div class="messages" id="messages"></div>
|
<div class="messages" id="messages"></div>
|
||||||
<div class="composer-wrap">
|
<div class="composer-wrap">
|
||||||
<form class="composer" id="chat-form">
|
<form class="composer" id="chat-form">
|
||||||
|
<div class="composer-main">
|
||||||
<textarea id="message-input" rows="2" placeholder="Search listings, draft a reply, prepare an offer..."></textarea>
|
<textarea id="message-input" rows="2" placeholder="Search listings, draft a reply, prepare an offer..."></textarea>
|
||||||
|
<div class="composer-images" id="composer-images" hidden></div>
|
||||||
|
</div>
|
||||||
<button type="submit">Send</button>
|
<button type="submit">Send</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@@ -38,21 +73,6 @@
|
|||||||
<div id="pending-actions" class="pending-empty">No pending actions</div>
|
<div id="pending-actions" class="pending-empty">No pending actions</div>
|
||||||
</section>
|
</section>
|
||||||
<section class="side-section sidebar-tools">
|
<section class="side-section sidebar-tools">
|
||||||
<div class="sidebar-tool-buttons" role="tablist" aria-label="Sidebar panels">
|
|
||||||
<button class="sidebar-tool-button" id="settings-toggle" type="button" aria-expanded="false" aria-controls="settings-panel" title="Settings">
|
|
||||||
<i data-lucide="settings" aria-hidden="true"></i>
|
|
||||||
<span>Settings</span>
|
|
||||||
</button>
|
|
||||||
<button class="sidebar-tool-button" id="memory-toggle" type="button" aria-expanded="false" aria-controls="memory-panel" title="Memory">
|
|
||||||
<i data-lucide="brain" aria-hidden="true"></i>
|
|
||||||
<span>Memory</span>
|
|
||||||
</button>
|
|
||||||
<button class="sidebar-tool-button" id="ollama-toggle" type="button" aria-expanded="false" aria-controls="ollama-panel" title="Ollama">
|
|
||||||
<img class="sidebar-tool-image" src="/static/art/ollama-icon.svg" alt="" onerror="this.remove();">
|
|
||||||
<i data-lucide="bot" aria-hidden="true"></i>
|
|
||||||
<span>Ollama</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div class="sidebar-panel" id="settings-panel" hidden>
|
<div class="sidebar-panel" id="settings-panel" hidden>
|
||||||
<div class="section-title-row">
|
<div class="section-title-row">
|
||||||
<h2>Config</h2>
|
<h2>Config</h2>
|
||||||
@@ -99,14 +119,26 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="sidebar-panel" id="ollama-panel" hidden>
|
<div class="sidebar-panel" id="ollama-panel" hidden>
|
||||||
<div class="section-title-row">
|
<div class="section-title-row">
|
||||||
<h2>Ollama</h2>
|
<h2>Inference</h2>
|
||||||
<button class="secondary small-button" id="ollama-refresh" type="button">Refresh</button>
|
<button class="secondary small-button" id="ollama-refresh" type="button">Refresh</button>
|
||||||
</div>
|
</div>
|
||||||
<form class="config-form" id="ollama-config-form">
|
<form class="config-form" id="ollama-config-form">
|
||||||
<label>Ollama URL<input id="ollama-base-url" name="ollama_base_url" type="text"></label>
|
<label>Provider
|
||||||
<label>Model<input id="ollama-model" name="ollama_model" type="text"></label>
|
<select id="model-provider" name="model_provider">
|
||||||
<label>Context Tokens<input id="ollama-num-ctx" name="ollama_num_ctx" type="number" min="1024" step="1024"></label>
|
<option value="deepseek">DeepSeek V4 (Recommended)</option>
|
||||||
<button type="submit">Save Ollama Config</button>
|
<option value="ollama">Local Ollama</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label data-provider-scope="deepseek">DeepSeek URL<input id="deepseek-base-url" name="deepseek_base_url" type="text"></label>
|
||||||
|
<label data-provider-scope="deepseek">DeepSeek API Key<input id="deepseek-api-key" name="deepseek_api_key" type="password" autocomplete="off"></label>
|
||||||
|
<label data-provider-scope="deepseek" data-manual-model="true">DeepSeek Model<input id="deepseek-model" name="deepseek_model" type="text" list="provider-models"></label>
|
||||||
|
<label data-provider-scope="ollama">Ollama URL<input id="ollama-base-url" name="ollama_base_url" type="text"></label>
|
||||||
|
<label data-provider-scope="ollama">Ollama Model<input id="ollama-model" name="ollama_model" type="text" list="provider-models"></label>
|
||||||
|
<label data-provider-scope="ollama">Context Tokens<input id="ollama-num-ctx" name="ollama_num_ctx" type="number" min="1024" step="1024"></label>
|
||||||
|
<label><span id="provider-model-label">Model</span><select id="provider-model-select"></select></label>
|
||||||
|
<label>Reasoning Effort<select id="model-reasoning-effort" name="model_reasoning_effort"></select></label>
|
||||||
|
<datalist id="provider-models"></datalist>
|
||||||
|
<button type="submit">Save Provider Config</button>
|
||||||
</form>
|
</form>
|
||||||
<div class="ollama-status" id="ollama-status"></div>
|
<div class="ollama-status" id="ollama-status"></div>
|
||||||
<div class="ollama-actions">
|
<div class="ollama-actions">
|
||||||
@@ -117,9 +149,113 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="config-status" id="ollama-message"></div>
|
<div class="config-status" id="ollama-message"></div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="sidebar-tool-buttons" role="tablist" aria-label="Sidebar panels">
|
||||||
|
<button class="sidebar-tool-button" id="settings-toggle" type="button" aria-expanded="false" aria-controls="settings-panel" title="Settings">
|
||||||
|
<i data-lucide="settings" aria-hidden="true"></i>
|
||||||
|
<span>Settings</span>
|
||||||
|
</button>
|
||||||
|
<button class="sidebar-tool-button" id="memory-toggle" type="button" aria-expanded="false" aria-controls="memory-panel" title="Memory">
|
||||||
|
<i data-lucide="brain" aria-hidden="true"></i>
|
||||||
|
<span>Memory</span>
|
||||||
|
</button>
|
||||||
|
<button class="sidebar-tool-button" id="ollama-toggle" type="button" aria-expanded="false" aria-controls="ollama-panel" title="Inference">
|
||||||
|
<img class="sidebar-tool-image" src="/static/art/ollama-icon.svg" alt="" onerror="this.remove();">
|
||||||
|
<i data-lucide="bot" aria-hidden="true"></i>
|
||||||
|
<span>Inference</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
</aside>
|
</aside>
|
||||||
</main>
|
</main>
|
||||||
|
<div class="floating-panel" id="negotiation-panel" hidden>
|
||||||
|
<div class="floating-panel-header">
|
||||||
|
<div>
|
||||||
|
<p class="eyebrow">UEX negotiation</p>
|
||||||
|
<h2 id="negotiation-title">Negotiation</h2>
|
||||||
|
</div>
|
||||||
|
<button class="icon-button light" id="negotiation-close" type="button" title="Close">
|
||||||
|
<i data-lucide="x" aria-hidden="true"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="negotiation-messages" id="negotiation-messages"></div>
|
||||||
|
<form class="negotiation-composer" id="negotiation-form">
|
||||||
|
<textarea id="negotiation-input" rows="2" placeholder="Reply to the other party..."></textarea>
|
||||||
|
<button type="submit">Send</button>
|
||||||
|
</form>
|
||||||
|
<div class="config-status" id="negotiation-status"></div>
|
||||||
|
</div>
|
||||||
|
<div class="floating-panel plans-floating-panel" id="plans-panel" hidden>
|
||||||
|
<div class="floating-panel-header">
|
||||||
|
<div>
|
||||||
|
<p class="eyebrow">Continual work</p>
|
||||||
|
<h2>Plans</h2>
|
||||||
|
</div>
|
||||||
|
<div class="floating-panel-actions">
|
||||||
|
<button class="icon-button light" id="plans-refresh" type="button" title="Refresh plans">
|
||||||
|
<i data-lucide="refresh-cw" aria-hidden="true"></i>
|
||||||
|
</button>
|
||||||
|
<button class="icon-button light" id="plans-close" type="button" title="Close">
|
||||||
|
<i data-lucide="x" aria-hidden="true"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="plans-panel-body">
|
||||||
|
<aside class="plan-creator-shell">
|
||||||
|
<div class="plan-creator-card">
|
||||||
|
<div class="plan-creator-copy">
|
||||||
|
<p class="eyebrow">New continual plan</p>
|
||||||
|
<h3>Set the watch once</h3>
|
||||||
|
<p>Spin up buying runs or custom follow-up work with a title, a goal, and just enough guardrails to keep it on track.</p>
|
||||||
|
</div>
|
||||||
|
<form class="config-form plan-form-grid" id="plan-form">
|
||||||
|
<label>Title<input id="plan-title" type="text" placeholder="Wikelo Idris parts"></label>
|
||||||
|
<label>Objective<input id="plan-objective" type="text" placeholder="Find and draft deals for the parts I list"></label>
|
||||||
|
<div class="plan-form-split">
|
||||||
|
<label>Kind
|
||||||
|
<select id="plan-kind">
|
||||||
|
<option value="buying">Buying</option>
|
||||||
|
<option value="custom">Custom</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>Message Tone<input id="plan-tone" type="text" placeholder="polite and concise"></label>
|
||||||
|
</div>
|
||||||
|
<label>Items<textarea id="plan-items" rows="5" placeholder="One item per line, optionally: name | quantity | max unit price"></textarea></label>
|
||||||
|
<label>Instructions<textarea id="plan-instructions" rows="4" placeholder="Extra guidance for custom or buying plans"></textarea></label>
|
||||||
|
<div class="plan-form-split">
|
||||||
|
<label>Cron Cadence<input id="plan-cadence" type="text" placeholder="0 */6 * * *"></label>
|
||||||
|
<div class="plan-form-hint">
|
||||||
|
<strong>Tip</strong>
|
||||||
|
<span>Buying plans work best with item lines. Custom plans can run with just instructions.</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="plan-form-actions">
|
||||||
|
<button id="plan-autofill" type="button">AI Fill</button>
|
||||||
|
<button type="submit">Create Plan</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-status" id="plans-status"></div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
<section class="plans-dashboard-shell">
|
||||||
|
<div class="plans-dashboard" id="plans-dashboard"></div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-backdrop" id="update-modal" hidden>
|
||||||
|
<section class="update-modal-card">
|
||||||
|
<div class="section-title-row">
|
||||||
|
<h2>Update Available</h2>
|
||||||
|
<button class="icon-button light" id="update-modal-close" type="button" title="Close">
|
||||||
|
<i data-lucide="x" aria-hidden="true"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<p id="update-modal-copy"></p>
|
||||||
|
<div class="update-actions">
|
||||||
|
<button class="secondary small-button" id="update-modal-releases" type="button">Releases</button>
|
||||||
|
<button class="small-button" id="update-modal-install" type="button">Update</button>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
<script src="https://unpkg.com/lucide@0.562.0/dist/umd/lucide.min.js"></script>
|
<script src="https://unpkg.com/lucide@0.562.0/dist/umd/lucide.min.js"></script>
|
||||||
<script src="/static/app.js"></script>
|
<script src="/static/app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
+1091
-36
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,175 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<link href="https://qtrypzzcjebvfcihiynt.supabase.co/storage/v1/object/public/base44-prod/public/695be2905c0b4866dfb21265/62b39a568_Wikapp3.webp" rel="icon" type="image/svg+xml"/>
|
||||||
|
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
|
||||||
|
<link href="/manifest.json" rel="manifest"/>
|
||||||
|
<title>
|
||||||
|
Ships | Wikelo Project Tracker
|
||||||
|
</title>
|
||||||
|
<script crossorigin="" src="/assets/index-DWqdqkK8.js" type="module">
|
||||||
|
</script>
|
||||||
|
<link crossorigin="" href="/assets/index-BzxCYXI2.css" rel="stylesheet"/>
|
||||||
|
<meta content="Ships on Wikelo Project Tracker. Track materials needed and contributed for building your Star Citizen ships." name="description"/>
|
||||||
|
<meta content="Ships | Wikelo Project Tracker" property="og:title"/>
|
||||||
|
<meta content="Ships on Wikelo Project Tracker. Track materials needed and contributed for building your Star Citizen ships." property="og:description"/>
|
||||||
|
<meta content="https://qtrypzzcjebvfcihiynt.supabase.co/storage/v1/render/image/public/base44-prod/public/695be2905c0b4866dfb21265/62b39a568_Wikapp3.webp?width=1200&height=630&resize=contain" property="og:image"/>
|
||||||
|
<meta content="https://wikelo-projects.com/Ships" property="og:url"/>
|
||||||
|
<meta content="website" property="og:type"/>
|
||||||
|
<meta content="Wikelo Project Tracker" property="og:site_name"/>
|
||||||
|
<meta content="Ships | Wikelo Project Tracker" name="twitter:title"/>
|
||||||
|
<meta content="Ships on Wikelo Project Tracker. Track materials needed and contributed for building your Star Citizen ships." name="twitter:description"/>
|
||||||
|
<meta content="https://qtrypzzcjebvfcihiynt.supabase.co/storage/v1/render/image/public/base44-prod/public/695be2905c0b4866dfb21265/62b39a568_Wikapp3.webp?width=1200&height=630&resize=contain" name="twitter:image"/>
|
||||||
|
<meta content="summary_large_image" name="twitter:card"/>
|
||||||
|
<meta content="https://wikelo-projects.com/Ships" name="twitter:url"/>
|
||||||
|
<meta content="yes" name="mobile-web-app-capable"/>
|
||||||
|
<meta content="black" name="apple-mobile-web-app-status-bar-style"/>
|
||||||
|
<meta content="Wikelo Project Tracker" name="apple-mobile-web-app-title"/>
|
||||||
|
<link href="https://wikelo-projects.com/Ships" rel="canonical"/>
|
||||||
|
<script data-seo-source="builder" type="application/ld+json">
|
||||||
|
{"name": "Wikelo Project Tracker", "@context": "https://schema.org", "@type": "WebSite", "url": "https://wikelo-projects.com"}
|
||||||
|
</script>
|
||||||
|
<script data-seo-source="builder" type="application/ld+json">
|
||||||
|
{"name": "Wikelo Project Tracker", "logo": "https://qtrypzzcjebvfcihiynt.supabase.co/storage/v1/object/public/base44-prod/public/695be2905c0b4866dfb21265/62b39a568_Wikapp3.webp", "@context": "https://schema.org", "@type": "Organization", "url": "https://wikelo-projects.com"}
|
||||||
|
</script>
|
||||||
|
<script data-seo-source="builder" type="application/ld+json">
|
||||||
|
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://wikelo-projects.com/"}, {"@type": "ListItem", "position": 2, "name": "Ships | Wikelo Project Tracker", "item": "https://wikelo-projects.com/Ships"}]}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root">
|
||||||
|
<div data-seo-source="builder" id="seo-snapshot" style="position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0;">
|
||||||
|
<h1>
|
||||||
|
Ships | Wikelo Project Tracker
|
||||||
|
</h1>
|
||||||
|
<p>
|
||||||
|
Ships on Wikelo Project Tracker. Track materials needed and contributed for building your Star Citizen ships.
|
||||||
|
</p>
|
||||||
|
<nav aria-label="Pages">
|
||||||
|
<h2>
|
||||||
|
Pages
|
||||||
|
</h2>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/AdminAds">
|
||||||
|
Admin Ads
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/AdvertiseWithUs">
|
||||||
|
Advertise With Us
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/ArmorProjectDetails">
|
||||||
|
Armor Project Details
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Armors">
|
||||||
|
Armors
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/CleanupMaterials">
|
||||||
|
Cleanup Materials
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Guide">
|
||||||
|
Guide
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/">
|
||||||
|
Home
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Inventory">
|
||||||
|
Inventory
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Messages">
|
||||||
|
Messages
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/NotificationSettings">
|
||||||
|
Notification Settings
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Notifications">
|
||||||
|
Notifications
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/OrganizationDetails">
|
||||||
|
Organization Details
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Organizations">
|
||||||
|
Organizations
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Profile">
|
||||||
|
Profile
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/ProjectDetails">
|
||||||
|
Project Details
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/RecalculateContributionReputation">
|
||||||
|
Recalculate Contribution Reputation
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/RecalculateMaterials">
|
||||||
|
Recalculate Materials
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/RecalculateReputation">
|
||||||
|
Recalculate Reputation
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Reputation">
|
||||||
|
Reputation
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/UpdateInfo">
|
||||||
|
Update Info
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/WeaponProjectDetails">
|
||||||
|
Weapon Project Details
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/Weapons">
|
||||||
|
Weapons
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/WikeloProjectDetails">
|
||||||
|
Wikelo Project Details
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
Reference in New Issue
Block a user