Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions bindings/nodejs/dts-header.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,51 @@ export interface PolicyReferenceSite {
kind: PolicyReferenceKind;
}

/** What kind of content a `PolicySearchHit` matched. */
export type PolicySearchHitKind =
| 'document'
| 'heading'
| 'paragraph'
| 'listItem'
| 'codeBlock'
| 'expression'
| 'expressionKey'
| 'tableColumn'
| 'tableCell'
| 'matchKey'
| 'matchCondition'
| 'matchValue'
| 'assertionOutput'
| 'assertionCondition'
| 'dataModel'
| 'dataModelProperty'
| 'dictionary'
| 'dictionaryEntry'
| 'graphNode'
| 'switchCondition'
| 'function';

/**
* One ranked fuzzy-search hit returned by `search()`. Location fields are
* populated per kind: policy hits carry `blockId` (plus `expressionId`,
* `row`/`column` for table cells), graph hits carry `nodeId`. `span` is a
* character range within `text` (which may be a snippet of long sources).
*/
export interface PolicySearchHit {
path: string;
blockId?: string;
nodeId?: string;
expressionId?: string;
row?: number;
column?: string;
kind: PolicySearchHitKind;
text: string;
/** Human context for the hit, e.g. column name, entity, or node name. */
context?: string;
span: PolicySpan;
score: number;
}

/**
* One node in the transitive dependency tree returned by `dependencies()`.
* Each node names a property; `writtenBy` is the block that produces it
Expand Down
46 changes: 46 additions & 0 deletions bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,51 @@ export interface PolicyReferenceSite {
kind: PolicyReferenceKind;
}

/** What kind of content a `PolicySearchHit` matched. */
export type PolicySearchHitKind =
| 'document'
| 'heading'
| 'paragraph'
| 'listItem'
| 'codeBlock'
| 'expression'
| 'expressionKey'
| 'tableColumn'
| 'tableCell'
| 'matchKey'
| 'matchCondition'
| 'matchValue'
| 'assertionOutput'
| 'assertionCondition'
| 'dataModel'
| 'dataModelProperty'
| 'dictionary'
| 'dictionaryEntry'
| 'graphNode'
| 'switchCondition'
| 'function';

/**
* One ranked fuzzy-search hit returned by `search()`. Location fields are
* populated per kind: policy hits carry `blockId` (plus `expressionId`,
* `row`/`column` for table cells), graph hits carry `nodeId`. `span` is a
* character range within `text` (which may be a snippet of long sources).
*/
export interface PolicySearchHit {
path: string;
blockId?: string;
nodeId?: string;
expressionId?: string;
row?: number;
column?: string;
kind: PolicySearchHitKind;
text: string;
/** Human context for the hit, e.g. column name, entity, or node name. */
context?: string;
span: PolicySpan;
score: number;
}

/**
* One node in the transitive dependency tree returned by `dependencies()`.
* Each node names a property; `writtenBy` is the block that produces it
Expand Down Expand Up @@ -418,6 +463,7 @@ export declare class Workspace {
prepareRename(cursor: PolicyExpressionCursor): PolicyPrepareRenameResult | null
rename(req: PolicyRenameRequest): PolicyEngineEdit[]
references(target: any): PolicyReferenceSite[]
search(query: string, limit?: number | undefined | null): PolicySearchHit[]
inputSkeleton(req: PolicyScopeRequest): unknown
dependencies(target: string, document?: string | undefined | null): PolicyDependencyNode
evaluate(req: PolicyEvaluateRequest): PolicyEvaluationResult
Expand Down
10 changes: 10 additions & 0 deletions bindings/nodejs/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,16 @@ impl Workspace {
.collect())
}

#[napi(ts_return_type = "PolicySearchHit[]")]
pub fn search(&self, query: String, limit: Option<u32>) -> napi::Result<Vec<Value>> {
Ok(self
.inner
.search(&query, limit)
.into_iter()
.map(|h| serde_json::to_value(h).expect("SearchHit serializes"))
.collect())
}

#[napi(ts_return_type = "unknown")]
pub fn input_skeleton(&self, env: Env, req: PolicyScopeRequest) -> napi::Result<Value> {
self.ensure_function_types(&env)?;
Expand Down
9 changes: 7 additions & 2 deletions core/engine/src/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod db;
pub(crate) mod editor;
pub(crate) mod graph;
pub(crate) mod search;
pub(crate) mod types;

use std::sync::Arc;
Expand All @@ -23,8 +24,8 @@ pub use types::{
EngineEdit, Entity, EntityField, EvaluateRequest, EvaluationError, EvaluationResult,
ExpressionKind, FieldOrigin, GuardedProperty, InputProperty, InputValidationError,
InspectResult, NlExpression, OutputProperty, PrepareRename, PropertyKind, ReferenceKind,
ReferenceSite, RenameTarget, SchemaFieldKind, SchemaGroup, ScopeRequest, Severity, Span, Trace,
WriteConflict, WriteTrace,
ReferenceSite, RenameTarget, SchemaFieldKind, SchemaGroup, ScopeRequest, SearchHit,
SearchHitKind, Severity, Span, Trace, WriteConflict, WriteTrace,
};

use types::Global;
Expand Down Expand Up @@ -196,6 +197,10 @@ impl Workspace {
self.db.references(target)
}

pub fn search(&self, query: &str, limit: Option<u32>) -> Vec<SearchHit> {
self.db.search(query, limit)
}

pub fn input_skeleton(&self, req: &ScopeRequest) -> serde_json::Value {
self.db.input_skeleton(req)
}
Expand Down
Loading
Loading