Compare commits
35 Commits
7fcc97f525
...
projekti1
| Author | SHA1 | Date | |
|---|---|---|---|
| 20cea8f268 | |||
| 38a18c555b | |||
| 8138e41aa1 | |||
| 6ee5bdf960 | |||
| cf3bf54bf8 | |||
| 56f21a96c9 | |||
| 763b93396c | |||
| e09962940a | |||
| 5e44b63b0c | |||
| 0f3881aa02 | |||
| fa85dcc5b3 | |||
| 58d93613f0 | |||
| 66b4435362 | |||
| 3a00de9b8e | |||
| 670141c8c3 | |||
| 59daebbd38 | |||
| 42b71dbf77 | |||
| b88a741f85 | |||
|
|
68c7195d54 | ||
|
|
3d20238eef | ||
|
|
8b8ba01af3 | ||
|
|
a3b95a56e8 | ||
|
|
5b20ebe800 | ||
|
|
ffe9bd6902 | ||
|
|
d27068b11a | ||
|
|
8468724a4c | ||
|
|
6ef71b7e5c | ||
|
|
b2ee8b9031 | ||
|
|
c1a5f8aff5 | ||
|
|
8ee997cb56 | ||
|
|
cd67562a67 | ||
|
|
1f85c03624 | ||
|
|
74a2045def | ||
|
|
9b2b7767b5 | ||
|
|
1718805978 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -42,4 +42,7 @@ Cargo.lock
|
||||
*.log
|
||||
|
||||
# Wanha versio
|
||||
temp/
|
||||
temp/
|
||||
|
||||
# Muut
|
||||
zipit/**
|
||||
157
TEMPLATING.md
Normal file
157
TEMPLATING.md
Normal file
@@ -0,0 +1,157 @@
|
||||
# Templating — rakennuspalaset koodigeneroinnissa
|
||||
|
||||
## Perusperiaate
|
||||
|
||||
Kielimalli päättää **mitä** rakennetaan (entiteetit, kentät, tyypit, yhteydet).
|
||||
Template-funktiot päättävät **miten** se rakennetaan (importit, engine setup, testikonfiguraatio).
|
||||
|
||||
```
|
||||
Projektikuvaus → LLM → JSON-speksi → Templateit → Koodi → Validointi
|
||||
```
|
||||
|
||||
LLM:n kontribuutio on yksi JSON-rakenne. Kaikki muu on determinististä —
|
||||
sama speksi tuottaa aina saman koodin.
|
||||
|
||||
## Miksi tämä toimii
|
||||
|
||||
Pienen kielimallin (0.5B–7B) vahvuudet ja heikkoudet ovat epäsymmetrisiä:
|
||||
|
||||
| Tehtävä | LLM:n kyky | Ratkaisu |
|
||||
|---------|-----------|----------|
|
||||
| Tunnista entiteetit kuvauksesta | Hyvä | LLM tekee |
|
||||
| Valitse kenttätyypit | Hyvä | LLM tekee |
|
||||
| Muista importit oikein | Huono | Template tekee |
|
||||
| SQLite connect_args | Huono | Template tekee |
|
||||
| Testikonfiguraatio | Huono | Template tekee |
|
||||
| Dockerfile-rakenne | Huono | Template tekee |
|
||||
|
||||
Annetaan mallin tehdä se missä se on hyvä. Hoidetaan loput mekaanisesti.
|
||||
|
||||
## JSON-speksi
|
||||
|
||||
Kielimallin ainoa tuotos on JSON joka kuvaa projektin rakenteen:
|
||||
|
||||
```json
|
||||
{
|
||||
"project_name": "library-app",
|
||||
"entities": [
|
||||
{
|
||||
"name": "Author",
|
||||
"table_name": "authors",
|
||||
"fields": [
|
||||
{"name": "name", "sa_type": "String(255)", "py_type": "str", "nullable": false, "default": null}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Book",
|
||||
"table_name": "books",
|
||||
"fields": [
|
||||
{"name": "title", "sa_type": "String(255)", "py_type": "str", "nullable": false, "default": null},
|
||||
{"name": "author_id", "sa_type": "Integer", "py_type": "int", "nullable": false, "default": null}
|
||||
]
|
||||
}
|
||||
],
|
||||
"relationships": [
|
||||
{"from": "Book", "field": "author_id", "to": "Author", "type": "many-to-one"}
|
||||
],
|
||||
"extra_imports": []
|
||||
}
|
||||
```
|
||||
|
||||
Speksin laatu ratkaisee kaiken. Hyvä speksi → hyvä projekti. Huono speksi →
|
||||
teknisesti toimiva mutta sisällöllisesti väärä projekti.
|
||||
|
||||
## Architect-promptin rooli
|
||||
|
||||
Architect-agentti (JSON-speksin generoija) on kriittisin kohta koko pipelinessa.
|
||||
Sitä ohjataan neljällä keinolla:
|
||||
|
||||
1. **Chain-of-thought** — malli miettii ensin entiteetit, sitten kentät,
|
||||
sitten yhteydet, vasta lopuksi JSON
|
||||
2. **Domain-esimerkit** — Todo, verkkokauppa, blogi — malli näkee miltä
|
||||
hyvä speksi näyttää eri domaineissa
|
||||
3. **Anti-patternit** — turhat ID-kentät, Enum-tyypit, suomenkieliset nimet
|
||||
4. **Yhteyssäännöt** — jokainen `_id`-kenttä tarvitsee relationship-merkinnän
|
||||
|
||||
Isompi malli tässä yhdessä kohdassa parantaisi kaikkien projektien laatua.
|
||||
|
||||
## Templateit
|
||||
|
||||
Jokainen template on funktio joka ottaa speksin ja palauttaa koodia:
|
||||
|
||||
```
|
||||
tmplModels(spec) → models.py (SQLAlchemy, ForeignKey, relationship)
|
||||
tmplSchemas(spec) → schemas.py (Pydantic Create/Response/Detail)
|
||||
tmplMain(spec) → main.py (FastAPI CRUD + nested endpoints + FK-validointi)
|
||||
tmplTests(spec) → test_main.py (pytest + TestClient + helper-funktiot)
|
||||
tmplPyproject(spec) → pyproject.toml (PEP 621)
|
||||
tmplDockerfile() → Dockerfile (uv + non-root user)
|
||||
```
|
||||
|
||||
Templateit generoivat automaattisesti:
|
||||
- ForeignKey-constraintit ja relationship()-määrittelyt
|
||||
- Nested endpointit (`GET /authors/{id}/books/`)
|
||||
- FK-validointi (404 jos parent-entiteettiä ei ole)
|
||||
- Detail-schemat (Book + author-data mukana)
|
||||
- Test-helperit jotka luovat parent-entiteetit ensin
|
||||
- Bad FK -testit (varmistaa että orpo-validointi toimii)
|
||||
|
||||
## Validointi
|
||||
|
||||
Generoitu koodi validoidaan mekaanisesti ennen käyttöä:
|
||||
|
||||
- Syntaksitarkistus (AST parse)
|
||||
- Projektin sisäiset importit (löytyykö nimi lähdetiedostosta)
|
||||
- SQLite connect_args
|
||||
- Relatiiviset importit (kielletty)
|
||||
- Testien rakenne (ei saa kopioida appia)
|
||||
- pyproject.toml (ei poetryä)
|
||||
- Dockerfile (ei poetryä, uv cache -oikeudet)
|
||||
|
||||
Docker-testi ajaa koko projektin: build → pytest → API smoke test.
|
||||
|
||||
## Rajoitukset
|
||||
|
||||
Templateit kattavat rakenteellisesti tunnetut projektit:
|
||||
|
||||
| Stack | Kattavuus |
|
||||
|-------|-----------|
|
||||
| FastAPI + SQLAlchemy CRUD | Toimii hyvin |
|
||||
| Streamlit + DuckDB dashboard | Toimii hyvin |
|
||||
| Muu | Ei templatea → ei toimi |
|
||||
|
||||
**Ei kata:**
|
||||
- Custom business-logiikka (algoritmit, laskenta, ML)
|
||||
- Epätyypilliset arkkitehtuurit (WebSocket, graafit, tapahtumapohjaiset)
|
||||
- Frontend-sovellukset (React, Vue)
|
||||
- Mikä tahansa mitä template ei tunne
|
||||
|
||||
Arvio: templateit kattavat ~20% kaikista mahdollisista projekteista, mutta juuri
|
||||
sen 20% mitä opiskelu- ja prototyyppiympäristöissä tarvitaan useimmin.
|
||||
|
||||
## Laajentaminen
|
||||
|
||||
Uuden stackin lisääminen vaatii:
|
||||
|
||||
1. Uudet template-funktiot (käsityö, ~200–400 riviä per stack)
|
||||
2. JSON-speksin laajennos (uudet kentät jos tarvitaan)
|
||||
3. Validointisäännöt uudelle stackille
|
||||
4. Docker-testikonfiguraatio
|
||||
|
||||
Jokainen template on staattinen — se ei opi eikä sopeudu. Kattavuus kasvaa
|
||||
vain kirjoittamalla lisää templateja.
|
||||
|
||||
## Hybridi: seuraava askel
|
||||
|
||||
Paras lopputulos syntyisi yhdistelmällä:
|
||||
|
||||
```
|
||||
Speksi → Template (runko) → LLM (business-logiikka) → Validointi
|
||||
```
|
||||
|
||||
Template tuottaa toimivan CRUD-pohjan. LLM lisää domain-kohtaisen logiikan
|
||||
pienissä palasissa (yksi funktio kerrallaan). Mekaaninen validointi
|
||||
tarkistaa jokaisen lisäyksen.
|
||||
|
||||
Tämä palauttaa LLM:n epäluotettavuuden takaisin peliin, mutta rajattuna:
|
||||
virheet ovat paikallisia (yksi funktio) eivätkä rakenteellisia (koko projekti).
|
||||
@@ -230,6 +230,188 @@ mitä luokkia importata.
|
||||
|
||||
---
|
||||
|
||||
## Rakennuspalaset vs. vapaa generointi
|
||||
|
||||
Kielimalli voi generoida koodia kahdella perustavanlaatuisesti eri tavalla.
|
||||
Ymmärtäminen milloin kumpikin toimii on avain luotettavaan koodigenerointi-pipelineen.
|
||||
|
||||
### Tapa 1: Vapaa generointi (naivi)
|
||||
|
||||
LLM generoi jokaisen tiedoston tyhjästä. Prompti kuvaa mitä halutaan,
|
||||
malli tuottaa koko tiedoston — importeista lähtien.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
P["Prompti"] --> LLM1["LLM: models.py"]
|
||||
LLM1 --> V1{"Validointi"}
|
||||
V1 -->|virhe| LLM1
|
||||
V1 -->|ok| LLM2["LLM: schemas.py"]
|
||||
LLM2 --> V2{"Validointi"}
|
||||
V2 -->|virhe| LLM2
|
||||
V2 -->|ok| LLM3["LLM: main.py"]
|
||||
LLM3 --> V3{"..."}
|
||||
|
||||
style V1 fill:#1a1e2e,stroke:#f85149,color:#c9d1d9
|
||||
style V2 fill:#1a1e2e,stroke:#f85149,color:#c9d1d9
|
||||
style V3 fill:#1a1e2e,stroke:#f85149,color:#c9d1d9
|
||||
```
|
||||
|
||||
**Ongelma:** Pieni malli (0.5B–7B) tekee toistuvia rakenteellisia virheitä:
|
||||
|
||||
| Virhe | Esiintymistiheys | Selitys |
|
||||
|-------|:---:|------|
|
||||
| Puuttuva import | ~60% | `from datetime import date` unohtuu |
|
||||
| SQLite `connect_args` | ~80% | Malli ei muista SQLite-erityisyyttä |
|
||||
| Väärä Enum-käyttö | ~50% | Sekoittaa `sqlalchemy.Enum` ja `enum.Enum` |
|
||||
| Poetry pyproject.toml:ssa | ~40% | Malli suosii Poetryä vaikka ohje sanoo uv |
|
||||
| Testit kopioivat koko appin | ~70% | Malli ei osaa importata, luo uudet reitit |
|
||||
|
||||
Retry-loopilla (virhe → uusi yritys virheviestin kanssa) osa korjautuu,
|
||||
mutta **sama malli toistaa samoja virheitä** koska ne johtuvat harjoitusdatasta.
|
||||
7 tiedoston projekti vaatii 7–14 LLM-kutsua ja 80–120 sekuntia.
|
||||
|
||||
### Tapa 2: Rakennuspalaset (template pipeline)
|
||||
|
||||
LLM:ltä pyydetään **vain JSON-speksi** — entiteetit, kentät ja tyypit.
|
||||
Koodi kootaan mekaanisesti valmiista pohjista joiden rakenne on todistettavasti
|
||||
oikein.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
P["Projektin kuvaus"] --> LLM["LLM: JSON-speksi"]
|
||||
LLM --> S["{ entities: [...] }"]
|
||||
S --> T1["Template: models.py"]
|
||||
S --> T2["Template: schemas.py"]
|
||||
S --> T3["Template: main.py"]
|
||||
S --> T4["Template: test_main.py"]
|
||||
S --> T5["Template: Dockerfile"]
|
||||
T1 & T2 & T3 & T4 & T5 --> D["Docker build + pytest"]
|
||||
|
||||
style LLM fill:#1a1e2e,stroke:#d29922,color:#c9d1d9
|
||||
style S fill:#1a1e2e,stroke:#3fb950,color:#c9d1d9
|
||||
style D fill:#1a1e2e,stroke:#58a6ff,color:#c9d1d9
|
||||
```
|
||||
|
||||
**Idea:** Malli on hyvä päättämään *mitä* (entiteetit, kentät), mutta huono
|
||||
muistamaan *miten* (importit, engine setup, testikonfiguraatio). Annetaan
|
||||
mallin tehdä se missä se on hyvä, ja hoidetaan loput mekaanisesti.
|
||||
|
||||
### LLM:n ainoa tehtävä
|
||||
|
||||
Malli tuottaa JSON-rakenteen kuten:
|
||||
|
||||
```json
|
||||
{
|
||||
"project_name": "todo-app",
|
||||
"entities": [
|
||||
{
|
||||
"name": "Todo",
|
||||
"table_name": "todos",
|
||||
"fields": [
|
||||
{"name": "title", "sa_type": "String(255)", "py_type": "str", "nullable": false},
|
||||
{"name": "due_date", "sa_type": "Date", "py_type": "date | None", "nullable": true},
|
||||
{"name": "status", "sa_type": "String(20)", "py_type": "str", "default": "pending"}
|
||||
]
|
||||
}
|
||||
],
|
||||
"extra_imports": ["from datetime import date"]
|
||||
}
|
||||
```
|
||||
|
||||
Tämä on yksinkertainen tehtävä jossa pienikin malli onnistuu luotettavasti:
|
||||
entiteettien tunnistus projektin kuvauksesta ja kenttätyyppien valinta.
|
||||
|
||||
Speksi sisältää myös **taulujen väliset yhteydet** (relationships):
|
||||
|
||||
```json
|
||||
{
|
||||
"entities": [
|
||||
{"name": "Author", "table_name": "authors", "fields": [...]},
|
||||
{"name": "Book", "table_name": "books", "fields": [
|
||||
{"name": "title", "sa_type": "String(255)", "py_type": "str", "nullable": false},
|
||||
{"name": "author_id", "sa_type": "Integer", "py_type": "int", "nullable": false}
|
||||
]}
|
||||
],
|
||||
"relationships": [
|
||||
{"from": "Book", "field": "author_id", "to": "Author", "type": "many-to-one"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Templateit generoivat yhteyksistä automaattisesti:
|
||||
- `ForeignKey('authors.id')` models.py:hin
|
||||
- `relationship("Book", back_populates="author")` molempiin suuntiin
|
||||
- `BookDetail`-schema jossa author-data mukana
|
||||
- `GET /authors/{id}/books/` nested endpoint
|
||||
- FK-validointi: 404 jos parent-entiteettiä ei ole
|
||||
|
||||
### Architect-agentti: speksin laatu ratkaisee
|
||||
|
||||
Arkkitehti on **kriittisin agentti** koko pipelinessa. Jos speksi on hyvä
|
||||
(oikeat taulut, kentät, yhteydet), kaikki muu seuraa automaattisesti.
|
||||
Jos speksi on huono, templateitkaan eivät pelasta.
|
||||
|
||||
Arkkitehtia ohjataan:
|
||||
1. **Chain-of-thought**: "Mieti ensin taulut, sitten kentät, sitten yhteydet"
|
||||
2. **Domain-esimerkit**: Todo, verkkokauppa, blogi — malli näkee miltä hyvä speksi näyttää
|
||||
3. **Anti-patternit**: "Ei turhia ID-kenttiä, ei Enumeita, ei suomenkielisiä nimiä koodissa"
|
||||
4. **Yhteyssäännöt**: "Jokainen `_id`-kenttä tarvitsee vastaavan relationship-merkinnän"
|
||||
|
||||
Isompi malli (tai API) tässä yhdessä kohdassa parantaa kaikkien projektien laatua
|
||||
koska speksi on ainoa paikka jossa LLM:n ymmärrys vaikuttaa.
|
||||
|
||||
### Template täyttää loput
|
||||
|
||||
Jokainen template on kuin madlib — aukot täytetään speksin datalla:
|
||||
|
||||
**models.py template (yksinkertaistettu):**
|
||||
```python
|
||||
from sqlalchemy import create_engine, Column, Integer, {sa_types}, ForeignKey
|
||||
from sqlalchemy.orm import sessionmaker, relationship
|
||||
# ... aina samat importit, engine setup, SessionLocal ...
|
||||
|
||||
class {entity.name}(Base):
|
||||
__tablename__ = "{entity.table_name}"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
{field.name} = Column({field.sa_type}, nullable={field.nullable})
|
||||
# FK-kentät: ForeignKey + relationship automaattisesti
|
||||
{fk_field} = Column(Integer, ForeignKey('{parent_table}.id'))
|
||||
{parent_lower} = relationship("{Parent}", back_populates="{children}")
|
||||
```
|
||||
|
||||
Tulos: importit ovat aina oikein, `connect_args` on aina mukana,
|
||||
taulujen yhteydet generoituvat oikein, testit importoivat `main.py`:stä eivätkä kopioi sitä.
|
||||
|
||||
### Vertailu: mittaustulokset
|
||||
|
||||
| | Vapaa generointi | Rakennuspalaset |
|
||||
|---|:---:|:---:|
|
||||
| LLM-kutsuja | 7–14 | **3** (speksi + requirements + README) |
|
||||
| Aika | 80–120s | **~25s** |
|
||||
| Syntaksi OK | ~70% | **100%** |
|
||||
| Docker build | vaihteleva | **100%** |
|
||||
| Pytest läpi | 0% | **100%** |
|
||||
| API toimii | ~30% | **100%** |
|
||||
| Taulujen yhteydet (FK) | ei koskaan | **100%** |
|
||||
| Nested endpointit | ei koskaan | **automaattisesti** |
|
||||
|
||||
### Milloin kumpikin toimii
|
||||
|
||||
**Rakennuspalaset** kun:
|
||||
- Projektin rakenne on tunnettu (FastAPI + SQLAlchemy CRUD)
|
||||
- Laatu ja luotettavuus ovat tärkeitä
|
||||
- Malli on pieni (0.5B–7B)
|
||||
|
||||
**Vapaa generointi** kun:
|
||||
- Projektin rakenne on epätavallinen
|
||||
- Tarvitaan custom-logiikkaa jota template ei kata
|
||||
- Malli on riittävän iso (>70B tai pilvi-API)
|
||||
|
||||
Paras lopputulos syntyy yhdistelmällä: **rakennuspalaset perusrakenteelle,
|
||||
vapaa generointi business-logiikalle**.
|
||||
|
||||
---
|
||||
|
||||
## Laadun parantaminen
|
||||
|
||||
### 1. Isompi malli (suurin vaikutus)
|
||||
|
||||
@@ -1 +1 @@
|
||||
dirty-3e9cdd70c60dadfb970cee47ebbd912c
|
||||
cf3bf54
|
||||
|
||||
Binary file not shown.
BIN
network-poc/frontend/public/forge_hero.webp
Normal file
BIN
network-poc/frontend/public/forge_hero.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 91 KiB |
BIN
network-poc/frontend/public/gecko_hero.webp
Normal file
BIN
network-poc/frontend/public/gecko_hero.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 105 KiB |
@@ -99,23 +99,27 @@ if [ -n "$KIPINA_MODEL" ]; then
|
||||
echo " Malli: $KIPINA_MODEL (Ympäristömuuttujasta)"
|
||||
fi
|
||||
|
||||
# Lataa binääri
|
||||
# Binäärin automaattinen päivitys — vertaa build-hashia palvelimeen
|
||||
BIN_PATH="./kipina-node-bin"
|
||||
if [ -f "$BIN_PATH" ]; then
|
||||
echo ""
|
||||
read -p " Löydettiin vanha kipina-node-bin lokaalisti. Haluatko poistaa sen ja ladata uusimman version? [Y/n] " -r DEL_CHOICE
|
||||
if [[ "$DEL_CHOICE" =~ ^[Nn]$ ]]; then
|
||||
echo " ✓ Käytetään lokaalia versiota."
|
||||
else
|
||||
rm -f "$BIN_PATH"
|
||||
echo " ✓ Vanha binääri poistettu ja korvataan uudella."
|
||||
fi
|
||||
fi
|
||||
HASH_PATH="./kipina-node-bin.hash"
|
||||
|
||||
if [ ! -f "$BIN_PATH" ]; then
|
||||
echo " Ladataan tuorein $BINARY..."
|
||||
REMOTE_HASH=$(curl -sSL "$BASE_URL/.build-hash?v=$(date +%s)" 2>/dev/null | tr -d '[:space:]')
|
||||
LOCAL_HASH=""
|
||||
[ -f "$HASH_PATH" ] && LOCAL_HASH=$(cat "$HASH_PATH" | tr -d '[:space:]')
|
||||
|
||||
if [ -f "$BIN_PATH" ] && [ -n "$REMOTE_HASH" ] && [ "$REMOTE_HASH" = "$LOCAL_HASH" ]; then
|
||||
echo " ✓ Binääri ajan tasalla (versio: $LOCAL_HASH)"
|
||||
else
|
||||
if [ -f "$BIN_PATH" ]; then
|
||||
echo " ↻ Uusi versio saatavilla ($LOCAL_HASH → $REMOTE_HASH)"
|
||||
else
|
||||
echo " Ladataan $BINARY..."
|
||||
fi
|
||||
rm -f "$BIN_PATH"
|
||||
curl -sSL "$BASE_URL/$BINARY?v=$(date +%s)" -o "$BIN_PATH"
|
||||
chmod +x "$BIN_PATH"
|
||||
echo "$REMOTE_HASH" > "$HASH_PATH"
|
||||
echo " ✓ Päivitetty versioon $REMOTE_HASH"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
BIN
network-poc/frontend/public/serpent_hero.webp
Normal file
BIN
network-poc/frontend/public/serpent_hero.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
@@ -1,37 +1,37 @@
|
||||
<!-- Agenttigalleria + konfigurointipaneeli -->
|
||||
<!-- Agent gallery + configuration panel -->
|
||||
<div style="display:flex;gap:16px;padding:10px 0;align-items:flex-start">
|
||||
<!-- Agenttilista (drag & drop) -->
|
||||
<div id="agent-bar" style="display:flex;gap:6px;align-items:flex-end;flex-wrap:wrap">
|
||||
<!-- Renderöidään JS:stä -->
|
||||
</div>
|
||||
<!-- + Lisää agentti -->
|
||||
<div id="add-agent-btn" class="agent-avatar" onclick="addCustomAgent()" title="Lisää oma agentti" style="opacity:0.4">
|
||||
<!-- + Add agent -->
|
||||
<div id="add-agent-btn" class="agent-avatar" onclick="addCustomAgent()" title="Add custom agent" style="opacity:0.4">
|
||||
<div style="width:48px;height:48px;border-radius:50%;border:2px dashed var(--border);display:flex;align-items:center;justify-content:center;font-size:24px;color:var(--border)">+</div>
|
||||
<span style="font-size:10px;color:#8b949e;text-align:center;display:block">Lisää</span>
|
||||
<span style="font-size:10px;color:#8b949e;text-align:center;display:block">Add</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Agentin konfigurointipaneeli (avautuu klikkaamalla avataria) -->
|
||||
<!-- Agent configuration panel (opens clicking avatar) -->
|
||||
<div id="agent-config" style="display:none;background:var(--panel);border:1px solid var(--border);border-radius:6px;padding:16px;margin-bottom:10px">
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px">
|
||||
<div style="display:flex;align-items:center;gap:10px">
|
||||
<img id="config-avatar" src="" style="width:40px;height:40px;border-radius:50%">
|
||||
<div>
|
||||
<input id="config-name" style="background:transparent;border:none;color:var(--text);font-size:16px;font-weight:600;outline:none;width:200px" placeholder="Agentin nimi">
|
||||
<input id="config-name" style="background:transparent;border:none;color:var(--text);font-size:16px;font-weight:600;outline:none;width:200px" placeholder="Agent Name">
|
||||
<div id="config-role" style="font-size:11px;color:#8b949e"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div style="display:flex;gap:6px">
|
||||
<button class="btn btn-red" onclick="deleteAgent()" title="Poista agentti">Poista</button>
|
||||
<button class="btn btn-muted" onclick="closeAgentConfig()">Sulje</button>
|
||||
<button class="btn btn-red" onclick="deleteAgent()" title="Delete agent">Delete</button>
|
||||
<button class="btn btn-muted" onclick="closeAgentConfig()">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Malli -->
|
||||
<!-- Model -->
|
||||
<div style="margin-bottom:10px">
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:4px">Kielimalli</label>
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:4px">Model</label>
|
||||
<select id="config-model" style="background:var(--bg);color:var(--text);border:1px solid var(--border);border-radius:4px;padding:6px 10px;font-size:13px;width:100%">
|
||||
<option value="qwen-coder">Qwen2.5-Coder:0.5B (selain)</option>
|
||||
<option value="qwen-coder">Qwen2.5-Coder:0.5B (browser)</option>
|
||||
<option value="qwen-coder-3b">Qwen2.5-Coder:3B (Ollama)</option>
|
||||
<option value="qwen2.5-coder:7b">Qwen2.5-Coder:7B (Ollama)</option>
|
||||
<option value="qwen2.5-coder:1.5b">Qwen2.5-Coder:1.5B (Ollama)</option>
|
||||
@@ -39,41 +39,41 @@
|
||||
</div>
|
||||
|
||||
<!-- System prompt -->
|
||||
<div style="margin-bottom:10px" title="Agentin perusohje joka lähetetään kielimallille jokaisessa pyynnössä. Hyvän promptin rakenne: 1. Rooli: 'You are an expert...' 2. Säännöt: RULES/CRITICAL RULES listana 3. Esimerkit: EXAMPLE OUTPUT 4. Kiellot: NEVER-lista Vinkki: käytä englantia — malli ymmärtää sen paremmin ja se kuluttaa vähemmän tokeneita.">
|
||||
<div style="margin-bottom:10px" title="System prompt sent to the LLM on every request. Good prompt structure: 1. Role: 'You are an expert...' 2. Rules: RULES/CRITICAL RULES as list 3. Examples: EXAMPLE OUTPUT 4. Restrictions: NEVER-list ">
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:4px;cursor:help">System prompt 💡</label>
|
||||
<textarea id="config-prompt" style="width:100%;background:var(--bg);color:var(--text);border:1px solid var(--border);border-radius:4px;padding:8px;font-size:13px;font-family:'Courier New',monospace;resize:vertical;overflow:hidden;min-height:60px" placeholder="Kuvaa agentin rooli ja käyttäytyminen..."></textarea>
|
||||
<textarea id="config-prompt" style="width:100%;background:var(--bg);color:var(--text);border:1px solid var(--border);border-radius:4px;padding:8px;font-size:13px;font-family:'Courier New',monospace;resize:vertical;overflow:hidden;min-height:60px" placeholder="Describe the agent's role and behavior..."></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Sampling-parametrit -->
|
||||
<!-- Sampling Parameters -->
|
||||
<div style="margin-bottom:10px">
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:8px">Sampling-parametrit</label>
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:8px">Sampling Parameters</label>
|
||||
<div style="display:grid;grid-template-columns:1fr 1fr;gap:10px">
|
||||
<div title="Kontrolloi 'luovuutta'. Matala arvo (0.2-0.4) tuottaa ennustettavaa, toistettavaa koodia — hyvä testaajille ja reviewereille. Keskiarvo (0.6-0.8) on paras koodin generointiin. Korkea arvo (1.0+) lisää vaihtelua mutta myös virheitä. Suositus: • Manageri: 0.5 (tarkat tiedostolistat) • Koodari: 0.7 (toimiva koodi + vaihtelu) • Testaaja: 0.3 (deterministinen arviointi)">
|
||||
<div title="Controls 'creativity'. Low value (0.2-0.4) produces predictable, repeatable code — good for testers and reviewers. Medium value (0.6-0.8) is best for generating code. High value (1.0+) adds variation but also errors. Recommendation: • Manager: 0.5 (precise file lists) • Coder: 0.7 (working code + variation) • Tester: 0.3 (deterministic evaluation)">
|
||||
<label style="font-size:11px;color:#8b949e;cursor:help">Temperature 💡 <span id="config-temp-val" style="color:var(--accent);float:right">0.7</span></label>
|
||||
<input type="range" id="config-temperature" min="0" max="1.5" step="0.1" value="0.7" style="width:100%;accent-color:var(--accent)">
|
||||
<div style="font-size:10px;color:#30363d">0=tarkka · 0.7=oletus · 1.5=luova</div>
|
||||
<div style="font-size:10px;color:#30363d">0=strict · 0.7=default · 1.5=creative</div>
|
||||
</div>
|
||||
<div title="Vastauksen maksimipituus tokeneina (~1 token ≈ 4 merkkiä). Suositus: • Manageri: 256-512 (lyhyet tiedostolistat) • Koodari: 1024-2048 (täydet tiedostot, CRUD-endpointit) • Testaaja: 256-512 (lyhyet arvioinnit) Jos koodi katkeaa kesken, nosta tätä. Jos malli tuottaa turhaa toistoa, laske.">
|
||||
<div title="Maximum response length in tokens (~1 token ≈ 4 chars). Recommendation: • Manager: 256-512 (short lists) • Coder: 1024-2048 (full files, CRUD endpoints) • Tester: 256-512 (short evaluations) If code cuts off early, increase this.">
|
||||
<label style="font-size:11px;color:#8b949e;cursor:help">Max tokens 💡 <span id="config-maxtok-val" style="color:var(--accent);float:right">1024</span></label>
|
||||
<input type="range" id="config-maxtokens" min="64" max="4096" step="64" value="1024" style="width:100%;accent-color:var(--accent)">
|
||||
<div style="font-size:10px;color:#30363d">Vastauksen maksimipituus</div>
|
||||
<div style="font-size:10px;color:#30363d">Maximum response length</div>
|
||||
</div>
|
||||
<div title="Montako todennäköisintä tokenia huomioidaan valinnassa. Pieni arvo (1-10) tekee vastauksesta deterministisen. Suuri arvo (50-100) sallii harvinaisempia sanoja. Suositus: • Boilerplate-koodi: 20-30 (tutut patternit) • Yleiskoodi: 40 (hyvä oletus) • Luova teksti: 60-80 Yleensä ei tarvitse muuttaa oletuksesta.">
|
||||
<div title="How many most probable tokens are considered. Low value (1-10) makes response deterministic. High value (50-100) allows rarer words. Recommendation: • Boilerplate code: 20-30 (familiar patterns) • General code: 40 (good default) • Creative text: 60-80">
|
||||
<label style="font-size:11px;color:#8b949e;cursor:help">Top-K 💡 <span id="config-topk-val" style="color:var(--accent);float:right">40</span></label>
|
||||
<input type="range" id="config-topk" min="1" max="100" step="1" value="40" style="width:100%;accent-color:var(--accent)">
|
||||
<div style="font-size:10px;color:#30363d">1=greedy · 40=oletus · 100=laaja</div>
|
||||
<div style="font-size:10px;color:#30363d">1=greedy · 40=default · 100=wide</div>
|
||||
</div>
|
||||
<div title="Vähentää jo tuotettujen sanojen todennäköisyyttä. Estää mallia toistamasta samaa lausetta. Liian korkea arvo (>1.5) voi rikkoa koodin koska samat avainsanat (return, if, def) ovat tarpeellisia. Suositus: • Koodi: 1.1-1.2 (lievä, sallii toiston) • Teksti: 1.15-1.3 (vahvempi) • Review: 1.0-1.1 (ei rangaistusta, lyhyet vastaukset)">
|
||||
<div title="Reduces the probability of already generated words. Prevents model from repeating same sentences. Too high value (>1.5) can break code because common keywords (return, if, def) are necessary. Recommendation: • Code: 1.1-1.2 (mild, allows repetition) • Text: 1.15-1.3 (stronger penalty) • Review: 1.0-1.1 (no penalty, short answers)">
|
||||
<label style="font-size:11px;color:#8b949e;cursor:help">Repetition penalty 💡 <span id="config-rep-val" style="color:var(--accent);float:right">1.15</span></label>
|
||||
<input type="range" id="config-repeat" min="1.0" max="2.0" step="0.05" value="1.15" style="width:100%;accent-color:var(--accent)">
|
||||
<div style="font-size:10px;color:#30363d">1.0=ei · 1.15=oletus · 2.0=vahva</div>
|
||||
<div style="font-size:10px;color:#30363d">1.0=none · 1.15=default · 2.0=strong</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Pipeline-järjestys -->
|
||||
<!-- Pipeline order -->
|
||||
<div>
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:4px">Pipeline-järjestys <span style="color:var(--border)">(vedä järjestääksesi)</span></label>
|
||||
<label style="font-size:12px;color:#8b949e;display:block;margin-bottom:4px">Pipeline Order <span style="color:var(--border)">(drag to sort)</span></label>
|
||||
<div id="config-pipeline" style="display:flex;gap:4px;flex-wrap:wrap"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
<!-- Monaco Editor paneeli -->
|
||||
<div id="panel-editor" class="panel">
|
||||
<div style="display:flex;height:calc(100vh - 200px);gap:0;border:1px solid var(--border);border-radius:6px;overflow:hidden">
|
||||
<div id="editor-filetree" style="width:200px;min-width:150px;background:var(--bg);border-right:1px solid var(--border);overflow-y:auto;font-family:'Courier New',monospace;font-size:13px">
|
||||
<div style="padding:10px 12px;color:#8b949e;font-size:11px;text-transform:uppercase;letter-spacing:0.5px;border-bottom:1px solid var(--border)">Tiedostot</div>
|
||||
<div style="display:flex;flex:1;min-height:0;gap:0;border:1px solid var(--border);border-radius:6px;overflow:hidden">
|
||||
<div id="editor-filetree" style="width:200px;min-width:150px;background:var(--bg);border-right:1px solid var(--border);overflow:auto;resize:horizontal;font-family:'Courier New',monospace;font-size:13px">
|
||||
<div style="padding:10px 12px;color:#8b949e;font-size:11px;display:flex;justify-content:space-between;align-items:center;text-transform:uppercase;letter-spacing:0.5px;border-bottom:1px solid var(--border)">
|
||||
<span>Tiedostot</span>
|
||||
<button class="btn btn-green" style="padding:2px 6px;font-size:10px" onclick="downloadProjectZip()">.ZIP</button>
|
||||
</div>
|
||||
<div id="editor-file-list" style="padding:4px 0">
|
||||
<div style="padding:8px 16px;color:#8b949e;font-size:12px">Generoi projekti:<br><code style="color:var(--accent)">kpn project "..."</code></div>
|
||||
</div>
|
||||
|
||||
@@ -58,6 +58,49 @@
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Pipeline-rajoitteet -->
|
||||
<div class="settings-section">
|
||||
<h3 class="settings-title">Pipeline-rajoitteet</h3>
|
||||
<p class="settings-desc">Projektin generoinnin rajat. Suuremmat arvot = rikkaampi output, hitaampi suoritus.</p>
|
||||
<div class="settings-grid">
|
||||
<div>
|
||||
<label class="settings-label">Client: max sanat <span id="set-plc-words-val" class="settings-val">400</span></label>
|
||||
<input type="range" id="set-plc-words" min="100" max="800" step="50" value="400" class="settings-slider">
|
||||
<div class="settings-hint">Vaatimusmäärittelyn maksimipituus sanoina</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">Client: max ominaisuudet <span id="set-plc-feats-val" class="settings-val">8</span></label>
|
||||
<input type="range" id="set-plc-feats" min="3" max="15" step="1" value="8" class="settings-slider">
|
||||
<div class="settings-hint">Montako ominaisuutta vaatimuksiin</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">Manager: max tiedostot <span id="set-plc-mfiles-val" class="settings-val">8</span></label>
|
||||
<input type="range" id="set-plc-mfiles" min="3" max="15" step="1" value="8" class="settings-slider">
|
||||
<div class="settings-hint">Managerin suunnittelemien tiedostojen yläraja</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">Vapaa tila: max tiedostot <span id="set-plc-ffiles-val" class="settings-val">8</span></label>
|
||||
<input type="range" id="set-plc-ffiles" min="3" max="15" step="1" value="8" class="settings-slider">
|
||||
<div class="settings-hint">Tiedostoraja kun ei mallipohjaa</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">Review-kierrokset <span id="set-plc-review-val" class="settings-val">3</span></label>
|
||||
<input type="range" id="set-plc-review" min="1" max="5" step="1" value="3" class="settings-slider">
|
||||
<div class="settings-hint">Katselmointi-korjaus-syklien max määrä</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">Terminaali: max rivit <span id="set-plc-term-val" class="settings-val">300</span></label>
|
||||
<input type="range" id="set-plc-term" min="50" max="1000" step="50" value="300" class="settings-slider">
|
||||
<div class="settings-hint">Terminaalin näyttämien rivien yläraja</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="settings-label">CrewAI: prompt-rivit <span id="set-plc-crew-val" class="settings-val">50</span></label>
|
||||
<input type="range" id="set-plc-crew" min="10" max="200" step="10" value="50" class="settings-slider">
|
||||
<div class="settings-hint">tasks.yaml:n promptin max rivimäärä</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Reset -->
|
||||
<div style="margin-top:24px;padding-top:16px;border-top:1px solid var(--border)">
|
||||
<button class="btn btn-red" onclick="resetSettings()" style="padding:6px 16px">Palauta oletukset</button>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,4 @@
|
||||
/* Oletusvärit — ylikirjoitetaan teemalla */
|
||||
:root {
|
||||
--bg: #0d1117;
|
||||
--panel: #161b22;
|
||||
@@ -8,6 +9,53 @@
|
||||
--red: #f85149;
|
||||
--purple: #a371f7;
|
||||
--border: #30363d;
|
||||
--hero-accent: #ff6b00;
|
||||
--hero-glow: rgba(255, 107, 0, 0.15);
|
||||
}
|
||||
|
||||
/* Gecko — lämmin kulta/oranssi (kipina.tech) */
|
||||
[data-theme="gecko"] {
|
||||
--bg: #0a0500;
|
||||
--panel: #1f1000;
|
||||
--text: #fff5e6;
|
||||
--accent: #ff7b00;
|
||||
--green: #3fb950;
|
||||
--yellow: #ffae00;
|
||||
--red: #f85149;
|
||||
--purple: #ff9d4d;
|
||||
--border: rgba(255, 174, 0, 0.2);
|
||||
--hero-accent: #ff7b00;
|
||||
--hero-glow: rgba(255, 123, 0, 0.15);
|
||||
}
|
||||
|
||||
/* Forge — kyber-sininen/syaani (kipina.tech) */
|
||||
[data-theme="forge"] {
|
||||
--bg: #060b11;
|
||||
--panel: #121e2d;
|
||||
--text: #e0f2fe;
|
||||
--accent: #00e5ff;
|
||||
--green: #3fb950;
|
||||
--yellow: #ff5e3a;
|
||||
--red: #f85149;
|
||||
--purple: #7dd3fc;
|
||||
--border: rgba(0, 229, 255, 0.15);
|
||||
--hero-accent: #00e5ff;
|
||||
--hero-glow: rgba(0, 229, 255, 0.15);
|
||||
}
|
||||
|
||||
/* Serpent — neon-turkoosi/teal (kipina.tech) */
|
||||
[data-theme="serpent"] {
|
||||
--bg: #000808;
|
||||
--panel: #001e1e;
|
||||
--text: #ccffff;
|
||||
--accent: #00ffff;
|
||||
--green: #00ffaa;
|
||||
--yellow: #d29922;
|
||||
--red: #f85149;
|
||||
--purple: #66cccc;
|
||||
--border: rgba(0, 255, 255, 0.15);
|
||||
--hero-accent: #00ffff;
|
||||
--hero-glow: rgba(0, 255, 255, 0.15);
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
@@ -20,10 +68,24 @@ body {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.container { max-width: 1600px; margin: 0 auto; padding: 20px 40px; }
|
||||
.container {
|
||||
max-width: 1600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px 40px;
|
||||
}
|
||||
|
||||
#app.container {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#app:not(.active) { display: none; }
|
||||
#landing.hidden { display: none; }
|
||||
|
||||
/* Tabs */
|
||||
.tabs { display: flex; gap: 4px; margin-bottom: 16px; }
|
||||
.tabs { display: flex; gap: 4px; margin-bottom: 16px; flex-shrink: 0; }
|
||||
.tab {
|
||||
padding: 10px 20px; border-radius: 6px 6px 0 0; cursor: pointer;
|
||||
border: 1px solid var(--border); border-bottom: none;
|
||||
@@ -33,7 +95,7 @@ body {
|
||||
|
||||
/* Panels */
|
||||
.panel { display: none; }
|
||||
.panel.active { display: block; }
|
||||
.panel.active { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow-y: auto; }
|
||||
|
||||
/* Status bar */
|
||||
.status-bar {
|
||||
@@ -52,7 +114,7 @@ body {
|
||||
.terminal {
|
||||
background: #010409; border: 1px solid var(--border); border-top: none;
|
||||
font-family: 'Courier New', monospace; font-size: 16px;
|
||||
min-height: 400px; max-height: 70vh; overflow-y: auto;
|
||||
flex: 1; min-height: 0; max-height: none; overflow-y: auto;
|
||||
padding: 12px 16px;
|
||||
}
|
||||
.terminal-line { padding: 1px 0; white-space: pre-wrap; word-break: break-word; }
|
||||
@@ -81,6 +143,12 @@ body {
|
||||
}
|
||||
.dd-item:hover, .dd-item.active { background: var(--border); color: var(--accent); }
|
||||
|
||||
#editor-file-list .dd-item {
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
/* Pipeline progress */
|
||||
.pipeline-bar {
|
||||
display: none; padding: 8px 14px; background: var(--bg);
|
||||
@@ -102,6 +170,7 @@ body {
|
||||
.project-tab {
|
||||
padding: 4px 10px; cursor: pointer; border-radius: 4px 4px 0 0;
|
||||
font-size: 12px; color: #8b949e;
|
||||
white-space: nowrap; flex-shrink: 0;
|
||||
}
|
||||
.project-tab.active { background: var(--panel); color: var(--accent); border: 1px solid var(--border); border-bottom: none; }
|
||||
|
||||
@@ -165,6 +234,13 @@ body {
|
||||
.agent-avatar.active img {
|
||||
border-color: var(--accent);
|
||||
box-shadow: 0 0 25px rgba(88,166,255,0.8);
|
||||
animation: agentBlink 1.5s infinite;
|
||||
}
|
||||
|
||||
@keyframes agentBlink {
|
||||
0% { opacity: 0.8; box-shadow: 0 0 15px rgba(88,166,255,0.5); }
|
||||
50% { opacity: 1.0; box-shadow: 0 0 35px rgba(88,166,255,1.0); }
|
||||
100% { opacity: 0.8; box-shadow: 0 0 15px rgba(88,166,255,0.5); }
|
||||
}
|
||||
|
||||
/* Settings */
|
||||
@@ -195,6 +271,218 @@ body {
|
||||
display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
|
||||
}
|
||||
|
||||
/* ===== LANDING PAGE ===== */
|
||||
|
||||
#landing {
|
||||
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
||||
min-height: 100vh;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.bg-mesh {
|
||||
position: fixed; inset: 0; z-index: -1;
|
||||
background:
|
||||
radial-gradient(ellipse 80% 60% at 20% 40%, var(--hero-glow) 0%, transparent 70%),
|
||||
radial-gradient(ellipse 60% 50% at 80% 20%, rgba(88,166,255,0.06) 0%, transparent 70%),
|
||||
var(--bg);
|
||||
}
|
||||
|
||||
.landing-nav {
|
||||
padding: 20px 40px;
|
||||
display: flex; align-items: center; justify-content: space-between;
|
||||
}
|
||||
.landing-logo { text-decoration: none; font-size: 18px; font-weight: 700; }
|
||||
.logo-accent { color: var(--hero-accent); }
|
||||
.logo-sub { color: #8b949e; font-weight: 400; }
|
||||
.theme-cycle-btn {
|
||||
background: none; border: 1px solid var(--border); border-radius: 8px;
|
||||
width: 38px; height: 38px; font-size: 20px; cursor: pointer;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
transition: border-color 0.2s, transform 0.15s;
|
||||
}
|
||||
.theme-cycle-btn:hover {
|
||||
border-color: var(--hero-accent); transform: scale(1.1);
|
||||
}
|
||||
|
||||
/* Hero */
|
||||
.hero {
|
||||
padding: 60px 40px 40px;
|
||||
}
|
||||
.hero-container {
|
||||
max-width: 1200px; margin: 0 auto;
|
||||
display: grid; grid-template-columns: 1fr 400px; gap: 60px; align-items: center;
|
||||
}
|
||||
.hero-title {
|
||||
font-size: clamp(2rem, 4vw, 3rem); font-weight: 800;
|
||||
line-height: 1.15; color: #e6edf3; margin-bottom: 16px;
|
||||
}
|
||||
.hero-divider {
|
||||
width: 60px; height: 3px; background: var(--hero-accent);
|
||||
border-radius: 2px; margin-bottom: 20px;
|
||||
}
|
||||
.hero-desc {
|
||||
font-size: 1.05rem; color: #8b949e; line-height: 1.7; margin-bottom: 12px;
|
||||
}
|
||||
.hero-notice {
|
||||
font-size: 0.9rem; color: #6e7681; line-height: 1.6;
|
||||
border-left: 2px solid var(--border); padding-left: 12px; margin-bottom: 28px;
|
||||
}
|
||||
|
||||
/* Hero input */
|
||||
.hero-input-group {
|
||||
display: flex; gap: 8px; margin-bottom: 20px;
|
||||
}
|
||||
.hero-input {
|
||||
flex: 1; padding: 14px 18px; font-size: 16px;
|
||||
font-family: 'JetBrains Mono', 'Courier New', monospace;
|
||||
background: var(--panel); color: var(--text);
|
||||
border: 1px solid var(--border); border-radius: 8px;
|
||||
outline: none; transition: border-color 0.2s;
|
||||
}
|
||||
.hero-input:focus {
|
||||
border-color: var(--hero-accent); box-shadow: 0 0 0 3px var(--hero-glow);
|
||||
}
|
||||
.hero-input::placeholder { color: #484f58; }
|
||||
.hero-input.shake {
|
||||
animation: shake 0.4s ease;
|
||||
border-color: #f85149;
|
||||
box-shadow: 0 0 0 3px rgba(248,81,73,0.2);
|
||||
}
|
||||
@keyframes shake {
|
||||
0%, 100% { transform: translateX(0); }
|
||||
20%, 60% { transform: translateX(-6px); }
|
||||
40%, 80% { transform: translateX(6px); }
|
||||
}
|
||||
.hero-btn {
|
||||
padding: 14px 28px; font-size: 16px; font-weight: 600;
|
||||
font-family: 'Inter', sans-serif;
|
||||
background: var(--hero-accent); color: #fff; border: none; border-radius: 8px;
|
||||
cursor: pointer; transition: background 0.2s, transform 0.1s;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.hero-btn:hover { filter: brightness(0.85); transform: translateY(-1px); }
|
||||
.hero-btn:active { transform: translateY(0); }
|
||||
|
||||
/* Example buttons */
|
||||
.hero-examples { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
||||
.hero-examples-label { color: #6e7681; font-size: 14px; margin-right: 4px; }
|
||||
.example-btn {
|
||||
padding: 8px 16px; font-size: 13px; font-family: 'Inter', sans-serif;
|
||||
background: transparent; color: var(--accent);
|
||||
border: 1px solid var(--border); border-radius: 6px;
|
||||
cursor: pointer; transition: all 0.2s;
|
||||
}
|
||||
.example-btn:hover {
|
||||
border-color: var(--accent); background: rgba(88,166,255,0.08);
|
||||
}
|
||||
|
||||
/* Hero orb */
|
||||
.hero-orb-wrapper {
|
||||
display: flex; justify-content: center; align-items: center;
|
||||
}
|
||||
.hero-orb {
|
||||
width: 340px; height: 340px; border-radius: 50%;
|
||||
background: radial-gradient(circle at 30% 30%, var(--hero-glow) 0%, transparent 70%);
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
animation: orb-float 6s ease-in-out infinite;
|
||||
}
|
||||
.hero-orb-img {
|
||||
width: 100%; height: 100%; object-fit: contain;
|
||||
filter: drop-shadow(0 0 40px var(--hero-glow));
|
||||
transition: opacity 0.2s ease;
|
||||
}
|
||||
@keyframes orb-float {
|
||||
0%, 100% { transform: translateY(0); }
|
||||
50% { transform: translateY(-12px); }
|
||||
}
|
||||
|
||||
/* How section */
|
||||
.how-section {
|
||||
padding: 60px 40px;
|
||||
background: rgba(22,27,34,0.6);
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
.how-container { max-width: 900px; margin: 0 auto; }
|
||||
.how-title {
|
||||
text-align: center; font-size: 1.5rem; font-weight: 700;
|
||||
color: #e6edf3; margin-bottom: 40px;
|
||||
}
|
||||
.how-steps {
|
||||
display: grid; grid-template-columns: repeat(3, 1fr); gap: 32px;
|
||||
}
|
||||
.how-step {
|
||||
text-align: center; padding: 24px;
|
||||
background: var(--panel); border: 1px solid var(--border);
|
||||
border-radius: 12px; transition: border-color 0.3s;
|
||||
}
|
||||
.how-step:hover { border-color: var(--hero-accent); }
|
||||
.how-step-num {
|
||||
width: 40px; height: 40px; line-height: 40px;
|
||||
border-radius: 50%; background: var(--hero-glow);
|
||||
color: var(--hero-accent); font-weight: 700; font-size: 18px;
|
||||
margin: 0 auto 14px;
|
||||
}
|
||||
.how-step h3 { color: #e6edf3; font-size: 1rem; margin-bottom: 8px; }
|
||||
.how-step p { color: #8b949e; font-size: 0.9rem; line-height: 1.5; }
|
||||
|
||||
/* Landing footer */
|
||||
.landing-footer {
|
||||
text-align: center; padding: 32px 40px;
|
||||
color: #484f58; font-size: 13px;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
.landing-footer a { color: #8b949e; }
|
||||
|
||||
/* Responsive */
|
||||
@media (max-width: 860px) {
|
||||
.hero-container { grid-template-columns: 1fr; gap: 32px; }
|
||||
.hero-orb-wrapper { order: -1; }
|
||||
.hero-orb { width: 220px; height: 220px; }
|
||||
.how-steps { grid-template-columns: 1fr; }
|
||||
.hero-input-group { flex-direction: column; }
|
||||
}
|
||||
|
||||
/* ===== OPPIMISPOLKU ===== */
|
||||
.learn-step {
|
||||
margin: 12px 0; border: 1px solid var(--border);
|
||||
border-radius: 8px; background: var(--panel); overflow: hidden;
|
||||
}
|
||||
.learn-step-header {
|
||||
display: flex; align-items: center; gap: 12px;
|
||||
padding: 12px 16px; cursor: pointer;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
.learn-step-header:hover { background: rgba(88,166,255,0.04); }
|
||||
.learn-step-num {
|
||||
width: 28px; height: 28px; line-height: 28px; text-align: center;
|
||||
border-radius: 50%; background: var(--hero-glow);
|
||||
color: var(--hero-accent); font-weight: 700; font-size: 13px; flex-shrink: 0;
|
||||
}
|
||||
.learn-step-agent {
|
||||
font-weight: 600; color: #e6edf3; font-size: 14px;
|
||||
}
|
||||
.learn-step-label {
|
||||
color: #8b949e; font-size: 13px; margin-left: auto;
|
||||
}
|
||||
.learn-step-body {
|
||||
display: none; padding: 0 16px 16px;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
.learn-step-body.open { display: block; }
|
||||
.learn-section-title {
|
||||
color: var(--accent); font-size: 12px; font-weight: 600;
|
||||
text-transform: uppercase; letter-spacing: 0.5px;
|
||||
margin: 14px 0 6px;
|
||||
}
|
||||
.learn-code {
|
||||
font-family: 'JetBrains Mono', 'Courier New', monospace;
|
||||
font-size: 12px; line-height: 1.6;
|
||||
background: #010409; border: 1px solid var(--border);
|
||||
border-radius: 6px; padding: 12px; overflow-x: auto;
|
||||
max-height: 300px; overflow-y: auto; white-space: pre-wrap;
|
||||
}
|
||||
|
||||
/* Animations */
|
||||
@keyframes blink { 0%,100% { opacity:1 } 50% { opacity:0 } }
|
||||
@keyframes spin { to { transform: rotate(360deg) } }
|
||||
|
||||
@@ -42,10 +42,12 @@ struct AppState {
|
||||
node_types: Mutex<HashMap<u64, String>>, // node_id → "native" | "browser"
|
||||
node_paused: Mutex<std::collections::HashSet<u64>>, // node_id → onko tauolla
|
||||
node_busy: Mutex<std::collections::HashSet<u64>>, // Solmut joilla on aktiivinen tehtävä
|
||||
node_active_task: Mutex<HashMap<u64, String>>, // node_id → task_id (mikä tehtävä on kesken)
|
||||
pending_task_ids: Mutex<std::collections::HashSet<String>>, // Hubin jakamat task_id:t (gamification-validointi)
|
||||
pending_responses: Mutex<HashMap<String, tokio::sync::oneshot::Sender<serde_json::Value>>>, // task_id → oneshot API-vastaukselle
|
||||
api_rate_limits: Mutex<HashMap<IpAddr, (std::time::Instant, u32)>>, // IP → (ikkuna-alku, pyyntömäärä)
|
||||
node_models: tokio::sync::RwLock<HashMap<u64, serde_json::Value>>, // node_id → ollama tags JSON
|
||||
node_max_param_b: tokio::sync::RwLock<HashMap<u64, u32>>, // node_id → suurimman mallin parametrit (B)
|
||||
db: db::NodeDb,
|
||||
}
|
||||
|
||||
@@ -329,10 +331,12 @@ async fn main() {
|
||||
node_types: Mutex::new(HashMap::new()),
|
||||
node_paused: Mutex::new(std::collections::HashSet::new()),
|
||||
node_busy: Mutex::new(std::collections::HashSet::new()),
|
||||
node_active_task: Mutex::new(HashMap::new()),
|
||||
pending_task_ids: Mutex::new(std::collections::HashSet::new()),
|
||||
pending_responses: Mutex::new(HashMap::new()),
|
||||
api_rate_limits: Mutex::new(HashMap::new()),
|
||||
node_models: tokio::sync::RwLock::new(HashMap::new()),
|
||||
node_max_param_b: tokio::sync::RwLock::new(HashMap::new()),
|
||||
db: db::NodeDb::new(&std::env::var("DATABASE_PATH").unwrap_or_else(|_| "nodes.db".to_string())),
|
||||
});
|
||||
|
||||
@@ -844,10 +848,34 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
node_id, ip, hostname, os, cores, ram, allocated
|
||||
);
|
||||
|
||||
// Tallennetaan välitetyt mallit muistiin
|
||||
// Tallennetaan välitetyt mallit muistiin + parsitaan suurin malli
|
||||
if let Some(models) = json.get("models") {
|
||||
let mut nm = state.node_models.write().await;
|
||||
nm.insert(node_id, models.clone());
|
||||
|
||||
// Parsitaan suurin mallikoko (B) nimestä: "qwen3:32b" → 32, "qwen2.5-coder:7b" → 7
|
||||
let max_b = models.get("models").and_then(|v| v.as_array()).map(|arr| {
|
||||
arr.iter().filter_map(|m| {
|
||||
let name = m.get("name")?.as_str()?;
|
||||
// Etsitään :N tai :Nb tai -Nb muoto
|
||||
let lower = name.to_lowercase();
|
||||
for part in lower.split(&[':', '-'][..]) {
|
||||
if let Some(num_str) = part.strip_suffix('b') {
|
||||
if let Ok(n) = num_str.parse::<f32>() { return Some(n as u32); }
|
||||
} else if let Ok(n) = part.parse::<f32>() {
|
||||
if n >= 0.5 && n <= 500.0 { return Some(n as u32); }
|
||||
}
|
||||
}
|
||||
// Fallback: koko tiedostosta (size / ~0.5GB per B param Q4)
|
||||
let size = m.get("size")?.as_u64()?;
|
||||
Some((size / 500_000_000) as u32) // karkea arvio
|
||||
}).max().unwrap_or(0)
|
||||
}).unwrap_or(0);
|
||||
|
||||
if max_b > 0 {
|
||||
state.node_max_param_b.write().await.insert(node_id, max_b);
|
||||
tracing::info!("Solmu {} — suurin malli: ~{}B parametria", node_id, max_b);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(gpus) = json.get("gpus").and_then(|v| v.as_array()) {
|
||||
@@ -908,6 +936,7 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
broadcast_stats(&state).await;
|
||||
} else if msg_type == "pair_done" {
|
||||
state.node_busy.lock().unwrap().remove(&node_id);
|
||||
state.node_active_task.lock().unwrap().remove(&node_id);
|
||||
{
|
||||
let mut json = json; // Siirretään omistajuus muokkausta varten
|
||||
if let Some(obj) = json.as_object_mut() {
|
||||
@@ -994,6 +1023,7 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
} else if msg_type == "llm_done" {
|
||||
// Vapautetaan solmu ja tarkistetaan task_id:n aitous
|
||||
state.node_busy.lock().unwrap().remove(&node_id);
|
||||
state.node_active_task.lock().unwrap().remove(&node_id);
|
||||
let task_id = json.get("task_id").and_then(|v| v.as_str()).map(|s| s.to_string());
|
||||
let valid_task = if let Some(ref tid) = task_id {
|
||||
state.pending_task_ids.lock().unwrap().remove(tid.as_str())
|
||||
@@ -1011,15 +1041,15 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
if let Some(obj) = json.as_object_mut() {
|
||||
let model = obj.get("model").and_then(|v| v.as_str()).unwrap_or("?");
|
||||
let prompt = obj.get("prompt").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let response = obj.get("response").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let _response = obj.get("response").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let tok_gen = obj.get("tokens_generated").and_then(|v| v.as_u64()).unwrap_or(0);
|
||||
let duration = obj.get("duration_ms").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
let tok_s = obj.get("tokens_per_sec").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
|
||||
println!();
|
||||
println!("\x1b[35m━━━ Solmu {} ━━━ {} ━━━\x1b[0m", node_id, model);
|
||||
println!(" Prompt: \x1b[33m\"{}\"\x1b[0m", prompt);
|
||||
println!(" Vastaus: \x1b[32m{}\x1b[0m", response);
|
||||
let prompt_preview: String = prompt.chars().take(80).collect();
|
||||
println!(" Prompt: \x1b[33m\"{}...\"\x1b[0m", prompt_preview);
|
||||
println!(" {} tokenia | {:.0}ms | \x1b[36m{:.1} tok/s\x1b[0m", tok_gen, duration, tok_s);
|
||||
|
||||
state.db.increment_tasks(node_id);
|
||||
@@ -1063,6 +1093,7 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
}
|
||||
} else if msg_type == "llm_error" {
|
||||
state.node_busy.lock().unwrap().remove(&node_id);
|
||||
state.node_active_task.lock().unwrap().remove(&node_id);
|
||||
let task_id = json.get("task_id").and_then(|v| v.as_str()).map(|s| s.to_string());
|
||||
if let Some(ref tid) = task_id {
|
||||
state.pending_task_ids.lock().unwrap().remove(tid.as_str());
|
||||
@@ -1109,6 +1140,22 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
|
||||
// Yhteys katkesi — merkitään session päättyneeksi ja siivotaan atomisesti
|
||||
state.db.close_session(node_id);
|
||||
|
||||
// Jos solmulla oli kesken tehtävä, ilmoitetaan odottavalle API-kutsulle
|
||||
let lost_task_id = state.node_active_task.lock().unwrap().remove(&node_id);
|
||||
if let Some(tid) = lost_task_id {
|
||||
tracing::warn!("Solmu {} katosi kesken tehtävän {} — palautetaan virhe API:lle", node_id, tid);
|
||||
state.pending_task_ids.lock().unwrap().remove(&tid);
|
||||
if let Some(resp_tx) = state.pending_responses.lock().unwrap().remove(&tid) {
|
||||
let err = serde_json::json!({
|
||||
"type": "llm_error",
|
||||
"error": format!("Solmu #{} katosi kesken laskennan (task {})", node_id, tid),
|
||||
"task_id": tid
|
||||
});
|
||||
let _ = resp_tx.send(err);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Lukitaan kaikki kerralla, jotta solmu ei ole osittain siivottu
|
||||
let mut tasks = state.node_tasks.lock().unwrap();
|
||||
@@ -1128,6 +1175,7 @@ async fn handle_socket(socket: WebSocket, state: Arc<AppState>, ip: IpAddr) {
|
||||
state.node_types.lock().unwrap().remove(&node_id);
|
||||
state.node_paused.lock().unwrap().remove(&node_id);
|
||||
state.node_models.write().await.remove(&node_id);
|
||||
state.node_max_param_b.write().await.remove(&node_id);
|
||||
tracing::info!("Solmu {} ({}) poistui verkosta.", node_id, ip);
|
||||
broadcast_stats(&state).await;
|
||||
sender_task.abort();
|
||||
@@ -1149,6 +1197,8 @@ struct ChatCompletionRequest {
|
||||
repeat_penalty: Option<f64>,
|
||||
#[serde(default)]
|
||||
stop: Option<Vec<String>>,
|
||||
#[serde(default)]
|
||||
capability: Option<String>, // "heavy" → priorisoi isoin malli, "light" → mikä tahansa
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
@@ -1258,15 +1308,26 @@ async fn api_chat_completions(
|
||||
}
|
||||
}
|
||||
|
||||
// Etsitään vapaa solmu — priorisoidaan natiivisolmut (GPU) selaimen edelle
|
||||
// Etsitään vapaa solmu — älykäs reititys kyvykkyyden mukaan
|
||||
let want_heavy = payload.capability.as_deref() == Some("heavy");
|
||||
// Haetaan param_b-snapshot ennen Mutex-lukituksia (async RwLock ei saa olla Mutex-scopen sisällä)
|
||||
let param_b_snapshot: HashMap<u64, u32> = state.node_max_param_b.read().await.clone();
|
||||
let (target_node, _total_matching) = {
|
||||
let tasks = state.node_tasks.lock().unwrap();
|
||||
let _busy = state.node_busy.lock().unwrap();
|
||||
let busy = state.node_busy.lock().unwrap();
|
||||
let node_types = state.node_types.lock().unwrap();
|
||||
let paused = state.node_paused.lock().unwrap();
|
||||
// Debug: logita kaikki solmut ja niiden tilat
|
||||
let all_nodes: Vec<String> = tasks.iter().map(|(id, task)| {
|
||||
let ty = node_types.get(id).map(|s| s.as_str()).unwrap_or("?");
|
||||
let b = if busy.contains(id) { " BUSY" } else { "" };
|
||||
let p = if paused.contains(id) { " PAUSED" } else { "" };
|
||||
format!("#{}({}:{}{}{}", id, ty, task, b, p)
|
||||
}).collect();
|
||||
tracing::info!("Reititys '{}'{} — solmut: [{}]", payload.model, if want_heavy { " (heavy)" } else { "" }, all_nodes.join(", "));
|
||||
let matching: Vec<u64> = tasks.iter().filter(|(k, task)| {
|
||||
if paused.contains(k) { return false; } // Ei sallita tauotettuja
|
||||
// Eksakti match tai qwen-perheen yhteensopivuus (selain: qwen-coder-05b, natiivi: qwen2.5-coder:7b)
|
||||
if paused.contains(k) { return false; } // Ei tauotettuja
|
||||
if busy.contains(k) { return false; } // Ei varattuja
|
||||
let req_model = payload.model.to_lowercase();
|
||||
let node_task = task.to_lowercase();
|
||||
if req_model.starts_with("qwen") {
|
||||
@@ -1277,11 +1338,32 @@ async fn api_chat_completions(
|
||||
**task == payload.model
|
||||
}
|
||||
}).map(|(k, _)| *k).collect();
|
||||
// Etsitään mikä tahansa matchaava solmu (natiivi priorisoidaan)
|
||||
let native = matching.iter().find(|id| {
|
||||
node_types.get(id).map(|t| t == "native").unwrap_or(false)
|
||||
}).copied();
|
||||
let any = native.or_else(|| matching.first().copied());
|
||||
|
||||
let any = if want_heavy {
|
||||
// Heavy: priorisoi solmu jolla on suurin malli (B-parametrit)
|
||||
let mut ranked: Vec<(u64, u32)> = matching.iter().map(|id| {
|
||||
(*id, param_b_snapshot.get(id).copied().unwrap_or(0))
|
||||
}).collect();
|
||||
ranked.sort_by(|a, b| b.1.cmp(&a.1)); // suurin ensin
|
||||
if let Some((best_id, best_b)) = ranked.first() {
|
||||
tracing::info!("Heavy-reititys: solmu {} valittu ({}B parametria)", best_id, best_b);
|
||||
Some(*best_id)
|
||||
} else {
|
||||
// Kaikki heavy-solmut busy — fallback mihin tahansa vapaaseen
|
||||
let all_matching: Vec<u64> = tasks.iter().filter(|(k, task)| {
|
||||
if paused.contains(k) || busy.contains(k) { return false; }
|
||||
let req_model = payload.model.to_lowercase();
|
||||
task.to_lowercase().starts_with(&req_model.split('-').next().unwrap_or(""))
|
||||
}).map(|(k, _)| *k).collect();
|
||||
all_matching.first().copied()
|
||||
}
|
||||
} else {
|
||||
// Oletus: vapaa natiivi ensin, sitten mikä tahansa vapaa
|
||||
let native = matching.iter().find(|id| {
|
||||
node_types.get(id).map(|t| t == "native").unwrap_or(false)
|
||||
}).copied();
|
||||
native.or_else(|| matching.first().copied())
|
||||
};
|
||||
(any, matching.len())
|
||||
};
|
||||
|
||||
@@ -1308,6 +1390,7 @@ async fn api_chat_completions(
|
||||
|
||||
// Merkitään solmu varatuksi ja task_id jaetuksi
|
||||
state.node_busy.lock().unwrap().insert(target_node_id);
|
||||
state.node_active_task.lock().unwrap().insert(target_node_id, payload.task_id.clone());
|
||||
state.pending_task_ids.lock().unwrap().insert(payload.task_id.clone());
|
||||
|
||||
let mut msg = serde_json::json!({
|
||||
@@ -1340,7 +1423,7 @@ async fn api_chat_completions(
|
||||
}
|
||||
}
|
||||
|
||||
let timeout = tokio::time::timeout(std::time::Duration::from_secs(600), resp_rx).await;
|
||||
let timeout = tokio::time::timeout(std::time::Duration::from_secs(120), resp_rx).await;
|
||||
|
||||
match timeout {
|
||||
Ok(Ok(v)) => {
|
||||
@@ -1356,12 +1439,17 @@ async fn api_chat_completions(
|
||||
}
|
||||
}
|
||||
Ok(Err(_)) => {
|
||||
// Oneshot-kanava sulkeutui (solmu katosi)
|
||||
// Oneshot-kanava sulkeutui (solmu katosi kesken laskennan)
|
||||
state.pending_responses.lock().unwrap().remove(&payload.task_id);
|
||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Verkkovirhe: yhteys katkesi").into_response()
|
||||
state.node_busy.lock().unwrap().remove(&target_node_id);
|
||||
state.node_active_task.lock().unwrap().remove(&target_node_id);
|
||||
(axum::http::StatusCode::SERVICE_UNAVAILABLE, "Solmu katosi kesken laskennan — yritä uudelleen").into_response()
|
||||
}
|
||||
Err(_) => {
|
||||
// Timeout — solmu ei vastannut ajoissa
|
||||
state.pending_responses.lock().unwrap().remove(&payload.task_id);
|
||||
state.node_busy.lock().unwrap().remove(&target_node_id);
|
||||
state.node_active_task.lock().unwrap().remove(&target_node_id);
|
||||
(axum::http::StatusCode::GATEWAY_TIMEOUT, "Aikakatkaisu: solmu ei saanut tehtävää ajoissa valmiiksi").into_response()
|
||||
}
|
||||
}
|
||||
|
||||
135
network-poc/kipina-node
Normal file
135
network-poc/kipina-node
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/bin/bash
|
||||
# Kipinä Node — lataa oikea binääri ja käynnistä
|
||||
set -e
|
||||
|
||||
BASE_URL="https://kipina.studio/download"
|
||||
HUB_URL="${KIPINA_HUB:-wss://kipina.studio/ws}"
|
||||
OLLAMA_URL="${OLLAMA_URL:-http://localhost:11434}"
|
||||
|
||||
# Tunnista OS ja arkkitehtuuri
|
||||
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
|
||||
ARCH=$(uname -m)
|
||||
|
||||
case "$OS-$ARCH" in
|
||||
darwin-arm64) BINARY="kipina-node-macos-arm64" ;;
|
||||
darwin-x86_64) BINARY="kipina-node-macos-arm64" ;; # Rosetta
|
||||
linux-x86_64) BINARY="kipina-node-linux-x86_64" ;;
|
||||
linux-aarch64) BINARY="kipina-node-linux-arm64" ;;
|
||||
*) echo "Ei tuettu: $OS-$ARCH"; exit 1 ;;
|
||||
esac
|
||||
|
||||
echo ""
|
||||
echo " ╔══════════════════════════════════════╗"
|
||||
echo " ║ Kipinä Agentic Node ║"
|
||||
echo " ╚══════════════════════════════════════╝"
|
||||
echo ""
|
||||
echo " OS: $OS ($ARCH)"
|
||||
echo ""
|
||||
|
||||
# Etsi Ollama-instanssit
|
||||
CANDIDATES=(
|
||||
"http://localhost:11434"
|
||||
"http://127.0.0.1:11434"
|
||||
"http://ollama:11434"
|
||||
"http://host.docker.internal:11434"
|
||||
)
|
||||
|
||||
# Lisää OLLAMA_URL listaan jos asetettu ja ei jo mukana
|
||||
if [ -n "$OLLAMA_URL" ]; then
|
||||
ALREADY=false
|
||||
for c in "${CANDIDATES[@]}"; do
|
||||
[ "$c" = "$OLLAMA_URL" ] && ALREADY=true
|
||||
done
|
||||
$ALREADY || CANDIDATES=("$OLLAMA_URL" "${CANDIDATES[@]}")
|
||||
fi
|
||||
|
||||
echo " Etsitään Ollama-instansseja..."
|
||||
FOUND=()
|
||||
for url in "${CANDIDATES[@]}"; do
|
||||
if curl -s --connect-timeout 1 "$url/api/tags" &>/dev/null; then
|
||||
FOUND+=("$url")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ${#FOUND[@]} -eq 0 ]; then
|
||||
# Ei löytynyt — yritä käynnistää lokaali
|
||||
if command -v ollama &>/dev/null; then
|
||||
echo " Käynnistetään Ollama..."
|
||||
ollama serve &>/dev/null &
|
||||
sleep 3
|
||||
if curl -s --connect-timeout 1 "http://localhost:11434/api/tags" &>/dev/null; then
|
||||
OLLAMA_URL="http://localhost:11434"
|
||||
echo " ✓ Ollama käynnistetty ($OLLAMA_URL)"
|
||||
else
|
||||
echo " ✗ Ollaman käynnistys epäonnistui."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo ""
|
||||
echo " ✗ Ollamaa ei löytynyt."
|
||||
echo " Kontti/remote: OLLAMA_URL=http://HOST:11434 ./kipina-node"
|
||||
echo " Asenna: curl -fsSL https://ollama.ai/install.sh | sh"
|
||||
exit 1
|
||||
fi
|
||||
elif [ ${#FOUND[@]} -eq 1 ]; then
|
||||
OLLAMA_URL="${FOUND[0]}"
|
||||
echo " ✓ Ollama löytyi: $OLLAMA_URL"
|
||||
else
|
||||
echo ""
|
||||
echo " Löytyi ${#FOUND[@]} Ollama-instanssia:"
|
||||
echo ""
|
||||
for i in "${!FOUND[@]}"; do
|
||||
echo " $((i+1))) ${FOUND[$i]}"
|
||||
done
|
||||
echo ""
|
||||
read -p " Valitse [1-${#FOUND[@]}]: " -r CHOICE
|
||||
if [[ "$CHOICE" =~ ^[0-9]+$ ]] && [ "$CHOICE" -ge 1 ] && [ "$CHOICE" -le ${#FOUND[@]} ]; then
|
||||
OLLAMA_URL="${FOUND[$((CHOICE-1))]}"
|
||||
else
|
||||
OLLAMA_URL="${FOUND[0]}"
|
||||
echo " Käytetään oletusta: $OLLAMA_URL"
|
||||
fi
|
||||
echo " ✓ Valittu: $OLLAMA_URL"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo " Hub: $HUB_URL"
|
||||
echo " Ollama: $OLLAMA_URL"
|
||||
if [ -n "$KIPINA_MODEL" ]; then
|
||||
echo " Malli: $KIPINA_MODEL (Ympäristömuuttujasta)"
|
||||
fi
|
||||
|
||||
# Binäärin automaattinen päivitys — vertaa build-hashia palvelimeen
|
||||
BIN_PATH="./kipina-node-bin"
|
||||
HASH_PATH="./kipina-node-bin.hash"
|
||||
|
||||
REMOTE_HASH=$(curl -sSL "$BASE_URL/.build-hash?v=$(date +%s)" 2>/dev/null | tr -d '[:space:]')
|
||||
LOCAL_HASH=""
|
||||
[ -f "$HASH_PATH" ] && LOCAL_HASH=$(cat "$HASH_PATH" | tr -d '[:space:]')
|
||||
|
||||
if [ -f "$BIN_PATH" ] && [ -n "$REMOTE_HASH" ] && [ "$REMOTE_HASH" = "$LOCAL_HASH" ]; then
|
||||
echo " ✓ Binääri ajan tasalla (versio: $LOCAL_HASH)"
|
||||
else
|
||||
if [ -f "$BIN_PATH" ]; then
|
||||
echo " ↻ Uusi versio saatavilla ($LOCAL_HASH → $REMOTE_HASH)"
|
||||
else
|
||||
echo " Ladataan $BINARY..."
|
||||
fi
|
||||
rm -f "$BIN_PATH"
|
||||
curl -sSL "$BASE_URL/$BINARY?v=$(date +%s)" -o "$BIN_PATH"
|
||||
chmod +x "$BIN_PATH"
|
||||
echo "$REMOTE_HASH" > "$HASH_PATH"
|
||||
echo " ✓ Päivitetty versioon $REMOTE_HASH"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo " ✓ Siirrytään Kipinä Noden hallintaan..."
|
||||
echo " Ctrl+C pysäyttää"
|
||||
echo ""
|
||||
|
||||
if [ -n "$KIPINA_MODEL" ]; then
|
||||
export OLLAMA_MODEL="$KIPINA_MODEL"
|
||||
fi
|
||||
export HUB_URL="$HUB_URL"
|
||||
export OLLAMA_URL="$OLLAMA_URL"
|
||||
exec "$BIN_PATH"
|
||||
@@ -23,3 +23,4 @@ dialoguer = "0.12.0"
|
||||
ratatui = "0.29.0"
|
||||
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
||||
tracing-appender = "0.2.4"
|
||||
chrono = "0.4"
|
||||
|
||||
@@ -69,6 +69,10 @@ impl LlmEngine {
|
||||
self.model.borrow().clone()
|
||||
}
|
||||
|
||||
pub fn ollama_url(&self) -> &str {
|
||||
&self.ollama_url
|
||||
}
|
||||
|
||||
pub fn set_model(&self, new_model: String) {
|
||||
*self.model.borrow_mut() = new_model;
|
||||
}
|
||||
@@ -91,6 +95,32 @@ impl LlmEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Hakee käynnissä olevan mallin VRAM-tilan (ollama ps)
|
||||
pub async fn fetch_ps(&self) -> Result<Option<ModelVramStatus>, String> {
|
||||
let resp = self.client.get(format!("{}/api/ps", self.ollama_url))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Ollama ps: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Ollama ps HTTP {}", resp.status()));
|
||||
}
|
||||
|
||||
let body: serde_json::Value = resp.json().await
|
||||
.map_err(|e| format!("Ollama ps json: {}", e))?;
|
||||
|
||||
let models = body["models"].as_array();
|
||||
if let Some(arr) = models {
|
||||
if let Some(m) = arr.first() {
|
||||
let name = m["name"].as_str().unwrap_or("?").to_string();
|
||||
let size = m["size"].as_u64().unwrap_or(0);
|
||||
let size_vram = m["size_vram"].as_u64().unwrap_or(0);
|
||||
return Ok(Some(ModelVramStatus { name, size, size_vram }));
|
||||
}
|
||||
}
|
||||
Ok(None) // ei ladattua mallia
|
||||
}
|
||||
|
||||
/// Hakee kaikki Ollamaan asennetut mallit
|
||||
pub async fn fetch_models(&self) -> Result<serde_json::Value, String> {
|
||||
let resp = self.client.get(format!("{}/api/tags", self.ollama_url))
|
||||
@@ -126,6 +156,7 @@ impl LlmEngine {
|
||||
"messages": messages,
|
||||
"stream": false,
|
||||
"options": {
|
||||
"num_ctx": 16384,
|
||||
"num_predict": opts.max_tokens,
|
||||
"temperature": opts.temperature.unwrap_or(0.7),
|
||||
"top_k": opts.top_k.unwrap_or(40),
|
||||
@@ -184,3 +215,32 @@ pub struct GenerateResult {
|
||||
pub duration_ms: f64,
|
||||
pub tokens_per_sec: f64,
|
||||
}
|
||||
|
||||
pub struct ModelVramStatus {
|
||||
pub name: String,
|
||||
pub size: u64, // kokonaiskoko (tavuina)
|
||||
pub size_vram: u64, // VRAM:ssa oleva osuus (tavuina)
|
||||
}
|
||||
|
||||
impl ModelVramStatus {
|
||||
pub fn fully_in_vram(&self) -> bool {
|
||||
self.size > 0 && self.size_vram >= self.size
|
||||
}
|
||||
|
||||
pub fn vram_percent(&self) -> f64 {
|
||||
if self.size == 0 { return 0.0; }
|
||||
(self.size_vram as f64 / self.size as f64) * 100.0
|
||||
}
|
||||
|
||||
pub fn display(&self) -> String {
|
||||
let size_gb = self.size as f64 / 1_073_741_824.0;
|
||||
let vram_gb = self.size_vram as f64 / 1_073_741_824.0;
|
||||
if self.fully_in_vram() {
|
||||
format!("✓ {} ({:.1} GB) — 100% GPU", self.name, size_gb)
|
||||
} else if self.size_vram == 0 {
|
||||
format!("✗ {} ({:.1} GB) — 100% CPU", self.name, size_gb)
|
||||
} else {
|
||||
format!("◐ {} ({:.1}/{:.1} GB VRAM, {:.0}% GPU)", self.name, vram_gb, size_gb, self.vram_percent())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,6 +363,48 @@ async fn main() {
|
||||
st.push_log("System", format!("Malli valmis: {}", active_model), None);
|
||||
}
|
||||
|
||||
// Lämmittelykutsu: ladataan malli VRAM:iin ja haetaan VRAM-tila
|
||||
if let Some(ref engine) = llm {
|
||||
{
|
||||
let mut st = tui_state.write().await;
|
||||
st.vram_status = "Ladataan VRAM:iin...".to_string();
|
||||
st.push_log("System", "Ladataan mallia VRAM:iin...".to_string(), None);
|
||||
}
|
||||
// Lyhyt generate-kutsu pakottaa Ollaman lataamaan mallin GPU:lle
|
||||
let _ = engine.generate("hi", &inference::GenerateOptions {
|
||||
max_tokens: 1, system_prompt: None, temperature: Some(0.0),
|
||||
top_k: Some(1), repeat_penalty: None, stop: None,
|
||||
}).await;
|
||||
if let Ok(Some(ps)) = engine.fetch_ps().await {
|
||||
let mut st = tui_state.write().await;
|
||||
st.vram_status = ps.display();
|
||||
st.push_log("System", format!("VRAM: {}", ps.display()), None);
|
||||
}
|
||||
let vram_engine_url = engine.ollama_url().to_string();
|
||||
let vram_state = tui_state.clone();
|
||||
tokio::spawn(async move {
|
||||
let client = reqwest::Client::new();
|
||||
loop {
|
||||
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
|
||||
if let Ok(resp) = client.get(format!("{}/api/ps", vram_engine_url)).send().await {
|
||||
if let Ok(body) = resp.json::<serde_json::Value>().await {
|
||||
if let Some(arr) = body["models"].as_array() {
|
||||
if let Some(m) = arr.first() {
|
||||
let name = m["name"].as_str().unwrap_or("?").to_string();
|
||||
let size = m["size"].as_u64().unwrap_or(0);
|
||||
let size_vram = m["size_vram"].as_u64().unwrap_or(0);
|
||||
let status = inference::ModelVramStatus { name, size, size_vram };
|
||||
vram_state.write().await.vram_status = status.display();
|
||||
} else {
|
||||
vram_state.write().await.vram_status = "Ei ladattua mallia".to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Käynnistetään graafinen TUI vain jos stdin on terminaali (ei taustaprosessina)
|
||||
let ui_state = tui_state.clone();
|
||||
if std::io::stdin().is_terminal() {
|
||||
@@ -401,6 +443,13 @@ async fn main() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Merkitään yhdistetyksi TUI:ssa
|
||||
{
|
||||
let mut st = tui_state.write().await;
|
||||
st.status = "ACTIVE".to_string();
|
||||
st.push_log("Network", "Yhdistetty hubiin".to_string(), None);
|
||||
}
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
cmd = cmd_rx.recv() => {
|
||||
@@ -497,6 +546,18 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Node joined → oma node_id
|
||||
if text.contains(r#""type":"node_joined""#) {
|
||||
if let Ok(msg) = serde_json::from_str::<serde_json::Value>(&text) {
|
||||
if let Some(nid) = msg.get("node_id").and_then(|v| v.as_u64()) {
|
||||
let mut st = tui_state.write().await;
|
||||
if st.node_id.is_none() {
|
||||
st.node_id = Some(nid);
|
||||
st.push_log("Network", format!("Node ID: #{}", nid), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Verkon globaali tila
|
||||
if text.contains(r#""type":"network_status""#) {
|
||||
if let Ok(status) = serde_json::from_str::<serde_json::Value>(&text) {
|
||||
@@ -615,9 +676,27 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Yhteys katkesi — nollataan TUI:n busy-tila
|
||||
{
|
||||
let mut st = tui_state.write().await;
|
||||
let lost_task = st.cur_task_id.clone();
|
||||
if let Some(tid) = lost_task {
|
||||
st.push_log("Network", format!("Tehtävä {} keskeytyi yhteyden katketessa", tid), None);
|
||||
}
|
||||
st.cur_task_id = None;
|
||||
st.cur_prompt = None;
|
||||
st.node_id = None;
|
||||
st.status = "RECONNECTING".to_string();
|
||||
st.push_log("Network", "Yhteys hubiin katkesi — yhdistetään uudelleen 5s...".to_string(), None);
|
||||
}
|
||||
tracing::warn!("Yhteys hubiin katkesi — yritetään uudelleen 5s...");
|
||||
}
|
||||
Err(e) => {
|
||||
{
|
||||
let mut st = tui_state.write().await;
|
||||
st.status = "RECONNECTING".to_string();
|
||||
st.push_log("Network", format!("Yhdistäminen epäonnistui: {} — yritetään 5s...", e), None);
|
||||
}
|
||||
tracing::warn!("Hubiin yhdistäminen epäonnistui: {} — yritetään uudelleen 5s...", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crossterm::{
|
||||
event::{self, Event, EventStream, KeyCode},
|
||||
event::{Event, EventStream, KeyCode},
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
@@ -21,6 +21,7 @@ pub struct LogEntry {
|
||||
pub ty: String,
|
||||
pub msg: String,
|
||||
pub speed: Option<f64>,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
pub struct DashboardState {
|
||||
@@ -35,6 +36,8 @@ pub struct DashboardState {
|
||||
pub last_tokens_sec: f64,
|
||||
pub network_active_nodes: usize,
|
||||
pub network_total_tasks: u64,
|
||||
// VRAM-tila (ollama ps)
|
||||
pub vram_status: String,
|
||||
// Mallivalikko
|
||||
pub model_picker_open: bool,
|
||||
pub model_picker_items: Vec<String>,
|
||||
@@ -55,6 +58,7 @@ impl DashboardState {
|
||||
last_tokens_sec: 0.0,
|
||||
network_active_nodes: 1, // oletetaan itsemme
|
||||
network_total_tasks: 0,
|
||||
vram_status: "Haetaan...".to_string(),
|
||||
model_picker_open: false,
|
||||
model_picker_items: Vec::new(),
|
||||
model_picker_idx: 0,
|
||||
@@ -62,7 +66,9 @@ impl DashboardState {
|
||||
}
|
||||
|
||||
pub fn push_log(&mut self, ty: &str, msg: String, speed: Option<f64>) {
|
||||
let now = chrono::Local::now().format("%H:%M:%S").to_string();
|
||||
self.logs.push(LogEntry {
|
||||
timestamp: now,
|
||||
ty: ty.to_string(),
|
||||
msg,
|
||||
speed,
|
||||
@@ -179,7 +185,7 @@ fn ui(f: &mut ratatui::Frame, st: &DashboardState) {
|
||||
let body_chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(7), // Yläosan info ja tehtävä
|
||||
Constraint::Length(8), // Yläosan info ja tehtävä
|
||||
Constraint::Min(0), // Lokit / Chat alas
|
||||
].as_ref())
|
||||
.split(chunks[1]);
|
||||
@@ -192,12 +198,38 @@ fn ui(f: &mut ratatui::Frame, st: &DashboardState) {
|
||||
].as_ref())
|
||||
.split(body_chunks[0]);
|
||||
|
||||
// Vasen paneeli: Laitteisto, Malli & Verkosto
|
||||
let info_text = format!(
|
||||
"🚀 Malli: {}\n💻 Järjestelmä: {}\n📊 Tehdyt: {} | Nopeus: {} t/s\n🌐 Verkosto: {} solmua | {} tehtävää",
|
||||
st.model_name, st.sys_info, st.tasks_completed, st.last_tokens_sec, st.network_active_nodes, st.network_total_tasks
|
||||
);
|
||||
let left_panel = Paragraph::new(info_text)
|
||||
// Vasen paneeli: Laitteisto, Malli & Verkosto — VRAM-rivi värikoodattu
|
||||
let vram_color = if st.vram_status.starts_with('✓') {
|
||||
Color::Green
|
||||
} else if st.vram_status.starts_with('◐') {
|
||||
Color::Yellow
|
||||
} else if st.vram_status.starts_with('✗') {
|
||||
Color::Red
|
||||
} else {
|
||||
Color::DarkGray
|
||||
};
|
||||
|
||||
let info_lines = vec![
|
||||
ratatui::text::Line::from(vec![
|
||||
ratatui::text::Span::raw("🚀 Malli: "),
|
||||
ratatui::text::Span::styled(&st.model_name, Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
|
||||
]),
|
||||
ratatui::text::Line::from(vec![
|
||||
ratatui::text::Span::raw("🎮 VRAM: "),
|
||||
ratatui::text::Span::styled(&st.vram_status, Style::default().fg(vram_color)),
|
||||
]),
|
||||
ratatui::text::Line::from(vec![
|
||||
ratatui::text::Span::raw("💻 Järjestelmä: "),
|
||||
ratatui::text::Span::styled(&st.sys_info, Style::default().fg(Color::White)),
|
||||
]),
|
||||
ratatui::text::Line::from(format!(
|
||||
"📊 Tehdyt: {} | Nopeus: {:.1} t/s", st.tasks_completed, st.last_tokens_sec
|
||||
)),
|
||||
ratatui::text::Line::from(format!(
|
||||
"🌐 Verkosto: {} solmua | {} tehtävää", st.network_active_nodes, st.network_total_tasks
|
||||
)),
|
||||
];
|
||||
let left_panel = Paragraph::new(info_lines)
|
||||
.block(Block::default().title(" Laitteisto ja AI ").borders(Borders::ALL))
|
||||
.style(Style::default().fg(Color::White))
|
||||
.wrap(Wrap { trim: true });
|
||||
@@ -241,6 +273,8 @@ fn ui(f: &mut ratatui::Frame, st: &DashboardState) {
|
||||
};
|
||||
|
||||
ratatui::text::Line::from(vec![
|
||||
ratatui::text::Span::styled(&log.timestamp, Style::default().fg(Color::DarkGray)),
|
||||
ratatui::text::Span::raw(" "),
|
||||
ratatui::text::Span::styled(format!("{: <8}", log.ty), Style::default().fg(ty_color).add_modifier(Modifier::BOLD)),
|
||||
ratatui::text::Span::raw(" | "),
|
||||
ratatui::text::Span::styled(log.msg.clone(), Style::default().fg(Color::White)),
|
||||
|
||||
Binary file not shown.
513
network-poc/tests/model-benchmark.mjs
Normal file
513
network-poc/tests/model-benchmark.mjs
Normal file
@@ -0,0 +1,513 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Kipinä Model Benchmark
|
||||
*
|
||||
* Generoi projekteja eri Ollama-malleilla ja testaa niiden toimivuus.
|
||||
* Käyttö:
|
||||
* node model-benchmark.mjs # kaikki mallit, oletusskenaario
|
||||
* node model-benchmark.mjs --models qwen3:8b,qwen3:30b
|
||||
* node model-benchmark.mjs --ollama http://host:11434
|
||||
* node model-benchmark.mjs --scenarios all # kaikki skenaariot
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import { writeFileSync, mkdirSync, rmSync, existsSync } from 'fs';
|
||||
|
||||
// === CLI-argumentit ===
|
||||
const args = process.argv.slice(2);
|
||||
function arg(name, fallback) {
|
||||
const i = args.indexOf(`--${name}`);
|
||||
return i >= 0 && args[i + 1] ? args[i + 1] : fallback;
|
||||
}
|
||||
const OLLAMA_URL = arg('ollama', process.env.OLLAMA_URL || 'http://localhost:11434');
|
||||
const HUB_URL = arg('hub', ''); // Vaihtoehto: --hub https://kipina.studio
|
||||
const FILTER_MODELS = arg('models', '');
|
||||
const SCENARIO_FILTER = arg('scenarios', 'default');
|
||||
const OUTPUT_DIR = arg('output', '/tmp/kipina-benchmark');
|
||||
const MAX_FIX_ROUNDS = 2;
|
||||
|
||||
// === Ollama / Hub -client ===
|
||||
async function ollamaChat(model, prompt, systemPrompt, maxTokens = 2048) {
|
||||
const start = Date.now();
|
||||
|
||||
if (HUB_URL) {
|
||||
// Hub-reitti: /api/v1/chat/completions
|
||||
const taskId = `bench-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;
|
||||
const resp = await fetch(`${HUB_URL}/api/v1/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ model, prompt, task_id: taskId, system_prompt: systemPrompt, max_tokens: maxTokens }),
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Hub HTTP ${resp.status}: ${await resp.text()}`);
|
||||
const data = await resp.json();
|
||||
const elapsed = Date.now() - start;
|
||||
return {
|
||||
text: (data.response || '').trim(),
|
||||
tokens: data.tokens_generated || 0,
|
||||
durationMs: elapsed,
|
||||
tokPerSec: data.tokens_per_sec || (data.tokens_generated || 0) / (elapsed / 1000),
|
||||
};
|
||||
}
|
||||
|
||||
// Suora Ollama-reitti: /api/chat
|
||||
const messages = [];
|
||||
if (systemPrompt) messages.push({ role: 'system', content: systemPrompt });
|
||||
messages.push({ role: 'user', content: prompt });
|
||||
|
||||
const resp = await fetch(`${OLLAMA_URL}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages,
|
||||
stream: false,
|
||||
options: { num_predict: maxTokens, temperature: 0.7, top_k: 40, repeat_penalty: 1.15 },
|
||||
}),
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Ollama HTTP ${resp.status}: ${await resp.text()}`);
|
||||
const data = await resp.json();
|
||||
const elapsed = Date.now() - start;
|
||||
const text = (data.message?.content || '').trim();
|
||||
const evalCount = data.eval_count || 0;
|
||||
const evalDurationNs = data.eval_duration || 1;
|
||||
const tokPerSec = evalCount / (evalDurationNs / 1e9);
|
||||
return { text, tokens: evalCount, durationMs: elapsed, tokPerSec };
|
||||
}
|
||||
|
||||
async function ollamaListModels() {
|
||||
const url = HUB_URL ? `${HUB_URL}/api/v1/ollama/tags` : `${OLLAMA_URL}/api/tags`;
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) throw new Error(`Tags: HTTP ${resp.status}`);
|
||||
const data = await resp.json();
|
||||
return (data.models || []).map(m => m.name);
|
||||
}
|
||||
|
||||
// === Promptit (kopioitu index.astrosta) ===
|
||||
const CLIENT_SYSTEM = `You are a product owner who turns vague ideas into clear, actionable software requirements.
|
||||
|
||||
GIVEN a short project description from the user, produce a structured brief:
|
||||
|
||||
1. PROJECT NAME: a short, descriptive name
|
||||
2. GOAL: one sentence explaining what the software does and who it's for
|
||||
3. CORE FEATURES: numbered list of 3-8 concrete features (not vague wishes)
|
||||
4. DATA MODEL: list the main entities and their key fields (include field types)
|
||||
5. API ENDPOINTS: list the REST endpoints (method + path + purpose)
|
||||
6. CONSTRAINTS: any technical constraints (e.g. "must use SQLite", "no auth needed")
|
||||
|
||||
RULES:
|
||||
- Be specific: "User can filter todos by status" not "todo management"
|
||||
- Use plain English, no code
|
||||
- Maximum 400 words total`;
|
||||
|
||||
const SPEC_SYSTEM = `You are a software architect who designs database schemas for Python web applications.
|
||||
|
||||
THINK STEP BY STEP before outputting JSON:
|
||||
1. What are the main ENTITIES (nouns) in this project?
|
||||
2. What FIELDS does each entity need? (name, type, required?)
|
||||
3. Which entities REFERENCE each other? (e.g. "a Book belongs to an Author" → Book has author_id)
|
||||
4. Are there Date/DateTime fields? → add extra_imports
|
||||
|
||||
Then output ONLY valid JSON (no explanations before or after).
|
||||
|
||||
SCHEMA:
|
||||
{"project_name":"short-name","description":"One sentence","entities":[{"name":"EntityName","table_name":"entity_names","fields":[{"name":"field_name","sa_type":"String(255)","py_type":"str","nullable":false,"default":null}]}],"relationships":[{"from":"ChildEntity","field":"parent_id","to":"ParentEntity","type":"many-to-one"}],"extra_imports":[]}
|
||||
|
||||
FIELD RULES:
|
||||
- sa_type: String(N), Text, Integer, Date, DateTime, Boolean, Float
|
||||
- py_type: str, int, float, bool, date, datetime — append " | None" if nullable
|
||||
- Status fields: use String(20) with default value, NEVER Enum
|
||||
- Every entity gets "id" automatically — do NOT add id or redundant ID fields
|
||||
- Use snake_case for field names
|
||||
|
||||
RELATIONSHIP RULES:
|
||||
- If entity A "belongs to" entity B → A has b_id field (Integer, nullable=false) + relationship entry
|
||||
- EVERY _id field MUST have a matching relationship entry
|
||||
- Parent entities must appear BEFORE children in the entities array
|
||||
- If no relationships, set "relationships": []
|
||||
|
||||
AVOID: redundant ID fields, generic names, more than 7 fields or 3 entities, non-English entity/field names (ALWAYS English even if description is Finnish)
|
||||
|
||||
EXAMPLES (adapt, don't copy):
|
||||
Todo app → Todo: title(str), description(Text|None), due_date(Date|None), status(String20="pending")
|
||||
Blog → Author: name,email,bio(Text|None) / Post: title, content(Text), author_id→Author, published_at(DateTime|None), status(String20="draft")`;
|
||||
|
||||
const FIX_SYSTEM = 'You are a Python code fixer. Return ONLY the corrected Python file. No markdown fences, no explanations — just valid Python code.';
|
||||
|
||||
// === Template-funktiot (kopioitu korjatusta index.astrosta) ===
|
||||
function pyLiteral(val) {
|
||||
if (val === true) return 'True';
|
||||
if (val === false) return 'False';
|
||||
if (val === null || val === undefined) return 'None';
|
||||
if (typeof val === 'string') return `"${val}"`;
|
||||
return String(val);
|
||||
}
|
||||
function pyJsonLiteral(obj) {
|
||||
const parts = Object.entries(obj).map(([k, v]) => {
|
||||
let pyVal;
|
||||
if (v === true) pyVal = 'True'; else if (v === false) pyVal = 'False';
|
||||
else if (v === null) pyVal = 'None'; else if (typeof v === 'string') pyVal = `"${v}"`;
|
||||
else pyVal = String(v);
|
||||
return `"${k}":${pyVal}`;
|
||||
});
|
||||
return '{' + parts.join(',') + '}';
|
||||
}
|
||||
function tmplModels(spec) {
|
||||
const saTypes = new Set(['Integer']);
|
||||
for (const e of spec.entities) for (const f of e.fields) saTypes.add(f.sa_type.match(/^(\w+)/)[1]);
|
||||
const relMap = {};
|
||||
for (const r of (spec.relationships || [])) {
|
||||
const target = spec.entities.find(e => e.name === r.to);
|
||||
if (target) relMap[`${r.from}.${r.field}`] = target.table_name;
|
||||
}
|
||||
if (Object.keys(relMap).length > 0) saTypes.add('ForeignKey');
|
||||
const imports = [...saTypes].sort().join(', ');
|
||||
let code = `from sqlalchemy import create_engine, Column, ${imports}\nfrom sqlalchemy.orm import declarative_base, sessionmaker\n\nDATABASE_URL = "sqlite:///./app.db"\nengine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})\nSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)\nBase = declarative_base()\n\n`;
|
||||
for (const e of spec.entities) {
|
||||
code += `class ${e.name}(Base):\n __tablename__ = "${e.table_name}"\n id = Column(Integer, primary_key=True, index=True)\n`;
|
||||
for (const f of e.fields) {
|
||||
const fkTarget = relMap[`${e.name}.${f.name}`];
|
||||
let parts = fkTarget ? [`Column(${f.sa_type}, ForeignKey("${fkTarget}.id")`] : [`Column(${f.sa_type}`];
|
||||
if (!f.nullable) parts.push('nullable=False');
|
||||
if (f.default !== null && f.default !== undefined) parts.push(`default=${pyLiteral(f.default)}`);
|
||||
code += ` ${f.name} = ${parts.join(', ')})\n`;
|
||||
}
|
||||
code += '\n';
|
||||
}
|
||||
code += 'Base.metadata.create_all(bind=engine)\n';
|
||||
return code;
|
||||
}
|
||||
function tmplSchemas(spec) {
|
||||
const dtTypes = new Set();
|
||||
for (const e of spec.entities) for (const f of e.fields) {
|
||||
if (/\bdate\b/i.test(f.py_type) && !/datetime/.test(f.py_type)) dtTypes.add('date');
|
||||
if (/\bdatetime\b/i.test(f.py_type)) dtTypes.add('datetime');
|
||||
}
|
||||
let code = 'from pydantic import BaseModel, ConfigDict\n';
|
||||
if (dtTypes.size > 0) code += `from datetime import ${[...dtTypes].sort().join(', ')}\n`;
|
||||
for (const imp of (spec.extra_imports || [])) {
|
||||
if (/^(date|datetime)$/.test(imp.trim())) continue;
|
||||
if (/^from\s/.test(imp) || /^import\s/.test(imp)) code += imp + '\n';
|
||||
}
|
||||
code += '\n';
|
||||
for (const e of spec.entities) {
|
||||
code += `class ${e.name}Create(BaseModel):\n`;
|
||||
for (const f of e.fields) {
|
||||
if (f.default !== null && f.default !== undefined) code += ` ${f.name}: ${f.py_type} = ${pyLiteral(f.default)}\n`;
|
||||
else if (f.nullable && f.py_type.includes('None')) code += ` ${f.name}: ${f.py_type} = None\n`;
|
||||
else code += ` ${f.name}: ${f.py_type}\n`;
|
||||
}
|
||||
code += `\nclass ${e.name}Response(${e.name}Create):\n id: int\n model_config = ConfigDict(from_attributes=True)\n\n`;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
function tmplMain(spec) {
|
||||
const modelNames = spec.entities.map(e => e.name).join(', ');
|
||||
const createNames = spec.entities.map(e => e.name+'Create').join(', ');
|
||||
const responseNames = spec.entities.map(e => e.name+'Response').join(', ');
|
||||
let code = `from fastapi import FastAPI, Depends, HTTPException\nfrom sqlalchemy.orm import Session\nfrom models import Base, engine, SessionLocal, ${modelNames}\nfrom schemas import ${createNames}, ${responseNames}\n\napp = FastAPI()\n\ndef get_db():\n db = SessionLocal()\n try:\n yield db\n finally:\n db.close()\n\n`;
|
||||
for (const e of spec.entities) {
|
||||
const lo = e.name.toLowerCase(), tb = e.table_name;
|
||||
code += `@app.post("/${tb}/", response_model=${e.name}Response, status_code=201)\ndef create_${lo}(item: ${e.name}Create, db: Session = Depends(get_db)):\n db_item = ${e.name}(**item.model_dump())\n db.add(db_item)\n db.commit()\n db.refresh(db_item)\n return db_item\n\n`;
|
||||
code += `@app.get("/${tb}/", response_model=list[${e.name}Response])\ndef list_${lo}s(db: Session = Depends(get_db)):\n return db.query(${e.name}).all()\n\n`;
|
||||
code += `@app.get("/${tb}/{item_id}", response_model=${e.name}Response)\ndef get_${lo}(item_id: int, db: Session = Depends(get_db)):\n item = db.query(${e.name}).filter(${e.name}.id == item_id).first()\n if not item:\n raise HTTPException(status_code=404, detail="${e.name} not found")\n return item\n\n`;
|
||||
code += `@app.put("/${tb}/{item_id}", response_model=${e.name}Response)\ndef update_${lo}(item_id: int, item: ${e.name}Create, db: Session = Depends(get_db)):\n db_item = db.query(${e.name}).filter(${e.name}.id == item_id).first()\n if not db_item:\n raise HTTPException(status_code=404, detail="${e.name} not found")\n for key, value in item.model_dump().items():\n setattr(db_item, key, value)\n db.commit()\n db.refresh(db_item)\n return db_item\n\n`;
|
||||
code += `@app.delete("/${tb}/{item_id}", status_code=204)\ndef delete_${lo}(item_id: int, db: Session = Depends(get_db)):\n db_item = db.query(${e.name}).filter(${e.name}.id == item_id).first()\n if not db_item:\n raise HTTPException(status_code=404, detail="${e.name} not found")\n db.delete(db_item)\n db.commit()\n\n`;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
function tmplTests(spec) {
|
||||
let code = `from fastapi.testclient import TestClient\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nfrom main import app, get_db\nfrom models import Base\n\nTEST_DB = "sqlite:///./test.db"\ntest_engine = create_engine(TEST_DB, connect_args={"check_same_thread": False})\nTestSession = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)\nBase.metadata.create_all(bind=test_engine)\n\ndef override_get_db():\n db = TestSession()\n try:\n yield db\n finally:\n db.close()\n\napp.dependency_overrides[get_db] = override_get_db\nclient = TestClient(app)\n\n`;
|
||||
for (const e of spec.entities) {
|
||||
const lo = e.name.toLowerCase(), tb = e.table_name;
|
||||
const testData = {};
|
||||
for (const f of e.fields) {
|
||||
if (f.default !== null && f.default !== undefined) { testData[f.name] = f.default; continue; }
|
||||
if (f.py_type.includes('str')) testData[f.name] = `Test ${f.name}`;
|
||||
else if (f.py_type.includes('int')) testData[f.name] = 1;
|
||||
else if (f.py_type.includes('float')) testData[f.name] = 1.0;
|
||||
else if (f.py_type.includes('bool')) testData[f.name] = true;
|
||||
else if (f.py_type.includes('date')) testData[f.name] = '2024-01-15';
|
||||
}
|
||||
const td = pyJsonLiteral(testData);
|
||||
const firstStr = e.fields.find(f => f.py_type.includes('str') && f.name !== 'status');
|
||||
const updateData = {...testData};
|
||||
if (firstStr) updateData[firstStr.name] = `Updated ${firstStr.name}`;
|
||||
const ud = pyJsonLiteral(updateData);
|
||||
code += `def test_create_${lo}():\n response = client.post('/${tb}/', json=${td})\n assert response.status_code == 201\n assert 'id' in response.json()\n\n`;
|
||||
code += `def test_list_${lo}s():\n client.post('/${tb}/', json=${td})\n response = client.get('/${tb}/')\n assert response.status_code == 200\n assert len(response.json()) >= 1\n\n`;
|
||||
code += `def test_get_${lo}_by_id():\n created = client.post('/${tb}/', json=${td}).json()\n item_id = created['id']\n response = client.get(f'/${tb}/{item_id}')\n assert response.status_code == 200\n assert response.json()['id'] == item_id\n\n`;
|
||||
code += `def test_get_${lo}_not_found():\n response = client.get('/${tb}/99999')\n assert response.status_code == 404\n\n`;
|
||||
code += `def test_update_${lo}():\n created = client.post('/${tb}/', json=${td}).json()\n item_id = created['id']\n response = client.put(f'/${tb}/{item_id}', json=${ud})\n assert response.status_code == 200\n\n`;
|
||||
code += `def test_delete_${lo}():\n created = client.post('/${tb}/', json=${td}).json()\n item_id = created['id']\n response = client.delete(f'/${tb}/{item_id}')\n assert response.status_code == 204\n response = client.get(f'/${tb}/{item_id}')\n assert response.status_code == 404\n\n`;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
function tmplPyproject(spec) {
|
||||
const name = (spec.project_name || 'app').toLowerCase().replace(/\s+/g, '-');
|
||||
return `[project]\nname = "${name}"\nversion = "0.1.0"\nrequires-python = ">=3.11"\ndependencies = [\n "fastapi",\n "uvicorn[standard]",\n "sqlalchemy",\n "pytest",\n "httpx",\n]\n`;
|
||||
}
|
||||
|
||||
// === Validaattori ===
|
||||
function validateProjectCode(files) {
|
||||
const issues = [];
|
||||
for (const [fname, code] of Object.entries(files)) {
|
||||
if (!fname.endsWith('.py')) continue;
|
||||
const lines = code.split('\n');
|
||||
for (const line of lines) {
|
||||
const m = line.match(/^from\s+\.(\w*)\s+import/);
|
||||
if (m) issues.push(`ISSUE: ${fname}: relatiivinen import`);
|
||||
}
|
||||
for (const line of lines) {
|
||||
const m = line.match(/^from\s+(models|schemas|main)\s+import\s+(.+)/);
|
||||
if (!m) continue;
|
||||
const srcCode = files[m[1] + '.py'];
|
||||
if (!srcCode) { issues.push(`ISSUE: ${fname}: ${m[1]}.py puuttuu`); continue; }
|
||||
const names = m[2].split(',').map(n => n.trim().split(/\s+as\s+/)[0].trim());
|
||||
for (const name of names) {
|
||||
if (name && !srcCode.includes(name)) issues.push(`ISSUE: ${fname}: "${name}" puuttuu ${m[1]}.py:stä`);
|
||||
}
|
||||
}
|
||||
if (fname === 'schemas.py') {
|
||||
if (/:\s*date\b/.test(code) && !/from datetime import/.test(code))
|
||||
issues.push('ISSUE: schemas.py: date-import puuttuu');
|
||||
if (/:\s*datetime\b/.test(code) && !/from datetime import/.test(code))
|
||||
issues.push('ISSUE: schemas.py: datetime-import puuttuu');
|
||||
}
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (/^\s*#/.test(line) || /^\s*$/.test(line)) continue;
|
||||
if (/(?<!["\w])false(?![\w"])/.test(line)) issues.push(`ISSUE: ${fname}:${i+1}: "false" → "False"`);
|
||||
if (/(?<!["\w])true(?![\w"])/.test(line)) issues.push(`ISSUE: ${fname}:${i+1}: "true" → "True"`);
|
||||
}
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
|
||||
function extractJson(text) {
|
||||
const m = text.match(/```(?:json)?\s*\n([\s\S]*?)```/);
|
||||
if (m) text = m[1].trim();
|
||||
let depth = 0, start = null;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
if (text[i] === '{') { if (depth === 0) start = i; depth++; }
|
||||
else if (text[i] === '}') { depth--; if (depth === 0 && start !== null) { try { return JSON.parse(text.slice(start, i+1)); } catch(e) { continue; } } }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// === Testiskenaariot ===
|
||||
const SCENARIOS = [
|
||||
{ id: 'todo', prompt: 'Todo-sovellus: tehtävien hallinta, deadline, prioriteetti ja status' },
|
||||
{ id: 'users', prompt: 'REST API käyttäjähallinnalle SQLite-tietokannalla' },
|
||||
{ id: 'blog', prompt: 'Blogi-API: kirjoittajat ja artikkelit, julkaisupäivämäärä ja status' },
|
||||
];
|
||||
|
||||
// === Pipeline: yhdelle mallille ja skenaariolle ===
|
||||
async function runPipeline(model, scenario) {
|
||||
const result = {
|
||||
model, scenario: scenario.id,
|
||||
reqOk: false, specOk: false, specEntities: 0,
|
||||
validationIssues: 0, fixRounds: 0,
|
||||
testsTotal: 0, testsPassed: 0, testsFailed: 0,
|
||||
totalDurationMs: 0, totalTokens: 0, avgTokPerSec: 0,
|
||||
error: null,
|
||||
};
|
||||
const timings = [];
|
||||
const dir = `${OUTPUT_DIR}/${model.replace(/[/:]/g, '_')}__${scenario.id}`;
|
||||
mkdirSync(dir, { recursive: true });
|
||||
|
||||
try {
|
||||
// 1. Vaatimukset
|
||||
console.log(` [1/5] Vaatimukset...`);
|
||||
const req = await ollamaChat(model, scenario.prompt, CLIENT_SYSTEM, 1024);
|
||||
timings.push(req);
|
||||
if (!req.text || req.text.length < 50) { result.error = 'Vaatimukset liian lyhyet'; return result; }
|
||||
result.reqOk = true;
|
||||
writeFileSync(`${dir}/_requirements.txt`, req.text);
|
||||
|
||||
// 2. JSON-speksi
|
||||
console.log(` [2/5] JSON-speksi...`);
|
||||
const specResp = await ollamaChat(model, `${req.text}\n\nOutput a JSON spec for this project.`, SPEC_SYSTEM, 2048);
|
||||
timings.push(specResp);
|
||||
const spec = extractJson(specResp.text);
|
||||
if (!spec || !spec.entities || spec.entities.length === 0) { result.error = 'JSON-speksi epäonnistui'; writeFileSync(`${dir}/_spec_raw.txt`, specResp.text); return result; }
|
||||
result.specOk = true;
|
||||
result.specEntities = spec.entities.length;
|
||||
writeFileSync(`${dir}/_spec.json`, JSON.stringify(spec, null, 2));
|
||||
|
||||
// 3. Template-generointi
|
||||
console.log(` [3/5] Koodigenerointi...`);
|
||||
const files = {
|
||||
'models.py': tmplModels(spec),
|
||||
'schemas.py': tmplSchemas(spec),
|
||||
'main.py': tmplMain(spec),
|
||||
'test_main.py': tmplTests(spec),
|
||||
'pyproject.toml': tmplPyproject(spec),
|
||||
};
|
||||
|
||||
// 4. Validointi + korjaussilmukka
|
||||
let issues = validateProjectCode(files);
|
||||
let fixRound = 0;
|
||||
while (issues.length > 0 && fixRound < MAX_FIX_ROUNDS) {
|
||||
fixRound++;
|
||||
console.log(` [4/5] Korjauskierros ${fixRound} (${issues.length} ongelmaa)...`);
|
||||
const issuesByFile = {};
|
||||
for (const issue of issues) {
|
||||
const m = issue.match(/^ISSUE:\s*(\S+?):/);
|
||||
const fname = m ? m[1] : 'unknown';
|
||||
if (!issuesByFile[fname]) issuesByFile[fname] = [];
|
||||
issuesByFile[fname].push(issue);
|
||||
}
|
||||
for (const [fname, fIssues] of Object.entries(issuesByFile)) {
|
||||
if (!files[fname]) continue;
|
||||
const fixPrompt = `Fix the following issues in this Python file. Return ONLY the complete corrected file, no explanations.\n\nISSUES:\n${fIssues.join('\n')}\n\nCURRENT FILE (${fname}):\n\`\`\`python\n${files[fname]}\`\`\``;
|
||||
const fixResp = await ollamaChat(model, fixPrompt, FIX_SYSTEM, 2048);
|
||||
timings.push(fixResp);
|
||||
if (fixResp.text) {
|
||||
files[fname] = fixResp.text.replace(/^```(?:python)?\s*\n?/m, '').replace(/\n?```\s*$/m, '').trim() + '\n';
|
||||
}
|
||||
}
|
||||
issues = validateProjectCode(files);
|
||||
}
|
||||
result.validationIssues = issues.length;
|
||||
result.fixRounds = fixRound;
|
||||
|
||||
// Kirjoita tiedostot levylle
|
||||
for (const [fn, content] of Object.entries(files)) writeFileSync(`${dir}/${fn}`, content);
|
||||
|
||||
// 5. Pytest
|
||||
console.log(` [5/5] Pytest...`);
|
||||
try {
|
||||
const uvPath = process.env.HOME + '/.local/bin/uv';
|
||||
const uv = existsSync(uvPath) ? uvPath : 'uv';
|
||||
execSync(`cd "${dir}" && ${uv} sync 2>/dev/null`, { timeout: 60000, stdio: 'pipe' });
|
||||
execSync(`cd "${dir}" && rm -f app.db test.db`, { stdio: 'pipe' });
|
||||
const pytestOut = execSync(`cd "${dir}" && ${uv} run pytest test_main.py -v --tb=short 2>&1`, { timeout: 60000, encoding: 'utf-8' });
|
||||
writeFileSync(`${dir}/_pytest.txt`, pytestOut);
|
||||
|
||||
const passedMatch = pytestOut.match(/(\d+) passed/);
|
||||
const failedMatch = pytestOut.match(/(\d+) failed/);
|
||||
result.testsPassed = passedMatch ? parseInt(passedMatch[1]) : 0;
|
||||
result.testsFailed = failedMatch ? parseInt(failedMatch[1]) : 0;
|
||||
result.testsTotal = result.testsPassed + result.testsFailed;
|
||||
} catch (e) {
|
||||
const output = e.stdout || e.stderr || e.message || '';
|
||||
writeFileSync(`${dir}/_pytest.txt`, output);
|
||||
const passedMatch = output.match(/(\d+) passed/);
|
||||
const failedMatch = output.match(/(\d+) failed/);
|
||||
const errorMatch = output.match(/(\d+) error/);
|
||||
result.testsPassed = passedMatch ? parseInt(passedMatch[1]) : 0;
|
||||
result.testsFailed = (failedMatch ? parseInt(failedMatch[1]) : 0) + (errorMatch ? parseInt(errorMatch[1]) : 0);
|
||||
result.testsTotal = result.testsPassed + result.testsFailed;
|
||||
if (result.testsTotal === 0) result.error = 'Pytest kaatui';
|
||||
}
|
||||
} catch (e) {
|
||||
result.error = e.message;
|
||||
}
|
||||
|
||||
// Yhteenveto
|
||||
result.totalDurationMs = timings.reduce((s, t) => s + t.durationMs, 0);
|
||||
result.totalTokens = timings.reduce((s, t) => s + t.tokens, 0);
|
||||
result.avgTokPerSec = timings.length > 0 ? timings.reduce((s, t) => s + t.tokPerSec, 0) / timings.length : 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// === Main ===
|
||||
async function main() {
|
||||
console.log('╔══════════════════════════════════════════════╗');
|
||||
console.log('║ Kipinä Model Benchmark ║');
|
||||
console.log('╚══════════════════════════════════════════════╝');
|
||||
console.log(`Ollama: ${OLLAMA_URL}`);
|
||||
|
||||
// Haetaan mallit
|
||||
let models;
|
||||
try {
|
||||
models = await ollamaListModels();
|
||||
} catch (e) {
|
||||
console.error(`Ei yhteyttä Ollamaan (${OLLAMA_URL}): ${e.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (FILTER_MODELS) {
|
||||
const filter = FILTER_MODELS.split(',').map(s => s.trim());
|
||||
models = models.filter(m => filter.some(f => m.includes(f)));
|
||||
}
|
||||
|
||||
console.log(`Mallit (${models.length}): ${models.join(', ')}`);
|
||||
|
||||
const scenarios = SCENARIO_FILTER === 'all' ? SCENARIOS : [SCENARIOS[0]];
|
||||
console.log(`Skenaariot (${scenarios.length}): ${scenarios.map(s => s.id).join(', ')}`);
|
||||
console.log(`Tulokset: ${OUTPUT_DIR}/`);
|
||||
console.log('');
|
||||
|
||||
// Puhdista output
|
||||
rmSync(OUTPUT_DIR, { recursive: true, force: true });
|
||||
mkdirSync(OUTPUT_DIR, { recursive: true });
|
||||
|
||||
const results = [];
|
||||
|
||||
for (const model of models) {
|
||||
for (const scenario of scenarios) {
|
||||
console.log(`\n━━━ ${model} × ${scenario.id} ━━━`);
|
||||
const r = await runPipeline(model, scenario);
|
||||
results.push(r);
|
||||
|
||||
const status = r.error ? `✗ ${r.error}` :
|
||||
r.testsPassed === r.testsTotal && r.testsTotal > 0 ? `✓ ${r.testsPassed}/${r.testsTotal}` :
|
||||
`◐ ${r.testsPassed}/${r.testsTotal}`;
|
||||
console.log(` → ${status} | ${(r.totalDurationMs/1000).toFixed(1)}s | ${r.totalTokens} tok | ${r.avgTokPerSec.toFixed(1)} tok/s`);
|
||||
}
|
||||
}
|
||||
|
||||
// === Tulostaulu ===
|
||||
console.log('\n\n╔══════════════════════════════════════════════════════════════════════════════════════════════════╗');
|
||||
console.log('║ TULOKSET ║');
|
||||
console.log('╠══════════════════════════════════════════════════════════════════════════════════════════════════╣');
|
||||
|
||||
const header = [
|
||||
'Malli'.padEnd(40),
|
||||
'Skenaario'.padEnd(10),
|
||||
'Speksi'.padEnd(8),
|
||||
'Testit'.padEnd(10),
|
||||
'Korjaus'.padEnd(8),
|
||||
'Aika'.padEnd(8),
|
||||
'tok/s'.padEnd(8),
|
||||
'Tulos',
|
||||
].join(' │ ');
|
||||
console.log(`║ ${header} ║`);
|
||||
console.log('╠' + '═'.repeat(header.length + 2) + '╣');
|
||||
|
||||
for (const r of results) {
|
||||
const specStatus = r.specOk ? `✓ ${r.specEntities}e` : '✗';
|
||||
const testStatus = r.testsTotal > 0 ? `${r.testsPassed}/${r.testsTotal}` : '-';
|
||||
const fixStatus = r.fixRounds > 0 ? `${r.fixRounds}×` : '-';
|
||||
const time = `${(r.totalDurationMs/1000).toFixed(0)}s`;
|
||||
const speed = `${r.avgTokPerSec.toFixed(0)}`;
|
||||
const verdict = r.error ? '✗ FAIL' : r.testsPassed === r.testsTotal && r.testsTotal > 0 ? '✓ PASS' : '◐ PARTIAL';
|
||||
|
||||
const row = [
|
||||
r.model.padEnd(40),
|
||||
r.scenario.padEnd(10),
|
||||
specStatus.padEnd(8),
|
||||
testStatus.padEnd(10),
|
||||
fixStatus.padEnd(8),
|
||||
time.padEnd(8),
|
||||
speed.padEnd(8),
|
||||
verdict,
|
||||
].join(' │ ');
|
||||
console.log(`║ ${row} ║`);
|
||||
}
|
||||
console.log('╚' + '═'.repeat(header.length + 2) + '╝');
|
||||
|
||||
// Tallenna JSON
|
||||
writeFileSync(`${OUTPUT_DIR}/results.json`, JSON.stringify(results, null, 2));
|
||||
console.log(`\nJSON: ${OUTPUT_DIR}/results.json`);
|
||||
|
||||
// Yhteenveto
|
||||
const passed = results.filter(r => !r.error && r.testsPassed === r.testsTotal && r.testsTotal > 0);
|
||||
const partial = results.filter(r => !r.error && r.testsPassed < r.testsTotal && r.testsTotal > 0);
|
||||
const failed = results.filter(r => r.error || r.testsTotal === 0);
|
||||
console.log(`\n✓ PASS: ${passed.length} | ◐ PARTIAL: ${partial.length} | ✗ FAIL: ${failed.length} | Yhteensä: ${results.length}`);
|
||||
}
|
||||
|
||||
main().catch(e => { console.error(e); process.exit(1); });
|
||||
BIN
projektit/luodut/rest-api-kyttjhallinnalle (1).zip
Normal file
BIN
projektit/luodut/rest-api-kyttjhallinnalle (1).zip
Normal file
Binary file not shown.
BIN
projektit/luodut/rest-api-kyttjhallinnalle.zip
Normal file
BIN
projektit/luodut/rest-api-kyttjhallinnalle.zip
Normal file
Binary file not shown.
Submodule projektit/projektiopinnot-1-datan-hallinta-ttm23sai added at c20e918b34
Reference in New Issue
Block a user