@xfey/tutti 0.1.106 → 0.1.108

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 (69) hide show
  1. package/README.md +2 -0
  2. package/dist/collaboration-ingestion/external-source.js +10 -1
  3. package/dist/control-plane/procedure-logging.d.ts +1 -0
  4. package/dist/control-plane/procedure-logging.js +16 -5
  5. package/dist/control-plane/reference-summary-refresh.js +2 -2
  6. package/dist/control-plane/task-source-context-types.d.ts +2 -1
  7. package/dist/control-plane/task-source-context.js +7 -0
  8. package/dist/control-plane/workflows/openai.js +1 -0
  9. package/dist/providers/openai/app-server/read-only-procedure.d.ts +10 -2
  10. package/dist/providers/openai/app-server/read-only-procedure.js +68 -15
  11. package/dist/providers/openai/app-server/skills.d.ts +4 -1
  12. package/dist/providers/openai/app-server/skills.js +12 -2
  13. package/dist/providers/openai/app-server/workspace-write-run.js +14 -10
  14. package/dist/reference-access/cache.d.ts +50 -0
  15. package/dist/reference-access/cache.js +268 -0
  16. package/dist/reference-access/read-evidence.d.ts +6 -0
  17. package/dist/reference-access/read-evidence.js +15 -0
  18. package/dist/reference-access/service.d.ts +26 -0
  19. package/dist/reference-access/service.js +42 -0
  20. package/dist/server-shell/cli/external-reference-runtime.d.ts +12 -0
  21. package/dist/server-shell/cli/external-reference-runtime.js +75 -0
  22. package/dist/server-shell/cli/host-server-runtime.js +17 -1
  23. package/dist/server-shell/cli/relay-registration.js +1 -0
  24. package/dist/server-shell/http/create-server.d.ts +2 -0
  25. package/dist/server-shell/http/create-server.js +5 -4
  26. package/dist/server-shell/http/routes/agent-context-auth.d.ts +2 -1
  27. package/dist/server-shell/http/routes/agent-context-auth.js +3 -1
  28. package/dist/server-shell/http/routes/agent-context-external-reference-routes.d.ts +4 -0
  29. package/dist/server-shell/http/routes/agent-context-external-reference-routes.js +83 -0
  30. package/dist/server-shell/http/routes/agent-context-shared.d.ts +2 -0
  31. package/dist/server-shell/http/routes/agent-context.js +3 -1
  32. package/dist/server-shell/http/routes/project-api/reference-files-routes.js +19 -2
  33. package/dist/workspace-ops/project-workspace.js +6 -6
  34. package/dist/workspace-ops/reference-cache-policy.d.ts +6 -0
  35. package/dist/workspace-ops/reference-cache-policy.js +79 -0
  36. package/migrations/0021_external_reference_reads.sql +11 -0
  37. package/node_modules/@tutti/relay-client/dist/external-references.d.ts +11 -0
  38. package/node_modules/@tutti/relay-client/dist/external-references.js +63 -0
  39. package/node_modules/@tutti/relay-client/dist/host-control.d.ts +1 -0
  40. package/node_modules/@tutti/relay-client/dist/host-control.js +1 -0
  41. package/node_modules/@tutti/relay-client/dist/index.d.ts +1 -0
  42. package/node_modules/@tutti/relay-client/dist/index.js +1 -0
  43. package/node_modules/@tutti/shared/dist/schemas/api/clarifications.d.ts +6 -0
  44. package/node_modules/@tutti/shared/dist/schemas/api/external-references.d.ts +23 -0
  45. package/node_modules/@tutti/shared/dist/schemas/api/external-references.js +21 -0
  46. package/node_modules/@tutti/shared/dist/schemas/api/feishu-documents.d.ts +46 -0
  47. package/node_modules/@tutti/shared/dist/schemas/api/feishu-documents.js +53 -0
  48. package/node_modules/@tutti/shared/dist/schemas/api/index.d.ts +4 -2
  49. package/node_modules/@tutti/shared/dist/schemas/api/index.js +3 -1
  50. package/node_modules/@tutti/shared/dist/schemas/api/messages.d.ts +24 -0
  51. package/node_modules/@tutti/shared/dist/schemas/api/messages.js +2 -0
  52. package/node_modules/@tutti/shared/dist/schemas/api/platform-sessions.d.ts +10 -1
  53. package/node_modules/@tutti/shared/dist/schemas/api/platform-sessions.js +23 -1
  54. package/node_modules/@tutti/shared/dist/schemas/api/viewer-reference.d.ts +6 -0
  55. package/node_modules/@tutti/shared/dist/schemas/internal/external-references.d.ts +109 -0
  56. package/node_modules/@tutti/shared/dist/schemas/internal/external-references.js +70 -0
  57. package/node_modules/@tutti/shared/dist/schemas/internal/feishu-documents.d.ts +49 -0
  58. package/node_modules/@tutti/shared/dist/schemas/internal/feishu-documents.js +36 -0
  59. package/node_modules/@tutti/shared/dist/schemas/internal/index.d.ts +2 -0
  60. package/node_modules/@tutti/shared/dist/schemas/internal/index.js +2 -0
  61. package/node_modules/@tutti/shared/dist/schemas/internal/platform-integration.d.ts +36 -0
  62. package/node_modules/@tutti/shared/dist/schemas/internal/platform-integration.js +3 -0
  63. package/package.json +1 -1
  64. package/prompts/skills/read-references/SKILL.md +32 -3
  65. package/web/assets/{homepage-motion-scene-tfFEdV9Y.js → homepage-motion-scene-B6yPJnY8.js} +1 -1
  66. package/web/assets/{index-C2Mmj25S.css → index-DvfZ57Nl.css} +1 -1
  67. package/web/assets/index-aQnJ5JoI.js +69 -0
  68. package/web/index.html +2 -2
  69. package/web/assets/index-B7ZGIvOv.js +0 -69
