mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
feat(orangecat): Loop A — mirror subscriptions to OrangeCat via @orangecat/sdk
First real cross-product call from FleetCrown into OrangeCat. Whenever a FleetCrown user adds a subscription on /money, the same record is mirrored into OrangeCat as a `service` owned by the FleetCrown group actor. Mao now sees FleetCrown's monthly burn inside the OrangeCat ledger — the bitbaum studio narrative gets its first real data point. - Adds @orangecat/sdk@0.1.0 as a file:vendor/ dep (vendor/orangecat-sdk-0.1.0.tgz committed; mirrors npm-install semantics for dev + Vercel until the SDK is published). - src/lib/integrations/orangecat.ts: singleton client, no-ops when ORANGECAT_API_KEY is unset. syncSubscriptionToOrangeCat() is fire-and-forget — never throws, never blocks the subscription POST. Stable Idempotency-Key (`fleetcrown_sub_<id>`) so retries dedupe cleanly when server-side dedup lands. - src/db/queries/money.ts: hook fires after the DB insert returns. Whether the OC sync succeeds or fails is irrelevant to the user response — the local subscription is already persisted. - .env.example: documents ORANGECAT_API_KEY + ORANGECAT_API_BASE. Reliability defaults baked in by the SDK: 20s timeout, 3 exp-backoff retries on network / 5xx / 429 honouring Retry-After, User-Agent identifying FleetCrown, AbortSignal-clean. Failure modes are non-fatal and logged with enough context for backfill: - ORANGECAT_API_KEY unset → silent no-op (most dev envs today) - OrangeCat returns OrangeCatError → console.warn with code+status, subscription still saved locally - Network error → SDK retries internally, then console.warn Follow-ups (each its own commit, all post-this-spike): - schema: add subscriptions.orangecat_service_id column so the link persists and the UI can render a "Synced ✓" badge - once /api/v1 exposes GET via integration-key auth, run a backfill job for existing subscriptions - when OrangeCat publishes the SDK to npm, replace the vendor/ tarball with a normal version range Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e19a059feb
commit
1e969983c0
7 files changed
+167
-3
No files matched your search
@@ -77,3 +77,11 @@ VAPID_PUBLIC_KEY=
|
||||
VAPID_PRIVATE_KEY=
|
||||
VAPID_SUBJECT=mailto:you@example.com
|
||||
NEXT_PUBLIC_VAPID_PUBLIC_KEY=
|
||||
|
||||
# ── OrangeCat integration (Loop A: mirror subscriptions to the economic layer)
|
||||
# Mint at https://orangecat.ch/settings/integrations, choose the FleetCrown
|
||||
# group actor at mint time. Format: ock_<48-hex>. Treat like a password — the
|
||||
# plaintext is shown once and stored as a sha256 hash on OrangeCat's side.
|
||||
# When unset, the integration no-ops (subscriptions are still saved locally).
|
||||
ORANGECAT_API_KEY=
|
||||
ORANGECAT_API_BASE=https://orangecat.ch
|
||||
Generated
+10
@@ -16,6 +16,7 @@
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@orangecat/sdk": "file:vendor/orangecat-sdk-0.1.0.tgz",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"date-fns": "^4.1.0",
|
||||
@@ -3036,6 +3037,15 @@
|
||||
"integrity": "sha512-U69T3ItWHvLwGg5eJ0n3I62nWuE6ilHlmz7zM0npLBRvPRd7e6NYmg54vvRtP5mZG7kZqZCFVdsTWo7BPtBujg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@orangecat/sdk": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "file:vendor/orangecat-sdk-0.1.0.tgz",
|
||||
"integrity": "sha512-uRH77by/+C5LiPERy6igSjGmZnkru6EoT0suluI9AM+0Pr52+P3wceDGG7RS4TCrjICQfJq3OWIqsKBJWtGgAg==",
|
||||
"license": "UNLICENSED",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@panva/hkdf": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@panva/hkdf/-/hkdf-1.2.1.tgz",
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@auth/drizzle-adapter": "^1.11.2",
|
||||
"@orangecat/sdk": "file:vendor/orangecat-sdk-0.1.0.tgz",
|
||||
"@base-ui/react": "^1.3.0",
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
|
||||
@@ -380,14 +380,19 @@ export const DESKTOP_DOWNLOAD = {
|
||||
label: "macOS",
|
||||
status: "ready" as const,
|
||||
primary: {
|
||||
label: "Download .dmg",
|
||||
note: "Apple Silicon · ~98 MB",
|
||||
label: "Download .dmg (Apple Silicon)",
|
||||
note: "M1/M2/M3/M4 · ~98 MB",
|
||||
url:
|
||||
"https://github.com/maonakamoto/fleetcrown-releases/releases/latest/download/Fleet-Runner-mac-arm64.dmg",
|
||||
},
|
||||
secondary: [
|
||||
{
|
||||
label: ".zip (no installer)",
|
||||
label: ".dmg (Intel)",
|
||||
url:
|
||||
"https://github.com/maonakamoto/fleetcrown-releases/releases/latest/download/Fleet-Runner-mac-x64.dmg",
|
||||
},
|
||||
{
|
||||
label: ".zip (Apple Silicon, no installer)",
|
||||
url:
|
||||
"https://github.com/maonakamoto/fleetcrown-releases/releases/latest/download/Fleet-Runner-mac-arm64.zip",
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { db } from "@/db";
|
||||
import { subscriptions, commitments } from "@/db/schema";
|
||||
import { syncSubscriptionToOrangeCat } from "@/lib/integrations/orangecat";
|
||||
|
||||
type SubscriptionRow = typeof subscriptions.$inferSelect;
|
||||
import { eq, and, sql } from "drizzle-orm";
|
||||
@@ -60,6 +61,23 @@ export async function createSubscription(userId: string, data: CreateSubscriptio
|
||||
status: SUB_STATUS.ACTIVE,
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Loop A: mirror the subscription into OrangeCat as a `service` record.
|
||||
// Fire-and-forget — a network blip on OrangeCat must not fail the
|
||||
// subscription POST. The integration no-ops when ORANGECAT_API_KEY is
|
||||
// unset, which is the case for most dev environments today.
|
||||
if (created) {
|
||||
void syncSubscriptionToOrangeCat({
|
||||
id: created.id,
|
||||
name: created.name,
|
||||
vendor: created.vendor,
|
||||
amount: created.amount,
|
||||
currency: created.currency,
|
||||
frequency: created.frequency,
|
||||
notes: created.notes,
|
||||
});
|
||||
}
|
||||
|
||||
return created;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* OrangeCat integration — outbound calls FROM FleetCrown TO OrangeCat.
|
||||
*
|
||||
* Loop A (subscriptions): every time a FleetCrown user adds a subscription
|
||||
* on the /money page, we mirror it into OrangeCat as a `service` record
|
||||
* owned by the actor the integration key is bound to (the FleetCrown
|
||||
* group actor inside OrangeCat). This gives:
|
||||
* - one ledger across the studio (Mao sees FleetCrown's spend in OC)
|
||||
* - the bitbaum studio narrative its first real data point
|
||||
*
|
||||
* Auth: `ORANGECAT_API_KEY` (mint at /settings/integrations on OrangeCat,
|
||||
* choose the FleetCrown group actor at mint time). The SDK takes care of
|
||||
* Idempotency-Key, retries (3, exp backoff honouring Retry-After),
|
||||
* timeouts (20s), and the standard error envelope.
|
||||
*
|
||||
* The sync is FIRE-AND-FORGET — a network blip on OrangeCat must not
|
||||
* fail the subscription POST. Failures are logged with enough context
|
||||
* to backfill later; user-facing flow is unaffected.
|
||||
*
|
||||
* Created: 2026-06-03
|
||||
*/
|
||||
|
||||
import { OrangeCatClient, OrangeCatError } from "@orangecat/sdk";
|
||||
|
||||
interface SubscriptionForSync {
|
||||
id: string;
|
||||
name: string;
|
||||
vendor: string | null;
|
||||
amount: number | null;
|
||||
currency: string | null;
|
||||
frequency: string | null;
|
||||
notes: string | null;
|
||||
}
|
||||
|
||||
let cached: OrangeCatClient | null = null;
|
||||
|
||||
/**
|
||||
* Returns the singleton client when configured, or `null` when the env
|
||||
* vars are missing. Callers must handle null — most users / dev
|
||||
* environments will not have the integration set up, and the absence is
|
||||
* not an error.
|
||||
*/
|
||||
export function getOrangeCatClient(): OrangeCatClient | null {
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const apiKey = process.env.ORANGECAT_API_KEY;
|
||||
if (!apiKey || !apiKey.startsWith("ock_")) {
|
||||
return null;
|
||||
}
|
||||
cached = new OrangeCatClient({
|
||||
apiKey,
|
||||
baseUrl: process.env.ORANGECAT_API_BASE ?? "https://orangecat.ch",
|
||||
userAgent: `fleetcrown/${process.env.npm_package_version ?? "0.1.0"} (+sdk)`,
|
||||
});
|
||||
return cached;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirror a FleetCrown subscription into OrangeCat as a `service` record.
|
||||
* Fire-and-forget — never throws. Returns the OrangeCat service id on
|
||||
* success so callers can persist the link later (schema migration is a
|
||||
* follow-up; for the spike we log it).
|
||||
*/
|
||||
export async function syncSubscriptionToOrangeCat(
|
||||
sub: SubscriptionForSync,
|
||||
): Promise<string | null> {
|
||||
const client = getOrangeCatClient();
|
||||
if (!client) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const service = await client.services.create(
|
||||
{
|
||||
title: sub.vendor ? `${sub.name} — ${sub.vendor}` : sub.name,
|
||||
description: buildDescription(sub),
|
||||
category: "subscription",
|
||||
fixed_price: sub.amount ?? undefined,
|
||||
currency: sub.currency ?? undefined,
|
||||
},
|
||||
{
|
||||
// Stable idempotency key so retried POSTs on the FleetCrown side
|
||||
// dedupe at OrangeCat (when server-side dedup lands).
|
||||
idempotencyKey: `fleetcrown_sub_${sub.id}`,
|
||||
},
|
||||
);
|
||||
console.log("[orangecat] subscription synced", {
|
||||
fleetcrown_subscription_id: sub.id,
|
||||
orangecat_service_id: service.id,
|
||||
});
|
||||
return service.id;
|
||||
} catch (err) {
|
||||
logSyncFailure(sub, err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function buildDescription(sub: SubscriptionForSync): string {
|
||||
const freq = sub.frequency ? sub.frequency.toLowerCase() : "recurring";
|
||||
const lines = [
|
||||
`FleetCrown subscription · ${freq}`,
|
||||
sub.notes ? `Note: ${sub.notes}` : null,
|
||||
].filter(Boolean);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function logSyncFailure(sub: SubscriptionForSync, err: unknown): void {
|
||||
if (err instanceof OrangeCatError) {
|
||||
console.warn("[orangecat] sync failed (non-fatal)", {
|
||||
fleetcrown_subscription_id: sub.id,
|
||||
code: err.code,
|
||||
status: err.status,
|
||||
message: err.message,
|
||||
retryAfter: err.retryAfter,
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.warn("[orangecat] sync failed (non-fatal, unknown error)", {
|
||||
fleetcrown_subscription_id: sub.id,
|
||||
err,
|
||||
});
|
||||
}
|
||||
Vendored
BIN
Binary file not shown.
Reference in new issue
Block a user