@terreno/ai 57.1.0 → 57.3.0

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.
@@ -1,6 +1,21 @@
1
1
  # Terreno
2
2
 
3
- A monorepo containing shared packages for building full-stack applications with React Native and Express/Mongoose.
3
+ Terreno is Django/Rails for TypeScript — with universal app support.
4
+
5
+ Terreno is Django/Rails for TypeScript — a batteries-included, full-stack
6
+ framework where the undifferentiated 80% of an app is already written. On the
7
+ backend you get Mongoose models, auto-generated REST APIs, permissions, an admin
8
+ panel, authentication, and an AI service. On the frontend you get one universal
9
+ app — a single React Native codebase that ships to iOS, Android, and web. It is
10
+ built to be driven by AI coding agents from the first prompt to a production
11
+ deploy.
12
+
13
+ - Batteries included — auth, CRUD APIs, admin, permissions, AI, realtime,
14
+ feature flags, and consent are already built, so your code is business logic.
15
+ - Universal by default — one React Native codebase ships to iOS, Android, and
16
+ web. Not a web framework with a mobile bolt-on.
17
+ - AI-native — agents are a first-class client of the framework, not an
18
+ afterthought.
4
19
 
5
20
  ## Packages
6
21
 
@@ -151,7 +166,7 @@ import {
151
166
 
152
167
  ### @terreno/ui
153
168
 
154
- React Native component library with 88+ components:
169
+ React Native UI component library (a large component library):
155
170
 
156
171
  - **Layout**: Box, Page, SplitPage, Card
157
172
  - **Forms**: TextField, SelectField, DateTimeField, CheckBox
@@ -5,7 +5,22 @@ globs: **/*
5
5
 
6
6
  # Terreno
7
7
 
8
- A monorepo containing shared packages for building full-stack applications with React Native and Express/Mongoose.
8
+ Terreno is Django/Rails for TypeScript — with universal app support.
9
+
10
+ Terreno is Django/Rails for TypeScript — a batteries-included, full-stack
11
+ framework where the undifferentiated 80% of an app is already written. On the
12
+ backend you get Mongoose models, auto-generated REST APIs, permissions, an admin
13
+ panel, authentication, and an AI service. On the frontend you get one universal
14
+ app — a single React Native codebase that ships to iOS, Android, and web. It is
15
+ built to be driven by AI coding agents from the first prompt to a production
16
+ deploy.
17
+
18
+ - Batteries included — auth, CRUD APIs, admin, permissions, AI, realtime,
19
+ feature flags, and consent are already built, so your code is business logic.
20
+ - Universal by default — one React Native codebase ships to iOS, Android, and
21
+ web. Not a web framework with a mobile bolt-on.
22
+ - AI-native — agents are a first-class client of the framework, not an
23
+ afterthought.
9
24
 
10
25
  ## Packages
11
26
 
@@ -206,7 +221,7 @@ router.get("/yourRoute/:id", [
206
221
 
207
222
  ### @terreno/ui
208
223
 
209
- React Native component library with 88+ components:
224
+ React Native UI component library (a large component library):
210
225
 
211
226
  - **Layout**: Box, Page, SplitPage, Card
212
227
  - **Forms**: TextField, SelectField, DateTimeField, CheckBox
@@ -1,6 +1,21 @@
1
1
  # Terreno
2
2
 
3
- A monorepo containing shared packages for building full-stack applications with React Native and Express/Mongoose.
3
+ Terreno is Django/Rails for TypeScript — with universal app support.
4
+
5
+ Terreno is Django/Rails for TypeScript — a batteries-included, full-stack
6
+ framework where the undifferentiated 80% of an app is already written. On the
7
+ backend you get Mongoose models, auto-generated REST APIs, permissions, an admin
8
+ panel, authentication, and an AI service. On the frontend you get one universal
9
+ app — a single React Native codebase that ships to iOS, Android, and web. It is
10
+ built to be driven by AI coding agents from the first prompt to a production
11
+ deploy.
12
+
13
+ - Batteries included — auth, CRUD APIs, admin, permissions, AI, realtime,
14
+ feature flags, and consent are already built, so your code is business logic.
15
+ - Universal by default — one React Native codebase ships to iOS, Android, and
16
+ web. Not a web framework with a mobile bolt-on.
17
+ - AI-native — agents are a first-class client of the framework, not an
18
+ afterthought.
4
19
 
5
20
  ## Packages
6
21
 
@@ -201,7 +216,7 @@ router.get("/yourRoute/:id", [
201
216
 
202
217
  ### @terreno/ui
203
218
 
204
- React Native component library with 88+ components:
219
+ React Native UI component library (a large component library):
205
220
 
206
221
  - **Layout**: Box, Page, SplitPage, Card
207
222
  - **Forms**: TextField, SelectField, DateTimeField, CheckBox
package/AGENTS.md CHANGED
@@ -1,6 +1,21 @@
1
1
  # Terreno
2
2
 
3
- A monorepo containing shared packages for building full-stack applications with React Native and Express/Mongoose.
3
+ Terreno is Django/Rails for TypeScript — with universal app support.
4
+
5
+ Terreno is Django/Rails for TypeScript — a batteries-included, full-stack
6
+ framework where the undifferentiated 80% of an app is already written. On the
7
+ backend you get Mongoose models, auto-generated REST APIs, permissions, an admin
8
+ panel, authentication, and an AI service. On the frontend you get one universal
9
+ app — a single React Native codebase that ships to iOS, Android, and web. It is
10
+ built to be driven by AI coding agents from the first prompt to a production
11
+ deploy.
12
+
13
+ - Batteries included — auth, CRUD APIs, admin, permissions, AI, realtime,
14
+ feature flags, and consent are already built, so your code is business logic.
15
+ - Universal by default — one React Native codebase ships to iOS, Android, and
16
+ web. Not a web framework with a mobile bolt-on.
17
+ - AI-native — agents are a first-class client of the framework, not an
18
+ afterthought.
4
19
 
5
20
  ## Packages
6
21
 
@@ -201,7 +216,7 @@ router.get("/yourRoute/:id", [
201
216
 
202
217
  ### @terreno/ui
203
218
 
204
- React Native component library with 88+ components:
219
+ React Native UI component library (a large component library):
205
220
 
206
221
  - **Layout**: Box, Page, SplitPage, Card
207
222
  - **Forms**: TextField, SelectField, DateTimeField, CheckBox
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # @terreno/ai
2
2
 
3
+ AI service layer for Terreno apps - GPT chat, request logging, and admin tools.
4
+
3
5
  AI service layer for Terreno backends — provider-agnostic chat, request logging, GPT history, projects, file uploads, MCP tools, and optional Langfuse integration.
4
6
 
5
7
  ## Install
package/package.json CHANGED
@@ -15,12 +15,12 @@
15
15
  "luxon": "^3.7.2",
16
16
  "multer": "2.2.0"
17
17
  },
18
- "description": "AI service layer for Terreno apps - GPT chat, request logging, and admin tools.",
18
+ "description": "Provider-agnostic AI service with streaming chat, request logging, and admin tools — part of Terreno, the batteries-included TypeScript framework for universal apps.",
19
19
  "devDependencies": {
20
20
  "@biomejs/biome": "^2.3.6",
21
- "@terreno/api": "57.1.0",
22
- "@terreno/test": "57.1.0",
23
- "@types/bun": "^1.3.8",
21
+ "@terreno/api": "57.3.0",
22
+ "@terreno/test": "57.3.0",
23
+ "@types/bun": "^1.4.0",
24
24
  "@types/express": "^5.0.6",
25
25
  "@types/luxon": "^3.7.1",
26
26
  "@types/multer": "^1.4.12",
@@ -71,7 +71,7 @@
71
71
  "access": "public"
72
72
  },
73
73
  "peerDependencies": {
74
- "@terreno/api": "57.1.0",
74
+ "@terreno/api": "57.3.0",
75
75
  "mongoose": "^8.0.0 || ^9.0.0"
76
76
  },
77
77
  "repository": {
@@ -91,5 +91,5 @@
91
91
  "test:coverage": "bun run ../scripts/check-coverage.ts"
92
92
  },
93
93
  "types": "dist/index.d.ts",
94
- "version": "57.1.0"
94
+ "version": "57.3.0"
95
95
  }
@@ -1555,6 +1555,50 @@ describe("AI Routes", () => {
1555
1555
  expect(types).toEqual(["general", "remix"]);
1556
1556
  });
1557
1557
 
1558
+ it("filters by repeated requestType query parameters", async () => {
1559
+ await AIRequest.create({
1560
+ aiModel: "gpt-4",
1561
+ prompt: "remix prompt",
1562
+ requestType: "remix",
1563
+ response: "remix response",
1564
+ });
1565
+ await AIRequest.create({
1566
+ aiModel: "gpt-4",
1567
+ prompt: "general prompt",
1568
+ requestType: "general",
1569
+ response: "general response",
1570
+ });
1571
+ await AIRequest.create({
1572
+ aiModel: "gpt-4",
1573
+ prompt: "json prompt",
1574
+ requestType: "json_object",
1575
+ response: "json response",
1576
+ });
1577
+
1578
+ const agent = await authAsUser(app, "admin");
1579
+ const res = await agent.get("/aiRequestsExplorer?requestType=remix&requestType=general");
1580
+
1581
+ expect(res.status).toBe(200);
1582
+ expect(res.body.total).toBe(2);
1583
+ const types = (res.body.data as {requestType: string}[]).map((row) => row.requestType).sort();
1584
+ expect(types).toEqual(["general", "remix"]);
1585
+ });
1586
+
1587
+ it("ignores blank and non-string requestType values", async () => {
1588
+ await AIRequest.create({
1589
+ aiModel: "gpt-4",
1590
+ prompt: "general prompt",
1591
+ requestType: "general",
1592
+ response: "general response",
1593
+ });
1594
+
1595
+ const agent = await authAsUser(app, "admin");
1596
+ const res = await agent.get("/aiRequestsExplorer?requestType=,%20&requestType[key]=general");
1597
+
1598
+ expect(res.status).toBe(200);
1599
+ expect(res.body.total).toBe(1);
1600
+ });
1601
+
1558
1602
  it("filters by model query parameter", async () => {
1559
1603
  await AIRequest.create({
1560
1604
  aiModel: "gpt-4",
@@ -1,5 +1,6 @@
1
1
  import {afterEach, beforeEach, describe, expect, it, mock} from "bun:test";
2
2
  import {jsonSchema, type LanguageModel} from "ai";
3
+ import {assert} from "chai";
3
4
  import mongoose from "mongoose";
4
5
 
5
6
  import {AIRequest} from "../models/aiRequest";
@@ -204,6 +205,55 @@ describe("AIService", () => {
204
205
 
205
206
  expect(result).toEqual({count: 9, id: "fenced"});
206
207
  });
208
+
209
+ it("parses object output that has a prose preamble and a trailing comma", async () => {
210
+ const model = createMockModel(
211
+ 'Sure! Here is the object you asked for:\n{"id":"messy","count":4,}\nLet me know if you need more.'
212
+ );
213
+ const service = new AIService({model: model as unknown as LanguageModel});
214
+
215
+ const schema = jsonSchema<{count: number; id: string}>({
216
+ additionalProperties: false,
217
+ properties: {
218
+ count: {type: "number"},
219
+ id: {type: "string"},
220
+ },
221
+ required: ["id", "count"],
222
+ type: "object",
223
+ });
224
+
225
+ const result = await service.generateJsonObject({
226
+ prompt: "Extract fields",
227
+ schema,
228
+ });
229
+
230
+ assert.deepEqual(result, {count: 4, id: "messy"});
231
+ });
232
+
233
+ it("logs a sentinel response when the model returns no text content", async () => {
234
+ const model = createMockModel();
235
+ model.doGenerate = mock(async () => ({
236
+ content: [],
237
+ finishReason: "stop" as const,
238
+ usage: {inputTokens: 5, outputTokens: 0},
239
+ }));
240
+ const service = new AIService({model: model as unknown as LanguageModel});
241
+ const schema = jsonSchema<{id: string}>({
242
+ properties: {id: {type: "string"}},
243
+ required: ["id"],
244
+ type: "object",
245
+ });
246
+
247
+ await expect(
248
+ service.generateJsonObject({prompt: "no content", schema})
249
+ ).rejects.toBeDefined();
250
+
251
+ const logs = await AIRequest.find({prompt: "no content"});
252
+ assert.lengthOf(logs, 1);
253
+ assert.equal(logs[0].requestType, "json_object");
254
+ assert.isOk(logs[0].error);
255
+ assert.notEqual(logs[0].metadata?.rawModelTextCaptured, true);
256
+ });
207
257
  });
208
258
 
209
259
  describe("generateJsonArray", () => {
@@ -238,6 +288,20 @@ describe("AIService", () => {
238
288
 
239
289
  expect(result).toEqual([1, 2]);
240
290
  });
291
+
292
+ it("logs the raw model text when the array payload cannot be parsed", async () => {
293
+ const model = createMockModel("sorry, I cannot do that");
294
+ const service = new AIService({model: model as unknown as LanguageModel});
295
+ const element = jsonSchema<number>({type: "number"});
296
+
297
+ await expect(service.generateJsonArray({element, prompt: "bad array"})).rejects.toBeDefined();
298
+
299
+ const logs = await AIRequest.find({prompt: "bad array"});
300
+ assert.lengthOf(logs, 1);
301
+ assert.equal(logs[0].requestType, "json_array");
302
+ assert.equal(logs[0].response, "sorry, I cannot do that");
303
+ assert.isTrue(logs[0].metadata?.rawModelTextCaptured);
304
+ });
241
305
  });
242
306
 
243
307
  describe("generateTextStream", () => {
@@ -1,6 +1,15 @@
1
1
  import {beforeEach, describe, expect, it} from "bun:test";
2
- import {clearMCPRegistry, Permissions, registerMCPModel, type User} from "@terreno/api";
2
+ import {
3
+ clearMCPRegistry,
4
+ type MCPToolResult,
5
+ Permissions,
6
+ registerMCPModel,
7
+ registerMCPTool,
8
+ type User,
9
+ } from "@terreno/api";
10
+ import {assert} from "chai";
3
11
  import mongoose, {Schema} from "mongoose";
12
+ import {z} from "zod";
4
13
 
5
14
  import {getMCPTools} from "./getMCPTools";
6
15
 
@@ -65,4 +74,55 @@ describe("getMCPTools", () => {
65
74
  })
66
75
  );
67
76
  });
77
+
78
+ it("exposes custom tools with their description and json schema", async () => {
79
+ registerMCPTool({
80
+ description: "Echoes structured json back to the caller",
81
+ handler: async (args): Promise<MCPToolResult> => ({
82
+ content: [{text: JSON.stringify({echoed: args.value}), type: "text"}],
83
+ }),
84
+ name: "echo_json",
85
+ zodSchema: z.object({value: z.string()}),
86
+ });
87
+
88
+ const tools = getMCPTools();
89
+ assert.deepEqual(Object.keys(tools), ["echo_json"]);
90
+ assert.equal(tools.echo_json.description, "Echoes structured json back to the caller");
91
+ assert.deepEqual(await tools.echo_json.execute?.({value: "hi"}), {echoed: "hi"});
92
+ });
93
+
94
+ it("returns joined plain text when the tool response is not json", async () => {
95
+ registerMCPTool({
96
+ description: "Returns prose",
97
+ handler: async (): Promise<MCPToolResult> => ({
98
+ content: [
99
+ {text: "first line", type: "text"},
100
+ {text: "second line", type: "text"},
101
+ ],
102
+ }),
103
+ name: "prose",
104
+ zodSchema: z.object({}),
105
+ });
106
+
107
+ const tools = getMCPTools();
108
+ assert.equal(await tools.prose.execute?.({}), "first line\nsecond line");
109
+ });
110
+
111
+ it("passes the authenticated user through to custom tool handlers", async () => {
112
+ const seenUserIds: (string | undefined)[] = [];
113
+ registerMCPTool({
114
+ description: "Records the calling user",
115
+ handler: async (_args, user): Promise<MCPToolResult> => {
116
+ seenUserIds.push(user?.id);
117
+ return {content: [{text: "ok", type: "text"}]};
118
+ },
119
+ name: "whoami",
120
+ zodSchema: z.object({}),
121
+ });
122
+
123
+ const user = asUser();
124
+ await getMCPTools(user).whoami.execute?.({});
125
+ await getMCPTools().whoami.execute?.({});
126
+ assert.deepEqual(seenUserIds, [user.id, undefined]);
127
+ });
68
128
  });
@@ -0,0 +1,22 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {
4
+ CONTENT_SUMMARY_PROMPT,
5
+ DEFAULT_GPT_MEMORY,
6
+ JSON_VALUE_SYSTEM_PROMPT,
7
+ REMIX_PROMPT,
8
+ TITLE_GENERATION_PROMPT,
9
+ TRANSLATION_PROMPT,
10
+ } from "./prompts";
11
+
12
+ describe("AI prompt constants", () => {
13
+ it("exports non-empty system and helper prompts", () => {
14
+ expect(DEFAULT_GPT_MEMORY.length).toBeGreaterThan(20);
15
+ expect(REMIX_PROMPT).toContain("Reword");
16
+ expect(CONTENT_SUMMARY_PROMPT).toContain("two-paragraph");
17
+ expect(TRANSLATION_PROMPT).toContain("{sourceLanguage}");
18
+ expect(TRANSLATION_PROMPT).toContain("{targetLanguage}");
19
+ expect(TITLE_GENERATION_PROMPT).toContain("3-6 words");
20
+ expect(JSON_VALUE_SYSTEM_PROMPT).toContain("JSON value");
21
+ });
22
+ });
@@ -0,0 +1,46 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import type {WebSearchProvider, WebSearchResult} from "./webSearchTool";
4
+
5
+ describe("webSearchTool types", () => {
6
+ it("defines WebSearchResult with snippet, title, and url", () => {
7
+ const result: WebSearchResult = {
8
+ snippet: "An example snippet describing the page.",
9
+ title: "Example Title",
10
+ url: "https://example.com",
11
+ };
12
+
13
+ expect(result.title).toBe("Example Title");
14
+ expect(result.url).toBe("https://example.com");
15
+ expect(result.snippet).toContain("snippet");
16
+ });
17
+
18
+ it("allows implementing WebSearchProvider and returning results", async () => {
19
+ const provider: WebSearchProvider = {
20
+ async search(query: string): Promise<WebSearchResult[]> {
21
+ return [
22
+ {
23
+ snippet: `Result for ${query}`,
24
+ title: `Title for ${query}`,
25
+ url: `https://example.com/search?q=${encodeURIComponent(query)}`,
26
+ },
27
+ ];
28
+ },
29
+ };
30
+
31
+ const results = await provider.search("terreno");
32
+ expect(results).toHaveLength(1);
33
+ expect(results[0]?.title).toBe("Title for terreno");
34
+ expect(results[0]?.url).toContain("q=terreno");
35
+ });
36
+
37
+ it("supports providers that return no results", async () => {
38
+ const emptyProvider: WebSearchProvider = {
39
+ async search(): Promise<WebSearchResult[]> {
40
+ return [];
41
+ },
42
+ };
43
+
44
+ await expect(emptyProvider.search("nothing")).resolves.toEqual([]);
45
+ });
46
+ });
@@ -1,5 +1,3 @@
1
- // noExplicitAny: test mock typing
2
- // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
3
1
  import {registerSimpleMongoPreload} from "@terreno/test";
4
2
 
5
3
  if (!process.env.TERRENO_TEST_MONGODB_URI?.trim()) {