@@ -0,0 +1,268 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync, } from "node:fs";
3
+ import { dirname, join, resolve } from "node:path";
4
+ import { assertReferenceCacheIsolation, initializeReferenceCacheDirectory, REFERENCE_CACHE_PATH, } from "../workspace-ops/reference-cache-policy.js";
5
+ const MAX_BYTES = 10 * 1024 * 1024;
6
+ const SAFE_REF = /^[A-Za-z0-9_-]{1,128}$/u;
7
+ const DIGEST = /^[a-f0-9]{64}$/u;
8
+ const MAX_PIN_MS = 24 * 60 * 60 * 1000;
9
+ export class ReferenceCacheError extends Error {
10
+ code;
11
+ constructor(code) {
12
+ super(code);
13
+ this.code = code;
14
+ this.name = "ReferenceCacheError";
15
+ }
16
+ }
17
+ function fail() {
18
+ throw new ReferenceCacheError("reference_cache_invalid");
19
+ }
20
+ function hash(bytes) {
21
+ return createHash("sha256").update(bytes).digest("hex");
22
+ }
23
+ function regular(path) {
24
+ const stat = lstatSync(path, { throwIfNoEntry: false });
25
+ if (stat === undefined)
26
+ return false;
27
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1)
28
+ fail();
29
+ return true;
30
+ }
31
+ function ensureDirectories(path) {
32
+ const absolute = resolve(path);
33
+ const parent = dirname(absolute);
34
+ if (parent !== absolute)
35
+ ensureDirectories(parent);
36
+ const stat = lstatSync(absolute, { throwIfNoEntry: false });
37
+ if (stat !== undefined) {
38
+ if (!stat.isDirectory() || stat.isSymbolicLink())
39
+ fail();
40
+ }
41
+ else
42
+ mkdirSync(absolute, { mode: 0o700 });
43
+ }
44
+ function relativeCopyPath(source, digest) {
45
+ return `${REFERENCE_CACHE_PATH}/${source}/${digest}.bin`;
46
+ }
47
+ export class ExternalReferenceCache {
48
+ options;
49
+ workspace;
50
+ ledgerFile;
51
+ ledger;
52
+ tail = Promise.resolve();
53
+ constructor(options) {
54
+ this.options = options;
55
+ this.workspace = realpathSync(options.workspaceRoot);
56
+ if ((options.maxTotalBytes !== undefined &&
57
+ (!Number.isSafeInteger(options.maxTotalBytes) || options.maxTotalBytes < 1)) ||
58
+ (options.retentionMs !== undefined &&
59
+ (!Number.isSafeInteger(options.retentionMs) || options.retentionMs < 0)))
60
+ fail();
61
+ const ledgerRoot = resolve(options.ledgerRoot);
62
+ if (ledgerRoot === this.workspace || ledgerRoot.startsWith(`${this.workspace}/`))
63
+ fail();
64
+ ensureDirectories(ledgerRoot);
65
+ this.ledgerFile = join(ledgerRoot, "cache.json");
66
+ if (regular(this.ledgerFile)) {
67
+ try {
68
+ const parsed = JSON.parse(readFileSync(this.ledgerFile, "utf8"));
69
+ this.ledger = this.validateLedger(parsed);
70
+ }
71
+ catch {
72
+ fail();
73
+ }
74
+ }
75
+ else {
76
+ this.ledger = { version: 1, workspace: this.workspace, entries: {}, pins: {} };
77
+ }
78
+ initializeReferenceCacheDirectory(this.workspace);
79
+ this.save();
80
+ }
81
+ now() {
82
+ return (this.options.now ?? Date.now)();
83
+ }
84
+ validateLedger(value) {
85
+ if (value === null || typeof value !== "object")
86
+ fail();
87
+ const candidate = value;
88
+ if (candidate.version !== 1 ||
89
+ candidate.workspace !== this.workspace ||
90
+ !candidate.entries ||
91
+ !candidate.pins ||
92
+ typeof candidate.entries !== "object" ||
93
+ typeof candidate.pins !== "object" ||
94
+ Array.isArray(candidate.entries) ||
95
+ Array.isArray(candidate.pins))
96
+ fail();
97
+ for (const [key, entry] of Object.entries(candidate.entries)) {
98
+ if (!entry ||
99
+ !SAFE_REF.test(entry.source_ref) ||
100
+ !DIGEST.test(entry.sha256) ||
101
+ key !== `${entry.source_ref}:${entry.sha256}` ||
102
+ entry.path !== relativeCopyPath(entry.source_ref, entry.sha256) ||
103
+ typeof entry.revision !== "string" ||
104
+ entry.revision.length > 256 ||
105
+ typeof entry.media_type !== "string" ||
106
+ entry.media_type.length > 128 ||
107
+ !Number.isSafeInteger(entry.size_bytes) ||
108
+ entry.size_bytes < 0 ||
109
+ entry.size_bytes > MAX_BYTES ||
110
+ !Number.isFinite(entry.last_used_at) ||
111
+ !Number.isFinite(Date.parse(entry.checked_at)))
112
+ fail();
113
+ }
114
+ for (const [key, pin] of Object.entries(candidate.pins)) {
115
+ if (!/^[a-f0-9]{64}$/u.test(key) ||
116
+ !pin ||
117
+ !Number.isFinite(pin.expires_at) ||
118
+ !Object.hasOwn(candidate.entries, pin.key) ||
119
+ !pin.copy ||
120
+ pin.copy.path !== candidate.entries[pin.key]?.path ||
121
+ pin.copy.sha256 !== candidate.entries[pin.key]?.sha256 ||
122
+ pin.copy.source_ref !== candidate.entries[pin.key]?.source_ref ||
123
+ pin.copy.size_bytes !== candidate.entries[pin.key]?.size_bytes ||
124
+ typeof pin.copy.media_type !== "string" ||
125
+ pin.copy.media_type.length > 128 ||
126
+ typeof pin.copy.revision !== "string" ||
127
+ !pin.copy.revision ||
128
+ pin.copy.revision.length > 256 ||
129
+ !Number.isFinite(Date.parse(pin.copy.checked_at)))
130
+ fail();
131
+ }
132
+ return candidate;
133
+ }
134
+ save() {
135
+ regular(this.ledgerFile);
136
+ const temporary = `${this.ledgerFile}.${randomUUID()}.tmp`;
137
+ writeFileSync(temporary, JSON.stringify(this.ledger), { flag: "wx", mode: 0o600 });
138
+ renameSync(temporary, this.ledgerFile);
139
+ }
140
+ bytes(entry) {
141
+ assertReferenceCacheIsolation(this.workspace, true);
142
+ const directory = join(this.workspace, REFERENCE_CACHE_PATH, entry.source_ref);
143
+ const stat = lstatSync(directory, { throwIfNoEntry: false });
144
+ if (!stat?.isDirectory() || stat.isSymbolicLink())
145
+ fail();
146
+ const path = join(this.workspace, entry.path);
147
+ if (!regular(path))
148
+ throw new ReferenceCacheError("reference_cache_missing");
149
+ if (lstatSync(path).size !== entry.size_bytes)
150
+ fail();
151
+ const content = readFileSync(path);
152
+ if (hash(content) !== entry.sha256)
153
+ fail();
154
+ return content;
155
+ }
156
+ /** A pin belongs to a server-issued invocation, never to model-supplied identity. */
157
+ async acquire(input) {
158
+ if (!SAFE_REF.test(input.sourceRef) ||
159
+ !SAFE_REF.test(input.invocationRef) ||
160
+ !Number.isFinite(input.expiresAt) ||
161
+ input.expiresAt <= this.now() ||
162
+ input.expiresAt > this.now() + MAX_PIN_MS)
163
+ fail();
164
+ const operation = this.tail.then(async () => {
165
+ if (input.expiresAt <= this.now())
166
+ throw new ReferenceCacheError("reference_invocation_expired");
167
+ assertReferenceCacheIsolation(this.workspace, true);
168
+ const pinKey = hash(Buffer.from(`${input.invocationRef}\0${input.sourceRef}`));
169
+ const pin = this.ledger.pins[pinKey];
170
+ if (pin !== undefined && pin.expires_at > this.now()) {
171
+ const entry = this.ledger.entries[pin.key];
172
+ if (entry === undefined)
173
+ fail();
174
+ const content = this.bytes(entry);
175
+ entry.last_used_at = this.now();
176
+ this.save();
177
+ return { copy: pin.copy, cache_hit: true, content };
178
+ }
179
+ // A new invocation checks access and content. Failures never silently use stale bytes.
180
+ const fetched = await input.fetch();
181
+ assertReferenceCacheIsolation(this.workspace, true);
182
+ if (input.expiresAt <= this.now())
183
+ throw new ReferenceCacheError("reference_invocation_expired");
184
+ if (fetched.content.byteLength > MAX_BYTES ||
185
+ !fetched.revision ||
186
+ fetched.revision.length > 256 ||
187
+ !fetched.media_type ||
188
+ fetched.media_type.length > 128)
189
+ fail();
190
+ const content = Buffer.from(fetched.content);
191
+ const digest = hash(content);
192
+ const key = `${input.sourceRef}:${digest}`;
193
+ const existing = this.ledger.entries[key];
194
+ this.prune(content.byteLength, key);
195
+ const entry = {
196
+ source_ref: input.sourceRef,
197
+ revision: fetched.revision,
198
+ sha256: digest,
199
+ path: relativeCopyPath(input.sourceRef, digest),
200
+ size_bytes: content.byteLength,
201
+ media_type: fetched.media_type,
202
+ checked_at: new Date(this.now()).toISOString(),
203
+ last_used_at: this.now(),
204
+ };
205
+ ensureDirectories(join(this.workspace, REFERENCE_CACHE_PATH, input.sourceRef));
206
+ const path = join(this.workspace, entry.path);
207
+ if (existing !== undefined && regular(path))
208
+ this.bytes(existing);
209
+ else if (regular(path)) {
210
+ // Response-loss/crash orphan: adopt only an exact content-addressed regular file.
211
+ if (!readFileSync(path).equals(content))
212
+ fail();
213
+ }
214
+ else {
215
+ const temporary = `${path}.${randomUUID()}.tmp`;
216
+ writeFileSync(temporary, content, { flag: "wx", mode: 0o600 });
217
+ renameSync(temporary, path);
218
+ }
219
+ // Each version records its own observation. Pins capture revision independently below.
220
+ this.ledger.entries[key] = entry;
221
+ this.ledger.pins[pinKey] = { key, expires_at: input.expiresAt, copy: this.projection(entry) };
222
+ this.save();
223
+ return { copy: this.projection(entry), cache_hit: existing !== undefined, content };
224
+ });
225
+ this.tail = operation.catch(() => undefined);
226
+ return operation;
227
+ }
228
+ projection(entry) {
229
+ return {
230
+ source_ref: entry.source_ref,
231
+ revision: entry.revision,
232
+ sha256: entry.sha256,
233
+ path: entry.path,
234
+ size_bytes: entry.size_bytes,
235
+ media_type: entry.media_type,
236
+ checked_at: entry.checked_at,
237
+ };
238
+ }
239
+ prune(incomingBytes, incomingKey) {
240
+ const now = this.now();
241
+ for (const [key, pin] of Object.entries(this.ledger.pins)) {
242
+ if (pin.expires_at <= now)
243
+ delete this.ledger.pins[key];
244
+ }
245
+ const protectedKeys = new Set(Object.values(this.ledger.pins).map((pin) => pin.key));
246
+ const entries = Object.entries(this.ledger.entries).sort((a, b) => a[1].last_used_at - b[1].last_used_at);
247
+ let total = entries.reduce((sum, [, entry]) => sum + entry.size_bytes, 0);
248
+ const additional = Object.hasOwn(this.ledger.entries, incomingKey) ? 0 : incomingBytes;
249
+ const maximum = this.options.maxTotalBytes ?? 100 * 1024 * 1024;
250
+ const retention = this.options.retentionMs ?? 7 * 24 * 60 * 60 * 1000;
251
+ for (const [key, entry] of entries) {
252
+ if (key === incomingKey || protectedKeys.has(key))
253
+ continue;
254
+ if (total + additional <= maximum && entry.last_used_at + retention > now)
255
+ continue;
256
+ if (regular(join(this.workspace, entry.path))) {
257
+ this.bytes(entry); // Reject replacement/symlink/tampering before removal.
258
+ unlinkSync(join(this.workspace, entry.path));
259
+ }
260
+ delete this.ledger.entries[key];
261
+ total -= entry.size_bytes;
262
+ }
263
+ this.save();
264
+ if (total + additional > maximum)
265
+ throw new ReferenceCacheError("reference_cache_capacity");
266
+ }
267
+ }
268
+ //# sourceMappingURL=cache.js.map
@@ -0,0 +1,6 @@
1
+ import type { SqliteDatabase } from "../store/index.js";
2
+ import type { ReferenceCopy } from "./cache.js";
3
+ import type { ReferenceInvocation } from "./service.js";
4
+ /** Durable observation, separate from evictable cache metadata; no content or locator. */
5
+ export declare function recordExternalReferenceRead(db: SqliteDatabase, invocation: ReferenceInvocation, copy: ReferenceCopy): void;
6
+ //# sourceMappingURL=read-evidence.d.ts.map
@@ -0,0 +1,15 @@
1
+ /** Durable observation, separate from evictable cache metadata; no content or locator. */
2
+ export function recordExternalReferenceRead(db, invocation, copy) {
3
+ const invocationRef = invocation.reference_invocation_ref;
4
+ const existing = db
5
+ .prepare(`
6
+ SELECT revision, content_sha256 FROM external_reference_reads WHERE invocation_ref = ? AND source_ref = ?`)
7
+ .get(invocationRef, copy.source_ref);
8
+ if (existing &&
9
+ (existing.revision !== copy.revision || existing.content_sha256 !== copy.sha256)) {
10
+ throw new Error("reference_read_evidence_conflict");
11
+ }
12
+ db.prepare(`INSERT OR IGNORE INTO external_reference_reads
13
+ (invocation_ref, activity_ref, source_ref, revision, content_sha256, size_bytes, checked_at) VALUES (?, ?, ?, ?, ?, ?, ?)`).run(invocationRef, invocation.activity_ref ?? null, copy.source_ref, copy.revision, copy.sha256, copy.size_bytes, copy.checked_at);
14
+ }
15
+ //# sourceMappingURL=read-evidence.js.map
@@ -0,0 +1,26 @@
1
+ import type { ExternalReferenceDescriptor } from "@tutti/shared/schemas/api";
2
+ import type { ReferenceCopy, ReferenceFetchResult } from "./cache.js";
3
+ import { type ExternalReferenceCache } from "./cache.js";
4
+ export type ReferenceInvocation = {
5
+ reference_invocation_ref: string;
6
+ activity_ref?: string;
7
+ expires_at: string;
8
+ };
9
+ export declare class ExternalReferenceAccess {
10
+ private readonly options;
11
+ private cache;
12
+ private readonly selections;
13
+ constructor(options: {
14
+ createCache: () => ExternalReferenceCache;
15
+ list: (invocation: ReferenceInvocation) => Promise<ExternalReferenceDescriptor[]>;
16
+ read: (sourceRef: string, invocation: ReferenceInvocation) => Promise<ReferenceFetchResult>;
17
+ recordRead: (invocation: ReferenceInvocation, copy: ReferenceCopy) => void;
18
+ });
19
+ list(invocation: ReferenceInvocation): Promise<ExternalReferenceDescriptor[]>;
20
+ read(invocation: ReferenceInvocation, sourceRef: string): Promise<{
21
+ copy: ReferenceCopy;
22
+ cache_hit: boolean;
23
+ content: Buffer;
24
+ }>;
25
+ }
26
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { ReferenceCacheError } from "./cache.js";
2
+ export class ExternalReferenceAccess {
3
+ options;
4
+ cache;
5
+ selections = new Map();
6
+ constructor(options) {
7
+ this.options = options;
8
+ }
9
+ async list(invocation) {
10
+ const now = Date.now();
11
+ for (const [key, selection] of this.selections)
12
+ if (selection.expires <= now)
13
+ this.selections.delete(key);
14
+ let selection = this.selections.get(invocation.reference_invocation_ref);
15
+ if (!selection) {
16
+ if (this.selections.size >= 128)
17
+ throw new ReferenceCacheError("reference_reader_busy");
18
+ selection = {
19
+ expires: Date.parse(invocation.expires_at),
20
+ sources: this.options.list(invocation),
21
+ };
22
+ this.selections.set(invocation.reference_invocation_ref, selection);
23
+ selection.sources.catch(() => this.selections.delete(invocation.reference_invocation_ref));
24
+ }
25
+ return selection.sources;
26
+ }
27
+ async read(invocation, sourceRef) {
28
+ const sources = await this.list(invocation);
29
+ if (!sources.some((source) => source.source_ref === sourceRef))
30
+ throw new ReferenceCacheError("reference_source_not_selected");
31
+ this.cache ??= this.options.createCache();
32
+ const result = await this.cache.acquire({
33
+ sourceRef,
34
+ invocationRef: invocation.reference_invocation_ref,
35
+ expiresAt: Date.parse(invocation.expires_at),
36
+ fetch: () => this.options.read(sourceRef, invocation),
37
+ });
38
+ this.options.recordRead(invocation, result.copy);
39
+ return result;
40
+ }
41
+ }
42
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1,12 @@
1
+ import { ExternalReferenceAccess } from "../../reference-access/service.js";
2
+ import type { HostProjectStore } from "../../store/index.js";
3
+ import { type StagedUploadResolver } from "../http/routes/project-api/staged-uploads.js";
4
+ import type { HostLocalLaunchStatus } from "../http/routes/local-control.js";
5
+ import type { LaunchPreparationResult } from "./launch.js";
6
+ export declare function createExternalReferenceRuntime(options: {
7
+ preparation: LaunchPreparationResult;
8
+ store: HostProjectStore;
9
+ resolveStagedUpload: StagedUploadResolver;
10
+ getLaunchStatus: () => HostLocalLaunchStatus;
11
+ }): ExternalReferenceAccess;
12
+ //# sourceMappingURL=external-reference-runtime.d.ts.map
@@ -0,0 +1,75 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { join } from "node:path";
3
+ import { requestExternalReferences } from "@tutti/relay-client";
4
+ import { isExternalReferenceList, isExternalReferenceReadResult, } from "@tutti/shared/schemas/internal";
5
+ import { ExternalReferenceCache } from "../../reference-access/cache.js";
6
+ import { ExternalReferenceAccess, } from "../../reference-access/service.js";
7
+ import { recordExternalReferenceRead } from "../../reference-access/read-evidence.js";
8
+ import { downloadStagedUploadWithResolver, cleanupDownloadedStagedUpload, } from "../http/routes/project-api/staged-uploads.js";
9
+ import { readHostRegistrationSecret } from "./machine-local.js";
10
+ export function createExternalReferenceRuntime(options) {
11
+ const { preparation } = options;
12
+ async function request(action, invocation, sourceRef) {
13
+ const relay = options.getLaunchStatus().relay;
14
+ if (!relay)
15
+ throw new Error("reference_host_offline");
16
+ const secret = readHostRegistrationSecret(preparation.tutti_home, preparation.project_id);
17
+ return requestExternalReferences({
18
+ relayUrl: preparation.relay_url,
19
+ body: {
20
+ project_identity: { source: "git_config", project_id: preparation.project_id },
21
+ host_auth: { kind: "host_registration_secret_v1", secret: secret.secret },
22
+ relay_project_ref: relay.relay_project_ref,
23
+ host_connection_ref: relay.host_connection_ref,
24
+ request_ref: `eref_${randomUUID().replaceAll("-", "")}`,
25
+ invocation_ref: invocation.reference_invocation_ref,
26
+ action,
27
+ ...(sourceRef === undefined ? {} : { source_ref: sourceRef }),
28
+ },
29
+ });
30
+ }
31
+ return new ExternalReferenceAccess({
32
+ createCache: () => new ExternalReferenceCache({
33
+ workspaceRoot: preparation.workspace_root,
34
+ ledgerRoot: join(preparation.tutti_home, "projects", preparation.project_id, "reference-cache"),
35
+ }),
36
+ list: async (invocation) => {
37
+ const result = await request("list", invocation);
38
+ if (!isExternalReferenceList(result))
39
+ throw new Error("reference_list_invalid");
40
+ return result.items;
41
+ },
42
+ read: async (sourceRef, invocation) => {
43
+ const result = await request("read", invocation, sourceRef);
44
+ if (!isExternalReferenceReadResult(result) || result.source.source_ref !== sourceRef)
45
+ throw new Error("reference_read_invalid");
46
+ if (result.size_bytes === 0)
47
+ return {
48
+ content: Buffer.alloc(0),
49
+ revision: result.revision,
50
+ media_type: result.media_type,
51
+ };
52
+ if (!result.upload_id)
53
+ throw new Error("reference_read_invalid");
54
+ const downloaded = await downloadStagedUploadWithResolver({
55
+ resolveStagedUpload: options.resolveStagedUpload,
56
+ purpose: "reference_file",
57
+ expected: {
58
+ upload_id: result.upload_id,
59
+ file_name: `${sourceRef}.bin`,
60
+ size_bytes: result.size_bytes,
61
+ sha256: `sha256:${result.sha256}`,
62
+ media_type: result.media_type,
63
+ },
64
+ });
65
+ await cleanupDownloadedStagedUpload(downloaded);
66
+ return {
67
+ content: downloaded.content,
68
+ revision: result.revision,
69
+ media_type: result.media_type,
70
+ };
71
+ },
72
+ recordRead: (invocation, copy) => recordExternalReferenceRead(options.store.db, invocation, copy),
73
+ });
74
+ }
75
+ //# sourceMappingURL=external-reference-runtime.js.map
@@ -27,6 +27,7 @@ import { createTrustedRelaySessionMetadataStore, } from "../session/relay-sessio
27
27
  import { LaunchError } from "./errors.js";
