lambda-erp 0.1.18__tar.gz → 0.1.20__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/PKG-INFO +1 -1
  2. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/chat.py +136 -3
  3. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/providers.py +18 -0
  4. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/database.py +14 -1
  5. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/pyproject.toml +1 -1
  6. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/.gitignore +0 -0
  7. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/LICENSE +0 -0
  8. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/README.md +0 -0
  9. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/__init__.py +0 -0
  10. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/attachments.py +0 -0
  11. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/auth.py +0 -0
  12. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/bootstrap.py +0 -0
  13. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/demo_limits.py +0 -0
  14. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/deps.py +0 -0
  15. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/errors.py +0 -0
  16. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/main.py +0 -0
  17. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/pdf.py +0 -0
  18. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/__init__.py +0 -0
  19. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/accounting.py +0 -0
  20. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/admin.py +0 -0
  21. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/analytics.py +0 -0
  22. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/bank_reconciliation.py +0 -0
  23. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/documents.py +0 -0
  24. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/masters.py +0 -0
  25. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/reports.py +0 -0
  26. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/routers/setup.py +0 -0
  27. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/services.py +0 -0
  28. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/api/templates/document.html +0 -0
  29. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/docs/agents/README.md +0 -0
  30. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/frontend/README.md +0 -0
  31. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/frontend/src/api/client.ts +0 -0
  32. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/__init__.py +0 -0
  33. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/__init__.py +0 -0
  34. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/bank_transaction.py +0 -0
  35. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/budget.py +0 -0
  36. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/chart_of_accounts.py +0 -0
  37. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/general_ledger.py +0 -0
  38. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/journal_entry.py +0 -0
  39. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/payment_entry.py +0 -0
  40. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/pos_invoice.py +0 -0
  41. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/purchase_invoice.py +0 -0
  42. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/revaluation.py +0 -0
  43. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/sales_invoice.py +0 -0
  44. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/accounting/subscription.py +0 -0
  45. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/buying/__init__.py +0 -0
  46. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/buying/purchase_order.py +0 -0
  47. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/controllers/__init__.py +0 -0
  48. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/controllers/currency.py +0 -0
  49. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/controllers/defaults.py +0 -0
  50. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/controllers/pricing_rule.py +0 -0
  51. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/controllers/taxes_and_totals.py +0 -0
  52. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/exceptions.py +0 -0
  53. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/hooks.py +0 -0
  54. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/model.py +0 -0
  55. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/selling/__init__.py +0 -0
  56. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/selling/quotation.py +0 -0
  57. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/selling/sales_order.py +0 -0
  58. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/simulation.py +0 -0
  59. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/stock/__init__.py +0 -0
  60. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/stock/delivery_note.py +0 -0
  61. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/stock/purchase_receipt.py +0 -0
  62. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/stock/stock_entry.py +0 -0
  63. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/stock/stock_ledger.py +0 -0
  64. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/lambda_erp/utils.py +0 -0
  65. {lambda_erp-0.1.18 → lambda_erp-0.1.20}/terraform/README.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lambda-erp
3
- Version: 0.1.18
3
+ Version: 0.1.20
4
4
  Summary: Core ERP logic - accounting, sales, purchasing, inventory
5
5
  Author: TORUS INVESTMENTS AG
6
6
  License-Expression: MIT
