@terreno/ai 0.3.2 → 0.4.2

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 (49) hide show
  1. package/README.md +5 -0
  2. package/dist/aiApp.d.ts +2 -0
  3. package/dist/aiApp.js +4 -1
  4. package/dist/models/aiRequest.js +68 -9
  5. package/dist/models/aiRequest.test.js +7 -5
  6. package/dist/models/gptHistory.js +6 -8
  7. package/dist/models/gptHistory.test.js +4 -22
  8. package/dist/models/index.d.ts +1 -0
  9. package/dist/models/index.js +3 -1
  10. package/dist/models/project.d.ts +2 -0
  11. package/dist/models/project.js +47 -0
  12. package/dist/routes/files.js +2 -2
  13. package/dist/routes/gpt.js +368 -128
  14. package/dist/routes/gptHistories.js +1 -1
  15. package/dist/routes/index.d.ts +1 -0
  16. package/dist/routes/index.js +3 -1
  17. package/dist/routes/projects.d.ts +3 -0
  18. package/dist/routes/projects.js +147 -0
  19. package/dist/service/aiService.js +21 -5
  20. package/dist/service/fileStorage.d.ts +3 -1
  21. package/dist/service/fileStorage.js +2 -2
  22. package/dist/service/index.d.ts +2 -1
  23. package/dist/service/index.js +2 -1
  24. package/dist/service/prompts.d.ts +1 -0
  25. package/dist/service/prompts.js +3 -1
  26. package/dist/service/webSearchTool.d.ts +39 -0
  27. package/dist/service/webSearchTool.js +2 -0
  28. package/dist/types/index.d.ts +39 -2
  29. package/dist/types/index.js +7 -2
  30. package/package.json +5 -5
  31. package/src/aiApp.ts +6 -0
  32. package/src/models/aiRequest.test.ts +8 -8
  33. package/src/models/aiRequest.ts +68 -10
  34. package/src/models/gptHistory.test.ts +4 -16
  35. package/src/models/gptHistory.ts +6 -9
  36. package/src/models/index.ts +1 -0
  37. package/src/models/project.ts +54 -0
  38. package/src/routes/files.ts +4 -4
  39. package/src/routes/gpt.ts +493 -295
  40. package/src/routes/gptHistories.ts +1 -1
  41. package/src/routes/index.ts +1 -0
  42. package/src/routes/mcp.ts +1 -1
  43. package/src/routes/projects.ts +115 -0
  44. package/src/service/aiService.ts +22 -6
  45. package/src/service/fileStorage.ts +3 -3
  46. package/src/service/index.ts +2 -0
  47. package/src/service/prompts.ts +4 -0
  48. package/src/service/webSearchTool.ts +40 -0
  49. package/src/types/index.ts +54 -2
@@ -23,7 +23,7 @@ export const addGptHistoryRoutes = (
23
23
  userId: (req.user as {_id: unknown})?._id,
24
24
  } as GptHistoryDocument;
25
25
  },
26
- queryFields: ["userId"],
26
+ queryFields: ["userId", "projectId"],
27
27
  queryFilter: (user) => ({userId: user?.id}),
28
28
  sort: "-updated",
29
29
  })
@@ -3,3 +3,4 @@ export {addFileRoutes} from "./files";
3
3
  export {addGptRoutes} from "./gpt";
4
4
  export {addGptHistoryRoutes} from "./gptHistories";
5
5
  export {addMcpRoutes} from "./mcp";
6
+ export {addProjectRoutes} from "./projects";
package/src/routes/mcp.ts CHANGED
@@ -68,7 +68,7 @@ export const addMcpRoutes = (router: any, options: McpRouteOptions): void => {
68
68
  throw new APIError({status: 403, title: "Admin access required"});
69
69
  }
70
70
 
71
- const {name} = req.params;
71
+ const name = req.params.name as string;
72
72
  const connected = await mcpService.reconnectServer(name);
73
73
  return res.json({data: {connected, name}});
74
74
  })
