File size: 1,502 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { ErrorWithMetadata, UnknownRecord } from "../shared/types";
import type { TokenCharCounts } from "../shared/tokens";
import type {
	AttachmentFileRef,
	AttachmentUploadResult,
} from "../attachments/types";

export type FileRef = AttachmentFileRef;

export type ToolDef = {
	name: string;
	description?: string;
	parameters?: unknown;
};

export type PromptWithTokens = {
	text: string;
	tokens: number;
	counts?: unknown;
};

export type PromptMetadata = {
	hasToolPrompt?: boolean;
	hasToolInstructions?: boolean;
};

export type AttachmentResolutionResult = AttachmentUploadResult;

export type ImageResolutionResult = AttachmentResolutionResult;

export type FileResolutionResult = AttachmentResolutionResult;

export type ContextFileResult = {
	fileRefs: FileRef[];
	prompt: string;
	promptTokenCounts: TokenCharCounts & { hasText: boolean };
	promptTokenText: string;
};

export type ContextFileFailure = {
	error: ErrorWithMetadata;
};

export type PreparedGeminiContext = {
	toolDefs: ToolDef[];
	toolChoiceInstruction: string;
	prompt: string;
	promptTokens: number;
	fileRefs: FileRef[] | null;
	contextFiles: ContextFileResult | null;
	promptMetadata?: PromptMetadata;
};

export type GeminiContextPrepareResult =
	| PreparedGeminiContext
	| ContextFileFailure;

export type LooseRequest = UnknownRecord;

export function hasCompletionError<T>(
	value: T | ContextFileFailure,
): value is ContextFileFailure {
	return !!value && typeof value === "object" && "error" in value;
}