@terreno/ai 57.10.1 → 57.12.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.
Files changed (66) hide show
  1. package/dist/aiApp.d.ts +5 -0
  2. package/dist/aiApp.js +4 -2
  3. package/dist/harness/commit.d.ts +17 -1
  4. package/dist/harness/commit.js +86 -2
  5. package/dist/harness/errors.d.ts +6 -0
  6. package/dist/harness/errors.js +6 -0
  7. package/dist/harness/harness.d.ts +2 -2
  8. package/dist/harness/harness.js +4 -2
  9. package/dist/harness/models/harnessTask.js +5 -0
  10. package/dist/harness/routes/approvals.js +2 -2
  11. package/dist/harness/routes/conversations.js +1 -1
  12. package/dist/harness/routes/tasks.js +85 -3
  13. package/dist/harness/runners/inProcessRunner.d.ts +11 -4
  14. package/dist/harness/runners/inProcessRunner.js +44 -13
  15. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  16. package/dist/harness/runners/jobsRunner.js +212 -0
  17. package/dist/harness/runtime.d.ts +20 -2
  18. package/dist/harness/runtime.js +91 -32
  19. package/dist/harness/subagent.d.ts +17 -2
  20. package/dist/harness/subagent.js +26 -2
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +3 -2
  23. package/dist/models/project.d.ts +13 -0
  24. package/dist/models/project.js +32 -2
  25. package/dist/observability/local/models/obsTrace.js +16 -2
  26. package/dist/observability/local/traceStore.d.ts +5 -0
  27. package/dist/observability/local/traceStore.js +5 -0
  28. package/dist/observability/routes/traces.js +2 -0
  29. package/dist/routes/projects.js +3 -3
  30. package/dist/service/chatTurn.js +1 -1
  31. package/dist/types/harness.d.ts +47 -3
  32. package/dist/types/observability.d.ts +3 -0
  33. package/package.json +65 -6
  34. package/src/aiApp.test.ts +15 -0
  35. package/src/aiApp.ts +9 -1
  36. package/src/harness/commit.ts +103 -1
  37. package/src/harness/concurrency.test.ts +155 -0
  38. package/src/harness/errors.ts +6 -0
  39. package/src/harness/events.test.ts +17 -5
  40. package/src/harness/harness.ts +14 -3
  41. package/src/harness/models/harnessTask.ts +5 -0
  42. package/src/harness/ownership.test.ts +9 -9
  43. package/src/harness/recovery.test.ts +2 -7
  44. package/src/harness/routes/approvals.ts +2 -2
  45. package/src/harness/routes/conversations.ts +1 -1
  46. package/src/harness/routes/tasks.test.ts +34 -0
  47. package/src/harness/routes/tasks.ts +98 -3
  48. package/src/harness/runners/inProcessRunner.test.ts +128 -0
  49. package/src/harness/runners/inProcessRunner.ts +54 -14
  50. package/src/harness/runners/jobsRunner.test.ts +709 -0
  51. package/src/harness/runners/jobsRunner.ts +252 -0
  52. package/src/harness/runtime.ts +125 -30
  53. package/src/harness/subagent.test.ts +124 -3
  54. package/src/harness/subagent.ts +43 -1
  55. package/src/index.ts +1 -1
  56. package/src/isolated/lazyProjectModel.isolated.ts +25 -0
  57. package/src/models/project.ts +31 -1
  58. package/src/observability/local/models/obsTrace.ts +22 -5
  59. package/src/observability/local/traceStore.test.ts +7 -0
  60. package/src/observability/local/traceStore.ts +12 -0
  61. package/src/observability/routes/traces.test.ts +15 -0
  62. package/src/observability/routes/traces.ts +2 -0
  63. package/src/routes/projects.ts +4 -4
  64. package/src/service/chatTurn.ts +2 -2
  65. package/src/types/harness.ts +56 -3
  66. package/src/types/observability.ts +3 -0