@@ -9,9 +9,12 @@ Supports multiple chat sessions, each with their own history and auto-generated
9
9
  """
10
10
 
11
11
  import asyncio
12
+ import base64
13
+ import io
12
14
  import json
13
15
  import logging
14
16
  import os
17
+ import re
15
18
  import traceback
16
19
  import uuid
17
20
  from datetime import date
@@ -29,7 +32,7 @@ from api.demo_limits import (
29
32
  is_demo_role,
30
33
  limiter as demo_limiter,
31
34
  )
32
- from api.providers import cost_of_anthropic_call, cost_of_openai_call
35
+ from api.providers import cost_of_anthropic_call, cost_of_openai_call, cost_of_transcription
33
36
  from api.routers.masters import create_master_record, update_master_record, MASTER_IDENTITY_ALIAS
34
37
  from lambda_erp.database import get_db
35
38
  from lambda_erp.utils import flt, now, nowdate
@@ -597,7 +600,7 @@ TOOLS = [
597
600
  "description": (
598
601
  "Create a new master record. You MUST include the full `data` object using the EXACT field names listed below — unknown fields are silently dropped.\n\n"
599
602
  "Every record's id is `name`. Customer/Supplier/Item/Warehouse auto-generate it (CUST-/SUPP-/ITEM-/WH-NNN) when omitted; Company/Account/Cost Center have NO auto-id, so `name` is required for them.\n\n"
600
- "**Customer fields:** name (optional custom id; auto CUST-NNN), customer_name (required), customer_group, territory, default_currency, credit_limit, email, phone, address, city, zip_code, country, tax_id.\n"
603
+ "**Customer fields:** name (optional custom id; auto CUST-NNN), customer_name (required), customer_group, territory, default_currency, credit_limit, email, phone, address, city, zip_code, country, tax_id, contact_person (named contact at the customer), contact_email, contact_phone.\n"
601
604
  "**Supplier fields:** name (optional custom id; auto SUPP-NNN), supplier_name (required), supplier_group, default_currency, email, phone, address, city, zip_code, country, tax_id.\n"
602
605
  "**Item fields:** item_code (the unique item code/id, e.g. \"SVC-SPARK\" — optional, may use ANY prefix, not just ITEM; auto-generated as ITEM-NNN if omitted), item_name (required, the human-readable name), item_group, stock_uom, standard_rate, is_stock_item, default_warehouse, description.\n"
603
606
  "**Warehouse fields:** name (optional custom id; auto WH-NNN), warehouse_name (required), company, parent_warehouse (omit or null when not needed), address, city, zip_code, country.\n"
@@ -623,7 +626,7 @@ TOOLS = [
623
626
  "function": {
624
627
  "name": "update_master",
625
628
  "description": (
626
- "Update an existing master record. You MUST include the `name` of the existing record and a `data` object with the fields to change. Use the same field names listed in create_master (customer: customer_name, email, phone, address, city, zip_code, country, tax_id, etc.). Example: {\"master_type\":\"customer\",\"name\":\"CUST-001\",\"data\":{\"address\":\"123 New Street\",\"city\":\"Boston\",\"zip_code\":\"02110\"}}"
629
+ "Update an existing master record. You MUST include the `name` of the existing record and a `data` object with the fields to change. Use the same field names listed in create_master (customer: customer_name, email, phone, address, city, zip_code, country, tax_id, contact_person, contact_email, contact_phone, etc.). Example: {\"master_type\":\"customer\",\"name\":\"CUST-001\",\"data\":{\"address\":\"123 New Street\",\"city\":\"Boston\",\"zip_code\":\"02110\"}}"
627
630
  ),
628
631
  "parameters": {
629
632
  "type": "object",
@@ -2326,6 +2329,65 @@ async def run_thinking_loop(
2326
2329
  await on_event({"type": "complete", "content": content})
2327
2330
 
2328
2331
 
2332
+ # ---------------------------------------------------------------------------
2333
+ # Speech-to-text (voice → text)
2334
+ # ---------------------------------------------------------------------------
2335
+
2336
+ # Domain-vocabulary biasing for the transcription `prompt`. A bare term list,
2337
+ # NO language constraint — the spoken language is auto-detected, and a
2338
+ # locale-derived hint would mis-transcribe users who speak another language
2339
+ # than their UI. Swiss German transcribes to Standard German on its own, which
2340
+ # is what the thinking loop wants. These are the ERP terms short utterances
2341
+ # otherwise mangle.
2342
+ VOICE_TRANSCRIBE_PROMPT = (
2343
+ "Lambda, ERP, Offerte, Angebot, Auftrag, Rechnung, Gutschrift, Beleg, "
2344
+ "Lieferschein, Bestellung, Zahlung, Buchung, Kunde, Lieferant, Artikel, "
2345
+ "Lager, Konto, Kostenstelle, quotation, sales order, invoice, credit note, "
2346
+ "purchase order, payment, journal entry, customer, supplier, item, warehouse"
2347
+ )
2348
+
2349
+ # Below this the API rejects the clip as "too short"; we treat it as a no-op
2350
+ # (the user tapped the mic by accident) rather than an error.
2351
+ _MIN_TRANSCRIBE_BYTES = 1500 # ~1s of webm/opus at the ~1.5 KB/s we assume below
2352
+
2353
+
2354
+ def transcribe_audio(audio_bytes: bytes, audio_format: str, api_key: str) -> str:
2355
+ """Transcribe audio to text via OpenAI gpt-4o-transcribe.
2356
+
2357
+ Returns the transcript, or "" when the model detects silence/near-silence
2358
+ (empty result or a prompt-echo hallucination on a near-silent clip).
2359
+ Raises on an actual API error so the caller can surface it.
2360
+ """
2361
+ client = OpenAI(api_key=api_key, timeout=httpx.Timeout(60.0, connect=10.0))
2362
+
2363
+ audio_file = io.BytesIO(audio_bytes)
2364
+ audio_file.name = f"voice.{audio_format or 'webm'}"
2365
+
2366
+ transcription = client.audio.transcriptions.create(
2367
+ model="gpt-4o-transcribe",
2368
+ file=audio_file,
2369
+ response_format="json",
2370
+ prompt=VOICE_TRANSCRIBE_PROMPT,
2371
+ )
2372
+
2373
+ text = (getattr(transcription, "text", "") or "").strip()
2374
+ if len(text) < 2:
2375
+ return ""
2376
+
2377
+ # Prompt-echo guard: on silent/noise audio, prompt-biased transcription
2378
+ # tends to hallucinate the biasing vocabulary back. If (nearly) every word
2379
+ # of the transcript is drawn from the prompt itself, it's an echo, not
2380
+ # speech — real utterances carry verbs/objects outside the glossary.
2381
+ prompt_tokens = set(re.findall(r"[\w'-]+", VOICE_TRANSCRIBE_PROMPT.lower()))
2382
+ text_tokens = re.findall(r"[\w'-]+", text.lower())
2383
+ if text_tokens:
2384
+ coverage = sum(1 for tok in text_tokens if tok in prompt_tokens) / len(text_tokens)
2385
+ if coverage >= 0.8:
2386
+ return ""
2387
+
2388
+ return text
2389
+
2390
+
2329
2391
  # ---------------------------------------------------------------------------
2330
2392
  # WebSocket endpoint
2331
2393
  # ---------------------------------------------------------------------------
@@ -2675,6 +2737,77 @@ async def chat_websocket(
2675
2737
  session_tasks[session_id] = asyncio.create_task(replay_demo_session(session_id))
2676
2738
  continue
2677
2739
 
2740
+ if msg_type == "transcribe":
2741
+ # Voice → text: transcribe a recorded clip and return the text
2742
+ # for the composer. No LLM turn here — the user reviews/edits
2743
+ # the text and sends it like any typed message.
2744
+ openai_api_key = os.environ.get("OPENAI_API_KEY", "")
2745
+ if not openai_api_key or openai_api_key == "sk-your-key-here":
2746
+ await send_event(
2747
+ "transcription_result", request_id=request_id,
2748
+ success=False, error="Speech-to-text is not available (OPENAI_API_KEY not configured).",
2749
+ )
2750
+ continue
2751
+
2752
+ # Demo visitors are rate-limited the same way as LLM turns — a
2753
+ # paid OpenAI call still costs money. Block before transcribing.
2754
+ if is_demo_role(ws_user_role) and client_ip:
2755
+ blocked = demo_limiter.check(client_ip)
2756
+ if blocked:
2757
+ await send_event(
2758
+ "transcription_result", request_id=request_id,
2759
+ success=False, error=blocked,
2760
+ )
2761
+ continue
2762
+
2763
+ audio_b64 = data.get("audio_data") or ""
2764
+ audio_format = (data.get("audio_format") or "webm").lower()
2765
+ try:
2766
+ audio_bytes = base64.b64decode(audio_b64) if audio_b64 else b""
2767
+ except Exception:
2768
+ audio_bytes = b""
2769
+
2770
+ if len(audio_bytes) < _MIN_TRANSCRIBE_BYTES:
2771
+ # Too short to be speech (accidental tap) — treat as empty,
2772
+ # no API call, no cost.
2773
+ await send_event(
2774
+ "transcription_result", request_id=request_id,
2775
+ success=True, text="",
2776
+ )
2777
+ continue
2778
+
2779
+ try:
2780
+ text = await asyncio.to_thread(
2781
+ transcribe_audio, audio_bytes, audio_format, openai_api_key,
2782
+ )
2783
+ except Exception as e:
2784
+ logger.warning("Transcription failed: %s", e)
2785
+ await send_event(
2786
+ "transcription_result", request_id=request_id,
2787
+ success=False, error="Transcription failed. Please try again.",
2788
+ )
2789
+ continue
2790
+
2791
+ # Track the spend. Audio duration is estimated from byte size
2792
+ # (~1.5 KB/s for webm/opus); the Demo Spend Log records every
2793
+ # role for the admin dashboard, and counts public_manager rows
2794
+ # against the hourly cap.
2795
+ estimated_seconds = len(audio_bytes) / 1500
2796
+ demo_limiter.record(
2797
+ client_ip or "unknown",
2798
+ cost_of_transcription("gpt-4o-transcribe", estimated_seconds),
2799
+ role=ws_user_role,
2800
+ provider="openai",
2801
+ model="gpt-4o-transcribe",
2802
+ session_id=data.get("session_id"),
2803
+ )
2804
+
2805
+ await send_event(
2806
+ "transcription_result", request_id=request_id,
2807
+ success=True, text=text,
2808
+ )
2809
+ continue
2810
+
2678
2811
  if msg_type != "send_message":
2679
2812
  await send_error(f"Unknown message type: {msg_type}", request_id=request_id)
2680
2813
  continue
@@ -43,6 +43,13 @@ ANTHROPIC_PRICING: dict[str, dict[str, Any]] = {
43
43
  "claude-haiku-4-5": {"input": 1.00, "output": 5.00, "cache_write": 1.25, "cache_read": 0.10},
44
44
  }
45
45
 
46
+ # Speech-to-text, priced per minute of audio (not per token).
47
+ TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
48
+ "gpt-4o-transcribe": {"per_minute": 0.006},
49
+ "gpt-4o-mini-transcribe": {"per_minute": 0.003},
50
+ "whisper-1": {"per_minute": 0.006},
51
+ }
52
+
46
53
 
47
54
  # ---------------------------------------------------------------------------
48
55
  # Rate lookup — unknown models fall back to the most expensive model in each
@@ -114,3 +121,14 @@ def cost_of_anthropic_call(model: str, usage: Optional[Any]) -> float:
114
121
  + output_tokens * rates["output"]
115
122
  )
116
123
  return total / 1_000_000
124
+
125
+
126
+ def cost_of_transcription(model: str, audio_seconds: float) -> float:
127
+ """USD cost of one speech-to-text call, billed per minute of audio.
128
+
129
+ Unknown models fall back to the most expensive STT rate so we never
130
+ under-count. `audio_seconds` is the (estimated) audio duration."""
131
+ rates = TRANSCRIBE_PRICING.get(model)
132
+ if rates is None:
133
+ rates = max(TRANSCRIBE_PRICING.values(), key=lambda r: r["per_minute"])
134
+ return max(float(audio_seconds or 0.0), 0.0) / 60.0 * rates["per_minute"]
@@ -331,7 +331,10 @@ class Database:
331
331
  city TEXT,
332
332
  zip_code TEXT,
333
333
  country TEXT,
334
- tax_id TEXT
334
+ tax_id TEXT,
335
+ contact_person TEXT,
336
+ contact_email TEXT,
337
+ contact_phone TEXT
335
338
  )""",
