@anvia/studio 0.2.1 → 0.2.3
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.
- package/README.md +14 -4
- package/dist/index.d.ts +29 -1
- package/dist/index.js +362 -34
- package/dist/index.js.map +1 -1
- package/dist/ui/assets/index-BUBW1GM4.css +1 -0
- package/dist/ui/assets/index-BrLQCu4l.js +99 -0
- package/dist/ui/assets/index-BrLQCu4l.js.map +1 -0
- package/dist/ui/index.html +2 -2
- package/package.json +5 -5
- package/dist/ui/assets/index-BYYOd6bv.css +0 -1
- package/dist/ui/assets/index-D7QvySKU.js +0 -91
- package/dist/ui/assets/index-D7QvySKU.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @anvia/studio
|
|
2
2
|
|
|
3
|
-
Studio UI and HTTP runtime for Anvia agents.
|
|
3
|
+
Studio UI and HTTP runtime for Anvia agents, pipelines, tools, MCPs, and knowledge inspection.
|
|
4
4
|
|
|
5
|
-
Use this package to serve local agents over HTTP, inspect sessions and
|
|
5
|
+
Use this package to serve local agents and pipelines over HTTP, inspect sessions, traces, tools, MCPs, and Knowledge in the browser UI, and exercise tool approval workflows during development.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -44,9 +44,19 @@ Then open:
|
|
|
44
44
|
http://localhost:4021/playground
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
## Browser UI
|
|
48
|
+
|
|
49
|
+
Studio exposes:
|
|
50
|
+
|
|
51
|
+
- Chat playground and persisted sessions
|
|
52
|
+
- Trace browser and session logs
|
|
53
|
+
- Pipeline graph, logs, and run history
|
|
54
|
+
- Agent, static tool, dynamic tool, and MCP inspectors
|
|
55
|
+
- Knowledge tabs for static context, dynamic context, dynamic tools, and retrieval log
|
|
56
|
+
|
|
47
57
|
## Session Storage
|
|
48
58
|
|
|
49
|
-
Studio uses a local SQLite store by default so sessions and
|
|
59
|
+
Studio uses a local SQLite store by default so sessions, traces, and pipeline run history can persist across process restarts. If you omit the port, Studio uses `RUNNER_PORT` and then falls back to `4021`.
|
|
50
60
|
|
|
51
61
|
Set `ANVIA_STUDIO_DB` to control the database path:
|
|
52
62
|
|
|
@@ -58,7 +68,7 @@ ANVIA_STUDIO_DB=.anvia/studio.sqlite node ./dist/server.js
|
|
|
58
68
|
|
|
59
69
|
- `Studio`
|
|
60
70
|
- `createSqliteSessionStore`
|
|
61
|
-
- Studio session, trace, approval, and runtime types
|
|
71
|
+
- Studio session, trace, approval, pipeline, knowledge, tool, MCP, and runtime types
|
|
62
72
|
|
|
63
73
|
## Development
|
|
64
74
|
|
package/dist/index.d.ts
CHANGED
|
@@ -260,8 +260,15 @@ type StudioTraceStore = {
|
|
|
260
260
|
};
|
|
261
261
|
type StudioKnowledgeSourceKind = "static_context" | "dynamic_context" | "dynamic_tools";
|
|
262
262
|
type StudioKnowledgeSourceSummary = {
|
|
263
|
+
sourceId?: string;
|
|
263
264
|
kind: StudioKnowledgeSourceKind;
|
|
265
|
+
label?: string;
|
|
264
266
|
count: number;
|
|
267
|
+
registrationIndex?: number;
|
|
268
|
+
topK?: number;
|
|
269
|
+
threshold?: number;
|
|
270
|
+
inspectable?: boolean;
|
|
271
|
+
itemCount?: number;
|
|
265
272
|
};
|
|
266
273
|
type StudioStaticKnowledgeDocument = {
|
|
267
274
|
id: string;
|
|
@@ -292,6 +299,27 @@ type StudioAgentKnowledgeConfig = {
|
|
|
292
299
|
sources: StudioKnowledgeSourceSummary[];
|
|
293
300
|
staticContext: StudioStaticKnowledgeDocument[];
|
|
294
301
|
};
|
|
302
|
+
type StudioKnowledgeItemKind = "static_context" | "dynamic_context" | "dynamic_tool";
|
|
303
|
+
type StudioKnowledgeItem = {
|
|
304
|
+
id: string;
|
|
305
|
+
kind: StudioKnowledgeItemKind;
|
|
306
|
+
text?: string;
|
|
307
|
+
document?: JsonValue;
|
|
308
|
+
toolName?: string;
|
|
309
|
+
description?: string;
|
|
310
|
+
parameterKeys?: string[];
|
|
311
|
+
metadata?: JsonObject;
|
|
312
|
+
};
|
|
313
|
+
type StudioKnowledgeItemsPage = {
|
|
314
|
+
agentId: string;
|
|
315
|
+
sourceId: string;
|
|
316
|
+
kind: StudioKnowledgeSourceKind;
|
|
317
|
+
inspectable: boolean;
|
|
318
|
+
items: StudioKnowledgeItem[];
|
|
319
|
+
nextCursor?: string;
|
|
320
|
+
totalCount?: number;
|
|
321
|
+
message?: string;
|
|
322
|
+
};
|
|
295
323
|
type StudioKnowledgeSummary = {
|
|
296
324
|
agents: StudioAgentKnowledgeConfig[];
|
|
297
325
|
evidence: StudioKnowledgeEvidence[];
|
|
@@ -544,4 +572,4 @@ type SqliteSessionStoreOptions = {
|
|
|
544
572
|
};
|
|
545
573
|
declare function createSqliteSessionStore(options?: SqliteSessionStoreOptions): StudioSessionStore & StudioTraceStore & StudioPipelineLogStore & StudioPipelineRunStore;
|
|
546
574
|
|
|
547
|
-
export { type AgentRunRequest, type AgentRunResponse, type AgentRunStreamEvent, type AnviaStudio, type SqliteSessionStoreOptions, Studio, type StudioAgent, type StudioAgentConfig, type StudioAgentKnowledgeConfig, type StudioAgentMcpServerMetadata, type StudioAgentMcpToolMetadata, type StudioAgentMcpsSummary, type StudioAgentToolApprovalMetadata, type StudioAgentToolMetadata, type StudioAgentToolSource, type StudioAgentToolsSummary, type StudioCapability, type StudioCapabilityConfig, type StudioConfig, type StudioErrorCode, type StudioErrorResponse, type StudioKnowledgeEvidence, type StudioKnowledgeEvidenceDocument, type StudioKnowledgeSourceKind, type StudioKnowledgeSourceSummary, type StudioKnowledgeSummary, type StudioOptions, type StudioPipeline, type StudioPipelineConfig, type StudioPipelineDetail, type StudioPipelineFinalEvent, type StudioPipelineLogAppendInput, type StudioPipelineLogCategory, type StudioPipelineLogEntry, type StudioPipelineLogEvent, type StudioPipelineLogLevel, type StudioPipelineLogListOptions, type StudioPipelineLogStore, type StudioPipelineRunListOptions, type StudioPipelineRunRecord, type StudioPipelineRunRequest, type StudioPipelineRunResponse, type StudioPipelineRunSaveInput, type StudioPipelineRunStatus, type StudioPipelineRunStore, type StudioServeOptions, type StudioSession, type StudioSessionCreateInput, type StudioSessionListOptions, type StudioSessionLogAppendInput, type StudioSessionLogCategory, type StudioSessionLogEntry, type StudioSessionLogEvent, type StudioSessionLogLevel, type StudioSessionLogListOptions, type StudioSessionRunStatus, type StudioSessionRunTranscriptInput, type StudioSessionStore, type StudioSessionSummary, type StudioSessionTraceListOptions, type StudioStaticKnowledgeDocument, type StudioStores, type StudioTarget, type StudioToolApproval, type StudioToolApprovalDecision, type StudioToolApprovalRequestEvent, type StudioToolApprovalResultEvent, type StudioToolApprovalStatus, type StudioToolApprovalTranscript, type StudioToolQuestion, type StudioToolQuestionAnswer, type StudioToolQuestionChoice, type StudioToolQuestionPrompt, type StudioToolQuestionRequestEvent, type StudioToolQuestionResultEvent, type StudioToolQuestionStatus, type StudioToolQuestionTranscript, type StudioTrace, type StudioTraceListOptions, type StudioTraceObservation, type StudioTraceObservationKind, StudioTraceObserver, type StudioTraceObserverOptions, type StudioTraceStatus, type StudioTraceStore, type StudioTraceSummary, type StudioTranscriptChatEntry, type StudioTranscriptChildAgentEvent, type StudioTranscriptEntry, type StudioTranscriptReasoningEntry, type StudioTranscriptToolEntry, type StudioUiOptions, createSqliteSessionStore };
|
|
575
|
+
export { type AgentRunRequest, type AgentRunResponse, type AgentRunStreamEvent, type AnviaStudio, type SqliteSessionStoreOptions, Studio, type StudioAgent, type StudioAgentConfig, type StudioAgentKnowledgeConfig, type StudioAgentMcpServerMetadata, type StudioAgentMcpToolMetadata, type StudioAgentMcpsSummary, type StudioAgentToolApprovalMetadata, type StudioAgentToolMetadata, type StudioAgentToolSource, type StudioAgentToolsSummary, type StudioCapability, type StudioCapabilityConfig, type StudioConfig, type StudioErrorCode, type StudioErrorResponse, type StudioKnowledgeEvidence, type StudioKnowledgeEvidenceDocument, type StudioKnowledgeItem, type StudioKnowledgeItemKind, type StudioKnowledgeItemsPage, type StudioKnowledgeSourceKind, type StudioKnowledgeSourceSummary, type StudioKnowledgeSummary, type StudioOptions, type StudioPipeline, type StudioPipelineConfig, type StudioPipelineDetail, type StudioPipelineFinalEvent, type StudioPipelineLogAppendInput, type StudioPipelineLogCategory, type StudioPipelineLogEntry, type StudioPipelineLogEvent, type StudioPipelineLogLevel, type StudioPipelineLogListOptions, type StudioPipelineLogStore, type StudioPipelineRunListOptions, type StudioPipelineRunRecord, type StudioPipelineRunRequest, type StudioPipelineRunResponse, type StudioPipelineRunSaveInput, type StudioPipelineRunStatus, type StudioPipelineRunStore, type StudioServeOptions, type StudioSession, type StudioSessionCreateInput, type StudioSessionListOptions, type StudioSessionLogAppendInput, type StudioSessionLogCategory, type StudioSessionLogEntry, type StudioSessionLogEvent, type StudioSessionLogLevel, type StudioSessionLogListOptions, type StudioSessionRunStatus, type StudioSessionRunTranscriptInput, type StudioSessionStore, type StudioSessionSummary, type StudioSessionTraceListOptions, type StudioStaticKnowledgeDocument, type StudioStores, type StudioTarget, type StudioToolApproval, type StudioToolApprovalDecision, type StudioToolApprovalRequestEvent, type StudioToolApprovalResultEvent, type StudioToolApprovalStatus, type StudioToolApprovalTranscript, type StudioToolQuestion, type StudioToolQuestionAnswer, type StudioToolQuestionChoice, type StudioToolQuestionPrompt, type StudioToolQuestionRequestEvent, type StudioToolQuestionResultEvent, type StudioToolQuestionStatus, type StudioToolQuestionTranscript, type StudioTrace, type StudioTraceListOptions, type StudioTraceObservation, type StudioTraceObservationKind, StudioTraceObserver, type StudioTraceObserverOptions, type StudioTraceStatus, type StudioTraceStore, type StudioTraceSummary, type StudioTranscriptChatEntry, type StudioTranscriptChildAgentEvent, type StudioTranscriptEntry, type StudioTranscriptReasoningEntry, type StudioTranscriptToolEntry, type StudioUiOptions, createSqliteSessionStore };
|
package/dist/index.js
CHANGED
|
@@ -51,11 +51,7 @@ var StudioRunTraceObserver = class {
|
|
|
51
51
|
startedAt,
|
|
52
52
|
input: toJsonValue(args.request),
|
|
53
53
|
output: toJsonValue(endArgs.response),
|
|
54
|
-
metadata:
|
|
55
|
-
model: args.request.model ?? "default",
|
|
56
|
-
toolCount: args.request.tools.length,
|
|
57
|
-
...endArgs.firstDeltaMs === void 0 ? {} : { firstDeltaMs: endArgs.firstDeltaMs }
|
|
58
|
-
}
|
|
54
|
+
metadata: generationMetadata(args, endArgs)
|
|
59
55
|
})
|
|
60
56
|
);
|
|
61
57
|
},
|
|
@@ -69,10 +65,7 @@ var StudioRunTraceObserver = class {
|
|
|
69
65
|
startedAt,
|
|
70
66
|
input: toJsonValue(args.request),
|
|
71
67
|
error: serializeError(errorArgs.error),
|
|
72
|
-
metadata:
|
|
73
|
-
model: args.request.model ?? "default",
|
|
74
|
-
toolCount: args.request.tools.length
|
|
75
|
-
}
|
|
68
|
+
metadata: generationMetadata(args)
|
|
76
69
|
})
|
|
77
70
|
);
|
|
78
71
|
}
|
|
@@ -94,7 +87,7 @@ var StudioRunTraceObserver = class {
|
|
|
94
87
|
startedAt,
|
|
95
88
|
input: parseOrString(args.args),
|
|
96
89
|
output: parseOrString(endArgs.result),
|
|
97
|
-
metadata: toolMetadata(args, endArgs.skipped)
|
|
90
|
+
metadata: toolMetadata(args, endArgs.skipped, endArgs.result)
|
|
98
91
|
});
|
|
99
92
|
this.observations.push(parentObservation);
|
|
100
93
|
this.observations.push(...childTrace.observations(parentObservation.id));
|
|
@@ -372,13 +365,134 @@ function traceMetadata(args, messages) {
|
|
|
372
365
|
messages
|
|
373
366
|
});
|
|
374
367
|
}
|
|
375
|
-
function
|
|
368
|
+
function generationMetadata(args, endArgs) {
|
|
369
|
+
const request = args.request;
|
|
370
|
+
const response = endArgs?.response;
|
|
371
|
+
const rawResponse = isRecord(response?.rawResponse) ? response.rawResponse : void 0;
|
|
372
|
+
const effectiveModel = request.model ?? stringValue(rawResponse?.model) ?? args.modelInfo?.defaultModel ?? "default";
|
|
373
|
+
const providerResponse = providerResponseSummary(rawResponse);
|
|
374
|
+
const usage = response?.usage;
|
|
375
|
+
return compactJsonObject({
|
|
376
|
+
provider: args.modelInfo?.provider,
|
|
377
|
+
model: effectiveModel,
|
|
378
|
+
requestedModel: request.model,
|
|
379
|
+
defaultModel: args.modelInfo?.defaultModel,
|
|
380
|
+
messageId: response?.messageId,
|
|
381
|
+
usage,
|
|
382
|
+
toolCount: request.tools.length,
|
|
383
|
+
toolNames: request.tools.map((tool) => tool.name),
|
|
384
|
+
documentCount: request.documents.length,
|
|
385
|
+
historyCount: request.chatHistory.length,
|
|
386
|
+
temperature: request.temperature,
|
|
387
|
+
maxTokens: request.maxTokens,
|
|
388
|
+
toolChoice: request.toolChoice,
|
|
389
|
+
additionalParamKeys: isRecord(request.additionalParams) ? Object.keys(request.additionalParams).sort() : void 0,
|
|
390
|
+
hasOutputSchema: request.outputSchema !== void 0,
|
|
391
|
+
firstDeltaMs: endArgs?.firstDeltaMs,
|
|
392
|
+
providerResponse,
|
|
393
|
+
modelInfo: compactJsonObject({
|
|
394
|
+
provider: args.modelInfo?.provider,
|
|
395
|
+
model: effectiveModel,
|
|
396
|
+
requestedModel: request.model,
|
|
397
|
+
defaultModel: args.modelInfo?.defaultModel,
|
|
398
|
+
capabilities: args.modelInfo?.capabilities
|
|
399
|
+
}),
|
|
400
|
+
modelCall: compactJsonObject({
|
|
401
|
+
request: completionRequestSummary(request),
|
|
402
|
+
providerRequest: args.providerRequest
|
|
403
|
+
}),
|
|
404
|
+
response: compactJsonObject({
|
|
405
|
+
messageId: response?.messageId,
|
|
406
|
+
usage,
|
|
407
|
+
contentTypes: response?.choice.map((item) => item.type),
|
|
408
|
+
providerResponse
|
|
409
|
+
}),
|
|
410
|
+
tools: compactJsonObject({
|
|
411
|
+
count: request.tools.length,
|
|
412
|
+
names: request.tools.map((tool) => tool.name),
|
|
413
|
+
toolChoice: request.toolChoice,
|
|
414
|
+
hasOutputSchema: request.outputSchema !== void 0
|
|
415
|
+
}),
|
|
416
|
+
timing: compactJsonObject({
|
|
417
|
+
firstDeltaMs: endArgs?.firstDeltaMs
|
|
418
|
+
})
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
function completionRequestSummary(request) {
|
|
422
|
+
return compactJsonObject({
|
|
423
|
+
model: request.model,
|
|
424
|
+
instructions: request.instructions === void 0 ? void 0 : { present: true },
|
|
425
|
+
messageCount: request.chatHistory.length,
|
|
426
|
+
documentCount: request.documents.length,
|
|
427
|
+
documentIds: request.documents.map((document) => document.id),
|
|
428
|
+
toolCount: request.tools.length,
|
|
429
|
+
toolNames: request.tools.map((tool) => tool.name),
|
|
430
|
+
temperature: request.temperature,
|
|
431
|
+
maxTokens: request.maxTokens,
|
|
432
|
+
toolChoice: request.toolChoice,
|
|
433
|
+
additionalParamKeys: isRecord(request.additionalParams) ? Object.keys(request.additionalParams).sort() : void 0,
|
|
434
|
+
hasOutputSchema: request.outputSchema !== void 0
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function providerResponseSummary(rawResponse) {
|
|
438
|
+
if (rawResponse === void 0) {
|
|
439
|
+
return void 0;
|
|
440
|
+
}
|
|
441
|
+
const reasoning = isRecord(rawResponse.reasoning) ? rawResponse.reasoning : void 0;
|
|
442
|
+
const text = isRecord(rawResponse.text) ? rawResponse.text : void 0;
|
|
443
|
+
const toolUsage = isRecord(rawResponse.tool_usage) ? rawResponse.tool_usage : void 0;
|
|
444
|
+
const webSearch = isRecord(toolUsage?.web_search) ? toolUsage.web_search : void 0;
|
|
445
|
+
const summary = compactJsonObject({
|
|
446
|
+
id: rawResponse.id,
|
|
447
|
+
status: rawResponse.status,
|
|
448
|
+
serviceTier: rawResponse.service_tier,
|
|
449
|
+
store: rawResponse.store,
|
|
450
|
+
parallelToolCalls: rawResponse.parallel_tool_calls,
|
|
451
|
+
promptCacheKey: rawResponse.prompt_cache_key,
|
|
452
|
+
promptCacheRetention: rawResponse.prompt_cache_retention,
|
|
453
|
+
reasoningEffort: reasoning?.effort,
|
|
454
|
+
textVerbosity: text?.verbosity,
|
|
455
|
+
webSearchRequestCount: webSearch?.num_requests
|
|
456
|
+
});
|
|
457
|
+
return Object.keys(summary).length === 0 ? void 0 : summary;
|
|
458
|
+
}
|
|
459
|
+
function toolMetadata(args, skipped, result) {
|
|
460
|
+
const schema = isRecord(args.toolDefinition?.parameters) ? args.toolDefinition.parameters : {};
|
|
461
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
462
|
+
const required = Array.isArray(schema.required) ? schema.required.filter((item) => typeof item === "string") : [];
|
|
376
463
|
return compactJsonObject({
|
|
377
464
|
internalCallId: args.internalCallId,
|
|
378
465
|
toolCallId: args.toolCallId,
|
|
379
|
-
skipped
|
|
466
|
+
skipped,
|
|
467
|
+
argumentBytes: byteLength(args.args),
|
|
468
|
+
resultBytes: result === void 0 ? void 0 : byteLength(result),
|
|
469
|
+
hasCallSignature: args.toolCall.signature !== void 0,
|
|
470
|
+
hasAdditionalParams: args.toolCall.additionalParams !== void 0,
|
|
471
|
+
toolDescription: args.toolDefinition?.description,
|
|
472
|
+
parameterKeys: Object.keys(properties).sort(),
|
|
473
|
+
requiredParameterKeys: required,
|
|
474
|
+
approvalRequired: args.toolMetadata?.approvalRequired,
|
|
475
|
+
mcpServerName: args.toolMetadata?.mcpServerName,
|
|
476
|
+
tools: compactJsonObject({
|
|
477
|
+
name: args.toolName,
|
|
478
|
+
internalCallId: args.internalCallId,
|
|
479
|
+
toolCallId: args.toolCallId,
|
|
480
|
+
skipped,
|
|
481
|
+
description: args.toolDefinition?.description,
|
|
482
|
+
parameterKeys: Object.keys(properties).sort(),
|
|
483
|
+
requiredParameterKeys: required,
|
|
484
|
+
approvalRequired: args.toolMetadata?.approvalRequired,
|
|
485
|
+
mcpServerName: args.toolMetadata?.mcpServerName,
|
|
486
|
+
argumentBytes: byteLength(args.args),
|
|
487
|
+
resultBytes: result === void 0 ? void 0 : byteLength(result),
|
|
488
|
+
hasCallSignature: args.toolCall.signature !== void 0,
|
|
489
|
+
hasAdditionalParams: args.toolCall.additionalParams !== void 0
|
|
490
|
+
})
|
|
380
491
|
});
|
|
381
492
|
}
|
|
493
|
+
function byteLength(value) {
|
|
494
|
+
return new TextEncoder().encode(value).length;
|
|
495
|
+
}
|
|
382
496
|
function compactJsonObject(values) {
|
|
383
497
|
const entries = Object.entries(values).flatMap(
|
|
384
498
|
([key, value]) => value === void 0 ? [] : [[key, toJsonValue(value)]]
|
|
@@ -473,6 +587,8 @@ function registerStudioUi(app, options) {
|
|
|
473
587
|
app.get(`${options.path}/sessions`, async (c) => c.html(await renderShell()));
|
|
474
588
|
app.get(`${options.path}/agents`, async (c) => c.html(await renderShell()));
|
|
475
589
|
app.get(`${options.path}/knowledge`, async (c) => c.html(await renderShell()));
|
|
590
|
+
app.get(`${options.path}/knowledge/:tab`, async (c) => c.html(await renderShell()));
|
|
591
|
+
app.get(`${options.path}/knowledge/*`, async (c) => c.html(await renderShell()));
|
|
476
592
|
if (options.rootRoutes) {
|
|
477
593
|
app.get("/playground", async (c) => c.html(await renderRootShell()));
|
|
478
594
|
app.get("/playground/:sessionId", async (c) => c.html(await renderRootShell()));
|
|
@@ -2382,22 +2498,41 @@ function registerKnowledgeRoutes(app, props) {
|
|
|
2382
2498
|
return errorResponse(c, 400, "bad_request", "Invalid limit");
|
|
2383
2499
|
}
|
|
2384
2500
|
const summary = {
|
|
2385
|
-
agents: props.agents.map(agentKnowledgeConfig),
|
|
2501
|
+
agents: await Promise.all(props.agents.map(agentKnowledgeConfig)),
|
|
2386
2502
|
evidence: await recentKnowledgeEvidence(props.traceStore, limit)
|
|
2387
2503
|
};
|
|
2388
2504
|
return c.json(summary);
|
|
2389
2505
|
});
|
|
2506
|
+
app.get("/knowledge/items", async (c) => {
|
|
2507
|
+
const limit = parseLimit(c.req.query("limit"));
|
|
2508
|
+
if (limit === void 0) {
|
|
2509
|
+
return errorResponse(c, 400, "bad_request", "Invalid limit");
|
|
2510
|
+
}
|
|
2511
|
+
const agentId = optionalQueryString(c.req.query("agentId"));
|
|
2512
|
+
const sourceId = optionalQueryString(c.req.query("sourceId"));
|
|
2513
|
+
if (agentId === void 0 || sourceId === void 0) {
|
|
2514
|
+
return errorResponse(c, 400, "bad_request", "agentId and sourceId are required");
|
|
2515
|
+
}
|
|
2516
|
+
const agent = props.agents.find((item) => item.id === agentId);
|
|
2517
|
+
if (agent === void 0) {
|
|
2518
|
+
return errorResponse(c, 404, "not_found", "Agent not found");
|
|
2519
|
+
}
|
|
2520
|
+
const page = await knowledgeItemsPage(agent, sourceId, {
|
|
2521
|
+
limit,
|
|
2522
|
+
cursor: optionalQueryString(c.req.query("cursor"))
|
|
2523
|
+
});
|
|
2524
|
+
if (page === void 0) {
|
|
2525
|
+
return errorResponse(c, 404, "not_found", "Knowledge source not found");
|
|
2526
|
+
}
|
|
2527
|
+
return c.json(page);
|
|
2528
|
+
});
|
|
2390
2529
|
}
|
|
2391
|
-
function agentKnowledgeConfig(agent) {
|
|
2530
|
+
async function agentKnowledgeConfig(agent) {
|
|
2392
2531
|
const agentName = agent.name ?? agent.agent.name;
|
|
2393
2532
|
return {
|
|
2394
2533
|
agentId: agent.id,
|
|
2395
2534
|
...agentName === void 0 ? {} : { agentName },
|
|
2396
|
-
sources:
|
|
2397
|
-
{ kind: "static_context", count: agent.agent.staticContext.length },
|
|
2398
|
-
{ kind: "dynamic_context", count: agent.agent.dynamicContexts.length },
|
|
2399
|
-
{ kind: "dynamic_tools", count: agent.agent.dynamicTools.length }
|
|
2400
|
-
],
|
|
2535
|
+
sources: await knowledgeSources(agent),
|
|
2401
2536
|
staticContext: agent.agent.staticContext.map((document) => ({
|
|
2402
2537
|
id: document.id,
|
|
2403
2538
|
text: document.text,
|
|
@@ -2405,6 +2540,199 @@ function agentKnowledgeConfig(agent) {
|
|
|
2405
2540
|
}))
|
|
2406
2541
|
};
|
|
2407
2542
|
}
|
|
2543
|
+
async function knowledgeSources(agent) {
|
|
2544
|
+
const sources = [
|
|
2545
|
+
{
|
|
2546
|
+
sourceId: staticSourceId(),
|
|
2547
|
+
kind: "static_context",
|
|
2548
|
+
label: "Static context",
|
|
2549
|
+
count: agent.agent.staticContext.length,
|
|
2550
|
+
inspectable: true,
|
|
2551
|
+
itemCount: agent.agent.staticContext.length
|
|
2552
|
+
}
|
|
2553
|
+
];
|
|
2554
|
+
const dynamicContextSources = await Promise.all(
|
|
2555
|
+
agent.agent.dynamicContexts.map(async (registration, index) => {
|
|
2556
|
+
const inspect = inspectFn(registration.index);
|
|
2557
|
+
const count = await inspectableCount(inspect, registration.options.filter);
|
|
2558
|
+
return {
|
|
2559
|
+
sourceId: dynamicContextSourceId(index),
|
|
2560
|
+
kind: "dynamic_context",
|
|
2561
|
+
label: `Dynamic context ${index + 1}`,
|
|
2562
|
+
count: 1,
|
|
2563
|
+
registrationIndex: index,
|
|
2564
|
+
topK: registration.options.topK,
|
|
2565
|
+
...registration.options.threshold === void 0 ? {} : { threshold: registration.options.threshold },
|
|
2566
|
+
inspectable: inspect !== void 0,
|
|
2567
|
+
...count === void 0 ? {} : { itemCount: count }
|
|
2568
|
+
};
|
|
2569
|
+
})
|
|
2570
|
+
);
|
|
2571
|
+
const dynamicToolSources = await Promise.all(
|
|
2572
|
+
agent.agent.dynamicTools.map(async (registration, index) => {
|
|
2573
|
+
const inspect = inspectFn(registration.index);
|
|
2574
|
+
const count = await inspectableCount(inspect, registration.options.filter);
|
|
2575
|
+
return {
|
|
2576
|
+
sourceId: dynamicToolsSourceId(index),
|
|
2577
|
+
kind: "dynamic_tools",
|
|
2578
|
+
label: `Dynamic tools ${index + 1}`,
|
|
2579
|
+
count: 1,
|
|
2580
|
+
registrationIndex: index,
|
|
2581
|
+
topK: registration.options.topK,
|
|
2582
|
+
...registration.options.threshold === void 0 ? {} : { threshold: registration.options.threshold },
|
|
2583
|
+
inspectable: inspect !== void 0,
|
|
2584
|
+
...count === void 0 ? {} : { itemCount: count }
|
|
2585
|
+
};
|
|
2586
|
+
})
|
|
2587
|
+
);
|
|
2588
|
+
return [...sources, ...dynamicContextSources, ...dynamicToolSources];
|
|
2589
|
+
}
|
|
2590
|
+
async function inspectableCount(inspect, filter) {
|
|
2591
|
+
if (inspect === void 0) {
|
|
2592
|
+
return void 0;
|
|
2593
|
+
}
|
|
2594
|
+
const page = await inspect({ limit: 1, filter });
|
|
2595
|
+
return page.totalCount;
|
|
2596
|
+
}
|
|
2597
|
+
async function knowledgeItemsPage(agent, sourceId, request) {
|
|
2598
|
+
if (sourceId === staticSourceId()) {
|
|
2599
|
+
return staticKnowledgeItemsPage(agent, request);
|
|
2600
|
+
}
|
|
2601
|
+
const dynamicContextIndex = dynamicSourceIndex(sourceId, "dynamic_context");
|
|
2602
|
+
if (dynamicContextIndex !== void 0) {
|
|
2603
|
+
const registration = agent.agent.dynamicContexts[dynamicContextIndex];
|
|
2604
|
+
if (registration === void 0) {
|
|
2605
|
+
return void 0;
|
|
2606
|
+
}
|
|
2607
|
+
const inspect = inspectFn(registration.index);
|
|
2608
|
+
if (inspect === void 0) {
|
|
2609
|
+
return nonInspectablePage(agent.id, sourceId, "dynamic_context");
|
|
2610
|
+
}
|
|
2611
|
+
const page = await inspect({
|
|
2612
|
+
limit: request.limit,
|
|
2613
|
+
cursor: request.cursor,
|
|
2614
|
+
filter: registration.options.filter
|
|
2615
|
+
});
|
|
2616
|
+
return {
|
|
2617
|
+
agentId: agent.id,
|
|
2618
|
+
sourceId,
|
|
2619
|
+
kind: "dynamic_context",
|
|
2620
|
+
inspectable: true,
|
|
2621
|
+
items: page.items.map((item) => dynamicContextItem(item)),
|
|
2622
|
+
...page.nextCursor === void 0 ? {} : { nextCursor: page.nextCursor },
|
|
2623
|
+
...page.totalCount === void 0 ? {} : { totalCount: page.totalCount }
|
|
2624
|
+
};
|
|
2625
|
+
}
|
|
2626
|
+
const dynamicToolsIndex = dynamicSourceIndex(sourceId, "dynamic_tools");
|
|
2627
|
+
if (dynamicToolsIndex !== void 0) {
|
|
2628
|
+
const registration = agent.agent.dynamicTools[dynamicToolsIndex];
|
|
2629
|
+
if (registration === void 0) {
|
|
2630
|
+
return void 0;
|
|
2631
|
+
}
|
|
2632
|
+
const inspect = inspectFn(registration.index);
|
|
2633
|
+
if (inspect === void 0) {
|
|
2634
|
+
return nonInspectablePage(agent.id, sourceId, "dynamic_tools");
|
|
2635
|
+
}
|
|
2636
|
+
const page = await inspect({
|
|
2637
|
+
limit: request.limit,
|
|
2638
|
+
cursor: request.cursor,
|
|
2639
|
+
filter: registration.options.filter
|
|
2640
|
+
});
|
|
2641
|
+
return {
|
|
2642
|
+
agentId: agent.id,
|
|
2643
|
+
sourceId,
|
|
2644
|
+
kind: "dynamic_tools",
|
|
2645
|
+
inspectable: true,
|
|
2646
|
+
items: page.items.map((item) => dynamicToolItem(item)),
|
|
2647
|
+
...page.nextCursor === void 0 ? {} : { nextCursor: page.nextCursor },
|
|
2648
|
+
...page.totalCount === void 0 ? {} : { totalCount: page.totalCount }
|
|
2649
|
+
};
|
|
2650
|
+
}
|
|
2651
|
+
return void 0;
|
|
2652
|
+
}
|
|
2653
|
+
function inspectFn(index) {
|
|
2654
|
+
if (!isRecord2(index) || typeof index.inspect !== "function") {
|
|
2655
|
+
return void 0;
|
|
2656
|
+
}
|
|
2657
|
+
const inspect = index.inspect;
|
|
2658
|
+
return (request) => inspect.call(index, request);
|
|
2659
|
+
}
|
|
2660
|
+
function staticKnowledgeItemsPage(agent, request) {
|
|
2661
|
+
const start = Math.max(0, Math.trunc(Number(request.cursor ?? "0")));
|
|
2662
|
+
const page = agent.agent.staticContext.slice(start, start + request.limit);
|
|
2663
|
+
const nextOffset = start + page.length;
|
|
2664
|
+
return {
|
|
2665
|
+
agentId: agent.id,
|
|
2666
|
+
sourceId: staticSourceId(),
|
|
2667
|
+
kind: "static_context",
|
|
2668
|
+
inspectable: true,
|
|
2669
|
+
items: page.map((document) => ({
|
|
2670
|
+
id: document.id,
|
|
2671
|
+
kind: "static_context",
|
|
2672
|
+
text: document.text,
|
|
2673
|
+
...document.additionalProps === void 0 ? {} : { metadata: jsonObjectFromRecord(document.additionalProps) }
|
|
2674
|
+
})),
|
|
2675
|
+
...nextOffset < agent.agent.staticContext.length ? { nextCursor: String(nextOffset) } : {},
|
|
2676
|
+
totalCount: agent.agent.staticContext.length
|
|
2677
|
+
};
|
|
2678
|
+
}
|
|
2679
|
+
function nonInspectablePage(agentId, sourceId, kind) {
|
|
2680
|
+
return {
|
|
2681
|
+
agentId,
|
|
2682
|
+
sourceId,
|
|
2683
|
+
kind,
|
|
2684
|
+
inspectable: false,
|
|
2685
|
+
items: [],
|
|
2686
|
+
message: "This source can be searched at runtime, but it does not expose browseable chunks."
|
|
2687
|
+
};
|
|
2688
|
+
}
|
|
2689
|
+
function dynamicContextItem(item) {
|
|
2690
|
+
const text = isRecord2(item.document) && typeof item.document.text === "string" ? item.document.text : typeof item.document === "string" ? item.document : void 0;
|
|
2691
|
+
return {
|
|
2692
|
+
id: item.id,
|
|
2693
|
+
kind: "dynamic_context",
|
|
2694
|
+
...text === void 0 ? { document: toJsonValue2(item.document) } : { text },
|
|
2695
|
+
...item.metadata === void 0 ? {} : { metadata: jsonObjectFromRecord(item.metadata) }
|
|
2696
|
+
};
|
|
2697
|
+
}
|
|
2698
|
+
function dynamicToolItem(item) {
|
|
2699
|
+
const document = isRecord2(item.document) ? item.document : {};
|
|
2700
|
+
const definition = isRecord2(document.definition) ? document.definition : {};
|
|
2701
|
+
const toolName = typeof document.toolName === "string" ? document.toolName : typeof definition.name === "string" ? definition.name : item.id;
|
|
2702
|
+
const description = typeof definition.description === "string" ? definition.description : "";
|
|
2703
|
+
return {
|
|
2704
|
+
id: item.id,
|
|
2705
|
+
kind: "dynamic_tool",
|
|
2706
|
+
toolName,
|
|
2707
|
+
description,
|
|
2708
|
+
parameterKeys: parameterKeys(definition.parameters),
|
|
2709
|
+
document: toJsonValue2(item.document),
|
|
2710
|
+
...item.metadata === void 0 ? {} : { metadata: jsonObjectFromRecord(item.metadata) }
|
|
2711
|
+
};
|
|
2712
|
+
}
|
|
2713
|
+
function parameterKeys(parameters) {
|
|
2714
|
+
if (!isRecord2(parameters) || !isRecord2(parameters.properties)) {
|
|
2715
|
+
return [];
|
|
2716
|
+
}
|
|
2717
|
+
return Object.keys(parameters.properties);
|
|
2718
|
+
}
|
|
2719
|
+
function staticSourceId() {
|
|
2720
|
+
return "static-context";
|
|
2721
|
+
}
|
|
2722
|
+
function dynamicContextSourceId(index) {
|
|
2723
|
+
return `dynamic-context-${index}`;
|
|
2724
|
+
}
|
|
2725
|
+
function dynamicToolsSourceId(index) {
|
|
2726
|
+
return `dynamic-tools-${index}`;
|
|
2727
|
+
}
|
|
2728
|
+
function dynamicSourceIndex(sourceId, kind) {
|
|
2729
|
+
const prefix = kind === "dynamic_context" ? "dynamic-context-" : "dynamic-tools-";
|
|
2730
|
+
if (!sourceId.startsWith(prefix)) {
|
|
2731
|
+
return void 0;
|
|
2732
|
+
}
|
|
2733
|
+
const index = Number(sourceId.slice(prefix.length));
|
|
2734
|
+
return Number.isInteger(index) && index >= 0 ? index : void 0;
|
|
2735
|
+
}
|
|
2408
2736
|
async function recentKnowledgeEvidence(traceStore, limit) {
|
|
2409
2737
|
if (traceStore?.listTraces === void 0) {
|
|
2410
2738
|
return [];
|
|
@@ -2610,7 +2938,7 @@ function pipelineRunReceivedLog(props) {
|
|
|
2610
2938
|
message: "Pipeline run request received",
|
|
2611
2939
|
metadata: cleanMetadata({
|
|
2612
2940
|
stream: props.stream,
|
|
2613
|
-
inputBytes:
|
|
2941
|
+
inputBytes: byteLength2(formatUnknown(props.input)),
|
|
2614
2942
|
metadataKeys: Object.keys(props.metadata ?? {})
|
|
2615
2943
|
})
|
|
2616
2944
|
};
|
|
@@ -2640,7 +2968,7 @@ function pipelineRunCompletedLog(props) {
|
|
|
2640
2968
|
message: "Pipeline run completed",
|
|
2641
2969
|
metadata: cleanMetadata({
|
|
2642
2970
|
durationMs: props.durationMs,
|
|
2643
|
-
outputBytes:
|
|
2971
|
+
outputBytes: byteLength2(formatUnknown(props.output))
|
|
2644
2972
|
})
|
|
2645
2973
|
};
|
|
2646
2974
|
}
|
|
@@ -2726,7 +3054,7 @@ function cleanMetadata(value) {
|
|
|
2726
3054
|
Object.entries(value).filter(([, item]) => item !== void 0)
|
|
2727
3055
|
);
|
|
2728
3056
|
}
|
|
2729
|
-
function
|
|
3057
|
+
function byteLength2(value) {
|
|
2730
3058
|
return value === void 0 ? void 0 : new TextEncoder().encode(value).length;
|
|
2731
3059
|
}
|
|
2732
3060
|
function formatUnknown(value) {
|
|
@@ -4019,7 +4347,7 @@ function runCompletedLog(props) {
|
|
|
4019
4347
|
metadata: cleanMetadata2({
|
|
4020
4348
|
durationMs: props.durationMs,
|
|
4021
4349
|
usage: usageSummary(props.usage),
|
|
4022
|
-
outputBytes:
|
|
4350
|
+
outputBytes: byteLength3(props.output),
|
|
4023
4351
|
messageCount: props.messageCount
|
|
4024
4352
|
})
|
|
4025
4353
|
};
|
|
@@ -4083,7 +4411,7 @@ function logsFromStreamEvent(props) {
|
|
|
4083
4411
|
turn: event.turn,
|
|
4084
4412
|
toolName: event.toolCall.function.name,
|
|
4085
4413
|
callId: event.toolCall.callId ?? event.toolCall.id,
|
|
4086
|
-
argumentBytes:
|
|
4414
|
+
argumentBytes: byteLength3(formatUnknown2(event.toolCall.function.arguments))
|
|
4087
4415
|
})
|
|
4088
4416
|
}
|
|
4089
4417
|
];
|
|
@@ -4102,8 +4430,8 @@ function logsFromStreamEvent(props) {
|
|
|
4102
4430
|
toolName: event.toolName,
|
|
4103
4431
|
callId: event.toolCallId,
|
|
4104
4432
|
internalCallId: event.internalCallId,
|
|
4105
|
-
argumentBytes:
|
|
4106
|
-
resultBytes:
|
|
4433
|
+
argumentBytes: byteLength3(event.args),
|
|
4434
|
+
resultBytes: byteLength3(event.result)
|
|
4107
4435
|
})
|
|
4108
4436
|
}
|
|
4109
4437
|
];
|
|
@@ -4156,7 +4484,7 @@ function logsFromStreamEvent(props) {
|
|
|
4156
4484
|
callId: event.approval.callId,
|
|
4157
4485
|
status: event.approval.status,
|
|
4158
4486
|
hasReason: event.approval.reason !== void 0,
|
|
4159
|
-
argumentBytes:
|
|
4487
|
+
argumentBytes: byteLength3(event.approval.args)
|
|
4160
4488
|
})
|
|
4161
4489
|
}
|
|
4162
4490
|
];
|
|
@@ -4195,7 +4523,7 @@ function logsFromStreamEvent(props) {
|
|
|
4195
4523
|
callId: event.question.callId,
|
|
4196
4524
|
status: event.question.status,
|
|
4197
4525
|
questionCount: event.question.questions.length,
|
|
4198
|
-
argumentBytes:
|
|
4526
|
+
argumentBytes: byteLength3(event.question.args)
|
|
4199
4527
|
})
|
|
4200
4528
|
}
|
|
4201
4529
|
];
|
|
@@ -4249,7 +4577,7 @@ function childAgentLog(event, sessionId, runId) {
|
|
|
4249
4577
|
childTurn: child.turn,
|
|
4250
4578
|
toolName: child.toolCall.function.name,
|
|
4251
4579
|
callId: child.toolCall.callId ?? child.toolCall.id,
|
|
4252
|
-
argumentBytes:
|
|
4580
|
+
argumentBytes: byteLength3(formatUnknown2(child.toolCall.function.arguments))
|
|
4253
4581
|
})
|
|
4254
4582
|
}
|
|
4255
4583
|
];
|
|
@@ -4271,7 +4599,7 @@ function childAgentLog(event, sessionId, runId) {
|
|
|
4271
4599
|
childTurn: child.turn,
|
|
4272
4600
|
toolName: child.toolName,
|
|
4273
4601
|
callId: child.toolCallId,
|
|
4274
|
-
resultBytes:
|
|
4602
|
+
resultBytes: byteLength3(child.result)
|
|
4275
4603
|
})
|
|
4276
4604
|
}
|
|
4277
4605
|
];
|
|
@@ -4309,7 +4637,7 @@ function childAgentLog(event, sessionId, runId) {
|
|
|
4309
4637
|
agentId: event.agentId,
|
|
4310
4638
|
hasAgentName: event.agentName !== void 0,
|
|
4311
4639
|
usage: usageSummary(child.usage),
|
|
4312
|
-
outputBytes:
|
|
4640
|
+
outputBytes: byteLength3(child.output),
|
|
4313
4641
|
messageCount: child.messages.length
|
|
4314
4642
|
})
|
|
4315
4643
|
}
|
|
@@ -4340,14 +4668,14 @@ function messageSummary(message) {
|
|
|
4340
4668
|
return {
|
|
4341
4669
|
role: "user",
|
|
4342
4670
|
contentKind: "text",
|
|
4343
|
-
byteLength:
|
|
4671
|
+
byteLength: byteLength3(message)
|
|
4344
4672
|
};
|
|
4345
4673
|
}
|
|
4346
4674
|
return {
|
|
4347
4675
|
role: message.role,
|
|
4348
4676
|
contentKind: Array.isArray(message.content) ? "parts" : "text",
|
|
4349
4677
|
partCount: Array.isArray(message.content) ? message.content.length : 1,
|
|
4350
|
-
byteLength:
|
|
4678
|
+
byteLength: byteLength3(formatUnknown2(message.content))
|
|
4351
4679
|
};
|
|
4352
4680
|
}
|
|
4353
4681
|
function usageSummary(value) {
|
|
@@ -4391,7 +4719,7 @@ function cleanJsonValue(value) {
|
|
|
4391
4719
|
function numericValue(value) {
|
|
4392
4720
|
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
4393
4721
|
}
|
|
4394
|
-
function
|
|
4722
|
+
function byteLength3(value) {
|
|
4395
4723
|
return value === void 0 ? 0 : new TextEncoder().encode(value).byteLength;
|
|
4396
4724
|
}
|
|
4397
4725
|
function formatUnknown2(value) {
|