@@ -0,0 +1,115 @@
1
+ import {
2
+ APIError,
3
+ asyncHandler,
4
+ authenticateMiddleware,
5
+ createOpenApiBuilder,
6
+ modelRouter,
7
+ Permissions,
8
+ } from "@terreno/api";
9
+ import type express from "express";
10
+ import type mongoose from "mongoose";
11
+
12
+ import {Project} from "../models/project";
13
+
14
+ export const addProjectRoutes = (
15
+ router: any,
16
+ options?: {openApiOptions?: Record<string, unknown>}
17
+ ): void => {
18
+ router.use(
19
+ "/gpt/projects",
20
+ modelRouter(Project, {
21
+ ...options?.openApiOptions,
22
+ permissions: {
23
+ create: [Permissions.IsAuthenticated],
24
+ delete: [Permissions.IsOwner],
25
+ list: [Permissions.IsAuthenticated],
26
+ read: [Permissions.IsOwner],
27
+ update: [Permissions.IsOwner],
28
+ },
29
+ preCreate: (body: Record<string, unknown>, req: express.Request) => ({
30
+ ...body,
31
+ userId: (req as any).user?._id,
32
+ }),
33
+ queryFields: ["userId"],
34
+ queryFilter: (user: any) => ({userId: user?.id}),
35
+ sort: "-updated",
36
+ })
37
+ );
38
+
39
+ // Add memory to a project
40
+ router.post(
41
+ "/gpt/projects/:id/memories",
42
+ [
43
+ authenticateMiddleware(),
44
+ createOpenApiBuilder(options?.openApiOptions ?? {})
45
+ .withTags(["gpt"])
46
+ .withSummary("Add a memory to a project")
47
+ .withPathParameter("id", {type: "string"})
48
+ .withRequestBody({
49
+ category: {type: "string"},
50
+ text: {type: "string"},
51
+ })
52
+ .withResponse(200, {data: {type: "object"}})
53
+ .build(),
54
+ ],
55
+ asyncHandler(async (req: express.Request, res: express.Response) => {
56
+ const {id} = req.params;
57
+ const {text, category} = req.body;
58
+ const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
59
+
60
+ if (!text || typeof text !== "string") {
61
+ throw new APIError({status: 400, title: "text is required"});
62
+ }
63
+
64
+ const project = await Project.findById(id);
65
+ if (!project) {
66
+ throw new APIError({status: 404, title: "Project not found"});
67
+ }
68
+ if (project.userId.toString() !== userId?.toString()) {
69
+ throw new APIError({status: 403, title: "Not authorized"});
70
+ }
71
+
72
+ project.memories.push({category, source: "user", text});
73
+ await project.save();
74
+
75
+ return res.json({data: project.memories[project.memories.length - 1]});
76
+ })
77
+ );
78
+
79
+ // Remove memory from a project
80
+ router.delete(
81
+ "/gpt/projects/:id/memories/:memoryId",
82
+ [
83
+ authenticateMiddleware(),
84
+ createOpenApiBuilder(options?.openApiOptions ?? {})
85
+ .withTags(["gpt"])
86
+ .withSummary("Remove a memory from a project")
87
+ .withPathParameter("id", {type: "string"})
88
+ .withPathParameter("memoryId", {type: "string"})
89
+ .withResponse(200, {data: {type: "object"}})
90
+ .build(),
91
+ ],
92
+ asyncHandler(async (req: express.Request, res: express.Response) => {
93
+ const {id, memoryId} = req.params;
94
+ const userId = (req as any).user?._id as mongoose.Types.ObjectId | undefined;
95
+
96
+ const project = await Project.findById(id);
97
+ if (!project) {
98
+ throw new APIError({status: 404, title: "Project not found"});
99
+ }
100
+ if (project.userId.toString() !== userId?.toString()) {
101
+ throw new APIError({status: 403, title: "Not authorized"});
102
+ }
103
+
104
+ const memoryIndex = project.memories.findIndex((m) => m._id?.toString() === memoryId);
105
+ if (memoryIndex === -1) {
106
+ throw new APIError({status: 404, title: "Memory not found"});
107
+ }
108
+
109
+ project.memories.splice(memoryIndex, 1);
110
+ await project.save();
111
+
112
+ return res.json({data: {deleted: true}});
113
+ })
114
+ );
115
+ };
@@ -1,4 +1,5 @@
1
- import type {LanguageModel, ModelMessage} from "ai";
1
+ import {logger} from "@terreno/api";
2
+ import type {DataContent, LanguageModel, ModelMessage} from "ai";
2
3
  import {generateText as aiGenerateText, stepCountIs, streamText} from "ai";
3
4
  import type mongoose from "mongoose";
4
5
 