336
339
 
337
340
  """CREATE TABLE IF NOT EXISTS "Supplier" (
@@ -1743,6 +1746,15 @@ def _m015_document_discarded(db: "Database") -> None:
1743
1746
  db._add_column_if_missing(table, "discarded", "INTEGER DEFAULT 0")
1744
1747
 
1745
1748
 
1749
+ def _m016_customer_contact_person(db: "Database") -> None:
1750
+ """Add a named contact person (Ansprechperson) to the Customer master:
1751
+ a free-text name plus their own email and phone, kept separate from the
1752
+ company-level email/phone already on the record."""
1753
+ db._add_column_if_missing("Customer", "contact_person", "TEXT")
1754
+ db._add_column_if_missing("Customer", "contact_email", "TEXT")
1755
+ db._add_column_if_missing("Customer", "contact_phone", "TEXT")
1756
+
1757
+
1746
1758
  Database.MIGRATIONS = [
1747
1759
  (1, "chat_message_session_id", _m001_chat_message_session_id),
1748
1760
  (2, "chat_session_user_id", _m002_chat_session_user_id),
@@ -1759,6 +1771,7 @@ Database.MIGRATIONS = [
1759
1771
  (13, "unrealized_exchange_account", _m013_unrealized_exchange_account),
1760
1772
  (14, "company_iban", _m014_company_iban),
1761
1773
  (15, "document_discarded", _m015_document_discarded),
1774
+ (16, "customer_contact_person", _m016_customer_contact_person),
1762
1775
  ]
1763
1776
 
1764
1777
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lambda-erp"
3
- version = "0.1.18"
3
+ version = "0.1.20"
4
4
  description = "Core ERP logic - accounting, sales, purchasing, inventory"
5
5
  readme = "README.md"
6
6
  license = "MIT"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes