Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerRuntime.ts

137 lines5.3 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
4​
5const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
6​
7const RuntimeTurnState = Schema.Literals(["completed", "failed", "interrupted", "cancelled"]);
8export type RuntimeTurnState = typeof RuntimeTurnState.Type;
9​
10const RuntimeItemStatus = Schema.Literals(["inProgress", "completed", "failed", "declined"]);
11export type RuntimeItemStatus = typeof RuntimeItemStatus.Type;
12​
13const TOOL_LIFECYCLE_ITEM_TYPES = [
14 "command_execution",
15 "file_change",
16 "mcp_tool_call",
17 "dynamic_tool_call",
18 "collab_agent_tool_call",
19 "web_search",
20 "image_view",
21] as const;
22​
23export const ToolLifecycleItemType = Schema.Literals(TOOL_LIFECYCLE_ITEM_TYPES);
24export type ToolLifecycleItemType = typeof ToolLifecycleItemType.Type;
25​
26export function isToolLifecycleItemType(value: string): value is ToolLifecycleItemType {
27 return TOOL_LIFECYCLE_ITEM_TYPES.includes(value as ToolLifecycleItemType);
29​
30export const ThreadTokenUsageSnapshot = Schema.Struct({
31 usedTokens: NonNegativeInt,
32 totalProcessedTokens: Schema.optional(NonNegativeInt),
33 maxTokens: Schema.optional(PositiveInt),
34 inputTokens: Schema.optional(NonNegativeInt),
35 cachedInputTokens: Schema.optional(NonNegativeInt),
36 outputTokens: Schema.optional(NonNegativeInt),
37 reasoningOutputTokens: Schema.optional(NonNegativeInt),
38 lastUsedTokens: Schema.optional(NonNegativeInt),
39 lastInputTokens: Schema.optional(NonNegativeInt),
40 lastCachedInputTokens: Schema.optional(NonNegativeInt),
41 lastOutputTokens: Schema.optional(NonNegativeInt),
42 lastReasoningOutputTokens: Schema.optional(NonNegativeInt),
43 toolUses: Schema.optional(NonNegativeInt),
44 durationMs: Schema.optional(NonNegativeInt),
45 compactsAutomatically: Schema.optional(Schema.Boolean),
46 autoCompactThreshold: Schema.optional(PositiveInt),
47 cost: Schema.optional(
48 Schema.Struct({
49 amount: Schema.Number.check(Schema.isFinite()),
50 currency: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(32)),
51 }),
52 ),
53});
54export type ThreadTokenUsageSnapshot = typeof ThreadTokenUsageSnapshot.Type;
55​
56// Input includes cache reads/writes; output includes the optional reasoningTokens subset.
57const TurnTokenUsageCommonFields = {
58 usageScope: Schema.Literal("main_agent"),
59 cachedInputTokens: Schema.optional(NonNegativeInt),
60 cacheCreationTokens: Schema.optional(NonNegativeInt),
61 reasoningTokens: Schema.optional(NonNegativeInt),
62 hasSubagents: Schema.Boolean,
63};
64export const TurnTokenUsage = Schema.Union([
65 Schema.Struct({
66 ...TurnTokenUsageCommonFields,
67 usageStatus: Schema.Literal("complete"),
68 inputTokens: NonNegativeInt,
69 outputTokens: NonNegativeInt,
70 }),
71 Schema.Struct({
72 ...TurnTokenUsageCommonFields,
73 usageStatus: Schema.Literals(["partial", "unavailable"]),
74 inputTokens: Schema.optional(NonNegativeInt),
75 outputTokens: Schema.optional(NonNegativeInt),
76 }),
77]);
78export type TurnTokenUsage = typeof TurnTokenUsage.Type;
79​
80export const ToolActivitySurface = Schema.Literals(["browser", "computer"]);
81export type ToolActivitySurface = typeof ToolActivitySurface.Type;
82​
83export const ToolActivityNativeAppReference = Schema.Union([
84 Schema.TaggedStruct("app-id", {
85 appId: TrimmedNonEmptyStringSchema.check(
86 Schema.isMaxLength(512),
87 Schema.isPattern(/^[A-Za-z0-9._-]+$/u),
88 ),
89 }),
90 Schema.TaggedStruct("display-name", {
91 displayName: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(160)),
92 }),
93]);
94export type ToolActivityNativeAppReference = typeof ToolActivityNativeAppReference.Type;
95​
96export const ToolActivityIcon = Schema.Union([
97 Schema.TaggedStruct("website", {
98 pageUrl: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(4096)),
99 faviconUrl: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(4096))),
100 faviconUrlDark: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(4096))),
101 }),
102 Schema.TaggedStruct("native-app", {
103 app: ToolActivityNativeAppReference,
104 }),
105 Schema.TaggedStruct("themed-logo", {
106 logoUrl: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(4096)),
107 logoUrlDark: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(4096))),
108 }),
109]);
110export type ToolActivityIcon = typeof ToolActivityIcon.Type;
111​
112export const ToolActivitySource = Schema.Struct({
113 key: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(512)),
114 name: TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(160)),
115 kind: Schema.Literals(["browser", "computer", "integration"]),
116 icon: Schema.optional(ToolActivityIcon),
117});
118export type ToolActivitySource = typeof ToolActivitySource.Type;
119​
120const UserInputQuestionOption = Schema.Struct({
121 label: TrimmedNonEmptyStringSchema,
122 description: Schema.String,
123 value: Schema.optional(Schema.String),
124});
125​
126export const UserInputQuestion = Schema.Struct({
127 id: TrimmedNonEmptyStringSchema,
128 header: TrimmedNonEmptyStringSchema,
129 question: TrimmedNonEmptyStringSchema,
130 options: Schema.Array(UserInputQuestionOption),
131 allowCustomAnswer: Schema.optional(Schema.Boolean),
132 initialAnswer: Schema.optional(Schema.String),
133 multiSelect: Schema.optional(Schema.Boolean).pipe(
134 Schema.withConstructorDefault(Effect.succeed(false)),
135 ),
136});
137export type UserInputQuestion = typeof UserInputQuestion.Type;