@harperfast/template-react-studio 1.6.2 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. package/.agents/skills/harper-best-practices/AGENTS.md +1428 -303
  2. package/.agents/skills/harper-best-practices/SKILL.md +24 -20
  3. package/.agents/skills/harper-best-practices/rules/adding-tables-with-schemas.md +4 -2
  4. package/.agents/skills/harper-best-practices/rules/automatic-apis.md +144 -16
  5. package/.agents/skills/harper-best-practices/rules/caching.md +134 -21
  6. package/.agents/skills/harper-best-practices/rules/checking-authentication.md +139 -148
  7. package/.agents/skills/harper-best-practices/rules/creating-a-fabric-account-and-cluster.md +2 -0
  8. package/.agents/skills/harper-best-practices/rules/creating-harper-apps.md +2 -0
  9. package/.agents/skills/harper-best-practices/rules/custom-resources.md +5 -3
  10. package/.agents/skills/harper-best-practices/rules/defining-relationships.md +2 -0
  11. package/.agents/skills/harper-best-practices/rules/deploying-to-harper-fabric.md +97 -77
  12. package/.agents/skills/harper-best-practices/rules/extending-tables.md +3 -1
  13. package/.agents/skills/harper-best-practices/rules/handling-binary-data.md +2 -0
  14. package/.agents/skills/harper-best-practices/rules/logging.md +154 -77
  15. package/.agents/skills/harper-best-practices/rules/programmatic-table-requests.md +91 -0
  16. package/.agents/skills/harper-best-practices/rules/querying-rest-apis.md +190 -15
  17. package/.agents/skills/harper-best-practices/rules/real-time-apps.md +80 -21
  18. package/.agents/skills/harper-best-practices/rules/schema-design-tooling.md +4 -2
  19. package/.agents/skills/harper-best-practices/rules/serving-web-content.md +3 -2
  20. package/.agents/skills/harper-best-practices/rules/typescript-type-stripping.md +3 -1
  21. package/.agents/skills/harper-best-practices/rules/using-blob-datatype.md +3 -1
  22. package/.agents/skills/harper-best-practices/rules/vector-indexing.md +85 -120
  23. package/.agents/skills/harper-best-practices/rules.manifest.yaml +258 -0
  24. package/package.json +1 -1
  25. package/skills-lock.json +1 -1
@@ -1,43 +1,102 @@
1
1
  ---
2
2
  name: real-time-apps
3
3
  description: How to build real-time features in Harper using WebSockets and Pub/Sub.
4
+ metadata:
5
+ mode: generate
6
+ sources:
7
+ - reference/v5/rest/websockets.md
8
+ sourceCommit: b7fbddadd42eb4487190b650a9abc4bcfeef5819
9
+ inputHash: a8afd4d3a52f77ba
4
10
  ---
5
11
 
6
- # Real-time Applications
12
+ # Real-Time Apps with WebSockets and Pub/Sub
7
13
 
8
- Instructions for the agent to follow when building real-time applications in Harper.
14
+ Instructions for the agent to follow when building real-time features in Harper using WebSockets and Pub/Sub.
9
15
 
10
16
  ## When to Use
11
17
 
12
- Use this skill when you need to stream live updates to clients, implement chat features, or provide real-time data synchronization between the database and a frontend.
18
+ Apply this rule when implementing any feature that requires real-time bidirectional communication, live data streaming, or push-based updates in a Harper application. This includes chat, live dashboards, sensor feeds, and any scenario where clients must receive resource changes as they happen.
13
19
 
14
20
  ## How It Works
15
21
 
