Skip to content

Commit 273bca1

Browse files
committed
♻️ refactor: extract truncate and extractTextFromParts to utils module
Move utility functions to dedicated src/utils.ts module for better code organization and reusability across the codebase. Closes: warpdotdev#10
1 parent 70b192d commit 273bca1

3 files changed

Lines changed: 18 additions & 16 deletions

File tree

src/index.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
import type { Plugin } from "@opencode-ai/plugin"
2-
import type { Event, Part, Permission } from "@opencode-ai/sdk"
2+
import type { Event, Permission } from "@opencode-ai/sdk"
33

44
import { buildPayload } from "./payload"
55
import { warpNotify } from "./notify"
6+
import { truncate, extractTextFromParts } from "./utils"
67
import pkg from "../package.json" with { type: "json" }
78

89
const PLUGIN_VERSION = pkg.version
910
const NOTIFICATION_TITLE = "warp://cli-agent"
1011

11-
export function truncate(str: string, maxLen: number): string {
12-
if (str.length <= maxLen) return str
13-
return str.slice(0, maxLen - 3) + "..."
14-
}
15-
16-
export function extractTextFromParts(parts: Part[]): string {
17-
return parts
18-
.filter((p): p is Part & { type: "text"; text: string } =>
19-
p.type === "text" && "text" in p && Boolean(p.text),
20-
)
21-
.map((p) => p.text)
22-
.join(" ")
23-
}
24-
2512
function sendPermissionNotification(perm: Permission, cwd: string): void {
2613
const sessionId = perm.sessionID
2714
const toolName = perm.type || "unknown"

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Part } from "@opencode-ai/sdk"
2+
3+
export function truncate(str: string, maxLen: number): string {
4+
if (str.length <= maxLen) return str
5+
return str.slice(0, maxLen - 3) + "..."
6+
}
7+
8+
export function extractTextFromParts(parts: Part[]): string {
9+
return parts
10+
.filter((p): p is Part & { type: "text"; text: string } =>
11+
p.type === "text" && "text" in p && Boolean(p.text),
12+
)
13+
.map((p) => p.text)
14+
.join(" ")
15+
}

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it } from "node:test"
22
import assert from "node:assert/strict"
3-
import { truncate, extractTextFromParts } from "../src/index"
3+
import { truncate, extractTextFromParts } from "../src/utils"
44
import { buildPayload } from "../src/payload"
55

66
describe("truncate", () => {

0 commit comments

Comments
 (0)