@@ -197,17 +198,32 @@ export class AIService {
197
198
  const parts: Array<{
198
199
  type: string;
199
200
  text?: string;
200
- image?: URL;
201
- data?: URL;
202
- mimeType?: string;
201
+ image?: DataContent | URL;
202
+ data?: DataContent | URL;
203
+ filename?: string;
204
+ mediaType?: string;
203
205
  }> = [];
204
206
  for (const part of prompt.content) {
205
207
  if (part.type === "text") {
206
208
  parts.push({text: part.text, type: "text"});
207
209
  } else if (part.type === "image") {
208
- parts.push({image: new URL(part.url), mimeType: part.mimeType, type: "image"});
210
+ logger.debug("Building image message part", {
211
+ mediaType: part.mimeType,
212
+ urlPrefix: part.url?.substring(0, 50),
213
+ });
214
+ parts.push({image: new URL(part.url), mediaType: part.mimeType, type: "image"});
209
215
  } else if (part.type === "file") {
210
- parts.push({data: new URL(part.url), mimeType: part.mimeType, type: "file"});
216
+ logger.debug("Building file message part", {
217
+ filename: (part as any).filename,
218
+ mediaType: part.mimeType,
219
+ urlPrefix: part.url?.substring(0, 50),
220
+ });
221
+ parts.push({
222
+ data: new URL(part.url),
223
+ filename: (part as any).filename,
224
+ mediaType: part.mimeType,
225
+ type: "file",
226
+ });
211
227
  }
212
228
  }
213
229
  messages.push({content: parts, role: "user"} as ModelMessage);
@@ -1,4 +1,4 @@
1
- import {Storage} from "@google-cloud/storage";
1
+ import {Storage, type StorageOptions} from "@google-cloud/storage";
2
2
 
3
3
  import {FileAttachment} from "../models/fileAttachment";
4
4
 
@@ -21,8 +21,8 @@ export class FileStorageService {
21
21
  private storage: Storage;
22
22
  private bucketName: string;
23
23
 
24
- constructor({bucketName}: {bucketName: string}) {
25
- this.storage = new Storage();
24
+ constructor({bucketName, storageOptions}: {bucketName: string; storageOptions?: StorageOptions}) {
25
+ this.storage = new Storage(storageOptions);
26
26
  this.bucketName = bucketName;
27
27
  }
28
28
 
@@ -5,5 +5,7 @@ export {
5
5
  CONTENT_SUMMARY_PROMPT,
6
6
  DEFAULT_GPT_MEMORY,
7
7
  REMIX_PROMPT,
8
+ TITLE_GENERATION_PROMPT,
8
9
  TRANSLATION_PROMPT,
9
10
  } from "./prompts";
11
+ export type {WebSearchProvider, WebSearchResult} from "./webSearchTool";
@@ -16,3 +16,7 @@ export const TRANSLATION_PROMPT =
16
16
  "Translate the following text from {sourceLanguage} to {targetLanguage}. " +
17
17
  "Maintain the original tone and meaning as closely as possible. " +
18
18
  "Return only the translated text with no additional commentary.";
19
+
20
+ export const TITLE_GENERATION_PROMPT =
21
+ "Generate a short title (3-6 words) that summarizes this conversation for a sidebar label. " +
22
+ "Return only the title text with no quotes, punctuation, or additional commentary.";
@@ -0,0 +1,40 @@
1
+ export interface WebSearchResult {
2
+ snippet: string;
3
+ title: string;
4
+ url: string;
5
+ }
6
+
7
+ /**
8
+ * Interface for pluggable web search providers.
9
+ *
10
+ * Implement this to integrate any search API (Tavily, Brave, Google Custom Search, etc.)
11
+ * with the AI chat tool system.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import {tool, zodSchema} from "ai";
16
+ * import {z} from "zod";
17
+ * import type {WebSearchProvider} from "@terreno/ai";
18
+ *
19
+ * const myProvider: WebSearchProvider = {
20
+ * search: async (query) => {
21
+ * const res = await fetch(`https://api.tavily.com/search`, { ... });
22
+ * return res.json().results;
23
+ * },
24
+ * };
25
+ *
26
+ * const webSearchTool = tool({
27
+ * description: "Search the web for current information.",
28
+ * execute: async ({query}) => {
29
+ * const results = await myProvider.search(query);
30
+ * return {query, results: results.slice(0, 5)};
31
+ * },
32
+ * inputSchema: zodSchema(z.object({
33
+ * query: z.string().describe("Search query"),
34
+ * })),
35
+ * });
36
+ * ```
37
+ */
38
+ export interface WebSearchProvider {
39
+ search(query: string): Promise<WebSearchResult[]>;
40
+ }
@@ -6,8 +6,14 @@ import type mongoose from "mongoose";
6
6
  // AIRequest Types
7
7
  // ============================================================
8
8
 
9
- export const AI_REQUEST_TYPES = ["general", "remix", "summarization", "translation"] as const;
10
- export type AIRequestType = (typeof AI_REQUEST_TYPES)[number];
9
+ export const DEFAULT_AI_REQUEST_TYPES = [
10
+ "general",
11
+ "remix",
12
+ "summarization",
13
+ "translation",
14
+ ] as const;
15
+ export type DefaultAIRequestType = (typeof DEFAULT_AI_REQUEST_TYPES)[number];
16
+ export type AIRequestType = DefaultAIRequestType | (string & {});
11
17
 
12
18
  export type AIRequestDocument = mongoose.Document<mongoose.Types.ObjectId> & {
13
19
  created: Date;
@@ -15,11 +21,15 @@ export type AIRequestDocument = mongoose.Document<mongoose.Types.ObjectId> & {
15
21
  error?: string;
16
22
  metadata?: Record<string, unknown>;
17
23
  aiModel: string;
24
+ parentRequestId?: mongoose.Types.ObjectId;
18
25
  prompt: string;
19
26
  requestType: AIRequestType;
20
27
  response?: string;
21
28
  responseTime?: number;
29
+ subRequestIds?: mongoose.Types.ObjectId[];
22
30
  tokensUsed?: number;
31
+ totalResponseTime?: number;
32
+ totalTokensUsed?: number;
23
33
  updated: Date;
24
34
  userId?: mongoose.Types.ObjectId;
25
35
  };
@@ -27,6 +37,7 @@ export type AIRequestDocument = mongoose.Document<mongoose.Types.ObjectId> & {
27
37
  export interface AIRequestStatics
28
38
  extends FindExactlyOnePlugin<AIRequestDocument>,
29
39
  FindOneOrNonePlugin<AIRequestDocument> {
40
+ logMultiAgentRequest(params: LogMultiAgentRequestParams): Promise<AIRequestDocument>;
30
41
  logRequest(params: LogRequestParams): Promise<AIRequestDocument>;
31
42
  }
32
43
 
@@ -44,6 +55,16 @@ export interface LogRequestParams {
44
55
  userId?: mongoose.Types.ObjectId;
45
56
  }
46
57
 
58
+ export interface LogMultiAgentRequestParams {
59
+ aiModel: string;
60
+ metadata?: Record<string, unknown>;
61
+ requestType: AIRequestType;
62
+ subRequestIds: mongoose.Types.ObjectId[];
63
+ totalResponseTime: number;
64
+ totalTokensUsed: number;
65
+ userId?: mongoose.Types.ObjectId;
66
+ }
67
+
47
68
  // ============================================================
48
69
  // Content Part Types (Multi-modal)
49
70
  // ============================================================
@@ -87,6 +108,7 @@ export interface GptHistoryPrompt {
87
108
  export type GptHistoryDocument = mongoose.Document<mongoose.Types.ObjectId> & {
88
109
  created: Date;
89
110
  deleted: boolean;
111
+ projectId?: mongoose.Types.ObjectId;
90
112
  prompts: GptHistoryPrompt[];
91
113
  title?: string;
92
114
  updated: Date;
@@ -99,6 +121,34 @@ export interface GptHistoryStatics
99
121
 
100
122
  export type GptHistoryModel = mongoose.Model<GptHistoryDocument> & GptHistoryStatics;
101
123
 
124
+ // ============================================================
125
+ // Project Types
126
+ // ============================================================
127
+
128
+ export interface ProjectMemory {
129
+ _id?: mongoose.Types.ObjectId;
130
+ category?: string;
131
+ created?: Date;
132
+ source: "user" | "auto";
133
+ text: string;
134
+ }
135
+
136
+ export type ProjectDocument = mongoose.Document<mongoose.Types.ObjectId> & {
137
+ created: Date;
138
+ deleted: boolean;
139
+ memories: ProjectMemory[];
140
+ name: string;
141
+ systemContext: string;
142
+ updated: Date;
143
+ userId: mongoose.Types.ObjectId;
144
+ };
145
+
146
+ export interface ProjectStatics
147
+ extends FindExactlyOnePlugin<ProjectDocument>,
148
+ FindOneOrNonePlugin<ProjectDocument> {}
149
+
150
+ export type ProjectModel = mongoose.Model<ProjectDocument> & ProjectStatics;
151
+
102
152
  // ============================================================
103
153
  // AI Service Types
104
154
  // ============================================================
@@ -171,6 +221,8 @@ export interface GptRouteOptions {
171
221
  tools?: Record<string, import("ai").Tool>;
172
222
  toolChoice?: "auto" | "none" | "required";
173
223
  maxSteps?: number;
224
+ /** Cheap model ID used for generating conversation titles (e.g. "gemini-2.0-flash-lite"). Falls back to the main model if not set. */
225
+ titleModelId?: string;
174
226
  }
175
227
 
176
228
  export interface GptHistoryRouteOptions {