Skip to content

Commit 197fb7d

Browse files
committed
fix: code review cleanup, pin deps, update descriptions, fix formatting
1 parent 7fe0d6d commit 197fb7d

9 files changed

Lines changed: 112 additions & 53 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
node_modules/
44
dist/
55
.env
6+
.cache

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "0.0.1",
55
"exports": "./search.ts",
66
"repository": "https://github.com/whaaaley/opencode-search",
7-
"description": "OpenCode plugin that provides web search, inter-instance chat, and session management tools.",
7+
"description": "OpenCode plugin that provides web search tools using Google CSE and DuckDuckGo.",
88
"tasks": {
99
"test": "deno test --allow-read --allow-env --allow-write --allow-net",
1010
"fmt": "deno run -A npm:dprint fmt"

deno.lock

Lines changed: 75 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "opencode-search",
33
"version": "0.0.1",
4-
"description": "OpenCode plugin that provides web search, inter-instance chat, and session management tools.",
4+
"description": "OpenCode plugin that provides web search tools using Google CSE and DuckDuckGo.",
55
"keywords": [
66
"opencode",
77
"opencode-plugin",
@@ -20,7 +20,7 @@
2020
"url": "git+https://github.com/whaaaley/opencode-search.git"
2121
},
2222
"license": "MIT",
23-
"author": "Dustin Whalen",
23+
"author": "Dustin Dowell",
2424
"type": "module",
2525
"exports": {
2626
".": {
@@ -46,7 +46,7 @@
4646
},
4747
"devDependencies": {
4848
"@opencode-ai/plugin": "1.1.12",
49-
"@types/jsdom": "^27.0.0",
49+
"@types/jsdom": "27.0.0",
5050
"@types/node": "^25.1.0",
5151
"typescript": "5.9.3"
5252
},

search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from '@opencode-ai/plugin'
2-
import { webSearchTool, googleSearchTool, ddgSearchTool } from './src/search.ts'
2+
import { ddgSearchTool, googleSearchTool, webSearchTool } from './src/search.ts'
33

44
// deno-lint-ignore require-await
55
const plugin: Plugin = async () => {

src/search.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
import { tool } from '@opencode-ai/plugin'
2-
import { googleSearch } from './utils/google-search.ts'
32
import { ddgSearch } from './utils/ddg-search.ts'
3+
import { googleSearch } from './utils/google-search.ts'
44

55
// deno-lint-ignore no-explicit-any
66
const formatGoogleResults = (results: any): string => {
7+
const request = results.queries && results.queries.request
8+
const totalResults = request && request[0] && request[0].totalResults
9+
10+
// deno-lint-ignore no-explicit-any
11+
const items = results.items
12+
? results.items.map((item: any) => ({
13+
title: item.title,
14+
link: item.link,
15+
snippet: item.snippet,
16+
}))
17+
: []
18+
719
return JSON.stringify(
820
{
921
source: 'google',
10-
totalResults: results.queries?.request?.[0]?.totalResults,
11-
items:
12-
// deno-lint-ignore no-explicit-any
13-
results.items?.map((item: any) => ({
14-
title: item.title,
15-
link: item.link,
16-
snippet: item.snippet,
17-
})) || [],
22+
totalResults,
23+
items,
1824
},
1925
null,
2026
2,
2127
)
2228
}
2329

24-
// Shared result formatter for DDG results
2530
const formatDdgResults = (textContent: string) => {
2631
return JSON.stringify(
2732
{
@@ -65,8 +70,7 @@ export const webSearchTool = tool({
6570
})
6671

6772
export const googleSearchTool = tool({
68-
description:
69-
'Search Google Custom Search Engine and return results with titles, links, and snippets',
73+
description: 'Search Google Custom Search Engine and return results with titles, links, and snippets',
7074
args: {
7175
query: tool.schema.string().describe('The search query'),
7276
},

src/utils/cache.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync } from 'node:fs'
2-
import { readFile, writeFile, mkdir } from 'node:fs/promises'
3-
import { join, dirname } from 'node:path'
2+
import { readFile, writeFile } from 'node:fs/promises'
3+
import { dirname, join } from 'node:path'
44
import { fileURLToPath } from 'node:url'
55

66
const CACHE_FILE = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '.cache')
@@ -34,10 +34,6 @@ const loadCache = async () => {
3434
}
3535

3636
const saveCache = async () => {
37-
const dir = dirname(CACHE_FILE)
38-
if (!existsSync(dir)) {
39-
await mkdir(dir, { recursive: true })
40-
}
4137
await writeFile(CACHE_FILE, JSON.stringify(store, null, 2))
4238
}
4339

src/utils/ddg-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { JSDOM, VirtualConsole } from 'jsdom'
21
import { Readability } from '@mozilla/readability'
2+
import { JSDOM, VirtualConsole } from 'jsdom'
33
import * as cache from './cache.ts'
44

55
const DDG_URL = 'https://html.duckduckgo.com/html/'

src/utils/google-search.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
import { env } from './env.ts'
21
import * as cache from './cache.ts'
3-
4-
export type SearchRequestInfo = {
5-
title: string
6-
totalResults: string
7-
searchTerms: string
8-
count: number
9-
startIndex: number
10-
inputEncoding: string
11-
outputEncoding: string
12-
safe: string
13-
cx: string
14-
}
2+
import { env } from './env.ts'
153

164
export type SearchItem = {
175
kind: string
@@ -25,19 +13,12 @@ export type SearchItem = {
2513
htmlFormattedUrl: string
2614
}
2715

28-
export type SearchQueries = {
29-
request: Array<SearchRequestInfo>
30-
}
31-
32-
export type SearchUrl = {
33-
type: string
34-
template: string
35-
}
36-
3716
export type SearchResult = {
3817
kind: string
39-
url: SearchUrl
40-
queries: SearchQueries
18+
// deno-lint-ignore no-explicit-any
19+
url: any
20+
// deno-lint-ignore no-explicit-any
21+
queries: any
4122
items: Array<SearchItem>
4223
}
4324

@@ -51,7 +32,7 @@ export const googleSearch = async (query: string): Promise<SearchResult> => {
5132
const cacheKey = 'search:' + query
5233

5334
const cached = await cache.get(cacheKey)
54-
if (cached && cached.items?.length > 0) {
35+
if (cached && cached.items && cached.items.length > 0) {
5536
return cached
5637
}
5738

@@ -73,7 +54,9 @@ export const googleSearch = async (query: string): Promise<SearchResult> => {
7354
if (!res.ok) {
7455
const errorMessage = json.error?.message || 'Unknown error'
7556
const errorCode = json.error?.code || res.status
76-
throw new Error('Google Search API error (' + errorCode + '): ' + errorMessage)
57+
throw new Error(
58+
['Google Search API error (', errorCode, '): ', errorMessage].join(''),
59+
)
7760
}
7861

7962
if (!json.items || json.items.length === 0) {
@@ -86,7 +69,9 @@ export const googleSearch = async (query: string): Promise<SearchResult> => {
8669
})
8770

8871
if (filtered.length === 0) {
89-
throw new Error('Google Search returned results but all were from blocked domains for: ' + query)
72+
throw new Error(
73+
'Google Search returned results but all were from blocked domains for: ' + query,
74+
)
9075
}
9176

9277
const result: SearchResult = { ...json, items: filtered }

0 commit comments

Comments
 (0)