b21bfb2f1d
- PricingCatalog.php: single source of truth for plans (free/plus/pro), top-ups, Stripe price env keys, tool costs (0–6 credits), STT variable billing, feature limits - FreeTier.php: monthly-first credit deduction, ledger (user_tool_credit_ledger), STT reservation/settle/release, monthly reset, trial logic - StripeClient.php: canonical SKUs (plus/pro/topup_100/300/1000), legacy aliases kept - stripe-checkout.php: subscription vs payment mode, trial gating, catalog metadata - stripe-webhook.php: idempotent via stripe_events, handles subscription lifecycle + invoice.paid renewal + one-time topup credit grants - All API tools: success-based credit deduction (check before, charge after) - transcribe.php: file-size heuristic reservation, settle from actual provider duration - ask.php + LegalTools.php: ToolModels engine resolution — Pro gets gpt-4o - KorrespondAgent.php + korrespond.php: tier-gated draft deployment — Free/Plus gets gpt-4o-mini, Pro gets gpt-4o - pricing.php: NOK-only, plan cards, top-up packs, Organisation contact card, tool cost table, separate monthly/prepaid balance display - 003_pricing_credit_catalog.sql: ledger and STT reservation tables Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
215 lines
9.7 KiB
PHP
215 lines
9.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../includes/bootstrap.php';
|
|
require_once __DIR__ . '/../includes/KorrespondAgent.php';
|
|
require_once __DIR__ . '/../includes/ToolModels.php';
|
|
|
|
dbnToolsRequireMethod('POST');
|
|
dbnToolsRequireAuth();
|
|
|
|
@ini_set('output_buffering', '0');
|
|
@ini_set('zlib.output_compression', '0');
|
|
@ini_set('implicit_flush', '1');
|
|
while (ob_get_level() > 0) { @ob_end_clean(); }
|
|
ob_implicit_flush(true);
|
|
|
|
header('Content-Type: application/x-ndjson; charset=utf-8');
|
|
header('Cache-Control: no-store');
|
|
header('X-Accel-Buffering: no');
|
|
|
|
$startTime = microtime(true);
|
|
$language = 'en';
|
|
$creditDeducted = false;
|
|
$ftUid = 0;
|
|
|
|
$emit = function (string $event, array $payload = []) use ($startTime): void {
|
|
$payload['event'] = $event;
|
|
$payload['t_ms'] = (int)round((microtime(true) - $startTime) * 1000);
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
|
|
@flush();
|
|
};
|
|
|
|
try {
|
|
// ── Parse input (JSON or multipart) ─────────────────────────────────────────
|
|
$isMultipart = stripos((string)($_SERVER['CONTENT_TYPE'] ?? ''), 'multipart/form-data') !== false;
|
|
if ($isMultipart) {
|
|
$payloadRaw = (string)($_POST['payload'] ?? '');
|
|
if ($payloadRaw === '') {
|
|
throw new DbnToolsHttpException('Multipart request missing payload.', 422, 'missing_payload');
|
|
}
|
|
$input = json_decode($payloadRaw, true);
|
|
if (!is_array($input)) {
|
|
throw new DbnToolsHttpException('Invalid payload JSON.', 422, 'invalid_payload_json');
|
|
}
|
|
} else {
|
|
$raw = file_get_contents('php://input');
|
|
if ($raw === false || strlen($raw) > 200000) {
|
|
throw new DbnToolsHttpException('Request body unreadable or too large.', 413, 'body_too_large');
|
|
}
|
|
$input = json_decode((string)$raw, true);
|
|
if (!is_array($input)) {
|
|
throw new DbnToolsHttpException('Request body must be valid JSON.', 400, 'invalid_json');
|
|
}
|
|
}
|
|
|
|
// ── Normalise + validate ────────────────────────────────────────────────────
|
|
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
|
|
|
|
$allowedBodies = ['barnehage','school_1_10','sfo','nav','bufdir','barnevernet',
|
|
'kommune_other','statsforvalter','trygderetten','tingrett','other'];
|
|
$allowedOutput = ['email','formal','filing','call_prep'];
|
|
$allowedTone = ['cooperative','neutral','firm','adversarial','warm'];
|
|
$allowedMode = ['reply','initiate'];
|
|
|
|
$intake = [
|
|
'mode' => in_array($input['mode'] ?? '', $allowedMode, true) ? $input['mode'] : 'initiate',
|
|
'recipient_body' => in_array($input['recipient_body'] ?? '', $allowedBodies, true) ? $input['recipient_body'] : 'other',
|
|
'output_type' => in_array($input['output_type'] ?? '', $allowedOutput, true) ? $input['output_type'] : 'email',
|
|
'tone' => in_array($input['tone'] ?? '', $allowedTone, true) ? $input['tone'] : 'neutral',
|
|
'language' => $language,
|
|
'case_ref' => mb_substr(trim((string)($input['case_ref'] ?? '')), 0, 120, 'UTF-8'),
|
|
'where' => mb_substr(trim((string)($input['where'] ?? '')), 0, 200, 'UTF-8'),
|
|
'parties_text' => mb_substr(trim((string)($input['parties_text'] ?? '')), 0, 2000, 'UTF-8'),
|
|
'narrative' => mb_substr(trim((string)($input['narrative'] ?? '')), 0, 8000, 'UTF-8'),
|
|
'goal' => mb_substr(trim((string)($input['goal'] ?? '')), 0, 600, 'UTF-8'),
|
|
'deadlines' => is_array($input['deadlines'] ?? null)
|
|
? array_slice(array_values(array_filter(array_map(
|
|
fn($d) => mb_substr(trim((string)$d), 0, 60, 'UTF-8'),
|
|
$input['deadlines']
|
|
))), 0, 6) : [],
|
|
'clarifications' => is_array($input['clarifications'] ?? null) ? $input['clarifications'] : [],
|
|
'use_my_case' => !empty($input['use_my_case']),
|
|
];
|
|
$intake['narrative'] = dbnToolsInjectDocContent($input, $intake['narrative']);
|
|
|
|
$forceDraft = !empty($input['force_draft']);
|
|
$hasClarif = !empty($intake['clarifications']);
|
|
|
|
// ── Extract uploaded files (Reply mode) ─────────────────────────────────────
|
|
$receivedText = '';
|
|
$attachmentsText = '';
|
|
$attachmentNames = [];
|
|
if (!empty($_FILES['files']) && is_array($_FILES['files']['tmp_name'] ?? null)) {
|
|
$count = count($_FILES['files']['tmp_name']);
|
|
if ($count > 4) {
|
|
throw new DbnToolsHttpException('At most 4 files can be uploaded per request.', 413, 'too_many_files');
|
|
}
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$file = [
|
|
'name' => $_FILES['files']['name'][$i] ?? '',
|
|
'type' => $_FILES['files']['type'][$i] ?? '',
|
|
'tmp_name' => $_FILES['files']['tmp_name'][$i] ?? '',
|
|
'error' => $_FILES['files']['error'][$i] ?? UPLOAD_ERR_NO_FILE,
|
|
'size' => $_FILES['files']['size'][$i] ?? 0,
|
|
];
|
|
$extracted = dbnToolsExtractUploadedFile($file);
|
|
$attachmentNames[] = $extracted['filename'];
|
|
if ($i === 0) {
|
|
$receivedText = $extracted['text'];
|
|
} else {
|
|
$attachmentsText .= "\n\n--- " . $extracted['filename'] . " ---\n\n" . $extracted['text'];
|
|
}
|
|
$emit('progress', [
|
|
'detail' => DbnKorrespondAgent::L('file_read', $language, [
|
|
'name' => $extracted['filename'],
|
|
'chars' => $extracted['chars'],
|
|
]),
|
|
]);
|
|
}
|
|
}
|
|
$intake['received_text'] = $receivedText;
|
|
$intake['attachments_text'] = $attachmentsText;
|
|
|
|
if ($intake['mode'] === 'reply' && $receivedText === '' && $intake['narrative'] === '') {
|
|
throw new DbnToolsHttpException(
|
|
'For reply mode, upload the received letter or paste its text.',
|
|
422, 'reply_no_input'
|
|
);
|
|
}
|
|
if ($intake['mode'] === 'initiate' && $intake['narrative'] === '') {
|
|
throw new DbnToolsHttpException(
|
|
'Describe the situation in "What happened" before drafting.',
|
|
422, 'initiate_no_narrative'
|
|
);
|
|
}
|
|
|
|
$emit('start', [
|
|
'mode' => $intake['mode'],
|
|
'body' => $intake['recipient_body'],
|
|
'output_type' => $intake['output_type'],
|
|
'language' => $language,
|
|
'attachments' => $attachmentNames,
|
|
]);
|
|
|
|
// ── Pass 1: classify + gap-check ────────────────────────────────────────────
|
|
$emit('progress', ['detail' => DbnKorrespondAgent::L('analyzing', $language)]);
|
|
$agent = new DbnKorrespondAgent();
|
|
$classify = $agent->classify($intake);
|
|
$emit('classify', ['result' => [
|
|
'summary' => $classify['summary'],
|
|
'parties' => $classify['parties'],
|
|
'decision_or_action' => $classify['decision_or_action'],
|
|
'deadlines' => $classify['deadlines'],
|
|
'applicable_acts' => $classify['applicable_acts'],
|
|
'jurisdiction' => $classify['jurisdiction'],
|
|
'suggested_goal' => $classify['suggested_goal'],
|
|
]]);
|
|
|
|
$missing = $classify['missing_facts'] ?? [];
|
|
if (!empty($missing) && !$forceDraft && !$hasClarif) {
|
|
// Gate: emit clarify and stop — NO credit deduct
|
|
$emit('clarify', ['questions' => $missing]);
|
|
$emit('end', ['stopped_at' => 'clarify']);
|
|
exit;
|
|
}
|
|
|
|
// ── Deduct credit now (Pass 2 starts) ───────────────────────────────────────
|
|
$ftUid = dbnToolsFreeTierCheck('korrespond');
|
|
$engine = ToolModels::engineForUser($ftUid, 'azure_mini');
|
|
$ftRemaining = dbnToolsFreeTierDeduct($ftUid, 'korrespond');
|
|
$creditDeducted = true;
|
|
|
|
// ── Pass 2: retrieve law → draft → self-check → translate ──────────────────
|
|
$result = $agent->generate($intake, $classify, $emit, $engine);
|
|
$result['ok'] = true;
|
|
$result['latency_ms'] = (int)round((microtime(true) - $startTime) * 1000);
|
|
if ($ftRemaining >= 0) {
|
|
$result['balance'] = $ftRemaining;
|
|
}
|
|
|
|
dbnToolsLogMetadata([
|
|
'tool' => 'korrespond',
|
|
'language' => $language,
|
|
'ok' => true,
|
|
'latency_ms' => $result['latency_ms'],
|
|
'source_count' => is_array($result['cited_law'] ?? null) ? count($result['cited_law']) : 0,
|
|
'deployment' => ($engine === 'azure_full') ? 'gpt-4o' : 'gpt-4o-mini',
|
|
]);
|
|
|
|
|
|
$emit('final', ['result' => $result]);
|
|
|
|
} catch (DbnToolsHttpException $e) {
|
|
$latency = (int)round((microtime(true) - $startTime) * 1000);
|
|
dbnToolsLogMetadata([
|
|
'tool' => 'korrespond',
|
|
'language' => $language,
|
|
'ok' => false,
|
|
'latency_ms' => $latency,
|
|
'error_code' => $e->errorCode,
|
|
]);
|
|
$emit('error', ['code' => $e->errorCode, 'message' => $e->getMessage(), 'status' => $e->status]);
|
|
} catch (Throwable $e) {
|
|
error_log('Korrespond fatal: ' . $e->getMessage());
|
|
$latency = (int)round((microtime(true) - $startTime) * 1000);
|
|
dbnToolsLogMetadata([
|
|
'tool' => 'korrespond',
|
|
'language' => $language,
|
|
'ok' => false,
|
|
'latency_ms' => $latency,
|
|
'error_code' => 'internal_error',
|
|
]);
|
|
$emit('error', ['code' => 'internal_error', 'message' => 'Korrespond could not complete this request.']);
|
|
}
|