ec27228a8e
Em dashes (—) and en dashes (–) had spread across comments, docs, tests, and a few UI strings, reading as AI-generated boilerplate rather than house style. Replaced each with punctuation matching its context: colon for explanatory clauses, comma for asides, plain hyphen for numeric/legal ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for paired-dash asides. messages/en.json and messages/sv.json were fixed by hand together to keep sv/en in sync. Left untouched where the dash is the functional subject rather than decorative punctuation: date-range-parser.ts's separator regex, charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the agent system-prompt files that already instruct against em dashes, and a golden iXBRL test fixture compared byte-for-byte. Also fixes two bugs surfaced along the way: an off-by-one in ApiKeysPanel's scope-label split (a leftover from an earlier partial pass), and a charset-repair test that had lost the literal en-dash it exists to verify. Regenerated the agent atom seed migration (skills:generate) since 27 SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes, with an explicit carve-out for the functional-dash cases above. Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
159 lines
4.8 KiB
TypeScript
159 lines
4.8 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import type { CoreEvent } from '@/lib/events/types'
|
|
import { eventBus } from '@/lib/events/bus'
|
|
import { ingestTransactions } from '@/lib/transactions/ingest'
|
|
import { listForCompany as cashAccountsList, getPrimary as cashAccountsGetPrimary } from '@/lib/cash-accounts/service'
|
|
import { createLogger } from '@/lib/logger'
|
|
import type {
|
|
ExtensionContext,
|
|
ExtensionLogger,
|
|
ExtensionSettings,
|
|
ExtensionStorage,
|
|
ExtensionServices,
|
|
} from './types'
|
|
|
|
/**
|
|
* Create a prefixed logger for an extension. When `bind` is supplied the
|
|
* fields (e.g. requestId, userId, companyId) are merged into every log line.
|
|
*/
|
|
function createExtLogger(extensionId: string, bind?: Record<string, unknown>): ExtensionLogger {
|
|
const logger = bind
|
|
? createLogger(`ext:${extensionId}`, bind)
|
|
: createLogger(`ext:${extensionId}`)
|
|
return {
|
|
info: (message: string, ...args: unknown[]) => logger.info(message, ...args),
|
|
warn: (message: string, ...args: unknown[]) => logger.warn(message, ...args),
|
|
error: (message: string, ...args: unknown[]) => logger.error(message, ...args),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a settings accessor scoped to a specific extension.
|
|
*/
|
|
function createSettings(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
companyId: string,
|
|
extensionId: string
|
|
): ExtensionSettings {
|
|
return {
|
|
async get<T>(key?: string): Promise<T | null> {
|
|
const lookupKey = key ?? 'settings'
|
|
const { data } = await supabase
|
|
.from('extension_data')
|
|
.select('value')
|
|
.eq('company_id', companyId)
|
|
.eq('extension_id', extensionId)
|
|
.eq('key', lookupKey)
|
|
.single()
|
|
|
|
return (data?.value as T) ?? null
|
|
},
|
|
|
|
async set<T>(key: string, value: T): Promise<void> {
|
|
const { error } = await supabase
|
|
.from('extension_data')
|
|
.upsert(
|
|
{
|
|
user_id: userId,
|
|
company_id: companyId,
|
|
extension_id: extensionId,
|
|
key,
|
|
value,
|
|
},
|
|
{ onConflict: 'company_id,extension_id,key' }
|
|
)
|
|
if (error) {
|
|
throw new Error(`extension_data set failed for ${extensionId}/${key}: ${error.message}`)
|
|
}
|
|
},
|
|
|
|
async clear(key: string): Promise<void> {
|
|
const { error } = await supabase
|
|
.from('extension_data')
|
|
.delete()
|
|
.eq('company_id', companyId)
|
|
.eq('extension_id', extensionId)
|
|
.eq('key', key)
|
|
if (error) {
|
|
throw new Error(`extension_data clear failed for ${extensionId}/${key}: ${error.message}`)
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a storage accessor wrapping Supabase storage.
|
|
*/
|
|
function createStorage(supabase: SupabaseClient): ExtensionStorage {
|
|
return {
|
|
async download(bucket: string, path: string) {
|
|
const { data, error } = await supabase.storage
|
|
.from(bucket)
|
|
.download(path)
|
|
return { data, error: error?.message }
|
|
},
|
|
|
|
async upload(bucket: string, path: string, data: ArrayBuffer, options?: { contentType?: string }) {
|
|
const { error } = await supabase.storage
|
|
.from(bucket)
|
|
.upload(path, data, options ? { contentType: options.contentType } : undefined)
|
|
if (error) return { path: '', error: error.message }
|
|
return { path }
|
|
},
|
|
|
|
getPublicUrl(bucket: string, path: string): string {
|
|
const { data } = supabase.storage
|
|
.from(bucket)
|
|
.getPublicUrl(path)
|
|
return data.publicUrl
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create core services exposed to extensions.
|
|
*/
|
|
function createServices(): ExtensionServices {
|
|
return {
|
|
ingestTransactions,
|
|
getCashAccounts: (supabase, companyId, opts) => cashAccountsList(supabase, companyId, opts),
|
|
getPrimaryCashAccount: (supabase, companyId, currency) =>
|
|
cashAccountsGetPrimary(supabase, companyId, currency),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build a fully populated ExtensionContext.
|
|
*
|
|
* The context gives extensions access to Supabase, event emission, settings,
|
|
* storage, logging, and core services: without importing from core modules.
|
|
*
|
|
* `requestId` (when supplied by the dispatcher) flows through the bound logger
|
|
* and is exposed on the context so handlers can pass it into
|
|
* `errorResponseFromCode(...)` for the envelope + `X-Request-Id` header.
|
|
*/
|
|
export function createExtensionContext(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
companyId: string,
|
|
extensionId: string,
|
|
requestId?: string,
|
|
): ExtensionContext {
|
|
const logBindings: Record<string, unknown> = { userId, companyId, extensionId }
|
|
if (requestId) logBindings.requestId = requestId
|
|
|
|
return {
|
|
userId,
|
|
companyId,
|
|
extensionId,
|
|
requestId,
|
|
supabase,
|
|
emit: (event: CoreEvent) => eventBus.emit(event),
|
|
settings: createSettings(supabase, userId, companyId, extensionId),
|
|
storage: createStorage(supabase),
|
|
log: createExtLogger(extensionId, logBindings),
|
|
services: createServices(),
|
|
}
|
|
}
|