28
28
  import { HostProjectRuntimeLifecycle } from "./host-project-runtime-lifecycle.js";
29
29
  import { readCliVersion } from "./version.js";
30
+ import { createExternalReferenceRuntime } from "./external-reference-runtime.js";
30
31
  import { relayStateFromLaunchStatus } from "./host-relay-status.js";
31
32
  import { appendHostLogLine, readHostRegistrationSecret, createMachineRuntimeEndpointToken, deleteMachineRuntimeEndpoint, ensureHostLogFile, getHostLogFilePath, updateMachineProjectDisplayNameSnapshot, writeMachineRuntimeEndpoint, } from "./machine-local.js";
32
33
  import { readProjectDescription, readProjectDisplayName, writeProjectDescription, writeProjectDisplayName, } from "./project-identity.js";
@@ -128,6 +129,7 @@ function createAgentContextRuntime(options) {
128
129
  baseUrl: new URL(AGENT_CONTEXT_BASE_PATH, options.baseUrl).toString(),
129
130
  issueToken: (input) => {
130
131
  const token = options.registry.issueToken({
132
+ ...(input.activityRef === undefined ? {} : { activityRef: input.activityRef }),
131
133
  projectId: options.projectId,
132
134
  selection: input.selectionName,
133
135
  scopes: input.scopes,
@@ -533,7 +535,21 @@ export async function startForegroundHostServer(options) {
533
535
  : { getLaunchStatus: options.getLaunchStatus }),
534
536
  });
535
537
  app = options.createServer({
536
- agentContext: { tokenRegistry: agentContextTokenRegistry },
538
+ agentContext: {
539
+ tokenRegistry: agentContextTokenRegistry,
540
+ ...(options.preparation.machine_local.binding.surface_profile.kind === "feishu" &&
541
+ options.getLaunchStatus !== undefined &&
542
+ resolveStagedUpload !== undefined
543
+ ? {
544
+ externalReferences: createExternalReferenceRuntime({
545
+ preparation: options.preparation,
546
+ store,
547
+ getLaunchStatus: options.getLaunchStatus,
548
+ resolveStagedUpload,
549
+ }),
550
+ }
551
+ : {}),
552
+ },
537
553
  serviceVersion: HOST_SERVER_VERSION,
538
554
  health: {
539
555
  readiness: {
@@ -37,6 +37,7 @@ export async function registerHostWithRelay(options) {
37
37
  joinToken: { mode: options.joinTokenMode ?? "rotate" },
38
38
  capabilities: {
39
39
  integration_source_ingestion_v1: true,
40
+ integration_external_references_v1: true,
40
41
  integration_attachment_ingestion_v1: true,
41
42
  integration_managed_reference_ingestion_v1: true,
42
43
  integration_outbound_v1: true,
@@ -3,9 +3,11 @@ import { type HostLocalControlOptions } from "./routes/local-control.js";
3
3
  import { type HostProjectApiRouteOptions } from "./routes/project-api.js";
4
4
  import { AgentContextTokenRegistry } from "./routes/agent-context.js";
5
5
  import { type HostWebStaticOptions } from "./static-web.js";
6
+ import type { ExternalReferenceAccess } from "../../reference-access/service.js";
6
7
  export type HostServerOptions = {
7
8
  agentContext?: false | {
8
9
  tokenRegistry?: AgentContextTokenRegistry;
10
+ externalReferences?: ExternalReferenceAccess;
9
11
  };
10
12
  health?: {
11
13
  readiness?: {
@@ -3,7 +3,7 @@ import { createRedactedLoggerOptions } from "./logger.js";
3
3
  import { registerHostHealthRoute } from "./routes/health.js";
4
4
  import { registerHostLocalControlRoutes, } from "./routes/local-control.js";
5
5
  import { registerHostProjectApiRoutes, } from "./routes/project-api.js";
6
- import { AgentContextTokenRegistry, registerAgentContextRoutes, } from "./routes/agent-context.js";
6
+ import { AgentContextTokenRegistry, registerAgentContextRoutes } from "./routes/agent-context.js";
7
7
  import { registerHostWebStaticRoutes } from "./static-web.js";
8
8
  import { registerApiErrorHandler } from "./validation.js";
9
9
  export function createHostServer(options = {}) {
@@ -12,9 +12,7 @@ export function createHostServer(options = {}) {
12
12
  : fastify({ logger: createRedactedLoggerOptions("tutti-host", options.logger) });
13
13
  registerApiErrorHandler(app);
14
14
  registerHostHealthRoute(app, {
15
- ...(options.health?.readiness === undefined
16
- ? {}
17
- : { readiness: options.health.readiness }),
15
+ ...(options.health?.readiness === undefined ? {} : { readiness: options.health.readiness }),
18
16
  now: options.now ?? (() => new Date()),
19
17
  serviceVersion: options.serviceVersion ?? "0.0.0",
20
18
  });
@@ -31,6 +29,9 @@ export function createHostServer(options = {}) {
31
29
  }
32
30
  if (options.agentContext !== false) {
33
31
  registerAgentContextRoutes(app, {
32
+ ...(options.agentContext?.externalReferences === undefined
33
+ ? {}
34
+ : { externalReferences: options.agentContext.externalReferences }),
34
35
  project: projectApiOptions.project,
35
36
  ...(projectApiOptions.store === undefined ? {} : { store: projectApiOptions.store }),
36
37
  ...(projectApiOptions.workspaceOps === undefined
@@ -3,6 +3,7 @@ import type { ActivityRef, ProjectId } from "@tutti/shared/ids";
3
3
  import type { CodexAppServerContextApiScope, CodexAppServerSkillSelectionName } from "../../../providers/openai/app-server/skills.js";
4
4
  export declare const DEFAULT_AGENT_CONTEXT_TOKEN_TTL_MS: number;
5
5
  export type AgentContextTokenRecord = {
6
+ reference_invocation_ref: string;
6
7
  token: string;
7
8
  project_id: ProjectId;
8
9
  activity_ref?: ActivityRef;
@@ -37,5 +38,5 @@ export declare function authorizeAgentContextRequest(request: FastifyRequest, op
37
38
  project_id: ProjectId;
38
39
  };
39
40
  tokenRegistry: AgentContextTokenRegistry;
40
- }, requiredScope: CodexAppServerContextApiScope): void;
41
+ }, requiredScope: CodexAppServerContextApiScope): AgentContextTokenRecord;
41
42
  //# sourceMappingURL=agent-context-auth.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import { randomBytes } from "node:crypto";
2
+ import { createEventRef } from "@tutti/shared/ids";
2
3
  export const DEFAULT_AGENT_CONTEXT_TOKEN_TTL_MS = 30 * 60 * 1000;
3
4
  export class AgentContextTokenRegistry {
4
5
  options;
@@ -11,6 +12,7 @@ export class AgentContextTokenRegistry {
11
12
  const ttlMs = input.ttlMs ?? this.options.ttlMs ?? DEFAULT_AGENT_CONTEXT_TOKEN_TTL_MS;
12
13
  const token = this.options.tokenFactory?.() ?? `agctx_${randomBytes(32).toString("base64url")}`;
13
14
  const record = {
15
+ reference_invocation_ref: createEventRef(),
14
16
  token,
15
17
  project_id: input.projectId,
16
18
  ...(input.activityRef === undefined ? {} : { activity_ref: input.activityRef }),
@@ -64,7 +66,7 @@ function bearerToken(request) {
64
66
  return match[1].trim();
65
67
  }
66
68
  export function authorizeAgentContextRequest(request, options, requiredScope) {
67
- options.tokenRegistry.validate({
69
+ return options.tokenRegistry.validate({
68
70
  token: bearerToken(request),
69
71
  projectId: options.project.project_id,
70
72
  requiredScope,
@@ -0,0 +1,4 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type { AgentContextRouteOptions } from "./agent-context-shared.js";
3
+ export declare function registerAgentContextExternalReferenceRoutes(app: FastifyInstance, options: AgentContextRouteOptions): void;
4
+ //# sourceMappingURL=agent-context-external-reference-routes.d.ts.map