@@ -24,13 +24,18 @@ const obsTraceSchema = new mongoose_1.default.Schema({
24
24
  default: [],
25
25
  description: "Prompt versions used during this trace",
26
26
  type: [
27
- {
27
+ // No subdocument _id, so `$addToSet` dedupes repeated refs from replayed phases.
28
+ new mongoose_1.default.Schema({
28
29
  label: { description: "Label resolved at call time", type: String },
29
30
  name: { description: "Prompt name", required: true, type: String },
30
31
  version: { description: "Prompt version number", required: true, type: Number },
31
- },
32
+ }, { _id: false }),
32
33
  ],
33
34
  },
35
+ scope: {
36
+ description: "App-defined scope (tenant, workspace) for metering and filtering traces",
37
+ type: String,
38
+ },
34
39
  sensitive: {
35
40
  default: false,
36
41
  description: "When true, admin I/O disclosures start collapsed",
@@ -44,6 +49,11 @@ const obsTraceSchema = new mongoose_1.default.Schema({
44
49
  required: true,
45
50
  type: String,
46
51
  },
52
+ tags: {
53
+ default: [],
54
+ description: "App-defined labels for grouping traces",
55
+ type: [String],
56
+ },
47
57
  usage: {
48
58
  description: "Aggregated token and cost usage for the trace",
49
59
  type: mongoose_1.default.Schema.Types.Mixed,
@@ -80,6 +90,10 @@ const traceIndexSpecs = [
80
90
  ["flaggedForDataset", 1],
81
91
  ["created", -1],
82
92
  ],
93
+ [
94
+ ["scope", 1],
95
+ ["created", -1],
96
+ ],
83
97
  ];
84
98
  for (const spec of traceIndexSpecs) {
85
99
  obsTraceSchema.index(Object.fromEntries(spec));
@@ -7,6 +7,7 @@ export interface TraceListQuery {
7
7
  page?: number;
8
8
  prompt?: string;
9
9
  promptVersion?: number;
10
+ scope?: string;
10
11
  sensitive?: boolean;
11
12
  sessionId?: string;
12
13
  status?: "error" | "ok";
@@ -39,12 +40,14 @@ export interface TraceDetail {
39
40
  name: string;
40
41
  output?: unknown;
41
42
  prompts: TraceRecord["prompts"];
43
+ scope?: string;
42
44
  scores: ScoreRecord[];
43
45
  sensitive: boolean;
44
46
  sessionId?: string;
45
47
  spans: TraceSpanNode[];
46
48
  startedAt: string;
47
49
  status: "error" | "ok";
50
+ tags: string[];
48
51
  usage?: TraceRecord["usage"];
49
52
  userId?: string;
50
53
  }
@@ -55,12 +58,14 @@ export interface TraceListItem {
55
58
  id: string;
56
59
  name: string;
57
60
  prompts: TraceRecord["prompts"];
61
+ scope?: string;
58
62
  scoreCount: number;
59
63
  sensitive: boolean;
60
64
  sessionId?: string;
61
65
  spanCount: number;
62
66
  startedAt: string;
63
67
  status: "error" | "ok";
68
+ tags: string[];
64
69
  usage?: TraceRecord["usage"];
65
70
  userId?: string;
66
71
  }
@@ -140,6 +140,9 @@ class LocalTraceStore {
140
140
  if (query.sessionId) {
141
141
  filter.sessionId = query.sessionId;
142
142
  }
143
+ if (query.scope) {
144
+ filter.scope = query.scope;
145
+ }
143
146
  if (query.promptVersion !== undefined && !query.prompt) {
144
147
  throw new api_1.APIError({ status: 400, title: "prompt is required when promptVersion is set" });
145
148
  }
@@ -243,12 +246,14 @@ class LocalTraceStore {
243
246
  id: String(row._id),
244
247
  name: row.name,
245
248
  prompts: row.prompts,
249
+ scope: row.scope,
246
250
  scoreCount: counts.scoreCount,
247
251
  sensitive: row.sensitive,
248
252
  sessionId: row.sessionId,
249
253
  spanCount: counts.spanCount,
250
254
  startedAt: toIso(row.startedAt) ?? "",
251
255
  status: row.status,
256
+ tags: row.tags ?? [],
252
257
  usage: usageWithoutUndefinedCost(row.usage),
253
258
  userId: row.userId ? String(row.userId) : undefined,
254
259
  };
@@ -50,6 +50,7 @@ const addObservabilityTraceRoutes = (router, options) => {
50
50
  .withSummary("List observability traces")
51
51
  .withQueryParameter("prompt", { type: "string" }, { required: false })
52
52
  .withQueryParameter("promptVersion", { type: "number" }, { required: false })
53
+ .withQueryParameter("scope", { type: "string" }, { required: false })
53
54
  .withResponse(200, { data: { type: "array" } })
54
55
  .build()), (0, api_1.asyncHandler)(async (req, res) => {
55
56
  const prompt = typeof req.query.prompt === "string" ? req.query.prompt : undefined;
@@ -61,6 +62,7 @@ const addObservabilityTraceRoutes = (router, options) => {
61
62
  page: parsePositiveInt(req.query.page, 1),
62
63
  prompt,
63
64
  promptVersion: parsePromptVersion(req.query.promptVersion, prompt),
65
+ scope: typeof req.query.scope === "string" ? req.query.scope : undefined,
64
66
  sensitive: parseBoolean(req.query.sensitive),
65
67
  sessionId: typeof req.query.sessionId === "string" ? req.query.sessionId : undefined,
66
68
  status: req.query.status === "ok" || req.query.status === "error" ? req.query.status : undefined,
@@ -24,7 +24,7 @@ const addProjectRoutes = (router, options) => {
24
24
  if (!text || typeof text !== "string") {
25
25
  throw new api_1.APIError({ status: 400, title: "text is required" });
26
26
  }
27
- const project = await project_1.Project.findById(id);
27
+ const project = await (0, project_1.getProjectModel)().findById(id);
28
28
  if (!project) {
29
29
  throw new api_1.APIError({ status: 404, title: "Project not found" });
30
30
  }
@@ -48,7 +48,7 @@ const addProjectRoutes = (router, options) => {
48
48
  ], (0, api_1.asyncHandler)(async (req, res) => {
49
49
  const { id, memoryId } = req.params;
50
50
  const userId = req.user?._id;
51
- const project = await project_1.Project.findById(id);
51
+ const project = await (0, project_1.getProjectModel)().findById(id);
52
52
  if (!project) {
53
53
  throw new api_1.APIError({ status: 404, title: "Project not found" });
54
54
  }
@@ -63,7 +63,7 @@ const addProjectRoutes = (router, options) => {
63
63
  await project.save();
64
64
  return res.json({ data: { deleted: true } });
65
65
  }));
66
- router.use("/gpt/projects", (0, api_1.modelRouter)(project_1.Project, {
66
+ router.use("/gpt/projects", (0, api_1.modelRouter)((0, project_1.getProjectModel)(), {
67
67
  ...options?.openApiOptions,
68
68
  permissions: {
69
69
  create: [api_1.Permissions.IsAuthenticated],
@@ -670,7 +670,7 @@ const buildSystemPrompt = async ({ history, options, projectId, systemPrompt, us
670
670
  let effectiveSystemPrompt = systemPrompt;
671
671
  if (effectiveProjectId) {
672
672
  try {
673
- const project = await project_1.Project.findById(effectiveProjectId);
673
+ const project = await (0, project_1.getProjectModel)().findById(effectiveProjectId);
674
674
  if (project && project.userId.toString() === userId?.toString()) {
675
675
  const parts = [];
676
676
  if (project.systemContext) {
@@ -3,6 +3,23 @@ import type { LanguageModel, ModelMessage } from "ai";
3
3
  import type { Duration, DurationLike } from "luxon";
4
4
  import type mongoose from "mongoose";
5
5
  import type { ExecutionEnv } from "../harness/executionEnv";
6
+ import type { ObsPromptRef } from "./observability";
7
+ /**
8
+ * A prompt version a task used, recorded on its `ObsTrace.prompts`. A `PromptVersionRef`
9
+ * from `promptRegistry.get` fits as-is; its `body` is ignored.
10
+ */
11
+ export type HarnessPromptRef = ObsPromptRef;
12
+ /** A registry prompt passed as instructions: its `body` is used and its version recorded. */
13
+ export interface HarnessPromptInstructions extends HarnessPromptRef {
14
+ body: string;
15
+ }
16
+ /** Fields written onto the task's `ObsTrace`, shared by every child task and subagent. */
17
+ export interface HarnessTraceOptions {
18
+ /** App-defined scope (tenant, workspace) for metering; indexed with `created`. */
19
+ scope?: string;
20
+ /** App-defined labels for grouping traces. */
21
+ tags?: string[];
22
+ }
6
23
  /** Every lifecycle status a harness task can hold. */
7
24
  export declare const HARNESS_TASK_STATUSES: {
8
25
  readonly aborted: "aborted";
@@ -119,6 +136,8 @@ export interface HarnessTaskDocument extends mongoose.Document<mongoose.Types.Ob
119
136
  ancestorIds?: mongoose.Types.ObjectId[];
120
137
  attempt: number;
121
138
  background: boolean;
139
+ /** Times a runner claimed the task; numbers each runnable visit for job dispatch. */
140
+ claims?: number;
122
141
  created: Date;
123
142
  deleted: boolean;
124
143
  input?: unknown;
@@ -317,8 +336,13 @@ export interface HarnessMemo {
317
336
  export interface HarnessRunAgentOptions<Result> {
318
337
  /** The subagent's user message. A non-string value is sent as JSON. */
319
338
  input: unknown;
320
- /** Replaces the agent's instructions for this subagent conversation only. */
321
- instructions?: string;
339
+ /**
340
+ * Replaces the agent's instructions for this subagent conversation only. A registry
341
+ * prompt (`promptRegistry.get`) is used by its `body` and recorded on the trace.
342
+ */
343
+ instructions?: HarnessPromptInstructions | string;
344
+ /** Prompt versions this subagent used, recorded on the task's `ObsTrace.prompts`. */
345
+ prompts?: HarnessPromptRef[];
322
346
  /**
323
347
  * Structured output schema; defaults to the agent's own `output`. Requested through the
324
348
  * AI SDK's `Output.object`, then validated with this schema before it is returned.
@@ -381,8 +405,12 @@ export interface HarnessAbortOptions {
381
405
  userId?: mongoose.Types.ObjectId | string;
382
406
  }
383
407
  export interface HarnessCreateTaskOptions {
408
+ /** Prompt versions the task uses, recorded on its `ObsTrace.prompts`. */
409
+ prompts?: HarnessPromptRef[];
384
410
  /** Idempotency key: a second create with the same id returns the existing task. */
385
411
  requestId?: string;
412
+ /** Scope and tags for the task's `ObsTrace`; child tasks and subagents share it. */
413
+ trace?: HarnessTraceOptions;
386
414
  userId?: mongoose.Types.ObjectId | string;
387
415
  }
388
416
  /** How an interrupted task is resolved by an operator. */
@@ -425,6 +453,18 @@ export interface HarnessLeaseSettings {
425
453
  /** Runner instance id written into every lease it holds. */
426
454
  owner: string;
427
455
  }
456
+ /** A task a runner may claim now, as listed for dispatch. */
457
+ export interface HarnessRunnableTask {
458
+ attempt: number;
459
+ /** Claims so far; changes every time the task becomes runnable again after a claim. */
460
+ claims: number;
461
+ phase: string;
462
+ taskId: string;
463
+ }
464
+ export interface HarnessRunTaskOptions {
465
+ /** Stop after this many phases and hand a still-running task back as `pending`. */
466
+ maxPhases?: number;
467
+ }
428
468
  /**
429
469
  * What the harness hands a runner: hold the owner lease, recover tasks whose lease
430
470
  * expired, claim one runnable task, then run it to a stop.
@@ -433,6 +473,10 @@ export interface HarnessRunnerContext {
433
473
  /** Take or renew the singleton `HarnessOwner` lease; false when another owner holds it. */
434
474
  acquireOwnerLease: (lease: HarnessLeaseSettings) => Promise<boolean>;
435
475
  claimNext: (lease: HarnessLeaseSettings) => Promise<HarnessTaskDocument | null>;
476
+ /** Claim one task by id when it is runnable now; null when it is not (or another runner won). */
477
+ claimTask: (taskId: string, lease: HarnessLeaseSettings) => Promise<HarnessTaskDocument | null>;
478
+ /** Oldest runnable tasks first, at most `limit`, without claiming them. */
479
+ listRunnable: (limit: number) => Promise<HarnessRunnableTask[]>;
436
480
  /**
437
481
  * Resume or park every `running` task whose lease expired. Returns how many were made
438
482
  * runnable again.
@@ -440,7 +484,7 @@ export interface HarnessRunnerContext {
440
484
  recoverExpired: () => Promise<number>;
441
485
  /** Give up the owner lease so a standby can take over without waiting for expiry. */
442
486
  releaseOwnerLease: (lease: HarnessLeaseSettings) => Promise<void>;
443
- runTask: (task: HarnessTaskDocument, lease: HarnessLeaseSettings) => Promise<void>;
487
+ runTask: (task: HarnessTaskDocument, lease: HarnessLeaseSettings, options?: HarnessRunTaskOptions) => Promise<void>;
444
488
  }
445
489
  /** Decides who executes runnable tasks and when. */
446
490
  export interface HarnessRunner {
@@ -72,10 +72,13 @@ export interface ObsTraceDocument extends mongoose.Document<mongoose.Types.Objec
72
72
  name: string;
73
73
  output?: unknown;
74
74
  prompts: ObsPromptRef[];
75
+ /** App-defined scope (tenant, workspace) set by `harness.createTask({trace: {scope}})`. */
76
+ scope?: string;
75
77
  sensitive: boolean;
76
78
  sessionId?: string;
77
79
  startedAt: Date;
78
80
  status: "error" | "ok";
81
+ tags: string[];
79
82
  updated: Date;
80
83
  usage?: ObsPromptUsage;
81
84
  userId?: mongoose.Types.ObjectId;
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "@langfuse/otel": "^5.0.1",
11
11
  "@opentelemetry/api": "^1.9.0",
12
12
  "@opentelemetry/sdk-node": "0.217.0",
13
- "@terreno/blocks": "57.10.1",
13
+ "@terreno/blocks": "57.12.0",
14
14
  "ai": "^6.0.97",
15
15
  "ajv": "8.20.0",
16
16
  "csv-parse": "^6.1.0",
@@ -22,9 +22,10 @@
22
22
  "description": "Provider-agnostic AI service with streaming chat, request logging, and admin tools — part of Terreno, the batteries-included TypeScript framework for universal apps.",
23
23
  "devDependencies": {
24
24
  "@biomejs/biome": "^2.3.6",
25
- "@terreno/admin-backend": "57.10.1",
26
- "@terreno/api": "57.10.1",
27
- "@terreno/test": "57.10.1",
25
+ "@terreno/admin-backend": "57.12.0",
26
+ "@terreno/api": "57.12.0",
27
+ "@terreno/jobs": "57.12.0",
28
+ "@terreno/test": "57.12.0",
28
29
  "@types/bun": "^1.4.0",
29
30
  "@types/express": "^5.0.6",
30
31
  "@types/luxon": "^3.7.1",
@@ -44,12 +45,48 @@
44
45
  "require": "./dist/index.js",
45
46
  "types": "./dist/index.d.ts"
46
47
  },
48
+ "./admin": {
49
+ "default": "./dist/aiAdminApp.js",
50
+ "import": "./dist/aiAdminApp.js",
51
+ "require": "./dist/aiAdminApp.js",
52
+ "types": "./dist/aiAdminApp.d.ts"
53
+ },
47
54
  "./harness": {
48
55
  "default": "./dist/harness/harness.js",
49
56
  "import": "./dist/harness/harness.js",
50
57
  "require": "./dist/harness/harness.js",
51
58
  "types": "./dist/harness/harness.d.ts"
52
59
  },
60
+ "./harness/agentLoop": {
61
+ "default": "./dist/harness/agentLoop.js",
62
+ "import": "./dist/harness/agentLoop.js",
63
+ "require": "./dist/harness/agentLoop.js",
64
+ "types": "./dist/harness/agentLoop.d.ts"
65
+ },
66
+ "./harness/commit": {
67
+ "default": "./dist/harness/commit.js",
68
+ "import": "./dist/harness/commit.js",
69
+ "require": "./dist/harness/commit.js",
70
+ "types": "./dist/harness/commit.d.ts"
71
+ },
72
+ "./harness/events": {
73
+ "default": "./dist/harness/events.js",
74
+ "import": "./dist/harness/events.js",
75
+ "require": "./dist/harness/events.js",
76
+ "types": "./dist/harness/events.d.ts"
77
+ },
78
+ "./harness/internalRuntime": {
79
+ "default": "./dist/harness/internalRuntime.js",
80
+ "import": "./dist/harness/internalRuntime.js",
81
+ "require": "./dist/harness/internalRuntime.js",
82
+ "types": "./dist/harness/internalRuntime.d.ts"
83
+ },
84
+ "./harness/jobsRunner": {
85
+ "default": "./dist/harness/runners/jobsRunner.js",
86
+ "import": "./dist/harness/runners/jobsRunner.js",
87
+ "require": "./dist/harness/runners/jobsRunner.js",
88
+ "types": "./dist/harness/runners/jobsRunner.d.ts"
89
+ },
53
90
  "./langfuseApp": {
54
91
  "default": "./dist/langfuseApp.js",
55
92
  "import": "./dist/langfuseApp.js",
@@ -62,6 +99,24 @@
62
99
  "require": "./dist/langfuseClient.js",
63
100
  "types": "./dist/langfuseClient.d.ts"
64
101
  },
102
+ "./observability/localPlugin": {
103
+ "default": "./dist/observability/local/localPlugin.js",
104
+ "import": "./dist/observability/local/localPlugin.js",
105
+ "require": "./dist/observability/local/localPlugin.js",
106
+ "types": "./dist/observability/local/localPlugin.d.ts"
107
+ },
108
+ "./observability/observabilityApp": {
109
+ "default": "./dist/observability/observabilityApp.js",
110
+ "import": "./dist/observability/observabilityApp.js",
111
+ "require": "./dist/observability/observabilityApp.js",
112
+ "types": "./dist/observability/observabilityApp.d.ts"
113
+ },
114
+ "./observability/promptStore": {
115
+ "default": "./dist/observability/local/promptStore.js",
116
+ "import": "./dist/observability/local/promptStore.js",
117
+ "require": "./dist/observability/local/promptStore.js",
118
+ "types": "./dist/observability/local/promptStore.d.ts"
119
+ },
65
120
  "./parseAiJson": {
66
121
  "default": "./dist/service/parseAiJson.js",
67
122
  "import": "./dist/service/parseAiJson.js",
@@ -81,12 +136,16 @@
81
136
  "name": "@terreno/ai",
82
137
  "peerDependencies": {
83
138
  "@ai-sdk/google-vertex": "^4.0.142",
84
- "@terreno/api": "57.10.1",
139
+ "@terreno/api": "57.12.0",
140
+ "@terreno/jobs": "57.12.0",
85
141
  "mongoose": "^8.0.0 || ^9.0.0"
86
142
  },
87
143
  "peerDependenciesMeta": {
88
144
  "@ai-sdk/google-vertex": {
89
145
  "optional": true
146
+ },
147
+ "@terreno/jobs": {
148
+ "optional": true
90
149
  }
91
150
  },
92
151
  "publishConfig": {
@@ -109,5 +168,5 @@
109
168
  "test:coverage": "bun run ../scripts/check-coverage.ts"
110
169
  },
111
170
  "types": "dist/index.d.ts",
112
- "version": "57.10.1"
171
+ "version": "57.12.0"
113
172
  }
package/src/aiApp.test.ts CHANGED
@@ -79,6 +79,21 @@ describe("AiApp", () => {
79
79
  expect(projects.status).toBe(200);
80
80
  });
81
81
 
82
+ it("skips the project routes with projects: false", async () => {
83
+ const {AIService} = await import("./service/aiService");
84
+ const aiService = new AIService({model: createMockModel() as unknown as LanguageModel});
85
+ const plugin = new AiApp({aiService, projects: false});
86
+ const app = new TerrenoApp({
87
+ configureApp: (router) => plugin.register(router as unknown as express.Application),
88
+ skipListen: true,
89
+ userModel: UserModel,
90
+ }).build();
91
+
92
+ const agent = await authAsUser(app);
93
+ expect((await agent.get("/gpt/histories")).status).toBe(200);
94
+ expect((await agent.get("/gpt/projects")).status).toBe(404);
95
+ });
96
+
82
97
  it("passes asks to the gpt routes, so the model is offered the ask tools", async () => {
83
98
  const {AIService} = await import("./service/aiService");
84
99
  const model = createMockModel();
package/src/aiApp.ts CHANGED
@@ -42,6 +42,11 @@ export interface AiAppOptions {
42
42
  mcpService?: MCPService;
43
43
  /** OpenAPI options passed through to route builders for spec generation. */
44
44
  openApiOptions?: Record<string, unknown>;
45
+ /**
46
+ * Mount the `/gpt/projects` routes, which register the `Project` model. Defaults to `true`;
47
+ * pass `false` when the app owns a model named `Project`.
48
+ */
49
+ projects?: boolean;
45
50
  /** Tool choice strategy for chat requests. Defaults to "auto" when tools are present. */
46
51
  toolChoice?: "auto" | "none" | "required";
47
52
  /** Cheap model ID used for generating conversation titles (e.g. "gemini-3.5-flash-lite"). Falls back to the main model if not set. */
@@ -97,6 +102,7 @@ export class AiApp implements TerrenoPlugin {
97
102
  maxSteps,
98
103
  mcpService,
99
104
  openApiOptions,
105
+ projects,
100
106
  titleModelId,
101
107
  toolChoice,
102
108
  tools,
@@ -122,7 +128,9 @@ export class AiApp implements TerrenoPlugin {
122
128
  addGptHistoryRoutes(router, {chat, openApiOptions});
123
129
  addGptRoutes(router, chat);
124
130
  addAiRequestsExplorerRoutes(router, {openApiOptions});
125
- addProjectRoutes(router, {openApiOptions});
131
+ if (projects !== false) {
132
+ addProjectRoutes(router, {openApiOptions});
133
+ }
126
134
 
127
135
  if (fileStorageService && gcsBucket) {
128
136
  addFileRoutes(router, {
@@ -17,6 +17,7 @@ import type {
17
17
  HarnessMessageModel,
18
18
  HarnessOwnerModel,
19
19
  HarnessOwnership,
20
+ HarnessPromptRef,
20
21
  HarnessReplayPolicy,
21
22
  HarnessResolveInterruptedOptions,
22
23
  HarnessTaskDefinition,
@@ -150,6 +151,94 @@ const findByRequestId = async ({
150
151
  return existing;
151
152
  };
152
153
 
154
+ /**
155
+ * Prompt refs as stored on `ObsTrace.prompts` (`{label?, name, version}`; a registry
156
+ * `body` is dropped), once each. Throws `invalidRequest` for a ref without a name, with a
157
+ * non-string label, or without a positive integer version.
158
+ */
159
+ export const tracePromptRefs = (refs: HarnessPromptRef[] | undefined): HarnessPromptRef[] => {
160
+ if (refs === undefined) {
161
+ return [];
162
+ }
163
+ if (!Array.isArray(refs)) {
164
+ throw harnessError({detail: "prompts must be an array", kind: "invalidRequest"});
165
+ }
166
+ const unique = new Map<string, HarnessPromptRef>();
167
+ for (const ref of refs) {
168
+ if (typeof ref?.name !== "string" || !ref.name.trim()) {
169
+ throw harnessError({detail: "Each prompt ref needs a name", kind: "invalidRequest"});
170
+ }
171
+ if (!Number.isInteger(ref.version) || ref.version < 1) {
172
+ throw harnessError({
173
+ detail: `Prompt ref "${ref.name}" needs a positive integer version`,
174
+ kind: "invalidRequest",
175
+ });
176
+ }
177
+ if (ref.label !== undefined && typeof ref.label !== "string") {
178
+ throw harnessError({
179
+ detail: `Prompt ref "${ref.name}" label must be a string`,
180
+ kind: "invalidRequest",
181
+ });
182
+ }
183
+ const normalized = {
184
+ ...(ref.label !== undefined ? {label: ref.label} : {}),
185
+ name: ref.name,
186
+ version: ref.version,
187
+ };
188
+ unique.set(JSON.stringify(normalized), normalized);
189
+ }
190
+ return [...unique.values()];
191
+ };
192
+
193
+ /** The `scope` and `tags` written onto a root task's `ObsTrace`. */
194
+ const traceFields = (
195
+ trace: HarnessCreateTaskOptions["trace"]
196
+ ): {scope?: string; tags?: string[]} => {
197
+ if (trace === undefined) {
198
+ return {};
199
+ }
200
+ const {scope, tags} = trace;
201
+ if (scope !== undefined && (typeof scope !== "string" || !scope.trim())) {
202
+ throw harnessError({detail: "trace.scope must be a non-empty string", kind: "invalidRequest"});
203
+ }
204
+ const isBlankTag = (tag: unknown): boolean => typeof tag !== "string" || !tag.trim();
205
+ if (tags !== undefined && (!Array.isArray(tags) || tags.some(isBlankTag))) {
206
+ throw harnessError({
207
+ detail: "trace.tags must be an array of non-empty strings",
208
+ kind: "invalidRequest",
209
+ });
210
+ }
211
+ return {
212
+ ...(scope !== undefined ? {scope} : {}),
213
+ ...(tags !== undefined ? {tags: [...new Set(tags)]} : {}),
214
+ };
215
+ };
216
+
217
+ /**
218
+ * Record `refs` on `traceId`'s `ObsTrace.prompts`, once each. Pass `session` to write
219
+ * inside a commit's transaction.
220
+ */
221
+ export const addTracePrompts = async ({
222
+ models,
223
+ refs,
224
+ session,
225
+ traceId,
226
+ }: {
227
+ models: HarnessModels;
228
+ refs: HarnessPromptRef[];
229
+ session?: ClientSession;
230
+ traceId: mongoose.Types.ObjectId;
231
+ }): Promise<void> => {
232
+ if (refs.length === 0) {
233
+ return;
234
+ }
235
+ await models.trace.updateOne(
236
+ {_id: traceId},
237
+ {$addToSet: {prompts: {$each: refs}}},
238
+ session ? {session} : {}
239
+ );
240
+ };
241
+
153
242
  /**
154
243
  * Insert a root task with its `ObsTrace` and root `CHAIN` span in one transaction.
155
244
  * A repeated `requestId` hits the unique index, rolls the whole transaction back (so no
@@ -185,11 +274,24 @@ export const createTaskRecords = async ({
185
274
  const startedAt = traceStartedAt.toJSDate();
186
275
  const userId = toObjectId(options.userId);
187
276
  const spanIdentity = taskSpanIdentity(definition, input);
277
+ const prompts = tracePromptRefs(options.prompts);
278
+ const traceExtras = traceFields(options.trace);
188
279
 
189
280
  try {
190
281
  return await inTransaction(async (session) => {
191
282
  await models.trace.create(
192
- [{_id: traceId, input, name: definition.key, startedAt, status: "ok", userId}],
283
+ [
284
+ {
285
+ _id: traceId,
286
+ input,
287
+ name: definition.key,
288
+ prompts,
289
+ startedAt,
290
+ status: "ok",
291
+ userId,
292
+ ...traceExtras,
293
+ },
294
+ ],
193
295
  {session}
194
296
  );
195
297
  await models.span.create(