16
- 1. **Check Automatic WebSockets**: If you only need to stream table changes, use [Automatic APIs](automatic-apis.md) which provide a WebSocket endpoint for every `@export`ed table.
17
- 2. **Implement `connect` in a Resource**: For custom bi-directional logic, implement the `connect` method.
18
- 3. **Use Pub/Sub**: Use `tables.TableName.subscribe(query)` to listen for specific data changes and stream them to the client.
19
- 4. **Handle SSE**: Ensure your `connect` method gracefully handles cases where `incomingMessages` is null (Server-Sent Events).
20
- 5. **Connect from Client**: Use standard WebSockets (`new WebSocket('wss://...')`) to connect to your resource endpoint. Ensure you use the appropriate scheme (`ws://` for HTTP, `wss://` for HTTPS).
22
+ 1. **Enable WebSocket support**: WebSocket support is enabled automatically when the `rest` plugin is enabled. To explicitly disable it, set the following in your config:
21
23
 
22
- ## Examples
24
+ ```yaml
25
+ rest:
26
+ webSocket: false
27
+ ```
23
28
 
24
- ### Bi-directional WebSocket Resource
29
+ 2. **Connect a client to a resource**: A WebSocket connection to a resource URL automatically subscribes to that resource. When the record changes or a message is published to it, the connection receives the update.
25
30
 
26
- ```typescript
27
- import { Resource, tables } from 'harperdb';
31
+ ```javascript
32
+ let ws = new WebSocket('wss://server/my-resource/341');
33
+ ws.onmessage = (event) => {
34
+ let data = JSON.parse(event.data);
35
+ };
36
+ ```
28
37
 
29
- export class MySocket extends Resource {
30
- async *connect(target, incomingMessages) {
31
- // Subscribe to table changes
32
- const subscription = await tables.MyTable.subscribe(target);
33
- if (!incomingMessages) {
34
- return subscription; // SSE mode
35
- }
38
+ `new WebSocket('wss://server/my-resource/341')` accesses the resource defined for `my-resource` with record id `341` and subscribes to it.
39
+
40
+ 3. **Implement a custom `connect()` handler**: Override the `connect(incomingMessages)` method on a resource class to control WebSocket behavior. The method must return an async iterable (or generator) that produces messages to send to the client. See [automatic-apis.md](automatic-apis.md) for more on defining resource classes.
41
+
42
+ 4. **Use the default `connect()` for event-style access**: Call `super.connect()` to get a streaming iterable that provides:
43
+ - A `send(message)` method for pushing outgoing messages
44
+ - A `close` event for cleanup on disconnect
45
+
46
+ 5. **Handle message ordering in distributed environments**: Harper delivers messages to local subscribers immediately without inter-node coordination delay.
47
+
48
+ | Message Type | Behavior |
49
+ | -------------------------------------------------------- | ----------------------------------------------------------------------- |
50
+ | Non-retained (no `retain` flag) | Every message delivered in order received; suitable for chat |
51
+ | Retained (published with `retain`, or PUT/updated in DB) | Only the latest-timestamp message is kept; suitable for sensor readings |
36
52
 
37
- // Handle incoming client messages
53
+ 6. **Use MQTT over WebSockets** when needed by setting the sub-protocol header:
54
+ ```
55
+ Sec-WebSocket-Protocol: mqtt
56
+ ```
57
+
58
+ ## Examples
59
+
60
+ **Simple echo server** — override `connect(incomingMessages)` to yield each incoming message back to the client:
61
+
62
+ ```javascript
63
+ export class Echo extends Resource {
64
+ async *connect(incomingMessages) {
38
65
  for await (let message of incomingMessages) {
39
- yield { received: message };
66
+ yield message; // echo each message back
40
67
  }
41
68
  }
42
69
  }
