skillfree 0.1.18 โ†’ 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skillfree",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "๐Ÿฆž ไธ€ไธช API๏ผŒๆปก่ถณๆ‰€ๆœ‰้พ™่™พๆŠ€่ƒฝ้œ€ๆฑ‚",
5
5
  "main": "bin/skillfree.js",
6
6
  "bin": {
@@ -1,4 +1,4 @@
1
- const { post, postStream, get, request, getApiKey, BASE_URL } = require('../lib/client')
1
+ const { post, postStream, get, request, getApiKey, BASE_URL, checkCredits } = require('../lib/client')
2
2
  const fs = require('fs')
3
3
  const path = require('path')
4
4
 
@@ -30,6 +30,9 @@ async function pilot(flags) {
30
30
  const output = flags.output || null
31
31
  const model = flags.model || null
32
32
 
33
+ // โ”€โ”€ ็งฏๅˆ†้ข„ๆฃ€๏ผˆ< 100 ็งฏๅˆ†ๆ—ถๆ‹ฆๆˆชๅนถๆ็คบๅ……ๅ€ผ๏ผ‰โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
34
+ await checkCredits()
35
+
33
36
  // โ”€โ”€ CHAT โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
34
37
  if (type === 'chat') {
35
38
  if (!output) {
@@ -31,6 +31,51 @@ function getApiKey() {
31
31
  return _config.apiKey || process.env.SKILLFREE_API_KEY || ''
32
32
  }
33
33
 
34
+ // โ”€โ”€โ”€ ไฝŽ็งฏๅˆ†ๆ้†’ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
35
+ const LOW_CREDITS_THRESHOLD = 100
36
+
37
+ function formatLowCreditsWarning(credits) {
38
+ return `
39
+ โš ๏ธ ็งฏๅˆ†ไฝ™้ขๅไฝŽ๏ผˆๅฝ“ๅ‰็งฏๅˆ†๏ผš${credits}๏ผ‰๏ผŒๅปบ่ฎฎๅฐฝๅฟซๅ……ๅ€ผ๏ผŒ้ฟๅ…ไธญๆ–ญไฝฟ็”จใ€‚
40
+
41
+ ๐Ÿ’ณ ๅ……ๅ€ผๆต็จ‹๏ผš
42
+ 1. ๆ‰“ๅผ€ๆต่งˆๅ™จ๏ผŒ่ฎฟ้—ฎ https://skillfree.tech/app/billing
43
+ 2. ้€‰ๆ‹ฉๅ……ๅ€ผๅฅ—้ค๏ผˆๆŽจ่ ยฅ30 = 350็งฏๅˆ†๏ผŒๆ€งไปทๆฏ”ๆœ€้ซ˜๏ผ‰
44
+ 3. ๅพฎไฟกๆ‰ซ็ ๆ”ฏไป˜๏ผŒ็งฏๅˆ†่‡ชๅŠจๅˆฐ่ดฆ๏ผˆ็บฆ 3 ็ง’๏ผ‰
45
+
46
+ ๐Ÿ“ฆ ๅฅ—้คๅ‚่€ƒ๏ผš
47
+ ยฅ10 โ†’ 100 ็งฏๅˆ†
48
+ ยฅ30 โ†’ 350 ็งฏๅˆ†๏ผˆๅคš้€ 50๏ผ‰
49
+ ยฅ100 โ†’ 1300 ็งฏๅˆ†๏ผˆๅคš้€ 300๏ผ‰
50
+ ยฅ300 โ†’ 4500 ็งฏๅˆ†๏ผˆๅคš้€ 1500๏ผ‰
51
+ `
52
+ }
53
+
54
+ /**
55
+ * ไธปๅŠจๆฃ€ๆŸฅ็งฏๅˆ†ไฝ™้ข๏ผš
56
+ * < LOW_CREDITS_THRESHOLD โ†’ ๆ‰“ๅฐ่ญฆๅ‘Š๏ผŒไฝ†็ปง็ปญๆ‰ง่กŒ๏ผˆไธๆŠ›้”™๏ผ‰
57
+ * ็œŸๆญฃ็งฏๅˆ†ไธๅคŸๆ‰ฃๆ—ถ็”ฑๆœๅŠกๅ™จ่ฟ”ๅ›ž 402๏ผŒๅœจ request() ้‡Œๅค„็†
58
+ */
59
+ async function checkCredits() {
60
+ const apiKey = getApiKey()
61
+ if (!apiKey) return // ๆœช็™ปๅฝ•ๆ—ถๆ”พ่กŒ๏ผŒ็”ฑ request ้‡Œ็ปŸไธ€ๆŠฅ้”™
62
+ try {
63
+ const res = await fetch(`${API_BASE}/balance`, {
64
+ headers: { 'Authorization': `Bearer ${apiKey}` },
65
+ })
66
+ if (res.ok) {
67
+ const data = await res.json()
68
+ const credits = data.credits ?? data.balance ?? 0
69
+ if (credits < LOW_CREDITS_THRESHOLD) {
70
+ // ๅชๆ‰“ๅฐ่ญฆๅ‘Š๏ผŒไธๆŠ›้”™๏ผŒ็ปง็ปญๆ‰ง่กŒ
71
+ console.warn(formatLowCreditsWarning(credits))
72
+ }
73
+ }
74
+ } catch (e) {
75
+ // ็ฝ‘็ปœๅคฑ่ดฅ็ญ‰ๅฟฝ็•ฅ๏ผŒไธ้˜ปๅกž็”จๆˆท
76
+ }
77
+ }
78
+
34
79
  // โ”€โ”€โ”€ HTTP ๅทฅๅ…ท โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
35
80
  async function request(endpoint, options = {}) {
36
81
  const apiKey = getApiKey()
@@ -48,7 +93,7 @@ async function request(endpoint, options = {}) {
48
93
  },
49
94
  })
50
95
 
51
- if (res.status === 402) throw new Error('็งฏๅˆ†ไธ่ถณ๏ผŒ่ฏทๅ‰ๅพ€ https://skillfree.tech/app ๅ……ๅ€ผ')
96
+ if (res.status === 402) throw new Error(`โŒ ็งฏๅˆ†ไธ่ถณ๏ผŒๆœฌๆฌก่ฐƒ็”จๆ— ๆณ•ๅฎŒๆˆใ€‚\n\n๐Ÿ’ณ ๅ……ๅ€ผๆต็จ‹๏ผš\n 1. ่ฎฟ้—ฎ https://skillfree.tech/app/billing\n 2. ้€‰ๆ‹ฉๅฅ—้ค๏ผˆๆŽจ่ ยฅ30 = 350็งฏๅˆ†๏ผ‰๏ผŒๅพฎไฟกๆ‰ซ็ ๆ”ฏไป˜๏ผŒ็บฆ 3 ็ง’ๅˆฐ่ดฆ`)
52
97
  if (res.status === 401) throw new Error('API Key ๆ— ๆ•ˆๆˆ–ๅทฒ่ฟ‡ๆœŸ๏ผŒ่ฏท้‡ๆ–ฐ็™ปๅฝ•: skillfree auth login')
53
98
 
54
99
  return res
@@ -89,4 +134,5 @@ module.exports = {
89
134
  post,
90
135
  postStream,
91
136
  get,
137
+ checkCredits,
92
138
  }