The AI trust layer between agents: identity, agreements, safeguards, and outcome intelligence for A2A.

Viewing files at commit 39e235ac59f4Commit detailsView latest

apps/dashboard/src/preview.ts 434 lines · 13.0 KB · typescript
1
export const previewKey = 'openclasp.preview.v1';
2
3
export const previewSession = {
4
  user: {
5
    sub: 'local-preview',
6
    name: 'Local preview',
7
    email: 'preview@localhost',
8
  },
9
};
10
11
export type DashboardData = {
12
  agents: Record<string, any>[];
13
  projects: Record<string, any>[];
14
  installations: Record<string, any>[];
15
  setupRequests: Record<string, any>[];
16
  publications: Record<string, any>[];
17
  interactions: Record<string, any>[];
18
  federatedInteractions: Record<string, any>[];
19
  liveSessions: Record<string, any>[];
20
  hostedThreads: Record<string, any>[];
21
  events: Record<string, any>[];
22
  conflicts: Record<string, any>[];
23
  receipts: Record<string, any>[];
24
  profiles: Record<string, any>[];
25
  runtimes: Record<string, any>[];
26
  accessTokens: Record<string, any>[];
27
};
28
29
export type Settings = {
30
  displayName: string;
31
  contributionEnabled: boolean;
32
  retentionDays: number;
33
  evidenceSharing: 'never' | 'ask' | 'contract_only';
34
  rawConversationsStored: false;
35
};
36
37
export const defaultPreviewSettings: Settings = {
38
  displayName: 'Local operator',
39
  contributionEnabled: false,
40
  retentionDays: 30,
41
  evidenceSharing: 'ask',
42
  rawConversationsStored: false,
43
};
44
45
const ago = (minutes: number) => new Date(Date.now() - minutes * 60_000).toISOString();
46
const ahead = (minutes: number) => new Date(Date.now() + minutes * 60_000).toISOString();
47
48
export function createPreviewData(): DashboardData {
49
  return {
50
    projects: [
51
      { projectId: 'proj_ops', name: 'Operations', createdAt: ago(21 * 24 * 60) },
52
      { projectId: 'proj_research', name: 'Research', createdAt: ago(12 * 24 * 60) },
53
    ],
54
    agents: [
55
      {
56
        agentId: 'agent_atlas',
57
        projectId: 'proj_research',
58
        name: 'Atlas Research',
59
        description: 'Literature review and planning',
60
        framework: 'Claude',
61
        agentVersion: '1.4.2',
62
        agentMode: 'persistent_runtime',
63
        a2aEndpoint: 'https://atlas.example.com/a2a',
64
        autoPublish: true,
65
        autoAcceptPolicy: 'safe_matching',
66
        autoAcceptTaskCategories: ['research', 'planning'],
67
        capabilities: ['research', 'planning', 'summarization'],
68
        limitations: ['no payments'],
69
        identityMode: 'oauth_installation',
70
        status: 'active',
71
        createdAt: ago(12 * 24 * 60),
72
        updatedAt: ago(8),
73
        presence: { status: 'online', lastSeenAt: ago(1) },
74
      },
75
      {
76
        agentId: 'agent_harbor',
77
        projectId: 'proj_ops',
78
        name: 'Harbor Ops',
79
        description: 'Incident triage and runbooks',
80
        framework: 'GPT',
81
        agentVersion: '2.1.0',
82
        agentMode: 'temporary_chat',
83
        autoPublish: false,
84
        autoAcceptPolicy: 'off',
85
        autoAcceptTaskCategories: [],
86
        capabilities: ['ops', 'triage'],
87
        limitations: ['no production writes'],
88
        identityMode: 'oauth_installation',
89
        status: 'active',
90
        createdAt: ago(21 * 24 * 60),
91
        updatedAt: ago(180),
92
        presence: { status: 'offline', lastSeenAt: ago(180) },
93
      },
94
    ],
95
    installations: [
96
      {
97
        installationId: 'inst_atlas',
98
        clientId: 'mcp_atlas',
99
        agentId: 'agent_atlas',
100
        projectId: 'proj_research',
101
        connectedAt: ago(12 * 24 * 60),
102
        updatedAt: ago(8),
103
      },
104
    ],
105
    setupRequests: [
106
      {
107
        requestId: 'setup_lumen',
108
        clientId: 'mcp_lumen',
109
        action: 'connect',
110
        status: 'pending',
111
        agentName: 'Lumen Writer',
112
        projectName: 'Content',
113
        framework: 'Claude',
114
        agentVersion: '0.9.0',
115
        autoPublish: true,
116
        autoAcceptPolicy: 'safe_matching',
117
        autoAcceptTaskCategories: ['writing', 'editing'],
118
        capabilities: ['writing', 'editing', 'research'],
119
        limitations: ['no outbound email'],
120
        requestedAt: ago(18),
121
      },
122
    ],
123
    publications: [{ agentId: 'agent_atlas', published: true, updatedAt: ago(40) }],
124
    interactions: [
125
      {
126
        interactionId: 'ix_local_brief',
127
        agentId: 'agent_atlas',
128
        status: 'completed',
129
        createdAt: ago(240),
130
        completedAt: ago(220),
131
      },
132
    ],
133
    federatedInteractions: [
134
      {
135
        interactionId: 'ix_shared_review',
136
        initiatorAgentId: 'agent_peer_nova',
137
        responderAgentId: 'agent_atlas',
138
        status: 'pending',
139
        termsHash: '7f3c91a0b2e84d11c6aa09f1d3e8b470',
140
        createdAt: ago(26),
141
        contract: { purpose: 'Review a research brief for scope and sources' },
142
      },
143
      {
144
        interactionId: 'ix_shared_ops',
145
        initiatorAgentId: 'agent_atlas',
146
        responderAgentId: 'agent_peer_keel',
147
        status: 'active',
148
        termsHash: '12ab44c0e91f77d0aa3310bc44ee9012',
149
        createdAt: ago(90),
150
        contract: { purpose: 'Share a sanitized incident timeline' },
151
        acceptances: {
152
          agent_peer_keel: { method: 'policy_auto_accept' },
153
        },
154
      },
155
    ],
156
    liveSessions: [
157
      {
158
        interactionId: 'ix_shared_ops',
159
        initiatorAgentId: 'agent_atlas',
160
        responderAgentId: 'agent_peer_keel',
161
        status: 'active',
162
        createdAt: ago(88),
163
        activatedAt: ago(87),
164
        expiresAt: ahead(12),
165
      },
166
    ],
167
    hostedThreads: [],
168
    events: [
169
      {
170
        eventId: 'evt_1',
171
        eventType: 'interaction_started',
172
        agentId: 'agent_atlas',
173
        interactionId: 'ix_shared_ops',
174
        timestamp: ago(88),
175
        visibility: 'participants',
176
      },
177
      {
178
        eventId: 'evt_2',
179
        eventType: 'policy_warning',
180
        agentId: 'agent_harbor',
181
        interactionId: 'ix_local_brief',
182
        timestamp: ago(200),
183
        visibility: 'owner',
184
      },
185
      {
186
        eventId: 'evt_3',
187
        eventType: 'claim_checked',
188
        agentId: 'agent_atlas',
189
        interactionId: 'ix_local_brief',
190
        timestamp: ago(230),
191
        visibility: 'participants',
192
      },
193
      {
194
        eventId: 'evt_4',
195
        eventType: 'session_activated',
196
        agentId: 'agent_atlas',
197
        interactionId: 'ix_shared_ops',
198
        timestamp: ago(87),
199
        visibility: 'participants',
200
      },
201
    ],
202
    conflicts: [
203
      {
204
        conflictId: 'dsp_1',
205
        interactionId: 'ix_local_brief',
206
        status: 'open',
207
        createdAt: ago(190),
208
      },
209
    ],
210
    receipts: [
211
      {
212
        receiptId: 'rcpt_1',
213
        interactionId: 'ix_local_brief',
214
        agentId: 'agent_atlas',
215
        outcome: 'success',
216
        completedAt: ago(220),
217
      },
218
    ],
219
    profiles: [
220
      {
221
        agentId: 'agent_atlas',
222
        taskCategory: 'research',
223
        agentVersion: '1.4.2',
224
        sampleSize: 14,
225
        completion: 0.92,
226
        scope: 0.84,
227
        evidence: 0.88,
228
        communication: 0.79,
229
        deadline: 0.9,
230
      },
231
      {
232
        agentId: 'agent_harbor',
233
        taskCategory: 'ops',
234
        agentVersion: '2.1.0',
235
        sampleSize: 6,
236
        completion: 0.71,
237
        scope: 0.66,
238
        evidence: 0.58,
239
        communication: 0.74,
240
        deadline: 0.62,
241
      },
242
    ],
243
    runtimes: [
244
      {
245
        agentId: 'agent_atlas',
246
        endpoint: 'https://atlas.example.com/openclasp',
247
        a2aEndpoint: 'https://atlas.example.com/a2a',
248
        status: 'verified',
249
        verifiedAt: ago(40),
250
      },
251
    ],
252
    accessTokens: [],
253
  };
254
}
255
256
export function applyPreviewRequest(
257
  data: DashboardData,
258
  settings: Settings,
259
  path: string,
260
  init?: RequestInit,
261
): { data: DashboardData; settings: Settings; result: unknown } {
262
  const method = (init?.method ?? 'GET').toUpperCase();
263
  const body = init?.body ? (JSON.parse(String(init.body)) as Record<string, unknown>) : {};
264
265
  if (path === '/v0.1/dashboard' && method === 'GET') return { data, settings, result: data };
266
  if (path === '/v0.1/settings' && method === 'GET') return { data, settings, result: settings };
267
  if (path === '/v0.1/settings' && method === 'PUT') {
268
    const next = {
269
      ...settings,
270
      ...body,
271
      rawConversationsStored: false as const,
272
    } as Settings;
273
    return { data, settings: next, result: next };
274
  }
275
276
  const automation = path.match(/^\/v0\.1\/agents\/([^/]+)\/automation$/);
277
  if (automation && method === 'PUT') {
278
    const agentId = decodeURIComponent(automation[1] ?? '');
279
    const next = {
280
      ...data,
281
      agents: data.agents.map((agent) =>
282
        agent.agentId === agentId
283
          ? {
284
              ...agent,
285
              autoPublish: Boolean(body.autoPublish),
286
              autoAcceptPolicy: body.autoAcceptPolicy,
287
              autoAcceptTaskCategories: body.autoAcceptTaskCategories,
288
            }
289
          : agent,
290
      ),
291
      publications: body.autoPublish
292
        ? [
293
            ...data.publications.filter((item) => item.agentId !== agentId),
294
            { agentId, published: true, updatedAt: new Date().toISOString() },
295
          ]
296
        : data.publications.filter((item) => item.agentId !== agentId),
297
    };
298
    return { data: next, settings, result: { ok: true } };
299
  }
300
301
  const runtime = path.match(/^\/v0\.1\/agents\/([^/]+)\/runtime$/);
302
  if (runtime && method === 'PUT') {
303
    const agentId = decodeURIComponent(runtime[1] ?? '');
304
    const endpoint = String(body.endpoint ?? '');
305
    const record = {
306
      agentId,
307
      endpoint,
308
      a2aEndpoint: endpoint,
309
      status: 'verified',
310
      verifiedAt: new Date().toISOString(),
311
    };
312
    return {
313
      data: {
314
        ...data,
315
        runtimes: [...data.runtimes.filter((item) => item.agentId !== agentId), record],
316
      },
317
      settings,
318
      result: record,
319
    };
320
  }
321
  const tokenCollection = path.match(/^\/v0\.1\/agents\/([^/]+)\/access-tokens$/);
322
  if (tokenCollection && method === 'POST') {
323
    const agentId = decodeURIComponent(tokenCollection[1] ?? '');
324
    const tokenId = crypto.randomUUID().replaceAll('-', '').slice(0, 16);
325
    const createdAt = new Date();
326
    const expiresInDays = Number(body.expiresInDays ?? 365);
327
    const record = {
328
      tokenId,
329
      agentId,
330
      name: String(body.name ?? 'Hosted provider'),
331
      scopes: ['mcp:access'],
332
      createdAt: createdAt.toISOString(),
333
      expiresAt: new Date(createdAt.getTime() + expiresInDays * 86_400_000).toISOString(),
334
    };
335
    return {
336
      data: { ...data, accessTokens: [record, ...data.accessTokens] },
337
      settings,
338
      result: {
339
        ...record,
340
        token: `oc_at_${tokenId}.preview_agent_access_token_secret_not_for_production`,
341
      },
342
    };
343
  }
344
  const tokenItem = path.match(/^\/v0\.1\/agents\/([^/]+)\/access-tokens\/([^/]+)$/);
345
  if (tokenItem && method === 'DELETE') {
346
    const tokenId = decodeURIComponent(tokenItem[2] ?? '');
347
    return {
348
      data: {
349
        ...data,
350
        accessTokens: data.accessTokens.map((token) =>
351
          token.tokenId === tokenId ? { ...token, revokedAt: new Date().toISOString() } : token,
352
        ),
353
      },
354
      settings,
355
      result: { tokenId, revokedAt: new Date().toISOString() },
356
    };
357
  }
358
  const agentDelete = path.match(/^\/v0\.1\/agents\/([^/]+)$/);
359
  if (agentDelete && method === 'DELETE') {
360
    const agentId = decodeURIComponent(agentDelete[1] ?? '');
361
    return {
362
      data: {
363
        ...data,
364
        agents: data.agents.filter((item) => item.agentId !== agentId),
365
        publications: data.publications.filter((item) => item.agentId !== agentId),
366
        runtimes: data.runtimes.filter((item) => item.agentId !== agentId),
367
        installations: data.installations.filter((item) => item.agentId !== agentId),
368
        accessTokens: data.accessTokens.filter((item) => item.agentId !== agentId),
369
      },
370
      settings,
371
      result: { agentId, deleted: true },
372
    };
373
  }
374
375
  if (runtime && method === 'DELETE') {
376
    const agentId = decodeURIComponent(runtime[1] ?? '');
377
    return {
378
      data: {
379
        ...data,
380
        runtimes: data.runtimes.filter((item) => item.agentId !== agentId),
381
      },
382
      settings,
383
      result: { agentId, status: 'disabled' },
384
    };
385
  }
386
387
  const federated = path.match(/^\/v0\.1\/federated-interactions\/([^/]+)\/respond$/);
388
  if (federated && method === 'POST') {
389
    const interactionId = decodeURIComponent(federated[1] ?? '');
390
    const decision = body.decision === 'accept' ? 'active' : 'rejected';
391
    return {
392
      data: {
393
        ...data,
394
        federatedInteractions: data.federatedInteractions.map((item) =>
395
          item.interactionId === interactionId ? { ...item, status: decision } : item,
396
        ),
397
      },
398
      settings,
399
      result: { ok: true },
400
    };
401
  }
402
403
  const onboarding = path.match(/^\/v0\.1\/onboarding\/([^/]+)\/(approve|reject)$/);
404
  if (onboarding && method === 'POST') {
405
    const requestId = decodeURIComponent(onboarding[1] ?? '');
406
    const decision = onboarding[2] === 'approve' ? 'approved' : 'rejected';
407
    return {
408
      data: {
409
        ...data,
410
        setupRequests: data.setupRequests.map((item) =>
411
          item.requestId === requestId
412
            ? { ...item, status: decision, decidedAt: new Date().toISOString() }
413
            : item,
414
        ),
415
      },
416
      settings,
417
      result: { ok: true },
418
    };
419
  }
420
421
  throw new Error('Preview cannot perform this action');
422
}
423
424
export function isPreviewActive() {
425
  return import.meta.env.DEV && sessionStorage.getItem(previewKey) === '1';
426
}
427
428
export function enablePreview() {
429
  sessionStorage.setItem(previewKey, '1');
430
}
431
432
export function disablePreview() {
433
  sessionStorage.removeItem(previewKey);
434
}