43
70
  ```
71
+
72
+ **Custom connect with timer and event-style access** — use `super.connect()` to get the outgoing stream, push periodic messages, echo incoming messages, and clean up on disconnect:
73
+
74
+ ```javascript
75
+ export class Example extends Resource {
76
+ connect(incomingMessages) {
77
+ let outgoingMessages = super.connect();
78
+
79
+ let timer = setInterval(() => {
80
+ outgoingMessages.send({ greeting: 'hi again!' });
81
+ }, 1000);
82
+
83
+ incomingMessages.on('data', (message) => {
84
+ outgoingMessages.send(message); // echo incoming messages
85
+ });
86
+
87
+ outgoingMessages.on('close', () => {
88
+ clearInterval(timer);
89
+ });
90
+
91
+ return outgoingMessages;
92
+ }
93
+ }
94
+ ```
95
+
96
+ ## Notes
97
+
98
+ - WebSocket connections target a resource URL path. By default, connecting to a resource subscribes to changes for that resource.
99
+ - The `connect(incomingMessages)` method **must** return an async iterable or generator; returning a plain value will not work.
100
+ - `super.connect()` returns a streaming iterable with `send(message)` and a `close` event — use this when you need to push messages outside of the incoming message loop.
101
+ - For one-way real-time streaming without bidirectional communication, consider Server-Sent Events instead.
102
+ - For full pub/sub capabilities, Harper also supports MQTT; set `Sec-WebSocket-Protocol: mqtt` to use MQTT over WebSockets.
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  name: schema-design-tooling
3
3
  description: Best practices for Harper schema design, including core directives and GraphQL tooling configuration.
4
+ metadata:
5
+ mode: synthesized
4
6
  ---
5
7
 
6
8
  # Schema Design & Tooling
@@ -9,7 +11,7 @@ Harper uses GraphQL schemas to define database tables, relationships, and APIs.
9
11
 
10
12
  ## Core Harper Directives
11
13
 
12
- Harper extends GraphQL with custom directives that define database behavior. These are typically defined in `node_modules/harperdb/schema.graphql`. If you don't have access to that file, here is a reference of the most important ones:
14
+ Harper extends GraphQL with custom directives that define database behavior. These are typically defined in `node_modules/harper/schema.graphql`. If you don't have access to that file, here is a reference of the most important ones:
13
15
 
14
16
  ### Table Definition
15
17
 
@@ -43,7 +45,7 @@ Create a file named `graphql.config.yml` in your project root with the following
43
45
 
44
46
  ```yaml
45
47
  schema:
46
- - 'node_modules/harperdb/schema.graphql'
48
+ - 'node_modules/harper/schema.graphql'
47
49
  - 'schema.graphql'
48
50
  - 'schemas/*.graphql'
49
51
  ```
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  name: serving-web-content
3
3
  description: How to serve static files and integrated Vite/React applications in Harper.
4
+ metadata:
5
+ mode: synthesized
4
6
  ---
5
7
 
6
8
  # Serving Web Content
@@ -59,7 +61,6 @@ Use this skill when you need to serve a frontend (HTML, CSS, JS, or a React app)
59
61
  4. **Deploy for Production**: For Vite apps, use a build script to generate static files into a `web/` folder and deploy them using the static handler pattern. For example, these scripts in a package.json can perform the necessary steps:
60
62
  ```json
61
63
  "build": "vite build",
62
- "deploy": "rm -Rf deploy && npm run build && mkdir deploy && mv web deploy/ && cp -R deploy-template/* deploy/ && cp -R schemas resources deploy/ && dotenv -- npm run deploy:component && rm -Rf deploy",
63
- "deploy:component": "(cd deploy && harper deploy_component . project=web restart=rolling replicated=true)"
64
+ "deploy": "rm -Rf deploy && npm run build && mkdir deploy && mv web deploy/ && cp -R deploy-template/* deploy/ && cp -R schemas resources deploy/ && (cd deploy && harper deploy_component . project=web restart=rolling replicated=true) && rm -Rf deploy",
64
65
  ```
65
66
  Then in production, the "Static Plugin" option will performantly and securely serve your assets. `npm create harper@latest` scaffolds all of this for you.
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  name: typescript-type-stripping
3
3
  description: How to run TypeScript files directly in Harper without a build step.
4
+ metadata:
5
+ mode: synthesized
4
6
  ---
5
7
 
6
8
  # TypeScript Type Stripping
@@ -17,7 +19,7 @@ Use this skill when you want to write Harper Resources in TypeScript and have th
17
19
  2. **Name Files with `.ts`**: Create your resource files in the `resources/` directory with a `.ts` extension.
18
20
  3. **Use TypeScript Syntax**: Write your resource classes using standard TypeScript (interfaces, types, etc.).
19
21
  ```typescript
