import type { ContextLinkType } from "./context"; /** * Store event API types */ import type { EventKind } from "./event_kind"; import type { EventSource } from "./source"; /** * Store Event Response (matches CreateEventResponse) * POST /v1/events. Response wrapped in ApiResponse { success, data }; client unwraps to this. */ export interface StoreEventResponse { /** * The memory this write landed on. Null when the admission gate filtered the * event, because there is no memory to name. */ locus_id: string | null; /** * What happened to the context. * * `created` — it held nothing. `unchanged` — byte-identical to what it held, * so a retry rather than an edit. `replaced` — different text, and the write * sent `replace: true`. `filtered` — the gate did not admit it. * * A write that would change what a context holds without `replace: true` is * refused with 409 rather than reported here. */ status: StoreStatus; /** How far the engine trusts this memory, 0 to 1. Null when filtered. */ confidence: number | null; /** * Normalized context_id the server stored, when the request carried one. * Omitted otherwise. */ context_id?: string; /** Error message when storage failed (e.g. in batch); omitted on success */ error_message?: string; } /** * API request body for POST /v1/events (matches CreateEventRequest). * Top-level link fields take precedence over payload.data when both present. * * Server payload: serde_json::Value - any JSON. No required structure. * Common formats: { data: ... }, { data: {...}, good_example, bad_example }, etc. * event_kind is at request level, not inside payload. * * @example * ```ts * await client.storeEvent({ * graph_id: 'my-graph', * event_kind: 'fact', * source: 'user', // provenance — sets base confidence * context_id: 'person:alice', // primary context (type:slug) * extends: ['org:acme'], // optional relationship links * payload: { data: 'Alice joined Acme' }, * }); * ``` */ export interface CreateEventApiRequest { /** Graph ID to store the event in */ graph_id: string; /** Event kind: "fact", "action", "decision", "observation", "feedback", etc. */ event_kind: EventKind; /** Primary context ID in format `{type}:{name}` (e.g. `constraint:file_exists`) */ context_id?: string; /** Event source (e.g. "agent", "validator") */ source?: EventSource; /** Context IDs for RelatedTo links */ related_to?: string[]; /** Context IDs for Extends links */ extends?: string[]; /** Context IDs for Reinforces links */ reinforces?: string[]; /** Context IDs for Contradicts links */ contradicts?: string[]; /** Arbitrary JSON. Server accepts any structure. Link inference looks for data.id, data.related_to, etc. */ payload: Record; /** Unix timestamp as string */ timestamp?: string; } /** * One event inside a batch — CreateEventApiRequest minus graph_id (the batch * shares one graph; the server rejects unknown fields on items). */ export type BatchEventItem = Omit; /** API request body for POST /v1/events/batch. */ export interface BatchStoreEventRequest { /** Graph all events are stored in. */ graph_id: string; /** Events, processed in order (so `extends` parents can precede children). Max 1000. */ events: BatchEventItem[]; } /** What a context write did, or that the gate refused the event. */ export type StoreStatus = "created" | "replaced" | "unchanged" | "filtered"; /** Per-item outcome in a batch response (same order as the request). */ export interface BatchEventResult { status: StoreStatus | "error"; /** Null when filtered, absent when the item errored. */ locus_id?: string | null; confidence?: number | null; /** Validation/storage error for this item; omitted on success. */ error?: string; } /** Response for POST /v1/events/batch. */ export interface BatchStoreEventResponse { stored: number; filtered: number; failed: number; results: BatchEventResult[]; } /** * One operation in a graph transaction. `op` defaults to `store`, so an * existing event item is already a valid store operation. */ export type TransactionOperation = ({ op?: "store"; } & BatchEventItem) | { op: "delete"; /** Context to remove, with any memory it was the last thread holding. */ context_id: string; } | { op: "unlink"; from_context_id: string; to_context_id: string; link_type: ContextLinkType; }; /** API request body for POST /v1/graphs/{graph_id}/transaction. */ export interface TransactionRequest { operations: TransactionOperation[]; } /** Per-operation outcome in a transaction response (same order as the request). */ export type TransactionResult = { op: "store"; status: "recorded" | "filtered"; locus_id: string | null; confidence: number | null; } | { op: "delete"; context_id: string; deleted_locus_ids: string[]; unlinked_locus_ids: string[]; } | { op: "unlink"; removed: boolean; links_removed: number; }; /** * Response for POST /v1/graphs/{graph_id}/transaction. * * There is no `failed` count: the whole request is one transaction, so a * rejected operation returns 400 naming its index and nothing is written. */ export interface TransactionResponse { applied: number; stored: number; filtered: number; results: TransactionResult[]; }