aboutsummaryrefslogtreecommitdiff
path: root/packages/tools/src/vercel/util.ts
blob: 1c06ec165d405c5e5067f95c77512dd51c7612cc (plain) (blame)
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
66
67
68
69
70
71
72
import type { LanguageModelV2CallOptions, LanguageModelV2Message } from "@ai-sdk/provider"

export interface ProfileStructure {
	profile: {
		static?: string[]
		dynamic?: string[]
	}
	searchResults: {
		results: [
			{
				memory: string
			},
		]
	}
}

export type OutputContentItem =
	| { type: "text"; text: string }
	| { type: "reasoning"; text: string }
	| {
			type: "tool-call"
			id: string
			function: { name: string; arguments: string }
	  }
	| { type: "file"; name: string; mediaType: string; data: string }
	| {
			type: "source"
			sourceType: string
			id: string
			url: string
			title: string
	  }

/**
 * Convert ProfileStructure to markdown
 * based on profile.static and profile.dynamic properties
 * @param data ProfileStructure
 * @returns Markdown string
 */
export function convertProfileToMarkdown(data: ProfileStructure): string {
	const sections: string[] = []

	if (data.profile.static && data.profile.static.length > 0) {
		sections.push("## Static Profile")
		sections.push(data.profile.static.map((item) => `- ${item}`).join("\n"))
	}

	if (data.profile.dynamic && data.profile.dynamic.length > 0) {
		sections.push("## Dynamic Profile")
		sections.push(data.profile.dynamic.map((item) => `- ${item}`).join("\n"))
	}

	return sections.join("\n\n")
}

export const getLastUserMessage = (params: LanguageModelV2CallOptions) => {
	const lastUserMessage = params.prompt
		.slice()
		.reverse()
		.find((prompt: LanguageModelV2Message) => prompt.role === "user")
	const memories = lastUserMessage?.content
		.filter((content) => content.type === "text")
		.map((content) => content.text)
		.join(" ")
	return memories
}


export const filterOutSupermemories = (content: string) => {
	return content.split("User Supermemories: ")[0]
}