20
- import { Resource } from 'harperdb';
22
+ import { Resource } from 'harper';
21
23
  export class MyResource extends Resource {
22
24
  async get(): Promise<{ message: string }> {
23
25
  return { message: 'Running TS directly!' };
@@ -1,6 +1,8 @@
1
1
  ---
2
2
  name: using-blob-datatype
3
3
  description: How to use the Blob data type for efficient binary storage in Harper.
4
+ metadata:
5
+ mode: synthesized
4
6
  ---
5
7
 
6
8
  # Using Blob Datatype
@@ -22,7 +24,7 @@ Use this skill when you need to store unstructured or large binary data (media,
22
24
  ```
23
25
  2. **Create and Store Blobs**: Use `createBlob()` from Harper's globals to wrap Buffers or Streams:
24
26
  ```javascript
25
- import { tables } from 'harperdb';
27
+ import { tables } from 'harper';
26
28
  const blob = createBlob(largeBuffer);
27
29
  await tables.MyTable.put('my-id', { data: blob });
28
30
  ```
@@ -1,40 +1,56 @@
1
1
  ---
2
2
  name: vector-indexing
3
3
  description: How to enable and query vector indexes for similarity search in Harper.
4
+ metadata:
5
+ mode: generate
6
+ sources:
7
+ - reference/v5/database/schema.md#Vector Indexing
8
+ sourceCommit: b7fbddadd42eb4487190b650a9abc4bcfeef5819
9
+ inputHash: 3732961c671aac00
4
10
  ---
5
11
 
6
12
  # Vector Indexing
7
13
 
8
- Instructions for the agent to follow when implementing vector search in Harper.
14
+ Instructions for the agent to follow when enabling and querying vector indexes for similarity search in Harper using the HNSW algorithm.
9
15
 
10
16
  ## When to Use
11
17
 
12
- Use this skill when you need to perform similarity searches on high-dimensional data, such as AI embeddings for semantic search, recommendations, or image retrieval.
18
+ Apply this rule when adding a vector index to a Harper table schema or writing similarity search queries against high-dimensional vector fields. Use it whenever you need approximate nearest-neighbor search, distance-threshold filtering, or distance-scored results.
13
19
 
14
20
  ## How It Works
15
21
 
16
- 1. **Enable Vector Indexing**: In your GraphQL schema, add `@indexed(type: "HNSW")` to a numeric array field:
22
+ 1. **Declare the vector index on a `[Float]` field**: Add `@indexed(type: "HNSW")` to any `[Float]` attribute in a `@table` type. See [adding-tables-with-schemas.md](adding-tables-with-schemas.md) for general schema setup.
23
+
17
24
  ```graphql
18
- type Product @table {
19
- id: ID @primaryKey
25
+ type Document @table {
26
+ id: Long @primaryKey
20
27
  textEmbeddings: [Float] @indexed(type: "HNSW")
21
28
  }
22
29
  ```
23
- 2. **Configure Index Options (Optional)**: Fine-tune the index with parameters like `distance` (`cosine` or `euclidean`), `M`, and `efConstruction`.
24
- 3. **Query with Vector Search**: Use `tables.Table.search()` with a `sort` object containing the `target` vector:
30
+
31
+ 2. **Query by nearest neighbors using `sort`**: Call `Document.search()` with a `sort` object containing `attribute` (the indexed field name) and `target` (the query vector). Include `limit` to cap results.
32
+
25
33
  ```javascript
26
- const results = await tables.Product.search({
27
- select: ['name', '$distance'],
28
- sort: {
29
- attribute: 'textEmbeddings',
30
- target: [0.1, 0.2, ...], // query vector
31
- },
32
- limit: 5,
34
+ let results = Document.search({
35
+ sort: { attribute: 'textEmbeddings', target: searchVector },
36
+ limit: 5,
33
37
  });
34
38
  ```
35
- 4. **Filter by Distance**: Use `conditions` with a `target` vector and a `comparator` (e.g., `lt`) to return results within a similarity threshold:
39
+
40
+ 3. **Combine with filter conditions**: Add a `conditions` array alongside `sort` to pre-filter records before ranking by similarity.
41
+
36
42
  ```javascript
37
- const results = await tables.Product.search({
43
+ let results = Document.search({
44
+ conditions: [{ attribute: 'price', comparator: 'lt', value: 50 }],
45
+ sort: { attribute: 'textEmbeddings', target: searchVector },
46
+ limit: 5,
47
+ });
48
+ ```
49
+
50
+ 4. **Filter by distance threshold**: To return only records within a similarity cutoff (without ranking), place `target` directly on the condition alongside `comparator` and `value`. Omit `sort`.
51
+
52
+ ```javascript
53
+ let results = Document.search({
38
54
  conditions: {
39
55
  attribute: 'textEmbeddings',
40
56
  comparator: 'lt',
@@ -43,117 +59,66 @@ Use this skill when you need to perform similarity searches on high-dimensional
43
59
  },
44
60
  });
45
61
  ```
46
- 5. **Generate Embeddings**: Use external services (OpenAI, Ollama) to generate the numeric vectors before storing or searching them in Harper.
47
-
48
- ```typescript
49
- import OpenAI from 'openai';
50
- import ollama from 'ollama';
51
-
52
- const { Product } = tables;
53
- const openai = new OpenAI();
54
- // the name of the OpenAI embedding model
55
- const OPENAI_EMBEDDING_MODEL = 'text-embedding-3-small';
56
-
57
- // the name of the Ollama embedding model
58
- const OLLAMA_EMBEDDING_MODEL = 'llama3';
59
-
60
- const SIMILARITY_THRESHOLD = 0.5;
61
-
62
- export class ProductSearch extends Resource {
63
- // based on env variable we choose the appropriate embedding generator
64
- generateEmbedding =
65
- process.env.EMBEDDING_GENERATOR === 'ollama'
66
- ? this._generateOllamaEmbedding
67
- : this._generateOpenAIEmbedding;
68
-
69
- /**
70
- * Executes a search query using a generated text embedding and returns the matching products.
71
- *
72
- * @param {Object} data - The input data for the request.
73
- * @param {string} data.prompt - The prompt to generate the text embedding from.
74
- * @return {Promise<Array>} Returns a promise that resolves to an array of products matching the conditions,
75
- * including fields: name, description, price, and $distance.
76
- */
77
- async post(data) {
78
- const embedding = await this.generateEmbedding(data.prompt);
79
-
80
- return await Product.search({
81
- select: ['name', 'description', 'price', '$distance'],
82
- conditions: {
83
- attribute: 'textEmbeddings',
84
- comparator: 'lt',
85
- value: SIMILARITY_THRESHOLD,
86
- target: embedding[0],
87
- },
88
- limit: 5,
89
- });
90
- }
91
-
92
- /**
93
- * Generates an embedding using the Ollama API.
94
- *
95
- * @param {string} promptData - The input data for which the embedding is to be generated.
96
- * @return {Promise<number[][]>} A promise that resolves to the generated embedding as an array of numbers.
97
- */
98
- async _generateOllamaEmbedding(promptData) {
99
- const embedding = await ollama.embed({
100
- model: OLLAMA_EMBEDDING_MODEL,
101
- input: promptData,
102
- });
103
- return embedding?.embeddings;
104
- }
105
-
106
- /**
107
- * Generates OpenAI embeddings based on the given prompt data.
108
- *
109
- * @param {string} promptData - The input data used for generating the embedding.
110
- * @return {Promise<number[][]>} A promise that resolves to an array of embeddings, where each embedding is an array of floats.
111
- */
112
- async _generateOpenAIEmbedding(promptData) {
113
- const embedding = await openai.embeddings.create({
114
- model: OPENAI_EMBEDDING_MODEL,
115
- input: promptData,
116
- encoding_format: 'float',
117
- });
118
-
119
- let embeddings = [];
120
- embedding.data.forEach((embeddingData) => {
121
- embeddings.push(embeddingData.embedding);
122
- });
123
-
124
- return embeddings;
125
- }
126
- }
127
- ```
128
62
 
129
- ## Examples
63
+ 5. **Include computed distance in results**: Use the special `$distance` field in `select` to return the distance from the target vector. Works with both `sort`-based and `conditions`-based queries.
130
64
 
131
- Sample request to the `ProductSearch` resource which prompts to find "shorts for the gym":
65
+ ```javascript
66
+ let results = Document.search({
67
+ select: ['name', '$distance'],
68
+ sort: { attribute: 'textEmbeddings', target: searchVector },
69
+ limit: 5,
70
+ });
71
+ ```
132
72
 
133
- ```bash
134
- curl -X POST "http://localhost:9926/ProductSearch/" \
135
- -H "Accept: application/json" \
136
- -H "Content-Type: application/json" \
137
- -H "Authorization: Basic <YOUR_AUTH>" \
138
- -d '{"prompt": "shorts for the gym"}'
139
- ```
73
+ 6. **Tune HNSW parameters**: Pass additional parameters to `@indexed(type: "HNSW", ...)` to control index quality and performance.
140
74
 
141
- ---
75
+ | Parameter | Default | Description |
76
+ | ---------------------- | ----------------- | --------------------------------------------------------------------------------------------------- |
77
+ | `distance` | `"cosine"` | Distance function: `"euclidean"` or `"cosine"` (negative cosine similarity) |
78
+ | `efConstruction` | `100` | Max nodes explored during index construction. Higher = better recall, lower = better performance |
79
+ | `M` | `16` | Preferred connections per graph layer. Higher = more space, better recall for high-dimensional data |
80
+ | `optimizeRouting` | `0.5` | Heuristic aggressiveness for omitting redundant connections (0 = off, 1 = most aggressive) |
81
+ | `mL` | computed from `M` | Normalization factor for level generation |
82
+ | `efSearchConstruction` | `50` | Max nodes explored during search |
142
83
 
143
- ## When to Use Vector Indexing
84
+ ## Examples
144
85
 
145
- Vector indexing is ideal when:
86
+ **Schema with custom HNSW parameters:**
146
87
 
147
- - Storing embedding vectors from ML models
148
- - Performing semantic or similarity-based search
149
- - Working with high-dimensional numeric data
150
- - Exact-match indexes are insufficient
88
+ ```graphql
89
+ type Document @table {
90
+ id: Long @primaryKey
91
+ textEmbeddings: [Float]
92
+ @indexed(type: "HNSW", distance: "euclidean", optimizeRouting: 0, efSearchConstruction: 100)
93
+ }
94
+ ```
151
95
 
152
- ---
96
+ **Nearest-neighbor search with distance score:**
97
+
98
+ ```javascript
99
+ let results = Document.search({
100
+ select: ['name', '$distance'],
101
+ sort: { attribute: 'textEmbeddings', target: searchVector },
102
+ limit: 5,
103
+ });
104
+ ```
105
+
106
+ **Distance-threshold filter (no ranking):**
107
+
108
+ ```javascript
109
+ let results = Document.search({
110
+ conditions: {
111
+ attribute: 'textEmbeddings',
112
+ comparator: 'lt',
113
+ value: 0.1,
114
+ target: searchVector,
115
+ },
116
+ });
117
+ ```
153
118
 
154
- ## Summary
119
+ ## Notes
155
120
 
156
- - Vector indexing enables fast similarity search on numeric arrays
157
- - Defined using `@indexed(type: "HNSW")`
158
- - Queried using a target vector in search sorting
159
- - Tunable for performance and accuracy
121
+ - The default `distance` function is `cosine`. Pass `distance: "euclidean"` to switch.
122
+ - `efConstruction` controls index build quality; raising it improves recall at the cost of build time.
123
+ - `$distance` is available in both `sort`-based ranking and `conditions`-based threshold queries.
124
+ - Use the threshold (`conditions` + `target`) form when you want to bound result quality by a similarity cutoff rather than ranking by similarity.